2 *------------------------------------------------------------------
3 * api.c - message handler registration
5 * Copyright (c) 2010-2016 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 <vpp/stats/stats.h>
61 #include <vpp/api/vpe_msg_enum.h>
63 #define vl_typedefs /* define message structures */
64 #include <vpp/api/vpe_all_api_h.h>
66 #define vl_endianfun /* define message structures */
67 #include <vpp/api/vpe_all_api_h.h>
69 /* instantiate all the print functions we know about */
70 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
72 #include <vpp/api/vpe_all_api_h.h>
74 #include <vlibapi/api_helper_macros.h>
75 #define foreach_vpe_api_msg \
76 _(CONTROL_PING, control_ping) \
78 _(CLI_INBAND, cli_inband) \
79 _(GET_NODE_INDEX, get_node_index) \
80 _(ADD_NODE_NEXT, add_node_next) \
81 _(SHOW_VERSION, show_version) \
82 _(GET_NODE_GRAPH, get_node_graph) \
83 _(GET_NEXT_INDEX, get_next_index) \
86 #define QUOTE(x) QUOTE_(x)
89 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
90 RESOLVE_IP6_ADD_DEL_ROUTE,
93 extern vpe_api_main_t vpe_api_main;
95 /* Clean up all registrations belonging to the indicated client */
97 memclnt_delete_callback (u32 client_index)
99 vpe_api_main_t *vam = &vpe_api_main;
100 vpe_client_registration_t *rp;
102 int stats_memclnt_delete_callback (u32 client_index);
104 stats_memclnt_delete_callback (client_index);
107 p = hash_get (vam->a##_registration_hash, client_index); \
109 rp = pool_elt_at_index (vam->a##_registrations, p[0]); \
110 pool_put (vam->a##_registrations, rp); \
111 hash_unset (vam->a##_registration_hash, client_index); \
113 foreach_registration_hash;
118 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
121 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
123 vl_api_control_ping_reply_t *rmp;
127 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
129 rmp->vpe_pid = ntohl (getpid());
135 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
137 u8 **shmem_vecp = (u8 **) arg;
140 api_main_t *am = &api_main;
143 shmem_vec = *shmem_vecp;
145 offset = vec_len (shmem_vec);
147 pthread_mutex_lock (&am->vlib_rp->mutex);
148 oldheap = svm_push_data_heap (am->vlib_rp);
150 vec_validate (shmem_vec, offset + buffer_bytes - 1);
152 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
154 svm_pop_heap (oldheap);
155 pthread_mutex_unlock (&am->vlib_rp->mutex);
157 *shmem_vecp = shmem_vec;
162 vl_api_cli_t_handler (vl_api_cli_t * mp)
164 vl_api_cli_reply_t *rp;
165 unix_shared_memory_queue_t *q;
166 vlib_main_t *vm = vlib_get_main ();
167 api_main_t *am = &api_main;
168 unformat_input_t input;
172 q = vl_api_client_index_to_input_queue (mp->client_index);
176 rp = vl_msg_api_alloc (sizeof (*rp));
177 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
178 rp->context = mp->context;
180 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
182 vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
184 pthread_mutex_lock (&am->vlib_rp->mutex);
185 oldheap = svm_push_data_heap (am->vlib_rp);
187 vec_add1 (shmem_vec, 0);
189 svm_pop_heap (oldheap);
190 pthread_mutex_unlock (&am->vlib_rp->mutex);
192 rp->reply_in_shmem = (uword) shmem_vec;
194 vl_msg_api_send_shmem (q, (u8 *) & rp);
198 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
200 u8 **mem_vecp = (u8 **) arg;
201 u8 *mem_vec = *mem_vecp;
202 u32 offset = vec_len (mem_vec);
204 vec_validate (mem_vec, offset + buffer_bytes - 1);
205 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
210 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
212 vl_api_cli_inband_reply_t *rmp;
214 vlib_main_t *vm = vlib_get_main ();
215 unformat_input_t input;
218 unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
219 vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
221 u32 len = vec_len (out_vec);
223 REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
225 rmp->length = htonl (len);
226 clib_memcpy (rmp->reply, out_vec, len);
233 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
235 vl_api_show_version_reply_t *rmp;
237 char *vpe_api_get_build_directory (void);
238 char *vpe_api_get_version (void);
239 char *vpe_api_get_build_date (void);
242 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
244 strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
245 strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
246 ARRAY_LEN(rmp->build_directory)-1);
247 strncpy ((char *) rmp->version, vpe_api_get_version(),
248 ARRAY_LEN(rmp->version)-1);
249 strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
250 ARRAY_LEN(rmp->build_date)-1);
256 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
258 vlib_main_t *vm = vlib_get_main ();
259 vl_api_get_node_index_reply_t *rmp;
264 n = vlib_get_node_by_name (vm, mp->node_name);
267 rv = VNET_API_ERROR_NO_SUCH_NODE;
269 node_index = n->index;
272 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
274 rmp->node_index = ntohl(node_index);
280 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
282 vlib_main_t *vm = vlib_get_main ();
283 vl_api_get_next_index_reply_t *rmp;
284 vlib_node_t *node, *next_node;
286 u32 next_node_index = ~0, next_index = ~0;
289 node = vlib_get_node_by_name (vm, mp->node_name);
293 rv = VNET_API_ERROR_NO_SUCH_NODE;
297 next_node = vlib_get_node_by_name (vm, mp->next_name);
301 rv = VNET_API_ERROR_NO_SUCH_NODE2;
305 next_node_index = next_node->index;
307 p = hash_get (node->next_slot_by_node, next_node_index);
311 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
319 REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
321 rmp->next_index = ntohl(next_index);
327 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
329 vlib_main_t *vm = vlib_get_main ();
330 vl_api_add_node_next_reply_t *rmp;
331 vlib_node_t *n, *next;
335 n = vlib_get_node_by_name (vm, mp->node_name);
339 rv = VNET_API_ERROR_NO_SUCH_NODE;
343 next = vlib_get_node_by_name (vm, mp->next_name);
346 rv = VNET_API_ERROR_NO_SUCH_NODE2;
348 next_index = vlib_node_add_next (vm, n->index, next->index);
352 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
354 rmp->next_index = ntohl(next_index);
360 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
364 api_main_t *am = &api_main;
365 vlib_main_t *vm = vlib_get_main ();
367 vl_api_get_node_graph_reply_t *rmp;
369 pthread_mutex_lock (&am->vlib_rp->mutex);
370 oldheap = svm_push_data_heap (am->vlib_rp);
373 * Keep the number of memcpy ops to a minimum (e.g. 1).
375 vec_validate (vector, 16384);
376 vec_reset_length (vector);
379 vector = vlib_node_serialize (&vm->node_main, vector,
380 (u32) ~ 0 /* all threads */ ,
381 1 /* include nexts */ ,
382 1 /* include stats */ );
384 svm_pop_heap (oldheap);
385 pthread_mutex_unlock (&am->vlib_rp->mutex);
388 REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
390 rmp->reply_in_shmem = (uword) vector;
395 #define BOUNCE_HANDLER(nn) \
396 static void vl_api_##nn##_t_handler ( \
397 vl_api_##nn##_t *mp) \
399 vpe_client_registration_t *reg; \
400 vpe_api_main_t * vam = &vpe_api_main; \
401 unix_shared_memory_queue_t * q; \
403 /* One registration only... */ \
404 pool_foreach(reg, vam->nn##_registrations, \
406 q = vl_api_client_index_to_input_queue (reg->client_index); \
409 * If the queue is stuffed, turf the msg and complain \
410 * It's unlikely that the intended recipient is \
411 * alive; avoid deadlock at all costs. \
413 if (q->cursize == q->maxsize) { \
414 clib_warning ("ERROR: receiver queue full, drop msg"); \
415 vl_msg_api_free (mp); \
418 vl_msg_api_send_shmem (q, (u8 *)&mp); \
422 vl_msg_api_free (mp); \
425 static void setup_message_id_table (api_main_t * am);
429 * Add vpe's API message handlers to the table.
430 * vlib has alread mapped shared memory and
431 * added the client registration handlers.
432 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
434 static clib_error_t *
435 vpe_api_hookup (vlib_main_t * vm)
437 api_main_t *am = &api_main;
440 vl_msg_api_set_handlers(VL_API_##N, #n, \
441 vl_api_##n##_t_handler, \
443 vl_api_##n##_t_endian, \
444 vl_api_##n##_t_print, \
445 sizeof(vl_api_##n##_t), 1);
450 * Trace space for classifier mask+match
452 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
453 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
454 += 5 * sizeof (u32x4);
457 * Thread-safe API messages
459 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
460 am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
463 * Set up the (msg_name, crc, message-id) table
465 setup_message_id_table (am);
470 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
472 static clib_error_t *
473 vpe_api_init (vlib_main_t * vm)
475 vpe_api_main_t *am = &vpe_api_main;
478 am->vnet_main = vnet_get_main ();
480 am->a##_registration_hash = hash_create (0, sizeof (uword));
481 foreach_registration_hash;
484 vl_set_memory_region_name ("/vpe-api");
485 vl_enable_disable_memory_api (vm, 1 /* enable it */ );
490 VLIB_INIT_FUNCTION (vpe_api_init);
493 static clib_error_t *
494 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
497 u64 baseva, size, pvt_heap_size;
499 const int max_buf_size = 4096;
501 struct passwd _pw, *pw;
502 struct group _grp, *grp;
504 buf = vec_new (char, 128);
505 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 if (unformat (input, "prefix %s", &chroot_path))
509 vec_add1 (chroot_path, 0);
510 vl_set_memory_root_path ((char *) chroot_path);
512 else if (unformat (input, "uid %d", &uid))
513 vl_set_memory_uid (uid);
514 else if (unformat (input, "gid %d", &gid))
515 vl_set_memory_gid (gid);
516 else if (unformat (input, "baseva %llx", &baseva))
517 vl_set_global_memory_baseva (baseva);
518 else if (unformat (input, "global-size %lldM", &size))
519 vl_set_global_memory_size (size * (1ULL << 20));
520 else if (unformat (input, "global-size %lldG", &size))
521 vl_set_global_memory_size (size * (1ULL << 30));
522 else if (unformat (input, "global-size %lld", &size))
523 vl_set_global_memory_size (size);
524 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
525 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
526 else if (unformat (input, "global-pvt-heap-size size %lld",
528 vl_set_global_pvt_heap_size (pvt_heap_size);
529 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
530 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
531 else if (unformat (input, "api-pvt-heap-size size %lld",
533 vl_set_api_pvt_heap_size (pvt_heap_size);
534 else if (unformat (input, "api-size %lldM", &size))
535 vl_set_api_memory_size (size * (1ULL << 20));
536 else if (unformat (input, "api-size %lldG", &size))
537 vl_set_api_memory_size (size * (1ULL << 30));
538 else if (unformat (input, "api-size %lld", &size))
539 vl_set_api_memory_size (size);
540 else if (unformat (input, "uid %s", &s))
542 /* lookup the username */
545 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
546 && (vec_len (buf) <= max_buf_size))
548 vec_resize (buf, vec_len (buf) * 2);
552 e = clib_error_return_code (0, rv,
553 CLIB_ERROR_ERRNO_VALID |
555 "cannot fetch username %s", s);
563 clib_error_return_fatal (0, "username %s does not exist", s);
569 vl_set_memory_uid (pw->pw_uid);
571 else if (unformat (input, "gid %s", &s))
573 /* lookup the group name */
576 getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
577 && (vec_len (buf) <= max_buf_size))
579 vec_resize (buf, vec_len (buf) * 2);
583 e = clib_error_return_code (0, rv,
584 CLIB_ERROR_ERRNO_VALID |
586 "cannot fetch group %s", s);
593 e = clib_error_return_fatal (0, "group %s does not exist", s);
600 vl_set_memory_gid (grp->gr_gid);
603 return clib_error_return (0, "unknown input `%U'",
604 format_unformat_error, input);
609 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
612 get_unformat_vnet_sw_interface (void)
614 return (void *) &unformat_vnet_sw_interface;
618 format_arp_event (u8 * s, va_list * args)
620 vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
622 s = format (s, "pid %d: ", ntohl (event->pid));
623 s = format (s, "resolution for %U", format_ip4_address, &event->address);
628 format_nd_event (u8 * s, va_list * args)
630 vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
632 s = format (s, "pid %d: ", ntohl (event->pid));
633 s = format (s, "resolution for %U", format_ip6_address, event->address);
637 static clib_error_t *
638 show_ip_arp_nd_events_fn (vlib_main_t * vm,
639 unformat_input_t * input, vlib_cli_command_t * cmd)
641 vpe_api_main_t *am = &vpe_api_main;
642 vl_api_ip4_arp_event_t *arp_event;
643 vl_api_ip6_nd_event_t *nd_event;
645 if (pool_elts (am->arp_events) == 0 && pool_elts (am->nd_events) == 0 &&
646 pool_elts (am->wc_ip4_arp_events_registrations) == 0 &&
647 pool_elts (am->wc_ip6_nd_events_registrations) == 0)
649 vlib_cli_output (vm, "No active arp or nd event registrations");
654 pool_foreach (arp_event, am->arp_events,
656 vlib_cli_output (vm, "%U", format_arp_event, arp_event);
659 vpe_client_registration_t *reg;
660 pool_foreach(reg, am->wc_ip4_arp_events_registrations,
662 vlib_cli_output (vm, "pid %d: bd mac/ip4 binding events",
663 ntohl (reg->client_pid));
666 pool_foreach (nd_event, am->nd_events,
668 vlib_cli_output (vm, "%U", format_nd_event, nd_event);
671 pool_foreach(reg, am->wc_ip6_nd_events_registrations,
673 vlib_cli_output (vm, "pid %d: bd mac/ip6 binding events",
674 ntohl (reg->client_pid));
682 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
683 .path = "show arp-nd-event registrations",
684 .function = show_ip_arp_nd_events_fn,
685 .short_help = "Show ip4 arp and ip6 nd event registrations",
689 #define vl_msg_name_crc_list
690 #include <vpp/api/vpe_all_api_h.h>
691 #undef vl_msg_name_crc_list
694 setup_message_id_table (api_main_t * am)
696 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
697 foreach_vl_msg_name_crc_memclnt;
698 foreach_vl_msg_name_crc_vpe;
701 #define vl_api_version_tuple(n,mj, mi, p) \
702 vl_msg_api_add_version (am, #n, mj, mi, p);
703 #include <vpp/api/vpe_all_api_h.h>
704 #undef vl_api_version_tuple
709 * fd.io coding-style-patch-verification: ON
712 * eval: (c-set-style "gnu")