switchdev.c 13 KB

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