]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
xdp: improve coding style of the filter
authorDaniel Salzman <daniel.salzman@nic.cz>
Tue, 24 Mar 2020 20:03:49 +0000 (21:03 +0100)
committerDaniel Salzman <daniel.salzman@nic.cz>
Tue, 21 Apr 2020 16:43:10 +0000 (18:43 +0200)
src/libknot/xdp/bpf-kernel.c

index deb5cd2c564f8febe8582690e82c759257ed55ca..c385fa8b6e451fffb0f7daf30c7f9a9c519a9fbb 100644 (file)
@@ -62,7 +62,7 @@ int xdp_redirect_udp_func(struct xdp_md *ctx)
        __u8 ip_proto;
        __u8 fragmented = 0;
 
-       /* Parse Ethernet header (VLAN not supported). */
+       /* Parse Ethernet header. */
        if ((void *)eth + sizeof(*eth) > data_end) {
                return XDP_PASS;
        }
@@ -70,37 +70,38 @@ int xdp_redirect_udp_func(struct xdp_md *ctx)
 
        /* Parse IPv4 or IPv6 header. */
        switch (eth->h_proto) {
-               case 0x0008: /* htons(ETH_P_IP) */
-                       ip4 = data;
-                       if ((void *)ip4 + sizeof(*ip4) > data_end) {
-                               return XDP_PASS;
-                       }
-                       if (ip4->frag_off != 0 && ip4->frag_off != 0x0040) { /* htons(IP_DF) */
-                               fragmented = 1;
-                       }
-                       ip_proto = ip4->protocol;
-                       udp = data + ip4->ihl * 4;
-                       break;
-               case 0xDD86: /* htons(ETH_P_IPV6) */
-                       ip6 = data;
-                       if ((void *)ip6 + sizeof(*ip6) > data_end) {
+       case 0x0008: /* htons(ETH_P_IP) */
+               ip4 = data;
+               if ((void *)ip4 + sizeof(*ip4) > data_end) {
+                       return XDP_PASS;
+               }
+               if (ip4->frag_off != 0 && ip4->frag_off != 0x0040) { /* htons(IP_DF) */
+                       fragmented = 1;
+               }
+               ip_proto = ip4->protocol;
+               udp = data + ip4->ihl * 4;
+               break;
+       case 0xDD86: /* htons(ETH_P_IPV6) */
+               ip6 = data;
+               if ((void *)ip6 + sizeof(*ip6) > data_end) {
+                       return XDP_PASS;
+               }
+               ip_proto = ip6->nexthdr;
+               data += sizeof(*ip6);
+               if (ip_proto == IPPROTO_FRAGMENT) {
+                       fragmented = 1;
+                       const struct ipv6_frag_hdr *frag = data;
+                       if ((void *)frag + sizeof(*frag) > data_end) {
                                return XDP_PASS;
                        }
-                       ip_proto = ip6->nexthdr;
-                       data += sizeof(*ip6);
-                       if (ip_proto == IPPROTO_FRAGMENT) {
-                               fragmented = 1;
-                               const struct ipv6_frag_hdr *frag = data;
-                               if ((void *)frag + sizeof(*frag) > data_end) {
-                                       return XDP_PASS;
-                               }
-                               ip_proto = frag->nexthdr;
-                               data += sizeof(*frag);
-                       }
-                       udp = data;
-                       break;
-               default:
-                       return XDP_PASS;
+                       ip_proto = frag->nexthdr;
+                       data += sizeof(*frag);
+               }
+               udp = data;
+               break;
+       default:
+               /* Also applies to VLAN. */
+               return XDP_PASS;
        }
 
        /* Treat UDP only. */
@@ -113,6 +114,7 @@ int xdp_redirect_udp_func(struct xdp_md *ctx)
                return XDP_PASS;
        }
 
+       /* Get the queue options. */
        int index = ctx->rx_queue_index;
        int *qidconf = bpf_map_lookup_elem(&qidconf_map, &index);
        if (!qidconf) {
@@ -137,5 +139,6 @@ int xdp_redirect_udp_func(struct xdp_md *ctx)
                return XDP_DROP;
        }
 
+       /* Forward the packet to user space. */
        return bpf_redirect_map(&xsks_map, index, 0);
 }