xref: /linux/tools/testing/selftests/net/forwarding/router_nh.sh (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +-------------------------+  +-------------------------+
5# | H1                      |  |                      H2 |
6# |               $h1 +     |  |               $h2 +     |
7# |      192.0.2.2/24 |     |  |   198.51.100.2/24 |     |
8# |  2001:db8:1::2/64 |     |  |  2001:db8:2::2/64 |     |
9# +-------------------|-----+  +-------------------|-----+
10#                     |                            |
11# +-------------------|----------------------------|-----+
12# | R1                |                            |     |
13# |              $rp1 +                       $rp2 +     |
14# |      192.0.2.1/24              198.51.100.1/24       |
15# |  2001:db8:1::1/64             2001:db8:2::1/64       |
16# +------------------------------------------------------+
17
18ALL_TESTS="
19	ping_ipv4
20	ping_ipv6
21"
22
23NUM_NETIFS=4
24source lib.sh
25source tc_common.sh
26
27h1_create()
28{
29	vrf_create "vrf-h1"
30	ip link set dev $h1 master vrf-h1
31
32	ip link set dev vrf-h1 up
33	ip link set dev $h1 up
34
35	ip address add 192.0.2.2/24 dev $h1
36	ip address add 2001:db8:1::2/64 dev $h1
37
38	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
39	ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
40}
41
42h1_destroy()
43{
44	ip route del 2001:db8:2::/64 vrf vrf-h1
45	ip route del 198.51.100.0/24 vrf vrf-h1
46
47	ip address del 2001:db8:1::2/64 dev $h1
48	ip address del 192.0.2.2/24 dev $h1
49
50	ip link set dev $h1 down
51	vrf_destroy "vrf-h1"
52}
53
54h2_create()
55{
56	vrf_create "vrf-h2"
57	ip link set dev $h2 master vrf-h2
58
59	ip link set dev vrf-h2 up
60	ip link set dev $h2 up
61
62	ip address add 198.51.100.2/24 dev $h2
63	ip address add 2001:db8:2::2/64 dev $h2
64
65	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
66	ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
67}
68
69h2_destroy()
70{
71	ip route del 2001:db8:1::/64 vrf vrf-h2
72	ip route del 192.0.2.0/24 vrf vrf-h2
73
74	ip address del 2001:db8:2::2/64 dev $h2
75	ip address del 198.51.100.2/24 dev $h2
76
77	ip link set dev $h2 down
78	vrf_destroy "vrf-h2"
79}
80
81router_create()
82{
83	ip link set dev $rp1 up
84	ip link set dev $rp2 up
85
86	tc qdisc add dev $rp2 clsact
87
88	ip address add 192.0.2.1/24 dev $rp1
89	ip address add 2001:db8:1::1/64 dev $rp1
90
91	ip address add 198.51.100.1/24 dev $rp2
92	ip address add 2001:db8:2::1/64 dev $rp2
93}
94
95router_destroy()
96{
97	ip address del 2001:db8:2::1/64 dev $rp2
98	ip address del 198.51.100.1/24 dev $rp2
99
100	ip address del 2001:db8:1::1/64 dev $rp1
101	ip address del 192.0.2.1/24 dev $rp1
102
103	tc qdisc del dev $rp2 clsact
104
105	ip link set dev $rp2 down
106	ip link set dev $rp1 down
107}
108
109routing_nh_obj()
110{
111	# Create the nexthops as AF_INET6, so that IPv4 and IPv6 routes could
112	# use them.
113	ip -6 nexthop add id 101 dev $rp1
114	ip -6 nexthop add id 102 dev $rp2
115
116	ip route replace 192.0.2.0/24 nhid 101
117	ip route replace 2001:db8:1::/64 nhid 101
118	ip route replace 198.51.100.0/24 nhid 102
119	ip route replace 2001:db8:2::/64 nhid 102
120}
121
122setup_prepare()
123{
124	h1=${NETIFS[p1]}
125	rp1=${NETIFS[p2]}
126
127	rp2=${NETIFS[p3]}
128	h2=${NETIFS[p4]}
129
130	rp1mac=$(mac_get $rp1)
131
132	vrf_prepare
133
134	h1_create
135	h2_create
136
137	router_create
138
139	forwarding_enable
140}
141
142cleanup()
143{
144	pre_cleanup
145
146	forwarding_restore
147
148	router_destroy
149
150	h2_destroy
151	h1_destroy
152
153	vrf_cleanup
154}
155
156ping_ipv4()
157{
158	ping_test $h1 198.51.100.2
159}
160
161ping_ipv6()
162{
163	ping6_test $h1 2001:db8:2::2
164}
165
166trap cleanup EXIT
167
168setup_prepare
169setup_wait
170routing_nh_obj
171
172tests_run
173
174exit $EXIT_STATUS
175