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