blob: 05c5efe8459148be152db604da421fe35e95ba41 [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 Pirko1d76efe2012-08-17 04:00:48 +000076int team_port_set_team_dev_addr(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000077{
Jiri Pirko1d76efe2012-08-17 04:00:48 +000078 return __set_port_dev_addr(port->dev, port->team->dev->dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000079}
Jiri Pirko1d76efe2012-08-17 04:00:48 +000080EXPORT_SYMBOL(team_port_set_team_dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000081
Jiri Pirko71472ec2012-04-10 05:15:44 +000082static void team_refresh_port_linkup(struct team_port *port)
83{
84 port->linkup = port->user.linkup_enabled ? port->user.linkup :
85 port->state.linkup;
86}
Jiri Pirko3d249d42011-11-11 22:16:48 +000087
Jiri Pirko0f1aad22012-06-19 05:54:11 +000088
Jiri Pirko3d249d42011-11-11 22:16:48 +000089/*******************
90 * Options handling
91 *******************/
92
Jiri Pirko80f7c662012-04-10 05:15:42 +000093struct team_option_inst { /* One for each option instance */
94 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +000095 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +000096 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +000097 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +000098 bool changed;
99 bool removed;
100};
101
102static struct team_option *__team_find_option(struct team *team,
103 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000104{
105 struct team_option *option;
106
107 list_for_each_entry(option, &team->option_list, list) {
108 if (strcmp(option->name, opt_name) == 0)
109 return option;
110 }
111 return NULL;
112}
113
Jiri Pirko80f7c662012-04-10 05:15:42 +0000114static void __team_option_inst_del(struct team_option_inst *opt_inst)
115{
116 list_del(&opt_inst->list);
117 kfree(opt_inst);
118}
119
120static void __team_option_inst_del_option(struct team *team,
121 struct team_option *option)
122{
123 struct team_option_inst *opt_inst, *tmp;
124
125 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
126 if (opt_inst->option == option)
127 __team_option_inst_del(opt_inst);
128 }
129}
130
Jiri Pirkob1303322012-06-19 05:54:08 +0000131static int __team_option_inst_add(struct team *team, struct team_option *option,
132 struct team_port *port)
133{
134 struct team_option_inst *opt_inst;
135 unsigned int array_size;
136 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000137 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000138
139 array_size = option->array_size;
140 if (!array_size)
141 array_size = 1; /* No array but still need one instance */
142
143 for (i = 0; i < array_size; i++) {
144 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
145 if (!opt_inst)
146 return -ENOMEM;
147 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000148 opt_inst->info.port = port;
149 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000150 opt_inst->changed = true;
151 opt_inst->removed = false;
152 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000153 if (option->init) {
154 err = option->init(team, &opt_inst->info);
155 if (err)
156 return err;
157 }
158
Jiri Pirkob1303322012-06-19 05:54:08 +0000159 }
160 return 0;
161}
162
Jiri Pirko80f7c662012-04-10 05:15:42 +0000163static int __team_option_inst_add_option(struct team *team,
164 struct team_option *option)
165{
166 struct team_port *port;
167 int err;
168
Jiri Pirkob1303322012-06-19 05:54:08 +0000169 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000170 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000171 if (err)
172 goto inst_del_option;
173 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000174
175 list_for_each_entry(port, &team->port_list, list) {
176 err = __team_option_inst_add(team, option, port);
177 if (err)
178 goto inst_del_option;
179 }
180 return 0;
181
182inst_del_option:
183 __team_option_inst_del_option(team, option);
184 return err;
185}
186
187static void __team_option_inst_mark_removed_option(struct team *team,
188 struct team_option *option)
189{
190 struct team_option_inst *opt_inst;
191
192 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
193 if (opt_inst->option == option) {
194 opt_inst->changed = true;
195 opt_inst->removed = true;
196 }
197 }
198}
199
200static void __team_option_inst_del_port(struct team *team,
201 struct team_port *port)
202{
203 struct team_option_inst *opt_inst, *tmp;
204
205 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
206 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000207 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000208 __team_option_inst_del(opt_inst);
209 }
210}
211
212static int __team_option_inst_add_port(struct team *team,
213 struct team_port *port)
214{
215 struct team_option *option;
216 int err;
217
218 list_for_each_entry(option, &team->option_list, list) {
219 if (!option->per_port)
220 continue;
221 err = __team_option_inst_add(team, option, port);
222 if (err)
223 goto inst_del_port;
224 }
225 return 0;
226
227inst_del_port:
228 __team_option_inst_del_port(team, port);
229 return err;
230}
231
232static void __team_option_inst_mark_removed_port(struct team *team,
233 struct team_port *port)
234{
235 struct team_option_inst *opt_inst;
236
237 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000238 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000239 opt_inst->changed = true;
240 opt_inst->removed = true;
241 }
242 }
243}
244
245static int __team_options_register(struct team *team,
246 const struct team_option *option,
247 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000248{
249 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000250 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000251 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000252
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000253 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
254 GFP_KERNEL);
255 if (!dst_opts)
256 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000257 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000258 if (__team_find_option(team, option->name)) {
259 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000260 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000261 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000262 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
263 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000264 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000265 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000266 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000267 }
268
Jiri Pirkob82b9182012-01-24 05:16:00 +0000269 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000270 err = __team_option_inst_add_option(team, dst_opts[i]);
271 if (err)
272 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000273 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000274 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000275
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000276 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000277 return 0;
278
Jiri Pirko80f7c662012-04-10 05:15:42 +0000279inst_rollback:
280 for (i--; i >= 0; i--)
281 __team_option_inst_del_option(team, dst_opts[i]);
282
283 i = option_count - 1;
284alloc_rollback:
285 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000286 kfree(dst_opts[i]);
287
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000288 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000289 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000290}
Jiri Pirko358b8382011-11-16 11:09:09 +0000291
Jiri Pirkob82b9182012-01-24 05:16:00 +0000292static void __team_options_mark_removed(struct team *team,
293 const struct team_option *option,
294 size_t option_count)
295{
296 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000297
Jiri Pirkob82b9182012-01-24 05:16:00 +0000298 for (i = 0; i < option_count; i++, option++) {
299 struct team_option *del_opt;
300
301 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000302 if (del_opt)
303 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000304 }
305}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000306
307static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000308 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000309 size_t option_count)
310{
311 int i;
312
Jiri Pirko358b8382011-11-16 11:09:09 +0000313 for (i = 0; i < option_count; i++, option++) {
314 struct team_option *del_opt;
315
316 del_opt = __team_find_option(team, option->name);
317 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000318 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000319 list_del(&del_opt->list);
320 kfree(del_opt);
321 }
322 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000323}
324
Jiri Pirkob82b9182012-01-24 05:16:00 +0000325static void __team_options_change_check(struct team *team);
326
327int team_options_register(struct team *team,
328 const struct team_option *option,
329 size_t option_count)
330{
331 int err;
332
333 err = __team_options_register(team, option, option_count);
334 if (err)
335 return err;
336 __team_options_change_check(team);
337 return 0;
338}
339EXPORT_SYMBOL(team_options_register);
340
Jiri Pirko358b8382011-11-16 11:09:09 +0000341void team_options_unregister(struct team *team,
342 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000343 size_t option_count)
344{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000345 __team_options_mark_removed(team, option, option_count);
346 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000347 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000348}
349EXPORT_SYMBOL(team_options_unregister);
350
Jiri Pirko80f7c662012-04-10 05:15:42 +0000351static int team_option_get(struct team *team,
352 struct team_option_inst *opt_inst,
353 struct team_gsetter_ctx *ctx)
354{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000355 if (!opt_inst->option->getter)
356 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000357 return opt_inst->option->getter(team, ctx);
358}
359
360static int team_option_set(struct team *team,
361 struct team_option_inst *opt_inst,
362 struct team_gsetter_ctx *ctx)
363{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000364 if (!opt_inst->option->setter)
365 return -EOPNOTSUPP;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +0000366 return opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000367}
368
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000369void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
370{
371 struct team_option_inst *opt_inst;
372
373 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
374 opt_inst->changed = true;
375}
376EXPORT_SYMBOL(team_option_inst_set_change);
377
378void team_options_change_check(struct team *team)
379{
380 __team_options_change_check(team);
381}
382EXPORT_SYMBOL(team_options_change_check);
383
384
Jiri Pirko3d249d42011-11-11 22:16:48 +0000385/****************
386 * Mode handling
387 ****************/
388
389static LIST_HEAD(mode_list);
390static DEFINE_SPINLOCK(mode_list_lock);
391
Jiri Pirko04027882012-06-19 05:54:03 +0000392struct team_mode_item {
393 struct list_head list;
394 const struct team_mode *mode;
395};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000396
Jiri Pirko04027882012-06-19 05:54:03 +0000397static struct team_mode_item *__find_mode(const char *kind)
398{
399 struct team_mode_item *mitem;
400
401 list_for_each_entry(mitem, &mode_list, list) {
402 if (strcmp(mitem->mode->kind, kind) == 0)
403 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000404 }
405 return NULL;
406}
407
408static bool is_good_mode_name(const char *name)
409{
410 while (*name != '\0') {
411 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
412 return false;
413 name++;
414 }
415 return true;
416}
417
Jiri Pirko04027882012-06-19 05:54:03 +0000418int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000419{
420 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000421 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000422
423 if (!is_good_mode_name(mode->kind) ||
424 mode->priv_size > TEAM_MODE_PRIV_SIZE)
425 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000426
427 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
428 if (!mitem)
429 return -ENOMEM;
430
Jiri Pirko3d249d42011-11-11 22:16:48 +0000431 spin_lock(&mode_list_lock);
432 if (__find_mode(mode->kind)) {
433 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000434 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000435 goto unlock;
436 }
Jiri Pirko04027882012-06-19 05:54:03 +0000437 mitem->mode = mode;
438 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000439unlock:
440 spin_unlock(&mode_list_lock);
441 return err;
442}
443EXPORT_SYMBOL(team_mode_register);
444
Jiri Pirko04027882012-06-19 05:54:03 +0000445void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000446{
Jiri Pirko04027882012-06-19 05:54:03 +0000447 struct team_mode_item *mitem;
448
Jiri Pirko3d249d42011-11-11 22:16:48 +0000449 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000450 mitem = __find_mode(mode->kind);
451 if (mitem) {
452 list_del_init(&mitem->list);
453 kfree(mitem);
454 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000455 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000456}
457EXPORT_SYMBOL(team_mode_unregister);
458
Jiri Pirko04027882012-06-19 05:54:03 +0000459static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000460{
Jiri Pirko04027882012-06-19 05:54:03 +0000461 struct team_mode_item *mitem;
462 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000463
464 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000465 mitem = __find_mode(kind);
466 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000467 spin_unlock(&mode_list_lock);
468 request_module("team-mode-%s", kind);
469 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000470 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000471 }
Jiri Pirko04027882012-06-19 05:54:03 +0000472 if (mitem) {
473 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000474 if (!try_module_get(mode->owner))
475 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000476 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000477
478 spin_unlock(&mode_list_lock);
479 return mode;
480}
481
482static void team_mode_put(const struct team_mode *mode)
483{
484 module_put(mode->owner);
485}
486
487static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
488{
489 dev_kfree_skb_any(skb);
490 return false;
491}
492
493rx_handler_result_t team_dummy_receive(struct team *team,
494 struct team_port *port,
495 struct sk_buff *skb)
496{
497 return RX_HANDLER_ANOTHER;
498}
499
Jiri Pirkod299cd52012-06-19 05:54:04 +0000500static const struct team_mode __team_no_mode = {
501 .kind = "*NOMODE*",
502};
503
504static bool team_is_mode_set(struct team *team)
505{
506 return team->mode != &__team_no_mode;
507}
508
509static void team_set_no_mode(struct team *team)
510{
Flavio Leitnere1854832013-02-05 09:30:55 +0000511 team->user_carrier_enabled = false;
Jiri Pirkod299cd52012-06-19 05:54:04 +0000512 team->mode = &__team_no_mode;
513}
514
Jiri Pirko122bb042012-06-26 06:52:45 +0000515static void __team_adjust_ops(struct team *team, int en_port_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000516{
517 /*
518 * To avoid checks in rx/tx skb paths, ensure here that non-null and
519 * correct ops are always set.
520 */
521
Jiri Pirko122bb042012-06-26 06:52:45 +0000522 if (!en_port_count || !team_is_mode_set(team) ||
523 !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000524 team->ops.transmit = team_dummy_transmit;
525 else
526 team->ops.transmit = team->mode->ops->transmit;
527
Jiri Pirko122bb042012-06-26 06:52:45 +0000528 if (!en_port_count || !team_is_mode_set(team) ||
529 !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000530 team->ops.receive = team_dummy_receive;
531 else
532 team->ops.receive = team->mode->ops->receive;
533}
534
Jiri Pirko122bb042012-06-26 06:52:45 +0000535static void team_adjust_ops(struct team *team)
536{
537 __team_adjust_ops(team, team->en_port_count);
538}
539
Jiri Pirko3d249d42011-11-11 22:16:48 +0000540/*
541 * We can benefit from the fact that it's ensured no port is present
542 * at the time of mode change. Therefore no packets are in fly so there's no
543 * need to set mode operations in any special way.
544 */
545static int __team_change_mode(struct team *team,
546 const struct team_mode *new_mode)
547{
548 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000549 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000550 void (*exit_op)(struct team *team) = team->ops.exit;
551
552 /* Clear ops area so no callback is called any longer */
553 memset(&team->ops, 0, sizeof(struct team_mode_ops));
554 team_adjust_ops(team);
555
556 if (exit_op)
557 exit_op(team);
558 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000559 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000560 /* zero private data area */
561 memset(&team->mode_priv, 0,
562 sizeof(struct team) - offsetof(struct team, mode_priv));
563 }
564
565 if (!new_mode)
566 return 0;
567
568 if (new_mode->ops->init) {
569 int err;
570
571 err = new_mode->ops->init(team);
572 if (err)
573 return err;
574 }
575
576 team->mode = new_mode;
577 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
578 team_adjust_ops(team);
579
580 return 0;
581}
582
583static int team_change_mode(struct team *team, const char *kind)
584{
Jiri Pirko04027882012-06-19 05:54:03 +0000585 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000586 struct net_device *dev = team->dev;
587 int err;
588
589 if (!list_empty(&team->port_list)) {
590 netdev_err(dev, "No ports can be present during mode change\n");
591 return -EBUSY;
592 }
593
Jiri Pirkod299cd52012-06-19 05:54:04 +0000594 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000595 netdev_err(dev, "Unable to change to the same mode the team is in\n");
596 return -EINVAL;
597 }
598
599 new_mode = team_mode_get(kind);
600 if (!new_mode) {
601 netdev_err(dev, "Mode \"%s\" not found\n", kind);
602 return -EINVAL;
603 }
604
605 err = __team_change_mode(team, new_mode);
606 if (err) {
607 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
608 team_mode_put(new_mode);
609 return err;
610 }
611
612 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
613 return 0;
614}
615
616
617/************************
618 * Rx path frame handler
619 ************************/
620
621/* note: already called with rcu_read_lock */
622static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
623{
624 struct sk_buff *skb = *pskb;
625 struct team_port *port;
626 struct team *team;
627 rx_handler_result_t res;
628
629 skb = skb_share_check(skb, GFP_ATOMIC);
630 if (!skb)
631 return RX_HANDLER_CONSUMED;
632
633 *pskb = skb;
634
635 port = team_port_get_rcu(skb->dev);
636 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000637 if (!team_port_enabled(port)) {
638 /* allow exact match delivery for disabled ports */
639 res = RX_HANDLER_EXACT;
640 } else {
641 res = team->ops.receive(team, port, skb);
642 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000643 if (res == RX_HANDLER_ANOTHER) {
644 struct team_pcpu_stats *pcpu_stats;
645
646 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
647 u64_stats_update_begin(&pcpu_stats->syncp);
648 pcpu_stats->rx_packets++;
649 pcpu_stats->rx_bytes += skb->len;
650 if (skb->pkt_type == PACKET_MULTICAST)
651 pcpu_stats->rx_multicast++;
652 u64_stats_update_end(&pcpu_stats->syncp);
653
654 skb->dev = team->dev;
655 } else {
656 this_cpu_inc(team->pcpu_stats->rx_dropped);
657 }
658
659 return res;
660}
661
662
Jiri Pirko8ff51052012-07-27 06:28:55 +0000663/*************************************
664 * Multiqueue Tx port select override
665 *************************************/
666
667static int team_queue_override_init(struct team *team)
668{
669 struct list_head *listarr;
670 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
671 unsigned int i;
672
673 if (!queue_cnt)
674 return 0;
675 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
676 if (!listarr)
677 return -ENOMEM;
678 team->qom_lists = listarr;
679 for (i = 0; i < queue_cnt; i++)
680 INIT_LIST_HEAD(listarr++);
681 return 0;
682}
683
684static void team_queue_override_fini(struct team *team)
685{
686 kfree(team->qom_lists);
687}
688
689static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
690{
691 return &team->qom_lists[queue_id - 1];
692}
693
694/*
695 * note: already called with rcu_read_lock
696 */
697static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
698{
699 struct list_head *qom_list;
700 struct team_port *port;
701
702 if (!team->queue_override_enabled || !skb->queue_mapping)
703 return false;
704 qom_list = __team_get_qom_list(team, skb->queue_mapping);
705 list_for_each_entry_rcu(port, qom_list, qom_list) {
706 if (!team_dev_queue_xmit(team, port, skb))
707 return true;
708 }
709 return false;
710}
711
712static void __team_queue_override_port_del(struct team *team,
713 struct team_port *port)
714{
715 list_del_rcu(&port->qom_list);
716 synchronize_rcu();
717 INIT_LIST_HEAD(&port->qom_list);
718}
719
720static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
721 struct team_port *cur)
722{
723 if (port->priority < cur->priority)
724 return true;
725 if (port->priority > cur->priority)
726 return false;
727 if (port->index < cur->index)
728 return true;
729 return false;
730}
731
732static void __team_queue_override_port_add(struct team *team,
733 struct team_port *port)
734{
735 struct team_port *cur;
736 struct list_head *qom_list;
737 struct list_head *node;
738
739 if (!port->queue_id || !team_port_enabled(port))
740 return;
741
742 qom_list = __team_get_qom_list(team, port->queue_id);
743 node = qom_list;
744 list_for_each_entry(cur, qom_list, qom_list) {
745 if (team_queue_override_port_has_gt_prio_than(port, cur))
746 break;
747 node = &cur->qom_list;
748 }
749 list_add_tail_rcu(&port->qom_list, node);
750}
751
752static void __team_queue_override_enabled_check(struct team *team)
753{
754 struct team_port *port;
755 bool enabled = false;
756
757 list_for_each_entry(port, &team->port_list, list) {
758 if (!list_empty(&port->qom_list)) {
759 enabled = true;
760 break;
761 }
762 }
763 if (enabled == team->queue_override_enabled)
764 return;
765 netdev_dbg(team->dev, "%s queue override\n",
766 enabled ? "Enabling" : "Disabling");
767 team->queue_override_enabled = enabled;
768}
769
770static void team_queue_override_port_refresh(struct team *team,
771 struct team_port *port)
772{
773 __team_queue_override_port_del(team, port);
774 __team_queue_override_port_add(team, port);
775 __team_queue_override_enabled_check(team);
776}
777
778
Jiri Pirko3d249d42011-11-11 22:16:48 +0000779/****************
780 * Port handling
781 ****************/
782
783static bool team_port_find(const struct team *team,
784 const struct team_port *port)
785{
786 struct team_port *cur;
787
788 list_for_each_entry(cur, &team->port_list, list)
789 if (cur == port)
790 return true;
791 return false;
792}
793
Jiri Pirko19a0b582012-04-20 04:42:05 +0000794/*
795 * Enable/disable port by adding to enabled port hashlist and setting
796 * port->index (Might be racy so reader could see incorrect ifindex when
797 * processing a flying packet, but that is not a problem). Write guarded
798 * by team->lock.
799 */
800static void team_port_enable(struct team *team,
801 struct team_port *port)
802{
803 if (team_port_enabled(port))
804 return;
805 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000806 hlist_add_head_rcu(&port->hlist,
807 team_port_index_hash(team, port->index));
Jiri Pirko122bb042012-06-26 06:52:45 +0000808 team_adjust_ops(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +0000809 team_queue_override_port_refresh(team, port);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000810 if (team->ops.port_enabled)
811 team->ops.port_enabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000812}
813
814static void __reconstruct_port_hlist(struct team *team, int rm_index)
815{
816 int i;
817 struct team_port *port;
818
Jiri Pirko19a0b582012-04-20 04:42:05 +0000819 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000820 port = team_get_port_by_index(team, i);
821 hlist_del_rcu(&port->hlist);
822 port->index--;
823 hlist_add_head_rcu(&port->hlist,
824 team_port_index_hash(team, port->index));
825 }
826}
827
Jiri Pirko19a0b582012-04-20 04:42:05 +0000828static void team_port_disable(struct team *team,
829 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000830{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000831 if (!team_port_enabled(port))
832 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000833 if (team->ops.port_disabled)
834 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000835 hlist_del_rcu(&port->hlist);
Jiri Pirko122bb042012-06-26 06:52:45 +0000836 __reconstruct_port_hlist(team, port->index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000837 port->index = -1;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000838 team_queue_override_port_refresh(team, port);
Jiri Pirko122bb042012-06-26 06:52:45 +0000839 __team_adjust_ops(team, team->en_port_count - 1);
840 /*
841 * Wait until readers see adjusted ops. This ensures that
842 * readers never see team->en_port_count == 0
843 */
844 synchronize_rcu();
845 team->en_port_count--;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000846}
847
848#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
849 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
850 NETIF_F_HIGHDMA | NETIF_F_LRO)
851
852static void __team_compute_features(struct team *team)
853{
854 struct team_port *port;
855 u32 vlan_features = TEAM_VLAN_FEATURES;
856 unsigned short max_hard_header_len = ETH_HLEN;
Jiri Pirkodc905952012-07-18 07:39:38 +0000857 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000858
859 list_for_each_entry(port, &team->port_list, list) {
860 vlan_features = netdev_increment_features(vlan_features,
861 port->dev->vlan_features,
862 TEAM_VLAN_FEATURES);
863
Jiri Pirkodc905952012-07-18 07:39:38 +0000864 dst_release_flag &= port->dev->priv_flags;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000865 if (port->dev->hard_header_len > max_hard_header_len)
866 max_hard_header_len = port->dev->hard_header_len;
867 }
868
869 team->dev->vlan_features = vlan_features;
870 team->dev->hard_header_len = max_hard_header_len;
871
Jiri Pirkodc905952012-07-18 07:39:38 +0000872 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
873 team->dev->priv_flags = flags | dst_release_flag;
874
Jiri Pirko3d249d42011-11-11 22:16:48 +0000875 netdev_change_features(team->dev);
876}
877
878static void team_compute_features(struct team *team)
879{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000880 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000881 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000882 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000883}
884
885static int team_port_enter(struct team *team, struct team_port *port)
886{
887 int err = 0;
888
889 dev_hold(team->dev);
890 port->dev->priv_flags |= IFF_TEAM_PORT;
891 if (team->ops.port_enter) {
892 err = team->ops.port_enter(team, port);
893 if (err) {
894 netdev_err(team->dev, "Device %s failed to enter team mode\n",
895 port->dev->name);
896 goto err_port_enter;
897 }
898 }
899
900 return 0;
901
902err_port_enter:
903 port->dev->priv_flags &= ~IFF_TEAM_PORT;
904 dev_put(team->dev);
905
906 return err;
907}
908
909static void team_port_leave(struct team *team, struct team_port *port)
910{
911 if (team->ops.port_leave)
912 team->ops.port_leave(team, port);
913 port->dev->priv_flags &= ~IFF_TEAM_PORT;
914 dev_put(team->dev);
915}
916
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000917#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang47be03a22012-08-10 01:24:37 +0000918static int team_port_enable_netpoll(struct team *team, struct team_port *port,
919 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000920{
921 struct netpoll *np;
922 int err;
923
Amerigo Wang47be03a22012-08-10 01:24:37 +0000924 np = kzalloc(sizeof(*np), gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000925 if (!np)
926 return -ENOMEM;
927
Amerigo Wang47be03a22012-08-10 01:24:37 +0000928 err = __netpoll_setup(np, port->dev, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000929 if (err) {
930 kfree(np);
931 return err;
932 }
933 port->np = np;
934 return err;
935}
936
937static void team_port_disable_netpoll(struct team_port *port)
938{
939 struct netpoll *np = port->np;
940
941 if (!np)
942 return;
943 port->np = NULL;
944
945 /* Wait for transmitting packets to finish before freeing. */
946 synchronize_rcu_bh();
947 __netpoll_cleanup(np);
948 kfree(np);
949}
950
951static struct netpoll_info *team_netpoll_info(struct team *team)
952{
953 return team->dev->npinfo;
954}
955
956#else
Amerigo Wang47be03a22012-08-10 01:24:37 +0000957static int team_port_enable_netpoll(struct team *team, struct team_port *port,
958 gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +0000959{
960 return 0;
961}
962static void team_port_disable_netpoll(struct team_port *port)
963{
964}
965static struct netpoll_info *team_netpoll_info(struct team *team)
966{
967 return NULL;
968}
969#endif
970
Jiri Pirko10f3f512012-09-21 01:51:59 +0000971static void __team_port_change_port_added(struct team_port *port, bool linkup);
Jiri Pirko1d76efe2012-08-17 04:00:48 +0000972static int team_dev_type_check_change(struct net_device *dev,
973 struct net_device *port_dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000974
975static int team_port_add(struct team *team, struct net_device *port_dev)
976{
977 struct net_device *dev = team->dev;
978 struct team_port *port;
979 char *portname = port_dev->name;
980 int err;
981
Jiri Pirko1d76efe2012-08-17 04:00:48 +0000982 if (port_dev->flags & IFF_LOOPBACK) {
983 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 +0000984 portname);
985 return -EINVAL;
986 }
987
988 if (team_port_exists(port_dev)) {
989 netdev_err(dev, "Device %s is already a port "
990 "of a team device\n", portname);
991 return -EBUSY;
992 }
993
Jiri Pirko2c33bb32012-08-23 03:26:53 +0000994 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
995 vlan_uses_dev(dev)) {
996 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
997 portname);
998 return -EPERM;
999 }
1000
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001001 err = team_dev_type_check_change(dev, port_dev);
1002 if (err)
1003 return err;
1004
Jiri Pirko3d249d42011-11-11 22:16:48 +00001005 if (port_dev->flags & IFF_UP) {
1006 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1007 portname);
1008 return -EBUSY;
1009 }
1010
Jiri Pirko5149ee52012-06-19 05:54:05 +00001011 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1012 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001013 if (!port)
1014 return -ENOMEM;
1015
1016 port->dev = port_dev;
1017 port->team = team;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001018 INIT_LIST_HEAD(&port->qom_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001019
1020 port->orig.mtu = port_dev->mtu;
1021 err = dev_set_mtu(port_dev, dev->mtu);
1022 if (err) {
1023 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1024 goto err_set_mtu;
1025 }
1026
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001027 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001028
1029 err = team_port_enter(team, port);
1030 if (err) {
1031 netdev_err(dev, "Device %s failed to enter team mode\n",
1032 portname);
1033 goto err_port_enter;
1034 }
1035
1036 err = dev_open(port_dev);
1037 if (err) {
1038 netdev_dbg(dev, "Device %s opening failed\n",
1039 portname);
1040 goto err_dev_open;
1041 }
1042
Jiri Pirko57459182011-12-08 04:11:20 +00001043 err = vlan_vids_add_by_dev(port_dev, dev);
1044 if (err) {
1045 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1046 portname);
1047 goto err_vids_add;
1048 }
1049
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001050 if (team_netpoll_info(team)) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001051 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001052 if (err) {
1053 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1054 portname);
1055 goto err_enable_netpoll;
1056 }
1057 }
1058
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001059 err = netdev_master_upper_dev_link(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001060 if (err) {
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001061 netdev_err(dev, "Device %s failed to set upper link\n",
1062 portname);
1063 goto err_set_upper_link;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001064 }
1065
1066 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1067 port);
1068 if (err) {
1069 netdev_err(dev, "Device %s failed to register rx_handler\n",
1070 portname);
1071 goto err_handler_register;
1072 }
1073
Jiri Pirko35b384b2012-06-19 05:54:19 +00001074 err = __team_option_inst_add_port(team, port);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001075 if (err) {
1076 netdev_err(dev, "Device %s failed to add per-port options\n",
1077 portname);
1078 goto err_option_port_add;
1079 }
1080
Jiri Pirko19a0b582012-04-20 04:42:05 +00001081 port->index = -1;
1082 team_port_enable(team, port);
1083 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001084 __team_compute_features(team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00001085 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
Jiri Pirko35b384b2012-06-19 05:54:19 +00001086 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001087
1088 netdev_info(dev, "Port device %s added\n", portname);
1089
1090 return 0;
1091
Jiri Pirko80f7c662012-04-10 05:15:42 +00001092err_option_port_add:
1093 netdev_rx_handler_unregister(port_dev);
1094
Jiri Pirko3d249d42011-11-11 22:16:48 +00001095err_handler_register:
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001096 netdev_upper_dev_unlink(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001097
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001098err_set_upper_link:
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001099 team_port_disable_netpoll(port);
1100
1101err_enable_netpoll:
Jiri Pirko57459182011-12-08 04:11:20 +00001102 vlan_vids_del_by_dev(port_dev, dev);
1103
1104err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001105 dev_close(port_dev);
1106
1107err_dev_open:
1108 team_port_leave(team, port);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001109 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001110
1111err_port_enter:
1112 dev_set_mtu(port_dev, port->orig.mtu);
1113
1114err_set_mtu:
1115 kfree(port);
1116
1117 return err;
1118}
1119
Jiri Pirko10f3f512012-09-21 01:51:59 +00001120static void __team_port_change_port_removed(struct team_port *port);
1121
Jiri Pirko3d249d42011-11-11 22:16:48 +00001122static int team_port_del(struct team *team, struct net_device *port_dev)
1123{
1124 struct net_device *dev = team->dev;
1125 struct team_port *port;
1126 char *portname = port_dev->name;
1127
1128 port = team_port_get_rtnl(port_dev);
1129 if (!port || !team_port_find(team, port)) {
1130 netdev_err(dev, "Device %s does not act as a port of this team\n",
1131 portname);
1132 return -ENOENT;
1133 }
1134
Jiri Pirko19a0b582012-04-20 04:42:05 +00001135 team_port_disable(team, port);
1136 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001137 netdev_rx_handler_unregister(port_dev);
Jiri Pirkob1cc9852013-01-03 22:48:53 +00001138 netdev_upper_dev_unlink(port_dev, dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001139 team_port_disable_netpoll(port);
Jiri Pirko57459182011-12-08 04:11:20 +00001140 vlan_vids_del_by_dev(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001141 dev_close(port_dev);
1142 team_port_leave(team, port);
Jiri Pirkoc3969d802013-02-01 08:17:25 +00001143
1144 __team_option_inst_mark_removed_port(team, port);
1145 __team_options_change_check(team);
1146 __team_option_inst_del_port(team, port);
1147 __team_port_change_port_removed(port);
1148
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001149 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001150 dev_set_mtu(port_dev, port->orig.mtu);
1151 synchronize_rcu();
1152 kfree(port);
1153 netdev_info(dev, "Port device %s removed\n", portname);
1154 __team_compute_features(team);
1155
1156 return 0;
1157}
1158
1159
1160/*****************
1161 * Net device ops
1162 *****************/
1163
Jiri Pirko80f7c662012-04-10 05:15:42 +00001164static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001165{
Jiri Pirkod299cd52012-06-19 05:54:04 +00001166 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001167 return 0;
1168}
1169
Jiri Pirko80f7c662012-04-10 05:15:42 +00001170static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001171{
Jiri Pirko80f7c662012-04-10 05:15:42 +00001172 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001173}
1174
Jiri Pirkoacd69962012-04-20 04:42:06 +00001175static int team_port_en_option_get(struct team *team,
1176 struct team_gsetter_ctx *ctx)
1177{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001178 struct team_port *port = ctx->info->port;
1179
1180 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001181 return 0;
1182}
1183
1184static int team_port_en_option_set(struct team *team,
1185 struct team_gsetter_ctx *ctx)
1186{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001187 struct team_port *port = ctx->info->port;
1188
Jiri Pirkoacd69962012-04-20 04:42:06 +00001189 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001190 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001191 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001192 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001193 return 0;
1194}
1195
Jiri Pirko71472ec2012-04-10 05:15:44 +00001196static int team_user_linkup_option_get(struct team *team,
1197 struct team_gsetter_ctx *ctx)
1198{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001199 struct team_port *port = ctx->info->port;
1200
1201 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001202 return 0;
1203}
1204
1205static int team_user_linkup_option_set(struct team *team,
1206 struct team_gsetter_ctx *ctx)
1207{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001208 struct team_port *port = ctx->info->port;
1209
1210 port->user.linkup = ctx->data.bool_val;
1211 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001212 return 0;
1213}
1214
1215static int team_user_linkup_en_option_get(struct team *team,
1216 struct team_gsetter_ctx *ctx)
1217{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001218 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001219
1220 ctx->data.bool_val = port->user.linkup_enabled;
1221 return 0;
1222}
1223
1224static int team_user_linkup_en_option_set(struct team *team,
1225 struct team_gsetter_ctx *ctx)
1226{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001227 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001228
1229 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001230 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001231 return 0;
1232}
1233
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001234static int team_priority_option_get(struct team *team,
1235 struct team_gsetter_ctx *ctx)
1236{
1237 struct team_port *port = ctx->info->port;
1238
1239 ctx->data.s32_val = port->priority;
1240 return 0;
1241}
1242
1243static int team_priority_option_set(struct team *team,
1244 struct team_gsetter_ctx *ctx)
1245{
1246 struct team_port *port = ctx->info->port;
1247
1248 port->priority = ctx->data.s32_val;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001249 team_queue_override_port_refresh(team, port);
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001250 return 0;
1251}
1252
Jiri Pirko8ff51052012-07-27 06:28:55 +00001253static int team_queue_id_option_get(struct team *team,
1254 struct team_gsetter_ctx *ctx)
1255{
1256 struct team_port *port = ctx->info->port;
1257
1258 ctx->data.u32_val = port->queue_id;
1259 return 0;
1260}
1261
1262static int team_queue_id_option_set(struct team *team,
1263 struct team_gsetter_ctx *ctx)
1264{
1265 struct team_port *port = ctx->info->port;
1266
1267 if (port->queue_id == ctx->data.u32_val)
1268 return 0;
1269 if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
1270 return -EINVAL;
1271 port->queue_id = ctx->data.u32_val;
1272 team_queue_override_port_refresh(team, port);
1273 return 0;
1274}
1275
1276
Jiri Pirko358b8382011-11-16 11:09:09 +00001277static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001278 {
1279 .name = "mode",
1280 .type = TEAM_OPTION_TYPE_STRING,
1281 .getter = team_mode_option_get,
1282 .setter = team_mode_option_set,
1283 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001284 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001285 .name = "enabled",
1286 .type = TEAM_OPTION_TYPE_BOOL,
1287 .per_port = true,
1288 .getter = team_port_en_option_get,
1289 .setter = team_port_en_option_set,
1290 },
1291 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001292 .name = "user_linkup",
1293 .type = TEAM_OPTION_TYPE_BOOL,
1294 .per_port = true,
1295 .getter = team_user_linkup_option_get,
1296 .setter = team_user_linkup_option_set,
1297 },
1298 {
1299 .name = "user_linkup_enabled",
1300 .type = TEAM_OPTION_TYPE_BOOL,
1301 .per_port = true,
1302 .getter = team_user_linkup_en_option_get,
1303 .setter = team_user_linkup_en_option_set,
1304 },
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001305 {
1306 .name = "priority",
1307 .type = TEAM_OPTION_TYPE_S32,
1308 .per_port = true,
1309 .getter = team_priority_option_get,
1310 .setter = team_priority_option_set,
1311 },
Jiri Pirko8ff51052012-07-27 06:28:55 +00001312 {
1313 .name = "queue_id",
1314 .type = TEAM_OPTION_TYPE_U32,
1315 .per_port = true,
1316 .getter = team_queue_id_option_get,
1317 .setter = team_queue_id_option_set,
1318 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001319};
1320
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001321static struct lock_class_key team_netdev_xmit_lock_key;
1322static struct lock_class_key team_netdev_addr_lock_key;
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001323static struct lock_class_key team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001324
1325static void team_set_lockdep_class_one(struct net_device *dev,
1326 struct netdev_queue *txq,
1327 void *unused)
1328{
1329 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1330}
1331
1332static void team_set_lockdep_class(struct net_device *dev)
1333{
1334 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1335 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001336 dev->qdisc_tx_busylock = &team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001337}
1338
Jiri Pirko3d249d42011-11-11 22:16:48 +00001339static int team_init(struct net_device *dev)
1340{
1341 struct team *team = netdev_priv(dev);
1342 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001343 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001344
1345 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001346 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001347 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001348
1349 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1350 if (!team->pcpu_stats)
1351 return -ENOMEM;
1352
1353 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001354 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001355 INIT_LIST_HEAD(&team->port_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001356 err = team_queue_override_init(team);
1357 if (err)
1358 goto err_team_queue_override_init;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001359
1360 team_adjust_ops(team);
1361
1362 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001363 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirko358b8382011-11-16 11:09:09 +00001364 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1365 if (err)
1366 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001367 netif_carrier_off(dev);
1368
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001369 team_set_lockdep_class(dev);
1370
Jiri Pirko3d249d42011-11-11 22:16:48 +00001371 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001372
1373err_options_register:
Jiri Pirko8ff51052012-07-27 06:28:55 +00001374 team_queue_override_fini(team);
1375err_team_queue_override_init:
Jiri Pirko358b8382011-11-16 11:09:09 +00001376 free_percpu(team->pcpu_stats);
1377
1378 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001379}
1380
1381static void team_uninit(struct net_device *dev)
1382{
1383 struct team *team = netdev_priv(dev);
1384 struct team_port *port;
1385 struct team_port *tmp;
1386
Jiri Pirko61dc3462011-11-16 11:09:08 +00001387 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001388 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1389 team_port_del(team, port->dev);
1390
1391 __team_change_mode(team, NULL); /* cleanup */
1392 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko8ff51052012-07-27 06:28:55 +00001393 team_queue_override_fini(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001394 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001395}
1396
1397static void team_destructor(struct net_device *dev)
1398{
1399 struct team *team = netdev_priv(dev);
1400
1401 free_percpu(team->pcpu_stats);
1402 free_netdev(dev);
1403}
1404
1405static int team_open(struct net_device *dev)
1406{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001407 return 0;
1408}
1409
1410static int team_close(struct net_device *dev)
1411{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001412 return 0;
1413}
1414
1415/*
1416 * note: already called with rcu_read_lock
1417 */
1418static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1419{
1420 struct team *team = netdev_priv(dev);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001421 bool tx_success;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001422 unsigned int len = skb->len;
1423
Jiri Pirko8ff51052012-07-27 06:28:55 +00001424 tx_success = team_queue_override_transmit(team, skb);
1425 if (!tx_success)
1426 tx_success = team->ops.transmit(team, skb);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001427 if (tx_success) {
1428 struct team_pcpu_stats *pcpu_stats;
1429
1430 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1431 u64_stats_update_begin(&pcpu_stats->syncp);
1432 pcpu_stats->tx_packets++;
1433 pcpu_stats->tx_bytes += len;
1434 u64_stats_update_end(&pcpu_stats->syncp);
1435 } else {
1436 this_cpu_inc(team->pcpu_stats->tx_dropped);
1437 }
1438
1439 return NETDEV_TX_OK;
1440}
1441
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001442static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1443{
1444 /*
1445 * This helper function exists to help dev_pick_tx get the correct
1446 * destination queue. Using a helper function skips a call to
1447 * skb_tx_hash and will put the skbs in the queue we expect on their
1448 * way down to the team driver.
1449 */
1450 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1451
1452 /*
1453 * Save the original txq to restore before passing to the driver
1454 */
1455 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1456
1457 if (unlikely(txq >= dev->real_num_tx_queues)) {
1458 do {
1459 txq -= dev->real_num_tx_queues;
1460 } while (txq >= dev->real_num_tx_queues);
1461 }
1462 return txq;
1463}
1464
Jiri Pirko3d249d42011-11-11 22:16:48 +00001465static void team_change_rx_flags(struct net_device *dev, int change)
1466{
1467 struct team *team = netdev_priv(dev);
1468 struct team_port *port;
1469 int inc;
1470
1471 rcu_read_lock();
1472 list_for_each_entry_rcu(port, &team->port_list, list) {
1473 if (change & IFF_PROMISC) {
1474 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1475 dev_set_promiscuity(port->dev, inc);
1476 }
1477 if (change & IFF_ALLMULTI) {
1478 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1479 dev_set_allmulti(port->dev, inc);
1480 }
1481 }
1482 rcu_read_unlock();
1483}
1484
1485static void team_set_rx_mode(struct net_device *dev)
1486{
1487 struct team *team = netdev_priv(dev);
1488 struct team_port *port;
1489
1490 rcu_read_lock();
1491 list_for_each_entry_rcu(port, &team->port_list, list) {
1492 dev_uc_sync(port->dev, dev);
1493 dev_mc_sync(port->dev, dev);
1494 }
1495 rcu_read_unlock();
1496}
1497
1498static int team_set_mac_address(struct net_device *dev, void *p)
1499{
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001500 struct sockaddr *addr = p;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001501 struct team *team = netdev_priv(dev);
1502 struct team_port *port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001503
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001504 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1505 return -EADDRNOTAVAIL;
1506 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001507 rcu_read_lock();
1508 list_for_each_entry_rcu(port, &team->port_list, list)
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001509 if (team->ops.port_change_dev_addr)
1510 team->ops.port_change_dev_addr(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001511 rcu_read_unlock();
1512 return 0;
1513}
1514
1515static int team_change_mtu(struct net_device *dev, int new_mtu)
1516{
1517 struct team *team = netdev_priv(dev);
1518 struct team_port *port;
1519 int err;
1520
1521 /*
1522 * Alhough this is reader, it's guarded by team lock. It's not possible
1523 * to traverse list in reverse under rcu_read_lock
1524 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001525 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001526 list_for_each_entry(port, &team->port_list, list) {
1527 err = dev_set_mtu(port->dev, new_mtu);
1528 if (err) {
1529 netdev_err(dev, "Device %s failed to change mtu",
1530 port->dev->name);
1531 goto unwind;
1532 }
1533 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001534 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001535
1536 dev->mtu = new_mtu;
1537
1538 return 0;
1539
1540unwind:
1541 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1542 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001543 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001544
1545 return err;
1546}
1547
1548static struct rtnl_link_stats64 *
1549team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1550{
1551 struct team *team = netdev_priv(dev);
1552 struct team_pcpu_stats *p;
1553 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1554 u32 rx_dropped = 0, tx_dropped = 0;
1555 unsigned int start;
1556 int i;
1557
1558 for_each_possible_cpu(i) {
1559 p = per_cpu_ptr(team->pcpu_stats, i);
1560 do {
1561 start = u64_stats_fetch_begin_bh(&p->syncp);
1562 rx_packets = p->rx_packets;
1563 rx_bytes = p->rx_bytes;
1564 rx_multicast = p->rx_multicast;
1565 tx_packets = p->tx_packets;
1566 tx_bytes = p->tx_bytes;
1567 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1568
1569 stats->rx_packets += rx_packets;
1570 stats->rx_bytes += rx_bytes;
1571 stats->multicast += rx_multicast;
1572 stats->tx_packets += tx_packets;
1573 stats->tx_bytes += tx_bytes;
1574 /*
1575 * rx_dropped & tx_dropped are u32, updated
1576 * without syncp protection.
1577 */
1578 rx_dropped += p->rx_dropped;
1579 tx_dropped += p->tx_dropped;
1580 }
1581 stats->rx_dropped = rx_dropped;
1582 stats->tx_dropped = tx_dropped;
1583 return stats;
1584}
1585
Jiri Pirko8e586132011-12-08 19:52:37 -05001586static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001587{
1588 struct team *team = netdev_priv(dev);
1589 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001590 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001591
Jiri Pirko87002b02011-12-08 04:11:17 +00001592 /*
1593 * Alhough this is reader, it's guarded by team lock. It's not possible
1594 * to traverse list in reverse under rcu_read_lock
1595 */
1596 mutex_lock(&team->lock);
1597 list_for_each_entry(port, &team->port_list, list) {
1598 err = vlan_vid_add(port->dev, vid);
1599 if (err)
1600 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001601 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001602 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001603
1604 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001605
1606unwind:
1607 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1608 vlan_vid_del(port->dev, vid);
1609 mutex_unlock(&team->lock);
1610
1611 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001612}
1613
Jiri Pirko8e586132011-12-08 19:52:37 -05001614static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001615{
1616 struct team *team = netdev_priv(dev);
1617 struct team_port *port;
1618
1619 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001620 list_for_each_entry_rcu(port, &team->port_list, list)
1621 vlan_vid_del(port->dev, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001622 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001623
1624 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001625}
1626
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001627#ifdef CONFIG_NET_POLL_CONTROLLER
1628static void team_poll_controller(struct net_device *dev)
1629{
1630}
1631
1632static void __team_netpoll_cleanup(struct team *team)
1633{
1634 struct team_port *port;
1635
1636 list_for_each_entry(port, &team->port_list, list)
1637 team_port_disable_netpoll(port);
1638}
1639
1640static void team_netpoll_cleanup(struct net_device *dev)
1641{
1642 struct team *team = netdev_priv(dev);
1643
1644 mutex_lock(&team->lock);
1645 __team_netpoll_cleanup(team);
1646 mutex_unlock(&team->lock);
1647}
1648
1649static int team_netpoll_setup(struct net_device *dev,
Amerigo Wang47be03a22012-08-10 01:24:37 +00001650 struct netpoll_info *npifo, gfp_t gfp)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001651{
1652 struct team *team = netdev_priv(dev);
1653 struct team_port *port;
Jiri Pirko3324d022012-07-24 01:20:48 +00001654 int err = 0;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001655
1656 mutex_lock(&team->lock);
1657 list_for_each_entry(port, &team->port_list, list) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001658 err = team_port_enable_netpoll(team, port, gfp);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001659 if (err) {
1660 __team_netpoll_cleanup(team);
1661 break;
1662 }
1663 }
1664 mutex_unlock(&team->lock);
1665 return err;
1666}
1667#endif
1668
Jiri Pirko3d249d42011-11-11 22:16:48 +00001669static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1670{
1671 struct team *team = netdev_priv(dev);
1672 int err;
1673
Jiri Pirko61dc3462011-11-16 11:09:08 +00001674 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001675 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001676 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001677 return err;
1678}
1679
1680static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1681{
1682 struct team *team = netdev_priv(dev);
1683 int err;
1684
Jiri Pirko61dc3462011-11-16 11:09:08 +00001685 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001686 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001687 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001688 return err;
1689}
1690
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001691static netdev_features_t team_fix_features(struct net_device *dev,
1692 netdev_features_t features)
1693{
1694 struct team_port *port;
1695 struct team *team = netdev_priv(dev);
1696 netdev_features_t mask;
1697
1698 mask = features;
1699 features &= ~NETIF_F_ONE_FOR_ALL;
1700 features |= NETIF_F_ALL_FOR_ALL;
1701
1702 rcu_read_lock();
1703 list_for_each_entry_rcu(port, &team->port_list, list) {
1704 features = netdev_increment_features(features,
1705 port->dev->features,
1706 mask);
1707 }
1708 rcu_read_unlock();
1709 return features;
1710}
1711
Flavio Leitner4cafe372012-12-29 15:31:01 +00001712static int team_change_carrier(struct net_device *dev, bool new_carrier)
1713{
Flavio Leitnere1854832013-02-05 09:30:55 +00001714 struct team *team = netdev_priv(dev);
1715
1716 team->user_carrier_enabled = true;
1717
Flavio Leitner4cafe372012-12-29 15:31:01 +00001718 if (new_carrier)
1719 netif_carrier_on(dev);
1720 else
1721 netif_carrier_off(dev);
1722 return 0;
1723}
1724
Jiri Pirko3d249d42011-11-11 22:16:48 +00001725static const struct net_device_ops team_netdev_ops = {
1726 .ndo_init = team_init,
1727 .ndo_uninit = team_uninit,
1728 .ndo_open = team_open,
1729 .ndo_stop = team_close,
1730 .ndo_start_xmit = team_xmit,
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001731 .ndo_select_queue = team_select_queue,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001732 .ndo_change_rx_flags = team_change_rx_flags,
1733 .ndo_set_rx_mode = team_set_rx_mode,
1734 .ndo_set_mac_address = team_set_mac_address,
1735 .ndo_change_mtu = team_change_mtu,
1736 .ndo_get_stats64 = team_get_stats64,
1737 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1738 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001739#ifdef CONFIG_NET_POLL_CONTROLLER
1740 .ndo_poll_controller = team_poll_controller,
1741 .ndo_netpoll_setup = team_netpoll_setup,
1742 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1743#endif
Jiri Pirko3d249d42011-11-11 22:16:48 +00001744 .ndo_add_slave = team_add_slave,
1745 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001746 .ndo_fix_features = team_fix_features,
Flavio Leitner4cafe372012-12-29 15:31:01 +00001747 .ndo_change_carrier = team_change_carrier,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001748};
1749
Flavio Leitner7f51c582012-12-29 16:37:33 +00001750/***********************
1751 * ethtool interface
1752 ***********************/
1753
1754static void team_ethtool_get_drvinfo(struct net_device *dev,
1755 struct ethtool_drvinfo *drvinfo)
1756{
Flavio Leitner3066af52013-01-05 02:53:10 +00001757 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1758 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
Flavio Leitner7f51c582012-12-29 16:37:33 +00001759}
1760
1761static const struct ethtool_ops team_ethtool_ops = {
1762 .get_drvinfo = team_ethtool_get_drvinfo,
1763 .get_link = ethtool_op_get_link,
1764};
Jiri Pirko3d249d42011-11-11 22:16:48 +00001765
1766/***********************
1767 * rt netlink interface
1768 ***********************/
1769
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001770static void team_setup_by_port(struct net_device *dev,
1771 struct net_device *port_dev)
1772{
1773 dev->header_ops = port_dev->header_ops;
1774 dev->type = port_dev->type;
1775 dev->hard_header_len = port_dev->hard_header_len;
1776 dev->addr_len = port_dev->addr_len;
1777 dev->mtu = port_dev->mtu;
1778 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
1779 memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001780}
1781
1782static int team_dev_type_check_change(struct net_device *dev,
1783 struct net_device *port_dev)
1784{
1785 struct team *team = netdev_priv(dev);
1786 char *portname = port_dev->name;
1787 int err;
1788
1789 if (dev->type == port_dev->type)
1790 return 0;
1791 if (!list_empty(&team->port_list)) {
1792 netdev_err(dev, "Device %s is of different type\n", portname);
1793 return -EBUSY;
1794 }
1795 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
1796 err = notifier_to_errno(err);
1797 if (err) {
1798 netdev_err(dev, "Refused to change device type\n");
1799 return err;
1800 }
1801 dev_uc_flush(dev);
1802 dev_mc_flush(dev);
1803 team_setup_by_port(dev, port_dev);
1804 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1805 return 0;
1806}
1807
Jiri Pirko3d249d42011-11-11 22:16:48 +00001808static void team_setup(struct net_device *dev)
1809{
1810 ether_setup(dev);
1811
1812 dev->netdev_ops = &team_netdev_ops;
Flavio Leitner7f51c582012-12-29 16:37:33 +00001813 dev->ethtool_ops = &team_ethtool_ops;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001814 dev->destructor = team_destructor;
1815 dev->tx_queue_len = 0;
1816 dev->flags |= IFF_MULTICAST;
1817 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1818
1819 /*
1820 * Indicate we support unicast address filtering. That way core won't
1821 * bring us to promisc mode in case a unicast addr is added.
1822 * Let this up to underlay drivers.
1823 */
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00001824 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001825
1826 dev->features |= NETIF_F_LLTX;
1827 dev->features |= NETIF_F_GRO;
Jiri Pirko3ed71472012-11-28 06:13:10 +00001828 dev->hw_features = TEAM_VLAN_FEATURES |
1829 NETIF_F_HW_VLAN_TX |
Jiri Pirko3d249d42011-11-11 22:16:48 +00001830 NETIF_F_HW_VLAN_RX |
1831 NETIF_F_HW_VLAN_FILTER;
1832
Jiri Pirko3ed71472012-11-28 06:13:10 +00001833 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001834 dev->features |= dev->hw_features;
1835}
1836
1837static int team_newlink(struct net *src_net, struct net_device *dev,
1838 struct nlattr *tb[], struct nlattr *data[])
1839{
1840 int err;
1841
1842 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001843 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001844
1845 err = register_netdevice(dev);
1846 if (err)
1847 return err;
1848
1849 return 0;
1850}
1851
1852static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1853{
1854 if (tb[IFLA_ADDRESS]) {
1855 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1856 return -EINVAL;
1857 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1858 return -EADDRNOTAVAIL;
1859 }
1860 return 0;
1861}
1862
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001863static unsigned int team_get_num_tx_queues(void)
1864{
1865 return TEAM_DEFAULT_NUM_TX_QUEUES;
1866}
1867
1868static unsigned int team_get_num_rx_queues(void)
1869{
1870 return TEAM_DEFAULT_NUM_RX_QUEUES;
1871}
1872
Jiri Pirko3d249d42011-11-11 22:16:48 +00001873static struct rtnl_link_ops team_link_ops __read_mostly = {
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001874 .kind = DRV_NAME,
1875 .priv_size = sizeof(struct team),
1876 .setup = team_setup,
1877 .newlink = team_newlink,
1878 .validate = team_validate,
1879 .get_num_tx_queues = team_get_num_tx_queues,
1880 .get_num_rx_queues = team_get_num_rx_queues,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001881};
1882
1883
1884/***********************************
1885 * Generic netlink custom interface
1886 ***********************************/
1887
1888static struct genl_family team_nl_family = {
1889 .id = GENL_ID_GENERATE,
1890 .name = TEAM_GENL_NAME,
1891 .version = TEAM_GENL_VERSION,
1892 .maxattr = TEAM_ATTR_MAX,
1893 .netnsok = true,
1894};
1895
1896static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1897 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1898 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1899 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1900 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1901};
1902
1903static const struct nla_policy
1904team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1905 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1906 [TEAM_ATTR_OPTION_NAME] = {
1907 .type = NLA_STRING,
1908 .len = TEAM_STRING_MAX_LEN,
1909 },
1910 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1911 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00001912 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001913};
1914
1915static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1916{
1917 struct sk_buff *msg;
1918 void *hdr;
1919 int err;
1920
Thomas Graf58050fc2012-06-28 03:57:45 +00001921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001922 if (!msg)
1923 return -ENOMEM;
1924
Eric W. Biederman15e47302012-09-07 20:12:54 +00001925 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001926 &team_nl_family, 0, TEAM_CMD_NOOP);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00001927 if (!hdr) {
1928 err = -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001929 goto err_msg_put;
1930 }
1931
1932 genlmsg_end(msg, hdr);
1933
Eric W. Biederman15e47302012-09-07 20:12:54 +00001934 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001935
1936err_msg_put:
1937 nlmsg_free(msg);
1938
1939 return err;
1940}
1941
1942/*
1943 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001944 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00001945 */
1946static struct team *team_nl_team_get(struct genl_info *info)
1947{
1948 struct net *net = genl_info_net(info);
1949 int ifindex;
1950 struct net_device *dev;
1951 struct team *team;
1952
1953 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1954 return NULL;
1955
1956 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001957 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001958 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001959 if (dev)
1960 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001961 return NULL;
1962 }
1963
1964 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001965 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001966 return team;
1967}
1968
1969static void team_nl_team_put(struct team *team)
1970{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001971 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001972 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001973}
1974
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001975typedef int team_nl_send_func_t(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001976 struct team *team, u32 portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001977
Eric W. Biederman15e47302012-09-07 20:12:54 +00001978static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001979{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001980 return genlmsg_unicast(dev_net(team->dev), skb, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001981}
1982
Jiri Pirko01048d92012-06-19 05:54:14 +00001983static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1984 struct team_option_inst *opt_inst)
1985{
1986 struct nlattr *option_item;
1987 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001988 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00001989 struct team_gsetter_ctx ctx;
1990 int err;
1991
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001992 ctx.info = opt_inst_info;
1993 err = team_option_get(team, opt_inst, &ctx);
1994 if (err)
1995 return err;
1996
Jiri Pirko01048d92012-06-19 05:54:14 +00001997 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1998 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001999 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002000
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002001 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2002 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002003 if (opt_inst_info->port &&
2004 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2005 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002006 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002007 if (opt_inst->option->array_size &&
2008 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2009 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002010 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002011
2012 switch (option->type) {
2013 case TEAM_OPTION_TYPE_U32:
2014 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002015 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002016 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002017 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002018 break;
2019 case TEAM_OPTION_TYPE_STRING:
2020 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002021 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002022 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2023 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002024 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002025 break;
2026 case TEAM_OPTION_TYPE_BINARY:
2027 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002028 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002029 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2030 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002031 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002032 break;
2033 case TEAM_OPTION_TYPE_BOOL:
2034 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002035 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002036 if (ctx.data.bool_val &&
2037 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002038 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002039 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002040 case TEAM_OPTION_TYPE_S32:
2041 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2042 goto nest_cancel;
2043 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2044 goto nest_cancel;
2045 break;
Jiri Pirko01048d92012-06-19 05:54:14 +00002046 default:
2047 BUG();
2048 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002049 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2050 goto nest_cancel;
2051 if (opt_inst->changed) {
2052 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2053 goto nest_cancel;
2054 opt_inst->changed = false;
2055 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002056 nla_nest_end(skb, option_item);
2057 return 0;
2058
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002059nest_cancel:
2060 nla_nest_cancel(skb, option_item);
2061 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002062}
2063
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002064static int __send_and_alloc_skb(struct sk_buff **pskb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002065 struct team *team, u32 portid,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002066 team_nl_send_func_t *send_func)
2067{
2068 int err;
2069
2070 if (*pskb) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002071 err = send_func(*pskb, team, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002072 if (err)
2073 return err;
2074 }
Thomas Graf58050fc2012-06-28 03:57:45 +00002075 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002076 if (!*pskb)
2077 return -ENOMEM;
2078 return 0;
2079}
2080
Eric W. Biederman15e47302012-09-07 20:12:54 +00002081static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002082 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00002083 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002084{
2085 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002086 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002087 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002088 struct team_option_inst *opt_inst;
2089 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002090 struct sk_buff *skb = NULL;
2091 bool incomplete;
2092 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002093
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002094 opt_inst = list_first_entry(sel_opt_inst_list,
2095 struct team_option_inst, tmp_list);
2096
2097start_again:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002098 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002099 if (err)
2100 return err;
2101
Eric W. Biederman15e47302012-09-07 20:12:54 +00002102 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002103 TEAM_CMD_OPTIONS_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002104 if (!hdr)
2105 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002106
David S. Miller86ebb022012-04-01 20:25:18 -04002107 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2108 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002109 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2110 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00002111 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002112
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002113 i = 0;
2114 incomplete = false;
2115 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00002116 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002117 if (err) {
2118 if (err == -EMSGSIZE) {
2119 if (!i)
2120 goto errout;
2121 incomplete = true;
2122 break;
2123 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002124 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002125 }
2126 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002127 }
2128
2129 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002130 genlmsg_end(skb, hdr);
2131 if (incomplete)
2132 goto start_again;
2133
2134send_done:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002135 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002136 if (!nlh) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002137 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002138 if (err)
2139 goto errout;
2140 goto send_done;
2141 }
2142
Eric W. Biederman15e47302012-09-07 20:12:54 +00002143 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002144
2145nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00002146 err = -EMSGSIZE;
2147errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002148 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002149 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00002150 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002151}
2152
Jiri Pirko3d249d42011-11-11 22:16:48 +00002153static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2154{
2155 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002156 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002157 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002158 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002159
2160 team = team_nl_team_get(info);
2161 if (!team)
2162 return -EINVAL;
2163
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002164 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2165 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002166 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002167 NLM_F_ACK, team_nl_send_unicast,
2168 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002169
2170 team_nl_team_put(team);
2171
2172 return err;
2173}
2174
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002175static int team_nl_send_event_options_get(struct team *team,
2176 struct list_head *sel_opt_inst_list);
2177
Jiri Pirko3d249d42011-11-11 22:16:48 +00002178static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2179{
2180 struct team *team;
2181 int err = 0;
2182 int i;
2183 struct nlattr *nl_option;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002184 LIST_HEAD(opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002185
2186 team = team_nl_team_get(info);
2187 if (!team)
2188 return -EINVAL;
2189
2190 err = -EINVAL;
2191 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2192 err = -EINVAL;
2193 goto team_put;
2194 }
2195
2196 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00002197 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00002198 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002199 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002200 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002201 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00002202 u32 opt_array_index = 0;
2203 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002204 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002205 char *opt_name;
2206 bool opt_found = false;
2207
2208 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2209 err = -EINVAL;
2210 goto team_put;
2211 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002212 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002213 nl_option, team_nl_option_policy);
2214 if (err)
2215 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002216 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00002217 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002218 err = -EINVAL;
2219 goto team_put;
2220 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002221 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002222 case NLA_U32:
2223 opt_type = TEAM_OPTION_TYPE_U32;
2224 break;
2225 case NLA_STRING:
2226 opt_type = TEAM_OPTION_TYPE_STRING;
2227 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002228 case NLA_BINARY:
2229 opt_type = TEAM_OPTION_TYPE_BINARY;
2230 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002231 case NLA_FLAG:
2232 opt_type = TEAM_OPTION_TYPE_BOOL;
2233 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002234 case NLA_S32:
2235 opt_type = TEAM_OPTION_TYPE_S32;
2236 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002237 default:
2238 goto team_put;
2239 }
2240
Jiri Pirko14f066b2012-04-10 05:15:43 +00002241 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2242 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2243 err = -EINVAL;
2244 goto team_put;
2245 }
2246
Jiri Pirko80f7c662012-04-10 05:15:42 +00002247 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00002248 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2249 if (attr)
2250 opt_port_ifindex = nla_get_u32(attr);
2251
2252 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2253 if (attr) {
2254 opt_is_array = true;
2255 opt_array_index = nla_get_u32(attr);
2256 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002257
Jiri Pirko80f7c662012-04-10 05:15:42 +00002258 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2259 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002260 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002261 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002262 int tmp_ifindex;
2263
Jiri Pirko85d59a82012-06-19 05:54:10 +00002264 opt_inst_info = &opt_inst->info;
2265 tmp_ifindex = opt_inst_info->port ?
2266 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002267 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00002268 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00002269 tmp_ifindex != opt_port_ifindex ||
2270 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00002271 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002272 continue;
2273 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002274 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002275 switch (opt_type) {
2276 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002277 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002278 break;
2279 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002280 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00002281 err = -EINVAL;
2282 goto team_put;
2283 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00002284 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002285 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002286 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002287 ctx.data.bin_val.len = nla_len(attr_data);
2288 ctx.data.bin_val.ptr = nla_data(attr_data);
2289 break;
2290 case TEAM_OPTION_TYPE_BOOL:
2291 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00002292 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002293 case TEAM_OPTION_TYPE_S32:
2294 ctx.data.s32_val = nla_get_s32(attr_data);
2295 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002296 default:
2297 BUG();
2298 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002299 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002300 if (err)
2301 goto team_put;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002302 opt_inst->changed = true;
2303 list_add(&opt_inst->tmp_list, &opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002304 }
2305 if (!opt_found) {
2306 err = -ENOENT;
2307 goto team_put;
2308 }
2309 }
2310
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002311 err = team_nl_send_event_options_get(team, &opt_inst_list);
2312
Jiri Pirko3d249d42011-11-11 22:16:48 +00002313team_put:
2314 team_nl_team_put(team);
2315
2316 return err;
2317}
2318
Jiri Pirkod90f8892013-02-01 08:17:24 +00002319static int team_nl_fill_one_port_get(struct sk_buff *skb,
2320 struct team_port *port)
2321{
2322 struct nlattr *port_item;
2323
2324 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2325 if (!port_item)
2326 goto nest_cancel;
2327 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2328 goto nest_cancel;
2329 if (port->changed) {
2330 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2331 goto nest_cancel;
2332 port->changed = false;
2333 }
2334 if ((port->removed &&
2335 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2336 (port->state.linkup &&
2337 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2338 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2339 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2340 goto nest_cancel;
2341 nla_nest_end(skb, port_item);
2342 return 0;
2343
2344nest_cancel:
2345 nla_nest_cancel(skb, port_item);
2346 return -EMSGSIZE;
2347}
2348
2349static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2350 int flags, team_nl_send_func_t *send_func,
2351 struct team_port *one_port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002352{
2353 struct nlattr *port_list;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002354 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002355 void *hdr;
2356 struct team_port *port;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002357 int err;
2358 struct sk_buff *skb = NULL;
2359 bool incomplete;
2360 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002361
Jiri Pirkod90f8892013-02-01 08:17:24 +00002362 port = list_first_entry(&team->port_list, struct team_port, list);
2363
2364start_again:
2365 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2366 if (err)
2367 return err;
2368
2369 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002370 TEAM_CMD_PORT_LIST_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002371 if (!hdr)
2372 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002373
David S. Miller86ebb022012-04-01 20:25:18 -04002374 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2375 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002376 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2377 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00002378 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002379
Jiri Pirkod90f8892013-02-01 08:17:24 +00002380 i = 0;
2381 incomplete = false;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002382
Jiri Pirkod90f8892013-02-01 08:17:24 +00002383 /* If one port is selected, called wants to send port list containing
2384 * only this port. Otherwise go through all listed ports and send all
2385 */
2386 if (one_port) {
2387 err = team_nl_fill_one_port_get(skb, one_port);
2388 if (err)
2389 goto errout;
2390 } else {
2391 list_for_each_entry(port, &team->port_list, list) {
2392 err = team_nl_fill_one_port_get(skb, port);
2393 if (err) {
2394 if (err == -EMSGSIZE) {
2395 if (!i)
2396 goto errout;
2397 incomplete = true;
2398 break;
2399 }
2400 goto errout;
2401 }
2402 i++;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002403 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002404 }
2405
2406 nla_nest_end(skb, port_list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002407 genlmsg_end(skb, hdr);
2408 if (incomplete)
2409 goto start_again;
2410
2411send_done:
2412 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2413 if (!nlh) {
2414 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2415 if (err)
2416 goto errout;
2417 goto send_done;
2418 }
2419
2420 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002421
2422nla_put_failure:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002423 err = -EMSGSIZE;
2424errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002425 genlmsg_cancel(skb, hdr);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002426 nlmsg_free(skb);
2427 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002428}
2429
2430static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2431 struct genl_info *info)
2432{
2433 struct team *team;
2434 int err;
2435
2436 team = team_nl_team_get(info);
2437 if (!team)
2438 return -EINVAL;
2439
Jiri Pirkod90f8892013-02-01 08:17:24 +00002440 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2441 NLM_F_ACK, team_nl_send_unicast, NULL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002442
2443 team_nl_team_put(team);
2444
2445 return err;
2446}
2447
2448static struct genl_ops team_nl_ops[] = {
2449 {
2450 .cmd = TEAM_CMD_NOOP,
2451 .doit = team_nl_cmd_noop,
2452 .policy = team_nl_policy,
2453 },
2454 {
2455 .cmd = TEAM_CMD_OPTIONS_SET,
2456 .doit = team_nl_cmd_options_set,
2457 .policy = team_nl_policy,
2458 .flags = GENL_ADMIN_PERM,
2459 },
2460 {
2461 .cmd = TEAM_CMD_OPTIONS_GET,
2462 .doit = team_nl_cmd_options_get,
2463 .policy = team_nl_policy,
2464 .flags = GENL_ADMIN_PERM,
2465 },
2466 {
2467 .cmd = TEAM_CMD_PORT_LIST_GET,
2468 .doit = team_nl_cmd_port_list_get,
2469 .policy = team_nl_policy,
2470 .flags = GENL_ADMIN_PERM,
2471 },
2472};
2473
2474static struct genl_multicast_group team_change_event_mcgrp = {
2475 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2476};
2477
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002478static int team_nl_send_multicast(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002479 struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002480{
2481 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2482 team_change_event_mcgrp.id, GFP_KERNEL);
2483}
2484
Jiri Pirko01048d92012-06-19 05:54:14 +00002485static int team_nl_send_event_options_get(struct team *team,
2486 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002487{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002488 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2489 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002490}
2491
Jiri Pirkod90f8892013-02-01 08:17:24 +00002492static int team_nl_send_event_port_get(struct team *team,
2493 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002494{
Jiri Pirkod90f8892013-02-01 08:17:24 +00002495 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2496 port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002497}
2498
2499static int team_nl_init(void)
2500{
2501 int err;
2502
2503 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2504 ARRAY_SIZE(team_nl_ops));
2505 if (err)
2506 return err;
2507
2508 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2509 if (err)
2510 goto err_change_event_grp_reg;
2511
2512 return 0;
2513
2514err_change_event_grp_reg:
2515 genl_unregister_family(&team_nl_family);
2516
2517 return err;
2518}
2519
2520static void team_nl_fini(void)
2521{
2522 genl_unregister_family(&team_nl_family);
2523}
2524
2525
2526/******************
2527 * Change checkers
2528 ******************/
2529
Jiri Pirkob82b9182012-01-24 05:16:00 +00002530static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002531{
2532 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002533 struct team_option_inst *opt_inst;
2534 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002535
Jiri Pirko01048d92012-06-19 05:54:14 +00002536 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2537 if (opt_inst->changed)
2538 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2539 }
2540 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002541 if (err && err != -ESRCH)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002542 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2543 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002544}
2545
2546/* rtnl lock is held */
Jiri Pirko10f3f512012-09-21 01:51:59 +00002547
2548static void __team_port_change_send(struct team_port *port, bool linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002549{
2550 int err;
2551
Jiri Pirkob82b9182012-01-24 05:16:00 +00002552 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002553 port->state.linkup = linkup;
2554 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002555 if (linkup) {
2556 struct ethtool_cmd ecmd;
2557
2558 err = __ethtool_get_settings(port->dev, &ecmd);
2559 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002560 port->state.speed = ethtool_cmd_speed(&ecmd);
2561 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002562 goto send_event;
2563 }
2564 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002565 port->state.speed = 0;
2566 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002567
2568send_event:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002569 err = team_nl_send_event_port_get(port->team, port);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002570 if (err && err != -ESRCH)
2571 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2572 port->dev->name, err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002573
2574}
2575
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002576static void __team_carrier_check(struct team *team)
2577{
2578 struct team_port *port;
2579 bool team_linkup;
2580
Flavio Leitnere1854832013-02-05 09:30:55 +00002581 if (team->user_carrier_enabled)
2582 return;
2583
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002584 team_linkup = false;
2585 list_for_each_entry(port, &team->port_list, list) {
2586 if (port->linkup) {
2587 team_linkup = true;
2588 break;
2589 }
2590 }
2591
2592 if (team_linkup)
2593 netif_carrier_on(team->dev);
2594 else
2595 netif_carrier_off(team->dev);
2596}
2597
Jiri Pirko10f3f512012-09-21 01:51:59 +00002598static void __team_port_change_check(struct team_port *port, bool linkup)
2599{
2600 if (port->state.linkup != linkup)
2601 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002602 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002603}
2604
2605static void __team_port_change_port_added(struct team_port *port, bool linkup)
2606{
2607 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002608 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002609}
2610
2611static void __team_port_change_port_removed(struct team_port *port)
2612{
2613 port->removed = true;
2614 __team_port_change_send(port, false);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002615 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002616}
2617
Jiri Pirko3d249d42011-11-11 22:16:48 +00002618static void team_port_change_check(struct team_port *port, bool linkup)
2619{
2620 struct team *team = port->team;
2621
Jiri Pirko61dc3462011-11-16 11:09:08 +00002622 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002623 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002624 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002625}
2626
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002627
Jiri Pirko3d249d42011-11-11 22:16:48 +00002628/************************************
2629 * Net device notifier event handler
2630 ************************************/
2631
2632static int team_device_event(struct notifier_block *unused,
2633 unsigned long event, void *ptr)
2634{
2635 struct net_device *dev = (struct net_device *) ptr;
2636 struct team_port *port;
2637
2638 port = team_port_get_rtnl(dev);
2639 if (!port)
2640 return NOTIFY_DONE;
2641
2642 switch (event) {
2643 case NETDEV_UP:
2644 if (netif_carrier_ok(dev))
2645 team_port_change_check(port, true);
2646 case NETDEV_DOWN:
2647 team_port_change_check(port, false);
2648 case NETDEV_CHANGE:
2649 if (netif_running(port->dev))
2650 team_port_change_check(port,
2651 !!netif_carrier_ok(port->dev));
2652 break;
2653 case NETDEV_UNREGISTER:
2654 team_del_slave(port->team->dev, dev);
2655 break;
2656 case NETDEV_FEAT_CHANGE:
2657 team_compute_features(port->team);
2658 break;
2659 case NETDEV_CHANGEMTU:
2660 /* Forbid to change mtu of underlaying device */
2661 return NOTIFY_BAD;
2662 case NETDEV_PRE_TYPE_CHANGE:
2663 /* Forbid to change type of underlaying device */
2664 return NOTIFY_BAD;
2665 }
2666 return NOTIFY_DONE;
2667}
2668
2669static struct notifier_block team_notifier_block __read_mostly = {
2670 .notifier_call = team_device_event,
2671};
2672
2673
2674/***********************
2675 * Module init and exit
2676 ***********************/
2677
2678static int __init team_module_init(void)
2679{
2680 int err;
2681
2682 register_netdevice_notifier(&team_notifier_block);
2683
2684 err = rtnl_link_register(&team_link_ops);
2685 if (err)
2686 goto err_rtnl_reg;
2687
2688 err = team_nl_init();
2689 if (err)
2690 goto err_nl_init;
2691
2692 return 0;
2693
2694err_nl_init:
2695 rtnl_link_unregister(&team_link_ops);
2696
2697err_rtnl_reg:
2698 unregister_netdevice_notifier(&team_notifier_block);
2699
2700 return err;
2701}
2702
2703static void __exit team_module_exit(void)
2704{
2705 team_nl_fini();
2706 rtnl_link_unregister(&team_link_ops);
2707 unregister_netdevice_notifier(&team_notifier_block);
2708}
2709
2710module_init(team_module_init);
2711module_exit(team_module_exit);
2712
2713MODULE_LICENSE("GPL v2");
2714MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2715MODULE_DESCRIPTION("Ethernet team device driver");
2716MODULE_ALIAS_RTNL_LINK(DRV_NAME);