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