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/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
48 #include <vnet/api_errno.h>
49 #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>
64 #define vl_typedefs /* define message structures */
65 #include <vpp/api/vpe_all_api_h.h>
67 #define vl_endianfun /* define message structures */
68 #include <vpp/api/vpe_all_api_h.h>
70 /* instantiate all the print functions we know about */
71 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
73 #include <vpp/api/vpe_all_api_h.h>
75 #include <vlibapi/api_helper_macros.h>
77 #define foreach_vpe_api_msg \
78 _(CONTROL_PING, control_ping) \
80 _(CLI_INBAND, cli_inband) \
81 _(GET_NODE_INDEX, get_node_index) \
82 _(ADD_NODE_NEXT, add_node_next) \
83 _(SHOW_VERSION, show_version) \
84 _(SHOW_THREADS, show_threads) \
85 _(GET_NODE_GRAPH, get_node_graph) \
86 _(GET_NEXT_INDEX, get_next_index) \
89 #define QUOTE(x) QUOTE_(x)
93 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
94 RESOLVE_IP6_ADD_DEL_ROUTE,
97 extern vpe_api_main_t vpe_api_main;
99 /* Clean up all registrations belonging to the indicated client */
100 static clib_error_t *
101 memclnt_delete_callback (u32 client_index)
103 vpe_api_main_t *vam = &vpe_api_main;
104 vpe_client_registration_t *rp;
108 p = hash_get (vam->a##_registration_hash, client_index); \
110 rp = pool_elt_at_index (vam->a##_registrations, p[0]); \
111 pool_put (vam->a##_registrations, rp); \
112 hash_unset (vam->a##_registration_hash, client_index); \
114 foreach_registration_hash;
119 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
122 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
124 vl_api_control_ping_reply_t *rmp;
128 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
130 rmp->vpe_pid = ntohl (getpid());
136 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
138 u8 **shmem_vecp = (u8 **) arg;
141 api_main_t *am = &api_main;
144 shmem_vec = *shmem_vecp;
146 offset = vec_len (shmem_vec);
148 pthread_mutex_lock (&am->vlib_rp->mutex);
149 oldheap = svm_push_data_heap (am->vlib_rp);
151 vec_validate (shmem_vec, offset + buffer_bytes - 1);
153 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
155 svm_pop_heap (oldheap);
156 pthread_mutex_unlock (&am->vlib_rp->mutex);
158 *shmem_vecp = shmem_vec;
163 vl_api_cli_t_handler (vl_api_cli_t * mp)
165 vl_api_cli_reply_t *rp;
166 vl_api_registration_t *reg;
167 vlib_main_t *vm = vlib_get_main ();
168 api_main_t *am = &api_main;
169 unformat_input_t input;
173 reg = vl_api_client_index_to_registration (mp->client_index);
177 rp = vl_msg_api_alloc (sizeof (*rp));
178 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
179 rp->context = mp->context;
181 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
183 vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
185 pthread_mutex_lock (&am->vlib_rp->mutex);
186 oldheap = svm_push_data_heap (am->vlib_rp);
188 vec_add1 (shmem_vec, 0);
190 svm_pop_heap (oldheap);
191 pthread_mutex_unlock (&am->vlib_rp->mutex);
193 rp->reply_in_shmem = (uword) shmem_vec;
195 vl_api_send_msg (reg, (u8 *) rp);
199 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
201 u8 **mem_vecp = (u8 **) arg;
202 u8 *mem_vec = *mem_vecp;
203 u32 offset = vec_len (mem_vec);
205 vec_validate (mem_vec, offset + buffer_bytes - 1);
206 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
211 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
213 vl_api_cli_inband_reply_t *rmp;
215 vlib_main_t *vm = vlib_get_main ();
216 unformat_input_t input;
220 if (vl_msg_api_get_msg_length (mp) <
221 vl_api_string_len (&mp->cmd) + sizeof (*mp))
227 unformat_init_string (&input, (char *) vl_api_from_api_string (&mp->cmd),
228 vl_api_string_len (&mp->cmd));
229 vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
231 len = vec_len (out_vec);
235 REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
237 vl_api_to_api_string(len, (const char *)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);
252 u32 program_len = strnlen_s ("vpe", 32);
253 u32 version_len = strnlen_s (vpe_api_get_version (), 32);
254 u32 build_date_len = strnlen_s (vpe_api_get_build_date (), 32);
255 u32 build_directory_len = strnlen_s (vpe_api_get_build_directory (), 256);
257 u32 n = program_len + version_len + build_date_len + build_directory_len;
260 REPLY_MACRO3(VL_API_SHOW_VERSION_REPLY, n,
262 char *p = (char *)&rmp->program;
263 p += vl_api_to_api_string(program_len, "vpe", (vl_api_string_t *)p);
264 p += vl_api_to_api_string(version_len, vpe_api_get_version(), (vl_api_string_t *)p);
265 p += vl_api_to_api_string(build_date_len, vpe_api_get_build_date(), (vl_api_string_t *)p);
266 vl_api_to_api_string(build_directory_len, vpe_api_get_build_directory(), (vl_api_string_t *)p);
272 get_thread_data (vl_api_thread_data_t * td, int index)
274 vlib_worker_thread_t *w = vlib_worker_threads + index;
275 td->id = htonl (index);
277 strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
279 strncpy ((char *) td->type, (char *) w->registration->name,
280 ARRAY_LEN (td->type) - 1);
281 td->pid = htonl (w->lwp);
282 td->cpu_id = htonl (w->cpu_id);
283 td->core = htonl (w->core_id);
284 td->cpu_socket = htonl (w->socket_id);
288 vl_api_show_threads_t_handler (vl_api_show_threads_t * mp)
290 vlib_main_t *vm = vlib_get_main ();
291 int rv = 0, count = 0;
293 #if !defined(__powerpc64__)
294 vl_api_registration_t *reg;
295 vl_api_show_threads_reply_t *rmp;
296 vl_api_thread_data_t *td;
298 count = vec_len (vlib_worker_threads);
302 msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
303 reg = vl_api_client_index_to_registration (mp->client_index);
307 rmp = vl_msg_api_alloc (msg_size);
308 clib_memset (rmp, 0, msg_size);
309 rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY);
310 rmp->context = mp->context;
311 rmp->count = htonl (count);
312 td = rmp->thread_data;
314 for (i = 0; i < count; i++)
316 get_thread_data (&td[i], i);
319 vl_api_send_msg (reg, (u8 *) rmp);
322 /* unimplemented support */
324 clib_warning ("power pc does not support show threads api");
326 REPLY_MACRO2(VL_API_SHOW_THREADS_REPLY,
328 rmp->count = htonl(count);
335 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
337 vlib_main_t *vm = vlib_get_main ();
338 vl_api_get_node_index_reply_t *rmp;
343 n = vlib_get_node_by_name (vm, mp->node_name);
346 rv = VNET_API_ERROR_NO_SUCH_NODE;
348 node_index = n->index;
351 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
353 rmp->node_index = ntohl(node_index);
359 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
361 vlib_main_t *vm = vlib_get_main ();
362 vl_api_get_next_index_reply_t *rmp;
363 vlib_node_t *node, *next_node;
365 u32 next_node_index = ~0, next_index = ~0;
368 node = vlib_get_node_by_name (vm, mp->node_name);
372 rv = VNET_API_ERROR_NO_SUCH_NODE;
376 next_node = vlib_get_node_by_name (vm, mp->next_name);
380 rv = VNET_API_ERROR_NO_SUCH_NODE2;
384 next_node_index = next_node->index;
386 p = hash_get (node->next_slot_by_node, next_node_index);
390 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
398 REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
400 rmp->next_index = ntohl(next_index);
406 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
408 vlib_main_t *vm = vlib_get_main ();
409 vl_api_add_node_next_reply_t *rmp;
410 vlib_node_t *n, *next;
414 n = vlib_get_node_by_name (vm, mp->node_name);
418 rv = VNET_API_ERROR_NO_SUCH_NODE;
422 next = vlib_get_node_by_name (vm, mp->next_name);
425 rv = VNET_API_ERROR_NO_SUCH_NODE2;
427 next_index = vlib_node_add_next (vm, n->index, next->index);
431 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
433 rmp->next_index = ntohl(next_index);
439 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
443 api_main_t *am = &api_main;
444 vlib_main_t *vm = vlib_get_main ();
446 vl_api_get_node_graph_reply_t *rmp;
447 static vlib_node_t ***node_dups;
448 static vlib_main_t **stat_vms;
450 pthread_mutex_lock (&am->vlib_rp->mutex);
451 oldheap = svm_push_data_heap (am->vlib_rp);
454 * Keep the number of memcpy ops to a minimum (e.g. 1).
456 vec_validate (vector, 16384);
457 vec_reset_length (vector);
459 vlib_node_get_nodes (vm, 0 /* main threads */ ,
460 0 /* include stats */ ,
461 1 /* barrier sync */ ,
462 &node_dups, &stat_vms);
463 vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
464 1 /* include stats */ );
466 svm_pop_heap (oldheap);
467 pthread_mutex_unlock (&am->vlib_rp->mutex);
470 REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
472 rmp->reply_in_shmem = (uword) vector;
477 #define BOUNCE_HANDLER(nn) \
478 static void vl_api_##nn##_t_handler ( \
479 vl_api_##nn##_t *mp) \
481 vpe_client_registration_t *reg; \
482 vpe_api_main_t * vam = &vpe_api_main; \
485 /* One registration only... */ \
486 pool_foreach(reg, vam->nn##_registrations, \
488 q = vl_api_client_index_to_input_queue (reg->client_index); \
491 * If the queue is stuffed, turf the msg and complain \
492 * It's unlikely that the intended recipient is \
493 * alive; avoid deadlock at all costs. \
495 if (q->cursize == q->maxsize) { \
496 clib_warning ("ERROR: receiver queue full, drop msg"); \
497 vl_msg_api_free (mp); \
500 vl_msg_api_send_shmem (q, (u8 *)&mp); \
504 vl_msg_api_free (mp); \
507 static void setup_message_id_table (api_main_t * am);
511 * Add vpe's API message handlers to the table.
512 * vlib has already mapped shared memory and
513 * added the client registration handlers.
514 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
516 static clib_error_t *
517 vpe_api_hookup (vlib_main_t * vm)
519 api_main_t *am = &api_main;
522 vl_msg_api_set_handlers(VL_API_##N, #n, \
523 vl_api_##n##_t_handler, \
525 vl_api_##n##_t_endian, \
526 vl_api_##n##_t_print, \
527 sizeof(vl_api_##n##_t), 1);
532 * Trace space for classifier mask+match
534 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
535 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
536 += 5 * sizeof (u32x4);
539 * Thread-safe API messages
541 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
542 am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
545 * Set up the (msg_name, crc, message-id) table
547 setup_message_id_table (am);
552 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
555 vpe_api_init (vlib_main_t * vm)
557 vpe_api_main_t *am = &vpe_api_main;
560 am->vnet_main = vnet_get_main ();
562 am->a##_registration_hash = hash_create (0, sizeof (uword));
563 foreach_registration_hash;
566 vl_set_memory_region_name ("/vpe-api");
567 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
572 static clib_error_t *
573 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
576 u64 baseva, size, pvt_heap_size;
578 const int max_buf_size = 4096;
580 struct passwd _pw, *pw;
581 struct group _grp, *grp;
583 buf = vec_new (char, 128);
584 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
586 if (unformat (input, "prefix %s", &chroot_path))
588 vec_add1 (chroot_path, 0);
589 vl_set_memory_root_path ((char *) chroot_path);
591 else if (unformat (input, "uid %d", &uid))
592 vl_set_memory_uid (uid);
593 else if (unformat (input, "gid %d", &gid))
594 vl_set_memory_gid (gid);
595 else if (unformat (input, "baseva %llx", &baseva))
596 vl_set_global_memory_baseva (baseva);
597 else if (unformat (input, "global-size %lldM", &size))
598 vl_set_global_memory_size (size * (1ULL << 20));
599 else if (unformat (input, "global-size %lldG", &size))
600 vl_set_global_memory_size (size * (1ULL << 30));
601 else if (unformat (input, "global-size %lld", &size))
602 vl_set_global_memory_size (size);
603 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
604 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
605 else if (unformat (input, "global-pvt-heap-size size %lld",
607 vl_set_global_pvt_heap_size (pvt_heap_size);
608 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
609 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
610 else if (unformat (input, "api-pvt-heap-size size %lld",
612 vl_set_api_pvt_heap_size (pvt_heap_size);
613 else if (unformat (input, "api-size %lldM", &size))
614 vl_set_api_memory_size (size * (1ULL << 20));
615 else if (unformat (input, "api-size %lldG", &size))
616 vl_set_api_memory_size (size * (1ULL << 30));
617 else if (unformat (input, "api-size %lld", &size))
618 vl_set_api_memory_size (size);
619 else if (unformat (input, "uid %s", &s))
621 /* lookup the username */
624 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
625 && (vec_len (buf) <= max_buf_size))
627 vec_resize (buf, vec_len (buf) * 2);
631 e = clib_error_return_code (0, rv,
632 CLIB_ERROR_ERRNO_VALID |
634 "cannot fetch username %s", s);
642 clib_error_return_fatal (0, "username %s does not exist", s);
648 vl_set_memory_uid (pw->pw_uid);
650 else if (unformat (input, "gid %s", &s))
652 /* lookup the group name */
655 getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
656 && (vec_len (buf) <= max_buf_size))
658 vec_resize (buf, vec_len (buf) * 2);
662 e = clib_error_return_code (0, rv,
663 CLIB_ERROR_ERRNO_VALID |
665 "cannot fetch group %s", s);
672 e = clib_error_return_fatal (0, "group %s does not exist", s);
679 vl_set_memory_gid (grp->gr_gid);
682 return clib_error_return (0, "unknown input `%U'",
683 format_unformat_error, input);
688 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
691 get_unformat_vnet_sw_interface (void)
693 return (void *) &unformat_vnet_sw_interface;
697 format_arp_event (u8 * s, va_list * args)
699 vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
701 s = format (s, "pid %d: ", ntohl (event->pid));
702 s = format (s, "resolution for %U", format_vl_api_ip4_address, event->ip);
707 format_nd_event (u8 * s, va_list * args)
709 vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
711 s = format (s, "pid %d: ", ntohl (event->pid));
712 s = format (s, "resolution for %U", format_vl_api_ip6_address, event->ip);
716 static clib_error_t *
717 show_ip_arp_nd_events_fn (vlib_main_t * vm,
718 unformat_input_t * input, vlib_cli_command_t * cmd)
720 vpe_api_main_t *am = &vpe_api_main;
721 vl_api_ip4_arp_event_t *arp_event;
722 vl_api_ip6_nd_event_t *nd_event;
724 if (pool_elts (am->arp_events) == 0 && pool_elts (am->nd_events) == 0 &&
725 pool_elts (am->wc_ip4_arp_events_registrations) == 0 &&
726 pool_elts (am->wc_ip6_nd_events_registrations) == 0)
728 vlib_cli_output (vm, "No active arp or nd event registrations");
733 pool_foreach (arp_event, am->arp_events,
735 vlib_cli_output (vm, "%U", format_arp_event, arp_event);
738 vpe_client_registration_t *reg;
739 pool_foreach(reg, am->wc_ip4_arp_events_registrations,
741 vlib_cli_output (vm, "pid %d: bd mac/ip4 binding events",
742 ntohl (reg->client_pid));
745 pool_foreach (nd_event, am->nd_events,
747 vlib_cli_output (vm, "%U", format_nd_event, nd_event);
750 pool_foreach(reg, am->wc_ip6_nd_events_registrations,
752 vlib_cli_output (vm, "pid %d: bd mac/ip6 binding events",
753 ntohl (reg->client_pid));
761 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
762 .path = "show arp-nd-event registrations",
763 .function = show_ip_arp_nd_events_fn,
764 .short_help = "Show ip4 arp and ip6 nd event registrations",
768 #define vl_msg_name_crc_list
769 #include <vpp/api/vpe_all_api_h.h>
770 #undef vl_msg_name_crc_list
773 setup_message_id_table (api_main_t * am)
775 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
776 foreach_vl_msg_name_crc_memclnt;
777 foreach_vl_msg_name_crc_vpe;
780 #define vl_api_version_tuple(n,mj, mi, p) \
781 vl_msg_api_add_version (am, #n, mj, mi, p);
782 #include <vpp/api/vpe_all_api_h.h>
783 #undef vl_api_version_tuple
788 * fd.io coding-style-patch-verification: ON
791 * eval: (c-set-style "gnu")