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 if (!slave || !bond->send_peer_notif || 1101 bond->send_peer_notif % 1102 max(1, bond->params.peer_notif_delay) != 0 || 1103 !netif_carrier_ok(bond->dev) || 1104 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state)) 1105 return false; 1106 1107 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n", 1108 slave ? slave->dev->name : "NULL"); 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 hwtstamp_config cfg; 4098 struct ifreq ifrr; 4099 int res = 0; 4100 4101 netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd); 4102 4103 switch (cmd) { 4104 case SIOCGMIIPHY: 4105 mii = if_mii(ifr); 4106 if (!mii) 4107 return -EINVAL; 4108 4109 mii->phy_id = 0; 4110 fallthrough; 4111 case SIOCGMIIREG: 4112 /* We do this again just in case we were called by SIOCGMIIREG 4113 * instead of SIOCGMIIPHY. 4114 */ 4115 mii = if_mii(ifr); 4116 if (!mii) 4117 return -EINVAL; 4118 4119 if (mii->reg_num == 1) { 4120 mii->val_out = 0; 4121 if (netif_carrier_ok(bond->dev)) 4122 mii->val_out = BMSR_LSTATUS; 4123 } 4124 4125 break; 4126 case SIOCSHWTSTAMP: 4127 case SIOCGHWTSTAMP: 4128 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 4129 return -EFAULT; 4130 4131 if (cfg.flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX) { 4132 rcu_read_lock(); 4133 real_dev = bond_option_active_slave_get_rcu(bond); 4134 rcu_read_unlock(); 4135 if (real_dev) { 4136 strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ); 4137 ifrr.ifr_ifru = ifr->ifr_ifru; 4138 4139 ops = real_dev->netdev_ops; 4140 if (netif_device_present(real_dev) && ops->ndo_eth_ioctl) { 4141 res = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd); 4142 4143 if (!res) 4144 ifr->ifr_ifru = ifrr.ifr_ifru; 4145 4146 return res; 4147 } 4148 } 4149 } 4150 fallthrough; 4151 default: 4152 res = -EOPNOTSUPP; 4153 } 4154 4155 return res; 4156 } 4157 4158 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) 4159 { 4160 struct bonding *bond = netdev_priv(bond_dev); 4161 struct net_device *slave_dev = NULL; 4162 struct ifbond k_binfo; 4163 struct ifbond __user *u_binfo = NULL; 4164 struct ifslave k_sinfo; 4165 struct ifslave __user *u_sinfo = NULL; 4166 struct bond_opt_value newval; 4167 struct net *net; 4168 int res = 0; 4169 4170 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd); 4171 4172 switch (cmd) { 4173 case SIOCBONDINFOQUERY: 4174 u_binfo = (struct ifbond __user *)ifr->ifr_data; 4175 4176 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) 4177 return -EFAULT; 4178 4179 bond_info_query(bond_dev, &k_binfo); 4180 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) 4181 return -EFAULT; 4182 4183 return 0; 4184 case SIOCBONDSLAVEINFOQUERY: 4185 u_sinfo = (struct ifslave __user *)ifr->ifr_data; 4186 4187 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) 4188 return -EFAULT; 4189 4190 res = bond_slave_info_query(bond_dev, &k_sinfo); 4191 if (res == 0 && 4192 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) 4193 return -EFAULT; 4194 4195 return res; 4196 default: 4197 break; 4198 } 4199 4200 net = dev_net(bond_dev); 4201 4202 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4203 return -EPERM; 4204 4205 slave_dev = __dev_get_by_name(net, ifr->ifr_slave); 4206 4207 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev); 4208 4209 if (!slave_dev) 4210 return -ENODEV; 4211 4212 switch (cmd) { 4213 case SIOCBONDENSLAVE: 4214 res = bond_enslave(bond_dev, slave_dev, NULL); 4215 break; 4216 case SIOCBONDRELEASE: 4217 res = bond_release(bond_dev, slave_dev); 4218 break; 4219 case SIOCBONDSETHWADDR: 4220 res = bond_set_dev_addr(bond_dev, slave_dev); 4221 break; 4222 case SIOCBONDCHANGEACTIVE: 4223 bond_opt_initstr(&newval, slave_dev->name); 4224 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE, 4225 &newval); 4226 break; 4227 default: 4228 res = -EOPNOTSUPP; 4229 } 4230 4231 return res; 4232 } 4233 4234 static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr, 4235 void __user *data, int cmd) 4236 { 4237 struct ifreq ifrdata = { .ifr_data = data }; 4238 4239 switch (cmd) { 4240 case BOND_INFO_QUERY_OLD: 4241 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY); 4242 case BOND_SLAVE_INFO_QUERY_OLD: 4243 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY); 4244 case BOND_ENSLAVE_OLD: 4245 return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE); 4246 case BOND_RELEASE_OLD: 4247 return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE); 4248 case BOND_SETHWADDR_OLD: 4249 return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR); 4250 case BOND_CHANGE_ACTIVE_OLD: 4251 return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE); 4252 } 4253 4254 return -EOPNOTSUPP; 4255 } 4256 4257 static void bond_change_rx_flags(struct net_device *bond_dev, int change) 4258 { 4259 struct bonding *bond = netdev_priv(bond_dev); 4260 4261 if (change & IFF_PROMISC) 4262 bond_set_promiscuity(bond, 4263 bond_dev->flags & IFF_PROMISC ? 1 : -1); 4264 4265 if (change & IFF_ALLMULTI) 4266 bond_set_allmulti(bond, 4267 bond_dev->flags & IFF_ALLMULTI ? 1 : -1); 4268 } 4269 4270 static void bond_set_rx_mode(struct net_device *bond_dev) 4271 { 4272 struct bonding *bond = netdev_priv(bond_dev); 4273 struct list_head *iter; 4274 struct slave *slave; 4275 4276 rcu_read_lock(); 4277 if (bond_uses_primary(bond)) { 4278 slave = rcu_dereference(bond->curr_active_slave); 4279 if (slave) { 4280 dev_uc_sync(slave->dev, bond_dev); 4281 dev_mc_sync(slave->dev, bond_dev); 4282 } 4283 } else { 4284 bond_for_each_slave_rcu(bond, slave, iter) { 4285 dev_uc_sync_multiple(slave->dev, bond_dev); 4286 dev_mc_sync_multiple(slave->dev, bond_dev); 4287 } 4288 } 4289 rcu_read_unlock(); 4290 } 4291 4292 static int bond_neigh_init(struct neighbour *n) 4293 { 4294 struct bonding *bond = netdev_priv(n->dev); 4295 const struct net_device_ops *slave_ops; 4296 struct neigh_parms parms; 4297 struct slave *slave; 4298 int ret = 0; 4299 4300 rcu_read_lock(); 4301 slave = bond_first_slave_rcu(bond); 4302 if (!slave) 4303 goto out; 4304 slave_ops = slave->dev->netdev_ops; 4305 if (!slave_ops->ndo_neigh_setup) 4306 goto out; 4307 4308 /* TODO: find another way [1] to implement this. 4309 * Passing a zeroed structure is fragile, 4310 * but at least we do not pass garbage. 4311 * 4312 * [1] One way would be that ndo_neigh_setup() never touch 4313 * struct neigh_parms, but propagate the new neigh_setup() 4314 * back to ___neigh_create() / neigh_parms_alloc() 4315 */ 4316 memset(&parms, 0, sizeof(parms)); 4317 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms); 4318 4319 if (ret) 4320 goto out; 4321 4322 if (parms.neigh_setup) 4323 ret = parms.neigh_setup(n); 4324 out: 4325 rcu_read_unlock(); 4326 return ret; 4327 } 4328 4329 /* The bonding ndo_neigh_setup is called at init time beofre any 4330 * slave exists. So we must declare proxy setup function which will 4331 * be used at run time to resolve the actual slave neigh param setup. 4332 * 4333 * It's also called by master devices (such as vlans) to setup their 4334 * underlying devices. In that case - do nothing, we're already set up from 4335 * our init. 4336 */ 4337 static int bond_neigh_setup(struct net_device *dev, 4338 struct neigh_parms *parms) 4339 { 4340 /* modify only our neigh_parms */ 4341 if (parms->dev == dev) 4342 parms->neigh_setup = bond_neigh_init; 4343 4344 return 0; 4345 } 4346 4347 /* Change the MTU of all of a master's slaves to match the master */ 4348 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) 4349 { 4350 struct bonding *bond = netdev_priv(bond_dev); 4351 struct slave *slave, *rollback_slave; 4352 struct list_head *iter; 4353 int res = 0; 4354 4355 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu); 4356 4357 bond_for_each_slave(bond, slave, iter) { 4358 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n", 4359 slave, slave->dev->netdev_ops->ndo_change_mtu); 4360 4361 res = dev_set_mtu(slave->dev, new_mtu); 4362 4363 if (res) { 4364 /* If we failed to set the slave's mtu to the new value 4365 * we must abort the operation even in ACTIVE_BACKUP 4366 * mode, because if we allow the backup slaves to have 4367 * different mtu values than the active slave we'll 4368 * need to change their mtu when doing a failover. That 4369 * means changing their mtu from timer context, which 4370 * is probably not a good idea. 4371 */ 4372 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n", 4373 res, new_mtu); 4374 goto unwind; 4375 } 4376 } 4377 4378 bond_dev->mtu = new_mtu; 4379 4380 return 0; 4381 4382 unwind: 4383 /* unwind from head to the slave that failed */ 4384 bond_for_each_slave(bond, rollback_slave, iter) { 4385 int tmp_res; 4386 4387 if (rollback_slave == slave) 4388 break; 4389 4390 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu); 4391 if (tmp_res) 4392 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n", 4393 tmp_res); 4394 } 4395 4396 return res; 4397 } 4398 4399 /* Change HW address 4400 * 4401 * Note that many devices must be down to change the HW address, and 4402 * downing the master releases all slaves. We can make bonds full of 4403 * bonding devices to test this, however. 4404 */ 4405 static int bond_set_mac_address(struct net_device *bond_dev, void *addr) 4406 { 4407 struct bonding *bond = netdev_priv(bond_dev); 4408 struct slave *slave, *rollback_slave; 4409 struct sockaddr_storage *ss = addr, tmp_ss; 4410 struct list_head *iter; 4411 int res = 0; 4412 4413 if (BOND_MODE(bond) == BOND_MODE_ALB) 4414 return bond_alb_set_mac_address(bond_dev, addr); 4415 4416 4417 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond); 4418 4419 /* If fail_over_mac is enabled, do nothing and return success. 4420 * Returning an error causes ifenslave to fail. 4421 */ 4422 if (bond->params.fail_over_mac && 4423 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 4424 return 0; 4425 4426 if (!is_valid_ether_addr(ss->__data)) 4427 return -EADDRNOTAVAIL; 4428 4429 bond_for_each_slave(bond, slave, iter) { 4430 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n", 4431 __func__, slave); 4432 res = dev_set_mac_address(slave->dev, addr, NULL); 4433 if (res) { 4434 /* TODO: consider downing the slave 4435 * and retry ? 4436 * User should expect communications 4437 * breakage anyway until ARP finish 4438 * updating, so... 4439 */ 4440 slave_dbg(bond_dev, slave->dev, "%s: err %d\n", 4441 __func__, res); 4442 goto unwind; 4443 } 4444 } 4445 4446 /* success */ 4447 dev_addr_set(bond_dev, ss->__data); 4448 return 0; 4449 4450 unwind: 4451 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len); 4452 tmp_ss.ss_family = bond_dev->type; 4453 4454 /* unwind from head to the slave that failed */ 4455 bond_for_each_slave(bond, rollback_slave, iter) { 4456 int tmp_res; 4457 4458 if (rollback_slave == slave) 4459 break; 4460 4461 tmp_res = dev_set_mac_address(rollback_slave->dev, 4462 (struct sockaddr *)&tmp_ss, NULL); 4463 if (tmp_res) { 4464 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n", 4465 __func__, tmp_res); 4466 } 4467 } 4468 4469 return res; 4470 } 4471 4472 /** 4473 * bond_get_slave_by_id - get xmit slave with slave_id 4474 * @bond: bonding device that is transmitting 4475 * @slave_id: slave id up to slave_cnt-1 through which to transmit 4476 * 4477 * This function tries to get slave with slave_id but in case 4478 * it fails, it tries to find the first available slave for transmission. 4479 */ 4480 static struct slave *bond_get_slave_by_id(struct bonding *bond, 4481 int slave_id) 4482 { 4483 struct list_head *iter; 4484 struct slave *slave; 4485 int i = slave_id; 4486 4487 /* Here we start from the slave with slave_id */ 4488 bond_for_each_slave_rcu(bond, slave, iter) { 4489 if (--i < 0) { 4490 if (bond_slave_can_tx(slave)) 4491 return slave; 4492 } 4493 } 4494 4495 /* Here we start from the first slave up to slave_id */ 4496 i = slave_id; 4497 bond_for_each_slave_rcu(bond, slave, iter) { 4498 if (--i < 0) 4499 break; 4500 if (bond_slave_can_tx(slave)) 4501 return slave; 4502 } 4503 /* no slave that can tx has been found */ 4504 return NULL; 4505 } 4506 4507 /** 4508 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave 4509 * @bond: bonding device to use 4510 * 4511 * Based on the value of the bonding device's packets_per_slave parameter 4512 * this function generates a slave id, which is usually used as the next 4513 * slave to transmit through. 4514 */ 4515 static u32 bond_rr_gen_slave_id(struct bonding *bond) 4516 { 4517 u32 slave_id; 4518 struct reciprocal_value reciprocal_packets_per_slave; 4519 int packets_per_slave = bond->params.packets_per_slave; 4520 4521 switch (packets_per_slave) { 4522 case 0: 4523 slave_id = prandom_u32(); 4524 break; 4525 case 1: 4526 slave_id = this_cpu_inc_return(*bond->rr_tx_counter); 4527 break; 4528 default: 4529 reciprocal_packets_per_slave = 4530 bond->params.reciprocal_packets_per_slave; 4531 slave_id = this_cpu_inc_return(*bond->rr_tx_counter); 4532 slave_id = reciprocal_divide(slave_id, 4533 reciprocal_packets_per_slave); 4534 break; 4535 } 4536 4537 return slave_id; 4538 } 4539 4540 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond, 4541 struct sk_buff *skb) 4542 { 4543 struct slave *slave; 4544 int slave_cnt; 4545 u32 slave_id; 4546 4547 /* Start with the curr_active_slave that joined the bond as the 4548 * default for sending IGMP traffic. For failover purposes one 4549 * needs to maintain some consistency for the interface that will 4550 * send the join/membership reports. The curr_active_slave found 4551 * will send all of this type of traffic. 4552 */ 4553 if (skb->protocol == htons(ETH_P_IP)) { 4554 int noff = skb_network_offset(skb); 4555 struct iphdr *iph; 4556 4557 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph)))) 4558 goto non_igmp; 4559 4560 iph = ip_hdr(skb); 4561 if (iph->protocol == IPPROTO_IGMP) { 4562 slave = rcu_dereference(bond->curr_active_slave); 4563 if (slave) 4564 return slave; 4565 return bond_get_slave_by_id(bond, 0); 4566 } 4567 } 4568 4569 non_igmp: 4570 slave_cnt = READ_ONCE(bond->slave_cnt); 4571 if (likely(slave_cnt)) { 4572 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; 4573 return bond_get_slave_by_id(bond, slave_id); 4574 } 4575 return NULL; 4576 } 4577 4578 static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond, 4579 struct xdp_buff *xdp) 4580 { 4581 struct slave *slave; 4582 int slave_cnt; 4583 u32 slave_id; 4584 const struct ethhdr *eth; 4585 void *data = xdp->data; 4586 4587 if (data + sizeof(struct ethhdr) > xdp->data_end) 4588 goto non_igmp; 4589 4590 eth = (struct ethhdr *)data; 4591 data += sizeof(struct ethhdr); 4592 4593 /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */ 4594 if (eth->h_proto == htons(ETH_P_IP)) { 4595 const struct iphdr *iph; 4596 4597 if (data + sizeof(struct iphdr) > xdp->data_end) 4598 goto non_igmp; 4599 4600 iph = (struct iphdr *)data; 4601 4602 if (iph->protocol == IPPROTO_IGMP) { 4603 slave = rcu_dereference(bond->curr_active_slave); 4604 if (slave) 4605 return slave; 4606 return bond_get_slave_by_id(bond, 0); 4607 } 4608 } 4609 4610 non_igmp: 4611 slave_cnt = READ_ONCE(bond->slave_cnt); 4612 if (likely(slave_cnt)) { 4613 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; 4614 return bond_get_slave_by_id(bond, slave_id); 4615 } 4616 return NULL; 4617 } 4618 4619 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb, 4620 struct net_device *bond_dev) 4621 { 4622 struct bonding *bond = netdev_priv(bond_dev); 4623 struct slave *slave; 4624 4625 slave = bond_xmit_roundrobin_slave_get(bond, skb); 4626 if (likely(slave)) 4627 return bond_dev_queue_xmit(bond, skb, slave->dev); 4628 4629 return bond_tx_drop(bond_dev, skb); 4630 } 4631 4632 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond) 4633 { 4634 return rcu_dereference(bond->curr_active_slave); 4635 } 4636 4637 /* In active-backup mode, we know that bond->curr_active_slave is always valid if 4638 * the bond has a usable interface. 4639 */ 4640 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb, 4641 struct net_device *bond_dev) 4642 { 4643 struct bonding *bond = netdev_priv(bond_dev); 4644 struct slave *slave; 4645 4646 slave = bond_xmit_activebackup_slave_get(bond); 4647 if (slave) 4648 return bond_dev_queue_xmit(bond, skb, slave->dev); 4649 4650 return bond_tx_drop(bond_dev, skb); 4651 } 4652 4653 /* Use this to update slave_array when (a) it's not appropriate to update 4654 * slave_array right away (note that update_slave_array() may sleep) 4655 * and / or (b) RTNL is not held. 4656 */ 4657 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay) 4658 { 4659 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay); 4660 } 4661 4662 /* Slave array work handler. Holds only RTNL */ 4663 static void bond_slave_arr_handler(struct work_struct *work) 4664 { 4665 struct bonding *bond = container_of(work, struct bonding, 4666 slave_arr_work.work); 4667 int ret; 4668 4669 if (!rtnl_trylock()) 4670 goto err; 4671 4672 ret = bond_update_slave_arr(bond, NULL); 4673 rtnl_unlock(); 4674 if (ret) { 4675 pr_warn_ratelimited("Failed to update slave array from WT\n"); 4676 goto err; 4677 } 4678 return; 4679 4680 err: 4681 bond_slave_arr_work_rearm(bond, 1); 4682 } 4683 4684 static void bond_skip_slave(struct bond_up_slave *slaves, 4685 struct slave *skipslave) 4686 { 4687 int idx; 4688 4689 /* Rare situation where caller has asked to skip a specific 4690 * slave but allocation failed (most likely!). BTW this is 4691 * only possible when the call is initiated from 4692 * __bond_release_one(). In this situation; overwrite the 4693 * skipslave entry in the array with the last entry from the 4694 * array to avoid a situation where the xmit path may choose 4695 * this to-be-skipped slave to send a packet out. 4696 */ 4697 for (idx = 0; slaves && idx < slaves->count; idx++) { 4698 if (skipslave == slaves->arr[idx]) { 4699 slaves->arr[idx] = 4700 slaves->arr[slaves->count - 1]; 4701 slaves->count--; 4702 break; 4703 } 4704 } 4705 } 4706 4707 static void bond_set_slave_arr(struct bonding *bond, 4708 struct bond_up_slave *usable_slaves, 4709 struct bond_up_slave *all_slaves) 4710 { 4711 struct bond_up_slave *usable, *all; 4712 4713 usable = rtnl_dereference(bond->usable_slaves); 4714 rcu_assign_pointer(bond->usable_slaves, usable_slaves); 4715 kfree_rcu(usable, rcu); 4716 4717 all = rtnl_dereference(bond->all_slaves); 4718 rcu_assign_pointer(bond->all_slaves, all_slaves); 4719 kfree_rcu(all, rcu); 4720 } 4721 4722 static void bond_reset_slave_arr(struct bonding *bond) 4723 { 4724 struct bond_up_slave *usable, *all; 4725 4726 usable = rtnl_dereference(bond->usable_slaves); 4727 if (usable) { 4728 RCU_INIT_POINTER(bond->usable_slaves, NULL); 4729 kfree_rcu(usable, rcu); 4730 } 4731 4732 all = rtnl_dereference(bond->all_slaves); 4733 if (all) { 4734 RCU_INIT_POINTER(bond->all_slaves, NULL); 4735 kfree_rcu(all, rcu); 4736 } 4737 } 4738 4739 /* Build the usable slaves array in control path for modes that use xmit-hash 4740 * to determine the slave interface - 4741 * (a) BOND_MODE_8023AD 4742 * (b) BOND_MODE_XOR 4743 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0 4744 * 4745 * The caller is expected to hold RTNL only and NO other lock! 4746 */ 4747 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave) 4748 { 4749 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL; 4750 struct slave *slave; 4751 struct list_head *iter; 4752 int agg_id = 0; 4753 int ret = 0; 4754 4755 might_sleep(); 4756 4757 usable_slaves = kzalloc(struct_size(usable_slaves, arr, 4758 bond->slave_cnt), GFP_KERNEL); 4759 all_slaves = kzalloc(struct_size(all_slaves, arr, 4760 bond->slave_cnt), GFP_KERNEL); 4761 if (!usable_slaves || !all_slaves) { 4762 ret = -ENOMEM; 4763 goto out; 4764 } 4765 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 4766 struct ad_info ad_info; 4767 4768 spin_lock_bh(&bond->mode_lock); 4769 if (bond_3ad_get_active_agg_info(bond, &ad_info)) { 4770 spin_unlock_bh(&bond->mode_lock); 4771 pr_debug("bond_3ad_get_active_agg_info failed\n"); 4772 /* No active aggragator means it's not safe to use 4773 * the previous array. 4774 */ 4775 bond_reset_slave_arr(bond); 4776 goto out; 4777 } 4778 spin_unlock_bh(&bond->mode_lock); 4779 agg_id = ad_info.aggregator_id; 4780 } 4781 bond_for_each_slave(bond, slave, iter) { 4782 if (skipslave == slave) 4783 continue; 4784 4785 all_slaves->arr[all_slaves->count++] = slave; 4786 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 4787 struct aggregator *agg; 4788 4789 agg = SLAVE_AD_INFO(slave)->port.aggregator; 4790 if (!agg || agg->aggregator_identifier != agg_id) 4791 continue; 4792 } 4793 if (!bond_slave_can_tx(slave)) 4794 continue; 4795 4796 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n", 4797 usable_slaves->count); 4798 4799 usable_slaves->arr[usable_slaves->count++] = slave; 4800 } 4801 4802 bond_set_slave_arr(bond, usable_slaves, all_slaves); 4803 return ret; 4804 out: 4805 if (ret != 0 && skipslave) { 4806 bond_skip_slave(rtnl_dereference(bond->all_slaves), 4807 skipslave); 4808 bond_skip_slave(rtnl_dereference(bond->usable_slaves), 4809 skipslave); 4810 } 4811 kfree_rcu(all_slaves, rcu); 4812 kfree_rcu(usable_slaves, rcu); 4813 4814 return ret; 4815 } 4816 4817 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond, 4818 struct sk_buff *skb, 4819 struct bond_up_slave *slaves) 4820 { 4821 struct slave *slave; 4822 unsigned int count; 4823 u32 hash; 4824 4825 hash = bond_xmit_hash(bond, skb); 4826 count = slaves ? READ_ONCE(slaves->count) : 0; 4827 if (unlikely(!count)) 4828 return NULL; 4829 4830 slave = slaves->arr[hash % count]; 4831 return slave; 4832 } 4833 4834 static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond, 4835 struct xdp_buff *xdp) 4836 { 4837 struct bond_up_slave *slaves; 4838 unsigned int count; 4839 u32 hash; 4840 4841 hash = bond_xmit_hash_xdp(bond, xdp); 4842 slaves = rcu_dereference(bond->usable_slaves); 4843 count = slaves ? READ_ONCE(slaves->count) : 0; 4844 if (unlikely(!count)) 4845 return NULL; 4846 4847 return slaves->arr[hash % count]; 4848 } 4849 4850 /* Use this Xmit function for 3AD as well as XOR modes. The current 4851 * usable slave array is formed in the control path. The xmit function 4852 * just calculates hash and sends the packet out. 4853 */ 4854 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb, 4855 struct net_device *dev) 4856 { 4857 struct bonding *bond = netdev_priv(dev); 4858 struct bond_up_slave *slaves; 4859 struct slave *slave; 4860 4861 slaves = rcu_dereference(bond->usable_slaves); 4862 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); 4863 if (likely(slave)) 4864 return bond_dev_queue_xmit(bond, skb, slave->dev); 4865 4866 return bond_tx_drop(dev, skb); 4867 } 4868 4869 /* in broadcast mode, we send everything to all usable interfaces. */ 4870 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb, 4871 struct net_device *bond_dev) 4872 { 4873 struct bonding *bond = netdev_priv(bond_dev); 4874 struct slave *slave = NULL; 4875 struct list_head *iter; 4876 4877 bond_for_each_slave_rcu(bond, slave, iter) { 4878 if (bond_is_last_slave(bond, slave)) 4879 break; 4880 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { 4881 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 4882 4883 if (!skb2) { 4884 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n", 4885 bond_dev->name, __func__); 4886 continue; 4887 } 4888 bond_dev_queue_xmit(bond, skb2, slave->dev); 4889 } 4890 } 4891 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) 4892 return bond_dev_queue_xmit(bond, skb, slave->dev); 4893 4894 return bond_tx_drop(bond_dev, skb); 4895 } 4896 4897 /*------------------------- Device initialization ---------------------------*/ 4898 4899 /* Lookup the slave that corresponds to a qid */ 4900 static inline int bond_slave_override(struct bonding *bond, 4901 struct sk_buff *skb) 4902 { 4903 struct slave *slave = NULL; 4904 struct list_head *iter; 4905 4906 if (!skb_rx_queue_recorded(skb)) 4907 return 1; 4908 4909 /* Find out if any slaves have the same mapping as this skb. */ 4910 bond_for_each_slave_rcu(bond, slave, iter) { 4911 if (slave->queue_id == skb_get_queue_mapping(skb)) { 4912 if (bond_slave_is_up(slave) && 4913 slave->link == BOND_LINK_UP) { 4914 bond_dev_queue_xmit(bond, skb, slave->dev); 4915 return 0; 4916 } 4917 /* If the slave isn't UP, use default transmit policy. */ 4918 break; 4919 } 4920 } 4921 4922 return 1; 4923 } 4924 4925 4926 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb, 4927 struct net_device *sb_dev) 4928 { 4929 /* This helper function exists to help dev_pick_tx get the correct 4930 * destination queue. Using a helper function skips a call to 4931 * skb_tx_hash and will put the skbs in the queue we expect on their 4932 * way down to the bonding driver. 4933 */ 4934 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 4935 4936 /* Save the original txq to restore before passing to the driver */ 4937 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb); 4938 4939 if (unlikely(txq >= dev->real_num_tx_queues)) { 4940 do { 4941 txq -= dev->real_num_tx_queues; 4942 } while (txq >= dev->real_num_tx_queues); 4943 } 4944 return txq; 4945 } 4946 4947 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev, 4948 struct sk_buff *skb, 4949 bool all_slaves) 4950 { 4951 struct bonding *bond = netdev_priv(master_dev); 4952 struct bond_up_slave *slaves; 4953 struct slave *slave = NULL; 4954 4955 switch (BOND_MODE(bond)) { 4956 case BOND_MODE_ROUNDROBIN: 4957 slave = bond_xmit_roundrobin_slave_get(bond, skb); 4958 break; 4959 case BOND_MODE_ACTIVEBACKUP: 4960 slave = bond_xmit_activebackup_slave_get(bond); 4961 break; 4962 case BOND_MODE_8023AD: 4963 case BOND_MODE_XOR: 4964 if (all_slaves) 4965 slaves = rcu_dereference(bond->all_slaves); 4966 else 4967 slaves = rcu_dereference(bond->usable_slaves); 4968 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); 4969 break; 4970 case BOND_MODE_BROADCAST: 4971 break; 4972 case BOND_MODE_ALB: 4973 slave = bond_xmit_alb_slave_get(bond, skb); 4974 break; 4975 case BOND_MODE_TLB: 4976 slave = bond_xmit_tlb_slave_get(bond, skb); 4977 break; 4978 default: 4979 /* Should never happen, mode already checked */ 4980 WARN_ONCE(true, "Unknown bonding mode"); 4981 break; 4982 } 4983 4984 if (slave) 4985 return slave->dev; 4986 return NULL; 4987 } 4988 4989 static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow) 4990 { 4991 switch (sk->sk_family) { 4992 #if IS_ENABLED(CONFIG_IPV6) 4993 case AF_INET6: 4994 if (sk->sk_ipv6only || 4995 ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) { 4996 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 4997 flow->addrs.v6addrs.src = inet6_sk(sk)->saddr; 4998 flow->addrs.v6addrs.dst = sk->sk_v6_daddr; 4999 break; 5000 } 5001 fallthrough; 5002 #endif 5003 default: /* AF_INET */ 5004 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 5005 flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr; 5006 flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr; 5007 break; 5008 } 5009 5010 flow->ports.src = inet_sk(sk)->inet_sport; 5011 flow->ports.dst = inet_sk(sk)->inet_dport; 5012 } 5013 5014 /** 5015 * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields 5016 * @sk: socket to use for headers 5017 * 5018 * This function will extract the necessary field from the socket and use 5019 * them to generate a hash based on the LAYER34 xmit_policy. 5020 * Assumes that sk is a TCP or UDP socket. 5021 */ 5022 static u32 bond_sk_hash_l34(struct sock *sk) 5023 { 5024 struct flow_keys flow; 5025 u32 hash; 5026 5027 bond_sk_to_flow(sk, &flow); 5028 5029 /* L4 */ 5030 memcpy(&hash, &flow.ports.ports, sizeof(hash)); 5031 /* L3 */ 5032 return bond_ip_hash(hash, &flow); 5033 } 5034 5035 static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond, 5036 struct sock *sk) 5037 { 5038 struct bond_up_slave *slaves; 5039 struct slave *slave; 5040 unsigned int count; 5041 u32 hash; 5042 5043 slaves = rcu_dereference(bond->usable_slaves); 5044 count = slaves ? READ_ONCE(slaves->count) : 0; 5045 if (unlikely(!count)) 5046 return NULL; 5047 5048 hash = bond_sk_hash_l34(sk); 5049 slave = slaves->arr[hash % count]; 5050 5051 return slave->dev; 5052 } 5053 5054 static struct net_device *bond_sk_get_lower_dev(struct net_device *dev, 5055 struct sock *sk) 5056 { 5057 struct bonding *bond = netdev_priv(dev); 5058 struct net_device *lower = NULL; 5059 5060 rcu_read_lock(); 5061 if (bond_sk_check(bond)) 5062 lower = __bond_sk_get_lower_dev(bond, sk); 5063 rcu_read_unlock(); 5064 5065 return lower; 5066 } 5067 5068 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5069 static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb, 5070 struct net_device *dev) 5071 { 5072 if (likely(bond_get_slave_by_dev(bond, tls_get_ctx(skb->sk)->netdev))) 5073 return bond_dev_queue_xmit(bond, skb, tls_get_ctx(skb->sk)->netdev); 5074 return bond_tx_drop(dev, skb); 5075 } 5076 #endif 5077 5078 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 5079 { 5080 struct bonding *bond = netdev_priv(dev); 5081 5082 if (bond_should_override_tx_queue(bond) && 5083 !bond_slave_override(bond, skb)) 5084 return NETDEV_TX_OK; 5085 5086 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5087 if (skb->sk && tls_is_sk_tx_device_offloaded(skb->sk)) 5088 return bond_tls_device_xmit(bond, skb, dev); 5089 #endif 5090 5091 switch (BOND_MODE(bond)) { 5092 case BOND_MODE_ROUNDROBIN: 5093 return bond_xmit_roundrobin(skb, dev); 5094 case BOND_MODE_ACTIVEBACKUP: 5095 return bond_xmit_activebackup(skb, dev); 5096 case BOND_MODE_8023AD: 5097 case BOND_MODE_XOR: 5098 return bond_3ad_xor_xmit(skb, dev); 5099 case BOND_MODE_BROADCAST: 5100 return bond_xmit_broadcast(skb, dev); 5101 case BOND_MODE_ALB: 5102 return bond_alb_xmit(skb, dev); 5103 case BOND_MODE_TLB: 5104 return bond_tlb_xmit(skb, dev); 5105 default: 5106 /* Should never happen, mode already checked */ 5107 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond)); 5108 WARN_ON_ONCE(1); 5109 return bond_tx_drop(dev, skb); 5110 } 5111 } 5112 5113 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 5114 { 5115 struct bonding *bond = netdev_priv(dev); 5116 netdev_tx_t ret = NETDEV_TX_OK; 5117 5118 /* If we risk deadlock from transmitting this in the 5119 * netpoll path, tell netpoll to queue the frame for later tx 5120 */ 5121 if (unlikely(is_netpoll_tx_blocked(dev))) 5122 return NETDEV_TX_BUSY; 5123 5124 rcu_read_lock(); 5125 if (bond_has_slaves(bond)) 5126 ret = __bond_start_xmit(skb, dev); 5127 else 5128 ret = bond_tx_drop(dev, skb); 5129 rcu_read_unlock(); 5130 5131 return ret; 5132 } 5133 5134 static struct net_device * 5135 bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp) 5136 { 5137 struct bonding *bond = netdev_priv(bond_dev); 5138 struct slave *slave; 5139 5140 /* Caller needs to hold rcu_read_lock() */ 5141 5142 switch (BOND_MODE(bond)) { 5143 case BOND_MODE_ROUNDROBIN: 5144 slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp); 5145 break; 5146 5147 case BOND_MODE_ACTIVEBACKUP: 5148 slave = bond_xmit_activebackup_slave_get(bond); 5149 break; 5150 5151 case BOND_MODE_8023AD: 5152 case BOND_MODE_XOR: 5153 slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp); 5154 break; 5155 5156 default: 5157 /* Should never happen. Mode guarded by bond_xdp_check() */ 5158 netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n", BOND_MODE(bond)); 5159 WARN_ON_ONCE(1); 5160 return NULL; 5161 } 5162 5163 if (slave) 5164 return slave->dev; 5165 5166 return NULL; 5167 } 5168 5169 static int bond_xdp_xmit(struct net_device *bond_dev, 5170 int n, struct xdp_frame **frames, u32 flags) 5171 { 5172 int nxmit, err = -ENXIO; 5173 5174 rcu_read_lock(); 5175 5176 for (nxmit = 0; nxmit < n; nxmit++) { 5177 struct xdp_frame *frame = frames[nxmit]; 5178 struct xdp_frame *frames1[] = {frame}; 5179 struct net_device *slave_dev; 5180 struct xdp_buff xdp; 5181 5182 xdp_convert_frame_to_buff(frame, &xdp); 5183 5184 slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp); 5185 if (!slave_dev) { 5186 err = -ENXIO; 5187 break; 5188 } 5189 5190 err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags); 5191 if (err < 1) 5192 break; 5193 } 5194 5195 rcu_read_unlock(); 5196 5197 /* If error happened on the first frame then we can pass the error up, otherwise 5198 * report the number of frames that were xmitted. 5199 */ 5200 if (err < 0) 5201 return (nxmit == 0 ? err : nxmit); 5202 5203 return nxmit; 5204 } 5205 5206 static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog, 5207 struct netlink_ext_ack *extack) 5208 { 5209 struct bonding *bond = netdev_priv(dev); 5210 struct list_head *iter; 5211 struct slave *slave, *rollback_slave; 5212 struct bpf_prog *old_prog; 5213 struct netdev_bpf xdp = { 5214 .command = XDP_SETUP_PROG, 5215 .flags = 0, 5216 .prog = prog, 5217 .extack = extack, 5218 }; 5219 int err; 5220 5221 ASSERT_RTNL(); 5222 5223 if (!bond_xdp_check(bond)) 5224 return -EOPNOTSUPP; 5225 5226 old_prog = bond->xdp_prog; 5227 bond->xdp_prog = prog; 5228 5229 bond_for_each_slave(bond, slave, iter) { 5230 struct net_device *slave_dev = slave->dev; 5231 5232 if (!slave_dev->netdev_ops->ndo_bpf || 5233 !slave_dev->netdev_ops->ndo_xdp_xmit) { 5234 SLAVE_NL_ERR(dev, slave_dev, extack, 5235 "Slave device does not support XDP"); 5236 err = -EOPNOTSUPP; 5237 goto err; 5238 } 5239 5240 if (dev_xdp_prog_count(slave_dev) > 0) { 5241 SLAVE_NL_ERR(dev, slave_dev, extack, 5242 "Slave has XDP program loaded, please unload before enslaving"); 5243 err = -EOPNOTSUPP; 5244 goto err; 5245 } 5246 5247 err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); 5248 if (err < 0) { 5249 /* ndo_bpf() sets extack error message */ 5250 slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err); 5251 goto err; 5252 } 5253 if (prog) 5254 bpf_prog_inc(prog); 5255 } 5256 5257 if (prog) { 5258 static_branch_inc(&bpf_master_redirect_enabled_key); 5259 } else if (old_prog) { 5260 bpf_prog_put(old_prog); 5261 static_branch_dec(&bpf_master_redirect_enabled_key); 5262 } 5263 5264 return 0; 5265 5266 err: 5267 /* unwind the program changes */ 5268 bond->xdp_prog = old_prog; 5269 xdp.prog = old_prog; 5270 xdp.extack = NULL; /* do not overwrite original error */ 5271 5272 bond_for_each_slave(bond, rollback_slave, iter) { 5273 struct net_device *slave_dev = rollback_slave->dev; 5274 int err_unwind; 5275 5276 if (slave == rollback_slave) 5277 break; 5278 5279 err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); 5280 if (err_unwind < 0) 5281 slave_err(dev, slave_dev, 5282 "Error %d when unwinding XDP program change\n", err_unwind); 5283 else if (xdp.prog) 5284 bpf_prog_inc(xdp.prog); 5285 } 5286 return err; 5287 } 5288 5289 static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp) 5290 { 5291 switch (xdp->command) { 5292 case XDP_SETUP_PROG: 5293 return bond_xdp_set(dev, xdp->prog, xdp->extack); 5294 default: 5295 return -EINVAL; 5296 } 5297 } 5298 5299 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed) 5300 { 5301 if (speed == 0 || speed == SPEED_UNKNOWN) 5302 speed = slave->speed; 5303 else 5304 speed = min(speed, slave->speed); 5305 5306 return speed; 5307 } 5308 5309 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev, 5310 struct ethtool_link_ksettings *cmd) 5311 { 5312 struct bonding *bond = netdev_priv(bond_dev); 5313 struct list_head *iter; 5314 struct slave *slave; 5315 u32 speed = 0; 5316 5317 cmd->base.duplex = DUPLEX_UNKNOWN; 5318 cmd->base.port = PORT_OTHER; 5319 5320 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we 5321 * do not need to check mode. Though link speed might not represent 5322 * the true receive or transmit bandwidth (not all modes are symmetric) 5323 * this is an accurate maximum. 5324 */ 5325 bond_for_each_slave(bond, slave, iter) { 5326 if (bond_slave_can_tx(slave)) { 5327 if (slave->speed != SPEED_UNKNOWN) { 5328 if (BOND_MODE(bond) == BOND_MODE_BROADCAST) 5329 speed = bond_mode_bcast_speed(slave, 5330 speed); 5331 else 5332 speed += slave->speed; 5333 } 5334 if (cmd->base.duplex == DUPLEX_UNKNOWN && 5335 slave->duplex != DUPLEX_UNKNOWN) 5336 cmd->base.duplex = slave->duplex; 5337 } 5338 } 5339 cmd->base.speed = speed ? : SPEED_UNKNOWN; 5340 5341 return 0; 5342 } 5343 5344 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev, 5345 struct ethtool_drvinfo *drvinfo) 5346 { 5347 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); 5348 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d", 5349 BOND_ABI_VERSION); 5350 } 5351 5352 static int bond_ethtool_get_ts_info(struct net_device *bond_dev, 5353 struct ethtool_ts_info *info) 5354 { 5355 struct bonding *bond = netdev_priv(bond_dev); 5356 const struct ethtool_ops *ops; 5357 struct net_device *real_dev; 5358 struct phy_device *phydev; 5359 5360 rcu_read_lock(); 5361 real_dev = bond_option_active_slave_get_rcu(bond); 5362 rcu_read_unlock(); 5363 if (real_dev) { 5364 ops = real_dev->ethtool_ops; 5365 phydev = real_dev->phydev; 5366 5367 if (phy_has_tsinfo(phydev)) { 5368 return phy_ts_info(phydev, info); 5369 } else if (ops->get_ts_info) { 5370 return ops->get_ts_info(real_dev, info); 5371 } 5372 } 5373 5374 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | 5375 SOF_TIMESTAMPING_SOFTWARE; 5376 info->phc_index = -1; 5377 5378 return 0; 5379 } 5380 5381 static const struct ethtool_ops bond_ethtool_ops = { 5382 .get_drvinfo = bond_ethtool_get_drvinfo, 5383 .get_link = ethtool_op_get_link, 5384 .get_link_ksettings = bond_ethtool_get_link_ksettings, 5385 .get_ts_info = bond_ethtool_get_ts_info, 5386 }; 5387 5388 static const struct net_device_ops bond_netdev_ops = { 5389 .ndo_init = bond_init, 5390 .ndo_uninit = bond_uninit, 5391 .ndo_open = bond_open, 5392 .ndo_stop = bond_close, 5393 .ndo_start_xmit = bond_start_xmit, 5394 .ndo_select_queue = bond_select_queue, 5395 .ndo_get_stats64 = bond_get_stats, 5396 .ndo_eth_ioctl = bond_eth_ioctl, 5397 .ndo_siocbond = bond_do_ioctl, 5398 .ndo_siocdevprivate = bond_siocdevprivate, 5399 .ndo_change_rx_flags = bond_change_rx_flags, 5400 .ndo_set_rx_mode = bond_set_rx_mode, 5401 .ndo_change_mtu = bond_change_mtu, 5402 .ndo_set_mac_address = bond_set_mac_address, 5403 .ndo_neigh_setup = bond_neigh_setup, 5404 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid, 5405 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid, 5406 #ifdef CONFIG_NET_POLL_CONTROLLER 5407 .ndo_netpoll_setup = bond_netpoll_setup, 5408 .ndo_netpoll_cleanup = bond_netpoll_cleanup, 5409 .ndo_poll_controller = bond_poll_controller, 5410 #endif 5411 .ndo_add_slave = bond_enslave, 5412 .ndo_del_slave = bond_release, 5413 .ndo_fix_features = bond_fix_features, 5414 .ndo_features_check = passthru_features_check, 5415 .ndo_get_xmit_slave = bond_xmit_get_slave, 5416 .ndo_sk_get_lower_dev = bond_sk_get_lower_dev, 5417 .ndo_bpf = bond_xdp, 5418 .ndo_xdp_xmit = bond_xdp_xmit, 5419 .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave, 5420 }; 5421 5422 static const struct device_type bond_type = { 5423 .name = "bond", 5424 }; 5425 5426 static void bond_destructor(struct net_device *bond_dev) 5427 { 5428 struct bonding *bond = netdev_priv(bond_dev); 5429 5430 if (bond->wq) 5431 destroy_workqueue(bond->wq); 5432 5433 if (bond->rr_tx_counter) 5434 free_percpu(bond->rr_tx_counter); 5435 } 5436 5437 void bond_setup(struct net_device *bond_dev) 5438 { 5439 struct bonding *bond = netdev_priv(bond_dev); 5440 5441 spin_lock_init(&bond->mode_lock); 5442 bond->params = bonding_defaults; 5443 5444 /* Initialize pointers */ 5445 bond->dev = bond_dev; 5446 5447 /* Initialize the device entry points */ 5448 ether_setup(bond_dev); 5449 bond_dev->max_mtu = ETH_MAX_MTU; 5450 bond_dev->netdev_ops = &bond_netdev_ops; 5451 bond_dev->ethtool_ops = &bond_ethtool_ops; 5452 5453 bond_dev->needs_free_netdev = true; 5454 bond_dev->priv_destructor = bond_destructor; 5455 5456 SET_NETDEV_DEVTYPE(bond_dev, &bond_type); 5457 5458 /* Initialize the device options */ 5459 bond_dev->flags |= IFF_MASTER; 5460 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE; 5461 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 5462 5463 #ifdef CONFIG_XFRM_OFFLOAD 5464 /* set up xfrm device ops (only supported in active-backup right now) */ 5465 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops; 5466 INIT_LIST_HEAD(&bond->ipsec_list); 5467 spin_lock_init(&bond->ipsec_lock); 5468 #endif /* CONFIG_XFRM_OFFLOAD */ 5469 5470 /* don't acquire bond device's netif_tx_lock when transmitting */ 5471 bond_dev->features |= NETIF_F_LLTX; 5472 5473 /* By default, we declare the bond to be fully 5474 * VLAN hardware accelerated capable. Special 5475 * care is taken in the various xmit functions 5476 * when there are slaves that are not hw accel 5477 * capable 5478 */ 5479 5480 /* Don't allow bond devices to change network namespaces. */ 5481 bond_dev->features |= NETIF_F_NETNS_LOCAL; 5482 5483 bond_dev->hw_features = BOND_VLAN_FEATURES | 5484 NETIF_F_HW_VLAN_CTAG_RX | 5485 NETIF_F_HW_VLAN_CTAG_FILTER; 5486 5487 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; 5488 bond_dev->features |= bond_dev->hw_features; 5489 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 5490 #ifdef CONFIG_XFRM_OFFLOAD 5491 bond_dev->hw_features |= BOND_XFRM_FEATURES; 5492 /* Only enable XFRM features if this is an active-backup config */ 5493 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 5494 bond_dev->features |= BOND_XFRM_FEATURES; 5495 #endif /* CONFIG_XFRM_OFFLOAD */ 5496 #if IS_ENABLED(CONFIG_TLS_DEVICE) 5497 if (bond_sk_check(bond)) 5498 bond_dev->features |= BOND_TLS_FEATURES; 5499 #endif 5500 } 5501 5502 /* Destroy a bonding device. 5503 * Must be under rtnl_lock when this function is called. 5504 */ 5505 static void bond_uninit(struct net_device *bond_dev) 5506 { 5507 struct bonding *bond = netdev_priv(bond_dev); 5508 struct bond_up_slave *usable, *all; 5509 struct list_head *iter; 5510 struct slave *slave; 5511 5512 bond_netpoll_cleanup(bond_dev); 5513 5514 /* Release the bonded slaves */ 5515 bond_for_each_slave(bond, slave, iter) 5516 __bond_release_one(bond_dev, slave->dev, true, true); 5517 netdev_info(bond_dev, "Released all slaves\n"); 5518 5519 usable = rtnl_dereference(bond->usable_slaves); 5520 if (usable) { 5521 RCU_INIT_POINTER(bond->usable_slaves, NULL); 5522 kfree_rcu(usable, rcu); 5523 } 5524 5525 all = rtnl_dereference(bond->all_slaves); 5526 if (all) { 5527 RCU_INIT_POINTER(bond->all_slaves, NULL); 5528 kfree_rcu(all, rcu); 5529 } 5530 5531 list_del(&bond->bond_list); 5532 5533 bond_debug_unregister(bond); 5534 } 5535 5536 /*------------------------- Module initialization ---------------------------*/ 5537 5538 static int bond_check_params(struct bond_params *params) 5539 { 5540 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i; 5541 struct bond_opt_value newval; 5542 const struct bond_opt_value *valptr; 5543 int arp_all_targets_value = 0; 5544 u16 ad_actor_sys_prio = 0; 5545 u16 ad_user_port_key = 0; 5546 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 }; 5547 int arp_ip_count; 5548 int bond_mode = BOND_MODE_ROUNDROBIN; 5549 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2; 5550 int lacp_fast = 0; 5551 int tlb_dynamic_lb; 5552 5553 /* Convert string parameters. */ 5554 if (mode) { 5555 bond_opt_initstr(&newval, mode); 5556 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval); 5557 if (!valptr) { 5558 pr_err("Error: Invalid bonding mode \"%s\"\n", mode); 5559 return -EINVAL; 5560 } 5561 bond_mode = valptr->value; 5562 } 5563 5564 if (xmit_hash_policy) { 5565 if (bond_mode == BOND_MODE_ROUNDROBIN || 5566 bond_mode == BOND_MODE_ACTIVEBACKUP || 5567 bond_mode == BOND_MODE_BROADCAST) { 5568 pr_info("xmit_hash_policy param is irrelevant in mode %s\n", 5569 bond_mode_name(bond_mode)); 5570 } else { 5571 bond_opt_initstr(&newval, xmit_hash_policy); 5572 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH), 5573 &newval); 5574 if (!valptr) { 5575 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n", 5576 xmit_hash_policy); 5577 return -EINVAL; 5578 } 5579 xmit_hashtype = valptr->value; 5580 } 5581 } 5582 5583 if (lacp_rate) { 5584 if (bond_mode != BOND_MODE_8023AD) { 5585 pr_info("lacp_rate param is irrelevant in mode %s\n", 5586 bond_mode_name(bond_mode)); 5587 } else { 5588 bond_opt_initstr(&newval, lacp_rate); 5589 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE), 5590 &newval); 5591 if (!valptr) { 5592 pr_err("Error: Invalid lacp rate \"%s\"\n", 5593 lacp_rate); 5594 return -EINVAL; 5595 } 5596 lacp_fast = valptr->value; 5597 } 5598 } 5599 5600 if (ad_select) { 5601 bond_opt_initstr(&newval, ad_select); 5602 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT), 5603 &newval); 5604 if (!valptr) { 5605 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select); 5606 return -EINVAL; 5607 } 5608 params->ad_select = valptr->value; 5609 if (bond_mode != BOND_MODE_8023AD) 5610 pr_warn("ad_select param only affects 802.3ad mode\n"); 5611 } else { 5612 params->ad_select = BOND_AD_STABLE; 5613 } 5614 5615 if (max_bonds < 0) { 5616 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n", 5617 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS); 5618 max_bonds = BOND_DEFAULT_MAX_BONDS; 5619 } 5620 5621 if (miimon < 0) { 5622 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5623 miimon, INT_MAX); 5624 miimon = 0; 5625 } 5626 5627 if (updelay < 0) { 5628 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5629 updelay, INT_MAX); 5630 updelay = 0; 5631 } 5632 5633 if (downdelay < 0) { 5634 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5635 downdelay, INT_MAX); 5636 downdelay = 0; 5637 } 5638 5639 if ((use_carrier != 0) && (use_carrier != 1)) { 5640 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n", 5641 use_carrier); 5642 use_carrier = 1; 5643 } 5644 5645 if (num_peer_notif < 0 || num_peer_notif > 255) { 5646 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n", 5647 num_peer_notif); 5648 num_peer_notif = 1; 5649 } 5650 5651 /* reset values for 802.3ad/TLB/ALB */ 5652 if (!bond_mode_uses_arp(bond_mode)) { 5653 if (!miimon) { 5654 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"); 5655 pr_warn("Forcing miimon to 100msec\n"); 5656 miimon = BOND_DEFAULT_MIIMON; 5657 } 5658 } 5659 5660 if (tx_queues < 1 || tx_queues > 255) { 5661 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n", 5662 tx_queues, BOND_DEFAULT_TX_QUEUES); 5663 tx_queues = BOND_DEFAULT_TX_QUEUES; 5664 } 5665 5666 if ((all_slaves_active != 0) && (all_slaves_active != 1)) { 5667 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n", 5668 all_slaves_active); 5669 all_slaves_active = 0; 5670 } 5671 5672 if (resend_igmp < 0 || resend_igmp > 255) { 5673 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n", 5674 resend_igmp, BOND_DEFAULT_RESEND_IGMP); 5675 resend_igmp = BOND_DEFAULT_RESEND_IGMP; 5676 } 5677 5678 bond_opt_initval(&newval, packets_per_slave); 5679 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) { 5680 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n", 5681 packets_per_slave, USHRT_MAX); 5682 packets_per_slave = 1; 5683 } 5684 5685 if (bond_mode == BOND_MODE_ALB) { 5686 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", 5687 updelay); 5688 } 5689 5690 if (!miimon) { 5691 if (updelay || downdelay) { 5692 /* just warn the user the up/down delay will have 5693 * no effect since miimon is zero... 5694 */ 5695 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", 5696 updelay, downdelay); 5697 } 5698 } else { 5699 /* don't allow arp monitoring */ 5700 if (arp_interval) { 5701 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n", 5702 miimon, arp_interval); 5703 arp_interval = 0; 5704 } 5705 5706 if ((updelay % miimon) != 0) { 5707 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", 5708 updelay, miimon, (updelay / miimon) * miimon); 5709 } 5710 5711 updelay /= miimon; 5712 5713 if ((downdelay % miimon) != 0) { 5714 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n", 5715 downdelay, miimon, 5716 (downdelay / miimon) * miimon); 5717 } 5718 5719 downdelay /= miimon; 5720 } 5721 5722 if (arp_interval < 0) { 5723 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n", 5724 arp_interval, INT_MAX); 5725 arp_interval = 0; 5726 } 5727 5728 for (arp_ip_count = 0, i = 0; 5729 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) { 5730 __be32 ip; 5731 5732 /* not a complete check, but good enough to catch mistakes */ 5733 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) || 5734 !bond_is_ip_target_ok(ip)) { 5735 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n", 5736 arp_ip_target[i]); 5737 arp_interval = 0; 5738 } else { 5739 if (bond_get_targets_ip(arp_target, ip) == -1) 5740 arp_target[arp_ip_count++] = ip; 5741 else 5742 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n", 5743 &ip); 5744 } 5745 } 5746 5747 if (arp_interval && !arp_ip_count) { 5748 /* don't allow arping if no arp_ip_target given... */ 5749 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n", 5750 arp_interval); 5751 arp_interval = 0; 5752 } 5753 5754 if (arp_validate) { 5755 if (!arp_interval) { 5756 pr_err("arp_validate requires arp_interval\n"); 5757 return -EINVAL; 5758 } 5759 5760 bond_opt_initstr(&newval, arp_validate); 5761 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE), 5762 &newval); 5763 if (!valptr) { 5764 pr_err("Error: invalid arp_validate \"%s\"\n", 5765 arp_validate); 5766 return -EINVAL; 5767 } 5768 arp_validate_value = valptr->value; 5769 } else { 5770 arp_validate_value = 0; 5771 } 5772 5773 if (arp_all_targets) { 5774 bond_opt_initstr(&newval, arp_all_targets); 5775 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS), 5776 &newval); 5777 if (!valptr) { 5778 pr_err("Error: invalid arp_all_targets_value \"%s\"\n", 5779 arp_all_targets); 5780 arp_all_targets_value = 0; 5781 } else { 5782 arp_all_targets_value = valptr->value; 5783 } 5784 } 5785 5786 if (miimon) { 5787 pr_info("MII link monitoring set to %d ms\n", miimon); 5788 } else if (arp_interval) { 5789 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE, 5790 arp_validate_value); 5791 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):", 5792 arp_interval, valptr->string, arp_ip_count); 5793 5794 for (i = 0; i < arp_ip_count; i++) 5795 pr_cont(" %s", arp_ip_target[i]); 5796 5797 pr_cont("\n"); 5798 5799 } else if (max_bonds) { 5800 /* miimon and arp_interval not set, we need one so things 5801 * work as expected, see bonding.txt for details 5802 */ 5803 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"); 5804 } 5805 5806 if (primary && !bond_mode_uses_primary(bond_mode)) { 5807 /* currently, using a primary only makes sense 5808 * in active backup, TLB or ALB modes 5809 */ 5810 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n", 5811 primary, bond_mode_name(bond_mode)); 5812 primary = NULL; 5813 } 5814 5815 if (primary && primary_reselect) { 5816 bond_opt_initstr(&newval, primary_reselect); 5817 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT), 5818 &newval); 5819 if (!valptr) { 5820 pr_err("Error: Invalid primary_reselect \"%s\"\n", 5821 primary_reselect); 5822 return -EINVAL; 5823 } 5824 primary_reselect_value = valptr->value; 5825 } else { 5826 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS; 5827 } 5828 5829 if (fail_over_mac) { 5830 bond_opt_initstr(&newval, fail_over_mac); 5831 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC), 5832 &newval); 5833 if (!valptr) { 5834 pr_err("Error: invalid fail_over_mac \"%s\"\n", 5835 fail_over_mac); 5836 return -EINVAL; 5837 } 5838 fail_over_mac_value = valptr->value; 5839 if (bond_mode != BOND_MODE_ACTIVEBACKUP) 5840 pr_warn("Warning: fail_over_mac only affects active-backup mode\n"); 5841 } else { 5842 fail_over_mac_value = BOND_FOM_NONE; 5843 } 5844 5845 bond_opt_initstr(&newval, "default"); 5846 valptr = bond_opt_parse( 5847 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO), 5848 &newval); 5849 if (!valptr) { 5850 pr_err("Error: No ad_actor_sys_prio default value"); 5851 return -EINVAL; 5852 } 5853 ad_actor_sys_prio = valptr->value; 5854 5855 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY), 5856 &newval); 5857 if (!valptr) { 5858 pr_err("Error: No ad_user_port_key default value"); 5859 return -EINVAL; 5860 } 5861 ad_user_port_key = valptr->value; 5862 5863 bond_opt_initstr(&newval, "default"); 5864 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval); 5865 if (!valptr) { 5866 pr_err("Error: No tlb_dynamic_lb default value"); 5867 return -EINVAL; 5868 } 5869 tlb_dynamic_lb = valptr->value; 5870 5871 if (lp_interval == 0) { 5872 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n", 5873 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL); 5874 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 5875 } 5876 5877 /* fill params struct with the proper values */ 5878 params->mode = bond_mode; 5879 params->xmit_policy = xmit_hashtype; 5880 params->miimon = miimon; 5881 params->num_peer_notif = num_peer_notif; 5882 params->arp_interval = arp_interval; 5883 params->arp_validate = arp_validate_value; 5884 params->arp_all_targets = arp_all_targets_value; 5885 params->missed_max = 2; 5886 params->updelay = updelay; 5887 params->downdelay = downdelay; 5888 params->peer_notif_delay = 0; 5889 params->use_carrier = use_carrier; 5890 params->lacp_active = 1; 5891 params->lacp_fast = lacp_fast; 5892 params->primary[0] = 0; 5893 params->primary_reselect = primary_reselect_value; 5894 params->fail_over_mac = fail_over_mac_value; 5895 params->tx_queues = tx_queues; 5896 params->all_slaves_active = all_slaves_active; 5897 params->resend_igmp = resend_igmp; 5898 params->min_links = min_links; 5899 params->lp_interval = lp_interval; 5900 params->packets_per_slave = packets_per_slave; 5901 params->tlb_dynamic_lb = tlb_dynamic_lb; 5902 params->ad_actor_sys_prio = ad_actor_sys_prio; 5903 eth_zero_addr(params->ad_actor_system); 5904 params->ad_user_port_key = ad_user_port_key; 5905 if (packets_per_slave > 0) { 5906 params->reciprocal_packets_per_slave = 5907 reciprocal_value(packets_per_slave); 5908 } else { 5909 /* reciprocal_packets_per_slave is unused if 5910 * packets_per_slave is 0 or 1, just initialize it 5911 */ 5912 params->reciprocal_packets_per_slave = 5913 (struct reciprocal_value) { 0 }; 5914 } 5915 5916 if (primary) 5917 strscpy_pad(params->primary, primary, sizeof(params->primary)); 5918 5919 memcpy(params->arp_targets, arp_target, sizeof(arp_target)); 5920 5921 return 0; 5922 } 5923 5924 /* Called from registration process */ 5925 static int bond_init(struct net_device *bond_dev) 5926 { 5927 struct bonding *bond = netdev_priv(bond_dev); 5928 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id); 5929 5930 netdev_dbg(bond_dev, "Begin bond_init\n"); 5931 5932 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM); 5933 if (!bond->wq) 5934 return -ENOMEM; 5935 5936 if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN) { 5937 bond->rr_tx_counter = alloc_percpu(u32); 5938 if (!bond->rr_tx_counter) { 5939 destroy_workqueue(bond->wq); 5940 bond->wq = NULL; 5941 return -ENOMEM; 5942 } 5943 } 5944 5945 spin_lock_init(&bond->stats_lock); 5946 netdev_lockdep_set_classes(bond_dev); 5947 5948 list_add_tail(&bond->bond_list, &bn->dev_list); 5949 5950 bond_prepare_sysfs_group(bond); 5951 5952 bond_debug_register(bond); 5953 5954 /* Ensure valid dev_addr */ 5955 if (is_zero_ether_addr(bond_dev->dev_addr) && 5956 bond_dev->addr_assign_type == NET_ADDR_PERM) 5957 eth_hw_addr_random(bond_dev); 5958 5959 return 0; 5960 } 5961 5962 unsigned int bond_get_num_tx_queues(void) 5963 { 5964 return tx_queues; 5965 } 5966 5967 /* Create a new bond based on the specified name and bonding parameters. 5968 * If name is NULL, obtain a suitable "bond%d" name for us. 5969 * Caller must NOT hold rtnl_lock; we need to release it here before we 5970 * set up our sysfs entries. 5971 */ 5972 int bond_create(struct net *net, const char *name) 5973 { 5974 struct net_device *bond_dev; 5975 struct bonding *bond; 5976 struct alb_bond_info *bond_info; 5977 int res; 5978 5979 rtnl_lock(); 5980 5981 bond_dev = alloc_netdev_mq(sizeof(struct bonding), 5982 name ? name : "bond%d", NET_NAME_UNKNOWN, 5983 bond_setup, tx_queues); 5984 if (!bond_dev) { 5985 pr_err("%s: eek! can't alloc netdev!\n", name); 5986 rtnl_unlock(); 5987 return -ENOMEM; 5988 } 5989 5990 /* 5991 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX. 5992 * It is set to 0 by default which is wrong. 5993 */ 5994 bond = netdev_priv(bond_dev); 5995 bond_info = &(BOND_ALB_INFO(bond)); 5996 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; 5997 5998 dev_net_set(bond_dev, net); 5999 bond_dev->rtnl_link_ops = &bond_link_ops; 6000 6001 res = register_netdevice(bond_dev); 6002 if (res < 0) { 6003 free_netdev(bond_dev); 6004 rtnl_unlock(); 6005 6006 return res; 6007 } 6008 6009 netif_carrier_off(bond_dev); 6010 6011 bond_work_init_all(bond); 6012 6013 rtnl_unlock(); 6014 return 0; 6015 } 6016 6017 static int __net_init bond_net_init(struct net *net) 6018 { 6019 struct bond_net *bn = net_generic(net, bond_net_id); 6020 6021 bn->net = net; 6022 INIT_LIST_HEAD(&bn->dev_list); 6023 6024 bond_create_proc_dir(bn); 6025 bond_create_sysfs(bn); 6026 6027 return 0; 6028 } 6029 6030 static void __net_exit bond_net_exit(struct net *net) 6031 { 6032 struct bond_net *bn = net_generic(net, bond_net_id); 6033 struct bonding *bond, *tmp_bond; 6034 LIST_HEAD(list); 6035 6036 bond_destroy_sysfs(bn); 6037 6038 /* Kill off any bonds created after unregistering bond rtnl ops */ 6039 rtnl_lock(); 6040 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list) 6041 unregister_netdevice_queue(bond->dev, &list); 6042 unregister_netdevice_many(&list); 6043 rtnl_unlock(); 6044 6045 bond_destroy_proc_dir(bn); 6046 } 6047 6048 static struct pernet_operations bond_net_ops = { 6049 .init = bond_net_init, 6050 .exit = bond_net_exit, 6051 .id = &bond_net_id, 6052 .size = sizeof(struct bond_net), 6053 }; 6054 6055 static int __init bonding_init(void) 6056 { 6057 int i; 6058 int res; 6059 6060 res = bond_check_params(&bonding_defaults); 6061 if (res) 6062 goto out; 6063 6064 res = register_pernet_subsys(&bond_net_ops); 6065 if (res) 6066 goto out; 6067 6068 res = bond_netlink_init(); 6069 if (res) 6070 goto err_link; 6071 6072 bond_create_debugfs(); 6073 6074 for (i = 0; i < max_bonds; i++) { 6075 res = bond_create(&init_net, NULL); 6076 if (res) 6077 goto err; 6078 } 6079 6080 skb_flow_dissector_init(&flow_keys_bonding, 6081 flow_keys_bonding_keys, 6082 ARRAY_SIZE(flow_keys_bonding_keys)); 6083 6084 register_netdevice_notifier(&bond_netdev_notifier); 6085 out: 6086 return res; 6087 err: 6088 bond_destroy_debugfs(); 6089 bond_netlink_fini(); 6090 err_link: 6091 unregister_pernet_subsys(&bond_net_ops); 6092 goto out; 6093 6094 } 6095 6096 static void __exit bonding_exit(void) 6097 { 6098 unregister_netdevice_notifier(&bond_netdev_notifier); 6099 6100 bond_destroy_debugfs(); 6101 6102 bond_netlink_fini(); 6103 unregister_pernet_subsys(&bond_net_ops); 6104 6105 #ifdef CONFIG_NET_POLL_CONTROLLER 6106 /* Make sure we don't have an imbalance on our netpoll blocking */ 6107 WARN_ON(atomic_read(&netpoll_block_tx)); 6108 #endif 6109 } 6110 6111 module_init(bonding_init); 6112 module_exit(bonding_exit); 6113 MODULE_LICENSE("GPL"); 6114 MODULE_DESCRIPTION(DRV_DESCRIPTION); 6115 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others"); 6116