2 *------------------------------------------------------------------
3 * vlib_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>
56 vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
59 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
61 clib_host_to_net_u16 (a->first_msg_id),
62 clib_host_to_net_u16 (a->last_msg_id));
66 /* instantiate all the endian swap functions we know about */
68 #include <vlibmemory/vl_memory_api_h.h>
72 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
74 serialize_main_t _sm, *sm = &_sm;
76 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
78 serialize_open_vector (sm, vector);
80 /* serialize the count */
81 serialize_integer (sm, nmsg, sizeof (u32));
84 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
86 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
87 serialize_cstring (sm, (char *) hp->key);
91 return serialize_close_vector (sm);
95 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
97 vl_api_get_first_msg_id_reply_t *rmp;
98 vl_api_registration_t *regp;
100 api_main_t *am = &api_main;
101 vl_api_msg_range_t *rp;
103 u16 first_msg_id = ~0;
104 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
106 regp = vl_api_client_index_to_registration (mp->client_index);
110 if (am->msg_range_by_name == 0)
112 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
113 name[ARRAY_LEN (name) - 1] = '\0';
114 p = hash_get_mem (am->msg_range_by_name, name);
118 rp = vec_elt_at_index (am->msg_ranges, p[0]);
119 first_msg_id = rp->first_msg_id;
123 rmp = vl_msg_api_alloc (sizeof (*rmp));
124 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
125 rmp->context = mp->context;
126 rmp->retval = ntohl (rv);
127 rmp->first_msg_id = ntohs (first_msg_id);
128 vl_api_send_msg (regp, (u8 *) rmp);
132 vl_api_api_versions_t_handler (vl_api_api_versions_t * mp)
134 api_main_t *am = &api_main;
135 vl_api_api_versions_reply_t *rmp;
136 vl_api_registration_t *reg;
137 u32 nmsg = vec_len (am->api_version_list);
138 int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
141 reg = vl_api_client_index_to_registration (mp->client_index);
145 rmp = vl_msg_api_alloc (msg_size);
146 clib_memset (rmp, 0, msg_size);
147 rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
149 /* fill in the message */
150 rmp->context = mp->context;
151 rmp->count = htonl (nmsg);
153 for (i = 0; i < nmsg; ++i)
155 api_version_t *vl = &am->api_version_list[i];
156 rmp->api_versions[i].major = htonl (vl->major);
157 rmp->api_versions[i].minor = htonl (vl->minor);
158 rmp->api_versions[i].patch = htonl (vl->patch);
159 strncpy ((char *) rmp->api_versions[i].name, vl->name,
160 ARRAY_LEN (rmp->api_versions[i].name));
161 rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
165 vl_api_send_msg (reg, (u8 *) rmp);
168 #define foreach_vlib_api_msg \
169 _(GET_FIRST_MSG_ID, get_first_msg_id) \
170 _(API_VERSIONS, api_versions)
178 vl_msg_api_msg_config_t cfg;
179 vl_msg_api_msg_config_t *c = &cfg;
181 clib_memset (c, 0, sizeof (*c));
183 #define _(N,n) do { \
184 c->id = VL_API_##N; \
186 c->handler = vl_api_##n##_t_handler; \
187 c->cleanup = vl_noop_handler; \
188 c->endian = vl_api_##n##_t_endian; \
189 c->print = vl_api_##n##_t_print; \
190 c->size = sizeof(vl_api_##n##_t); \
191 c->traced = 1; /* trace, so these msgs print */ \
192 c->replay = 0; /* don't replay client create/delete msgs */ \
193 c->message_bounce = 0; /* don't bounce this message */ \
194 vl_msg_api_config(c);} while (0);
196 foreach_vlib_api_msg;
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 = &api_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 = &api_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);
260 rv = write (fd, serialized_message_table,
261 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);
275 vl_api_clnt_process (vlib_main_t * vm, vlib_node_runtime_t * node,
278 int private_segment_rotor = 0, i, rv;
279 vl_socket_args_for_process_t *a;
283 api_main_t *am = &api_main;
284 f64 dead_client_scan_time;
285 f64 sleep_time, start_time;
289 uword *event_data = 0;
292 if ((error = vl_sock_api_init (vm)))
294 clib_error_report (error);
295 clib_warning ("socksvr_api_init failed, quitting...");
299 if ((rv = vlib_api_init ()) < 0)
301 clib_warning ("vlib_api_init returned %d, quitting...", rv);
306 q = shm->vl_input_queue;
308 e = vlib_call_init_exit_functions
309 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
311 clib_error_report (e);
314 dead_client_scan_time = vlib_time_now (vm) + 10.0;
317 * Send plugin message range messages for each plugin we loaded
319 for (i = 0; i < vec_len (am->msg_ranges); i++)
321 vl_api_msg_range_t *rp = am->msg_ranges + i;
322 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
327 * Save the api message table snapshot, if configured
329 if (am->save_msg_table_filename)
330 vl_api_save_msg_table ();
332 /* $$$ pay attention to frame size, control CPU usage */
336 * There's a reason for checking the queue before
337 * sleeping. If the vlib application crashes, it's entirely
338 * possible for a client to enqueue a connect request
339 * during the process restart interval.
341 * Unless some force of physics causes the new incarnation
342 * of the application to process the request, the client will
343 * sit and wait for Godot...
345 vector_rate = vlib_last_vector_length_per_node (vm);
346 start_time = vlib_time_now (vm);
349 if (vl_mem_api_handle_rpc (vm, node)
350 || vl_mem_api_handle_msg_main (vm, node))
352 vm->api_queue_nonempty = 0;
353 VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
358 /* Allow no more than 10us without a pause */
359 if (vlib_time_now (vm) > start_time + 10e-6)
361 int index = SLEEP_400_US;
362 if (vector_rate > 40.0)
364 else if (vector_rate > 20.0)
366 index = SLEEP_200_US;
369 else if (vector_rate >= 1.0)
371 index = SLEEP_100_US;
379 vector_rate_histogram[index] += 1;
385 * see if we have any private api shared-memory segments
386 * If so, push required context variables, and process
389 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
391 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
392 private_segment_rotor = 0;
393 vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
396 vlib_process_wait_for_event_or_clock (vm, sleep_time);
397 vec_reset_length (event_data);
398 event_type = vlib_process_get_events (vm, &event_data);
399 now = vlib_time_now (vm);
403 case QUEUE_SIGNAL_EVENT:
404 vm->queue_signal_pending = 0;
405 VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);
408 case SOCKET_READ_EVENT:
409 for (i = 0; i < vec_len (event_data); i++)
411 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
412 vl_socket_process_api_msg (a->clib_file, a->regp,
415 pool_put (socket_main.process_args, a);
424 clib_warning ("unknown event type %d", event_type);
428 if (now > dead_client_scan_time)
430 vl_mem_api_dead_client_scan (am, shm, now);
431 dead_client_scan_time = vlib_time_now (vm) + 10.0;
438 VLIB_REGISTER_NODE (vl_api_clnt_node) =
440 .function = vl_api_clnt_process,
441 .type = VLIB_NODE_TYPE_PROCESS,
442 .name = "api-rx-from-ring",
443 .state = VLIB_NODE_STATE_DISABLED,
448 vl_mem_api_enable_disable (vlib_main_t * vm, int enable)
450 vlib_node_set_state (vm, vl_api_clnt_node.index,
452 ? VLIB_NODE_STATE_POLLING
453 : VLIB_NODE_STATE_DISABLED));
457 api_rx_from_node (vlib_main_t * vm,
458 vlib_node_runtime_t * node, vlib_frame_t * frame)
460 uword n_packets = frame->n_vectors;
465 vec_validate (long_msg, 4095);
466 n_left_from = frame->n_vectors;
467 from = vlib_frame_args (frame);
469 while (n_left_from > 0)
477 b0 = vlib_get_buffer (vm, bi0);
481 msg = b0->data + b0->current_data;
482 msg_len = b0->current_length;
483 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
485 ASSERT (long_msg != 0);
486 _vec_len (long_msg) = 0;
487 vec_add (long_msg, msg, msg_len);
488 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
490 b0 = vlib_get_buffer (vm, b0->next_buffer);
491 msg = b0->data + b0->current_data;
492 msg_len = b0->current_length;
493 vec_add (long_msg, msg, msg_len);
497 vl_msg_api_handler_no_trace_no_free (msg);
500 /* Free what we've been given. */
501 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
507 VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
508 .function = api_rx_from_node,
509 .type = VLIB_NODE_TYPE_INTERNAL,
511 .name = "api-rx-from-node",
516 vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
518 vl_api_rpc_call_reply_t *rmp;
521 vlib_main_t *vm = vlib_get_main ();
523 if (mp->function == 0)
526 clib_warning ("rpc NULL function pointer");
531 if (mp->need_barrier_sync)
532 vlib_worker_thread_barrier_sync (vm);
534 fp = uword_to_pointer (mp->function, int (*)(void *));
537 if (mp->need_barrier_sync)
538 vlib_worker_thread_barrier_release (vm);
543 svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
546 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
547 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
548 rmp->context = mp->context;
550 vl_msg_api_send_shmem (q, (u8 *) & rmp);
555 clib_warning ("multicast not yet implemented...");
560 vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
562 clib_warning ("unimplemented");
566 vl_api_send_pending_rpc_requests (vlib_main_t * vm)
568 vlib_main_t *vm_global = &vlib_global_main;
570 ASSERT (vm != vm_global);
572 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
573 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
574 vec_reset_length (vm->pending_rpc_requests);
575 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
579 vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
582 vl_api_rpc_call_t *mp;
583 vlib_main_t *vm_global = &vlib_global_main;
584 vlib_main_t *vm = vlib_get_main ();
586 /* Main thread and not a forced RPC: call the function directly */
587 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
589 void (*call_fp) (void *);
591 vlib_worker_thread_barrier_sync (vm);
596 vlib_worker_thread_barrier_release (vm);
600 /* Otherwise, actually do an RPC */
601 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
603 clib_memset (mp, 0, sizeof (*mp));
604 clib_memcpy (mp->data, data, data_length);
605 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
606 mp->function = pointer_to_uword (fp);
607 mp->need_barrier_sync = 1;
609 /* Add to the pending vector. Thread 0 requires locking. */
611 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
612 vec_add1 (vm->pending_rpc_requests, (uword) mp);
614 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
618 * Check if called from worker threads.
619 * If so, make rpc call of fp through shmem.
620 * Otherwise, call fp directly
623 vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
625 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
630 * Always make rpc call of fp through shmem, useful for calling from threads
631 * not setup as worker threads, such as DPDK callback thread
634 vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
636 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
641 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
643 api_main_t *am = &api_main;
644 vl_api_msg_range_t *rp;
647 /* Noop (except for tracing) during normal operation */
648 if (am->replay_in_progress == 0)
651 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
654 clib_warning ("WARNING: traced plugin '%s' not in current image",
659 rp = vec_elt_at_index (am->msg_ranges, p[0]);
660 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
662 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
663 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
667 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
669 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
670 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
675 #define foreach_rpc_api_msg \
676 _(RPC_CALL,rpc_call) \
677 _(RPC_CALL_REPLY,rpc_call_reply)
679 #define foreach_plugin_trace_msg \
680 _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
683 * Set the rpc callback at our earliest possible convenience.
684 * This avoids ordering issues between thread_init() -> start_workers and
685 * an init function which we could define here. If we ever intend to use
686 * vlib all by itself, we can't create a link-time dependency on
687 * an init function here and a typical "call foo_init first"
691 extern void *rpc_call_main_thread_cb_fn;
693 static clib_error_t *
694 rpc_api_hookup (vlib_main_t * vm)
696 api_main_t *am = &api_main;
698 vl_msg_api_set_handlers(VL_API_##N, #n, \
699 vl_api_##n##_t_handler, \
702 vl_api_##n##_t_print, \
703 sizeof(vl_api_##n##_t), 0 /* do not trace */);
708 vl_msg_api_set_handlers(VL_API_##N, #n, \
709 vl_api_##n##_t_handler, \
712 vl_api_##n##_t_print, \
713 sizeof(vl_api_##n##_t), 1 /* do trace */);
714 foreach_plugin_trace_msg;
717 /* No reason to halt the parade to create a trace record... */
718 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
719 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
723 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
726 * fd.io coding-style-patch-verification: ON
729 * eval: (c-set-style "gnu")