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