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