switchdev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * net/switchdev/switchdev.c - Switch device API
  3. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/mutex.h>
  15. #include <linux/notifier.h>
  16. #include <linux/netdevice.h>
  17. #include <net/ip_fib.h>
  18. #include <net/switchdev.h>
  19. /**
  20. * switchdev_port_attr_get - Get port attribute
  21. *
  22. * @dev: port device
  23. * @attr: attribute to get
  24. */
  25. int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
  26. {
  27. const struct switchdev_ops *ops = dev->switchdev_ops;
  28. struct net_device *lower_dev;
  29. struct list_head *iter;
  30. struct switchdev_attr first = {
  31. .id = SWITCHDEV_ATTR_UNDEFINED
  32. };
  33. int err = -EOPNOTSUPP;
  34. if (ops && ops->switchdev_port_attr_get)
  35. return ops->switchdev_port_attr_get(dev, attr);
  36. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  37. return err;
  38. /* Switch device port(s) may be stacked under
  39. * bond/team/vlan dev, so recurse down to get attr on
  40. * each port. Return -ENODATA if attr values don't
  41. * compare across ports.
  42. */
  43. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  44. err = switchdev_port_attr_get(lower_dev, attr);
  45. if (err)
  46. break;
  47. if (first.id == SWITCHDEV_ATTR_UNDEFINED)
  48. first = *attr;
  49. else if (memcmp(&first, attr, sizeof(*attr)))
  50. return -ENODATA;
  51. }
  52. return err;
  53. }
  54. EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
  55. static int __switchdev_port_attr_set(struct net_device *dev,
  56. struct switchdev_attr *attr)
  57. {
  58. const struct switchdev_ops *ops = dev->switchdev_ops;
  59. struct net_device *lower_dev;
  60. struct list_head *iter;
  61. int err = -EOPNOTSUPP;
  62. if (ops && ops->switchdev_port_attr_set)
  63. return ops->switchdev_port_attr_set(dev, attr);
  64. if (attr->flags & SWITCHDEV_F_NO_RECURSE)
  65. return err;
  66. /* Switch device port(s) may be stacked under
  67. * bond/team/vlan dev, so recurse down to set attr on
  68. * each port.
  69. */
  70. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  71. err = __switchdev_port_attr_set(lower_dev, attr);
  72. if (err)
  73. break;
  74. }
  75. return err;
  76. }
  77. struct switchdev_attr_set_work {
  78. struct work_struct work;
  79. struct net_device *dev;
  80. struct switchdev_attr attr;
  81. };
  82. static void switchdev_port_attr_set_work(struct work_struct *work)
  83. {
  84. struct switchdev_attr_set_work *asw =
  85. container_of(work, struct switchdev_attr_set_work, work);
  86. int err;
  87. rtnl_lock();
  88. err = switchdev_port_attr_set(asw->dev, &asw->attr);
  89. BUG_ON(err);
  90. rtnl_unlock();
  91. dev_put(asw->dev);
  92. kfree(work);
  93. }
  94. static int switchdev_port_attr_set_defer(struct net_device *dev,
  95. struct switchdev_attr *attr)
  96. {
  97. struct switchdev_attr_set_work *asw;
  98. asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
  99. if (!asw)
  100. return -ENOMEM;
  101. INIT_WORK(&asw->work, switchdev_port_attr_set_work);
  102. dev_hold(dev);
  103. asw->dev = dev;
  104. memcpy(&asw->attr, attr, sizeof(asw->attr));
  105. schedule_work(&asw->work);
  106. return 0;
  107. }
  108. /**
  109. * switchdev_port_attr_set - Set port attribute
  110. *
  111. * @dev: port device
  112. * @attr: attribute to set
  113. *
  114. * Use a 2-phase prepare-commit transaction model to ensure
  115. * system is not left in a partially updated state due to
  116. * failure from driver/device.
  117. */
  118. int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
  119. {
  120. int err;
  121. if (!rtnl_is_locked()) {
  122. /* Running prepare-commit transaction across stacked
  123. * devices requires nothing moves, so if rtnl_lock is
  124. * not held, schedule a worker thread to hold rtnl_lock
  125. * while setting attr.
  126. */
  127. return switchdev_port_attr_set_defer(dev, attr);
  128. }
  129. /* Phase I: prepare for attr set. Driver/device should fail
  130. * here if there are going to be issues in the commit phase,
  131. * such as lack of resources or support. The driver/device
  132. * should reserve resources needed for the commit phase here,
  133. * but should not commit the attr.
  134. */
  135. attr->trans = SWITCHDEV_TRANS_PREPARE;
  136. err = __switchdev_port_attr_set(dev, attr);
  137. if (err) {
  138. /* Prepare phase failed: abort the transaction. Any
  139. * resources reserved in the prepare phase are
  140. * released.
  141. */
  142. attr->trans = SWITCHDEV_TRANS_ABORT;
  143. __switchdev_port_attr_set(dev, attr);
  144. return err;
  145. }
  146. /* Phase II: commit attr set. This cannot fail as a fault
  147. * of driver/device. If it does, it's a bug in the driver/device
  148. * because the driver said everythings was OK in phase I.
  149. */
  150. attr->trans = SWITCHDEV_TRANS_COMMIT;
  151. err = __switchdev_port_attr_set(dev, attr);
  152. BUG_ON(err);
  153. return err;
  154. }
  155. EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
  156. int __switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  157. {
  158. const struct switchdev_ops *ops = dev->switchdev_ops;
  159. struct net_device *lower_dev;
  160. struct list_head *iter;
  161. int err = -EOPNOTSUPP;
  162. if (ops && ops->switchdev_port_obj_add)
  163. return ops->switchdev_port_obj_add(dev, obj);
  164. /* Switch device port(s) may be stacked under
  165. * bond/team/vlan dev, so recurse down to add object on
  166. * each port.
  167. */
  168. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  169. err = __switchdev_port_obj_add(lower_dev, obj);
  170. if (err)
  171. break;
  172. }
  173. return err;
  174. }
  175. /**
  176. * switchdev_port_obj_add - Add port object
  177. *
  178. * @dev: port device
  179. * @obj: object to add
  180. *
  181. * Use a 2-phase prepare-commit transaction model to ensure
  182. * system is not left in a partially updated state due to
  183. * failure from driver/device.
  184. *
  185. * rtnl_lock must be held.
  186. */
  187. int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
  188. {
  189. int err;
  190. ASSERT_RTNL();
  191. /* Phase I: prepare for obj add. Driver/device should fail
  192. * here if there are going to be issues in the commit phase,
  193. * such as lack of resources or support. The driver/device
  194. * should reserve resources needed for the commit phase here,
  195. * but should not commit the obj.
  196. */
  197. obj->trans = SWITCHDEV_TRANS_PREPARE;
  198. err = __switchdev_port_obj_add(dev, obj);
  199. if (err) {
  200. /* Prepare phase failed: abort the transaction. Any
  201. * resources reserved in the prepare phase are
  202. * released.
  203. */
  204. obj->trans = SWITCHDEV_TRANS_ABORT;
  205. __switchdev_port_obj_add(dev, obj);
  206. return err;
  207. }
  208. /* Phase II: commit obj add. This cannot fail as a fault
  209. * of driver/device. If it does, it's a bug in the driver/device
  210. * because the driver said everythings was OK in phase I.
  211. */
  212. obj->trans = SWITCHDEV_TRANS_COMMIT;
  213. err = __switchdev_port_obj_add(dev, obj);
  214. WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
  215. return err;
  216. }
  217. EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
  218. /**
  219. * switchdev_port_obj_del - Delete port object
  220. *
  221. * @dev: port device
  222. * @obj: object to delete
  223. */
  224. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
  225. {
  226. const struct switchdev_ops *ops = dev->switchdev_ops;
  227. struct net_device *lower_dev;
  228. struct list_head *iter;
  229. int err = -EOPNOTSUPP;
  230. if (ops && ops->switchdev_port_obj_del)
  231. return ops->switchdev_port_obj_del(dev, obj);
  232. /* Switch device port(s) may be stacked under
  233. * bond/team/vlan dev, so recurse down to delete object on
  234. * each port.
  235. */
  236. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  237. err = switchdev_port_obj_del(lower_dev, obj);
  238. if (err)
  239. break;
  240. }
  241. return err;
  242. }
  243. EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
  244. static DEFINE_MUTEX(switchdev_mutex);
  245. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  246. /**
  247. * register_switchdev_notifier - Register notifier
  248. * @nb: notifier_block
  249. *
  250. * Register switch device notifier. This should be used by code
  251. * which needs to monitor events happening in particular device.
  252. * Return values are same as for atomic_notifier_chain_register().
  253. */
  254. int register_switchdev_notifier(struct notifier_block *nb)
  255. {
  256. int err;
  257. mutex_lock(&switchdev_mutex);
  258. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  259. mutex_unlock(&switchdev_mutex);
  260. return err;
  261. }
  262. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  263. /**
  264. * unregister_switchdev_notifier - Unregister notifier
  265. * @nb: notifier_block
  266. *
  267. * Unregister switch device notifier.
  268. * Return values are same as for atomic_notifier_chain_unregister().
  269. */
  270. int unregister_switchdev_notifier(struct notifier_block *nb)
  271. {
  272. int err;
  273. mutex_lock(&switchdev_mutex);
  274. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  275. mutex_unlock(&switchdev_mutex);
  276. return err;
  277. }
  278. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  279. /**
  280. * call_switchdev_notifiers - Call notifiers
  281. * @val: value passed unmodified to notifier function
  282. * @dev: port device
  283. * @info: notifier information data
  284. *
  285. * Call all network notifier blocks. This should be called by driver
  286. * when it needs to propagate hardware event.
  287. * Return values are same as for atomic_notifier_call_chain().
  288. */
  289. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  290. struct switchdev_notifier_info *info)
  291. {
  292. int err;
  293. info->dev = dev;
  294. mutex_lock(&switchdev_mutex);
  295. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  296. mutex_unlock(&switchdev_mutex);
  297. return err;
  298. }
  299. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  300. /**
  301. * switchdev_port_bridge_setlink - Notify switch device port of bridge
  302. * port attributes
  303. *
  304. * @dev: port device
  305. * @nlh: netlink msg with bridge port attributes
  306. * @flags: bridge setlink flags
  307. *
  308. * Notify switch device port of bridge port attributes
  309. */
  310. int switchdev_port_bridge_setlink(struct net_device *dev,
  311. struct nlmsghdr *nlh, u16 flags)
  312. {
  313. const struct net_device_ops *ops = dev->netdev_ops;
  314. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  315. return 0;
  316. if (!ops->ndo_bridge_setlink)
  317. return -EOPNOTSUPP;
  318. return ops->ndo_bridge_setlink(dev, nlh, flags);
  319. }
  320. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  321. /**
  322. * switchdev_port_bridge_dellink - Notify switch device port of bridge
  323. * port attribute delete
  324. *
  325. * @dev: port device
  326. * @nlh: netlink msg with bridge port attributes
  327. * @flags: bridge setlink flags
  328. *
  329. * Notify switch device port of bridge port attribute delete
  330. */
  331. int switchdev_port_bridge_dellink(struct net_device *dev,
  332. struct nlmsghdr *nlh, u16 flags)
  333. {
  334. const struct net_device_ops *ops = dev->netdev_ops;
  335. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  336. return 0;
  337. if (!ops->ndo_bridge_dellink)
  338. return -EOPNOTSUPP;
  339. return ops->ndo_bridge_dellink(dev, nlh, flags);
  340. }
  341. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  342. /**
  343. * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
  344. * op for master devices
  345. *
  346. * @dev: port device
  347. * @nlh: netlink msg with bridge port attributes
  348. * @flags: bridge setlink flags
  349. *
  350. * Notify master device slaves of bridge port attributes
  351. */
  352. int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
  353. struct nlmsghdr *nlh, u16 flags)
  354. {
  355. struct net_device *lower_dev;
  356. struct list_head *iter;
  357. int ret = 0, err = 0;
  358. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  359. return ret;
  360. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  361. err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
  362. if (err && err != -EOPNOTSUPP)
  363. ret = err;
  364. }
  365. return ret;
  366. }
  367. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
  368. /**
  369. * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
  370. * op for master devices
  371. *
  372. * @dev: port device
  373. * @nlh: netlink msg with bridge port attributes
  374. * @flags: bridge dellink flags
  375. *
  376. * Notify master device slaves of bridge port attribute deletes
  377. */
  378. int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
  379. struct nlmsghdr *nlh, u16 flags)
  380. {
  381. struct net_device *lower_dev;
  382. struct list_head *iter;
  383. int ret = 0, err = 0;
  384. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  385. return ret;
  386. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  387. err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
  388. if (err && err != -EOPNOTSUPP)
  389. ret = err;
  390. }
  391. return ret;
  392. }
  393. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
  394. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  395. {
  396. const struct switchdev_ops *ops = dev->switchdev_ops;
  397. struct net_device *lower_dev;
  398. struct net_device *port_dev;
  399. struct list_head *iter;
  400. /* Recusively search down until we find a sw port dev.
  401. * (A sw port dev supports switchdev_port_attr_get).
  402. */
  403. if (ops && ops->switchdev_port_attr_get)
  404. return dev;
  405. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  406. port_dev = switchdev_get_lowest_dev(lower_dev);
  407. if (port_dev)
  408. return port_dev;
  409. }
  410. return NULL;
  411. }
  412. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  413. {
  414. struct switchdev_attr attr = {
  415. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  416. };
  417. struct switchdev_attr prev_attr;
  418. struct net_device *dev = NULL;
  419. int nhsel;
  420. /* For this route, all nexthop devs must be on the same switch. */
  421. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  422. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  423. if (!nh->nh_dev)
  424. return NULL;
  425. dev = switchdev_get_lowest_dev(nh->nh_dev);
  426. if (!dev)
  427. return NULL;
  428. if (switchdev_port_attr_get(dev, &attr))
  429. return NULL;
  430. if (nhsel > 0) {
  431. if (prev_attr.ppid.id_len != attr.ppid.id_len)
  432. return NULL;
  433. if (memcmp(prev_attr.ppid.id, attr.ppid.id,
  434. attr.ppid.id_len))
  435. return NULL;
  436. }
  437. prev_attr = attr;
  438. }
  439. return dev;
  440. }
  441. /**
  442. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  443. *
  444. * @dst: route's IPv4 destination address
  445. * @dst_len: destination address length (prefix length)
  446. * @fi: route FIB info structure
  447. * @tos: route TOS
  448. * @type: route type
  449. * @nlflags: netlink flags passed in (NLM_F_*)
  450. * @tb_id: route table ID
  451. *
  452. * Add IPv4 route entry to switch device.
  453. */
  454. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  455. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  456. {
  457. struct net_device *dev;
  458. const struct switchdev_ops *ops;
  459. int err = 0;
  460. /* Don't offload route if using custom ip rules or if
  461. * IPv4 FIB offloading has been disabled completely.
  462. */
  463. #ifdef CONFIG_IP_MULTIPLE_TABLES
  464. if (fi->fib_net->ipv4.fib_has_custom_rules)
  465. return 0;
  466. #endif
  467. if (fi->fib_net->ipv4.fib_offload_disabled)
  468. return 0;
  469. dev = switchdev_get_dev_by_nhs(fi);
  470. if (!dev)
  471. return 0;
  472. ops = dev->switchdev_ops;
  473. if (ops->switchdev_fib_ipv4_add) {
  474. err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
  475. fi, tos, type, nlflags,
  476. tb_id);
  477. if (!err)
  478. fi->fib_flags |= RTNH_F_EXTERNAL;
  479. }
  480. return err;
  481. }
  482. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  483. /**
  484. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  485. *
  486. * @dst: route's IPv4 destination address
  487. * @dst_len: destination address length (prefix length)
  488. * @fi: route FIB info structure
  489. * @tos: route TOS
  490. * @type: route type
  491. * @tb_id: route table ID
  492. *
  493. * Delete IPv4 route entry from switch device.
  494. */
  495. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  496. u8 tos, u8 type, u32 tb_id)
  497. {
  498. struct net_device *dev;
  499. const struct switchdev_ops *ops;
  500. int err = 0;
  501. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  502. return 0;
  503. dev = switchdev_get_dev_by_nhs(fi);
  504. if (!dev)
  505. return 0;
  506. ops = dev->switchdev_ops;
  507. if (ops->switchdev_fib_ipv4_del) {
  508. err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
  509. fi, tos, type, tb_id);
  510. if (!err)
  511. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  512. }
  513. return err;
  514. }
  515. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  516. /**
  517. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  518. *
  519. * @fi: route FIB info structure
  520. */
  521. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  522. {
  523. /* There was a problem installing this route to the offload
  524. * device. For now, until we come up with more refined
  525. * policy handling, abruptly end IPv4 fib offloading for
  526. * for entire net by flushing offload device(s) of all
  527. * IPv4 routes, and mark IPv4 fib offloading broken from
  528. * this point forward.
  529. */
  530. fib_flush_external(fi->fib_net);
  531. fi->fib_net->ipv4.fib_offload_disabled = true;
  532. }
  533. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);