Skip to content

Commit d2c0d14

Browse files
committed
Merge branch 'tutorial30-improve-parsing'
Started working on issue #59 again, but notice that code have several style issues, mostly with spaces instead of tabs. Thus, fix up those first, else it will be harder to review the planned return of network-byte-order from parse_ethhdr().
2 parents 5bd1397 + dd0aefa commit d2c0d14

4 files changed

Lines changed: 81 additions & 78 deletions

File tree

common/parsing_helpers.h

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct hdr_cursor {
3535
};
3636

3737
/*
38-
* struct vlan_hdr - vlan header
39-
* @h_vlan_TCI: priority and VLAN ID
38+
* struct vlan_hdr - vlan header
39+
* @h_vlan_TCI: priority and VLAN ID
4040
* @h_vlan_encapsulated_proto: packet type ID or len
4141
*/
4242
struct vlan_hdr {
@@ -51,7 +51,7 @@ struct vlan_hdr {
5151
struct icmphdr_common {
5252
__u8 type;
5353
__u8 code;
54-
__sum16 cksum;
54+
__sum16 cksum;
5555
};
5656

5757
/* Allow users of header file to redefine VLAN max depth */
@@ -61,8 +61,8 @@ struct icmphdr_common {
6161

6262
static __always_inline int proto_is_vlan(__u16 h_proto)
6363
{
64-
return !!(h_proto == bpf_htons(ETH_P_8021Q) ||
65-
h_proto == bpf_htons(ETH_P_8021AD));
64+
return !!(h_proto == bpf_htons(ETH_P_8021Q) ||
65+
h_proto == bpf_htons(ETH_P_8021AD));
6666
}
6767

6868
/* Notice, parse_ethhdr() will skip VLAN tags, by advancing nh->pos and returns
@@ -75,9 +75,9 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
7575
{
7676
struct ethhdr *eth = nh->pos;
7777
int hdrsize = sizeof(*eth);
78-
struct vlan_hdr *vlh;
79-
__u16 h_proto;
80-
int i;
78+
struct vlan_hdr *vlh;
79+
__u16 h_proto;
80+
int i;
8181

8282
/* Byte-count bounds check; check if current pointer + size of header
8383
* is after data_end.
@@ -87,25 +87,25 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
8787

8888
nh->pos += hdrsize;
8989
*ethhdr = eth;
90-
vlh = nh->pos;
91-
h_proto = eth->h_proto;
90+
vlh = nh->pos;
91+
h_proto = eth->h_proto;
9292

93-
/* Use loop unrolling to avoid the verifier restriction on loops;
94-
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
95-
*/
96-
#pragma unroll
97-
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
98-
if (!proto_is_vlan(h_proto))
99-
break;
93+
/* Use loop unrolling to avoid the verifier restriction on loops;
94+
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
95+
*/
96+
#pragma unroll
97+
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
98+
if (!proto_is_vlan(h_proto))
99+
break;
100100

101-
if (vlh + 1 > data_end)
102-
break;
101+
if (vlh + 1 > data_end)
102+
break;
103103

104-
h_proto = vlh->h_vlan_encapsulated_proto;
105-
vlh++;
106-
}
104+
h_proto = vlh->h_vlan_encapsulated_proto;
105+
vlh++;
106+
}
107107

108-
nh->pos = vlh;
108+
nh->pos = vlh;
109109
return bpf_ntohs(h_proto);
110110
}
111111

@@ -129,20 +129,20 @@ static __always_inline int parse_ip6hdr(struct hdr_cursor *nh,
129129
}
130130

131131
static __always_inline int parse_iphdr(struct hdr_cursor *nh,
132-
void *data_end,
133-
struct iphdr **iphdr)
132+
void *data_end,
133+
struct iphdr **iphdr)
134134
{
135135
struct iphdr *iph = nh->pos;
136136
int hdrsize;
137137

138138
if (iph + 1 > data_end)
139139
return -1;
140140

141-
hdrsize = iph->ihl * 4;
141+
hdrsize = iph->ihl * 4;
142142

143-
/* Variable-length IPv4 header, need to use byte-based arithmetic */
144-
if (nh->pos + hdrsize > data_end)
145-
return -1;
143+
/* Variable-length IPv4 header, need to use byte-based arithmetic */
144+
if (nh->pos + hdrsize > data_end)
145+
return -1;
146146

147147
nh->pos += hdrsize;
148148
*iphdr = iph;
@@ -166,8 +166,8 @@ static __always_inline int parse_icmp6hdr(struct hdr_cursor *nh,
166166
}
167167

168168
static __always_inline int parse_icmphdr(struct hdr_cursor *nh,
169-
void *data_end,
170-
struct icmphdr **icmphdr)
169+
void *data_end,
170+
struct icmphdr **icmphdr)
171171
{
172172
struct icmphdr *icmph = nh->pos;
173173

common/rewrite_helpers.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ static __always_inline int vlan_tag_push(struct xdp_md *ctx,
9090
if (eth + 1 > data_end)
9191
return -1;
9292

93-
/* Copy back the Ethernet header in the right place, populate the VLAN
94-
* tag with the ID and proto, and set the outer Ethernet header to VLAN
95-
* type. */
93+
/* Copy back Ethernet header in the right place, populate VLAN tag with
94+
* ID and proto, and set outer Ethernet header to VLAN type.
95+
*/
9696
__builtin_memcpy(eth, &eth_cpy, sizeof(*eth));
9797

98-
vlh = (void *)(eth +1);
98+
vlh = (void *)(eth + 1);
9999

100100
if (vlh + 1 > data_end)
101101
return -1;
@@ -113,6 +113,7 @@ static __always_inline int vlan_tag_push(struct xdp_md *ctx,
113113
static __always_inline void swap_src_dst_mac(struct ethhdr *eth)
114114
{
115115
__u8 h_tmp[ETH_ALEN];
116+
116117
__builtin_memcpy(h_tmp, eth->h_source, ETH_ALEN);
117118
__builtin_memcpy(eth->h_source, eth->h_dest, ETH_ALEN);
118119
__builtin_memcpy(eth->h_dest, h_tmp, ETH_ALEN);
@@ -124,6 +125,7 @@ static __always_inline void swap_src_dst_mac(struct ethhdr *eth)
124125
static __always_inline void swap_src_dst_ipv6(struct ipv6hdr *ipv6)
125126
{
126127
struct in6_addr tmp = ipv6->saddr;
128+
127129
ipv6->saddr = ipv6->daddr;
128130
ipv6->daddr = tmp;
129131
}
@@ -134,6 +136,7 @@ static __always_inline void swap_src_dst_ipv6(struct ipv6hdr *ipv6)
134136
static __always_inline void swap_src_dst_ipv4(struct iphdr *iphdr)
135137
{
136138
__be32 tmp = iphdr->saddr;
139+
137140
iphdr->saddr = iphdr->daddr;
138141
iphdr->daddr = tmp;
139142
}

packet-solutions/xdp_vlan01_kern.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
1+
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22
#include <linux/bpf.h>
33
#include <linux/if_ether.h>
44

@@ -20,13 +20,13 @@ struct hdr_cursor {
2020

2121
static __always_inline int proto_is_vlan(__u16 h_proto)
2222
{
23-
return !!(h_proto == bpf_htons(ETH_P_8021Q) ||
24-
h_proto == bpf_htons(ETH_P_8021AD));
23+
return !!(h_proto == bpf_htons(ETH_P_8021Q) ||
24+
h_proto == bpf_htons(ETH_P_8021AD));
2525
}
2626

2727
/*
28-
* struct vlan_hdr - vlan header
29-
* @h_vlan_TCI: priority and VLAN ID
28+
* struct vlan_hdr - vlan header
29+
* @h_vlan_TCI: priority and VLAN ID
3030
* @h_vlan_encapsulated_proto: packet type ID or len
3131
*/
3232
struct vlan_hdr {
@@ -44,9 +44,9 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
4444
{
4545
struct ethhdr *eth = nh->pos;
4646
int hdrsize = sizeof(*eth);
47-
struct vlan_hdr *vlh;
48-
__u16 h_proto;
49-
int i;
47+
struct vlan_hdr *vlh;
48+
__u16 h_proto;
49+
int i;
5050

5151
/* Byte-count bounds check; check if current pointer + size of header
5252
* is after data_end.
@@ -56,25 +56,25 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
5656

5757
nh->pos += hdrsize;
5858
*ethhdr = eth;
59-
vlh = nh->pos;
60-
h_proto = eth->h_proto;
59+
vlh = nh->pos;
60+
h_proto = eth->h_proto;
6161

62-
/* Use loop unrolling to avoid the verifier restriction on loops;
63-
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
64-
*/
65-
#pragma unroll
66-
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
67-
if (!proto_is_vlan(h_proto))
68-
break;
62+
/* Use loop unrolling to avoid the verifier restriction on loops;
63+
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
64+
*/
65+
#pragma unroll
66+
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
67+
if (!proto_is_vlan(h_proto))
68+
break;
6969

70-
if (vlh + 1 > data_end)
71-
break;
70+
if (vlh + 1 > data_end)
71+
break;
7272

73-
h_proto = vlh->h_vlan_encapsulated_proto;
74-
vlh++;
75-
}
73+
h_proto = vlh->h_vlan_encapsulated_proto;
74+
vlh++;
75+
}
7676

77-
nh->pos = vlh;
77+
nh->pos = vlh;
7878
return bpf_ntohs(h_proto);
7979
}
8080

packet-solutions/xdp_vlan02_kern.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
1+
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22
#include <linux/bpf.h>
33
#include <linux/if_ether.h>
44
#include <linux/in.h>
@@ -19,13 +19,13 @@ struct vlans {
1919
static __always_inline int __parse_ethhdr_vlan(struct hdr_cursor *nh,
2020
void *data_end,
2121
struct ethhdr **ethhdr,
22-
struct vlans* vlans)
22+
struct vlans *vlans)
2323
{
2424
struct ethhdr *eth = nh->pos;
2525
int hdrsize = sizeof(*eth);
26-
struct vlan_hdr *vlh;
27-
__u16 h_proto;
28-
int i;
26+
struct vlan_hdr *vlh;
27+
__u16 h_proto;
28+
int i;
2929

3030
/* Byte-count bounds check; check if current pointer + size of header
3131
* is after data_end.
@@ -35,28 +35,28 @@ static __always_inline int __parse_ethhdr_vlan(struct hdr_cursor *nh,
3535

3636
nh->pos += hdrsize;
3737
*ethhdr = eth;
38-
vlh = nh->pos;
39-
h_proto = eth->h_proto;
38+
vlh = nh->pos;
39+
h_proto = eth->h_proto;
4040

41-
/* Use loop unrolling to avoid the verifier restriction on loops;
42-
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
43-
*/
44-
#pragma unroll
45-
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
46-
if (!proto_is_vlan(h_proto))
47-
break;
41+
/* Use loop unrolling to avoid the verifier restriction on loops;
42+
* support up to VLAN_MAX_DEPTH layers of VLAN encapsulation.
43+
*/
44+
#pragma unroll
45+
for (i = 0; i < VLAN_MAX_DEPTH; i++) {
46+
if (!proto_is_vlan(h_proto))
47+
break;
4848

49-
if (vlh + 1 > data_end)
50-
break;
49+
if (vlh + 1 > data_end)
50+
break;
5151

52-
h_proto = vlh->h_vlan_encapsulated_proto;
52+
h_proto = vlh->h_vlan_encapsulated_proto;
5353
if (vlans) {
5454
vlans->id[i] = vlh->h_vlan_TCI & VLAN_VID_MASK;
5555
}
56-
vlh++;
57-
}
56+
vlh++;
57+
}
5858

59-
nh->pos = vlh;
59+
nh->pos = vlh;
6060
return bpf_ntohs(h_proto);
6161
}
6262

0 commit comments

Comments
 (0)