]> asedeno.scripts.mit.edu Git - linux.git/blob - net/bpf/test_run.c
62933279fbba9ddde5547ccdee6c12c9bff12dd1
[linux.git] / net / bpf / test_run.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <linux/bpf.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/etherdevice.h>
8 #include <linux/filter.h>
9 #include <linux/sched/signal.h>
10 #include <net/bpf_sk_storage.h>
11 #include <net/sock.h>
12 #include <net/tcp.h>
13
14 #define CREATE_TRACE_POINTS
15 #include <trace/events/bpf_test_run.h>
16
17 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
18                         u32 *retval, u32 *time)
19 {
20         struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL };
21         enum bpf_cgroup_storage_type stype;
22         u64 time_start, time_spent = 0;
23         int ret = 0;
24         u32 i;
25
26         for_each_cgroup_storage_type(stype) {
27                 storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
28                 if (IS_ERR(storage[stype])) {
29                         storage[stype] = NULL;
30                         for_each_cgroup_storage_type(stype)
31                                 bpf_cgroup_storage_free(storage[stype]);
32                         return -ENOMEM;
33                 }
34         }
35
36         if (!repeat)
37                 repeat = 1;
38
39         rcu_read_lock();
40         preempt_disable();
41         time_start = ktime_get_ns();
42         for (i = 0; i < repeat; i++) {
43                 bpf_cgroup_storage_set(storage);
44                 *retval = BPF_PROG_RUN(prog, ctx);
45
46                 if (signal_pending(current)) {
47                         ret = -EINTR;
48                         break;
49                 }
50
51                 if (need_resched()) {
52                         time_spent += ktime_get_ns() - time_start;
53                         preempt_enable();
54                         rcu_read_unlock();
55
56                         cond_resched();
57
58                         rcu_read_lock();
59                         preempt_disable();
60                         time_start = ktime_get_ns();
61                 }
62         }
63         time_spent += ktime_get_ns() - time_start;
64         preempt_enable();
65         rcu_read_unlock();
66
67         do_div(time_spent, repeat);
68         *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
69
70         for_each_cgroup_storage_type(stype)
71                 bpf_cgroup_storage_free(storage[stype]);
72
73         return ret;
74 }
75
76 static int bpf_test_finish(const union bpf_attr *kattr,
77                            union bpf_attr __user *uattr, const void *data,
78                            u32 size, u32 retval, u32 duration)
79 {
80         void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
81         int err = -EFAULT;
82         u32 copy_size = size;
83
84         /* Clamp copy if the user has provided a size hint, but copy the full
85          * buffer if not to retain old behaviour.
86          */
87         if (kattr->test.data_size_out &&
88             copy_size > kattr->test.data_size_out) {
89                 copy_size = kattr->test.data_size_out;
90                 err = -ENOSPC;
91         }
92
93         if (data_out && copy_to_user(data_out, data, copy_size))
94                 goto out;
95         if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
96                 goto out;
97         if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
98                 goto out;
99         if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
100                 goto out;
101         if (err != -ENOSPC)
102                 err = 0;
103 out:
104         trace_bpf_test_finish(&err);
105         return err;
106 }
107
108 /* Integer types of various sizes and pointer combinations cover variety of
109  * architecture dependent calling conventions. 7+ can be supported in the
110  * future.
111  */
112 int noinline bpf_fentry_test1(int a)
113 {
114         return a + 1;
115 }
116
117 int noinline bpf_fentry_test2(int a, u64 b)
118 {
119         return a + b;
120 }
121
122 int noinline bpf_fentry_test3(char a, int b, u64 c)
123 {
124         return a + b + c;
125 }
126
127 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
128 {
129         return (long)a + b + c + d;
130 }
131
132 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
133 {
134         return a + (long)b + c + d + e;
135 }
136
137 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
138 {
139         return a + (long)b + c + d + (long)e + f;
140 }
141
142 static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
143                            u32 headroom, u32 tailroom)
144 {
145         void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
146         void *data;
147
148         if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
149                 return ERR_PTR(-EINVAL);
150
151         data = kzalloc(size + headroom + tailroom, GFP_USER);
152         if (!data)
153                 return ERR_PTR(-ENOMEM);
154
155         if (copy_from_user(data + headroom, data_in, size)) {
156                 kfree(data);
157                 return ERR_PTR(-EFAULT);
158         }
159         if (bpf_fentry_test1(1) != 2 ||
160             bpf_fentry_test2(2, 3) != 5 ||
161             bpf_fentry_test3(4, 5, 6) != 15 ||
162             bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
163             bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
164             bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111)
165                 return ERR_PTR(-EFAULT);
166         return data;
167 }
168
169 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
170 {
171         void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
172         void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
173         u32 size = kattr->test.ctx_size_in;
174         void *data;
175         int err;
176
177         if (!data_in && !data_out)
178                 return NULL;
179
180         data = kzalloc(max_size, GFP_USER);
181         if (!data)
182                 return ERR_PTR(-ENOMEM);
183
184         if (data_in) {
185                 err = bpf_check_uarg_tail_zero(data_in, max_size, size);
186                 if (err) {
187                         kfree(data);
188                         return ERR_PTR(err);
189                 }
190
191                 size = min_t(u32, max_size, size);
192                 if (copy_from_user(data, data_in, size)) {
193                         kfree(data);
194                         return ERR_PTR(-EFAULT);
195                 }
196         }
197         return data;
198 }
199
200 static int bpf_ctx_finish(const union bpf_attr *kattr,
201                           union bpf_attr __user *uattr, const void *data,
202                           u32 size)
203 {
204         void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
205         int err = -EFAULT;
206         u32 copy_size = size;
207
208         if (!data || !data_out)
209                 return 0;
210
211         if (copy_size > kattr->test.ctx_size_out) {
212                 copy_size = kattr->test.ctx_size_out;
213                 err = -ENOSPC;
214         }
215
216         if (copy_to_user(data_out, data, copy_size))
217                 goto out;
218         if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
219                 goto out;
220         if (err != -ENOSPC)
221                 err = 0;
222 out:
223         return err;
224 }
225
226 /**
227  * range_is_zero - test whether buffer is initialized
228  * @buf: buffer to check
229  * @from: check from this position
230  * @to: check up until (excluding) this position
231  *
232  * This function returns true if the there is a non-zero byte
233  * in the buf in the range [from,to).
234  */
235 static inline bool range_is_zero(void *buf, size_t from, size_t to)
236 {
237         return !memchr_inv((u8 *)buf + from, 0, to - from);
238 }
239
240 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
241 {
242         struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
243
244         if (!__skb)
245                 return 0;
246
247         /* make sure the fields we don't use are zeroed */
248         if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority)))
249                 return -EINVAL;
250
251         /* priority is allowed */
252
253         if (!range_is_zero(__skb, offsetof(struct __sk_buff, priority) +
254                            FIELD_SIZEOF(struct __sk_buff, priority),
255                            offsetof(struct __sk_buff, cb)))
256                 return -EINVAL;
257
258         /* cb is allowed */
259
260         if (!range_is_zero(__skb, offsetof(struct __sk_buff, cb) +
261                            FIELD_SIZEOF(struct __sk_buff, cb),
262                            offsetof(struct __sk_buff, tstamp)))
263                 return -EINVAL;
264
265         /* tstamp is allowed */
266
267         if (!range_is_zero(__skb, offsetof(struct __sk_buff, tstamp) +
268                            FIELD_SIZEOF(struct __sk_buff, tstamp),
269                            sizeof(struct __sk_buff)))
270                 return -EINVAL;
271
272         skb->priority = __skb->priority;
273         skb->tstamp = __skb->tstamp;
274         memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
275
276         return 0;
277 }
278
279 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
280 {
281         struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
282
283         if (!__skb)
284                 return;
285
286         __skb->priority = skb->priority;
287         __skb->tstamp = skb->tstamp;
288         memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
289 }
290
291 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
292                           union bpf_attr __user *uattr)
293 {
294         bool is_l2 = false, is_direct_pkt_access = false;
295         u32 size = kattr->test.data_size_in;
296         u32 repeat = kattr->test.repeat;
297         struct __sk_buff *ctx = NULL;
298         u32 retval, duration;
299         int hh_len = ETH_HLEN;
300         struct sk_buff *skb;
301         struct sock *sk;
302         void *data;
303         int ret;
304
305         data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
306                              SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
307         if (IS_ERR(data))
308                 return PTR_ERR(data);
309
310         ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
311         if (IS_ERR(ctx)) {
312                 kfree(data);
313                 return PTR_ERR(ctx);
314         }
315
316         switch (prog->type) {
317         case BPF_PROG_TYPE_SCHED_CLS:
318         case BPF_PROG_TYPE_SCHED_ACT:
319                 is_l2 = true;
320                 /* fall through */
321         case BPF_PROG_TYPE_LWT_IN:
322         case BPF_PROG_TYPE_LWT_OUT:
323         case BPF_PROG_TYPE_LWT_XMIT:
324                 is_direct_pkt_access = true;
325                 break;
326         default:
327                 break;
328         }
329
330         sk = kzalloc(sizeof(struct sock), GFP_USER);
331         if (!sk) {
332                 kfree(data);
333                 kfree(ctx);
334                 return -ENOMEM;
335         }
336         sock_net_set(sk, current->nsproxy->net_ns);
337         sock_init_data(NULL, sk);
338
339         skb = build_skb(data, 0);
340         if (!skb) {
341                 kfree(data);
342                 kfree(ctx);
343                 kfree(sk);
344                 return -ENOMEM;
345         }
346         skb->sk = sk;
347
348         skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
349         __skb_put(skb, size);
350         skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
351         skb_reset_network_header(skb);
352
353         if (is_l2)
354                 __skb_push(skb, hh_len);
355         if (is_direct_pkt_access)
356                 bpf_compute_data_pointers(skb);
357         ret = convert___skb_to_skb(skb, ctx);
358         if (ret)
359                 goto out;
360         ret = bpf_test_run(prog, skb, repeat, &retval, &duration);
361         if (ret)
362                 goto out;
363         if (!is_l2) {
364                 if (skb_headroom(skb) < hh_len) {
365                         int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
366
367                         if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
368                                 ret = -ENOMEM;
369                                 goto out;
370                         }
371                 }
372                 memset(__skb_push(skb, hh_len), 0, hh_len);
373         }
374         convert_skb_to___skb(skb, ctx);
375
376         size = skb->len;
377         /* bpf program can never convert linear skb to non-linear */
378         if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
379                 size = skb_headlen(skb);
380         ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
381         if (!ret)
382                 ret = bpf_ctx_finish(kattr, uattr, ctx,
383                                      sizeof(struct __sk_buff));
384 out:
385         kfree_skb(skb);
386         bpf_sk_storage_free(sk);
387         kfree(sk);
388         kfree(ctx);
389         return ret;
390 }
391
392 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
393                           union bpf_attr __user *uattr)
394 {
395         u32 size = kattr->test.data_size_in;
396         u32 repeat = kattr->test.repeat;
397         struct netdev_rx_queue *rxqueue;
398         struct xdp_buff xdp = {};
399         u32 retval, duration;
400         void *data;
401         int ret;
402
403         if (kattr->test.ctx_in || kattr->test.ctx_out)
404                 return -EINVAL;
405
406         data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
407         if (IS_ERR(data))
408                 return PTR_ERR(data);
409
410         xdp.data_hard_start = data;
411         xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
412         xdp.data_meta = xdp.data;
413         xdp.data_end = xdp.data + size;
414
415         rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
416         xdp.rxq = &rxqueue->xdp_rxq;
417
418         ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration);
419         if (ret)
420                 goto out;
421         if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
422             xdp.data_end != xdp.data + size)
423                 size = xdp.data_end - xdp.data;
424         ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
425 out:
426         kfree(data);
427         return ret;
428 }
429
430 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
431 {
432         /* make sure the fields we don't use are zeroed */
433         if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
434                 return -EINVAL;
435
436         /* flags is allowed */
437
438         if (!range_is_zero(ctx, offsetof(struct bpf_flow_keys, flags) +
439                            FIELD_SIZEOF(struct bpf_flow_keys, flags),
440                            sizeof(struct bpf_flow_keys)))
441                 return -EINVAL;
442
443         return 0;
444 }
445
446 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
447                                      const union bpf_attr *kattr,
448                                      union bpf_attr __user *uattr)
449 {
450         u32 size = kattr->test.data_size_in;
451         struct bpf_flow_dissector ctx = {};
452         u32 repeat = kattr->test.repeat;
453         struct bpf_flow_keys *user_ctx;
454         struct bpf_flow_keys flow_keys;
455         u64 time_start, time_spent = 0;
456         const struct ethhdr *eth;
457         unsigned int flags = 0;
458         u32 retval, duration;
459         void *data;
460         int ret;
461         u32 i;
462
463         if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
464                 return -EINVAL;
465
466         if (size < ETH_HLEN)
467                 return -EINVAL;
468
469         data = bpf_test_init(kattr, size, 0, 0);
470         if (IS_ERR(data))
471                 return PTR_ERR(data);
472
473         eth = (struct ethhdr *)data;
474
475         if (!repeat)
476                 repeat = 1;
477
478         user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
479         if (IS_ERR(user_ctx)) {
480                 kfree(data);
481                 return PTR_ERR(user_ctx);
482         }
483         if (user_ctx) {
484                 ret = verify_user_bpf_flow_keys(user_ctx);
485                 if (ret)
486                         goto out;
487                 flags = user_ctx->flags;
488         }
489
490         ctx.flow_keys = &flow_keys;
491         ctx.data = data;
492         ctx.data_end = (__u8 *)data + size;
493
494         rcu_read_lock();
495         preempt_disable();
496         time_start = ktime_get_ns();
497         for (i = 0; i < repeat; i++) {
498                 retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
499                                           size, flags);
500
501                 if (signal_pending(current)) {
502                         preempt_enable();
503                         rcu_read_unlock();
504
505                         ret = -EINTR;
506                         goto out;
507                 }
508
509                 if (need_resched()) {
510                         time_spent += ktime_get_ns() - time_start;
511                         preempt_enable();
512                         rcu_read_unlock();
513
514                         cond_resched();
515
516                         rcu_read_lock();
517                         preempt_disable();
518                         time_start = ktime_get_ns();
519                 }
520         }
521         time_spent += ktime_get_ns() - time_start;
522         preempt_enable();
523         rcu_read_unlock();
524
525         do_div(time_spent, repeat);
526         duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
527
528         ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
529                               retval, duration);
530         if (!ret)
531                 ret = bpf_ctx_finish(kattr, uattr, user_ctx,
532                                      sizeof(struct bpf_flow_keys));
533
534 out:
535         kfree(user_ctx);
536         kfree(data);
537         return ret;
538 }