team_mode_activebackup.c 3.6 KB

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