-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <posk@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Peter Oskolkov
authored and
Alexei Starovoitov
committed
Feb 14, 2019
1 parent
755db47
commit 0fde56e
Showing
3 changed files
with
398 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include <stddef.h> | ||
#include <string.h> | ||
#include <linux/bpf.h> | ||
#include <linux/ip.h> | ||
#include <linux/ipv6.h> | ||
#include "bpf_helpers.h" | ||
#include "bpf_endian.h" | ||
|
||
struct grehdr { | ||
__be16 flags; | ||
__be16 protocol; | ||
}; | ||
|
||
SEC("encap_gre") | ||
int bpf_lwt_encap_gre(struct __sk_buff *skb) | ||
{ | ||
struct encap_hdr { | ||
struct iphdr iph; | ||
struct grehdr greh; | ||
} hdr; | ||
int err; | ||
|
||
memset(&hdr, 0, sizeof(struct encap_hdr)); | ||
|
||
hdr.iph.ihl = 5; | ||
hdr.iph.version = 4; | ||
hdr.iph.ttl = 0x40; | ||
hdr.iph.protocol = 47; /* IPPROTO_GRE */ | ||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | ||
hdr.iph.saddr = 0x640110ac; /* 172.16.1.100 */ | ||
hdr.iph.daddr = 0x641010ac; /* 172.16.16.100 */ | ||
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||
hdr.iph.saddr = 0xac100164; /* 172.16.1.100 */ | ||
hdr.iph.daddr = 0xac101064; /* 172.16.16.100 */ | ||
#else | ||
#error "Fix your compiler's __BYTE_ORDER__?!" | ||
#endif | ||
hdr.iph.tot_len = bpf_htons(skb->len + sizeof(struct encap_hdr)); | ||
|
||
hdr.greh.protocol = skb->protocol; | ||
|
||
err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr, | ||
sizeof(struct encap_hdr)); | ||
if (err) | ||
return BPF_DROP; | ||
|
||
return BPF_LWT_REROUTE; | ||
} | ||
|
||
SEC("encap_gre6") | ||
int bpf_lwt_encap_gre6(struct __sk_buff *skb) | ||
{ | ||
struct encap_hdr { | ||
struct ipv6hdr ip6hdr; | ||
struct grehdr greh; | ||
} hdr; | ||
int err; | ||
|
||
memset(&hdr, 0, sizeof(struct encap_hdr)); | ||
|
||
hdr.ip6hdr.version = 6; | ||
hdr.ip6hdr.payload_len = bpf_htons(skb->len + sizeof(struct grehdr)); | ||
hdr.ip6hdr.nexthdr = 47; /* IPPROTO_GRE */ | ||
hdr.ip6hdr.hop_limit = 0x40; | ||
/* fb01::1 */ | ||
hdr.ip6hdr.saddr.s6_addr[0] = 0xfb; | ||
hdr.ip6hdr.saddr.s6_addr[1] = 1; | ||
hdr.ip6hdr.saddr.s6_addr[15] = 1; | ||
/* fb10::1 */ | ||
hdr.ip6hdr.daddr.s6_addr[0] = 0xfb; | ||
hdr.ip6hdr.daddr.s6_addr[1] = 0x10; | ||
hdr.ip6hdr.daddr.s6_addr[15] = 1; | ||
|
||
hdr.greh.protocol = skb->protocol; | ||
|
||
err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr, | ||
sizeof(struct encap_hdr)); | ||
if (err) | ||
return BPF_DROP; | ||
|
||
return BPF_LWT_REROUTE; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
Oops, something went wrong.