team_mode_roundrobin.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * drivers/net/team/team_mode_roundrobin.c - Round-robin 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 <linux/if_team.h>
  17. struct rr_priv {
  18. unsigned int sent_packets;
  19. };
  20. static struct rr_priv *rr_priv(struct team *team)
  21. {
  22. return (struct rr_priv *) &team->mode_priv;
  23. }
  24. static struct team_port *__get_first_port_up(struct team *team,
  25. struct team_port *port)
  26. {
  27. struct team_port *cur;
  28. if (team_port_txable(port))
  29. return port;
  30. cur = port;
  31. list_for_each_entry_continue_rcu(cur, &team->port_list, list)
  32. if (team_port_txable(port))
  33. return cur;
  34. list_for_each_entry_rcu(cur, &team->port_list, list) {
  35. if (cur == port)
  36. break;
  37. if (team_port_txable(port))
  38. return cur;
  39. }
  40. return NULL;
  41. }
  42. static bool rr_transmit(struct team *team, struct sk_buff *skb)
  43. {
  44. struct team_port *port;
  45. int port_index;
  46. port_index = rr_priv(team)->sent_packets++ % team->en_port_count;
  47. port = team_get_port_by_index_rcu(team, port_index);
  48. port = __get_first_port_up(team, port);
  49. if (unlikely(!port))
  50. goto drop;
  51. if (team_dev_queue_xmit(team, port, skb))
  52. return false;
  53. return true;
  54. drop:
  55. dev_kfree_skb_any(skb);
  56. return false;
  57. }
  58. static int rr_port_enter(struct team *team, struct team_port *port)
  59. {
  60. return team_port_set_team_dev_addr(port);
  61. }
  62. static void rr_port_change_dev_addr(struct team *team, struct team_port *port)
  63. {
  64. team_port_set_team_dev_addr(port);
  65. }
  66. static const struct team_mode_ops rr_mode_ops = {
  67. .transmit = rr_transmit,
  68. .port_enter = rr_port_enter,
  69. .port_change_dev_addr = rr_port_change_dev_addr,
  70. };
  71. static const struct team_mode rr_mode = {
  72. .kind = "roundrobin",
  73. .owner = THIS_MODULE,
  74. .priv_size = sizeof(struct rr_priv),
  75. .ops = &rr_mode_ops,
  76. };
  77. static int __init rr_init_module(void)
  78. {
  79. return team_mode_register(&rr_mode);
  80. }
  81. static void __exit rr_cleanup_module(void)
  82. {
  83. team_mode_unregister(&rr_mode);
  84. }
  85. module_init(rr_init_module);
  86. module_exit(rr_cleanup_module);
  87. MODULE_LICENSE("GPL v2");
  88. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  89. MODULE_DESCRIPTION("Round-robin mode for team");
  90. MODULE_ALIAS("team-mode-roundrobin");