bpf: Add cb access in kfree_skb test
authorMartin KaFai Lau <kafai@fb.com>
Thu, 7 Nov 2019 18:09:05 +0000 (10:09 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 7 Nov 2019 18:59:08 +0000 (10:59 -0800)
Access the skb->cb[] in the kfree_skb test.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191107180905.4097871-1-kafai@fb.com
tools/testing/selftests/bpf/prog_tests/kfree_skb.c
tools/testing/selftests/bpf/progs/kfree_skb.c

index 430b50d..55d3685 100644 (file)
@@ -1,15 +1,38 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 
+struct meta {
+       int ifindex;
+       __u32 cb32_0;
+       __u8 cb8_0;
+};
+
+static union {
+       __u32 cb32[5];
+       __u8 cb8[20];
+} cb = {
+       .cb32[0] = 0x81828384,
+};
+
 static void on_sample(void *ctx, int cpu, void *data, __u32 size)
 {
-       int ifindex = *(int *)data, duration = 0;
-       struct ipv6_packet *pkt_v6 = data + 4;
+       struct meta *meta = (struct meta *)data;
+       struct ipv6_packet *pkt_v6 = data + sizeof(*meta);
+       int duration = 0;
 
-       if (ifindex != 1)
+       if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n",
+                 size, 72 + sizeof(*meta)))
+               return;
+       if (CHECK(meta->ifindex != 1, "check_meta_ifindex",
+                 "meta->ifindex = %d\n", meta->ifindex))
                /* spurious kfree_skb not on loopback device */
                return;
-       if (CHECK(size != 76, "check_size", "size %u != 76\n", size))
+       if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n",
+                 meta->cb8_0, cb.cb8[0]))
+               return;
+       if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0",
+                 "cb32_0 %x != %x\n",
+                 meta->cb32_0, cb.cb32[0]))
                return;
        if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth",
                  "h_proto %x\n", pkt_v6->eth.h_proto))
@@ -26,6 +49,13 @@ static void on_sample(void *ctx, int cpu, void *data, __u32 size)
 
 void test_kfree_skb(void)
 {
+       struct __sk_buff skb = {};
+       struct bpf_prog_test_run_attr tattr = {
+               .data_in = &pkt_v6,
+               .data_size_in = sizeof(pkt_v6),
+               .ctx_in = &skb,
+               .ctx_size_in = sizeof(skb),
+       };
        struct bpf_prog_load_attr attr = {
                .file = "./kfree_skb.o",
        };
@@ -36,11 +66,12 @@ void test_kfree_skb(void)
        struct bpf_link *link = NULL;
        struct bpf_map *perf_buf_map;
        struct bpf_program *prog;
-       __u32 duration, retval;
-       int err, pkt_fd, kfree_skb_fd;
+       int err, kfree_skb_fd;
        bool passed = false;
+       __u32 duration = 0;
 
-       err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &pkt_fd);
+       err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
+                           &obj, &tattr.prog_fd);
        if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
                return;
 
@@ -66,11 +97,12 @@ void test_kfree_skb(void)
        if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
                goto close_prog;
 
-       err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6),
-                               NULL, NULL, &retval, &duration);
-       CHECK(err || retval, "ipv6",
+       memcpy(skb.cb, &cb, sizeof(cb));
+       err = bpf_prog_test_run_xattr(&tattr);
+       duration = tattr.duration;
+       CHECK(err || tattr.retval, "ipv6",
              "err %d errno %d retval %d duration %d\n",
-             err, errno, retval, duration);
+             err, errno, tattr.retval, duration);
 
        /* read perf buffer */
        err = perf_buffer__poll(pb, 100);
index 489319e..f769fdb 100644 (file)
@@ -43,6 +43,7 @@ struct sk_buff {
        refcount_t users;
        unsigned char *data;
        char __pkt_type_offset[0];
+       char cb[48];
 };
 
 /* copy arguments from
@@ -57,28 +58,41 @@ struct trace_kfree_skb {
        void *location;
 };
 
+struct meta {
+       int ifindex;
+       __u32 cb32_0;
+       __u8 cb8_0;
+};
+
 SEC("tp_btf/kfree_skb")
 int trace_kfree_skb(struct trace_kfree_skb *ctx)
 {
        struct sk_buff *skb = ctx->skb;
        struct net_device *dev;
-       int ifindex;
        struct callback_head *ptr;
        void *func;
        int users;
        unsigned char *data;
        unsigned short pkt_data;
+       struct meta meta = {};
        char pkt_type;
+       __u32 *cb32;
+       __u8 *cb8;
 
        __builtin_preserve_access_index(({
                users = skb->users.refs.counter;
                data = skb->data;
                dev = skb->dev;
-               ifindex = dev->ifindex;
                ptr = dev->ifalias->rcuhead.next;
                func = ptr->func;
+               cb8 = (__u8 *)&skb->cb;
+               cb32 = (__u32 *)&skb->cb;
        }));
 
+       meta.ifindex = _(dev->ifindex);
+       meta.cb8_0 = cb8[8];
+       meta.cb32_0 = cb32[2];
+
        bpf_probe_read_kernel(&pkt_type, sizeof(pkt_type), _(&skb->__pkt_type_offset));
        pkt_type &= 7;
 
@@ -90,14 +104,15 @@ int trace_kfree_skb(struct trace_kfree_skb *ctx)
                   _(skb->len), users, pkt_type);
        bpf_printk("skb->queue_mapping %d\n", _(skb->queue_mapping));
        bpf_printk("dev->ifindex %d data %llx pkt_data %x\n",
-                  ifindex, data, pkt_data);
+                  meta.ifindex, data, pkt_data);
+       bpf_printk("cb8_0:%x cb32_0:%x\n", meta.cb8_0, meta.cb32_0);
 
-       if (users != 1 || pkt_data != bpf_htons(0x86dd) || ifindex != 1)
+       if (users != 1 || pkt_data != bpf_htons(0x86dd) || meta.ifindex != 1)
                /* raw tp ignores return value */
                return 0;
 
        /* send first 72 byte of the packet to user space */
        bpf_skb_output(skb, &perf_buf_map, (72ull << 32) | BPF_F_CURRENT_CPU,
-                      &ifindex, sizeof(ifindex));
+                      &meta, sizeof(meta));
        return 0;
 }