lacp: Remove custom transmission intervals.
[sliver-openvswitch.git] / lib / lacp.c
1 /* Copyright (c) 2011 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "lacp.h"
18
19 #include <assert.h>
20 #include <stdlib.h>
21
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "hmap.h"
25 #include "ofpbuf.h"
26 #include "packets.h"
27 #include "poll-loop.h"
28 #include "shash.h"
29 #include "timer.h"
30 #include "timeval.h"
31 #include "unixctl.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(lacp);
35
36 /* Masks for lacp_info state member. */
37 #define LACP_STATE_ACT  0x01 /* Activity. Active or passive? */
38 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
39 #define LACP_STATE_AGG  0x04 /* Aggregation. Is the link is bondable? */
40 #define LACP_STATE_SYNC 0x08 /* Synchronization. Is the link in up to date? */
41 #define LACP_STATE_COL  0x10 /* Collecting. Is the link receiving frames? */
42 #define LACP_STATE_DIST 0x20 /* Distributing. Is the link sending frames? */
43 #define LACP_STATE_DEF  0x40 /* Defaulted. Using default partner info? */
44 #define LACP_STATE_EXP  0x80 /* Expired. Using expired partner info? */
45
46 #define LACP_FAST_TIME_TX 1000  /* Fast transmission rate. */
47 #define LACP_SLOW_TIME_TX 30000 /* Slow transmission rate. */
48 #define LACP_RX_MULTIPLIER 3    /* Multiply by TX rate to get RX rate. */
49
50 #define LACP_INFO_LEN 15
51 struct lacp_info {
52     ovs_be16 sys_priority;            /* System priority. */
53     uint8_t sys_id[ETH_ADDR_LEN];     /* System ID. */
54     ovs_be16 key;                     /* Operational key. */
55     ovs_be16 port_priority;           /* Port priority. */
56     ovs_be16 port_id;                 /* Port ID. */
57     uint8_t state;                    /* State mask.  See LACP_STATE macros. */
58 } __attribute__((packed));
59 BUILD_ASSERT_DECL(LACP_INFO_LEN == sizeof(struct lacp_info));
60
61 #define LACP_PDU_LEN 110
62 struct lacp_pdu {
63     uint8_t subtype;          /* Always 1. */
64     uint8_t version;          /* Always 1. */
65
66     uint8_t actor_type;       /* Always 1. */
67     uint8_t actor_len;        /* Always 20. */
68     struct lacp_info actor;   /* LACP actor information. */
69     uint8_t z1[3];            /* Reserved.  Always 0. */
70
71     uint8_t partner_type;     /* Always 2. */
72     uint8_t partner_len;      /* Always 20. */
73     struct lacp_info partner; /* LACP partner information. */
74     uint8_t z2[3];            /* Reserved.  Always 0. */
75
76     uint8_t collector_type;   /* Always 3. */
77     uint8_t collector_len;    /* Always 16. */
78     ovs_be16 collector_delay; /* Maximum collector delay. Set to UINT16_MAX. */
79     uint8_t z3[64];           /* Combination of several fields.  Always 0. */
80 } __attribute__((packed));
81 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
82 \f
83 /* Implementation. */
84
85 enum slave_status {
86     LACP_CURRENT,   /* Current State.  Partner up to date. */
87     LACP_EXPIRED,   /* Expired State.  Partner out of date. */
88     LACP_DEFAULTED, /* Defaulted State.  No partner. */
89 };
90
91 struct lacp {
92     struct list node;             /* Node in all_lacps list. */
93     char *name;                   /* Name of this lacp object. */
94     uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
95     uint16_t sys_priority;        /* System Priority. */
96     bool active;                  /* Active or Passive. */
97
98     struct hmap slaves;      /* Slaves this LACP object controls. */
99     struct slave *key_slave; /* Slave whose ID will be the aggregation key. */
100
101     bool fast;               /* True if using fast probe interval. */
102     bool negotiated;         /* True if LACP negotiations were successful. */
103     bool update;             /* True if lacp_update() needs to be called. */
104     bool heartbeat;          /* LACP heartbeat mode. */
105 };
106
107 struct slave {
108     void *aux;                    /* Handle used to identify this slave. */
109     struct hmap_node node;        /* Node in master's slaves map. */
110
111     struct lacp *lacp;            /* LACP object containing this slave. */
112     uint16_t port_id;             /* Port ID. */
113     uint16_t port_priority;       /* Port Priority. */
114     uint16_t key;                 /* Aggregation Key. 0 if default. */
115     char *name;                   /* Name of this slave. */
116
117     enum slave_status status;     /* Slave status. */
118     bool attached;                /* Attached. Traffic may flow. */
119     struct lacp_info partner;     /* Partner information. */
120     struct lacp_info ntt_actor;   /* Used to decide if we Need To Transmit. */
121     struct timer tx;              /* Next message transmission timer. */
122     struct timer rx;              /* Expected message receive timer. */
123 };
124
125 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
126
127 static void lacp_update_attached(struct lacp *);
128
129 static void slave_destroy(struct slave *);
130 static void slave_set_defaulted(struct slave *);
131 static void slave_set_expired(struct slave *);
132 static void slave_get_actor(struct slave *, struct lacp_info *actor);
133 static void slave_get_priority(struct slave *, struct lacp_info *priority);
134 static bool slave_may_tx(const struct slave *);
135 static struct slave *slave_lookup(const struct lacp *, const void *slave);
136 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
137
138 static unixctl_cb_func lacp_unixctl_show;
139
140 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
141 static void
142 compose_lacp_pdu(const struct lacp_info *actor,
143                  const struct lacp_info *partner, struct lacp_pdu *pdu)
144 {
145     memset(pdu, 0, sizeof *pdu);
146
147     pdu->subtype = 1;
148     pdu->version = 1;
149
150     pdu->actor_type = 1;
151     pdu->actor_len = 20;
152     pdu->actor = *actor;
153
154     pdu->partner_type = 2;
155     pdu->partner_len = 20;
156     pdu->partner = *partner;
157
158     pdu->collector_type = 3;
159     pdu->collector_len = 16;
160     pdu->collector_delay = htons(0);
161 }
162
163 /* Parses 'b' which represents a packet containing a LACP PDU.  This function
164  * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
165  * supported by OVS.  Otherwise, it returns a pointer to the lacp_pdu contained
166  * within 'b'. */
167 static const struct lacp_pdu *
168 parse_lacp_packet(const struct ofpbuf *b)
169 {
170     const struct lacp_pdu *pdu;
171
172     pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
173
174     if (pdu && pdu->subtype == 1
175         && pdu->actor_type == 1 && pdu->actor_len == 20
176         && pdu->partner_type == 2 && pdu->partner_len == 20) {
177         return pdu;
178     } else {
179         return NULL;
180     }
181 }
182 \f
183 /* LACP Protocol Implementation. */
184
185 /* Initializes the lacp module. */
186 void
187 lacp_init(void)
188 {
189     unixctl_command_register("lacp/show", "[port]", 0, 1,
190                              lacp_unixctl_show, NULL);
191 }
192
193 /* Creates a LACP object. */
194 struct lacp *
195 lacp_create(void)
196 {
197     struct lacp *lacp;
198
199     lacp = xzalloc(sizeof *lacp);
200     hmap_init(&lacp->slaves);
201     list_push_back(&all_lacps, &lacp->node);
202     return lacp;
203 }
204
205 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
206 void
207 lacp_destroy(struct lacp *lacp)
208 {
209     if (lacp) {
210         struct slave *slave, *next;
211
212         HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
213             slave_destroy(slave);
214         }
215
216         hmap_destroy(&lacp->slaves);
217         list_remove(&lacp->node);
218         free(lacp->name);
219         free(lacp);
220     }
221 }
222
223 /* Configures 'lacp' with settings from 's'. */
224 void
225 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
226 {
227     assert(!eth_addr_is_zero(s->id));
228
229     if (!lacp->name || strcmp(s->name, lacp->name)) {
230         free(lacp->name);
231         lacp->name = xstrdup(s->name);
232     }
233
234     if (!eth_addr_equals(lacp->sys_id, s->id)
235         || lacp->sys_priority != s->priority
236         || lacp->heartbeat != s->heartbeat) {
237         memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
238         lacp->sys_priority = s->priority;
239         lacp->heartbeat = s->heartbeat;
240         lacp->update = true;
241     }
242
243     lacp->active = s->active;
244     lacp->fast = s->fast;
245 }
246
247 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
248  * configured for passive mode. */
249 bool
250 lacp_is_active(const struct lacp *lacp)
251 {
252     return lacp->active;
253 }
254
255 /* Processes 'packet' which was received on 'slave_'.  This function should be
256  * called on all packets received on 'slave_' with Ethernet Type ETH_TYPE_LACP.
257  */
258 void
259 lacp_process_packet(struct lacp *lacp, const void *slave_,
260                     const struct ofpbuf *packet)
261 {
262     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
263     struct slave *slave = slave_lookup(lacp, slave_);
264     const struct lacp_pdu *pdu;
265     long long int tx_rate;
266
267     pdu = parse_lacp_packet(packet);
268     if (!pdu) {
269         VLOG_WARN_RL(&rl, "%s: received an unparsable LACP PDU.", lacp->name);
270         return;
271     }
272
273     slave->status = LACP_CURRENT;
274     tx_rate = lacp->fast ? LACP_FAST_TIME_TX : LACP_SLOW_TIME_TX;
275     timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
276
277     slave->ntt_actor = pdu->partner;
278
279     /* Update our information about our partner if it's out of date.  This may
280      * cause priorities to change so re-calculate attached status of all
281      * slaves.  */
282     if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
283         lacp->update = true;
284         slave->partner = pdu->actor;
285     }
286 }
287
288 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
289 enum lacp_status
290 lacp_status(const struct lacp *lacp)
291 {
292     if (!lacp) {
293         return LACP_DISABLED;
294     } else if (lacp->negotiated) {
295         return LACP_NEGOTIATED;
296     } else {
297         return LACP_CONFIGURED;
298     }
299 }
300
301 /* Registers 'slave_' as subordinate to 'lacp'.  This should be called at least
302  * once per slave in a LACP managed bond.  Should also be called whenever a
303  * slave's settings change. */
304 void
305 lacp_slave_register(struct lacp *lacp, void *slave_,
306                     const struct lacp_slave_settings *s)
307 {
308     struct slave *slave = slave_lookup(lacp, slave_);
309
310     if (!slave) {
311         slave = xzalloc(sizeof *slave);
312         slave->lacp = lacp;
313         slave->aux = slave_;
314         hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
315         slave_set_defaulted(slave);
316
317         if (!lacp->key_slave) {
318             lacp->key_slave = slave;
319         }
320     }
321
322     if (!slave->name || strcmp(s->name, slave->name)) {
323         free(slave->name);
324         slave->name = xstrdup(s->name);
325     }
326
327     if (slave->port_id != s->id
328         || slave->port_priority != s->priority
329         || slave->key != s->key) {
330         slave->port_id = s->id;
331         slave->port_priority = s->priority;
332         slave->key = s->key;
333
334         lacp->update = true;
335
336         if (lacp->active || lacp->negotiated) {
337             slave_set_expired(slave);
338         }
339     }
340 }
341
342 /* Unregisters 'slave_' with 'lacp'.  */
343 void
344 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
345 {
346     struct slave *slave = slave_lookup(lacp, slave_);
347
348     if (slave) {
349         slave_destroy(slave);
350         lacp->update = true;
351     }
352 }
353
354 /* This function should be called whenever the carrier status of 'slave_' has
355  * changed.  If 'lacp' is null, this function has no effect.*/
356 void
357 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
358 {
359     if (lacp) {
360         struct slave *slave = slave_lookup(lacp, slave_);
361
362         if (slave->status == LACP_CURRENT || slave->lacp->active) {
363             slave_set_expired(slave);
364         }
365     }
366 }
367
368 /* This function should be called before enabling 'slave_' to send or receive
369  * traffic.  If it returns false, 'slave_' should not enabled.  As a
370  * convenience, returns true if 'lacp' is NULL. */
371 bool
372 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
373 {
374     if (lacp) {
375         struct slave *slave = slave_lookup(lacp, slave_);
376
377         /* The slave may be enabled if it's attached to an aggregator and its
378          * partner is synchronized.*/
379         return slave->attached && (slave->partner.state & LACP_STATE_SYNC);
380     } else {
381         return true;
382     }
383 }
384
385 /* Returns the port ID used for 'slave_' in LACP communications. */
386 uint16_t
387 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
388 {
389     struct slave *slave = slave_lookup(lacp, slave_);
390     return slave->port_id;
391 }
392
393 /* Returns true if partner information on 'slave_' is up to date.  'slave_'
394  * not being current, generally indicates a connectivity problem, or a
395  * misconfigured (or broken) partner. */
396 bool
397 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
398 {
399     return slave_lookup(lacp, slave_)->status != LACP_DEFAULTED;
400 }
401
402 /* This function should be called periodically to update 'lacp'. */
403 void
404 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
405 {
406     struct slave *slave;
407
408     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
409         if (timer_expired(&slave->rx)) {
410             if (slave->status == LACP_CURRENT) {
411                 slave_set_expired(slave);
412             } else if (slave->status == LACP_EXPIRED) {
413                 slave_set_defaulted(slave);
414             }
415         }
416     }
417
418     if (lacp->update) {
419         lacp_update_attached(lacp);
420     }
421
422     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
423         struct lacp_info actor;
424
425         if (!slave_may_tx(slave)) {
426             continue;
427         }
428
429         slave_get_actor(slave, &actor);
430
431         if (timer_expired(&slave->tx)
432             || !info_tx_equal(&actor, &slave->ntt_actor)) {
433             long long int duration;
434             struct lacp_pdu pdu;
435
436             slave->ntt_actor = actor;
437             compose_lacp_pdu(&actor, &slave->partner, &pdu);
438             send_pdu(slave->aux, &pdu, sizeof pdu);
439
440             duration = (slave->partner.state & LACP_STATE_TIME
441                         ? LACP_FAST_TIME_TX
442                         : LACP_SLOW_TIME_TX);
443
444             timer_set_duration(&slave->tx, duration);
445         }
446     }
447 }
448
449 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
450 void
451 lacp_wait(struct lacp *lacp)
452 {
453     struct slave *slave;
454
455     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
456         if (slave_may_tx(slave)) {
457             timer_wait(&slave->tx);
458         }
459
460         if (slave->status != LACP_DEFAULTED) {
461             timer_wait(&slave->rx);
462         }
463     }
464 }
465 \f
466 /* Static Helpers. */
467
468 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
469  * negotiated parameter to true if any slaves are attachable. */
470 static void
471 lacp_update_attached(struct lacp *lacp)
472 {
473     struct slave *lead, *slave;
474     struct lacp_info lead_pri;
475     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
476
477     if (lacp->heartbeat) {
478         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
479             slave->attached = slave->status != LACP_DEFAULTED;
480         }
481         return;
482     }
483
484     lacp->update = false;
485
486     lead = NULL;
487     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
488         struct lacp_info pri;
489
490         slave->attached = false;
491
492         /* XXX: In the future allow users to configure the expected system ID.
493          * For now just special case loopback. */
494         if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
495             VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
496                          "connected to its own bond", slave->name);
497             continue;
498         }
499
500         if (slave->status == LACP_DEFAULTED) {
501             continue;
502         }
503
504         slave->attached = true;
505         slave_get_priority(slave, &pri);
506
507         if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
508             lead = slave;
509             lead_pri = pri;
510         }
511     }
512
513     lacp->negotiated = lead != NULL;
514
515     if (lead) {
516         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
517             if (lead->partner.key != slave->partner.key
518                 || !eth_addr_equals(lead->partner.sys_id,
519                                     slave->partner.sys_id)) {
520                 slave->attached = false;
521             }
522         }
523     }
524 }
525
526 static void
527 slave_destroy(struct slave *slave)
528 {
529     if (slave) {
530         struct lacp *lacp = slave->lacp;
531
532         lacp->update = true;
533         hmap_remove(&lacp->slaves, &slave->node);
534
535         if (lacp->key_slave == slave) {
536             struct hmap_node *slave_node = hmap_first(&lacp->slaves);
537
538             if (slave_node) {
539                 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
540             } else {
541                 lacp->key_slave = NULL;
542             }
543         }
544
545         free(slave->name);
546         free(slave);
547     }
548 }
549
550 static void
551 slave_set_defaulted(struct slave *slave)
552 {
553     memset(&slave->partner, 0, sizeof slave->partner);
554
555     slave->lacp->update = true;
556     slave->status = LACP_DEFAULTED;
557 }
558
559 static void
560 slave_set_expired(struct slave *slave)
561 {
562     slave->status = LACP_EXPIRED;
563     slave->partner.state |= LACP_STATE_TIME;
564     slave->partner.state &= ~LACP_STATE_SYNC;
565
566     timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX);
567 }
568
569 static void
570 slave_get_actor(struct slave *slave, struct lacp_info *actor)
571 {
572     struct lacp *lacp = slave->lacp;
573     uint16_t key;
574     uint8_t state = 0;
575
576     if (lacp->active) {
577         state |= LACP_STATE_ACT;
578     }
579
580     if (lacp->fast) {
581         state |= LACP_STATE_TIME;
582     }
583
584     if (slave->attached) {
585         state |= LACP_STATE_SYNC;
586     }
587
588     if (slave->status == LACP_DEFAULTED) {
589         state |= LACP_STATE_DEF;
590     }
591
592     if (slave->status == LACP_EXPIRED) {
593         state |= LACP_STATE_EXP;
594     }
595
596     if (lacp->heartbeat || hmap_count(&lacp->slaves) > 1) {
597         state |= LACP_STATE_AGG;
598     }
599
600     if (slave->attached || !lacp->negotiated) {
601         state |= LACP_STATE_COL | LACP_STATE_DIST;
602     }
603
604     key = lacp->key_slave->key;
605     if (!key) {
606         key = lacp->key_slave->port_id;
607     }
608
609     actor->state = state;
610     actor->key = htons(key);
611     actor->port_priority = htons(slave->port_priority);
612     actor->port_id = htons(slave->port_id);
613     actor->sys_priority = htons(lacp->sys_priority);
614     memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
615 }
616
617 /* Given 'slave', populates 'priority' with data representing its LACP link
618  * priority.  If two priority objects populated by this function are compared
619  * using memcmp, the higher priority link will be less than the lower priority
620  * link. */
621 static void
622 slave_get_priority(struct slave *slave, struct lacp_info *priority)
623 {
624     uint16_t partner_priority, actor_priority;
625
626     /* Choose the lacp_info of the higher priority system by comparing their
627      * system priorities and mac addresses. */
628     actor_priority = slave->lacp->sys_priority;
629     partner_priority = ntohs(slave->partner.sys_priority);
630     if (actor_priority < partner_priority) {
631         slave_get_actor(slave, priority);
632     } else if (partner_priority < actor_priority) {
633         *priority = slave->partner;
634     } else if (eth_addr_compare_3way(slave->lacp->sys_id,
635                                      slave->partner.sys_id) < 0) {
636         slave_get_actor(slave, priority);
637     } else {
638         *priority = slave->partner;
639     }
640
641     /* Key and state are not used in priority comparisons. */
642     priority->key = 0;
643     priority->state = 0;
644 }
645
646 static bool
647 slave_may_tx(const struct slave *slave)
648 {
649     return slave->lacp->active || slave->status != LACP_DEFAULTED;
650 }
651
652 static struct slave *
653 slave_lookup(const struct lacp *lacp, const void *slave_)
654 {
655     struct slave *slave;
656
657     HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
658                              &lacp->slaves) {
659         if (slave->aux == slave_) {
660             return slave;
661         }
662     }
663
664     return NULL;
665 }
666
667 /* Two lacp_info structures are tx_equal if and only if they do not differ in
668  * ways which would require a lacp_pdu transmission. */
669 static bool
670 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
671 {
672
673     /* LACP specification dictates that we transmit whenever the actor and
674      * remote_actor differ in the following fields: Port, Port Priority,
675      * System, System Priority, Aggregation Key, Activity State, Timeout State,
676      * Sync State, and Aggregation State. The state flags are most likely to
677      * change so are checked first. */
678     return !((a->state ^ b->state) & (LACP_STATE_ACT
679                                       | LACP_STATE_TIME
680                                       | LACP_STATE_SYNC
681                                       | LACP_STATE_AGG))
682         && a->port_id == b->port_id
683         && a->port_priority == b->port_priority
684         && a->key == b->key
685         && a->sys_priority == b->sys_priority
686         && eth_addr_equals(a->sys_id, b->sys_id);
687 }
688 \f
689 static struct lacp *
690 lacp_find(const char *name)
691 {
692     struct lacp *lacp;
693
694     LIST_FOR_EACH (lacp, node, &all_lacps) {
695         if (!strcmp(lacp->name, name)) {
696             return lacp;
697         }
698     }
699
700     return NULL;
701 }
702
703 static void
704 ds_put_lacp_state(struct ds *ds, uint8_t state)
705 {
706     if (state & LACP_STATE_ACT) {
707         ds_put_cstr(ds, " activity");
708     }
709
710     if (state & LACP_STATE_TIME) {
711         ds_put_cstr(ds, " timeout");
712     }
713
714     if (state & LACP_STATE_AGG) {
715         ds_put_cstr(ds, " aggregation");
716     }
717
718     if (state & LACP_STATE_SYNC) {
719         ds_put_cstr(ds, " synchronized");
720     }
721
722     if (state & LACP_STATE_COL) {
723         ds_put_cstr(ds, " collecting");
724     }
725
726     if (state & LACP_STATE_DIST) {
727         ds_put_cstr(ds, " distributing");
728     }
729
730     if (state & LACP_STATE_DEF) {
731         ds_put_cstr(ds, " defaulted");
732     }
733
734     if (state & LACP_STATE_EXP) {
735         ds_put_cstr(ds, " expired");
736     }
737 }
738
739 static void
740 lacp_print_details(struct ds *ds, struct lacp *lacp)
741 {
742     struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
743     const struct shash_node **sorted_slaves = NULL;
744
745     struct slave *slave;
746     int i;
747
748     ds_put_format(ds, "---- %s ----\n", lacp->name);
749     ds_put_format(ds, "\tstatus: %s", lacp->active ? "active" : "passive");
750     if (lacp->heartbeat) {
751         ds_put_cstr(ds, " heartbeat");
752     }
753     if (lacp->negotiated) {
754         ds_put_cstr(ds, " negotiated");
755     }
756     ds_put_cstr(ds, "\n");
757
758     ds_put_format(ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
759     ds_put_format(ds, "\tsys_priority: %u\n", lacp->sys_priority);
760     ds_put_cstr(ds, "\taggregation key: ");
761     if (lacp->key_slave) {
762         ds_put_format(ds, "%u", lacp->key_slave->port_id);
763     } else {
764         ds_put_cstr(ds, "none");
765     }
766     ds_put_cstr(ds, "\n");
767
768     ds_put_cstr(ds, "\tlacp_time: ");
769     if (lacp->fast) {
770         ds_put_cstr(ds, "fast\n");
771     } else {
772         ds_put_cstr(ds, "slow\n");
773     }
774
775     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
776         shash_add(&slave_shash, slave->name, slave);
777     }
778     sorted_slaves = shash_sort(&slave_shash);
779
780     for (i = 0; i < shash_count(&slave_shash); i++) {
781         char *status;
782         struct lacp_info actor;
783
784         slave = sorted_slaves[i]->data;
785         slave_get_actor(slave, &actor);
786         switch (slave->status) {
787         case LACP_CURRENT:
788             status = "current";
789             break;
790         case LACP_EXPIRED:
791             status = "expired";
792             break;
793         case LACP_DEFAULTED:
794             status = "defaulted";
795             break;
796         default:
797             NOT_REACHED();
798         }
799
800         ds_put_format(ds, "\nslave: %s: %s %s\n", slave->name, status,
801                       slave->attached ? "attached" : "detached");
802         ds_put_format(ds, "\tport_id: %u\n", slave->port_id);
803         ds_put_format(ds, "\tport_priority: %u\n", slave->port_priority);
804
805         ds_put_format(ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
806                       ETH_ADDR_ARGS(actor.sys_id));
807         ds_put_format(ds, "\tactor sys_priority: %u\n",
808                       ntohs(actor.sys_priority));
809         ds_put_format(ds, "\tactor port_id: %u\n",
810                       ntohs(actor.port_id));
811         ds_put_format(ds, "\tactor port_priority: %u\n",
812                       ntohs(actor.port_priority));
813         ds_put_format(ds, "\tactor key: %u\n",
814                       ntohs(actor.key));
815         ds_put_cstr(ds, "\tactor state:");
816         ds_put_lacp_state(ds, actor.state);
817         ds_put_cstr(ds, "\n\n");
818
819         ds_put_format(ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
820                       ETH_ADDR_ARGS(slave->partner.sys_id));
821         ds_put_format(ds, "\tpartner sys_priority: %u\n",
822                       ntohs(slave->partner.sys_priority));
823         ds_put_format(ds, "\tpartner port_id: %u\n",
824                       ntohs(slave->partner.port_id));
825         ds_put_format(ds, "\tpartner port_priority: %u\n",
826                       ntohs(slave->partner.port_priority));
827         ds_put_format(ds, "\tpartner key: %u\n",
828                       ntohs(slave->partner.key));
829         ds_put_cstr(ds, "\tpartner state:");
830         ds_put_lacp_state(ds, slave->partner.state);
831         ds_put_cstr(ds, "\n");
832     }
833
834     shash_destroy(&slave_shash);
835     free(sorted_slaves);
836 }
837
838 static void
839 lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
840                   void *aux OVS_UNUSED)
841 {
842     struct ds ds = DS_EMPTY_INITIALIZER;
843     struct lacp *lacp;
844
845     if (argc > 1) {
846         lacp = lacp_find(argv[1]);
847         if (!lacp) {
848             unixctl_command_reply_error(conn, "no such lacp object");
849             return;
850         }
851         lacp_print_details(&ds, lacp);
852     } else {
853         LIST_FOR_EACH (lacp, node, &all_lacps) {
854             lacp_print_details(&ds, lacp);
855         }
856     }
857
858     unixctl_command_reply(conn, ds_cstr(&ds));
859     ds_destroy(&ds);
860 }