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 negotiated; /* True if LACP negotiations were successful. */
53 bool update; /* True if lacp_update() needs to be called. */
57 void *aux; /* Handle used to identify this slave. */
58 struct hmap_node node; /* Node in master's slaves map. */
60 struct lacp *lacp; /* LACP object containing this slave. */
61 uint16_t port_id; /* Port ID. */
62 uint16_t port_priority; /* Port Priority. */
63 char *name; /* Name of this slave. */
65 enum slave_status status; /* Slave status. */
66 bool attached; /* Attached. Traffic may flow. */
67 bool enabled; /* Enabled. Traffic is flowing. */
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 /* Initializes the lacp module. */
94 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
97 /* Creates a LACP object. */
103 lacp = xzalloc(sizeof *lacp);
104 hmap_init(&lacp->slaves);
105 list_push_back(&all_lacps, &lacp->node);
109 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
111 lacp_destroy(struct lacp *lacp)
114 struct slave *slave, *next;
116 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
117 slave_destroy(slave);
120 hmap_destroy(&lacp->slaves);
121 list_remove(&lacp->node);
127 /* Configures 'lacp' with settings from 's'. */
129 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
131 if (!lacp->name || strcmp(s->name, lacp->name)) {
133 lacp->name = xstrdup(s->name);
136 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
137 lacp->sys_priority = s->priority;
138 lacp->active = s->active;
139 lacp->fast = s->fast;
142 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
143 * configured for passive mode. */
145 lacp_is_active(const struct lacp *lacp)
150 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
151 * should be called on all packets received on 'slave_' with Ethernet Type
152 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
154 lacp_process_pdu(struct lacp *lacp, const void *slave_,
155 const struct lacp_pdu *pdu)
157 struct slave *slave = slave_lookup(lacp, slave_);
159 slave->status = LACP_CURRENT;
160 timer_set_duration(&slave->rx, (lacp->fast
162 : LACP_SLOW_TIME_RX));
164 slave->ntt_actor = pdu->partner;
166 /* Update our information about our partner if it's out of date. This may
167 * cause priorities to change so re-calculate attached status of all
169 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
171 slave->partner = pdu->actor;
175 /* Returns true if 'lacp' has successfully negotiated with its partner. False
176 * if 'lacp' is NULL. */
178 lacp_negotiated(const struct lacp *lacp)
180 return lacp ? lacp->negotiated : false;
183 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
184 * once per slave in a LACP managed bond. Should also be called whenever a
185 * slave's settings change. */
187 lacp_slave_register(struct lacp *lacp, void *slave_,
188 const struct lacp_slave_settings *s)
190 struct slave *slave = slave_lookup(lacp, slave_);
193 slave = xzalloc(sizeof *slave);
196 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
197 slave_set_defaulted(slave);
199 if (!lacp->key_slave) {
200 lacp->key_slave = slave;
204 if (!slave->name || strcmp(s->name, slave->name)) {
206 slave->name = xstrdup(s->name);
209 if (slave->port_id != s->id || slave->port_priority != s->priority) {
210 slave->port_id = s->id;
211 slave->port_priority = s->priority;
215 if (lacp->active || lacp->negotiated) {
216 slave_set_expired(slave);
221 /* Unregisters 'slave_' with 'lacp'. */
223 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
225 struct slave *slave = slave_lookup(lacp, slave_);
228 slave_destroy(slave);
232 /* Should be called regularly to indicate whether 'slave_' is enabled. An
233 * enabled slave is allowed to send and receive traffic. Generally a slave
234 * should not be enabled if its carrier is down, or lacp_slave_may_enable()
235 * indicates it should not be enabled. */
237 lacp_slave_enable(struct lacp *lacp, void *slave_, bool enabled)
239 slave_lookup(lacp, slave_)->enabled = enabled;
242 /* This function should be called whenever the carrier status of 'slave_' has
245 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
247 struct slave *slave = slave_lookup(lacp, slave_);
249 if (slave->status == LACP_CURRENT || slave->lacp->active) {
250 slave_set_expired(slave);
254 /* This function should be called before enabling 'slave_' to send or receive
255 * traffic. If it returns false, 'slave_' should not enabled. As a
256 * convenience, returns true if 'lacp' is NULL. */
258 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
261 struct slave *slave = slave_lookup(lacp, slave_);
263 /* The slave may be enabled if it's attached to an aggregator and its
264 * partner is synchronized. The only exception is defaulted slaves.
265 * They are not required to have synchronized partners because they
266 * have no partners at all. They will only be attached if negotiations
267 * failed on all slaves in the bond. */
268 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
269 || slave->status == LACP_DEFAULTED);
275 /* This function should be called periodically to update 'lacp'. */
277 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
281 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
282 if (timer_expired(&slave->rx)) {
283 if (slave->status == LACP_CURRENT) {
284 slave_set_expired(slave);
285 } else if (slave->status == LACP_EXPIRED) {
286 slave_set_defaulted(slave);
292 lacp_update_attached(lacp);
295 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
297 struct lacp_info actor;
299 if (!slave_may_tx(slave)) {
303 slave_get_actor(slave, &actor);
305 if (timer_expired(&slave->tx)
306 || !info_tx_equal(&actor, &slave->ntt_actor)) {
308 slave->ntt_actor = actor;
309 compose_lacp_pdu(&actor, &slave->partner, &pdu);
310 send_pdu(slave->aux, &pdu);
312 timer_set_duration(&slave->tx,
313 (slave->partner.state & LACP_STATE_TIME
315 : LACP_SLOW_TIME_TX));
320 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
322 lacp_wait(struct lacp *lacp)
326 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
327 if (slave_may_tx(slave)) {
328 timer_wait(&slave->tx);
331 if (slave->status != LACP_DEFAULTED) {
332 timer_wait(&slave->rx);
337 /* Static Helpers. */
339 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
340 * negotiated parameter to true if any slaves are attachable. */
342 lacp_update_attached(struct lacp *lacp)
344 struct slave *lead, *slave;
345 struct lacp_info lead_pri;
346 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
348 lacp->update = false;
351 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
352 struct lacp_info pri;
354 slave->attached = true;
356 /* XXX: In the future allow users to configure the expected system ID.
357 * For now just special case loopback. */
358 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
359 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
360 "connected to its own bond", slave->name);
361 slave->attached = false;
365 if (slave->status == LACP_DEFAULTED) {
369 slave_get_priority(slave, &pri);
371 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
377 lacp->negotiated = lead != NULL;
380 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
381 if (slave->status == LACP_DEFAULTED
382 || lead->partner.key != slave->partner.key
383 || !eth_addr_equals(lead->partner.sys_id,
384 slave->partner.sys_id)) {
385 slave->attached = false;
392 slave_destroy(struct slave *slave)
395 struct lacp *lacp = slave->lacp;
398 hmap_remove(&lacp->slaves, &slave->node);
400 if (lacp->key_slave == slave) {
401 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
404 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
406 lacp->key_slave = NULL;
416 slave_set_defaulted(struct slave *slave)
418 memset(&slave->partner, 0, sizeof slave->partner);
420 slave->lacp->update = true;
421 slave->status = LACP_DEFAULTED;
425 slave_set_expired(struct slave *slave)
427 slave->status = LACP_EXPIRED;
428 slave->partner.state |= LACP_STATE_TIME;
429 slave->partner.state &= ~LACP_STATE_SYNC;
430 timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
434 slave_get_actor(struct slave *slave, struct lacp_info *actor)
438 if (slave->lacp->active) {
439 state |= LACP_STATE_ACT;
442 if (slave->lacp->fast) {
443 state |= LACP_STATE_TIME;
446 if (slave->attached) {
447 state |= LACP_STATE_SYNC;
450 if (slave->status == LACP_DEFAULTED) {
451 state |= LACP_STATE_DEF;
454 if (slave->status == LACP_EXPIRED) {
455 state |= LACP_STATE_EXP;
458 if (hmap_count(&slave->lacp->slaves) > 1) {
459 state |= LACP_STATE_AGG;
462 if (slave->enabled) {
463 state |= LACP_STATE_COL | LACP_STATE_DIST;
466 actor->state = state;
467 actor->key = htons(slave->lacp->key_slave->port_id);
468 actor->port_priority = htons(slave->port_priority);
469 actor->port_id = htons(slave->port_id);
470 actor->sys_priority = htons(slave->lacp->sys_priority);
471 memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
474 /* Given 'slave', populates 'priority' with data representing its LACP link
475 * priority. If two priority objects populated by this function are compared
476 * using memcmp, the higher priority link will be less than the lower priority
479 slave_get_priority(struct slave *slave, struct lacp_info *priority)
481 uint16_t partner_priority, actor_priority;
483 /* Choose the lacp_info of the higher priority system by comparing their
484 * system priorities and mac addresses. */
485 actor_priority = slave->lacp->sys_priority;
486 partner_priority = ntohs(slave->partner.sys_priority);
487 if (actor_priority < partner_priority) {
488 slave_get_actor(slave, priority);
489 } else if (partner_priority < actor_priority) {
490 *priority = slave->partner;
491 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
492 slave->partner.sys_id) < 0) {
493 slave_get_actor(slave, priority);
495 *priority = slave->partner;
498 /* Key and state are not used in priority comparisons. */
504 slave_may_tx(const struct slave *slave)
506 return slave->lacp->active || slave->status != LACP_DEFAULTED;
509 static struct slave *
510 slave_lookup(const struct lacp *lacp, const void *slave_)
514 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
516 if (slave->aux == slave_) {
524 /* Two lacp_info structures are tx_equal if and only if they do not differ in
525 * ways which would require a lacp_pdu transmission. */
527 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
530 /* LACP specification dictates that we transmit whenever the actor and
531 * remote_actor differ in the following fields: Port, Port Priority,
532 * System, System Priority, Aggregation Key, Activity State, Timeout State,
533 * Sync State, and Aggregation State. The state flags are most likely to
534 * change so are checked first. */
535 return !((a->state ^ b->state) & (LACP_STATE_ACT
539 && a->port_id == b->port_id
540 && a->port_priority == b->port_priority
542 && a->sys_priority == b->sys_priority
543 && eth_addr_equals(a->sys_id, b->sys_id);
547 lacp_find(const char *name)
551 LIST_FOR_EACH (lacp, node, &all_lacps) {
552 if (!strcmp(lacp->name, name)) {
561 ds_put_lacp_state(struct ds *ds, uint8_t state)
563 if (state & LACP_STATE_ACT) {
564 ds_put_cstr(ds, "activity ");
567 if (state & LACP_STATE_TIME) {
568 ds_put_cstr(ds, "timeout ");
571 if (state & LACP_STATE_AGG) {
572 ds_put_cstr(ds, "aggregation ");
575 if (state & LACP_STATE_SYNC) {
576 ds_put_cstr(ds, "synchronized ");
579 if (state & LACP_STATE_COL) {
580 ds_put_cstr(ds, "collecting ");
583 if (state & LACP_STATE_DIST) {
584 ds_put_cstr(ds, "distributing ");
587 if (state & LACP_STATE_DEF) {
588 ds_put_cstr(ds, "defaulted ");
591 if (state & LACP_STATE_EXP) {
592 ds_put_cstr(ds, "expired ");
597 lacp_unixctl_show(struct unixctl_conn *conn,
598 const char *args, void *aux OVS_UNUSED)
600 struct ds ds = DS_EMPTY_INITIALIZER;
604 lacp = lacp_find(args);
606 unixctl_command_reply(conn, 501, "no such lacp object");
610 ds_put_format(&ds, "lacp: %s\n", lacp->name);
611 ds_put_format(&ds, "\tstatus: %s %s\n",
612 lacp->active ? "active" : "passive",
613 lacp->negotiated ? "negotiated" : "");
614 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
615 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
616 ds_put_cstr(&ds, "\taggregation key: ");
617 if (lacp->key_slave) {
618 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
620 ds_put_cstr(&ds, "none");
622 ds_put_cstr(&ds, "\n");
624 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
626 struct lacp_info actor;
628 slave_get_actor(slave, &actor);
629 switch (slave->status) {
637 status = "defaulted";
643 ds_put_format(&ds, "\nslave: %s: %s %s %s\n", slave->name, status,
644 slave->attached ? "attached" : "detached",
645 slave->enabled ? "enabled" : "disabled");
646 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
647 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
649 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
650 ETH_ADDR_ARGS(actor.sys_id));
651 ds_put_format(&ds, "\tactor sys_priority: %u\n",
652 ntohs(actor.sys_priority));
653 ds_put_format(&ds, "\tactor port_id: %u\n",
654 ntohs(actor.port_id));
655 ds_put_format(&ds, "\tactor port_priority: %u\n",
656 ntohs(actor.port_priority));
657 ds_put_format(&ds, "\tactor key: %u\n",
659 ds_put_cstr(&ds, "\tactor state: ");
660 ds_put_lacp_state(&ds, actor.state);
661 ds_put_cstr(&ds, "\n\n");
663 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
664 ETH_ADDR_ARGS(slave->partner.sys_id));
665 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
666 ntohs(slave->partner.sys_priority));
667 ds_put_format(&ds, "\tpartner port_id: %u\n",
668 ntohs(slave->partner.port_id));
669 ds_put_format(&ds, "\tpartner port_priority: %u\n",
670 ntohs(slave->partner.port_priority));
671 ds_put_format(&ds, "\tpartner key: %u\n",
672 ntohs(slave->partner.key));
673 ds_put_cstr(&ds, "\tpartner state: ");
674 ds_put_lacp_state(&ds, slave->partner.state);
675 ds_put_cstr(&ds, "\n");
678 unixctl_command_reply(conn, 200, ds_cstr(&ds));