1 /* 2 * originally based on the dummy device. 3 * 4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov. 5 * Licensed under the GPL. Based on dummy.c, and eql.c devices. 6 * 7 * bonding.c: an Ethernet Bonding driver 8 * 9 * This is useful to talk to a Cisco EtherChannel compatible equipment: 10 * Cisco 5500 11 * Sun Trunking (Solaris) 12 * Alteon AceDirector Trunks 13 * Linux Bonding 14 * and probably many L2 switches ... 15 * 16 * How it works: 17 * ifconfig bond0 ipaddress netmask up 18 * will setup a network device, with an ip address. No mac address 19 * will be assigned at this time. The hw mac address will come from 20 * the first slave bonded to the channel. All slaves will then use 21 * this hw mac address. 22 * 23 * ifconfig bond0 down 24 * will release all slaves, marking them as down. 25 * 26 * ifenslave bond0 eth0 27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either 28 * a: be used as initial mac address 29 * b: if a hw mac address already is there, eth0's hw mac address 30 * will then be set from bond0. 31 * 32 */ 33 34 #include <linux/kernel.h> 35 #include <linux/module.h> 36 #include <linux/types.h> 37 #include <linux/fcntl.h> 38 #include <linux/interrupt.h> 39 #include <linux/ptrace.h> 40 #include <linux/ioport.h> 41 #include <linux/in.h> 42 #include <net/ip.h> 43 #include <linux/ip.h> 44 #include <linux/icmp.h> 45 #include <linux/icmpv6.h> 46 #include <linux/tcp.h> 47 #include <linux/udp.h> 48 #include <linux/slab.h> 49 #include <linux/string.h> 50 #include <linux/init.h> 51 #include <linux/timer.h> 52 #include <linux/socket.h> 53 #include <linux/ctype.h> 54 #include <linux/inet.h> 55 #include <linux/bitops.h> 56 #include <linux/io.h> 57 #include <asm/dma.h> 58 #include <linux/uaccess.h> 59 #include <linux/errno.h> 60 #include <linux/netdevice.h> 61 #include <linux/inetdevice.h> 62 #include <linux/igmp.h> 63 #include <linux/etherdevice.h> 64 #include <linux/skbuff.h> 65 #include <net/sock.h> 66 #include <linux/rtnetlink.h> 67 #include <linux/smp.h> 68 #include <linux/if_ether.h> 69 #include <net/arp.h> 70 #include <linux/mii.h> 71 #include <linux/ethtool.h> 72 #include <linux/if_vlan.h> 73 #include <linux/if_bonding.h> 74 #include <linux/phy.h> 75 #include <linux/jiffies.h> 76 #include <linux/preempt.h> 77 #include <net/route.h> 78 #include <net/net_namespace.h> 79 #include <net/netns/generic.h> 80 #include <net/pkt_sched.h> 81 #include <linux/rculist.h> 82 #include <net/flow_dissector.h> 83 #include <net/xfrm.h> 84 #include <net/bonding.h> 85 #include <net/bond_3ad.h> 86 #include <net/bond_alb.h> 87 #if IS_ENABLED(CONFIG_TLS_DEVICE) 88 #include <net/tls.h> 89 #endif 90 91 #include "bonding_priv.h" 92 93 /*---------------------------- Module parameters ----------------------------*/ 94 95 /* monitor all links that often (in milliseconds). <=0 disables monitoring */ 96 97 static int max_bonds = BOND_DEFAULT_MAX_BONDS; 98 static int tx_queues = BOND_DEFAULT_TX_QUEUES; 99 static int num_peer_notif = 1; 100 static int miimon; 101 static int updelay; 102 static int downdelay; 103 static int use_carrier = 1; 104 static char *mode; 105 static char *primary; 106 static char *primary_reselect; 107 static char *lacp_rate; 108 static int min_links; 109 static char *ad_select; 110 static char *xmit_hash_policy; 111 static int arp_interval; 112 static char *arp_ip_target[BOND_MAX_ARP_TARGETS]; 113 static char *arp_validate; 114 static char *arp_all_targets; 115 static char *fail_over_mac; 116 static int all_slaves_active; 117 static struct bond_params bonding_defaults; 118 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP; 119 static int packets_per_slave = 1; 120 static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 121 122 module_param(max_bonds, int, 0); 123 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices"); 124 module_param(tx_queues, int, 0); 125 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)"); 126 module_param_named(num_grat_arp, num_peer_notif, int, 0644); 127 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on " 128 "failover event (alias of num_unsol_na)"); 129 module_param_named(num_unsol_na, num_peer_notif, int, 0644); 130 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on " 131 "failover event (alias of num_grat_arp)"); 132 module_param(miimon, int, 0); 133 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds"); 134 module_param(updelay, int, 0); 135 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds"); 136 module_param(downdelay, int, 0); 137 MODULE_PARM_DESC(downdelay, "Delay before considering link down, " 138 "in milliseconds"); 139 module_param(use_carrier, int, 0); 140 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; " 141 "0 for off, 1 for on (default)"); 142 module_param(mode, charp, 0); 143 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, " 144 "1 for active-backup, 2 for balance-xor, " 145 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, " 146 "6 for balance-alb"); 147 module_param(primary, charp, 0); 148 MODULE_PARM_DESC(primary, "Primary network device to use"); 149 module_param(primary_reselect, charp, 0); 150 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave " 151 "once it comes up; " 152 "0 for always (default), " 153 "1 for only if speed of primary is " 154 "better, " 155 "2 for only on active slave " 156 "failure"); 157 module_param(lacp_rate, charp, 0); 158 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; " 159 "0 for slow, 1 for fast"); 160 module_param(ad_select, charp, 0); 161 MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; " 162 "0 for stable (default), 1 for bandwidth, " 163 "2 for count"); 164 module_param(min_links, int, 0); 165 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier"); 166 167 module_param(xmit_hash_policy, charp, 0); 168 MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; " 169 "0 for layer 2 (default), 1 for layer 3+4, " 170 "2 for layer 2+3, 3 for encap layer 2+3, " 171 "4 for encap layer 3+4, 5 for vlan+srcmac"); 172 module_param(arp_interval, int, 0); 173 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds"); 174 module_param_array(arp_ip_target, charp, NULL, 0); 175 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form"); 176 module_param(arp_validate, charp, 0); 177 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; " 178 "0 for none (default), 1 for active, " 179 "2 for backup, 3 for all"); 180 module_param(arp_all_targets, charp, 0); 181 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all"); 182 module_param(fail_over_mac, charp, 0); 183 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to " 184 "the same MAC; 0 for none (default), " 185 "1 for active, 2 for follow"); 186 module_param(all_slaves_active, int, 0); 187 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface " 188 "by setting active flag for all slaves; " 189 "0 for never (default), 1 for always."); 190 module_param(resend_igmp, int, 0); 191 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on " 192 "link failure"); 193 module_param(packets_per_slave, int, 0); 194 MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr " 195 "mode; 0 for a random slave, 1 packet per " 196 "slave (default), >1 packets per slave."); 197 module_param(lp_interval, uint, 0); 198 MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where " 199 "the bonding driver sends learning packets to " 200 "each slaves peer switch. The default is 1."); 201 202 /*----------------------------- Global variables ----------------------------*/ 203 204 #ifdef CONFIG_NET_POLL_CONTROLLER 205 atomic_t netpoll_block_tx = ATOMIC_INIT(0); 206 #endif 207 208 unsigned int bond_net_id __read_mostly; 209 210 static const struct flow_dissector_key flow_keys_bonding_keys[] = { 211 { 212 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 213 .offset = offsetof(struct flow_keys, control), 214 }, 215 { 216 .key_id = FLOW_DISSECTOR_KEY_BASIC, 217 .offset = offsetof(struct flow_keys, basic), 218 }, 219 { 220 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, 221 .offset = offsetof(struct flow_keys, addrs.v4addrs), 222 }, 223 { 224 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, 225 .offset = offsetof(struct flow_keys, addrs.v6addrs), 226 }, 227 { 228 .key_id = FLOW_DISSECTOR_KEY_TIPC, 229 .offset = offsetof(struct flow_keys, addrs.tipckey), 230 }, 231 { 232 .key_id = FLOW_DISSECTOR_KEY_PORTS, 233 .offset = offsetof(struct flow_keys, ports), 234 }, 235 { 236 .key_id = FLOW_DISSECTOR_KEY_ICMP, 237 .offset = offsetof(struct flow_keys, icmp), 238 }, 239 { 240 .key_id = FLOW_DISSECTOR_KEY_VLAN, 241 .offset = offsetof(struct flow_keys, vlan), 242 }, 243 { 244 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, 245 .offset = offsetof(struct flow_keys, tags), 246 }, 247 { 248 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, 249 .offset = offsetof(struct flow_keys, keyid), 250 }, 251 }; 252 253 static struct flow_dissector flow_keys_bonding __read_mostly; 254 255 /*-------------------------- Forward declarations ---------------------------*/ 256 257 static int bond_init(struct net_device *bond_dev); 258 static void bond_uninit(struct net_device *bond_dev); 259 static void bond_get_stats(struct net_device *bond_dev, 260 struct rtnl_link_stats64 *stats); 261 static void bond_slave_arr_handler(struct work_struct *work); 262 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act, 263 int mod); 264 static void bond_netdev_notify_work(struct work_struct *work); 265 266 /*---------------------------- General routines -----------------------------*/ 267 268 const char *bond_mode_name(int mode) 269 { 270 static const char *names[] = { 271 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)", 272 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)", 273 [BOND_MODE_XOR] = "load balancing (xor)", 274 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)", 275 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation", 276 [BOND_MODE_TLB] = "transmit load balancing", 277 [BOND_MODE_ALB] = "adaptive load balancing", 278 }; 279 280 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB) 281 return "unknown"; 282 283 return names[mode]; 284 } 285 286 /** 287 * bond_dev_queue_xmit - Prepare skb for xmit. 288 * 289 * @bond: bond device that got this skb for tx. 290 * @skb: hw accel VLAN tagged skb to transmit 291 * @slave_dev: slave that is supposed to xmit this skbuff 292 */ 293 netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, 294 struct net_device *slave_dev) 295 { 296 skb->dev = slave_dev; 297 298 BUILD_BUG_ON(sizeof(skb->queue_mapping) != 299 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping)); 300 skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping); 301 302 if (unlikely(netpoll_tx_running(bond->dev))) 303 return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb); 304 305 return dev_queue_xmit(skb); 306 } 307 308 bool bond_sk_check(struct bonding *bond) 309 { 310 switch (BOND_MODE(bond)) { 311 case BOND_MODE_8023AD: 312 case BOND_MODE_XOR: 313 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34) 314 return true; 315 fallthrough; 316 default: 317 return false; 318 } 319 } 320 321 static bool bond_xdp_check(struct bonding *bond) 322 { 323 switch (BOND_MODE(bond)) { 324 case BOND_MODE_ROUNDROBIN: 325 case BOND_MODE_ACTIVEBACKUP: 326 return true; 327 case BOND_MODE_8023AD: 328 case BOND_MODE_XOR: 329 /* vlan+srcmac is not supported with XDP as in most cases the 802.1q 330 * payload is not in the packet due to hardware offload. 331 */ 332 if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC) 333 return true; 334 fallthrough; 335 default: 336 return false; 337 } 338 } 339 340 /*---------------------------------- VLAN -----------------------------------*/ 341 342 /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid, 343 * We don't protect the slave list iteration with a lock because: 344 * a. This operation is performed in IOCTL context, 345 * b. The operation is protected by the RTNL semaphore in the 8021q code, 346 * c. Holding a lock with BH disabled while directly calling a base driver 347 * entry point is generally a BAD idea. 348 * 349 * The design of synchronization/protection for this operation in the 8021q 350 * module is good for one or more VLAN devices over a single physical device 351 * and cannot be extended for a teaming solution like bonding, so there is a 352 * potential race condition here where a net device from the vlan group might 353 * be referenced (either by a base driver or the 8021q code) while it is being 354 * removed from the system. However, it turns out we're not making matters 355 * worse, and if it works for regular VLAN usage it will work here too. 356 */ 357 358 /** 359 * bond_vlan_rx_add_vid - Propagates adding an id to slaves 360 * @bond_dev: bonding net device that got called 361 * @proto: network protocol ID 362 * @vid: vlan id being added 363 */ 364 static int bond_vlan_rx_add_vid(struct net_device *bond_dev, 365 __be16 proto, u16 vid) 366 { 367 struct bonding *bond = netdev_priv(bond_dev); 368 struct slave *slave, *rollback_slave; 369 struct list_head *iter; 370 int res; 371 372 bond_for_each_slave(bond, slave, iter) { 373 res = vlan_vid_add(slave->dev, proto, vid); 374 if (res) 375 goto unwind; 376 } 377 378 return 0; 379 380 unwind: 381 /* unwind to the slave that failed */ 382 bond_for_each_slave(bond, rollback_slave, iter) { 383 if (rollback_slave == slave) 384 break; 385 386 vlan_vid_del(rollback_slave->dev, proto, vid); 387 } 388 389 return res; 390 } 391 392 /** 393 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves 394 * @bond_dev: bonding net device that got called 395 * @proto: network protocol ID 396 * @vid: vlan id being removed 397 */ 398 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev, 399 __be16 proto, u16 vid) 400 { 401 struct bonding *bond = netdev_priv(bond_dev); 402 struct list_head *iter; 403 struct slave *slave; 404 405 bond_for_each_slave(bond, slave, iter) 406 vlan_vid_del(slave->dev, proto, vid); 407 408 if (bond_is_lb(bond)) 409 bond_alb_clear_vlan(bond, vid); 410 411 return 0; 412 } 413 414 /*---------------------------------- XFRM -----------------------------------*/ 415 416 #ifdef CONFIG_XFRM_OFFLOAD 417 /** 418 * bond_ipsec_add_sa - program device with a security association 419 * @xs: pointer to transformer state struct 420 **/ 421 static int bond_ipsec_add_sa(struct xfrm_state *xs) 422 { 423 struct net_device *bond_dev = xs->xso.dev; 424 struct bond_ipsec *ipsec; 425 struct bonding *bond; 426 struct slave *slave; 427 int err; 428 429 if (!bond_dev) 430 return -EINVAL; 431 432 rcu_read_lock(); 433 bond = netdev_priv(bond_dev); 434 slave = rcu_dereference(bond->curr_active_slave); 435 if (!slave) { 436 rcu_read_unlock(); 437 return -ENODEV; 438 } 439 440 if (!slave->dev->xfrmdev_ops || 441 !slave->dev->xfrmdev_ops->xdo_dev_state_add || 442 netif_is_bond_master(slave->dev)) { 443 slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n"); 444 rcu_read_unlock(); 445 return -EINVAL; 446 } 447 448 ipsec = kmalloc(sizeof(*ipsec), GFP_ATOMIC); 449 if (!ipsec) { 450 rcu_read_unlock(); 451 return -ENOMEM; 452 } 453 xs->xso.real_dev = slave->dev; 454 455 err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs); 456 if (!err) { 457 ipsec->xs = xs; 458 INIT_LIST_HEAD(&ipsec->list); 459 spin_lock_bh(&bond->ipsec_lock); 460 list_add(&ipsec->list, &bond->ipsec_list); 461 spin_unlock_bh(&bond->ipsec_lock); 462 } else { 463 kfree(ipsec); 464 } 465 rcu_read_unlock(); 466 return err; 467 } 468 469 static void bond_ipsec_add_sa_all(struct bonding *bond) 470 { 471 struct net_device *bond_dev = bond->dev; 472 struct bond_ipsec *ipsec; 473 struct slave *slave; 474 475 rcu_read_lock(); 476 slave = rcu_dereference(bond->curr_active_slave); 477 if (!slave) 478 goto out; 479 480 if (!slave->dev->xfrmdev_ops || 481 !slave->dev->xfrmdev_ops->xdo_dev_state_add || 482 netif_is_bond_master(slave->dev)) { 483 spin_lock_bh(&bond->ipsec_lock); 484 if (!list_empty(&bond->ipsec_list)) 485 slave_warn(bond_dev, slave->dev, 486 "%s: no slave xdo_dev_state_add\n", 487 __func__); 488 spin_unlock_bh(&bond->ipsec_lock); 489 goto out; 490 } 491 492 spin_lock_bh(&bond->ipsec_lock); 493 list_for_each_entry(ipsec, &bond->ipsec_list, list) { 494 ipsec->xs->xso.real_dev = slave->dev; 495 if (slave->dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs)) { 496 slave_warn(bond_dev, slave->dev, "%s: failed to add SA\n", __func__); 497 ipsec->xs->xso.real_dev = NULL; 498 } 499 } 500 spin_unlock_bh(&bond->ipsec_lock); 501 out: 502 rcu_read_unlock(); 503 } 504 505 /** 506 * bond_ipsec_del_sa - clear out this specific SA 507 * @xs: pointer to transformer state struct 508 **/ 509 static void bond_ipsec_del_sa(struct xfrm_state *xs) 510 { 511 struct net_device *bond_dev = xs->xso.dev; 512 struct bond_ipsec *ipsec; 513 struct bonding *bond; 514 struct slave *slave; 515 516 if (!bond_dev) 517 return; 518 519 rcu_read_lock(); 520 bond = netdev_priv(bond_dev); 521 slave = rcu_dereference(bond->curr_active_slave); 522 523 if (!slave) 524 goto out; 525 526 if (!xs->xso.real_dev) 527 goto out; 528 529 WARN_ON(xs->xso.real_dev != slave->dev); 530 531 if (!slave->dev->xfrmdev_ops || 532 !slave->dev->xfrmdev_ops->xdo_dev_state_delete || 533 netif_is_bond_master(slave->dev)) { 534 slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__); 535 goto out; 536 } 537 538 slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs); 539 out: 540 spin_lock_bh(&bond->ipsec_lock); 541 list_for_each_entry(ipsec, &bond->ipsec_list, list) { 542 if (ipsec->xs == xs) { 543 list_del(&ipsec->list); 544 kfree(ipsec); 545 break; 546 } 547 } 548 spin_unlock_bh(&bond->ipsec_lock); 549 rcu_read_unlock(); 550 } 551 552 static void bond_ipsec_del_sa_all(struct bonding *bond) 553 { 554 struct net_device *bond_dev = bond->dev; 555 struct bond_ipsec *ipsec; 556 struct slave *slave; 557 558 rcu_read_lock(); 559 slave = rcu_dereference(bond->curr_active_slave); 560 if (!slave) { 561 rcu_read_unlock(); 562 return; 563 } 564 565 spin_lock_bh(&bond->ipsec_lock); 566 list_for_each_entry(ipsec, &bond->ipsec_list, list) { 567 if (!ipsec->xs->xso.real_dev) 568 continue; 569 570 if (!slave->dev->xfrmdev_ops || 571 !slave->dev->xfrmdev_ops->xdo_dev_state_delete || 572 netif_is_bond_master(slave->dev)) { 573 slave_warn(bond_dev, slave->dev, 574 "%s: no slave xdo_dev_state_delete\n", 575 __func__); 576 } else { 577 slave->dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs); 578 } 579 ipsec->xs->xso.real_dev = NULL; 580 } 581 spin_unlock_bh(&bond->ipsec_lock); 582 rcu_read_unlock(); 583 } 584 585 /** 586 * bond_ipsec_offload_ok - can this packet use the xfrm hw offload 587 * @skb: current data packet 588 * @xs: pointer to transformer state struct 589 **/ 590 static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs) 591 { 592 struct net_device *bond_dev = xs->xso.dev; 593 struct net_device *real_dev; 594 struct slave *curr_active; 595 struct bonding *bond; 596 int err; 597 598 bond = netdev_priv(bond_dev); 599 rcu_read_lock(); 600 curr_active = rcu_dereference(bond->curr_active_slave); 601 real_dev = curr_active->dev; 602 603 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 604 err = false; 605 goto out; 606 } 607 608 if (!xs->xso.real_dev) { 609 err = false; 610 goto out; 611 } 612 613 if (!real_dev->xfrmdev_ops || 614 !real_dev->xfrmdev_ops->xdo_dev_offload_ok || 615 netif_is_bond_master(real_dev)) { 616 err = false; 617 goto out; 618 } 619 620 err = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs); 621 out: 622 rcu_read_unlock(); 623 return err; 624 } 625 626 static const struct xfrmdev_ops bond_xfrmdev_ops = { 627 .xdo_dev_state_add = bond_ipsec_add_sa, 628 .xdo_dev_state_delete = bond_ipsec_del_sa, 629 .xdo_dev_offload_ok = bond_ipsec_offload_ok, 630 }; 631 #endif /* CONFIG_XFRM_OFFLOAD */ 632 633 /*------------------------------- Link status -------------------------------*/ 634 635 /* Set the carrier state for the master according to the state of its 636 * slaves. If any slaves are up, the master is up. In 802.3ad mode, 637 * do special 802.3ad magic. 638 * 639 * Returns zero if carrier state does not change, nonzero if it does. 640 */ 641 int bond_set_carrier(struct bonding *bond) 642 { 643 struct list_head *iter; 644 struct slave *slave; 645 646 if (!bond_has_slaves(bond)) 647 goto down; 648 649 if (BOND_MODE(bond) == BOND_MODE_8023AD) 650 return bond_3ad_set_carrier(bond); 651 652 bond_for_each_slave(bond, slave, iter) { 653 if (slave->link == BOND_LINK_UP) { 654 if (!netif_carrier_ok(bond->dev)) { 655 netif_carrier_on(bond->dev); 656 return 1; 657 } 658 return 0; 659 } 660 } 661 662 down: 663 if (netif_carrier_ok(bond->dev)) { 664 netif_carrier_off(bond->dev); 665 return 1; 666 } 667 return 0; 668 } 669 670 /* Get link speed and duplex from the slave's base driver 671 * using ethtool. If for some reason the call fails or the 672 * values are invalid, set speed and duplex to -1, 673 * and return. Return 1 if speed or duplex settings are 674 * UNKNOWN; 0 otherwise. 675 */ 676 static int bond_update_speed_duplex(struct slave *slave) 677 { 678 struct net_device *slave_dev = slave->dev; 679 struct ethtool_link_ksettings ecmd; 680 int res; 681 682 slave->speed = SPEED_UNKNOWN; 683 slave->duplex = DUPLEX_UNKNOWN; 684 685 res = __ethtool_get_link_ksettings(slave_dev, &ecmd); 686 if (res < 0) 687 return 1; 688 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1)) 689 return 1; 690 switch (ecmd.base.duplex) { 691 case DUPLEX_FULL: 692 case DUPLEX_HALF: 693 break; 694 default: 695 return 1; 696 } 697 698 slave->speed = ecmd.base.speed; 699 slave->duplex = ecmd.base.duplex; 700 701 return 0; 702 } 703 704 const char *bond_slave_link_status(s8 link) 705 { 706 switch (link) { 707 case BOND_LINK_UP: 708 return "up"; 709 case BOND_LINK_FAIL: 710 return "going down"; 711 case BOND_LINK_DOWN: 712 return "down"; 713 case BOND_LINK_BACK: 714 return "going back"; 715 default: 716 return "unknown"; 717 } 718 } 719 720 /* if <dev> supports MII link status reporting, check its link status. 721 * 722 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(), 723 * depending upon the setting of the use_carrier parameter. 724 * 725 * Return either BMSR_LSTATUS, meaning that the link is up (or we 726 * can't tell and just pretend it is), or 0, meaning that the link is 727 * down. 728 * 729 * If reporting is non-zero, instead of faking link up, return -1 if 730 * both ETHTOOL and MII ioctls fail (meaning the device does not 731 * support them). If use_carrier is set, return whatever it says. 732 * It'd be nice if there was a good way to tell if a driver supports 733 * netif_carrier, but there really isn't. 734 */ 735 static int bond_check_dev_link(struct bonding *bond, 736 struct net_device *slave_dev, int reporting) 737 { 738 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 739 int (*ioctl)(struct net_device *, struct ifreq *, int); 740 struct ifreq ifr; 741 struct mii_ioctl_data *mii; 742 743 if (!reporting && !netif_running(slave_dev)) 744 return 0; 745 746 if (bond->params.use_carrier) 747 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0; 748 749 /* Try to get link status using Ethtool first. */ 750 if (slave_dev->ethtool_ops->get_link) 751 return slave_dev->ethtool_ops->get_link(slave_dev) ? 752 BMSR_LSTATUS : 0; 753 754 /* Ethtool can't be used, fallback to MII ioctls. */ 755 ioctl = slave_ops->ndo_eth_ioctl; 756 if (ioctl) { 757 /* TODO: set pointer to correct ioctl on a per team member 758 * bases to make this more efficient. that is, once 759 * we determine the correct ioctl, we will always 760 * call it and not the others for that team 761 * member. 762 */ 763 764 /* We cannot assume that SIOCGMIIPHY will also read a 765 * register; not all network drivers (e.g., e100) 766 * support that. 767 */ 768 769 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */ 770 strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ); 771 mii = if_mii(&ifr); 772 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) { 773 mii->reg_num = MII_BMSR; 774 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0) 775 return mii->val_out & BMSR_LSTATUS; 776 } 777 } 778 779 /* If reporting, report that either there's no ndo_eth_ioctl, 780 * or both SIOCGMIIREG and get_link failed (meaning that we 781 * cannot report link status). If not reporting, pretend 782 * we're ok. 783 */ 784 return reporting ? -1 : BMSR_LSTATUS; 785 } 786 787 /*----------------------------- Multicast list ------------------------------*/ 788 789 /* Push the promiscuity flag down to appropriate slaves */ 790 static int bond_set_promiscuity(struct bonding *bond, int inc) 791 { 792 struct list_head *iter; 793 int err = 0; 794 795 if (bond_uses_primary(bond)) { 796 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); 797 798 if (curr_active) 799 err = dev_set_promiscuity(curr_active->dev, inc); 800 } else { 801 struct slave *slave; 802 803 bond_for_each_slave(bond, slave, iter) { 804 err = dev_set_promiscuity(slave->dev, inc); 805 if (err) 806 return err; 807 } 808 } 809 return err; 810 } 811 812 /* Push the allmulti flag down to all slaves */ 813 static int bond_set_allmulti(struct bonding *bond, int inc) 814 { 815 struct list_head *iter; 816 int err = 0; 817 818 if (bond_uses_primary(bond)) { 819 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); 820 821 if (curr_active) 822 err = dev_set_allmulti(curr_active->dev, inc); 823 } else { 824 struct slave *slave; 825 826 bond_for_each_slave(bond, slave, iter) { 827 err = dev_set_allmulti(slave->dev, inc); 828 if (err) 829 return err; 830 } 831 } 832 return err; 833 } 834 835 /* Retrieve the list of registered multicast addresses for the bonding 836 * device and retransmit an IGMP JOIN request to the current active 837 * slave. 838 */ 839 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work) 840 { 841 struct bonding *bond = container_of(work, struct bonding, 842 mcast_work.work); 843 844 if (!rtnl_trylock()) { 845 queue_delayed_work(bond->wq, &bond->mcast_work, 1); 846 return; 847 } 848 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev); 849 850 if (bond->igmp_retrans > 1) { 851 bond->igmp_retrans--; 852 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5); 853 } 854 rtnl_unlock(); 855 } 856 857 /* Flush bond's hardware addresses from slave */ 858 static void bond_hw_addr_flush(struct net_device *bond_dev, 859 struct net_device *slave_dev) 860 { 861 struct bonding *bond = netdev_priv(bond_dev); 862 863 dev_uc_unsync(slave_dev, bond_dev); 864 dev_mc_unsync(slave_dev, bond_dev); 865 866 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 867 /* del lacpdu mc addr from mc list */ 868 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR; 869 870 dev_mc_del(slave_dev, lacpdu_multicast); 871 } 872 } 873 874 /*--------------------------- Active slave change ---------------------------*/ 875 876 /* Update the hardware address list and promisc/allmulti for the new and 877 * old active slaves (if any). Modes that are not using primary keep all 878 * slaves up date at all times; only the modes that use primary need to call 879 * this function to swap these settings during a failover. 880 */ 881 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active, 882 struct slave *old_active) 883 { 884 if (old_active) { 885 if (bond->dev->flags & IFF_PROMISC) 886 dev_set_promiscuity(old_active->dev, -1); 887 888 if (bond->dev->flags & IFF_ALLMULTI) 889 dev_set_allmulti(old_active->dev, -1); 890 891 bond_hw_addr_flush(bond->dev, old_active->dev); 892 } 893 894 if (new_active) { 895 /* FIXME: Signal errors upstream. */ 896 if (bond->dev->flags & IFF_PROMISC) 897 dev_set_promiscuity(new_active->dev, 1); 898 899 if (bond->dev->flags & IFF_ALLMULTI) 900 dev_set_allmulti(new_active->dev, 1); 901 902 netif_addr_lock_bh(bond->dev); 903 dev_uc_sync(new_active->dev, bond->dev); 904 dev_mc_sync(new_active->dev, bond->dev); 905 netif_addr_unlock_bh(bond->dev); 906 } 907 } 908 909 /** 910 * bond_set_dev_addr - clone slave's address to bond 911 * @bond_dev: bond net device 912 * @slave_dev: slave net device 913 * 914 * Should be called with RTNL held. 915 */ 916 static int bond_set_dev_addr(struct net_device *bond_dev, 917 struct net_device *slave_dev) 918 { 919 int err; 920 921 slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n", 922 bond_dev, slave_dev, slave_dev->addr_len); 923 err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL); 924 if (err) 925 return err; 926 927 __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len); 928 bond_dev->addr_assign_type = NET_ADDR_STOLEN; 929 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev); 930 return 0; 931 } 932 933 static struct slave *bond_get_old_active(struct bonding *bond, 934 struct slave *new_active) 935 { 936 struct slave *slave; 937 struct list_head *iter; 938 939 bond_for_each_slave(bond, slave, iter) { 940 if (slave == new_active) 941 continue; 942 943 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr)) 944 return slave; 945 } 946 947 return NULL; 948 } 949 950 /* bond_do_fail_over_mac 951 * 952 * Perform special MAC address swapping for fail_over_mac settings 953 * 954 * Called with RTNL 955 */ 956 static void bond_do_fail_over_mac(struct bonding *bond, 957 struct slave *new_active, 958 struct slave *old_active) 959 { 960 u8 tmp_mac[MAX_ADDR_LEN]; 961 struct sockaddr_storage ss; 962 int rv; 963 964 switch (bond->params.fail_over_mac) { 965 case BOND_FOM_ACTIVE: 966 if (new_active) { 967 rv = bond_set_dev_addr(bond->dev, new_active->dev); 968 if (rv) 969 slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n", 970 -rv); 971 } 972 break; 973 case BOND_FOM_FOLLOW: 974 /* if new_active && old_active, swap them 975 * if just old_active, do nothing (going to no active slave) 976 * if just new_active, set new_active to bond's MAC 977 */ 978 if (!new_active) 979 return; 980 981 if (!old_active) 982 old_active = bond_get_old_active(bond, new_active); 983 984 if (old_active) { 985 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr, 986 new_active->dev->addr_len); 987 bond_hw_addr_copy(ss.__data, 988 old_active->dev->dev_addr, 989 old_active->dev->addr_len); 990 ss.ss_family = new_active->dev->type; 991 } else { 992 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr, 993 bond->dev->addr_len); 994 ss.ss_family = bond->dev->type; 995 } 996 997 rv = dev_set_mac_address(new_active->dev, 998 (struct sockaddr *)&ss, NULL); 999 if (rv) { 1000 slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n", 1001 -rv); 1002 goto out; 1003 } 1004 1005 if (!old_active) 1006 goto out; 1007 1008 bond_hw_addr_copy(ss.__data, tmp_mac, 1009 new_active->dev->addr_len); 1010 ss.ss_family = old_active->dev->type; 1011 1012 rv = dev_set_mac_address(old_active->dev, 1013 (struct sockaddr *)&ss, NULL); 1014 if (rv) 1015 slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n", 1016 -rv); 1017 out: 1018 break; 1019 default: 1020 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n", 1021 bond->params.fail_over_mac); 1022 break; 1023 } 1024 1025 } 1026 1027 static struct slave *bond_choose_primary_or_current(struct bonding *bond) 1028 { 1029 struct slave *prim = rtnl_dereference(bond->primary_slave); 1030 struct slave *curr = rtnl_dereference(bond->curr_active_slave); 1031 1032 if (!prim || prim->link != BOND_LINK_UP) { 1033 if (!curr || curr->link != BOND_LINK_UP) 1034 return NULL; 1035 return curr; 1036 } 1037 1038 if (bond->force_primary) { 1039 bond->force_primary = false; 1040 return prim; 1041 } 1042 1043 if (!curr || curr->link != BOND_LINK_UP) 1044 return prim; 1045 1046 /* At this point, prim and curr are both up */ 1047 switch (bond->params.primary_reselect) { 1048 case BOND_PRI_RESELECT_ALWAYS: 1049 return prim; 1050 case BOND_PRI_RESELECT_BETTER: 1051 if (prim->speed < curr->speed) 1052 return curr; 1053 if (prim->speed == curr->speed && prim->duplex <= curr->duplex) 1054 return curr; 1055 return prim; 1056 case BOND_PRI_RESELECT_FAILURE: 1057 return curr; 1058 default: 1059 netdev_err(bond->dev, "impossible primary_reselect %d\n", 1060 bond->params.primary_reselect); 1061 return curr; 1062 } 1063 } 1064 1065 /** 1066 * bond_find_best_slave - select the best available slave to be the active one 1067 * @bond: our bonding struct 1068 */ 1069 static struct slave *bond_find_best_slave(struct bonding *bond) 1070 { 1071 struct slave *slave, *bestslave = NULL; 1072 struct list_head *iter; 1073 int mintime = bond->params.updelay; 1074 1075 slave = bond_choose_primary_or_current(bond); 1076 if (slave) 1077 return slave; 1078 1079 bond_for_each_slave(bond, slave, iter) { 1080 if (slave->link == BOND_LINK_UP) 1081 return slave; 1082 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) && 1083 slave->delay < mintime) { 1084 mintime = slave->delay; 1085 bestslave = slave; 1086 } 1087 } 1088 1089 return bestslave; 1090 } 1091 1092 static bool bond_should_notify_peers(struct bonding *bond) 1093 { 1094 struct slave *slave; 1095 1096 rcu_read_lock(); 1097 slave = rcu_dereference(bond->curr_active_slave); 1098 rcu_read_unlock(); 1099 1100 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n", 1101 slave ? slave->dev->name : "NULL"); 1102 1103 if (!slave || !bond->send_peer_notif || 1104 bond->send_peer_notif % 1105 max(1, bond->params.peer_notif_delay) != 0 || 1106 !netif_carrier_ok(bond->dev) || 1107 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state)) 1108 return false; 1109 1110 return true; 1111 } 1112 1113 /** 1114 * bond_change_active_slave - change the active slave into the specified one 1115 * @bond: our bonding struct 1116 * @new_active: the new slave to make the active one 1117 * 1118 * Set the new slave to the bond's settings and unset them on the old 1119 * curr_active_slave. 1120 * Setting include flags, mc-list, promiscuity, allmulti, etc. 1121 * 1122 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP, 1123 * because it is apparently the best available slave we have, even though its 1124 * updelay hasn't timed out yet. 1125 * 1126 * Caller must hold RTNL. 1127 */ 1128 void bond_change_active_slave(struct bonding *bond, struct slave *new_active) 1129 { 1130 struct slave *old_active; 1131 1132 ASSERT_RTNL(); 1133 1134 old_active = rtnl_dereference(bond->curr_active_slave); 1135 1136 if (old_active == new_active) 1137 return; 1138 1139 #ifdef CONFIG_XFRM_OFFLOAD 1140 bond_ipsec_del_sa_all(bond); 1141 #endif /* CONFIG_XFRM_OFFLOAD */ 1142 1143 if (new_active) { 1144 new_active->last_link_up = jiffies; 1145 1146 if (new_active->link == BOND_LINK_BACK) { 1147 if (bond_uses_primary(bond)) { 1148 slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n", 1149 (bond->params.updelay - new_active->delay) * bond->params.miimon); 1150 } 1151 1152 new_active->delay = 0; 1153 bond_set_slave_link_state(new_active, BOND_LINK_UP, 1154 BOND_SLAVE_NOTIFY_NOW); 1155 1156 if (BOND_MODE(bond) == BOND_MODE_8023AD) 1157 bond_3ad_handle_link_change(new_active, BOND_LINK_UP); 1158 1159 if (bond_is_lb(bond)) 1160 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP); 1161 } else { 1162 if (bond_uses_primary(bond)) 1163 slave_info(bond->dev, new_active->dev, "making interface the new active one\n"); 1164 } 1165 } 1166 1167 if (bond_uses_primary(bond)) 1168 bond_hw_addr_swap(bond, new_active, old_active); 1169 1170 if (bond_is_lb(bond)) { 1171 bond_alb_handle_active_change(bond, new_active); 1172 if (old_active) 1173 bond_set_slave_inactive_flags(old_active, 1174 BOND_SLAVE_NOTIFY_NOW); 1175 if (new_active) 1176 bond_set_slave_active_flags(new_active, 1177 BOND_SLAVE_NOTIFY_NOW); 1178 } else { 1179 rcu_assign_pointer(bond->curr_active_slave, new_active); 1180 } 1181 1182 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) { 1183 if (old_active) 1184 bond_set_slave_inactive_flags(old_active, 1185 BOND_SLAVE_NOTIFY_NOW); 1186 1187 if (new_active) { 1188 bool should_notify_peers = false; 1189 1190 bond_set_slave_active_flags(new_active, 1191 BOND_SLAVE_NOTIFY_NOW); 1192 1193 if (bond->params.fail_over_mac) 1194 bond_do_fail_over_mac(bond, new_active, 1195 old_active); 1196 1197 if (netif_running(bond->dev)) { 1198 bond->send_peer_notif = 1199 bond->params.num_peer_notif * 1200 max(1, bond->params.peer_notif_delay); 1201 should_notify_peers = 1202 bond_should_notify_peers(bond); 1203 } 1204 1205 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev); 1206 if (should_notify_peers) { 1207 bond->send_peer_notif--; 1208 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, 1209 bond->dev); 1210 } 1211 } 1212 } 1213 1214 #ifdef CONFIG_XFRM_OFFLOAD 1215 bond_ipsec_add_sa_all(bond); 1216 #endif /* CONFIG_XFRM_OFFLOAD */ 1217 1218 /* resend IGMP joins since active slave has changed or 1219 * all were sent on curr_active_slave. 1220 * resend only if bond is brought up with the affected 1221 * bonding modes and the retransmission is enabled 1222 */ 1223 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) && 1224 ((bond_uses_primary(bond) && new_active) || 1225 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) { 1226 bond->igmp_retrans = bond->params.resend_igmp; 1227 queue_delayed_work(bond->wq, &bond->mcast_work, 1); 1228 } 1229 } 1230 1231 /** 1232 * bond_select_active_slave - select a new active slave, if needed 1233 * @bond: our bonding struct 1234 * 1235 * This functions should be called when one of the following occurs: 1236 * - The old curr_active_slave has been released or lost its link. 1237 * - The primary_slave has got its link back. 1238 * - A slave has got its link back and there's no old curr_active_slave. 1239 * 1240 * Caller must hold RTNL. 1241 */ 1242 void bond_select_active_slave(struct bonding *bond) 1243 { 1244 struct slave *best_slave; 1245 int rv; 1246 1247 ASSERT_RTNL(); 1248 1249 best_slave = bond_find_best_slave(bond); 1250 if (best_slave != rtnl_dereference(bond->curr_active_slave)) { 1251 bond_change_active_slave(bond, best_slave); 1252 rv = bond_set_carrier(bond); 1253 if (!rv) 1254 return; 1255 1256 if (netif_carrier_ok(bond->dev)) 1257 netdev_info(bond->dev, "active interface up!\n"); 1258 else 1259 netdev_info(bond->dev, "now running without any active interface!\n"); 1260 } 1261 } 1262 1263 #ifdef CONFIG_NET_POLL_CONTROLLER 1264 static inline int slave_enable_netpoll(struct slave *slave) 1265 { 1266 struct netpoll *np; 1267 int err = 0; 1268 1269 np = kzalloc(sizeof(*np), GFP_KERNEL); 1270 err = -ENOMEM; 1271 if (!np) 1272 goto out; 1273 1274 err = __netpoll_setup(np, slave->dev); 1275 if (err) { 1276 kfree(np); 1277 goto out; 1278 } 1279 slave->np = np; 1280 out: 1281 return err; 1282 } 1283 static inline void slave_disable_netpoll(struct slave *slave) 1284 { 1285 struct netpoll *np = slave->np; 1286 1287 if (!np) 1288 return; 1289 1290 slave->np = NULL; 1291 1292 __netpoll_free(np); 1293 } 1294 1295 static void bond_poll_controller(struct net_device *bond_dev) 1296 { 1297 struct bonding *bond = netdev_priv(bond_dev); 1298 struct slave *slave = NULL; 1299 struct list_head *iter; 1300 struct ad_info ad_info; 1301 1302 if (BOND_MODE(bond) == BOND_MODE_8023AD) 1303 if (bond_3ad_get_active_agg_info(bond, &ad_info)) 1304 return; 1305 1306 bond_for_each_slave_rcu(bond, slave, iter) { 1307 if (!bond_slave_is_up(slave)) 1308 continue; 1309 1310 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 1311 struct aggregator *agg = 1312 SLAVE_AD_INFO(slave)->port.aggregator; 1313 1314 if (agg && 1315 agg->aggregator_identifier != ad_info.aggregator_id) 1316 continue; 1317 } 1318 1319 netpoll_poll_dev(slave->dev); 1320 } 1321 } 1322 1323 static void bond_netpoll_cleanup(struct net_device *bond_dev) 1324 { 1325 struct bonding *bond = netdev_priv(bond_dev); 1326 struct list_head *iter; 1327 struct slave *slave; 1328 1329 bond_for_each_slave(bond, slave, iter) 1330 if (bond_slave_is_up(slave)) 1331 slave_disable_netpoll(slave); 1332 } 1333 1334 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) 1335 { 1336 struct bonding *bond = netdev_priv(dev); 1337 struct list_head *iter; 1338 struct slave *slave; 1339 int err = 0; 1340 1341 bond_for_each_slave(bond, slave, iter) { 1342 err = slave_enable_netpoll(slave); 1343 if (err) { 1344 bond_netpoll_cleanup(dev); 1345 break; 1346 } 1347 } 1348 return err; 1349 } 1350 #else 1351 static inline int slave_enable_netpoll(struct slave *slave) 1352 { 1353 return 0; 1354 } 1355 static inline void slave_disable_netpoll(struct slave *slave) 1356 { 1357 } 1358 static void bond_netpoll_cleanup(struct net_device *bond_dev) 1359 { 1360 } 1361 #endif 1362 1363 /*---------------------------------- IOCTL ----------------------------------*/ 1364 1365 static netdev_features_t bond_fix_features(struct net_device *dev, 1366 netdev_features_t features) 1367 { 1368 struct bonding *bond = netdev_priv(dev); 1369 struct list_head *iter; 1370 netdev_features_t mask; 1371 struct slave *slave; 1372 1373 #if IS_ENABLED(CONFIG_TLS_DEVICE) 1374 if (bond_sk_check(bond)) 1375 features |= BOND_TLS_FEATURES; 1376 else 1377 features &= ~BOND_TLS_FEATURES; 1378 #endif 1379 1380 mask = features; 1381 1382 features &= ~NETIF_F_ONE_FOR_ALL; 1383 features |= NETIF_F_ALL_FOR_ALL; 1384 1385 bond_for_each_slave(bond, slave, iter) { 1386 features = netdev_increment_features(features, 1387 slave->dev->features, 1388 mask); 1389 } 1390 features = netdev_add_tso_features(features, mask); 1391 1392 return features; 1393 } 1394 1395 #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ 1396 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \ 1397 NETIF_F_HIGHDMA | NETIF_F_LRO) 1398 1399 #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ 1400 NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE) 1401 1402 #define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ 1403 NETIF_F_GSO_SOFTWARE) 1404 1405 1406 static void bond_compute_features(struct bonding *bond) 1407 { 1408 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE | 1409 IFF_XMIT_DST_RELEASE_PERM; 1410 netdev_features_t vlan_features = BOND_VLAN_FEATURES; 1411 netdev_features_t enc_features = BOND_ENC_FEATURES; 1412 #ifdef CONFIG_XFRM_OFFLOAD 1413 netdev_features_t xfrm_features = BOND_XFRM_FEATURES; 1414 #endif /* CONFIG_XFRM_OFFLOAD */ 1415 netdev_features_t mpls_features = BOND_MPLS_FEATURES; 1416 struct net_device *bond_dev = bond->dev; 1417 struct list_head *iter; 1418 struct slave *slave; 1419 unsigned short max_hard_header_len = ETH_HLEN; 1420 unsigned int gso_max_size = GSO_MAX_SIZE; 1421 u16 gso_max_segs = GSO_MAX_SEGS; 1422 1423 if (!bond_has_slaves(bond)) 1424 goto done; 1425 vlan_features &= NETIF_F_ALL_FOR_ALL; 1426 mpls_features &= NETIF_F_ALL_FOR_ALL; 1427 1428 bond_for_each_slave(bond, slave, iter) { 1429 vlan_features = netdev_increment_features(vlan_features, 1430 slave->dev->vlan_features, BOND_VLAN_FEATURES); 1431 1432 enc_features = netdev_increment_features(enc_features, 1433 slave->dev->hw_enc_features, 1434 BOND_ENC_FEATURES); 1435 1436 #ifdef CONFIG_XFRM_OFFLOAD 1437 xfrm_features = netdev_increment_features(xfrm_features, 1438 slave->dev->hw_enc_features, 1439 BOND_XFRM_FEATURES); 1440 #endif /* CONFIG_XFRM_OFFLOAD */ 1441 1442 mpls_features = netdev_increment_features(mpls_features, 1443 slave->dev->mpls_features, 1444 BOND_MPLS_FEATURES); 1445 1446 dst_release_flag &= slave->dev->priv_flags; 1447 if (slave->dev->hard_header_len > max_hard_header_len) 1448 max_hard_header_len = slave->dev->hard_header_len; 1449 1450 gso_max_size = min(gso_max_size, slave->dev->gso_max_size); 1451 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs); 1452 } 1453 bond_dev->hard_header_len = max_hard_header_len; 1454 1455 done: 1456 bond_dev->vlan_features = vlan_features; 1457 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL | 1458 NETIF_F_HW_VLAN_CTAG_TX | 1459 NETIF_F_HW_VLAN_STAG_TX; 1460 #ifdef CONFIG_XFRM_OFFLOAD 1461 bond_dev->hw_enc_features |= xfrm_features; 1462 #endif /* CONFIG_XFRM_OFFLOAD */ 1463 bond_dev->mpls_features = mpls_features; 1464 netif_set_gso_max_segs(bond_dev, gso_max_segs); 1465 netif_set_gso_max_size(bond_dev, gso_max_size); 1466 1467 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 1468 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) && 1469 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM)) 1470 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE; 1471 1472 netdev_change_features(bond_dev); 1473 } 1474 1475 static void bond_setup_by_slave(struct net_device *bond_dev, 1476 struct net_device *slave_dev) 1477 { 1478 bond_dev->header_ops = slave_dev->header_ops; 1479 1480 bond_dev->type = slave_dev->type; 1481 bond_dev->hard_header_len = slave_dev->hard_header_len; 1482 bond_dev->needed_headroom = slave_dev->needed_headroom; 1483 bond_dev->addr_len = slave_dev->addr_len; 1484 1485 memcpy(bond_dev->broadcast, slave_dev->broadcast, 1486 slave_dev->addr_len); 1487 } 1488 1489 /* On bonding slaves other than the currently active slave, suppress 1490 * duplicates except for alb non-mcast/bcast. 1491 */ 1492 static bool bond_should_deliver_exact_match(struct sk_buff *skb, 1493 struct slave *slave, 1494 struct bonding *bond) 1495 { 1496 if (bond_is_slave_inactive(slave)) { 1497 if (BOND_MODE(bond) == BOND_MODE_ALB && 1498 skb->pkt_type != PACKET_BROADCAST && 1499 skb->pkt_type != PACKET_MULTICAST) 1500 return false; 1501 return true; 1502 } 1503 return false; 1504 } 1505 1506 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb) 1507 { 1508 struct sk_buff *skb = *pskb; 1509 struct slave *slave; 1510 struct bonding *bond; 1511 int (*recv_probe)(const struct sk_buff *, struct bonding *, 1512 struct slave *); 1513 int ret = RX_HANDLER_ANOTHER; 1514 1515 skb = skb_share_check(skb, GFP_ATOMIC); 1516 if (unlikely(!skb)) 1517 return RX_HANDLER_CONSUMED; 1518 1519 *pskb = skb; 1520 1521 slave = bond_slave_get_rcu(skb->dev); 1522 bond = slave->bond; 1523 1524 recv_probe = READ_ONCE(bond->recv_probe); 1525 if (recv_probe) { 1526 ret = recv_probe(skb, bond, slave); 1527 if (ret == RX_HANDLER_CONSUMED) { 1528 consume_skb(skb); 1529 return ret; 1530 } 1531 } 1532 1533 /* 1534 * For packets determined by bond_should_deliver_exact_match() call to 1535 * be suppressed we want to make an exception for link-local packets. 1536 * This is necessary for e.g. LLDP daemons to be able to monitor 1537 * inactive slave links without being forced to bind to them 1538 * explicitly. 1539 * 1540 * At the same time, packets that are passed to the bonding master 1541 * (including link-local ones) can have their originating interface 1542 * determined via PACKET_ORIGDEV socket option. 1543 */ 1544 if (bond_should_deliver_exact_match(skb, slave, bond)) { 1545 if (is_link_local_ether_addr(eth_hdr(skb)->h_dest)) 1546 return RX_HANDLER_PASS; 1547 return RX_HANDLER_EXACT; 1548 } 1549 1550 skb->dev = bond->dev; 1551 1552 if (BOND_MODE(bond) == BOND_MODE_ALB && 1553 netif_is_bridge_port(bond->dev) && 1554 skb->pkt_type == PACKET_HOST) { 1555 1556 if (unlikely(skb_cow_head(skb, 1557 skb->data - skb_mac_header(skb)))) { 1558 kfree_skb(skb); 1559 return RX_HANDLER_CONSUMED; 1560 } 1561 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, 1562 bond->dev->addr_len); 1563 } 1564 1565 return ret; 1566 } 1567 1568 static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond) 1569 { 1570 switch (BOND_MODE(bond)) { 1571 case BOND_MODE_ROUNDROBIN: 1572 return NETDEV_LAG_TX_TYPE_ROUNDROBIN; 1573 case BOND_MODE_ACTIVEBACKUP: 1574 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP; 1575 case BOND_MODE_BROADCAST: 1576 return NETDEV_LAG_TX_TYPE_BROADCAST; 1577 case BOND_MODE_XOR: 1578 case BOND_MODE_8023AD: 1579 return NETDEV_LAG_TX_TYPE_HASH; 1580 default: 1581 return NETDEV_LAG_TX_TYPE_UNKNOWN; 1582 } 1583 } 1584 1585 static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond, 1586 enum netdev_lag_tx_type type) 1587 { 1588 if (type != NETDEV_LAG_TX_TYPE_HASH) 1589 return NETDEV_LAG_HASH_NONE; 1590 1591 switch (bond->params.xmit_policy) { 1592 case BOND_XMIT_POLICY_LAYER2: 1593 return NETDEV_LAG_HASH_L2; 1594 case BOND_XMIT_POLICY_LAYER34: 1595 return NETDEV_LAG_HASH_L34; 1596 case BOND_XMIT_POLICY_LAYER23: 1597 return NETDEV_LAG_HASH_L23; 1598 case BOND_XMIT_POLICY_ENCAP23: 1599 return NETDEV_LAG_HASH_E23; 1600 case BOND_XMIT_POLICY_ENCAP34: 1601 return NETDEV_LAG_HASH_E34; 1602 case BOND_XMIT_POLICY_VLAN_SRCMAC: 1603 return NETDEV_LAG_HASH_VLAN_SRCMAC; 1604 default: 1605 return NETDEV_LAG_HASH_UNKNOWN; 1606 } 1607 } 1608 1609 static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave, 1610 struct netlink_ext_ack *extack) 1611 { 1612 struct netdev_lag_upper_info lag_upper_info; 1613 enum netdev_lag_tx_type type; 1614 1615 type = bond_lag_tx_type(bond); 1616 lag_upper_info.tx_type = type; 1617 lag_upper_info.hash_type = bond_lag_hash_type(bond, type); 1618 1619 return netdev_master_upper_dev_link(slave->dev, bond->dev, slave, 1620 &lag_upper_info, extack); 1621 } 1622 1623 static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave) 1624 { 1625 netdev_upper_dev_unlink(slave->dev, bond->dev); 1626 slave->dev->flags &= ~IFF_SLAVE; 1627 } 1628 1629 static void slave_kobj_release(struct kobject *kobj) 1630 { 1631 struct slave *slave = to_slave(kobj); 1632 struct bonding *bond = bond_get_bond_by_slave(slave); 1633 1634 cancel_delayed_work_sync(&slave->notify_work); 1635 if (BOND_MODE(bond) == BOND_MODE_8023AD) 1636 kfree(SLAVE_AD_INFO(slave)); 1637 1638 kfree(slave); 1639 } 1640 1641 static struct kobj_type slave_ktype = { 1642 .release = slave_kobj_release, 1643 #ifdef CONFIG_SYSFS 1644 .sysfs_ops = &slave_sysfs_ops, 1645 #endif 1646 }; 1647 1648 static int bond_kobj_init(struct slave *slave) 1649 { 1650 int err; 1651 1652 err = kobject_init_and_add(&slave->kobj, &slave_ktype, 1653 &(slave->dev->dev.kobj), "bonding_slave"); 1654 if (err) 1655 kobject_put(&slave->kobj); 1656 1657 return err; 1658 } 1659 1660 static struct slave *bond_alloc_slave(struct bonding *bond, 1661 struct net_device *slave_dev) 1662 { 1663 struct slave *slave = NULL; 1664 1665 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 1666 if (!slave) 1667 return NULL; 1668 1669 slave->bond = bond; 1670 slave->dev = slave_dev; 1671 INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work); 1672 1673 if (bond_kobj_init(slave)) 1674 return NULL; 1675 1676 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 1677 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info), 1678 GFP_KERNEL); 1679 if (!SLAVE_AD_INFO(slave)) { 1680 kobject_put(&slave->kobj); 1681 return NULL; 1682 } 1683 } 1684 1685 return slave; 1686 } 1687 1688 static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info) 1689 { 1690 info->bond_mode = BOND_MODE(bond); 1691 info->miimon = bond->params.miimon; 1692 info->num_slaves = bond->slave_cnt; 1693 } 1694 1695 static void bond_fill_ifslave(struct slave *slave, struct ifslave *info) 1696 { 1697 strcpy(info->slave_name, slave->dev->name); 1698 info->link = slave->link; 1699 info->state = bond_slave_state(slave); 1700 info->link_failure_count = slave->link_failure_count; 1701 } 1702 1703 static void bond_netdev_notify_work(struct work_struct *_work) 1704 { 1705 struct slave *slave = container_of(_work, struct slave, 1706 notify_work.work); 1707 1708 if (rtnl_trylock()) { 1709 struct netdev_bonding_info binfo; 1710 1711 bond_fill_ifslave(slave, &binfo.slave); 1712 bond_fill_ifbond(slave->bond, &binfo.master); 1713 netdev_bonding_info_change(slave->dev, &binfo); 1714 rtnl_unlock(); 1715 } else { 1716 queue_delayed_work(slave->bond->wq, &slave->notify_work, 1); 1717 } 1718 } 1719 1720 void bond_queue_slave_event(struct slave *slave) 1721 { 1722 queue_delayed_work(slave->bond->wq, &slave->notify_work, 0); 1723 } 1724 1725 void bond_lower_state_changed(struct slave *slave) 1726 { 1727 struct netdev_lag_lower_state_info info; 1728 1729 info.link_up = slave->link == BOND_LINK_UP || 1730 slave->link == BOND_LINK_FAIL; 1731 info.tx_enabled = bond_is_active_slave(slave); 1732 netdev_lower_state_changed(slave->dev, &info); 1733 } 1734 1735 #define BOND_NL_ERR(bond_dev, extack, errmsg) do { \ 1736 if (extack) \ 1737 NL_SET_ERR_MSG(extack, errmsg); \ 1738 else \ 1739 netdev_err(bond_dev, "Error: %s\n", errmsg); \ 1740 } while (0) 1741 1742 #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do { \ 1743 if (extack) \ 1744 NL_SET_ERR_MSG(extack, errmsg); \ 1745 else \ 1746 slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg); \ 1747 } while (0) 1748 1749 /* enslave device <slave> to bond device <master> */ 1750 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, 1751 struct netlink_ext_ack *extack) 1752 { 1753 struct bonding *bond = netdev_priv(bond_dev); 1754 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 1755 struct slave *new_slave = NULL, *prev_slave; 1756 struct sockaddr_storage ss; 1757 int link_reporting; 1758 int res = 0, i; 1759 1760 if (slave_dev->flags & IFF_MASTER && 1761 !netif_is_bond_master(slave_dev)) { 1762 BOND_NL_ERR(bond_dev, extack, 1763 "Device type (master device) cannot be enslaved"); 1764 return -EPERM; 1765 } 1766 1767 if (!bond->params.use_carrier && 1768 slave_dev->ethtool_ops->get_link == NULL && 1769 slave_ops->ndo_eth_ioctl == NULL) { 1770 slave_warn(bond_dev, slave_dev, "no link monitoring support\n"); 1771 } 1772 1773 /* already in-use? */ 1774 if (netdev_is_rx_handler_busy(slave_dev)) { 1775 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1776 "Device is in use and cannot be enslaved"); 1777 return -EBUSY; 1778 } 1779 1780 if (bond_dev == slave_dev) { 1781 BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself."); 1782 return -EPERM; 1783 } 1784 1785 /* vlan challenged mutual exclusion */ 1786 /* no need to lock since we're protected by rtnl_lock */ 1787 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) { 1788 slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n"); 1789 if (vlan_uses_dev(bond_dev)) { 1790 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1791 "Can not enslave VLAN challenged device to VLAN enabled bond"); 1792 return -EPERM; 1793 } else { 1794 slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n"); 1795 } 1796 } else { 1797 slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n"); 1798 } 1799 1800 if (slave_dev->features & NETIF_F_HW_ESP) 1801 slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n"); 1802 1803 /* Old ifenslave binaries are no longer supported. These can 1804 * be identified with moderate accuracy by the state of the slave: 1805 * the current ifenslave will set the interface down prior to 1806 * enslaving it; the old ifenslave will not. 1807 */ 1808 if (slave_dev->flags & IFF_UP) { 1809 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1810 "Device can not be enslaved while up"); 1811 return -EPERM; 1812 } 1813 1814 /* set bonding device ether type by slave - bonding netdevices are 1815 * created with ether_setup, so when the slave type is not ARPHRD_ETHER 1816 * there is a need to override some of the type dependent attribs/funcs. 1817 * 1818 * bond ether type mutual exclusion - don't allow slaves of dissimilar 1819 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond 1820 */ 1821 if (!bond_has_slaves(bond)) { 1822 if (bond_dev->type != slave_dev->type) { 1823 slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n", 1824 bond_dev->type, slave_dev->type); 1825 1826 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, 1827 bond_dev); 1828 res = notifier_to_errno(res); 1829 if (res) { 1830 slave_err(bond_dev, slave_dev, "refused to change device type\n"); 1831 return -EBUSY; 1832 } 1833 1834 /* Flush unicast and multicast addresses */ 1835 dev_uc_flush(bond_dev); 1836 dev_mc_flush(bond_dev); 1837 1838 if (slave_dev->type != ARPHRD_ETHER) 1839 bond_setup_by_slave(bond_dev, slave_dev); 1840 else { 1841 ether_setup(bond_dev); 1842 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1843 } 1844 1845 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, 1846 bond_dev); 1847 } 1848 } else if (bond_dev->type != slave_dev->type) { 1849 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1850 "Device type is different from other slaves"); 1851 return -EINVAL; 1852 } 1853 1854 if (slave_dev->type == ARPHRD_INFINIBAND && 1855 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1856 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1857 "Only active-backup mode is supported for infiniband slaves"); 1858 res = -EOPNOTSUPP; 1859 goto err_undo_flags; 1860 } 1861 1862 if (!slave_ops->ndo_set_mac_address || 1863 slave_dev->type == ARPHRD_INFINIBAND) { 1864 slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n"); 1865 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP && 1866 bond->params.fail_over_mac != BOND_FOM_ACTIVE) { 1867 if (!bond_has_slaves(bond)) { 1868 bond->params.fail_over_mac = BOND_FOM_ACTIVE; 1869 slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n"); 1870 } else { 1871 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 1872 "Slave device does not support setting the MAC address, but fail_over_mac is not set to active"); 1873 res = -EOPNOTSUPP; 1874 goto err_undo_flags; 1875 } 1876 } 1877 } 1878 1879 call_netdevice_notifiers(NETDEV_JOIN, slave_dev); 1880 1881 /* If this is the first slave, then we need to set the master's hardware 1882 * address to be the same as the slave's. 1883 */ 1884 if (!bond_has_slaves(bond) && 1885 bond->dev->addr_assign_type == NET_ADDR_RANDOM) { 1886 res = bond_set_dev_addr(bond->dev, slave_dev); 1887 if (res) 1888 goto err_undo_flags; 1889 } 1890 1891 new_slave = bond_alloc_slave(bond, slave_dev); 1892 if (!new_slave) { 1893 res = -ENOMEM; 1894 goto err_undo_flags; 1895 } 1896 1897 /* Set the new_slave's queue_id to be zero. Queue ID mapping 1898 * is set via sysfs or module option if desired. 1899 */ 1900 new_slave->queue_id = 0; 1901 1902 /* Save slave's original mtu and then set it to match the bond */ 1903 new_slave->original_mtu = slave_dev->mtu; 1904 res = dev_set_mtu(slave_dev, bond->dev->mtu); 1905 if (res) { 1906 slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res); 1907 goto err_free; 1908 } 1909 1910 /* Save slave's original ("permanent") mac address for modes 1911 * that need it, and for restoring it upon release, and then 1912 * set it to the master's address 1913 */ 1914 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr, 1915 slave_dev->addr_len); 1916 1917 if (!bond->params.fail_over_mac || 1918 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1919 /* Set slave to master's mac address. The application already 1920 * set the master's mac address to that of the first slave 1921 */ 1922 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len); 1923 ss.ss_family = slave_dev->type; 1924 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, 1925 extack); 1926 if (res) { 1927 slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res); 1928 goto err_restore_mtu; 1929 } 1930 } 1931 1932 /* set slave flag before open to prevent IPv6 addrconf */ 1933 slave_dev->flags |= IFF_SLAVE; 1934 1935 /* open the slave since the application closed it */ 1936 res = dev_open(slave_dev, extack); 1937 if (res) { 1938 slave_err(bond_dev, slave_dev, "Opening slave failed\n"); 1939 goto err_restore_mac; 1940 } 1941 1942 slave_dev->priv_flags |= IFF_BONDING; 1943 /* initialize slave stats */ 1944 dev_get_stats(new_slave->dev, &new_slave->slave_stats); 1945 1946 if (bond_is_lb(bond)) { 1947 /* bond_alb_init_slave() must be called before all other stages since 1948 * it might fail and we do not want to have to undo everything 1949 */ 1950 res = bond_alb_init_slave(bond, new_slave); 1951 if (res) 1952 goto err_close; 1953 } 1954 1955 res = vlan_vids_add_by_dev(slave_dev, bond_dev); 1956 if (res) { 1957 slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n"); 1958 goto err_close; 1959 } 1960 1961 prev_slave = bond_last_slave(bond); 1962 1963 new_slave->delay = 0; 1964 new_slave->link_failure_count = 0; 1965 1966 if (bond_update_speed_duplex(new_slave) && 1967 bond_needs_speed_duplex(bond)) 1968 new_slave->link = BOND_LINK_DOWN; 1969 1970 new_slave->last_rx = jiffies - 1971 (msecs_to_jiffies(bond->params.arp_interval) + 1); 1972 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 1973 new_slave->target_last_arp_rx[i] = new_slave->last_rx; 1974 1975 if (bond->params.miimon && !bond->params.use_carrier) { 1976 link_reporting = bond_check_dev_link(bond, slave_dev, 1); 1977 1978 if ((link_reporting == -1) && !bond->params.arp_interval) { 1979 /* miimon is set but a bonded network driver 1980 * does not support ETHTOOL/MII and 1981 * arp_interval is not set. Note: if 1982 * use_carrier is enabled, we will never go 1983 * here (because netif_carrier is always 1984 * supported); thus, we don't need to change 1985 * the messages for netif_carrier. 1986 */ 1987 slave_warn(bond_dev, slave_dev, "MII and ETHTOOL support not available for slave, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details\n"); 1988 } else if (link_reporting == -1) { 1989 /* unable get link status using mii/ethtool */ 1990 slave_warn(bond_dev, slave_dev, "can't get link status from slave; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface\n"); 1991 } 1992 } 1993 1994 /* check for initial state */ 1995 new_slave->link = BOND_LINK_NOCHANGE; 1996 if (bond->params.miimon) { 1997 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) { 1998 if (bond->params.updelay) { 1999 bond_set_slave_link_state(new_slave, 2000 BOND_LINK_BACK, 2001 BOND_SLAVE_NOTIFY_NOW); 2002 new_slave->delay = bond->params.updelay; 2003 } else { 2004 bond_set_slave_link_state(new_slave, 2005 BOND_LINK_UP, 2006 BOND_SLAVE_NOTIFY_NOW); 2007 } 2008 } else { 2009 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN, 2010 BOND_SLAVE_NOTIFY_NOW); 2011 } 2012 } else if (bond->params.arp_interval) { 2013 bond_set_slave_link_state(new_slave, 2014 (netif_carrier_ok(slave_dev) ? 2015 BOND_LINK_UP : BOND_LINK_DOWN), 2016 BOND_SLAVE_NOTIFY_NOW); 2017 } else { 2018 bond_set_slave_link_state(new_slave, BOND_LINK_UP, 2019 BOND_SLAVE_NOTIFY_NOW); 2020 } 2021 2022 if (new_slave->link != BOND_LINK_DOWN) 2023 new_slave->last_link_up = jiffies; 2024 slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n", 2025 new_slave->link == BOND_LINK_DOWN ? "DOWN" : 2026 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK")); 2027 2028 if (bond_uses_primary(bond) && bond->params.primary[0]) { 2029 /* if there is a primary slave, remember it */ 2030 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) { 2031 rcu_assign_pointer(bond->primary_slave, new_slave); 2032 bond->force_primary = true; 2033 } 2034 } 2035 2036 switch (BOND_MODE(bond)) { 2037 case BOND_MODE_ACTIVEBACKUP: 2038 bond_set_slave_inactive_flags(new_slave, 2039 BOND_SLAVE_NOTIFY_NOW); 2040 break; 2041 case BOND_MODE_8023AD: 2042 /* in 802.3ad mode, the internal mechanism 2043 * will activate the slaves in the selected 2044 * aggregator 2045 */ 2046 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); 2047 /* if this is the first slave */ 2048 if (!prev_slave) { 2049 SLAVE_AD_INFO(new_slave)->id = 1; 2050 /* Initialize AD with the number of times that the AD timer is called in 1 second 2051 * can be called only after the mac address of the bond is set 2052 */ 2053 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL); 2054 } else { 2055 SLAVE_AD_INFO(new_slave)->id = 2056 SLAVE_AD_INFO(prev_slave)->id + 1; 2057 } 2058 2059 bond_3ad_bind_slave(new_slave); 2060 break; 2061 case BOND_MODE_TLB: 2062 case BOND_MODE_ALB: 2063 bond_set_active_slave(new_slave); 2064 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); 2065 break; 2066 default: 2067 slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n"); 2068 2069 /* always active in trunk mode */ 2070 bond_set_active_slave(new_slave); 2071 2072 /* In trunking mode there is little meaning to curr_active_slave 2073 * anyway (it holds no special properties of the bond device), 2074 * so we can change it without calling change_active_interface() 2075 */ 2076 if (!rcu_access_pointer(bond->curr_active_slave) && 2077 new_slave->link == BOND_LINK_UP) 2078 rcu_assign_pointer(bond->curr_active_slave, new_slave); 2079 2080 break; 2081 } /* switch(bond_mode) */ 2082 2083 #ifdef CONFIG_NET_POLL_CONTROLLER 2084 if (bond->dev->npinfo) { 2085 if (slave_enable_netpoll(new_slave)) { 2086 slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n"); 2087 res = -EBUSY; 2088 goto err_detach; 2089 } 2090 } 2091 #endif 2092 2093 if (!(bond_dev->features & NETIF_F_LRO)) 2094 dev_disable_lro(slave_dev); 2095 2096 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, 2097 new_slave); 2098 if (res) { 2099 slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res); 2100 goto err_detach; 2101 } 2102 2103 res = bond_master_upper_dev_link(bond, new_slave, extack); 2104 if (res) { 2105 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res); 2106 goto err_unregister; 2107 } 2108 2109 bond_lower_state_changed(new_slave); 2110 2111 res = bond_sysfs_slave_add(new_slave); 2112 if (res) { 2113 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res); 2114 goto err_upper_unlink; 2115 } 2116 2117 /* If the mode uses primary, then the following is handled by 2118 * bond_change_active_slave(). 2119 */ 2120 if (!bond_uses_primary(bond)) { 2121 /* set promiscuity level to new slave */ 2122 if (bond_dev->flags & IFF_PROMISC) { 2123 res = dev_set_promiscuity(slave_dev, 1); 2124 if (res) 2125 goto err_sysfs_del; 2126 } 2127 2128 /* set allmulti level to new slave */ 2129 if (bond_dev->flags & IFF_ALLMULTI) { 2130 res = dev_set_allmulti(slave_dev, 1); 2131 if (res) { 2132 if (bond_dev->flags & IFF_PROMISC) 2133 dev_set_promiscuity(slave_dev, -1); 2134 goto err_sysfs_del; 2135 } 2136 } 2137 2138 netif_addr_lock_bh(bond_dev); 2139 dev_mc_sync_multiple(slave_dev, bond_dev); 2140 dev_uc_sync_multiple(slave_dev, bond_dev); 2141 netif_addr_unlock_bh(bond_dev); 2142 2143 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 2144 /* add lacpdu mc addr to mc list */ 2145 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR; 2146 2147 dev_mc_add(slave_dev, lacpdu_multicast); 2148 } 2149 } 2150 2151 bond->slave_cnt++; 2152 bond_compute_features(bond); 2153 bond_set_carrier(bond); 2154 2155 if (bond_uses_primary(bond)) { 2156 block_netpoll_tx(); 2157 bond_select_active_slave(bond); 2158 unblock_netpoll_tx(); 2159 } 2160 2161 if (bond_mode_can_use_xmit_hash(bond)) 2162 bond_update_slave_arr(bond, NULL); 2163 2164 2165 if (!slave_dev->netdev_ops->ndo_bpf || 2166 !slave_dev->netdev_ops->ndo_xdp_xmit) { 2167 if (bond->xdp_prog) { 2168 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 2169 "Slave does not support XDP"); 2170 res = -EOPNOTSUPP; 2171 goto err_sysfs_del; 2172 } 2173 } else if (bond->xdp_prog) { 2174 struct netdev_bpf xdp = { 2175 .command = XDP_SETUP_PROG, 2176 .flags = 0, 2177 .prog = bond->xdp_prog, 2178 .extack = extack, 2179 }; 2180 2181 if (dev_xdp_prog_count(slave_dev) > 0) { 2182 SLAVE_NL_ERR(bond_dev, slave_dev, extack, 2183 "Slave has XDP program loaded, please unload before enslaving"); 2184 res = -EOPNOTSUPP; 2185 goto err_sysfs_del; 2186 } 2187 2188 res = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); 2189 if (res < 0) { 2190 /* ndo_bpf() sets extack error message */ 2191 slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res); 2192 goto err_sysfs_del; 2193 } 2194 if (bond->xdp_prog) 2195 bpf_prog_inc(bond->xdp_prog); 2196 } 2197 2198 slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n", 2199 bond_is_active_slave(new_slave) ? "an active" : "a backup", 2200 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down"); 2201 2202 /* enslave is successful */ 2203 bond_queue_slave_event(new_slave); 2204 return 0; 2205 2206 /* Undo stages on error */ 2207 err_sysfs_del: 2208 bond_sysfs_slave_del(new_slave); 2209 2210 err_upper_unlink: 2211 bond_upper_dev_unlink(bond, new_slave); 2212 2213 err_unregister: 2214 netdev_rx_handler_unregister(slave_dev); 2215 2216 err_detach: 2217 vlan_vids_del_by_dev(slave_dev, bond_dev); 2218 if (rcu_access_pointer(bond->primary_slave) == new_slave) 2219 RCU_INIT_POINTER(bond->primary_slave, NULL); 2220 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) { 2221 block_netpoll_tx(); 2222 bond_change_active_slave(bond, NULL); 2223 bond_select_active_slave(bond); 2224 unblock_netpoll_tx(); 2225 } 2226 /* either primary_slave or curr_active_slave might've changed */ 2227 synchronize_rcu(); 2228 slave_disable_netpoll(new_slave); 2229 2230 err_close: 2231 if (!netif_is_bond_master(slave_dev)) 2232 slave_dev->priv_flags &= ~IFF_BONDING; 2233 dev_close(slave_dev); 2234 2235 err_restore_mac: 2236 slave_dev->flags &= ~IFF_SLAVE; 2237 if (!bond->params.fail_over_mac || 2238 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 2239 /* XXX TODO - fom follow mode needs to change master's 2240 * MAC if this slave's MAC is in use by the bond, or at 2241 * least print a warning. 2242 */ 2243 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr, 2244 new_slave->dev->addr_len); 2245 ss.ss_family = slave_dev->type; 2246 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL); 2247 } 2248 2249 err_restore_mtu: 2250 dev_set_mtu(slave_dev, new_slave->original_mtu); 2251 2252 err_free: 2253 kobject_put(&new_slave->kobj); 2254 2255 err_undo_flags: 2256 /* Enslave of first slave has failed and we need to fix master's mac */ 2257 if (!bond_has_slaves(bond)) { 2258 if (ether_addr_equal_64bits(bond_dev->dev_addr, 2259 slave_dev->dev_addr)) 2260 eth_hw_addr_random(bond_dev); 2261 if (bond_dev->type != ARPHRD_ETHER) { 2262 dev_close(bond_dev); 2263 ether_setup(bond_dev); 2264 bond_dev->flags |= IFF_MASTER; 2265 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; 2266 } 2267 } 2268 2269 return res; 2270 } 2271 2272 /* Try to release the slave device <slave> from the bond device <master> 2273 * It is legal to access curr_active_slave without a lock because all the function 2274 * is RTNL-locked. If "all" is true it means that the function is being called 2275 * while destroying a bond interface and all slaves are being released. 2276 * 2277 * The rules for slave state should be: 2278 * for Active/Backup: 2279 * Active stays on all backups go down 2280 * for Bonded connections: 2281 * The first up interface should be left on and all others downed. 2282 */ 2283 static int __bond_release_one(struct net_device *bond_dev, 2284 struct net_device *slave_dev, 2285 bool all, bool unregister) 2286 { 2287 struct bonding *bond = netdev_priv(bond_dev); 2288 struct slave *slave, *oldcurrent; 2289 struct sockaddr_storage ss; 2290 int old_flags = bond_dev->flags; 2291 netdev_features_t old_features = bond_dev->features; 2292 2293 /* slave is not a slave or master is not master of this slave */ 2294 if (!(slave_dev->flags & IFF_SLAVE) || 2295 !netdev_has_upper_dev(slave_dev, bond_dev)) { 2296 slave_dbg(bond_dev, slave_dev, "cannot release slave\n"); 2297 return -EINVAL; 2298 } 2299 2300 block_netpoll_tx(); 2301 2302 slave = bond_get_slave_by_dev(bond, slave_dev); 2303 if (!slave) { 2304 /* not a slave of this bond */ 2305 slave_info(bond_dev, slave_dev, "interface not enslaved\n"); 2306 unblock_netpoll_tx(); 2307 return -EINVAL; 2308 } 2309 2310 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW); 2311 2312 bond_sysfs_slave_del(slave); 2313 2314 /* recompute stats just before removing the slave */ 2315 bond_get_stats(bond->dev, &bond->bond_stats); 2316 2317 if (bond->xdp_prog) { 2318 struct netdev_bpf xdp = { 2319 .command = XDP_SETUP_PROG, 2320 .flags = 0, 2321 .prog = NULL, 2322 .extack = NULL, 2323 }; 2324 if (slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp)) 2325 slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n"); 2326 } 2327 2328 /* unregister rx_handler early so bond_handle_frame wouldn't be called 2329 * for this slave anymore. 2330 */ 2331 netdev_rx_handler_unregister(slave_dev); 2332 2333 if (BOND_MODE(bond) == BOND_MODE_8023AD) 2334 bond_3ad_unbind_slave(slave); 2335 2336 bond_upper_dev_unlink(bond, slave); 2337 2338 if (bond_mode_can_use_xmit_hash(bond)) 2339 bond_update_slave_arr(bond, slave); 2340 2341 slave_info(bond_dev, slave_dev, "Releasing %s interface\n", 2342 bond_is_active_slave(slave) ? "active" : "backup"); 2343 2344 oldcurrent = rcu_access_pointer(bond->curr_active_slave); 2345 2346 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 2347 2348 if (!all && (!bond->params.fail_over_mac || 2349 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) { 2350 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) && 2351 bond_has_slaves(bond)) 2352 slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n", 2353 slave->perm_hwaddr); 2354 } 2355 2356 if (rtnl_dereference(bond->primary_slave) == slave) 2357 RCU_INIT_POINTER(bond->primary_slave, NULL); 2358 2359 if (oldcurrent == slave) 2360 bond_change_active_slave(bond, NULL); 2361 2362 if (bond_is_lb(bond)) { 2363 /* Must be called only after the slave has been 2364 * detached from the list and the curr_active_slave 2365 * has been cleared (if our_slave == old_current), 2366 * but before a new active slave is selected. 2367 */ 2368 bond_alb_deinit_slave(bond, slave); 2369 } 2370 2371 if (all) { 2372 RCU_INIT_POINTER(bond->curr_active_slave, NULL); 2373 } else if (oldcurrent == slave) { 2374 /* Note that we hold RTNL over this sequence, so there 2375 * is no concern that another slave add/remove event 2376 * will interfere. 2377 */ 2378 bond_select_active_slave(bond); 2379 } 2380 2381 if (!bond_has_slaves(bond)) { 2382 bond_set_carrier(bond); 2383 eth_hw_addr_random(bond_dev); 2384 } 2385 2386 unblock_netpoll_tx(); 2387 synchronize_rcu(); 2388 bond->slave_cnt--; 2389 2390 if (!bond_has_slaves(bond)) { 2391 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev); 2392 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev); 2393 } 2394 2395 bond_compute_features(bond); 2396 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) && 2397 (old_features & NETIF_F_VLAN_CHALLENGED)) 2398 slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n"); 2399 2400 vlan_vids_del_by_dev(slave_dev, bond_dev); 2401 2402 /* If the mode uses primary, then this case was handled above by 2403 * bond_change_active_slave(..., NULL) 2404 */ 2405 if (!bond_uses_primary(bond)) { 2406 /* unset promiscuity level from slave 2407 * NOTE: The NETDEV_CHANGEADDR call above may change the value 2408 * of the IFF_PROMISC flag in the bond_dev, but we need the 2409 * value of that flag before that change, as that was the value 2410 * when this slave was attached, so we cache at the start of the 2411 * function and use it here. Same goes for ALLMULTI below 2412 */ 2413 if (old_flags & IFF_PROMISC) 2414 dev_set_promiscuity(slave_dev, -1); 2415 2416 /* unset allmulti level from slave */ 2417 if (old_flags & IFF_ALLMULTI) 2418 dev_set_allmulti(slave_dev, -1); 2419 2420 bond_hw_addr_flush(bond_dev, slave_dev); 2421 } 2422 2423 slave_disable_netpoll(slave); 2424 2425 /* close slave before restoring its mac address */ 2426 dev_close(slave_dev); 2427 2428 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE || 2429 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 2430 /* restore original ("permanent") mac address */ 2431 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr, 2432 slave->dev->addr_len); 2433 ss.ss_family = slave_dev->type; 2434 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL); 2435 } 2436 2437 if (unregister) 2438 __dev_set_mtu(slave_dev, slave->original_mtu); 2439 else 2440 dev_set_mtu(slave_dev, slave->original_mtu); 2441 2442 if (!netif_is_bond_master(slave_dev)) 2443 slave_dev->priv_flags &= ~IFF_BONDING; 2444 2445 kobject_put(&slave->kobj); 2446 2447 return 0; 2448 } 2449 2450 /* A wrapper used because of ndo_del_link */ 2451 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev) 2452 { 2453 return __bond_release_one(bond_dev, slave_dev, false, false); 2454 } 2455 2456 /* First release a slave and then destroy the bond if no more slaves are left. 2457 * Must be under rtnl_lock when this function is called. 2458 */ 2459 static int bond_release_and_destroy(struct net_device *bond_dev, 2460 struct net_device *slave_dev) 2461 { 2462 struct bonding *bond = netdev_priv(bond_dev); 2463 int ret; 2464 2465 ret = __bond_release_one(bond_dev, slave_dev, false, true); 2466 if (ret == 0 && !bond_has_slaves(bond) && 2467 bond_dev->reg_state != NETREG_UNREGISTERING) { 2468 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL; 2469 netdev_info(bond_dev, "Destroying bond\n"); 2470 bond_remove_proc_entry(bond); 2471 unregister_netdevice(bond_dev); 2472 } 2473 return ret; 2474 } 2475 2476 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info) 2477 { 2478 struct bonding *bond = netdev_priv(bond_dev); 2479 2480 bond_fill_ifbond(bond, info); 2481 } 2482 2483 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info) 2484 { 2485 struct bonding *bond = netdev_priv(bond_dev); 2486 struct list_head *iter; 2487 int i = 0, res = -ENODEV; 2488 struct slave *slave; 2489 2490 bond_for_each_slave(bond, slave, iter) { 2491 if (i++ == (int)info->slave_id) { 2492 res = 0; 2493 bond_fill_ifslave(slave, info); 2494 break; 2495 } 2496 } 2497 2498 return res; 2499 } 2500 2501 /*-------------------------------- Monitoring -------------------------------*/ 2502 2503 /* called with rcu_read_lock() */ 2504 static int bond_miimon_inspect(struct bonding *bond) 2505 { 2506 int link_state, commit = 0; 2507 struct list_head *iter; 2508 struct slave *slave; 2509 bool ignore_updelay; 2510 2511 ignore_updelay = !rcu_dereference(bond->curr_active_slave); 2512 2513 bond_for_each_slave_rcu(bond, slave, iter) { 2514 bond_propose_link_state(slave, BOND_LINK_NOCHANGE); 2515 2516 link_state = bond_check_dev_link(bond, slave->dev, 0); 2517 2518 switch (slave->link) { 2519 case BOND_LINK_UP: 2520 if (link_state) 2521 continue; 2522 2523 bond_propose_link_state(slave, BOND_LINK_FAIL); 2524 commit++; 2525 slave->delay = bond->params.downdelay; 2526 if (slave->delay) { 2527 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n", 2528 (BOND_MODE(bond) == 2529 BOND_MODE_ACTIVEBACKUP) ? 2530 (bond_is_active_slave(slave) ? 2531 "active " : "backup ") : "", 2532 bond->params.downdelay * bond->params.miimon); 2533 } 2534 fallthrough; 2535 case BOND_LINK_FAIL: 2536 if (link_state) { 2537 /* recovered before downdelay expired */ 2538 bond_propose_link_state(slave, BOND_LINK_UP); 2539 slave->last_link_up = jiffies; 2540 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n", 2541 (bond->params.downdelay - slave->delay) * 2542 bond->params.miimon); 2543 commit++; 2544 continue; 2545 } 2546 2547 if (slave->delay <= 0) { 2548 bond_propose_link_state(slave, BOND_LINK_DOWN); 2549 commit++; 2550 continue; 2551 } 2552 2553 slave->delay--; 2554 break; 2555 2556 case BOND_LINK_DOWN: 2557 if (!link_state) 2558 continue; 2559 2560 bond_propose_link_state(slave, BOND_LINK_BACK); 2561 commit++; 2562 slave->delay = bond->params.updelay; 2563 2564 if (slave->delay) { 2565 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n", 2566 ignore_updelay ? 0 : 2567 bond->params.updelay * 2568 bond->params.miimon); 2569 } 2570 fallthrough; 2571 case BOND_LINK_BACK: 2572 if (!link_state) { 2573 bond_propose_link_state(slave, BOND_LINK_DOWN); 2574 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n", 2575 (bond->params.updelay - slave->delay) * 2576 bond->params.miimon); 2577 commit++; 2578 continue; 2579 } 2580 2581 if (ignore_updelay) 2582 slave->delay = 0; 2583 2584 if (slave->delay <= 0) { 2585 bond_propose_link_state(slave, BOND_LINK_UP); 2586 commit++; 2587 ignore_updelay = false; 2588 continue; 2589 } 2590 2591 slave->delay--; 2592 break; 2593 } 2594 } 2595 2596 return commit; 2597 } 2598 2599 static void bond_miimon_link_change(struct bonding *bond, 2600 struct slave *slave, 2601 char link) 2602 { 2603 switch (BOND_MODE(bond)) { 2604 case BOND_MODE_8023AD: 2605 bond_3ad_handle_link_change(slave, link); 2606 break; 2607 case BOND_MODE_TLB: 2608 case BOND_MODE_ALB: 2609 bond_alb_handle_link_change(bond, slave, link); 2610 break; 2611 case BOND_MODE_XOR: 2612 bond_update_slave_arr(bond, NULL); 2613 break; 2614 } 2615 } 2616 2617 static void bond_miimon_commit(struct bonding *bond) 2618 { 2619 struct list_head *iter; 2620 struct slave *slave, *primary; 2621 2622 bond_for_each_slave(bond, slave, iter) { 2623 switch (slave->link_new_state) { 2624 case BOND_LINK_NOCHANGE: 2625 /* For 802.3ad mode, check current slave speed and 2626 * duplex again in case its port was disabled after 2627 * invalid speed/duplex reporting but recovered before 2628 * link monitoring could make a decision on the actual 2629 * link status 2630 */ 2631 if (BOND_MODE(bond) == BOND_MODE_8023AD && 2632 slave->link == BOND_LINK_UP) 2633 bond_3ad_adapter_speed_duplex_changed(slave); 2634 continue; 2635 2636 case BOND_LINK_UP: 2637 if (bond_update_speed_duplex(slave) && 2638 bond_needs_speed_duplex(bond)) { 2639 slave->link = BOND_LINK_DOWN; 2640 if (net_ratelimit()) 2641 slave_warn(bond->dev, slave->dev, 2642 "failed to get link speed/duplex\n"); 2643 continue; 2644 } 2645 bond_set_slave_link_state(slave, BOND_LINK_UP, 2646 BOND_SLAVE_NOTIFY_NOW); 2647 slave->last_link_up = jiffies; 2648 2649 primary = rtnl_dereference(bond->primary_slave); 2650 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 2651 /* prevent it from being the active one */ 2652 bond_set_backup_slave(slave); 2653 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 2654 /* make it immediately active */ 2655 bond_set_active_slave(slave); 2656 } 2657 2658 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n", 2659 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed, 2660 slave->duplex ? "full" : "half"); 2661 2662 bond_miimon_link_change(bond, slave, BOND_LINK_UP); 2663 2664 if (!bond->curr_active_slave || slave == primary) 2665 goto do_failover; 2666 2667 continue; 2668 2669 case BOND_LINK_DOWN: 2670 if (slave->link_failure_count < UINT_MAX) 2671 slave->link_failure_count++; 2672 2673 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 2674 BOND_SLAVE_NOTIFY_NOW); 2675 2676 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP || 2677 BOND_MODE(bond) == BOND_MODE_8023AD) 2678 bond_set_slave_inactive_flags(slave, 2679 BOND_SLAVE_NOTIFY_NOW); 2680 2681 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n"); 2682 2683 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN); 2684 2685 if (slave == rcu_access_pointer(bond->curr_active_slave)) 2686 goto do_failover; 2687 2688 continue; 2689 2690 default: 2691 slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n", 2692 slave->link_new_state); 2693 bond_propose_link_state(slave, BOND_LINK_NOCHANGE); 2694 2695 continue; 2696 } 2697 2698 do_failover: 2699 block_netpoll_tx(); 2700 bond_select_active_slave(bond); 2701 unblock_netpoll_tx(); 2702 } 2703 2704 bond_set_carrier(bond); 2705 } 2706 2707 /* bond_mii_monitor 2708 * 2709 * Really a wrapper that splits the mii monitor into two phases: an 2710 * inspection, then (if inspection indicates something needs to be done) 2711 * an acquisition of appropriate locks followed by a commit phase to 2712 * implement whatever link state changes are indicated. 2713 */ 2714 static void bond_mii_monitor(struct work_struct *work) 2715 { 2716 struct bonding *bond = container_of(work, struct bonding, 2717 mii_work.work); 2718 bool should_notify_peers = false; 2719 bool commit; 2720 unsigned long delay; 2721 struct slave *slave; 2722 struct list_head *iter; 2723 2724 delay = msecs_to_jiffies(bond->params.miimon); 2725 2726 if (!bond_has_slaves(bond)) 2727 goto re_arm; 2728 2729 rcu_read_lock(); 2730 should_notify_peers = bond_should_notify_peers(bond); 2731 commit = !!bond_miimon_inspect(bond); 2732 if (bond->send_peer_notif) { 2733 rcu_read_unlock(); 2734 if (rtnl_trylock()) { 2735 bond->send_peer_notif--; 2736 rtnl_unlock(); 2737 } 2738 } else { 2739 rcu_read_unlock(); 2740 } 2741 2742 if (commit) { 2743 /* Race avoidance with bond_close cancel of workqueue */ 2744 if (!rtnl_trylock()) { 2745 delay = 1; 2746 should_notify_peers = false; 2747 goto re_arm; 2748 } 2749 2750 bond_for_each_slave(bond, slave, iter) { 2751 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER); 2752 } 2753 bond_miimon_commit(bond); 2754 2755 rtnl_unlock(); /* might sleep, hold no other locks */ 2756 } 2757 2758 re_arm: 2759 if (bond->params.miimon) 2760 queue_delayed_work(bond->wq, &bond->mii_work, delay); 2761 2762 if (should_notify_peers) { 2763 if (!rtnl_trylock()) 2764 return; 2765 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); 2766 rtnl_unlock(); 2767 } 2768 } 2769 2770 static int bond_upper_dev_walk(struct net_device *upper, 2771 struct netdev_nested_priv *priv) 2772 { 2773 __be32 ip = *(__be32 *)priv->data; 2774 2775 return ip == bond_confirm_addr(upper, 0, ip); 2776 } 2777 2778 static bool bond_has_this_ip(struct bonding *bond, __be32 ip) 2779 { 2780 struct netdev_nested_priv priv = { 2781 .data = (void *)&ip, 2782 }; 2783 bool ret = false; 2784 2785 if (ip == bond_confirm_addr(bond->dev, 0, ip)) 2786 return true; 2787 2788 rcu_read_lock(); 2789 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv)) 2790 ret = true; 2791 rcu_read_unlock(); 2792 2793 return ret; 2794 } 2795 2796 /* We go to the (large) trouble of VLAN tagging ARP frames because 2797 * switches in VLAN mode (especially if ports are configured as 2798 * "native" to a VLAN) might not pass non-tagged frames. 2799 */ 2800 static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip, 2801 __be32 src_ip, struct bond_vlan_tag *tags) 2802 { 2803 struct sk_buff *skb; 2804 struct bond_vlan_tag *outer_tag = tags; 2805 struct net_device *slave_dev = slave->dev; 2806 struct net_device *bond_dev = slave->bond->dev; 2807 2808 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n", 2809 arp_op, &dest_ip, &src_ip); 2810 2811 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip, 2812 NULL, slave_dev->dev_addr, NULL); 2813 2814 if (!skb) { 2815 net_err_ratelimited("ARP packet allocation failed\n"); 2816 return; 2817 } 2818 2819 if (!tags || tags->vlan_proto == VLAN_N_VID) 2820 goto xmit; 2821 2822 tags++; 2823 2824 /* Go through all the tags backwards and add them to the packet */ 2825 while (tags->vlan_proto != VLAN_N_VID) { 2826 if (!tags->vlan_id) { 2827 tags++; 2828 continue; 2829 } 2830 2831 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n", 2832 ntohs(outer_tag->vlan_proto), tags->vlan_id); 2833 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto, 2834 tags->vlan_id); 2835 if (!skb) { 2836 net_err_ratelimited("failed to insert inner VLAN tag\n"); 2837 return; 2838 } 2839 2840 tags++; 2841 } 2842 /* Set the outer tag */ 2843 if (outer_tag->vlan_id) { 2844 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n", 2845 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id); 2846 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto, 2847 outer_tag->vlan_id); 2848 } 2849 2850 xmit: 2851 arp_xmit(skb); 2852 } 2853 2854 /* Validate the device path between the @start_dev and the @end_dev. 2855 * The path is valid if the @end_dev is reachable through device 2856 * stacking. 2857 * When the path is validated, collect any vlan information in the 2858 * path. 2859 */ 2860 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev, 2861 struct net_device *end_dev, 2862 int level) 2863 { 2864 struct bond_vlan_tag *tags; 2865 struct net_device *upper; 2866 struct list_head *iter; 2867 2868 if (start_dev == end_dev) { 2869 tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC); 2870 if (!tags) 2871 return ERR_PTR(-ENOMEM); 2872 tags[level].vlan_proto = VLAN_N_VID; 2873 return tags; 2874 } 2875 2876 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) { 2877 tags = bond_verify_device_path(upper, end_dev, level + 1); 2878 if (IS_ERR_OR_NULL(tags)) { 2879 if (IS_ERR(tags)) 2880 return tags; 2881 continue; 2882 } 2883 if (is_vlan_dev(upper)) { 2884 tags[level].vlan_proto = vlan_dev_vlan_proto(upper); 2885 tags[level].vlan_id = vlan_dev_vlan_id(upper); 2886 } 2887 2888 return tags; 2889 } 2890 2891 return NULL; 2892 } 2893 2894 static void bond_arp_send_all(struct bonding *bond, struct slave *slave) 2895 { 2896 struct rtable *rt; 2897 struct bond_vlan_tag *tags; 2898 __be32 *targets = bond->params.arp_targets, addr; 2899 int i; 2900 2901 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) { 2902 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n", 2903 __func__, &targets[i]); 2904 tags = NULL; 2905 2906 /* Find out through which dev should the packet go */ 2907 rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 2908 RTO_ONLINK, 0); 2909 if (IS_ERR(rt)) { 2910 /* there's no route to target - try to send arp 2911 * probe to generate any traffic (arp_validate=0) 2912 */ 2913 if (bond->params.arp_validate) 2914 pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n", 2915 bond->dev->name, 2916 &targets[i]); 2917 bond_arp_send(slave, ARPOP_REQUEST, targets[i], 2918 0, tags); 2919 continue; 2920 } 2921 2922 /* bond device itself */ 2923 if (rt->dst.dev == bond->dev) 2924 goto found; 2925 2926 rcu_read_lock(); 2927 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0); 2928 rcu_read_unlock(); 2929 2930 if (!IS_ERR_OR_NULL(tags)) 2931 goto found; 2932 2933 /* Not our device - skip */ 2934 slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n", 2935 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL"); 2936 2937 ip_rt_put(rt); 2938 continue; 2939 2940 found: 2941 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0); 2942 ip_rt_put(rt); 2943 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags); 2944 kfree(tags); 2945 } 2946 } 2947 2948 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip) 2949 { 2950 int i; 2951 2952 if (!sip || !bond_has_this_ip(bond, tip)) { 2953 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n", 2954 __func__, &sip, &tip); 2955 return; 2956 } 2957 2958 i = bond_get_targets_ip(bond->params.arp_targets, sip); 2959 if (i == -1) { 2960 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n", 2961 __func__, &sip); 2962 return; 2963 } 2964 slave->last_rx = jiffies; 2965 slave->target_last_arp_rx[i] = jiffies; 2966 } 2967 2968 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, 2969 struct slave *slave) 2970 { 2971 struct arphdr *arp = (struct arphdr *)skb->data; 2972 struct slave *curr_active_slave, *curr_arp_slave; 2973 unsigned char *arp_ptr; 2974 __be32 sip, tip; 2975 int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP); 2976 unsigned int alen; 2977 2978 if (!slave_do_arp_validate(bond, slave)) { 2979 if ((slave_do_arp_validate_only(bond) && is_arp) || 2980 !slave_do_arp_validate_only(bond)) 2981 slave->last_rx = jiffies; 2982 return RX_HANDLER_ANOTHER; 2983 } else if (!is_arp) { 2984 return RX_HANDLER_ANOTHER; 2985 } 2986 2987 alen = arp_hdr_len(bond->dev); 2988 2989 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n", 2990 __func__, skb->dev->name); 2991 2992 if (alen > skb_headlen(skb)) { 2993 arp = kmalloc(alen, GFP_ATOMIC); 2994 if (!arp) 2995 goto out_unlock; 2996 if (skb_copy_bits(skb, 0, arp, alen) < 0) 2997 goto out_unlock; 2998 } 2999 3000 if (arp->ar_hln != bond->dev->addr_len || 3001 skb->pkt_type == PACKET_OTHERHOST || 3002 skb->pkt_type == PACKET_LOOPBACK || 3003 arp->ar_hrd != htons(ARPHRD_ETHER) || 3004 arp->ar_pro != htons(ETH_P_IP) || 3005 arp->ar_pln != 4) 3006 goto out_unlock; 3007 3008 arp_ptr = (unsigned char *)(arp + 1); 3009 arp_ptr += bond->dev->addr_len; 3010 memcpy(&sip, arp_ptr, 4); 3011 arp_ptr += 4 + bond->dev->addr_len; 3012 memcpy(&tip, arp_ptr, 4); 3013 3014 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n", 3015 __func__, slave->dev->name, bond_slave_state(slave), 3016 bond->params.arp_validate, slave_do_arp_validate(bond, slave), 3017 &sip, &tip); 3018 3019 curr_active_slave = rcu_dereference(bond->curr_active_slave); 3020 curr_arp_slave = rcu_dereference(bond->current_arp_slave); 3021 3022 /* We 'trust' the received ARP enough to validate it if: 3023 * 3024 * (a) the slave receiving the ARP is active (which includes the 3025 * current ARP slave, if any), or 3026 * 3027 * (b) the receiving slave isn't active, but there is a currently 3028 * active slave and it received valid arp reply(s) after it became 3029 * the currently active slave, or 3030 * 3031 * (c) there is an ARP slave that sent an ARP during the prior ARP 3032 * interval, and we receive an ARP reply on any slave. We accept 3033 * these because switch FDB update delays may deliver the ARP 3034 * reply to a slave other than the sender of the ARP request. 3035 * 3036 * Note: for (b), backup slaves are receiving the broadcast ARP 3037 * request, not a reply. This request passes from the sending 3038 * slave through the L2 switch(es) to the receiving slave. Since 3039 * this is checking the request, sip/tip are swapped for 3040 * validation. 3041 * 3042 * This is done to avoid endless looping when we can't reach the 3043 * arp_ip_target and fool ourselves with our own arp requests. 3044 */ 3045 if (bond_is_active_slave(slave)) 3046 bond_validate_arp(bond, slave, sip, tip); 3047 else if (curr_active_slave && 3048 time_after(slave_last_rx(bond, curr_active_slave), 3049 curr_active_slave->last_link_up)) 3050 bond_validate_arp(bond, slave, tip, sip); 3051 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) && 3052 bond_time_in_interval(bond, 3053 dev_trans_start(curr_arp_slave->dev), 1)) 3054 bond_validate_arp(bond, slave, sip, tip); 3055 3056 out_unlock: 3057 if (arp != (struct arphdr *)skb->data) 3058 kfree(arp); 3059 return RX_HANDLER_ANOTHER; 3060 } 3061 3062 /* function to verify if we're in the arp_interval timeslice, returns true if 3063 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval + 3064 * arp_interval/2) . the arp_interval/2 is needed for really fast networks. 3065 */ 3066 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act, 3067 int mod) 3068 { 3069 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 3070 3071 return time_in_range(jiffies, 3072 last_act - delta_in_ticks, 3073 last_act + mod * delta_in_ticks + delta_in_ticks/2); 3074 } 3075 3076 /* This function is called regularly to monitor each slave's link 3077 * ensuring that traffic is being sent and received when arp monitoring 3078 * is used in load-balancing mode. if the adapter has been dormant, then an 3079 * arp is transmitted to generate traffic. see activebackup_arp_monitor for 3080 * arp monitoring in active backup mode. 3081 */ 3082 static void bond_loadbalance_arp_mon(struct bonding *bond) 3083 { 3084 struct slave *slave, *oldcurrent; 3085 struct list_head *iter; 3086 int do_failover = 0, slave_state_changed = 0; 3087 3088 if (!bond_has_slaves(bond)) 3089 goto re_arm; 3090 3091 rcu_read_lock(); 3092 3093 oldcurrent = rcu_dereference(bond->curr_active_slave); 3094 /* see if any of the previous devices are up now (i.e. they have 3095 * xmt and rcv traffic). the curr_active_slave does not come into 3096 * the picture unless it is null. also, slave->last_link_up is not 3097 * needed here because we send an arp on each slave and give a slave 3098 * as long as it needs to get the tx/rx within the delta. 3099 * TODO: what about up/down delay in arp mode? it wasn't here before 3100 * so it can wait 3101 */ 3102 bond_for_each_slave_rcu(bond, slave, iter) { 3103 unsigned long trans_start = dev_trans_start(slave->dev); 3104 3105 bond_propose_link_state(slave, BOND_LINK_NOCHANGE); 3106 3107 if (slave->link != BOND_LINK_UP) { 3108 if (bond_time_in_interval(bond, trans_start, 1) && 3109 bond_time_in_interval(bond, slave->last_rx, 1)) { 3110 3111 bond_propose_link_state(slave, BOND_LINK_UP); 3112 slave_state_changed = 1; 3113 3114 /* primary_slave has no meaning in round-robin 3115 * mode. the window of a slave being up and 3116 * curr_active_slave being null after enslaving 3117 * is closed. 3118 */ 3119 if (!oldcurrent) { 3120 slave_info(bond->dev, slave->dev, "link status definitely up\n"); 3121 do_failover = 1; 3122 } else { 3123 slave_info(bond->dev, slave->dev, "interface is now up\n"); 3124 } 3125 } 3126 } else { 3127 /* slave->link == BOND_LINK_UP */ 3128 3129 /* not all switches will respond to an arp request 3130 * when the source ip is 0, so don't take the link down 3131 * if we don't know our ip yet 3132 */ 3133 if (!bond_time_in_interval(bond, trans_start, bond->params.missed_max) || 3134 !bond_time_in_interval(bond, slave->last_rx, bond->params.missed_max)) { 3135 3136 bond_propose_link_state(slave, BOND_LINK_DOWN); 3137 slave_state_changed = 1; 3138 3139 if (slave->link_failure_count < UINT_MAX) 3140 slave->link_failure_count++; 3141 3142 slave_info(bond->dev, slave->dev, "interface is now down\n"); 3143 3144 if (slave == oldcurrent) 3145 do_failover = 1; 3146 } 3147 } 3148 3149 /* note: if switch is in round-robin mode, all links 3150 * must tx arp to ensure all links rx an arp - otherwise 3151 * links may oscillate or not come up at all; if switch is 3152 * in something like xor mode, there is nothing we can 3153 * do - all replies will be rx'ed on same link causing slaves 3154 * to be unstable during low/no traffic periods 3155 */ 3156 if (bond_slave_is_up(slave)) 3157 bond_arp_send_all(bond, slave); 3158 } 3159 3160 rcu_read_unlock(); 3161 3162 if (do_failover || slave_state_changed) { 3163 if (!rtnl_trylock()) 3164 goto re_arm; 3165 3166 bond_for_each_slave(bond, slave, iter) { 3167 if (slave->link_new_state != BOND_LINK_NOCHANGE) 3168 slave->link = slave->link_new_state; 3169 } 3170 3171 if (slave_state_changed) { 3172 bond_slave_state_change(bond); 3173 if (BOND_MODE(bond) == BOND_MODE_XOR) 3174 bond_update_slave_arr(bond, NULL); 3175 } 3176 if (do_failover) { 3177 block_netpoll_tx(); 3178 bond_select_active_slave(bond); 3179 unblock_netpoll_tx(); 3180 } 3181 rtnl_unlock(); 3182 } 3183 3184 re_arm: 3185 if (bond->params.arp_interval) 3186 queue_delayed_work(bond->wq, &bond->arp_work, 3187 msecs_to_jiffies(bond->params.arp_interval)); 3188 } 3189 3190 /* Called to inspect slaves for active-backup mode ARP monitor link state 3191 * changes. Sets proposed link state in slaves to specify what action 3192 * should take place for the slave. Returns 0 if no changes are found, >0 3193 * if changes to link states must be committed. 3194 * 3195 * Called with rcu_read_lock held. 3196 */ 3197 static int bond_ab_arp_inspect(struct bonding *bond) 3198 { 3199 unsigned long trans_start, last_rx; 3200 struct list_head *iter; 3201 struct slave *slave; 3202 int commit = 0; 3203 3204 bond_for_each_slave_rcu(bond, slave, iter) { 3205 bond_propose_link_state(slave, BOND_LINK_NOCHANGE); 3206 last_rx = slave_last_rx(bond, slave); 3207 3208 if (slave->link != BOND_LINK_UP) { 3209 if (bond_time_in_interval(bond, last_rx, 1)) { 3210 bond_propose_link_state(slave, BOND_LINK_UP); 3211 commit++; 3212 } else if (slave->link == BOND_LINK_BACK) { 3213 bond_propose_link_state(slave, BOND_LINK_FAIL); 3214 commit++; 3215 } 3216 continue; 3217 } 3218 3219 /* Give slaves 2*delta after being enslaved or made 3220 * active. This avoids bouncing, as the last receive 3221 * times need a full ARP monitor cycle to be updated. 3222 */ 3223 if (bond_time_in_interval(bond, slave->last_link_up, 2)) 3224 continue; 3225 3226 /* Backup slave is down if: 3227 * - No current_arp_slave AND 3228 * - more than (missed_max+1)*delta since last receive AND 3229 * - the bond has an IP address 3230 * 3231 * Note: a non-null current_arp_slave indicates 3232 * the curr_active_slave went down and we are 3233 * searching for a new one; under this condition 3234 * we only take the curr_active_slave down - this 3235 * gives each slave a chance to tx/rx traffic 3236 * before being taken out 3237 */ 3238 if (!bond_is_active_slave(slave) && 3239 !rcu_access_pointer(bond->current_arp_slave) && 3240 !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) { 3241 bond_propose_link_state(slave, BOND_LINK_DOWN); 3242 commit++; 3243 } 3244 3245 /* Active slave is down if: 3246 * - more than missed_max*delta since transmitting OR 3247 * - (more than missed_max*delta since receive AND 3248 * the bond has an IP address) 3249 */ 3250 trans_start = dev_trans_start(slave->dev); 3251 if (bond_is_active_slave(slave) && 3252 (!bond_time_in_interval(bond, trans_start, bond->params.missed_max) || 3253 !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) { 3254 bond_propose_link_state(slave, BOND_LINK_DOWN); 3255 commit++; 3256 } 3257 } 3258 3259 return commit; 3260 } 3261 3262 /* Called to commit link state changes noted by inspection step of 3263 * active-backup mode ARP monitor. 3264 * 3265 * Called with RTNL hold. 3266 */ 3267 static void bond_ab_arp_commit(struct bonding *bond) 3268 { 3269 unsigned long trans_start; 3270 struct list_head *iter; 3271 struct slave *slave; 3272 3273 bond_for_each_slave(bond, slave, iter) { 3274 switch (slave->link_new_state) { 3275 case BOND_LINK_NOCHANGE: 3276 continue; 3277 3278 case BOND_LINK_UP: 3279 trans_start = dev_trans_start(slave->dev); 3280 if (rtnl_dereference(bond->curr_active_slave) != slave || 3281 (!rtnl_dereference(bond->curr_active_slave) && 3282 bond_time_in_interval(bond, trans_start, 1))) { 3283 struct slave *current_arp_slave; 3284 3285 current_arp_slave = rtnl_dereference(bond->current_arp_slave); 3286 bond_set_slave_link_state(slave, BOND_LINK_UP, 3287 BOND_SLAVE_NOTIFY_NOW); 3288 if (current_arp_slave) { 3289 bond_set_slave_inactive_flags( 3290 current_arp_slave, 3291 BOND_SLAVE_NOTIFY_NOW); 3292 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 3293 } 3294 3295 slave_info(bond->dev, slave->dev, "link status definitely up\n"); 3296 3297 if (!rtnl_dereference(bond->curr_active_slave) || 3298 slave == rtnl_dereference(bond->primary_slave)) 3299 goto do_failover; 3300 3301 } 3302 3303 continue; 3304 3305 case BOND_LINK_DOWN: 3306 if (slave->link_failure_count < UINT_MAX) 3307 slave->link_failure_count++; 3308 3309 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 3310 BOND_SLAVE_NOTIFY_NOW); 3311 bond_set_slave_inactive_flags(slave, 3312 BOND_SLAVE_NOTIFY_NOW); 3313 3314 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n"); 3315 3316 if (slave == rtnl_dereference(bond->curr_active_slave)) { 3317 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 3318 goto do_failover; 3319 } 3320 3321 continue; 3322 3323 case BOND_LINK_FAIL: 3324 bond_set_slave_link_state(slave, BOND_LINK_FAIL, 3325 BOND_SLAVE_NOTIFY_NOW); 3326 bond_set_slave_inactive_flags(slave, 3327 BOND_SLAVE_NOTIFY_NOW); 3328 3329 /* A slave has just been enslaved and has become 3330 * the current active slave. 3331 */ 3332 if (rtnl_dereference(bond->curr_active_slave)) 3333 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 3334 continue; 3335 3336 default: 3337 slave_err(bond->dev, slave->dev, 3338 "impossible: link_new_state %d on slave\n", 3339 slave->link_new_state); 3340 continue; 3341 } 3342 3343 do_failover: 3344 block_netpoll_tx(); 3345 bond_select_active_slave(bond); 3346 unblock_netpoll_tx(); 3347 } 3348 3349 bond_set_carrier(bond); 3350 } 3351 3352 /* Send ARP probes for active-backup mode ARP monitor. 3353 * 3354 * Called with rcu_read_lock held. 3355 */ 3356 static bool bond_ab_arp_probe(struct bonding *bond) 3357 { 3358 struct slave *slave, *before = NULL, *new_slave = NULL, 3359 *curr_arp_slave = rcu_dereference(bond->current_arp_slave), 3360 *curr_active_slave = rcu_dereference(bond->curr_active_slave); 3361 struct list_head *iter; 3362 bool found = false; 3363 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER; 3364 3365 if (curr_arp_slave && curr_active_slave) 3366 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n", 3367 curr_arp_slave->dev->name, 3368 curr_active_slave->dev->name); 3369 3370 if (curr_active_slave) { 3371 bond_arp_send_all(bond, curr_active_slave); 3372 return should_notify_rtnl; 3373 } 3374 3375 /* if we don't have a curr_active_slave, search for the next available 3376 * backup slave from the current_arp_slave and make it the candidate 3377 * for becoming the curr_active_slave 3378 */ 3379 3380 if (!curr_arp_slave) { 3381 curr_arp_slave = bond_first_slave_rcu(bond); 3382 if (!curr_arp_slave) 3383 return should_notify_rtnl; 3384 } 3385 3386 bond_for_each_slave_rcu(bond, slave, iter) { 3387 if (!found && !before && bond_slave_is_up(slave)) 3388 before = slave; 3389 3390 if (found && !new_slave && bond_slave_is_up(slave)) 3391 new_slave = slave; 3392 /* if the link state is up at this point, we 3393 * mark it down - this can happen if we have 3394 * simultaneous link failures and 3395 * reselect_active_interface doesn't make this 3396 * one the current slave so it is still marked 3397 * up when it is actually down 3398 */ 3399 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { 3400 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 3401 BOND_SLAVE_NOTIFY_LATER); 3402 if (slave->link_failure_count < UINT_MAX) 3403 slave->link_failure_count++; 3404 3405 bond_set_slave_inactive_flags(slave, 3406 BOND_SLAVE_NOTIFY_LATER); 3407 3408 slave_info(bond->dev, slave->dev, "backup interface is now down\n"); 3409 } 3410 if (slave == curr_arp_slave) 3411 found = true; 3412 } 3413 3414 if (!new_slave && before) 3415 new_slave = before; 3416 3417 if (!new_slave) 3418 goto check_state; 3419 3420 bond_set_slave_link_state(new_slave, BOND_LINK_BACK, 3421 BOND_SLAVE_NOTIFY_LATER); 3422 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER); 3423 bond_arp_send_all(bond, new_slave); 3424 new_slave->last_link_up = jiffies; 3425 rcu_assign_pointer(bond->current_arp_slave, new_slave); 3426 3427 check_state: 3428 bond_for_each_slave_rcu(bond, slave, iter) { 3429 if (slave->should_notify || slave->should_notify_link) { 3430 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW; 3431 break; 3432 } 3433 } 3434 return should_notify_rtnl; 3435 } 3436 3437 static void bond_activebackup_arp_mon(struct bonding *bond) 3438 { 3439 bool should_notify_peers = false; 3440 bool should_notify_rtnl = false; 3441 int delta_in_ticks; 3442 3443 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 3444 3445 if (!bond_has_slaves(bond)) 3446 goto re_arm; 3447 3448 rcu_read_lock(); 3449 3450 should_notify_peers = bond_should_notify_peers(bond); 3451 3452 if (bond_ab_arp_inspect(bond)) { 3453 rcu_read_unlock(); 3454 3455 /* Race avoidance with bond_close flush of workqueue */ 3456 if (!rtnl_trylock()) { 3457 delta_in_ticks = 1; 3458 should_notify_peers = false; 3459 goto re_arm; 3460 } 3461 3462 bond_ab_arp_commit(bond); 3463 3464 rtnl_unlock(); 3465 rcu_read_lock(); 3466 } 3467 3468 should_notify_rtnl = bond_ab_arp_probe(bond); 3469 rcu_read_unlock(); 3470 3471 re_arm: 3472 if (bond->params.arp_interval) 3473 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks); 3474 3475 if (should_notify_peers || should_notify_rtnl) { 3476 if (!rtnl_trylock()) 3477 return; 3478 3479 if (should_notify_peers) 3480 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, 3481 bond->dev); 3482 if (should_notify_rtnl) { 3483 bond_slave_state_notify(bond); 3484 bond_slave_link_notify(bond); 3485 } 3486 3487 rtnl_unlock(); 3488 } 3489 } 3490 3491 static void bond_arp_monitor(struct work_struct *work) 3492 { 3493 struct bonding *bond = container_of(work, struct bonding, 3494 arp_work.work); 3495 3496 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 3497 bond_activebackup_arp_mon(bond); 3498 else 3499 bond_loadbalance_arp_mon(bond); 3500 } 3501 3502 /*-------------------------- netdev event handling --------------------------*/ 3503 3504 /* Change device name */ 3505 static int bond_event_changename(struct bonding *bond) 3506 { 3507 bond_remove_proc_entry(bond); 3508 bond_create_proc_entry(bond); 3509 3510 bond_debug_reregister(bond); 3511 3512 return NOTIFY_DONE; 3513 } 3514 3515 static int bond_master_netdev_event(unsigned long event, 3516 struct net_device *bond_dev) 3517 { 3518 struct bonding *event_bond = netdev_priv(bond_dev); 3519 3520 netdev_dbg(bond_dev, "%s called\n", __func__); 3521 3522 switch (event) { 3523 case NETDEV_CHANGENAME: 3524 return bond_event_changename(event_bond); 3525 case NETDEV_UNREGISTER: 3526 bond_remove_proc_entry(event_bond); 3527 #ifdef CONFIG_XFRM_OFFLOAD 3528 xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true); 3529 #endif /* CONFIG_XFRM_OFFLOAD */ 3530 break; 3531 case NETDEV_REGISTER: 3532 bond_create_proc_entry(event_bond); 3533 break; 3534 default: 3535 break; 3536 } 3537 3538 return NOTIFY_DONE; 3539 } 3540 3541 static int bond_slave_netdev_event(unsigned long event, 3542 struct net_device *slave_dev) 3543 { 3544 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary; 3545 struct bonding *bond; 3546 struct net_device *bond_dev; 3547 3548 /* A netdev event can be generated while enslaving a device 3549 * before netdev_rx_handler_register is called in which case 3550 * slave will be NULL 3551 */ 3552 if (!slave) { 3553 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__); 3554 return NOTIFY_DONE; 3555 } 3556 3557 bond_dev = slave->bond->dev; 3558 bond = slave->bond; 3559 primary = rtnl_dereference(bond->primary_slave); 3560 3561 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__); 3562 3563 switch (event) { 3564 case NETDEV_UNREGISTER: 3565 if (bond_dev->type != ARPHRD_ETHER) 3566 bond_release_and_destroy(bond_dev, slave_dev); 3567 else 3568 __bond_release_one(bond_dev, slave_dev, false, true); 3569 break; 3570 case NETDEV_UP: 3571 case NETDEV_CHANGE: 3572 /* For 802.3ad mode only: 3573 * Getting invalid Speed/Duplex values here will put slave 3574 * in weird state. Mark it as link-fail if the link was 3575 * previously up or link-down if it hasn't yet come up, and 3576 * let link-monitoring (miimon) set it right when correct 3577 * speeds/duplex are available. 3578 */ 3579 if (bond_update_speed_duplex(slave) && 3580 BOND_MODE(bond) == BOND_MODE_8023AD) { 3581 if (slave->last_link_up) 3582 slave->link = BOND_LINK_FAIL; 3583 else 3584 slave->link = BOND_LINK_DOWN; 3585 } 3586 3587 if (BOND_MODE(bond) == BOND_MODE_8023AD) 3588 bond_3ad_adapter_speed_duplex_changed(slave); 3589 fallthrough; 3590 case NETDEV_DOWN: 3591 /* Refresh slave-array if applicable! 3592 * If the setup does not use miimon or arpmon (mode-specific!), 3593 * then these events will not cause the slave-array to be 3594 * refreshed. This will cause xmit to use a slave that is not 3595 * usable. Avoid such situation by refeshing the array at these 3596 * events. If these (miimon/arpmon) parameters are configured 3597 * then array gets refreshed twice and that should be fine! 3598 */ 3599 if (bond_mode_can_use_xmit_hash(bond)) 3600 bond_update_slave_arr(bond, NULL); 3601 break; 3602 case NETDEV_CHANGEMTU: 3603 /* TODO: Should slaves be allowed to 3604 * independently alter their MTU? For 3605 * an active-backup bond, slaves need 3606 * not be the same type of device, so 3607 * MTUs may vary. For other modes, 3608 * slaves arguably should have the 3609 * same MTUs. To do this, we'd need to 3610 * take over the slave's change_mtu 3611 * function for the duration of their 3612 * servitude. 3613 */ 3614 break; 3615 case NETDEV_CHANGENAME: 3616 /* we don't care if we don't have primary set */ 3617 if (!bond_uses_primary(bond) || 3618 !bond->params.primary[0]) 3619 break; 3620 3621 if (slave == primary) { 3622 /* slave's name changed - he's no longer primary */ 3623 RCU_INIT_POINTER(bond->primary_slave, NULL); 3624 } else if (!strcmp(slave_dev->name, bond->params.primary)) { 3625 /* we have a new primary slave */ 3626 rcu_assign_pointer(bond->primary_slave, slave); 3627 } else { /* we didn't change primary - exit */ 3628 break; 3629 } 3630 3631 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n", 3632 primary ? slave_dev->name : "none"); 3633 3634 block_netpoll_tx(); 3635 bond_select_active_slave(bond); 3636 unblock_netpoll_tx(); 3637 break; 3638 case NETDEV_FEAT_CHANGE: 3639 bond_compute_features(bond); 3640 break; 3641 case NETDEV_RESEND_IGMP: 3642 /* Propagate to master device */ 3643 call_netdevice_notifiers(event, slave->bond->dev); 3644 break; 3645 default: 3646 break; 3647 } 3648 3649 return NOTIFY_DONE; 3650 } 3651 3652 /* bond_netdev_event: handle netdev notifier chain events. 3653 * 3654 * This function receives events for the netdev chain. The caller (an 3655 * ioctl handler calling blocking_notifier_call_chain) holds the necessary 3656 * locks for us to safely manipulate the slave devices (RTNL lock, 3657 * dev_probe_lock). 3658 */ 3659 static int bond_netdev_event(struct notifier_block *this, 3660 unsigned long event, void *ptr) 3661 { 3662 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 3663 3664 netdev_dbg(event_dev, "%s received %s\n", 3665 __func__, netdev_cmd_to_name(event)); 3666 3667 if (!(event_dev->priv_flags & IFF_BONDING)) 3668 return NOTIFY_DONE; 3669 3670 if (event_dev->flags & IFF_MASTER) { 3671 int ret; 3672 3673 ret = bond_master_netdev_event(event, event_dev); 3674 if (ret != NOTIFY_DONE) 3675 return ret; 3676 } 3677 3678 if (event_dev->flags & IFF_SLAVE) 3679 return bond_slave_netdev_event(event, event_dev); 3680 3681 return NOTIFY_DONE; 3682 } 3683 3684 static struct notifier_block bond_netdev_notifier = { 3685 .notifier_call = bond_netdev_event, 3686 }; 3687 3688 /*---------------------------- Hashing Policies -----------------------------*/ 3689 3690 /* Helper to access data in a packet, with or without a backing skb. 3691 * If skb is given the data is linearized if necessary via pskb_may_pull. 3692 */ 3693 static inline const void *bond_pull_data(struct sk_buff *skb, 3694 const void *data, int hlen, int n) 3695 { 3696 if (likely(n <= hlen)) 3697 return data; 3698 else if (skb && likely(pskb_may_pull(skb, n))) 3699 return skb->head; 3700 3701 return NULL; 3702 } 3703 3704 /* L2 hash helper */ 3705 static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen) 3706 { 3707 struct ethhdr *ep; 3708 3709 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr)); 3710 if (!data) 3711 return 0; 3712 3713 ep = (struct ethhdr *)(data + mhoff); 3714 return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto); 3715 } 3716 3717 static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data, 3718 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34) 3719 { 3720 const struct ipv6hdr *iph6; 3721 const struct iphdr *iph; 3722 3723 if (l2_proto == htons(ETH_P_IP)) { 3724 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph)); 3725 if (!data) 3726 return false; 3727 3728 iph = (const struct iphdr *)(data + *nhoff); 3729 iph_to_flow_copy_v4addrs(fk, iph); 3730 *nhoff += iph->ihl << 2; 3731 if (!ip_is_fragment(iph)) 3732 *ip_proto = iph->protocol; 3733 } else if (l2_proto == htons(ETH_P_IPV6)) { 3734 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6)); 3735 if (!data) 3736 return false; 3737 3738 iph6 = (const struct ipv6hdr *)(data + *nhoff); 3739 iph_to_flow_copy_v6addrs(fk, iph6); 3740 *nhoff += sizeof(*iph6); 3741 *ip_proto = iph6->nexthdr; 3742 } else { 3743 return false; 3744 } 3745 3746 if (l34 && *ip_proto >= 0) 3747 fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen); 3748 3749 return true; 3750 } 3751 3752 static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen) 3753 { 3754 u32 srcmac_vendor = 0, srcmac_dev = 0; 3755 struct ethhdr *mac_hdr; 3756 u16 vlan = 0; 3757 int i; 3758 3759 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr)); 3760 if (!data) 3761 return 0; 3762 mac_hdr = (struct ethhdr *)(data + mhoff); 3763 3764 for (i = 0; i < 3; i++) 3765 srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i]; 3766 3767 for (i = 3; i < ETH_ALEN; i++) 3768 srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i]; 3769 3770 if (skb && skb_vlan_tag_present(skb)) 3771 vlan = skb_vlan_tag_get(skb); 3772 3773 return vlan ^ srcmac_vendor ^ srcmac_dev; 3774 } 3775 3776 /* Extract the appropriate headers based on bond's xmit policy */ 3777 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data, 3778 __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk) 3779 { 3780 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34; 3781 int ip_proto = -1; 3782 3783 switch (bond->params.xmit_policy) { 3784 case BOND_XMIT_POLICY_ENCAP23: 3785 case BOND_XMIT_POLICY_ENCAP34: 3786 memset(fk, 0, sizeof(*fk)); 3787 return __skb_flow_dissect(NULL, skb, &flow_keys_bonding, 3788 fk, data, l2_proto, nhoff, hlen, 0); 3789 default: 3790 break; 3791 } 3792 3793 fk->ports.ports = 0; 3794 memset(&fk->icmp, 0, sizeof(fk->icmp)); 3795 if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34)) 3796 return false; 3797 3798 /* ICMP error packets contains at least 8 bytes of the header 3799 * of the packet which generated the error. Use this information 3800 * to correlate ICMP error packets within the same flow which 3801 * generated the error. 3802 */ 3803 if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) { 3804 skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen); 3805 if (ip_proto == IPPROTO_ICMP) { 3806 if (!icmp_is_err(fk->icmp.type)) 3807 return true; 3808 3809 nhoff += sizeof(struct icmphdr); 3810 } else if (ip_proto == IPPROTO_ICMPV6) { 3811 if (!icmpv6_is_err(fk->icmp.type)) 3812 return true; 3813 3814 nhoff += sizeof(struct icmp6hdr); 3815 } 3816 return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34); 3817 } 3818 3819 return true; 3820 } 3821 3822 static u32 bond_ip_hash(u32 hash, struct flow_keys *flow) 3823 { 3824 hash ^= (__force u32)flow_get_u32_dst(flow) ^ 3825 (__force u32)flow_get_u32_src(flow); 3826 hash ^= (hash >> 16); 3827 hash ^= (hash >> 8); 3828 /* discard lowest hash bit to deal with the common even ports pattern */ 3829 return hash >> 1; 3830 } 3831 3832 /* Generate hash based on xmit policy. If @skb is given it is used to linearize 3833 * the data as required, but this function can be used without it if the data is 3834 * known to be linear (e.g. with xdp_buff). 3835 */ 3836 static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data, 3837 __be16 l2_proto, int mhoff, int nhoff, int hlen) 3838 { 3839 struct flow_keys flow; 3840 u32 hash; 3841 3842 if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC) 3843 return bond_vlan_srcmac_hash(skb, data, mhoff, hlen); 3844 3845 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 || 3846 !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow)) 3847 return bond_eth_hash(skb, data, mhoff, hlen); 3848 3849 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 || 3850 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) { 3851 hash = bond_eth_hash(skb, data, mhoff, hlen); 3852 } else { 3853 if (flow.icmp.id) 3854 memcpy(&hash, &flow.icmp, sizeof(hash)); 3855 else 3856 memcpy(&hash, &flow.ports.ports, sizeof(hash)); 3857 } 3858 3859 return bond_ip_hash(hash, &flow); 3860 } 3861 3862 /** 3863 * bond_xmit_hash - generate a hash value based on the xmit policy 3864 * @bond: bonding device 3865 * @skb: buffer to use for headers 3866 * 3867 * This function will extract the necessary headers from the skb buffer and use 3868 * them to generate a hash based on the xmit_policy set in the bonding device 3869 */ 3870 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb) 3871 { 3872 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 && 3873 skb->l4_hash) 3874 return skb->hash; 3875 3876 return __bond_xmit_hash(bond, skb, skb->head, skb->protocol, 3877 skb->mac_header, skb->network_header, 3878 skb_headlen(skb)); 3879 } 3880 3881 /** 3882 * bond_xmit_hash_xdp - generate a hash value based on the xmit policy 3883 * @bond: bonding device 3884 * @xdp: buffer to use for headers 3885 * 3886 * The XDP variant of bond_xmit_hash. 3887 */ 3888 static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp) 3889 { 3890 struct ethhdr *eth; 3891 3892 if (xdp->data + sizeof(struct ethhdr) > xdp->data_end) 3893 return 0; 3894 3895 eth = (struct ethhdr *)xdp->data; 3896 3897 return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0, 3898 sizeof(struct ethhdr), xdp->data_end - xdp->data); 3899 } 3900 3901 /*-------------------------- Device entry points ----------------------------*/ 3902 3903 void bond_work_init_all(struct bonding *bond) 3904 { 3905 INIT_DELAYED_WORK(&bond->mcast_work, 3906 bond_resend_igmp_join_requests_delayed); 3907 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor); 3908 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor); 3909 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor); 3910 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler); 3911 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler); 3912 } 3913 3914 static void bond_work_cancel_all(struct bonding *bond) 3915 { 3916 cancel_delayed_work_sync(&bond->mii_work); 3917 cancel_delayed_work_sync(&bond->arp_work); 3918 cancel_delayed_work_sync(&bond->alb_work); 3919 cancel_delayed_work_sync(&bond->ad_work); 3920 cancel_delayed_work_sync(&bond->mcast_work); 3921 cancel_delayed_work_sync(&bond->slave_arr_work); 3922 } 3923 3924 static int bond_open(struct net_device *bond_dev) 3925 { 3926 struct bonding *bond = netdev_priv(bond_dev); 3927 struct list_head *iter; 3928 struct slave *slave; 3929 3930 /* reset slave->backup and slave->inactive */ 3931 if (bond_has_slaves(bond)) { 3932 bond_for_each_slave(bond, slave, iter) { 3933 if (bond_uses_primary(bond) && 3934 slave != rcu_access_pointer(bond->curr_active_slave)) { 3935 bond_set_slave_inactive_flags(slave, 3936 BOND_SLAVE_NOTIFY_NOW); 3937 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) { 3938 bond_set_slave_active_flags(slave, 3939 BOND_SLAVE_NOTIFY_NOW); 3940 } 3941 } 3942 } 3943 3944 if (bond_is_lb(bond)) { 3945 /* bond_alb_initialize must be called before the timer 3946 * is started. 3947 */ 3948 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB))) 3949 return -ENOMEM; 3950 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB) 3951 queue_delayed_work(bond->wq, &bond->alb_work, 0); 3952 } 3953 3954 if (bond->params.miimon) /* link check interval, in milliseconds. */ 3955 queue_delayed_work(bond->wq, &bond->mii_work, 0); 3956 3957 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */ 3958 queue_delayed_work(bond->wq, &bond->arp_work, 0); 3959 bond->recv_probe = bond_arp_rcv; 3960 } 3961 3962 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 3963 queue_delayed_work(bond->wq, &bond->ad_work, 0); 3964 /* register to receive LACPDUs */ 3965 bond->recv_probe = bond_3ad_lacpdu_recv; 3966 bond_3ad_initiate_agg_selection(bond, 1); 3967 } 3968 3969 if (bond_mode_can_use_xmit_hash(bond)) 3970 bond_update_slave_arr(bond, NULL); 3971 3972 return 0; 3973 } 3974 3975 static int bond_close(struct net_device *bond_dev) 3976 { 3977 struct bonding *bond = netdev_priv(bond_dev); 3978 3979 bond_work_cancel_all(bond); 3980 bond->send_peer_notif = 0; 3981 if (bond_is_lb(bond)) 3982 bond_alb_deinitialize(bond); 3983 bond->recv_probe = NULL; 3984 3985 return 0; 3986 } 3987 3988 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but 3989 * that some drivers can provide 32bit values only. 3990 */ 3991 static void bond_fold_stats(struct rtnl_link_stats64 *_res, 3992 const struct rtnl_link_stats64 *_new, 3993 const struct rtnl_link_stats64 *_old) 3994 { 3995 const u64 *new = (const u64 *)_new; 3996 const u64 *old = (const u64 *)_old; 3997 u64 *res = (u64 *)_res; 3998 int i; 3999 4000 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) { 4001 u64 nv = new[i]; 4002 u64 ov = old[i]; 4003 s64 delta = nv - ov; 4004 4005 /* detects if this particular field is 32bit only */ 4006 if (((nv | ov) >> 32) == 0) 4007 delta = (s64)(s32)((u32)nv - (u32)ov); 4008 4009 /* filter anomalies, some drivers reset their stats 4010 * at down/up events. 4011 */ 4012 if (delta > 0) 4013 res[i] += delta; 4014 } 4015 } 4016 4017 #ifdef CONFIG_LOCKDEP 4018 static int bond_get_lowest_level_rcu(struct net_device *dev) 4019 { 4020 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 4021 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 4022 int cur = 0, max = 0; 4023 4024 now = dev; 4025 iter = &dev->adj_list.lower; 4026 4027 while (1) { 4028 next = NULL; 4029 while (1) { 4030 ldev = netdev_next_lower_dev_rcu(now, &iter); 4031 if (!ldev) 4032 break; 4033 4034 next = ldev; 4035 niter = &ldev->adj_list.lower; 4036 dev_stack[cur] = now; 4037 iter_stack[cur++] = iter; 4038 if (max <= cur) 4039 max = cur; 4040 break; 4041 } 4042 4043 if (!next) { 4044 if (!cur) 4045 return max; 4046 next = dev_stack[--cur]; 4047 niter = iter_stack[cur]; 4048 } 4049 4050 now = next; 4051 iter = niter; 4052 } 4053 4054 return max; 4055 } 4056 #endif 4057 4058 static void bond_get_stats(struct net_device *bond_dev, 4059 struct rtnl_link_stats64 *stats) 4060 { 4061 struct bonding *bond = netdev_priv(bond_dev); 4062 struct rtnl_link_stats64 temp; 4063 struct list_head *iter; 4064 struct slave *slave; 4065 int nest_level = 0; 4066 4067 4068 rcu_read_lock(); 4069 #ifdef CONFIG_LOCKDEP 4070 nest_level = bond_get_lowest_level_rcu(bond_dev); 4071 #endif 4072 4073 spin_lock_nested(&bond->stats_lock, nest_level); 4074 memcpy(stats, &bond->bond_stats, sizeof(*stats)); 4075 4076 bond_for_each_slave_rcu(bond, slave, iter) { 4077 const struct rtnl_link_stats64 *new = 4078 dev_get_stats(slave->dev, &temp); 4079 4080 bond_fold_stats(stats, new, &slave->slave_stats); 4081 4082 /* save off the slave stats for the next run */ 4083 memcpy(&slave->slave_stats, new, sizeof(*new)); 4084 } 4085 4086 memcpy(&bond->bond_stats, stats, sizeof(*stats)); 4087 spin_unlock(&bond->stats_lock); 4088 rcu_read_unlock(); 4089 } 4090 4091 static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) 4092 { 4093 struct bonding *bond = netdev_priv(bond_dev); 4094 struct mii_ioctl_data *mii = NULL; 4095 const struct net_device_ops *ops; 4096 struct net_device *real_dev; 4097 struct ifreq ifrr; 4098 int res = 0; 4099 4100 netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd); 4101 4102 switch (cmd) { 4103 case SIOCGMIIPHY: 4104 mii = if_mii(ifr); 4105 if (!mii) 4106 return -EINVAL; 4107 4108 mii->phy_id = 0; 4109 fallthrough; 4110 case SIOCGMIIREG: 4111 /* We do this again just in case we were called by SIOCGMIIREG 4112 * instead of SIOCGMIIPHY. 4113 */ 4114 mii = if_mii(ifr); 4115 if (!mii) 4116 return -EINVAL; 4117 4118 if (mii->reg_num == 1) { 4119 mii->val_out = 0; 4120 if (netif_carrier_ok(bond->dev)) 4121 mii->val_out = BMSR_LSTATUS; 4122 } 4123 4124 break; 4125 case SIOCSHWTSTAMP: 4126 case SIOCGHWTSTAMP: 4127 rcu_read_lock(); 4128 real_dev = bond_option_active_slave_get_rcu(bond); 4129 rcu_read_unlock(); 4130 if (real_dev) { 4131 strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ); 4132 ifrr.ifr_ifru = ifr->ifr_ifru; 4133 4134 ops = real_dev->netdev_ops; 4135 if (netif_device_present(real_dev) && ops->ndo_eth_ioctl) 4136 res = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd); 4137 4138 if (!res) 4139 ifr->ifr_ifru = ifrr.ifr_ifru; 4140 } 4141 break; 4142 default: 4143 res = -EOPNOTSUPP; 4144 } 4145 4146 return res; 4147 } 4148 4149 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) 4150 { 4151 struct bonding *bond = netdev_priv(bond_dev); 4152 struct net_device *slave_dev = NULL; 4153 struct ifbond k_binfo; 4154 struct ifbond __user *u_binfo = NULL; 4155 struct ifslave k_sinfo; 4156 struct ifslave __user *u_sinfo = NULL; 4157 struct bond_opt_value newval; 4158 struct net *net; 4159 int res = 0; 4160 4161 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd); 4162 4163 switch (cmd) { 4164 case SIOCBONDINFOQUERY: 4165 u_binfo = (struct ifbond __user *)ifr->ifr_data; 4166 4167 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) 4168 return -EFAULT; 4169 4170 bond_info_query(bond_dev, &k_binfo); 4171 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) 4172 return -EFAULT; 4173 4174 return 0; 4175 case SIOCBONDSLAVEINFOQUERY: 4176 u_sinfo = (struct ifslave __user *)ifr->ifr_data; 4177 4178 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) 4179 return -EFAULT; 4180 4181 res = bond_slave_info_query(bond_dev, &k_sinfo); 4182 if (res == 0 && 4183 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) 4184 return -EFAULT; 4185 4186 return res; 4187 default: 4188 break; 4189 } 4190 4191 net = dev_net(bond_dev); 4192 4193 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4194 return -EPERM; 4195 4196 slave_dev = __dev_get_by_name(net, ifr->ifr_slave); 4197 4198 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev); 4199 4200 if (!slave_dev) 4201 return -ENODEV; 4202 4203 switch (cmd) { 4204 case SIOCBONDENSLAVE: 4205 res = bond_enslave(bond_dev, slave_dev, NULL); 4206 break; 4207 case SIOCBONDRELEASE: 4208 res = bond_release(bond_dev, slave_dev); 4209 break; 4210 case SIOCBONDSETHWADDR: 4211 res = bond_set_dev_addr(bond_dev, slave_dev); 4212 break; 4213 case SIOCBONDCHANGEACTIVE: 4214 bond_opt_initstr(&newval, slave_dev->name); 4215 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE, 4216 &newval); 4217 break; 4218 default: 4219 res = -EOPNOTSUPP; 4220 } 4221 4222 return res; 4223 } 4224 4225 static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr, 4226 void __user *data, int cmd) 4227 { 4228 struct ifreq ifrdata = { .ifr_data = data }; 4229 4230 switch (cmd) { 4231 case BOND_INFO_QUERY_OLD: 4232 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY); 4233 case BOND_SLAVE_INFO_QUERY_OLD: 4234 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY); 4235 case BOND_ENSLAVE_OLD: 4236 return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE); 4237 case BOND_RELEASE_OLD: 4238 return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE); 4239 case BOND_SETHWADDR_OLD: 4240 return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR); 4241 case BOND_CHANGE_ACTIVE_OLD: 4242 return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE); 4243 } 4244 4245 return -EOPNOTSUPP; 4246 } 4247 4248 static void bond_change_rx_flags(struct net_device *bond_dev, int change) 4249 { 4250 struct bonding *bond = netdev_priv(bond_dev); 4251 4252 if (change & IFF_PROMISC) 4253 bond_set_promiscuity(bond, 4254 bond_dev->flags & IFF_PROMISC ? 1 : -1); 4255 4256 if (change & IFF_ALLMULTI) 4257 bond_set_allmulti(bond, 4258 bond_dev->flags & IFF_ALLMULTI ? 1 : -1); 4259 } 4260 4261 static void bond_set_rx_mode(struct net_device *bond_dev) 4262 { 4263 struct bonding *bond = netdev_priv(bond_dev); 4264 struct list_head *iter; 4265 struct slave *slave; 4266 4267 rcu_read_lock(); 4268 if (bond_uses_primary(bond)) { 4269 slave = rcu_dereference(bond->curr_active_slave); 4270 if (slave) { 4271 dev_uc_sync(slave->dev, bond_dev); 4272 dev_mc_sync(slave->dev, bond_dev); 4273 } 4274 } else { 4275 bond_for_each_slave_rcu(bond, slave, iter) { 4276 dev_uc_sync_multiple(slave->dev, bond_dev); 4277 dev_mc_sync_multiple(slave->dev, bond_dev); 4278 } 4279 } 4280 rcu_read_unlock(); 4281 } 4282 4283 static int bond_neigh_init(struct neighbour *n) 4284 { 4285 struct bonding *bond = netdev_priv(n->dev); 4286 const struct net_device_ops *slave_ops; 4287 struct neigh_parms parms; 4288 struct slave *slave; 4289 int ret = 0; 4290 4291 rcu_read_lock(); 4292 slave = bond_first_slave_rcu(bond); 4293 if (!slave) 4294 goto out; 4295 slave_ops = slave->dev->netdev_ops; 4296 if (!slave_ops->ndo_neigh_setup) 4297 goto out; 4298 4299 /* TODO: find another way [1] to implement this. 4300 * Passing a zeroed structure is fragile, 4301 * but at least we do not pass garbage. 4302 * 4303 * [1] One way would be that ndo_neigh_setup() never touch 4304 * struct neigh_parms, but propagate the new neigh_setup() 4305 * back to ___neigh_create() / neigh_parms_alloc() 4306 */ 4307 memset(&parms, 0, sizeof(parms)); 4308 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms); 4309 4310 if (ret) 4311 goto out; 4312 4313 if (parms.neigh_setup) 4314 ret = parms.neigh_setup(n); 4315 out: 4316 rcu_read_unlock(); 4317 return ret; 4318 } 4319 4320 /* The bonding ndo_neigh_setup is called at init time beofre any 4321 * slave exists. So we must declare proxy setup function which will 4322 * be used at run time to resolve the actual slave neigh param setup. 4323 * 4324 * It's also called by master devices (such as vlans) to setup their 4325 * underlying devices. In that case - do nothing, we're already set up from 4326 * our init. 4327 */ 4328 static int bond_neigh_setup(struct net_device *dev, 4329 struct neigh_parms *parms) 4330 { 4331 /* modify only our neigh_parms */ 4332 if (parms->dev == dev) 4333 parms->neigh_setup = bond_neigh_init; 4334 4335 return 0; 4336 } 4337 4338 /* Change the MTU of all of a master's slaves to match the master */ 4339 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) 4340 { 4341 struct bonding *bond = netdev_priv(bond_dev); 4342 struct slave *slave, *rollback_slave; 4343 struct list_head *iter; 4344 int res = 0; 4345 4346 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu); 4347 4348 bond_for_each_slave(bond, slave, iter) { 4349 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n", 4350 slave, slave->dev->netdev_ops->ndo_change_mtu); 4351 4352 res = dev_set_mtu(slave->dev, new_mtu); 4353 4354 if (res) { 4355 /* If we failed to set the slave's mtu to the new value 4356 * we must abort the operation even in ACTIVE_BACKUP 4357 * mode, because if we allow the backup slaves to have 4358 * different mtu values than the active slave we'll 4359 * need to change their mtu when doing a failover. That 4360 * means changing their mtu from timer context, which 4361 * is probably not a good idea. 4362 */ 4363 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n", 4364 res, new_mtu); 4365 goto unwind; 4366 } 4367 } 4368 4369 bond_dev->mtu = new_mtu; 4370 4371 return 0; 4372 4373 unwind: 4374 /* unwind from head to the slave that failed */ 4375 bond_for_each_slave(bond, rollback_slave, iter) { 4376 int tmp_res; 4377 4378 if (rollback_slave == slave) 4379 break; 4380 4381 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu); 4382 if (tmp_res) 4383 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n", 4384 tmp_res); 4385 } 4386 4387 return res; 4388 } 4389 4390 /* Change HW address 4391 * 4392 * Note that many devices must be down to change the HW address, and 4393 * downing the master releases all slaves. We can make bonds full of 4394 * bonding devices to test this, however. 4395 */ 4396 static int bond_set_mac_address(struct net_device *bond_dev, void *addr) 4397 { 4398 struct bonding *bond = netdev_priv(bond_dev); 4399 struct slave *slave, *rollback_slave; 4400 struct sockaddr_storage *ss = addr, tmp_ss; 4401 struct list_head *iter; 4402 int res = 0; 4403 4404 if (BOND_MODE(bond) == BOND_MODE_ALB) 4405 return bond_alb_set_mac_address(bond_dev, addr); 4406 4407 4408 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond); 4409 4410 /* If fail_over_mac is enabled, do nothing and return success. 4411 * Returning an error causes ifenslave to fail. 4412 */ 4413 if (bond->params.fail_over_mac && 4414 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 4415 return 0; 4416 4417 if (!is_valid_ether_addr(ss->__data)) 4418 return -EADDRNOTAVAIL; 4419 4420 bond_for_each_slave(bond, slave, iter) { 4421 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n", 4422 __func__, slave); 4423 res = dev_set_mac_address(slave->dev, addr, NULL); 4424 if (res) { 4425 /* TODO: consider downing the slave 4426 * and retry ? 4427 * User should expect communications 4428 * breakage anyway until ARP finish 4429 * updating, so... 4430 */ 4431 slave_dbg(bond_dev, slave->dev, "%s: err %d\n", 4432 __func__, res); 4433 goto unwind; 4434 } 4435 } 4436 4437 /* success */ 4438 dev_addr_set(bond_dev, ss->__data); 4439 return 0; 4440 4441 unwind: 4442 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len); 4443 tmp_ss.ss_family = bond_dev->type; 4444 4445 /* unwind from head to the slave that failed */ 4446 bond_for_each_slave(bond, rollback_slave, iter) { 4447 int tmp_res; 4448 4449 if (rollback_slave == slave) 4450 break; 4451 4452 tmp_res = dev_set_mac_address(rollback_slave->dev, 4453 (struct sockaddr *)&tmp_ss, NULL); 4454 if (tmp_res) { 4455 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n", 4456 __func__, tmp_res); 4457 } 4458 } 4459 4460 return res; 4461 } 4462 4463 /** 4464 * bond_get_slave_by_id - get xmit slave with slave_id 4465 * @bond: bonding device that is transmitting 4466 * @slave_id: slave id up to slave_cnt-1 through which to transmit 4467 * 4468 * This function tries to get slave with slave_id but in case 4469 * it fails, it tries to find the first available slave for transmission. 4470 */ 4471 static struct slave *bond_get_slave_by_id(struct bonding *bond, 4472 int slave_id) 4473 { 4474 struct list_head *iter; 4475 struct slave *slave; 4476 int i = slave_id; 4477 4478 /* Here we start from the slave with slave_id */ 4479 bond_for_each_slave_rcu(bond, slave, iter) { 4480 if (--i < 0) { 4481 if (bond_slave_can_tx(slave)) 4482 return slave; 4483 } 4484 } 4485 4486 /* Here we start from the first slave up to slave_id */ 4487 i = slave_id; 4488 bond_for_each_slave_rcu(bond, slave, iter) { 4489 if (--i < 0) 4490 break; 4491 if (bond_slave_can_tx(slave)) 4492 return slave; 4493 } 4494 /* no slave that can tx has been found */ 4495 return NULL; 4496 } 4497 4498 /** 4499 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave 4500 * @bond: bonding device to use 4501 * 4502 * Based on the value of the bonding device's packets_per_slave parameter 4503 * this function generates a slave id, which is usually used as the next 4504 * slave to transmit through. 4505 */ 4506 static u32 bond_rr_gen_slave_id(struct bonding *bond) 4507 { 4508 u32 slave_id; 4509 struct reciprocal_value reciprocal_packets_per_slave; 4510 int packets_per_slave = bond->params.packets_per_slave; 4511 4512 switch (packets_per_slave) { 4513 case 0: 4514 slave_id = prandom_u32(); 4515 break; 4516 case 1: 4517 slave_id = this_cpu_inc_return(*bond->rr_tx_counter); 4518 break; 4519 default: 4520 reciprocal_packets_per_slave = 4521 bond->params.reciprocal_packets_per_slave; 4522 slave_id = this_cpu_inc_return(*bond->rr_tx_counter); 4523 slave_id = reciprocal_divide(slave_id, 4524 reciprocal_packets_per_slave); 4525 break; 4526 } 4527 4528 return slave_id; 4529 } 4530 4531 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond, 4532 struct sk_buff *skb) 4533 { 4534 struct slave *slave; 4535 int slave_cnt; 4536 u32 slave_id; 4537 4538 /* Start with the curr_active_slave that joined the bond as the 4539 * default for sending IGMP traffic. For failover purposes one 4540 * needs to maintain some consistency for the interface that will 4541 * send the join/membership reports. The curr_active_slave found 4542 * will send all of this type of traffic. 4543 */ 4544 if (skb->protocol == htons(ETH_P_IP)) { 4545 int noff = skb_network_offset(skb); 4546 struct iphdr *iph; 4547 4548 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph)))) 4549 goto non_igmp; 4550 4551 iph = ip_hdr(skb); 4552 if (iph->protocol == IPPROTO_IGMP) { 4553 slave = rcu_dereference(bond->curr_active_slave); 4554 if (slave) 4555 return slave; 4556 return bond_get_slave_by_id(bond, 0); 4557 } 4558 } 4559 4560 non_igmp: 4561 slave_cnt = READ_ONCE(bond->slave_cnt); 4562 if (likely(slave_cnt)) { 4563 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; 4564 return bond_get_slave_by_id(bond, slave_id); 4565 } 4566 return NULL; 4567 } 4568 4569 static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond, 4570 struct xdp_buff *xdp) 4571 { 4572 struct slave *slave; 4573 int slave_cnt; 4574 u32 slave_id; 4575 const struct ethhdr *eth; 4576 void *data = xdp->data; 4577 4578 if (data + sizeof(struct ethhdr) > xdp->data_end) 4579 goto non_igmp; 4580 4581 eth = (struct ethhdr *)data; 4582 data += sizeof(struct ethhdr); 4583 4584 /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */ 4585 if (eth->h_proto == htons(ETH_P_IP)) { 4586 const struct iphdr *iph; 4587 4588 if (data + sizeof(struct iphdr) > xdp->data_end) 4589 goto non_igmp; 4590 4591 iph = (struct iphdr *)data; 4592 4593 if (iph->protocol == IPPROTO_IGMP) { 4594 slave = rcu_dereference(bond->curr_active_slave); 4595 if (slave) 4596 return slave; 4597 return bond_get_slave_by_id(bond, 0); 4598 } 4599 } 4600 4601 non_igmp: 4602 slave_cnt = READ_ONCE(bond->slave_cnt); 4603 if (likely(slave_cnt)) { 4604 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; 4605 return bond_get_slave_by_id(bond, slave_id); 4606 } 4607 return NULL; 4608 } 4609 4610 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb, 4611 struct net_device *bond_dev) 4612 { 4613 struct bonding *bond = netdev_priv(bond_dev); 4614 struct slave *slave; 4615 4616 slave = bond_xmit_roundrobin_slave_get(bond, skb); 4617 if (likely(slave)) 4618 return bond_dev_queue_xmit(bond, skb, slave->dev); 4619 4620 return bond_tx_drop(bond_dev, skb); 4621 } 4622 4623 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond) 4624 { 4625 return rcu_dereference(bond->curr_active_slave); 4626 } 4627 4628 /* In active-backup mode, we know that bond->curr_active_slave is always valid if 4629 * the bond has a usable interface. 4630 */ 4631 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb, 4632 struct net_device *bond_dev) 4633 { 4634 struct bonding *bond = netdev_priv(bond_dev); 4635 struct slave *slave; 4636 4637 slave = bond_xmit_activebackup_slave_get(bond); 4638 if (slave) 4639 return bond_dev_queue_xmit(bond, skb, slave->dev); 4640 4641 return bond_tx_drop(bond_dev, skb); 4642 } 4643 4644 /* Use this to update slave_array when (a) it's not appropriate to update 4645 * slave_array right away (note that update_slave_array() may sleep) 4646 * and / or (b) RTNL is not held. 4647 */ 4648 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay) 4649 { 4650 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay); 4651 } 4652 4653 /* Slave array work handler. Holds only RTNL */ 4654 static void bond_slave_arr_handler(struct work_struct *work) 4655 { 4656 struct bonding *bond = container_of(work, struct bonding, 4657 slave_arr_work.work); 4658 int ret; 4659 4660 if (!rtnl_trylock()) 4661 goto err; 4662 4663 ret = bond_update_slave_arr(bond, NULL); 4664 rtnl_unlock(); 4665 if (ret) { 4666 pr_warn_ratelimited("Failed to update slave array from WT\n"); 4667 goto err; 4668 } 4669 return; 4670 4671 err: 4672 bond_slave_arr_work_rearm(bond, 1); 4673 } 4674 4675 static void bond_skip_slave(struct bond_up_slave *slaves, 4676 struct slave *skipslave) 4677 { 4678 int idx; 4679 4680 /* Rare situation where caller has asked to skip a specific 4681 * slave but allocation failed (most likely!). BTW this is 4682 * only possible when the call is initiated from 4683 * __bond_release_one(). In this situation; overwrite the 4684 * skipslave entry in the array with the last entry from the 4685 * array to avoid a situation where the xmit path may choose 4686 * this to-be-skipped slave to send a packet out. 4687 */ 4688 for (idx = 0; slaves && idx < slaves->count; idx++) { 4689 if (skipslave == slaves->arr[idx]) { 4690 slaves->arr[idx] = 4691 slaves->arr[slaves->count - 1]; 4692 slaves->count--; 4693 break; 4694 } 4695 } 4696 } 4697 4698 static void bond_set_slave_arr(struct bonding *bond, 4699 struct bond_up_slave *usable_slaves, 4700 struct bond_up_slave *all_slaves) 4701 { 4702 struct bond_up_slave *usable, *all; 4703 4704 usable = rtnl_dereference(bond->usable_slaves); 4705 rcu_assign_pointer(bond->usable_slaves, usable_slaves); 4706 kfree_rcu(usable, rcu); 4707 4708 all = rtnl_dereference(bond->all_slaves); 4709 rcu_assign_pointer(bond->all_slaves, all_slaves); 4710 kfree_rcu(all, rcu); 4711 } 4712 4713 static void bond_reset_slave_arr(struct bonding *bond) 4714 { 4715 struct bond_up_slave *usable, *all; 4716 4717 usable = rtnl_dereference(bond->usable_slaves); 4718 if (usable) { 4719 RCU_INIT_POINTER(bond->usable_slaves, NULL); 4720 kfree_rcu(usable, rcu); 4721 } 4722 4723 all = rtnl_dereference(bond->all_slaves); 4724 if (all) { 4725 RCU_INIT_POINTER(bond->all_slaves, NULL); 4726 kfree_rcu(all, rcu); 4727 } 4728 } 4729 4730 /* Build the usable slaves array in control path for modes that use xmit-hash 4731 * to determine the slave interface - 4732 * (a) BOND_MODE_8023AD 4733 * (b) BOND_MODE_XOR 4734 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0 4735 * 4736 * The caller is expected to hold RTNL only and NO other lock! 4737 */ 4738 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave) 4739 { 4740 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL; 4741 struct slave *slave; 4742 struct list_head *iter; 4743 int agg_id = 0; 4744 int ret = 0; 4745 4746 might_sleep(); 4747 4748 usable_slaves = kzalloc(struct_size(usable_slaves, arr, 4749 bond->slave_cnt), GFP_KERNEL); 4750 all_slaves = kzalloc(struct_size(all_slaves, arr, 4751 bond->slave_cnt), GFP_KERNEL); 4752 if (!usable_slaves || !all_slaves) { 4753 ret = -ENOMEM; 4754 goto out; 4755 } 4756 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 4757 struct ad_info ad_info; 4758 4759 spin_lock_bh(&bond->mode_lock); 4760 if (bond_3ad_get_active_agg_info(bond, &ad_info)) { 4761 spin_unlock_bh(&bond->mode_lock); 4762 pr_debug("bond_3ad_get_active_agg_info failed\n"); 4763 /* No active aggragator means it's not safe to use 4764 * the previous array. 4765 */ 4766 bond_reset_slave_arr(bond); 4767 goto out; 4768 } 4769 spin_unlock_bh(&bond->mode_lock); 4770 agg_id = ad_info.aggregator_id; 4771 } 4772 bond_for_each_slave(bond, slave, iter) { 4773 if (skipslave == slave) 4774 continue; 4775 4776 all_slaves->arr[all_slaves->count++] = slave; 4777 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 4778 struct aggregator *agg; 4779 4780 agg = SLAVE_AD_INFO(slave)->port.aggregator; 4781 if (!agg || agg->aggregator_identifier != agg_id) 4782 continue; 4783 } 4784 if (!bond_slave_can_tx(slave)) 4785 continue; 4786 4787 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n", 4788 usable_slaves->count); 4789 4790 usable_slaves->arr[usable_slaves->count++] = slave; 4791 } 4792 4793 bond_set_slave_arr(bond, usable_slaves, all_slaves); 4794 return ret; 4795 out: 4796 if (ret != 0 && skipslave) { 4797 bond_skip_slave(rtnl_dereference(bond->all_slaves), 4798 skipslave); 4799 bond_skip_slave(rtnl_dereference(bond->usable_slaves), 4800 skipslave); 4801 } 4802 kfree_rcu(all_slaves, rcu); 4803 kfree_rcu(usable_slaves, rcu); 4804 4805 return ret; 4806 } 4807 4808 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond, 4809 struct sk_buff *skb, 4810 struct bond_up_slave *slaves) 4811 { 4812 struct slave *slave; 4813 unsigned int count; 4814 u32 hash; 4815 4816 hash = bond_xmit_hash(bond, skb); 4817 count = slaves ? READ_ONCE(slaves->count) : 0; 4818 if (unlikely(!count)) 4819 return NULL; 4820 4821 slave = slaves->arr[hash % count]; 4822 return slave; 4823 } 4824 4825 static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond, 4826 struct xdp_buff *xdp) 4827 { 4828 struct bond_up_slave *slaves; 4829 unsigned int count; 4830 u32 hash; 4831 4832 hash = bond_xmit_hash_xdp(bond, xdp); 4833 slaves = rcu_dereference(bond->usable_slaves); 4834 count = slaves ? READ_ONCE(slaves->count) : 0; 4835 if (unlikely(!count)) 4836 return NULL; 4837 4838 return slaves->arr[hash % count]; 4839 } 4840 4841 /* Use this Xmit function for 3AD as well as XOR modes. The current 4842 * usable slave array is formed in the control path. The xmit function 4843 * just calculates hash and sends the packet out. 4844 */ 4845 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb, 4846 struct net_device *dev) 4847 { 4848 struct bonding *bond = netdev_priv(dev); 4849 struct bond_up_slave *slaves; 4850 struct slave *slave; 4851 4852 slaves = rcu_dereference(bond->usable_slaves); 4853 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); 4854 if (likely(slave)) 4855 return bond_dev_queue_xmit(bond, skb, slave->dev); 4856 4857 return bond_tx_drop(dev, skb); 4858 } 4859 4860 /* in broadcast mode, we send everything to all usable interfaces. */ 4861 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb, 4862 struct net_device *bond_dev) 4863 { 4864 struct bonding *bond = netdev_priv(bond_dev); 4865 struct slave *slave = NULL; 4866 struct list_head *iter; 4867 4868 bond_for_each_slave_rcu(bond, slave, iter) { 4869 if (bond_is_last_slave(bond, slave)) 4870 break; 4871 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { 4872 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 4873 4874 if (!skb2) { 4875 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n", 4876 bond_dev->name, __func__); 4877 continue; 4878 } 4879 bond_dev_queue_xmit(bond, skb2, slave->dev); 4880 } 4881 } 4882 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) 4883 return bond_dev_queue_xmit(bond, skb, slave->dev); 4884 4885 return bond_tx_drop(bond_dev, skb); 4886 } 4887 4888 /*------------------------- Device initialization ---------------------------*/ 4889 4890 /* Lookup the slave that corresponds to a qid */ 4891 static inline int bond_slave_override(struct bonding *bond, 4892 struct sk_buff *skb) 4893 { 4894 struct slave *slave = NULL; 4895 struct list_head *iter; 4896 4897 if (!skb_rx_queue_recorded(skb)) 4898 return 1; 4899 4900 /* Find out if any slaves have the same mapping as this skb. */ 4901 bond_for_each_slave_rcu(bond, slave, iter) { 4902 if (slave->queue_id == skb_get_queue_mapping(skb)) { 4903 if (bond_slave_is_up(slave) && 4904 slave->link == BOND_LINK_UP) { 4905 bond_dev_queue_xmit(bond, skb, slave->dev); 4906 return 0; 4907 } 4908 /* If the slave isn't UP, use default transmit policy. */ 4909 break; 4910 } 4911 } 4912 4913 return 1; 4914 } 4915 4916 4917 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb, 4918 struct net_device *sb_dev) 4919 { 4920 /* This helper function exists to help dev_pick_tx get the correct 4921 * destination queue. Using a helper function skips a call to 4922 * skb_tx_hash and will put the skbs in the queue we expect on their 4923 * way down to the bonding driver. 4924 */ 4925 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 4926 4927 /* Save the original txq to restore before passing to the driver */ 4928 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb); 4929 4930 if (unlikely(txq >= dev->real_num_tx_queues)) { 4931 do { 4932 txq -= dev->real_num_tx_queues; 4933 } while (txq >= dev->real_num_tx_queues); 4934 } 4935 return txq; 4936 } 4937 4938 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev, 4939 struct sk_buff *skb, 4940 bool all_slaves) 4941 { 4942 struct bonding *bond = netdev_priv(master_dev); 4943 struct bond_up_slave *slaves; 4944 struct slave *slave = NULL; 4945 4946 switch (BOND_MODE(bond)) { 4947 case BOND_MODE_ROUNDROBIN: 4948 slave = bond_xmit_roundrobin_slave_get(bond, skb); 4949 break; 4950 case BOND_MODE_ACTIVEBACKUP: 4951 slave = bond_xmit_activebackup_slave_get(bond); 4952 break; 4953 case BOND_MODE_8023AD: 4954 case BOND_MODE_XOR: 4955 if (all_slaves) 4956 slaves = rcu_dereference(bond->all_slaves); 4957 else 4958 slaves = rcu_dereference(bond->usable_slaves); 4959 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); 4960 break; 4961 case BOND_MODE_BROADCAST: 4962 break; 4963 case BOND_MODE_ALB: 4964 slave = bond_xmit_alb_slave_get(bond, skb); 4965 break; 4966 case BOND_MODE_TLB: 4967 slave = bond_xmit_tlb_slave_get(bond, skb); 4968 break; 4969 default: 4970 /* Should never happen, mode already checked */ 4971 WARN_ONCE(true, "Unknown bonding mode"); 4972 break; 4973 } 4974 4975 if (slave) 4976 return slave->dev; 4977 return NULL; 4978 } 4979 4980 static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow) 4981 { 4982 switch (sk->sk_family) { 4983 #if IS_ENABLED(CONFIG_IPV6) 4984 case AF_INET6: 4985 if (sk->sk_ipv6only || 4986 ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) { 4987 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 4988 flow->addrs.v6addrs.src = inet6_sk(sk)->saddr; 4989 flow->addrs.v6addrs.dst = sk->sk_v6_daddr; 4990 break; 4991 } 4992 fallthrough; 4993 #endif 4994 default: /* AF_INET */ 4995 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 4996 flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr; 4997 flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr; 4998 break; 4999 } 5000 5001 flow->ports.src = inet_sk(sk)->inet_sport; 5002 flow->ports.dst = inet_sk(sk)->inet_dport; 5003 } 5004 5005 /** 5006 * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields 5007 * @sk: socket to use for headers 5008 * 5009 * This function will extract the necessary field from the socket and use 5010 * them to generate a hash based on the LAYER34 xmit_policy. 5011 * Assumes that sk is a TCP or UDP socket. 5012 */ 5013 static u32 bond_sk_hash_l34(struct sock *sk) 5014 { 5015 struct flow_keys flow; 5016 u32 hash; 5017 5018 bond_sk_to_flow(sk, &flow); 5019 5020 /* L4 */ 5021 memcpy(&hash, &flow.ports.ports, sizeof(hash)); 5022 /* L3 */ 5023 return bond_ip_hash(hash, &flow); 5024 } 5025 5026 static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond, 5027 struct sock *sk) 5028 { 5029 struct bond_up_slave *slaves; 5030 struct slave *slave; 5031 unsigned int count; 5032 u32 hash; 5033 5034 slaves = rcu_dereference(bond->usable_slaves); 5035 count = slaves ? READ_ONCE(slaves->count) : 0; 5036 if (unlikely(!count)) 5037 return NULL; 5038 5039 hash = bond_sk_hash_l34(sk); 5040 slave = slaves->arr[hash % count]; 5041 5042 return slave->dev; 5043 } 5044 5045 static struct net_device *bond_sk_get_lower_dev(struct net_device *dev, 5046 struct sock *sk) 5047 { 5048 struct bonding *bond = netdev_priv(dev); 5049 struct net_device *lower = NULL; 5050 5051 rcu_read_lock(); 5052 if (bond_sk_check(bond)) 5053 lower = __bond_sk_get_lower_dev(bond, sk); 5054 rcu_read_unlock(); 5055 5056 return lower; 5057 } 5058 5059 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5060 static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb, 5061 struct net_device *dev) 5062 { 5063 if (likely(bond_get_slave_by_dev(bond, tls_get_ctx(skb->sk)->netdev))) 5064 return bond_dev_queue_xmit(bond, skb, tls_get_ctx(skb->sk)->netdev); 5065 return bond_tx_drop(dev, skb); 5066 } 5067 #endif 5068 5069 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 5070 { 5071 struct bonding *bond = netdev_priv(dev); 5072 5073 if (bond_should_override_tx_queue(bond) && 5074 !bond_slave_override(bond, skb)) 5075 return NETDEV_TX_OK; 5076 5077 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5078 if (skb->sk && tls_is_sk_tx_device_offloaded(skb->sk)) 5079 return bond_tls_device_xmit(bond, skb, dev); 5080 #endif 5081 5082 switch (BOND_MODE(bond)) { 5083 case BOND_MODE_ROUNDROBIN: 5084 return bond_xmit_roundrobin(skb, dev); 5085 case BOND_MODE_ACTIVEBACKUP: 5086 return bond_xmit_activebackup(skb, dev); 5087 case BOND_MODE_8023AD: 5088 case BOND_MODE_XOR: 5089 return bond_3ad_xor_xmit(skb, dev); 5090 case BOND_MODE_BROADCAST: 5091 return bond_xmit_broadcast(skb, dev); 5092 case BOND_MODE_ALB: 5093 return bond_alb_xmit(skb, dev); 5094 case BOND_MODE_TLB: 5095 return bond_tlb_xmit(skb, dev); 5096 default: 5097 /* Should never happen, mode already checked */ 5098 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond)); 5099 WARN_ON_ONCE(1); 5100 return bond_tx_drop(dev, skb); 5101 } 5102 } 5103 5104 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 5105 { 5106 struct bonding *bond = netdev_priv(dev); 5107 netdev_tx_t ret = NETDEV_TX_OK; 5108 5109 /* If we risk deadlock from transmitting this in the 5110 * netpoll path, tell netpoll to queue the frame for later tx 5111 */ 5112 if (unlikely(is_netpoll_tx_blocked(dev))) 5113 return NETDEV_TX_BUSY; 5114 5115 rcu_read_lock(); 5116 if (bond_has_slaves(bond)) 5117 ret = __bond_start_xmit(skb, dev); 5118 else 5119 ret = bond_tx_drop(dev, skb); 5120 rcu_read_unlock(); 5121 5122 return ret; 5123 } 5124 5125 static struct net_device * 5126 bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp) 5127 { 5128 struct bonding *bond = netdev_priv(bond_dev); 5129 struct slave *slave; 5130 5131 /* Caller needs to hold rcu_read_lock() */ 5132 5133 switch (BOND_MODE(bond)) { 5134 case BOND_MODE_ROUNDROBIN: 5135 slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp); 5136 break; 5137 5138 case BOND_MODE_ACTIVEBACKUP: 5139 slave = bond_xmit_activebackup_slave_get(bond); 5140 break; 5141 5142 case BOND_MODE_8023AD: 5143 case BOND_MODE_XOR: 5144 slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp); 5145 break; 5146 5147 default: 5148 /* Should never happen. Mode guarded by bond_xdp_check() */ 5149 netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n", BOND_MODE(bond)); 5150 WARN_ON_ONCE(1); 5151 return NULL; 5152 } 5153 5154 if (slave) 5155 return slave->dev; 5156 5157 return NULL; 5158 } 5159 5160 static int bond_xdp_xmit(struct net_device *bond_dev, 5161 int n, struct xdp_frame **frames, u32 flags) 5162 { 5163 int nxmit, err = -ENXIO; 5164 5165 rcu_read_lock(); 5166 5167 for (nxmit = 0; nxmit < n; nxmit++) { 5168 struct xdp_frame *frame = frames[nxmit]; 5169 struct xdp_frame *frames1[] = {frame}; 5170 struct net_device *slave_dev; 5171 struct xdp_buff xdp; 5172 5173 xdp_convert_frame_to_buff(frame, &xdp); 5174 5175 slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp); 5176 if (!slave_dev) { 5177 err = -ENXIO; 5178 break; 5179 } 5180 5181 err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags); 5182 if (err < 1) 5183 break; 5184 } 5185 5186 rcu_read_unlock(); 5187 5188 /* If error happened on the first frame then we can pass the error up, otherwise 5189 * report the number of frames that were xmitted. 5190 */ 5191 if (err < 0) 5192 return (nxmit == 0 ? err : nxmit); 5193 5194 return nxmit; 5195 } 5196 5197 static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog, 5198 struct netlink_ext_ack *extack) 5199 { 5200 struct bonding *bond = netdev_priv(dev); 5201 struct list_head *iter; 5202 struct slave *slave, *rollback_slave; 5203 struct bpf_prog *old_prog; 5204 struct netdev_bpf xdp = { 5205 .command = XDP_SETUP_PROG, 5206 .flags = 0, 5207 .prog = prog, 5208 .extack = extack, 5209 }; 5210 int err; 5211 5212 ASSERT_RTNL(); 5213 5214 if (!bond_xdp_check(bond)) 5215 return -EOPNOTSUPP; 5216 5217 old_prog = bond->xdp_prog; 5218 bond->xdp_prog = prog; 5219 5220 bond_for_each_slave(bond, slave, iter) { 5221 struct net_device *slave_dev = slave->dev; 5222 5223 if (!slave_dev->netdev_ops->ndo_bpf || 5224 !slave_dev->netdev_ops->ndo_xdp_xmit) { 5225 SLAVE_NL_ERR(dev, slave_dev, extack, 5226 "Slave device does not support XDP"); 5227 err = -EOPNOTSUPP; 5228 goto err; 5229 } 5230 5231 if (dev_xdp_prog_count(slave_dev) > 0) { 5232 SLAVE_NL_ERR(dev, slave_dev, extack, 5233 "Slave has XDP program loaded, please unload before enslaving"); 5234 err = -EOPNOTSUPP; 5235 goto err; 5236 } 5237 5238 err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); 5239 if (err < 0) { 5240 /* ndo_bpf() sets extack error message */ 5241 slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err); 5242 goto err; 5243 } 5244 if (prog) 5245 bpf_prog_inc(prog); 5246 } 5247 5248 if (prog) { 5249 static_branch_inc(&bpf_master_redirect_enabled_key); 5250 } else if (old_prog) { 5251 bpf_prog_put(old_prog); 5252 static_branch_dec(&bpf_master_redirect_enabled_key); 5253 } 5254 5255 return 0; 5256 5257 err: 5258 /* unwind the program changes */ 5259 bond->xdp_prog = old_prog; 5260 xdp.prog = old_prog; 5261 xdp.extack = NULL; /* do not overwrite original error */ 5262 5263 bond_for_each_slave(bond, rollback_slave, iter) { 5264 struct net_device *slave_dev = rollback_slave->dev; 5265 int err_unwind; 5266 5267 if (slave == rollback_slave) 5268 break; 5269 5270 err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); 5271 if (err_unwind < 0) 5272 slave_err(dev, slave_dev, 5273 "Error %d when unwinding XDP program change\n", err_unwind); 5274 else if (xdp.prog) 5275 bpf_prog_inc(xdp.prog); 5276 } 5277 return err; 5278 } 5279 5280 static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp) 5281 { 5282 switch (xdp->command) { 5283 case XDP_SETUP_PROG: 5284 return bond_xdp_set(dev, xdp->prog, xdp->extack); 5285 default: 5286 return -EINVAL; 5287 } 5288 } 5289 5290 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed) 5291 { 5292 if (speed == 0 || speed == SPEED_UNKNOWN) 5293 speed = slave->speed; 5294 else 5295 speed = min(speed, slave->speed); 5296 5297 return speed; 5298 } 5299 5300 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev, 5301 struct ethtool_link_ksettings *cmd) 5302 { 5303 struct bonding *bond = netdev_priv(bond_dev); 5304 struct list_head *iter; 5305 struct slave *slave; 5306 u32 speed = 0; 5307 5308 cmd->base.duplex = DUPLEX_UNKNOWN; 5309 cmd->base.port = PORT_OTHER; 5310 5311 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we 5312 * do not need to check mode. Though link speed might not represent 5313 * the true receive or transmit bandwidth (not all modes are symmetric) 5314 * this is an accurate maximum. 5315 */ 5316 bond_for_each_slave(bond, slave, iter) { 5317 if (bond_slave_can_tx(slave)) { 5318 if (slave->speed != SPEED_UNKNOWN) { 5319 if (BOND_MODE(bond) == BOND_MODE_BROADCAST) 5320 speed = bond_mode_bcast_speed(slave, 5321 speed); 5322 else 5323 speed += slave->speed; 5324 } 5325 if (cmd->base.duplex == DUPLEX_UNKNOWN && 5326 slave->duplex != DUPLEX_UNKNOWN) 5327 cmd->base.duplex = slave->duplex; 5328 } 5329 } 5330 cmd->base.speed = speed ? : SPEED_UNKNOWN; 5331 5332 return 0; 5333 } 5334 5335 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev, 5336 struct ethtool_drvinfo *drvinfo) 5337 { 5338 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); 5339 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d", 5340 BOND_ABI_VERSION); 5341 } 5342 5343 static int bond_ethtool_get_ts_info(struct net_device *bond_dev, 5344 struct ethtool_ts_info *info) 5345 { 5346 struct bonding *bond = netdev_priv(bond_dev); 5347 const struct ethtool_ops *ops; 5348 struct net_device *real_dev; 5349 struct phy_device *phydev; 5350 5351 rcu_read_lock(); 5352 real_dev = bond_option_active_slave_get_rcu(bond); 5353 rcu_read_unlock(); 5354 if (real_dev) { 5355 ops = real_dev->ethtool_ops; 5356 phydev = real_dev->phydev; 5357 5358 if (phy_has_tsinfo(phydev)) { 5359 return phy_ts_info(phydev, info); 5360 } else if (ops->get_ts_info) { 5361 return ops->get_ts_info(real_dev, info); 5362 } 5363 } 5364 5365 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | 5366 SOF_TIMESTAMPING_SOFTWARE; 5367 info->phc_index = -1; 5368 5369 return 0; 5370 } 5371 5372 static const struct ethtool_ops bond_ethtool_ops = { 5373 .get_drvinfo = bond_ethtool_get_drvinfo, 5374 .get_link = ethtool_op_get_link, 5375 .get_link_ksettings = bond_ethtool_get_link_ksettings, 5376 .get_ts_info = bond_ethtool_get_ts_info, 5377 }; 5378 5379 static const struct net_device_ops bond_netdev_ops = { 5380 .ndo_init = bond_init, 5381 .ndo_uninit = bond_uninit, 5382 .ndo_open = bond_open, 5383 .ndo_stop = bond_close, 5384 .ndo_start_xmit = bond_start_xmit, 5385 .ndo_select_queue = bond_select_queue, 5386 .ndo_get_stats64 = bond_get_stats, 5387 .ndo_eth_ioctl = bond_eth_ioctl, 5388 .ndo_siocbond = bond_do_ioctl, 5389 .ndo_siocdevprivate = bond_siocdevprivate, 5390 .ndo_change_rx_flags = bond_change_rx_flags, 5391 .ndo_set_rx_mode = bond_set_rx_mode, 5392 .ndo_change_mtu = bond_change_mtu, 5393 .ndo_set_mac_address = bond_set_mac_address, 5394 .ndo_neigh_setup = bond_neigh_setup, 5395 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid, 5396 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid, 5397 #ifdef CONFIG_NET_POLL_CONTROLLER 5398 .ndo_netpoll_setup = bond_netpoll_setup, 5399 .ndo_netpoll_cleanup = bond_netpoll_cleanup, 5400 .ndo_poll_controller = bond_poll_controller, 5401 #endif 5402 .ndo_add_slave = bond_enslave, 5403 .ndo_del_slave = bond_release, 5404 .ndo_fix_features = bond_fix_features, 5405 .ndo_features_check = passthru_features_check, 5406 .ndo_get_xmit_slave = bond_xmit_get_slave, 5407 .ndo_sk_get_lower_dev = bond_sk_get_lower_dev, 5408 .ndo_bpf = bond_xdp, 5409 .ndo_xdp_xmit = bond_xdp_xmit, 5410 .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave, 5411 }; 5412 5413 static const struct device_type bond_type = { 5414 .name = "bond", 5415 }; 5416 5417 static void bond_destructor(struct net_device *bond_dev) 5418 { 5419 struct bonding *bond = netdev_priv(bond_dev); 5420 5421 if (bond->wq) 5422 destroy_workqueue(bond->wq); 5423 5424 if (bond->rr_tx_counter) 5425 free_percpu(bond->rr_tx_counter); 5426 } 5427 5428 void bond_setup(struct net_device *bond_dev) 5429 { 5430 struct bonding *bond = netdev_priv(bond_dev); 5431 5432 spin_lock_init(&bond->mode_lock); 5433 bond->params = bonding_defaults; 5434 5435 /* Initialize pointers */ 5436 bond->dev = bond_dev; 5437 5438 /* Initialize the device entry points */ 5439 ether_setup(bond_dev); 5440 bond_dev->max_mtu = ETH_MAX_MTU; 5441 bond_dev->netdev_ops = &bond_netdev_ops; 5442 bond_dev->ethtool_ops = &bond_ethtool_ops; 5443 5444 bond_dev->needs_free_netdev = true; 5445 bond_dev->priv_destructor = bond_destructor; 5446 5447 SET_NETDEV_DEVTYPE(bond_dev, &bond_type); 5448 5449 /* Initialize the device options */ 5450 bond_dev->flags |= IFF_MASTER; 5451 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE; 5452 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 5453 5454 #ifdef CONFIG_XFRM_OFFLOAD 5455 /* set up xfrm device ops (only supported in active-backup right now) */ 5456 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops; 5457 INIT_LIST_HEAD(&bond->ipsec_list); 5458 spin_lock_init(&bond->ipsec_lock); 5459 #endif /* CONFIG_XFRM_OFFLOAD */ 5460 5461 /* don't acquire bond device's netif_tx_lock when transmitting */ 5462 bond_dev->features |= NETIF_F_LLTX; 5463 5464 /* By default, we declare the bond to be fully 5465 * VLAN hardware accelerated capable. Special 5466 * care is taken in the various xmit functions 5467 * when there are slaves that are not hw accel 5468 * capable 5469 */ 5470 5471 /* Don't allow bond devices to change network namespaces. */ 5472 bond_dev->features |= NETIF_F_NETNS_LOCAL; 5473 5474 bond_dev->hw_features = BOND_VLAN_FEATURES | 5475 NETIF_F_HW_VLAN_CTAG_RX | 5476 NETIF_F_HW_VLAN_CTAG_FILTER; 5477 5478 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; 5479 bond_dev->features |= bond_dev->hw_features; 5480 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 5481 #ifdef CONFIG_XFRM_OFFLOAD 5482 bond_dev->hw_features |= BOND_XFRM_FEATURES; 5483 /* Only enable XFRM features if this is an active-backup config */ 5484 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 5485 bond_dev->features |= BOND_XFRM_FEATURES; 5486 #endif /* CONFIG_XFRM_OFFLOAD */ 5487 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5488 if (bond_sk_check(bond)) 5489 bond_dev->features |= BOND_TLS_FEATURES; 5490 #endif 5491 } 5492 5493 /* Destroy a bonding device. 5494 * Must be under rtnl_lock when this function is called. 5495 */ 5496 static void bond_uninit(struct net_device *bond_dev) 5497 { 5498 struct bonding *bond = netdev_priv(bond_dev); 5499 struct bond_up_slave *usable, *all; 5500 struct list_head *iter; 5501 struct slave *slave; 5502 5503 bond_netpoll_cleanup(bond_dev); 5504 5505 /* Release the bonded slaves */ 5506 bond_for_each_slave(bond, slave, iter) 5507 __bond_release_one(bond_dev, slave->dev, true, true); 5508 netdev_info(bond_dev, "Released all slaves\n"); 5509 5510 usable = rtnl_dereference(bond->usable_slaves); 5511 if (usable) { 5512 RCU_INIT_POINTER(bond->usable_slaves, NULL); 5513 kfree_rcu(usable, rcu); 5514 } 5515 5516 all = rtnl_dereference(bond->all_slaves); 5517 if (all) { 5518 RCU_INIT_POINTER(bond->all_slaves, NULL); 5519 kfree_rcu(all, rcu); 5520 } 5521 5522 list_del(&bond->bond_list); 5523 5524 bond_debug_unregister(bond); 5525 } 5526 5527 /*------------------------- Module initialization ---------------------------*/ 5528 5529 static int bond_check_params(struct bond_params *params) 5530 { 5531 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i; 5532 struct bond_opt_value newval; 5533 const struct bond_opt_value *valptr; 5534 int arp_all_targets_value = 0; 5535 u16 ad_actor_sys_prio = 0; 5536 u16 ad_user_port_key = 0; 5537 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 }; 5538 int arp_ip_count; 5539 int bond_mode = BOND_MODE_ROUNDROBIN; 5540 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2; 5541 int lacp_fast = 0; 5542 int tlb_dynamic_lb; 5543 5544 /* Convert string parameters. */ 5545 if (mode) { 5546 bond_opt_initstr(&newval, mode); 5547 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval); 5548 if (!valptr) { 5549 pr_err("Error: Invalid bonding mode \"%s\"\n", mode); 5550 return -EINVAL; 5551 } 5552 bond_mode = valptr->value; 5553 } 5554 5555 if (xmit_hash_policy) { 5556 if (bond_mode == BOND_MODE_ROUNDROBIN || 5557 bond_mode == BOND_MODE_ACTIVEBACKUP || 5558 bond_mode == BOND_MODE_BROADCAST) { 5559 pr_info("xmit_hash_policy param is irrelevant in mode %s\n", 5560 bond_mode_name(bond_mode)); 5561 } else { 5562 bond_opt_initstr(&newval, xmit_hash_policy); 5563 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH), 5564 &newval); 5565 if (!valptr) { 5566 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n", 5567 xmit_hash_policy); 5568 return -EINVAL; 5569 } 5570 xmit_hashtype = valptr->value; 5571 } 5572 } 5573 5574 if (lacp_rate) { 5575 if (bond_mode != BOND_MODE_8023AD) { 5576 pr_info("lacp_rate param is irrelevant in mode %s\n", 5577 bond_mode_name(bond_mode)); 5578 } else { 5579 bond_opt_initstr(&newval, lacp_rate); 5580 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE), 5581 &newval); 5582 if (!valptr) { 5583 pr_err("Error: Invalid lacp rate \"%s\"\n", 5584 lacp_rate); 5585 return -EINVAL; 5586 } 5587 lacp_fast = valptr->value; 5588 } 5589 } 5590 5591 if (ad_select) { 5592 bond_opt_initstr(&newval, ad_select); 5593 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT), 5594 &newval); 5595 if (!valptr) { 5596 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select); 5597 return -EINVAL; 5598 } 5599 params->ad_select = valptr->value; 5600 if (bond_mode != BOND_MODE_8023AD) 5601 pr_warn("ad_select param only affects 802.3ad mode\n"); 5602 } else { 5603 params->ad_select = BOND_AD_STABLE; 5604 } 5605 5606 if (max_bonds < 0) { 5607 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n", 5608 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS); 5609 max_bonds = BOND_DEFAULT_MAX_BONDS; 5610 } 5611 5612 if (miimon < 0) { 5613 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5614 miimon, INT_MAX); 5615 miimon = 0; 5616 } 5617 5618 if (updelay < 0) { 5619 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5620 updelay, INT_MAX); 5621 updelay = 0; 5622 } 5623 5624 if (downdelay < 0) { 5625 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5626 downdelay, INT_MAX); 5627 downdelay = 0; 5628 } 5629 5630 if ((use_carrier != 0) && (use_carrier != 1)) { 5631 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n", 5632 use_carrier); 5633 use_carrier = 1; 5634 } 5635 5636 if (num_peer_notif < 0 || num_peer_notif > 255) { 5637 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n", 5638 num_peer_notif); 5639 num_peer_notif = 1; 5640 } 5641 5642 /* reset values for 802.3ad/TLB/ALB */ 5643 if (!bond_mode_uses_arp(bond_mode)) { 5644 if (!miimon) { 5645 pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n"); 5646 pr_warn("Forcing miimon to 100msec\n"); 5647 miimon = BOND_DEFAULT_MIIMON; 5648 } 5649 } 5650 5651 if (tx_queues < 1 || tx_queues > 255) { 5652 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n", 5653 tx_queues, BOND_DEFAULT_TX_QUEUES); 5654 tx_queues = BOND_DEFAULT_TX_QUEUES; 5655 } 5656 5657 if ((all_slaves_active != 0) && (all_slaves_active != 1)) { 5658 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n", 5659 all_slaves_active); 5660 all_slaves_active = 0; 5661 } 5662 5663 if (resend_igmp < 0 || resend_igmp > 255) { 5664 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n", 5665 resend_igmp, BOND_DEFAULT_RESEND_IGMP); 5666 resend_igmp = BOND_DEFAULT_RESEND_IGMP; 5667 } 5668 5669 bond_opt_initval(&newval, packets_per_slave); 5670 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) { 5671 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n", 5672 packets_per_slave, USHRT_MAX); 5673 packets_per_slave = 1; 5674 } 5675 5676 if (bond_mode == BOND_MODE_ALB) { 5677 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n", 5678 updelay); 5679 } 5680 5681 if (!miimon) { 5682 if (updelay || downdelay) { 5683 /* just warn the user the up/down delay will have 5684 * no effect since miimon is zero... 5685 */ 5686 pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n", 5687 updelay, downdelay); 5688 } 5689 } else { 5690 /* don't allow arp monitoring */ 5691 if (arp_interval) { 5692 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n", 5693 miimon, arp_interval); 5694 arp_interval = 0; 5695 } 5696 5697 if ((updelay % miimon) != 0) { 5698 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", 5699 updelay, miimon, (updelay / miimon) * miimon); 5700 } 5701 5702 updelay /= miimon; 5703 5704 if ((downdelay % miimon) != 0) { 5705 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n", 5706 downdelay, miimon, 5707 (downdelay / miimon) * miimon); 5708 } 5709 5710 downdelay /= miimon; 5711 } 5712 5713 if (arp_interval < 0) { 5714 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5715 arp_interval, INT_MAX); 5716 arp_interval = 0; 5717 } 5718 5719 for (arp_ip_count = 0, i = 0; 5720 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) { 5721 __be32 ip; 5722 5723 /* not a complete check, but good enough to catch mistakes */ 5724 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) || 5725 !bond_is_ip_target_ok(ip)) { 5726 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n", 5727 arp_ip_target[i]); 5728 arp_interval = 0; 5729 } else { 5730 if (bond_get_targets_ip(arp_target, ip) == -1) 5731 arp_target[arp_ip_count++] = ip; 5732 else 5733 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n", 5734 &ip); 5735 } 5736 } 5737 5738 if (arp_interval && !arp_ip_count) { 5739 /* don't allow arping if no arp_ip_target given... */ 5740 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n", 5741 arp_interval); 5742 arp_interval = 0; 5743 } 5744 5745 if (arp_validate) { 5746 if (!arp_interval) { 5747 pr_err("arp_validate requires arp_interval\n"); 5748 return -EINVAL; 5749 } 5750 5751 bond_opt_initstr(&newval, arp_validate); 5752 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE), 5753 &newval); 5754 if (!valptr) { 5755 pr_err("Error: invalid arp_validate \"%s\"\n", 5756 arp_validate); 5757 return -EINVAL; 5758 } 5759 arp_validate_value = valptr->value; 5760 } else { 5761 arp_validate_value = 0; 5762 } 5763 5764 if (arp_all_targets) { 5765 bond_opt_initstr(&newval, arp_all_targets); 5766 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS), 5767 &newval); 5768 if (!valptr) { 5769 pr_err("Error: invalid arp_all_targets_value \"%s\"\n", 5770 arp_all_targets); 5771 arp_all_targets_value = 0; 5772 } else { 5773 arp_all_targets_value = valptr->value; 5774 } 5775 } 5776 5777 if (miimon) { 5778 pr_info("MII link monitoring set to %d ms\n", miimon); 5779 } else if (arp_interval) { 5780 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE, 5781 arp_validate_value); 5782 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):", 5783 arp_interval, valptr->string, arp_ip_count); 5784 5785 for (i = 0; i < arp_ip_count; i++) 5786 pr_cont(" %s", arp_ip_target[i]); 5787 5788 pr_cont("\n"); 5789 5790 } else if (max_bonds) { 5791 /* miimon and arp_interval not set, we need one so things 5792 * work as expected, see bonding.txt for details 5793 */ 5794 pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n"); 5795 } 5796 5797 if (primary && !bond_mode_uses_primary(bond_mode)) { 5798 /* currently, using a primary only makes sense 5799 * in active backup, TLB or ALB modes 5800 */ 5801 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n", 5802 primary, bond_mode_name(bond_mode)); 5803 primary = NULL; 5804 } 5805 5806 if (primary && primary_reselect) { 5807 bond_opt_initstr(&newval, primary_reselect); 5808 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT), 5809 &newval); 5810 if (!valptr) { 5811 pr_err("Error: Invalid primary_reselect \"%s\"\n", 5812 primary_reselect); 5813 return -EINVAL; 5814 } 5815 primary_reselect_value = valptr->value; 5816 } else { 5817 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS; 5818 } 5819 5820 if (fail_over_mac) { 5821 bond_opt_initstr(&newval, fail_over_mac); 5822 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC), 5823 &newval); 5824 if (!valptr) { 5825 pr_err("Error: invalid fail_over_mac \"%s\"\n", 5826 fail_over_mac); 5827 return -EINVAL; 5828 } 5829 fail_over_mac_value = valptr->value; 5830 if (bond_mode != BOND_MODE_ACTIVEBACKUP) 5831 pr_warn("Warning: fail_over_mac only affects active-backup mode\n"); 5832 } else { 5833 fail_over_mac_value = BOND_FOM_NONE; 5834 } 5835 5836 bond_opt_initstr(&newval, "default"); 5837 valptr = bond_opt_parse( 5838 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO), 5839 &newval); 5840 if (!valptr) { 5841 pr_err("Error: No ad_actor_sys_prio default value"); 5842 return -EINVAL; 5843 } 5844 ad_actor_sys_prio = valptr->value; 5845 5846 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY), 5847 &newval); 5848 if (!valptr) { 5849 pr_err("Error: No ad_user_port_key default value"); 5850 return -EINVAL; 5851 } 5852 ad_user_port_key = valptr->value; 5853 5854 bond_opt_initstr(&newval, "default"); 5855 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval); 5856 if (!valptr) { 5857 pr_err("Error: No tlb_dynamic_lb default value"); 5858 return -EINVAL; 5859 } 5860 tlb_dynamic_lb = valptr->value; 5861 5862 if (lp_interval == 0) { 5863 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n", 5864 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL); 5865 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 5866 } 5867 5868 /* fill params struct with the proper values */ 5869 params->mode = bond_mode; 5870 params->xmit_policy = xmit_hashtype; 5871 params->miimon = miimon; 5872 params->num_peer_notif = num_peer_notif; 5873 params->arp_interval = arp_interval; 5874 params->arp_validate = arp_validate_value; 5875 params->arp_all_targets = arp_all_targets_value; 5876 params->missed_max = 2; 5877 params->updelay = updelay; 5878 params->downdelay = downdelay; 5879 params->peer_notif_delay = 0; 5880 params->use_carrier = use_carrier; 5881 params->lacp_active = 1; 5882 params->lacp_fast = lacp_fast; 5883 params->primary[0] = 0; 5884 params->primary_reselect = primary_reselect_value; 5885 params->fail_over_mac = fail_over_mac_value; 5886 params->tx_queues = tx_queues; 5887 params->all_slaves_active = all_slaves_active; 5888 params->resend_igmp = resend_igmp; 5889 params->min_links = min_links; 5890 params->lp_interval = lp_interval; 5891 params->packets_per_slave = packets_per_slave; 5892 params->tlb_dynamic_lb = tlb_dynamic_lb; 5893 params->ad_actor_sys_prio = ad_actor_sys_prio; 5894 eth_zero_addr(params->ad_actor_system); 5895 params->ad_user_port_key = ad_user_port_key; 5896 if (packets_per_slave > 0) { 5897 params->reciprocal_packets_per_slave = 5898 reciprocal_value(packets_per_slave); 5899 } else { 5900 /* reciprocal_packets_per_slave is unused if 5901 * packets_per_slave is 0 or 1, just initialize it 5902 */ 5903 params->reciprocal_packets_per_slave = 5904 (struct reciprocal_value) { 0 }; 5905 } 5906 5907 if (primary) 5908 strscpy_pad(params->primary, primary, sizeof(params->primary)); 5909 5910 memcpy(params->arp_targets, arp_target, sizeof(arp_target)); 5911 5912 return 0; 5913 } 5914 5915 /* Called from registration process */ 5916 static int bond_init(struct net_device *bond_dev) 5917 { 5918 struct bonding *bond = netdev_priv(bond_dev); 5919 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id); 5920 5921 netdev_dbg(bond_dev, "Begin bond_init\n"); 5922 5923 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM); 5924 if (!bond->wq) 5925 return -ENOMEM; 5926 5927 if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN) { 5928 bond->rr_tx_counter = alloc_percpu(u32); 5929 if (!bond->rr_tx_counter) { 5930 destroy_workqueue(bond->wq); 5931 bond->wq = NULL; 5932 return -ENOMEM; 5933 } 5934 } 5935 5936 spin_lock_init(&bond->stats_lock); 5937 netdev_lockdep_set_classes(bond_dev); 5938 5939 list_add_tail(&bond->bond_list, &bn->dev_list); 5940 5941 bond_prepare_sysfs_group(bond); 5942 5943 bond_debug_register(bond); 5944 5945 /* Ensure valid dev_addr */ 5946 if (is_zero_ether_addr(bond_dev->dev_addr) && 5947 bond_dev->addr_assign_type == NET_ADDR_PERM) 5948 eth_hw_addr_random(bond_dev); 5949 5950 return 0; 5951 } 5952 5953 unsigned int bond_get_num_tx_queues(void) 5954 { 5955 return tx_queues; 5956 } 5957 5958 /* Create a new bond based on the specified name and bonding parameters. 5959 * If name is NULL, obtain a suitable "bond%d" name for us. 5960 * Caller must NOT hold rtnl_lock; we need to release it here before we 5961 * set up our sysfs entries. 5962 */ 5963 int bond_create(struct net *net, const char *name) 5964 { 5965 struct net_device *bond_dev; 5966 struct bonding *bond; 5967 struct alb_bond_info *bond_info; 5968 int res; 5969 5970 rtnl_lock(); 5971 5972 bond_dev = alloc_netdev_mq(sizeof(struct bonding), 5973 name ? name : "bond%d", NET_NAME_UNKNOWN, 5974 bond_setup, tx_queues); 5975 if (!bond_dev) { 5976 pr_err("%s: eek! can't alloc netdev!\n", name); 5977 rtnl_unlock(); 5978 return -ENOMEM; 5979 } 5980 5981 /* 5982 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX. 5983 * It is set to 0 by default which is wrong. 5984 */ 5985 bond = netdev_priv(bond_dev); 5986 bond_info = &(BOND_ALB_INFO(bond)); 5987 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; 5988 5989 dev_net_set(bond_dev, net); 5990 bond_dev->rtnl_link_ops = &bond_link_ops; 5991 5992 res = register_netdevice(bond_dev); 5993 if (res < 0) { 5994 free_netdev(bond_dev); 5995 rtnl_unlock(); 5996 5997 return res; 5998 } 5999 6000 netif_carrier_off(bond_dev); 6001 6002 bond_work_init_all(bond); 6003 6004 rtnl_unlock(); 6005 return 0; 6006 } 6007 6008 static int __net_init bond_net_init(struct net *net) 6009 { 6010 struct bond_net *bn = net_generic(net, bond_net_id); 6011 6012 bn->net = net; 6013 INIT_LIST_HEAD(&bn->dev_list); 6014 6015 bond_create_proc_dir(bn); 6016 bond_create_sysfs(bn); 6017 6018 return 0; 6019 } 6020 6021 static void __net_exit bond_net_exit(struct net *net) 6022 { 6023 struct bond_net *bn = net_generic(net, bond_net_id); 6024 struct bonding *bond, *tmp_bond; 6025 LIST_HEAD(list); 6026 6027 bond_destroy_sysfs(bn); 6028 6029 /* Kill off any bonds created after unregistering bond rtnl ops */ 6030 rtnl_lock(); 6031 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list) 6032 unregister_netdevice_queue(bond->dev, &list); 6033 unregister_netdevice_many(&list); 6034 rtnl_unlock(); 6035 6036 bond_destroy_proc_dir(bn); 6037 } 6038 6039 static struct pernet_operations bond_net_ops = { 6040 .init = bond_net_init, 6041 .exit = bond_net_exit, 6042 .id = &bond_net_id, 6043 .size = sizeof(struct bond_net), 6044 }; 6045 6046 static int __init bonding_init(void) 6047 { 6048 int i; 6049 int res; 6050 6051 res = bond_check_params(&bonding_defaults); 6052 if (res) 6053 goto out; 6054 6055 res = register_pernet_subsys(&bond_net_ops); 6056 if (res) 6057 goto out; 6058 6059 res = bond_netlink_init(); 6060 if (res) 6061 goto err_link; 6062 6063 bond_create_debugfs(); 6064 6065 for (i = 0; i < max_bonds; i++) { 6066 res = bond_create(&init_net, NULL); 6067 if (res) 6068 goto err; 6069 } 6070 6071 skb_flow_dissector_init(&flow_keys_bonding, 6072 flow_keys_bonding_keys, 6073 ARRAY_SIZE(flow_keys_bonding_keys)); 6074 6075 register_netdevice_notifier(&bond_netdev_notifier); 6076 out: 6077 return res; 6078 err: 6079 bond_destroy_debugfs(); 6080 bond_netlink_fini(); 6081 err_link: 6082 unregister_pernet_subsys(&bond_net_ops); 6083 goto out; 6084 6085 } 6086 6087 static void __exit bonding_exit(void) 6088 { 6089 unregister_netdevice_notifier(&bond_netdev_notifier); 6090 6091 bond_destroy_debugfs(); 6092 6093 bond_netlink_fini(); 6094 unregister_pernet_subsys(&bond_net_ops); 6095 6096 #ifdef CONFIG_NET_POLL_CONTROLLER 6097 /* Make sure we don't have an imbalance on our netpoll blocking */ 6098 WARN_ON(atomic_read(&netpoll_block_tx)); 6099 #endif 6100 } 6101 6102 module_init(bonding_init); 6103 module_exit(bonding_exit); 6104 MODULE_LICENSE("GPL"); 6105 MODULE_DESCRIPTION(DRV_DESCRIPTION); 6106 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others"); 6107