test_tunnel_bpf.sh 4.2 KB

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