2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "dynamic-string.h"
35 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(bond);
43 /* Bit-mask for hashing a flow down to a bucket.
44 * There are (BOND_MASK + 1) buckets. */
45 #define BOND_MASK 0xff
47 /* A hash bucket for mapping a flow to a slave.
48 * "struct bond" has an array of (BOND_MASK + 1) of these. */
50 struct bond_slave *slave; /* Assigned slave, NULL if unassigned. */
51 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
52 tag_type tag; /* Tag for entry<->slave association. */
53 struct list list_node; /* In bond_slave's 'entries' list. */
56 /* A bond slave, that is, one of the links comprising a bond. */
58 struct hmap_node hmap_node; /* In struct bond's slaves hmap. */
59 struct bond *bond; /* The bond that contains this slave. */
60 void *aux; /* Client-provided handle for this slave. */
62 struct netdev *netdev; /* Network device, owned by the client. */
63 char *name; /* Name (a copy of netdev_get_name(netdev)). */
66 long long delay_expires; /* Time after which 'enabled' may change. */
67 bool up; /* Last link status read from netdev. */
68 bool enabled; /* May be chosen for flows? */
69 tag_type tag; /* Tag associated with this slave. */
71 /* Rebalancing info. Used only by bond_rebalance(). */
72 struct list bal_node; /* In bond_rebalance()'s 'bals' list. */
73 struct list entries; /* 'struct bond_entry's assigned here. */
74 uint64_t tx_bytes; /* Sum across 'tx_bytes' of entries. */
76 /* BM_STABLE specific bonding info. */
77 size_t stb_idx; /* Index in 'bond''s 'stb_slaves' array.
78 Undefined value if participating in a
79 BTM_STABLE bond or not enabled. */
82 /* A bond, that is, a set of network devices grouped to improve performance or
85 struct hmap_node hmap_node; /* In 'all_bonds' hmap. */
86 char *name; /* Name provided by client. */
92 enum bond_mode balance; /* Balancing mode, one of BM_*. */
93 struct bond_slave *active_slave;
94 tag_type no_slaves_tag; /* Tag for flows when all slaves disabled. */
95 int updelay, downdelay; /* Delay before slave goes up/down, in ms. */
97 /* SLB specific bonding info. */
98 struct bond_entry *hash; /* An array of (BOND_MASK + 1) elements. */
99 int rebalance_interval; /* Interval between rebalances, in ms. */
100 long long int next_rebalance; /* Next rebalancing time. */
101 bool send_learning_packets;
103 /* BM_STABLE specific bonding info. */
104 struct bond_slave **stb_slaves; /* Ordered list of enabled slaves. */
105 size_t n_stb_slaves; /* Number of slaves in 'stb_slaves'. */
106 size_t len_stb_slaves; /* Slaves allocated in 'stb_slaves'. */
107 bool stb_need_sort; /* True if stb_slaves is not sorted. */
110 const struct lacp *lacp; /* Client provided LACP handle. */
113 enum bond_detect_mode detect; /* Link status mode, one of BLSM_*. */
114 struct netdev_monitor *monitor; /* detect == BLSM_CARRIER only. */
115 long long int miimon_interval; /* Miimon status refresh interval. */
116 long long int miimon_next_update; /* Time of next miimon update. */
118 /* Legacy compatibility. */
119 long long int next_fake_iface_update; /* LLONG_MAX if disabled. */
121 /* Tag set saved for next bond_run(). This tag set is a kluge for cases
122 * where we can't otherwise provide revalidation feedback to the client.
123 * That's only unixctl commands now; I hope no other cases will arise. */
124 struct tag_set unixctl_tags;
127 static struct hmap all_bonds = HMAP_INITIALIZER(&all_bonds);
129 static void bond_entry_reset(struct bond *);
130 static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_);
131 static bool bond_is_link_up(struct bond *, struct netdev *);
132 static void bond_enable_slave(struct bond_slave *, bool enable,
134 static bool bond_stb_sort(struct bond *);
135 static void bond_stb_enable_slave(struct bond_slave *);
136 static void bond_link_status_update(struct bond_slave *, struct tag_set *);
137 static void bond_choose_active_slave(struct bond *, struct tag_set *);
138 static bool bond_is_tcp_hash(const struct bond *);
139 static unsigned int bond_hash_src(const uint8_t mac[ETH_ADDR_LEN],
141 static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan);
142 static struct bond_entry *lookup_bond_entry(const struct bond *,
145 static tag_type bond_get_active_slave_tag(const struct bond *);
146 static struct bond_slave *choose_output_slave(const struct bond *,
149 static void bond_update_fake_slave_stats(struct bond *);
151 /* Attempts to parse 's' as the name of a bond balancing mode. If successful,
152 * stores the mode in '*balance' and returns true. Otherwise returns false
153 * without modifying '*balance'. */
155 bond_mode_from_string(enum bond_mode *balance, const char *s)
157 if (!strcmp(s, bond_mode_to_string(BM_TCP))) {
159 } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) {
161 } else if (!strcmp(s, bond_mode_to_string(BM_STABLE))) {
162 *balance = BM_STABLE;
163 } else if (!strcmp(s, bond_mode_to_string(BM_AB))) {
171 /* Returns a string representing 'balance'. */
173 bond_mode_to_string(enum bond_mode balance) {
176 return "balance-tcp";
178 return "balance-slb";
182 return "active-backup";
187 /* Attempts to parse 's' as the name of a bond link status detection mode. If
188 * successful, stores the mode in '*detect' and returns true. Otherwise
189 * returns false without modifying '*detect'. */
191 bond_detect_mode_from_string(enum bond_detect_mode *detect, const char *s)
193 if (!strcmp(s, bond_detect_mode_to_string(BLSM_CARRIER))) {
194 *detect = BLSM_CARRIER;
195 } else if (!strcmp(s, bond_detect_mode_to_string(BLSM_MIIMON))) {
196 *detect = BLSM_MIIMON;
203 /* Returns a string representing 'detect'. */
205 bond_detect_mode_to_string(enum bond_detect_mode detect)
216 /* Creates and returns a new bond whose configuration is initially taken from
219 * The caller should register each slave on the new bond by calling
220 * bond_slave_register(). */
222 bond_create(const struct bond_settings *s)
226 bond = xzalloc(sizeof *bond);
227 hmap_init(&bond->slaves);
228 bond->no_slaves_tag = tag_create_random();
229 bond->miimon_next_update = LLONG_MAX;
230 bond->next_fake_iface_update = LLONG_MAX;
232 bond_reconfigure(bond, s);
234 tag_set_init(&bond->unixctl_tags);
241 bond_destroy(struct bond *bond)
243 struct bond_slave *slave, *next_slave;
249 hmap_remove(&all_bonds, &bond->hmap_node);
251 HMAP_FOR_EACH_SAFE (slave, next_slave, hmap_node, &bond->slaves) {
252 hmap_remove(&bond->slaves, &slave->hmap_node);
253 /* Client owns 'slave->netdev'. */
257 hmap_destroy(&bond->slaves);
261 netdev_monitor_destroy(bond->monitor);
267 /* Updates 'bond''s overall configuration to 's'.
269 * The caller should register each slave on 'bond' by calling
270 * bond_slave_register(). This is optional if none of the slaves'
271 * configuration has changed, except that it is mandatory if 's' enables LACP
272 * and 'bond' previously didn't have LACP enabled. In any case it can't
275 * Returns true if the configuration has changed in such a way that requires
279 bond_reconfigure(struct bond *bond, const struct bond_settings *s)
281 bool revalidate = false;
283 if (!bond->name || strcmp(bond->name, s->name)) {
285 hmap_remove(&all_bonds, &bond->hmap_node);
288 bond->name = xstrdup(s->name);
289 hmap_insert(&all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
292 bond->lacp = s->lacp;
293 bond->detect = s->detect;
294 bond->miimon_interval = s->miimon_interval;
295 bond->updelay = s->up_delay;
296 bond->downdelay = s->down_delay;
297 bond->rebalance_interval = s->rebalance_interval;
299 if (bond->balance != s->balance) {
300 bond->balance = s->balance;
304 if (bond->detect == BLSM_CARRIER) {
305 struct bond_slave *slave;
307 if (!bond->monitor) {
308 bond->monitor = netdev_monitor_create();
311 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
312 netdev_monitor_add(bond->monitor, slave->netdev);
315 netdev_monitor_destroy(bond->monitor);
316 bond->monitor = NULL;
318 if (bond->miimon_next_update == LLONG_MAX) {
319 bond->miimon_next_update = time_msec() + bond->miimon_interval;
324 if (bond->next_fake_iface_update == LLONG_MAX) {
325 bond->next_fake_iface_update = time_msec();
328 bond->next_fake_iface_update = LLONG_MAX;
331 if (bond->balance != BM_STABLE) {
332 free(bond->stb_slaves);
333 bond->stb_slaves = NULL;
334 } else if (!bond->stb_slaves) {
335 struct bond_slave *slave;
337 bond->n_stb_slaves = 0;
338 bond->len_stb_slaves = 0;
339 bond->stb_slaves = NULL;
341 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
342 if (slave->enabled) {
343 bond_stb_enable_slave(slave);
348 if (bond->balance == BM_AB || !bond->hash || revalidate) {
349 bond_entry_reset(bond);
355 /* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an
356 * arbitrary client-provided pointer that uniquely identifies a slave within a
357 * bond. If 'slave_' already exists within 'bond' then this function
358 * reconfigures the existing slave.
360 * 'netdev' must be the network device that 'slave_' represents. It is owned
361 * by the client, so the client must not close it before either unregistering
362 * 'slave_' or destroying 'bond'.
364 * If 'bond' has a LACP configuration then 'slave_' should be the same handle
365 * used in the LACP module. */
367 bond_slave_register(struct bond *bond, void *slave_, struct netdev *netdev)
369 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
372 slave = xzalloc(sizeof *slave);
374 hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0));
377 slave->delay_expires = LLONG_MAX;
378 slave->up = bond_is_link_up(bond, netdev);
379 slave->enabled = false;
380 bond_enable_slave(slave, slave->up, NULL);
383 slave->netdev = netdev;
385 slave->name = xstrdup(netdev_get_name(netdev));
388 /* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave
389 * then this function has no effect.
391 * Unregistering a slave invalidates all flows. */
393 bond_slave_unregister(struct bond *bond, const void *slave_)
395 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
402 bond_enable_slave(slave, false, NULL);
404 del_active = bond->active_slave == slave;
406 struct bond_entry *e;
407 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
408 if (e->slave == slave) {
416 hmap_remove(&bond->slaves, &slave->hmap_node);
417 /* Client owns 'slave->netdev'. */
424 bond_choose_active_slave(bond, &tags);
425 bond->send_learning_packets = true;
429 /* Performs periodic maintenance on 'bond'. The caller must provide 'tags' to
430 * allow tagged flows to be invalidated.
432 * The caller should check bond_should_send_learning_packets() afterward. */
434 bond_run(struct bond *bond, struct tag_set *tags)
436 struct bond_slave *slave;
437 bool is_tcp_hash = bond_is_tcp_hash(bond);
439 /* Update link status. */
440 if (bond->detect == BLSM_CARRIER
441 || time_msec() >= bond->miimon_next_update)
443 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
444 slave->up = bond_is_link_up(bond, slave->netdev);
446 bond->miimon_next_update = time_msec() + bond->miimon_interval;
449 /* Enable slaves based on link status and LACP feedback. */
450 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
451 bond_link_status_update(slave, tags);
453 if (!bond->active_slave || !bond->active_slave->enabled) {
454 bond_choose_active_slave(bond, tags);
457 /* Update fake bond interface stats. */
458 if (time_msec() >= bond->next_fake_iface_update) {
459 bond_update_fake_slave_stats(bond);
460 bond->next_fake_iface_update = time_msec() + 1000;
463 if (bond_stb_sort(bond) || is_tcp_hash != bond_is_tcp_hash(bond)) {
464 struct bond_slave *slave;
466 bond_entry_reset(bond);
467 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
468 tag_set_add(tags, slave->tag);
472 /* Invalidate any tags required by */
473 tag_set_union(tags, &bond->unixctl_tags);
474 tag_set_init(&bond->unixctl_tags);
477 /* Causes poll_block() to wake up when 'bond' needs something to be done. */
479 bond_wait(struct bond *bond)
481 struct bond_slave *slave;
483 if (bond->detect == BLSM_CARRIER) {
484 netdev_monitor_poll_wait(bond->monitor);
486 poll_timer_wait_until(bond->miimon_next_update);
489 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
490 if (slave->delay_expires != LLONG_MAX) {
491 poll_timer_wait_until(slave->delay_expires);
495 if (bond->next_fake_iface_update != LLONG_MAX) {
496 poll_timer_wait_until(bond->next_fake_iface_update);
499 /* Ensure that any saved tags get revalidated right away. */
500 if (!tag_set_is_empty(&bond->unixctl_tags)) {
501 poll_immediate_wake();
504 /* We don't wait for bond->next_rebalance because rebalancing can only run
505 * at a flow account checkpoint. ofproto does checkpointing on its own
506 * schedule and bond_rebalance() gets called afterward, so we'd just be
507 * waking up for no purpose. */
510 /* MAC learning table interaction. */
513 may_send_learning_packets(const struct bond *bond)
515 return !lacp_negotiated(bond->lacp) && bond->balance != BM_AB;
518 /* Returns true if 'bond' needs the client to send out packets to assist with
519 * MAC learning on 'bond'. If this function returns true, then the client
520 * should iterate through its MAC learning table for the bridge on which 'bond'
521 * is located. For each MAC that has been learned on a port other than 'bond',
522 * it should call bond_send_learning_packet().
524 * This function will only return true if 'bond' is in SLB mode and LACP is not
525 * negotiated. Otherwise sending learning packets isn't necessary.
527 * Calling this function resets the state that it checks. */
529 bond_should_send_learning_packets(struct bond *bond)
531 bool send = bond->send_learning_packets && may_send_learning_packets(bond);
532 bond->send_learning_packets = false;
536 /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
538 * See bond_should_send_learning_packets() for description of usage. */
540 bond_send_learning_packet(struct bond *bond,
541 const uint8_t eth_src[ETH_ADDR_LEN],
544 struct bond_slave *slave;
545 struct ofpbuf packet;
549 assert(may_send_learning_packets(bond));
550 if (!bond->active_slave) {
551 /* Nowhere to send the learning packet. */
555 memset(&flow, 0, sizeof flow);
556 memcpy(flow.dl_src, eth_src, ETH_ADDR_LEN);
557 slave = choose_output_slave(bond, &flow, vlan);
559 ofpbuf_init(&packet, 0);
560 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
563 eth_set_vlan_tci(&packet, htons(vlan));
565 error = netdev_send(slave->netdev, &packet);
566 ofpbuf_uninit(&packet);
571 /* Checks whether a packet that arrived on 'slave_' within 'bond', with an
572 * Ethernet destination address of 'eth_dst', should be admitted.
574 * The return value is one of the following:
576 * - BV_ACCEPT: Admit the packet.
578 * - BV_DROP: Drop the packet.
580 * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
581 * Ethernet source address and VLAN. If there is none, or if the packet
582 * is on the learned port, then admit the packet. If a different port has
583 * been learned, however, drop the packet (and do not use it for MAC
587 bond_check_admissibility(struct bond *bond, const void *slave_,
588 const uint8_t eth_dst[ETH_ADDR_LEN], tag_type *tags)
590 /* Admit all packets if LACP has been negotiated, because that means that
591 * the remote switch is aware of the bond and will "do the right thing". */
592 if (lacp_negotiated(bond->lacp)) {
596 /* Drop all multicast packets on inactive slaves. */
597 if (eth_addr_is_multicast(eth_dst)) {
598 *tags |= bond_get_active_slave_tag(bond);
599 if (bond->active_slave != bond_slave_lookup(bond, slave_)) {
604 /* Drop all packets for which we have learned a different input port,
605 * because we probably sent the packet on one slave and got it back on the
606 * other. Gratuitous ARP packets are an exception to this rule: the host
607 * has moved to another switch. The exception to the exception is if we
608 * locked the learning table to avoid reflections on bond slaves. */
609 return BV_DROP_IF_MOVED;
612 /* Returns the slave (registered on 'bond' by bond_slave_register()) to which
613 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
614 * NULL if the packet should be dropped because no slaves are enabled.
616 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
617 * should be a VID only (i.e. excluding the PCP bits). Second,
618 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
619 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
620 * packet belongs to (so for an access port it will be the access port's VLAN).
622 * Adds a tag to '*tags' that associates the flow with the returned slave.
625 bond_choose_output_slave(struct bond *bond, const struct flow *flow,
626 uint16_t vlan, tag_type *tags)
628 struct bond_slave *slave = choose_output_slave(bond, flow, vlan);
633 *tags |= bond->no_slaves_tag;
641 bond_is_balanced(const struct bond *bond)
643 return bond->balance == BM_SLB || bond->balance == BM_TCP;
646 /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
648 bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
652 if (bond_is_balanced(bond)) {
653 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
657 static struct bond_slave *
658 bond_slave_from_bal_node(struct list *bal)
660 return CONTAINER_OF(bal, struct bond_slave, bal_node);
664 log_bals(struct bond *bond, const struct list *bals)
666 if (VLOG_IS_DBG_ENABLED()) {
667 struct ds ds = DS_EMPTY_INITIALIZER;
668 const struct bond_slave *slave;
670 LIST_FOR_EACH (slave, bal_node, bals) {
672 ds_put_char(&ds, ',');
674 ds_put_format(&ds, " %s %"PRIu64"kB",
675 slave->name, slave->tx_bytes / 1024);
677 if (!slave->enabled) {
678 ds_put_cstr(&ds, " (disabled)");
680 if (!list_is_empty(&slave->entries)) {
681 struct bond_entry *e;
683 ds_put_cstr(&ds, " (");
684 LIST_FOR_EACH (e, list_node, &slave->entries) {
685 if (&e->list_node != list_front(&slave->entries)) {
686 ds_put_cstr(&ds, " + ");
688 ds_put_format(&ds, "h%td: %"PRIu64"kB",
689 e - bond->hash, e->tx_bytes / 1024);
691 ds_put_cstr(&ds, ")");
694 VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
699 /* Shifts 'hash' from its current slave to 'to'. */
701 bond_shift_load(struct bond_entry *hash, struct bond_slave *to,
704 struct bond_slave *from = hash->slave;
705 struct bond *bond = from->bond;
706 uint64_t delta = hash->tx_bytes;
708 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
709 "from %s to %s (now carrying %"PRIu64"kB and "
710 "%"PRIu64"kB load, respectively)",
711 bond->name, delta / 1024, hash - bond->hash,
712 from->name, to->name,
713 (from->tx_bytes - delta) / 1024,
714 (to->tx_bytes + delta) / 1024);
716 /* Shift load away from 'from' to 'to'. */
717 from->tx_bytes -= delta;
718 to->tx_bytes += delta;
720 /* Arrange for flows to be revalidated. */
721 tag_set_add(set, hash->tag);
723 hash->tag = tag_create_random();
726 /* Pick and returns a bond_entry to migrate to 'to' (the least-loaded slave),
727 * given that doing so must decrease the ratio of the load on the two slaves by
728 * at least 0.1. Returns NULL if there is no appropriate entry.
730 * The list of entries isn't sorted. I don't know of a reason to prefer to
731 * shift away small hashes or large hashes. */
732 static struct bond_entry *
733 choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
735 struct bond_entry *e;
737 if (list_is_short(&from->entries)) {
738 /* 'from' carries no more than one MAC hash, so shifting load away from
739 * it would be pointless. */
743 LIST_FOR_EACH (e, list_node, &from->entries) {
744 double old_ratio, new_ratio;
747 if (to_tx_bytes == 0) {
748 /* Nothing on the new slave, move it. */
753 old_ratio = (double)from->tx_bytes / to_tx_bytes;
754 new_ratio = (double)(from->tx_bytes - delta) / (to_tx_bytes + delta);
755 if (old_ratio - new_ratio > 0.1) {
756 /* Would decrease the ratio, move it. */
764 /* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
767 insert_bal(struct list *bals, struct bond_slave *slave)
769 struct bond_slave *pos;
771 LIST_FOR_EACH (pos, bal_node, bals) {
772 if (slave->tx_bytes > pos->tx_bytes) {
776 list_insert(&pos->bal_node, &slave->bal_node);
779 /* Removes 'slave' from its current list and then inserts it into 'bals' so
780 * that descending order of 'tx_bytes' is maintained. */
782 reinsert_bal(struct list *bals, struct bond_slave *slave)
784 list_remove(&slave->bal_node);
785 insert_bal(bals, slave);
788 /* If 'bond' needs rebalancing, does so.
790 * The caller should have called bond_account() for each active flow, to ensure
791 * that flow data is consistently accounted at this point. */
793 bond_rebalance(struct bond *bond, struct tag_set *tags)
795 struct bond_slave *slave;
796 struct bond_entry *e;
799 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
802 bond->next_rebalance = time_msec() + bond->rebalance_interval;
804 /* Add each bond_entry to its slave's 'entries' list.
805 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
806 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
808 list_init(&slave->entries);
810 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
811 if (e->slave && e->tx_bytes) {
812 e->slave->tx_bytes += e->tx_bytes;
813 list_push_back(&e->slave->entries, &e->list_node);
817 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
819 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
820 * with a proper list sort algorithm. */
822 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
823 if (slave->enabled) {
824 insert_bal(&bals, slave);
827 log_bals(bond, &bals);
829 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
830 while (!list_is_short(&bals)) {
831 struct bond_slave *from = bond_slave_from_bal_node(list_front(&bals));
832 struct bond_slave *to = bond_slave_from_bal_node(list_back(&bals));
835 overload = from->tx_bytes - to->tx_bytes;
836 if (overload < to->tx_bytes >> 5 || overload < 100000) {
837 /* The extra load on 'from' (and all less-loaded slaves), compared
838 * to that of 'to' (the least-loaded slave), is less than ~3%, or
839 * it is less than ~1Mbps. No point in rebalancing. */
843 /* 'from' is carrying significantly more load than 'to', and that load
844 * is split across at least two different hashes. */
845 e = choose_entry_to_migrate(from, to->tx_bytes);
847 bond_shift_load(e, to, tags);
849 /* Delete element from from->entries.
851 * We don't add the element to to->hashes. That would only allow
852 * 'e' to be migrated to another slave in this rebalancing run, and
853 * there is no point in doing that. */
854 list_remove(&e->list_node);
856 /* Re-sort 'bals'. */
857 reinsert_bal(&bals, from);
858 reinsert_bal(&bals, to);
860 /* Can't usefully migrate anything away from 'from'.
861 * Don't reconsider it. */
862 list_remove(&from->bal_node);
866 /* Implement exponentially weighted moving average. A weight of 1/2 causes
867 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
868 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
869 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
877 /* Bonding unixctl user interface functions. */
880 bond_find(const char *name)
884 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
886 if (!strcmp(bond->name, name)) {
893 static struct bond_slave *
894 bond_lookup_slave(struct bond *bond, const char *slave_name)
896 struct bond_slave *slave;
898 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
899 if (!strcmp(slave->name, slave_name)) {
907 bond_unixctl_list(struct unixctl_conn *conn,
908 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
910 struct ds ds = DS_EMPTY_INITIALIZER;
911 const struct bond *bond;
913 ds_put_cstr(&ds, "bond\ttype\tslaves\n");
915 HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
916 const struct bond_slave *slave;
919 ds_put_format(&ds, "%s\t%s\t",
920 bond->name, bond_mode_to_string(bond->balance));
923 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
925 ds_put_cstr(&ds, ", ");
927 ds_put_cstr(&ds, slave->name);
929 ds_put_char(&ds, '\n');
931 unixctl_command_reply(conn, 200, ds_cstr(&ds));
936 bond_unixctl_show(struct unixctl_conn *conn,
937 const char *args, void *aux OVS_UNUSED)
939 struct ds ds = DS_EMPTY_INITIALIZER;
940 const struct bond_slave *slave;
941 const struct bond *bond;
943 bond = bond_find(args);
945 unixctl_command_reply(conn, 501, "no such bond");
949 ds_put_format(&ds, "bond_mode: %s\n",
950 bond_mode_to_string(bond->balance));
953 ds_put_format(&ds, "lacp: %s\n",
954 lacp_is_active(bond->lacp) ? "active" : "passive");
956 ds_put_cstr(&ds, "lacp: off\n");
959 if (bond->balance != BM_AB) {
960 ds_put_format(&ds, "bond-hash-algorithm: %s\n",
961 bond_is_tcp_hash(bond) ? "balance-tcp" : "balance-slb");
964 ds_put_format(&ds, "bond-detect-mode: %s\n",
965 bond->monitor ? "carrier" : "miimon");
967 if (!bond->monitor) {
968 ds_put_format(&ds, "bond-miimon-interval: %lld\n",
969 bond->miimon_interval);
972 ds_put_format(&ds, "updelay: %d ms\n", bond->updelay);
973 ds_put_format(&ds, "downdelay: %d ms\n", bond->downdelay);
975 if (bond_is_balanced(bond)) {
976 ds_put_format(&ds, "next rebalance: %lld ms\n",
977 bond->next_rebalance - time_msec());
980 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
981 struct bond_entry *be;
985 ds_put_format(&ds, "\nslave %s: %s\n",
986 slave->name, slave->enabled ? "enabled" : "disabled");
987 if (slave == bond->active_slave) {
988 ds_put_cstr(&ds, "\tactive slave\n");
990 if (slave->delay_expires != LLONG_MAX) {
991 ds_put_format(&ds, "\t%s expires in %lld ms\n",
992 slave->enabled ? "downdelay" : "updelay",
993 slave->delay_expires - time_msec());
996 if (!bond_is_balanced(bond)) {
1001 memset(&flow, 0, sizeof flow);
1002 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1003 int hash = be - bond->hash;
1005 if (be->slave != slave) {
1009 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
1010 hash, be->tx_bytes / 1024);
1012 if (bond->balance != BM_SLB) {
1016 /* XXX How can we list the MACs assigned to hashes? */
1019 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1024 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_,
1025 void *aux OVS_UNUSED)
1027 char *args = (char *) args_;
1028 char *save_ptr = NULL;
1029 char *bond_s, *hash_s, *slave_s;
1031 struct bond_slave *slave;
1032 struct bond_entry *entry;
1035 bond_s = strtok_r(args, " ", &save_ptr);
1036 hash_s = strtok_r(NULL, " ", &save_ptr);
1037 slave_s = strtok_r(NULL, " ", &save_ptr);
1039 unixctl_command_reply(conn, 501,
1040 "usage: bond/migrate BOND HASH SLAVE");
1044 bond = bond_find(bond_s);
1046 unixctl_command_reply(conn, 501, "no such bond");
1050 if (bond->balance != BM_SLB) {
1051 unixctl_command_reply(conn, 501, "not an SLB bond");
1055 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1056 hash = atoi(hash_s) & BOND_MASK;
1058 unixctl_command_reply(conn, 501, "bad hash");
1062 slave = bond_lookup_slave(bond, slave_s);
1064 unixctl_command_reply(conn, 501, "no such slave");
1068 if (!slave->enabled) {
1069 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
1073 entry = &bond->hash[hash];
1074 tag_set_add(&bond->unixctl_tags, entry->tag);
1075 entry->slave = slave;
1076 entry->tag = tag_create_random();
1077 unixctl_command_reply(conn, 200, "migrated");
1081 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_,
1082 void *aux OVS_UNUSED)
1084 char *args = (char *) args_;
1085 char *save_ptr = NULL;
1086 char *bond_s, *slave_s;
1088 struct bond_slave *slave;
1090 bond_s = strtok_r(args, " ", &save_ptr);
1091 slave_s = strtok_r(NULL, " ", &save_ptr);
1093 unixctl_command_reply(conn, 501,
1094 "usage: bond/set-active-slave BOND SLAVE");
1098 bond = bond_find(bond_s);
1100 unixctl_command_reply(conn, 501, "no such bond");
1104 slave = bond_lookup_slave(bond, slave_s);
1106 unixctl_command_reply(conn, 501, "no such slave");
1110 if (!slave->enabled) {
1111 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
1115 if (bond->active_slave != slave) {
1116 tag_set_add(&bond->unixctl_tags, bond_get_active_slave_tag(bond));
1117 bond->active_slave = slave;
1118 bond->active_slave->tag = tag_create_random();
1119 VLOG_INFO("bond %s: active interface is now %s",
1120 bond->name, slave->name);
1121 bond->send_learning_packets = true;
1122 unixctl_command_reply(conn, 200, "done");
1124 unixctl_command_reply(conn, 200, "no change");
1129 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
1131 char *args = (char *) args_;
1132 char *save_ptr = NULL;
1133 char *bond_s, *slave_s;
1135 struct bond_slave *slave;
1137 bond_s = strtok_r(args, " ", &save_ptr);
1138 slave_s = strtok_r(NULL, " ", &save_ptr);
1140 char *usage = xasprintf("usage: bond/%s-slave BOND SLAVE",
1141 enable ? "enable" : "disable");
1142 unixctl_command_reply(conn, 501, usage);
1147 bond = bond_find(bond_s);
1149 unixctl_command_reply(conn, 501, "no such bond");
1153 slave = bond_lookup_slave(bond, slave_s);
1155 unixctl_command_reply(conn, 501, "no such slave");
1159 bond_enable_slave(slave, enable, &bond->unixctl_tags);
1160 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
1164 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args,
1165 void *aux OVS_UNUSED)
1167 enable_slave(conn, args, true);
1171 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args,
1172 void *aux OVS_UNUSED)
1174 enable_slave(conn, args, false);
1178 bond_unixctl_hash(struct unixctl_conn *conn, const char *args_,
1179 void *aux OVS_UNUSED)
1181 char *args = (char *) args_;
1182 uint8_t mac[ETH_ADDR_LEN];
1186 char *mac_s, *vlan_s;
1187 char *save_ptr = NULL;
1189 mac_s = strtok_r(args, " ", &save_ptr);
1190 vlan_s = strtok_r(NULL, " ", &save_ptr);
1193 if (sscanf(vlan_s, "%u", &vlan) != 1) {
1194 unixctl_command_reply(conn, 501, "invalid vlan");
1198 vlan = OFP_VLAN_NONE;
1201 if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
1202 == ETH_ADDR_SCAN_COUNT) {
1203 hash = bond_hash_src(mac, vlan) & BOND_MASK;
1205 hash_cstr = xasprintf("%u", hash);
1206 unixctl_command_reply(conn, 200, hash_cstr);
1209 unixctl_command_reply(conn, 501, "invalid mac");
1216 unixctl_command_register("bond/list", bond_unixctl_list, NULL);
1217 unixctl_command_register("bond/show", bond_unixctl_show, NULL);
1218 unixctl_command_register("bond/migrate", bond_unixctl_migrate, NULL);
1219 unixctl_command_register("bond/set-active-slave",
1220 bond_unixctl_set_active_slave, NULL);
1221 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave,
1223 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave,
1225 unixctl_command_register("bond/hash", bond_unixctl_hash, NULL);
1229 bond_entry_reset(struct bond *bond)
1231 if (bond->balance != BM_AB) {
1232 size_t hash_len = (BOND_MASK + 1) * sizeof *bond->hash;
1235 bond->hash = xmalloc(hash_len);
1237 memset(bond->hash, 0, hash_len);
1239 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1246 static struct bond_slave *
1247 bond_slave_lookup(struct bond *bond, const void *slave_)
1249 struct bond_slave *slave;
1251 HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1253 if (slave->aux == slave_) {
1262 bond_is_link_up(struct bond *bond, struct netdev *netdev)
1264 return (bond->detect == BLSM_CARRIER
1265 ? netdev_get_carrier(netdev)
1266 : netdev_get_miimon(netdev));
1270 bond_stb_sort_cmp__(const void *a_, const void *b_)
1272 const struct bond_slave *const *ap = a_;
1273 const struct bond_slave *const *bp = b_;
1274 const struct bond_slave *a = *ap;
1275 const struct bond_slave *b = *bp;
1276 const struct lacp *lacp = a->bond->lacp;
1280 a_id = lacp_slave_get_port_id(lacp, a->aux);
1281 b_id = lacp_slave_get_port_id(lacp, b->aux);
1283 a_id = netdev_get_ifindex(a->netdev);
1284 b_id = netdev_get_ifindex(b->netdev);
1287 return (a_id == b_id ? 0 : (a_id < b_id ? -1 : 1));
1291 bond_stb_sort(struct bond *bond)
1295 if (!bond->stb_slaves || !bond->stb_need_sort) {
1298 bond->stb_need_sort = false;
1300 qsort(bond->stb_slaves, bond->n_stb_slaves, sizeof *bond->stb_slaves,
1301 bond_stb_sort_cmp__);
1303 for (i = 0; i < bond->n_stb_slaves; i++) {
1304 bond->stb_slaves[i]->stb_idx = i;
1311 bond_stb_enable_slave(struct bond_slave *slave)
1313 struct bond *bond = slave->bond;
1315 if (!bond->stb_slaves) {
1319 bond->stb_need_sort = true;
1321 if (slave->enabled) {
1322 if (bond->len_stb_slaves <= bond->n_stb_slaves) {
1323 bond->stb_slaves = x2nrealloc(bond->stb_slaves,
1324 &bond->len_stb_slaves,
1325 sizeof *bond->stb_slaves);
1328 slave->stb_idx = bond->n_stb_slaves++;
1329 bond->stb_slaves[slave->stb_idx] = slave;
1331 size_t index = slave->stb_idx;
1332 bond->stb_slaves[index] = bond->stb_slaves[--bond->n_stb_slaves];
1333 bond->stb_slaves[index]->stb_idx = index;
1338 bond_enable_slave(struct bond_slave *slave, bool enable, struct tag_set *tags)
1340 slave->delay_expires = LLONG_MAX;
1341 if (enable != slave->enabled) {
1342 slave->enabled = enable;
1343 if (!slave->enabled) {
1344 VLOG_WARN("interface %s: disabled", slave->name);
1346 tag_set_add(tags, slave->tag);
1349 VLOG_WARN("interface %s: enabled", slave->name);
1350 slave->tag = tag_create_random();
1352 bond_stb_enable_slave(slave);
1357 bond_link_status_update(struct bond_slave *slave, struct tag_set *tags)
1359 struct bond *bond = slave->bond;
1362 up = slave->up && lacp_slave_may_enable(bond->lacp, slave->aux);
1363 if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1364 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1365 VLOG_INFO_RL(&rl, "interface %s: link state %s",
1366 slave->name, up ? "up" : "down");
1367 if (up == slave->enabled) {
1368 slave->delay_expires = LLONG_MAX;
1369 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1370 slave->name, up ? "disabled" : "enabled");
1372 int delay = (lacp_negotiated(bond->lacp) ? 0
1373 : up ? bond->updelay : bond->downdelay);
1374 slave->delay_expires = time_msec() + delay;
1376 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1379 up ? "enabled" : "disabled",
1386 if (time_msec() >= slave->delay_expires) {
1387 bond_enable_slave(slave, up, tags);
1392 bond_is_tcp_hash(const struct bond *bond)
1394 return (bond->balance == BM_TCP || bond->balance == BM_STABLE)
1395 && lacp_negotiated(bond->lacp);
1399 bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
1401 return hash_bytes(mac, ETH_ADDR_LEN, vlan);
1405 bond_hash_tcp(const struct flow *flow, uint16_t vlan)
1407 struct flow hash_flow = *flow;
1408 hash_flow.vlan_tci = vlan;
1410 /* The symmetric quality of this hash function is not required, but
1411 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1412 * purposes, so we use it out of convenience. */
1413 return flow_hash_symmetric_l4(&hash_flow, 0);
1417 bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1419 assert(bond->balance != BM_AB);
1421 return (bond_is_tcp_hash(bond)
1422 ? bond_hash_tcp(flow, vlan)
1423 : bond_hash_src(flow->dl_src, vlan));
1426 static struct bond_entry *
1427 lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1430 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
1433 static struct bond_slave *
1434 choose_output_slave(const struct bond *bond, const struct flow *flow,
1437 struct bond_entry *e;
1439 switch (bond->balance) {
1441 return bond->active_slave;
1444 if (bond->n_stb_slaves) {
1445 return bond->stb_slaves[bond_hash(bond, flow, vlan)
1446 % bond->n_stb_slaves];
1448 return bond->active_slave;
1453 e = lookup_bond_entry(bond, flow, vlan);
1454 if (!e->slave || !e->slave->enabled) {
1455 e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
1456 struct bond_slave, hmap_node);
1457 if (!e->slave->enabled) {
1458 e->slave = bond->active_slave;
1460 e->tag = tag_create_random();
1469 static struct bond_slave *
1470 bond_choose_slave(const struct bond *bond)
1472 struct bond_slave *slave, *best;
1474 /* Find an enabled slave. */
1475 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1476 if (slave->enabled) {
1481 /* All interfaces are disabled. Find an interface that will be enabled
1482 * after its updelay expires. */
1484 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1485 if (slave->delay_expires != LLONG_MAX
1486 && lacp_slave_may_enable(bond->lacp, slave->aux)
1487 && (!best || slave->delay_expires < best->delay_expires)) {
1495 bond_choose_active_slave(struct bond *bond, struct tag_set *tags)
1497 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1498 struct bond_slave *old_active_slave = bond->active_slave;
1500 bond->active_slave = bond_choose_slave(bond);
1501 if (bond->active_slave) {
1502 if (bond->active_slave->enabled) {
1503 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1504 bond->name, bond->active_slave->name);
1506 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1507 "remaining %lld ms updelay (since no interface was "
1508 "enabled)", bond->name, bond->active_slave->name,
1509 bond->active_slave->delay_expires - time_msec());
1510 bond_enable_slave(bond->active_slave, true, tags);
1513 if (!old_active_slave) {
1514 tag_set_add(tags, bond->no_slaves_tag);
1517 bond->send_learning_packets = true;
1518 } else if (old_active_slave) {
1519 VLOG_WARN_RL(&rl, "bond %s: all interfaces disabled", bond->name);
1523 /* Returns the tag for 'bond''s active slave, or 'bond''s no_slaves_tag if
1524 * there is no active slave. */
1526 bond_get_active_slave_tag(const struct bond *bond)
1528 return (bond->active_slave
1529 ? bond->active_slave->tag
1530 : bond->no_slaves_tag);
1533 /* Attempts to make the sum of the bond slaves' statistics appear on the fake
1534 * bond interface. */
1536 bond_update_fake_slave_stats(struct bond *bond)
1538 struct netdev_stats bond_stats;
1539 struct bond_slave *slave;
1540 struct netdev *bond_dev;
1542 memset(&bond_stats, 0, sizeof bond_stats);
1544 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1545 struct netdev_stats slave_stats;
1547 if (!netdev_get_stats(slave->netdev, &slave_stats)) {
1548 /* XXX: We swap the stats here because they are swapped back when
1549 * reported by the internal device. The reason for this is
1550 * internal devices normally represent packets going into the
1551 * system but when used as fake bond device they represent packets
1552 * leaving the system. We really should do this in the internal
1553 * device itself because changing it here reverses the counts from
1554 * the perspective of the switch. However, the internal device
1555 * doesn't know what type of device it represents so we have to do
1556 * it here for now. */
1557 bond_stats.tx_packets += slave_stats.rx_packets;
1558 bond_stats.tx_bytes += slave_stats.rx_bytes;
1559 bond_stats.rx_packets += slave_stats.tx_packets;
1560 bond_stats.rx_bytes += slave_stats.tx_bytes;
1564 if (!netdev_open_default(bond->name, &bond_dev)) {
1565 netdev_set_stats(bond_dev, &bond_stats);
1566 netdev_close(bond_dev);