Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / team / team_mode_activebackup.c
CommitLineData
3d249d4c
JP
1/*
2 * net/drivers/team/team_mode_activebackup.c - Active-backup 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 <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
43 active_port = rcu_dereference(ab_priv(team)->active_port);
44 if (unlikely(!active_port))
45 goto drop;
46 skb->dev = active_port->dev;
47 if (dev_queue_xmit(skb))
48 return false;
49 return true;
50
51drop:
52 dev_kfree_skb_any(skb);
53 return false;
54}
55
56static void ab_port_leave(struct team *team, struct team_port *port)
57{
58 if (ab_priv(team)->active_port == port)
2cfa5a04 59 RCU_INIT_POINTER(ab_priv(team)->active_port, NULL);
3d249d4c
JP
60}
61
80f7c668 62static int ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 63{
3d249d4c 64 if (ab_priv(team)->active_port)
80f7c668
JP
65 ctx->data.u32_val = ab_priv(team)->active_port->dev->ifindex;
66 else
67 ctx->data.u32_val = 0;
3d249d4c
JP
68 return 0;
69}
70
80f7c668 71static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 72{
3d249d4c
JP
73 struct team_port *port;
74
679b1607 75 list_for_each_entry(port, &team->port_list, list) {
80f7c668 76 if (port->dev->ifindex == ctx->data.u32_val) {
3d249d4c
JP
77 rcu_assign_pointer(ab_priv(team)->active_port, port);
78 return 0;
79 }
80 }
81 return -ENOENT;
82}
83
358b8382 84static const struct team_option ab_options[] = {
3d249d4c
JP
85 {
86 .name = "activeport",
87 .type = TEAM_OPTION_TYPE_U32,
88 .getter = ab_active_port_get,
89 .setter = ab_active_port_set,
90 },
91};
92
cade4555 93static int ab_init(struct team *team)
3d249d4c 94{
358b8382 95 return team_options_register(team, ab_options, ARRAY_SIZE(ab_options));
3d249d4c
JP
96}
97
cade4555 98static void ab_exit(struct team *team)
3d249d4c
JP
99{
100 team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options));
101}
102
103static const struct team_mode_ops ab_mode_ops = {
104 .init = ab_init,
105 .exit = ab_exit,
106 .receive = ab_receive,
107 .transmit = ab_transmit,
108 .port_leave = ab_port_leave,
109};
110
111static struct team_mode ab_mode = {
112 .kind = "activebackup",
113 .owner = THIS_MODULE,
114 .priv_size = sizeof(struct ab_priv),
115 .ops = &ab_mode_ops,
116};
117
118static int __init ab_init_module(void)
119{
120 return team_mode_register(&ab_mode);
121}
122
123static void __exit ab_cleanup_module(void)
124{
125 team_mode_unregister(&ab_mode);
126}
127
128module_init(ab_init_module);
129module_exit(ab_cleanup_module);
130
131MODULE_LICENSE("GPL v2");
132MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
133MODULE_DESCRIPTION("Active-backup mode for team");
134MODULE_ALIAS("team-mode-activebackup");