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>
35 * @brief Binary API messaging via shared memory
36 * Low-level, primary provisioning interface
38 /*? %%clicmd:group_label Binary API CLI %% ?*/
39 /*? %%syscfg:group_label Binary API configuration %% ?*/
41 #define TRACE_VLIB_MEMORY_QUEUE 0
43 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
45 #define vl_typedefs /* define message structures */
46 #include <vlibmemory/vl_memory_api_h.h>
49 /* instantiate all the print functions we know about */
50 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
52 #include <vlibmemory/vl_memory_api_h.h>
55 /* instantiate all the endian swap functions we know about */
57 #include <vlibmemory/vl_memory_api_h.h>
61 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t *mp)
63 vl_api_get_first_msg_id_reply_t *rmp;
64 vl_api_registration_t *regp;
66 api_main_t *am = vlibapi_get_main ();
67 vl_api_msg_range_t *rp;
69 u16 first_msg_id = ~0;
70 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
72 regp = vl_api_client_index_to_registration (mp->client_index);
76 if (am->msg_range_by_name == 0)
78 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
79 name[ARRAY_LEN (name) - 1] = '\0';
80 p = hash_get_mem (am->msg_range_by_name, name);
84 rp = vec_elt_at_index (am->msg_ranges, p[0]);
85 first_msg_id = rp->first_msg_id;
89 rmp = vl_msg_api_alloc (sizeof (*rmp));
90 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
91 rmp->context = mp->context;
92 rmp->retval = ntohl (rv);
93 rmp->first_msg_id = ntohs (first_msg_id);
94 vl_api_send_msg (regp, (u8 *) rmp);
98 vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
100 api_main_t *am = vlibapi_get_main ();
101 vl_api_api_versions_reply_t *rmp;
102 vl_api_registration_t *reg;
103 u32 nmsg = vec_len (am->api_version_list);
104 int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
107 reg = vl_api_client_index_to_registration (mp->client_index);
111 rmp = vl_msg_api_alloc (msg_size);
112 clib_memset (rmp, 0, msg_size);
113 rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
115 /* fill in the message */
116 rmp->context = mp->context;
117 rmp->count = htonl (nmsg);
119 for (i = 0; i < nmsg; ++i)
121 api_version_t *vl = &am->api_version_list[i];
122 rmp->api_versions[i].major = htonl (vl->major);
123 rmp->api_versions[i].minor = htonl (vl->minor);
124 rmp->api_versions[i].patch = htonl (vl->patch);
125 strncpy ((char *) rmp->api_versions[i].name, vl->name,
126 ARRAY_LEN (rmp->api_versions[i].name));
127 rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
131 vl_api_send_msg (reg, (u8 *) rmp);
134 #define foreach_vlib_api_msg \
135 _ (GET_FIRST_MSG_ID, get_first_msg_id) \
136 _ (API_VERSIONS, api_versions)
144 vl_msg_api_msg_config_t cfg;
145 vl_msg_api_msg_config_t *c = &cfg;
147 cJSON_Hooks cjson_hooks = {
148 .malloc_fn = clib_mem_alloc,
149 .free_fn = clib_mem_free,
151 cJSON_InitHooks (&cjson_hooks);
153 clib_memset (c, 0, sizeof (*c));
158 c->id = VL_API_##N; \
160 c->handler = vl_api_##n##_t_handler; \
161 c->cleanup = vl_noop_handler; \
162 c->endian = vl_api_##n##_t_endian; \
163 c->print = vl_api_##n##_t_print; \
164 c->print_json = vl_api_##n##_t_print_json; \
165 c->tojson = vl_api_##n##_t_tojson; \
166 c->fromjson = vl_api_##n##_t_fromjson; \
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); \
175 foreach_vlib_api_msg;
181 u64 vector_rate_histogram[SLEEP_N_BUCKETS];
184 * Callback to send ourselves a plugin numbering-space trace msg
187 send_one_plugin_msg_ids_msg (u8 *name, u16 first_msg_id, u16 last_msg_id)
189 vl_api_trace_plugin_msg_ids_t *mp;
190 api_main_t *am = vlibapi_get_main ();
191 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
194 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
195 clib_memset (mp, 0, sizeof (*mp));
197 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
198 strncpy ((char *) mp->plugin_name, (char *) name,
199 sizeof (mp->plugin_name) - 1);
200 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
201 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
203 q = shmem_hdr->vl_input_queue;
205 vl_msg_api_send_shmem (q, (u8 *) &mp);
209 vl_api_save_msg_table (void)
211 u8 *serialized_message_table;
212 api_main_t *am = vlibapi_get_main ();
217 * Snapshoot the api message table.
219 if (strstr ((char *) am->save_msg_table_filename, "..") ||
220 index ((char *) am->save_msg_table_filename, '/'))
222 clib_warning ("illegal save-message-table filename '%s'",
223 am->save_msg_table_filename);
227 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
229 fd = creat ((char *) chroot_file, 0644);
233 clib_unix_warning ("creat");
237 serialized_message_table = vl_api_serialize_message_table (am, 0);
240 write (fd, serialized_message_table, vec_len (serialized_message_table));
242 if (rv != vec_len (serialized_message_table))
243 clib_unix_warning ("write");
247 clib_unix_warning ("close");
249 vec_free (chroot_file);
250 vec_free (serialized_message_table);
253 clib_error_t *vat_builtin_main_init (vlib_main_t *vm) __attribute__ ((weak));
255 vat_builtin_main_init (vlib_main_t *vm)
261 vl_api_clnt_process (vlib_main_t *vm, vlib_node_runtime_t *node,
264 vlib_global_main_t *vgm = vlib_get_global_main ();
265 int private_segment_rotor = 0, i, rv;
266 vl_socket_args_for_process_t *a;
270 api_main_t *am = vlibapi_get_main ();
271 f64 dead_client_scan_time;
272 f64 sleep_time, start_time;
276 uword *event_data = 0;
279 if ((error = vl_sock_api_init (vm)))
281 clib_error_report (error);
282 clib_warning ("socksvr_api_init failed, quitting...");
286 if ((rv = vlib_api_init ()) < 0)
288 clib_warning ("vlib_api_init returned %d, quitting...", rv);
293 q = shm->vl_input_queue;
295 e = vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations,
296 1 /* call_once */, 1 /* is_global */);
298 clib_error_report (e);
300 e = vat_builtin_main_init (vm);
302 clib_error_report (e);
305 dead_client_scan_time = vlib_time_now (vm) + 10.0;
308 * Send plugin message range messages for each plugin we loaded
310 for (i = 0; i < vec_len (am->msg_ranges); i++)
312 vl_api_msg_range_t *rp = am->msg_ranges + i;
313 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
318 * Save the api message table snapshot, if configured
320 if (am->save_msg_table_filename)
321 vl_api_save_msg_table ();
323 /* $$$ pay attention to frame size, control CPU usage */
327 * There's a reason for checking the queue before
328 * sleeping. If the vlib application crashes, it's entirely
329 * possible for a client to enqueue a connect request
330 * during the process restart interval.
332 * Unless some force of physics causes the new incarnation
333 * of the application to process the request, the client will
334 * sit and wait for Godot...
336 vector_rate = (f64) vlib_last_vectors_per_main_loop (vm);
337 start_time = vlib_time_now (vm);
340 if (vl_mem_api_handle_rpc (vm, node) ||
341 vl_mem_api_handle_msg_main (vm, node))
343 vm->api_queue_nonempty = 0;
344 VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
349 /* Allow no more than 10us without a pause */
350 if (vlib_time_now (vm) > start_time + 10e-6)
352 int index = SLEEP_400_US;
353 if (vector_rate > 40.0)
355 else if (vector_rate > 20.0)
357 index = SLEEP_200_US;
360 else if (vector_rate >= 1.0)
362 index = SLEEP_100_US;
370 vector_rate_histogram[index] += 1;
376 * see if we have any private api shared-memory segments
377 * If so, push required context variables, and process
380 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
382 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
383 private_segment_rotor = 0;
384 vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
387 vlib_process_wait_for_event_or_clock (vm, sleep_time);
388 vec_reset_length (event_data);
389 event_type = vlib_process_get_events (vm, &event_data);
390 now = vlib_time_now (vm);
394 case QUEUE_SIGNAL_EVENT:
395 vm->queue_signal_pending = 0;
396 VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);
399 case SOCKET_READ_EVENT:
400 for (i = 0; i < vec_len (event_data); i++)
402 vl_api_registration_t *regp;
404 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
405 regp = vl_socket_get_registration (a->reg_index);
408 vl_socket_process_api_msg (regp, (i8 *) a->data);
409 a = pool_elt_at_index (socket_main.process_args,
413 pool_put (socket_main.process_args, a);
422 clib_warning ("unknown event type %d", event_type);
426 if (now > dead_client_scan_time)
428 vl_mem_api_dead_client_scan (am, shm, now);
429 dead_client_scan_time = vlib_time_now (vm) + 10.0;
436 VLIB_REGISTER_NODE (vl_api_clnt_node) = {
437 .function = vl_api_clnt_process,
438 .type = VLIB_NODE_TYPE_PROCESS,
439 .name = "api-rx-from-ring",
440 .state = VLIB_NODE_STATE_DISABLED,
441 .process_log2_n_stack_bytes = 18,
445 vl_mem_api_enable_disable (vlib_main_t *vm, int enable)
447 vlib_node_set_state (
448 vm, vl_api_clnt_node.index,
449 (enable ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED));
453 api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
456 uword n_packets = frame->n_vectors;
461 vec_validate (long_msg, 4095);
462 n_left_from = frame->n_vectors;
463 from = vlib_frame_vector_args (frame);
465 while (n_left_from > 0)
473 b0 = vlib_get_buffer (vm, bi0);
477 msg = b0->data + b0->current_data;
478 msg_len = b0->current_length;
479 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
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)
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);
493 vl_msg_api_handler_no_trace_no_free (msg);
496 /* Free what we've been given. */
497 vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_packets);
502 VLIB_REGISTER_NODE (api_rx_from_node_node, static) = {
503 .function = api_rx_from_node,
504 .type = VLIB_NODE_TYPE_INTERNAL,
506 .name = "api-rx-from-node",
510 vl_api_rpc_call_t_handler (vl_api_rpc_call_t *mp)
512 vl_api_rpc_call_reply_t *rmp;
515 vlib_main_t *vm = vlib_get_main ();
517 if (mp->function == 0)
520 clib_warning ("rpc NULL function pointer");
525 if (mp->need_barrier_sync)
526 vlib_worker_thread_barrier_sync (vm);
528 fp = uword_to_pointer (mp->function, int (*) (void *));
531 if (mp->need_barrier_sync)
532 vlib_worker_thread_barrier_release (vm);
537 svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
540 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
541 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
542 rmp->context = mp->context;
544 vl_msg_api_send_shmem (q, (u8 *) &rmp);
549 clib_warning ("multicast not yet implemented...");
554 vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t *mp)
556 clib_warning ("unimplemented");
560 vl_api_send_pending_rpc_requests (vlib_main_t *vm)
562 vlib_main_t *vm_global = vlib_get_first_main ();
564 ASSERT (vm != vm_global);
566 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
567 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
568 vec_reset_length (vm->pending_rpc_requests);
569 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
573 vl_api_rpc_call_main_thread_inline (void *fp, u8 *data, u32 data_length,
576 vl_api_rpc_call_t *mp;
577 vlib_main_t *vm_global = vlib_get_first_main ();
578 vlib_main_t *vm = vlib_get_main ();
580 /* Main thread and not a forced RPC: call the function directly */
581 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
583 void (*call_fp) (void *);
585 vlib_worker_thread_barrier_sync (vm);
590 vlib_worker_thread_barrier_release (vm);
594 /* Otherwise, actually do an RPC */
595 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
597 clib_memset (mp, 0, sizeof (*mp));
598 clib_memcpy_fast (mp->data, data, data_length);
599 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
600 mp->function = pointer_to_uword (fp);
601 mp->need_barrier_sync = 1;
603 /* Add to the pending vector. Thread 0 requires locking. */
605 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
606 vec_add1 (vm->pending_rpc_requests, (uword) mp);
608 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
612 * Check if called from worker threads.
613 * If so, make rpc call of fp through shmem.
614 * Otherwise, call fp directly
617 vl_api_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
619 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
624 * Always make rpc call of fp through shmem, useful for calling from threads
625 * not setup as worker threads, such as DPDK callback thread
628 vl_api_force_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
630 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
635 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t *mp)
637 api_main_t *am = vlibapi_get_main ();
638 vl_api_msg_range_t *rp;
641 /* Noop (except for tracing) during normal operation */
642 if (am->replay_in_progress == 0)
645 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
648 clib_warning ("WARNING: traced plugin '%s' not in current image",
653 rp = vec_elt_at_index (am->msg_ranges, p[0]);
654 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
656 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
657 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
661 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
663 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
664 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
669 #define foreach_rpc_api_msg \
670 _ (RPC_CALL, rpc_call) \
671 _ (RPC_CALL_REPLY, rpc_call_reply)
673 #define foreach_plugin_trace_msg _ (TRACE_PLUGIN_MSG_IDS, trace_plugin_msg_ids)
676 * Set the rpc callback at our earliest possible convenience.
677 * This avoids ordering issues between thread_init() -> start_workers and
678 * an init function which we could define here. If we ever intend to use
679 * vlib all by itself, we can't create a link-time dependency on
680 * an init function here and a typical "call foo_init first"
684 extern void *rpc_call_main_thread_cb_fn;
686 static clib_error_t *
687 rpc_api_hookup (vlib_main_t *vm)
689 api_main_t *am = vlibapi_get_main ();
691 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
692 vl_noop_handler, vl_noop_handler, \
693 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), \
694 0 /* do not trace */, vl_api_##n##_t_print_json, \
695 vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
700 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
701 vl_noop_handler, vl_noop_handler, \
702 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), \
703 1 /* do trace */, vl_api_##n##_t_print_json, \
704 vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
705 foreach_plugin_trace_msg;
708 am->api_trace_cfg[VL_API_TRACE_PLUGIN_MSG_IDS].replay_enable = 0;
710 /* No reason to halt the parade to create a trace record... */
711 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
712 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
716 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
719 * fd.io coding-style-patch-verification: ON
722 * eval: (c-set-style "gnu")