team_mode_random.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/team/team_mode_random.c - Random mode for team
  4. * Copyright (c) 2013 Jiri Pirko <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/if_team.h>
  12. static bool rnd_transmit(struct team *team, struct sk_buff *skb)
  13. {
  14. struct team_port *port;
  15. int port_index;
  16. port_index = prandom_u32_max(team->en_port_count);
  17. port = team_get_port_by_index_rcu(team, port_index);
  18. if (unlikely(!port))
  19. goto drop;
  20. port = team_get_first_port_txable_rcu(team, port);
  21. if (unlikely(!port))
  22. goto drop;
  23. if (team_dev_queue_xmit(team, port, skb))
  24. return false;
  25. return true;
  26. drop:
  27. dev_kfree_skb_any(skb);
  28. return false;
  29. }
  30. static const struct team_mode_ops rnd_mode_ops = {
  31. .transmit = rnd_transmit,
  32. .port_enter = team_modeop_port_enter,
  33. .port_change_dev_addr = team_modeop_port_change_dev_addr,
  34. };
  35. static const struct team_mode rnd_mode = {
  36. .kind = "random",
  37. .owner = THIS_MODULE,
  38. .ops = &rnd_mode_ops,
  39. .lag_tx_type = NETDEV_LAG_TX_TYPE_RANDOM,
  40. };
  41. static int __init rnd_init_module(void)
  42. {
  43. return team_mode_register(&rnd_mode);
  44. }
  45. static void __exit rnd_cleanup_module(void)
  46. {
  47. team_mode_unregister(&rnd_mode);
  48. }
  49. module_init(rnd_init_module);
  50. module_exit(rnd_cleanup_module);
  51. MODULE_LICENSE("GPL v2");
  52. MODULE_AUTHOR("Jiri Pirko <[email protected]>");
  53. MODULE_DESCRIPTION("Random mode for team");
  54. MODULE_ALIAS_TEAM_MODE("random");