Skip to content

Commit 0fde56e

Browse files
Peter OskolkovAlexei Starovoitov
Peter Oskolkov
authored and
Alexei Starovoitov
committed
selftests: bpf: add test_lwt_ip_encap selftest
This patch adds a bpf self-test to cover BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap. Covered: - encapping in LWT_IN and LWT_XMIT - IPv4 and IPv6 A follow-up patch will add GSO and VRF-enabled tests. Signed-off-by: Peter Oskolkov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 755db47 commit 0fde56e

File tree

3 files changed

+398
-1
lines changed

3 files changed

+398
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ TEST_PROGS := test_kmod.sh \
5050
test_lirc_mode2.sh \
5151
test_skb_cgroup_id.sh \
5252
test_flow_dissector.sh \
53-
test_xdp_vlan.sh
53+
test_xdp_vlan.sh \
54+
test_lwt_ip_encap.sh
5455

5556
TEST_PROGS_EXTENDED := with_addr.sh \
5657
with_tunnels.sh \
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stddef.h>
3+
#include <string.h>
4+
#include <linux/bpf.h>
5+
#include <linux/ip.h>
6+
#include <linux/ipv6.h>
7+
#include "bpf_helpers.h"
8+
#include "bpf_endian.h"
9+
10+
struct grehdr {
11+
__be16 flags;
12+
__be16 protocol;
13+
};
14+
15+
SEC("encap_gre")
16+
int bpf_lwt_encap_gre(struct __sk_buff *skb)
17+
{
18+
struct encap_hdr {
19+
struct iphdr iph;
20+
struct grehdr greh;
21+
} hdr;
22+
int err;
23+
24+
memset(&hdr, 0, sizeof(struct encap_hdr));
25+
26+
hdr.iph.ihl = 5;
27+
hdr.iph.version = 4;
28+
hdr.iph.ttl = 0x40;
29+
hdr.iph.protocol = 47; /* IPPROTO_GRE */
30+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
31+
hdr.iph.saddr = 0x640110ac; /* 172.16.1.100 */
32+
hdr.iph.daddr = 0x641010ac; /* 172.16.16.100 */
33+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
34+
hdr.iph.saddr = 0xac100164; /* 172.16.1.100 */
35+
hdr.iph.daddr = 0xac101064; /* 172.16.16.100 */
36+
#else
37+
#error "Fix your compiler's __BYTE_ORDER__?!"
38+
#endif
39+
hdr.iph.tot_len = bpf_htons(skb->len + sizeof(struct encap_hdr));
40+
41+
hdr.greh.protocol = skb->protocol;
42+
43+
err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
44+
sizeof(struct encap_hdr));
45+
if (err)
46+
return BPF_DROP;
47+
48+
return BPF_LWT_REROUTE;
49+
}
50+
51+
SEC("encap_gre6")
52+
int bpf_lwt_encap_gre6(struct __sk_buff *skb)
53+
{
54+
struct encap_hdr {
55+
struct ipv6hdr ip6hdr;
56+
struct grehdr greh;
57+
} hdr;
58+
int err;
59+
60+
memset(&hdr, 0, sizeof(struct encap_hdr));
61+
62+
hdr.ip6hdr.version = 6;
63+
hdr.ip6hdr.payload_len = bpf_htons(skb->len + sizeof(struct grehdr));
64+
hdr.ip6hdr.nexthdr = 47; /* IPPROTO_GRE */
65+
hdr.ip6hdr.hop_limit = 0x40;
66+
/* fb01::1 */
67+
hdr.ip6hdr.saddr.s6_addr[0] = 0xfb;
68+
hdr.ip6hdr.saddr.s6_addr[1] = 1;
69+
hdr.ip6hdr.saddr.s6_addr[15] = 1;
70+
/* fb10::1 */
71+
hdr.ip6hdr.daddr.s6_addr[0] = 0xfb;
72+
hdr.ip6hdr.daddr.s6_addr[1] = 0x10;
73+
hdr.ip6hdr.daddr.s6_addr[15] = 1;
74+
75+
hdr.greh.protocol = skb->protocol;
76+
77+
err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
78+
sizeof(struct encap_hdr));
79+
if (err)
80+
return BPF_DROP;
81+
82+
return BPF_LWT_REROUTE;
83+
}
84+
85+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)