switchdev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. /**
  157. * switchdev_port_stp_update - Notify switch device port of STP
  158. * state change
  159. * @dev: port device
  160. * @state: port STP state
  161. *
  162. * Notify switch device port of bridge port STP state change.
  163. */
  164. int switchdev_port_stp_update(struct net_device *dev, u8 state)
  165. {
  166. const struct switchdev_ops *ops = dev->switchdev_ops;
  167. struct net_device *lower_dev;
  168. struct list_head *iter;
  169. int err = -EOPNOTSUPP;
  170. if (ops && ops->switchdev_port_stp_update)
  171. return ops->switchdev_port_stp_update(dev, state);
  172. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  173. err = switchdev_port_stp_update(lower_dev, state);
  174. if (err && err != -EOPNOTSUPP)
  175. return err;
  176. }
  177. return err;
  178. }
  179. EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
  180. static DEFINE_MUTEX(switchdev_mutex);
  181. static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
  182. /**
  183. * register_switchdev_notifier - Register notifier
  184. * @nb: notifier_block
  185. *
  186. * Register switch device notifier. This should be used by code
  187. * which needs to monitor events happening in particular device.
  188. * Return values are same as for atomic_notifier_chain_register().
  189. */
  190. int register_switchdev_notifier(struct notifier_block *nb)
  191. {
  192. int err;
  193. mutex_lock(&switchdev_mutex);
  194. err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
  195. mutex_unlock(&switchdev_mutex);
  196. return err;
  197. }
  198. EXPORT_SYMBOL_GPL(register_switchdev_notifier);
  199. /**
  200. * unregister_switchdev_notifier - Unregister notifier
  201. * @nb: notifier_block
  202. *
  203. * Unregister switch device notifier.
  204. * Return values are same as for atomic_notifier_chain_unregister().
  205. */
  206. int unregister_switchdev_notifier(struct notifier_block *nb)
  207. {
  208. int err;
  209. mutex_lock(&switchdev_mutex);
  210. err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
  211. mutex_unlock(&switchdev_mutex);
  212. return err;
  213. }
  214. EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
  215. /**
  216. * call_switchdev_notifiers - Call notifiers
  217. * @val: value passed unmodified to notifier function
  218. * @dev: port device
  219. * @info: notifier information data
  220. *
  221. * Call all network notifier blocks. This should be called by driver
  222. * when it needs to propagate hardware event.
  223. * Return values are same as for atomic_notifier_call_chain().
  224. */
  225. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  226. struct switchdev_notifier_info *info)
  227. {
  228. int err;
  229. info->dev = dev;
  230. mutex_lock(&switchdev_mutex);
  231. err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
  232. mutex_unlock(&switchdev_mutex);
  233. return err;
  234. }
  235. EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
  236. /**
  237. * switchdev_port_bridge_setlink - Notify switch device port of bridge
  238. * port attributes
  239. *
  240. * @dev: port device
  241. * @nlh: netlink msg with bridge port attributes
  242. * @flags: bridge setlink flags
  243. *
  244. * Notify switch device port of bridge port attributes
  245. */
  246. int switchdev_port_bridge_setlink(struct net_device *dev,
  247. struct nlmsghdr *nlh, u16 flags)
  248. {
  249. const struct net_device_ops *ops = dev->netdev_ops;
  250. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  251. return 0;
  252. if (!ops->ndo_bridge_setlink)
  253. return -EOPNOTSUPP;
  254. return ops->ndo_bridge_setlink(dev, nlh, flags);
  255. }
  256. EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
  257. /**
  258. * switchdev_port_bridge_dellink - Notify switch device port of bridge
  259. * port attribute delete
  260. *
  261. * @dev: port device
  262. * @nlh: netlink msg with bridge port attributes
  263. * @flags: bridge setlink flags
  264. *
  265. * Notify switch device port of bridge port attribute delete
  266. */
  267. int switchdev_port_bridge_dellink(struct net_device *dev,
  268. struct nlmsghdr *nlh, u16 flags)
  269. {
  270. const struct net_device_ops *ops = dev->netdev_ops;
  271. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  272. return 0;
  273. if (!ops->ndo_bridge_dellink)
  274. return -EOPNOTSUPP;
  275. return ops->ndo_bridge_dellink(dev, nlh, flags);
  276. }
  277. EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
  278. /**
  279. * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
  280. * op for master devices
  281. *
  282. * @dev: port device
  283. * @nlh: netlink msg with bridge port attributes
  284. * @flags: bridge setlink flags
  285. *
  286. * Notify master device slaves of bridge port attributes
  287. */
  288. int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
  289. struct nlmsghdr *nlh, u16 flags)
  290. {
  291. struct net_device *lower_dev;
  292. struct list_head *iter;
  293. int ret = 0, err = 0;
  294. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  295. return ret;
  296. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  297. err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
  298. if (err && err != -EOPNOTSUPP)
  299. ret = err;
  300. }
  301. return ret;
  302. }
  303. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
  304. /**
  305. * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
  306. * op for master devices
  307. *
  308. * @dev: port device
  309. * @nlh: netlink msg with bridge port attributes
  310. * @flags: bridge dellink flags
  311. *
  312. * Notify master device slaves of bridge port attribute deletes
  313. */
  314. int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
  315. struct nlmsghdr *nlh, u16 flags)
  316. {
  317. struct net_device *lower_dev;
  318. struct list_head *iter;
  319. int ret = 0, err = 0;
  320. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  321. return ret;
  322. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  323. err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
  324. if (err && err != -EOPNOTSUPP)
  325. ret = err;
  326. }
  327. return ret;
  328. }
  329. EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
  330. static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
  331. {
  332. const struct switchdev_ops *ops = dev->switchdev_ops;
  333. struct net_device *lower_dev;
  334. struct net_device *port_dev;
  335. struct list_head *iter;
  336. /* Recusively search down until we find a sw port dev.
  337. * (A sw port dev supports switchdev_port_attr_get).
  338. */
  339. if (ops && ops->switchdev_port_attr_get)
  340. return dev;
  341. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  342. port_dev = switchdev_get_lowest_dev(lower_dev);
  343. if (port_dev)
  344. return port_dev;
  345. }
  346. return NULL;
  347. }
  348. static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
  349. {
  350. struct switchdev_attr attr = {
  351. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  352. };
  353. struct switchdev_attr prev_attr;
  354. struct net_device *dev = NULL;
  355. int nhsel;
  356. /* For this route, all nexthop devs must be on the same switch. */
  357. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  358. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  359. if (!nh->nh_dev)
  360. return NULL;
  361. dev = switchdev_get_lowest_dev(nh->nh_dev);
  362. if (!dev)
  363. return NULL;
  364. if (switchdev_port_attr_get(dev, &attr))
  365. return NULL;
  366. if (nhsel > 0) {
  367. if (prev_attr.ppid.id_len != attr.ppid.id_len)
  368. return NULL;
  369. if (memcmp(prev_attr.ppid.id, attr.ppid.id,
  370. attr.ppid.id_len))
  371. return NULL;
  372. }
  373. prev_attr = attr;
  374. }
  375. return dev;
  376. }
  377. /**
  378. * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
  379. *
  380. * @dst: route's IPv4 destination address
  381. * @dst_len: destination address length (prefix length)
  382. * @fi: route FIB info structure
  383. * @tos: route TOS
  384. * @type: route type
  385. * @nlflags: netlink flags passed in (NLM_F_*)
  386. * @tb_id: route table ID
  387. *
  388. * Add IPv4 route entry to switch device.
  389. */
  390. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  391. u8 tos, u8 type, u32 nlflags, u32 tb_id)
  392. {
  393. struct net_device *dev;
  394. const struct switchdev_ops *ops;
  395. int err = 0;
  396. /* Don't offload route if using custom ip rules or if
  397. * IPv4 FIB offloading has been disabled completely.
  398. */
  399. #ifdef CONFIG_IP_MULTIPLE_TABLES
  400. if (fi->fib_net->ipv4.fib_has_custom_rules)
  401. return 0;
  402. #endif
  403. if (fi->fib_net->ipv4.fib_offload_disabled)
  404. return 0;
  405. dev = switchdev_get_dev_by_nhs(fi);
  406. if (!dev)
  407. return 0;
  408. ops = dev->switchdev_ops;
  409. if (ops->switchdev_fib_ipv4_add) {
  410. err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
  411. fi, tos, type, nlflags,
  412. tb_id);
  413. if (!err)
  414. fi->fib_flags |= RTNH_F_EXTERNAL;
  415. }
  416. return err;
  417. }
  418. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
  419. /**
  420. * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
  421. *
  422. * @dst: route's IPv4 destination address
  423. * @dst_len: destination address length (prefix length)
  424. * @fi: route FIB info structure
  425. * @tos: route TOS
  426. * @type: route type
  427. * @tb_id: route table ID
  428. *
  429. * Delete IPv4 route entry from switch device.
  430. */
  431. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  432. u8 tos, u8 type, u32 tb_id)
  433. {
  434. struct net_device *dev;
  435. const struct switchdev_ops *ops;
  436. int err = 0;
  437. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  438. return 0;
  439. dev = switchdev_get_dev_by_nhs(fi);
  440. if (!dev)
  441. return 0;
  442. ops = dev->switchdev_ops;
  443. if (ops->switchdev_fib_ipv4_del) {
  444. err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
  445. fi, tos, type, tb_id);
  446. if (!err)
  447. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  448. }
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
  452. /**
  453. * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
  454. *
  455. * @fi: route FIB info structure
  456. */
  457. void switchdev_fib_ipv4_abort(struct fib_info *fi)
  458. {
  459. /* There was a problem installing this route to the offload
  460. * device. For now, until we come up with more refined
  461. * policy handling, abruptly end IPv4 fib offloading for
  462. * for entire net by flushing offload device(s) of all
  463. * IPv4 routes, and mark IPv4 fib offloading broken from
  464. * this point forward.
  465. */
  466. fib_flush_external(fi->fib_net);
  467. fi->fib_net->ipv4.fib_offload_disabled = true;
  468. }
  469. EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);