netdev.c 9.2 KB

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