bridge-vlan
# 1.topo
# 2.创建命名空间
ip netns add ns0
ip netns add ns1
ip netns add ns2
ip netns add ns3
# 3.创建veth设备
ip link add ns0-veth0 type veth peer name hn0-veth0
ip link add ns1-veth0 type veth peer name hn1-veth0
ip link add ns2-veth0 type veth peer name hn2-veth0
ip link add ns3-veth0 type veth peer name hn3-veth0
# 4.veth设备放入命名空间,启动接口
ip link set ns0-veth0 netns ns0
ip link set ns1-veth0 netns ns1
ip link set ns2-veth0 netns ns2
ip link set ns3-veth0 netns ns3
ip -netns ns0 link set ns0-veth0 up
ip -netns ns1 link set ns1-veth0 up
ip -netns ns2 link set ns2-veth0 up
ip -netns ns3 link set ns3-veth0 up
# 5.创建br0,启动br0过滤功能,添加接口到br0
brctl addbr br0
ip link set dev br0 type bridge vlan_filtering 1 //1是启用
ip link set br0 up
brctl addif br0 hn0-veth0
brctl addif br0 hn1-veth0
brctl addif br0 hn2-veth0
brctl addif br0 hn3-veth0
ip link set dev hn0-veth0 up
ip link set dev hn1-veth0 up
ip link set dev hn2-veth0 up
ip link set dev hn3-veth0 up
# 6.配置vlan
bridge vlan add dev hn0-veth0 vid 10 pvid untagged
bridge vlan add dev hn1-veth0 vid 20 pvid untagged
bridge vlan add dev hn2-veth0 vid 10 pvid untagged
bridge vlan add dev hn3-veth0 vid 20 pvid untagged
# 7.配置地址
ip -netns ns0 addr add 1.1.1.10/24 dev ns0-veth0
ip -netns ns1 addr add 1.1.1.11/24 dev ns1-veth0
ip -netns ns2 addr add 1.1.1.12/24 dev ns2-veth0
ip -netns ns3 addr add 1.1.1.13/24 dev ns3-veth0
# 8.测试
# 8.1测试1
ip netns exec ns0 ping -c 3 -W 1 1.1.1.11 不通
ip netns exec ns0 ping -c 3 -W 1 1.1.1.12 通
tcpdump -i br0 -e
hn0-veth0 属于vlan 10 ,hn1-veth0 属于vlan 20 ,所以ip netns exec ns0 ping -c 3 -W 1 1.1.1.11 不通;
hn0-veth0 属于vlan 10 ,hn2-veth0 属于vlan 10 ,所以ip netns exec ns0 ping -c 3 -W 1 1.1.1.12 通
# 8.2测试2
ip netns exec ns1 ping -c 3 -W 1 1.1.1.12 不通
ip netns exec ns1 ping -c 3 -W 1 1.1.1.13 通
tcpdump -i br0 -e
同上;
# 9.删除
ip netns del ns0
ip netns del ns1
ip netns del ns2
ip netns del ns3
# 10.bridge vlan命令
bridge -c vlan show
bridge vlan del vid 100-200 dev eth0
bridge vlan add dev eth0 vid 10
bridge vlan add dev eth0 vid 10 pvid untagged
bridge vlan add dev br0 vid 20 self
ip -details add show br0
# 11.参考
VLAN - ArchWiki
networking - Linux VLAN-aware bridges and trunk ports - Unix & Linux Stack Exchange