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