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 
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/if_team.h>
18 
19 struct rr_priv {
20 	unsigned int sent_packets;
21 };
22 
23 static struct rr_priv *rr_priv(struct team *team)
24 {
25 	return (struct rr_priv *) &team->mode_priv;
26 }
27 
28 static bool rr_transmit(struct team *team, struct sk_buff *skb)
29 {
30 	struct team_port *port;
31 	int port_index;
32 
33 	port_index = rr_priv(team)->sent_packets++ % team->en_port_count;
34 	port = team_get_port_by_index_rcu(team, port_index);
35 	port = team_get_first_port_txable_rcu(team, port);
36 	if (unlikely(!port))
37 		goto drop;
38 	if (team_dev_queue_xmit(team, port, skb))
39 		return false;
40 	return true;
41 
42 drop:
43 	dev_kfree_skb_any(skb);
44 	return false;
45 }
46 
47 static const struct team_mode_ops rr_mode_ops = {
48 	.transmit		= rr_transmit,
49 	.port_enter		= team_modeop_port_enter,
50 	.port_change_dev_addr	= team_modeop_port_change_dev_addr,
51 };
52 
53 static const struct team_mode rr_mode = {
54 	.kind		= "roundrobin",
55 	.owner		= THIS_MODULE,
56 	.priv_size	= sizeof(struct rr_priv),
57 	.ops		= &rr_mode_ops,
58 };
59 
60 static int __init rr_init_module(void)
61 {
62 	return team_mode_register(&rr_mode);
63 }
64 
65 static void __exit rr_cleanup_module(void)
66 {
67 	team_mode_unregister(&rr_mode);
68 }
69 
70 module_init(rr_init_module);
71 module_exit(rr_cleanup_module);
72 
73 MODULE_LICENSE("GPL v2");
74 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
75 MODULE_DESCRIPTION("Round-robin mode for team");
76 MODULE_ALIAS("team-mode-roundrobin");
77