blob: 0433ee994f8c1ac2a9979ef14544ca90bd7f8115 [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>
Flavio Leitner7f51c582012-12-29 16:37:33 +000031#include <generated/utsrelease.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000032#include <linux/if_team.h>
33
34#define DRV_NAME "team"
35
36
37/**********
38 * Helpers
39 **********/
40
41#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
42
43static struct team_port *team_port_get_rcu(const struct net_device *dev)
44{
45 struct team_port *port = rcu_dereference(dev->rx_handler_data);
46
47 return team_port_exists(dev) ? port : NULL;
48}
49
50static struct team_port *team_port_get_rtnl(const struct net_device *dev)
51{
52 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
53
54 return team_port_exists(dev) ? port : NULL;
55}
56
57/*
Jiri Pirko1d76efe2012-08-17 04:00:48 +000058 * Since the ability to change device address for open port device is tested in
Jiri Pirko3d249d42011-11-11 22:16:48 +000059 * team_port_add, this function can be called without control of return value
60 */
Jiri Pirko1d76efe2012-08-17 04:00:48 +000061static int __set_port_dev_addr(struct net_device *port_dev,
62 const unsigned char *dev_addr)
Jiri Pirko3d249d42011-11-11 22:16:48 +000063{
64 struct sockaddr addr;
65
Jiri Pirko1d76efe2012-08-17 04:00:48 +000066 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
67 addr.sa_family = port_dev->type;
Jiri Pirko3d249d42011-11-11 22:16:48 +000068 return dev_set_mac_address(port_dev, &addr);
69}
70
Jiri Pirko1d76efe2012-08-17 04:00:48 +000071static int team_port_set_orig_dev_addr(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000072{
Jiri Pirko1d76efe2012-08-17 04:00:48 +000073 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000074}
75
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000076static int team_port_set_team_dev_addr(struct team *team,
77 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000078{
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000079 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000080}
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000081
82int team_modeop_port_enter(struct team *team, struct team_port *port)
83{
84 return team_port_set_team_dev_addr(team, port);
85}
86EXPORT_SYMBOL(team_modeop_port_enter);
87
88void team_modeop_port_change_dev_addr(struct team *team,
89 struct team_port *port)
90{
91 team_port_set_team_dev_addr(team, port);
92}
93EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000094
Jiri Pirko71472ec2012-04-10 05:15:44 +000095static void team_refresh_port_linkup(struct team_port *port)
96{
97 port->linkup = port->user.linkup_enabled ? port->user.linkup :
98 port->state.linkup;
99}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000100
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000101
Jiri Pirko3d249d42011-11-11 22:16:48 +0000102/*******************
103 * Options handling
104 *******************/
105
Jiri Pirko80f7c662012-04-10 05:15:42 +0000106struct team_option_inst { /* One for each option instance */
107 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +0000108 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000109 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000110 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000111 bool changed;
112 bool removed;
113};
114
115static struct team_option *__team_find_option(struct team *team,
116 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000117{
118 struct team_option *option;
119
120 list_for_each_entry(option, &team->option_list, list) {
121 if (strcmp(option->name, opt_name) == 0)
122 return option;
123 }
124 return NULL;
125}
126
Jiri Pirko80f7c662012-04-10 05:15:42 +0000127static void __team_option_inst_del(struct team_option_inst *opt_inst)
128{
129 list_del(&opt_inst->list);
130 kfree(opt_inst);
131}
132
133static void __team_option_inst_del_option(struct team *team,
134 struct team_option *option)
135{
136 struct team_option_inst *opt_inst, *tmp;
137
138 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
139 if (opt_inst->option == option)
140 __team_option_inst_del(opt_inst);
141 }
142}
143
Jiri Pirkob1303322012-06-19 05:54:08 +0000144static int __team_option_inst_add(struct team *team, struct team_option *option,
145 struct team_port *port)
146{
147 struct team_option_inst *opt_inst;
148 unsigned int array_size;
149 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000150 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000151
152 array_size = option->array_size;
153 if (!array_size)
154 array_size = 1; /* No array but still need one instance */
155
156 for (i = 0; i < array_size; i++) {
157 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
158 if (!opt_inst)
159 return -ENOMEM;
160 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000161 opt_inst->info.port = port;
162 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000163 opt_inst->changed = true;
164 opt_inst->removed = false;
165 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000166 if (option->init) {
167 err = option->init(team, &opt_inst->info);
168 if (err)
169 return err;
170 }
171
Jiri Pirkob1303322012-06-19 05:54:08 +0000172 }
173 return 0;
174}
175
Jiri Pirko80f7c662012-04-10 05:15:42 +0000176static int __team_option_inst_add_option(struct team *team,
177 struct team_option *option)
178{
179 struct team_port *port;
180 int err;
181
Jiri Pirkob1303322012-06-19 05:54:08 +0000182 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000183 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000184 if (err)
185 goto inst_del_option;
186 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000187
188 list_for_each_entry(port, &team->port_list, list) {
189 err = __team_option_inst_add(team, option, port);
190 if (err)
191 goto inst_del_option;
192 }
193 return 0;
194
195inst_del_option:
196 __team_option_inst_del_option(team, option);
197 return err;
198}
199
200static void __team_option_inst_mark_removed_option(struct team *team,
201 struct team_option *option)
202{
203 struct team_option_inst *opt_inst;
204
205 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
206 if (opt_inst->option == option) {
207 opt_inst->changed = true;
208 opt_inst->removed = true;
209 }
210 }
211}
212
213static void __team_option_inst_del_port(struct team *team,
214 struct team_port *port)
215{
216 struct team_option_inst *opt_inst, *tmp;
217
218 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
219 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000220 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000221 __team_option_inst_del(opt_inst);
222 }
223}
224
225static int __team_option_inst_add_port(struct team *team,
226 struct team_port *port)
227{
228 struct team_option *option;
229 int err;
230
231 list_for_each_entry(option, &team->option_list, list) {
232 if (!option->per_port)
233 continue;
234 err = __team_option_inst_add(team, option, port);
235 if (err)
236 goto inst_del_port;
237 }
238 return 0;
239
240inst_del_port:
241 __team_option_inst_del_port(team, port);
242 return err;
243}
244
245static void __team_option_inst_mark_removed_port(struct team *team,
246 struct team_port *port)
247{
248 struct team_option_inst *opt_inst;
249
250 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000251 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000252 opt_inst->changed = true;
253 opt_inst->removed = true;
254 }
255 }
256}
257
258static int __team_options_register(struct team *team,
259 const struct team_option *option,
260 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000261{
262 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000263 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000264 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000265
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000266 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
267 GFP_KERNEL);
268 if (!dst_opts)
269 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000270 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000271 if (__team_find_option(team, option->name)) {
272 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000273 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000274 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000275 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
276 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000277 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000278 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000279 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000280 }
281
Jiri Pirkob82b9182012-01-24 05:16:00 +0000282 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000283 err = __team_option_inst_add_option(team, dst_opts[i]);
284 if (err)
285 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000286 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000287 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000288
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000289 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000290 return 0;
291
Jiri Pirko80f7c662012-04-10 05:15:42 +0000292inst_rollback:
293 for (i--; i >= 0; i--)
294 __team_option_inst_del_option(team, dst_opts[i]);
295
296 i = option_count - 1;
297alloc_rollback:
298 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000299 kfree(dst_opts[i]);
300
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000301 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000302 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000303}
Jiri Pirko358b8382011-11-16 11:09:09 +0000304
Jiri Pirkob82b9182012-01-24 05:16:00 +0000305static void __team_options_mark_removed(struct team *team,
306 const struct team_option *option,
307 size_t option_count)
308{
309 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000310
Jiri Pirkob82b9182012-01-24 05:16:00 +0000311 for (i = 0; i < option_count; i++, option++) {
312 struct team_option *del_opt;
313
314 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000315 if (del_opt)
316 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000317 }
318}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000319
320static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000321 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000322 size_t option_count)
323{
324 int i;
325
Jiri Pirko358b8382011-11-16 11:09:09 +0000326 for (i = 0; i < option_count; i++, option++) {
327 struct team_option *del_opt;
328
329 del_opt = __team_find_option(team, option->name);
330 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000331 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000332 list_del(&del_opt->list);
333 kfree(del_opt);
334 }
335 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000336}
337
Jiri Pirkob82b9182012-01-24 05:16:00 +0000338static void __team_options_change_check(struct team *team);
339
340int team_options_register(struct team *team,
341 const struct team_option *option,
342 size_t option_count)
343{
344 int err;
345
346 err = __team_options_register(team, option, option_count);
347 if (err)
348 return err;
349 __team_options_change_check(team);
350 return 0;
351}
352EXPORT_SYMBOL(team_options_register);
353
Jiri Pirko358b8382011-11-16 11:09:09 +0000354void team_options_unregister(struct team *team,
355 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000356 size_t option_count)
357{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000358 __team_options_mark_removed(team, option, option_count);
359 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000360 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000361}
362EXPORT_SYMBOL(team_options_unregister);
363
Jiri Pirko80f7c662012-04-10 05:15:42 +0000364static int team_option_get(struct team *team,
365 struct team_option_inst *opt_inst,
366 struct team_gsetter_ctx *ctx)
367{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000368 if (!opt_inst->option->getter)
369 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000370 return opt_inst->option->getter(team, ctx);
371}
372
373static int team_option_set(struct team *team,
374 struct team_option_inst *opt_inst,
375 struct team_gsetter_ctx *ctx)
376{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000377 if (!opt_inst->option->setter)
378 return -EOPNOTSUPP;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +0000379 return opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000380}
381
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000382void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
383{
384 struct team_option_inst *opt_inst;
385
386 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
387 opt_inst->changed = true;
388}
389EXPORT_SYMBOL(team_option_inst_set_change);
390
391void team_options_change_check(struct team *team)
392{
393 __team_options_change_check(team);
394}
395EXPORT_SYMBOL(team_options_change_check);
396
397
Jiri Pirko3d249d42011-11-11 22:16:48 +0000398/****************
399 * Mode handling
400 ****************/
401
402static LIST_HEAD(mode_list);
403static DEFINE_SPINLOCK(mode_list_lock);
404
Jiri Pirko04027882012-06-19 05:54:03 +0000405struct team_mode_item {
406 struct list_head list;
407 const struct team_mode *mode;
408};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000409
Jiri Pirko04027882012-06-19 05:54:03 +0000410static struct team_mode_item *__find_mode(const char *kind)
411{
412 struct team_mode_item *mitem;
413
414 list_for_each_entry(mitem, &mode_list, list) {
415 if (strcmp(mitem->mode->kind, kind) == 0)
416 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000417 }
418 return NULL;
419}
420
421static bool is_good_mode_name(const char *name)
422{
423 while (*name != '\0') {
424 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
425 return false;
426 name++;
427 }
428 return true;
429}
430
Jiri Pirko04027882012-06-19 05:54:03 +0000431int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000432{
433 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000434 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000435
436 if (!is_good_mode_name(mode->kind) ||
437 mode->priv_size > TEAM_MODE_PRIV_SIZE)
438 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000439
440 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
441 if (!mitem)
442 return -ENOMEM;
443
Jiri Pirko3d249d42011-11-11 22:16:48 +0000444 spin_lock(&mode_list_lock);
445 if (__find_mode(mode->kind)) {
446 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000447 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000448 goto unlock;
449 }
Jiri Pirko04027882012-06-19 05:54:03 +0000450 mitem->mode = mode;
451 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000452unlock:
453 spin_unlock(&mode_list_lock);
454 return err;
455}
456EXPORT_SYMBOL(team_mode_register);
457
Jiri Pirko04027882012-06-19 05:54:03 +0000458void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000459{
Jiri Pirko04027882012-06-19 05:54:03 +0000460 struct team_mode_item *mitem;
461
Jiri Pirko3d249d42011-11-11 22:16:48 +0000462 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000463 mitem = __find_mode(mode->kind);
464 if (mitem) {
465 list_del_init(&mitem->list);
466 kfree(mitem);
467 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000468 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000469}
470EXPORT_SYMBOL(team_mode_unregister);
471
Jiri Pirko04027882012-06-19 05:54:03 +0000472static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000473{
Jiri Pirko04027882012-06-19 05:54:03 +0000474 struct team_mode_item *mitem;
475 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000476
477 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000478 mitem = __find_mode(kind);
479 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000480 spin_unlock(&mode_list_lock);
481 request_module("team-mode-%s", kind);
482 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000483 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000484 }
Jiri Pirko04027882012-06-19 05:54:03 +0000485 if (mitem) {
486 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000487 if (!try_module_get(mode->owner))
488 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000489 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000490
491 spin_unlock(&mode_list_lock);
492 return mode;
493}
494
495static void team_mode_put(const struct team_mode *mode)
496{
497 module_put(mode->owner);
498}
499
500static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
501{
502 dev_kfree_skb_any(skb);
503 return false;
504}
505
stephen hemminger2ea46592013-03-08 09:07:43 +0000506static rx_handler_result_t team_dummy_receive(struct team *team,
507 struct team_port *port,
508 struct sk_buff *skb)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000509{
510 return RX_HANDLER_ANOTHER;
511}
512
Jiri Pirkod299cd52012-06-19 05:54:04 +0000513static const struct team_mode __team_no_mode = {
514 .kind = "*NOMODE*",
515};
516
517static bool team_is_mode_set(struct team *team)
518{
519 return team->mode != &__team_no_mode;
520}
521
522static void team_set_no_mode(struct team *team)
523{
Flavio Leitnere1854832013-02-05 09:30:55 +0000524 team->user_carrier_enabled = false;
Jiri Pirkod299cd52012-06-19 05:54:04 +0000525 team->mode = &__team_no_mode;
526}
527
Jiri Pirko735d3812013-06-10 17:42:25 +0200528static void team_adjust_ops(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000529{
530 /*
531 * To avoid checks in rx/tx skb paths, ensure here that non-null and
532 * correct ops are always set.
533 */
534
Jiri Pirko735d3812013-06-10 17:42:25 +0200535 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000536 !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000537 team->ops.transmit = team_dummy_transmit;
538 else
539 team->ops.transmit = team->mode->ops->transmit;
540
Jiri Pirko735d3812013-06-10 17:42:25 +0200541 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000542 !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000543 team->ops.receive = team_dummy_receive;
544 else
545 team->ops.receive = team->mode->ops->receive;
546}
547
548/*
549 * We can benefit from the fact that it's ensured no port is present
550 * at the time of mode change. Therefore no packets are in fly so there's no
551 * need to set mode operations in any special way.
552 */
553static int __team_change_mode(struct team *team,
554 const struct team_mode *new_mode)
555{
556 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000557 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000558 void (*exit_op)(struct team *team) = team->ops.exit;
559
560 /* Clear ops area so no callback is called any longer */
561 memset(&team->ops, 0, sizeof(struct team_mode_ops));
562 team_adjust_ops(team);
563
564 if (exit_op)
565 exit_op(team);
566 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000567 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000568 /* zero private data area */
569 memset(&team->mode_priv, 0,
570 sizeof(struct team) - offsetof(struct team, mode_priv));
571 }
572
573 if (!new_mode)
574 return 0;
575
576 if (new_mode->ops->init) {
577 int err;
578
579 err = new_mode->ops->init(team);
580 if (err)
581 return err;
582 }
583
584 team->mode = new_mode;
585 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
586 team_adjust_ops(team);
587
588 return 0;
589}
590
591static int team_change_mode(struct team *team, const char *kind)
592{
Jiri Pirko04027882012-06-19 05:54:03 +0000593 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000594 struct net_device *dev = team->dev;
595 int err;
596
597 if (!list_empty(&team->port_list)) {
598 netdev_err(dev, "No ports can be present during mode change\n");
599 return -EBUSY;
600 }
601
Jiri Pirkod299cd52012-06-19 05:54:04 +0000602 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000603 netdev_err(dev, "Unable to change to the same mode the team is in\n");
604 return -EINVAL;
605 }
606
607 new_mode = team_mode_get(kind);
608 if (!new_mode) {
609 netdev_err(dev, "Mode \"%s\" not found\n", kind);
610 return -EINVAL;
611 }
612
613 err = __team_change_mode(team, new_mode);
614 if (err) {
615 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
616 team_mode_put(new_mode);
617 return err;
618 }
619
620 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
621 return 0;
622}
623
624
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200625/*********************
626 * Peers notification
627 *********************/
628
629static void team_notify_peers_work(struct work_struct *work)
630{
631 struct team *team;
632
633 team = container_of(work, struct team, notify_peers.dw.work);
634
635 if (!rtnl_trylock()) {
636 schedule_delayed_work(&team->notify_peers.dw, 0);
637 return;
638 }
639 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
640 rtnl_unlock();
641 if (!atomic_dec_and_test(&team->notify_peers.count_pending))
642 schedule_delayed_work(&team->notify_peers.dw,
643 msecs_to_jiffies(team->notify_peers.interval));
644}
645
646static void team_notify_peers(struct team *team)
647{
648 if (!team->notify_peers.count || !netif_running(team->dev))
649 return;
650 atomic_set(&team->notify_peers.count_pending, team->notify_peers.count);
651 schedule_delayed_work(&team->notify_peers.dw, 0);
652}
653
654static void team_notify_peers_init(struct team *team)
655{
656 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
657}
658
659static void team_notify_peers_fini(struct team *team)
660{
661 cancel_delayed_work_sync(&team->notify_peers.dw);
662}
663
664
Jiri Pirko3d249d42011-11-11 22:16:48 +0000665/************************
666 * Rx path frame handler
667 ************************/
668
669/* note: already called with rcu_read_lock */
670static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
671{
672 struct sk_buff *skb = *pskb;
673 struct team_port *port;
674 struct team *team;
675 rx_handler_result_t res;
676
677 skb = skb_share_check(skb, GFP_ATOMIC);
678 if (!skb)
679 return RX_HANDLER_CONSUMED;
680
681 *pskb = skb;
682
683 port = team_port_get_rcu(skb->dev);
684 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000685 if (!team_port_enabled(port)) {
686 /* allow exact match delivery for disabled ports */
687 res = RX_HANDLER_EXACT;
688 } else {
689 res = team->ops.receive(team, port, skb);
690 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000691 if (res == RX_HANDLER_ANOTHER) {
692 struct team_pcpu_stats *pcpu_stats;
693
694 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
695 u64_stats_update_begin(&pcpu_stats->syncp);
696 pcpu_stats->rx_packets++;
697 pcpu_stats->rx_bytes += skb->len;
698 if (skb->pkt_type == PACKET_MULTICAST)
699 pcpu_stats->rx_multicast++;
700 u64_stats_update_end(&pcpu_stats->syncp);
701
702 skb->dev = team->dev;
703 } else {
704 this_cpu_inc(team->pcpu_stats->rx_dropped);
705 }
706
707 return res;
708}
709
710
Jiri Pirko8ff51052012-07-27 06:28:55 +0000711/*************************************
712 * Multiqueue Tx port select override
713 *************************************/
714
715static int team_queue_override_init(struct team *team)
716{
717 struct list_head *listarr;
718 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
719 unsigned int i;
720
721 if (!queue_cnt)
722 return 0;
723 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
724 if (!listarr)
725 return -ENOMEM;
726 team->qom_lists = listarr;
727 for (i = 0; i < queue_cnt; i++)
728 INIT_LIST_HEAD(listarr++);
729 return 0;
730}
731
732static void team_queue_override_fini(struct team *team)
733{
734 kfree(team->qom_lists);
735}
736
737static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
738{
739 return &team->qom_lists[queue_id - 1];
740}
741
742/*
743 * note: already called with rcu_read_lock
744 */
745static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
746{
747 struct list_head *qom_list;
748 struct team_port *port;
749
750 if (!team->queue_override_enabled || !skb->queue_mapping)
751 return false;
752 qom_list = __team_get_qom_list(team, skb->queue_mapping);
753 list_for_each_entry_rcu(port, qom_list, qom_list) {
754 if (!team_dev_queue_xmit(team, port, skb))
755 return true;
756 }
757 return false;
758}
759
760static void __team_queue_override_port_del(struct team *team,
761 struct team_port *port)
762{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200763 if (!port->queue_id)
764 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000765 list_del_rcu(&port->qom_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +0000766}
767
768static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
769 struct team_port *cur)
770{
771 if (port->priority < cur->priority)
772 return true;
773 if (port->priority > cur->priority)
774 return false;
775 if (port->index < cur->index)
776 return true;
777 return false;
778}
779
780static void __team_queue_override_port_add(struct team *team,
781 struct team_port *port)
782{
783 struct team_port *cur;
784 struct list_head *qom_list;
785 struct list_head *node;
786
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200787 if (!port->queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000788 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000789 qom_list = __team_get_qom_list(team, port->queue_id);
790 node = qom_list;
791 list_for_each_entry(cur, qom_list, qom_list) {
792 if (team_queue_override_port_has_gt_prio_than(port, cur))
793 break;
794 node = &cur->qom_list;
795 }
796 list_add_tail_rcu(&port->qom_list, node);
797}
798
799static void __team_queue_override_enabled_check(struct team *team)
800{
801 struct team_port *port;
802 bool enabled = false;
803
804 list_for_each_entry(port, &team->port_list, list) {
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200805 if (port->queue_id) {
Jiri Pirko8ff51052012-07-27 06:28:55 +0000806 enabled = true;
807 break;
808 }
809 }
810 if (enabled == team->queue_override_enabled)
811 return;
812 netdev_dbg(team->dev, "%s queue override\n",
813 enabled ? "Enabling" : "Disabling");
814 team->queue_override_enabled = enabled;
815}
816
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200817static void team_queue_override_port_prio_changed(struct team *team,
818 struct team_port *port)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000819{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200820 if (!port->queue_id || team_port_enabled(port))
821 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000822 __team_queue_override_port_del(team, port);
823 __team_queue_override_port_add(team, port);
824 __team_queue_override_enabled_check(team);
825}
826
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200827static void team_queue_override_port_change_queue_id(struct team *team,
828 struct team_port *port,
829 u16 new_queue_id)
830{
831 if (team_port_enabled(port)) {
832 __team_queue_override_port_del(team, port);
833 port->queue_id = new_queue_id;
834 __team_queue_override_port_add(team, port);
835 __team_queue_override_enabled_check(team);
836 } else {
837 port->queue_id = new_queue_id;
838 }
839}
840
841static void team_queue_override_port_add(struct team *team,
842 struct team_port *port)
843{
844 __team_queue_override_port_add(team, port);
845 __team_queue_override_enabled_check(team);
846}
847
848static void team_queue_override_port_del(struct team *team,
849 struct team_port *port)
850{
851 __team_queue_override_port_del(team, port);
852 __team_queue_override_enabled_check(team);
853}
854
Jiri Pirko8ff51052012-07-27 06:28:55 +0000855
Jiri Pirko3d249d42011-11-11 22:16:48 +0000856/****************
857 * Port handling
858 ****************/
859
860static bool team_port_find(const struct team *team,
861 const struct team_port *port)
862{
863 struct team_port *cur;
864
865 list_for_each_entry(cur, &team->port_list, list)
866 if (cur == port)
867 return true;
868 return false;
869}
870
Jiri Pirko19a0b582012-04-20 04:42:05 +0000871/*
872 * Enable/disable port by adding to enabled port hashlist and setting
873 * port->index (Might be racy so reader could see incorrect ifindex when
874 * processing a flying packet, but that is not a problem). Write guarded
875 * by team->lock.
876 */
877static void team_port_enable(struct team *team,
878 struct team_port *port)
879{
880 if (team_port_enabled(port))
881 return;
882 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000883 hlist_add_head_rcu(&port->hlist,
884 team_port_index_hash(team, port->index));
Jiri Pirko122bb042012-06-26 06:52:45 +0000885 team_adjust_ops(team);
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200886 team_queue_override_port_add(team, port);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000887 if (team->ops.port_enabled)
888 team->ops.port_enabled(team, port);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200889 team_notify_peers(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000890}
891
892static void __reconstruct_port_hlist(struct team *team, int rm_index)
893{
894 int i;
895 struct team_port *port;
896
Jiri Pirko19a0b582012-04-20 04:42:05 +0000897 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000898 port = team_get_port_by_index(team, i);
899 hlist_del_rcu(&port->hlist);
900 port->index--;
901 hlist_add_head_rcu(&port->hlist,
902 team_port_index_hash(team, port->index));
903 }
904}
905
Jiri Pirko19a0b582012-04-20 04:42:05 +0000906static void team_port_disable(struct team *team,
907 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000908{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000909 if (!team_port_enabled(port))
910 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000911 if (team->ops.port_disabled)
912 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000913 hlist_del_rcu(&port->hlist);
Jiri Pirko122bb042012-06-26 06:52:45 +0000914 __reconstruct_port_hlist(team, port->index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000915 port->index = -1;
Jiri Pirko122bb042012-06-26 06:52:45 +0000916 team->en_port_count--;
Jiri Pirko735d3812013-06-10 17:42:25 +0200917 team_queue_override_port_del(team, port);
918 team_adjust_ops(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200919 team_notify_peers(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000920}
921
922#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
923 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
924 NETIF_F_HIGHDMA | NETIF_F_LRO)
925
926static void __team_compute_features(struct team *team)
927{
928 struct team_port *port;
929 u32 vlan_features = TEAM_VLAN_FEATURES;
930 unsigned short max_hard_header_len = ETH_HLEN;
Jiri Pirkodc905952012-07-18 07:39:38 +0000931 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000932
933 list_for_each_entry(port, &team->port_list, list) {
934 vlan_features = netdev_increment_features(vlan_features,
935 port->dev->vlan_features,
936 TEAM_VLAN_FEATURES);
937
Jiri Pirkodc905952012-07-18 07:39:38 +0000938 dst_release_flag &= port->dev->priv_flags;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000939 if (port->dev->hard_header_len > max_hard_header_len)
940 max_hard_header_len = port->dev->hard_header_len;
941 }
942
943 team->dev->vlan_features = vlan_features;
944 team->dev->hard_header_len = max_hard_header_len;
945
Jiri Pirkodc905952012-07-18 07:39:38 +0000946 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
947 team->dev->priv_flags = flags | dst_release_flag;
948
Jiri Pirko3d249d42011-11-11 22:16:48 +0000949 netdev_change_features(team->dev);
950}
951
952static void team_compute_features(struct team *team)
953{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000954 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000955 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000956 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000957}
958
959static int team_port_enter(struct team *team, struct team_port *port)
960{
961 int err = 0;
962
963 dev_hold(team->dev);
964 port->dev->priv_flags |= IFF_TEAM_PORT;
965 if (team->ops.port_enter) {
966 err = team->ops.port_enter(team, port);
967 if (err) {
968 netdev_err(team->dev, "Device %s failed to enter team mode\n",
969 port->dev->name);
970 goto err_port_enter;
971 }
972 }
973
974 return 0;
975
976err_port_enter:
977 port->dev->priv_flags &= ~IFF_TEAM_PORT;
978 dev_put(team->dev);
979
980 return err;
981}
982
983static void team_port_leave(struct team *team, struct team_port *port)
984{
985 if (team->ops.port_leave)
986 team->ops.port_leave(team, port);
987 port->dev->priv_flags &= ~IFF_TEAM_PORT;
988 dev_put(team->dev);
989}
990
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000991#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang47be03a22012-08-10 01:24:37 +0000992static int team_port_enable_netpoll(struct team *team, struct team_port *port,
993 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000994{
995 struct netpoll *np;
996 int err;
997
Amerigo Wang47be03a22012-08-10 01:24:37 +0000998 np = kzalloc(sizeof(*np), gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000999 if (!np)
1000 return -ENOMEM;
1001
Amerigo Wang47be03a22012-08-10 01:24:37 +00001002 err = __netpoll_setup(np, port->dev, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001003 if (err) {
1004 kfree(np);
1005 return err;
1006 }
1007 port->np = np;
1008 return err;
1009}
1010
1011static void team_port_disable_netpoll(struct team_port *port)
1012{
1013 struct netpoll *np = port->np;
1014
1015 if (!np)
1016 return;
1017 port->np = NULL;
1018
1019 /* Wait for transmitting packets to finish before freeing. */
1020 synchronize_rcu_bh();
1021 __netpoll_cleanup(np);
1022 kfree(np);
1023}
1024
1025static struct netpoll_info *team_netpoll_info(struct team *team)
1026{
1027 return team->dev->npinfo;
1028}
1029
1030#else
Amerigo Wang47be03a22012-08-10 01:24:37 +00001031static int team_port_enable_netpoll(struct team *team, struct team_port *port,
1032 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001033{
1034 return 0;
1035}
1036static void team_port_disable_netpoll(struct team_port *port)
1037{
1038}
1039static struct netpoll_info *team_netpoll_info(struct team *team)
1040{
1041 return NULL;
1042}
1043#endif
1044
Jiri Pirko10f3f512012-09-21 01:51:59 +00001045static void __team_port_change_port_added(struct team_port *port, bool linkup);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001046static int team_dev_type_check_change(struct net_device *dev,
1047 struct net_device *port_dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001048
1049static int team_port_add(struct team *team, struct net_device *port_dev)
1050{
1051 struct net_device *dev = team->dev;
1052 struct team_port *port;
1053 char *portname = port_dev->name;
1054 int err;
1055
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001056 if (port_dev->flags & IFF_LOOPBACK) {
1057 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
Jiri Pirko3d249d42011-11-11 22:16:48 +00001058 portname);
1059 return -EINVAL;
1060 }
1061
1062 if (team_port_exists(port_dev)) {
1063 netdev_err(dev, "Device %s is already a port "
1064 "of a team device\n", portname);
1065 return -EBUSY;
1066 }
1067
Jiri Pirko2c33bb32012-08-23 03:26:53 +00001068 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1069 vlan_uses_dev(dev)) {
1070 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1071 portname);
1072 return -EPERM;
1073 }
1074
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001075 err = team_dev_type_check_change(dev, port_dev);
1076 if (err)
1077 return err;
1078
Jiri Pirko3d249d42011-11-11 22:16:48 +00001079 if (port_dev->flags & IFF_UP) {
1080 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1081 portname);
1082 return -EBUSY;
1083 }
1084
Jiri Pirko5149ee52012-06-19 05:54:05 +00001085 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1086 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001087 if (!port)
1088 return -ENOMEM;
1089
1090 port->dev = port_dev;
1091 port->team = team;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001092 INIT_LIST_HEAD(&port->qom_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001093
1094 port->orig.mtu = port_dev->mtu;
1095 err = dev_set_mtu(port_dev, dev->mtu);
1096 if (err) {
1097 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1098 goto err_set_mtu;
1099 }
1100
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001101 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001102
1103 err = team_port_enter(team, port);
1104 if (err) {
1105 netdev_err(dev, "Device %s failed to enter team mode\n",
1106 portname);
1107 goto err_port_enter;
1108 }
1109
1110 err = dev_open(port_dev);
1111 if (err) {
1112 netdev_dbg(dev, "Device %s opening failed\n",
1113 portname);
1114 goto err_dev_open;
1115 }
1116
Jiri Pirko57459182011-12-08 04:11:20 +00001117 err = vlan_vids_add_by_dev(port_dev, dev);
1118 if (err) {
1119 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1120 portname);
1121 goto err_vids_add;
1122 }
1123
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001124 if (team_netpoll_info(team)) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001125 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001126 if (err) {
1127 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1128 portname);
1129 goto err_enable_netpoll;
1130 }
1131 }
1132
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001133 err = netdev_master_upper_dev_link(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001134 if (err) {
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001135 netdev_err(dev, "Device %s failed to set upper link\n",
1136 portname);
1137 goto err_set_upper_link;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001138 }
1139
1140 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1141 port);
1142 if (err) {
1143 netdev_err(dev, "Device %s failed to register rx_handler\n",
1144 portname);
1145 goto err_handler_register;
1146 }
1147
Jiri Pirko35b384b2012-06-19 05:54:19 +00001148 err = __team_option_inst_add_port(team, port);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001149 if (err) {
1150 netdev_err(dev, "Device %s failed to add per-port options\n",
1151 portname);
1152 goto err_option_port_add;
1153 }
1154
Jiri Pirko19a0b582012-04-20 04:42:05 +00001155 port->index = -1;
Jiri Pirko19a0b582012-04-20 04:42:05 +00001156 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko72df9352013-06-08 15:00:54 +02001157 team_port_enable(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001158 __team_compute_features(team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00001159 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
Jiri Pirko35b384b2012-06-19 05:54:19 +00001160 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001161
1162 netdev_info(dev, "Port device %s added\n", portname);
1163
1164 return 0;
1165
Jiri Pirko80f7c662012-04-10 05:15:42 +00001166err_option_port_add:
1167 netdev_rx_handler_unregister(port_dev);
1168
Jiri Pirko3d249d42011-11-11 22:16:48 +00001169err_handler_register:
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001170 netdev_upper_dev_unlink(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001171
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001172err_set_upper_link:
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001173 team_port_disable_netpoll(port);
1174
1175err_enable_netpoll:
Jiri Pirko57459182011-12-08 04:11:20 +00001176 vlan_vids_del_by_dev(port_dev, dev);
1177
1178err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001179 dev_close(port_dev);
1180
1181err_dev_open:
1182 team_port_leave(team, port);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001183 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001184
1185err_port_enter:
1186 dev_set_mtu(port_dev, port->orig.mtu);
1187
1188err_set_mtu:
1189 kfree(port);
1190
1191 return err;
1192}
1193
Jiri Pirko10f3f512012-09-21 01:51:59 +00001194static void __team_port_change_port_removed(struct team_port *port);
1195
Jiri Pirko3d249d42011-11-11 22:16:48 +00001196static int team_port_del(struct team *team, struct net_device *port_dev)
1197{
1198 struct net_device *dev = team->dev;
1199 struct team_port *port;
1200 char *portname = port_dev->name;
1201
1202 port = team_port_get_rtnl(port_dev);
1203 if (!port || !team_port_find(team, port)) {
1204 netdev_err(dev, "Device %s does not act as a port of this team\n",
1205 portname);
1206 return -ENOENT;
1207 }
1208
Jiri Pirko19a0b582012-04-20 04:42:05 +00001209 team_port_disable(team, port);
1210 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001211 netdev_rx_handler_unregister(port_dev);
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001212 netdev_upper_dev_unlink(port_dev, dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001213 team_port_disable_netpoll(port);
Jiri Pirko57459182011-12-08 04:11:20 +00001214 vlan_vids_del_by_dev(port_dev, dev);
Vlad Yasevichba812762013-03-07 07:59:25 +00001215 dev_uc_unsync(port_dev, dev);
1216 dev_mc_unsync(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001217 dev_close(port_dev);
1218 team_port_leave(team, port);
Jiri Pirkoc3969d802013-02-01 08:17:25 +00001219
1220 __team_option_inst_mark_removed_port(team, port);
1221 __team_options_change_check(team);
1222 __team_option_inst_del_port(team, port);
1223 __team_port_change_port_removed(port);
1224
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001225 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001226 dev_set_mtu(port_dev, port->orig.mtu);
Jiri Pirkod80b35b2013-06-10 17:42:24 +02001227 kfree_rcu(port, rcu);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001228 netdev_info(dev, "Port device %s removed\n", portname);
1229 __team_compute_features(team);
1230
1231 return 0;
1232}
1233
1234
1235/*****************
1236 * Net device ops
1237 *****************/
1238
Jiri Pirko80f7c662012-04-10 05:15:42 +00001239static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001240{
Jiri Pirkod299cd52012-06-19 05:54:04 +00001241 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001242 return 0;
1243}
1244
Jiri Pirko80f7c662012-04-10 05:15:42 +00001245static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001246{
Jiri Pirko80f7c662012-04-10 05:15:42 +00001247 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001248}
1249
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001250static int team_notify_peers_count_get(struct team *team,
1251 struct team_gsetter_ctx *ctx)
1252{
1253 ctx->data.u32_val = team->notify_peers.count;
1254 return 0;
1255}
1256
1257static int team_notify_peers_count_set(struct team *team,
1258 struct team_gsetter_ctx *ctx)
1259{
1260 team->notify_peers.count = ctx->data.u32_val;
1261 return 0;
1262}
1263
1264static int team_notify_peers_interval_get(struct team *team,
1265 struct team_gsetter_ctx *ctx)
1266{
1267 ctx->data.u32_val = team->notify_peers.interval;
1268 return 0;
1269}
1270
1271static int team_notify_peers_interval_set(struct team *team,
1272 struct team_gsetter_ctx *ctx)
1273{
1274 team->notify_peers.interval = ctx->data.u32_val;
1275 return 0;
1276}
1277
Jiri Pirkoacd69962012-04-20 04:42:06 +00001278static int team_port_en_option_get(struct team *team,
1279 struct team_gsetter_ctx *ctx)
1280{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001281 struct team_port *port = ctx->info->port;
1282
1283 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001284 return 0;
1285}
1286
1287static int team_port_en_option_set(struct team *team,
1288 struct team_gsetter_ctx *ctx)
1289{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001290 struct team_port *port = ctx->info->port;
1291
Jiri Pirkoacd69962012-04-20 04:42:06 +00001292 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001293 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001294 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001295 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001296 return 0;
1297}
1298
Jiri Pirko71472ec2012-04-10 05:15:44 +00001299static int team_user_linkup_option_get(struct team *team,
1300 struct team_gsetter_ctx *ctx)
1301{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001302 struct team_port *port = ctx->info->port;
1303
1304 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001305 return 0;
1306}
1307
1308static int team_user_linkup_option_set(struct team *team,
1309 struct team_gsetter_ctx *ctx)
1310{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001311 struct team_port *port = ctx->info->port;
1312
1313 port->user.linkup = ctx->data.bool_val;
1314 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001315 return 0;
1316}
1317
1318static int team_user_linkup_en_option_get(struct team *team,
1319 struct team_gsetter_ctx *ctx)
1320{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001321 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001322
1323 ctx->data.bool_val = port->user.linkup_enabled;
1324 return 0;
1325}
1326
1327static int team_user_linkup_en_option_set(struct team *team,
1328 struct team_gsetter_ctx *ctx)
1329{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001330 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001331
1332 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001333 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001334 return 0;
1335}
1336
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001337static int team_priority_option_get(struct team *team,
1338 struct team_gsetter_ctx *ctx)
1339{
1340 struct team_port *port = ctx->info->port;
1341
1342 ctx->data.s32_val = port->priority;
1343 return 0;
1344}
1345
1346static int team_priority_option_set(struct team *team,
1347 struct team_gsetter_ctx *ctx)
1348{
1349 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001350 s32 priority = ctx->data.s32_val;
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001351
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001352 if (port->priority == priority)
1353 return 0;
1354 port->priority = priority;
1355 team_queue_override_port_prio_changed(team, port);
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001356 return 0;
1357}
1358
Jiri Pirko8ff51052012-07-27 06:28:55 +00001359static int team_queue_id_option_get(struct team *team,
1360 struct team_gsetter_ctx *ctx)
1361{
1362 struct team_port *port = ctx->info->port;
1363
1364 ctx->data.u32_val = port->queue_id;
1365 return 0;
1366}
1367
1368static int team_queue_id_option_set(struct team *team,
1369 struct team_gsetter_ctx *ctx)
1370{
1371 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001372 u16 new_queue_id = ctx->data.u32_val;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001373
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001374 if (port->queue_id == new_queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001375 return 0;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001376 if (new_queue_id >= team->dev->real_num_tx_queues)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001377 return -EINVAL;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001378 team_queue_override_port_change_queue_id(team, port, new_queue_id);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001379 return 0;
1380}
1381
Jiri Pirko358b8382011-11-16 11:09:09 +00001382static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001383 {
1384 .name = "mode",
1385 .type = TEAM_OPTION_TYPE_STRING,
1386 .getter = team_mode_option_get,
1387 .setter = team_mode_option_set,
1388 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001389 {
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001390 .name = "notify_peers_count",
1391 .type = TEAM_OPTION_TYPE_U32,
1392 .getter = team_notify_peers_count_get,
1393 .setter = team_notify_peers_count_set,
1394 },
1395 {
1396 .name = "notify_peers_interval",
1397 .type = TEAM_OPTION_TYPE_U32,
1398 .getter = team_notify_peers_interval_get,
1399 .setter = team_notify_peers_interval_set,
1400 },
1401 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001402 .name = "enabled",
1403 .type = TEAM_OPTION_TYPE_BOOL,
1404 .per_port = true,
1405 .getter = team_port_en_option_get,
1406 .setter = team_port_en_option_set,
1407 },
1408 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001409 .name = "user_linkup",
1410 .type = TEAM_OPTION_TYPE_BOOL,
1411 .per_port = true,
1412 .getter = team_user_linkup_option_get,
1413 .setter = team_user_linkup_option_set,
1414 },
1415 {
1416 .name = "user_linkup_enabled",
1417 .type = TEAM_OPTION_TYPE_BOOL,
1418 .per_port = true,
1419 .getter = team_user_linkup_en_option_get,
1420 .setter = team_user_linkup_en_option_set,
1421 },
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001422 {
1423 .name = "priority",
1424 .type = TEAM_OPTION_TYPE_S32,
1425 .per_port = true,
1426 .getter = team_priority_option_get,
1427 .setter = team_priority_option_set,
1428 },
Jiri Pirko8ff51052012-07-27 06:28:55 +00001429 {
1430 .name = "queue_id",
1431 .type = TEAM_OPTION_TYPE_U32,
1432 .per_port = true,
1433 .getter = team_queue_id_option_get,
1434 .setter = team_queue_id_option_set,
1435 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001436};
1437
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001438static struct lock_class_key team_netdev_xmit_lock_key;
1439static struct lock_class_key team_netdev_addr_lock_key;
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001440static struct lock_class_key team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001441
1442static void team_set_lockdep_class_one(struct net_device *dev,
1443 struct netdev_queue *txq,
1444 void *unused)
1445{
1446 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1447}
1448
1449static void team_set_lockdep_class(struct net_device *dev)
1450{
1451 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1452 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001453 dev->qdisc_tx_busylock = &team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001454}
1455
Jiri Pirko3d249d42011-11-11 22:16:48 +00001456static int team_init(struct net_device *dev)
1457{
1458 struct team *team = netdev_priv(dev);
1459 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001460 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001461
1462 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001463 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001464 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001465
1466 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1467 if (!team->pcpu_stats)
1468 return -ENOMEM;
1469
1470 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001471 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001472 INIT_LIST_HEAD(&team->port_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001473 err = team_queue_override_init(team);
1474 if (err)
1475 goto err_team_queue_override_init;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001476
1477 team_adjust_ops(team);
1478
1479 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001480 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001481
1482 team_notify_peers_init(team);
1483
Jiri Pirko358b8382011-11-16 11:09:09 +00001484 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1485 if (err)
1486 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001487 netif_carrier_off(dev);
1488
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001489 team_set_lockdep_class(dev);
1490
Jiri Pirko3d249d42011-11-11 22:16:48 +00001491 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001492
1493err_options_register:
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001494 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001495 team_queue_override_fini(team);
1496err_team_queue_override_init:
Jiri Pirko358b8382011-11-16 11:09:09 +00001497 free_percpu(team->pcpu_stats);
1498
1499 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001500}
1501
1502static void team_uninit(struct net_device *dev)
1503{
1504 struct team *team = netdev_priv(dev);
1505 struct team_port *port;
1506 struct team_port *tmp;
1507
Jiri Pirko61dc3462011-11-16 11:09:08 +00001508 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001509 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1510 team_port_del(team, port->dev);
1511
1512 __team_change_mode(team, NULL); /* cleanup */
1513 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001514 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001515 team_queue_override_fini(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001516 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001517}
1518
1519static void team_destructor(struct net_device *dev)
1520{
1521 struct team *team = netdev_priv(dev);
1522
1523 free_percpu(team->pcpu_stats);
1524 free_netdev(dev);
1525}
1526
1527static int team_open(struct net_device *dev)
1528{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001529 return 0;
1530}
1531
1532static int team_close(struct net_device *dev)
1533{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001534 return 0;
1535}
1536
1537/*
1538 * note: already called with rcu_read_lock
1539 */
1540static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1541{
1542 struct team *team = netdev_priv(dev);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001543 bool tx_success;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001544 unsigned int len = skb->len;
1545
Jiri Pirko8ff51052012-07-27 06:28:55 +00001546 tx_success = team_queue_override_transmit(team, skb);
1547 if (!tx_success)
1548 tx_success = team->ops.transmit(team, skb);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001549 if (tx_success) {
1550 struct team_pcpu_stats *pcpu_stats;
1551
1552 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1553 u64_stats_update_begin(&pcpu_stats->syncp);
1554 pcpu_stats->tx_packets++;
1555 pcpu_stats->tx_bytes += len;
1556 u64_stats_update_end(&pcpu_stats->syncp);
1557 } else {
1558 this_cpu_inc(team->pcpu_stats->tx_dropped);
1559 }
1560
1561 return NETDEV_TX_OK;
1562}
1563
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001564static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1565{
1566 /*
1567 * This helper function exists to help dev_pick_tx get the correct
1568 * destination queue. Using a helper function skips a call to
1569 * skb_tx_hash and will put the skbs in the queue we expect on their
1570 * way down to the team driver.
1571 */
1572 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1573
1574 /*
1575 * Save the original txq to restore before passing to the driver
1576 */
1577 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1578
1579 if (unlikely(txq >= dev->real_num_tx_queues)) {
1580 do {
1581 txq -= dev->real_num_tx_queues;
1582 } while (txq >= dev->real_num_tx_queues);
1583 }
1584 return txq;
1585}
1586
Jiri Pirko3d249d42011-11-11 22:16:48 +00001587static void team_change_rx_flags(struct net_device *dev, int change)
1588{
1589 struct team *team = netdev_priv(dev);
1590 struct team_port *port;
1591 int inc;
1592
1593 rcu_read_lock();
1594 list_for_each_entry_rcu(port, &team->port_list, list) {
1595 if (change & IFF_PROMISC) {
1596 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1597 dev_set_promiscuity(port->dev, inc);
1598 }
1599 if (change & IFF_ALLMULTI) {
1600 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1601 dev_set_allmulti(port->dev, inc);
1602 }
1603 }
1604 rcu_read_unlock();
1605}
1606
1607static void team_set_rx_mode(struct net_device *dev)
1608{
1609 struct team *team = netdev_priv(dev);
1610 struct team_port *port;
1611
1612 rcu_read_lock();
1613 list_for_each_entry_rcu(port, &team->port_list, list) {
Vlad Yasevich72b27032013-04-15 09:54:26 +00001614 dev_uc_sync_multiple(port->dev, dev);
1615 dev_mc_sync_multiple(port->dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001616 }
1617 rcu_read_unlock();
1618}
1619
1620static int team_set_mac_address(struct net_device *dev, void *p)
1621{
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001622 struct sockaddr *addr = p;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001623 struct team *team = netdev_priv(dev);
1624 struct team_port *port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001625
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001626 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1627 return -EADDRNOTAVAIL;
1628 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001629 rcu_read_lock();
1630 list_for_each_entry_rcu(port, &team->port_list, list)
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001631 if (team->ops.port_change_dev_addr)
1632 team->ops.port_change_dev_addr(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001633 rcu_read_unlock();
1634 return 0;
1635}
1636
1637static int team_change_mtu(struct net_device *dev, int new_mtu)
1638{
1639 struct team *team = netdev_priv(dev);
1640 struct team_port *port;
1641 int err;
1642
1643 /*
1644 * Alhough this is reader, it's guarded by team lock. It's not possible
1645 * to traverse list in reverse under rcu_read_lock
1646 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001647 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001648 list_for_each_entry(port, &team->port_list, list) {
1649 err = dev_set_mtu(port->dev, new_mtu);
1650 if (err) {
1651 netdev_err(dev, "Device %s failed to change mtu",
1652 port->dev->name);
1653 goto unwind;
1654 }
1655 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001656 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001657
1658 dev->mtu = new_mtu;
1659
1660 return 0;
1661
1662unwind:
1663 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1664 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001665 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001666
1667 return err;
1668}
1669
1670static struct rtnl_link_stats64 *
1671team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1672{
1673 struct team *team = netdev_priv(dev);
1674 struct team_pcpu_stats *p;
1675 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1676 u32 rx_dropped = 0, tx_dropped = 0;
1677 unsigned int start;
1678 int i;
1679
1680 for_each_possible_cpu(i) {
1681 p = per_cpu_ptr(team->pcpu_stats, i);
1682 do {
1683 start = u64_stats_fetch_begin_bh(&p->syncp);
1684 rx_packets = p->rx_packets;
1685 rx_bytes = p->rx_bytes;
1686 rx_multicast = p->rx_multicast;
1687 tx_packets = p->tx_packets;
1688 tx_bytes = p->tx_bytes;
1689 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1690
1691 stats->rx_packets += rx_packets;
1692 stats->rx_bytes += rx_bytes;
1693 stats->multicast += rx_multicast;
1694 stats->tx_packets += tx_packets;
1695 stats->tx_bytes += tx_bytes;
1696 /*
1697 * rx_dropped & tx_dropped are u32, updated
1698 * without syncp protection.
1699 */
1700 rx_dropped += p->rx_dropped;
1701 tx_dropped += p->tx_dropped;
1702 }
1703 stats->rx_dropped = rx_dropped;
1704 stats->tx_dropped = tx_dropped;
1705 return stats;
1706}
1707
Patrick McHardy80d5c362013-04-19 02:04:28 +00001708static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001709{
1710 struct team *team = netdev_priv(dev);
1711 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001712 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001713
Jiri Pirko87002b02011-12-08 04:11:17 +00001714 /*
1715 * Alhough this is reader, it's guarded by team lock. It's not possible
1716 * to traverse list in reverse under rcu_read_lock
1717 */
1718 mutex_lock(&team->lock);
1719 list_for_each_entry(port, &team->port_list, list) {
Patrick McHardy80d5c362013-04-19 02:04:28 +00001720 err = vlan_vid_add(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001721 if (err)
1722 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001723 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001724 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001725
1726 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001727
1728unwind:
1729 list_for_each_entry_continue_reverse(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001730 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001731 mutex_unlock(&team->lock);
1732
1733 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001734}
1735
Patrick McHardy80d5c362013-04-19 02:04:28 +00001736static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001737{
1738 struct team *team = netdev_priv(dev);
1739 struct team_port *port;
1740
1741 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001742 list_for_each_entry_rcu(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001743 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001744 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001745
1746 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001747}
1748
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001749#ifdef CONFIG_NET_POLL_CONTROLLER
1750static void team_poll_controller(struct net_device *dev)
1751{
1752}
1753
1754static void __team_netpoll_cleanup(struct team *team)
1755{
1756 struct team_port *port;
1757
1758 list_for_each_entry(port, &team->port_list, list)
1759 team_port_disable_netpoll(port);
1760}
1761
1762static void team_netpoll_cleanup(struct net_device *dev)
1763{
1764 struct team *team = netdev_priv(dev);
1765
1766 mutex_lock(&team->lock);
1767 __team_netpoll_cleanup(team);
1768 mutex_unlock(&team->lock);
1769}
1770
1771static int team_netpoll_setup(struct net_device *dev,
Amerigo Wang47be03a22012-08-10 01:24:37 +00001772 struct netpoll_info *npifo, gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001773{
1774 struct team *team = netdev_priv(dev);
1775 struct team_port *port;
Jiri Pirko3324d022012-07-24 01:20:48 +00001776 int err = 0;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001777
1778 mutex_lock(&team->lock);
1779 list_for_each_entry(port, &team->port_list, list) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001780 err = team_port_enable_netpoll(team, port, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001781 if (err) {
1782 __team_netpoll_cleanup(team);
1783 break;
1784 }
1785 }
1786 mutex_unlock(&team->lock);
1787 return err;
1788}
1789#endif
1790
Jiri Pirko3d249d42011-11-11 22:16:48 +00001791static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1792{
1793 struct team *team = netdev_priv(dev);
1794 int err;
1795
Jiri Pirko61dc3462011-11-16 11:09:08 +00001796 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001797 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001798 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001799 return err;
1800}
1801
1802static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1803{
1804 struct team *team = netdev_priv(dev);
1805 int err;
1806
Jiri Pirko61dc3462011-11-16 11:09:08 +00001807 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001808 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001809 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001810 return err;
1811}
1812
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001813static netdev_features_t team_fix_features(struct net_device *dev,
1814 netdev_features_t features)
1815{
1816 struct team_port *port;
1817 struct team *team = netdev_priv(dev);
1818 netdev_features_t mask;
1819
1820 mask = features;
1821 features &= ~NETIF_F_ONE_FOR_ALL;
1822 features |= NETIF_F_ALL_FOR_ALL;
1823
1824 rcu_read_lock();
1825 list_for_each_entry_rcu(port, &team->port_list, list) {
1826 features = netdev_increment_features(features,
1827 port->dev->features,
1828 mask);
1829 }
1830 rcu_read_unlock();
1831 return features;
1832}
1833
Flavio Leitner4cafe372012-12-29 15:31:01 +00001834static int team_change_carrier(struct net_device *dev, bool new_carrier)
1835{
Flavio Leitnere1854832013-02-05 09:30:55 +00001836 struct team *team = netdev_priv(dev);
1837
1838 team->user_carrier_enabled = true;
1839
Flavio Leitner4cafe372012-12-29 15:31:01 +00001840 if (new_carrier)
1841 netif_carrier_on(dev);
1842 else
1843 netif_carrier_off(dev);
1844 return 0;
1845}
1846
Jiri Pirko3d249d42011-11-11 22:16:48 +00001847static const struct net_device_ops team_netdev_ops = {
1848 .ndo_init = team_init,
1849 .ndo_uninit = team_uninit,
1850 .ndo_open = team_open,
1851 .ndo_stop = team_close,
1852 .ndo_start_xmit = team_xmit,
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001853 .ndo_select_queue = team_select_queue,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001854 .ndo_change_rx_flags = team_change_rx_flags,
1855 .ndo_set_rx_mode = team_set_rx_mode,
1856 .ndo_set_mac_address = team_set_mac_address,
1857 .ndo_change_mtu = team_change_mtu,
1858 .ndo_get_stats64 = team_get_stats64,
1859 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1860 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001861#ifdef CONFIG_NET_POLL_CONTROLLER
1862 .ndo_poll_controller = team_poll_controller,
1863 .ndo_netpoll_setup = team_netpoll_setup,
1864 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1865#endif
Jiri Pirko3d249d42011-11-11 22:16:48 +00001866 .ndo_add_slave = team_add_slave,
1867 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001868 .ndo_fix_features = team_fix_features,
Flavio Leitner4cafe372012-12-29 15:31:01 +00001869 .ndo_change_carrier = team_change_carrier,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001870};
1871
Flavio Leitner7f51c582012-12-29 16:37:33 +00001872/***********************
1873 * ethtool interface
1874 ***********************/
1875
1876static void team_ethtool_get_drvinfo(struct net_device *dev,
1877 struct ethtool_drvinfo *drvinfo)
1878{
Flavio Leitner3066af52013-01-05 02:53:10 +00001879 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1880 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
Flavio Leitner7f51c582012-12-29 16:37:33 +00001881}
1882
1883static const struct ethtool_ops team_ethtool_ops = {
1884 .get_drvinfo = team_ethtool_get_drvinfo,
1885 .get_link = ethtool_op_get_link,
1886};
Jiri Pirko3d249d42011-11-11 22:16:48 +00001887
1888/***********************
1889 * rt netlink interface
1890 ***********************/
1891
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001892static void team_setup_by_port(struct net_device *dev,
1893 struct net_device *port_dev)
1894{
1895 dev->header_ops = port_dev->header_ops;
1896 dev->type = port_dev->type;
1897 dev->hard_header_len = port_dev->hard_header_len;
1898 dev->addr_len = port_dev->addr_len;
1899 dev->mtu = port_dev->mtu;
1900 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
1901 memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001902}
1903
1904static int team_dev_type_check_change(struct net_device *dev,
1905 struct net_device *port_dev)
1906{
1907 struct team *team = netdev_priv(dev);
1908 char *portname = port_dev->name;
1909 int err;
1910
1911 if (dev->type == port_dev->type)
1912 return 0;
1913 if (!list_empty(&team->port_list)) {
1914 netdev_err(dev, "Device %s is of different type\n", portname);
1915 return -EBUSY;
1916 }
1917 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
1918 err = notifier_to_errno(err);
1919 if (err) {
1920 netdev_err(dev, "Refused to change device type\n");
1921 return err;
1922 }
1923 dev_uc_flush(dev);
1924 dev_mc_flush(dev);
1925 team_setup_by_port(dev, port_dev);
1926 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1927 return 0;
1928}
1929
Jiri Pirko3d249d42011-11-11 22:16:48 +00001930static void team_setup(struct net_device *dev)
1931{
1932 ether_setup(dev);
1933
1934 dev->netdev_ops = &team_netdev_ops;
Flavio Leitner7f51c582012-12-29 16:37:33 +00001935 dev->ethtool_ops = &team_ethtool_ops;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001936 dev->destructor = team_destructor;
1937 dev->tx_queue_len = 0;
1938 dev->flags |= IFF_MULTICAST;
1939 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1940
1941 /*
1942 * Indicate we support unicast address filtering. That way core won't
1943 * bring us to promisc mode in case a unicast addr is added.
1944 * Let this up to underlay drivers.
1945 */
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00001946 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001947
1948 dev->features |= NETIF_F_LLTX;
1949 dev->features |= NETIF_F_GRO;
Jiri Pirko3ed71472012-11-28 06:13:10 +00001950 dev->hw_features = TEAM_VLAN_FEATURES |
Patrick McHardyf6469682013-04-19 02:04:27 +00001951 NETIF_F_HW_VLAN_CTAG_TX |
1952 NETIF_F_HW_VLAN_CTAG_RX |
1953 NETIF_F_HW_VLAN_CTAG_FILTER;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001954
Jiri Pirko3ed71472012-11-28 06:13:10 +00001955 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001956 dev->features |= dev->hw_features;
1957}
1958
1959static int team_newlink(struct net *src_net, struct net_device *dev,
1960 struct nlattr *tb[], struct nlattr *data[])
1961{
1962 int err;
1963
1964 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001965 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001966
1967 err = register_netdevice(dev);
1968 if (err)
1969 return err;
1970
1971 return 0;
1972}
1973
1974static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1975{
1976 if (tb[IFLA_ADDRESS]) {
1977 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1978 return -EINVAL;
1979 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1980 return -EADDRNOTAVAIL;
1981 }
1982 return 0;
1983}
1984
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001985static unsigned int team_get_num_tx_queues(void)
1986{
1987 return TEAM_DEFAULT_NUM_TX_QUEUES;
1988}
1989
1990static unsigned int team_get_num_rx_queues(void)
1991{
1992 return TEAM_DEFAULT_NUM_RX_QUEUES;
1993}
1994
Jiri Pirko3d249d42011-11-11 22:16:48 +00001995static struct rtnl_link_ops team_link_ops __read_mostly = {
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001996 .kind = DRV_NAME,
1997 .priv_size = sizeof(struct team),
1998 .setup = team_setup,
1999 .newlink = team_newlink,
2000 .validate = team_validate,
2001 .get_num_tx_queues = team_get_num_tx_queues,
2002 .get_num_rx_queues = team_get_num_rx_queues,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002003};
2004
2005
2006/***********************************
2007 * Generic netlink custom interface
2008 ***********************************/
2009
2010static struct genl_family team_nl_family = {
2011 .id = GENL_ID_GENERATE,
2012 .name = TEAM_GENL_NAME,
2013 .version = TEAM_GENL_VERSION,
2014 .maxattr = TEAM_ATTR_MAX,
2015 .netnsok = true,
2016};
2017
2018static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2019 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2020 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2021 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2022 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2023};
2024
2025static const struct nla_policy
2026team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2027 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2028 [TEAM_ATTR_OPTION_NAME] = {
2029 .type = NLA_STRING,
2030 .len = TEAM_STRING_MAX_LEN,
2031 },
2032 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2033 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00002034 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00002035};
2036
2037static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2038{
2039 struct sk_buff *msg;
2040 void *hdr;
2041 int err;
2042
Thomas Graf58050fc2012-06-28 03:57:45 +00002043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002044 if (!msg)
2045 return -ENOMEM;
2046
Eric W. Biederman15e47302012-09-07 20:12:54 +00002047 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002048 &team_nl_family, 0, TEAM_CMD_NOOP);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002049 if (!hdr) {
2050 err = -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002051 goto err_msg_put;
2052 }
2053
2054 genlmsg_end(msg, hdr);
2055
Eric W. Biederman15e47302012-09-07 20:12:54 +00002056 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002057
2058err_msg_put:
2059 nlmsg_free(msg);
2060
2061 return err;
2062}
2063
2064/*
2065 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002066 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00002067 */
2068static struct team *team_nl_team_get(struct genl_info *info)
2069{
2070 struct net *net = genl_info_net(info);
2071 int ifindex;
2072 struct net_device *dev;
2073 struct team *team;
2074
2075 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2076 return NULL;
2077
2078 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002079 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002080 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002081 if (dev)
2082 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002083 return NULL;
2084 }
2085
2086 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002087 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002088 return team;
2089}
2090
2091static void team_nl_team_put(struct team *team)
2092{
Jiri Pirko61dc3462011-11-16 11:09:08 +00002093 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002094 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002095}
2096
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002097typedef int team_nl_send_func_t(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002098 struct team *team, u32 portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002099
Eric W. Biederman15e47302012-09-07 20:12:54 +00002100static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002101{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002102 return genlmsg_unicast(dev_net(team->dev), skb, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002103}
2104
Jiri Pirko01048d92012-06-19 05:54:14 +00002105static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2106 struct team_option_inst *opt_inst)
2107{
2108 struct nlattr *option_item;
2109 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002110 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00002111 struct team_gsetter_ctx ctx;
2112 int err;
2113
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002114 ctx.info = opt_inst_info;
2115 err = team_option_get(team, opt_inst, &ctx);
2116 if (err)
2117 return err;
2118
Jiri Pirko01048d92012-06-19 05:54:14 +00002119 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2120 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002121 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002122
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002123 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2124 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002125 if (opt_inst_info->port &&
2126 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2127 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002128 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002129 if (opt_inst->option->array_size &&
2130 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2131 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002132 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002133
2134 switch (option->type) {
2135 case TEAM_OPTION_TYPE_U32:
2136 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002137 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002138 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002139 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002140 break;
2141 case TEAM_OPTION_TYPE_STRING:
2142 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002143 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002144 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2145 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002146 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002147 break;
2148 case TEAM_OPTION_TYPE_BINARY:
2149 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002150 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002151 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2152 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002153 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002154 break;
2155 case TEAM_OPTION_TYPE_BOOL:
2156 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002157 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002158 if (ctx.data.bool_val &&
2159 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002160 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002161 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002162 case TEAM_OPTION_TYPE_S32:
2163 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2164 goto nest_cancel;
2165 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2166 goto nest_cancel;
2167 break;
Jiri Pirko01048d92012-06-19 05:54:14 +00002168 default:
2169 BUG();
2170 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002171 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2172 goto nest_cancel;
2173 if (opt_inst->changed) {
2174 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2175 goto nest_cancel;
2176 opt_inst->changed = false;
2177 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002178 nla_nest_end(skb, option_item);
2179 return 0;
2180
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002181nest_cancel:
2182 nla_nest_cancel(skb, option_item);
2183 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002184}
2185
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002186static int __send_and_alloc_skb(struct sk_buff **pskb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002187 struct team *team, u32 portid,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002188 team_nl_send_func_t *send_func)
2189{
2190 int err;
2191
2192 if (*pskb) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002193 err = send_func(*pskb, team, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002194 if (err)
2195 return err;
2196 }
Thomas Graf58050fc2012-06-28 03:57:45 +00002197 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002198 if (!*pskb)
2199 return -ENOMEM;
2200 return 0;
2201}
2202
Eric W. Biederman15e47302012-09-07 20:12:54 +00002203static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002204 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00002205 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002206{
2207 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002208 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002209 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002210 struct team_option_inst *opt_inst;
2211 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002212 struct sk_buff *skb = NULL;
2213 bool incomplete;
2214 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002215
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002216 opt_inst = list_first_entry(sel_opt_inst_list,
2217 struct team_option_inst, tmp_list);
2218
2219start_again:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002220 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002221 if (err)
2222 return err;
2223
Eric W. Biederman15e47302012-09-07 20:12:54 +00002224 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002225 TEAM_CMD_OPTIONS_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002226 if (!hdr)
2227 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002228
David S. Miller86ebb022012-04-01 20:25:18 -04002229 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2230 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002231 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2232 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00002233 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002234
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002235 i = 0;
2236 incomplete = false;
2237 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00002238 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002239 if (err) {
2240 if (err == -EMSGSIZE) {
2241 if (!i)
2242 goto errout;
2243 incomplete = true;
2244 break;
2245 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002246 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002247 }
2248 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002249 }
2250
2251 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002252 genlmsg_end(skb, hdr);
2253 if (incomplete)
2254 goto start_again;
2255
2256send_done:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002257 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002258 if (!nlh) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002259 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002260 if (err)
2261 goto errout;
2262 goto send_done;
2263 }
2264
Eric W. Biederman15e47302012-09-07 20:12:54 +00002265 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002266
2267nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00002268 err = -EMSGSIZE;
2269errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002270 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002271 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00002272 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002273}
2274
Jiri Pirko3d249d42011-11-11 22:16:48 +00002275static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2276{
2277 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002278 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002279 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002280 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002281
2282 team = team_nl_team_get(info);
2283 if (!team)
2284 return -EINVAL;
2285
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002286 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2287 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002288 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002289 NLM_F_ACK, team_nl_send_unicast,
2290 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002291
2292 team_nl_team_put(team);
2293
2294 return err;
2295}
2296
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002297static int team_nl_send_event_options_get(struct team *team,
2298 struct list_head *sel_opt_inst_list);
2299
Jiri Pirko3d249d42011-11-11 22:16:48 +00002300static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2301{
2302 struct team *team;
2303 int err = 0;
2304 int i;
2305 struct nlattr *nl_option;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002306 LIST_HEAD(opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002307
2308 team = team_nl_team_get(info);
2309 if (!team)
2310 return -EINVAL;
2311
2312 err = -EINVAL;
2313 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2314 err = -EINVAL;
2315 goto team_put;
2316 }
2317
2318 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00002319 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00002320 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002321 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002322 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002323 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00002324 u32 opt_array_index = 0;
2325 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002326 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002327 char *opt_name;
2328 bool opt_found = false;
2329
2330 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2331 err = -EINVAL;
2332 goto team_put;
2333 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002334 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002335 nl_option, team_nl_option_policy);
2336 if (err)
2337 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002338 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00002339 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002340 err = -EINVAL;
2341 goto team_put;
2342 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002343 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002344 case NLA_U32:
2345 opt_type = TEAM_OPTION_TYPE_U32;
2346 break;
2347 case NLA_STRING:
2348 opt_type = TEAM_OPTION_TYPE_STRING;
2349 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002350 case NLA_BINARY:
2351 opt_type = TEAM_OPTION_TYPE_BINARY;
2352 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002353 case NLA_FLAG:
2354 opt_type = TEAM_OPTION_TYPE_BOOL;
2355 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002356 case NLA_S32:
2357 opt_type = TEAM_OPTION_TYPE_S32;
2358 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002359 default:
2360 goto team_put;
2361 }
2362
Jiri Pirko14f066b2012-04-10 05:15:43 +00002363 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2364 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2365 err = -EINVAL;
2366 goto team_put;
2367 }
2368
Jiri Pirko80f7c662012-04-10 05:15:42 +00002369 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00002370 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2371 if (attr)
2372 opt_port_ifindex = nla_get_u32(attr);
2373
2374 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2375 if (attr) {
2376 opt_is_array = true;
2377 opt_array_index = nla_get_u32(attr);
2378 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002379
Jiri Pirko80f7c662012-04-10 05:15:42 +00002380 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2381 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002382 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002383 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002384 int tmp_ifindex;
2385
Jiri Pirko85d59a82012-06-19 05:54:10 +00002386 opt_inst_info = &opt_inst->info;
2387 tmp_ifindex = opt_inst_info->port ?
2388 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002389 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00002390 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00002391 tmp_ifindex != opt_port_ifindex ||
2392 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00002393 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002394 continue;
2395 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002396 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002397 switch (opt_type) {
2398 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002399 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002400 break;
2401 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002402 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00002403 err = -EINVAL;
2404 goto team_put;
2405 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00002406 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002407 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002408 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002409 ctx.data.bin_val.len = nla_len(attr_data);
2410 ctx.data.bin_val.ptr = nla_data(attr_data);
2411 break;
2412 case TEAM_OPTION_TYPE_BOOL:
2413 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00002414 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002415 case TEAM_OPTION_TYPE_S32:
2416 ctx.data.s32_val = nla_get_s32(attr_data);
2417 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002418 default:
2419 BUG();
2420 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002421 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002422 if (err)
2423 goto team_put;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002424 opt_inst->changed = true;
2425 list_add(&opt_inst->tmp_list, &opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002426 }
2427 if (!opt_found) {
2428 err = -ENOENT;
2429 goto team_put;
2430 }
2431 }
2432
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002433 err = team_nl_send_event_options_get(team, &opt_inst_list);
2434
Jiri Pirko3d249d42011-11-11 22:16:48 +00002435team_put:
2436 team_nl_team_put(team);
2437
2438 return err;
2439}
2440
Jiri Pirkod90f8892013-02-01 08:17:24 +00002441static int team_nl_fill_one_port_get(struct sk_buff *skb,
2442 struct team_port *port)
2443{
2444 struct nlattr *port_item;
2445
2446 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2447 if (!port_item)
2448 goto nest_cancel;
2449 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2450 goto nest_cancel;
2451 if (port->changed) {
2452 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2453 goto nest_cancel;
2454 port->changed = false;
2455 }
2456 if ((port->removed &&
2457 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2458 (port->state.linkup &&
2459 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2460 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2461 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2462 goto nest_cancel;
2463 nla_nest_end(skb, port_item);
2464 return 0;
2465
2466nest_cancel:
2467 nla_nest_cancel(skb, port_item);
2468 return -EMSGSIZE;
2469}
2470
2471static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2472 int flags, team_nl_send_func_t *send_func,
2473 struct team_port *one_port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002474{
2475 struct nlattr *port_list;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002476 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002477 void *hdr;
2478 struct team_port *port;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002479 int err;
2480 struct sk_buff *skb = NULL;
2481 bool incomplete;
2482 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002483
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002484 port = list_first_entry_or_null(&team->port_list,
2485 struct team_port, list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002486
2487start_again:
2488 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2489 if (err)
2490 return err;
2491
2492 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002493 TEAM_CMD_PORT_LIST_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002494 if (!hdr)
2495 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002496
David S. Miller86ebb022012-04-01 20:25:18 -04002497 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2498 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002499 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2500 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00002501 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002502
Jiri Pirkod90f8892013-02-01 08:17:24 +00002503 i = 0;
2504 incomplete = false;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002505
Jiri Pirkod90f8892013-02-01 08:17:24 +00002506 /* If one port is selected, called wants to send port list containing
2507 * only this port. Otherwise go through all listed ports and send all
2508 */
2509 if (one_port) {
2510 err = team_nl_fill_one_port_get(skb, one_port);
2511 if (err)
2512 goto errout;
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002513 } else if (port) {
2514 list_for_each_entry_from(port, &team->port_list, list) {
Jiri Pirkod90f8892013-02-01 08:17:24 +00002515 err = team_nl_fill_one_port_get(skb, port);
2516 if (err) {
2517 if (err == -EMSGSIZE) {
2518 if (!i)
2519 goto errout;
2520 incomplete = true;
2521 break;
2522 }
2523 goto errout;
2524 }
2525 i++;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002526 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002527 }
2528
2529 nla_nest_end(skb, port_list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002530 genlmsg_end(skb, hdr);
2531 if (incomplete)
2532 goto start_again;
2533
2534send_done:
2535 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2536 if (!nlh) {
2537 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2538 if (err)
2539 goto errout;
2540 goto send_done;
2541 }
2542
2543 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002544
2545nla_put_failure:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002546 err = -EMSGSIZE;
2547errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002548 genlmsg_cancel(skb, hdr);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002549 nlmsg_free(skb);
2550 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002551}
2552
2553static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2554 struct genl_info *info)
2555{
2556 struct team *team;
2557 int err;
2558
2559 team = team_nl_team_get(info);
2560 if (!team)
2561 return -EINVAL;
2562
Jiri Pirkod90f8892013-02-01 08:17:24 +00002563 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2564 NLM_F_ACK, team_nl_send_unicast, NULL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002565
2566 team_nl_team_put(team);
2567
2568 return err;
2569}
2570
2571static struct genl_ops team_nl_ops[] = {
2572 {
2573 .cmd = TEAM_CMD_NOOP,
2574 .doit = team_nl_cmd_noop,
2575 .policy = team_nl_policy,
2576 },
2577 {
2578 .cmd = TEAM_CMD_OPTIONS_SET,
2579 .doit = team_nl_cmd_options_set,
2580 .policy = team_nl_policy,
2581 .flags = GENL_ADMIN_PERM,
2582 },
2583 {
2584 .cmd = TEAM_CMD_OPTIONS_GET,
2585 .doit = team_nl_cmd_options_get,
2586 .policy = team_nl_policy,
2587 .flags = GENL_ADMIN_PERM,
2588 },
2589 {
2590 .cmd = TEAM_CMD_PORT_LIST_GET,
2591 .doit = team_nl_cmd_port_list_get,
2592 .policy = team_nl_policy,
2593 .flags = GENL_ADMIN_PERM,
2594 },
2595};
2596
2597static struct genl_multicast_group team_change_event_mcgrp = {
2598 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2599};
2600
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002601static int team_nl_send_multicast(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002602 struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002603{
2604 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2605 team_change_event_mcgrp.id, GFP_KERNEL);
2606}
2607
Jiri Pirko01048d92012-06-19 05:54:14 +00002608static int team_nl_send_event_options_get(struct team *team,
2609 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002610{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002611 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2612 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002613}
2614
Jiri Pirkod90f8892013-02-01 08:17:24 +00002615static int team_nl_send_event_port_get(struct team *team,
2616 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002617{
Jiri Pirkod90f8892013-02-01 08:17:24 +00002618 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2619 port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002620}
2621
2622static int team_nl_init(void)
2623{
2624 int err;
2625
2626 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2627 ARRAY_SIZE(team_nl_ops));
2628 if (err)
2629 return err;
2630
2631 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2632 if (err)
2633 goto err_change_event_grp_reg;
2634
2635 return 0;
2636
2637err_change_event_grp_reg:
2638 genl_unregister_family(&team_nl_family);
2639
2640 return err;
2641}
2642
2643static void team_nl_fini(void)
2644{
2645 genl_unregister_family(&team_nl_family);
2646}
2647
2648
2649/******************
2650 * Change checkers
2651 ******************/
2652
Jiri Pirkob82b9182012-01-24 05:16:00 +00002653static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002654{
2655 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002656 struct team_option_inst *opt_inst;
2657 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002658
Jiri Pirko01048d92012-06-19 05:54:14 +00002659 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2660 if (opt_inst->changed)
2661 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2662 }
2663 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002664 if (err && err != -ESRCH)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002665 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2666 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002667}
2668
2669/* rtnl lock is held */
Jiri Pirko10f3f512012-09-21 01:51:59 +00002670
2671static void __team_port_change_send(struct team_port *port, bool linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002672{
2673 int err;
2674
Jiri Pirkob82b9182012-01-24 05:16:00 +00002675 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002676 port->state.linkup = linkup;
2677 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002678 if (linkup) {
2679 struct ethtool_cmd ecmd;
2680
2681 err = __ethtool_get_settings(port->dev, &ecmd);
2682 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002683 port->state.speed = ethtool_cmd_speed(&ecmd);
2684 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002685 goto send_event;
2686 }
2687 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002688 port->state.speed = 0;
2689 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002690
2691send_event:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002692 err = team_nl_send_event_port_get(port->team, port);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002693 if (err && err != -ESRCH)
2694 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2695 port->dev->name, err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002696
2697}
2698
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002699static void __team_carrier_check(struct team *team)
2700{
2701 struct team_port *port;
2702 bool team_linkup;
2703
Flavio Leitnere1854832013-02-05 09:30:55 +00002704 if (team->user_carrier_enabled)
2705 return;
2706
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002707 team_linkup = false;
2708 list_for_each_entry(port, &team->port_list, list) {
2709 if (port->linkup) {
2710 team_linkup = true;
2711 break;
2712 }
2713 }
2714
2715 if (team_linkup)
2716 netif_carrier_on(team->dev);
2717 else
2718 netif_carrier_off(team->dev);
2719}
2720
Jiri Pirko10f3f512012-09-21 01:51:59 +00002721static void __team_port_change_check(struct team_port *port, bool linkup)
2722{
2723 if (port->state.linkup != linkup)
2724 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002725 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002726}
2727
2728static void __team_port_change_port_added(struct team_port *port, bool linkup)
2729{
2730 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002731 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002732}
2733
2734static void __team_port_change_port_removed(struct team_port *port)
2735{
2736 port->removed = true;
2737 __team_port_change_send(port, false);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002738 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002739}
2740
Jiri Pirko3d249d42011-11-11 22:16:48 +00002741static void team_port_change_check(struct team_port *port, bool linkup)
2742{
2743 struct team *team = port->team;
2744
Jiri Pirko61dc3462011-11-16 11:09:08 +00002745 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002746 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002747 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002748}
2749
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002750
Jiri Pirko3d249d42011-11-11 22:16:48 +00002751/************************************
2752 * Net device notifier event handler
2753 ************************************/
2754
2755static int team_device_event(struct notifier_block *unused,
2756 unsigned long event, void *ptr)
2757{
Jiri Pirko351638e2013-05-28 01:30:21 +00002758 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002759 struct team_port *port;
2760
2761 port = team_port_get_rtnl(dev);
2762 if (!port)
2763 return NOTIFY_DONE;
2764
2765 switch (event) {
2766 case NETDEV_UP:
2767 if (netif_carrier_ok(dev))
2768 team_port_change_check(port, true);
2769 case NETDEV_DOWN:
2770 team_port_change_check(port, false);
2771 case NETDEV_CHANGE:
2772 if (netif_running(port->dev))
2773 team_port_change_check(port,
2774 !!netif_carrier_ok(port->dev));
2775 break;
2776 case NETDEV_UNREGISTER:
2777 team_del_slave(port->team->dev, dev);
2778 break;
2779 case NETDEV_FEAT_CHANGE:
2780 team_compute_features(port->team);
2781 break;
2782 case NETDEV_CHANGEMTU:
2783 /* Forbid to change mtu of underlaying device */
2784 return NOTIFY_BAD;
2785 case NETDEV_PRE_TYPE_CHANGE:
2786 /* Forbid to change type of underlaying device */
2787 return NOTIFY_BAD;
2788 }
2789 return NOTIFY_DONE;
2790}
2791
2792static struct notifier_block team_notifier_block __read_mostly = {
2793 .notifier_call = team_device_event,
2794};
2795
2796
2797/***********************
2798 * Module init and exit
2799 ***********************/
2800
2801static int __init team_module_init(void)
2802{
2803 int err;
2804
2805 register_netdevice_notifier(&team_notifier_block);
2806
2807 err = rtnl_link_register(&team_link_ops);
2808 if (err)
2809 goto err_rtnl_reg;
2810
2811 err = team_nl_init();
2812 if (err)
2813 goto err_nl_init;
2814
2815 return 0;
2816
2817err_nl_init:
2818 rtnl_link_unregister(&team_link_ops);
2819
2820err_rtnl_reg:
2821 unregister_netdevice_notifier(&team_notifier_block);
2822
2823 return err;
2824}
2825
2826static void __exit team_module_exit(void)
2827{
2828 team_nl_fini();
2829 rtnl_link_unregister(&team_link_ops);
2830 unregister_netdevice_notifier(&team_notifier_block);
2831}
2832
2833module_init(team_module_init);
2834module_exit(team_module_exit);
2835
2836MODULE_LICENSE("GPL v2");
2837MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2838MODULE_DESCRIPTION("Ethernet team device driver");
2839MODULE_ALIAS_RTNL_LINK(DRV_NAME);