team.c 63 KB
Newer Older
1
/*
2
 * drivers/net/team/team.c - Network team device driver
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/rcupdate.h>
#include <linux/errno.h>
#include <linux/ctype.h>
#include <linux/notifier.h>
#include <linux/netdevice.h>
Jiri Pirko's avatar
Jiri Pirko committed
21
#include <linux/netpoll.h>
22
#include <linux/if_vlan.h>
23
24
25
26
27
28
29
#include <linux/if_arp.h>
#include <linux/socket.h>
#include <linux/etherdevice.h>
#include <linux/rtnetlink.h>
#include <net/rtnetlink.h>
#include <net/genetlink.h>
#include <net/netlink.h>
Jiri Pirko's avatar
Jiri Pirko committed
30
#include <net/sch_generic.h>
Flavio Leitner's avatar
Flavio Leitner committed
31
#include <generated/utsrelease.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <linux/if_team.h>

#define DRV_NAME "team"


/**********
 * Helpers
 **********/

#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)

static struct team_port *team_port_get_rcu(const struct net_device *dev)
{
	struct team_port *port = rcu_dereference(dev->rx_handler_data);

	return team_port_exists(dev) ? port : NULL;
}

static struct team_port *team_port_get_rtnl(const struct net_device *dev)
{
	struct team_port *port = rtnl_dereference(dev->rx_handler_data);

	return team_port_exists(dev) ? port : NULL;
}

/*
58
 * Since the ability to change device address for open port device is tested in
59
60
 * team_port_add, this function can be called without control of return value
 */
61
62
static int __set_port_dev_addr(struct net_device *port_dev,
			       const unsigned char *dev_addr)
63
64
65
{
	struct sockaddr addr;

66
67
	memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
	addr.sa_family = port_dev->type;
68
69
70
	return dev_set_mac_address(port_dev, &addr);
}

71
static int team_port_set_orig_dev_addr(struct team_port *port)
72
{
73
	return __set_port_dev_addr(port->dev, port->orig.dev_addr);
74
75
}

76
int team_port_set_team_dev_addr(struct team_port *port)
77
{
78
	return __set_port_dev_addr(port->dev, port->team->dev->dev_addr);
79
}
80
EXPORT_SYMBOL(team_port_set_team_dev_addr);
81

82
83
84
85
86
static void team_refresh_port_linkup(struct team_port *port)
{
	port->linkup = port->user.linkup_enabled ? port->user.linkup :
						   port->state.linkup;
}
87

Jiri Pirko's avatar
Jiri Pirko committed
88

89
90
91
92
/*******************
 * Options handling
 *******************/

93
94
struct team_option_inst { /* One for each option instance */
	struct list_head list;
95
	struct list_head tmp_list;
96
	struct team_option *option;
97
	struct team_option_inst_info info;
98
99
100
101
102
103
	bool changed;
	bool removed;
};

static struct team_option *__team_find_option(struct team *team,
					      const char *opt_name)
104
105
106
107
108
109
110
111
112
113
{
	struct team_option *option;

	list_for_each_entry(option, &team->option_list, list) {
		if (strcmp(option->name, opt_name) == 0)
			return option;
	}
	return NULL;
}

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
static void __team_option_inst_del(struct team_option_inst *opt_inst)
{
	list_del(&opt_inst->list);
	kfree(opt_inst);
}

static void __team_option_inst_del_option(struct team *team,
					  struct team_option *option)
{
	struct team_option_inst *opt_inst, *tmp;

	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
		if (opt_inst->option == option)
			__team_option_inst_del(opt_inst);
	}
}

Jiri Pirko's avatar
Jiri Pirko committed
131
132
133
134
135
136
static int __team_option_inst_add(struct team *team, struct team_option *option,
				  struct team_port *port)
{
	struct team_option_inst *opt_inst;
	unsigned int array_size;
	unsigned int i;
137
	int err;
Jiri Pirko's avatar
Jiri Pirko committed
138
139
140
141
142
143
144
145
146
147

	array_size = option->array_size;
	if (!array_size)
		array_size = 1; /* No array but still need one instance */

	for (i = 0; i < array_size; i++) {
		opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
		if (!opt_inst)
			return -ENOMEM;
		opt_inst->option = option;
148
149
		opt_inst->info.port = port;
		opt_inst->info.array_index = i;
Jiri Pirko's avatar
Jiri Pirko committed
150
151
152
		opt_inst->changed = true;
		opt_inst->removed = false;
		list_add_tail(&opt_inst->list, &team->option_inst_list);
153
154
155
156
157
158
		if (option->init) {
			err = option->init(team, &opt_inst->info);
			if (err)
				return err;
		}

Jiri Pirko's avatar
Jiri Pirko committed
159
160
161
162
	}
	return 0;
}

163
164
165
166
167
168
static int __team_option_inst_add_option(struct team *team,
					 struct team_option *option)
{
	struct team_port *port;
	int err;

Jiri Pirko's avatar
Jiri Pirko committed
169
	if (!option->per_port) {
170
		err = __team_option_inst_add(team, option, NULL);
Jiri Pirko's avatar
Jiri Pirko committed
171
172
173
		if (err)
			goto inst_del_option;
	}
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

	list_for_each_entry(port, &team->port_list, list) {
		err = __team_option_inst_add(team, option, port);
		if (err)
			goto inst_del_option;
	}
	return 0;

inst_del_option:
	__team_option_inst_del_option(team, option);
	return err;
}

static void __team_option_inst_mark_removed_option(struct team *team,
						   struct team_option *option)
{
	struct team_option_inst *opt_inst;

	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
		if (opt_inst->option == option) {
			opt_inst->changed = true;
			opt_inst->removed = true;
		}
	}
}

static void __team_option_inst_del_port(struct team *team,
					struct team_port *port)
{
	struct team_option_inst *opt_inst, *tmp;

	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
		if (opt_inst->option->per_port &&
207
		    opt_inst->info.port == port)
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
			__team_option_inst_del(opt_inst);
	}
}

static int __team_option_inst_add_port(struct team *team,
				       struct team_port *port)
{
	struct team_option *option;
	int err;

	list_for_each_entry(option, &team->option_list, list) {
		if (!option->per_port)
			continue;
		err = __team_option_inst_add(team, option, port);
		if (err)
			goto inst_del_port;
	}
	return 0;

inst_del_port:
	__team_option_inst_del_port(team, port);
	return err;
}

static void __team_option_inst_mark_removed_port(struct team *team,
						 struct team_port *port)
{
	struct team_option_inst *opt_inst;

	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
238
		if (opt_inst->info.port == port) {
239
240
241
242
243
244
245
246
247
			opt_inst->changed = true;
			opt_inst->removed = true;
		}
	}
}

static int __team_options_register(struct team *team,
				   const struct team_option *option,
				   size_t option_count)
248
249
{
	int i;
250
	struct team_option **dst_opts;
251
	int err;
252

253
254
255
256
	dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
			   GFP_KERNEL);
	if (!dst_opts)
		return -ENOMEM;
257
258
259
	for (i = 0; i < option_count; i++, option++) {
		if (__team_find_option(team, option->name)) {
			err = -EEXIST;
260
			goto alloc_rollback;
261
		}
262
263
		dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
		if (!dst_opts[i]) {
264
			err = -ENOMEM;
265
			goto alloc_rollback;
266
267
268
		}
	}

269
	for (i = 0; i < option_count; i++) {
270
271
272
		err = __team_option_inst_add_option(team, dst_opts[i]);
		if (err)
			goto inst_rollback;
273
		list_add_tail(&dst_opts[i]->list, &team->option_list);
274
	}
275

276
	kfree(dst_opts);
277
278
	return 0;

279
280
281
282
283
284
285
inst_rollback:
	for (i--; i >= 0; i--)
		__team_option_inst_del_option(team, dst_opts[i]);

	i = option_count - 1;
alloc_rollback:
	for (i--; i >= 0; i--)
286
287
		kfree(dst_opts[i]);

288
	kfree(dst_opts);
289
	return err;
290
}
291

292
293
294
295
296
297
298
299
static void __team_options_mark_removed(struct team *team,
					const struct team_option *option,
					size_t option_count)
{
	int i;

	for (i = 0; i < option_count; i++, option++) {
		struct team_option *del_opt;
300

301
		del_opt = __team_find_option(team, option->name);
302
303
		if (del_opt)
			__team_option_inst_mark_removed_option(team, del_opt);
304
305
	}
}
306
307

static void __team_options_unregister(struct team *team,
308
				      const struct team_option *option,
309
310
311
312
				      size_t option_count)
{
	int i;

313
314
315
316
317
	for (i = 0; i < option_count; i++, option++) {
		struct team_option *del_opt;

		del_opt = __team_find_option(team, option->name);
		if (del_opt) {
318
			__team_option_inst_del_option(team, del_opt);
319
320
321
322
			list_del(&del_opt->list);
			kfree(del_opt);
		}
	}
323
324
}

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
static void __team_options_change_check(struct team *team);

int team_options_register(struct team *team,
			  const struct team_option *option,
			  size_t option_count)
{
	int err;

	err = __team_options_register(team, option, option_count);
	if (err)
		return err;
	__team_options_change_check(team);
	return 0;
}
EXPORT_SYMBOL(team_options_register);

341
342
void team_options_unregister(struct team *team,
			     const struct team_option *option,
343
344
			     size_t option_count)
{
345
346
	__team_options_mark_removed(team, option, option_count);
	__team_options_change_check(team);
347
348
349
350
	__team_options_unregister(team, option, option_count);
}
EXPORT_SYMBOL(team_options_unregister);

351
352
353
354
static int team_option_get(struct team *team,
			   struct team_option_inst *opt_inst,
			   struct team_gsetter_ctx *ctx)
{
355
356
	if (!opt_inst->option->getter)
		return -EOPNOTSUPP;
357
358
359
360
361
362
	return opt_inst->option->getter(team, ctx);
}

static int team_option_set(struct team *team,
			   struct team_option_inst *opt_inst,
			   struct team_gsetter_ctx *ctx)
363
{
364
365
	if (!opt_inst->option->setter)
		return -EOPNOTSUPP;
366
	return opt_inst->option->setter(team, ctx);
367
368
}

Jiri Pirko's avatar
Jiri Pirko committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
{
	struct team_option_inst *opt_inst;

	opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
	opt_inst->changed = true;
}
EXPORT_SYMBOL(team_option_inst_set_change);

void team_options_change_check(struct team *team)
{
	__team_options_change_check(team);
}
EXPORT_SYMBOL(team_options_change_check);


385
386
387
388
389
390
391
/****************
 * Mode handling
 ****************/

static LIST_HEAD(mode_list);
static DEFINE_SPINLOCK(mode_list_lock);

Jiri Pirko's avatar
Jiri Pirko committed
392
393
394
395
396
397
struct team_mode_item {
	struct list_head list;
	const struct team_mode *mode;
};

static struct team_mode_item *__find_mode(const char *kind)
398
{
Jiri Pirko's avatar
Jiri Pirko committed
399
	struct team_mode_item *mitem;
400

Jiri Pirko's avatar
Jiri Pirko committed
401
402
403
	list_for_each_entry(mitem, &mode_list, list) {
		if (strcmp(mitem->mode->kind, kind) == 0)
			return mitem;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	}
	return NULL;
}

static bool is_good_mode_name(const char *name)
{
	while (*name != '\0') {
		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
			return false;
		name++;
	}
	return true;
}

Jiri Pirko's avatar
Jiri Pirko committed
418
int team_mode_register(const struct team_mode *mode)
419
420
{
	int err = 0;
Jiri Pirko's avatar
Jiri Pirko committed
421
	struct team_mode_item *mitem;
422
423
424
425

	if (!is_good_mode_name(mode->kind) ||
	    mode->priv_size > TEAM_MODE_PRIV_SIZE)
		return -EINVAL;
Jiri Pirko's avatar
Jiri Pirko committed
426
427
428
429
430

	mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
	if (!mitem)
		return -ENOMEM;

431
432
433
	spin_lock(&mode_list_lock);
	if (__find_mode(mode->kind)) {
		err = -EEXIST;
Jiri Pirko's avatar
Jiri Pirko committed
434
		kfree(mitem);
435
436
		goto unlock;
	}
Jiri Pirko's avatar
Jiri Pirko committed
437
438
	mitem->mode = mode;
	list_add_tail(&mitem->list, &mode_list);
439
440
441
442
443
444
unlock:
	spin_unlock(&mode_list_lock);
	return err;
}
EXPORT_SYMBOL(team_mode_register);

Jiri Pirko's avatar
Jiri Pirko committed
445
void team_mode_unregister(const struct team_mode *mode)
446
{
Jiri Pirko's avatar
Jiri Pirko committed
447
448
	struct team_mode_item *mitem;

449
	spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
450
451
452
453
454
	mitem = __find_mode(mode->kind);
	if (mitem) {
		list_del_init(&mitem->list);
		kfree(mitem);
	}
455
456
457
458
	spin_unlock(&mode_list_lock);
}
EXPORT_SYMBOL(team_mode_unregister);

Jiri Pirko's avatar
Jiri Pirko committed
459
static const struct team_mode *team_mode_get(const char *kind)
460
{
Jiri Pirko's avatar
Jiri Pirko committed
461
462
	struct team_mode_item *mitem;
	const struct team_mode *mode = NULL;
463
464

	spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
465
466
	mitem = __find_mode(kind);
	if (!mitem) {
467
468
469
		spin_unlock(&mode_list_lock);
		request_module("team-mode-%s", kind);
		spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
470
		mitem = __find_mode(kind);
471
	}
Jiri Pirko's avatar
Jiri Pirko committed
472
473
	if (mitem) {
		mode = mitem->mode;
474
475
		if (!try_module_get(mode->owner))
			mode = NULL;
Jiri Pirko's avatar
Jiri Pirko committed
476
	}
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

	spin_unlock(&mode_list_lock);
	return mode;
}

static void team_mode_put(const struct team_mode *mode)
{
	module_put(mode->owner);
}

static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
{
	dev_kfree_skb_any(skb);
	return false;
}

rx_handler_result_t team_dummy_receive(struct team *team,
				       struct team_port *port,
				       struct sk_buff *skb)
{
	return RX_HANDLER_ANOTHER;
}

500
501
502
503
504
505
506
507
508
509
510
511
512
513
static const struct team_mode __team_no_mode = {
	.kind		= "*NOMODE*",
};

static bool team_is_mode_set(struct team *team)
{
	return team->mode != &__team_no_mode;
}

static void team_set_no_mode(struct team *team)
{
	team->mode = &__team_no_mode;
}

514
static void __team_adjust_ops(struct team *team, int en_port_count)
515
516
517
518
519
520
{
	/*
	 * To avoid checks in rx/tx skb paths, ensure here that non-null and
	 * correct ops are always set.
	 */

521
522
	if (!en_port_count || !team_is_mode_set(team) ||
	    !team->mode->ops->transmit)
523
524
525
526
		team->ops.transmit = team_dummy_transmit;
	else
		team->ops.transmit = team->mode->ops->transmit;

527
528
	if (!en_port_count || !team_is_mode_set(team) ||
	    !team->mode->ops->receive)
529
530
531
532
533
		team->ops.receive = team_dummy_receive;
	else
		team->ops.receive = team->mode->ops->receive;
}

534
535
536
537
538
static void team_adjust_ops(struct team *team)
{
	__team_adjust_ops(team, team->en_port_count);
}

539
540
541
542
543
544
545
546
547
/*
 * We can benefit from the fact that it's ensured no port is present
 * at the time of mode change. Therefore no packets are in fly so there's no
 * need to set mode operations in any special way.
 */
static int __team_change_mode(struct team *team,
			      const struct team_mode *new_mode)
{
	/* Check if mode was previously set and do cleanup if so */
548
	if (team_is_mode_set(team)) {
549
550
551
552
553
554
555
556
557
		void (*exit_op)(struct team *team) = team->ops.exit;

		/* Clear ops area so no callback is called any longer */
		memset(&team->ops, 0, sizeof(struct team_mode_ops));
		team_adjust_ops(team);

		if (exit_op)
			exit_op(team);
		team_mode_put(team->mode);
558
		team_set_no_mode(team);
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
		/* zero private data area */
		memset(&team->mode_priv, 0,
		       sizeof(struct team) - offsetof(struct team, mode_priv));
	}

	if (!new_mode)
		return 0;

	if (new_mode->ops->init) {
		int err;

		err = new_mode->ops->init(team);
		if (err)
			return err;
	}

	team->mode = new_mode;
	memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
	team_adjust_ops(team);

	return 0;
}

static int team_change_mode(struct team *team, const char *kind)
{
Jiri Pirko's avatar
Jiri Pirko committed
584
	const struct team_mode *new_mode;
585
586
587
588
589
590
591
592
	struct net_device *dev = team->dev;
	int err;

	if (!list_empty(&team->port_list)) {
		netdev_err(dev, "No ports can be present during mode change\n");
		return -EBUSY;
	}

593
	if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
		netdev_err(dev, "Unable to change to the same mode the team is in\n");
		return -EINVAL;
	}

	new_mode = team_mode_get(kind);
	if (!new_mode) {
		netdev_err(dev, "Mode \"%s\" not found\n", kind);
		return -EINVAL;
	}

	err = __team_change_mode(team, new_mode);
	if (err) {
		netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
		team_mode_put(new_mode);
		return err;
	}

	netdev_info(dev, "Mode changed to \"%s\"\n", kind);
	return 0;
}


/************************
 * Rx path frame handler
 ************************/

/* note: already called with rcu_read_lock */
static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
{
	struct sk_buff *skb = *pskb;
	struct team_port *port;
	struct team *team;
	rx_handler_result_t res;

	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
		return RX_HANDLER_CONSUMED;

	*pskb = skb;

	port = team_port_get_rcu(skb->dev);
	team = port->team;
636
637
638
639
640
641
	if (!team_port_enabled(port)) {
		/* allow exact match delivery for disabled ports */
		res = RX_HANDLER_EXACT;
	} else {
		res = team->ops.receive(team, port, skb);
	}
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
	if (res == RX_HANDLER_ANOTHER) {
		struct team_pcpu_stats *pcpu_stats;

		pcpu_stats = this_cpu_ptr(team->pcpu_stats);
		u64_stats_update_begin(&pcpu_stats->syncp);
		pcpu_stats->rx_packets++;
		pcpu_stats->rx_bytes += skb->len;
		if (skb->pkt_type == PACKET_MULTICAST)
			pcpu_stats->rx_multicast++;
		u64_stats_update_end(&pcpu_stats->syncp);

		skb->dev = team->dev;
	} else {
		this_cpu_inc(team->pcpu_stats->rx_dropped);
	}

	return res;
}


662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*************************************
 * Multiqueue Tx port select override
 *************************************/

static int team_queue_override_init(struct team *team)
{
	struct list_head *listarr;
	unsigned int queue_cnt = team->dev->num_tx_queues - 1;
	unsigned int i;

	if (!queue_cnt)
		return 0;
	listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
	if (!listarr)
		return -ENOMEM;
	team->qom_lists = listarr;
	for (i = 0; i < queue_cnt; i++)
		INIT_LIST_HEAD(listarr++);
	return 0;
}

static void team_queue_override_fini(struct team *team)
{
	kfree(team->qom_lists);
}

static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
{
	return &team->qom_lists[queue_id - 1];
}

/*
 * note: already called with rcu_read_lock
 */
static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
{
	struct list_head *qom_list;
	struct team_port *port;

	if (!team->queue_override_enabled || !skb->queue_mapping)
		return false;
	qom_list = __team_get_qom_list(team, skb->queue_mapping);
	list_for_each_entry_rcu(port, qom_list, qom_list) {
		if (!team_dev_queue_xmit(team, port, skb))
			return true;
	}
	return false;
}

static void __team_queue_override_port_del(struct team *team,
					   struct team_port *port)
{
	list_del_rcu(&port->qom_list);
	synchronize_rcu();
	INIT_LIST_HEAD(&port->qom_list);
}

static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
						      struct team_port *cur)
{
	if (port->priority < cur->priority)
		return true;
	if (port->priority > cur->priority)
		return false;
	if (port->index < cur->index)
		return true;
	return false;
}

static void __team_queue_override_port_add(struct team *team,
					   struct team_port *port)
{
	struct team_port *cur;
	struct list_head *qom_list;
	struct list_head *node;

	if (!port->queue_id || !team_port_enabled(port))
		return;

	qom_list = __team_get_qom_list(team, port->queue_id);
	node = qom_list;
	list_for_each_entry(cur, qom_list, qom_list) {
		if (team_queue_override_port_has_gt_prio_than(port, cur))
			break;
		node = &cur->qom_list;
	}
	list_add_tail_rcu(&port->qom_list, node);
}

static void __team_queue_override_enabled_check(struct team *team)
{
	struct team_port *port;
	bool enabled = false;

	list_for_each_entry(port, &team->port_list, list) {
		if (!list_empty(&port->qom_list)) {
			enabled = true;
			break;
		}
	}
	if (enabled == team->queue_override_enabled)
		return;
	netdev_dbg(team->dev, "%s queue override\n",
		   enabled ? "Enabling" : "Disabling");
	team->queue_override_enabled = enabled;
}

static void team_queue_override_port_refresh(struct team *team,
					     struct team_port *port)
{
	__team_queue_override_port_del(team, port);
	__team_queue_override_port_add(team, port);
	__team_queue_override_enabled_check(team);
}


778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/****************
 * Port handling
 ****************/

static bool team_port_find(const struct team *team,
			   const struct team_port *port)
{
	struct team_port *cur;

	list_for_each_entry(cur, &team->port_list, list)
		if (cur == port)
			return true;
	return false;
}

/*
794
795
796
797
 * Enable/disable port by adding to enabled port hashlist and setting
 * port->index (Might be racy so reader could see incorrect ifindex when
 * processing a flying packet, but that is not a problem). Write guarded
 * by team->lock.
798
 */
799
800
static void team_port_enable(struct team *team,
			     struct team_port *port)
801
{
802
803
804
	if (team_port_enabled(port))
		return;
	port->index = team->en_port_count++;
805
806
	hlist_add_head_rcu(&port->hlist,
			   team_port_index_hash(team, port->index));
807
	team_adjust_ops(team);
808
	team_queue_override_port_refresh(team, port);
809
810
	if (team->ops.port_enabled)
		team->ops.port_enabled(team, port);
811
812
813
814
815
816
817
}

static void __reconstruct_port_hlist(struct team *team, int rm_index)
{
	int i;
	struct team_port *port;

818
	for (i = rm_index + 1; i < team->en_port_count; i++) {
819
820
821
822
823
824
825
826
		port = team_get_port_by_index(team, i);
		hlist_del_rcu(&port->hlist);
		port->index--;
		hlist_add_head_rcu(&port->hlist,
				   team_port_index_hash(team, port->index));
	}
}

827
828
static void team_port_disable(struct team *team,
			      struct team_port *port)
829
{
830
831
	if (!team_port_enabled(port))
		return;
832
833
	if (team->ops.port_disabled)
		team->ops.port_disabled(team, port);
834
	hlist_del_rcu(&port->hlist);
835
	__reconstruct_port_hlist(team, port->index);
836
	port->index = -1;
837
	team_queue_override_port_refresh(team, port);
838
839
840
841
842
843
844
	__team_adjust_ops(team, team->en_port_count - 1);
	/*
	 * Wait until readers see adjusted ops. This ensures that
	 * readers never see team->en_port_count == 0
	 */
	synchronize_rcu();
	team->en_port_count--;
845
846
847
848
849
850
851
852
853
854
855
}

#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
			    NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
			    NETIF_F_HIGHDMA | NETIF_F_LRO)

static void __team_compute_features(struct team *team)
{
	struct team_port *port;
	u32 vlan_features = TEAM_VLAN_FEATURES;
	unsigned short max_hard_header_len = ETH_HLEN;
856
	unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
857
858
859
860
861
862

	list_for_each_entry(port, &team->port_list, list) {
		vlan_features = netdev_increment_features(vlan_features,
					port->dev->vlan_features,
					TEAM_VLAN_FEATURES);

863
		dst_release_flag &= port->dev->priv_flags;
864
865
866
867
868
869
870
		if (port->dev->hard_header_len > max_hard_header_len)
			max_hard_header_len = port->dev->hard_header_len;
	}

	team->dev->vlan_features = vlan_features;
	team->dev->hard_header_len = max_hard_header_len;

871
872
873
	flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
	team->dev->priv_flags = flags | dst_release_flag;

874
875
876
877
878
	netdev_change_features(team->dev);
}

static void team_compute_features(struct team *team)
{
879
	mutex_lock(&team->lock);
880
	__team_compute_features(team);
881
	mutex_unlock(&team->lock);
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
}

static int team_port_enter(struct team *team, struct team_port *port)
{
	int err = 0;

	dev_hold(team->dev);
	port->dev->priv_flags |= IFF_TEAM_PORT;
	if (team->ops.port_enter) {
		err = team->ops.port_enter(team, port);
		if (err) {
			netdev_err(team->dev, "Device %s failed to enter team mode\n",
				   port->dev->name);
			goto err_port_enter;
		}
	}

	return 0;

err_port_enter:
	port->dev->priv_flags &= ~IFF_TEAM_PORT;
	dev_put(team->dev);

	return err;
}

static void team_port_leave(struct team *team, struct team_port *port)
{
	if (team->ops.port_leave)
		team->ops.port_leave(team, port);
	port->dev->priv_flags &= ~IFF_TEAM_PORT;
	dev_put(team->dev);
}

Jiri Pirko's avatar
Jiri Pirko committed
916
#ifdef CONFIG_NET_POLL_CONTROLLER
917
918
static int team_port_enable_netpoll(struct team *team, struct team_port *port,
				    gfp_t gfp)
Jiri Pirko's avatar
Jiri Pirko committed
919
920
921
922
{
	struct netpoll *np;
	int err;

923
	np = kzalloc(sizeof(*np), gfp);
Jiri Pirko's avatar
Jiri Pirko committed
924
925
926
	if (!np)
		return -ENOMEM;

927
	err = __netpoll_setup(np, port->dev, gfp);
Jiri Pirko's avatar
Jiri Pirko committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
	if (err) {
		kfree(np);
		return err;
	}
	port->np = np;
	return err;
}

static void team_port_disable_netpoll(struct team_port *port)
{
	struct netpoll *np = port->np;

	if (!np)
		return;
	port->np = NULL;

	/* Wait for transmitting packets to finish before freeing. */
	synchronize_rcu_bh();
	__netpoll_cleanup(np);
	kfree(np);
}

static struct netpoll_info *team_netpoll_info(struct team *team)
{
	return team->dev->npinfo;
}

#else
956
957
static int team_port_enable_netpoll(struct team *team, struct team_port *port,
				    gfp_t gfp)
Jiri Pirko's avatar
Jiri Pirko committed
958
959
960
961
962
963
964
965
966
967
968
969
{
	return 0;
}
static void team_port_disable_netpoll(struct team_port *port)
{
}
static struct netpoll_info *team_netpoll_info(struct team *team)
{
	return NULL;
}
#endif

970
static void __team_port_change_port_added(struct team_port *port, bool linkup);
971
972
static int team_dev_type_check_change(struct net_device *dev,
				      struct net_device *port_dev);
973
974
975
976
977
978
979
980

static int team_port_add(struct team *team, struct net_device *port_dev)
{
	struct net_device *dev = team->dev;
	struct team_port *port;
	char *portname = port_dev->name;
	int err;

981
982
	if (port_dev->flags & IFF_LOOPBACK) {
		netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
983
984
985
986
987
988
989
990
991
992
			   portname);
		return -EINVAL;
	}

	if (team_port_exists(port_dev)) {
		netdev_err(dev, "Device %s is already a port "
				"of a team device\n", portname);
		return -EBUSY;
	}

993
994
995
996
997
998
999
	if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
	    vlan_uses_dev(dev)) {
		netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
			   portname);
		return -EPERM;
	}

1000
	err = team_dev_type_check_change(dev, port_dev);
For faster browsing, not all history is shown. View entire blame