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