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