team_mode_activebackup.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/team/team_mode_activebackup.c - Active-backup mode for team
  4. * Copyright (c) 2011 Jiri Pirko <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/errno.h>
  11. #include <linux/netdevice.h>
  12. #include <net/rtnetlink.h>
  13. #include <linux/if_team.h>
  14. struct ab_priv {
  15. struct team_port __rcu *active_port;
  16. struct team_option_inst_info *ap_opt_inst_info;
  17. };
  18. static struct ab_priv *ab_priv(struct team *team)
  19. {
  20. return (struct ab_priv *) &team->mode_priv;
  21. }
  22. static rx_handler_result_t ab_receive(struct team *team, struct team_port *port,
  23. struct sk_buff *skb) {
  24. struct team_port *active_port;
  25. active_port = rcu_dereference(ab_priv(team)->active_port);
  26. if (active_port != port)
  27. return RX_HANDLER_EXACT;
  28. return RX_HANDLER_ANOTHER;
  29. }
  30. static bool ab_transmit(struct team *team, struct sk_buff *skb)
  31. {
  32. struct team_port *active_port;
  33. active_port = rcu_dereference_bh(ab_priv(team)->active_port);
  34. if (unlikely(!active_port))
  35. goto drop;
  36. if (team_dev_queue_xmit(team, active_port, skb))
  37. return false;
  38. return true;
  39. drop:
  40. dev_kfree_skb_any(skb);
  41. return false;
  42. }
  43. static void ab_port_leave(struct team *team, struct team_port *port)
  44. {
  45. if (ab_priv(team)->active_port == port) {
  46. RCU_INIT_POINTER(ab_priv(team)->active_port, NULL);
  47. team_option_inst_set_change(ab_priv(team)->ap_opt_inst_info);
  48. }
  49. }
  50. static int ab_active_port_init(struct team *team,
  51. struct team_option_inst_info *info)
  52. {
  53. ab_priv(team)->ap_opt_inst_info = info;
  54. return 0;
  55. }
  56. static int ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
  57. {
  58. struct team_port *active_port;
  59. active_port = rcu_dereference_protected(ab_priv(team)->active_port,
  60. lockdep_is_held(&team->lock));
  61. if (active_port)
  62. ctx->data.u32_val = active_port->dev->ifindex;
  63. else
  64. ctx->data.u32_val = 0;
  65. return 0;
  66. }
  67. static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
  68. {
  69. struct team_port *port;
  70. list_for_each_entry(port, &team->port_list, list) {
  71. if (port->dev->ifindex == ctx->data.u32_val) {
  72. rcu_assign_pointer(ab_priv(team)->active_port, port);
  73. return 0;
  74. }
  75. }
  76. return -ENOENT;
  77. }
  78. static const struct team_option ab_options[] = {
  79. {
  80. .name = "activeport",
  81. .type = TEAM_OPTION_TYPE_U32,
  82. .init = ab_active_port_init,
  83. .getter = ab_active_port_get,
  84. .setter = ab_active_port_set,
  85. },
  86. };
  87. static int ab_init(struct team *team)
  88. {
  89. return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
  90. }
  91. static void ab_exit(struct team *team)
  92. {
  93. team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
  94. }
  95. static const struct team_mode_ops ab_mode_ops = {
  96. .init = ab_init,
  97. .exit = ab_exit,
  98. .receive = ab_receive,
  99. .transmit = ab_transmit,
  100. .port_leave = ab_port_leave,
  101. };
  102. static const struct team_mode ab_mode = {
  103. .kind = "activebackup",
  104. .owner = THIS_MODULE,
  105. .priv_size = sizeof(struct ab_priv),
  106. .ops = &ab_mode_ops,
  107. .lag_tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP,
  108. };
  109. static int __init ab_init_module(void)
  110. {
  111. return team_mode_register(&ab_mode);
  112. }
  113. static void __exit ab_cleanup_module(void)
  114. {
  115. team_mode_unregister(&ab_mode);
  116. }
  117. module_init(ab_init_module);
  118. module_exit(ab_cleanup_module);
  119. MODULE_LICENSE("GPL v2");
  120. MODULE_AUTHOR("Jiri Pirko <[email protected]>");
  121. MODULE_DESCRIPTION("Active-backup mode for team");
  122. MODULE_ALIAS_TEAM_MODE("activebackup");