blob: b356886a56f9a2d2e2af4d191dc8f3db75ff612a [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);
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
98static int
Neale Rannsb538dd82019-05-21 06:54:54 -070099vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
100 punt_ip_proto_t * out)
101{
102 int rv;
103
104 rv = ip_address_family_decode (in->af, &out->af);
105 rv += ip_proto_decode (in->protocol, &out->protocol);
106
107 return (rv);
108}
109
110static int
Neale Ranns50f0ac02019-05-15 02:13:37 -0700111vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
112 punt_exception_t * out)
113{
114 int rv;
115
116 out->reason = clib_net_to_host_u32 (in->id);
117 rv = vlib_punt_reason_validate (out->reason);
118
119 return (rv);
120}
121
122static int
123vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
124{
125 int rv;
126
127 rv = vl_api_punt_type_decode (in->type, &out->type);
128
129 if (rv)
130 return (rv);
131
132 switch (out->type)
133 {
134 case PUNT_TYPE_L4:
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));
Neale Rannsb538dd82019-05-21 06:54:54 -0700139 case PUNT_TYPE_IP_PROTO:
140 return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
141 &out->punt.ip_proto));
Neale Ranns50f0ac02019-05-15 02:13:37 -0700142 }
143
144 return (-1);
145}
146
147static void
148vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
149{
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);
153}
154
155static void
Neale Rannsb538dd82019-05-21 06:54:54 -0700156vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
157 vl_api_punt_ip_proto_t * out)
158{
159 out->af = ip_address_family_encode (in->af);
160 out->protocol = ip_proto_encode (in->protocol);
161}
162
163static void
Neale Ranns50f0ac02019-05-15 02:13:37 -0700164vl_api_punt_exception_encode (const punt_exception_t * in,
165 vl_api_punt_exception_t * out)
166{
167 out->id = clib_host_to_net_u32 (in->reason);
168}
169
170static void
171vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
172{
173 out->type = vl_api_punt_type_encode (in->type);
174
175 switch (in->type)
176 {
177 case PUNT_TYPE_L4:
178 vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
179 break;
Neale Rannsb538dd82019-05-21 06:54:54 -0700180 case PUNT_TYPE_IP_PROTO:
181 vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
182 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700183 case PUNT_TYPE_EXCEPTION:
184 vl_api_punt_exception_encode (&in->punt.exception,
185 &out->punt.exception);
186 break;
187 }
188}
Neale Rannsb8d44812017-11-10 06:53:54 -0800189
190static void
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100191vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -0800192{
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100193 vl_api_set_punt_reply_t *rmp;
Neale Rannsb8d44812017-11-10 06:53:54 -0800194 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800195 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700196 punt_reg_t pr;
197 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -0800198
Neale Ranns50f0ac02019-05-15 02:13:37 -0700199 rv = vl_api_punt_decode (&mp->punt, &pr);
200
201 if (rv)
202 goto out;
203
204 error = vnet_punt_add_del (vm, &pr, mp->is_add);
Neale Rannsb8d44812017-11-10 06:53:54 -0800205 if (error)
206 {
207 rv = -1;
208 clib_error_report (error);
209 }
210
Neale Ranns50f0ac02019-05-15 02:13:37 -0700211out:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100212 REPLY_MACRO (VL_API_SET_PUNT_REPLY);
213}
214
215static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800216vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
217{
218 vl_api_punt_socket_register_reply_t *rmp;
219 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800220 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700221 punt_reg_t pr;
222 int rv;
223
224 rv = vl_api_punt_decode (&mp->punt, &pr);
225
226 if (rv)
227 return;
Neale Rannsb8d44812017-11-10 06:53:54 -0800228
Steven Luong99ae31d2019-03-05 15:42:21 -0800229 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
Neale Ranns50f0ac02019-05-15 02:13:37 -0700230 &pr, (char *) mp->pathname);
Neale Rannsb8d44812017-11-10 06:53:54 -0800231 if (error)
232 {
233 rv = -1;
234 clib_error_report (error);
235 }
236
Neale Rannsb8d44812017-11-10 06:53:54 -0800237 char *p = vnet_punt_get_server_pathname ();
Neale Ranns50f0ac02019-05-15 02:13:37 -0700238
239 /* *INDENT-OFF* */
240 REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
241 ({
242 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
243 }));
244 /* *INDENT-ON* */
Neale Rannsb8d44812017-11-10 06:53:54 -0800245}
246
Neale Ranns50f0ac02019-05-15 02:13:37 -0700247typedef struct punt_socket_send_ctx_t_
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100248{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700249 vl_api_registration_t *reg;
250 u32 context;
251} punt_socket_send_ctx_t;
252
253static walk_rc_t
254vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
255{
256 punt_socket_send_ctx_t *ctx = args;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100257 vl_api_punt_socket_details_t *mp;
258
259 mp = vl_msg_api_alloc (sizeof (*mp));
260 if (!mp)
Neale Ranns50f0ac02019-05-15 02:13:37 -0700261 return (WALK_STOP);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100262
263 clib_memset (mp, 0, sizeof (*mp));
264 mp->_vl_msg_id = ntohs (VL_API_PUNT_SOCKET_DETAILS);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700265 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));
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100268
Neale Ranns50f0ac02019-05-15 02:13:37 -0700269 vl_api_send_msg (ctx->reg, (u8 *) mp);
270
271 return (WALK_CONTINUE);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100272}
273
274static void
275vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
276{
277 vl_api_registration_t *reg;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700278 punt_type_t pt;
279
280 if (0 != vl_api_punt_type_decode (mp->type, &pt))
281 return;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100282
283 reg = vl_api_client_index_to_registration (mp->client_index);
284 if (!reg)
285 return;
286
Neale Ranns50f0ac02019-05-15 02:13:37 -0700287 punt_socket_send_ctx_t ctx = {
288 .reg = reg,
289 .context = mp->context,
290 };
291
292 punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100293}
294
Neale Rannsb8d44812017-11-10 06:53:54 -0800295static void
296vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
297{
298 vl_api_punt_socket_deregister_reply_t *rmp;
299 vlib_main_t *vm = vlib_get_main ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800300 clib_error_t *error;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700301 punt_reg_t pr;
302 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -0800303
Neale Ranns50f0ac02019-05-15 02:13:37 -0700304 rv = vl_api_punt_decode (&mp->punt, &pr);
305
306 if (rv)
307 goto out;
308
309 error = vnet_punt_socket_del (vm, &pr);
Neale Rannsb8d44812017-11-10 06:53:54 -0800310 if (error)
311 {
312 rv = -1;
313 clib_error_report (error);
314 }
315
Neale Ranns50f0ac02019-05-15 02:13:37 -0700316out:
317 REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
318}
319
320typedef struct punt_reason_dump_walk_ctx_t_
321{
322 vl_api_registration_t *reg;
323 u32 context;
324} punt_reason_dump_walk_ctx_t;
325
326static int
327punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
328{
329 punt_reason_dump_walk_ctx_t *ctx = args;
330 vl_api_punt_reason_details_t *mp;
331
332 mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
333 if (!mp)
334 return (0);
335
336 clib_memset (mp, 0, sizeof (*mp));
337 mp->_vl_msg_id = ntohs (VL_API_PUNT_REASON_DETAILS);
338
339 mp->context = ctx->context;
340 mp->reason.id = clib_host_to_net_u32 (id);
341 vl_api_to_api_string (vec_len (name), (char *) name, &mp->reason.name);
342
343 vl_api_send_msg (ctx->reg, (u8 *) mp);
344
345 return (1);
346}
347
348static void
349vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
350{
351 vl_api_registration_t *reg;
352
Florin Coras6c4dae22018-01-09 06:39:23 -0800353 reg = vl_api_client_index_to_registration (mp->client_index);
354 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -0800355 return;
356
Neale Ranns50f0ac02019-05-15 02:13:37 -0700357 punt_reason_dump_walk_ctx_t ctx = {
358 .reg = reg,
359 .context = mp->context,
360 };
361
362 punt_reason_walk (punt_reason_dump_walk_cb, &ctx);
Neale Rannsb8d44812017-11-10 06:53:54 -0800363}
364
365#define vl_msg_name_crc_list
366#include <vnet/ip/punt.api.h>
367#undef vl_msg_name_crc_list
368
369static void
370setup_message_id_table (api_main_t * am)
371{
372#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
373 foreach_vl_msg_name_crc_punt;
374#undef _
375}
376
377static clib_error_t *
378punt_api_hookup (vlib_main_t * vm)
379{
380 api_main_t *am = &api_main;
381
382#define _(N,n) \
383 vl_msg_api_set_handlers(VL_API_##N, #n, \
384 vl_api_##n##_t_handler, \
385 vl_noop_handler, \
386 vl_api_##n##_t_endian, \
387 vl_api_##n##_t_print, \
388 sizeof(vl_api_##n##_t), 1);
389 foreach_punt_api_msg;
390#undef _
391
392 /*
393 * Set up the (msg_name, crc, message-id) table
394 */
395 setup_message_id_table (am);
396
397 return 0;
398}
399
400VLIB_API_INIT_FUNCTION (punt_api_hookup);
401
402
403/*
404 * fd.io coding-style-patch-verification: ON
405 *
406 * Local Variables:
407 * eval: (c-set-style "gnu")
408 * End:
409 */