switchdev.c 14 KB

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