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 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 32 { 33 struct netdevsim *ns = netdev_priv(dev); 34 unsigned int len = skb->len; 35 struct netdevsim *peer_ns; 36 37 rcu_read_lock(); 38 if (!nsim_ipsec_tx(ns, skb)) 39 goto out_drop_free; 40 41 peer_ns = rcu_dereference(ns->peer); 42 if (!peer_ns) 43 goto out_drop_free; 44 45 skb_tx_timestamp(skb); 46 if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) 47 goto out_drop_cnt; 48 49 rcu_read_unlock(); 50 u64_stats_update_begin(&ns->syncp); 51 ns->tx_packets++; 52 ns->tx_bytes += len; 53 u64_stats_update_end(&ns->syncp); 54 return NETDEV_TX_OK; 55 56 out_drop_free: 57 dev_kfree_skb(skb); 58 out_drop_cnt: 59 rcu_read_unlock(); 60 u64_stats_update_begin(&ns->syncp); 61 ns->tx_dropped++; 62 u64_stats_update_end(&ns->syncp); 63 return NETDEV_TX_OK; 64 } 65 66 static void nsim_set_rx_mode(struct net_device *dev) 67 { 68 } 69 70 static int nsim_change_mtu(struct net_device *dev, int new_mtu) 71 { 72 struct netdevsim *ns = netdev_priv(dev); 73 74 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU) 75 return -EBUSY; 76 77 dev->mtu = new_mtu; 78 79 return 0; 80 } 81 82 static void 83 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 84 { 85 struct netdevsim *ns = netdev_priv(dev); 86 unsigned int start; 87 88 do { 89 start = u64_stats_fetch_begin(&ns->syncp); 90 stats->tx_bytes = ns->tx_bytes; 91 stats->tx_packets = ns->tx_packets; 92 stats->tx_dropped = ns->tx_dropped; 93 } while (u64_stats_fetch_retry(&ns->syncp, start)); 94 } 95 96 static int 97 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv) 98 { 99 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv); 100 } 101 102 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac) 103 { 104 struct netdevsim *ns = netdev_priv(dev); 105 struct nsim_dev *nsim_dev = ns->nsim_dev; 106 107 /* Only refuse multicast addresses, zero address can mean unset/any. */ 108 if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac)) 109 return -EINVAL; 110 memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN); 111 112 return 0; 113 } 114 115 static int nsim_set_vf_vlan(struct net_device *dev, int vf, 116 u16 vlan, u8 qos, __be16 vlan_proto) 117 { 118 struct netdevsim *ns = netdev_priv(dev); 119 struct nsim_dev *nsim_dev = ns->nsim_dev; 120 121 if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7) 122 return -EINVAL; 123 124 nsim_dev->vfconfigs[vf].vlan = vlan; 125 nsim_dev->vfconfigs[vf].qos = qos; 126 nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto; 127 128 return 0; 129 } 130 131 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max) 132 { 133 struct netdevsim *ns = netdev_priv(dev); 134 struct nsim_dev *nsim_dev = ns->nsim_dev; 135 136 if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) { 137 pr_err("Not supported in switchdev mode. Please use devlink API.\n"); 138 return -EOPNOTSUPP; 139 } 140 141 if (vf >= nsim_dev_get_vfs(nsim_dev)) 142 return -EINVAL; 143 144 nsim_dev->vfconfigs[vf].min_tx_rate = min; 145 nsim_dev->vfconfigs[vf].max_tx_rate = max; 146 147 return 0; 148 } 149 150 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val) 151 { 152 struct netdevsim *ns = netdev_priv(dev); 153 struct nsim_dev *nsim_dev = ns->nsim_dev; 154 155 if (vf >= nsim_dev_get_vfs(nsim_dev)) 156 return -EINVAL; 157 nsim_dev->vfconfigs[vf].spoofchk_enabled = val; 158 159 return 0; 160 } 161 162 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val) 163 { 164 struct netdevsim *ns = netdev_priv(dev); 165 struct nsim_dev *nsim_dev = ns->nsim_dev; 166 167 if (vf >= nsim_dev_get_vfs(nsim_dev)) 168 return -EINVAL; 169 nsim_dev->vfconfigs[vf].rss_query_enabled = val; 170 171 return 0; 172 } 173 174 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val) 175 { 176 struct netdevsim *ns = netdev_priv(dev); 177 struct nsim_dev *nsim_dev = ns->nsim_dev; 178 179 if (vf >= nsim_dev_get_vfs(nsim_dev)) 180 return -EINVAL; 181 nsim_dev->vfconfigs[vf].trusted = val; 182 183 return 0; 184 } 185 186 static int 187 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi) 188 { 189 struct netdevsim *ns = netdev_priv(dev); 190 struct nsim_dev *nsim_dev = ns->nsim_dev; 191 192 if (vf >= nsim_dev_get_vfs(nsim_dev)) 193 return -EINVAL; 194 195 ivi->vf = vf; 196 ivi->linkstate = nsim_dev->vfconfigs[vf].link_state; 197 ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate; 198 ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate; 199 ivi->vlan = nsim_dev->vfconfigs[vf].vlan; 200 ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto; 201 ivi->qos = nsim_dev->vfconfigs[vf].qos; 202 memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN); 203 ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled; 204 ivi->trusted = nsim_dev->vfconfigs[vf].trusted; 205 ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled; 206 207 return 0; 208 } 209 210 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state) 211 { 212 struct netdevsim *ns = netdev_priv(dev); 213 struct nsim_dev *nsim_dev = ns->nsim_dev; 214 215 if (vf >= nsim_dev_get_vfs(nsim_dev)) 216 return -EINVAL; 217 218 switch (state) { 219 case IFLA_VF_LINK_STATE_AUTO: 220 case IFLA_VF_LINK_STATE_ENABLE: 221 case IFLA_VF_LINK_STATE_DISABLE: 222 break; 223 default: 224 return -EINVAL; 225 } 226 227 nsim_dev->vfconfigs[vf].link_state = state; 228 229 return 0; 230 } 231 232 static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats) 233 { 234 stats->window_drops = 0; 235 stats->tx_overruns = 0; 236 } 237 238 static int nsim_setup_tc_taprio(struct net_device *dev, 239 struct tc_taprio_qopt_offload *offload) 240 { 241 int err = 0; 242 243 switch (offload->cmd) { 244 case TAPRIO_CMD_REPLACE: 245 case TAPRIO_CMD_DESTROY: 246 break; 247 case TAPRIO_CMD_STATS: 248 nsim_taprio_stats(&offload->stats); 249 break; 250 default: 251 err = -EOPNOTSUPP; 252 } 253 254 return err; 255 } 256 257 static LIST_HEAD(nsim_block_cb_list); 258 259 static int 260 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data) 261 { 262 struct netdevsim *ns = netdev_priv(dev); 263 264 switch (type) { 265 case TC_SETUP_QDISC_TAPRIO: 266 return nsim_setup_tc_taprio(dev, type_data); 267 case TC_SETUP_BLOCK: 268 return flow_block_cb_setup_simple(type_data, 269 &nsim_block_cb_list, 270 nsim_setup_tc_block_cb, 271 ns, ns, true); 272 default: 273 return -EOPNOTSUPP; 274 } 275 } 276 277 static int 278 nsim_set_features(struct net_device *dev, netdev_features_t features) 279 { 280 struct netdevsim *ns = netdev_priv(dev); 281 282 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) 283 return nsim_bpf_disable_tc(ns); 284 285 return 0; 286 } 287 288 static int nsim_get_iflink(const struct net_device *dev) 289 { 290 struct netdevsim *nsim, *peer; 291 int iflink; 292 293 nsim = netdev_priv(dev); 294 295 rcu_read_lock(); 296 peer = rcu_dereference(nsim->peer); 297 iflink = peer ? READ_ONCE(peer->netdev->ifindex) : 0; 298 rcu_read_unlock(); 299 300 return iflink; 301 } 302 303 static int nsim_open(struct net_device *dev) 304 { 305 struct netdevsim *ns = netdev_priv(dev); 306 struct page_pool_params pp = { 0 }; 307 308 pp.pool_size = 128; 309 pp.dev = &dev->dev; 310 pp.dma_dir = DMA_BIDIRECTIONAL; 311 pp.netdev = dev; 312 313 ns->pp = page_pool_create(&pp); 314 return PTR_ERR_OR_ZERO(ns->pp); 315 } 316 317 static int nsim_stop(struct net_device *dev) 318 { 319 struct netdevsim *ns = netdev_priv(dev); 320 321 page_pool_destroy(ns->pp); 322 323 return 0; 324 } 325 326 static const struct net_device_ops nsim_netdev_ops = { 327 .ndo_start_xmit = nsim_start_xmit, 328 .ndo_set_rx_mode = nsim_set_rx_mode, 329 .ndo_set_mac_address = eth_mac_addr, 330 .ndo_validate_addr = eth_validate_addr, 331 .ndo_change_mtu = nsim_change_mtu, 332 .ndo_get_stats64 = nsim_get_stats64, 333 .ndo_set_vf_mac = nsim_set_vf_mac, 334 .ndo_set_vf_vlan = nsim_set_vf_vlan, 335 .ndo_set_vf_rate = nsim_set_vf_rate, 336 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk, 337 .ndo_set_vf_trust = nsim_set_vf_trust, 338 .ndo_get_vf_config = nsim_get_vf_config, 339 .ndo_set_vf_link_state = nsim_set_vf_link_state, 340 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, 341 .ndo_setup_tc = nsim_setup_tc, 342 .ndo_set_features = nsim_set_features, 343 .ndo_get_iflink = nsim_get_iflink, 344 .ndo_bpf = nsim_bpf, 345 .ndo_open = nsim_open, 346 .ndo_stop = nsim_stop, 347 }; 348 349 static const struct net_device_ops nsim_vf_netdev_ops = { 350 .ndo_start_xmit = nsim_start_xmit, 351 .ndo_set_rx_mode = nsim_set_rx_mode, 352 .ndo_set_mac_address = eth_mac_addr, 353 .ndo_validate_addr = eth_validate_addr, 354 .ndo_change_mtu = nsim_change_mtu, 355 .ndo_get_stats64 = nsim_get_stats64, 356 .ndo_setup_tc = nsim_setup_tc, 357 .ndo_set_features = nsim_set_features, 358 }; 359 360 /* We don't have true per-queue stats, yet, so do some random fakery here. 361 * Only report stuff for queue 0. 362 */ 363 static void nsim_get_queue_stats_rx(struct net_device *dev, int idx, 364 struct netdev_queue_stats_rx *stats) 365 { 366 struct rtnl_link_stats64 rtstats = {}; 367 368 if (!idx) 369 nsim_get_stats64(dev, &rtstats); 370 371 stats->packets = rtstats.rx_packets - !!rtstats.rx_packets; 372 stats->bytes = rtstats.rx_bytes; 373 } 374 375 static void nsim_get_queue_stats_tx(struct net_device *dev, int idx, 376 struct netdev_queue_stats_tx *stats) 377 { 378 struct rtnl_link_stats64 rtstats = {}; 379 380 if (!idx) 381 nsim_get_stats64(dev, &rtstats); 382 383 stats->packets = rtstats.tx_packets - !!rtstats.tx_packets; 384 stats->bytes = rtstats.tx_bytes; 385 } 386 387 static void nsim_get_base_stats(struct net_device *dev, 388 struct netdev_queue_stats_rx *rx, 389 struct netdev_queue_stats_tx *tx) 390 { 391 struct rtnl_link_stats64 rtstats = {}; 392 393 nsim_get_stats64(dev, &rtstats); 394 395 rx->packets = !!rtstats.rx_packets; 396 rx->bytes = 0; 397 tx->packets = !!rtstats.tx_packets; 398 tx->bytes = 0; 399 } 400 401 static const struct netdev_stat_ops nsim_stat_ops = { 402 .get_queue_stats_tx = nsim_get_queue_stats_tx, 403 .get_queue_stats_rx = nsim_get_queue_stats_rx, 404 .get_base_stats = nsim_get_base_stats, 405 }; 406 407 static ssize_t 408 nsim_pp_hold_read(struct file *file, char __user *data, 409 size_t count, loff_t *ppos) 410 { 411 struct netdevsim *ns = file->private_data; 412 char buf[3] = "n\n"; 413 414 if (ns->page) 415 buf[0] = 'y'; 416 417 return simple_read_from_buffer(data, count, ppos, buf, 2); 418 } 419 420 static ssize_t 421 nsim_pp_hold_write(struct file *file, const char __user *data, 422 size_t count, loff_t *ppos) 423 { 424 struct netdevsim *ns = file->private_data; 425 ssize_t ret; 426 bool val; 427 428 ret = kstrtobool_from_user(data, count, &val); 429 if (ret) 430 return ret; 431 432 rtnl_lock(); 433 ret = count; 434 if (val == !!ns->page) 435 goto exit; 436 437 if (!netif_running(ns->netdev) && val) { 438 ret = -ENETDOWN; 439 } else if (val) { 440 ns->page = page_pool_dev_alloc_pages(ns->pp); 441 if (!ns->page) 442 ret = -ENOMEM; 443 } else { 444 page_pool_put_full_page(ns->page->pp, ns->page, false); 445 ns->page = NULL; 446 } 447 rtnl_unlock(); 448 449 exit: 450 return count; 451 } 452 453 static const struct file_operations nsim_pp_hold_fops = { 454 .open = simple_open, 455 .read = nsim_pp_hold_read, 456 .write = nsim_pp_hold_write, 457 .llseek = generic_file_llseek, 458 .owner = THIS_MODULE, 459 }; 460 461 static void nsim_setup(struct net_device *dev) 462 { 463 ether_setup(dev); 464 eth_hw_addr_random(dev); 465 466 dev->tx_queue_len = 0; 467 dev->flags &= ~IFF_MULTICAST; 468 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 469 IFF_NO_QUEUE; 470 dev->features |= NETIF_F_HIGHDMA | 471 NETIF_F_SG | 472 NETIF_F_FRAGLIST | 473 NETIF_F_HW_CSUM | 474 NETIF_F_TSO; 475 dev->hw_features |= NETIF_F_HW_TC; 476 dev->max_mtu = ETH_MAX_MTU; 477 dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD; 478 } 479 480 static int nsim_init_netdevsim(struct netdevsim *ns) 481 { 482 struct mock_phc *phc; 483 int err; 484 485 phc = mock_phc_create(&ns->nsim_bus_dev->dev); 486 if (IS_ERR(phc)) 487 return PTR_ERR(phc); 488 489 ns->phc = phc; 490 ns->netdev->netdev_ops = &nsim_netdev_ops; 491 ns->netdev->stat_ops = &nsim_stat_ops; 492 493 err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev); 494 if (err) 495 goto err_phc_destroy; 496 497 rtnl_lock(); 498 err = nsim_bpf_init(ns); 499 if (err) 500 goto err_utn_destroy; 501 502 nsim_macsec_init(ns); 503 nsim_ipsec_init(ns); 504 505 err = register_netdevice(ns->netdev); 506 if (err) 507 goto err_ipsec_teardown; 508 rtnl_unlock(); 509 return 0; 510 511 err_ipsec_teardown: 512 nsim_ipsec_teardown(ns); 513 nsim_macsec_teardown(ns); 514 nsim_bpf_uninit(ns); 515 err_utn_destroy: 516 rtnl_unlock(); 517 nsim_udp_tunnels_info_destroy(ns->netdev); 518 err_phc_destroy: 519 mock_phc_destroy(ns->phc); 520 return err; 521 } 522 523 static int nsim_init_netdevsim_vf(struct netdevsim *ns) 524 { 525 int err; 526 527 ns->netdev->netdev_ops = &nsim_vf_netdev_ops; 528 rtnl_lock(); 529 err = register_netdevice(ns->netdev); 530 rtnl_unlock(); 531 return err; 532 } 533 534 static void nsim_exit_netdevsim(struct netdevsim *ns) 535 { 536 nsim_udp_tunnels_info_destroy(ns->netdev); 537 mock_phc_destroy(ns->phc); 538 } 539 540 struct netdevsim * 541 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) 542 { 543 struct net_device *dev; 544 struct netdevsim *ns; 545 int err; 546 547 dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup, 548 nsim_dev->nsim_bus_dev->num_queues); 549 if (!dev) 550 return ERR_PTR(-ENOMEM); 551 552 dev_net_set(dev, nsim_dev_net(nsim_dev)); 553 ns = netdev_priv(dev); 554 ns->netdev = dev; 555 u64_stats_init(&ns->syncp); 556 ns->nsim_dev = nsim_dev; 557 ns->nsim_dev_port = nsim_dev_port; 558 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev; 559 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev); 560 SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port); 561 nsim_ethtool_init(ns); 562 if (nsim_dev_port_is_pf(nsim_dev_port)) 563 err = nsim_init_netdevsim(ns); 564 else 565 err = nsim_init_netdevsim_vf(ns); 566 if (err) 567 goto err_free_netdev; 568 569 ns->pp_dfs = debugfs_create_file("pp_hold", 0600, nsim_dev_port->ddir, 570 ns, &nsim_pp_hold_fops); 571 572 return ns; 573 574 err_free_netdev: 575 free_netdev(dev); 576 return ERR_PTR(err); 577 } 578 579 void nsim_destroy(struct netdevsim *ns) 580 { 581 struct net_device *dev = ns->netdev; 582 struct netdevsim *peer; 583 584 debugfs_remove(ns->pp_dfs); 585 586 rtnl_lock(); 587 peer = rtnl_dereference(ns->peer); 588 if (peer) 589 RCU_INIT_POINTER(peer->peer, NULL); 590 RCU_INIT_POINTER(ns->peer, NULL); 591 unregister_netdevice(dev); 592 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) { 593 nsim_macsec_teardown(ns); 594 nsim_ipsec_teardown(ns); 595 nsim_bpf_uninit(ns); 596 } 597 rtnl_unlock(); 598 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) 599 nsim_exit_netdevsim(ns); 600 601 /* Put this intentionally late to exercise the orphaning path */ 602 if (ns->page) { 603 page_pool_put_full_page(ns->page->pp, ns->page, false); 604 ns->page = NULL; 605 } 606 607 free_netdev(dev); 608 } 609 610 bool netdev_is_nsim(struct net_device *dev) 611 { 612 return dev->netdev_ops == &nsim_netdev_ops; 613 } 614 615 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], 616 struct netlink_ext_ack *extack) 617 { 618 NL_SET_ERR_MSG_MOD(extack, 619 "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device"); 620 return -EOPNOTSUPP; 621 } 622 623 static struct rtnl_link_ops nsim_link_ops __read_mostly = { 624 .kind = DRV_NAME, 625 .validate = nsim_validate, 626 }; 627 628 static int __init nsim_module_init(void) 629 { 630 int err; 631 632 err = nsim_dev_init(); 633 if (err) 634 return err; 635 636 err = nsim_bus_init(); 637 if (err) 638 goto err_dev_exit; 639 640 err = rtnl_link_register(&nsim_link_ops); 641 if (err) 642 goto err_bus_exit; 643 644 return 0; 645 646 err_bus_exit: 647 nsim_bus_exit(); 648 err_dev_exit: 649 nsim_dev_exit(); 650 return err; 651 } 652 653 static void __exit nsim_module_exit(void) 654 { 655 rtnl_link_unregister(&nsim_link_ops); 656 nsim_bus_exit(); 657 nsim_dev_exit(); 658 } 659 660 module_init(nsim_module_init); 661 module_exit(nsim_module_exit); 662 MODULE_LICENSE("GPL"); 663 MODULE_DESCRIPTION("Simulated networking device for testing"); 664 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 665