1 /* Copyright (c) 2011 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "dynamic-string.h"
27 #include "poll-loop.h"
33 VLOG_DEFINE_THIS_MODULE(lacp);
36 LACP_CURRENT, /* Current State. Partner up to date. */
37 LACP_EXPIRED, /* Expired State. Partner out of date. */
38 LACP_DEFAULTED, /* Defaulted State. No partner. */
42 struct list node; /* Node in all_lacps list. */
43 char *name; /* Name of this lacp object. */
44 uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
45 uint16_t sys_priority; /* System Priority. */
46 bool active; /* Active or Passive. */
48 struct hmap slaves; /* Slaves this LACP object controls. */
49 struct slave *key_slave; /* Slave whose ID will be the aggregation key. */
51 enum lacp_time lacp_time; /* Fast, Slow or Custom LACP time. */
52 long long int custom_time; /* LACP_TIME_CUSTOM transmission rate. */
53 bool strict; /* True if in strict mode. */
54 bool negotiated; /* True if LACP negotiations were successful. */
55 bool update; /* True if lacp_update() needs to be called. */
59 void *aux; /* Handle used to identify this slave. */
60 struct hmap_node node; /* Node in master's slaves map. */
62 struct lacp *lacp; /* LACP object containing this slave. */
63 uint16_t port_id; /* Port ID. */
64 uint16_t port_priority; /* Port Priority. */
65 char *name; /* Name of this slave. */
67 enum slave_status status; /* Slave status. */
68 bool attached; /* Attached. Traffic may flow. */
69 struct lacp_info partner; /* Partner information. */
70 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
71 struct timer tx; /* Next message transmission timer. */
72 struct timer rx; /* Expected message receive timer. */
75 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
77 static void lacp_update_attached(struct lacp *);
79 static void slave_destroy(struct slave *);
80 static void slave_set_defaulted(struct slave *);
81 static void slave_set_expired(struct slave *);
82 static void slave_get_actor(struct slave *, struct lacp_info *actor);
83 static void slave_get_priority(struct slave *, struct lacp_info *priority);
84 static bool slave_may_tx(const struct slave *);
85 static struct slave *slave_lookup(const struct lacp *, const void *slave);
86 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
88 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
91 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
93 compose_lacp_pdu(const struct lacp_info *actor,
94 const struct lacp_info *partner, struct lacp_pdu *pdu)
96 memset(pdu, 0, sizeof *pdu);
105 pdu->partner_type = 2;
106 pdu->partner_len = 20;
107 pdu->partner = *partner;
109 pdu->collector_type = 3;
110 pdu->collector_len = 16;
111 pdu->collector_delay = htons(0);
114 /* Parses 'b' which represents a packet containing a LACP PDU. This function
115 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
116 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
118 const struct lacp_pdu *
119 parse_lacp_packet(const struct ofpbuf *b)
121 const struct lacp_pdu *pdu;
123 pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
125 if (pdu && pdu->subtype == 1
126 && pdu->actor_type == 1 && pdu->actor_len == 20
127 && pdu->partner_type == 2 && pdu->partner_len == 20) {
134 /* LACP Protocol Implementation. */
136 /* Initializes the lacp module. */
140 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
143 /* Creates a LACP object. */
149 lacp = xzalloc(sizeof *lacp);
150 hmap_init(&lacp->slaves);
151 list_push_back(&all_lacps, &lacp->node);
155 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
157 lacp_destroy(struct lacp *lacp)
160 struct slave *slave, *next;
162 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
163 slave_destroy(slave);
166 hmap_destroy(&lacp->slaves);
167 list_remove(&lacp->node);
173 /* Configures 'lacp' with settings from 's'. */
175 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
177 if (!lacp->name || strcmp(s->name, lacp->name)) {
179 lacp->name = xstrdup(s->name);
182 if (!eth_addr_equals(lacp->sys_id, s->id)
183 || lacp->sys_priority != s->priority
184 || lacp->strict != s->strict) {
185 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
186 lacp->sys_priority = s->priority;
187 lacp->strict = s->strict;
191 lacp->active = s->active;
192 lacp->lacp_time = s->lacp_time;
193 lacp->custom_time = MAX(TIME_UPDATE_INTERVAL, s->custom_time);
196 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
197 * configured for passive mode. */
199 lacp_is_active(const struct lacp *lacp)
204 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
205 * should be called on all packets received on 'slave_' with Ethernet Type
206 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
208 lacp_process_pdu(struct lacp *lacp, const void *slave_,
209 const struct lacp_pdu *pdu)
211 struct slave *slave = slave_lookup(lacp, slave_);
212 long long int tx_rate;
214 switch (lacp->lacp_time) {
216 tx_rate = LACP_FAST_TIME_TX;
219 tx_rate = LACP_SLOW_TIME_TX;
221 case LACP_TIME_CUSTOM:
222 tx_rate = lacp->custom_time;
224 default: NOT_REACHED();
227 slave->status = LACP_CURRENT;
228 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
230 slave->ntt_actor = pdu->partner;
232 /* Update our information about our partner if it's out of date. This may
233 * cause priorities to change so re-calculate attached status of all
235 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
237 slave->partner = pdu->actor;
241 /* Returns true if 'lacp' has successfully negotiated with its partner. False
242 * if 'lacp' is NULL. */
244 lacp_negotiated(const struct lacp *lacp)
246 return lacp ? lacp->negotiated : false;
249 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
250 * once per slave in a LACP managed bond. Should also be called whenever a
251 * slave's settings change. */
253 lacp_slave_register(struct lacp *lacp, void *slave_,
254 const struct lacp_slave_settings *s)
256 struct slave *slave = slave_lookup(lacp, slave_);
259 slave = xzalloc(sizeof *slave);
262 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
263 slave_set_defaulted(slave);
265 if (!lacp->key_slave) {
266 lacp->key_slave = slave;
270 if (!slave->name || strcmp(s->name, slave->name)) {
272 slave->name = xstrdup(s->name);
275 if (slave->port_id != s->id || slave->port_priority != s->priority) {
276 slave->port_id = s->id;
277 slave->port_priority = s->priority;
281 if (lacp->active || lacp->negotiated) {
282 slave_set_expired(slave);
287 /* Unregisters 'slave_' with 'lacp'. */
289 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
291 struct slave *slave = slave_lookup(lacp, slave_);
294 slave_destroy(slave);
299 /* This function should be called whenever the carrier status of 'slave_' has
302 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
304 struct slave *slave = slave_lookup(lacp, slave_);
306 if (slave->status == LACP_CURRENT || slave->lacp->active) {
307 slave_set_expired(slave);
311 /* This function should be called before enabling 'slave_' to send or receive
312 * traffic. If it returns false, 'slave_' should not enabled. As a
313 * convenience, returns true if 'lacp' is NULL. */
315 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
318 struct slave *slave = slave_lookup(lacp, slave_);
320 /* The slave may be enabled if it's attached to an aggregator and its
321 * partner is synchronized. The only exception is defaulted slaves.
322 * They are not required to have synchronized partners because they
323 * have no partners at all. They will only be attached if negotiations
324 * failed on all slaves in the bond. */
325 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
326 || slave->status == LACP_DEFAULTED);
332 /* Returns the port ID used for 'slave_' in LACP communications. */
334 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
336 struct slave *slave = slave_lookup(lacp, slave_);
337 return slave->port_id;
340 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
341 * not being current, generally indicates a connectivity problem, or a
342 * misconfigured (or broken) partner. */
344 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
346 return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
349 /* This function should be called periodically to update 'lacp'. */
351 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
355 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
356 if (timer_expired(&slave->rx)) {
357 if (slave->status == LACP_CURRENT) {
358 slave_set_expired(slave);
359 } else if (slave->status == LACP_EXPIRED) {
360 slave_set_defaulted(slave);
366 lacp_update_attached(lacp);
369 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
371 struct lacp_info actor;
373 if (!slave_may_tx(slave)) {
377 slave_get_actor(slave, &actor);
379 if (timer_expired(&slave->tx)
380 || !info_tx_equal(&actor, &slave->ntt_actor)) {
381 long long int duration;
383 slave->ntt_actor = actor;
384 compose_lacp_pdu(&actor, &slave->partner, &pdu);
385 send_pdu(slave->aux, &pdu);
387 if (lacp->lacp_time == LACP_TIME_CUSTOM) {
388 duration = lacp->custom_time;
390 duration = (slave->partner.state & LACP_STATE_TIME
392 : LACP_SLOW_TIME_TX);
395 timer_set_duration(&slave->tx, duration);
400 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
402 lacp_wait(struct lacp *lacp)
406 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
407 if (slave_may_tx(slave)) {
408 timer_wait(&slave->tx);
411 if (slave->status != LACP_DEFAULTED) {
412 timer_wait(&slave->rx);
417 /* Static Helpers. */
419 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
420 * negotiated parameter to true if any slaves are attachable. */
422 lacp_update_attached(struct lacp *lacp)
424 struct slave *lead, *slave;
425 struct lacp_info lead_pri;
426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
428 lacp->update = false;
431 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
432 struct lacp_info pri;
434 slave->attached = true;
436 /* XXX: In the future allow users to configure the expected system ID.
437 * For now just special case loopback. */
438 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
439 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
440 "connected to its own bond", slave->name);
441 slave->attached = false;
445 if (slave->status == LACP_DEFAULTED) {
449 slave_get_priority(slave, &pri);
451 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
457 lacp->negotiated = lead != NULL;
460 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
461 if (slave->status == LACP_DEFAULTED
462 || lead->partner.key != slave->partner.key
463 || !eth_addr_equals(lead->partner.sys_id,
464 slave->partner.sys_id)) {
465 slave->attached = false;
468 } else if (lacp->strict) {
469 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
470 slave->attached = false;
476 slave_destroy(struct slave *slave)
479 struct lacp *lacp = slave->lacp;
482 hmap_remove(&lacp->slaves, &slave->node);
484 if (lacp->key_slave == slave) {
485 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
488 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
490 lacp->key_slave = NULL;
500 slave_set_defaulted(struct slave *slave)
502 memset(&slave->partner, 0, sizeof slave->partner);
504 slave->lacp->update = true;
505 slave->status = LACP_DEFAULTED;
509 slave_set_expired(struct slave *slave)
511 struct lacp *lacp = slave->lacp;
513 slave->status = LACP_EXPIRED;
514 slave->partner.state |= LACP_STATE_TIME;
515 slave->partner.state &= ~LACP_STATE_SYNC;
517 /* The spec says we should wait LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX.
518 * This doesn't make sense when using custom times which can be much
519 * smaller than LACP_FAST_TIME. */
520 timer_set_duration(&slave->rx, (lacp->lacp_time == LACP_TIME_CUSTOM
522 : LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX));
526 slave_get_actor(struct slave *slave, struct lacp_info *actor)
530 if (slave->lacp->active) {
531 state |= LACP_STATE_ACT;
534 if (slave->lacp->lacp_time != LACP_TIME_SLOW) {
535 state |= LACP_STATE_TIME;
538 if (slave->attached) {
539 state |= LACP_STATE_SYNC;
542 if (slave->status == LACP_DEFAULTED) {
543 state |= LACP_STATE_DEF;
546 if (slave->status == LACP_EXPIRED) {
547 state |= LACP_STATE_EXP;
550 if (hmap_count(&slave->lacp->slaves) > 1) {
551 state |= LACP_STATE_AGG;
554 if (slave->attached || !slave->lacp->negotiated) {
555 state |= LACP_STATE_COL | LACP_STATE_DIST;
558 actor->state = state;
559 actor->key = htons(slave->lacp->key_slave->port_id);
560 actor->port_priority = htons(slave->port_priority);
561 actor->port_id = htons(slave->port_id);
562 actor->sys_priority = htons(slave->lacp->sys_priority);
563 memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
566 /* Given 'slave', populates 'priority' with data representing its LACP link
567 * priority. If two priority objects populated by this function are compared
568 * using memcmp, the higher priority link will be less than the lower priority
571 slave_get_priority(struct slave *slave, struct lacp_info *priority)
573 uint16_t partner_priority, actor_priority;
575 /* Choose the lacp_info of the higher priority system by comparing their
576 * system priorities and mac addresses. */
577 actor_priority = slave->lacp->sys_priority;
578 partner_priority = ntohs(slave->partner.sys_priority);
579 if (actor_priority < partner_priority) {
580 slave_get_actor(slave, priority);
581 } else if (partner_priority < actor_priority) {
582 *priority = slave->partner;
583 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
584 slave->partner.sys_id) < 0) {
585 slave_get_actor(slave, priority);
587 *priority = slave->partner;
590 /* Key and state are not used in priority comparisons. */
596 slave_may_tx(const struct slave *slave)
598 return slave->lacp->active || slave->status != LACP_DEFAULTED;
601 static struct slave *
602 slave_lookup(const struct lacp *lacp, const void *slave_)
606 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
608 if (slave->aux == slave_) {
616 /* Two lacp_info structures are tx_equal if and only if they do not differ in
617 * ways which would require a lacp_pdu transmission. */
619 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
622 /* LACP specification dictates that we transmit whenever the actor and
623 * remote_actor differ in the following fields: Port, Port Priority,
624 * System, System Priority, Aggregation Key, Activity State, Timeout State,
625 * Sync State, and Aggregation State. The state flags are most likely to
626 * change so are checked first. */
627 return !((a->state ^ b->state) & (LACP_STATE_ACT
631 && a->port_id == b->port_id
632 && a->port_priority == b->port_priority
634 && a->sys_priority == b->sys_priority
635 && eth_addr_equals(a->sys_id, b->sys_id);
639 lacp_find(const char *name)
643 LIST_FOR_EACH (lacp, node, &all_lacps) {
644 if (!strcmp(lacp->name, name)) {
653 ds_put_lacp_state(struct ds *ds, uint8_t state)
655 if (state & LACP_STATE_ACT) {
656 ds_put_cstr(ds, "activity ");
659 if (state & LACP_STATE_TIME) {
660 ds_put_cstr(ds, "timeout ");
663 if (state & LACP_STATE_AGG) {
664 ds_put_cstr(ds, "aggregation ");
667 if (state & LACP_STATE_SYNC) {
668 ds_put_cstr(ds, "synchronized ");
671 if (state & LACP_STATE_COL) {
672 ds_put_cstr(ds, "collecting ");
675 if (state & LACP_STATE_DIST) {
676 ds_put_cstr(ds, "distributing ");
679 if (state & LACP_STATE_DEF) {
680 ds_put_cstr(ds, "defaulted ");
683 if (state & LACP_STATE_EXP) {
684 ds_put_cstr(ds, "expired ");
689 lacp_unixctl_show(struct unixctl_conn *conn,
690 const char *args, void *aux OVS_UNUSED)
692 struct ds ds = DS_EMPTY_INITIALIZER;
696 lacp = lacp_find(args);
698 unixctl_command_reply(conn, 501, "no such lacp object");
702 ds_put_format(&ds, "lacp: %s\n", lacp->name);
704 ds_put_format(&ds, "\tstatus: %s", lacp->active ? "active" : "passive");
706 ds_put_cstr(&ds, " strict");
708 if (lacp->negotiated) {
709 ds_put_cstr(&ds, " negotiated");
711 ds_put_cstr(&ds, "\n");
713 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
714 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
715 ds_put_cstr(&ds, "\taggregation key: ");
716 if (lacp->key_slave) {
717 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
719 ds_put_cstr(&ds, "none");
721 ds_put_cstr(&ds, "\n");
723 ds_put_cstr(&ds, "\tlacp_time: ");
724 switch (lacp->lacp_time) {
726 ds_put_cstr(&ds, "fast\n");
729 ds_put_cstr(&ds, "slow\n");
731 case LACP_TIME_CUSTOM:
732 ds_put_format(&ds, "custom (%lld)\n", lacp->custom_time);
735 ds_put_cstr(&ds, "unknown\n");
738 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
740 struct lacp_info actor;
742 slave_get_actor(slave, &actor);
743 switch (slave->status) {
751 status = "defaulted";
757 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
758 slave->attached ? "attached" : "detached");
759 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
760 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
762 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
763 ETH_ADDR_ARGS(actor.sys_id));
764 ds_put_format(&ds, "\tactor sys_priority: %u\n",
765 ntohs(actor.sys_priority));
766 ds_put_format(&ds, "\tactor port_id: %u\n",
767 ntohs(actor.port_id));
768 ds_put_format(&ds, "\tactor port_priority: %u\n",
769 ntohs(actor.port_priority));
770 ds_put_format(&ds, "\tactor key: %u\n",
772 ds_put_cstr(&ds, "\tactor state: ");
773 ds_put_lacp_state(&ds, actor.state);
774 ds_put_cstr(&ds, "\n\n");
776 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
777 ETH_ADDR_ARGS(slave->partner.sys_id));
778 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
779 ntohs(slave->partner.sys_priority));
780 ds_put_format(&ds, "\tpartner port_id: %u\n",
781 ntohs(slave->partner.port_id));
782 ds_put_format(&ds, "\tpartner port_priority: %u\n",
783 ntohs(slave->partner.port_priority));
784 ds_put_format(&ds, "\tpartner key: %u\n",
785 ntohs(slave->partner.key));
786 ds_put_cstr(&ds, "\tpartner state: ");
787 ds_put_lacp_state(&ds, slave->partner.state);
788 ds_put_cstr(&ds, "\n");
791 unixctl_command_reply(conn, 200, ds_cstr(&ds));