1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/net/team/team_mode_activebackup.c - Active-backup 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/errno.h>
12 #include <linux/netdevice.h>
13 #include <net/rtnetlink.h>
14 #include <linux/if_team.h>
15
16 struct ab_priv {
17 struct team_port __rcu *active_port;
18 struct team_option_inst_info *ap_opt_inst_info;
19 };
20
ab_priv(struct team * team)21 static struct ab_priv *ab_priv(struct team *team)
22 {
23 return (struct ab_priv *) &team->mode_priv;
24 }
25
ab_receive(struct team * team,struct team_port * port,struct sk_buff * skb)26 static rx_handler_result_t ab_receive(struct team *team, struct team_port *port,
27 struct sk_buff *skb) {
28 struct team_port *active_port;
29
30 active_port = rcu_dereference(ab_priv(team)->active_port);
31 if (active_port != port)
32 return RX_HANDLER_EXACT;
33 return RX_HANDLER_ANOTHER;
34 }
35
ab_transmit(struct team * team,struct sk_buff * skb)36 static bool ab_transmit(struct team *team, struct sk_buff *skb)
37 {
38 struct team_port *active_port;
39
40 active_port = rcu_dereference_bh(ab_priv(team)->active_port);
41 if (unlikely(!active_port))
42 goto drop;
43 if (team_dev_queue_xmit(team, active_port, skb))
44 return false;
45 return true;
46
47 drop:
48 dev_kfree_skb_any(skb);
49 return false;
50 }
51
ab_port_leave(struct team * team,struct team_port * port)52 static void ab_port_leave(struct team *team, struct team_port *port)
53 {
54 if (ab_priv(team)->active_port == port) {
55 RCU_INIT_POINTER(ab_priv(team)->active_port, NULL);
56 team_option_inst_set_change(ab_priv(team)->ap_opt_inst_info);
57 }
58 }
59
ab_active_port_init(struct team * team,struct team_option_inst_info * info)60 static void ab_active_port_init(struct team *team,
61 struct team_option_inst_info *info)
62 {
63 ab_priv(team)->ap_opt_inst_info = info;
64 }
65
ab_active_port_get(struct team * team,struct team_gsetter_ctx * ctx)66 static void ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
67 {
68 struct team_port *active_port;
69
70 active_port = rtnl_dereference(ab_priv(team)->active_port);
71 if (active_port)
72 ctx->data.u32_val = active_port->dev->ifindex;
73 else
74 ctx->data.u32_val = 0;
75 }
76
ab_active_port_set(struct team * team,struct team_gsetter_ctx * ctx)77 static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
78 {
79 struct team_port *port;
80
81 list_for_each_entry(port, &team->port_list, list) {
82 if (port->dev->ifindex == ctx->data.u32_val) {
83 rcu_assign_pointer(ab_priv(team)->active_port, port);
84 return 0;
85 }
86 }
87 return -ENOENT;
88 }
89
90 static const struct team_option ab_options[] = {
91 {
92 .name = "activeport",
93 .type = TEAM_OPTION_TYPE_U32,
94 .init = ab_active_port_init,
95 .getter = ab_active_port_get,
96 .setter = ab_active_port_set,
97 },
98 };
99
ab_init(struct team * team)100 static int ab_init(struct team *team)
101 {
102 return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
103 }
104
ab_exit(struct team * team)105 static void ab_exit(struct team *team)
106 {
107 team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
108 }
109
110 static const struct team_mode_ops ab_mode_ops = {
111 .init = ab_init,
112 .exit = ab_exit,
113 .receive = ab_receive,
114 .transmit = ab_transmit,
115 .port_leave = ab_port_leave,
116 };
117
118 static const struct team_mode ab_mode = {
119 .kind = "activebackup",
120 .owner = THIS_MODULE,
121 .priv_size = sizeof(struct ab_priv),
122 .ops = &ab_mode_ops,
123 .lag_tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP,
124 };
125
ab_init_module(void)126 static int __init ab_init_module(void)
127 {
128 return team_mode_register(&ab_mode);
129 }
130
ab_cleanup_module(void)131 static void __exit ab_cleanup_module(void)
132 {
133 team_mode_unregister(&ab_mode);
134 }
135
136 module_init(ab_init_module);
137 module_exit(ab_cleanup_module);
138
139 MODULE_LICENSE("GPL v2");
140 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
141 MODULE_DESCRIPTION("Active-backup mode for team");
142 MODULE_ALIAS_TEAM_MODE("activebackup");
143