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