team_mode_activebackup.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. };
  21. static struct ab_priv *ab_priv(struct team *team)
  22. {
  23. return (struct ab_priv *) &team->mode_priv;
  24. }
  25. static rx_handler_result_t ab_receive(struct team *team, struct team_port *port,
  26. struct sk_buff *skb) {
  27. struct team_port *active_port;
  28. active_port = rcu_dereference(ab_priv(team)->active_port);
  29. if (active_port != port)
  30. return RX_HANDLER_EXACT;
  31. return RX_HANDLER_ANOTHER;
  32. }
  33. static bool ab_transmit(struct team *team, struct sk_buff *skb)
  34. {
  35. struct team_port *active_port;
  36. active_port = rcu_dereference_bh(ab_priv(team)->active_port);
  37. if (unlikely(!active_port))
  38. goto drop;
  39. if (team_dev_queue_xmit(team, active_port, skb))
  40. return false;
  41. return true;
  42. drop:
  43. dev_kfree_skb_any(skb);
  44. return false;
  45. }
  46. static void ab_port_leave(struct team *team, struct team_port *port)
  47. {
  48. if (ab_priv(team)->active_port == port)
  49. RCU_INIT_POINTER(ab_priv(team)->active_port, NULL);
  50. }
  51. static int ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
  52. {
  53. struct team_port *active_port;
  54. active_port = rcu_dereference_protected(ab_priv(team)->active_port,
  55. lockdep_is_held(&team->lock));
  56. if (active_port)
  57. ctx->data.u32_val = active_port->dev->ifindex;
  58. else
  59. ctx->data.u32_val = 0;
  60. return 0;
  61. }
  62. static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
  63. {
  64. struct team_port *port;
  65. list_for_each_entry(port, &team->port_list, list) {
  66. if (port->dev->ifindex == ctx->data.u32_val) {
  67. rcu_assign_pointer(ab_priv(team)->active_port, port);
  68. return 0;
  69. }
  70. }
  71. return -ENOENT;
  72. }
  73. static const struct team_option ab_options[] = {
  74. {
  75. .name = "activeport",
  76. .type = TEAM_OPTION_TYPE_U32,
  77. .getter = ab_active_port_get,
  78. .setter = ab_active_port_set,
  79. },
  80. };
  81. static int ab_init(struct team *team)
  82. {
  83. return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
  84. }
  85. static void ab_exit(struct team *team)
  86. {
  87. team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
  88. }
  89. static const struct team_mode_ops ab_mode_ops = {
  90. .init = ab_init,
  91. .exit = ab_exit,
  92. .receive = ab_receive,
  93. .transmit = ab_transmit,
  94. .port_leave = ab_port_leave,
  95. };
  96. static const struct team_mode ab_mode = {
  97. .kind = "activebackup",
  98. .owner = THIS_MODULE,
  99. .priv_size = sizeof(struct ab_priv),
  100. .ops = &ab_mode_ops,
  101. };
  102. static int __init ab_init_module(void)
  103. {
  104. return team_mode_register(&ab_mode);
  105. }
  106. static void __exit ab_cleanup_module(void)
  107. {
  108. team_mode_unregister(&ab_mode);
  109. }
  110. module_init(ab_init_module);
  111. module_exit(ab_cleanup_module);
  112. MODULE_LICENSE("GPL v2");
  113. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  114. MODULE_DESCRIPTION("Active-backup mode for team");
  115. MODULE_ALIAS("team-mode-activebackup");