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. */
108 void *aux; /* Handle used to identify this slave. */
109 struct hmap_node node; /* Node in master's slaves map. */
111 struct lacp *lacp; /* LACP object containing this slave. */
112 uint16_t port_id; /* Port ID. */
113 uint16_t port_priority; /* Port Priority. */
114 uint16_t key; /* Aggregation Key. 0 if default. */
115 char *name; /* Name of this slave. */
117 enum slave_status status; /* Slave status. */
118 bool attached; /* Attached. Traffic may flow. */
119 struct lacp_info partner; /* Partner information. */
120 struct lacp_info ntt_actor; /* Used to decide if we Need To Transmit. */
121 struct timer tx; /* Next message transmission timer. */
122 struct timer rx; /* Expected message receive timer. */
125 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
127 static void lacp_update_attached(struct lacp *);
129 static void slave_destroy(struct slave *);
130 static void slave_set_defaulted(struct slave *);
131 static void slave_set_expired(struct slave *);
132 static void slave_get_actor(struct slave *, struct lacp_info *actor);
133 static void slave_get_priority(struct slave *, struct lacp_info *priority);
134 static bool slave_may_tx(const struct slave *);
135 static struct slave *slave_lookup(const struct lacp *, const void *slave);
136 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
138 static unixctl_cb_func lacp_unixctl_show;
140 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
142 compose_lacp_pdu(const struct lacp_info *actor,
143 const struct lacp_info *partner, struct lacp_pdu *pdu)
145 memset(pdu, 0, sizeof *pdu);
154 pdu->partner_type = 2;
155 pdu->partner_len = 20;
156 pdu->partner = *partner;
158 pdu->collector_type = 3;
159 pdu->collector_len = 16;
160 pdu->collector_delay = htons(0);
163 /* Parses 'b' which represents a packet containing a LACP PDU. This function
164 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
165 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
167 static const struct lacp_pdu *
168 parse_lacp_packet(const struct ofpbuf *b)
170 const struct lacp_pdu *pdu;
172 pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
174 if (pdu && pdu->subtype == 1
175 && pdu->actor_type == 1 && pdu->actor_len == 20
176 && pdu->partner_type == 2 && pdu->partner_len == 20) {
183 /* LACP Protocol Implementation. */
185 /* Initializes the lacp module. */
189 unixctl_command_register("lacp/show", "[port]", 0, 1,
190 lacp_unixctl_show, NULL);
193 /* Creates a LACP object. */
199 lacp = xzalloc(sizeof *lacp);
200 hmap_init(&lacp->slaves);
201 list_push_back(&all_lacps, &lacp->node);
207 lacp_ref(const struct lacp *lacp_)
209 struct lacp *lacp = CONST_CAST(struct lacp *, lacp_);
211 ovs_assert(lacp->ref_cnt > 0);
217 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
219 lacp_unref(struct lacp *lacp)
225 ovs_assert(lacp->ref_cnt > 0);
226 if (!--lacp->ref_cnt) {
227 struct slave *slave, *next;
229 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
230 slave_destroy(slave);
233 hmap_destroy(&lacp->slaves);
234 list_remove(&lacp->node);
240 /* Configures 'lacp' with settings from 's'. */
242 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
244 ovs_assert(!eth_addr_is_zero(s->id));
246 if (!lacp->name || strcmp(s->name, lacp->name)) {
248 lacp->name = xstrdup(s->name);
251 if (!eth_addr_equals(lacp->sys_id, s->id)
252 || lacp->sys_priority != s->priority) {
253 memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
254 lacp->sys_priority = s->priority;
258 lacp->active = s->active;
259 lacp->fast = s->fast;
262 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
263 * configured for passive mode. */
265 lacp_is_active(const struct lacp *lacp)
270 /* Processes 'packet' which was received on 'slave_'. This function should be
271 * called on all packets received on 'slave_' with Ethernet Type ETH_TYPE_LACP.
274 lacp_process_packet(struct lacp *lacp, const void *slave_,
275 const struct ofpbuf *packet)
277 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
278 struct slave *slave = slave_lookup(lacp, slave_);
279 const struct lacp_pdu *pdu;
280 long long int tx_rate;
282 pdu = parse_lacp_packet(packet);
284 VLOG_WARN_RL(&rl, "%s: received an unparsable LACP PDU.", lacp->name);
288 slave->status = LACP_CURRENT;
289 tx_rate = lacp->fast ? LACP_FAST_TIME_TX : LACP_SLOW_TIME_TX;
290 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * tx_rate);
292 slave->ntt_actor = pdu->partner;
294 /* Update our information about our partner if it's out of date. This may
295 * cause priorities to change so re-calculate attached status of all
297 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
299 slave->partner = pdu->actor;
303 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
305 lacp_status(const struct lacp *lacp)
308 return LACP_DISABLED;
309 } else if (lacp->negotiated) {
310 return LACP_NEGOTIATED;
312 return LACP_CONFIGURED;
316 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
317 * once per slave in a LACP managed bond. Should also be called whenever a
318 * slave's settings change. */
320 lacp_slave_register(struct lacp *lacp, void *slave_,
321 const struct lacp_slave_settings *s)
323 struct slave *slave = slave_lookup(lacp, slave_);
326 slave = xzalloc(sizeof *slave);
329 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
330 slave_set_defaulted(slave);
332 if (!lacp->key_slave) {
333 lacp->key_slave = slave;
337 if (!slave->name || strcmp(s->name, slave->name)) {
339 slave->name = xstrdup(s->name);
342 if (slave->port_id != s->id
343 || slave->port_priority != s->priority
344 || slave->key != s->key) {
345 slave->port_id = s->id;
346 slave->port_priority = s->priority;
351 if (lacp->active || lacp->negotiated) {
352 slave_set_expired(slave);
357 /* Unregisters 'slave_' with 'lacp'. */
359 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
361 struct slave *slave = slave_lookup(lacp, slave_);
364 slave_destroy(slave);
369 /* This function should be called whenever the carrier status of 'slave_' has
370 * changed. If 'lacp' is null, this function has no effect.*/
372 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
375 struct slave *slave = slave_lookup(lacp, slave_);
377 if (slave->status == LACP_CURRENT || slave->lacp->active) {
378 slave_set_expired(slave);
384 slave_may_enable__(struct slave *slave)
386 /* The slave may be enabled if it's attached to an aggregator and its
387 * partner is synchronized.*/
388 return slave->attached && (slave->partner.state & LACP_STATE_SYNC);
391 /* This function should be called before enabling 'slave_' to send or receive
392 * traffic. If it returns false, 'slave_' should not enabled. As a
393 * convenience, returns true if 'lacp' is NULL. */
395 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
398 return slave_may_enable__(slave_lookup(lacp, slave_));
404 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
405 * not being current, generally indicates a connectivity problem, or a
406 * misconfigured (or broken) partner. */
408 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
410 return slave_lookup(lacp, slave_)->status != LACP_DEFAULTED;
413 /* This function should be called periodically to update 'lacp'. */
415 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
419 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
420 if (timer_expired(&slave->rx)) {
421 if (slave->status == LACP_CURRENT) {
422 slave_set_expired(slave);
423 } else if (slave->status == LACP_EXPIRED) {
424 slave_set_defaulted(slave);
430 lacp_update_attached(lacp);
433 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
434 struct lacp_info actor;
436 if (!slave_may_tx(slave)) {
440 slave_get_actor(slave, &actor);
442 if (timer_expired(&slave->tx)
443 || !info_tx_equal(&actor, &slave->ntt_actor)) {
444 long long int duration;
447 slave->ntt_actor = actor;
448 compose_lacp_pdu(&actor, &slave->partner, &pdu);
449 send_pdu(slave->aux, &pdu, sizeof pdu);
451 duration = (slave->partner.state & LACP_STATE_TIME
453 : LACP_SLOW_TIME_TX);
455 timer_set_duration(&slave->tx, duration);
460 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
462 lacp_wait(struct lacp *lacp)
466 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
467 if (slave_may_tx(slave)) {
468 timer_wait(&slave->tx);
471 if (slave->status != LACP_DEFAULTED) {
472 timer_wait(&slave->rx);
477 /* Static Helpers. */
479 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
480 * negotiated parameter to true if any slaves are attachable. */
482 lacp_update_attached(struct lacp *lacp)
484 struct slave *lead, *slave;
485 struct lacp_info lead_pri;
486 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
488 lacp->update = false;
491 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
492 struct lacp_info pri;
494 slave->attached = false;
496 /* XXX: In the future allow users to configure the expected system ID.
497 * For now just special case loopback. */
498 if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
499 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
500 "connected to its own bond", slave->name);
504 if (slave->status == LACP_DEFAULTED) {
508 slave->attached = true;
509 slave_get_priority(slave, &pri);
511 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
517 lacp->negotiated = lead != NULL;
520 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
521 if (lead->partner.key != slave->partner.key
522 || !eth_addr_equals(lead->partner.sys_id,
523 slave->partner.sys_id)) {
524 slave->attached = false;
531 slave_destroy(struct slave *slave)
534 struct lacp *lacp = slave->lacp;
537 hmap_remove(&lacp->slaves, &slave->node);
539 if (lacp->key_slave == slave) {
540 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
543 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
545 lacp->key_slave = NULL;
555 slave_set_defaulted(struct slave *slave)
557 memset(&slave->partner, 0, sizeof slave->partner);
559 slave->lacp->update = true;
560 slave->status = LACP_DEFAULTED;
564 slave_set_expired(struct slave *slave)
566 slave->status = LACP_EXPIRED;
567 slave->partner.state |= LACP_STATE_TIME;
568 slave->partner.state &= ~LACP_STATE_SYNC;
570 timer_set_duration(&slave->rx, LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX);
574 slave_get_actor(struct slave *slave, struct lacp_info *actor)
576 struct lacp *lacp = slave->lacp;
581 state |= LACP_STATE_ACT;
585 state |= LACP_STATE_TIME;
588 if (slave->attached) {
589 state |= LACP_STATE_SYNC;
592 if (slave->status == LACP_DEFAULTED) {
593 state |= LACP_STATE_DEF;
596 if (slave->status == LACP_EXPIRED) {
597 state |= LACP_STATE_EXP;
600 if (hmap_count(&lacp->slaves) > 1) {
601 state |= LACP_STATE_AGG;
604 if (slave->attached || !lacp->negotiated) {
605 state |= LACP_STATE_COL | LACP_STATE_DIST;
608 key = lacp->key_slave->key;
610 key = lacp->key_slave->port_id;
613 actor->state = state;
614 actor->key = htons(key);
615 actor->port_priority = htons(slave->port_priority);
616 actor->port_id = htons(slave->port_id);
617 actor->sys_priority = htons(lacp->sys_priority);
618 memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
621 /* Given 'slave', populates 'priority' with data representing its LACP link
622 * priority. If two priority objects populated by this function are compared
623 * using memcmp, the higher priority link will be less than the lower priority
626 slave_get_priority(struct slave *slave, struct lacp_info *priority)
628 uint16_t partner_priority, actor_priority;
630 /* Choose the lacp_info of the higher priority system by comparing their
631 * system priorities and mac addresses. */
632 actor_priority = slave->lacp->sys_priority;
633 partner_priority = ntohs(slave->partner.sys_priority);
634 if (actor_priority < partner_priority) {
635 slave_get_actor(slave, priority);
636 } else if (partner_priority < actor_priority) {
637 *priority = slave->partner;
638 } else if (eth_addr_compare_3way(slave->lacp->sys_id,
639 slave->partner.sys_id) < 0) {
640 slave_get_actor(slave, priority);
642 *priority = slave->partner;
645 /* Key and state are not used in priority comparisons. */
651 slave_may_tx(const struct slave *slave)
653 return slave->lacp->active || slave->status != LACP_DEFAULTED;
656 static struct slave *
657 slave_lookup(const struct lacp *lacp, const void *slave_)
661 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
663 if (slave->aux == slave_) {
671 /* Two lacp_info structures are tx_equal if and only if they do not differ in
672 * ways which would require a lacp_pdu transmission. */
674 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
677 /* LACP specification dictates that we transmit whenever the actor and
678 * remote_actor differ in the following fields: Port, Port Priority,
679 * System, System Priority, Aggregation Key, Activity State, Timeout State,
680 * Sync State, and Aggregation State. The state flags are most likely to
681 * change so are checked first. */
682 return !((a->state ^ b->state) & (LACP_STATE_ACT
686 && a->port_id == b->port_id
687 && a->port_priority == b->port_priority
689 && a->sys_priority == b->sys_priority
690 && eth_addr_equals(a->sys_id, b->sys_id);
694 lacp_find(const char *name)
698 LIST_FOR_EACH (lacp, node, &all_lacps) {
699 if (!strcmp(lacp->name, name)) {
708 ds_put_lacp_state(struct ds *ds, uint8_t state)
710 if (state & LACP_STATE_ACT) {
711 ds_put_cstr(ds, " activity");
714 if (state & LACP_STATE_TIME) {
715 ds_put_cstr(ds, " timeout");
718 if (state & LACP_STATE_AGG) {
719 ds_put_cstr(ds, " aggregation");
722 if (state & LACP_STATE_SYNC) {
723 ds_put_cstr(ds, " synchronized");
726 if (state & LACP_STATE_COL) {
727 ds_put_cstr(ds, " collecting");
730 if (state & LACP_STATE_DIST) {
731 ds_put_cstr(ds, " distributing");
734 if (state & LACP_STATE_DEF) {
735 ds_put_cstr(ds, " defaulted");
738 if (state & LACP_STATE_EXP) {
739 ds_put_cstr(ds, " expired");
744 lacp_print_details(struct ds *ds, struct lacp *lacp)
746 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
747 const struct shash_node **sorted_slaves = NULL;
752 ds_put_format(ds, "---- %s ----\n", lacp->name);
753 ds_put_format(ds, "\tstatus: %s", lacp->active ? "active" : "passive");
754 if (lacp->negotiated) {
755 ds_put_cstr(ds, " negotiated");
757 ds_put_cstr(ds, "\n");
759 ds_put_format(ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
760 ds_put_format(ds, "\tsys_priority: %u\n", lacp->sys_priority);
761 ds_put_cstr(ds, "\taggregation key: ");
762 if (lacp->key_slave) {
763 ds_put_format(ds, "%u", lacp->key_slave->key
764 ? lacp->key_slave->key
765 : lacp->key_slave->port_id);
767 ds_put_cstr(ds, "none");
769 ds_put_cstr(ds, "\n");
771 ds_put_cstr(ds, "\tlacp_time: ");
773 ds_put_cstr(ds, "fast\n");
775 ds_put_cstr(ds, "slow\n");
778 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
779 shash_add(&slave_shash, slave->name, slave);
781 sorted_slaves = shash_sort(&slave_shash);
783 for (i = 0; i < shash_count(&slave_shash); i++) {
785 struct lacp_info actor;
787 slave = sorted_slaves[i]->data;
788 slave_get_actor(slave, &actor);
789 switch (slave->status) {
797 status = "defaulted";
803 ds_put_format(ds, "\nslave: %s: %s %s\n", slave->name, status,
804 slave->attached ? "attached" : "detached");
805 ds_put_format(ds, "\tport_id: %u\n", slave->port_id);
806 ds_put_format(ds, "\tport_priority: %u\n", slave->port_priority);
807 ds_put_format(ds, "\tmay_enable: %s\n", (slave_may_enable__(slave)
808 ? "true" : "false"));
810 ds_put_format(ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
811 ETH_ADDR_ARGS(actor.sys_id));
812 ds_put_format(ds, "\tactor sys_priority: %u\n",
813 ntohs(actor.sys_priority));
814 ds_put_format(ds, "\tactor port_id: %u\n",
815 ntohs(actor.port_id));
816 ds_put_format(ds, "\tactor port_priority: %u\n",
817 ntohs(actor.port_priority));
818 ds_put_format(ds, "\tactor key: %u\n",
820 ds_put_cstr(ds, "\tactor state:");
821 ds_put_lacp_state(ds, actor.state);
822 ds_put_cstr(ds, "\n\n");
824 ds_put_format(ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
825 ETH_ADDR_ARGS(slave->partner.sys_id));
826 ds_put_format(ds, "\tpartner sys_priority: %u\n",
827 ntohs(slave->partner.sys_priority));
828 ds_put_format(ds, "\tpartner port_id: %u\n",
829 ntohs(slave->partner.port_id));
830 ds_put_format(ds, "\tpartner port_priority: %u\n",
831 ntohs(slave->partner.port_priority));
832 ds_put_format(ds, "\tpartner key: %u\n",
833 ntohs(slave->partner.key));
834 ds_put_cstr(ds, "\tpartner state:");
835 ds_put_lacp_state(ds, slave->partner.state);
836 ds_put_cstr(ds, "\n");
839 shash_destroy(&slave_shash);
844 lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
845 void *aux OVS_UNUSED)
847 struct ds ds = DS_EMPTY_INITIALIZER;
851 lacp = lacp_find(argv[1]);
853 unixctl_command_reply_error(conn, "no such lacp object");
856 lacp_print_details(&ds, lacp);
858 LIST_FOR_EACH (lacp, node, &all_lacps) {
859 lacp_print_details(&ds, lacp);
863 unixctl_command_reply(conn, ds_cstr(&ds));