2 *------------------------------------------------------------------
3 * memclnt_api.c VLIB API implementation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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 #include <vlibapi/api_helper_macros.h>
36 * @brief Binary API messaging via shared memory
37 * Low-level, primary provisioning interface
39 /*? %%clicmd:group_label Binary API CLI %% ?*/
40 /*? %%syscfg:group_label Binary API configuration %% ?*/
42 #define TRACE_VLIB_MEMORY_QUEUE 0
44 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
46 #define vl_typedefs /* define message structures */
47 #include <vlibmemory/vl_memory_api_h.h>
50 /* instantiate all the print functions we know about */
51 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
53 #include <vlibmemory/vl_memory_api_h.h>
56 /* instantiate all the endian swap functions we know about */
58 #include <vlibmemory/vl_memory_api_h.h>
61 #define vl_calcsizefun
62 #include <vlibmemory/vl_memory_api_h.h>
66 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t *mp)
68 vl_api_get_first_msg_id_reply_t *rmp;
69 vl_api_registration_t *regp;
71 api_main_t *am = vlibapi_get_main ();
72 vl_api_msg_range_t *rp;
74 u16 first_msg_id = ~0;
75 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
77 regp = vl_api_client_index_to_registration (mp->client_index);
81 if (am->msg_range_by_name == 0)
83 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
84 name[ARRAY_LEN (name) - 1] = '\0';
85 p = hash_get_mem (am->msg_range_by_name, name);
89 rp = vec_elt_at_index (am->msg_ranges, p[0]);
90 first_msg_id = rp->first_msg_id;
94 rmp = vl_msg_api_alloc (sizeof (*rmp));
95 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
96 rmp->context = mp->context;
97 rmp->retval = ntohl (rv);
98 rmp->first_msg_id = ntohs (first_msg_id);
99 vl_api_send_msg (regp, (u8 *) rmp);
103 vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
105 api_main_t *am = vlibapi_get_main ();
106 vl_api_api_versions_reply_t *rmp;
107 vl_api_registration_t *reg;
108 u32 nmsg = vec_len (am->api_version_list);
109 int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
112 reg = vl_api_client_index_to_registration (mp->client_index);
116 rmp = vl_msg_api_alloc (msg_size);
117 clib_memset (rmp, 0, msg_size);
118 rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
120 /* fill in the message */
121 rmp->context = mp->context;
122 rmp->count = htonl (nmsg);
124 for (i = 0; i < nmsg; ++i)
126 api_version_t *vl = &am->api_version_list[i];
127 rmp->api_versions[i].major = htonl (vl->major);
128 rmp->api_versions[i].minor = htonl (vl->minor);
129 rmp->api_versions[i].patch = htonl (vl->patch);
130 strncpy ((char *) rmp->api_versions[i].name, vl->name,
131 ARRAY_LEN (rmp->api_versions[i].name));
132 rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
136 vl_api_send_msg (reg, (u8 *) rmp);
140 vl_api_control_ping_t_handler (vl_api_control_ping_t *mp)
142 vl_api_control_ping_reply_t *rmp;
145 REPLY_MACRO2 (VL_API_CONTROL_PING_REPLY,
146 ({ rmp->vpe_pid = ntohl (getpid ()); }));
149 #define foreach_vlib_api_msg \
150 _ (GET_FIRST_MSG_ID, get_first_msg_id) \
151 _ (API_VERSIONS, api_versions) \
152 _ (CONTROL_PING, control_ping)
160 api_main_t *am = vlibapi_get_main ();
161 vl_msg_api_msg_config_t cfg;
162 vl_msg_api_msg_config_t *c = &cfg;
164 cJSON_Hooks cjson_hooks = {
165 .malloc_fn = clib_mem_alloc,
166 .free_fn = clib_mem_free,
168 cJSON_InitHooks (&cjson_hooks);
170 clib_memset (c, 0, sizeof (*c));
175 c->id = VL_API_##N; \
177 c->handler = vl_api_##n##_t_handler; \
178 c->cleanup = vl_noop_handler; \
179 c->endian = vl_api_##n##_t_endian; \
180 c->print = vl_api_##n##_t_print; \
181 c->print_json = vl_api_##n##_t_print_json; \
182 c->tojson = vl_api_##n##_t_tojson; \
183 c->fromjson = vl_api_##n##_t_fromjson; \
184 c->calc_size = vl_api_##n##_t_calc_size; \
185 c->size = sizeof (vl_api_##n##_t); \
186 c->traced = 1; /* trace, so these msgs print */ \
187 c->replay = 0; /* don't replay client create/delete msgs */ \
188 c->message_bounce = 0; /* don't bounce this message */ \
189 vl_msg_api_config (c); \
193 foreach_vlib_api_msg;
196 vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING, 1);
197 vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING_REPLY, 1);
202 u64 vector_rate_histogram[SLEEP_N_BUCKETS];
205 * Callback to send ourselves a plugin numbering-space trace msg
208 send_one_plugin_msg_ids_msg (u8 *name, u16 first_msg_id, u16 last_msg_id)
210 vl_api_trace_plugin_msg_ids_t *mp;
211 api_main_t *am = vlibapi_get_main ();
212 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
215 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
216 clib_memset (mp, 0, sizeof (*mp));
218 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
219 strncpy ((char *) mp->plugin_name, (char *) name,
220 sizeof (mp->plugin_name) - 1);
221 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
222 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
224 q = shmem_hdr->vl_input_queue;
226 vl_msg_api_send_shmem (q, (u8 *) &mp);
230 vl_api_save_msg_table (void)
232 u8 *serialized_message_table;
233 api_main_t *am = vlibapi_get_main ();
238 * Snapshoot the api message table.
240 if (strstr ((char *) am->save_msg_table_filename, "..") ||
241 index ((char *) am->save_msg_table_filename, '/'))
243 clib_warning ("illegal save-message-table filename '%s'",
244 am->save_msg_table_filename);
248 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
250 fd = creat ((char *) chroot_file, 0644);
254 clib_unix_warning ("creat");
258 serialized_message_table = vl_api_serialize_message_table (am, 0);
261 write (fd, serialized_message_table, vec_len (serialized_message_table));
263 if (rv != vec_len (serialized_message_table))
264 clib_unix_warning ("write");
268 clib_unix_warning ("close");
270 vec_free (chroot_file);
271 vec_free (serialized_message_table);
274 clib_error_t *vat_builtin_main_init (vlib_main_t *vm) __attribute__ ((weak));
276 vat_builtin_main_init (vlib_main_t *vm)
282 vl_api_clnt_process (vlib_main_t *vm, vlib_node_runtime_t *node,
285 vlib_global_main_t *vgm = vlib_get_global_main ();
286 int private_segment_rotor = 0, i, rv;
287 vl_socket_args_for_process_t *a;
291 api_main_t *am = vlibapi_get_main ();
292 f64 dead_client_scan_time;
293 f64 sleep_time, start_time;
297 uword *event_data = 0;
300 if ((error = vl_sock_api_init (vm)))
302 clib_error_report (error);
303 clib_warning ("socksvr_api_init failed, quitting...");
307 if ((rv = vlib_api_init ()) < 0)
309 clib_warning ("vlib_api_init returned %d, quitting...", rv);
314 q = shm->vl_input_queue;
316 e = vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations,
317 1 /* call_once */, 1 /* is_global */);
319 clib_error_report (e);
321 e = vat_builtin_main_init (vm);
323 clib_error_report (e);
326 dead_client_scan_time = vlib_time_now (vm) + 10.0;
329 * Send plugin message range messages for each plugin we loaded
331 for (i = 0; i < vec_len (am->msg_ranges); i++)
333 vl_api_msg_range_t *rp = am->msg_ranges + i;
334 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
339 * Save the api message table snapshot, if configured
341 if (am->save_msg_table_filename)
342 vl_api_save_msg_table ();
344 /* $$$ pay attention to frame size, control CPU usage */
348 * There's a reason for checking the queue before
349 * sleeping. If the vlib application crashes, it's entirely
350 * possible for a client to enqueue a connect request
351 * during the process restart interval.
353 * Unless some force of physics causes the new incarnation
354 * of the application to process the request, the client will
355 * sit and wait for Godot...
357 vector_rate = (f64) vlib_last_vectors_per_main_loop (vm);
358 start_time = vlib_time_now (vm);
361 if (vl_mem_api_handle_rpc (vm, node) ||
362 vl_mem_api_handle_msg_main (vm, node))
364 vm->api_queue_nonempty = 0;
365 VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
370 /* Allow no more than 10us without a pause */
371 if (vlib_time_now (vm) > start_time + 10e-6)
373 int index = SLEEP_400_US;
374 if (vector_rate > 40.0)
376 else if (vector_rate > 20.0)
378 index = SLEEP_200_US;
381 else if (vector_rate >= 1.0)
383 index = SLEEP_100_US;
391 vector_rate_histogram[index] += 1;
397 * see if we have any private api shared-memory segments
398 * If so, push required context variables, and process
401 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
403 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
404 private_segment_rotor = 0;
405 vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
408 vlib_process_wait_for_event_or_clock (vm, sleep_time);
409 vec_reset_length (event_data);
410 event_type = vlib_process_get_events (vm, &event_data);
411 now = vlib_time_now (vm);
415 case QUEUE_SIGNAL_EVENT:
416 vm->queue_signal_pending = 0;
417 VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);
420 case SOCKET_READ_EVENT:
421 for (i = 0; i < vec_len (event_data); i++)
423 vl_api_registration_t *regp;
425 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
426 regp = vl_socket_get_registration (a->reg_index);
429 vl_socket_process_api_msg (regp, (i8 *) a->data);
430 a = pool_elt_at_index (socket_main.process_args,
434 pool_put (socket_main.process_args, a);
443 clib_warning ("unknown event type %d", event_type);
447 if (now > dead_client_scan_time)
449 vl_mem_api_dead_client_scan (am, shm, now);
450 dead_client_scan_time = vlib_time_now (vm) + 10.0;
457 VLIB_REGISTER_NODE (vl_api_clnt_node) = {
458 .function = vl_api_clnt_process,
459 .type = VLIB_NODE_TYPE_PROCESS,
460 .name = "api-rx-from-ring",
461 .state = VLIB_NODE_STATE_DISABLED,
462 .process_log2_n_stack_bytes = 18,
466 vl_mem_api_enable_disable (vlib_main_t *vm, int enable)
468 vlib_node_set_state (
469 vm, vl_api_clnt_node.index,
470 (enable ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED));
474 api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
477 uword n_packets = frame->n_vectors;
482 vec_validate (long_msg, 4095);
483 n_left_from = frame->n_vectors;
484 from = vlib_frame_vector_args (frame);
486 while (n_left_from > 0)
494 b0 = vlib_get_buffer (vm, bi0);
498 msg = b0->data + b0->current_data;
499 msg_len = b0->current_length;
500 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
502 ASSERT (long_msg != 0);
503 vec_set_len (long_msg, 0);
504 vec_add (long_msg, msg, msg_len);
505 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
507 b0 = vlib_get_buffer (vm, b0->next_buffer);
508 msg = b0->data + b0->current_data;
509 msg_len = b0->current_length;
510 vec_add (long_msg, msg, msg_len);
513 msg_len = vec_len (long_msg);
515 vl_msg_api_handler_no_trace_no_free (msg, msg_len);
518 /* Free what we've been given. */
519 vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_packets);
524 VLIB_REGISTER_NODE (api_rx_from_node_node, static) = {
525 .function = api_rx_from_node,
526 .type = VLIB_NODE_TYPE_INTERNAL,
528 .name = "api-rx-from-node",
532 vl_api_rpc_call_t_handler (vl_api_rpc_call_t *mp)
534 vl_api_rpc_call_reply_t *rmp;
537 vlib_main_t *vm = vlib_get_main ();
539 if (mp->function == 0)
542 clib_warning ("rpc NULL function pointer");
547 if (mp->need_barrier_sync)
548 vlib_worker_thread_barrier_sync (vm);
550 fp = uword_to_pointer (mp->function, int (*) (void *));
553 if (mp->need_barrier_sync)
554 vlib_worker_thread_barrier_release (vm);
559 svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
562 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
563 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
564 rmp->context = mp->context;
566 vl_msg_api_send_shmem (q, (u8 *) &rmp);
571 clib_warning ("multicast not yet implemented...");
576 vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t *mp)
578 clib_warning ("unimplemented");
582 vl_api_send_pending_rpc_requests (vlib_main_t *vm)
584 vlib_main_t *vm_global = vlib_get_first_main ();
586 ASSERT (vm != vm_global);
588 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
589 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
590 vec_reset_length (vm->pending_rpc_requests);
591 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
595 vl_api_rpc_call_main_thread_inline (void *fp, u8 *data, u32 data_length,
598 vl_api_rpc_call_t *mp;
599 vlib_main_t *vm_global = vlib_get_first_main ();
600 vlib_main_t *vm = vlib_get_main ();
602 /* Main thread and not a forced RPC: call the function directly */
603 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
605 void (*call_fp) (void *);
607 vlib_worker_thread_barrier_sync (vm);
612 vlib_worker_thread_barrier_release (vm);
616 /* Otherwise, actually do an RPC */
617 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
619 clib_memset (mp, 0, sizeof (*mp));
620 clib_memcpy_fast (mp->data, data, data_length);
621 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
622 mp->function = pointer_to_uword (fp);
623 mp->need_barrier_sync = 1;
625 /* Add to the pending vector. Thread 0 requires locking. */
627 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
628 vec_add1 (vm->pending_rpc_requests, (uword) mp);
630 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
634 * Check if called from worker threads.
635 * If so, make rpc call of fp through shmem.
636 * Otherwise, call fp directly
639 vl_api_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
641 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
646 * Always make rpc call of fp through shmem, useful for calling from threads
647 * not setup as worker threads, such as DPDK callback thread
650 vl_api_force_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
652 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
657 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t *mp)
659 api_main_t *am = vlibapi_get_main ();
660 vl_api_msg_range_t *rp;
663 /* Noop (except for tracing) during normal operation */
664 if (am->replay_in_progress == 0)
667 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
670 clib_warning ("WARNING: traced plugin '%s' not in current image",
675 rp = vec_elt_at_index (am->msg_ranges, p[0]);
676 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
678 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
679 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
683 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
685 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
686 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
691 #define foreach_rpc_api_msg \
692 _ (RPC_CALL, rpc_call) \
693 _ (RPC_CALL_REPLY, rpc_call_reply)
695 #define foreach_plugin_trace_msg _ (TRACE_PLUGIN_MSG_IDS, trace_plugin_msg_ids)
698 * Set the rpc callback at our earliest possible convenience.
699 * This avoids ordering issues between thread_init() -> start_workers and
700 * an init function which we could define here. If we ever intend to use
701 * vlib all by itself, we can't create a link-time dependency on
702 * an init function here and a typical "call foo_init first"
706 extern void *rpc_call_main_thread_cb_fn;
708 static clib_error_t *
709 rpc_api_hookup (vlib_main_t *vm)
711 api_main_t *am = vlibapi_get_main ();
713 vl_msg_api_set_handlers ( \
714 VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
715 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0 /* do not trace */, \
716 vl_api_##n##_t_print_json, vl_api_##n##_t_tojson, \
717 vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
722 vl_msg_api_set_handlers ( \
723 VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
724 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1 /* do trace */, \
725 vl_api_##n##_t_print_json, vl_api_##n##_t_tojson, \
726 vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
727 foreach_plugin_trace_msg;
730 vl_api_allow_msg_replay (am, VL_API_TRACE_PLUGIN_MSG_IDS, 0);
732 /* No reason to halt the parade to create a trace record... */
733 vl_api_set_msg_thread_safe (am, VL_API_TRACE_PLUGIN_MSG_IDS, 1);
734 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
738 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
741 * fd.io coding-style-patch-verification: ON
744 * eval: (c-set-style "gnu")