lacp: Handle unknown slaves in lacp_process_packet().
[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     if (!slave) {
283         return;
284     }
285
286     pdu = parse_lacp_packet(packet);
287     if (!pdu) {
288         VLOG_WARN_RL(&rl, "%s: received an unparsable LACP PDU.", lacp->name);
289         return;
290     }
291
292     slave->status = LACP_CURRENT;
293     tx_rate = lacp->fast ? LACP_FAST_TIME_TX : LACP_SLOW_TIME_TX;
294     timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
295
296     slave->ntt_actor = pdu->partner;
297
298     /* Update our information about our partner if it's out of date.  This may
299      * cause priorities to change so re-calculate attached status of all
300      * slaves.  */
301     if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
302         lacp->update = true;
303         slave->partner = pdu->actor;
304     }
305 }
306
307 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
308 enum lacp_status
309 lacp_status(const struct lacp *lacp)
310 {
311     if (!lacp) {
312         return LACP_DISABLED;
313     } else if (lacp->negotiated) {
314         return LACP_NEGOTIATED;
315     } else {
316         return LACP_CONFIGURED;
317     }
318 }
319
320 /* Registers 'slave_' as subordinate to 'lacp'.  This should be called at least
321  * once per slave in a LACP managed bond.  Should also be called whenever a
322  * slave's settings change. */
323 void
324 lacp_slave_register(struct lacp *lacp, void *slave_,
325                     const struct lacp_slave_settings *s)
326 {
327     struct slave *slave = slave_lookup(lacp, slave_);
328
329     if (!slave) {
330         slave = xzalloc(sizeof *slave);
331         slave->lacp = lacp;
332         slave->aux = slave_;
333         hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
334         slave_set_defaulted(slave);
335
336         if (!lacp->key_slave) {
337             lacp->key_slave = slave;
338         }
339     }
340
341     if (!slave->name || strcmp(s->name, slave->name)) {
342         free(slave->name);
343         slave->name = xstrdup(s->name);
344     }
345
346     if (slave->port_id != s->id
347         || slave->port_priority != s->priority
348         || slave->key != s->key) {
349         slave->port_id = s->id;
350         slave->port_priority = s->priority;
351         slave->key = s->key;
352
353         lacp->update = true;
354
355         if (lacp->active || lacp->negotiated) {
356             slave_set_expired(slave);
357         }
358     }
359 }
360
361 /* Unregisters 'slave_' with 'lacp'.  */
362 void
363 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
364 {
365     struct slave *slave = slave_lookup(lacp, slave_);
366
367     if (slave) {
368         slave_destroy(slave);
369         lacp->update = true;
370     }
371 }
372
373 /* This function should be called whenever the carrier status of 'slave_' has
374  * changed.  If 'lacp' is null, this function has no effect.*/
375 void
376 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
377 {
378     if (lacp) {
379         struct slave *slave = slave_lookup(lacp, slave_);
380
381         if (!slave) {
382             return;
383         }
384
385         if (slave->status == LACP_CURRENT || slave->lacp->active) {
386             slave_set_expired(slave);
387         }
388     }
389 }
390
391 static bool
392 slave_may_enable__(struct slave *slave)
393 {
394     /* The slave may be enabled if it's attached to an aggregator and its
395      * partner is synchronized.*/
396     return slave->attached && (slave->partner.state & LACP_STATE_SYNC);
397 }
398
399 /* This function should be called before enabling 'slave_' to send or receive
400  * traffic.  If it returns false, 'slave_' should not enabled.  As a
401  * convenience, returns true if 'lacp' is NULL. */
402 bool
403 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
404 {
405     if (lacp) {
406         struct slave *slave = slave_lookup(lacp, slave_);
407         return slave ? slave_may_enable__(slave) : false;
408     } else {
409         return true;
410     }
411 }
412
413 /* Returns true if partner information on 'slave_' is up to date.  'slave_'
414  * not being current, generally indicates a connectivity problem, or a
415  * misconfigured (or broken) partner. */
416 bool
417 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
418 {
419     struct slave *slave = slave_lookup(lacp, slave_);
420     return slave ? slave->status != LACP_DEFAULTED : false;
421 }
422
423 /* This function should be called periodically to update 'lacp'. */
424 void
425 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
426 {
427     struct slave *slave;
428
429     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
430         if (timer_expired(&slave->rx)) {
431             if (slave->status == LACP_CURRENT) {
432                 slave_set_expired(slave);
433             } else if (slave->status == LACP_EXPIRED) {
434                 slave_set_defaulted(slave);
435             }
436         }
437     }
438
439     if (lacp->update) {
440         lacp_update_attached(lacp);
441     }
442
443     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
444         struct lacp_info actor;
445
446         if (!slave_may_tx(slave)) {
447             continue;
448         }
449
450         slave_get_actor(slave, &actor);
451
452         if (timer_expired(&slave->tx)
453             || !info_tx_equal(&actor, &slave->ntt_actor)) {
454             long long int duration;
455             struct lacp_pdu pdu;
456
457             slave->ntt_actor = actor;
458             compose_lacp_pdu(&actor, &slave->partner, &pdu);
459             send_pdu(slave->aux, &pdu, sizeof pdu);
460
461             duration = (slave->partner.state & LACP_STATE_TIME
462                         ? LACP_FAST_TIME_TX
463                         : LACP_SLOW_TIME_TX);
464
465             timer_set_duration(&slave->tx, duration);
466         }
467     }
468 }
469
470 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
471 void
472 lacp_wait(struct lacp *lacp)
473 {
474     struct slave *slave;
475
476     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
477         if (slave_may_tx(slave)) {
478             timer_wait(&slave->tx);
479         }
480
481         if (slave->status != LACP_DEFAULTED) {
482             timer_wait(&slave->rx);
483         }
484     }
485 }
486 \f
487 /* Static Helpers. */
488
489 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
490  * negotiated parameter to true if any slaves are attachable. */
491 static void
492 lacp_update_attached(struct lacp *lacp)
493 {
494     struct slave *lead, *slave;
495     struct lacp_info lead_pri;
496     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
497
498     lacp->update = false;
499
500     lead = NULL;
501     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
502         struct lacp_info pri;
503
504         slave->attached = false;
505
506         /* XXX: In the future allow users to configure the expected system ID.
507          * For now just special case loopback. */
508         if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
509             VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
510                          "connected to its own bond", slave->name);
511             continue;
512         }
513
514         if (slave->status == LACP_DEFAULTED) {
515             continue;
516         }
517
518         slave->attached = true;
519         slave_get_priority(slave, &pri);
520
521         if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
522             lead = slave;
523             lead_pri = pri;
524         }
525     }
526
527     lacp->negotiated = lead != NULL;
528
529     if (lead) {
530         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
531             if (lead->partner.key != slave->partner.key
532                 || !eth_addr_equals(lead->partner.sys_id,
533                                     slave->partner.sys_id)) {
534                 slave->attached = false;
535             }
536         }
537     }
538 }
539
540 static void
541 slave_destroy(struct slave *slave)
542 {
543     if (slave) {
544         struct lacp *lacp = slave->lacp;
545
546         lacp->update = true;
547         hmap_remove(&lacp->slaves, &slave->node);
548
549         if (lacp->key_slave == slave) {
550             struct hmap_node *slave_node = hmap_first(&lacp->slaves);
551
552             if (slave_node) {
553                 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
554             } else {
555                 lacp->key_slave = NULL;
556             }
557         }
558
559         free(slave->name);
560         free(slave);
561     }
562 }
563
564 static void
565 slave_set_defaulted(struct slave *slave)
566 {
567     memset(&slave->partner, 0, sizeof slave->partner);
568
569     slave->lacp->update = true;
570     slave->status = LACP_DEFAULTED;
571 }
572
573 static void
574 slave_set_expired(struct slave *slave)
575 {
576     slave->status = LACP_EXPIRED;
577     slave->partner.state |= LACP_STATE_TIME;
578     slave->partner.state &= ~LACP_STATE_SYNC;
579
580     timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX);
581 }
582
583 static void
584 slave_get_actor(struct slave *slave, struct lacp_info *actor)
585 {
586     struct lacp *lacp = slave->lacp;
587     uint16_t key;
588     uint8_t state = 0;
589
590     if (lacp->active) {
591         state |= LACP_STATE_ACT;
592     }
593
594     if (lacp->fast) {
595         state |= LACP_STATE_TIME;
596     }
597
598     if (slave->attached) {
599         state |= LACP_STATE_SYNC;
600     }
601
602     if (slave->status == LACP_DEFAULTED) {
603         state |= LACP_STATE_DEF;
604     }
605
606     if (slave->status == LACP_EXPIRED) {
607         state |= LACP_STATE_EXP;
608     }
609
610     if (hmap_count(&lacp->slaves) > 1) {
611         state |= LACP_STATE_AGG;
612     }
613
614     if (slave->attached || !lacp->negotiated) {
615         state |= LACP_STATE_COL | LACP_STATE_DIST;
616     }
617
618     key = lacp->key_slave->key;
619     if (!key) {
620         key = lacp->key_slave->port_id;
621     }
622
623     actor->state = state;
624     actor->key = htons(key);
625     actor->port_priority = htons(slave->port_priority);
626     actor->port_id = htons(slave->port_id);
627     actor->sys_priority = htons(lacp->sys_priority);
628     memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
629 }
630
631 /* Given 'slave', populates 'priority' with data representing its LACP link
632  * priority.  If two priority objects populated by this function are compared
633  * using memcmp, the higher priority link will be less than the lower priority
634  * link. */
635 static void
636 slave_get_priority(struct slave *slave, struct lacp_info *priority)
637 {
638     uint16_t partner_priority, actor_priority;
639
640     /* Choose the lacp_info of the higher priority system by comparing their
641      * system priorities and mac addresses. */
642     actor_priority = slave->lacp->sys_priority;
643     partner_priority = ntohs(slave->partner.sys_priority);
644     if (actor_priority < partner_priority) {
645         slave_get_actor(slave, priority);
646     } else if (partner_priority < actor_priority) {
647         *priority = slave->partner;
648     } else if (eth_addr_compare_3way(slave->lacp->sys_id,
649                                      slave->partner.sys_id) < 0) {
650         slave_get_actor(slave, priority);
651     } else {
652         *priority = slave->partner;
653     }
654
655     /* Key and state are not used in priority comparisons. */
656     priority->key = 0;
657     priority->state = 0;
658 }
659
660 static bool
661 slave_may_tx(const struct slave *slave)
662 {
663     return slave->lacp->active || slave->status != LACP_DEFAULTED;
664 }
665
666 static struct slave *
667 slave_lookup(const struct lacp *lacp, const void *slave_)
668 {
669     struct slave *slave;
670
671     HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
672                              &lacp->slaves) {
673         if (slave->aux == slave_) {
674             return slave;
675         }
676     }
677
678     return NULL;
679 }
680
681 /* Two lacp_info structures are tx_equal if and only if they do not differ in
682  * ways which would require a lacp_pdu transmission. */
683 static bool
684 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
685 {
686
687     /* LACP specification dictates that we transmit whenever the actor and
688      * remote_actor differ in the following fields: Port, Port Priority,
689      * System, System Priority, Aggregation Key, Activity State, Timeout State,
690      * Sync State, and Aggregation State. The state flags are most likely to
691      * change so are checked first. */
692     return !((a->state ^ b->state) & (LACP_STATE_ACT
693                                       | LACP_STATE_TIME
694                                       | LACP_STATE_SYNC
695                                       | LACP_STATE_AGG))
696         && a->port_id == b->port_id
697         && a->port_priority == b->port_priority
698         && a->key == b->key
699         && a->sys_priority == b->sys_priority
700         && eth_addr_equals(a->sys_id, b->sys_id);
701 }
702 \f
703 static struct lacp *
704 lacp_find(const char *name)
705 {
706     struct lacp *lacp;
707
708     LIST_FOR_EACH (lacp, node, &all_lacps) {
709         if (!strcmp(lacp->name, name)) {
710             return lacp;
711         }
712     }
713
714     return NULL;
715 }
716
717 static void
718 ds_put_lacp_state(struct ds *ds, uint8_t state)
719 {
720     if (state & LACP_STATE_ACT) {
721         ds_put_cstr(ds, " activity");
722     }
723
724     if (state & LACP_STATE_TIME) {
725         ds_put_cstr(ds, " timeout");
726     }
727
728     if (state & LACP_STATE_AGG) {
729         ds_put_cstr(ds, " aggregation");
730     }
731
732     if (state & LACP_STATE_SYNC) {
733         ds_put_cstr(ds, " synchronized");
734     }
735
736     if (state & LACP_STATE_COL) {
737         ds_put_cstr(ds, " collecting");
738     }
739
740     if (state & LACP_STATE_DIST) {
741         ds_put_cstr(ds, " distributing");
742     }
743
744     if (state & LACP_STATE_DEF) {
745         ds_put_cstr(ds, " defaulted");
746     }
747
748     if (state & LACP_STATE_EXP) {
749         ds_put_cstr(ds, " expired");
750     }
751 }
752
753 static void
754 lacp_print_details(struct ds *ds, struct lacp *lacp)
755 {
756     struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
757     const struct shash_node **sorted_slaves = NULL;
758
759     struct slave *slave;
760     int i;
761
762     ds_put_format(ds, "---- %s ----\n", lacp->name);
763     ds_put_format(ds, "\tstatus: %s", lacp->active ? "active" : "passive");
764     if (lacp->negotiated) {
765         ds_put_cstr(ds, " negotiated");
766     }
767     ds_put_cstr(ds, "\n");
768
769     ds_put_format(ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
770     ds_put_format(ds, "\tsys_priority: %u\n", lacp->sys_priority);
771     ds_put_cstr(ds, "\taggregation key: ");
772     if (lacp->key_slave) {
773         ds_put_format(ds, "%u", lacp->key_slave->key
774                                 ? lacp->key_slave->key
775                                 : lacp->key_slave->port_id);
776     } else {
777         ds_put_cstr(ds, "none");
778     }
779     ds_put_cstr(ds, "\n");
780
781     ds_put_cstr(ds, "\tlacp_time: ");
782     if (lacp->fast) {
783         ds_put_cstr(ds, "fast\n");
784     } else {
785         ds_put_cstr(ds, "slow\n");
786     }
787
788     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
789         shash_add(&slave_shash, slave->name, slave);
790     }
791     sorted_slaves = shash_sort(&slave_shash);
792
793     for (i = 0; i < shash_count(&slave_shash); i++) {
794         char *status;
795         struct lacp_info actor;
796
797         slave = sorted_slaves[i]->data;
798         slave_get_actor(slave, &actor);
799         switch (slave->status) {
800         case LACP_CURRENT:
801             status = "current";
802             break;
803         case LACP_EXPIRED:
804             status = "expired";
805             break;
806         case LACP_DEFAULTED:
807             status = "defaulted";
808             break;
809         default:
810             NOT_REACHED();
811         }
812
813         ds_put_format(ds, "\nslave: %s: %s %s\n", slave->name, status,
814                       slave->attached ? "attached" : "detached");
815         ds_put_format(ds, "\tport_id: %u\n", slave->port_id);
816         ds_put_format(ds, "\tport_priority: %u\n", slave->port_priority);
817         ds_put_format(ds, "\tmay_enable: %s\n", (slave_may_enable__(slave)
818                                                  ? "true" : "false"));
819
820         ds_put_format(ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
821                       ETH_ADDR_ARGS(actor.sys_id));
822         ds_put_format(ds, "\tactor sys_priority: %u\n",
823                       ntohs(actor.sys_priority));
824         ds_put_format(ds, "\tactor port_id: %u\n",
825                       ntohs(actor.port_id));
826         ds_put_format(ds, "\tactor port_priority: %u\n",
827                       ntohs(actor.port_priority));
828         ds_put_format(ds, "\tactor key: %u\n",
829                       ntohs(actor.key));
830         ds_put_cstr(ds, "\tactor state:");
831         ds_put_lacp_state(ds, actor.state);
832         ds_put_cstr(ds, "\n\n");
833
834         ds_put_format(ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
835                       ETH_ADDR_ARGS(slave->partner.sys_id));
836         ds_put_format(ds, "\tpartner sys_priority: %u\n",
837                       ntohs(slave->partner.sys_priority));
838         ds_put_format(ds, "\tpartner port_id: %u\n",
839                       ntohs(slave->partner.port_id));
840         ds_put_format(ds, "\tpartner port_priority: %u\n",
841                       ntohs(slave->partner.port_priority));
842         ds_put_format(ds, "\tpartner key: %u\n",
843                       ntohs(slave->partner.key));
844         ds_put_cstr(ds, "\tpartner state:");
845         ds_put_lacp_state(ds, slave->partner.state);
846         ds_put_cstr(ds, "\n");
847     }
848
849     shash_destroy(&slave_shash);
850     free(sorted_slaves);
851 }
852
853 static void
854 lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
855                   void *aux OVS_UNUSED)
856 {
857     struct ds ds = DS_EMPTY_INITIALIZER;
858     struct lacp *lacp;
859
860     if (argc > 1) {
861         lacp = lacp_find(argv[1]);
862         if (!lacp) {
863             unixctl_command_reply_error(conn, "no such lacp object");
864             return;
865         }
866         lacp_print_details(&ds, lacp);
867     } else {
868         LIST_FOR_EACH (lacp, node, &all_lacps) {
869             lacp_print_details(&ds, lacp);
870         }
871     }
872
873     unixctl_command_reply(conn, ds_cstr(&ds));
874     ds_destroy(&ds);
875 }