]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/bond.c
stp: Add link-state checking support for stp ports.
[mirror_ovs.git] / ofproto / bond.c
CommitLineData
f620b43a 1/*
07a3cd5c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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
AZ
374 if (error) {
375 char *err_s = match_to_string(&pr_op->match,
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) {
388 char *err_s = match_to_string(&pr_op->match,
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 }
830
f931a4c9
BP
831 case BM_AB:
832 /* Drop all packets which arrive on backup slaves. This is similar to
833 * how Linux bonding handles active-backup bonds. */
7ba7dcf0
EJ
834 if (bond->active_slave != slave) {
835 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
836
e6b2255c
BP
837 VLOG_DBG_RL(&rl, "active-backup bond received packet on backup"
838 " slave (%s) destined for " ETH_ADDR_FMT,
839 slave->name, ETH_ADDR_ARGS(eth_dst));
3bfd3972 840 goto out;
7ba7dcf0 841 }
3bfd3972
EJ
842 verdict = BV_ACCEPT;
843 goto out;
f931a4c9 844
f931a4c9
BP
845 case BM_SLB:
846 /* Drop all packets for which we have learned a different input port,
847 * because we probably sent the packet on one slave and got it back on
848 * the other. Gratuitous ARP packets are an exception to this rule:
849 * the host has moved to another switch. The exception to the
850 * exception is if we locked the learning table to avoid reflections on
851 * bond slaves. */
3bfd3972
EJ
852 verdict = BV_DROP_IF_MOVED;
853 goto out;
7ba7dcf0
EJ
854 }
855
428b2edd 856 OVS_NOT_REACHED();
3bfd3972
EJ
857out:
858 ovs_rwlock_unlock(&rwlock);
859 return verdict;
860
f620b43a
BP
861}
862
863/* Returns the slave (registered on 'bond' by bond_slave_register()) to which
864 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
865 * NULL if the packet should be dropped because no slaves are enabled.
866 *
867 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
868 * should be a VID only (i.e. excluding the PCP bits). Second,
869 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
870 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
871 * packet belongs to (so for an access port it will be the access port's VLAN).
872 *
bcd2633a
JP
873 * If 'wc' is non-NULL, bitwise-OR's 'wc' with the set of bits that were
874 * significant in the selection. At some point earlier, 'wc' should
875 * have been initialized (e.g., by flow_wildcards_init_catchall()).
f620b43a
BP
876 */
877void *
878bond_choose_output_slave(struct bond *bond, const struct flow *flow,
4a1b8f30 879 struct flow_wildcards *wc, uint16_t vlan)
f620b43a 880{
3bfd3972 881 struct bond_slave *slave;
b5d5d7d3 882 void *aux;
3bfd3972
EJ
883
884 ovs_rwlock_rdlock(&rwlock);
4a1b8f30 885 slave = choose_output_slave(bond, flow, wc, vlan);
b5d5d7d3 886 aux = slave ? slave->aux : NULL;
3bfd3972 887 ovs_rwlock_unlock(&rwlock);
b5d5d7d3
AW
888
889 return aux;
f620b43a 890}
f620b43a 891\f
adcf00ba
AZ
892/* Recirculation. */
893static void
894bond_entry_account(struct bond_entry *entry, uint64_t rule_tx_bytes)
c6855ec5 895 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
896{
897 if (entry->slave) {
898 uint64_t delta;
899
900 delta = rule_tx_bytes - entry->pr_tx_bytes;
901 entry->tx_bytes += delta;
902 entry->pr_tx_bytes = rule_tx_bytes;
903 }
904}
905
906/* Maintain bond stats using post recirculation rule byte counters.*/
60cda7d6 907static void
adcf00ba 908bond_recirculation_account(struct bond *bond)
80316557 909 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
910{
911 int i;
912
adcf00ba
AZ
913 for (i=0; i<=BOND_MASK; i++) {
914 struct bond_entry *entry = &bond->hash[i];
915 struct rule *rule = entry->pr_rule;
916
917 if (rule) {
918 uint64_t n_packets OVS_UNUSED;
919 long long int used OVS_UNUSED;
920 uint64_t n_bytes;
921
922 rule->ofproto->ofproto_class->rule_get_stats(
923 rule, &n_packets, &n_bytes, &used);
924 bond_entry_account(entry, n_bytes);
925 }
926 }
adcf00ba
AZ
927}
928
a80aba3a 929static bool
6b95d23c 930bond_may_recirc(const struct bond *bond)
adcf00ba 931{
6b95d23c 932 return bond->balance == BM_TCP && bond->recirc_id;
adcf00ba
AZ
933}
934
ca8127fd
AZ
935static void
936bond_update_post_recirc_rules__(struct bond* bond, const bool force)
937 OVS_REQ_WRLOCK(rwlock)
adcf00ba
AZ
938{
939 struct bond_entry *e;
940 bool update_rules = force; /* Always update rules if caller forces it. */
941
942 /* Make sure all bond entries are populated */
943 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
944 if (!e->slave || !e->slave->enabled) {
945 update_rules = true;
946 e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
947 struct bond_slave, hmap_node);
948 if (!e->slave->enabled) {
949 e->slave = bond->active_slave;
950 }
951 }
952 }
953
954 if (update_rules) {
955 update_recirc_rules(bond);
956 }
957}
ca8127fd
AZ
958
959void
82f9f1f5
AZ
960bond_update_post_recirc_rules(struct bond *bond, uint32_t *recirc_id,
961 uint32_t *hash_basis)
ca8127fd 962{
a80aba3a
AZ
963 bool may_recirc = bond_may_recirc(bond);
964
965 if (may_recirc) {
966 /* To avoid unnecessary locking, bond_may_recirc() is first
967 * called outside of the 'rwlock'. After acquiring the lock,
968 * check again to make sure bond configuration has not been changed. */
969 ovs_rwlock_wrlock(&rwlock);
970 may_recirc = bond_may_recirc(bond);
971 if (may_recirc) {
972 *recirc_id = bond->recirc_id;
973 *hash_basis = bond->basis;
974 bond_update_post_recirc_rules__(bond, false);
975 }
976 ovs_rwlock_unlock(&rwlock);
977 }
978
979 if (!may_recirc) {
6b95d23c 980 *recirc_id = *hash_basis = 0;
82f9f1f5 981 }
ca8127fd 982}
82f9f1f5 983
adcf00ba 984\f
f620b43a
BP
985/* Rebalancing. */
986
1b137691 987static bool
3bfd3972 988bond_is_balanced(const struct bond *bond) OVS_REQ_RDLOCK(rwlock)
1b137691 989{
bc1b010c
EJ
990 return bond->rebalance_interval
991 && (bond->balance == BM_SLB || bond->balance == BM_TCP);
1b137691
EJ
992}
993
f620b43a
BP
994/* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
995void
996bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
997 uint64_t n_bytes)
998{
3bfd3972 999 ovs_rwlock_wrlock(&rwlock);
1b137691 1000 if (bond_is_balanced(bond)) {
f620b43a 1001 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
f620b43a 1002 }
3bfd3972 1003 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1004}
1005
1006static struct bond_slave *
ca6ba700 1007bond_slave_from_bal_node(struct ovs_list *bal) OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
1008{
1009 return CONTAINER_OF(bal, struct bond_slave, bal_node);
1010}
1011
1012static void
ca6ba700 1013log_bals(struct bond *bond, const struct ovs_list *bals)
c6855ec5 1014 OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
1015{
1016 if (VLOG_IS_DBG_ENABLED()) {
1017 struct ds ds = DS_EMPTY_INITIALIZER;
1018 const struct bond_slave *slave;
1019
1020 LIST_FOR_EACH (slave, bal_node, bals) {
1021 if (ds.length) {
1022 ds_put_char(&ds, ',');
1023 }
1024 ds_put_format(&ds, " %s %"PRIu64"kB",
1025 slave->name, slave->tx_bytes / 1024);
1026
1027 if (!slave->enabled) {
1028 ds_put_cstr(&ds, " (disabled)");
1029 }
417e7e66 1030 if (!ovs_list_is_empty(&slave->entries)) {
f620b43a
BP
1031 struct bond_entry *e;
1032
1033 ds_put_cstr(&ds, " (");
1034 LIST_FOR_EACH (e, list_node, &slave->entries) {
417e7e66 1035 if (&e->list_node != ovs_list_front(&slave->entries)) {
f620b43a
BP
1036 ds_put_cstr(&ds, " + ");
1037 }
34582733 1038 ds_put_format(&ds, "h%"PRIdPTR": %"PRIu64"kB",
f620b43a
BP
1039 e - bond->hash, e->tx_bytes / 1024);
1040 }
1041 ds_put_cstr(&ds, ")");
1042 }
1043 }
1044 VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
1045 ds_destroy(&ds);
1046 }
1047}
1048
1049/* Shifts 'hash' from its current slave to 'to'. */
1050static void
4a1b8f30 1051bond_shift_load(struct bond_entry *hash, struct bond_slave *to)
c6855ec5 1052 OVS_REQ_WRLOCK(rwlock)
f620b43a
BP
1053{
1054 struct bond_slave *from = hash->slave;
1055 struct bond *bond = from->bond;
1056 uint64_t delta = hash->tx_bytes;
1057
34582733 1058 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %"PRIdPTR") "
f620b43a
BP
1059 "from %s to %s (now carrying %"PRIu64"kB and "
1060 "%"PRIu64"kB load, respectively)",
1061 bond->name, delta / 1024, hash - bond->hash,
1062 from->name, to->name,
1063 (from->tx_bytes - delta) / 1024,
1064 (to->tx_bytes + delta) / 1024);
1065
1066 /* Shift load away from 'from' to 'to'. */
1067 from->tx_bytes -= delta;
1068 to->tx_bytes += delta;
1069
1070 /* Arrange for flows to be revalidated. */
dc30ea2d 1071 hash->slave = to;
4a1b8f30 1072 bond->bond_revalidate = true;
f620b43a
BP
1073}
1074
09a5d390
BP
1075/* Picks and returns a bond_entry to migrate from 'from' (the most heavily
1076 * loaded bond slave) to a bond slave that has 'to_tx_bytes' bytes of load,
f620b43a
BP
1077 * given that doing so must decrease the ratio of the load on the two slaves by
1078 * at least 0.1. Returns NULL if there is no appropriate entry.
1079 *
1080 * The list of entries isn't sorted. I don't know of a reason to prefer to
1081 * shift away small hashes or large hashes. */
1082static struct bond_entry *
1083choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
c6855ec5 1084 OVS_REQ_WRLOCK(rwlock)
f620b43a
BP
1085{
1086 struct bond_entry *e;
1087
417e7e66 1088 if (ovs_list_is_short(&from->entries)) {
f620b43a
BP
1089 /* 'from' carries no more than one MAC hash, so shifting load away from
1090 * it would be pointless. */
1091 return NULL;
1092 }
1093
1094 LIST_FOR_EACH (e, list_node, &from->entries) {
c460a6a7
AZ
1095 uint64_t delta = e->tx_bytes; /* The amount to rebalance. */
1096 uint64_t ideal_tx_bytes = (from->tx_bytes + to_tx_bytes)/2;
1097 /* Note, the ideal traffic is the mid point
1098 * between 'from' and 'to'. This value does
1099 * not change by rebalancing. */
1100 uint64_t new_low; /* The lower bandwidth between 'to' and 'from'
1101 after rebalancing. */
1102
1103 new_low = MIN(from->tx_bytes - delta, to_tx_bytes + delta);
1104
1105 if ((new_low > to_tx_bytes) &&
1106 (new_low - to_tx_bytes >= (ideal_tx_bytes - to_tx_bytes) / 10)) {
1107 /* Only rebalance if the new 'low' is closer to to the mid point,
1108 * and the improvement exceeds 10% of current traffic
1109 * deviation from the ideal split.
1110 *
1111 * The improvement on the 'high' side is always the same as the
1112 * 'low' side. Thus consider 'low' side is sufficient. */
f620b43a
BP
1113 return e;
1114 }
1115 }
1116
1117 return NULL;
1118}
1119
1120/* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
1121 * maintained. */
1122static void
ca6ba700 1123insert_bal(struct ovs_list *bals, struct bond_slave *slave)
f620b43a
BP
1124{
1125 struct bond_slave *pos;
1126
1127 LIST_FOR_EACH (pos, bal_node, bals) {
1128 if (slave->tx_bytes > pos->tx_bytes) {
1129 break;
1130 }
1131 }
417e7e66 1132 ovs_list_insert(&pos->bal_node, &slave->bal_node);
f620b43a
BP
1133}
1134
1135/* Removes 'slave' from its current list and then inserts it into 'bals' so
1136 * that descending order of 'tx_bytes' is maintained. */
1137static void
ca6ba700 1138reinsert_bal(struct ovs_list *bals, struct bond_slave *slave)
f620b43a 1139{
417e7e66 1140 ovs_list_remove(&slave->bal_node);
f620b43a
BP
1141 insert_bal(bals, slave);
1142}
1143
1144/* If 'bond' needs rebalancing, does so.
1145 *
adcf00ba
AZ
1146 * The caller should have called bond_account() for each active flow, or in case
1147 * of recirculation is used, have called bond_recirculation_account(bond),
1148 * to ensure that flow data is consistently accounted at this point.
60cda7d6
AZ
1149 */
1150void
4a1b8f30 1151bond_rebalance(struct bond *bond)
f620b43a
BP
1152{
1153 struct bond_slave *slave;
1154 struct bond_entry *e;
ca6ba700 1155 struct ovs_list bals;
adcf00ba 1156 bool rebalanced = false;
60cda7d6 1157 bool use_recirc;
f620b43a 1158
3bfd3972 1159 ovs_rwlock_wrlock(&rwlock);
1b137691 1160 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
adcf00ba 1161 goto done;
f620b43a
BP
1162 }
1163 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1164
07a3cd5c 1165 use_recirc = bond->ofproto->backer->support.odp.recirc &&
6b95d23c 1166 bond_may_recirc(bond);
60cda7d6
AZ
1167
1168 if (use_recirc) {
1169 bond_recirculation_account(bond);
1170 }
1171
f620b43a
BP
1172 /* Add each bond_entry to its slave's 'entries' list.
1173 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
1174 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1175 slave->tx_bytes = 0;
417e7e66 1176 ovs_list_init(&slave->entries);
f620b43a
BP
1177 }
1178 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
1179 if (e->slave && e->tx_bytes) {
1180 e->slave->tx_bytes += e->tx_bytes;
417e7e66 1181 ovs_list_push_back(&e->slave->entries, &e->list_node);
f620b43a
BP
1182 }
1183 }
1184
1185 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
1186 *
1187 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
1188 * with a proper list sort algorithm. */
417e7e66 1189 ovs_list_init(&bals);
f620b43a
BP
1190 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1191 if (slave->enabled) {
1192 insert_bal(&bals, slave);
1193 }
1194 }
1195 log_bals(bond, &bals);
1196
1197 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
417e7e66
BW
1198 while (!ovs_list_is_short(&bals)) {
1199 struct bond_slave *from = bond_slave_from_bal_node(ovs_list_front(&bals));
1200 struct bond_slave *to = bond_slave_from_bal_node(ovs_list_back(&bals));
f620b43a
BP
1201 uint64_t overload;
1202
1203 overload = from->tx_bytes - to->tx_bytes;
1204 if (overload < to->tx_bytes >> 5 || overload < 100000) {
1205 /* The extra load on 'from' (and all less-loaded slaves), compared
1206 * to that of 'to' (the least-loaded slave), is less than ~3%, or
1207 * it is less than ~1Mbps. No point in rebalancing. */
1208 break;
1209 }
1210
09a5d390
BP
1211 /* 'from' is carrying significantly more load than 'to'. Pick a hash
1212 * to move from 'from' to 'to'. */
f620b43a
BP
1213 e = choose_entry_to_migrate(from, to->tx_bytes);
1214 if (e) {
4a1b8f30 1215 bond_shift_load(e, to);
f620b43a
BP
1216
1217 /* Delete element from from->entries.
1218 *
1219 * We don't add the element to to->hashes. That would only allow
1220 * 'e' to be migrated to another slave in this rebalancing run, and
1221 * there is no point in doing that. */
417e7e66 1222 ovs_list_remove(&e->list_node);
f620b43a
BP
1223
1224 /* Re-sort 'bals'. */
1225 reinsert_bal(&bals, from);
1226 reinsert_bal(&bals, to);
60cda7d6 1227 rebalanced = true;
f620b43a
BP
1228 } else {
1229 /* Can't usefully migrate anything away from 'from'.
1230 * Don't reconsider it. */
417e7e66 1231 ovs_list_remove(&from->bal_node);
f620b43a
BP
1232 }
1233 }
1234
1235 /* Implement exponentially weighted moving average. A weight of 1/2 causes
1236 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
1237 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
1238 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
1239 e->tx_bytes /= 2;
f620b43a 1240 }
adcf00ba 1241
60cda7d6 1242 if (use_recirc && rebalanced) {
ca8127fd 1243 bond_update_post_recirc_rules__(bond,true);
60cda7d6 1244 }
2f486d4c
AZ
1245
1246done:
3bfd3972 1247 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1248}
1249\f
1250/* Bonding unixctl user interface functions. */
1251
1252static struct bond *
3bfd3972 1253bond_find(const char *name) OVS_REQ_RDLOCK(rwlock)
f620b43a
BP
1254{
1255 struct bond *bond;
1256
1257 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
3bfd3972 1258 all_bonds) {
f620b43a
BP
1259 if (!strcmp(bond->name, name)) {
1260 return bond;
1261 }
1262 }
1263 return NULL;
1264}
1265
1266static struct bond_slave *
1267bond_lookup_slave(struct bond *bond, const char *slave_name)
1268{
1269 struct bond_slave *slave;
1270
1271 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1272 if (!strcmp(slave->name, slave_name)) {
1273 return slave;
1274 }
1275 }
1276 return NULL;
1277}
1278
1279static void
1280bond_unixctl_list(struct unixctl_conn *conn,
0e15264f
BP
1281 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1282 void *aux OVS_UNUSED)
f620b43a
BP
1283{
1284 struct ds ds = DS_EMPTY_INITIALIZER;
1285 const struct bond *bond;
1286
adcf00ba 1287 ds_put_cstr(&ds, "bond\ttype\trecircID\tslaves\n");
f620b43a 1288
3bfd3972
EJ
1289 ovs_rwlock_rdlock(&rwlock);
1290 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
f620b43a
BP
1291 const struct bond_slave *slave;
1292 size_t i;
1293
adcf00ba
AZ
1294 ds_put_format(&ds, "%s\t%s\t%d\t", bond->name,
1295 bond_mode_to_string(bond->balance), bond->recirc_id);
f620b43a
BP
1296
1297 i = 0;
1298 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1299 if (i++ > 0) {
1300 ds_put_cstr(&ds, ", ");
1301 }
1302 ds_put_cstr(&ds, slave->name);
1303 }
1304 ds_put_char(&ds, '\n');
1305 }
3bfd3972 1306 ovs_rwlock_unlock(&rwlock);
bde9f75d 1307 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a
BP
1308 ds_destroy(&ds);
1309}
1310
1311static void
c33a8a25 1312bond_print_details(struct ds *ds, const struct bond *bond)
3bfd3972 1313 OVS_REQ_RDLOCK(rwlock)
f620b43a 1314{
fc1d4f01
EJ
1315 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
1316 const struct shash_node **sorted_slaves = NULL;
f620b43a 1317 const struct bond_slave *slave;
adcf00ba
AZ
1318 bool may_recirc;
1319 uint32_t recirc_id;
fc1d4f01 1320 int i;
f620b43a 1321
c33a8a25
EJ
1322 ds_put_format(ds, "---- %s ----\n", bond->name);
1323 ds_put_format(ds, "bond_mode: %s\n",
f620b43a
BP
1324 bond_mode_to_string(bond->balance));
1325
6b95d23c
AZ
1326 may_recirc = bond_may_recirc(bond);
1327 recirc_id = bond->recirc_id;
adcf00ba
AZ
1328 ds_put_format(ds, "bond may use recirculation: %s, Recirc-ID : %d\n",
1329 may_recirc ? "yes" : "no", may_recirc ? recirc_id: -1);
1330
c33a8a25 1331 ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis);
672d18b2 1332
c33a8a25
EJ
1333 ds_put_format(ds, "updelay: %d ms\n", bond->updelay);
1334 ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay);
f620b43a 1335
1b137691 1336 if (bond_is_balanced(bond)) {
c33a8a25 1337 ds_put_format(ds, "next rebalance: %lld ms\n",
f620b43a
BP
1338 bond->next_rebalance - time_msec());
1339 }
1340
bdebeece
EJ
1341 ds_put_cstr(ds, "lacp_status: ");
1342 switch (bond->lacp_status) {
1343 case LACP_NEGOTIATED:
1344 ds_put_cstr(ds, "negotiated\n");
1345 break;
1346 case LACP_CONFIGURED:
1347 ds_put_cstr(ds, "configured\n");
1348 break;
1349 case LACP_DISABLED:
1350 ds_put_cstr(ds, "off\n");
1351 break;
1352 default:
1353 ds_put_cstr(ds, "<unknown>\n");
1354 break;
1355 }
4d6fb5eb 1356
57fc4fd0 1357 ds_put_format(ds, "lacp_fallback_ab: %s\n",
1358 bond->lacp_fallback_ab ? "true" : "false");
1359
3e5aeeb5
AZ
1360 ds_put_cstr(ds, "active slave mac: ");
1361 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(bond->active_slave_mac));
1362 slave = bond_find_slave_by_mac(bond, bond->active_slave_mac);
1363 ds_put_format(ds,"(%s)\n", slave ? slave->name : "none");
1364
f620b43a 1365 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
fc1d4f01
EJ
1366 shash_add(&slave_shash, slave->name, slave);
1367 }
1368 sorted_slaves = shash_sort(&slave_shash);
1369
1370 for (i = 0; i < shash_count(&slave_shash); i++) {
f620b43a 1371 struct bond_entry *be;
f620b43a 1372
fc1d4f01
EJ
1373 slave = sorted_slaves[i]->data;
1374
f620b43a 1375 /* Basic info. */
c33a8a25 1376 ds_put_format(ds, "\nslave %s: %s\n",
f620b43a
BP
1377 slave->name, slave->enabled ? "enabled" : "disabled");
1378 if (slave == bond->active_slave) {
c33a8a25 1379 ds_put_cstr(ds, "\tactive slave\n");
f620b43a
BP
1380 }
1381 if (slave->delay_expires != LLONG_MAX) {
c33a8a25 1382 ds_put_format(ds, "\t%s expires in %lld ms\n",
f620b43a
BP
1383 slave->enabled ? "downdelay" : "updelay",
1384 slave->delay_expires - time_msec());
1385 }
1386
c33a8a25 1387 ds_put_format(ds, "\tmay_enable: %s\n",
296f6519 1388 slave->may_enable ? "true" : "false");
4d6fb5eb 1389
1b137691 1390 if (!bond_is_balanced(bond)) {
f620b43a
BP
1391 continue;
1392 }
1393
1394 /* Hashes. */
f620b43a
BP
1395 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1396 int hash = be - bond->hash;
f6ba1f35 1397 uint64_t be_tx_k;
f620b43a
BP
1398
1399 if (be->slave != slave) {
1400 continue;
1401 }
1402
f6ba1f35
AZ
1403 be_tx_k = be->tx_bytes / 1024;
1404 if (be_tx_k) {
1405 ds_put_format(ds, "\thash %d: %"PRIu64" kB load\n",
1406 hash, be_tx_k);
1407 }
f620b43a 1408
7b9f1974 1409 /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
f620b43a
BP
1410 }
1411 }
fc1d4f01
EJ
1412 shash_destroy(&slave_shash);
1413 free(sorted_slaves);
c33a8a25
EJ
1414 ds_put_cstr(ds, "\n");
1415}
1416
1417static void
1418bond_unixctl_show(struct unixctl_conn *conn,
1419 int argc, const char *argv[],
1420 void *aux OVS_UNUSED)
1421{
1422 struct ds ds = DS_EMPTY_INITIALIZER;
1423
3bfd3972 1424 ovs_rwlock_rdlock(&rwlock);
c33a8a25
EJ
1425 if (argc > 1) {
1426 const struct bond *bond = bond_find(argv[1]);
1427
1428 if (!bond) {
bde9f75d 1429 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1430 goto out;
c33a8a25
EJ
1431 }
1432 bond_print_details(&ds, bond);
1433 } else {
1434 const struct bond *bond;
1435
3bfd3972 1436 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
c33a8a25
EJ
1437 bond_print_details(&ds, bond);
1438 }
1439 }
1440
bde9f75d 1441 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a 1442 ds_destroy(&ds);
3bfd3972
EJ
1443
1444out:
1445 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1446}
1447
1448static void
0e15264f
BP
1449bond_unixctl_migrate(struct unixctl_conn *conn,
1450 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1451 void *aux OVS_UNUSED)
1452{
0e15264f
BP
1453 const char *bond_s = argv[1];
1454 const char *hash_s = argv[2];
1455 const char *slave_s = argv[3];
f620b43a
BP
1456 struct bond *bond;
1457 struct bond_slave *slave;
1458 struct bond_entry *entry;
1459 int hash;
1460
3bfd3972 1461 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1462 bond = bond_find(bond_s);
1463 if (!bond) {
bde9f75d 1464 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1465 goto out;
f620b43a
BP
1466 }
1467
1468 if (bond->balance != BM_SLB) {
bde9f75d 1469 unixctl_command_reply_error(conn, "not an SLB bond");
3bfd3972 1470 goto out;
f620b43a
BP
1471 }
1472
1473 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1474 hash = atoi(hash_s) & BOND_MASK;
1475 } else {
bde9f75d 1476 unixctl_command_reply_error(conn, "bad hash");
3bfd3972 1477 goto out;
f620b43a
BP
1478 }
1479
1480 slave = bond_lookup_slave(bond, slave_s);
1481 if (!slave) {
bde9f75d 1482 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1483 goto out;
f620b43a
BP
1484 }
1485
1486 if (!slave->enabled) {
bde9f75d 1487 unixctl_command_reply_error(conn, "cannot migrate to disabled slave");
3bfd3972 1488 goto out;
f620b43a
BP
1489 }
1490
1491 entry = &bond->hash[hash];
4a1b8f30 1492 bond->bond_revalidate = true;
f620b43a 1493 entry->slave = slave;
bde9f75d 1494 unixctl_command_reply(conn, "migrated");
3bfd3972
EJ
1495
1496out:
1497 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1498}
1499
1500static void
0e15264f
BP
1501bond_unixctl_set_active_slave(struct unixctl_conn *conn,
1502 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1503 void *aux OVS_UNUSED)
1504{
0e15264f
BP
1505 const char *bond_s = argv[1];
1506 const char *slave_s = argv[2];
f620b43a
BP
1507 struct bond *bond;
1508 struct bond_slave *slave;
1509
3bfd3972 1510 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1511 bond = bond_find(bond_s);
1512 if (!bond) {
bde9f75d 1513 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1514 goto out;
f620b43a
BP
1515 }
1516
1517 slave = bond_lookup_slave(bond, slave_s);
1518 if (!slave) {
bde9f75d 1519 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1520 goto out;
f620b43a
BP
1521 }
1522
1523 if (!slave->enabled) {
bde9f75d 1524 unixctl_command_reply_error(conn, "cannot make disabled slave active");
3bfd3972 1525 goto out;
f620b43a
BP
1526 }
1527
1528 if (bond->active_slave != slave) {
4a1b8f30 1529 bond->bond_revalidate = true;
f620b43a 1530 bond->active_slave = slave;
f620b43a
BP
1531 VLOG_INFO("bond %s: active interface is now %s",
1532 bond->name, slave->name);
1533 bond->send_learning_packets = true;
bde9f75d 1534 unixctl_command_reply(conn, "done");
3e5aeeb5 1535 bond_active_slave_changed(bond);
f620b43a 1536 } else {
bde9f75d 1537 unixctl_command_reply(conn, "no change");
f620b43a 1538 }
3bfd3972
EJ
1539out:
1540 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1541}
1542
1543static void
0e15264f 1544enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable)
f620b43a 1545{
0e15264f
BP
1546 const char *bond_s = argv[1];
1547 const char *slave_s = argv[2];
f620b43a
BP
1548 struct bond *bond;
1549 struct bond_slave *slave;
1550
3bfd3972 1551 ovs_rwlock_wrlock(&rwlock);
f620b43a
BP
1552 bond = bond_find(bond_s);
1553 if (!bond) {
bde9f75d 1554 unixctl_command_reply_error(conn, "no such bond");
3bfd3972 1555 goto out;
f620b43a
BP
1556 }
1557
1558 slave = bond_lookup_slave(bond, slave_s);
1559 if (!slave) {
bde9f75d 1560 unixctl_command_reply_error(conn, "no such slave");
3bfd3972 1561 goto out;
f620b43a
BP
1562 }
1563
4a1b8f30 1564 bond_enable_slave(slave, enable);
bde9f75d 1565 unixctl_command_reply(conn, enable ? "enabled" : "disabled");
3bfd3972
EJ
1566
1567out:
1568 ovs_rwlock_unlock(&rwlock);
f620b43a
BP
1569}
1570
1571static void
0e15264f
BP
1572bond_unixctl_enable_slave(struct unixctl_conn *conn,
1573 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1574 void *aux OVS_UNUSED)
1575{
0e15264f 1576 enable_slave(conn, argv, true);
f620b43a
BP
1577}
1578
1579static void
0e15264f
BP
1580bond_unixctl_disable_slave(struct unixctl_conn *conn,
1581 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1582 void *aux OVS_UNUSED)
1583{
0e15264f 1584 enable_slave(conn, argv, false);
f620b43a
BP
1585}
1586
1587static void
0e15264f 1588bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[],
f620b43a
BP
1589 void *aux OVS_UNUSED)
1590{
0e15264f
BP
1591 const char *mac_s = argv[1];
1592 const char *vlan_s = argc > 2 ? argv[2] : NULL;
1593 const char *basis_s = argc > 3 ? argv[3] : NULL;
74ff3298 1594 struct eth_addr mac;
f620b43a
BP
1595 uint8_t hash;
1596 char *hash_cstr;
1597 unsigned int vlan;
672d18b2 1598 uint32_t basis;
f620b43a
BP
1599
1600 if (vlan_s) {
c2c28dfd 1601 if (!ovs_scan(vlan_s, "%u", &vlan)) {
bde9f75d 1602 unixctl_command_reply_error(conn, "invalid vlan");
f620b43a
BP
1603 return;
1604 }
1605 } else {
dc155bff 1606 vlan = 0;
f620b43a
BP
1607 }
1608
672d18b2 1609 if (basis_s) {
c2c28dfd 1610 if (!ovs_scan(basis_s, "%"SCNu32, &basis)) {
bde9f75d 1611 unixctl_command_reply_error(conn, "invalid basis");
672d18b2
EJ
1612 return;
1613 }
1614 } else {
1615 basis = 0;
1616 }
1617
c2c28dfd 1618 if (ovs_scan(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
672d18b2 1619 hash = bond_hash_src(mac, vlan, basis) & BOND_MASK;
f620b43a
BP
1620
1621 hash_cstr = xasprintf("%u", hash);
bde9f75d 1622 unixctl_command_reply(conn, hash_cstr);
f620b43a
BP
1623 free(hash_cstr);
1624 } else {
bde9f75d 1625 unixctl_command_reply_error(conn, "invalid mac");
f620b43a
BP
1626 }
1627}
1628
1629void
1630bond_init(void)
1631{
0e15264f 1632 unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL);
c33a8a25
EJ
1633 unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show,
1634 NULL);
0e15264f 1635 unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
7ff2009a 1636 bond_unixctl_migrate, NULL);
0e15264f 1637 unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
f620b43a 1638 bond_unixctl_set_active_slave, NULL);
0e15264f 1639 unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
7ff2009a 1640 bond_unixctl_enable_slave, NULL);
0e15264f 1641 unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
7ff2009a 1642 bond_unixctl_disable_slave, NULL);
0e15264f 1643 unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
7ff2009a 1644 bond_unixctl_hash, NULL);
f620b43a
BP
1645}
1646\f
95aafb2a
EJ
1647static void
1648bond_entry_reset(struct bond *bond)
1649{
1650 if (bond->balance != BM_AB) {
9e1a6910 1651 size_t hash_len = BOND_BUCKETS * sizeof *bond->hash;
95aafb2a
EJ
1652
1653 if (!bond->hash) {
1654 bond->hash = xmalloc(hash_len);
1655 }
1656 memset(bond->hash, 0, hash_len);
1657
1658 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1659 } else {
1660 free(bond->hash);
1661 bond->hash = NULL;
05df1623
AZ
1662 /* Remove existing post recirc rules. */
1663 update_recirc_rules(bond);
95aafb2a
EJ
1664 }
1665}
1666
f620b43a
BP
1667static struct bond_slave *
1668bond_slave_lookup(struct bond *bond, const void *slave_)
1669{
1670 struct bond_slave *slave;
1671
1672 HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1673 &bond->slaves) {
1674 if (slave->aux == slave_) {
1675 return slave;
1676 }
1677 }
1678
1679 return NULL;
1680}
1681
f620b43a 1682static void
4a1b8f30 1683bond_enable_slave(struct bond_slave *slave, bool enable)
f620b43a
BP
1684{
1685 slave->delay_expires = LLONG_MAX;
1686 if (enable != slave->enabled) {
4a1b8f30 1687 slave->bond->bond_revalidate = true;
f620b43a 1688 slave->enabled = enable;
f1c8a79c
AW
1689
1690 ovs_mutex_lock(&slave->bond->mutex);
1691 if (enable) {
417e7e66 1692 ovs_list_insert(&slave->bond->enabled_slaves, &slave->list_node);
f1c8a79c 1693 } else {
417e7e66 1694 ovs_list_remove(&slave->list_node);
f1c8a79c
AW
1695 }
1696 ovs_mutex_unlock(&slave->bond->mutex);
1697
4a1b8f30
EJ
1698 VLOG_INFO("interface %s: %s", slave->name,
1699 slave->enabled ? "enabled" : "disabled");
f620b43a
BP
1700 }
1701}
1702
1703static void
4a1b8f30 1704bond_link_status_update(struct bond_slave *slave)
f620b43a
BP
1705{
1706 struct bond *bond = slave->bond;
1707 bool up;
1708
296f6519 1709 up = netdev_get_carrier(slave->netdev) && slave->may_enable;
f620b43a
BP
1710 if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1711 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1712 VLOG_INFO_RL(&rl, "interface %s: link state %s",
1713 slave->name, up ? "up" : "down");
1714 if (up == slave->enabled) {
1715 slave->delay_expires = LLONG_MAX;
1716 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1717 slave->name, up ? "disabled" : "enabled");
1718 } else {
bdebeece 1719 int delay = (bond->lacp_status != LACP_DISABLED ? 0
f620b43a
BP
1720 : up ? bond->updelay : bond->downdelay);
1721 slave->delay_expires = time_msec() + delay;
1722 if (delay) {
1723 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1724 "for %d ms",
1725 slave->name,
1726 up ? "enabled" : "disabled",
1727 up ? "up" : "down",
1728 delay);
1729 }
1730 }
1731 }
1732
1733 if (time_msec() >= slave->delay_expires) {
4a1b8f30 1734 bond_enable_slave(slave, up);
f620b43a
BP
1735 }
1736}
1737
f620b43a 1738static unsigned int
74ff3298 1739bond_hash_src(const struct eth_addr mac, uint16_t vlan, uint32_t basis)
f620b43a 1740{
7e36ac42 1741 return hash_mac(mac, vlan, basis);
f620b43a
BP
1742}
1743
1744static unsigned int
672d18b2 1745bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis)
f620b43a
BP
1746{
1747 struct flow hash_flow = *flow;
f0fb825a 1748 hash_flow.vlans[0].tci = htons(vlan);
f620b43a
BP
1749
1750 /* The symmetric quality of this hash function is not required, but
1751 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1752 * purposes, so we use it out of convenience. */
672d18b2 1753 return flow_hash_symmetric_l4(&hash_flow, basis);
f620b43a
BP
1754}
1755
fb0b29a3
EJ
1756static unsigned int
1757bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1758{
cb22974d 1759 ovs_assert(bond->balance == BM_TCP || bond->balance == BM_SLB);
fb0b29a3 1760
bdebeece 1761 return (bond->balance == BM_TCP
672d18b2
EJ
1762 ? bond_hash_tcp(flow, vlan, bond->basis)
1763 : bond_hash_src(flow->dl_src, vlan, bond->basis));
fb0b29a3
EJ
1764}
1765
f620b43a
BP
1766static struct bond_entry *
1767lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1768 uint16_t vlan)
1769{
fb0b29a3 1770 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
f620b43a
BP
1771}
1772
f1c8a79c
AW
1773/* Selects and returns an enabled slave from the 'enabled_slaves' list
1774 * in a round-robin fashion. If the 'enabled_slaves' list is empty,
1775 * returns NULL. */
1776static struct bond_slave *
1777get_enabled_slave(struct bond *bond)
1778{
ca6ba700 1779 struct ovs_list *node;
f1c8a79c
AW
1780
1781 ovs_mutex_lock(&bond->mutex);
417e7e66 1782 if (ovs_list_is_empty(&bond->enabled_slaves)) {
f1c8a79c
AW
1783 ovs_mutex_unlock(&bond->mutex);
1784 return NULL;
1785 }
1786
417e7e66
BW
1787 node = ovs_list_pop_front(&bond->enabled_slaves);
1788 ovs_list_push_back(&bond->enabled_slaves, node);
f1c8a79c
AW
1789 ovs_mutex_unlock(&bond->mutex);
1790
1791 return CONTAINER_OF(node, struct bond_slave, list_node);
1792}
1793
f620b43a
BP
1794static struct bond_slave *
1795choose_output_slave(const struct bond *bond, const struct flow *flow,
4a1b8f30 1796 struct flow_wildcards *wc, uint16_t vlan)
f620b43a
BP
1797{
1798 struct bond_entry *e;
9dd165e0 1799 int balance;
f620b43a 1800
9dd165e0 1801 balance = bond->balance;
bdebeece
EJ
1802 if (bond->lacp_status == LACP_CONFIGURED) {
1803 /* LACP has been configured on this bond but negotiations were
9dd165e0
RK
1804 * unsuccussful. If lacp_fallback_ab is enabled use active-
1805 * backup mode else drop all traffic. */
1806 if (!bond->lacp_fallback_ab) {
1807 return NULL;
1808 }
1809 balance = BM_AB;
bdebeece
EJ
1810 }
1811
9dd165e0 1812 switch (balance) {
f620b43a
BP
1813 case BM_AB:
1814 return bond->active_slave;
1815
f620b43a 1816 case BM_TCP:
bdebeece
EJ
1817 if (bond->lacp_status != LACP_NEGOTIATED) {
1818 /* Must have LACP negotiations for TCP balanced bonds. */
1819 return NULL;
1820 }
bcd2633a 1821 if (wc) {
6cdd5145 1822 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
bcd2633a 1823 }
bdebeece
EJ
1824 /* Fall Through. */
1825 case BM_SLB:
bcd2633a 1826 if (wc) {
6cdd5145 1827 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_ETH_SRC);
bcd2633a 1828 }
f620b43a
BP
1829 e = lookup_bond_entry(bond, flow, vlan);
1830 if (!e->slave || !e->slave->enabled) {
f1c8a79c 1831 e->slave = get_enabled_slave(CONST_CAST(struct bond*, bond));
f620b43a
BP
1832 }
1833 return e->slave;
1834
1835 default:
428b2edd 1836 OVS_NOT_REACHED();
f620b43a
BP
1837 }
1838}
1839
1840static struct bond_slave *
1841bond_choose_slave(const struct bond *bond)
1842{
1843 struct bond_slave *slave, *best;
1844
3e5aeeb5
AZ
1845 /* Find the last active slave. */
1846 slave = bond_find_slave_by_mac(bond, bond->active_slave_mac);
1847 if (slave && slave->enabled) {
1848 return slave;
1849 }
1850
f620b43a
BP
1851 /* Find an enabled slave. */
1852 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1853 if (slave->enabled) {
1854 return slave;
1855 }
1856 }
1857
1858 /* All interfaces are disabled. Find an interface that will be enabled
1859 * after its updelay expires. */
1860 best = NULL;
1861 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1862 if (slave->delay_expires != LLONG_MAX
296f6519 1863 && slave->may_enable
f620b43a
BP
1864 && (!best || slave->delay_expires < best->delay_expires)) {
1865 best = slave;
1866 }
1867 }
1868 return best;
1869}
1870
1871static void
4a1b8f30 1872bond_choose_active_slave(struct bond *bond)
f620b43a
BP
1873{
1874 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1875 struct bond_slave *old_active_slave = bond->active_slave;
1876
1877 bond->active_slave = bond_choose_slave(bond);
1878 if (bond->active_slave) {
1879 if (bond->active_slave->enabled) {
1880 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1881 bond->name, bond->active_slave->name);
1882 } else {
1883 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1884 "remaining %lld ms updelay (since no interface was "
1885 "enabled)", bond->name, bond->active_slave->name,
1886 bond->active_slave->delay_expires - time_msec());
4a1b8f30 1887 bond_enable_slave(bond->active_slave, true);
f620b43a
BP
1888 }
1889
1890 bond->send_learning_packets = true;
3e5aeeb5
AZ
1891
1892 if (bond->active_slave != old_active_slave) {
1893 bond_active_slave_changed(bond);
1894 }
f620b43a 1895 } else if (old_active_slave) {
f626af7a 1896 bond_active_slave_changed(bond);
d28b9ead 1897 VLOG_INFO_RL(&rl, "bond %s: all interfaces disabled", bond->name);
f620b43a
BP
1898 }
1899}
3e5aeeb5
AZ
1900
1901/*
1902 * Return true if bond has unstored active slave change.
1903 * If return true, 'mac' will store the bond's current active slave's
1904 * MAC address. */
1905bool
74ff3298
JR
1906bond_get_changed_active_slave(const char *name, struct eth_addr *mac,
1907 bool force)
3e5aeeb5
AZ
1908{
1909 struct bond *bond;
1910
1911 ovs_rwlock_wrlock(&rwlock);
1912 bond = bond_find(name);
1913 if (bond) {
1914 if (bond->active_slave_changed || force) {
74ff3298 1915 *mac = bond->active_slave_mac;
3e5aeeb5
AZ
1916 bond->active_slave_changed = false;
1917 ovs_rwlock_unlock(&rwlock);
1918 return true;
1919 }
1920 }
1921 ovs_rwlock_unlock(&rwlock);
1922
1923 return false;
1924}