1# SPDX-License-Identifier: GPL-2.0 2 3import json 4import os 5import random 6import re 7import time 8from .utils import cmd, ip 9 10 11class NetdevSim: 12 """ 13 Class for netdevsim netdevice and its attributes. 14 """ 15 16 def __init__(self, nsimdev, port_index, ifname, ns=None): 17 # In case udev renamed the netdev to according to new schema, 18 # check if the name matches the port_index. 19 nsimnamere = re.compile(r"eni\d+np(\d+)") 20 match = nsimnamere.match(ifname) 21 if match and int(match.groups()[0]) != port_index + 1: 22 raise Exception("netdevice name mismatches the expected one") 23 24 self.ifname = ifname 25 self.nsimdev = nsimdev 26 self.port_index = port_index 27 self.ns = ns 28 self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index) 29 ret = ip("-j link show dev %s" % ifname, ns=ns) 30 self.dev = json.loads(ret.stdout)[0] 31 32 def dfs_write(self, path, val): 33 self.nsimdev.dfs_write(f'ports/{self.port_index}/' + path, val) 34 35 36class NetdevSimDev: 37 """ 38 Class for netdevsim bus device and its attributes. 39 """ 40 @staticmethod 41 def ctrl_write(path, val): 42 fullpath = os.path.join("/sys/bus/netdevsim/", path) 43 with open(fullpath, "w") as f: 44 f.write(val) 45 46 def dfs_write(self, path, val): 47 fullpath = os.path.join(f"/sys/kernel/debug/netdevsim/netdevsim{self.addr}/", path) 48 with open(fullpath, "w") as f: 49 f.write(val) 50 51 def __init__(self, port_count=1, ns=None): 52 # nsim will spawn in init_net, we'll set to actual ns once we switch it there 53 self.ns = None 54 55 if not os.path.exists("/sys/bus/netdevsim"): 56 cmd("modprobe netdevsim") 57 58 addr = random.randrange(1 << 15) 59 while True: 60 try: 61 self.ctrl_write("new_device", "%u %u" % (addr, port_count)) 62 except OSError as e: 63 if e.errno == errno.ENOSPC: 64 addr = random.randrange(1 << 15) 65 continue 66 raise e 67 break 68 self.addr = addr 69 70 # As probe of netdevsim device might happen from a workqueue, 71 # so wait here until all netdevs appear. 72 self.wait_for_netdevs(port_count) 73 74 if ns: 75 cmd(f"devlink dev reload netdevsim/netdevsim{addr} netns {ns.name}") 76 self.ns = ns 77 78 cmd("udevadm settle", ns=self.ns) 79 ifnames = self.get_ifnames() 80 81 self.dfs_dir = "/sys/kernel/debug/netdevsim/netdevsim%u/" % addr 82 83 self.nsims = [] 84 for port_index in range(port_count): 85 self.nsims.append(self._make_port(port_index, ifnames[port_index])) 86 87 def _make_port(self, port_index, ifname): 88 return NetdevSim(self, port_index, ifname, self.ns) 89 90 def get_ifnames(self): 91 ifnames = [] 92 listdir = cmd(f"ls /sys/bus/netdevsim/devices/netdevsim{self.addr}/net/", 93 ns=self.ns).stdout.split() 94 for ifname in listdir: 95 ifnames.append(ifname) 96 ifnames.sort() 97 return ifnames 98 99 def wait_for_netdevs(self, port_count): 100 timeout = 5 101 timeout_start = time.time() 102 103 while True: 104 try: 105 ifnames = self.get_ifnames() 106 except FileNotFoundError as e: 107 ifnames = [] 108 if len(ifnames) == port_count: 109 break 110 if time.time() < timeout_start + timeout: 111 continue 112 raise Exception("netdevices did not appear within timeout") 113 114 def remove(self): 115 self.ctrl_write("del_device", "%u" % (self.addr, )) 116 117 def remove_nsim(self, nsim): 118 self.nsims.remove(nsim) 119 self.ctrl_write("devices/netdevsim%u/del_port" % (self.addr, ), 120 "%u" % (nsim.port_index, )) 121