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 negotiated; /* True if LACP negotiations were successful. */
54 bool update; /* True if lacp_update() needs to be called. */
55 bool heartbeat; /* LACP heartbeat mode. */
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 uint16_t key; /* Aggregation Key. 0 if default. */
66 char *name; /* Name of this slave. */
68 enum slave_status status; /* Slave status. */
69 bool attached; /* Attached. Traffic may flow. */
70 struct lacp_info partner; /* Partner information. */
71 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
72 struct timer tx; /* Next message transmission timer. */
73 struct timer rx; /* Expected message receive timer. */
76 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
78 static void lacp_update_attached(struct lacp *);
80 static void slave_destroy(struct slave *);
81 static void slave_set_defaulted(struct slave *);
82 static void slave_set_expired(struct slave *);
83 static void slave_get_actor(struct slave *, struct lacp_info *actor);
84 static void slave_get_priority(struct slave *, struct lacp_info *priority);
85 static bool slave_may_tx(const struct slave *);
86 static struct slave *slave_lookup(const struct lacp *, const void *slave);
87 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
89 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
92 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
94 compose_lacp_pdu(const struct lacp_info *actor,
95 const struct lacp_info *partner, struct lacp_pdu *pdu)
97 memset(pdu, 0, sizeof *pdu);
106 pdu->partner_type = 2;
107 pdu->partner_len = 20;
108 pdu->partner = *partner;
110 pdu->collector_type = 3;
111 pdu->collector_len = 16;
112 pdu->collector_delay = htons(0);
115 /* Parses 'b' which represents a packet containing a LACP PDU. This function
116 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
117 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
119 const struct lacp_pdu *
120 parse_lacp_packet(const struct ofpbuf *b)
122 const struct lacp_pdu *pdu;
124 pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
126 if (pdu && pdu->subtype == 1
127 && pdu->actor_type == 1 && pdu->actor_len == 20
128 && pdu->partner_type == 2 && pdu->partner_len == 20) {
135 /* LACP Protocol Implementation. */
137 /* Initializes the lacp module. */
141 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
144 /* Creates a LACP object. */
150 lacp = xzalloc(sizeof *lacp);
151 hmap_init(&lacp->slaves);
152 list_push_back(&all_lacps, &lacp->node);
156 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
158 lacp_destroy(struct lacp *lacp)
161 struct slave *slave, *next;
163 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
164 slave_destroy(slave);
167 hmap_destroy(&lacp->slaves);
168 list_remove(&lacp->node);
174 /* Configures 'lacp' with settings from 's'. */
176 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
178 if (!lacp->name || strcmp(s->name, lacp->name)) {
180 lacp->name = xstrdup(s->name);
183 if (!eth_addr_equals(lacp->sys_id, s->id)
184 || lacp->sys_priority != s->priority
185 || lacp->heartbeat != s->heartbeat) {
186 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
187 lacp->sys_priority = s->priority;
188 lacp->heartbeat = s->heartbeat;
192 lacp->active = s->active;
193 lacp->lacp_time = s->lacp_time;
194 lacp->custom_time = MAX(TIME_UPDATE_INTERVAL, s->custom_time);
197 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
198 * configured for passive mode. */
200 lacp_is_active(const struct lacp *lacp)
205 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
206 * should be called on all packets received on 'slave_' with Ethernet Type
207 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
209 lacp_process_pdu(struct lacp *lacp, const void *slave_,
210 const struct lacp_pdu *pdu)
212 struct slave *slave = slave_lookup(lacp, slave_);
213 long long int tx_rate;
215 switch (lacp->lacp_time) {
217 tx_rate = LACP_FAST_TIME_TX;
220 tx_rate = LACP_SLOW_TIME_TX;
222 case LACP_TIME_CUSTOM:
223 tx_rate = lacp->custom_time;
225 default: NOT_REACHED();
228 slave->status = LACP_CURRENT;
229 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
231 slave->ntt_actor = pdu->partner;
233 /* Update our information about our partner if it's out of date. This may
234 * cause priorities to change so re-calculate attached status of all
236 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
238 slave->partner = pdu->actor;
242 /* Returns true if 'lacp' has successfully negotiated with its partner. False
243 * if 'lacp' is NULL. */
245 lacp_negotiated(const struct lacp *lacp)
247 return lacp ? lacp->negotiated : false;
250 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
251 * once per slave in a LACP managed bond. Should also be called whenever a
252 * slave's settings change. */
254 lacp_slave_register(struct lacp *lacp, void *slave_,
255 const struct lacp_slave_settings *s)
257 struct slave *slave = slave_lookup(lacp, slave_);
260 slave = xzalloc(sizeof *slave);
263 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
264 slave_set_defaulted(slave);
266 if (!lacp->key_slave) {
267 lacp->key_slave = slave;
271 if (!slave->name || strcmp(s->name, slave->name)) {
273 slave->name = xstrdup(s->name);
276 if (slave->port_id != s->id
277 || slave->port_priority != s->priority
278 || slave->key != s->key) {
279 slave->port_id = s->id;
280 slave->port_priority = s->priority;
285 if (lacp->active || lacp->negotiated) {
286 slave_set_expired(slave);
291 /* Unregisters 'slave_' with 'lacp'. */
293 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
295 struct slave *slave = slave_lookup(lacp, slave_);
298 slave_destroy(slave);
303 /* This function should be called whenever the carrier status of 'slave_' has
306 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
308 struct slave *slave = slave_lookup(lacp, slave_);
310 if (slave->status == LACP_CURRENT || slave->lacp->active) {
311 slave_set_expired(slave);
315 /* This function should be called before enabling 'slave_' to send or receive
316 * traffic. If it returns false, 'slave_' should not enabled. As a
317 * convenience, returns true if 'lacp' is NULL. */
319 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
322 struct slave *slave = slave_lookup(lacp, slave_);
324 /* The slave may be enabled if it's attached to an aggregator and its
325 * partner is synchronized. The only exception is defaulted slaves.
326 * They are not required to have synchronized partners because they
327 * have no partners at all. They will only be attached if negotiations
328 * failed on all slaves in the bond. */
329 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
330 || slave->status == LACP_DEFAULTED);
336 /* Returns the port ID used for 'slave_' in LACP communications. */
338 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
340 struct slave *slave = slave_lookup(lacp, slave_);
341 return slave->port_id;
344 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
345 * not being current, generally indicates a connectivity problem, or a
346 * misconfigured (or broken) partner. */
348 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
350 return slave_lookup(lacp, slave_)->status != LACP_DEFAULTED;
353 /* This function should be called periodically to update 'lacp'. */
355 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
359 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
360 if (timer_expired(&slave->rx)) {
361 if (slave->status == LACP_CURRENT) {
362 slave_set_expired(slave);
363 } else if (slave->status == LACP_EXPIRED) {
364 slave_set_defaulted(slave);
370 lacp_update_attached(lacp);
373 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
375 struct lacp_info actor;
377 if (!slave_may_tx(slave)) {
381 slave_get_actor(slave, &actor);
383 if (timer_expired(&slave->tx)
384 || !info_tx_equal(&actor, &slave->ntt_actor)) {
385 long long int duration;
387 slave->ntt_actor = actor;
388 compose_lacp_pdu(&actor, &slave->partner, &pdu);
389 send_pdu(slave->aux, &pdu);
391 if (lacp->lacp_time == LACP_TIME_CUSTOM) {
392 duration = lacp->custom_time;
394 duration = (slave->partner.state & LACP_STATE_TIME
396 : LACP_SLOW_TIME_TX);
399 timer_set_duration(&slave->tx, duration);
404 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
406 lacp_wait(struct lacp *lacp)
410 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
411 if (slave_may_tx(slave)) {
412 timer_wait(&slave->tx);
415 if (slave->status != LACP_DEFAULTED) {
416 timer_wait(&slave->rx);
421 /* Static Helpers. */
423 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
424 * negotiated parameter to true if any slaves are attachable. */
426 lacp_update_attached(struct lacp *lacp)
428 struct slave *lead, *slave;
429 struct lacp_info lead_pri;
430 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
432 if (lacp->heartbeat) {
433 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
434 slave->attached = slave->status != LACP_DEFAULTED;
439 lacp->update = false;
442 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
443 struct lacp_info pri;
445 slave->attached = true;
447 /* XXX: In the future allow users to configure the expected system ID.
448 * For now just special case loopback. */
449 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
450 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
451 "connected to its own bond", slave->name);
452 slave->attached = false;
456 if (slave->status == LACP_DEFAULTED) {
460 slave_get_priority(slave, &pri);
462 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
468 lacp->negotiated = lead != NULL;
471 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
472 if (slave->status == LACP_DEFAULTED
473 || lead->partner.key != slave->partner.key
474 || !eth_addr_equals(lead->partner.sys_id,
475 slave->partner.sys_id)) {
476 slave->attached = false;
483 slave_destroy(struct slave *slave)
486 struct lacp *lacp = slave->lacp;
489 hmap_remove(&lacp->slaves, &slave->node);
491 if (lacp->key_slave == slave) {
492 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
495 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
497 lacp->key_slave = NULL;
507 slave_set_defaulted(struct slave *slave)
509 memset(&slave->partner, 0, sizeof slave->partner);
511 slave->lacp->update = true;
512 slave->status = LACP_DEFAULTED;
516 slave_set_expired(struct slave *slave)
518 struct lacp *lacp = slave->lacp;
520 slave->status = LACP_EXPIRED;
521 slave->partner.state |= LACP_STATE_TIME;
522 slave->partner.state &= ~LACP_STATE_SYNC;
524 /* The spec says we should wait LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX.
525 * This doesn't make sense when using custom times which can be much
526 * smaller than LACP_FAST_TIME. */
527 timer_set_duration(&slave->rx, (lacp->lacp_time == LACP_TIME_CUSTOM
529 : LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX));
533 slave_get_actor(struct slave *slave, struct lacp_info *actor)
535 struct lacp *lacp = slave->lacp;
540 state |= LACP_STATE_ACT;
543 if (lacp->lacp_time != LACP_TIME_SLOW) {
544 state |= LACP_STATE_TIME;
547 if (slave->attached) {
548 state |= LACP_STATE_SYNC;
551 if (slave->status == LACP_DEFAULTED) {
552 state |= LACP_STATE_DEF;
555 if (slave->status == LACP_EXPIRED) {
556 state |= LACP_STATE_EXP;
559 if (lacp->heartbeat || hmap_count(&lacp->slaves) > 1) {
560 state |= LACP_STATE_AGG;
563 if (slave->attached || !lacp->negotiated) {
564 state |= LACP_STATE_COL | LACP_STATE_DIST;
567 key = lacp->key_slave->key;
569 key = lacp->key_slave->port_id;
572 actor->state = state;
573 actor->key = htons(key);
574 actor->port_priority = htons(slave->port_priority);
575 actor->port_id = htons(slave->port_id);
576 actor->sys_priority = htons(lacp->sys_priority);
577 memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
580 /* Given 'slave', populates 'priority' with data representing its LACP link
581 * priority. If two priority objects populated by this function are compared
582 * using memcmp, the higher priority link will be less than the lower priority
585 slave_get_priority(struct slave *slave, struct lacp_info *priority)
587 uint16_t partner_priority, actor_priority;
589 /* Choose the lacp_info of the higher priority system by comparing their
590 * system priorities and mac addresses. */
591 actor_priority = slave->lacp->sys_priority;
592 partner_priority = ntohs(slave->partner.sys_priority);
593 if (actor_priority < partner_priority) {
594 slave_get_actor(slave, priority);
595 } else if (partner_priority < actor_priority) {
596 *priority = slave->partner;
597 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
598 slave->partner.sys_id) < 0) {
599 slave_get_actor(slave, priority);
601 *priority = slave->partner;
604 /* Key and state are not used in priority comparisons. */
610 slave_may_tx(const struct slave *slave)
612 return slave->lacp->active || slave->status != LACP_DEFAULTED;
615 static struct slave *
616 slave_lookup(const struct lacp *lacp, const void *slave_)
620 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
622 if (slave->aux == slave_) {
630 /* Two lacp_info structures are tx_equal if and only if they do not differ in
631 * ways which would require a lacp_pdu transmission. */
633 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
636 /* LACP specification dictates that we transmit whenever the actor and
637 * remote_actor differ in the following fields: Port, Port Priority,
638 * System, System Priority, Aggregation Key, Activity State, Timeout State,
639 * Sync State, and Aggregation State. The state flags are most likely to
640 * change so are checked first. */
641 return !((a->state ^ b->state) & (LACP_STATE_ACT
645 && a->port_id == b->port_id
646 && a->port_priority == b->port_priority
648 && a->sys_priority == b->sys_priority
649 && eth_addr_equals(a->sys_id, b->sys_id);
653 lacp_find(const char *name)
657 LIST_FOR_EACH (lacp, node, &all_lacps) {
658 if (!strcmp(lacp->name, name)) {
667 ds_put_lacp_state(struct ds *ds, uint8_t state)
669 if (state & LACP_STATE_ACT) {
670 ds_put_cstr(ds, "activity ");
673 if (state & LACP_STATE_TIME) {
674 ds_put_cstr(ds, "timeout ");
677 if (state & LACP_STATE_AGG) {
678 ds_put_cstr(ds, "aggregation ");
681 if (state & LACP_STATE_SYNC) {
682 ds_put_cstr(ds, "synchronized ");
685 if (state & LACP_STATE_COL) {
686 ds_put_cstr(ds, "collecting ");
689 if (state & LACP_STATE_DIST) {
690 ds_put_cstr(ds, "distributing ");
693 if (state & LACP_STATE_DEF) {
694 ds_put_cstr(ds, "defaulted ");
697 if (state & LACP_STATE_EXP) {
698 ds_put_cstr(ds, "expired ");
703 lacp_unixctl_show(struct unixctl_conn *conn,
704 const char *args, void *aux OVS_UNUSED)
706 struct ds ds = DS_EMPTY_INITIALIZER;
710 lacp = lacp_find(args);
712 unixctl_command_reply(conn, 501, "no such lacp object");
716 ds_put_format(&ds, "lacp: %s\n", lacp->name);
718 ds_put_format(&ds, "\tstatus: %s", lacp->active ? "active" : "passive");
719 if (lacp->heartbeat) {
720 ds_put_cstr(&ds, " heartbeat");
722 if (lacp->negotiated) {
723 ds_put_cstr(&ds, " negotiated");
725 ds_put_cstr(&ds, "\n");
727 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
728 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
729 ds_put_cstr(&ds, "\taggregation key: ");
730 if (lacp->key_slave) {
731 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
733 ds_put_cstr(&ds, "none");
735 ds_put_cstr(&ds, "\n");
737 ds_put_cstr(&ds, "\tlacp_time: ");
738 switch (lacp->lacp_time) {
740 ds_put_cstr(&ds, "fast\n");
743 ds_put_cstr(&ds, "slow\n");
745 case LACP_TIME_CUSTOM:
746 ds_put_format(&ds, "custom (%lld)\n", lacp->custom_time);
749 ds_put_cstr(&ds, "unknown\n");
752 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
754 struct lacp_info actor;
756 slave_get_actor(slave, &actor);
757 switch (slave->status) {
765 status = "defaulted";
771 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
772 slave->attached ? "attached" : "detached");
773 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
774 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
776 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
777 ETH_ADDR_ARGS(actor.sys_id));
778 ds_put_format(&ds, "\tactor sys_priority: %u\n",
779 ntohs(actor.sys_priority));
780 ds_put_format(&ds, "\tactor port_id: %u\n",
781 ntohs(actor.port_id));
782 ds_put_format(&ds, "\tactor port_priority: %u\n",
783 ntohs(actor.port_priority));
784 ds_put_format(&ds, "\tactor key: %u\n",
786 ds_put_cstr(&ds, "\tactor state: ");
787 ds_put_lacp_state(&ds, actor.state);
788 ds_put_cstr(&ds, "\n\n");
790 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
791 ETH_ADDR_ARGS(slave->partner.sys_id));
792 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
793 ntohs(slave->partner.sys_priority));
794 ds_put_format(&ds, "\tpartner port_id: %u\n",
795 ntohs(slave->partner.port_id));
796 ds_put_format(&ds, "\tpartner port_priority: %u\n",
797 ntohs(slave->partner.port_priority));
798 ds_put_format(&ds, "\tpartner key: %u\n",
799 ntohs(slave->partner.key));
800 ds_put_cstr(&ds, "\tpartner state: ");
801 ds_put_lacp_state(&ds, slave->partner.state);
802 ds_put_cstr(&ds, "\n");
805 unixctl_command_reply(conn, 200, ds_cstr(&ds));