2 *------------------------------------------------------------------
3 * api.c - message handler registration
5 * Copyright (c) 2010-2018 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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/heap.h>
43 #include <vppinfra/pool.h>
44 #include <vppinfra/format.h>
45 #include <vppinfra/error.h>
47 #include <vnet/api_errno.h>
48 #include <vnet/vnet.h>
51 #include <vlib/vlib.h>
52 #include <vlib/unix/unix.h>
53 #include <vlibapi/api.h>
54 #include <vlibmemory/api.h>
57 #undef __included_bihash_template_h__
59 #include <vnet/ip/format.h>
61 #include <vpp/api/vpe_msg_enum.h>
62 #include <vpp/api/types.h>
63 #include <vnet/classify/classify.api_enum.h>
64 #include <vnet/ip/ip.api_enum.h>
66 #define vl_typedefs /* define message structures */
67 #include <vpp/api/vpe_all_api_h.h>
69 #define vl_endianfun /* define message structures */
70 #include <vpp/api/vpe_all_api_h.h>
72 /* instantiate all the print functions we know about */
73 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
75 #include <vpp/api/vpe_all_api_h.h>
77 #include <vlibapi/api_helper_macros.h>
79 #define foreach_vpe_api_msg \
80 _(CONTROL_PING, control_ping) \
82 _(CLI_INBAND, cli_inband) \
83 _(GET_NODE_INDEX, get_node_index) \
84 _(ADD_NODE_NEXT, add_node_next) \
85 _(SHOW_VERSION, show_version) \
86 _(SHOW_THREADS, show_threads) \
87 _(GET_NODE_GRAPH, get_node_graph) \
88 _(GET_NEXT_INDEX, get_next_index) \
89 _(LOG_DUMP, log_dump) \
90 _(SHOW_VPE_SYSTEM_TIME, show_vpe_system_time) \
91 _(GET_F64_ENDIAN_VALUE, get_f64_endian_value) \
92 _(GET_F64_INCREMENT_BY_ONE, get_f64_increment_by_one) \
95 #define QUOTE(x) QUOTE_(x)
99 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
100 RESOLVE_IP6_ADD_DEL_ROUTE,
103 extern vpe_api_main_t vpe_api_main;
105 /* Clean up all registrations belonging to the indicated client */
106 static clib_error_t *
107 memclnt_delete_callback (u32 client_index)
109 vpe_api_main_t *vam = &vpe_api_main;
110 vpe_client_registration_t *rp;
114 p = hash_get (vam->a##_registration_hash, client_index); \
116 rp = pool_elt_at_index (vam->a##_registrations, p[0]); \
117 pool_put (vam->a##_registrations, rp); \
118 hash_unset (vam->a##_registration_hash, client_index); \
120 foreach_registration_hash;
125 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
128 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
130 vl_api_control_ping_reply_t *rmp;
134 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
136 rmp->vpe_pid = ntohl (getpid());
142 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
144 u8 **shmem_vecp = (u8 **) arg;
149 shmem_vec = *shmem_vecp;
151 offset = vec_len (shmem_vec);
153 oldheap = vl_msg_push_heap ();
155 vec_validate (shmem_vec, offset + buffer_bytes - 1);
157 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
159 vl_msg_pop_heap (oldheap);
161 *shmem_vecp = shmem_vec;
166 vl_api_cli_t_handler (vl_api_cli_t * mp)
168 vl_api_cli_reply_t *rp;
169 vl_api_registration_t *reg;
170 vlib_main_t *vm = vlib_get_main ();
171 unformat_input_t input;
175 reg = vl_api_client_index_to_registration (mp->client_index);
179 rp = vl_msg_api_alloc (sizeof (*rp));
180 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
181 rp->context = mp->context;
183 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
185 vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
187 oldheap = vl_msg_push_heap ();
188 vec_add1 (shmem_vec, 0);
189 vl_msg_pop_heap (oldheap);
191 rp->reply_in_shmem = (uword) shmem_vec;
193 vl_api_send_msg (reg, (u8 *) rp);
197 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
199 u8 **mem_vecp = (u8 **) arg;
200 u8 *mem_vec = *mem_vecp;
201 u32 offset = vec_len (mem_vec);
203 vec_validate (mem_vec, offset + buffer_bytes - 1);
204 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
209 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
211 vl_api_cli_inband_reply_t *rmp;
213 vlib_main_t *vm = vlib_get_main ();
214 unformat_input_t input;
218 if (vl_msg_api_get_msg_length (mp) <
219 vl_api_string_len (&mp->cmd) + sizeof (*mp))
225 cmd_vec = vl_api_from_api_to_new_vec (mp, &mp->cmd);
227 unformat_init_string (&input, (char *) cmd_vec,
228 vl_api_string_len (&mp->cmd));
229 rv = vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
230 unformat_free (&input);
234 REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, vec_len (out_vec),
236 vl_api_vec_to_api_string(out_vec, &rmp->reply);
244 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
246 vl_api_show_version_reply_t *rmp;
248 char *vpe_api_get_build_directory (void);
249 char *vpe_api_get_version (void);
250 char *vpe_api_get_build_date (void);
253 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
255 strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
256 strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
257 ARRAY_LEN(rmp->build_directory)-1);
258 strncpy ((char *) rmp->version, vpe_api_get_version(),
259 ARRAY_LEN(rmp->version)-1);
260 strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
261 ARRAY_LEN(rmp->build_date)-1);
267 get_thread_data (vl_api_thread_data_t * td, int index)
269 vlib_worker_thread_t *w = vlib_worker_threads + index;
270 td->id = htonl (index);
272 strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
274 strncpy ((char *) td->type, (char *) w->registration->name,
275 ARRAY_LEN (td->type) - 1);
276 td->pid = htonl (w->lwp);
277 td->cpu_id = htonl (w->cpu_id);
278 td->core = htonl (w->core_id);
279 td->cpu_socket = htonl (w->numa_id);
283 vl_api_show_threads_t_handler (vl_api_show_threads_t * mp)
287 #if !defined(__powerpc64__)
288 vl_api_registration_t *reg;
289 vl_api_show_threads_reply_t *rmp;
290 vl_api_thread_data_t *td;
292 count = vec_len (vlib_worker_threads);
296 msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
297 reg = vl_api_client_index_to_registration (mp->client_index);
301 rmp = vl_msg_api_alloc (msg_size);
302 clib_memset (rmp, 0, msg_size);
303 rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY);
304 rmp->context = mp->context;
305 rmp->count = htonl (count);
306 td = rmp->thread_data;
308 for (i = 0; i < count; i++)
310 get_thread_data (&td[i], i);
313 vl_api_send_msg (reg, (u8 *) rmp);
316 /* unimplemented support */
318 clib_warning ("power pc does not support show threads api");
320 REPLY_MACRO2(VL_API_SHOW_THREADS_REPLY,
322 rmp->count = htonl(count);
329 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
331 vlib_main_t *vm = vlib_get_main ();
332 vl_api_get_node_index_reply_t *rmp;
337 n = vlib_get_node_by_name (vm, mp->node_name);
340 rv = VNET_API_ERROR_NO_SUCH_NODE;
342 node_index = n->index;
345 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
347 rmp->node_index = htonl(node_index);
353 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
355 vlib_main_t *vm = vlib_get_main ();
356 vl_api_get_next_index_reply_t *rmp;
357 vlib_node_t *node, *next_node;
359 u32 next_node_index = ~0, next_index = ~0;
362 node = vlib_get_node_by_name (vm, mp->node_name);
366 rv = VNET_API_ERROR_NO_SUCH_NODE;
370 next_node = vlib_get_node_by_name (vm, mp->next_name);
374 rv = VNET_API_ERROR_NO_SUCH_NODE2;
378 next_node_index = next_node->index;
380 p = hash_get (node->next_slot_by_node, next_node_index);
384 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
392 REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
394 rmp->next_index = htonl(next_index);
400 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
402 vlib_main_t *vm = vlib_get_main ();
403 vl_api_add_node_next_reply_t *rmp;
404 vlib_node_t *n, *next;
408 n = vlib_get_node_by_name (vm, mp->node_name);
412 rv = VNET_API_ERROR_NO_SUCH_NODE;
416 next = vlib_get_node_by_name (vm, mp->next_name);
419 rv = VNET_API_ERROR_NO_SUCH_NODE2;
421 next_index = vlib_node_add_next (vm, n->index, next->index);
425 REPLY_MACRO2(VL_API_ADD_NODE_NEXT_REPLY,
427 rmp->next_index = htonl(next_index);
433 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
437 vlib_main_t *vm = vlib_get_main ();
439 vl_api_get_node_graph_reply_t *rmp;
440 static vlib_node_t ***node_dups;
441 static vlib_main_t **stat_vms;
443 oldheap = vl_msg_push_heap ();
446 * Keep the number of memcpy ops to a minimum (e.g. 1).
448 vec_validate (vector, 16384);
449 vec_reset_length (vector);
451 vlib_node_get_nodes (vm, 0 /* main threads */ ,
452 0 /* include stats */ ,
453 1 /* barrier sync */ ,
454 &node_dups, &stat_vms);
455 vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
456 1 /* include stats */ );
458 vl_msg_pop_heap (oldheap);
461 REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
463 rmp->reply_in_shmem = (uword) vector;
469 show_log_details (vl_api_registration_t * reg, u32 context,
471 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
475 vl_api_log_details_t *rmp;
477 clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
479 clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
480 msg_size = sizeof (*rmp) + class_len + message_len;
482 rmp = vl_msg_api_alloc (msg_size);
483 clib_memset (rmp, 0, msg_size);
484 rmp->_vl_msg_id = ntohs (VL_API_LOG_DETAILS);
486 rmp->context = context;
487 rmp->timestamp = clib_host_to_net_f64 (timestamp);
488 rmp->level = htonl (*level);
490 memcpy (rmp->msg_class, msg_class, class_len - 1);
491 memcpy (rmp->message, message, message_len - 1);
492 /* enforced by memset() above */
493 ASSERT (0 == rmp->msg_class[class_len - 1]);
494 ASSERT (0 == rmp->message[message_len - 1]);
496 vl_api_send_msg (reg, (u8 *) rmp);
500 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
504 vlib_log_main_t *lm = &log_main;
506 int i = last_log_entry ();
507 int count = lm->count;
508 f64 time_offset, start_time;
509 vl_api_registration_t *reg;
511 reg = vl_api_client_index_to_registration (mp->client_index);
515 start_time = clib_net_to_host_f64 (mp->start_timestamp);
517 time_offset = (f64) lm->time_zero_timeval.tv_sec
518 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
522 e = vec_elt_at_index (lm->entries, i);
523 if (start_time <= e->timestamp + time_offset)
524 show_log_details (reg, mp->context, e->timestamp + time_offset,
525 (vl_api_log_level_t *) & e->level,
526 format (0, "%U", format_vlib_log_class, e->class),
528 i = (i + 1) % lm->size;
534 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t * mp)
537 vl_api_show_vpe_system_time_reply_t *rmp;
539 REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
541 rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ());
547 vl_api_get_f64_endian_value_t_handler (vl_api_get_f64_endian_value_t * mp)
551 vl_api_get_f64_endian_value_reply_t *rmp;
552 if (1.0 != clib_net_to_host_f64 (mp->f64_one))
553 rv = VNET_API_ERROR_API_ENDIAN_FAILED;
556 REPLY_MACRO2(VL_API_GET_F64_ENDIAN_VALUE_REPLY,
558 rmp->f64_one_result = clib_host_to_net_f64 (one);
564 vl_api_get_f64_increment_by_one_t_handler (vl_api_get_f64_increment_by_one_t *
568 vl_api_get_f64_increment_by_one_reply_t *rmp;
571 REPLY_MACRO2(VL_API_GET_F64_INCREMENT_BY_ONE_REPLY,
573 rmp->f64_value = clib_host_to_net_f64 (clib_net_to_host_f64(mp->f64_value) + 1.0);
578 #define BOUNCE_HANDLER(nn) \
579 static void vl_api_##nn##_t_handler ( \
580 vl_api_##nn##_t *mp) \
582 vpe_client_registration_t *reg; \
583 vpe_api_main_t * vam = &vpe_api_main; \
586 /* One registration only... */ \
587 pool_foreach (reg, vam->nn##_registrations) \
589 q = vl_api_client_index_to_input_queue (reg->client_index); \
592 * If the queue is stuffed, turf the msg and complain \
593 * It's unlikely that the intended recipient is \
594 * alive; avoid deadlock at all costs. \
596 if (q->cursize == q->maxsize) { \
597 clib_warning ("ERROR: receiver queue full, drop msg"); \
598 vl_msg_api_free (mp); \
601 vl_msg_api_send_shmem (q, (u8 *)&mp); \
605 vl_msg_api_free (mp); \
608 static void setup_message_id_table (api_main_t * am);
612 * Add vpe's API message handlers to the table.
613 * vlib has already mapped shared memory and
614 * added the client registration handlers.
615 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
617 static clib_error_t *
618 vpe_api_hookup (vlib_main_t * vm)
620 api_main_t *am = vlibapi_get_main ();
623 vl_msg_api_set_handlers(VL_API_##N, #n, \
624 vl_api_##n##_t_handler, \
626 vl_api_##n##_t_endian, \
627 vl_api_##n##_t_print, \
628 sizeof(vl_api_##n##_t), 1);
633 * Trace space for classifier mask+match
635 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
636 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size +=
640 * Thread-safe API messages
642 am->is_mp_safe[VL_API_CONTROL_PING] = 1;
643 am->is_mp_safe[VL_API_CONTROL_PING_REPLY] = 1;
644 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
645 am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
648 * Set up the (msg_name, crc, message-id) table
650 setup_message_id_table (am);
655 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
658 vpe_api_init (vlib_main_t * vm)
660 vpe_api_main_t *am = &vpe_api_main;
663 am->vnet_main = vnet_get_main ();
665 am->a##_registration_hash = hash_create (0, sizeof (uword));
666 foreach_registration_hash;
669 vl_set_memory_region_name ("/vpe-api");
670 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
675 static clib_error_t *
676 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
679 u64 baseva, size, pvt_heap_size;
681 const int max_buf_size = 4096;
683 struct passwd _pw, *pw;
684 struct group _grp, *grp;
686 buf = vec_new (char, 128);
687 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
689 if (unformat (input, "prefix %s", &chroot_path))
691 vec_add1 (chroot_path, 0);
692 vl_set_memory_root_path ((char *) chroot_path);
694 else if (unformat (input, "uid %d", &uid))
695 vl_set_memory_uid (uid);
696 else if (unformat (input, "gid %d", &gid))
697 vl_set_memory_gid (gid);
698 else if (unformat (input, "baseva %llx", &baseva))
699 vl_set_global_memory_baseva (baseva);
700 else if (unformat (input, "global-size %lldM", &size))
701 vl_set_global_memory_size (size * (1ULL << 20));
702 else if (unformat (input, "global-size %lldG", &size))
703 vl_set_global_memory_size (size * (1ULL << 30));
704 else if (unformat (input, "global-size %lld", &size))
705 vl_set_global_memory_size (size);
706 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
707 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
708 else if (unformat (input, "global-pvt-heap-size size %lld",
710 vl_set_global_pvt_heap_size (pvt_heap_size);
711 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
712 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
713 else if (unformat (input, "api-pvt-heap-size size %lld",
715 vl_set_api_pvt_heap_size (pvt_heap_size);
716 else if (unformat (input, "api-size %lldM", &size))
717 vl_set_api_memory_size (size * (1ULL << 20));
718 else if (unformat (input, "api-size %lldG", &size))
719 vl_set_api_memory_size (size * (1ULL << 30));
720 else if (unformat (input, "api-size %lld", &size))
721 vl_set_api_memory_size (size);
722 else if (unformat (input, "uid %s", &s))
724 /* lookup the username */
727 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
728 && (vec_len (buf) <= max_buf_size))
730 vec_resize (buf, vec_len (buf) * 2);
734 e = clib_error_return_code (0, rv,
735 CLIB_ERROR_ERRNO_VALID |
737 "cannot fetch username %s", s);
745 clib_error_return_fatal (0, "username %s does not exist", s);
751 vl_set_memory_uid (pw->pw_uid);
753 else if (unformat (input, "gid %s", &s))
755 /* lookup the group name */
758 getgrnam_r (s, &_grp, buf, vec_len (buf),
760 && (vec_len (buf) <= max_buf_size))
762 vec_resize (buf, vec_len (buf) * 2);
766 e = clib_error_return_code (0, rv,
767 CLIB_ERROR_ERRNO_VALID |
769 "cannot fetch group %s", s);
776 e = clib_error_return_fatal (0, "group %s does not exist", s);
783 vl_set_memory_gid (grp->gr_gid);
786 return clib_error_return (0, "unknown input `%U'",
787 format_unformat_error, input);
792 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
795 get_unformat_vnet_sw_interface (void)
797 return (void *) &unformat_vnet_sw_interface;
800 #define vl_msg_name_crc_list
801 #include <vpp/api/vpe_all_api_h.h>
802 #undef vl_msg_name_crc_list
805 setup_message_id_table (api_main_t * am)
807 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
808 foreach_vl_msg_name_crc_memclnt;
809 foreach_vl_msg_name_crc_vpe;
812 #define vl_api_version_tuple(n,mj, mi, p) \
813 vl_msg_api_add_version (am, #n, mj, mi, p);
814 #include <vpp/api/vpe_all_api_h.h>
815 #undef vl_api_version_tuple
820 * fd.io coding-style-patch-verification: ON
823 * eval: (c-set-style "gnu")