blob: 2acf8265dc1f8072e77b731e6ef0670e828dddf9 [file] [log] [blame]
Neale Rannsb8d44812017-11-10 06:53:54 -08001/*
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>
Neale Ranns50f0ac02019-05-15 02:13:37 -070023#include <vnet/ip/ip_types_api.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080024
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 \
Pavel Kotuceke88865d2018-11-28 07:42:11 +010044_(SET_PUNT, set_punt) \
Neale Rannsb8d44812017-11-10 06:53:54 -080045_(PUNT_SOCKET_REGISTER, punt_socket_register) \
Pavel Kotuceke88865d2018-11-28 07:42:11 +010046_(PUNT_SOCKET_DEREGISTER, punt_socket_deregister) \
Neale Ranns50f0ac02019-05-15 02:13:37 -070047_(PUNT_SOCKET_DUMP, punt_socket_dump) \
48_(PUNT_REASON_DUMP, punt_reason_dump)
49
50static int
51vl_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
68static vl_api_punt_type_t
69vl_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
86static int
87vl_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);
Jakub Grajciar7dd63e52020-03-19 08:03:55 +010092 if (rv < 0)
93 return (rv);
94 rv = ip_proto_decode (in->protocol, &out->protocol);
95 if (rv < 0)
96 return (rv);
Neale Ranns50f0ac02019-05-15 02:13:37 -070097 out->port = clib_net_to_host_u16 (in->port);
98
99 return (rv);
100}
101
102static int
Neale Rannsb538dd82019-05-21 06:54:54 -0700103vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
104 punt_ip_proto_t * out)
105{
106 int rv;
107
108 rv = ip_address_family_decode (in->af, &out->af);
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100109 if (rv < 0)
110 return (rv);
111 rv = ip_proto_decode (in->protocol, &out->protocol);
Neale Rannsb538dd82019-05-21 06:54:54 -0700112
113 return (rv);
114}
115
116static int
Neale Ranns50f0ac02019-05-15 02:13:37 -0700117vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
118 punt_exception_t * out)
119{
120 int rv;
121
122 out->reason = clib_net_to_host_u32 (in->id);
123 rv = vlib_punt_reason_validate (out->reason);
124
125 return (rv);
126}
127
128static int
129vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
130{
131 int rv;
132
133 rv = vl_api_punt_type_decode (in->type, &out->type);
134
135 if (rv)
136 return (rv);
137
138 switch (out->type)
139 {
140 case PUNT_TYPE_L4:
141 return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
142 case PUNT_TYPE_EXCEPTION:
143 return (vl_api_punt_exception_decode (&in->punt.exception,
144 &out->punt.exception));
Neale Rannsb538dd82019-05-21 06:54:54 -0700145 case PUNT_TYPE_IP_PROTO:
146 return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
147 &out->punt.ip_proto));
Neale Ranns50f0ac02019-05-15 02:13:37 -0700148 }
149
150 return (-1);
151}
152
153static void
154vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
155{
156 out->af = ip_address_family_encode (in->af);
157 out->protocol = ip_proto_encode (in->protocol);
158 out->port = clib_net_to_host_u16 (in->port);
159}
160
161static void
Neale Rannsb538dd82019-05-21 06:54:54 -0700162vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
163 vl_api_punt_ip_proto_t * out)
164{
165 out->af = ip_address_family_encode (in->af);
166 out->protocol = ip_proto_encode (in->protocol);
167}
168
169static void
Neale Ranns50f0ac02019-05-15 02:13:37 -0700170vl_api_punt_exception_encode (const punt_exception_t * in,
171 vl_api_punt_exception_t * out)
172{
173 out->id = clib_host_to_net_u32 (in->reason);
174}
175
176static void
177vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
178{
179 out->type = vl_api_punt_type_encode (in->type);
180
181 switch (in->type)
182 {
183 case PUNT_TYPE_L4:
184 vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
185 break;
Neale Rannsb538dd82019-05-21 06:54:54 -0700186 case PUNT_TYPE_IP_PROTO:
187 vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
188 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700189 case PUNT_TYPE_EXCEPTION:
190 vl_api_punt_exception_encode (&in->punt.exception,
191 &out->punt.exception);
192 break;
193 }
194}
Neale Rannsb8d44812017-11-10 06:53:54 -0800195
196static void
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100197vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -0800198{
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100199 vl_api_set_punt_reply_t *rmp;
Neale Rannsb8d44812017-11-10 06:53:54 -0800200 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800201 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700202 punt_reg_t pr;
203 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -0800204
Neale Ranns50f0ac02019-05-15 02:13:37 -0700205 rv = vl_api_punt_decode (&mp->punt, &pr);
206
207 if (rv)
208 goto out;
209
210 error = vnet_punt_add_del (vm, &pr, mp->is_add);
Neale Rannsb8d44812017-11-10 06:53:54 -0800211 if (error)
212 {
213 rv = -1;
214 clib_error_report (error);
215 }
216
Neale Ranns50f0ac02019-05-15 02:13:37 -0700217out:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100218 REPLY_MACRO (VL_API_SET_PUNT_REPLY);
219}
220
221static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800222vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
223{
224 vl_api_punt_socket_register_reply_t *rmp;
225 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800226 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700227 punt_reg_t pr;
228 int rv;
229
230 rv = vl_api_punt_decode (&mp->punt, &pr);
231
232 if (rv)
233 return;
Neale Rannsb8d44812017-11-10 06:53:54 -0800234
Steven Luong99ae31d2019-03-05 15:42:21 -0800235 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
Neale Ranns50f0ac02019-05-15 02:13:37 -0700236 &pr, (char *) mp->pathname);
Neale Rannsb8d44812017-11-10 06:53:54 -0800237 if (error)
238 {
239 rv = -1;
240 clib_error_report (error);
241 }
242
Neale Rannsb8d44812017-11-10 06:53:54 -0800243 char *p = vnet_punt_get_server_pathname ();
Neale Ranns50f0ac02019-05-15 02:13:37 -0700244
245 /* *INDENT-OFF* */
246 REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
247 ({
248 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
249 }));
250 /* *INDENT-ON* */
Neale Rannsb8d44812017-11-10 06:53:54 -0800251}
252
Neale Ranns50f0ac02019-05-15 02:13:37 -0700253typedef struct punt_socket_send_ctx_t_
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100254{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700255 vl_api_registration_t *reg;
256 u32 context;
257} punt_socket_send_ctx_t;
258
259static walk_rc_t
260vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
261{
262 punt_socket_send_ctx_t *ctx = args;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100263 vl_api_punt_socket_details_t *mp;
264
265 mp = vl_msg_api_alloc (sizeof (*mp));
266 if (!mp)
Neale Ranns50f0ac02019-05-15 02:13:37 -0700267 return (WALK_STOP);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100268
269 clib_memset (mp, 0, sizeof (*mp));
270 mp->_vl_msg_id = ntohs (VL_API_PUNT_SOCKET_DETAILS);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700271 mp->context = ctx->context;
272 vl_api_punt_encode (&pc->reg, &mp->punt);
273 memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100274
Neale Ranns50f0ac02019-05-15 02:13:37 -0700275 vl_api_send_msg (ctx->reg, (u8 *) mp);
276
277 return (WALK_CONTINUE);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100278}
279
280static void
281vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
282{
283 vl_api_registration_t *reg;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700284 punt_type_t pt;
285
286 if (0 != vl_api_punt_type_decode (mp->type, &pt))
287 return;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100288
289 reg = vl_api_client_index_to_registration (mp->client_index);
290 if (!reg)
291 return;
292
Neale Ranns50f0ac02019-05-15 02:13:37 -0700293 punt_socket_send_ctx_t ctx = {
294 .reg = reg,
295 .context = mp->context,
296 };
297
298 punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100299}
300
Neale Rannsb8d44812017-11-10 06:53:54 -0800301static void
302vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
303{
304 vl_api_punt_socket_deregister_reply_t *rmp;
305 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800306 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700307 punt_reg_t pr;
308 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -0800309
Neale Ranns50f0ac02019-05-15 02:13:37 -0700310 rv = vl_api_punt_decode (&mp->punt, &pr);
311
312 if (rv)
313 goto out;
314
315 error = vnet_punt_socket_del (vm, &pr);
Neale Rannsb8d44812017-11-10 06:53:54 -0800316 if (error)
317 {
318 rv = -1;
319 clib_error_report (error);
320 }
321
Neale Ranns50f0ac02019-05-15 02:13:37 -0700322out:
323 REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
324}
325
326typedef struct punt_reason_dump_walk_ctx_t_
327{
328 vl_api_registration_t *reg;
329 u32 context;
Neale Ranns719beb72019-07-10 07:10:25 +0000330 u8 *name;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700331} punt_reason_dump_walk_ctx_t;
332
333static int
334punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
335{
336 punt_reason_dump_walk_ctx_t *ctx = args;
337 vl_api_punt_reason_details_t *mp;
338
Neale Ranns719beb72019-07-10 07:10:25 +0000339 if (ctx->name)
340 {
341 /* user requested a specific punt-reason */
342 if (vec_cmp (name, ctx->name))
343 /* not the reasonn we're lookgin for */
344 return 1;
345 }
346
Neale Ranns50f0ac02019-05-15 02:13:37 -0700347 mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
348 if (!mp)
349 return (0);
350
351 clib_memset (mp, 0, sizeof (*mp));
352 mp->_vl_msg_id = ntohs (VL_API_PUNT_REASON_DETAILS);
353
354 mp->context = ctx->context;
355 mp->reason.id = clib_host_to_net_u32 (id);
Jakub Grajciar2dbee932020-02-07 11:30:26 +0100356 vl_api_vec_to_api_string (name, &mp->reason.name);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700357
358 vl_api_send_msg (ctx->reg, (u8 *) mp);
359
360 return (1);
361}
362
363static void
364vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
365{
366 vl_api_registration_t *reg;
367
Florin Coras6c4dae22018-01-09 06:39:23 -0800368 reg = vl_api_client_index_to_registration (mp->client_index);
369 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -0800370 return;
371
Neale Ranns50f0ac02019-05-15 02:13:37 -0700372 punt_reason_dump_walk_ctx_t ctx = {
373 .reg = reg,
374 .context = mp->context,
Jakub Grajciar2dbee932020-02-07 11:30:26 +0100375 .name = vl_api_from_api_to_new_vec (&mp->reason.name),
Neale Ranns50f0ac02019-05-15 02:13:37 -0700376 };
377
378 punt_reason_walk (punt_reason_dump_walk_cb, &ctx);
Neale Ranns719beb72019-07-10 07:10:25 +0000379
380 vec_free (ctx.name);
Neale Rannsb8d44812017-11-10 06:53:54 -0800381}
382
383#define vl_msg_name_crc_list
384#include <vnet/ip/punt.api.h>
385#undef vl_msg_name_crc_list
386
387static void
388setup_message_id_table (api_main_t * am)
389{
390#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
391 foreach_vl_msg_name_crc_punt;
392#undef _
393}
394
395static clib_error_t *
396punt_api_hookup (vlib_main_t * vm)
397{
Dave Barach39d69112019-11-27 11:42:13 -0500398 api_main_t *am = vlibapi_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800399
400#define _(N,n) \
401 vl_msg_api_set_handlers(VL_API_##N, #n, \
402 vl_api_##n##_t_handler, \
403 vl_noop_handler, \
404 vl_api_##n##_t_endian, \
405 vl_api_##n##_t_print, \
406 sizeof(vl_api_##n##_t), 1);
407 foreach_punt_api_msg;
408#undef _
409
410 /*
411 * Set up the (msg_name, crc, message-id) table
412 */
413 setup_message_id_table (am);
414
415 return 0;
416}
417
418VLIB_API_INIT_FUNCTION (punt_api_hookup);
419
420
421/*
422 * fd.io coding-style-patch-verification: ON
423 *
424 * Local Variables:
425 * eval: (c-set-style "gnu")
426 * End:
427 */