2 *------------------------------------------------------------------
3 * punt_api.c - Punt api
5 * Copyright (c) 2016 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>
22 #include <vnet/ip/punt.h>
23 #include <vnet/ip/ip_types_api.h>
25 #include <vnet/vnet_msg_enum.h>
27 #define vl_typedefs /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
31 #define vl_endianfun /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
38 #include <vnet/vnet_all_api_h.h>
41 #include <vlibapi/api_helper_macros.h>
43 #define foreach_punt_api_msg \
44 _(SET_PUNT, set_punt) \
45 _(PUNT_SOCKET_REGISTER, punt_socket_register) \
46 _(PUNT_SOCKET_DEREGISTER, punt_socket_deregister) \
47 _(PUNT_SOCKET_DUMP, punt_socket_dump) \
48 _(PUNT_REASON_DUMP, punt_reason_dump)
51 vl_api_punt_type_decode (vl_api_punt_type_t in, punt_type_t * out)
53 in = clib_net_to_host_u32 (in);
58 case PUNT_API_TYPE_##v: \
59 *out = PUNT_TYPE_##v; \
68 static vl_api_punt_type_t
69 vl_api_punt_type_encode (punt_type_t in)
71 vl_api_punt_type_t pt = PUNT_API_TYPE_L4;
77 pt = PUNT_API_TYPE_##v; \
83 return (clib_host_to_net_u32 (pt));
87 vl_api_punt_l4_decode (const vl_api_punt_l4_t * in, punt_l4_t * out)
91 rv = ip_address_family_decode (in->af, &out->af);
92 rv += ip_proto_decode (in->protocol, &out->protocol);
93 out->port = clib_net_to_host_u16 (in->port);
99 vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
100 punt_ip_proto_t * out)
104 rv = ip_address_family_decode (in->af, &out->af);
105 rv += ip_proto_decode (in->protocol, &out->protocol);
111 vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
112 punt_exception_t * out)
116 out->reason = clib_net_to_host_u32 (in->id);
117 rv = vlib_punt_reason_validate (out->reason);
123 vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
127 rv = vl_api_punt_type_decode (in->type, &out->type);
135 return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
136 case PUNT_TYPE_EXCEPTION:
137 return (vl_api_punt_exception_decode (&in->punt.exception,
138 &out->punt.exception));
139 case PUNT_TYPE_IP_PROTO:
140 return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
141 &out->punt.ip_proto));
148 vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
150 out->af = ip_address_family_encode (in->af);
151 out->protocol = ip_proto_encode (in->protocol);
152 out->port = clib_net_to_host_u16 (in->port);
156 vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
157 vl_api_punt_ip_proto_t * out)
159 out->af = ip_address_family_encode (in->af);
160 out->protocol = ip_proto_encode (in->protocol);
164 vl_api_punt_exception_encode (const punt_exception_t * in,
165 vl_api_punt_exception_t * out)
167 out->id = clib_host_to_net_u32 (in->reason);
171 vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
173 out->type = vl_api_punt_type_encode (in->type);
178 vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
180 case PUNT_TYPE_IP_PROTO:
181 vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
183 case PUNT_TYPE_EXCEPTION:
184 vl_api_punt_exception_encode (&in->punt.exception,
185 &out->punt.exception);
191 vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
193 vl_api_set_punt_reply_t *rmp;
194 vlib_main_t *vm = vlib_get_main ();
199 rv = vl_api_punt_decode (&mp->punt, &pr);
204 error = vnet_punt_add_del (vm, &pr, mp->is_add);
208 clib_error_report (error);
212 REPLY_MACRO (VL_API_SET_PUNT_REPLY);
216 vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
218 vl_api_punt_socket_register_reply_t *rmp;
219 vlib_main_t *vm = vlib_get_main ();
224 rv = vl_api_punt_decode (&mp->punt, &pr);
229 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
230 &pr, (char *) mp->pathname);
234 clib_error_report (error);
237 char *p = vnet_punt_get_server_pathname ();
240 REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
242 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
247 typedef struct punt_socket_send_ctx_t_
249 vl_api_registration_t *reg;
251 } punt_socket_send_ctx_t;
254 vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
256 punt_socket_send_ctx_t *ctx = args;
257 vl_api_punt_socket_details_t *mp;
259 mp = vl_msg_api_alloc (sizeof (*mp));
263 clib_memset (mp, 0, sizeof (*mp));
264 mp->_vl_msg_id = ntohs (VL_API_PUNT_SOCKET_DETAILS);
265 mp->context = ctx->context;
266 vl_api_punt_encode (&pc->reg, &mp->punt);
267 memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));
269 vl_api_send_msg (ctx->reg, (u8 *) mp);
271 return (WALK_CONTINUE);
275 vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
277 vl_api_registration_t *reg;
280 if (0 != vl_api_punt_type_decode (mp->type, &pt))
283 reg = vl_api_client_index_to_registration (mp->client_index);
287 punt_socket_send_ctx_t ctx = {
289 .context = mp->context,
292 punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
296 vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
298 vl_api_punt_socket_deregister_reply_t *rmp;
299 vlib_main_t *vm = vlib_get_main ();
304 rv = vl_api_punt_decode (&mp->punt, &pr);
309 error = vnet_punt_socket_del (vm, &pr);
313 clib_error_report (error);
317 REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
320 typedef struct punt_reason_dump_walk_ctx_t_
322 vl_api_registration_t *reg;
325 } punt_reason_dump_walk_ctx_t;
328 punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
330 punt_reason_dump_walk_ctx_t *ctx = args;
331 vl_api_punt_reason_details_t *mp;
335 /* user requested a specific punt-reason */
336 if (vec_cmp (name, ctx->name))
337 /* not the reasonn we're lookgin for */
341 mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
345 clib_memset (mp, 0, sizeof (*mp));
346 mp->_vl_msg_id = ntohs (VL_API_PUNT_REASON_DETAILS);
348 mp->context = ctx->context;
349 mp->reason.id = clib_host_to_net_u32 (id);
350 vl_api_to_api_string (vec_len (name), (char *) name, &mp->reason.name);
352 vl_api_send_msg (ctx->reg, (u8 *) mp);
358 vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
360 vl_api_registration_t *reg;
362 reg = vl_api_client_index_to_registration (mp->client_index);
366 punt_reason_dump_walk_ctx_t ctx = {
368 .context = mp->context,
369 .name = vl_api_from_api_to_vec (&mp->reason.name),
372 punt_reason_walk (punt_reason_dump_walk_cb, &ctx);
377 #define vl_msg_name_crc_list
378 #include <vnet/ip/punt.api.h>
379 #undef vl_msg_name_crc_list
382 setup_message_id_table (api_main_t * am)
384 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
385 foreach_vl_msg_name_crc_punt;
389 static clib_error_t *
390 punt_api_hookup (vlib_main_t * vm)
392 api_main_t *am = vlibapi_get_main ();
395 vl_msg_api_set_handlers(VL_API_##N, #n, \
396 vl_api_##n##_t_handler, \
398 vl_api_##n##_t_endian, \
399 vl_api_##n##_t_print, \
400 sizeof(vl_api_##n##_t), 1);
401 foreach_punt_api_msg;
405 * Set up the (msg_name, crc, message-id) table
407 setup_message_id_table (am);
412 VLIB_API_INIT_FUNCTION (punt_api_hookup);
416 * fd.io coding-style-patch-verification: ON
419 * eval: (c-set-style "gnu")