Skip to content

Commit 166b5a7

Browse files
alan-maguireborkmann
authored andcommitted
selftests_bpf: extend test_tc_tunnel for UDP encap
commit 868d523 ("bpf: add bpf_skb_adjust_room encap flags") introduced support to bpf_skb_adjust_room for GSO-friendly GRE and UDP encapsulation and later introduced associated test_tc_tunnel tests. Here those tests are extended to cover UDP encapsulation also. Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent c695865 commit 166b5a7

3 files changed

Lines changed: 143 additions & 48 deletions

File tree

‎tools/testing/selftests/bpf/config‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ CONFIG_XDP_SOCKETS=y
2525
CONFIG_FTRACE_SYSCALLS=y
2626
CONFIG_IPV6_TUNNEL=y
2727
CONFIG_IPV6_GRE=y
28+
CONFIG_NET_FOU=m
29+
CONFIG_NET_FOU_IP_TUNNELS=y
30+
CONFIG_IPV6_FOU=m
31+
CONFIG_IPV6_FOU_TUNNEL=m

‎tools/testing/selftests/bpf/progs/test_tc_tunnel.c‎

Lines changed: 98 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/ip.h>
1313
#include <linux/ipv6.h>
1414
#include <linux/tcp.h>
15+
#include <linux/udp.h>
1516
#include <linux/pkt_cls.h>
1617
#include <linux/types.h>
1718

@@ -20,16 +21,27 @@
2021

2122
static const int cfg_port = 8000;
2223

23-
struct grev4hdr {
24-
struct iphdr ip;
24+
static const int cfg_udp_src = 20000;
25+
static const int cfg_udp_dst = 5555;
26+
27+
struct gre_hdr {
2528
__be16 flags;
2629
__be16 protocol;
2730
} __attribute__((packed));
2831

29-
struct grev6hdr {
32+
union l4hdr {
33+
struct udphdr udp;
34+
struct gre_hdr gre;
35+
};
36+
37+
struct v4hdr {
38+
struct iphdr ip;
39+
union l4hdr l4hdr;
40+
} __attribute__((packed));
41+
42+
struct v6hdr {
3043
struct ipv6hdr ip;
31-
__be16 flags;
32-
__be16 protocol;
44+
union l4hdr l4hdr;
3345
} __attribute__((packed));
3446

3547
static __always_inline void set_ipv4_csum(struct iphdr *iph)
@@ -47,10 +59,10 @@ static __always_inline void set_ipv4_csum(struct iphdr *iph)
4759
iph->check = ~((csum & 0xffff) + (csum >> 16));
4860
}
4961

50-
static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
62+
static __always_inline int encap_ipv4(struct __sk_buff *skb, __u8 encap_proto)
5163
{
52-
struct grev4hdr h_outer;
5364
struct iphdr iph_inner;
65+
struct v4hdr h_outer;
5466
struct tcphdr tcph;
5567
__u64 flags;
5668
int olen;
@@ -70,12 +82,29 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
7082
if (tcph.dest != __bpf_constant_htons(cfg_port))
7183
return TC_ACT_OK;
7284

85+
olen = sizeof(h_outer.ip);
86+
7387
flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV4;
74-
if (with_gre) {
88+
switch (encap_proto) {
89+
case IPPROTO_GRE:
7590
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE;
76-
olen = sizeof(h_outer);
77-
} else {
78-
olen = sizeof(h_outer.ip);
91+
olen += sizeof(h_outer.l4hdr.gre);
92+
h_outer.l4hdr.gre.protocol = bpf_htons(ETH_P_IP);
93+
h_outer.l4hdr.gre.flags = 0;
94+
break;
95+
case IPPROTO_UDP:
96+
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP;
97+
olen += sizeof(h_outer.l4hdr.udp);
98+
h_outer.l4hdr.udp.source = __bpf_constant_htons(cfg_udp_src);
99+
h_outer.l4hdr.udp.dest = __bpf_constant_htons(cfg_udp_dst);
100+
h_outer.l4hdr.udp.check = 0;
101+
h_outer.l4hdr.udp.len = bpf_htons(bpf_ntohs(iph_inner.tot_len) +
102+
sizeof(h_outer.l4hdr.udp));
103+
break;
104+
case IPPROTO_IPIP:
105+
break;
106+
default:
107+
return TC_ACT_OK;
79108
}
80109

81110
/* add room between mac and network header */
@@ -85,16 +114,10 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
85114
/* prepare new outer network header */
86115
h_outer.ip = iph_inner;
87116
h_outer.ip.tot_len = bpf_htons(olen +
88-
bpf_htons(h_outer.ip.tot_len));
89-
if (with_gre) {
90-
h_outer.ip.protocol = IPPROTO_GRE;
91-
h_outer.protocol = bpf_htons(ETH_P_IP);
92-
h_outer.flags = 0;
93-
} else {
94-
h_outer.ip.protocol = IPPROTO_IPIP;
95-
}
117+
bpf_ntohs(h_outer.ip.tot_len));
118+
h_outer.ip.protocol = encap_proto;
96119

97-
set_ipv4_csum((void *)&h_outer.ip);
120+
set_ipv4_csum(&h_outer.ip);
98121

99122
/* store new outer network header */
100123
if (bpf_skb_store_bytes(skb, ETH_HLEN, &h_outer, olen,
@@ -104,11 +127,12 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
104127
return TC_ACT_OK;
105128
}
106129

107-
static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
130+
static __always_inline int encap_ipv6(struct __sk_buff *skb, __u8 encap_proto)
108131
{
109132
struct ipv6hdr iph_inner;
110-
struct grev6hdr h_outer;
133+
struct v6hdr h_outer;
111134
struct tcphdr tcph;
135+
__u16 tot_len;
112136
__u64 flags;
113137
int olen;
114138

@@ -124,15 +148,32 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
124148
if (tcph.dest != __bpf_constant_htons(cfg_port))
125149
return TC_ACT_OK;
126150

151+
olen = sizeof(h_outer.ip);
152+
127153
flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV6;
128-
if (with_gre) {
154+
switch (encap_proto) {
155+
case IPPROTO_GRE:
129156
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE;
130-
olen = sizeof(h_outer);
131-
} else {
132-
olen = sizeof(h_outer.ip);
157+
olen += sizeof(h_outer.l4hdr.gre);
158+
h_outer.l4hdr.gre.protocol = bpf_htons(ETH_P_IPV6);
159+
h_outer.l4hdr.gre.flags = 0;
160+
break;
161+
case IPPROTO_UDP:
162+
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP;
163+
olen += sizeof(h_outer.l4hdr.udp);
164+
h_outer.l4hdr.udp.source = __bpf_constant_htons(cfg_udp_src);
165+
h_outer.l4hdr.udp.dest = __bpf_constant_htons(cfg_udp_dst);
166+
tot_len = bpf_ntohs(iph_inner.payload_len) + sizeof(iph_inner) +
167+
sizeof(h_outer.l4hdr.udp);
168+
h_outer.l4hdr.udp.check = 0;
169+
h_outer.l4hdr.udp.len = bpf_htons(tot_len);
170+
break;
171+
case IPPROTO_IPV6:
172+
break;
173+
default:
174+
return TC_ACT_OK;
133175
}
134176

135-
136177
/* add room between mac and network header */
137178
if (bpf_skb_adjust_room(skb, olen, BPF_ADJ_ROOM_MAC, flags))
138179
return TC_ACT_SHOT;
@@ -141,13 +182,8 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
141182
h_outer.ip = iph_inner;
142183
h_outer.ip.payload_len = bpf_htons(olen +
143184
bpf_ntohs(h_outer.ip.payload_len));
144-
if (with_gre) {
145-
h_outer.ip.nexthdr = IPPROTO_GRE;
146-
h_outer.protocol = bpf_htons(ETH_P_IPV6);
147-
h_outer.flags = 0;
148-
} else {
149-
h_outer.ip.nexthdr = IPPROTO_IPV6;
150-
}
185+
186+
h_outer.ip.nexthdr = encap_proto;
151187

152188
/* store new outer network header */
153189
if (bpf_skb_store_bytes(skb, ETH_HLEN, &h_outer, olen,
@@ -161,7 +197,7 @@ SEC("encap_ipip")
161197
int __encap_ipip(struct __sk_buff *skb)
162198
{
163199
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
164-
return encap_ipv4(skb, false);
200+
return encap_ipv4(skb, IPPROTO_IPIP);
165201
else
166202
return TC_ACT_OK;
167203
}
@@ -170,7 +206,16 @@ SEC("encap_gre")
170206
int __encap_gre(struct __sk_buff *skb)
171207
{
172208
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
173-
return encap_ipv4(skb, true);
209+
return encap_ipv4(skb, IPPROTO_GRE);
210+
else
211+
return TC_ACT_OK;
212+
}
213+
214+
SEC("encap_udp")
215+
int __encap_udp(struct __sk_buff *skb)
216+
{
217+
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
218+
return encap_ipv4(skb, IPPROTO_UDP);
174219
else
175220
return TC_ACT_OK;
176221
}
@@ -179,7 +224,7 @@ SEC("encap_ip6tnl")
179224
int __encap_ip6tnl(struct __sk_buff *skb)
180225
{
181226
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
182-
return encap_ipv6(skb, false);
227+
return encap_ipv6(skb, IPPROTO_IPV6);
183228
else
184229
return TC_ACT_OK;
185230
}
@@ -188,23 +233,34 @@ SEC("encap_ip6gre")
188233
int __encap_ip6gre(struct __sk_buff *skb)
189234
{
190235
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
191-
return encap_ipv6(skb, true);
236+
return encap_ipv6(skb, IPPROTO_GRE);
237+
else
238+
return TC_ACT_OK;
239+
}
240+
241+
SEC("encap_ip6udp")
242+
int __encap_ip6udp(struct __sk_buff *skb)
243+
{
244+
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
245+
return encap_ipv6(skb, IPPROTO_UDP);
192246
else
193247
return TC_ACT_OK;
194248
}
195249

196250
static int decap_internal(struct __sk_buff *skb, int off, int len, char proto)
197251
{
198-
char buf[sizeof(struct grev6hdr)];
199-
int olen;
252+
char buf[sizeof(struct v6hdr)];
253+
int olen = len;
200254

201255
switch (proto) {
202256
case IPPROTO_IPIP:
203257
case IPPROTO_IPV6:
204-
olen = len;
205258
break;
206259
case IPPROTO_GRE:
207-
olen = len + 4 /* gre hdr */;
260+
olen += sizeof(struct gre_hdr);
261+
break;
262+
case IPPROTO_UDP:
263+
olen += sizeof(struct udphdr);
208264
break;
209265
default:
210266
return TC_ACT_OK;

‎tools/testing/selftests/bpf/test_tc_tunnel.sh‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ readonly ns2_v4=192.168.1.2
1515
readonly ns1_v6=fd::1
1616
readonly ns2_v6=fd::2
1717

18+
# Must match port used by bpf program
19+
readonly udpport=5555
20+
1821
readonly infile="$(mktemp)"
1922
readonly outfile="$(mktemp)"
2023

@@ -38,8 +41,8 @@ setup() {
3841
# clamp route to reserve room for tunnel headers
3942
ip -netns "${ns1}" -4 route flush table main
4043
ip -netns "${ns1}" -6 route flush table main
41-
ip -netns "${ns1}" -4 route add "${ns2_v4}" mtu 1476 dev veth1
42-
ip -netns "${ns1}" -6 route add "${ns2_v6}" mtu 1456 dev veth1
44+
ip -netns "${ns1}" -4 route add "${ns2_v4}" mtu 1472 dev veth1
45+
ip -netns "${ns1}" -6 route add "${ns2_v6}" mtu 1452 dev veth1
4346

4447
sleep 1
4548

@@ -103,6 +106,18 @@ if [[ "$#" -eq "0" ]]; then
103106
echo "ip6 gre gso"
104107
$0 ipv6 ip6gre 2000
105108

109+
echo "ip udp"
110+
$0 ipv4 udp 100
111+
112+
echo "ip6 udp"
113+
$0 ipv6 ip6udp 100
114+
115+
echo "ip udp gso"
116+
$0 ipv4 udp 2000
117+
118+
echo "ip6 udp gso"
119+
$0 ipv6 ip6udp 2000
120+
106121
echo "OK. All tests passed"
107122
exit 0
108123
fi
@@ -117,12 +132,20 @@ case "$1" in
117132
"ipv4")
118133
readonly addr1="${ns1_v4}"
119134
readonly addr2="${ns2_v4}"
120-
readonly netcat_opt=-4
135+
readonly ipproto=4
136+
readonly netcat_opt=-${ipproto}
137+
readonly foumod=fou
138+
readonly foutype=ipip
139+
readonly fouproto=4
121140
;;
122141
"ipv6")
123142
readonly addr1="${ns1_v6}"
124143
readonly addr2="${ns2_v6}"
125-
readonly netcat_opt=-6
144+
readonly ipproto=6
145+
readonly netcat_opt=-${ipproto}
146+
readonly foumod=fou6
147+
readonly foutype=ip6tnl
148+
readonly fouproto="41 -6"
126149
;;
127150
*)
128151
echo "unknown arg: $1"
@@ -155,11 +178,23 @@ echo "test bpf encap without decap (expect failure)"
155178
server_listen
156179
! client_connect
157180

181+
if [[ "$tuntype" =~ "udp" ]]; then
182+
# Set up fou tunnel.
183+
ttype="${foutype}"
184+
targs="encap fou encap-sport auto encap-dport $udpport"
185+
# fou may be a module; allow this to fail.
186+
modprobe "${foumod}" ||true
187+
ip netns exec "${ns2}" ip fou add port 5555 ipproto ${fouproto}
188+
else
189+
ttype=$tuntype
190+
targs=""
191+
fi
192+
158193
# serverside, insert decap module
159194
# server is still running
160195
# client can connect again
161-
ip netns exec "${ns2}" ip link add dev testtun0 type "${tuntype}" \
162-
remote "${addr1}" local "${addr2}"
196+
ip netns exec "${ns2}" ip link add name testtun0 type "${ttype}" \
197+
remote "${addr1}" local "${addr2}" $targs
163198
# Because packets are decapped by the tunnel they arrive on testtun0 from
164199
# the IP stack perspective. Ensure reverse path filtering is disabled
165200
# otherwise we drop the TCP SYN as arriving on testtun0 instead of the

0 commit comments

Comments
 (0)