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