blob: 341b65dbbcd324d9512f957fc7b663481a073aea [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
Jiri Pirko0d572e42012-06-19 05:54:09 +00002 * drivers/net/team/team.c - Network team device driver
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/slab.h>
16#include <linux/rcupdate.h>
17#include <linux/errno.h>
18#include <linux/ctype.h>
19#include <linux/notifier.h>
20#include <linux/netdevice.h>
Jiri Pirkobd2d0832012-07-17 05:22:36 +000021#include <linux/netpoll.h>
Jiri Pirko87002b02011-12-08 04:11:17 +000022#include <linux/if_vlan.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000023#include <linux/if_arp.h>
24#include <linux/socket.h>
25#include <linux/etherdevice.h>
26#include <linux/rtnetlink.h>
27#include <net/rtnetlink.h>
28#include <net/genetlink.h>
29#include <net/netlink.h>
Jiri Pirko6c85f2b2012-07-20 02:28:51 +000030#include <net/sch_generic.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000031#include <linux/if_team.h>
32
33#define DRV_NAME "team"
34
35
36/**********
37 * Helpers
38 **********/
39
40#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
41
42static struct team_port *team_port_get_rcu(const struct net_device *dev)
43{
44 struct team_port *port = rcu_dereference(dev->rx_handler_data);
45
46 return team_port_exists(dev) ? port : NULL;
47}
48
49static struct team_port *team_port_get_rtnl(const struct net_device *dev)
50{
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
52
53 return team_port_exists(dev) ? port : NULL;
54}
55
56/*
57 * Since the ability to change mac address for open port device is tested in
58 * team_port_add, this function can be called without control of return value
59 */
60static int __set_port_mac(struct net_device *port_dev,
61 const unsigned char *dev_addr)
62{
63 struct sockaddr addr;
64
65 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
66 addr.sa_family = ARPHRD_ETHER;
67 return dev_set_mac_address(port_dev, &addr);
68}
69
Jiri Pirkocade4552012-04-10 05:15:46 +000070static int team_port_set_orig_mac(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000071{
72 return __set_port_mac(port->dev, port->orig.dev_addr);
73}
74
75int team_port_set_team_mac(struct team_port *port)
76{
77 return __set_port_mac(port->dev, port->team->dev->dev_addr);
78}
79EXPORT_SYMBOL(team_port_set_team_mac);
80
Jiri Pirko71472ec2012-04-10 05:15:44 +000081static void team_refresh_port_linkup(struct team_port *port)
82{
83 port->linkup = port->user.linkup_enabled ? port->user.linkup :
84 port->state.linkup;
85}
Jiri Pirko3d249d42011-11-11 22:16:48 +000086
Jiri Pirko0f1aad22012-06-19 05:54:11 +000087
Jiri Pirko3d249d42011-11-11 22:16:48 +000088/*******************
89 * Options handling
90 *******************/
91
Jiri Pirko80f7c662012-04-10 05:15:42 +000092struct team_option_inst { /* One for each option instance */
93 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +000094 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +000095 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +000096 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +000097 bool changed;
98 bool removed;
99};
100
101static struct team_option *__team_find_option(struct team *team,
102 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000103{
104 struct team_option *option;
105
106 list_for_each_entry(option, &team->option_list, list) {
107 if (strcmp(option->name, opt_name) == 0)
108 return option;
109 }
110 return NULL;
111}
112
Jiri Pirko80f7c662012-04-10 05:15:42 +0000113static void __team_option_inst_del(struct team_option_inst *opt_inst)
114{
115 list_del(&opt_inst->list);
116 kfree(opt_inst);
117}
118
119static void __team_option_inst_del_option(struct team *team,
120 struct team_option *option)
121{
122 struct team_option_inst *opt_inst, *tmp;
123
124 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
125 if (opt_inst->option == option)
126 __team_option_inst_del(opt_inst);
127 }
128}
129
Jiri Pirkob1303322012-06-19 05:54:08 +0000130static int __team_option_inst_add(struct team *team, struct team_option *option,
131 struct team_port *port)
132{
133 struct team_option_inst *opt_inst;
134 unsigned int array_size;
135 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000136 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000137
138 array_size = option->array_size;
139 if (!array_size)
140 array_size = 1; /* No array but still need one instance */
141
142 for (i = 0; i < array_size; i++) {
143 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
144 if (!opt_inst)
145 return -ENOMEM;
146 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000147 opt_inst->info.port = port;
148 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000149 opt_inst->changed = true;
150 opt_inst->removed = false;
151 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000152 if (option->init) {
153 err = option->init(team, &opt_inst->info);
154 if (err)
155 return err;
156 }
157
Jiri Pirkob1303322012-06-19 05:54:08 +0000158 }
159 return 0;
160}
161
Jiri Pirko80f7c662012-04-10 05:15:42 +0000162static int __team_option_inst_add_option(struct team *team,
163 struct team_option *option)
164{
165 struct team_port *port;
166 int err;
167
Jiri Pirkob1303322012-06-19 05:54:08 +0000168 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000169 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000170 if (err)
171 goto inst_del_option;
172 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000173
174 list_for_each_entry(port, &team->port_list, list) {
175 err = __team_option_inst_add(team, option, port);
176 if (err)
177 goto inst_del_option;
178 }
179 return 0;
180
181inst_del_option:
182 __team_option_inst_del_option(team, option);
183 return err;
184}
185
186static void __team_option_inst_mark_removed_option(struct team *team,
187 struct team_option *option)
188{
189 struct team_option_inst *opt_inst;
190
191 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
192 if (opt_inst->option == option) {
193 opt_inst->changed = true;
194 opt_inst->removed = true;
195 }
196 }
197}
198
199static void __team_option_inst_del_port(struct team *team,
200 struct team_port *port)
201{
202 struct team_option_inst *opt_inst, *tmp;
203
204 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
205 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000206 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000207 __team_option_inst_del(opt_inst);
208 }
209}
210
211static int __team_option_inst_add_port(struct team *team,
212 struct team_port *port)
213{
214 struct team_option *option;
215 int err;
216
217 list_for_each_entry(option, &team->option_list, list) {
218 if (!option->per_port)
219 continue;
220 err = __team_option_inst_add(team, option, port);
221 if (err)
222 goto inst_del_port;
223 }
224 return 0;
225
226inst_del_port:
227 __team_option_inst_del_port(team, port);
228 return err;
229}
230
231static void __team_option_inst_mark_removed_port(struct team *team,
232 struct team_port *port)
233{
234 struct team_option_inst *opt_inst;
235
236 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000237 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000238 opt_inst->changed = true;
239 opt_inst->removed = true;
240 }
241 }
242}
243
244static int __team_options_register(struct team *team,
245 const struct team_option *option,
246 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000247{
248 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000249 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000250 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000251
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000252 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
253 GFP_KERNEL);
254 if (!dst_opts)
255 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000256 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000257 if (__team_find_option(team, option->name)) {
258 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000259 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000260 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000261 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
262 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000263 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000264 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000265 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000266 }
267
Jiri Pirkob82b9182012-01-24 05:16:00 +0000268 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000269 err = __team_option_inst_add_option(team, dst_opts[i]);
270 if (err)
271 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000272 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000273 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000274
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000275 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000276 return 0;
277
Jiri Pirko80f7c662012-04-10 05:15:42 +0000278inst_rollback:
279 for (i--; i >= 0; i--)
280 __team_option_inst_del_option(team, dst_opts[i]);
281
282 i = option_count - 1;
283alloc_rollback:
284 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000285 kfree(dst_opts[i]);
286
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000287 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000288 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000289}
Jiri Pirko358b8382011-11-16 11:09:09 +0000290
Jiri Pirkob82b9182012-01-24 05:16:00 +0000291static void __team_options_mark_removed(struct team *team,
292 const struct team_option *option,
293 size_t option_count)
294{
295 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000296
Jiri Pirkob82b9182012-01-24 05:16:00 +0000297 for (i = 0; i < option_count; i++, option++) {
298 struct team_option *del_opt;
299
300 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000301 if (del_opt)
302 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000303 }
304}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000305
306static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000307 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000308 size_t option_count)
309{
310 int i;
311
Jiri Pirko358b8382011-11-16 11:09:09 +0000312 for (i = 0; i < option_count; i++, option++) {
313 struct team_option *del_opt;
314
315 del_opt = __team_find_option(team, option->name);
316 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000317 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000318 list_del(&del_opt->list);
319 kfree(del_opt);
320 }
321 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000322}
323
Jiri Pirkob82b9182012-01-24 05:16:00 +0000324static void __team_options_change_check(struct team *team);
325
326int team_options_register(struct team *team,
327 const struct team_option *option,
328 size_t option_count)
329{
330 int err;
331
332 err = __team_options_register(team, option, option_count);
333 if (err)
334 return err;
335 __team_options_change_check(team);
336 return 0;
337}
338EXPORT_SYMBOL(team_options_register);
339
Jiri Pirko358b8382011-11-16 11:09:09 +0000340void team_options_unregister(struct team *team,
341 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000342 size_t option_count)
343{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000344 __team_options_mark_removed(team, option, option_count);
345 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000346 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000347}
348EXPORT_SYMBOL(team_options_unregister);
349
Jiri Pirko80f7c662012-04-10 05:15:42 +0000350static int team_option_get(struct team *team,
351 struct team_option_inst *opt_inst,
352 struct team_gsetter_ctx *ctx)
353{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000354 if (!opt_inst->option->getter)
355 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000356 return opt_inst->option->getter(team, ctx);
357}
358
359static int team_option_set(struct team *team,
360 struct team_option_inst *opt_inst,
361 struct team_gsetter_ctx *ctx)
362{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000363 if (!opt_inst->option->setter)
364 return -EOPNOTSUPP;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +0000365 return opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000366}
367
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000368void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
369{
370 struct team_option_inst *opt_inst;
371
372 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
373 opt_inst->changed = true;
374}
375EXPORT_SYMBOL(team_option_inst_set_change);
376
377void team_options_change_check(struct team *team)
378{
379 __team_options_change_check(team);
380}
381EXPORT_SYMBOL(team_options_change_check);
382
383
Jiri Pirko3d249d42011-11-11 22:16:48 +0000384/****************
385 * Mode handling
386 ****************/
387
388static LIST_HEAD(mode_list);
389static DEFINE_SPINLOCK(mode_list_lock);
390
Jiri Pirko04027882012-06-19 05:54:03 +0000391struct team_mode_item {
392 struct list_head list;
393 const struct team_mode *mode;
394};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000395
Jiri Pirko04027882012-06-19 05:54:03 +0000396static struct team_mode_item *__find_mode(const char *kind)
397{
398 struct team_mode_item *mitem;
399
400 list_for_each_entry(mitem, &mode_list, list) {
401 if (strcmp(mitem->mode->kind, kind) == 0)
402 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000403 }
404 return NULL;
405}
406
407static bool is_good_mode_name(const char *name)
408{
409 while (*name != '\0') {
410 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
411 return false;
412 name++;
413 }
414 return true;
415}
416
Jiri Pirko04027882012-06-19 05:54:03 +0000417int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000418{
419 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000420 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000421
422 if (!is_good_mode_name(mode->kind) ||
423 mode->priv_size > TEAM_MODE_PRIV_SIZE)
424 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000425
426 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
427 if (!mitem)
428 return -ENOMEM;
429
Jiri Pirko3d249d42011-11-11 22:16:48 +0000430 spin_lock(&mode_list_lock);
431 if (__find_mode(mode->kind)) {
432 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000433 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000434 goto unlock;
435 }
Jiri Pirko04027882012-06-19 05:54:03 +0000436 mitem->mode = mode;
437 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000438unlock:
439 spin_unlock(&mode_list_lock);
440 return err;
441}
442EXPORT_SYMBOL(team_mode_register);
443
Jiri Pirko04027882012-06-19 05:54:03 +0000444void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000445{
Jiri Pirko04027882012-06-19 05:54:03 +0000446 struct team_mode_item *mitem;
447
Jiri Pirko3d249d42011-11-11 22:16:48 +0000448 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000449 mitem = __find_mode(mode->kind);
450 if (mitem) {
451 list_del_init(&mitem->list);
452 kfree(mitem);
453 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000454 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000455}
456EXPORT_SYMBOL(team_mode_unregister);
457
Jiri Pirko04027882012-06-19 05:54:03 +0000458static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000459{
Jiri Pirko04027882012-06-19 05:54:03 +0000460 struct team_mode_item *mitem;
461 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000462
463 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000464 mitem = __find_mode(kind);
465 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000466 spin_unlock(&mode_list_lock);
467 request_module("team-mode-%s", kind);
468 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000469 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000470 }
Jiri Pirko04027882012-06-19 05:54:03 +0000471 if (mitem) {
472 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000473 if (!try_module_get(mode->owner))
474 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000475 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000476
477 spin_unlock(&mode_list_lock);
478 return mode;
479}
480
481static void team_mode_put(const struct team_mode *mode)
482{
483 module_put(mode->owner);
484}
485
486static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
487{
488 dev_kfree_skb_any(skb);
489 return false;
490}
491
492rx_handler_result_t team_dummy_receive(struct team *team,
493 struct team_port *port,
494 struct sk_buff *skb)
495{
496 return RX_HANDLER_ANOTHER;
497}
498
Jiri Pirkod299cd52012-06-19 05:54:04 +0000499static const struct team_mode __team_no_mode = {
500 .kind = "*NOMODE*",
501};
502
503static bool team_is_mode_set(struct team *team)
504{
505 return team->mode != &__team_no_mode;
506}
507
508static void team_set_no_mode(struct team *team)
509{
510 team->mode = &__team_no_mode;
511}
512
Jiri Pirko122bb042012-06-26 06:52:45 +0000513static void __team_adjust_ops(struct team *team, int en_port_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000514{
515 /*
516 * To avoid checks in rx/tx skb paths, ensure here that non-null and
517 * correct ops are always set.
518 */
519
Jiri Pirko122bb042012-06-26 06:52:45 +0000520 if (!en_port_count || !team_is_mode_set(team) ||
521 !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000522 team->ops.transmit = team_dummy_transmit;
523 else
524 team->ops.transmit = team->mode->ops->transmit;
525
Jiri Pirko122bb042012-06-26 06:52:45 +0000526 if (!en_port_count || !team_is_mode_set(team) ||
527 !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000528 team->ops.receive = team_dummy_receive;
529 else
530 team->ops.receive = team->mode->ops->receive;
531}
532
Jiri Pirko122bb042012-06-26 06:52:45 +0000533static void team_adjust_ops(struct team *team)
534{
535 __team_adjust_ops(team, team->en_port_count);
536}
537
Jiri Pirko3d249d42011-11-11 22:16:48 +0000538/*
539 * We can benefit from the fact that it's ensured no port is present
540 * at the time of mode change. Therefore no packets are in fly so there's no
541 * need to set mode operations in any special way.
542 */
543static int __team_change_mode(struct team *team,
544 const struct team_mode *new_mode)
545{
546 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000547 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000548 void (*exit_op)(struct team *team) = team->ops.exit;
549
550 /* Clear ops area so no callback is called any longer */
551 memset(&team->ops, 0, sizeof(struct team_mode_ops));
552 team_adjust_ops(team);
553
554 if (exit_op)
555 exit_op(team);
556 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000557 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000558 /* zero private data area */
559 memset(&team->mode_priv, 0,
560 sizeof(struct team) - offsetof(struct team, mode_priv));
561 }
562
563 if (!new_mode)
564 return 0;
565
566 if (new_mode->ops->init) {
567 int err;
568
569 err = new_mode->ops->init(team);
570 if (err)
571 return err;
572 }
573
574 team->mode = new_mode;
575 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
576 team_adjust_ops(team);
577
578 return 0;
579}
580
581static int team_change_mode(struct team *team, const char *kind)
582{
Jiri Pirko04027882012-06-19 05:54:03 +0000583 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000584 struct net_device *dev = team->dev;
585 int err;
586
587 if (!list_empty(&team->port_list)) {
588 netdev_err(dev, "No ports can be present during mode change\n");
589 return -EBUSY;
590 }
591
Jiri Pirkod299cd52012-06-19 05:54:04 +0000592 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000593 netdev_err(dev, "Unable to change to the same mode the team is in\n");
594 return -EINVAL;
595 }
596
597 new_mode = team_mode_get(kind);
598 if (!new_mode) {
599 netdev_err(dev, "Mode \"%s\" not found\n", kind);
600 return -EINVAL;
601 }
602
603 err = __team_change_mode(team, new_mode);
604 if (err) {
605 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
606 team_mode_put(new_mode);
607 return err;
608 }
609
610 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
611 return 0;
612}
613
614
615/************************
616 * Rx path frame handler
617 ************************/
618
619/* note: already called with rcu_read_lock */
620static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
621{
622 struct sk_buff *skb = *pskb;
623 struct team_port *port;
624 struct team *team;
625 rx_handler_result_t res;
626
627 skb = skb_share_check(skb, GFP_ATOMIC);
628 if (!skb)
629 return RX_HANDLER_CONSUMED;
630
631 *pskb = skb;
632
633 port = team_port_get_rcu(skb->dev);
634 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000635 if (!team_port_enabled(port)) {
636 /* allow exact match delivery for disabled ports */
637 res = RX_HANDLER_EXACT;
638 } else {
639 res = team->ops.receive(team, port, skb);
640 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000641 if (res == RX_HANDLER_ANOTHER) {
642 struct team_pcpu_stats *pcpu_stats;
643
644 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
645 u64_stats_update_begin(&pcpu_stats->syncp);
646 pcpu_stats->rx_packets++;
647 pcpu_stats->rx_bytes += skb->len;
648 if (skb->pkt_type == PACKET_MULTICAST)
649 pcpu_stats->rx_multicast++;
650 u64_stats_update_end(&pcpu_stats->syncp);
651
652 skb->dev = team->dev;
653 } else {
654 this_cpu_inc(team->pcpu_stats->rx_dropped);
655 }
656
657 return res;
658}
659
660
661/****************
662 * Port handling
663 ****************/
664
665static bool team_port_find(const struct team *team,
666 const struct team_port *port)
667{
668 struct team_port *cur;
669
670 list_for_each_entry(cur, &team->port_list, list)
671 if (cur == port)
672 return true;
673 return false;
674}
675
Jiri Pirko19a0b582012-04-20 04:42:05 +0000676/*
677 * Enable/disable port by adding to enabled port hashlist and setting
678 * port->index (Might be racy so reader could see incorrect ifindex when
679 * processing a flying packet, but that is not a problem). Write guarded
680 * by team->lock.
681 */
682static void team_port_enable(struct team *team,
683 struct team_port *port)
684{
685 if (team_port_enabled(port))
686 return;
687 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000688 hlist_add_head_rcu(&port->hlist,
689 team_port_index_hash(team, port->index));
Jiri Pirko122bb042012-06-26 06:52:45 +0000690 team_adjust_ops(team);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000691 if (team->ops.port_enabled)
692 team->ops.port_enabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000693}
694
695static void __reconstruct_port_hlist(struct team *team, int rm_index)
696{
697 int i;
698 struct team_port *port;
699
Jiri Pirko19a0b582012-04-20 04:42:05 +0000700 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000701 port = team_get_port_by_index(team, i);
702 hlist_del_rcu(&port->hlist);
703 port->index--;
704 hlist_add_head_rcu(&port->hlist,
705 team_port_index_hash(team, port->index));
706 }
707}
708
Jiri Pirko19a0b582012-04-20 04:42:05 +0000709static void team_port_disable(struct team *team,
710 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000711{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000712 if (!team_port_enabled(port))
713 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000714 if (team->ops.port_disabled)
715 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000716 hlist_del_rcu(&port->hlist);
Jiri Pirko122bb042012-06-26 06:52:45 +0000717 __reconstruct_port_hlist(team, port->index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000718 port->index = -1;
Jiri Pirko122bb042012-06-26 06:52:45 +0000719 __team_adjust_ops(team, team->en_port_count - 1);
720 /*
721 * Wait until readers see adjusted ops. This ensures that
722 * readers never see team->en_port_count == 0
723 */
724 synchronize_rcu();
725 team->en_port_count--;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000726}
727
728#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
729 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
730 NETIF_F_HIGHDMA | NETIF_F_LRO)
731
732static void __team_compute_features(struct team *team)
733{
734 struct team_port *port;
735 u32 vlan_features = TEAM_VLAN_FEATURES;
736 unsigned short max_hard_header_len = ETH_HLEN;
Jiri Pirkodc905952012-07-18 07:39:38 +0000737 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000738
739 list_for_each_entry(port, &team->port_list, list) {
740 vlan_features = netdev_increment_features(vlan_features,
741 port->dev->vlan_features,
742 TEAM_VLAN_FEATURES);
743
Jiri Pirkodc905952012-07-18 07:39:38 +0000744 dst_release_flag &= port->dev->priv_flags;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000745 if (port->dev->hard_header_len > max_hard_header_len)
746 max_hard_header_len = port->dev->hard_header_len;
747 }
748
749 team->dev->vlan_features = vlan_features;
750 team->dev->hard_header_len = max_hard_header_len;
751
Jiri Pirkodc905952012-07-18 07:39:38 +0000752 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
753 team->dev->priv_flags = flags | dst_release_flag;
754
Jiri Pirko3d249d42011-11-11 22:16:48 +0000755 netdev_change_features(team->dev);
756}
757
758static void team_compute_features(struct team *team)
759{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000760 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000761 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000762 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000763}
764
765static int team_port_enter(struct team *team, struct team_port *port)
766{
767 int err = 0;
768
769 dev_hold(team->dev);
770 port->dev->priv_flags |= IFF_TEAM_PORT;
771 if (team->ops.port_enter) {
772 err = team->ops.port_enter(team, port);
773 if (err) {
774 netdev_err(team->dev, "Device %s failed to enter team mode\n",
775 port->dev->name);
776 goto err_port_enter;
777 }
778 }
779
780 return 0;
781
782err_port_enter:
783 port->dev->priv_flags &= ~IFF_TEAM_PORT;
784 dev_put(team->dev);
785
786 return err;
787}
788
789static void team_port_leave(struct team *team, struct team_port *port)
790{
791 if (team->ops.port_leave)
792 team->ops.port_leave(team, port);
793 port->dev->priv_flags &= ~IFF_TEAM_PORT;
794 dev_put(team->dev);
795}
796
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000797#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang47be03a22012-08-10 01:24:37 +0000798static int team_port_enable_netpoll(struct team *team, struct team_port *port,
799 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000800{
801 struct netpoll *np;
802 int err;
803
Amerigo Wang47be03a22012-08-10 01:24:37 +0000804 np = kzalloc(sizeof(*np), gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000805 if (!np)
806 return -ENOMEM;
807
Amerigo Wang47be03a22012-08-10 01:24:37 +0000808 err = __netpoll_setup(np, port->dev, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000809 if (err) {
810 kfree(np);
811 return err;
812 }
813 port->np = np;
814 return err;
815}
816
817static void team_port_disable_netpoll(struct team_port *port)
818{
819 struct netpoll *np = port->np;
820
821 if (!np)
822 return;
823 port->np = NULL;
824
825 /* Wait for transmitting packets to finish before freeing. */
826 synchronize_rcu_bh();
827 __netpoll_cleanup(np);
828 kfree(np);
829}
830
831static struct netpoll_info *team_netpoll_info(struct team *team)
832{
833 return team->dev->npinfo;
834}
835
836#else
Amerigo Wang47be03a22012-08-10 01:24:37 +0000837static int team_port_enable_netpoll(struct team *team, struct team_port *port,
838 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000839{
840 return 0;
841}
842static void team_port_disable_netpoll(struct team_port *port)
843{
844}
845static struct netpoll_info *team_netpoll_info(struct team *team)
846{
847 return NULL;
848}
849#endif
850
Jiri Pirko3d249d42011-11-11 22:16:48 +0000851static void __team_port_change_check(struct team_port *port, bool linkup);
852
853static int team_port_add(struct team *team, struct net_device *port_dev)
854{
855 struct net_device *dev = team->dev;
856 struct team_port *port;
857 char *portname = port_dev->name;
858 int err;
859
860 if (port_dev->flags & IFF_LOOPBACK ||
861 port_dev->type != ARPHRD_ETHER) {
862 netdev_err(dev, "Device %s is of an unsupported type\n",
863 portname);
864 return -EINVAL;
865 }
866
867 if (team_port_exists(port_dev)) {
868 netdev_err(dev, "Device %s is already a port "
869 "of a team device\n", portname);
870 return -EBUSY;
871 }
872
873 if (port_dev->flags & IFF_UP) {
874 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
875 portname);
876 return -EBUSY;
877 }
878
Jiri Pirko5149ee52012-06-19 05:54:05 +0000879 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
880 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000881 if (!port)
882 return -ENOMEM;
883
884 port->dev = port_dev;
885 port->team = team;
886
887 port->orig.mtu = port_dev->mtu;
888 err = dev_set_mtu(port_dev, dev->mtu);
889 if (err) {
890 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
891 goto err_set_mtu;
892 }
893
894 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
895
896 err = team_port_enter(team, port);
897 if (err) {
898 netdev_err(dev, "Device %s failed to enter team mode\n",
899 portname);
900 goto err_port_enter;
901 }
902
903 err = dev_open(port_dev);
904 if (err) {
905 netdev_dbg(dev, "Device %s opening failed\n",
906 portname);
907 goto err_dev_open;
908 }
909
Jiri Pirko57459182011-12-08 04:11:20 +0000910 err = vlan_vids_add_by_dev(port_dev, dev);
911 if (err) {
912 netdev_err(dev, "Failed to add vlan ids to device %s\n",
913 portname);
914 goto err_vids_add;
915 }
916
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000917 if (team_netpoll_info(team)) {
Amerigo Wang47be03a22012-08-10 01:24:37 +0000918 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000919 if (err) {
920 netdev_err(dev, "Failed to enable netpoll on device %s\n",
921 portname);
922 goto err_enable_netpoll;
923 }
924 }
925
Jiri Pirko3d249d42011-11-11 22:16:48 +0000926 err = netdev_set_master(port_dev, dev);
927 if (err) {
928 netdev_err(dev, "Device %s failed to set master\n", portname);
929 goto err_set_master;
930 }
931
932 err = netdev_rx_handler_register(port_dev, team_handle_frame,
933 port);
934 if (err) {
935 netdev_err(dev, "Device %s failed to register rx_handler\n",
936 portname);
937 goto err_handler_register;
938 }
939
Jiri Pirko35b384bd2012-06-19 05:54:19 +0000940 err = __team_option_inst_add_port(team, port);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000941 if (err) {
942 netdev_err(dev, "Device %s failed to add per-port options\n",
943 portname);
944 goto err_option_port_add;
945 }
946
Jiri Pirko19a0b582012-04-20 04:42:05 +0000947 port->index = -1;
948 team_port_enable(team, port);
949 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000950 __team_compute_features(team);
951 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
Jiri Pirko35b384bd2012-06-19 05:54:19 +0000952 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000953
954 netdev_info(dev, "Port device %s added\n", portname);
955
956 return 0;
957
Jiri Pirko80f7c662012-04-10 05:15:42 +0000958err_option_port_add:
959 netdev_rx_handler_unregister(port_dev);
960
Jiri Pirko3d249d42011-11-11 22:16:48 +0000961err_handler_register:
962 netdev_set_master(port_dev, NULL);
963
964err_set_master:
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000965 team_port_disable_netpoll(port);
966
967err_enable_netpoll:
Jiri Pirko57459182011-12-08 04:11:20 +0000968 vlan_vids_del_by_dev(port_dev, dev);
969
970err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +0000971 dev_close(port_dev);
972
973err_dev_open:
974 team_port_leave(team, port);
975 team_port_set_orig_mac(port);
976
977err_port_enter:
978 dev_set_mtu(port_dev, port->orig.mtu);
979
980err_set_mtu:
981 kfree(port);
982
983 return err;
984}
985
986static int team_port_del(struct team *team, struct net_device *port_dev)
987{
988 struct net_device *dev = team->dev;
989 struct team_port *port;
990 char *portname = port_dev->name;
991
992 port = team_port_get_rtnl(port_dev);
993 if (!port || !team_port_find(team, port)) {
994 netdev_err(dev, "Device %s does not act as a port of this team\n",
995 portname);
996 return -ENOENT;
997 }
998
Jiri Pirko35b384bd2012-06-19 05:54:19 +0000999 __team_option_inst_mark_removed_port(team, port);
1000 __team_options_change_check(team);
1001 __team_option_inst_del_port(team, port);
Jiri Pirkob82b9182012-01-24 05:16:00 +00001002 port->removed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001003 __team_port_change_check(port, false);
Jiri Pirko19a0b582012-04-20 04:42:05 +00001004 team_port_disable(team, port);
1005 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001006 netdev_rx_handler_unregister(port_dev);
1007 netdev_set_master(port_dev, NULL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001008 team_port_disable_netpoll(port);
Jiri Pirko57459182011-12-08 04:11:20 +00001009 vlan_vids_del_by_dev(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001010 dev_close(port_dev);
1011 team_port_leave(team, port);
1012 team_port_set_orig_mac(port);
1013 dev_set_mtu(port_dev, port->orig.mtu);
1014 synchronize_rcu();
1015 kfree(port);
1016 netdev_info(dev, "Port device %s removed\n", portname);
1017 __team_compute_features(team);
1018
1019 return 0;
1020}
1021
1022
1023/*****************
1024 * Net device ops
1025 *****************/
1026
Jiri Pirko80f7c662012-04-10 05:15:42 +00001027static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001028{
Jiri Pirkod299cd52012-06-19 05:54:04 +00001029 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001030 return 0;
1031}
1032
Jiri Pirko80f7c662012-04-10 05:15:42 +00001033static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001034{
Jiri Pirko80f7c662012-04-10 05:15:42 +00001035 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001036}
1037
Jiri Pirkoacd69962012-04-20 04:42:06 +00001038static int team_port_en_option_get(struct team *team,
1039 struct team_gsetter_ctx *ctx)
1040{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001041 struct team_port *port = ctx->info->port;
1042
1043 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001044 return 0;
1045}
1046
1047static int team_port_en_option_set(struct team *team,
1048 struct team_gsetter_ctx *ctx)
1049{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001050 struct team_port *port = ctx->info->port;
1051
Jiri Pirkoacd69962012-04-20 04:42:06 +00001052 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001053 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001054 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001055 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001056 return 0;
1057}
1058
Jiri Pirko71472ec2012-04-10 05:15:44 +00001059static int team_user_linkup_option_get(struct team *team,
1060 struct team_gsetter_ctx *ctx)
1061{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001062 struct team_port *port = ctx->info->port;
1063
1064 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001065 return 0;
1066}
1067
1068static int team_user_linkup_option_set(struct team *team,
1069 struct team_gsetter_ctx *ctx)
1070{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001071 struct team_port *port = ctx->info->port;
1072
1073 port->user.linkup = ctx->data.bool_val;
1074 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001075 return 0;
1076}
1077
1078static int team_user_linkup_en_option_get(struct team *team,
1079 struct team_gsetter_ctx *ctx)
1080{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001081 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001082
1083 ctx->data.bool_val = port->user.linkup_enabled;
1084 return 0;
1085}
1086
1087static int team_user_linkup_en_option_set(struct team *team,
1088 struct team_gsetter_ctx *ctx)
1089{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001090 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001091
1092 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001093 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001094 return 0;
1095}
1096
Jiri Pirko358b8382011-11-16 11:09:09 +00001097static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001098 {
1099 .name = "mode",
1100 .type = TEAM_OPTION_TYPE_STRING,
1101 .getter = team_mode_option_get,
1102 .setter = team_mode_option_set,
1103 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001104 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001105 .name = "enabled",
1106 .type = TEAM_OPTION_TYPE_BOOL,
1107 .per_port = true,
1108 .getter = team_port_en_option_get,
1109 .setter = team_port_en_option_set,
1110 },
1111 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001112 .name = "user_linkup",
1113 .type = TEAM_OPTION_TYPE_BOOL,
1114 .per_port = true,
1115 .getter = team_user_linkup_option_get,
1116 .setter = team_user_linkup_option_set,
1117 },
1118 {
1119 .name = "user_linkup_enabled",
1120 .type = TEAM_OPTION_TYPE_BOOL,
1121 .per_port = true,
1122 .getter = team_user_linkup_en_option_get,
1123 .setter = team_user_linkup_en_option_set,
1124 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001125};
1126
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001127static struct lock_class_key team_netdev_xmit_lock_key;
1128static struct lock_class_key team_netdev_addr_lock_key;
1129
1130static void team_set_lockdep_class_one(struct net_device *dev,
1131 struct netdev_queue *txq,
1132 void *unused)
1133{
1134 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1135}
1136
1137static void team_set_lockdep_class(struct net_device *dev)
1138{
1139 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1140 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1141}
1142
Jiri Pirko3d249d42011-11-11 22:16:48 +00001143static int team_init(struct net_device *dev)
1144{
1145 struct team *team = netdev_priv(dev);
1146 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001147 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001148
1149 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001150 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001151 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001152
1153 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1154 if (!team->pcpu_stats)
1155 return -ENOMEM;
1156
1157 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001158 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001159 INIT_LIST_HEAD(&team->port_list);
1160
1161 team_adjust_ops(team);
1162
1163 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001164 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirko358b8382011-11-16 11:09:09 +00001165 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1166 if (err)
1167 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001168 netif_carrier_off(dev);
1169
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001170 team_set_lockdep_class(dev);
1171
Jiri Pirko3d249d42011-11-11 22:16:48 +00001172 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001173
1174err_options_register:
1175 free_percpu(team->pcpu_stats);
1176
1177 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001178}
1179
1180static void team_uninit(struct net_device *dev)
1181{
1182 struct team *team = netdev_priv(dev);
1183 struct team_port *port;
1184 struct team_port *tmp;
1185
Jiri Pirko61dc3462011-11-16 11:09:08 +00001186 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001187 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1188 team_port_del(team, port->dev);
1189
1190 __team_change_mode(team, NULL); /* cleanup */
1191 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko61dc3462011-11-16 11:09:08 +00001192 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001193}
1194
1195static void team_destructor(struct net_device *dev)
1196{
1197 struct team *team = netdev_priv(dev);
1198
1199 free_percpu(team->pcpu_stats);
1200 free_netdev(dev);
1201}
1202
1203static int team_open(struct net_device *dev)
1204{
1205 netif_carrier_on(dev);
1206 return 0;
1207}
1208
1209static int team_close(struct net_device *dev)
1210{
1211 netif_carrier_off(dev);
1212 return 0;
1213}
1214
1215/*
1216 * note: already called with rcu_read_lock
1217 */
1218static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1219{
1220 struct team *team = netdev_priv(dev);
1221 bool tx_success = false;
1222 unsigned int len = skb->len;
1223
1224 tx_success = team->ops.transmit(team, skb);
1225 if (tx_success) {
1226 struct team_pcpu_stats *pcpu_stats;
1227
1228 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1229 u64_stats_update_begin(&pcpu_stats->syncp);
1230 pcpu_stats->tx_packets++;
1231 pcpu_stats->tx_bytes += len;
1232 u64_stats_update_end(&pcpu_stats->syncp);
1233 } else {
1234 this_cpu_inc(team->pcpu_stats->tx_dropped);
1235 }
1236
1237 return NETDEV_TX_OK;
1238}
1239
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001240static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1241{
1242 /*
1243 * This helper function exists to help dev_pick_tx get the correct
1244 * destination queue. Using a helper function skips a call to
1245 * skb_tx_hash and will put the skbs in the queue we expect on their
1246 * way down to the team driver.
1247 */
1248 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1249
1250 /*
1251 * Save the original txq to restore before passing to the driver
1252 */
1253 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1254
1255 if (unlikely(txq >= dev->real_num_tx_queues)) {
1256 do {
1257 txq -= dev->real_num_tx_queues;
1258 } while (txq >= dev->real_num_tx_queues);
1259 }
1260 return txq;
1261}
1262
Jiri Pirko3d249d42011-11-11 22:16:48 +00001263static void team_change_rx_flags(struct net_device *dev, int change)
1264{
1265 struct team *team = netdev_priv(dev);
1266 struct team_port *port;
1267 int inc;
1268
1269 rcu_read_lock();
1270 list_for_each_entry_rcu(port, &team->port_list, list) {
1271 if (change & IFF_PROMISC) {
1272 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1273 dev_set_promiscuity(port->dev, inc);
1274 }
1275 if (change & IFF_ALLMULTI) {
1276 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1277 dev_set_allmulti(port->dev, inc);
1278 }
1279 }
1280 rcu_read_unlock();
1281}
1282
1283static void team_set_rx_mode(struct net_device *dev)
1284{
1285 struct team *team = netdev_priv(dev);
1286 struct team_port *port;
1287
1288 rcu_read_lock();
1289 list_for_each_entry_rcu(port, &team->port_list, list) {
1290 dev_uc_sync(port->dev, dev);
1291 dev_mc_sync(port->dev, dev);
1292 }
1293 rcu_read_unlock();
1294}
1295
1296static int team_set_mac_address(struct net_device *dev, void *p)
1297{
1298 struct team *team = netdev_priv(dev);
1299 struct team_port *port;
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00001300 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001301
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00001302 err = eth_mac_addr(dev, p);
1303 if (err)
1304 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001305 rcu_read_lock();
1306 list_for_each_entry_rcu(port, &team->port_list, list)
1307 if (team->ops.port_change_mac)
1308 team->ops.port_change_mac(team, port);
1309 rcu_read_unlock();
1310 return 0;
1311}
1312
1313static int team_change_mtu(struct net_device *dev, int new_mtu)
1314{
1315 struct team *team = netdev_priv(dev);
1316 struct team_port *port;
1317 int err;
1318
1319 /*
1320 * Alhough this is reader, it's guarded by team lock. It's not possible
1321 * to traverse list in reverse under rcu_read_lock
1322 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001323 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001324 list_for_each_entry(port, &team->port_list, list) {
1325 err = dev_set_mtu(port->dev, new_mtu);
1326 if (err) {
1327 netdev_err(dev, "Device %s failed to change mtu",
1328 port->dev->name);
1329 goto unwind;
1330 }
1331 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001332 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001333
1334 dev->mtu = new_mtu;
1335
1336 return 0;
1337
1338unwind:
1339 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1340 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001341 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001342
1343 return err;
1344}
1345
1346static struct rtnl_link_stats64 *
1347team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1348{
1349 struct team *team = netdev_priv(dev);
1350 struct team_pcpu_stats *p;
1351 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1352 u32 rx_dropped = 0, tx_dropped = 0;
1353 unsigned int start;
1354 int i;
1355
1356 for_each_possible_cpu(i) {
1357 p = per_cpu_ptr(team->pcpu_stats, i);
1358 do {
1359 start = u64_stats_fetch_begin_bh(&p->syncp);
1360 rx_packets = p->rx_packets;
1361 rx_bytes = p->rx_bytes;
1362 rx_multicast = p->rx_multicast;
1363 tx_packets = p->tx_packets;
1364 tx_bytes = p->tx_bytes;
1365 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1366
1367 stats->rx_packets += rx_packets;
1368 stats->rx_bytes += rx_bytes;
1369 stats->multicast += rx_multicast;
1370 stats->tx_packets += tx_packets;
1371 stats->tx_bytes += tx_bytes;
1372 /*
1373 * rx_dropped & tx_dropped are u32, updated
1374 * without syncp protection.
1375 */
1376 rx_dropped += p->rx_dropped;
1377 tx_dropped += p->tx_dropped;
1378 }
1379 stats->rx_dropped = rx_dropped;
1380 stats->tx_dropped = tx_dropped;
1381 return stats;
1382}
1383
Jiri Pirko8e586132011-12-08 19:52:37 -05001384static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001385{
1386 struct team *team = netdev_priv(dev);
1387 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001388 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001389
Jiri Pirko87002b02011-12-08 04:11:17 +00001390 /*
1391 * Alhough this is reader, it's guarded by team lock. It's not possible
1392 * to traverse list in reverse under rcu_read_lock
1393 */
1394 mutex_lock(&team->lock);
1395 list_for_each_entry(port, &team->port_list, list) {
1396 err = vlan_vid_add(port->dev, vid);
1397 if (err)
1398 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001399 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001400 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001401
1402 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001403
1404unwind:
1405 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1406 vlan_vid_del(port->dev, vid);
1407 mutex_unlock(&team->lock);
1408
1409 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001410}
1411
Jiri Pirko8e586132011-12-08 19:52:37 -05001412static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001413{
1414 struct team *team = netdev_priv(dev);
1415 struct team_port *port;
1416
1417 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001418 list_for_each_entry_rcu(port, &team->port_list, list)
1419 vlan_vid_del(port->dev, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001420 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001421
1422 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001423}
1424
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001425#ifdef CONFIG_NET_POLL_CONTROLLER
1426static void team_poll_controller(struct net_device *dev)
1427{
1428}
1429
1430static void __team_netpoll_cleanup(struct team *team)
1431{
1432 struct team_port *port;
1433
1434 list_for_each_entry(port, &team->port_list, list)
1435 team_port_disable_netpoll(port);
1436}
1437
1438static void team_netpoll_cleanup(struct net_device *dev)
1439{
1440 struct team *team = netdev_priv(dev);
1441
1442 mutex_lock(&team->lock);
1443 __team_netpoll_cleanup(team);
1444 mutex_unlock(&team->lock);
1445}
1446
1447static int team_netpoll_setup(struct net_device *dev,
Amerigo Wang47be03a22012-08-10 01:24:37 +00001448 struct netpoll_info *npifo, gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001449{
1450 struct team *team = netdev_priv(dev);
1451 struct team_port *port;
Jiri Pirko3324d022012-07-24 01:20:48 +00001452 int err = 0;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001453
1454 mutex_lock(&team->lock);
1455 list_for_each_entry(port, &team->port_list, list) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001456 err = team_port_enable_netpoll(team, port, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001457 if (err) {
1458 __team_netpoll_cleanup(team);
1459 break;
1460 }
1461 }
1462 mutex_unlock(&team->lock);
1463 return err;
1464}
1465#endif
1466
Jiri Pirko3d249d42011-11-11 22:16:48 +00001467static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1468{
1469 struct team *team = netdev_priv(dev);
1470 int err;
1471
Jiri Pirko61dc3462011-11-16 11:09:08 +00001472 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001473 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001474 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001475 return err;
1476}
1477
1478static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1479{
1480 struct team *team = netdev_priv(dev);
1481 int err;
1482
Jiri Pirko61dc3462011-11-16 11:09:08 +00001483 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001484 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001485 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001486 return err;
1487}
1488
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001489static netdev_features_t team_fix_features(struct net_device *dev,
1490 netdev_features_t features)
1491{
1492 struct team_port *port;
1493 struct team *team = netdev_priv(dev);
1494 netdev_features_t mask;
1495
1496 mask = features;
1497 features &= ~NETIF_F_ONE_FOR_ALL;
1498 features |= NETIF_F_ALL_FOR_ALL;
1499
1500 rcu_read_lock();
1501 list_for_each_entry_rcu(port, &team->port_list, list) {
1502 features = netdev_increment_features(features,
1503 port->dev->features,
1504 mask);
1505 }
1506 rcu_read_unlock();
1507 return features;
1508}
1509
Jiri Pirko3d249d42011-11-11 22:16:48 +00001510static const struct net_device_ops team_netdev_ops = {
1511 .ndo_init = team_init,
1512 .ndo_uninit = team_uninit,
1513 .ndo_open = team_open,
1514 .ndo_stop = team_close,
1515 .ndo_start_xmit = team_xmit,
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001516 .ndo_select_queue = team_select_queue,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001517 .ndo_change_rx_flags = team_change_rx_flags,
1518 .ndo_set_rx_mode = team_set_rx_mode,
1519 .ndo_set_mac_address = team_set_mac_address,
1520 .ndo_change_mtu = team_change_mtu,
1521 .ndo_get_stats64 = team_get_stats64,
1522 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1523 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001524#ifdef CONFIG_NET_POLL_CONTROLLER
1525 .ndo_poll_controller = team_poll_controller,
1526 .ndo_netpoll_setup = team_netpoll_setup,
1527 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1528#endif
Jiri Pirko3d249d42011-11-11 22:16:48 +00001529 .ndo_add_slave = team_add_slave,
1530 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001531 .ndo_fix_features = team_fix_features,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001532};
1533
1534
1535/***********************
1536 * rt netlink interface
1537 ***********************/
1538
1539static void team_setup(struct net_device *dev)
1540{
1541 ether_setup(dev);
1542
1543 dev->netdev_ops = &team_netdev_ops;
1544 dev->destructor = team_destructor;
1545 dev->tx_queue_len = 0;
1546 dev->flags |= IFF_MULTICAST;
1547 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1548
1549 /*
1550 * Indicate we support unicast address filtering. That way core won't
1551 * bring us to promisc mode in case a unicast addr is added.
1552 * Let this up to underlay drivers.
1553 */
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00001554 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001555
1556 dev->features |= NETIF_F_LLTX;
1557 dev->features |= NETIF_F_GRO;
1558 dev->hw_features = NETIF_F_HW_VLAN_TX |
1559 NETIF_F_HW_VLAN_RX |
1560 NETIF_F_HW_VLAN_FILTER;
1561
1562 dev->features |= dev->hw_features;
1563}
1564
1565static int team_newlink(struct net *src_net, struct net_device *dev,
1566 struct nlattr *tb[], struct nlattr *data[])
1567{
1568 int err;
1569
1570 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001571 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001572
1573 err = register_netdevice(dev);
1574 if (err)
1575 return err;
1576
1577 return 0;
1578}
1579
1580static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1581{
1582 if (tb[IFLA_ADDRESS]) {
1583 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1584 return -EINVAL;
1585 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1586 return -EADDRNOTAVAIL;
1587 }
1588 return 0;
1589}
1590
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001591static unsigned int team_get_num_tx_queues(void)
1592{
1593 return TEAM_DEFAULT_NUM_TX_QUEUES;
1594}
1595
1596static unsigned int team_get_num_rx_queues(void)
1597{
1598 return TEAM_DEFAULT_NUM_RX_QUEUES;
1599}
1600
Jiri Pirko3d249d42011-11-11 22:16:48 +00001601static struct rtnl_link_ops team_link_ops __read_mostly = {
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001602 .kind = DRV_NAME,
1603 .priv_size = sizeof(struct team),
1604 .setup = team_setup,
1605 .newlink = team_newlink,
1606 .validate = team_validate,
1607 .get_num_tx_queues = team_get_num_tx_queues,
1608 .get_num_rx_queues = team_get_num_rx_queues,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001609};
1610
1611
1612/***********************************
1613 * Generic netlink custom interface
1614 ***********************************/
1615
1616static struct genl_family team_nl_family = {
1617 .id = GENL_ID_GENERATE,
1618 .name = TEAM_GENL_NAME,
1619 .version = TEAM_GENL_VERSION,
1620 .maxattr = TEAM_ATTR_MAX,
1621 .netnsok = true,
1622};
1623
1624static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1625 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1626 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1627 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1628 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1629};
1630
1631static const struct nla_policy
1632team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1633 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1634 [TEAM_ATTR_OPTION_NAME] = {
1635 .type = NLA_STRING,
1636 .len = TEAM_STRING_MAX_LEN,
1637 },
1638 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1639 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00001640 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001641};
1642
1643static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1644{
1645 struct sk_buff *msg;
1646 void *hdr;
1647 int err;
1648
Thomas Graf58050fc2012-06-28 03:57:45 +00001649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001650 if (!msg)
1651 return -ENOMEM;
1652
1653 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1654 &team_nl_family, 0, TEAM_CMD_NOOP);
1655 if (IS_ERR(hdr)) {
1656 err = PTR_ERR(hdr);
1657 goto err_msg_put;
1658 }
1659
1660 genlmsg_end(msg, hdr);
1661
1662 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1663
1664err_msg_put:
1665 nlmsg_free(msg);
1666
1667 return err;
1668}
1669
1670/*
1671 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001672 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00001673 */
1674static struct team *team_nl_team_get(struct genl_info *info)
1675{
1676 struct net *net = genl_info_net(info);
1677 int ifindex;
1678 struct net_device *dev;
1679 struct team *team;
1680
1681 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1682 return NULL;
1683
1684 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001685 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001686 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001687 if (dev)
1688 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001689 return NULL;
1690 }
1691
1692 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001693 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001694 return team;
1695}
1696
1697static void team_nl_team_put(struct team *team)
1698{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001699 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001700 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001701}
1702
1703static int team_nl_send_generic(struct genl_info *info, struct team *team,
1704 int (*fill_func)(struct sk_buff *skb,
1705 struct genl_info *info,
1706 int flags, struct team *team))
1707{
1708 struct sk_buff *skb;
1709 int err;
1710
Thomas Graf58050fc2012-06-28 03:57:45 +00001711 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001712 if (!skb)
1713 return -ENOMEM;
1714
1715 err = fill_func(skb, info, NLM_F_ACK, team);
1716 if (err < 0)
1717 goto err_fill;
1718
1719 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1720 return err;
1721
1722err_fill:
1723 nlmsg_free(skb);
1724 return err;
1725}
1726
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001727typedef int team_nl_send_func_t(struct sk_buff *skb,
1728 struct team *team, u32 pid);
1729
1730static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid)
1731{
1732 return genlmsg_unicast(dev_net(team->dev), skb, pid);
1733}
1734
Jiri Pirko01048d92012-06-19 05:54:14 +00001735static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1736 struct team_option_inst *opt_inst)
1737{
1738 struct nlattr *option_item;
1739 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001740 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00001741 struct team_gsetter_ctx ctx;
1742 int err;
1743
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001744 ctx.info = opt_inst_info;
1745 err = team_option_get(team, opt_inst, &ctx);
1746 if (err)
1747 return err;
1748
Jiri Pirko01048d92012-06-19 05:54:14 +00001749 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1750 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001751 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00001752
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001753 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1754 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001755 if (opt_inst_info->port &&
1756 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1757 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001758 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001759 if (opt_inst->option->array_size &&
1760 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1761 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001762 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001763
1764 switch (option->type) {
1765 case TEAM_OPTION_TYPE_U32:
1766 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001767 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001768 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001769 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001770 break;
1771 case TEAM_OPTION_TYPE_STRING:
1772 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001773 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001774 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
1775 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001776 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001777 break;
1778 case TEAM_OPTION_TYPE_BINARY:
1779 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001780 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001781 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
1782 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001783 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001784 break;
1785 case TEAM_OPTION_TYPE_BOOL:
1786 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001787 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001788 if (ctx.data.bool_val &&
1789 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001790 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001791 break;
1792 default:
1793 BUG();
1794 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001795 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1796 goto nest_cancel;
1797 if (opt_inst->changed) {
1798 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1799 goto nest_cancel;
1800 opt_inst->changed = false;
1801 }
Jiri Pirko01048d92012-06-19 05:54:14 +00001802 nla_nest_end(skb, option_item);
1803 return 0;
1804
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001805nest_cancel:
1806 nla_nest_cancel(skb, option_item);
1807 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00001808}
1809
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001810static int __send_and_alloc_skb(struct sk_buff **pskb,
1811 struct team *team, u32 pid,
1812 team_nl_send_func_t *send_func)
1813{
1814 int err;
1815
1816 if (*pskb) {
1817 err = send_func(*pskb, team, pid);
1818 if (err)
1819 return err;
1820 }
Thomas Graf58050fc2012-06-28 03:57:45 +00001821 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001822 if (!*pskb)
1823 return -ENOMEM;
1824 return 0;
1825}
1826
1827static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq,
1828 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00001829 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001830{
1831 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001832 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001833 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001834 struct team_option_inst *opt_inst;
1835 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001836 struct sk_buff *skb = NULL;
1837 bool incomplete;
1838 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001839
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001840 opt_inst = list_first_entry(sel_opt_inst_list,
1841 struct team_option_inst, tmp_list);
1842
1843start_again:
1844 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1845 if (err)
1846 return err;
1847
1848 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001849 TEAM_CMD_OPTIONS_GET);
1850 if (IS_ERR(hdr))
1851 return PTR_ERR(hdr);
1852
David S. Miller86ebb022012-04-01 20:25:18 -04001853 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1854 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001855 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1856 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00001857 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001858
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001859 i = 0;
1860 incomplete = false;
1861 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00001862 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001863 if (err) {
1864 if (err == -EMSGSIZE) {
1865 if (!i)
1866 goto errout;
1867 incomplete = true;
1868 break;
1869 }
Jiri Pirko01048d92012-06-19 05:54:14 +00001870 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001871 }
1872 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001873 }
1874
1875 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001876 genlmsg_end(skb, hdr);
1877 if (incomplete)
1878 goto start_again;
1879
1880send_done:
1881 nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
1882 if (!nlh) {
1883 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1884 if (err)
1885 goto errout;
1886 goto send_done;
1887 }
1888
1889 return send_func(skb, team, pid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001890
1891nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001892 err = -EMSGSIZE;
1893errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001894 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001895 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001896 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001897}
1898
Jiri Pirko3d249d42011-11-11 22:16:48 +00001899static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1900{
1901 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001902 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001903 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001904 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001905
1906 team = team_nl_team_get(info);
1907 if (!team)
1908 return -EINVAL;
1909
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001910 list_for_each_entry(opt_inst, &team->option_inst_list, list)
1911 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
1912 err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq,
1913 NLM_F_ACK, team_nl_send_unicast,
1914 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001915
1916 team_nl_team_put(team);
1917
1918 return err;
1919}
1920
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00001921static int team_nl_send_event_options_get(struct team *team,
1922 struct list_head *sel_opt_inst_list);
1923
Jiri Pirko3d249d42011-11-11 22:16:48 +00001924static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1925{
1926 struct team *team;
1927 int err = 0;
1928 int i;
1929 struct nlattr *nl_option;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00001930 LIST_HEAD(opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001931
1932 team = team_nl_team_get(info);
1933 if (!team)
1934 return -EINVAL;
1935
1936 err = -EINVAL;
1937 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1938 err = -EINVAL;
1939 goto team_put;
1940 }
1941
1942 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00001943 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00001944 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001945 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001946 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001947 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00001948 u32 opt_array_index = 0;
1949 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001950 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001951 char *opt_name;
1952 bool opt_found = false;
1953
1954 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1955 err = -EINVAL;
1956 goto team_put;
1957 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001958 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001959 nl_option, team_nl_option_policy);
1960 if (err)
1961 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001962 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00001963 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001964 err = -EINVAL;
1965 goto team_put;
1966 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001967 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001968 case NLA_U32:
1969 opt_type = TEAM_OPTION_TYPE_U32;
1970 break;
1971 case NLA_STRING:
1972 opt_type = TEAM_OPTION_TYPE_STRING;
1973 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001974 case NLA_BINARY:
1975 opt_type = TEAM_OPTION_TYPE_BINARY;
1976 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001977 case NLA_FLAG:
1978 opt_type = TEAM_OPTION_TYPE_BOOL;
1979 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001980 default:
1981 goto team_put;
1982 }
1983
Jiri Pirko14f066b2012-04-10 05:15:43 +00001984 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1985 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1986 err = -EINVAL;
1987 goto team_put;
1988 }
1989
Jiri Pirko80f7c662012-04-10 05:15:42 +00001990 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00001991 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1992 if (attr)
1993 opt_port_ifindex = nla_get_u32(attr);
1994
1995 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1996 if (attr) {
1997 opt_is_array = true;
1998 opt_array_index = nla_get_u32(attr);
1999 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002000
Jiri Pirko80f7c662012-04-10 05:15:42 +00002001 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2002 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002003 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002004 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002005 int tmp_ifindex;
2006
Jiri Pirko85d59a82012-06-19 05:54:10 +00002007 opt_inst_info = &opt_inst->info;
2008 tmp_ifindex = opt_inst_info->port ?
2009 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002010 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00002011 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00002012 tmp_ifindex != opt_port_ifindex ||
2013 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00002014 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002015 continue;
2016 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002017 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002018 switch (opt_type) {
2019 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002020 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002021 break;
2022 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002023 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00002024 err = -EINVAL;
2025 goto team_put;
2026 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00002027 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002028 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002029 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002030 ctx.data.bin_val.len = nla_len(attr_data);
2031 ctx.data.bin_val.ptr = nla_data(attr_data);
2032 break;
2033 case TEAM_OPTION_TYPE_BOOL:
2034 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00002035 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002036 default:
2037 BUG();
2038 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002039 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002040 if (err)
2041 goto team_put;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002042 opt_inst->changed = true;
2043 list_add(&opt_inst->tmp_list, &opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002044 }
2045 if (!opt_found) {
2046 err = -ENOENT;
2047 goto team_put;
2048 }
2049 }
2050
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002051 err = team_nl_send_event_options_get(team, &opt_inst_list);
2052
Jiri Pirko3d249d42011-11-11 22:16:48 +00002053team_put:
2054 team_nl_team_put(team);
2055
2056 return err;
2057}
2058
Jiri Pirkob82b9182012-01-24 05:16:00 +00002059static int team_nl_fill_port_list_get(struct sk_buff *skb,
2060 u32 pid, u32 seq, int flags,
2061 struct team *team,
2062 bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002063{
2064 struct nlattr *port_list;
2065 void *hdr;
2066 struct team_port *port;
2067
2068 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
2069 TEAM_CMD_PORT_LIST_GET);
2070 if (IS_ERR(hdr))
2071 return PTR_ERR(hdr);
2072
David S. Miller86ebb022012-04-01 20:25:18 -04002073 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2074 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002075 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2076 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00002077 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002078
2079 list_for_each_entry(port, &team->port_list, list) {
2080 struct nlattr *port_item;
2081
Jiri Pirkob82b9182012-01-24 05:16:00 +00002082 /* Include only changed ports if fill all mode is not on */
2083 if (!fillall && !port->changed)
2084 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002085 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2086 if (!port_item)
2087 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04002088 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2089 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002090 if (port->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04002091 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2092 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002093 port->changed = false;
2094 }
David S. Miller86ebb022012-04-01 20:25:18 -04002095 if ((port->removed &&
2096 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00002097 (port->state.linkup &&
David S. Miller86ebb022012-04-01 20:25:18 -04002098 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00002099 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2100 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
David S. Miller86ebb022012-04-01 20:25:18 -04002101 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002102 nla_nest_end(skb, port_item);
2103 }
2104
2105 nla_nest_end(skb, port_list);
2106 return genlmsg_end(skb, hdr);
2107
2108nla_put_failure:
2109 genlmsg_cancel(skb, hdr);
2110 return -EMSGSIZE;
2111}
2112
Jiri Pirkob82b9182012-01-24 05:16:00 +00002113static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
2114 struct genl_info *info, int flags,
2115 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002116{
Jiri Pirkob82b9182012-01-24 05:16:00 +00002117 return team_nl_fill_port_list_get(skb, info->snd_pid,
2118 info->snd_seq, NLM_F_ACK,
2119 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002120}
2121
2122static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2123 struct genl_info *info)
2124{
2125 struct team *team;
2126 int err;
2127
2128 team = team_nl_team_get(info);
2129 if (!team)
2130 return -EINVAL;
2131
Jiri Pirkob82b9182012-01-24 05:16:00 +00002132 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002133
2134 team_nl_team_put(team);
2135
2136 return err;
2137}
2138
2139static struct genl_ops team_nl_ops[] = {
2140 {
2141 .cmd = TEAM_CMD_NOOP,
2142 .doit = team_nl_cmd_noop,
2143 .policy = team_nl_policy,
2144 },
2145 {
2146 .cmd = TEAM_CMD_OPTIONS_SET,
2147 .doit = team_nl_cmd_options_set,
2148 .policy = team_nl_policy,
2149 .flags = GENL_ADMIN_PERM,
2150 },
2151 {
2152 .cmd = TEAM_CMD_OPTIONS_GET,
2153 .doit = team_nl_cmd_options_get,
2154 .policy = team_nl_policy,
2155 .flags = GENL_ADMIN_PERM,
2156 },
2157 {
2158 .cmd = TEAM_CMD_PORT_LIST_GET,
2159 .doit = team_nl_cmd_port_list_get,
2160 .policy = team_nl_policy,
2161 .flags = GENL_ADMIN_PERM,
2162 },
2163};
2164
2165static struct genl_multicast_group team_change_event_mcgrp = {
2166 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2167};
2168
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002169static int team_nl_send_multicast(struct sk_buff *skb,
2170 struct team *team, u32 pid)
2171{
2172 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2173 team_change_event_mcgrp.id, GFP_KERNEL);
2174}
2175
Jiri Pirko01048d92012-06-19 05:54:14 +00002176static int team_nl_send_event_options_get(struct team *team,
2177 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002178{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002179 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2180 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002181}
2182
Jiri Pirkob82b9182012-01-24 05:16:00 +00002183static int team_nl_send_event_port_list_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002184{
2185 struct sk_buff *skb;
2186 int err;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002187 struct net *net = dev_net(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002188
Thomas Graf58050fc2012-06-28 03:57:45 +00002189 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002190 if (!skb)
2191 return -ENOMEM;
2192
Jiri Pirkob82b9182012-01-24 05:16:00 +00002193 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002194 if (err < 0)
2195 goto err_fill;
2196
2197 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2198 GFP_KERNEL);
2199 return err;
2200
2201err_fill:
2202 nlmsg_free(skb);
2203 return err;
2204}
2205
2206static int team_nl_init(void)
2207{
2208 int err;
2209
2210 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2211 ARRAY_SIZE(team_nl_ops));
2212 if (err)
2213 return err;
2214
2215 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2216 if (err)
2217 goto err_change_event_grp_reg;
2218
2219 return 0;
2220
2221err_change_event_grp_reg:
2222 genl_unregister_family(&team_nl_family);
2223
2224 return err;
2225}
2226
2227static void team_nl_fini(void)
2228{
2229 genl_unregister_family(&team_nl_family);
2230}
2231
2232
2233/******************
2234 * Change checkers
2235 ******************/
2236
Jiri Pirkob82b9182012-01-24 05:16:00 +00002237static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002238{
2239 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002240 struct team_option_inst *opt_inst;
2241 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002242
Jiri Pirko01048d92012-06-19 05:54:14 +00002243 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2244 if (opt_inst->changed)
2245 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2246 }
2247 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002248 if (err)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002249 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2250 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002251}
2252
2253/* rtnl lock is held */
2254static void __team_port_change_check(struct team_port *port, bool linkup)
2255{
2256 int err;
2257
Jiri Pirko71472ec2012-04-10 05:15:44 +00002258 if (!port->removed && port->state.linkup == linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002259 return;
2260
Jiri Pirkob82b9182012-01-24 05:16:00 +00002261 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002262 port->state.linkup = linkup;
2263 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002264 if (linkup) {
2265 struct ethtool_cmd ecmd;
2266
2267 err = __ethtool_get_settings(port->dev, &ecmd);
2268 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002269 port->state.speed = ethtool_cmd_speed(&ecmd);
2270 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002271 goto send_event;
2272 }
2273 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002274 port->state.speed = 0;
2275 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002276
2277send_event:
Jiri Pirkob82b9182012-01-24 05:16:00 +00002278 err = team_nl_send_event_port_list_get(port->team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002279 if (err)
2280 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2281 port->dev->name);
2282
2283}
2284
2285static void team_port_change_check(struct team_port *port, bool linkup)
2286{
2287 struct team *team = port->team;
2288
Jiri Pirko61dc3462011-11-16 11:09:08 +00002289 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002290 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002291 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002292}
2293
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002294
Jiri Pirko3d249d42011-11-11 22:16:48 +00002295/************************************
2296 * Net device notifier event handler
2297 ************************************/
2298
2299static int team_device_event(struct notifier_block *unused,
2300 unsigned long event, void *ptr)
2301{
2302 struct net_device *dev = (struct net_device *) ptr;
2303 struct team_port *port;
2304
2305 port = team_port_get_rtnl(dev);
2306 if (!port)
2307 return NOTIFY_DONE;
2308
2309 switch (event) {
2310 case NETDEV_UP:
2311 if (netif_carrier_ok(dev))
2312 team_port_change_check(port, true);
2313 case NETDEV_DOWN:
2314 team_port_change_check(port, false);
2315 case NETDEV_CHANGE:
2316 if (netif_running(port->dev))
2317 team_port_change_check(port,
2318 !!netif_carrier_ok(port->dev));
2319 break;
2320 case NETDEV_UNREGISTER:
2321 team_del_slave(port->team->dev, dev);
2322 break;
2323 case NETDEV_FEAT_CHANGE:
2324 team_compute_features(port->team);
2325 break;
2326 case NETDEV_CHANGEMTU:
2327 /* Forbid to change mtu of underlaying device */
2328 return NOTIFY_BAD;
2329 case NETDEV_PRE_TYPE_CHANGE:
2330 /* Forbid to change type of underlaying device */
2331 return NOTIFY_BAD;
2332 }
2333 return NOTIFY_DONE;
2334}
2335
2336static struct notifier_block team_notifier_block __read_mostly = {
2337 .notifier_call = team_device_event,
2338};
2339
2340
2341/***********************
2342 * Module init and exit
2343 ***********************/
2344
2345static int __init team_module_init(void)
2346{
2347 int err;
2348
2349 register_netdevice_notifier(&team_notifier_block);
2350
2351 err = rtnl_link_register(&team_link_ops);
2352 if (err)
2353 goto err_rtnl_reg;
2354
2355 err = team_nl_init();
2356 if (err)
2357 goto err_nl_init;
2358
2359 return 0;
2360
2361err_nl_init:
2362 rtnl_link_unregister(&team_link_ops);
2363
2364err_rtnl_reg:
2365 unregister_netdevice_notifier(&team_notifier_block);
2366
2367 return err;
2368}
2369
2370static void __exit team_module_exit(void)
2371{
2372 team_nl_fini();
2373 rtnl_link_unregister(&team_link_ops);
2374 unregister_netdevice_notifier(&team_notifier_block);
2375}
2376
2377module_init(team_module_init);
2378module_exit(team_module_exit);
2379
2380MODULE_LICENSE("GPL v2");
2381MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2382MODULE_DESCRIPTION("Ethernet team device driver");
2383MODULE_ALIAS_RTNL_LINK(DRV_NAME);