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/format_fns.h>
26 #include <vnet/ip/punt.api_enum.h>
27 #include <vnet/ip/punt.api_types.h>
29 #define REPLY_MSG_ID_BASE punt_main.msg_id_base
30 #include <vlibapi/api_helper_macros.h>
33 vl_api_punt_type_decode (vl_api_punt_type_t in, punt_type_t * out)
35 in = clib_net_to_host_u32 (in);
40 case PUNT_API_TYPE_##v: \
41 *out = PUNT_TYPE_##v; \
50 static vl_api_punt_type_t
51 vl_api_punt_type_encode (punt_type_t in)
53 vl_api_punt_type_t pt = PUNT_API_TYPE_L4;
59 pt = PUNT_API_TYPE_##v; \
65 return (clib_host_to_net_u32 (pt));
69 vl_api_punt_l4_decode (const vl_api_punt_l4_t * in, punt_l4_t * out)
73 rv = ip_address_family_decode (in->af, &out->af);
76 rv = ip_proto_decode (in->protocol, &out->protocol);
79 out->port = clib_net_to_host_u16 (in->port);
85 vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
86 punt_ip_proto_t * out)
90 rv = ip_address_family_decode (in->af, &out->af);
93 rv = ip_proto_decode (in->protocol, &out->protocol);
99 vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
100 punt_exception_t * out)
104 out->reason = clib_net_to_host_u32 (in->id);
105 rv = vlib_punt_reason_validate (out->reason);
111 vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
115 rv = vl_api_punt_type_decode (in->type, &out->type);
123 return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
124 case PUNT_TYPE_EXCEPTION:
125 return (vl_api_punt_exception_decode (&in->punt.exception,
126 &out->punt.exception));
127 case PUNT_TYPE_IP_PROTO:
128 return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
129 &out->punt.ip_proto));
136 vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
138 out->af = ip_address_family_encode (in->af);
139 out->protocol = ip_proto_encode (in->protocol);
140 out->port = clib_net_to_host_u16 (in->port);
144 vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
145 vl_api_punt_ip_proto_t * out)
147 out->af = ip_address_family_encode (in->af);
148 out->protocol = ip_proto_encode (in->protocol);
152 vl_api_punt_exception_encode (const punt_exception_t * in,
153 vl_api_punt_exception_t * out)
155 out->id = clib_host_to_net_u32 (in->reason);
159 vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
161 out->type = vl_api_punt_type_encode (in->type);
166 vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
168 case PUNT_TYPE_IP_PROTO:
169 vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
171 case PUNT_TYPE_EXCEPTION:
172 vl_api_punt_exception_encode (&in->punt.exception,
173 &out->punt.exception);
179 vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
181 vl_api_set_punt_reply_t *rmp;
182 vlib_main_t *vm = vlib_get_main ();
187 rv = vl_api_punt_decode (&mp->punt, &pr);
192 error = vnet_punt_add_del (vm, &pr, mp->is_add);
196 clib_error_report (error);
200 REPLY_MACRO (VL_API_SET_PUNT_REPLY);
204 vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
206 vl_api_punt_socket_register_reply_t *rmp;
207 vlib_main_t *vm = vlib_get_main ();
212 rv = vl_api_punt_decode (&mp->punt, &pr);
217 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
218 &pr, (char *) mp->pathname);
222 clib_error_report (error);
225 char *p = vnet_punt_get_server_pathname ();
228 REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
230 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
235 typedef struct punt_socket_send_ctx_t_
237 vl_api_registration_t *reg;
239 } punt_socket_send_ctx_t;
242 vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
244 punt_socket_send_ctx_t *ctx = args;
245 vl_api_punt_socket_details_t *mp;
247 mp = vl_msg_api_alloc (sizeof (*mp));
251 clib_memset (mp, 0, sizeof (*mp));
252 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_SOCKET_DETAILS);
253 mp->context = ctx->context;
254 vl_api_punt_encode (&pc->reg, &mp->punt);
255 memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));
257 vl_api_send_msg (ctx->reg, (u8 *) mp);
259 return (WALK_CONTINUE);
263 vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
265 vl_api_registration_t *reg;
268 if (0 != vl_api_punt_type_decode (mp->type, &pt))
271 reg = vl_api_client_index_to_registration (mp->client_index);
275 punt_socket_send_ctx_t ctx = {
277 .context = mp->context,
280 punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
284 vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
286 vl_api_punt_socket_deregister_reply_t *rmp;
287 vlib_main_t *vm = vlib_get_main ();
292 rv = vl_api_punt_decode (&mp->punt, &pr);
297 error = vnet_punt_socket_del (vm, &pr);
301 clib_error_report (error);
305 REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
308 typedef struct punt_reason_dump_walk_ctx_t_
310 vl_api_registration_t *reg;
313 } punt_reason_dump_walk_ctx_t;
316 punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
318 punt_reason_dump_walk_ctx_t *ctx = args;
319 vl_api_punt_reason_details_t *mp;
323 /* user requested a specific punt-reason */
324 if (vec_cmp (name, ctx->name))
325 /* not the reasonn we're lookgin for */
329 mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
333 clib_memset (mp, 0, sizeof (*mp));
334 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_REASON_DETAILS);
336 mp->context = ctx->context;
337 mp->reason.id = clib_host_to_net_u32 (id);
338 vl_api_vec_to_api_string (name, &mp->reason.name);
340 vl_api_send_msg (ctx->reg, (u8 *) mp);
346 vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
348 vl_api_registration_t *reg;
350 reg = vl_api_client_index_to_registration (mp->client_index);
354 punt_reason_dump_walk_ctx_t ctx = {
356 .context = mp->context,
357 .name = vl_api_from_api_to_new_vec (mp, &mp->reason.name),
360 punt_reason_walk (punt_reason_dump_walk_cb, &ctx);
365 #include <vnet/ip/punt.api.c>
367 static clib_error_t *
368 punt_api_hookup (vlib_main_t * vm)
371 * Set up the (msg_name, crc, message-id) table
373 REPLY_MSG_ID_BASE = setup_message_id_table ();
378 VLIB_API_INIT_FUNCTION (punt_api_hookup);
382 * fd.io coding-style-patch-verification: ON
385 * eval: (c-set-style "gnu")