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