netdev.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #include <linux/debugfs.h>
  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/pkt_cls.h>
  23. #include <net/rtnetlink.h>
  24. #include <net/udp_tunnel.h>
  25. #include "netdevsim.h"
  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. if (!nsim_ipsec_tx(ns, skb))
  30. goto out;
  31. u64_stats_update_begin(&ns->syncp);
  32. ns->tx_packets++;
  33. ns->tx_bytes += skb->len;
  34. u64_stats_update_end(&ns->syncp);
  35. out:
  36. dev_kfree_skb(skb);
  37. return NETDEV_TX_OK;
  38. }
  39. static void nsim_set_rx_mode(struct net_device *dev)
  40. {
  41. }
  42. static int nsim_change_mtu(struct net_device *dev, int new_mtu)
  43. {
  44. struct netdevsim *ns = netdev_priv(dev);
  45. if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
  46. return -EBUSY;
  47. dev->mtu = new_mtu;
  48. return 0;
  49. }
  50. static void
  51. nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  52. {
  53. struct netdevsim *ns = netdev_priv(dev);
  54. unsigned int start;
  55. do {
  56. start = u64_stats_fetch_begin(&ns->syncp);
  57. stats->tx_bytes = ns->tx_bytes;
  58. stats->tx_packets = ns->tx_packets;
  59. } while (u64_stats_fetch_retry(&ns->syncp, start));
  60. }
  61. static int
  62. nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  63. {
  64. return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
  65. }
  66. static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
  67. {
  68. struct netdevsim *ns = netdev_priv(dev);
  69. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  70. /* Only refuse multicast addresses, zero address can mean unset/any. */
  71. if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
  72. return -EINVAL;
  73. memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
  74. return 0;
  75. }
  76. static int nsim_set_vf_vlan(struct net_device *dev, int vf,
  77. u16 vlan, u8 qos, __be16 vlan_proto)
  78. {
  79. struct netdevsim *ns = netdev_priv(dev);
  80. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  81. if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
  82. return -EINVAL;
  83. nsim_bus_dev->vfconfigs[vf].vlan = vlan;
  84. nsim_bus_dev->vfconfigs[vf].qos = qos;
  85. nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
  86. return 0;
  87. }
  88. static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
  89. {
  90. struct netdevsim *ns = netdev_priv(dev);
  91. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  92. if (vf >= nsim_bus_dev->num_vfs)
  93. return -EINVAL;
  94. nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
  95. nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
  96. return 0;
  97. }
  98. static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
  99. {
  100. struct netdevsim *ns = netdev_priv(dev);
  101. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  102. if (vf >= nsim_bus_dev->num_vfs)
  103. return -EINVAL;
  104. nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
  105. return 0;
  106. }
  107. static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
  108. {
  109. struct netdevsim *ns = netdev_priv(dev);
  110. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  111. if (vf >= nsim_bus_dev->num_vfs)
  112. return -EINVAL;
  113. nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
  114. return 0;
  115. }
  116. static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
  117. {
  118. struct netdevsim *ns = netdev_priv(dev);
  119. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  120. if (vf >= nsim_bus_dev->num_vfs)
  121. return -EINVAL;
  122. nsim_bus_dev->vfconfigs[vf].trusted = val;
  123. return 0;
  124. }
  125. static int
  126. nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
  127. {
  128. struct netdevsim *ns = netdev_priv(dev);
  129. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  130. if (vf >= nsim_bus_dev->num_vfs)
  131. return -EINVAL;
  132. ivi->vf = vf;
  133. ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
  134. ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
  135. ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
  136. ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
  137. ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
  138. ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
  139. memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
  140. ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
  141. ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
  142. ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
  143. return 0;
  144. }
  145. static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
  146. {
  147. struct netdevsim *ns = netdev_priv(dev);
  148. struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
  149. if (vf >= nsim_bus_dev->num_vfs)
  150. return -EINVAL;
  151. switch (state) {
  152. case IFLA_VF_LINK_STATE_AUTO:
  153. case IFLA_VF_LINK_STATE_ENABLE:
  154. case IFLA_VF_LINK_STATE_DISABLE:
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. nsim_bus_dev->vfconfigs[vf].link_state = state;
  160. return 0;
  161. }
  162. static LIST_HEAD(nsim_block_cb_list);
  163. static int
  164. nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
  165. {
  166. struct netdevsim *ns = netdev_priv(dev);
  167. switch (type) {
  168. case TC_SETUP_BLOCK:
  169. return flow_block_cb_setup_simple(type_data,
  170. &nsim_block_cb_list,
  171. nsim_setup_tc_block_cb,
  172. ns, ns, true);
  173. default:
  174. return -EOPNOTSUPP;
  175. }
  176. }
  177. static int
  178. nsim_set_features(struct net_device *dev, netdev_features_t features)
  179. {
  180. struct netdevsim *ns = netdev_priv(dev);
  181. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
  182. return nsim_bpf_disable_tc(ns);
  183. return 0;
  184. }
  185. static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
  186. {
  187. struct netdevsim *ns = netdev_priv(dev);
  188. return &ns->nsim_dev_port->devlink_port;
  189. }
  190. static const struct net_device_ops nsim_netdev_ops = {
  191. .ndo_start_xmit = nsim_start_xmit,
  192. .ndo_set_rx_mode = nsim_set_rx_mode,
  193. .ndo_set_mac_address = eth_mac_addr,
  194. .ndo_validate_addr = eth_validate_addr,
  195. .ndo_change_mtu = nsim_change_mtu,
  196. .ndo_get_stats64 = nsim_get_stats64,
  197. .ndo_set_vf_mac = nsim_set_vf_mac,
  198. .ndo_set_vf_vlan = nsim_set_vf_vlan,
  199. .ndo_set_vf_rate = nsim_set_vf_rate,
  200. .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
  201. .ndo_set_vf_trust = nsim_set_vf_trust,
  202. .ndo_get_vf_config = nsim_get_vf_config,
  203. .ndo_set_vf_link_state = nsim_set_vf_link_state,
  204. .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
  205. .ndo_setup_tc = nsim_setup_tc,
  206. .ndo_set_features = nsim_set_features,
  207. .ndo_bpf = nsim_bpf,
  208. .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
  209. .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
  210. .ndo_get_devlink_port = nsim_get_devlink_port,
  211. };
  212. static void nsim_setup(struct net_device *dev)
  213. {
  214. ether_setup(dev);
  215. eth_hw_addr_random(dev);
  216. dev->tx_queue_len = 0;
  217. dev->flags |= IFF_NOARP;
  218. dev->flags &= ~IFF_MULTICAST;
  219. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
  220. IFF_NO_QUEUE;
  221. dev->features |= NETIF_F_HIGHDMA |
  222. NETIF_F_SG |
  223. NETIF_F_FRAGLIST |
  224. NETIF_F_HW_CSUM |
  225. NETIF_F_TSO;
  226. dev->hw_features |= NETIF_F_HW_TC;
  227. dev->max_mtu = ETH_MAX_MTU;
  228. }
  229. struct netdevsim *
  230. nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
  231. {
  232. struct net_device *dev;
  233. struct netdevsim *ns;
  234. int err;
  235. dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
  236. if (!dev)
  237. return ERR_PTR(-ENOMEM);
  238. dev_net_set(dev, nsim_dev_net(nsim_dev));
  239. ns = netdev_priv(dev);
  240. ns->netdev = dev;
  241. u64_stats_init(&ns->syncp);
  242. ns->nsim_dev = nsim_dev;
  243. ns->nsim_dev_port = nsim_dev_port;
  244. ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
  245. SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
  246. dev->netdev_ops = &nsim_netdev_ops;
  247. nsim_ethtool_init(ns);
  248. err = nsim_udp_tunnels_info_create(nsim_dev, dev);
  249. if (err)
  250. goto err_free_netdev;
  251. rtnl_lock();
  252. err = nsim_bpf_init(ns);
  253. if (err)
  254. goto err_utn_destroy;
  255. nsim_ipsec_init(ns);
  256. err = register_netdevice(dev);
  257. if (err)
  258. goto err_ipsec_teardown;
  259. rtnl_unlock();
  260. return ns;
  261. err_ipsec_teardown:
  262. nsim_ipsec_teardown(ns);
  263. nsim_bpf_uninit(ns);
  264. err_utn_destroy:
  265. rtnl_unlock();
  266. nsim_udp_tunnels_info_destroy(dev);
  267. err_free_netdev:
  268. free_netdev(dev);
  269. return ERR_PTR(err);
  270. }
  271. void nsim_destroy(struct netdevsim *ns)
  272. {
  273. struct net_device *dev = ns->netdev;
  274. rtnl_lock();
  275. unregister_netdevice(dev);
  276. nsim_ipsec_teardown(ns);
  277. nsim_bpf_uninit(ns);
  278. rtnl_unlock();
  279. nsim_udp_tunnels_info_destroy(dev);
  280. free_netdev(dev);
  281. }
  282. static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
  283. struct netlink_ext_ack *extack)
  284. {
  285. NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
  286. return -EOPNOTSUPP;
  287. }
  288. static struct rtnl_link_ops nsim_link_ops __read_mostly = {
  289. .kind = DRV_NAME,
  290. .validate = nsim_validate,
  291. };
  292. static int __init nsim_module_init(void)
  293. {
  294. int err;
  295. err = nsim_dev_init();
  296. if (err)
  297. return err;
  298. err = nsim_bus_init();
  299. if (err)
  300. goto err_dev_exit;
  301. err = rtnl_link_register(&nsim_link_ops);
  302. if (err)
  303. goto err_bus_exit;
  304. return 0;
  305. err_bus_exit:
  306. nsim_bus_exit();
  307. err_dev_exit:
  308. nsim_dev_exit();
  309. return err;
  310. }
  311. static void __exit nsim_module_exit(void)
  312. {
  313. rtnl_link_unregister(&nsim_link_ops);
  314. nsim_bus_exit();
  315. nsim_dev_exit();
  316. }
  317. module_init(nsim_module_init);
  318. module_exit(nsim_module_exit);
  319. MODULE_LICENSE("GPL");
  320. MODULE_ALIAS_RTNL_LINK(DRV_NAME);