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