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->fast
214 : LACP_SLOW_TIME_RX));
216 slave->ntt_actor = pdu->partner;
218 /* Update our information about our partner if it's out of date. This may
219 * cause priorities to change so re-calculate attached status of all
221 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
223 slave->partner = pdu->actor;
227 /* Returns true if 'lacp' has successfully negotiated with its partner. False
228 * if 'lacp' is NULL. */
230 lacp_negotiated(const struct lacp *lacp)
232 return lacp ? lacp->negotiated : false;
235 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
236 * once per slave in a LACP managed bond. Should also be called whenever a
237 * slave's settings change. */
239 lacp_slave_register(struct lacp *lacp, void *slave_,
240 const struct lacp_slave_settings *s)
242 struct slave *slave = slave_lookup(lacp, slave_);
245 slave = xzalloc(sizeof *slave);
248 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
249 slave_set_defaulted(slave);
251 if (!lacp->key_slave) {
252 lacp->key_slave = slave;
256 if (!slave->name || strcmp(s->name, slave->name)) {
258 slave->name = xstrdup(s->name);
261 if (slave->port_id != s->id || slave->port_priority != s->priority) {
262 slave->port_id = s->id;
263 slave->port_priority = s->priority;
267 if (lacp->active || lacp->negotiated) {
268 slave_set_expired(slave);
273 /* Unregisters 'slave_' with 'lacp'. */
275 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
277 struct slave *slave = slave_lookup(lacp, slave_);
280 slave_destroy(slave);
285 /* This function should be called whenever the carrier status of 'slave_' has
288 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
290 struct slave *slave = slave_lookup(lacp, slave_);
292 if (slave->status == LACP_CURRENT || slave->lacp->active) {
293 slave_set_expired(slave);
297 /* This function should be called before enabling 'slave_' to send or receive
298 * traffic. If it returns false, 'slave_' should not enabled. As a
299 * convenience, returns true if 'lacp' is NULL. */
301 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
304 struct slave *slave = slave_lookup(lacp, slave_);
306 /* The slave may be enabled if it's attached to an aggregator and its
307 * partner is synchronized. The only exception is defaulted slaves.
308 * They are not required to have synchronized partners because they
309 * have no partners at all. They will only be attached if negotiations
310 * failed on all slaves in the bond. */
311 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
312 || slave->status == LACP_DEFAULTED);
318 /* Returns the port ID used for 'slave_' in LACP communications. */
320 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
322 struct slave *slave = slave_lookup(lacp, slave_);
323 return slave->port_id;
326 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
327 * not being current, generally indicates a connectivity problem, or a
328 * misconfigured (or broken) partner. */
330 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
332 return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
335 /* This function should be called periodically to update 'lacp'. */
337 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
341 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
342 if (timer_expired(&slave->rx)) {
343 if (slave->status == LACP_CURRENT) {
344 slave_set_expired(slave);
345 } else if (slave->status == LACP_EXPIRED) {
346 slave_set_defaulted(slave);
352 lacp_update_attached(lacp);
355 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
357 struct lacp_info actor;
359 if (!slave_may_tx(slave)) {
363 slave_get_actor(slave, &actor);
365 if (timer_expired(&slave->tx)
366 || !info_tx_equal(&actor, &slave->ntt_actor)) {
368 slave->ntt_actor = actor;
369 compose_lacp_pdu(&actor, &slave->partner, &pdu);
370 send_pdu(slave->aux, &pdu);
372 timer_set_duration(&slave->tx,
373 (slave->partner.state & LACP_STATE_TIME
375 : LACP_SLOW_TIME_TX));
380 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
382 lacp_wait(struct lacp *lacp)
386 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
387 if (slave_may_tx(slave)) {
388 timer_wait(&slave->tx);
391 if (slave->status != LACP_DEFAULTED) {
392 timer_wait(&slave->rx);
397 /* Static Helpers. */
399 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
400 * negotiated parameter to true if any slaves are attachable. */
402 lacp_update_attached(struct lacp *lacp)
404 struct slave *lead, *slave;
405 struct lacp_info lead_pri;
406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
408 lacp->update = false;
411 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
412 struct lacp_info pri;
414 slave->attached = true;
416 /* XXX: In the future allow users to configure the expected system ID.
417 * For now just special case loopback. */
418 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
419 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
420 "connected to its own bond", slave->name);
421 slave->attached = false;
425 if (slave->status == LACP_DEFAULTED) {
429 slave_get_priority(slave, &pri);
431 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
437 lacp->negotiated = lead != NULL;
440 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
441 if (slave->status == LACP_DEFAULTED
442 || lead->partner.key != slave->partner.key
443 || !eth_addr_equals(lead->partner.sys_id,
444 slave->partner.sys_id)) {
445 slave->attached = false;
448 } else if (lacp->strict) {
449 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
450 slave->attached = false;
456 slave_destroy(struct slave *slave)
459 struct lacp *lacp = slave->lacp;
462 hmap_remove(&lacp->slaves, &slave->node);
464 if (lacp->key_slave == slave) {
465 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
468 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
470 lacp->key_slave = NULL;
480 slave_set_defaulted(struct slave *slave)
482 memset(&slave->partner, 0, sizeof slave->partner);
484 slave->lacp->update = true;
485 slave->status = LACP_DEFAULTED;
489 slave_set_expired(struct slave *slave)
491 slave->status = LACP_EXPIRED;
492 slave->partner.state |= LACP_STATE_TIME;
493 slave->partner.state &= ~LACP_STATE_SYNC;
494 timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
498 slave_get_actor(struct slave *slave, struct lacp_info *actor)
502 if (slave->lacp->active) {
503 state |= LACP_STATE_ACT;
506 if (slave->lacp->fast) {
507 state |= LACP_STATE_TIME;
510 if (slave->attached) {
511 state |= LACP_STATE_SYNC;
514 if (slave->status == LACP_DEFAULTED) {
515 state |= LACP_STATE_DEF;
518 if (slave->status == LACP_EXPIRED) {
519 state |= LACP_STATE_EXP;
522 if (hmap_count(&slave->lacp->slaves) > 1) {
523 state |= LACP_STATE_AGG;
526 if (slave->attached || !slave->lacp->negotiated) {
527 state |= LACP_STATE_COL | LACP_STATE_DIST;
530 actor->state = state;
531 actor->key = htons(slave->lacp->key_slave->port_id);
532 actor->port_priority = htons(slave->port_priority);
533 actor->port_id = htons(slave->port_id);
534 actor->sys_priority = htons(slave->lacp->sys_priority);
535 memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
538 /* Given 'slave', populates 'priority' with data representing its LACP link
539 * priority. If two priority objects populated by this function are compared
540 * using memcmp, the higher priority link will be less than the lower priority
543 slave_get_priority(struct slave *slave, struct lacp_info *priority)
545 uint16_t partner_priority, actor_priority;
547 /* Choose the lacp_info of the higher priority system by comparing their
548 * system priorities and mac addresses. */
549 actor_priority = slave->lacp->sys_priority;
550 partner_priority = ntohs(slave->partner.sys_priority);
551 if (actor_priority < partner_priority) {
552 slave_get_actor(slave, priority);
553 } else if (partner_priority < actor_priority) {
554 *priority = slave->partner;
555 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
556 slave->partner.sys_id) < 0) {
557 slave_get_actor(slave, priority);
559 *priority = slave->partner;
562 /* Key and state are not used in priority comparisons. */
568 slave_may_tx(const struct slave *slave)
570 return slave->lacp->active || slave->status != LACP_DEFAULTED;
573 static struct slave *
574 slave_lookup(const struct lacp *lacp, const void *slave_)
578 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
580 if (slave->aux == slave_) {
588 /* Two lacp_info structures are tx_equal if and only if they do not differ in
589 * ways which would require a lacp_pdu transmission. */
591 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
594 /* LACP specification dictates that we transmit whenever the actor and
595 * remote_actor differ in the following fields: Port, Port Priority,
596 * System, System Priority, Aggregation Key, Activity State, Timeout State,
597 * Sync State, and Aggregation State. The state flags are most likely to
598 * change so are checked first. */
599 return !((a->state ^ b->state) & (LACP_STATE_ACT
603 && a->port_id == b->port_id
604 && a->port_priority == b->port_priority
606 && a->sys_priority == b->sys_priority
607 && eth_addr_equals(a->sys_id, b->sys_id);
611 lacp_find(const char *name)
615 LIST_FOR_EACH (lacp, node, &all_lacps) {
616 if (!strcmp(lacp->name, name)) {
625 ds_put_lacp_state(struct ds *ds, uint8_t state)
627 if (state & LACP_STATE_ACT) {
628 ds_put_cstr(ds, "activity ");
631 if (state & LACP_STATE_TIME) {
632 ds_put_cstr(ds, "timeout ");
635 if (state & LACP_STATE_AGG) {
636 ds_put_cstr(ds, "aggregation ");
639 if (state & LACP_STATE_SYNC) {
640 ds_put_cstr(ds, "synchronized ");
643 if (state & LACP_STATE_COL) {
644 ds_put_cstr(ds, "collecting ");
647 if (state & LACP_STATE_DIST) {
648 ds_put_cstr(ds, "distributing ");
651 if (state & LACP_STATE_DEF) {
652 ds_put_cstr(ds, "defaulted ");
655 if (state & LACP_STATE_EXP) {
656 ds_put_cstr(ds, "expired ");
661 lacp_unixctl_show(struct unixctl_conn *conn,
662 const char *args, void *aux OVS_UNUSED)
664 struct ds ds = DS_EMPTY_INITIALIZER;
668 lacp = lacp_find(args);
670 unixctl_command_reply(conn, 501, "no such lacp object");
674 ds_put_format(&ds, "lacp: %s\n", lacp->name);
676 ds_put_format(&ds, "\tstatus: %s", lacp->active ? "active" : "passive");
678 ds_put_cstr(&ds, " strict");
680 if (lacp->negotiated) {
681 ds_put_cstr(&ds, " negotiated");
683 ds_put_cstr(&ds, "\n");
685 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
686 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
687 ds_put_cstr(&ds, "\taggregation key: ");
688 if (lacp->key_slave) {
689 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
691 ds_put_cstr(&ds, "none");
693 ds_put_cstr(&ds, "\n");
695 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
697 struct lacp_info actor;
699 slave_get_actor(slave, &actor);
700 switch (slave->status) {
708 status = "defaulted";
714 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
715 slave->attached ? "attached" : "detached");
716 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
717 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
719 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
720 ETH_ADDR_ARGS(actor.sys_id));
721 ds_put_format(&ds, "\tactor sys_priority: %u\n",
722 ntohs(actor.sys_priority));
723 ds_put_format(&ds, "\tactor port_id: %u\n",
724 ntohs(actor.port_id));
725 ds_put_format(&ds, "\tactor port_priority: %u\n",
726 ntohs(actor.port_priority));
727 ds_put_format(&ds, "\tactor key: %u\n",
729 ds_put_cstr(&ds, "\tactor state: ");
730 ds_put_lacp_state(&ds, actor.state);
731 ds_put_cstr(&ds, "\n\n");
733 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
734 ETH_ADDR_ARGS(slave->partner.sys_id));
735 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
736 ntohs(slave->partner.sys_priority));
737 ds_put_format(&ds, "\tpartner port_id: %u\n",
738 ntohs(slave->partner.port_id));
739 ds_put_format(&ds, "\tpartner port_priority: %u\n",
740 ntohs(slave->partner.port_priority));
741 ds_put_format(&ds, "\tpartner key: %u\n",
742 ntohs(slave->partner.key));
743 ds_put_cstr(&ds, "\tpartner state: ");
744 ds_put_lacp_state(&ds, slave->partner.state);
745 ds_put_cstr(&ds, "\n");
748 unixctl_command_reply(conn, 200, ds_cstr(&ds));