port.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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,
  118. const struct switchdev_obj_port_fdb *fdb,
  119. struct switchdev_trans *trans)
  120. {
  121. struct dsa_notifier_fdb_info info = {
  122. .sw_index = dp->ds->index,
  123. .port = dp->index,
  124. .trans = trans,
  125. .fdb = fdb,
  126. };
  127. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  128. }
  129. int dsa_port_fdb_del(struct dsa_port *dp,
  130. const struct switchdev_obj_port_fdb *fdb)
  131. {
  132. struct dsa_notifier_fdb_info info = {
  133. .sw_index = dp->ds->index,
  134. .port = dp->index,
  135. .fdb = fdb,
  136. };
  137. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  138. }
  139. int dsa_port_fdb_dump(struct dsa_port *dp, struct switchdev_obj_port_fdb *fdb,
  140. switchdev_obj_dump_cb_t *cb)
  141. {
  142. struct dsa_switch *ds = dp->ds;
  143. if (ds->ops->port_fdb_dump)
  144. return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
  145. return -EOPNOTSUPP;
  146. }
  147. int dsa_port_mdb_add(struct dsa_port *dp,
  148. const struct switchdev_obj_port_mdb *mdb,
  149. struct switchdev_trans *trans)
  150. {
  151. struct dsa_notifier_mdb_info info = {
  152. .sw_index = dp->ds->index,
  153. .port = dp->index,
  154. .trans = trans,
  155. .mdb = mdb,
  156. };
  157. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  158. }
  159. int dsa_port_mdb_del(struct dsa_port *dp,
  160. const struct switchdev_obj_port_mdb *mdb)
  161. {
  162. struct dsa_notifier_mdb_info info = {
  163. .sw_index = dp->ds->index,
  164. .port = dp->index,
  165. .mdb = mdb,
  166. };
  167. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  168. }
  169. int dsa_port_mdb_dump(struct dsa_port *dp, struct switchdev_obj_port_mdb *mdb,
  170. switchdev_obj_dump_cb_t *cb)
  171. {
  172. struct dsa_switch *ds = dp->ds;
  173. if (ds->ops->port_mdb_dump)
  174. return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
  175. return -EOPNOTSUPP;
  176. }
  177. int dsa_port_vlan_add(struct dsa_port *dp,
  178. const struct switchdev_obj_port_vlan *vlan,
  179. struct switchdev_trans *trans)
  180. {
  181. struct dsa_notifier_vlan_info info = {
  182. .sw_index = dp->ds->index,
  183. .port = dp->index,
  184. .trans = trans,
  185. .vlan = vlan,
  186. };
  187. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  188. }
  189. int dsa_port_vlan_del(struct dsa_port *dp,
  190. const struct switchdev_obj_port_vlan *vlan)
  191. {
  192. struct dsa_notifier_vlan_info info = {
  193. .sw_index = dp->ds->index,
  194. .port = dp->index,
  195. .vlan = vlan,
  196. };
  197. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  198. }
  199. int dsa_port_vlan_dump(struct dsa_port *dp,
  200. struct switchdev_obj_port_vlan *vlan,
  201. switchdev_obj_dump_cb_t *cb)
  202. {
  203. struct dsa_switch *ds = dp->ds;
  204. if (ds->ops->port_vlan_dump)
  205. return ds->ops->port_vlan_dump(ds, dp->index, vlan, cb);
  206. return -EOPNOTSUPP;
  207. }