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>
76 #define foreach_vpe_api_msg \
77 _(CONTROL_PING, control_ping) \
79 _(CLI_INBAND, cli_inband) \
80 _(GET_NODE_INDEX, get_node_index) \
81 _(ADD_NODE_NEXT, add_node_next) \
82 _(SHOW_VERSION, show_version) \
83 _(GET_NODE_GRAPH, get_node_graph) \
84 _(GET_NEXT_INDEX, get_next_index) \
87 #define QUOTE(x) QUOTE_(x)
91 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
92 RESOLVE_IP6_ADD_DEL_ROUTE,
95 extern vpe_api_main_t vpe_api_main;
97 /* Clean up all registrations belonging to the indicated client */
99 memclnt_delete_callback (u32 client_index)
101 vpe_api_main_t *vam = &vpe_api_main;
102 vpe_client_registration_t *rp;
104 int stats_memclnt_delete_callback (u32 client_index);
106 stats_memclnt_delete_callback (client_index);
109 p = hash_get (vam->a##_registration_hash, client_index); \
111 rp = pool_elt_at_index (vam->a##_registrations, p[0]); \
112 pool_put (vam->a##_registrations, rp); \
113 hash_unset (vam->a##_registration_hash, client_index); \
115 foreach_registration_hash;
120 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
123 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
125 vl_api_control_ping_reply_t *rmp;
129 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
131 rmp->vpe_pid = ntohl (getpid());
137 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
139 u8 **shmem_vecp = (u8 **) arg;
142 api_main_t *am = &api_main;
145 shmem_vec = *shmem_vecp;
147 offset = vec_len (shmem_vec);
149 pthread_mutex_lock (&am->vlib_rp->mutex);
150 oldheap = svm_push_data_heap (am->vlib_rp);
152 vec_validate (shmem_vec, offset + buffer_bytes - 1);
154 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
156 svm_pop_heap (oldheap);
157 pthread_mutex_unlock (&am->vlib_rp->mutex);
159 *shmem_vecp = shmem_vec;
164 vl_api_cli_t_handler (vl_api_cli_t * mp)
166 vl_api_cli_reply_t *rp;
167 vl_api_registration_t *reg;
168 vlib_main_t *vm = vlib_get_main ();
169 api_main_t *am = &api_main;
170 unformat_input_t input;
174 reg = vl_api_client_index_to_registration (mp->client_index);
178 rp = vl_msg_api_alloc (sizeof (*rp));
179 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
180 rp->context = mp->context;
182 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
184 vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
186 pthread_mutex_lock (&am->vlib_rp->mutex);
187 oldheap = svm_push_data_heap (am->vlib_rp);
189 vec_add1 (shmem_vec, 0);
191 svm_pop_heap (oldheap);
192 pthread_mutex_unlock (&am->vlib_rp->mutex);
194 rp->reply_in_shmem = (uword) shmem_vec;
196 vl_api_send_msg (reg, (u8 *) rp);
200 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
202 u8 **mem_vecp = (u8 **) arg;
203 u8 *mem_vec = *mem_vecp;
204 u32 offset = vec_len (mem_vec);
206 vec_validate (mem_vec, offset + buffer_bytes - 1);
207 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
212 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
214 vl_api_cli_inband_reply_t *rmp;
216 vlib_main_t *vm = vlib_get_main ();
217 unformat_input_t input;
220 unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
221 vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
223 u32 len = vec_len (out_vec);
225 REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
227 rmp->length = htonl (len);
228 clib_memcpy (rmp->reply, out_vec, len);
235 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
237 vl_api_show_version_reply_t *rmp;
239 char *vpe_api_get_build_directory (void);
240 char *vpe_api_get_version (void);
241 char *vpe_api_get_build_date (void);
244 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
246 strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
247 strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
248 ARRAY_LEN(rmp->build_directory)-1);
249 strncpy ((char *) rmp->version, vpe_api_get_version(),
250 ARRAY_LEN(rmp->version)-1);
251 strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
252 ARRAY_LEN(rmp->build_date)-1);
258 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
260 vlib_main_t *vm = vlib_get_main ();
261 vl_api_get_node_index_reply_t *rmp;
266 n = vlib_get_node_by_name (vm, mp->node_name);
269 rv = VNET_API_ERROR_NO_SUCH_NODE;
271 node_index = n->index;
274 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
276 rmp->node_index = ntohl(node_index);
282 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
284 vlib_main_t *vm = vlib_get_main ();
285 vl_api_get_next_index_reply_t *rmp;
286 vlib_node_t *node, *next_node;
288 u32 next_node_index = ~0, next_index = ~0;
291 node = vlib_get_node_by_name (vm, mp->node_name);
295 rv = VNET_API_ERROR_NO_SUCH_NODE;
299 next_node = vlib_get_node_by_name (vm, mp->next_name);
303 rv = VNET_API_ERROR_NO_SUCH_NODE2;
307 next_node_index = next_node->index;
309 p = hash_get (node->next_slot_by_node, next_node_index);
313 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
321 REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
323 rmp->next_index = ntohl(next_index);
329 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
331 vlib_main_t *vm = vlib_get_main ();
332 vl_api_add_node_next_reply_t *rmp;
333 vlib_node_t *n, *next;
337 n = vlib_get_node_by_name (vm, mp->node_name);
341 rv = VNET_API_ERROR_NO_SUCH_NODE;
345 next = vlib_get_node_by_name (vm, mp->next_name);
348 rv = VNET_API_ERROR_NO_SUCH_NODE2;
350 next_index = vlib_node_add_next (vm, n->index, next->index);
354 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
356 rmp->next_index = ntohl(next_index);
362 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
366 api_main_t *am = &api_main;
367 vlib_main_t *vm = vlib_get_main ();
369 vl_api_get_node_graph_reply_t *rmp;
370 vlib_node_t ***node_dups;
372 pthread_mutex_lock (&am->vlib_rp->mutex);
373 oldheap = svm_push_data_heap (am->vlib_rp);
376 * Keep the number of memcpy ops to a minimum (e.g. 1).
378 vec_validate (vector, 16384);
379 vec_reset_length (vector);
382 node_dups = vlib_node_get_nodes (vm, (u32) ~ 0 /* all threads */ ,
383 1 /* include stats */ );
384 vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
385 1 /* include stats */ );
387 svm_pop_heap (oldheap);
388 pthread_mutex_unlock (&am->vlib_rp->mutex);
391 REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
393 rmp->reply_in_shmem = (uword) vector;
398 #define BOUNCE_HANDLER(nn) \
399 static void vl_api_##nn##_t_handler ( \
400 vl_api_##nn##_t *mp) \
402 vpe_client_registration_t *reg; \
403 vpe_api_main_t * vam = &vpe_api_main; \
406 /* One registration only... */ \
407 pool_foreach(reg, vam->nn##_registrations, \
409 q = vl_api_client_index_to_input_queue (reg->client_index); \
412 * If the queue is stuffed, turf the msg and complain \
413 * It's unlikely that the intended recipient is \
414 * alive; avoid deadlock at all costs. \
416 if (q->cursize == q->maxsize) { \
417 clib_warning ("ERROR: receiver queue full, drop msg"); \
418 vl_msg_api_free (mp); \
421 vl_msg_api_send_shmem (q, (u8 *)&mp); \
425 vl_msg_api_free (mp); \
428 static void setup_message_id_table (api_main_t * am);
432 * Add vpe's API message handlers to the table.
433 * vlib has alread mapped shared memory and
434 * added the client registration handlers.
435 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
437 static clib_error_t *
438 vpe_api_hookup (vlib_main_t * vm)
440 api_main_t *am = &api_main;
443 vl_msg_api_set_handlers(VL_API_##N, #n, \
444 vl_api_##n##_t_handler, \
446 vl_api_##n##_t_endian, \
447 vl_api_##n##_t_print, \
448 sizeof(vl_api_##n##_t), 1);
453 * Trace space for classifier mask+match
455 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
456 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
457 += 5 * sizeof (u32x4);
460 * Thread-safe API messages
462 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
463 am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
466 * Set up the (msg_name, crc, message-id) table
468 setup_message_id_table (am);
473 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
475 static clib_error_t *
476 vpe_api_init (vlib_main_t * vm)
478 vpe_api_main_t *am = &vpe_api_main;
481 am->vnet_main = vnet_get_main ();
483 am->a##_registration_hash = hash_create (0, sizeof (uword));
484 foreach_registration_hash;
487 vl_set_memory_region_name ("/vpe-api");
488 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
493 VLIB_INIT_FUNCTION (vpe_api_init);
496 static clib_error_t *
497 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
500 u64 baseva, size, pvt_heap_size;
502 const int max_buf_size = 4096;
504 struct passwd _pw, *pw;
505 struct group _grp, *grp;
507 buf = vec_new (char, 128);
508 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510 if (unformat (input, "prefix %s", &chroot_path))
512 vec_add1 (chroot_path, 0);
513 vl_set_memory_root_path ((char *) chroot_path);
515 else if (unformat (input, "uid %d", &uid))
516 vl_set_memory_uid (uid);
517 else if (unformat (input, "gid %d", &gid))
518 vl_set_memory_gid (gid);
519 else if (unformat (input, "baseva %llx", &baseva))
520 vl_set_global_memory_baseva (baseva);
521 else if (unformat (input, "global-size %lldM", &size))
522 vl_set_global_memory_size (size * (1ULL << 20));
523 else if (unformat (input, "global-size %lldG", &size))
524 vl_set_global_memory_size (size * (1ULL << 30));
525 else if (unformat (input, "global-size %lld", &size))
526 vl_set_global_memory_size (size);
527 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
528 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
529 else if (unformat (input, "global-pvt-heap-size size %lld",
531 vl_set_global_pvt_heap_size (pvt_heap_size);
532 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
533 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
534 else if (unformat (input, "api-pvt-heap-size size %lld",
536 vl_set_api_pvt_heap_size (pvt_heap_size);
537 else if (unformat (input, "api-size %lldM", &size))
538 vl_set_api_memory_size (size * (1ULL << 20));
539 else if (unformat (input, "api-size %lldG", &size))
540 vl_set_api_memory_size (size * (1ULL << 30));
541 else if (unformat (input, "api-size %lld", &size))
542 vl_set_api_memory_size (size);
543 else if (unformat (input, "uid %s", &s))
545 /* lookup the username */
548 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
549 && (vec_len (buf) <= max_buf_size))
551 vec_resize (buf, vec_len (buf) * 2);
555 e = clib_error_return_code (0, rv,
556 CLIB_ERROR_ERRNO_VALID |
558 "cannot fetch username %s", s);
566 clib_error_return_fatal (0, "username %s does not exist", s);
572 vl_set_memory_uid (pw->pw_uid);
574 else if (unformat (input, "gid %s", &s))
576 /* lookup the group name */
579 getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
580 && (vec_len (buf) <= max_buf_size))
582 vec_resize (buf, vec_len (buf) * 2);
586 e = clib_error_return_code (0, rv,
587 CLIB_ERROR_ERRNO_VALID |
589 "cannot fetch group %s", s);
596 e = clib_error_return_fatal (0, "group %s does not exist", s);
603 vl_set_memory_gid (grp->gr_gid);
606 return clib_error_return (0, "unknown input `%U'",
607 format_unformat_error, input);
612 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
615 get_unformat_vnet_sw_interface (void)
617 return (void *) &unformat_vnet_sw_interface;
621 format_arp_event (u8 * s, va_list * args)
623 vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
625 s = format (s, "pid %d: ", ntohl (event->pid));
626 s = format (s, "resolution for %U", format_ip4_address, &event->address);
631 format_nd_event (u8 * s, va_list * args)
633 vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
635 s = format (s, "pid %d: ", ntohl (event->pid));
636 s = format (s, "resolution for %U", format_ip6_address, event->address);
640 static clib_error_t *
641 show_ip_arp_nd_events_fn (vlib_main_t * vm,
642 unformat_input_t * input, vlib_cli_command_t * cmd)
644 vpe_api_main_t *am = &vpe_api_main;
645 vl_api_ip4_arp_event_t *arp_event;
646 vl_api_ip6_nd_event_t *nd_event;
648 if (pool_elts (am->arp_events) == 0 && pool_elts (am->nd_events) == 0 &&
649 pool_elts (am->wc_ip4_arp_events_registrations) == 0 &&
650 pool_elts (am->wc_ip6_nd_events_registrations) == 0)
652 vlib_cli_output (vm, "No active arp or nd event registrations");
657 pool_foreach (arp_event, am->arp_events,
659 vlib_cli_output (vm, "%U", format_arp_event, arp_event);
662 vpe_client_registration_t *reg;
663 pool_foreach(reg, am->wc_ip4_arp_events_registrations,
665 vlib_cli_output (vm, "pid %d: bd mac/ip4 binding events",
666 ntohl (reg->client_pid));
669 pool_foreach (nd_event, am->nd_events,
671 vlib_cli_output (vm, "%U", format_nd_event, nd_event);
674 pool_foreach(reg, am->wc_ip6_nd_events_registrations,
676 vlib_cli_output (vm, "pid %d: bd mac/ip6 binding events",
677 ntohl (reg->client_pid));
685 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
686 .path = "show arp-nd-event registrations",
687 .function = show_ip_arp_nd_events_fn,
688 .short_help = "Show ip4 arp and ip6 nd event registrations",
692 #define vl_msg_name_crc_list
693 #include <vpp/api/vpe_all_api_h.h>
694 #undef vl_msg_name_crc_list
697 setup_message_id_table (api_main_t * am)
699 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
700 foreach_vl_msg_name_crc_memclnt;
701 foreach_vl_msg_name_crc_vpe;
704 #define vl_api_version_tuple(n,mj, mi, p) \
705 vl_msg_api_add_version (am, #n, mj, mi, p);
706 #include <vpp/api/vpe_all_api_h.h>
707 #undef vl_api_version_tuple
712 * fd.io coding-style-patch-verification: ON
715 * eval: (c-set-style "gnu")