blob: 2c88cb4ddfd8535a82936b999b376a22b5b4d718 [file] [log] [blame]
Jakub Kicinskia39e17b2017-11-27 12:10:23 -08001/*
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -07002 * Copyright (C) 2017-2018 Netronome Systems, Inc.
Jakub Kicinskia39e17b2017-11-27 12:10:23 -08003 *
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 Kicinskiab3f0062017-11-03 13:56:17 -070016#include <linux/bpf.h>
17#include <linux/bpf_verifier.h>
18#include <linux/bug.h>
Jakub Kicinski675fc272017-12-27 18:39:09 -080019#include <linux/kdev_t.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070020#include <linux/list.h>
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070021#include <linux/lockdep.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070022#include <linux/netdevice.h>
23#include <linux/printk.h>
Jakub Kicinski675fc272017-12-27 18:39:09 -080024#include <linux/proc_ns.h>
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070025#include <linux/rhashtable.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070026#include <linux/rtnetlink.h>
Jakub Kicinskie0d39742017-12-27 18:39:03 -080027#include <linux/rwsem.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070028
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070029/* Protects offdevs, members of bpf_offload_netdev and offload members
Jakub Kicinskia3884572018-01-11 20:29:09 -080030 * of all progs.
Jakub Kicinskie0d39742017-12-27 18:39:03 -080031 * RTNL lock cannot be taken when holding this lock.
32 */
33static DECLARE_RWSEM(bpf_devs_lock);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070034
Jakub Kicinski602144c2018-07-17 10:53:25 -070035struct bpf_offload_dev {
Quentin Monnet1385d752018-11-09 13:03:25 +000036 const struct bpf_prog_offload_ops *ops;
Jakub Kicinski602144c2018-07-17 10:53:25 -070037 struct list_head netdevs;
38};
39
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070040struct bpf_offload_netdev {
41 struct rhash_head l;
42 struct net_device *netdev;
Jakub Kicinski602144c2018-07-17 10:53:25 -070043 struct bpf_offload_dev *offdev;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070044 struct list_head progs;
45 struct list_head maps;
Jakub Kicinski602144c2018-07-17 10:53:25 -070046 struct list_head offdev_netdevs;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070047};
48
49static 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
57static struct rhashtable offdevs;
58static bool offdevs_inited;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070059
Jakub Kicinski5bc2d552018-01-11 20:29:08 -080060static 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 Kicinski9fd7c552018-07-17 10:53:24 -070069static struct bpf_offload_netdev *
70bpf_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 Kicinskiab3f0062017-11-03 13:56:17 -070079int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
80{
Jakub Kicinski9fd7c552018-07-17 10:53:24 -070081 struct bpf_offload_netdev *ondev;
Jakub Kicinski0a9c1992018-01-11 20:29:07 -080082 struct bpf_prog_offload *offload;
Jakub Kicinski5bc2d552018-01-11 20:29:08 -080083 int err;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070084
Jakub Kicinski649f11d2017-11-20 15:21:52 -080085 if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
86 attr->prog_type != BPF_PROG_TYPE_XDP)
87 return -EINVAL;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070088
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 Kicinskiab3f0062017-11-03 13:56:17 -070097
Jakub Kicinskie0d39742017-12-27 18:39:03 -080098 offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
99 attr->prog_ifindex);
Jakub Kicinski5bc2d552018-01-11 20:29:08 -0800100 err = bpf_dev_offload_check(offload->netdev);
101 if (err)
102 goto err_maybe_put;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700103
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800104 down_write(&bpf_devs_lock);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700105 ondev = bpf_offload_find_netdev(offload->netdev);
106 if (!ondev) {
Jakub Kicinski5bc2d552018-01-11 20:29:08 -0800107 err = -EINVAL;
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800108 goto err_unlock;
Jakub Kicinski5bc2d552018-01-11 20:29:08 -0800109 }
Quentin Monnet341b3e72018-11-09 13:03:26 +0000110 offload->offdev = ondev->offdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700111 prog->aux->offload = offload;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700112 list_add_tail(&offload->offloads, &ondev->progs);
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800113 dev_put(offload->netdev);
114 up_write(&bpf_devs_lock);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700115
116 return 0;
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800117err_unlock:
118 up_write(&bpf_devs_lock);
Jakub Kicinski5bc2d552018-01-11 20:29:08 -0800119err_maybe_put:
120 if (offload->netdev)
121 dev_put(offload->netdev);
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800122 kfree(offload);
Jakub Kicinski5bc2d552018-01-11 20:29:08 -0800123 return err;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700124}
125
126static int __bpf_offload_ndo(struct bpf_prog *prog, enum bpf_netdev_command cmd,
127 struct netdev_bpf *data)
128{
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800129 struct bpf_prog_offload *offload = prog->aux->offload;
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800130 struct net_device *netdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700131
132 ASSERT_RTNL();
133
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800134 if (!offload)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700135 return -ENODEV;
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800136 netdev = offload->netdev;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700137
138 data->command = cmd;
139
140 return netdev->netdev_ops->ndo_bpf(netdev, data);
141}
142
143int 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 Kicinskicae19272017-12-27 18:39:05 -0800155 env->prog->aux->offload->dev_ops = data.verifier.ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700156 env->prog->aux->offload->dev_state = true;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700157exit_unlock:
158 rtnl_unlock();
159 return err;
160}
161
Jakub Kicinskicae19272017-12-27 18:39:05 -0800162int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
163 int insn_idx, int prev_insn_idx)
164{
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800165 struct bpf_prog_offload *offload;
Jakub Kicinskicae19272017-12-27 18:39:05 -0800166 int ret = -ENODEV;
167
168 down_read(&bpf_devs_lock);
169 offload = env->prog->aux->offload;
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800170 if (offload)
Quentin Monnet341b3e72018-11-09 13:03:26 +0000171 ret = offload->offdev->ops->insn_hook(env, insn_idx,
172 prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800173 up_read(&bpf_devs_lock);
174
175 return ret;
176}
177
Quentin Monnetc941ce92018-10-07 12:56:47 +0100178int 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 Monnet6dc18fa2018-11-09 13:03:27 +0000186 if (offload->offdev->ops->finalize)
187 ret = offload->offdev->ops->finalize(env);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100188 else
189 ret = 0;
190 }
191 up_read(&bpf_devs_lock);
192
193 return ret;
194}
195
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700196static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
197{
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800198 struct bpf_prog_offload *offload = prog->aux->offload;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700199 struct netdev_bpf data = {};
200
201 data.offload.prog = prog;
202
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700203 if (offload->dev_state)
204 WARN_ON(__bpf_offload_ndo(prog, BPF_OFFLOAD_DESTROY, &data));
205
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800206 /* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */
207 bpf_prog_free_id(prog, true);
208
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700209 list_del_init(&offload->offloads);
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800210 kfree(offload);
211 prog->aux->offload = NULL;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700212}
213
214void bpf_prog_offload_destroy(struct bpf_prog *prog)
215{
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700216 rtnl_lock();
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800217 down_write(&bpf_devs_lock);
Jakub Kicinskice3b9db2017-12-27 18:39:06 -0800218 if (prog->aux->offload)
219 __bpf_prog_offload_destroy(prog);
Jakub Kicinskie0d39742017-12-27 18:39:03 -0800220 up_write(&bpf_devs_lock);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700221 rtnl_unlock();
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700222}
223
224static int bpf_prog_offload_translate(struct bpf_prog *prog)
225{
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700226 struct netdev_bpf data = {};
227 int ret;
228
229 data.offload.prog = prog;
230
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700231 rtnl_lock();
232 ret = __bpf_offload_ndo(prog, BPF_OFFLOAD_TRANSLATE, &data);
233 rtnl_unlock();
234
235 return ret;
236}
237
238static 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
245int 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 Kicinski675fc272017-12-27 18:39:09 -0800252struct ns_get_path_bpf_prog_args {
253 struct bpf_prog *prog;
254 struct bpf_prog_info *info;
255};
256
257static 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
283int 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 Wangfcfb1262018-01-16 16:05:19 -0800290 struct bpf_prog_aux *aux = prog->aux;
Jakub Kicinski675fc272017-12-27 18:39:09 -0800291 struct inode *ns_inode;
292 struct path ns_path;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800293 char __user *uinsns;
Jakub Kicinski675fc272017-12-27 18:39:09 -0800294 void *res;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800295 u32 ulen;
Jakub Kicinski675fc272017-12-27 18:39:09 -0800296
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 Wangfcfb1262018-01-16 16:05:19 -0800304 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 Kicinski675fc272017-12-27 18:39:09 -0800324 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 Kicinskiab3f0062017-11-03 13:56:17 -0700332const struct bpf_prog_ops bpf_offload_prog_ops = {
333};
334
Jakub Kicinskia3884572018-01-11 20:29:09 -0800335static 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
351struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
352{
353 struct net *net = current->nsproxy->net_ns;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700354 struct bpf_offload_netdev *ondev;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800355 struct bpf_offloaded_map *offmap;
356 int err;
357
358 if (!capable(CAP_SYS_ADMIN))
359 return ERR_PTR(-EPERM);
Jakub Kicinski7a0ef692018-01-17 19:13:27 -0800360 if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
361 attr->map_type != BPF_MAP_TYPE_HASH)
Jakub Kicinskia3884572018-01-11 20:29:09 -0800362 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 Kicinski9fd7c552018-07-17 10:53:24 -0700377 ondev = bpf_offload_find_netdev(offmap->netdev);
378 if (!ondev) {
379 err = -EINVAL;
380 goto err_unlock;
381 }
382
Jakub Kicinskia3884572018-01-11 20:29:09 -0800383 err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC);
384 if (err)
385 goto err_unlock;
386
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700387 list_add_tail(&offmap->offloads, &ondev->maps);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800388 up_write(&bpf_devs_lock);
389 rtnl_unlock();
390
391 return &offmap->map;
392
393err_unlock:
394 up_write(&bpf_devs_lock);
395 rtnl_unlock();
396 kfree(offmap);
397 return ERR_PTR(err);
398}
399
400static 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
409void 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
423int 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
436int 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
454int 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
467int 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 Kicinski52775b32018-01-17 19:13:28 -0800480struct ns_get_path_bpf_map_args {
481 struct bpf_offloaded_map *offmap;
482 struct bpf_map_info *info;
483};
484
485static 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
510int 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 Kicinskifd4f2272018-07-17 10:53:26 -0700535static bool __bpf_offload_dev_match(struct bpf_prog *prog,
536 struct net_device *netdev)
Jakub Kicinskia3884572018-01-11 20:29:09 -0800537{
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700538 struct bpf_offload_netdev *ondev1, *ondev2;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800539 struct bpf_prog_offload *offload;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800540
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700541 if (!bpf_prog_is_dev_bound(prog->aux))
Jakub Kicinskia3884572018-01-11 20:29:09 -0800542 return false;
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700543
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
556bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev)
557{
558 bool ret;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800559
560 down_read(&bpf_devs_lock);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700561 ret = __bpf_offload_dev_match(prog, netdev);
562 up_read(&bpf_devs_lock);
563
564 return ret;
565}
566EXPORT_SYMBOL_GPL(bpf_offload_dev_match);
567
568bool 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 Kicinskia3884572018-01-11 20:29:09 -0800575 offmap = map_to_offmap(map);
576
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700577 down_read(&bpf_devs_lock);
578 ret = __bpf_offload_dev_match(prog, offmap->netdev);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800579 up_read(&bpf_devs_lock);
580
581 return ret;
582}
583
Jakub Kicinski602144c2018-07-17 10:53:25 -0700584int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
585 struct net_device *netdev)
Jakub Kicinskia3884572018-01-11 20:29:09 -0800586{
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700587 struct bpf_offload_netdev *ondev;
588 int err;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800589
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700590 ondev = kzalloc(sizeof(*ondev), GFP_KERNEL);
591 if (!ondev)
592 return -ENOMEM;
593
594 ondev->netdev = netdev;
Jakub Kicinski602144c2018-07-17 10:53:25 -0700595 ondev->offdev = offdev;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700596 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 Kicinski602144c2018-07-17 10:53:25 -0700606 list_add(&ondev->offdev_netdevs, &offdev->netdevs);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700607 up_write(&bpf_devs_lock);
608 return 0;
609
610err_unlock_free:
611 up_write(&bpf_devs_lock);
612 kfree(ondev);
613 return err;
Jakub Kicinskia3884572018-01-11 20:29:09 -0800614}
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700615EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800616
Jakub Kicinski602144c2018-07-17 10:53:25 -0700617void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
618 struct net_device *netdev)
Jakub Kicinskia3884572018-01-11 20:29:09 -0800619{
Jakub Kicinski602144c2018-07-17 10:53:25 -0700620 struct bpf_offload_netdev *ondev, *altdev;
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700621 struct bpf_offloaded_map *offmap, *mtmp;
622 struct bpf_prog_offload *offload, *ptmp;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700623
624 ASSERT_RTNL();
625
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700626 down_write(&bpf_devs_lock);
627 ondev = rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
628 if (WARN_ON(!ondev))
629 goto unlock;
Jakub Kicinski62c71b42017-11-20 15:21:57 -0800630
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700631 WARN_ON(rhashtable_remove_fast(&offdevs, &ondev->l, offdevs_params));
Jakub Kicinski602144c2018-07-17 10:53:25 -0700632 list_del(&ondev->offdev_netdevs);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700633
Jakub Kicinski602144c2018-07-17 10:53:25 -0700634 /* 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 Kicinski9fd7c552018-07-17 10:53:24 -0700652
653 WARN_ON(!list_empty(&ondev->progs));
654 WARN_ON(!list_empty(&ondev->maps));
655 kfree(ondev);
656unlock:
657 up_write(&bpf_devs_lock);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700658}
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700659EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister);
Jakub Kicinski602144c2018-07-17 10:53:25 -0700660
Quentin Monnet1385d752018-11-09 13:03:25 +0000661struct bpf_offload_dev *
662bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops)
Jakub Kicinski602144c2018-07-17 10:53:25 -0700663{
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 Monnet1385d752018-11-09 13:03:25 +0000680 offdev->ops = ops;
Jakub Kicinski602144c2018-07-17 10:53:25 -0700681 INIT_LIST_HEAD(&offdev->netdevs);
682
683 return offdev;
684}
685EXPORT_SYMBOL_GPL(bpf_offload_dev_create);
686
687void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev)
688{
689 WARN_ON(!list_empty(&offdev->netdevs));
690 kfree(offdev);
691}
692EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy);