]> git.proxmox.com Git - ovs.git/blob - ofproto/bond.c
ofproto/bond: Make bond_may_recirc() private within bond.c
[ovs.git] / ofproto / bond.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 unsigned int bond_hash_src(const struct eth_addr mac,
179 uint16_t vlan, uint32_t basis);
180 static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan,
181 uint32_t basis);
182 static struct bond_entry *lookup_bond_entry(const struct bond *,
183 const struct flow *,
184 uint16_t vlan)
185 OVS_REQ_RDLOCK(rwlock);
186 static struct bond_slave *get_enabled_slave(struct bond *)
187 OVS_REQ_RDLOCK(rwlock);
188 static struct bond_slave *choose_output_slave(const struct bond *,
189 const struct flow *,
190 struct flow_wildcards *,
191 uint16_t vlan)
192 OVS_REQ_RDLOCK(rwlock);
193 static void update_recirc_rules__(struct bond *bond);
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'. */
198 bool
199 bond_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'. */
214 const char *
215 bond_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 }
224 OVS_NOT_REACHED();
225 }
226
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(). */
233 struct bond *
234 bond_create(const struct bond_settings *s, struct ofproto_dpif *ofproto)
235 {
236 struct bond *bond;
237
238 bond = xzalloc(sizeof *bond);
239 bond->ofproto = ofproto;
240 hmap_init(&bond->slaves);
241 ovs_list_init(&bond->enabled_slaves);
242 ovs_mutex_init(&bond->mutex);
243 ovs_refcount_init(&bond->ref_cnt);
244 hmap_init(&bond->pr_rule_ops);
245
246 bond->active_slave_mac = eth_addr_zero;
247 bond->active_slave_changed = false;
248
249 bond_reconfigure(bond, s);
250 return bond;
251 }
252
253 struct bond *
254 bond_ref(const struct bond *bond_)
255 {
256 struct bond *bond = CONST_CAST(struct bond *, bond_);
257
258 if (bond) {
259 ovs_refcount_ref(&bond->ref_cnt);
260 }
261 return bond;
262 }
263
264 /* Frees 'bond'. */
265 void
266 bond_unref(struct bond *bond)
267 {
268 struct bond_slave *slave;
269
270 if (!bond || ovs_refcount_unref_relaxed(&bond->ref_cnt) != 1) {
271 return;
272 }
273
274 ovs_rwlock_wrlock(&rwlock);
275 hmap_remove(all_bonds, &bond->hmap_node);
276 ovs_rwlock_unlock(&rwlock);
277
278 HMAP_FOR_EACH_POP (slave, hmap_node, &bond->slaves) {
279 /* Client owns 'slave->netdev'. */
280 free(slave->name);
281 free(slave);
282 }
283 hmap_destroy(&bond->slaves);
284
285 ovs_mutex_destroy(&bond->mutex);
286
287 /* Free bond resources. Remove existing post recirc rules. */
288 if (bond->recirc_id) {
289 recirc_free_id(bond->recirc_id);
290 bond->recirc_id = 0;
291 }
292 free(bond->hash);
293 bond->hash = NULL;
294 update_recirc_rules__(bond);
295
296 hmap_destroy(&bond->pr_rule_ops);
297 free(bond->name);
298 free(bond);
299 }
300
301 static void
302 add_pr_rule(struct bond *bond, const struct match *match,
303 ofp_port_t out_ofport, struct rule **rule)
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
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. */
334 static void
335 update_recirc_rules__(struct bond *bond)
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
349 if (bond->hash && bond->recirc_id) {
350 for (i = 0; i < BOND_BUCKETS; i++) {
351 struct bond_slave *slave = bond->hash[i].slave;
352
353 if (slave) {
354 match_init_catchall(&match);
355 match_set_recirc_id(&match, bond->recirc_id);
356 match_set_dp_hash_masked(&match, i, BOND_MASK);
357
358 add_pr_rule(bond, &match, slave->ofp_port,
359 &bond->hash[i].pr_rule);
360 }
361 }
362 }
363
364 HMAP_FOR_EACH_SAFE(pr_op, next_op, hmap_node, &bond->pr_rule_ops) {
365 int error;
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,
372 RECIRC_RULE_PRIORITY, 0,
373 &ofpacts, pr_op->pr_rule);
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);
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);
396 *pr_op->pr_rule = NULL;
397 free(pr_op);
398 break;
399 }
400 }
401
402 ofpbuf_uninit(&ofpacts);
403 }
404
405 static void
406 update_recirc_rules(struct bond *bond)
407 OVS_REQ_RDLOCK(rwlock)
408 {
409 update_recirc_rules__(bond);
410 }
411
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'
416 * configuration has changed. In any case it can't hurt.
417 *
418 * Returns true if the configuration has changed in such a way that requires
419 * flow revalidation.
420 * */
421 bool
422 bond_reconfigure(struct bond *bond, const struct bond_settings *s)
423 {
424 bool revalidate = false;
425
426 ovs_rwlock_wrlock(&rwlock);
427 if (!bond->name || strcmp(bond->name, s->name)) {
428 if (bond->name) {
429 hmap_remove(all_bonds, &bond->hmap_node);
430 free(bond->name);
431 }
432 bond->name = xstrdup(s->name);
433 hmap_insert(all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
434 }
435
436 bond->updelay = s->up_delay;
437 bond->downdelay = s->down_delay;
438
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
444 if (bond->rebalance_interval != s->rebalance_interval) {
445 bond->rebalance_interval = s->rebalance_interval;
446 revalidate = true;
447 }
448
449 if (bond->balance != s->balance) {
450 bond->balance = s->balance;
451 revalidate = true;
452 }
453
454 if (bond->basis != s->basis) {
455 bond->basis = s->basis;
456 revalidate = true;
457 }
458
459 if (bond->bond_revalidate) {
460 revalidate = true;
461 bond->bond_revalidate = false;
462 }
463
464 if (bond->balance != BM_AB) {
465 if (!bond->recirc_id) {
466 bond->recirc_id = recirc_alloc_id(bond->ofproto);
467 }
468 } else if (bond->recirc_id) {
469 recirc_free_id(bond->recirc_id);
470 bond->recirc_id = 0;
471 }
472
473 if (bond->balance == BM_AB || !bond->hash || revalidate) {
474 bond_entry_reset(bond);
475 }
476
477 ovs_rwlock_unlock(&rwlock);
478 return revalidate;
479 }
480
481 static struct bond_slave *
482 bond_find_slave_by_mac(const struct bond *bond, const struct eth_addr mac)
483 {
484 struct bond_slave *slave;
485
486 /* Find the last active slave */
487 HMAP_FOR_EACH(slave, hmap_node, &bond->slaves) {
488 struct eth_addr slave_mac;
489
490 if (netdev_get_etheraddr(slave->netdev, &slave_mac)) {
491 continue;
492 }
493
494 if (eth_addr_equals(slave_mac, mac)) {
495 return slave;
496 }
497 }
498
499 return NULL;
500 }
501
502 static void
503 bond_active_slave_changed(struct bond *bond)
504 {
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 }
512 bond->active_slave_changed = true;
513 seq_change(connectivity_seq_get());
514 }
515
516 static void
517 bond_slave_set_netdev__(struct bond_slave *slave, struct netdev *netdev)
518 OVS_REQ_WRLOCK(rwlock)
519 {
520 if (slave->netdev != netdev) {
521 slave->netdev = netdev;
522 slave->change_seq = 0;
523 }
524 }
525
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'.
534 */
535 void
536 bond_slave_register(struct bond *bond, void *slave_,
537 ofp_port_t ofport, struct netdev *netdev)
538 {
539 struct bond_slave *slave;
540
541 ovs_rwlock_wrlock(&rwlock);
542 slave = bond_slave_lookup(bond, slave_);
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_;
549 slave->ofp_port = ofport;
550 slave->delay_expires = LLONG_MAX;
551 slave->name = xstrdup(netdev_get_name(netdev));
552 bond->bond_revalidate = true;
553
554 slave->enabled = false;
555 bond_enable_slave(slave, netdev_get_carrier(netdev));
556 }
557
558 bond_slave_set_netdev__(slave, netdev);
559
560 free(slave->name);
561 slave->name = xstrdup(netdev_get_name(netdev));
562 ovs_rwlock_unlock(&rwlock);
563 }
564
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. */
570 void
571 bond_slave_set_netdev(struct bond *bond, void *slave_, struct netdev *netdev)
572 {
573 struct bond_slave *slave;
574
575 ovs_rwlock_wrlock(&rwlock);
576 slave = bond_slave_lookup(bond, slave_);
577 if (slave) {
578 bond_slave_set_netdev__(slave, netdev);
579 }
580 ovs_rwlock_unlock(&rwlock);
581 }
582
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. */
587 void
588 bond_slave_unregister(struct bond *bond, const void *slave_)
589 {
590 struct bond_slave *slave;
591 bool del_active;
592
593 ovs_rwlock_wrlock(&rwlock);
594 slave = bond_slave_lookup(bond, slave_);
595 if (!slave) {
596 goto out;
597 }
598
599 bond->bond_revalidate = true;
600 bond_enable_slave(slave, false);
601
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) {
619 bond_choose_active_slave(bond);
620 bond->send_learning_packets = true;
621 }
622 out:
623 ovs_rwlock_unlock(&rwlock);
624 }
625
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. */
631 void
632 bond_slave_set_may_enable(struct bond *bond, void *slave_, bool may_enable)
633 {
634 ovs_rwlock_wrlock(&rwlock);
635 bond_slave_lookup(bond, slave_)->may_enable = may_enable;
636 ovs_rwlock_unlock(&rwlock);
637 }
638
639 /* Performs periodic maintenance on 'bond'.
640 *
641 * Returns true if the caller should revalidate its flows.
642 *
643 * The caller should check bond_should_send_learning_packets() afterward. */
644 bool
645 bond_run(struct bond *bond, enum lacp_status lacp_status)
646 {
647 struct bond_slave *slave;
648 bool revalidate;
649
650 ovs_rwlock_wrlock(&rwlock);
651 if (bond->lacp_status != lacp_status) {
652 bond->lacp_status = lacp_status;
653 bond->bond_revalidate = true;
654 }
655
656 /* Enable slaves based on link status and LACP feedback. */
657 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
658 bond_link_status_update(slave);
659 slave->change_seq = seq_read(connectivity_seq_get());
660 }
661 if (!bond->active_slave || !bond->active_slave->enabled) {
662 bond_choose_active_slave(bond);
663 }
664
665 revalidate = bond->bond_revalidate;
666 bond->bond_revalidate = false;
667 ovs_rwlock_unlock(&rwlock);
668
669 return revalidate;
670 }
671
672 /* Causes poll_block() to wake up when 'bond' needs something to be done. */
673 void
674 bond_wait(struct bond *bond)
675 {
676 struct bond_slave *slave;
677
678 ovs_rwlock_rdlock(&rwlock);
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 }
683
684 seq_wait(connectivity_seq_get(), slave->change_seq);
685 }
686
687 if (bond->bond_revalidate) {
688 poll_immediate_wake();
689 }
690 ovs_rwlock_unlock(&rwlock);
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
700 static bool
701 may_send_learning_packets(const struct bond *bond)
702 {
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))
706 && bond->active_slave;
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',
713 * it should call bond_compose_learning_packet().
714 *
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.
718 *
719 * Calling this function resets the state that it checks. */
720 bool
721 bond_should_send_learning_packets(struct bond *bond)
722 {
723 bool send;
724
725 ovs_rwlock_wrlock(&rwlock);
726 send = bond->send_learning_packets && may_send_learning_packets(bond);
727 bond->send_learning_packets = false;
728 ovs_rwlock_unlock(&rwlock);
729 return send;
730 }
731
732 /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
733 *
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. */
737 struct dp_packet *
738 bond_compose_learning_packet(struct bond *bond, const struct eth_addr eth_src,
739 uint16_t vlan, void **port_aux)
740 {
741 struct bond_slave *slave;
742 struct dp_packet *packet;
743 struct flow flow;
744
745 ovs_rwlock_rdlock(&rwlock);
746 ovs_assert(may_send_learning_packets(bond));
747 memset(&flow, 0, sizeof flow);
748 flow.dl_src = eth_src;
749 slave = choose_output_slave(bond, &flow, NULL, vlan);
750
751 packet = dp_packet_new(0);
752 compose_rarp(packet, eth_src);
753 if (vlan) {
754 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), htons(vlan));
755 }
756
757 *port_aux = slave->aux;
758 ovs_rwlock_unlock(&rwlock);
759 return packet;
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 */
777 enum bond_verdict
778 bond_check_admissibility(struct bond *bond, const void *slave_,
779 const struct eth_addr eth_dst)
780 {
781 enum bond_verdict verdict = BV_DROP;
782 struct bond_slave *slave;
783
784 ovs_rwlock_rdlock(&rwlock);
785 slave = bond_slave_lookup(bond, slave_);
786 if (!slave) {
787 goto out;
788 }
789
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
794 * packets to them.
795 *
796 * If LACP is configured, but LACP negotiations have been unsuccessful, we
797 * drop all incoming traffic except if lacp_fallback_ab is enabled. */
798 switch (bond->lacp_status) {
799 case LACP_NEGOTIATED:
800 verdict = slave->enabled ? BV_ACCEPT : BV_DROP;
801 goto out;
802 case LACP_CONFIGURED:
803 if (!bond->lacp_fallback_ab) {
804 goto out;
805 }
806 break;
807 case LACP_DISABLED:
808 if (bond->balance == BM_TCP) {
809 goto out;
810 }
811 break;
812 }
813
814 /* Drop all multicast packets on inactive slaves. */
815 if (eth_addr_is_multicast(eth_dst)) {
816 if (bond->active_slave != slave) {
817 goto out;
818 }
819 }
820
821 switch (bond->balance) {
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
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. */
834 if (bond->active_slave != slave) {
835 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
836
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));
840 goto out;
841 }
842 verdict = BV_ACCEPT;
843 goto out;
844
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. */
852 verdict = BV_DROP_IF_MOVED;
853 goto out;
854 }
855
856 OVS_NOT_REACHED();
857 out:
858 ovs_rwlock_unlock(&rwlock);
859 return verdict;
860
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 *
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()).
876 */
877 void *
878 bond_choose_output_slave(struct bond *bond, const struct flow *flow,
879 struct flow_wildcards *wc, uint16_t vlan)
880 {
881 struct bond_slave *slave;
882 void *aux;
883
884 ovs_rwlock_rdlock(&rwlock);
885 slave = choose_output_slave(bond, flow, wc, vlan);
886 aux = slave ? slave->aux : NULL;
887 ovs_rwlock_unlock(&rwlock);
888
889 return aux;
890 }
891 \f
892 /* Recirculation. */
893 static void
894 bond_entry_account(struct bond_entry *entry, uint64_t rule_tx_bytes)
895 OVS_REQ_WRLOCK(rwlock)
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.*/
907 static void
908 bond_recirculation_account(struct bond *bond)
909 OVS_REQ_WRLOCK(rwlock)
910 {
911 int i;
912
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 }
927 }
928
929 static bool
930 bond_may_recirc(const struct bond *bond)
931 {
932 return bond->balance == BM_TCP && bond->recirc_id;
933 }
934
935 static void
936 bond_update_post_recirc_rules__(struct bond* bond, const bool force)
937 OVS_REQ_WRLOCK(rwlock)
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 }
958
959 void
960 bond_update_post_recirc_rules(struct bond *bond, uint32_t *recirc_id,
961 uint32_t *hash_basis)
962 {
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) {
980 *recirc_id = *hash_basis = 0;
981 }
982 }
983
984 \f
985 /* Rebalancing. */
986
987 static bool
988 bond_is_balanced(const struct bond *bond) OVS_REQ_RDLOCK(rwlock)
989 {
990 return bond->rebalance_interval
991 && (bond->balance == BM_SLB || bond->balance == BM_TCP);
992 }
993
994 /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
995 void
996 bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
997 uint64_t n_bytes)
998 {
999 ovs_rwlock_wrlock(&rwlock);
1000 if (bond_is_balanced(bond)) {
1001 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
1002 }
1003 ovs_rwlock_unlock(&rwlock);
1004 }
1005
1006 static struct bond_slave *
1007 bond_slave_from_bal_node(struct ovs_list *bal) OVS_REQ_RDLOCK(rwlock)
1008 {
1009 return CONTAINER_OF(bal, struct bond_slave, bal_node);
1010 }
1011
1012 static void
1013 log_bals(struct bond *bond, const struct ovs_list *bals)
1014 OVS_REQ_RDLOCK(rwlock)
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 }
1030 if (!ovs_list_is_empty(&slave->entries)) {
1031 struct bond_entry *e;
1032
1033 ds_put_cstr(&ds, " (");
1034 LIST_FOR_EACH (e, list_node, &slave->entries) {
1035 if (&e->list_node != ovs_list_front(&slave->entries)) {
1036 ds_put_cstr(&ds, " + ");
1037 }
1038 ds_put_format(&ds, "h%"PRIdPTR": %"PRIu64"kB",
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'. */
1050 static void
1051 bond_shift_load(struct bond_entry *hash, struct bond_slave *to)
1052 OVS_REQ_WRLOCK(rwlock)
1053 {
1054 struct bond_slave *from = hash->slave;
1055 struct bond *bond = from->bond;
1056 uint64_t delta = hash->tx_bytes;
1057
1058 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %"PRIdPTR") "
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. */
1071 hash->slave = to;
1072 bond->bond_revalidate = true;
1073 }
1074
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,
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. */
1082 static struct bond_entry *
1083 choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
1084 OVS_REQ_WRLOCK(rwlock)
1085 {
1086 struct bond_entry *e;
1087
1088 if (ovs_list_is_short(&from->entries)) {
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) {
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. */
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. */
1122 static void
1123 insert_bal(struct ovs_list *bals, struct bond_slave *slave)
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 }
1132 ovs_list_insert(&pos->bal_node, &slave->bal_node);
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. */
1137 static void
1138 reinsert_bal(struct ovs_list *bals, struct bond_slave *slave)
1139 {
1140 ovs_list_remove(&slave->bal_node);
1141 insert_bal(bals, slave);
1142 }
1143
1144 /* If 'bond' needs rebalancing, does so.
1145 *
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.
1149 */
1150 void
1151 bond_rebalance(struct bond *bond)
1152 {
1153 struct bond_slave *slave;
1154 struct bond_entry *e;
1155 struct ovs_list bals;
1156 bool rebalanced = false;
1157 bool use_recirc;
1158
1159 ovs_rwlock_wrlock(&rwlock);
1160 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
1161 goto done;
1162 }
1163 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1164
1165 use_recirc = bond->ofproto->backer->support.odp.recirc &&
1166 bond_may_recirc(bond);
1167
1168 if (use_recirc) {
1169 bond_recirculation_account(bond);
1170 }
1171
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;
1176 ovs_list_init(&slave->entries);
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;
1181 ovs_list_push_back(&e->slave->entries, &e->list_node);
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. */
1189 ovs_list_init(&bals);
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. */
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));
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
1211 /* 'from' is carrying significantly more load than 'to'. Pick a hash
1212 * to move from 'from' to 'to'. */
1213 e = choose_entry_to_migrate(from, to->tx_bytes);
1214 if (e) {
1215 bond_shift_load(e, to);
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. */
1222 ovs_list_remove(&e->list_node);
1223
1224 /* Re-sort 'bals'. */
1225 reinsert_bal(&bals, from);
1226 reinsert_bal(&bals, to);
1227 rebalanced = true;
1228 } else {
1229 /* Can't usefully migrate anything away from 'from'.
1230 * Don't reconsider it. */
1231 ovs_list_remove(&from->bal_node);
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;
1240 }
1241
1242 if (use_recirc && rebalanced) {
1243 bond_update_post_recirc_rules__(bond,true);
1244 }
1245
1246 done:
1247 ovs_rwlock_unlock(&rwlock);
1248 }
1249 \f
1250 /* Bonding unixctl user interface functions. */
1251
1252 static struct bond *
1253 bond_find(const char *name) OVS_REQ_RDLOCK(rwlock)
1254 {
1255 struct bond *bond;
1256
1257 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
1258 all_bonds) {
1259 if (!strcmp(bond->name, name)) {
1260 return bond;
1261 }
1262 }
1263 return NULL;
1264 }
1265
1266 static struct bond_slave *
1267 bond_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
1279 static void
1280 bond_unixctl_list(struct unixctl_conn *conn,
1281 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1282 void *aux OVS_UNUSED)
1283 {
1284 struct ds ds = DS_EMPTY_INITIALIZER;
1285 const struct bond *bond;
1286
1287 ds_put_cstr(&ds, "bond\ttype\trecircID\tslaves\n");
1288
1289 ovs_rwlock_rdlock(&rwlock);
1290 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
1291 const struct bond_slave *slave;
1292 size_t i;
1293
1294 ds_put_format(&ds, "%s\t%s\t%d\t", bond->name,
1295 bond_mode_to_string(bond->balance), bond->recirc_id);
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 }
1306 ovs_rwlock_unlock(&rwlock);
1307 unixctl_command_reply(conn, ds_cstr(&ds));
1308 ds_destroy(&ds);
1309 }
1310
1311 static void
1312 bond_print_details(struct ds *ds, const struct bond *bond)
1313 OVS_REQ_RDLOCK(rwlock)
1314 {
1315 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
1316 const struct shash_node **sorted_slaves = NULL;
1317 const struct bond_slave *slave;
1318 bool may_recirc;
1319 uint32_t recirc_id;
1320 int i;
1321
1322 ds_put_format(ds, "---- %s ----\n", bond->name);
1323 ds_put_format(ds, "bond_mode: %s\n",
1324 bond_mode_to_string(bond->balance));
1325
1326 may_recirc = bond_may_recirc(bond);
1327 recirc_id = bond->recirc_id;
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
1331 ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis);
1332
1333 ds_put_format(ds, "updelay: %d ms\n", bond->updelay);
1334 ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay);
1335
1336 if (bond_is_balanced(bond)) {
1337 ds_put_format(ds, "next rebalance: %lld ms\n",
1338 bond->next_rebalance - time_msec());
1339 }
1340
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 }
1356
1357 ds_put_format(ds, "lacp_fallback_ab: %s\n",
1358 bond->lacp_fallback_ab ? "true" : "false");
1359
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
1365 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
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++) {
1371 struct bond_entry *be;
1372
1373 slave = sorted_slaves[i]->data;
1374
1375 /* Basic info. */
1376 ds_put_format(ds, "\nslave %s: %s\n",
1377 slave->name, slave->enabled ? "enabled" : "disabled");
1378 if (slave == bond->active_slave) {
1379 ds_put_cstr(ds, "\tactive slave\n");
1380 }
1381 if (slave->delay_expires != LLONG_MAX) {
1382 ds_put_format(ds, "\t%s expires in %lld ms\n",
1383 slave->enabled ? "downdelay" : "updelay",
1384 slave->delay_expires - time_msec());
1385 }
1386
1387 ds_put_format(ds, "\tmay_enable: %s\n",
1388 slave->may_enable ? "true" : "false");
1389
1390 if (!bond_is_balanced(bond)) {
1391 continue;
1392 }
1393
1394 /* Hashes. */
1395 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1396 int hash = be - bond->hash;
1397 uint64_t be_tx_k;
1398
1399 if (be->slave != slave) {
1400 continue;
1401 }
1402
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 }
1408
1409 /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
1410 }
1411 }
1412 shash_destroy(&slave_shash);
1413 free(sorted_slaves);
1414 ds_put_cstr(ds, "\n");
1415 }
1416
1417 static void
1418 bond_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
1424 ovs_rwlock_rdlock(&rwlock);
1425 if (argc > 1) {
1426 const struct bond *bond = bond_find(argv[1]);
1427
1428 if (!bond) {
1429 unixctl_command_reply_error(conn, "no such bond");
1430 goto out;
1431 }
1432 bond_print_details(&ds, bond);
1433 } else {
1434 const struct bond *bond;
1435
1436 HMAP_FOR_EACH (bond, hmap_node, all_bonds) {
1437 bond_print_details(&ds, bond);
1438 }
1439 }
1440
1441 unixctl_command_reply(conn, ds_cstr(&ds));
1442 ds_destroy(&ds);
1443
1444 out:
1445 ovs_rwlock_unlock(&rwlock);
1446 }
1447
1448 static void
1449 bond_unixctl_migrate(struct unixctl_conn *conn,
1450 int argc OVS_UNUSED, const char *argv[],
1451 void *aux OVS_UNUSED)
1452 {
1453 const char *bond_s = argv[1];
1454 const char *hash_s = argv[2];
1455 const char *slave_s = argv[3];
1456 struct bond *bond;
1457 struct bond_slave *slave;
1458 struct bond_entry *entry;
1459 int hash;
1460
1461 ovs_rwlock_wrlock(&rwlock);
1462 bond = bond_find(bond_s);
1463 if (!bond) {
1464 unixctl_command_reply_error(conn, "no such bond");
1465 goto out;
1466 }
1467
1468 if (bond->balance != BM_SLB) {
1469 unixctl_command_reply_error(conn, "not an SLB bond");
1470 goto out;
1471 }
1472
1473 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1474 hash = atoi(hash_s) & BOND_MASK;
1475 } else {
1476 unixctl_command_reply_error(conn, "bad hash");
1477 goto out;
1478 }
1479
1480 slave = bond_lookup_slave(bond, slave_s);
1481 if (!slave) {
1482 unixctl_command_reply_error(conn, "no such slave");
1483 goto out;
1484 }
1485
1486 if (!slave->enabled) {
1487 unixctl_command_reply_error(conn, "cannot migrate to disabled slave");
1488 goto out;
1489 }
1490
1491 entry = &bond->hash[hash];
1492 bond->bond_revalidate = true;
1493 entry->slave = slave;
1494 unixctl_command_reply(conn, "migrated");
1495
1496 out:
1497 ovs_rwlock_unlock(&rwlock);
1498 }
1499
1500 static void
1501 bond_unixctl_set_active_slave(struct unixctl_conn *conn,
1502 int argc OVS_UNUSED, const char *argv[],
1503 void *aux OVS_UNUSED)
1504 {
1505 const char *bond_s = argv[1];
1506 const char *slave_s = argv[2];
1507 struct bond *bond;
1508 struct bond_slave *slave;
1509
1510 ovs_rwlock_wrlock(&rwlock);
1511 bond = bond_find(bond_s);
1512 if (!bond) {
1513 unixctl_command_reply_error(conn, "no such bond");
1514 goto out;
1515 }
1516
1517 slave = bond_lookup_slave(bond, slave_s);
1518 if (!slave) {
1519 unixctl_command_reply_error(conn, "no such slave");
1520 goto out;
1521 }
1522
1523 if (!slave->enabled) {
1524 unixctl_command_reply_error(conn, "cannot make disabled slave active");
1525 goto out;
1526 }
1527
1528 if (bond->active_slave != slave) {
1529 bond->bond_revalidate = true;
1530 bond->active_slave = slave;
1531 VLOG_INFO("bond %s: active interface is now %s",
1532 bond->name, slave->name);
1533 bond->send_learning_packets = true;
1534 unixctl_command_reply(conn, "done");
1535 bond_active_slave_changed(bond);
1536 } else {
1537 unixctl_command_reply(conn, "no change");
1538 }
1539 out:
1540 ovs_rwlock_unlock(&rwlock);
1541 }
1542
1543 static void
1544 enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable)
1545 {
1546 const char *bond_s = argv[1];
1547 const char *slave_s = argv[2];
1548 struct bond *bond;
1549 struct bond_slave *slave;
1550
1551 ovs_rwlock_wrlock(&rwlock);
1552 bond = bond_find(bond_s);
1553 if (!bond) {
1554 unixctl_command_reply_error(conn, "no such bond");
1555 goto out;
1556 }
1557
1558 slave = bond_lookup_slave(bond, slave_s);
1559 if (!slave) {
1560 unixctl_command_reply_error(conn, "no such slave");
1561 goto out;
1562 }
1563
1564 bond_enable_slave(slave, enable);
1565 unixctl_command_reply(conn, enable ? "enabled" : "disabled");
1566
1567 out:
1568 ovs_rwlock_unlock(&rwlock);
1569 }
1570
1571 static void
1572 bond_unixctl_enable_slave(struct unixctl_conn *conn,
1573 int argc OVS_UNUSED, const char *argv[],
1574 void *aux OVS_UNUSED)
1575 {
1576 enable_slave(conn, argv, true);
1577 }
1578
1579 static void
1580 bond_unixctl_disable_slave(struct unixctl_conn *conn,
1581 int argc OVS_UNUSED, const char *argv[],
1582 void *aux OVS_UNUSED)
1583 {
1584 enable_slave(conn, argv, false);
1585 }
1586
1587 static void
1588 bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[],
1589 void *aux OVS_UNUSED)
1590 {
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;
1594 struct eth_addr mac;
1595 uint8_t hash;
1596 char *hash_cstr;
1597 unsigned int vlan;
1598 uint32_t basis;
1599
1600 if (vlan_s) {
1601 if (!ovs_scan(vlan_s, "%u", &vlan)) {
1602 unixctl_command_reply_error(conn, "invalid vlan");
1603 return;
1604 }
1605 } else {
1606 vlan = 0;
1607 }
1608
1609 if (basis_s) {
1610 if (!ovs_scan(basis_s, "%"SCNu32, &basis)) {
1611 unixctl_command_reply_error(conn, "invalid basis");
1612 return;
1613 }
1614 } else {
1615 basis = 0;
1616 }
1617
1618 if (ovs_scan(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
1619 hash = bond_hash_src(mac, vlan, basis) & BOND_MASK;
1620
1621 hash_cstr = xasprintf("%u", hash);
1622 unixctl_command_reply(conn, hash_cstr);
1623 free(hash_cstr);
1624 } else {
1625 unixctl_command_reply_error(conn, "invalid mac");
1626 }
1627 }
1628
1629 void
1630 bond_init(void)
1631 {
1632 unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL);
1633 unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show,
1634 NULL);
1635 unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
1636 bond_unixctl_migrate, NULL);
1637 unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
1638 bond_unixctl_set_active_slave, NULL);
1639 unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
1640 bond_unixctl_enable_slave, NULL);
1641 unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
1642 bond_unixctl_disable_slave, NULL);
1643 unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
1644 bond_unixctl_hash, NULL);
1645 }
1646 \f
1647 static void
1648 bond_entry_reset(struct bond *bond)
1649 {
1650 if (bond->balance != BM_AB) {
1651 size_t hash_len = BOND_BUCKETS * sizeof *bond->hash;
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;
1662 /* Remove existing post recirc rules. */
1663 update_recirc_rules(bond);
1664 }
1665 }
1666
1667 static struct bond_slave *
1668 bond_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
1682 static void
1683 bond_enable_slave(struct bond_slave *slave, bool enable)
1684 {
1685 slave->delay_expires = LLONG_MAX;
1686 if (enable != slave->enabled) {
1687 slave->bond->bond_revalidate = true;
1688 slave->enabled = enable;
1689
1690 ovs_mutex_lock(&slave->bond->mutex);
1691 if (enable) {
1692 ovs_list_insert(&slave->bond->enabled_slaves, &slave->list_node);
1693 } else {
1694 ovs_list_remove(&slave->list_node);
1695 }
1696 ovs_mutex_unlock(&slave->bond->mutex);
1697
1698 VLOG_INFO("interface %s: %s", slave->name,
1699 slave->enabled ? "enabled" : "disabled");
1700 }
1701 }
1702
1703 static void
1704 bond_link_status_update(struct bond_slave *slave)
1705 {
1706 struct bond *bond = slave->bond;
1707 bool up;
1708
1709 up = netdev_get_carrier(slave->netdev) && slave->may_enable;
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 {
1719 int delay = (bond->lacp_status != LACP_DISABLED ? 0
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) {
1734 bond_enable_slave(slave, up);
1735 }
1736 }
1737
1738 static unsigned int
1739 bond_hash_src(const struct eth_addr mac, uint16_t vlan, uint32_t basis)
1740 {
1741 return hash_mac(mac, vlan, basis);
1742 }
1743
1744 static unsigned int
1745 bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis)
1746 {
1747 struct flow hash_flow = *flow;
1748 hash_flow.vlans[0].tci = htons(vlan);
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. */
1753 return flow_hash_symmetric_l4(&hash_flow, basis);
1754 }
1755
1756 static unsigned int
1757 bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1758 {
1759 ovs_assert(bond->balance == BM_TCP || bond->balance == BM_SLB);
1760
1761 return (bond->balance == BM_TCP
1762 ? bond_hash_tcp(flow, vlan, bond->basis)
1763 : bond_hash_src(flow->dl_src, vlan, bond->basis));
1764 }
1765
1766 static struct bond_entry *
1767 lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1768 uint16_t vlan)
1769 {
1770 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
1771 }
1772
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. */
1776 static struct bond_slave *
1777 get_enabled_slave(struct bond *bond)
1778 {
1779 struct ovs_list *node;
1780
1781 ovs_mutex_lock(&bond->mutex);
1782 if (ovs_list_is_empty(&bond->enabled_slaves)) {
1783 ovs_mutex_unlock(&bond->mutex);
1784 return NULL;
1785 }
1786
1787 node = ovs_list_pop_front(&bond->enabled_slaves);
1788 ovs_list_push_back(&bond->enabled_slaves, node);
1789 ovs_mutex_unlock(&bond->mutex);
1790
1791 return CONTAINER_OF(node, struct bond_slave, list_node);
1792 }
1793
1794 static struct bond_slave *
1795 choose_output_slave(const struct bond *bond, const struct flow *flow,
1796 struct flow_wildcards *wc, uint16_t vlan)
1797 {
1798 struct bond_entry *e;
1799 int balance;
1800
1801 balance = bond->balance;
1802 if (bond->lacp_status == LACP_CONFIGURED) {
1803 /* LACP has been configured on this bond but negotiations were
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;
1810 }
1811
1812 switch (balance) {
1813 case BM_AB:
1814 return bond->active_slave;
1815
1816 case BM_TCP:
1817 if (bond->lacp_status != LACP_NEGOTIATED) {
1818 /* Must have LACP negotiations for TCP balanced bonds. */
1819 return NULL;
1820 }
1821 if (wc) {
1822 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
1823 }
1824 /* Fall Through. */
1825 case BM_SLB:
1826 if (wc) {
1827 flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_ETH_SRC);
1828 }
1829 e = lookup_bond_entry(bond, flow, vlan);
1830 if (!e->slave || !e->slave->enabled) {
1831 e->slave = get_enabled_slave(CONST_CAST(struct bond*, bond));
1832 }
1833 return e->slave;
1834
1835 default:
1836 OVS_NOT_REACHED();
1837 }
1838 }
1839
1840 static struct bond_slave *
1841 bond_choose_slave(const struct bond *bond)
1842 {
1843 struct bond_slave *slave, *best;
1844
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
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
1863 && slave->may_enable
1864 && (!best || slave->delay_expires < best->delay_expires)) {
1865 best = slave;
1866 }
1867 }
1868 return best;
1869 }
1870
1871 static void
1872 bond_choose_active_slave(struct bond *bond)
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());
1887 bond_enable_slave(bond->active_slave, true);
1888 }
1889
1890 bond->send_learning_packets = true;
1891
1892 if (bond->active_slave != old_active_slave) {
1893 bond_active_slave_changed(bond);
1894 }
1895 } else if (old_active_slave) {
1896 bond_active_slave_changed(bond);
1897 VLOG_INFO_RL(&rl, "bond %s: all interfaces disabled", bond->name);
1898 }
1899 }
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. */
1905 bool
1906 bond_get_changed_active_slave(const char *name, struct eth_addr *mac,
1907 bool force)
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) {
1915 *mac = bond->active_slave_mac;
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 }