1616 */
1717static __always_inline int vlan_tag_pop (struct xdp_md * ctx , struct ethhdr * eth )
1818{
19- /*void *data_end = (void *)(long)ctx->data_end;
20- struct ethhdr eth_cpy;
21- struct vlan_hdr *vlh;
22- __be16 h_proto;*/
23- int vlid = -1 ;
19+ /*
20+ void *data_end = (void *)(long)ctx->data_end;
21+ struct ethhdr eth_cpy;
22+ struct vlan_hdr *vlh;
23+ __be16 h_proto;
24+ */
25+ int vlid = -1 ;
2426
25- /* Check if there is a vlan tag to pop */
27+ /* Check if there is a vlan tag to pop */
2628
27- /* Still need to do bounds checking */
29+ /* Still need to do bounds checking */
2830
29- /* Save vlan ID for returning, h_proto for updating Ethernet header */
31+ /* Save vlan ID for returning, h_proto for updating Ethernet header */
3032
31- /* Make a copy of the outer Ethernet header before we cut it off */
33+ /* Make a copy of the outer Ethernet header before we cut it off */
3234
33- /* Actually adjust the head pointer */
35+ /* Actually adjust the head pointer */
3436
35- /* Need to re-evaluate data *and* data_end and do new bounds checking
36- * after adjusting head
37- */
37+ /* Need to re-evaluate data *and* data_end and do new bounds checking
38+ * after adjusting head
39+ */
3840
39- /* Copy back the old Ethernet header and update the proto type */
41+ /* Copy back the old Ethernet header and update the proto type */
4042
4143
42- return vlid ;
44+ return vlid ;
4345}
4446
4547/* Pushes a new VLAN tag after the Ethernet header. Returns 0 on success,
4648 * -1 on failure.
4749 */
4850static __always_inline int vlan_tag_push (struct xdp_md * ctx ,
49- struct ethhdr * eth , int vlid )
51+ struct ethhdr * eth , int vlid )
5052{
51- return 0 ;
53+ return 0 ;
5254}
5355
5456/* Implement assignment 1 in this section */
5557SEC ("xdp_port_rewrite" )
5658int xdp_port_rewrite_func (struct xdp_md * ctx )
5759{
58- return XDP_PASS ;
60+ return XDP_PASS ;
5961}
6062
6163/* VLAN swapper; will pop outermost VLAN tag if it exists, otherwise push a new
@@ -67,23 +69,23 @@ int xdp_vlan_swap_func(struct xdp_md *ctx)
6769 void * data_end = (void * )(long )ctx -> data_end ;
6870 void * data = (void * )(long )ctx -> data ;
6971
70- /* These keep track of the next header type and iterator pointer */
72+ /* These keep track of the next header type and iterator pointer */
7173 struct hdr_cursor nh ;
7274 int nh_type ;
73- nh .pos = data ;
75+ nh .pos = data ;
7476
7577 struct ethhdr * eth ;
7678 nh_type = parse_ethhdr (& nh , data_end , & eth );
77- if (nh_type < 0 )
78- return XDP_PASS ;
79+ if (nh_type < 0 )
80+ return XDP_PASS ;
7981
80- /* Assignment 2 and 3 will implement these. For now they do nothing */
81- if (proto_is_vlan (eth -> h_proto ))
82- vlan_tag_pop (ctx , eth );
83- else
84- vlan_tag_push (ctx , eth , 1 );
82+ /* Assignment 2 and 3 will implement these. For now they do nothing */
83+ if (proto_is_vlan (eth -> h_proto ))
84+ vlan_tag_pop (ctx , eth );
85+ else
86+ vlan_tag_push (ctx , eth , 1 );
8587
86- return XDP_PASS ;
88+ return XDP_PASS ;
8789}
8890
8991/* Solution to the parsing exercise in lesson packet01. Handles VLANs and legacy
@@ -101,10 +103,10 @@ int xdp_parser_func(struct xdp_md *ctx)
101103 */
102104 __u32 action = XDP_PASS ; /* Default action */
103105
104- /* These keep track of the next header type and iterator pointer */
106+ /* These keep track of the next header type and iterator pointer */
105107 struct hdr_cursor nh ;
106108 int nh_type ;
107- nh .pos = data ;
109+ nh .pos = data ;
108110
109111 struct ethhdr * eth ;
110112
@@ -114,37 +116,37 @@ int xdp_parser_func(struct xdp_md *ctx)
114116 */
115117 nh_type = parse_ethhdr (& nh , data_end , & eth );
116118
117- if (nh_type == ETH_P_IPV6 ) {
118- struct ipv6hdr * ip6h ;
119- struct icmp6hdr * icmp6h ;
119+ if (nh_type == ETH_P_IPV6 ) {
120+ struct ipv6hdr * ip6h ;
121+ struct icmp6hdr * icmp6h ;
120122
121- nh_type = parse_ip6hdr (& nh , data_end , & ip6h );
122- if (nh_type != IPPROTO_ICMPV6 )
123- goto out ;
123+ nh_type = parse_ip6hdr (& nh , data_end , & ip6h );
124+ if (nh_type != IPPROTO_ICMPV6 )
125+ goto out ;
124126
125- nh_type = parse_icmp6hdr (& nh , data_end , & icmp6h );
126- if (nh_type != ICMPV6_ECHO_REQUEST )
127- goto out ;
127+ nh_type = parse_icmp6hdr (& nh , data_end , & icmp6h );
128+ if (nh_type != ICMPV6_ECHO_REQUEST )
129+ goto out ;
128130
129- if (bpf_ntohs (icmp6h -> icmp6_sequence ) % 2 == 0 )
130- action = XDP_DROP ;
131+ if (bpf_ntohs (icmp6h -> icmp6_sequence ) % 2 == 0 )
132+ action = XDP_DROP ;
131133
132- } else if (nh_type == ETH_P_IP ) {
133- struct iphdr * iph ;
134- struct icmphdr * icmph ;
134+ } else if (nh_type == ETH_P_IP ) {
135+ struct iphdr * iph ;
136+ struct icmphdr * icmph ;
135137
136- nh_type = parse_iphdr (& nh , data_end , & iph );
137- if (nh_type != IPPROTO_ICMP )
138- goto out ;
138+ nh_type = parse_iphdr (& nh , data_end , & iph );
139+ if (nh_type != IPPROTO_ICMP )
140+ goto out ;
139141
140- nh_type = parse_icmphdr (& nh , data_end , & icmph );
141- if (nh_type != ICMP_ECHO )
142- goto out ;
142+ nh_type = parse_icmphdr (& nh , data_end , & icmph );
143+ if (nh_type != ICMP_ECHO )
144+ goto out ;
143145
144- if (bpf_ntohs (icmph -> un .echo .sequence ) % 2 == 0 )
145- action = XDP_DROP ;
146- }
147- out :
146+ if (bpf_ntohs (icmph -> un .echo .sequence ) % 2 == 0 )
147+ action = XDP_DROP ;
148+ }
149+ out :
148150 return xdp_stats_record_action (ctx , action );
149151}
150152
0 commit comments