Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * drivers/net/team/team_mode_roundrobin.c - Round-robin mode for team
4 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/netdevice.h>
12#include <linux/if_team.h>
13
14struct rr_priv {
15 unsigned int sent_packets;
16};
17
18static struct rr_priv *rr_priv(struct team *team)
19{
20 return (struct rr_priv *) &team->mode_priv;
21}
22
23static bool rr_transmit(struct team *team, struct sk_buff *skb)
24{
25 struct team_port *port;
26 int port_index;
27
28 port_index = team_num_to_port_index(team,
29 rr_priv(team)->sent_packets++);
30 port = team_get_port_by_index_rcu(team, port_index);
31 if (unlikely(!port))
32 goto drop;
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
40drop:
41 dev_kfree_skb_any(skb);
42 return false;
43}
44
45static const struct team_mode_ops rr_mode_ops = {
46 .transmit = rr_transmit,
47 .port_enter = team_modeop_port_enter,
48 .port_change_dev_addr = team_modeop_port_change_dev_addr,
49};
50
51static const struct team_mode rr_mode = {
52 .kind = "roundrobin",
53 .owner = THIS_MODULE,
54 .priv_size = sizeof(struct rr_priv),
55 .ops = &rr_mode_ops,
56 .lag_tx_type = NETDEV_LAG_TX_TYPE_ROUNDROBIN,
57};
58
59static int __init rr_init_module(void)
60{
61 return team_mode_register(&rr_mode);
62}
63
64static void __exit rr_cleanup_module(void)
65{
66 team_mode_unregister(&rr_mode);
67}
68
69module_init(rr_init_module);
70module_exit(rr_cleanup_module);
71
72MODULE_LICENSE("GPL v2");
73MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
74MODULE_DESCRIPTION("Round-robin mode for team");
75MODULE_ALIAS_TEAM_MODE("roundrobin");
1/*
2 * net/drivers/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
19struct rr_priv {
20 unsigned int sent_packets;
21};
22
23static struct rr_priv *rr_priv(struct team *team)
24{
25 return (struct rr_priv *) &team->mode_priv;
26}
27
28static struct team_port *__get_first_port_up(struct team *team,
29 struct team_port *port)
30{
31 struct team_port *cur;
32
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;
44 }
45 return NULL;
46}
47
48static bool rr_transmit(struct team *team, struct sk_buff *skb)
49{
50 struct team_port *port;
51 int port_index;
52
53 port_index = rr_priv(team)->sent_packets++ % team->en_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;
62
63drop:
64 dev_kfree_skb_any(skb);
65 return false;
66}
67
68static int rr_port_enter(struct team *team, struct team_port *port)
69{
70 return team_port_set_team_mac(port);
71}
72
73static void rr_port_change_mac(struct team *team, struct team_port *port)
74{
75 team_port_set_team_mac(port);
76}
77
78static 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,
82};
83
84static struct team_mode rr_mode = {
85 .kind = "roundrobin",
86 .owner = THIS_MODULE,
87 .priv_size = sizeof(struct rr_priv),
88 .ops = &rr_mode_ops,
89};
90
91static int __init rr_init_module(void)
92{
93 return team_mode_register(&rr_mode);
94}
95
96static void __exit rr_cleanup_module(void)
97{
98 team_mode_unregister(&rr_mode);
99}
100
101module_init(rr_init_module);
102module_exit(rr_cleanup_module);
103
104MODULE_LICENSE("GPL v2");
105MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
106MODULE_DESCRIPTION("Round-robin mode for team");
107MODULE_ALIAS("team-mode-roundrobin");