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