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