netdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_irq(&ns->syncp);
  57. stats->tx_bytes = ns->tx_bytes;
  58. stats->tx_packets = ns->tx_packets;
  59. } while (u64_stats_fetch_retry_irq(&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_dev *nsim_dev = ns->nsim_dev;
  70. /* Only refuse multicast addresses, zero address can mean unset/any. */
  71. if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
  72. return -EINVAL;
  73. memcpy(nsim_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_dev *nsim_dev = ns->nsim_dev;
  81. if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
  82. return -EINVAL;
  83. nsim_dev->vfconfigs[vf].vlan = vlan;
  84. nsim_dev->vfconfigs[vf].qos = qos;
  85. nsim_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_dev *nsim_dev = ns->nsim_dev;
  92. if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
  93. pr_err("Not supported in switchdev mode. Please use devlink API.\n");
  94. return -EOPNOTSUPP;
  95. }
  96. if (vf >= nsim_dev_get_vfs(nsim_dev))
  97. return -EINVAL;
  98. nsim_dev->vfconfigs[vf].min_tx_rate = min;
  99. nsim_dev->vfconfigs[vf].max_tx_rate = max;
  100. return 0;
  101. }
  102. static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
  103. {
  104. struct netdevsim *ns = netdev_priv(dev);
  105. struct nsim_dev *nsim_dev = ns->nsim_dev;
  106. if (vf >= nsim_dev_get_vfs(nsim_dev))
  107. return -EINVAL;
  108. nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
  109. return 0;
  110. }
  111. static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
  112. {
  113. struct netdevsim *ns = netdev_priv(dev);
  114. struct nsim_dev *nsim_dev = ns->nsim_dev;
  115. if (vf >= nsim_dev_get_vfs(nsim_dev))
  116. return -EINVAL;
  117. nsim_dev->vfconfigs[vf].rss_query_enabled = val;
  118. return 0;
  119. }
  120. static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
  121. {
  122. struct netdevsim *ns = netdev_priv(dev);
  123. struct nsim_dev *nsim_dev = ns->nsim_dev;
  124. if (vf >= nsim_dev_get_vfs(nsim_dev))
  125. return -EINVAL;
  126. nsim_dev->vfconfigs[vf].trusted = val;
  127. return 0;
  128. }
  129. static int
  130. nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
  131. {
  132. struct netdevsim *ns = netdev_priv(dev);
  133. struct nsim_dev *nsim_dev = ns->nsim_dev;
  134. if (vf >= nsim_dev_get_vfs(nsim_dev))
  135. return -EINVAL;
  136. ivi->vf = vf;
  137. ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
  138. ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
  139. ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
  140. ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
  141. ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
  142. ivi->qos = nsim_dev->vfconfigs[vf].qos;
  143. memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
  144. ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
  145. ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
  146. ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
  147. return 0;
  148. }
  149. static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
  150. {
  151. struct netdevsim *ns = netdev_priv(dev);
  152. struct nsim_dev *nsim_dev = ns->nsim_dev;
  153. if (vf >= nsim_dev_get_vfs(nsim_dev))
  154. return -EINVAL;
  155. switch (state) {
  156. case IFLA_VF_LINK_STATE_AUTO:
  157. case IFLA_VF_LINK_STATE_ENABLE:
  158. case IFLA_VF_LINK_STATE_DISABLE:
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. nsim_dev->vfconfigs[vf].link_state = state;
  164. return 0;
  165. }
  166. static LIST_HEAD(nsim_block_cb_list);
  167. static int
  168. nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
  169. {
  170. struct netdevsim *ns = netdev_priv(dev);
  171. switch (type) {
  172. case TC_SETUP_BLOCK:
  173. return flow_block_cb_setup_simple(type_data,
  174. &nsim_block_cb_list,
  175. nsim_setup_tc_block_cb,
  176. ns, ns, true);
  177. default:
  178. return -EOPNOTSUPP;
  179. }
  180. }
  181. static int
  182. nsim_set_features(struct net_device *dev, netdev_features_t features)
  183. {
  184. struct netdevsim *ns = netdev_priv(dev);
  185. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
  186. return nsim_bpf_disable_tc(ns);
  187. return 0;
  188. }
  189. static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
  190. {
  191. struct netdevsim *ns = netdev_priv(dev);
  192. return &ns->nsim_dev_port->devlink_port;
  193. }
  194. static const struct net_device_ops nsim_netdev_ops = {
  195. .ndo_start_xmit = nsim_start_xmit,
  196. .ndo_set_rx_mode = nsim_set_rx_mode,
  197. .ndo_set_mac_address = eth_mac_addr,
  198. .ndo_validate_addr = eth_validate_addr,
  199. .ndo_change_mtu = nsim_change_mtu,
  200. .ndo_get_stats64 = nsim_get_stats64,
  201. .ndo_set_vf_mac = nsim_set_vf_mac,
  202. .ndo_set_vf_vlan = nsim_set_vf_vlan,
  203. .ndo_set_vf_rate = nsim_set_vf_rate,
  204. .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
  205. .ndo_set_vf_trust = nsim_set_vf_trust,
  206. .ndo_get_vf_config = nsim_get_vf_config,
  207. .ndo_set_vf_link_state = nsim_set_vf_link_state,
  208. .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
  209. .ndo_setup_tc = nsim_setup_tc,
  210. .ndo_set_features = nsim_set_features,
  211. .ndo_bpf = nsim_bpf,
  212. .ndo_get_devlink_port = nsim_get_devlink_port,
  213. };
  214. static const struct net_device_ops nsim_vf_netdev_ops = {
  215. .ndo_start_xmit = nsim_start_xmit,
  216. .ndo_set_rx_mode = nsim_set_rx_mode,
  217. .ndo_set_mac_address = eth_mac_addr,
  218. .ndo_validate_addr = eth_validate_addr,
  219. .ndo_change_mtu = nsim_change_mtu,
  220. .ndo_get_stats64 = nsim_get_stats64,
  221. .ndo_setup_tc = nsim_setup_tc,
  222. .ndo_set_features = nsim_set_features,
  223. .ndo_get_devlink_port = nsim_get_devlink_port,
  224. };
  225. static void nsim_setup(struct net_device *dev)
  226. {
  227. ether_setup(dev);
  228. eth_hw_addr_random(dev);
  229. dev->tx_queue_len = 0;
  230. dev->flags |= IFF_NOARP;
  231. dev->flags &= ~IFF_MULTICAST;
  232. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
  233. IFF_NO_QUEUE;
  234. dev->features |= NETIF_F_HIGHDMA |
  235. NETIF_F_SG |
  236. NETIF_F_FRAGLIST |
  237. NETIF_F_HW_CSUM |
  238. NETIF_F_TSO;
  239. dev->hw_features |= NETIF_F_HW_TC;
  240. dev->max_mtu = ETH_MAX_MTU;
  241. }
  242. static int nsim_init_netdevsim(struct netdevsim *ns)
  243. {
  244. int err;
  245. ns->netdev->netdev_ops = &nsim_netdev_ops;
  246. err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
  247. if (err)
  248. return err;
  249. rtnl_lock();
  250. err = nsim_bpf_init(ns);
  251. if (err)
  252. goto err_utn_destroy;
  253. nsim_ipsec_init(ns);
  254. err = register_netdevice(ns->netdev);
  255. if (err)
  256. goto err_ipsec_teardown;
  257. rtnl_unlock();
  258. return 0;
  259. err_ipsec_teardown:
  260. nsim_ipsec_teardown(ns);
  261. nsim_bpf_uninit(ns);
  262. err_utn_destroy:
  263. rtnl_unlock();
  264. nsim_udp_tunnels_info_destroy(ns->netdev);
  265. return err;
  266. }
  267. static int nsim_init_netdevsim_vf(struct netdevsim *ns)
  268. {
  269. int err;
  270. ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
  271. rtnl_lock();
  272. err = register_netdevice(ns->netdev);
  273. rtnl_unlock();
  274. return err;
  275. }
  276. struct netdevsim *
  277. nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
  278. {
  279. struct net_device *dev;
  280. struct netdevsim *ns;
  281. int err;
  282. dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
  283. nsim_dev->nsim_bus_dev->num_queues);
  284. if (!dev)
  285. return ERR_PTR(-ENOMEM);
  286. dev_net_set(dev, nsim_dev_net(nsim_dev));
  287. ns = netdev_priv(dev);
  288. ns->netdev = dev;
  289. u64_stats_init(&ns->syncp);
  290. ns->nsim_dev = nsim_dev;
  291. ns->nsim_dev_port = nsim_dev_port;
  292. ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
  293. SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
  294. nsim_ethtool_init(ns);
  295. if (nsim_dev_port_is_pf(nsim_dev_port))
  296. err = nsim_init_netdevsim(ns);
  297. else
  298. err = nsim_init_netdevsim_vf(ns);
  299. if (err)
  300. goto err_free_netdev;
  301. return ns;
  302. err_free_netdev:
  303. free_netdev(dev);
  304. return ERR_PTR(err);
  305. }
  306. void nsim_destroy(struct netdevsim *ns)
  307. {
  308. struct net_device *dev = ns->netdev;
  309. rtnl_lock();
  310. unregister_netdevice(dev);
  311. if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
  312. nsim_ipsec_teardown(ns);
  313. nsim_bpf_uninit(ns);
  314. }
  315. rtnl_unlock();
  316. if (nsim_dev_port_is_pf(ns->nsim_dev_port))
  317. nsim_udp_tunnels_info_destroy(dev);
  318. free_netdev(dev);
  319. }
  320. static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
  321. struct netlink_ext_ack *extack)
  322. {
  323. NL_SET_ERR_MSG_MOD(extack,
  324. "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
  325. return -EOPNOTSUPP;
  326. }
  327. static struct rtnl_link_ops nsim_link_ops __read_mostly = {
  328. .kind = DRV_NAME,
  329. .validate = nsim_validate,
  330. };
  331. static int __init nsim_module_init(void)
  332. {
  333. int err;
  334. err = nsim_dev_init();
  335. if (err)
  336. return err;
  337. err = nsim_bus_init();
  338. if (err)
  339. goto err_dev_exit;
  340. err = rtnl_link_register(&nsim_link_ops);
  341. if (err)
  342. goto err_bus_exit;
  343. return 0;
  344. err_bus_exit:
  345. nsim_bus_exit();
  346. err_dev_exit:
  347. nsim_dev_exit();
  348. return err;
  349. }
  350. static void __exit nsim_module_exit(void)
  351. {
  352. rtnl_link_unregister(&nsim_link_ops);
  353. nsim_bus_exit();
  354. nsim_dev_exit();
  355. }
  356. module_init(nsim_module_init);
  357. module_exit(nsim_module_exit);
  358. MODULE_LICENSE("GPL");
  359. MODULE_ALIAS_RTNL_LINK(DRV_NAME);