Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 1 | /* |
Jiri Pirko | 0d572e4 | 2012-06-19 05:54:09 +0000 | [diff] [blame] | 2 | * drivers/net/team/team_mode_roundrobin.c - Round-robin mode for team |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 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 struct team_port *__get_first_port_up(struct team *team, |
| 29 | struct team_port *port) |
| 30 | { |
| 31 | struct team_port *cur; |
| 32 | |
Jiri Pirko | 6e88e13 | 2012-07-11 05:34:02 +0000 | [diff] [blame] | 33 | if (team_port_txable(port)) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 34 | return port; |
| 35 | cur = port; |
| 36 | list_for_each_entry_continue_rcu(cur, &team->port_list, list) |
Jiri Pirko | 6e88e13 | 2012-07-11 05:34:02 +0000 | [diff] [blame] | 37 | if (team_port_txable(port)) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 38 | return cur; |
| 39 | list_for_each_entry_rcu(cur, &team->port_list, list) { |
| 40 | if (cur == port) |
| 41 | break; |
Jiri Pirko | 6e88e13 | 2012-07-11 05:34:02 +0000 | [diff] [blame] | 42 | if (team_port_txable(port)) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 43 | return cur; |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | static bool rr_transmit(struct team *team, struct sk_buff *skb) |
| 49 | { |
| 50 | struct team_port *port; |
| 51 | int port_index; |
| 52 | |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 53 | port_index = rr_priv(team)->sent_packets++ % team->en_port_count; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 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; |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame] | 58 | if (team_dev_queue_xmit(team, port, skb)) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 59 | return false; |
| 60 | return true; |
| 61 | |
| 62 | drop: |
| 63 | dev_kfree_skb_any(skb); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | static int rr_port_enter(struct team *team, struct team_port *port) |
| 68 | { |
Jiri Pirko | 1d76efe | 2012-08-17 04:00:48 +0000 | [diff] [blame] | 69 | return team_port_set_team_dev_addr(port); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jiri Pirko | 1d76efe | 2012-08-17 04:00:48 +0000 | [diff] [blame] | 72 | static void rr_port_change_dev_addr(struct team *team, struct team_port *port) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 73 | { |
Jiri Pirko | 1d76efe | 2012-08-17 04:00:48 +0000 | [diff] [blame] | 74 | team_port_set_team_dev_addr(port); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static const struct team_mode_ops rr_mode_ops = { |
| 78 | .transmit = rr_transmit, |
| 79 | .port_enter = rr_port_enter, |
Jiri Pirko | 1d76efe | 2012-08-17 04:00:48 +0000 | [diff] [blame] | 80 | .port_change_dev_addr = rr_port_change_dev_addr, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 83 | static const struct team_mode rr_mode = { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 84 | .kind = "roundrobin", |
| 85 | .owner = THIS_MODULE, |
| 86 | .priv_size = sizeof(struct rr_priv), |
| 87 | .ops = &rr_mode_ops, |
| 88 | }; |
| 89 | |
| 90 | static int __init rr_init_module(void) |
| 91 | { |
| 92 | return team_mode_register(&rr_mode); |
| 93 | } |
| 94 | |
| 95 | static void __exit rr_cleanup_module(void) |
| 96 | { |
| 97 | team_mode_unregister(&rr_mode); |
| 98 | } |
| 99 | |
| 100 | module_init(rr_init_module); |
| 101 | module_exit(rr_cleanup_module); |
| 102 | |
| 103 | MODULE_LICENSE("GPL v2"); |
| 104 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); |
| 105 | MODULE_DESCRIPTION("Round-robin mode for team"); |
| 106 | MODULE_ALIAS("team-mode-roundrobin"); |