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 bool fast; /* Fast or Slow LACP time. */
52 bool strict; /* True if in strict mode. */
53 bool negotiated; /* True if LACP negotiations were successful. */
54 bool update; /* True if lacp_update() needs to be called. */
58 void *aux; /* Handle used to identify this slave. */
59 struct hmap_node node; /* Node in master's slaves map. */
61 struct lacp *lacp; /* LACP object containing this slave. */
62 uint16_t port_id; /* Port ID. */
63 uint16_t port_priority; /* Port Priority. */
64 char *name; /* Name of this slave. */
66 enum slave_status status; /* Slave status. */
67 bool attached; /* Attached. Traffic may flow. */
68 struct lacp_info partner; /* Partner information. */
69 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
70 struct timer tx; /* Next message transmission timer. */
71 struct timer rx; /* Expected message receive timer. */
74 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
76 static void lacp_update_attached(struct lacp *);
78 static void slave_destroy(struct slave *);
79 static void slave_set_defaulted(struct slave *);
80 static void slave_set_expired(struct slave *);
81 static void slave_get_actor(struct slave *, struct lacp_info *actor);
82 static void slave_get_priority(struct slave *, struct lacp_info *priority);
83 static bool slave_may_tx(const struct slave *);
84 static struct slave *slave_lookup(const struct lacp *, const void *slave);
85 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
87 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
90 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
92 compose_lacp_pdu(const struct lacp_info *actor,
93 const struct lacp_info *partner, struct lacp_pdu *pdu)
95 memset(pdu, 0, sizeof *pdu);
104 pdu->partner_type = 2;
105 pdu->partner_len = 20;
106 pdu->partner = *partner;
108 pdu->collector_type = 3;
109 pdu->collector_len = 16;
110 pdu->collector_delay = htons(0);
113 /* Parses 'b' which represents a packet containing a LACP PDU. This function
114 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
115 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
117 const struct lacp_pdu *
118 parse_lacp_packet(const struct ofpbuf *b)
120 const struct lacp_pdu *pdu;
122 pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
124 if (pdu && pdu->subtype == 1
125 && pdu->actor_type == 1 && pdu->actor_len == 20
126 && pdu->partner_type == 2 && pdu->partner_len == 20) {
133 /* LACP Protocol Implementation. */
135 /* Initializes the lacp module. */
139 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
142 /* Creates a LACP object. */
148 lacp = xzalloc(sizeof *lacp);
149 hmap_init(&lacp->slaves);
150 list_push_back(&all_lacps, &lacp->node);
154 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
156 lacp_destroy(struct lacp *lacp)
159 struct slave *slave, *next;
161 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
162 slave_destroy(slave);
165 hmap_destroy(&lacp->slaves);
166 list_remove(&lacp->node);
172 /* Configures 'lacp' with settings from 's'. */
174 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
176 if (!lacp->name || strcmp(s->name, lacp->name)) {
178 lacp->name = xstrdup(s->name);
181 if (!eth_addr_equals(lacp->sys_id, s->id)
182 || lacp->sys_priority != s->priority
183 || lacp->strict != s->strict) {
184 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
185 lacp->sys_priority = s->priority;
186 lacp->strict = s->strict;
190 lacp->active = s->active;
191 lacp->fast = s->fast;
194 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
195 * configured for passive mode. */
197 lacp_is_active(const struct lacp *lacp)
202 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
203 * should be called on all packets received on 'slave_' with Ethernet Type
204 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
206 lacp_process_pdu(struct lacp *lacp, const void *slave_,
207 const struct lacp_pdu *pdu)
209 struct slave *slave = slave_lookup(lacp, slave_);
211 slave->status = LACP_CURRENT;
212 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER *
213 (lacp->fast ? LACP_FAST_TIME_TX : LACP_SLOW_TIME_TX));
215 slave->ntt_actor = pdu->partner;
217 /* Update our information about our partner if it's out of date. This may
218 * cause priorities to change so re-calculate attached status of all
220 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
222 slave->partner = pdu->actor;
226 /* Returns true if 'lacp' has successfully negotiated with its partner. False
227 * if 'lacp' is NULL. */
229 lacp_negotiated(const struct lacp *lacp)
231 return lacp ? lacp->negotiated : false;
234 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
235 * once per slave in a LACP managed bond. Should also be called whenever a
236 * slave's settings change. */
238 lacp_slave_register(struct lacp *lacp, void *slave_,
239 const struct lacp_slave_settings *s)
241 struct slave *slave = slave_lookup(lacp, slave_);
244 slave = xzalloc(sizeof *slave);
247 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
248 slave_set_defaulted(slave);
250 if (!lacp->key_slave) {
251 lacp->key_slave = slave;
255 if (!slave->name || strcmp(s->name, slave->name)) {
257 slave->name = xstrdup(s->name);
260 if (slave->port_id != s->id || slave->port_priority != s->priority) {
261 slave->port_id = s->id;
262 slave->port_priority = s->priority;
266 if (lacp->active || lacp->negotiated) {
267 slave_set_expired(slave);
272 /* Unregisters 'slave_' with 'lacp'. */
274 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
276 struct slave *slave = slave_lookup(lacp, slave_);
279 slave_destroy(slave);
284 /* This function should be called whenever the carrier status of 'slave_' has
287 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
289 struct slave *slave = slave_lookup(lacp, slave_);
291 if (slave->status == LACP_CURRENT || slave->lacp->active) {
292 slave_set_expired(slave);
296 /* This function should be called before enabling 'slave_' to send or receive
297 * traffic. If it returns false, 'slave_' should not enabled. As a
298 * convenience, returns true if 'lacp' is NULL. */
300 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
303 struct slave *slave = slave_lookup(lacp, slave_);
305 /* The slave may be enabled if it's attached to an aggregator and its
306 * partner is synchronized. The only exception is defaulted slaves.
307 * They are not required to have synchronized partners because they
308 * have no partners at all. They will only be attached if negotiations
309 * failed on all slaves in the bond. */
310 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
311 || slave->status == LACP_DEFAULTED);
317 /* Returns the port ID used for 'slave_' in LACP communications. */
319 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
321 struct slave *slave = slave_lookup(lacp, slave_);
322 return slave->port_id;
325 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
326 * not being current, generally indicates a connectivity problem, or a
327 * misconfigured (or broken) partner. */
329 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
331 return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
334 /* This function should be called periodically to update 'lacp'. */
336 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
340 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
341 if (timer_expired(&slave->rx)) {
342 if (slave->status == LACP_CURRENT) {
343 slave_set_expired(slave);
344 } else if (slave->status == LACP_EXPIRED) {
345 slave_set_defaulted(slave);
351 lacp_update_attached(lacp);
354 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
356 struct lacp_info actor;
358 if (!slave_may_tx(slave)) {
362 slave_get_actor(slave, &actor);
364 if (timer_expired(&slave->tx)
365 || !info_tx_equal(&actor, &slave->ntt_actor)) {
367 slave->ntt_actor = actor;
368 compose_lacp_pdu(&actor, &slave->partner, &pdu);
369 send_pdu(slave->aux, &pdu);
371 timer_set_duration(&slave->tx,
372 (slave->partner.state & LACP_STATE_TIME
374 : LACP_SLOW_TIME_TX));
379 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
381 lacp_wait(struct lacp *lacp)
385 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
386 if (slave_may_tx(slave)) {
387 timer_wait(&slave->tx);
390 if (slave->status != LACP_DEFAULTED) {
391 timer_wait(&slave->rx);
396 /* Static Helpers. */
398 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
399 * negotiated parameter to true if any slaves are attachable. */
401 lacp_update_attached(struct lacp *lacp)
403 struct slave *lead, *slave;
404 struct lacp_info lead_pri;
405 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
407 lacp->update = false;
410 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
411 struct lacp_info pri;
413 slave->attached = true;
415 /* XXX: In the future allow users to configure the expected system ID.
416 * For now just special case loopback. */
417 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
418 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
419 "connected to its own bond", slave->name);
420 slave->attached = false;
424 if (slave->status == LACP_DEFAULTED) {
428 slave_get_priority(slave, &pri);
430 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
436 lacp->negotiated = lead != NULL;
439 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
440 if (slave->status == LACP_DEFAULTED
441 || lead->partner.key != slave->partner.key
442 || !eth_addr_equals(lead->partner.sys_id,
443 slave->partner.sys_id)) {
444 slave->attached = false;
447 } else if (lacp->strict) {
448 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
449 slave->attached = false;
455 slave_destroy(struct slave *slave)
458 struct lacp *lacp = slave->lacp;
461 hmap_remove(&lacp->slaves, &slave->node);
463 if (lacp->key_slave == slave) {
464 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
467 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
469 lacp->key_slave = NULL;
479 slave_set_defaulted(struct slave *slave)
481 memset(&slave->partner, 0, sizeof slave->partner);
483 slave->lacp->update = true;
484 slave->status = LACP_DEFAULTED;
488 slave_set_expired(struct slave *slave)
490 slave->status = LACP_EXPIRED;
491 slave->partner.state |= LACP_STATE_TIME;
492 slave->partner.state &= ~LACP_STATE_SYNC;
493 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX);
497 slave_get_actor(struct slave *slave, struct lacp_info *actor)
501 if (slave->lacp->active) {
502 state |= LACP_STATE_ACT;
505 if (slave->lacp->fast) {
506 state |= LACP_STATE_TIME;
509 if (slave->attached) {
510 state |= LACP_STATE_SYNC;
513 if (slave->status == LACP_DEFAULTED) {
514 state |= LACP_STATE_DEF;
517 if (slave->status == LACP_EXPIRED) {
518 state |= LACP_STATE_EXP;
521 if (hmap_count(&slave->lacp->slaves) > 1) {
522 state |= LACP_STATE_AGG;
525 if (slave->attached || !slave->lacp->negotiated) {
526 state |= LACP_STATE_COL | LACP_STATE_DIST;
529 actor->state = state;
530 actor->key = htons(slave->lacp->key_slave->port_id);
531 actor->port_priority = htons(slave->port_priority);
532 actor->port_id = htons(slave->port_id);
533 actor->sys_priority = htons(slave->lacp->sys_priority);
534 memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
537 /* Given 'slave', populates 'priority' with data representing its LACP link
538 * priority. If two priority objects populated by this function are compared
539 * using memcmp, the higher priority link will be less than the lower priority
542 slave_get_priority(struct slave *slave, struct lacp_info *priority)
544 uint16_t partner_priority, actor_priority;
546 /* Choose the lacp_info of the higher priority system by comparing their
547 * system priorities and mac addresses. */
548 actor_priority = slave->lacp->sys_priority;
549 partner_priority = ntohs(slave->partner.sys_priority);
550 if (actor_priority < partner_priority) {
551 slave_get_actor(slave, priority);
552 } else if (partner_priority < actor_priority) {
553 *priority = slave->partner;
554 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
555 slave->partner.sys_id) < 0) {
556 slave_get_actor(slave, priority);
558 *priority = slave->partner;
561 /* Key and state are not used in priority comparisons. */
567 slave_may_tx(const struct slave *slave)
569 return slave->lacp->active || slave->status != LACP_DEFAULTED;
572 static struct slave *
573 slave_lookup(const struct lacp *lacp, const void *slave_)
577 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
579 if (slave->aux == slave_) {
587 /* Two lacp_info structures are tx_equal if and only if they do not differ in
588 * ways which would require a lacp_pdu transmission. */
590 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
593 /* LACP specification dictates that we transmit whenever the actor and
594 * remote_actor differ in the following fields: Port, Port Priority,
595 * System, System Priority, Aggregation Key, Activity State, Timeout State,
596 * Sync State, and Aggregation State. The state flags are most likely to
597 * change so are checked first. */
598 return !((a->state ^ b->state) & (LACP_STATE_ACT
602 && a->port_id == b->port_id
603 && a->port_priority == b->port_priority
605 && a->sys_priority == b->sys_priority
606 && eth_addr_equals(a->sys_id, b->sys_id);
610 lacp_find(const char *name)
614 LIST_FOR_EACH (lacp, node, &all_lacps) {
615 if (!strcmp(lacp->name, name)) {
624 ds_put_lacp_state(struct ds *ds, uint8_t state)
626 if (state & LACP_STATE_ACT) {
627 ds_put_cstr(ds, "activity ");
630 if (state & LACP_STATE_TIME) {
631 ds_put_cstr(ds, "timeout ");
634 if (state & LACP_STATE_AGG) {
635 ds_put_cstr(ds, "aggregation ");
638 if (state & LACP_STATE_SYNC) {
639 ds_put_cstr(ds, "synchronized ");
642 if (state & LACP_STATE_COL) {
643 ds_put_cstr(ds, "collecting ");
646 if (state & LACP_STATE_DIST) {
647 ds_put_cstr(ds, "distributing ");
650 if (state & LACP_STATE_DEF) {
651 ds_put_cstr(ds, "defaulted ");
654 if (state & LACP_STATE_EXP) {
655 ds_put_cstr(ds, "expired ");
660 lacp_unixctl_show(struct unixctl_conn *conn,
661 const char *args, void *aux OVS_UNUSED)
663 struct ds ds = DS_EMPTY_INITIALIZER;
667 lacp = lacp_find(args);
669 unixctl_command_reply(conn, 501, "no such lacp object");
673 ds_put_format(&ds, "lacp: %s\n", lacp->name);
675 ds_put_format(&ds, "\tstatus: %s", lacp->active ? "active" : "passive");
677 ds_put_cstr(&ds, " strict");
679 if (lacp->negotiated) {
680 ds_put_cstr(&ds, " negotiated");
682 ds_put_cstr(&ds, "\n");
684 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
685 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
686 ds_put_cstr(&ds, "\taggregation key: ");
687 if (lacp->key_slave) {
688 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
690 ds_put_cstr(&ds, "none");
692 ds_put_cstr(&ds, "\n");
694 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
696 struct lacp_info actor;
698 slave_get_actor(slave, &actor);
699 switch (slave->status) {
707 status = "defaulted";
713 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
714 slave->attached ? "attached" : "detached");
715 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
716 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
718 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
719 ETH_ADDR_ARGS(actor.sys_id));
720 ds_put_format(&ds, "\tactor sys_priority: %u\n",
721 ntohs(actor.sys_priority));
722 ds_put_format(&ds, "\tactor port_id: %u\n",
723 ntohs(actor.port_id));
724 ds_put_format(&ds, "\tactor port_priority: %u\n",
725 ntohs(actor.port_priority));
726 ds_put_format(&ds, "\tactor key: %u\n",
728 ds_put_cstr(&ds, "\tactor state: ");
729 ds_put_lacp_state(&ds, actor.state);
730 ds_put_cstr(&ds, "\n\n");
732 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
733 ETH_ADDR_ARGS(slave->partner.sys_id));
734 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
735 ntohs(slave->partner.sys_priority));
736 ds_put_format(&ds, "\tpartner port_id: %u\n",
737 ntohs(slave->partner.port_id));
738 ds_put_format(&ds, "\tpartner port_priority: %u\n",
739 ntohs(slave->partner.port_priority));
740 ds_put_format(&ds, "\tpartner key: %u\n",
741 ntohs(slave->partner.key));
742 ds_put_cstr(&ds, "\tpartner state: ");
743 ds_put_lacp_state(&ds, slave->partner.state);
744 ds_put_cstr(&ds, "\n");
747 unixctl_command_reply(conn, 200, ds_cstr(&ds));