netdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. if (IS_ERR_OR_NULL(ns->ddir))
  129. return -ENOMEM;
  130. err = nsim_bpf_init(ns);
  131. if (err)
  132. goto err_debugfs_destroy;
  133. ns->dev.id = nsim_dev_id++;
  134. ns->dev.bus = &nsim_bus;
  135. ns->dev.type = &nsim_dev_type;
  136. err = device_register(&ns->dev);
  137. if (err)
  138. goto err_bpf_uninit;
  139. SET_NETDEV_DEV(dev, &ns->dev);
  140. err = nsim_devlink_setup(ns);
  141. if (err)
  142. goto err_unreg_dev;
  143. return 0;
  144. err_unreg_dev:
  145. device_unregister(&ns->dev);
  146. err_bpf_uninit:
  147. nsim_bpf_uninit(ns);
  148. err_debugfs_destroy:
  149. debugfs_remove_recursive(ns->ddir);
  150. return err;
  151. }
  152. static void nsim_uninit(struct net_device *dev)
  153. {
  154. struct netdevsim *ns = netdev_priv(dev);
  155. nsim_devlink_teardown(ns);
  156. debugfs_remove_recursive(ns->ddir);
  157. nsim_bpf_uninit(ns);
  158. }
  159. static void nsim_free(struct net_device *dev)
  160. {
  161. struct netdevsim *ns = netdev_priv(dev);
  162. device_unregister(&ns->dev);
  163. /* netdev and vf state will be freed out of device_release() */
  164. }
  165. static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
  166. {
  167. struct netdevsim *ns = netdev_priv(dev);
  168. u64_stats_update_begin(&ns->syncp);
  169. ns->tx_packets++;
  170. ns->tx_bytes += skb->len;
  171. u64_stats_update_end(&ns->syncp);
  172. dev_kfree_skb(skb);
  173. return NETDEV_TX_OK;
  174. }
  175. static void nsim_set_rx_mode(struct net_device *dev)
  176. {
  177. }
  178. static int nsim_change_mtu(struct net_device *dev, int new_mtu)
  179. {
  180. struct netdevsim *ns = netdev_priv(dev);
  181. if (ns->xdp_prog_mode == XDP_ATTACHED_DRV &&
  182. new_mtu > NSIM_XDP_MAX_MTU)
  183. return -EBUSY;
  184. dev->mtu = new_mtu;
  185. return 0;
  186. }
  187. static void
  188. nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  189. {
  190. struct netdevsim *ns = netdev_priv(dev);
  191. unsigned int start;
  192. do {
  193. start = u64_stats_fetch_begin(&ns->syncp);
  194. stats->tx_bytes = ns->tx_bytes;
  195. stats->tx_packets = ns->tx_packets;
  196. } while (u64_stats_fetch_retry(&ns->syncp, start));
  197. }
  198. static int
  199. nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  200. {
  201. return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
  202. }
  203. static int
  204. nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
  205. {
  206. struct netdevsim *ns = netdev_priv(dev);
  207. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  208. return -EOPNOTSUPP;
  209. switch (f->command) {
  210. case TC_BLOCK_BIND:
  211. return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb,
  212. ns, ns);
  213. case TC_BLOCK_UNBIND:
  214. tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns);
  215. return 0;
  216. default:
  217. return -EOPNOTSUPP;
  218. }
  219. }
  220. static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
  221. {
  222. struct netdevsim *ns = netdev_priv(dev);
  223. /* Only refuse multicast addresses, zero address can mean unset/any. */
  224. if (vf >= ns->num_vfs || is_multicast_ether_addr(mac))
  225. return -EINVAL;
  226. memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
  227. return 0;
  228. }
  229. static int nsim_set_vf_vlan(struct net_device *dev, int vf,
  230. u16 vlan, u8 qos, __be16 vlan_proto)
  231. {
  232. struct netdevsim *ns = netdev_priv(dev);
  233. if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
  234. return -EINVAL;
  235. ns->vfconfigs[vf].vlan = vlan;
  236. ns->vfconfigs[vf].qos = qos;
  237. ns->vfconfigs[vf].vlan_proto = vlan_proto;
  238. return 0;
  239. }
  240. static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
  241. {
  242. struct netdevsim *ns = netdev_priv(dev);
  243. if (vf >= ns->num_vfs)
  244. return -EINVAL;
  245. ns->vfconfigs[vf].min_tx_rate = min;
  246. ns->vfconfigs[vf].max_tx_rate = max;
  247. return 0;
  248. }
  249. static int nsim_set_vf_spoofchk(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].spoofchk_enabled = val;
  255. return 0;
  256. }
  257. static int nsim_set_vf_rss_query_en(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].rss_query_enabled = val;
  263. return 0;
  264. }
  265. static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
  266. {
  267. struct netdevsim *ns = netdev_priv(dev);
  268. if (vf >= ns->num_vfs)
  269. return -EINVAL;
  270. ns->vfconfigs[vf].trusted = val;
  271. return 0;
  272. }
  273. static int
  274. nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
  275. {
  276. struct netdevsim *ns = netdev_priv(dev);
  277. if (vf >= ns->num_vfs)
  278. return -EINVAL;
  279. ivi->vf = vf;
  280. ivi->linkstate = ns->vfconfigs[vf].link_state;
  281. ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate;
  282. ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate;
  283. ivi->vlan = ns->vfconfigs[vf].vlan;
  284. ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto;
  285. ivi->qos = ns->vfconfigs[vf].qos;
  286. memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN);
  287. ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled;
  288. ivi->trusted = ns->vfconfigs[vf].trusted;
  289. ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled;
  290. return 0;
  291. }
  292. static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
  293. {
  294. struct netdevsim *ns = netdev_priv(dev);
  295. if (vf >= ns->num_vfs)
  296. return -EINVAL;
  297. switch (state) {
  298. case IFLA_VF_LINK_STATE_AUTO:
  299. case IFLA_VF_LINK_STATE_ENABLE:
  300. case IFLA_VF_LINK_STATE_DISABLE:
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. ns->vfconfigs[vf].link_state = state;
  306. return 0;
  307. }
  308. static int
  309. nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
  310. {
  311. switch (type) {
  312. case TC_SETUP_BLOCK:
  313. return nsim_setup_tc_block(dev, type_data);
  314. default:
  315. return -EOPNOTSUPP;
  316. }
  317. }
  318. static int
  319. nsim_set_features(struct net_device *dev, netdev_features_t features)
  320. {
  321. struct netdevsim *ns = netdev_priv(dev);
  322. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
  323. return nsim_bpf_disable_tc(ns);
  324. return 0;
  325. }
  326. static const struct net_device_ops nsim_netdev_ops = {
  327. .ndo_init = nsim_init,
  328. .ndo_uninit = nsim_uninit,
  329. .ndo_start_xmit = nsim_start_xmit,
  330. .ndo_set_rx_mode = nsim_set_rx_mode,
  331. .ndo_set_mac_address = eth_mac_addr,
  332. .ndo_validate_addr = eth_validate_addr,
  333. .ndo_change_mtu = nsim_change_mtu,
  334. .ndo_get_stats64 = nsim_get_stats64,
  335. .ndo_set_vf_mac = nsim_set_vf_mac,
  336. .ndo_set_vf_vlan = nsim_set_vf_vlan,
  337. .ndo_set_vf_rate = nsim_set_vf_rate,
  338. .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
  339. .ndo_set_vf_trust = nsim_set_vf_trust,
  340. .ndo_get_vf_config = nsim_get_vf_config,
  341. .ndo_set_vf_link_state = nsim_set_vf_link_state,
  342. .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
  343. .ndo_setup_tc = nsim_setup_tc,
  344. .ndo_set_features = nsim_set_features,
  345. .ndo_bpf = nsim_bpf,
  346. };
  347. static void nsim_setup(struct net_device *dev)
  348. {
  349. ether_setup(dev);
  350. eth_hw_addr_random(dev);
  351. dev->netdev_ops = &nsim_netdev_ops;
  352. dev->priv_destructor = nsim_free;
  353. dev->tx_queue_len = 0;
  354. dev->flags |= IFF_NOARP;
  355. dev->flags &= ~IFF_MULTICAST;
  356. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
  357. IFF_NO_QUEUE;
  358. dev->features |= NETIF_F_HIGHDMA |
  359. NETIF_F_SG |
  360. NETIF_F_FRAGLIST |
  361. NETIF_F_HW_CSUM |
  362. NETIF_F_TSO;
  363. dev->hw_features |= NETIF_F_HW_TC;
  364. dev->max_mtu = ETH_MAX_MTU;
  365. }
  366. static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
  367. struct netlink_ext_ack *extack)
  368. {
  369. if (tb[IFLA_ADDRESS]) {
  370. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  371. return -EINVAL;
  372. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  373. return -EADDRNOTAVAIL;
  374. }
  375. return 0;
  376. }
  377. static struct rtnl_link_ops nsim_link_ops __read_mostly = {
  378. .kind = DRV_NAME,
  379. .priv_size = sizeof(struct netdevsim),
  380. .setup = nsim_setup,
  381. .validate = nsim_validate,
  382. };
  383. struct dentry *nsim_ddir;
  384. static int __init nsim_module_init(void)
  385. {
  386. int err;
  387. nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
  388. if (IS_ERR_OR_NULL(nsim_ddir))
  389. return -ENOMEM;
  390. err = bus_register(&nsim_bus);
  391. if (err)
  392. goto err_debugfs_destroy;
  393. err = nsim_devlink_init();
  394. if (err)
  395. goto err_unreg_bus;
  396. err = rtnl_link_register(&nsim_link_ops);
  397. if (err)
  398. goto err_dl_fini;
  399. return 0;
  400. err_dl_fini:
  401. nsim_devlink_exit();
  402. err_unreg_bus:
  403. bus_unregister(&nsim_bus);
  404. err_debugfs_destroy:
  405. debugfs_remove_recursive(nsim_ddir);
  406. return err;
  407. }
  408. static void __exit nsim_module_exit(void)
  409. {
  410. rtnl_link_unregister(&nsim_link_ops);
  411. nsim_devlink_exit();
  412. bus_unregister(&nsim_bus);
  413. debugfs_remove_recursive(nsim_ddir);
  414. }
  415. module_init(nsim_module_init);
  416. module_exit(nsim_module_exit);
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS_RTNL_LINK(DRV_NAME);