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