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