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