misc: api move continued
[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 #include <vnet/ip/ip.api_enum.h>
65
66 #define vl_typedefs             /* define message structures */
67 #include <vpp/api/vpe_all_api_h.h>
68 #undef vl_typedefs
69 #define vl_endianfun            /* define message structures */
70 #include <vpp/api/vpe_all_api_h.h>
71 #undef vl_endianfun
72 /* instantiate all the print functions we know about */
73 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
74 #define vl_printfun
75 #include <vpp/api/vpe_all_api_h.h>
76 #undef vl_printfun
77 #include <vlibapi/api_helper_macros.h>
78
79 #define foreach_vpe_api_msg                                                   \
80   _ (SHOW_VERSION, show_version)                                              \
81   _ (SHOW_VPE_SYSTEM_TIME, show_vpe_system_time)                              \
82   _ (LOG_DUMP, log_dump)
83
84 #define QUOTE_(x) #x
85 #define QUOTE(x) QUOTE_(x)
86
87 typedef enum
88 {
89   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
90   RESOLVE_IP6_ADD_DEL_ROUTE,
91 } resolve_t;
92
93 extern vpe_api_main_t vpe_api_main;
94
95 /* Clean up all registrations belonging to the indicated client */
96 static clib_error_t *
97 memclnt_delete_callback (u32 client_index)
98 {
99   vpe_api_main_t *vam = &vpe_api_main;
100   vpe_client_registration_t *rp;
101   uword *p;
102
103 #define _(a)                                                    \
104     p = hash_get (vam->a##_registration_hash, client_index);    \
105     if (p) {                                                    \
106         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
107         pool_put (vam->a##_registrations, rp);                  \
108         hash_unset (vam->a##_registration_hash, client_index);  \
109     }
110   foreach_registration_hash;
111 #undef _
112   return 0;
113 }
114
115 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
116
117 static void
118 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
119 {
120   vl_api_show_version_reply_t *rmp;
121   int rv = 0;
122   char *vpe_api_get_build_directory (void);
123   char *vpe_api_get_version (void);
124   char *vpe_api_get_build_date (void);
125
126   /* *INDENT-OFF* */
127   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
128   ({
129     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
130     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
131              ARRAY_LEN(rmp->build_directory)-1);
132     strncpy ((char *) rmp->version, vpe_api_get_version(),
133              ARRAY_LEN(rmp->version)-1);
134     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
135              ARRAY_LEN(rmp->build_date)-1);
136   }));
137   /* *INDENT-ON* */
138 }
139
140 static void
141 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t *mp)
142 {
143   int rv = 0;
144   vl_api_show_vpe_system_time_reply_t *rmp;
145   /* *INDENT-OFF* */
146   REPLY_MACRO2 (
147     VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
148     ({ rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ()); }));
149   /* *INDENT-ON* */
150 }
151
152 static void
153 show_log_details (vl_api_registration_t * reg, u32 context,
154                   f64 timestamp,
155                   vl_api_log_level_t * level, u8 * msg_class, u8 * message)
156 {
157   u32 msg_size;
158
159   vl_api_log_details_t *rmp;
160   int class_len =
161     clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
162   int message_len =
163     clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
164   msg_size = sizeof (*rmp) + class_len + message_len;
165
166   rmp = vl_msg_api_alloc (msg_size);
167   clib_memset (rmp, 0, msg_size);
168   rmp->_vl_msg_id = ntohs (VL_API_LOG_DETAILS);
169
170   rmp->context = context;
171   rmp->timestamp = clib_host_to_net_f64 (timestamp);
172   rmp->level = htonl (*level);
173
174   memcpy (rmp->msg_class, msg_class, class_len - 1);
175   memcpy (rmp->message, message, message_len - 1);
176   /* enforced by memset() above */
177   ASSERT (0 == rmp->msg_class[class_len - 1]);
178   ASSERT (0 == rmp->message[message_len - 1]);
179
180   vl_api_send_msg (reg, (u8 *) rmp);
181 }
182
183 static void
184 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
185 {
186
187   /* from log.c */
188   vlib_log_main_t *lm = &log_main;
189   vlib_log_entry_t *e;
190   int i = last_log_entry ();
191   int count = lm->count;
192   f64 time_offset, start_time;
193   vl_api_registration_t *reg;
194
195   reg = vl_api_client_index_to_registration (mp->client_index);
196   if (reg == 0)
197     return;
198
199   start_time = clib_net_to_host_f64 (mp->start_timestamp);
200
201   time_offset = (f64) lm->time_zero_timeval.tv_sec
202     + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
203
204   while (count--)
205     {
206       e = vec_elt_at_index (lm->entries, i);
207       if (start_time <= e->timestamp + time_offset)
208         show_log_details (reg, mp->context, e->timestamp + time_offset,
209                           (vl_api_log_level_t *) & e->level,
210                           format (0, "%U", format_vlib_log_class, e->class),
211                           e->string);
212       i = (i + 1) % lm->size;
213     }
214
215 }
216
217 #define BOUNCE_HANDLER(nn)                                              \
218 static void vl_api_##nn##_t_handler (                                   \
219     vl_api_##nn##_t *mp)                                                \
220 {                                                                       \
221     vpe_client_registration_t *reg;                                     \
222     vpe_api_main_t * vam = &vpe_api_main;                               \
223     svm_queue_t * q;                                     \
224                                                                         \
225     /* One registration only... */                                      \
226     pool_foreach (reg, vam->nn##_registrations)                          \
227     ({                                                                  \
228         q = vl_api_client_index_to_input_queue (reg->client_index);     \
229         if (q) {                                                        \
230             /*                                                          \
231              * If the queue is stuffed, turf the msg and complain       \
232              * It's unlikely that the intended recipient is             \
233              * alive; avoid deadlock at all costs.                      \
234              */                                                         \
235             if (q->cursize == q->maxsize) {                             \
236                 clib_warning ("ERROR: receiver queue full, drop msg");  \
237                 vl_msg_api_free (mp);                                   \
238                 return;                                                 \
239             }                                                           \
240             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
241             return;                                                     \
242         }                                                               \
243     }));                                                                \
244     vl_msg_api_free (mp);                                               \
245 }
246
247 static void setup_message_id_table (api_main_t * am);
248
249 /*
250  * vpe_api_hookup
251  * Add vpe's API message handlers to the table.
252  * vlib has already mapped shared memory and
253  * added the client registration handlers.
254  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
255  */
256 static clib_error_t *
257 vpe_api_hookup (vlib_main_t * vm)
258 {
259   api_main_t *am = vlibapi_get_main ();
260
261 #define _(N,n)                                                  \
262     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
263                            vl_api_##n##_t_handler,              \
264                            vl_noop_handler,                     \
265                            vl_api_##n##_t_endian,               \
266                            vl_api_##n##_t_print,                \
267                            sizeof(vl_api_##n##_t), 1);
268   foreach_vpe_api_msg;
269 #undef _
270
271   /*
272    * Trace space for classifier mask+match
273    */
274   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
275   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size +=
276     5 * sizeof (u32x4);
277
278   /*
279    * Thread-safe API messages
280    */
281   am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
282
283   /*
284    * Set up the (msg_name, crc, message-id) table
285    */
286   setup_message_id_table (am);
287
288   return 0;
289 }
290
291 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
292
293 clib_error_t *
294 vpe_api_init (vlib_main_t * vm)
295 {
296   vpe_api_main_t *am = &vpe_api_main;
297
298   am->vlib_main = vm;
299   am->vnet_main = vnet_get_main ();
300 #define _(a)                                                    \
301   am->a##_registration_hash = hash_create (0, sizeof (uword));
302   foreach_registration_hash;
303 #undef _
304
305   vl_set_memory_region_name ("/vpe-api");
306   vl_mem_api_enable_disable (vm, 1 /* enable it */ );
307
308   return 0;
309 }
310
311 static clib_error_t *
312 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
313 {
314   u8 *chroot_path;
315   u64 baseva, size, pvt_heap_size;
316   int uid, gid, rv;
317   const int max_buf_size = 4096;
318   char *s, *buf;
319   struct passwd _pw, *pw;
320   struct group _grp, *grp;
321   clib_error_t *e;
322   buf = vec_new (char, 128);
323   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
324     {
325       if (unformat (input, "prefix %s", &chroot_path))
326         {
327           vec_add1 (chroot_path, 0);
328           vl_set_memory_root_path ((char *) chroot_path);
329         }
330       else if (unformat (input, "uid %d", &uid))
331         vl_set_memory_uid (uid);
332       else if (unformat (input, "gid %d", &gid))
333         vl_set_memory_gid (gid);
334       else if (unformat (input, "baseva %llx", &baseva))
335         vl_set_global_memory_baseva (baseva);
336       else if (unformat (input, "global-size %lldM", &size))
337         vl_set_global_memory_size (size * (1ULL << 20));
338       else if (unformat (input, "global-size %lldG", &size))
339         vl_set_global_memory_size (size * (1ULL << 30));
340       else if (unformat (input, "global-size %lld", &size))
341         vl_set_global_memory_size (size);
342       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
343         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
344       else if (unformat (input, "global-pvt-heap-size size %lld",
345                          &pvt_heap_size))
346         vl_set_global_pvt_heap_size (pvt_heap_size);
347       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
348         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
349       else if (unformat (input, "api-pvt-heap-size size %lld",
350                          &pvt_heap_size))
351         vl_set_api_pvt_heap_size (pvt_heap_size);
352       else if (unformat (input, "api-size %lldM", &size))
353         vl_set_api_memory_size (size * (1ULL << 20));
354       else if (unformat (input, "api-size %lldG", &size))
355         vl_set_api_memory_size (size * (1ULL << 30));
356       else if (unformat (input, "api-size %lld", &size))
357         vl_set_api_memory_size (size);
358       else if (unformat (input, "uid %s", &s))
359         {
360           /* lookup the username */
361           pw = NULL;
362           while (((rv =
363                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
364                  && (vec_len (buf) <= max_buf_size))
365             {
366               vec_resize (buf, vec_len (buf) * 2);
367             }
368           if (rv < 0)
369             {
370               e = clib_error_return_code (0, rv,
371                                           CLIB_ERROR_ERRNO_VALID |
372                                           CLIB_ERROR_FATAL,
373                                           "cannot fetch username %s", s);
374               vec_free (s);
375               vec_free (buf);
376               return e;
377             }
378           if (pw == NULL)
379             {
380               e =
381                 clib_error_return_fatal (0, "username %s does not exist", s);
382               vec_free (s);
383               vec_free (buf);
384               return e;
385             }
386           vec_free (s);
387           vl_set_memory_uid (pw->pw_uid);
388         }
389       else if (unformat (input, "gid %s", &s))
390         {
391           /* lookup the group name */
392           grp = NULL;
393           while (((rv =
394                    getgrnam_r (s, &_grp, buf, vec_len (buf),
395                                &grp)) == ERANGE)
396                  && (vec_len (buf) <= max_buf_size))
397             {
398               vec_resize (buf, vec_len (buf) * 2);
399             }
400           if (rv != 0)
401             {
402               e = clib_error_return_code (0, rv,
403                                           CLIB_ERROR_ERRNO_VALID |
404                                           CLIB_ERROR_FATAL,
405                                           "cannot fetch group %s", s);
406               vec_free (s);
407               vec_free (buf);
408               return e;
409             }
410           if (grp == NULL)
411             {
412               e = clib_error_return_fatal (0, "group %s does not exist", s);
413               vec_free (s);
414               vec_free (buf);
415               return e;
416             }
417           vec_free (s);
418           vec_free (buf);
419           vl_set_memory_gid (grp->gr_gid);
420         }
421       else
422         return clib_error_return (0, "unknown input `%U'",
423                                   format_unformat_error, input);
424     }
425   return 0;
426 }
427
428 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
429
430 void *
431 get_unformat_vnet_sw_interface (void)
432 {
433   return (void *) &unformat_vnet_sw_interface;
434 }
435
436 #define vl_msg_name_crc_list
437 #include <vpp/api/vpe_all_api_h.h>
438 #undef vl_msg_name_crc_list
439
440 static void
441 setup_message_id_table (api_main_t * am)
442 {
443 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
444   foreach_vl_msg_name_crc_memclnt;
445   foreach_vl_msg_name_crc_vpe;
446 #undef _
447
448 #define vl_api_version_tuple(n,mj, mi, p) \
449   vl_msg_api_add_version (am, #n, mj, mi, p);
450 #include <vpp/api/vpe_all_api_h.h>
451 #undef vl_api_version_tuple
452 }
453
454
455 /*
456  * fd.io coding-style-patch-verification: ON
457  *
458  * Local Variables:
459  * eval: (c-set-style "gnu")
460  * End:
461  */