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 COVERAGE_DEFINE(bond_process_lacp);
45 /* Bit-mask for hashing a flow down to a bucket.
46 * There are (BOND_MASK + 1) buckets. */
47 #define BOND_MASK 0xff
49 /* A hash bucket for mapping a flow to a slave.
50 * "struct bond" has an array of (BOND_MASK + 1) of these. */
52 struct bond_slave *slave; /* Assigned slave, NULL if unassigned. */
53 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
54 tag_type tag; /* Tag for entry<->slave association. */
55 struct list list_node; /* In bond_slave's 'entries' list. */
58 /* A bond slave, that is, one of the links comprising a bond. */
60 struct hmap_node hmap_node; /* In struct bond's slaves hmap. */
61 struct bond *bond; /* The bond that contains this slave. */
62 void *aux; /* Client-provided handle for this slave. */
64 struct netdev *netdev; /* Network device, owned by the client. */
65 char *name; /* Name (a copy of netdev_get_name(netdev)). */
68 long long delay_expires; /* Time after which 'enabled' may change. */
69 bool up; /* Last link status read from netdev. */
70 bool enabled; /* May be chosen for flows? */
71 tag_type tag; /* Tag associated with this slave. */
73 /* Rebalancing info. Used only by bond_rebalance(). */
74 struct list bal_node; /* In bond_rebalance()'s 'bals' list. */
75 struct list entries; /* 'struct bond_entry's assigned here. */
76 uint64_t tx_bytes; /* Sum across 'tx_bytes' of entries. */
78 /* BM_STABLE specific bonding info. */
79 size_t stb_idx; /* Index in 'bond''s 'stb_slaves' array.
80 Undefined value if participating in a
81 BTM_STABLE bond or not enabled. */
84 /* A bond, that is, a set of network devices grouped to improve performance or
87 struct hmap_node hmap_node; /* In 'all_bonds' hmap. */
88 char *name; /* Name provided by client. */
94 enum bond_mode balance; /* Balancing mode, one of BM_*. */
95 struct bond_slave *active_slave;
96 tag_type no_slaves_tag; /* Tag for flows when all slaves disabled. */
97 int updelay, downdelay; /* Delay before slave goes up/down, in ms. */
99 /* SLB specific bonding info. */
100 struct bond_entry *hash; /* An array of (BOND_MASK + 1) elements. */
101 int rebalance_interval; /* Interval between rebalances, in ms. */
102 long long int next_rebalance; /* Next rebalancing time. */
103 bool send_learning_packets;
105 /* BM_STABLE specific bonding info. */
106 struct bond_slave **stb_slaves; /* Ordered list of enabled slaves. */
107 size_t n_stb_slaves; /* Number of slaves in 'stb_slaves'. */
108 size_t len_stb_slaves; /* Slaves allocated in 'stb_slaves'. */
109 bool stb_need_sort; /* True if stb_slaves is not sorted. */
112 struct lacp *lacp; /* LACP object. NULL if LACP is disabled. */
115 enum bond_detect_mode detect; /* Link status mode, one of BLSM_*. */
116 struct netdev_monitor *monitor; /* detect == BLSM_CARRIER only. */
117 long long int miimon_interval; /* Miimon status refresh interval. */
118 long long int miimon_next_update; /* Time of next miimon update. */
120 /* Legacy compatibility. */
121 long long int next_fake_iface_update; /* LLONG_MAX if disabled. */
123 /* Tag set saved for next bond_run(). This tag set is a kluge for cases
124 * where we can't otherwise provide revalidation feedback to the client.
125 * That's only unixctl commands now; I hope no other cases will arise. */
126 struct tag_set unixctl_tags;
129 static struct hmap all_bonds = HMAP_INITIALIZER(&all_bonds);
131 static void bond_entry_reset(struct bond *);
132 static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_);
133 static bool bond_is_link_up(struct bond *, struct netdev *);
134 static void bond_enable_slave(struct bond_slave *, bool enable,
136 static bool bond_stb_sort(struct bond *);
137 static void bond_stb_enable_slave(struct bond_slave *);
138 static void bond_link_status_update(struct bond_slave *, struct tag_set *);
139 static void bond_choose_active_slave(struct bond *, struct tag_set *);
140 static bool bond_is_tcp_hash(const struct bond *);
141 static unsigned int bond_hash_src(const uint8_t mac[ETH_ADDR_LEN],
143 static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan);
144 static struct bond_entry *lookup_bond_entry(const struct bond *,
147 static tag_type bond_get_active_slave_tag(const struct bond *);
148 static struct bond_slave *choose_output_slave(const struct bond *,
151 static void bond_update_fake_slave_stats(struct bond *);
153 /* Attempts to parse 's' as the name of a bond balancing mode. If successful,
154 * stores the mode in '*balance' and returns true. Otherwise returns false
155 * without modifying '*balance'. */
157 bond_mode_from_string(enum bond_mode *balance, const char *s)
159 if (!strcmp(s, bond_mode_to_string(BM_TCP))) {
161 } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) {
163 } else if (!strcmp(s, bond_mode_to_string(BM_STABLE))) {
164 *balance = BM_STABLE;
165 } else if (!strcmp(s, bond_mode_to_string(BM_AB))) {
173 /* Returns a string representing 'balance'. */
175 bond_mode_to_string(enum bond_mode balance) {
178 return "balance-tcp";
180 return "balance-slb";
184 return "active-backup";
189 /* Attempts to parse 's' as the name of a bond link status detection mode. If
190 * successful, stores the mode in '*detect' and returns true. Otherwise
191 * returns false without modifying '*detect'. */
193 bond_detect_mode_from_string(enum bond_detect_mode *detect, const char *s)
195 if (!strcmp(s, bond_detect_mode_to_string(BLSM_CARRIER))) {
196 *detect = BLSM_CARRIER;
197 } else if (!strcmp(s, bond_detect_mode_to_string(BLSM_MIIMON))) {
198 *detect = BLSM_MIIMON;
205 /* Returns a string representing 'detect'. */
207 bond_detect_mode_to_string(enum bond_detect_mode detect)
218 /* Creates and returns a new bond whose configuration is initially taken from
221 * The caller should register each slave on the new bond by calling
222 * bond_slave_register(). */
224 bond_create(const struct bond_settings *s)
228 bond = xzalloc(sizeof *bond);
229 hmap_init(&bond->slaves);
230 bond->no_slaves_tag = tag_create_random();
231 bond->miimon_next_update = LLONG_MAX;
232 bond->next_fake_iface_update = LLONG_MAX;
234 bond_reconfigure(bond, s);
236 tag_set_init(&bond->unixctl_tags);
243 bond_destroy(struct bond *bond)
245 struct bond_slave *slave, *next_slave;
251 hmap_remove(&all_bonds, &bond->hmap_node);
253 HMAP_FOR_EACH_SAFE (slave, next_slave, hmap_node, &bond->slaves) {
254 hmap_remove(&bond->slaves, &slave->hmap_node);
255 /* Client owns 'slave->netdev'. */
259 hmap_destroy(&bond->slaves);
263 lacp_destroy(bond->lacp);
265 netdev_monitor_destroy(bond->monitor);
271 /* Updates 'bond''s overall configuration to 's'.
273 * The caller should register each slave on 'bond' by calling
274 * bond_slave_register(). This is optional if none of the slaves'
275 * configuration has changed, except that it is mandatory if 's' enables LACP
276 * and 'bond' previously didn't have LACP enabled. In any case it can't
279 * Returns true if the configuration has changed in such a way that requires
283 bond_reconfigure(struct bond *bond, const struct bond_settings *s)
285 bool revalidate = false;
287 if (!bond->name || strcmp(bond->name, s->name)) {
289 hmap_remove(&all_bonds, &bond->hmap_node);
292 bond->name = xstrdup(s->name);
293 hmap_insert(&all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
296 bond->detect = s->detect;
297 bond->miimon_interval = s->miimon_interval;
298 bond->updelay = s->up_delay;
299 bond->downdelay = s->down_delay;
300 bond->rebalance_interval = s->rebalance_interval;
302 if (bond->balance != s->balance) {
303 bond->balance = s->balance;
307 if (bond->detect == BLSM_CARRIER) {
308 struct bond_slave *slave;
310 if (!bond->monitor) {
311 bond->monitor = netdev_monitor_create();
314 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
315 netdev_monitor_add(bond->monitor, slave->netdev);
318 netdev_monitor_destroy(bond->monitor);
319 bond->monitor = NULL;
321 if (bond->miimon_next_update == LLONG_MAX) {
322 bond->miimon_next_update = time_msec() + bond->miimon_interval;
328 bond->lacp = lacp_create();
330 lacp_configure(bond->lacp, s->lacp);
332 lacp_destroy(bond->lacp);
337 if (bond->next_fake_iface_update == LLONG_MAX) {
338 bond->next_fake_iface_update = time_msec();
341 bond->next_fake_iface_update = LLONG_MAX;
344 if (bond->balance != BM_STABLE) {
345 free(bond->stb_slaves);
346 bond->stb_slaves = NULL;
347 } else if (!bond->stb_slaves) {
348 struct bond_slave *slave;
350 bond->n_stb_slaves = 0;
351 bond->len_stb_slaves = 0;
352 bond->stb_slaves = NULL;
354 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
355 if (slave->enabled) {
356 bond_stb_enable_slave(slave);
361 if (bond->balance == BM_AB || !bond->hash || revalidate) {
362 bond_entry_reset(bond);
368 /* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an
369 * arbitrary client-provided pointer that uniquely identifies a slave within a
370 * bond. If 'slave_' already exists within 'bond' then this function
371 * reconfigures the existing slave.
373 * 'netdev' must be the network device that 'slave_' represents. It is owned
374 * by the client, so the client must not close it before either unregistering
375 * 'slave_' or destroying 'bond'.
377 * If 'bond' has a LACP configuration then 'lacp_settings' must point to LACP
378 * settings for 'slave_'; otherwise 'lacp_settings' is ignored.
381 bond_slave_register(struct bond *bond, void *slave_, struct netdev *netdev,
382 const struct lacp_slave_settings *lacp_settings)
384 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
387 slave = xzalloc(sizeof *slave);
389 hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0));
392 slave->delay_expires = LLONG_MAX;
393 slave->up = bond_is_link_up(bond, netdev);
394 slave->enabled = false;
395 bond_enable_slave(slave, slave->up, NULL);
398 slave->netdev = netdev;
400 slave->name = xstrdup(netdev_get_name(netdev));
403 assert(lacp_settings != NULL);
404 lacp_slave_register(bond->lacp, slave, lacp_settings);
408 /* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave
409 * then this function has no effect.
411 * Unregistering a slave invalidates all flows. */
413 bond_slave_unregister(struct bond *bond, const void *slave_)
415 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
422 bond_enable_slave(slave, false, NULL);
424 del_active = bond->active_slave == slave;
426 struct bond_entry *e;
427 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
428 if (e->slave == slave) {
436 hmap_remove(&bond->slaves, &slave->hmap_node);
437 /* Client owns 'slave->netdev'. */
444 bond_choose_active_slave(bond, &tags);
445 bond->send_learning_packets = true;
449 /* Callback for lacp_run(). */
451 bond_send_pdu_cb(void *slave_, const struct lacp_pdu *pdu)
453 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
454 struct bond_slave *slave = slave_;
455 uint8_t ea[ETH_ADDR_LEN];
458 error = netdev_get_etheraddr(slave->netdev, ea);
460 struct lacp_pdu *packet_pdu;
461 struct ofpbuf packet;
463 ofpbuf_init(&packet, 0);
464 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
467 error = netdev_send(slave->netdev, &packet);
469 VLOG_WARN_RL(&rl, "bond %s: sending LACP PDU on slave %s failed "
471 slave->bond->name, slave->name, strerror(error));
473 ofpbuf_uninit(&packet);
475 VLOG_ERR_RL(&rl, "bond %s: cannot obtain Ethernet address of slave "
477 slave->bond->name, slave->name, strerror(error));
481 /* Performs periodic maintenance on 'bond'. The caller must provide 'tags' to
482 * allow tagged flows to be invalidated.
484 * The caller should check bond_should_send_learning_packets() afterward. */
486 bond_run(struct bond *bond, struct tag_set *tags)
488 struct bond_slave *slave;
489 bool is_tcp_hash = bond_is_tcp_hash(bond);
491 /* Update link status. */
492 if (bond->detect == BLSM_CARRIER
493 || time_msec() >= bond->miimon_next_update)
495 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
496 slave->up = bond_is_link_up(bond, slave->netdev);
498 bond->miimon_next_update = time_msec() + bond->miimon_interval;
503 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
504 lacp_slave_enable(bond->lacp, slave, slave->enabled);
507 lacp_run(bond->lacp, bond_send_pdu_cb);
510 /* Enable slaves based on link status and LACP feedback. */
511 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
512 bond_link_status_update(slave, tags);
514 if (!bond->active_slave || !bond->active_slave->enabled) {
515 bond_choose_active_slave(bond, tags);
518 /* Update fake bond interface stats. */
519 if (time_msec() >= bond->next_fake_iface_update) {
520 bond_update_fake_slave_stats(bond);
521 bond->next_fake_iface_update = time_msec() + 1000;
524 if (bond_stb_sort(bond) || is_tcp_hash != bond_is_tcp_hash(bond)) {
525 struct bond_slave *slave;
527 bond_entry_reset(bond);
528 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
529 tag_set_add(tags, slave->tag);
533 /* Invalidate any tags required by */
534 tag_set_union(tags, &bond->unixctl_tags);
535 tag_set_init(&bond->unixctl_tags);
538 /* Causes poll_block() to wake up when 'bond' needs something to be done. */
540 bond_wait(struct bond *bond)
542 struct bond_slave *slave;
544 if (bond->detect == BLSM_CARRIER) {
545 netdev_monitor_poll_wait(bond->monitor);
547 poll_timer_wait_until(bond->miimon_next_update);
550 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
551 if (slave->delay_expires != LLONG_MAX) {
552 poll_timer_wait_until(slave->delay_expires);
556 if (bond->next_fake_iface_update != LLONG_MAX) {
557 poll_timer_wait_until(bond->next_fake_iface_update);
560 /* Ensure that any saved tags get revalidated right away. */
561 if (!tag_set_is_empty(&bond->unixctl_tags)) {
562 poll_immediate_wake();
565 /* We don't wait for bond->next_rebalance because rebalancing can only run
566 * at a flow account checkpoint. ofproto does checkpointing on its own
567 * schedule and bond_rebalance() gets called afterward, so we'd just be
568 * waking up for no purpose. */
571 /* MAC learning table interaction. */
574 may_send_learning_packets(const struct bond *bond)
576 return !lacp_negotiated(bond->lacp) && bond->balance != BM_AB;
579 /* Returns true if 'bond' needs the client to send out packets to assist with
580 * MAC learning on 'bond'. If this function returns true, then the client
581 * should iterate through its MAC learning table for the bridge on which 'bond'
582 * is located. For each MAC that has been learned on a port other than 'bond',
583 * it should call bond_send_learning_packet().
585 * This function will only return true if 'bond' is in SLB mode and LACP is not
586 * negotiated. Otherwise sending learning packets isn't necessary.
588 * Calling this function resets the state that it checks. */
590 bond_should_send_learning_packets(struct bond *bond)
592 bool send = bond->send_learning_packets && may_send_learning_packets(bond);
593 bond->send_learning_packets = false;
597 /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
599 * See bond_should_send_learning_packets() for description of usage. */
601 bond_send_learning_packet(struct bond *bond,
602 const uint8_t eth_src[ETH_ADDR_LEN],
605 struct bond_slave *slave;
606 struct ofpbuf packet;
610 assert(may_send_learning_packets(bond));
611 if (!bond->active_slave) {
612 /* Nowhere to send the learning packet. */
616 memset(&flow, 0, sizeof flow);
617 memcpy(flow.dl_src, eth_src, ETH_ADDR_LEN);
618 slave = choose_output_slave(bond, &flow, vlan);
620 ofpbuf_init(&packet, 0);
621 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
624 eth_set_vlan_tci(&packet, htons(vlan));
626 error = netdev_send(slave->netdev, &packet);
627 ofpbuf_uninit(&packet);
632 /* Checks whether a packet that arrived on 'slave_' within 'bond', with an
633 * Ethernet destination address of 'eth_dst', should be admitted.
635 * The return value is one of the following:
637 * - BV_ACCEPT: Admit the packet.
639 * - BV_DROP: Drop the packet.
641 * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
642 * Ethernet source address and VLAN. If there is none, or if the packet
643 * is on the learned port, then admit the packet. If a different port has
644 * been learned, however, drop the packet (and do not use it for MAC
648 bond_check_admissibility(struct bond *bond, const void *slave_,
649 const uint8_t eth_dst[ETH_ADDR_LEN], tag_type *tags)
651 /* Admit all packets if LACP has been negotiated, because that means that
652 * the remote switch is aware of the bond and will "do the right thing". */
653 if (lacp_negotiated(bond->lacp)) {
657 /* Drop all multicast packets on inactive slaves. */
658 if (eth_addr_is_multicast(eth_dst)) {
659 *tags |= bond_get_active_slave_tag(bond);
660 if (bond->active_slave != bond_slave_lookup(bond, slave_)) {
665 /* Drop all packets for which we have learned a different input port,
666 * because we probably sent the packet on one slave and got it back on the
667 * other. Gratuitous ARP packets are an exception to this rule: the host
668 * has moved to another switch. The exception to the exception is if we
669 * locked the learning table to avoid reflections on bond slaves. */
670 return BV_DROP_IF_MOVED;
673 /* Returns the slave (registered on 'bond' by bond_slave_register()) to which
674 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
675 * NULL if the packet should be dropped because no slaves are enabled.
677 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
678 * should be a VID only (i.e. excluding the PCP bits). Second,
679 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
680 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
681 * packet belongs to (so for an access port it will be the access port's VLAN).
683 * Adds a tag to '*tags' that associates the flow with the returned slave.
686 bond_choose_output_slave(struct bond *bond, const struct flow *flow,
687 uint16_t vlan, tag_type *tags)
689 struct bond_slave *slave = choose_output_slave(bond, flow, vlan);
694 *tags |= bond->no_slaves_tag;
699 /* Processes LACP packet 'packet', which was received on 'slave_' within
702 * The client should use this function to pass along LACP messages received on
703 * any of 'bond''s slaves. */
705 bond_process_lacp(struct bond *bond, void *slave_, const struct ofpbuf *packet)
708 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
709 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
711 COVERAGE_INC(bond_process_lacp);
712 lacp_process_pdu(bond->lacp, slave, pdu);
720 bond_is_balanced(const struct bond *bond)
722 return bond->balance == BM_SLB || bond->balance == BM_TCP;
725 /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
727 bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
731 if (bond_is_balanced(bond)) {
732 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
736 static struct bond_slave *
737 bond_slave_from_bal_node(struct list *bal)
739 return CONTAINER_OF(bal, struct bond_slave, bal_node);
743 log_bals(struct bond *bond, const struct list *bals)
745 if (VLOG_IS_DBG_ENABLED()) {
746 struct ds ds = DS_EMPTY_INITIALIZER;
747 const struct bond_slave *slave;
749 LIST_FOR_EACH (slave, bal_node, bals) {
751 ds_put_char(&ds, ',');
753 ds_put_format(&ds, " %s %"PRIu64"kB",
754 slave->name, slave->tx_bytes / 1024);
756 if (!slave->enabled) {
757 ds_put_cstr(&ds, " (disabled)");
759 if (!list_is_empty(&slave->entries)) {
760 struct bond_entry *e;
762 ds_put_cstr(&ds, " (");
763 LIST_FOR_EACH (e, list_node, &slave->entries) {
764 if (&e->list_node != list_front(&slave->entries)) {
765 ds_put_cstr(&ds, " + ");
767 ds_put_format(&ds, "h%td: %"PRIu64"kB",
768 e - bond->hash, e->tx_bytes / 1024);
770 ds_put_cstr(&ds, ")");
773 VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
778 /* Shifts 'hash' from its current slave to 'to'. */
780 bond_shift_load(struct bond_entry *hash, struct bond_slave *to,
783 struct bond_slave *from = hash->slave;
784 struct bond *bond = from->bond;
785 uint64_t delta = hash->tx_bytes;
787 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
788 "from %s to %s (now carrying %"PRIu64"kB and "
789 "%"PRIu64"kB load, respectively)",
790 bond->name, delta / 1024, hash - bond->hash,
791 from->name, to->name,
792 (from->tx_bytes - delta) / 1024,
793 (to->tx_bytes + delta) / 1024);
795 /* Shift load away from 'from' to 'to'. */
796 from->tx_bytes -= delta;
797 to->tx_bytes += delta;
799 /* Arrange for flows to be revalidated. */
800 tag_set_add(set, hash->tag);
802 hash->tag = tag_create_random();
805 /* Pick and returns a bond_entry to migrate to 'to' (the least-loaded slave),
806 * given that doing so must decrease the ratio of the load on the two slaves by
807 * at least 0.1. Returns NULL if there is no appropriate entry.
809 * The list of entries isn't sorted. I don't know of a reason to prefer to
810 * shift away small hashes or large hashes. */
811 static struct bond_entry *
812 choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
814 struct bond_entry *e;
816 if (list_is_short(&from->entries)) {
817 /* 'from' carries no more than one MAC hash, so shifting load away from
818 * it would be pointless. */
822 LIST_FOR_EACH (e, list_node, &from->entries) {
823 double old_ratio, new_ratio;
826 if (to_tx_bytes == 0) {
827 /* Nothing on the new slave, move it. */
832 old_ratio = (double)from->tx_bytes / to_tx_bytes;
833 new_ratio = (double)(from->tx_bytes - delta) / (to_tx_bytes + delta);
834 if (old_ratio - new_ratio > 0.1) {
835 /* Would decrease the ratio, move it. */
843 /* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
846 insert_bal(struct list *bals, struct bond_slave *slave)
848 struct bond_slave *pos;
850 LIST_FOR_EACH (pos, bal_node, bals) {
851 if (slave->tx_bytes > pos->tx_bytes) {
855 list_insert(&pos->bal_node, &slave->bal_node);
858 /* Removes 'slave' from its current list and then inserts it into 'bals' so
859 * that descending order of 'tx_bytes' is maintained. */
861 reinsert_bal(struct list *bals, struct bond_slave *slave)
863 list_remove(&slave->bal_node);
864 insert_bal(bals, slave);
867 /* If 'bond' needs rebalancing, does so.
869 * The caller should have called bond_account() for each active flow, to ensure
870 * that flow data is consistently accounted at this point. */
872 bond_rebalance(struct bond *bond, struct tag_set *tags)
874 struct bond_slave *slave;
875 struct bond_entry *e;
878 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
881 bond->next_rebalance = time_msec() + bond->rebalance_interval;
883 /* Add each bond_entry to its slave's 'entries' list.
884 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
885 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
887 list_init(&slave->entries);
889 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
890 if (e->slave && e->tx_bytes) {
891 e->slave->tx_bytes += e->tx_bytes;
892 list_push_back(&e->slave->entries, &e->list_node);
896 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
898 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
899 * with a proper list sort algorithm. */
901 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
902 if (slave->enabled) {
903 insert_bal(&bals, slave);
906 log_bals(bond, &bals);
908 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
909 while (!list_is_short(&bals)) {
910 struct bond_slave *from = bond_slave_from_bal_node(list_front(&bals));
911 struct bond_slave *to = bond_slave_from_bal_node(list_back(&bals));
914 overload = from->tx_bytes - to->tx_bytes;
915 if (overload < to->tx_bytes >> 5 || overload < 100000) {
916 /* The extra load on 'from' (and all less-loaded slaves), compared
917 * to that of 'to' (the least-loaded slave), is less than ~3%, or
918 * it is less than ~1Mbps. No point in rebalancing. */
922 /* 'from' is carrying significantly more load than 'to', and that load
923 * is split across at least two different hashes. */
924 e = choose_entry_to_migrate(from, to->tx_bytes);
926 bond_shift_load(e, to, tags);
928 /* Delete element from from->entries.
930 * We don't add the element to to->hashes. That would only allow
931 * 'e' to be migrated to another slave in this rebalancing run, and
932 * there is no point in doing that. */
933 list_remove(&e->list_node);
935 /* Re-sort 'bals'. */
936 reinsert_bal(&bals, from);
937 reinsert_bal(&bals, to);
939 /* Can't usefully migrate anything away from 'from'.
940 * Don't reconsider it. */
941 list_remove(&from->bal_node);
945 /* Implement exponentially weighted moving average. A weight of 1/2 causes
946 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
947 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
948 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
956 /* Bonding unixctl user interface functions. */
959 bond_find(const char *name)
963 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
965 if (!strcmp(bond->name, name)) {
972 static struct bond_slave *
973 bond_lookup_slave(struct bond *bond, const char *slave_name)
975 struct bond_slave *slave;
977 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
978 if (!strcmp(slave->name, slave_name)) {
986 bond_unixctl_list(struct unixctl_conn *conn,
987 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
989 struct ds ds = DS_EMPTY_INITIALIZER;
990 const struct bond *bond;
992 ds_put_cstr(&ds, "bond\ttype\tslaves\n");
994 HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
995 const struct bond_slave *slave;
998 ds_put_format(&ds, "%s\t%s\t",
999 bond->name, bond_mode_to_string(bond->balance));
1002 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1004 ds_put_cstr(&ds, ", ");
1006 ds_put_cstr(&ds, slave->name);
1008 ds_put_char(&ds, '\n');
1010 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1015 bond_unixctl_show(struct unixctl_conn *conn,
1016 const char *args, void *aux OVS_UNUSED)
1018 struct ds ds = DS_EMPTY_INITIALIZER;
1019 const struct bond_slave *slave;
1020 const struct bond *bond;
1022 bond = bond_find(args);
1024 unixctl_command_reply(conn, 501, "no such bond");
1028 ds_put_format(&ds, "bond_mode: %s\n",
1029 bond_mode_to_string(bond->balance));
1032 ds_put_format(&ds, "lacp: %s\n",
1033 lacp_is_active(bond->lacp) ? "active" : "passive");
1035 ds_put_cstr(&ds, "lacp: off\n");
1038 if (bond->balance != BM_AB) {
1039 ds_put_format(&ds, "bond-hash-algorithm: %s\n",
1040 bond_is_tcp_hash(bond) ? "balance-tcp" : "balance-slb");
1043 ds_put_format(&ds, "bond-detect-mode: %s\n",
1044 bond->monitor ? "carrier" : "miimon");
1046 if (!bond->monitor) {
1047 ds_put_format(&ds, "bond-miimon-interval: %lld\n",
1048 bond->miimon_interval);
1051 ds_put_format(&ds, "updelay: %d ms\n", bond->updelay);
1052 ds_put_format(&ds, "downdelay: %d ms\n", bond->downdelay);
1054 if (bond_is_balanced(bond)) {
1055 ds_put_format(&ds, "next rebalance: %lld ms\n",
1056 bond->next_rebalance - time_msec());
1059 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1060 struct bond_entry *be;
1064 ds_put_format(&ds, "\nslave %s: %s\n",
1065 slave->name, slave->enabled ? "enabled" : "disabled");
1066 if (slave == bond->active_slave) {
1067 ds_put_cstr(&ds, "\tactive slave\n");
1069 if (slave->delay_expires != LLONG_MAX) {
1070 ds_put_format(&ds, "\t%s expires in %lld ms\n",
1071 slave->enabled ? "downdelay" : "updelay",
1072 slave->delay_expires - time_msec());
1075 if (!bond_is_balanced(bond)) {
1080 memset(&flow, 0, sizeof flow);
1081 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1082 int hash = be - bond->hash;
1084 if (be->slave != slave) {
1088 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
1089 hash, be->tx_bytes / 1024);
1091 if (bond->balance != BM_SLB) {
1095 /* XXX How can we list the MACs assigned to hashes? */
1098 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1103 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_,
1104 void *aux OVS_UNUSED)
1106 char *args = (char *) args_;
1107 char *save_ptr = NULL;
1108 char *bond_s, *hash_s, *slave_s;
1110 struct bond_slave *slave;
1111 struct bond_entry *entry;
1114 bond_s = strtok_r(args, " ", &save_ptr);
1115 hash_s = strtok_r(NULL, " ", &save_ptr);
1116 slave_s = strtok_r(NULL, " ", &save_ptr);
1118 unixctl_command_reply(conn, 501,
1119 "usage: bond/migrate BOND HASH SLAVE");
1123 bond = bond_find(bond_s);
1125 unixctl_command_reply(conn, 501, "no such bond");
1129 if (bond->balance != BM_SLB) {
1130 unixctl_command_reply(conn, 501, "not an SLB bond");
1134 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1135 hash = atoi(hash_s) & BOND_MASK;
1137 unixctl_command_reply(conn, 501, "bad hash");
1141 slave = bond_lookup_slave(bond, slave_s);
1143 unixctl_command_reply(conn, 501, "no such slave");
1147 if (!slave->enabled) {
1148 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
1152 entry = &bond->hash[hash];
1153 tag_set_add(&bond->unixctl_tags, entry->tag);
1154 entry->slave = slave;
1155 entry->tag = tag_create_random();
1156 unixctl_command_reply(conn, 200, "migrated");
1160 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_,
1161 void *aux OVS_UNUSED)
1163 char *args = (char *) args_;
1164 char *save_ptr = NULL;
1165 char *bond_s, *slave_s;
1167 struct bond_slave *slave;
1169 bond_s = strtok_r(args, " ", &save_ptr);
1170 slave_s = strtok_r(NULL, " ", &save_ptr);
1172 unixctl_command_reply(conn, 501,
1173 "usage: bond/set-active-slave BOND SLAVE");
1177 bond = bond_find(bond_s);
1179 unixctl_command_reply(conn, 501, "no such bond");
1183 slave = bond_lookup_slave(bond, slave_s);
1185 unixctl_command_reply(conn, 501, "no such slave");
1189 if (!slave->enabled) {
1190 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
1194 if (bond->active_slave != slave) {
1195 tag_set_add(&bond->unixctl_tags, bond_get_active_slave_tag(bond));
1196 bond->active_slave = slave;
1197 bond->active_slave->tag = tag_create_random();
1198 VLOG_INFO("bond %s: active interface is now %s",
1199 bond->name, slave->name);
1200 bond->send_learning_packets = true;
1201 unixctl_command_reply(conn, 200, "done");
1203 unixctl_command_reply(conn, 200, "no change");
1208 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
1210 char *args = (char *) args_;
1211 char *save_ptr = NULL;
1212 char *bond_s, *slave_s;
1214 struct bond_slave *slave;
1216 bond_s = strtok_r(args, " ", &save_ptr);
1217 slave_s = strtok_r(NULL, " ", &save_ptr);
1219 char *usage = xasprintf("usage: bond/%s-slave BOND SLAVE",
1220 enable ? "enable" : "disable");
1221 unixctl_command_reply(conn, 501, usage);
1226 bond = bond_find(bond_s);
1228 unixctl_command_reply(conn, 501, "no such bond");
1232 slave = bond_lookup_slave(bond, slave_s);
1234 unixctl_command_reply(conn, 501, "no such slave");
1238 bond_enable_slave(slave, enable, &bond->unixctl_tags);
1239 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
1243 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args,
1244 void *aux OVS_UNUSED)
1246 enable_slave(conn, args, true);
1250 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args,
1251 void *aux OVS_UNUSED)
1253 enable_slave(conn, args, false);
1257 bond_unixctl_hash(struct unixctl_conn *conn, const char *args_,
1258 void *aux OVS_UNUSED)
1260 char *args = (char *) args_;
1261 uint8_t mac[ETH_ADDR_LEN];
1265 char *mac_s, *vlan_s;
1266 char *save_ptr = NULL;
1268 mac_s = strtok_r(args, " ", &save_ptr);
1269 vlan_s = strtok_r(NULL, " ", &save_ptr);
1272 if (sscanf(vlan_s, "%u", &vlan) != 1) {
1273 unixctl_command_reply(conn, 501, "invalid vlan");
1277 vlan = OFP_VLAN_NONE;
1280 if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
1281 == ETH_ADDR_SCAN_COUNT) {
1282 hash = bond_hash_src(mac, vlan) & BOND_MASK;
1284 hash_cstr = xasprintf("%u", hash);
1285 unixctl_command_reply(conn, 200, hash_cstr);
1288 unixctl_command_reply(conn, 501, "invalid mac");
1297 unixctl_command_register("bond/list", bond_unixctl_list, NULL);
1298 unixctl_command_register("bond/show", bond_unixctl_show, NULL);
1299 unixctl_command_register("bond/migrate", bond_unixctl_migrate, NULL);
1300 unixctl_command_register("bond/set-active-slave",
1301 bond_unixctl_set_active_slave, NULL);
1302 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave,
1304 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave,
1306 unixctl_command_register("bond/hash", bond_unixctl_hash, NULL);
1310 bond_entry_reset(struct bond *bond)
1312 if (bond->balance != BM_AB) {
1313 size_t hash_len = (BOND_MASK + 1) * sizeof *bond->hash;
1316 bond->hash = xmalloc(hash_len);
1318 memset(bond->hash, 0, hash_len);
1320 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1327 static struct bond_slave *
1328 bond_slave_lookup(struct bond *bond, const void *slave_)
1330 struct bond_slave *slave;
1332 HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1334 if (slave->aux == slave_) {
1343 bond_is_link_up(struct bond *bond, struct netdev *netdev)
1345 return (bond->detect == BLSM_CARRIER
1346 ? netdev_get_carrier(netdev)
1347 : netdev_get_miimon(netdev));
1351 bond_stb_sort_cmp__(const void *a_, const void *b_)
1353 const struct bond_slave *const *ap = a_;
1354 const struct bond_slave *const *bp = b_;
1355 const struct bond_slave *a = *ap;
1356 const struct bond_slave *b = *bp;
1357 struct lacp *lacp = a->bond->lacp;
1361 a_id = lacp_slave_get_port_id(lacp, a);
1362 b_id = lacp_slave_get_port_id(lacp, b);
1364 a_id = netdev_get_ifindex(a->netdev);
1365 b_id = netdev_get_ifindex(b->netdev);
1368 return (a_id == b_id ? 0 : (a_id < b_id ? -1 : 1));
1372 bond_stb_sort(struct bond *bond)
1376 if (!bond->stb_slaves || !bond->stb_need_sort) {
1379 bond->stb_need_sort = false;
1381 qsort(bond->stb_slaves, bond->n_stb_slaves, sizeof *bond->stb_slaves,
1382 bond_stb_sort_cmp__);
1384 for (i = 0; i < bond->n_stb_slaves; i++) {
1385 bond->stb_slaves[i]->stb_idx = i;
1392 bond_stb_enable_slave(struct bond_slave *slave)
1394 struct bond *bond = slave->bond;
1396 if (!bond->stb_slaves) {
1400 bond->stb_need_sort = true;
1402 if (slave->enabled) {
1403 if (bond->len_stb_slaves <= bond->n_stb_slaves) {
1404 bond->stb_slaves = x2nrealloc(bond->stb_slaves,
1405 &bond->len_stb_slaves,
1406 sizeof *bond->stb_slaves);
1409 slave->stb_idx = bond->n_stb_slaves++;
1410 bond->stb_slaves[slave->stb_idx] = slave;
1412 size_t index = slave->stb_idx;
1413 bond->stb_slaves[index] = bond->stb_slaves[--bond->n_stb_slaves];
1414 bond->stb_slaves[index]->stb_idx = index;
1419 bond_enable_slave(struct bond_slave *slave, bool enable, struct tag_set *tags)
1421 slave->delay_expires = LLONG_MAX;
1422 if (enable != slave->enabled) {
1423 slave->enabled = enable;
1424 if (!slave->enabled) {
1425 VLOG_WARN("interface %s: disabled", slave->name);
1427 tag_set_add(tags, slave->tag);
1430 VLOG_WARN("interface %s: enabled", slave->name);
1431 slave->tag = tag_create_random();
1433 bond_stb_enable_slave(slave);
1438 bond_link_status_update(struct bond_slave *slave, struct tag_set *tags)
1440 struct bond *bond = slave->bond;
1443 up = slave->up && lacp_slave_may_enable(bond->lacp, slave);
1444 if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1445 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1446 VLOG_INFO_RL(&rl, "interface %s: link state %s",
1447 slave->name, up ? "up" : "down");
1448 if (up == slave->enabled) {
1449 slave->delay_expires = LLONG_MAX;
1450 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1451 slave->name, up ? "disabled" : "enabled");
1453 int delay = (lacp_negotiated(bond->lacp) ? 0
1454 : up ? bond->updelay : bond->downdelay);
1455 slave->delay_expires = time_msec() + delay;
1457 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1460 up ? "enabled" : "disabled",
1467 if (time_msec() >= slave->delay_expires) {
1468 bond_enable_slave(slave, up, tags);
1473 bond_is_tcp_hash(const struct bond *bond)
1475 return (bond->balance == BM_TCP || bond->balance == BM_STABLE)
1476 && lacp_negotiated(bond->lacp);
1480 bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
1482 return hash_bytes(mac, ETH_ADDR_LEN, vlan);
1486 bond_hash_tcp(const struct flow *flow, uint16_t vlan)
1488 struct flow hash_flow = *flow;
1489 hash_flow.vlan_tci = vlan;
1491 /* The symmetric quality of this hash function is not required, but
1492 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1493 * purposes, so we use it out of convenience. */
1494 return flow_hash_symmetric_l4(&hash_flow, 0);
1498 bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1500 assert(bond->balance != BM_AB);
1502 return (bond_is_tcp_hash(bond)
1503 ? bond_hash_tcp(flow, vlan)
1504 : bond_hash_src(flow->dl_src, vlan));
1507 static struct bond_entry *
1508 lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1511 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
1514 static struct bond_slave *
1515 choose_output_slave(const struct bond *bond, const struct flow *flow,
1518 struct bond_entry *e;
1520 switch (bond->balance) {
1522 return bond->active_slave;
1525 if (bond->n_stb_slaves) {
1526 return bond->stb_slaves[bond_hash(bond, flow, vlan)
1527 % bond->n_stb_slaves];
1529 return bond->active_slave;
1534 e = lookup_bond_entry(bond, flow, vlan);
1535 if (!e->slave || !e->slave->enabled) {
1536 e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
1537 struct bond_slave, hmap_node);
1538 if (!e->slave->enabled) {
1539 e->slave = bond->active_slave;
1541 e->tag = tag_create_random();
1550 static struct bond_slave *
1551 bond_choose_slave(const struct bond *bond)
1553 struct bond_slave *slave, *best;
1555 /* Find an enabled slave. */
1556 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1557 if (slave->enabled) {
1562 /* All interfaces are disabled. Find an interface that will be enabled
1563 * after its updelay expires. */
1565 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1566 if (slave->delay_expires != LLONG_MAX
1567 && lacp_slave_may_enable(bond->lacp, slave)
1568 && (!best || slave->delay_expires < best->delay_expires)) {
1576 bond_choose_active_slave(struct bond *bond, struct tag_set *tags)
1578 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1579 struct bond_slave *old_active_slave = bond->active_slave;
1581 bond->active_slave = bond_choose_slave(bond);
1582 if (bond->active_slave) {
1583 if (bond->active_slave->enabled) {
1584 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1585 bond->name, bond->active_slave->name);
1587 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1588 "remaining %lld ms updelay (since no interface was "
1589 "enabled)", bond->name, bond->active_slave->name,
1590 bond->active_slave->delay_expires - time_msec());
1591 bond_enable_slave(bond->active_slave, true, tags);
1594 if (!old_active_slave) {
1595 tag_set_add(tags, bond->no_slaves_tag);
1598 bond->send_learning_packets = true;
1599 } else if (old_active_slave) {
1600 VLOG_WARN_RL(&rl, "bond %s: all interfaces disabled", bond->name);
1604 /* Returns the tag for 'bond''s active slave, or 'bond''s no_slaves_tag if
1605 * there is no active slave. */
1607 bond_get_active_slave_tag(const struct bond *bond)
1609 return (bond->active_slave
1610 ? bond->active_slave->tag
1611 : bond->no_slaves_tag);
1614 /* Attempts to make the sum of the bond slaves' statistics appear on the fake
1615 * bond interface. */
1617 bond_update_fake_slave_stats(struct bond *bond)
1619 struct netdev_stats bond_stats;
1620 struct bond_slave *slave;
1621 struct netdev *bond_dev;
1623 memset(&bond_stats, 0, sizeof bond_stats);
1625 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1626 struct netdev_stats slave_stats;
1628 if (!netdev_get_stats(slave->netdev, &slave_stats)) {
1629 /* XXX: We swap the stats here because they are swapped back when
1630 * reported by the internal device. The reason for this is
1631 * internal devices normally represent packets going into the
1632 * system but when used as fake bond device they represent packets
1633 * leaving the system. We really should do this in the internal
1634 * device itself because changing it here reverses the counts from
1635 * the perspective of the switch. However, the internal device
1636 * doesn't know what type of device it represents so we have to do
1637 * it here for now. */
1638 bond_stats.tx_packets += slave_stats.rx_packets;
1639 bond_stats.tx_bytes += slave_stats.rx_bytes;
1640 bond_stats.rx_packets += slave_stats.tx_packets;
1641 bond_stats.rx_bytes += slave_stats.tx_bytes;
1645 if (!netdev_open_default(bond->name, &bond_dev)) {
1646 netdev_set_stats(bond_dev, &bond_stats);
1647 netdev_close(bond_dev);