netdev.c 14 KB

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