perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / net / netdevsim / bpf.c
blobcb3518474f0e4248967b5c5710c3137288cf23d7
1 /*
2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree.
8 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16 #include <linux/bpf.h>
17 #include <linux/bpf_verifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/kernel.h>
20 #include <linux/mutex.h>
21 #include <linux/rtnetlink.h>
22 #include <net/pkt_cls.h>
24 #include "netdevsim.h"
26 #define pr_vlog(env, fmt, ...) \
27 bpf_verifier_log_write(env, "[netdevsim] " fmt, ##__VA_ARGS__)
29 struct nsim_bpf_bound_prog {
30 struct netdevsim *ns;
31 struct bpf_prog *prog;
32 struct dentry *ddir;
33 const char *state;
34 bool is_loaded;
35 struct list_head l;
38 #define NSIM_BPF_MAX_KEYS 2
40 struct nsim_bpf_bound_map {
41 struct netdevsim *ns;
42 struct bpf_offloaded_map *map;
43 struct mutex mutex;
44 struct nsim_map_entry {
45 void *key;
46 void *value;
47 } entry[NSIM_BPF_MAX_KEYS];
48 struct list_head l;
51 static int nsim_debugfs_bpf_string_read(struct seq_file *file, void *data)
53 const char **str = file->private;
55 if (*str)
56 seq_printf(file, "%s\n", *str);
58 return 0;
61 static int nsim_debugfs_bpf_string_open(struct inode *inode, struct file *f)
63 return single_open(f, nsim_debugfs_bpf_string_read, inode->i_private);
66 static const struct file_operations nsim_bpf_string_fops = {
67 .owner = THIS_MODULE,
68 .open = nsim_debugfs_bpf_string_open,
69 .release = single_release,
70 .read = seq_read,
71 .llseek = seq_lseek
74 static int
75 nsim_bpf_verify_insn(struct bpf_verifier_env *env, int insn_idx, int prev_insn)
77 struct nsim_bpf_bound_prog *state;
79 state = env->prog->aux->offload->dev_priv;
80 if (state->ns->bpf_bind_verifier_delay && !insn_idx)
81 msleep(state->ns->bpf_bind_verifier_delay);
83 if (insn_idx == env->prog->len - 1)
84 pr_vlog(env, "Hello from netdevsim!\n");
86 return 0;
89 static int nsim_bpf_finalize(struct bpf_verifier_env *env)
91 return 0;
94 static const struct bpf_prog_offload_ops nsim_bpf_analyzer_ops = {
95 .insn_hook = nsim_bpf_verify_insn,
96 .finalize = nsim_bpf_finalize,
99 static bool nsim_xdp_offload_active(struct netdevsim *ns)
101 return ns->xdp_hw.prog;
104 static void nsim_prog_set_loaded(struct bpf_prog *prog, bool loaded)
106 struct nsim_bpf_bound_prog *state;
108 if (!prog || !prog->aux->offload)
109 return;
111 state = prog->aux->offload->dev_priv;
112 state->is_loaded = loaded;
115 static int
116 nsim_bpf_offload(struct netdevsim *ns, struct bpf_prog *prog, bool oldprog)
118 nsim_prog_set_loaded(ns->bpf_offloaded, false);
120 WARN(!!ns->bpf_offloaded != oldprog,
121 "bad offload state, expected offload %sto be active",
122 oldprog ? "" : "not ");
123 ns->bpf_offloaded = prog;
124 ns->bpf_offloaded_id = prog ? prog->aux->id : 0;
125 nsim_prog_set_loaded(prog, true);
127 return 0;
130 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
131 void *type_data, void *cb_priv)
133 struct tc_cls_bpf_offload *cls_bpf = type_data;
134 struct bpf_prog *prog = cls_bpf->prog;
135 struct netdevsim *ns = cb_priv;
136 struct bpf_prog *oldprog;
138 if (type != TC_SETUP_CLSBPF) {
139 NSIM_EA(cls_bpf->common.extack,
140 "only offload of BPF classifiers supported");
141 return -EOPNOTSUPP;
144 if (!tc_cls_can_offload_and_chain0(ns->netdev, &cls_bpf->common))
145 return -EOPNOTSUPP;
147 if (cls_bpf->common.protocol != htons(ETH_P_ALL)) {
148 NSIM_EA(cls_bpf->common.extack,
149 "only ETH_P_ALL supported as filter protocol");
150 return -EOPNOTSUPP;
153 if (!ns->bpf_tc_accept) {
154 NSIM_EA(cls_bpf->common.extack,
155 "netdevsim configured to reject BPF TC offload");
156 return -EOPNOTSUPP;
158 /* Note: progs without skip_sw will probably not be dev bound */
159 if (prog && !prog->aux->offload && !ns->bpf_tc_non_bound_accept) {
160 NSIM_EA(cls_bpf->common.extack,
161 "netdevsim configured to reject unbound programs");
162 return -EOPNOTSUPP;
165 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
166 return -EOPNOTSUPP;
168 oldprog = cls_bpf->oldprog;
170 /* Don't remove if oldprog doesn't match driver's state */
171 if (ns->bpf_offloaded != oldprog) {
172 oldprog = NULL;
173 if (!cls_bpf->prog)
174 return 0;
175 if (ns->bpf_offloaded) {
176 NSIM_EA(cls_bpf->common.extack,
177 "driver and netdev offload states mismatch");
178 return -EBUSY;
182 return nsim_bpf_offload(ns, cls_bpf->prog, oldprog);
185 int nsim_bpf_disable_tc(struct netdevsim *ns)
187 if (ns->bpf_offloaded && !nsim_xdp_offload_active(ns))
188 return -EBUSY;
189 return 0;
192 static int nsim_xdp_offload_prog(struct netdevsim *ns, struct netdev_bpf *bpf)
194 if (!nsim_xdp_offload_active(ns) && !bpf->prog)
195 return 0;
196 if (!nsim_xdp_offload_active(ns) && bpf->prog && ns->bpf_offloaded) {
197 NSIM_EA(bpf->extack, "TC program is already loaded");
198 return -EBUSY;
201 return nsim_bpf_offload(ns, bpf->prog, nsim_xdp_offload_active(ns));
204 static int
205 nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf,
206 struct xdp_attachment_info *xdp)
208 int err;
210 if (!xdp_attachment_flags_ok(xdp, bpf))
211 return -EBUSY;
213 if (bpf->command == XDP_SETUP_PROG && !ns->bpf_xdpdrv_accept) {
214 NSIM_EA(bpf->extack, "driver XDP disabled in DebugFS");
215 return -EOPNOTSUPP;
217 if (bpf->command == XDP_SETUP_PROG_HW && !ns->bpf_xdpoffload_accept) {
218 NSIM_EA(bpf->extack, "XDP offload disabled in DebugFS");
219 return -EOPNOTSUPP;
222 if (bpf->command == XDP_SETUP_PROG_HW) {
223 err = nsim_xdp_offload_prog(ns, bpf);
224 if (err)
225 return err;
228 xdp_attachment_setup(xdp, bpf);
230 return 0;
233 static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog)
235 struct nsim_bpf_bound_prog *state;
236 char name[16];
238 state = kzalloc(sizeof(*state), GFP_KERNEL);
239 if (!state)
240 return -ENOMEM;
242 state->ns = ns;
243 state->prog = prog;
244 state->state = "verify";
246 /* Program id is not populated yet when we create the state. */
247 sprintf(name, "%u", ns->sdev->prog_id_gen++);
248 state->ddir = debugfs_create_dir(name, ns->sdev->ddir_bpf_bound_progs);
249 if (IS_ERR_OR_NULL(state->ddir)) {
250 kfree(state);
251 return -ENOMEM;
254 debugfs_create_u32("id", 0400, state->ddir, &prog->aux->id);
255 debugfs_create_file("state", 0400, state->ddir,
256 &state->state, &nsim_bpf_string_fops);
257 debugfs_create_bool("loaded", 0400, state->ddir, &state->is_loaded);
259 list_add_tail(&state->l, &ns->sdev->bpf_bound_progs);
261 prog->aux->offload->dev_priv = state;
263 return 0;
266 static void nsim_bpf_destroy_prog(struct bpf_prog *prog)
268 struct nsim_bpf_bound_prog *state;
270 state = prog->aux->offload->dev_priv;
271 WARN(state->is_loaded,
272 "offload state destroyed while program still bound");
273 debugfs_remove_recursive(state->ddir);
274 list_del(&state->l);
275 kfree(state);
278 static int nsim_setup_prog_checks(struct netdevsim *ns, struct netdev_bpf *bpf)
280 if (bpf->prog && bpf->prog->aux->offload) {
281 NSIM_EA(bpf->extack, "attempt to load offloaded prog to drv");
282 return -EINVAL;
284 if (ns->netdev->mtu > NSIM_XDP_MAX_MTU) {
285 NSIM_EA(bpf->extack, "MTU too large w/ XDP enabled");
286 return -EINVAL;
288 return 0;
291 static int
292 nsim_setup_prog_hw_checks(struct netdevsim *ns, struct netdev_bpf *bpf)
294 struct nsim_bpf_bound_prog *state;
296 if (!bpf->prog)
297 return 0;
299 if (!bpf->prog->aux->offload) {
300 NSIM_EA(bpf->extack, "xdpoffload of non-bound program");
301 return -EINVAL;
303 if (!bpf_offload_dev_match(bpf->prog, ns->netdev)) {
304 NSIM_EA(bpf->extack, "program bound to different dev");
305 return -EINVAL;
308 state = bpf->prog->aux->offload->dev_priv;
309 if (WARN_ON(strcmp(state->state, "xlated"))) {
310 NSIM_EA(bpf->extack, "offloading program in bad state");
311 return -EINVAL;
313 return 0;
316 static bool
317 nsim_map_key_match(struct bpf_map *map, struct nsim_map_entry *e, void *key)
319 return e->key && !memcmp(key, e->key, map->key_size);
322 static int nsim_map_key_find(struct bpf_offloaded_map *offmap, void *key)
324 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
325 unsigned int i;
327 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++)
328 if (nsim_map_key_match(&offmap->map, &nmap->entry[i], key))
329 return i;
331 return -ENOENT;
334 static int
335 nsim_map_alloc_elem(struct bpf_offloaded_map *offmap, unsigned int idx)
337 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
339 nmap->entry[idx].key = kmalloc(offmap->map.key_size, GFP_USER);
340 if (!nmap->entry[idx].key)
341 return -ENOMEM;
342 nmap->entry[idx].value = kmalloc(offmap->map.value_size, GFP_USER);
343 if (!nmap->entry[idx].value) {
344 kfree(nmap->entry[idx].key);
345 nmap->entry[idx].key = NULL;
346 return -ENOMEM;
349 return 0;
352 static int
353 nsim_map_get_next_key(struct bpf_offloaded_map *offmap,
354 void *key, void *next_key)
356 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
357 int idx = -ENOENT;
359 mutex_lock(&nmap->mutex);
361 if (key)
362 idx = nsim_map_key_find(offmap, key);
363 if (idx == -ENOENT)
364 idx = 0;
365 else
366 idx++;
368 for (; idx < ARRAY_SIZE(nmap->entry); idx++) {
369 if (nmap->entry[idx].key) {
370 memcpy(next_key, nmap->entry[idx].key,
371 offmap->map.key_size);
372 break;
376 mutex_unlock(&nmap->mutex);
378 if (idx == ARRAY_SIZE(nmap->entry))
379 return -ENOENT;
380 return 0;
383 static int
384 nsim_map_lookup_elem(struct bpf_offloaded_map *offmap, void *key, void *value)
386 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
387 int idx;
389 mutex_lock(&nmap->mutex);
391 idx = nsim_map_key_find(offmap, key);
392 if (idx >= 0)
393 memcpy(value, nmap->entry[idx].value, offmap->map.value_size);
395 mutex_unlock(&nmap->mutex);
397 return idx < 0 ? idx : 0;
400 static int
401 nsim_map_update_elem(struct bpf_offloaded_map *offmap,
402 void *key, void *value, u64 flags)
404 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
405 int idx, err = 0;
407 mutex_lock(&nmap->mutex);
409 idx = nsim_map_key_find(offmap, key);
410 if (idx < 0 && flags == BPF_EXIST) {
411 err = idx;
412 goto exit_unlock;
414 if (idx >= 0 && flags == BPF_NOEXIST) {
415 err = -EEXIST;
416 goto exit_unlock;
419 if (idx < 0) {
420 for (idx = 0; idx < ARRAY_SIZE(nmap->entry); idx++)
421 if (!nmap->entry[idx].key)
422 break;
423 if (idx == ARRAY_SIZE(nmap->entry)) {
424 err = -E2BIG;
425 goto exit_unlock;
428 err = nsim_map_alloc_elem(offmap, idx);
429 if (err)
430 goto exit_unlock;
433 memcpy(nmap->entry[idx].key, key, offmap->map.key_size);
434 memcpy(nmap->entry[idx].value, value, offmap->map.value_size);
435 exit_unlock:
436 mutex_unlock(&nmap->mutex);
438 return err;
441 static int nsim_map_delete_elem(struct bpf_offloaded_map *offmap, void *key)
443 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
444 int idx;
446 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY)
447 return -EINVAL;
449 mutex_lock(&nmap->mutex);
451 idx = nsim_map_key_find(offmap, key);
452 if (idx >= 0) {
453 kfree(nmap->entry[idx].key);
454 kfree(nmap->entry[idx].value);
455 memset(&nmap->entry[idx], 0, sizeof(nmap->entry[idx]));
458 mutex_unlock(&nmap->mutex);
460 return idx < 0 ? idx : 0;
463 static const struct bpf_map_dev_ops nsim_bpf_map_ops = {
464 .map_get_next_key = nsim_map_get_next_key,
465 .map_lookup_elem = nsim_map_lookup_elem,
466 .map_update_elem = nsim_map_update_elem,
467 .map_delete_elem = nsim_map_delete_elem,
470 static int
471 nsim_bpf_map_alloc(struct netdevsim *ns, struct bpf_offloaded_map *offmap)
473 struct nsim_bpf_bound_map *nmap;
474 int i, err;
476 if (WARN_ON(offmap->map.map_type != BPF_MAP_TYPE_ARRAY &&
477 offmap->map.map_type != BPF_MAP_TYPE_HASH))
478 return -EINVAL;
479 if (offmap->map.max_entries > NSIM_BPF_MAX_KEYS)
480 return -ENOMEM;
481 if (offmap->map.map_flags)
482 return -EINVAL;
484 nmap = kzalloc(sizeof(*nmap), GFP_USER);
485 if (!nmap)
486 return -ENOMEM;
488 offmap->dev_priv = nmap;
489 nmap->ns = ns;
490 nmap->map = offmap;
491 mutex_init(&nmap->mutex);
493 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY) {
494 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) {
495 u32 *key;
497 err = nsim_map_alloc_elem(offmap, i);
498 if (err)
499 goto err_free;
500 key = nmap->entry[i].key;
501 *key = i;
505 offmap->dev_ops = &nsim_bpf_map_ops;
506 list_add_tail(&nmap->l, &ns->sdev->bpf_bound_maps);
508 return 0;
510 err_free:
511 while (--i >= 0) {
512 kfree(nmap->entry[i].key);
513 kfree(nmap->entry[i].value);
515 kfree(nmap);
516 return err;
519 static void nsim_bpf_map_free(struct bpf_offloaded_map *offmap)
521 struct nsim_bpf_bound_map *nmap = offmap->dev_priv;
522 unsigned int i;
524 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) {
525 kfree(nmap->entry[i].key);
526 kfree(nmap->entry[i].value);
528 list_del_init(&nmap->l);
529 mutex_destroy(&nmap->mutex);
530 kfree(nmap);
533 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
535 struct netdevsim *ns = netdev_priv(dev);
536 struct nsim_bpf_bound_prog *state;
537 int err;
539 ASSERT_RTNL();
541 switch (bpf->command) {
542 case BPF_OFFLOAD_VERIFIER_PREP:
543 if (!ns->bpf_bind_accept)
544 return -EOPNOTSUPP;
546 err = nsim_bpf_create_prog(ns, bpf->verifier.prog);
547 if (err)
548 return err;
550 bpf->verifier.ops = &nsim_bpf_analyzer_ops;
551 return 0;
552 case BPF_OFFLOAD_TRANSLATE:
553 state = bpf->offload.prog->aux->offload->dev_priv;
555 state->state = "xlated";
556 return 0;
557 case BPF_OFFLOAD_DESTROY:
558 nsim_bpf_destroy_prog(bpf->offload.prog);
559 return 0;
560 case XDP_QUERY_PROG:
561 return xdp_attachment_query(&ns->xdp, bpf);
562 case XDP_QUERY_PROG_HW:
563 return xdp_attachment_query(&ns->xdp_hw, bpf);
564 case XDP_SETUP_PROG:
565 err = nsim_setup_prog_checks(ns, bpf);
566 if (err)
567 return err;
569 return nsim_xdp_set_prog(ns, bpf, &ns->xdp);
570 case XDP_SETUP_PROG_HW:
571 err = nsim_setup_prog_hw_checks(ns, bpf);
572 if (err)
573 return err;
575 return nsim_xdp_set_prog(ns, bpf, &ns->xdp_hw);
576 case BPF_OFFLOAD_MAP_ALLOC:
577 if (!ns->bpf_map_accept)
578 return -EOPNOTSUPP;
580 return nsim_bpf_map_alloc(ns, bpf->offmap);
581 case BPF_OFFLOAD_MAP_FREE:
582 nsim_bpf_map_free(bpf->offmap);
583 return 0;
584 default:
585 return -EINVAL;
589 int nsim_bpf_init(struct netdevsim *ns)
591 int err;
593 if (ns->sdev->refcnt == 1) {
594 INIT_LIST_HEAD(&ns->sdev->bpf_bound_progs);
595 INIT_LIST_HEAD(&ns->sdev->bpf_bound_maps);
597 ns->sdev->ddir_bpf_bound_progs =
598 debugfs_create_dir("bpf_bound_progs", ns->sdev->ddir);
599 if (IS_ERR_OR_NULL(ns->sdev->ddir_bpf_bound_progs))
600 return -ENOMEM;
602 ns->sdev->bpf_dev = bpf_offload_dev_create();
603 err = PTR_ERR_OR_ZERO(ns->sdev->bpf_dev);
604 if (err)
605 return err;
608 err = bpf_offload_dev_netdev_register(ns->sdev->bpf_dev, ns->netdev);
609 if (err)
610 goto err_destroy_bdev;
612 debugfs_create_u32("bpf_offloaded_id", 0400, ns->ddir,
613 &ns->bpf_offloaded_id);
615 ns->bpf_bind_accept = true;
616 debugfs_create_bool("bpf_bind_accept", 0600, ns->ddir,
617 &ns->bpf_bind_accept);
618 debugfs_create_u32("bpf_bind_verifier_delay", 0600, ns->ddir,
619 &ns->bpf_bind_verifier_delay);
621 ns->bpf_tc_accept = true;
622 debugfs_create_bool("bpf_tc_accept", 0600, ns->ddir,
623 &ns->bpf_tc_accept);
624 debugfs_create_bool("bpf_tc_non_bound_accept", 0600, ns->ddir,
625 &ns->bpf_tc_non_bound_accept);
626 ns->bpf_xdpdrv_accept = true;
627 debugfs_create_bool("bpf_xdpdrv_accept", 0600, ns->ddir,
628 &ns->bpf_xdpdrv_accept);
629 ns->bpf_xdpoffload_accept = true;
630 debugfs_create_bool("bpf_xdpoffload_accept", 0600, ns->ddir,
631 &ns->bpf_xdpoffload_accept);
633 ns->bpf_map_accept = true;
634 debugfs_create_bool("bpf_map_accept", 0600, ns->ddir,
635 &ns->bpf_map_accept);
637 return 0;
639 err_destroy_bdev:
640 if (ns->sdev->refcnt == 1)
641 bpf_offload_dev_destroy(ns->sdev->bpf_dev);
642 return err;
645 void nsim_bpf_uninit(struct netdevsim *ns)
647 WARN_ON(ns->xdp.prog);
648 WARN_ON(ns->xdp_hw.prog);
649 WARN_ON(ns->bpf_offloaded);
650 bpf_offload_dev_netdev_unregister(ns->sdev->bpf_dev, ns->netdev);
652 if (ns->sdev->refcnt == 1) {
653 WARN_ON(!list_empty(&ns->sdev->bpf_bound_progs));
654 WARN_ON(!list_empty(&ns->sdev->bpf_bound_maps));
655 bpf_offload_dev_destroy(ns->sdev->bpf_dev);