2 * Copyright (c) 2015 Nicira, Inc.
3 * Copyright (c) 2014 WindRiver, Inc.
4 * Copyright (c) 2015 Avaya, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 /* Implementation of Auto Attach.
20 * Based on sample implementation in 802.1ab. Above copyright and license
21 * applies to all modifications.
23 * - No support for multiple bridge.
24 * - Auto Attach state machine not implemented.
25 * - Auto Attach and LLDP code are bundled together. The plan is to decoupled
31 #include <arpa/inet.h>
33 #include <netinet/in.h>
36 #include <sys/types.h>
37 #include "dynamic-string.h"
40 #include "lldp/lldpd.h"
41 #include "lldp/lldpd-structs.h"
43 #include "openvswitch/types.h"
45 #include "poll-loop.h"
49 #include "openvswitch/vlog.h"
51 VLOG_DEFINE_THIS_MODULE(ovs_lldp
);
53 #define LLDP_PROTOCOL_ID 0x0000
54 #define LLDP_PROTOCOL_VERSION 0x00
55 #define LLDP_TYPE_CONFIG 0x00
56 #define LLDP_CHASSIS_TTL 120
57 #define ETH_TYPE_LLDP 0x88cc
58 #define MINIMUM_ETH_PACKET_SIZE 68
60 #define AA_STATUS_MULTIPLE \
61 AA_STATUS(ACTIVE,2,Active) \
62 AA_STATUS(REJECT_GENERIC,3,Reject (Generic)) \
63 AA_STATUS(REJECT_AA_RES_NOTAVAIL,4,Reject (AA resources unavailable)) \
64 AA_STATUS(REJECT_INVALID,6,Reject (Invalid)) \
65 AA_STATUS(REJECT_VLAN_RES_UNAVAIL,8,Reject (VLAN resources unavailable)) \
66 AA_STATUS(REJECT_VLAN_APP_ISSUE,9,Reject (Application interaction issue)) \
67 AA_STATUS(PENDING,255,Pending)
70 #define AA_STATUS(NAME, VALUE, STR) AA_STATUS_##NAME = VALUE,
76 /* Internal structure for an Auto Attach mapping.
78 struct aa_mapping_internal
{
79 struct hmap_node hmap_node_isid
;
80 struct hmap_node hmap_node_aux
;
84 enum aa_status status
;
87 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
89 /* Hash map of all LLDP instances keyed by name (port at the moment).
91 static struct hmap all_lldps__
= HMAP_INITIALIZER(&all_lldps__
);
92 static struct hmap
*const all_lldps
OVS_GUARDED_BY(mutex
) = &all_lldps__
;
94 /* Hash map of all the Auto Attach mappings. Global at the moment (but will
95 * be per bridge). Used when adding a new port to a bridge so that we can
96 * properly install all the configured mapping on the port and export them
97 * To the Auto Attach server via LLDP.
99 static struct hmap all_mappings__
= HMAP_INITIALIZER(&all_mappings__
);
100 static struct hmap
*const all_mappings
OVS_GUARDED_BY(mutex
) = &all_mappings__
;
102 static struct lldp_aa_element_system_id system_id_null
;
104 /* Convert an LLDP chassis ID to a string.
107 chassisid_to_string(uint8_t *array
, size_t len
, char **str
)
111 *str
= xmalloc(len
* 3);
113 for (i
= 0; i
< len
; i
++) {
114 snprintf(&(*str
)[i
* 3], 4, "%02x:", array
[i
]);
116 (*str
)[(i
* 3) - 1] = '\0';
119 /* Find an Auto Attach mapping keyed by I-SID.
121 static struct aa_mapping_internal
*
122 mapping_find_by_isid(struct lldp
*lldp
, const uint64_t isid
)
125 struct aa_mapping_internal
*m
;
127 HMAP_FOR_EACH_IN_BUCKET (m
,
129 hash_bytes(&isid
, sizeof isid
, 0),
130 &lldp
->mappings_by_isid
) {
131 if (isid
== m
->isid
) {
139 /* Find an Auto Attach mapping keyed by aux. aux is an opaque pointer created
140 * by the bridge that refers to an OVSDB mapping record.
142 static struct aa_mapping_internal
*
143 mapping_find_by_aux(struct lldp
*lldp
, const void *aux
) OVS_REQUIRES(mutex
)
145 struct aa_mapping_internal
*m
;
147 HMAP_FOR_EACH_IN_BUCKET (m
, hmap_node_aux
, hash_pointer(aux
, 0),
148 &lldp
->mappings_by_aux
) {
157 /* Convert an Auto Attach request status to a string.
160 aa_status_to_str(uint8_t status
)
163 #define AA_STATUS(NAME, VALUE, STR) case AA_STATUS_##NAME: return #STR;
166 default: return "Undefined";
170 /* Display LLDP and Auto Attach statistics.
173 aa_print_lldp_and_aa_stats(struct ds
*ds
, struct lldp
*lldp
)
176 struct lldpd_hardware
*hw
;
178 ds_put_format(ds
, "Statistics: %s\n", lldp
->name
);
184 LIST_FOR_EACH (hw
, h_entries
, &lldp
->lldpd
->g_hardware
.h_entries
) {
185 ds_put_format(ds
, "\ttx cnt: %"PRIu64
"\n", hw
->h_tx_cnt
);
186 ds_put_format(ds
, "\trx cnt: %"PRIu64
"\n", hw
->h_rx_cnt
);
187 ds_put_format(ds
, "\trx discarded cnt: %"PRIu64
"\n",
188 hw
->h_rx_discarded_cnt
);
189 ds_put_format(ds
, "\trx unrecognized cnt: %"PRIu64
"\n",
190 hw
->h_rx_unrecognized_cnt
);
191 ds_put_format(ds
, "\tageout cnt: %"PRIu64
"\n", hw
->h_ageout_cnt
);
192 ds_put_format(ds
, "\tinsert cnt: %"PRIu64
"\n", hw
->h_insert_cnt
);
193 ds_put_format(ds
, "\tdelete cnt: %"PRIu64
"\n", hw
->h_delete_cnt
);
194 ds_put_format(ds
, "\tdrop cnt: %"PRIu64
"\n", hw
->h_drop_cnt
);
199 aa_print_element_status_port(struct ds
*ds
, struct lldpd_hardware
*hw
)
201 struct lldpd_port
*port
;
203 LIST_FOR_EACH (port
, p_entries
, &hw
->h_rports
.p_entries
) {
204 if (memcmp(&port
->p_element
.system_id
,
206 sizeof port
->p_element
.system_id
)) {
207 static char *none_str
= "<None>";
208 char *id
= none_str
, *descr
= none_str
, *system
= none_str
;
210 if (port
->p_chassis
) {
211 if (port
->p_chassis
->c_id_len
> 0) {
212 chassisid_to_string((uint8_t *) port
->p_chassis
->c_id
,
213 port
->p_chassis
->c_id_len
, &id
);
216 descr
= port
->p_chassis
->c_descr
217 ? port
->p_chassis
->c_descr
: none_str
;
220 chassisid_to_string((uint8_t *) &port
->p_element
.system_id
,
221 sizeof port
->p_element
.system_id
, &system
);
224 "\tAuto Attach Primary Server Id: %s\n",
227 "\tAuto Attach Primary Server Descr: %s\n",
230 "\tAuto Attach Primary Server System Id: %s\n",
239 /* Auto Attach server broadcast an LLDP message periodically. Display
240 * the discovered server.
243 aa_print_element_status(struct ds
*ds
, struct lldp
*lldp
) OVS_REQUIRES(mutex
)
245 struct lldpd_hardware
*hw
;
247 ds_put_format(ds
, "LLDP: %s\n", lldp
->name
);
253 LIST_FOR_EACH (hw
, h_entries
, &lldp
->lldpd
->g_hardware
.h_entries
) {
254 aa_print_element_status_port(ds
, hw
);
259 aa_print_isid_status_port_isid(struct lldp
*lldp
, struct lldpd_port
*port
)
262 struct lldpd_aa_isid_vlan_maps_tlv
*mapping
;
264 if (list_is_empty(&port
->p_isid_vlan_maps
)) {
268 LIST_FOR_EACH (mapping
, m_entries
, &port
->p_isid_vlan_maps
) {
269 uint32_t isid
= mapping
->isid_vlan_data
.isid
;
270 struct aa_mapping_internal
*m
= mapping_find_by_isid(lldp
, isid
);
272 VLOG_INFO("h_rport: isid=%u, vlan=%u, status=%d",
274 mapping
->isid_vlan_data
.vlan
,
275 mapping
->isid_vlan_data
.status
);
277 /* Update the status of our internal state for the mapping. */
279 VLOG_INFO("Setting status for ISID=%"PRIu32
" to %"PRIu16
,
280 isid
, mapping
->isid_vlan_data
.status
);
281 m
->status
= mapping
->isid_vlan_data
.status
;
283 VLOG_WARN("Couldn't find mapping for I-SID=%"PRIu32
, isid
);
289 aa_print_isid_status_port(struct lldp
*lldp
, struct lldpd_hardware
*hw
)
292 struct lldpd_port
*port
;
294 LIST_FOR_EACH (port
, p_entries
, &hw
->h_rports
.p_entries
) {
295 aa_print_isid_status_port_isid(lldp
, port
);
299 /* The Auto Attach server will broadcast the status of the configured mappings
300 * via LLDP. Display the status.
303 aa_print_isid_status(struct ds
*ds
, struct lldp
*lldp
) OVS_REQUIRES(mutex
)
305 struct lldpd_hardware
*hw
;
306 struct aa_mapping_internal
*m
;
312 ds_put_format(ds
, "LLDP: %s\n", lldp
->name
);
314 LIST_FOR_EACH (hw
, h_entries
, &lldp
->lldpd
->g_hardware
.h_entries
) {
315 aa_print_isid_status_port(lldp
, hw
);
318 ds_put_format(ds
, "%-8s %-4s %-11s %-8s\n",
323 ds_put_format(ds
, "-------- ---- ----------- --------\n");
325 HMAP_FOR_EACH (m
, hmap_node_isid
, &lldp
->mappings_by_isid
) {
326 ds_put_format(ds
, "%-8ld %-4ld %-11s %-11s\n",
330 aa_status_to_str(m
->status
));
335 aa_unixctl_status(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
336 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
340 struct ds ds
= DS_EMPTY_INITIALIZER
;
342 ovs_mutex_lock(&mutex
);
344 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
345 aa_print_element_status(&ds
, lldp
);
347 unixctl_command_reply(conn
, ds_cstr(&ds
));
350 ovs_mutex_unlock(&mutex
);
354 aa_unixctl_show_isid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
355 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
359 struct ds ds
= DS_EMPTY_INITIALIZER
;
361 ovs_mutex_lock(&mutex
);
363 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
364 aa_print_isid_status(&ds
, lldp
);
366 unixctl_command_reply(conn
, ds_cstr(&ds
));
369 ovs_mutex_unlock(&mutex
);
373 aa_unixctl_statistics(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
374 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
377 struct ds ds
= DS_EMPTY_INITIALIZER
;
380 ovs_mutex_lock(&mutex
);
382 /* Cycle through all ports and dump the stats for each one */
383 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
384 aa_print_lldp_and_aa_stats(&ds
, lldp
);
387 ovs_mutex_unlock(&mutex
);
389 unixctl_command_reply(conn
, ds_cstr(&ds
));
392 /* An Auto Attach mapping was configured. Populate the corresponding
393 * structures in the LLDP hardware.
396 update_mapping_on_lldp(struct lldp
*lldp
, struct lldpd_hardware
*hardware
,
397 struct aa_mapping_internal
*m
)
399 struct lldpd_aa_isid_vlan_maps_tlv
*lm
= xzalloc(sizeof *lm
);
401 if (hardware
->h_ifname
) {
402 VLOG_INFO("\t\t hardware->h_ifname=%s", hardware
->h_ifname
);
405 lm
->isid_vlan_data
.isid
= m
->isid
;
406 lm
->isid_vlan_data
.vlan
= m
->vlan
;
408 list_push_back(&hardware
->h_lport
.p_isid_vlan_maps
, &lm
->m_entries
);
410 /* TODO Should be done in the Auto Attach state machine when a mapping goes
411 * from "pending" to "active".
414 struct bridge_aa_vlan
*node
= xmalloc(sizeof *node
);
416 node
->port_name
= xstrdup(hardware
->h_ifname
);
417 node
->vlan
= m
->vlan
;
418 node
->oper
= BRIDGE_AA_VLAN_OPER_ADD
;
420 list_push_back(&lldp
->active_mapping_queue
, &node
->list_node
);
424 /* Bridge will poll the list of VLAN that needs to be auto configure based on
425 * the Auto Attach mappings that have been exchanged with the server.
428 aa_get_vlan_queued(struct ovs_list
*list
)
432 ovs_mutex_lock(&mutex
);
434 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
435 struct bridge_aa_vlan
*node
, *node_next
;
437 LIST_FOR_EACH_SAFE (node
,
440 &lldp
->active_mapping_queue
) {
441 struct bridge_aa_vlan
*copy
;
443 copy
= xmalloc(sizeof *copy
);
444 copy
->port_name
= xstrdup(node
->port_name
);
445 copy
->vlan
= node
->vlan
;
446 copy
->oper
= node
->oper
;
448 list_push_back(list
, ©
->list_node
);
451 list_remove(&node
->list_node
);
452 free(node
->port_name
);
457 ovs_mutex_unlock(&mutex
);
462 /* Bridge will poll whether or not VLAN have been auto-configured.
465 aa_get_vlan_queue_size(void)
468 unsigned int size
= 0;
470 ovs_mutex_lock(&mutex
);
472 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
473 size
+= list_size(&lldp
->active_mapping_queue
);
476 ovs_mutex_unlock(&mutex
);
481 /* Configure Auto Attach.
484 aa_configure(const struct aa_settings
*s
)
488 ovs_mutex_lock(&mutex
);
490 /* TODO Change all instances for now */
491 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
492 struct lldpd_chassis
*chassis
;
494 LIST_FOR_EACH (chassis
, list
, &lldp
->lldpd
->g_chassis
.list
) {
495 /* System Description */
496 if (chassis
->c_descr
) {
497 free(chassis
->c_descr
);
499 chassis
->c_descr
= s
->system_description
[0] ?
500 xstrdup(s
->system_description
) : xstrdup(PACKAGE_STRING
);
503 if (chassis
->c_name
) {
504 free(chassis
->c_name
);
506 chassis
->c_name
= xstrdup(s
->system_name
);
510 ovs_mutex_unlock(&mutex
);
515 /* Add a new Auto Attach mapping.
518 aa_mapping_register(void *aux
, const struct aa_mapping_settings
*s
)
520 struct aa_mapping_internal
*bridge_m
;
523 VLOG_INFO("Adding mapping ISID=%ld, VLAN=%ld, aux=%p", (long int) s
->isid
,
524 (long int) s
->vlan
, aux
);
526 ovs_mutex_lock(&mutex
);
528 /* TODO These mappings should be stores per bridge. This is used
529 * When a port is added. Auto Attach mappings need to be added on this
532 bridge_m
= xzalloc(sizeof *bridge_m
);
533 bridge_m
->isid
= s
->isid
;
534 bridge_m
->vlan
= s
->vlan
;
536 bridge_m
->status
= AA_STATUS_PENDING
;
537 hmap_insert(all_mappings
,
538 &bridge_m
->hmap_node_isid
,
539 hash_bytes((const void *) &bridge_m
->isid
,
540 sizeof bridge_m
->isid
,
543 /* Update mapping on the all the LLDP instances. */
544 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
545 struct lldpd_hardware
*hw
;
546 struct aa_mapping_internal
*m
;
548 VLOG_INFO("\t lldp->name=%s", lldp
->name
);
550 if (mapping_find_by_isid(lldp
, s
->isid
)) {
554 m
= xzalloc(sizeof *m
);
557 m
->status
= AA_STATUS_PENDING
;
559 hmap_insert(&lldp
->mappings_by_isid
,
561 hash_bytes((const void *) &m
->isid
,
564 hmap_insert(&lldp
->mappings_by_aux
,
566 hash_pointer(m
->aux
, 0));
568 /* Configure the mapping on each port of the LLDP stack. */
569 LIST_FOR_EACH (hw
, h_entries
, &lldp
->lldpd
->g_hardware
.h_entries
) {
570 update_mapping_on_lldp(lldp
, hw
, m
);
574 ovs_mutex_unlock(&mutex
);
580 aa_mapping_unregister_mapping(struct lldp
*lldp
,
581 struct lldpd_hardware
*hw
,
582 struct aa_mapping_internal
*m
)
584 struct lldpd_aa_isid_vlan_maps_tlv
*lm
, *lm_next
;
586 LIST_FOR_EACH_SAFE (lm
, lm_next
, m_entries
,
587 &hw
->h_lport
.p_isid_vlan_maps
) {
588 uint32_t isid
= lm
->isid_vlan_data
.isid
;
590 if (isid
== (uint32_t) m
->isid
) {
591 VLOG_INFO("\t\t Removing lport, isid=%u, vlan=%u",
593 lm
->isid_vlan_data
.vlan
);
595 list_remove(&lm
->m_entries
);
597 /* TODO Should be done in the AA SM when a mapping goes
598 * from "pending" to "active".
601 struct bridge_aa_vlan
*node
= xmalloc(sizeof *node
);
603 node
->port_name
= xstrdup(hw
->h_ifname
);
604 node
->vlan
= (uint32_t) m
->vlan
;
605 node
->oper
= BRIDGE_AA_VLAN_OPER_REMOVE
;
607 list_push_back(&lldp
->active_mapping_queue
, &node
->list_node
);
615 /* Remove an existing Auto Attach mapping.
618 aa_mapping_unregister(void *aux
)
622 VLOG_INFO("Removing mapping aux=%p", aux
);
624 ovs_mutex_lock(&mutex
);
626 HMAP_FOR_EACH (lldp
, hmap_node
, all_lldps
) {
627 struct lldpd_hardware
*hw
;
628 struct aa_mapping_internal
*m
= mapping_find_by_aux(lldp
, aux
);
629 int64_t isid_tmp
= -1, vlan_tmp
= -1;
631 /* Remove from internal hash tables. */
633 struct aa_mapping_internal
*p
=
634 mapping_find_by_isid(lldp
, m
->isid
);
638 VLOG_INFO("\t Removing mapping ISID=%ld, VLAN=%ld (lldp->name=%s)",
639 (long int) m
->isid
, (long int) m
->vlan
, lldp
->name
);
642 hmap_remove(&lldp
->mappings_by_isid
, &p
->hmap_node_isid
);
645 hmap_remove(&lldp
->mappings_by_aux
, &m
->hmap_node_aux
);
648 /* Remove from all the lldp instances */
649 LIST_FOR_EACH (hw
, h_entries
, &lldp
->lldpd
->g_hardware
.h_entries
) {
651 VLOG_INFO("\t\t hardware->h_ifname=%s", hw
->h_ifname
);
654 aa_mapping_unregister_mapping(lldp
, hw
, m
);
657 if (isid_tmp
>= 0 && vlan_tmp
>= 0) {
658 /* Remove from the all_mappings */
659 HMAP_FOR_EACH (m
, hmap_node_isid
, all_mappings
) {
660 if (m
&& isid_tmp
== m
->isid
&& vlan_tmp
== m
->vlan
) {
661 hmap_remove(all_mappings
, &m
->hmap_node_isid
);
669 ovs_mutex_unlock(&mutex
);
677 unixctl_command_register("autoattach/status", "[bridge]", 0, 1,
678 aa_unixctl_status
, NULL
);
679 unixctl_command_register("autoattach/show-isid", "[bridge]", 0, 1,
680 aa_unixctl_show_isid
, NULL
);
681 unixctl_command_register("autoattach/statistics", "[bridge]", 0, 1,
682 aa_unixctl_statistics
, NULL
);
685 /* Returns true if 'lldp' should process packets from 'flow'. Sets
686 * fields in 'wc' that were used to make the determination.
689 lldp_should_process_flow(const struct flow
*flow
)
691 return (flow
->dl_type
== htons(ETH_TYPE_LLDP
));
695 /* Process an LLDP packet that was received on a bridge port.
698 lldp_process_packet(struct lldp
*lldp
, const struct dp_packet
*p
)
701 lldpd_recv(lldp
->lldpd
,
702 (struct lldpd_hardware
*)
703 lldp
->lldpd
->g_hardware
.h_entries
.next
,
709 /* This code is called periodically to check if the LLDP module has an LLDP
710 * message it wishes to send. It is called several times every second.
713 lldp_should_send_packet(struct lldp
*cfg
) OVS_EXCLUDED(mutex
)
717 ovs_mutex_lock(&mutex
);
718 ret
= timer_expired(&cfg
->tx_timer
);
719 ovs_mutex_unlock(&mutex
);
724 /* Returns the next wake up time.
727 lldp_wake_time(const struct lldp
*lldp
) OVS_EXCLUDED(mutex
)
729 long long int retval
;
735 ovs_mutex_lock(&mutex
);
736 retval
= lldp
->tx_timer
.t
;
737 ovs_mutex_unlock(&mutex
);
742 /* Put the monitor thread to sleep until it's next wake time.
745 lldp_wait(struct lldp
*lldp
) OVS_EXCLUDED(mutex
)
747 long long int wake_time
= lldp_wake_time(lldp
);
748 poll_timer_wait_until(wake_time
);
752 /* Prepare the LLDP packet to be sent on a bridge port.
755 lldp_put_packet(struct lldp
*lldp
, struct dp_packet
*packet
,
756 uint8_t eth_src
[ETH_ADDR_LEN
]) OVS_EXCLUDED(mutex
)
758 struct lldpd
*mylldpd
= lldp
->lldpd
;
759 struct lldpd_hardware
*hw
= (struct lldpd_hardware
*)
760 mylldpd
->g_hardware
.h_entries
.next
;
761 uint32_t lldp_size
= 0;
762 static const uint8_t eth_addr_lldp
[6] =
763 {0x01, 0x80, 0xC2, 0x00, 0x00, 0x0e};
765 ovs_mutex_lock(&mutex
);
767 eth_compose(packet
, eth_addr_lldp
, eth_src
, ETH_TYPE_LLDP
, 0);
769 lldp_size
= lldpd_send(hw
, packet
);
770 if (lldp_size
+ ETH_HEADER_LEN
< MINIMUM_ETH_PACKET_SIZE
) {
771 lldp_size
= MINIMUM_ETH_PACKET_SIZE
;
774 timer_set_duration(&lldp
->tx_timer
, lldp
->lldpd
->g_config
.c_tx_interval
);
775 ovs_mutex_unlock(&mutex
);
778 /* Configures the LLDP stack.
781 lldp_configure(struct lldp
*lldp
) OVS_EXCLUDED(mutex
)
784 ovs_mutex_lock(&mutex
);
785 timer_set_expired(&lldp
->tx_timer
);
786 timer_set_duration(&lldp
->tx_timer
, LLDP_DEFAULT_TRANSMIT_INTERVAL_MS
);
787 lldp
->lldpd
->g_config
.c_tx_interval
=
788 LLDP_DEFAULT_TRANSMIT_INTERVAL_MS
;
789 ovs_mutex_unlock(&mutex
);
795 /* Create an LLDP stack instance. At the moment there is one per bridge port.
798 lldp_create(const struct netdev
*netdev
,
800 const struct smap
*cfg
) OVS_EXCLUDED(mutex
)
803 struct lldpd_chassis
*lchassis
;
804 struct lldpd_hardware
*hw
;
805 struct aa_mapping_internal
*m
;
807 if (!cfg
|| !smap_get_bool(cfg
, "enable", false)) {
811 lldp
= xzalloc(sizeof *lldp
);
812 lldp
->name
= xstrdup(netdev_get_name(netdev
));
813 lldp
->lldpd
= xzalloc(sizeof *lldp
->lldpd
);
815 hmap_init(&lldp
->mappings_by_isid
);
816 hmap_init(&lldp
->mappings_by_aux
);
817 list_init(&lldp
->active_mapping_queue
);
819 lchassis
= xzalloc(sizeof *lchassis
);
820 lchassis
->c_cap_available
= LLDP_CAP_BRIDGE
;
821 lchassis
->c_cap_enabled
= LLDP_CAP_BRIDGE
;
822 lchassis
->c_id_subtype
= LLDP_CHASSISID_SUBTYPE_LLADDR
;
823 lchassis
->c_id_len
= ETH_ADDR_LEN
;
824 lchassis
->c_id
= xmalloc(ETH_ADDR_LEN
);
825 netdev_get_etheraddr(netdev
, (uint8_t *) lchassis
->c_id
);
827 list_init(&lchassis
->c_mgmt
.m_entries
);
828 lchassis
->c_ttl
= lldp
->lldpd
->g_config
.c_tx_interval
*
829 lldp
->lldpd
->g_config
.c_tx_hold
;
830 lchassis
->c_ttl
= LLDP_CHASSIS_TTL
;
831 lldpd_assign_cfg_to_protocols(lldp
->lldpd
);
832 list_init(&lldp
->lldpd
->g_chassis
.list
);
833 list_push_back(&lldp
->lldpd
->g_chassis
.list
, &lchassis
->list
);
835 if ((hw
= lldpd_alloc_hardware(lldp
->lldpd
,
836 (char *) netdev_get_name(netdev
),
838 VLOG_WARN("Unable to allocate space for %s",
839 (char *) netdev_get_name(netdev
));
843 ovs_refcount_init(&lldp
->ref_cnt
);
845 hw
->h_flags
|= IFF_RUNNING
;
848 hw
->h_lport
.p_id_subtype
= LLDP_PORTID_SUBTYPE_IFNAME
;
849 hw
->h_lport
.p_id
= xstrdup(netdev_get_name(netdev
));
851 /* p_id is not necessarily a null terminated string. */
852 hw
->h_lport
.p_id_len
= strlen(netdev_get_name(netdev
));
854 /* Auto Attach element tlv */
855 hw
->h_lport
.p_element
.type
= LLDP_TLV_AA_ELEM_TYPE_TAG_CLIENT
;
856 hw
->h_lport
.p_element
.mgmt_vlan
= 0;
857 memcpy(&hw
->h_lport
.p_element
.system_id
.system_mac
,
858 lchassis
->c_id
, lchassis
->c_id_len
);
859 hw
->h_lport
.p_element
.system_id
.conn_type
=
860 LLDP_TLV_AA_ELEM_CONN_TYPE_SINGLE
;
862 hw
->h_lport
.p_element
.system_id
.smlt_id
= 0;
863 hw
->h_lport
.p_element
.system_id
.mlt_id
[0] = 0;
864 hw
->h_lport
.p_element
.system_id
.mlt_id
[1] = 0;
866 list_init(&hw
->h_lport
.p_isid_vlan_maps
);
867 list_init(&lldp
->lldpd
->g_hardware
.h_entries
);
868 list_push_back(&lldp
->lldpd
->g_hardware
.h_entries
, &hw
->h_entries
);
870 ovs_mutex_lock(&mutex
);
872 /* Update port with Auto Attach mappings configured. */
873 HMAP_FOR_EACH (m
, hmap_node_isid
, all_mappings
) {
874 struct aa_mapping_internal
*p
;
876 if (mapping_find_by_isid(lldp
, m
->isid
)) {
880 p
= xmemdup(m
, sizeof *p
);
881 hmap_insert(&lldp
->mappings_by_isid
,
883 hash_bytes((const void *) &p
->isid
,
886 hmap_insert(&lldp
->mappings_by_aux
,
888 hash_pointer(p
->aux
, 0));
890 update_mapping_on_lldp(lldp
, hw
, p
);
893 hmap_insert(all_lldps
, &lldp
->hmap_node
,
894 hash_string(netdev_get_name(netdev
), 0));
896 ovs_mutex_unlock(&mutex
);
903 lldp_create_dummy(void)
906 struct lldpd_chassis
*lchassis
;
907 struct lldpd_hardware
*hw
;
909 lldp
= xzalloc(sizeof *lldp
);
910 lldp
->name
= "dummy-lldp";
911 lldp
->lldpd
= xzalloc(sizeof *lldp
->lldpd
);
913 hmap_init(&lldp
->mappings_by_isid
);
914 hmap_init(&lldp
->mappings_by_aux
);
915 list_init(&lldp
->active_mapping_queue
);
917 lchassis
= xzalloc(sizeof *lchassis
);
918 lchassis
->c_cap_available
= LLDP_CAP_BRIDGE
;
919 lchassis
->c_cap_enabled
= LLDP_CAP_BRIDGE
;
920 lchassis
->c_id_subtype
= LLDP_CHASSISID_SUBTYPE_LLADDR
;
921 lchassis
->c_id_len
= ETH_ADDR_LEN
;
922 lchassis
->c_id
= xmalloc(ETH_ADDR_LEN
);
924 list_init(&lchassis
->c_mgmt
.m_entries
);
925 lchassis
->c_ttl
= LLDP_CHASSIS_TTL
;
926 lldpd_assign_cfg_to_protocols(lldp
->lldpd
);
927 list_init(&lldp
->lldpd
->g_chassis
.list
);
928 list_push_back(&lldp
->lldpd
->g_chassis
.list
, &lchassis
->list
);
930 hw
= lldpd_alloc_hardware(lldp
->lldpd
, "dummy-hw", 0);
932 ovs_refcount_init(&lldp
->ref_cnt
);
934 hw
->h_flags
|= IFF_RUNNING
;
937 hw
->h_lport
.p_id_subtype
= LLDP_PORTID_SUBTYPE_IFNAME
;
938 hw
->h_lport
.p_id
= "dummy-port";
940 /* p_id is not necessarily a null terminated string. */
941 hw
->h_lport
.p_id_len
= strlen(hw
->h_lport
.p_id
);
943 /* Auto Attach element tlv */
944 hw
->h_lport
.p_element
.type
= LLDP_TLV_AA_ELEM_TYPE_TAG_CLIENT
;
945 hw
->h_lport
.p_element
.mgmt_vlan
= 0;
946 memcpy(&hw
->h_lport
.p_element
.system_id
.system_mac
,
947 lchassis
->c_id
, lchassis
->c_id_len
);
948 hw
->h_lport
.p_element
.system_id
.conn_type
=
949 LLDP_TLV_AA_ELEM_CONN_TYPE_SINGLE
;
950 hw
->h_lport
.p_element
.system_id
.smlt_id
= 0;
951 hw
->h_lport
.p_element
.system_id
.mlt_id
[0] = 0;
952 hw
->h_lport
.p_element
.system_id
.mlt_id
[1] = 0;
954 list_init(&hw
->h_lport
.p_isid_vlan_maps
);
955 list_init(&lldp
->lldpd
->g_hardware
.h_entries
);
956 list_push_back(&lldp
->lldpd
->g_hardware
.h_entries
, &hw
->h_entries
);
961 /* Unreference a specific LLDP instance.
964 lldp_unref(struct lldp
*lldp
)
970 ovs_mutex_lock(&mutex
);
971 if (ovs_refcount_unref_relaxed(&lldp
->ref_cnt
) != 1) {
972 ovs_mutex_unlock(&mutex
);
976 hmap_remove(all_lldps
, &lldp
->hmap_node
);
977 ovs_mutex_unlock(&mutex
);
979 lldpd_cleanup(lldp
->lldpd
);
985 /* Unreference a specific LLDP instance.
988 lldp_ref(const struct lldp
*lldp_
)
990 struct lldp
*lldp
= CONST_CAST(struct lldp
*, lldp_
);
992 ovs_refcount_ref(&lldp
->ref_cnt
);