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