Pau Espin Pedrol | e972828 | 2018-10-25 14:35:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Pau Espin Pedrol <pespin@sysmocom.de> |
| 3 | # MIT |
| 4 | |
| 5 | # manage netns for ofono modems |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | import subprocess |
| 10 | import usb.core |
| 11 | import usb.util |
| 12 | from pprint import pprint |
| 13 | |
| 14 | def get_path_ids(bus, port_numbers): |
| 15 | port_numbers = [str(port) for port in port_numbers] |
| 16 | ports = '.'.join(port_numbers) |
| 17 | return '{}-{}'.format(bus, ports) |
| 18 | |
| 19 | def get_usb_dir(bus, port_numbers): |
| 20 | return '/sys/bus/usb/devices/' + get_path_ids(bus, port_numbers) + '/' |
| 21 | |
| 22 | def get_net_from_usb(bus, port_numbers): |
| 23 | net_ifaces = [] |
| 24 | path = get_usb_dir(bus, port_numbers) |
| 25 | path_ids = get_path_ids(bus, port_numbers) |
| 26 | |
| 27 | usb_interfaces = [f for f in os.listdir(path) if f.startswith(path_ids)] |
| 28 | for usb_iface in usb_interfaces: |
| 29 | listdir = [f for f in os.listdir(path + usb_iface) if f == ('net')] |
| 30 | if listdir: |
| 31 | # found a net iface |
| 32 | net_ifaces += os.listdir(path + usb_iface + '/net/') |
| 33 | return net_ifaces |
| 34 | |
| 35 | def move_modem_to_netns(usb_path_id, net_li): |
| 36 | |
| 37 | if len(net_li) == 0: |
| 38 | print("%s: Device has no net ifaces, skipping" %(usb_path_id)) |
| 39 | return |
| 40 | |
| 41 | if not os.path.exists("/var/run/netns/%s" % usb_path_id): |
| 42 | print("%s: Creating netns" % (usb_path_id)) |
| 43 | subprocess.check_call(["ip", "netns", "add", usb_path_id]) |
| 44 | else: |
| 45 | print("%s: netns already exists" % (usb_path_id)) |
| 46 | |
| 47 | for netif in net_li: |
| 48 | print("%s: Moving iface %s to netns" % (usb_path_id, netif)) |
| 49 | subprocess.check_call(["ip", "link", "set", netif, "netns", usb_path_id]) |
| 50 | # iface Must be set up AFTER pdp ctx is activated, otherwise we get no DHCP response. |
| 51 | #print("%s: Setting up iface %s" % (usb_path_id, netif)) |
| 52 | #subprocess.check_call(["ip", "netns", "exec", usb_path_id, "ip", "link", "set", "dev", netif, "up"]) |
| 53 | #subprocess.check_call(["ip", "netns", "exec", usb_path_id, "udhcpc", "-i", netif]) |
| 54 | |
| 55 | def delete_modem_netns(usb_path_id): |
| 56 | if os.path.exists("/var/run/netns/%s" % usb_path_id): |
| 57 | print("%s: Deleting netns" % (usb_path_id)) |
| 58 | subprocess.check_call(["ip", "netns", "delete", usb_path_id]) |
| 59 | else: |
| 60 | print("%s: netns doesn't exist" % (usb_path_id)) |
| 61 | |
| 62 | def print_help(): |
| 63 | print("Usage: %s start|stop" % sys.argv[0]) |
| 64 | exit(1) |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | |
| 69 | if len(sys.argv) != 2: |
| 70 | print_help() |
| 71 | |
| 72 | USB_DEVS = [dev for dev in usb.core.find(find_all=True)] |
| 73 | RESULT = {} |
| 74 | for device in USB_DEVS: |
| 75 | result = {} |
| 76 | if not device.port_numbers: |
| 77 | continue |
| 78 | |
| 79 | usb_path_id = get_path_ids(device.bus, device.port_numbers) |
| 80 | net_li = get_net_from_usb(device.bus, device.port_numbers) |
| 81 | |
| 82 | if sys.argv[1] == "start": |
| 83 | move_modem_to_netns(usb_path_id, net_li) |
| 84 | elif sys.argv[1] == "stop": |
| 85 | delete_modem_netns(usb_path_id) |
| 86 | else: |
| 87 | print_help() |