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/netlink.h> 23 #include <net/pkt_cls.h> 24 #include <net/rtnetlink.h> 25 #include <net/switchdev.h> 26 27 #include "netdevsim.h" 28 29 struct nsim_vf_config { 30 int link_state; 31 u16 min_tx_rate; 32 u16 max_tx_rate; 33 u16 vlan; 34 __be16 vlan_proto; 35 u16 qos; 36 u8 vf_mac[ETH_ALEN]; 37 bool spoofchk_enabled; 38 bool trusted; 39 bool rss_query_enabled; 40 }; 41 42 static u32 nsim_dev_id; 43 44 static int nsim_num_vf(struct device *dev) 45 { 46 struct netdevsim *ns = to_nsim(dev); 47 48 return ns->num_vfs; 49 } 50 51 static struct bus_type nsim_bus = { 52 .name = DRV_NAME, 53 .dev_name = DRV_NAME, 54 .num_vf = nsim_num_vf, 55 }; 56 57 static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs) 58 { 59 ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config), 60 GFP_KERNEL); 61 if (!ns->vfconfigs) 62 return -ENOMEM; 63 ns->num_vfs = num_vfs; 64 65 return 0; 66 } 67 68 static void nsim_vfs_disable(struct netdevsim *ns) 69 { 70 kfree(ns->vfconfigs); 71 ns->vfconfigs = NULL; 72 ns->num_vfs = 0; 73 } 74 75 static ssize_t 76 nsim_numvfs_store(struct device *dev, struct device_attribute *attr, 77 const char *buf, size_t count) 78 { 79 struct netdevsim *ns = to_nsim(dev); 80 unsigned int num_vfs; 81 int ret; 82 83 ret = kstrtouint(buf, 0, &num_vfs); 84 if (ret) 85 return ret; 86 87 rtnl_lock(); 88 if (ns->num_vfs == num_vfs) 89 goto exit_good; 90 if (ns->num_vfs && num_vfs) { 91 ret = -EBUSY; 92 goto exit_unlock; 93 } 94 95 if (num_vfs) { 96 ret = nsim_vfs_enable(ns, num_vfs); 97 if (ret) 98 goto exit_unlock; 99 } else { 100 nsim_vfs_disable(ns); 101 } 102 exit_good: 103 ret = count; 104 exit_unlock: 105 rtnl_unlock(); 106 107 return ret; 108 } 109 110 static ssize_t 111 nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf) 112 { 113 struct netdevsim *ns = to_nsim(dev); 114 115 return sprintf(buf, "%u\n", ns->num_vfs); 116 } 117 118 static struct device_attribute nsim_numvfs_attr = 119 __ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store); 120 121 static struct attribute *nsim_dev_attrs[] = { 122 &nsim_numvfs_attr.attr, 123 NULL, 124 }; 125 126 static const struct attribute_group nsim_dev_attr_group = { 127 .attrs = nsim_dev_attrs, 128 }; 129 130 static const struct attribute_group *nsim_dev_attr_groups[] = { 131 &nsim_dev_attr_group, 132 NULL, 133 }; 134 135 static void nsim_dev_release(struct device *dev) 136 { 137 struct netdevsim *ns = to_nsim(dev); 138 139 nsim_vfs_disable(ns); 140 free_netdev(ns->netdev); 141 } 142 143 static struct device_type nsim_dev_type = { 144 .groups = nsim_dev_attr_groups, 145 .release = nsim_dev_release, 146 }; 147 148 static int 149 nsim_port_attr_get(struct net_device *dev, struct switchdev_attr *attr) 150 { 151 struct netdevsim *ns = netdev_priv(dev); 152 153 switch (attr->id) { 154 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 155 attr->u.ppid.id_len = sizeof(ns->sdev->switch_id); 156 memcpy(&attr->u.ppid.id, &ns->sdev->switch_id, 157 attr->u.ppid.id_len); 158 return 0; 159 default: 160 return -EOPNOTSUPP; 161 } 162 } 163 164 static const struct switchdev_ops nsim_switchdev_ops = { 165 .switchdev_port_attr_get = nsim_port_attr_get, 166 }; 167 168 static int nsim_init(struct net_device *dev) 169 { 170 char sdev_ddir_name[10], sdev_link_name[32]; 171 struct netdevsim *ns = netdev_priv(dev); 172 int err; 173 174 ns->netdev = dev; 175 ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir); 176 if (IS_ERR_OR_NULL(ns->ddir)) 177 return -ENOMEM; 178 179 if (!ns->sdev) { 180 ns->sdev = kzalloc(sizeof(*ns->sdev), GFP_KERNEL); 181 if (!ns->sdev) { 182 err = -ENOMEM; 183 goto err_debugfs_destroy; 184 } 185 ns->sdev->refcnt = 1; 186 ns->sdev->switch_id = nsim_dev_id; 187 sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id); 188 ns->sdev->ddir = debugfs_create_dir(sdev_ddir_name, 189 nsim_sdev_ddir); 190 if (IS_ERR_OR_NULL(ns->sdev->ddir)) { 191 err = PTR_ERR_OR_ZERO(ns->sdev->ddir) ?: -EINVAL; 192 goto err_sdev_free; 193 } 194 } else { 195 sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id); 196 ns->sdev->refcnt++; 197 } 198 199 sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%s", sdev_ddir_name); 200 debugfs_create_symlink("sdev", ns->ddir, sdev_link_name); 201 202 err = nsim_bpf_init(ns); 203 if (err) 204 goto err_sdev_destroy; 205 206 ns->dev.id = nsim_dev_id++; 207 ns->dev.bus = &nsim_bus; 208 ns->dev.type = &nsim_dev_type; 209 err = device_register(&ns->dev); 210 if (err) 211 goto err_bpf_uninit; 212 213 SET_NETDEV_DEV(dev, &ns->dev); 214 SWITCHDEV_SET_OPS(dev, &nsim_switchdev_ops); 215 216 err = nsim_devlink_setup(ns); 217 if (err) 218 goto err_unreg_dev; 219 220 nsim_ipsec_init(ns); 221 222 return 0; 223 224 err_unreg_dev: 225 device_unregister(&ns->dev); 226 err_bpf_uninit: 227 nsim_bpf_uninit(ns); 228 err_sdev_destroy: 229 if (!--ns->sdev->refcnt) { 230 debugfs_remove_recursive(ns->sdev->ddir); 231 err_sdev_free: 232 kfree(ns->sdev); 233 } 234 err_debugfs_destroy: 235 debugfs_remove_recursive(ns->ddir); 236 return err; 237 } 238 239 static void nsim_uninit(struct net_device *dev) 240 { 241 struct netdevsim *ns = netdev_priv(dev); 242 243 nsim_ipsec_teardown(ns); 244 nsim_devlink_teardown(ns); 245 debugfs_remove_recursive(ns->ddir); 246 nsim_bpf_uninit(ns); 247 if (!--ns->sdev->refcnt) { 248 debugfs_remove_recursive(ns->sdev->ddir); 249 kfree(ns->sdev); 250 } 251 } 252 253 static void nsim_free(struct net_device *dev) 254 { 255 struct netdevsim *ns = netdev_priv(dev); 256 257 device_unregister(&ns->dev); 258 /* netdev and vf state will be freed out of device_release() */ 259 } 260 261 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 262 { 263 struct netdevsim *ns = netdev_priv(dev); 264 265 if (!nsim_ipsec_tx(ns, skb)) 266 goto out; 267 268 u64_stats_update_begin(&ns->syncp); 269 ns->tx_packets++; 270 ns->tx_bytes += skb->len; 271 u64_stats_update_end(&ns->syncp); 272 273 out: 274 dev_kfree_skb(skb); 275 276 return NETDEV_TX_OK; 277 } 278 279 static void nsim_set_rx_mode(struct net_device *dev) 280 { 281 } 282 283 static int nsim_change_mtu(struct net_device *dev, int new_mtu) 284 { 285 struct netdevsim *ns = netdev_priv(dev); 286 287 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU) 288 return -EBUSY; 289 290 dev->mtu = new_mtu; 291 292 return 0; 293 } 294 295 static void 296 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 297 { 298 struct netdevsim *ns = netdev_priv(dev); 299 unsigned int start; 300 301 do { 302 start = u64_stats_fetch_begin(&ns->syncp); 303 stats->tx_bytes = ns->tx_bytes; 304 stats->tx_packets = ns->tx_packets; 305 } while (u64_stats_fetch_retry(&ns->syncp, start)); 306 } 307 308 static int 309 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv) 310 { 311 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv); 312 } 313 314 static int 315 nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f) 316 { 317 struct netdevsim *ns = netdev_priv(dev); 318 319 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 320 return -EOPNOTSUPP; 321 322 switch (f->command) { 323 case TC_BLOCK_BIND: 324 return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb, 325 ns, ns, f->extack); 326 case TC_BLOCK_UNBIND: 327 tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns); 328 return 0; 329 default: 330 return -EOPNOTSUPP; 331 } 332 } 333 334 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac) 335 { 336 struct netdevsim *ns = netdev_priv(dev); 337 338 /* Only refuse multicast addresses, zero address can mean unset/any. */ 339 if (vf >= ns->num_vfs || is_multicast_ether_addr(mac)) 340 return -EINVAL; 341 memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN); 342 343 return 0; 344 } 345 346 static int nsim_set_vf_vlan(struct net_device *dev, int vf, 347 u16 vlan, u8 qos, __be16 vlan_proto) 348 { 349 struct netdevsim *ns = netdev_priv(dev); 350 351 if (vf >= ns->num_vfs || vlan > 4095 || qos > 7) 352 return -EINVAL; 353 354 ns->vfconfigs[vf].vlan = vlan; 355 ns->vfconfigs[vf].qos = qos; 356 ns->vfconfigs[vf].vlan_proto = vlan_proto; 357 358 return 0; 359 } 360 361 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max) 362 { 363 struct netdevsim *ns = netdev_priv(dev); 364 365 if (vf >= ns->num_vfs) 366 return -EINVAL; 367 368 ns->vfconfigs[vf].min_tx_rate = min; 369 ns->vfconfigs[vf].max_tx_rate = max; 370 371 return 0; 372 } 373 374 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val) 375 { 376 struct netdevsim *ns = netdev_priv(dev); 377 378 if (vf >= ns->num_vfs) 379 return -EINVAL; 380 ns->vfconfigs[vf].spoofchk_enabled = val; 381 382 return 0; 383 } 384 385 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val) 386 { 387 struct netdevsim *ns = netdev_priv(dev); 388 389 if (vf >= ns->num_vfs) 390 return -EINVAL; 391 ns->vfconfigs[vf].rss_query_enabled = val; 392 393 return 0; 394 } 395 396 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val) 397 { 398 struct netdevsim *ns = netdev_priv(dev); 399 400 if (vf >= ns->num_vfs) 401 return -EINVAL; 402 ns->vfconfigs[vf].trusted = val; 403 404 return 0; 405 } 406 407 static int 408 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi) 409 { 410 struct netdevsim *ns = netdev_priv(dev); 411 412 if (vf >= ns->num_vfs) 413 return -EINVAL; 414 415 ivi->vf = vf; 416 ivi->linkstate = ns->vfconfigs[vf].link_state; 417 ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate; 418 ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate; 419 ivi->vlan = ns->vfconfigs[vf].vlan; 420 ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto; 421 ivi->qos = ns->vfconfigs[vf].qos; 422 memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN); 423 ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled; 424 ivi->trusted = ns->vfconfigs[vf].trusted; 425 ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled; 426 427 return 0; 428 } 429 430 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state) 431 { 432 struct netdevsim *ns = netdev_priv(dev); 433 434 if (vf >= ns->num_vfs) 435 return -EINVAL; 436 437 switch (state) { 438 case IFLA_VF_LINK_STATE_AUTO: 439 case IFLA_VF_LINK_STATE_ENABLE: 440 case IFLA_VF_LINK_STATE_DISABLE: 441 break; 442 default: 443 return -EINVAL; 444 } 445 446 ns->vfconfigs[vf].link_state = state; 447 448 return 0; 449 } 450 451 static int 452 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data) 453 { 454 switch (type) { 455 case TC_SETUP_BLOCK: 456 return nsim_setup_tc_block(dev, type_data); 457 default: 458 return -EOPNOTSUPP; 459 } 460 } 461 462 static int 463 nsim_set_features(struct net_device *dev, netdev_features_t features) 464 { 465 struct netdevsim *ns = netdev_priv(dev); 466 467 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) 468 return nsim_bpf_disable_tc(ns); 469 470 return 0; 471 } 472 473 static const struct net_device_ops nsim_netdev_ops = { 474 .ndo_init = nsim_init, 475 .ndo_uninit = nsim_uninit, 476 .ndo_start_xmit = nsim_start_xmit, 477 .ndo_set_rx_mode = nsim_set_rx_mode, 478 .ndo_set_mac_address = eth_mac_addr, 479 .ndo_validate_addr = eth_validate_addr, 480 .ndo_change_mtu = nsim_change_mtu, 481 .ndo_get_stats64 = nsim_get_stats64, 482 .ndo_set_vf_mac = nsim_set_vf_mac, 483 .ndo_set_vf_vlan = nsim_set_vf_vlan, 484 .ndo_set_vf_rate = nsim_set_vf_rate, 485 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk, 486 .ndo_set_vf_trust = nsim_set_vf_trust, 487 .ndo_get_vf_config = nsim_get_vf_config, 488 .ndo_set_vf_link_state = nsim_set_vf_link_state, 489 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, 490 .ndo_setup_tc = nsim_setup_tc, 491 .ndo_set_features = nsim_set_features, 492 .ndo_bpf = nsim_bpf, 493 }; 494 495 static void nsim_setup(struct net_device *dev) 496 { 497 ether_setup(dev); 498 eth_hw_addr_random(dev); 499 500 dev->netdev_ops = &nsim_netdev_ops; 501 dev->priv_destructor = nsim_free; 502 503 dev->tx_queue_len = 0; 504 dev->flags |= IFF_NOARP; 505 dev->flags &= ~IFF_MULTICAST; 506 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 507 IFF_NO_QUEUE; 508 dev->features |= NETIF_F_HIGHDMA | 509 NETIF_F_SG | 510 NETIF_F_FRAGLIST | 511 NETIF_F_HW_CSUM | 512 NETIF_F_TSO; 513 dev->hw_features |= NETIF_F_HW_TC; 514 dev->max_mtu = ETH_MAX_MTU; 515 } 516 517 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], 518 struct netlink_ext_ack *extack) 519 { 520 if (tb[IFLA_ADDRESS]) { 521 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 522 return -EINVAL; 523 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 524 return -EADDRNOTAVAIL; 525 } 526 return 0; 527 } 528 529 static int nsim_newlink(struct net *src_net, struct net_device *dev, 530 struct nlattr *tb[], struct nlattr *data[], 531 struct netlink_ext_ack *extack) 532 { 533 struct netdevsim *ns = netdev_priv(dev); 534 535 if (tb[IFLA_LINK]) { 536 struct net_device *joindev; 537 struct netdevsim *joinns; 538 539 joindev = __dev_get_by_index(src_net, 540 nla_get_u32(tb[IFLA_LINK])); 541 if (!joindev) 542 return -ENODEV; 543 if (joindev->netdev_ops != &nsim_netdev_ops) 544 return -EINVAL; 545 546 joinns = netdev_priv(joindev); 547 if (!joinns->sdev || !joinns->sdev->refcnt) 548 return -EINVAL; 549 ns->sdev = joinns->sdev; 550 } 551 552 return register_netdevice(dev); 553 } 554 555 static void nsim_dellink(struct net_device *dev, struct list_head *head) 556 { 557 unregister_netdevice_queue(dev, head); 558 } 559 560 static struct rtnl_link_ops nsim_link_ops __read_mostly = { 561 .kind = DRV_NAME, 562 .priv_size = sizeof(struct netdevsim), 563 .setup = nsim_setup, 564 .validate = nsim_validate, 565 .newlink = nsim_newlink, 566 .dellink = nsim_dellink, 567 }; 568 569 struct dentry *nsim_ddir; 570 struct dentry *nsim_sdev_ddir; 571 572 static int __init nsim_module_init(void) 573 { 574 int err; 575 576 nsim_ddir = debugfs_create_dir(DRV_NAME, NULL); 577 if (IS_ERR_OR_NULL(nsim_ddir)) 578 return -ENOMEM; 579 580 nsim_sdev_ddir = debugfs_create_dir(DRV_NAME "_sdev", NULL); 581 if (IS_ERR_OR_NULL(nsim_sdev_ddir)) { 582 err = -ENOMEM; 583 goto err_debugfs_destroy; 584 } 585 586 err = bus_register(&nsim_bus); 587 if (err) 588 goto err_sdir_destroy; 589 590 err = nsim_devlink_init(); 591 if (err) 592 goto err_unreg_bus; 593 594 err = rtnl_link_register(&nsim_link_ops); 595 if (err) 596 goto err_dl_fini; 597 598 return 0; 599 600 err_dl_fini: 601 nsim_devlink_exit(); 602 err_unreg_bus: 603 bus_unregister(&nsim_bus); 604 err_sdir_destroy: 605 debugfs_remove_recursive(nsim_sdev_ddir); 606 err_debugfs_destroy: 607 debugfs_remove_recursive(nsim_ddir); 608 return err; 609 } 610 611 static void __exit nsim_module_exit(void) 612 { 613 rtnl_link_unregister(&nsim_link_ops); 614 nsim_devlink_exit(); 615 bus_unregister(&nsim_bus); 616 debugfs_remove_recursive(nsim_sdev_ddir); 617 debugfs_remove_recursive(nsim_ddir); 618 } 619 620 module_init(nsim_module_init); 621 module_exit(nsim_module_exit); 622 MODULE_LICENSE("GPL"); 623 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 624