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