1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
#!/usr/bin/env python
# Utility functions for QEMU tests ##
import subprocess
import sys
import os
import time
import random
import string
from multiprocessing import Lock, Process
lock = Lock()
def can_create_namespaces(namespace="vpp_chk_4212"):
"""Check if the environment allows creating the namespaces"""
with lock:
try:
result = subprocess.run(
["ip", "netns", "add", namespace], capture_output=True
)
if result.returncode != 0:
return False
subprocess.run(["ip", "netns", "del", namespace], capture_output=True)
return True
except Exception:
return False
def create_namespace(history_file, ns=None):
"""Create one or more namespaces."""
with lock:
namespaces = []
retries = 5
if ns is None:
result = None
for retry in range(retries):
suffix = "".join(
random.choices(string.ascii_lowercase + string.digits, k=8)
)
new_namespace_name = f"vpp_ns{suffix}"
# Check if the namespace already exists
result = subprocess.run(
["ip", "netns", "add", new_namespace_name],
capture_output=True,
text=True,
)
if result.returncode == 0:
with open(history_file, "a") as ns_file:
ns_file.write(f"{new_namespace_name}\n")
return new_namespace_name
error_message = result.stderr if result else "Unknown error"
raise Exception(
f"Failed to generate a unique namespace name after {retries} attempts."
f"Error from last attempt: {error_message}"
)
elif isinstance(ns, str):
namespaces = [ns]
else:
namespaces = ns
for namespace in namespaces:
for attempt in range(retries):
result = subprocess.run(
["ip", "netns", "add", namespace],
capture_output=True,
text=True,
)
if result.returncode == 0:
with open(history_file, "a") as ns_file:
ns_file.write(f"{namespace}\n")
break
if attempt >= retries - 1:
raise Exception(
f"Failed to create namespace {namespace} after {retries} attempts. Error: {result.stderr.decode()}"
)
return ns
def add_namespace_route(ns, prefix, gw_ip):
"""Add a route to a namespace.
arguments:
ns -- namespace string value
prefix -- NETWORK/MASK or "default"
gw_ip -- Gateway IP
"""
with lock:
try:
subprocess.run(
["ip", "netns", "exec", ns, "ip", "route", "add", prefix, "via", gw_ip],
capture_output=True,
)
except subprocess.CalledProcessError as e:
raise Exception("Error adding route to namespace:", e.output)
def delete_all_host_interfaces(history_file):
"""Delete all host interfaces whose names have been added to the history file."""
with lock:
if os.path.exists(history_file):
with open(history_file, "r") as if_file:
for line in if_file:
if_name = line.strip()
if if_name:
_delete_host_interfaces(if_name)
os.remove(history_file)
def _delete_host_interfaces(*host_interface_names):
"""Delete host interfaces.
arguments:
host_interface_names - sequence of host interface names to be deleted
"""
for host_interface_name in host_interface_names:
retries = 3
for attempt in range(retries):
check_result = subprocess.run(
["ip", "link", "show", host_interface_name],
capture_output=True,
text=True,
)
if check_result.returncode != 0:
break
result = subprocess.run(
["ip", "link", "del", host_interface_name],
capture_output=True,
text=True,
)
if result.returncode == 0:
break
if attempt < retries - 1:
time.sleep(1)
else:
raise Exception(
f"Failed to delete host interface {host_interface_name} after {retries} attempts"
)
def create_host_interface(
history_file, host_namespace, *host_ip_prefixes, vpp_if_name=None, host_if_name=None
):
"""Create a host interface of type veth.
arguments:
host_namespace -- host namespace into which the host_interface needs to be set
host_ip_prefixes -- a sequence of ip/prefix-lengths to be set
on the host_interface
vpp_if_name -- name of the veth interface on the VPP side
host_if_name -- name of the veth interface on the host side
"""
with lock:
retries = 5
for attempt in range(retries):
if_name = (
host_if_name
or f"hostif{''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}"
)
new_vpp_if_name = (
vpp_if_name
or f"vppout{''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}"
)
result = subprocess.run(
[
"ip",
"link",
"add",
"name",
new_vpp_if_name,
"type",
"veth",
"peer",
"name",
if_name,
],
capture_output=True,
)
if result.returncode == 0:
host_if_name = if_name
vpp_if_name = new_vpp_if_name
with open(history_file, "a") as if_file:
if_file.write(f"{host_if_name}\n{vpp_if_name}\n")
break
if attempt >= retries - 1:
raise Exception(
f"Failed to create host interface {if_name} and vpp {new_vpp_if_name} after {retries} attempts. Error: {result.stderr.decode()}"
)
result = subprocess.run(
["ip", "link", "set", host_if_name, "netns", host_namespace],
capture_output=True,
)
if result.returncode != 0:
raise Exception(
f"Error setting host interface namespace: {result.stderr.decode()}"
)
result = subprocess.run(
["ip", "link", "set", "dev", vpp_if_name, "up"], capture_output=True
)
if result.returncode != 0:
raise Exception(
f"Error bringing up the host interface: {result.stderr.decode()}"
)
result = subprocess.run(
[
"ip",
"netns",
"exec",
host_namespace,
"ip",
"link",
"set",
"dev",
host_if_name,
"up",
],
capture_output=True,
)
if result.returncode != 0:
raise Exception(
f"Error bringing up the host interface in namespace: {result.stderr.decode()}"
)
for host_ip_prefix in host_ip_prefixes:
result = subprocess.run(
[
"ip",
"netns",
"exec",
host_namespace,
"ip",
"addr",
"add",
host_ip_prefix,
"dev",
host_if_name,
],
capture_output=True,
)
if result.returncode != 0:
raise Exception(
f"Error setting ip prefix on the host interface: {result.stderr.decode()}"
)
return host_if_name, vpp_if_name
def set_interface_mtu(namespace, interface, mtu, logger):
"""Set an MTU number on a linux device interface."""
args = ["ip", "link", "set", "mtu", str(mtu), "dev", interface]
if namespace:
args = ["ip", "netns", "exec", namespace] + args
with lock:
retries = 3
for attempt in range(retries):
result = subprocess.run(args, capture_output=True)
if result.returncode == 0:
break
if attempt < retries - 1:
time.sleep(1)
else:
raise Exception(
f"Failed to set MTU on interface {interface} in namespace {namespace} after {retries} attempts"
)
def enable_interface_gso(namespace, interface):
"""Enable GSO offload on a linux device interface."""
args = ["ethtool", "-K", interface, "rx", "on", "tx", "on"]
if namespace:
args = ["ip", "netns", "exec", namespace] + args
with lock:
result = subprocess.run(args, capture_output=True)
if result.returncode != 0:
raise Exception(
f"Error enabling GSO offload on interface {interface} in namespace {namespace}: {result.stderr.decode()}"
)
def disable_interface_gso(namespace, interface):
"""Disable GSO offload on a linux device interface."""
args = ["ethtool", "-K", interface, "rx", "off", "tx", "off"]
if namespace:
args = ["ip", "netns", "exec", namespace] + args
with lock:
result = subprocess.run(args, capture_output=True)
if result.returncode != 0:
raise Exception(
f"Error disabling GSO offload on interface {interface} in namespace {namespace}: {result.stderr.decode()}"
)
def delete_all_namespaces(history_file):
"""Delete all namespaces whose names have been added to the history file."""
with lock:
if os.path.exists(history_file):
with open(history_file, "r") as ns_file:
for line in ns_file:
ns_name = line.strip()
if ns_name:
_delete_namespace(ns_name)
os.remove(history_file)
def _delete_namespace(ns):
"""Delete one or more namespaces.
arguments:
ns -- a list of namespace names or namespace
"""
if isinstance(ns, str):
namespaces = [ns]
else:
namespaces = ns
existing_namespaces = subprocess.run(
["ip", "netns", "list"], capture_output=True, text=True
).stdout.splitlines()
existing_namespaces = {line.split()[0] for line in existing_namespaces}
for namespace in namespaces:
if namespace not in existing_namespaces:
continue
retries = 3
for attempt in range(retries):
result = subprocess.run(
["ip", "netns", "del", namespace], capture_output=True
)
if result.returncode == 0:
break
if attempt < retries - 1:
time.sleep(1)
else:
raise Exception(
f"Failed to delete namespace {namespace} after {retries} attempts"
)
def list_namespace(ns):
"""List the IP address of a namespace."""
with lock:
result = subprocess.run(
["ip", "netns", "exec", ns, "ip", "addr"], capture_output=True
)
if result.returncode != 0:
raise Exception(
f"Error listing IP addresses in namespace {ns}: {result.stderr.decode()}"
)
def libmemif_test_app(memif_sock_path, logger):
"""Build & run the libmemif test_app for memif interface testing."""
test_dir = os.path.dirname(os.path.realpath(__file__))
ws_root = os.path.dirname(test_dir)
libmemif_app = os.path.join(
ws_root, "extras", "libmemif", "build", "examples", "test_app"
)
def build_libmemif_app():
if not os.path.exists(libmemif_app):
logger.info(f"Building app:{libmemif_app} for memif interface testing")
libmemif_app_dir = os.path.join(ws_root, "extras", "libmemif", "build")
os.makedirs(libmemif_app_dir, exist_ok=True)
os.chdir(libmemif_app_dir)
subprocess.run(["cmake", ".."], check=True)
subprocess.run(["make"], check=True)
def start_libmemif_app():
"""Restart once if the initial run fails."""
max_tries = 2
run = 0
while run < max_tries:
result = subprocess.run(
[libmemif_app, "-b", "9216", "-s", memif_sock_path], capture_output=True
)
if result.returncode == 0:
break
logger.error(
f"Restarting libmemif app due to error: {result.stderr.decode()}"
)
run += 1
time.sleep(1)
build_libmemif_app()
process = Process(target=start_libmemif_app)
process.start()
return process
|