stats: Deprecate old stats framework
[vpp.git] / src / vpp / api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35
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>
47
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50
51 #include <vlib/vlib.h>
52 #include <vlib/unix/unix.h>
53 #include <vlibapi/api.h>
54 #include <vlibmemory/api.h>
55
56 #undef BIHASH_TYPE
57 #undef __included_bihash_template_h__
58
59 #include <vnet/ip/format.h>
60
61 #include <vpp/api/vpe_msg_enum.h>
62
63 #define vl_typedefs             /* define message structures */
64 #include <vpp/api/vpe_all_api_h.h>
65 #undef vl_typedefs
66 #define vl_endianfun            /* define message structures */
67 #include <vpp/api/vpe_all_api_h.h>
68 #undef vl_endianfun
69 /* instantiate all the print functions we know about */
70 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
71 #define vl_printfun
72 #include <vpp/api/vpe_all_api_h.h>
73 #undef vl_printfun
74 #include <vlibapi/api_helper_macros.h>
75
76 #define foreach_vpe_api_msg                                             \
77 _(CONTROL_PING, control_ping)                                           \
78 _(CLI, cli)                                                             \
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 _(SHOW_THREADS, show_threads)                                           \
84 _(GET_NODE_GRAPH, get_node_graph)                                       \
85 _(GET_NEXT_INDEX, get_next_index)                                       \
86
87 #define QUOTE_(x) #x
88 #define QUOTE(x) QUOTE_(x)
89
90 typedef enum
91 {
92   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
93   RESOLVE_IP6_ADD_DEL_ROUTE,
94 } resolve_t;
95
96 extern vpe_api_main_t vpe_api_main;
97
98 /* Clean up all registrations belonging to the indicated client */
99 static clib_error_t *
100 memclnt_delete_callback (u32 client_index)
101 {
102   vpe_api_main_t *vam = &vpe_api_main;
103   vpe_client_registration_t *rp;
104   uword *p;
105
106 #define _(a)                                                    \
107     p = hash_get (vam->a##_registration_hash, client_index);    \
108     if (p) {                                                    \
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);  \
112     }
113   foreach_registration_hash;
114 #undef _
115   return 0;
116 }
117
118 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
119
120 static void
121 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
122 {
123   vl_api_control_ping_reply_t *rmp;
124   int rv = 0;
125
126   /* *INDENT-OFF* */
127   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
128   ({
129     rmp->vpe_pid = ntohl (getpid());
130   }));
131   /* *INDENT-ON* */
132 }
133
134 static void
135 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
136 {
137   u8 **shmem_vecp = (u8 **) arg;
138   u8 *shmem_vec;
139   void *oldheap;
140   api_main_t *am = &api_main;
141   u32 offset;
142
143   shmem_vec = *shmem_vecp;
144
145   offset = vec_len (shmem_vec);
146
147   pthread_mutex_lock (&am->vlib_rp->mutex);
148   oldheap = svm_push_data_heap (am->vlib_rp);
149
150   vec_validate (shmem_vec, offset + buffer_bytes - 1);
151
152   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
153
154   svm_pop_heap (oldheap);
155   pthread_mutex_unlock (&am->vlib_rp->mutex);
156
157   *shmem_vecp = shmem_vec;
158 }
159
160
161 static void
162 vl_api_cli_t_handler (vl_api_cli_t * mp)
163 {
164   vl_api_cli_reply_t *rp;
165   vl_api_registration_t *reg;
166   vlib_main_t *vm = vlib_get_main ();
167   api_main_t *am = &api_main;
168   unformat_input_t input;
169   u8 *shmem_vec = 0;
170   void *oldheap;
171
172   reg = vl_api_client_index_to_registration (mp->client_index);
173   if (!reg)
174     return;;
175
176   rp = vl_msg_api_alloc (sizeof (*rp));
177   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
178   rp->context = mp->context;
179
180   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
181
182   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
183
184   pthread_mutex_lock (&am->vlib_rp->mutex);
185   oldheap = svm_push_data_heap (am->vlib_rp);
186
187   vec_add1 (shmem_vec, 0);
188
189   svm_pop_heap (oldheap);
190   pthread_mutex_unlock (&am->vlib_rp->mutex);
191
192   rp->reply_in_shmem = (uword) shmem_vec;
193
194   vl_api_send_msg (reg, (u8 *) rp);
195 }
196
197 static void
198 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
199 {
200   u8 **mem_vecp = (u8 **) arg;
201   u8 *mem_vec = *mem_vecp;
202   u32 offset = vec_len (mem_vec);
203
204   vec_validate (mem_vec, offset + buffer_bytes - 1);
205   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
206   *mem_vecp = mem_vec;
207 }
208
209 static void
210 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
211 {
212   vl_api_cli_inband_reply_t *rmp;
213   int rv = 0;
214   vlib_main_t *vm = vlib_get_main ();
215   unformat_input_t input;
216   u8 *out_vec = 0;
217   u32 len = 0;
218
219   if (vl_msg_api_get_msg_length (mp) <
220       vl_api_string_len (&mp->cmd) + sizeof (*mp))
221     {
222       rv = -1;
223       goto error;
224     }
225
226   unformat_init_string (&input, (char *) vl_api_from_api_string (&mp->cmd),
227                         vl_api_string_len (&mp->cmd));
228   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
229
230   len = vec_len (out_vec);
231
232 error:
233   /* *INDENT-OFF* */
234   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
235   ({
236     vl_api_to_api_string(len, (const char *)out_vec, &rmp->reply);
237   }));
238   /* *INDENT-ON* */
239   vec_free (out_vec);
240 }
241
242 static void
243 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
244 {
245   vl_api_show_version_reply_t *rmp;
246   int rv = 0;
247   char *vpe_api_get_build_directory (void);
248   char *vpe_api_get_version (void);
249   char *vpe_api_get_build_date (void);
250
251   u32 program_len = strnlen_s ("vpe", 32);
252   u32 version_len = strnlen_s (vpe_api_get_version (), 32);
253   u32 build_date_len = strnlen_s (vpe_api_get_build_date (), 32);
254   u32 build_directory_len = strnlen_s (vpe_api_get_build_directory (), 256);
255
256   u32 n = program_len + version_len + build_date_len + build_directory_len;
257
258   /* *INDENT-OFF* */
259   REPLY_MACRO3(VL_API_SHOW_VERSION_REPLY, n,
260   ({
261     char *p = (char *)&rmp->program;
262     p += vl_api_to_api_string(program_len, "vpe", (vl_api_string_t *)p);
263     p += vl_api_to_api_string(version_len, vpe_api_get_version(), (vl_api_string_t *)p);
264     p += vl_api_to_api_string(build_date_len, vpe_api_get_build_date(), (vl_api_string_t *)p);
265     vl_api_to_api_string(build_directory_len, vpe_api_get_build_directory(), (vl_api_string_t *)p);
266   }));
267   /* *INDENT-ON* */
268 }
269
270 static void
271 get_thread_data (vl_api_thread_data_t * td, int index)
272 {
273   vlib_worker_thread_t *w = vlib_worker_threads + index;
274   td->id = htonl (index);
275   if (w->name)
276     strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
277   if (w->registration)
278     strncpy ((char *) td->type, (char *) w->registration->name,
279              ARRAY_LEN (td->type) - 1);
280   td->pid = htonl (w->lwp);
281   td->cpu_id = htonl (w->cpu_id);
282   td->core = htonl (w->core_id);
283   td->cpu_socket = htonl (w->socket_id);
284 }
285
286 static void
287 vl_api_show_threads_t_handler (vl_api_show_threads_t * mp)
288 {
289   vlib_main_t *vm = vlib_get_main ();
290   int rv = 0, count = 0;
291
292 #if !defined(__powerpc64__)
293   vl_api_registration_t *reg;
294   vl_api_show_threads_reply_t *rmp;
295   vl_api_thread_data_t *td;
296   int i, msg_size = 0;
297   count = vec_len (vlib_worker_threads);
298   if (!count)
299     return;
300
301   msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
302   reg = vl_api_client_index_to_registration (mp->client_index);
303   if (!reg)
304     return;
305
306   rmp = vl_msg_api_alloc (msg_size);
307   clib_memset (rmp, 0, msg_size);
308   rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY);
309   rmp->context = mp->context;
310   rmp->count = htonl (count);
311   td = rmp->thread_data;
312
313   for (i = 0; i < count; i++)
314     {
315       get_thread_data (&td[i], i);
316     }
317
318   vl_api_send_msg (reg, (u8 *) rmp);
319 #else
320
321   /* unimplemented support */
322   rv = -9;
323   clib_warning ("power pc does not support show threads api");
324   /* *INDENT-OFF* */
325   REPLY_MACRO2(VL_API_SHOW_THREADS_REPLY,
326   ({
327     rmp->count = htonl(count);
328   }));
329   /* *INDENT-ON* */
330 #endif
331 }
332
333 static void
334 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
335 {
336   vlib_main_t *vm = vlib_get_main ();
337   vl_api_get_node_index_reply_t *rmp;
338   vlib_node_t *n;
339   int rv = 0;
340   u32 node_index = ~0;
341
342   n = vlib_get_node_by_name (vm, mp->node_name);
343
344   if (n == 0)
345     rv = VNET_API_ERROR_NO_SUCH_NODE;
346   else
347     node_index = n->index;
348
349   /* *INDENT-OFF* */
350   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
351   ({
352     rmp->node_index = ntohl(node_index);
353   }));
354   /* *INDENT-ON* */
355 }
356
357 static void
358 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
359 {
360   vlib_main_t *vm = vlib_get_main ();
361   vl_api_get_next_index_reply_t *rmp;
362   vlib_node_t *node, *next_node;
363   int rv = 0;
364   u32 next_node_index = ~0, next_index = ~0;
365   uword *p;
366
367   node = vlib_get_node_by_name (vm, mp->node_name);
368
369   if (node == 0)
370     {
371       rv = VNET_API_ERROR_NO_SUCH_NODE;
372       goto out;
373     }
374
375   next_node = vlib_get_node_by_name (vm, mp->next_name);
376
377   if (next_node == 0)
378     {
379       rv = VNET_API_ERROR_NO_SUCH_NODE2;
380       goto out;
381     }
382   else
383     next_node_index = next_node->index;
384
385   p = hash_get (node->next_slot_by_node, next_node_index);
386
387   if (p == 0)
388     {
389       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
390       goto out;
391     }
392   else
393     next_index = p[0];
394
395 out:
396   /* *INDENT-OFF* */
397   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
398   ({
399     rmp->next_index = ntohl(next_index);
400   }));
401   /* *INDENT-ON* */
402 }
403
404 static void
405 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
406 {
407   vlib_main_t *vm = vlib_get_main ();
408   vl_api_add_node_next_reply_t *rmp;
409   vlib_node_t *n, *next;
410   int rv = 0;
411   u32 next_index = ~0;
412
413   n = vlib_get_node_by_name (vm, mp->node_name);
414
415   if (n == 0)
416     {
417       rv = VNET_API_ERROR_NO_SUCH_NODE;
418       goto out;
419     }
420
421   next = vlib_get_node_by_name (vm, mp->next_name);
422
423   if (next == 0)
424     rv = VNET_API_ERROR_NO_SUCH_NODE2;
425   else
426     next_index = vlib_node_add_next (vm, n->index, next->index);
427
428 out:
429   /* *INDENT-OFF* */
430   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
431   ({
432     rmp->next_index = ntohl(next_index);
433   }));
434   /* *INDENT-ON* */
435 }
436
437 static void
438 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
439 {
440   int rv = 0;
441   u8 *vector = 0;
442   api_main_t *am = &api_main;
443   vlib_main_t *vm = vlib_get_main ();
444   void *oldheap;
445   vl_api_get_node_graph_reply_t *rmp;
446   static vlib_node_t ***node_dups;
447   static vlib_main_t **stat_vms;
448
449   pthread_mutex_lock (&am->vlib_rp->mutex);
450   oldheap = svm_push_data_heap (am->vlib_rp);
451
452   /*
453    * Keep the number of memcpy ops to a minimum (e.g. 1).
454    */
455   vec_validate (vector, 16384);
456   vec_reset_length (vector);
457
458   vlib_node_get_nodes (vm, 0 /* main threads */ ,
459                        0 /* include stats */ ,
460                        1 /* barrier sync */ ,
461                        &node_dups, &stat_vms);
462   vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
463                                 1 /* include stats */ );
464
465   svm_pop_heap (oldheap);
466   pthread_mutex_unlock (&am->vlib_rp->mutex);
467
468   /* *INDENT-OFF* */
469   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
470   ({
471     rmp->reply_in_shmem = (uword) vector;
472   }));
473   /* *INDENT-ON* */
474 }
475
476 #define BOUNCE_HANDLER(nn)                                              \
477 static void vl_api_##nn##_t_handler (                                   \
478     vl_api_##nn##_t *mp)                                                \
479 {                                                                       \
480     vpe_client_registration_t *reg;                                     \
481     vpe_api_main_t * vam = &vpe_api_main;                               \
482     svm_queue_t * q;                                     \
483                                                                         \
484     /* One registration only... */                                      \
485     pool_foreach(reg, vam->nn##_registrations,                          \
486     ({                                                                  \
487         q = vl_api_client_index_to_input_queue (reg->client_index);     \
488         if (q) {                                                        \
489             /*                                                          \
490              * If the queue is stuffed, turf the msg and complain       \
491              * It's unlikely that the intended recipient is             \
492              * alive; avoid deadlock at all costs.                      \
493              */                                                         \
494             if (q->cursize == q->maxsize) {                             \
495                 clib_warning ("ERROR: receiver queue full, drop msg");  \
496                 vl_msg_api_free (mp);                                   \
497                 return;                                                 \
498             }                                                           \
499             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
500             return;                                                     \
501         }                                                               \
502     }));                                                                \
503     vl_msg_api_free (mp);                                               \
504 }
505
506 static void setup_message_id_table (api_main_t * am);
507
508 /*
509  * vpe_api_hookup
510  * Add vpe's API message handlers to the table.
511  * vlib has already mapped shared memory and
512  * added the client registration handlers.
513  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
514  */
515 static clib_error_t *
516 vpe_api_hookup (vlib_main_t * vm)
517 {
518   api_main_t *am = &api_main;
519
520 #define _(N,n)                                                  \
521     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
522                            vl_api_##n##_t_handler,              \
523                            vl_noop_handler,                     \
524                            vl_api_##n##_t_endian,               \
525                            vl_api_##n##_t_print,                \
526                            sizeof(vl_api_##n##_t), 1);
527   foreach_vpe_api_msg;
528 #undef _
529
530   /*
531    * Trace space for classifier mask+match
532    */
533   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
534   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
535     += 5 * sizeof (u32x4);
536
537   /*
538    * Thread-safe API messages
539    */
540   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
541   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
542
543   /*
544    * Set up the (msg_name, crc, message-id) table
545    */
546   setup_message_id_table (am);
547
548   return 0;
549 }
550
551 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
552
553 clib_error_t *
554 vpe_api_init (vlib_main_t * vm)
555 {
556   vpe_api_main_t *am = &vpe_api_main;
557
558   am->vlib_main = vm;
559   am->vnet_main = vnet_get_main ();
560 #define _(a)                                                    \
561   am->a##_registration_hash = hash_create (0, sizeof (uword));
562   foreach_registration_hash;
563 #undef _
564
565   vl_set_memory_region_name ("/vpe-api");
566   vl_mem_api_enable_disable (vm, 1 /* enable it */ );
567
568   return 0;
569 }
570
571 static clib_error_t *
572 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
573 {
574   u8 *chroot_path;
575   u64 baseva, size, pvt_heap_size;
576   int uid, gid, rv;
577   const int max_buf_size = 4096;
578   char *s, *buf;
579   struct passwd _pw, *pw;
580   struct group _grp, *grp;
581   clib_error_t *e;
582   buf = vec_new (char, 128);
583   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
584     {
585       if (unformat (input, "prefix %s", &chroot_path))
586         {
587           vec_add1 (chroot_path, 0);
588           vl_set_memory_root_path ((char *) chroot_path);
589         }
590       else if (unformat (input, "uid %d", &uid))
591         vl_set_memory_uid (uid);
592       else if (unformat (input, "gid %d", &gid))
593         vl_set_memory_gid (gid);
594       else if (unformat (input, "baseva %llx", &baseva))
595         vl_set_global_memory_baseva (baseva);
596       else if (unformat (input, "global-size %lldM", &size))
597         vl_set_global_memory_size (size * (1ULL << 20));
598       else if (unformat (input, "global-size %lldG", &size))
599         vl_set_global_memory_size (size * (1ULL << 30));
600       else if (unformat (input, "global-size %lld", &size))
601         vl_set_global_memory_size (size);
602       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
603         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
604       else if (unformat (input, "global-pvt-heap-size size %lld",
605                          &pvt_heap_size))
606         vl_set_global_pvt_heap_size (pvt_heap_size);
607       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
608         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
609       else if (unformat (input, "api-pvt-heap-size size %lld",
610                          &pvt_heap_size))
611         vl_set_api_pvt_heap_size (pvt_heap_size);
612       else if (unformat (input, "api-size %lldM", &size))
613         vl_set_api_memory_size (size * (1ULL << 20));
614       else if (unformat (input, "api-size %lldG", &size))
615         vl_set_api_memory_size (size * (1ULL << 30));
616       else if (unformat (input, "api-size %lld", &size))
617         vl_set_api_memory_size (size);
618       else if (unformat (input, "uid %s", &s))
619         {
620           /* lookup the username */
621           pw = NULL;
622           while (((rv =
623                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
624                  && (vec_len (buf) <= max_buf_size))
625             {
626               vec_resize (buf, vec_len (buf) * 2);
627             }
628           if (rv < 0)
629             {
630               e = clib_error_return_code (0, rv,
631                                           CLIB_ERROR_ERRNO_VALID |
632                                           CLIB_ERROR_FATAL,
633                                           "cannot fetch username %s", s);
634               vec_free (s);
635               vec_free (buf);
636               return e;
637             }
638           if (pw == NULL)
639             {
640               e =
641                 clib_error_return_fatal (0, "username %s does not exist", s);
642               vec_free (s);
643               vec_free (buf);
644               return e;
645             }
646           vec_free (s);
647           vl_set_memory_uid (pw->pw_uid);
648         }
649       else if (unformat (input, "gid %s", &s))
650         {
651           /* lookup the group name */
652           grp = NULL;
653           while (((rv =
654                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
655                  && (vec_len (buf) <= max_buf_size))
656             {
657               vec_resize (buf, vec_len (buf) * 2);
658             }
659           if (rv != 0)
660             {
661               e = clib_error_return_code (0, rv,
662                                           CLIB_ERROR_ERRNO_VALID |
663                                           CLIB_ERROR_FATAL,
664                                           "cannot fetch group %s", s);
665               vec_free (s);
666               vec_free (buf);
667               return e;
668             }
669           if (grp == NULL)
670             {
671               e = clib_error_return_fatal (0, "group %s does not exist", s);
672               vec_free (s);
673               vec_free (buf);
674               return e;
675             }
676           vec_free (s);
677           vec_free (buf);
678           vl_set_memory_gid (grp->gr_gid);
679         }
680       else
681         return clib_error_return (0, "unknown input `%U'",
682                                   format_unformat_error, input);
683     }
684   return 0;
685 }
686
687 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
688
689 void *
690 get_unformat_vnet_sw_interface (void)
691 {
692   return (void *) &unformat_vnet_sw_interface;
693 }
694
695 static u8 *
696 format_arp_event (u8 * s, va_list * args)
697 {
698   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
699
700   s = format (s, "pid %d: ", ntohl (event->pid));
701   s = format (s, "resolution for %U", format_ip4_address, &event->address);
702   return s;
703 }
704
705 static u8 *
706 format_nd_event (u8 * s, va_list * args)
707 {
708   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
709
710   s = format (s, "pid %d: ", ntohl (event->pid));
711   s = format (s, "resolution for %U", format_ip6_address, event->address);
712   return s;
713 }
714
715 static clib_error_t *
716 show_ip_arp_nd_events_fn (vlib_main_t * vm,
717                           unformat_input_t * input, vlib_cli_command_t * cmd)
718 {
719   vpe_api_main_t *am = &vpe_api_main;
720   vl_api_ip4_arp_event_t *arp_event;
721   vl_api_ip6_nd_event_t *nd_event;
722
723   if (pool_elts (am->arp_events) == 0 && pool_elts (am->nd_events) == 0 &&
724       pool_elts (am->wc_ip4_arp_events_registrations) == 0 &&
725       pool_elts (am->wc_ip6_nd_events_registrations) == 0)
726     {
727       vlib_cli_output (vm, "No active arp or nd event registrations");
728       return 0;
729     }
730
731   /* *INDENT-OFF* */
732   pool_foreach (arp_event, am->arp_events,
733   ({
734     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
735   }));
736
737   vpe_client_registration_t *reg;
738   pool_foreach(reg, am->wc_ip4_arp_events_registrations,
739   ({
740     vlib_cli_output (vm, "pid %d: bd mac/ip4 binding events",
741                      ntohl (reg->client_pid));
742   }));
743
744   pool_foreach (nd_event, am->nd_events,
745   ({
746     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
747   }));
748
749   pool_foreach(reg, am->wc_ip6_nd_events_registrations,
750   ({
751     vlib_cli_output (vm, "pid %d: bd mac/ip6 binding events",
752                      ntohl (reg->client_pid));
753   }));
754   /* *INDENT-ON* */
755
756   return 0;
757 }
758
759 /* *INDENT-OFF* */
760 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
761   .path = "show arp-nd-event registrations",
762   .function = show_ip_arp_nd_events_fn,
763   .short_help = "Show ip4 arp and ip6 nd event registrations",
764 };
765 /* *INDENT-ON* */
766
767 #define vl_msg_name_crc_list
768 #include <vpp/api/vpe_all_api_h.h>
769 #undef vl_msg_name_crc_list
770
771 static void
772 setup_message_id_table (api_main_t * am)
773 {
774 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
775   foreach_vl_msg_name_crc_memclnt;
776   foreach_vl_msg_name_crc_vpe;
777 #undef _
778
779 #define vl_api_version_tuple(n,mj, mi, p) \
780   vl_msg_api_add_version (am, #n, mj, mi, p);
781 #include <vpp/api/vpe_all_api_h.h>
782 #undef vl_api_version_tuple
783 }
784
785
786 /*
787  * fd.io coding-style-patch-verification: ON
788  *
789  * Local Variables:
790  * eval: (c-set-style "gnu")
791  * End:
792  */