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