team_mode_random.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * drivers/net/team/team_mode_random.c - Random mode for team
  3. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  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/skbuff.h>
  15. #include <linux/reciprocal_div.h>
  16. #include <linux/if_team.h>
  17. static u32 random_N(unsigned int N)
  18. {
  19. return reciprocal_divide(prandom_u32(), N);
  20. }
  21. static bool rnd_transmit(struct team *team, struct sk_buff *skb)
  22. {
  23. struct team_port *port;
  24. int port_index;
  25. port_index = random_N(team->en_port_count);
  26. port = team_get_port_by_index_rcu(team, port_index);
  27. if (unlikely(!port))
  28. goto drop;
  29. port = team_get_first_port_txable_rcu(team, port);
  30. if (unlikely(!port))
  31. goto drop;
  32. if (team_dev_queue_xmit(team, port, skb))
  33. return false;
  34. return true;
  35. drop:
  36. dev_kfree_skb_any(skb);
  37. return false;
  38. }
  39. static const struct team_mode_ops rnd_mode_ops = {
  40. .transmit = rnd_transmit,
  41. .port_enter = team_modeop_port_enter,
  42. .port_change_dev_addr = team_modeop_port_change_dev_addr,
  43. };
  44. static const struct team_mode rnd_mode = {
  45. .kind = "random",
  46. .owner = THIS_MODULE,
  47. .ops = &rnd_mode_ops,
  48. };
  49. static int __init rnd_init_module(void)
  50. {
  51. return team_mode_register(&rnd_mode);
  52. }
  53. static void __exit rnd_cleanup_module(void)
  54. {
  55. team_mode_unregister(&rnd_mode);
  56. }
  57. module_init(rnd_init_module);
  58. module_exit(rnd_cleanup_module);
  59. MODULE_LICENSE("GPL v2");
  60. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  61. MODULE_DESCRIPTION("Random mode for team");
  62. MODULE_ALIAS("team-mode-random");