Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / team / team_mode_roundrobin.c
bloba0e8f806331af591e2faa3e3d8926f58b5acb392
1 /*
2 * net/drivers/team/team_mode_roundrobin.c - Round-robin mode for team
3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
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 */
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>
19 struct rr_priv {
20 unsigned int sent_packets;
23 static struct rr_priv *rr_priv(struct team *team)
25 return (struct rr_priv *) &team->mode_priv;
28 static struct team_port *__get_first_port_up(struct team *team,
29 struct team_port *port)
31 struct team_port *cur;
33 if (port->linkup)
34 return port;
35 cur = port;
36 list_for_each_entry_continue_rcu(cur, &team->port_list, list)
37 if (cur->linkup)
38 return cur;
39 list_for_each_entry_rcu(cur, &team->port_list, list) {
40 if (cur == port)
41 break;
42 if (cur->linkup)
43 return cur;
45 return NULL;
48 static bool rr_transmit(struct team *team, struct sk_buff *skb)
50 struct team_port *port;
51 int port_index;
53 port_index = rr_priv(team)->sent_packets++ % team->port_count;
54 port = team_get_port_by_index_rcu(team, port_index);
55 port = __get_first_port_up(team, port);
56 if (unlikely(!port))
57 goto drop;
58 skb->dev = port->dev;
59 if (dev_queue_xmit(skb))
60 return false;
61 return true;
63 drop:
64 dev_kfree_skb_any(skb);
65 return false;
68 static int rr_port_enter(struct team *team, struct team_port *port)
70 return team_port_set_team_mac(port);
73 static void rr_port_change_mac(struct team *team, struct team_port *port)
75 team_port_set_team_mac(port);
78 static const struct team_mode_ops rr_mode_ops = {
79 .transmit = rr_transmit,
80 .port_enter = rr_port_enter,
81 .port_change_mac = rr_port_change_mac,
84 static struct team_mode rr_mode = {
85 .kind = "roundrobin",
86 .owner = THIS_MODULE,
87 .priv_size = sizeof(struct rr_priv),
88 .ops = &rr_mode_ops,
91 static int __init rr_init_module(void)
93 return team_mode_register(&rr_mode);
96 static void __exit rr_cleanup_module(void)
98 team_mode_unregister(&rr_mode);
101 module_init(rr_init_module);
102 module_exit(rr_cleanup_module);
104 MODULE_LICENSE("GPL v2");
105 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
106 MODULE_DESCRIPTION("Round-robin mode for team");
107 MODULE_ALIAS("team-mode-roundrobin");