|
# Creates a vlan with vlan id $vlanid ontop of $eth |
|
# and connect to a network namespace with the help of |
|
# a bridge and a veth pair |
|
function vlan() { |
|
vlan="$eth.$vlanid" |
|
ns="ns$vlanid" |
|
br="br$vlanid" |
|
|
|
ip link add link $eth name $vlan type vlan id $vlanid |
|
ip link add dev $br type bridge |
|
ip link set $vlan master $br |
|
ip link set dev $vlan up |
|
ip link set dev $br up |
|
mkdir -p /var/run/netns |
|
ip netns add $ns |
|
ip link add q$ns type veth peer name r$ns |
|
ip link set q$ns master $br |
|
ip link set q$ns up |
|
ip link set r$ns netns $ns |
|
ip netns exec $ns ip link set dev r$ns name eth0 |
|
ip netns exec $ns ip link set eth0 up |
|
ip netns exec $ns ip addr add $ipcidr dev eth0 |
|
} |
|
|
|
# on host 1 |
|
vlanid="3" |
|
eth="eth0" |
|
ipcidr="192.168.3.2/16" |
|
vlan |
|
|
|
# on host 2 |
|
vlanid="3" |
|
eth="eth0" |
|
ipcidr="192.168.3.3/16" |
|
vlan |
|
|
|
# on host 1 |
|
ip netns exec ns2 ping 192.168.3.2 |
|
|
|
# successful result example |
|
root@2f169f9a6c9f:/# ip netns exec ns3 ping 192.168.3.3 |
|
PING 192.168.3.3 (192.168.3.3) 56(84) bytes of data. |
|
64 bytes from 192.168.3.3: icmp_seq=1 ttl=64 time=0.058 ms |
|
64 bytes from 192.168.3.3: icmp_seq=2 ttl=64 time=0.065 ms |
|
64 bytes from 192.168.3.3: icmp_seq=3 ttl=64 time=0.118 ms |
|
64 bytes from 192.168.3.3: icmp_seq=4 ttl=64 time=0.095 ms |
|
64 bytes from 192.168.3.3: icmp_seq=5 ttl=64 time=0.079 ms |
|
|
|
|
|
root@cb37293169b9:/# tcpdump -vv -n -s 0 -e -i eth0 |
|
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes |
|
12:21:48.523443 46:ca:7f:bd:fd:6c > f2:48:df:b4:d3:2a, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 2544, offset 0, flags [DF], proto ICMP (1), length 84) |
|
192.168.3.2 > 192.168.3.3: ICMP echo request, id 101, seq 1, length 64 |
|
12:21:48.523466 f2:48:df:b4:d3:2a > 46:ca:7f:bd:fd:6c, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 31975, offset 0, flags [none], proto ICMP (1), length 84) |
|
192.168.3.3 > 192.168.3.2: ICMP echo reply, id 101, seq 1, length 64 |
|
12:21:49.522451 46:ca:7f:bd:fd:6c > f2:48:df:b4:d3:2a, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 2787, offset 0, flags [DF], proto ICMP (1), length 84) |
|
192.168.3.2 > 192.168.3.3: ICMP echo request, id 101, seq 2, length 64 |
|
12:21:49.522473 f2:48:df:b4:d3:2a > 46:ca:7f:bd:fd:6c, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 32023, offset 0, flags [none], proto ICMP (1), length 84) |
|
192.168.3.3 > 192.168.3.2: ICMP echo reply, id 101, seq 2, length 64 |
|
12:21:50.521451 46:ca:7f:bd:fd:6c > f2:48:df:b4:d3:2a, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 2980, offset 0, flags [DF], proto ICMP (1), length 84) |
|
192.168.3.2 > 192.168.3.3: ICMP echo request, id 101, seq 3, length 64 |
|
12:21:50.521522 f2:48:df:b4:d3:2a > 46:ca:7f:bd:fd:6c, ethertype 802.1Q (0x8100), length 102: vlan 3, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 32269, offset 0, flags [none], proto ICMP (1), length 84) |
|
192.168.3.3 > 192.168.3.2: ICMP echo reply, id 101, seq 3, length 64 |