lldp: dump api
[vpp.git] / src / plugins / lldp / lldp_api.c
1 /*
2  *------------------------------------------------------------------
3  * lldp_api.c - lldp api
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <lldp/lldp.h>
26 #include <lldp/lldp_node.h>
27
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/ip/ip_types_api.h>
31
32 /* define message IDs */
33 #include <vnet/format_fns.h>
34 #include <lldp/lldp.api_enum.h>
35 #include <lldp/lldp.api_types.h>
36
37 /**
38  * Base message ID fot the plugin
39  */
40 static u32 lldp_base_msg_id;
41 #define REPLY_MSG_ID_BASE lldp_base_msg_id
42
43 #include <vlibapi/api_helper_macros.h>
44
45 static void
46 vl_api_lldp_config_t_handler (vl_api_lldp_config_t *mp)
47 {
48   vl_api_lldp_config_reply_t *rmp;
49   int rv = 0;
50   u8 *sys_name = 0;
51
52   sys_name = vl_api_from_api_to_new_vec (mp, &mp->system_name);
53
54   if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval)) !=
55       lldp_ok)
56     {
57       vec_free (sys_name);
58       rv = VNET_API_ERROR_INVALID_VALUE;
59     }
60
61   REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY);
62 }
63
64 static void
65 vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t *mp)
66 {
67   vl_api_sw_interface_set_lldp_reply_t *rmp;
68   int rv = 0;
69   u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0;
70   char *port_desc = 0;
71   u8 no_data[128] = { 0 };
72   ip4_address_t ip4;
73   ip6_address_t ip6;
74
75   if (vl_api_string_len (&mp->port_desc) > 0)
76     {
77       port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc);
78     }
79
80   ip4_address_decode (mp->mgmt_ip4, &ip4);
81
82   if (ip4.as_u32 != 0)
83     {
84       vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
85       clib_memcpy (mgmt_ip4, &ip4, sizeof (ip4));
86     }
87
88   ip6_address_decode (mp->mgmt_ip6, &ip6);
89
90   if (!ip6_address_is_zero (&ip6))
91     {
92       vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
93       clib_memcpy (mgmt_ip6, &ip6, sizeof (ip6));
94     }
95
96   if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0)
97     {
98       vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1);
99       strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid));
100     }
101
102   VALIDATE_SW_IF_INDEX (mp);
103
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)
107     {
108       vec_free (port_desc);
109       vec_free (mgmt_ip4);
110       vec_free (mgmt_ip6);
111       vec_free (mgmt_oid);
112       rv = VNET_API_ERROR_INVALID_VALUE;
113     }
114
115   BAD_SW_IF_INDEX_LABEL;
116
117   REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY);
118 }
119
120 static void
121 send_lldp (u32 index, vl_api_registration_t *rp, u32 context)
122 {
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);
128
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;
134       rmp->ttl = n->ttl;
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);
142     }));
143 }
144
145 static void
146 vl_api_lldp_dump_t_handler (vl_api_lldp_dump_t *mp)
147 {
148   int rv = 0;
149   lldp_main_t *lm = &lldp_main;
150   vl_api_lldp_dump_reply_t *rmp;
151
152   REPLY_AND_DETAILS_MACRO_END (VL_API_LLDP_DUMP_REPLY, lm->intfs,
153                                ({ send_lldp (cursor, rp, mp->context); }));
154 }
155
156 /*
157  *  * lldp_api_hookup
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()
162  *       */
163 #include <lldp/lldp.api.c>
164
165 static clib_error_t *
166 lldp_api_hookup (vlib_main_t *vm)
167 {
168   /*
169    * Set up the (msg_name, crc, message-id) table
170    */
171   lldp_base_msg_id = setup_message_id_table ();
172
173   return 0;
174 }
175
176 VLIB_API_INIT_FUNCTION (lldp_api_hookup);
177
178 #include <vlib/unix/plugin.h>
179 #include <vpp/app/version.h>
180
181 /* *INDENT-OFF* */
182 VLIB_PLUGIN_REGISTER () = {
183   .version = VPP_BUILD_VER,
184   .description = "Link Layer Discovery Protocol (LLDP)",
185 };
186 /* *INDENT-ON* */
187
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */