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 struct lacp_info partner; /* Partner information. */
68 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
69 struct timer tx; /* Next message transmission timer. */
70 struct timer rx; /* Expected message receive timer. */
73 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
75 static void lacp_update_attached(struct lacp *);
77 static void slave_destroy(struct slave *);
78 static void slave_set_defaulted(struct slave *);
79 static void slave_set_expired(struct slave *);
80 static void slave_get_actor(struct slave *, struct lacp_info *actor);
81 static void slave_get_priority(struct slave *, struct lacp_info *priority);
82 static bool slave_may_tx(const struct slave *);
83 static struct slave *slave_lookup(const struct lacp *, const void *slave);
84 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
86 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
89 /* Initializes the lacp module. */
93 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
96 /* Creates a LACP object. */
102 lacp = xzalloc(sizeof *lacp);
103 hmap_init(&lacp->slaves);
104 list_push_back(&all_lacps, &lacp->node);
108 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
110 lacp_destroy(struct lacp *lacp)
113 struct slave *slave, *next;
115 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
116 slave_destroy(slave);
119 hmap_destroy(&lacp->slaves);
120 list_remove(&lacp->node);
126 /* Configures 'lacp' with settings from 's'. */
128 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
130 if (!lacp->name || strcmp(s->name, lacp->name)) {
132 lacp->name = xstrdup(s->name);
135 if (!eth_addr_equals(lacp->sys_id, s->id)
136 || lacp->sys_priority != s->priority) {
137 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
138 lacp->sys_priority = s->priority;
142 lacp->active = s->active;
143 lacp->fast = s->fast;
146 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
147 * configured for passive mode. */
149 lacp_is_active(const struct lacp *lacp)
154 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
155 * should be called on all packets received on 'slave_' with Ethernet Type
156 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
158 lacp_process_pdu(struct lacp *lacp, const void *slave_,
159 const struct lacp_pdu *pdu)
161 struct slave *slave = slave_lookup(lacp, slave_);
163 slave->status = LACP_CURRENT;
164 timer_set_duration(&slave->rx, (lacp->fast
166 : LACP_SLOW_TIME_RX));
168 slave->ntt_actor = pdu->partner;
170 /* Update our information about our partner if it's out of date. This may
171 * cause priorities to change so re-calculate attached status of all
173 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
175 slave->partner = pdu->actor;
179 /* Returns true if 'lacp' has successfully negotiated with its partner. False
180 * if 'lacp' is NULL. */
182 lacp_negotiated(const struct lacp *lacp)
184 return lacp ? lacp->negotiated : false;
187 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
188 * once per slave in a LACP managed bond. Should also be called whenever a
189 * slave's settings change. */
191 lacp_slave_register(struct lacp *lacp, void *slave_,
192 const struct lacp_slave_settings *s)
194 struct slave *slave = slave_lookup(lacp, slave_);
197 slave = xzalloc(sizeof *slave);
200 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
201 slave_set_defaulted(slave);
203 if (!lacp->key_slave) {
204 lacp->key_slave = slave;
208 if (!slave->name || strcmp(s->name, slave->name)) {
210 slave->name = xstrdup(s->name);
213 if (slave->port_id != s->id || slave->port_priority != s->priority) {
214 slave->port_id = s->id;
215 slave->port_priority = s->priority;
219 if (lacp->active || lacp->negotiated) {
220 slave_set_expired(slave);
225 /* Unregisters 'slave_' with 'lacp'. */
227 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
229 struct slave *slave = slave_lookup(lacp, slave_);
232 slave_destroy(slave);
237 /* This function should be called whenever the carrier status of 'slave_' has
240 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
242 struct slave *slave = slave_lookup(lacp, slave_);
244 if (slave->status == LACP_CURRENT || slave->lacp->active) {
245 slave_set_expired(slave);
249 /* This function should be called before enabling 'slave_' to send or receive
250 * traffic. If it returns false, 'slave_' should not enabled. As a
251 * convenience, returns true if 'lacp' is NULL. */
253 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
256 struct slave *slave = slave_lookup(lacp, slave_);
258 /* The slave may be enabled if it's attached to an aggregator and its
259 * partner is synchronized. The only exception is defaulted slaves.
260 * They are not required to have synchronized partners because they
261 * have no partners at all. They will only be attached if negotiations
262 * failed on all slaves in the bond. */
263 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
264 || slave->status == LACP_DEFAULTED);
270 /* Returns the port ID used for 'slave_' in LACP communications. */
272 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
274 struct slave *slave = slave_lookup(lacp, slave_);
275 return slave->port_id;
278 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
279 * not being current, generally indicates a connectivity problem, or a
280 * misconfigured (or broken) partner. */
282 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
284 return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
287 /* This function should be called periodically to update 'lacp'. */
289 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
293 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
294 if (timer_expired(&slave->rx)) {
295 if (slave->status == LACP_CURRENT) {
296 slave_set_expired(slave);
297 } else if (slave->status == LACP_EXPIRED) {
298 slave_set_defaulted(slave);
304 lacp_update_attached(lacp);
307 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
309 struct lacp_info actor;
311 if (!slave_may_tx(slave)) {
315 slave_get_actor(slave, &actor);
317 if (timer_expired(&slave->tx)
318 || !info_tx_equal(&actor, &slave->ntt_actor)) {
320 slave->ntt_actor = actor;
321 compose_lacp_pdu(&actor, &slave->partner, &pdu);
322 send_pdu(slave->aux, &pdu);
324 timer_set_duration(&slave->tx,
325 (slave->partner.state & LACP_STATE_TIME
327 : LACP_SLOW_TIME_TX));
332 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
334 lacp_wait(struct lacp *lacp)
338 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
339 if (slave_may_tx(slave)) {
340 timer_wait(&slave->tx);
343 if (slave->status != LACP_DEFAULTED) {
344 timer_wait(&slave->rx);
349 /* Static Helpers. */
351 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
352 * negotiated parameter to true if any slaves are attachable. */
354 lacp_update_attached(struct lacp *lacp)
356 struct slave *lead, *slave;
357 struct lacp_info lead_pri;
358 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
360 lacp->update = false;
363 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
364 struct lacp_info pri;
366 slave->attached = true;
368 /* XXX: In the future allow users to configure the expected system ID.
369 * For now just special case loopback. */
370 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
371 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
372 "connected to its own bond", slave->name);
373 slave->attached = false;
377 if (slave->status == LACP_DEFAULTED) {
381 slave_get_priority(slave, &pri);
383 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
389 lacp->negotiated = lead != NULL;
392 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
393 if (slave->status == LACP_DEFAULTED
394 || lead->partner.key != slave->partner.key
395 || !eth_addr_equals(lead->partner.sys_id,
396 slave->partner.sys_id)) {
397 slave->attached = false;
404 slave_destroy(struct slave *slave)
407 struct lacp *lacp = slave->lacp;
410 hmap_remove(&lacp->slaves, &slave->node);
412 if (lacp->key_slave == slave) {
413 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
416 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
418 lacp->key_slave = NULL;
428 slave_set_defaulted(struct slave *slave)
430 memset(&slave->partner, 0, sizeof slave->partner);
432 slave->lacp->update = true;
433 slave->status = LACP_DEFAULTED;
437 slave_set_expired(struct slave *slave)
439 slave->status = LACP_EXPIRED;
440 slave->partner.state |= LACP_STATE_TIME;
441 slave->partner.state &= ~LACP_STATE_SYNC;
442 timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
446 slave_get_actor(struct slave *slave, struct lacp_info *actor)
450 if (slave->lacp->active) {
451 state |= LACP_STATE_ACT;
454 if (slave->lacp->fast) {
455 state |= LACP_STATE_TIME;
458 if (slave->attached) {
459 state |= LACP_STATE_SYNC;
462 if (slave->status == LACP_DEFAULTED) {
463 state |= LACP_STATE_DEF;
466 if (slave->status == LACP_EXPIRED) {
467 state |= LACP_STATE_EXP;
470 if (hmap_count(&slave->lacp->slaves) > 1) {
471 state |= LACP_STATE_AGG;
474 if (slave->attached || !slave->lacp->negotiated) {
475 state |= LACP_STATE_COL | LACP_STATE_DIST;
478 actor->state = state;
479 actor->key = htons(slave->lacp->key_slave->port_id);
480 actor->port_priority = htons(slave->port_priority);
481 actor->port_id = htons(slave->port_id);
482 actor->sys_priority = htons(slave->lacp->sys_priority);
483 memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
486 /* Given 'slave', populates 'priority' with data representing its LACP link
487 * priority. If two priority objects populated by this function are compared
488 * using memcmp, the higher priority link will be less than the lower priority
491 slave_get_priority(struct slave *slave, struct lacp_info *priority)
493 uint16_t partner_priority, actor_priority;
495 /* Choose the lacp_info of the higher priority system by comparing their
496 * system priorities and mac addresses. */
497 actor_priority = slave->lacp->sys_priority;
498 partner_priority = ntohs(slave->partner.sys_priority);
499 if (actor_priority < partner_priority) {
500 slave_get_actor(slave, priority);
501 } else if (partner_priority < actor_priority) {
502 *priority = slave->partner;
503 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
504 slave->partner.sys_id) < 0) {
505 slave_get_actor(slave, priority);
507 *priority = slave->partner;
510 /* Key and state are not used in priority comparisons. */
516 slave_may_tx(const struct slave *slave)
518 return slave->lacp->active || slave->status != LACP_DEFAULTED;
521 static struct slave *
522 slave_lookup(const struct lacp *lacp, const void *slave_)
526 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
528 if (slave->aux == slave_) {
536 /* Two lacp_info structures are tx_equal if and only if they do not differ in
537 * ways which would require a lacp_pdu transmission. */
539 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
542 /* LACP specification dictates that we transmit whenever the actor and
543 * remote_actor differ in the following fields: Port, Port Priority,
544 * System, System Priority, Aggregation Key, Activity State, Timeout State,
545 * Sync State, and Aggregation State. The state flags are most likely to
546 * change so are checked first. */
547 return !((a->state ^ b->state) & (LACP_STATE_ACT
551 && a->port_id == b->port_id
552 && a->port_priority == b->port_priority
554 && a->sys_priority == b->sys_priority
555 && eth_addr_equals(a->sys_id, b->sys_id);
559 lacp_find(const char *name)
563 LIST_FOR_EACH (lacp, node, &all_lacps) {
564 if (!strcmp(lacp->name, name)) {
573 ds_put_lacp_state(struct ds *ds, uint8_t state)
575 if (state & LACP_STATE_ACT) {
576 ds_put_cstr(ds, "activity ");
579 if (state & LACP_STATE_TIME) {
580 ds_put_cstr(ds, "timeout ");
583 if (state & LACP_STATE_AGG) {
584 ds_put_cstr(ds, "aggregation ");
587 if (state & LACP_STATE_SYNC) {
588 ds_put_cstr(ds, "synchronized ");
591 if (state & LACP_STATE_COL) {
592 ds_put_cstr(ds, "collecting ");
595 if (state & LACP_STATE_DIST) {
596 ds_put_cstr(ds, "distributing ");
599 if (state & LACP_STATE_DEF) {
600 ds_put_cstr(ds, "defaulted ");
603 if (state & LACP_STATE_EXP) {
604 ds_put_cstr(ds, "expired ");
609 lacp_unixctl_show(struct unixctl_conn *conn,
610 const char *args, void *aux OVS_UNUSED)
612 struct ds ds = DS_EMPTY_INITIALIZER;
616 lacp = lacp_find(args);
618 unixctl_command_reply(conn, 501, "no such lacp object");
622 ds_put_format(&ds, "lacp: %s\n", lacp->name);
623 ds_put_format(&ds, "\tstatus: %s %s\n",
624 lacp->active ? "active" : "passive",
625 lacp->negotiated ? "negotiated" : "");
626 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
627 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
628 ds_put_cstr(&ds, "\taggregation key: ");
629 if (lacp->key_slave) {
630 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
632 ds_put_cstr(&ds, "none");
634 ds_put_cstr(&ds, "\n");
636 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
638 struct lacp_info actor;
640 slave_get_actor(slave, &actor);
641 switch (slave->status) {
649 status = "defaulted";
655 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
656 slave->attached ? "attached" : "detached");
657 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
658 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
660 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
661 ETH_ADDR_ARGS(actor.sys_id));
662 ds_put_format(&ds, "\tactor sys_priority: %u\n",
663 ntohs(actor.sys_priority));
664 ds_put_format(&ds, "\tactor port_id: %u\n",
665 ntohs(actor.port_id));
666 ds_put_format(&ds, "\tactor port_priority: %u\n",
667 ntohs(actor.port_priority));
668 ds_put_format(&ds, "\tactor key: %u\n",
670 ds_put_cstr(&ds, "\tactor state: ");
671 ds_put_lacp_state(&ds, actor.state);
672 ds_put_cstr(&ds, "\n\n");
674 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
675 ETH_ADDR_ARGS(slave->partner.sys_id));
676 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
677 ntohs(slave->partner.sys_priority));
678 ds_put_format(&ds, "\tpartner port_id: %u\n",
679 ntohs(slave->partner.port_id));
680 ds_put_format(&ds, "\tpartner port_priority: %u\n",
681 ntohs(slave->partner.port_priority));
682 ds_put_format(&ds, "\tpartner key: %u\n",
683 ntohs(slave->partner.key));
684 ds_put_cstr(&ds, "\tpartner state: ");
685 ds_put_lacp_state(&ds, slave->partner.state);
686 ds_put_cstr(&ds, "\n");
689 unixctl_command_reply(conn, 200, ds_cstr(&ds));