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, void *accel_priv, 119 select_queue_fallback_t fallback) 120 { 121 struct net_failover_info *nfo_info = netdev_priv(dev); 122 struct net_device *primary_dev; 123 u16 txq; 124 125 primary_dev = rcu_dereference(nfo_info->primary_dev); 126 if (primary_dev) { 127 const struct net_device_ops *ops = primary_dev->netdev_ops; 128 129 if (ops->ndo_select_queue) 130 txq = ops->ndo_select_queue(primary_dev, skb, 131 accel_priv, fallback); 132 else 133 txq = fallback(primary_dev, skb); 134 135 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 136 137 return txq; 138 } 139 140 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 141 142 /* Save the original txq to restore before passing to the driver */ 143 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 144 145 if (unlikely(txq >= dev->real_num_tx_queues)) { 146 do { 147 txq -= dev->real_num_tx_queues; 148 } while (txq >= dev->real_num_tx_queues); 149 } 150 151 return txq; 152 } 153 154 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but 155 * that some drivers can provide 32bit values only. 156 */ 157 static void net_failover_fold_stats(struct rtnl_link_stats64 *_res, 158 const struct rtnl_link_stats64 *_new, 159 const struct rtnl_link_stats64 *_old) 160 { 161 const u64 *new = (const u64 *)_new; 162 const u64 *old = (const u64 *)_old; 163 u64 *res = (u64 *)_res; 164 int i; 165 166 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) { 167 u64 nv = new[i]; 168 u64 ov = old[i]; 169 s64 delta = nv - ov; 170 171 /* detects if this particular field is 32bit only */ 172 if (((nv | ov) >> 32) == 0) 173 delta = (s64)(s32)((u32)nv - (u32)ov); 174 175 /* filter anomalies, some drivers reset their stats 176 * at down/up events. 177 */ 178 if (delta > 0) 179 res[i] += delta; 180 } 181 } 182 183 static void net_failover_get_stats(struct net_device *dev, 184 struct rtnl_link_stats64 *stats) 185 { 186 struct net_failover_info *nfo_info = netdev_priv(dev); 187 const struct rtnl_link_stats64 *new; 188 struct rtnl_link_stats64 temp; 189 struct net_device *slave_dev; 190 191 spin_lock(&nfo_info->stats_lock); 192 memcpy(stats, &nfo_info->failover_stats, sizeof(*stats)); 193 194 rcu_read_lock(); 195 196 slave_dev = rcu_dereference(nfo_info->primary_dev); 197 if (slave_dev) { 198 new = dev_get_stats(slave_dev, &temp); 199 net_failover_fold_stats(stats, new, &nfo_info->primary_stats); 200 memcpy(&nfo_info->primary_stats, new, sizeof(*new)); 201 } 202 203 slave_dev = rcu_dereference(nfo_info->standby_dev); 204 if (slave_dev) { 205 new = dev_get_stats(slave_dev, &temp); 206 net_failover_fold_stats(stats, new, &nfo_info->standby_stats); 207 memcpy(&nfo_info->standby_stats, new, sizeof(*new)); 208 } 209 210 rcu_read_unlock(); 211 212 memcpy(&nfo_info->failover_stats, stats, sizeof(*stats)); 213 spin_unlock(&nfo_info->stats_lock); 214 } 215 216 static int net_failover_change_mtu(struct net_device *dev, int new_mtu) 217 { 218 struct net_failover_info *nfo_info = netdev_priv(dev); 219 struct net_device *primary_dev, *standby_dev; 220 int ret = 0; 221 222 primary_dev = rcu_dereference(nfo_info->primary_dev); 223 if (primary_dev) { 224 ret = dev_set_mtu(primary_dev, new_mtu); 225 if (ret) 226 return ret; 227 } 228 229 standby_dev = rcu_dereference(nfo_info->standby_dev); 230 if (standby_dev) { 231 ret = dev_set_mtu(standby_dev, new_mtu); 232 if (ret) { 233 if (primary_dev) 234 dev_set_mtu(primary_dev, dev->mtu); 235 return ret; 236 } 237 } 238 239 dev->mtu = new_mtu; 240 241 return 0; 242 } 243 244 static void net_failover_set_rx_mode(struct net_device *dev) 245 { 246 struct net_failover_info *nfo_info = netdev_priv(dev); 247 struct net_device *slave_dev; 248 249 rcu_read_lock(); 250 251 slave_dev = rcu_dereference(nfo_info->primary_dev); 252 if (slave_dev) { 253 dev_uc_sync_multiple(slave_dev, dev); 254 dev_mc_sync_multiple(slave_dev, dev); 255 } 256 257 slave_dev = rcu_dereference(nfo_info->standby_dev); 258 if (slave_dev) { 259 dev_uc_sync_multiple(slave_dev, dev); 260 dev_mc_sync_multiple(slave_dev, dev); 261 } 262 263 rcu_read_unlock(); 264 } 265 266 static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 267 u16 vid) 268 { 269 struct net_failover_info *nfo_info = netdev_priv(dev); 270 struct net_device *primary_dev, *standby_dev; 271 int ret = 0; 272 273 primary_dev = rcu_dereference(nfo_info->primary_dev); 274 if (primary_dev) { 275 ret = vlan_vid_add(primary_dev, proto, vid); 276 if (ret) 277 return ret; 278 } 279 280 standby_dev = rcu_dereference(nfo_info->standby_dev); 281 if (standby_dev) { 282 ret = vlan_vid_add(standby_dev, proto, vid); 283 if (ret) 284 if (primary_dev) 285 vlan_vid_del(primary_dev, proto, vid); 286 } 287 288 return ret; 289 } 290 291 static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 292 u16 vid) 293 { 294 struct net_failover_info *nfo_info = netdev_priv(dev); 295 struct net_device *slave_dev; 296 297 slave_dev = rcu_dereference(nfo_info->primary_dev); 298 if (slave_dev) 299 vlan_vid_del(slave_dev, proto, vid); 300 301 slave_dev = rcu_dereference(nfo_info->standby_dev); 302 if (slave_dev) 303 vlan_vid_del(slave_dev, proto, vid); 304 305 return 0; 306 } 307 308 static const struct net_device_ops failover_dev_ops = { 309 .ndo_open = net_failover_open, 310 .ndo_stop = net_failover_close, 311 .ndo_start_xmit = net_failover_start_xmit, 312 .ndo_select_queue = net_failover_select_queue, 313 .ndo_get_stats64 = net_failover_get_stats, 314 .ndo_change_mtu = net_failover_change_mtu, 315 .ndo_set_rx_mode = net_failover_set_rx_mode, 316 .ndo_vlan_rx_add_vid = net_failover_vlan_rx_add_vid, 317 .ndo_vlan_rx_kill_vid = net_failover_vlan_rx_kill_vid, 318 .ndo_validate_addr = eth_validate_addr, 319 .ndo_features_check = passthru_features_check, 320 }; 321 322 #define FAILOVER_NAME "net_failover" 323 #define FAILOVER_VERSION "0.1" 324 325 static void nfo_ethtool_get_drvinfo(struct net_device *dev, 326 struct ethtool_drvinfo *drvinfo) 327 { 328 strlcpy(drvinfo->driver, FAILOVER_NAME, sizeof(drvinfo->driver)); 329 strlcpy(drvinfo->version, FAILOVER_VERSION, sizeof(drvinfo->version)); 330 } 331 332 static int nfo_ethtool_get_link_ksettings(struct net_device *dev, 333 struct ethtool_link_ksettings *cmd) 334 { 335 struct net_failover_info *nfo_info = netdev_priv(dev); 336 struct net_device *slave_dev; 337 338 slave_dev = rtnl_dereference(nfo_info->primary_dev); 339 if (!slave_dev || !net_failover_xmit_ready(slave_dev)) { 340 slave_dev = rtnl_dereference(nfo_info->standby_dev); 341 if (!slave_dev || !net_failover_xmit_ready(slave_dev)) { 342 cmd->base.duplex = DUPLEX_UNKNOWN; 343 cmd->base.port = PORT_OTHER; 344 cmd->base.speed = SPEED_UNKNOWN; 345 346 return 0; 347 } 348 } 349 350 return __ethtool_get_link_ksettings(slave_dev, cmd); 351 } 352 353 static const struct ethtool_ops failover_ethtool_ops = { 354 .get_drvinfo = nfo_ethtool_get_drvinfo, 355 .get_link = ethtool_op_get_link, 356 .get_link_ksettings = nfo_ethtool_get_link_ksettings, 357 }; 358 359 /* Called when slave dev is injecting data into network stack. 360 * Change the associated network device from lower dev to failover dev. 361 * note: already called with rcu_read_lock 362 */ 363 static rx_handler_result_t net_failover_handle_frame(struct sk_buff **pskb) 364 { 365 struct sk_buff *skb = *pskb; 366 struct net_device *dev = rcu_dereference(skb->dev->rx_handler_data); 367 struct net_failover_info *nfo_info = netdev_priv(dev); 368 struct net_device *primary_dev, *standby_dev; 369 370 primary_dev = rcu_dereference(nfo_info->primary_dev); 371 standby_dev = rcu_dereference(nfo_info->standby_dev); 372 373 if (primary_dev && skb->dev == standby_dev) 374 return RX_HANDLER_EXACT; 375 376 skb->dev = dev; 377 378 return RX_HANDLER_ANOTHER; 379 } 380 381 static void net_failover_compute_features(struct net_device *dev) 382 { 383 u32 vlan_features = FAILOVER_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL; 384 netdev_features_t enc_features = FAILOVER_ENC_FEATURES; 385 unsigned short max_hard_header_len = ETH_HLEN; 386 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE | 387 IFF_XMIT_DST_RELEASE_PERM; 388 struct net_failover_info *nfo_info = netdev_priv(dev); 389 struct net_device *primary_dev, *standby_dev; 390 391 primary_dev = rcu_dereference(nfo_info->primary_dev); 392 if (primary_dev) { 393 vlan_features = 394 netdev_increment_features(vlan_features, 395 primary_dev->vlan_features, 396 FAILOVER_VLAN_FEATURES); 397 enc_features = 398 netdev_increment_features(enc_features, 399 primary_dev->hw_enc_features, 400 FAILOVER_ENC_FEATURES); 401 402 dst_release_flag &= primary_dev->priv_flags; 403 if (primary_dev->hard_header_len > max_hard_header_len) 404 max_hard_header_len = primary_dev->hard_header_len; 405 } 406 407 standby_dev = rcu_dereference(nfo_info->standby_dev); 408 if (standby_dev) { 409 vlan_features = 410 netdev_increment_features(vlan_features, 411 standby_dev->vlan_features, 412 FAILOVER_VLAN_FEATURES); 413 enc_features = 414 netdev_increment_features(enc_features, 415 standby_dev->hw_enc_features, 416 FAILOVER_ENC_FEATURES); 417 418 dst_release_flag &= standby_dev->priv_flags; 419 if (standby_dev->hard_header_len > max_hard_header_len) 420 max_hard_header_len = standby_dev->hard_header_len; 421 } 422 423 dev->vlan_features = vlan_features; 424 dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL; 425 dev->hard_header_len = max_hard_header_len; 426 427 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 428 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | 429 IFF_XMIT_DST_RELEASE_PERM)) 430 dev->priv_flags |= IFF_XMIT_DST_RELEASE; 431 432 netdev_change_features(dev); 433 } 434 435 static void net_failover_lower_state_changed(struct net_device *slave_dev, 436 struct net_device *primary_dev, 437 struct net_device *standby_dev) 438 { 439 struct netdev_lag_lower_state_info info; 440 441 if (netif_carrier_ok(slave_dev)) 442 info.link_up = true; 443 else 444 info.link_up = false; 445 446 if (slave_dev == primary_dev) { 447 if (netif_running(primary_dev)) 448 info.tx_enabled = true; 449 else 450 info.tx_enabled = false; 451 } else { 452 if ((primary_dev && netif_running(primary_dev)) || 453 (!netif_running(standby_dev))) 454 info.tx_enabled = false; 455 else 456 info.tx_enabled = true; 457 } 458 459 netdev_lower_state_changed(slave_dev, &info); 460 } 461 462 static int net_failover_slave_pre_register(struct net_device *slave_dev, 463 struct net_device *failover_dev) 464 { 465 struct net_device *standby_dev, *primary_dev; 466 struct net_failover_info *nfo_info; 467 bool slave_is_standby; 468 469 nfo_info = netdev_priv(failover_dev); 470 standby_dev = rtnl_dereference(nfo_info->standby_dev); 471 primary_dev = rtnl_dereference(nfo_info->primary_dev); 472 slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent; 473 if (slave_is_standby ? standby_dev : primary_dev) { 474 netdev_err(failover_dev, "%s attempting to register as slave dev when %s already present\n", 475 slave_dev->name, 476 slave_is_standby ? "standby" : "primary"); 477 return -EINVAL; 478 } 479 480 /* We want to allow only a direct attached VF device as a primary 481 * netdev. As there is no easy way to check for a VF device, restrict 482 * this to a pci device. 483 */ 484 if (!slave_is_standby && (!slave_dev->dev.parent || 485 !dev_is_pci(slave_dev->dev.parent))) 486 return -EINVAL; 487 488 if (failover_dev->features & NETIF_F_VLAN_CHALLENGED && 489 vlan_uses_dev(failover_dev)) { 490 netdev_err(failover_dev, "Device %s is VLAN challenged and failover device has VLAN set up\n", 491 failover_dev->name); 492 return -EINVAL; 493 } 494 495 return 0; 496 } 497 498 static int net_failover_slave_register(struct net_device *slave_dev, 499 struct net_device *failover_dev) 500 { 501 struct net_device *standby_dev, *primary_dev; 502 struct net_failover_info *nfo_info; 503 bool slave_is_standby; 504 u32 orig_mtu; 505 int err; 506 507 /* Align MTU of slave with failover dev */ 508 orig_mtu = slave_dev->mtu; 509 err = dev_set_mtu(slave_dev, failover_dev->mtu); 510 if (err) { 511 netdev_err(failover_dev, "unable to change mtu of %s to %u register failed\n", 512 slave_dev->name, failover_dev->mtu); 513 goto done; 514 } 515 516 dev_hold(slave_dev); 517 518 if (netif_running(failover_dev)) { 519 err = dev_open(slave_dev); 520 if (err && (err != -EBUSY)) { 521 netdev_err(failover_dev, "Opening slave %s failed err:%d\n", 522 slave_dev->name, err); 523 goto err_dev_open; 524 } 525 } 526 527 netif_addr_lock_bh(failover_dev); 528 dev_uc_sync_multiple(slave_dev, failover_dev); 529 dev_uc_sync_multiple(slave_dev, failover_dev); 530 netif_addr_unlock_bh(failover_dev); 531 532 err = vlan_vids_add_by_dev(slave_dev, failover_dev); 533 if (err) { 534 netdev_err(failover_dev, "Failed to add vlan ids to device %s err:%d\n", 535 slave_dev->name, err); 536 goto err_vlan_add; 537 } 538 539 nfo_info = netdev_priv(failover_dev); 540 standby_dev = rtnl_dereference(nfo_info->standby_dev); 541 primary_dev = rtnl_dereference(nfo_info->primary_dev); 542 slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent; 543 544 if (slave_is_standby) { 545 rcu_assign_pointer(nfo_info->standby_dev, slave_dev); 546 standby_dev = slave_dev; 547 dev_get_stats(standby_dev, &nfo_info->standby_stats); 548 } else { 549 rcu_assign_pointer(nfo_info->primary_dev, slave_dev); 550 primary_dev = slave_dev; 551 dev_get_stats(primary_dev, &nfo_info->primary_stats); 552 failover_dev->min_mtu = slave_dev->min_mtu; 553 failover_dev->max_mtu = slave_dev->max_mtu; 554 } 555 556 net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev); 557 net_failover_compute_features(failover_dev); 558 559 call_netdevice_notifiers(NETDEV_JOIN, slave_dev); 560 561 netdev_info(failover_dev, "failover %s slave:%s registered\n", 562 slave_is_standby ? "standby" : "primary", slave_dev->name); 563 564 return 0; 565 566 err_vlan_add: 567 dev_uc_unsync(slave_dev, failover_dev); 568 dev_mc_unsync(slave_dev, failover_dev); 569 dev_close(slave_dev); 570 err_dev_open: 571 dev_put(slave_dev); 572 dev_set_mtu(slave_dev, orig_mtu); 573 done: 574 return err; 575 } 576 577 static int net_failover_slave_pre_unregister(struct net_device *slave_dev, 578 struct net_device *failover_dev) 579 { 580 struct net_device *standby_dev, *primary_dev; 581 struct net_failover_info *nfo_info; 582 583 nfo_info = netdev_priv(failover_dev); 584 primary_dev = rtnl_dereference(nfo_info->primary_dev); 585 standby_dev = rtnl_dereference(nfo_info->standby_dev); 586 587 if (slave_dev != primary_dev && slave_dev != standby_dev) 588 return -ENODEV; 589 590 return 0; 591 } 592 593 static int net_failover_slave_unregister(struct net_device *slave_dev, 594 struct net_device *failover_dev) 595 { 596 struct net_device *standby_dev, *primary_dev; 597 struct net_failover_info *nfo_info; 598 bool slave_is_standby; 599 600 nfo_info = netdev_priv(failover_dev); 601 primary_dev = rtnl_dereference(nfo_info->primary_dev); 602 standby_dev = rtnl_dereference(nfo_info->standby_dev); 603 604 vlan_vids_del_by_dev(slave_dev, failover_dev); 605 dev_uc_unsync(slave_dev, failover_dev); 606 dev_mc_unsync(slave_dev, failover_dev); 607 dev_close(slave_dev); 608 609 nfo_info = netdev_priv(failover_dev); 610 dev_get_stats(failover_dev, &nfo_info->failover_stats); 611 612 slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent; 613 if (slave_is_standby) { 614 RCU_INIT_POINTER(nfo_info->standby_dev, NULL); 615 } else { 616 RCU_INIT_POINTER(nfo_info->primary_dev, NULL); 617 if (standby_dev) { 618 failover_dev->min_mtu = standby_dev->min_mtu; 619 failover_dev->max_mtu = standby_dev->max_mtu; 620 } 621 } 622 623 dev_put(slave_dev); 624 625 net_failover_compute_features(failover_dev); 626 627 netdev_info(failover_dev, "failover %s slave:%s unregistered\n", 628 slave_is_standby ? "standby" : "primary", slave_dev->name); 629 630 return 0; 631 } 632 633 static int net_failover_slave_link_change(struct net_device *slave_dev, 634 struct net_device *failover_dev) 635 { 636 struct net_device *primary_dev, *standby_dev; 637 struct net_failover_info *nfo_info; 638 639 nfo_info = netdev_priv(failover_dev); 640 641 primary_dev = rtnl_dereference(nfo_info->primary_dev); 642 standby_dev = rtnl_dereference(nfo_info->standby_dev); 643 644 if (slave_dev != primary_dev && slave_dev != standby_dev) 645 return -ENODEV; 646 647 if ((primary_dev && net_failover_xmit_ready(primary_dev)) || 648 (standby_dev && net_failover_xmit_ready(standby_dev))) { 649 netif_carrier_on(failover_dev); 650 netif_tx_wake_all_queues(failover_dev); 651 } else { 652 dev_get_stats(failover_dev, &nfo_info->failover_stats); 653 netif_carrier_off(failover_dev); 654 netif_tx_stop_all_queues(failover_dev); 655 } 656 657 net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev); 658 659 return 0; 660 } 661 662 static int net_failover_slave_name_change(struct net_device *slave_dev, 663 struct net_device *failover_dev) 664 { 665 struct net_device *primary_dev, *standby_dev; 666 struct net_failover_info *nfo_info; 667 668 nfo_info = netdev_priv(failover_dev); 669 670 primary_dev = rtnl_dereference(nfo_info->primary_dev); 671 standby_dev = rtnl_dereference(nfo_info->standby_dev); 672 673 if (slave_dev != primary_dev && slave_dev != standby_dev) 674 return -ENODEV; 675 676 /* We need to bring up the slave after the rename by udev in case 677 * open failed with EBUSY when it was registered. 678 */ 679 dev_open(slave_dev); 680 681 return 0; 682 } 683 684 static struct failover_ops net_failover_ops = { 685 .slave_pre_register = net_failover_slave_pre_register, 686 .slave_register = net_failover_slave_register, 687 .slave_pre_unregister = net_failover_slave_pre_unregister, 688 .slave_unregister = net_failover_slave_unregister, 689 .slave_link_change = net_failover_slave_link_change, 690 .slave_name_change = net_failover_slave_name_change, 691 .slave_handle_frame = net_failover_handle_frame, 692 }; 693 694 /** 695 * net_failover_create - Create and register a failover instance 696 * 697 * @dev: standby netdev 698 * 699 * Creates a failover netdev and registers a failover instance for a standby 700 * netdev. Used by paravirtual drivers that use 3-netdev model. 701 * The failover netdev acts as a master device and controls 2 slave devices - 702 * the original standby netdev and a VF netdev with the same MAC gets 703 * registered as primary netdev. 704 * 705 * Return: pointer to failover instance 706 */ 707 struct failover *net_failover_create(struct net_device *standby_dev) 708 { 709 struct device *dev = standby_dev->dev.parent; 710 struct net_device *failover_dev; 711 struct failover *failover; 712 int err; 713 714 /* Alloc at least 2 queues, for now we are going with 16 assuming 715 * that VF devices being enslaved won't have too many queues. 716 */ 717 failover_dev = alloc_etherdev_mq(sizeof(struct net_failover_info), 16); 718 if (!failover_dev) { 719 dev_err(dev, "Unable to allocate failover_netdev!\n"); 720 return ERR_PTR(-ENOMEM); 721 } 722 723 dev_net_set(failover_dev, dev_net(standby_dev)); 724 SET_NETDEV_DEV(failover_dev, dev); 725 726 failover_dev->netdev_ops = &failover_dev_ops; 727 failover_dev->ethtool_ops = &failover_ethtool_ops; 728 729 /* Initialize the device options */ 730 failover_dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE; 731 failover_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | 732 IFF_TX_SKB_SHARING); 733 734 /* don't acquire failover netdev's netif_tx_lock when transmitting */ 735 failover_dev->features |= NETIF_F_LLTX; 736 737 /* Don't allow failover devices to change network namespaces. */ 738 failover_dev->features |= NETIF_F_NETNS_LOCAL; 739 740 failover_dev->hw_features = FAILOVER_VLAN_FEATURES | 741 NETIF_F_HW_VLAN_CTAG_TX | 742 NETIF_F_HW_VLAN_CTAG_RX | 743 NETIF_F_HW_VLAN_CTAG_FILTER; 744 745 failover_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; 746 failover_dev->features |= failover_dev->hw_features; 747 748 memcpy(failover_dev->dev_addr, standby_dev->dev_addr, 749 failover_dev->addr_len); 750 751 failover_dev->min_mtu = standby_dev->min_mtu; 752 failover_dev->max_mtu = standby_dev->max_mtu; 753 754 err = register_netdev(failover_dev); 755 if (err) { 756 dev_err(dev, "Unable to register failover_dev!\n"); 757 goto err_register_netdev; 758 } 759 760 netif_carrier_off(failover_dev); 761 762 failover = failover_register(failover_dev, &net_failover_ops); 763 if (IS_ERR(failover)) 764 goto err_failover_register; 765 766 return failover; 767 768 err_failover_register: 769 unregister_netdev(failover_dev); 770 err_register_netdev: 771 free_netdev(failover_dev); 772 773 return ERR_PTR(err); 774 } 775 EXPORT_SYMBOL_GPL(net_failover_create); 776 777 /** 778 * net_failover_destroy - Destroy a failover instance 779 * 780 * @failover: pointer to failover instance 781 * 782 * Unregisters any slave netdevs associated with the failover instance by 783 * calling failover_slave_unregister(). 784 * unregisters the failover instance itself and finally frees the failover 785 * netdev. Used by paravirtual drivers that use 3-netdev model. 786 * 787 */ 788 void net_failover_destroy(struct failover *failover) 789 { 790 struct net_failover_info *nfo_info; 791 struct net_device *failover_dev; 792 struct net_device *slave_dev; 793 794 if (!failover) 795 return; 796 797 failover_dev = rcu_dereference(failover->failover_dev); 798 nfo_info = netdev_priv(failover_dev); 799 800 netif_device_detach(failover_dev); 801 802 rtnl_lock(); 803 804 slave_dev = rtnl_dereference(nfo_info->primary_dev); 805 if (slave_dev) 806 failover_slave_unregister(slave_dev); 807 808 slave_dev = rtnl_dereference(nfo_info->standby_dev); 809 if (slave_dev) 810 failover_slave_unregister(slave_dev); 811 812 failover_unregister(failover); 813 814 unregister_netdevice(failover_dev); 815 816 rtnl_unlock(); 817 818 free_netdev(failover_dev); 819 } 820 EXPORT_SYMBOL_GPL(net_failover_destroy); 821 822 static __init int 823 net_failover_init(void) 824 { 825 return 0; 826 } 827 module_init(net_failover_init); 828 829 static __exit 830 void net_failover_exit(void) 831 { 832 } 833 module_exit(net_failover_exit); 834 835 MODULE_DESCRIPTION("Failover driver for Paravirtual drivers"); 836 MODULE_LICENSE("GPL v2"); 837