netdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. struct nsim_vf_config {
  26. int link_state;
  27. u16 min_tx_rate;
  28. u16 max_tx_rate;
  29. u16 vlan;
  30. __be16 vlan_proto;
  31. u16 qos;
  32. u8 vf_mac[ETH_ALEN];
  33. bool spoofchk_enabled;
  34. bool trusted;
  35. bool rss_query_enabled;
  36. };
  37. static u32 nsim_dev_id;
  38. static int nsim_num_vf(struct device *dev)
  39. {
  40. struct netdevsim *ns = to_nsim(dev);
  41. return ns->num_vfs;
  42. }
  43. static struct bus_type nsim_bus = {
  44. .name = DRV_NAME,
  45. .dev_name = DRV_NAME,
  46. .num_vf = nsim_num_vf,
  47. };
  48. static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
  49. {
  50. ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config),
  51. GFP_KERNEL);
  52. if (!ns->vfconfigs)
  53. return -ENOMEM;
  54. ns->num_vfs = num_vfs;
  55. return 0;
  56. }
  57. static void nsim_vfs_disable(struct netdevsim *ns)
  58. {
  59. kfree(ns->vfconfigs);
  60. ns->vfconfigs = NULL;
  61. ns->num_vfs = 0;
  62. }
  63. static ssize_t
  64. nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
  65. const char *buf, size_t count)
  66. {
  67. struct netdevsim *ns = to_nsim(dev);
  68. unsigned int num_vfs;
  69. int ret;
  70. ret = kstrtouint(buf, 0, &num_vfs);
  71. if (ret)
  72. return ret;
  73. rtnl_lock();
  74. if (ns->num_vfs == num_vfs)
  75. goto exit_good;
  76. if (ns->num_vfs && num_vfs) {
  77. ret = -EBUSY;
  78. goto exit_unlock;
  79. }
  80. if (num_vfs) {
  81. ret = nsim_vfs_enable(ns, num_vfs);
  82. if (ret)
  83. goto exit_unlock;
  84. } else {
  85. nsim_vfs_disable(ns);
  86. }
  87. exit_good:
  88. ret = count;
  89. exit_unlock:
  90. rtnl_unlock();
  91. return ret;
  92. }
  93. static ssize_t
  94. nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf)
  95. {
  96. struct netdevsim *ns = to_nsim(dev);
  97. return sprintf(buf, "%u\n", ns->num_vfs);
  98. }
  99. static struct device_attribute nsim_numvfs_attr =
  100. __ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store);
  101. static struct attribute *nsim_dev_attrs[] = {
  102. &nsim_numvfs_attr.attr,
  103. NULL,
  104. };
  105. static const struct attribute_group nsim_dev_attr_group = {
  106. .attrs = nsim_dev_attrs,
  107. };
  108. static const struct attribute_group *nsim_dev_attr_groups[] = {
  109. &nsim_dev_attr_group,
  110. NULL,
  111. };
  112. static void nsim_dev_release(struct device *dev)
  113. {
  114. struct netdevsim *ns = to_nsim(dev);
  115. nsim_vfs_disable(ns);
  116. free_netdev(ns->netdev);
  117. }
  118. static struct device_type nsim_dev_type = {
  119. .groups = nsim_dev_attr_groups,
  120. .release = nsim_dev_release,
  121. };
  122. static int nsim_init(struct net_device *dev)
  123. {
  124. struct netdevsim *ns = netdev_priv(dev);
  125. int err;
  126. ns->netdev = dev;
  127. ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir);
  128. err = nsim_bpf_init(ns);
  129. if (err)
  130. goto err_debugfs_destroy;
  131. ns->dev.id = nsim_dev_id++;
  132. ns->dev.bus = &nsim_bus;
  133. ns->dev.type = &nsim_dev_type;
  134. err = device_register(&ns->dev);
  135. if (err)
  136. goto err_bpf_uninit;
  137. SET_NETDEV_DEV(dev, &ns->dev);
  138. return 0;
  139. err_bpf_uninit:
  140. nsim_bpf_uninit(ns);
  141. err_debugfs_destroy:
  142. debugfs_remove_recursive(ns->ddir);
  143. return err;
  144. }
  145. static void nsim_uninit(struct net_device *dev)
  146. {
  147. struct netdevsim *ns = netdev_priv(dev);
  148. debugfs_remove_recursive(ns->ddir);
  149. nsim_bpf_uninit(ns);
  150. }
  151. static void nsim_free(struct net_device *dev)
  152. {
  153. struct netdevsim *ns = netdev_priv(dev);
  154. device_unregister(&ns->dev);
  155. /* netdev and vf state will be freed out of device_release() */
  156. }
  157. static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
  158. {
  159. struct netdevsim *ns = netdev_priv(dev);
  160. u64_stats_update_begin(&ns->syncp);
  161. ns->tx_packets++;
  162. ns->tx_bytes += skb->len;
  163. u64_stats_update_end(&ns->syncp);
  164. dev_kfree_skb(skb);
  165. return NETDEV_TX_OK;
  166. }
  167. static void nsim_set_rx_mode(struct net_device *dev)
  168. {
  169. }
  170. static int nsim_change_mtu(struct net_device *dev, int new_mtu)
  171. {
  172. struct netdevsim *ns = netdev_priv(dev);
  173. if (ns->xdp_prog_mode == XDP_ATTACHED_DRV &&
  174. new_mtu > NSIM_XDP_MAX_MTU)
  175. return -EBUSY;
  176. dev->mtu = new_mtu;
  177. return 0;
  178. }
  179. static void
  180. nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  181. {
  182. struct netdevsim *ns = netdev_priv(dev);
  183. unsigned int start;
  184. do {
  185. start = u64_stats_fetch_begin(&ns->syncp);
  186. stats->tx_bytes = ns->tx_bytes;
  187. stats->tx_packets = ns->tx_packets;
  188. } while (u64_stats_fetch_retry(&ns->syncp, start));
  189. }
  190. static int
  191. nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  192. {
  193. return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
  194. }
  195. static int
  196. nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
  197. {
  198. struct netdevsim *ns = netdev_priv(dev);
  199. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  200. return -EOPNOTSUPP;
  201. switch (f->command) {
  202. case TC_BLOCK_BIND:
  203. return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb,
  204. ns, ns);
  205. case TC_BLOCK_UNBIND:
  206. tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns);
  207. return 0;
  208. default:
  209. return -EOPNOTSUPP;
  210. }
  211. }
  212. static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
  213. {
  214. struct netdevsim *ns = netdev_priv(dev);
  215. /* Only refuse multicast addresses, zero address can mean unset/any. */
  216. if (vf >= ns->num_vfs || is_multicast_ether_addr(mac))
  217. return -EINVAL;
  218. memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
  219. return 0;
  220. }
  221. static int nsim_set_vf_vlan(struct net_device *dev, int vf,
  222. u16 vlan, u8 qos, __be16 vlan_proto)
  223. {
  224. struct netdevsim *ns = netdev_priv(dev);
  225. if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
  226. return -EINVAL;
  227. ns->vfconfigs[vf].vlan = vlan;
  228. ns->vfconfigs[vf].qos = qos;
  229. ns->vfconfigs[vf].vlan_proto = vlan_proto;
  230. return 0;
  231. }
  232. static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
  233. {
  234. struct netdevsim *ns = netdev_priv(dev);
  235. if (vf >= ns->num_vfs)
  236. return -EINVAL;
  237. ns->vfconfigs[vf].min_tx_rate = min;
  238. ns->vfconfigs[vf].max_tx_rate = max;
  239. return 0;
  240. }
  241. static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
  242. {
  243. struct netdevsim *ns = netdev_priv(dev);
  244. if (vf >= ns->num_vfs)
  245. return -EINVAL;
  246. ns->vfconfigs[vf].spoofchk_enabled = val;
  247. return 0;
  248. }
  249. static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
  250. {
  251. struct netdevsim *ns = netdev_priv(dev);
  252. if (vf >= ns->num_vfs)
  253. return -EINVAL;
  254. ns->vfconfigs[vf].rss_query_enabled = val;
  255. return 0;
  256. }
  257. static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
  258. {
  259. struct netdevsim *ns = netdev_priv(dev);
  260. if (vf >= ns->num_vfs)
  261. return -EINVAL;
  262. ns->vfconfigs[vf].trusted = val;
  263. return 0;
  264. }
  265. static int
  266. nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
  267. {
  268. struct netdevsim *ns = netdev_priv(dev);
  269. if (vf >= ns->num_vfs)
  270. return -EINVAL;
  271. ivi->vf = vf;
  272. ivi->linkstate = ns->vfconfigs[vf].link_state;
  273. ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate;
  274. ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate;
  275. ivi->vlan = ns->vfconfigs[vf].vlan;
  276. ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto;
  277. ivi->qos = ns->vfconfigs[vf].qos;
  278. memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN);
  279. ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled;
  280. ivi->trusted = ns->vfconfigs[vf].trusted;
  281. ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled;
  282. return 0;
  283. }
  284. static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
  285. {
  286. struct netdevsim *ns = netdev_priv(dev);
  287. if (vf >= ns->num_vfs)
  288. return -EINVAL;
  289. switch (state) {
  290. case IFLA_VF_LINK_STATE_AUTO:
  291. case IFLA_VF_LINK_STATE_ENABLE:
  292. case IFLA_VF_LINK_STATE_DISABLE:
  293. break;
  294. default:
  295. return -EINVAL;
  296. }
  297. ns->vfconfigs[vf].link_state = state;
  298. return 0;
  299. }
  300. static int
  301. nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
  302. {
  303. switch (type) {
  304. case TC_SETUP_BLOCK:
  305. return nsim_setup_tc_block(dev, type_data);
  306. default:
  307. return -EOPNOTSUPP;
  308. }
  309. }
  310. static int
  311. nsim_set_features(struct net_device *dev, netdev_features_t features)
  312. {
  313. struct netdevsim *ns = netdev_priv(dev);
  314. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
  315. return nsim_bpf_disable_tc(ns);
  316. return 0;
  317. }
  318. static const struct net_device_ops nsim_netdev_ops = {
  319. .ndo_init = nsim_init,
  320. .ndo_uninit = nsim_uninit,
  321. .ndo_start_xmit = nsim_start_xmit,
  322. .ndo_set_rx_mode = nsim_set_rx_mode,
  323. .ndo_set_mac_address = eth_mac_addr,
  324. .ndo_validate_addr = eth_validate_addr,
  325. .ndo_change_mtu = nsim_change_mtu,
  326. .ndo_get_stats64 = nsim_get_stats64,
  327. .ndo_set_vf_mac = nsim_set_vf_mac,
  328. .ndo_set_vf_vlan = nsim_set_vf_vlan,
  329. .ndo_set_vf_rate = nsim_set_vf_rate,
  330. .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
  331. .ndo_set_vf_trust = nsim_set_vf_trust,
  332. .ndo_get_vf_config = nsim_get_vf_config,
  333. .ndo_set_vf_link_state = nsim_set_vf_link_state,
  334. .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
  335. .ndo_setup_tc = nsim_setup_tc,
  336. .ndo_set_features = nsim_set_features,
  337. .ndo_bpf = nsim_bpf,
  338. };
  339. static void nsim_setup(struct net_device *dev)
  340. {
  341. ether_setup(dev);
  342. eth_hw_addr_random(dev);
  343. dev->netdev_ops = &nsim_netdev_ops;
  344. dev->priv_destructor = nsim_free;
  345. dev->tx_queue_len = 0;
  346. dev->flags |= IFF_NOARP;
  347. dev->flags &= ~IFF_MULTICAST;
  348. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
  349. IFF_NO_QUEUE;
  350. dev->features |= NETIF_F_HIGHDMA |
  351. NETIF_F_SG |
  352. NETIF_F_FRAGLIST |
  353. NETIF_F_HW_CSUM |
  354. NETIF_F_TSO;
  355. dev->hw_features |= NETIF_F_HW_TC;
  356. dev->max_mtu = ETH_MAX_MTU;
  357. }
  358. static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
  359. struct netlink_ext_ack *extack)
  360. {
  361. if (tb[IFLA_ADDRESS]) {
  362. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  363. return -EINVAL;
  364. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  365. return -EADDRNOTAVAIL;
  366. }
  367. return 0;
  368. }
  369. static struct rtnl_link_ops nsim_link_ops __read_mostly = {
  370. .kind = DRV_NAME,
  371. .priv_size = sizeof(struct netdevsim),
  372. .setup = nsim_setup,
  373. .validate = nsim_validate,
  374. };
  375. struct dentry *nsim_ddir;
  376. static int __init nsim_module_init(void)
  377. {
  378. int err;
  379. nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
  380. if (IS_ERR(nsim_ddir))
  381. return PTR_ERR(nsim_ddir);
  382. err = bus_register(&nsim_bus);
  383. if (err)
  384. goto err_debugfs_destroy;
  385. err = rtnl_link_register(&nsim_link_ops);
  386. if (err)
  387. goto err_unreg_bus;
  388. return 0;
  389. err_unreg_bus:
  390. bus_unregister(&nsim_bus);
  391. err_debugfs_destroy:
  392. debugfs_remove_recursive(nsim_ddir);
  393. return err;
  394. }
  395. static void __exit nsim_module_exit(void)
  396. {
  397. rtnl_link_unregister(&nsim_link_ops);
  398. bus_unregister(&nsim_bus);
  399. debugfs_remove_recursive(nsim_ddir);
  400. }
  401. module_init(nsim_module_init);
  402. module_exit(nsim_module_exit);
  403. MODULE_LICENSE("GPL");
  404. MODULE_ALIAS_RTNL_LINK(DRV_NAME);