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