projects/linux·blob master · refs

258 lines · 8.1 KiB

1import os
2import signal
3from string import Template
4import subprocess
5import time
6from multiprocessing import Pool
7from functools import cached_property
8from TdcPlugin import TdcPlugin
9
10from tdc_config import *
11
12try:
13 from pyroute2 import netns
14 from pyroute2 import IPRoute
15 netlink = True
16except ImportError:
17 netlink = False
18 print("!!! Consider installing pyroute2 !!!")
19
20class SubPlugin(TdcPlugin):
21 def __init__(self):
22 self.sub_class = 'ns/SubPlugin'
23 super().__init__()
24
25 def pre_suite(self, testcount, testlist):
26 super().pre_suite(testcount, testlist)
27
28 def prepare_test(self, test):
29 if 'skip' in test and test['skip'] == 'yes':
30 return
31
32 if 'nsPlugin' not in test['plugins']:
33 return
34
35 if netlink == True:
36 self._nl_ns_create()
37 else:
38 self._ipr2_ns_create()
39
40 # Make sure the netns is visible in the fs
41 ticks = 20
42 while True:
43 if ticks == 0:
44 raise TimeoutError
45 self._proc_check()
46 try:
47 ns = self.args.NAMES['NS']
48 f = open('/run/netns/{}'.format(ns))
49 f.close()
50 break
51 except:
52 time.sleep(0.1)
53 ticks -= 1
54 continue
55
56 def pre_case(self, test, test_skip):
57 if self.args.verbose:
58 print('{}.pre_case'.format(self.sub_class))
59
60 if test_skip:
61 return
62
63 self.prepare_test(test)
64
65 def post_case(self):
66 if self.args.verbose:
67 print('{}.post_case'.format(self.sub_class))
68
69 if netlink == True:
70 self._nl_ns_destroy()
71 else:
72 self._ipr2_ns_destroy()
73
74 def post_suite(self, index):
75 if self.args.verbose:
76 print('{}.post_suite'.format(self.sub_class))
77
78 # Make sure we don't leak resources
79 cmd = self._replace_keywords("$IP -a netns del")
80
81 if self.args.verbose > 3:
82 print('_exec_cmd: command "{}"'.format(cmd))
83
84 subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
85
86 def adjust_command(self, stage, command):
87 super().adjust_command(stage, command)
88 cmdform = 'list'
89 cmdlist = list()
90
91 if self.args.verbose:
92 print('{}.adjust_command'.format(self.sub_class))
93
94 if not isinstance(command, list):
95 cmdform = 'str'
96 cmdlist = command.split()
97 else:
98 cmdlist = command
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'])
106 else:
107 pass
108
109 if cmdform == 'str':
110 command = ' '.join(cmdlist)
111 else:
112 command = cmdlist
113
114 if self.args.verbose:
115 print('adjust_command: return command [{}]'.format(command))
116 return command
117
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"];
123 ifb = self.args.NAMES['IFB']
124
125 if self.args.verbose:
126 print('{}._nl_ns_create'.format(self.sub_class))
127
128 netns.create(ns)
129 netns.pushns(newns=ns)
130 with IPRoute() as ip:
131 ip.link('add', ifname=dev1, kind='veth', peer={'ifname': dev0, 'net_ns_fd':'/proc/1/ns/net'})
132 ip.link('add', ifname=dummy, kind='dummy')
133 ip.link('add', ifname=ifb, kind='ifb')
134 ticks = 20
135 while True:
136 if ticks == 0:
137 raise TimeoutError
138 try:
139 dev1_idx = ip.link_lookup(ifname=dev1)[0]
140 dummy_idx = ip.link_lookup(ifname=dummy)[0]
141 ifb_idx = ip.link_lookup(ifname=ifb)[0]
142 ip.link('set', index=dev1_idx, state='up')
143 ip.link('set', index=dummy_idx, state='up')
144 ip.link('set', index=ifb_idx, state='up')
145 break
146 except:
147 time.sleep(0.1)
148 ticks -= 1
149 continue
150 netns.popns()
151
152 with IPRoute() as ip:
153 ticks = 20
154 while True:
155 if ticks == 0:
156 raise TimeoutError
157 try:
158 dev0_idx = ip.link_lookup(ifname=dev0)[0]
159 ip.link('set', index=dev0_idx, state='up')
160 break
161 except:
162 time.sleep(0.1)
163 ticks -= 1
164 continue
165
166 def _ipr2_ns_create_cmds(self):
167 cmds = []
168
169 ns = self.args.NAMES['NS']
170
171 cmds.append(self._replace_keywords('netns add {}'.format(ns)))
172 cmds.append(self._replace_keywords('link add $DEV1 type veth peer name $DEV0'))
173 cmds.append(self._replace_keywords('link set $DEV1 netns {}'.format(ns)))
174 cmds.append(self._replace_keywords('link add $DUMMY type dummy'.format(ns)))
175 cmds.append(self._replace_keywords('link set $DUMMY netns {}'.format(ns)))
176 cmds.append(self._replace_keywords('link add $IFB type ifb'))
177 cmds.append(self._replace_keywords('link set $IFB netns {}'.format(ns)))
178 cmds.append(self._replace_keywords('netns exec {} $IP link set $DEV1 up'.format(ns)))
179 cmds.append(self._replace_keywords('netns exec {} $IP link set $DUMMY up'.format(ns)))
180 cmds.append(self._replace_keywords('netns exec {} $IP link set $IFB up'.format(ns)))
181 cmds.append(self._replace_keywords('link set $DEV0 up'.format(ns)))
182
183 if self.args.device:
184 cmds.append(self._replace_keywords('link set $DEV2 netns {}'.format(ns)))
185 cmds.append(self._replace_keywords('netns exec {} $IP link set $DEV2 up'.format(ns)))
186
187 return cmds
188
189 def _ipr2_ns_create(self):
190 '''
191 Create the network namespace in which the tests will be run and set up
192 the required network devices for it.
193 '''
194 self._exec_cmd_batched('pre', self._ipr2_ns_create_cmds())
195
196 def _nl_ns_destroy(self):
197 ns = self.args.NAMES['NS']
198 netns.remove(ns)
199
200 def _ipr2_ns_destroy_cmd(self):
201 return self._replace_keywords('netns delete {}'.format(self.args.NAMES['NS']))
202
203 def _ipr2_ns_destroy(self):
204 '''
205 Destroy the network namespace for testing (and any associated network
206 devices as well)
207 '''
208 self._exec_cmd('post', self._ipr2_ns_destroy_cmd())
209
210 @cached_property
211 def _proc(self):
212 ip = self._replace_keywords("$IP -b -")
213 proc = subprocess.Popen(ip,
214 shell=True,
215 stdin=subprocess.PIPE,
216 env=ENVIR)
217
218 return proc
219
220 def _proc_check(self):
221 proc = self._proc
222
223 proc.poll()
224
225 if proc.returncode is not None and proc.returncode != 0:
226 raise RuntimeError("iproute2 exited with an error code")
227
228 def _exec_cmd(self, stage, command):
229 '''
230 Perform any required modifications on an executable command, then run
231 it in a subprocess and return the results.
232 '''
233
234 if self.args.verbose > 3:
235 print('_exec_cmd: command "{}"'.format(command))
236
237 proc = self._proc
238
239 proc.stdin.write((command + '\n').encode())
240 proc.stdin.flush()
241
242 if self.args.verbose > 3:
243 print('_exec_cmd proc: {}'.format(proc))
244
245 self._proc_check()
246
247 def _exec_cmd_batched(self, stage, commands):
248 for cmd in commands:
249 self._exec_cmd(stage, cmd)
250
251 def _replace_keywords(self, cmd):
252 """
253 For a given executable command, substitute any known
254 variables contained within NAMES with the correct values
255 """
256 tcmd = Template(cmd)
257 subcmd = tcmd.safe_substitute(self.args.NAMES)
258 return subcmd