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