switchdev.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /**
  202. * netdev_switch_fib_ipv4_add - Add IPv4 route entry to switch
  203. *
  204. * @dst: route's IPv4 destination address
  205. * @dst_len: destination address length (prefix length)
  206. * @fi: route FIB info structure
  207. * @tos: route TOS
  208. * @type: route type
  209. * @tb_id: route table ID
  210. *
  211. * Add IPv4 route entry to switch device.
  212. */
  213. int netdev_switch_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  214. u8 tos, u8 type, u32 tb_id)
  215. {
  216. /* Don't offload route if using custom ip rules */
  217. if (fi->fib_net->ipv4.fib_has_custom_rules)
  218. return 0;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(netdev_switch_fib_ipv4_add);
  222. /**
  223. * netdev_switch_fib_ipv4_del - Delete IPv4 route entry from switch
  224. *
  225. * @dst: route's IPv4 destination address
  226. * @dst_len: destination address length (prefix length)
  227. * @fi: route FIB info structure
  228. * @tos: route TOS
  229. * @type: route type
  230. * @tb_id: route table ID
  231. *
  232. * Delete IPv4 route entry from switch device.
  233. */
  234. int netdev_switch_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  235. u8 tos, u8 type, u32 tb_id)
  236. {
  237. return 0;
  238. }
  239. EXPORT_SYMBOL(netdev_switch_fib_ipv4_del);