1 /* Copyright (c) 2011, 2012, 2013, 2014, 2015 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 "connectivity.h"
22 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/hmap.h"
25 #include "dp-packet.h"
26 #include "ovs-atomic.h"
28 #include "openvswitch/poll-loop.h"
30 #include "openvswitch/shash.h"
34 #include "openvswitch/vlog.h"
37 VLOG_DEFINE_THIS_MODULE(lacp
);
39 /* Masks for lacp_info state member. */
40 #define LACP_STATE_ACT 0x01 /* Activity. Active or passive? */
41 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
42 #define LACP_STATE_AGG 0x04 /* Aggregation. Is the link is bondable? */
43 #define LACP_STATE_SYNC 0x08 /* Synchronization. Is the link in up to date? */
44 #define LACP_STATE_COL 0x10 /* Collecting. Is the link receiving frames? */
45 #define LACP_STATE_DIST 0x20 /* Distributing. Is the link sending frames? */
46 #define LACP_STATE_DEF 0x40 /* Defaulted. Using default partner info? */
47 #define LACP_STATE_EXP 0x80 /* Expired. Using expired partner info? */
49 #define LACP_FAST_TIME_TX 1000 /* Fast transmission rate. */
50 #define LACP_SLOW_TIME_TX 30000 /* Slow transmission rate. */
51 #define LACP_RX_MULTIPLIER 3 /* Multiply by TX rate to get RX rate. */
53 #define LACP_INFO_LEN 15
56 ovs_be16 sys_priority
; /* System priority. */
57 struct eth_addr sys_id
; /* System ID. */
58 ovs_be16 key
; /* Operational key. */
59 ovs_be16 port_priority
; /* Port priority. */
60 ovs_be16 port_id
; /* Port ID. */
61 uint8_t state
; /* State mask. See LACP_STATE macros. */
63 BUILD_ASSERT_DECL(LACP_INFO_LEN
== sizeof(struct lacp_info
));
65 #define LACP_PDU_LEN 110
67 uint8_t subtype
; /* Always 1. */
68 uint8_t version
; /* Always 1. */
70 uint8_t actor_type
; /* Always 1. */
71 uint8_t actor_len
; /* Always 20. */
72 struct lacp_info actor
; /* LACP actor information. */
73 uint8_t z1
[3]; /* Reserved. Always 0. */
75 uint8_t partner_type
; /* Always 2. */
76 uint8_t partner_len
; /* Always 20. */
77 struct lacp_info partner
; /* LACP partner information. */
78 uint8_t z2
[3]; /* Reserved. Always 0. */
80 uint8_t collector_type
; /* Always 3. */
81 uint8_t collector_len
; /* Always 16. */
82 ovs_be16 collector_delay
; /* Maximum collector delay. Set to UINT16_MAX. */
83 uint8_t z3
[64]; /* Combination of several fields. Always 0. */
85 BUILD_ASSERT_DECL(LACP_PDU_LEN
== sizeof(struct lacp_pdu
));
90 LACP_CURRENT
, /* Current State. Partner up to date. */
91 LACP_EXPIRED
, /* Expired State. Partner out of date. */
92 LACP_DEFAULTED
, /* Defaulted State. No partner. */
96 struct ovs_list node
; /* Node in all_lacps list. */
97 char *name
; /* Name of this lacp object. */
98 struct eth_addr sys_id
; /* System ID. */
99 uint16_t sys_priority
; /* System Priority. */
100 bool active
; /* Active or Passive. */
102 struct hmap slaves
; /* Slaves this LACP object controls. */
103 struct slave
*key_slave
; /* Slave whose ID will be the aggregation key. */
105 bool fast
; /* True if using fast probe interval. */
106 bool negotiated
; /* True if LACP negotiations were successful. */
107 bool update
; /* True if lacp_update() needs to be called. */
108 bool fallback_ab
; /* True if fallback to active-backup on LACP failure. */
110 struct ovs_refcount ref_cnt
;
114 void *aux
; /* Handle used to identify this slave. */
115 struct hmap_node node
; /* Node in master's slaves map. */
117 struct lacp
*lacp
; /* LACP object containing this slave. */
118 uint16_t port_id
; /* Port ID. */
119 uint16_t port_priority
; /* Port Priority. */
120 uint16_t key
; /* Aggregation Key. 0 if default. */
121 char *name
; /* Name of this slave. */
123 enum slave_status status
; /* Slave status. */
124 bool attached
; /* Attached. Traffic may flow. */
125 struct lacp_info partner
; /* Partner information. */
126 struct lacp_info ntt_actor
; /* Used to decide if we Need To Transmit. */
127 struct timer tx
; /* Next message transmission timer. */
128 struct timer rx
; /* Expected message receive timer. */
130 uint32_t count_rx_pdus
; /* dot3adAggPortStatsLACPDUsRx */
131 uint32_t count_rx_pdus_bad
; /* dot3adAggPortStatsIllegalRx */
132 uint32_t count_tx_pdus
; /* dot3adAggPortStatsLACPDUsTx */
133 uint32_t count_link_expired
; /* Num of times link expired */
134 uint32_t count_link_defaulted
; /* Num of times link defaulted */
135 uint32_t count_carrier_changed
; /* Num of times link status changed */
138 static struct ovs_mutex mutex
;
139 static struct ovs_list all_lacps__
= OVS_LIST_INITIALIZER(&all_lacps__
);
140 static struct ovs_list
*const all_lacps
OVS_GUARDED_BY(mutex
) = &all_lacps__
;
142 static void lacp_update_attached(struct lacp
*) OVS_REQUIRES(mutex
);
144 static void slave_destroy(struct slave
*) OVS_REQUIRES(mutex
);
145 static void slave_set_defaulted(struct slave
*) OVS_REQUIRES(mutex
);
146 static void slave_set_expired(struct slave
*) OVS_REQUIRES(mutex
);
147 static void slave_get_actor(struct slave
*, struct lacp_info
*actor
)
149 static void slave_get_priority(struct slave
*, struct lacp_info
*priority
)
151 static bool slave_may_tx(const struct slave
*)
153 static struct slave
*slave_lookup(const struct lacp
*, const void *slave
)
155 static bool info_tx_equal(struct lacp_info
*, struct lacp_info
*)
157 static bool slave_may_enable__(struct slave
*slave
) OVS_REQUIRES(mutex
);
159 static unixctl_cb_func lacp_unixctl_show
;
160 static unixctl_cb_func lacp_unixctl_show_stats
;
162 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
164 compose_lacp_pdu(const struct lacp_info
*actor
,
165 const struct lacp_info
*partner
, struct lacp_pdu
*pdu
)
167 memset(pdu
, 0, sizeof *pdu
);
176 pdu
->partner_type
= 2;
177 pdu
->partner_len
= 20;
178 pdu
->partner
= *partner
;
180 pdu
->collector_type
= 3;
181 pdu
->collector_len
= 16;
182 pdu
->collector_delay
= htons(0);
185 /* Parses 'b' which represents a packet containing a LACP PDU. This function
186 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
187 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
189 static const struct lacp_pdu
*
190 parse_lacp_packet(const struct dp_packet
*p
)
192 const struct lacp_pdu
*pdu
;
194 pdu
= dp_packet_at(p
, (uint8_t *)dp_packet_l3(p
) - (uint8_t *)dp_packet_data(p
),
197 if (pdu
&& pdu
->subtype
== 1
198 && pdu
->actor_type
== 1 && pdu
->actor_len
== 20
199 && pdu
->partner_type
== 2 && pdu
->partner_len
== 20) {
206 /* LACP Protocol Implementation. */
208 /* Initializes the lacp module. */
212 unixctl_command_register("lacp/show", "[port]", 0, 1,
213 lacp_unixctl_show
, NULL
);
214 unixctl_command_register("lacp/show-stats", "[port]", 0, 1,
215 lacp_unixctl_show_stats
, NULL
);
219 lacp_lock(void) OVS_ACQUIRES(mutex
)
221 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
223 if (ovsthread_once_start(&once
)) {
224 ovs_mutex_init_recursive(&mutex
);
225 ovsthread_once_done(&once
);
227 ovs_mutex_lock(&mutex
);
231 lacp_unlock(void) OVS_RELEASES(mutex
)
233 ovs_mutex_unlock(&mutex
);
236 /* Creates a LACP object. */
238 lacp_create(void) OVS_EXCLUDED(mutex
)
242 lacp
= xzalloc(sizeof *lacp
);
243 hmap_init(&lacp
->slaves
);
244 ovs_refcount_init(&lacp
->ref_cnt
);
247 ovs_list_push_back(all_lacps
, &lacp
->node
);
253 lacp_ref(const struct lacp
*lacp_
)
255 struct lacp
*lacp
= CONST_CAST(struct lacp
*, lacp_
);
257 ovs_refcount_ref(&lacp
->ref_cnt
);
262 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
264 lacp_unref(struct lacp
*lacp
) OVS_EXCLUDED(mutex
)
266 if (lacp
&& ovs_refcount_unref_relaxed(&lacp
->ref_cnt
) == 1) {
267 struct slave
*slave
, *next
;
270 HMAP_FOR_EACH_SAFE (slave
, next
, node
, &lacp
->slaves
) {
271 slave_destroy(slave
);
274 hmap_destroy(&lacp
->slaves
);
275 ovs_list_remove(&lacp
->node
);
282 /* Configures 'lacp' with settings from 's'. */
284 lacp_configure(struct lacp
*lacp
, const struct lacp_settings
*s
)
287 ovs_assert(!eth_addr_is_zero(s
->id
));
290 if (!lacp
->name
|| strcmp(s
->name
, lacp
->name
)) {
292 lacp
->name
= xstrdup(s
->name
);
295 if (!eth_addr_equals(lacp
->sys_id
, s
->id
)
296 || lacp
->sys_priority
!= s
->priority
) {
297 lacp
->sys_id
= s
->id
;
298 lacp
->sys_priority
= s
->priority
;
302 lacp
->active
= s
->active
;
303 lacp
->fast
= s
->fast
;
305 if (lacp
->fallback_ab
!= s
->fallback_ab_cfg
) {
306 lacp
->fallback_ab
= s
->fallback_ab_cfg
;
313 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
314 * configured for passive mode. */
316 lacp_is_active(const struct lacp
*lacp
) OVS_EXCLUDED(mutex
)
325 /* Processes 'packet' which was received on 'slave_'. This function should be
326 * called on all packets received on 'slave_' with Ethernet Type ETH_TYPE_LACP.
329 lacp_process_packet(struct lacp
*lacp
, const void *slave_
,
330 const struct dp_packet
*packet
)
333 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
334 const struct lacp_pdu
*pdu
;
335 long long int tx_rate
;
337 bool lacp_may_enable
= false;
340 slave
= slave_lookup(lacp
, slave_
);
344 slave
->count_rx_pdus
++;
346 pdu
= parse_lacp_packet(packet
);
348 slave
->count_rx_pdus_bad
++;
349 VLOG_WARN_RL(&rl
, "%s: received an unparsable LACP PDU.", lacp
->name
);
353 slave
->status
= LACP_CURRENT
;
354 tx_rate
= lacp
->fast
? LACP_FAST_TIME_TX
: LACP_SLOW_TIME_TX
;
355 timer_set_duration(&slave
->rx
, LACP_RX_MULTIPLIER
* tx_rate
);
357 slave
->ntt_actor
= pdu
->partner
;
359 /* Update our information about our partner if it's out of date. This may
360 * cause priorities to change so re-calculate attached status of all
362 if (memcmp(&slave
->partner
, &pdu
->actor
, sizeof pdu
->actor
)) {
364 slave
->partner
= pdu
->actor
;
367 /* Evaluate may_enable here to avoid dropping of packets till main thread
368 * sets may_enable to true. */
369 lacp_may_enable
= slave_may_enable__(slave
);
374 return lacp_may_enable
;
377 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
379 lacp_status(const struct lacp
*lacp
) OVS_EXCLUDED(mutex
)
382 enum lacp_status ret
;
385 ret
= lacp
->negotiated
? LACP_NEGOTIATED
: LACP_CONFIGURED
;
389 /* Don't take 'mutex'. It might not even be initialized, since we
390 * don't know that any lacp object has been created. */
391 return LACP_DISABLED
;
395 /* Registers 'slave_' as subordinate to 'lacp'. This should be called at least
396 * once per slave in a LACP managed bond. Should also be called whenever a
397 * slave's settings change. */
399 lacp_slave_register(struct lacp
*lacp
, void *slave_
,
400 const struct lacp_slave_settings
*s
)
406 slave
= slave_lookup(lacp
, slave_
);
408 slave
= xzalloc(sizeof *slave
);
411 hmap_insert(&lacp
->slaves
, &slave
->node
, hash_pointer(slave_
, 0));
412 slave_set_defaulted(slave
);
414 if (!lacp
->key_slave
) {
415 lacp
->key_slave
= slave
;
419 if (!slave
->name
|| strcmp(s
->name
, slave
->name
)) {
421 slave
->name
= xstrdup(s
->name
);
424 if (slave
->port_id
!= s
->id
425 || slave
->port_priority
!= s
->priority
426 || slave
->key
!= s
->key
) {
427 slave
->port_id
= s
->id
;
428 slave
->port_priority
= s
->priority
;
433 if (lacp
->active
|| lacp
->negotiated
) {
434 slave_set_expired(slave
);
440 /* Unregisters 'slave_' with 'lacp'. */
442 lacp_slave_unregister(struct lacp
*lacp
, const void *slave_
)
448 slave
= slave_lookup(lacp
, slave_
);
450 slave_destroy(slave
);
456 /* This function should be called whenever the carrier status of 'slave_' has
457 * changed. If 'lacp' is null, this function has no effect.*/
459 lacp_slave_carrier_changed(const struct lacp
*lacp
, const void *slave_
)
468 slave
= slave_lookup(lacp
, slave_
);
473 if (slave
->status
== LACP_CURRENT
|| slave
->lacp
->active
) {
474 slave_set_expired(slave
);
476 slave
->count_carrier_changed
++;
483 slave_may_enable__(struct slave
*slave
) OVS_REQUIRES(mutex
)
485 /* The slave may be enabled if it's attached to an aggregator and its
486 * partner is synchronized.*/
487 return slave
->attached
&& (slave
->partner
.state
& LACP_STATE_SYNC
488 || (slave
->lacp
&& slave
->lacp
->fallback_ab
489 && slave
->status
== LACP_DEFAULTED
));
492 /* This function should be called before enabling 'slave_' to send or receive
493 * traffic. If it returns false, 'slave_' should not enabled. As a
494 * convenience, returns true if 'lacp' is NULL. */
496 lacp_slave_may_enable(const struct lacp
*lacp
, const void *slave_
)
504 slave
= slave_lookup(lacp
, slave_
);
505 ret
= slave
? slave_may_enable__(slave
) : false;
513 /* Returns true if partner information on 'slave_' is up to date. 'slave_'
514 * not being current, generally indicates a connectivity problem, or a
515 * misconfigured (or broken) partner. */
517 lacp_slave_is_current(const struct lacp
*lacp
, const void *slave_
)
524 slave
= slave_lookup(lacp
, slave_
);
525 ret
= slave
? slave
->status
!= LACP_DEFAULTED
: false;
530 /* This function should be called periodically to update 'lacp'. */
532 lacp_run(struct lacp
*lacp
, lacp_send_pdu
*send_pdu
) OVS_EXCLUDED(mutex
)
537 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
538 if (timer_expired(&slave
->rx
)) {
539 enum slave_status old_status
= slave
->status
;
541 if (slave
->status
== LACP_CURRENT
) {
542 slave_set_expired(slave
);
543 slave
->count_link_expired
++;
544 } else if (slave
->status
== LACP_EXPIRED
) {
545 slave_set_defaulted(slave
);
546 slave
->count_link_defaulted
++;
548 if (slave
->status
!= old_status
) {
549 seq_change(connectivity_seq_get());
555 lacp_update_attached(lacp
);
556 seq_change(connectivity_seq_get());
559 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
560 struct lacp_info actor
;
562 if (!slave_may_tx(slave
)) {
566 slave_get_actor(slave
, &actor
);
568 if (timer_expired(&slave
->tx
)
569 || !info_tx_equal(&actor
, &slave
->ntt_actor
)) {
570 long long int duration
;
573 slave
->ntt_actor
= actor
;
574 compose_lacp_pdu(&actor
, &slave
->partner
, &pdu
);
575 send_pdu(slave
->aux
, &pdu
, sizeof pdu
);
576 slave
->count_tx_pdus
++;
578 duration
= (slave
->partner
.state
& LACP_STATE_TIME
580 : LACP_SLOW_TIME_TX
);
582 timer_set_duration(&slave
->tx
, duration
);
583 seq_change(connectivity_seq_get());
589 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
591 lacp_wait(struct lacp
*lacp
) OVS_EXCLUDED(mutex
)
596 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
597 if (slave_may_tx(slave
)) {
598 timer_wait(&slave
->tx
);
601 if (slave
->status
!= LACP_DEFAULTED
) {
602 timer_wait(&slave
->rx
);
608 /* Static Helpers. */
610 /* Updates the attached status of all slaves controlled by 'lacp' and sets its
611 * negotiated parameter to true if any slaves are attachable. */
613 lacp_update_attached(struct lacp
*lacp
) OVS_REQUIRES(mutex
)
615 struct slave
*lead
, *lead_current
, *slave
;
616 struct lacp_info lead_pri
;
618 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
620 lacp
->update
= false;
626 /* Check if there is a working interface.
627 * Store as lead_current, if there is one. */
628 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
629 if (slave
->status
== LACP_CURRENT
&& slave
->attached
) {
630 struct lacp_info pri
;
631 slave_get_priority(slave
, &pri
);
632 if (!lead_current
|| memcmp(&pri
, &lead_pri
, sizeof pri
) < 0) {
633 lead_current
= slave
;
641 /* Find interface with highest priority. */
642 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
643 struct lacp_info pri
;
645 slave
->attached
= false;
647 /* XXX: In the future allow users to configure the expected system ID.
648 * For now just special case loopback. */
649 if (eth_addr_equals(slave
->partner
.sys_id
, slave
->lacp
->sys_id
)) {
650 VLOG_WARN_RL(&rl
, "slave %s: Loopback detected. Slave is "
651 "connected to its own bond", slave
->name
);
655 if (slave
->status
== LACP_DEFAULTED
) {
656 if (lacp
->fallback_ab
) {
657 slave
->attached
= true;
662 slave_get_priority(slave
, &pri
);
663 bool enable
= slave_may_enable__(slave
);
665 /* Check if partner MAC address is the same as on the working
666 * interface. Activate slave only if the MAC is the same, or
667 * there is no working interface. */
668 if (!lead_current
|| (lead_current
669 && eth_addr_equals(slave
->partner
.sys_id
,
670 lead_current
->partner
.sys_id
))) {
671 slave
->attached
= true;
673 if (slave
->attached
&&
675 || enable
> lead_enable
676 || (enable
== lead_enable
677 && memcmp(&pri
, &lead_pri
, sizeof pri
) < 0))) {
679 lead_enable
= enable
;
684 lacp
->negotiated
= lead
!= NULL
;
687 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
688 if ((lacp
->fallback_ab
&& slave
->status
== LACP_DEFAULTED
)
689 || lead
->partner
.key
!= slave
->partner
.key
690 || !eth_addr_equals(lead
->partner
.sys_id
,
691 slave
->partner
.sys_id
)) {
692 slave
->attached
= false;
699 slave_destroy(struct slave
*slave
) OVS_REQUIRES(mutex
)
702 struct lacp
*lacp
= slave
->lacp
;
705 hmap_remove(&lacp
->slaves
, &slave
->node
);
707 if (lacp
->key_slave
== slave
) {
708 struct hmap_node
*slave_node
= hmap_first(&lacp
->slaves
);
711 lacp
->key_slave
= CONTAINER_OF(slave_node
, struct slave
, node
);
713 lacp
->key_slave
= NULL
;
723 slave_set_defaulted(struct slave
*slave
) OVS_REQUIRES(mutex
)
725 memset(&slave
->partner
, 0, sizeof slave
->partner
);
727 slave
->lacp
->update
= true;
728 slave
->status
= LACP_DEFAULTED
;
732 slave_set_expired(struct slave
*slave
) OVS_REQUIRES(mutex
)
734 slave
->status
= LACP_EXPIRED
;
735 slave
->partner
.state
|= LACP_STATE_TIME
;
736 slave
->partner
.state
&= ~LACP_STATE_SYNC
;
738 timer_set_duration(&slave
->rx
, LACP_RX_MULTIPLIER
* LACP_FAST_TIME_TX
);
742 slave_get_actor(struct slave
*slave
, struct lacp_info
*actor
)
745 struct lacp
*lacp
= slave
->lacp
;
750 state
|= LACP_STATE_ACT
;
754 state
|= LACP_STATE_TIME
;
757 if (slave
->attached
) {
758 state
|= LACP_STATE_SYNC
;
761 if (slave
->status
== LACP_DEFAULTED
) {
762 state
|= LACP_STATE_DEF
;
765 if (slave
->status
== LACP_EXPIRED
) {
766 state
|= LACP_STATE_EXP
;
769 if (hmap_count(&lacp
->slaves
) > 1) {
770 state
|= LACP_STATE_AGG
;
773 if (slave
->attached
|| !lacp
->negotiated
) {
774 state
|= LACP_STATE_COL
| LACP_STATE_DIST
;
777 key
= lacp
->key_slave
->key
;
779 key
= lacp
->key_slave
->port_id
;
782 actor
->state
= state
;
783 actor
->key
= htons(key
);
784 actor
->port_priority
= htons(slave
->port_priority
);
785 actor
->port_id
= htons(slave
->port_id
);
786 actor
->sys_priority
= htons(lacp
->sys_priority
);
787 actor
->sys_id
= lacp
->sys_id
;
790 /* Given 'slave', populates 'priority' with data representing its LACP link
791 * priority. If two priority objects populated by this function are compared
792 * using memcmp, the higher priority link will be less than the lower priority
795 slave_get_priority(struct slave
*slave
, struct lacp_info
*priority
)
798 uint16_t partner_priority
, actor_priority
;
800 /* Choose the lacp_info of the higher priority system by comparing their
801 * system priorities and mac addresses. */
802 actor_priority
= slave
->lacp
->sys_priority
;
803 partner_priority
= ntohs(slave
->partner
.sys_priority
);
804 if (actor_priority
< partner_priority
) {
805 slave_get_actor(slave
, priority
);
806 } else if (partner_priority
< actor_priority
) {
807 *priority
= slave
->partner
;
808 } else if (eth_addr_compare_3way(slave
->lacp
->sys_id
,
809 slave
->partner
.sys_id
) < 0) {
810 slave_get_actor(slave
, priority
);
812 *priority
= slave
->partner
;
815 /* Key and state are not used in priority comparisons. */
821 slave_may_tx(const struct slave
*slave
) OVS_REQUIRES(mutex
)
823 return slave
->lacp
->active
|| slave
->status
!= LACP_DEFAULTED
;
826 static struct slave
*
827 slave_lookup(const struct lacp
*lacp
, const void *slave_
) OVS_REQUIRES(mutex
)
831 HMAP_FOR_EACH_IN_BUCKET (slave
, node
, hash_pointer(slave_
, 0),
833 if (slave
->aux
== slave_
) {
841 /* Two lacp_info structures are tx_equal if and only if they do not differ in
842 * ways which would require a lacp_pdu transmission. */
844 info_tx_equal(struct lacp_info
*a
, struct lacp_info
*b
)
847 /* LACP specification dictates that we transmit whenever the actor and
848 * remote_actor differ in the following fields: Port, Port Priority,
849 * System, System Priority, Aggregation Key, Activity State, Timeout State,
850 * Sync State, and Aggregation State. The state flags are most likely to
851 * change so are checked first. */
852 return !((a
->state
^ b
->state
) & (LACP_STATE_ACT
856 && a
->port_id
== b
->port_id
857 && a
->port_priority
== b
->port_priority
859 && a
->sys_priority
== b
->sys_priority
860 && eth_addr_equals(a
->sys_id
, b
->sys_id
);
864 lacp_find(const char *name
) OVS_REQUIRES(mutex
)
868 LIST_FOR_EACH (lacp
, node
, all_lacps
) {
869 if (!strcmp(lacp
->name
, name
)) {
878 ds_put_lacp_state(struct ds
*ds
, uint8_t state
)
880 if (state
& LACP_STATE_ACT
) {
881 ds_put_cstr(ds
, " activity");
884 if (state
& LACP_STATE_TIME
) {
885 ds_put_cstr(ds
, " timeout");
888 if (state
& LACP_STATE_AGG
) {
889 ds_put_cstr(ds
, " aggregation");
892 if (state
& LACP_STATE_SYNC
) {
893 ds_put_cstr(ds
, " synchronized");
896 if (state
& LACP_STATE_COL
) {
897 ds_put_cstr(ds
, " collecting");
900 if (state
& LACP_STATE_DIST
) {
901 ds_put_cstr(ds
, " distributing");
904 if (state
& LACP_STATE_DEF
) {
905 ds_put_cstr(ds
, " defaulted");
908 if (state
& LACP_STATE_EXP
) {
909 ds_put_cstr(ds
, " expired");
914 lacp_print_details(struct ds
*ds
, struct lacp
*lacp
) OVS_REQUIRES(mutex
)
916 struct shash slave_shash
= SHASH_INITIALIZER(&slave_shash
);
917 const struct shash_node
**sorted_slaves
= NULL
;
922 ds_put_format(ds
, "---- %s ----\n", lacp
->name
);
923 ds_put_format(ds
, " status: %s", lacp
->active
? "active" : "passive");
924 if (lacp
->negotiated
) {
925 ds_put_cstr(ds
, " negotiated");
927 ds_put_cstr(ds
, "\n");
929 ds_put_format(ds
, " sys_id: " ETH_ADDR_FMT
"\n", ETH_ADDR_ARGS(lacp
->sys_id
));
930 ds_put_format(ds
, " sys_priority: %u\n", lacp
->sys_priority
);
931 ds_put_cstr(ds
, " aggregation key: ");
932 if (lacp
->key_slave
) {
933 ds_put_format(ds
, "%u", lacp
->key_slave
->key
934 ? lacp
->key_slave
->key
935 : lacp
->key_slave
->port_id
);
937 ds_put_cstr(ds
, "none");
939 ds_put_cstr(ds
, "\n");
941 ds_put_cstr(ds
, " lacp_time: ");
943 ds_put_cstr(ds
, "fast\n");
945 ds_put_cstr(ds
, "slow\n");
948 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
949 shash_add(&slave_shash
, slave
->name
, slave
);
951 sorted_slaves
= shash_sort(&slave_shash
);
953 for (i
= 0; i
< shash_count(&slave_shash
); i
++) {
955 struct lacp_info actor
;
957 slave
= sorted_slaves
[i
]->data
;
958 slave_get_actor(slave
, &actor
);
959 switch (slave
->status
) {
967 status
= "defaulted";
973 ds_put_format(ds
, "\nslave: %s: %s %s\n", slave
->name
, status
,
974 slave
->attached
? "attached" : "detached");
975 ds_put_format(ds
, " port_id: %u\n", slave
->port_id
);
976 ds_put_format(ds
, " port_priority: %u\n", slave
->port_priority
);
977 ds_put_format(ds
, " may_enable: %s\n", (slave_may_enable__(slave
)
978 ? "true" : "false"));
980 ds_put_format(ds
, "\n actor sys_id: " ETH_ADDR_FMT
"\n",
981 ETH_ADDR_ARGS(actor
.sys_id
));
982 ds_put_format(ds
, " actor sys_priority: %u\n",
983 ntohs(actor
.sys_priority
));
984 ds_put_format(ds
, " actor port_id: %u\n",
985 ntohs(actor
.port_id
));
986 ds_put_format(ds
, " actor port_priority: %u\n",
987 ntohs(actor
.port_priority
));
988 ds_put_format(ds
, " actor key: %u\n",
990 ds_put_cstr(ds
, " actor state:");
991 ds_put_lacp_state(ds
, actor
.state
);
992 ds_put_cstr(ds
, "\n\n");
994 ds_put_format(ds
, " partner sys_id: " ETH_ADDR_FMT
"\n",
995 ETH_ADDR_ARGS(slave
->partner
.sys_id
));
996 ds_put_format(ds
, " partner sys_priority: %u\n",
997 ntohs(slave
->partner
.sys_priority
));
998 ds_put_format(ds
, " partner port_id: %u\n",
999 ntohs(slave
->partner
.port_id
));
1000 ds_put_format(ds
, " partner port_priority: %u\n",
1001 ntohs(slave
->partner
.port_priority
));
1002 ds_put_format(ds
, " partner key: %u\n",
1003 ntohs(slave
->partner
.key
));
1004 ds_put_cstr(ds
, " partner state:");
1005 ds_put_lacp_state(ds
, slave
->partner
.state
);
1006 ds_put_cstr(ds
, "\n");
1009 shash_destroy(&slave_shash
);
1010 free(sorted_slaves
);
1014 lacp_print_stats(struct ds
*ds
, struct lacp
*lacp
) OVS_REQUIRES(mutex
)
1016 struct shash slave_shash
= SHASH_INITIALIZER(&slave_shash
);
1017 const struct shash_node
**sorted_slaves
= NULL
;
1019 struct slave
*slave
;
1022 ds_put_format(ds
, "---- %s statistics ----\n", lacp
->name
);
1024 HMAP_FOR_EACH (slave
, node
, &lacp
->slaves
) {
1025 shash_add(&slave_shash
, slave
->name
, slave
);
1027 sorted_slaves
= shash_sort(&slave_shash
);
1029 for (i
= 0; i
< shash_count(&slave_shash
); i
++) {
1030 slave
= sorted_slaves
[i
]->data
;
1031 ds_put_format(ds
, "\nslave: %s:\n", slave
->name
);
1032 ds_put_format(ds
, " RX PDUs: %u\n", slave
->count_rx_pdus
);
1033 ds_put_format(ds
, " RX Bad PDUs: %u\n", slave
->count_rx_pdus_bad
);
1034 ds_put_format(ds
, " TX PDUs: %u\n", slave
->count_tx_pdus
);
1035 ds_put_format(ds
, " Link Expired: %u\n",
1036 slave
->count_link_expired
);
1037 ds_put_format(ds
, " Link Defaulted: %u\n",
1038 slave
->count_link_defaulted
);
1039 ds_put_format(ds
, " Carrier Status Changed: %u\n",
1040 slave
->count_carrier_changed
);
1043 shash_destroy(&slave_shash
);
1044 free(sorted_slaves
);
1048 lacp_unixctl_show(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1049 void *aux OVS_UNUSED
) OVS_EXCLUDED(mutex
)
1051 struct ds ds
= DS_EMPTY_INITIALIZER
;
1056 lacp
= lacp_find(argv
[1]);
1058 unixctl_command_reply_error(conn
, "no such lacp object");
1061 lacp_print_details(&ds
, lacp
);
1063 LIST_FOR_EACH (lacp
, node
, all_lacps
) {
1064 lacp_print_details(&ds
, lacp
);
1068 unixctl_command_reply(conn
, ds_cstr(&ds
));
1076 lacp_unixctl_show_stats(struct unixctl_conn
*conn
,
1079 void *aux OVS_UNUSED
) OVS_EXCLUDED(mutex
)
1081 struct ds ds
= DS_EMPTY_INITIALIZER
;
1086 lacp
= lacp_find(argv
[1]);
1088 unixctl_command_reply_error(conn
, "no such lacp object");
1091 lacp_print_stats(&ds
, lacp
);
1093 LIST_FOR_EACH (lacp
, node
, all_lacps
) {
1094 lacp_print_stats(&ds
, lacp
);
1098 unixctl_command_reply(conn
, ds_cstr(&ds
));
1105 /* Extract a snapshot of the current state and counters for a slave port.
1106 Return false if the slave is not active. */
1108 lacp_get_slave_stats(const struct lacp
*lacp
, const void *slave_
, struct lacp_slave_stats
*stats
)
1111 struct slave
*slave
;
1112 struct lacp_info actor
;
1115 ovs_mutex_lock(&mutex
);
1117 slave
= slave_lookup(lacp
, slave_
);
1120 slave_get_actor(slave
, &actor
);
1121 stats
->dot3adAggPortActorSystemID
= actor
.sys_id
;
1122 stats
->dot3adAggPortPartnerOperSystemID
= slave
->partner
.sys_id
;
1123 stats
->dot3adAggPortAttachedAggID
= (lacp
->key_slave
->key
?
1124 lacp
->key_slave
->key
:
1125 lacp
->key_slave
->port_id
);
1127 /* Construct my admin-state. Assume aggregation is configured on. */
1128 stats
->dot3adAggPortActorAdminState
= LACP_STATE_AGG
;
1130 stats
->dot3adAggPortActorAdminState
|= LACP_STATE_ACT
;
1133 stats
->dot3adAggPortActorAdminState
|= LACP_STATE_TIME
;
1135 /* XXX Not sure how to know the partner admin state. It
1136 * might have to be captured and remembered during the
1137 * negotiation phase.
1139 stats
->dot3adAggPortPartnerAdminState
= 0;
1141 stats
->dot3adAggPortActorOperState
= actor
.state
;
1142 stats
->dot3adAggPortPartnerOperState
= slave
->partner
.state
;
1144 /* Read out the latest counters */
1145 stats
->dot3adAggPortStatsLACPDUsRx
= slave
->count_rx_pdus
;
1146 stats
->dot3adAggPortStatsIllegalRx
= slave
->count_rx_pdus_bad
;
1147 stats
->dot3adAggPortStatsLACPDUsTx
= slave
->count_tx_pdus
;
1151 ovs_mutex_unlock(&mutex
);