netdev.c 11 KB

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