netdev.c (8320d145912738655cf631d27aa1829d8b17804e) | netdev.c (e05b2d141fef22cfac1928cf0eb6890e5dae4216) |
---|---|
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" --- 11 unchanged lines hidden (view full) --- 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 | 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" --- 11 unchanged lines hidden (view full) --- 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 |
28static int nsim_get_port_parent_id(struct net_device *dev, 29 struct netdev_phys_item_id *ppid) 30{ 31 struct netdevsim *ns = netdev_priv(dev); 32 33 memcpy(ppid, &ns->nsim_dev->switch_id, sizeof(*ppid)); 34 return 0; 35} 36 37static int nsim_init(struct net_device *dev) 38{ 39 struct netdevsim *ns = netdev_priv(dev); 40 char dev_link_name[32]; 41 int err; 42 43 ns->ddir = debugfs_create_dir("0", ns->nsim_dev->ports_ddir); 44 if (IS_ERR_OR_NULL(ns->ddir)) 45 return -ENOMEM; 46 47 sprintf(dev_link_name, "../../../" DRV_NAME "%u", 48 ns->nsim_dev->nsim_bus_dev->dev.id); 49 debugfs_create_symlink("dev", ns->ddir, dev_link_name); 50 51 err = nsim_bpf_init(ns); 52 if (err) 53 goto err_debugfs_destroy; 54 55 nsim_ipsec_init(ns); 56 57 return 0; 58 59err_debugfs_destroy: 60 debugfs_remove_recursive(ns->ddir); 61 return err; 62} 63 64static void nsim_uninit(struct net_device *dev) 65{ 66 struct netdevsim *ns = netdev_priv(dev); 67 68 nsim_ipsec_teardown(ns); 69 debugfs_remove_recursive(ns->ddir); 70 nsim_bpf_uninit(ns); 71} 72 73static void nsim_free(struct net_device *dev) 74{ 75 struct netdevsim *ns = netdev_priv(dev); 76 77 nsim_bus_dev_del(ns->nsim_bus_dev); 78 /* netdev and vf state will be freed out of device_release() */ 79} 80 | |
81static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 82{ 83 struct netdevsim *ns = netdev_priv(dev); 84 85 if (!nsim_ipsec_tx(ns, skb)) 86 goto out; 87 88 u64_stats_update_begin(&ns->syncp); --- 205 unchanged lines hidden (view full) --- 294 295 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) 296 return nsim_bpf_disable_tc(ns); 297 298 return 0; 299} 300 301static const struct net_device_ops nsim_netdev_ops = { | 28static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 29{ 30 struct netdevsim *ns = netdev_priv(dev); 31 32 if (!nsim_ipsec_tx(ns, skb)) 33 goto out; 34 35 u64_stats_update_begin(&ns->syncp); --- 205 unchanged lines hidden (view full) --- 241 242 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) 243 return nsim_bpf_disable_tc(ns); 244 245 return 0; 246} 247 248static const struct net_device_ops nsim_netdev_ops = { |
302 .ndo_init = nsim_init, 303 .ndo_uninit = nsim_uninit, | |
304 .ndo_start_xmit = nsim_start_xmit, 305 .ndo_set_rx_mode = nsim_set_rx_mode, 306 .ndo_set_mac_address = eth_mac_addr, 307 .ndo_validate_addr = eth_validate_addr, 308 .ndo_change_mtu = nsim_change_mtu, 309 .ndo_get_stats64 = nsim_get_stats64, 310 .ndo_set_vf_mac = nsim_set_vf_mac, 311 .ndo_set_vf_vlan = nsim_set_vf_vlan, 312 .ndo_set_vf_rate = nsim_set_vf_rate, 313 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk, 314 .ndo_set_vf_trust = nsim_set_vf_trust, 315 .ndo_get_vf_config = nsim_get_vf_config, 316 .ndo_set_vf_link_state = nsim_set_vf_link_state, 317 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, 318 .ndo_setup_tc = nsim_setup_tc, 319 .ndo_set_features = nsim_set_features, 320 .ndo_bpf = nsim_bpf, | 249 .ndo_start_xmit = nsim_start_xmit, 250 .ndo_set_rx_mode = nsim_set_rx_mode, 251 .ndo_set_mac_address = eth_mac_addr, 252 .ndo_validate_addr = eth_validate_addr, 253 .ndo_change_mtu = nsim_change_mtu, 254 .ndo_get_stats64 = nsim_get_stats64, 255 .ndo_set_vf_mac = nsim_set_vf_mac, 256 .ndo_set_vf_vlan = nsim_set_vf_vlan, 257 .ndo_set_vf_rate = nsim_set_vf_rate, 258 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk, 259 .ndo_set_vf_trust = nsim_set_vf_trust, 260 .ndo_get_vf_config = nsim_get_vf_config, 261 .ndo_set_vf_link_state = nsim_set_vf_link_state, 262 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, 263 .ndo_setup_tc = nsim_setup_tc, 264 .ndo_set_features = nsim_set_features, 265 .ndo_bpf = nsim_bpf, |
321 .ndo_get_port_parent_id = nsim_get_port_parent_id, | |
322}; 323 324static void nsim_setup(struct net_device *dev) 325{ 326 ether_setup(dev); 327 eth_hw_addr_random(dev); 328 | 266}; 267 268static void nsim_setup(struct net_device *dev) 269{ 270 ether_setup(dev); 271 eth_hw_addr_random(dev); 272 |
329 dev->netdev_ops = &nsim_netdev_ops; 330 dev->needs_free_netdev = true; 331 dev->priv_destructor = nsim_free; 332 | |
333 dev->tx_queue_len = 0; 334 dev->flags |= IFF_NOARP; 335 dev->flags &= ~IFF_MULTICAST; 336 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 337 IFF_NO_QUEUE; 338 dev->features |= NETIF_F_HIGHDMA | 339 NETIF_F_SG | 340 NETIF_F_FRAGLIST | 341 NETIF_F_HW_CSUM | 342 NETIF_F_TSO; 343 dev->hw_features |= NETIF_F_HW_TC; 344 dev->max_mtu = ETH_MAX_MTU; 345} 346 | 273 dev->tx_queue_len = 0; 274 dev->flags |= IFF_NOARP; 275 dev->flags &= ~IFF_MULTICAST; 276 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 277 IFF_NO_QUEUE; 278 dev->features |= NETIF_F_HIGHDMA | 279 NETIF_F_SG | 280 NETIF_F_FRAGLIST | 281 NETIF_F_HW_CSUM | 282 NETIF_F_TSO; 283 dev->hw_features |= NETIF_F_HW_TC; 284 dev->max_mtu = ETH_MAX_MTU; 285} 286 |
347static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], 348 struct netlink_ext_ack *extack) | 287struct netdevsim * 288nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) |
349{ | 289{ |
350 if (tb[IFLA_ADDRESS]) { 351 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 352 return -EINVAL; 353 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 354 return -EADDRNOTAVAIL; 355 } 356 return 0; 357} 358 359static int nsim_newlink(struct net *src_net, struct net_device *dev, 360 struct nlattr *tb[], struct nlattr *data[], 361 struct netlink_ext_ack *extack) 362{ 363 struct netdevsim *ns = netdev_priv(dev); | 290 struct net_device *dev; 291 struct netdevsim *ns; |
364 int err; 365 | 292 int err; 293 |
366 ns->netdev = dev; 367 ns->nsim_bus_dev = nsim_bus_dev_new_with_ns(ns); 368 if (IS_ERR(ns->nsim_bus_dev)) 369 return PTR_ERR(ns->nsim_bus_dev); | 294 dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup); 295 if (!dev) 296 return ERR_PTR(-ENOMEM); |
370 | 297 |
298 ns = netdev_priv(dev); 299 ns->netdev = dev; 300 ns->nsim_dev = nsim_dev; 301 ns->nsim_dev_port = nsim_dev_port; 302 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev; |
|
371 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev); | 303 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev); |
304 dev->netdev_ops = &nsim_netdev_ops; |
|
372 | 305 |
373 ns->nsim_dev = dev_get_drvdata(&ns->nsim_bus_dev->dev); | 306 rtnl_lock(); 307 err = nsim_bpf_init(ns); 308 if (err) 309 goto err_free_netdev; |
374 | 310 |
311 nsim_ipsec_init(ns); 312 |
|
375 err = register_netdevice(dev); 376 if (err) | 313 err = register_netdevice(dev); 314 if (err) |
377 goto err_dev_del; 378 return 0; | 315 goto err_ipsec_teardown; 316 rtnl_unlock(); |
379 | 317 |
380err_dev_del: 381 nsim_bus_dev_del(ns->nsim_bus_dev); 382 return err; | 318 return ns; 319 320err_ipsec_teardown: 321 nsim_ipsec_teardown(ns); 322 nsim_bpf_uninit(ns); 323 rtnl_unlock(); 324err_free_netdev: 325 free_netdev(dev); 326 return ERR_PTR(err); |
383} 384 | 327} 328 |
329void nsim_destroy(struct netdevsim *ns) 330{ 331 struct net_device *dev = ns->netdev; 332 333 rtnl_lock(); 334 unregister_netdevice(dev); 335 nsim_ipsec_teardown(ns); 336 nsim_bpf_uninit(ns); 337 rtnl_unlock(); 338 free_netdev(dev); 339} 340 341static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], 342 struct netlink_ext_ack *extack) 343{ 344 NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device"); 345 return -EOPNOTSUPP; 346} 347 |
|
385static struct rtnl_link_ops nsim_link_ops __read_mostly = { 386 .kind = DRV_NAME, | 348static struct rtnl_link_ops nsim_link_ops __read_mostly = { 349 .kind = DRV_NAME, |
387 .priv_size = sizeof(struct netdevsim), 388 .setup = nsim_setup, | |
389 .validate = nsim_validate, | 350 .validate = nsim_validate, |
390 .newlink = nsim_newlink, | |
391}; 392 393static int __init nsim_module_init(void) 394{ 395 int err; 396 397 err = nsim_dev_init(); 398 if (err) --- 30 unchanged lines hidden --- | 351}; 352 353static int __init nsim_module_init(void) 354{ 355 int err; 356 357 err = nsim_dev_init(); 358 if (err) --- 30 unchanged lines hidden --- |