1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Intel Corporation. */ 3 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. 12 * 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. 15 */ 16 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> 30 31 static bool net_failover_xmit_ready(struct net_device *dev) 32 { 33 return netif_running(dev) && netif_carrier_ok(dev); 34 } 35 36 static int net_failover_open(struct net_device *dev) 37 { 38 struct net_failover_info *nfo_info = netdev_priv(dev); 39 struct net_device *primary_dev, *standby_dev; 40 int err; 41 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; 47 } 48 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; 54 } 55 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); 60 } 61 62 return 0; 63 64 err_standby_open: 65 dev_close(primary_dev); 66 err_primary_open: 67 netif_tx_disable(dev); 68 return err; 69 } 70 71 static int net_failover_close(struct net_device *dev) 72 { 73 struct net_failover_info *nfo_info = netdev_priv(dev); 74 struct net_device *slave_dev; 75 76 netif_tx_disable(dev); 77 78 slave_dev = rtnl_dereference(nfo_info->primary_dev); 79 if (slave_dev) 80 dev_close(slave_dev); 81 82 slave_dev = rtnl_dereference(nfo_info->standby_dev); 83 if (slave_dev) 84 dev_close(slave_dev); 85 86 return 0; 87 } 88 89 static netdev_tx_t net_failover_drop_xmit(struct sk_buff *skb, 90 struct net_device *dev) 91 { 92 atomic_long_inc(&dev->tx_dropped); 93 dev_kfree_skb_any(skb); 94 return NETDEV_TX_OK; 95 } 96 97 static netdev_tx_t net_failover_start_xmit(struct sk_buff *skb, 98 struct net_device *dev) 99 { 100 struct net_failover_info *nfo_info = netdev_priv(dev); 101 struct net_device *xmit_dev; 102 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); 109 } 110 111 skb->dev = xmit_dev; 112 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; 113 114 return dev_queue_xmit(skb); 115 } 116 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) 121 { 122 struct net_failover_info *nfo_info = netdev_priv(dev); 123 struct net_device *primary_dev; 124 u16 txq; 125 126 primary_dev = rcu_dereference(nfo_info->primary_dev); 127 if (primary_dev) { 128 const struct net_device_ops *ops = primary_dev->netdev_ops; 129 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); 135 136 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 137 138 return txq; 139 } 140 141 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 142 143 /* Save the original txq to restore before passing to the driver */ 144 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 145 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); 150 } 151 152 return txq; 153 } 154 155 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but 156 * that some drivers can provide 32bit values only. 157 */ 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) 161 { 162 const u64 *new = (const u64 *)_new; 163 const u64 *old = (const u64 *)_old; 164 u64 *res = (u64 *)_res; 165 int i; 166 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; 171 172 /* detects if this particular field is 32bit only */ 173 if (((nv | ov) >> 32) == 0) 174 delta = (s64)(s32)((u32)nv - (u32)ov); 175 176 /* filter anomalies, some drivers reset their stats 177 * at down/up events. 178 */ 179 if (delta > 0) 180 res[i] += delta; 181 } 182 } 183 184 static void net_failover_get_stats(struct net_device *dev, 185 struct rtnl_link_stats64 *stats) 186 { 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; 191 192 spin_lock(&nfo_info->stats_lock); 193 memcpy(stats, &nfo_info->failover_stats, sizeof(*stats)); 194 195 rcu_read_lock(); 196 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)); 202 } 203 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)); 209 } 210 211 rcu_read_unlock(); 212 213 memcpy(&nfo_info->failover_stats, stats, sizeof(*stats)); 214 spin_unlock(&nfo_info->stats_lock); 215 } 216 217 static int net_failover_change_mtu(struct net_device *dev, int new_mtu) 218 { 219 struct net_failover_info *nfo_info = netdev_priv(dev); 220 struct net_device *primary_dev, *standby_dev; 221 int ret = 0; 222 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; 228 } 229 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; 237 } 238 } 239 240 dev->mtu = new_mtu; 241 242 return 0; 243 } 244 245 static void net_failover_set_rx_mode(struct net_device *dev) 246 { 247 struct net_failover_info *nfo_info = netdev_priv(dev); 248 struct net_device *slave_dev; 249 250 rcu_read_lock(); 251 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); 256 } 257 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); 262 } 263 264 rcu_read_unlock(); 265 } 266 267 static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 268 u16 vid) 269 { 270 struct net_failover_info *nfo_info = netdev_priv(dev); 271 struct net_device *primary_dev, *standby_dev; 272 int ret = 0; 273 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; 279 } 280 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); 287 } 288 289 return ret; 290 } 291 292 static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 293 u16 vid) 294 { 295 struct net_failover_info *nfo_info = netdev_priv(dev); 296 struct net_device *slave_dev; 297 298 slave_dev = rcu_dereference(nfo_info->primary_dev); 299 if (slave_dev) 300 vlan_vid_del(slave_dev, proto, vid); 301 302 slave_dev = rcu_dereference(nfo_info->standby_dev); 303 if (slave_dev) 304 vlan_vid_del(slave_dev, proto, vid); 305 306 return 0; 307 } 308 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, 321 }; 322 323 #define FAILOVER_NAME "net_failover" 324 #define FAILOVER_VERSION "0.1" 325 326 static void nfo_ethtool_get_drvinfo(struct net_device *dev, 327 struct ethtool_drvinfo *drvinfo) 328 { 329 strlcpy(drvinfo->driver, FAILOVER_NAME, sizeof(drvinfo->driver)); 330 strlcpy(drvinfo->version, FAILOVER_VERSION, sizeof(drvinfo->version)); 331 } 332 333 static int nfo_ethtool_get_link_ksettings(struct net_device *dev, 334 struct ethtool_link_ksettings *cmd) 335 { 336 struct net_failover_info *nfo_info = netdev_priv(dev); 337 struct net_device *slave_dev; 338 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; 346 347 return 0; 348 } 349 } 350 351 return __ethtool_get_link_ksettings(slave_dev, cmd); 352 } 353 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, 358 }; 359 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 363 */ 364 static rx_handler_result_t net_failover_handle_frame(struct sk_buff **pskb) 365 { 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; 370 371 primary_dev = rcu_dereference(nfo_info->primary_dev); 372 standby_dev = rcu_dereference(nfo_info->standby_dev); 373 374 if (primary_dev && skb->dev == standby_dev) 375 return RX_HANDLER_EXACT; 376 377 skb->dev = dev; 378 379 return RX_HANDLER_ANOTHER; 380 } 381 382 static void net_failover_compute_features(struct net_device *dev) 383 { 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; 392 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); 403 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; 407 } 408 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); 419 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; 423 } 424 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; 428 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; 433 434 netdev_change_features(dev); 435 } 436 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) 440 { 441 struct netdev_lag_lower_state_info info; 442 443 if (netif_carrier_ok(slave_dev)) 444 info.link_up = true; 445 else 446 info.link_up = false; 447 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; 459 } 460 461 netdev_lower_state_changed(slave_dev, &info); 462 } 463 464 static int net_failover_slave_pre_register(struct net_device *slave_dev, 465 struct net_device *failover_dev) 466 { 467 struct net_device *standby_dev, *primary_dev; 468 struct net_failover_info *nfo_info; 469 bool slave_is_standby; 470 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; 480 } 481 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. 485 */ 486 if (!slave_is_standby && (!slave_dev->dev.parent || 487 !dev_is_pci(slave_dev->dev.parent))) 488 return -EINVAL; 489 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; 495 } 496 497 return 0; 498 } 499 500 static int net_failover_slave_register(struct net_device *slave_dev, 501 struct net_device *failover_dev) 502 { 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; 508 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; 516 } 517 518 dev_hold(slave_dev); 519 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; 526 } 527 } 528 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); 533 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; 539 } 540 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; 545 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; 556 } 557 558 net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev); 559 net_failover_compute_features(failover_dev); 560 561 call_netdevice_notifiers(NETDEV_JOIN, slave_dev); 562 563 netdev_info(failover_dev, "failover %s slave:%s registered\n", 564 slave_is_standby ? "standby" : "primary", slave_dev->name); 565 566 return 0; 567 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; 577 } 578 579 static int net_failover_slave_pre_unregister(struct net_device *slave_dev, 580 struct net_device *failover_dev) 581 { 582 struct net_device *standby_dev, *primary_dev; 583 struct net_failover_info *nfo_info; 584 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); 588 589 if (slave_dev != primary_dev && slave_dev != standby_dev) 590 return -ENODEV; 591 592 return 0; 593 } 594 595 static int net_failover_slave_unregister(struct net_device *slave_dev, 596 struct net_device *failover_dev) 597 { 598 struct net_device *standby_dev, *primary_dev; 599 struct net_failover_info *nfo_info; 600 bool slave_is_standby; 601 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); 605 606 vlan_vids_del_by_dev(slave_dev, failover_dev); 607 dev_uc_unsync(slave_dev, failover_dev); 608 dev_mc_unsync(slave_dev, failover_dev); 609 dev_close(slave_dev); 610 611 nfo_info = netdev_priv(failover_dev); 612 dev_get_stats(failover_dev, &nfo_info->failover_stats); 613 614 slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent; 615 if (slave_is_standby) { 616 RCU_INIT_POINTER(nfo_info->standby_dev, NULL); 617 } else { 618 RCU_INIT_POINTER(nfo_info->primary_dev, NULL); 619 if (standby_dev) { 620 failover_dev->min_mtu = standby_dev->min_mtu; 621 failover_dev->max_mtu = standby_dev->max_mtu; 622 } 623 } 624 625 dev_put(slave_dev); 626 627 net_failover_compute_features(failover_dev); 628 629 netdev_info(failover_dev, "failover %s slave:%s unregistered\n", 630 slave_is_standby ? "standby" : "primary", slave_dev->name); 631 632 return 0; 633 } 634 635 static int net_failover_slave_link_change(struct net_device *slave_dev, 636 struct net_device *failover_dev) 637 { 638 struct net_device *primary_dev, *standby_dev; 639 struct net_failover_info *nfo_info; 640 641 nfo_info = netdev_priv(failover_dev); 642 643 primary_dev = rtnl_dereference(nfo_info->primary_dev); 644 standby_dev = rtnl_dereference(nfo_info->standby_dev); 645 646 if (slave_dev != primary_dev && slave_dev != standby_dev) 647 return -ENODEV; 648 649 if ((primary_dev && net_failover_xmit_ready(primary_dev)) || 650 (standby_dev && net_failover_xmit_ready(standby_dev))) { 651 netif_carrier_on(failover_dev); 652 netif_tx_wake_all_queues(failover_dev); 653 } else { 654 dev_get_stats(failover_dev, &nfo_info->failover_stats); 655 netif_carrier_off(failover_dev); 656 netif_tx_stop_all_queues(failover_dev); 657 } 658 659 net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev); 660 661 return 0; 662 } 663 664 static int net_failover_slave_name_change(struct net_device *slave_dev, 665 struct net_device *failover_dev) 666 { 667 struct net_device *primary_dev, *standby_dev; 668 struct net_failover_info *nfo_info; 669 670 nfo_info = netdev_priv(failover_dev); 671 672 primary_dev = rtnl_dereference(nfo_info->primary_dev); 673 standby_dev = rtnl_dereference(nfo_info->standby_dev); 674 675 if (slave_dev != primary_dev && slave_dev != standby_dev) 676 return -ENODEV; 677 678 /* We need to bring up the slave after the rename by udev in case 679 * open failed with EBUSY when it was registered. 680 */ 681 dev_open(slave_dev); 682 683 return 0; 684 } 685 686 static struct failover_ops net_failover_ops = { 687 .slave_pre_register = net_failover_slave_pre_register, 688 .slave_register = net_failover_slave_register, 689 .slave_pre_unregister = net_failover_slave_pre_unregister, 690 .slave_unregister = net_failover_slave_unregister, 691 .slave_link_change = net_failover_slave_link_change, 692 .slave_name_change = net_failover_slave_name_change, 693 .slave_handle_frame = net_failover_handle_frame, 694 }; 695 696 /** 697 * net_failover_create - Create and register a failover instance 698 * 699 * @dev: standby netdev 700 * 701 * Creates a failover netdev and registers a failover instance for a standby 702 * netdev. Used by paravirtual drivers that use 3-netdev model. 703 * The failover netdev acts as a master device and controls 2 slave devices - 704 * the original standby netdev and a VF netdev with the same MAC gets 705 * registered as primary netdev. 706 * 707 * Return: pointer to failover instance 708 */ 709 struct failover *net_failover_create(struct net_device *standby_dev) 710 { 711 struct device *dev = standby_dev->dev.parent; 712 struct net_device *failover_dev; 713 struct failover *failover; 714 int err; 715 716 /* Alloc at least 2 queues, for now we are going with 16 assuming 717 * that VF devices being enslaved won't have too many queues. 718 */ 719 failover_dev = alloc_etherdev_mq(sizeof(struct net_failover_info), 16); 720 if (!failover_dev) { 721 dev_err(dev, "Unable to allocate failover_netdev!\n"); 722 return ERR_PTR(-ENOMEM); 723 } 724 725 dev_net_set(failover_dev, dev_net(standby_dev)); 726 SET_NETDEV_DEV(failover_dev, dev); 727 728 failover_dev->netdev_ops = &failover_dev_ops; 729 failover_dev->ethtool_ops = &failover_ethtool_ops; 730 731 /* Initialize the device options */ 732 failover_dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE; 733 failover_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | 734 IFF_TX_SKB_SHARING); 735 736 /* don't acquire failover netdev's netif_tx_lock when transmitting */ 737 failover_dev->features |= NETIF_F_LLTX; 738 739 /* Don't allow failover devices to change network namespaces. */ 740 failover_dev->features |= NETIF_F_NETNS_LOCAL; 741 742 failover_dev->hw_features = FAILOVER_VLAN_FEATURES | 743 NETIF_F_HW_VLAN_CTAG_TX | 744 NETIF_F_HW_VLAN_CTAG_RX | 745 NETIF_F_HW_VLAN_CTAG_FILTER; 746 747 failover_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; 748 failover_dev->features |= failover_dev->hw_features; 749 750 memcpy(failover_dev->dev_addr, standby_dev->dev_addr, 751 failover_dev->addr_len); 752 753 failover_dev->min_mtu = standby_dev->min_mtu; 754 failover_dev->max_mtu = standby_dev->max_mtu; 755 756 err = register_netdev(failover_dev); 757 if (err) { 758 dev_err(dev, "Unable to register failover_dev!\n"); 759 goto err_register_netdev; 760 } 761 762 netif_carrier_off(failover_dev); 763 764 failover = failover_register(failover_dev, &net_failover_ops); 765 if (IS_ERR(failover)) 766 goto err_failover_register; 767 768 return failover; 769 770 err_failover_register: 771 unregister_netdev(failover_dev); 772 err_register_netdev: 773 free_netdev(failover_dev); 774 775 return ERR_PTR(err); 776 } 777 EXPORT_SYMBOL_GPL(net_failover_create); 778 779 /** 780 * net_failover_destroy - Destroy a failover instance 781 * 782 * @failover: pointer to failover instance 783 * 784 * Unregisters any slave netdevs associated with the failover instance by 785 * calling failover_slave_unregister(). 786 * unregisters the failover instance itself and finally frees the failover 787 * netdev. Used by paravirtual drivers that use 3-netdev model. 788 * 789 */ 790 void net_failover_destroy(struct failover *failover) 791 { 792 struct net_failover_info *nfo_info; 793 struct net_device *failover_dev; 794 struct net_device *slave_dev; 795 796 if (!failover) 797 return; 798 799 failover_dev = rcu_dereference(failover->failover_dev); 800 nfo_info = netdev_priv(failover_dev); 801 802 netif_device_detach(failover_dev); 803 804 rtnl_lock(); 805 806 slave_dev = rtnl_dereference(nfo_info->primary_dev); 807 if (slave_dev) 808 failover_slave_unregister(slave_dev); 809 810 slave_dev = rtnl_dereference(nfo_info->standby_dev); 811 if (slave_dev) 812 failover_slave_unregister(slave_dev); 813 814 failover_unregister(failover); 815 816 unregister_netdevice(failover_dev); 817 818 rtnl_unlock(); 819 820 free_netdev(failover_dev); 821 } 822 EXPORT_SYMBOL_GPL(net_failover_destroy); 823 824 static __init int 825 net_failover_init(void) 826 { 827 return 0; 828 } 829 module_init(net_failover_init); 830 831 static __exit 832 void net_failover_exit(void) 833 { 834 } 835 module_exit(net_failover_exit); 836 837 MODULE_DESCRIPTION("Failover driver for Paravirtual drivers"); 838 MODULE_LICENSE("GPL v2"); 839