1 /* Copyright (c) 2011, 2012 Nicira, Inc.
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.
21 #include "dynamic-string.h"
26 #include "poll-loop.h"
33 VLOG_DEFINE_THIS_MODULE(lacp);
35 /* Masks for lacp_info state member. */
36 #define LACP_STATE_ACT 0x01 /* Activity. Active or passive? */
37 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
38 #define LACP_STATE_AGG 0x04 /* Aggregation. Is the link is bondable? */
39 #define LACP_STATE_SYNC 0x08 /* Synchronization. Is the link in up to date? */
40 #define LACP_STATE_COL 0x10 /* Collecting. Is the link receiving frames? */
41 #define LACP_STATE_DIST 0x20 /* Distributing. Is the link sending frames? */
42 #define LACP_STATE_DEF 0x40 /* Defaulted. Using default partner info? */
43 #define LACP_STATE_EXP 0x80 /* Expired. Using expired partner info? */
45 #define LACP_FAST_TIME_TX 1000 /* Fast transmission rate. */
46 #define LACP_SLOW_TIME_TX 30000 /* Slow transmission rate. */
47 #define LACP_RX_MULTIPLIER 3 /* Multiply by TX rate to get RX rate. */
49 #define LACP_INFO_LEN 15
51 ovs_be16 sys_priority; /* System priority. */
52 uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
53 ovs_be16 key; /* Operational key. */
54 ovs_be16 port_priority; /* Port priority. */
55 ovs_be16 port_id; /* Port ID. */
56 uint8_t state; /* State mask. See LACP_STATE macros. */
57 } __attribute__((packed));
58 BUILD_ASSERT_DECL(LACP_INFO_LEN == sizeof(struct lacp_info));
60 #define LACP_PDU_LEN 110
62 uint8_t subtype; /* Always 1. */
63 uint8_t version; /* Always 1. */
65 uint8_t actor_type; /* Always 1. */
66 uint8_t actor_len; /* Always 20. */
67 struct lacp_info actor; /* LACP actor information. */
68 uint8_t z1[3]; /* Reserved. Always 0. */
70 uint8_t partner_type; /* Always 2. */
71 uint8_t partner_len; /* Always 20. */
72 struct lacp_info partner; /* LACP partner information. */
73 uint8_t z2[3]; /* Reserved. Always 0. */
75 uint8_t collector_type; /* Always 3. */
76 uint8_t collector_len; /* Always 16. */
77 ovs_be16 collector_delay; /* Maximum collector delay. Set to UINT16_MAX. */
78 uint8_t z3[64]; /* Combination of several fields. Always 0. */
79 } __attribute__((packed));
80 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
85 LACP_CURRENT, /* Current State. Partner up to date. */
86 LACP_EXPIRED, /* Expired State. Partner out of date. */
87 LACP_DEFAULTED, /* Defaulted State. No partner. */
91 struct list node; /* Node in all_lacps list. */
92 char *name; /* Name of this lacp object. */
93 uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
94 uint16_t sys_priority; /* System Priority. */
95 bool active; /* Active or Passive. */
97 struct hmap slaves; /* Slaves this LACP object controls. */
98 struct slave *key_slave; /* Slave whose ID will be the aggregation key. */
100 bool fast; /* True if using fast probe interval. */
101 bool negotiated; /* True if LACP negotiations were successful. */
102 bool update; /* True if lacp_update() needs to be called. */
106 void *aux; /* Handle used to identify this slave. */
107 struct hmap_node node; /* Node in master's slaves map. */
109 struct lacp *lacp; /* LACP object containing this slave. */
110 uint16_t port_id; /* Port ID. */
111 uint16_t port_priority; /* Port Priority. */
112 uint16_t key; /* Aggregation Key. 0 if default. */
113 char *name; /* Name of this slave. */
115 enum slave_status status; /* Slave status. */
116 bool attached; /* Attached. Traffic may flow. */
117 struct lacp_info partner; /* Partner information. */
118 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
119 struct timer tx; /* Next message transmission timer. */
120 struct timer rx; /* Expected message receive timer. */
123 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
125 static void lacp_update_attached(struct lacp *);
127 static void slave_destroy(struct slave *);
128 static void slave_set_defaulted(struct slave *);
129 static void slave_set_expired(struct slave *);
130 static void slave_get_actor(struct slave *, struct lacp_info *actor);
131 static void slave_get_priority(struct slave *, struct lacp_info *priority);
132 static bool slave_may_tx(const struct slave *);
133 static struct slave *slave_lookup(const struct lacp *, const void *slave);
134 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
136 static unixctl_cb_func lacp_unixctl_show;
138 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
140 compose_lacp_pdu(const struct lacp_info *actor,
141 const struct lacp_info *partner, struct lacp_pdu *pdu)
143 memset(pdu, 0, sizeof *pdu);
152 pdu->partner_type = 2;
153 pdu->partner_len = 20;
154 pdu->partner = *partner;
156 pdu->collector_type = 3;
157 pdu->collector_len = 16;
158 pdu->collector_delay = htons(0);
161 /* Parses 'b' which represents a packet containing a LACP PDU. This function
162 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
163 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
165 static const struct lacp_pdu *
166 parse_lacp_packet(const struct ofpbuf *b)
168 const struct lacp_pdu *pdu;
170 pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
172 if (pdu && pdu->subtype == 1
173 && pdu->actor_type == 1 && pdu->actor_len == 20
174 && pdu->partner_type == 2 && pdu->partner_len == 20) {
181 /* LACP Protocol Implementation. */
183 /* Initializes the lacp module. */
187 unixctl_command_register("lacp/show", "[port]", 0, 1,
188 lacp_unixctl_show, NULL);
191 /* Creates a LACP object. */
197 lacp = xzalloc(sizeof *lacp);
198 hmap_init(&lacp->slaves);
199 list_push_back(&all_lacps, &lacp->node);
203 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
205 lacp_destroy(struct lacp *lacp)
208 struct slave *slave, *next;
210 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
211 slave_destroy(slave);
214 hmap_destroy(&lacp->slaves);
215 list_remove(&lacp->node);
221 /* Configures 'lacp' with settings from 's'. */
223 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
225 ovs_assert(!eth_addr_is_zero(s->id));
227 if (!lacp->name || strcmp(s->name, lacp->name)) {
229 lacp->name = xstrdup(s->name);
232 if (!eth_addr_equals(lacp->sys_id, s->id)
233 || lacp->sys_priority != s->priority) {
234 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
235 lacp->sys_priority = s->priority;
239 lacp->active = s->active;
240 lacp->fast = s->fast;
243 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
244 * configured for passive mode. */
246 lacp_is_active(const struct lacp *lacp)
251 /* Processes 'packet' which was received on 'slave_'. This function should be
252 * called on all packets received on 'slave_' with Ethernet Type ETH_TYPE_LACP.
255 lacp_process_packet(struct lacp *lacp, const void *slave_,
256 const struct ofpbuf *packet)
258 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
259 struct slave *slave = slave_lookup(lacp, slave_);
260 const struct lacp_pdu *pdu;
261 long long int tx_rate;
263 pdu = parse_lacp_packet(packet);
265 VLOG_WARN_RL(&rl, "%s: received an unparsable LACP PDU.", lacp->name);
269 slave->status = LACP_CURRENT;
270 tx_rate = lacp->fast ? LACP_FAST_TIME_TX : LACP_SLOW_TIME_TX;
271 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
273 slave->ntt_actor = pdu->partner;
275 /* Update our information about our partner if it's out of date. This may
276 * cause priorities to change so re-calculate attached status of all
278 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
280 slave->partner = pdu->actor;
284 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
286 lacp_status(const struct lacp *lacp)
289 return LACP_DISABLED;
290 } else if (lacp->negotiated) {
291 return LACP_NEGOTIATED;
293 return LACP_CONFIGURED;
297 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
298 * once per slave in a LACP managed bond. Should also be called whenever a
299 * slave's settings change. */
301 lacp_slave_register(struct lacp *lacp, void *slave_,
302 const struct lacp_slave_settings *s)
304 struct slave *slave = slave_lookup(lacp, slave_);
307 slave = xzalloc(sizeof *slave);
310 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
311 slave_set_defaulted(slave);
313 if (!lacp->key_slave) {
314 lacp->key_slave = slave;
318 if (!slave->name || strcmp(s->name, slave->name)) {
320 slave->name = xstrdup(s->name);
323 if (slave->port_id != s->id
324 || slave->port_priority != s->priority
325 || slave->key != s->key) {
326 slave->port_id = s->id;
327 slave->port_priority = s->priority;
332 if (lacp->active || lacp->negotiated) {
333 slave_set_expired(slave);
338 /* Unregisters 'slave_' with 'lacp'. */
340 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
342 struct slave *slave = slave_lookup(lacp, slave_);
345 slave_destroy(slave);
350 /* This function should be called whenever the carrier status of 'slave_' has
351 * changed. If 'lacp' is null, this function has no effect.*/
353 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
356 struct slave *slave = slave_lookup(lacp, slave_);
358 if (slave->status == LACP_CURRENT || slave->lacp->active) {
359 slave_set_expired(slave);
365 slave_may_enable__(struct slave *slave)
367 /* The slave may be enabled if it's attached to an aggregator and its
368 * partner is synchronized.*/
369 return slave->attached && (slave->partner.state & LACP_STATE_SYNC);
372 /* This function should be called before enabling 'slave_' to send or receive
373 * traffic. If it returns false, 'slave_' should not enabled. As a
374 * convenience, returns true if 'lacp' is NULL. */
376 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
379 return slave_may_enable__(slave_lookup(lacp, slave_));
385 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
386 * not being current, generally indicates a connectivity problem, or a
387 * misconfigured (or broken) partner. */
389 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
391 return slave_lookup(lacp, slave_)->status != LACP_DEFAULTED;
394 /* This function should be called periodically to update 'lacp'. */
396 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
400 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
401 if (timer_expired(&slave->rx)) {
402 if (slave->status == LACP_CURRENT) {
403 slave_set_expired(slave);
404 } else if (slave->status == LACP_EXPIRED) {
405 slave_set_defaulted(slave);
411 lacp_update_attached(lacp);
414 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
415 struct lacp_info actor;
417 if (!slave_may_tx(slave)) {
421 slave_get_actor(slave, &actor);
423 if (timer_expired(&slave->tx)
424 || !info_tx_equal(&actor, &slave->ntt_actor)) {
425 long long int duration;
428 slave->ntt_actor = actor;
429 compose_lacp_pdu(&actor, &slave->partner, &pdu);
430 send_pdu(slave->aux, &pdu, sizeof pdu);
432 duration = (slave->partner.state & LACP_STATE_TIME
434 : LACP_SLOW_TIME_TX);
436 timer_set_duration(&slave->tx, duration);
441 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
443 lacp_wait(struct lacp *lacp)
447 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
448 if (slave_may_tx(slave)) {
449 timer_wait(&slave->tx);
452 if (slave->status != LACP_DEFAULTED) {
453 timer_wait(&slave->rx);
458 /* Static Helpers. */
460 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
461 * negotiated parameter to true if any slaves are attachable. */
463 lacp_update_attached(struct lacp *lacp)
465 struct slave *lead, *slave;
466 struct lacp_info lead_pri;
467 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
469 lacp->update = false;
472 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
473 struct lacp_info pri;
475 slave->attached = false;
477 /* XXX: In the future allow users to configure the expected system ID.
478 * For now just special case loopback. */
479 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
480 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
481 "connected to its own bond", slave->name);
485 if (slave->status == LACP_DEFAULTED) {
489 slave->attached = true;
490 slave_get_priority(slave, &pri);
492 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
498 lacp->negotiated = lead != NULL;
501 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
502 if (lead->partner.key != slave->partner.key
503 || !eth_addr_equals(lead->partner.sys_id,
504 slave->partner.sys_id)) {
505 slave->attached = false;
512 slave_destroy(struct slave *slave)
515 struct lacp *lacp = slave->lacp;
518 hmap_remove(&lacp->slaves, &slave->node);
520 if (lacp->key_slave == slave) {
521 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
524 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
526 lacp->key_slave = NULL;
536 slave_set_defaulted(struct slave *slave)
538 memset(&slave->partner, 0, sizeof slave->partner);
540 slave->lacp->update = true;
541 slave->status = LACP_DEFAULTED;
545 slave_set_expired(struct slave *slave)
547 slave->status = LACP_EXPIRED;
548 slave->partner.state |= LACP_STATE_TIME;
549 slave->partner.state &= ~LACP_STATE_SYNC;
551 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX);
555 slave_get_actor(struct slave *slave, struct lacp_info *actor)
557 struct lacp *lacp = slave->lacp;
562 state |= LACP_STATE_ACT;
566 state |= LACP_STATE_TIME;
569 if (slave->attached) {
570 state |= LACP_STATE_SYNC;
573 if (slave->status == LACP_DEFAULTED) {
574 state |= LACP_STATE_DEF;
577 if (slave->status == LACP_EXPIRED) {
578 state |= LACP_STATE_EXP;
581 if (hmap_count(&lacp->slaves) > 1) {
582 state |= LACP_STATE_AGG;
585 if (slave->attached || !lacp->negotiated) {
586 state |= LACP_STATE_COL | LACP_STATE_DIST;
589 key = lacp->key_slave->key;
591 key = lacp->key_slave->port_id;
594 actor->state = state;
595 actor->key = htons(key);
596 actor->port_priority = htons(slave->port_priority);
597 actor->port_id = htons(slave->port_id);
598 actor->sys_priority = htons(lacp->sys_priority);
599 memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
602 /* Given 'slave', populates 'priority' with data representing its LACP link
603 * priority. If two priority objects populated by this function are compared
604 * using memcmp, the higher priority link will be less than the lower priority
607 slave_get_priority(struct slave *slave, struct lacp_info *priority)
609 uint16_t partner_priority, actor_priority;
611 /* Choose the lacp_info of the higher priority system by comparing their
612 * system priorities and mac addresses. */
613 actor_priority = slave->lacp->sys_priority;
614 partner_priority = ntohs(slave->partner.sys_priority);
615 if (actor_priority < partner_priority) {
616 slave_get_actor(slave, priority);
617 } else if (partner_priority < actor_priority) {
618 *priority = slave->partner;
619 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
620 slave->partner.sys_id) < 0) {
621 slave_get_actor(slave, priority);
623 *priority = slave->partner;
626 /* Key and state are not used in priority comparisons. */
632 slave_may_tx(const struct slave *slave)
634 return slave->lacp->active || slave->status != LACP_DEFAULTED;
637 static struct slave *
638 slave_lookup(const struct lacp *lacp, const void *slave_)
642 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
644 if (slave->aux == slave_) {
652 /* Two lacp_info structures are tx_equal if and only if they do not differ in
653 * ways which would require a lacp_pdu transmission. */
655 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
658 /* LACP specification dictates that we transmit whenever the actor and
659 * remote_actor differ in the following fields: Port, Port Priority,
660 * System, System Priority, Aggregation Key, Activity State, Timeout State,
661 * Sync State, and Aggregation State. The state flags are most likely to
662 * change so are checked first. */
663 return !((a->state ^ b->state) & (LACP_STATE_ACT
667 && a->port_id == b->port_id
668 && a->port_priority == b->port_priority
670 && a->sys_priority == b->sys_priority
671 && eth_addr_equals(a->sys_id, b->sys_id);
675 lacp_find(const char *name)
679 LIST_FOR_EACH (lacp, node, &all_lacps) {
680 if (!strcmp(lacp->name, name)) {
689 ds_put_lacp_state(struct ds *ds, uint8_t state)
691 if (state & LACP_STATE_ACT) {
692 ds_put_cstr(ds, " activity");
695 if (state & LACP_STATE_TIME) {
696 ds_put_cstr(ds, " timeout");
699 if (state & LACP_STATE_AGG) {
700 ds_put_cstr(ds, " aggregation");
703 if (state & LACP_STATE_SYNC) {
704 ds_put_cstr(ds, " synchronized");
707 if (state & LACP_STATE_COL) {
708 ds_put_cstr(ds, " collecting");
711 if (state & LACP_STATE_DIST) {
712 ds_put_cstr(ds, " distributing");
715 if (state & LACP_STATE_DEF) {
716 ds_put_cstr(ds, " defaulted");
719 if (state & LACP_STATE_EXP) {
720 ds_put_cstr(ds, " expired");
725 lacp_print_details(struct ds *ds, struct lacp *lacp)
727 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
728 const struct shash_node **sorted_slaves = NULL;
733 ds_put_format(ds, "---- %s ----\n", lacp->name);
734 ds_put_format(ds, "\tstatus: %s", lacp->active ? "active" : "passive");
735 if (lacp->negotiated) {
736 ds_put_cstr(ds, " negotiated");
738 ds_put_cstr(ds, "\n");
740 ds_put_format(ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
741 ds_put_format(ds, "\tsys_priority: %u\n", lacp->sys_priority);
742 ds_put_cstr(ds, "\taggregation key: ");
743 if (lacp->key_slave) {
744 ds_put_format(ds, "%u", lacp->key_slave->key
745 ? lacp->key_slave->key
746 : lacp->key_slave->port_id);
748 ds_put_cstr(ds, "none");
750 ds_put_cstr(ds, "\n");
752 ds_put_cstr(ds, "\tlacp_time: ");
754 ds_put_cstr(ds, "fast\n");
756 ds_put_cstr(ds, "slow\n");
759 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
760 shash_add(&slave_shash, slave->name, slave);
762 sorted_slaves = shash_sort(&slave_shash);
764 for (i = 0; i < shash_count(&slave_shash); i++) {
766 struct lacp_info actor;
768 slave = sorted_slaves[i]->data;
769 slave_get_actor(slave, &actor);
770 switch (slave->status) {
778 status = "defaulted";
784 ds_put_format(ds, "\nslave: %s: %s %s\n", slave->name, status,
785 slave->attached ? "attached" : "detached");
786 ds_put_format(ds, "\tport_id: %u\n", slave->port_id);
787 ds_put_format(ds, "\tport_priority: %u\n", slave->port_priority);
788 ds_put_format(ds, "\tmay_enable: %s\n", (slave_may_enable__(slave)
789 ? "true" : "false"));
791 ds_put_format(ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
792 ETH_ADDR_ARGS(actor.sys_id));
793 ds_put_format(ds, "\tactor sys_priority: %u\n",
794 ntohs(actor.sys_priority));
795 ds_put_format(ds, "\tactor port_id: %u\n",
796 ntohs(actor.port_id));
797 ds_put_format(ds, "\tactor port_priority: %u\n",
798 ntohs(actor.port_priority));
799 ds_put_format(ds, "\tactor key: %u\n",
801 ds_put_cstr(ds, "\tactor state:");
802 ds_put_lacp_state(ds, actor.state);
803 ds_put_cstr(ds, "\n\n");
805 ds_put_format(ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
806 ETH_ADDR_ARGS(slave->partner.sys_id));
807 ds_put_format(ds, "\tpartner sys_priority: %u\n",
808 ntohs(slave->partner.sys_priority));
809 ds_put_format(ds, "\tpartner port_id: %u\n",
810 ntohs(slave->partner.port_id));
811 ds_put_format(ds, "\tpartner port_priority: %u\n",
812 ntohs(slave->partner.port_priority));
813 ds_put_format(ds, "\tpartner key: %u\n",
814 ntohs(slave->partner.key));
815 ds_put_cstr(ds, "\tpartner state:");
816 ds_put_lacp_state(ds, slave->partner.state);
817 ds_put_cstr(ds, "\n");
820 shash_destroy(&slave_shash);
825 lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
826 void *aux OVS_UNUSED)
828 struct ds ds = DS_EMPTY_INITIALIZER;
832 lacp = lacp_find(argv[1]);
834 unixctl_command_reply_error(conn, "no such lacp object");
837 lacp_print_details(&ds, lacp);
839 LIST_FOR_EACH (lacp, node, &all_lacps) {
840 lacp_print_details(&ds, lacp);
844 unixctl_command_reply(conn, ds_cstr(&ds));