test_tunnel.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # End-to-end eBPF tunnel test suite
  4. # The script tests BPF network tunnel implementation.
  5. #
  6. # Topology:
  7. # ---------
  8. # root namespace | at_ns0 namespace
  9. # |
  10. # ----------- | -----------
  11. # | tnl dev | | | tnl dev | (overlay network)
  12. # ----------- | -----------
  13. # metadata-mode | native-mode
  14. # with bpf |
  15. # |
  16. # ---------- | ----------
  17. # | veth1 | --------- | veth0 | (underlay network)
  18. # ---------- peer ----------
  19. #
  20. #
  21. # Device Configuration
  22. # --------------------
  23. # Root namespace with metadata-mode tunnel + BPF
  24. # Device names and addresses:
  25. # veth1 IP: 172.16.1.200, IPv6: 00::22 (underlay)
  26. # tunnel dev <type>11, ex: gre11, IPv4: 10.1.1.200 (overlay)
  27. #
  28. # Namespace at_ns0 with native tunnel
  29. # Device names and addresses:
  30. # veth0 IPv4: 172.16.1.100, IPv6: 00::11 (underlay)
  31. # tunnel dev <type>00, ex: gre00, IPv4: 10.1.1.100 (overlay)
  32. #
  33. #
  34. # End-to-end ping packet flow
  35. # ---------------------------
  36. # Most of the tests start by namespace creation, device configuration,
  37. # then ping the underlay and overlay network. When doing 'ping 10.1.1.100'
  38. # from root namespace, the following operations happen:
  39. # 1) Route lookup shows 10.1.1.100/24 belongs to tnl dev, fwd to tnl dev.
  40. # 2) Tnl device's egress BPF program is triggered and set the tunnel metadata,
  41. # with remote_ip=172.16.1.200 and others.
  42. # 3) Outer tunnel header is prepended and route the packet to veth1's egress
  43. # 4) veth0's ingress queue receive the tunneled packet at namespace at_ns0
  44. # 5) Tunnel protocol handler, ex: vxlan_rcv, decap the packet
  45. # 6) Forward the packet to the overlay tnl dev
  46. PING_ARG="-c 3 -w 10 -q"
  47. ret=0
  48. GREEN='\033[0;92m'
  49. RED='\033[0;31m'
  50. NC='\033[0m' # No Color
  51. config_device()
  52. {
  53. ip netns add at_ns0
  54. ip link add veth0 type veth peer name veth1
  55. ip link set veth0 netns at_ns0
  56. ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
  57. ip netns exec at_ns0 ip link set dev veth0 up
  58. ip link set dev veth1 up mtu 1500
  59. ip addr add dev veth1 172.16.1.200/24
  60. }
  61. add_gre_tunnel()
  62. {
  63. # at_ns0 namespace
  64. ip netns exec at_ns0 \
  65. ip link add dev $DEV_NS type $TYPE seq key 2 \
  66. local 172.16.1.100 remote 172.16.1.200
  67. ip netns exec at_ns0 ip link set dev $DEV_NS up
  68. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  69. # root namespace
  70. ip link add dev $DEV type $TYPE key 2 external
  71. ip link set dev $DEV up
  72. ip addr add dev $DEV 10.1.1.200/24
  73. }
  74. add_ip6gretap_tunnel()
  75. {
  76. # assign ipv6 address
  77. ip netns exec at_ns0 ip addr add ::11/96 dev veth0
  78. ip netns exec at_ns0 ip link set dev veth0 up
  79. ip addr add dev veth1 ::22/96
  80. ip link set dev veth1 up
  81. # at_ns0 namespace
  82. ip netns exec at_ns0 \
  83. ip link add dev $DEV_NS type $TYPE seq flowlabel 0xbcdef key 2 \
  84. local ::11 remote ::22
  85. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  86. ip netns exec at_ns0 ip addr add dev $DEV_NS fc80::100/96
  87. ip netns exec at_ns0 ip link set dev $DEV_NS up
  88. # root namespace
  89. ip link add dev $DEV type $TYPE external
  90. ip addr add dev $DEV 10.1.1.200/24
  91. ip addr add dev $DEV fc80::200/24
  92. ip link set dev $DEV up
  93. }
  94. add_erspan_tunnel()
  95. {
  96. # at_ns0 namespace
  97. if [ "$1" == "v1" ]; then
  98. ip netns exec at_ns0 \
  99. ip link add dev $DEV_NS type $TYPE seq key 2 \
  100. local 172.16.1.100 remote 172.16.1.200 \
  101. erspan_ver 1 erspan 123
  102. else
  103. ip netns exec at_ns0 \
  104. ip link add dev $DEV_NS type $TYPE seq key 2 \
  105. local 172.16.1.100 remote 172.16.1.200 \
  106. erspan_ver 2 erspan_dir egress erspan_hwid 3
  107. fi
  108. ip netns exec at_ns0 ip link set dev $DEV_NS up
  109. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  110. # root namespace
  111. ip link add dev $DEV type $TYPE external
  112. ip link set dev $DEV up
  113. ip addr add dev $DEV 10.1.1.200/24
  114. }
  115. add_ip6erspan_tunnel()
  116. {
  117. # assign ipv6 address
  118. ip netns exec at_ns0 ip addr add ::11/96 dev veth0
  119. ip netns exec at_ns0 ip link set dev veth0 up
  120. ip addr add dev veth1 ::22/96
  121. ip link set dev veth1 up
  122. # at_ns0 namespace
  123. if [ "$1" == "v1" ]; then
  124. ip netns exec at_ns0 \
  125. ip link add dev $DEV_NS type $TYPE seq key 2 \
  126. local ::11 remote ::22 \
  127. erspan_ver 1 erspan 123
  128. else
  129. ip netns exec at_ns0 \
  130. ip link add dev $DEV_NS type $TYPE seq key 2 \
  131. local ::11 remote ::22 \
  132. erspan_ver 2 erspan_dir egress erspan_hwid 7
  133. fi
  134. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  135. ip netns exec at_ns0 ip link set dev $DEV_NS up
  136. # root namespace
  137. ip link add dev $DEV type $TYPE external
  138. ip addr add dev $DEV 10.1.1.200/24
  139. ip link set dev $DEV up
  140. }
  141. add_vxlan_tunnel()
  142. {
  143. # Set static ARP entry here because iptables set-mark works
  144. # on L3 packet, as a result not applying to ARP packets,
  145. # causing errors at get_tunnel_{key/opt}.
  146. # at_ns0 namespace
  147. ip netns exec at_ns0 \
  148. ip link add dev $DEV_NS type $TYPE \
  149. id 2 dstport 4789 gbp remote 172.16.1.200
  150. ip netns exec at_ns0 \
  151. ip link set dev $DEV_NS address 52:54:00:d9:01:00 up
  152. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  153. ip netns exec at_ns0 arp -s 10.1.1.200 52:54:00:d9:02:00
  154. ip netns exec at_ns0 iptables -A OUTPUT -j MARK --set-mark 0x800FF
  155. # root namespace
  156. ip link add dev $DEV type $TYPE external gbp dstport 4789
  157. ip link set dev $DEV address 52:54:00:d9:02:00 up
  158. ip addr add dev $DEV 10.1.1.200/24
  159. arp -s 10.1.1.100 52:54:00:d9:01:00
  160. }
  161. add_ip6vxlan_tunnel()
  162. {
  163. #ip netns exec at_ns0 ip -4 addr del 172.16.1.100 dev veth0
  164. ip netns exec at_ns0 ip -6 addr add ::11/96 dev veth0
  165. ip netns exec at_ns0 ip link set dev veth0 up
  166. #ip -4 addr del 172.16.1.200 dev veth1
  167. ip -6 addr add dev veth1 ::22/96
  168. ip link set dev veth1 up
  169. # at_ns0 namespace
  170. ip netns exec at_ns0 \
  171. ip link add dev $DEV_NS type $TYPE id 22 dstport 4789 \
  172. local ::11 remote ::22
  173. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  174. ip netns exec at_ns0 ip link set dev $DEV_NS up
  175. # root namespace
  176. ip link add dev $DEV type $TYPE external dstport 4789
  177. ip addr add dev $DEV 10.1.1.200/24
  178. ip link set dev $DEV up
  179. }
  180. add_geneve_tunnel()
  181. {
  182. # at_ns0 namespace
  183. ip netns exec at_ns0 \
  184. ip link add dev $DEV_NS type $TYPE \
  185. id 2 dstport 6081 remote 172.16.1.200
  186. ip netns exec at_ns0 ip link set dev $DEV_NS up
  187. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  188. # root namespace
  189. ip link add dev $DEV type $TYPE dstport 6081 external
  190. ip link set dev $DEV up
  191. ip addr add dev $DEV 10.1.1.200/24
  192. }
  193. add_ip6geneve_tunnel()
  194. {
  195. ip netns exec at_ns0 ip addr add ::11/96 dev veth0
  196. ip netns exec at_ns0 ip link set dev veth0 up
  197. ip addr add dev veth1 ::22/96
  198. ip link set dev veth1 up
  199. # at_ns0 namespace
  200. ip netns exec at_ns0 \
  201. ip link add dev $DEV_NS type $TYPE id 22 \
  202. remote ::22 # geneve has no local option
  203. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  204. ip netns exec at_ns0 ip link set dev $DEV_NS up
  205. # root namespace
  206. ip link add dev $DEV type $TYPE external
  207. ip addr add dev $DEV 10.1.1.200/24
  208. ip link set dev $DEV up
  209. }
  210. add_ipip_tunnel()
  211. {
  212. # at_ns0 namespace
  213. ip netns exec at_ns0 \
  214. ip link add dev $DEV_NS type $TYPE \
  215. local 172.16.1.100 remote 172.16.1.200
  216. ip netns exec at_ns0 ip link set dev $DEV_NS up
  217. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  218. # root namespace
  219. ip link add dev $DEV type $TYPE external
  220. ip link set dev $DEV up
  221. ip addr add dev $DEV 10.1.1.200/24
  222. }
  223. add_ipip6tnl_tunnel()
  224. {
  225. ip netns exec at_ns0 ip addr add ::11/96 dev veth0
  226. ip netns exec at_ns0 ip link set dev veth0 up
  227. ip addr add dev veth1 ::22/96
  228. ip link set dev veth1 up
  229. # at_ns0 namespace
  230. ip netns exec at_ns0 \
  231. ip link add dev $DEV_NS type $TYPE \
  232. local ::11 remote ::22
  233. ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
  234. ip netns exec at_ns0 ip link set dev $DEV_NS up
  235. # root namespace
  236. ip link add dev $DEV type $TYPE external
  237. ip addr add dev $DEV 10.1.1.200/24
  238. ip link set dev $DEV up
  239. }
  240. test_gre()
  241. {
  242. TYPE=gretap
  243. DEV_NS=gretap00
  244. DEV=gretap11
  245. ret=0
  246. check $TYPE
  247. config_device
  248. add_gre_tunnel
  249. attach_bpf $DEV gre_set_tunnel gre_get_tunnel
  250. ping $PING_ARG 10.1.1.100
  251. check_err $?
  252. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  253. check_err $?
  254. cleanup
  255. if [ $ret -ne 0 ]; then
  256. echo -e ${RED}"FAIL: $TYPE"${NC}
  257. return 1
  258. fi
  259. echo -e ${GREEN}"PASS: $TYPE"${NC}
  260. }
  261. test_ip6gre()
  262. {
  263. TYPE=ip6gre
  264. DEV_NS=ip6gre00
  265. DEV=ip6gre11
  266. ret=0
  267. check $TYPE
  268. config_device
  269. # reuse the ip6gretap function
  270. add_ip6gretap_tunnel
  271. attach_bpf $DEV ip6gretap_set_tunnel ip6gretap_get_tunnel
  272. # underlay
  273. ping6 $PING_ARG ::11
  274. # overlay: ipv4 over ipv6
  275. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  276. ping $PING_ARG 10.1.1.100
  277. check_err $?
  278. # overlay: ipv6 over ipv6
  279. ip netns exec at_ns0 ping6 $PING_ARG fc80::200
  280. check_err $?
  281. cleanup
  282. if [ $ret -ne 0 ]; then
  283. echo -e ${RED}"FAIL: $TYPE"${NC}
  284. return 1
  285. fi
  286. echo -e ${GREEN}"PASS: $TYPE"${NC}
  287. }
  288. test_ip6gretap()
  289. {
  290. TYPE=ip6gretap
  291. DEV_NS=ip6gretap00
  292. DEV=ip6gretap11
  293. ret=0
  294. check $TYPE
  295. config_device
  296. add_ip6gretap_tunnel
  297. attach_bpf $DEV ip6gretap_set_tunnel ip6gretap_get_tunnel
  298. # underlay
  299. ping6 $PING_ARG ::11
  300. # overlay: ipv4 over ipv6
  301. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  302. ping $PING_ARG 10.1.1.100
  303. check_err $?
  304. # overlay: ipv6 over ipv6
  305. ip netns exec at_ns0 ping6 $PING_ARG fc80::200
  306. check_err $?
  307. cleanup
  308. if [ $ret -ne 0 ]; then
  309. echo -e ${RED}"FAIL: $TYPE"${NC}
  310. return 1
  311. fi
  312. echo -e ${GREEN}"PASS: $TYPE"${NC}
  313. }
  314. test_erspan()
  315. {
  316. TYPE=erspan
  317. DEV_NS=erspan00
  318. DEV=erspan11
  319. ret=0
  320. check $TYPE
  321. config_device
  322. add_erspan_tunnel $1
  323. attach_bpf $DEV erspan_set_tunnel erspan_get_tunnel
  324. ping $PING_ARG 10.1.1.100
  325. check_err $?
  326. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  327. check_err $?
  328. cleanup
  329. if [ $ret -ne 0 ]; then
  330. echo -e ${RED}"FAIL: $TYPE"${NC}
  331. return 1
  332. fi
  333. echo -e ${GREEN}"PASS: $TYPE"${NC}
  334. }
  335. test_ip6erspan()
  336. {
  337. TYPE=ip6erspan
  338. DEV_NS=ip6erspan00
  339. DEV=ip6erspan11
  340. ret=0
  341. check $TYPE
  342. config_device
  343. add_ip6erspan_tunnel $1
  344. attach_bpf $DEV ip4ip6erspan_set_tunnel ip4ip6erspan_get_tunnel
  345. ping6 $PING_ARG ::11
  346. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  347. check_err $?
  348. cleanup
  349. if [ $ret -ne 0 ]; then
  350. echo -e ${RED}"FAIL: $TYPE"${NC}
  351. return 1
  352. fi
  353. echo -e ${GREEN}"PASS: $TYPE"${NC}
  354. }
  355. test_vxlan()
  356. {
  357. TYPE=vxlan
  358. DEV_NS=vxlan00
  359. DEV=vxlan11
  360. ret=0
  361. check $TYPE
  362. config_device
  363. add_vxlan_tunnel
  364. attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
  365. ping $PING_ARG 10.1.1.100
  366. check_err $?
  367. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  368. check_err $?
  369. cleanup
  370. if [ $ret -ne 0 ]; then
  371. echo -e ${RED}"FAIL: $TYPE"${NC}
  372. return 1
  373. fi
  374. echo -e ${GREEN}"PASS: $TYPE"${NC}
  375. }
  376. test_ip6vxlan()
  377. {
  378. TYPE=vxlan
  379. DEV_NS=ip6vxlan00
  380. DEV=ip6vxlan11
  381. ret=0
  382. check $TYPE
  383. config_device
  384. add_ip6vxlan_tunnel
  385. ip link set dev veth1 mtu 1500
  386. attach_bpf $DEV ip6vxlan_set_tunnel ip6vxlan_get_tunnel
  387. # underlay
  388. ping6 $PING_ARG ::11
  389. # ip4 over ip6
  390. ping $PING_ARG 10.1.1.100
  391. check_err $?
  392. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  393. check_err $?
  394. cleanup
  395. if [ $ret -ne 0 ]; then
  396. echo -e ${RED}"FAIL: ip6$TYPE"${NC}
  397. return 1
  398. fi
  399. echo -e ${GREEN}"PASS: ip6$TYPE"${NC}
  400. }
  401. test_geneve()
  402. {
  403. TYPE=geneve
  404. DEV_NS=geneve00
  405. DEV=geneve11
  406. ret=0
  407. check $TYPE
  408. config_device
  409. add_geneve_tunnel
  410. attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
  411. ping $PING_ARG 10.1.1.100
  412. check_err $?
  413. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  414. check_err $?
  415. cleanup
  416. if [ $ret -ne 0 ]; then
  417. echo -e ${RED}"FAIL: $TYPE"${NC}
  418. return 1
  419. fi
  420. echo -e ${GREEN}"PASS: $TYPE"${NC}
  421. }
  422. test_ip6geneve()
  423. {
  424. TYPE=geneve
  425. DEV_NS=ip6geneve00
  426. DEV=ip6geneve11
  427. ret=0
  428. check $TYPE
  429. config_device
  430. add_ip6geneve_tunnel
  431. attach_bpf $DEV ip6geneve_set_tunnel ip6geneve_get_tunnel
  432. ping $PING_ARG 10.1.1.100
  433. check_err $?
  434. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  435. check_err $?
  436. cleanup
  437. if [ $ret -ne 0 ]; then
  438. echo -e ${RED}"FAIL: ip6$TYPE"${NC}
  439. return 1
  440. fi
  441. echo -e ${GREEN}"PASS: ip6$TYPE"${NC}
  442. }
  443. test_ipip()
  444. {
  445. TYPE=ipip
  446. DEV_NS=ipip00
  447. DEV=ipip11
  448. ret=0
  449. check $TYPE
  450. config_device
  451. add_ipip_tunnel
  452. ip link set dev veth1 mtu 1500
  453. attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
  454. ping $PING_ARG 10.1.1.100
  455. check_err $?
  456. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  457. check_err $?
  458. cleanup
  459. if [ $ret -ne 0 ]; then
  460. echo -e ${RED}"FAIL: $TYPE"${NC}
  461. return 1
  462. fi
  463. echo -e ${GREEN}"PASS: $TYPE"${NC}
  464. }
  465. test_ipip6()
  466. {
  467. TYPE=ip6tnl
  468. DEV_NS=ipip6tnl00
  469. DEV=ipip6tnl11
  470. ret=0
  471. check $TYPE
  472. config_device
  473. add_ipip6tnl_tunnel
  474. ip link set dev veth1 mtu 1500
  475. attach_bpf $DEV ipip6_set_tunnel ipip6_get_tunnel
  476. # underlay
  477. ping6 $PING_ARG ::11
  478. # ip4 over ip6
  479. ping $PING_ARG 10.1.1.100
  480. check_err $?
  481. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  482. check_err $?
  483. cleanup
  484. if [ $ret -ne 0 ]; then
  485. echo -e ${RED}"FAIL: $TYPE"${NC}
  486. return 1
  487. fi
  488. echo -e ${GREEN}"PASS: $TYPE"${NC}
  489. }
  490. setup_xfrm_tunnel()
  491. {
  492. auth=0x$(printf '1%.0s' {1..40})
  493. enc=0x$(printf '2%.0s' {1..32})
  494. spi_in_to_out=0x1
  495. spi_out_to_in=0x2
  496. # at_ns0 namespace
  497. # at_ns0 -> root
  498. ip netns exec at_ns0 \
  499. ip xfrm state add src 172.16.1.100 dst 172.16.1.200 proto esp \
  500. spi $spi_in_to_out reqid 1 mode tunnel \
  501. auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc
  502. ip netns exec at_ns0 \
  503. ip xfrm policy add src 10.1.1.100/32 dst 10.1.1.200/32 dir out \
  504. tmpl src 172.16.1.100 dst 172.16.1.200 proto esp reqid 1 \
  505. mode tunnel
  506. # root -> at_ns0
  507. ip netns exec at_ns0 \
  508. ip xfrm state add src 172.16.1.200 dst 172.16.1.100 proto esp \
  509. spi $spi_out_to_in reqid 2 mode tunnel \
  510. auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc
  511. ip netns exec at_ns0 \
  512. ip xfrm policy add src 10.1.1.200/32 dst 10.1.1.100/32 dir in \
  513. tmpl src 172.16.1.200 dst 172.16.1.100 proto esp reqid 2 \
  514. mode tunnel
  515. # address & route
  516. ip netns exec at_ns0 \
  517. ip addr add dev veth0 10.1.1.100/32
  518. ip netns exec at_ns0 \
  519. ip route add 10.1.1.200 dev veth0 via 172.16.1.200 \
  520. src 10.1.1.100
  521. # root namespace
  522. # at_ns0 -> root
  523. ip xfrm state add src 172.16.1.100 dst 172.16.1.200 proto esp \
  524. spi $spi_in_to_out reqid 1 mode tunnel \
  525. auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc
  526. ip xfrm policy add src 10.1.1.100/32 dst 10.1.1.200/32 dir in \
  527. tmpl src 172.16.1.100 dst 172.16.1.200 proto esp reqid 1 \
  528. mode tunnel
  529. # root -> at_ns0
  530. ip xfrm state add src 172.16.1.200 dst 172.16.1.100 proto esp \
  531. spi $spi_out_to_in reqid 2 mode tunnel \
  532. auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc
  533. ip xfrm policy add src 10.1.1.200/32 dst 10.1.1.100/32 dir out \
  534. tmpl src 172.16.1.200 dst 172.16.1.100 proto esp reqid 2 \
  535. mode tunnel
  536. # address & route
  537. ip addr add dev veth1 10.1.1.200/32
  538. ip route add 10.1.1.100 dev veth1 via 172.16.1.100 src 10.1.1.200
  539. }
  540. test_xfrm_tunnel()
  541. {
  542. config_device
  543. > /sys/kernel/debug/tracing/trace
  544. setup_xfrm_tunnel
  545. tc qdisc add dev veth1 clsact
  546. tc filter add dev veth1 proto ip ingress bpf da obj test_tunnel_kern.o \
  547. sec xfrm_get_state
  548. ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
  549. sleep 1
  550. grep "reqid 1" /sys/kernel/debug/tracing/trace
  551. check_err $?
  552. grep "spi 0x1" /sys/kernel/debug/tracing/trace
  553. check_err $?
  554. grep "remote ip 0xac100164" /sys/kernel/debug/tracing/trace
  555. check_err $?
  556. cleanup
  557. if [ $ret -ne 0 ]; then
  558. echo -e ${RED}"FAIL: xfrm tunnel"${NC}
  559. return 1
  560. fi
  561. echo -e ${GREEN}"PASS: xfrm tunnel"${NC}
  562. }
  563. attach_bpf()
  564. {
  565. DEV=$1
  566. SET=$2
  567. GET=$3
  568. tc qdisc add dev $DEV clsact
  569. tc filter add dev $DEV egress bpf da obj test_tunnel_kern.o sec $SET
  570. tc filter add dev $DEV ingress bpf da obj test_tunnel_kern.o sec $GET
  571. }
  572. cleanup()
  573. {
  574. ip netns delete at_ns0 2> /dev/null
  575. ip link del veth1 2> /dev/null
  576. ip link del ipip11 2> /dev/null
  577. ip link del ipip6tnl11 2> /dev/null
  578. ip link del gretap11 2> /dev/null
  579. ip link del ip6gre11 2> /dev/null
  580. ip link del ip6gretap11 2> /dev/null
  581. ip link del vxlan11 2> /dev/null
  582. ip link del ip6vxlan11 2> /dev/null
  583. ip link del geneve11 2> /dev/null
  584. ip link del ip6geneve11 2> /dev/null
  585. ip link del erspan11 2> /dev/null
  586. ip link del ip6erspan11 2> /dev/null
  587. ip xfrm policy delete dir out src 10.1.1.200/32 dst 10.1.1.100/32 2> /dev/null
  588. ip xfrm policy delete dir in src 10.1.1.100/32 dst 10.1.1.200/32 2> /dev/null
  589. ip xfrm state delete src 172.16.1.100 dst 172.16.1.200 proto esp spi 0x1 2> /dev/null
  590. ip xfrm state delete src 172.16.1.200 dst 172.16.1.100 proto esp spi 0x2 2> /dev/null
  591. }
  592. cleanup_exit()
  593. {
  594. echo "CATCH SIGKILL or SIGINT, cleanup and exit"
  595. cleanup
  596. exit 0
  597. }
  598. check()
  599. {
  600. ip link help 2>&1 | grep -q "\s$1\s"
  601. if [ $? -ne 0 ];then
  602. echo "SKIP $1: iproute2 not support"
  603. cleanup
  604. return 1
  605. fi
  606. }
  607. enable_debug()
  608. {
  609. echo 'file ip_gre.c +p' > /sys/kernel/debug/dynamic_debug/control
  610. echo 'file ip6_gre.c +p' > /sys/kernel/debug/dynamic_debug/control
  611. echo 'file vxlan.c +p' > /sys/kernel/debug/dynamic_debug/control
  612. echo 'file geneve.c +p' > /sys/kernel/debug/dynamic_debug/control
  613. echo 'file ipip.c +p' > /sys/kernel/debug/dynamic_debug/control
  614. }
  615. check_err()
  616. {
  617. if [ $ret -eq 0 ]; then
  618. ret=$1
  619. fi
  620. }
  621. bpf_tunnel_test()
  622. {
  623. echo "Testing GRE tunnel..."
  624. test_gre
  625. echo "Testing IP6GRE tunnel..."
  626. test_ip6gre
  627. echo "Testing IP6GRETAP tunnel..."
  628. test_ip6gretap
  629. echo "Testing ERSPAN tunnel..."
  630. test_erspan v2
  631. echo "Testing IP6ERSPAN tunnel..."
  632. test_ip6erspan v2
  633. echo "Testing VXLAN tunnel..."
  634. test_vxlan
  635. echo "Testing IP6VXLAN tunnel..."
  636. test_ip6vxlan
  637. echo "Testing GENEVE tunnel..."
  638. test_geneve
  639. echo "Testing IP6GENEVE tunnel..."
  640. test_ip6geneve
  641. echo "Testing IPIP tunnel..."
  642. test_ipip
  643. echo "Testing IPIP6 tunnel..."
  644. test_ipip6
  645. echo "Testing IPSec tunnel..."
  646. test_xfrm_tunnel
  647. }
  648. trap cleanup 0 3 6
  649. trap cleanup_exit 2 9
  650. cleanup
  651. bpf_tunnel_test
  652. exit 0