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