Jiri Pirko | 753f993 | 2013-03-06 01:31:13 +0000 | [diff] [blame] | 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 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/reciprocal_div.h> |
| 17 | #include <linux/if_team.h> |
| 18 | |
| 19 | static u32 random_N(unsigned int N) |
| 20 | { |
Akinobu Mita | e00adf3 | 2013-05-07 16:18:15 -0700 | [diff] [blame] | 21 | return reciprocal_divide(prandom_u32(), N); |
Jiri Pirko | 753f993 | 2013-03-06 01:31:13 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static bool rnd_transmit(struct team *team, struct sk_buff *skb) |
| 25 | { |
| 26 | struct team_port *port; |
| 27 | int port_index; |
| 28 | |
| 29 | port_index = random_N(team->en_port_count); |
| 30 | port = team_get_port_by_index_rcu(team, port_index); |
Jiri Pirko | 76c455d | 2013-06-08 15:00:53 +0200 | [diff] [blame] | 31 | if (unlikely(!port)) |
| 32 | goto drop; |
Jiri Pirko | 753f993 | 2013-03-06 01:31:13 +0000 | [diff] [blame] | 33 | port = team_get_first_port_txable_rcu(team, port); |
| 34 | if (unlikely(!port)) |
| 35 | goto drop; |
| 36 | if (team_dev_queue_xmit(team, port, skb)) |
| 37 | return false; |
| 38 | return true; |
| 39 | |
| 40 | drop: |
| 41 | dev_kfree_skb_any(skb); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | static const struct team_mode_ops rnd_mode_ops = { |
| 46 | .transmit = rnd_transmit, |
| 47 | .port_enter = team_modeop_port_enter, |
| 48 | .port_change_dev_addr = team_modeop_port_change_dev_addr, |
| 49 | }; |
| 50 | |
| 51 | static const struct team_mode rnd_mode = { |
| 52 | .kind = "random", |
| 53 | .owner = THIS_MODULE, |
| 54 | .ops = &rnd_mode_ops, |
| 55 | }; |
| 56 | |
| 57 | static int __init rnd_init_module(void) |
| 58 | { |
| 59 | return team_mode_register(&rnd_mode); |
| 60 | } |
| 61 | |
| 62 | static void __exit rnd_cleanup_module(void) |
| 63 | { |
| 64 | team_mode_unregister(&rnd_mode); |
| 65 | } |
| 66 | |
| 67 | module_init(rnd_init_module); |
| 68 | module_exit(rnd_cleanup_module); |
| 69 | |
| 70 | MODULE_LICENSE("GPL v2"); |
| 71 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 72 | MODULE_DESCRIPTION("Random mode for team"); |
| 73 | MODULE_ALIAS("team-mode-random"); |