failover: Add missing check to validate 'slave_dev' in net_failover_slave_unregister
[linux/fpc-iii.git] / drivers / net / net_failover.c
blob5a749dc25bec485359dd4b92d26e06404b7dc312
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 dev_close(primary_dev);
66 err_primary_open:
67 netif_tx_disable(dev);
68 return err;
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);
79 if (slave_dev)
80 dev_close(slave_dev);
82 slave_dev = rtnl_dereference(nfo_info->standby_dev);
83 if (slave_dev)
84 dev_close(slave_dev);
86 return 0;
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);
94 return NETDEV_TX_OK;
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);
111 skb->dev = xmit_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,
118 struct sk_buff *skb,
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;
124 u16 txq;
126 primary_dev = rcu_dereference(nfo_info->primary_dev);
127 if (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,
132 sb_dev, fallback);
133 else
134 txq = fallback(primary_dev, skb, NULL);
136 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
138 return txq;
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)) {
147 do {
148 txq -= dev->real_num_tx_queues;
149 } while (txq >= dev->real_num_tx_queues);
152 return txq;
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;
165 int i;
167 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
168 u64 nv = new[i];
169 u64 ov = old[i];
170 s64 delta = nv - ov;
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
177 * at down/up events.
179 if (delta > 0)
180 res[i] += delta;
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));
195 rcu_read_lock();
197 slave_dev = rcu_dereference(nfo_info->primary_dev);
198 if (slave_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);
205 if (slave_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));
211 rcu_read_unlock();
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;
221 int ret = 0;
223 primary_dev = rtnl_dereference(nfo_info->primary_dev);
224 if (primary_dev) {
225 ret = dev_set_mtu(primary_dev, new_mtu);
226 if (ret)
227 return ret;
230 standby_dev = rtnl_dereference(nfo_info->standby_dev);
231 if (standby_dev) {
232 ret = dev_set_mtu(standby_dev, new_mtu);
233 if (ret) {
234 if (primary_dev)
235 dev_set_mtu(primary_dev, dev->mtu);
236 return ret;
240 dev->mtu = new_mtu;
242 return 0;
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;
250 rcu_read_lock();
252 slave_dev = rcu_dereference(nfo_info->primary_dev);
253 if (slave_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);
259 if (slave_dev) {
260 dev_uc_sync_multiple(slave_dev, dev);
261 dev_mc_sync_multiple(slave_dev, dev);
264 rcu_read_unlock();
267 static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
268 u16 vid)
270 struct net_failover_info *nfo_info = netdev_priv(dev);
271 struct net_device *primary_dev, *standby_dev;
272 int ret = 0;
274 primary_dev = rcu_dereference(nfo_info->primary_dev);
275 if (primary_dev) {
276 ret = vlan_vid_add(primary_dev, proto, vid);
277 if (ret)
278 return ret;
281 standby_dev = rcu_dereference(nfo_info->standby_dev);
282 if (standby_dev) {
283 ret = vlan_vid_add(standby_dev, proto, vid);
284 if (ret)
285 if (primary_dev)
286 vlan_vid_del(primary_dev, proto, vid);
289 return ret;
292 static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
293 u16 vid)
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);
299 if (slave_dev)
300 vlan_vid_del(slave_dev, proto, vid);
302 slave_dev = rcu_dereference(nfo_info->standby_dev);
303 if (slave_dev)
304 vlan_vid_del(slave_dev, proto, vid);
306 return 0;
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;
347 return 0;
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;
377 skb->dev = dev;
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 &
385 NETIF_F_ALL_FOR_ALL;
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);
394 if (primary_dev) {
395 vlan_features =
396 netdev_increment_features(vlan_features,
397 primary_dev->vlan_features,
398 FAILOVER_VLAN_FEATURES);
399 enc_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);
410 if (standby_dev) {
411 vlan_features =
412 netdev_increment_features(vlan_features,
413 standby_dev->vlan_features,
414 FAILOVER_VLAN_FEATURES);
415 enc_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))
444 info.link_up = true;
445 else
446 info.link_up = false;
448 if (slave_dev == primary_dev) {
449 if (netif_running(primary_dev))
450 info.tx_enabled = true;
451 else
452 info.tx_enabled = false;
453 } else {
454 if ((primary_dev && netif_running(primary_dev)) ||
455 (!netif_running(standby_dev)))
456 info.tx_enabled = false;
457 else
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",
477 slave_dev->name,
478 slave_is_standby ? "standby" : "primary");
479 return -EINVAL;
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)))
488 return -EINVAL;
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",
493 failover_dev->name);
494 return -EINVAL;
497 return 0;
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;
506 u32 orig_mtu;
507 int err;
509 /* Align MTU of slave with failover dev */
510 orig_mtu = slave_dev->mtu;
511 err = dev_set_mtu(slave_dev, failover_dev->mtu);
512 if (err) {
513 netdev_err(failover_dev, "unable to change mtu of %s to %u register failed\n",
514 slave_dev->name, failover_dev->mtu);
515 goto done;
518 dev_hold(slave_dev);
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);
525 goto err_dev_open;
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);
535 if (err) {
536 netdev_err(failover_dev, "Failed to add vlan ids to device %s err:%d\n",
537 slave_dev->name, err);
538 goto err_vlan_add;
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);
550 } else {
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);
566 return 0;
568 err_vlan_add:
569 dev_uc_unsync(slave_dev, failover_dev);
570 dev_mc_unsync(slave_dev, failover_dev);
571 dev_close(slave_dev);
572 err_dev_open:
573 dev_put(slave_dev);
574 dev_set_mtu(slave_dev, orig_mtu);
575 done:
576 return err;
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)
590 return -ENODEV;
592 return 0;
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))
607 return -ENODEV;
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);
620 } else {
621 RCU_INIT_POINTER(nfo_info->primary_dev, NULL);
622 if (standby_dev) {
623 failover_dev->min_mtu = standby_dev->min_mtu;
624 failover_dev->max_mtu = standby_dev->max_mtu;
628 dev_put(slave_dev);
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);
635 return 0;
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)
650 return -ENODEV;
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);
656 } else {
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);
664 return 0;
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)
679 return -ENODEV;
681 /* We need to bring up the slave after the rename by udev in case
682 * open failed with EBUSY when it was registered.
684 dev_open(slave_dev);
686 return 0;
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;
717 int err;
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);
723 if (!failover_dev) {
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 |
737 IFF_TX_SKB_SHARING);
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);
760 if (err) {
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;
771 return failover;
773 err_failover_register:
774 unregister_netdev(failover_dev);
775 err_register_netdev:
776 free_netdev(failover_dev);
778 return ERR_PTR(err);
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;
799 if (!failover)
800 return;
802 failover_dev = rcu_dereference(failover->failover_dev);
803 nfo_info = netdev_priv(failover_dev);
805 netif_device_detach(failover_dev);
807 rtnl_lock();
809 slave_dev = rtnl_dereference(nfo_info->primary_dev);
810 if (slave_dev)
811 failover_slave_unregister(slave_dev);
813 slave_dev = rtnl_dereference(nfo_info->standby_dev);
814 if (slave_dev)
815 failover_slave_unregister(slave_dev);
817 failover_unregister(failover);
819 unregister_netdevice(failover_dev);
821 rtnl_unlock();
823 free_netdev(failover_dev);
825 EXPORT_SYMBOL_GPL(net_failover_destroy);
827 static __init int
828 net_failover_init(void)
830 return 0;
832 module_init(net_failover_init);
834 static __exit
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");