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 strict; /* True if in strict mode. */
54 bool negotiated; /* True if LACP negotiations were successful. */
55 bool update; /* True if lacp_update() needs to be called. */
56 bool force_agg; /* Forces LACP_STATE_AGG bit on all slaves. */
60 void *aux; /* Handle used to identify this slave. */
61 struct hmap_node node; /* Node in master's slaves map. */
63 struct lacp *lacp; /* LACP object containing this slave. */
64 uint16_t port_id; /* Port ID. */
65 uint16_t port_priority; /* Port Priority. */
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->strict != s->strict) {
186 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
187 lacp->sys_priority = s->priority;
188 lacp->strict = s->strict;
192 lacp->active = s->active;
193 lacp->lacp_time = s->lacp_time;
194 lacp->force_agg = s->force_agg;
195 lacp->custom_time = MAX(TIME_UPDATE_INTERVAL, s->custom_time);
198 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
199 * configured for passive mode. */
201 lacp_is_active(const struct lacp *lacp)
206 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
207 * should be called on all packets received on 'slave_' with Ethernet Type
208 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
210 lacp_process_pdu(struct lacp *lacp, const void *slave_,
211 const struct lacp_pdu *pdu)
213 struct slave *slave = slave_lookup(lacp, slave_);
214 long long int tx_rate;
216 switch (lacp->lacp_time) {
218 tx_rate = LACP_FAST_TIME_TX;
221 tx_rate = LACP_SLOW_TIME_TX;
223 case LACP_TIME_CUSTOM:
224 tx_rate = lacp->custom_time;
226 default: NOT_REACHED();
229 slave->status = LACP_CURRENT;
230 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
232 slave->ntt_actor = pdu->partner;
234 /* Update our information about our partner if it's out of date. This may
235 * cause priorities to change so re-calculate attached status of all
237 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
239 slave->partner = pdu->actor;
243 /* Returns true if 'lacp' has successfully negotiated with its partner. False
244 * if 'lacp' is NULL. */
246 lacp_negotiated(const struct lacp *lacp)
248 return lacp ? lacp->negotiated : false;
251 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
252 * once per slave in a LACP managed bond. Should also be called whenever a
253 * slave's settings change. */
255 lacp_slave_register(struct lacp *lacp, void *slave_,
256 const struct lacp_slave_settings *s)
258 struct slave *slave = slave_lookup(lacp, slave_);
261 slave = xzalloc(sizeof *slave);
264 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
265 slave_set_defaulted(slave);
267 if (!lacp->key_slave) {
268 lacp->key_slave = slave;
272 if (!slave->name || strcmp(s->name, slave->name)) {
274 slave->name = xstrdup(s->name);
277 if (slave->port_id != s->id || slave->port_priority != s->priority) {
278 slave->port_id = s->id;
279 slave->port_priority = s->priority;
283 if (lacp->active || lacp->negotiated) {
284 slave_set_expired(slave);
289 /* Unregisters 'slave_' with 'lacp'. */
291 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
293 struct slave *slave = slave_lookup(lacp, slave_);
296 slave_destroy(slave);
301 /* This function should be called whenever the carrier status of 'slave_' has
304 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
306 struct slave *slave = slave_lookup(lacp, slave_);
308 if (slave->status == LACP_CURRENT || slave->lacp->active) {
309 slave_set_expired(slave);
313 /* This function should be called before enabling 'slave_' to send or receive
314 * traffic. If it returns false, 'slave_' should not enabled. As a
315 * convenience, returns true if 'lacp' is NULL. */
317 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
320 struct slave *slave = slave_lookup(lacp, slave_);
322 /* The slave may be enabled if it's attached to an aggregator and its
323 * partner is synchronized. The only exception is defaulted slaves.
324 * They are not required to have synchronized partners because they
325 * have no partners at all. They will only be attached if negotiations
326 * failed on all slaves in the bond. */
327 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
328 || slave->status == LACP_DEFAULTED);
334 /* Returns the port ID used for 'slave_' in LACP communications. */
336 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
338 struct slave *slave = slave_lookup(lacp, slave_);
339 return slave->port_id;
342 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
343 * not being current, generally indicates a connectivity problem, or a
344 * misconfigured (or broken) partner. */
346 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
348 return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
351 /* This function should be called periodically to update 'lacp'. */
353 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
357 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
358 if (timer_expired(&slave->rx)) {
359 if (slave->status == LACP_CURRENT) {
360 slave_set_expired(slave);
361 } else if (slave->status == LACP_EXPIRED) {
362 slave_set_defaulted(slave);
368 lacp_update_attached(lacp);
371 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
373 struct lacp_info actor;
375 if (!slave_may_tx(slave)) {
379 slave_get_actor(slave, &actor);
381 if (timer_expired(&slave->tx)
382 || !info_tx_equal(&actor, &slave->ntt_actor)) {
383 long long int duration;
385 slave->ntt_actor = actor;
386 compose_lacp_pdu(&actor, &slave->partner, &pdu);
387 send_pdu(slave->aux, &pdu);
389 if (lacp->lacp_time == LACP_TIME_CUSTOM) {
390 duration = lacp->custom_time;
392 duration = (slave->partner.state & LACP_STATE_TIME
394 : LACP_SLOW_TIME_TX);
397 timer_set_duration(&slave->tx, duration);
402 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
404 lacp_wait(struct lacp *lacp)
408 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
409 if (slave_may_tx(slave)) {
410 timer_wait(&slave->tx);
413 if (slave->status != LACP_DEFAULTED) {
414 timer_wait(&slave->rx);
419 /* Static Helpers. */
421 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
422 * negotiated parameter to true if any slaves are attachable. */
424 lacp_update_attached(struct lacp *lacp)
426 struct slave *lead, *slave;
427 struct lacp_info lead_pri;
428 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
430 lacp->update = false;
433 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
434 struct lacp_info pri;
436 slave->attached = true;
438 /* XXX: In the future allow users to configure the expected system ID.
439 * For now just special case loopback. */
440 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
441 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
442 "connected to its own bond", slave->name);
443 slave->attached = false;
447 if (slave->status == LACP_DEFAULTED) {
451 slave_get_priority(slave, &pri);
453 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
459 lacp->negotiated = lead != NULL;
462 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
463 if (slave->status == LACP_DEFAULTED
464 || lead->partner.key != slave->partner.key
465 || !eth_addr_equals(lead->partner.sys_id,
466 slave->partner.sys_id)) {
467 slave->attached = false;
470 } else if (lacp->strict) {
471 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
472 slave->attached = false;
478 slave_destroy(struct slave *slave)
481 struct lacp *lacp = slave->lacp;
484 hmap_remove(&lacp->slaves, &slave->node);
486 if (lacp->key_slave == slave) {
487 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
490 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
492 lacp->key_slave = NULL;
502 slave_set_defaulted(struct slave *slave)
504 memset(&slave->partner, 0, sizeof slave->partner);
506 slave->lacp->update = true;
507 slave->status = LACP_DEFAULTED;
511 slave_set_expired(struct slave *slave)
513 struct lacp *lacp = slave->lacp;
515 slave->status = LACP_EXPIRED;
516 slave->partner.state |= LACP_STATE_TIME;
517 slave->partner.state &= ~LACP_STATE_SYNC;
519 /* The spec says we should wait LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX.
520 * This doesn't make sense when using custom times which can be much
521 * smaller than LACP_FAST_TIME. */
522 timer_set_duration(&slave->rx, (lacp->lacp_time == LACP_TIME_CUSTOM
524 : LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX));
528 slave_get_actor(struct slave *slave, struct lacp_info *actor)
530 struct lacp *lacp = slave->lacp;
534 state |= LACP_STATE_ACT;
537 if (lacp->lacp_time != LACP_TIME_SLOW) {
538 state |= LACP_STATE_TIME;
541 if (slave->attached) {
542 state |= LACP_STATE_SYNC;
545 if (slave->status == LACP_DEFAULTED) {
546 state |= LACP_STATE_DEF;
549 if (slave->status == LACP_EXPIRED) {
550 state |= LACP_STATE_EXP;
553 if (lacp->force_agg || hmap_count(&lacp->slaves) > 1) {
554 state |= LACP_STATE_AGG;
557 if (slave->attached || !lacp->negotiated) {
558 state |= LACP_STATE_COL | LACP_STATE_DIST;
561 actor->state = state;
562 actor->key = htons(lacp->key_slave->port_id);
563 actor->port_priority = htons(slave->port_priority);
564 actor->port_id = htons(slave->port_id);
565 actor->sys_priority = htons(lacp->sys_priority);
566 memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
569 /* Given 'slave', populates 'priority' with data representing its LACP link
570 * priority. If two priority objects populated by this function are compared
571 * using memcmp, the higher priority link will be less than the lower priority
574 slave_get_priority(struct slave *slave, struct lacp_info *priority)
576 uint16_t partner_priority, actor_priority;
578 /* Choose the lacp_info of the higher priority system by comparing their
579 * system priorities and mac addresses. */
580 actor_priority = slave->lacp->sys_priority;
581 partner_priority = ntohs(slave->partner.sys_priority);
582 if (actor_priority < partner_priority) {
583 slave_get_actor(slave, priority);
584 } else if (partner_priority < actor_priority) {
585 *priority = slave->partner;
586 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
587 slave->partner.sys_id) < 0) {
588 slave_get_actor(slave, priority);
590 *priority = slave->partner;
593 /* Key and state are not used in priority comparisons. */
599 slave_may_tx(const struct slave *slave)
601 return slave->lacp->active || slave->status != LACP_DEFAULTED;
604 static struct slave *
605 slave_lookup(const struct lacp *lacp, const void *slave_)
609 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
611 if (slave->aux == slave_) {
619 /* Two lacp_info structures are tx_equal if and only if they do not differ in
620 * ways which would require a lacp_pdu transmission. */
622 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
625 /* LACP specification dictates that we transmit whenever the actor and
626 * remote_actor differ in the following fields: Port, Port Priority,
627 * System, System Priority, Aggregation Key, Activity State, Timeout State,
628 * Sync State, and Aggregation State. The state flags are most likely to
629 * change so are checked first. */
630 return !((a->state ^ b->state) & (LACP_STATE_ACT
634 && a->port_id == b->port_id
635 && a->port_priority == b->port_priority
637 && a->sys_priority == b->sys_priority
638 && eth_addr_equals(a->sys_id, b->sys_id);
642 lacp_find(const char *name)
646 LIST_FOR_EACH (lacp, node, &all_lacps) {
647 if (!strcmp(lacp->name, name)) {
656 ds_put_lacp_state(struct ds *ds, uint8_t state)
658 if (state & LACP_STATE_ACT) {
659 ds_put_cstr(ds, "activity ");
662 if (state & LACP_STATE_TIME) {
663 ds_put_cstr(ds, "timeout ");
666 if (state & LACP_STATE_AGG) {
667 ds_put_cstr(ds, "aggregation ");
670 if (state & LACP_STATE_SYNC) {
671 ds_put_cstr(ds, "synchronized ");
674 if (state & LACP_STATE_COL) {
675 ds_put_cstr(ds, "collecting ");
678 if (state & LACP_STATE_DIST) {
679 ds_put_cstr(ds, "distributing ");
682 if (state & LACP_STATE_DEF) {
683 ds_put_cstr(ds, "defaulted ");
686 if (state & LACP_STATE_EXP) {
687 ds_put_cstr(ds, "expired ");
692 lacp_unixctl_show(struct unixctl_conn *conn,
693 const char *args, void *aux OVS_UNUSED)
695 struct ds ds = DS_EMPTY_INITIALIZER;
699 lacp = lacp_find(args);
701 unixctl_command_reply(conn, 501, "no such lacp object");
705 ds_put_format(&ds, "lacp: %s\n", lacp->name);
707 ds_put_format(&ds, "\tstatus: %s", lacp->active ? "active" : "passive");
709 ds_put_cstr(&ds, " strict");
711 if (lacp->negotiated) {
712 ds_put_cstr(&ds, " negotiated");
714 ds_put_cstr(&ds, "\n");
716 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
717 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
718 ds_put_cstr(&ds, "\taggregation key: ");
719 if (lacp->key_slave) {
720 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
722 ds_put_cstr(&ds, "none");
724 ds_put_cstr(&ds, "\n");
726 ds_put_cstr(&ds, "\tlacp_time: ");
727 switch (lacp->lacp_time) {
729 ds_put_cstr(&ds, "fast\n");
732 ds_put_cstr(&ds, "slow\n");
734 case LACP_TIME_CUSTOM:
735 ds_put_format(&ds, "custom (%lld)\n", lacp->custom_time);
738 ds_put_cstr(&ds, "unknown\n");
741 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
743 struct lacp_info actor;
745 slave_get_actor(slave, &actor);
746 switch (slave->status) {
754 status = "defaulted";
760 ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
761 slave->attached ? "attached" : "detached");
762 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
763 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
765 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
766 ETH_ADDR_ARGS(actor.sys_id));
767 ds_put_format(&ds, "\tactor sys_priority: %u\n",
768 ntohs(actor.sys_priority));
769 ds_put_format(&ds, "\tactor port_id: %u\n",
770 ntohs(actor.port_id));
771 ds_put_format(&ds, "\tactor port_priority: %u\n",
772 ntohs(actor.port_priority));
773 ds_put_format(&ds, "\tactor key: %u\n",
775 ds_put_cstr(&ds, "\tactor state: ");
776 ds_put_lacp_state(&ds, actor.state);
777 ds_put_cstr(&ds, "\n\n");
779 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
780 ETH_ADDR_ARGS(slave->partner.sys_id));
781 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
782 ntohs(slave->partner.sys_priority));
783 ds_put_format(&ds, "\tpartner port_id: %u\n",
784 ntohs(slave->partner.port_id));
785 ds_put_format(&ds, "\tpartner port_priority: %u\n",
786 ntohs(slave->partner.port_priority));
787 ds_put_format(&ds, "\tpartner key: %u\n",
788 ntohs(slave->partner.key));
789 ds_put_cstr(&ds, "\tpartner state: ");
790 ds_put_lacp_state(&ds, slave->partner.state);
791 ds_put_cstr(&ds, "\n");
794 unixctl_command_reply(conn, 200, ds_cstr(&ds));