switchdev.c 10 KB

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