blob: 6262b4defd93ebdaac7cdd2f468d050e3b238b72 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
Jiri Pirko0d572e42012-06-19 05:54:09 +00002 * drivers/net/team/team_mode_activebackup.c - Active-backup mode for team
Jiri Pirko3d249d42011-11-11 22:16:48 +00003 * 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 <net/rtnetlink.h>
18#include <linux/if_team.h>
19
20struct ab_priv {
21 struct team_port __rcu *active_port;
22};
23
24static struct ab_priv *ab_priv(struct team *team)
25{
26 return (struct ab_priv *) &team->mode_priv;
27}
28
29static rx_handler_result_t ab_receive(struct team *team, struct team_port *port,
30 struct sk_buff *skb) {
31 struct team_port *active_port;
32
33 active_port = rcu_dereference(ab_priv(team)->active_port);
34 if (active_port != port)
35 return RX_HANDLER_EXACT;
36 return RX_HANDLER_ANOTHER;
37}
38
39static bool ab_transmit(struct team *team, struct sk_buff *skb)
40{
41 struct team_port *active_port;
42
Jiri Pirkod1904fb2012-06-19 05:54:21 +000043 active_port = rcu_dereference_bh(ab_priv(team)->active_port);
Jiri Pirko3d249d42011-11-11 22:16:48 +000044 if (unlikely(!active_port))
45 goto drop;
Jiri Pirkobd2d0832012-07-17 05:22:36 +000046 if (team_dev_queue_xmit(team, active_port, skb))
Jiri Pirko3d249d42011-11-11 22:16:48 +000047 return false;
48 return true;
49
50drop:
51 dev_kfree_skb_any(skb);
52 return false;
53}
54
55static void ab_port_leave(struct team *team, struct team_port *port)
56{
57 if (ab_priv(team)->active_port == port)
Eric Dumazet2cfa5a02011-11-23 07:09:32 +000058 RCU_INIT_POINTER(ab_priv(team)->active_port, NULL);
Jiri Pirko3d249d42011-11-11 22:16:48 +000059}
60
Jiri Pirko80f7c662012-04-10 05:15:42 +000061static int ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +000062{
Jiri Pirko6dab0152012-06-20 08:39:39 +000063 struct team_port *active_port;
64
65 active_port = rcu_dereference_protected(ab_priv(team)->active_port,
66 lockdep_is_held(&team->lock));
67 if (active_port)
68 ctx->data.u32_val = active_port->dev->ifindex;
Jiri Pirko80f7c662012-04-10 05:15:42 +000069 else
70 ctx->data.u32_val = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +000071 return 0;
72}
73
Jiri Pirko80f7c662012-04-10 05:15:42 +000074static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +000075{
Jiri Pirko3d249d42011-11-11 22:16:48 +000076 struct team_port *port;
77
Jiri Pirko679b1602012-04-10 05:15:45 +000078 list_for_each_entry(port, &team->port_list, list) {
Jiri Pirko80f7c662012-04-10 05:15:42 +000079 if (port->dev->ifindex == ctx->data.u32_val) {
Jiri Pirko3d249d42011-11-11 22:16:48 +000080 rcu_assign_pointer(ab_priv(team)->active_port, port);
81 return 0;
82 }
83 }
84 return -ENOENT;
85}
86
Jiri Pirko358b8382011-11-16 11:09:09 +000087static const struct team_option ab_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +000088 {
89 .name = "activeport",
90 .type = TEAM_OPTION_TYPE_U32,
91 .getter = ab_active_port_get,
92 .setter = ab_active_port_set,
93 },
94};
95
Jiri Pirkocade4552012-04-10 05:15:46 +000096static int ab_init(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +000097{
Jiri Pirko358b8382011-11-16 11:09:09 +000098 return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
Jiri Pirko3d249d42011-11-11 22:16:48 +000099}
100
Jiri Pirkocade4552012-04-10 05:15:46 +0000101static void ab_exit(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000102{
103 team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
104}
105
106static const struct team_mode_ops ab_mode_ops = {
107 .init = ab_init,
108 .exit = ab_exit,
109 .receive = ab_receive,
110 .transmit = ab_transmit,
111 .port_leave = ab_port_leave,
112};
113
Jiri Pirko04027882012-06-19 05:54:03 +0000114static const struct team_mode ab_mode = {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000115 .kind = "activebackup",
116 .owner = THIS_MODULE,
117 .priv_size = sizeof(struct ab_priv),
118 .ops = &ab_mode_ops,
119};
120
121static int __init ab_init_module(void)
122{
123 return team_mode_register(&ab_mode);
124}
125
126static void __exit ab_cleanup_module(void)
127{
128 team_mode_unregister(&ab_mode);
129}
130
131module_init(ab_init_module);
132module_exit(ab_cleanup_module);
133
134MODULE_LICENSE("GPL v2");
135MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
136MODULE_DESCRIPTION("Active-backup mode for team");
137MODULE_ALIAS("team-mode-activebackup");