Skip to content

Commit 0e7c0a8

Browse files
Switch to tc mirrored to get rid of macvtap (#110)
1 parent 3ae7f59 commit 0e7c0a8

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

scripts/manage_vms.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"disk-path": "/machine01.img",
1414
"disk-size": "5G",
1515
"memory": "2G",
16-
"tap-index-fd": [(0, 30), (1, 40)],
16+
"lan_indices": [0, 1],
1717
"serial-port": 4000,
1818
},
1919
"machine02": {
@@ -22,7 +22,7 @@
2222
"disk-path": "/machine02.img",
2323
"disk-size": "5G",
2424
"memory": "2G",
25-
"tap-index-fd": [(2, 50), (3, 60)],
25+
"lan_indices": [2, 3],
2626
"serial-port": 4001,
2727
},
2828
"machine03": {
@@ -31,7 +31,7 @@
3131
"disk-path": "/machine03.img",
3232
"disk-size": "5G",
3333
"memory": "2G",
34-
"tap-index-fd": [(4, 70), (5, 80)],
34+
"lan_indices": [4, 5],
3535
"serial-port": 4002,
3636
},
3737
}
@@ -126,18 +126,6 @@ def _delete_vm_disk(path):
126126

127127
@staticmethod
128128
def _start_vm(machine):
129-
nics = []
130-
netdevices = []
131-
for tap in machine.get("tap-index-fd", []):
132-
ifindex = tap[0]
133-
fd = tap[1]
134-
135-
mac = subprocess.check_output(["cat", "/sys/class/net/macvtap{ifindex}/address".format(ifindex=ifindex)]).decode("utf-8").strip()
136-
tapindex = subprocess.check_output(["cat", "/sys/class/net/macvtap{ifindex}/ifindex".format(ifindex=ifindex)]).decode("utf-8").strip()
137-
138-
nics.append("virtio-net,netdev=hn{ifindex},mac={mac}".format(ifindex=ifindex, mac=mac))
139-
netdevices.append("tap,fd={fd},id=hn{ifindex} {fd}<>/dev/tap{tapindex}".format(fd=fd, ifindex=ifindex, tapindex=tapindex))
140-
141129
cmd = [
142130
"qemu-system-x86_64",
143131
"-name", machine.get("name"),
@@ -153,13 +141,13 @@ def _start_vm(machine):
153141
"-nographic",
154142
]
155143

156-
for nic in nics:
157-
cmd.append("-device")
158-
cmd.append(nic)
159-
160-
for device in netdevices:
161-
cmd.append("-netdev")
162-
cmd.append(device)
144+
for i in machine["lan_indices"]:
145+
with open(f'/sys/class/net/lan{i}/address', 'r') as f:
146+
mac = f.read().strip()
147+
cmd.append('-device')
148+
cmd.append(f'virtio-net,netdev=hn{i},mac={mac}')
149+
cmd.append(f'-netdev')
150+
cmd.append(f'tap,id=hn{i},ifname=tap{i},script=/mini-lab/mirror_tap_to_lan.sh,downscript=no')
163151

164152
cmd.append("&")
165153

scripts/mirror_tap_to_lan.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Script is taken from https://netdevops.me/2021/transparently-redirecting-packets/frames-between-interfaces/
4+
# Script is taken from https://netdevops.me/2021/transparently-redirecting-packetsframes-between-interfaces/
5+
# Read it for better understanding
6+
7+
set -o errexit
8+
TAP_IF=$1
9+
# get interface index number up to 3 digits (everything after first three chars)
10+
# tap0 -> 0
11+
# tap123 -> 123
12+
INDEX=${TAP_IF:3:3}
13+
14+
ip link set $TAP_IF up
15+
ip link set $TAP_IF mtu 65000
16+
17+
# create tc lan<->tap redirect rules
18+
tc qdisc add dev lan$INDEX ingress
19+
tc filter add dev lan$INDEX parent ffff: protocol all u32 match u8 0 0 action mirred egress redirect dev $TAP_IF
20+
21+
tc qdisc add dev $TAP_IF ingress
22+
tc filter add dev $TAP_IF parent ffff: protocol all u32 match u8 0 0 action mirred egress redirect dev lan$INDEX

scripts/vms_entrypoint.sh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,4 @@ while [ "$MYINT" -lt "$INTFS" ]; do
2525
int_calc
2626
done
2727

28-
# creating macvtap interfaces for the qemu vms
29-
for i in $(seq 0 5); do
30-
ip link add link lan${i} name macvtap${i} type macvtap mode passthru
31-
ip link set macvtap${i} up
32-
ip link set macvtap${i} promisc on
33-
done
34-
35-
echo "Connected all interfaces"
36-
ifdown -a || true
37-
ifup -a || true
38-
3928
tail -f /dev/null

0 commit comments

Comments
 (0)