]>
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 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 uint16_t key
; /* Aggregation Key. 0 if default. */
67 char *name
; /* Name of this slave. */
69 enum slave_status status
; /* Slave status. */
70 bool attached
; /* Attached. Traffic may flow. */
71 struct lacp_info partner
; /* Partner information. */
72 struct lacp_info ntt_actor
; /* Used to decide if we Need To Transmit. */
73 struct timer tx
; /* Next message transmission timer. */
74 struct timer rx
; /* Expected message receive timer. */
77 static struct list all_lacps
= LIST_INITIALIZER(&all_lacps
);
79 static void lacp_update_attached(struct lacp
*);
81 static void slave_destroy(struct slave
*);
82 static void slave_set_defaulted(struct slave
*);
83 static void slave_set_expired(struct slave
*);
84 static void slave_get_actor(struct slave
*, struct lacp_info
*actor
);
85 static void slave_get_priority(struct slave
*, struct lacp_info
*priority
);
86 static bool slave_may_tx(const struct slave
*);
87 static struct slave
*slave_lookup(const struct lacp
*, const void *slave
);
88 static bool info_tx_equal(struct lacp_info
*, struct lacp_info
*);
90 static void lacp_unixctl_show(struct unixctl_conn
*, const char *args
,
93 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
95 compose_lacp_pdu(const struct lacp_info
*actor
,
96 const struct lacp_info
*partner
, struct lacp_pdu
*pdu
)
98 memset(pdu
, 0, sizeof *pdu
);
107 pdu
->partner_type
= 2;
108 pdu
->partner_len
= 20;
109 pdu
->partner
= *partner
;
111 pdu
->collector_type
= 3;
112 pdu
->collector_len
= 16;
113 pdu
->collector_delay
= htons(0);
116 /* Parses 'b' which represents a packet containing a LACP PDU. This function
117 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
118 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
120 const struct lacp_pdu
*
121 parse_lacp_packet(const struct ofpbuf
*b
)
123 const struct lacp_pdu
*pdu
;
125 pdu
= ofpbuf_at(b
, (uint8_t *)b
->l3
- (uint8_t *)b
->data
, LACP_PDU_LEN
);
127 if (pdu
&& pdu
->subtype
== 1
128 && pdu
->actor_type
== 1 && pdu
->actor_len
== 20
129 && pdu
->partner_type
== 2 && pdu
->partner_len
== 20) {
136 /* LACP Protocol Implementation. */
138 /* Initializes the lacp module. */
142 unixctl_command_register("lacp/show", lacp_unixctl_show
, NULL
);
145 /* Creates a LACP object. */
151 lacp
= xzalloc(sizeof *lacp
);
152 hmap_init(&lacp
->slaves
);
153 list_push_back(&all_lacps
, &lacp
->node
);
157 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
159 lacp_destroy(struct lacp
*lacp
)
162 struct slave
*slave
, *next
;
164 HMAP_FOR_EACH_SAFE (slave
, next
, node
, &lacp
->slaves
) {
165 slave_destroy(slave
);
168 hmap_destroy(&lacp
->slaves
);
169 list_remove(&lacp
->node
);
175 /* Configures 'lacp' with settings from 's'. */
177 lacp_configure(struct lacp
*lacp
, const struct lacp_settings
*s
)
179 if (!lacp
->name
|| strcmp(s
->name
, lacp
->name
)) {
181 lacp
->name
= xstrdup(s
->name
);
184 if (!eth_addr_equals(lacp
->sys_id
, s
->id
)
185 || lacp
->sys_priority
!= s
->priority
186 || lacp
->strict
!= s
->strict
) {
187 memcpy(lacp
->sys_id
, s
->id
, ETH_ADDR_LEN
);
188 lacp
->sys_priority
= s
->priority
;
189 lacp
->strict
= s
->strict
;
193 lacp
->active
= s
->active
;
194 lacp
->lacp_time
= s
->lacp_time
;
195 lacp
->force_agg
= s
->force_agg
;
196 lacp
->custom_time
= MAX(TIME_UPDATE_INTERVAL
, s
->custom_time
);
199 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
200 * configured for passive mode. */
202 lacp_is_active(const struct lacp
*lacp
)
207 /* Processes 'pdu', a parsed LACP packet received on 'slave_'. This function
208 * should be called on all packets received on 'slave_' with Ethernet Type
209 * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
211 lacp_process_pdu(struct lacp
*lacp
, const void *slave_
,
212 const struct lacp_pdu
*pdu
)
214 struct slave
*slave
= slave_lookup(lacp
, slave_
);
215 long long int tx_rate
;
217 switch (lacp
->lacp_time
) {
219 tx_rate
= LACP_FAST_TIME_TX
;
222 tx_rate
= LACP_SLOW_TIME_TX
;
224 case LACP_TIME_CUSTOM
:
225 tx_rate
= lacp
->custom_time
;
227 default: NOT_REACHED();
230 slave
->status
= LACP_CURRENT
;
231 timer_set_duration(&slave
->rx
, LACP_RX_MULTIPLIER
* tx_rate
);
233 slave
->ntt_actor
= pdu
->partner
;
235 /* Update our information about our partner if it's out of date. This may
236 * cause priorities to change so re-calculate attached status of all
238 if (memcmp(&slave
->partner
, &pdu
->actor
, sizeof pdu
->actor
)) {
240 slave
->partner
= pdu
->actor
;
244 /* Returns true if 'lacp' has successfully negotiated with its partner. False
245 * if 'lacp' is NULL. */
247 lacp_negotiated(const struct lacp
*lacp
)
249 return lacp
? lacp
->negotiated
: false;
252 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
253 * once per slave in a LACP managed bond. Should also be called whenever a
254 * slave's settings change. */
256 lacp_slave_register(struct lacp
*lacp
, void *slave_
,
257 const struct lacp_slave_settings
*s
)
259 struct slave
*slave
= slave_lookup(lacp
, slave_
);
262 slave
= xzalloc(sizeof *slave
);
265 hmap_insert(&lacp
->slaves
, &slave
->node
, hash_pointer(slave_
, 0));
266 slave_set_defaulted(slave
);
268 if (!lacp
->key_slave
) {
269 lacp
->key_slave
= slave
;
273 if (!slave
->name
|| strcmp(s
->name
, slave
->name
)) {
275 slave
->name
= xstrdup(s
->name
);
278 if (slave
->port_id
!= s
->id
279 || slave
->port_priority
!= s
->priority
280 || slave
->key
!= s
->key
) {
281 slave
->port_id
= s
->id
;
282 slave
->port_priority
= s
->priority
;
287 if (lacp
->active
|| lacp
->negotiated
) {
288 slave_set_expired(slave
);
293 /* Unregisters 'slave_' with 'lacp'. */
295 lacp_slave_unregister(struct lacp
*lacp
, const void *slave_
)
297 struct slave
*slave
= slave_lookup(lacp
, slave_
);
300 slave_destroy(slave
);
305 /* This function should be called whenever the carrier status of 'slave_' has
308 lacp_slave_carrier_changed(const struct lacp
*lacp
, const void *slave_
)
310 struct slave
*slave
= slave_lookup(lacp
, slave_
);
312 if (slave
->status
== LACP_CURRENT
|| slave
->lacp
->active
) {
313 slave_set_expired(slave
);
317 /* This function should be called before enabling 'slave_' to send or receive
318 * traffic. If it returns false, 'slave_' should not enabled. As a
319 * convenience, returns true if 'lacp' is NULL. */
321 lacp_slave_may_enable(const struct lacp
*lacp
, const void *slave_
)
324 struct slave
*slave
= slave_lookup(lacp
, slave_
);
326 /* The slave may be enabled if it's attached to an aggregator and its
327 * partner is synchronized. The only exception is defaulted slaves.
328 * They are not required to have synchronized partners because they
329 * have no partners at all. They will only be attached if negotiations
330 * failed on all slaves in the bond. */
331 return slave
->attached
&& (slave
->partner
.state
& LACP_STATE_SYNC
332 || slave
->status
== LACP_DEFAULTED
);
338 /* Returns the port ID used for 'slave_' in LACP communications. */
340 lacp_slave_get_port_id(const struct lacp
*lacp
, const void *slave_
)
342 struct slave
*slave
= slave_lookup(lacp
, slave_
);
343 return slave
->port_id
;
346 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
347 * not being current, generally indicates a connectivity problem, or a
348 * misconfigured (or broken) partner. */
350 lacp_slave_is_current(const struct lacp
*lacp
, const void *slave_
)
352 return slave_lookup(lacp
, slave_
)->status
== LACP_CURRENT
;
355 /* This function should be called periodically to update 'lacp'. */
357 lacp_run(struct lacp
*lacp
, lacp_send_pdu
*send_pdu
)
361 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
362 if (timer_expired(&slave
->rx
)) {
363 if (slave
->status
== LACP_CURRENT
) {
364 slave_set_expired(slave
);
365 } else if (slave
->status
== LACP_EXPIRED
) {
366 slave_set_defaulted(slave
);
372 lacp_update_attached(lacp
);
375 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
377 struct lacp_info actor
;
379 if (!slave_may_tx(slave
)) {
383 slave_get_actor(slave
, &actor
);
385 if (timer_expired(&slave
->tx
)
386 || !info_tx_equal(&actor
, &slave
->ntt_actor
)) {
387 long long int duration
;
389 slave
->ntt_actor
= actor
;
390 compose_lacp_pdu(&actor
, &slave
->partner
, &pdu
);
391 send_pdu(slave
->aux
, &pdu
);
393 if (lacp
->lacp_time
== LACP_TIME_CUSTOM
) {
394 duration
= lacp
->custom_time
;
396 duration
= (slave
->partner
.state
& LACP_STATE_TIME
398 : LACP_SLOW_TIME_TX
);
401 timer_set_duration(&slave
->tx
, duration
);
406 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
408 lacp_wait(struct lacp
*lacp
)
412 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
413 if (slave_may_tx(slave
)) {
414 timer_wait(&slave
->tx
);
417 if (slave
->status
!= LACP_DEFAULTED
) {
418 timer_wait(&slave
->rx
);
423 /* Static Helpers. */
425 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
426 * negotiated parameter to true if any slaves are attachable. */
428 lacp_update_attached(struct lacp
*lacp
)
430 struct slave
*lead
, *slave
;
431 struct lacp_info lead_pri
;
432 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
434 lacp
->update
= false;
437 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
438 struct lacp_info pri
;
440 slave
->attached
= true;
442 /* XXX: In the future allow users to configure the expected system ID.
443 * For now just special case loopback. */
444 if (eth_addr_equals(slave
->partner
.sys_id
, slave
->lacp
->sys_id
)) {
445 VLOG_WARN_RL(&rl
, "slave %s: Loopback detected. Slave is "
446 "connected to its own bond", slave
->name
);
447 slave
->attached
= false;
451 if (slave
->status
== LACP_DEFAULTED
) {
455 slave_get_priority(slave
, &pri
);
457 if (!lead
|| memcmp(&pri
, &lead_pri
, sizeof pri
) < 0) {
463 lacp
->negotiated
= lead
!= NULL
;
466 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
467 if (slave
->status
== LACP_DEFAULTED
468 || lead
->partner
.key
!= slave
->partner
.key
469 || !eth_addr_equals(lead
->partner
.sys_id
,
470 slave
->partner
.sys_id
)) {
471 slave
->attached
= false;
474 } else if (lacp
->strict
) {
475 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
476 slave
->attached
= false;
482 slave_destroy(struct slave
*slave
)
485 struct lacp
*lacp
= slave
->lacp
;
488 hmap_remove(&lacp
->slaves
, &slave
->node
);
490 if (lacp
->key_slave
== slave
) {
491 struct hmap_node
*slave_node
= hmap_first(&lacp
->slaves
);
494 lacp
->key_slave
= CONTAINER_OF(slave_node
, struct slave
, node
);
496 lacp
->key_slave
= NULL
;
506 slave_set_defaulted(struct slave
*slave
)
508 memset(&slave
->partner
, 0, sizeof slave
->partner
);
510 slave
->lacp
->update
= true;
511 slave
->status
= LACP_DEFAULTED
;
515 slave_set_expired(struct slave
*slave
)
517 struct lacp
*lacp
= slave
->lacp
;
519 slave
->status
= LACP_EXPIRED
;
520 slave
->partner
.state
|= LACP_STATE_TIME
;
521 slave
->partner
.state
&= ~LACP_STATE_SYNC
;
523 /* The spec says we should wait LACP_RX_MULTIPLIER * LACP_FAST_TIME_TX.
524 * This doesn't make sense when using custom times which can be much
525 * smaller than LACP_FAST_TIME. */
526 timer_set_duration(&slave
->rx
, (lacp
->lacp_time
== LACP_TIME_CUSTOM
528 : LACP_RX_MULTIPLIER
* LACP_FAST_TIME_TX
));
532 slave_get_actor(struct slave
*slave
, struct lacp_info
*actor
)
534 struct lacp
*lacp
= slave
->lacp
;
539 state
|= LACP_STATE_ACT
;
542 if (lacp
->lacp_time
!= LACP_TIME_SLOW
) {
543 state
|= LACP_STATE_TIME
;
546 if (slave
->attached
) {
547 state
|= LACP_STATE_SYNC
;
550 if (slave
->status
== LACP_DEFAULTED
) {
551 state
|= LACP_STATE_DEF
;
554 if (slave
->status
== LACP_EXPIRED
) {
555 state
|= LACP_STATE_EXP
;
558 if (lacp
->force_agg
|| hmap_count(&lacp
->slaves
) > 1) {
559 state
|= LACP_STATE_AGG
;
562 if (slave
->attached
|| !lacp
->negotiated
) {
563 state
|= LACP_STATE_COL
| LACP_STATE_DIST
;
566 key
= lacp
->key_slave
->key
;
568 key
= lacp
->key_slave
->port_id
;
571 actor
->state
= state
;
572 actor
->key
= htons(key
);
573 actor
->port_priority
= htons(slave
->port_priority
);
574 actor
->port_id
= htons(slave
->port_id
);
575 actor
->sys_priority
= htons(lacp
->sys_priority
);
576 memcpy(&actor
->sys_id
, lacp
->sys_id
, ETH_ADDR_LEN
);
579 /* Given 'slave', populates 'priority' with data representing its LACP link
580 * priority. If two priority objects populated by this function are compared
581 * using memcmp, the higher priority link will be less than the lower priority
584 slave_get_priority(struct slave
*slave
, struct lacp_info
*priority
)
586 uint16_t partner_priority
, actor_priority
;
588 /* Choose the lacp_info of the higher priority system by comparing their
589 * system priorities and mac addresses. */
590 actor_priority
= slave
->lacp
->sys_priority
;
591 partner_priority
= ntohs(slave
->partner
.sys_priority
);
592 if (actor_priority
< partner_priority
) {
593 slave_get_actor(slave
, priority
);
594 } else if (partner_priority
< actor_priority
) {
595 *priority
= slave
->partner
;
596 } else if (eth_addr_compare_3way(slave
->lacp
->sys_id
,
597 slave
->partner
.sys_id
) < 0) {
598 slave_get_actor(slave
, priority
);
600 *priority
= slave
->partner
;
603 /* Key and state are not used in priority comparisons. */
609 slave_may_tx(const struct slave
*slave
)
611 return slave
->lacp
->active
|| slave
->status
!= LACP_DEFAULTED
;
614 static struct slave
*
615 slave_lookup(const struct lacp
*lacp
, const void *slave_
)
619 HMAP_FOR_EACH_IN_BUCKET (slave
, node
, hash_pointer(slave_
, 0),
621 if (slave
->aux
== slave_
) {
629 /* Two lacp_info structures are tx_equal if and only if they do not differ in
630 * ways which would require a lacp_pdu transmission. */
632 info_tx_equal(struct lacp_info
*a
, struct lacp_info
*b
)
635 /* LACP specification dictates that we transmit whenever the actor and
636 * remote_actor differ in the following fields: Port, Port Priority,
637 * System, System Priority, Aggregation Key, Activity State, Timeout State,
638 * Sync State, and Aggregation State. The state flags are most likely to
639 * change so are checked first. */
640 return !((a
->state
^ b
->state
) & (LACP_STATE_ACT
644 && a
->port_id
== b
->port_id
645 && a
->port_priority
== b
->port_priority
647 && a
->sys_priority
== b
->sys_priority
648 && eth_addr_equals(a
->sys_id
, b
->sys_id
);
652 lacp_find(const char *name
)
656 LIST_FOR_EACH (lacp
, node
, &all_lacps
) {
657 if (!strcmp(lacp
->name
, name
)) {
666 ds_put_lacp_state(struct ds
*ds
, uint8_t state
)
668 if (state
& LACP_STATE_ACT
) {
669 ds_put_cstr(ds
, "activity ");
672 if (state
& LACP_STATE_TIME
) {
673 ds_put_cstr(ds
, "timeout ");
676 if (state
& LACP_STATE_AGG
) {
677 ds_put_cstr(ds
, "aggregation ");
680 if (state
& LACP_STATE_SYNC
) {
681 ds_put_cstr(ds
, "synchronized ");
684 if (state
& LACP_STATE_COL
) {
685 ds_put_cstr(ds
, "collecting ");
688 if (state
& LACP_STATE_DIST
) {
689 ds_put_cstr(ds
, "distributing ");
692 if (state
& LACP_STATE_DEF
) {
693 ds_put_cstr(ds
, "defaulted ");
696 if (state
& LACP_STATE_EXP
) {
697 ds_put_cstr(ds
, "expired ");
702 lacp_unixctl_show(struct unixctl_conn
*conn
,
703 const char *args
, void *aux OVS_UNUSED
)
705 struct ds ds
= DS_EMPTY_INITIALIZER
;
709 lacp
= lacp_find(args
);
711 unixctl_command_reply(conn
, 501, "no such lacp object");
715 ds_put_format(&ds
, "lacp: %s\n", lacp
->name
);
717 ds_put_format(&ds
, "\tstatus: %s", lacp
->active
? "active" : "passive");
719 ds_put_cstr(&ds
, " strict");
721 if (lacp
->negotiated
) {
722 ds_put_cstr(&ds
, " negotiated");
724 ds_put_cstr(&ds
, "\n");
726 ds_put_format(&ds
, "\tsys_id: " ETH_ADDR_FMT
"\n", ETH_ADDR_ARGS(lacp
->sys_id
));
727 ds_put_format(&ds
, "\tsys_priority: %u\n", lacp
->sys_priority
);
728 ds_put_cstr(&ds
, "\taggregation key: ");
729 if (lacp
->key_slave
) {
730 ds_put_format(&ds
, "%u", lacp
->key_slave
->port_id
);
732 ds_put_cstr(&ds
, "none");
734 ds_put_cstr(&ds
, "\n");
736 ds_put_cstr(&ds
, "\tlacp_time: ");
737 switch (lacp
->lacp_time
) {
739 ds_put_cstr(&ds
, "fast\n");
742 ds_put_cstr(&ds
, "slow\n");
744 case LACP_TIME_CUSTOM
:
745 ds_put_format(&ds
, "custom (%lld)\n", lacp
->custom_time
);
748 ds_put_cstr(&ds
, "unknown\n");
751 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
753 struct lacp_info actor
;
755 slave_get_actor(slave
, &actor
);
756 switch (slave
->status
) {
764 status
= "defaulted";
770 ds_put_format(&ds
, "\nslave: %s: %s %s\n", slave
->name
, status
,
771 slave
->attached
? "attached" : "detached");
772 ds_put_format(&ds
, "\tport_id: %u\n", slave
->port_id
);
773 ds_put_format(&ds
, "\tport_priority: %u\n", slave
->port_priority
);
775 ds_put_format(&ds
, "\n\tactor sys_id: " ETH_ADDR_FMT
"\n",
776 ETH_ADDR_ARGS(actor
.sys_id
));
777 ds_put_format(&ds
, "\tactor sys_priority: %u\n",
778 ntohs(actor
.sys_priority
));
779 ds_put_format(&ds
, "\tactor port_id: %u\n",
780 ntohs(actor
.port_id
));
781 ds_put_format(&ds
, "\tactor port_priority: %u\n",
782 ntohs(actor
.port_priority
));
783 ds_put_format(&ds
, "\tactor key: %u\n",
785 ds_put_cstr(&ds
, "\tactor state: ");
786 ds_put_lacp_state(&ds
, actor
.state
);
787 ds_put_cstr(&ds
, "\n\n");
789 ds_put_format(&ds
, "\tpartner sys_id: " ETH_ADDR_FMT
"\n",
790 ETH_ADDR_ARGS(slave
->partner
.sys_id
));
791 ds_put_format(&ds
, "\tpartner sys_priority: %u\n",
792 ntohs(slave
->partner
.sys_priority
));
793 ds_put_format(&ds
, "\tpartner port_id: %u\n",
794 ntohs(slave
->partner
.port_id
));
795 ds_put_format(&ds
, "\tpartner port_priority: %u\n",
796 ntohs(slave
->partner
.port_priority
));
797 ds_put_format(&ds
, "\tpartner key: %u\n",
798 ntohs(slave
->partner
.key
));
799 ds_put_cstr(&ds
, "\tpartner state: ");
800 ds_put_lacp_state(&ds
, slave
->partner
.state
);
801 ds_put_cstr(&ds
, "\n");
804 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));