|
| 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/etherdevice.h> |
| 17 | +#include <linux/kernel.h> |
| 18 | +#include <linux/module.h> |
| 19 | +#include <linux/netdevice.h> |
| 20 | +#include <linux/slab.h> |
| 21 | +#include <net/netlink.h> |
| 22 | +#include <net/rtnetlink.h> |
| 23 | + |
| 24 | +#include "netdevsim.h" |
| 25 | + |
| 26 | +static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 27 | +{ |
| 28 | + struct netdevsim *ns = netdev_priv(dev); |
| 29 | + |
| 30 | + u64_stats_update_begin(&ns->syncp); |
| 31 | + ns->tx_packets++; |
| 32 | + ns->tx_bytes += skb->len; |
| 33 | + u64_stats_update_end(&ns->syncp); |
| 34 | + |
| 35 | + dev_kfree_skb(skb); |
| 36 | + |
| 37 | + return NETDEV_TX_OK; |
| 38 | +} |
| 39 | + |
| 40 | +static void nsim_set_rx_mode(struct net_device *dev) |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +static void |
| 45 | +nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) |
| 46 | +{ |
| 47 | + struct netdevsim *ns = netdev_priv(dev); |
| 48 | + unsigned int start; |
| 49 | + |
| 50 | + do { |
| 51 | + start = u64_stats_fetch_begin(&ns->syncp); |
| 52 | + stats->tx_bytes = ns->tx_bytes; |
| 53 | + stats->tx_packets = ns->tx_packets; |
| 54 | + } while (u64_stats_fetch_retry(&ns->syncp, start)); |
| 55 | +} |
| 56 | + |
| 57 | +static const struct net_device_ops nsim_netdev_ops = { |
| 58 | + .ndo_start_xmit = nsim_start_xmit, |
| 59 | + .ndo_set_rx_mode = nsim_set_rx_mode, |
| 60 | + .ndo_set_mac_address = eth_mac_addr, |
| 61 | + .ndo_validate_addr = eth_validate_addr, |
| 62 | + .ndo_get_stats64 = nsim_get_stats64, |
| 63 | +}; |
| 64 | + |
| 65 | +static void nsim_setup(struct net_device *dev) |
| 66 | +{ |
| 67 | + ether_setup(dev); |
| 68 | + eth_hw_addr_random(dev); |
| 69 | + |
| 70 | + dev->netdev_ops = &nsim_netdev_ops; |
| 71 | + dev->needs_free_netdev = true; |
| 72 | + |
| 73 | + dev->tx_queue_len = 0; |
| 74 | + dev->flags |= IFF_NOARP; |
| 75 | + dev->flags &= ~IFF_MULTICAST; |
| 76 | + dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | |
| 77 | + IFF_NO_QUEUE; |
| 78 | + dev->features |= NETIF_F_HIGHDMA | |
| 79 | + NETIF_F_SG | |
| 80 | + NETIF_F_FRAGLIST | |
| 81 | + NETIF_F_HW_CSUM | |
| 82 | + NETIF_F_TSO; |
| 83 | + dev->max_mtu = ETH_MAX_MTU; |
| 84 | +} |
| 85 | + |
| 86 | +static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], |
| 87 | + struct netlink_ext_ack *extack) |
| 88 | +{ |
| 89 | + if (tb[IFLA_ADDRESS]) { |
| 90 | + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 91 | + return -EINVAL; |
| 92 | + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 93 | + return -EADDRNOTAVAIL; |
| 94 | + } |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +static struct rtnl_link_ops nsim_link_ops __read_mostly = { |
| 99 | + .kind = DRV_NAME, |
| 100 | + .priv_size = sizeof(struct netdevsim), |
| 101 | + .setup = nsim_setup, |
| 102 | + .validate = nsim_validate, |
| 103 | +}; |
| 104 | + |
| 105 | +static int __init nsim_module_init(void) |
| 106 | +{ |
| 107 | + return rtnl_link_register(&nsim_link_ops); |
| 108 | +} |
| 109 | + |
| 110 | +static void __exit nsim_module_exit(void) |
| 111 | +{ |
| 112 | + rtnl_link_unregister(&nsim_link_ops); |
| 113 | +} |
| 114 | + |
| 115 | +module_init(nsim_module_init); |
| 116 | +module_exit(nsim_module_exit); |
| 117 | +MODULE_LICENSE("GPL"); |
| 118 | +MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
0 commit comments