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"
32 VLOG_DEFINE_THIS_MODULE(lacp);
35 LACP_CURRENT, /* Current State. Partner up to date. */
36 LACP_EXPIRED, /* Expired State. Partner out of date. */
37 LACP_DEFAULTED, /* Defaulted State. No partner. */
41 struct list node; /* Node in all_lacps list. */
42 char *name; /* Name of this lacp object. */
43 uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
44 uint16_t sys_priority; /* System Priority. */
45 bool active; /* Active or Passive. */
47 struct hmap slaves; /* Slaves this LACP object controls. */
48 struct slave *key_slave; /* Slave whose ID will be the aggregation key. */
50 bool negotiated; /* True if LACP negotiations were successful. */
51 bool update; /* True if lacp_update() needs to be called. */
55 void *aux; /* Handle used to identify this slave. */
56 struct hmap_node node; /* Node in master's slaves map. */
58 struct lacp *lacp; /* LACP object containing this slave. */
59 uint16_t port_id; /* Port ID. */
60 uint16_t port_priority; /* Port Priority. */
61 char *name; /* Name of this slave. */
63 enum slave_status status; /* Slave status. */
64 bool attached; /* Attached. Traffic may flow. */
65 bool enabled; /* Enabled. Traffic is flowing. */
66 struct lacp_info actor; /* Actor information. */
67 struct lacp_info partner; /* Partner information. */
68 long long int tx; /* Next message transmission time. */
69 long long int rx; /* Expected message receive time. */
72 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
74 static void lacp_update_attached(struct lacp *);
76 static void slave_destroy(struct slave *);
77 static void slave_set_defaulted(struct slave *);
78 static void slave_set_expired(struct slave *);
79 static void slave_update_actor(struct slave *);
80 static void slave_get_priority(struct slave *, struct lacp_info *priority);
81 static bool slave_may_tx(const struct slave *);
82 static struct slave *slave_lookup(const struct lacp *, const void *slave);
84 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
87 /* Initializes the lacp module. */
91 unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
94 /* Creates a LACP object. */
100 lacp = xzalloc(sizeof *lacp);
101 hmap_init(&lacp->slaves);
102 list_push_back(&all_lacps, &lacp->node);
106 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
108 lacp_destroy(struct lacp *lacp)
111 struct slave *slave, *next;
113 HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
114 slave_destroy(slave);
117 hmap_destroy(&lacp->slaves);
118 list_remove(&lacp->node);
124 /* Configures 'lacp' with the given 'name', 'sys_id', 'sys_priority', and
125 * 'active' parameters. */
127 lacp_configure(struct lacp *lacp, const char *name,
128 uint8_t sys_id[ETH_ADDR_LEN], uint16_t sys_priority,
131 if (!lacp->name || strcmp(name, lacp->name)) {
133 lacp->name = xstrdup(name);
136 memcpy(lacp->sys_id, sys_id, ETH_ADDR_LEN);
137 lacp->sys_priority = sys_priority;
138 lacp->active = active;
141 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
142 * should be called on all packets received on 'slave_' with Ethernet Type
143 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
145 lacp_process_pdu(struct lacp *lacp, const void *slave_,
146 const struct lacp_pdu *pdu)
148 struct slave *slave = slave_lookup(lacp, slave_);
150 slave->status = LACP_CURRENT;
151 slave->rx = time_msec() + LACP_SLOW_TIME_RX;
153 /* Check if our partner has incorrect information about our current state.
154 * If so update them. */
155 slave_update_actor(slave);
156 if (memcmp(&slave->actor, &pdu->partner, sizeof pdu->partner)) {
157 slave->tx = LLONG_MIN;
160 /* Update our information about our partner if it's out of date. This may
161 * cause priorities to change so re-calculate attached status of all
163 if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
165 slave->partner = pdu->actor;
169 /* Returns true if 'lacp' has successfully negotiated with its partner. False
170 * if 'lacp' is NULL. */
172 lacp_negotiated(const struct lacp *lacp)
174 return lacp ? lacp->negotiated : false;
177 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
178 * once per slave in a LACP managed bond. Should also be called whenever a
179 * slave's name, port_id, or port_priority change. */
181 lacp_slave_register(struct lacp *lacp, void *slave_, const char *name,
182 uint16_t port_id, uint16_t port_priority)
184 struct slave *slave = slave_lookup(lacp, slave_);
187 slave = xzalloc(sizeof *slave);
190 hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
191 slave_set_defaulted(slave);
193 if (!lacp->key_slave) {
194 lacp->key_slave = slave;
198 if (!slave->name || strcmp(name, slave->name)) {
200 slave->name = xstrdup(name);
203 if (slave->port_id != port_id || slave->port_priority != port_priority) {
205 slave->port_id = port_id;
206 slave->port_priority = port_priority;
208 slave->tx = LLONG_MIN;
211 if (lacp->active || lacp->negotiated) {
212 slave_set_expired(slave);
217 /* Unregisters 'slave_' with 'lacp'. */
219 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
221 struct slave *slave = slave_lookup(lacp, slave_);
224 slave_destroy(slave);
228 /* Should be called regularly to indicate whether 'slave_' is enabled. An
229 * enabled slave is allowed to send and receive traffic. Generally a slave
230 * should not be enabled if its carrier is down, or lacp_slave_may_enable()
231 * indicates it should not be enabled. */
233 lacp_slave_enable(struct lacp *lacp, void *slave_, bool enabled)
235 struct slave *slave = slave_lookup(lacp, slave_);
237 if (slave->enabled != enabled) {
238 slave->enabled = enabled;
239 slave->tx = LLONG_MIN;
243 /* This function should be called whenever the carrier status of 'slave_' has
246 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
248 struct slave *slave = slave_lookup(lacp, slave_);
250 slave->tx = LLONG_MIN;
251 if (slave->status == LACP_CURRENT || slave->lacp->active) {
252 slave_set_expired(slave);
256 /* This function should be called before enabling 'slave_' to send or receive
257 * traffic. If it returns false, 'slave_' should not enabled. As a
258 * convenience, returns true if 'lacp' is NULL. */
260 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
263 struct slave *slave = slave_lookup(lacp, slave_);
265 /* The slave may be enabled if it's attached to an aggregator and its
266 * partner is synchronized. The only exception is defaulted slaves.
267 * They are not required to have synchronized partners because they
268 * have no partners at all. They will only be attached if negotiations
269 * failed on all slaves in the bond. */
270 return slave->attached && (slave->partner.state & LACP_STATE_SYNC
271 || slave->status == LACP_DEFAULTED);
277 /* This function should be called periodically to update 'lacp'. */
279 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
283 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
284 if (time_msec() >= slave->rx) {
285 if (slave->status == LACP_CURRENT) {
286 slave_set_expired(slave);
287 } else if (slave->status == LACP_EXPIRED) {
288 slave_set_defaulted(slave);
291 slave_update_actor(slave);
295 lacp_update_attached(lacp);
298 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
301 if (time_msec() < slave->tx || !slave_may_tx(slave)) {
305 compose_lacp_pdu(&slave->actor, &slave->partner, &pdu);
306 send_pdu(slave->aux, &pdu);
308 slave->tx = time_msec() +
309 (slave->partner.state & LACP_STATE_TIME
311 : LACP_SLOW_TIME_TX);
315 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
317 lacp_wait(struct lacp *lacp)
321 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
322 if (slave_may_tx(slave)) {
323 poll_timer_wait_until(slave->tx);
326 if (slave->status != LACP_DEFAULTED) {
327 poll_timer_wait_until(slave->rx);
332 /* Static Helpers. */
334 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
335 * negotiated parameter to true if any slaves are attachable. */
337 lacp_update_attached(struct lacp *lacp)
339 struct slave *lead, *slave;
340 struct lacp_info lead_pri;
341 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
343 lacp->update = false;
346 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
347 struct lacp_info pri;
349 slave->attached = true;
351 /* XXX: In the future allow users to configure the expected system ID.
352 * For now just special case loopback. */
353 if (eth_addr_equals(slave->partner.sys_id, slave->actor.sys_id)) {
354 VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
355 "connected to its own bond", slave->name);
356 slave->attached = false;
360 if (slave->status == LACP_DEFAULTED) {
364 slave_get_priority(slave, &pri);
366 if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
372 lacp->negotiated = lead != NULL;
375 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
376 if (slave->status == LACP_DEFAULTED
377 || lead->partner.key != slave->partner.key
378 || !eth_addr_equals(lead->partner.sys_id,
379 slave->partner.sys_id)) {
380 slave->attached = false;
387 slave_destroy(struct slave *slave)
390 struct lacp *lacp = slave->lacp;
393 hmap_remove(&lacp->slaves, &slave->node);
395 if (lacp->key_slave == slave) {
396 struct hmap_node *slave_node = hmap_first(&lacp->slaves);
399 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
401 lacp->key_slave = NULL;
411 slave_set_defaulted(struct slave *slave)
413 memset(&slave->partner, 0, sizeof slave->partner);
415 slave->tx = LLONG_MIN;
416 slave->lacp->update = true;
417 slave->status = LACP_DEFAULTED;
421 slave_set_expired(struct slave *slave)
423 slave->status = LACP_EXPIRED;
424 slave->partner.state |= LACP_STATE_TIME;
425 slave->partner.state &= ~LACP_STATE_SYNC;
427 slave->rx = time_msec() + LACP_FAST_TIME_RX;
428 slave->tx = LLONG_MIN;
432 slave_update_actor(struct slave *slave)
436 if (slave->lacp->active) {
437 state |= LACP_STATE_ACT;
440 if (slave->attached) {
441 state |= LACP_STATE_SYNC;
444 if (slave->status == LACP_DEFAULTED) {
445 state |= LACP_STATE_DEF;
448 if (slave->status == LACP_EXPIRED) {
449 state |= LACP_STATE_EXP;
452 if (hmap_count(&slave->lacp->slaves) > 1) {
453 state |= LACP_STATE_AGG;
456 if (slave->enabled) {
457 state |= LACP_STATE_COL | LACP_STATE_DIST;
460 slave->actor.state = state;
461 slave->actor.key = htons(slave->lacp->key_slave->port_id);
462 slave->actor.port_priority = htons(slave->port_priority);
463 slave->actor.port_id = htons(slave->port_id);
464 slave->actor.sys_priority = htons(slave->lacp->sys_priority);
465 memcpy(&slave->actor.sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
468 /* Given 'slave', populates 'priority' with data representing its LACP link
469 * priority. If two priority objects populated by this function are compared
470 * using memcmp, the higher priority link will be less than the lower priority
473 slave_get_priority(struct slave *slave, struct lacp_info *priority)
475 uint16_t partner_priority, actor_priority;
477 /* Choose the lacp_info of the higher priority system by comparing their
478 * system priorities and mac addresses. */
479 actor_priority = ntohs(slave->actor.sys_priority);
480 partner_priority = ntohs(slave->partner.sys_priority);
481 if (actor_priority < partner_priority) {
482 *priority = slave->actor;
483 } else if (partner_priority < actor_priority) {
484 *priority = slave->partner;
485 } else if (eth_addr_compare_3way(slave->actor.sys_id,
486 slave->partner.sys_id) < 0) {
487 *priority = slave->actor;
489 *priority = slave->partner;
492 /* Key and state are not used in priority comparisons. */
498 slave_may_tx(const struct slave *slave)
500 return slave->lacp->active || slave->status != LACP_DEFAULTED;
503 static struct slave *
504 slave_lookup(const struct lacp *lacp, const void *slave_)
508 HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
510 if (slave->aux == slave_) {
519 lacp_find(const char *name)
523 LIST_FOR_EACH (lacp, node, &all_lacps) {
524 if (!strcmp(lacp->name, name)) {
533 ds_put_lacp_state(struct ds *ds, uint8_t state)
535 if (state & LACP_STATE_ACT) {
536 ds_put_cstr(ds, "activity ");
539 if (state & LACP_STATE_TIME) {
540 ds_put_cstr(ds, "timeout ");
543 if (state & LACP_STATE_AGG) {
544 ds_put_cstr(ds, "aggregation ");
547 if (state & LACP_STATE_SYNC) {
548 ds_put_cstr(ds, "synchronized ");
551 if (state & LACP_STATE_COL) {
552 ds_put_cstr(ds, "collecting ");
555 if (state & LACP_STATE_DIST) {
556 ds_put_cstr(ds, "distributing ");
559 if (state & LACP_STATE_DEF) {
560 ds_put_cstr(ds, "defaulted ");
563 if (state & LACP_STATE_EXP) {
564 ds_put_cstr(ds, "expired ");
569 lacp_unixctl_show(struct unixctl_conn *conn,
570 const char *args, void *aux OVS_UNUSED)
572 struct ds ds = DS_EMPTY_INITIALIZER;
576 lacp = lacp_find(args);
578 unixctl_command_reply(conn, 501, "no such lacp object");
582 ds_put_format(&ds, "lacp: %s\n", lacp->name);
583 ds_put_format(&ds, "\tstatus: %s %s\n",
584 lacp->active ? "active" : "passive",
585 lacp->negotiated ? "negotiated" : "");
586 ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
587 ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
588 ds_put_cstr(&ds, "\taggregation key: ");
589 if (lacp->key_slave) {
590 ds_put_format(&ds, "%u", lacp->key_slave->port_id);
592 ds_put_cstr(&ds, "none");
594 ds_put_cstr(&ds, "\n");
596 HMAP_FOR_EACH (slave, node, &lacp->slaves) {
599 slave_update_actor(slave);
600 switch (slave->status) {
608 status = "defaulted";
614 ds_put_format(&ds, "\nslave: %s: %s %s %s\n", slave->name, status,
615 slave->attached ? "attached" : "detached",
616 slave->enabled ? "enabled" : "disabled");
617 ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
618 ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
620 ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
621 ETH_ADDR_ARGS(slave->actor.sys_id));
622 ds_put_format(&ds, "\tactor sys_priority: %u\n",
623 ntohs(slave->actor.sys_priority));
624 ds_put_format(&ds, "\tactor port_id: %u\n",
625 ntohs(slave->actor.port_id));
626 ds_put_format(&ds, "\tactor port_priority: %u\n",
627 ntohs(slave->actor.port_priority));
628 ds_put_format(&ds, "\tactor key: %u\n",
629 ntohs(slave->actor.key));
630 ds_put_cstr(&ds, "\tactor state: ");
631 ds_put_lacp_state(&ds, slave->actor.state);
632 ds_put_cstr(&ds, "\n\n");
634 ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
635 ETH_ADDR_ARGS(slave->partner.sys_id));
636 ds_put_format(&ds, "\tpartner sys_priority: %u\n",
637 ntohs(slave->partner.sys_priority));
638 ds_put_format(&ds, "\tpartner port_id: %u\n",
639 ntohs(slave->partner.port_id));
640 ds_put_format(&ds, "\tpartner port_priority: %u\n",
641 ntohs(slave->partner.port_priority));
642 ds_put_format(&ds, "\tpartner key: %u\n",
643 ntohs(slave->partner.key));
644 ds_put_cstr(&ds, "\tpartner state: ");
645 ds_put_lacp_state(&ds, slave->partner.state);
646 ds_put_cstr(&ds, "\n");
649 unixctl_command_reply(conn, 200, ds_cstr(&ds));