test_ipip.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. function config_device {
  3. ip netns add at_ns0
  4. ip netns add at_ns1
  5. ip netns add at_ns2
  6. ip link add veth0 type veth peer name veth0b
  7. ip link add veth1 type veth peer name veth1b
  8. ip link add veth2 type veth peer name veth2b
  9. ip link set veth0b up
  10. ip link set veth1b up
  11. ip link set veth2b up
  12. ip link set dev veth0b mtu 1500
  13. ip link set dev veth1b mtu 1500
  14. ip link set dev veth2b mtu 1500
  15. ip link set veth0 netns at_ns0
  16. ip link set veth1 netns at_ns1
  17. ip link set veth2 netns at_ns2
  18. ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
  19. ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
  20. ip netns exec at_ns0 ip link set dev veth0 up
  21. ip netns exec at_ns1 ip addr add 172.16.1.101/24 dev veth1
  22. ip netns exec at_ns1 ip addr add 2401:db00::2/64 dev veth1 nodad
  23. ip netns exec at_ns1 ip link set dev veth1 up
  24. ip netns exec at_ns2 ip addr add 172.16.1.200/24 dev veth2
  25. ip netns exec at_ns2 ip addr add 2401:db00::3/64 dev veth2 nodad
  26. ip netns exec at_ns2 ip link set dev veth2 up
  27. ip link add br0 type bridge
  28. ip link set br0 up
  29. ip link set dev br0 mtu 1500
  30. ip link set veth0b master br0
  31. ip link set veth1b master br0
  32. ip link set veth2b master br0
  33. }
  34. function add_ipip_tunnel {
  35. ip netns exec at_ns0 \
  36. ip link add dev $DEV_NS type ipip local 172.16.1.100 remote 172.16.1.200
  37. ip netns exec at_ns0 ip link set dev $DEV_NS up
  38. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  39. ip netns exec at_ns1 \
  40. ip link add dev $DEV_NS type ipip local 172.16.1.101 remote 172.16.1.200
  41. ip netns exec at_ns1 ip link set dev $DEV_NS up
  42. # same inner IP address in at_ns0 and at_ns1
  43. ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24
  44. ip netns exec at_ns2 ip link add dev $DEV type ipip external
  45. ip netns exec at_ns2 ip link set dev $DEV up
  46. ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
  47. }
  48. function add_ipip6_tunnel {
  49. ip netns exec at_ns0 \
  50. ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::1/64 remote 2401:db00::3/64
  51. ip netns exec at_ns0 ip link set dev $DEV_NS up
  52. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  53. ip netns exec at_ns1 \
  54. ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::2/64 remote 2401:db00::3/64
  55. ip netns exec at_ns1 ip link set dev $DEV_NS up
  56. # same inner IP address in at_ns0 and at_ns1
  57. ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24
  58. ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ipip6 external
  59. ip netns exec at_ns2 ip link set dev $DEV up
  60. ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
  61. }
  62. function add_ip6ip6_tunnel {
  63. ip netns exec at_ns0 \
  64. ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::1/64 remote 2401:db00::3/64
  65. ip netns exec at_ns0 ip link set dev $DEV_NS up
  66. ip netns exec at_ns0 ip addr add dev $DEV_NS 2601:646::1/64
  67. ip netns exec at_ns1 \
  68. ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::2/64 remote 2401:db00::3/64
  69. ip netns exec at_ns1 ip link set dev $DEV_NS up
  70. # same inner IP address in at_ns0 and at_ns1
  71. ip netns exec at_ns1 ip addr add dev $DEV_NS 2601:646::1/64
  72. ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ip6ip6 external
  73. ip netns exec at_ns2 ip link set dev $DEV up
  74. ip netns exec at_ns2 ip addr add dev $DEV 2601:646::2/64
  75. }
  76. function attach_bpf {
  77. DEV=$1
  78. SET_TUNNEL=$2
  79. GET_TUNNEL=$3
  80. ip netns exec at_ns2 tc qdisc add dev $DEV clsact
  81. ip netns exec at_ns2 tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
  82. ip netns exec at_ns2 tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
  83. }
  84. function test_ipip {
  85. DEV_NS=ipip_std
  86. DEV=ipip_bpf
  87. config_device
  88. # tcpdump -nei br0 &
  89. cat /sys/kernel/debug/tracing/trace_pipe &
  90. add_ipip_tunnel
  91. attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
  92. ip netns exec at_ns0 ping -c 1 10.1.1.200
  93. ip netns exec at_ns2 ping -c 1 10.1.1.100
  94. ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
  95. ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
  96. sleep 0.2
  97. # tcp check _same_ IP over different tunnels
  98. ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
  99. ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
  100. cleanup
  101. }
  102. # IPv4 over IPv6 tunnel
  103. function test_ipip6 {
  104. DEV_NS=ipip_std
  105. DEV=ipip_bpf
  106. config_device
  107. # tcpdump -nei br0 &
  108. cat /sys/kernel/debug/tracing/trace_pipe &
  109. add_ipip6_tunnel
  110. attach_bpf $DEV ipip6_set_tunnel ipip6_get_tunnel
  111. ip netns exec at_ns0 ping -c 1 10.1.1.200
  112. ip netns exec at_ns2 ping -c 1 10.1.1.100
  113. ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
  114. ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
  115. sleep 0.2
  116. # tcp check _same_ IP over different tunnels
  117. ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
  118. ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
  119. cleanup
  120. }
  121. # IPv6 over IPv6 tunnel
  122. function test_ip6ip6 {
  123. DEV_NS=ipip_std
  124. DEV=ipip_bpf
  125. config_device
  126. # tcpdump -nei br0 &
  127. cat /sys/kernel/debug/tracing/trace_pipe &
  128. add_ip6ip6_tunnel
  129. attach_bpf $DEV ip6ip6_set_tunnel ip6ip6_get_tunnel
  130. ip netns exec at_ns0 ping -6 -c 1 2601:646::2
  131. ip netns exec at_ns2 ping -6 -c 1 2601:646::1
  132. ip netns exec at_ns0 iperf -6sD -p 5200 > /dev/null
  133. ip netns exec at_ns1 iperf -6sD -p 5201 > /dev/null
  134. sleep 0.2
  135. # tcp check _same_ IP over different tunnels
  136. ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5200
  137. ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5201
  138. cleanup
  139. }
  140. function cleanup {
  141. set +ex
  142. pkill iperf
  143. ip netns delete at_ns0
  144. ip netns delete at_ns1
  145. ip netns delete at_ns2
  146. ip link del veth0
  147. ip link del veth1
  148. ip link del veth2
  149. ip link del br0
  150. pkill tcpdump
  151. pkill cat
  152. set -ex
  153. }
  154. cleanup
  155. echo "Testing IP tunnels..."
  156. test_ipip
  157. test_ipip6
  158. test_ip6ip6
  159. echo "*** PASS ***"