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
);
66 dev_close(primary_dev
);
68 netif_tx_disable(dev
);
72 static int net_failover_close(struct net_device
*dev
)
74 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
75 struct net_device
*slave_dev
;
77 netif_tx_disable(dev
);
79 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
83 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
90 static netdev_tx_t
net_failover_drop_xmit(struct sk_buff
*skb
,
91 struct net_device
*dev
)
93 atomic_long_inc(&dev
->tx_dropped
);
94 dev_kfree_skb_any(skb
);
98 static netdev_tx_t
net_failover_start_xmit(struct sk_buff
*skb
,
99 struct net_device
*dev
)
101 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
102 struct net_device
*xmit_dev
;
104 /* Try xmit via primary netdev followed by standby netdev */
105 xmit_dev
= rcu_dereference_bh(nfo_info
->primary_dev
);
106 if (!xmit_dev
|| !net_failover_xmit_ready(xmit_dev
)) {
107 xmit_dev
= rcu_dereference_bh(nfo_info
->standby_dev
);
108 if (!xmit_dev
|| !net_failover_xmit_ready(xmit_dev
))
109 return net_failover_drop_xmit(skb
, dev
);
113 skb
->queue_mapping
= qdisc_skb_cb(skb
)->slave_dev_queue_mapping
;
115 return dev_queue_xmit(skb
);
118 static u16
net_failover_select_queue(struct net_device
*dev
,
120 struct net_device
*sb_dev
,
121 select_queue_fallback_t fallback
)
123 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
124 struct net_device
*primary_dev
;
127 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
129 const struct net_device_ops
*ops
= primary_dev
->netdev_ops
;
131 if (ops
->ndo_select_queue
)
132 txq
= ops
->ndo_select_queue(primary_dev
, skb
,
135 txq
= fallback(primary_dev
, skb
, NULL
);
137 qdisc_skb_cb(skb
)->slave_dev_queue_mapping
= skb
->queue_mapping
;
142 txq
= skb_rx_queue_recorded(skb
) ? skb_get_rx_queue(skb
) : 0;
144 /* Save the original txq to restore before passing to the driver */
145 qdisc_skb_cb(skb
)->slave_dev_queue_mapping
= skb
->queue_mapping
;
147 if (unlikely(txq
>= dev
->real_num_tx_queues
)) {
149 txq
-= dev
->real_num_tx_queues
;
150 } while (txq
>= dev
->real_num_tx_queues
);
156 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
157 * that some drivers can provide 32bit values only.
159 static void net_failover_fold_stats(struct rtnl_link_stats64
*_res
,
160 const struct rtnl_link_stats64
*_new
,
161 const struct rtnl_link_stats64
*_old
)
163 const u64
*new = (const u64
*)_new
;
164 const u64
*old
= (const u64
*)_old
;
165 u64
*res
= (u64
*)_res
;
168 for (i
= 0; i
< sizeof(*_res
) / sizeof(u64
); i
++) {
173 /* detects if this particular field is 32bit only */
174 if (((nv
| ov
) >> 32) == 0)
175 delta
= (s64
)(s32
)((u32
)nv
- (u32
)ov
);
177 /* filter anomalies, some drivers reset their stats
185 static void net_failover_get_stats(struct net_device
*dev
,
186 struct rtnl_link_stats64
*stats
)
188 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
189 const struct rtnl_link_stats64
*new;
190 struct rtnl_link_stats64 temp
;
191 struct net_device
*slave_dev
;
193 spin_lock(&nfo_info
->stats_lock
);
194 memcpy(stats
, &nfo_info
->failover_stats
, sizeof(*stats
));
198 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
200 new = dev_get_stats(slave_dev
, &temp
);
201 net_failover_fold_stats(stats
, new, &nfo_info
->primary_stats
);
202 memcpy(&nfo_info
->primary_stats
, new, sizeof(*new));
205 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
207 new = dev_get_stats(slave_dev
, &temp
);
208 net_failover_fold_stats(stats
, new, &nfo_info
->standby_stats
);
209 memcpy(&nfo_info
->standby_stats
, new, sizeof(*new));
214 memcpy(&nfo_info
->failover_stats
, stats
, sizeof(*stats
));
215 spin_unlock(&nfo_info
->stats_lock
);
218 static int net_failover_change_mtu(struct net_device
*dev
, int new_mtu
)
220 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
221 struct net_device
*primary_dev
, *standby_dev
;
224 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
226 ret
= dev_set_mtu(primary_dev
, new_mtu
);
231 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
233 ret
= dev_set_mtu(standby_dev
, new_mtu
);
236 dev_set_mtu(primary_dev
, dev
->mtu
);
246 static void net_failover_set_rx_mode(struct net_device
*dev
)
248 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
249 struct net_device
*slave_dev
;
253 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
255 dev_uc_sync_multiple(slave_dev
, dev
);
256 dev_mc_sync_multiple(slave_dev
, dev
);
259 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
261 dev_uc_sync_multiple(slave_dev
, dev
);
262 dev_mc_sync_multiple(slave_dev
, dev
);
268 static int net_failover_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
,
271 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
272 struct net_device
*primary_dev
, *standby_dev
;
275 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
277 ret
= vlan_vid_add(primary_dev
, proto
, vid
);
282 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
284 ret
= vlan_vid_add(standby_dev
, proto
, vid
);
287 vlan_vid_del(primary_dev
, proto
, vid
);
293 static int net_failover_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
,
296 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
297 struct net_device
*slave_dev
;
299 slave_dev
= rcu_dereference(nfo_info
->primary_dev
);
301 vlan_vid_del(slave_dev
, proto
, vid
);
303 slave_dev
= rcu_dereference(nfo_info
->standby_dev
);
305 vlan_vid_del(slave_dev
, proto
, vid
);
310 static const struct net_device_ops failover_dev_ops
= {
311 .ndo_open
= net_failover_open
,
312 .ndo_stop
= net_failover_close
,
313 .ndo_start_xmit
= net_failover_start_xmit
,
314 .ndo_select_queue
= net_failover_select_queue
,
315 .ndo_get_stats64
= net_failover_get_stats
,
316 .ndo_change_mtu
= net_failover_change_mtu
,
317 .ndo_set_rx_mode
= net_failover_set_rx_mode
,
318 .ndo_vlan_rx_add_vid
= net_failover_vlan_rx_add_vid
,
319 .ndo_vlan_rx_kill_vid
= net_failover_vlan_rx_kill_vid
,
320 .ndo_validate_addr
= eth_validate_addr
,
321 .ndo_features_check
= passthru_features_check
,
324 #define FAILOVER_NAME "net_failover"
325 #define FAILOVER_VERSION "0.1"
327 static void nfo_ethtool_get_drvinfo(struct net_device
*dev
,
328 struct ethtool_drvinfo
*drvinfo
)
330 strlcpy(drvinfo
->driver
, FAILOVER_NAME
, sizeof(drvinfo
->driver
));
331 strlcpy(drvinfo
->version
, FAILOVER_VERSION
, sizeof(drvinfo
->version
));
334 static int nfo_ethtool_get_link_ksettings(struct net_device
*dev
,
335 struct ethtool_link_ksettings
*cmd
)
337 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
338 struct net_device
*slave_dev
;
340 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
341 if (!slave_dev
|| !net_failover_xmit_ready(slave_dev
)) {
342 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
343 if (!slave_dev
|| !net_failover_xmit_ready(slave_dev
)) {
344 cmd
->base
.duplex
= DUPLEX_UNKNOWN
;
345 cmd
->base
.port
= PORT_OTHER
;
346 cmd
->base
.speed
= SPEED_UNKNOWN
;
352 return __ethtool_get_link_ksettings(slave_dev
, cmd
);
355 static const struct ethtool_ops failover_ethtool_ops
= {
356 .get_drvinfo
= nfo_ethtool_get_drvinfo
,
357 .get_link
= ethtool_op_get_link
,
358 .get_link_ksettings
= nfo_ethtool_get_link_ksettings
,
361 /* Called when slave dev is injecting data into network stack.
362 * Change the associated network device from lower dev to failover dev.
363 * note: already called with rcu_read_lock
365 static rx_handler_result_t
net_failover_handle_frame(struct sk_buff
**pskb
)
367 struct sk_buff
*skb
= *pskb
;
368 struct net_device
*dev
= rcu_dereference(skb
->dev
->rx_handler_data
);
369 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
370 struct net_device
*primary_dev
, *standby_dev
;
372 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
373 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
375 if (primary_dev
&& skb
->dev
== standby_dev
)
376 return RX_HANDLER_EXACT
;
380 return RX_HANDLER_ANOTHER
;
383 static void net_failover_compute_features(struct net_device
*dev
)
385 netdev_features_t vlan_features
= FAILOVER_VLAN_FEATURES
&
387 netdev_features_t enc_features
= FAILOVER_ENC_FEATURES
;
388 unsigned short max_hard_header_len
= ETH_HLEN
;
389 unsigned int dst_release_flag
= IFF_XMIT_DST_RELEASE
|
390 IFF_XMIT_DST_RELEASE_PERM
;
391 struct net_failover_info
*nfo_info
= netdev_priv(dev
);
392 struct net_device
*primary_dev
, *standby_dev
;
394 primary_dev
= rcu_dereference(nfo_info
->primary_dev
);
397 netdev_increment_features(vlan_features
,
398 primary_dev
->vlan_features
,
399 FAILOVER_VLAN_FEATURES
);
401 netdev_increment_features(enc_features
,
402 primary_dev
->hw_enc_features
,
403 FAILOVER_ENC_FEATURES
);
405 dst_release_flag
&= primary_dev
->priv_flags
;
406 if (primary_dev
->hard_header_len
> max_hard_header_len
)
407 max_hard_header_len
= primary_dev
->hard_header_len
;
410 standby_dev
= rcu_dereference(nfo_info
->standby_dev
);
413 netdev_increment_features(vlan_features
,
414 standby_dev
->vlan_features
,
415 FAILOVER_VLAN_FEATURES
);
417 netdev_increment_features(enc_features
,
418 standby_dev
->hw_enc_features
,
419 FAILOVER_ENC_FEATURES
);
421 dst_release_flag
&= standby_dev
->priv_flags
;
422 if (standby_dev
->hard_header_len
> max_hard_header_len
)
423 max_hard_header_len
= standby_dev
->hard_header_len
;
426 dev
->vlan_features
= vlan_features
;
427 dev
->hw_enc_features
= enc_features
| NETIF_F_GSO_ENCAP_ALL
;
428 dev
->hard_header_len
= max_hard_header_len
;
430 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
431 if (dst_release_flag
== (IFF_XMIT_DST_RELEASE
|
432 IFF_XMIT_DST_RELEASE_PERM
))
433 dev
->priv_flags
|= IFF_XMIT_DST_RELEASE
;
435 netdev_change_features(dev
);
438 static void net_failover_lower_state_changed(struct net_device
*slave_dev
,
439 struct net_device
*primary_dev
,
440 struct net_device
*standby_dev
)
442 struct netdev_lag_lower_state_info info
;
444 if (netif_carrier_ok(slave_dev
))
447 info
.link_up
= false;
449 if (slave_dev
== primary_dev
) {
450 if (netif_running(primary_dev
))
451 info
.tx_enabled
= true;
453 info
.tx_enabled
= false;
455 if ((primary_dev
&& netif_running(primary_dev
)) ||
456 (!netif_running(standby_dev
)))
457 info
.tx_enabled
= false;
459 info
.tx_enabled
= true;
462 netdev_lower_state_changed(slave_dev
, &info
);
465 static int net_failover_slave_pre_register(struct net_device
*slave_dev
,
466 struct net_device
*failover_dev
)
468 struct net_device
*standby_dev
, *primary_dev
;
469 struct net_failover_info
*nfo_info
;
470 bool slave_is_standby
;
472 nfo_info
= netdev_priv(failover_dev
);
473 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
474 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
475 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
476 if (slave_is_standby
? standby_dev
: primary_dev
) {
477 netdev_err(failover_dev
, "%s attempting to register as slave dev when %s already present\n",
479 slave_is_standby
? "standby" : "primary");
483 /* We want to allow only a direct attached VF device as a primary
484 * netdev. As there is no easy way to check for a VF device, restrict
485 * this to a pci device.
487 if (!slave_is_standby
&& (!slave_dev
->dev
.parent
||
488 !dev_is_pci(slave_dev
->dev
.parent
)))
491 if (failover_dev
->features
& NETIF_F_VLAN_CHALLENGED
&&
492 vlan_uses_dev(failover_dev
)) {
493 netdev_err(failover_dev
, "Device %s is VLAN challenged and failover device has VLAN set up\n",
501 static int net_failover_slave_register(struct net_device
*slave_dev
,
502 struct net_device
*failover_dev
)
504 struct net_device
*standby_dev
, *primary_dev
;
505 struct net_failover_info
*nfo_info
;
506 bool slave_is_standby
;
510 /* Align MTU of slave with failover dev */
511 orig_mtu
= slave_dev
->mtu
;
512 err
= dev_set_mtu(slave_dev
, failover_dev
->mtu
);
514 netdev_err(failover_dev
, "unable to change mtu of %s to %u register failed\n",
515 slave_dev
->name
, failover_dev
->mtu
);
521 if (netif_running(failover_dev
)) {
522 err
= dev_open(slave_dev
);
523 if (err
&& (err
!= -EBUSY
)) {
524 netdev_err(failover_dev
, "Opening slave %s failed err:%d\n",
525 slave_dev
->name
, err
);
530 netif_addr_lock_bh(failover_dev
);
531 dev_uc_sync_multiple(slave_dev
, failover_dev
);
532 dev_mc_sync_multiple(slave_dev
, failover_dev
);
533 netif_addr_unlock_bh(failover_dev
);
535 err
= vlan_vids_add_by_dev(slave_dev
, failover_dev
);
537 netdev_err(failover_dev
, "Failed to add vlan ids to device %s err:%d\n",
538 slave_dev
->name
, err
);
542 nfo_info
= netdev_priv(failover_dev
);
543 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
544 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
545 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
547 if (slave_is_standby
) {
548 rcu_assign_pointer(nfo_info
->standby_dev
, slave_dev
);
549 standby_dev
= slave_dev
;
550 dev_get_stats(standby_dev
, &nfo_info
->standby_stats
);
552 rcu_assign_pointer(nfo_info
->primary_dev
, slave_dev
);
553 primary_dev
= slave_dev
;
554 dev_get_stats(primary_dev
, &nfo_info
->primary_stats
);
555 failover_dev
->min_mtu
= slave_dev
->min_mtu
;
556 failover_dev
->max_mtu
= slave_dev
->max_mtu
;
559 net_failover_lower_state_changed(slave_dev
, primary_dev
, standby_dev
);
560 net_failover_compute_features(failover_dev
);
562 call_netdevice_notifiers(NETDEV_JOIN
, slave_dev
);
564 netdev_info(failover_dev
, "failover %s slave:%s registered\n",
565 slave_is_standby
? "standby" : "primary", slave_dev
->name
);
570 dev_uc_unsync(slave_dev
, failover_dev
);
571 dev_mc_unsync(slave_dev
, failover_dev
);
572 dev_close(slave_dev
);
575 dev_set_mtu(slave_dev
, orig_mtu
);
580 static int net_failover_slave_pre_unregister(struct net_device
*slave_dev
,
581 struct net_device
*failover_dev
)
583 struct net_device
*standby_dev
, *primary_dev
;
584 struct net_failover_info
*nfo_info
;
586 nfo_info
= netdev_priv(failover_dev
);
587 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
588 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
590 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
596 static int net_failover_slave_unregister(struct net_device
*slave_dev
,
597 struct net_device
*failover_dev
)
599 struct net_device
*standby_dev
, *primary_dev
;
600 struct net_failover_info
*nfo_info
;
601 bool slave_is_standby
;
603 nfo_info
= netdev_priv(failover_dev
);
604 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
605 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
607 if (WARN_ON_ONCE(slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
))
610 vlan_vids_del_by_dev(slave_dev
, failover_dev
);
611 dev_uc_unsync(slave_dev
, failover_dev
);
612 dev_mc_unsync(slave_dev
, failover_dev
);
613 dev_close(slave_dev
);
615 nfo_info
= netdev_priv(failover_dev
);
616 dev_get_stats(failover_dev
, &nfo_info
->failover_stats
);
618 slave_is_standby
= slave_dev
->dev
.parent
== failover_dev
->dev
.parent
;
619 if (slave_is_standby
) {
620 RCU_INIT_POINTER(nfo_info
->standby_dev
, NULL
);
622 RCU_INIT_POINTER(nfo_info
->primary_dev
, NULL
);
624 failover_dev
->min_mtu
= standby_dev
->min_mtu
;
625 failover_dev
->max_mtu
= standby_dev
->max_mtu
;
631 net_failover_compute_features(failover_dev
);
633 netdev_info(failover_dev
, "failover %s slave:%s unregistered\n",
634 slave_is_standby
? "standby" : "primary", slave_dev
->name
);
639 static int net_failover_slave_link_change(struct net_device
*slave_dev
,
640 struct net_device
*failover_dev
)
642 struct net_device
*primary_dev
, *standby_dev
;
643 struct net_failover_info
*nfo_info
;
645 nfo_info
= netdev_priv(failover_dev
);
647 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
648 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
650 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
653 if ((primary_dev
&& net_failover_xmit_ready(primary_dev
)) ||
654 (standby_dev
&& net_failover_xmit_ready(standby_dev
))) {
655 netif_carrier_on(failover_dev
);
656 netif_tx_wake_all_queues(failover_dev
);
658 dev_get_stats(failover_dev
, &nfo_info
->failover_stats
);
659 netif_carrier_off(failover_dev
);
660 netif_tx_stop_all_queues(failover_dev
);
663 net_failover_lower_state_changed(slave_dev
, primary_dev
, standby_dev
);
668 static int net_failover_slave_name_change(struct net_device
*slave_dev
,
669 struct net_device
*failover_dev
)
671 struct net_device
*primary_dev
, *standby_dev
;
672 struct net_failover_info
*nfo_info
;
674 nfo_info
= netdev_priv(failover_dev
);
676 primary_dev
= rtnl_dereference(nfo_info
->primary_dev
);
677 standby_dev
= rtnl_dereference(nfo_info
->standby_dev
);
679 if (slave_dev
!= primary_dev
&& slave_dev
!= standby_dev
)
682 /* We need to bring up the slave after the rename by udev in case
683 * open failed with EBUSY when it was registered.
690 static struct failover_ops net_failover_ops
= {
691 .slave_pre_register
= net_failover_slave_pre_register
,
692 .slave_register
= net_failover_slave_register
,
693 .slave_pre_unregister
= net_failover_slave_pre_unregister
,
694 .slave_unregister
= net_failover_slave_unregister
,
695 .slave_link_change
= net_failover_slave_link_change
,
696 .slave_name_change
= net_failover_slave_name_change
,
697 .slave_handle_frame
= net_failover_handle_frame
,
701 * net_failover_create - Create and register a failover instance
703 * @dev: standby netdev
705 * Creates a failover netdev and registers a failover instance for a standby
706 * netdev. Used by paravirtual drivers that use 3-netdev model.
707 * The failover netdev acts as a master device and controls 2 slave devices -
708 * the original standby netdev and a VF netdev with the same MAC gets
709 * registered as primary netdev.
711 * Return: pointer to failover instance
713 struct failover
*net_failover_create(struct net_device
*standby_dev
)
715 struct device
*dev
= standby_dev
->dev
.parent
;
716 struct net_device
*failover_dev
;
717 struct failover
*failover
;
720 /* Alloc at least 2 queues, for now we are going with 16 assuming
721 * that VF devices being enslaved won't have too many queues.
723 failover_dev
= alloc_etherdev_mq(sizeof(struct net_failover_info
), 16);
725 dev_err(dev
, "Unable to allocate failover_netdev!\n");
726 return ERR_PTR(-ENOMEM
);
729 dev_net_set(failover_dev
, dev_net(standby_dev
));
730 SET_NETDEV_DEV(failover_dev
, dev
);
732 failover_dev
->netdev_ops
= &failover_dev_ops
;
733 failover_dev
->ethtool_ops
= &failover_ethtool_ops
;
735 /* Initialize the device options */
736 failover_dev
->priv_flags
|= IFF_UNICAST_FLT
| IFF_NO_QUEUE
;
737 failover_dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
|
740 /* don't acquire failover netdev's netif_tx_lock when transmitting */
741 failover_dev
->features
|= NETIF_F_LLTX
;
743 /* Don't allow failover devices to change network namespaces. */
744 failover_dev
->features
|= NETIF_F_NETNS_LOCAL
;
746 failover_dev
->hw_features
= FAILOVER_VLAN_FEATURES
|
747 NETIF_F_HW_VLAN_CTAG_TX
|
748 NETIF_F_HW_VLAN_CTAG_RX
|
749 NETIF_F_HW_VLAN_CTAG_FILTER
;
751 failover_dev
->hw_features
|= NETIF_F_GSO_ENCAP_ALL
;
752 failover_dev
->features
|= failover_dev
->hw_features
;
754 memcpy(failover_dev
->dev_addr
, standby_dev
->dev_addr
,
755 failover_dev
->addr_len
);
757 failover_dev
->min_mtu
= standby_dev
->min_mtu
;
758 failover_dev
->max_mtu
= standby_dev
->max_mtu
;
760 err
= register_netdev(failover_dev
);
762 dev_err(dev
, "Unable to register failover_dev!\n");
763 goto err_register_netdev
;
766 netif_carrier_off(failover_dev
);
768 failover
= failover_register(failover_dev
, &net_failover_ops
);
769 if (IS_ERR(failover
)) {
770 err
= PTR_ERR(failover
);
771 goto err_failover_register
;
776 err_failover_register
:
777 unregister_netdev(failover_dev
);
779 free_netdev(failover_dev
);
783 EXPORT_SYMBOL_GPL(net_failover_create
);
786 * net_failover_destroy - Destroy a failover instance
788 * @failover: pointer to failover instance
790 * Unregisters any slave netdevs associated with the failover instance by
791 * calling failover_slave_unregister().
792 * unregisters the failover instance itself and finally frees the failover
793 * netdev. Used by paravirtual drivers that use 3-netdev model.
796 void net_failover_destroy(struct failover
*failover
)
798 struct net_failover_info
*nfo_info
;
799 struct net_device
*failover_dev
;
800 struct net_device
*slave_dev
;
805 failover_dev
= rcu_dereference(failover
->failover_dev
);
806 nfo_info
= netdev_priv(failover_dev
);
808 netif_device_detach(failover_dev
);
812 slave_dev
= rtnl_dereference(nfo_info
->primary_dev
);
814 failover_slave_unregister(slave_dev
);
816 slave_dev
= rtnl_dereference(nfo_info
->standby_dev
);
818 failover_slave_unregister(slave_dev
);
820 failover_unregister(failover
);
822 unregister_netdevice(failover_dev
);
826 free_netdev(failover_dev
);
828 EXPORT_SYMBOL_GPL(net_failover_destroy
);
831 net_failover_init(void)
835 module_init(net_failover_init
);
838 void net_failover_exit(void)
841 module_exit(net_failover_exit
);
843 MODULE_DESCRIPTION("Failover driver for Paravirtual drivers");
844 MODULE_LICENSE("GPL v2");