Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/types.h> |
| 37 | #include <linux/fcntl.h> |
Jakub Kicinski | b645941 | 2021-12-28 16:49:13 -0800 | [diff] [blame] | 38 | #include <linux/filter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/ptrace.h> |
| 41 | #include <linux/ioport.h> |
| 42 | #include <linux/in.h> |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 43 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/ip.h> |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 45 | #include <linux/icmp.h> |
| 46 | #include <linux/icmpv6.h> |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 47 | #include <linux/tcp.h> |
| 48 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 57 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <asm/dma.h> |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 59 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/errno.h> |
| 61 | #include <linux/netdevice.h> |
| 62 | #include <linux/inetdevice.h> |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 63 | #include <linux/igmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/etherdevice.h> |
| 65 | #include <linux/skbuff.h> |
| 66 | #include <net/sock.h> |
| 67 | #include <linux/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 75 | #include <linux/phy.h> |
David Sterba | b63bb73 | 2007-12-06 23:40:33 -0800 | [diff] [blame] | 76 | #include <linux/jiffies.h> |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 77 | #include <linux/preempt.h> |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 78 | #include <net/route.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 79 | #include <net/net_namespace.h> |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 80 | #include <net/netns/generic.h> |
Eric Dumazet | 5ee31c68 | 2012-06-12 06:03:51 +0000 | [diff] [blame] | 81 | #include <net/pkt_sched.h> |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 82 | #include <linux/rculist.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 83 | #include <net/flow_dissector.h> |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 84 | #include <net/xfrm.h> |
David S. Miller | 1ef8019 | 2014-11-10 13:27:49 -0500 | [diff] [blame] | 85 | #include <net/bonding.h> |
| 86 | #include <net/bond_3ad.h> |
| 87 | #include <net/bond_alb.h> |
Tariq Toukan | 89df6a8 | 2021-01-17 16:59:46 +0200 | [diff] [blame] | 88 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
| 89 | #include <net/tls.h> |
| 90 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Matan Barak | 73b5a6f2 | 2015-04-26 15:55:57 +0300 | [diff] [blame] | 92 | #include "bonding_priv.h" |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /*---------------------------- Module parameters ----------------------------*/ |
| 95 | |
| 96 | /* monitor all links that often (in milliseconds). <=0 disables monitoring */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | static int max_bonds = BOND_DEFAULT_MAX_BONDS; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 99 | static int tx_queues = BOND_DEFAULT_TX_QUEUES; |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 100 | static int num_peer_notif = 1; |
Nikolay Aleksandrov | b98d9c6 | 2014-01-22 14:53:31 +0100 | [diff] [blame] | 101 | static int miimon; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 102 | static int updelay; |
| 103 | static int downdelay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static int use_carrier = 1; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 105 | static char *mode; |
| 106 | static char *primary; |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 107 | static char *primary_reselect; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 108 | static char *lacp_rate; |
stephen hemminger | 655f891 | 2011-06-22 09:54:39 +0000 | [diff] [blame] | 109 | static int min_links; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 110 | static char *ad_select; |
| 111 | static char *xmit_hash_policy; |
Nikolay Aleksandrov | 7bdb04e | 2014-01-22 14:53:23 +0100 | [diff] [blame] | 112 | static int arp_interval; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 113 | static char *arp_ip_target[BOND_MAX_ARP_TARGETS]; |
| 114 | static char *arp_validate; |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 115 | static char *arp_all_targets; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 116 | static char *fail_over_mac; |
dingtianhong | b07ea07 | 2013-07-23 15:25:47 +0800 | [diff] [blame] | 117 | static int all_slaves_active; |
Stephen Hemminger | d2991f7 | 2009-06-12 19:02:44 +0000 | [diff] [blame] | 118 | static struct bond_params bonding_defaults; |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 119 | static int resend_igmp = BOND_DEFAULT_RESEND_IGMP; |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 120 | static int packets_per_slave = 1; |
dingtianhong | 3a7129e | 2013-12-21 14:40:12 +0800 | [diff] [blame] | 121 | static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | module_param(max_bonds, int, 0); |
| 124 | MODULE_PARM_DESC(max_bonds, "Max number of bonded devices"); |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 125 | module_param(tx_queues, int, 0); |
| 126 | MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)"); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 127 | module_param_named(num_grat_arp, num_peer_notif, int, 0644); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 128 | MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on " |
| 129 | "failover event (alias of num_unsol_na)"); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 130 | module_param_named(num_unsol_na, num_peer_notif, int, 0644); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 131 | MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on " |
| 132 | "failover event (alias of num_grat_arp)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Mitch Williams | 2ac4766 | 2005-11-09 10:35:03 -0800 | [diff] [blame] | 138 | MODULE_PARM_DESC(downdelay, "Delay before considering link down, " |
| 139 | "in milliseconds"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | module_param(use_carrier, int, 0); |
Mitch Williams | 2ac4766 | 2005-11-09 10:35:03 -0800 | [diff] [blame] | 141 | MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; " |
Debabrata Banerjee | b3c898e | 2018-05-16 14:02:13 -0400 | [diff] [blame] | 142 | "0 for off, 1 for on (default)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | module_param(mode, charp, 0); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 144 | MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, " |
Mitch Williams | 2ac4766 | 2005-11-09 10:35:03 -0800 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | module_param(primary, charp, 0); |
| 149 | MODULE_PARM_DESC(primary, "Primary network device to use"); |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | module_param(lacp_rate, charp, 0); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 159 | MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; " |
| 160 | "0 for slow, 1 for fast"); |
Jay Vosburgh | fd989c8 | 2008-11-04 17:51:16 -0800 | [diff] [blame] | 161 | module_param(ad_select, charp, 0); |
Zhu Yanjun | 0d039f3 | 2016-08-09 21:36:04 +0800 | [diff] [blame] | 162 | MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; " |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 163 | "0 for stable (default), 1 for bandwidth, " |
| 164 | "2 for count"); |
stephen hemminger | 655f891 | 2011-06-22 09:54:39 +0000 | [diff] [blame] | 165 | module_param(min_links, int, 0); |
| 166 | MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier"); |
| 167 | |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 168 | module_param(xmit_hash_policy, charp, 0); |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 169 | MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; " |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 170 | "0 for layer 2 (default), 1 for layer 3+4, " |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 171 | "2 for layer 2+3, 3 for encap layer 2+3, " |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 172 | "4 for encap layer 3+4, 5 for vlan+srcmac"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 177 | module_param(arp_validate, charp, 0); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 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"); |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 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"); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 183 | module_param(fail_over_mac, charp, 0); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 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"); |
Andy Gospodarek | ebd8e49 | 2010-06-02 08:39:21 +0000 | [diff] [blame] | 187 | module_param(all_slaves_active, int, 0); |
Masanari Iida | 37b7021 | 2014-09-09 18:07:55 +0900 | [diff] [blame] | 188 | MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface " |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 189 | "by setting active flag for all slaves; " |
Andy Gospodarek | ebd8e49 | 2010-06-02 08:39:21 +0000 | [diff] [blame] | 190 | "0 for never (default), 1 for always."); |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 191 | module_param(resend_igmp, int, 0); |
Andy Gospodarek | 90e6247 | 2011-05-25 04:41:59 +0000 | [diff] [blame] | 192 | MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on " |
| 193 | "link failure"); |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 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."); |
dingtianhong | 3a7129e | 2013-12-21 14:40:12 +0800 | [diff] [blame] | 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."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /*----------------------------- Global variables ----------------------------*/ |
| 204 | |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Neil Horman | fb4fa76 | 2010-12-06 09:05:50 +0000 | [diff] [blame] | 206 | atomic_t netpoll_block_tx = ATOMIC_INIT(0); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 209 | unsigned int bond_net_id __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /*-------------------------- Forward declarations ---------------------------*/ |
| 257 | |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 258 | static int bond_init(struct net_device *bond_dev); |
Eric W. Biederman | c67dfb2 | 2009-10-29 14:18:24 +0000 | [diff] [blame] | 259 | static void bond_uninit(struct net_device *bond_dev); |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 260 | static void bond_get_stats(struct net_device *bond_dev, |
| 261 | struct rtnl_link_stats64 *stats); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 262 | static void bond_slave_arr_handler(struct work_struct *work); |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 263 | static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act, |
| 264 | int mod); |
Mahesh Bandewar | d4859d7 | 2018-09-24 14:40:11 -0700 | [diff] [blame] | 265 | static void bond_netdev_notify_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | /*---------------------------- General routines -----------------------------*/ |
| 268 | |
Amerigo Wang | bd33acc | 2011-03-06 21:58:46 +0000 | [diff] [blame] | 269 | const char *bond_mode_name(int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Holger Eitzenberger | 77afc92 | 2008-12-09 23:08:09 -0800 | [diff] [blame] | 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)", |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 276 | [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation", |
Holger Eitzenberger | 77afc92 | 2008-12-09 23:08:09 -0800 | [diff] [blame] | 277 | [BOND_MODE_TLB] = "transmit load balancing", |
| 278 | [BOND_MODE_ALB] = "adaptive load balancing", |
| 279 | }; |
| 280 | |
Wang Sheng-Hui | f528094 | 2013-07-24 14:53:26 +0800 | [diff] [blame] | 281 | if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return "unknown"; |
Holger Eitzenberger | 77afc92 | 2008-12-09 23:08:09 -0800 | [diff] [blame] | 283 | |
| 284 | return names[mode]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | * bond_dev_queue_xmit - Prepare skb for xmit. |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 289 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | */ |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 294 | netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 295 | struct net_device *slave_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Ben Hutchings | 8387451 | 2010-12-13 08:19:28 +0000 | [diff] [blame] | 297 | skb->dev = slave_dev; |
Neil Horman | 374eeb5 | 2011-06-03 10:35:52 +0000 | [diff] [blame] | 298 | |
Eric Dumazet | 5ee31c68 | 2012-06-12 06:03:51 +0000 | [diff] [blame] | 299 | BUILD_BUG_ON(sizeof(skb->queue_mapping) != |
Jiri Pirko | df4ab5b | 2012-07-20 02:28:49 +0000 | [diff] [blame] | 300 | sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping)); |
Tonghao Zhang | ae35c6f | 2018-05-11 02:53:11 -0700 | [diff] [blame] | 301 | skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping); |
Neil Horman | 374eeb5 | 2011-06-03 10:35:52 +0000 | [diff] [blame] | 302 | |
Amerigo Wang | e15c3c22 | 2012-08-10 01:24:45 +0000 | [diff] [blame] | 303 | if (unlikely(netpoll_tx_running(bond->dev))) |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 304 | return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb); |
| 305 | |
| 306 | return dev_queue_xmit(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Tariq Toukan | 007feb8 | 2021-01-17 16:59:44 +0200 | [diff] [blame] | 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 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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: |
Jussi Maki | 39a0876 | 2021-08-12 14:52:41 +0000 | [diff] [blame] | 327 | return true; |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 328 | case BOND_MODE_8023AD: |
| 329 | case BOND_MODE_XOR: |
Jussi Maki | 39a0876 | 2021-08-12 14:52:41 +0000 | [diff] [blame] | 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; |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 336 | default: |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 341 | /*---------------------------------- VLAN -----------------------------------*/ |
| 342 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 343 | /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid, |
Jiri Pirko | cc0e407 | 2011-07-20 04:54:46 +0000 | [diff] [blame] | 344 | * We don't protect the slave list iteration with a lock because: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 349 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * bond_vlan_rx_add_vid - Propagates adding an id to slaves |
| 361 | * @bond_dev: bonding net device that got called |
Lee Jones | 45a1553 | 2020-08-14 12:39:05 +0100 | [diff] [blame] | 362 | * @proto: network protocol ID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | * @vid: vlan id being added |
| 364 | */ |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 365 | static int bond_vlan_rx_add_vid(struct net_device *bond_dev, |
| 366 | __be16 proto, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 368 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 369 | struct slave *slave, *rollback_slave; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 370 | struct list_head *iter; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 371 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 373 | bond_for_each_slave(bond, slave, iter) { |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 374 | res = vlan_vid_add(slave->dev, proto, vid); |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 375 | if (res) |
| 376 | goto unwind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jiri Pirko | 8e58613 | 2011-12-08 19:52:37 -0500 | [diff] [blame] | 379 | return 0; |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 380 | |
| 381 | unwind: |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 382 | /* unwind to the slave that failed */ |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 383 | bond_for_each_slave(bond, rollback_slave, iter) { |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 384 | if (rollback_slave == slave) |
| 385 | break; |
| 386 | |
| 387 | vlan_vid_del(rollback_slave->dev, proto, vid); |
| 388 | } |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 389 | |
| 390 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves |
| 395 | * @bond_dev: bonding net device that got called |
Lee Jones | 45a1553 | 2020-08-14 12:39:05 +0100 | [diff] [blame] | 396 | * @proto: network protocol ID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | * @vid: vlan id being removed |
| 398 | */ |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 399 | static int bond_vlan_rx_kill_vid(struct net_device *bond_dev, |
| 400 | __be16 proto, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 402 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 403 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 406 | bond_for_each_slave(bond, slave, iter) |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 407 | vlan_vid_del(slave->dev, proto, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Veaceslav Falico | e868b0c | 2013-08-28 23:25:15 +0200 | [diff] [blame] | 409 | if (bond_is_lb(bond)) |
| 410 | bond_alb_clear_vlan(bond, vid); |
Jiri Pirko | 8e58613 | 2011-12-08 19:52:37 -0500 | [diff] [blame] | 411 | |
| 412 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 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; |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 425 | struct bond_ipsec *ipsec; |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 426 | struct bonding *bond; |
| 427 | struct slave *slave; |
Taehee Yoo | b648eba | 2021-07-05 15:38:06 +0000 | [diff] [blame] | 428 | int err; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 429 | |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 430 | if (!bond_dev) |
| 431 | return -EINVAL; |
| 432 | |
Taehee Yoo | b648eba | 2021-07-05 15:38:06 +0000 | [diff] [blame] | 433 | rcu_read_lock(); |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 434 | bond = netdev_priv(bond_dev); |
Jarod Wilson | f548a47 | 2020-07-08 18:58:49 -0400 | [diff] [blame] | 435 | slave = rcu_dereference(bond->curr_active_slave); |
Taehee Yoo | 105cd17 | 2021-07-05 15:38:07 +0000 | [diff] [blame] | 436 | if (!slave) { |
| 437 | rcu_read_unlock(); |
| 438 | return -ENODEV; |
| 439 | } |
| 440 | |
Taehee Yoo | b121693 | 2021-07-05 15:38:11 +0000 | [diff] [blame] | 441 | if (!slave->dev->xfrmdev_ops || |
| 442 | !slave->dev->xfrmdev_ops->xdo_dev_state_add || |
| 443 | netif_is_bond_master(slave->dev)) { |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 444 | slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n"); |
Taehee Yoo | b648eba | 2021-07-05 15:38:06 +0000 | [diff] [blame] | 445 | rcu_read_unlock(); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 446 | return -EINVAL; |
| 447 | } |
| 448 | |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 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 | |
Taehee Yoo | b648eba | 2021-07-05 15:38:06 +0000 | [diff] [blame] | 456 | err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs); |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 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 | } |
Taehee Yoo | b648eba | 2021-07-05 15:38:06 +0000 | [diff] [blame] | 466 | rcu_read_unlock(); |
| 467 | return err; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 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 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 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; |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 513 | struct bond_ipsec *ipsec; |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 514 | struct bonding *bond; |
| 515 | struct slave *slave; |
| 516 | |
| 517 | if (!bond_dev) |
| 518 | return; |
| 519 | |
Taehee Yoo | a22c39b | 2021-07-05 15:38:10 +0000 | [diff] [blame] | 520 | rcu_read_lock(); |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 521 | bond = netdev_priv(bond_dev); |
Jarod Wilson | f548a47 | 2020-07-08 18:58:49 -0400 | [diff] [blame] | 522 | slave = rcu_dereference(bond->curr_active_slave); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 523 | |
| 524 | if (!slave) |
Taehee Yoo | a22c39b | 2021-07-05 15:38:10 +0000 | [diff] [blame] | 525 | goto out; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 526 | |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 527 | if (!xs->xso.real_dev) |
| 528 | goto out; |
| 529 | |
| 530 | WARN_ON(xs->xso.real_dev != slave->dev); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 531 | |
Taehee Yoo | b121693 | 2021-07-05 15:38:11 +0000 | [diff] [blame] | 532 | if (!slave->dev->xfrmdev_ops || |
| 533 | !slave->dev->xfrmdev_ops->xdo_dev_state_delete || |
| 534 | netif_is_bond_master(slave->dev)) { |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 535 | slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__); |
Taehee Yoo | a22c39b | 2021-07-05 15:38:10 +0000 | [diff] [blame] | 536 | goto out; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs); |
Taehee Yoo | a22c39b | 2021-07-05 15:38:10 +0000 | [diff] [blame] | 540 | out: |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 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); |
Taehee Yoo | a22c39b | 2021-07-05 15:38:10 +0000 | [diff] [blame] | 583 | rcu_read_unlock(); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 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; |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 594 | struct net_device *real_dev; |
| 595 | struct slave *curr_active; |
| 596 | struct bonding *bond; |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 597 | int err; |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 598 | |
| 599 | bond = netdev_priv(bond_dev); |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 600 | rcu_read_lock(); |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 601 | curr_active = rcu_dereference(bond->curr_active_slave); |
| 602 | real_dev = curr_active->dev; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 603 | |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 604 | if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Taehee Yoo | 168e696 | 2021-07-05 15:38:14 +0000 | [diff] [blame] | 605 | err = false; |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 606 | goto out; |
| 607 | } |
Jarod Wilson | a3b658c | 2020-06-30 14:49:41 -0400 | [diff] [blame] | 608 | |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 609 | if (!xs->xso.real_dev) { |
| 610 | err = false; |
| 611 | goto out; |
| 612 | } |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 613 | |
| 614 | if (!real_dev->xfrmdev_ops || |
| 615 | !real_dev->xfrmdev_ops->xdo_dev_offload_ok || |
| 616 | netif_is_bond_master(real_dev)) { |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 617 | err = false; |
| 618 | goto out; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 619 | } |
| 620 | |
Taehee Yoo | 955b785 | 2021-07-05 15:38:13 +0000 | [diff] [blame] | 621 | err = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs); |
| 622 | out: |
| 623 | rcu_read_unlock(); |
| 624 | return err; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | /*------------------------------- Link status -------------------------------*/ |
| 635 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 636 | /* Set the carrier state for the master according to the state of its |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 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 | */ |
Jonathan Toppins | 2477bc9 | 2015-01-26 01:16:57 -0500 | [diff] [blame] | 642 | int bond_set_carrier(struct bonding *bond) |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 643 | { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 644 | struct list_head *iter; |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 645 | struct slave *slave; |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 646 | |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 647 | if (!bond_has_slaves(bond)) |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 648 | goto down; |
| 649 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 650 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 651 | return bond_3ad_set_carrier(bond); |
| 652 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 653 | bond_for_each_slave(bond, slave, iter) { |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 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 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 671 | /* Get link speed and duplex from the slave's base driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | * using ethtool. If for some reason the call fails or the |
Weiping Pan | 98f41f6 | 2011-10-31 17:20:48 +0000 | [diff] [blame] | 673 | * values are invalid, set speed and duplex to -1, |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 674 | * and return. Return 1 if speed or duplex settings are |
| 675 | * UNKNOWN; 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | */ |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 677 | static int bond_update_speed_duplex(struct slave *slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
| 679 | struct net_device *slave_dev = slave->dev; |
David Decotigny | 9856909 | 2016-02-24 10:58:02 -0800 | [diff] [blame] | 680 | struct ethtool_link_ksettings ecmd; |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 681 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Dan Carpenter | 589665f | 2011-11-04 08:21:38 +0000 | [diff] [blame] | 683 | slave->speed = SPEED_UNKNOWN; |
| 684 | slave->duplex = DUPLEX_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
David Decotigny | 9856909 | 2016-02-24 10:58:02 -0800 | [diff] [blame] | 686 | res = __ethtool_get_link_ksettings(slave_dev, &ecmd); |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 687 | if (res < 0) |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 688 | return 1; |
David Decotigny | 9856909 | 2016-02-24 10:58:02 -0800 | [diff] [blame] | 689 | if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1)) |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 690 | return 1; |
David Decotigny | 9856909 | 2016-02-24 10:58:02 -0800 | [diff] [blame] | 691 | switch (ecmd.base.duplex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | case DUPLEX_FULL: |
| 693 | case DUPLEX_HALF: |
| 694 | break; |
| 695 | default: |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 696 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
David Decotigny | 9856909 | 2016-02-24 10:58:02 -0800 | [diff] [blame] | 699 | slave->speed = ecmd.base.speed; |
| 700 | slave->duplex = ecmd.base.duplex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Mahesh Bandewar | c4adfc8 | 2017-03-27 11:37:35 -0700 | [diff] [blame] | 702 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 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 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 721 | /* if <dev> supports MII link status reporting, check its link status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | * |
| 723 | * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(), |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 724 | * depending upon the setting of the use_carrier parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 736 | static int bond_check_dev_link(struct bonding *bond, |
| 737 | struct net_device *slave_dev, int reporting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 739 | const struct net_device_ops *slave_ops = slave_dev->netdev_ops; |
Jiri Bohac | d9d5283 | 2009-10-28 22:23:54 -0700 | [diff] [blame] | 740 | int (*ioctl)(struct net_device *, struct ifreq *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | struct ifreq ifr; |
| 742 | struct mii_ioctl_data *mii; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Petri Gynther | 6c98885 | 2009-08-28 12:05:15 +0000 | [diff] [blame] | 744 | if (!reporting && !netif_running(slave_dev)) |
| 745 | return 0; |
| 746 | |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 747 | if (bond->params.use_carrier) |
Debabrata Banerjee | b3c898e | 2018-05-16 14:02:13 -0400 | [diff] [blame] | 748 | return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Jiri Pirko | 29112f4 | 2009-04-24 01:58:23 +0000 | [diff] [blame] | 750 | /* Try to get link status using Ethtool first. */ |
Ben Hutchings | c772dde | 2012-12-07 06:15:32 +0000 | [diff] [blame] | 751 | if (slave_dev->ethtool_ops->get_link) |
| 752 | return slave_dev->ethtool_ops->get_link(slave_dev) ? |
| 753 | BMSR_LSTATUS : 0; |
Jiri Pirko | 29112f4 | 2009-04-24 01:58:23 +0000 | [diff] [blame] | 754 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 755 | /* Ethtool can't be used, fallback to MII ioctls. */ |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 756 | ioctl = slave_ops->ndo_eth_ioctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | if (ioctl) { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 765 | /* We cannot assume that SIOCGMIIPHY will also read a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Kees Cook | 4390207 | 2021-06-02 13:58:20 -0700 | [diff] [blame] | 771 | strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | mii = if_mii(&ifr); |
Al Viro | 4ad41c1 | 2016-09-03 19:37:25 -0400 | [diff] [blame] | 773 | if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | mii->reg_num = MII_BMSR; |
Al Viro | 4ad41c1 | 2016-09-03 19:37:25 -0400 | [diff] [blame] | 775 | if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0) |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 776 | return mii->val_out & BMSR_LSTATUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 780 | /* If reporting, report that either there's no ndo_eth_ioctl, |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 781 | * or both SIOCGMIIREG and get_link failed (meaning that we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | * cannot report link status). If not reporting, pretend |
| 783 | * we're ok. |
| 784 | */ |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 785 | return reporting ? -1 : BMSR_LSTATUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /*----------------------------- Multicast list ------------------------------*/ |
| 789 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 790 | /* Push the promiscuity flag down to appropriate slaves */ |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 791 | static int bond_set_promiscuity(struct bonding *bond, int inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 793 | struct list_head *iter; |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 794 | int err = 0; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 795 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 796 | if (bond_uses_primary(bond)) { |
Veaceslav Falico | 14056e7 | 2014-07-16 18:32:01 +0200 | [diff] [blame] | 797 | struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 798 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 799 | if (curr_active) |
| 800 | err = dev_set_promiscuity(curr_active->dev, inc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } else { |
| 802 | struct slave *slave; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 803 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 804 | bond_for_each_slave(bond, slave, iter) { |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 805 | err = dev_set_promiscuity(slave->dev, inc); |
| 806 | if (err) |
| 807 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 810 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 813 | /* Push the allmulti flag down to all slaves */ |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 814 | static int bond_set_allmulti(struct bonding *bond, int inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 816 | struct list_head *iter; |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 817 | int err = 0; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 818 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 819 | if (bond_uses_primary(bond)) { |
Veaceslav Falico | 14056e7 | 2014-07-16 18:32:01 +0200 | [diff] [blame] | 820 | struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 821 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 822 | if (curr_active) |
| 823 | err = dev_set_allmulti(curr_active->dev, inc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } else { |
| 825 | struct slave *slave; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 826 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 827 | bond_for_each_slave(bond, slave, iter) { |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 828 | err = dev_set_allmulti(slave->dev, inc); |
| 829 | if (err) |
| 830 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 833 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 836 | /* Retrieve the list of registered multicast addresses for the bonding |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 837 | * device and retransmit an IGMP JOIN request to the current active |
| 838 | * slave. |
| 839 | */ |
stephen hemminger | 379b738 | 2010-10-15 11:02:56 +0000 | [diff] [blame] | 840 | static void bond_resend_igmp_join_requests_delayed(struct work_struct *work) |
Flavio Leitner | 5a37e8c | 2010-10-05 14:23:57 +0000 | [diff] [blame] | 841 | { |
| 842 | struct bonding *bond = container_of(work, struct bonding, |
Flavio Leitner | 94265cf | 2011-05-25 08:38:58 +0000 | [diff] [blame] | 843 | mcast_work.work); |
Veaceslav Falico | ad999ee | 2013-03-26 04:10:02 +0000 | [diff] [blame] | 844 | |
dingtianhong | f236910 | 2013-12-13 10:20:26 +0800 | [diff] [blame] | 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(); |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 858 | /* Flush bond's hardware addresses from slave */ |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 859 | static void bond_hw_addr_flush(struct net_device *bond_dev, |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 860 | struct net_device *slave_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 862 | struct bonding *bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 864 | dev_uc_unsync(slave_dev, bond_dev); |
| 865 | dev_mc_unsync(slave_dev, bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 867 | if (BOND_MODE(bond) == BOND_MODE_8023AD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | /* del lacpdu mc addr from mc list */ |
| 869 | u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR; |
| 870 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 871 | dev_mc_del(slave_dev, lacpdu_multicast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | /*--------------------------- Active slave change ---------------------------*/ |
| 876 | |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 877 | /* Update the hardware address list and promisc/allmulti for the new and |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 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 |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 880 | * this function to swap these settings during a failover. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 882 | static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active, |
| 883 | struct slave *old_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (old_active) { |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 886 | if (bond->dev->flags & IFF_PROMISC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | dev_set_promiscuity(old_active->dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 889 | if (bond->dev->flags & IFF_ALLMULTI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | dev_set_allmulti(old_active->dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 892 | bond_hw_addr_flush(bond->dev, old_active->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (new_active) { |
Wang Chen | 7e1a1ac | 2008-07-14 20:51:36 -0700 | [diff] [blame] | 896 | /* FIXME: Signal errors upstream. */ |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 897 | if (bond->dev->flags & IFF_PROMISC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | dev_set_promiscuity(new_active->dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 900 | if (bond->dev->flags & IFF_ALLMULTI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | dev_set_allmulti(new_active->dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
nikolay@redhat.com | d632ce9 | 2013-04-18 07:33:38 +0000 | [diff] [blame] | 903 | netif_addr_lock_bh(bond->dev); |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 904 | dev_uc_sync(new_active->dev, bond->dev); |
| 905 | dev_mc_sync(new_active->dev, bond->dev); |
nikolay@redhat.com | d632ce9 | 2013-04-18 07:33:38 +0000 | [diff] [blame] | 906 | netif_addr_unlock_bh(bond->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
nikolay@redhat.com | ae0d675 | 2013-06-26 17:13:39 +0200 | [diff] [blame] | 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 | */ |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 917 | static int bond_set_dev_addr(struct net_device *bond_dev, |
| 918 | struct net_device *slave_dev) |
nikolay@redhat.com | ae0d675 | 2013-06-26 17:13:39 +0200 | [diff] [blame] | 919 | { |
Petr Machata | 1caf40d | 2018-12-13 11:54:46 +0000 | [diff] [blame] | 920 | int err; |
| 921 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Petr Machata | 1caf40d | 2018-12-13 11:54:46 +0000 | [diff] [blame] | 924 | err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL); |
| 925 | if (err) |
| 926 | return err; |
| 927 | |
Jakub Kicinski | 6f23810 | 2021-10-22 16:20:59 -0700 | [diff] [blame] | 928 | __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len); |
nikolay@redhat.com | ae0d675 | 2013-06-26 17:13:39 +0200 | [diff] [blame] | 929 | bond_dev->addr_assign_type = NET_ADDR_STOLEN; |
| 930 | call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev); |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 931 | return 0; |
nikolay@redhat.com | ae0d675 | 2013-06-26 17:13:39 +0200 | [diff] [blame] | 932 | } |
| 933 | |
dingtianhong | a951bc1 | 2015-07-16 16:30:02 +0800 | [diff] [blame] | 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 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 951 | /* bond_do_fail_over_mac |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 952 | * |
| 953 | * Perform special MAC address swapping for fail_over_mac settings |
| 954 | * |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 955 | * Called with RTNL |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 956 | */ |
| 957 | static void bond_do_fail_over_mac(struct bonding *bond, |
| 958 | struct slave *new_active, |
| 959 | struct slave *old_active) |
| 960 | { |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 961 | u8 tmp_mac[MAX_ADDR_LEN]; |
| 962 | struct sockaddr_storage ss; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 963 | int rv; |
| 964 | |
| 965 | switch (bond->params.fail_over_mac) { |
| 966 | case BOND_FOM_ACTIVE: |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 967 | if (new_active) { |
| 968 | rv = bond_set_dev_addr(bond->dev, new_active->dev); |
| 969 | if (rv) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 970 | slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n", |
| 971 | -rv); |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 972 | } |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 973 | break; |
| 974 | case BOND_FOM_FOLLOW: |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 975 | /* if new_active && old_active, swap them |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 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 | |
dingtianhong | a951bc1 | 2015-07-16 16:30:02 +0800 | [diff] [blame] | 982 | if (!old_active) |
| 983 | old_active = bond_get_old_active(bond, new_active); |
| 984 | |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 985 | if (old_active) { |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 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; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 992 | } else { |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 993 | bond_hw_addr_copy(ss.__data, bond->dev->dev_addr, |
| 994 | bond->dev->addr_len); |
| 995 | ss.ss_family = bond->dev->type; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 998 | rv = dev_set_mac_address(new_active->dev, |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 999 | (struct sockaddr *)&ss, NULL); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1000 | if (rv) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1001 | slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n", |
| 1002 | -rv); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1003 | goto out; |
| 1004 | } |
| 1005 | |
| 1006 | if (!old_active) |
| 1007 | goto out; |
| 1008 | |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1009 | bond_hw_addr_copy(ss.__data, tmp_mac, |
| 1010 | new_active->dev->addr_len); |
| 1011 | ss.ss_family = old_active->dev->type; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1012 | |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1013 | rv = dev_set_mac_address(old_active->dev, |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 1014 | (struct sockaddr *)&ss, NULL); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1015 | if (rv) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1016 | slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n", |
| 1017 | -rv); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1018 | out: |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1019 | break; |
| 1020 | default: |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 1021 | netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n", |
| 1022 | bond->params.fail_over_mac); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | |
| 1026 | } |
| 1027 | |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1028 | static struct slave *bond_choose_primary_or_current(struct bonding *bond) |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 1029 | { |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 1030 | struct slave *prim = rtnl_dereference(bond->primary_slave); |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 1031 | struct slave *curr = rtnl_dereference(bond->curr_active_slave); |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 1032 | |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1033 | if (!prim || prim->link != BOND_LINK_UP) { |
| 1034 | if (!curr || curr->link != BOND_LINK_UP) |
| 1035 | return NULL; |
| 1036 | return curr; |
| 1037 | } |
| 1038 | |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 1039 | if (bond->force_primary) { |
| 1040 | bond->force_primary = false; |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1041 | return prim; |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 1042 | } |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 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 | } |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 1064 | } |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | /** |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1067 | * bond_find_best_slave - select the best available slave to be the active one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | * @bond: our bonding struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | */ |
| 1070 | static struct slave *bond_find_best_slave(struct bonding *bond) |
| 1071 | { |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1072 | struct slave *slave, *bestslave = NULL; |
Veaceslav Falico | 77140d2 | 2013-09-25 09:20:18 +0200 | [diff] [blame] | 1073 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | int mintime = bond->params.updelay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Mazhar Rana | b5a983f | 2015-07-07 15:04:50 +0530 | [diff] [blame] | 1076 | slave = bond_choose_primary_or_current(bond); |
| 1077 | if (slave) |
| 1078 | return slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Veaceslav Falico | 77140d2 | 2013-09-25 09:20:18 +0200 | [diff] [blame] | 1080 | bond_for_each_slave(bond, slave, iter) { |
| 1081 | if (slave->link == BOND_LINK_UP) |
| 1082 | return slave; |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 1083 | if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) && |
Veaceslav Falico | 77140d2 | 2013-09-25 09:20:18 +0200 | [diff] [blame] | 1084 | slave->delay < mintime) { |
| 1085 | mintime = slave->delay; |
| 1086 | bestslave = slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | return bestslave; |
| 1091 | } |
| 1092 | |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1093 | static bool bond_should_notify_peers(struct bonding *bond) |
| 1094 | { |
dingtianhong | 4cb4f97 | 2013-12-13 10:19:39 +0800 | [diff] [blame] | 1095 | struct slave *slave; |
| 1096 | |
| 1097 | rcu_read_lock(); |
| 1098 | slave = rcu_dereference(bond->curr_active_slave); |
| 1099 | rcu_read_unlock(); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1100 | |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1101 | if (!slave || !bond->send_peer_notif || |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 1102 | bond->send_peer_notif % |
| 1103 | max(1, bond->params.peer_notif_delay) != 0 || |
Venkat Venkatsubra | b02e3e9 | 2015-08-11 07:57:23 -0700 | [diff] [blame] | 1104 | !netif_carrier_ok(bond->dev) || |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1105 | test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state)) |
| 1106 | return false; |
| 1107 | |
Suresh Kumar | fee32de | 2021-12-13 11:17:09 +0530 | [diff] [blame] | 1108 | netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n", |
| 1109 | slave ? slave->dev->name : "NULL"); |
| 1110 | |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1111 | return true; |
| 1112 | } |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | /** |
Yang Yingliang | acf61b3 | 2021-03-29 20:42:57 +0800 | [diff] [blame] | 1115 | * bond_change_active_slave - change the active slave into the specified one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | * @bond: our bonding struct |
Lee Jones | 45a1553 | 2020-08-14 12:39:05 +0100 | [diff] [blame] | 1117 | * @new_active: the new slave to make the active one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 1127 | * Caller must hold RTNL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | */ |
Mitch Williams | a77b532 | 2005-11-09 10:35:51 -0800 | [diff] [blame] | 1129 | void bond_change_active_slave(struct bonding *bond, struct slave *new_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 1131 | struct slave *old_active; |
| 1132 | |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 1133 | ASSERT_RTNL(); |
| 1134 | |
| 1135 | old_active = rtnl_dereference(bond->curr_active_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 1137 | if (old_active == new_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1140 | #ifdef CONFIG_XFRM_OFFLOAD |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 1141 | bond_ipsec_del_sa_all(bond); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1142 | #endif /* CONFIG_XFRM_OFFLOAD */ |
| 1143 | |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 1144 | if (new_active) { |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 1145 | new_active->last_link_up = jiffies; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | if (new_active->link == BOND_LINK_BACK) { |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 1148 | if (bond_uses_primary(bond)) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | new_active->delay = 0; |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 1154 | bond_set_slave_link_state(new_active, BOND_LINK_UP, |
| 1155 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1157 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | bond_3ad_handle_link_change(new_active, BOND_LINK_UP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Holger Eitzenberger | 5840205 | 2008-12-09 23:07:13 -0800 | [diff] [blame] | 1160 | if (bond_is_lb(bond)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } else { |
Yufeng Mo | 5233351 | 2021-05-20 14:18:34 +0800 | [diff] [blame] | 1163 | if (bond_uses_primary(bond)) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1164 | slave_info(bond->dev, new_active->dev, "making interface the new active one\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 1168 | if (bond_uses_primary(bond)) |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 1169 | bond_hw_addr_swap(bond, new_active, old_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Holger Eitzenberger | 5840205 | 2008-12-09 23:07:13 -0800 | [diff] [blame] | 1171 | if (bond_is_lb(bond)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | bond_alb_handle_active_change(bond, new_active); |
Jay Vosburgh | 8f903c7 | 2006-02-21 16:36:44 -0800 | [diff] [blame] | 1173 | if (old_active) |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 1174 | bond_set_slave_inactive_flags(old_active, |
| 1175 | BOND_SLAVE_NOTIFY_NOW); |
Jay Vosburgh | 8f903c7 | 2006-02-21 16:36:44 -0800 | [diff] [blame] | 1176 | if (new_active) |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 1177 | bond_set_slave_active_flags(new_active, |
| 1178 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } else { |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 1180 | rcu_assign_pointer(bond->curr_active_slave, new_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 1182 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1183 | if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) { |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 1184 | if (old_active) |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 1185 | bond_set_slave_inactive_flags(old_active, |
| 1186 | BOND_SLAVE_NOTIFY_NOW); |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 1187 | |
| 1188 | if (new_active) { |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1189 | bool should_notify_peers = false; |
| 1190 | |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 1191 | bond_set_slave_active_flags(new_active, |
| 1192 | BOND_SLAVE_NOTIFY_NOW); |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 1193 | |
Or Gerlitz | 709f8a4 | 2008-06-13 18:12:01 -0700 | [diff] [blame] | 1194 | if (bond->params.fail_over_mac) |
| 1195 | bond_do_fail_over_mac(bond, new_active, |
| 1196 | old_active); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 1197 | |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1198 | if (netif_running(bond->dev)) { |
| 1199 | bond->send_peer_notif = |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 1200 | bond->params.num_peer_notif * |
| 1201 | max(1, bond->params.peer_notif_delay); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 1202 | should_notify_peers = |
| 1203 | bond_should_notify_peers(bond); |
| 1204 | } |
| 1205 | |
Amerigo Wang | b7bc2a5 | 2012-08-09 22:14:57 +0000 | [diff] [blame] | 1206 | call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev); |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 1207 | if (should_notify_peers) { |
| 1208 | bond->send_peer_notif--; |
Amerigo Wang | b7bc2a5 | 2012-08-09 22:14:57 +0000 | [diff] [blame] | 1209 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, |
| 1210 | bond->dev); |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 1211 | } |
Moni Shoua | 7893b24 | 2008-05-17 21:10:12 -0700 | [diff] [blame] | 1212 | } |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 1213 | } |
Andy Gospodarek | a2fd940 | 2010-03-25 14:49:05 +0000 | [diff] [blame] | 1214 | |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 1215 | #ifdef CONFIG_XFRM_OFFLOAD |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 1216 | bond_ipsec_add_sa_all(bond); |
Jarod Wilson | 5cd24cb | 2020-07-08 13:46:31 -0400 | [diff] [blame] | 1217 | #endif /* CONFIG_XFRM_OFFLOAD */ |
| 1218 | |
Flavio Leitner | 5a37e8c | 2010-10-05 14:23:57 +0000 | [diff] [blame] | 1219 | /* resend IGMP joins since active slave has changed or |
Flavio Leitner | 94265cf | 2011-05-25 08:38:58 +0000 | [diff] [blame] | 1220 | * all were sent on curr_active_slave. |
| 1221 | * resend only if bond is brought up with the affected |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1222 | * bonding modes and the retransmission is enabled |
| 1223 | */ |
Flavio Leitner | 94265cf | 2011-05-25 08:38:58 +0000 | [diff] [blame] | 1224 | if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) && |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 1225 | ((bond_uses_primary(bond) && new_active) || |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1226 | BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) { |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 1227 | bond->igmp_retrans = bond->params.resend_igmp; |
Nikolay Aleksandrov | 4beac02 | 2013-08-01 11:51:42 +0200 | [diff] [blame] | 1228 | queue_delayed_work(bond->wq, &bond->mcast_work, 1); |
Andy Gospodarek | a2fd940 | 2010-03-25 14:49:05 +0000 | [diff] [blame] | 1229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * bond_select_active_slave - select a new active slave, if needed |
| 1234 | * @bond: our bonding struct |
| 1235 | * |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 1236 | * This functions should be called when one of the following occurs: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 1241 | * Caller must hold RTNL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | */ |
Mitch Williams | a77b532 | 2005-11-09 10:35:51 -0800 | [diff] [blame] | 1243 | void bond_select_active_slave(struct bonding *bond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | { |
| 1245 | struct slave *best_slave; |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 1246 | int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Nikolay Aleksandrov | e097458 | 2014-09-15 17:19:35 +0200 | [diff] [blame] | 1248 | ASSERT_RTNL(); |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | best_slave = bond_find_best_slave(bond); |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 1251 | if (best_slave != rtnl_dereference(bond->curr_active_slave)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | bond_change_active_slave(bond, best_slave); |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 1253 | rv = bond_set_carrier(bond); |
| 1254 | if (!rv) |
| 1255 | return; |
| 1256 | |
Zhang Shengju | d66bd90 | 2016-02-03 02:02:32 +0000 | [diff] [blame] | 1257 | if (netif_carrier_ok(bond->dev)) |
Eric Dumazet | b8bd72d | 2019-07-01 10:48:51 -0700 | [diff] [blame] | 1258 | netdev_info(bond->dev, "active interface up!\n"); |
Zhang Shengju | d66bd90 | 2016-02-03 02:02:32 +0000 | [diff] [blame] | 1259 | else |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 1260 | netdev_info(bond->dev, "now running without any active interface!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1264 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1265 | static inline int slave_enable_netpoll(struct slave *slave) |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1266 | { |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1267 | struct netpoll *np; |
| 1268 | int err = 0; |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1269 | |
Eric W. Biederman | a8779ec | 2014-03-27 15:36:38 -0700 | [diff] [blame] | 1270 | np = kzalloc(sizeof(*np), GFP_KERNEL); |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1271 | err = -ENOMEM; |
| 1272 | if (!np) |
| 1273 | goto out; |
| 1274 | |
Eric W. Biederman | a8779ec | 2014-03-27 15:36:38 -0700 | [diff] [blame] | 1275 | err = __netpoll_setup(np, slave->dev); |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1276 | if (err) { |
| 1277 | kfree(np); |
| 1278 | goto out; |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1279 | } |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 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; |
Debabrata Banerjee | c9fbd71 | 2018-10-18 11:18:26 -0400 | [diff] [blame] | 1292 | |
| 1293 | __netpoll_free(np); |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1294 | } |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1295 | |
| 1296 | static void bond_poll_controller(struct net_device *bond_dev) |
| 1297 | { |
Mahesh Bandewar | 616f454 | 2015-03-04 21:57:52 -0800 | [diff] [blame] | 1298 | struct bonding *bond = netdev_priv(bond_dev); |
| 1299 | struct slave *slave = NULL; |
| 1300 | struct list_head *iter; |
| 1301 | struct ad_info ad_info; |
Mahesh Bandewar | 616f454 | 2015-03-04 21:57:52 -0800 | [diff] [blame] | 1302 | |
| 1303 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
| 1304 | if (bond_3ad_get_active_agg_info(bond, &ad_info)) |
| 1305 | return; |
| 1306 | |
Mahesh Bandewar | 616f454 | 2015-03-04 21:57:52 -0800 | [diff] [blame] | 1307 | bond_for_each_slave_rcu(bond, slave, iter) { |
Eric Dumazet | 93f62ad | 2018-09-21 15:27:39 -0700 | [diff] [blame] | 1308 | if (!bond_slave_is_up(slave)) |
Mahesh Bandewar | 616f454 | 2015-03-04 21:57:52 -0800 | [diff] [blame] | 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 | |
Eric Dumazet | 93f62ad | 2018-09-21 15:27:39 -0700 | [diff] [blame] | 1320 | netpoll_poll_dev(slave->dev); |
Mahesh Bandewar | 616f454 | 2015-03-04 21:57:52 -0800 | [diff] [blame] | 1321 | } |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
dingtianhong | c4cdef9 | 2013-07-23 15:25:27 +0800 | [diff] [blame] | 1324 | static void bond_netpoll_cleanup(struct net_device *bond_dev) |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1325 | { |
dingtianhong | c4cdef9 | 2013-07-23 15:25:27 +0800 | [diff] [blame] | 1326 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1327 | struct list_head *iter; |
Neil Horman | c2355e1 | 2010-10-13 16:01:49 +0000 | [diff] [blame] | 1328 | struct slave *slave; |
Neil Horman | c2355e1 | 2010-10-13 16:01:49 +0000 | [diff] [blame] | 1329 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1330 | bond_for_each_slave(bond, slave, iter) |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 1331 | if (bond_slave_is_up(slave)) |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1332 | slave_disable_netpoll(slave); |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1333 | } |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1334 | |
Eric W. Biederman | a8779ec | 2014-03-27 15:36:38 -0700 | [diff] [blame] | 1335 | static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1336 | { |
| 1337 | struct bonding *bond = netdev_priv(dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1338 | struct list_head *iter; |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1339 | struct slave *slave; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 1340 | int err = 0; |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1341 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1342 | bond_for_each_slave(bond, slave, iter) { |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1343 | err = slave_enable_netpoll(slave); |
| 1344 | if (err) { |
dingtianhong | c4cdef9 | 2013-07-23 15:25:27 +0800 | [diff] [blame] | 1345 | bond_netpoll_cleanup(dev); |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | } |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 1349 | return err; |
| 1350 | } |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 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 | } |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1359 | static void bond_netpoll_cleanup(struct net_device *bond_dev) |
| 1360 | { |
| 1361 | } |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 1362 | #endif |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | /*---------------------------------- IOCTL ----------------------------------*/ |
| 1365 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1366 | static netdev_features_t bond_fix_features(struct net_device *dev, |
nikolay@redhat.com | 9b7b165 | 2013-09-02 13:51:41 +0200 | [diff] [blame] | 1367 | netdev_features_t features) |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1368 | { |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1369 | struct bonding *bond = netdev_priv(dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1370 | struct list_head *iter; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1371 | netdev_features_t mask; |
nikolay@redhat.com | 9b7b165 | 2013-09-02 13:51:41 +0200 | [diff] [blame] | 1372 | struct slave *slave; |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1373 | |
Tariq Toukan | 89df6a8 | 2021-01-17 16:59:46 +0200 | [diff] [blame] | 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 | |
Scott Feldman | 7889cbe | 2015-05-10 09:48:07 -0700 | [diff] [blame] | 1381 | mask = features; |
Roopa Prabhu | c158cba | 2015-01-29 22:40:16 -0800 | [diff] [blame] | 1382 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1383 | features &= ~NETIF_F_ONE_FOR_ALL; |
| 1384 | features |= NETIF_F_ALL_FOR_ALL; |
| 1385 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1386 | bond_for_each_slave(bond, slave, iter) { |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1387 | features = netdev_increment_features(features, |
| 1388 | slave->dev->features, |
| 1389 | mask); |
| 1390 | } |
Eric Dumazet | b0ce350 | 2013-05-16 07:34:53 +0000 | [diff] [blame] | 1391 | features = netdev_add_tso_features(features, mask); |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1392 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1393 | return features; |
| 1394 | } |
| 1395 | |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 1396 | #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ |
Alexander Lobakin | ecb8fed | 2020-11-01 13:17:17 +0000 | [diff] [blame] | 1397 | NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \ |
Michał Mirosław | 62f2a3a | 2011-07-13 14:10:29 +0000 | [diff] [blame] | 1398 | NETIF_F_HIGHDMA | NETIF_F_LRO) |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1399 | |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 1400 | #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ |
Alexander Lobakin | ecb8fed | 2020-11-01 13:17:17 +0000 | [diff] [blame] | 1401 | NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE) |
Or Gerlitz | 5a7baa7 | 2014-06-17 16:11:09 +0300 | [diff] [blame] | 1402 | |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1403 | #define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ |
Alexander Lobakin | ecb8fed | 2020-11-01 13:17:17 +0000 | [diff] [blame] | 1404 | NETIF_F_GSO_SOFTWARE) |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1405 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1406 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1407 | static void bond_compute_features(struct bonding *bond) |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 1408 | { |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 1409 | unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE | |
| 1410 | IFF_XMIT_DST_RELEASE_PERM; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1411 | netdev_features_t vlan_features = BOND_VLAN_FEATURES; |
Or Gerlitz | 5a7baa7 | 2014-06-17 16:11:09 +0300 | [diff] [blame] | 1412 | netdev_features_t enc_features = BOND_ENC_FEATURES; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1413 | #ifdef CONFIG_XFRM_OFFLOAD |
| 1414 | netdev_features_t xfrm_features = BOND_XFRM_FEATURES; |
| 1415 | #endif /* CONFIG_XFRM_OFFLOAD */ |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1416 | netdev_features_t mpls_features = BOND_MPLS_FEATURES; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1417 | struct net_device *bond_dev = bond->dev; |
| 1418 | struct list_head *iter; |
| 1419 | struct slave *slave; |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1420 | unsigned short max_hard_header_len = ETH_HLEN; |
Sarveshwar Bandi | 0e376bd | 2012-11-21 04:35:03 +0000 | [diff] [blame] | 1421 | unsigned int gso_max_size = GSO_MAX_SIZE; |
| 1422 | u16 gso_max_segs = GSO_MAX_SEGS; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 1423 | |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 1424 | if (!bond_has_slaves(bond)) |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 1425 | goto done; |
Michal Kubeček | a9b3ace | 2014-05-20 08:29:35 +0200 | [diff] [blame] | 1426 | vlan_features &= NETIF_F_ALL_FOR_ALL; |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1427 | mpls_features &= NETIF_F_ALL_FOR_ALL; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 1428 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 1429 | bond_for_each_slave(bond, slave, iter) { |
Jay Vosburgh | 278339a | 2009-08-28 12:05:12 +0000 | [diff] [blame] | 1430 | vlan_features = netdev_increment_features(vlan_features, |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1431 | slave->dev->vlan_features, BOND_VLAN_FEATURES); |
| 1432 | |
Or Gerlitz | 5a7baa7 | 2014-06-17 16:11:09 +0300 | [diff] [blame] | 1433 | enc_features = netdev_increment_features(enc_features, |
| 1434 | slave->dev->hw_enc_features, |
| 1435 | BOND_ENC_FEATURES); |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1436 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 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 | |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1443 | mpls_features = netdev_increment_features(mpls_features, |
| 1444 | slave->dev->mpls_features, |
| 1445 | BOND_MPLS_FEATURES); |
| 1446 | |
Eric Dumazet | b6fe83e | 2012-07-17 12:19:48 +0000 | [diff] [blame] | 1447 | dst_release_flag &= slave->dev->priv_flags; |
Jay Vosburgh | 54ef313 | 2006-09-22 21:53:39 -0700 | [diff] [blame] | 1448 | if (slave->dev->hard_header_len > max_hard_header_len) |
| 1449 | max_hard_header_len = slave->dev->hard_header_len; |
Sarveshwar Bandi | 0e376bd | 2012-11-21 04:35:03 +0000 | [diff] [blame] | 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); |
Jay Vosburgh | 54ef313 | 2006-09-22 21:53:39 -0700 | [diff] [blame] | 1453 | } |
Paolo Abeni | 19cdead | 2017-04-27 19:29:34 +0200 | [diff] [blame] | 1454 | bond_dev->hard_header_len = max_hard_header_len; |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 1455 | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 1456 | done: |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1457 | bond_dev->vlan_features = vlan_features; |
Willem de Bruijn | 8eea1ca | 2018-05-22 11:34:40 -0400 | [diff] [blame] | 1458 | bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL | |
YueHaibing | d595b03 | 2019-08-07 10:19:59 +0800 | [diff] [blame] | 1459 | NETIF_F_HW_VLAN_CTAG_TX | |
Alexander Lobakin | ecb8fed | 2020-11-01 13:17:17 +0000 | [diff] [blame] | 1460 | NETIF_F_HW_VLAN_STAG_TX; |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1461 | #ifdef CONFIG_XFRM_OFFLOAD |
| 1462 | bond_dev->hw_enc_features |= xfrm_features; |
| 1463 | #endif /* CONFIG_XFRM_OFFLOAD */ |
Ariel Levkovich | 2e770b5 | 2019-06-03 22:36:46 +0000 | [diff] [blame] | 1464 | bond_dev->mpls_features = mpls_features; |
Eric Dumazet | 6d872df | 2021-11-19 07:43:32 -0800 | [diff] [blame] | 1465 | netif_set_gso_max_segs(bond_dev, gso_max_segs); |
Sarveshwar Bandi | 0e376bd | 2012-11-21 04:35:03 +0000 | [diff] [blame] | 1466 | netif_set_gso_max_size(bond_dev, gso_max_size); |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 1467 | |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 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; |
Eric Dumazet | b6fe83e | 2012-07-17 12:19:48 +0000 | [diff] [blame] | 1472 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 1473 | netdev_change_features(bond_dev); |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 1474 | } |
| 1475 | |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 1476 | static void bond_setup_by_slave(struct net_device *bond_dev, |
| 1477 | struct net_device *slave_dev) |
| 1478 | { |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 1479 | bond_dev->header_ops = slave_dev->header_ops; |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 1480 | |
| 1481 | bond_dev->type = slave_dev->type; |
| 1482 | bond_dev->hard_header_len = slave_dev->hard_header_len; |
Eric Dumazet | f32f193 | 2020-09-25 06:38:07 -0700 | [diff] [blame] | 1483 | bond_dev->needed_headroom = slave_dev->needed_headroom; |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 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 | |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1490 | /* On bonding slaves other than the currently active slave, suppress |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 1491 | * duplicates except for alb non-mcast/bcast. |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1492 | */ |
| 1493 | static bool bond_should_deliver_exact_match(struct sk_buff *skb, |
Jiri Pirko | 0bd80da | 2011-03-16 08:45:23 +0000 | [diff] [blame] | 1494 | struct slave *slave, |
| 1495 | struct bonding *bond) |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1496 | { |
Jiri Pirko | 2d7011c | 2011-03-16 08:46:43 +0000 | [diff] [blame] | 1497 | if (bond_is_slave_inactive(slave)) { |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1498 | if (BOND_MODE(bond) == BOND_MODE_ALB && |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1499 | skb->pkt_type != PACKET_BROADCAST && |
| 1500 | skb->pkt_type != PACKET_MULTICAST) |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1501 | return false; |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1502 | return true; |
| 1503 | } |
| 1504 | return false; |
| 1505 | } |
| 1506 | |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 1507 | static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb) |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1508 | { |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 1509 | struct sk_buff *skb = *pskb; |
Jiri Pirko | f1c1775 | 2011-03-12 03:14:35 +0000 | [diff] [blame] | 1510 | struct slave *slave; |
Jiri Pirko | 0bd80da | 2011-03-16 08:45:23 +0000 | [diff] [blame] | 1511 | struct bonding *bond; |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 1512 | int (*recv_probe)(const struct sk_buff *, struct bonding *, |
| 1513 | struct slave *); |
Jiri Bohac | 13a8e0c | 2012-05-09 01:01:40 +0000 | [diff] [blame] | 1514 | int ret = RX_HANDLER_ANOTHER; |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1515 | |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 1516 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 1517 | if (unlikely(!skb)) |
| 1518 | return RX_HANDLER_CONSUMED; |
| 1519 | |
| 1520 | *pskb = skb; |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1521 | |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 1522 | slave = bond_slave_get_rcu(skb->dev); |
| 1523 | bond = slave->bond; |
Jiri Pirko | 0bd80da | 2011-03-16 08:45:23 +0000 | [diff] [blame] | 1524 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 1525 | recv_probe = READ_ONCE(bond->recv_probe); |
Mitsuo Hayasaka | 4d97480 | 2011-10-12 16:04:29 +0000 | [diff] [blame] | 1526 | if (recv_probe) { |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 1527 | ret = recv_probe(skb, bond, slave); |
| 1528 | if (ret == RX_HANDLER_CONSUMED) { |
| 1529 | consume_skb(skb); |
| 1530 | return ret; |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Michal Soltys | 3c963a3 | 2019-02-18 17:55:28 +0100 | [diff] [blame] | 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. |
Mahesh Bandewar | 6a9e461 | 2018-09-24 14:39:42 -0700 | [diff] [blame] | 1544 | */ |
Michal Soltys | 3c963a3 | 2019-02-18 17:55:28 +0100 | [diff] [blame] | 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; |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 1548 | return RX_HANDLER_EXACT; |
Michal Soltys | 3c963a3 | 2019-02-18 17:55:28 +0100 | [diff] [blame] | 1549 | } |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1550 | |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 1551 | skb->dev = bond->dev; |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1552 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1553 | if (BOND_MODE(bond) == BOND_MODE_ALB && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 1554 | netif_is_bridge_port(bond->dev) && |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1555 | skb->pkt_type == PACKET_HOST) { |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1556 | |
Changli Gao | 541ac7c | 2011-03-02 21:07:14 +0000 | [diff] [blame] | 1557 | if (unlikely(skb_cow_head(skb, |
| 1558 | skb->data - skb_mac_header(skb)))) { |
| 1559 | kfree_skb(skb); |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 1560 | return RX_HANDLER_CONSUMED; |
Changli Gao | 541ac7c | 2011-03-02 21:07:14 +0000 | [diff] [blame] | 1561 | } |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1562 | bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, |
| 1563 | bond->dev->addr_len); |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Jiri Bohac | 13a8e0c | 2012-05-09 01:01:40 +0000 | [diff] [blame] | 1566 | return ret; |
Jiri Pirko | 5b2c4dd | 2011-02-23 09:05:42 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 1569 | static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond) |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 1570 | { |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 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 | |
John Hurley | f44aa9e | 2018-05-23 19:22:52 -0700 | [diff] [blame] | 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; |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 1603 | case BOND_XMIT_POLICY_VLAN_SRCMAC: |
| 1604 | return NETDEV_LAG_HASH_VLAN_SRCMAC; |
John Hurley | f44aa9e | 2018-05-23 19:22:52 -0700 | [diff] [blame] | 1605 | default: |
| 1606 | return NETDEV_LAG_HASH_UNKNOWN; |
| 1607 | } |
| 1608 | } |
| 1609 | |
David Ahern | 42ab19e | 2017-10-04 17:48:47 -0700 | [diff] [blame] | 1610 | static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave, |
| 1611 | struct netlink_ext_ack *extack) |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 1612 | { |
| 1613 | struct netdev_lag_upper_info lag_upper_info; |
John Hurley | f44aa9e | 2018-05-23 19:22:52 -0700 | [diff] [blame] | 1614 | enum netdev_lag_tx_type type; |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 1615 | |
John Hurley | f44aa9e | 2018-05-23 19:22:52 -0700 | [diff] [blame] | 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); |
Xin Long | 4597efe | 2017-10-24 13:54:18 +0800 | [diff] [blame] | 1619 | |
| 1620 | return netdev_master_upper_dev_link(slave->dev, bond->dev, slave, |
| 1621 | &lag_upper_info, extack); |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 1624 | static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave) |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 1625 | { |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 1626 | netdev_upper_dev_unlink(slave->dev, bond->dev); |
| 1627 | slave->dev->flags &= ~IFF_SLAVE; |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 1630 | static void slave_kobj_release(struct kobject *kobj) |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 1631 | { |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 1632 | struct slave *slave = to_slave(kobj); |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 1633 | struct bonding *bond = bond_get_bond_by_slave(slave); |
| 1634 | |
Mahesh Bandewar | d4859d7 | 2018-09-24 14:40:11 -0700 | [diff] [blame] | 1635 | cancel_delayed_work_sync(&slave->notify_work); |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1636 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 1637 | kfree(SLAVE_AD_INFO(slave)); |
| 1638 | |
| 1639 | kfree(slave); |
| 1640 | } |
| 1641 | |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 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; |
Johannes Berg | 35d96e6 | 2021-05-17 16:13:35 +0200 | [diff] [blame] | 1672 | INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work); |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 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 | } |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 1685 | |
| 1686 | return slave; |
| 1687 | } |
| 1688 | |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 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 | |
Moni Shoua | 69e6113 | 2015-02-03 16:48:31 +0200 | [diff] [blame] | 1704 | static void bond_netdev_notify_work(struct work_struct *_work) |
| 1705 | { |
Mahesh Bandewar | d4859d7 | 2018-09-24 14:40:11 -0700 | [diff] [blame] | 1706 | struct slave *slave = container_of(_work, struct slave, |
| 1707 | notify_work.work); |
Moni Shoua | 69e6113 | 2015-02-03 16:48:31 +0200 | [diff] [blame] | 1708 | |
Mahesh Bandewar | d4859d7 | 2018-09-24 14:40:11 -0700 | [diff] [blame] | 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 | } |
Moni Shoua | 69e6113 | 2015-02-03 16:48:31 +0200 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | void bond_queue_slave_event(struct slave *slave) |
| 1722 | { |
Mahesh Bandewar | d4859d7 | 2018-09-24 14:40:11 -0700 | [diff] [blame] | 1723 | queue_delayed_work(slave->bond->wq, &slave->notify_work, 0); |
Moni Shoua | 69e6113 | 2015-02-03 16:48:31 +0200 | [diff] [blame] | 1724 | } |
| 1725 | |
Jiri Pirko | f7c7eb7 | 2015-12-03 12:12:20 +0100 | [diff] [blame] | 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 | |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | /* enslave device <slave> to bond device <master> */ |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 1751 | int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, |
| 1752 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 1754 | struct bonding *bond = netdev_priv(bond_dev); |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 1755 | const struct net_device_ops *slave_ops = slave_dev->netdev_ops; |
Veaceslav Falico | c8c2390 | 2013-09-25 09:20:25 +0200 | [diff] [blame] | 1756 | struct slave *new_slave = NULL, *prev_slave; |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1757 | struct sockaddr_storage ss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | int link_reporting; |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 1759 | int res = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
Di Zhu | 4d293fe | 2021-06-23 11:21:08 +0800 | [diff] [blame] | 1761 | if (slave_dev->flags & IFF_MASTER && |
| 1762 | !netif_is_bond_master(slave_dev)) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1763 | BOND_NL_ERR(bond_dev, extack, |
Antoine Tenart | 1b3f78d | 2021-08-16 12:08:28 +0200 | [diff] [blame] | 1764 | "Device type (master device) cannot be enslaved"); |
Di Zhu | 3c9ef51 | 2021-06-22 11:09:29 +0800 | [diff] [blame] | 1765 | return -EPERM; |
| 1766 | } |
| 1767 | |
Ben Hutchings | c772dde | 2012-12-07 06:15:32 +0000 | [diff] [blame] | 1768 | if (!bond->params.use_carrier && |
| 1769 | slave_dev->ethtool_ops->get_link == NULL && |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 1770 | slave_ops->ndo_eth_ioctl == NULL) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1771 | slave_warn(bond_dev, slave_dev, "no link monitoring support\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Mahesh Bandewar | 24b27fc | 2016-09-01 22:18:34 -0700 | [diff] [blame] | 1774 | /* already in-use? */ |
| 1775 | if (netdev_is_rx_handler_busy(slave_dev)) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1776 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 1777 | "Device is in use and cannot be enslaved"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | return -EBUSY; |
| 1779 | } |
| 1780 | |
Jiri Bohac | 09a89c2 | 2014-02-26 18:20:13 +0100 | [diff] [blame] | 1781 | if (bond_dev == slave_dev) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1782 | BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself."); |
Jiri Bohac | 09a89c2 | 2014-02-26 18:20:13 +0100 | [diff] [blame] | 1783 | return -EPERM; |
| 1784 | } |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1789 | slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n"); |
Jiri Pirko | 55462cf | 2012-10-14 04:30:56 +0000 | [diff] [blame] | 1790 | if (vlan_uses_dev(bond_dev)) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1791 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 1792 | "Can not enslave VLAN challenged device to VLAN enabled bond"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | return -EPERM; |
| 1794 | } else { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | } |
| 1797 | } else { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1798 | slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 1801 | if (slave_dev->features & NETIF_F_HW_ESP) |
| 1802 | slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n"); |
| 1803 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1804 | /* Old ifenslave binaries are no longer supported. These can |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 1805 | * be identified with moderate accuracy by the state of the slave: |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1806 | * the current ifenslave will set the interface down prior to |
| 1807 | * enslaving it; the old ifenslave will not. |
| 1808 | */ |
yzhu1 | ce3ea1c | 2015-12-03 18:00:55 +0800 | [diff] [blame] | 1809 | if (slave_dev->flags & IFF_UP) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1810 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 1811 | "Device can not be enslaved while up"); |
Zhang Shengju | 1e2a886 | 2016-02-09 10:37:46 +0000 | [diff] [blame] | 1812 | return -EPERM; |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 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 | */ |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 1822 | if (!bond_has_slaves(bond)) { |
Moni Shoua | e36b9d1 | 2009-07-15 04:56:31 +0000 | [diff] [blame] | 1823 | if (bond_dev->type != slave_dev->type) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1824 | slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n", |
| 1825 | bond_dev->type, slave_dev->type); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1826 | |
Amerigo Wang | b7bc2a5 | 2012-08-09 22:14:57 +0000 | [diff] [blame] | 1827 | res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, |
| 1828 | bond_dev); |
Jiri Pirko | 3ca5b40 | 2010-03-10 10:29:35 +0000 | [diff] [blame] | 1829 | res = notifier_to_errno(res); |
| 1830 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1831 | slave_err(bond_dev, slave_dev, "refused to change device type\n"); |
Zhang Shengju | 1e2a886 | 2016-02-09 10:37:46 +0000 | [diff] [blame] | 1832 | return -EBUSY; |
Jiri Pirko | 3ca5b40 | 2010-03-10 10:29:35 +0000 | [diff] [blame] | 1833 | } |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1834 | |
Jiri Pirko | 32a806c | 2010-03-19 04:00:23 +0000 | [diff] [blame] | 1835 | /* Flush unicast and multicast addresses */ |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 1836 | dev_uc_flush(bond_dev); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1837 | dev_mc_flush(bond_dev); |
Jiri Pirko | 32a806c | 2010-03-19 04:00:23 +0000 | [diff] [blame] | 1838 | |
Moni Shoua | e36b9d1 | 2009-07-15 04:56:31 +0000 | [diff] [blame] | 1839 | if (slave_dev->type != ARPHRD_ETHER) |
| 1840 | bond_setup_by_slave(bond_dev, slave_dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 1841 | else { |
Moni Shoua | e36b9d1 | 2009-07-15 04:56:31 +0000 | [diff] [blame] | 1842 | ether_setup(bond_dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 1843 | bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 1844 | } |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1845 | |
Amerigo Wang | b7bc2a5 | 2012-08-09 22:14:57 +0000 | [diff] [blame] | 1846 | call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, |
| 1847 | bond_dev); |
Moni Shoua | e36b9d1 | 2009-07-15 04:56:31 +0000 | [diff] [blame] | 1848 | } |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 1849 | } else if (bond_dev->type != slave_dev->type) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1850 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 1851 | "Device type is different from other slaves"); |
Zhang Shengju | 1e2a886 | 2016-02-09 10:37:46 +0000 | [diff] [blame] | 1852 | return -EINVAL; |
Moni Shoua | 872254d | 2007-10-09 19:43:38 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Mark Bloch | 1533e77 | 2016-07-21 11:52:55 +0300 | [diff] [blame] | 1855 | if (slave_dev->type == ARPHRD_INFINIBAND && |
| 1856 | BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 1857 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 1858 | "Only active-backup mode is supported for infiniband slaves"); |
Mark Bloch | 1533e77 | 2016-07-21 11:52:55 +0300 | [diff] [blame] | 1859 | res = -EOPNOTSUPP; |
| 1860 | goto err_undo_flags; |
| 1861 | } |
| 1862 | |
| 1863 | if (!slave_ops->ndo_set_mac_address || |
| 1864 | slave_dev->type == ARPHRD_INFINIBAND) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1865 | slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n"); |
Veaceslav Falico | f544244 | 2014-07-15 13:26:01 +0200 | [diff] [blame] | 1866 | if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP && |
| 1867 | bond->params.fail_over_mac != BOND_FOM_ACTIVE) { |
| 1868 | if (!bond_has_slaves(bond)) { |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 1869 | bond->params.fail_over_mac = BOND_FOM_ACTIVE; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1870 | slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n"); |
Veaceslav Falico | f544244 | 2014-07-15 13:26:01 +0200 | [diff] [blame] | 1871 | } else { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 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"); |
Veaceslav Falico | f544244 | 2014-07-15 13:26:01 +0200 | [diff] [blame] | 1874 | res = -EOPNOTSUPP; |
| 1875 | goto err_undo_flags; |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 1876 | } |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 1877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Amerigo Wang | 8d8fc29 | 2011-05-19 21:39:10 +0000 | [diff] [blame] | 1880 | call_netdevice_notifiers(NETDEV_JOIN, slave_dev); |
| 1881 | |
Jiri Pirko | c20811a | 2010-05-19 01:14:29 +0000 | [diff] [blame] | 1882 | /* If this is the first slave, then we need to set the master's hardware |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1883 | * address to be the same as the slave's. |
| 1884 | */ |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 1885 | if (!bond_has_slaves(bond) && |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 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 | } |
Jiri Pirko | c20811a | 2010-05-19 01:14:29 +0000 | [diff] [blame] | 1891 | |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 1892 | new_slave = bond_alloc_slave(bond, slave_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | if (!new_slave) { |
| 1894 | res = -ENOMEM; |
| 1895 | goto err_undo_flags; |
| 1896 | } |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 1897 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1898 | /* Set the new_slave's queue_id to be zero. Queue ID mapping |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 1899 | * is set via sysfs or module option if desired. |
| 1900 | */ |
| 1901 | new_slave->queue_id = 0; |
| 1902 | |
Jiri Pirko | b15ba0f | 2010-05-18 05:42:40 +0000 | [diff] [blame] | 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) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1907 | slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res); |
Jiri Pirko | b15ba0f | 2010-05-18 05:42:40 +0000 | [diff] [blame] | 1908 | goto err_free; |
| 1909 | } |
| 1910 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1911 | /* Save slave's original ("permanent") mac address for modes |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1912 | * that need it, and for restoring it upon release, and then |
| 1913 | * set it to the master's address |
| 1914 | */ |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1915 | bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr, |
| 1916 | slave_dev->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 1918 | if (!bond->params.fail_over_mac || |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 1919 | BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1920 | /* Set slave to master's mac address. The application already |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 1921 | * set the master's mac address to that of the first slave |
| 1922 | */ |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 1923 | memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len); |
| 1924 | ss.ss_family = slave_dev->type; |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 1925 | res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, |
| 1926 | extack); |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 1927 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1928 | slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res); |
Jiri Pirko | b15ba0f | 2010-05-18 05:42:40 +0000 | [diff] [blame] | 1929 | goto err_restore_mtu; |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 1930 | } |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
Karl Heiss | 03d84a5 | 2016-01-11 08:28:43 -0500 | [diff] [blame] | 1933 | /* set slave flag before open to prevent IPv6 addrconf */ |
| 1934 | slave_dev->flags |= IFF_SLAVE; |
| 1935 | |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1936 | /* open the slave since the application closed it */ |
Petr Machata | 00f54e6 | 2018-12-06 17:05:36 +0000 | [diff] [blame] | 1937 | res = dev_open(slave_dev, extack); |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 1938 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1939 | slave_err(bond_dev, slave_dev, "Opening slave failed\n"); |
Veaceslav Falico | 1f718f0 | 2013-09-25 09:20:10 +0200 | [diff] [blame] | 1940 | goto err_restore_mac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Jay Vosburgh | 0b680e7 | 2006-09-22 21:54:10 -0700 | [diff] [blame] | 1943 | slave_dev->priv_flags |= IFF_BONDING; |
Andy Gospodarek | 5f0c5f7 | 2014-09-28 22:34:37 -0400 | [diff] [blame] | 1944 | /* initialize slave stats */ |
| 1945 | dev_get_stats(new_slave->dev, &new_slave->slave_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
Holger Eitzenberger | 5840205 | 2008-12-09 23:07:13 -0800 | [diff] [blame] | 1947 | if (bond_is_lb(bond)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 1952 | if (res) |
Jay Vosburgh | 569f0c4 | 2008-05-02 18:06:02 -0700 | [diff] [blame] | 1953 | goto err_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
Wei Yongjun | b8e2fde | 2013-08-23 10:45:07 +0800 | [diff] [blame] | 1956 | res = vlan_vids_add_by_dev(slave_dev, bond_dev); |
| 1957 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 1958 | slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n"); |
Xin Long | ae42cc6 | 2018-03-26 01:16:46 +0800 | [diff] [blame] | 1959 | goto err_close; |
nikolay@redhat.com | 1ff412a | 2013-08-06 12:40:15 +0200 | [diff] [blame] | 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
Veaceslav Falico | c8c2390 | 2013-09-25 09:20:25 +0200 | [diff] [blame] | 1962 | prev_slave = bond_last_slave(bond); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
| 1964 | new_slave->delay = 0; |
| 1965 | new_slave->link_failure_count = 0; |
| 1966 | |
Andreas Born | ad729bc9 | 2017-08-10 06:41:44 +0200 | [diff] [blame] | 1967 | if (bond_update_speed_duplex(new_slave) && |
| 1968 | bond_needs_speed_duplex(bond)) |
Mahesh Bandewar | 3f3c278 | 2017-04-03 18:38:39 -0700 | [diff] [blame] | 1969 | new_slave->link = BOND_LINK_DOWN; |
Veaceslav Falico | 876254a | 2013-03-12 06:31:32 +0000 | [diff] [blame] | 1970 | |
Veaceslav Falico | 49f17de | 2014-02-18 07:48:47 +0100 | [diff] [blame] | 1971 | new_slave->last_rx = jiffies - |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 1972 | (msecs_to_jiffies(bond->params.arp_interval) + 1); |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 1973 | for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) |
Veaceslav Falico | 49f17de | 2014-02-18 07:48:47 +0100 | [diff] [blame] | 1974 | new_slave->target_last_arp_rx[i] = new_slave->last_rx; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 1980 | /* miimon is set but a bonded network driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | } else if (link_reporting == -1) { |
| 1990 | /* unable get link status using mii/ethtool */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | /* check for initial state */ |
Aviv Heller | a30b016 | 2016-07-05 12:09:47 +0300 | [diff] [blame] | 1996 | new_slave->link = BOND_LINK_NOCHANGE; |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 1997 | if (bond->params.miimon) { |
| 1998 | if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) { |
| 1999 | if (bond->params.updelay) { |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 2000 | bond_set_slave_link_state(new_slave, |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2001 | BOND_LINK_BACK, |
| 2002 | BOND_SLAVE_NOTIFY_NOW); |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 2003 | new_slave->delay = bond->params.updelay; |
| 2004 | } else { |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 2005 | bond_set_slave_link_state(new_slave, |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2006 | BOND_LINK_UP, |
| 2007 | BOND_SLAVE_NOTIFY_NOW); |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 2008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | } else { |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2010 | bond_set_slave_link_state(new_slave, BOND_LINK_DOWN, |
| 2011 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 2013 | } else if (bond->params.arp_interval) { |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 2014 | bond_set_slave_link_state(new_slave, |
| 2015 | (netif_carrier_ok(slave_dev) ? |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2016 | BOND_LINK_UP : BOND_LINK_DOWN), |
| 2017 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | } else { |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2019 | bond_set_slave_link_state(new_slave, BOND_LINK_UP, |
| 2020 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 2023 | if (new_slave->link != BOND_LINK_DOWN) |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 2024 | new_slave->last_link_up = jiffies; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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")); |
Michal Kubeček | f31c793 | 2012-04-17 02:02:06 +0000 | [diff] [blame] | 2028 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 2029 | if (bond_uses_primary(bond) && bond->params.primary[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | /* if there is a primary slave, remember it */ |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 2031 | if (strcmp(bond->params.primary, new_slave->dev->name) == 0) { |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2032 | rcu_assign_pointer(bond->primary_slave, new_slave); |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 2033 | bond->force_primary = true; |
| 2034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2037 | switch (BOND_MODE(bond)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | case BOND_MODE_ACTIVEBACKUP: |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 2039 | bond_set_slave_inactive_flags(new_slave, |
| 2040 | BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 2047 | bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | /* if this is the first slave */ |
Veaceslav Falico | 23c147e | 2013-09-27 15:10:57 +0200 | [diff] [blame] | 2049 | if (!prev_slave) { |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 2050 | SLAVE_AD_INFO(new_slave)->id = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Peter Pan(潘卫平) | 56d00c67 | 2011-06-08 21:19:02 +0000 | [diff] [blame] | 2054 | bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } else { |
dingtianhong | 3fdddd8 | 2014-05-12 15:08:43 +0800 | [diff] [blame] | 2056 | SLAVE_AD_INFO(new_slave)->id = |
| 2057 | SLAVE_AD_INFO(prev_slave)->id + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | bond_3ad_bind_slave(new_slave); |
| 2061 | break; |
| 2062 | case BOND_MODE_TLB: |
| 2063 | case BOND_MODE_ALB: |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 2064 | bond_set_active_slave(new_slave); |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 2065 | bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | break; |
| 2067 | default: |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2068 | slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
| 2070 | /* always active in trunk mode */ |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 2071 | bond_set_active_slave(new_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2077 | if (!rcu_access_pointer(bond->curr_active_slave) && |
| 2078 | new_slave->link == BOND_LINK_UP) |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 2079 | rcu_assign_pointer(bond->curr_active_slave, new_slave); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | break; |
| 2082 | } /* switch(bond_mode) */ |
| 2083 | |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 2084 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Xin Long | ddea788 | 2018-04-22 19:11:50 +0800 | [diff] [blame] | 2085 | if (bond->dev->npinfo) { |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2086 | if (slave_enable_netpoll(new_slave)) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2087 | slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n"); |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2088 | res = -EBUSY; |
stephen hemminger | f7d9821 | 2011-12-31 13:26:46 +0000 | [diff] [blame] | 2089 | goto err_detach; |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2090 | } |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 2091 | } |
| 2092 | #endif |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2093 | |
Michal Kubeček | fbe168b | 2014-11-13 07:54:50 +0100 | [diff] [blame] | 2094 | if (!(bond_dev->features & NETIF_F_LRO)) |
| 2095 | dev_disable_lro(slave_dev); |
| 2096 | |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 2097 | res = netdev_rx_handler_register(slave_dev, bond_handle_frame, |
| 2098 | new_slave); |
| 2099 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2100 | slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res); |
Veaceslav Falico | 5831d66 | 2013-09-25 09:20:32 +0200 | [diff] [blame] | 2101 | goto err_detach; |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
David Ahern | 42ab19e | 2017-10-04 17:48:47 -0700 | [diff] [blame] | 2104 | res = bond_master_upper_dev_link(bond, new_slave, extack); |
Veaceslav Falico | 1f718f0 | 2013-09-25 09:20:10 +0200 | [diff] [blame] | 2105 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2106 | slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res); |
Veaceslav Falico | 1f718f0 | 2013-09-25 09:20:10 +0200 | [diff] [blame] | 2107 | goto err_unregister; |
| 2108 | } |
| 2109 | |
Tobias Waldekranz | 32d4c56 | 2021-01-13 09:42:51 +0100 | [diff] [blame] | 2110 | bond_lower_state_changed(new_slave); |
| 2111 | |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 2112 | res = bond_sysfs_slave_add(new_slave); |
| 2113 | if (res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2114 | slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res); |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 2115 | goto err_upper_unlink; |
| 2116 | } |
| 2117 | |
Xin Long | ae42cc6 | 2018-03-26 01:16:46 +0800 | [diff] [blame] | 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); |
Xin Long | 9f5a90c | 2018-03-26 01:16:47 +0800 | [diff] [blame] | 2132 | if (res) { |
| 2133 | if (bond_dev->flags & IFF_PROMISC) |
| 2134 | dev_set_promiscuity(slave_dev, -1); |
Xin Long | ae42cc6 | 2018-03-26 01:16:46 +0800 | [diff] [blame] | 2135 | goto err_sysfs_del; |
Xin Long | 9f5a90c | 2018-03-26 01:16:47 +0800 | [diff] [blame] | 2136 | } |
Xin Long | ae42cc6 | 2018-03-26 01:16:46 +0800 | [diff] [blame] | 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 | |
Veaceslav Falico | 5378c2e | 2013-10-21 11:48:30 +0200 | [diff] [blame] | 2152 | bond->slave_cnt++; |
| 2153 | bond_compute_features(bond); |
| 2154 | bond_set_carrier(bond); |
| 2155 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 2156 | if (bond_uses_primary(bond)) { |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 2157 | block_netpoll_tx(); |
Veaceslav Falico | 5378c2e | 2013-10-21 11:48:30 +0200 | [diff] [blame] | 2158 | bond_select_active_slave(bond); |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 2159 | unblock_netpoll_tx(); |
Veaceslav Falico | 5378c2e | 2013-10-21 11:48:30 +0200 | [diff] [blame] | 2160 | } |
Veaceslav Falico | 1f718f0 | 2013-09-25 09:20:10 +0200 | [diff] [blame] | 2161 | |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 2162 | if (bond_mode_can_use_xmit_hash(bond)) |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 2163 | bond_update_slave_arr(bond, NULL); |
| 2164 | |
Debabrata Banerjee | 21706ee | 2018-05-09 19:32:11 -0400 | [diff] [blame] | 2165 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 2166 | if (!slave_dev->netdev_ops->ndo_bpf || |
| 2167 | !slave_dev->netdev_ops->ndo_xdp_xmit) { |
| 2168 | if (bond->xdp_prog) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 2169 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 2170 | "Slave does not support XDP"); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 2171 | res = -EOPNOTSUPP; |
| 2172 | goto err_sysfs_del; |
| 2173 | } |
Jussi Maki | 6d5f1ef | 2021-09-06 10:56:37 +0200 | [diff] [blame] | 2174 | } else if (bond->xdp_prog) { |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 2183 | SLAVE_NL_ERR(bond_dev, slave_dev, extack, |
| 2184 | "Slave has XDP program loaded, please unload before enslaving"); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
| 2203 | /* enslave is successful */ |
Moni Shoua | 69e6113 | 2015-02-03 16:48:31 +0200 | [diff] [blame] | 2204 | bond_queue_slave_event(new_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | return 0; |
| 2206 | |
| 2207 | /* Undo stages on error */ |
Xin Long | ae42cc6 | 2018-03-26 01:16:46 +0800 | [diff] [blame] | 2208 | err_sysfs_del: |
| 2209 | bond_sysfs_slave_del(new_slave); |
| 2210 | |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 2211 | err_upper_unlink: |
Jiri Pirko | 41f0b04 | 2015-12-03 12:12:14 +0100 | [diff] [blame] | 2212 | bond_upper_dev_unlink(bond, new_slave); |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 2213 | |
Veaceslav Falico | 1f718f0 | 2013-09-25 09:20:10 +0200 | [diff] [blame] | 2214 | err_unregister: |
| 2215 | netdev_rx_handler_unregister(slave_dev); |
| 2216 | |
stephen hemminger | f7d9821 | 2011-12-31 13:26:46 +0000 | [diff] [blame] | 2217 | err_detach: |
nikolay@redhat.com | 1ff412a | 2013-08-06 12:40:15 +0200 | [diff] [blame] | 2218 | vlan_vids_del_by_dev(slave_dev, bond_dev); |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2219 | if (rcu_access_pointer(bond->primary_slave) == new_slave) |
| 2220 | RCU_INIT_POINTER(bond->primary_slave, NULL); |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2221 | if (rcu_access_pointer(bond->curr_active_slave) == new_slave) { |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 2222 | block_netpoll_tx(); |
dingtianhong | c851703 | 2013-12-13 10:20:07 +0800 | [diff] [blame] | 2223 | bond_change_active_slave(bond, NULL); |
nikolay@redhat.com | 3c5913b53 | 2013-04-18 07:33:36 +0000 | [diff] [blame] | 2224 | bond_select_active_slave(bond); |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 2225 | unblock_netpoll_tx(); |
nikolay@redhat.com | 3c5913b53 | 2013-04-18 07:33:36 +0000 | [diff] [blame] | 2226 | } |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2227 | /* either primary_slave or curr_active_slave might've changed */ |
| 2228 | synchronize_rcu(); |
nikolay@redhat.com | fc7a72a | 2013-04-18 07:33:37 +0000 | [diff] [blame] | 2229 | slave_disable_netpoll(new_slave); |
stephen hemminger | f7d9821 | 2011-12-31 13:26:46 +0000 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | err_close: |
Taehee Yoo | 65de65d | 2019-10-21 18:47:52 +0000 | [diff] [blame] | 2232 | if (!netif_is_bond_master(slave_dev)) |
| 2233 | slave_dev->priv_flags &= ~IFF_BONDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | dev_close(slave_dev); |
| 2235 | |
| 2236 | err_restore_mac: |
Karl Heiss | 03d84a5 | 2016-01-11 08:28:43 -0500 | [diff] [blame] | 2237 | slave_dev->flags &= ~IFF_SLAVE; |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 2238 | if (!bond->params.fail_over_mac || |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2239 | BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 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 | */ |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 2244 | bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr, |
| 2245 | new_slave->dev->addr_len); |
| 2246 | ss.ss_family = slave_dev->type; |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 2247 | dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL); |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 2248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | |
Jiri Pirko | b15ba0f | 2010-05-18 05:42:40 +0000 | [diff] [blame] | 2250 | err_restore_mtu: |
| 2251 | dev_set_mtu(slave_dev, new_slave->original_mtu); |
| 2252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | err_free: |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 2254 | kobject_put(&new_slave->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
| 2256 | err_undo_flags: |
Nikolay Aleksandrov | b8fad45 | 2013-06-12 00:07:01 +0200 | [diff] [blame] | 2257 | /* Enslave of first slave has failed and we need to fix master's mac */ |
Nikolay Aleksandrov | 7d5cd2c | 2015-07-15 22:57:01 +0200 | [diff] [blame] | 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) { |
Jay Vosburgh | 40baec2 | 2015-11-06 17:23:23 -0800 | [diff] [blame] | 2263 | dev_close(bond_dev); |
Nikolay Aleksandrov | 7d5cd2c | 2015-07-15 22:57:01 +0200 | [diff] [blame] | 2264 | ether_setup(bond_dev); |
| 2265 | bond_dev->flags |= IFF_MASTER; |
| 2266 | bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 2267 | } |
| 2268 | } |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 2269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | return res; |
| 2271 | } |
| 2272 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2273 | /* Try to release the slave device <slave> from the bond device <master> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | * It is legal to access curr_active_slave without a lock because all the function |
Nikolay Aleksandrov | 8c0bc55 | 2014-09-11 22:49:28 +0200 | [diff] [blame] | 2275 | * is RTNL-locked. If "all" is true it means that the function is being called |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2276 | * while destroying a bond interface and all slaves are being released. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2284 | static int __bond_release_one(struct net_device *bond_dev, |
| 2285 | struct net_device *slave_dev, |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 2286 | bool all, bool unregister) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 2288 | struct bonding *bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | struct slave *slave, *oldcurrent; |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 2290 | struct sockaddr_storage ss; |
Neil Horman | 5a0068d | 2013-09-27 12:22:15 -0400 | [diff] [blame] | 2291 | int old_flags = bond_dev->flags; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2292 | netdev_features_t old_features = bond_dev->features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
| 2294 | /* slave is not a slave or master is not master of this slave */ |
| 2295 | if (!(slave_dev->flags & IFF_SLAVE) || |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 2296 | !netdev_has_upper_dev(slave_dev, bond_dev)) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2297 | slave_dbg(bond_dev, slave_dev, "cannot release slave\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | return -EINVAL; |
| 2299 | } |
| 2300 | |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 2301 | block_netpoll_tx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | |
| 2303 | slave = bond_get_slave_by_dev(bond, slave_dev); |
| 2304 | if (!slave) { |
| 2305 | /* not a slave of this bond */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2306 | slave_info(bond_dev, slave_dev, "interface not enslaved\n"); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 2307 | unblock_netpoll_tx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | return -EINVAL; |
| 2309 | } |
| 2310 | |
Jiri Pirko | 57beaca | 2015-12-03 12:12:21 +0100 | [diff] [blame] | 2311 | bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW); |
| 2312 | |
sfeldma@cumulusnetworks.com | 07699f9 | 2014-01-16 22:57:49 -0800 | [diff] [blame] | 2313 | bond_sysfs_slave_del(slave); |
| 2314 | |
Andy Gospodarek | 5f0c5f7 | 2014-09-28 22:34:37 -0400 | [diff] [blame] | 2315 | /* recompute stats just before removing the slave */ |
| 2316 | bond_get_stats(bond->dev, &bond->bond_stats); |
| 2317 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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 | |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 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); |
Jiri Pirko | 35d4890 | 2011-03-22 02:38:12 +0000 | [diff] [blame] | 2333 | |
Nikolay Aleksandrov | e470259 | 2014-09-11 22:49:27 +0200 | [diff] [blame] | 2334 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | bond_3ad_unbind_slave(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | |
Yufeng Mo | 220ade7 | 2021-07-30 10:19:11 +0800 | [diff] [blame] | 2337 | bond_upper_dev_unlink(bond, slave); |
| 2338 | |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 2339 | if (bond_mode_can_use_xmit_hash(bond)) |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 2340 | bond_update_slave_arr(bond, slave); |
| 2341 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2342 | slave_info(bond_dev, slave_dev, "Releasing %s interface\n", |
| 2343 | bond_is_active_slave(slave) ? "active" : "backup"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2345 | oldcurrent = rcu_access_pointer(bond->curr_active_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 2347 | RCU_INIT_POINTER(bond->current_arp_slave, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 2349 | if (!all && (!bond->params.fail_over_mac || |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2350 | BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) { |
dingtianhong | 844223a | 2014-01-02 09:13:16 +0800 | [diff] [blame] | 2351 | if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) && |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 2352 | bond_has_slaves(bond)) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 2355 | } |
| 2356 | |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2357 | if (rtnl_dereference(bond->primary_slave) == slave) |
| 2358 | RCU_INIT_POINTER(bond->primary_slave, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | |
Nikolay Aleksandrov | 1c72cfdc9 | 2014-09-11 22:49:24 +0200 | [diff] [blame] | 2360 | if (oldcurrent == slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | bond_change_active_slave(bond, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | |
Holger Eitzenberger | 5840205 | 2008-12-09 23:07:13 -0800 | [diff] [blame] | 2363 | if (bond_is_lb(bond)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2372 | if (all) { |
Paul E. McKenney | 36708b8 | 2013-12-09 15:19:53 -0800 | [diff] [blame] | 2373 | RCU_INIT_POINTER(bond->curr_active_slave, NULL); |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2374 | } else if (oldcurrent == slave) { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2375 | /* Note that we hold RTNL over this sequence, so there |
Jay Vosburgh | 059fe7a | 2007-10-17 17:37:49 -0700 | [diff] [blame] | 2376 | * is no concern that another slave add/remove event |
| 2377 | * will interfere. |
| 2378 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | bond_select_active_slave(bond); |
Jay Vosburgh | 059fe7a | 2007-10-17 17:37:49 -0700 | [diff] [blame] | 2380 | } |
| 2381 | |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 2382 | if (!bond_has_slaves(bond)) { |
Jay Vosburgh | ff59c45 | 2006-03-27 13:27:43 -0800 | [diff] [blame] | 2383 | bond_set_carrier(bond); |
Jiri Pirko | 409cc1f | 2013-01-30 11:08:11 +0100 | [diff] [blame] | 2384 | eth_hw_addr_random(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 2387 | unblock_netpoll_tx(); |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 2388 | synchronize_rcu(); |
Nikolay Aleksandrov | ee6154e | 2014-02-26 14:20:30 +0100 | [diff] [blame] | 2389 | bond->slave_cnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 2391 | if (!bond_has_slaves(bond)) { |
Shlomo Pongratz | 2af73d4 | 2012-04-03 22:56:19 +0000 | [diff] [blame] | 2392 | call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev); |
Veaceslav Falico | 80028ea | 2013-03-06 07:10:32 +0000 | [diff] [blame] | 2393 | call_netdevice_notifiers(NETDEV_RELEASE, bond->dev); |
| 2394 | } |
Shlomo Pongratz | 2af73d4 | 2012-04-03 22:56:19 +0000 | [diff] [blame] | 2395 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 2396 | bond_compute_features(bond); |
| 2397 | if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) && |
| 2398 | (old_features & NETIF_F_VLAN_CHALLENGED)) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2399 | slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n"); |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 2400 | |
nikolay@redhat.com | 1ff412a | 2013-08-06 12:40:15 +0200 | [diff] [blame] | 2401 | vlan_vids_del_by_dev(slave_dev, bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2403 | /* If the mode uses primary, then this case was handled above by |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 2404 | * bond_change_active_slave(..., NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | */ |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 2406 | if (!bond_uses_primary(bond)) { |
Neil Horman | 5a0068d | 2013-09-27 12:22:15 -0400 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | dev_set_promiscuity(slave_dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
| 2417 | /* unset allmulti level from slave */ |
Neil Horman | 5a0068d | 2013-09-27 12:22:15 -0400 | [diff] [blame] | 2418 | if (old_flags & IFF_ALLMULTI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | dev_set_allmulti(slave_dev, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 2421 | bond_hw_addr_flush(bond_dev, slave_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2424 | slave_disable_netpoll(slave); |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 2425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | /* close slave before restoring its mac address */ |
| 2427 | dev_close(slave_dev); |
| 2428 | |
dingtianhong | 00503b6 | 2014-01-25 13:00:29 +0800 | [diff] [blame] | 2429 | if (bond->params.fail_over_mac != BOND_FOM_ACTIVE || |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2430 | BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 2431 | /* restore original ("permanent") mac address */ |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 2432 | bond_hw_addr_copy(ss.__data, slave->perm_hwaddr, |
| 2433 | slave->dev->addr_len); |
| 2434 | ss.ss_family = slave_dev->type; |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 2435 | dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL); |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 2436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 2438 | if (unregister) |
| 2439 | __dev_set_mtu(slave_dev, slave->original_mtu); |
| 2440 | else |
| 2441 | dev_set_mtu(slave_dev, slave->original_mtu); |
Jiri Pirko | b15ba0f | 2010-05-18 05:42:40 +0000 | [diff] [blame] | 2442 | |
Taehee Yoo | 65de65d | 2019-10-21 18:47:52 +0000 | [diff] [blame] | 2443 | if (!netif_is_bond_master(slave_dev)) |
| 2444 | slave_dev->priv_flags &= ~IFF_BONDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
Jamie Iles | b9ad3e9 | 2020-11-20 14:28:27 +0000 | [diff] [blame] | 2446 | kobject_put(&slave->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2448 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2451 | /* A wrapper used because of ndo_del_link */ |
| 2452 | int bond_release(struct net_device *bond_dev, struct net_device *slave_dev) |
| 2453 | { |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 2454 | return __bond_release_one(bond_dev, slave_dev, false, false); |
nikolay@redhat.com | 0896341 | 2013-02-18 14:09:42 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 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 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2460 | static int bond_release_and_destroy(struct net_device *bond_dev, |
| 2461 | struct net_device *slave_dev) |
Moni Shoua | d90a162 | 2007-10-09 19:43:43 -0700 | [diff] [blame] | 2462 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 2463 | struct bonding *bond = netdev_priv(bond_dev); |
Moni Shoua | d90a162 | 2007-10-09 19:43:43 -0700 | [diff] [blame] | 2464 | int ret; |
| 2465 | |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 2466 | ret = __bond_release_one(bond_dev, slave_dev, false, true); |
Cong Wang | 8327070 | 2020-08-14 20:05:58 -0700 | [diff] [blame] | 2467 | if (ret == 0 && !bond_has_slaves(bond) && |
| 2468 | bond_dev->reg_state != NETREG_UNREGISTERING) { |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 2469 | bond_dev->priv_flags |= IFF_DISABLE_NETPOLL; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2470 | netdev_info(bond_dev, "Destroying bond\n"); |
Nikolay Aleksandrov | 06f6d10 | 2015-07-15 21:52:51 +0200 | [diff] [blame] | 2471 | bond_remove_proc_entry(bond); |
Stephen Hemminger | 9e71626 | 2009-06-12 19:02:47 +0000 | [diff] [blame] | 2472 | unregister_netdevice(bond_dev); |
Moni Shoua | d90a162 | 2007-10-09 19:43:43 -0700 | [diff] [blame] | 2473 | } |
| 2474 | return ret; |
| 2475 | } |
| 2476 | |
Zhu Yanjun | 3d67576 | 2017-02-02 23:46:21 -0500 | [diff] [blame] | 2477 | static void bond_info_query(struct net_device *bond_dev, struct ifbond *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 2479 | struct bonding *bond = netdev_priv(bond_dev); |
Yufeng Mo | 86a5ad0 | 2021-05-20 14:18:32 +0800 | [diff] [blame] | 2480 | |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 2481 | bond_fill_ifbond(bond, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info) |
| 2485 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 2486 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 2487 | struct list_head *iter; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 2488 | int i = 0, res = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 2491 | bond_for_each_slave(bond, slave, iter) { |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 2492 | if (i++ == (int)info->slave_id) { |
Eric Dumazet | 689c96c | 2009-04-23 03:39:04 +0000 | [diff] [blame] | 2493 | res = 0; |
Moni Shoua | 69a2338 | 2015-02-03 16:48:30 +0200 | [diff] [blame] | 2494 | bond_fill_ifslave(slave, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| 2497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | |
Eric Dumazet | 689c96c | 2009-04-23 03:39:04 +0000 | [diff] [blame] | 2499 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /*-------------------------------- Monitoring -------------------------------*/ |
| 2503 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2504 | /* called with rcu_read_lock() */ |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2505 | static int bond_miimon_inspect(struct bonding *bond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | { |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 2507 | int link_state, commit = 0; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 2508 | struct list_head *iter; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2509 | struct slave *slave; |
Jiri Pirko | 41f8910 | 2009-04-24 03:57:29 +0000 | [diff] [blame] | 2510 | bool ignore_updelay; |
| 2511 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2512 | ignore_updelay = !rcu_dereference(bond->curr_active_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
dingtianhong | 4cb4f97 | 2013-12-13 10:19:39 +0800 | [diff] [blame] | 2514 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 2515 | bond_propose_link_state(slave, BOND_LINK_NOCHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2517 | link_state = bond_check_dev_link(bond, slave->dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
| 2519 | switch (slave->link) { |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2520 | case BOND_LINK_UP: |
| 2521 | if (link_state) |
| 2522 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2524 | bond_propose_link_state(slave, BOND_LINK_FAIL); |
WANG Cong | d94708a | 2017-07-25 09:44:25 -0700 | [diff] [blame] | 2525 | commit++; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2526 | slave->delay = bond->params.downdelay; |
| 2527 | if (slave->delay) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2535 | fallthrough; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2536 | case BOND_LINK_FAIL: |
| 2537 | if (link_state) { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2538 | /* recovered before downdelay expired */ |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2539 | bond_propose_link_state(slave, BOND_LINK_UP); |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 2540 | slave->last_link_up = jiffies; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Mahesh Bandewar | fb9eb89 | 2017-04-11 22:36:00 -0700 | [diff] [blame] | 2544 | commit++; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2545 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | } |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2547 | |
| 2548 | if (slave->delay <= 0) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 2549 | bond_propose_link_state(slave, BOND_LINK_DOWN); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2550 | commit++; |
| 2551 | continue; |
| 2552 | } |
| 2553 | |
| 2554 | slave->delay--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2557 | case BOND_LINK_DOWN: |
| 2558 | if (!link_state) |
| 2559 | continue; |
| 2560 | |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2561 | bond_propose_link_state(slave, BOND_LINK_BACK); |
WANG Cong | d94708a | 2017-07-25 09:44:25 -0700 | [diff] [blame] | 2562 | commit++; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2563 | slave->delay = bond->params.updelay; |
| 2564 | |
| 2565 | if (slave->delay) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2571 | fallthrough; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2572 | case BOND_LINK_BACK: |
| 2573 | if (!link_state) { |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2574 | bond_propose_link_state(slave, BOND_LINK_DOWN); |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Mahesh Bandewar | fb9eb89 | 2017-04-11 22:36:00 -0700 | [diff] [blame] | 2578 | commit++; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2579 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2581 | |
Jiri Pirko | 41f8910 | 2009-04-24 03:57:29 +0000 | [diff] [blame] | 2582 | if (ignore_updelay) |
| 2583 | slave->delay = 0; |
| 2584 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2585 | if (slave->delay <= 0) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 2586 | bond_propose_link_state(slave, BOND_LINK_UP); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2587 | commit++; |
Jiri Pirko | 41f8910 | 2009-04-24 03:57:29 +0000 | [diff] [blame] | 2588 | ignore_updelay = false; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2589 | continue; |
| 2590 | } |
| 2591 | |
| 2592 | slave->delay--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | break; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | return commit; |
| 2598 | } |
| 2599 | |
Tonghao Zhang | 7e878b6 | 2018-05-16 19:09:23 -0700 | [diff] [blame] | 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 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2618 | static void bond_miimon_commit(struct bonding *bond) |
| 2619 | { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 2620 | struct list_head *iter; |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2621 | struct slave *slave, *primary; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2622 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 2623 | bond_for_each_slave(bond, slave, iter) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 2624 | switch (slave->link_new_state) { |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2625 | case BOND_LINK_NOCHANGE: |
Thomas Falcon | 12185df | 2019-07-16 17:25:10 -0500 | [diff] [blame] | 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); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2635 | continue; |
| 2636 | |
| 2637 | case BOND_LINK_UP: |
Andreas Born | ad729bc9 | 2017-08-10 06:41:44 +0200 | [diff] [blame] | 2638 | if (bond_update_speed_duplex(slave) && |
| 2639 | bond_needs_speed_duplex(bond)) { |
Mahesh Bandewar | 3f3c278 | 2017-04-03 18:38:39 -0700 | [diff] [blame] | 2640 | slave->link = BOND_LINK_DOWN; |
Andreas Born | 11e9d78 | 2017-08-12 00:36:55 +0200 | [diff] [blame] | 2641 | if (net_ratelimit()) |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2642 | slave_warn(bond->dev, slave->dev, |
| 2643 | "failed to get link speed/duplex\n"); |
Mahesh Bandewar | b5bf0f5 | 2017-03-27 11:37:37 -0700 | [diff] [blame] | 2644 | continue; |
| 2645 | } |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2646 | bond_set_slave_link_state(slave, BOND_LINK_UP, |
| 2647 | BOND_SLAVE_NOTIFY_NOW); |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 2648 | slave->last_link_up = jiffies; |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2649 | |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2650 | primary = rtnl_dereference(bond->primary_slave); |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2651 | if (BOND_MODE(bond) == BOND_MODE_8023AD) { |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2652 | /* prevent it from being the active one */ |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 2653 | bond_set_backup_slave(slave); |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2654 | } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2655 | /* make it immediately active */ |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 2656 | bond_set_active_slave(slave); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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"); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2662 | |
Tonghao Zhang | 7e878b6 | 2018-05-16 19:09:23 -0700 | [diff] [blame] | 2663 | bond_miimon_link_change(bond, slave, BOND_LINK_UP); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 2664 | |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 2665 | if (!bond->curr_active_slave || slave == primary) |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2666 | goto do_failover; |
| 2667 | |
| 2668 | continue; |
| 2669 | |
| 2670 | case BOND_LINK_DOWN: |
Jay Vosburgh | fba4acd | 2008-10-30 17:41:14 -0700 | [diff] [blame] | 2671 | if (slave->link_failure_count < UINT_MAX) |
| 2672 | slave->link_failure_count++; |
| 2673 | |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 2674 | bond_set_slave_link_state(slave, BOND_LINK_DOWN, |
| 2675 | BOND_SLAVE_NOTIFY_NOW); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2676 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 2677 | if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP || |
| 2678 | BOND_MODE(bond) == BOND_MODE_8023AD) |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 2679 | bond_set_slave_inactive_flags(slave, |
| 2680 | BOND_SLAVE_NOTIFY_NOW); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2681 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2682 | slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n"); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2683 | |
Tonghao Zhang | 7e878b6 | 2018-05-16 19:09:23 -0700 | [diff] [blame] | 2684 | bond_miimon_link_change(bond, slave, BOND_LINK_DOWN); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 2685 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 2686 | if (slave == rcu_access_pointer(bond->curr_active_slave)) |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2687 | goto do_failover; |
| 2688 | |
| 2689 | continue; |
| 2690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | default: |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2692 | slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n", |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 2693 | slave->link_new_state); |
| 2694 | bond_propose_link_state(slave, BOND_LINK_NOCHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2696 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | } |
| 2698 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2699 | do_failover: |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 2700 | block_netpoll_tx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | bond_select_active_slave(bond); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 2702 | unblock_netpoll_tx(); |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2703 | } |
Jay Vosburgh | 059fe7a | 2007-10-17 17:37:49 -0700 | [diff] [blame] | 2704 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2705 | bond_set_carrier(bond); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2708 | /* bond_mii_monitor |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2709 | * |
| 2710 | * Really a wrapper that splits the mii monitor into two phases: an |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 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. |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2714 | */ |
stephen hemminger | 6da67d2 | 2013-12-30 10:43:41 -0800 | [diff] [blame] | 2715 | static void bond_mii_monitor(struct work_struct *work) |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2716 | { |
| 2717 | struct bonding *bond = container_of(work, struct bonding, |
| 2718 | mii_work.work); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 2719 | bool should_notify_peers = false; |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 2720 | bool commit; |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2721 | unsigned long delay; |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2722 | struct slave *slave; |
| 2723 | struct list_head *iter; |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2724 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2725 | delay = msecs_to_jiffies(bond->params.miimon); |
| 2726 | |
| 2727 | if (!bond_has_slaves(bond)) |
dingtianhong | 6b6c526 | 2013-10-24 11:09:03 +0800 | [diff] [blame] | 2728 | goto re_arm; |
dingtianhong | 6b6c526 | 2013-10-24 11:09:03 +0800 | [diff] [blame] | 2729 | |
dingtianhong | 4cb4f97 | 2013-12-13 10:19:39 +0800 | [diff] [blame] | 2730 | rcu_read_lock(); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 2731 | should_notify_peers = bond_should_notify_peers(bond); |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 2732 | commit = !!bond_miimon_inspect(bond); |
| 2733 | if (bond->send_peer_notif) { |
dingtianhong | 4cb4f97 | 2013-12-13 10:19:39 +0800 | [diff] [blame] | 2734 | rcu_read_unlock(); |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 2735 | if (rtnl_trylock()) { |
| 2736 | bond->send_peer_notif--; |
| 2737 | rtnl_unlock(); |
| 2738 | } |
| 2739 | } else { |
| 2740 | rcu_read_unlock(); |
| 2741 | } |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2742 | |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 2743 | if (commit) { |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2744 | /* Race avoidance with bond_close cancel of workqueue */ |
| 2745 | if (!rtnl_trylock()) { |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2746 | delay = 1; |
| 2747 | should_notify_peers = false; |
| 2748 | goto re_arm; |
| 2749 | } |
| 2750 | |
Mahesh Bandewar | de77ecd | 2017-03-27 11:37:33 -0700 | [diff] [blame] | 2751 | bond_for_each_slave(bond, slave, iter) { |
| 2752 | bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER); |
| 2753 | } |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2754 | bond_miimon_commit(bond); |
| 2755 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2756 | rtnl_unlock(); /* might sleep, hold no other locks */ |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 2757 | } |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2758 | |
Jay Vosburgh | f0c76d6 | 2008-07-02 18:21:58 -0700 | [diff] [blame] | 2759 | re_arm: |
Jay Vosburgh | e6d265e | 2011-10-28 15:42:50 +0000 | [diff] [blame] | 2760 | if (bond->params.miimon) |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2761 | queue_delayed_work(bond->wq, &bond->mii_work, delay); |
| 2762 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 2763 | if (should_notify_peers) { |
| 2764 | if (!rtnl_trylock()) |
| 2765 | return; |
| 2766 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); |
| 2767 | rtnl_unlock(); |
| 2768 | } |
Jay Vosburgh | 0b0eef6 | 2007-10-17 17:37:48 -0700 | [diff] [blame] | 2769 | } |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2770 | |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 2771 | static int bond_upper_dev_walk(struct net_device *upper, |
| 2772 | struct netdev_nested_priv *priv) |
David Ahern | b3208b2 | 2016-10-17 19:15:45 -0700 | [diff] [blame] | 2773 | { |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 2774 | __be32 ip = *(__be32 *)priv->data; |
David Ahern | b3208b2 | 2016-10-17 19:15:45 -0700 | [diff] [blame] | 2775 | |
| 2776 | return ip == bond_confirm_addr(upper, 0, ip); |
| 2777 | } |
| 2778 | |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2779 | static bool bond_has_this_ip(struct bonding *bond, __be32 ip) |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2780 | { |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 2781 | struct netdev_nested_priv priv = { |
| 2782 | .data = (void *)&ip, |
| 2783 | }; |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2784 | bool ret = false; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2785 | |
Andy Gospodarek | eaddcd7 | 2012-03-22 16:14:29 +0000 | [diff] [blame] | 2786 | if (ip == bond_confirm_addr(bond->dev, 0, ip)) |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2787 | return true; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2788 | |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2789 | rcu_read_lock(); |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 2790 | if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv)) |
David Ahern | b3208b2 | 2016-10-17 19:15:45 -0700 | [diff] [blame] | 2791 | ret = true; |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2792 | rcu_read_unlock(); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2793 | |
Veaceslav Falico | 50223ce | 2013-08-28 23:25:11 +0200 | [diff] [blame] | 2794 | return ret; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 2797 | /* We go to the (large) trouble of VLAN tagging ARP frames because |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2798 | * switches in VLAN mode (especially if ports are configured as |
| 2799 | * "native" to a VLAN) might not pass non-tagged frames. |
| 2800 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2801 | static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip, |
| 2802 | __be32 src_ip, struct bond_vlan_tag *tags) |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2803 | { |
| 2804 | struct sk_buff *skb; |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2805 | struct bond_vlan_tag *outer_tag = tags; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2806 | struct net_device *slave_dev = slave->dev; |
| 2807 | struct net_device *bond_dev = slave->bond->dev; |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2808 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2809 | slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n", |
| 2810 | arp_op, &dest_ip, &src_ip); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 2811 | |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 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) { |
dingtianhong | 4873ac3 | 2014-03-25 17:44:44 +0800 | [diff] [blame] | 2816 | net_err_ratelimited("ARP packet allocation failed\n"); |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2817 | return; |
| 2818 | } |
dingtianhong | fbd929f | 2014-03-25 17:44:43 +0800 | [diff] [blame] | 2819 | |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2820 | if (!tags || tags->vlan_proto == VLAN_N_VID) |
| 2821 | goto xmit; |
| 2822 | |
| 2823 | tags++; |
| 2824 | |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2825 | /* Go through all the tags backwards and add them to the packet */ |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2826 | while (tags->vlan_proto != VLAN_N_VID) { |
| 2827 | if (!tags->vlan_id) { |
| 2828 | tags++; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2829 | continue; |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2830 | } |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2831 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2832 | slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n", |
| 2833 | ntohs(outer_tag->vlan_proto), tags->vlan_id); |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 2834 | skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto, |
| 2835 | tags->vlan_id); |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2836 | if (!skb) { |
| 2837 | net_err_ratelimited("failed to insert inner VLAN tag\n"); |
| 2838 | return; |
| 2839 | } |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2840 | |
| 2841 | tags++; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2842 | } |
| 2843 | /* Set the outer tag */ |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2844 | if (outer_tag->vlan_id) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2845 | slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n", |
| 2846 | ntohs(outer_tag->vlan_proto), outer_tag->vlan_id); |
Jiri Pirko | b4bef1b | 2014-11-19 14:04:57 +0100 | [diff] [blame] | 2847 | __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto, |
| 2848 | outer_tag->vlan_id); |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2849 | } |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2850 | |
| 2851 | xmit: |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2852 | arp_xmit(skb); |
| 2853 | } |
| 2854 | |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 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 | */ |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2861 | struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev, |
| 2862 | struct net_device *end_dev, |
| 2863 | int level) |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2864 | { |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2865 | struct bond_vlan_tag *tags; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2866 | struct net_device *upper; |
| 2867 | struct list_head *iter; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2868 | |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2869 | if (start_dev == end_dev) { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2870 | tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC); |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2871 | if (!tags) |
| 2872 | return ERR_PTR(-ENOMEM); |
| 2873 | tags[level].vlan_proto = VLAN_N_VID; |
| 2874 | return tags; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2875 | } |
| 2876 | |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 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; |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2893 | } |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | static void bond_arp_send_all(struct bonding *bond, struct slave *slave) |
| 2896 | { |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2897 | struct rtable *rt; |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2898 | struct bond_vlan_tag *tags; |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2899 | __be32 *targets = bond->params.arp_targets, addr; |
dingtianhong | fbd929f | 2014-03-25 17:44:43 +0800 | [diff] [blame] | 2900 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2902 | for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2903 | slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n", |
| 2904 | __func__, &targets[i]); |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2905 | tags = NULL; |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2906 | |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2907 | /* Find out through which dev should the packet go */ |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 2908 | rt = ip_route_output(dev_net(bond->dev), targets[i], 0, |
| 2909 | RTO_ONLINK, 0); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 2910 | if (IS_ERR(rt)) { |
Veaceslav Falico | 2857276 | 2014-02-28 12:39:19 +0100 | [diff] [blame] | 2911 | /* there's no route to target - try to send arp |
| 2912 | * probe to generate any traffic (arp_validate=0) |
| 2913 | */ |
dingtianhong | 4873ac3 | 2014-03-25 17:44:44 +0800 | [diff] [blame] | 2914 | if (bond->params.arp_validate) |
David Decotigny | 0a4fd8d | 2021-09-03 23:31:29 -0700 | [diff] [blame] | 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]); |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2918 | bond_arp_send(slave, ARPOP_REQUEST, targets[i], |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2919 | 0, tags); |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2920 | continue; |
| 2921 | } |
| 2922 | |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2923 | /* bond device itself */ |
| 2924 | if (rt->dst.dev == bond->dev) |
| 2925 | goto found; |
| 2926 | |
| 2927 | rcu_read_lock(); |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2928 | tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0); |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2929 | rcu_read_unlock(); |
| 2930 | |
Veaceslav Falico | 3e403a7 | 2014-07-17 17:02:23 +0200 | [diff] [blame] | 2931 | if (!IS_ERR_OR_NULL(tags)) |
Vlad Yasevich | 44a4085 | 2014-05-16 17:20:38 -0400 | [diff] [blame] | 2932 | goto found; |
| 2933 | |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2934 | /* Not our device - skip */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2935 | slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n", |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 2936 | &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL"); |
Veaceslav Falico | 3e32582 | 2013-08-28 23:25:16 +0200 | [diff] [blame] | 2937 | |
Jay Vosburgh | ed4b9f8 | 2005-09-14 14:52:09 -0700 | [diff] [blame] | 2938 | ip_rt_put(rt); |
Veaceslav Falico | 27bc11e | 2013-08-28 23:25:10 +0200 | [diff] [blame] | 2939 | continue; |
| 2940 | |
| 2941 | found: |
| 2942 | addr = bond_confirm_addr(rt->dst.dev, targets[i], 0); |
| 2943 | ip_rt_put(rt); |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2944 | bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags); |
Dan Carpenter | a67eed5 | 2014-07-25 15:21:21 +0300 | [diff] [blame] | 2945 | kfree(tags); |
Jay Vosburgh | c3ade5c | 2005-06-26 17:52:20 -0400 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | |
Al Viro | d3bb52b | 2007-08-22 20:06:58 -0400 | [diff] [blame] | 2949 | static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip) |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2950 | { |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 2951 | int i; |
| 2952 | |
Veaceslav Falico | 87a7b84 | 2013-06-24 11:49:29 +0200 | [diff] [blame] | 2953 | if (!sip || !bond_has_this_ip(bond, tip)) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2954 | slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n", |
| 2955 | __func__, &sip, &tip); |
Veaceslav Falico | 87a7b84 | 2013-06-24 11:49:29 +0200 | [diff] [blame] | 2956 | return; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2957 | } |
Veaceslav Falico | 87a7b84 | 2013-06-24 11:49:29 +0200 | [diff] [blame] | 2958 | |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 2959 | i = bond_get_targets_ip(bond->params.arp_targets, sip); |
| 2960 | if (i == -1) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2961 | slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n", |
| 2962 | __func__, &sip); |
Veaceslav Falico | 87a7b84 | 2013-06-24 11:49:29 +0200 | [diff] [blame] | 2963 | return; |
| 2964 | } |
Veaceslav Falico | 49f17de | 2014-02-18 07:48:47 +0100 | [diff] [blame] | 2965 | slave->last_rx = jiffies; |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 2966 | slave->target_last_arp_rx[i] = jiffies; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
nikolay@redhat.com | 5bb9e0b | 2013-09-07 00:00:26 +0200 | [diff] [blame] | 2969 | int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, |
| 2970 | struct slave *slave) |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2971 | { |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 2972 | struct arphdr *arp = (struct arphdr *)skb->data; |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 2973 | struct slave *curr_active_slave, *curr_arp_slave; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2974 | unsigned char *arp_ptr; |
Al Viro | d3bb52b | 2007-08-22 20:06:58 -0400 | [diff] [blame] | 2975 | __be32 sip, tip; |
Alexey Dobriyan | 6ade97d | 2017-09-26 23:12:28 +0300 | [diff] [blame] | 2976 | int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP); |
| 2977 | unsigned int alen; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2978 | |
Veaceslav Falico | f2cb691 | 2014-02-18 07:48:42 +0100 | [diff] [blame] | 2979 | if (!slave_do_arp_validate(bond, slave)) { |
dingtianhong | bedabf9 | 2014-05-07 22:10:20 +0800 | [diff] [blame] | 2980 | if ((slave_do_arp_validate_only(bond) && is_arp) || |
| 2981 | !slave_do_arp_validate_only(bond)) |
Veaceslav Falico | 49f17de | 2014-02-18 07:48:47 +0100 | [diff] [blame] | 2982 | slave->last_rx = jiffies; |
Jiri Bohac | 13a8e0c | 2012-05-09 01:01:40 +0000 | [diff] [blame] | 2983 | return RX_HANDLER_ANOTHER; |
Veaceslav Falico | f2cb691 | 2014-02-18 07:48:42 +0100 | [diff] [blame] | 2984 | } else if (!is_arp) { |
| 2985 | return RX_HANDLER_ANOTHER; |
| 2986 | } |
Veaceslav Falico | 2c14610 | 2013-06-24 11:49:31 +0200 | [diff] [blame] | 2987 | |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 2988 | alen = arp_hdr_len(bond->dev); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2989 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 2990 | slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n", |
| 2991 | __func__, skb->dev->name); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 2992 | |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 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 | } |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3000 | |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 3001 | if (arp->ar_hln != bond->dev->addr_len || |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 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); |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 3010 | arp_ptr += bond->dev->addr_len; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3011 | memcpy(&sip, arp_ptr, 4); |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 3012 | arp_ptr += 4 + bond->dev->addr_len; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3013 | memcpy(&tip, arp_ptr, 4); |
| 3014 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 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); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3019 | |
Veaceslav Falico | 010d3c3 | 2014-02-20 12:07:57 +0100 | [diff] [blame] | 3020 | curr_active_slave = rcu_dereference(bond->curr_active_slave); |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 3021 | curr_arp_slave = rcu_dereference(bond->current_arp_slave); |
Veaceslav Falico | 010d3c3 | 2014-02-20 12:07:57 +0100 | [diff] [blame] | 3022 | |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 3023 | /* We 'trust' the received ARP enough to validate it if: |
Veaceslav Falico | aeea64a | 2013-06-24 11:49:32 +0200 | [diff] [blame] | 3024 | * |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 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 |
Veaceslav Falico | aeea64a | 2013-06-24 11:49:32 +0200 | [diff] [blame] | 3044 | * arp_ip_target and fool ourselves with our own arp requests. |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3045 | */ |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 3046 | if (bond_is_active_slave(slave)) |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3047 | bond_validate_arp(bond, slave, sip, tip); |
Veaceslav Falico | 010d3c3 | 2014-02-20 12:07:57 +0100 | [diff] [blame] | 3048 | else if (curr_active_slave && |
| 3049 | time_after(slave_last_rx(bond, curr_active_slave), |
| 3050 | curr_active_slave->last_link_up)) |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3051 | bond_validate_arp(bond, slave, tip, sip); |
Jay Vosburgh | 21a75f0 | 2016-02-02 13:35:56 -0800 | [diff] [blame] | 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); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3056 | |
| 3057 | out_unlock: |
Eric Dumazet | de063b7 | 2012-06-11 19:23:07 +0000 | [diff] [blame] | 3058 | if (arp != (struct arphdr *)skb->data) |
| 3059 | kfree(arp); |
Jiri Bohac | 13a8e0c | 2012-05-09 01:01:40 +0000 | [diff] [blame] | 3060 | return RX_HANDLER_ANOTHER; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 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 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3077 | /* This function is called regularly to monitor each slave's link |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Mahesh Bandewar | d5e73f7 | 2017-03-08 10:55:51 -0800 | [diff] [blame] | 3083 | static void bond_loadbalance_arp_mon(struct bonding *bond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | struct slave *slave, *oldcurrent; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3086 | struct list_head *iter; |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3087 | int do_failover = 0, slave_state_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3089 | if (!bond_has_slaves(bond)) |
dingtianhong | 7f1bb57 | 2013-10-24 11:09:17 +0800 | [diff] [blame] | 3090 | goto re_arm; |
dingtianhong | 7f1bb57 | 2013-10-24 11:09:17 +0800 | [diff] [blame] | 3091 | |
dingtianhong | 2e52f4f | 2013-12-13 10:19:50 +0800 | [diff] [blame] | 3092 | rcu_read_lock(); |
| 3093 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 3094 | oldcurrent = rcu_dereference(bond->curr_active_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | * TODO: what about up/down delay in arp mode? it wasn't here before |
| 3101 | * so it can wait |
| 3102 | */ |
dingtianhong | 2e52f4f | 2013-12-13 10:19:50 +0800 | [diff] [blame] | 3103 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jiri Bohac | cb32f2a | 2010-09-02 05:45:54 +0000 | [diff] [blame] | 3104 | unsigned long trans_start = dev_trans_start(slave->dev); |
| 3105 | |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3106 | bond_propose_link_state(slave, BOND_LINK_NOCHANGE); |
Nithin Sujir | 797a936 | 2017-05-24 19:45:17 -0700 | [diff] [blame] | 3107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | if (slave->link != BOND_LINK_UP) { |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3109 | if (bond_time_in_interval(bond, trans_start, 1) && |
Veaceslav Falico | 49f17de | 2014-02-18 07:48:47 +0100 | [diff] [blame] | 3110 | bond_time_in_interval(bond, slave->last_rx, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3112 | bond_propose_link_state(slave, BOND_LINK_UP); |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3113 | slave_state_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3121 | slave_info(bond->dev, slave->dev, "link status definitely up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | do_failover = 1; |
| 3123 | } else { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3124 | slave_info(bond->dev, slave->dev, "interface is now up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 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)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3137 | bond_propose_link_state(slave, BOND_LINK_DOWN); |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3138 | slave_state_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 3140 | if (slave->link_failure_count < UINT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | slave->link_failure_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3143 | slave_info(bond->dev, slave->dev, "interface is now down\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 3145 | if (slave == oldcurrent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | do_failover = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 3157 | if (bond_slave_is_up(slave)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | bond_arp_send_all(bond, slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
dingtianhong | 2e52f4f | 2013-12-13 10:19:50 +0800 | [diff] [blame] | 3161 | rcu_read_unlock(); |
| 3162 | |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3163 | if (do_failover || slave_state_changed) { |
dingtianhong | 2e52f4f | 2013-12-13 10:19:50 +0800 | [diff] [blame] | 3164 | if (!rtnl_trylock()) |
| 3165 | goto re_arm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | |
Nithin Sujir | 797a936 | 2017-05-24 19:45:17 -0700 | [diff] [blame] | 3167 | bond_for_each_slave(bond, slave, iter) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3168 | if (slave->link_new_state != BOND_LINK_NOCHANGE) |
| 3169 | slave->link = slave->link_new_state; |
Nithin Sujir | 797a936 | 2017-05-24 19:45:17 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3172 | if (slave_state_changed) { |
| 3173 | bond_slave_state_change(bond); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 3174 | if (BOND_MODE(bond) == BOND_MODE_XOR) |
| 3175 | bond_update_slave_arr(bond, NULL); |
Nikolay Aleksandrov | b8e4500 | 2014-11-18 15:14:44 +0100 | [diff] [blame] | 3176 | } |
| 3177 | if (do_failover) { |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3178 | block_netpoll_tx(); |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3179 | bond_select_active_slave(bond); |
Ding Tianhong | 6fde8f0 | 2014-01-28 11:48:53 +0800 | [diff] [blame] | 3180 | unblock_netpoll_tx(); |
| 3181 | } |
dingtianhong | 2e52f4f | 2013-12-13 10:19:50 +0800 | [diff] [blame] | 3182 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | re_arm: |
Jay Vosburgh | e6d265e | 2011-10-28 15:42:50 +0000 | [diff] [blame] | 3186 | if (bond->params.arp_interval) |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3187 | queue_delayed_work(bond->wq, &bond->arp_work, |
| 3188 | msecs_to_jiffies(bond->params.arp_interval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3191 | /* Called to inspect slaves for active-backup mode ARP monitor link state |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 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. |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3195 | * |
Nikolay Aleksandrov | 8c0bc55 | 2014-09-11 22:49:28 +0200 | [diff] [blame] | 3196 | * Called with rcu_read_lock held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | */ |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3198 | static int bond_ab_arp_inspect(struct bonding *bond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | { |
Veaceslav Falico | def4460 | 2013-08-03 03:50:35 +0200 | [diff] [blame] | 3200 | unsigned long trans_start, last_rx; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3201 | struct list_head *iter; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 3202 | struct slave *slave; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 3203 | int commit = 0; |
Jiri Bohac | da210f5 | 2012-08-30 12:02:47 +0000 | [diff] [blame] | 3204 | |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3205 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3206 | bond_propose_link_state(slave, BOND_LINK_NOCHANGE); |
Veaceslav Falico | def4460 | 2013-08-03 03:50:35 +0200 | [diff] [blame] | 3207 | last_rx = slave_last_rx(bond, slave); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | if (slave->link != BOND_LINK_UP) { |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3210 | if (bond_time_in_interval(bond, last_rx, 1)) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3211 | bond_propose_link_state(slave, BOND_LINK_UP); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3212 | commit++; |
Jiri Wiesner | 0410d07 | 2020-08-16 20:52:44 +0200 | [diff] [blame] | 3213 | } else if (slave->link == BOND_LINK_BACK) { |
| 3214 | bond_propose_link_state(slave, BOND_LINK_FAIL); |
| 3215 | commit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | } |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3217 | continue; |
| 3218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3220 | /* Give slaves 2*delta after being enslaved or made |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3221 | * active. This avoids bouncing, as the last receive |
| 3222 | * times need a full ARP monitor cycle to be updated. |
| 3223 | */ |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 3224 | if (bond_time_in_interval(bond, slave->last_link_up, 2)) |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3225 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3227 | /* Backup slave is down if: |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3228 | * - No current_arp_slave AND |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 3229 | * - more than (missed_max+1)*delta since last receive AND |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 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 | */ |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 3239 | if (!bond_is_active_slave(slave) && |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3240 | !rcu_access_pointer(bond->current_arp_slave) && |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 3241 | !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3242 | bond_propose_link_state(slave, BOND_LINK_DOWN); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3243 | commit++; |
| 3244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3246 | /* Active slave is down if: |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 3247 | * - more than missed_max*delta since transmitting OR |
| 3248 | * - (more than missed_max*delta since receive AND |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3249 | * the bond has an IP address) |
| 3250 | */ |
Jiri Bohac | cb32f2a | 2010-09-02 05:45:54 +0000 | [diff] [blame] | 3251 | trans_start = dev_trans_start(slave->dev); |
Jiri Pirko | e30bc06 | 2011-03-12 03:14:37 +0000 | [diff] [blame] | 3252 | if (bond_is_active_slave(slave) && |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 3253 | (!bond_time_in_interval(bond, trans_start, bond->params.missed_max) || |
| 3254 | !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3255 | bond_propose_link_state(slave, BOND_LINK_DOWN); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3256 | commit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3260 | return commit; |
| 3261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3263 | /* Called to commit link state changes noted by inspection step of |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3264 | * active-backup mode ARP monitor. |
| 3265 | * |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3266 | * Called with RTNL hold. |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3267 | */ |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3268 | static void bond_ab_arp_commit(struct bonding *bond) |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3269 | { |
Jiri Bohac | cb32f2a | 2010-09-02 05:45:54 +0000 | [diff] [blame] | 3270 | unsigned long trans_start; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3271 | struct list_head *iter; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 3272 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3274 | bond_for_each_slave(bond, slave, iter) { |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3275 | switch (slave->link_new_state) { |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3276 | case BOND_LINK_NOCHANGE: |
| 3277 | continue; |
| 3278 | |
| 3279 | case BOND_LINK_UP: |
Jiri Bohac | cb32f2a | 2010-09-02 05:45:54 +0000 | [diff] [blame] | 3280 | trans_start = dev_trans_start(slave->dev); |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 3281 | if (rtnl_dereference(bond->curr_active_slave) != slave || |
| 3282 | (!rtnl_dereference(bond->curr_active_slave) && |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3283 | bond_time_in_interval(bond, trans_start, 1))) { |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3284 | struct slave *current_arp_slave; |
| 3285 | |
| 3286 | current_arp_slave = rtnl_dereference(bond->current_arp_slave); |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3287 | bond_set_slave_link_state(slave, BOND_LINK_UP, |
| 3288 | BOND_SLAVE_NOTIFY_NOW); |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3289 | if (current_arp_slave) { |
Veaceslav Falico | 5a430974 | 2012-04-05 03:47:43 +0000 | [diff] [blame] | 3290 | bond_set_slave_inactive_flags( |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3291 | current_arp_slave, |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 3292 | BOND_SLAVE_NOTIFY_NOW); |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3293 | RCU_INIT_POINTER(bond->current_arp_slave, NULL); |
Veaceslav Falico | 5a430974 | 2012-04-05 03:47:43 +0000 | [diff] [blame] | 3294 | } |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3295 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3296 | slave_info(bond->dev, slave->dev, "link status definitely up\n"); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3297 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 3298 | if (!rtnl_dereference(bond->curr_active_slave) || |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3299 | slave == rtnl_dereference(bond->primary_slave)) |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3300 | goto do_failover; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3301 | |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3304 | continue; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3305 | |
| 3306 | case BOND_LINK_DOWN: |
| 3307 | if (slave->link_failure_count < UINT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | slave->link_failure_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3310 | bond_set_slave_link_state(slave, BOND_LINK_DOWN, |
| 3311 | BOND_SLAVE_NOTIFY_NOW); |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 3312 | bond_set_slave_inactive_flags(slave, |
| 3313 | BOND_SLAVE_NOTIFY_NOW); |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3314 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3315 | slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n"); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3316 | |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 3317 | if (slave == rtnl_dereference(bond->curr_active_slave)) { |
Eric Dumazet | 8574171 | 2014-07-15 06:56:56 -0700 | [diff] [blame] | 3318 | RCU_INIT_POINTER(bond->current_arp_slave, NULL); |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3319 | goto do_failover; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3320 | } |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3321 | |
| 3322 | continue; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3323 | |
Jiri Wiesner | 0410d07 | 2020-08-16 20:52:44 +0200 | [diff] [blame] | 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 | |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3337 | default: |
Jay Vosburgh | 1899bb3 | 2019-11-01 21:56:42 -0700 | [diff] [blame] | 3338 | slave_err(bond->dev, slave->dev, |
| 3339 | "impossible: link_new_state %d on slave\n", |
| 3340 | slave->link_new_state); |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3341 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3344 | do_failover: |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 3345 | block_netpoll_tx(); |
Jiri Pirko | b9f6025 | 2009-08-31 11:09:38 +0000 | [diff] [blame] | 3346 | bond_select_active_slave(bond); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 3347 | unblock_netpoll_tx(); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | bond_set_carrier(bond); |
| 3351 | } |
| 3352 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3353 | /* Send ARP probes for active-backup mode ARP monitor. |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3354 | * |
Nikolay Aleksandrov | 8c0bc55 | 2014-09-11 22:49:28 +0200 | [diff] [blame] | 3355 | * Called with rcu_read_lock held. |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3356 | */ |
Veaceslav Falico | f2ebd47 | 2014-01-27 14:37:32 +0100 | [diff] [blame] | 3357 | static bool bond_ab_arp_probe(struct bonding *bond) |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3358 | { |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3359 | struct slave *slave, *before = NULL, *new_slave = NULL, |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3360 | *curr_arp_slave = rcu_dereference(bond->current_arp_slave), |
| 3361 | *curr_active_slave = rcu_dereference(bond->curr_active_slave); |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3362 | struct list_head *iter; |
| 3363 | bool found = false; |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3364 | bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER; |
Veaceslav Falico | f2ebd47 | 2014-01-27 14:37:32 +0100 | [diff] [blame] | 3365 | |
Veaceslav Falico | 98b90f2 | 2014-01-27 14:37:31 +0100 | [diff] [blame] | 3366 | if (curr_arp_slave && curr_active_slave) |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 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); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3370 | |
Veaceslav Falico | 98b90f2 | 2014-01-27 14:37:31 +0100 | [diff] [blame] | 3371 | if (curr_active_slave) { |
| 3372 | bond_arp_send_all(bond, curr_active_slave); |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3373 | return should_notify_rtnl; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3380 | |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3381 | if (!curr_arp_slave) { |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3382 | curr_arp_slave = bond_first_slave_rcu(bond); |
| 3383 | if (!curr_arp_slave) |
| 3384 | return should_notify_rtnl; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3387 | bond_for_each_slave_rcu(bond, slave, iter) { |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 3388 | if (!found && !before && bond_slave_is_up(slave)) |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3389 | before = slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 3391 | if (found && !new_slave && bond_slave_is_up(slave)) |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3392 | new_slave = slave; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 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 | */ |
Veaceslav Falico | b6adc61 | 2014-05-15 21:39:57 +0200 | [diff] [blame] | 3400 | if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3401 | bond_set_slave_link_state(slave, BOND_LINK_DOWN, |
| 3402 | BOND_SLAVE_NOTIFY_LATER); |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3403 | if (slave->link_failure_count < UINT_MAX) |
| 3404 | slave->link_failure_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 3406 | bond_set_slave_inactive_flags(slave, |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3407 | BOND_SLAVE_NOTIFY_LATER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3409 | slave_info(bond->dev, slave->dev, "backup interface is now down\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | } |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3411 | if (slave == curr_arp_slave) |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3412 | found = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | } |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3414 | |
| 3415 | if (!new_slave && before) |
| 3416 | new_slave = before; |
| 3417 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3418 | if (!new_slave) |
| 3419 | goto check_state; |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3420 | |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3421 | bond_set_slave_link_state(new_slave, BOND_LINK_BACK, |
| 3422 | BOND_SLAVE_NOTIFY_LATER); |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3423 | bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER); |
Veaceslav Falico | 4087df87 | 2013-09-25 09:20:19 +0200 | [diff] [blame] | 3424 | bond_arp_send_all(bond, new_slave); |
Veaceslav Falico | 8e60346 | 2014-02-18 07:48:46 +0100 | [diff] [blame] | 3425 | new_slave->last_link_up = jiffies; |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3426 | rcu_assign_pointer(bond->current_arp_slave, new_slave); |
Veaceslav Falico | f2ebd47 | 2014-01-27 14:37:32 +0100 | [diff] [blame] | 3427 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3428 | check_state: |
| 3429 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3430 | if (slave->should_notify || slave->should_notify_link) { |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3431 | should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW; |
| 3432 | break; |
| 3433 | } |
| 3434 | } |
| 3435 | return should_notify_rtnl; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
Mahesh Bandewar | d5e73f7 | 2017-03-08 10:55:51 -0800 | [diff] [blame] | 3438 | static void bond_activebackup_arp_mon(struct bonding *bond) |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3439 | { |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3440 | bool should_notify_peers = false; |
| 3441 | bool should_notify_rtnl = false; |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3442 | int delta_in_ticks; |
Jay Vosburgh | b2220ca | 2008-05-17 21:10:13 -0700 | [diff] [blame] | 3443 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3444 | delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); |
| 3445 | |
| 3446 | if (!bond_has_slaves(bond)) |
dingtianhong | 80b9d23 | 2013-10-24 11:09:25 +0800 | [diff] [blame] | 3447 | goto re_arm; |
dingtianhong | 80b9d23 | 2013-10-24 11:09:25 +0800 | [diff] [blame] | 3448 | |
dingtianhong | eb9fa4b | 2013-12-13 10:20:02 +0800 | [diff] [blame] | 3449 | rcu_read_lock(); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 3450 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3451 | should_notify_peers = bond_should_notify_peers(bond); |
| 3452 | |
| 3453 | if (bond_ab_arp_inspect(bond)) { |
| 3454 | rcu_read_unlock(); |
| 3455 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3456 | /* Race avoidance with bond_close flush of workqueue */ |
| 3457 | if (!rtnl_trylock()) { |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3458 | delta_in_ticks = 1; |
| 3459 | should_notify_peers = false; |
| 3460 | goto re_arm; |
| 3461 | } |
| 3462 | |
Veaceslav Falico | e7f63f1 | 2013-08-03 03:50:36 +0200 | [diff] [blame] | 3463 | bond_ab_arp_commit(bond); |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3464 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3465 | rtnl_unlock(); |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3466 | rcu_read_lock(); |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3467 | } |
| 3468 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3469 | should_notify_rtnl = bond_ab_arp_probe(bond); |
| 3470 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | |
| 3472 | re_arm: |
Jay Vosburgh | e6d265e | 2011-10-28 15:42:50 +0000 | [diff] [blame] | 3473 | if (bond->params.arp_interval) |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3474 | queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks); |
| 3475 | |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3476 | if (should_notify_peers || should_notify_rtnl) { |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3477 | if (!rtnl_trylock()) |
| 3478 | return; |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3479 | |
| 3480 | if (should_notify_peers) |
| 3481 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, |
| 3482 | bond->dev); |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3483 | if (should_notify_rtnl) { |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3484 | bond_slave_state_notify(bond); |
Jiri Pirko | 5d39706 | 2015-12-03 12:12:19 +0100 | [diff] [blame] | 3485 | bond_slave_link_notify(bond); |
| 3486 | } |
dingtianhong | b092991 | 2014-02-26 11:05:23 +0800 | [diff] [blame] | 3487 | |
David S. Miller | 1f2cd84 | 2013-10-28 00:11:22 -0400 | [diff] [blame] | 3488 | rtnl_unlock(); |
| 3489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Mahesh Bandewar | d5e73f7 | 2017-03-08 10:55:51 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | /*-------------------------- netdev event handling --------------------------*/ |
| 3504 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3505 | /* Change device name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | static int bond_event_changename(struct bonding *bond) |
| 3507 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | bond_remove_proc_entry(bond); |
| 3509 | bond_create_proc_entry(bond); |
Stephen Hemminger | 7e08384 | 2009-06-12 19:02:46 +0000 | [diff] [blame] | 3510 | |
Taku Izumi | f073c7c | 2010-12-09 15:17:13 +0000 | [diff] [blame] | 3511 | bond_debug_reregister(bond); |
| 3512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | return NOTIFY_DONE; |
| 3514 | } |
| 3515 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 3516 | static int bond_master_netdev_event(unsigned long event, |
| 3517 | struct net_device *bond_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 3519 | struct bonding *event_bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3521 | netdev_dbg(bond_dev, "%s called\n", __func__); |
| 3522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | switch (event) { |
| 3524 | case NETDEV_CHANGENAME: |
| 3525 | return bond_event_changename(event_bond); |
Eric W. Biederman | a64d49c | 2012-07-09 10:51:45 +0000 | [diff] [blame] | 3526 | case NETDEV_UNREGISTER: |
| 3527 | bond_remove_proc_entry(event_bond); |
Mahesh Bandewar | 5b69874 | 2021-07-16 16:09:41 -0700 | [diff] [blame] | 3528 | #ifdef CONFIG_XFRM_OFFLOAD |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 3529 | xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true); |
Mahesh Bandewar | 5b69874 | 2021-07-16 16:09:41 -0700 | [diff] [blame] | 3530 | #endif /* CONFIG_XFRM_OFFLOAD */ |
Eric W. Biederman | a64d49c | 2012-07-09 10:51:45 +0000 | [diff] [blame] | 3531 | break; |
| 3532 | case NETDEV_REGISTER: |
| 3533 | bond_create_proc_entry(event_bond); |
| 3534 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | default: |
| 3536 | break; |
| 3537 | } |
| 3538 | |
| 3539 | return NOTIFY_DONE; |
| 3540 | } |
| 3541 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 3542 | static int bond_slave_netdev_event(unsigned long event, |
| 3543 | struct net_device *slave_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | { |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3545 | struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary; |
nikolay@redhat.com | 6101391 | 2013-04-11 09:18:55 +0000 | [diff] [blame] | 3546 | struct bonding *bond; |
| 3547 | struct net_device *bond_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
nikolay@redhat.com | 6101391 | 2013-04-11 09:18:55 +0000 | [diff] [blame] | 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 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3553 | if (!slave) { |
| 3554 | netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__); |
nikolay@redhat.com | 6101391 | 2013-04-11 09:18:55 +0000 | [diff] [blame] | 3555 | return NOTIFY_DONE; |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3556 | } |
| 3557 | |
nikolay@redhat.com | 6101391 | 2013-04-11 09:18:55 +0000 | [diff] [blame] | 3558 | bond_dev = slave->bond->dev; |
| 3559 | bond = slave->bond; |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3560 | primary = rtnl_dereference(bond->primary_slave); |
nikolay@redhat.com | 6101391 | 2013-04-11 09:18:55 +0000 | [diff] [blame] | 3561 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3562 | slave_dbg(bond_dev, slave_dev, "%s called\n", __func__); |
| 3563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | switch (event) { |
| 3565 | case NETDEV_UNREGISTER: |
nikolay@redhat.com | 8d2ada7 | 2013-06-26 17:13:37 +0200 | [diff] [blame] | 3566 | if (bond_dev->type != ARPHRD_ETHER) |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 3567 | bond_release_and_destroy(bond_dev, slave_dev); |
| 3568 | else |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 3569 | __bond_release_one(bond_dev, slave_dev, false, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | break; |
Weiping Pan | 98f41f6 | 2011-10-31 17:20:48 +0000 | [diff] [blame] | 3571 | case NETDEV_UP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | case NETDEV_CHANGE: |
Mahesh Bandewar | 4d2c0cd | 2017-09-27 18:03:49 -0700 | [diff] [blame] | 3573 | /* For 802.3ad mode only: |
| 3574 | * Getting invalid Speed/Duplex values here will put slave |
Jarod Wilson | 3340312 | 2019-05-24 09:49:28 -0400 | [diff] [blame] | 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. |
Mahesh Bandewar | 4d2c0cd | 2017-09-27 18:03:49 -0700 | [diff] [blame] | 3579 | */ |
| 3580 | if (bond_update_speed_duplex(slave) && |
Jarod Wilson | 3340312 | 2019-05-24 09:49:28 -0400 | [diff] [blame] | 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 | } |
Mahesh Bandewar | 4d2c0cd | 2017-09-27 18:03:49 -0700 | [diff] [blame] | 3587 | |
Mahesh Bandewar | 52bc671 | 2015-10-31 12:45:11 -0700 | [diff] [blame] | 3588 | if (BOND_MODE(bond) == BOND_MODE_8023AD) |
| 3589 | bond_3ad_adapter_speed_duplex_changed(slave); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3590 | fallthrough; |
Mahesh Bandewar | 950ddcb | 2015-02-19 10:13:25 -0800 | [diff] [blame] | 3591 | case NETDEV_DOWN: |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 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 | */ |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 3600 | if (bond_mode_can_use_xmit_hash(bond)) |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 3601 | bond_update_slave_arr(bond, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | case NETDEV_CHANGEMTU: |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3604 | /* TODO: Should slaves be allowed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3617 | /* we don't care if we don't have primary set */ |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 3618 | if (!bond_uses_primary(bond) || |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3619 | !bond->params.primary[0]) |
| 3620 | break; |
| 3621 | |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3622 | if (slave == primary) { |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3623 | /* slave's name changed - he's no longer primary */ |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3624 | RCU_INIT_POINTER(bond->primary_slave, NULL); |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3625 | } else if (!strcmp(slave_dev->name, bond->params.primary)) { |
| 3626 | /* we have a new primary slave */ |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3627 | rcu_assign_pointer(bond->primary_slave, slave); |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3628 | } else { /* we didn't change primary - exit */ |
| 3629 | break; |
| 3630 | } |
| 3631 | |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 3632 | netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n", |
Nikolay Aleksandrov | 059b47e | 2014-09-09 23:17:00 +0200 | [diff] [blame] | 3633 | primary ? slave_dev->name : "none"); |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 3634 | |
| 3635 | block_netpoll_tx(); |
Veaceslav Falico | 3ec775b | 2014-01-16 02:04:29 +0100 | [diff] [blame] | 3636 | bond_select_active_slave(bond); |
dingtianhong | f80889a | 2014-02-12 12:06:40 +0800 | [diff] [blame] | 3637 | unblock_netpoll_tx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | break; |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 3639 | case NETDEV_FEAT_CHANGE: |
| 3640 | bond_compute_features(bond); |
| 3641 | break; |
Jiri Pirko | 4aa5dee | 2013-07-20 12:13:53 +0200 | [diff] [blame] | 3642 | case NETDEV_RESEND_IGMP: |
| 3643 | /* Propagate to master device */ |
| 3644 | call_netdevice_notifiers(event, slave->bond->dev); |
| 3645 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | default: |
| 3647 | break; |
| 3648 | } |
| 3649 | |
| 3650 | return NOTIFY_DONE; |
| 3651 | } |
| 3652 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 3653 | /* bond_netdev_event: handle netdev notifier chain events. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | * |
| 3655 | * This function receives events for the netdev chain. The caller (an |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 3656 | * ioctl handler calling blocking_notifier_call_chain) holds the necessary |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | * locks for us to safely manipulate the slave devices (RTNL lock, |
| 3658 | * dev_probe_lock). |
| 3659 | */ |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 3660 | static int bond_netdev_event(struct notifier_block *this, |
| 3661 | unsigned long event, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3663 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | |
Jarod Wilson | 75466dc | 2019-06-07 10:59:26 -0400 | [diff] [blame] | 3665 | netdev_dbg(event_dev, "%s received %s\n", |
| 3666 | __func__, netdev_cmd_to_name(event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | |
Jay Vosburgh | 0b680e7 | 2006-09-22 21:54:10 -0700 | [diff] [blame] | 3668 | if (!(event_dev->priv_flags & IFF_BONDING)) |
| 3669 | return NOTIFY_DONE; |
| 3670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | if (event_dev->flags & IFF_MASTER) { |
Sabrina Dubroca | 92480b3 | 2019-04-12 15:04:10 +0200 | [diff] [blame] | 3672 | int ret; |
| 3673 | |
Sabrina Dubroca | 92480b3 | 2019-04-12 15:04:10 +0200 | [diff] [blame] | 3674 | ret = bond_master_netdev_event(event, event_dev); |
| 3675 | if (ret != NOTIFY_DONE) |
| 3676 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 3679 | if (event_dev->flags & IFF_SLAVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | return bond_slave_netdev_event(event, event_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | |
| 3682 | return NOTIFY_DONE; |
| 3683 | } |
| 3684 | |
| 3685 | static struct notifier_block bond_netdev_notifier = { |
| 3686 | .notifier_call = bond_netdev_event, |
| 3687 | }; |
| 3688 | |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 3689 | /*---------------------------- Hashing Policies -----------------------------*/ |
| 3690 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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) |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 3696 | { |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3697 | if (likely(n <= hlen)) |
| 3698 | return data; |
| 3699 | else if (skb && likely(pskb_may_pull(skb, n))) |
| 3700 | return skb->head; |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 3701 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3702 | return NULL; |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 3715 | return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto); |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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) |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3720 | { |
| 3721 | const struct ipv6hdr *iph6; |
| 3722 | const struct iphdr *iph; |
| 3723 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3724 | if (l2_proto == htons(ETH_P_IP)) { |
| 3725 | data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph)); |
| 3726 | if (!data) |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3727 | return false; |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3728 | |
| 3729 | iph = (const struct iphdr *)(data + *nhoff); |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3730 | iph_to_flow_copy_v4addrs(fk, iph); |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3731 | *nhoff += iph->ihl << 2; |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3732 | if (!ip_is_fragment(iph)) |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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) |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3737 | return false; |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3738 | |
| 3739 | iph6 = (const struct ipv6hdr *)(data + *nhoff); |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3740 | iph_to_flow_copy_v6addrs(fk, iph6); |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3741 | *nhoff += sizeof(*iph6); |
| 3742 | *ip_proto = iph6->nexthdr; |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3743 | } else { |
| 3744 | return false; |
| 3745 | } |
| 3746 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3747 | if (l34 && *ip_proto >= 0) |
| 3748 | fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen); |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3749 | |
| 3750 | return true; |
| 3751 | } |
| 3752 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3753 | static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen) |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3754 | { |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3755 | u32 srcmac_vendor = 0, srcmac_dev = 0; |
Jussi Maki | 39a0876 | 2021-08-12 14:52:41 +0000 | [diff] [blame] | 3756 | struct ethhdr *mac_hdr; |
| 3757 | u16 vlan = 0; |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3758 | int i; |
| 3759 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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 | |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 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 | |
Jussi Maki | 39a0876 | 2021-08-12 14:52:41 +0000 | [diff] [blame] | 3771 | if (skb && skb_vlan_tag_present(skb)) |
| 3772 | vlan = skb_vlan_tag_get(skb); |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3773 | |
| 3774 | return vlan ^ srcmac_vendor ^ srcmac_dev; |
| 3775 | } |
| 3776 | |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 3777 | /* Extract the appropriate headers based on bond's xmit policy */ |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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) |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3780 | { |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3781 | bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34; |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3782 | int ip_proto = -1; |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3783 | |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3784 | switch (bond->params.xmit_policy) { |
| 3785 | case BOND_XMIT_POLICY_ENCAP23: |
| 3786 | case BOND_XMIT_POLICY_ENCAP34: |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 3787 | memset(fk, 0, sizeof(*fk)); |
| 3788 | return __skb_flow_dissect(NULL, skb, &flow_keys_bonding, |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3789 | fk, data, l2_proto, nhoff, hlen, 0); |
Jarod Wilson | 7b8fc01 | 2021-01-18 20:09:27 -0500 | [diff] [blame] | 3790 | default: |
| 3791 | break; |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 3792 | } |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 3793 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 3794 | fk->ports.ports = 0; |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 3795 | memset(&fk->icmp, 0, sizeof(fk->icmp)); |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3796 | if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34)) |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 3797 | return false; |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 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 | */ |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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) { |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3807 | if (!icmp_is_err(fk->icmp.type)) |
| 3808 | return true; |
| 3809 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3810 | nhoff += sizeof(struct icmphdr); |
| 3811 | } else if (ip_proto == IPPROTO_ICMPV6) { |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3812 | if (!icmpv6_is_err(fk->icmp.type)) |
| 3813 | return true; |
| 3814 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3815 | nhoff += sizeof(struct icmp6hdr); |
Matteo Croce | df98be0 | 2019-11-15 12:10:37 +0100 | [diff] [blame] | 3816 | } |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3817 | return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34); |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 3818 | } |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3819 | |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 3820 | return true; |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Tariq Toukan | 5b99854 | 2021-01-17 16:59:43 +0200 | [diff] [blame] | 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 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 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 | |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 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 |
Nikolay Aleksandrov | 32819dc | 2013-10-02 13:39:25 +0200 | [diff] [blame] | 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 |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3870 | */ |
Mahesh Bandewar | ee62e86 | 2014-04-22 16:30:15 -0700 | [diff] [blame] | 3871 | u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb) |
John Eaglesham | 6b923cb | 2012-08-21 20:43:35 +0000 | [diff] [blame] | 3872 | { |
Eric Dumazet | 4b1b865 | 2015-09-15 15:24:28 -0700 | [diff] [blame] | 3873 | if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 && |
| 3874 | skb->l4_hash) |
| 3875 | return skb->hash; |
| 3876 | |
Moshe Tal | 429e3d1 | 2022-01-16 19:39:29 +0200 | [diff] [blame] | 3877 | return __bond_xmit_hash(bond, skb, skb->data, skb->protocol, |
| 3878 | skb_mac_offset(skb), skb_network_offset(skb), |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 3879 | skb_headlen(skb)); |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 3880 | } |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 3881 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 3893 | if (xdp->data + sizeof(struct ethhdr) > xdp->data_end) |
| 3894 | return 0; |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 3895 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 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); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 3922 | cancel_delayed_work_sync(&bond->slave_arr_work); |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | static int bond_open(struct net_device *bond_dev) |
| 3926 | { |
| 3927 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3928 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | |
| 3931 | /* reset slave->backup and slave->inactive */ |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 3932 | if (bond_has_slaves(bond)) { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 3933 | bond_for_each_slave(bond, slave, iter) { |
Eric Dumazet | 4740d63 | 2014-07-15 06:56:55 -0700 | [diff] [blame] | 3934 | if (bond_uses_primary(bond) && |
| 3935 | slave != rcu_access_pointer(bond->curr_active_slave)) { |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 3936 | bond_set_slave_inactive_flags(slave, |
| 3937 | BOND_SLAVE_NOTIFY_NOW); |
Wilson Kok | 8bbe71a | 2015-01-26 01:16:58 -0500 | [diff] [blame] | 3938 | } else if (BOND_MODE(bond) != BOND_MODE_8023AD) { |
dingtianhong | 5e5b066 | 2014-02-26 11:05:22 +0800 | [diff] [blame] | 3939 | bond_set_slave_active_flags(slave, |
| 3940 | BOND_SLAVE_NOTIFY_NOW); |
Jay Vosburgh | 1b76b31 | 2007-10-17 17:37:45 -0700 | [diff] [blame] | 3941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | } |
Jay Vosburgh | 1b76b31 | 2007-10-17 17:37:45 -0700 | [diff] [blame] | 3944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | if (bond_is_lb(bond)) { |
| 3946 | /* bond_alb_initialize must be called before the timer |
| 3947 | * is started. |
| 3948 | */ |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 3949 | if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | return -ENOMEM; |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 3951 | if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB) |
Mahesh Bandewar | e9f0fb8 | 2014-04-22 16:30:22 -0700 | [diff] [blame] | 3952 | queue_delayed_work(bond->wq, &bond->alb_work, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 3955 | if (bond->params.miimon) /* link check interval, in milliseconds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | queue_delayed_work(bond->wq, &bond->mii_work, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | |
| 3958 | if (bond->params.arp_interval) { /* arp interval, in milliseconds. */ |
Jay Vosburgh | 1b76b31 | 2007-10-17 17:37:45 -0700 | [diff] [blame] | 3959 | queue_delayed_work(bond->wq, &bond->arp_work, 0); |
Veaceslav Falico | 3fe68df | 2014-02-18 07:48:39 +0100 | [diff] [blame] | 3960 | bond->recv_probe = bond_arp_rcv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 3963 | if (BOND_MODE(bond) == BOND_MODE_8023AD) { |
Jay Vosburgh | 1b76b31 | 2007-10-17 17:37:45 -0700 | [diff] [blame] | 3964 | queue_delayed_work(bond->wq, &bond->ad_work, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | /* register to receive LACPDUs */ |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 3966 | bond->recv_probe = bond_3ad_lacpdu_recv; |
Jay Vosburgh | fd989c8 | 2008-11-04 17:51:16 -0800 | [diff] [blame] | 3967 | bond_3ad_initiate_agg_selection(bond, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | } |
| 3969 | |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 3970 | if (bond_mode_can_use_xmit_hash(bond)) |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 3971 | bond_update_slave_arr(bond, NULL); |
| 3972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | return 0; |
| 3974 | } |
| 3975 | |
| 3976 | static int bond_close(struct net_device *bond_dev) |
| 3977 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 3978 | struct bonding *bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | |
nikolay@redhat.com | fbb0c41 | 2012-11-29 01:31:31 +0000 | [diff] [blame] | 3980 | bond_work_cancel_all(bond); |
nikolay@redhat.com | 6c8d23f | 2013-09-02 13:51:38 +0200 | [diff] [blame] | 3981 | bond->send_peer_notif = 0; |
nikolay@redhat.com | ee8487c | 2013-09-02 13:51:39 +0200 | [diff] [blame] | 3982 | if (bond_is_lb(bond)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | bond_alb_deinitialize(bond); |
Jiri Pirko | 3aba891 | 2011-04-19 03:48:16 +0000 | [diff] [blame] | 3984 | bond->recv_probe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | |
| 3986 | return 0; |
| 3987 | } |
| 3988 | |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 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]; |
Eric Dumazet | 142c659 | 2017-03-29 10:45:44 -0700 | [diff] [blame] | 4004 | s64 delta = nv - ov; |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4005 | |
| 4006 | /* detects if this particular field is 32bit only */ |
| 4007 | if (((nv | ov) >> 32) == 0) |
Eric Dumazet | 142c659 | 2017-03-29 10:45:44 -0700 | [diff] [blame] | 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; |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | |
Taehee Yoo | b3e80d4 | 2020-02-15 10:50:40 +0000 | [diff] [blame] | 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 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 4059 | static void bond_get_stats(struct net_device *bond_dev, |
| 4060 | struct rtnl_link_stats64 *stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4062 | struct bonding *bond = netdev_priv(bond_dev); |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 4063 | struct rtnl_link_stats64 temp; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4064 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | struct slave *slave; |
Taehee Yoo | b3e80d4 | 2020-02-15 10:50:40 +0000 | [diff] [blame] | 4066 | int nest_level = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4069 | rcu_read_lock(); |
Taehee Yoo | b3e80d4 | 2020-02-15 10:50:40 +0000 | [diff] [blame] | 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 | |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4077 | bond_for_each_slave_rcu(bond, slave, iter) { |
| 4078 | const struct rtnl_link_stats64 *new = |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 4079 | dev_get_stats(slave->dev, &temp); |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 4080 | |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4081 | bond_fold_stats(stats, new, &slave->slave_stats); |
Andy Gospodarek | 5f0c5f7 | 2014-09-28 22:34:37 -0400 | [diff] [blame] | 4082 | |
| 4083 | /* save off the slave stats for the next run */ |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4084 | memcpy(&slave->slave_stats, new, sizeof(*new)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | } |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4086 | |
Andy Gospodarek | 5f0c5f7 | 2014-09-28 22:34:37 -0400 | [diff] [blame] | 4087 | memcpy(&bond->bond_stats, stats, sizeof(*stats)); |
Eric Dumazet | fe30937 | 2016-03-17 17:23:36 -0700 | [diff] [blame] | 4088 | spin_unlock(&bond->stats_lock); |
Taehee Yoo | b3e80d4 | 2020-02-15 10:50:40 +0000 | [diff] [blame] | 4089 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 4092 | static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
Jiri Pirko | 080a06e | 2013-10-18 17:43:36 +0200 | [diff] [blame] | 4094 | struct bonding *bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | struct mii_ioctl_data *mii = NULL; |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4096 | const struct net_device_ops *ops; |
| 4097 | struct net_device *real_dev; |
Hangbin Liu | 085d6100 | 2021-12-10 16:59:59 +0800 | [diff] [blame] | 4098 | struct hwtstamp_config cfg; |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4099 | struct ifreq ifrr; |
| 4100 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 4102 | netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | |
| 4104 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | case SIOCGMIIPHY: |
| 4106 | mii = if_mii(ifr); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4107 | if (!mii) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | return -EINVAL; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | mii->phy_id = 0; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4111 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | case SIOCGMIIREG: |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4113 | /* We do this again just in case we were called by SIOCGMIIREG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | * instead of SIOCGMIIPHY. |
| 4115 | */ |
| 4116 | mii = if_mii(ifr); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4117 | if (!mii) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | return -EINVAL; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | if (mii->reg_num == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | mii->val_out = 0; |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4122 | if (netif_carrier_ok(bond->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | mii->val_out = BMSR_LSTATUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4126 | break; |
| 4127 | case SIOCSHWTSTAMP: |
Hangbin Liu | 085d6100 | 2021-12-10 16:59:59 +0800 | [diff] [blame] | 4128 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 4129 | return -EFAULT; |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4130 | |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4131 | if (!(cfg.flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX)) |
| 4132 | return -EOPNOTSUPP; |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4133 | |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4134 | fallthrough; |
| 4135 | case SIOCGHWTSTAMP: |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4136 | real_dev = bond_option_active_slave_get_rcu(bond); |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4137 | if (!real_dev) |
| 4138 | return -EOPNOTSUPP; |
Hangbin Liu | 085d6100 | 2021-12-10 16:59:59 +0800 | [diff] [blame] | 4139 | |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4140 | strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ); |
| 4141 | ifrr.ifr_ifru = ifr->ifr_ifru; |
Hangbin Liu | 085d6100 | 2021-12-10 16:59:59 +0800 | [diff] [blame] | 4142 | |
Hangbin Liu | cfe355c | 2021-12-29 16:09:38 +0800 | [diff] [blame] | 4143 | ops = real_dev->netdev_ops; |
| 4144 | if (netif_device_present(real_dev) && ops->ndo_eth_ioctl) { |
| 4145 | res = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd); |
| 4146 | if (res) |
| 4147 | return res; |
| 4148 | |
| 4149 | ifr->ifr_ifru = ifrr.ifr_ifru; |
| 4150 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 4151 | return -EFAULT; |
| 4152 | |
| 4153 | /* Set the BOND_PHC_INDEX flag to notify user space */ |
| 4154 | cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX; |
| 4155 | |
| 4156 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? |
| 4157 | -EFAULT : 0; |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 4158 | } |
Hangbin Liu | 085d6100 | 2021-12-10 16:59:59 +0800 | [diff] [blame] | 4159 | fallthrough; |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 4160 | default: |
| 4161 | res = -EOPNOTSUPP; |
| 4162 | } |
| 4163 | |
| 4164 | return res; |
| 4165 | } |
| 4166 | |
| 4167 | static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) |
| 4168 | { |
| 4169 | struct bonding *bond = netdev_priv(bond_dev); |
| 4170 | struct net_device *slave_dev = NULL; |
| 4171 | struct ifbond k_binfo; |
| 4172 | struct ifbond __user *u_binfo = NULL; |
| 4173 | struct ifslave k_sinfo; |
| 4174 | struct ifslave __user *u_sinfo = NULL; |
| 4175 | struct bond_opt_value newval; |
| 4176 | struct net *net; |
| 4177 | int res = 0; |
| 4178 | |
| 4179 | netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd); |
| 4180 | |
| 4181 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | case SIOCBONDINFOQUERY: |
| 4183 | u_binfo = (struct ifbond __user *)ifr->ifr_data; |
| 4184 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4185 | if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | |
Zhu Yanjun | 3d67576 | 2017-02-02 23:46:21 -0500 | [diff] [blame] | 4188 | bond_info_query(bond_dev, &k_binfo); |
| 4189 | if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4190 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
Zhu Yanjun | 3d67576 | 2017-02-02 23:46:21 -0500 | [diff] [blame] | 4192 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | case SIOCBONDSLAVEINFOQUERY: |
| 4194 | u_sinfo = (struct ifslave __user *)ifr->ifr_data; |
| 4195 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4196 | if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
| 4199 | res = bond_slave_info_query(bond_dev, &k_sinfo); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4200 | if (res == 0 && |
| 4201 | copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) |
| 4202 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | |
| 4204 | return res; |
| 4205 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | break; |
| 4207 | } |
| 4208 | |
Gao feng | 387ff911 | 2013-01-31 16:31:00 +0000 | [diff] [blame] | 4209 | net = dev_net(bond_dev); |
| 4210 | |
| 4211 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4214 | slave_dev = __dev_get_by_name(net, ifr->ifr_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4216 | slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4218 | if (!slave_dev) |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4219 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4221 | switch (cmd) { |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4222 | case SIOCBONDENSLAVE: |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 4223 | res = bond_enslave(bond_dev, slave_dev, NULL); |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4224 | break; |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4225 | case SIOCBONDRELEASE: |
| 4226 | res = bond_release(bond_dev, slave_dev); |
| 4227 | break; |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4228 | case SIOCBONDSETHWADDR: |
Petr Machata | b924591 | 2018-12-13 11:54:44 +0000 | [diff] [blame] | 4229 | res = bond_set_dev_addr(bond_dev, slave_dev); |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4230 | break; |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4231 | case SIOCBONDCHANGEACTIVE: |
Nikolay Aleksandrov | d1fbd3ed | 2014-01-22 14:53:35 +0100 | [diff] [blame] | 4232 | bond_opt_initstr(&newval, slave_dev->name); |
Vlad Yasevich | 7a7e96e | 2017-05-27 10:14:35 -0400 | [diff] [blame] | 4233 | res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE, |
| 4234 | &newval); |
Ying Xue | 0917b93 | 2014-01-15 10:23:37 +0800 | [diff] [blame] | 4235 | break; |
| 4236 | default: |
| 4237 | res = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | return res; |
| 4241 | } |
| 4242 | |
Arnd Bergmann | 232ec98 | 2021-07-27 15:44:54 +0200 | [diff] [blame] | 4243 | static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr, |
| 4244 | void __user *data, int cmd) |
| 4245 | { |
| 4246 | struct ifreq ifrdata = { .ifr_data = data }; |
| 4247 | |
| 4248 | switch (cmd) { |
| 4249 | case BOND_INFO_QUERY_OLD: |
| 4250 | return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY); |
| 4251 | case BOND_SLAVE_INFO_QUERY_OLD: |
| 4252 | return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY); |
| 4253 | case BOND_ENSLAVE_OLD: |
| 4254 | return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE); |
| 4255 | case BOND_RELEASE_OLD: |
| 4256 | return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE); |
| 4257 | case BOND_SETHWADDR_OLD: |
| 4258 | return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR); |
| 4259 | case BOND_CHANGE_ACTIVE_OLD: |
| 4260 | return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE); |
| 4261 | } |
| 4262 | |
| 4263 | return -EOPNOTSUPP; |
| 4264 | } |
| 4265 | |
Jiri Pirko | d03462b | 2011-08-16 03:15:04 +0000 | [diff] [blame] | 4266 | static void bond_change_rx_flags(struct net_device *bond_dev, int change) |
| 4267 | { |
| 4268 | struct bonding *bond = netdev_priv(bond_dev); |
| 4269 | |
| 4270 | if (change & IFF_PROMISC) |
| 4271 | bond_set_promiscuity(bond, |
| 4272 | bond_dev->flags & IFF_PROMISC ? 1 : -1); |
| 4273 | |
| 4274 | if (change & IFF_ALLMULTI) |
| 4275 | bond_set_allmulti(bond, |
| 4276 | bond_dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 4277 | } |
| 4278 | |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 4279 | static void bond_set_rx_mode(struct net_device *bond_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4281 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4282 | struct list_head *iter; |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 4283 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | |
Veaceslav Falico | b324187 | 2013-09-28 21:18:56 +0200 | [diff] [blame] | 4285 | rcu_read_lock(); |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 4286 | if (bond_uses_primary(bond)) { |
Veaceslav Falico | b324187 | 2013-09-28 21:18:56 +0200 | [diff] [blame] | 4287 | slave = rcu_dereference(bond->curr_active_slave); |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 4288 | if (slave) { |
| 4289 | dev_uc_sync(slave->dev, bond_dev); |
| 4290 | dev_mc_sync(slave->dev, bond_dev); |
| 4291 | } |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 4292 | } else { |
Veaceslav Falico | b324187 | 2013-09-28 21:18:56 +0200 | [diff] [blame] | 4293 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 4294 | dev_uc_sync_multiple(slave->dev, bond_dev); |
| 4295 | dev_mc_sync_multiple(slave->dev, bond_dev); |
| 4296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | } |
Veaceslav Falico | b324187 | 2013-09-28 21:18:56 +0200 | [diff] [blame] | 4298 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | } |
| 4300 | |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4301 | static int bond_neigh_init(struct neighbour *n) |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 4302 | { |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4303 | struct bonding *bond = netdev_priv(n->dev); |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4304 | const struct net_device_ops *slave_ops; |
| 4305 | struct neigh_parms parms; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 4306 | struct slave *slave; |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4307 | int ret = 0; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 4308 | |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4309 | rcu_read_lock(); |
| 4310 | slave = bond_first_slave_rcu(bond); |
David S. Miller | 080bfa1 | 2021-03-12 12:15:03 -0800 | [diff] [blame] | 4311 | if (!slave) |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4312 | goto out; |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4313 | slave_ops = slave->dev->netdev_ops; |
David S. Miller | 080bfa1 | 2021-03-12 12:15:03 -0800 | [diff] [blame] | 4314 | if (!slave_ops->ndo_neigh_setup) |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4315 | goto out; |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4316 | |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4317 | /* TODO: find another way [1] to implement this. |
| 4318 | * Passing a zeroed structure is fragile, |
| 4319 | * but at least we do not pass garbage. |
| 4320 | * |
| 4321 | * [1] One way would be that ndo_neigh_setup() never touch |
| 4322 | * struct neigh_parms, but propagate the new neigh_setup() |
| 4323 | * back to ___neigh_create() / neigh_parms_alloc() |
| 4324 | */ |
| 4325 | memset(&parms, 0, sizeof(parms)); |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4326 | ret = slave_ops->ndo_neigh_setup(slave->dev, &parms); |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4327 | |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4328 | if (ret) |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4329 | goto out; |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4330 | |
Eric Dumazet | 9e99bfe | 2019-12-07 14:10:34 -0800 | [diff] [blame] | 4331 | if (parms.neigh_setup) |
| 4332 | ret = parms.neigh_setup(n); |
| 4333 | out: |
| 4334 | rcu_read_unlock(); |
| 4335 | return ret; |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4338 | /* The bonding ndo_neigh_setup is called at init time beofre any |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4339 | * slave exists. So we must declare proxy setup function which will |
| 4340 | * be used at run time to resolve the actual slave neigh param setup. |
Veaceslav Falico | 9918d5b | 2013-08-02 19:07:39 +0200 | [diff] [blame] | 4341 | * |
| 4342 | * It's also called by master devices (such as vlans) to setup their |
| 4343 | * underlying devices. In that case - do nothing, we're already set up from |
| 4344 | * our init. |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4345 | */ |
| 4346 | static int bond_neigh_setup(struct net_device *dev, |
| 4347 | struct neigh_parms *parms) |
| 4348 | { |
Veaceslav Falico | 9918d5b | 2013-08-02 19:07:39 +0200 | [diff] [blame] | 4349 | /* modify only our neigh_parms */ |
| 4350 | if (parms->dev == dev) |
| 4351 | parms->neigh_setup = bond_neigh_init; |
Shlomo Pongratz | 234bcf8 | 2012-04-03 22:56:20 +0000 | [diff] [blame] | 4352 | |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 4353 | return 0; |
| 4354 | } |
| 4355 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4356 | /* Change the MTU of all of a master's slaves to match the master */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) |
| 4358 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4359 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 4360 | struct slave *slave, *rollback_slave; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4361 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 4364 | netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4366 | bond_for_each_slave(bond, slave, iter) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4367 | slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n", |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 4368 | slave, slave->dev->netdev_ops->ndo_change_mtu); |
Mitch Williams | e944ef7 | 2005-11-09 10:36:50 -0800 | [diff] [blame] | 4369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | res = dev_set_mtu(slave->dev, new_mtu); |
| 4371 | |
| 4372 | if (res) { |
| 4373 | /* If we failed to set the slave's mtu to the new value |
| 4374 | * we must abort the operation even in ACTIVE_BACKUP |
| 4375 | * mode, because if we allow the backup slaves to have |
| 4376 | * different mtu values than the active slave we'll |
| 4377 | * need to change their mtu when doing a failover. That |
| 4378 | * means changing their mtu from timer context, which |
| 4379 | * is probably not a good idea. |
| 4380 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4381 | slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n", |
| 4382 | res, new_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | goto unwind; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | bond_dev->mtu = new_mtu; |
| 4388 | |
| 4389 | return 0; |
| 4390 | |
| 4391 | unwind: |
| 4392 | /* unwind from head to the slave that failed */ |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4393 | bond_for_each_slave(bond, rollback_slave, iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | int tmp_res; |
| 4395 | |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 4396 | if (rollback_slave == slave) |
| 4397 | break; |
| 4398 | |
| 4399 | tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu); |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4400 | if (tmp_res) |
| 4401 | slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n", |
| 4402 | tmp_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | return res; |
| 4406 | } |
| 4407 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4408 | /* Change HW address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | * |
| 4410 | * Note that many devices must be down to change the HW address, and |
| 4411 | * downing the master releases all slaves. We can make bonds full of |
| 4412 | * bonding devices to test this, however. |
| 4413 | */ |
| 4414 | static int bond_set_mac_address(struct net_device *bond_dev, void *addr) |
| 4415 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4416 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 4417 | struct slave *slave, *rollback_slave; |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 4418 | struct sockaddr_storage *ss = addr, tmp_ss; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4419 | struct list_head *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 4422 | if (BOND_MODE(bond) == BOND_MODE_ALB) |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 4423 | return bond_alb_set_mac_address(bond_dev, addr); |
| 4424 | |
| 4425 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4426 | netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Jay Vosburgh | 1b5acd2 | 2013-05-31 11:57:31 +0000 | [diff] [blame] | 4428 | /* If fail_over_mac is enabled, do nothing and return success. |
| 4429 | * Returning an error causes ifenslave to fail. |
Jay Vosburgh | dd957c5 | 2007-10-09 19:57:24 -0700 | [diff] [blame] | 4430 | */ |
dingtianhong | cc689aa | 2014-01-25 13:00:57 +0800 | [diff] [blame] | 4431 | if (bond->params.fail_over_mac && |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 4432 | BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) |
Jay Vosburgh | dd957c5 | 2007-10-09 19:57:24 -0700 | [diff] [blame] | 4433 | return 0; |
Moni Shoua | 2ab8285 | 2007-10-09 19:43:39 -0700 | [diff] [blame] | 4434 | |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 4435 | if (!is_valid_ether_addr(ss->__data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4438 | bond_for_each_slave(bond, slave, iter) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4439 | slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n", |
| 4440 | __func__, slave); |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 4441 | res = dev_set_mac_address(slave->dev, addr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | if (res) { |
| 4443 | /* TODO: consider downing the slave |
| 4444 | * and retry ? |
| 4445 | * User should expect communications |
| 4446 | * breakage anyway until ARP finish |
| 4447 | * updating, so... |
| 4448 | */ |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4449 | slave_dbg(bond_dev, slave->dev, "%s: err %d\n", |
| 4450 | __func__, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | goto unwind; |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | /* success */ |
Jakub Kicinski | ea52a0b | 2021-10-08 10:53:39 -0700 | [diff] [blame] | 4456 | dev_addr_set(bond_dev, ss->__data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | return 0; |
| 4458 | |
| 4459 | unwind: |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 4460 | memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len); |
| 4461 | tmp_ss.ss_family = bond_dev->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
| 4463 | /* unwind from head to the slave that failed */ |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4464 | bond_for_each_slave(bond, rollback_slave, iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | int tmp_res; |
| 4466 | |
Veaceslav Falico | 81f23b1 | 2013-09-25 09:20:13 +0200 | [diff] [blame] | 4467 | if (rollback_slave == slave) |
| 4468 | break; |
| 4469 | |
Jarod Wilson | faeeb31 | 2017-04-04 17:32:42 -0400 | [diff] [blame] | 4470 | tmp_res = dev_set_mac_address(rollback_slave->dev, |
Petr Machata | 3a37a96 | 2018-12-13 11:54:30 +0000 | [diff] [blame] | 4471 | (struct sockaddr *)&tmp_ss, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | if (tmp_res) { |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4473 | slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n", |
| 4474 | __func__, tmp_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | return res; |
| 4479 | } |
| 4480 | |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4481 | /** |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4482 | * bond_get_slave_by_id - get xmit slave with slave_id |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4483 | * @bond: bonding device that is transmitting |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4484 | * @slave_id: slave id up to slave_cnt-1 through which to transmit |
| 4485 | * |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4486 | * This function tries to get slave with slave_id but in case |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4487 | * it fails, it tries to find the first available slave for transmission. |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4488 | */ |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4489 | static struct slave *bond_get_slave_by_id(struct bonding *bond, |
| 4490 | int slave_id) |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4491 | { |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4492 | struct list_head *iter; |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4493 | struct slave *slave; |
| 4494 | int i = slave_id; |
| 4495 | |
| 4496 | /* Here we start from the slave with slave_id */ |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4497 | bond_for_each_slave_rcu(bond, slave, iter) { |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4498 | if (--i < 0) { |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4499 | if (bond_slave_can_tx(slave)) |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4500 | return slave; |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | /* Here we start from the first slave up to slave_id */ |
| 4505 | i = slave_id; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4506 | bond_for_each_slave_rcu(bond, slave, iter) { |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4507 | if (--i < 0) |
| 4508 | break; |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4509 | if (bond_slave_can_tx(slave)) |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4510 | return slave; |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4511 | } |
| 4512 | /* no slave that can tx has been found */ |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4513 | return NULL; |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4516 | /** |
| 4517 | * bond_rr_gen_slave_id - generate slave id based on packets_per_slave |
| 4518 | * @bond: bonding device to use |
| 4519 | * |
| 4520 | * Based on the value of the bonding device's packets_per_slave parameter |
| 4521 | * this function generates a slave id, which is usually used as the next |
| 4522 | * slave to transmit through. |
| 4523 | */ |
| 4524 | static u32 bond_rr_gen_slave_id(struct bonding *bond) |
| 4525 | { |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4526 | u32 slave_id; |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 4527 | struct reciprocal_value reciprocal_packets_per_slave; |
| 4528 | int packets_per_slave = bond->params.packets_per_slave; |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4529 | |
| 4530 | switch (packets_per_slave) { |
| 4531 | case 0: |
| 4532 | slave_id = prandom_u32(); |
| 4533 | break; |
| 4534 | case 1: |
Jussi Maki | 848ca91 | 2021-06-15 08:54:15 +0000 | [diff] [blame] | 4535 | slave_id = this_cpu_inc_return(*bond->rr_tx_counter); |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4536 | break; |
| 4537 | default: |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 4538 | reciprocal_packets_per_slave = |
| 4539 | bond->params.reciprocal_packets_per_slave; |
Jussi Maki | 848ca91 | 2021-06-15 08:54:15 +0000 | [diff] [blame] | 4540 | slave_id = this_cpu_inc_return(*bond->rr_tx_counter); |
| 4541 | slave_id = reciprocal_divide(slave_id, |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 4542 | reciprocal_packets_per_slave); |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4543 | break; |
| 4544 | } |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4545 | |
| 4546 | return slave_id; |
| 4547 | } |
| 4548 | |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4549 | static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond, |
| 4550 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | { |
Nikolay Aleksandrov | 1507722 | 2013-08-01 16:54:50 +0200 | [diff] [blame] | 4552 | struct slave *slave; |
Cong Wang | 9d1bc24 | 2019-07-01 20:40:24 -0700 | [diff] [blame] | 4553 | int slave_cnt; |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4554 | u32 slave_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 4556 | /* Start with the curr_active_slave that joined the bond as the |
Andy Gospodarek | a2fd940 | 2010-03-25 14:49:05 +0000 | [diff] [blame] | 4557 | * default for sending IGMP traffic. For failover purposes one |
| 4558 | * needs to maintain some consistency for the interface that will |
| 4559 | * send the join/membership reports. The curr_active_slave found |
| 4560 | * will send all of this type of traffic. |
Jay Vosburgh | cf5f904 | 2007-10-17 17:37:47 -0700 | [diff] [blame] | 4561 | */ |
Cong Wang | 9d1bc24 | 2019-07-01 20:40:24 -0700 | [diff] [blame] | 4562 | if (skb->protocol == htons(ETH_P_IP)) { |
| 4563 | int noff = skb_network_offset(skb); |
| 4564 | struct iphdr *iph; |
Nikolay Aleksandrov | 9a72c2d | 2014-09-12 17:38:18 +0200 | [diff] [blame] | 4565 | |
Cong Wang | 9d1bc24 | 2019-07-01 20:40:24 -0700 | [diff] [blame] | 4566 | if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph)))) |
| 4567 | goto non_igmp; |
| 4568 | |
| 4569 | iph = ip_hdr(skb); |
| 4570 | if (iph->protocol == IPPROTO_IGMP) { |
| 4571 | slave = rcu_dereference(bond->curr_active_slave); |
| 4572 | if (slave) |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4573 | return slave; |
| 4574 | return bond_get_slave_by_id(bond, 0); |
Nikolay Aleksandrov | 9a72c2d | 2014-09-12 17:38:18 +0200 | [diff] [blame] | 4575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | } |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 4577 | |
Cong Wang | 9d1bc24 | 2019-07-01 20:40:24 -0700 | [diff] [blame] | 4578 | non_igmp: |
| 4579 | slave_cnt = READ_ONCE(bond->slave_cnt); |
| 4580 | if (likely(slave_cnt)) { |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4581 | slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; |
| 4582 | return bond_get_slave_by_id(bond, slave_id); |
Cong Wang | 9d1bc24 | 2019-07-01 20:40:24 -0700 | [diff] [blame] | 4583 | } |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4584 | return NULL; |
| 4585 | } |
| 4586 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 4587 | static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond, |
| 4588 | struct xdp_buff *xdp) |
| 4589 | { |
| 4590 | struct slave *slave; |
| 4591 | int slave_cnt; |
| 4592 | u32 slave_id; |
| 4593 | const struct ethhdr *eth; |
| 4594 | void *data = xdp->data; |
| 4595 | |
| 4596 | if (data + sizeof(struct ethhdr) > xdp->data_end) |
| 4597 | goto non_igmp; |
| 4598 | |
| 4599 | eth = (struct ethhdr *)data; |
| 4600 | data += sizeof(struct ethhdr); |
| 4601 | |
| 4602 | /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */ |
| 4603 | if (eth->h_proto == htons(ETH_P_IP)) { |
| 4604 | const struct iphdr *iph; |
| 4605 | |
| 4606 | if (data + sizeof(struct iphdr) > xdp->data_end) |
| 4607 | goto non_igmp; |
| 4608 | |
| 4609 | iph = (struct iphdr *)data; |
| 4610 | |
| 4611 | if (iph->protocol == IPPROTO_IGMP) { |
| 4612 | slave = rcu_dereference(bond->curr_active_slave); |
| 4613 | if (slave) |
| 4614 | return slave; |
| 4615 | return bond_get_slave_by_id(bond, 0); |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | non_igmp: |
| 4620 | slave_cnt = READ_ONCE(bond->slave_cnt); |
| 4621 | if (likely(slave_cnt)) { |
| 4622 | slave_id = bond_rr_gen_slave_id(bond) % slave_cnt; |
| 4623 | return bond_get_slave_by_id(bond, slave_id); |
| 4624 | } |
| 4625 | return NULL; |
| 4626 | } |
| 4627 | |
Maor Gottlieb | 29d5bbc | 2020-04-30 22:21:36 +0300 | [diff] [blame] | 4628 | static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb, |
| 4629 | struct net_device *bond_dev) |
| 4630 | { |
| 4631 | struct bonding *bond = netdev_priv(bond_dev); |
| 4632 | struct slave *slave; |
| 4633 | |
| 4634 | slave = bond_xmit_roundrobin_slave_get(bond, skb); |
Saeed Mahameed | 76cd622 | 2020-05-09 00:06:35 -0700 | [diff] [blame] | 4635 | if (likely(slave)) |
| 4636 | return bond_dev_queue_xmit(bond, skb, slave->dev); |
| 4637 | |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4638 | return bond_tx_drop(bond_dev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 4641 | static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond) |
Maor Gottlieb | 5a19f1c | 2020-04-30 22:21:37 +0300 | [diff] [blame] | 4642 | { |
| 4643 | return rcu_dereference(bond->curr_active_slave); |
| 4644 | } |
| 4645 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4646 | /* In active-backup mode, we know that bond->curr_active_slave is always valid if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | * the bond has a usable interface. |
| 4648 | */ |
Tonghao Zhang | dbdc8a2 | 2018-05-11 02:53:10 -0700 | [diff] [blame] | 4649 | static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb, |
| 4650 | struct net_device *bond_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4652 | struct bonding *bond = netdev_priv(bond_dev); |
nikolay@redhat.com | 71bc3b2 | 2013-08-01 16:54:48 +0200 | [diff] [blame] | 4653 | struct slave *slave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 4655 | slave = bond_xmit_activebackup_slave_get(bond); |
nikolay@redhat.com | 71bc3b2 | 2013-08-01 16:54:48 +0200 | [diff] [blame] | 4656 | if (slave) |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4657 | return bond_dev_queue_xmit(bond, skb, slave->dev); |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 4658 | |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4659 | return bond_tx_drop(bond_dev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | } |
| 4661 | |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4662 | /* Use this to update slave_array when (a) it's not appropriate to update |
| 4663 | * slave_array right away (note that update_slave_array() may sleep) |
| 4664 | * and / or (b) RTNL is not held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | */ |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4666 | void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | { |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4668 | queue_delayed_work(bond->wq, &bond->slave_arr_work, delay); |
| 4669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4671 | /* Slave array work handler. Holds only RTNL */ |
| 4672 | static void bond_slave_arr_handler(struct work_struct *work) |
| 4673 | { |
| 4674 | struct bonding *bond = container_of(work, struct bonding, |
| 4675 | slave_arr_work.work); |
| 4676 | int ret; |
| 4677 | |
| 4678 | if (!rtnl_trylock()) |
| 4679 | goto err; |
| 4680 | |
| 4681 | ret = bond_update_slave_arr(bond, NULL); |
| 4682 | rtnl_unlock(); |
| 4683 | if (ret) { |
| 4684 | pr_warn_ratelimited("Failed to update slave array from WT\n"); |
| 4685 | goto err; |
| 4686 | } |
| 4687 | return; |
| 4688 | |
| 4689 | err: |
| 4690 | bond_slave_arr_work_rearm(bond, 1); |
| 4691 | } |
| 4692 | |
Maor Gottlieb | 119d48f | 2020-04-30 22:21:32 +0300 | [diff] [blame] | 4693 | static void bond_skip_slave(struct bond_up_slave *slaves, |
| 4694 | struct slave *skipslave) |
| 4695 | { |
| 4696 | int idx; |
| 4697 | |
| 4698 | /* Rare situation where caller has asked to skip a specific |
| 4699 | * slave but allocation failed (most likely!). BTW this is |
| 4700 | * only possible when the call is initiated from |
| 4701 | * __bond_release_one(). In this situation; overwrite the |
| 4702 | * skipslave entry in the array with the last entry from the |
| 4703 | * array to avoid a situation where the xmit path may choose |
| 4704 | * this to-be-skipped slave to send a packet out. |
| 4705 | */ |
| 4706 | for (idx = 0; slaves && idx < slaves->count; idx++) { |
| 4707 | if (skipslave == slaves->arr[idx]) { |
| 4708 | slaves->arr[idx] = |
| 4709 | slaves->arr[slaves->count - 1]; |
| 4710 | slaves->count--; |
| 4711 | break; |
| 4712 | } |
| 4713 | } |
| 4714 | } |
| 4715 | |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4716 | static void bond_set_slave_arr(struct bonding *bond, |
| 4717 | struct bond_up_slave *usable_slaves, |
| 4718 | struct bond_up_slave *all_slaves) |
| 4719 | { |
| 4720 | struct bond_up_slave *usable, *all; |
| 4721 | |
| 4722 | usable = rtnl_dereference(bond->usable_slaves); |
| 4723 | rcu_assign_pointer(bond->usable_slaves, usable_slaves); |
| 4724 | kfree_rcu(usable, rcu); |
| 4725 | |
| 4726 | all = rtnl_dereference(bond->all_slaves); |
| 4727 | rcu_assign_pointer(bond->all_slaves, all_slaves); |
| 4728 | kfree_rcu(all, rcu); |
| 4729 | } |
| 4730 | |
| 4731 | static void bond_reset_slave_arr(struct bonding *bond) |
| 4732 | { |
| 4733 | struct bond_up_slave *usable, *all; |
| 4734 | |
| 4735 | usable = rtnl_dereference(bond->usable_slaves); |
| 4736 | if (usable) { |
| 4737 | RCU_INIT_POINTER(bond->usable_slaves, NULL); |
| 4738 | kfree_rcu(usable, rcu); |
| 4739 | } |
| 4740 | |
| 4741 | all = rtnl_dereference(bond->all_slaves); |
| 4742 | if (all) { |
| 4743 | RCU_INIT_POINTER(bond->all_slaves, NULL); |
| 4744 | kfree_rcu(all, rcu); |
| 4745 | } |
| 4746 | } |
| 4747 | |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4748 | /* Build the usable slaves array in control path for modes that use xmit-hash |
| 4749 | * to determine the slave interface - |
| 4750 | * (a) BOND_MODE_8023AD |
| 4751 | * (b) BOND_MODE_XOR |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 4752 | * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0 |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4753 | * |
| 4754 | * The caller is expected to hold RTNL only and NO other lock! |
| 4755 | */ |
| 4756 | int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave) |
| 4757 | { |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4758 | struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4759 | struct slave *slave; |
| 4760 | struct list_head *iter; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4761 | int agg_id = 0; |
| 4762 | int ret = 0; |
| 4763 | |
jinyiting | 83d686a | 2021-04-21 16:38:21 +0800 | [diff] [blame] | 4764 | might_sleep(); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4765 | |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 4766 | usable_slaves = kzalloc(struct_size(usable_slaves, arr, |
| 4767 | bond->slave_cnt), GFP_KERNEL); |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4768 | all_slaves = kzalloc(struct_size(all_slaves, arr, |
| 4769 | bond->slave_cnt), GFP_KERNEL); |
| 4770 | if (!usable_slaves || !all_slaves) { |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4771 | ret = -ENOMEM; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4772 | goto out; |
| 4773 | } |
| 4774 | if (BOND_MODE(bond) == BOND_MODE_8023AD) { |
| 4775 | struct ad_info ad_info; |
| 4776 | |
jinyiting | 83d686a | 2021-04-21 16:38:21 +0800 | [diff] [blame] | 4777 | spin_lock_bh(&bond->mode_lock); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4778 | if (bond_3ad_get_active_agg_info(bond, &ad_info)) { |
jinyiting | 83d686a | 2021-04-21 16:38:21 +0800 | [diff] [blame] | 4779 | spin_unlock_bh(&bond->mode_lock); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4780 | pr_debug("bond_3ad_get_active_agg_info failed\n"); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4781 | /* No active aggragator means it's not safe to use |
| 4782 | * the previous array. |
| 4783 | */ |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4784 | bond_reset_slave_arr(bond); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4785 | goto out; |
| 4786 | } |
jinyiting | 83d686a | 2021-04-21 16:38:21 +0800 | [diff] [blame] | 4787 | spin_unlock_bh(&bond->mode_lock); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4788 | agg_id = ad_info.aggregator_id; |
| 4789 | } |
| 4790 | bond_for_each_slave(bond, slave, iter) { |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4791 | if (skipslave == slave) |
| 4792 | continue; |
| 4793 | |
| 4794 | all_slaves->arr[all_slaves->count++] = slave; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4795 | if (BOND_MODE(bond) == BOND_MODE_8023AD) { |
| 4796 | struct aggregator *agg; |
| 4797 | |
| 4798 | agg = SLAVE_AD_INFO(slave)->port.aggregator; |
| 4799 | if (!agg || agg->aggregator_identifier != agg_id) |
| 4800 | continue; |
| 4801 | } |
| 4802 | if (!bond_slave_can_tx(slave)) |
| 4803 | continue; |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 4804 | |
Jarod Wilson | e2a7420 | 2019-06-07 10:59:29 -0400 | [diff] [blame] | 4805 | slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n", |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 4806 | usable_slaves->count); |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 4807 | |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 4808 | usable_slaves->arr[usable_slaves->count++] = slave; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4811 | bond_set_slave_arr(bond, usable_slaves, all_slaves); |
| 4812 | return ret; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4813 | out: |
| 4814 | if (ret != 0 && skipslave) { |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4815 | bond_skip_slave(rtnl_dereference(bond->all_slaves), |
| 4816 | skipslave); |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 4817 | bond_skip_slave(rtnl_dereference(bond->usable_slaves), |
| 4818 | skipslave); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4819 | } |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 4820 | kfree_rcu(all_slaves, rcu); |
| 4821 | kfree_rcu(usable_slaves, rcu); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4822 | |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4823 | return ret; |
| 4824 | } |
| 4825 | |
Maor Gottlieb | c071d91 | 2020-04-30 22:21:35 +0300 | [diff] [blame] | 4826 | static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond, |
| 4827 | struct sk_buff *skb, |
| 4828 | struct bond_up_slave *slaves) |
| 4829 | { |
| 4830 | struct slave *slave; |
| 4831 | unsigned int count; |
| 4832 | u32 hash; |
| 4833 | |
| 4834 | hash = bond_xmit_hash(bond, skb); |
| 4835 | count = slaves ? READ_ONCE(slaves->count) : 0; |
| 4836 | if (unlikely(!count)) |
| 4837 | return NULL; |
| 4838 | |
| 4839 | slave = slaves->arr[hash % count]; |
| 4840 | return slave; |
| 4841 | } |
| 4842 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 4843 | static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond, |
| 4844 | struct xdp_buff *xdp) |
| 4845 | { |
| 4846 | struct bond_up_slave *slaves; |
| 4847 | unsigned int count; |
| 4848 | u32 hash; |
| 4849 | |
| 4850 | hash = bond_xmit_hash_xdp(bond, xdp); |
| 4851 | slaves = rcu_dereference(bond->usable_slaves); |
| 4852 | count = slaves ? READ_ONCE(slaves->count) : 0; |
| 4853 | if (unlikely(!count)) |
| 4854 | return NULL; |
| 4855 | |
| 4856 | return slaves->arr[hash % count]; |
| 4857 | } |
| 4858 | |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4859 | /* Use this Xmit function for 3AD as well as XOR modes. The current |
| 4860 | * usable slave array is formed in the control path. The xmit function |
| 4861 | * just calculates hash and sends the packet out. |
| 4862 | */ |
Tonghao Zhang | dbdc8a2 | 2018-05-11 02:53:10 -0700 | [diff] [blame] | 4863 | static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb, |
| 4864 | struct net_device *dev) |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4865 | { |
| 4866 | struct bonding *bond = netdev_priv(dev); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4867 | struct bond_up_slave *slaves; |
Maor Gottlieb | c071d91 | 2020-04-30 22:21:35 +0300 | [diff] [blame] | 4868 | struct slave *slave; |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 4869 | |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 4870 | slaves = rcu_dereference(bond->usable_slaves); |
Maor Gottlieb | c071d91 | 2020-04-30 22:21:35 +0300 | [diff] [blame] | 4871 | slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); |
| 4872 | if (likely(slave)) |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4873 | return bond_dev_queue_xmit(bond, skb, slave->dev); |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 4874 | |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 4875 | return bond_tx_drop(dev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
Nikolay Aleksandrov | 78a646c | 2013-08-01 16:54:49 +0200 | [diff] [blame] | 4878 | /* in broadcast mode, we send everything to all usable interfaces. */ |
Tonghao Zhang | dbdc8a2 | 2018-05-11 02:53:10 -0700 | [diff] [blame] | 4879 | static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb, |
| 4880 | struct net_device *bond_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 4882 | struct bonding *bond = netdev_priv(bond_dev); |
Nikolay Aleksandrov | 78a646c | 2013-08-01 16:54:49 +0200 | [diff] [blame] | 4883 | struct slave *slave = NULL; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4884 | struct list_head *iter; |
Jie Wang | 4e5bd03 | 2022-01-12 20:54:18 +0800 | [diff] [blame] | 4885 | bool xmit_suc = false; |
| 4886 | bool skb_used = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4888 | bond_for_each_slave_rcu(bond, slave, iter) { |
Jie Wang | 4e5bd03 | 2022-01-12 20:54:18 +0800 | [diff] [blame] | 4889 | struct sk_buff *skb2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | |
Jie Wang | 4e5bd03 | 2022-01-12 20:54:18 +0800 | [diff] [blame] | 4891 | if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP)) |
| 4892 | continue; |
| 4893 | |
| 4894 | if (bond_is_last_slave(bond, slave)) { |
| 4895 | skb2 = skb; |
| 4896 | skb_used = true; |
| 4897 | } else { |
| 4898 | skb2 = skb_clone(skb, GFP_ATOMIC); |
Nikolay Aleksandrov | 78a646c | 2013-08-01 16:54:49 +0200 | [diff] [blame] | 4899 | if (!skb2) { |
dingtianhong | 9152e26 | 2014-03-25 17:00:10 +0800 | [diff] [blame] | 4900 | net_err_ratelimited("%s: Error: %s: skb_clone() failed\n", |
| 4901 | bond_dev->name, __func__); |
Nikolay Aleksandrov | 78a646c | 2013-08-01 16:54:49 +0200 | [diff] [blame] | 4902 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | } |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 4905 | |
Jie Wang | 4e5bd03 | 2022-01-12 20:54:18 +0800 | [diff] [blame] | 4906 | if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK) |
| 4907 | xmit_suc = true; |
| 4908 | } |
| 4909 | |
| 4910 | if (!skb_used) |
| 4911 | dev_kfree_skb_any(skb); |
| 4912 | |
| 4913 | if (xmit_suc) |
| 4914 | return NETDEV_TX_OK; |
| 4915 | |
| 4916 | atomic_long_inc(&bond_dev->tx_dropped); |
| 4917 | return NET_XMIT_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | /*------------------------- Device initialization ---------------------------*/ |
| 4921 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4922 | /* Lookup the slave that corresponds to a qid */ |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4923 | static inline int bond_slave_override(struct bonding *bond, |
| 4924 | struct sk_buff *skb) |
| 4925 | { |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4926 | struct slave *slave = NULL; |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 4927 | struct list_head *iter; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4928 | |
Tonghao Zhang | ae35c6f | 2018-05-11 02:53:11 -0700 | [diff] [blame] | 4929 | if (!skb_rx_queue_recorded(skb)) |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 4930 | return 1; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4931 | |
| 4932 | /* Find out if any slaves have the same mapping as this skb. */ |
dingtianhong | 3900f29 | 2014-01-02 09:13:06 +0800 | [diff] [blame] | 4933 | bond_for_each_slave_rcu(bond, slave, iter) { |
Tonghao Zhang | ae35c6f | 2018-05-11 02:53:11 -0700 | [diff] [blame] | 4934 | if (slave->queue_id == skb_get_queue_mapping(skb)) { |
Anton Nayshtut | f5e2dc5 | 2015-03-29 14:20:25 +0300 | [diff] [blame] | 4935 | if (bond_slave_is_up(slave) && |
| 4936 | slave->link == BOND_LINK_UP) { |
dingtianhong | 3900f29 | 2014-01-02 09:13:06 +0800 | [diff] [blame] | 4937 | bond_dev_queue_xmit(bond, skb, slave->dev); |
| 4938 | return 0; |
| 4939 | } |
| 4940 | /* If the slave isn't UP, use default transmit policy. */ |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4941 | break; |
| 4942 | } |
| 4943 | } |
| 4944 | |
dingtianhong | 3900f29 | 2014-01-02 09:13:06 +0800 | [diff] [blame] | 4945 | return 1; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
Neil Horman | 374eeb5 | 2011-06-03 10:35:52 +0000 | [diff] [blame] | 4948 | |
Jason Wang | f663dd9 | 2014-01-10 16:18:26 +0800 | [diff] [blame] | 4949 | static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb, |
Paolo Abeni | a350ecc | 2019-03-20 11:02:06 +0100 | [diff] [blame] | 4950 | struct net_device *sb_dev) |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4951 | { |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4952 | /* This helper function exists to help dev_pick_tx get the correct |
Phil Oester | fd0e435 | 2011-03-14 06:22:04 +0000 | [diff] [blame] | 4953 | * destination queue. Using a helper function skips a call to |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4954 | * skb_tx_hash and will put the skbs in the queue we expect on their |
| 4955 | * way down to the bonding driver. |
| 4956 | */ |
Phil Oester | fd0e435 | 2011-03-14 06:22:04 +0000 | [diff] [blame] | 4957 | u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; |
| 4958 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 4959 | /* Save the original txq to restore before passing to the driver */ |
Tonghao Zhang | ae35c6f | 2018-05-11 02:53:11 -0700 | [diff] [blame] | 4960 | qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb); |
Neil Horman | 374eeb5 | 2011-06-03 10:35:52 +0000 | [diff] [blame] | 4961 | |
Phil Oester | fd0e435 | 2011-03-14 06:22:04 +0000 | [diff] [blame] | 4962 | if (unlikely(txq >= dev->real_num_tx_queues)) { |
David Decotigny | d30ee67 | 2011-04-13 15:22:29 +0000 | [diff] [blame] | 4963 | do { |
Phil Oester | fd0e435 | 2011-03-14 06:22:04 +0000 | [diff] [blame] | 4964 | txq -= dev->real_num_tx_queues; |
David Decotigny | d30ee67 | 2011-04-13 15:22:29 +0000 | [diff] [blame] | 4965 | } while (txq >= dev->real_num_tx_queues); |
Phil Oester | fd0e435 | 2011-03-14 06:22:04 +0000 | [diff] [blame] | 4966 | } |
| 4967 | return txq; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Maor Gottlieb | 33720aa | 2020-04-30 22:21:39 +0300 | [diff] [blame] | 4970 | static struct net_device *bond_xmit_get_slave(struct net_device *master_dev, |
| 4971 | struct sk_buff *skb, |
| 4972 | bool all_slaves) |
| 4973 | { |
| 4974 | struct bonding *bond = netdev_priv(master_dev); |
| 4975 | struct bond_up_slave *slaves; |
| 4976 | struct slave *slave = NULL; |
| 4977 | |
| 4978 | switch (BOND_MODE(bond)) { |
| 4979 | case BOND_MODE_ROUNDROBIN: |
| 4980 | slave = bond_xmit_roundrobin_slave_get(bond, skb); |
| 4981 | break; |
| 4982 | case BOND_MODE_ACTIVEBACKUP: |
Jussi Maki | a815bde | 2021-07-31 05:57:32 +0000 | [diff] [blame] | 4983 | slave = bond_xmit_activebackup_slave_get(bond); |
Maor Gottlieb | 33720aa | 2020-04-30 22:21:39 +0300 | [diff] [blame] | 4984 | break; |
| 4985 | case BOND_MODE_8023AD: |
| 4986 | case BOND_MODE_XOR: |
| 4987 | if (all_slaves) |
| 4988 | slaves = rcu_dereference(bond->all_slaves); |
| 4989 | else |
| 4990 | slaves = rcu_dereference(bond->usable_slaves); |
| 4991 | slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves); |
| 4992 | break; |
| 4993 | case BOND_MODE_BROADCAST: |
| 4994 | break; |
| 4995 | case BOND_MODE_ALB: |
| 4996 | slave = bond_xmit_alb_slave_get(bond, skb); |
| 4997 | break; |
| 4998 | case BOND_MODE_TLB: |
| 4999 | slave = bond_xmit_tlb_slave_get(bond, skb); |
| 5000 | break; |
| 5001 | default: |
| 5002 | /* Should never happen, mode already checked */ |
| 5003 | WARN_ONCE(true, "Unknown bonding mode"); |
| 5004 | break; |
| 5005 | } |
| 5006 | |
| 5007 | if (slave) |
| 5008 | return slave->dev; |
| 5009 | return NULL; |
| 5010 | } |
| 5011 | |
Tariq Toukan | 007feb8 | 2021-01-17 16:59:44 +0200 | [diff] [blame] | 5012 | static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow) |
| 5013 | { |
| 5014 | switch (sk->sk_family) { |
| 5015 | #if IS_ENABLED(CONFIG_IPV6) |
| 5016 | case AF_INET6: |
| 5017 | if (sk->sk_ipv6only || |
| 5018 | ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) { |
| 5019 | flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 5020 | flow->addrs.v6addrs.src = inet6_sk(sk)->saddr; |
| 5021 | flow->addrs.v6addrs.dst = sk->sk_v6_daddr; |
| 5022 | break; |
| 5023 | } |
| 5024 | fallthrough; |
| 5025 | #endif |
| 5026 | default: /* AF_INET */ |
| 5027 | flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 5028 | flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr; |
| 5029 | flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr; |
| 5030 | break; |
| 5031 | } |
| 5032 | |
| 5033 | flow->ports.src = inet_sk(sk)->inet_sport; |
| 5034 | flow->ports.dst = inet_sk(sk)->inet_dport; |
| 5035 | } |
| 5036 | |
| 5037 | /** |
| 5038 | * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields |
| 5039 | * @sk: socket to use for headers |
| 5040 | * |
| 5041 | * This function will extract the necessary field from the socket and use |
| 5042 | * them to generate a hash based on the LAYER34 xmit_policy. |
| 5043 | * Assumes that sk is a TCP or UDP socket. |
| 5044 | */ |
| 5045 | static u32 bond_sk_hash_l34(struct sock *sk) |
| 5046 | { |
| 5047 | struct flow_keys flow; |
| 5048 | u32 hash; |
| 5049 | |
| 5050 | bond_sk_to_flow(sk, &flow); |
| 5051 | |
| 5052 | /* L4 */ |
| 5053 | memcpy(&hash, &flow.ports.ports, sizeof(hash)); |
| 5054 | /* L3 */ |
| 5055 | return bond_ip_hash(hash, &flow); |
| 5056 | } |
| 5057 | |
| 5058 | static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond, |
| 5059 | struct sock *sk) |
| 5060 | { |
| 5061 | struct bond_up_slave *slaves; |
| 5062 | struct slave *slave; |
| 5063 | unsigned int count; |
| 5064 | u32 hash; |
| 5065 | |
| 5066 | slaves = rcu_dereference(bond->usable_slaves); |
| 5067 | count = slaves ? READ_ONCE(slaves->count) : 0; |
| 5068 | if (unlikely(!count)) |
| 5069 | return NULL; |
| 5070 | |
| 5071 | hash = bond_sk_hash_l34(sk); |
| 5072 | slave = slaves->arr[hash % count]; |
| 5073 | |
| 5074 | return slave->dev; |
| 5075 | } |
| 5076 | |
| 5077 | static struct net_device *bond_sk_get_lower_dev(struct net_device *dev, |
| 5078 | struct sock *sk) |
| 5079 | { |
| 5080 | struct bonding *bond = netdev_priv(dev); |
| 5081 | struct net_device *lower = NULL; |
| 5082 | |
| 5083 | rcu_read_lock(); |
| 5084 | if (bond_sk_check(bond)) |
| 5085 | lower = __bond_sk_get_lower_dev(bond, sk); |
| 5086 | rcu_read_unlock(); |
| 5087 | |
| 5088 | return lower; |
| 5089 | } |
| 5090 | |
Tariq Toukan | 89df6a8 | 2021-01-17 16:59:46 +0200 | [diff] [blame] | 5091 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
| 5092 | static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb, |
| 5093 | struct net_device *dev) |
| 5094 | { |
| 5095 | if (likely(bond_get_slave_by_dev(bond, tls_get_ctx(skb->sk)->netdev))) |
| 5096 | return bond_dev_queue_xmit(bond, skb, tls_get_ctx(skb->sk)->netdev); |
| 5097 | return bond_tx_drop(dev, skb); |
| 5098 | } |
| 5099 | #endif |
| 5100 | |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5101 | static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5102 | { |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 5103 | struct bonding *bond = netdev_priv(dev); |
| 5104 | |
Veaceslav Falico | d1e2e5c | 2014-05-15 21:39:52 +0200 | [diff] [blame] | 5105 | if (bond_should_override_tx_queue(bond) && |
| 5106 | !bond_slave_override(bond, skb)) |
| 5107 | return NETDEV_TX_OK; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5108 | |
Tariq Toukan | 89df6a8 | 2021-01-17 16:59:46 +0200 | [diff] [blame] | 5109 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
| 5110 | if (skb->sk && tls_is_sk_tx_device_offloaded(skb->sk)) |
| 5111 | return bond_tls_device_xmit(bond, skb, dev); |
| 5112 | #endif |
| 5113 | |
Veaceslav Falico | 0184409 | 2014-05-15 21:39:55 +0200 | [diff] [blame] | 5114 | switch (BOND_MODE(bond)) { |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5115 | case BOND_MODE_ROUNDROBIN: |
| 5116 | return bond_xmit_roundrobin(skb, dev); |
| 5117 | case BOND_MODE_ACTIVEBACKUP: |
| 5118 | return bond_xmit_activebackup(skb, dev); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 5119 | case BOND_MODE_8023AD: |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5120 | case BOND_MODE_XOR: |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 5121 | return bond_3ad_xor_xmit(skb, dev); |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5122 | case BOND_MODE_BROADCAST: |
| 5123 | return bond_xmit_broadcast(skb, dev); |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5124 | case BOND_MODE_ALB: |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5125 | return bond_alb_xmit(skb, dev); |
Mahesh Bandewar | f05b42e | 2014-04-22 16:30:20 -0700 | [diff] [blame] | 5126 | case BOND_MODE_TLB: |
| 5127 | return bond_tlb_xmit(skb, dev); |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5128 | default: |
| 5129 | /* Should never happen, mode already checked */ |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 5130 | netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond)); |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5131 | WARN_ON_ONCE(1); |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 5132 | return bond_tx_drop(dev, skb); |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5136 | static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 5137 | { |
| 5138 | struct bonding *bond = netdev_priv(dev); |
| 5139 | netdev_tx_t ret = NETDEV_TX_OK; |
| 5140 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5141 | /* If we risk deadlock from transmitting this in the |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5142 | * netpoll path, tell netpoll to queue the frame for later tx |
| 5143 | */ |
dingtianhong | 054bb88 | 2014-03-25 17:00:09 +0800 | [diff] [blame] | 5144 | if (unlikely(is_netpoll_tx_blocked(dev))) |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5145 | return NETDEV_TX_BUSY; |
| 5146 | |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 5147 | rcu_read_lock(); |
Veaceslav Falico | 0965a1f | 2013-09-25 09:20:21 +0200 | [diff] [blame] | 5148 | if (bond_has_slaves(bond)) |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5149 | ret = __bond_start_xmit(skb, dev); |
| 5150 | else |
Eric Dumazet | ae46f184 | 2020-05-07 09:32:22 -0700 | [diff] [blame] | 5151 | ret = bond_tx_drop(dev, skb); |
nikolay@redhat.com | 278b208 | 2013-08-01 16:54:51 +0200 | [diff] [blame] | 5152 | rcu_read_unlock(); |
Michał Mirosław | 0693e88 | 2011-05-07 01:48:02 +0000 | [diff] [blame] | 5153 | |
| 5154 | return ret; |
| 5155 | } |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5156 | |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5157 | static struct net_device * |
| 5158 | bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp) |
| 5159 | { |
| 5160 | struct bonding *bond = netdev_priv(bond_dev); |
| 5161 | struct slave *slave; |
| 5162 | |
| 5163 | /* Caller needs to hold rcu_read_lock() */ |
| 5164 | |
| 5165 | switch (BOND_MODE(bond)) { |
| 5166 | case BOND_MODE_ROUNDROBIN: |
| 5167 | slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp); |
| 5168 | break; |
| 5169 | |
| 5170 | case BOND_MODE_ACTIVEBACKUP: |
| 5171 | slave = bond_xmit_activebackup_slave_get(bond); |
| 5172 | break; |
| 5173 | |
| 5174 | case BOND_MODE_8023AD: |
| 5175 | case BOND_MODE_XOR: |
| 5176 | slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp); |
| 5177 | break; |
| 5178 | |
| 5179 | default: |
| 5180 | /* Should never happen. Mode guarded by bond_xdp_check() */ |
| 5181 | netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n", BOND_MODE(bond)); |
| 5182 | WARN_ON_ONCE(1); |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | |
| 5186 | if (slave) |
| 5187 | return slave->dev; |
| 5188 | |
| 5189 | return NULL; |
| 5190 | } |
| 5191 | |
| 5192 | static int bond_xdp_xmit(struct net_device *bond_dev, |
| 5193 | int n, struct xdp_frame **frames, u32 flags) |
| 5194 | { |
| 5195 | int nxmit, err = -ENXIO; |
| 5196 | |
| 5197 | rcu_read_lock(); |
| 5198 | |
| 5199 | for (nxmit = 0; nxmit < n; nxmit++) { |
| 5200 | struct xdp_frame *frame = frames[nxmit]; |
| 5201 | struct xdp_frame *frames1[] = {frame}; |
| 5202 | struct net_device *slave_dev; |
| 5203 | struct xdp_buff xdp; |
| 5204 | |
| 5205 | xdp_convert_frame_to_buff(frame, &xdp); |
| 5206 | |
| 5207 | slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp); |
| 5208 | if (!slave_dev) { |
| 5209 | err = -ENXIO; |
| 5210 | break; |
| 5211 | } |
| 5212 | |
| 5213 | err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags); |
| 5214 | if (err < 1) |
| 5215 | break; |
| 5216 | } |
| 5217 | |
| 5218 | rcu_read_unlock(); |
| 5219 | |
| 5220 | /* If error happened on the first frame then we can pass the error up, otherwise |
| 5221 | * report the number of frames that were xmitted. |
| 5222 | */ |
| 5223 | if (err < 0) |
| 5224 | return (nxmit == 0 ? err : nxmit); |
| 5225 | |
| 5226 | return nxmit; |
| 5227 | } |
| 5228 | |
| 5229 | static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
| 5230 | struct netlink_ext_ack *extack) |
| 5231 | { |
| 5232 | struct bonding *bond = netdev_priv(dev); |
| 5233 | struct list_head *iter; |
| 5234 | struct slave *slave, *rollback_slave; |
| 5235 | struct bpf_prog *old_prog; |
| 5236 | struct netdev_bpf xdp = { |
| 5237 | .command = XDP_SETUP_PROG, |
| 5238 | .flags = 0, |
| 5239 | .prog = prog, |
| 5240 | .extack = extack, |
| 5241 | }; |
| 5242 | int err; |
| 5243 | |
| 5244 | ASSERT_RTNL(); |
| 5245 | |
| 5246 | if (!bond_xdp_check(bond)) |
| 5247 | return -EOPNOTSUPP; |
| 5248 | |
| 5249 | old_prog = bond->xdp_prog; |
| 5250 | bond->xdp_prog = prog; |
| 5251 | |
| 5252 | bond_for_each_slave(bond, slave, iter) { |
| 5253 | struct net_device *slave_dev = slave->dev; |
| 5254 | |
| 5255 | if (!slave_dev->netdev_ops->ndo_bpf || |
| 5256 | !slave_dev->netdev_ops->ndo_xdp_xmit) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 5257 | SLAVE_NL_ERR(dev, slave_dev, extack, |
| 5258 | "Slave device does not support XDP"); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5259 | err = -EOPNOTSUPP; |
| 5260 | goto err; |
| 5261 | } |
| 5262 | |
| 5263 | if (dev_xdp_prog_count(slave_dev) > 0) { |
Jonathan Toppins | 6569fa2 | 2021-08-10 22:53:31 -0400 | [diff] [blame] | 5264 | SLAVE_NL_ERR(dev, slave_dev, extack, |
| 5265 | "Slave has XDP program loaded, please unload before enslaving"); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5266 | err = -EOPNOTSUPP; |
| 5267 | goto err; |
| 5268 | } |
| 5269 | |
| 5270 | err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); |
| 5271 | if (err < 0) { |
| 5272 | /* ndo_bpf() sets extack error message */ |
| 5273 | slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err); |
| 5274 | goto err; |
| 5275 | } |
| 5276 | if (prog) |
| 5277 | bpf_prog_inc(prog); |
| 5278 | } |
| 5279 | |
Jussi Maki | 6d5f1ef | 2021-09-06 10:56:37 +0200 | [diff] [blame] | 5280 | if (prog) { |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5281 | static_branch_inc(&bpf_master_redirect_enabled_key); |
Jussi Maki | 6d5f1ef | 2021-09-06 10:56:37 +0200 | [diff] [blame] | 5282 | } else if (old_prog) { |
| 5283 | bpf_prog_put(old_prog); |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5284 | static_branch_dec(&bpf_master_redirect_enabled_key); |
Jussi Maki | 6d5f1ef | 2021-09-06 10:56:37 +0200 | [diff] [blame] | 5285 | } |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5286 | |
| 5287 | return 0; |
| 5288 | |
| 5289 | err: |
| 5290 | /* unwind the program changes */ |
| 5291 | bond->xdp_prog = old_prog; |
| 5292 | xdp.prog = old_prog; |
| 5293 | xdp.extack = NULL; /* do not overwrite original error */ |
| 5294 | |
| 5295 | bond_for_each_slave(bond, rollback_slave, iter) { |
| 5296 | struct net_device *slave_dev = rollback_slave->dev; |
| 5297 | int err_unwind; |
| 5298 | |
| 5299 | if (slave == rollback_slave) |
| 5300 | break; |
| 5301 | |
| 5302 | err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp); |
| 5303 | if (err_unwind < 0) |
| 5304 | slave_err(dev, slave_dev, |
| 5305 | "Error %d when unwinding XDP program change\n", err_unwind); |
| 5306 | else if (xdp.prog) |
| 5307 | bpf_prog_inc(xdp.prog); |
| 5308 | } |
| 5309 | return err; |
| 5310 | } |
| 5311 | |
| 5312 | static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp) |
| 5313 | { |
| 5314 | switch (xdp->command) { |
| 5315 | case XDP_SETUP_PROG: |
| 5316 | return bond_xdp_set(dev, xdp->prog, xdp->extack); |
| 5317 | default: |
| 5318 | return -EINVAL; |
| 5319 | } |
| 5320 | } |
| 5321 | |
Jarod Wilson | 4ca0d9a | 2020-08-13 10:09:00 -0400 | [diff] [blame] | 5322 | static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed) |
| 5323 | { |
| 5324 | if (speed == 0 || speed == SPEED_UNKNOWN) |
| 5325 | speed = slave->speed; |
| 5326 | else |
| 5327 | speed = min(speed, slave->speed); |
| 5328 | |
| 5329 | return speed; |
| 5330 | } |
| 5331 | |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5332 | static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev, |
| 5333 | struct ethtool_link_ksettings *cmd) |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5334 | { |
| 5335 | struct bonding *bond = netdev_priv(bond_dev); |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 5336 | struct list_head *iter; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 5337 | struct slave *slave; |
Jarod Wilson | 4ca0d9a | 2020-08-13 10:09:00 -0400 | [diff] [blame] | 5338 | u32 speed = 0; |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5339 | |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5340 | cmd->base.duplex = DUPLEX_UNKNOWN; |
| 5341 | cmd->base.port = PORT_OTHER; |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5342 | |
Veaceslav Falico | 8557cd7 | 2014-05-15 21:39:59 +0200 | [diff] [blame] | 5343 | /* Since bond_slave_can_tx returns false for all inactive or down slaves, we |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5344 | * do not need to check mode. Though link speed might not represent |
| 5345 | * the true receive or transmit bandwidth (not all modes are symmetric) |
| 5346 | * this is an accurate maximum. |
| 5347 | */ |
Veaceslav Falico | 9caff1e7 | 2013-09-25 09:20:14 +0200 | [diff] [blame] | 5348 | bond_for_each_slave(bond, slave, iter) { |
Veaceslav Falico | 8557cd7 | 2014-05-15 21:39:59 +0200 | [diff] [blame] | 5349 | if (bond_slave_can_tx(slave)) { |
Jarod Wilson | 4ca0d9a | 2020-08-13 10:09:00 -0400 | [diff] [blame] | 5350 | if (slave->speed != SPEED_UNKNOWN) { |
| 5351 | if (BOND_MODE(bond) == BOND_MODE_BROADCAST) |
| 5352 | speed = bond_mode_bcast_speed(slave, |
| 5353 | speed); |
| 5354 | else |
| 5355 | speed += slave->speed; |
| 5356 | } |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5357 | if (cmd->base.duplex == DUPLEX_UNKNOWN && |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5358 | slave->duplex != DUPLEX_UNKNOWN) |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5359 | cmd->base.duplex = slave->duplex; |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5360 | } |
| 5361 | } |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5362 | cmd->base.speed = speed ? : SPEED_UNKNOWN; |
nikolay@redhat.com | dec1e90 | 2013-08-01 16:54:47 +0200 | [diff] [blame] | 5363 | |
Andy Gospodarek | bb5b052 | 2013-04-16 14:46:00 +0000 | [diff] [blame] | 5364 | return 0; |
| 5365 | } |
| 5366 | |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 5367 | static void bond_ethtool_get_drvinfo(struct net_device *bond_dev, |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 5368 | struct ethtool_drvinfo *drvinfo) |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 5369 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 5370 | strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 5371 | snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d", |
| 5372 | BOND_ABI_VERSION); |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 5373 | } |
| 5374 | |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 5375 | static int bond_ethtool_get_ts_info(struct net_device *bond_dev, |
| 5376 | struct ethtool_ts_info *info) |
| 5377 | { |
| 5378 | struct bonding *bond = netdev_priv(bond_dev); |
| 5379 | const struct ethtool_ops *ops; |
| 5380 | struct net_device *real_dev; |
| 5381 | struct phy_device *phydev; |
| 5382 | |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 5383 | real_dev = bond_option_active_slave_get_rcu(bond); |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 5384 | if (real_dev) { |
| 5385 | ops = real_dev->ethtool_ops; |
| 5386 | phydev = real_dev->phydev; |
| 5387 | |
| 5388 | if (phy_has_tsinfo(phydev)) { |
| 5389 | return phy_ts_info(phydev, info); |
| 5390 | } else if (ops->get_ts_info) { |
| 5391 | return ops->get_ts_info(real_dev, info); |
| 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | |
| 5396 | SOF_TIMESTAMPING_SOFTWARE; |
| 5397 | info->phc_index = -1; |
| 5398 | |
| 5399 | return 0; |
| 5400 | } |
| 5401 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 5402 | static const struct ethtool_ops bond_ethtool_ops = { |
Jay Vosburgh | 217df67 | 2005-09-26 16:11:50 -0700 | [diff] [blame] | 5403 | .get_drvinfo = bond_ethtool_get_drvinfo, |
Stephen Hemminger | fa53eba | 2008-09-13 21:17:09 -0400 | [diff] [blame] | 5404 | .get_link = ethtool_op_get_link, |
Philippe Reynes | d46b634 | 2016-10-25 18:41:31 +0200 | [diff] [blame] | 5405 | .get_link_ksettings = bond_ethtool_get_link_ksettings, |
Hangbin Liu | 94dd016 | 2021-11-30 15:09:32 +0800 | [diff] [blame] | 5406 | .get_ts_info = bond_ethtool_get_ts_info, |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 5407 | }; |
| 5408 | |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5409 | static const struct net_device_ops bond_netdev_ops = { |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5410 | .ndo_init = bond_init, |
Stephen Hemminger | 9e71626 | 2009-06-12 19:02:47 +0000 | [diff] [blame] | 5411 | .ndo_uninit = bond_uninit, |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5412 | .ndo_open = bond_open, |
| 5413 | .ndo_stop = bond_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5414 | .ndo_start_xmit = bond_start_xmit, |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 5415 | .ndo_select_queue = bond_select_queue, |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 5416 | .ndo_get_stats64 = bond_get_stats, |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 5417 | .ndo_eth_ioctl = bond_eth_ioctl, |
Arnd Bergmann | 3d9d00bd | 2021-07-27 15:45:17 +0200 | [diff] [blame] | 5418 | .ndo_siocbond = bond_do_ioctl, |
Arnd Bergmann | 232ec98 | 2021-07-27 15:44:54 +0200 | [diff] [blame] | 5419 | .ndo_siocdevprivate = bond_siocdevprivate, |
Jiri Pirko | d03462b | 2011-08-16 03:15:04 +0000 | [diff] [blame] | 5420 | .ndo_change_rx_flags = bond_change_rx_flags, |
Jay Vosburgh | 303d1cb | 2013-05-31 11:57:30 +0000 | [diff] [blame] | 5421 | .ndo_set_rx_mode = bond_set_rx_mode, |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5422 | .ndo_change_mtu = bond_change_mtu, |
Jiri Pirko | cc0e407 | 2011-07-20 04:54:46 +0000 | [diff] [blame] | 5423 | .ndo_set_mac_address = bond_set_mac_address, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 5424 | .ndo_neigh_setup = bond_neigh_setup, |
Jiri Pirko | cc0e407 | 2011-07-20 04:54:46 +0000 | [diff] [blame] | 5425 | .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid, |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5426 | .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid, |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 5427 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Amerigo Wang | 8a8efa2 | 2011-02-17 23:43:32 +0000 | [diff] [blame] | 5428 | .ndo_netpoll_setup = bond_netpoll_setup, |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 5429 | .ndo_netpoll_cleanup = bond_netpoll_cleanup, |
| 5430 | .ndo_poll_controller = bond_poll_controller, |
| 5431 | #endif |
Jiri Pirko | 9232ecc | 2011-02-13 09:33:01 +0000 | [diff] [blame] | 5432 | .ndo_add_slave = bond_enslave, |
| 5433 | .ndo_del_slave = bond_release, |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5434 | .ndo_fix_features = bond_fix_features, |
Toshiaki Makita | 4847f04 | 2015-03-27 14:31:14 +0900 | [diff] [blame] | 5435 | .ndo_features_check = passthru_features_check, |
Maor Gottlieb | 33720aa | 2020-04-30 22:21:39 +0300 | [diff] [blame] | 5436 | .ndo_get_xmit_slave = bond_xmit_get_slave, |
Tariq Toukan | 007feb8 | 2021-01-17 16:59:44 +0200 | [diff] [blame] | 5437 | .ndo_sk_get_lower_dev = bond_sk_get_lower_dev, |
Jussi Maki | 9e2ee5c | 2021-07-31 05:57:34 +0000 | [diff] [blame] | 5438 | .ndo_bpf = bond_xdp, |
| 5439 | .ndo_xdp_xmit = bond_xdp_xmit, |
| 5440 | .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave, |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5441 | }; |
| 5442 | |
Doug Goldstein | b3f92b6 | 2013-02-18 14:59:23 +0000 | [diff] [blame] | 5443 | static const struct device_type bond_type = { |
| 5444 | .name = "bond", |
| 5445 | }; |
| 5446 | |
Amerigo Wang | 9e2e61f | 2010-03-31 21:30:52 +0000 | [diff] [blame] | 5447 | static void bond_destructor(struct net_device *bond_dev) |
| 5448 | { |
| 5449 | struct bonding *bond = netdev_priv(bond_dev); |
Yufeng Mo | 86a5ad0 | 2021-05-20 14:18:32 +0800 | [diff] [blame] | 5450 | |
Amerigo Wang | 9e2e61f | 2010-03-31 21:30:52 +0000 | [diff] [blame] | 5451 | if (bond->wq) |
| 5452 | destroy_workqueue(bond->wq); |
Jussi Maki | 848ca91 | 2021-06-15 08:54:15 +0000 | [diff] [blame] | 5453 | |
| 5454 | if (bond->rr_tx_counter) |
| 5455 | free_percpu(bond->rr_tx_counter); |
Amerigo Wang | 9e2e61f | 2010-03-31 21:30:52 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 5458 | void bond_setup(struct net_device *bond_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 5460 | struct bonding *bond = netdev_priv(bond_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | |
Nikolay Aleksandrov | b743562 | 2014-09-11 22:49:25 +0200 | [diff] [blame] | 5462 | spin_lock_init(&bond->mode_lock); |
Stephen Hemminger | d2991f7 | 2009-06-12 19:02:44 +0000 | [diff] [blame] | 5463 | bond->params = bonding_defaults; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
| 5465 | /* Initialize pointers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | bond->dev = bond_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
| 5468 | /* Initialize the device entry points */ |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5469 | ether_setup(bond_dev); |
WANG Cong | 31c0541 | 2017-03-02 12:24:36 -0800 | [diff] [blame] | 5470 | bond_dev->max_mtu = ETH_MAX_MTU; |
Stephen Hemminger | eb7cc59 | 2008-11-19 21:56:05 -0800 | [diff] [blame] | 5471 | bond_dev->netdev_ops = &bond_netdev_ops; |
Arthur Kepner | 8531c5f | 2005-08-23 01:34:53 -0400 | [diff] [blame] | 5472 | bond_dev->ethtool_ops = &bond_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 5474 | bond_dev->needs_free_netdev = true; |
| 5475 | bond_dev->priv_destructor = bond_destructor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
Doug Goldstein | b3f92b6 | 2013-02-18 14:59:23 +0000 | [diff] [blame] | 5477 | SET_NETDEV_DEVTYPE(bond_dev, &bond_type); |
| 5478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | /* Initialize the device options */ |
Zhang Shengju | 1098cee | 2016-03-16 09:59:15 +0000 | [diff] [blame] | 5480 | bond_dev->flags |= IFF_MASTER; |
Phil Sutter | 1e6f20c | 2015-08-18 10:30:39 +0200 | [diff] [blame] | 5481 | bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE; |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 5482 | bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5483 | |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 5484 | #ifdef CONFIG_XFRM_OFFLOAD |
| 5485 | /* set up xfrm device ops (only supported in active-backup right now) */ |
Jarod Wilson | a3b658c | 2020-06-30 14:49:41 -0400 | [diff] [blame] | 5486 | bond_dev->xfrmdev_ops = &bond_xfrmdev_ops; |
Taehee Yoo | 9a56055 | 2021-07-05 15:38:12 +0000 | [diff] [blame] | 5487 | INIT_LIST_HEAD(&bond->ipsec_list); |
| 5488 | spin_lock_init(&bond->ipsec_lock); |
Jarod Wilson | 18cb261 | 2020-06-19 10:31:55 -0400 | [diff] [blame] | 5489 | #endif /* CONFIG_XFRM_OFFLOAD */ |
| 5490 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5491 | /* don't acquire bond device's netif_tx_lock when transmitting */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | bond_dev->features |= NETIF_F_LLTX; |
| 5493 | |
| 5494 | /* By default, we declare the bond to be fully |
| 5495 | * VLAN hardware accelerated capable. Special |
| 5496 | * care is taken in the various xmit functions |
| 5497 | * when there are slaves that are not hw accel |
| 5498 | * capable |
| 5499 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | |
Weilong Chen | f939981 | 2014-01-22 17:16:30 +0800 | [diff] [blame] | 5501 | /* Don't allow bond devices to change network namespaces. */ |
| 5502 | bond_dev->features |= NETIF_F_NETNS_LOCAL; |
| 5503 | |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5504 | bond_dev->hw_features = BOND_VLAN_FEATURES | |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 5505 | NETIF_F_HW_VLAN_CTAG_RX | |
| 5506 | NETIF_F_HW_VLAN_CTAG_FILTER; |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5507 | |
Alexander Lobakin | ecb8fed | 2020-11-01 13:17:17 +0000 | [diff] [blame] | 5508 | bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; |
Michał Mirosław | b2a103e | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5509 | bond_dev->features |= bond_dev->hw_features; |
YueHaibing | 30d8177 | 2019-06-26 16:08:44 +0800 | [diff] [blame] | 5510 | bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
Jarod Wilson | a3b658c | 2020-06-30 14:49:41 -0400 | [diff] [blame] | 5511 | #ifdef CONFIG_XFRM_OFFLOAD |
Jarod Wilson | d241b38 | 2020-12-05 12:40:03 -0500 | [diff] [blame] | 5512 | bond_dev->hw_features |= BOND_XFRM_FEATURES; |
| 5513 | /* Only enable XFRM features if this is an active-backup config */ |
| 5514 | if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) |
| 5515 | bond_dev->features |= BOND_XFRM_FEATURES; |
Jarod Wilson | a3b658c | 2020-06-30 14:49:41 -0400 | [diff] [blame] | 5516 | #endif /* CONFIG_XFRM_OFFLOAD */ |
Tariq Toukan | 89df6a8 | 2021-01-17 16:59:46 +0200 | [diff] [blame] | 5517 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
| 5518 | if (bond_sk_check(bond)) |
| 5519 | bond_dev->features |= BOND_TLS_FEATURES; |
| 5520 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | } |
| 5522 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5523 | /* Destroy a bonding device. |
| 5524 | * Must be under rtnl_lock when this function is called. |
| 5525 | */ |
Eric W. Biederman | c67dfb2 | 2009-10-29 14:18:24 +0000 | [diff] [blame] | 5526 | static void bond_uninit(struct net_device *bond_dev) |
Jay Vosburgh | a434e43 | 2008-10-30 17:41:15 -0700 | [diff] [blame] | 5527 | { |
Wang Chen | 454d7c9 | 2008-11-12 23:37:49 -0800 | [diff] [blame] | 5528 | struct bonding *bond = netdev_priv(bond_dev); |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 5529 | struct bond_up_slave *usable, *all; |
Veaceslav Falico | 544a028 | 2013-09-25 09:20:15 +0200 | [diff] [blame] | 5530 | struct list_head *iter; |
| 5531 | struct slave *slave; |
Jay Vosburgh | a434e43 | 2008-10-30 17:41:15 -0700 | [diff] [blame] | 5532 | |
WANG Cong | f6dc31a | 2010-05-06 00:48:51 -0700 | [diff] [blame] | 5533 | bond_netpoll_cleanup(bond_dev); |
| 5534 | |
Eric W. Biederman | c67dfb2 | 2009-10-29 14:18:24 +0000 | [diff] [blame] | 5535 | /* Release the bonded slaves */ |
Veaceslav Falico | 544a028 | 2013-09-25 09:20:15 +0200 | [diff] [blame] | 5536 | bond_for_each_slave(bond, slave, iter) |
WANG Cong | f51048c | 2017-07-06 15:01:57 -0700 | [diff] [blame] | 5537 | __bond_release_one(bond_dev, slave->dev, true, true); |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 5538 | netdev_info(bond_dev, "Released all slaves\n"); |
Eric W. Biederman | c67dfb2 | 2009-10-29 14:18:24 +0000 | [diff] [blame] | 5539 | |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 5540 | usable = rtnl_dereference(bond->usable_slaves); |
| 5541 | if (usable) { |
Maor Gottlieb | ed7d4f0 | 2020-04-30 22:21:33 +0300 | [diff] [blame] | 5542 | RCU_INIT_POINTER(bond->usable_slaves, NULL); |
Maor Gottlieb | 6b447e7 | 2020-04-30 22:21:38 +0300 | [diff] [blame] | 5543 | kfree_rcu(usable, rcu); |
| 5544 | } |
| 5545 | |
| 5546 | all = rtnl_dereference(bond->all_slaves); |
| 5547 | if (all) { |
| 5548 | RCU_INIT_POINTER(bond->all_slaves, NULL); |
| 5549 | kfree_rcu(all, rcu); |
Mahesh Bandewar | ee63771 | 2014-10-04 17:45:01 -0700 | [diff] [blame] | 5550 | } |
| 5551 | |
Jay Vosburgh | a434e43 | 2008-10-30 17:41:15 -0700 | [diff] [blame] | 5552 | list_del(&bond->bond_list); |
| 5553 | |
Taku Izumi | f073c7c | 2010-12-09 15:17:13 +0000 | [diff] [blame] | 5554 | bond_debug_unregister(bond); |
Jay Vosburgh | a434e43 | 2008-10-30 17:41:15 -0700 | [diff] [blame] | 5555 | } |
| 5556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | /*------------------------- Module initialization ---------------------------*/ |
| 5558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | static int bond_check_params(struct bond_params *params) |
| 5560 | { |
nikolay@redhat.com | 5a5c5fd | 2013-05-18 01:18:30 +0000 | [diff] [blame] | 5561 | int arp_validate_value, fail_over_mac_value, primary_reselect_value, i; |
stephen hemminger | f325333 | 2014-03-04 16:36:44 -0800 | [diff] [blame] | 5562 | struct bond_opt_value newval; |
| 5563 | const struct bond_opt_value *valptr; |
Jarod Wilson | 72ccc47 | 2017-05-19 14:46:46 -0400 | [diff] [blame] | 5564 | int arp_all_targets_value = 0; |
Mahesh Bandewar | 6791e46 | 2015-05-09 00:01:55 -0700 | [diff] [blame] | 5565 | u16 ad_actor_sys_prio = 0; |
Mahesh Bandewar | d22a5fc | 2015-05-09 00:01:57 -0700 | [diff] [blame] | 5566 | u16 ad_user_port_key = 0; |
Jarod Wilson | 72ccc47 | 2017-05-19 14:46:46 -0400 | [diff] [blame] | 5567 | __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 }; |
Mahesh Bandewar | dc9c4d0 | 2017-03-08 10:56:02 -0800 | [diff] [blame] | 5568 | int arp_ip_count; |
| 5569 | int bond_mode = BOND_MODE_ROUNDROBIN; |
| 5570 | int xmit_hashtype = BOND_XMIT_POLICY_LAYER2; |
| 5571 | int lacp_fast = 0; |
Nikolay Aleksandrov | f13ad10 | 2017-09-12 15:10:05 +0300 | [diff] [blame] | 5572 | int tlb_dynamic_lb; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5573 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5574 | /* Convert string parameters. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | if (mode) { |
Nikolay Aleksandrov | 2b3798d | 2014-01-22 14:53:17 +0100 | [diff] [blame] | 5576 | bond_opt_initstr(&newval, mode); |
| 5577 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval); |
| 5578 | if (!valptr) { |
| 5579 | pr_err("Error: Invalid bonding mode \"%s\"\n", mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | return -EINVAL; |
| 5581 | } |
Nikolay Aleksandrov | 2b3798d | 2014-01-22 14:53:17 +0100 | [diff] [blame] | 5582 | bond_mode = valptr->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | } |
| 5584 | |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 5585 | if (xmit_hash_policy) { |
Debabrata Banerjee | e79c105 | 2018-05-14 14:48:09 -0400 | [diff] [blame] | 5586 | if (bond_mode == BOND_MODE_ROUNDROBIN || |
| 5587 | bond_mode == BOND_MODE_ACTIVEBACKUP || |
| 5588 | bond_mode == BOND_MODE_BROADCAST) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5589 | pr_info("xmit_hash_policy param is irrelevant in mode %s\n", |
Joe Perches | 9019426 | 2014-02-15 16:01:45 -0800 | [diff] [blame] | 5590 | bond_mode_name(bond_mode)); |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 5591 | } else { |
Nikolay Aleksandrov | a4b32ce | 2014-01-22 14:53:19 +0100 | [diff] [blame] | 5592 | bond_opt_initstr(&newval, xmit_hash_policy); |
| 5593 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH), |
| 5594 | &newval); |
| 5595 | if (!valptr) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5596 | pr_err("Error: Invalid xmit_hash_policy \"%s\"\n", |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 5597 | xmit_hash_policy); |
| 5598 | return -EINVAL; |
| 5599 | } |
Nikolay Aleksandrov | a4b32ce | 2014-01-22 14:53:19 +0100 | [diff] [blame] | 5600 | xmit_hashtype = valptr->value; |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 5601 | } |
| 5602 | } |
| 5603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | if (lacp_rate) { |
| 5605 | if (bond_mode != BOND_MODE_8023AD) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5606 | pr_info("lacp_rate param is irrelevant in mode %s\n", |
| 5607 | bond_mode_name(bond_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | } else { |
Nikolay Aleksandrov | d3131de | 2014-01-22 14:53:27 +0100 | [diff] [blame] | 5609 | bond_opt_initstr(&newval, lacp_rate); |
| 5610 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE), |
| 5611 | &newval); |
| 5612 | if (!valptr) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5613 | pr_err("Error: Invalid lacp rate \"%s\"\n", |
Nikolay Aleksandrov | d3131de | 2014-01-22 14:53:27 +0100 | [diff] [blame] | 5614 | lacp_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | return -EINVAL; |
| 5616 | } |
Nikolay Aleksandrov | d3131de | 2014-01-22 14:53:27 +0100 | [diff] [blame] | 5617 | lacp_fast = valptr->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | } |
| 5619 | } |
| 5620 | |
Jay Vosburgh | fd989c8 | 2008-11-04 17:51:16 -0800 | [diff] [blame] | 5621 | if (ad_select) { |
Nikolay Aleksandrov | 548d28b | 2014-07-13 09:47:47 +0200 | [diff] [blame] | 5622 | bond_opt_initstr(&newval, ad_select); |
Nikolay Aleksandrov | 9e5f5ee | 2014-01-22 14:53:29 +0100 | [diff] [blame] | 5623 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT), |
| 5624 | &newval); |
| 5625 | if (!valptr) { |
| 5626 | pr_err("Error: Invalid ad_select \"%s\"\n", ad_select); |
Jay Vosburgh | fd989c8 | 2008-11-04 17:51:16 -0800 | [diff] [blame] | 5627 | return -EINVAL; |
| 5628 | } |
Nikolay Aleksandrov | 9e5f5ee | 2014-01-22 14:53:29 +0100 | [diff] [blame] | 5629 | params->ad_select = valptr->value; |
| 5630 | if (bond_mode != BOND_MODE_8023AD) |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5631 | pr_warn("ad_select param only affects 802.3ad mode\n"); |
Jay Vosburgh | fd989c8 | 2008-11-04 17:51:16 -0800 | [diff] [blame] | 5632 | } else { |
| 5633 | params->ad_select = BOND_AD_STABLE; |
| 5634 | } |
| 5635 | |
Nicolas de Pesloüan | f584130 | 2009-08-28 13:18:34 +0000 | [diff] [blame] | 5636 | if (max_bonds < 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5637 | pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n", |
| 5638 | max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | max_bonds = BOND_DEFAULT_MAX_BONDS; |
| 5640 | } |
| 5641 | |
| 5642 | if (miimon < 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5643 | pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n", |
| 5644 | miimon, INT_MAX); |
Nikolay Aleksandrov | b98d9c6 | 2014-01-22 14:53:31 +0100 | [diff] [blame] | 5645 | miimon = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | if (updelay < 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5649 | pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", |
| 5650 | updelay, INT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | updelay = 0; |
| 5652 | } |
| 5653 | |
| 5654 | if (downdelay < 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5655 | pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", |
| 5656 | downdelay, INT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | downdelay = 0; |
| 5658 | } |
| 5659 | |
Debabrata Banerjee | b3c898e | 2018-05-16 14:02:13 -0400 | [diff] [blame] | 5660 | if ((use_carrier != 0) && (use_carrier != 1)) { |
| 5661 | pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n", |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5662 | use_carrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | use_carrier = 1; |
| 5664 | } |
| 5665 | |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 5666 | if (num_peer_notif < 0 || num_peer_notif > 255) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5667 | pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n", |
| 5668 | num_peer_notif); |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 5669 | num_peer_notif = 1; |
| 5670 | } |
| 5671 | |
dingtianhong | 834db4b | 2013-12-21 14:40:17 +0800 | [diff] [blame] | 5672 | /* reset values for 802.3ad/TLB/ALB */ |
Veaceslav Falico | 267bed7 | 2014-05-15 21:39:53 +0200 | [diff] [blame] | 5673 | if (!bond_mode_uses_arp(bond_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | if (!miimon) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5675 | 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"); |
| 5676 | pr_warn("Forcing miimon to 100msec\n"); |
dingtianhong | fe9d04a | 2013-11-22 22:28:43 +0800 | [diff] [blame] | 5677 | miimon = BOND_DEFAULT_MIIMON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | } |
| 5679 | } |
| 5680 | |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 5681 | if (tx_queues < 1 || tx_queues > 255) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5682 | pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n", |
| 5683 | tx_queues, BOND_DEFAULT_TX_QUEUES); |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 5684 | tx_queues = BOND_DEFAULT_TX_QUEUES; |
| 5685 | } |
| 5686 | |
Andy Gospodarek | ebd8e49 | 2010-06-02 08:39:21 +0000 | [diff] [blame] | 5687 | if ((all_slaves_active != 0) && (all_slaves_active != 1)) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5688 | pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n", |
| 5689 | all_slaves_active); |
Andy Gospodarek | ebd8e49 | 2010-06-02 08:39:21 +0000 | [diff] [blame] | 5690 | all_slaves_active = 0; |
| 5691 | } |
| 5692 | |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 5693 | if (resend_igmp < 0 || resend_igmp > 255) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5694 | pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n", |
| 5695 | resend_igmp, BOND_DEFAULT_RESEND_IGMP); |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 5696 | resend_igmp = BOND_DEFAULT_RESEND_IGMP; |
| 5697 | } |
| 5698 | |
Nikolay Aleksandrov | aa59d85 | 2014-01-22 14:53:18 +0100 | [diff] [blame] | 5699 | bond_opt_initval(&newval, packets_per_slave); |
| 5700 | if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) { |
Nikolay Aleksandrov | 7395832 | 2013-11-05 13:51:41 +0100 | [diff] [blame] | 5701 | pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n", |
| 5702 | packets_per_slave, USHRT_MAX); |
| 5703 | packets_per_slave = 1; |
| 5704 | } |
| 5705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | if (bond_mode == BOND_MODE_ALB) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5707 | 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", |
| 5708 | updelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | if (!miimon) { |
| 5712 | if (updelay || downdelay) { |
| 5713 | /* just warn the user the up/down delay will have |
| 5714 | * no effect since miimon is zero... |
| 5715 | */ |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5716 | 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", |
| 5717 | updelay, downdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | } |
| 5719 | } else { |
| 5720 | /* don't allow arp monitoring */ |
| 5721 | if (arp_interval) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5722 | pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n", |
| 5723 | miimon, arp_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | arp_interval = 0; |
| 5725 | } |
| 5726 | |
| 5727 | if ((updelay % miimon) != 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5728 | pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", |
| 5729 | updelay, miimon, (updelay / miimon) * miimon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | updelay /= miimon; |
| 5733 | |
| 5734 | if ((downdelay % miimon) != 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5735 | pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n", |
| 5736 | downdelay, miimon, |
| 5737 | (downdelay / miimon) * miimon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | downdelay /= miimon; |
| 5741 | } |
| 5742 | |
| 5743 | if (arp_interval < 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5744 | pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n", |
| 5745 | arp_interval, INT_MAX); |
Nikolay Aleksandrov | 7bdb04e | 2014-01-22 14:53:23 +0100 | [diff] [blame] | 5746 | arp_interval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
nikolay@redhat.com | 5a5c5fd | 2013-05-18 01:18:30 +0000 | [diff] [blame] | 5749 | for (arp_ip_count = 0, i = 0; |
| 5750 | (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) { |
dingtianhong | 89015c1 | 2013-12-04 18:59:31 +0800 | [diff] [blame] | 5751 | __be32 ip; |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5752 | |
| 5753 | /* not a complete check, but good enough to catch mistakes */ |
dingtianhong | 89015c1 | 2013-12-04 18:59:31 +0800 | [diff] [blame] | 5754 | if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) || |
Veaceslav Falico | 2807a9f | 2014-05-15 21:39:56 +0200 | [diff] [blame] | 5755 | !bond_is_ip_target_ok(ip)) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5756 | pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n", |
| 5757 | arp_ip_target[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | arp_interval = 0; |
| 5759 | } else { |
Veaceslav Falico | 0afee4e | 2013-06-24 11:49:30 +0200 | [diff] [blame] | 5760 | if (bond_get_targets_ip(arp_target, ip) == -1) |
| 5761 | arp_target[arp_ip_count++] = ip; |
| 5762 | else |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5763 | pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n", |
| 5764 | &ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | if (arp_interval && !arp_ip_count) { |
| 5769 | /* don't allow arping if no arp_ip_target given... */ |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5770 | pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n", |
| 5771 | arp_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | arp_interval = 0; |
| 5773 | } |
| 5774 | |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5775 | if (arp_validate) { |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5776 | if (!arp_interval) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5777 | pr_err("arp_validate requires arp_interval\n"); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5778 | return -EINVAL; |
| 5779 | } |
| 5780 | |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5781 | bond_opt_initstr(&newval, arp_validate); |
| 5782 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE), |
| 5783 | &newval); |
| 5784 | if (!valptr) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5785 | pr_err("Error: invalid arp_validate \"%s\"\n", |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5786 | arp_validate); |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5787 | return -EINVAL; |
| 5788 | } |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5789 | arp_validate_value = valptr->value; |
| 5790 | } else { |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5791 | arp_validate_value = 0; |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5792 | } |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5793 | |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 5794 | if (arp_all_targets) { |
Nikolay Aleksandrov | edf36b2 | 2014-01-22 14:53:21 +0100 | [diff] [blame] | 5795 | bond_opt_initstr(&newval, arp_all_targets); |
| 5796 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS), |
| 5797 | &newval); |
| 5798 | if (!valptr) { |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 5799 | pr_err("Error: invalid arp_all_targets_value \"%s\"\n", |
| 5800 | arp_all_targets); |
| 5801 | arp_all_targets_value = 0; |
Nikolay Aleksandrov | edf36b2 | 2014-01-22 14:53:21 +0100 | [diff] [blame] | 5802 | } else { |
| 5803 | arp_all_targets_value = valptr->value; |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 5804 | } |
| 5805 | } |
| 5806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | if (miimon) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5808 | pr_info("MII link monitoring set to %d ms\n", miimon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | } else if (arp_interval) { |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5810 | valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE, |
| 5811 | arp_validate_value); |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5812 | pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):", |
Nikolay Aleksandrov | 1622888 | 2014-01-22 14:53:20 +0100 | [diff] [blame] | 5813 | arp_interval, valptr->string, arp_ip_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | |
| 5815 | for (i = 0; i < arp_ip_count; i++) |
Joe Perches | 9019426 | 2014-02-15 16:01:45 -0800 | [diff] [blame] | 5816 | pr_cont(" %s", arp_ip_target[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
Joe Perches | 9019426 | 2014-02-15 16:01:45 -0800 | [diff] [blame] | 5818 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | |
Jay Vosburgh | b8a9787 | 2008-06-13 18:12:04 -0700 | [diff] [blame] | 5820 | } else if (max_bonds) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | /* miimon and arp_interval not set, we need one so things |
| 5822 | * work as expected, see bonding.txt for details |
| 5823 | */ |
Joe Perches | 9019426 | 2014-02-15 16:01:45 -0800 | [diff] [blame] | 5824 | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | } |
| 5826 | |
Veaceslav Falico | ec0865a | 2014-05-15 21:39:54 +0200 | [diff] [blame] | 5827 | if (primary && !bond_mode_uses_primary(bond_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | /* currently, using a primary only makes sense |
| 5829 | * in active backup, TLB or ALB modes |
| 5830 | */ |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5831 | pr_warn("Warning: %s primary device specified but has no effect in %s mode\n", |
| 5832 | primary, bond_mode_name(bond_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | primary = NULL; |
| 5834 | } |
| 5835 | |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 5836 | if (primary && primary_reselect) { |
Nikolay Aleksandrov | 388d3a6 | 2014-01-22 14:53:33 +0100 | [diff] [blame] | 5837 | bond_opt_initstr(&newval, primary_reselect); |
| 5838 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT), |
| 5839 | &newval); |
| 5840 | if (!valptr) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5841 | pr_err("Error: Invalid primary_reselect \"%s\"\n", |
Nikolay Aleksandrov | 388d3a6 | 2014-01-22 14:53:33 +0100 | [diff] [blame] | 5842 | primary_reselect); |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 5843 | return -EINVAL; |
| 5844 | } |
Nikolay Aleksandrov | 388d3a6 | 2014-01-22 14:53:33 +0100 | [diff] [blame] | 5845 | primary_reselect_value = valptr->value; |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 5846 | } else { |
| 5847 | primary_reselect_value = BOND_PRI_RESELECT_ALWAYS; |
| 5848 | } |
| 5849 | |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 5850 | if (fail_over_mac) { |
Nikolay Aleksandrov | 1df6b6a | 2014-01-22 14:53:22 +0100 | [diff] [blame] | 5851 | bond_opt_initstr(&newval, fail_over_mac); |
| 5852 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC), |
| 5853 | &newval); |
| 5854 | if (!valptr) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 5855 | pr_err("Error: invalid fail_over_mac \"%s\"\n", |
Nikolay Aleksandrov | 1df6b6a | 2014-01-22 14:53:22 +0100 | [diff] [blame] | 5856 | fail_over_mac); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 5857 | return -EINVAL; |
| 5858 | } |
Nikolay Aleksandrov | 1df6b6a | 2014-01-22 14:53:22 +0100 | [diff] [blame] | 5859 | fail_over_mac_value = valptr->value; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 5860 | if (bond_mode != BOND_MODE_ACTIVEBACKUP) |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5861 | pr_warn("Warning: fail_over_mac only affects active-backup mode\n"); |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 5862 | } else { |
| 5863 | fail_over_mac_value = BOND_FOM_NONE; |
| 5864 | } |
Jay Vosburgh | dd957c5 | 2007-10-09 19:57:24 -0700 | [diff] [blame] | 5865 | |
Mahesh Bandewar | 6791e46 | 2015-05-09 00:01:55 -0700 | [diff] [blame] | 5866 | bond_opt_initstr(&newval, "default"); |
| 5867 | valptr = bond_opt_parse( |
| 5868 | bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO), |
| 5869 | &newval); |
| 5870 | if (!valptr) { |
| 5871 | pr_err("Error: No ad_actor_sys_prio default value"); |
| 5872 | return -EINVAL; |
| 5873 | } |
| 5874 | ad_actor_sys_prio = valptr->value; |
| 5875 | |
Mahesh Bandewar | d22a5fc | 2015-05-09 00:01:57 -0700 | [diff] [blame] | 5876 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY), |
| 5877 | &newval); |
| 5878 | if (!valptr) { |
| 5879 | pr_err("Error: No ad_user_port_key default value"); |
| 5880 | return -EINVAL; |
| 5881 | } |
| 5882 | ad_user_port_key = valptr->value; |
| 5883 | |
Nikolay Aleksandrov | f13ad10 | 2017-09-12 15:10:05 +0300 | [diff] [blame] | 5884 | bond_opt_initstr(&newval, "default"); |
| 5885 | valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval); |
| 5886 | if (!valptr) { |
| 5887 | pr_err("Error: No tlb_dynamic_lb default value"); |
| 5888 | return -EINVAL; |
Mahesh Bandewar | 8b426dc | 2017-03-08 10:55:56 -0800 | [diff] [blame] | 5889 | } |
Nikolay Aleksandrov | f13ad10 | 2017-09-12 15:10:05 +0300 | [diff] [blame] | 5890 | tlb_dynamic_lb = valptr->value; |
Mahesh Bandewar | 8b426dc | 2017-03-08 10:55:56 -0800 | [diff] [blame] | 5891 | |
dingtianhong | 3a7129e | 2013-12-21 14:40:12 +0800 | [diff] [blame] | 5892 | if (lp_interval == 0) { |
Joe Perches | 91565eb | 2014-02-15 15:57:04 -0800 | [diff] [blame] | 5893 | pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n", |
| 5894 | INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL); |
dingtianhong | 3a7129e | 2013-12-21 14:40:12 +0800 | [diff] [blame] | 5895 | lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; |
| 5896 | } |
| 5897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | /* fill params struct with the proper values */ |
| 5899 | params->mode = bond_mode; |
Jay Vosburgh | 169a3e6 | 2005-06-26 17:54:11 -0400 | [diff] [blame] | 5900 | params->xmit_policy = xmit_hashtype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | params->miimon = miimon; |
Ben Hutchings | ad246c9 | 2011-04-26 15:25:52 +0000 | [diff] [blame] | 5902 | params->num_peer_notif = num_peer_notif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | params->arp_interval = arp_interval; |
Jay Vosburgh | f5b2b96 | 2006-09-22 21:54:53 -0700 | [diff] [blame] | 5904 | params->arp_validate = arp_validate_value; |
Veaceslav Falico | 8599b52 | 2013-06-24 11:49:34 +0200 | [diff] [blame] | 5905 | params->arp_all_targets = arp_all_targets_value; |
Hangbin Liu | 5944b5a | 2021-11-30 12:29:47 +0800 | [diff] [blame] | 5906 | params->missed_max = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | params->updelay = updelay; |
| 5908 | params->downdelay = downdelay; |
Vincent Bernat | 07a4dde | 2019-07-02 19:43:54 +0200 | [diff] [blame] | 5909 | params->peer_notif_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | params->use_carrier = use_carrier; |
Hangbin Liu | 3a755cd | 2021-08-02 11:02:19 +0800 | [diff] [blame] | 5911 | params->lacp_active = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | params->lacp_fast = lacp_fast; |
| 5913 | params->primary[0] = 0; |
Jiri Pirko | a549952 | 2009-09-25 03:28:09 +0000 | [diff] [blame] | 5914 | params->primary_reselect = primary_reselect_value; |
Jay Vosburgh | 3915c1e8 | 2008-05-17 21:10:14 -0700 | [diff] [blame] | 5915 | params->fail_over_mac = fail_over_mac_value; |
Andy Gospodarek | bb1d912 | 2010-06-02 08:40:18 +0000 | [diff] [blame] | 5916 | params->tx_queues = tx_queues; |
Andy Gospodarek | ebd8e49 | 2010-06-02 08:39:21 +0000 | [diff] [blame] | 5917 | params->all_slaves_active = all_slaves_active; |
Flavio Leitner | c2952c3 | 2010-10-05 14:23:59 +0000 | [diff] [blame] | 5918 | params->resend_igmp = resend_igmp; |
stephen hemminger | 655f891 | 2011-06-22 09:54:39 +0000 | [diff] [blame] | 5919 | params->min_links = min_links; |
dingtianhong | 3a7129e | 2013-12-21 14:40:12 +0800 | [diff] [blame] | 5920 | params->lp_interval = lp_interval; |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 5921 | params->packets_per_slave = packets_per_slave; |
Mahesh Bandewar | 8b426dc | 2017-03-08 10:55:56 -0800 | [diff] [blame] | 5922 | params->tlb_dynamic_lb = tlb_dynamic_lb; |
Mahesh Bandewar | 6791e46 | 2015-05-09 00:01:55 -0700 | [diff] [blame] | 5923 | params->ad_actor_sys_prio = ad_actor_sys_prio; |
Mahesh Bandewar | 7451495 | 2015-05-09 00:01:56 -0700 | [diff] [blame] | 5924 | eth_zero_addr(params->ad_actor_system); |
Mahesh Bandewar | d22a5fc | 2015-05-09 00:01:57 -0700 | [diff] [blame] | 5925 | params->ad_user_port_key = ad_user_port_key; |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 5926 | if (packets_per_slave > 0) { |
| 5927 | params->reciprocal_packets_per_slave = |
| 5928 | reciprocal_value(packets_per_slave); |
| 5929 | } else { |
| 5930 | /* reciprocal_packets_per_slave is unused if |
| 5931 | * packets_per_slave is 0 or 1, just initialize it |
| 5932 | */ |
| 5933 | params->reciprocal_packets_per_slave = |
| 5934 | (struct reciprocal_value) { 0 }; |
| 5935 | } |
| 5936 | |
Kees Cook | 4390207 | 2021-06-02 13:58:20 -0700 | [diff] [blame] | 5937 | if (primary) |
| 5938 | strscpy_pad(params->primary, primary, sizeof(params->primary)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | |
| 5940 | memcpy(params->arp_targets, arp_target, sizeof(arp_target)); |
| 5941 | |
| 5942 | return 0; |
| 5943 | } |
| 5944 | |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 5945 | /* Called from registration process */ |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5946 | static int bond_init(struct net_device *bond_dev) |
| 5947 | { |
| 5948 | struct bonding *bond = netdev_priv(bond_dev); |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 5949 | struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5950 | |
Veaceslav Falico | 76444f5 | 2014-07-15 19:35:58 +0200 | [diff] [blame] | 5951 | netdev_dbg(bond_dev, "Begin bond_init\n"); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5952 | |
Bhaktipriya Shridhar | f9f225e | 2016-08-30 22:02:01 +0530 | [diff] [blame] | 5953 | bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5954 | if (!bond->wq) |
| 5955 | return -ENOMEM; |
| 5956 | |
Jussi Maki | 848ca91 | 2021-06-15 08:54:15 +0000 | [diff] [blame] | 5957 | if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN) { |
| 5958 | bond->rr_tx_counter = alloc_percpu(u32); |
| 5959 | if (!bond->rr_tx_counter) { |
| 5960 | destroy_workqueue(bond->wq); |
| 5961 | bond->wq = NULL; |
| 5962 | return -ENOMEM; |
| 5963 | } |
| 5964 | } |
| 5965 | |
Taehee Yoo | 089bca2 | 2019-10-21 18:47:53 +0000 | [diff] [blame] | 5966 | spin_lock_init(&bond->stats_lock); |
Cong Wang | 1a33e10 | 2020-05-02 22:22:19 -0700 | [diff] [blame] | 5967 | netdev_lockdep_set_classes(bond_dev); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5968 | |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 5969 | list_add_tail(&bond->bond_list, &bn->dev_list); |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5970 | |
Eric W. Biederman | 6151b3d | 2009-10-29 14:18:22 +0000 | [diff] [blame] | 5971 | bond_prepare_sysfs_group(bond); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 5972 | |
Taku Izumi | f073c7c | 2010-12-09 15:17:13 +0000 | [diff] [blame] | 5973 | bond_debug_register(bond); |
| 5974 | |
Jiri Pirko | 409cc1f | 2013-01-30 11:08:11 +0100 | [diff] [blame] | 5975 | /* Ensure valid dev_addr */ |
| 5976 | if (is_zero_ether_addr(bond_dev->dev_addr) && |
nikolay@redhat.com | 97a1e63 | 2013-06-26 17:13:38 +0200 | [diff] [blame] | 5977 | bond_dev->addr_assign_type == NET_ADDR_PERM) |
Jiri Pirko | 409cc1f | 2013-01-30 11:08:11 +0100 | [diff] [blame] | 5978 | eth_hw_addr_random(bond_dev); |
Jiri Pirko | 409cc1f | 2013-01-30 11:08:11 +0100 | [diff] [blame] | 5979 | |
Stephen Hemminger | 181470f | 2009-06-12 19:02:52 +0000 | [diff] [blame] | 5980 | return 0; |
| 5981 | } |
| 5982 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 5983 | unsigned int bond_get_num_tx_queues(void) |
Jiri Pirko | d5da451 | 2011-08-10 06:09:44 +0000 | [diff] [blame] | 5984 | { |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 5985 | return tx_queues; |
Jiri Pirko | d5da451 | 2011-08-10 06:09:44 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 5988 | /* Create a new bond based on the specified name and bonding parameters. |
Jay Vosburgh | e4b91c4 | 2007-01-19 18:15:31 -0800 | [diff] [blame] | 5989 | * If name is NULL, obtain a suitable "bond%d" name for us. |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 5990 | * Caller must NOT hold rtnl_lock; we need to release it here before we |
| 5991 | * set up our sysfs entries. |
| 5992 | */ |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 5993 | int bond_create(struct net *net, const char *name) |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 5994 | { |
| 5995 | struct net_device *bond_dev; |
Pai | e913fb2 | 2015-04-29 14:24:23 -0400 | [diff] [blame] | 5996 | struct bonding *bond; |
| 5997 | struct alb_bond_info *bond_info; |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 5998 | int res; |
| 5999 | |
| 6000 | rtnl_lock(); |
Jay Vosburgh | 027ea04 | 2008-01-17 16:25:02 -0800 | [diff] [blame] | 6001 | |
Jiri Pirko | 1c5cae8 | 2011-04-30 01:21:32 +0000 | [diff] [blame] | 6002 | bond_dev = alloc_netdev_mq(sizeof(struct bonding), |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 6003 | name ? name : "bond%d", NET_NAME_UNKNOWN, |
Jiri Pirko | 1c5cae8 | 2011-04-30 01:21:32 +0000 | [diff] [blame] | 6004 | bond_setup, tx_queues); |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6005 | if (!bond_dev) { |
Joe Perches | a4aee5c | 2009-12-13 20:06:07 -0800 | [diff] [blame] | 6006 | pr_err("%s: eek! can't alloc netdev!\n", name); |
Amerigo Wang | 9e2e61f | 2010-03-31 21:30:52 +0000 | [diff] [blame] | 6007 | rtnl_unlock(); |
| 6008 | return -ENOMEM; |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6009 | } |
| 6010 | |
Pai | e913fb2 | 2015-04-29 14:24:23 -0400 | [diff] [blame] | 6011 | /* |
| 6012 | * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX. |
| 6013 | * It is set to 0 by default which is wrong. |
| 6014 | */ |
| 6015 | bond = netdev_priv(bond_dev); |
| 6016 | bond_info = &(BOND_ALB_INFO(bond)); |
| 6017 | bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; |
| 6018 | |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6019 | dev_net_set(bond_dev, net); |
Eric W. Biederman | 88ead97 | 2009-10-29 14:18:25 +0000 | [diff] [blame] | 6020 | bond_dev->rtnl_link_ops = &bond_link_ops; |
| 6021 | |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6022 | res = register_netdevice(bond_dev); |
Taehee Yoo | 544f287 | 2020-07-19 12:11:24 +0000 | [diff] [blame] | 6023 | if (res < 0) { |
| 6024 | free_netdev(bond_dev); |
| 6025 | rtnl_unlock(); |
| 6026 | |
| 6027 | return res; |
| 6028 | } |
Peter Zijlstra | 0daa2303 | 2006-11-08 19:51:01 -0800 | [diff] [blame] | 6029 | |
Phil Oester | e826eaf | 2011-03-14 06:22:05 +0000 | [diff] [blame] | 6030 | netif_carrier_off(bond_dev); |
| 6031 | |
Mahesh Bandewar | 4493b81 | 2017-03-08 10:55:54 -0800 | [diff] [blame] | 6032 | bond_work_init_all(bond); |
| 6033 | |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6034 | rtnl_unlock(); |
Taehee Yoo | 544f287 | 2020-07-19 12:11:24 +0000 | [diff] [blame] | 6035 | return 0; |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6036 | } |
| 6037 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6038 | static int __net_init bond_net_init(struct net *net) |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6039 | { |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6040 | struct bond_net *bn = net_generic(net, bond_net_id); |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6041 | |
| 6042 | bn->net = net; |
| 6043 | INIT_LIST_HEAD(&bn->dev_list); |
| 6044 | |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6045 | bond_create_proc_dir(bn); |
Eric W. Biederman | 4c22400 | 2011-10-12 21:56:25 +0000 | [diff] [blame] | 6046 | bond_create_sysfs(bn); |
Veaceslav Falico | 87a7b84 | 2013-06-24 11:49:29 +0200 | [diff] [blame] | 6047 | |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6048 | return 0; |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6049 | } |
| 6050 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6051 | static void __net_exit bond_net_exit(struct net *net) |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6052 | { |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6053 | struct bond_net *bn = net_generic(net, bond_net_id); |
nikolay@redhat.com | 69b0216 | 2013-04-06 00:54:38 +0000 | [diff] [blame] | 6054 | struct bonding *bond, *tmp_bond; |
| 6055 | LIST_HEAD(list); |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6056 | |
Eric W. Biederman | 4c22400 | 2011-10-12 21:56:25 +0000 | [diff] [blame] | 6057 | bond_destroy_sysfs(bn); |
nikolay@redhat.com | 69b0216 | 2013-04-06 00:54:38 +0000 | [diff] [blame] | 6058 | |
| 6059 | /* Kill off any bonds created after unregistering bond rtnl ops */ |
| 6060 | rtnl_lock(); |
| 6061 | list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list) |
| 6062 | unregister_netdevice_queue(bond->dev, &list); |
| 6063 | unregister_netdevice_many(&list); |
| 6064 | rtnl_unlock(); |
Veaceslav Falico | 23fa5c2 | 2014-07-17 12:04:08 +0200 | [diff] [blame] | 6065 | |
| 6066 | bond_destroy_proc_dir(bn); |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | static struct pernet_operations bond_net_ops = { |
| 6070 | .init = bond_net_init, |
| 6071 | .exit = bond_net_exit, |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6072 | .id = &bond_net_id, |
| 6073 | .size = sizeof(struct bond_net), |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6074 | }; |
| 6075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | static int __init bonding_init(void) |
| 6077 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | int i; |
| 6079 | int res; |
| 6080 | |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6081 | res = bond_check_params(&bonding_defaults); |
Stephen Hemminger | 3d632c3 | 2009-06-12 19:02:48 +0000 | [diff] [blame] | 6082 | if (res) |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6083 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6085 | res = register_pernet_subsys(&bond_net_ops); |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6086 | if (res) |
| 6087 | goto out; |
Jay Vosburgh | 027ea04 | 2008-01-17 16:25:02 -0800 | [diff] [blame] | 6088 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 6089 | res = bond_netlink_init(); |
Eric W. Biederman | 88ead97 | 2009-10-29 14:18:25 +0000 | [diff] [blame] | 6090 | if (res) |
Eric W. Biederman | 6639104 | 2009-10-29 23:58:54 +0000 | [diff] [blame] | 6091 | goto err_link; |
Eric W. Biederman | 88ead97 | 2009-10-29 14:18:25 +0000 | [diff] [blame] | 6092 | |
Taku Izumi | f073c7c | 2010-12-09 15:17:13 +0000 | [diff] [blame] | 6093 | bond_create_debugfs(); |
| 6094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | for (i = 0; i < max_bonds; i++) { |
Eric W. Biederman | ec87fd3 | 2009-10-29 14:18:26 +0000 | [diff] [blame] | 6096 | res = bond_create(&init_net, NULL); |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6097 | if (res) |
| 6098 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | } |
| 6100 | |
Matteo Croce | 58deb77 | 2019-10-29 14:50:53 +0100 | [diff] [blame] | 6101 | skb_flow_dissector_init(&flow_keys_bonding, |
| 6102 | flow_keys_bonding_keys, |
| 6103 | ARRAY_SIZE(flow_keys_bonding_keys)); |
| 6104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | register_netdevice_notifier(&bond_netdev_notifier); |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6106 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | return res; |
Eric W. Biederman | 88ead97 | 2009-10-29 14:18:25 +0000 | [diff] [blame] | 6108 | err: |
Thomas Richter | db29868 | 2014-04-09 12:52:59 +0200 | [diff] [blame] | 6109 | bond_destroy_debugfs(); |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 6110 | bond_netlink_fini(); |
Eric W. Biederman | 6639104 | 2009-10-29 23:58:54 +0000 | [diff] [blame] | 6111 | err_link: |
Eric W. Biederman | 1544974 | 2009-11-29 15:46:04 +0000 | [diff] [blame] | 6112 | unregister_pernet_subsys(&bond_net_ops); |
Eric W. Biederman | 88ead97 | 2009-10-29 14:18:25 +0000 | [diff] [blame] | 6113 | goto out; |
Mitch Williams | dfe6039 | 2005-11-09 10:36:04 -0800 | [diff] [blame] | 6114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | } |
| 6116 | |
| 6117 | static void __exit bonding_exit(void) |
| 6118 | { |
| 6119 | unregister_netdevice_notifier(&bond_netdev_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | |
Taku Izumi | f073c7c | 2010-12-09 15:17:13 +0000 | [diff] [blame] | 6121 | bond_destroy_debugfs(); |
Pavel Emelyanov | ae68c39 | 2008-05-02 17:49:39 -0700 | [diff] [blame] | 6122 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 6123 | bond_netlink_fini(); |
nikolay@redhat.com | ffcdedb | 2013-04-06 00:54:37 +0000 | [diff] [blame] | 6124 | unregister_pernet_subsys(&bond_net_ops); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 6125 | |
| 6126 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Nikolay Aleksandrov | 547942c | 2014-09-15 17:19:34 +0200 | [diff] [blame] | 6127 | /* Make sure we don't have an imbalance on our netpoll blocking */ |
Neil Horman | fb4fa76 | 2010-12-06 09:05:50 +0000 | [diff] [blame] | 6128 | WARN_ON(atomic_read(&netpoll_block_tx)); |
Neil Horman | e843fa5 | 2010-10-13 16:01:50 +0000 | [diff] [blame] | 6129 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | module_init(bonding_init); |
| 6133 | module_exit(bonding_exit); |
| 6134 | MODULE_LICENSE("GPL"); |
Leon Romanovsky | 2b526b5 | 2020-02-24 10:52:54 +0200 | [diff] [blame] | 6135 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others"); |