Skip to content

Commit 7b8a130

Browse files
fomichevborkmann
authored andcommitted
bpf: when doing BPF_PROG_TEST_RUN for flow dissector use no-skb mode
Now that we have bpf_flow_dissect which can work on raw data, use it when doing BPF_PROG_TEST_RUN for flow dissector. Simplifies bpf_prog_test_run_flow_dissector and allows us to test no-skb mode. Note, that previously, with bpf_flow_dissect_skb we used to call eth_type_trans which pulled L2 (ETH_HLEN) header and we explicitly called skb_reset_network_header. That means flow_keys->nhoff would be initialized to 0 (skb_network_offset) in init_flow_keys. Now we call bpf_flow_dissect with nhoff set to ETH_HLEN and need to undo it once the dissection is done to preserve the existing behavior. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 089b19a commit 7b8a130

1 file changed

Lines changed: 17 additions & 30 deletions

File tree

‎net/bpf/test_run.c‎

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
379379
union bpf_attr __user *uattr)
380380
{
381381
u32 size = kattr->test.data_size_in;
382+
struct bpf_flow_dissector ctx = {};
382383
u32 repeat = kattr->test.repeat;
383384
struct bpf_flow_keys flow_keys;
384385
u64 time_start, time_spent = 0;
386+
const struct ethhdr *eth;
385387
u32 retval, duration;
386-
struct sk_buff *skb;
387-
struct sock *sk;
388388
void *data;
389389
int ret;
390390
u32 i;
@@ -395,43 +395,31 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
395395
if (kattr->test.ctx_in || kattr->test.ctx_out)
396396
return -EINVAL;
397397

398-
data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
399-
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
398+
if (size < ETH_HLEN)
399+
return -EINVAL;
400+
401+
data = bpf_test_init(kattr, size, 0, 0);
400402
if (IS_ERR(data))
401403
return PTR_ERR(data);
402404

403-
sk = kzalloc(sizeof(*sk), GFP_USER);
404-
if (!sk) {
405-
kfree(data);
406-
return -ENOMEM;
407-
}
408-
sock_net_set(sk, current->nsproxy->net_ns);
409-
sock_init_data(NULL, sk);
410-
411-
skb = build_skb(data, 0);
412-
if (!skb) {
413-
kfree(data);
414-
kfree(sk);
415-
return -ENOMEM;
416-
}
417-
skb->sk = sk;
418-
419-
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
420-
__skb_put(skb, size);
421-
skb->protocol = eth_type_trans(skb,
422-
current->nsproxy->net_ns->loopback_dev);
423-
skb_reset_network_header(skb);
405+
eth = (struct ethhdr *)data;
424406

425407
if (!repeat)
426408
repeat = 1;
427409

410+
ctx.flow_keys = &flow_keys;
411+
ctx.data = data;
412+
ctx.data_end = (__u8 *)data + size;
413+
428414
rcu_read_lock();
429415
preempt_disable();
430416
time_start = ktime_get_ns();
431417
for (i = 0; i < repeat; i++) {
432-
retval = __skb_flow_bpf_dissect(prog, skb,
433-
&flow_keys_dissector,
434-
&flow_keys);
418+
retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
419+
size);
420+
421+
flow_keys.nhoff -= ETH_HLEN;
422+
flow_keys.thoff -= ETH_HLEN;
435423

436424
if (signal_pending(current)) {
437425
preempt_enable();
@@ -464,7 +452,6 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
464452
retval, duration);
465453

466454
out:
467-
kfree_skb(skb);
468-
kfree(sk);
455+
kfree(data);
469456
return ret;
470457
}

0 commit comments

Comments
 (0)