aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2019-04-04 16:08:27 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2019-04-04 16:10:14 +0200
commit4c8cd7b9a58d9100fe49b16f281ff24d3e7f81db (patch)
tree0683be94f4554c2c9d7d543986fd0e002d2fdd28
parentb27827b7bc76cee7a573318e9d4f58d85cb2dd80 (diff)
modem: Move modem iface to its netns before using it
We used to do that once after ofono startup for all iface (modem-netns-setup.py), but then if a modem crashes the interface is re-created on the default netns, and tests fail until manual re-run of modem-netns-setup.py. This way now we always make sure the iface is moved to the expected netns before it is used. Related: OS#3881 Change-Id: I506309c424aa46684c4516a1a0217343ecbf32c6
-rw-r--r--src/osmo_gsm_tester/modem.py1
-rw-r--r--src/osmo_gsm_tester/util.py11
-rwxr-xr-xutils/osmo-gsm-tester_netns_setup.sh22
3 files changed, 34 insertions, 0 deletions
diff --git a/src/osmo_gsm_tester/modem.py b/src/osmo_gsm_tester/modem.py
index 28f7f04..7e58e32 100644
--- a/src/osmo_gsm_tester/modem.py
+++ b/src/osmo_gsm_tester/modem.py
@@ -677,6 +677,7 @@ class Modem(MS):
iface = ctx_settings.get('Interface', None)
if not iface:
raise log.Error('%s Settings contains no iface! %r' % (ctx_id, repr(ctx_settings)))
+ util.move_iface_to_netns(iface, self.netns(), self.run_dir.new_dir('move_netns'))
self.run_netns_wait('ifup', ('ip', 'link', 'set', 'dev', iface, 'up'))
self.run_netns_wait('dhcp', ('udhcpc', '-q', '-i', iface))
diff --git a/src/osmo_gsm_tester/util.py b/src/osmo_gsm_tester/util.py
index 21653be..b0ce041 100644
--- a/src/osmo_gsm_tester/util.py
+++ b/src/osmo_gsm_tester/util.py
@@ -100,6 +100,17 @@ def setcap_net_admin(binary, run_dir):
proc = Process(SETCAP_NET_ADMIN_BIN, run_dir, ['sudo', SETCAP_NET_ADMIN_BIN, binary])
proc.launch_sync()
+def move_iface_to_netns(ifname, netns, run_dir):
+ '''
+ Moves an iface to a netns. It creates the netns if it doesn't exist.
+ fail_iface_not_found=False is handy in order to assume the iface is already
+ in another netns and thus cannot be foud.
+ '''
+ from .process import Process
+ NETNS_SETUP_BIN = 'osmo-gsm-tester_netns_setup.sh'
+ proc = Process('move_netns', run_dir, ['sudo', NETNS_SETUP_BIN, ifname, netns])
+ proc.launch_sync()
+
def import_path_prepend(pathname):
dir = os.path.realpath(pathname)
if dir not in sys.path:
diff --git a/utils/osmo-gsm-tester_netns_setup.sh b/utils/osmo-gsm-tester_netns_setup.sh
new file mode 100755
index 0000000..1600c44
--- /dev/null
+++ b/utils/osmo-gsm-tester_netns_setup.sh
@@ -0,0 +1,22 @@
+#!/bin/bash -e
+
+ifname="$1"
+netns="$2"
+shift
+shift
+
+
+
+if [ -f "/var/run/netns/${netns}" ]; then
+ echo "netns $netns already exists"
+else
+ echo "Creating netns $netns"
+ ip netns add "$netns"
+fi
+
+if [ -d "/sys/class/net/${ifname}" ]; then
+ echo "Moving iface $ifname to netns $netns"
+ ip link set $ifname netns $netns
+else
+ ip netns exec $netns ls "/sys/class/net/${ifname}" >/dev/null && echo "iface $ifname already in netns $netns"
+fi