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/types.h>
63 #include <vpp/api/vpe.api_enum.h>
64 #include <vpp/api/vpe.api_types.h>
66 static u16 msg_id_base;
67 #define REPLY_MSG_ID_BASE msg_id_base
68 #include <vlibapi/api_helper_macros.h>
71 #define QUOTE(x) QUOTE_(x)
75 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
76 RESOLVE_IP6_ADD_DEL_ROUTE,
79 extern vpe_api_main_t vpe_api_main;
81 /* Clean up all registrations belonging to the indicated client */
83 memclnt_delete_callback (u32 client_index)
85 vpe_api_main_t *vam = &vpe_api_main;
86 vpe_client_registration_t *rp;
90 p = hash_get (vam->a##_registration_hash, client_index); \
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); \
96 foreach_registration_hash;
101 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
104 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
106 vl_api_show_version_reply_t *rmp;
108 char *vpe_api_get_build_directory (void);
109 char *vpe_api_get_version (void);
110 char *vpe_api_get_build_date (void);
112 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
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);
125 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t *mp)
128 vl_api_show_vpe_system_time_reply_t *rmp;
130 VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
131 ({ rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ()); }));
135 show_log_details (vl_api_registration_t * reg, u32 context,
137 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
141 vl_api_log_details_t *rmp;
143 clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
145 clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
146 msg_size = sizeof (*rmp) + class_len + message_len;
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);
152 rmp->context = context;
153 rmp->timestamp = clib_host_to_net_f64 (timestamp);
154 rmp->level = htonl (*level);
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]);
162 vl_api_send_msg (reg, (u8 *) rmp);
166 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
170 vlib_log_main_t *lm = &log_main;
172 int i = last_log_entry ();
173 int count = lm->count;
174 f64 time_offset, start_time;
175 vl_api_registration_t *reg;
177 reg = vl_api_client_index_to_registration (mp->client_index);
181 start_time = clib_net_to_host_f64 (mp->start_timestamp);
183 time_offset = (f64) lm->time_zero_timeval.tv_sec
184 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
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),
194 i = (i + 1) % lm->size;
199 #define BOUNCE_HANDLER(nn) \
200 static void vl_api_##nn##_t_handler ( \
201 vl_api_##nn##_t *mp) \
203 vpe_client_registration_t *reg; \
204 vpe_api_main_t * vam = &vpe_api_main; \
207 /* One registration only... */ \
208 pool_foreach (reg, vam->nn##_registrations) \
210 q = vl_api_client_index_to_input_queue (reg->client_index); \
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. \
217 if (q->cursize == q->maxsize) { \
218 clib_warning ("ERROR: receiver queue full, drop msg"); \
219 vl_msg_api_free (mp); \
222 vl_msg_api_send_shmem (q, (u8 *)&mp); \
226 vl_msg_api_free (mp); \
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()
236 #include <vpp/api/vpe.api.c>
237 static clib_error_t *
238 vpe_api_hookup (vlib_main_t * vm)
240 api_main_t *am = vlibapi_get_main ();
243 * Set up the (msg_name, crc, message-id) table
245 msg_id_base = setup_message_id_table ();
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,
255 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
258 vpe_api_init (vlib_main_t * vm)
260 vpe_api_main_t *am = &vpe_api_main;
263 am->vnet_main = vnet_get_main ();
265 am->a##_registration_hash = hash_create (0, sizeof (uword));
266 foreach_registration_hash;
269 vl_set_memory_region_name ("/vpe-api");
270 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
275 static clib_error_t *
276 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
279 u64 baseva, size, pvt_heap_size;
281 const int max_buf_size = 4096;
283 struct passwd _pw, *pw;
284 struct group _grp, *grp;
286 buf = vec_new (char, 128);
287 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
289 if (unformat (input, "prefix %s", &chroot_path))
291 vec_add1 (chroot_path, 0);
292 vl_set_memory_root_path ((char *) chroot_path);
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",
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",
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))
324 /* lookup the username */
327 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
328 && (vec_len (buf) <= max_buf_size))
330 vec_resize (buf, vec_len (buf) * 2);
334 e = clib_error_return_code (0, rv,
335 CLIB_ERROR_ERRNO_VALID |
337 "cannot fetch username %s", s);
345 clib_error_return_fatal (0, "username %s does not exist", s);
351 vl_set_memory_uid (pw->pw_uid);
353 else if (unformat (input, "gid %s", &s))
355 /* lookup the group name */
358 getgrnam_r (s, &_grp, buf, vec_len (buf),
360 && (vec_len (buf) <= max_buf_size))
362 vec_resize (buf, vec_len (buf) * 2);
366 e = clib_error_return_code (0, rv,
367 CLIB_ERROR_ERRNO_VALID |
369 "cannot fetch group %s", s);
376 e = clib_error_return_fatal (0, "group %s does not exist", s);
383 vl_set_memory_gid (grp->gr_gid);
386 return clib_error_return (0, "unknown input `%U'",
387 format_unformat_error, input);
392 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
395 get_unformat_vnet_sw_interface (void)
397 return (void *) &unformat_vnet_sw_interface;
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "gnu")