summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorViacheslav <v.gletenko@vyos.io>2021-11-23 18:11:10 +0000
committerViacheslav <v.gletenko@vyos.io>2021-11-26 09:43:13 +0000
commit47584e030f3341b265e826643951648982cb20d0 (patch)
tree60a6861e2c84da01722ec39384b254be488c4c46 /python
parenta2fe5dc40d439a28deb4d09e78fcf93aba4555a4 (diff)
downloadvyos-1x-47584e030f3341b265e826643951648982cb20d0.tar.gz
vyos-1x-47584e030f3341b265e826643951648982cb20d0.zip
netns: T3829: Ability to configure network namespaces
Diffstat (limited to 'python')
-rwxr-xr-xpython/vyos/ifconfig/interface.py42
-rw-r--r--python/vyos/util.py18
2 files changed, 48 insertions, 12 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index 50da2553a..bcb692697 100755
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -37,6 +37,7 @@ from vyos.util import mac2eui64
from vyos.util import dict_search
from vyos.util import read_file
from vyos.util import get_interface_config
+from vyos.util import get_interface_namespace
from vyos.util import is_systemd_service_active
from vyos.template import is_ipv4
from vyos.template import is_ipv6
@@ -515,19 +516,33 @@ class Interface(Control):
if prev_state == 'up':
self.set_admin_state('up')
+ def del_netns(self, netns):
+ """
+ Remove interface from given NETNS.
+ """
+
+ # If NETNS does not exist then there is nothing to delete
+ if not os.path.exists(f'/run/netns/{netns}'):
+ return None
+
+ # As a PoC we only allow 'dummy' interfaces
+ if 'dum' not in self.ifname:
+ return None
+
+ # Check if interface realy exists in namespace
+ if get_interface_namespace(self.ifname) != None:
+ self._cmd(f'ip netns exec {get_interface_namespace(self.ifname)} ip link del dev {self.ifname}')
+ return
+
def set_netns(self, netns):
"""
- Add/Remove interface from given NETNS.
+ Add interface from given NETNS.
Example:
>>> from vyos.ifconfig import Interface
>>> Interface('dum0').set_netns('foo')
"""
- #tmp = self.get_interface('netns')
- #if tmp == netns:
- # return None
-
self.set_interface('netns', netns)
def set_vrf(self, vrf):
@@ -1371,6 +1386,16 @@ class Interface(Control):
if mac:
self.set_mac(mac)
+ # If interface is connected to NETNS we don't have to check all other
+ # settings like MTU/IPv6/sysctl values, etc.
+ # Since the interface is pushed onto a separate logical stack
+ # Configure NETNS
+ if dict_search('netns', config) != None:
+ self.set_netns(config.get('netns', ''))
+ return
+ else:
+ self.del_netns(config.get('netns', ''))
+
# Update interface description
self.set_alias(config.get('description', ''))
@@ -1423,13 +1448,6 @@ class Interface(Control):
# checked before
self.set_vrf(config.get('vrf', ''))
- # If interface attached to NETNS we shouldn't check all other settings
- # As interface placed to separate logical stack
- # Configure NETNS
- if dict_search('netns', config) != None:
- self.set_netns(config.get('netns', ''))
- return
-
# Configure MSS value for IPv4 TCP connections
tmp = dict_search('ip.adjust_mss', config)
value = tmp if (tmp != None) else '0'
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 9aa1f98d2..6c375e1a6 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -794,6 +794,24 @@ def get_interface_address(interface):
tmp = loads(cmd(f'ip -d -j addr show {interface}'))[0]
return tmp
+def get_interface_namespace(iface):
+ """
+ Returns wich netns the interface belongs to
+ """
+ from json import loads
+ # Check if netns exist
+ tmp = loads(cmd(f'ip --json netns ls'))
+ if len(tmp) == 0:
+ return None
+
+ for ns in tmp:
+ namespace = f'{ns["name"]}'
+ # Search interface in each netns
+ data = loads(cmd(f'ip netns exec {namespace} ip -j link show'))
+ for compare in data:
+ if iface == compare["ifname"]:
+ return namespace
+
def get_all_vrfs():
""" Return a dictionary of all system wide known VRF instances """
from json import loads