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