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/debugfs.h> 17 #include <linux/etherdevice.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/netdevice.h> 21 #include <linux/slab.h> 22 #include <net/netdev_queues.h> 23 #include <net/page_pool/helpers.h> 24 #include <net/netlink.h> 25 #include <net/pkt_cls.h> 26 #include <net/rtnetlink.h> 27 #include <net/udp_tunnel.h> 28 29 #include "netdevsim.h" 30 31 #define NSIM_RING_SIZE 256 32 33 static int nsim_napi_rx(struct nsim_rq *rq, struct sk_buff *skb) 34 { 35 if (skb_queue_len(&rq->skb_queue) > NSIM_RING_SIZE) { 36 dev_kfree_skb_any(skb); 37 return NET_RX_DROP; 38 } 39 40 skb_queue_tail(&rq->skb_queue, skb); 41 return NET_RX_SUCCESS; 42 } 43 44 static int nsim_forward_skb(struct net_device *dev, struct sk_buff *skb, 45 struct nsim_rq *rq) 46 { 47 return __dev_forward_skb(dev, skb) ?: nsim_napi_rx(rq, skb); 48 } 49 50 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 51 { 52 struct netdevsim *ns = netdev_priv(dev); 53 struct net_device *peer_dev; 54 unsigned int len = skb->len; 55 struct netdevsim *peer_ns; 56 struct nsim_rq *rq; 57 int rxq; 58 59 rcu_read_lock(); 60 if (!nsim_ipsec_tx(ns, skb)) 61 goto out_drop_free; 62 63 peer_ns = rcu_dereference(ns->peer); 64 if (!peer_ns) 65 goto out_drop_free; 66 67 peer_dev = peer_ns->netdev; 68 rxq = skb_get_queue_mapping(skb); 69 if (rxq >= peer_dev->num_rx_queues) 70 rxq = rxq % peer_dev->num_rx_queues; 71 rq = &peer_ns->rq[rxq]; 72 73 skb_tx_timestamp(skb); 74 if (unlikely(nsim_forward_skb(peer_dev, skb, rq) == NET_RX_DROP)) 75 goto out_drop_cnt; 76 77 napi_schedule(&rq->napi); 78 79 rcu_read_unlock(); 80 u64_stats_update_begin(&ns->syncp); 81 ns->tx_packets++; 82 ns->tx_bytes += len; 83 u64_stats_update_end(&ns->syncp); 84 return NETDEV_TX_OK; 85 86 out_drop_free: 87 dev_kfree_skb(skb); 88 out_drop_cnt: 89 rcu_read_unlock(); 90 u64_stats_update_begin(&ns->syncp); 91 ns->tx_dropped++; 92 u64_stats_update_end(&ns->syncp); 93 return NETDEV_TX_OK; 94 } 95 96 static void nsim_set_rx_mode(struct net_device *dev) 97 { 98 } 99 100 static int nsim_change_mtu(struct net_device *dev, int new_mtu) 101 { 102 struct netdevsim *ns = netdev_priv(dev); 103 104 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU) 105 return -EBUSY; 106 107 WRITE_ONCE(dev->mtu, new_mtu); 108 109 return 0; 110 } 111 112 static void 113 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 114 { 115 struct netdevsim *ns = netdev_priv(dev); 116 unsigned int start; 117 118 do { 119 start = u64_stats_fetch_begin(&ns->syncp); 120 stats->tx_bytes = ns->tx_bytes; 121 stats->tx_packets = ns->tx_packets; 122 stats->tx_dropped = ns->tx_dropped; 123 } while (u64_stats_fetch_retry(&ns->syncp, start)); 124 } 125 126 static int 127 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv) 128 { 129 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv); 130 } 131 132 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac) 133 { 134 struct netdevsim *ns = netdev_priv(dev); 135 struct nsim_dev *nsim_dev = ns->nsim_dev; 136 137 /* Only refuse multicast addresses, zero address can mean unset/any. */ 138 if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac)) 139 return -EINVAL; 140 memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN); 141 142 return 0; 143 } 144 145 static int nsim_set_vf_vlan(struct net_device *dev, int vf, 146 u16 vlan, u8 qos, __be16 vlan_proto) 147 { 148 struct netdevsim *ns = netdev_priv(dev); 149 struct nsim_dev *nsim_dev = ns->nsim_dev; 150 151 if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7) 152 return -EINVAL; 153 154 nsim_dev->vfconfigs[vf].vlan = vlan; 155 nsim_dev->vfconfigs[vf].qos = qos; 156 nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto; 157 158 return 0; 159 } 160 161 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max) 162 { 163 struct netdevsim *ns = netdev_priv(dev); 164 struct nsim_dev *nsim_dev = ns->nsim_dev; 165 166 if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) { 167 pr_err("Not supported in switchdev mode. Please use devlink API.\n"); 168 return -EOPNOTSUPP; 169 } 170 171 if (vf >= nsim_dev_get_vfs(nsim_dev)) 172 return -EINVAL; 173 174 nsim_dev->vfconfigs[vf].min_tx_rate = min; 175 nsim_dev->vfconfigs[vf].max_tx_rate = max; 176 177 return 0; 178 } 179 180 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val) 181 { 182 struct netdevsim *ns = netdev_priv(dev); 183 struct nsim_dev *nsim_dev = ns->nsim_dev; 184 185 if (vf >= nsim_dev_get_vfs(nsim_dev)) 186 return -EINVAL; 187 nsim_dev->vfconfigs[vf].spoofchk_enabled = val; 188 189 return 0; 190 } 191 192 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val) 193 { 194 struct netdevsim *ns = netdev_priv(dev); 195 struct nsim_dev *nsim_dev = ns->nsim_dev; 196 197 if (vf >= nsim_dev_get_vfs(nsim_dev)) 198 return -EINVAL; 199 nsim_dev->vfconfigs[vf].rss_query_enabled = val; 200 201 return 0; 202 } 203 204 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val) 205 { 206 struct netdevsim *ns = netdev_priv(dev); 207 struct nsim_dev *nsim_dev = ns->nsim_dev; 208 209 if (vf >= nsim_dev_get_vfs(nsim_dev)) 210 return -EINVAL; 211 nsim_dev->vfconfigs[vf].trusted = val; 212 213 return 0; 214 } 215 216 static int 217 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi) 218 { 219 struct netdevsim *ns = netdev_priv(dev); 220 struct nsim_dev *nsim_dev = ns->nsim_dev; 221 222 if (vf >= nsim_dev_get_vfs(nsim_dev)) 223 return -EINVAL; 224 225 ivi->vf = vf; 226 ivi->linkstate = nsim_dev->vfconfigs[vf].link_state; 227 ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate; 228 ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate; 229 ivi->vlan = nsim_dev->vfconfigs[vf].vlan; 230 ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto; 231 ivi->qos = nsim_dev->vfconfigs[vf].qos; 232 memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN); 233 ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled; 234 ivi->trusted = nsim_dev->vfconfigs[vf].trusted; 235 ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled; 236 237 return 0; 238 } 239 240 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state) 241 { 242 struct netdevsim *ns = netdev_priv(dev); 243 struct nsim_dev *nsim_dev = ns->nsim_dev; 244 245 if (vf >= nsim_dev_get_vfs(nsim_dev)) 246 return -EINVAL; 247 248 switch (state) { 249 case IFLA_VF_LINK_STATE_AUTO: 250 case IFLA_VF_LINK_STATE_ENABLE: 251 case IFLA_VF_LINK_STATE_DISABLE: 252 break; 253 default: 254 return -EINVAL; 255 } 256 257 nsim_dev->vfconfigs[vf].link_state = state; 258 259 return 0; 260 } 261 262 static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats) 263 { 264 stats->window_drops = 0; 265 stats->tx_overruns = 0; 266 } 267 268 static int nsim_setup_tc_taprio(struct net_device *dev, 269 struct tc_taprio_qopt_offload *offload) 270 { 271 int err = 0; 272 273 switch (offload->cmd) { 274 case TAPRIO_CMD_REPLACE: 275 case TAPRIO_CMD_DESTROY: 276 break; 277 case TAPRIO_CMD_STATS: 278 nsim_taprio_stats(&offload->stats); 279 break; 280 default: 281 err = -EOPNOTSUPP; 282 } 283 284 return err; 285 } 286 287 static LIST_HEAD(nsim_block_cb_list); 288 289 static int 290 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data) 291 { 292 struct netdevsim *ns = netdev_priv(dev); 293 294 switch (type) { 295 case TC_SETUP_QDISC_TAPRIO: 296 return nsim_setup_tc_taprio(dev, type_data); 297 case TC_SETUP_BLOCK: 298 return flow_block_cb_setup_simple(type_data, 299 &nsim_block_cb_list, 300 nsim_setup_tc_block_cb, 301 ns, ns, true); 302 default: 303 return -EOPNOTSUPP; 304 } 305 } 306 307 static int 308 nsim_set_features(struct net_device *dev, netdev_features_t features) 309 { 310 struct netdevsim *ns = netdev_priv(dev); 311 312 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) 313 return nsim_bpf_disable_tc(ns); 314 315 return 0; 316 } 317 318 static int nsim_get_iflink(const struct net_device *dev) 319 { 320 struct netdevsim *nsim, *peer; 321 int iflink; 322 323 nsim = netdev_priv(dev); 324 325 rcu_read_lock(); 326 peer = rcu_dereference(nsim->peer); 327 iflink = peer ? READ_ONCE(peer->netdev->ifindex) : 0; 328 rcu_read_unlock(); 329 330 return iflink; 331 } 332 333 static int nsim_rcv(struct nsim_rq *rq, int budget) 334 { 335 struct sk_buff *skb; 336 int i; 337 338 for (i = 0; i < budget; i++) { 339 if (skb_queue_empty(&rq->skb_queue)) 340 break; 341 342 skb = skb_dequeue(&rq->skb_queue); 343 netif_receive_skb(skb); 344 } 345 346 return i; 347 } 348 349 static int nsim_poll(struct napi_struct *napi, int budget) 350 { 351 struct nsim_rq *rq = container_of(napi, struct nsim_rq, napi); 352 int done; 353 354 done = nsim_rcv(rq, budget); 355 napi_complete(napi); 356 357 return done; 358 } 359 360 static int nsim_create_page_pool(struct nsim_rq *rq) 361 { 362 struct page_pool_params p = { 363 .order = 0, 364 .pool_size = NSIM_RING_SIZE, 365 .nid = NUMA_NO_NODE, 366 .dev = &rq->napi.dev->dev, 367 .napi = &rq->napi, 368 .dma_dir = DMA_BIDIRECTIONAL, 369 .netdev = rq->napi.dev, 370 }; 371 372 rq->page_pool = page_pool_create(&p); 373 if (IS_ERR(rq->page_pool)) { 374 int err = PTR_ERR(rq->page_pool); 375 376 rq->page_pool = NULL; 377 return err; 378 } 379 return 0; 380 } 381 382 static int nsim_init_napi(struct netdevsim *ns) 383 { 384 struct net_device *dev = ns->netdev; 385 struct nsim_rq *rq; 386 int err, i; 387 388 for (i = 0; i < dev->num_rx_queues; i++) { 389 rq = &ns->rq[i]; 390 391 netif_napi_add(dev, &rq->napi, nsim_poll); 392 } 393 394 for (i = 0; i < dev->num_rx_queues; i++) { 395 rq = &ns->rq[i]; 396 397 err = nsim_create_page_pool(rq); 398 if (err) 399 goto err_pp_destroy; 400 } 401 402 return 0; 403 404 err_pp_destroy: 405 while (i--) { 406 page_pool_destroy(ns->rq[i].page_pool); 407 ns->rq[i].page_pool = NULL; 408 } 409 410 for (i = 0; i < dev->num_rx_queues; i++) 411 __netif_napi_del(&ns->rq[i].napi); 412 413 return err; 414 } 415 416 static void nsim_enable_napi(struct netdevsim *ns) 417 { 418 struct net_device *dev = ns->netdev; 419 int i; 420 421 for (i = 0; i < dev->num_rx_queues; i++) { 422 struct nsim_rq *rq = &ns->rq[i]; 423 424 netif_queue_set_napi(dev, i, NETDEV_QUEUE_TYPE_RX, &rq->napi); 425 napi_enable(&rq->napi); 426 } 427 } 428 429 static int nsim_open(struct net_device *dev) 430 { 431 struct netdevsim *ns = netdev_priv(dev); 432 int err; 433 434 err = nsim_init_napi(ns); 435 if (err) 436 return err; 437 438 nsim_enable_napi(ns); 439 440 return 0; 441 } 442 443 static void nsim_del_napi(struct netdevsim *ns) 444 { 445 struct net_device *dev = ns->netdev; 446 int i; 447 448 for (i = 0; i < dev->num_rx_queues; i++) { 449 struct nsim_rq *rq = &ns->rq[i]; 450 451 napi_disable(&rq->napi); 452 __netif_napi_del(&rq->napi); 453 } 454 synchronize_net(); 455 456 for (i = 0; i < dev->num_rx_queues; i++) { 457 page_pool_destroy(ns->rq[i].page_pool); 458 ns->rq[i].page_pool = NULL; 459 } 460 } 461 462 static int nsim_stop(struct net_device *dev) 463 { 464 struct netdevsim *ns = netdev_priv(dev); 465 struct netdevsim *peer; 466 467 netif_carrier_off(dev); 468 peer = rtnl_dereference(ns->peer); 469 if (peer) 470 netif_carrier_off(peer->netdev); 471 472 nsim_del_napi(ns); 473 474 return 0; 475 } 476 477 static const struct net_device_ops nsim_netdev_ops = { 478 .ndo_start_xmit = nsim_start_xmit, 479 .ndo_set_rx_mode = nsim_set_rx_mode, 480 .ndo_set_mac_address = eth_mac_addr, 481 .ndo_validate_addr = eth_validate_addr, 482 .ndo_change_mtu = nsim_change_mtu, 483 .ndo_get_stats64 = nsim_get_stats64, 484 .ndo_set_vf_mac = nsim_set_vf_mac, 485 .ndo_set_vf_vlan = nsim_set_vf_vlan, 486 .ndo_set_vf_rate = nsim_set_vf_rate, 487 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk, 488 .ndo_set_vf_trust = nsim_set_vf_trust, 489 .ndo_get_vf_config = nsim_get_vf_config, 490 .ndo_set_vf_link_state = nsim_set_vf_link_state, 491 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, 492 .ndo_setup_tc = nsim_setup_tc, 493 .ndo_set_features = nsim_set_features, 494 .ndo_get_iflink = nsim_get_iflink, 495 .ndo_bpf = nsim_bpf, 496 .ndo_open = nsim_open, 497 .ndo_stop = nsim_stop, 498 }; 499 500 static const struct net_device_ops nsim_vf_netdev_ops = { 501 .ndo_start_xmit = nsim_start_xmit, 502 .ndo_set_rx_mode = nsim_set_rx_mode, 503 .ndo_set_mac_address = eth_mac_addr, 504 .ndo_validate_addr = eth_validate_addr, 505 .ndo_change_mtu = nsim_change_mtu, 506 .ndo_get_stats64 = nsim_get_stats64, 507 .ndo_setup_tc = nsim_setup_tc, 508 .ndo_set_features = nsim_set_features, 509 }; 510 511 /* We don't have true per-queue stats, yet, so do some random fakery here. 512 * Only report stuff for queue 0. 513 */ 514 static void nsim_get_queue_stats_rx(struct net_device *dev, int idx, 515 struct netdev_queue_stats_rx *stats) 516 { 517 struct rtnl_link_stats64 rtstats = {}; 518 519 if (!idx) 520 nsim_get_stats64(dev, &rtstats); 521 522 stats->packets = rtstats.rx_packets - !!rtstats.rx_packets; 523 stats->bytes = rtstats.rx_bytes; 524 } 525 526 static void nsim_get_queue_stats_tx(struct net_device *dev, int idx, 527 struct netdev_queue_stats_tx *stats) 528 { 529 struct rtnl_link_stats64 rtstats = {}; 530 531 if (!idx) 532 nsim_get_stats64(dev, &rtstats); 533 534 stats->packets = rtstats.tx_packets - !!rtstats.tx_packets; 535 stats->bytes = rtstats.tx_bytes; 536 } 537 538 static void nsim_get_base_stats(struct net_device *dev, 539 struct netdev_queue_stats_rx *rx, 540 struct netdev_queue_stats_tx *tx) 541 { 542 struct rtnl_link_stats64 rtstats = {}; 543 544 nsim_get_stats64(dev, &rtstats); 545 546 rx->packets = !!rtstats.rx_packets; 547 rx->bytes = 0; 548 tx->packets = !!rtstats.tx_packets; 549 tx->bytes = 0; 550 } 551 552 static const struct netdev_stat_ops nsim_stat_ops = { 553 .get_queue_stats_tx = nsim_get_queue_stats_tx, 554 .get_queue_stats_rx = nsim_get_queue_stats_rx, 555 .get_base_stats = nsim_get_base_stats, 556 }; 557 558 static ssize_t 559 nsim_pp_hold_read(struct file *file, char __user *data, 560 size_t count, loff_t *ppos) 561 { 562 struct netdevsim *ns = file->private_data; 563 char buf[3] = "n\n"; 564 565 if (ns->page) 566 buf[0] = 'y'; 567 568 return simple_read_from_buffer(data, count, ppos, buf, 2); 569 } 570 571 static ssize_t 572 nsim_pp_hold_write(struct file *file, const char __user *data, 573 size_t count, loff_t *ppos) 574 { 575 struct netdevsim *ns = file->private_data; 576 ssize_t ret; 577 bool val; 578 579 ret = kstrtobool_from_user(data, count, &val); 580 if (ret) 581 return ret; 582 583 rtnl_lock(); 584 ret = count; 585 if (val == !!ns->page) 586 goto exit; 587 588 if (!netif_running(ns->netdev) && val) { 589 ret = -ENETDOWN; 590 } else if (val) { 591 ns->page = page_pool_dev_alloc_pages(ns->rq[0].page_pool); 592 if (!ns->page) 593 ret = -ENOMEM; 594 } else { 595 page_pool_put_full_page(ns->page->pp, ns->page, false); 596 ns->page = NULL; 597 } 598 rtnl_unlock(); 599 600 exit: 601 return count; 602 } 603 604 static const struct file_operations nsim_pp_hold_fops = { 605 .open = simple_open, 606 .read = nsim_pp_hold_read, 607 .write = nsim_pp_hold_write, 608 .llseek = generic_file_llseek, 609 .owner = THIS_MODULE, 610 }; 611 612 static void nsim_setup(struct net_device *dev) 613 { 614 ether_setup(dev); 615 eth_hw_addr_random(dev); 616 617 dev->tx_queue_len = 0; 618 dev->flags &= ~IFF_MULTICAST; 619 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 620 IFF_NO_QUEUE; 621 dev->features |= NETIF_F_HIGHDMA | 622 NETIF_F_SG | 623 NETIF_F_FRAGLIST | 624 NETIF_F_HW_CSUM | 625 NETIF_F_TSO; 626 dev->hw_features |= NETIF_F_HW_TC; 627 dev->max_mtu = ETH_MAX_MTU; 628 dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD; 629 } 630 631 static int nsim_queue_init(struct netdevsim *ns) 632 { 633 struct net_device *dev = ns->netdev; 634 int i; 635 636 ns->rq = kvcalloc(dev->num_rx_queues, sizeof(*ns->rq), 637 GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); 638 if (!ns->rq) 639 return -ENOMEM; 640 641 for (i = 0; i < dev->num_rx_queues; i++) 642 skb_queue_head_init(&ns->rq[i].skb_queue); 643 644 return 0; 645 } 646 647 static void nsim_queue_free(struct netdevsim *ns) 648 { 649 struct net_device *dev = ns->netdev; 650 int i; 651 652 for (i = 0; i < dev->num_rx_queues; i++) 653 skb_queue_purge_reason(&ns->rq[i].skb_queue, 654 SKB_DROP_REASON_QUEUE_PURGE); 655 656 kvfree(ns->rq); 657 ns->rq = NULL; 658 } 659 660 static int nsim_init_netdevsim(struct netdevsim *ns) 661 { 662 struct mock_phc *phc; 663 int err; 664 665 phc = mock_phc_create(&ns->nsim_bus_dev->dev); 666 if (IS_ERR(phc)) 667 return PTR_ERR(phc); 668 669 ns->phc = phc; 670 ns->netdev->netdev_ops = &nsim_netdev_ops; 671 ns->netdev->stat_ops = &nsim_stat_ops; 672 673 err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev); 674 if (err) 675 goto err_phc_destroy; 676 677 rtnl_lock(); 678 err = nsim_queue_init(ns); 679 if (err) 680 goto err_utn_destroy; 681 682 err = nsim_bpf_init(ns); 683 if (err) 684 goto err_rq_destroy; 685 686 nsim_macsec_init(ns); 687 nsim_ipsec_init(ns); 688 689 err = register_netdevice(ns->netdev); 690 if (err) 691 goto err_ipsec_teardown; 692 rtnl_unlock(); 693 return 0; 694 695 err_ipsec_teardown: 696 nsim_ipsec_teardown(ns); 697 nsim_macsec_teardown(ns); 698 nsim_bpf_uninit(ns); 699 err_rq_destroy: 700 nsim_queue_free(ns); 701 err_utn_destroy: 702 rtnl_unlock(); 703 nsim_udp_tunnels_info_destroy(ns->netdev); 704 err_phc_destroy: 705 mock_phc_destroy(ns->phc); 706 return err; 707 } 708 709 static int nsim_init_netdevsim_vf(struct netdevsim *ns) 710 { 711 int err; 712 713 ns->netdev->netdev_ops = &nsim_vf_netdev_ops; 714 rtnl_lock(); 715 err = register_netdevice(ns->netdev); 716 rtnl_unlock(); 717 return err; 718 } 719 720 static void nsim_exit_netdevsim(struct netdevsim *ns) 721 { 722 nsim_udp_tunnels_info_destroy(ns->netdev); 723 mock_phc_destroy(ns->phc); 724 } 725 726 struct netdevsim * 727 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) 728 { 729 struct net_device *dev; 730 struct netdevsim *ns; 731 int err; 732 733 dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup, 734 nsim_dev->nsim_bus_dev->num_queues); 735 if (!dev) 736 return ERR_PTR(-ENOMEM); 737 738 dev_net_set(dev, nsim_dev_net(nsim_dev)); 739 ns = netdev_priv(dev); 740 ns->netdev = dev; 741 u64_stats_init(&ns->syncp); 742 ns->nsim_dev = nsim_dev; 743 ns->nsim_dev_port = nsim_dev_port; 744 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev; 745 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev); 746 SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port); 747 nsim_ethtool_init(ns); 748 if (nsim_dev_port_is_pf(nsim_dev_port)) 749 err = nsim_init_netdevsim(ns); 750 else 751 err = nsim_init_netdevsim_vf(ns); 752 if (err) 753 goto err_free_netdev; 754 755 ns->pp_dfs = debugfs_create_file("pp_hold", 0600, nsim_dev_port->ddir, 756 ns, &nsim_pp_hold_fops); 757 758 return ns; 759 760 err_free_netdev: 761 free_netdev(dev); 762 return ERR_PTR(err); 763 } 764 765 void nsim_destroy(struct netdevsim *ns) 766 { 767 struct net_device *dev = ns->netdev; 768 struct netdevsim *peer; 769 770 debugfs_remove(ns->pp_dfs); 771 772 rtnl_lock(); 773 peer = rtnl_dereference(ns->peer); 774 if (peer) 775 RCU_INIT_POINTER(peer->peer, NULL); 776 RCU_INIT_POINTER(ns->peer, NULL); 777 unregister_netdevice(dev); 778 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) { 779 nsim_macsec_teardown(ns); 780 nsim_ipsec_teardown(ns); 781 nsim_bpf_uninit(ns); 782 nsim_queue_free(ns); 783 } 784 rtnl_unlock(); 785 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) 786 nsim_exit_netdevsim(ns); 787 788 /* Put this intentionally late to exercise the orphaning path */ 789 if (ns->page) { 790 page_pool_put_full_page(ns->page->pp, ns->page, false); 791 ns->page = NULL; 792 } 793 794 free_netdev(dev); 795 } 796 797 bool netdev_is_nsim(struct net_device *dev) 798 { 799 return dev->netdev_ops == &nsim_netdev_ops; 800 } 801 802 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], 803 struct netlink_ext_ack *extack) 804 { 805 NL_SET_ERR_MSG_MOD(extack, 806 "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device"); 807 return -EOPNOTSUPP; 808 } 809 810 static struct rtnl_link_ops nsim_link_ops __read_mostly = { 811 .kind = DRV_NAME, 812 .validate = nsim_validate, 813 }; 814 815 static int __init nsim_module_init(void) 816 { 817 int err; 818 819 err = nsim_dev_init(); 820 if (err) 821 return err; 822 823 err = nsim_bus_init(); 824 if (err) 825 goto err_dev_exit; 826 827 err = rtnl_link_register(&nsim_link_ops); 828 if (err) 829 goto err_bus_exit; 830 831 return 0; 832 833 err_bus_exit: 834 nsim_bus_exit(); 835 err_dev_exit: 836 nsim_dev_exit(); 837 return err; 838 } 839 840 static void __exit nsim_module_exit(void) 841 { 842 rtnl_link_unregister(&nsim_link_ops); 843 nsim_bus_exit(); 844 nsim_dev_exit(); 845 } 846 847 module_init(nsim_module_init); 848 module_exit(nsim_module_exit); 849 MODULE_LICENSE("GPL"); 850 MODULE_DESCRIPTION("Simulated networking device for testing"); 851 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 852