ip netns add ns1 ip netns add ns2 ip netns add ns3 ip netns add ns4
ovs-vsctl add-br br0 ovs-vsctl add-br br1
ovs-vsctl add-port br0 tap1 -- set Interface tap1 type=internal ip linkset tap1 netns ns1 ip netns exec ns1 ip addr add 1.1.1.1/24 dev tap1 ip netns exec ns1 ip linkset tap1 up ip netns exec ns1 ip linkset lo up
ovs-vsctl add-port br0 tap2 -- set Interface tap2 type=internal ip linkset tap2 netns ns2 ip netns exec ns2 ip addr add 1.1.1.2/24 dev tap2 ip netns exec ns2 ip linkset tap2 up ip netns exec ns2 ip linkset lo up
ip link add firstbr type veth peer name firstif ip link add secondbr type veth peer name secondif ip link add thirdbr type veth peer name thirdif
ovs-vsctl add-port br0 firstbr ip linkset firstif netns ns3
ovs-vsctl add-port br1 thirdbr ip linkset thirdif netns ns4
ip netns exec ns3 ip linkset firstif up ip netns exec ns4 ip linkset thirdif up ip linkset firstbr up ip linkset secondbr up ip linkset secondif up ip linkset thirdbr up
本地端口镜像
将ns1 ping ns2的双向流量镜像到 ns3
1 2 3 4
ovs-vsctl -- --id=@tap1 get port tap1 -- --id=@firstbr get port firstbr -- --id=@m create mirror name=mirror0 select-dst-port=@tap1 select-src-port=@tap1 output-port=@firstbr -- set bridge br0 mirrors=@m
ns1 ping ns2 (tap1: 1.1.1.1-> tap2: 1.1.1.2)
1 2 3 4 5
$ ip netns exec ns1 ping -c3 1.1.1.2 PING 1.1.1.2 (1.1.1.2) 56(84) bytes of data. 64 bytes from 1.1.1.2: icmp_seq=1 ttl=64 time=0.596 ms 64 bytes from 1.1.1.2: icmp_seq=2 ttl=64 time=0.125 ms 64 bytes from 1.1.1.2: icmp_seq=3 ttl=64 time=0.122 ms
在 firstif 上抓包可以看到成功获得 tap1 到 tap2 双向的 ICMP 响应数据包
1 2 3 4 5 6 7 8 9
$ sudo ip netns exec ns3 tcpdump -i firstif tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on firstif, link-type EN10MB (Ethernet), capture size 262144 bytes 22:18:21.765662 IP 1.1.1.1 > 1.1.1.2: ICMP echo request, id 27011, seq 1, length 64 22:18:21.765934 IP 1.1.1.2 > 1.1.1.1: ICMP echo reply, id 27011, seq 1, length 64 22:18:22.779652 IP 1.1.1.1 > 1.1.1.2: ICMP echo request, id 27011, seq 2, length 64 22:18:22.779712 IP 1.1.1.2 > 1.1.1.1: ICMP echo reply, id 27011, seq 2, length 64 22:18:23.803625 IP 1.1.1.1 > 1.1.1.2: ICMP echo request, id 27011, seq 3, length 64 22:18:23.803682 IP 1.1.1.2 > 1.1.1.1: ICMP echo reply, id 27011, seq 3, length 64
远程 VLAN 镜像
设置 secondbr、secondif、thirdbr 为 vlan110
1 2 3
ovs-vsctl set port secondbr tag=110 ovs-vsctl set port secondif tag=110 ovs-vsctl set port thirdbr tag=110
关闭 vlan110 的 MAC 学习功能,避免影响正常网络转发
1 2
ovs-vsctl set bridge br0 flood-vlans=110 ovs-vsctl set bridge br1 flood-vlans=110
将ns1 ping ns2的双向流量都 output 到 vlan110
1 2 3
ovs-vsctl -- --id=@tap1 get port tap1 -- --id=@m create mirror name=mirror1 select-dst-port=@tap1 select-src-port=@tap1 output-vlan=110 -- set bridge br0 mirrors=@m