netdev.c 11 KB

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