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