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