Jakub Kicinski | a39e17b | 2017-11-27 12:10:23 -0800 | [diff] [blame] | 1 | /* |
Jakub Kicinski | 0cd3cbe | 2018-05-03 18:37:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
Jakub Kicinski | a39e17b | 2017-11-27 12:10:23 -0800 | [diff] [blame] | 3 | * |
| 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. |
| 7 | * |
| 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. |
| 14 | */ |
| 15 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 16 | #include <linux/bpf.h> |
| 17 | #include <linux/bpf_verifier.h> |
| 18 | #include <linux/bug.h> |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 19 | #include <linux/kdev_t.h> |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 20 | #include <linux/list.h> |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 21 | #include <linux/lockdep.h> |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/printk.h> |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 24 | #include <linux/proc_ns.h> |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 25 | #include <linux/rhashtable.h> |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 26 | #include <linux/rtnetlink.h> |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 27 | #include <linux/rwsem.h> |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 28 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 29 | /* Protects offdevs, members of bpf_offload_netdev and offload members |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 30 | * of all progs. |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 31 | * RTNL lock cannot be taken when holding this lock. |
| 32 | */ |
| 33 | static DECLARE_RWSEM(bpf_devs_lock); |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 34 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 35 | struct bpf_offload_dev { |
Quentin Monnet | 1385d75 | 2018-11-09 13:03:25 +0000 | [diff] [blame] | 36 | const struct bpf_prog_offload_ops *ops; |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 37 | struct list_head netdevs; |
| 38 | }; |
| 39 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 40 | struct bpf_offload_netdev { |
| 41 | struct rhash_head l; |
| 42 | struct net_device *netdev; |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 43 | struct bpf_offload_dev *offdev; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 44 | struct list_head progs; |
| 45 | struct list_head maps; |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 46 | struct list_head offdev_netdevs; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static const struct rhashtable_params offdevs_params = { |
| 50 | .nelem_hint = 4, |
| 51 | .key_len = sizeof(struct net_device *), |
| 52 | .key_offset = offsetof(struct bpf_offload_netdev, netdev), |
| 53 | .head_offset = offsetof(struct bpf_offload_netdev, l), |
| 54 | .automatic_shrinking = true, |
| 55 | }; |
| 56 | |
| 57 | static struct rhashtable offdevs; |
| 58 | static bool offdevs_inited; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 59 | |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 60 | static int bpf_dev_offload_check(struct net_device *netdev) |
| 61 | { |
| 62 | if (!netdev) |
| 63 | return -EINVAL; |
| 64 | if (!netdev->netdev_ops->ndo_bpf) |
| 65 | return -EOPNOTSUPP; |
| 66 | return 0; |
| 67 | } |
| 68 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 69 | static struct bpf_offload_netdev * |
| 70 | bpf_offload_find_netdev(struct net_device *netdev) |
| 71 | { |
| 72 | lockdep_assert_held(&bpf_devs_lock); |
| 73 | |
| 74 | if (!offdevs_inited) |
| 75 | return NULL; |
| 76 | return rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params); |
| 77 | } |
| 78 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 79 | int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) |
| 80 | { |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 81 | struct bpf_offload_netdev *ondev; |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 82 | struct bpf_prog_offload *offload; |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 83 | int err; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 84 | |
Jakub Kicinski | 649f11d | 2017-11-20 15:21:52 -0800 | [diff] [blame] | 85 | if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS && |
| 86 | attr->prog_type != BPF_PROG_TYPE_XDP) |
| 87 | return -EINVAL; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 88 | |
| 89 | if (attr->prog_flags) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | offload = kzalloc(sizeof(*offload), GFP_USER); |
| 93 | if (!offload) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | offload->prog = prog; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 97 | |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 98 | offload->netdev = dev_get_by_index(current->nsproxy->net_ns, |
| 99 | attr->prog_ifindex); |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 100 | err = bpf_dev_offload_check(offload->netdev); |
| 101 | if (err) |
| 102 | goto err_maybe_put; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 103 | |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 104 | down_write(&bpf_devs_lock); |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 105 | ondev = bpf_offload_find_netdev(offload->netdev); |
| 106 | if (!ondev) { |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 107 | err = -EINVAL; |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 108 | goto err_unlock; |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 109 | } |
Quentin Monnet | 341b3e7 | 2018-11-09 13:03:26 +0000 | [diff] [blame] | 110 | offload->offdev = ondev->offdev; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 111 | prog->aux->offload = offload; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 112 | list_add_tail(&offload->offloads, &ondev->progs); |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 113 | dev_put(offload->netdev); |
| 114 | up_write(&bpf_devs_lock); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 115 | |
| 116 | return 0; |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 117 | err_unlock: |
| 118 | up_write(&bpf_devs_lock); |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 119 | err_maybe_put: |
| 120 | if (offload->netdev) |
| 121 | dev_put(offload->netdev); |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 122 | kfree(offload); |
Jakub Kicinski | 5bc2d55 | 2018-01-11 20:29:08 -0800 | [diff] [blame] | 123 | return err; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int __bpf_offload_ndo(struct bpf_prog *prog, enum bpf_netdev_command cmd, |
| 127 | struct netdev_bpf *data) |
| 128 | { |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 129 | struct bpf_prog_offload *offload = prog->aux->offload; |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 130 | struct net_device *netdev; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 131 | |
| 132 | ASSERT_RTNL(); |
| 133 | |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 134 | if (!offload) |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 135 | return -ENODEV; |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 136 | netdev = offload->netdev; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 137 | |
| 138 | data->command = cmd; |
| 139 | |
| 140 | return netdev->netdev_ops->ndo_bpf(netdev, data); |
| 141 | } |
| 142 | |
| 143 | int bpf_prog_offload_verifier_prep(struct bpf_verifier_env *env) |
| 144 | { |
| 145 | struct netdev_bpf data = {}; |
| 146 | int err; |
| 147 | |
| 148 | data.verifier.prog = env->prog; |
| 149 | |
| 150 | rtnl_lock(); |
| 151 | err = __bpf_offload_ndo(env->prog, BPF_OFFLOAD_VERIFIER_PREP, &data); |
| 152 | if (err) |
| 153 | goto exit_unlock; |
| 154 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 155 | env->prog->aux->offload->dev_ops = data.verifier.ops; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 156 | env->prog->aux->offload->dev_state = true; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 157 | exit_unlock: |
| 158 | rtnl_unlock(); |
| 159 | return err; |
| 160 | } |
| 161 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 162 | int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, |
| 163 | int insn_idx, int prev_insn_idx) |
| 164 | { |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 165 | struct bpf_prog_offload *offload; |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 166 | int ret = -ENODEV; |
| 167 | |
| 168 | down_read(&bpf_devs_lock); |
| 169 | offload = env->prog->aux->offload; |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 170 | if (offload) |
Quentin Monnet | 341b3e7 | 2018-11-09 13:03:26 +0000 | [diff] [blame] | 171 | ret = offload->offdev->ops->insn_hook(env, insn_idx, |
| 172 | prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 173 | up_read(&bpf_devs_lock); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 178 | int bpf_prog_offload_finalize(struct bpf_verifier_env *env) |
| 179 | { |
| 180 | struct bpf_prog_offload *offload; |
| 181 | int ret = -ENODEV; |
| 182 | |
| 183 | down_read(&bpf_devs_lock); |
| 184 | offload = env->prog->aux->offload; |
| 185 | if (offload) { |
Quentin Monnet | 6dc18fa | 2018-11-09 13:03:27 +0000 | [diff] [blame^] | 186 | if (offload->offdev->ops->finalize) |
| 187 | ret = offload->offdev->ops->finalize(env); |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 188 | else |
| 189 | ret = 0; |
| 190 | } |
| 191 | up_read(&bpf_devs_lock); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 196 | static void __bpf_prog_offload_destroy(struct bpf_prog *prog) |
| 197 | { |
Jakub Kicinski | 0a9c199 | 2018-01-11 20:29:07 -0800 | [diff] [blame] | 198 | struct bpf_prog_offload *offload = prog->aux->offload; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 199 | struct netdev_bpf data = {}; |
| 200 | |
| 201 | data.offload.prog = prog; |
| 202 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 203 | if (offload->dev_state) |
| 204 | WARN_ON(__bpf_offload_ndo(prog, BPF_OFFLOAD_DESTROY, &data)); |
| 205 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 206 | /* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */ |
| 207 | bpf_prog_free_id(prog, true); |
| 208 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 209 | list_del_init(&offload->offloads); |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 210 | kfree(offload); |
| 211 | prog->aux->offload = NULL; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void bpf_prog_offload_destroy(struct bpf_prog *prog) |
| 215 | { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 216 | rtnl_lock(); |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 217 | down_write(&bpf_devs_lock); |
Jakub Kicinski | ce3b9db | 2017-12-27 18:39:06 -0800 | [diff] [blame] | 218 | if (prog->aux->offload) |
| 219 | __bpf_prog_offload_destroy(prog); |
Jakub Kicinski | e0d3974 | 2017-12-27 18:39:03 -0800 | [diff] [blame] | 220 | up_write(&bpf_devs_lock); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 221 | rtnl_unlock(); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int bpf_prog_offload_translate(struct bpf_prog *prog) |
| 225 | { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 226 | struct netdev_bpf data = {}; |
| 227 | int ret; |
| 228 | |
| 229 | data.offload.prog = prog; |
| 230 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 231 | rtnl_lock(); |
| 232 | ret = __bpf_offload_ndo(prog, BPF_OFFLOAD_TRANSLATE, &data); |
| 233 | rtnl_unlock(); |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static unsigned int bpf_prog_warn_on_exec(const void *ctx, |
| 239 | const struct bpf_insn *insn) |
| 240 | { |
| 241 | WARN(1, "attempt to execute device eBPF program on the host!"); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int bpf_prog_offload_compile(struct bpf_prog *prog) |
| 246 | { |
| 247 | prog->bpf_func = bpf_prog_warn_on_exec; |
| 248 | |
| 249 | return bpf_prog_offload_translate(prog); |
| 250 | } |
| 251 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 252 | struct ns_get_path_bpf_prog_args { |
| 253 | struct bpf_prog *prog; |
| 254 | struct bpf_prog_info *info; |
| 255 | }; |
| 256 | |
| 257 | static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data) |
| 258 | { |
| 259 | struct ns_get_path_bpf_prog_args *args = private_data; |
| 260 | struct bpf_prog_aux *aux = args->prog->aux; |
| 261 | struct ns_common *ns; |
| 262 | struct net *net; |
| 263 | |
| 264 | rtnl_lock(); |
| 265 | down_read(&bpf_devs_lock); |
| 266 | |
| 267 | if (aux->offload) { |
| 268 | args->info->ifindex = aux->offload->netdev->ifindex; |
| 269 | net = dev_net(aux->offload->netdev); |
| 270 | get_net(net); |
| 271 | ns = &net->ns; |
| 272 | } else { |
| 273 | args->info->ifindex = 0; |
| 274 | ns = NULL; |
| 275 | } |
| 276 | |
| 277 | up_read(&bpf_devs_lock); |
| 278 | rtnl_unlock(); |
| 279 | |
| 280 | return ns; |
| 281 | } |
| 282 | |
| 283 | int bpf_prog_offload_info_fill(struct bpf_prog_info *info, |
| 284 | struct bpf_prog *prog) |
| 285 | { |
| 286 | struct ns_get_path_bpf_prog_args args = { |
| 287 | .prog = prog, |
| 288 | .info = info, |
| 289 | }; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 290 | struct bpf_prog_aux *aux = prog->aux; |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 291 | struct inode *ns_inode; |
| 292 | struct path ns_path; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 293 | char __user *uinsns; |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 294 | void *res; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 295 | u32 ulen; |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 296 | |
| 297 | res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args); |
| 298 | if (IS_ERR(res)) { |
| 299 | if (!info->ifindex) |
| 300 | return -ENODEV; |
| 301 | return PTR_ERR(res); |
| 302 | } |
| 303 | |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 304 | down_read(&bpf_devs_lock); |
| 305 | |
| 306 | if (!aux->offload) { |
| 307 | up_read(&bpf_devs_lock); |
| 308 | return -ENODEV; |
| 309 | } |
| 310 | |
| 311 | ulen = info->jited_prog_len; |
| 312 | info->jited_prog_len = aux->offload->jited_len; |
| 313 | if (info->jited_prog_len & ulen) { |
| 314 | uinsns = u64_to_user_ptr(info->jited_prog_insns); |
| 315 | ulen = min_t(u32, info->jited_prog_len, ulen); |
| 316 | if (copy_to_user(uinsns, aux->offload->jited_image, ulen)) { |
| 317 | up_read(&bpf_devs_lock); |
| 318 | return -EFAULT; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | up_read(&bpf_devs_lock); |
| 323 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 324 | ns_inode = ns_path.dentry->d_inode; |
| 325 | info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); |
| 326 | info->netns_ino = ns_inode->i_ino; |
| 327 | path_put(&ns_path); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 332 | const struct bpf_prog_ops bpf_offload_prog_ops = { |
| 333 | }; |
| 334 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 335 | static int bpf_map_offload_ndo(struct bpf_offloaded_map *offmap, |
| 336 | enum bpf_netdev_command cmd) |
| 337 | { |
| 338 | struct netdev_bpf data = {}; |
| 339 | struct net_device *netdev; |
| 340 | |
| 341 | ASSERT_RTNL(); |
| 342 | |
| 343 | data.command = cmd; |
| 344 | data.offmap = offmap; |
| 345 | /* Caller must make sure netdev is valid */ |
| 346 | netdev = offmap->netdev; |
| 347 | |
| 348 | return netdev->netdev_ops->ndo_bpf(netdev, &data); |
| 349 | } |
| 350 | |
| 351 | struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) |
| 352 | { |
| 353 | struct net *net = current->nsproxy->net_ns; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 354 | struct bpf_offload_netdev *ondev; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 355 | struct bpf_offloaded_map *offmap; |
| 356 | int err; |
| 357 | |
| 358 | if (!capable(CAP_SYS_ADMIN)) |
| 359 | return ERR_PTR(-EPERM); |
Jakub Kicinski | 7a0ef69 | 2018-01-17 19:13:27 -0800 | [diff] [blame] | 360 | if (attr->map_type != BPF_MAP_TYPE_ARRAY && |
| 361 | attr->map_type != BPF_MAP_TYPE_HASH) |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 362 | return ERR_PTR(-EINVAL); |
| 363 | |
| 364 | offmap = kzalloc(sizeof(*offmap), GFP_USER); |
| 365 | if (!offmap) |
| 366 | return ERR_PTR(-ENOMEM); |
| 367 | |
| 368 | bpf_map_init_from_attr(&offmap->map, attr); |
| 369 | |
| 370 | rtnl_lock(); |
| 371 | down_write(&bpf_devs_lock); |
| 372 | offmap->netdev = __dev_get_by_index(net, attr->map_ifindex); |
| 373 | err = bpf_dev_offload_check(offmap->netdev); |
| 374 | if (err) |
| 375 | goto err_unlock; |
| 376 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 377 | ondev = bpf_offload_find_netdev(offmap->netdev); |
| 378 | if (!ondev) { |
| 379 | err = -EINVAL; |
| 380 | goto err_unlock; |
| 381 | } |
| 382 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 383 | err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC); |
| 384 | if (err) |
| 385 | goto err_unlock; |
| 386 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 387 | list_add_tail(&offmap->offloads, &ondev->maps); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 388 | up_write(&bpf_devs_lock); |
| 389 | rtnl_unlock(); |
| 390 | |
| 391 | return &offmap->map; |
| 392 | |
| 393 | err_unlock: |
| 394 | up_write(&bpf_devs_lock); |
| 395 | rtnl_unlock(); |
| 396 | kfree(offmap); |
| 397 | return ERR_PTR(err); |
| 398 | } |
| 399 | |
| 400 | static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap) |
| 401 | { |
| 402 | WARN_ON(bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_FREE)); |
| 403 | /* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map */ |
| 404 | bpf_map_free_id(&offmap->map, true); |
| 405 | list_del_init(&offmap->offloads); |
| 406 | offmap->netdev = NULL; |
| 407 | } |
| 408 | |
| 409 | void bpf_map_offload_map_free(struct bpf_map *map) |
| 410 | { |
| 411 | struct bpf_offloaded_map *offmap = map_to_offmap(map); |
| 412 | |
| 413 | rtnl_lock(); |
| 414 | down_write(&bpf_devs_lock); |
| 415 | if (offmap->netdev) |
| 416 | __bpf_map_offload_destroy(offmap); |
| 417 | up_write(&bpf_devs_lock); |
| 418 | rtnl_unlock(); |
| 419 | |
| 420 | kfree(offmap); |
| 421 | } |
| 422 | |
| 423 | int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value) |
| 424 | { |
| 425 | struct bpf_offloaded_map *offmap = map_to_offmap(map); |
| 426 | int ret = -ENODEV; |
| 427 | |
| 428 | down_read(&bpf_devs_lock); |
| 429 | if (offmap->netdev) |
| 430 | ret = offmap->dev_ops->map_lookup_elem(offmap, key, value); |
| 431 | up_read(&bpf_devs_lock); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int bpf_map_offload_update_elem(struct bpf_map *map, |
| 437 | void *key, void *value, u64 flags) |
| 438 | { |
| 439 | struct bpf_offloaded_map *offmap = map_to_offmap(map); |
| 440 | int ret = -ENODEV; |
| 441 | |
| 442 | if (unlikely(flags > BPF_EXIST)) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | down_read(&bpf_devs_lock); |
| 446 | if (offmap->netdev) |
| 447 | ret = offmap->dev_ops->map_update_elem(offmap, key, value, |
| 448 | flags); |
| 449 | up_read(&bpf_devs_lock); |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | int bpf_map_offload_delete_elem(struct bpf_map *map, void *key) |
| 455 | { |
| 456 | struct bpf_offloaded_map *offmap = map_to_offmap(map); |
| 457 | int ret = -ENODEV; |
| 458 | |
| 459 | down_read(&bpf_devs_lock); |
| 460 | if (offmap->netdev) |
| 461 | ret = offmap->dev_ops->map_delete_elem(offmap, key); |
| 462 | up_read(&bpf_devs_lock); |
| 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | int bpf_map_offload_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 468 | { |
| 469 | struct bpf_offloaded_map *offmap = map_to_offmap(map); |
| 470 | int ret = -ENODEV; |
| 471 | |
| 472 | down_read(&bpf_devs_lock); |
| 473 | if (offmap->netdev) |
| 474 | ret = offmap->dev_ops->map_get_next_key(offmap, key, next_key); |
| 475 | up_read(&bpf_devs_lock); |
| 476 | |
| 477 | return ret; |
| 478 | } |
| 479 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 480 | struct ns_get_path_bpf_map_args { |
| 481 | struct bpf_offloaded_map *offmap; |
| 482 | struct bpf_map_info *info; |
| 483 | }; |
| 484 | |
| 485 | static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data) |
| 486 | { |
| 487 | struct ns_get_path_bpf_map_args *args = private_data; |
| 488 | struct ns_common *ns; |
| 489 | struct net *net; |
| 490 | |
| 491 | rtnl_lock(); |
| 492 | down_read(&bpf_devs_lock); |
| 493 | |
| 494 | if (args->offmap->netdev) { |
| 495 | args->info->ifindex = args->offmap->netdev->ifindex; |
| 496 | net = dev_net(args->offmap->netdev); |
| 497 | get_net(net); |
| 498 | ns = &net->ns; |
| 499 | } else { |
| 500 | args->info->ifindex = 0; |
| 501 | ns = NULL; |
| 502 | } |
| 503 | |
| 504 | up_read(&bpf_devs_lock); |
| 505 | rtnl_unlock(); |
| 506 | |
| 507 | return ns; |
| 508 | } |
| 509 | |
| 510 | int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map) |
| 511 | { |
| 512 | struct ns_get_path_bpf_map_args args = { |
| 513 | .offmap = map_to_offmap(map), |
| 514 | .info = info, |
| 515 | }; |
| 516 | struct inode *ns_inode; |
| 517 | struct path ns_path; |
| 518 | void *res; |
| 519 | |
| 520 | res = ns_get_path_cb(&ns_path, bpf_map_offload_info_fill_ns, &args); |
| 521 | if (IS_ERR(res)) { |
| 522 | if (!info->ifindex) |
| 523 | return -ENODEV; |
| 524 | return PTR_ERR(res); |
| 525 | } |
| 526 | |
| 527 | ns_inode = ns_path.dentry->d_inode; |
| 528 | info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); |
| 529 | info->netns_ino = ns_inode->i_ino; |
| 530 | path_put(&ns_path); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 535 | static bool __bpf_offload_dev_match(struct bpf_prog *prog, |
| 536 | struct net_device *netdev) |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 537 | { |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 538 | struct bpf_offload_netdev *ondev1, *ondev2; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 539 | struct bpf_prog_offload *offload; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 540 | |
Jakub Kicinski | 0cd3cbe | 2018-05-03 18:37:08 -0700 | [diff] [blame] | 541 | if (!bpf_prog_is_dev_bound(prog->aux)) |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 542 | return false; |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 543 | |
| 544 | offload = prog->aux->offload; |
| 545 | if (!offload) |
| 546 | return false; |
| 547 | if (offload->netdev == netdev) |
| 548 | return true; |
| 549 | |
| 550 | ondev1 = bpf_offload_find_netdev(offload->netdev); |
| 551 | ondev2 = bpf_offload_find_netdev(netdev); |
| 552 | |
| 553 | return ondev1 && ondev2 && ondev1->offdev == ondev2->offdev; |
| 554 | } |
| 555 | |
| 556 | bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev) |
| 557 | { |
| 558 | bool ret; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 559 | |
| 560 | down_read(&bpf_devs_lock); |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 561 | ret = __bpf_offload_dev_match(prog, netdev); |
| 562 | up_read(&bpf_devs_lock); |
| 563 | |
| 564 | return ret; |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(bpf_offload_dev_match); |
| 567 | |
| 568 | bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map) |
| 569 | { |
| 570 | struct bpf_offloaded_map *offmap; |
| 571 | bool ret; |
| 572 | |
| 573 | if (!bpf_map_is_dev_bound(map)) |
| 574 | return bpf_map_offload_neutral(map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 575 | offmap = map_to_offmap(map); |
| 576 | |
Jakub Kicinski | fd4f227 | 2018-07-17 10:53:26 -0700 | [diff] [blame] | 577 | down_read(&bpf_devs_lock); |
| 578 | ret = __bpf_offload_dev_match(prog, offmap->netdev); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 579 | up_read(&bpf_devs_lock); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 584 | int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, |
| 585 | struct net_device *netdev) |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 586 | { |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 587 | struct bpf_offload_netdev *ondev; |
| 588 | int err; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 589 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 590 | ondev = kzalloc(sizeof(*ondev), GFP_KERNEL); |
| 591 | if (!ondev) |
| 592 | return -ENOMEM; |
| 593 | |
| 594 | ondev->netdev = netdev; |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 595 | ondev->offdev = offdev; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 596 | INIT_LIST_HEAD(&ondev->progs); |
| 597 | INIT_LIST_HEAD(&ondev->maps); |
| 598 | |
| 599 | down_write(&bpf_devs_lock); |
| 600 | err = rhashtable_insert_fast(&offdevs, &ondev->l, offdevs_params); |
| 601 | if (err) { |
| 602 | netdev_warn(netdev, "failed to register for BPF offload\n"); |
| 603 | goto err_unlock_free; |
| 604 | } |
| 605 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 606 | list_add(&ondev->offdev_netdevs, &offdev->netdevs); |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 607 | up_write(&bpf_devs_lock); |
| 608 | return 0; |
| 609 | |
| 610 | err_unlock_free: |
| 611 | up_write(&bpf_devs_lock); |
| 612 | kfree(ondev); |
| 613 | return err; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 614 | } |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 615 | EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 616 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 617 | void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, |
| 618 | struct net_device *netdev) |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 619 | { |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 620 | struct bpf_offload_netdev *ondev, *altdev; |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 621 | struct bpf_offloaded_map *offmap, *mtmp; |
| 622 | struct bpf_prog_offload *offload, *ptmp; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 623 | |
| 624 | ASSERT_RTNL(); |
| 625 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 626 | down_write(&bpf_devs_lock); |
| 627 | ondev = rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params); |
| 628 | if (WARN_ON(!ondev)) |
| 629 | goto unlock; |
Jakub Kicinski | 62c71b4 | 2017-11-20 15:21:57 -0800 | [diff] [blame] | 630 | |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 631 | WARN_ON(rhashtable_remove_fast(&offdevs, &ondev->l, offdevs_params)); |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 632 | list_del(&ondev->offdev_netdevs); |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 633 | |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 634 | /* Try to move the objects to another netdev of the device */ |
| 635 | altdev = list_first_entry_or_null(&offdev->netdevs, |
| 636 | struct bpf_offload_netdev, |
| 637 | offdev_netdevs); |
| 638 | if (altdev) { |
| 639 | list_for_each_entry(offload, &ondev->progs, offloads) |
| 640 | offload->netdev = altdev->netdev; |
| 641 | list_splice_init(&ondev->progs, &altdev->progs); |
| 642 | |
| 643 | list_for_each_entry(offmap, &ondev->maps, offloads) |
| 644 | offmap->netdev = altdev->netdev; |
| 645 | list_splice_init(&ondev->maps, &altdev->maps); |
| 646 | } else { |
| 647 | list_for_each_entry_safe(offload, ptmp, &ondev->progs, offloads) |
| 648 | __bpf_prog_offload_destroy(offload->prog); |
| 649 | list_for_each_entry_safe(offmap, mtmp, &ondev->maps, offloads) |
| 650 | __bpf_map_offload_destroy(offmap); |
| 651 | } |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 652 | |
| 653 | WARN_ON(!list_empty(&ondev->progs)); |
| 654 | WARN_ON(!list_empty(&ondev->maps)); |
| 655 | kfree(ondev); |
| 656 | unlock: |
| 657 | up_write(&bpf_devs_lock); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 658 | } |
Jakub Kicinski | 9fd7c55 | 2018-07-17 10:53:24 -0700 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister); |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 660 | |
Quentin Monnet | 1385d75 | 2018-11-09 13:03:25 +0000 | [diff] [blame] | 661 | struct bpf_offload_dev * |
| 662 | bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops) |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 663 | { |
| 664 | struct bpf_offload_dev *offdev; |
| 665 | int err; |
| 666 | |
| 667 | down_write(&bpf_devs_lock); |
| 668 | if (!offdevs_inited) { |
| 669 | err = rhashtable_init(&offdevs, &offdevs_params); |
| 670 | if (err) |
| 671 | return ERR_PTR(err); |
| 672 | offdevs_inited = true; |
| 673 | } |
| 674 | up_write(&bpf_devs_lock); |
| 675 | |
| 676 | offdev = kzalloc(sizeof(*offdev), GFP_KERNEL); |
| 677 | if (!offdev) |
| 678 | return ERR_PTR(-ENOMEM); |
| 679 | |
Quentin Monnet | 1385d75 | 2018-11-09 13:03:25 +0000 | [diff] [blame] | 680 | offdev->ops = ops; |
Jakub Kicinski | 602144c | 2018-07-17 10:53:25 -0700 | [diff] [blame] | 681 | INIT_LIST_HEAD(&offdev->netdevs); |
| 682 | |
| 683 | return offdev; |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(bpf_offload_dev_create); |
| 686 | |
| 687 | void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev) |
| 688 | { |
| 689 | WARN_ON(!list_empty(&offdev->netdevs)); |
| 690 | kfree(offdev); |
| 691 | } |
| 692 | EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy); |