Skip to content

Commit fb5a15b

Browse files
committed
Don't use iptools '-echo' option
Versions of iproute2 older than 6.0.0, such as those shipped with Ubuntu 22, do not support the '-echo' option. Rewrites the labgrid-raw-interface's handle_ns_macvtap() method to no longer rely on echoing back the result of the 'ip link add' command to determine the new interface name. Signed-off-by: Nick Potenski <nick.potenski@garmin.com>
1 parent 268c098 commit fb5a15b

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

helpers/labgrid-raw-interface

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,28 @@ def open_tap(name, device="/dev/net/tun"):
5858
return fd
5959

6060

61+
def find_unused_macvtap_name():
62+
"""Find an unused macvtap interface name by checking existing interfaces."""
63+
output = subprocess.check_output(["ip", "-j", "link", "show"])
64+
existing = {iface["ifname"] for iface in json.loads(output)}
65+
for i in range(0, 1000):
66+
name = f"macvtap{i}"
67+
if name not in existing:
68+
return name
69+
raise RuntimeError("Could not find an unused macvtap interface name")
70+
71+
6172
def handle_ns_macvtap(options):
6273
# Use macvtap in bridge mode. This is more consistent than using hairpin
6374
# mode and relying on a switch to relay packets back into the interface.
64-
output = subprocess.check_output(
65-
["ip", "-j", "-echo", "link", "add", "link", options.ifname, "type", "macvtap", "mode", "bridge"]
66-
)
67-
output = json.loads(output)
68-
assert len(output) == 1
69-
ifindex_new = output[0]["ifindex"]
70-
ifname_new = output[0]["ifname"]
71-
assert ifname_new.startswith("macvtap")
75+
76+
# TODO: Can simplify this to use the "-echo" option for "ip link add" once iproute2 v6.0.0+ is ubiquitous.
77+
ifname_new = find_unused_macvtap_name()
78+
subprocess.check_call(["ip", "link", "add", "link", options.ifname, "name", ifname_new, "type", "macvtap", "mode", "bridge"])
79+
output = subprocess.check_output(["ip", "-j", "link", "show", ifname_new])
80+
info = json.loads(output)
81+
assert len(info) == 1
82+
ifindex_new = info[0]["ifindex"]
7283
try:
7384
# Set the flag that tells the kernel to allow multicast to flow through
7485
# the macvtap. This is required to be done on the exporter side for the
@@ -82,7 +93,7 @@ def handle_ns_macvtap(options):
8293
subprocess.check_call(
8394
["ip", "link", "set", "address", options.mac_address, "dev", ifname_new])
8495
subprocess.check_call(
85-
["ip", "link", "set", "dev", ifname_new, "name", "macvtap0", "up", "netns", str(options.pid), "index", str(ifindex_new)]
96+
["ip", "link", "set", "dev", ifname_new, "name", "macvtap0", "up", "netns", str(options.pid)]
8697
)
8798
except:
8899
subprocess.check_call(

0 commit comments

Comments
 (0)