1 /* 2 * Copyright (C) 2017 Netronome Systems, Inc. 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 16 #include <linux/bpf.h> 17 #include <linux/bpf_verifier.h> 18 #include <linux/debugfs.h> 19 #include <linux/kernel.h> 20 #include <linux/mutex.h> 21 #include <linux/rtnetlink.h> 22 #include <net/pkt_cls.h> 23 24 #include "netdevsim.h" 25 26 #define pr_vlog(env, fmt, ...) \ 27 bpf_verifier_log_write(env, "[netdevsim] " fmt, ##__VA_ARGS__) 28 29 struct nsim_bpf_bound_prog { 30 struct netdevsim *ns; 31 struct bpf_prog *prog; 32 struct dentry *ddir; 33 const char *state; 34 bool is_loaded; 35 struct list_head l; 36 }; 37 38 #define NSIM_BPF_MAX_KEYS 2 39 40 struct nsim_bpf_bound_map { 41 struct netdevsim *ns; 42 struct bpf_offloaded_map *map; 43 struct mutex mutex; 44 struct nsim_map_entry { 45 void *key; 46 void *value; 47 } entry[NSIM_BPF_MAX_KEYS]; 48 struct list_head l; 49 }; 50 51 static int nsim_debugfs_bpf_string_read(struct seq_file *file, void *data) 52 { 53 const char **str = file->private; 54 55 if (*str) 56 seq_printf(file, "%s\n", *str); 57 58 return 0; 59 } 60 61 static int nsim_debugfs_bpf_string_open(struct inode *inode, struct file *f) 62 { 63 return single_open(f, nsim_debugfs_bpf_string_read, inode->i_private); 64 } 65 66 static const struct file_operations nsim_bpf_string_fops = { 67 .owner = THIS_MODULE, 68 .open = nsim_debugfs_bpf_string_open, 69 .release = single_release, 70 .read = seq_read, 71 .llseek = seq_lseek 72 }; 73 74 static int 75 nsim_bpf_verify_insn(struct bpf_verifier_env *env, int insn_idx, int prev_insn) 76 { 77 struct nsim_bpf_bound_prog *state; 78 79 state = env->prog->aux->offload->dev_priv; 80 if (state->ns->bpf_bind_verifier_delay && !insn_idx) 81 msleep(state->ns->bpf_bind_verifier_delay); 82 83 if (insn_idx == env->prog->len - 1) 84 pr_vlog(env, "Hello from netdevsim!\n"); 85 86 return 0; 87 } 88 89 static const struct bpf_prog_offload_ops nsim_bpf_analyzer_ops = { 90 .insn_hook = nsim_bpf_verify_insn, 91 }; 92 93 static bool nsim_xdp_offload_active(struct netdevsim *ns) 94 { 95 return ns->xdp_hw.prog; 96 } 97 98 static void nsim_prog_set_loaded(struct bpf_prog *prog, bool loaded) 99 { 100 struct nsim_bpf_bound_prog *state; 101 102 if (!prog || !prog->aux->offload) 103 return; 104 105 state = prog->aux->offload->dev_priv; 106 state->is_loaded = loaded; 107 } 108 109 static int 110 nsim_bpf_offload(struct netdevsim *ns, struct bpf_prog *prog, bool oldprog) 111 { 112 nsim_prog_set_loaded(ns->bpf_offloaded, false); 113 114 WARN(!!ns->bpf_offloaded != oldprog, 115 "bad offload state, expected offload %sto be active", 116 oldprog ? "" : "not "); 117 ns->bpf_offloaded = prog; 118 ns->bpf_offloaded_id = prog ? prog->aux->id : 0; 119 nsim_prog_set_loaded(prog, true); 120 121 return 0; 122 } 123 124 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, 125 void *type_data, void *cb_priv) 126 { 127 struct tc_cls_bpf_offload *cls_bpf = type_data; 128 struct bpf_prog *prog = cls_bpf->prog; 129 struct netdevsim *ns = cb_priv; 130 struct bpf_prog *oldprog; 131 132 if (type != TC_SETUP_CLSBPF) { 133 NSIM_EA(cls_bpf->common.extack, 134 "only offload of BPF classifiers supported"); 135 return -EOPNOTSUPP; 136 } 137 138 if (!tc_cls_can_offload_and_chain0(ns->netdev, &cls_bpf->common)) 139 return -EOPNOTSUPP; 140 141 if (cls_bpf->common.protocol != htons(ETH_P_ALL)) { 142 NSIM_EA(cls_bpf->common.extack, 143 "only ETH_P_ALL supported as filter protocol"); 144 return -EOPNOTSUPP; 145 } 146 147 if (!ns->bpf_tc_accept) { 148 NSIM_EA(cls_bpf->common.extack, 149 "netdevsim configured to reject BPF TC offload"); 150 return -EOPNOTSUPP; 151 } 152 /* Note: progs without skip_sw will probably not be dev bound */ 153 if (prog && !prog->aux->offload && !ns->bpf_tc_non_bound_accept) { 154 NSIM_EA(cls_bpf->common.extack, 155 "netdevsim configured to reject unbound programs"); 156 return -EOPNOTSUPP; 157 } 158 159 if (cls_bpf->command != TC_CLSBPF_OFFLOAD) 160 return -EOPNOTSUPP; 161 162 oldprog = cls_bpf->oldprog; 163 164 /* Don't remove if oldprog doesn't match driver's state */ 165 if (ns->bpf_offloaded != oldprog) { 166 oldprog = NULL; 167 if (!cls_bpf->prog) 168 return 0; 169 if (ns->bpf_offloaded) { 170 NSIM_EA(cls_bpf->common.extack, 171 "driver and netdev offload states mismatch"); 172 return -EBUSY; 173 } 174 } 175 176 return nsim_bpf_offload(ns, cls_bpf->prog, oldprog); 177 } 178 179 int nsim_bpf_disable_tc(struct netdevsim *ns) 180 { 181 if (ns->bpf_offloaded && !nsim_xdp_offload_active(ns)) 182 return -EBUSY; 183 return 0; 184 } 185 186 static int nsim_xdp_offload_prog(struct netdevsim *ns, struct netdev_bpf *bpf) 187 { 188 if (!nsim_xdp_offload_active(ns) && !bpf->prog) 189 return 0; 190 if (!nsim_xdp_offload_active(ns) && bpf->prog && ns->bpf_offloaded) { 191 NSIM_EA(bpf->extack, "TC program is already loaded"); 192 return -EBUSY; 193 } 194 195 return nsim_bpf_offload(ns, bpf->prog, nsim_xdp_offload_active(ns)); 196 } 197 198 static int 199 nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf, 200 struct xdp_attachment_info *xdp) 201 { 202 int err; 203 204 if (!xdp_attachment_flags_ok(xdp, bpf)) 205 return -EBUSY; 206 207 if (bpf->command == XDP_SETUP_PROG && !ns->bpf_xdpdrv_accept) { 208 NSIM_EA(bpf->extack, "driver XDP disabled in DebugFS"); 209 return -EOPNOTSUPP; 210 } 211 if (bpf->command == XDP_SETUP_PROG_HW && !ns->bpf_xdpoffload_accept) { 212 NSIM_EA(bpf->extack, "XDP offload disabled in DebugFS"); 213 return -EOPNOTSUPP; 214 } 215 216 if (bpf->command == XDP_SETUP_PROG_HW) { 217 err = nsim_xdp_offload_prog(ns, bpf); 218 if (err) 219 return err; 220 } 221 222 xdp_attachment_setup(xdp, bpf); 223 224 return 0; 225 } 226 227 static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog) 228 { 229 struct nsim_bpf_bound_prog *state; 230 char name[16]; 231 232 state = kzalloc(sizeof(*state), GFP_KERNEL); 233 if (!state) 234 return -ENOMEM; 235 236 state->ns = ns; 237 state->prog = prog; 238 state->state = "verify"; 239 240 /* Program id is not populated yet when we create the state. */ 241 sprintf(name, "%u", ns->sdev->prog_id_gen++); 242 state->ddir = debugfs_create_dir(name, ns->sdev->ddir_bpf_bound_progs); 243 if (IS_ERR_OR_NULL(state->ddir)) { 244 kfree(state); 245 return -ENOMEM; 246 } 247 248 debugfs_create_u32("id", 0400, state->ddir, &prog->aux->id); 249 debugfs_create_file("state", 0400, state->ddir, 250 &state->state, &nsim_bpf_string_fops); 251 debugfs_create_bool("loaded", 0400, state->ddir, &state->is_loaded); 252 253 list_add_tail(&state->l, &ns->sdev->bpf_bound_progs); 254 255 prog->aux->offload->dev_priv = state; 256 257 return 0; 258 } 259 260 static void nsim_bpf_destroy_prog(struct bpf_prog *prog) 261 { 262 struct nsim_bpf_bound_prog *state; 263 264 state = prog->aux->offload->dev_priv; 265 WARN(state->is_loaded, 266 "offload state destroyed while program still bound"); 267 debugfs_remove_recursive(state->ddir); 268 list_del(&state->l); 269 kfree(state); 270 } 271 272 static int nsim_setup_prog_checks(struct netdevsim *ns, struct netdev_bpf *bpf) 273 { 274 if (bpf->prog && bpf->prog->aux->offload) { 275 NSIM_EA(bpf->extack, "attempt to load offloaded prog to drv"); 276 return -EINVAL; 277 } 278 if (ns->netdev->mtu > NSIM_XDP_MAX_MTU) { 279 NSIM_EA(bpf->extack, "MTU too large w/ XDP enabled"); 280 return -EINVAL; 281 } 282 return 0; 283 } 284 285 static int 286 nsim_setup_prog_hw_checks(struct netdevsim *ns, struct netdev_bpf *bpf) 287 { 288 struct nsim_bpf_bound_prog *state; 289 290 if (!bpf->prog) 291 return 0; 292 293 if (!bpf->prog->aux->offload) { 294 NSIM_EA(bpf->extack, "xdpoffload of non-bound program"); 295 return -EINVAL; 296 } 297 if (!bpf_offload_dev_match(bpf->prog, ns->netdev)) { 298 NSIM_EA(bpf->extack, "program bound to different dev"); 299 return -EINVAL; 300 } 301 302 state = bpf->prog->aux->offload->dev_priv; 303 if (WARN_ON(strcmp(state->state, "xlated"))) { 304 NSIM_EA(bpf->extack, "offloading program in bad state"); 305 return -EINVAL; 306 } 307 return 0; 308 } 309 310 static bool 311 nsim_map_key_match(struct bpf_map *map, struct nsim_map_entry *e, void *key) 312 { 313 return e->key && !memcmp(key, e->key, map->key_size); 314 } 315 316 static int nsim_map_key_find(struct bpf_offloaded_map *offmap, void *key) 317 { 318 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 319 unsigned int i; 320 321 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) 322 if (nsim_map_key_match(&offmap->map, &nmap->entry[i], key)) 323 return i; 324 325 return -ENOENT; 326 } 327 328 static int 329 nsim_map_alloc_elem(struct bpf_offloaded_map *offmap, unsigned int idx) 330 { 331 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 332 333 nmap->entry[idx].key = kmalloc(offmap->map.key_size, GFP_USER); 334 if (!nmap->entry[idx].key) 335 return -ENOMEM; 336 nmap->entry[idx].value = kmalloc(offmap->map.value_size, GFP_USER); 337 if (!nmap->entry[idx].value) { 338 kfree(nmap->entry[idx].key); 339 nmap->entry[idx].key = NULL; 340 return -ENOMEM; 341 } 342 343 return 0; 344 } 345 346 static int 347 nsim_map_get_next_key(struct bpf_offloaded_map *offmap, 348 void *key, void *next_key) 349 { 350 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 351 int idx = -ENOENT; 352 353 mutex_lock(&nmap->mutex); 354 355 if (key) 356 idx = nsim_map_key_find(offmap, key); 357 if (idx == -ENOENT) 358 idx = 0; 359 else 360 idx++; 361 362 for (; idx < ARRAY_SIZE(nmap->entry); idx++) { 363 if (nmap->entry[idx].key) { 364 memcpy(next_key, nmap->entry[idx].key, 365 offmap->map.key_size); 366 break; 367 } 368 } 369 370 mutex_unlock(&nmap->mutex); 371 372 if (idx == ARRAY_SIZE(nmap->entry)) 373 return -ENOENT; 374 return 0; 375 } 376 377 static int 378 nsim_map_lookup_elem(struct bpf_offloaded_map *offmap, void *key, void *value) 379 { 380 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 381 int idx; 382 383 mutex_lock(&nmap->mutex); 384 385 idx = nsim_map_key_find(offmap, key); 386 if (idx >= 0) 387 memcpy(value, nmap->entry[idx].value, offmap->map.value_size); 388 389 mutex_unlock(&nmap->mutex); 390 391 return idx < 0 ? idx : 0; 392 } 393 394 static int 395 nsim_map_update_elem(struct bpf_offloaded_map *offmap, 396 void *key, void *value, u64 flags) 397 { 398 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 399 int idx, err = 0; 400 401 mutex_lock(&nmap->mutex); 402 403 idx = nsim_map_key_find(offmap, key); 404 if (idx < 0 && flags == BPF_EXIST) { 405 err = idx; 406 goto exit_unlock; 407 } 408 if (idx >= 0 && flags == BPF_NOEXIST) { 409 err = -EEXIST; 410 goto exit_unlock; 411 } 412 413 if (idx < 0) { 414 for (idx = 0; idx < ARRAY_SIZE(nmap->entry); idx++) 415 if (!nmap->entry[idx].key) 416 break; 417 if (idx == ARRAY_SIZE(nmap->entry)) { 418 err = -E2BIG; 419 goto exit_unlock; 420 } 421 422 err = nsim_map_alloc_elem(offmap, idx); 423 if (err) 424 goto exit_unlock; 425 } 426 427 memcpy(nmap->entry[idx].key, key, offmap->map.key_size); 428 memcpy(nmap->entry[idx].value, value, offmap->map.value_size); 429 exit_unlock: 430 mutex_unlock(&nmap->mutex); 431 432 return err; 433 } 434 435 static int nsim_map_delete_elem(struct bpf_offloaded_map *offmap, void *key) 436 { 437 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 438 int idx; 439 440 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY) 441 return -EINVAL; 442 443 mutex_lock(&nmap->mutex); 444 445 idx = nsim_map_key_find(offmap, key); 446 if (idx >= 0) { 447 kfree(nmap->entry[idx].key); 448 kfree(nmap->entry[idx].value); 449 memset(&nmap->entry[idx], 0, sizeof(nmap->entry[idx])); 450 } 451 452 mutex_unlock(&nmap->mutex); 453 454 return idx < 0 ? idx : 0; 455 } 456 457 static const struct bpf_map_dev_ops nsim_bpf_map_ops = { 458 .map_get_next_key = nsim_map_get_next_key, 459 .map_lookup_elem = nsim_map_lookup_elem, 460 .map_update_elem = nsim_map_update_elem, 461 .map_delete_elem = nsim_map_delete_elem, 462 }; 463 464 static int 465 nsim_bpf_map_alloc(struct netdevsim *ns, struct bpf_offloaded_map *offmap) 466 { 467 struct nsim_bpf_bound_map *nmap; 468 int i, err; 469 470 if (WARN_ON(offmap->map.map_type != BPF_MAP_TYPE_ARRAY && 471 offmap->map.map_type != BPF_MAP_TYPE_HASH)) 472 return -EINVAL; 473 if (offmap->map.max_entries > NSIM_BPF_MAX_KEYS) 474 return -ENOMEM; 475 if (offmap->map.map_flags) 476 return -EINVAL; 477 478 nmap = kzalloc(sizeof(*nmap), GFP_USER); 479 if (!nmap) 480 return -ENOMEM; 481 482 offmap->dev_priv = nmap; 483 nmap->ns = ns; 484 nmap->map = offmap; 485 mutex_init(&nmap->mutex); 486 487 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY) { 488 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) { 489 u32 *key; 490 491 err = nsim_map_alloc_elem(offmap, i); 492 if (err) 493 goto err_free; 494 key = nmap->entry[i].key; 495 *key = i; 496 } 497 } 498 499 offmap->dev_ops = &nsim_bpf_map_ops; 500 list_add_tail(&nmap->l, &ns->sdev->bpf_bound_maps); 501 502 return 0; 503 504 err_free: 505 while (--i >= 0) { 506 kfree(nmap->entry[i].key); 507 kfree(nmap->entry[i].value); 508 } 509 kfree(nmap); 510 return err; 511 } 512 513 static void nsim_bpf_map_free(struct bpf_offloaded_map *offmap) 514 { 515 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 516 unsigned int i; 517 518 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) { 519 kfree(nmap->entry[i].key); 520 kfree(nmap->entry[i].value); 521 } 522 list_del_init(&nmap->l); 523 mutex_destroy(&nmap->mutex); 524 kfree(nmap); 525 } 526 527 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) 528 { 529 struct netdevsim *ns = netdev_priv(dev); 530 struct nsim_bpf_bound_prog *state; 531 int err; 532 533 ASSERT_RTNL(); 534 535 switch (bpf->command) { 536 case BPF_OFFLOAD_VERIFIER_PREP: 537 if (!ns->bpf_bind_accept) 538 return -EOPNOTSUPP; 539 540 err = nsim_bpf_create_prog(ns, bpf->verifier.prog); 541 if (err) 542 return err; 543 544 bpf->verifier.ops = &nsim_bpf_analyzer_ops; 545 return 0; 546 case BPF_OFFLOAD_TRANSLATE: 547 state = bpf->offload.prog->aux->offload->dev_priv; 548 549 state->state = "xlated"; 550 return 0; 551 case BPF_OFFLOAD_DESTROY: 552 nsim_bpf_destroy_prog(bpf->offload.prog); 553 return 0; 554 case XDP_QUERY_PROG: 555 return xdp_attachment_query(&ns->xdp, bpf); 556 case XDP_QUERY_PROG_HW: 557 return xdp_attachment_query(&ns->xdp_hw, bpf); 558 case XDP_SETUP_PROG: 559 err = nsim_setup_prog_checks(ns, bpf); 560 if (err) 561 return err; 562 563 return nsim_xdp_set_prog(ns, bpf, &ns->xdp); 564 case XDP_SETUP_PROG_HW: 565 err = nsim_setup_prog_hw_checks(ns, bpf); 566 if (err) 567 return err; 568 569 return nsim_xdp_set_prog(ns, bpf, &ns->xdp_hw); 570 case BPF_OFFLOAD_MAP_ALLOC: 571 if (!ns->bpf_map_accept) 572 return -EOPNOTSUPP; 573 574 return nsim_bpf_map_alloc(ns, bpf->offmap); 575 case BPF_OFFLOAD_MAP_FREE: 576 nsim_bpf_map_free(bpf->offmap); 577 return 0; 578 default: 579 return -EINVAL; 580 } 581 } 582 583 int nsim_bpf_init(struct netdevsim *ns) 584 { 585 int err; 586 587 if (ns->sdev->refcnt == 1) { 588 INIT_LIST_HEAD(&ns->sdev->bpf_bound_progs); 589 INIT_LIST_HEAD(&ns->sdev->bpf_bound_maps); 590 591 ns->sdev->ddir_bpf_bound_progs = 592 debugfs_create_dir("bpf_bound_progs", ns->sdev->ddir); 593 if (IS_ERR_OR_NULL(ns->sdev->ddir_bpf_bound_progs)) 594 return -ENOMEM; 595 596 ns->sdev->bpf_dev = bpf_offload_dev_create(); 597 err = PTR_ERR_OR_ZERO(ns->sdev->bpf_dev); 598 if (err) 599 return err; 600 } 601 602 err = bpf_offload_dev_netdev_register(ns->sdev->bpf_dev, ns->netdev); 603 if (err) 604 goto err_destroy_bdev; 605 606 debugfs_create_u32("bpf_offloaded_id", 0400, ns->ddir, 607 &ns->bpf_offloaded_id); 608 609 ns->bpf_bind_accept = true; 610 debugfs_create_bool("bpf_bind_accept", 0600, ns->ddir, 611 &ns->bpf_bind_accept); 612 debugfs_create_u32("bpf_bind_verifier_delay", 0600, ns->ddir, 613 &ns->bpf_bind_verifier_delay); 614 615 ns->bpf_tc_accept = true; 616 debugfs_create_bool("bpf_tc_accept", 0600, ns->ddir, 617 &ns->bpf_tc_accept); 618 debugfs_create_bool("bpf_tc_non_bound_accept", 0600, ns->ddir, 619 &ns->bpf_tc_non_bound_accept); 620 ns->bpf_xdpdrv_accept = true; 621 debugfs_create_bool("bpf_xdpdrv_accept", 0600, ns->ddir, 622 &ns->bpf_xdpdrv_accept); 623 ns->bpf_xdpoffload_accept = true; 624 debugfs_create_bool("bpf_xdpoffload_accept", 0600, ns->ddir, 625 &ns->bpf_xdpoffload_accept); 626 627 ns->bpf_map_accept = true; 628 debugfs_create_bool("bpf_map_accept", 0600, ns->ddir, 629 &ns->bpf_map_accept); 630 631 return 0; 632 633 err_destroy_bdev: 634 if (ns->sdev->refcnt == 1) 635 bpf_offload_dev_destroy(ns->sdev->bpf_dev); 636 return err; 637 } 638 639 void nsim_bpf_uninit(struct netdevsim *ns) 640 { 641 WARN_ON(ns->xdp.prog); 642 WARN_ON(ns->xdp_hw.prog); 643 WARN_ON(ns->bpf_offloaded); 644 bpf_offload_dev_netdev_unregister(ns->sdev->bpf_dev, ns->netdev); 645 646 if (ns->sdev->refcnt == 1) { 647 WARN_ON(!list_empty(&ns->sdev->bpf_bound_progs)); 648 WARN_ON(!list_empty(&ns->sdev->bpf_bound_maps)); 649 bpf_offload_dev_destroy(ns->sdev->bpf_dev); 650 } 651 } 652