blob: 25001913d03b599dde50e85a20de61156465359b [file] [log] [blame]
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001/* Copyright (c) 2017 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/bpf.h>
8#include <linux/slab.h>
9#include <linux/vmalloc.h>
10#include <linux/etherdevice.h>
11#include <linux/filter.h>
12#include <linux/sched/signal.h>
Song Liu2cb494a2018-10-19 09:57:58 -070013#include <net/sock.h>
14#include <net/tcp.h>
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070015
Roman Gushchinf42ee092018-08-02 14:27:27 -070016static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx,
Roman Gushchin8bad74f2018-09-28 14:45:36 +000017 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070018{
19 u32 ret;
20
21 preempt_disable();
22 rcu_read_lock();
Roman Gushchinf42ee092018-08-02 14:27:27 -070023 bpf_cgroup_storage_set(storage);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070024 ret = BPF_PROG_RUN(prog, ctx);
25 rcu_read_unlock();
26 preempt_enable();
27
28 return ret;
29}
30
Roman Gushchindcb40592018-12-01 10:39:44 -080031static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *ret,
32 u32 *time)
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070033{
Roman Gushchin8bad74f2018-09-28 14:45:36 +000034 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
35 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070036 u64 time_start, time_spent = 0;
Roman Gushchindcb40592018-12-01 10:39:44 -080037 u32 i;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070038
Roman Gushchin8bad74f2018-09-28 14:45:36 +000039 for_each_cgroup_storage_type(stype) {
40 storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
41 if (IS_ERR(storage[stype])) {
42 storage[stype] = NULL;
43 for_each_cgroup_storage_type(stype)
44 bpf_cgroup_storage_free(storage[stype]);
45 return -ENOMEM;
46 }
47 }
Roman Gushchinf42ee092018-08-02 14:27:27 -070048
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070049 if (!repeat)
50 repeat = 1;
51 time_start = ktime_get_ns();
52 for (i = 0; i < repeat; i++) {
Roman Gushchindcb40592018-12-01 10:39:44 -080053 *ret = bpf_test_run_one(prog, ctx, storage);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070054 if (need_resched()) {
55 if (signal_pending(current))
56 break;
57 time_spent += ktime_get_ns() - time_start;
58 cond_resched();
59 time_start = ktime_get_ns();
60 }
61 }
62 time_spent += ktime_get_ns() - time_start;
63 do_div(time_spent, repeat);
64 *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
65
Roman Gushchin8bad74f2018-09-28 14:45:36 +000066 for_each_cgroup_storage_type(stype)
67 bpf_cgroup_storage_free(storage[stype]);
Roman Gushchinf42ee092018-08-02 14:27:27 -070068
Roman Gushchindcb40592018-12-01 10:39:44 -080069 return 0;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070070}
71
David Miller78e52272017-05-02 11:36:33 -040072static int bpf_test_finish(const union bpf_attr *kattr,
73 union bpf_attr __user *uattr, const void *data,
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070074 u32 size, u32 retval, u32 duration)
75{
David Miller78e52272017-05-02 11:36:33 -040076 void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -070077 int err = -EFAULT;
78
79 if (data_out && copy_to_user(data_out, data, size))
80 goto out;
81 if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
82 goto out;
83 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
84 goto out;
85 if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
86 goto out;
87 err = 0;
88out:
89 return err;
90}
91
92static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
93 u32 headroom, u32 tailroom)
94{
95 void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
96 void *data;
97
98 if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
99 return ERR_PTR(-EINVAL);
100
101 data = kzalloc(size + headroom + tailroom, GFP_USER);
102 if (!data)
103 return ERR_PTR(-ENOMEM);
104
105 if (copy_from_user(data + headroom, data_in, size)) {
106 kfree(data);
107 return ERR_PTR(-EFAULT);
108 }
109 return data;
110}
111
112int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
113 union bpf_attr __user *uattr)
114{
115 bool is_l2 = false, is_direct_pkt_access = false;
116 u32 size = kattr->test.data_size_in;
117 u32 repeat = kattr->test.repeat;
118 u32 retval, duration;
Daniel Borkmann6e6fddc2018-07-11 15:30:14 +0200119 int hh_len = ETH_HLEN;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700120 struct sk_buff *skb;
Song Liu2cb494a2018-10-19 09:57:58 -0700121 struct sock *sk;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700122 void *data;
123 int ret;
124
David Miller586f8522017-05-02 11:36:45 -0400125 data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700126 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
127 if (IS_ERR(data))
128 return PTR_ERR(data);
129
130 switch (prog->type) {
131 case BPF_PROG_TYPE_SCHED_CLS:
132 case BPF_PROG_TYPE_SCHED_ACT:
133 is_l2 = true;
134 /* fall through */
135 case BPF_PROG_TYPE_LWT_IN:
136 case BPF_PROG_TYPE_LWT_OUT:
137 case BPF_PROG_TYPE_LWT_XMIT:
138 is_direct_pkt_access = true;
139 break;
140 default:
141 break;
142 }
143
Song Liu2cb494a2018-10-19 09:57:58 -0700144 sk = kzalloc(sizeof(struct sock), GFP_USER);
145 if (!sk) {
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700146 kfree(data);
147 return -ENOMEM;
148 }
Song Liu2cb494a2018-10-19 09:57:58 -0700149 sock_net_set(sk, current->nsproxy->net_ns);
150 sock_init_data(NULL, sk);
151
152 skb = build_skb(data, 0);
153 if (!skb) {
154 kfree(data);
155 kfree(sk);
156 return -ENOMEM;
157 }
158 skb->sk = sk;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700159
David Miller586f8522017-05-02 11:36:45 -0400160 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700161 __skb_put(skb, size);
162 skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
163 skb_reset_network_header(skb);
164
165 if (is_l2)
Daniel Borkmann6e6fddc2018-07-11 15:30:14 +0200166 __skb_push(skb, hh_len);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700167 if (is_direct_pkt_access)
Daniel Borkmann6aaae2b2017-09-25 02:25:50 +0200168 bpf_compute_data_pointers(skb);
Roman Gushchindcb40592018-12-01 10:39:44 -0800169 ret = bpf_test_run(prog, skb, repeat, &retval, &duration);
170 if (ret) {
171 kfree_skb(skb);
172 kfree(sk);
173 return ret;
174 }
Daniel Borkmann6e6fddc2018-07-11 15:30:14 +0200175 if (!is_l2) {
176 if (skb_headroom(skb) < hh_len) {
177 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
178
179 if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
180 kfree_skb(skb);
Song Liu2cb494a2018-10-19 09:57:58 -0700181 kfree(sk);
Daniel Borkmann6e6fddc2018-07-11 15:30:14 +0200182 return -ENOMEM;
183 }
184 }
185 memset(__skb_push(skb, hh_len), 0, hh_len);
186 }
187
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700188 size = skb->len;
189 /* bpf program can never convert linear skb to non-linear */
190 if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
191 size = skb_headlen(skb);
David Miller78e52272017-05-02 11:36:33 -0400192 ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700193 kfree_skb(skb);
Song Liu2cb494a2018-10-19 09:57:58 -0700194 kfree(sk);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700195 return ret;
196}
197
198int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
199 union bpf_attr __user *uattr)
200{
201 u32 size = kattr->test.data_size_in;
202 u32 repeat = kattr->test.repeat;
Daniel Borkmann65073a62018-01-31 12:58:56 +0100203 struct netdev_rx_queue *rxqueue;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700204 struct xdp_buff xdp = {};
205 u32 retval, duration;
206 void *data;
207 int ret;
208
David Miller586f8522017-05-02 11:36:45 -0400209 data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700210 if (IS_ERR(data))
211 return PTR_ERR(data);
212
213 xdp.data_hard_start = data;
David Miller586f8522017-05-02 11:36:45 -0400214 xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200215 xdp.data_meta = xdp.data;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700216 xdp.data_end = xdp.data + size;
217
Daniel Borkmann65073a62018-01-31 12:58:56 +0100218 rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
219 xdp.rxq = &rxqueue->xdp_rxq;
220
Roman Gushchindcb40592018-12-01 10:39:44 -0800221 ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration);
222 if (ret)
223 goto out;
Nikita V. Shirokov587b80c2018-04-17 21:42:21 -0700224 if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
225 xdp.data_end != xdp.data + size)
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700226 size = xdp.data_end - xdp.data;
David Miller78e52272017-05-02 11:36:33 -0400227 ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
Roman Gushchindcb40592018-12-01 10:39:44 -0800228out:
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700229 kfree(data);
230 return ret;
231}