Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / net_failover.c
blob57273188b71e5642732a0a66268f76f124a48c3f
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;
40 int err;
42 primary_dev = rtnl_dereference(nfo_info->primary_dev);
43 if (primary_dev) {
44 err = dev_open(primary_dev);
45 if (err)
46 goto err_primary_open;
49 standby_dev = rtnl_dereference(nfo_info->standby_dev);
50 if (standby_dev) {
51 err = dev_open(standby_dev);
52 if (err)
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);
62 return 0;
64 err_standby_open:
65 if (primary_dev)
66 dev_close(primary_dev);
67 err_primary_open:
68 netif_tx_disable(dev);
69 return err;
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);
80 if (slave_dev)
81 dev_close(slave_dev);
83 slave_dev = rtnl_dereference(nfo_info->standby_dev);
84 if (slave_dev)
85 dev_close(slave_dev);
87 return 0;
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);
95 return NETDEV_TX_OK;
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);
112 skb->dev = xmit_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,
119 struct sk_buff *skb,
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;
125 u16 txq;
127 primary_dev = rcu_dereference(nfo_info->primary_dev);
128 if (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,
133 sb_dev, fallback);
134 else
135 txq = fallback(primary_dev, skb, NULL);
137 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
139 return txq;
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)) {
148 do {
149 txq -= dev->real_num_tx_queues;
150 } while (txq >= dev->real_num_tx_queues);
153 return txq;
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;
166 int i;
168 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
169 u64 nv = new[i];
170 u64 ov = old[i];
171 s64 delta = nv - ov;
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
178 * at down/up events.
180 if (delta > 0)
181 res[i] += delta;
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));
196 rcu_read_lock();
198 slave_dev = rcu_dereference(nfo_info->primary_dev);
199 if (slave_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);
206 if (slave_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));
212 rcu_read_unlock();
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;
222 int ret = 0;
224 primary_dev = rtnl_dereference(nfo_info->primary_dev);
225 if (primary_dev) {
226 ret = dev_set_mtu(primary_dev, new_mtu);
227 if (ret)
228 return ret;
231 standby_dev = rtnl_dereference(nfo_info->standby_dev);
232 if (standby_dev) {
233 ret = dev_set_mtu(standby_dev, new_mtu);
234 if (ret) {
235 if (primary_dev)
236 dev_set_mtu(primary_dev, dev->mtu);
237 return ret;
241 dev->mtu = new_mtu;
243 return 0;
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;
251 rcu_read_lock();
253 slave_dev = rcu_dereference(nfo_info->primary_dev);
254 if (slave_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);
260 if (slave_dev) {
261 dev_uc_sync_multiple(slave_dev, dev);
262 dev_mc_sync_multiple(slave_dev, dev);
265 rcu_read_unlock();
268 static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
269 u16 vid)
271 struct net_failover_info *nfo_info = netdev_priv(dev);
272 struct net_device *primary_dev, *standby_dev;
273 int ret = 0;
275 primary_dev = rcu_dereference(nfo_info->primary_dev);
276 if (primary_dev) {
277 ret = vlan_vid_add(primary_dev, proto, vid);
278 if (ret)
279 return ret;
282 standby_dev = rcu_dereference(nfo_info->standby_dev);
283 if (standby_dev) {
284 ret = vlan_vid_add(standby_dev, proto, vid);
285 if (ret)
286 if (primary_dev)
287 vlan_vid_del(primary_dev, proto, vid);
290 return ret;
293 static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
294 u16 vid)
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);
300 if (slave_dev)
301 vlan_vid_del(slave_dev, proto, vid);
303 slave_dev = rcu_dereference(nfo_info->standby_dev);
304 if (slave_dev)
305 vlan_vid_del(slave_dev, proto, vid);
307 return 0;
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;
348 return 0;
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;
378 skb->dev = dev;
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 &
386 NETIF_F_ALL_FOR_ALL;
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);
395 if (primary_dev) {
396 vlan_features =
397 netdev_increment_features(vlan_features,
398 primary_dev->vlan_features,
399 FAILOVER_VLAN_FEATURES);
400 enc_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);
411 if (standby_dev) {
412 vlan_features =
413 netdev_increment_features(vlan_features,
414 standby_dev->vlan_features,
415 FAILOVER_VLAN_FEATURES);
416 enc_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))
445 info.link_up = true;
446 else
447 info.link_up = false;
449 if (slave_dev == primary_dev) {
450 if (netif_running(primary_dev))
451 info.tx_enabled = true;
452 else
453 info.tx_enabled = false;
454 } else {
455 if ((primary_dev && netif_running(primary_dev)) ||
456 (!netif_running(standby_dev)))
457 info.tx_enabled = false;
458 else
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",
478 slave_dev->name,
479 slave_is_standby ? "standby" : "primary");
480 return -EINVAL;
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)))
489 return -EINVAL;
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",
494 failover_dev->name);
495 return -EINVAL;
498 return 0;
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;
507 u32 orig_mtu;
508 int err;
510 /* Align MTU of slave with failover dev */
511 orig_mtu = slave_dev->mtu;
512 err = dev_set_mtu(slave_dev, failover_dev->mtu);
513 if (err) {
514 netdev_err(failover_dev, "unable to change mtu of %s to %u register failed\n",
515 slave_dev->name, failover_dev->mtu);
516 goto done;
519 dev_hold(slave_dev);
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);
526 goto err_dev_open;
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);
536 if (err) {
537 netdev_err(failover_dev, "Failed to add vlan ids to device %s err:%d\n",
538 slave_dev->name, err);
539 goto err_vlan_add;
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);
551 } else {
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);
567 return 0;
569 err_vlan_add:
570 dev_uc_unsync(slave_dev, failover_dev);
571 dev_mc_unsync(slave_dev, failover_dev);
572 dev_close(slave_dev);
573 err_dev_open:
574 dev_put(slave_dev);
575 dev_set_mtu(slave_dev, orig_mtu);
576 done:
577 return err;
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)
591 return -ENODEV;
593 return 0;
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))
608 return -ENODEV;
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);
621 } else {
622 RCU_INIT_POINTER(nfo_info->primary_dev, NULL);
623 if (standby_dev) {
624 failover_dev->min_mtu = standby_dev->min_mtu;
625 failover_dev->max_mtu = standby_dev->max_mtu;
629 dev_put(slave_dev);
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);
636 return 0;
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)
651 return -ENODEV;
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);
657 } else {
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);
665 return 0;
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)
680 return -ENODEV;
682 /* We need to bring up the slave after the rename by udev in case
683 * open failed with EBUSY when it was registered.
685 dev_open(slave_dev);
687 return 0;
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;
718 int err;
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);
724 if (!failover_dev) {
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 |
738 IFF_TX_SKB_SHARING);
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);
761 if (err) {
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;
774 return failover;
776 err_failover_register:
777 unregister_netdev(failover_dev);
778 err_register_netdev:
779 free_netdev(failover_dev);
781 return ERR_PTR(err);
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;
802 if (!failover)
803 return;
805 failover_dev = rcu_dereference(failover->failover_dev);
806 nfo_info = netdev_priv(failover_dev);
808 netif_device_detach(failover_dev);
810 rtnl_lock();
812 slave_dev = rtnl_dereference(nfo_info->primary_dev);
813 if (slave_dev)
814 failover_slave_unregister(slave_dev);
816 slave_dev = rtnl_dereference(nfo_info->standby_dev);
817 if (slave_dev)
818 failover_slave_unregister(slave_dev);
820 failover_unregister(failover);
822 unregister_netdevice(failover_dev);
824 rtnl_unlock();
826 free_netdev(failover_dev);
828 EXPORT_SYMBOL_GPL(net_failover_destroy);
830 static __init int
831 net_failover_init(void)
833 return 0;
835 module_init(net_failover_init);
837 static __exit
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");