]>
git.proxmox.com Git - ovs.git/blob - lib/lacp.c
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 bool fast
; /* Fast or Slow LACP time. */
52 bool negotiated
; /* True if LACP negotiations were successful. */
53 bool update
; /* True if lacp_update() needs to be called. */
57 void *aux
; /* Handle used to identify this slave. */
58 struct hmap_node node
; /* Node in master's slaves map. */
60 struct lacp
*lacp
; /* LACP object containing this slave. */
61 uint16_t port_id
; /* Port ID. */
62 uint16_t port_priority
; /* Port Priority. */
63 char *name
; /* Name of this slave. */
65 enum slave_status status
; /* Slave status. */
66 bool attached
; /* Attached. Traffic may flow. */
67 struct lacp_info partner
; /* Partner information. */
68 struct lacp_info ntt_actor
; /* Used to decide if we Need To Transmit. */
69 struct timer tx
; /* Next message transmission timer. */
70 struct timer rx
; /* Expected message receive timer. */
73 static struct list all_lacps
= LIST_INITIALIZER(&all_lacps
);
75 static void lacp_update_attached(struct lacp
*);
77 static void slave_destroy(struct slave
*);
78 static void slave_set_defaulted(struct slave
*);
79 static void slave_set_expired(struct slave
*);
80 static void slave_get_actor(struct slave
*, struct lacp_info
*actor
);
81 static void slave_get_priority(struct slave
*, struct lacp_info
*priority
);
82 static bool slave_may_tx(const struct slave
*);
83 static struct slave
*slave_lookup(const struct lacp
*, const void *slave
);
84 static bool info_tx_equal(struct lacp_info
*, struct lacp_info
*);
86 static void lacp_unixctl_show(struct unixctl_conn
*, const char *args
,
89 /* Initializes the lacp module. */
93 unixctl_command_register("lacp/show", lacp_unixctl_show
, NULL
);
96 /* Creates a LACP object. */
102 lacp
= xzalloc(sizeof *lacp
);
103 hmap_init(&lacp
->slaves
);
104 list_push_back(&all_lacps
, &lacp
->node
);
108 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
110 lacp_destroy(struct lacp
*lacp
)
113 struct slave
*slave
, *next
;
115 HMAP_FOR_EACH_SAFE (slave
, next
, node
, &lacp
->slaves
) {
116 slave_destroy(slave
);
119 hmap_destroy(&lacp
->slaves
);
120 list_remove(&lacp
->node
);
126 /* Configures 'lacp' with settings from 's'. */
128 lacp_configure(struct lacp
*lacp
, const struct lacp_settings
*s
)
130 if (!lacp
->name
|| strcmp(s
->name
, lacp
->name
)) {
132 lacp
->name
= xstrdup(s
->name
);
135 memcpy(lacp
->sys_id
, s
->id
, ETH_ADDR_LEN
);
136 lacp
->sys_priority
= s
->priority
;
137 lacp
->active
= s
->active
;
138 lacp
->fast
= s
->fast
;
141 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
142 * configured for passive mode. */
144 lacp_is_active(const struct lacp
*lacp
)
149 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
150 * should be called on all packets received on 'slave_' with Ethernet Type
151 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
153 lacp_process_pdu(struct lacp
*lacp
, const void *slave_
,
154 const struct lacp_pdu
*pdu
)
156 struct slave
*slave
= slave_lookup(lacp
, slave_
);
158 slave
->status
= LACP_CURRENT
;
159 timer_set_duration(&slave
->rx
, (lacp
->fast
161 : LACP_SLOW_TIME_RX
));
163 slave
->ntt_actor
= pdu
->partner
;
165 /* Update our information about our partner if it's out of date. This may
166 * cause priorities to change so re-calculate attached status of all
168 if (memcmp(&slave
->partner
, &pdu
->actor
, sizeof pdu
->actor
)) {
170 slave
->partner
= pdu
->actor
;
174 /* Returns true if 'lacp' has successfully negotiated with its partner. False
175 * if 'lacp' is NULL. */
177 lacp_negotiated(const struct lacp
*lacp
)
179 return lacp
? lacp
->negotiated
: false;
182 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
183 * once per slave in a LACP managed bond. Should also be called whenever a
184 * slave's settings change. */
186 lacp_slave_register(struct lacp
*lacp
, void *slave_
,
187 const struct lacp_slave_settings
*s
)
189 struct slave
*slave
= slave_lookup(lacp
, slave_
);
192 slave
= xzalloc(sizeof *slave
);
195 hmap_insert(&lacp
->slaves
, &slave
->node
, hash_pointer(slave_
, 0));
196 slave_set_defaulted(slave
);
198 if (!lacp
->key_slave
) {
199 lacp
->key_slave
= slave
;
203 if (!slave
->name
|| strcmp(s
->name
, slave
->name
)) {
205 slave
->name
= xstrdup(s
->name
);
208 if (slave
->port_id
!= s
->id
|| slave
->port_priority
!= s
->priority
) {
209 slave
->port_id
= s
->id
;
210 slave
->port_priority
= s
->priority
;
214 if (lacp
->active
|| lacp
->negotiated
) {
215 slave_set_expired(slave
);
220 /* Unregisters 'slave_' with 'lacp'. */
222 lacp_slave_unregister(struct lacp
*lacp
, const void *slave_
)
224 struct slave
*slave
= slave_lookup(lacp
, slave_
);
227 slave_destroy(slave
);
231 /* This function should be called whenever the carrier status of 'slave_' has
234 lacp_slave_carrier_changed(const struct lacp
*lacp
, const void *slave_
)
236 struct slave
*slave
= slave_lookup(lacp
, slave_
);
238 if (slave
->status
== LACP_CURRENT
|| slave
->lacp
->active
) {
239 slave_set_expired(slave
);
243 /* This function should be called before enabling 'slave_' to send or receive
244 * traffic. If it returns false, 'slave_' should not enabled. As a
245 * convenience, returns true if 'lacp' is NULL. */
247 lacp_slave_may_enable(const struct lacp
*lacp
, const void *slave_
)
250 struct slave
*slave
= slave_lookup(lacp
, slave_
);
252 /* The slave may be enabled if it's attached to an aggregator and its
253 * partner is synchronized. The only exception is defaulted slaves.
254 * They are not required to have synchronized partners because they
255 * have no partners at all. They will only be attached if negotiations
256 * failed on all slaves in the bond. */
257 return slave
->attached
&& (slave
->partner
.state
& LACP_STATE_SYNC
258 || slave
->status
== LACP_DEFAULTED
);
264 /* Returns the port ID used for 'slave_' in LACP communications. */
266 lacp_slave_get_port_id(const struct lacp
*lacp
, const void *slave_
)
268 struct slave
*slave
= slave_lookup(lacp
, slave_
);
269 return slave
->port_id
;
272 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
273 * not being current, generally indicates a connectivity problem, or a
274 * misconfigured (or broken) partner. */
276 lacp_slave_is_current(const struct lacp
*lacp
, const void *slave_
)
278 return slave_lookup(lacp
, slave_
)->status
== LACP_CURRENT
;
281 /* This function should be called periodically to update 'lacp'. */
283 lacp_run(struct lacp
*lacp
, lacp_send_pdu
*send_pdu
)
287 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
288 if (timer_expired(&slave
->rx
)) {
289 if (slave
->status
== LACP_CURRENT
) {
290 slave_set_expired(slave
);
291 } else if (slave
->status
== LACP_EXPIRED
) {
292 slave_set_defaulted(slave
);
298 lacp_update_attached(lacp
);
301 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
303 struct lacp_info actor
;
305 if (!slave_may_tx(slave
)) {
309 slave_get_actor(slave
, &actor
);
311 if (timer_expired(&slave
->tx
)
312 || !info_tx_equal(&actor
, &slave
->ntt_actor
)) {
314 slave
->ntt_actor
= actor
;
315 compose_lacp_pdu(&actor
, &slave
->partner
, &pdu
);
316 send_pdu(slave
->aux
, &pdu
);
318 timer_set_duration(&slave
->tx
,
319 (slave
->partner
.state
& LACP_STATE_TIME
321 : LACP_SLOW_TIME_TX
));
326 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
328 lacp_wait(struct lacp
*lacp
)
332 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
333 if (slave_may_tx(slave
)) {
334 timer_wait(&slave
->tx
);
337 if (slave
->status
!= LACP_DEFAULTED
) {
338 timer_wait(&slave
->rx
);
343 /* Static Helpers. */
345 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
346 * negotiated parameter to true if any slaves are attachable. */
348 lacp_update_attached(struct lacp
*lacp
)
350 struct slave
*lead
, *slave
;
351 struct lacp_info lead_pri
;
352 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
354 lacp
->update
= false;
357 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
358 struct lacp_info pri
;
360 slave
->attached
= true;
362 /* XXX: In the future allow users to configure the expected system ID.
363 * For now just special case loopback. */
364 if (eth_addr_equals(slave
->partner
.sys_id
, slave
->lacp
->sys_id
)) {
365 VLOG_WARN_RL(&rl
, "slave %s: Loopback detected. Slave is "
366 "connected to its own bond", slave
->name
);
367 slave
->attached
= false;
371 if (slave
->status
== LACP_DEFAULTED
) {
375 slave_get_priority(slave
, &pri
);
377 if (!lead
|| memcmp(&pri
, &lead_pri
, sizeof pri
) < 0) {
383 lacp
->negotiated
= lead
!= NULL
;
386 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
387 if (slave
->status
== LACP_DEFAULTED
388 || lead
->partner
.key
!= slave
->partner
.key
389 || !eth_addr_equals(lead
->partner
.sys_id
,
390 slave
->partner
.sys_id
)) {
391 slave
->attached
= false;
398 slave_destroy(struct slave
*slave
)
401 struct lacp
*lacp
= slave
->lacp
;
404 hmap_remove(&lacp
->slaves
, &slave
->node
);
406 if (lacp
->key_slave
== slave
) {
407 struct hmap_node
*slave_node
= hmap_first(&lacp
->slaves
);
410 lacp
->key_slave
= CONTAINER_OF(slave_node
, struct slave
, node
);
412 lacp
->key_slave
= NULL
;
422 slave_set_defaulted(struct slave
*slave
)
424 memset(&slave
->partner
, 0, sizeof slave
->partner
);
426 slave
->lacp
->update
= true;
427 slave
->status
= LACP_DEFAULTED
;
431 slave_set_expired(struct slave
*slave
)
433 slave
->status
= LACP_EXPIRED
;
434 slave
->partner
.state
|= LACP_STATE_TIME
;
435 slave
->partner
.state
&= ~LACP_STATE_SYNC
;
436 timer_set_duration(&slave
->rx
, LACP_FAST_TIME_RX
);
440 slave_get_actor(struct slave
*slave
, struct lacp_info
*actor
)
444 if (slave
->lacp
->active
) {
445 state
|= LACP_STATE_ACT
;
448 if (slave
->lacp
->fast
) {
449 state
|= LACP_STATE_TIME
;
452 if (slave
->attached
) {
453 state
|= LACP_STATE_SYNC
;
456 if (slave
->status
== LACP_DEFAULTED
) {
457 state
|= LACP_STATE_DEF
;
460 if (slave
->status
== LACP_EXPIRED
) {
461 state
|= LACP_STATE_EXP
;
464 if (hmap_count(&slave
->lacp
->slaves
) > 1) {
465 state
|= LACP_STATE_AGG
;
468 if (slave
->attached
|| !slave
->lacp
->negotiated
) {
469 state
|= LACP_STATE_COL
| LACP_STATE_DIST
;
472 actor
->state
= state
;
473 actor
->key
= htons(slave
->lacp
->key_slave
->port_id
);
474 actor
->port_priority
= htons(slave
->port_priority
);
475 actor
->port_id
= htons(slave
->port_id
);
476 actor
->sys_priority
= htons(slave
->lacp
->sys_priority
);
477 memcpy(&actor
->sys_id
, slave
->lacp
->sys_id
, ETH_ADDR_LEN
);
480 /* Given 'slave', populates 'priority' with data representing its LACP link
481 * priority. If two priority objects populated by this function are compared
482 * using memcmp, the higher priority link will be less than the lower priority
485 slave_get_priority(struct slave
*slave
, struct lacp_info
*priority
)
487 uint16_t partner_priority
, actor_priority
;
489 /* Choose the lacp_info of the higher priority system by comparing their
490 * system priorities and mac addresses. */
491 actor_priority
= slave
->lacp
->sys_priority
;
492 partner_priority
= ntohs(slave
->partner
.sys_priority
);
493 if (actor_priority
< partner_priority
) {
494 slave_get_actor(slave
, priority
);
495 } else if (partner_priority
< actor_priority
) {
496 *priority
= slave
->partner
;
497 } else if (eth_addr_compare_3way(slave
->lacp
->sys_id
,
498 slave
->partner
.sys_id
) < 0) {
499 slave_get_actor(slave
, priority
);
501 *priority
= slave
->partner
;
504 /* Key and state are not used in priority comparisons. */
510 slave_may_tx(const struct slave
*slave
)
512 return slave
->lacp
->active
|| slave
->status
!= LACP_DEFAULTED
;
515 static struct slave
*
516 slave_lookup(const struct lacp
*lacp
, const void *slave_
)
520 HMAP_FOR_EACH_IN_BUCKET (slave
, node
, hash_pointer(slave_
, 0),
522 if (slave
->aux
== slave_
) {
530 /* Two lacp_info structures are tx_equal if and only if they do not differ in
531 * ways which would require a lacp_pdu transmission. */
533 info_tx_equal(struct lacp_info
*a
, struct lacp_info
*b
)
536 /* LACP specification dictates that we transmit whenever the actor and
537 * remote_actor differ in the following fields: Port, Port Priority,
538 * System, System Priority, Aggregation Key, Activity State, Timeout State,
539 * Sync State, and Aggregation State. The state flags are most likely to
540 * change so are checked first. */
541 return !((a
->state
^ b
->state
) & (LACP_STATE_ACT
545 && a
->port_id
== b
->port_id
546 && a
->port_priority
== b
->port_priority
548 && a
->sys_priority
== b
->sys_priority
549 && eth_addr_equals(a
->sys_id
, b
->sys_id
);
553 lacp_find(const char *name
)
557 LIST_FOR_EACH (lacp
, node
, &all_lacps
) {
558 if (!strcmp(lacp
->name
, name
)) {
567 ds_put_lacp_state(struct ds
*ds
, uint8_t state
)
569 if (state
& LACP_STATE_ACT
) {
570 ds_put_cstr(ds
, "activity ");
573 if (state
& LACP_STATE_TIME
) {
574 ds_put_cstr(ds
, "timeout ");
577 if (state
& LACP_STATE_AGG
) {
578 ds_put_cstr(ds
, "aggregation ");
581 if (state
& LACP_STATE_SYNC
) {
582 ds_put_cstr(ds
, "synchronized ");
585 if (state
& LACP_STATE_COL
) {
586 ds_put_cstr(ds
, "collecting ");
589 if (state
& LACP_STATE_DIST
) {
590 ds_put_cstr(ds
, "distributing ");
593 if (state
& LACP_STATE_DEF
) {
594 ds_put_cstr(ds
, "defaulted ");
597 if (state
& LACP_STATE_EXP
) {
598 ds_put_cstr(ds
, "expired ");
603 lacp_unixctl_show(struct unixctl_conn
*conn
,
604 const char *args
, void *aux OVS_UNUSED
)
606 struct ds ds
= DS_EMPTY_INITIALIZER
;
610 lacp
= lacp_find(args
);
612 unixctl_command_reply(conn
, 501, "no such lacp object");
616 ds_put_format(&ds
, "lacp: %s\n", lacp
->name
);
617 ds_put_format(&ds
, "\tstatus: %s %s\n",
618 lacp
->active
? "active" : "passive",
619 lacp
->negotiated
? "negotiated" : "");
620 ds_put_format(&ds
, "\tsys_id: " ETH_ADDR_FMT
"\n", ETH_ADDR_ARGS(lacp
->sys_id
));
621 ds_put_format(&ds
, "\tsys_priority: %u\n", lacp
->sys_priority
);
622 ds_put_cstr(&ds
, "\taggregation key: ");
623 if (lacp
->key_slave
) {
624 ds_put_format(&ds
, "%u", lacp
->key_slave
->port_id
);
626 ds_put_cstr(&ds
, "none");
628 ds_put_cstr(&ds
, "\n");
630 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
632 struct lacp_info actor
;
634 slave_get_actor(slave
, &actor
);
635 switch (slave
->status
) {
643 status
= "defaulted";
649 ds_put_format(&ds
, "\nslave: %s: %s %s\n", slave
->name
, status
,
650 slave
->attached
? "attached" : "detached");
651 ds_put_format(&ds
, "\tport_id: %u\n", slave
->port_id
);
652 ds_put_format(&ds
, "\tport_priority: %u\n", slave
->port_priority
);
654 ds_put_format(&ds
, "\n\tactor sys_id: " ETH_ADDR_FMT
"\n",
655 ETH_ADDR_ARGS(actor
.sys_id
));
656 ds_put_format(&ds
, "\tactor sys_priority: %u\n",
657 ntohs(actor
.sys_priority
));
658 ds_put_format(&ds
, "\tactor port_id: %u\n",
659 ntohs(actor
.port_id
));
660 ds_put_format(&ds
, "\tactor port_priority: %u\n",
661 ntohs(actor
.port_priority
));
662 ds_put_format(&ds
, "\tactor key: %u\n",
664 ds_put_cstr(&ds
, "\tactor state: ");
665 ds_put_lacp_state(&ds
, actor
.state
);
666 ds_put_cstr(&ds
, "\n\n");
668 ds_put_format(&ds
, "\tpartner sys_id: " ETH_ADDR_FMT
"\n",
669 ETH_ADDR_ARGS(slave
->partner
.sys_id
));
670 ds_put_format(&ds
, "\tpartner sys_priority: %u\n",
671 ntohs(slave
->partner
.sys_priority
));
672 ds_put_format(&ds
, "\tpartner port_id: %u\n",
673 ntohs(slave
->partner
.port_id
));
674 ds_put_format(&ds
, "\tpartner port_priority: %u\n",
675 ntohs(slave
->partner
.port_priority
));
676 ds_put_format(&ds
, "\tpartner key: %u\n",
677 ntohs(slave
->partner
.key
));
678 ds_put_cstr(&ds
, "\tpartner state: ");
679 ds_put_lacp_state(&ds
, slave
->partner
.state
);
680 ds_put_cstr(&ds
, "\n");
683 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));