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