1a39e17b2SJakub Kicinski /*
20cd3cbedSJakub Kicinski * Copyright (C) 2017-2018 Netronome Systems, Inc.
3a39e17b2SJakub Kicinski *
4a39e17b2SJakub Kicinski * This software is licensed under the GNU General License Version 2,
5a39e17b2SJakub Kicinski * June 1991 as shown in the file COPYING in the top-level directory of this
6a39e17b2SJakub Kicinski * source tree.
7a39e17b2SJakub Kicinski *
8a39e17b2SJakub Kicinski * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9a39e17b2SJakub Kicinski * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10a39e17b2SJakub Kicinski * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11a39e17b2SJakub Kicinski * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12a39e17b2SJakub Kicinski * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13a39e17b2SJakub Kicinski * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
14a39e17b2SJakub Kicinski */
15a39e17b2SJakub Kicinski
16ab3f0063SJakub Kicinski #include <linux/bpf.h>
17ab3f0063SJakub Kicinski #include <linux/bpf_verifier.h>
18ab3f0063SJakub Kicinski #include <linux/bug.h>
19675fc275SJakub Kicinski #include <linux/kdev_t.h>
20ab3f0063SJakub Kicinski #include <linux/list.h>
219fd7c555SJakub Kicinski #include <linux/lockdep.h>
22ab3f0063SJakub Kicinski #include <linux/netdevice.h>
23ab3f0063SJakub Kicinski #include <linux/printk.h>
24675fc275SJakub Kicinski #include <linux/proc_ns.h>
259fd7c555SJakub Kicinski #include <linux/rhashtable.h>
26ab3f0063SJakub Kicinski #include <linux/rtnetlink.h>
27e0d3974aSJakub Kicinski #include <linux/rwsem.h>
28680ee045SJakub Kicinski #include <net/xdp.h>
29ab3f0063SJakub Kicinski
309fd7c555SJakub Kicinski /* Protects offdevs, members of bpf_offload_netdev and offload members
31a3884572SJakub Kicinski * of all progs.
32e0d3974aSJakub Kicinski * RTNL lock cannot be taken when holding this lock.
33e0d3974aSJakub Kicinski */
34e0d3974aSJakub Kicinski static DECLARE_RWSEM(bpf_devs_lock);
359fd7c555SJakub Kicinski
36602144c2SJakub Kicinski struct bpf_offload_dev {
371385d755SQuentin Monnet const struct bpf_prog_offload_ops *ops;
38602144c2SJakub Kicinski struct list_head netdevs;
39dd27c2e3SJakub Kicinski void *priv;
40602144c2SJakub Kicinski };
41602144c2SJakub Kicinski
429fd7c555SJakub Kicinski struct bpf_offload_netdev {
439fd7c555SJakub Kicinski struct rhash_head l;
449fd7c555SJakub Kicinski struct net_device *netdev;
452b3486bcSStanislav Fomichev struct bpf_offload_dev *offdev; /* NULL when bound-only */
469fd7c555SJakub Kicinski struct list_head progs;
479fd7c555SJakub Kicinski struct list_head maps;
48602144c2SJakub Kicinski struct list_head offdev_netdevs;
499fd7c555SJakub Kicinski };
509fd7c555SJakub Kicinski
519fd7c555SJakub Kicinski static const struct rhashtable_params offdevs_params = {
529fd7c555SJakub Kicinski .nelem_hint = 4,
539fd7c555SJakub Kicinski .key_len = sizeof(struct net_device *),
549fd7c555SJakub Kicinski .key_offset = offsetof(struct bpf_offload_netdev, netdev),
559fd7c555SJakub Kicinski .head_offset = offsetof(struct bpf_offload_netdev, l),
569fd7c555SJakub Kicinski .automatic_shrinking = true,
579fd7c555SJakub Kicinski };
589fd7c555SJakub Kicinski
599fd7c555SJakub Kicinski static struct rhashtable offdevs;
60ab3f0063SJakub Kicinski
bpf_dev_offload_check(struct net_device * netdev)615bc2d55cSJakub Kicinski static int bpf_dev_offload_check(struct net_device *netdev)
625bc2d55cSJakub Kicinski {
635bc2d55cSJakub Kicinski if (!netdev)
645bc2d55cSJakub Kicinski return -EINVAL;
655bc2d55cSJakub Kicinski if (!netdev->netdev_ops->ndo_bpf)
665bc2d55cSJakub Kicinski return -EOPNOTSUPP;
675bc2d55cSJakub Kicinski return 0;
685bc2d55cSJakub Kicinski }
695bc2d55cSJakub Kicinski
709fd7c555SJakub Kicinski static struct bpf_offload_netdev *
bpf_offload_find_netdev(struct net_device * netdev)719fd7c555SJakub Kicinski bpf_offload_find_netdev(struct net_device *netdev)
729fd7c555SJakub Kicinski {
739fd7c555SJakub Kicinski lockdep_assert_held(&bpf_devs_lock);
749fd7c555SJakub Kicinski
759fd7c555SJakub Kicinski return rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
769fd7c555SJakub Kicinski }
779fd7c555SJakub Kicinski
__bpf_offload_dev_netdev_register(struct bpf_offload_dev * offdev,struct net_device * netdev)7889bbc53aSStanislav Fomichev static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
7989bbc53aSStanislav Fomichev struct net_device *netdev)
8089bbc53aSStanislav Fomichev {
8189bbc53aSStanislav Fomichev struct bpf_offload_netdev *ondev;
8289bbc53aSStanislav Fomichev int err;
8389bbc53aSStanislav Fomichev
8489bbc53aSStanislav Fomichev ondev = kzalloc(sizeof(*ondev), GFP_KERNEL);
8589bbc53aSStanislav Fomichev if (!ondev)
8689bbc53aSStanislav Fomichev return -ENOMEM;
8789bbc53aSStanislav Fomichev
8889bbc53aSStanislav Fomichev ondev->netdev = netdev;
8989bbc53aSStanislav Fomichev ondev->offdev = offdev;
9089bbc53aSStanislav Fomichev INIT_LIST_HEAD(&ondev->progs);
9189bbc53aSStanislav Fomichev INIT_LIST_HEAD(&ondev->maps);
9289bbc53aSStanislav Fomichev
9389bbc53aSStanislav Fomichev err = rhashtable_insert_fast(&offdevs, &ondev->l, offdevs_params);
9489bbc53aSStanislav Fomichev if (err) {
9589bbc53aSStanislav Fomichev netdev_warn(netdev, "failed to register for BPF offload\n");
962b3486bcSStanislav Fomichev goto err_free;
9789bbc53aSStanislav Fomichev }
9889bbc53aSStanislav Fomichev
992b3486bcSStanislav Fomichev if (offdev)
10089bbc53aSStanislav Fomichev list_add(&ondev->offdev_netdevs, &offdev->netdevs);
10189bbc53aSStanislav Fomichev return 0;
10289bbc53aSStanislav Fomichev
1032b3486bcSStanislav Fomichev err_free:
10489bbc53aSStanislav Fomichev kfree(ondev);
10589bbc53aSStanislav Fomichev return err;
10689bbc53aSStanislav Fomichev }
10789bbc53aSStanislav Fomichev
__bpf_prog_offload_destroy(struct bpf_prog * prog)10889bbc53aSStanislav Fomichev static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
10989bbc53aSStanislav Fomichev {
11089bbc53aSStanislav Fomichev struct bpf_prog_offload *offload = prog->aux->offload;
11189bbc53aSStanislav Fomichev
11289bbc53aSStanislav Fomichev if (offload->dev_state)
11389bbc53aSStanislav Fomichev offload->offdev->ops->destroy(prog);
11489bbc53aSStanislav Fomichev
11589bbc53aSStanislav Fomichev list_del_init(&offload->offloads);
11689bbc53aSStanislav Fomichev kfree(offload);
11789bbc53aSStanislav Fomichev prog->aux->offload = NULL;
11889bbc53aSStanislav Fomichev }
11989bbc53aSStanislav Fomichev
bpf_map_offload_ndo(struct bpf_offloaded_map * offmap,enum bpf_netdev_command cmd)12089bbc53aSStanislav Fomichev static int bpf_map_offload_ndo(struct bpf_offloaded_map *offmap,
12189bbc53aSStanislav Fomichev enum bpf_netdev_command cmd)
12289bbc53aSStanislav Fomichev {
12389bbc53aSStanislav Fomichev struct netdev_bpf data = {};
12489bbc53aSStanislav Fomichev struct net_device *netdev;
12589bbc53aSStanislav Fomichev
12689bbc53aSStanislav Fomichev ASSERT_RTNL();
12789bbc53aSStanislav Fomichev
12889bbc53aSStanislav Fomichev data.command = cmd;
12989bbc53aSStanislav Fomichev data.offmap = offmap;
13089bbc53aSStanislav Fomichev /* Caller must make sure netdev is valid */
13189bbc53aSStanislav Fomichev netdev = offmap->netdev;
13289bbc53aSStanislav Fomichev
13389bbc53aSStanislav Fomichev return netdev->netdev_ops->ndo_bpf(netdev, &data);
13489bbc53aSStanislav Fomichev }
13589bbc53aSStanislav Fomichev
__bpf_map_offload_destroy(struct bpf_offloaded_map * offmap)13689bbc53aSStanislav Fomichev static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap)
13789bbc53aSStanislav Fomichev {
13889bbc53aSStanislav Fomichev WARN_ON(bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_FREE));
13989bbc53aSStanislav Fomichev /* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map */
140158e5e9eSTobias Klauser bpf_map_free_id(&offmap->map);
14189bbc53aSStanislav Fomichev list_del_init(&offmap->offloads);
14289bbc53aSStanislav Fomichev offmap->netdev = NULL;
14389bbc53aSStanislav Fomichev }
14489bbc53aSStanislav Fomichev
__bpf_offload_dev_netdev_unregister(struct bpf_offload_dev * offdev,struct net_device * netdev)14589bbc53aSStanislav Fomichev static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
14689bbc53aSStanislav Fomichev struct net_device *netdev)
14789bbc53aSStanislav Fomichev {
1482b3486bcSStanislav Fomichev struct bpf_offload_netdev *ondev, *altdev = NULL;
14989bbc53aSStanislav Fomichev struct bpf_offloaded_map *offmap, *mtmp;
15089bbc53aSStanislav Fomichev struct bpf_prog_offload *offload, *ptmp;
15189bbc53aSStanislav Fomichev
15289bbc53aSStanislav Fomichev ASSERT_RTNL();
15389bbc53aSStanislav Fomichev
15489bbc53aSStanislav Fomichev ondev = rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
15589bbc53aSStanislav Fomichev if (WARN_ON(!ondev))
1562b3486bcSStanislav Fomichev return;
15789bbc53aSStanislav Fomichev
15889bbc53aSStanislav Fomichev WARN_ON(rhashtable_remove_fast(&offdevs, &ondev->l, offdevs_params));
15989bbc53aSStanislav Fomichev
16089bbc53aSStanislav Fomichev /* Try to move the objects to another netdev of the device */
1612b3486bcSStanislav Fomichev if (offdev) {
1622b3486bcSStanislav Fomichev list_del(&ondev->offdev_netdevs);
16389bbc53aSStanislav Fomichev altdev = list_first_entry_or_null(&offdev->netdevs,
16489bbc53aSStanislav Fomichev struct bpf_offload_netdev,
16589bbc53aSStanislav Fomichev offdev_netdevs);
1662b3486bcSStanislav Fomichev }
1672b3486bcSStanislav Fomichev
16889bbc53aSStanislav Fomichev if (altdev) {
16989bbc53aSStanislav Fomichev list_for_each_entry(offload, &ondev->progs, offloads)
17089bbc53aSStanislav Fomichev offload->netdev = altdev->netdev;
17189bbc53aSStanislav Fomichev list_splice_init(&ondev->progs, &altdev->progs);
17289bbc53aSStanislav Fomichev
17389bbc53aSStanislav Fomichev list_for_each_entry(offmap, &ondev->maps, offloads)
17489bbc53aSStanislav Fomichev offmap->netdev = altdev->netdev;
17589bbc53aSStanislav Fomichev list_splice_init(&ondev->maps, &altdev->maps);
17689bbc53aSStanislav Fomichev } else {
17789bbc53aSStanislav Fomichev list_for_each_entry_safe(offload, ptmp, &ondev->progs, offloads)
17889bbc53aSStanislav Fomichev __bpf_prog_offload_destroy(offload->prog);
17989bbc53aSStanislav Fomichev list_for_each_entry_safe(offmap, mtmp, &ondev->maps, offloads)
18089bbc53aSStanislav Fomichev __bpf_map_offload_destroy(offmap);
18189bbc53aSStanislav Fomichev }
18289bbc53aSStanislav Fomichev
18389bbc53aSStanislav Fomichev WARN_ON(!list_empty(&ondev->progs));
18489bbc53aSStanislav Fomichev WARN_ON(!list_empty(&ondev->maps));
18589bbc53aSStanislav Fomichev kfree(ondev);
18689bbc53aSStanislav Fomichev }
18789bbc53aSStanislav Fomichev
__bpf_prog_dev_bound_init(struct bpf_prog * prog,struct net_device * netdev)188fd7c211dSToke Høiland-Jørgensen static int __bpf_prog_dev_bound_init(struct bpf_prog *prog, struct net_device *netdev)
189ab3f0063SJakub Kicinski {
1909fd7c555SJakub Kicinski struct bpf_offload_netdev *ondev;
1910a9c1991SJakub Kicinski struct bpf_prog_offload *offload;
1925bc2d55cSJakub Kicinski int err;
193ab3f0063SJakub Kicinski
194ab3f0063SJakub Kicinski offload = kzalloc(sizeof(*offload), GFP_USER);
195ab3f0063SJakub Kicinski if (!offload)
196ab3f0063SJakub Kicinski return -ENOMEM;
197ab3f0063SJakub Kicinski
198ab3f0063SJakub Kicinski offload->prog = prog;
199fd7c211dSToke Høiland-Jørgensen offload->netdev = netdev;
200ab3f0063SJakub Kicinski
2019fd7c555SJakub Kicinski ondev = bpf_offload_find_netdev(offload->netdev);
202*1a49f419SEduard Zingerman /* When program is offloaded require presence of "true"
203*1a49f419SEduard Zingerman * bpf_offload_netdev, avoid the one created for !ondev case below.
204*1a49f419SEduard Zingerman */
205*1a49f419SEduard Zingerman if (bpf_prog_is_offloaded(prog->aux) && (!ondev || !ondev->offdev)) {
2065bc2d55cSJakub Kicinski err = -EINVAL;
207fd7c211dSToke Høiland-Jørgensen goto err_free;
208fd7c211dSToke Høiland-Jørgensen }
209*1a49f419SEduard Zingerman if (!ondev) {
210fd7c211dSToke Høiland-Jørgensen /* When only binding to the device, explicitly
211fd7c211dSToke Høiland-Jørgensen * create an entry in the hashtable.
212fd7c211dSToke Høiland-Jørgensen */
213fd7c211dSToke Høiland-Jørgensen err = __bpf_offload_dev_netdev_register(NULL, offload->netdev);
214fd7c211dSToke Høiland-Jørgensen if (err)
215fd7c211dSToke Høiland-Jørgensen goto err_free;
216fd7c211dSToke Høiland-Jørgensen ondev = bpf_offload_find_netdev(offload->netdev);
2175bc2d55cSJakub Kicinski }
218341b3e7bSQuentin Monnet offload->offdev = ondev->offdev;
219ab3f0063SJakub Kicinski prog->aux->offload = offload;
2209fd7c555SJakub Kicinski list_add_tail(&offload->offloads, &ondev->progs);
221ab3f0063SJakub Kicinski
222ab3f0063SJakub Kicinski return 0;
223fd7c211dSToke Høiland-Jørgensen err_free:
224e0d3974aSJakub Kicinski kfree(offload);
2255bc2d55cSJakub Kicinski return err;
226ab3f0063SJakub Kicinski }
227ab3f0063SJakub Kicinski
bpf_prog_dev_bound_init(struct bpf_prog * prog,union bpf_attr * attr)228fd7c211dSToke Høiland-Jørgensen int bpf_prog_dev_bound_init(struct bpf_prog *prog, union bpf_attr *attr)
229fd7c211dSToke Høiland-Jørgensen {
230fd7c211dSToke Høiland-Jørgensen struct net_device *netdev;
231fd7c211dSToke Høiland-Jørgensen int err;
232fd7c211dSToke Høiland-Jørgensen
233ab3f0063SJakub Kicinski if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
234ab3f0063SJakub Kicinski attr->prog_type != BPF_PROG_TYPE_XDP)
235ab3f0063SJakub Kicinski return -EINVAL;
236ab3f0063SJakub Kicinski
2372b3486bcSStanislav Fomichev if (attr->prog_flags & ~BPF_F_XDP_DEV_BOUND_ONLY)
2382b3486bcSStanislav Fomichev return -EINVAL;
2392b3486bcSStanislav Fomichev
2402b3486bcSStanislav Fomichev if (attr->prog_type == BPF_PROG_TYPE_SCHED_CLS &&
2412b3486bcSStanislav Fomichev attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY)
242ab3f0063SJakub Kicinski return -EINVAL;
243ab3f0063SJakub Kicinski
244fd7c211dSToke Høiland-Jørgensen netdev = dev_get_by_index(current->nsproxy->net_ns, attr->prog_ifindex);
245fd7c211dSToke Høiland-Jørgensen if (!netdev)
246fd7c211dSToke Høiland-Jørgensen return -EINVAL;
247ab3f0063SJakub Kicinski
248fd7c211dSToke Høiland-Jørgensen err = bpf_dev_offload_check(netdev);
249ab3f0063SJakub Kicinski if (err)
250fd7c211dSToke Høiland-Jørgensen goto out;
251ab3f0063SJakub Kicinski
2522b3486bcSStanislav Fomichev prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY);
2532b3486bcSStanislav Fomichev
254ab3f0063SJakub Kicinski down_write(&bpf_devs_lock);
255fd7c211dSToke Høiland-Jørgensen err = __bpf_prog_dev_bound_init(prog, netdev);
256ab3f0063SJakub Kicinski up_write(&bpf_devs_lock);
257ab3f0063SJakub Kicinski
258fd7c211dSToke Høiland-Jørgensen out:
259fd7c211dSToke Høiland-Jørgensen dev_put(netdev);
260fd7c211dSToke Høiland-Jørgensen return err;
261fd7c211dSToke Høiland-Jørgensen }
262fd7c211dSToke Høiland-Jørgensen
bpf_prog_dev_bound_inherit(struct bpf_prog * new_prog,struct bpf_prog * old_prog)263fd7c211dSToke Høiland-Jørgensen int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog, struct bpf_prog *old_prog)
264fd7c211dSToke Høiland-Jørgensen {
265fd7c211dSToke Høiland-Jørgensen int err;
266fd7c211dSToke Høiland-Jørgensen
267fd7c211dSToke Høiland-Jørgensen if (!bpf_prog_is_dev_bound(old_prog->aux))
268ab3f0063SJakub Kicinski return 0;
269fd7c211dSToke Høiland-Jørgensen
270fd7c211dSToke Høiland-Jørgensen if (bpf_prog_is_offloaded(old_prog->aux))
271fd7c211dSToke Høiland-Jørgensen return -EINVAL;
272fd7c211dSToke Høiland-Jørgensen
273fd7c211dSToke Høiland-Jørgensen new_prog->aux->dev_bound = old_prog->aux->dev_bound;
274fd7c211dSToke Høiland-Jørgensen new_prog->aux->offload_requested = old_prog->aux->offload_requested;
275fd7c211dSToke Høiland-Jørgensen
276fd7c211dSToke Høiland-Jørgensen down_write(&bpf_devs_lock);
277fd7c211dSToke Høiland-Jørgensen if (!old_prog->aux->offload) {
278fd7c211dSToke Høiland-Jørgensen err = -EINVAL;
279fd7c211dSToke Høiland-Jørgensen goto out;
280fd7c211dSToke Høiland-Jørgensen }
281fd7c211dSToke Høiland-Jørgensen
282fd7c211dSToke Høiland-Jørgensen err = __bpf_prog_dev_bound_init(new_prog, old_prog->aux->offload->netdev);
283fd7c211dSToke Høiland-Jørgensen
284fd7c211dSToke Høiland-Jørgensen out:
285ab3f0063SJakub Kicinski up_write(&bpf_devs_lock);
286ab3f0063SJakub Kicinski return err;
287ab3f0063SJakub Kicinski }
288ab3f0063SJakub Kicinski
bpf_prog_offload_verifier_prep(struct bpf_prog * prog)289a40a2632SQuentin Monnet int bpf_prog_offload_verifier_prep(struct bpf_prog *prog)
290ab3f0063SJakub Kicinski {
29100db12c3SQuentin Monnet struct bpf_prog_offload *offload;
29200db12c3SQuentin Monnet int ret = -ENODEV;
293ab3f0063SJakub Kicinski
29400db12c3SQuentin Monnet down_read(&bpf_devs_lock);
295a40a2632SQuentin Monnet offload = prog->aux->offload;
296592ee43fSColin Ian King if (offload) {
29716a8cb5cSQuentin Monnet ret = offload->offdev->ops->prepare(prog);
29800db12c3SQuentin Monnet offload->dev_state = !ret;
299592ee43fSColin Ian King }
30000db12c3SQuentin Monnet up_read(&bpf_devs_lock);
301ab3f0063SJakub Kicinski
30200db12c3SQuentin Monnet return ret;
303ab3f0063SJakub Kicinski }
304ab3f0063SJakub Kicinski
bpf_prog_offload_verify_insn(struct bpf_verifier_env * env,int insn_idx,int prev_insn_idx)305cae1927cSJakub Kicinski int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
306cae1927cSJakub Kicinski int insn_idx, int prev_insn_idx)
307cae1927cSJakub Kicinski {
3080a9c1991SJakub Kicinski struct bpf_prog_offload *offload;
309cae1927cSJakub Kicinski int ret = -ENODEV;
310cae1927cSJakub Kicinski
311cae1927cSJakub Kicinski down_read(&bpf_devs_lock);
312cae1927cSJakub Kicinski offload = env->prog->aux->offload;
313ce3b9db4SJakub Kicinski if (offload)
314341b3e7bSQuentin Monnet ret = offload->offdev->ops->insn_hook(env, insn_idx,
315341b3e7bSQuentin Monnet prev_insn_idx);
316cae1927cSJakub Kicinski up_read(&bpf_devs_lock);
317cae1927cSJakub Kicinski
318cae1927cSJakub Kicinski return ret;
319cae1927cSJakub Kicinski }
320cae1927cSJakub Kicinski
bpf_prog_offload_finalize(struct bpf_verifier_env * env)321c941ce9cSQuentin Monnet int bpf_prog_offload_finalize(struct bpf_verifier_env *env)
322c941ce9cSQuentin Monnet {
323c941ce9cSQuentin Monnet struct bpf_prog_offload *offload;
324c941ce9cSQuentin Monnet int ret = -ENODEV;
325c941ce9cSQuentin Monnet
326c941ce9cSQuentin Monnet down_read(&bpf_devs_lock);
327c941ce9cSQuentin Monnet offload = env->prog->aux->offload;
328c941ce9cSQuentin Monnet if (offload) {
3296dc18fa6SQuentin Monnet if (offload->offdev->ops->finalize)
3306dc18fa6SQuentin Monnet ret = offload->offdev->ops->finalize(env);
331c941ce9cSQuentin Monnet else
332c941ce9cSQuentin Monnet ret = 0;
333c941ce9cSQuentin Monnet }
334c941ce9cSQuentin Monnet up_read(&bpf_devs_lock);
335c941ce9cSQuentin Monnet
336c941ce9cSQuentin Monnet return ret;
337c941ce9cSQuentin Monnet }
338c941ce9cSQuentin Monnet
33908ca90afSJakub Kicinski void
bpf_prog_offload_replace_insn(struct bpf_verifier_env * env,u32 off,struct bpf_insn * insn)34008ca90afSJakub Kicinski bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
34108ca90afSJakub Kicinski struct bpf_insn *insn)
34208ca90afSJakub Kicinski {
34308ca90afSJakub Kicinski const struct bpf_prog_offload_ops *ops;
34408ca90afSJakub Kicinski struct bpf_prog_offload *offload;
34508ca90afSJakub Kicinski int ret = -EOPNOTSUPP;
34608ca90afSJakub Kicinski
34708ca90afSJakub Kicinski down_read(&bpf_devs_lock);
34808ca90afSJakub Kicinski offload = env->prog->aux->offload;
34908ca90afSJakub Kicinski if (offload) {
35008ca90afSJakub Kicinski ops = offload->offdev->ops;
35108ca90afSJakub Kicinski if (!offload->opt_failed && ops->replace_insn)
35208ca90afSJakub Kicinski ret = ops->replace_insn(env, off, insn);
35308ca90afSJakub Kicinski offload->opt_failed |= ret;
35408ca90afSJakub Kicinski }
35508ca90afSJakub Kicinski up_read(&bpf_devs_lock);
35608ca90afSJakub Kicinski }
35708ca90afSJakub Kicinski
35808ca90afSJakub Kicinski void
bpf_prog_offload_remove_insns(struct bpf_verifier_env * env,u32 off,u32 cnt)35908ca90afSJakub Kicinski bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
36008ca90afSJakub Kicinski {
36108ca90afSJakub Kicinski struct bpf_prog_offload *offload;
36208ca90afSJakub Kicinski int ret = -EOPNOTSUPP;
36308ca90afSJakub Kicinski
36408ca90afSJakub Kicinski down_read(&bpf_devs_lock);
36508ca90afSJakub Kicinski offload = env->prog->aux->offload;
36608ca90afSJakub Kicinski if (offload) {
36708ca90afSJakub Kicinski if (!offload->opt_failed && offload->offdev->ops->remove_insns)
36808ca90afSJakub Kicinski ret = offload->offdev->ops->remove_insns(env, off, cnt);
36908ca90afSJakub Kicinski offload->opt_failed |= ret;
37008ca90afSJakub Kicinski }
37108ca90afSJakub Kicinski up_read(&bpf_devs_lock);
37208ca90afSJakub Kicinski }
37308ca90afSJakub Kicinski
bpf_prog_dev_bound_destroy(struct bpf_prog * prog)3742b3486bcSStanislav Fomichev void bpf_prog_dev_bound_destroy(struct bpf_prog *prog)
375ab3f0063SJakub Kicinski {
3762b3486bcSStanislav Fomichev struct bpf_offload_netdev *ondev;
3772b3486bcSStanislav Fomichev struct net_device *netdev;
378ab3f0063SJakub Kicinski
3792b3486bcSStanislav Fomichev rtnl_lock();
380e0d3974aSJakub Kicinski down_write(&bpf_devs_lock);
3812b3486bcSStanislav Fomichev if (prog->aux->offload) {
3822b3486bcSStanislav Fomichev list_del_init(&prog->aux->offload->offloads);
3832b3486bcSStanislav Fomichev
3842b3486bcSStanislav Fomichev netdev = prog->aux->offload->netdev;
385ab3f0063SJakub Kicinski __bpf_prog_offload_destroy(prog);
3862b3486bcSStanislav Fomichev
3872b3486bcSStanislav Fomichev ondev = bpf_offload_find_netdev(netdev);
3882b3486bcSStanislav Fomichev if (!ondev->offdev && list_empty(&ondev->progs))
3892b3486bcSStanislav Fomichev __bpf_offload_dev_netdev_unregister(NULL, netdev);
3902b3486bcSStanislav Fomichev }
391e0d3974aSJakub Kicinski up_write(&bpf_devs_lock);
3922b3486bcSStanislav Fomichev rtnl_unlock();
393ab3f0063SJakub Kicinski }
394ab3f0063SJakub Kicinski
bpf_prog_offload_translate(struct bpf_prog * prog)395ab3f0063SJakub Kicinski static int bpf_prog_offload_translate(struct bpf_prog *prog)
396ab3f0063SJakub Kicinski {
397b07ade27SQuentin Monnet struct bpf_prog_offload *offload;
398b07ade27SQuentin Monnet int ret = -ENODEV;
399ab3f0063SJakub Kicinski
400b07ade27SQuentin Monnet down_read(&bpf_devs_lock);
401b07ade27SQuentin Monnet offload = prog->aux->offload;
402b07ade27SQuentin Monnet if (offload)
40316a8cb5cSQuentin Monnet ret = offload->offdev->ops->translate(prog);
404b07ade27SQuentin Monnet up_read(&bpf_devs_lock);
405ab3f0063SJakub Kicinski
406ab3f0063SJakub Kicinski return ret;
407ab3f0063SJakub Kicinski }
408ab3f0063SJakub Kicinski
bpf_prog_warn_on_exec(const void * ctx,const struct bpf_insn * insn)409ab3f0063SJakub Kicinski static unsigned int bpf_prog_warn_on_exec(const void *ctx,
410ab3f0063SJakub Kicinski const struct bpf_insn *insn)
411ab3f0063SJakub Kicinski {
412ab3f0063SJakub Kicinski WARN(1, "attempt to execute device eBPF program on the host!");
413ab3f0063SJakub Kicinski return 0;
414ab3f0063SJakub Kicinski }
415ab3f0063SJakub Kicinski
bpf_prog_offload_compile(struct bpf_prog * prog)416ab3f0063SJakub Kicinski int bpf_prog_offload_compile(struct bpf_prog *prog)
417ab3f0063SJakub Kicinski {
418ab3f0063SJakub Kicinski prog->bpf_func = bpf_prog_warn_on_exec;
419ab3f0063SJakub Kicinski
420ab3f0063SJakub Kicinski return bpf_prog_offload_translate(prog);
421ab3f0063SJakub Kicinski }
422ab3f0063SJakub Kicinski
423675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args {
424675fc275SJakub Kicinski struct bpf_prog *prog;
425675fc275SJakub Kicinski struct bpf_prog_info *info;
426675fc275SJakub Kicinski };
427675fc275SJakub Kicinski
bpf_prog_offload_info_fill_ns(void * private_data)428675fc275SJakub Kicinski static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data)
429675fc275SJakub Kicinski {
430675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args *args = private_data;
431675fc275SJakub Kicinski struct bpf_prog_aux *aux = args->prog->aux;
432675fc275SJakub Kicinski struct ns_common *ns;
433675fc275SJakub Kicinski struct net *net;
434675fc275SJakub Kicinski
435675fc275SJakub Kicinski rtnl_lock();
436675fc275SJakub Kicinski down_read(&bpf_devs_lock);
437675fc275SJakub Kicinski
438675fc275SJakub Kicinski if (aux->offload) {
439675fc275SJakub Kicinski args->info->ifindex = aux->offload->netdev->ifindex;
440675fc275SJakub Kicinski net = dev_net(aux->offload->netdev);
441675fc275SJakub Kicinski get_net(net);
442675fc275SJakub Kicinski ns = &net->ns;
443675fc275SJakub Kicinski } else {
444675fc275SJakub Kicinski args->info->ifindex = 0;
445675fc275SJakub Kicinski ns = NULL;
446675fc275SJakub Kicinski }
447675fc275SJakub Kicinski
448675fc275SJakub Kicinski up_read(&bpf_devs_lock);
449675fc275SJakub Kicinski rtnl_unlock();
450675fc275SJakub Kicinski
451675fc275SJakub Kicinski return ns;
452675fc275SJakub Kicinski }
453675fc275SJakub Kicinski
bpf_prog_offload_info_fill(struct bpf_prog_info * info,struct bpf_prog * prog)454675fc275SJakub Kicinski int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
455675fc275SJakub Kicinski struct bpf_prog *prog)
456675fc275SJakub Kicinski {
457675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args args = {
458675fc275SJakub Kicinski .prog = prog,
459675fc275SJakub Kicinski .info = info,
460675fc275SJakub Kicinski };
461fcfb126dSJiong Wang struct bpf_prog_aux *aux = prog->aux;
462675fc275SJakub Kicinski struct inode *ns_inode;
463675fc275SJakub Kicinski struct path ns_path;
464fcfb126dSJiong Wang char __user *uinsns;
465ce623f89SAleksa Sarai int res;
466fcfb126dSJiong Wang u32 ulen;
467675fc275SJakub Kicinski
468675fc275SJakub Kicinski res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args);
469ce623f89SAleksa Sarai if (res) {
470675fc275SJakub Kicinski if (!info->ifindex)
471675fc275SJakub Kicinski return -ENODEV;
472ce623f89SAleksa Sarai return res;
473675fc275SJakub Kicinski }
474675fc275SJakub Kicinski
475fcfb126dSJiong Wang down_read(&bpf_devs_lock);
476fcfb126dSJiong Wang
477fcfb126dSJiong Wang if (!aux->offload) {
478fcfb126dSJiong Wang up_read(&bpf_devs_lock);
479fcfb126dSJiong Wang return -ENODEV;
480fcfb126dSJiong Wang }
481fcfb126dSJiong Wang
482fcfb126dSJiong Wang ulen = info->jited_prog_len;
483fcfb126dSJiong Wang info->jited_prog_len = aux->offload->jited_len;
484e20d3a05SJohannes Krude if (info->jited_prog_len && ulen) {
485fcfb126dSJiong Wang uinsns = u64_to_user_ptr(info->jited_prog_insns);
486fcfb126dSJiong Wang ulen = min_t(u32, info->jited_prog_len, ulen);
487fcfb126dSJiong Wang if (copy_to_user(uinsns, aux->offload->jited_image, ulen)) {
488fcfb126dSJiong Wang up_read(&bpf_devs_lock);
489fcfb126dSJiong Wang return -EFAULT;
490fcfb126dSJiong Wang }
491fcfb126dSJiong Wang }
492fcfb126dSJiong Wang
493fcfb126dSJiong Wang up_read(&bpf_devs_lock);
494fcfb126dSJiong Wang
495675fc275SJakub Kicinski ns_inode = ns_path.dentry->d_inode;
496675fc275SJakub Kicinski info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev);
497675fc275SJakub Kicinski info->netns_ino = ns_inode->i_ino;
498675fc275SJakub Kicinski path_put(&ns_path);
499675fc275SJakub Kicinski
500675fc275SJakub Kicinski return 0;
501675fc275SJakub Kicinski }
502675fc275SJakub Kicinski
503ab3f0063SJakub Kicinski const struct bpf_prog_ops bpf_offload_prog_ops = {
504ab3f0063SJakub Kicinski };
505ab3f0063SJakub Kicinski
bpf_map_offload_map_alloc(union bpf_attr * attr)506a3884572SJakub Kicinski struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
507a3884572SJakub Kicinski {
508a3884572SJakub Kicinski struct net *net = current->nsproxy->net_ns;
5099fd7c555SJakub Kicinski struct bpf_offload_netdev *ondev;
510a3884572SJakub Kicinski struct bpf_offloaded_map *offmap;
511a3884572SJakub Kicinski int err;
512a3884572SJakub Kicinski
513a3884572SJakub Kicinski if (!capable(CAP_SYS_ADMIN))
514a3884572SJakub Kicinski return ERR_PTR(-EPERM);
5157a0ef693SJakub Kicinski if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
5167a0ef693SJakub Kicinski attr->map_type != BPF_MAP_TYPE_HASH)
517a3884572SJakub Kicinski return ERR_PTR(-EINVAL);
518a3884572SJakub Kicinski
51973cf09a3SYafang Shao offmap = bpf_map_area_alloc(sizeof(*offmap), NUMA_NO_NODE);
520a3884572SJakub Kicinski if (!offmap)
521a3884572SJakub Kicinski return ERR_PTR(-ENOMEM);
522a3884572SJakub Kicinski
523a3884572SJakub Kicinski bpf_map_init_from_attr(&offmap->map, attr);
524a3884572SJakub Kicinski
525a3884572SJakub Kicinski rtnl_lock();
526a3884572SJakub Kicinski down_write(&bpf_devs_lock);
527a3884572SJakub Kicinski offmap->netdev = __dev_get_by_index(net, attr->map_ifindex);
528a3884572SJakub Kicinski err = bpf_dev_offload_check(offmap->netdev);
529a3884572SJakub Kicinski if (err)
530a3884572SJakub Kicinski goto err_unlock;
531a3884572SJakub Kicinski
5329fd7c555SJakub Kicinski ondev = bpf_offload_find_netdev(offmap->netdev);
5339fd7c555SJakub Kicinski if (!ondev) {
5349fd7c555SJakub Kicinski err = -EINVAL;
5359fd7c555SJakub Kicinski goto err_unlock;
5369fd7c555SJakub Kicinski }
5379fd7c555SJakub Kicinski
538a3884572SJakub Kicinski err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC);
539a3884572SJakub Kicinski if (err)
540a3884572SJakub Kicinski goto err_unlock;
541a3884572SJakub Kicinski
5429fd7c555SJakub Kicinski list_add_tail(&offmap->offloads, &ondev->maps);
543a3884572SJakub Kicinski up_write(&bpf_devs_lock);
544a3884572SJakub Kicinski rtnl_unlock();
545a3884572SJakub Kicinski
546a3884572SJakub Kicinski return &offmap->map;
547a3884572SJakub Kicinski
548a3884572SJakub Kicinski err_unlock:
549a3884572SJakub Kicinski up_write(&bpf_devs_lock);
550a3884572SJakub Kicinski rtnl_unlock();
55173cf09a3SYafang Shao bpf_map_area_free(offmap);
552a3884572SJakub Kicinski return ERR_PTR(err);
553a3884572SJakub Kicinski }
554a3884572SJakub Kicinski
bpf_map_offload_map_free(struct bpf_map * map)555a3884572SJakub Kicinski void bpf_map_offload_map_free(struct bpf_map *map)
556a3884572SJakub Kicinski {
557a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map);
558a3884572SJakub Kicinski
559a3884572SJakub Kicinski rtnl_lock();
560a3884572SJakub Kicinski down_write(&bpf_devs_lock);
561a3884572SJakub Kicinski if (offmap->netdev)
562a3884572SJakub Kicinski __bpf_map_offload_destroy(offmap);
563a3884572SJakub Kicinski up_write(&bpf_devs_lock);
564a3884572SJakub Kicinski rtnl_unlock();
565a3884572SJakub Kicinski
56673cf09a3SYafang Shao bpf_map_area_free(offmap);
567a3884572SJakub Kicinski }
568a3884572SJakub Kicinski
bpf_map_offload_map_mem_usage(const struct bpf_map * map)5699629363cSYafang Shao u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map)
5709629363cSYafang Shao {
5719629363cSYafang Shao /* The memory dynamically allocated in netdev dev_ops is not counted */
5729629363cSYafang Shao return sizeof(struct bpf_offloaded_map);
5739629363cSYafang Shao }
5749629363cSYafang Shao
bpf_map_offload_lookup_elem(struct bpf_map * map,void * key,void * value)575a3884572SJakub Kicinski int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value)
576a3884572SJakub Kicinski {
577a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map);
578a3884572SJakub Kicinski int ret = -ENODEV;
579a3884572SJakub Kicinski
580a3884572SJakub Kicinski down_read(&bpf_devs_lock);
581a3884572SJakub Kicinski if (offmap->netdev)
582a3884572SJakub Kicinski ret = offmap->dev_ops->map_lookup_elem(offmap, key, value);
583a3884572SJakub Kicinski up_read(&bpf_devs_lock);
584a3884572SJakub Kicinski
585a3884572SJakub Kicinski return ret;
586a3884572SJakub Kicinski }
587a3884572SJakub Kicinski
bpf_map_offload_update_elem(struct bpf_map * map,void * key,void * value,u64 flags)588a3884572SJakub Kicinski int bpf_map_offload_update_elem(struct bpf_map *map,
589a3884572SJakub Kicinski void *key, void *value, u64 flags)
590a3884572SJakub Kicinski {
591a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map);
592a3884572SJakub Kicinski int ret = -ENODEV;
593a3884572SJakub Kicinski
594a3884572SJakub Kicinski if (unlikely(flags > BPF_EXIST))
595a3884572SJakub Kicinski return -EINVAL;
596a3884572SJakub Kicinski
597a3884572SJakub Kicinski down_read(&bpf_devs_lock);
598a3884572SJakub Kicinski if (offmap->netdev)
599a3884572SJakub Kicinski ret = offmap->dev_ops->map_update_elem(offmap, key, value,
600a3884572SJakub Kicinski flags);
601a3884572SJakub Kicinski up_read(&bpf_devs_lock);
602a3884572SJakub Kicinski
603a3884572SJakub Kicinski return ret;
604a3884572SJakub Kicinski }
605a3884572SJakub Kicinski
bpf_map_offload_delete_elem(struct bpf_map * map,void * key)606a3884572SJakub Kicinski int bpf_map_offload_delete_elem(struct bpf_map *map, void *key)
607a3884572SJakub Kicinski {
608a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map);
609a3884572SJakub Kicinski int ret = -ENODEV;
610a3884572SJakub Kicinski
611a3884572SJakub Kicinski down_read(&bpf_devs_lock);
612a3884572SJakub Kicinski if (offmap->netdev)
613a3884572SJakub Kicinski ret = offmap->dev_ops->map_delete_elem(offmap, key);
614a3884572SJakub Kicinski up_read(&bpf_devs_lock);
615a3884572SJakub Kicinski
616a3884572SJakub Kicinski return ret;
617a3884572SJakub Kicinski }
618a3884572SJakub Kicinski
bpf_map_offload_get_next_key(struct bpf_map * map,void * key,void * next_key)619a3884572SJakub Kicinski int bpf_map_offload_get_next_key(struct bpf_map *map, void *key, void *next_key)
620a3884572SJakub Kicinski {
621a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map);
622a3884572SJakub Kicinski int ret = -ENODEV;
623a3884572SJakub Kicinski
624a3884572SJakub Kicinski down_read(&bpf_devs_lock);
625a3884572SJakub Kicinski if (offmap->netdev)
626a3884572SJakub Kicinski ret = offmap->dev_ops->map_get_next_key(offmap, key, next_key);
627a3884572SJakub Kicinski up_read(&bpf_devs_lock);
628a3884572SJakub Kicinski
629a3884572SJakub Kicinski return ret;
630a3884572SJakub Kicinski }
631a3884572SJakub Kicinski
63252775b33SJakub Kicinski struct ns_get_path_bpf_map_args {
63352775b33SJakub Kicinski struct bpf_offloaded_map *offmap;
63452775b33SJakub Kicinski struct bpf_map_info *info;
63552775b33SJakub Kicinski };
63652775b33SJakub Kicinski
bpf_map_offload_info_fill_ns(void * private_data)63752775b33SJakub Kicinski static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data)
63852775b33SJakub Kicinski {
63952775b33SJakub Kicinski struct ns_get_path_bpf_map_args *args = private_data;
64052775b33SJakub Kicinski struct ns_common *ns;
64152775b33SJakub Kicinski struct net *net;
64252775b33SJakub Kicinski
64352775b33SJakub Kicinski rtnl_lock();
64452775b33SJakub Kicinski down_read(&bpf_devs_lock);
64552775b33SJakub Kicinski
64652775b33SJakub Kicinski if (args->offmap->netdev) {
64752775b33SJakub Kicinski args->info->ifindex = args->offmap->netdev->ifindex;
64852775b33SJakub Kicinski net = dev_net(args->offmap->netdev);
64952775b33SJakub Kicinski get_net(net);
65052775b33SJakub Kicinski ns = &net->ns;
65152775b33SJakub Kicinski } else {
65252775b33SJakub Kicinski args->info->ifindex = 0;
65352775b33SJakub Kicinski ns = NULL;
65452775b33SJakub Kicinski }
65552775b33SJakub Kicinski
65652775b33SJakub Kicinski up_read(&bpf_devs_lock);
65752775b33SJakub Kicinski rtnl_unlock();
65852775b33SJakub Kicinski
65952775b33SJakub Kicinski return ns;
66052775b33SJakub Kicinski }
66152775b33SJakub Kicinski
bpf_map_offload_info_fill(struct bpf_map_info * info,struct bpf_map * map)66252775b33SJakub Kicinski int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map)
66352775b33SJakub Kicinski {
66452775b33SJakub Kicinski struct ns_get_path_bpf_map_args args = {
66552775b33SJakub Kicinski .offmap = map_to_offmap(map),
66652775b33SJakub Kicinski .info = info,
66752775b33SJakub Kicinski };
66852775b33SJakub Kicinski struct inode *ns_inode;
66952775b33SJakub Kicinski struct path ns_path;
670ce623f89SAleksa Sarai int res;
67152775b33SJakub Kicinski
67252775b33SJakub Kicinski res = ns_get_path_cb(&ns_path, bpf_map_offload_info_fill_ns, &args);
673ce623f89SAleksa Sarai if (res) {
67452775b33SJakub Kicinski if (!info->ifindex)
67552775b33SJakub Kicinski return -ENODEV;
676ce623f89SAleksa Sarai return res;
67752775b33SJakub Kicinski }
67852775b33SJakub Kicinski
67952775b33SJakub Kicinski ns_inode = ns_path.dentry->d_inode;
68052775b33SJakub Kicinski info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev);
68152775b33SJakub Kicinski info->netns_ino = ns_inode->i_ino;
68252775b33SJakub Kicinski path_put(&ns_path);
68352775b33SJakub Kicinski
68452775b33SJakub Kicinski return 0;
68552775b33SJakub Kicinski }
68652775b33SJakub Kicinski
__bpf_offload_dev_match(struct bpf_prog * prog,struct net_device * netdev)687fd4f227dSJakub Kicinski static bool __bpf_offload_dev_match(struct bpf_prog *prog,
688fd4f227dSJakub Kicinski struct net_device *netdev)
689a3884572SJakub Kicinski {
690fd4f227dSJakub Kicinski struct bpf_offload_netdev *ondev1, *ondev2;
691a3884572SJakub Kicinski struct bpf_prog_offload *offload;
692a3884572SJakub Kicinski
6930cd3cbedSJakub Kicinski if (!bpf_prog_is_dev_bound(prog->aux))
694a3884572SJakub Kicinski return false;
695fd4f227dSJakub Kicinski
696fd4f227dSJakub Kicinski offload = prog->aux->offload;
697fd4f227dSJakub Kicinski if (!offload)
698fd4f227dSJakub Kicinski return false;
699fd4f227dSJakub Kicinski if (offload->netdev == netdev)
700fd4f227dSJakub Kicinski return true;
701fd4f227dSJakub Kicinski
702fd4f227dSJakub Kicinski ondev1 = bpf_offload_find_netdev(offload->netdev);
703fd4f227dSJakub Kicinski ondev2 = bpf_offload_find_netdev(netdev);
704fd4f227dSJakub Kicinski
705fd4f227dSJakub Kicinski return ondev1 && ondev2 && ondev1->offdev == ondev2->offdev;
706fd4f227dSJakub Kicinski }
707fd4f227dSJakub Kicinski
bpf_offload_dev_match(struct bpf_prog * prog,struct net_device * netdev)708fd4f227dSJakub Kicinski bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev)
709fd4f227dSJakub Kicinski {
710fd4f227dSJakub Kicinski bool ret;
711a3884572SJakub Kicinski
712a3884572SJakub Kicinski down_read(&bpf_devs_lock);
713fd4f227dSJakub Kicinski ret = __bpf_offload_dev_match(prog, netdev);
714fd4f227dSJakub Kicinski up_read(&bpf_devs_lock);
715fd4f227dSJakub Kicinski
716fd4f227dSJakub Kicinski return ret;
717fd4f227dSJakub Kicinski }
718fd4f227dSJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_match);
719fd4f227dSJakub Kicinski
bpf_prog_dev_bound_match(const struct bpf_prog * lhs,const struct bpf_prog * rhs)720fd7c211dSToke Høiland-Jørgensen bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs)
721fd7c211dSToke Høiland-Jørgensen {
722fd7c211dSToke Høiland-Jørgensen bool ret;
723fd7c211dSToke Høiland-Jørgensen
724fd7c211dSToke Høiland-Jørgensen if (bpf_prog_is_offloaded(lhs->aux) != bpf_prog_is_offloaded(rhs->aux))
725fd7c211dSToke Høiland-Jørgensen return false;
726fd7c211dSToke Høiland-Jørgensen
727fd7c211dSToke Høiland-Jørgensen down_read(&bpf_devs_lock);
728fd7c211dSToke Høiland-Jørgensen ret = lhs->aux->offload && rhs->aux->offload &&
729fd7c211dSToke Høiland-Jørgensen lhs->aux->offload->netdev &&
730fd7c211dSToke Høiland-Jørgensen lhs->aux->offload->netdev == rhs->aux->offload->netdev;
731fd7c211dSToke Høiland-Jørgensen up_read(&bpf_devs_lock);
732fd7c211dSToke Høiland-Jørgensen
733fd7c211dSToke Høiland-Jørgensen return ret;
734fd7c211dSToke Høiland-Jørgensen }
735fd7c211dSToke Høiland-Jørgensen
bpf_offload_prog_map_match(struct bpf_prog * prog,struct bpf_map * map)736fd4f227dSJakub Kicinski bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map)
737fd4f227dSJakub Kicinski {
738fd4f227dSJakub Kicinski struct bpf_offloaded_map *offmap;
739fd4f227dSJakub Kicinski bool ret;
740fd4f227dSJakub Kicinski
7419d03ebc7SStanislav Fomichev if (!bpf_map_is_offloaded(map))
742fd4f227dSJakub Kicinski return bpf_map_offload_neutral(map);
743a3884572SJakub Kicinski offmap = map_to_offmap(map);
744a3884572SJakub Kicinski
745fd4f227dSJakub Kicinski down_read(&bpf_devs_lock);
746fd4f227dSJakub Kicinski ret = __bpf_offload_dev_match(prog, offmap->netdev);
747a3884572SJakub Kicinski up_read(&bpf_devs_lock);
748a3884572SJakub Kicinski
749a3884572SJakub Kicinski return ret;
750a3884572SJakub Kicinski }
751a3884572SJakub Kicinski
bpf_offload_dev_netdev_register(struct bpf_offload_dev * offdev,struct net_device * netdev)752602144c2SJakub Kicinski int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
753602144c2SJakub Kicinski struct net_device *netdev)
754a3884572SJakub Kicinski {
7559fd7c555SJakub Kicinski int err;
756a3884572SJakub Kicinski
7579fd7c555SJakub Kicinski down_write(&bpf_devs_lock);
7582b3486bcSStanislav Fomichev err = __bpf_offload_dev_netdev_register(offdev, netdev);
7599fd7c555SJakub Kicinski up_write(&bpf_devs_lock);
7609fd7c555SJakub Kicinski return err;
761a3884572SJakub Kicinski }
7629fd7c555SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register);
763a3884572SJakub Kicinski
bpf_offload_dev_netdev_unregister(struct bpf_offload_dev * offdev,struct net_device * netdev)764602144c2SJakub Kicinski void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
765602144c2SJakub Kicinski struct net_device *netdev)
766ab3f0063SJakub Kicinski {
767e0d3974aSJakub Kicinski down_write(&bpf_devs_lock);
76889bbc53aSStanislav Fomichev __bpf_offload_dev_netdev_unregister(offdev, netdev);
769e0d3974aSJakub Kicinski up_write(&bpf_devs_lock);
770ab3f0063SJakub Kicinski }
7719fd7c555SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister);
772602144c2SJakub Kicinski
7731385d755SQuentin Monnet struct bpf_offload_dev *
bpf_offload_dev_create(const struct bpf_prog_offload_ops * ops,void * priv)774dd27c2e3SJakub Kicinski bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
775602144c2SJakub Kicinski {
776602144c2SJakub Kicinski struct bpf_offload_dev *offdev;
777602144c2SJakub Kicinski
778602144c2SJakub Kicinski offdev = kzalloc(sizeof(*offdev), GFP_KERNEL);
779602144c2SJakub Kicinski if (!offdev)
780602144c2SJakub Kicinski return ERR_PTR(-ENOMEM);
781602144c2SJakub Kicinski
7821385d755SQuentin Monnet offdev->ops = ops;
783dd27c2e3SJakub Kicinski offdev->priv = priv;
784602144c2SJakub Kicinski INIT_LIST_HEAD(&offdev->netdevs);
785602144c2SJakub Kicinski
786602144c2SJakub Kicinski return offdev;
787602144c2SJakub Kicinski }
788602144c2SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_create);
789602144c2SJakub Kicinski
bpf_offload_dev_destroy(struct bpf_offload_dev * offdev)790602144c2SJakub Kicinski void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev)
791602144c2SJakub Kicinski {
792602144c2SJakub Kicinski WARN_ON(!list_empty(&offdev->netdevs));
793602144c2SJakub Kicinski kfree(offdev);
794602144c2SJakub Kicinski }
795602144c2SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy);
796dd27c2e3SJakub Kicinski
bpf_offload_dev_priv(struct bpf_offload_dev * offdev)797dd27c2e3SJakub Kicinski void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev)
798dd27c2e3SJakub Kicinski {
799dd27c2e3SJakub Kicinski return offdev->priv;
800dd27c2e3SJakub Kicinski }
801dd27c2e3SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_priv);
802f1fc43d0SStanislav Fomichev
bpf_dev_bound_netdev_unregister(struct net_device * dev)8032b3486bcSStanislav Fomichev void bpf_dev_bound_netdev_unregister(struct net_device *dev)
8042b3486bcSStanislav Fomichev {
8052b3486bcSStanislav Fomichev struct bpf_offload_netdev *ondev;
8062b3486bcSStanislav Fomichev
8072b3486bcSStanislav Fomichev ASSERT_RTNL();
8082b3486bcSStanislav Fomichev
8092b3486bcSStanislav Fomichev down_write(&bpf_devs_lock);
8102b3486bcSStanislav Fomichev ondev = bpf_offload_find_netdev(dev);
8112b3486bcSStanislav Fomichev if (ondev && !ondev->offdev)
8122b3486bcSStanislav Fomichev __bpf_offload_dev_netdev_unregister(NULL, ondev->netdev);
8132b3486bcSStanislav Fomichev up_write(&bpf_devs_lock);
8142b3486bcSStanislav Fomichev }
8152b3486bcSStanislav Fomichev
bpf_dev_bound_kfunc_check(struct bpf_verifier_log * log,struct bpf_prog_aux * prog_aux)8163d76a4d3SStanislav Fomichev int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log,
8173d76a4d3SStanislav Fomichev struct bpf_prog_aux *prog_aux)
8183d76a4d3SStanislav Fomichev {
8193d76a4d3SStanislav Fomichev if (!bpf_prog_is_dev_bound(prog_aux)) {
8203d76a4d3SStanislav Fomichev bpf_log(log, "metadata kfuncs require device-bound program\n");
8213d76a4d3SStanislav Fomichev return -EINVAL;
8223d76a4d3SStanislav Fomichev }
8233d76a4d3SStanislav Fomichev
8243d76a4d3SStanislav Fomichev if (bpf_prog_is_offloaded(prog_aux)) {
8253d76a4d3SStanislav Fomichev bpf_log(log, "metadata kfuncs can't be offloaded\n");
8263d76a4d3SStanislav Fomichev return -EINVAL;
8273d76a4d3SStanislav Fomichev }
8283d76a4d3SStanislav Fomichev
8293d76a4d3SStanislav Fomichev return 0;
8303d76a4d3SStanislav Fomichev }
8313d76a4d3SStanislav Fomichev
bpf_dev_bound_resolve_kfunc(struct bpf_prog * prog,u32 func_id)8323d76a4d3SStanislav Fomichev void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
8333d76a4d3SStanislav Fomichev {
8343d76a4d3SStanislav Fomichev const struct xdp_metadata_ops *ops;
8353d76a4d3SStanislav Fomichev void *p = NULL;
8363d76a4d3SStanislav Fomichev
8373d76a4d3SStanislav Fomichev /* We don't hold bpf_devs_lock while resolving several
8383d76a4d3SStanislav Fomichev * kfuncs and can race with the unregister_netdevice().
8393d76a4d3SStanislav Fomichev * We rely on bpf_dev_bound_match() check at attach
8403d76a4d3SStanislav Fomichev * to render this program unusable.
8413d76a4d3SStanislav Fomichev */
8423d76a4d3SStanislav Fomichev down_read(&bpf_devs_lock);
8433d76a4d3SStanislav Fomichev if (!prog->aux->offload)
8443d76a4d3SStanislav Fomichev goto out;
8453d76a4d3SStanislav Fomichev
8463d76a4d3SStanislav Fomichev ops = prog->aux->offload->netdev->xdp_metadata_ops;
8473d76a4d3SStanislav Fomichev if (!ops)
8483d76a4d3SStanislav Fomichev goto out;
8493d76a4d3SStanislav Fomichev
8503d76a4d3SStanislav Fomichev if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP))
8513d76a4d3SStanislav Fomichev p = ops->xmo_rx_timestamp;
8523d76a4d3SStanislav Fomichev else if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH))
8533d76a4d3SStanislav Fomichev p = ops->xmo_rx_hash;
8543d76a4d3SStanislav Fomichev out:
8553d76a4d3SStanislav Fomichev up_read(&bpf_devs_lock);
8563d76a4d3SStanislav Fomichev
8573d76a4d3SStanislav Fomichev return p;
8583d76a4d3SStanislav Fomichev }
8593d76a4d3SStanislav Fomichev
bpf_offload_init(void)860f1fc43d0SStanislav Fomichev static int __init bpf_offload_init(void)
861f1fc43d0SStanislav Fomichev {
862f1fc43d0SStanislav Fomichev return rhashtable_init(&offdevs, &offdevs_params);
863f1fc43d0SStanislav Fomichev }
864f1fc43d0SStanislav Fomichev
865e1505c1cSJakub Kicinski core_initcall(bpf_offload_init);
866