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