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 nsim_dev *nsim_dev; 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->nsim_dev->bpf_bind_verifier_delay && !insn_idx) 69 msleep(state->nsim_dev->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 nsim_dev *nsim_dev, 217 struct bpf_prog *prog) 218 { 219 struct nsim_bpf_bound_prog *state; 220 char name[16]; 221 int ret; 222 223 state = kzalloc(sizeof(*state), GFP_KERNEL); 224 if (!state) 225 return -ENOMEM; 226 227 state->nsim_dev = nsim_dev; 228 state->prog = prog; 229 state->state = "verify"; 230 231 /* Program id is not populated yet when we create the state. */ 232 sprintf(name, "%u", nsim_dev->prog_id_gen++); 233 state->ddir = debugfs_create_dir(name, nsim_dev->ddir_bpf_bound_progs); 234 if (IS_ERR(state->ddir)) { 235 ret = PTR_ERR(state->ddir); 236 kfree(state); 237 return ret; 238 } 239 240 debugfs_create_u32("id", 0400, state->ddir, &prog->aux->id); 241 debugfs_create_file("state", 0400, state->ddir, 242 &state->state, &nsim_bpf_string_fops); 243 debugfs_create_bool("loaded", 0400, state->ddir, &state->is_loaded); 244 245 list_add_tail(&state->l, &nsim_dev->bpf_bound_progs); 246 247 prog->aux->offload->dev_priv = state; 248 249 return 0; 250 } 251 252 static int nsim_bpf_verifier_prep(struct bpf_prog *prog) 253 { 254 struct nsim_dev *nsim_dev = 255 bpf_offload_dev_priv(prog->aux->offload->offdev); 256 257 if (!nsim_dev->bpf_bind_accept) 258 return -EOPNOTSUPP; 259 260 return nsim_bpf_create_prog(nsim_dev, prog); 261 } 262 263 static int nsim_bpf_translate(struct bpf_prog *prog) 264 { 265 struct nsim_bpf_bound_prog *state = prog->aux->offload->dev_priv; 266 267 state->state = "xlated"; 268 return 0; 269 } 270 271 static void nsim_bpf_destroy_prog(struct bpf_prog *prog) 272 { 273 struct nsim_bpf_bound_prog *state; 274 275 state = prog->aux->offload->dev_priv; 276 WARN(state->is_loaded, 277 "offload state destroyed while program still bound"); 278 debugfs_remove_recursive(state->ddir); 279 list_del(&state->l); 280 kfree(state); 281 } 282 283 static const struct bpf_prog_offload_ops nsim_bpf_dev_ops = { 284 .insn_hook = nsim_bpf_verify_insn, 285 .finalize = nsim_bpf_finalize, 286 .prepare = nsim_bpf_verifier_prep, 287 .translate = nsim_bpf_translate, 288 .destroy = nsim_bpf_destroy_prog, 289 }; 290 291 static int nsim_setup_prog_checks(struct netdevsim *ns, struct netdev_bpf *bpf) 292 { 293 if (bpf->prog && bpf->prog->aux->offload) { 294 NSIM_EA(bpf->extack, "attempt to load offloaded prog to drv"); 295 return -EINVAL; 296 } 297 if (ns->netdev->mtu > NSIM_XDP_MAX_MTU) { 298 NSIM_EA(bpf->extack, "MTU too large w/ XDP enabled"); 299 return -EINVAL; 300 } 301 return 0; 302 } 303 304 static int 305 nsim_setup_prog_hw_checks(struct netdevsim *ns, struct netdev_bpf *bpf) 306 { 307 struct nsim_bpf_bound_prog *state; 308 309 if (!bpf->prog) 310 return 0; 311 312 if (!bpf->prog->aux->offload) { 313 NSIM_EA(bpf->extack, "xdpoffload of non-bound program"); 314 return -EINVAL; 315 } 316 if (!bpf_offload_dev_match(bpf->prog, ns->netdev)) { 317 NSIM_EA(bpf->extack, "program bound to different dev"); 318 return -EINVAL; 319 } 320 321 state = bpf->prog->aux->offload->dev_priv; 322 if (WARN_ON(strcmp(state->state, "xlated"))) { 323 NSIM_EA(bpf->extack, "offloading program in bad state"); 324 return -EINVAL; 325 } 326 return 0; 327 } 328 329 static bool 330 nsim_map_key_match(struct bpf_map *map, struct nsim_map_entry *e, void *key) 331 { 332 return e->key && !memcmp(key, e->key, map->key_size); 333 } 334 335 static int nsim_map_key_find(struct bpf_offloaded_map *offmap, void *key) 336 { 337 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 338 unsigned int i; 339 340 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) 341 if (nsim_map_key_match(&offmap->map, &nmap->entry[i], key)) 342 return i; 343 344 return -ENOENT; 345 } 346 347 static int 348 nsim_map_alloc_elem(struct bpf_offloaded_map *offmap, unsigned int idx) 349 { 350 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 351 352 nmap->entry[idx].key = kmalloc(offmap->map.key_size, GFP_USER); 353 if (!nmap->entry[idx].key) 354 return -ENOMEM; 355 nmap->entry[idx].value = kmalloc(offmap->map.value_size, GFP_USER); 356 if (!nmap->entry[idx].value) { 357 kfree(nmap->entry[idx].key); 358 nmap->entry[idx].key = NULL; 359 return -ENOMEM; 360 } 361 362 return 0; 363 } 364 365 static int 366 nsim_map_get_next_key(struct bpf_offloaded_map *offmap, 367 void *key, void *next_key) 368 { 369 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 370 int idx = -ENOENT; 371 372 mutex_lock(&nmap->mutex); 373 374 if (key) 375 idx = nsim_map_key_find(offmap, key); 376 if (idx == -ENOENT) 377 idx = 0; 378 else 379 idx++; 380 381 for (; idx < ARRAY_SIZE(nmap->entry); idx++) { 382 if (nmap->entry[idx].key) { 383 memcpy(next_key, nmap->entry[idx].key, 384 offmap->map.key_size); 385 break; 386 } 387 } 388 389 mutex_unlock(&nmap->mutex); 390 391 if (idx == ARRAY_SIZE(nmap->entry)) 392 return -ENOENT; 393 return 0; 394 } 395 396 static int 397 nsim_map_lookup_elem(struct bpf_offloaded_map *offmap, void *key, void *value) 398 { 399 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 400 int idx; 401 402 mutex_lock(&nmap->mutex); 403 404 idx = nsim_map_key_find(offmap, key); 405 if (idx >= 0) 406 memcpy(value, nmap->entry[idx].value, offmap->map.value_size); 407 408 mutex_unlock(&nmap->mutex); 409 410 return idx < 0 ? idx : 0; 411 } 412 413 static int 414 nsim_map_update_elem(struct bpf_offloaded_map *offmap, 415 void *key, void *value, u64 flags) 416 { 417 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 418 int idx, err = 0; 419 420 mutex_lock(&nmap->mutex); 421 422 idx = nsim_map_key_find(offmap, key); 423 if (idx < 0 && flags == BPF_EXIST) { 424 err = idx; 425 goto exit_unlock; 426 } 427 if (idx >= 0 && flags == BPF_NOEXIST) { 428 err = -EEXIST; 429 goto exit_unlock; 430 } 431 432 if (idx < 0) { 433 for (idx = 0; idx < ARRAY_SIZE(nmap->entry); idx++) 434 if (!nmap->entry[idx].key) 435 break; 436 if (idx == ARRAY_SIZE(nmap->entry)) { 437 err = -E2BIG; 438 goto exit_unlock; 439 } 440 441 err = nsim_map_alloc_elem(offmap, idx); 442 if (err) 443 goto exit_unlock; 444 } 445 446 memcpy(nmap->entry[idx].key, key, offmap->map.key_size); 447 memcpy(nmap->entry[idx].value, value, offmap->map.value_size); 448 exit_unlock: 449 mutex_unlock(&nmap->mutex); 450 451 return err; 452 } 453 454 static int nsim_map_delete_elem(struct bpf_offloaded_map *offmap, void *key) 455 { 456 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 457 int idx; 458 459 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY) 460 return -EINVAL; 461 462 mutex_lock(&nmap->mutex); 463 464 idx = nsim_map_key_find(offmap, key); 465 if (idx >= 0) { 466 kfree(nmap->entry[idx].key); 467 kfree(nmap->entry[idx].value); 468 memset(&nmap->entry[idx], 0, sizeof(nmap->entry[idx])); 469 } 470 471 mutex_unlock(&nmap->mutex); 472 473 return idx < 0 ? idx : 0; 474 } 475 476 static const struct bpf_map_dev_ops nsim_bpf_map_ops = { 477 .map_get_next_key = nsim_map_get_next_key, 478 .map_lookup_elem = nsim_map_lookup_elem, 479 .map_update_elem = nsim_map_update_elem, 480 .map_delete_elem = nsim_map_delete_elem, 481 }; 482 483 static int 484 nsim_bpf_map_alloc(struct netdevsim *ns, struct bpf_offloaded_map *offmap) 485 { 486 struct nsim_bpf_bound_map *nmap; 487 int i, err; 488 489 if (WARN_ON(offmap->map.map_type != BPF_MAP_TYPE_ARRAY && 490 offmap->map.map_type != BPF_MAP_TYPE_HASH)) 491 return -EINVAL; 492 if (offmap->map.max_entries > NSIM_BPF_MAX_KEYS) 493 return -ENOMEM; 494 if (offmap->map.map_flags) 495 return -EINVAL; 496 497 nmap = kzalloc(sizeof(*nmap), GFP_USER); 498 if (!nmap) 499 return -ENOMEM; 500 501 offmap->dev_priv = nmap; 502 nmap->ns = ns; 503 nmap->map = offmap; 504 mutex_init(&nmap->mutex); 505 506 if (offmap->map.map_type == BPF_MAP_TYPE_ARRAY) { 507 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) { 508 u32 *key; 509 510 err = nsim_map_alloc_elem(offmap, i); 511 if (err) 512 goto err_free; 513 key = nmap->entry[i].key; 514 *key = i; 515 } 516 } 517 518 offmap->dev_ops = &nsim_bpf_map_ops; 519 list_add_tail(&nmap->l, &ns->nsim_dev->bpf_bound_maps); 520 521 return 0; 522 523 err_free: 524 while (--i >= 0) { 525 kfree(nmap->entry[i].key); 526 kfree(nmap->entry[i].value); 527 } 528 kfree(nmap); 529 return err; 530 } 531 532 static void nsim_bpf_map_free(struct bpf_offloaded_map *offmap) 533 { 534 struct nsim_bpf_bound_map *nmap = offmap->dev_priv; 535 unsigned int i; 536 537 for (i = 0; i < ARRAY_SIZE(nmap->entry); i++) { 538 kfree(nmap->entry[i].key); 539 kfree(nmap->entry[i].value); 540 } 541 list_del_init(&nmap->l); 542 mutex_destroy(&nmap->mutex); 543 kfree(nmap); 544 } 545 546 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) 547 { 548 struct netdevsim *ns = netdev_priv(dev); 549 int err; 550 551 ASSERT_RTNL(); 552 553 switch (bpf->command) { 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_dev_init(struct nsim_dev *nsim_dev) 580 { 581 int err; 582 583 INIT_LIST_HEAD(&nsim_dev->bpf_bound_progs); 584 INIT_LIST_HEAD(&nsim_dev->bpf_bound_maps); 585 586 nsim_dev->ddir_bpf_bound_progs = debugfs_create_dir("bpf_bound_progs", 587 nsim_dev->ddir); 588 if (IS_ERR(nsim_dev->ddir_bpf_bound_progs)) 589 return PTR_ERR(nsim_dev->ddir_bpf_bound_progs); 590 591 nsim_dev->bpf_dev = bpf_offload_dev_create(&nsim_bpf_dev_ops, nsim_dev); 592 err = PTR_ERR_OR_ZERO(nsim_dev->bpf_dev); 593 if (err) 594 return err; 595 596 nsim_dev->bpf_bind_accept = true; 597 debugfs_create_bool("bpf_bind_accept", 0600, nsim_dev->ddir, 598 &nsim_dev->bpf_bind_accept); 599 debugfs_create_u32("bpf_bind_verifier_delay", 0600, nsim_dev->ddir, 600 &nsim_dev->bpf_bind_verifier_delay); 601 return 0; 602 } 603 604 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev) 605 { 606 WARN_ON(!list_empty(&nsim_dev->bpf_bound_progs)); 607 WARN_ON(!list_empty(&nsim_dev->bpf_bound_maps)); 608 bpf_offload_dev_destroy(nsim_dev->bpf_dev); 609 } 610 611 int nsim_bpf_init(struct netdevsim *ns) 612 { 613 struct dentry *ddir = ns->nsim_dev_port->ddir; 614 int err; 615 616 err = bpf_offload_dev_netdev_register(ns->nsim_dev->bpf_dev, 617 ns->netdev); 618 if (err) 619 return err; 620 621 debugfs_create_u32("bpf_offloaded_id", 0400, ddir, 622 &ns->bpf_offloaded_id); 623 624 ns->bpf_tc_accept = true; 625 debugfs_create_bool("bpf_tc_accept", 0600, ddir, 626 &ns->bpf_tc_accept); 627 debugfs_create_bool("bpf_tc_non_bound_accept", 0600, ddir, 628 &ns->bpf_tc_non_bound_accept); 629 ns->bpf_xdpdrv_accept = true; 630 debugfs_create_bool("bpf_xdpdrv_accept", 0600, ddir, 631 &ns->bpf_xdpdrv_accept); 632 ns->bpf_xdpoffload_accept = true; 633 debugfs_create_bool("bpf_xdpoffload_accept", 0600, ddir, 634 &ns->bpf_xdpoffload_accept); 635 636 ns->bpf_map_accept = true; 637 debugfs_create_bool("bpf_map_accept", 0600, ddir, 638 &ns->bpf_map_accept); 639 640 return 0; 641 } 642 643 void nsim_bpf_uninit(struct netdevsim *ns) 644 { 645 WARN_ON(ns->xdp.prog); 646 WARN_ON(ns->xdp_hw.prog); 647 WARN_ON(ns->bpf_offloaded); 648 bpf_offload_dev_netdev_unregister(ns->nsim_dev->bpf_dev, ns->netdev); 649 } 650