2 # SPDX-License-Identifier: GPL-2.0
4 # Test various bareudp tunnel configurations.
6 # The bareudp module allows to tunnel network protocols like IP or MPLS over
7 # UDP, without adding any intermediate header. This scripts tests several
8 # configurations of bareudp (using IPv4 or IPv6 as underlay and transporting
9 # IPv4, IPv6 or MPLS packets on the overlay).
13 # * A chain of 4 network namespaces, connected with veth pairs. Each veth
14 # is assigned an IPv4 and an IPv6 address. A host-route allows a veth to
17 # * NS0 and NS3 are at the extremities of the chain. They have additional
18 # IPv4 and IPv6 addresses on their loopback device. Routes are added in NS0
19 # and NS3, so that they can communicate using these overlay IP addresses.
20 # For IPv4 and IPv6 reachability tests, the route simply sets the peer's
21 # veth address as gateway. For MPLS reachability tests, an MPLS header is
22 # also pushed before the IP header.
24 # * NS1 and NS2 are the intermediate namespaces. They use a bareudp device to
25 # encapsulate the traffic into UDP.
27 # +-----------------------------------------------------------------------+
31 # | * IPv4 address: 192.0.2.100/32 |
32 # | * IPv6 address: 2001:db8::100/128 |
33 # | * IPv6 address: 2001:db8::200/128 |
34 # | * IPv4 route: 192.0.2.103/32 reachable via 192.0.2.11 |
35 # | * IPv6 route: 2001:db8::103/128 reachable via 2001:db8::11 |
36 # | * IPv6 route: 2001:db8::203/128 reachable via 2001:db8::11 |
37 # | (encapsulated with MPLS label 203) |
40 # | ^ * IPv4 address: 192.0.2.10, peer 192.0.2.11/32 |
41 # | | * IPv6 address: 2001:db8::10, peer 2001:db8::11/128 |
43 # +---+-------------------------------------------------------------------+
45 # | Traffic type: IP or MPLS (depending on test)
47 # +---+-------------------------------------------------------------------+
52 # | * IPv4 address: 192.0.2.11, peer 192.0.2.10/32 |
53 # | * IPv6 address: 2001:db8::11, peer 2001:db8::10/128 |
56 # | * Encapsulate IP or MPLS packets received on veth10 into UDP |
57 # | and send the resulting packets through veth12. |
58 # | * Decapsulate bareudp packets (either IP or MPLS, over UDP) |
59 # | received on veth12 and send the inner packets through veth10. |
62 # | ^ * IPv4 address: 192.0.2.21, peer 192.0.2.22/32 |
63 # | | * IPv6 address: 2001:db8::21, peer 2001:db8::22/128 |
65 # +---+-------------------------------------------------------------------+
67 # | Traffic type: IP or MPLS (depending on test), over UDP
69 # +---+-------------------------------------------------------------------+
74 # | * IPv4 address: 192.0.2.22, peer 192.0.2.21/32 |
75 # | * IPv6 address: 2001:db8::22, peer 2001:db8::21/128 |
78 # | * Decapsulate bareudp packets (either IP or MPLS, over UDP) |
79 # | received on veth21 and send the inner packets through veth23. |
80 # | * Encapsulate IP or MPLS packets received on veth23 into UDP |
81 # | and send the resulting packets through veth21. |
84 # | ^ * IPv4 address: 192.0.2.32, peer 192.0.2.33/32 |
85 # | | * IPv6 address: 2001:db8::32, peer 2001:db8::33/128 |
87 # +---+-------------------------------------------------------------------+
89 # | Traffic type: IP or MPLS (depending on test)
91 # +---+-------------------------------------------------------------------+
95 # | * IPv4 address: 192.0.2.33, peer 192.0.2.32/32 |
96 # | * IPv6 address: 2001:db8::33, peer 2001:db8::32/128 |
99 # | * IPv4 address: 192.0.2.103/32 |
100 # | * IPv6 address: 2001:db8::103/128 |
101 # | * IPv6 address: 2001:db8::203/128 |
102 # | * IPv4 route: 192.0.2.100/32 reachable via 192.0.2.32 |
103 # | * IPv6 route: 2001:db8::100/128 reachable via 2001:db8::32 |
104 # | * IPv6 route: 2001:db8::200/128 reachable via 2001:db8::32 |
105 # | (encapsulated with MPLS label 200) |
107 # +-----------------------------------------------------------------------+
109 ERR
=4 # Return 4 by default, which is the SKIP code for kselftest
113 readonly NS0
=$
(mktemp
-u ns0-XXXXXXXX
)
114 readonly NS1
=$
(mktemp
-u ns1-XXXXXXXX
)
115 readonly NS2
=$
(mktemp
-u ns2-XXXXXXXX
)
116 readonly NS3
=$
(mktemp
-u ns3-XXXXXXXX
)
118 # Exit the script after having removed the network namespaces it created
122 # * The list of network namespaces to delete before exiting.
127 ip netns delete
"${ns}" 2>/dev
/null || true
130 if [ "${ERR}" -eq 4 ]; then
131 echo "Error: Setting up the testing environment failed." >&2
137 # Create the four network namespaces used by the script (NS0, NS1, NS2 and NS3)
139 # New namespaces are cleaned up manually in case of error, to ensure that only
140 # namespaces created by this script are deleted.
143 ip netns add
"${NS0}" || exit_cleanup
144 ip netns add
"${NS1}" || exit_cleanup
"${NS0}"
145 ip netns add
"${NS2}" || exit_cleanup "${NS0}" "${NS1}"
146 ip netns add
"${NS3}" || exit_cleanup "${NS0}" "${NS1}" "${NS2}"
149 # The trap function handler
153 exit_cleanup
"${NS0}" "${NS1}" "${NS2}" "${NS3}"
156 # Configure a network interface using a host route
160 # * $1: the netns the network interface resides in,
161 # * $2: the network interface name,
162 # * $3: the local IPv4 address to assign to this interface,
163 # * $4: the IPv4 address of the remote network interface,
164 # * $5: the local IPv6 address to assign to this interface,
165 # * $6: the IPv6 address of the remote network interface.
169 local NS
="${1}"; readonly NS
170 local DEV
="${2}"; readonly DEV
171 local LOCAL_IP4
="${3}"; readonly LOCAL_IP4
172 local PEER_IP4
="${4}"; readonly PEER_IP4
173 local LOCAL_IP6
="${5}"; readonly LOCAL_IP6
174 local PEER_IP6
="${6}"; readonly PEER_IP6
176 ip
-netns "${NS}" link
set dev
"${DEV}" up
177 ip
-netns "${NS}" address add dev "${DEV}" "${LOCAL_IP4}" peer "${PEER_IP4}"
178 ip
-netns "${NS}" address add dev "${DEV}" "${LOCAL_IP6}" peer "${PEER_IP6}" nodad
181 # Create base networking topology:
183 # * set up the loopback device in all network namespaces (NS0..NS3),
184 # * set up a veth pair to connect each netns in sequence (NS0 with NS1,
185 # NS1 with NS2, etc.),
186 # * add and IPv4 and an IPv6 address on each veth interface,
187 # * prepare the ingress qdiscs in the intermediate namespaces.
191 for ns
in "${NS0}" "${NS1}" "${NS2}" "${NS3}"; do
192 ip
-netns "${ns}" link
set dev lo up
195 ip link add name veth01 netns
"${NS0}" type veth peer name veth10 netns
"${NS1}"
196 ip link add name veth12 netns
"${NS1}" type veth peer name veth21 netns
"${NS2}"
197 ip link add name veth23 netns
"${NS2}" type veth peer name veth32 netns
"${NS3}"
198 iface_config
"${NS0}" veth01
192.0.2.10 192.0.2.11/32 2001:db8
::10 2001:db8
::11/128
199 iface_config
"${NS1}" veth10
192.0.2.11 192.0.2.10/32 2001:db8
::11 2001:db8
::10/128
200 iface_config
"${NS1}" veth12
192.0.2.21 192.0.2.22/32 2001:db8
::21 2001:db8
::22/128
201 iface_config
"${NS2}" veth21
192.0.2.22 192.0.2.21/32 2001:db8
::22 2001:db8
::21/128
202 iface_config
"${NS2}" veth23
192.0.2.32 192.0.2.33/32 2001:db8
::32 2001:db8
::33/128
203 iface_config
"${NS3}" veth32
192.0.2.33 192.0.2.32/32 2001:db8
::33 2001:db8
::32/128
205 tc
-netns "${NS1}" qdisc add dev veth10 ingress
206 tc
-netns "${NS2}" qdisc add dev veth23 ingress
209 # Set up the IPv4, IPv6 and MPLS overlays.
211 # Configuration is similar for all protocols:
213 # * add an overlay IP address on the loopback interface of each edge
215 # * route these IP addresses via the intermediate namespaces (for the MPLS
216 # tests, this is also where MPLS encapsulation is done),
217 # * add routes for these IP addresses (or MPLS labels) in the intermediate
220 # The bareudp encapsulation isn't configured in setup_overlay_*(). That will be
221 # done just before running the reachability tests.
225 # Add the overlay IP addresses and route them through the veth devices
226 ip
-netns "${NS0}" address add
192.0.2.100/32 dev lo
227 ip
-netns "${NS3}" address add
192.0.2.103/32 dev lo
228 ip
-netns "${NS0}" route add
192.0.2.103/32 src
192.0.2.100 via
192.0.2.11
229 ip
-netns "${NS3}" route add
192.0.2.100/32 src
192.0.2.103 via
192.0.2.32
231 # Route the overlay addresses in the intermediate namespaces
232 # (used after bareudp decapsulation)
233 ip netns
exec "${NS1}" sysctl
-qw net.ipv4.ip_forward
=1
234 ip netns
exec "${NS2}" sysctl
-qw net.ipv4.ip_forward
=1
235 ip
-netns "${NS1}" route add
192.0.2.100/32 via
192.0.2.10
236 ip
-netns "${NS2}" route add
192.0.2.103/32 via
192.0.2.33
238 # The intermediate namespaces don't have routes for the reverse path,
239 # as it will be handled by tc. So we need to ensure that rp_filter is
240 # not going to block the traffic.
241 ip netns
exec "${NS1}" sysctl
-qw net.ipv4.conf.all.rp_filter
=0
242 ip netns
exec "${NS2}" sysctl
-qw net.ipv4.conf.all.rp_filter
=0
243 ip netns
exec "${NS1}" sysctl
-qw net.ipv4.conf.default.rp_filter
=0
244 ip netns
exec "${NS2}" sysctl
-qw net.ipv4.conf.default.rp_filter
=0
249 # Add the overlay IP addresses and route them through the veth devices
250 ip
-netns "${NS0}" address add
2001:db8
::100/128 dev lo
251 ip
-netns "${NS3}" address add
2001:db8
::103/128 dev lo
252 ip
-netns "${NS0}" route add
2001:db8
::103/128 src
2001:db8
::100 via
2001:db8
::11
253 ip
-netns "${NS3}" route add
2001:db8
::100/128 src
2001:db8
::103 via
2001:db8
::32
255 # Route the overlay addresses in the intermediate namespaces
256 # (used after bareudp decapsulation)
257 ip netns
exec "${NS1}" sysctl
-qw net.ipv6.conf.all.forwarding
=1
258 ip netns
exec "${NS2}" sysctl
-qw net.ipv6.conf.all.forwarding
=1
259 ip
-netns "${NS1}" route add
2001:db8
::100/128 via
2001:db8
::10
260 ip
-netns "${NS2}" route add
2001:db8
::103/128 via
2001:db8
::33
265 # Add specific overlay IP addresses, routed over MPLS
266 ip
-netns "${NS0}" address add
2001:db8
::200/128 dev lo
267 ip
-netns "${NS3}" address add
2001:db8
::203/128 dev lo
268 ip
-netns "${NS0}" route add
2001:db8
::203/128 src
2001:db8
::200 encap mpls
203 via
2001:db8
::11
269 ip
-netns "${NS3}" route add
2001:db8
::200/128 src
2001:db8
::203 encap mpls
200 via
2001:db8
::32
271 # Route the MPLS packets in the intermediate namespaces
272 # (used after bareudp decapsulation)
273 ip netns
exec "${NS1}" sysctl
-qw net.mpls.platform_labels
=256
274 ip netns
exec "${NS2}" sysctl
-qw net.mpls.platform_labels
=256
275 ip
-netns "${NS1}" -family mpls route add
200 via inet6
2001:db8
::10
276 ip
-netns "${NS2}" -family mpls route add
203 via inet6
2001:db8
::33
279 # Run "ping" from NS0 and print the result
283 # * $1: the variant of ping to use (normally either "ping" or "ping6"),
284 # * $2: the IP address to ping,
285 # * $3: a human readable description of the purpose of the test.
287 # If the test fails and PAUSE_ON_FAIL is active, the user is given the
288 # possibility to continue with the next test or to quit immediately.
292 local PING
="$1"; readonly PING
293 local IP
="$2"; readonly IP
294 local MSG
="$3"; readonly MSG
297 printf "TEST: %-60s " "${MSG}"
300 ip netns
exec "${NS0}" "${PING}" -w 5 -c 1 "${IP}" > /dev
/null
2>&1
304 if [ "${RET}" -eq 0 ]; then
309 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
310 printf "\nHit enter to continue, 'q' to quit\n"
312 if [ "$a" = "q" ]; then
319 # Run reachability tests
323 # * $1: human readable string describing the underlay protocol.
325 # $IPV4, $IPV6, $MPLS_UC and $MULTIPROTO are inherited from the calling
330 local UNDERLAY
="$1"; readonly UNDERLAY
334 if [ "${MULTIPROTO}" = "multiproto" ]; then
335 MODE
=" (multiproto mode)"
341 ping_test_one
"ping" "192.0.2.103" "IPv4 packets over ${UNDERLAY}${MODE}"
344 ping_test_one
"${PING6}" "2001:db8::103" "IPv6 packets over ${UNDERLAY}${MODE}"
346 if [ $MPLS_UC ]; then
347 ping_test_one
"${PING6}" "2001:db8::203" "Unicast MPLS packets over ${UNDERLAY}${MODE}"
351 # Set up a bareudp overlay and run reachability tests over IPv4 and IPv6
355 # * $1: the packet type (protocol) to be handled by bareudp,
356 # * $2: a flag to activate or deactivate bareudp's "multiproto" mode.
360 local ETHERTYPE
="$1"; readonly ETHERTYPE
361 local MULTIPROTO
="$2"; readonly MULTIPROTO
366 case "${ETHERTYPE}" in
369 if [ "${MULTIPROTO}" = "multiproto" ]; then
394 # Create the bareudp devices in the intermediate namespaces
395 ip
-netns "${NS1}" link add name bareudp_ns1 up type bareudp dstport 6635 ethertype "${ETHERTYPE}" "${MULTIPROTO}"
396 ip
-netns "${NS2}" link add name bareudp_ns2 up type bareudp dstport 6635 ethertype "${ETHERTYPE}" "${MULTIPROTO}"
400 # Encapsulation instructions for bareudp over IPv4
401 tc
-netns "${NS1}" filter add dev veth10 ingress protocol ipv4 \
402 flower dst_ip
192.0.2.103/32 \
403 action tunnel_key
set src_ip
192.0.2.21 dst_ip
192.0.2.22 id
0 \
404 action mirred egress redirect dev bareudp_ns1
405 tc
-netns "${NS2}" filter add dev veth23 ingress protocol ipv4 \
406 flower dst_ip
192.0.2.100/32 \
407 action tunnel_key
set src_ip
192.0.2.22 dst_ip
192.0.2.21 id
0 \
408 action mirred egress redirect dev bareudp_ns2
413 # Encapsulation instructions for bareudp over IPv4
414 tc
-netns "${NS1}" filter add dev veth10 ingress protocol ipv6 \
415 flower dst_ip
2001:db8
::103/128 \
416 action tunnel_key
set src_ip
192.0.2.21 dst_ip
192.0.2.22 id
0 \
417 action mirred egress redirect dev bareudp_ns1
418 tc
-netns "${NS2}" filter add dev veth23 ingress protocol ipv6 \
419 flower dst_ip
2001:db8
::100/128 \
420 action tunnel_key
set src_ip
192.0.2.22 dst_ip
192.0.2.21 id
0 \
421 action mirred egress redirect dev bareudp_ns2
424 # MPLS (unicast) over UDPv4
425 if [ $MPLS_UC ]; then
426 ip netns
exec "${NS1}" sysctl
-qw net.mpls.conf.bareudp_ns1.input
=1
427 ip netns
exec "${NS2}" sysctl
-qw net.mpls.conf.bareudp_ns2.input
=1
429 # Encapsulation instructions for bareudp over IPv4
430 tc
-netns "${NS1}" filter add dev veth10 ingress protocol mpls_uc \
431 flower mpls_label
203 \
432 action tunnel_key
set src_ip
192.0.2.21 dst_ip
192.0.2.22 id
0 \
433 action mirred egress redirect dev bareudp_ns1
434 tc
-netns "${NS2}" filter add dev veth23 ingress protocol mpls_uc \
435 flower mpls_label
200 \
436 action tunnel_key
set src_ip
192.0.2.22 dst_ip
192.0.2.21 id
0 \
437 action mirred egress redirect dev bareudp_ns2
443 # Cleanup bareudp encapsulation instructions, as they were specific to
444 # the IPv4 underlay, before setting up and testing the IPv6 underlay
445 tc
-netns "${NS1}" filter delete dev veth10 ingress
446 tc
-netns "${NS2}" filter delete dev veth23 ingress
450 # New encapsulation instructions for bareudp over IPv6
451 tc
-netns "${NS1}" filter add dev veth10 ingress protocol ipv4 \
452 flower dst_ip
192.0.2.103/32 \
453 action tunnel_key
set src_ip
2001:db8
::21 dst_ip
2001:db8
::22 id
0 \
454 action mirred egress redirect dev bareudp_ns1
455 tc
-netns "${NS2}" filter add dev veth23 ingress protocol ipv4 \
456 flower dst_ip
192.0.2.100/32 \
457 action tunnel_key
set src_ip
2001:db8
::22 dst_ip
2001:db8
::21 id
0 \
458 action mirred egress redirect dev bareudp_ns2
463 # New encapsulation instructions for bareudp over IPv6
464 tc
-netns "${NS1}" filter add dev veth10 ingress protocol ipv6 \
465 flower dst_ip
2001:db8
::103/128 \
466 action tunnel_key
set src_ip
2001:db8
::21 dst_ip
2001:db8
::22 id
0 \
467 action mirred egress redirect dev bareudp_ns1
468 tc
-netns "${NS2}" filter add dev veth23 ingress protocol ipv6 \
469 flower dst_ip
2001:db8
::100/128 \
470 action tunnel_key
set src_ip
2001:db8
::22 dst_ip
2001:db8
::21 id
0 \
471 action mirred egress redirect dev bareudp_ns2
474 # MPLS (unicast) over UDPv6
475 if [ $MPLS_UC ]; then
476 # New encapsulation instructions for bareudp over IPv6
477 tc
-netns "${NS1}" filter add dev veth10 ingress protocol mpls_uc \
478 flower mpls_label
203 \
479 action tunnel_key
set src_ip
2001:db8
::21 dst_ip
2001:db8
::22 id
0 \
480 action mirred egress redirect dev bareudp_ns1
481 tc
-netns "${NS2}" filter add dev veth23 ingress protocol mpls_uc \
482 flower mpls_label
200 \
483 action tunnel_key
set src_ip
2001:db8
::22 dst_ip
2001:db8
::21 id
0 \
484 action mirred egress redirect dev bareudp_ns2
490 tc
-netns "${NS1}" filter delete dev veth10 ingress
491 tc
-netns "${NS2}" filter delete dev veth23 ingress
492 ip
-netns "${NS1}" link delete bareudp_ns1
493 ip
-netns "${NS2}" link delete bareudp_ns2
498 ip link
help 2>&1 |
grep -q bareudp
499 if [ $?
-ne 0 ]; then
500 echo "Missing bareudp support in iproute2" >&2
504 # Use ping6 on systems where ping doesn't handle IPv6
505 ping -w 1 -c 1 ::1 > /dev
/null
2>&1 || PING6
="ping6"
510 echo "Usage: $0 [-p]"
517 p
) PAUSE_ON_FAIL
="yes";;
524 # Create namespaces before setting up the exit trap.
525 # Otherwise, exit_cleanup_all() could delete namespaces that were not created
530 trap exit_cleanup_all EXIT
537 test_overlay ipv4 nomultiproto
538 test_overlay ipv6 nomultiproto
539 test_overlay ipv4 multiproto
540 test_overlay mpls_uc nomultiproto
542 if [ "${ERR}" -eq 1 ]; then
543 echo "Some tests failed." >&2