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 _ (CONTROL_PING, control_ping) \
81 _ (SHOW_VERSION, show_version) \
82 _ (SHOW_VPE_SYSTEM_TIME, show_vpe_system_time) \
83 _ (LOG_DUMP, log_dump)
86 #define QUOTE(x) QUOTE_(x)
90 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
91 RESOLVE_IP6_ADD_DEL_ROUTE,
94 extern vpe_api_main_t vpe_api_main;
96 /* Clean up all registrations belonging to the indicated client */
98 memclnt_delete_callback (u32 client_index)
100 vpe_api_main_t *vam = &vpe_api_main;
101 vpe_client_registration_t *rp;
105 p = hash_get (vam->a##_registration_hash, client_index); \
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); \
111 foreach_registration_hash;
116 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
119 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
121 vl_api_control_ping_reply_t *rmp;
125 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
127 rmp->vpe_pid = ntohl (getpid());
133 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
135 vl_api_show_version_reply_t *rmp;
137 char *vpe_api_get_build_directory (void);
138 char *vpe_api_get_version (void);
139 char *vpe_api_get_build_date (void);
142 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
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);
156 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t *mp)
159 vl_api_show_vpe_system_time_reply_t *rmp;
162 VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
163 ({ rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ()); }));
168 show_log_details (vl_api_registration_t * reg, u32 context,
170 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
174 vl_api_log_details_t *rmp;
176 clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
178 clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
179 msg_size = sizeof (*rmp) + class_len + message_len;
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);
185 rmp->context = context;
186 rmp->timestamp = clib_host_to_net_f64 (timestamp);
187 rmp->level = htonl (*level);
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]);
195 vl_api_send_msg (reg, (u8 *) rmp);
199 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
203 vlib_log_main_t *lm = &log_main;
205 int i = last_log_entry ();
206 int count = lm->count;
207 f64 time_offset, start_time;
208 vl_api_registration_t *reg;
210 reg = vl_api_client_index_to_registration (mp->client_index);
214 start_time = clib_net_to_host_f64 (mp->start_timestamp);
216 time_offset = (f64) lm->time_zero_timeval.tv_sec
217 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
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),
227 i = (i + 1) % lm->size;
232 #define BOUNCE_HANDLER(nn) \
233 static void vl_api_##nn##_t_handler ( \
234 vl_api_##nn##_t *mp) \
236 vpe_client_registration_t *reg; \
237 vpe_api_main_t * vam = &vpe_api_main; \
240 /* One registration only... */ \
241 pool_foreach (reg, vam->nn##_registrations) \
243 q = vl_api_client_index_to_input_queue (reg->client_index); \
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. \
250 if (q->cursize == q->maxsize) { \
251 clib_warning ("ERROR: receiver queue full, drop msg"); \
252 vl_msg_api_free (mp); \
255 vl_msg_api_send_shmem (q, (u8 *)&mp); \
259 vl_msg_api_free (mp); \
262 static void setup_message_id_table (api_main_t * am);
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()
271 static clib_error_t *
272 vpe_api_hookup (vlib_main_t * vm)
274 api_main_t *am = vlibapi_get_main ();
277 vl_msg_api_set_handlers(VL_API_##N, #n, \
278 vl_api_##n##_t_handler, \
280 vl_api_##n##_t_endian, \
281 vl_api_##n##_t_print, \
282 sizeof(vl_api_##n##_t), 1);
287 * Trace space for classifier mask+match
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 +=
294 * Thread-safe API messages
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;
301 * Set up the (msg_name, crc, message-id) table
303 setup_message_id_table (am);
308 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
311 vpe_api_init (vlib_main_t * vm)
313 vpe_api_main_t *am = &vpe_api_main;
316 am->vnet_main = vnet_get_main ();
318 am->a##_registration_hash = hash_create (0, sizeof (uword));
319 foreach_registration_hash;
322 vl_set_memory_region_name ("/vpe-api");
323 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
328 static clib_error_t *
329 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
332 u64 baseva, size, pvt_heap_size;
334 const int max_buf_size = 4096;
336 struct passwd _pw, *pw;
337 struct group _grp, *grp;
339 buf = vec_new (char, 128);
340 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
342 if (unformat (input, "prefix %s", &chroot_path))
344 vec_add1 (chroot_path, 0);
345 vl_set_memory_root_path ((char *) chroot_path);
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",
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",
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))
377 /* lookup the username */
380 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
381 && (vec_len (buf) <= max_buf_size))
383 vec_resize (buf, vec_len (buf) * 2);
387 e = clib_error_return_code (0, rv,
388 CLIB_ERROR_ERRNO_VALID |
390 "cannot fetch username %s", s);
398 clib_error_return_fatal (0, "username %s does not exist", s);
404 vl_set_memory_uid (pw->pw_uid);
406 else if (unformat (input, "gid %s", &s))
408 /* lookup the group name */
411 getgrnam_r (s, &_grp, buf, vec_len (buf),
413 && (vec_len (buf) <= max_buf_size))
415 vec_resize (buf, vec_len (buf) * 2);
419 e = clib_error_return_code (0, rv,
420 CLIB_ERROR_ERRNO_VALID |
422 "cannot fetch group %s", s);
429 e = clib_error_return_fatal (0, "group %s does not exist", s);
436 vl_set_memory_gid (grp->gr_gid);
439 return clib_error_return (0, "unknown input `%U'",
440 format_unformat_error, input);
445 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
448 get_unformat_vnet_sw_interface (void)
450 return (void *) &unformat_vnet_sw_interface;
453 #define vl_msg_name_crc_list
454 #include <vpp/api/vpe_all_api_h.h>
455 #undef vl_msg_name_crc_list
458 setup_message_id_table (api_main_t * am)
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;
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
473 * fd.io coding-style-patch-verification: ON
476 * eval: (c-set-style "gnu")