summaryrefslogtreecommitdiffstats
path: root/drivers/net/net_failover.c
blob: 83f7420ddea569126db0cc25719940892d760075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
207
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
584
585
586
587
588
589
590
591
592
593
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2018, Intel Corporation. */

/* This provides a net_failover interface for paravirtual drivers to
 * provide an alternate datapath by exporting APIs to create and
 * destroy a upper 'net_failover' netdev. The upper dev manages the
 * original paravirtual interface as a 'standby' netdev and uses the
 * generic failover infrastructure to register and manage a direct
 * attached VF as a 'primary' netdev. This enables live migration of
 * a VM with direct attached VF by failing over to the paravirtual
 * datapath when the VF is unplugged.
 *
 * Some of the netdev management routines are based on bond/team driver as
 * this driver provides active-backup functionality similar to those drivers.
 */

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/netpoll.h>
#include <linux/rtnetlink.h>
#include <linux/if_vlan.h>
#include <linux/pci.h>
#include <net/sch_generic.h>
#include <uapi/linux/if_arp.h>
#include <net/net_failover.h>

static bool net_failover_xmit_ready(struct net_device *dev)
{
	return netif_running(dev) && netif_carrier_ok(dev);
}

static int net_failover_open(struct net_device *dev)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev, *standby_dev;
	int err;

	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	if (primary_dev) {
		err = dev_open(primary_dev);
		if (err)
			goto err_primary_open;
	}

	standby_dev = rtnl_dereference(nfo_info->standby_dev);
	if (standby_dev) {
		err = dev_open(standby_dev);
		if (err)
			goto err_standby_open;
	}

	if ((primary_dev && net_failover_xmit_ready(primary_dev)) ||
	    (standby_dev && net_failover_xmit_ready(standby_dev))) {
		netif_carrier_on(dev);
		netif_tx_wake_all_queues(dev);
	}

	return 0;

err_standby_open:
	dev_close(primary_dev);
err_primary_open:
	netif_tx_disable(dev);
	return err;
}

static int net_failover_close(struct net_device *dev)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *slave_dev;

	netif_tx_disable(dev);

	slave_dev = rtnl_dereference(nfo_info->primary_dev);
	if (slave_dev)
		dev_close(slave_dev);

	slave_dev = rtnl_dereference(nfo_info->standby_dev);
	if (slave_dev)
		dev_close(slave_dev);

	return 0;
}

static netdev_tx_t net_failover_drop_xmit(struct sk_buff *skb,
					  struct net_device *dev)
{
	atomic_long_inc(&dev->tx_dropped);
	dev_kfree_skb_any(skb);
	return NETDEV_TX_OK;
}

static netdev_tx_t net_failover_start_xmit(struct sk_buff *skb,
					   struct net_device *dev)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *xmit_dev;

	/* Try xmit via primary netdev followed by standby netdev */
	xmit_dev = rcu_dereference_bh(nfo_info->primary_dev);
	if (!xmit_dev || !net_failover_xmit_ready(xmit_dev)) {
		xmit_dev = rcu_dereference_bh(nfo_info->standby_dev);
		if (!xmit_dev || !net_failover_xmit_ready(xmit_dev))
			return net_failover_drop_xmit(skb, dev);
	}

	skb->dev = xmit_dev;
	skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;

	return dev_queue_xmit(skb);
}

static u16 net_failover_select_queue(struct net_device *dev,
				     struct sk_buff *skb, void *accel_priv,
				     select_queue_fallback_t fallback)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev;
	u16 txq;

	primary_dev = rcu_dereference(nfo_info->primary_dev);
	if (primary_dev) {
		const struct net_device_ops *ops = primary_dev->netdev_ops;

		if (ops->ndo_select_queue)
			txq = ops->ndo_select_queue(primary_dev, skb,
						    accel_priv, fallback);
		else
			txq = fallback(primary_dev, skb);

		qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;

		return txq;
	}

	txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;

	/* Save the original txq to restore before passing to the driver */
	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;

	if (unlikely(txq >= dev->real_num_tx_queues)) {
		do {
			txq -= dev->real_num_tx_queues;
		} while (txq >= dev->real_num_tx_queues);
	}

	return txq;
}

/* fold stats, assuming all rtnl_link_stats64 fields are u64, but
 * that some drivers can provide 32bit values only.
 */
static void net_failover_fold_stats(struct rtnl_link_stats64 *_res,
				    const struct rtnl_link_stats64 *_new,
				    const struct rtnl_link_stats64 *_old)
{
	const u64 *new = (const u64 *)_new;
	const u64 *old = (const u64 *)_old;
	u64 *res = (u64 *)_res;
	int i;

	for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
		u64 nv = new[i];
		u64 ov = old[i];
		s64 delta = nv - ov;

		/* detects if this particular field is 32bit only */
		if (((nv | ov) >> 32) == 0)
			delta = (s64)(s32)((u32)nv - (u32)ov);

		/* filter anomalies, some drivers reset their stats
		 * at down/up events.
		 */
		if (delta > 0)
			res[i] += delta;
	}
}

static void net_failover_get_stats(struct net_device *dev,
				   struct rtnl_link_stats64 *stats)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	const struct rtnl_link_stats64 *new;
	struct rtnl_link_stats64 temp;
	struct net_device *slave_dev;

	spin_lock(&nfo_info->stats_lock);
	memcpy(stats, &nfo_info->failover_stats, sizeof(*stats));

	rcu_read_lock();

	slave_dev = rcu_dereference(nfo_info->primary_dev);
	if (slave_dev) {
		new = dev_get_stats(slave_dev, &temp);
		net_failover_fold_stats(stats, new, &nfo_info->primary_stats);
		memcpy(&nfo_info->primary_stats, new, sizeof(*new));
	}

	slave_dev = rcu_dereference(nfo_info->standby_dev);
	if (slave_dev) {
		new = dev_get_stats(slave_dev, &temp);
		net_failover_fold_stats(stats, new, &nfo_info->standby_stats);
		memcpy(&nfo_info->standby_stats, new, sizeof(*new));
	}

	rcu_read_unlock();

	memcpy(&nfo_info->failover_stats, stats, sizeof(*stats));
	spin_unlock(&nfo_info->stats_lock);
}

static int net_failover_change_mtu(struct net_device *dev, int new_mtu)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev, *standby_dev;
	int ret = 0;

	primary_dev = rcu_dereference(nfo_info->primary_dev);
	if (primary_dev) {
		ret = dev_set_mtu(primary_dev, new_mtu);
		if (ret)
			return ret;
	}

	standby_dev = rcu_dereference(nfo_info->standby_dev);
	if (standby_dev) {
		ret = dev_set_mtu(standby_dev, new_mtu);
		if (ret) {
			if (primary_dev)
				dev_set_mtu(primary_dev, dev->mtu);
			return ret;
		}
	}

	dev->mtu = new_mtu;

	return 0;
}

static void net_failover_set_rx_mode(struct net_device *dev)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *slave_dev;

	rcu_read_lock();

	slave_dev = rcu_dereference(nfo_info->primary_dev);
	if (slave_dev) {
		dev_uc_sync_multiple(slave_dev, dev);
		dev_mc_sync_multiple(slave_dev, dev);
	}

	slave_dev = rcu_dereference(nfo_info->standby_dev);
	if (slave_dev) {
		dev_uc_sync_multiple(slave_dev, dev);
		dev_mc_sync_multiple(slave_dev, dev);
	}

	rcu_read_unlock();
}

static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
					u16 vid)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev, *standby_dev;
	int ret = 0;

	primary_dev = rcu_dereference(nfo_info->primary_dev);
	if (primary_dev) {
		ret = vlan_vid_add(primary_dev, proto, vid);
		if (ret)
			return ret;
	}

	standby_dev = rcu_dereference(nfo_info->standby_dev);
	if (standby_dev) {
		ret = vlan_vid_add(standby_dev, proto, vid);
		if (ret)
			if (primary_dev)
				vlan_vid_del(primary_dev, proto, vid);
	}

	return ret;
}

static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
					 u16 vid)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *slave_dev;

	slave_dev = rcu_dereference(nfo_info->primary_dev);
	if (slave_dev)
		vlan_vid_del(slave_dev, proto, vid);

	slave_dev = rcu_dereference(nfo_info->standby_dev);
	if (slave_dev)
		vlan_vid_del(slave_dev, proto, vid);

	return 0;
}

static const struct net_device_ops failover_dev_ops = {
	.ndo_open		= net_failover_open,
	.ndo_stop		= net_failover_close,
	.ndo_start_xmit		= net_failover_start_xmit,
	.ndo_select_queue	= net_failover_select_queue,
	.ndo_get_stats64	= net_failover_get_stats,
	.ndo_change_mtu		= net_failover_change_mtu,
	.ndo_set_rx_mode	= net_failover_set_rx_mode,
	.ndo_vlan_rx_add_vid	= net_failover_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= net_failover_vlan_rx_kill_vid,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_features_check	= passthru_features_check,
};

#define FAILOVER_NAME "net_failover"
#define FAILOVER_VERSION "0.1"

static void nfo_ethtool_get_drvinfo(struct net_device *dev,
				    struct ethtool_drvinfo *drvinfo)
{
	strlcpy(drvinfo->driver, FAILOVER_NAME, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, FAILOVER_VERSION, sizeof(drvinfo->version));
}

static int nfo_ethtool_get_link_ksettings(struct net_device *dev,
					  struct ethtool_link_ksettings *cmd)
{
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *slave_dev;

	slave_dev = rtnl_dereference(nfo_info->primary_dev);
	if (!slave_dev || !net_failover_xmit_ready(slave_dev)) {
		slave_dev = rtnl_dereference(nfo_info->standby_dev);
		if (!slave_dev || !net_failover_xmit_ready(slave_dev)) {
			cmd->base.duplex = DUPLEX_UNKNOWN;
			cmd->base.port = PORT_OTHER;
			cmd->base.speed = SPEED_UNKNOWN;

			return 0;
		}
	}

	return __ethtool_get_link_ksettings(slave_dev, cmd);
}

static const struct ethtool_ops failover_ethtool_ops = {
	.get_drvinfo            = nfo_ethtool_get_drvinfo,
	.get_link               = ethtool_op_get_link,
	.get_link_ksettings     = nfo_ethtool_get_link_ksettings,
};

/* Called when slave dev is injecting data into network stack.
 * Change the associated network device from lower dev to failover dev.
 * note: already called with rcu_read_lock
 */
static rx_handler_result_t net_failover_handle_frame(struct sk_buff **pskb)
{
	struct sk_buff *skb = *pskb;
	struct net_device *dev = rcu_dereference(skb->dev->rx_handler_data);
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev, *standby_dev;

	primary_dev = rcu_dereference(nfo_info->primary_dev);
	standby_dev = rcu_dereference(nfo_info->standby_dev);

	if (primary_dev && skb->dev == standby_dev)
		return RX_HANDLER_EXACT;

	skb->dev = dev;

	return RX_HANDLER_ANOTHER;
}

static void net_failover_compute_features(struct net_device *dev)
{
	netdev_features_t vlan_features = FAILOVER_VLAN_FEATURES &
					  NETIF_F_ALL_FOR_ALL;
	netdev_features_t enc_features  = FAILOVER_ENC_FEATURES;
	unsigned short max_hard_header_len = ETH_HLEN;
	unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
					IFF_XMIT_DST_RELEASE_PERM;
	struct net_failover_info *nfo_info = netdev_priv(dev);
	struct net_device *primary_dev, *standby_dev;

	primary_dev = rcu_dereference(nfo_info->primary_dev);
	if (primary_dev) {
		vlan_features =
			netdev_increment_features(vlan_features,
						  primary_dev->vlan_features,
						  FAILOVER_VLAN_FEATURES);
		enc_features =
			netdev_increment_features(enc_features,
						  primary_dev->hw_enc_features,
						  FAILOVER_ENC_FEATURES);

		dst_release_flag &= primary_dev->priv_flags;
		if (primary_dev->hard_header_len > max_hard_header_len)
			max_hard_header_len = primary_dev->hard_header_len;
	}

	standby_dev = rcu_dereference(nfo_info->standby_dev);
	if (standby_dev) {
		vlan_features =
			netdev_increment_features(vlan_features,
						  standby_dev->vlan_features,
						  FAILOVER_VLAN_FEATURES);
		enc_features =
			netdev_increment_features(enc_features,
						  standby_dev->hw_enc_features,
						  FAILOVER_ENC_FEATURES);

		dst_release_flag &= standby_dev->priv_flags;
		if (standby_dev->hard_header_len > max_hard_header_len)
			max_hard_header_len = standby_dev->hard_header_len;
	}

	dev->vlan_features = vlan_features;
	dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL;
	dev->hard_header_len = max_hard_header_len;

	dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
	if (dst_release_flag == (IFF_XMIT_DST_RELEASE |
				 IFF_XMIT_DST_RELEASE_PERM))
		dev->priv_flags |= IFF_XMIT_DST_RELEASE;

	netdev_change_features(dev);
}

static void net_failover_lower_state_changed(struct net_device *slave_dev,
					     struct net_device *primary_dev,
					     struct net_device *standby_dev)
{
	struct netdev_lag_lower_state_info info;

	if (netif_carrier_ok(slave_dev))
		info.link_up = true;
	else
		info.link_up = false;

	if (slave_dev == primary_dev) {
		if (netif_running(primary_dev))
			info.tx_enabled = true;
		else
			info.tx_enabled = false;
	} else {
		if ((primary_dev && netif_running(primary_dev)) ||
		    (!netif_running(standby_dev)))
			info.tx_enabled = false;
		else
			info.tx_enabled = true;
	}

	netdev_lower_state_changed(slave_dev, &info);
}

static int net_failover_slave_pre_register(struct net_device *slave_dev,
					   struct net_device *failover_dev)
{
	struct net_device *standby_dev, *primary_dev;
	struct net_failover_info *nfo_info;
	bool slave_is_standby;

	nfo_info = netdev_priv(failover_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);
	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;
	if (slave_is_standby ? standby_dev : primary_dev) {
		netdev_err(failover_dev, "%s attempting to register as slave dev when %s already present\n",
			   slave_dev->name,
			   slave_is_standby ? "standby" : "primary");
		return -EINVAL;
	}

	/* We want to allow only a direct attached VF device as a primary
	 * netdev. As there is no easy way to check for a VF device, restrict
	 * this to a pci device.
	 */
	if (!slave_is_standby && (!slave_dev->dev.parent ||
				  !dev_is_pci(slave_dev->dev.parent)))
		return -EINVAL;

	if (failover_dev->features & NETIF_F_VLAN_CHALLENGED &&
	    vlan_uses_dev(failover_dev)) {
		netdev_err(failover_dev, "Device %s is VLAN challenged and failover device has VLAN set up\n",
			   failover_dev->name);
		return -EINVAL;
	}

	return 0;
}

static int net_failover_slave_register(struct net_device *slave_dev,
				       struct net_device *failover_dev)
{
	struct net_device *standby_dev, *primary_dev;
	struct net_failover_info *nfo_info;
	bool slave_is_standby;
	u32 orig_mtu;
	int err;

	/* Align MTU of slave with failover dev */
	orig_mtu = slave_dev->mtu;
	err = dev_set_mtu(slave_dev, failover_dev->mtu);
	if (err) {
		netdev_err(failover_dev, "unable to change mtu of %s to %u register failed\n",
			   slave_dev->name, failover_dev->mtu);
		goto done;
	}

	dev_hold(slave_dev);

	if (netif_running(failover_dev)) {
		err = dev_open(slave_dev);
		if (err && (err != -EBUSY)) {
			netdev_err(failover_dev, "Opening slave %s failed err:%d\n",
				   slave_dev->name, err);
			goto err_dev_open;
		}
	}

	netif_addr_lock_bh(failover_dev);
	dev_uc_sync_multiple(slave_dev, failover_dev);
	dev_uc_sync_multiple(slave_dev, failover_dev);
	netif_addr_unlock_bh(failover_dev);

	err = vlan_vids_add_by_dev(slave_dev, failover_dev);
	if (err) {
		netdev_err(failover_dev, "Failed to add vlan ids to device %s err:%d\n",
			   slave_dev->name, err);
		goto err_vlan_add;
	}

	nfo_info = netdev_priv(failover_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);
	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;

	if (slave_is_standby) {
		rcu_assign_pointer(nfo_info->standby_dev, slave_dev);
		standby_dev = slave_dev;
		dev_get_stats(standby_dev, &nfo_info->standby_stats);
	} else {
		rcu_assign_pointer(nfo_info->primary_dev, slave_dev);
		primary_dev = slave_dev;
		dev_get_stats(primary_dev, &nfo_info->primary_stats);
		failover_dev->min_mtu = slave_dev->min_mtu;
		failover_dev->max_mtu = slave_dev->max_mtu;
	}

	net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev);
	net_failover_compute_features(failover_dev);

	call_netdevice_notifiers(NETDEV_JOIN, slave_dev);

	netdev_info(failover_dev, "failover %s slave:%s registered\n",
		    slave_is_standby ? "standby" : "primary", slave_dev->name);

	return 0;

err_vlan_add:
	dev_uc_unsync(slave_dev, failover_dev);
	dev_mc_unsync(slave_dev, failover_dev);
	dev_close(slave_dev);
err_dev_open:
	dev_put(slave_dev);
	dev_set_mtu(slave_dev, orig_mtu);
done:
	return err;
}

static int net_failover_slave_pre_unregister(struct net_device *slave_dev,
					     struct net_device *failover_dev)
{
	struct net_device *standby_dev, *primary_dev;
	struct net_failover_info *nfo_info;

	nfo_info = netdev_priv(failover_dev);
	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);

	if (slave_dev != primary_dev && slave_dev != standby_dev)
		return -ENODEV;

	return 0;
}

static int net_failover_slave_unregister(struct net_device *slave_dev,
					 struct net_device *failover_dev)
{
	struct net_device *standby_dev, *primary_dev;
	struct net_failover_info *nfo_info;
	bool slave_is_standby;

	nfo_info = netdev_priv(failover_dev);
	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);

	vlan_vids_del_by_dev(slave_dev, failover_dev);
	dev_uc_unsync(slave_dev, failover_dev);
	dev_mc_unsync(slave_dev, failover_dev);
	dev_close(slave_dev);

	nfo_info = netdev_priv(failover_dev);
	dev_get_stats(failover_dev, &nfo_info->failover_stats);

	slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;
	if (slave_is_standby) {
		RCU_INIT_POINTER(nfo_info->standby_dev, NULL);
	} else {
		RCU_INIT_POINTER(nfo_info->primary_dev, NULL);
		if (standby_dev) {
			failover_dev->min_mtu = standby_dev->min_mtu;
			failover_dev->max_mtu = standby_dev->max_mtu;
		}
	}

	dev_put(slave_dev);

	net_failover_compute_features(failover_dev);

	netdev_info(failover_dev, "failover %s slave:%s unregistered\n",
		    slave_is_standby ? "standby" : "primary", slave_dev->name);

	return 0;
}

static int net_failover_slave_link_change(struct net_device *slave_dev,
					  struct net_device *failover_dev)
{
	struct net_device *primary_dev, *standby_dev;
	struct net_failover_info *nfo_info;

	nfo_info = netdev_priv(failover_dev);

	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);

	if (slave_dev != primary_dev && slave_dev != standby_dev)
		return -ENODEV;

	if ((primary_dev && net_failover_xmit_ready(primary_dev)) ||
	    (standby_dev && net_failover_xmit_ready(standby_dev))) {
		netif_carrier_on(failover_dev);
		netif_tx_wake_all_queues(failover_dev);
	} else {
		dev_get_stats(failover_dev, &nfo_info->failover_stats);
		netif_carrier_off(failover_dev);
		netif_tx_stop_all_queues(failover_dev);
	}

	net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev);

	return 0;
}

static int net_failover_slave_name_change(struct net_device *slave_dev,
					  struct net_device *failover_dev)
{
	struct net_device *primary_dev, *standby_dev;
	struct net_failover_info *nfo_info;

	nfo_info = netdev_priv(failover_dev);

	primary_dev = rtnl_dereference(nfo_info->primary_dev);
	standby_dev = rtnl_dereference(nfo_info->standby_dev);

	if (slave_dev != primary_dev && slave_dev != standby_dev)
		return -ENODEV;

	/* We need to bring up the slave after the rename by udev in case
	 * open failed with EBUSY when it was registered.
	 */
	dev_open(slave_dev);

	return 0;
}

static struct failover_ops net_failover_ops = {
	.slave_pre_register	= net_failover_slave_pre_register,
	.slave_register		= net_failover_slave_register,
	.slave_pre_unregister	= net_failover_slave_pre_unregister,
	.slave_unregister	= net_failover_slave_unregister,
	.slave_link_change	= net_failover_slave_link_change,
	.slave_name_change	= net_failover_slave_name_change,
	.slave_handle_frame	= net_failover_handle_frame,
};

/**
 * net_failover_create - Create and register a failover instance
 *
 * @dev: standby netdev
 *
 * Creates a failover netdev and registers a failover instance for a standby
 * netdev. Used by paravirtual drivers that use 3-netdev model.
 * The failover netdev acts as a master device and controls 2 slave devices -
 * the original standby netdev and a VF netdev with the same MAC gets
 * registered as primary netdev.
 *
 * Return: pointer to failover instance
 */
struct failover *net_failover_create(struct net_device *standby_dev)
{
	struct device *dev = standby_dev->dev.parent;
	struct net_device *failover_dev;
	struct failover *failover;
	int err;

	/* Alloc at least 2 queues, for now we are going with 16 assuming
	 * that VF devices being enslaved won't have too many queues.
	 */
	failover_dev = alloc_etherdev_mq(sizeof(struct net_failover_info), 16);
	if (!failover_dev) {
		dev_err(dev, "Unable to allocate failover_netdev!\n");
		return ERR_PTR(-ENOMEM);
	}

	dev_net_set(failover_dev, dev_net(standby_dev));
	SET_NETDEV_DEV(failover_dev, dev);

	failover_dev->netdev_ops = &failover_dev_ops;
	failover_dev->ethtool_ops = &failover_ethtool_ops;

	/* Initialize the device options */
	failover_dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
	failover_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
				       IFF_TX_SKB_SHARING);

	/* don't acquire failover netdev's netif_tx_lock when transmitting */
	failover_dev->features |= NETIF_F_LLTX;

	/* Don't allow failover devices to change network namespaces. */
	failover_dev->features |= NETIF_F_NETNS_LOCAL;

	failover_dev->hw_features = FAILOVER_VLAN_FEATURES |
				    NETIF_F_HW_VLAN_CTAG_TX |
				    NETIF_F_HW_VLAN_CTAG_RX |
				    NETIF_F_HW_VLAN_CTAG_FILTER;

	failover_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
	failover_dev->features |= failover_dev->hw_features;

	memcpy(failover_dev->dev_addr, standby_dev->dev_addr,
	       failover_dev->addr_len);

	failover_dev->min_mtu = standby_dev->min_mtu;
	failover_dev->max_mtu = standby_dev->max_mtu;

	err = register_netdev(failover_dev);
	if (err) {
		dev_err(dev, "Unable to register failover_dev!\n");
		goto err_register_netdev;
	}

	netif_carrier_off(failover_dev);

	failover = failover_register(failover_dev, &net_failover_ops);
	if (IS_ERR(failover))
		goto err_failover_register;

	return failover;

err_failover_register:
	unregister_netdev(failover_dev);
err_register_netdev:
	free_netdev(failover_dev);

	return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(net_failover_create);

/**
 * net_failover_destroy - Destroy a failover instance
 *
 * @failover: pointer to failover instance
 *
 * Unregisters any slave netdevs associated with the failover instance by
 * calling failover_slave_unregister().
 * unregisters the failover instance itself and finally frees the failover
 * netdev. Used by paravirtual drivers that use 3-netdev model.
 *
 */
void net_failover_destroy(struct failover *failover)
{
	struct net_failover_info *nfo_info;
	struct net_device *failover_dev;
	struct net_device *slave_dev;

	if (!failover)
		return;

	failover_dev = rcu_dereference(failover->failover_dev);
	nfo_info = netdev_priv(failover_dev);

	netif_device_detach(failover_dev);

	rtnl_lock();

	slave_dev = rtnl_dereference(nfo_info->primary_dev);
	if (slave_dev)
		failover_slave_unregister(slave_dev);

	slave_dev = rtnl_dereference(nfo_info->standby_dev);
	if (slave_dev)
		failover_slave_unregister(slave_dev);

	failover_unregister(failover);

	unregister_netdevice(failover_dev);

	rtnl_unlock();

	free_netdev(failover_dev);
}
EXPORT_SYMBOL_GPL(net_failover_destroy);

static __init int
net_failover_init(void)
{
	return 0;
}
module_init(net_failover_init);

static __exit
void net_failover_exit(void)
{
}
module_exit(net_failover_exit);

MODULE_DESCRIPTION("Failover driver for Paravirtual drivers");
MODULE_LICENSE("GPL v2");
OpenPOWER on IntegriCloud