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