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