2 *------------------------------------------------------------------
3 * api.c - message handler registration
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
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>
47 #include <vnet/api_errno.h>
48 #include <vnet/vnet.h>
51 #include <vlib/vlib.h>
52 #include <vlib/unix/unix.h>
53 #include <vlibapi/api.h>
54 #include <vlibmemory/api.h>
57 #undef __included_bihash_template_h__
59 #include <vnet/ip/format.h>
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>
66 #define vl_typedefs /* define message structures */
67 #include <vpp/api/vpe_all_api_h.h>
69 #define vl_endianfun /* define message structures */
70 #include <vpp/api/vpe_all_api_h.h>
72 /* instantiate all the print functions we know about */
73 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
75 #include <vpp/api/vpe_all_api_h.h>
77 #include <vlibapi/api_helper_macros.h>
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)
85 #define QUOTE(x) QUOTE_(x)
89 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
90 RESOLVE_IP6_ADD_DEL_ROUTE,
93 extern vpe_api_main_t vpe_api_main;
95 /* Clean up all registrations belonging to the indicated client */
97 memclnt_delete_callback (u32 client_index)
99 vpe_api_main_t *vam = &vpe_api_main;
100 vpe_client_registration_t *rp;
104 p = hash_get (vam->a##_registration_hash, client_index); \
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); \
110 foreach_registration_hash;
115 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
118 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
120 vl_api_show_version_reply_t *rmp;
122 char *vpe_api_get_build_directory (void);
123 char *vpe_api_get_version (void);
124 char *vpe_api_get_build_date (void);
127 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
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);
141 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t *mp)
144 vl_api_show_vpe_system_time_reply_t *rmp;
147 VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
148 ({ rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ()); }));
153 show_log_details (vl_api_registration_t * reg, u32 context,
155 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
159 vl_api_log_details_t *rmp;
161 clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
163 clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
164 msg_size = sizeof (*rmp) + class_len + message_len;
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);
170 rmp->context = context;
171 rmp->timestamp = clib_host_to_net_f64 (timestamp);
172 rmp->level = htonl (*level);
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]);
180 vl_api_send_msg (reg, (u8 *) rmp);
184 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
188 vlib_log_main_t *lm = &log_main;
190 int i = last_log_entry ();
191 int count = lm->count;
192 f64 time_offset, start_time;
193 vl_api_registration_t *reg;
195 reg = vl_api_client_index_to_registration (mp->client_index);
199 start_time = clib_net_to_host_f64 (mp->start_timestamp);
201 time_offset = (f64) lm->time_zero_timeval.tv_sec
202 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
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),
212 i = (i + 1) % lm->size;
217 #define BOUNCE_HANDLER(nn) \
218 static void vl_api_##nn##_t_handler ( \
219 vl_api_##nn##_t *mp) \
221 vpe_client_registration_t *reg; \
222 vpe_api_main_t * vam = &vpe_api_main; \
225 /* One registration only... */ \
226 pool_foreach (reg, vam->nn##_registrations) \
228 q = vl_api_client_index_to_input_queue (reg->client_index); \
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. \
235 if (q->cursize == q->maxsize) { \
236 clib_warning ("ERROR: receiver queue full, drop msg"); \
237 vl_msg_api_free (mp); \
240 vl_msg_api_send_shmem (q, (u8 *)&mp); \
244 vl_msg_api_free (mp); \
247 static void setup_message_id_table (api_main_t * am);
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()
256 static clib_error_t *
257 vpe_api_hookup (vlib_main_t * vm)
259 api_main_t *am = vlibapi_get_main ();
262 vl_msg_api_set_handlers(VL_API_##N, #n, \
263 vl_api_##n##_t_handler, \
265 vl_api_##n##_t_endian, \
266 vl_api_##n##_t_print, \
267 sizeof(vl_api_##n##_t), 1);
272 * Trace space for classifier mask+match
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 +=
279 * Thread-safe API messages
281 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
284 * Set up the (msg_name, crc, message-id) table
286 setup_message_id_table (am);
291 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
294 vpe_api_init (vlib_main_t * vm)
296 vpe_api_main_t *am = &vpe_api_main;
299 am->vnet_main = vnet_get_main ();
301 am->a##_registration_hash = hash_create (0, sizeof (uword));
302 foreach_registration_hash;
305 vl_set_memory_region_name ("/vpe-api");
306 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
311 static clib_error_t *
312 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
315 u64 baseva, size, pvt_heap_size;
317 const int max_buf_size = 4096;
319 struct passwd _pw, *pw;
320 struct group _grp, *grp;
322 buf = vec_new (char, 128);
323 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
325 if (unformat (input, "prefix %s", &chroot_path))
327 vec_add1 (chroot_path, 0);
328 vl_set_memory_root_path ((char *) chroot_path);
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",
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",
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))
360 /* lookup the username */
363 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
364 && (vec_len (buf) <= max_buf_size))
366 vec_resize (buf, vec_len (buf) * 2);
370 e = clib_error_return_code (0, rv,
371 CLIB_ERROR_ERRNO_VALID |
373 "cannot fetch username %s", s);
381 clib_error_return_fatal (0, "username %s does not exist", s);
387 vl_set_memory_uid (pw->pw_uid);
389 else if (unformat (input, "gid %s", &s))
391 /* lookup the group name */
394 getgrnam_r (s, &_grp, buf, vec_len (buf),
396 && (vec_len (buf) <= max_buf_size))
398 vec_resize (buf, vec_len (buf) * 2);
402 e = clib_error_return_code (0, rv,
403 CLIB_ERROR_ERRNO_VALID |
405 "cannot fetch group %s", s);
412 e = clib_error_return_fatal (0, "group %s does not exist", s);
419 vl_set_memory_gid (grp->gr_gid);
422 return clib_error_return (0, "unknown input `%U'",
423 format_unformat_error, input);
428 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
431 get_unformat_vnet_sw_interface (void)
433 return (void *) &unformat_vnet_sw_interface;
436 #define vl_msg_name_crc_list
437 #include <vpp/api/vpe_all_api_h.h>
438 #undef vl_msg_name_crc_list
441 setup_message_id_table (api_main_t * am)
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;
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
456 * fd.io coding-style-patch-verification: ON
459 * eval: (c-set-style "gnu")