blob: 615f03b20500902a021f08085c4872e7b304cfa8 [file] [log] [blame]
Steve Shin99a0e602017-07-01 04:16:20 +00001/*
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 <vnet/lldp/lldp.h>
26
Jakub Grajciar1c684f92020-01-30 14:01:17 +010027#include <vnet/ip/ip4_packet.h>
28#include <vnet/ip/ip6_packet.h>
29#include <vnet/ip/ip_types_api.h>
30
Steve Shin99a0e602017-07-01 04:16:20 +000031#include <vnet/vnet_msg_enum.h>
32
33#define vl_typedefs /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_typedefs
36
37#define vl_endianfun /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_endianfun
40
41/* instantiate all the print functions we know about */
42#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43#define vl_printfun
44#include <vnet/vnet_all_api_h.h>
45#undef vl_printfun
46
47#include <vlibapi/api_helper_macros.h>
48
49#define foreach_vpe_api_msg \
50_(LLDP_CONFIG, lldp_config) \
51_(SW_INTERFACE_SET_LLDP, sw_interface_set_lldp)
52
53static void
54vl_api_lldp_config_t_handler (vl_api_lldp_config_t * mp)
55{
56 vl_api_lldp_config_reply_t *rmp;
57 int rv = 0;
58 u8 *sys_name = 0;
59
Jakub Grajciar1c684f92020-01-30 14:01:17 +010060 sys_name = vl_api_from_api_to_new_vec (&mp->system_name);
Steve Shin99a0e602017-07-01 04:16:20 +000061
Steve Shin9a6fcef2017-10-11 13:55:16 -070062 if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval))
63 != lldp_ok)
Steve Shin99a0e602017-07-01 04:16:20 +000064 {
65 vec_free (sys_name);
66 rv = VNET_API_ERROR_INVALID_VALUE;
67 }
68
69 REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY);
70}
71
72static void
73vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t * mp)
74{
75 vl_api_sw_interface_set_lldp_reply_t *rmp;
76 int rv = 0;
Jakub Grajciar1c684f92020-01-30 14:01:17 +010077 u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0;
78 char *port_desc = 0;
79 u8 no_data[128];
80 ip4_address_t ip4;
81 ip6_address_t ip6;
Steve Shin99a0e602017-07-01 04:16:20 +000082
Jakub Grajciar1c684f92020-01-30 14:01:17 +010083 if (vl_api_string_len (&mp->port_desc) > 0)
Steve Shin9a6fcef2017-10-11 13:55:16 -070084 {
Jakub Grajciar1c684f92020-01-30 14:01:17 +010085 port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc);
Steve Shin9a6fcef2017-10-11 13:55:16 -070086 }
87
Jakub Grajciar1c684f92020-01-30 14:01:17 +010088 ip4_address_decode (mp->mgmt_ip4, &ip4);
89
90 if (ip4.as_u32 != 0)
Steve Shin9a6fcef2017-10-11 13:55:16 -070091 {
Jakub Grajciar1c684f92020-01-30 14:01:17 +010092 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
93 clib_memcpy (mgmt_ip4, &ip4, vec_len (mgmt_ip4));
Steve Shin9a6fcef2017-10-11 13:55:16 -070094 }
95
Jakub Grajciar1c684f92020-01-30 14:01:17 +010096 ip6_address_decode (mp->mgmt_ip6, &ip6);
97
98 if (!ip6_address_is_zero (&ip6))
Steve Shin9a6fcef2017-10-11 13:55:16 -070099 {
Jakub Grajciar1c684f92020-01-30 14:01:17 +0100100 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
101 clib_memcpy (mgmt_ip6, &ip6, vec_len (mgmt_ip6));
Steve Shin9a6fcef2017-10-11 13:55:16 -0700102 }
103
104 if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0)
105 {
106 vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1);
107 strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid));
108 }
Steve Shin99a0e602017-07-01 04:16:20 +0000109
110 VALIDATE_SW_IF_INDEX (mp);
111
Jakub Grajciar1c684f92020-01-30 14:01:17 +0100112 if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), (u8 **) & port_desc,
Steve Shin9a6fcef2017-10-11 13:55:16 -0700113 &mgmt_ip4, &mgmt_ip6, &mgmt_oid,
Steve Shin99a0e602017-07-01 04:16:20 +0000114 mp->enable) != lldp_ok)
115 {
116 vec_free (port_desc);
Steve Shin9a6fcef2017-10-11 13:55:16 -0700117 vec_free (mgmt_ip4);
118 vec_free (mgmt_ip6);
119 vec_free (mgmt_oid);
Steve Shin99a0e602017-07-01 04:16:20 +0000120 rv = VNET_API_ERROR_INVALID_VALUE;
121 }
122
123 BAD_SW_IF_INDEX_LABEL;
124
125 REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY);
126}
127
128
129/*
130 * * lldp_api_hookup
131 * * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700132 * * vlib has already mapped shared memory and
Steve Shin99a0e602017-07-01 04:16:20 +0000133 * * added the client registration handlers.
134 * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
135 * */
136#define vl_msg_name_crc_list
137#include <vnet/vnet_all_api_h.h>
138#undef vl_msg_name_crc_list
139
140static void
141setup_message_id_table (api_main_t * am)
142{
143#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
144 foreach_vl_msg_name_crc_lldp;
145#undef _
146}
147
148static clib_error_t *
149lldp_api_hookup (vlib_main_t * vm)
150{
Dave Barach39d69112019-11-27 11:42:13 -0500151 api_main_t *am = vlibapi_get_main ();
Steve Shin99a0e602017-07-01 04:16:20 +0000152
153#define _(N,n) \
154 vl_msg_api_set_handlers(VL_API_##N, #n, \
155 vl_api_##n##_t_handler, \
156 vl_noop_handler, \
157 vl_api_##n##_t_endian, \
158 vl_api_##n##_t_print, \
159 sizeof(vl_api_##n##_t), 1);
160 foreach_vpe_api_msg;
161#undef _
162
163 /*
164 * * Set up the (msg_name, crc, message-id) table
165 * */
166 setup_message_id_table (am);
167
168 return 0;
169}
170
171VLIB_API_INIT_FUNCTION (lldp_api_hookup);
172
173/*
174 * fd.io coding-style-patch-verification: ON
175 *
176 * Local Variables:
177 * eval: (c-set-style "gnu")
178 * End:
179 */