Merge remote-tracking branch 'ovs-dev/master'
[sliver-openvswitch.git] / lib / bond.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 "coverage.h"
27 #include "dynamic-string.h"
28 #include "flow.h"
29 #include "hmap.h"
30 #include "lacp.h"
31 #include "list.h"
32 #include "netdev.h"
33 #include "odp-util.h"
34 #include "ofpbuf.h"
35 #include "packets.h"
36 #include "poll-loop.h"
37 #include "shash.h"
38 #include "tag.h"
39 #include "timeval.h"
40 #include "unixctl.h"
41 #include "vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(bond);
44
45 /* Bit-mask for hashing a flow down to a bucket.
46  * There are (BOND_MASK + 1) buckets. */
47 #define BOND_MASK 0xff
48
49 /* A hash bucket for mapping a flow to a slave.
50  * "struct bond" has an array of (BOND_MASK + 1) of these. */
51 struct bond_entry {
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<->facet association. */
55     struct list list_node;      /* In bond_slave's 'entries' list. */
56 };
57
58 /* A bond slave, that is, one of the links comprising a bond. */
59 struct bond_slave {
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. */
63
64     struct netdev *netdev;      /* Network device, owned by the client. */
65     unsigned int change_seq;    /* Tracks changes in 'netdev'. */
66     char *name;                 /* Name (a copy of netdev_get_name(netdev)). */
67
68     /* Link status. */
69     long long delay_expires;    /* Time after which 'enabled' may change. */
70     bool enabled;               /* May be chosen for flows? */
71     bool may_enable;            /* Client considers this slave bondable. */
72     tag_type tag;               /* Tag associated with this slave. */
73
74     /* Rebalancing info.  Used only by bond_rebalance(). */
75     struct list bal_node;       /* In bond_rebalance()'s 'bals' list. */
76     struct list entries;        /* 'struct bond_entry's assigned here. */
77     uint64_t tx_bytes;          /* Sum across 'tx_bytes' of entries. */
78 };
79
80 /* A bond, that is, a set of network devices grouped to improve performance or
81  * robustness.  */
82 struct bond {
83     struct hmap_node hmap_node; /* In 'all_bonds' hmap. */
84     char *name;                 /* Name provided by client. */
85
86     /* Slaves. */
87     struct hmap slaves;
88
89     /* Bonding info. */
90     enum bond_mode balance;     /* Balancing mode, one of BM_*. */
91     struct bond_slave *active_slave;
92     tag_type no_slaves_tag;     /* Tag for flows when all slaves disabled. */
93     int updelay, downdelay;     /* Delay before slave goes up/down, in ms. */
94     enum lacp_status lacp_status; /* Status of LACP negotiations. */
95     bool bond_revalidate;       /* True if flows need revalidation. */
96     uint32_t basis;             /* Basis for flow hash function. */
97
98     /* SLB specific bonding info. */
99     struct bond_entry *hash;     /* An array of (BOND_MASK + 1) elements. */
100     int rebalance_interval;      /* Interval between rebalances, in ms. */
101     long long int next_rebalance; /* Next rebalancing time. */
102     bool send_learning_packets;
103
104     /* Legacy compatibility. */
105     long long int next_fake_iface_update; /* LLONG_MAX if disabled. */
106
107     /* Tag set saved for next bond_run().  This tag set is a kluge for cases
108      * where we can't otherwise provide revalidation feedback to the client.
109      * That's only unixctl commands now; I hope no other cases will arise. */
110     struct tag_set unixctl_tags;
111
112     int ref_cnt;
113 };
114
115 static struct hmap all_bonds = HMAP_INITIALIZER(&all_bonds);
116
117 static void bond_entry_reset(struct bond *);
118 static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_);
119 static void bond_enable_slave(struct bond_slave *, bool enable,
120                               struct tag_set *);
121 static void bond_link_status_update(struct bond_slave *, struct tag_set *);
122 static void bond_choose_active_slave(struct bond *, struct tag_set *);
123 static unsigned int bond_hash_src(const uint8_t mac[ETH_ADDR_LEN],
124                                   uint16_t vlan, uint32_t basis);
125 static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan,
126                                   uint32_t basis);
127 static struct bond_entry *lookup_bond_entry(const struct bond *,
128                                             const struct flow *,
129                                             uint16_t vlan);
130 static tag_type bond_get_active_slave_tag(const struct bond *);
131 static struct bond_slave *choose_output_slave(const struct bond *,
132                                               const struct flow *,
133                                               struct flow_wildcards *,
134                                               uint16_t vlan, tag_type *tags);
135 static void bond_update_fake_slave_stats(struct bond *);
136
137 /* Attempts to parse 's' as the name of a bond balancing mode.  If successful,
138  * stores the mode in '*balance' and returns true.  Otherwise returns false
139  * without modifying '*balance'. */
140 bool
141 bond_mode_from_string(enum bond_mode *balance, const char *s)
142 {
143     if (!strcmp(s, bond_mode_to_string(BM_TCP))) {
144         *balance = BM_TCP;
145     } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) {
146         *balance = BM_SLB;
147     } else if (!strcmp(s, bond_mode_to_string(BM_AB))) {
148         *balance = BM_AB;
149     } else {
150         return false;
151     }
152     return true;
153 }
154
155 /* Returns a string representing 'balance'. */
156 const char *
157 bond_mode_to_string(enum bond_mode balance) {
158     switch (balance) {
159     case BM_TCP:
160         return "balance-tcp";
161     case BM_SLB:
162         return "balance-slb";
163     case BM_AB:
164         return "active-backup";
165     }
166     NOT_REACHED();
167 }
168
169 \f
170 /* Creates and returns a new bond whose configuration is initially taken from
171  * 's'.
172  *
173  * The caller should register each slave on the new bond by calling
174  * bond_slave_register().  */
175 struct bond *
176 bond_create(const struct bond_settings *s)
177 {
178     struct bond *bond;
179
180     bond = xzalloc(sizeof *bond);
181     hmap_init(&bond->slaves);
182     bond->no_slaves_tag = tag_create_random();
183     bond->next_fake_iface_update = LLONG_MAX;
184     bond->ref_cnt = 1;
185
186     bond_reconfigure(bond, s);
187
188     tag_set_init(&bond->unixctl_tags);
189
190     return bond;
191 }
192
193 struct bond *
194 bond_ref(const struct bond *bond_)
195 {
196     struct bond *bond = CONST_CAST(struct bond *, bond_);
197
198     if (bond) {
199         ovs_assert(bond->ref_cnt > 0);
200         bond->ref_cnt++;
201     }
202     return bond;
203 }
204
205 /* Frees 'bond'. */
206 void
207 bond_unref(struct bond *bond)
208 {
209     struct bond_slave *slave, *next_slave;
210
211     if (!bond) {
212         return;
213     }
214
215     ovs_assert(bond->ref_cnt > 0);
216     if (--bond->ref_cnt) {
217         return;
218     }
219
220     hmap_remove(&all_bonds, &bond->hmap_node);
221
222     HMAP_FOR_EACH_SAFE (slave, next_slave, hmap_node, &bond->slaves) {
223         hmap_remove(&bond->slaves, &slave->hmap_node);
224         /* Client owns 'slave->netdev'. */
225         free(slave->name);
226         free(slave);
227     }
228     hmap_destroy(&bond->slaves);
229
230     free(bond->hash);
231     free(bond->name);
232     free(bond);
233 }
234
235 /* Updates 'bond''s overall configuration to 's'.
236  *
237  * The caller should register each slave on 'bond' by calling
238  * bond_slave_register().  This is optional if none of the slaves'
239  * configuration has changed.  In any case it can't hurt.
240  *
241  * Returns true if the configuration has changed in such a way that requires
242  * flow revalidation.
243  * */
244 bool
245 bond_reconfigure(struct bond *bond, const struct bond_settings *s)
246 {
247     bool revalidate = false;
248
249     if (!bond->name || strcmp(bond->name, s->name)) {
250         if (bond->name) {
251             hmap_remove(&all_bonds, &bond->hmap_node);
252             free(bond->name);
253         }
254         bond->name = xstrdup(s->name);
255         hmap_insert(&all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
256     }
257
258     bond->updelay = s->up_delay;
259     bond->downdelay = s->down_delay;
260
261     if (bond->rebalance_interval != s->rebalance_interval) {
262         bond->rebalance_interval = s->rebalance_interval;
263         revalidate = true;
264     }
265
266     if (bond->balance != s->balance) {
267         bond->balance = s->balance;
268         revalidate = true;
269     }
270
271     if (bond->basis != s->basis) {
272         bond->basis = s->basis;
273         revalidate = true;
274     }
275
276     if (s->fake_iface) {
277         if (bond->next_fake_iface_update == LLONG_MAX) {
278             bond->next_fake_iface_update = time_msec();
279         }
280     } else {
281         bond->next_fake_iface_update = LLONG_MAX;
282     }
283
284     if (bond->bond_revalidate) {
285         revalidate = true;
286         bond->bond_revalidate = false;
287     }
288
289     if (bond->balance == BM_AB || !bond->hash || revalidate) {
290         bond_entry_reset(bond);
291     }
292
293     return revalidate;
294 }
295
296 static void
297 bond_slave_set_netdev__(struct bond_slave *slave, struct netdev *netdev)
298 {
299     if (slave->netdev != netdev) {
300         slave->netdev = netdev;
301         slave->change_seq = 0;
302     }
303 }
304
305 /* Registers 'slave_' as a slave of 'bond'.  The 'slave_' pointer is an
306  * arbitrary client-provided pointer that uniquely identifies a slave within a
307  * bond.  If 'slave_' already exists within 'bond' then this function
308  * reconfigures the existing slave.
309  *
310  * 'netdev' must be the network device that 'slave_' represents.  It is owned
311  * by the client, so the client must not close it before either unregistering
312  * 'slave_' or destroying 'bond'.
313  */
314 void
315 bond_slave_register(struct bond *bond, void *slave_, struct netdev *netdev)
316 {
317     struct bond_slave *slave = bond_slave_lookup(bond, slave_);
318
319     if (!slave) {
320         slave = xzalloc(sizeof *slave);
321
322         hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0));
323         slave->bond = bond;
324         slave->aux = slave_;
325         slave->delay_expires = LLONG_MAX;
326         slave->name = xstrdup(netdev_get_name(netdev));
327         bond->bond_revalidate = true;
328
329         slave->enabled = false;
330         bond_enable_slave(slave, netdev_get_carrier(netdev), NULL);
331     }
332
333     bond_slave_set_netdev__(slave, netdev);
334
335     free(slave->name);
336     slave->name = xstrdup(netdev_get_name(netdev));
337 }
338
339 /* Updates the network device to be used with 'slave_' to 'netdev'.
340  *
341  * This is useful if the caller closes and re-opens the network device
342  * registered with bond_slave_register() but doesn't need to change anything
343  * else. */
344 void
345 bond_slave_set_netdev(struct bond *bond, void *slave_, struct netdev *netdev)
346 {
347     struct bond_slave *slave = bond_slave_lookup(bond, slave_);
348     if (slave) {
349         bond_slave_set_netdev__(slave, netdev);
350     }
351 }
352
353 /* Unregisters 'slave_' from 'bond'.  If 'bond' does not contain such a slave
354  * then this function has no effect.
355  *
356  * Unregistering a slave invalidates all flows. */
357 void
358 bond_slave_unregister(struct bond *bond, const void *slave_)
359 {
360     struct bond_slave *slave = bond_slave_lookup(bond, slave_);
361     bool del_active;
362
363     if (!slave) {
364         return;
365     }
366
367     bond_enable_slave(slave, false, NULL);
368
369     del_active = bond->active_slave == slave;
370     if (bond->hash) {
371         struct bond_entry *e;
372         for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
373             if (e->slave == slave) {
374                 e->slave = NULL;
375             }
376         }
377     }
378
379     free(slave->name);
380
381     hmap_remove(&bond->slaves, &slave->hmap_node);
382     /* Client owns 'slave->netdev'. */
383     free(slave);
384
385     if (del_active) {
386         struct tag_set tags;
387
388         tag_set_init(&tags);
389         bond_choose_active_slave(bond, &tags);
390         bond->send_learning_packets = true;
391     }
392 }
393
394 /* Should be called on each slave in 'bond' before bond_run() to indicate
395  * whether or not 'slave_' may be enabled. This function is intended to allow
396  * other protocols to have some impact on bonding decisions.  For example LACP
397  * or high level link monitoring protocols may decide that a given slave should
398  * not be able to send traffic. */
399 void
400 bond_slave_set_may_enable(struct bond *bond, void *slave_, bool may_enable)
401 {
402     bond_slave_lookup(bond, slave_)->may_enable = may_enable;
403 }
404
405 /* Performs periodic maintenance on 'bond'.  The caller must provide 'tags' to
406  * allow tagged flows to be invalidated.
407  *
408  * The caller should check bond_should_send_learning_packets() afterward. */
409 void
410 bond_run(struct bond *bond, struct tag_set *tags, enum lacp_status lacp_status)
411 {
412     struct bond_slave *slave;
413
414     if (bond->lacp_status != lacp_status) {
415         bond->lacp_status = lacp_status;
416         bond->bond_revalidate = true;
417     }
418
419     /* Enable slaves based on link status and LACP feedback. */
420     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
421         bond_link_status_update(slave, tags);
422         slave->change_seq = netdev_change_seq(slave->netdev);
423     }
424     if (!bond->active_slave || !bond->active_slave->enabled) {
425         bond_choose_active_slave(bond, tags);
426     }
427
428     /* Update fake bond interface stats. */
429     if (time_msec() >= bond->next_fake_iface_update) {
430         bond_update_fake_slave_stats(bond);
431         bond->next_fake_iface_update = time_msec() + 1000;
432     }
433
434     if (bond->bond_revalidate) {
435         struct bond_slave *slave;
436
437         bond->bond_revalidate = false;
438         bond_entry_reset(bond);
439         HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
440             tag_set_add(tags, slave->tag);
441         }
442         tag_set_add(tags, bond->no_slaves_tag);
443     }
444
445     /* Invalidate any tags required by  */
446     tag_set_union(tags, &bond->unixctl_tags);
447     tag_set_init(&bond->unixctl_tags);
448 }
449
450 /* Causes poll_block() to wake up when 'bond' needs something to be done. */
451 void
452 bond_wait(struct bond *bond)
453 {
454     struct bond_slave *slave;
455
456     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
457         if (slave->delay_expires != LLONG_MAX) {
458             poll_timer_wait_until(slave->delay_expires);
459         }
460
461         if (slave->change_seq != netdev_change_seq(slave->netdev)) {
462             poll_immediate_wake();
463         }
464     }
465
466     if (bond->next_fake_iface_update != LLONG_MAX) {
467         poll_timer_wait_until(bond->next_fake_iface_update);
468     }
469
470     /* Ensure that any saved tags get revalidated right away. */
471     if (!tag_set_is_empty(&bond->unixctl_tags)) {
472         poll_immediate_wake();
473     }
474
475     /* We don't wait for bond->next_rebalance because rebalancing can only run
476      * at a flow account checkpoint.  ofproto does checkpointing on its own
477      * schedule and bond_rebalance() gets called afterward, so we'd just be
478      * waking up for no purpose. */
479 }
480 \f
481 /* MAC learning table interaction. */
482
483 static bool
484 may_send_learning_packets(const struct bond *bond)
485 {
486     return bond->lacp_status == LACP_DISABLED
487         && (bond->balance == BM_SLB || bond->balance == BM_AB)
488         && bond->active_slave;
489 }
490
491 /* Returns true if 'bond' needs the client to send out packets to assist with
492  * MAC learning on 'bond'.  If this function returns true, then the client
493  * should iterate through its MAC learning table for the bridge on which 'bond'
494  * is located.  For each MAC that has been learned on a port other than 'bond',
495  * it should call bond_compose_learning_packet().
496  *
497  * This function will only return true if 'bond' is in SLB or active-backup
498  * mode and LACP is not negotiated.  Otherwise sending learning packets isn't
499  * necessary.
500  *
501  * Calling this function resets the state that it checks. */
502 bool
503 bond_should_send_learning_packets(struct bond *bond)
504 {
505     bool send = bond->send_learning_packets && may_send_learning_packets(bond);
506     bond->send_learning_packets = false;
507     return send;
508 }
509
510 /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
511  *
512  * See bond_should_send_learning_packets() for description of usage. The
513  * caller should send the composed packet on the port associated with
514  * port_aux and takes ownership of the returned ofpbuf. */
515 struct ofpbuf *
516 bond_compose_learning_packet(struct bond *bond,
517                              const uint8_t eth_src[ETH_ADDR_LEN],
518                              uint16_t vlan, void **port_aux)
519 {
520     struct bond_slave *slave;
521     struct ofpbuf *packet;
522     tag_type tags = 0;
523     struct flow flow;
524
525     ovs_assert(may_send_learning_packets(bond));
526
527     memset(&flow, 0, sizeof flow);
528     memcpy(flow.dl_src, eth_src, ETH_ADDR_LEN);
529     slave = choose_output_slave(bond, &flow, NULL, vlan, &tags);
530
531     packet = ofpbuf_new(0);
532     compose_rarp(packet, eth_src);
533     if (vlan) {
534         eth_push_vlan(packet, htons(vlan));
535     }
536
537     *port_aux = slave->aux;
538     return packet;
539 }
540 \f
541 /* Checks whether a packet that arrived on 'slave_' within 'bond', with an
542  * Ethernet destination address of 'eth_dst', should be admitted.
543  *
544  * The return value is one of the following:
545  *
546  *    - BV_ACCEPT: Admit the packet.
547  *
548  *    - BV_DROP: Drop the packet.
549  *
550  *    - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
551  *      Ethernet source address and VLAN.  If there is none, or if the packet
552  *      is on the learned port, then admit the packet.  If a different port has
553  *      been learned, however, drop the packet (and do not use it for MAC
554  *      learning).
555  */
556 enum bond_verdict
557 bond_check_admissibility(struct bond *bond, const void *slave_,
558                          const uint8_t eth_dst[ETH_ADDR_LEN], tag_type *tags)
559 {
560     struct bond_slave *slave = bond_slave_lookup(bond, slave_);
561
562     if (!slave) {
563         return BV_DROP;
564     }
565
566     /* LACP bonds have very loose admissibility restrictions because we can
567      * assume the remote switch is aware of the bond and will "do the right
568      * thing".  However, as a precaution we drop packets on disabled slaves
569      * because no correctly implemented partner switch should be sending
570      * packets to them.
571      *
572      * If LACP is configured, but LACP negotiations have been unsuccessful, we
573      * drop all incoming traffic. */
574     switch (bond->lacp_status) {
575     case LACP_NEGOTIATED: return slave->enabled ? BV_ACCEPT : BV_DROP;
576     case LACP_CONFIGURED: return BV_DROP;
577     case LACP_DISABLED: break;
578     }
579
580     /* Drop all multicast packets on inactive slaves. */
581     if (eth_addr_is_multicast(eth_dst)) {
582         *tags |= bond_get_active_slave_tag(bond);
583         if (bond->active_slave != slave) {
584             return BV_DROP;
585         }
586     }
587
588     switch (bond->balance) {
589     case BM_AB:
590         /* Drop all packets which arrive on backup slaves.  This is similar to
591          * how Linux bonding handles active-backup bonds. */
592         *tags |= bond_get_active_slave_tag(bond);
593         if (bond->active_slave != slave) {
594             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
595
596             VLOG_DBG_RL(&rl, "active-backup bond received packet on backup"
597                         " slave (%s) destined for " ETH_ADDR_FMT,
598                         slave->name, ETH_ADDR_ARGS(eth_dst));
599             return BV_DROP;
600         }
601         return BV_ACCEPT;
602
603     case BM_TCP:
604         /* TCP balanced bonds require successful LACP negotiated. Based on the
605          * above check, LACP is off on this bond.  Therfore, we drop all
606          * incoming traffic. */
607         return BV_DROP;
608
609     case BM_SLB:
610         /* Drop all packets for which we have learned a different input port,
611          * because we probably sent the packet on one slave and got it back on
612          * the other.  Gratuitous ARP packets are an exception to this rule:
613          * the host has moved to another switch.  The exception to the
614          * exception is if we locked the learning table to avoid reflections on
615          * bond slaves. */
616         return BV_DROP_IF_MOVED;
617     }
618
619     NOT_REACHED();
620 }
621
622 /* Returns the slave (registered on 'bond' by bond_slave_register()) to which
623  * a packet with the given 'flow' and 'vlan' should be forwarded.  Returns
624  * NULL if the packet should be dropped because no slaves are enabled.
625  *
626  * 'vlan' is not necessarily the same as 'flow->vlan_tci'.  First, 'vlan'
627  * should be a VID only (i.e. excluding the PCP bits).  Second,
628  * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
629  * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
630  * packet belongs to (so for an access port it will be the access port's VLAN).
631  *
632  * Adds a tag to '*tags' that associates the flow with the returned slave.
633  *
634  * If 'wc' is non-NULL, bitwise-OR's 'wc' with the set of bits that were
635  * significant in the selection.  At some point earlier, 'wc' should
636  * have been initialized (e.g., by flow_wildcards_init_catchall()).
637  */
638 void *
639 bond_choose_output_slave(struct bond *bond, const struct flow *flow,
640                          struct flow_wildcards *wc, uint16_t vlan,
641                          tag_type *tags)
642 {
643     struct bond_slave *slave = choose_output_slave(bond, flow, wc, vlan, tags);
644     if (slave) {
645         *tags |= slave->tag;
646         return slave->aux;
647     } else {
648         *tags |= bond->no_slaves_tag;
649         return NULL;
650     }
651 }
652 \f
653 /* Rebalancing. */
654
655 static bool
656 bond_is_balanced(const struct bond *bond)
657 {
658     return bond->rebalance_interval
659         && (bond->balance == BM_SLB || bond->balance == BM_TCP);
660 }
661
662 /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
663 void
664 bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
665              uint64_t n_bytes)
666 {
667     if (bond_is_balanced(bond)) {
668         lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
669     }
670 }
671
672 static struct bond_slave *
673 bond_slave_from_bal_node(struct list *bal)
674 {
675     return CONTAINER_OF(bal, struct bond_slave, bal_node);
676 }
677
678 static void
679 log_bals(struct bond *bond, const struct list *bals)
680 {
681     if (VLOG_IS_DBG_ENABLED()) {
682         struct ds ds = DS_EMPTY_INITIALIZER;
683         const struct bond_slave *slave;
684
685         LIST_FOR_EACH (slave, bal_node, bals) {
686             if (ds.length) {
687                 ds_put_char(&ds, ',');
688             }
689             ds_put_format(&ds, " %s %"PRIu64"kB",
690                           slave->name, slave->tx_bytes / 1024);
691
692             if (!slave->enabled) {
693                 ds_put_cstr(&ds, " (disabled)");
694             }
695             if (!list_is_empty(&slave->entries)) {
696                 struct bond_entry *e;
697
698                 ds_put_cstr(&ds, " (");
699                 LIST_FOR_EACH (e, list_node, &slave->entries) {
700                     if (&e->list_node != list_front(&slave->entries)) {
701                         ds_put_cstr(&ds, " + ");
702                     }
703                     ds_put_format(&ds, "h%td: %"PRIu64"kB",
704                                   e - bond->hash, e->tx_bytes / 1024);
705                 }
706                 ds_put_cstr(&ds, ")");
707             }
708         }
709         VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
710         ds_destroy(&ds);
711     }
712 }
713
714 /* Shifts 'hash' from its current slave to 'to'. */
715 static void
716 bond_shift_load(struct bond_entry *hash, struct bond_slave *to,
717                 struct tag_set *set)
718 {
719     struct bond_slave *from = hash->slave;
720     struct bond *bond = from->bond;
721     uint64_t delta = hash->tx_bytes;
722
723     VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
724               "from %s to %s (now carrying %"PRIu64"kB and "
725               "%"PRIu64"kB load, respectively)",
726               bond->name, delta / 1024, hash - bond->hash,
727               from->name, to->name,
728               (from->tx_bytes - delta) / 1024,
729               (to->tx_bytes + delta) / 1024);
730
731     /* Shift load away from 'from' to 'to'. */
732     from->tx_bytes -= delta;
733     to->tx_bytes += delta;
734
735     /* Arrange for flows to be revalidated. */
736     tag_set_add(set, hash->tag);
737     hash->slave = to;
738     hash->tag = tag_create_random();
739 }
740
741 /* Picks and returns a bond_entry to migrate from 'from' (the most heavily
742  * loaded bond slave) to a bond slave that has 'to_tx_bytes' bytes of load,
743  * given that doing so must decrease the ratio of the load on the two slaves by
744  * at least 0.1.  Returns NULL if there is no appropriate entry.
745  *
746  * The list of entries isn't sorted.  I don't know of a reason to prefer to
747  * shift away small hashes or large hashes. */
748 static struct bond_entry *
749 choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
750 {
751     struct bond_entry *e;
752
753     if (list_is_short(&from->entries)) {
754         /* 'from' carries no more than one MAC hash, so shifting load away from
755          * it would be pointless. */
756         return NULL;
757     }
758
759     LIST_FOR_EACH (e, list_node, &from->entries) {
760         double old_ratio, new_ratio;
761         uint64_t delta;
762
763         if (to_tx_bytes == 0) {
764             /* Nothing on the new slave, move it. */
765             return e;
766         }
767
768         delta = e->tx_bytes;
769         old_ratio = (double)from->tx_bytes / to_tx_bytes;
770         new_ratio = (double)(from->tx_bytes - delta) / (to_tx_bytes + delta);
771         if (old_ratio - new_ratio > 0.1
772             && fabs(new_ratio - 1.0) < fabs(old_ratio - 1.0)) {
773             /* We're aiming for an ideal ratio of 1, meaning both the 'from'
774                and 'to' slave have the same load.  Therefore, we only move an
775                entry if it decreases the load on 'from', and brings us closer
776                to equal traffic load. */
777             return e;
778         }
779     }
780
781     return NULL;
782 }
783
784 /* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
785  * maintained. */
786 static void
787 insert_bal(struct list *bals, struct bond_slave *slave)
788 {
789     struct bond_slave *pos;
790
791     LIST_FOR_EACH (pos, bal_node, bals) {
792         if (slave->tx_bytes > pos->tx_bytes) {
793             break;
794         }
795     }
796     list_insert(&pos->bal_node, &slave->bal_node);
797 }
798
799 /* Removes 'slave' from its current list and then inserts it into 'bals' so
800  * that descending order of 'tx_bytes' is maintained. */
801 static void
802 reinsert_bal(struct list *bals, struct bond_slave *slave)
803 {
804     list_remove(&slave->bal_node);
805     insert_bal(bals, slave);
806 }
807
808 /* If 'bond' needs rebalancing, does so.
809  *
810  * The caller should have called bond_account() for each active flow, to ensure
811  * that flow data is consistently accounted at this point. */
812 void
813 bond_rebalance(struct bond *bond, struct tag_set *tags)
814 {
815     struct bond_slave *slave;
816     struct bond_entry *e;
817     struct list bals;
818
819     if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
820         return;
821     }
822     bond->next_rebalance = time_msec() + bond->rebalance_interval;
823
824     /* Add each bond_entry to its slave's 'entries' list.
825      * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
826     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
827         slave->tx_bytes = 0;
828         list_init(&slave->entries);
829     }
830     for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
831         if (e->slave && e->tx_bytes) {
832             e->slave->tx_bytes += e->tx_bytes;
833             list_push_back(&e->slave->entries, &e->list_node);
834         }
835     }
836
837     /* Add enabled slaves to 'bals' in descending order of tx_bytes.
838      *
839      * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
840      * with a proper list sort algorithm. */
841     list_init(&bals);
842     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
843         if (slave->enabled) {
844             insert_bal(&bals, slave);
845         }
846     }
847     log_bals(bond, &bals);
848
849     /* Shift load from the most-loaded slaves to the least-loaded slaves. */
850     while (!list_is_short(&bals)) {
851         struct bond_slave *from = bond_slave_from_bal_node(list_front(&bals));
852         struct bond_slave *to = bond_slave_from_bal_node(list_back(&bals));
853         uint64_t overload;
854
855         overload = from->tx_bytes - to->tx_bytes;
856         if (overload < to->tx_bytes >> 5 || overload < 100000) {
857             /* The extra load on 'from' (and all less-loaded slaves), compared
858              * to that of 'to' (the least-loaded slave), is less than ~3%, or
859              * it is less than ~1Mbps.  No point in rebalancing. */
860             break;
861         }
862
863         /* 'from' is carrying significantly more load than 'to'.  Pick a hash
864          * to move from 'from' to 'to'. */
865         e = choose_entry_to_migrate(from, to->tx_bytes);
866         if (e) {
867             bond_shift_load(e, to, tags);
868
869             /* Delete element from from->entries.
870              *
871              * We don't add the element to to->hashes.  That would only allow
872              * 'e' to be migrated to another slave in this rebalancing run, and
873              * there is no point in doing that. */
874             list_remove(&e->list_node);
875
876             /* Re-sort 'bals'. */
877             reinsert_bal(&bals, from);
878             reinsert_bal(&bals, to);
879         } else {
880             /* Can't usefully migrate anything away from 'from'.
881              * Don't reconsider it. */
882             list_remove(&from->bal_node);
883         }
884     }
885
886     /* Implement exponentially weighted moving average.  A weight of 1/2 causes
887      * historical data to decay to <1% in 7 rebalancing runs.  1,000,000 bytes
888      * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
889     for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
890         e->tx_bytes /= 2;
891         if (!e->tx_bytes) {
892             e->slave = NULL;
893         }
894     }
895 }
896 \f
897 /* Bonding unixctl user interface functions. */
898
899 static struct bond *
900 bond_find(const char *name)
901 {
902     struct bond *bond;
903
904     HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
905                              &all_bonds) {
906         if (!strcmp(bond->name, name)) {
907             return bond;
908         }
909     }
910     return NULL;
911 }
912
913 static struct bond_slave *
914 bond_lookup_slave(struct bond *bond, const char *slave_name)
915 {
916     struct bond_slave *slave;
917
918     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
919         if (!strcmp(slave->name, slave_name)) {
920             return slave;
921         }
922     }
923     return NULL;
924 }
925
926 static void
927 bond_unixctl_list(struct unixctl_conn *conn,
928                   int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
929                   void *aux OVS_UNUSED)
930 {
931     struct ds ds = DS_EMPTY_INITIALIZER;
932     const struct bond *bond;
933
934     ds_put_cstr(&ds, "bond\ttype\tslaves\n");
935
936     HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
937         const struct bond_slave *slave;
938         size_t i;
939
940         ds_put_format(&ds, "%s\t%s\t",
941                       bond->name, bond_mode_to_string(bond->balance));
942
943         i = 0;
944         HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
945             if (i++ > 0) {
946                 ds_put_cstr(&ds, ", ");
947             }
948             ds_put_cstr(&ds, slave->name);
949         }
950         ds_put_char(&ds, '\n');
951     }
952     unixctl_command_reply(conn, ds_cstr(&ds));
953     ds_destroy(&ds);
954 }
955
956 static void
957 bond_print_details(struct ds *ds, const struct bond *bond)
958 {
959     struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
960     const struct shash_node **sorted_slaves = NULL;
961     const struct bond_slave *slave;
962     int i;
963
964     ds_put_format(ds, "---- %s ----\n", bond->name);
965     ds_put_format(ds, "bond_mode: %s\n",
966                   bond_mode_to_string(bond->balance));
967
968     ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis);
969
970     ds_put_format(ds, "updelay: %d ms\n", bond->updelay);
971     ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay);
972
973     if (bond_is_balanced(bond)) {
974         ds_put_format(ds, "next rebalance: %lld ms\n",
975                       bond->next_rebalance - time_msec());
976     }
977
978     ds_put_cstr(ds, "lacp_status: ");
979     switch (bond->lacp_status) {
980     case LACP_NEGOTIATED:
981         ds_put_cstr(ds, "negotiated\n");
982         break;
983     case LACP_CONFIGURED:
984         ds_put_cstr(ds, "configured\n");
985         break;
986     case LACP_DISABLED:
987         ds_put_cstr(ds, "off\n");
988         break;
989     default:
990         ds_put_cstr(ds, "<unknown>\n");
991         break;
992     }
993
994     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
995         shash_add(&slave_shash, slave->name, slave);
996     }
997     sorted_slaves = shash_sort(&slave_shash);
998
999     for (i = 0; i < shash_count(&slave_shash); i++) {
1000         struct bond_entry *be;
1001
1002         slave = sorted_slaves[i]->data;
1003
1004         /* Basic info. */
1005         ds_put_format(ds, "\nslave %s: %s\n",
1006                       slave->name, slave->enabled ? "enabled" : "disabled");
1007         if (slave == bond->active_slave) {
1008             ds_put_cstr(ds, "\tactive slave\n");
1009         }
1010         if (slave->delay_expires != LLONG_MAX) {
1011             ds_put_format(ds, "\t%s expires in %lld ms\n",
1012                           slave->enabled ? "downdelay" : "updelay",
1013                           slave->delay_expires - time_msec());
1014         }
1015
1016         ds_put_format(ds, "\tmay_enable: %s\n",
1017                       slave->may_enable ? "true" : "false");
1018
1019         if (!bond_is_balanced(bond)) {
1020             continue;
1021         }
1022
1023         /* Hashes. */
1024         for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1025             int hash = be - bond->hash;
1026
1027             if (be->slave != slave) {
1028                 continue;
1029             }
1030
1031             ds_put_format(ds, "\thash %d: %"PRIu64" kB load\n",
1032                           hash, be->tx_bytes / 1024);
1033
1034             /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
1035         }
1036     }
1037     shash_destroy(&slave_shash);
1038     free(sorted_slaves);
1039     ds_put_cstr(ds, "\n");
1040 }
1041
1042 static void
1043 bond_unixctl_show(struct unixctl_conn *conn,
1044                   int argc, const char *argv[],
1045                   void *aux OVS_UNUSED)
1046 {
1047     struct ds ds = DS_EMPTY_INITIALIZER;
1048
1049     if (argc > 1) {
1050         const struct bond *bond = bond_find(argv[1]);
1051
1052         if (!bond) {
1053             unixctl_command_reply_error(conn, "no such bond");
1054             return;
1055         }
1056         bond_print_details(&ds, bond);
1057     } else {
1058         const struct bond *bond;
1059
1060         HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
1061             bond_print_details(&ds, bond);
1062         }
1063     }
1064
1065     unixctl_command_reply(conn, ds_cstr(&ds));
1066     ds_destroy(&ds);
1067 }
1068
1069 static void
1070 bond_unixctl_migrate(struct unixctl_conn *conn,
1071                      int argc OVS_UNUSED, const char *argv[],
1072                      void *aux OVS_UNUSED)
1073 {
1074     const char *bond_s = argv[1];
1075     const char *hash_s = argv[2];
1076     const char *slave_s = argv[3];
1077     struct bond *bond;
1078     struct bond_slave *slave;
1079     struct bond_entry *entry;
1080     int hash;
1081
1082     bond = bond_find(bond_s);
1083     if (!bond) {
1084         unixctl_command_reply_error(conn, "no such bond");
1085         return;
1086     }
1087
1088     if (bond->balance != BM_SLB) {
1089         unixctl_command_reply_error(conn, "not an SLB bond");
1090         return;
1091     }
1092
1093     if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1094         hash = atoi(hash_s) & BOND_MASK;
1095     } else {
1096         unixctl_command_reply_error(conn, "bad hash");
1097         return;
1098     }
1099
1100     slave = bond_lookup_slave(bond, slave_s);
1101     if (!slave) {
1102         unixctl_command_reply_error(conn, "no such slave");
1103         return;
1104     }
1105
1106     if (!slave->enabled) {
1107         unixctl_command_reply_error(conn, "cannot migrate to disabled slave");
1108         return;
1109     }
1110
1111     entry = &bond->hash[hash];
1112     tag_set_add(&bond->unixctl_tags, entry->tag);
1113     entry->slave = slave;
1114     entry->tag = tag_create_random();
1115     unixctl_command_reply(conn, "migrated");
1116 }
1117
1118 static void
1119 bond_unixctl_set_active_slave(struct unixctl_conn *conn,
1120                               int argc OVS_UNUSED, const char *argv[],
1121                               void *aux OVS_UNUSED)
1122 {
1123     const char *bond_s = argv[1];
1124     const char *slave_s = argv[2];
1125     struct bond *bond;
1126     struct bond_slave *slave;
1127
1128     bond = bond_find(bond_s);
1129     if (!bond) {
1130         unixctl_command_reply_error(conn, "no such bond");
1131         return;
1132     }
1133
1134     slave = bond_lookup_slave(bond, slave_s);
1135     if (!slave) {
1136         unixctl_command_reply_error(conn, "no such slave");
1137         return;
1138     }
1139
1140     if (!slave->enabled) {
1141         unixctl_command_reply_error(conn, "cannot make disabled slave active");
1142         return;
1143     }
1144
1145     if (bond->active_slave != slave) {
1146         tag_set_add(&bond->unixctl_tags, bond_get_active_slave_tag(bond));
1147         bond->active_slave = slave;
1148         bond->active_slave->tag = tag_create_random();
1149         VLOG_INFO("bond %s: active interface is now %s",
1150                   bond->name, slave->name);
1151         bond->send_learning_packets = true;
1152         unixctl_command_reply(conn, "done");
1153     } else {
1154         unixctl_command_reply(conn, "no change");
1155     }
1156 }
1157
1158 static void
1159 enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable)
1160 {
1161     const char *bond_s = argv[1];
1162     const char *slave_s = argv[2];
1163     struct bond *bond;
1164     struct bond_slave *slave;
1165
1166     bond = bond_find(bond_s);
1167     if (!bond) {
1168         unixctl_command_reply_error(conn, "no such bond");
1169         return;
1170     }
1171
1172     slave = bond_lookup_slave(bond, slave_s);
1173     if (!slave) {
1174         unixctl_command_reply_error(conn, "no such slave");
1175         return;
1176     }
1177
1178     bond_enable_slave(slave, enable, &bond->unixctl_tags);
1179     unixctl_command_reply(conn, enable ? "enabled" : "disabled");
1180 }
1181
1182 static void
1183 bond_unixctl_enable_slave(struct unixctl_conn *conn,
1184                           int argc OVS_UNUSED, const char *argv[],
1185                           void *aux OVS_UNUSED)
1186 {
1187     enable_slave(conn, argv, true);
1188 }
1189
1190 static void
1191 bond_unixctl_disable_slave(struct unixctl_conn *conn,
1192                            int argc OVS_UNUSED, const char *argv[],
1193                            void *aux OVS_UNUSED)
1194 {
1195     enable_slave(conn, argv, false);
1196 }
1197
1198 static void
1199 bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[],
1200                   void *aux OVS_UNUSED)
1201 {
1202     const char *mac_s = argv[1];
1203     const char *vlan_s = argc > 2 ? argv[2] : NULL;
1204     const char *basis_s = argc > 3 ? argv[3] : NULL;
1205     uint8_t mac[ETH_ADDR_LEN];
1206     uint8_t hash;
1207     char *hash_cstr;
1208     unsigned int vlan;
1209     uint32_t basis;
1210
1211     if (vlan_s) {
1212         if (sscanf(vlan_s, "%u", &vlan) != 1) {
1213             unixctl_command_reply_error(conn, "invalid vlan");
1214             return;
1215         }
1216     } else {
1217         vlan = 0;
1218     }
1219
1220     if (basis_s) {
1221         if (sscanf(basis_s, "%"PRIu32, &basis) != 1) {
1222             unixctl_command_reply_error(conn, "invalid basis");
1223             return;
1224         }
1225     } else {
1226         basis = 0;
1227     }
1228
1229     if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
1230         == ETH_ADDR_SCAN_COUNT) {
1231         hash = bond_hash_src(mac, vlan, basis) & BOND_MASK;
1232
1233         hash_cstr = xasprintf("%u", hash);
1234         unixctl_command_reply(conn, hash_cstr);
1235         free(hash_cstr);
1236     } else {
1237         unixctl_command_reply_error(conn, "invalid mac");
1238     }
1239 }
1240
1241 void
1242 bond_init(void)
1243 {
1244     unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL);
1245     unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show,
1246                              NULL);
1247     unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
1248                              bond_unixctl_migrate, NULL);
1249     unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
1250                              bond_unixctl_set_active_slave, NULL);
1251     unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
1252                              bond_unixctl_enable_slave, NULL);
1253     unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
1254                              bond_unixctl_disable_slave, NULL);
1255     unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
1256                              bond_unixctl_hash, NULL);
1257 }
1258 \f
1259 static void
1260 bond_entry_reset(struct bond *bond)
1261 {
1262     if (bond->balance != BM_AB) {
1263         size_t hash_len = (BOND_MASK + 1) * sizeof *bond->hash;
1264
1265         if (!bond->hash) {
1266             bond->hash = xmalloc(hash_len);
1267         }
1268         memset(bond->hash, 0, hash_len);
1269
1270         bond->next_rebalance = time_msec() + bond->rebalance_interval;
1271     } else {
1272         free(bond->hash);
1273         bond->hash = NULL;
1274     }
1275 }
1276
1277 static struct bond_slave *
1278 bond_slave_lookup(struct bond *bond, const void *slave_)
1279 {
1280     struct bond_slave *slave;
1281
1282     HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1283                              &bond->slaves) {
1284         if (slave->aux == slave_) {
1285             return slave;
1286         }
1287     }
1288
1289     return NULL;
1290 }
1291
1292 static void
1293 bond_enable_slave(struct bond_slave *slave, bool enable, struct tag_set *tags)
1294 {
1295     slave->delay_expires = LLONG_MAX;
1296     if (enable != slave->enabled) {
1297         slave->enabled = enable;
1298         if (!slave->enabled) {
1299             VLOG_INFO("interface %s: disabled", slave->name);
1300             if (tags) {
1301                 tag_set_add(tags, slave->tag);
1302             }
1303         } else {
1304             VLOG_INFO("interface %s: enabled", slave->name);
1305             slave->tag = tag_create_random();
1306         }
1307     }
1308 }
1309
1310 static void
1311 bond_link_status_update(struct bond_slave *slave, struct tag_set *tags)
1312 {
1313     struct bond *bond = slave->bond;
1314     bool up;
1315
1316     up = netdev_get_carrier(slave->netdev) && slave->may_enable;
1317     if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1318         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1319         VLOG_INFO_RL(&rl, "interface %s: link state %s",
1320                      slave->name, up ? "up" : "down");
1321         if (up == slave->enabled) {
1322             slave->delay_expires = LLONG_MAX;
1323             VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1324                          slave->name, up ? "disabled" : "enabled");
1325         } else {
1326             int delay = (bond->lacp_status != LACP_DISABLED ? 0
1327                          : up ? bond->updelay : bond->downdelay);
1328             slave->delay_expires = time_msec() + delay;
1329             if (delay) {
1330                 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1331                              "for %d ms",
1332                              slave->name,
1333                              up ? "enabled" : "disabled",
1334                              up ? "up" : "down",
1335                              delay);
1336             }
1337         }
1338     }
1339
1340     if (time_msec() >= slave->delay_expires) {
1341         bond_enable_slave(slave, up, tags);
1342     }
1343 }
1344
1345 static unsigned int
1346 bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan, uint32_t basis)
1347 {
1348     return hash_3words(hash_bytes(mac, ETH_ADDR_LEN, 0), vlan, basis);
1349 }
1350
1351 static unsigned int
1352 bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis)
1353 {
1354     struct flow hash_flow = *flow;
1355     hash_flow.vlan_tci = htons(vlan);
1356
1357     /* The symmetric quality of this hash function is not required, but
1358      * flow_hash_symmetric_l4 already exists, and is sufficient for our
1359      * purposes, so we use it out of convenience. */
1360     return flow_hash_symmetric_l4(&hash_flow, basis);
1361 }
1362
1363 static unsigned int
1364 bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1365 {
1366     ovs_assert(bond->balance == BM_TCP || bond->balance == BM_SLB);
1367
1368     return (bond->balance == BM_TCP
1369             ? bond_hash_tcp(flow, vlan, bond->basis)
1370             : bond_hash_src(flow->dl_src, vlan, bond->basis));
1371 }
1372
1373 static struct bond_entry *
1374 lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1375                   uint16_t vlan)
1376 {
1377     return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
1378 }
1379
1380 static struct bond_slave *
1381 choose_output_slave(const struct bond *bond, const struct flow *flow,
1382                     struct flow_wildcards *wc, uint16_t vlan, tag_type *tags)
1383 {
1384     struct bond_entry *e;
1385
1386     if (bond->lacp_status == LACP_CONFIGURED) {
1387         /* LACP has been configured on this bond but negotiations were
1388          * unsuccussful.  Drop all traffic. */
1389         return NULL;
1390     }
1391
1392     switch (bond->balance) {
1393     case BM_AB:
1394         return bond->active_slave;
1395
1396     case BM_TCP:
1397         if (bond->lacp_status != LACP_NEGOTIATED) {
1398             /* Must have LACP negotiations for TCP balanced bonds. */
1399             return NULL;
1400         }
1401         if (wc) {
1402             flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
1403         }
1404         /* Fall Through. */
1405     case BM_SLB:
1406         if (wc) {
1407             flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_ETH_SRC);
1408         }
1409         e = lookup_bond_entry(bond, flow, vlan);
1410         if (!e->slave || !e->slave->enabled) {
1411             e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
1412                                     struct bond_slave, hmap_node);
1413             if (!e->slave->enabled) {
1414                 e->slave = bond->active_slave;
1415             }
1416             e->tag = tag_create_random();
1417         }
1418         *tags |= e->tag;
1419         return e->slave;
1420
1421     default:
1422         NOT_REACHED();
1423     }
1424 }
1425
1426 static struct bond_slave *
1427 bond_choose_slave(const struct bond *bond)
1428 {
1429     struct bond_slave *slave, *best;
1430
1431     /* Find an enabled slave. */
1432     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1433         if (slave->enabled) {
1434             return slave;
1435         }
1436     }
1437
1438     /* All interfaces are disabled.  Find an interface that will be enabled
1439      * after its updelay expires.  */
1440     best = NULL;
1441     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1442         if (slave->delay_expires != LLONG_MAX
1443             && slave->may_enable
1444             && (!best || slave->delay_expires < best->delay_expires)) {
1445             best = slave;
1446         }
1447     }
1448     return best;
1449 }
1450
1451 static void
1452 bond_choose_active_slave(struct bond *bond, struct tag_set *tags)
1453 {
1454     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1455     struct bond_slave *old_active_slave = bond->active_slave;
1456
1457     bond->active_slave = bond_choose_slave(bond);
1458     if (bond->active_slave) {
1459         if (bond->active_slave->enabled) {
1460             VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1461                          bond->name, bond->active_slave->name);
1462         } else {
1463             VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1464                          "remaining %lld ms updelay (since no interface was "
1465                          "enabled)", bond->name, bond->active_slave->name,
1466                          bond->active_slave->delay_expires - time_msec());
1467             bond_enable_slave(bond->active_slave, true, tags);
1468         }
1469
1470         if (!old_active_slave) {
1471             tag_set_add(tags, bond->no_slaves_tag);
1472         }
1473
1474         bond->send_learning_packets = true;
1475     } else if (old_active_slave) {
1476         VLOG_INFO_RL(&rl, "bond %s: all interfaces disabled", bond->name);
1477     }
1478 }
1479
1480 /* Returns the tag for 'bond''s active slave, or 'bond''s no_slaves_tag if
1481  * there is no active slave. */
1482 static tag_type
1483 bond_get_active_slave_tag(const struct bond *bond)
1484 {
1485     return (bond->active_slave
1486             ? bond->active_slave->tag
1487             : bond->no_slaves_tag);
1488 }
1489
1490 /* Attempts to make the sum of the bond slaves' statistics appear on the fake
1491  * bond interface. */
1492 static void
1493 bond_update_fake_slave_stats(struct bond *bond)
1494 {
1495     struct netdev_stats bond_stats;
1496     struct bond_slave *slave;
1497     struct netdev *bond_dev;
1498
1499     memset(&bond_stats, 0, sizeof bond_stats);
1500
1501     HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1502         struct netdev_stats slave_stats;
1503
1504         if (!netdev_get_stats(slave->netdev, &slave_stats)) {
1505             /* XXX: We swap the stats here because they are swapped back when
1506              * reported by the internal device.  The reason for this is
1507              * internal devices normally represent packets going into the
1508              * system but when used as fake bond device they represent packets
1509              * leaving the system.  We really should do this in the internal
1510              * device itself because changing it here reverses the counts from
1511              * the perspective of the switch.  However, the internal device
1512              * doesn't know what type of device it represents so we have to do
1513              * it here for now. */
1514             bond_stats.tx_packets += slave_stats.rx_packets;
1515             bond_stats.tx_bytes += slave_stats.rx_bytes;
1516             bond_stats.rx_packets += slave_stats.tx_packets;
1517             bond_stats.rx_bytes += slave_stats.tx_bytes;
1518         }
1519     }
1520
1521     if (!netdev_open(bond->name, "system", &bond_dev)) {
1522         netdev_set_stats(bond_dev, &bond_stats);
1523         netdev_close(bond_dev);
1524     }
1525 }