Re: [PATCH] samples/bpf: Remove unneeded variable

From: Jesper Dangaard Brouer
Date: Tue Jun 13 2023 - 07:45:17 EST




On 13/06/2023 11.18, baomingtong001@xxxxxxxxxx wrote:
Fix the following coccicheck warning:

samples/bpf/xdp1_kern.c:50: Unneeded variable: "rc".

Signed-off-by: Mingtong Bao <baomingtong001@xxxxxxxxxx>
---
 samples/bpf/xdp1_kern.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/samples/bpf/xdp1_kern.c b/samples/bpf/xdp1_kern.c
index d91f27cbcfa9..d426df4a9d6b 100644
--- a/samples/bpf/xdp1_kern.c
+++ b/samples/bpf/xdp1_kern.c
@@ -47,18 +47,17 @@ int xdp_prog1(struct xdp_md *ctx)
     void *data_end = &pkt[XDPBUFSIZE-1];
     void *data = pkt;
     struct ethhdr *eth = data;
-    int rc = XDP_DROP;
     long *value;
     u16 h_proto;
     u64 nh_off;
     u32 ipproto;

     if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
-        return rc;
+        return XDP_DROP;

IMHO for these error cases, we should return XDP_ABORTED instead.
This will make is easier to debug, e.g. with xdpdump[1] or xdp-monitor[2].

Reminder that drivers (usually) have a tracepoint for XDP_ABORTED
(code see trace_xdp_exception).

[1] https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump
[2] https://github.com/xdp-project/xdp-tools/tree/master/xdp-monitor


     nh_off = sizeof(*eth);
     if (data + nh_off > data_end)
-        return rc;
+        return XDP_DROP;

     h_proto = eth->h_proto;

@@ -69,7 +68,7 @@ int xdp_prog1(struct xdp_md *ctx)
         vhdr = data + nh_off;
         nh_off += sizeof(struct vlan_hdr);
         if (data + nh_off > data_end)
-            return rc;
+            return XDP_DROP;

Use XDP_ABORTED

         h_proto = vhdr->h_vlan_encapsulated_proto;
     }
     /* Handle double VLAN tagged packet */
@@ -79,7 +78,7 @@ int xdp_prog1(struct xdp_md *ctx)
         vhdr = data + nh_off;
         nh_off += sizeof(struct vlan_hdr);
         if (data + nh_off > data_end)
-            return rc;
+            return XDP_DROP;

Use XDP_ABORTED

         h_proto = vhdr->h_vlan_encapsulated_proto;
     }

@@ -94,7 +93,7 @@ int xdp_prog1(struct xdp_md *ctx)
     if (value)
         *value += 1;

-    return rc;
+    return XDP_DROP;

This is correct. Here use XDP_DROP.

 }

 char _license[] SEC("license") = "GPL";