1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 /* This provides a net_failover interface for paravirtual drivers to
5 * provide an alternate datapath by exporting APIs to create and
6 * destroy a upper 'net_failover' netdev. The upper dev manages the
7 * original paravirtual interface as a 'standby' netdev and uses the
8 * generic failover infrastructure to register and manage a direct
9 * attached VF as a 'primary' netdev. This enables live migration of
10 * a VM with direct attached VF by failing over to the paravirtual
11 * datapath when the VF is unplugged.
13 * Some of the netdev management routines are based on bond/team driver as
14 * this driver provides active-backup functionality similar to those drivers.
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/netdevice.h>
23 #include <linux/netpoll.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/if_vlan.h>
26 #include <linux/pci.h>
27 #include <net/sch_generic.h>
28 #include <uapi/linux/if_arp.h>
29 #include <net/net_failover.h>
31 static bool net_failover_xmit_ready(struct net_device
*dev
)
33 return netif_running(dev
) && netif_carrier_ok(dev
);
36 static int net_failover_open(struct net_device
*dev
)
38 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
39 struct net_device
*primary_dev
, *standby_dev
;
42 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
44 err
= dev_open(primary_dev
);
46 goto err_primary_open
;
49 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
51 err
= dev_open(standby_dev
);
53 goto err_standby_open
;
56 if ((primary_dev
&& net_failover_xmit_ready(primary_dev
)) ||
57 (standby_dev
&& net_failover_xmit_ready(standby_dev
))) {
58 netif_carrier_on(dev
);
59 netif_tx_wake_all_queues(dev
);
65 dev_close(primary_dev
);
67 netif_tx_disable(dev
);
71 static int net_failover_close(struct net_device
*dev
)
73 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
74 struct net_device
*slave_dev
;
76 netif_tx_disable(dev
);
78 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
82 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
89 static netdev_tx_t
net_failover_drop_xmit(struct sk_buff
*skb
,
90 struct net_device
*dev
)
92 atomic_long_inc(&dev
->tx_dropped
);
93 dev_kfree_skb_any(skb
);
97 static netdev_tx_t
net_failover_start_xmit(struct sk_buff
*skb
,
98 struct net_device
*dev
)
100 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
101 struct net_device
*xmit_dev
;
103 /* Try xmit via primary netdev followed by standby netdev */
104 xmit_dev
= rcu_dereference_bh(nfo_info
->primary_dev
);
105 if (!xmit_dev
|| !net_failover_xmit_ready(xmit_dev
)) {
106 xmit_dev
= rcu_dereference_bh(nfo_info
->standby_dev
);
107 if (!xmit_dev
|| !net_failover_xmit_ready(xmit_dev
))
108 return net_failover_drop_xmit(skb
, dev
);
112 skb
->queue_mapping
= qdisc_skb_cb(skb
)->slave_dev_queue_mapping
;
114 return dev_queue_xmit(skb
);
117 static u16
net_failover_select_queue(struct net_device
*dev
,
119 struct net_device
*sb_dev
,
120 select_queue_fallback_t fallback
)
122 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
123 struct net_device
*primary_dev
;
126 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
128 const struct net_device_ops
*ops
= primary_dev
->netdev_ops
;
130 if (ops
->ndo_select_queue
)
131 txq
= ops
->ndo_select_queue(primary_dev
, skb
,
134 txq
= fallback(primary_dev
, skb
, NULL
);
136 qdisc_skb_cb(skb
)->slave_dev_queue_mapping
= skb
->queue_mapping
;
141 txq
= skb_rx_queue_recorded(skb
) ? skb_get_rx_queue(skb
) : 0;
143 /* Save the original txq to restore before passing to the driver */
144 qdisc_skb_cb(skb
)->slave_dev_queue_mapping
= skb
->queue_mapping
;
146 if (unlikely(txq
>= dev
->real_num_tx_queues
)) {
148 txq
-= dev
->real_num_tx_queues
;
149 } while (txq
>= dev
->real_num_tx_queues
);
155 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
156 * that some drivers can provide 32bit values only.
158 static void net_failover_fold_stats(struct rtnl_link_stats64
*_res
,
159 const struct rtnl_link_stats64
*_new
,
160 const struct rtnl_link_stats64
*_old
)
162 const u64
*new = (const u64
*)_new
;
163 const u64
*old
= (const u64
*)_old
;
164 u64
*res
= (u64
*)_res
;
167 for (i
= 0; i
< sizeof(*_res
) / sizeof(u64
); i
++) {
172 /* detects if this particular field is 32bit only */
173 if (((nv
| ov
) >> 32) == 0)
174 delta
= (s64
)(s32
)((u32
)nv
- (u32
)ov
);
176 /* filter anomalies, some drivers reset their stats
184 static void net_failover_get_stats(struct net_device
*dev
,
185 struct rtnl_link_stats64
*stats
)
187 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
188 const struct rtnl_link_stats64
*new;
189 struct rtnl_link_stats64 temp
;
190 struct net_device
*slave_dev
;
192 spin_lock(&nfo_info
->stats_lock
);
193 memcpy(stats
, &nfo_info
->failover_stats
, sizeof(*stats
));
197 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
199 new = dev_get_stats(slave_dev
, &temp
);
200 net_failover_fold_stats(stats
, new, &nfo_info
->primary_stats
);
201 memcpy(&nfo_info
->primary_stats
, new, sizeof(*new));
204 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
206 new = dev_get_stats(slave_dev
, &temp
);
207 net_failover_fold_stats(stats
, new, &nfo_info
->standby_stats
);
208 memcpy(&nfo_info
->standby_stats
, new, sizeof(*new));
213 memcpy(&nfo_info
->failover_stats
, stats
, sizeof(*stats
));
214 spin_unlock(&nfo_info
->stats_lock
);
217 static int net_failover_change_mtu(struct net_device
*dev
, int new_mtu
)
219 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
220 struct net_device
*primary_dev
, *standby_dev
;
223 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
225 ret
= dev_set_mtu(primary_dev
, new_mtu
);
230 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
232 ret
= dev_set_mtu(standby_dev
, new_mtu
);
235 dev_set_mtu(primary_dev
, dev
->mtu
);
245 static void net_failover_set_rx_mode(struct net_device
*dev
)
247 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
248 struct net_device
*slave_dev
;
252 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
254 dev_uc_sync_multiple(slave_dev
, dev
);
255 dev_mc_sync_multiple(slave_dev
, dev
);
258 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
260 dev_uc_sync_multiple(slave_dev
, dev
);
261 dev_mc_sync_multiple(slave_dev
, dev
);
267 static int net_failover_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
,
270 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
271 struct net_device
*primary_dev
, *standby_dev
;
274 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
276 ret
= vlan_vid_add(primary_dev
, proto
, vid
);
281 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
283 ret
= vlan_vid_add(standby_dev
, proto
, vid
);
286 vlan_vid_del(primary_dev
, proto
, vid
);
292 static int net_failover_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
,
295 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
296 struct net_device
*slave_dev
;
298 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
300 vlan_vid_del(slave_dev
, proto
, vid
);
302 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
304 vlan_vid_del(slave_dev
, proto
, vid
);
309 static const struct net_device_ops failover_dev_ops
= {
310 .ndo_open
= net_failover_open
,
311 .ndo_stop
= net_failover_close
,
312 .ndo_start_xmit
= net_failover_start_xmit
,
313 .ndo_select_queue
= net_failover_select_queue
,
314 .ndo_get_stats64
= net_failover_get_stats
,
315 .ndo_change_mtu
= net_failover_change_mtu
,
316 .ndo_set_rx_mode
= net_failover_set_rx_mode
,
317 .ndo_vlan_rx_add_vid
= net_failover_vlan_rx_add_vid
,
318 .ndo_vlan_rx_kill_vid
= net_failover_vlan_rx_kill_vid
,
319 .ndo_validate_addr
= eth_validate_addr
,
320 .ndo_features_check
= passthru_features_check
,
323 #define FAILOVER_NAME "net_failover"
324 #define FAILOVER_VERSION "0.1"
326 static void nfo_ethtool_get_drvinfo(struct net_device
*dev
,
327 struct ethtool_drvinfo
*drvinfo
)
329 strlcpy(drvinfo
->driver
, FAILOVER_NAME
, sizeof(drvinfo
->driver
));
330 strlcpy(drvinfo
->version
, FAILOVER_VERSION
, sizeof(drvinfo
->version
));
333 static int nfo_ethtool_get_link_ksettings(struct net_device
*dev
,
334 struct ethtool_link_ksettings
*cmd
)
336 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
337 struct net_device
*slave_dev
;
339 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
340 if (!slave_dev
|| !net_failover_xmit_ready(slave_dev
)) {
341 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
342 if (!slave_dev
|| !net_failover_xmit_ready(slave_dev
)) {
343 cmd
->base
.duplex
= DUPLEX_UNKNOWN
;
344 cmd
->base
.port
= PORT_OTHER
;
345 cmd
->base
.speed
= SPEED_UNKNOWN
;
351 return __ethtool_get_link_ksettings(slave_dev
, cmd
);
354 static const struct ethtool_ops failover_ethtool_ops
= {
355 .get_drvinfo
= nfo_ethtool_get_drvinfo
,
356 .get_link
= ethtool_op_get_link
,
357 .get_link_ksettings
= nfo_ethtool_get_link_ksettings
,
360 /* Called when slave dev is injecting data into network stack.
361 * Change the associated network device from lower dev to failover dev.
362 * note: already called with rcu_read_lock
364 static rx_handler_result_t
net_failover_handle_frame(struct sk_buff
**pskb
)
366 struct sk_buff
*skb
= *pskb
;
367 struct net_device
*dev
= rcu_dereference(skb
->dev
->rx_handler_data
);
368 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
369 struct net_device
*primary_dev
, *standby_dev
;
371 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
372 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
374 if (primary_dev
&& skb
->dev
== standby_dev
)
375 return RX_HANDLER_EXACT
;
379 return RX_HANDLER_ANOTHER
;
382 static void net_failover_compute_features(struct net_device
*dev
)
384 netdev_features_t vlan_features
= FAILOVER_VLAN_FEATURES
&
386 netdev_features_t enc_features
= FAILOVER_ENC_FEATURES
;
387 unsigned short max_hard_header_len
= ETH_HLEN
;
388 unsigned int dst_release_flag
= IFF_XMIT_DST_RELEASE
|
389 IFF_XMIT_DST_RELEASE_PERM
;
390 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
391 struct net_device
*primary_dev
, *standby_dev
;
393 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
396 netdev_increment_features(vlan_features
,
397 primary_dev
->vlan_features
,
398 FAILOVER_VLAN_FEATURES
);
400 netdev_increment_features(enc_features
,
401 primary_dev
->hw_enc_features
,
402 FAILOVER_ENC_FEATURES
);
404 dst_release_flag
&= primary_dev
->priv_flags
;
405 if (primary_dev
->hard_header_len
> max_hard_header_len
)
406 max_hard_header_len
= primary_dev
->hard_header_len
;
409 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
412 netdev_increment_features(vlan_features
,
413 standby_dev
->vlan_features
,
414 FAILOVER_VLAN_FEATURES
);
416 netdev_increment_features(enc_features
,
417 standby_dev
->hw_enc_features
,
418 FAILOVER_ENC_FEATURES
);
420 dst_release_flag
&= standby_dev
->priv_flags
;
421 if (standby_dev
->hard_header_len
> max_hard_header_len
)
422 max_hard_header_len
= standby_dev
->hard_header_len
;
425 dev
->vlan_features
= vlan_features
;
426 dev
->hw_enc_features
= enc_features
| NETIF_F_GSO_ENCAP_ALL
;
427 dev
->hard_header_len
= max_hard_header_len
;
429 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
430 if (dst_release_flag
== (IFF_XMIT_DST_RELEASE
|
431 IFF_XMIT_DST_RELEASE_PERM
))
432 dev
->priv_flags
|= IFF_XMIT_DST_RELEASE
;
434 netdev_change_features(dev
);
437 static void net_failover_lower_state_changed(struct net_device
*slave_dev
,
438 struct net_device
*primary_dev
,
439 struct net_device
*standby_dev
)
441 struct netdev_lag_lower_state_info info
;
443 if (netif_carrier_ok(slave_dev
))
446 info
.link_up
= false;
448 if (slave_dev
== primary_dev
) {
449 if (netif_running(primary_dev
))
450 info
.tx_enabled
= true;
452 info
.tx_enabled
= false;
454 if ((primary_dev
&& netif_running(primary_dev
)) ||
455 (!netif_running(standby_dev
)))
456 info
.tx_enabled
= false;
458 info
.tx_enabled
= true;
461 netdev_lower_state_changed(slave_dev
, &info
);
464 static int net_failover_slave_pre_register(struct net_device
*slave_dev
,
465 struct net_device
*failover_dev
)
467 struct net_device
*standby_dev
, *primary_dev
;
468 struct net_failover_info
*nfo_info
;
469 bool slave_is_standby
;
471 nfo_info
= netdev_priv(failover_dev
);
472 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
473 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
474 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
475 if (slave_is_standby
? standby_dev
: primary_dev
) {
476 netdev_err(failover_dev
, "%s attempting to register as slave dev when %s already present\n",
478 slave_is_standby
? "standby" : "primary");
482 /* We want to allow only a direct attached VF device as a primary
483 * netdev. As there is no easy way to check for a VF device, restrict
484 * this to a pci device.
486 if (!slave_is_standby
&& (!slave_dev
->dev
.parent
||
487 !dev_is_pci(slave_dev
->dev
.parent
)))
490 if (failover_dev
->features
& NETIF_F_VLAN_CHALLENGED
&&
491 vlan_uses_dev(failover_dev
)) {
492 netdev_err(failover_dev
, "Device %s is VLAN challenged and failover device has VLAN set up\n",
500 static int net_failover_slave_register(struct net_device
*slave_dev
,
501 struct net_device
*failover_dev
)
503 struct net_device
*standby_dev
, *primary_dev
;
504 struct net_failover_info
*nfo_info
;
505 bool slave_is_standby
;
509 /* Align MTU of slave with failover dev */
510 orig_mtu
= slave_dev
->mtu
;
511 err
= dev_set_mtu(slave_dev
, failover_dev
->mtu
);
513 netdev_err(failover_dev
, "unable to change mtu of %s to %u register failed\n",
514 slave_dev
->name
, failover_dev
->mtu
);
520 if (netif_running(failover_dev
)) {
521 err
= dev_open(slave_dev
);
522 if (err
&& (err
!= -EBUSY
)) {
523 netdev_err(failover_dev
, "Opening slave %s failed err:%d\n",
524 slave_dev
->name
, err
);
529 netif_addr_lock_bh(failover_dev
);
530 dev_uc_sync_multiple(slave_dev
, failover_dev
);
531 dev_mc_sync_multiple(slave_dev
, failover_dev
);
532 netif_addr_unlock_bh(failover_dev
);
534 err
= vlan_vids_add_by_dev(slave_dev
, failover_dev
);
536 netdev_err(failover_dev
, "Failed to add vlan ids to device %s err:%d\n",
537 slave_dev
->name
, err
);
541 nfo_info
= netdev_priv(failover_dev
);
542 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
543 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
544 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
546 if (slave_is_standby
) {
547 rcu_assign_pointer(nfo_info
->standby_dev
, slave_dev
);
548 standby_dev
= slave_dev
;
549 dev_get_stats(standby_dev
, &nfo_info
->standby_stats
);
551 rcu_assign_pointer(nfo_info
->primary_dev
, slave_dev
);
552 primary_dev
= slave_dev
;
553 dev_get_stats(primary_dev
, &nfo_info
->primary_stats
);
554 failover_dev
->min_mtu
= slave_dev
->min_mtu
;
555 failover_dev
->max_mtu
= slave_dev
->max_mtu
;
558 net_failover_lower_state_changed(slave_dev
, primary_dev
, standby_dev
);
559 net_failover_compute_features(failover_dev
);
561 call_netdevice_notifiers(NETDEV_JOIN
, slave_dev
);
563 netdev_info(failover_dev
, "failover %s slave:%s registered\n",
564 slave_is_standby
? "standby" : "primary", slave_dev
->name
);
569 dev_uc_unsync(slave_dev
, failover_dev
);
570 dev_mc_unsync(slave_dev
, failover_dev
);
571 dev_close(slave_dev
);
574 dev_set_mtu(slave_dev
, orig_mtu
);
579 static int net_failover_slave_pre_unregister(struct net_device
*slave_dev
,
580 struct net_device
*failover_dev
)
582 struct net_device
*standby_dev
, *primary_dev
;
583 struct net_failover_info
*nfo_info
;
585 nfo_info
= netdev_priv(failover_dev
);
586 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
587 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
589 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
595 static int net_failover_slave_unregister(struct net_device
*slave_dev
,
596 struct net_device
*failover_dev
)
598 struct net_device
*standby_dev
, *primary_dev
;
599 struct net_failover_info
*nfo_info
;
600 bool slave_is_standby
;
602 nfo_info
= netdev_priv(failover_dev
);
603 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
604 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
606 if (WARN_ON_ONCE(slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
))
609 vlan_vids_del_by_dev(slave_dev
, failover_dev
);
610 dev_uc_unsync(slave_dev
, failover_dev
);
611 dev_mc_unsync(slave_dev
, failover_dev
);
612 dev_close(slave_dev
);
614 nfo_info
= netdev_priv(failover_dev
);
615 dev_get_stats(failover_dev
, &nfo_info
->failover_stats
);
617 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
618 if (slave_is_standby
) {
619 RCU_INIT_POINTER(nfo_info
->standby_dev
, NULL
);
621 RCU_INIT_POINTER(nfo_info
->primary_dev
, NULL
);
623 failover_dev
->min_mtu
= standby_dev
->min_mtu
;
624 failover_dev
->max_mtu
= standby_dev
->max_mtu
;
630 net_failover_compute_features(failover_dev
);
632 netdev_info(failover_dev
, "failover %s slave:%s unregistered\n",
633 slave_is_standby
? "standby" : "primary", slave_dev
->name
);
638 static int net_failover_slave_link_change(struct net_device
*slave_dev
,
639 struct net_device
*failover_dev
)
641 struct net_device
*primary_dev
, *standby_dev
;
642 struct net_failover_info
*nfo_info
;
644 nfo_info
= netdev_priv(failover_dev
);
646 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
647 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
649 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
652 if ((primary_dev
&& net_failover_xmit_ready(primary_dev
)) ||
653 (standby_dev
&& net_failover_xmit_ready(standby_dev
))) {
654 netif_carrier_on(failover_dev
);
655 netif_tx_wake_all_queues(failover_dev
);
657 dev_get_stats(failover_dev
, &nfo_info
->failover_stats
);
658 netif_carrier_off(failover_dev
);
659 netif_tx_stop_all_queues(failover_dev
);
662 net_failover_lower_state_changed(slave_dev
, primary_dev
, standby_dev
);
667 static int net_failover_slave_name_change(struct net_device
*slave_dev
,
668 struct net_device
*failover_dev
)
670 struct net_device
*primary_dev
, *standby_dev
;
671 struct net_failover_info
*nfo_info
;
673 nfo_info
= netdev_priv(failover_dev
);
675 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
676 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
678 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
681 /* We need to bring up the slave after the rename by udev in case
682 * open failed with EBUSY when it was registered.
689 static struct failover_ops net_failover_ops
= {
690 .slave_pre_register
= net_failover_slave_pre_register
,
691 .slave_register
= net_failover_slave_register
,
692 .slave_pre_unregister
= net_failover_slave_pre_unregister
,
693 .slave_unregister
= net_failover_slave_unregister
,
694 .slave_link_change
= net_failover_slave_link_change
,
695 .slave_name_change
= net_failover_slave_name_change
,
696 .slave_handle_frame
= net_failover_handle_frame
,
700 * net_failover_create - Create and register a failover instance
702 * @dev: standby netdev
704 * Creates a failover netdev and registers a failover instance for a standby
705 * netdev. Used by paravirtual drivers that use 3-netdev model.
706 * The failover netdev acts as a master device and controls 2 slave devices -
707 * the original standby netdev and a VF netdev with the same MAC gets
708 * registered as primary netdev.
710 * Return: pointer to failover instance
712 struct failover
*net_failover_create(struct net_device
*standby_dev
)
714 struct device
*dev
= standby_dev
->dev
.parent
;
715 struct net_device
*failover_dev
;
716 struct failover
*failover
;
719 /* Alloc at least 2 queues, for now we are going with 16 assuming
720 * that VF devices being enslaved won't have too many queues.
722 failover_dev
= alloc_etherdev_mq(sizeof(struct net_failover_info
), 16);
724 dev_err(dev
, "Unable to allocate failover_netdev!\n");
725 return ERR_PTR(-ENOMEM
);
728 dev_net_set(failover_dev
, dev_net(standby_dev
));
729 SET_NETDEV_DEV(failover_dev
, dev
);
731 failover_dev
->netdev_ops
= &failover_dev_ops
;
732 failover_dev
->ethtool_ops
= &failover_ethtool_ops
;
734 /* Initialize the device options */
735 failover_dev
->priv_flags
|= IFF_UNICAST_FLT
| IFF_NO_QUEUE
;
736 failover_dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
|
739 /* don't acquire failover netdev's netif_tx_lock when transmitting */
740 failover_dev
->features
|= NETIF_F_LLTX
;
742 /* Don't allow failover devices to change network namespaces. */
743 failover_dev
->features
|= NETIF_F_NETNS_LOCAL
;
745 failover_dev
->hw_features
= FAILOVER_VLAN_FEATURES
|
746 NETIF_F_HW_VLAN_CTAG_TX
|
747 NETIF_F_HW_VLAN_CTAG_RX
|
748 NETIF_F_HW_VLAN_CTAG_FILTER
;
750 failover_dev
->hw_features
|= NETIF_F_GSO_ENCAP_ALL
;
751 failover_dev
->features
|= failover_dev
->hw_features
;
753 memcpy(failover_dev
->dev_addr
, standby_dev
->dev_addr
,
754 failover_dev
->addr_len
);
756 failover_dev
->min_mtu
= standby_dev
->min_mtu
;
757 failover_dev
->max_mtu
= standby_dev
->max_mtu
;
759 err
= register_netdev(failover_dev
);
761 dev_err(dev
, "Unable to register failover_dev!\n");
762 goto err_register_netdev
;
765 netif_carrier_off(failover_dev
);
767 failover
= failover_register(failover_dev
, &net_failover_ops
);
768 if (IS_ERR(failover
))
769 goto err_failover_register
;
773 err_failover_register
:
774 unregister_netdev(failover_dev
);
776 free_netdev(failover_dev
);
780 EXPORT_SYMBOL_GPL(net_failover_create
);
783 * net_failover_destroy - Destroy a failover instance
785 * @failover: pointer to failover instance
787 * Unregisters any slave netdevs associated with the failover instance by
788 * calling failover_slave_unregister().
789 * unregisters the failover instance itself and finally frees the failover
790 * netdev. Used by paravirtual drivers that use 3-netdev model.
793 void net_failover_destroy(struct failover
*failover
)
795 struct net_failover_info
*nfo_info
;
796 struct net_device
*failover_dev
;
797 struct net_device
*slave_dev
;
802 failover_dev
= rcu_dereference(failover
->failover_dev
);
803 nfo_info
= netdev_priv(failover_dev
);
805 netif_device_detach(failover_dev
);
809 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
811 failover_slave_unregister(slave_dev
);
813 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
815 failover_slave_unregister(slave_dev
);
817 failover_unregister(failover
);
819 unregister_netdevice(failover_dev
);
823 free_netdev(failover_dev
);
825 EXPORT_SYMBOL_GPL(net_failover_destroy
);
828 net_failover_init(void)
832 module_init(net_failover_init
);
835 void net_failover_exit(void)
838 module_exit(net_failover_exit
);
840 MODULE_DESCRIPTION("Failover driver for Paravirtual drivers");
841 MODULE_LICENSE("GPL v2");