]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/bond.c
ofproto/bond: Drop traffic in balance-tcp mode without lacp.
[mirror_ovs.git] / ofproto / bond.c
CommitLineData
f620b43a 1/*
07a3cd5c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
f620b43a
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18
19#include "bond.h"
20
21#include <limits.h>
22#include <stdint.h>
23#include <stdlib.h>
75fad143 24#include <math.h>
f620b43a 25
da4a6191 26#include "connectivity.h"
f620b43a 27#include "coverage.h"
b598f214 28#include "dp-packet.h"
f620b43a 29#include "flow.h"
ee89ea7b 30#include "openvswitch/hmap.h"
bdebeece 31#include "lacp.h"
f620b43a
BP
32#include "netdev.h"
33#include "odp-util.h"
b598f214
BW
34#include "ofproto/ofproto-dpif.h"
35#include "ofproto/ofproto-dpif-rid.h"
36#include "ofproto/ofproto-provider.h"
37#include "openvswitch/dynamic-string.h"
38#include "openvswitch/list.h"
39#include "openvswitch/match.h"
40#include "openvswitch/ofp-actions.h"
41#include "openvswitch/ofp-util.h"
64c96779 42#include "openvswitch/ofpbuf.h"
b598f214 43#include "openvswitch/vlog.h"
f620b43a
BP
44#include "packets.h"
45#include "poll-loop.h"
da4a6191 46#include "seq.h"
ee89ea7b 47#include "openvswitch/shash.h"
f620b43a
BP
48#include "timeval.h"
49#include "unixctl.h"
ee89ea7b 50#include "util.h"
f620b43a
BP
51
52VLOG_DEFINE_THIS_MODULE(bond);
53
f1c8a79c
AW
54static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER;
55static struct hmap all_bonds__ = HMAP_INITIALIZER(&all_bonds__);
56static struct hmap *const all_bonds OVS_GUARDED_BY(rwlock) = &all_bonds__;
57
9e1a6910 58/* Bit-mask for hashing a flow down to a bucket. */
f620b43a 59#define BOND_MASK 0xff
9e1a6910 60#define BOND_BUCKETS (BOND_MASK + 1)
f620b43a 61
07a3cd5c
BP
62/* Priority for internal rules created to handle recirculation */
63#define RECIRC_RULE_PRIORITY 20
64
f620b43a 65/* A hash bucket for mapping a flow to a slave.
9e1a6910 66 * "struct bond" has an array of BOND_BUCKETS of these. */
f620b43a
BP
67struct bond_entry {
68 struct bond_slave *slave; /* Assigned slave, NULL if unassigned. */
c6855ec5
JS
69 uint64_t tx_bytes /* Count of bytes recently transmitted. */
70 OVS_GUARDED_BY(rwlock);
ca6ba700 71 struct ovs_list list_node; /* In bond_slave's 'entries' list. */
adcf00ba 72
c6855ec5
JS
73 /* Recirculation.
74 *
75 * 'pr_rule' is the post-recirculation rule for this entry.
76 * 'pr_tx_bytes' is the most recently seen statistics for 'pr_rule', which
77 * is used to determine delta (applied to 'tx_bytes' above.) */
78 struct rule *pr_rule;
79 uint64_t pr_tx_bytes OVS_GUARDED_BY(rwlock);
f620b43a
BP
80};
81
82/* A bond slave, that is, one of the links comprising a bond. */
83struct bond_slave {
84 struct hmap_node hmap_node; /* In struct bond's slaves hmap. */
ca6ba700 85 struct ovs_list list_node; /* In struct bond's enabled_slaves list. */
f620b43a
BP
86 struct bond *bond; /* The bond that contains this slave. */
87 void *aux; /* Client-provided handle for this slave. */
88
89 struct netdev *netdev; /* Network device, owned by the client. */
6422372c 90 uint64_t change_seq; /* Tracks changes in 'netdev'. */
0746a84f 91 ofp_port_t ofp_port; /* OpenFlow port number. */
f620b43a
BP
92 char *name; /* Name (a copy of netdev_get_name(netdev)). */
93
94 /* Link status. */
95 long long delay_expires; /* Time after which 'enabled' may change. */
f620b43a 96 bool enabled; /* May be chosen for flows? */
296f6519 97 bool may_enable; /* Client considers this slave bondable. */
f620b43a
BP
98
99 /* Rebalancing info. Used only by bond_rebalance(). */
ca6ba700
TG
100 struct ovs_list bal_node; /* In bond_rebalance()'s 'bals' list. */
101 struct ovs_list entries; /* 'struct bond_entry's assigned here. */
f620b43a
BP
102 uint64_t tx_bytes; /* Sum across 'tx_bytes' of entries. */
103};
104
105/* A bond, that is, a set of network devices grouped to improve performance or
106 * robustness. */
107struct bond {
108 struct hmap_node hmap_node; /* In 'all_bonds' hmap. */
109 char *name; /* Name provided by client. */
adcf00ba 110 struct ofproto_dpif *ofproto; /* The bridge this bond belongs to. */
f620b43a
BP
111
112 /* Slaves. */
113 struct hmap slaves;
114
f1c8a79c
AW
115 /* Enabled slaves.
116 *
117 * Any reader or writer of 'enabled_slaves' must hold 'mutex'.
118 * (To prevent the bond_slave from disappearing they must also hold
119 * 'rwlock'.) */
120 struct ovs_mutex mutex OVS_ACQ_AFTER(rwlock);
ca6ba700 121 struct ovs_list enabled_slaves OVS_GUARDED; /* Contains struct bond_slaves. */
f1c8a79c 122
f620b43a
BP
123 /* Bonding info. */
124 enum bond_mode balance; /* Balancing mode, one of BM_*. */
125 struct bond_slave *active_slave;
f620b43a 126 int updelay, downdelay; /* Delay before slave goes up/down, in ms. */
bdebeece 127 enum lacp_status lacp_status; /* Status of LACP negotiations. */
62904702 128 bool bond_revalidate; /* True if flows need revalidation. */
672d18b2 129 uint32_t basis; /* Basis for flow hash function. */
f620b43a
BP
130
131 /* SLB specific bonding info. */
9e1a6910 132 struct bond_entry *hash; /* An array of BOND_BUCKETS elements. */
f620b43a
BP
133 int rebalance_interval; /* Interval between rebalances, in ms. */
134 long long int next_rebalance; /* Next rebalancing time. */
135 bool send_learning_packets;
adcf00ba
AZ
136 uint32_t recirc_id; /* Non zero if recirculation can be used.*/
137 struct hmap pr_rule_ops; /* Helps to maintain post recirculation rules.*/
f620b43a 138
3e5aeeb5
AZ
139 /* Store active slave to OVSDB. */
140 bool active_slave_changed; /* Set to true whenever the bond changes
141 active slave. It will be reset to false
142 after it is stored into OVSDB */
143
144 /* Interface name may not be persistent across an OS reboot, use
145 * MAC address for identifing the active slave */
74ff3298 146 struct eth_addr active_slave_mac;
3e5aeeb5 147 /* The MAC address of the active interface. */
f620b43a 148 /* Legacy compatibility. */
9dd165e0 149 bool lacp_fallback_ab; /* Fallback to active-backup on LACP failure. */
f620b43a 150
37bec3d3 151 struct ovs_refcount ref_cnt;
f620b43a
BP
152};
153
adcf00ba
AZ
154/* What to do with an bond_recirc_rule. */
155enum bond_op {
156 ADD, /* Add the rule to ofproto's flow table. */
157 DEL, /* Delete the rule from the ofproto's flow table. */
158};
159
160/* A rule to add to or delete from ofproto's internal flow table. */
161struct bond_pr_rule_op {
162 struct hmap_node hmap_node;
163 struct match match;
164 ofp_port_t out_ofport;
165 enum bond_op op;
6c932bc8 166 struct rule **pr_rule;
adcf00ba
AZ
167};
168
3bfd3972
EJ
169static void bond_entry_reset(struct bond *) OVS_REQ_WRLOCK(rwlock);
170static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_)
171 OVS_REQ_RDLOCK(rwlock);
4a1b8f30
EJ
172static void bond_enable_slave(struct bond_slave *, bool enable)
173 OVS_REQ_WRLOCK(rwlock);
174static void bond_link_status_update(struct bond_slave *)
3bfd3972 175 OVS_REQ_WRLOCK(rwlock);
4a1b8f30 176static void bond_choose_active_slave(struct bond *)
9e1a6910 177 OVS_REQ_WRLOCK(rwlock);
74ff3298 178static unsigned int bond_hash_src(const struct eth_addr mac,
672d18b2
EJ
179 uint16_t vlan, uint32_t basis);
180static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan,
181 uint32_t basis);
f620b43a
BP
182static struct bond_entry *lookup_bond_entry(const struct bond *,
183 const struct flow *,
3bfd3972
EJ
184 uint16_t vlan)
185 OVS_REQ_RDLOCK(rwlock);
f1c8a79c
AW
186static struct bond_slave *get_enabled_slave(struct bond *)
187 OVS_REQ_RDLOCK(rwlock);
f620b43a
BP
188static struct bond_slave *choose_output_slave(const struct bond *,
189 const struct flow *,
bcd2633a 190 struct flow_wildcards *,
4a1b8f30 191 uint16_t vlan)
3bfd3972 192 OVS_REQ_RDLOCK(rwlock);
f620b43a
BP
193
194/* Attempts to parse 's' as the name of a bond balancing mode. If successful,
195 * stores the mode in '*balance' and returns true. Otherwise returns false
196 * without modifying '*balance'. */
197bool
198bond_mode_from_string(enum bond_mode *balance, const char *s)
199{
200 if (!strcmp(s, bond_mode_to_string(BM_TCP))) {
201 *balance = BM_TCP;
202 } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) {
203 *balance = BM_SLB;
204 } else if (!strcmp(s, bond_mode_to_string(BM_AB))) {
205 *balance = BM_AB;
206 } else {
207 return false;
208 }
209 return true;
210}
211
212/* Returns a string representing 'balance'. */
213const char *
214bond_mode_to_string(enum bond_mode balance) {
215 switch (balance) {
216 case BM_TCP:
217 return "balance-tcp";
218 case BM_SLB:
219 return "balance-slb";
220 case BM_AB:
221 return "active-backup";
222 }
428b2edd 223 OVS_NOT_REACHED();
f620b43a
BP
224}
225
f620b43a
BP
226\f
227/* Creates and returns a new bond whose configuration is initially taken from
228 * 's'.
229 *
230 * The caller should register each slave on the new bond by calling
231 * bond_slave_register(). */
232struct bond *
adcf00ba 233bond_create(const struct bond_settings *s, struct ofproto_dpif *ofproto)
f620b43a
BP
234{
235 struct bond *bond;
236
237 bond = xzalloc(sizeof *bond);
adcf00ba 238 bond->ofproto = ofproto;
f620b43a 239 hmap_init(&bond->slaves);
417e7e66 240 ovs_list_init(&bond->enabled_slaves);
f1c8a79c 241 ovs_mutex_init(&bond->mutex);
37bec3d3 242 ovs_refcount_init(&bond->ref_cnt);
adcf00ba
AZ
243 hmap_init(&bond->pr_rule_ops);
244
30353934 245 bond->active_slave_mac = eth_addr_zero;
246 bond->active_slave_changed = false;
247
f620b43a 248 bond_reconfigure(bond, s);
f620b43a
BP
249 return bond;
250}
251
03366a2d
EJ
252struct bond *
253bond_ref(const struct bond *bond_)
254{
255 struct bond *bond = CONST_CAST(struct bond *, bond_);
256
bca0b3b4 257 if (bond) {
37bec3d3 258 ovs_refcount_ref(&bond->ref_cnt);
bca0b3b4 259 }
03366a2d
EJ
260 return bond;
261}
262
f620b43a
BP
263/* Frees 'bond'. */
264void
03366a2d 265bond_unref(struct bond *bond)
f620b43a 266{
4ec3d7c7
DDP
267 struct bond_pr_rule_op *pr_op;
268 struct bond_slave *slave;
f620b43a 269
24f83812 270 if (!bond || ovs_refcount_unref_relaxed(&bond->ref_cnt) != 1) {
03366a2d
EJ
271 return;
272 }
273
3bfd3972
EJ
274 ovs_rwlock_wrlock(&rwlock);
275 hmap_remove(all_bonds, &bond->hmap_node);
276 ovs_rwlock_unlock(&rwlock);
f620b43a 277
4ec3d7c7 278 HMAP_FOR_EACH_POP (slave, hmap_node, &bond->slaves) {
f620b43a
BP
279 /* Client owns 'slave->netdev'. */
280 free(slave->name);
281 free(slave);
282 }
283 hmap_destroy(&bond->slaves);
284
f1c8a79c 285 ovs_mutex_destroy(&bond->mutex);
f620b43a 286 free(bond->hash);
f620b43a 287 free(bond->name);
adcf00ba 288
4ec3d7c7 289 HMAP_FOR_EACH_POP (pr_op, hmap_node, &bond->pr_rule_ops) {
adcf00ba
AZ
290 free(pr_op);
291 }
292 hmap_destroy(&bond->pr_rule_ops);
293
294 if (bond->recirc_id) {
e672ff9b 295 recirc_free_id(bond->recirc_id);
adcf00ba
AZ
296 }
297
f620b43a
BP
298 free(bond);
299}
300
adcf00ba
AZ
301static void
302add_pr_rule(struct bond *bond, const struct match *match,
6c932bc8 303 ofp_port_t out_ofport, struct rule **rule)
adcf00ba
AZ
304{
305 uint32_t hash = match_hash(match, 0);
306 struct bond_pr_rule_op *pr_op;
307
308 HMAP_FOR_EACH_WITH_HASH(pr_op, hmap_node, hash, &bond->pr_rule_ops) {
309 if (match_equal(&pr_op->match, match)) {
310 pr_op->op = ADD;
311 pr_op->out_ofport = out_ofport;
312 pr_op->pr_rule = rule;
313 return;
314 }
315 }
316
317 pr_op = xmalloc(sizeof *pr_op);
318 pr_op->match = *match;
319 pr_op->op = ADD;
320 pr_op->out_ofport = out_ofport;
321 pr_op->pr_rule = rule;
322 hmap_insert(&bond->pr_rule_ops, &pr_op->hmap_node, hash);
323}
324
325static void
326update_recirc_rules(struct bond *bond)
ca8127fd 327 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
328{
329 struct match match;
330 struct bond_pr_rule_op *pr_op, *next_op;
331 uint64_t ofpacts_stub[128 / 8];
332 struct ofpbuf ofpacts;
333 int i;
334
335 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
336
337 HMAP_FOR_EACH(pr_op, hmap_node, &bond->pr_rule_ops) {
338 pr_op->op = DEL;
339 }
340
6c932bc8
AZ
341 if (bond->hash && bond->recirc_id) {
342 for (i = 0; i < BOND_BUCKETS; i++) {
343 struct bond_slave *slave = bond->hash[i].slave;
adcf00ba 344
6c932bc8
AZ
345 if (slave) {
346 match_init_catchall(&match);
347 match_set_recirc_id(&match, bond->recirc_id);
6c932bc8 348 match_set_dp_hash_masked(&match, i, BOND_MASK);
adcf00ba 349
6c932bc8
AZ
350 add_pr_rule(bond, &match, slave->ofp_port,
351 &bond->hash[i].pr_rule);
352 }
adcf00ba
AZ
353 }
354 }
355
356 HMAP_FOR_EACH_SAFE(pr_op, next_op, hmap_node, &bond->pr_rule_ops) {
357 int error;
adcf00ba
AZ
358 switch (pr_op->op) {
359 case ADD:
360 ofpbuf_clear(&ofpacts);
361 ofpact_put_OUTPUT(&ofpacts)->port = pr_op->out_ofport;
362 error = ofproto_dpif_add_internal_flow(bond->ofproto,
363 &pr_op->match,
290ad78a 364 RECIRC_RULE_PRIORITY, 0,
6c932bc8 365 &ofpacts, pr_op->pr_rule);
adcf00ba
AZ
366 if (error) {
367 char *err_s = match_to_string(&pr_op->match,
368 RECIRC_RULE_PRIORITY);
369
370 VLOG_ERR("failed to add post recirculation flow %s", err_s);
371 free(err_s);
adcf00ba
AZ
372 }
373 break;
374
375 case DEL:
376 error = ofproto_dpif_delete_internal_flow(bond->ofproto,
377 &pr_op->match,
378 RECIRC_RULE_PRIORITY);
379 if (error) {
380 char *err_s = match_to_string(&pr_op->match,
381 RECIRC_RULE_PRIORITY);
382
383 VLOG_ERR("failed to remove post recirculation flow %s", err_s);
384 free(err_s);
385 }
386
387 hmap_remove(&bond->pr_rule_ops, &pr_op->hmap_node);
6c932bc8 388 *pr_op->pr_rule = NULL;
adcf00ba
AZ
389 free(pr_op);
390 break;
391 }
392 }
393
394 ofpbuf_uninit(&ofpacts);
395}
396
397
f620b43a
BP
398/* Updates 'bond''s overall configuration to 's'.
399 *
400 * The caller should register each slave on 'bond' by calling
401 * bond_slave_register(). This is optional if none of the slaves'
4d6fb5eb 402 * configuration has changed. In any case it can't hurt.
59d7b2b6
EJ
403 *
404 * Returns true if the configuration has changed in such a way that requires
405 * flow revalidation.
406 * */
407bool
f620b43a
BP
408bond_reconfigure(struct bond *bond, const struct bond_settings *s)
409{
59d7b2b6
EJ
410 bool revalidate = false;
411
3bfd3972 412 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
413 if (!bond->name || strcmp(bond->name, s->name)) {
414 if (bond->name) {
3bfd3972 415 hmap_remove(all_bonds, &bond->hmap_node);
f620b43a
BP
416 free(bond->name);
417 }
418 bond->name = xstrdup(s->name);
3bfd3972 419 hmap_insert(all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
f620b43a
BP
420 }
421
f620b43a
BP
422 bond->updelay = s->up_delay;
423 bond->downdelay = s->down_delay;
bc1b010c 424
9dd165e0
RK
425 if (bond->lacp_fallback_ab != s->lacp_fallback_ab_cfg) {
426 bond->lacp_fallback_ab = s->lacp_fallback_ab_cfg;
427 revalidate = true;
428 }
429
bc1b010c
EJ
430 if (bond->rebalance_interval != s->rebalance_interval) {
431 bond->rebalance_interval = s->rebalance_interval;
432 revalidate = true;
433 }
f620b43a 434
59d7b2b6
EJ
435 if (bond->balance != s->balance) {
436 bond->balance = s->balance;
437 revalidate = true;
438 }
439
672d18b2
EJ
440 if (bond->basis != s->basis) {
441 bond->basis = s->basis;
442 revalidate = true;
443 }
444
62904702
EJ
445 if (bond->bond_revalidate) {
446 revalidate = true;
447 bond->bond_revalidate = false;
448 }
449
adcf00ba
AZ
450 if (bond->balance != BM_AB) {
451 if (!bond->recirc_id) {
e672ff9b 452 bond->recirc_id = recirc_alloc_id(bond->ofproto);
adcf00ba
AZ
453 }
454 } else if (bond->recirc_id) {
e672ff9b 455 recirc_free_id(bond->recirc_id);
adcf00ba
AZ
456 bond->recirc_id = 0;
457 }
458
95aafb2a
EJ
459 if (bond->balance == BM_AB || !bond->hash || revalidate) {
460 bond_entry_reset(bond);
461 }
462
3bfd3972 463 ovs_rwlock_unlock(&rwlock);
59d7b2b6 464 return revalidate;
f620b43a
BP
465}
466
3e5aeeb5 467static struct bond_slave *
74ff3298 468bond_find_slave_by_mac(const struct bond *bond, const struct eth_addr mac)
3e5aeeb5
AZ
469{
470 struct bond_slave *slave;
471
472 /* Find the last active slave */
473 HMAP_FOR_EACH(slave, hmap_node, &bond->slaves) {
74ff3298 474 struct eth_addr slave_mac;
3e5aeeb5 475
74ff3298 476 if (netdev_get_etheraddr(slave->netdev, &slave_mac)) {
3e5aeeb5
AZ
477 continue;
478 }
479
74ff3298 480 if (eth_addr_equals(slave_mac, mac)) {
3e5aeeb5
AZ
481 return slave;
482 }
483 }
484
485 return NULL;
486}
487
488static void
489bond_active_slave_changed(struct bond *bond)
490{
74ff3298 491 struct eth_addr mac;
3e5aeeb5 492
74ff3298
JR
493 netdev_get_etheraddr(bond->active_slave->netdev, &mac);
494 bond->active_slave_mac = mac;
3e5aeeb5
AZ
495 bond->active_slave_changed = true;
496 seq_change(connectivity_seq_get());
497}
498
f8ddccd2 499static void
1ea24138 500bond_slave_set_netdev__(struct bond_slave *slave, struct netdev *netdev)
3bfd3972 501 OVS_REQ_WRLOCK(rwlock)
f8ddccd2
BP
502{
503 if (slave->netdev != netdev) {
f8ddccd2 504 slave->netdev = netdev;
1ea24138 505 slave->change_seq = 0;
f8ddccd2
BP
506 }
507}
508
f620b43a
BP
509/* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an
510 * arbitrary client-provided pointer that uniquely identifies a slave within a
511 * bond. If 'slave_' already exists within 'bond' then this function
512 * reconfigures the existing slave.
513 *
514 * 'netdev' must be the network device that 'slave_' represents. It is owned
515 * by the client, so the client must not close it before either unregistering
516 * 'slave_' or destroying 'bond'.
4d6fb5eb 517 */
f620b43a 518void
adcf00ba
AZ
519bond_slave_register(struct bond *bond, void *slave_,
520 ofp_port_t ofport, struct netdev *netdev)
f620b43a 521{
3bfd3972 522 struct bond_slave *slave;
f620b43a 523
3bfd3972
EJ
524 ovs_rwlock_wrlock(&rwlock);
525 slave = bond_slave_lookup(bond, slave_);
f620b43a
BP
526 if (!slave) {
527 slave = xzalloc(sizeof *slave);
528
529 hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0));
530 slave->bond = bond;
531 slave->aux = slave_;
adcf00ba 532 slave->ofp_port = ofport;
f620b43a 533 slave->delay_expires = LLONG_MAX;
244b2160 534 slave->name = xstrdup(netdev_get_name(netdev));
7321e30e 535 bond->bond_revalidate = true;
244b2160 536
b3c18f66 537 slave->enabled = false;
4a1b8f30 538 bond_enable_slave(slave, netdev_get_carrier(netdev));
f620b43a
BP
539 }
540
1ea24138 541 bond_slave_set_netdev__(slave, netdev);
a6934aa9 542
f620b43a
BP
543 free(slave->name);
544 slave->name = xstrdup(netdev_get_name(netdev));
3bfd3972 545 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
546}
547
f8ddccd2
BP
548/* Updates the network device to be used with 'slave_' to 'netdev'.
549 *
550 * This is useful if the caller closes and re-opens the network device
551 * registered with bond_slave_register() but doesn't need to change anything
552 * else. */
553void
554bond_slave_set_netdev(struct bond *bond, void *slave_, struct netdev *netdev)
555{
3bfd3972
EJ
556 struct bond_slave *slave;
557
558 ovs_rwlock_wrlock(&rwlock);
559 slave = bond_slave_lookup(bond, slave_);
f8ddccd2 560 if (slave) {
1ea24138 561 bond_slave_set_netdev__(slave, netdev);
f8ddccd2 562 }
3bfd3972 563 ovs_rwlock_unlock(&rwlock);
f8ddccd2
BP
564}
565
f620b43a
BP
566/* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave
567 * then this function has no effect.
568 *
569 * Unregistering a slave invalidates all flows. */
570void
571bond_slave_unregister(struct bond *bond, const void *slave_)
572{
3bfd3972 573 struct bond_slave *slave;
f620b43a
BP
574 bool del_active;
575
3bfd3972
EJ
576 ovs_rwlock_wrlock(&rwlock);
577 slave = bond_slave_lookup(bond, slave_);
f620b43a 578 if (!slave) {
3bfd3972 579 goto out;
f620b43a
BP
580 }
581
4a1b8f30
EJ
582 bond->bond_revalidate = true;
583 bond_enable_slave(slave, false);
b3c18f66 584
f620b43a
BP
585 del_active = bond->active_slave == slave;
586 if (bond->hash) {
587 struct bond_entry *e;
588 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
589 if (e->slave == slave) {
590 e->slave = NULL;
591 }
592 }
593 }
594
595 free(slave->name);
596
597 hmap_remove(&bond->slaves, &slave->hmap_node);
598 /* Client owns 'slave->netdev'. */
599 free(slave);
600
601 if (del_active) {
4a1b8f30 602 bond_choose_active_slave(bond);
f620b43a
BP
603 bond->send_learning_packets = true;
604 }
3bfd3972
EJ
605out:
606 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
607}
608
296f6519
EJ
609/* Should be called on each slave in 'bond' before bond_run() to indicate
610 * whether or not 'slave_' may be enabled. This function is intended to allow
611 * other protocols to have some impact on bonding decisions. For example LACP
612 * or high level link monitoring protocols may decide that a given slave should
613 * not be able to send traffic. */
4d6fb5eb 614void
296f6519 615bond_slave_set_may_enable(struct bond *bond, void *slave_, bool may_enable)
4d6fb5eb 616{
3bfd3972 617 ovs_rwlock_wrlock(&rwlock);
296f6519 618 bond_slave_lookup(bond, slave_)->may_enable = may_enable;
3bfd3972 619 ovs_rwlock_unlock(&rwlock);
4d6fb5eb
EJ
620}
621
4a1b8f30
EJ
622/* Performs periodic maintenance on 'bond'.
623 *
624 * Returns true if the caller should revalidate its flows.
f620b43a
BP
625 *
626 * The caller should check bond_should_send_learning_packets() afterward. */
4a1b8f30
EJ
627bool
628bond_run(struct bond *bond, enum lacp_status lacp_status)
f620b43a
BP
629{
630 struct bond_slave *slave;
4a1b8f30 631 bool revalidate;
f620b43a 632
3bfd3972 633 ovs_rwlock_wrlock(&rwlock);
bdebeece
EJ
634 if (bond->lacp_status != lacp_status) {
635 bond->lacp_status = lacp_status;
4592d0e2
EJ
636 bond->bond_revalidate = true;
637 }
4d6fb5eb 638
f620b43a
BP
639 /* Enable slaves based on link status and LACP feedback. */
640 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
4a1b8f30 641 bond_link_status_update(slave);
da4a6191 642 slave->change_seq = seq_read(connectivity_seq_get());
f620b43a
BP
643 }
644 if (!bond->active_slave || !bond->active_slave->enabled) {
4a1b8f30 645 bond_choose_active_slave(bond);
f620b43a
BP
646 }
647
4a1b8f30
EJ
648 revalidate = bond->bond_revalidate;
649 bond->bond_revalidate = false;
3bfd3972 650 ovs_rwlock_unlock(&rwlock);
4a1b8f30
EJ
651
652 return revalidate;
f620b43a
BP
653}
654
655/* Causes poll_block() to wake up when 'bond' needs something to be done. */
656void
657bond_wait(struct bond *bond)
658{
659 struct bond_slave *slave;
660
3bfd3972 661 ovs_rwlock_rdlock(&rwlock);
f620b43a
BP
662 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
663 if (slave->delay_expires != LLONG_MAX) {
664 poll_timer_wait_until(slave->delay_expires);
665 }
1ea24138 666
da4a6191 667 seq_wait(connectivity_seq_get(), slave->change_seq);
f620b43a
BP
668 }
669
bbc13389 670 if (bond->bond_revalidate) {
f620b43a
BP
671 poll_immediate_wake();
672 }
3bfd3972 673 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
674
675 /* We don't wait for bond->next_rebalance because rebalancing can only run
676 * at a flow account checkpoint. ofproto does checkpointing on its own
677 * schedule and bond_rebalance() gets called afterward, so we'd just be
678 * waking up for no purpose. */
679}
680\f
681/* MAC learning table interaction. */
682
683static bool
684may_send_learning_packets(const struct bond *bond)
685{
9dd165e0
RK
686 return ((bond->lacp_status == LACP_DISABLED
687 && (bond->balance == BM_SLB || bond->balance == BM_AB))
688 || (bond->lacp_fallback_ab && bond->lacp_status == LACP_CONFIGURED))
bdebeece 689 && bond->active_slave;
f620b43a
BP
690}
691
692/* Returns true if 'bond' needs the client to send out packets to assist with
693 * MAC learning on 'bond'. If this function returns true, then the client
694 * should iterate through its MAC learning table for the bridge on which 'bond'
695 * is located. For each MAC that has been learned on a port other than 'bond',
ea131871 696 * it should call bond_compose_learning_packet().
f620b43a 697 *
477879ea
BP
698 * This function will only return true if 'bond' is in SLB or active-backup
699 * mode and LACP is not negotiated. Otherwise sending learning packets isn't
700 * necessary.
f620b43a
BP
701 *
702 * Calling this function resets the state that it checks. */
703bool
704bond_should_send_learning_packets(struct bond *bond)
705{
3bfd3972
EJ
706 bool send;
707
708 ovs_rwlock_wrlock(&rwlock);
709 send = bond->send_learning_packets && may_send_learning_packets(bond);
f620b43a 710 bond->send_learning_packets = false;
3bfd3972 711 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
712 return send;
713}
714
715/* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
716 *
ea131871
JG
717 * See bond_should_send_learning_packets() for description of usage. The
718 * caller should send the composed packet on the port associated with
719 * port_aux and takes ownership of the returned ofpbuf. */
cf62fa4c 720struct dp_packet *
74ff3298 721bond_compose_learning_packet(struct bond *bond, const struct eth_addr eth_src,
ea131871 722 uint16_t vlan, void **port_aux)
f620b43a
BP
723{
724 struct bond_slave *slave;
cf62fa4c 725 struct dp_packet *packet;
f620b43a 726 struct flow flow;
f620b43a 727
3bfd3972 728 ovs_rwlock_rdlock(&rwlock);
cb22974d 729 ovs_assert(may_send_learning_packets(bond));
f620b43a 730 memset(&flow, 0, sizeof flow);
74ff3298 731 flow.dl_src = eth_src;
4a1b8f30 732 slave = choose_output_slave(bond, &flow, NULL, vlan);
f620b43a 733
cf62fa4c 734 packet = dp_packet_new(0);
2ea838ac 735 compose_rarp(packet, eth_src);
f620b43a 736 if (vlan) {
1bf02876 737 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), htons(vlan));
f620b43a 738 }
f620b43a 739
ea131871 740 *port_aux = slave->aux;
3bfd3972 741 ovs_rwlock_unlock(&rwlock);
ea131871 742 return packet;
f620b43a
BP
743}
744\f
745/* Checks whether a packet that arrived on 'slave_' within 'bond', with an
746 * Ethernet destination address of 'eth_dst', should be admitted.
747 *
748 * The return value is one of the following:
749 *
750 * - BV_ACCEPT: Admit the packet.
751 *
752 * - BV_DROP: Drop the packet.
753 *
754 * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
755 * Ethernet source address and VLAN. If there is none, or if the packet
756 * is on the learned port, then admit the packet. If a different port has
757 * been learned, however, drop the packet (and do not use it for MAC
758 * learning).
759 */
760enum bond_verdict
761bond_check_admissibility(struct bond *bond, const void *slave_,
74ff3298 762 const struct eth_addr eth_dst)
f620b43a 763{
3bfd3972
EJ
764 enum bond_verdict verdict = BV_DROP;
765 struct bond_slave *slave;
9a1c6450 766
3bfd3972
EJ
767 ovs_rwlock_rdlock(&rwlock);
768 slave = bond_slave_lookup(bond, slave_);
4222bbc8 769 if (!slave) {
3bfd3972 770 goto out;
4222bbc8
EJ
771 }
772
9a1c6450
EJ
773 /* LACP bonds have very loose admissibility restrictions because we can
774 * assume the remote switch is aware of the bond and will "do the right
775 * thing". However, as a precaution we drop packets on disabled slaves
776 * because no correctly implemented partner switch should be sending
bdebeece
EJ
777 * packets to them.
778 *
779 * If LACP is configured, but LACP negotiations have been unsuccessful, we
9dd165e0 780 * drop all incoming traffic except if lacp_fallback_ab is enabled. */
bdebeece 781 switch (bond->lacp_status) {
3bfd3972
EJ
782 case LACP_NEGOTIATED:
783 verdict = slave->enabled ? BV_ACCEPT : BV_DROP;
784 goto out;
785 case LACP_CONFIGURED:
9dd165e0
RK
786 if (!bond->lacp_fallback_ab) {
787 goto out;
788 }
e5c4f827 789 break;
3bfd3972 790 case LACP_DISABLED:
e5c4f827 791 if (bond->balance == BM_TCP) {
792 goto out;
793 }
3bfd3972 794 break;
f620b43a
BP
795 }
796
797 /* Drop all multicast packets on inactive slaves. */
798 if (eth_addr_is_multicast(eth_dst)) {
4222bbc8 799 if (bond->active_slave != slave) {
3bfd3972 800 goto out;
f620b43a
BP
801 }
802 }
803
f931a4c9 804 switch (bond->balance) {
9dd165e0
RK
805 case BM_TCP:
806 /* TCP balanced bonds require successful LACP negotiations. Based on the
807 * above check, LACP is off or lacp_fallback_ab is true on this bond.
808 * If lacp_fallback_ab is true fall through to BM_AB case else, we
809 * drop all incoming traffic. */
810 if (!bond->lacp_fallback_ab) {
811 goto out;
812 }
813
f931a4c9
BP
814 case BM_AB:
815 /* Drop all packets which arrive on backup slaves. This is similar to
816 * how Linux bonding handles active-backup bonds. */
7ba7dcf0
EJ
817 if (bond->active_slave != slave) {
818 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
819
e6b2255c
BP
820 VLOG_DBG_RL(&rl, "active-backup bond received packet on backup"
821 " slave (%s) destined for " ETH_ADDR_FMT,
822 slave->name, ETH_ADDR_ARGS(eth_dst));
3bfd3972 823 goto out;
7ba7dcf0 824 }
3bfd3972
EJ
825 verdict = BV_ACCEPT;
826 goto out;
f931a4c9 827
f931a4c9
BP
828 case BM_SLB:
829 /* Drop all packets for which we have learned a different input port,
830 * because we probably sent the packet on one slave and got it back on
831 * the other. Gratuitous ARP packets are an exception to this rule:
832 * the host has moved to another switch. The exception to the
833 * exception is if we locked the learning table to avoid reflections on
834 * bond slaves. */
3bfd3972
EJ
835 verdict = BV_DROP_IF_MOVED;
836 goto out;
7ba7dcf0
EJ
837 }
838
428b2edd 839 OVS_NOT_REACHED();
3bfd3972
EJ
840out:
841 ovs_rwlock_unlock(&rwlock);
842 return verdict;
843
f620b43a
BP
844}
845
846/* Returns the slave (registered on 'bond' by bond_slave_register()) to which
847 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
848 * NULL if the packet should be dropped because no slaves are enabled.
849 *
850 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
851 * should be a VID only (i.e. excluding the PCP bits). Second,
852 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
853 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
854 * packet belongs to (so for an access port it will be the access port's VLAN).
855 *
bcd2633a
JP
856 * If 'wc' is non-NULL, bitwise-OR's 'wc' with the set of bits that were
857 * significant in the selection. At some point earlier, 'wc' should
858 * have been initialized (e.g., by flow_wildcards_init_catchall()).
f620b43a
BP
859 */
860void *
861bond_choose_output_slave(struct bond *bond, const struct flow *flow,
4a1b8f30 862 struct flow_wildcards *wc, uint16_t vlan)
f620b43a 863{
3bfd3972 864 struct bond_slave *slave;
b5d5d7d3 865 void *aux;
3bfd3972
EJ
866
867 ovs_rwlock_rdlock(&rwlock);
4a1b8f30 868 slave = choose_output_slave(bond, flow, wc, vlan);
b5d5d7d3 869 aux = slave ? slave->aux : NULL;
3bfd3972 870 ovs_rwlock_unlock(&rwlock);
b5d5d7d3
AW
871
872 return aux;
f620b43a 873}
f620b43a 874\f
adcf00ba
AZ
875/* Recirculation. */
876static void
877bond_entry_account(struct bond_entry *entry, uint64_t rule_tx_bytes)
c6855ec5 878 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
879{
880 if (entry->slave) {
881 uint64_t delta;
882
883 delta = rule_tx_bytes - entry->pr_tx_bytes;
884 entry->tx_bytes += delta;
885 entry->pr_tx_bytes = rule_tx_bytes;
886 }
887}
888
889/* Maintain bond stats using post recirculation rule byte counters.*/
60cda7d6 890static void
adcf00ba 891bond_recirculation_account(struct bond *bond)
80316557 892 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
893{
894 int i;
895
adcf00ba
AZ
896 for (i=0; i<=BOND_MASK; i++) {
897 struct bond_entry *entry = &bond->hash[i];
898 struct rule *rule = entry->pr_rule;
899
900 if (rule) {
901 uint64_t n_packets OVS_UNUSED;
902 long long int used OVS_UNUSED;
903 uint64_t n_bytes;
904
905 rule->ofproto->ofproto_class->rule_get_stats(
906 rule, &n_packets, &n_bytes, &used);
907 bond_entry_account(entry, n_bytes);
908 }
909 }
adcf00ba
AZ
910}
911
912bool
913bond_may_recirc(const struct bond *bond, uint32_t *recirc_id,
914 uint32_t *hash_bias)
915{
80316557 916 if (bond->balance == BM_TCP && bond->recirc_id) {
adcf00ba
AZ
917 if (recirc_id) {
918 *recirc_id = bond->recirc_id;
919 }
920 if (hash_bias) {
921 *hash_bias = bond->basis;
922 }
923 return true;
924 } else {
925 return false;
926 }
927}
928
ca8127fd
AZ
929static void
930bond_update_post_recirc_rules__(struct bond* bond, const bool force)
931 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
932{
933 struct bond_entry *e;
934 bool update_rules = force; /* Always update rules if caller forces it. */
935
936 /* Make sure all bond entries are populated */
937 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
938 if (!e->slave || !e->slave->enabled) {
939 update_rules = true;
940 e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
941 struct bond_slave, hmap_node);
942 if (!e->slave->enabled) {
943 e->slave = bond->active_slave;
944 }
945 }
946 }
947
948 if (update_rules) {
949 update_recirc_rules(bond);
950 }
951}
ca8127fd
AZ
952
953void
954bond_update_post_recirc_rules(struct bond* bond, const bool force)
955{
956 ovs_rwlock_wrlock(&rwlock);
957 bond_update_post_recirc_rules__(bond, force);
958 ovs_rwlock_unlock(&rwlock);
959}
adcf00ba 960\f
f620b43a
BP
961/* Rebalancing. */
962
1b137691 963static bool
3bfd3972 964bond_is_balanced(const struct bond *bond) OVS_REQ_RDLOCK(rwlock)
1b137691 965{
bc1b010c
EJ
966 return bond->rebalance_interval
967 && (bond->balance == BM_SLB || bond->balance == BM_TCP);
1b137691
EJ
968}
969
f620b43a
BP
970/* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
971void
972bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
973 uint64_t n_bytes)
974{
3bfd3972 975 ovs_rwlock_wrlock(&rwlock);
1b137691 976 if (bond_is_balanced(bond)) {
f620b43a 977 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
f620b43a 978 }
3bfd3972 979 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
980}
981
982static struct bond_slave *
ca6ba700 983bond_slave_from_bal_node(struct ovs_list *bal) OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
984{
985 return CONTAINER_OF(bal, struct bond_slave, bal_node);
986}
987
988static void
ca6ba700 989log_bals(struct bond *bond, const struct ovs_list *bals)
c6855ec5 990 OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
991{
992 if (VLOG_IS_DBG_ENABLED()) {
993 struct ds ds = DS_EMPTY_INITIALIZER;
994 const struct bond_slave *slave;
995
996 LIST_FOR_EACH (slave, bal_node, bals) {
997 if (ds.length) {
998 ds_put_char(&ds, ',');
999 }
1000 ds_put_format(&ds, " %s %"PRIu64"kB",
1001 slave->name, slave->tx_bytes / 1024);
1002
1003 if (!slave->enabled) {
1004 ds_put_cstr(&ds, " (disabled)");
1005 }
417e7e66 1006 if (!ovs_list_is_empty(&slave->entries)) {
f620b43a
BP
1007 struct bond_entry *e;
1008
1009 ds_put_cstr(&ds, " (");
1010 LIST_FOR_EACH (e, list_node, &slave->entries) {
417e7e66 1011 if (&e->list_node != ovs_list_front(&slave->entries)) {
f620b43a
BP
1012 ds_put_cstr(&ds, " + ");
1013 }
34582733 1014 ds_put_format(&ds, "h%"PRIdPTR": %"PRIu64"kB",
f620b43a
BP
1015 e - bond->hash, e->tx_bytes / 1024);
1016 }
1017 ds_put_cstr(&ds, ")");
1018 }
1019 }
1020 VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
1021 ds_destroy(&ds);
1022 }
1023}
1024
1025/* Shifts 'hash' from its current slave to 'to'. */
1026static void
4a1b8f30 1027bond_shift_load(struct bond_entry *hash, struct bond_slave *to)
c6855ec5 1028 OVS_REQ_WRLOCK(rwlock)
f620b43a
BP
1029{
1030 struct bond_slave *from = hash->slave;
1031 struct bond *bond = from->bond;
1032 uint64_t delta = hash->tx_bytes;
1033
34582733 1034 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %"PRIdPTR") "
f620b43a
BP
1035 "from %s to %s (now carrying %"PRIu64"kB and "
1036 "%"PRIu64"kB load, respectively)",
1037 bond->name, delta / 1024, hash - bond->hash,
1038 from->name, to->name,
1039 (from->tx_bytes - delta) / 1024,
1040 (to->tx_bytes + delta) / 1024);
1041
1042 /* Shift load away from 'from' to 'to'. */
1043 from->tx_bytes -= delta;
1044 to->tx_bytes += delta;
1045
1046 /* Arrange for flows to be revalidated. */
dc30ea2d 1047 hash->slave = to;
4a1b8f30 1048 bond->bond_revalidate = true;
f620b43a
BP
1049}
1050
09a5d390
BP
1051/* Picks and returns a bond_entry to migrate from 'from' (the most heavily
1052 * loaded bond slave) to a bond slave that has 'to_tx_bytes' bytes of load,
f620b43a
BP
1053 * given that doing so must decrease the ratio of the load on the two slaves by
1054 * at least 0.1. Returns NULL if there is no appropriate entry.
1055 *
1056 * The list of entries isn't sorted. I don't know of a reason to prefer to
1057 * shift away small hashes or large hashes. */
1058static struct bond_entry *
1059choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
c6855ec5 1060 OVS_REQ_WRLOCK(rwlock)
f620b43a
BP
1061{
1062 struct bond_entry *e;
1063
417e7e66 1064 if (ovs_list_is_short(&from->entries)) {
f620b43a
BP
1065 /* 'from' carries no more than one MAC hash, so shifting load away from
1066 * it would be pointless. */
1067 return NULL;
1068 }
1069
1070 LIST_FOR_EACH (e, list_node, &from->entries) {
c460a6a7
AZ
1071 uint64_t delta = e->tx_bytes; /* The amount to rebalance. */
1072 uint64_t ideal_tx_bytes = (from->tx_bytes + to_tx_bytes)/2;
1073 /* Note, the ideal traffic is the mid point
1074 * between 'from' and 'to'. This value does
1075 * not change by rebalancing. */
1076 uint64_t new_low; /* The lower bandwidth between 'to' and 'from'
1077 after rebalancing. */
1078
1079 new_low = MIN(from->tx_bytes - delta, to_tx_bytes + delta);
1080
1081 if ((new_low > to_tx_bytes) &&
1082 (new_low - to_tx_bytes >= (ideal_tx_bytes - to_tx_bytes) / 10)) {
1083 /* Only rebalance if the new 'low' is closer to to the mid point,
1084 * and the improvement exceeds 10% of current traffic
1085 * deviation from the ideal split.
1086 *
1087 * The improvement on the 'high' side is always the same as the
1088 * 'low' side. Thus consider 'low' side is sufficient. */
f620b43a
BP
1089 return e;
1090 }
1091 }
1092
1093 return NULL;
1094}
1095
1096/* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
1097 * maintained. */
1098static void
ca6ba700 1099insert_bal(struct ovs_list *bals, struct bond_slave *slave)
f620b43a
BP
1100{
1101 struct bond_slave *pos;
1102
1103 LIST_FOR_EACH (pos, bal_node, bals) {
1104 if (slave->tx_bytes > pos->tx_bytes) {
1105 break;
1106 }
1107 }
417e7e66 1108 ovs_list_insert(&pos->bal_node, &slave->bal_node);
f620b43a
BP
1109}
1110
1111/* Removes 'slave' from its current list and then inserts it into 'bals' so
1112 * that descending order of 'tx_bytes' is maintained. */
1113static void
ca6ba700 1114reinsert_bal(struct ovs_list *bals, struct bond_slave *slave)
f620b43a 1115{
417e7e66 1116 ovs_list_remove(&slave->bal_node);
f620b43a
BP
1117 insert_bal(bals, slave);
1118}
1119
1120/* If 'bond' needs rebalancing, does so.
1121 *
adcf00ba
AZ
1122 * The caller should have called bond_account() for each active flow, or in case
1123 * of recirculation is used, have called bond_recirculation_account(bond),
1124 * to ensure that flow data is consistently accounted at this point.
60cda7d6
AZ
1125 */
1126void
4a1b8f30 1127bond_rebalance(struct bond *bond)
f620b43a
BP
1128{
1129 struct bond_slave *slave;
1130 struct bond_entry *e;
ca6ba700 1131 struct ovs_list bals;
adcf00ba 1132 bool rebalanced = false;
60cda7d6 1133 bool use_recirc;
f620b43a 1134
3bfd3972 1135 ovs_rwlock_wrlock(&rwlock);
1b137691 1136 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
adcf00ba 1137 goto done;
f620b43a
BP
1138 }
1139 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1140
07a3cd5c 1141 use_recirc = bond->ofproto->backer->support.odp.recirc &&
60cda7d6
AZ
1142 bond_may_recirc(bond, NULL, NULL);
1143
1144 if (use_recirc) {
1145 bond_recirculation_account(bond);
1146 }
1147
f620b43a
BP
1148 /* Add each bond_entry to its slave's 'entries' list.
1149 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
1150 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1151 slave->tx_bytes = 0;
417e7e66 1152 ovs_list_init(&slave->entries);
f620b43a
BP
1153 }
1154 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
1155 if (e->slave && e->tx_bytes) {
1156 e->slave->tx_bytes += e->tx_bytes;
417e7e66 1157 ovs_list_push_back(&e->slave->entries, &e->list_node);
f620b43a
BP
1158 }
1159 }
1160
1161 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
1162 *
1163 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
1164 * with a proper list sort algorithm. */
417e7e66 1165 ovs_list_init(&bals);
f620b43a
BP
1166 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1167 if (slave->enabled) {
1168 insert_bal(&bals, slave);
1169 }
1170 }
1171 log_bals(bond, &bals);
1172
1173 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
417e7e66
BW
1174 while (!ovs_list_is_short(&bals)) {
1175 struct bond_slave *from = bond_slave_from_bal_node(ovs_list_front(&bals));
1176 struct bond_slave *to = bond_slave_from_bal_node(ovs_list_back(&bals));
f620b43a
BP
1177 uint64_t overload;
1178
1179 overload = from->tx_bytes - to->tx_bytes;
1180 if (overload < to->tx_bytes >> 5 || overload < 100000) {
1181 /* The extra load on 'from' (and all less-loaded slaves), compared
1182 * to that of 'to' (the least-loaded slave), is less than ~3%, or
1183 * it is less than ~1Mbps. No point in rebalancing. */
1184 break;
1185 }
1186
09a5d390
BP
1187 /* 'from' is carrying significantly more load than 'to'. Pick a hash
1188 * to move from 'from' to 'to'. */
f620b43a
BP
1189 e = choose_entry_to_migrate(from, to->tx_bytes);
1190 if (e) {
4a1b8f30 1191 bond_shift_load(e, to);
f620b43a
BP
1192
1193 /* Delete element from from->entries.
1194 *
1195 * We don't add the element to to->hashes. That would only allow
1196 * 'e' to be migrated to another slave in this rebalancing run, and
1197 * there is no point in doing that. */
417e7e66 1198 ovs_list_remove(&e->list_node);
f620b43a
BP
1199
1200 /* Re-sort 'bals'. */
1201 reinsert_bal(&bals, from);
1202 reinsert_bal(&bals, to);
60cda7d6 1203 rebalanced = true;
f620b43a
BP
1204 } else {
1205 /* Can't usefully migrate anything away from 'from'.
1206 * Don't reconsider it. */
417e7e66 1207 ovs_list_remove(&from->bal_node);
f620b43a
BP
1208 }
1209 }
1210
1211 /* Implement exponentially weighted moving average. A weight of 1/2 causes
1212 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
1213 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
1214 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
1215 e->tx_bytes /= 2;
f620b43a 1216 }
adcf00ba 1217
60cda7d6 1218 if (use_recirc && rebalanced) {
ca8127fd 1219 bond_update_post_recirc_rules__(bond,true);
60cda7d6 1220 }
2f486d4c
AZ
1221
1222done:
3bfd3972 1223 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1224}
1225\f
1226/* Bonding unixctl user interface functions. */
1227
1228static struct bond *
3bfd3972 1229bond_find(const char *name) OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
1230{
1231 struct bond *bond;
1232
1233 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
3bfd3972 1234 all_bonds) {
f620b43a
BP
1235 if (!strcmp(bond->name, name)) {
1236 return bond;
1237 }
1238 }
1239 return NULL;
1240}
1241
1242static struct bond_slave *
1243bond_lookup_slave(struct bond *bond, const char *slave_name)
1244{
1245 struct bond_slave *slave;
1246
1247 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1248 if (!strcmp(slave->name, slave_name)) {
1249 return slave;
1250 }
1251 }
1252 return NULL;
1253}
1254
1255static void
1256bond_unixctl_list(struct unixctl_conn *conn,
0e15264f
BP
1257 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1258 void *aux OVS_UNUSED)
f620b43a
BP
1259{
1260 struct ds ds = DS_EMPTY_INITIALIZER;
1261 const struct bond *bond;
1262
adcf00ba 1263 ds_put_cstr(&ds, "bond\ttype\trecircID\tslaves\n");
f620b43a 1264
3bfd3972
EJ
1265 ovs_rwlock_rdlock(&rwlock);
1266 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
f620b43a
BP
1267 const struct bond_slave *slave;
1268 size_t i;
1269
adcf00ba
AZ
1270 ds_put_format(&ds, "%s\t%s\t%d\t", bond->name,
1271 bond_mode_to_string(bond->balance), bond->recirc_id);
f620b43a
BP
1272
1273 i = 0;
1274 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1275 if (i++ > 0) {
1276 ds_put_cstr(&ds, ", ");
1277 }
1278 ds_put_cstr(&ds, slave->name);
1279 }
1280 ds_put_char(&ds, '\n');
1281 }
3bfd3972 1282 ovs_rwlock_unlock(&rwlock);
bde9f75d 1283 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a
BP
1284 ds_destroy(&ds);
1285}
1286
1287static void
c33a8a25 1288bond_print_details(struct ds *ds, const struct bond *bond)
3bfd3972 1289 OVS_REQ_RDLOCK(rwlock)
f620b43a 1290{
fc1d4f01
EJ
1291 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
1292 const struct shash_node **sorted_slaves = NULL;
f620b43a 1293 const struct bond_slave *slave;
adcf00ba
AZ
1294 bool may_recirc;
1295 uint32_t recirc_id;
fc1d4f01 1296 int i;
f620b43a 1297
c33a8a25
EJ
1298 ds_put_format(ds, "---- %s ----\n", bond->name);
1299 ds_put_format(ds, "bond_mode: %s\n",
f620b43a
BP
1300 bond_mode_to_string(bond->balance));
1301
adcf00ba
AZ
1302 may_recirc = bond_may_recirc(bond, &recirc_id, NULL);
1303 ds_put_format(ds, "bond may use recirculation: %s, Recirc-ID : %d\n",
1304 may_recirc ? "yes" : "no", may_recirc ? recirc_id: -1);
1305
c33a8a25 1306 ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis);
672d18b2 1307
c33a8a25
EJ
1308 ds_put_format(ds, "updelay: %d ms\n", bond->updelay);
1309 ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay);
f620b43a 1310
1b137691 1311 if (bond_is_balanced(bond)) {
c33a8a25 1312 ds_put_format(ds, "next rebalance: %lld ms\n",
f620b43a
BP
1313 bond->next_rebalance - time_msec());
1314 }
1315
bdebeece
EJ
1316 ds_put_cstr(ds, "lacp_status: ");
1317 switch (bond->lacp_status) {
1318 case LACP_NEGOTIATED:
1319 ds_put_cstr(ds, "negotiated\n");
1320 break;
1321 case LACP_CONFIGURED:
1322 ds_put_cstr(ds, "configured\n");
1323 break;
1324 case LACP_DISABLED:
1325 ds_put_cstr(ds, "off\n");
1326 break;
1327 default:
1328 ds_put_cstr(ds, "<unknown>\n");
1329 break;
1330 }
4d6fb5eb 1331
3e5aeeb5
AZ
1332 ds_put_cstr(ds, "active slave mac: ");
1333 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(bond->active_slave_mac));
1334 slave = bond_find_slave_by_mac(bond, bond->active_slave_mac);
1335 ds_put_format(ds,"(%s)\n", slave ? slave->name : "none");
1336
f620b43a 1337 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
fc1d4f01
EJ
1338 shash_add(&slave_shash, slave->name, slave);
1339 }
1340 sorted_slaves = shash_sort(&slave_shash);
1341
1342 for (i = 0; i < shash_count(&slave_shash); i++) {
f620b43a 1343 struct bond_entry *be;
f620b43a 1344
fc1d4f01
EJ
1345 slave = sorted_slaves[i]->data;
1346
f620b43a 1347 /* Basic info. */
c33a8a25 1348 ds_put_format(ds, "\nslave %s: %s\n",
f620b43a
BP
1349 slave->name, slave->enabled ? "enabled" : "disabled");
1350 if (slave == bond->active_slave) {
c33a8a25 1351 ds_put_cstr(ds, "\tactive slave\n");
f620b43a
BP
1352 }
1353 if (slave->delay_expires != LLONG_MAX) {
c33a8a25 1354 ds_put_format(ds, "\t%s expires in %lld ms\n",
f620b43a
BP
1355 slave->enabled ? "downdelay" : "updelay",
1356 slave->delay_expires - time_msec());
1357 }
1358
c33a8a25 1359 ds_put_format(ds, "\tmay_enable: %s\n",
296f6519 1360 slave->may_enable ? "true" : "false");
4d6fb5eb 1361
1b137691 1362 if (!bond_is_balanced(bond)) {
f620b43a
BP
1363 continue;
1364 }
1365
1366 /* Hashes. */
f620b43a
BP
1367 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1368 int hash = be - bond->hash;
f6ba1f35 1369 uint64_t be_tx_k;
f620b43a
BP
1370
1371 if (be->slave != slave) {
1372 continue;
1373 }
1374
f6ba1f35
AZ
1375 be_tx_k = be->tx_bytes / 1024;
1376 if (be_tx_k) {
1377 ds_put_format(ds, "\thash %d: %"PRIu64" kB load\n",
1378 hash, be_tx_k);
1379 }
f620b43a 1380
7b9f1974 1381 /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
f620b43a
BP
1382 }
1383 }
fc1d4f01
EJ
1384 shash_destroy(&slave_shash);
1385 free(sorted_slaves);
c33a8a25
EJ
1386 ds_put_cstr(ds, "\n");
1387}
1388
1389static void
1390bond_unixctl_show(struct unixctl_conn *conn,
1391 int argc, const char *argv[],
1392 void *aux OVS_UNUSED)
1393{
1394 struct ds ds = DS_EMPTY_INITIALIZER;
1395
3bfd3972 1396 ovs_rwlock_rdlock(&rwlock);
c33a8a25
EJ
1397 if (argc > 1) {
1398 const struct bond *bond = bond_find(argv[1]);
1399
1400 if (!bond) {
bde9f75d 1401 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1402 goto out;
c33a8a25
EJ
1403 }
1404 bond_print_details(&ds, bond);
1405 } else {
1406 const struct bond *bond;
1407
3bfd3972 1408 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
c33a8a25
EJ
1409 bond_print_details(&ds, bond);
1410 }
1411 }
1412
bde9f75d 1413 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a 1414 ds_destroy(&ds);
3bfd3972
EJ
1415
1416out:
1417 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1418}
1419
1420static void
0e15264f
BP
1421bond_unixctl_migrate(struct unixctl_conn *conn,
1422 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1423 void *aux OVS_UNUSED)
1424{
0e15264f
BP
1425 const char *bond_s = argv[1];
1426 const char *hash_s = argv[2];
1427 const char *slave_s = argv[3];
f620b43a
BP
1428 struct bond *bond;
1429 struct bond_slave *slave;
1430 struct bond_entry *entry;
1431 int hash;
1432
3bfd3972 1433 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1434 bond = bond_find(bond_s);
1435 if (!bond) {
bde9f75d 1436 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1437 goto out;
f620b43a
BP
1438 }
1439
1440 if (bond->balance != BM_SLB) {
bde9f75d 1441 unixctl_command_reply_error(conn, "not an SLB bond");
3bfd3972 1442 goto out;
f620b43a
BP
1443 }
1444
1445 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1446 hash = atoi(hash_s) & BOND_MASK;
1447 } else {
bde9f75d 1448 unixctl_command_reply_error(conn, "bad hash");
3bfd3972 1449 goto out;
f620b43a
BP
1450 }
1451
1452 slave = bond_lookup_slave(bond, slave_s);
1453 if (!slave) {
bde9f75d 1454 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1455 goto out;
f620b43a
BP
1456 }
1457
1458 if (!slave->enabled) {
bde9f75d 1459 unixctl_command_reply_error(conn, "cannot migrate to disabled slave");
3bfd3972 1460 goto out;
f620b43a
BP
1461 }
1462
1463 entry = &bond->hash[hash];
4a1b8f30 1464 bond->bond_revalidate = true;
f620b43a 1465 entry->slave = slave;
bde9f75d 1466 unixctl_command_reply(conn, "migrated");
3bfd3972
EJ
1467
1468out:
1469 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1470}
1471
1472static void
0e15264f
BP
1473bond_unixctl_set_active_slave(struct unixctl_conn *conn,
1474 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1475 void *aux OVS_UNUSED)
1476{
0e15264f
BP
1477 const char *bond_s = argv[1];
1478 const char *slave_s = argv[2];
f620b43a
BP
1479 struct bond *bond;
1480 struct bond_slave *slave;
1481
3bfd3972 1482 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1483 bond = bond_find(bond_s);
1484 if (!bond) {
bde9f75d 1485 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1486 goto out;
f620b43a
BP
1487 }
1488
1489 slave = bond_lookup_slave(bond, slave_s);
1490 if (!slave) {
bde9f75d 1491 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1492 goto out;
f620b43a
BP
1493 }
1494
1495 if (!slave->enabled) {
bde9f75d 1496 unixctl_command_reply_error(conn, "cannot make disabled slave active");
3bfd3972 1497 goto out;
f620b43a
BP
1498 }
1499
1500 if (bond->active_slave != slave) {
4a1b8f30 1501 bond->bond_revalidate = true;
f620b43a 1502 bond->active_slave = slave;
f620b43a
BP
1503 VLOG_INFO("bond %s: active interface is now %s",
1504 bond->name, slave->name);
1505 bond->send_learning_packets = true;
bde9f75d 1506 unixctl_command_reply(conn, "done");
3e5aeeb5 1507 bond_active_slave_changed(bond);
f620b43a 1508 } else {
bde9f75d 1509 unixctl_command_reply(conn, "no change");
f620b43a 1510 }
3bfd3972
EJ
1511out:
1512 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1513}
1514
1515static void
0e15264f 1516enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable)
f620b43a 1517{
0e15264f
BP
1518 const char *bond_s = argv[1];
1519 const char *slave_s = argv[2];
f620b43a
BP
1520 struct bond *bond;
1521 struct bond_slave *slave;
1522
3bfd3972 1523 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1524 bond = bond_find(bond_s);
1525 if (!bond) {
bde9f75d 1526 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1527 goto out;
f620b43a
BP
1528 }
1529
1530 slave = bond_lookup_slave(bond, slave_s);
1531 if (!slave) {
bde9f75d 1532 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1533 goto out;
f620b43a
BP
1534 }
1535
4a1b8f30 1536 bond_enable_slave(slave, enable);
bde9f75d 1537 unixctl_command_reply(conn, enable ? "enabled" : "disabled");
3bfd3972
EJ
1538
1539out:
1540 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1541}
1542
1543static void
0e15264f
BP
1544bond_unixctl_enable_slave(struct unixctl_conn *conn,
1545 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1546 void *aux OVS_UNUSED)
1547{
0e15264f 1548 enable_slave(conn, argv, true);
f620b43a
BP
1549}
1550
1551static void
0e15264f
BP
1552bond_unixctl_disable_slave(struct unixctl_conn *conn,
1553 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1554 void *aux OVS_UNUSED)
1555{
0e15264f 1556 enable_slave(conn, argv, false);
f620b43a
BP
1557}
1558
1559static void
0e15264f 1560bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[],
f620b43a
BP
1561 void *aux OVS_UNUSED)
1562{
0e15264f
BP
1563 const char *mac_s = argv[1];
1564 const char *vlan_s = argc > 2 ? argv[2] : NULL;
1565 const char *basis_s = argc > 3 ? argv[3] : NULL;
74ff3298 1566 struct eth_addr mac;
f620b43a
BP
1567 uint8_t hash;
1568 char *hash_cstr;
1569 unsigned int vlan;
672d18b2 1570 uint32_t basis;
f620b43a
BP
1571
1572 if (vlan_s) {
c2c28dfd 1573 if (!ovs_scan(vlan_s, "%u", &vlan)) {
bde9f75d 1574 unixctl_command_reply_error(conn, "invalid vlan");
f620b43a
BP
1575 return;
1576 }
1577 } else {
dc155bff 1578 vlan = 0;
f620b43a
BP
1579 }
1580
672d18b2 1581 if (basis_s) {
c2c28dfd 1582 if (!ovs_scan(basis_s, "%"SCNu32, &basis)) {
bde9f75d 1583 unixctl_command_reply_error(conn, "invalid basis");
672d18b2
EJ
1584 return;
1585 }
1586 } else {
1587 basis = 0;
1588 }
1589
c2c28dfd 1590 if (ovs_scan(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
672d18b2 1591 hash = bond_hash_src(mac, vlan, basis) & BOND_MASK;
f620b43a
BP
1592
1593 hash_cstr = xasprintf("%u", hash);
bde9f75d 1594 unixctl_command_reply(conn, hash_cstr);
f620b43a
BP
1595 free(hash_cstr);
1596 } else {
bde9f75d 1597 unixctl_command_reply_error(conn, "invalid mac");
f620b43a
BP
1598 }
1599}
1600
1601void
1602bond_init(void)
1603{
0e15264f 1604 unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL);
c33a8a25
EJ
1605 unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show,
1606 NULL);
0e15264f 1607 unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
7ff2009a 1608 bond_unixctl_migrate, NULL);
0e15264f 1609 unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
f620b43a 1610 bond_unixctl_set_active_slave, NULL);
0e15264f 1611 unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
7ff2009a 1612 bond_unixctl_enable_slave, NULL);
0e15264f 1613 unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
7ff2009a 1614 bond_unixctl_disable_slave, NULL);
0e15264f 1615 unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
7ff2009a 1616 bond_unixctl_hash, NULL);
f620b43a
BP
1617}
1618\f
95aafb2a
EJ
1619static void
1620bond_entry_reset(struct bond *bond)
1621{
1622 if (bond->balance != BM_AB) {
9e1a6910 1623 size_t hash_len = BOND_BUCKETS * sizeof *bond->hash;
95aafb2a
EJ
1624
1625 if (!bond->hash) {
1626 bond->hash = xmalloc(hash_len);
1627 }
1628 memset(bond->hash, 0, hash_len);
1629
1630 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1631 } else {
1632 free(bond->hash);
1633 bond->hash = NULL;
1634 }
1635}
1636
f620b43a
BP
1637static struct bond_slave *
1638bond_slave_lookup(struct bond *bond, const void *slave_)
1639{
1640 struct bond_slave *slave;
1641
1642 HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1643 &bond->slaves) {
1644 if (slave->aux == slave_) {
1645 return slave;
1646 }
1647 }
1648
1649 return NULL;
1650}
1651
f620b43a 1652static void
4a1b8f30 1653bond_enable_slave(struct bond_slave *slave, bool enable)
f620b43a
BP
1654{
1655 slave->delay_expires = LLONG_MAX;
1656 if (enable != slave->enabled) {
4a1b8f30 1657 slave->bond->bond_revalidate = true;
f620b43a 1658 slave->enabled = enable;
f1c8a79c
AW
1659
1660 ovs_mutex_lock(&slave->bond->mutex);
1661 if (enable) {
417e7e66 1662 ovs_list_insert(&slave->bond->enabled_slaves, &slave->list_node);
f1c8a79c 1663 } else {
417e7e66 1664 ovs_list_remove(&slave->list_node);
f1c8a79c
AW
1665 }
1666 ovs_mutex_unlock(&slave->bond->mutex);
1667
4a1b8f30
EJ
1668 VLOG_INFO("interface %s: %s", slave->name,
1669 slave->enabled ? "enabled" : "disabled");
f620b43a
BP
1670 }
1671}
1672
1673static void
4a1b8f30 1674bond_link_status_update(struct bond_slave *slave)
f620b43a
BP
1675{
1676 struct bond *bond = slave->bond;
1677 bool up;
1678
296f6519 1679 up = netdev_get_carrier(slave->netdev) && slave->may_enable;
f620b43a
BP
1680 if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1681 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1682 VLOG_INFO_RL(&rl, "interface %s: link state %s",
1683 slave->name, up ? "up" : "down");
1684 if (up == slave->enabled) {
1685 slave->delay_expires = LLONG_MAX;
1686 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1687 slave->name, up ? "disabled" : "enabled");
1688 } else {
bdebeece 1689 int delay = (bond->lacp_status != LACP_DISABLED ? 0
f620b43a
BP
1690 : up ? bond->updelay : bond->downdelay);
1691 slave->delay_expires = time_msec() + delay;
1692 if (delay) {
1693 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1694 "for %d ms",
1695 slave->name,
1696 up ? "enabled" : "disabled",
1697 up ? "up" : "down",
1698 delay);
1699 }
1700 }
1701 }
1702
1703 if (time_msec() >= slave->delay_expires) {
4a1b8f30 1704 bond_enable_slave(slave, up);
f620b43a
BP
1705 }
1706}
1707
f620b43a 1708static unsigned int
74ff3298 1709bond_hash_src(const struct eth_addr mac, uint16_t vlan, uint32_t basis)
f620b43a 1710{
7e36ac42 1711 return hash_mac(mac, vlan, basis);
f620b43a
BP
1712}
1713
1714static unsigned int
672d18b2 1715bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis)
f620b43a
BP
1716{
1717 struct flow hash_flow = *flow;
d84d4b88 1718 hash_flow.vlan_tci = htons(vlan);
f620b43a
BP
1719
1720 /* The symmetric quality of this hash function is not required, but
1721 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1722 * purposes, so we use it out of convenience. */
672d18b2 1723 return flow_hash_symmetric_l4(&hash_flow, basis);
f620b43a
BP
1724}
1725
fb0b29a3
EJ
1726static unsigned int
1727bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1728{
cb22974d 1729 ovs_assert(bond->balance == BM_TCP || bond->balance == BM_SLB);
fb0b29a3 1730
bdebeece 1731 return (bond->balance == BM_TCP
672d18b2
EJ
1732 ? bond_hash_tcp(flow, vlan, bond->basis)
1733 : bond_hash_src(flow->dl_src, vlan, bond->basis));
fb0b29a3
EJ
1734}
1735
f620b43a
BP
1736static struct bond_entry *
1737lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1738 uint16_t vlan)
1739{
fb0b29a3 1740 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
f620b43a
BP
1741}
1742
f1c8a79c
AW
1743/* Selects and returns an enabled slave from the 'enabled_slaves' list
1744 * in a round-robin fashion. If the 'enabled_slaves' list is empty,
1745 * returns NULL. */
1746static struct bond_slave *
1747get_enabled_slave(struct bond *bond)
1748{
ca6ba700 1749 struct ovs_list *node;
f1c8a79c
AW
1750
1751 ovs_mutex_lock(&bond->mutex);
417e7e66 1752 if (ovs_list_is_empty(&bond->enabled_slaves)) {
f1c8a79c
AW
1753 ovs_mutex_unlock(&bond->mutex);
1754 return NULL;
1755 }
1756
417e7e66
BW
1757 node = ovs_list_pop_front(&bond->enabled_slaves);
1758 ovs_list_push_back(&bond->enabled_slaves, node);
f1c8a79c
AW
1759 ovs_mutex_unlock(&bond->mutex);
1760
1761 return CONTAINER_OF(node, struct bond_slave, list_node);
1762}
1763
f620b43a
BP
1764static struct bond_slave *
1765choose_output_slave(const struct bond *bond, const struct flow *flow,
4a1b8f30 1766 struct flow_wildcards *wc, uint16_t vlan)
f620b43a
BP
1767{
1768 struct bond_entry *e;
9dd165e0 1769 int balance;
f620b43a 1770
9dd165e0 1771 balance = bond->balance;
bdebeece
EJ
1772 if (bond->lacp_status == LACP_CONFIGURED) {
1773 /* LACP has been configured on this bond but negotiations were
9dd165e0
RK
1774 * unsuccussful. If lacp_fallback_ab is enabled use active-
1775 * backup mode else drop all traffic. */
1776 if (!bond->lacp_fallback_ab) {
1777 return NULL;
1778 }
1779 balance = BM_AB;
bdebeece
EJ
1780 }
1781
9dd165e0 1782 switch (balance) {
f620b43a
BP
1783 case BM_AB:
1784 return bond->active_slave;
1785
f620b43a 1786 case BM_TCP:
bdebeece
EJ
1787 if (bond->lacp_status != LACP_NEGOTIATED) {
1788 /* Must have LACP negotiations for TCP balanced bonds. */
1789 return NULL;
1790 }
bcd2633a 1791 if (wc) {
6cdd5145 1792 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
bcd2633a 1793 }
bdebeece
EJ
1794 /* Fall Through. */
1795 case BM_SLB:
bcd2633a 1796 if (wc) {
6cdd5145 1797 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_ETH_SRC);
bcd2633a 1798 }
f620b43a
BP
1799 e = lookup_bond_entry(bond, flow, vlan);
1800 if (!e->slave || !e->slave->enabled) {
f1c8a79c 1801 e->slave = get_enabled_slave(CONST_CAST(struct bond*, bond));
f620b43a
BP
1802 }
1803 return e->slave;
1804
1805 default:
428b2edd 1806 OVS_NOT_REACHED();
f620b43a
BP
1807 }
1808}
1809
1810static struct bond_slave *
1811bond_choose_slave(const struct bond *bond)
1812{
1813 struct bond_slave *slave, *best;
1814
3e5aeeb5
AZ
1815 /* Find the last active slave. */
1816 slave = bond_find_slave_by_mac(bond, bond->active_slave_mac);
1817 if (slave && slave->enabled) {
1818 return slave;
1819 }
1820
f620b43a
BP
1821 /* Find an enabled slave. */
1822 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1823 if (slave->enabled) {
1824 return slave;
1825 }
1826 }
1827
1828 /* All interfaces are disabled. Find an interface that will be enabled
1829 * after its updelay expires. */
1830 best = NULL;
1831 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1832 if (slave->delay_expires != LLONG_MAX
296f6519 1833 && slave->may_enable
f620b43a
BP
1834 && (!best || slave->delay_expires < best->delay_expires)) {
1835 best = slave;
1836 }
1837 }
1838 return best;
1839}
1840
1841static void
4a1b8f30 1842bond_choose_active_slave(struct bond *bond)
f620b43a
BP
1843{
1844 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1845 struct bond_slave *old_active_slave = bond->active_slave;
1846
1847 bond->active_slave = bond_choose_slave(bond);
1848 if (bond->active_slave) {
1849 if (bond->active_slave->enabled) {
1850 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1851 bond->name, bond->active_slave->name);
1852 } else {
1853 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1854 "remaining %lld ms updelay (since no interface was "
1855 "enabled)", bond->name, bond->active_slave->name,
1856 bond->active_slave->delay_expires - time_msec());
4a1b8f30 1857 bond_enable_slave(bond->active_slave, true);
f620b43a
BP
1858 }
1859
1860 bond->send_learning_packets = true;
3e5aeeb5
AZ
1861
1862 if (bond->active_slave != old_active_slave) {
1863 bond_active_slave_changed(bond);
1864 }
f620b43a 1865 } else if (old_active_slave) {
d28b9ead 1866 VLOG_INFO_RL(&rl, "bond %s: all interfaces disabled", bond->name);
f620b43a
BP
1867 }
1868}
3e5aeeb5
AZ
1869
1870/*
1871 * Return true if bond has unstored active slave change.
1872 * If return true, 'mac' will store the bond's current active slave's
1873 * MAC address. */
1874bool
74ff3298
JR
1875bond_get_changed_active_slave(const char *name, struct eth_addr *mac,
1876 bool force)
3e5aeeb5
AZ
1877{
1878 struct bond *bond;
1879
1880 ovs_rwlock_wrlock(&rwlock);
1881 bond = bond_find(name);
1882 if (bond) {
1883 if (bond->active_slave_changed || force) {
74ff3298 1884 *mac = bond->active_slave_mac;
3e5aeeb5
AZ
1885 bond->active_slave_changed = false;
1886 ovs_rwlock_unlock(&rwlock);
1887 return true;
1888 }
1889 }
1890 ovs_rwlock_unlock(&rwlock);
1891
1892 return false;
1893}