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