Skip to contentSkip to conversation

selftests/bpf: test setting tunnel key from lwt xmit

PATCH In review Eyal Birger 2 participants 5 messages Apr 24, 2022 – Apr 28, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: test setting tunnel key from lwt xmit

This commit adds test_egress_md() tests which perform a similar flow as test_egress() only that they use gre devices in collect_md mode and set the tunnel key from lwt bpf xmit.

VRF scenarios are not checked since it is currently not possible to set the underlying device or vrf from bpf_set_tunnel_key().

This introduces minor changes to the existing setup for consistency with the new tests:

- GRE key must exist as bpf_set_tunnel_key() explicitly sets the TUNNEL_KEY flag

- Source address for GRE traffic is set to IPv*_5 instead of IPv*_1 since GRE traffic is sent via veth5 so its address is selected when using bpf_set_tunnel_key()

---
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 51 ++++++++++-
 .../selftests/bpf/test_lwt_ip_encap.sh        | 85 ++++++++++++++++++-
 2 files changed, 128 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +47 -4
index d6cb986e7533..39c6bd5402ae 100644
@@ -10,8 +10,11 @@
 struct grehdr {
 	__be16 flags;
 	__be16 protocol;
+	__be32 key;
 };
 
+#define GRE_KEY	0x2000
+
 SEC("encap_gre")
 int bpf_lwt_encap_gre(struct __sk_buff *skb)
 {
@@ -28,10 +31,10 @@ int bpf_lwt_encap_gre(struct __sk_buff *skb)
 	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.saddr = 0x640510ac;  /* 172.16.5.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.saddr = 0xac100564;  /* 172.16.5.100 */
 	hdr.iph.daddr = 0xac101064;  /* 172.16.16.100 */
 #else
 #error "Fix your compiler's __BYTE_ORDER__?!"
@@ -39,6 +42,7 @@ int bpf_lwt_encap_gre(struct __sk_buff *skb)
 	hdr.iph.tot_len = bpf_htons(skb->len + sizeof(struct encap_hdr));
 
 	hdr.greh.protocol = skb->protocol;
+	hdr.greh.flags = bpf_htons(GRE_KEY);
 
 	err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
 				 sizeof(struct encap_hdr));
@@ -63,9 +67,9 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
 	hdr.ip6hdr.payload_len = bpf_htons(skb->len + sizeof(struct grehdr));
 	hdr.ip6hdr.nexthdr = 47;  /* IPPROTO_GRE */
 	hdr.ip6hdr.hop_limit = 0x40;
-	/* fb01::1 */
+	/* fb05::1 */
 	hdr.ip6hdr.saddr.s6_addr[0] = 0xfb;
-	hdr.ip6hdr.saddr.s6_addr[1] = 1;
+	hdr.ip6hdr.saddr.s6_addr[1] = 5;
 	hdr.ip6hdr.saddr.s6_addr[15] = 1;
 	/* fb10::1 */
 	hdr.ip6hdr.daddr.s6_addr[0] = 0xfb;
@@ -73,6 +77,7 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
 	hdr.ip6hdr.daddr.s6_addr[15] = 1;
 
 	hdr.greh.protocol = skb->protocol;
+	hdr.greh.flags = bpf_htons(GRE_KEY);
 
 	err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
 				 sizeof(struct encap_hdr));
@@ -82,4 +87,42 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
 	return BPF_LWT_REROUTE;
 }
 
+SEC("encap_gre_md")
+int bpf_lwt_encap_gre_md(struct __sk_buff *skb)
+{
+	struct bpf_tunnel_key key;
+	int err;
+
+	__builtin_memset(&key, 0x0, sizeof(key));
+	key.remote_ipv4 = 0xac101064; /* 172.16.16.100 - always in host order */
+	key.tunnel_ttl = 0x40;
+	err = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
+				     BPF_F_ZERO_CSUM_TX | BPF_F_SEQ_NUMBER);
+	if (err)
+		return BPF_DROP;
+
+	return BPF_OK;
+}
+
+SEC("encap_gre6_md")
+int bpf_lwt_encap_gre6_md(struct __sk_buff *skb)
+{
+	struct bpf_tunnel_key key;
+	int err;
+
+	__builtin_memset(&key, 0x0, sizeof(key));
+
+	/* fb10::1 */
+	key.remote_ipv6[0] = bpf_htonl(0xfb100000);
+	key.remote_ipv6[3] = bpf_htonl(0x01);
+	key.tunnel_ttl = 0x40;
+	err = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
+				     BPF_F_ZERO_CSUM_TX | BPF_F_SEQ_NUMBER |
+				     BPF_F_TUNINFO_IPV6);
+	if (err)
+		return BPF_DROP;
+
+	return BPF_OK;
+}
+
 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_lwt_ip_encap.sh +81 -4

126 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c

Quoted 28 lines · Eyal Birger

This commit adds test_egress_md() tests which perform a similar flow as test_egress() only that they use gre devices in collect_md mode and set the tunnel key from lwt bpf xmit.

VRF scenarios are not checked since it is currently not possible to set the underlying device or vrf from bpf_set_tunnel_key().

This introduces minor changes to the existing setup for consistency with the new tests:

- GRE key must exist as bpf_set_tunnel_key() explicitly sets the TUNNEL_KEY flag

- Source address for GRE traffic is set to IPv*_5 instead of IPv*_1 since GRE traffic is sent via veth5 so its address is selected when using bpf_set_tunnel_key()

---
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 51 ++++++++++-
 .../selftests/bpf/test_lwt_ip_encap.sh        | 85 ++++++++++++++++++-
 2 files changed, 128 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +0 -0
index d6cb986e7533..39c6bd5402ae 100644

Thinking about this some more, I'm not sure if these tests fit better here or in test_tunnel.sh.

If the latter is preferred, please drop this patch and I'll submit one for test_tunnel.sh.

Eyal.

#2 lore raw
Quoted 34 lines · Eyal Birger

tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c

Quoted 28 lines · Eyal Birger

This commit adds test_egress_md() tests which perform a similar flow as test_egress() only that they use gre devices in collect_md mode and set the tunnel key from lwt bpf xmit.

VRF scenarios are not checked since it is currently not possible to set the underlying device or vrf from bpf_set_tunnel_key().

This introduces minor changes to the existing setup for consistency with the new tests:

- GRE key must exist as bpf_set_tunnel_key() explicitly sets the TUNNEL_KEY flag

- Source address for GRE traffic is set to IPv*_5 instead of IPv*_1 since GRE traffic is sent via veth5 so its address is selected when using bpf_set_tunnel_key()

---
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 51 ++++++++++-
 .../selftests/bpf/test_lwt_ip_encap.sh        | 85 ++++++++++++++++++-
 2 files changed, 128 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +0 -0
index d6cb986e7533..39c6bd5402ae 100644

Thinking about this some more, I'm not sure if these tests fit better here or in test_tunnel.sh.

Quoted 2 lines

If the latter is preferred, please drop this patch and I'll submit one for test_tunnel.sh.

general preference is to put test into test_progs as those are regularly and extensively exercised, while test_tunnel.sh is not

Quoted 2 lines

Eyal.

#3 lore raw
Quoted 36 lines · Andrii Nakryiko
Quoted 34 lines · Eyal Birger

tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c

Quoted 28 lines · Eyal Birger

This commit adds test_egress_md() tests which perform a similar flow as test_egress() only that they use gre devices in collect_md mode and set the tunnel key from lwt bpf xmit.

VRF scenarios are not checked since it is currently not possible to set the underlying device or vrf from bpf_set_tunnel_key().

This introduces minor changes to the existing setup for consistency with the new tests:

- GRE key must exist as bpf_set_tunnel_key() explicitly sets the TUNNEL_KEY flag

- Source address for GRE traffic is set to IPv*_5 instead of IPv*_1 since GRE traffic is sent via veth5 so its address is selected when using bpf_set_tunnel_key()

---
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 51 ++++++++++-
 .../selftests/bpf/test_lwt_ip_encap.sh        | 85 ++++++++++++++++++-
 2 files changed, 128 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +0 -0
index d6cb986e7533..39c6bd5402ae 100644

Thinking about this some more, I'm not sure if these tests fit better here or in test_tunnel.sh.

Quoted 5 lines
Quoted 2 lines

If the latter is preferred, please drop this patch and I'll submit one for test_tunnel.sh.

general preference is to put test into test_progs as those are regularly and extensively exercised, while test_tunnel.sh is not

Thanks. Will move the logic there then.

Quoted 3 lines
Quoted 3 lines

Eyal.

#4 lore raw
In reply to Eyal Birger
Quoted 24 lines · Eyal Birger

This commit adds test_egress_md() tests which perform a similar flow as test_egress() only that they use gre devices in collect_md mode and set the tunnel key from lwt bpf xmit.

VRF scenarios are not checked since it is currently not possible to set the underlying device or vrf from bpf_set_tunnel_key().

This introduces minor changes to the existing setup for consistency with the new tests:

- GRE key must exist as bpf_set_tunnel_key() explicitly sets the TUNNEL_KEY flag

- Source address for GRE traffic is set to IPv*_5 instead of IPv*_1 since GRE traffic is sent via veth5 so its address is selected when using bpf_set_tunnel_key()

---
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 51 ++++++++++-
 .../selftests/bpf/test_lwt_ip_encap.sh        | 85 ++++++++++++++++++-
 2 files changed, 128 insertions(+), 8 deletions(-)

[...]

tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c int bpf_lwt_encap_gre6(struct __sk_buff *skb) · line 90

Quoted 13 lines
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +2 -0
@@ -73,6 +77,7 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
        hdr.ip6hdr.daddr.s6_addr[15] = 1;
 
        hdr.greh.protocol = skb->protocol;
+       hdr.greh.flags = bpf_htons(GRE_KEY);
 
        err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
                                 sizeof(struct encap_hdr));
@@ -82,4 +87,42 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
        return BPF_LWT_REROUTE;
 }
 
+SEC("encap_gre_md")

This is not a section name that's supported by libbpf in it's strict 1.0 mode. Please update all SEC() definition to be compliant. Is this SEC("tc") case?

Quoted 16 lines
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +16 -0
+int bpf_lwt_encap_gre_md(struct __sk_buff *skb)
+{
+       struct bpf_tunnel_key key;
+       int err;
+
+       __builtin_memset(&key, 0x0, sizeof(key));
+       key.remote_ipv4 = 0xac101064; /* 172.16.16.100 - always in host order */
+       key.tunnel_ttl = 0x40;
+       err = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
+                                    BPF_F_ZERO_CSUM_TX | BPF_F_SEQ_NUMBER);
+       if (err)
+               return BPF_DROP;
+
+       return BPF_OK;
+}
+

[...]