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