Commit 7b8a1304 authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Daniel Borkmann
Browse files

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: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 089b19a9
Loading
Loading
Loading
Loading
+17 −30
Original line number Original line Diff line number Diff line
@@ -379,12 +379,12 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
				     union bpf_attr __user *uattr)
				     union bpf_attr __user *uattr)
{
{
	u32 size = kattr->test.data_size_in;
	u32 size = kattr->test.data_size_in;
	struct bpf_flow_dissector ctx = {};
	u32 repeat = kattr->test.repeat;
	u32 repeat = kattr->test.repeat;
	struct bpf_flow_keys flow_keys;
	struct bpf_flow_keys flow_keys;
	u64 time_start, time_spent = 0;
	u64 time_start, time_spent = 0;
	const struct ethhdr *eth;
	u32 retval, duration;
	u32 retval, duration;
	struct sk_buff *skb;
	struct sock *sk;
	void *data;
	void *data;
	int ret;
	int ret;
	u32 i;
	u32 i;
@@ -395,43 +395,31 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
	if (kattr->test.ctx_in || kattr->test.ctx_out)
	if (kattr->test.ctx_in || kattr->test.ctx_out)
		return -EINVAL;
		return -EINVAL;


	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
	if (size < ETH_HLEN)
			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
		return -EINVAL;

	data = bpf_test_init(kattr, size, 0, 0);
	if (IS_ERR(data))
	if (IS_ERR(data))
		return PTR_ERR(data);
		return PTR_ERR(data);


	sk = kzalloc(sizeof(*sk), GFP_USER);
	eth = (struct ethhdr *)data;
	if (!sk) {
		kfree(data);
		return -ENOMEM;
	}
	sock_net_set(sk, current->nsproxy->net_ns);
	sock_init_data(NULL, sk);

	skb = build_skb(data, 0);
	if (!skb) {
		kfree(data);
		kfree(sk);
		return -ENOMEM;
	}
	skb->sk = sk;

	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
	__skb_put(skb, size);
	skb->protocol = eth_type_trans(skb,
				       current->nsproxy->net_ns->loopback_dev);
	skb_reset_network_header(skb);


	if (!repeat)
	if (!repeat)
		repeat = 1;
		repeat = 1;


	ctx.flow_keys = &flow_keys;
	ctx.data = data;
	ctx.data_end = (__u8 *)data + size;

	rcu_read_lock();
	rcu_read_lock();
	preempt_disable();
	preempt_disable();
	time_start = ktime_get_ns();
	time_start = ktime_get_ns();
	for (i = 0; i < repeat; i++) {
	for (i = 0; i < repeat; i++) {
		retval = __skb_flow_bpf_dissect(prog, skb,
		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
						&flow_keys_dissector,
					  size);
						&flow_keys);

		flow_keys.nhoff -= ETH_HLEN;
		flow_keys.thoff -= ETH_HLEN;


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


out:
out:
	kfree_skb(skb);
	kfree(data);
	kfree(sk);
	return ret;
	return ret;
}
}