port.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Handling of a single switch port
  3. *
  4. * Copyright (c) 2017 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/if_bridge.h>
  13. #include <linux/notifier.h>
  14. #include "dsa_priv.h"
  15. static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
  16. {
  17. struct raw_notifier_head *nh = &dp->ds->dst->nh;
  18. int err;
  19. err = raw_notifier_call_chain(nh, e, v);
  20. return notifier_to_errno(err);
  21. }
  22. int dsa_port_set_state(struct dsa_port *dp, u8 state,
  23. struct switchdev_trans *trans)
  24. {
  25. struct dsa_switch *ds = dp->ds;
  26. int port = dp->index;
  27. if (switchdev_trans_ph_prepare(trans))
  28. return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
  29. if (ds->ops->port_stp_state_set)
  30. ds->ops->port_stp_state_set(ds, port, state);
  31. if (ds->ops->port_fast_age) {
  32. /* Fast age FDB entries or flush appropriate forwarding database
  33. * for the given port, if we are moving it from Learning or
  34. * Forwarding state, to Disabled or Blocking or Listening state.
  35. */
  36. if ((dp->stp_state == BR_STATE_LEARNING ||
  37. dp->stp_state == BR_STATE_FORWARDING) &&
  38. (state == BR_STATE_DISABLED ||
  39. state == BR_STATE_BLOCKING ||
  40. state == BR_STATE_LISTENING))
  41. ds->ops->port_fast_age(ds, port);
  42. }
  43. dp->stp_state = state;
  44. return 0;
  45. }
  46. void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
  47. {
  48. int err;
  49. err = dsa_port_set_state(dp, state, NULL);
  50. if (err)
  51. pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
  52. }
  53. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
  54. {
  55. struct dsa_notifier_bridge_info info = {
  56. .sw_index = dp->ds->index,
  57. .port = dp->index,
  58. .br = br,
  59. };
  60. int err;
  61. /* Here the port is already bridged. Reflect the current configuration
  62. * so that drivers can program their chips accordingly.
  63. */
  64. dp->bridge_dev = br;
  65. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
  66. /* The bridging is rolled back on error */
  67. if (err)
  68. dp->bridge_dev = NULL;
  69. return err;
  70. }
  71. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  72. {
  73. struct dsa_notifier_bridge_info info = {
  74. .sw_index = dp->ds->index,
  75. .port = dp->index,
  76. .br = br,
  77. };
  78. int err;
  79. /* Here the port is already unbridged. Reflect the current configuration
  80. * so that drivers can program their chips accordingly.
  81. */
  82. dp->bridge_dev = NULL;
  83. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  84. if (err)
  85. pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
  86. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  87. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  88. */
  89. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  90. }
  91. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  92. struct switchdev_trans *trans)
  93. {
  94. struct dsa_switch *ds = dp->ds;
  95. /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
  96. if (switchdev_trans_ph_prepare(trans))
  97. return 0;
  98. if (ds->ops->port_vlan_filtering)
  99. return ds->ops->port_vlan_filtering(ds, dp->index,
  100. vlan_filtering);
  101. return 0;
  102. }
  103. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
  104. struct switchdev_trans *trans)
  105. {
  106. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  107. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  108. struct dsa_notifier_ageing_time_info info = {
  109. .ageing_time = ageing_time,
  110. .trans = trans,
  111. };
  112. if (switchdev_trans_ph_prepare(trans))
  113. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  114. dp->ageing_time = ageing_time;
  115. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  116. }
  117. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  118. u16 vid)
  119. {
  120. struct dsa_notifier_fdb_info info = {
  121. .sw_index = dp->ds->index,
  122. .port = dp->index,
  123. .addr = addr,
  124. .vid = vid,
  125. };
  126. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  127. }
  128. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  129. u16 vid)
  130. {
  131. struct dsa_notifier_fdb_info info = {
  132. .sw_index = dp->ds->index,
  133. .port = dp->index,
  134. .addr = addr,
  135. .vid = vid,
  136. };
  137. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  138. }
  139. int dsa_port_mdb_add(struct dsa_port *dp,
  140. const struct switchdev_obj_port_mdb *mdb,
  141. struct switchdev_trans *trans)
  142. {
  143. struct dsa_notifier_mdb_info info = {
  144. .sw_index = dp->ds->index,
  145. .port = dp->index,
  146. .trans = trans,
  147. .mdb = mdb,
  148. };
  149. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  150. }
  151. int dsa_port_mdb_del(struct dsa_port *dp,
  152. const struct switchdev_obj_port_mdb *mdb)
  153. {
  154. struct dsa_notifier_mdb_info info = {
  155. .sw_index = dp->ds->index,
  156. .port = dp->index,
  157. .mdb = mdb,
  158. };
  159. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  160. }
  161. int dsa_port_vlan_add(struct dsa_port *dp,
  162. const struct switchdev_obj_port_vlan *vlan,
  163. struct switchdev_trans *trans)
  164. {
  165. struct dsa_notifier_vlan_info info = {
  166. .sw_index = dp->ds->index,
  167. .port = dp->index,
  168. .trans = trans,
  169. .vlan = vlan,
  170. };
  171. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  172. }
  173. int dsa_port_vlan_del(struct dsa_port *dp,
  174. const struct switchdev_obj_port_vlan *vlan)
  175. {
  176. struct dsa_notifier_vlan_info info = {
  177. .sw_index = dp->ds->index,
  178. .port = dp->index,
  179. .vlan = vlan,
  180. };
  181. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  182. }