Commit ed5941af authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

bpf: Add cb access in kfree_skb test

Access the skb->cb[] in the kfree_skb test.
Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191107180905.4097871-1-kafai@fb.com
parent 7e3617a7
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include <test_progs.h> #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) static void on_sample(void *ctx, int cpu, void *data, __u32 size)
{ {
int ifindex = *(int *)data, duration = 0; struct meta *meta = (struct meta *)data;
struct ipv6_packet *pkt_v6 = data + 4; 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 */ /* spurious kfree_skb not on loopback device */
return; 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; return;
if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth", if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth",
"h_proto %x\n", pkt_v6->eth.h_proto)) "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) ...@@ -26,6 +49,13 @@ static void on_sample(void *ctx, int cpu, void *data, __u32 size)
void test_kfree_skb(void) 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 = { struct bpf_prog_load_attr attr = {
.file = "./kfree_skb.o", .file = "./kfree_skb.o",
}; };
...@@ -36,11 +66,12 @@ void test_kfree_skb(void) ...@@ -36,11 +66,12 @@ void test_kfree_skb(void)
struct bpf_link *link = NULL; struct bpf_link *link = NULL;
struct bpf_map *perf_buf_map; struct bpf_map *perf_buf_map;
struct bpf_program *prog; struct bpf_program *prog;
__u32 duration, retval; int err, kfree_skb_fd;
int err, pkt_fd, kfree_skb_fd;
bool passed = false; 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)) if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
return; return;
...@@ -66,11 +97,12 @@ void test_kfree_skb(void) ...@@ -66,11 +97,12 @@ void test_kfree_skb(void)
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
goto close_prog; goto close_prog;
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6), memcpy(skb.cb, &cb, sizeof(cb));
NULL, NULL, &retval, &duration); err = bpf_prog_test_run_xattr(&tattr);
CHECK(err || retval, "ipv6", duration = tattr.duration;
CHECK(err || tattr.retval, "ipv6",
"err %d errno %d retval %d duration %d\n", "err %d errno %d retval %d duration %d\n",
err, errno, retval, duration); err, errno, tattr.retval, duration);
/* read perf buffer */ /* read perf buffer */
err = perf_buffer__poll(pb, 100); err = perf_buffer__poll(pb, 100);
......
...@@ -43,6 +43,7 @@ struct sk_buff { ...@@ -43,6 +43,7 @@ struct sk_buff {
refcount_t users; refcount_t users;
unsigned char *data; unsigned char *data;
char __pkt_type_offset[0]; char __pkt_type_offset[0];
char cb[48];
}; };
/* copy arguments from /* copy arguments from
...@@ -57,28 +58,41 @@ struct trace_kfree_skb { ...@@ -57,28 +58,41 @@ struct trace_kfree_skb {
void *location; void *location;
}; };
struct meta {
int ifindex;
__u32 cb32_0;
__u8 cb8_0;
};
SEC("tp_btf/kfree_skb") SEC("tp_btf/kfree_skb")
int trace_kfree_skb(struct trace_kfree_skb *ctx) int trace_kfree_skb(struct trace_kfree_skb *ctx)
{ {
struct sk_buff *skb = ctx->skb; struct sk_buff *skb = ctx->skb;
struct net_device *dev; struct net_device *dev;
int ifindex;
struct callback_head *ptr; struct callback_head *ptr;
void *func; void *func;
int users; int users;
unsigned char *data; unsigned char *data;
unsigned short pkt_data; unsigned short pkt_data;
struct meta meta = {};
char pkt_type; char pkt_type;
__u32 *cb32;
__u8 *cb8;
__builtin_preserve_access_index(({ __builtin_preserve_access_index(({
users = skb->users.refs.counter; users = skb->users.refs.counter;
data = skb->data; data = skb->data;
dev = skb->dev; dev = skb->dev;
ifindex = dev->ifindex;
ptr = dev->ifalias->rcuhead.next; ptr = dev->ifalias->rcuhead.next;
func = ptr->func; 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)); bpf_probe_read_kernel(&pkt_type, sizeof(pkt_type), _(&skb->__pkt_type_offset));
pkt_type &= 7; pkt_type &= 7;
...@@ -90,14 +104,15 @@ int trace_kfree_skb(struct trace_kfree_skb *ctx) ...@@ -90,14 +104,15 @@ int trace_kfree_skb(struct trace_kfree_skb *ctx)
_(skb->len), users, pkt_type); _(skb->len), users, pkt_type);
bpf_printk("skb->queue_mapping %d\n", _(skb->queue_mapping)); bpf_printk("skb->queue_mapping %d\n", _(skb->queue_mapping));
bpf_printk("dev->ifindex %d data %llx pkt_data %x\n", 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 */ /* raw tp ignores return value */
return 0; return 0;
/* send first 72 byte of the packet to user space */ /* send first 72 byte of the packet to user space */
bpf_skb_output(skb, &perf_buf_map, (72ull << 32) | BPF_F_CURRENT_CPU, bpf_skb_output(skb, &perf_buf_map, (72ull << 32) | BPF_F_CURRENT_CPU,
&ifindex, sizeof(ifindex)); &meta, sizeof(meta));
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment