~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/net/lib/py/nsim.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php