Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2017 Netronome Systems, Inc.
0003  *
0004  * This software is licensed under the GNU General License Version 2,
0005  * June 1991 as shown in the file COPYING in the top-level directory of this
0006  * source tree.
0007  *
0008  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
0009  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
0010  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0011  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
0012  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
0013  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
0014  */
0015 
0016 #include <linux/debugfs.h>
0017 #include <linux/etherdevice.h>
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <linux/netdevice.h>
0021 #include <linux/slab.h>
0022 #include <net/netlink.h>
0023 #include <net/pkt_cls.h>
0024 #include <net/rtnetlink.h>
0025 #include <net/udp_tunnel.h>
0026 
0027 #include "netdevsim.h"
0028 
0029 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
0030 {
0031     struct netdevsim *ns = netdev_priv(dev);
0032 
0033     if (!nsim_ipsec_tx(ns, skb))
0034         goto out;
0035 
0036     u64_stats_update_begin(&ns->syncp);
0037     ns->tx_packets++;
0038     ns->tx_bytes += skb->len;
0039     u64_stats_update_end(&ns->syncp);
0040 
0041 out:
0042     dev_kfree_skb(skb);
0043 
0044     return NETDEV_TX_OK;
0045 }
0046 
0047 static void nsim_set_rx_mode(struct net_device *dev)
0048 {
0049 }
0050 
0051 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
0052 {
0053     struct netdevsim *ns = netdev_priv(dev);
0054 
0055     if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
0056         return -EBUSY;
0057 
0058     dev->mtu = new_mtu;
0059 
0060     return 0;
0061 }
0062 
0063 static void
0064 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
0065 {
0066     struct netdevsim *ns = netdev_priv(dev);
0067     unsigned int start;
0068 
0069     do {
0070         start = u64_stats_fetch_begin_irq(&ns->syncp);
0071         stats->tx_bytes = ns->tx_bytes;
0072         stats->tx_packets = ns->tx_packets;
0073     } while (u64_stats_fetch_retry_irq(&ns->syncp, start));
0074 }
0075 
0076 static int
0077 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
0078 {
0079     return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
0080 }
0081 
0082 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
0083 {
0084     struct netdevsim *ns = netdev_priv(dev);
0085     struct nsim_dev *nsim_dev = ns->nsim_dev;
0086 
0087     /* Only refuse multicast addresses, zero address can mean unset/any. */
0088     if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
0089         return -EINVAL;
0090     memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
0091 
0092     return 0;
0093 }
0094 
0095 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
0096                 u16 vlan, u8 qos, __be16 vlan_proto)
0097 {
0098     struct netdevsim *ns = netdev_priv(dev);
0099     struct nsim_dev *nsim_dev = ns->nsim_dev;
0100 
0101     if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
0102         return -EINVAL;
0103 
0104     nsim_dev->vfconfigs[vf].vlan = vlan;
0105     nsim_dev->vfconfigs[vf].qos = qos;
0106     nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
0107 
0108     return 0;
0109 }
0110 
0111 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
0112 {
0113     struct netdevsim *ns = netdev_priv(dev);
0114     struct nsim_dev *nsim_dev = ns->nsim_dev;
0115 
0116     if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
0117         pr_err("Not supported in switchdev mode. Please use devlink API.\n");
0118         return -EOPNOTSUPP;
0119     }
0120 
0121     if (vf >= nsim_dev_get_vfs(nsim_dev))
0122         return -EINVAL;
0123 
0124     nsim_dev->vfconfigs[vf].min_tx_rate = min;
0125     nsim_dev->vfconfigs[vf].max_tx_rate = max;
0126 
0127     return 0;
0128 }
0129 
0130 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
0131 {
0132     struct netdevsim *ns = netdev_priv(dev);
0133     struct nsim_dev *nsim_dev = ns->nsim_dev;
0134 
0135     if (vf >= nsim_dev_get_vfs(nsim_dev))
0136         return -EINVAL;
0137     nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
0138 
0139     return 0;
0140 }
0141 
0142 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
0143 {
0144     struct netdevsim *ns = netdev_priv(dev);
0145     struct nsim_dev *nsim_dev = ns->nsim_dev;
0146 
0147     if (vf >= nsim_dev_get_vfs(nsim_dev))
0148         return -EINVAL;
0149     nsim_dev->vfconfigs[vf].rss_query_enabled = val;
0150 
0151     return 0;
0152 }
0153 
0154 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
0155 {
0156     struct netdevsim *ns = netdev_priv(dev);
0157     struct nsim_dev *nsim_dev = ns->nsim_dev;
0158 
0159     if (vf >= nsim_dev_get_vfs(nsim_dev))
0160         return -EINVAL;
0161     nsim_dev->vfconfigs[vf].trusted = val;
0162 
0163     return 0;
0164 }
0165 
0166 static int
0167 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
0168 {
0169     struct netdevsim *ns = netdev_priv(dev);
0170     struct nsim_dev *nsim_dev = ns->nsim_dev;
0171 
0172     if (vf >= nsim_dev_get_vfs(nsim_dev))
0173         return -EINVAL;
0174 
0175     ivi->vf = vf;
0176     ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
0177     ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
0178     ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
0179     ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
0180     ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
0181     ivi->qos = nsim_dev->vfconfigs[vf].qos;
0182     memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
0183     ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
0184     ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
0185     ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
0186 
0187     return 0;
0188 }
0189 
0190 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
0191 {
0192     struct netdevsim *ns = netdev_priv(dev);
0193     struct nsim_dev *nsim_dev = ns->nsim_dev;
0194 
0195     if (vf >= nsim_dev_get_vfs(nsim_dev))
0196         return -EINVAL;
0197 
0198     switch (state) {
0199     case IFLA_VF_LINK_STATE_AUTO:
0200     case IFLA_VF_LINK_STATE_ENABLE:
0201     case IFLA_VF_LINK_STATE_DISABLE:
0202         break;
0203     default:
0204         return -EINVAL;
0205     }
0206 
0207     nsim_dev->vfconfigs[vf].link_state = state;
0208 
0209     return 0;
0210 }
0211 
0212 static LIST_HEAD(nsim_block_cb_list);
0213 
0214 static int
0215 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
0216 {
0217     struct netdevsim *ns = netdev_priv(dev);
0218 
0219     switch (type) {
0220     case TC_SETUP_BLOCK:
0221         return flow_block_cb_setup_simple(type_data,
0222                           &nsim_block_cb_list,
0223                           nsim_setup_tc_block_cb,
0224                           ns, ns, true);
0225     default:
0226         return -EOPNOTSUPP;
0227     }
0228 }
0229 
0230 static int
0231 nsim_set_features(struct net_device *dev, netdev_features_t features)
0232 {
0233     struct netdevsim *ns = netdev_priv(dev);
0234 
0235     if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
0236         return nsim_bpf_disable_tc(ns);
0237 
0238     return 0;
0239 }
0240 
0241 static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
0242 {
0243     struct netdevsim *ns = netdev_priv(dev);
0244 
0245     return &ns->nsim_dev_port->devlink_port;
0246 }
0247 
0248 static const struct net_device_ops nsim_netdev_ops = {
0249     .ndo_start_xmit     = nsim_start_xmit,
0250     .ndo_set_rx_mode    = nsim_set_rx_mode,
0251     .ndo_set_mac_address    = eth_mac_addr,
0252     .ndo_validate_addr  = eth_validate_addr,
0253     .ndo_change_mtu     = nsim_change_mtu,
0254     .ndo_get_stats64    = nsim_get_stats64,
0255     .ndo_set_vf_mac     = nsim_set_vf_mac,
0256     .ndo_set_vf_vlan    = nsim_set_vf_vlan,
0257     .ndo_set_vf_rate    = nsim_set_vf_rate,
0258     .ndo_set_vf_spoofchk    = nsim_set_vf_spoofchk,
0259     .ndo_set_vf_trust   = nsim_set_vf_trust,
0260     .ndo_get_vf_config  = nsim_get_vf_config,
0261     .ndo_set_vf_link_state  = nsim_set_vf_link_state,
0262     .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
0263     .ndo_setup_tc       = nsim_setup_tc,
0264     .ndo_set_features   = nsim_set_features,
0265     .ndo_bpf        = nsim_bpf,
0266     .ndo_get_devlink_port   = nsim_get_devlink_port,
0267 };
0268 
0269 static const struct net_device_ops nsim_vf_netdev_ops = {
0270     .ndo_start_xmit     = nsim_start_xmit,
0271     .ndo_set_rx_mode    = nsim_set_rx_mode,
0272     .ndo_set_mac_address    = eth_mac_addr,
0273     .ndo_validate_addr  = eth_validate_addr,
0274     .ndo_change_mtu     = nsim_change_mtu,
0275     .ndo_get_stats64    = nsim_get_stats64,
0276     .ndo_setup_tc       = nsim_setup_tc,
0277     .ndo_set_features   = nsim_set_features,
0278     .ndo_get_devlink_port   = nsim_get_devlink_port,
0279 };
0280 
0281 static void nsim_setup(struct net_device *dev)
0282 {
0283     ether_setup(dev);
0284     eth_hw_addr_random(dev);
0285 
0286     dev->tx_queue_len = 0;
0287     dev->flags |= IFF_NOARP;
0288     dev->flags &= ~IFF_MULTICAST;
0289     dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
0290                IFF_NO_QUEUE;
0291     dev->features |= NETIF_F_HIGHDMA |
0292              NETIF_F_SG |
0293              NETIF_F_FRAGLIST |
0294              NETIF_F_HW_CSUM |
0295              NETIF_F_TSO;
0296     dev->hw_features |= NETIF_F_HW_TC;
0297     dev->max_mtu = ETH_MAX_MTU;
0298 }
0299 
0300 static int nsim_init_netdevsim(struct netdevsim *ns)
0301 {
0302     int err;
0303 
0304     ns->netdev->netdev_ops = &nsim_netdev_ops;
0305 
0306     err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
0307     if (err)
0308         return err;
0309 
0310     rtnl_lock();
0311     err = nsim_bpf_init(ns);
0312     if (err)
0313         goto err_utn_destroy;
0314 
0315     nsim_ipsec_init(ns);
0316 
0317     err = register_netdevice(ns->netdev);
0318     if (err)
0319         goto err_ipsec_teardown;
0320     rtnl_unlock();
0321     return 0;
0322 
0323 err_ipsec_teardown:
0324     nsim_ipsec_teardown(ns);
0325     nsim_bpf_uninit(ns);
0326 err_utn_destroy:
0327     rtnl_unlock();
0328     nsim_udp_tunnels_info_destroy(ns->netdev);
0329     return err;
0330 }
0331 
0332 static int nsim_init_netdevsim_vf(struct netdevsim *ns)
0333 {
0334     int err;
0335 
0336     ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
0337     rtnl_lock();
0338     err = register_netdevice(ns->netdev);
0339     rtnl_unlock();
0340     return err;
0341 }
0342 
0343 struct netdevsim *
0344 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
0345 {
0346     struct net_device *dev;
0347     struct netdevsim *ns;
0348     int err;
0349 
0350     dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
0351                   nsim_dev->nsim_bus_dev->num_queues);
0352     if (!dev)
0353         return ERR_PTR(-ENOMEM);
0354 
0355     dev_net_set(dev, nsim_dev_net(nsim_dev));
0356     ns = netdev_priv(dev);
0357     ns->netdev = dev;
0358     u64_stats_init(&ns->syncp);
0359     ns->nsim_dev = nsim_dev;
0360     ns->nsim_dev_port = nsim_dev_port;
0361     ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
0362     SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
0363     nsim_ethtool_init(ns);
0364     if (nsim_dev_port_is_pf(nsim_dev_port))
0365         err = nsim_init_netdevsim(ns);
0366     else
0367         err = nsim_init_netdevsim_vf(ns);
0368     if (err)
0369         goto err_free_netdev;
0370     return ns;
0371 
0372 err_free_netdev:
0373     free_netdev(dev);
0374     return ERR_PTR(err);
0375 }
0376 
0377 void nsim_destroy(struct netdevsim *ns)
0378 {
0379     struct net_device *dev = ns->netdev;
0380 
0381     rtnl_lock();
0382     unregister_netdevice(dev);
0383     if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
0384         nsim_ipsec_teardown(ns);
0385         nsim_bpf_uninit(ns);
0386     }
0387     rtnl_unlock();
0388     if (nsim_dev_port_is_pf(ns->nsim_dev_port))
0389         nsim_udp_tunnels_info_destroy(dev);
0390     free_netdev(dev);
0391 }
0392 
0393 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
0394              struct netlink_ext_ack *extack)
0395 {
0396     NL_SET_ERR_MSG_MOD(extack,
0397                "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
0398     return -EOPNOTSUPP;
0399 }
0400 
0401 static struct rtnl_link_ops nsim_link_ops __read_mostly = {
0402     .kind       = DRV_NAME,
0403     .validate   = nsim_validate,
0404 };
0405 
0406 static int __init nsim_module_init(void)
0407 {
0408     int err;
0409 
0410     err = nsim_dev_init();
0411     if (err)
0412         return err;
0413 
0414     err = nsim_bus_init();
0415     if (err)
0416         goto err_dev_exit;
0417 
0418     err = rtnl_link_register(&nsim_link_ops);
0419     if (err)
0420         goto err_bus_exit;
0421 
0422     return 0;
0423 
0424 err_bus_exit:
0425     nsim_bus_exit();
0426 err_dev_exit:
0427     nsim_dev_exit();
0428     return err;
0429 }
0430 
0431 static void __exit nsim_module_exit(void)
0432 {
0433     rtnl_link_unregister(&nsim_link_ops);
0434     nsim_bus_exit();
0435     nsim_dev_exit();
0436 }
0437 
0438 module_init(nsim_module_init);
0439 module_exit(nsim_module_exit);
0440 MODULE_LICENSE("GPL");
0441 MODULE_ALIAS_RTNL_LINK(DRV_NAME);