switchdev.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/switchdev.h>
  17. /**
  18. * netdev_switch_parent_id_get - Get ID of a switch
  19. * @dev: port device
  20. * @psid: switch ID
  21. *
  22. * Get ID of a switch this port is part of.
  23. */
  24. int netdev_switch_parent_id_get(struct net_device *dev,
  25. struct netdev_phys_item_id *psid)
  26. {
  27. const struct net_device_ops *ops = dev->netdev_ops;
  28. if (!ops->ndo_switch_parent_id_get)
  29. return -EOPNOTSUPP;
  30. return ops->ndo_switch_parent_id_get(dev, psid);
  31. }
  32. EXPORT_SYMBOL(netdev_switch_parent_id_get);
  33. /**
  34. * netdev_switch_port_stp_update - Notify switch device port of STP
  35. * state change
  36. * @dev: port device
  37. * @state: port STP state
  38. *
  39. * Notify switch device port of bridge port STP state change.
  40. */
  41. int netdev_switch_port_stp_update(struct net_device *dev, u8 state)
  42. {
  43. const struct net_device_ops *ops = dev->netdev_ops;
  44. if (!ops->ndo_switch_port_stp_update)
  45. return -EOPNOTSUPP;
  46. WARN_ON(!ops->ndo_switch_parent_id_get);
  47. return ops->ndo_switch_port_stp_update(dev, state);
  48. }
  49. EXPORT_SYMBOL(netdev_switch_port_stp_update);
  50. static DEFINE_MUTEX(netdev_switch_mutex);
  51. static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain);
  52. /**
  53. * register_netdev_switch_notifier - Register nofifier
  54. * @nb: notifier_block
  55. *
  56. * Register switch device notifier. This should be used by code
  57. * which needs to monitor events happening in particular device.
  58. * Return values are same as for atomic_notifier_chain_register().
  59. */
  60. int register_netdev_switch_notifier(struct notifier_block *nb)
  61. {
  62. int err;
  63. mutex_lock(&netdev_switch_mutex);
  64. err = raw_notifier_chain_register(&netdev_switch_notif_chain, nb);
  65. mutex_unlock(&netdev_switch_mutex);
  66. return err;
  67. }
  68. EXPORT_SYMBOL(register_netdev_switch_notifier);
  69. /**
  70. * unregister_netdev_switch_notifier - Unregister nofifier
  71. * @nb: notifier_block
  72. *
  73. * Unregister switch device notifier.
  74. * Return values are same as for atomic_notifier_chain_unregister().
  75. */
  76. int unregister_netdev_switch_notifier(struct notifier_block *nb)
  77. {
  78. int err;
  79. mutex_lock(&netdev_switch_mutex);
  80. err = raw_notifier_chain_unregister(&netdev_switch_notif_chain, nb);
  81. mutex_unlock(&netdev_switch_mutex);
  82. return err;
  83. }
  84. EXPORT_SYMBOL(unregister_netdev_switch_notifier);
  85. /**
  86. * call_netdev_switch_notifiers - Call nofifiers
  87. * @val: value passed unmodified to notifier function
  88. * @dev: port device
  89. * @info: notifier information data
  90. *
  91. * Call all network notifier blocks. This should be called by driver
  92. * when it needs to propagate hardware event.
  93. * Return values are same as for atomic_notifier_call_chain().
  94. */
  95. int call_netdev_switch_notifiers(unsigned long val, struct net_device *dev,
  96. struct netdev_switch_notifier_info *info)
  97. {
  98. int err;
  99. info->dev = dev;
  100. mutex_lock(&netdev_switch_mutex);
  101. err = raw_notifier_call_chain(&netdev_switch_notif_chain, val, info);
  102. mutex_unlock(&netdev_switch_mutex);
  103. return err;
  104. }
  105. EXPORT_SYMBOL(call_netdev_switch_notifiers);
  106. /**
  107. * netdev_switch_port_bridge_setlink - Notify switch device port of bridge
  108. * port attributes
  109. *
  110. * @dev: port device
  111. * @nlh: netlink msg with bridge port attributes
  112. * @flags: bridge setlink flags
  113. *
  114. * Notify switch device port of bridge port attributes
  115. */
  116. int netdev_switch_port_bridge_setlink(struct net_device *dev,
  117. struct nlmsghdr *nlh, u16 flags)
  118. {
  119. const struct net_device_ops *ops = dev->netdev_ops;
  120. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  121. return 0;
  122. if (!ops->ndo_bridge_setlink)
  123. return -EOPNOTSUPP;
  124. return ops->ndo_bridge_setlink(dev, nlh, flags);
  125. }
  126. EXPORT_SYMBOL(netdev_switch_port_bridge_setlink);
  127. /**
  128. * netdev_switch_port_bridge_dellink - Notify switch device port of bridge
  129. * port attribute delete
  130. *
  131. * @dev: port device
  132. * @nlh: netlink msg with bridge port attributes
  133. * @flags: bridge setlink flags
  134. *
  135. * Notify switch device port of bridge port attribute delete
  136. */
  137. int netdev_switch_port_bridge_dellink(struct net_device *dev,
  138. struct nlmsghdr *nlh, u16 flags)
  139. {
  140. const struct net_device_ops *ops = dev->netdev_ops;
  141. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  142. return 0;
  143. if (!ops->ndo_bridge_dellink)
  144. return -EOPNOTSUPP;
  145. return ops->ndo_bridge_dellink(dev, nlh, flags);
  146. }
  147. EXPORT_SYMBOL(netdev_switch_port_bridge_dellink);
  148. /**
  149. * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink
  150. * op for master devices
  151. *
  152. * @dev: port device
  153. * @nlh: netlink msg with bridge port attributes
  154. * @flags: bridge setlink flags
  155. *
  156. * Notify master device slaves of bridge port attributes
  157. */
  158. int ndo_dflt_netdev_switch_port_bridge_setlink(struct net_device *dev,
  159. struct nlmsghdr *nlh, u16 flags)
  160. {
  161. struct net_device *lower_dev;
  162. struct list_head *iter;
  163. int ret = 0, err = 0;
  164. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  165. return ret;
  166. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  167. err = netdev_switch_port_bridge_setlink(lower_dev, nlh, flags);
  168. if (err && err != -EOPNOTSUPP)
  169. ret = err;
  170. }
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_setlink);
  174. /**
  175. * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink
  176. * op for master devices
  177. *
  178. * @dev: port device
  179. * @nlh: netlink msg with bridge port attributes
  180. * @flags: bridge dellink flags
  181. *
  182. * Notify master device slaves of bridge port attribute deletes
  183. */
  184. int ndo_dflt_netdev_switch_port_bridge_dellink(struct net_device *dev,
  185. struct nlmsghdr *nlh, u16 flags)
  186. {
  187. struct net_device *lower_dev;
  188. struct list_head *iter;
  189. int ret = 0, err = 0;
  190. if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
  191. return ret;
  192. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  193. err = netdev_switch_port_bridge_dellink(lower_dev, nlh, flags);
  194. if (err && err != -EOPNOTSUPP)
  195. ret = err;
  196. }
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_dellink);