team_mode_activebackup.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <jpirko@redhat.com>
  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 void 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. }
  55. static void ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
  56. {
  57. struct team_port *active_port;
  58. active_port = rtnl_dereference(ab_priv(team)->active_port);
  59. if (active_port)
  60. ctx->data.u32_val = active_port->dev->ifindex;
  61. else
  62. ctx->data.u32_val = 0;
  63. }
  64. static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
  65. {
  66. struct team_port *port;
  67. list_for_each_entry(port, &team->port_list, list) {
  68. if (port->dev->ifindex == ctx->data.u32_val) {
  69. rcu_assign_pointer(ab_priv(team)->active_port, port);
  70. return 0;
  71. }
  72. }
  73. return -ENOENT;
  74. }
  75. static const struct team_option ab_options[] = {
  76. {
  77. .name = "activeport",
  78. .type = TEAM_OPTION_TYPE_U32,
  79. .init = ab_active_port_init,
  80. .getter = ab_active_port_get,
  81. .setter = ab_active_port_set,
  82. },
  83. };
  84. static int ab_init(struct team *team)
  85. {
  86. return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
  87. }
  88. static void ab_exit(struct team *team)
  89. {
  90. team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
  91. }
  92. static const struct team_mode_ops ab_mode_ops = {
  93. .init = ab_init,
  94. .exit = ab_exit,
  95. .receive = ab_receive,
  96. .transmit = ab_transmit,
  97. .port_leave = ab_port_leave,
  98. };
  99. static const struct team_mode ab_mode = {
  100. .kind = "activebackup",
  101. .owner = THIS_MODULE,
  102. .priv_size = sizeof(struct ab_priv),
  103. .ops = &ab_mode_ops,
  104. .lag_tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP,
  105. };
  106. static int __init ab_init_module(void)
  107. {
  108. return team_mode_register(&ab_mode);
  109. }
  110. static void __exit ab_cleanup_module(void)
  111. {
  112. team_mode_unregister(&ab_mode);
  113. }
  114. module_init(ab_init_module);
  115. module_exit(ab_cleanup_module);
  116. MODULE_LICENSE("GPL v2");
  117. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  118. MODULE_DESCRIPTION("Active-backup mode for team");
  119. MODULE_ALIAS_TEAM_MODE("activebackup");