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