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> 28*680ee045SJakub 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 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 * 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 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 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 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 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 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 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); 2029fd7c555SJakub Kicinski if (!ondev) { 203fd7c211dSToke Høiland-Jørgensen if (bpf_prog_is_offloaded(prog->aux)) { 2045bc2d55cSJakub Kicinski err = -EINVAL; 205fd7c211dSToke Høiland-Jørgensen goto err_free; 206fd7c211dSToke Høiland-Jørgensen } 207fd7c211dSToke Høiland-Jørgensen 208fd7c211dSToke Høiland-Jørgensen /* When only binding to the device, explicitly 209fd7c211dSToke Høiland-Jørgensen * create an entry in the hashtable. 210fd7c211dSToke Høiland-Jørgensen */ 211fd7c211dSToke Høiland-Jørgensen err = __bpf_offload_dev_netdev_register(NULL, offload->netdev); 212fd7c211dSToke Høiland-Jørgensen if (err) 213fd7c211dSToke Høiland-Jørgensen goto err_free; 214fd7c211dSToke Høiland-Jørgensen ondev = bpf_offload_find_netdev(offload->netdev); 2155bc2d55cSJakub Kicinski } 216341b3e7bSQuentin Monnet offload->offdev = ondev->offdev; 217ab3f0063SJakub Kicinski prog->aux->offload = offload; 2189fd7c555SJakub Kicinski list_add_tail(&offload->offloads, &ondev->progs); 219ab3f0063SJakub Kicinski 220ab3f0063SJakub Kicinski return 0; 221fd7c211dSToke Høiland-Jørgensen err_free: 222e0d3974aSJakub Kicinski kfree(offload); 2235bc2d55cSJakub Kicinski return err; 224ab3f0063SJakub Kicinski } 225ab3f0063SJakub Kicinski 226fd7c211dSToke Høiland-Jørgensen int bpf_prog_dev_bound_init(struct bpf_prog *prog, union bpf_attr *attr) 227fd7c211dSToke Høiland-Jørgensen { 228fd7c211dSToke Høiland-Jørgensen struct net_device *netdev; 229fd7c211dSToke Høiland-Jørgensen int err; 230fd7c211dSToke Høiland-Jørgensen 231ab3f0063SJakub Kicinski if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS && 232ab3f0063SJakub Kicinski attr->prog_type != BPF_PROG_TYPE_XDP) 233ab3f0063SJakub Kicinski return -EINVAL; 234ab3f0063SJakub Kicinski 2352b3486bcSStanislav Fomichev if (attr->prog_flags & ~BPF_F_XDP_DEV_BOUND_ONLY) 2362b3486bcSStanislav Fomichev return -EINVAL; 2372b3486bcSStanislav Fomichev 2382b3486bcSStanislav Fomichev if (attr->prog_type == BPF_PROG_TYPE_SCHED_CLS && 2392b3486bcSStanislav Fomichev attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY) 240ab3f0063SJakub Kicinski return -EINVAL; 241ab3f0063SJakub Kicinski 242fd7c211dSToke Høiland-Jørgensen netdev = dev_get_by_index(current->nsproxy->net_ns, attr->prog_ifindex); 243fd7c211dSToke Høiland-Jørgensen if (!netdev) 244fd7c211dSToke Høiland-Jørgensen return -EINVAL; 245ab3f0063SJakub Kicinski 246fd7c211dSToke Høiland-Jørgensen err = bpf_dev_offload_check(netdev); 247ab3f0063SJakub Kicinski if (err) 248fd7c211dSToke Høiland-Jørgensen goto out; 249ab3f0063SJakub Kicinski 2502b3486bcSStanislav Fomichev prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY); 2512b3486bcSStanislav Fomichev 252ab3f0063SJakub Kicinski down_write(&bpf_devs_lock); 253fd7c211dSToke Høiland-Jørgensen err = __bpf_prog_dev_bound_init(prog, netdev); 254ab3f0063SJakub Kicinski up_write(&bpf_devs_lock); 255ab3f0063SJakub Kicinski 256fd7c211dSToke Høiland-Jørgensen out: 257fd7c211dSToke Høiland-Jørgensen dev_put(netdev); 258fd7c211dSToke Høiland-Jørgensen return err; 259fd7c211dSToke Høiland-Jørgensen } 260fd7c211dSToke Høiland-Jørgensen 261fd7c211dSToke Høiland-Jørgensen int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog, struct bpf_prog *old_prog) 262fd7c211dSToke Høiland-Jørgensen { 263fd7c211dSToke Høiland-Jørgensen int err; 264fd7c211dSToke Høiland-Jørgensen 265fd7c211dSToke Høiland-Jørgensen if (!bpf_prog_is_dev_bound(old_prog->aux)) 266ab3f0063SJakub Kicinski return 0; 267fd7c211dSToke Høiland-Jørgensen 268fd7c211dSToke Høiland-Jørgensen if (bpf_prog_is_offloaded(old_prog->aux)) 269fd7c211dSToke Høiland-Jørgensen return -EINVAL; 270fd7c211dSToke Høiland-Jørgensen 271fd7c211dSToke Høiland-Jørgensen new_prog->aux->dev_bound = old_prog->aux->dev_bound; 272fd7c211dSToke Høiland-Jørgensen new_prog->aux->offload_requested = old_prog->aux->offload_requested; 273fd7c211dSToke Høiland-Jørgensen 274fd7c211dSToke Høiland-Jørgensen down_write(&bpf_devs_lock); 275fd7c211dSToke Høiland-Jørgensen if (!old_prog->aux->offload) { 276fd7c211dSToke Høiland-Jørgensen err = -EINVAL; 277fd7c211dSToke Høiland-Jørgensen goto out; 278fd7c211dSToke Høiland-Jørgensen } 279fd7c211dSToke Høiland-Jørgensen 280fd7c211dSToke Høiland-Jørgensen err = __bpf_prog_dev_bound_init(new_prog, old_prog->aux->offload->netdev); 281fd7c211dSToke Høiland-Jørgensen 282fd7c211dSToke Høiland-Jørgensen out: 283ab3f0063SJakub Kicinski up_write(&bpf_devs_lock); 284ab3f0063SJakub Kicinski return err; 285ab3f0063SJakub Kicinski } 286ab3f0063SJakub Kicinski 287a40a2632SQuentin Monnet int bpf_prog_offload_verifier_prep(struct bpf_prog *prog) 288ab3f0063SJakub Kicinski { 28900db12c3SQuentin Monnet struct bpf_prog_offload *offload; 29000db12c3SQuentin Monnet int ret = -ENODEV; 291ab3f0063SJakub Kicinski 29200db12c3SQuentin Monnet down_read(&bpf_devs_lock); 293a40a2632SQuentin Monnet offload = prog->aux->offload; 294592ee43fSColin Ian King if (offload) { 29516a8cb5cSQuentin Monnet ret = offload->offdev->ops->prepare(prog); 29600db12c3SQuentin Monnet offload->dev_state = !ret; 297592ee43fSColin Ian King } 29800db12c3SQuentin Monnet up_read(&bpf_devs_lock); 299ab3f0063SJakub Kicinski 30000db12c3SQuentin Monnet return ret; 301ab3f0063SJakub Kicinski } 302ab3f0063SJakub Kicinski 303cae1927cSJakub Kicinski int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, 304cae1927cSJakub Kicinski int insn_idx, int prev_insn_idx) 305cae1927cSJakub Kicinski { 3060a9c1991SJakub Kicinski struct bpf_prog_offload *offload; 307cae1927cSJakub Kicinski int ret = -ENODEV; 308cae1927cSJakub Kicinski 309cae1927cSJakub Kicinski down_read(&bpf_devs_lock); 310cae1927cSJakub Kicinski offload = env->prog->aux->offload; 311ce3b9db4SJakub Kicinski if (offload) 312341b3e7bSQuentin Monnet ret = offload->offdev->ops->insn_hook(env, insn_idx, 313341b3e7bSQuentin Monnet prev_insn_idx); 314cae1927cSJakub Kicinski up_read(&bpf_devs_lock); 315cae1927cSJakub Kicinski 316cae1927cSJakub Kicinski return ret; 317cae1927cSJakub Kicinski } 318cae1927cSJakub Kicinski 319c941ce9cSQuentin Monnet int bpf_prog_offload_finalize(struct bpf_verifier_env *env) 320c941ce9cSQuentin Monnet { 321c941ce9cSQuentin Monnet struct bpf_prog_offload *offload; 322c941ce9cSQuentin Monnet int ret = -ENODEV; 323c941ce9cSQuentin Monnet 324c941ce9cSQuentin Monnet down_read(&bpf_devs_lock); 325c941ce9cSQuentin Monnet offload = env->prog->aux->offload; 326c941ce9cSQuentin Monnet if (offload) { 3276dc18fa6SQuentin Monnet if (offload->offdev->ops->finalize) 3286dc18fa6SQuentin Monnet ret = offload->offdev->ops->finalize(env); 329c941ce9cSQuentin Monnet else 330c941ce9cSQuentin Monnet ret = 0; 331c941ce9cSQuentin Monnet } 332c941ce9cSQuentin Monnet up_read(&bpf_devs_lock); 333c941ce9cSQuentin Monnet 334c941ce9cSQuentin Monnet return ret; 335c941ce9cSQuentin Monnet } 336c941ce9cSQuentin Monnet 33708ca90afSJakub Kicinski void 33808ca90afSJakub Kicinski bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, 33908ca90afSJakub Kicinski struct bpf_insn *insn) 34008ca90afSJakub Kicinski { 34108ca90afSJakub Kicinski const struct bpf_prog_offload_ops *ops; 34208ca90afSJakub Kicinski struct bpf_prog_offload *offload; 34308ca90afSJakub Kicinski int ret = -EOPNOTSUPP; 34408ca90afSJakub Kicinski 34508ca90afSJakub Kicinski down_read(&bpf_devs_lock); 34608ca90afSJakub Kicinski offload = env->prog->aux->offload; 34708ca90afSJakub Kicinski if (offload) { 34808ca90afSJakub Kicinski ops = offload->offdev->ops; 34908ca90afSJakub Kicinski if (!offload->opt_failed && ops->replace_insn) 35008ca90afSJakub Kicinski ret = ops->replace_insn(env, off, insn); 35108ca90afSJakub Kicinski offload->opt_failed |= ret; 35208ca90afSJakub Kicinski } 35308ca90afSJakub Kicinski up_read(&bpf_devs_lock); 35408ca90afSJakub Kicinski } 35508ca90afSJakub Kicinski 35608ca90afSJakub Kicinski void 35708ca90afSJakub Kicinski bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) 35808ca90afSJakub Kicinski { 35908ca90afSJakub Kicinski struct bpf_prog_offload *offload; 36008ca90afSJakub Kicinski int ret = -EOPNOTSUPP; 36108ca90afSJakub Kicinski 36208ca90afSJakub Kicinski down_read(&bpf_devs_lock); 36308ca90afSJakub Kicinski offload = env->prog->aux->offload; 36408ca90afSJakub Kicinski if (offload) { 36508ca90afSJakub Kicinski if (!offload->opt_failed && offload->offdev->ops->remove_insns) 36608ca90afSJakub Kicinski ret = offload->offdev->ops->remove_insns(env, off, cnt); 36708ca90afSJakub Kicinski offload->opt_failed |= ret; 36808ca90afSJakub Kicinski } 36908ca90afSJakub Kicinski up_read(&bpf_devs_lock); 37008ca90afSJakub Kicinski } 37108ca90afSJakub Kicinski 3722b3486bcSStanislav Fomichev void bpf_prog_dev_bound_destroy(struct bpf_prog *prog) 373ab3f0063SJakub Kicinski { 3742b3486bcSStanislav Fomichev struct bpf_offload_netdev *ondev; 3752b3486bcSStanislav Fomichev struct net_device *netdev; 376ab3f0063SJakub Kicinski 3772b3486bcSStanislav Fomichev rtnl_lock(); 378e0d3974aSJakub Kicinski down_write(&bpf_devs_lock); 3792b3486bcSStanislav Fomichev if (prog->aux->offload) { 3802b3486bcSStanislav Fomichev list_del_init(&prog->aux->offload->offloads); 3812b3486bcSStanislav Fomichev 3822b3486bcSStanislav Fomichev netdev = prog->aux->offload->netdev; 383ab3f0063SJakub Kicinski __bpf_prog_offload_destroy(prog); 3842b3486bcSStanislav Fomichev 3852b3486bcSStanislav Fomichev ondev = bpf_offload_find_netdev(netdev); 3862b3486bcSStanislav Fomichev if (!ondev->offdev && list_empty(&ondev->progs)) 3872b3486bcSStanislav Fomichev __bpf_offload_dev_netdev_unregister(NULL, netdev); 3882b3486bcSStanislav Fomichev } 389e0d3974aSJakub Kicinski up_write(&bpf_devs_lock); 3902b3486bcSStanislav Fomichev rtnl_unlock(); 391ab3f0063SJakub Kicinski } 392ab3f0063SJakub Kicinski 393ab3f0063SJakub Kicinski static int bpf_prog_offload_translate(struct bpf_prog *prog) 394ab3f0063SJakub Kicinski { 395b07ade27SQuentin Monnet struct bpf_prog_offload *offload; 396b07ade27SQuentin Monnet int ret = -ENODEV; 397ab3f0063SJakub Kicinski 398b07ade27SQuentin Monnet down_read(&bpf_devs_lock); 399b07ade27SQuentin Monnet offload = prog->aux->offload; 400b07ade27SQuentin Monnet if (offload) 40116a8cb5cSQuentin Monnet ret = offload->offdev->ops->translate(prog); 402b07ade27SQuentin Monnet up_read(&bpf_devs_lock); 403ab3f0063SJakub Kicinski 404ab3f0063SJakub Kicinski return ret; 405ab3f0063SJakub Kicinski } 406ab3f0063SJakub Kicinski 407ab3f0063SJakub Kicinski static unsigned int bpf_prog_warn_on_exec(const void *ctx, 408ab3f0063SJakub Kicinski const struct bpf_insn *insn) 409ab3f0063SJakub Kicinski { 410ab3f0063SJakub Kicinski WARN(1, "attempt to execute device eBPF program on the host!"); 411ab3f0063SJakub Kicinski return 0; 412ab3f0063SJakub Kicinski } 413ab3f0063SJakub Kicinski 414ab3f0063SJakub Kicinski int bpf_prog_offload_compile(struct bpf_prog *prog) 415ab3f0063SJakub Kicinski { 416ab3f0063SJakub Kicinski prog->bpf_func = bpf_prog_warn_on_exec; 417ab3f0063SJakub Kicinski 418ab3f0063SJakub Kicinski return bpf_prog_offload_translate(prog); 419ab3f0063SJakub Kicinski } 420ab3f0063SJakub Kicinski 421675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args { 422675fc275SJakub Kicinski struct bpf_prog *prog; 423675fc275SJakub Kicinski struct bpf_prog_info *info; 424675fc275SJakub Kicinski }; 425675fc275SJakub Kicinski 426675fc275SJakub Kicinski static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data) 427675fc275SJakub Kicinski { 428675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args *args = private_data; 429675fc275SJakub Kicinski struct bpf_prog_aux *aux = args->prog->aux; 430675fc275SJakub Kicinski struct ns_common *ns; 431675fc275SJakub Kicinski struct net *net; 432675fc275SJakub Kicinski 433675fc275SJakub Kicinski rtnl_lock(); 434675fc275SJakub Kicinski down_read(&bpf_devs_lock); 435675fc275SJakub Kicinski 436675fc275SJakub Kicinski if (aux->offload) { 437675fc275SJakub Kicinski args->info->ifindex = aux->offload->netdev->ifindex; 438675fc275SJakub Kicinski net = dev_net(aux->offload->netdev); 439675fc275SJakub Kicinski get_net(net); 440675fc275SJakub Kicinski ns = &net->ns; 441675fc275SJakub Kicinski } else { 442675fc275SJakub Kicinski args->info->ifindex = 0; 443675fc275SJakub Kicinski ns = NULL; 444675fc275SJakub Kicinski } 445675fc275SJakub Kicinski 446675fc275SJakub Kicinski up_read(&bpf_devs_lock); 447675fc275SJakub Kicinski rtnl_unlock(); 448675fc275SJakub Kicinski 449675fc275SJakub Kicinski return ns; 450675fc275SJakub Kicinski } 451675fc275SJakub Kicinski 452675fc275SJakub Kicinski int bpf_prog_offload_info_fill(struct bpf_prog_info *info, 453675fc275SJakub Kicinski struct bpf_prog *prog) 454675fc275SJakub Kicinski { 455675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args args = { 456675fc275SJakub Kicinski .prog = prog, 457675fc275SJakub Kicinski .info = info, 458675fc275SJakub Kicinski }; 459fcfb126dSJiong Wang struct bpf_prog_aux *aux = prog->aux; 460675fc275SJakub Kicinski struct inode *ns_inode; 461675fc275SJakub Kicinski struct path ns_path; 462fcfb126dSJiong Wang char __user *uinsns; 463ce623f89SAleksa Sarai int res; 464fcfb126dSJiong Wang u32 ulen; 465675fc275SJakub Kicinski 466675fc275SJakub Kicinski res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args); 467ce623f89SAleksa Sarai if (res) { 468675fc275SJakub Kicinski if (!info->ifindex) 469675fc275SJakub Kicinski return -ENODEV; 470ce623f89SAleksa Sarai return res; 471675fc275SJakub Kicinski } 472675fc275SJakub Kicinski 473fcfb126dSJiong Wang down_read(&bpf_devs_lock); 474fcfb126dSJiong Wang 475fcfb126dSJiong Wang if (!aux->offload) { 476fcfb126dSJiong Wang up_read(&bpf_devs_lock); 477fcfb126dSJiong Wang return -ENODEV; 478fcfb126dSJiong Wang } 479fcfb126dSJiong Wang 480fcfb126dSJiong Wang ulen = info->jited_prog_len; 481fcfb126dSJiong Wang info->jited_prog_len = aux->offload->jited_len; 482e20d3a05SJohannes Krude if (info->jited_prog_len && ulen) { 483fcfb126dSJiong Wang uinsns = u64_to_user_ptr(info->jited_prog_insns); 484fcfb126dSJiong Wang ulen = min_t(u32, info->jited_prog_len, ulen); 485fcfb126dSJiong Wang if (copy_to_user(uinsns, aux->offload->jited_image, ulen)) { 486fcfb126dSJiong Wang up_read(&bpf_devs_lock); 487fcfb126dSJiong Wang return -EFAULT; 488fcfb126dSJiong Wang } 489fcfb126dSJiong Wang } 490fcfb126dSJiong Wang 491fcfb126dSJiong Wang up_read(&bpf_devs_lock); 492fcfb126dSJiong Wang 493675fc275SJakub Kicinski ns_inode = ns_path.dentry->d_inode; 494675fc275SJakub Kicinski info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); 495675fc275SJakub Kicinski info->netns_ino = ns_inode->i_ino; 496675fc275SJakub Kicinski path_put(&ns_path); 497675fc275SJakub Kicinski 498675fc275SJakub Kicinski return 0; 499675fc275SJakub Kicinski } 500675fc275SJakub Kicinski 501ab3f0063SJakub Kicinski const struct bpf_prog_ops bpf_offload_prog_ops = { 502ab3f0063SJakub Kicinski }; 503ab3f0063SJakub Kicinski 504a3884572SJakub Kicinski struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) 505a3884572SJakub Kicinski { 506a3884572SJakub Kicinski struct net *net = current->nsproxy->net_ns; 5079fd7c555SJakub Kicinski struct bpf_offload_netdev *ondev; 508a3884572SJakub Kicinski struct bpf_offloaded_map *offmap; 509a3884572SJakub Kicinski int err; 510a3884572SJakub Kicinski 511a3884572SJakub Kicinski if (!capable(CAP_SYS_ADMIN)) 512a3884572SJakub Kicinski return ERR_PTR(-EPERM); 5137a0ef693SJakub Kicinski if (attr->map_type != BPF_MAP_TYPE_ARRAY && 5147a0ef693SJakub Kicinski attr->map_type != BPF_MAP_TYPE_HASH) 515a3884572SJakub Kicinski return ERR_PTR(-EINVAL); 516a3884572SJakub Kicinski 51773cf09a3SYafang Shao offmap = bpf_map_area_alloc(sizeof(*offmap), NUMA_NO_NODE); 518a3884572SJakub Kicinski if (!offmap) 519a3884572SJakub Kicinski return ERR_PTR(-ENOMEM); 520a3884572SJakub Kicinski 521a3884572SJakub Kicinski bpf_map_init_from_attr(&offmap->map, attr); 522a3884572SJakub Kicinski 523a3884572SJakub Kicinski rtnl_lock(); 524a3884572SJakub Kicinski down_write(&bpf_devs_lock); 525a3884572SJakub Kicinski offmap->netdev = __dev_get_by_index(net, attr->map_ifindex); 526a3884572SJakub Kicinski err = bpf_dev_offload_check(offmap->netdev); 527a3884572SJakub Kicinski if (err) 528a3884572SJakub Kicinski goto err_unlock; 529a3884572SJakub Kicinski 5309fd7c555SJakub Kicinski ondev = bpf_offload_find_netdev(offmap->netdev); 5319fd7c555SJakub Kicinski if (!ondev) { 5329fd7c555SJakub Kicinski err = -EINVAL; 5339fd7c555SJakub Kicinski goto err_unlock; 5349fd7c555SJakub Kicinski } 5359fd7c555SJakub Kicinski 536a3884572SJakub Kicinski err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC); 537a3884572SJakub Kicinski if (err) 538a3884572SJakub Kicinski goto err_unlock; 539a3884572SJakub Kicinski 5409fd7c555SJakub Kicinski list_add_tail(&offmap->offloads, &ondev->maps); 541a3884572SJakub Kicinski up_write(&bpf_devs_lock); 542a3884572SJakub Kicinski rtnl_unlock(); 543a3884572SJakub Kicinski 544a3884572SJakub Kicinski return &offmap->map; 545a3884572SJakub Kicinski 546a3884572SJakub Kicinski err_unlock: 547a3884572SJakub Kicinski up_write(&bpf_devs_lock); 548a3884572SJakub Kicinski rtnl_unlock(); 54973cf09a3SYafang Shao bpf_map_area_free(offmap); 550a3884572SJakub Kicinski return ERR_PTR(err); 551a3884572SJakub Kicinski } 552a3884572SJakub Kicinski 553a3884572SJakub Kicinski void bpf_map_offload_map_free(struct bpf_map *map) 554a3884572SJakub Kicinski { 555a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map); 556a3884572SJakub Kicinski 557a3884572SJakub Kicinski rtnl_lock(); 558a3884572SJakub Kicinski down_write(&bpf_devs_lock); 559a3884572SJakub Kicinski if (offmap->netdev) 560a3884572SJakub Kicinski __bpf_map_offload_destroy(offmap); 561a3884572SJakub Kicinski up_write(&bpf_devs_lock); 562a3884572SJakub Kicinski rtnl_unlock(); 563a3884572SJakub Kicinski 56473cf09a3SYafang Shao bpf_map_area_free(offmap); 565a3884572SJakub Kicinski } 566a3884572SJakub Kicinski 5679629363cSYafang Shao u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map) 5689629363cSYafang Shao { 5699629363cSYafang Shao /* The memory dynamically allocated in netdev dev_ops is not counted */ 5709629363cSYafang Shao return sizeof(struct bpf_offloaded_map); 5719629363cSYafang Shao } 5729629363cSYafang Shao 573a3884572SJakub Kicinski int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value) 574a3884572SJakub Kicinski { 575a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map); 576a3884572SJakub Kicinski int ret = -ENODEV; 577a3884572SJakub Kicinski 578a3884572SJakub Kicinski down_read(&bpf_devs_lock); 579a3884572SJakub Kicinski if (offmap->netdev) 580a3884572SJakub Kicinski ret = offmap->dev_ops->map_lookup_elem(offmap, key, value); 581a3884572SJakub Kicinski up_read(&bpf_devs_lock); 582a3884572SJakub Kicinski 583a3884572SJakub Kicinski return ret; 584a3884572SJakub Kicinski } 585a3884572SJakub Kicinski 586a3884572SJakub Kicinski int bpf_map_offload_update_elem(struct bpf_map *map, 587a3884572SJakub Kicinski void *key, void *value, u64 flags) 588a3884572SJakub Kicinski { 589a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map); 590a3884572SJakub Kicinski int ret = -ENODEV; 591a3884572SJakub Kicinski 592a3884572SJakub Kicinski if (unlikely(flags > BPF_EXIST)) 593a3884572SJakub Kicinski return -EINVAL; 594a3884572SJakub Kicinski 595a3884572SJakub Kicinski down_read(&bpf_devs_lock); 596a3884572SJakub Kicinski if (offmap->netdev) 597a3884572SJakub Kicinski ret = offmap->dev_ops->map_update_elem(offmap, key, value, 598a3884572SJakub Kicinski flags); 599a3884572SJakub Kicinski up_read(&bpf_devs_lock); 600a3884572SJakub Kicinski 601a3884572SJakub Kicinski return ret; 602a3884572SJakub Kicinski } 603a3884572SJakub Kicinski 604a3884572SJakub Kicinski int bpf_map_offload_delete_elem(struct bpf_map *map, void *key) 605a3884572SJakub Kicinski { 606a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map); 607a3884572SJakub Kicinski int ret = -ENODEV; 608a3884572SJakub Kicinski 609a3884572SJakub Kicinski down_read(&bpf_devs_lock); 610a3884572SJakub Kicinski if (offmap->netdev) 611a3884572SJakub Kicinski ret = offmap->dev_ops->map_delete_elem(offmap, key); 612a3884572SJakub Kicinski up_read(&bpf_devs_lock); 613a3884572SJakub Kicinski 614a3884572SJakub Kicinski return ret; 615a3884572SJakub Kicinski } 616a3884572SJakub Kicinski 617a3884572SJakub Kicinski int bpf_map_offload_get_next_key(struct bpf_map *map, void *key, void *next_key) 618a3884572SJakub Kicinski { 619a3884572SJakub Kicinski struct bpf_offloaded_map *offmap = map_to_offmap(map); 620a3884572SJakub Kicinski int ret = -ENODEV; 621a3884572SJakub Kicinski 622a3884572SJakub Kicinski down_read(&bpf_devs_lock); 623a3884572SJakub Kicinski if (offmap->netdev) 624a3884572SJakub Kicinski ret = offmap->dev_ops->map_get_next_key(offmap, key, next_key); 625a3884572SJakub Kicinski up_read(&bpf_devs_lock); 626a3884572SJakub Kicinski 627a3884572SJakub Kicinski return ret; 628a3884572SJakub Kicinski } 629a3884572SJakub Kicinski 63052775b33SJakub Kicinski struct ns_get_path_bpf_map_args { 63152775b33SJakub Kicinski struct bpf_offloaded_map *offmap; 63252775b33SJakub Kicinski struct bpf_map_info *info; 63352775b33SJakub Kicinski }; 63452775b33SJakub Kicinski 63552775b33SJakub Kicinski static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data) 63652775b33SJakub Kicinski { 63752775b33SJakub Kicinski struct ns_get_path_bpf_map_args *args = private_data; 63852775b33SJakub Kicinski struct ns_common *ns; 63952775b33SJakub Kicinski struct net *net; 64052775b33SJakub Kicinski 64152775b33SJakub Kicinski rtnl_lock(); 64252775b33SJakub Kicinski down_read(&bpf_devs_lock); 64352775b33SJakub Kicinski 64452775b33SJakub Kicinski if (args->offmap->netdev) { 64552775b33SJakub Kicinski args->info->ifindex = args->offmap->netdev->ifindex; 64652775b33SJakub Kicinski net = dev_net(args->offmap->netdev); 64752775b33SJakub Kicinski get_net(net); 64852775b33SJakub Kicinski ns = &net->ns; 64952775b33SJakub Kicinski } else { 65052775b33SJakub Kicinski args->info->ifindex = 0; 65152775b33SJakub Kicinski ns = NULL; 65252775b33SJakub Kicinski } 65352775b33SJakub Kicinski 65452775b33SJakub Kicinski up_read(&bpf_devs_lock); 65552775b33SJakub Kicinski rtnl_unlock(); 65652775b33SJakub Kicinski 65752775b33SJakub Kicinski return ns; 65852775b33SJakub Kicinski } 65952775b33SJakub Kicinski 66052775b33SJakub Kicinski int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map) 66152775b33SJakub Kicinski { 66252775b33SJakub Kicinski struct ns_get_path_bpf_map_args args = { 66352775b33SJakub Kicinski .offmap = map_to_offmap(map), 66452775b33SJakub Kicinski .info = info, 66552775b33SJakub Kicinski }; 66652775b33SJakub Kicinski struct inode *ns_inode; 66752775b33SJakub Kicinski struct path ns_path; 668ce623f89SAleksa Sarai int res; 66952775b33SJakub Kicinski 67052775b33SJakub Kicinski res = ns_get_path_cb(&ns_path, bpf_map_offload_info_fill_ns, &args); 671ce623f89SAleksa Sarai if (res) { 67252775b33SJakub Kicinski if (!info->ifindex) 67352775b33SJakub Kicinski return -ENODEV; 674ce623f89SAleksa Sarai return res; 67552775b33SJakub Kicinski } 67652775b33SJakub Kicinski 67752775b33SJakub Kicinski ns_inode = ns_path.dentry->d_inode; 67852775b33SJakub Kicinski info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); 67952775b33SJakub Kicinski info->netns_ino = ns_inode->i_ino; 68052775b33SJakub Kicinski path_put(&ns_path); 68152775b33SJakub Kicinski 68252775b33SJakub Kicinski return 0; 68352775b33SJakub Kicinski } 68452775b33SJakub Kicinski 685fd4f227dSJakub Kicinski static bool __bpf_offload_dev_match(struct bpf_prog *prog, 686fd4f227dSJakub Kicinski struct net_device *netdev) 687a3884572SJakub Kicinski { 688fd4f227dSJakub Kicinski struct bpf_offload_netdev *ondev1, *ondev2; 689a3884572SJakub Kicinski struct bpf_prog_offload *offload; 690a3884572SJakub Kicinski 6910cd3cbedSJakub Kicinski if (!bpf_prog_is_dev_bound(prog->aux)) 692a3884572SJakub Kicinski return false; 693fd4f227dSJakub Kicinski 694fd4f227dSJakub Kicinski offload = prog->aux->offload; 695fd4f227dSJakub Kicinski if (!offload) 696fd4f227dSJakub Kicinski return false; 697fd4f227dSJakub Kicinski if (offload->netdev == netdev) 698fd4f227dSJakub Kicinski return true; 699fd4f227dSJakub Kicinski 700fd4f227dSJakub Kicinski ondev1 = bpf_offload_find_netdev(offload->netdev); 701fd4f227dSJakub Kicinski ondev2 = bpf_offload_find_netdev(netdev); 702fd4f227dSJakub Kicinski 703fd4f227dSJakub Kicinski return ondev1 && ondev2 && ondev1->offdev == ondev2->offdev; 704fd4f227dSJakub Kicinski } 705fd4f227dSJakub Kicinski 706fd4f227dSJakub Kicinski bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev) 707fd4f227dSJakub Kicinski { 708fd4f227dSJakub Kicinski bool ret; 709a3884572SJakub Kicinski 710a3884572SJakub Kicinski down_read(&bpf_devs_lock); 711fd4f227dSJakub Kicinski ret = __bpf_offload_dev_match(prog, netdev); 712fd4f227dSJakub Kicinski up_read(&bpf_devs_lock); 713fd4f227dSJakub Kicinski 714fd4f227dSJakub Kicinski return ret; 715fd4f227dSJakub Kicinski } 716fd4f227dSJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_match); 717fd4f227dSJakub Kicinski 718fd7c211dSToke Høiland-Jørgensen bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs) 719fd7c211dSToke Høiland-Jørgensen { 720fd7c211dSToke Høiland-Jørgensen bool ret; 721fd7c211dSToke Høiland-Jørgensen 722fd7c211dSToke Høiland-Jørgensen if (bpf_prog_is_offloaded(lhs->aux) != bpf_prog_is_offloaded(rhs->aux)) 723fd7c211dSToke Høiland-Jørgensen return false; 724fd7c211dSToke Høiland-Jørgensen 725fd7c211dSToke Høiland-Jørgensen down_read(&bpf_devs_lock); 726fd7c211dSToke Høiland-Jørgensen ret = lhs->aux->offload && rhs->aux->offload && 727fd7c211dSToke Høiland-Jørgensen lhs->aux->offload->netdev && 728fd7c211dSToke Høiland-Jørgensen lhs->aux->offload->netdev == rhs->aux->offload->netdev; 729fd7c211dSToke Høiland-Jørgensen up_read(&bpf_devs_lock); 730fd7c211dSToke Høiland-Jørgensen 731fd7c211dSToke Høiland-Jørgensen return ret; 732fd7c211dSToke Høiland-Jørgensen } 733fd7c211dSToke Høiland-Jørgensen 734fd4f227dSJakub Kicinski bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map) 735fd4f227dSJakub Kicinski { 736fd4f227dSJakub Kicinski struct bpf_offloaded_map *offmap; 737fd4f227dSJakub Kicinski bool ret; 738fd4f227dSJakub Kicinski 7399d03ebc7SStanislav Fomichev if (!bpf_map_is_offloaded(map)) 740fd4f227dSJakub Kicinski return bpf_map_offload_neutral(map); 741a3884572SJakub Kicinski offmap = map_to_offmap(map); 742a3884572SJakub Kicinski 743fd4f227dSJakub Kicinski down_read(&bpf_devs_lock); 744fd4f227dSJakub Kicinski ret = __bpf_offload_dev_match(prog, offmap->netdev); 745a3884572SJakub Kicinski up_read(&bpf_devs_lock); 746a3884572SJakub Kicinski 747a3884572SJakub Kicinski return ret; 748a3884572SJakub Kicinski } 749a3884572SJakub Kicinski 750602144c2SJakub Kicinski int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, 751602144c2SJakub Kicinski struct net_device *netdev) 752a3884572SJakub Kicinski { 7539fd7c555SJakub Kicinski int err; 754a3884572SJakub Kicinski 7559fd7c555SJakub Kicinski down_write(&bpf_devs_lock); 7562b3486bcSStanislav Fomichev err = __bpf_offload_dev_netdev_register(offdev, netdev); 7579fd7c555SJakub Kicinski up_write(&bpf_devs_lock); 7589fd7c555SJakub Kicinski return err; 759a3884572SJakub Kicinski } 7609fd7c555SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register); 761a3884572SJakub Kicinski 762602144c2SJakub Kicinski void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, 763602144c2SJakub Kicinski struct net_device *netdev) 764ab3f0063SJakub Kicinski { 765e0d3974aSJakub Kicinski down_write(&bpf_devs_lock); 76689bbc53aSStanislav Fomichev __bpf_offload_dev_netdev_unregister(offdev, netdev); 767e0d3974aSJakub Kicinski up_write(&bpf_devs_lock); 768ab3f0063SJakub Kicinski } 7699fd7c555SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister); 770602144c2SJakub Kicinski 7711385d755SQuentin Monnet struct bpf_offload_dev * 772dd27c2e3SJakub Kicinski bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv) 773602144c2SJakub Kicinski { 774602144c2SJakub Kicinski struct bpf_offload_dev *offdev; 775602144c2SJakub Kicinski 776602144c2SJakub Kicinski offdev = kzalloc(sizeof(*offdev), GFP_KERNEL); 777602144c2SJakub Kicinski if (!offdev) 778602144c2SJakub Kicinski return ERR_PTR(-ENOMEM); 779602144c2SJakub Kicinski 7801385d755SQuentin Monnet offdev->ops = ops; 781dd27c2e3SJakub Kicinski offdev->priv = priv; 782602144c2SJakub Kicinski INIT_LIST_HEAD(&offdev->netdevs); 783602144c2SJakub Kicinski 784602144c2SJakub Kicinski return offdev; 785602144c2SJakub Kicinski } 786602144c2SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_create); 787602144c2SJakub Kicinski 788602144c2SJakub Kicinski void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev) 789602144c2SJakub Kicinski { 790602144c2SJakub Kicinski WARN_ON(!list_empty(&offdev->netdevs)); 791602144c2SJakub Kicinski kfree(offdev); 792602144c2SJakub Kicinski } 793602144c2SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy); 794dd27c2e3SJakub Kicinski 795dd27c2e3SJakub Kicinski void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev) 796dd27c2e3SJakub Kicinski { 797dd27c2e3SJakub Kicinski return offdev->priv; 798dd27c2e3SJakub Kicinski } 799dd27c2e3SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_offload_dev_priv); 800f1fc43d0SStanislav Fomichev 8012b3486bcSStanislav Fomichev void bpf_dev_bound_netdev_unregister(struct net_device *dev) 8022b3486bcSStanislav Fomichev { 8032b3486bcSStanislav Fomichev struct bpf_offload_netdev *ondev; 8042b3486bcSStanislav Fomichev 8052b3486bcSStanislav Fomichev ASSERT_RTNL(); 8062b3486bcSStanislav Fomichev 8072b3486bcSStanislav Fomichev down_write(&bpf_devs_lock); 8082b3486bcSStanislav Fomichev ondev = bpf_offload_find_netdev(dev); 8092b3486bcSStanislav Fomichev if (ondev && !ondev->offdev) 8102b3486bcSStanislav Fomichev __bpf_offload_dev_netdev_unregister(NULL, ondev->netdev); 8112b3486bcSStanislav Fomichev up_write(&bpf_devs_lock); 8122b3486bcSStanislav Fomichev } 8132b3486bcSStanislav Fomichev 8143d76a4d3SStanislav Fomichev int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log, 8153d76a4d3SStanislav Fomichev struct bpf_prog_aux *prog_aux) 8163d76a4d3SStanislav Fomichev { 8173d76a4d3SStanislav Fomichev if (!bpf_prog_is_dev_bound(prog_aux)) { 8183d76a4d3SStanislav Fomichev bpf_log(log, "metadata kfuncs require device-bound program\n"); 8193d76a4d3SStanislav Fomichev return -EINVAL; 8203d76a4d3SStanislav Fomichev } 8213d76a4d3SStanislav Fomichev 8223d76a4d3SStanislav Fomichev if (bpf_prog_is_offloaded(prog_aux)) { 8233d76a4d3SStanislav Fomichev bpf_log(log, "metadata kfuncs can't be offloaded\n"); 8243d76a4d3SStanislav Fomichev return -EINVAL; 8253d76a4d3SStanislav Fomichev } 8263d76a4d3SStanislav Fomichev 8273d76a4d3SStanislav Fomichev return 0; 8283d76a4d3SStanislav Fomichev } 8293d76a4d3SStanislav Fomichev 8303d76a4d3SStanislav Fomichev void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id) 8313d76a4d3SStanislav Fomichev { 8323d76a4d3SStanislav Fomichev const struct xdp_metadata_ops *ops; 8333d76a4d3SStanislav Fomichev void *p = NULL; 8343d76a4d3SStanislav Fomichev 8353d76a4d3SStanislav Fomichev /* We don't hold bpf_devs_lock while resolving several 8363d76a4d3SStanislav Fomichev * kfuncs and can race with the unregister_netdevice(). 8373d76a4d3SStanislav Fomichev * We rely on bpf_dev_bound_match() check at attach 8383d76a4d3SStanislav Fomichev * to render this program unusable. 8393d76a4d3SStanislav Fomichev */ 8403d76a4d3SStanislav Fomichev down_read(&bpf_devs_lock); 8413d76a4d3SStanislav Fomichev if (!prog->aux->offload) 8423d76a4d3SStanislav Fomichev goto out; 8433d76a4d3SStanislav Fomichev 8443d76a4d3SStanislav Fomichev ops = prog->aux->offload->netdev->xdp_metadata_ops; 8453d76a4d3SStanislav Fomichev if (!ops) 8463d76a4d3SStanislav Fomichev goto out; 8473d76a4d3SStanislav Fomichev 8483d76a4d3SStanislav Fomichev if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP)) 8493d76a4d3SStanislav Fomichev p = ops->xmo_rx_timestamp; 8503d76a4d3SStanislav Fomichev else if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH)) 8513d76a4d3SStanislav Fomichev p = ops->xmo_rx_hash; 8523d76a4d3SStanislav Fomichev out: 8533d76a4d3SStanislav Fomichev up_read(&bpf_devs_lock); 8543d76a4d3SStanislav Fomichev 8553d76a4d3SStanislav Fomichev return p; 8563d76a4d3SStanislav Fomichev } 8573d76a4d3SStanislav Fomichev 858f1fc43d0SStanislav Fomichev static int __init bpf_offload_init(void) 859f1fc43d0SStanislav Fomichev { 860f1fc43d0SStanislav Fomichev return rhashtable_init(&offdevs, &offdevs_params); 861f1fc43d0SStanislav Fomichev } 862f1fc43d0SStanislav Fomichev 863e1505c1cSJakub Kicinski core_initcall(bpf_offload_init); 864