Skip to content

Commit fb079b1

Browse files
committed
Create parse_ethhdr_vlan() in parsing_helpers.h
This is based on same function from packet-solutions/xdp_vlan02_kern.c that also extracts VLANs. It needs to be removed from packet-solutions/xdp_vlan02_kern.c, but for now its just ifdef defined out-of-code. Need to run some tests and look at BPF byte-code to make sure it does the right thing. The original parse_ethhdr() is implemented by calling parse_ethhdr_vlan() with NULL argument for stucture to collect IDs into. The compiler should remove the parts that are not needed. Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
1 parent ffba649 commit fb079b1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

common/parsing_helpers.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ struct icmphdr_common {
5959
#define VLAN_MAX_DEPTH 2
6060
#endif
6161

62+
#define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
63+
struct vlans {
64+
__u16 id[VLAN_MAX_DEPTH];
65+
};
66+
6267
static __always_inline int proto_is_vlan(__u16 h_proto)
6368
{
6469
return !!(h_proto == bpf_htons(ETH_P_8021Q) ||
@@ -70,8 +75,10 @@ static __always_inline int proto_is_vlan(__u16 h_proto)
7075
* Ethernet header. Thus, caller can look at eth->h_proto to see if this was a
7176
* VLAN tagged packet.
7277
*/
73-
static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
74-
struct ethhdr **ethhdr)
78+
static __always_inline int parse_ethhdr_vlan(struct hdr_cursor *nh,
79+
void *data_end,
80+
struct ethhdr **ethhdr,
81+
struct vlans *vlans)
7582
{
7683
struct ethhdr *eth = nh->pos;
7784
int hdrsize = sizeof(*eth);
@@ -102,13 +109,25 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
102109
break;
103110

104111
h_proto = vlh->h_vlan_encapsulated_proto;
112+
if (vlans) /* collect VLAN ids */
113+
vlans->id[i] =
114+
(bpf_ntohs(vlh->h_vlan_TCI) & VLAN_VID_MASK);
115+
105116
vlh++;
106117
}
107118

108119
nh->pos = vlh;
109120
return h_proto; /* network-byte-order */
110121
}
111122

123+
static __always_inline int parse_ethhdr(struct hdr_cursor *nh,
124+
void *data_end,
125+
struct ethhdr **ethhdr)
126+
{
127+
/* Expect compiler to remove collect VLAN ids */
128+
return parse_ethhdr_vlan(nh, data_end, ethhdr, NULL);
129+
}
130+
112131
static __always_inline int parse_ip6hdr(struct hdr_cursor *nh,
113132
void *data_end,
114133
struct ipv6hdr **ip6hdr)

packet-solutions/xdp_vlan02_kern.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#define VLAN_MAX_DEPTH 10
1111
#include "../common/parsing_helpers.h"
1212

13+
#if 0
1314
#define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
1415
struct vlans {
1516
__u16 id[VLAN_MAX_DEPTH];
1617
};
18+
#endif
1719

20+
#if 0 /* moved to parsing_helpers.h */
1821
/* Based on parse_ethhdr() */
1922
static __always_inline int __parse_ethhdr_vlan(struct hdr_cursor *nh,
2023
void *data_end,
@@ -60,6 +63,7 @@ static __always_inline int __parse_ethhdr_vlan(struct hdr_cursor *nh,
6063
nh->pos = vlh;
6164
return h_proto; /* network-byte-order */
6265
}
66+
#endif
6367

6468
SEC("xdp_vlan02")
6569
int xdp_vlan_02(struct xdp_md *ctx)
@@ -75,7 +79,7 @@ int xdp_vlan_02(struct xdp_md *ctx)
7579
struct vlans vlans;
7680

7781
struct ethhdr *eth;
78-
eth_type = __parse_ethhdr_vlan(&nh, data_end, &eth, &vlans);
82+
eth_type = parse_ethhdr_vlan(&nh, data_end, &eth, &vlans);
7983

8084
if (eth_type < 0)
8185
return XDP_ABORTED;

0 commit comments

Comments
 (0)