2 *------------------------------------------------------------------
3 * lldp_api.c - lldp api
5 * Copyright (c) 2017 Cisco and/or its affiliates.
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.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <lldp/lldp.h>
26 #include <lldp/lldp_node.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/ip/ip_types_api.h>
32 /* define message IDs */
33 #include <vnet/format_fns.h>
34 #include <lldp/lldp.api_enum.h>
35 #include <lldp/lldp.api_types.h>
38 * Base message ID fot the plugin
40 static u32 lldp_base_msg_id;
41 #define REPLY_MSG_ID_BASE lldp_base_msg_id
43 #include <vlibapi/api_helper_macros.h>
46 vl_api_lldp_config_t_handler (vl_api_lldp_config_t *mp)
48 vl_api_lldp_config_reply_t *rmp;
52 sys_name = vl_api_from_api_to_new_vec (mp, &mp->system_name);
54 if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval)) !=
58 rv = VNET_API_ERROR_INVALID_VALUE;
61 REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY);
65 vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t *mp)
67 vl_api_sw_interface_set_lldp_reply_t *rmp;
69 u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0;
71 u8 no_data[128] = { 0 };
75 if (vl_api_string_len (&mp->port_desc) > 0)
77 port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc);
80 ip4_address_decode (mp->mgmt_ip4, &ip4);
84 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
85 clib_memcpy (mgmt_ip4, &ip4, sizeof (ip4));
88 ip6_address_decode (mp->mgmt_ip6, &ip6);
90 if (!ip6_address_is_zero (&ip6))
92 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
93 clib_memcpy (mgmt_ip6, &ip6, sizeof (ip6));
96 if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0)
98 vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1);
99 strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid));
102 VALIDATE_SW_IF_INDEX (mp);
104 if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), (u8 **) &port_desc,
105 &mgmt_ip4, &mgmt_ip6, &mgmt_oid,
106 mp->enable) != lldp_ok)
108 vec_free (port_desc);
112 rv = VNET_API_ERROR_INVALID_VALUE;
115 BAD_SW_IF_INDEX_LABEL;
117 REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY);
121 send_lldp (u32 index, vl_api_registration_t *rp, u32 context)
123 vl_api_lldp_details_t *rmp = 0;
124 vnet_main_t *vnm = &vnet_main;
125 lldp_main_t *lm = &lldp_main;
126 const lldp_intf_t *n = vec_elt_at_index (lm->intfs, index);
127 const vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, n->hw_if_index);
129 REPLY_MACRO_DETAILS4_END (
130 VL_API_LLDP_DETAILS, rp, context, ({
131 rmp->sw_if_index = hw->sw_if_index;
132 rmp->last_heard = n->last_heard;
133 rmp->last_sent = n->last_sent;
135 rmp->port_id_subtype = (vl_api_port_id_subtype_t) n->port_id_subtype;
136 rmp->chassis_id_subtype =
137 (vl_api_chassis_id_subtype_t) n->chassis_id_subtype;
138 rmp->chassis_id_len = vec_len (n->chassis_id);
139 clib_memcpy (&rmp->chassis_id, n->chassis_id, rmp->chassis_id_len);
140 rmp->port_id_len = vec_len (n->port_id);
141 clib_memcpy (&rmp->port_id, n->port_id, rmp->port_id_len);
146 vl_api_lldp_dump_t_handler (vl_api_lldp_dump_t *mp)
149 lldp_main_t *lm = &lldp_main;
150 vl_api_lldp_dump_reply_t *rmp;
152 REPLY_AND_DETAILS_MACRO_END (VL_API_LLDP_DUMP_REPLY, lm->intfs,
153 ({ send_lldp (cursor, rp, mp->context); }));
158 * * Add vpe's API message handlers to the table.
159 * * vlib has already mapped shared memory and
160 * * added the client registration handlers.
161 * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
163 #include <lldp/lldp.api.c>
165 static clib_error_t *
166 lldp_api_hookup (vlib_main_t *vm)
169 * Set up the (msg_name, crc, message-id) table
171 lldp_base_msg_id = setup_message_id_table ();
176 VLIB_API_INIT_FUNCTION (lldp_api_hookup);
178 #include <vlib/unix/plugin.h>
179 #include <vpp/app/version.h>
182 VLIB_PLUGIN_REGISTER () = {
183 .version = VPP_BUILD_VER,
184 .description = "Link Layer Discovery Protocol (LLDP)",
189 * fd.io coding-style-patch-verification: ON
192 * eval: (c-set-style "gnu")