test_tunnel_bpf.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # In Namespace 0 (at_ns0) using native tunnel
  4. # Overlay IP: 10.1.1.100
  5. # local 192.16.1.100 remote 192.16.1.200
  6. # veth0 IP: 172.16.1.100, tunnel dev <type>00
  7. # Out of Namespace using BPF set/get on lwtunnel
  8. # Overlay IP: 10.1.1.200
  9. # local 172.16.1.200 remote 172.16.1.100
  10. # veth1 IP: 172.16.1.200, tunnel dev <type>11
  11. function config_device {
  12. ip netns add at_ns0
  13. ip link add veth0 type veth peer name veth1
  14. ip link set veth0 netns at_ns0
  15. ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
  16. ip netns exec at_ns0 ip link set dev veth0 up
  17. ip link set dev veth1 up mtu 1500
  18. ip addr add dev veth1 172.16.1.200/24
  19. }
  20. function add_gre_tunnel {
  21. # in namespace
  22. ip netns exec at_ns0 \
  23. ip link add dev $DEV_NS type $TYPE key 2 local 172.16.1.100 remote 172.16.1.200
  24. ip netns exec at_ns0 ip link set dev $DEV_NS up
  25. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  26. # out of namespace
  27. ip link add dev $DEV type $TYPE key 2 external
  28. ip link set dev $DEV up
  29. ip addr add dev $DEV 10.1.1.200/24
  30. }
  31. function add_erspan_tunnel {
  32. # in namespace
  33. ip netns exec at_ns0 \
  34. ip link add dev $DEV_NS type $TYPE seq key 2 local 172.16.1.100 remote 172.16.1.200 erspan 123
  35. ip netns exec at_ns0 ip link set dev $DEV_NS up
  36. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  37. # out of namespace
  38. ip link add dev $DEV type $TYPE external
  39. ip link set dev $DEV up
  40. ip addr add dev $DEV 10.1.1.200/24
  41. }
  42. function add_vxlan_tunnel {
  43. # Set static ARP entry here because iptables set-mark works
  44. # on L3 packet, as a result not applying to ARP packets,
  45. # causing errors at get_tunnel_{key/opt}.
  46. # in namespace
  47. ip netns exec at_ns0 \
  48. ip link add dev $DEV_NS type $TYPE id 2 dstport 4789 gbp remote 172.16.1.200
  49. ip netns exec at_ns0 ip link set dev $DEV_NS address 52:54:00:d9:01:00 up
  50. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  51. ip netns exec at_ns0 arp -s 10.1.1.200 52:54:00:d9:02:00
  52. ip netns exec at_ns0 iptables -A OUTPUT -j MARK --set-mark 0x800FF
  53. # out of namespace
  54. ip link add dev $DEV type $TYPE external gbp dstport 4789
  55. ip link set dev $DEV address 52:54:00:d9:02:00 up
  56. ip addr add dev $DEV 10.1.1.200/24
  57. arp -s 10.1.1.100 52:54:00:d9:01:00
  58. }
  59. function add_geneve_tunnel {
  60. # in namespace
  61. ip netns exec at_ns0 \
  62. ip link add dev $DEV_NS type $TYPE id 2 dstport 6081 remote 172.16.1.200
  63. ip netns exec at_ns0 ip link set dev $DEV_NS up
  64. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  65. # out of namespace
  66. ip link add dev $DEV type $TYPE dstport 6081 external
  67. ip link set dev $DEV up
  68. ip addr add dev $DEV 10.1.1.200/24
  69. }
  70. function add_ipip_tunnel {
  71. # in namespace
  72. ip netns exec at_ns0 \
  73. ip link add dev $DEV_NS type $TYPE local 172.16.1.100 remote 172.16.1.200
  74. ip netns exec at_ns0 ip link set dev $DEV_NS up
  75. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  76. # out of namespace
  77. ip link add dev $DEV type $TYPE external
  78. ip link set dev $DEV up
  79. ip addr add dev $DEV 10.1.1.200/24
  80. }
  81. function attach_bpf {
  82. DEV=$1
  83. SET_TUNNEL=$2
  84. GET_TUNNEL=$3
  85. tc qdisc add dev $DEV clsact
  86. tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
  87. tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
  88. }
  89. function test_gre {
  90. TYPE=gretap
  91. DEV_NS=gretap00
  92. DEV=gretap11
  93. config_device
  94. add_gre_tunnel
  95. attach_bpf $DEV gre_set_tunnel gre_get_tunnel
  96. ping -c 1 10.1.1.100
  97. ip netns exec at_ns0 ping -c 1 10.1.1.200
  98. cleanup
  99. }
  100. function test_erspan {
  101. TYPE=erspan
  102. DEV_NS=erspan00
  103. DEV=erspan11
  104. config_device
  105. add_erspan_tunnel
  106. attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
  107. ping -c 1 10.1.1.100
  108. ip netns exec at_ns0 ping -c 1 10.1.1.200
  109. cleanup
  110. }
  111. function test_vxlan {
  112. TYPE=vxlan
  113. DEV_NS=vxlan00
  114. DEV=vxlan11
  115. config_device
  116. add_vxlan_tunnel
  117. attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
  118. ping -c 1 10.1.1.100
  119. ip netns exec at_ns0 ping -c 1 10.1.1.200
  120. cleanup
  121. }
  122. function test_geneve {
  123. TYPE=geneve
  124. DEV_NS=geneve00
  125. DEV=geneve11
  126. config_device
  127. add_geneve_tunnel
  128. attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
  129. ping -c 1 10.1.1.100
  130. ip netns exec at_ns0 ping -c 1 10.1.1.200
  131. cleanup
  132. }
  133. function test_ipip {
  134. TYPE=ipip
  135. DEV_NS=ipip00
  136. DEV=ipip11
  137. config_device
  138. tcpdump -nei veth1 &
  139. cat /sys/kernel/debug/tracing/trace_pipe &
  140. add_ipip_tunnel
  141. ethtool -K veth1 gso off gro off rx off tx off
  142. ip link set dev veth1 mtu 1500
  143. attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
  144. ping -c 1 10.1.1.100
  145. ip netns exec at_ns0 ping -c 1 10.1.1.200
  146. ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
  147. sleep 0.2
  148. iperf -c 10.1.1.100 -n 5k -p 5200
  149. cleanup
  150. }
  151. function cleanup {
  152. set +ex
  153. pkill iperf
  154. ip netns delete at_ns0
  155. ip link del veth1
  156. ip link del ipip11
  157. ip link del gretap11
  158. ip link del vxlan11
  159. ip link del geneve11
  160. ip link del erspan11
  161. pkill tcpdump
  162. pkill cat
  163. set -ex
  164. }
  165. trap cleanup 0 2 3 6 9
  166. cleanup
  167. echo "Testing GRE tunnel..."
  168. test_gre
  169. echo "Testing ERSPAN tunnel..."
  170. test_erspan
  171. echo "Testing VXLAN tunnel..."
  172. test_vxlan
  173. echo "Testing GENEVE tunnel..."
  174. test_geneve
  175. echo "Testing IPIP tunnel..."
  176. test_ipip
  177. echo "*** PASS ***"