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
(
history_file, host_namespace, *host_ip_prefixes, vpp_if_name=None, host_if_name=None
)
| 302 | |
| 303 | |
| 304 | def create_host_interface( |
| 305 | history_file, host_namespace, *host_ip_prefixes, vpp_if_name=None, host_if_name=None |
| 306 | ): |
| 307 | """Create a host interface of type veth. |
| 308 | arguments: |
| 309 | host_namespace -- host namespace into which the host_interface needs to be set |
| 310 | host_ip_prefixes -- a sequence of ip/prefix-lengths to be set |
| 311 | on the host_interface |
| 312 | vpp_if_name -- name of the veth interface on the VPP side |
| 313 | host_if_name -- name of the veth interface on the host side |
| 314 | """ |
| 315 | with lock: |
| 316 | retries = 5 |
| 317 | |
| 318 | for attempt in range(retries): |
| 319 | if_name = ( |
| 320 | host_if_name |
| 321 | or f"hostif{''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}" |
| 322 | ) |
| 323 | new_vpp_if_name = ( |
| 324 | vpp_if_name |
| 325 | or f"vppout{''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}" |
| 326 | ) |
| 327 | |
| 328 | result = subprocess.run( |
| 329 | [ |
| 330 | "ip", |
| 331 | "link", |
| 332 | "add", |
| 333 | "name", |
| 334 | new_vpp_if_name, |
| 335 | "type", |
| 336 | "veth", |
| 337 | "peer", |
| 338 | "name", |
| 339 | if_name, |
| 340 | ], |
| 341 | capture_output=True, |
| 342 | ) |
| 343 | if result.returncode == 0: |
| 344 | host_if_name = if_name |
| 345 | vpp_if_name = new_vpp_if_name |
| 346 | with open(history_file, "a") as if_file: |
| 347 | if_file.write(f"{host_if_name}\n{vpp_if_name}\n") |
| 348 | break |
| 349 | if attempt >= retries - 1: |
| 350 | raise Exception( |
| 351 | f"Failed to create host interface {if_name} and vpp {new_vpp_if_name} after {retries} attempts. Error: {result.stderr.decode()}" |
| 352 | ) |
| 353 | |
| 354 | result = subprocess.run( |
| 355 | ["ip", "link", "set", host_if_name, "netns", host_namespace], |
| 356 | capture_output=True, |
| 357 | ) |
| 358 | if result.returncode != 0: |
| 359 | raise Exception( |
| 360 | f"Error setting host interface namespace: {result.stderr.decode()}" |
| 361 | ) |