echo "export PATH=/home/go/bin:$PATH" >> /etc/profile
source /etc/profile
ip netns add netns1
ip link add veth1 type veth peer name veth2
ip link set veth2 netns netns1
ip netns exec netns1 ifconfig veth2 1.1.1.2/24 up
ip netns add netns2
ip link add veth3 type veth peer name veth4
ip link set veth4 netns netns2
ip netns exec netns2 ifconfig veth4 1.1.2.2/24 up
ip link add name my-cni0 type bridge
ifconfig my-cni0 1.1.1.1/24 up
ip link set dev veth1 master my-cni0
ifconfig veth1 up
ip link add name my-cni0 type bridge
ifconfig my-cni0 1.1.2.1/24 up
ip link set dev veth3 master my-cni0
ifconfig veth3 up
ip netns exec netns1 ip route add default via 1.1.1.1 dev veth2
ip netns exec netns2 ip route add default via 1.1.2.1 dev veth4
ip netns exec netns1 route -n
ip netns exec netns2 route -n
ip tuntap add dev my-flannel0 mod tun
ifconfig my-flannel0 1.1.1.0/16 up
ip tuntap add dev my-flannel0 mod tun
ifconfig my-flannel0 1.1.2.0/16
ip route add 1.1.0.0/16 dev my-flannel0
ip route add 1.1.0.0/16 dev my-flannel0
ifconfig my-flannel0 mtu 1472
ifconfig my-flannel0 mtu 1472
清理
ip link set my-flannel0 down
ip link delete my-flannel0
ip link set my-cni0 down
ip link delete my-cni0
ip netns exec netns1 ip link set veth2 down
ip netns exec netns1 ip link delete veth2
ip netns exec netns2 ip link set veth4 down
ip netns exec netns2 ip link delete veth4
ip netns delete netns1
ip netns delete netns2
./main -dstUDPHost=10.211.5.16
./main -dstUDPHost=10.211.5.13
ip netns exec netns1 ping 1.1.2.2 -c 3
ip link show my-cni0
ip link show my-flannel0
ip netns exec netns1 socat tcp-l:9999,fork,reuseaddr echo
ip netns exec netns2 socat - tcp-connect:1.1.1.2:9999
发送方:
2024/10/02 22:25:31 read 56 bytes data from tun device
2024/10/02 22:25:31 udp send 56 bytes data
2024/10/02 22:25:31 read 56 bytes data from udp
2024/10/02 22:25:31 handler write 56 bytes data to tun device
2024/10/02 22:25:31 read 52 bytes data from tun device
2024/10/02 22:25:31 udp send 52 bytes data
接收方:
2024/10/02 22:25:31 read 56 bytes data from tun device
2024/10/02 22:25:31 udp send 56 bytes data
2024/10/02 22:25:31 read 56 bytes data from udp
2024/10/02 22:25:31 handler write 56 bytes data to tun device
2024/10/02 22:25:31 read 52 bytes data from tun device
2024/10/02 22:25:31 udp send 52 bytes data
发送:123
接收:123