selftests_bpf: extend test_tc_tunnel for UDP encap
commit 868d523535
("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>
This commit is contained in:

کامیت شده توسط
Daniel Borkmann

والد
c695865c5c
کامیت
166b5a7f2c
@@ -12,6 +12,7 @@
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/udp.h>
|
||||
#include <linux/pkt_cls.h>
|
||||
#include <linux/types.h>
|
||||
|
||||
@@ -20,16 +21,27 @@
|
||||
|
||||
static const int cfg_port = 8000;
|
||||
|
||||
struct grev4hdr {
|
||||
struct iphdr ip;
|
||||
static const int cfg_udp_src = 20000;
|
||||
static const int cfg_udp_dst = 5555;
|
||||
|
||||
struct gre_hdr {
|
||||
__be16 flags;
|
||||
__be16 protocol;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct grev6hdr {
|
||||
union l4hdr {
|
||||
struct udphdr udp;
|
||||
struct gre_hdr gre;
|
||||
};
|
||||
|
||||
struct v4hdr {
|
||||
struct iphdr ip;
|
||||
union l4hdr l4hdr;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct v6hdr {
|
||||
struct ipv6hdr ip;
|
||||
__be16 flags;
|
||||
__be16 protocol;
|
||||
union l4hdr l4hdr;
|
||||
} __attribute__((packed));
|
||||
|
||||
static __always_inline void set_ipv4_csum(struct iphdr *iph)
|
||||
@@ -47,10 +59,10 @@ static __always_inline void set_ipv4_csum(struct iphdr *iph)
|
||||
iph->check = ~((csum & 0xffff) + (csum >> 16));
|
||||
}
|
||||
|
||||
static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
|
||||
static __always_inline int encap_ipv4(struct __sk_buff *skb, __u8 encap_proto)
|
||||
{
|
||||
struct grev4hdr h_outer;
|
||||
struct iphdr iph_inner;
|
||||
struct v4hdr h_outer;
|
||||
struct tcphdr tcph;
|
||||
__u64 flags;
|
||||
int olen;
|
||||
@@ -70,12 +82,29 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
|
||||
if (tcph.dest != __bpf_constant_htons(cfg_port))
|
||||
return TC_ACT_OK;
|
||||
|
||||
olen = sizeof(h_outer.ip);
|
||||
|
||||
flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV4;
|
||||
if (with_gre) {
|
||||
switch (encap_proto) {
|
||||
case IPPROTO_GRE:
|
||||
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE;
|
||||
olen = sizeof(h_outer);
|
||||
} else {
|
||||
olen = sizeof(h_outer.ip);
|
||||
olen += sizeof(h_outer.l4hdr.gre);
|
||||
h_outer.l4hdr.gre.protocol = bpf_htons(ETH_P_IP);
|
||||
h_outer.l4hdr.gre.flags = 0;
|
||||
break;
|
||||
case IPPROTO_UDP:
|
||||
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP;
|
||||
olen += sizeof(h_outer.l4hdr.udp);
|
||||
h_outer.l4hdr.udp.source = __bpf_constant_htons(cfg_udp_src);
|
||||
h_outer.l4hdr.udp.dest = __bpf_constant_htons(cfg_udp_dst);
|
||||
h_outer.l4hdr.udp.check = 0;
|
||||
h_outer.l4hdr.udp.len = bpf_htons(bpf_ntohs(iph_inner.tot_len) +
|
||||
sizeof(h_outer.l4hdr.udp));
|
||||
break;
|
||||
case IPPROTO_IPIP:
|
||||
break;
|
||||
default:
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
/* add room between mac and network header */
|
||||
@@ -85,16 +114,10 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
|
||||
/* prepare new outer network header */
|
||||
h_outer.ip = iph_inner;
|
||||
h_outer.ip.tot_len = bpf_htons(olen +
|
||||
bpf_htons(h_outer.ip.tot_len));
|
||||
if (with_gre) {
|
||||
h_outer.ip.protocol = IPPROTO_GRE;
|
||||
h_outer.protocol = bpf_htons(ETH_P_IP);
|
||||
h_outer.flags = 0;
|
||||
} else {
|
||||
h_outer.ip.protocol = IPPROTO_IPIP;
|
||||
}
|
||||
bpf_ntohs(h_outer.ip.tot_len));
|
||||
h_outer.ip.protocol = encap_proto;
|
||||
|
||||
set_ipv4_csum((void *)&h_outer.ip);
|
||||
set_ipv4_csum(&h_outer.ip);
|
||||
|
||||
/* store new outer network header */
|
||||
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)
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
|
||||
static __always_inline int encap_ipv6(struct __sk_buff *skb, __u8 encap_proto)
|
||||
{
|
||||
struct ipv6hdr iph_inner;
|
||||
struct grev6hdr h_outer;
|
||||
struct v6hdr h_outer;
|
||||
struct tcphdr tcph;
|
||||
__u16 tot_len;
|
||||
__u64 flags;
|
||||
int olen;
|
||||
|
||||
@@ -124,14 +148,31 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
|
||||
if (tcph.dest != __bpf_constant_htons(cfg_port))
|
||||
return TC_ACT_OK;
|
||||
|
||||
flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV6;
|
||||
if (with_gre) {
|
||||
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE;
|
||||
olen = sizeof(h_outer);
|
||||
} else {
|
||||
olen = sizeof(h_outer.ip);
|
||||
}
|
||||
olen = sizeof(h_outer.ip);
|
||||
|
||||
flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV6;
|
||||
switch (encap_proto) {
|
||||
case IPPROTO_GRE:
|
||||
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE;
|
||||
olen += sizeof(h_outer.l4hdr.gre);
|
||||
h_outer.l4hdr.gre.protocol = bpf_htons(ETH_P_IPV6);
|
||||
h_outer.l4hdr.gre.flags = 0;
|
||||
break;
|
||||
case IPPROTO_UDP:
|
||||
flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP;
|
||||
olen += sizeof(h_outer.l4hdr.udp);
|
||||
h_outer.l4hdr.udp.source = __bpf_constant_htons(cfg_udp_src);
|
||||
h_outer.l4hdr.udp.dest = __bpf_constant_htons(cfg_udp_dst);
|
||||
tot_len = bpf_ntohs(iph_inner.payload_len) + sizeof(iph_inner) +
|
||||
sizeof(h_outer.l4hdr.udp);
|
||||
h_outer.l4hdr.udp.check = 0;
|
||||
h_outer.l4hdr.udp.len = bpf_htons(tot_len);
|
||||
break;
|
||||
case IPPROTO_IPV6:
|
||||
break;
|
||||
default:
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
/* add room between mac and network header */
|
||||
if (bpf_skb_adjust_room(skb, olen, BPF_ADJ_ROOM_MAC, flags))
|
||||
@@ -141,13 +182,8 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
|
||||
h_outer.ip = iph_inner;
|
||||
h_outer.ip.payload_len = bpf_htons(olen +
|
||||
bpf_ntohs(h_outer.ip.payload_len));
|
||||
if (with_gre) {
|
||||
h_outer.ip.nexthdr = IPPROTO_GRE;
|
||||
h_outer.protocol = bpf_htons(ETH_P_IPV6);
|
||||
h_outer.flags = 0;
|
||||
} else {
|
||||
h_outer.ip.nexthdr = IPPROTO_IPV6;
|
||||
}
|
||||
|
||||
h_outer.ip.nexthdr = encap_proto;
|
||||
|
||||
/* store new outer network header */
|
||||
if (bpf_skb_store_bytes(skb, ETH_HLEN, &h_outer, olen,
|
||||
@@ -161,7 +197,7 @@ SEC("encap_ipip")
|
||||
int __encap_ipip(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
|
||||
return encap_ipv4(skb, false);
|
||||
return encap_ipv4(skb, IPPROTO_IPIP);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
@@ -170,7 +206,16 @@ SEC("encap_gre")
|
||||
int __encap_gre(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
|
||||
return encap_ipv4(skb, true);
|
||||
return encap_ipv4(skb, IPPROTO_GRE);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
SEC("encap_udp")
|
||||
int __encap_udp(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IP))
|
||||
return encap_ipv4(skb, IPPROTO_UDP);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
@@ -179,7 +224,7 @@ SEC("encap_ip6tnl")
|
||||
int __encap_ip6tnl(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
|
||||
return encap_ipv6(skb, false);
|
||||
return encap_ipv6(skb, IPPROTO_IPV6);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
@@ -188,23 +233,34 @@ SEC("encap_ip6gre")
|
||||
int __encap_ip6gre(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
|
||||
return encap_ipv6(skb, true);
|
||||
return encap_ipv6(skb, IPPROTO_GRE);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
SEC("encap_ip6udp")
|
||||
int __encap_ip6udp(struct __sk_buff *skb)
|
||||
{
|
||||
if (skb->protocol == __bpf_constant_htons(ETH_P_IPV6))
|
||||
return encap_ipv6(skb, IPPROTO_UDP);
|
||||
else
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
static int decap_internal(struct __sk_buff *skb, int off, int len, char proto)
|
||||
{
|
||||
char buf[sizeof(struct grev6hdr)];
|
||||
int olen;
|
||||
char buf[sizeof(struct v6hdr)];
|
||||
int olen = len;
|
||||
|
||||
switch (proto) {
|
||||
case IPPROTO_IPIP:
|
||||
case IPPROTO_IPV6:
|
||||
olen = len;
|
||||
break;
|
||||
case IPPROTO_GRE:
|
||||
olen = len + 4 /* gre hdr */;
|
||||
olen += sizeof(struct gre_hdr);
|
||||
break;
|
||||
case IPPROTO_UDP:
|
||||
olen += sizeof(struct udphdr);
|
||||
break;
|
||||
default:
|
||||
return TC_ACT_OK;
|
||||
|
مرجع در شماره جدید
Block a user