switchdev.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * net/switchdev/switchdev.c - Switch device API
  3. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13. #include <linux/mutex.h>
  14. #include <linux/notifier.h>
  15. #include <linux/netdevice.h>
  16. #include <net/ip_fib.h>
  17. #include <net/switchdev.h>
  18. /**
  19. * netdev_switch_parent_id_get - Get ID of a switch
  20. * @dev: port device
  21. * @psid: switch ID
  22. *
  23. * Get ID of a switch this port is part of.
  24. */
  25. int netdev_switch_parent_id_get(struct net_device *dev,
  26. struct netdev_phys_item_id *psid)
  27. {
  28. const struct net_device_ops *ops = dev->netdev_ops;
  29. if (!ops->ndo_switch_parent_id_get)
  30. return -EOPNOTSUPP;
  31. return ops->ndo_switch_parent_id_get(dev, psid);
  32. }
  33. EXPORT_SYMBOL(netdev_switch_parent_id_get);
  34. /**
  35. * netdev_switch_port_stp_update - Notify switch device port of STP
  36. * state change
  37. * @dev: port device
  38. * @state: port STP state
  39. *
  40. * Notify switch device port of bridge port STP state change.
  41. */
  42. int netdev_switch_port_stp_update(struct net_device *dev, u8 state)
  43. {
  44. const struct net_device_ops *ops = dev->netdev_ops;
  45. if (!ops->ndo_switch_port_stp_update)
  46. return -EOPNOTSUPP;
  47. WARN_ON(!ops->ndo_switch_parent_id_get);
  48. return ops->ndo_switch_port_stp_update(dev, state);
  49. }
  50. EXPORT_SYMBOL(netdev_switch_port_stp_update);
  51. static DEFINE_MUTEX(netdev_switch_mutex);
  52. static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain);
  53. /**
  54. * register_netdev_switch_notifier - Register nofifier
  55. * @nb: notifier_block
  56. *
  57. * Register switch device notifier. This should be used by code
  58. * which needs to monitor events happening in particular device.
  59. * Return values are same as for atomic_notifier_chain_register().
  60. */
  61. int register_netdev_switch_notifier(struct notifier_block *nb)
  62. {
  63. int err;
  64. mutex_lock(&netdev_switch_mutex);
  65. err = raw_notifier_chain_register(&netdev_switch_notif_chain, nb);
  66. mutex_unlock(&netdev_switch_mutex);
  67. return err;
  68. }
  69. EXPORT_SYMBOL(register_netdev_switch_notifier);
  70. /**
  71. * unregister_netdev_switch_notifier - Unregister nofifier
  72. * @nb: notifier_block
  73. *
  74. * Unregister switch device notifier.
  75. * Return values are same as for atomic_notifier_chain_unregister().
  76. */
  77. int unregister_netdev_switch_notifier(struct notifier_block *nb)
  78. {
  79. int err;
  80. mutex_lock(&netdev_switch_mutex);
  81. err = raw_notifier_chain_unregister(&netdev_switch_notif_chain, nb);
  82. mutex_unlock(&netdev_switch_mutex);
  83. return err;
  84. }
  85. EXPORT_SYMBOL(unregister_netdev_switch_notifier);
  86. /**
  87. * call_netdev_switch_notifiers - Call nofifiers
  88. * @val: value passed unmodified to notifier function
  89. * @dev: port device
  90. * @info: notifier information data
  91. *
  92. * Call all network notifier blocks. This should be called by driver
  93. * when it needs to propagate hardware event.
  94. * Return values are same as for atomic_notifier_call_chain().
  95. */
  96. int call_netdev_switch_notifiers(unsigned long val, struct net_device *dev,
  97. struct netdev_switch_notifier_info *info)
  98. {
  99. int err;
  100. info->dev = dev;
  101. mutex_lock(&netdev_switch_mutex);
  102. err = raw_notifier_call_chain(&netdev_switch_notif_chain, val, info);
  103. mutex_unlock(&netdev_switch_mutex);
  104. return err;
  105. }
  106. EXPORT_SYMBOL(call_netdev_switch_notifiers);
  107. /**
  108. * netdev_switch_port_bridge_setlink - Notify switch device port of bridge
  109. * port attributes
  110. *
  111. * @dev: port device
  112. * @nlh: netlink msg with bridge port attributes
  113. * @flags: bridge setlink flags
  114. *
  115. * Notify switch device port of bridge port attributes
  116. */
  117. int netdev_switch_port_bridge_setlink(struct net_device *dev,
  118. struct nlmsghdr *nlh, u16 flags)
  119. {
  120. const struct net_device_ops *ops = dev->netdev_ops;
  121. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  122. return 0;
  123. if (!ops->ndo_bridge_setlink)
  124. return -EOPNOTSUPP;
  125. return ops->ndo_bridge_setlink(dev, nlh, flags);
  126. }
  127. EXPORT_SYMBOL(netdev_switch_port_bridge_setlink);
  128. /**
  129. * netdev_switch_port_bridge_dellink - Notify switch device port of bridge
  130. * port attribute delete
  131. *
  132. * @dev: port device
  133. * @nlh: netlink msg with bridge port attributes
  134. * @flags: bridge setlink flags
  135. *
  136. * Notify switch device port of bridge port attribute delete
  137. */
  138. int netdev_switch_port_bridge_dellink(struct net_device *dev,
  139. struct nlmsghdr *nlh, u16 flags)
  140. {
  141. const struct net_device_ops *ops = dev->netdev_ops;
  142. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  143. return 0;
  144. if (!ops->ndo_bridge_dellink)
  145. return -EOPNOTSUPP;
  146. return ops->ndo_bridge_dellink(dev, nlh, flags);
  147. }
  148. EXPORT_SYMBOL(netdev_switch_port_bridge_dellink);
  149. /**
  150. * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink
  151. * op for master devices
  152. *
  153. * @dev: port device
  154. * @nlh: netlink msg with bridge port attributes
  155. * @flags: bridge setlink flags
  156. *
  157. * Notify master device slaves of bridge port attributes
  158. */
  159. int ndo_dflt_netdev_switch_port_bridge_setlink(struct net_device *dev,
  160. struct nlmsghdr *nlh, u16 flags)
  161. {
  162. struct net_device *lower_dev;
  163. struct list_head *iter;
  164. int ret = 0, err = 0;
  165. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  166. return ret;
  167. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  168. err = netdev_switch_port_bridge_setlink(lower_dev, nlh, flags);
  169. if (err && err != -EOPNOTSUPP)
  170. ret = err;
  171. }
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_setlink);
  175. /**
  176. * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink
  177. * op for master devices
  178. *
  179. * @dev: port device
  180. * @nlh: netlink msg with bridge port attributes
  181. * @flags: bridge dellink flags
  182. *
  183. * Notify master device slaves of bridge port attribute deletes
  184. */
  185. int ndo_dflt_netdev_switch_port_bridge_dellink(struct net_device *dev,
  186. struct nlmsghdr *nlh, u16 flags)
  187. {
  188. struct net_device *lower_dev;
  189. struct list_head *iter;
  190. int ret = 0, err = 0;
  191. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  192. return ret;
  193. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  194. err = netdev_switch_port_bridge_dellink(lower_dev, nlh, flags);
  195. if (err && err != -EOPNOTSUPP)
  196. ret = err;
  197. }
  198. return ret;
  199. }
  200. EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_dellink);
  201. static struct net_device *netdev_switch_get_lowest_dev(struct net_device *dev)
  202. {
  203. const struct net_device_ops *ops = dev->netdev_ops;
  204. struct net_device *lower_dev;
  205. struct net_device *port_dev;
  206. struct list_head *iter;
  207. /* Recusively search down until we find a sw port dev.
  208. * (A sw port dev supports ndo_switch_parent_id_get).
  209. */
  210. if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
  211. ops->ndo_switch_parent_id_get)
  212. return dev;
  213. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  214. port_dev = netdev_switch_get_lowest_dev(lower_dev);
  215. if (port_dev)
  216. return port_dev;
  217. }
  218. return NULL;
  219. }
  220. static struct net_device *netdev_switch_get_dev_by_nhs(struct fib_info *fi)
  221. {
  222. struct netdev_phys_item_id psid;
  223. struct netdev_phys_item_id prev_psid;
  224. struct net_device *dev = NULL;
  225. int nhsel;
  226. /* For this route, all nexthop devs must be on the same switch. */
  227. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  228. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  229. if (!nh->nh_dev)
  230. return NULL;
  231. dev = netdev_switch_get_lowest_dev(nh->nh_dev);
  232. if (!dev)
  233. return NULL;
  234. if (netdev_switch_parent_id_get(dev, &psid))
  235. return NULL;
  236. if (nhsel > 0) {
  237. if (prev_psid.id_len != psid.id_len)
  238. return NULL;
  239. if (memcmp(prev_psid.id, psid.id, psid.id_len))
  240. return NULL;
  241. }
  242. prev_psid = psid;
  243. }
  244. return dev;
  245. }
  246. /**
  247. * netdev_switch_fib_ipv4_add - Add IPv4 route entry to switch
  248. *
  249. * @dst: route's IPv4 destination address
  250. * @dst_len: destination address length (prefix length)
  251. * @fi: route FIB info structure
  252. * @tos: route TOS
  253. * @type: route type
  254. * @tb_id: route table ID
  255. *
  256. * Add IPv4 route entry to switch device.
  257. */
  258. int netdev_switch_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  259. u8 tos, u8 type, u32 tb_id)
  260. {
  261. struct net_device *dev;
  262. const struct net_device_ops *ops;
  263. int err = 0;
  264. /* Don't offload route if using custom ip rules or if
  265. * IPv4 FIB offloading has been disabled completely.
  266. */
  267. if (fi->fib_net->ipv4.fib_has_custom_rules |
  268. fi->fib_net->ipv4.fib_offload_disabled)
  269. return 0;
  270. dev = netdev_switch_get_dev_by_nhs(fi);
  271. if (!dev)
  272. return 0;
  273. ops = dev->netdev_ops;
  274. if (ops->ndo_switch_fib_ipv4_add) {
  275. err = ops->ndo_switch_fib_ipv4_add(dev, htonl(dst), dst_len,
  276. fi, tos, type, tb_id);
  277. if (!err)
  278. fi->fib_flags |= RTNH_F_EXTERNAL;
  279. }
  280. return err;
  281. }
  282. EXPORT_SYMBOL(netdev_switch_fib_ipv4_add);
  283. /**
  284. * netdev_switch_fib_ipv4_del - Delete IPv4 route entry from switch
  285. *
  286. * @dst: route's IPv4 destination address
  287. * @dst_len: destination address length (prefix length)
  288. * @fi: route FIB info structure
  289. * @tos: route TOS
  290. * @type: route type
  291. * @tb_id: route table ID
  292. *
  293. * Delete IPv4 route entry from switch device.
  294. */
  295. int netdev_switch_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  296. u8 tos, u8 type, u32 tb_id)
  297. {
  298. struct net_device *dev;
  299. const struct net_device_ops *ops;
  300. int err = 0;
  301. if (!(fi->fib_flags & RTNH_F_EXTERNAL))
  302. return 0;
  303. dev = netdev_switch_get_dev_by_nhs(fi);
  304. if (!dev)
  305. return 0;
  306. ops = dev->netdev_ops;
  307. if (ops->ndo_switch_fib_ipv4_del) {
  308. err = ops->ndo_switch_fib_ipv4_del(dev, htonl(dst), dst_len,
  309. fi, tos, type, tb_id);
  310. if (!err)
  311. fi->fib_flags &= ~RTNH_F_EXTERNAL;
  312. }
  313. return err;
  314. }
  315. EXPORT_SYMBOL(netdev_switch_fib_ipv4_del);
  316. /**
  317. * netdev_switch_fib_ipv4_abort - Abort an IPv4 FIB operation
  318. *
  319. * @fi: route FIB info structure
  320. */
  321. void netdev_switch_fib_ipv4_abort(struct fib_info *fi)
  322. {
  323. /* There was a problem installing this route to the offload
  324. * device. For now, until we come up with more refined
  325. * policy handling, abruptly end IPv4 fib offloading for
  326. * for entire net by flushing offload device(s) of all
  327. * IPv4 routes, and mark IPv4 fib offloading broken from
  328. * this point forward.
  329. */
  330. fib_flush_external(fi->fib_net);
  331. fi->fib_net->ipv4.fib_offload_disabled = true;
  332. }
  333. EXPORT_SYMBOL(netdev_switch_fib_ipv4_abort);