Commit ea98e50b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 12cbc920
#!/usr/bin/env python #!/usr/bin/env python
"""XXX doc""" """Program tapsplit brings tap interface into state with several children
interfaces each covering part of original interface address space.
Usage: tapsplit <interface> <nchildren>
"""
import netifaces import netifaces
import netaddr import netaddr
...@@ -14,10 +18,10 @@ import subprocess ...@@ -14,10 +18,10 @@ import subprocess
def main(): def main():
tap = sys.argv[1] tap = sys.argv[1]
n = int(sys.argv[2]) n = int(sys.argv[2])
assert n > 0, n assert n >= 0, n
# determine tap's address, network and owner # determine tap's address, network and owner
owner = readfile(sysnet(tap, 'owner')) .strip() owner = readfile(sysnet(tap) + '/owner') .strip()
addr = None addr = None
net = None net = None
prefixlen = None prefixlen = None
...@@ -46,6 +50,11 @@ def main(): ...@@ -46,6 +50,11 @@ def main():
print('%s: split %s by %d' % (tap, net, n)) print('%s: split %s by %d' % (tap, net, n))
# cleanup existing children
for ifname in netifaces.interfaces():
if ifname.startswith('%s-' % tap):
run('ip', 'link', 'del', ifname)
# see how much prefix bits we need to take to divide by n # see how much prefix bits we need to take to divide by n
n += 1 # also leaving first range for the original tap n += 1 # also leaving first range for the original tap
ptake = ceil(log2(n)) ptake = ceil(log2(n))
...@@ -56,8 +65,6 @@ def main(): ...@@ -56,8 +65,6 @@ def main():
continue # leave this range for original tap continue # leave this range for original tap
subtap = '%s-%d' % (tap, i) subtap = '%s-%d' % (tap, i)
print('-> %s %s' % (subtap, subnet)) print('-> %s %s' % (subtap, subnet))
if exists(sysnet(subtap)):
run('ip', 'link', 'del', subtap)
run('ip', 'tuntap', 'add', 'dev', subtap, 'mode', 'tap', 'user', owner) run('ip', 'tuntap', 'add', 'dev', subtap, 'mode', 'tap', 'user', owner)
run('ip', 'link', 'set', subtap, 'up') run('ip', 'link', 'set', subtap, 'up')
run('ip', 'addr', 'add', str(subnet), 'dev', subtap, 'noprefixroute') run('ip', 'addr', 'add', str(subnet), 'dev', subtap, 'noprefixroute')
...@@ -66,11 +73,8 @@ def main(): ...@@ -66,11 +73,8 @@ def main():
# sysnet returns path on /sys corresponding to given interface. # sysnet returns path on /sys corresponding to given interface.
def sysnet(ifname, subpath=None): def sysnet(ifname):
path = '/sys/devices/virtual/net/%s' % ifname return '/sys/devices/virtual/net/%s' % ifname
if subpath is not None:
path += '/'+subpath
return path
def run(*argv): def run(*argv):
print(' # %s' % ' '.join(argv)) print(' # %s' % ' '.join(argv))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment