3 from string import Template
6 from multiprocessing import Pool
7 from functools import cached_property
8 from TdcPlugin import TdcPlugin
10 from tdc_config import *
13 from pyroute2 import netns
14 from pyroute2 import IPRoute
18 print("!!! Consider installing pyroute2 !!!")
20 class SubPlugin(TdcPlugin):
22 self.sub_class = 'ns/SubPlugin'
25 def pre_suite(self, testcount, testlist):
26 super().pre_suite(testcount, testlist)
28 def prepare_test(self, test):
29 if 'skip' in test and test['skip'] == 'yes':
32 if 'nsPlugin' not in test['plugins']:
38 self._ipr2_ns_create()
40 # Make sure the netns is visible in the fs
47 ns = self.args.NAMES['NS']
48 f = open('/run/netns/{}'.format(ns))
56 def pre_case(self, test, test_skip):
58 print('{}.pre_case'.format(self.sub_class))
63 self.prepare_test(test)
67 print('{}.post_case'.format(self.sub_class))
72 self._ipr2_ns_destroy()
74 def post_suite(self, index):
76 print('{}.post_suite'.format(self.sub_class))
78 # Make sure we don't leak resources
79 cmd = self._replace_keywords("$IP -a netns del")
81 if self.args.verbose > 3:
82 print('_exec_cmd: command "{}"'.format(cmd))
84 subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
86 def adjust_command(self, stage, command):
87 super().adjust_command(stage, command)
92 print('{}.adjust_command'.format(self.sub_class))
94 if not isinstance(command, list):
96 cmdlist = command.split()
99 if stage == 'setup' or stage == 'execute' or stage == 'verify' or stage == 'teardown':
100 if self.args.verbose:
101 print('adjust_command: stage is {}; inserting netns stuff in command [{}] list [{}]'.format(stage, command, cmdlist))
102 cmdlist.insert(0, self.args.NAMES['NS'])
103 cmdlist.insert(0, 'exec')
104 cmdlist.insert(0, 'netns')
105 cmdlist.insert(0, self.args.NAMES['IP'])
110 command = ' '.join(cmdlist)
114 if self.args.verbose:
115 print('adjust_command: return command [{}]'.format(command))
118 def _nl_ns_create(self):
119 ns = self.args.NAMES["NS"];
120 dev0 = self.args.NAMES["DEV0"];
121 dev1 = self.args.NAMES["DEV1"];
122 dummy = self.args.NAMES["DUMMY"];
124 if self.args.verbose:
125 print('{}._nl_ns_create'.format(self.sub_class))
128 netns.pushns(newns=ns)
129 with IPRoute() as ip:
130 ip.link('add', ifname=dev1, kind='veth', peer={'ifname': dev0, 'net_ns_fd':'/proc/1/ns/net'})
131 ip.link('add', ifname=dummy, kind='dummy')
137 dev1_idx = ip.link_lookup(ifname=dev1)[0]
138 dummy_idx = ip.link_lookup(ifname=dummy)[0]
139 ip.link('set', index=dev1_idx, state='up')
140 ip.link('set', index=dummy_idx, state='up')
148 with IPRoute() as ip:
154 dev0_idx = ip.link_lookup(ifname=dev0)[0]
155 ip.link('set', index=dev0_idx, state='up')
162 def _ipr2_ns_create_cmds(self):
165 ns = self.args.NAMES['NS']
167 cmds.append(self._replace_keywords('netns add {}'.format(ns)))
168 cmds.append(self._replace_keywords('link add $DEV1 type veth peer name $DEV0'))
169 cmds.append(self._replace_keywords('link set $DEV1 netns {}'.format(ns)))
170 cmds.append(self._replace_keywords('link add $DUMMY type dummy'.format(ns)))
171 cmds.append(self._replace_keywords('link set $DUMMY netns {}'.format(ns)))
172 cmds.append(self._replace_keywords('netns exec {} $IP link set $DEV1 up'.format(ns)))
173 cmds.append(self._replace_keywords('netns exec {} $IP link set $DUMMY up'.format(ns)))
174 cmds.append(self._replace_keywords('link set $DEV0 up'.format(ns)))
177 cmds.append(self._replace_keywords('link set $DEV2 netns {}'.format(ns)))
178 cmds.append(self._replace_keywords('netns exec {} $IP link set $DEV2 up'.format(ns)))
182 def _ipr2_ns_create(self):
184 Create the network namespace in which the tests will be run and set up
185 the required network devices for it.
187 self._exec_cmd_batched('pre', self._ipr2_ns_create_cmds())
189 def _nl_ns_destroy(self):
190 ns = self.args.NAMES['NS']
193 def _ipr2_ns_destroy_cmd(self):
194 return self._replace_keywords('netns delete {}'.format(self.args.NAMES['NS']))
196 def _ipr2_ns_destroy(self):
198 Destroy the network namespace for testing (and any associated network
201 self._exec_cmd('post', self._ipr2_ns_destroy_cmd())
205 ip = self._replace_keywords("$IP -b -")
206 proc = subprocess.Popen(ip,
208 stdin=subprocess.PIPE,
213 def _proc_check(self):
218 if proc.returncode is not None and proc.returncode != 0:
219 raise RuntimeError("iproute2 exited with an error code")
221 def _exec_cmd(self, stage, command):
223 Perform any required modifications on an executable command, then run
224 it in a subprocess and return the results.
227 if self.args.verbose > 3:
228 print('_exec_cmd: command "{}"'.format(command))
232 proc.stdin.write((command + '\n').encode())
235 if self.args.verbose > 3:
236 print('_exec_cmd proc: {}'.format(proc))
240 def _exec_cmd_batched(self, stage, commands):
242 self._exec_cmd(stage, cmd)
244 def _replace_keywords(self, cmd):
246 For a given executable command, substitute any known
247 variables contained within NAMES with the correct values
250 subcmd = tcmd.safe_substitute(self.args.NAMES)