team_mode_activebackup.c 3.3 KB

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