port.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. }