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