Commit 5c425930 authored by Martín Ferrari's avatar Martín Ferrari

some minor improvements

parent 8ec800fe
...@@ -27,6 +27,17 @@ def get_if_data(): ...@@ -27,6 +27,17 @@ def get_if_data():
byidx[idx] = bynam[i.name] = i byidx[idx] = bynam[i.name] = i
return byidx, bynam return byidx, bynam
def get_if(iface):
ifdata = get_if_data()
if isinstance(iface, netns.interface.interface):
if iface.index != None:
return ifdata[0][iface.index]
else:
return ifdata[1][iface.name]
if isinstance(iface, int):
return ifdata[0][iface]
return ifdata[1][iface]
def get_addr_data(): def get_addr_data():
ipcmd = subprocess.Popen(["ip", "-o", "addr", "list"], ipcmd = subprocess.Popen(["ip", "-o", "addr", "list"],
stdout = subprocess.PIPE) stdout = subprocess.PIPE)
...@@ -80,11 +91,11 @@ def create_if_pair(if1, if2): ...@@ -80,11 +91,11 @@ def create_if_pair(if1, if2):
return interfaces[if1.name], interfaces[if2.name] return interfaces[if1.name], interfaces[if2.name]
def del_if(iface): def del_if(iface):
ifname = get_if_name(iface) ifname = _get_if_name(iface)
_execute(["ip", "link", "del", ifname]) _execute(["ip", "link", "del", ifname])
def set_if(iface, recover = True): def set_if(iface, recover = True):
orig_iface = get_real_if(iface) orig_iface = get_if(iface)
_ils = ["ip", "link", "set", "dev", orig_iface.name] _ils = ["ip", "link", "set", "dev", orig_iface.name]
diff = iface - orig_iface # Only set what's needed diff = iface - orig_iface # Only set what's needed
cmds = [] cmds = []
...@@ -119,11 +130,11 @@ def set_if(iface, recover = True): ...@@ -119,11 +130,11 @@ def set_if(iface, recover = True):
raise raise
def change_netns(iface, netns): def change_netns(iface, netns):
ifname = get_if_name(iface) ifname = _get_if_name(iface)
_execute(["ip", "link", "set", "dev", ifname, "netns", str(netns)]) _execute(["ip", "link", "set", "dev", ifname, "netns", str(netns)])
def add_addr(iface, address): def add_addr(iface, address):
ifname = get_if_name(iface) ifname = _get_if_name(iface)
addresses = get_addr_data()[1][ifname] addresses = get_addr_data()[1][ifname]
assert address not in addresses assert address not in addresses
...@@ -134,7 +145,7 @@ def add_addr(iface, address): ...@@ -134,7 +145,7 @@ def add_addr(iface, address):
_execute(cmd) _execute(cmd)
def del_addr(iface, address): def del_addr(iface, address):
ifname = get_if_name(iface) ifname = _get_if_name(iface)
addresses = get_addr_data()[1][ifname] addresses = get_addr_data()[1][ifname]
assert address in addresses assert address in addresses
...@@ -143,7 +154,7 @@ def del_addr(iface, address): ...@@ -143,7 +154,7 @@ def del_addr(iface, address):
_execute(cmd) _execute(cmd)
def set_addr(iface, addresses, recover = True): def set_addr(iface, addresses, recover = True):
ifname = get_if_name(iface) ifname = _get_if_name(iface)
addresses = get_addr_data()[1][ifname] addresses = get_addr_data()[1][ifname]
to_remove = set(orig_addresses) - set(addresses) to_remove = set(orig_addresses) - set(addresses)
to_add = set(addresses) - set(orig_addresses) to_add = set(addresses) - set(orig_addresses)
...@@ -174,21 +185,10 @@ def _execute(cmd): ...@@ -174,21 +185,10 @@ def _execute(cmd):
if p.returncode != 0: if p.returncode != 0:
raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err)) raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
def get_real_if(iface): def _get_if_name(iface):
ifdata = get_if_data()
if isinstance(iface, netns.interface.interface):
if iface.index != None:
return ifdata[0][iface.index]
else:
return ifdata[1][iface.name]
if isinstance(iface, int):
return ifdata[0][iface]
return ifdata[1][iface]
def get_if_name(iface):
if isinstance(iface, netns.interface.interface): if isinstance(iface, netns.interface.interface):
if iface.name != None: if iface.name != None:
return iface.name return iface.name
if isinstance(iface, str): if isinstance(iface, str):
return iface return iface
return get_real_if(iface).name return get_if(iface).name
...@@ -535,8 +535,7 @@ class Client(object): ...@@ -535,8 +535,7 @@ class Client(object):
def set_if(self, interface): def set_if(self, interface):
cmd = ["IF", "SET", interface.index] cmd = ["IF", "SET", interface.index]
for k in ("name", "mtu", "lladdr", "broadcast", "up", "multicast", for k in interface.changeable_attributes:
"arp"):
v = getattr(interface, k) v = getattr(interface, k)
if v != None: if v != None:
cmd += [k, str(v)] cmd += [k, str(v)]
......
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