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