blob: 51378a5675e64082ca7fcbf0fd599816949782ac [file] [log] [blame]
Florin Corase86a8ed2018-01-05 03:20:25 -08001/*
2 *------------------------------------------------------------------
3 * vlib_api.c VLIB API implementation
4 *
5 * Copyright (c) 2009 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 <fcntl.h>
21#include <pthread.h>
22#include <vppinfra/vec.h>
23#include <vppinfra/hash.h>
24#include <vppinfra/pool.h>
25#include <vppinfra/format.h>
26#include <vppinfra/byte_order.h>
27#include <vppinfra/elog.h>
28#include <vlib/vlib.h>
29#include <vlib/unix/unix.h>
30#include <vlibapi/api.h>
31#include <vlibmemory/api.h>
32
33/**
34 * @file
35 * @brief Binary API messaging via shared memory
36 * Low-level, primary provisioning interface
37 */
38/*? %%clicmd:group_label Binary API CLI %% ?*/
39/*? %%syscfg:group_label Binary API configuration %% ?*/
40
41#define TRACE_VLIB_MEMORY_QUEUE 0
42
43#include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
44
45#define vl_typedefs /* define message structures */
46#include <vlibmemory/vl_memory_api_h.h>
47#undef vl_typedefs
48
49/* instantiate all the print functions we know about */
50#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51#define vl_printfun
52#include <vlibmemory/vl_memory_api_h.h>
53#undef vl_printfun
54
55static inline void *
56vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
57 void *handle)
58{
Ole Troan7adaa222019-08-27 15:05:27 +020059 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
60 a->plugin_name,
Florin Corase86a8ed2018-01-05 03:20:25 -080061 clib_host_to_net_u16 (a->first_msg_id),
62 clib_host_to_net_u16 (a->last_msg_id));
63 return handle;
64}
65
66/* instantiate all the endian swap functions we know about */
67#define vl_endianfun
68#include <vlibmemory/vl_memory_api_h.h>
69#undef vl_endianfun
70
Florin Corase86a8ed2018-01-05 03:20:25 -080071static void
72vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
73{
74 vl_api_get_first_msg_id_reply_t *rmp;
75 vl_api_registration_t *regp;
76 uword *p;
Dave Barach39d69112019-11-27 11:42:13 -050077 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -080078 vl_api_msg_range_t *rp;
Ole Troan7adaa222019-08-27 15:05:27 +020079 u8 name[64];
Florin Corase86a8ed2018-01-05 03:20:25 -080080 u16 first_msg_id = ~0;
81 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
82
83 regp = vl_api_client_index_to_registration (mp->client_index);
84 if (!regp)
85 return;
86
87 if (am->msg_range_by_name == 0)
88 goto out;
Ole Troan7adaa222019-08-27 15:05:27 +020089 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
90 name[ARRAY_LEN (name) - 1] = '\0';
Florin Corase86a8ed2018-01-05 03:20:25 -080091 p = hash_get_mem (am->msg_range_by_name, name);
92 if (p == 0)
93 goto out;
94
95 rp = vec_elt_at_index (am->msg_ranges, p[0]);
96 first_msg_id = rp->first_msg_id;
97 rv = 0;
98
99out:
100 rmp = vl_msg_api_alloc (sizeof (*rmp));
101 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
102 rmp->context = mp->context;
103 rmp->retval = ntohl (rv);
104 rmp->first_msg_id = ntohs (first_msg_id);
105 vl_api_send_msg (regp, (u8 *) rmp);
106}
107
108void
109vl_api_api_versions_t_handler (vl_api_api_versions_t * mp)
110{
Dave Barach39d69112019-11-27 11:42:13 -0500111 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800112 vl_api_api_versions_reply_t *rmp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800113 vl_api_registration_t *reg;
Florin Corase86a8ed2018-01-05 03:20:25 -0800114 u32 nmsg = vec_len (am->api_version_list);
115 int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
116 int i;
117
Florin Coras6c4dae22018-01-09 06:39:23 -0800118 reg = vl_api_client_index_to_registration (mp->client_index);
119 if (!reg)
Florin Corase86a8ed2018-01-05 03:20:25 -0800120 return;
121
122 rmp = vl_msg_api_alloc (msg_size);
Dave Barachb7b92992018-10-17 10:38:51 -0400123 clib_memset (rmp, 0, msg_size);
Florin Corase86a8ed2018-01-05 03:20:25 -0800124 rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
125
126 /* fill in the message */
127 rmp->context = mp->context;
128 rmp->count = htonl (nmsg);
129
130 for (i = 0; i < nmsg; ++i)
131 {
132 api_version_t *vl = &am->api_version_list[i];
133 rmp->api_versions[i].major = htonl (vl->major);
134 rmp->api_versions[i].minor = htonl (vl->minor);
135 rmp->api_versions[i].patch = htonl (vl->patch);
Ole Troan7adaa222019-08-27 15:05:27 +0200136 strncpy ((char *) rmp->api_versions[i].name, vl->name,
137 ARRAY_LEN (rmp->api_versions[i].name));
138 rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
139 '\0';
Florin Corase86a8ed2018-01-05 03:20:25 -0800140 }
141
Florin Coras6c4dae22018-01-09 06:39:23 -0800142 vl_api_send_msg (reg, (u8 *) rmp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800143}
144
Ole Troan94495f22018-08-02 11:58:12 +0200145#define foreach_vlib_api_msg \
146_(GET_FIRST_MSG_ID, get_first_msg_id) \
Florin Corase86a8ed2018-01-05 03:20:25 -0800147_(API_VERSIONS, api_versions)
148
149/*
150 * vl_api_init
151 */
152static int
153vlib_api_init (void)
154{
155 vl_msg_api_msg_config_t cfg;
156 vl_msg_api_msg_config_t *c = &cfg;
157
Dave Barachb7b92992018-10-17 10:38:51 -0400158 clib_memset (c, 0, sizeof (*c));
Florin Corase86a8ed2018-01-05 03:20:25 -0800159
160#define _(N,n) do { \
161 c->id = VL_API_##N; \
162 c->name = #n; \
163 c->handler = vl_api_##n##_t_handler; \
164 c->cleanup = vl_noop_handler; \
165 c->endian = vl_api_##n##_t_endian; \
166 c->print = vl_api_##n##_t_print; \
167 c->size = sizeof(vl_api_##n##_t); \
168 c->traced = 1; /* trace, so these msgs print */ \
169 c->replay = 0; /* don't replay client create/delete msgs */ \
170 c->message_bounce = 0; /* don't bounce this message */ \
171 vl_msg_api_config(c);} while (0);
172
173 foreach_vlib_api_msg;
174#undef _
175
176 return 0;
177}
178
179u64 vector_rate_histogram[SLEEP_N_BUCKETS];
180
181/*
182 * Callback to send ourselves a plugin numbering-space trace msg
183 */
184static void
185send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
186{
187 vl_api_trace_plugin_msg_ids_t *mp;
Dave Barach39d69112019-11-27 11:42:13 -0500188 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800189 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
190 svm_queue_t *q;
191
192 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400193 clib_memset (mp, 0, sizeof (*mp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800194
195 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
Ole Troan7adaa222019-08-27 15:05:27 +0200196 strncpy ((char *) mp->plugin_name, (char *) name,
197 sizeof (mp->plugin_name) - 1);
Florin Corase86a8ed2018-01-05 03:20:25 -0800198 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
199 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
200
201 q = shmem_hdr->vl_input_queue;
202
203 vl_msg_api_send_shmem (q, (u8 *) & mp);
204}
205
206void
207vl_api_save_msg_table (void)
208{
209 u8 *serialized_message_table;
Dave Barach39d69112019-11-27 11:42:13 -0500210 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800211 u8 *chroot_file;
212 int fd, rv;
213
214 /*
215 * Snapshoot the api message table.
216 */
217 if (strstr ((char *) am->save_msg_table_filename, "..")
218 || index ((char *) am->save_msg_table_filename, '/'))
219 {
220 clib_warning ("illegal save-message-table filename '%s'",
221 am->save_msg_table_filename);
222 return;
223 }
224
225 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
226
227 fd = creat ((char *) chroot_file, 0644);
228
229 if (fd < 0)
230 {
231 clib_unix_warning ("creat");
232 return;
233 }
234
235 serialized_message_table = vl_api_serialize_message_table (am, 0);
236
237 rv = write (fd, serialized_message_table,
238 vec_len (serialized_message_table));
239
240 if (rv != vec_len (serialized_message_table))
241 clib_unix_warning ("write");
242
243 rv = close (fd);
244 if (rv < 0)
245 clib_unix_warning ("close");
246
247 vec_free (chroot_file);
248 vec_free (serialized_message_table);
249}
250
Dave Barach7f475bf2020-05-26 11:19:28 -0400251clib_error_t *vat_builtin_main_init (vlib_main_t * vm) __attribute__ ((weak));
252clib_error_t *
253vat_builtin_main_init (vlib_main_t * vm)
254{
255 return 0;
256}
257
Florin Corase86a8ed2018-01-05 03:20:25 -0800258static uword
259vl_api_clnt_process (vlib_main_t * vm, vlib_node_runtime_t * node,
260 vlib_frame_t * f)
261{
262 int private_segment_rotor = 0, i, rv;
263 vl_socket_args_for_process_t *a;
264 vl_shmem_hdr_t *shm;
265 svm_queue_t *q;
266 clib_error_t *e;
Dave Barach39d69112019-11-27 11:42:13 -0500267 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800268 f64 dead_client_scan_time;
269 f64 sleep_time, start_time;
270 f64 vector_rate;
271 clib_error_t *error;
272 uword event_type;
273 uword *event_data = 0;
274 f64 now;
275
Florin Corase86a8ed2018-01-05 03:20:25 -0800276 if ((error = vl_sock_api_init (vm)))
277 {
278 clib_error_report (error);
279 clib_warning ("socksvr_api_init failed, quitting...");
280 return 0;
281 }
282
283 if ((rv = vlib_api_init ()) < 0)
284 {
285 clib_warning ("vlib_api_init returned %d, quitting...", rv);
286 return 0;
287 }
288
289 shm = am->shmem_hdr;
290 q = shm->vl_input_queue;
291
292 e = vlib_call_init_exit_functions
Dave Barachf8d50682019-05-14 18:01:44 -0400293 (vm, &vm->api_init_function_registrations, 1 /* call_once */ );
Florin Corase86a8ed2018-01-05 03:20:25 -0800294 if (e)
295 clib_error_report (e);
296
Dave Barach7f475bf2020-05-26 11:19:28 -0400297 e = vat_builtin_main_init (vm);
298 if (e)
299 clib_error_report (e);
300
Florin Corase86a8ed2018-01-05 03:20:25 -0800301 sleep_time = 10.0;
302 dead_client_scan_time = vlib_time_now (vm) + 10.0;
303
304 /*
305 * Send plugin message range messages for each plugin we loaded
306 */
307 for (i = 0; i < vec_len (am->msg_ranges); i++)
308 {
309 vl_api_msg_range_t *rp = am->msg_ranges + i;
310 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
311 rp->last_msg_id);
312 }
313
314 /*
315 * Save the api message table snapshot, if configured
316 */
317 if (am->save_msg_table_filename)
318 vl_api_save_msg_table ();
319
320 /* $$$ pay attention to frame size, control CPU usage */
321 while (1)
322 {
323 /*
324 * There's a reason for checking the queue before
325 * sleeping. If the vlib application crashes, it's entirely
326 * possible for a client to enqueue a connect request
327 * during the process restart interval.
328 *
329 * Unless some force of physics causes the new incarnation
330 * of the application to process the request, the client will
331 * sit and wait for Godot...
332 */
Dave Baracha8df85c2019-10-01 13:34:23 -0400333 vector_rate = (f64) vlib_last_vectors_per_main_loop (vm);
Florin Corase86a8ed2018-01-05 03:20:25 -0800334 start_time = vlib_time_now (vm);
335 while (1)
336 {
Dave Barachf6c68d72018-11-01 08:12:52 -0400337 if (vl_mem_api_handle_rpc (vm, node)
338 || vl_mem_api_handle_msg_main (vm, node))
Florin Corase86a8ed2018-01-05 03:20:25 -0800339 {
340 vm->api_queue_nonempty = 0;
341 VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
342 sleep_time = 20.0;
343 break;
344 }
345
346 /* Allow no more than 10us without a pause */
347 if (vlib_time_now (vm) > start_time + 10e-6)
348 {
349 int index = SLEEP_400_US;
350 if (vector_rate > 40.0)
351 sleep_time = 400e-6;
352 else if (vector_rate > 20.0)
353 {
354 index = SLEEP_200_US;
355 sleep_time = 200e-6;
356 }
357 else if (vector_rate >= 1.0)
358 {
359 index = SLEEP_100_US;
360 sleep_time = 100e-6;
361 }
362 else
363 {
364 index = SLEEP_10_US;
365 sleep_time = 10e-6;
366 }
367 vector_rate_histogram[index] += 1;
368 break;
369 }
370 }
371
372 /*
373 * see if we have any private api shared-memory segments
374 * If so, push required context variables, and process
375 * a message.
376 */
377 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
378 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800379 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
380 private_segment_rotor = 0;
Florin Corasec54e112018-08-16 15:45:34 -0700381 vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
Florin Corase86a8ed2018-01-05 03:20:25 -0800382 }
383
384 vlib_process_wait_for_event_or_clock (vm, sleep_time);
385 vec_reset_length (event_data);
386 event_type = vlib_process_get_events (vm, &event_data);
387 now = vlib_time_now (vm);
388
389 switch (event_type)
390 {
391 case QUEUE_SIGNAL_EVENT:
392 vm->queue_signal_pending = 0;
393 VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);
394
395 break;
396 case SOCKET_READ_EVENT:
397 for (i = 0; i < vec_len (event_data); i++)
398 {
Florin Coras5224b5c2019-12-06 17:05:08 -0800399 vl_api_registration_t *regp;
400
Florin Corase86a8ed2018-01-05 03:20:25 -0800401 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
Florin Coras5224b5c2019-12-06 17:05:08 -0800402 regp = vl_socket_get_registration (a->reg_index);
403 if (regp)
404 {
405 vl_socket_process_api_msg (regp, (i8 *) a->data);
406 a = pool_elt_at_index (socket_main.process_args,
407 event_data[i]);
408 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800409 vec_free (a->data);
410 pool_put (socket_main.process_args, a);
411 }
412 break;
413
414 /* Timeout... */
415 case -1:
416 break;
417
418 default:
419 clib_warning ("unknown event type %d", event_type);
420 break;
421 }
422
423 if (now > dead_client_scan_time)
424 {
425 vl_mem_api_dead_client_scan (am, shm, now);
426 dead_client_scan_time = vlib_time_now (vm) + 10.0;
427 }
428 }
429
430 return 0;
431}
432/* *INDENT-OFF* */
433VLIB_REGISTER_NODE (vl_api_clnt_node) =
434{
435 .function = vl_api_clnt_process,
436 .type = VLIB_NODE_TYPE_PROCESS,
437 .name = "api-rx-from-ring",
438 .state = VLIB_NODE_STATE_DISABLED,
Chenmin Suna488f372020-01-14 08:47:22 +0800439 .process_log2_n_stack_bytes = 18,
Florin Corase86a8ed2018-01-05 03:20:25 -0800440};
441/* *INDENT-ON* */
442
443void
444vl_mem_api_enable_disable (vlib_main_t * vm, int enable)
445{
446 vlib_node_set_state (vm, vl_api_clnt_node.index,
447 (enable
448 ? VLIB_NODE_STATE_POLLING
449 : VLIB_NODE_STATE_DISABLED));
450}
451
452static uword
453api_rx_from_node (vlib_main_t * vm,
454 vlib_node_runtime_t * node, vlib_frame_t * frame)
455{
456 uword n_packets = frame->n_vectors;
457 uword n_left_from;
458 u32 *from;
459 static u8 *long_msg;
460
461 vec_validate (long_msg, 4095);
462 n_left_from = frame->n_vectors;
Damjan Mariona3d59862018-11-10 10:23:00 +0100463 from = vlib_frame_vector_args (frame);
Florin Corase86a8ed2018-01-05 03:20:25 -0800464
465 while (n_left_from > 0)
466 {
467 u32 bi0;
468 vlib_buffer_t *b0;
469 void *msg;
470 uword msg_len;
471
472 bi0 = from[0];
473 b0 = vlib_get_buffer (vm, bi0);
474 from += 1;
475 n_left_from -= 1;
476
477 msg = b0->data + b0->current_data;
478 msg_len = b0->current_length;
479 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
480 {
481 ASSERT (long_msg != 0);
482 _vec_len (long_msg) = 0;
483 vec_add (long_msg, msg, msg_len);
484 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
485 {
486 b0 = vlib_get_buffer (vm, b0->next_buffer);
487 msg = b0->data + b0->current_data;
488 msg_len = b0->current_length;
489 vec_add (long_msg, msg, msg_len);
490 }
491 msg = long_msg;
492 }
493 vl_msg_api_handler_no_trace_no_free (msg);
494 }
495
496 /* Free what we've been given. */
Damjan Mariona3d59862018-11-10 10:23:00 +0100497 vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_packets);
Florin Corase86a8ed2018-01-05 03:20:25 -0800498
499 return n_packets;
500}
501
502/* *INDENT-OFF* */
503VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
504 .function = api_rx_from_node,
505 .type = VLIB_NODE_TYPE_INTERNAL,
506 .vector_size = 4,
507 .name = "api-rx-from-node",
508};
509/* *INDENT-ON* */
510
511static void
512vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
513{
514 vl_api_rpc_call_reply_t *rmp;
515 int (*fp) (void *);
516 i32 rv = 0;
517 vlib_main_t *vm = vlib_get_main ();
518
519 if (mp->function == 0)
520 {
521 rv = -1;
522 clib_warning ("rpc NULL function pointer");
523 }
524
525 else
526 {
527 if (mp->need_barrier_sync)
528 vlib_worker_thread_barrier_sync (vm);
529
530 fp = uword_to_pointer (mp->function, int (*)(void *));
531 rv = fp (mp->data);
532
533 if (mp->need_barrier_sync)
534 vlib_worker_thread_barrier_release (vm);
535 }
536
537 if (mp->send_reply)
538 {
539 svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
540 if (q)
541 {
542 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
543 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
544 rmp->context = mp->context;
545 rmp->retval = rv;
546 vl_msg_api_send_shmem (q, (u8 *) & rmp);
547 }
548 }
549 if (mp->multicast)
550 {
551 clib_warning ("multicast not yet implemented...");
552 }
553}
554
555static void
556vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
557{
558 clib_warning ("unimplemented");
559}
560
561void
562vl_api_send_pending_rpc_requests (vlib_main_t * vm)
563{
Dave Barachf6c68d72018-11-01 08:12:52 -0400564 vlib_main_t *vm_global = &vlib_global_main;
Florin Corase86a8ed2018-01-05 03:20:25 -0800565
Dave Barachf6c68d72018-11-01 08:12:52 -0400566 ASSERT (vm != vm_global);
Florin Corase86a8ed2018-01-05 03:20:25 -0800567
Dave Barachf6c68d72018-11-01 08:12:52 -0400568 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
569 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
570 vec_reset_length (vm->pending_rpc_requests);
571 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
Florin Corase86a8ed2018-01-05 03:20:25 -0800572}
573
574always_inline void
575vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
576 u8 force_rpc)
577{
578 vl_api_rpc_call_t *mp;
Dave Barachf6c68d72018-11-01 08:12:52 -0400579 vlib_main_t *vm_global = &vlib_global_main;
Florin Corase86a8ed2018-01-05 03:20:25 -0800580 vlib_main_t *vm = vlib_get_main ();
581
582 /* Main thread and not a forced RPC: call the function directly */
583 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
584 {
585 void (*call_fp) (void *);
586
587 vlib_worker_thread_barrier_sync (vm);
588
589 call_fp = fp;
590 call_fp (data);
591
592 vlib_worker_thread_barrier_release (vm);
593 return;
594 }
595
596 /* Otherwise, actually do an RPC */
597 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
598
Dave Barachb7b92992018-10-17 10:38:51 -0400599 clib_memset (mp, 0, sizeof (*mp));
Dave Barach178cf492018-11-13 16:34:13 -0500600 clib_memcpy_fast (mp->data, data, data_length);
Florin Corase86a8ed2018-01-05 03:20:25 -0800601 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
602 mp->function = pointer_to_uword (fp);
603 mp->need_barrier_sync = 1;
604
Dave Barachf6c68d72018-11-01 08:12:52 -0400605 /* Add to the pending vector. Thread 0 requires locking. */
606 if (vm == vm_global)
607 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
Florin Corase86a8ed2018-01-05 03:20:25 -0800608 vec_add1 (vm->pending_rpc_requests, (uword) mp);
Dave Barachf6c68d72018-11-01 08:12:52 -0400609 if (vm == vm_global)
610 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
Florin Corase86a8ed2018-01-05 03:20:25 -0800611}
612
613/*
614 * Check if called from worker threads.
615 * If so, make rpc call of fp through shmem.
616 * Otherwise, call fp directly
617 */
618void
619vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
620{
621 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
622 0);
623}
624
625/*
626 * Always make rpc call of fp through shmem, useful for calling from threads
627 * not setup as worker threads, such as DPDK callback thread
628 */
629void
630vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
631{
632 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
633 1);
634}
635
636static void
637vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
638{
Dave Barach39d69112019-11-27 11:42:13 -0500639 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800640 vl_api_msg_range_t *rp;
641 uword *p;
642
643 /* Noop (except for tracing) during normal operation */
644 if (am->replay_in_progress == 0)
645 return;
646
Ole Troan7adaa222019-08-27 15:05:27 +0200647 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800648 if (p == 0)
649 {
650 clib_warning ("WARNING: traced plugin '%s' not in current image",
Ole Troan7adaa222019-08-27 15:05:27 +0200651 mp->plugin_name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800652 return;
653 }
654
655 rp = vec_elt_at_index (am->msg_ranges, p[0]);
656 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
657 {
658 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
Ole Troan7adaa222019-08-27 15:05:27 +0200659 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
Florin Corase86a8ed2018-01-05 03:20:25 -0800660 rp->first_msg_id);
661 }
662
663 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
664 {
665 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
Ole Troan7adaa222019-08-27 15:05:27 +0200666 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
Florin Corase86a8ed2018-01-05 03:20:25 -0800667 rp->last_msg_id);
668 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800669}
670
671#define foreach_rpc_api_msg \
672_(RPC_CALL,rpc_call) \
673_(RPC_CALL_REPLY,rpc_call_reply)
674
675#define foreach_plugin_trace_msg \
676_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
677
678/*
679 * Set the rpc callback at our earliest possible convenience.
680 * This avoids ordering issues between thread_init() -> start_workers and
681 * an init function which we could define here. If we ever intend to use
682 * vlib all by itself, we can't create a link-time dependency on
683 * an init function here and a typical "call foo_init first"
684 * guitar lick.
685 */
686
687extern void *rpc_call_main_thread_cb_fn;
688
689static clib_error_t *
690rpc_api_hookup (vlib_main_t * vm)
691{
Dave Barach39d69112019-11-27 11:42:13 -0500692 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800693#define _(N,n) \
694 vl_msg_api_set_handlers(VL_API_##N, #n, \
695 vl_api_##n##_t_handler, \
696 vl_noop_handler, \
697 vl_noop_handler, \
698 vl_api_##n##_t_print, \
699 sizeof(vl_api_##n##_t), 0 /* do not trace */);
700 foreach_rpc_api_msg;
701#undef _
702
703#define _(N,n) \
704 vl_msg_api_set_handlers(VL_API_##N, #n, \
705 vl_api_##n##_t_handler, \
706 vl_noop_handler, \
707 vl_noop_handler, \
708 vl_api_##n##_t_print, \
709 sizeof(vl_api_##n##_t), 1 /* do trace */);
710 foreach_plugin_trace_msg;
711#undef _
712
713 /* No reason to halt the parade to create a trace record... */
714 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
715 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
716 return 0;
717}
718
719VLIB_API_INIT_FUNCTION (rpc_api_hookup);
720
721/*
722 * fd.io coding-style-patch-verification: ON
723 *
724 * Local Variables:
725 * eval: (c-set-style "gnu")
726 * End:
727 */