api: string type to convert to vector
[vpp.git] / src / vnet / ip / punt_api.c
1 /*
2  *------------------------------------------------------------------
3  * punt_api.c - Punt api
4  *
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:
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 #include <vnet/ip/punt.h>
23 #include <vnet/ip/ip_types_api.h>
24 #include <vlibapi/api_types_inlines.h>
25
26 #include <vnet/vnet_msg_enum.h>
27
28 #define vl_typedefs             /* define message structures */
29 #include <vnet/vnet_all_api_h.h>
30 #undef vl_typedefs
31
32 #define vl_endianfun            /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_endianfun
35
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
38 #define vl_printfun
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_printfun
41
42 #include <vlibapi/api_helper_macros.h>
43
44 #define foreach_punt_api_msg                                            \
45 _(SET_PUNT, set_punt)                                                   \
46 _(PUNT_SOCKET_REGISTER, punt_socket_register)                           \
47 _(PUNT_SOCKET_DEREGISTER, punt_socket_deregister)                       \
48 _(PUNT_SOCKET_DUMP, punt_socket_dump)                                   \
49 _(PUNT_REASON_DUMP, punt_reason_dump)
50
51 static int
52 vl_api_punt_type_decode (vl_api_punt_type_t in, punt_type_t * out)
53 {
54   in = clib_net_to_host_u32 (in);
55
56   switch (in)
57     {
58 #define _(v, s)                                 \
59       case PUNT_API_TYPE_##v:                   \
60         *out = PUNT_TYPE_##v;                   \
61         return (0);
62       foreach_punt_type
63 #undef _
64     }
65
66   return (-1);
67 }
68
69 static vl_api_punt_type_t
70 vl_api_punt_type_encode (punt_type_t in)
71 {
72   vl_api_punt_type_t pt = PUNT_API_TYPE_L4;
73
74   switch (in)
75     {
76 #define _(v, s)                                   \
77       case PUNT_TYPE_##v:                         \
78         pt = PUNT_API_TYPE_##v;                   \
79         break;
80       foreach_punt_type
81 #undef _
82     }
83
84   return (clib_host_to_net_u32 (pt));
85 }
86
87 static int
88 vl_api_punt_l4_decode (const vl_api_punt_l4_t * in, punt_l4_t * out)
89 {
90   int rv;
91
92   rv = ip_address_family_decode (in->af, &out->af);
93   rv += ip_proto_decode (in->protocol, &out->protocol);
94   out->port = clib_net_to_host_u16 (in->port);
95
96   return (rv);
97 }
98
99 static int
100 vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
101                              punt_ip_proto_t * out)
102 {
103   int rv;
104
105   rv = ip_address_family_decode (in->af, &out->af);
106   rv += ip_proto_decode (in->protocol, &out->protocol);
107
108   return (rv);
109 }
110
111 static int
112 vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
113                               punt_exception_t * out)
114 {
115   int rv;
116
117   out->reason = clib_net_to_host_u32 (in->id);
118   rv = vlib_punt_reason_validate (out->reason);
119
120   return (rv);
121 }
122
123 static int
124 vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
125 {
126   int rv;
127
128   rv = vl_api_punt_type_decode (in->type, &out->type);
129
130   if (rv)
131     return (rv);
132
133   switch (out->type)
134     {
135     case PUNT_TYPE_L4:
136       return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
137     case PUNT_TYPE_EXCEPTION:
138       return (vl_api_punt_exception_decode (&in->punt.exception,
139                                             &out->punt.exception));
140     case PUNT_TYPE_IP_PROTO:
141       return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
142                                            &out->punt.ip_proto));
143     }
144
145   return (-1);
146 }
147
148 static void
149 vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
150 {
151   out->af = ip_address_family_encode (in->af);
152   out->protocol = ip_proto_encode (in->protocol);
153   out->port = clib_net_to_host_u16 (in->port);
154 }
155
156 static void
157 vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
158                              vl_api_punt_ip_proto_t * out)
159 {
160   out->af = ip_address_family_encode (in->af);
161   out->protocol = ip_proto_encode (in->protocol);
162 }
163
164 static void
165 vl_api_punt_exception_encode (const punt_exception_t * in,
166                               vl_api_punt_exception_t * out)
167 {
168   out->id = clib_host_to_net_u32 (in->reason);
169 }
170
171 static void
172 vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
173 {
174   out->type = vl_api_punt_type_encode (in->type);
175
176   switch (in->type)
177     {
178     case PUNT_TYPE_L4:
179       vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
180       break;
181     case PUNT_TYPE_IP_PROTO:
182       vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
183       break;
184     case PUNT_TYPE_EXCEPTION:
185       vl_api_punt_exception_encode (&in->punt.exception,
186                                     &out->punt.exception);
187       break;
188     }
189 }
190
191 static void
192 vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
193 {
194   vl_api_set_punt_reply_t *rmp;
195   vlib_main_t *vm = vlib_get_main ();
196   clib_error_t *error;
197   punt_reg_t pr;
198   int rv;
199
200   rv = vl_api_punt_decode (&mp->punt, &pr);
201
202   if (rv)
203     goto out;
204
205   error = vnet_punt_add_del (vm, &pr, mp->is_add);
206   if (error)
207     {
208       rv = -1;
209       clib_error_report (error);
210     }
211
212 out:
213   REPLY_MACRO (VL_API_SET_PUNT_REPLY);
214 }
215
216 static void
217 vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
218 {
219   vl_api_punt_socket_register_reply_t *rmp;
220   vlib_main_t *vm = vlib_get_main ();
221   clib_error_t *error;
222   punt_reg_t pr;
223   int rv;
224
225   rv = vl_api_punt_decode (&mp->punt, &pr);
226
227   if (rv)
228     return;
229
230   error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
231                                 &pr, (char *) mp->pathname);
232   if (error)
233     {
234       rv = -1;
235       clib_error_report (error);
236     }
237
238   char *p = vnet_punt_get_server_pathname ();
239
240   /* *INDENT-OFF* */
241   REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
242   ({
243     memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
244   }));
245   /* *INDENT-ON* */
246 }
247
248 typedef struct punt_socket_send_ctx_t_
249 {
250   vl_api_registration_t *reg;
251   u32 context;
252 } punt_socket_send_ctx_t;
253
254 static walk_rc_t
255 vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
256 {
257   punt_socket_send_ctx_t *ctx = args;
258   vl_api_punt_socket_details_t *mp;
259
260   mp = vl_msg_api_alloc (sizeof (*mp));
261   if (!mp)
262     return (WALK_STOP);
263
264   clib_memset (mp, 0, sizeof (*mp));
265   mp->_vl_msg_id = ntohs (VL_API_PUNT_SOCKET_DETAILS);
266   mp->context = ctx->context;
267   vl_api_punt_encode (&pc->reg, &mp->punt);
268   memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));
269
270   vl_api_send_msg (ctx->reg, (u8 *) mp);
271
272   return (WALK_CONTINUE);
273 }
274
275 static void
276 vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
277 {
278   vl_api_registration_t *reg;
279   punt_type_t pt;
280
281   if (0 != vl_api_punt_type_decode (mp->type, &pt))
282     return;
283
284   reg = vl_api_client_index_to_registration (mp->client_index);
285   if (!reg)
286     return;
287
288   punt_socket_send_ctx_t ctx = {
289     .reg = reg,
290     .context = mp->context,
291   };
292
293   punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
294 }
295
296 static void
297 vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
298 {
299   vl_api_punt_socket_deregister_reply_t *rmp;
300   vlib_main_t *vm = vlib_get_main ();
301   clib_error_t *error;
302   punt_reg_t pr;
303   int rv;
304
305   rv = vl_api_punt_decode (&mp->punt, &pr);
306
307   if (rv)
308     goto out;
309
310   error = vnet_punt_socket_del (vm, &pr);
311   if (error)
312     {
313       rv = -1;
314       clib_error_report (error);
315     }
316
317 out:
318   REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
319 }
320
321 typedef struct punt_reason_dump_walk_ctx_t_
322 {
323   vl_api_registration_t *reg;
324   u32 context;
325 } punt_reason_dump_walk_ctx_t;
326
327 static int
328 punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
329 {
330   punt_reason_dump_walk_ctx_t *ctx = args;
331   vl_api_punt_reason_details_t *mp;
332
333   mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
334   if (!mp)
335     return (0);
336
337   clib_memset (mp, 0, sizeof (*mp));
338   mp->_vl_msg_id = ntohs (VL_API_PUNT_REASON_DETAILS);
339
340   mp->context = ctx->context;
341   mp->reason.id = clib_host_to_net_u32 (id);
342   vl_api_to_api_string (vec_len (name), (char *) name, &mp->reason.name);
343
344   vl_api_send_msg (ctx->reg, (u8 *) mp);
345
346   return (1);
347 }
348
349 static void
350 vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
351 {
352   vl_api_registration_t *reg;
353
354   reg = vl_api_client_index_to_registration (mp->client_index);
355   if (!reg)
356     return;
357
358   punt_reason_dump_walk_ctx_t ctx = {
359     .reg = reg,
360     .context = mp->context,
361   };
362
363   punt_reason_walk (punt_reason_dump_walk_cb, &ctx);
364 }
365
366 #define vl_msg_name_crc_list
367 #include <vnet/ip/punt.api.h>
368 #undef vl_msg_name_crc_list
369
370 static void
371 setup_message_id_table (api_main_t * am)
372 {
373 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
374   foreach_vl_msg_name_crc_punt;
375 #undef _
376 }
377
378 static clib_error_t *
379 punt_api_hookup (vlib_main_t * vm)
380 {
381   api_main_t *am = &api_main;
382
383 #define _(N,n)                                                  \
384     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
385                            vl_api_##n##_t_handler,              \
386                            vl_noop_handler,                     \
387                            vl_api_##n##_t_endian,               \
388                            vl_api_##n##_t_print,                \
389                            sizeof(vl_api_##n##_t), 1);
390   foreach_punt_api_msg;
391 #undef _
392
393   /*
394    * Set up the (msg_name, crc, message-id) table
395    */
396   setup_message_id_table (am);
397
398   return 0;
399 }
400
401 VLIB_API_INIT_FUNCTION (punt_api_hookup);
402
403
404 /*
405  * fd.io coding-style-patch-verification: ON
406  *
407  * Local Variables:
408  * eval: (c-set-style "gnu")
409  * End:
410  */