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);
113 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
115 strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
116 strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
117 ARRAY_LEN(rmp->build_directory)-1);
118 strncpy ((char *) rmp->version, vpe_api_get_version(),
119 ARRAY_LEN(rmp->version)-1);
120 strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
121 ARRAY_LEN(rmp->build_date)-1);
127 vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t *mp)
130 vl_api_show_vpe_system_time_reply_t *rmp;
133 VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
134 ({ rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ()); }));
139 show_log_details (vl_api_registration_t * reg, u32 context,
141 vl_api_log_level_t * level, u8 * msg_class, u8 * message)
145 vl_api_log_details_t *rmp;
147 clib_min (vec_len (msg_class) + 1, ARRAY_LEN (rmp->msg_class));
149 clib_min (vec_len (message) + 1, ARRAY_LEN (rmp->message));
150 msg_size = sizeof (*rmp) + class_len + message_len;
152 rmp = vl_msg_api_alloc (msg_size);
153 clib_memset (rmp, 0, msg_size);
154 rmp->_vl_msg_id = ntohs (VL_API_LOG_DETAILS + msg_id_base);
156 rmp->context = context;
157 rmp->timestamp = clib_host_to_net_f64 (timestamp);
158 rmp->level = htonl (*level);
160 memcpy (rmp->msg_class, msg_class, class_len - 1);
161 memcpy (rmp->message, message, message_len - 1);
162 /* enforced by memset() above */
163 ASSERT (0 == rmp->msg_class[class_len - 1]);
164 ASSERT (0 == rmp->message[message_len - 1]);
166 vl_api_send_msg (reg, (u8 *) rmp);
170 vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
174 vlib_log_main_t *lm = &log_main;
176 int i = last_log_entry ();
177 int count = lm->count;
178 f64 time_offset, start_time;
179 vl_api_registration_t *reg;
181 reg = vl_api_client_index_to_registration (mp->client_index);
185 start_time = clib_net_to_host_f64 (mp->start_timestamp);
187 time_offset = (f64) lm->time_zero_timeval.tv_sec
188 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
192 e = vec_elt_at_index (lm->entries, i);
193 if (start_time <= e->timestamp + time_offset)
194 show_log_details (reg, mp->context, e->timestamp + time_offset,
195 (vl_api_log_level_t *) & e->level,
196 format (0, "%U", format_vlib_log_class, e->class),
198 i = (i + 1) % lm->size;
203 #define BOUNCE_HANDLER(nn) \
204 static void vl_api_##nn##_t_handler ( \
205 vl_api_##nn##_t *mp) \
207 vpe_client_registration_t *reg; \
208 vpe_api_main_t * vam = &vpe_api_main; \
211 /* One registration only... */ \
212 pool_foreach (reg, vam->nn##_registrations) \
214 q = vl_api_client_index_to_input_queue (reg->client_index); \
217 * If the queue is stuffed, turf the msg and complain \
218 * It's unlikely that the intended recipient is \
219 * alive; avoid deadlock at all costs. \
221 if (q->cursize == q->maxsize) { \
222 clib_warning ("ERROR: receiver queue full, drop msg"); \
223 vl_msg_api_free (mp); \
226 vl_msg_api_send_shmem (q, (u8 *)&mp); \
230 vl_msg_api_free (mp); \
235 * Add vpe's API message handlers to the table.
236 * vlib has already mapped shared memory and
237 * added the client registration handlers.
238 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
240 #include <vpp/api/vpe.api.c>
241 static clib_error_t *
242 vpe_api_hookup (vlib_main_t * vm)
245 * Set up the (msg_name, crc, message-id) table
247 msg_id_base = setup_message_id_table ();
252 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
255 vpe_api_init (vlib_main_t * vm)
257 vpe_api_main_t *am = &vpe_api_main;
260 am->vnet_main = vnet_get_main ();
262 am->a##_registration_hash = hash_create (0, sizeof (uword));
263 foreach_registration_hash;
266 vl_set_memory_region_name ("/vpe-api");
267 vl_mem_api_enable_disable (vm, 1 /* enable it */ );
272 static clib_error_t *
273 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
276 u64 baseva, size, pvt_heap_size;
278 const int max_buf_size = 4096;
280 struct passwd _pw, *pw;
281 struct group _grp, *grp;
283 buf = vec_new (char, 128);
284 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
286 if (unformat (input, "prefix %s", &chroot_path))
288 vec_add1 (chroot_path, 0);
289 vl_set_memory_root_path ((char *) chroot_path);
291 else if (unformat (input, "uid %d", &uid))
292 vl_set_memory_uid (uid);
293 else if (unformat (input, "gid %d", &gid))
294 vl_set_memory_gid (gid);
295 else if (unformat (input, "baseva %llx", &baseva))
296 vl_set_global_memory_baseva (baseva);
297 else if (unformat (input, "global-size %lldM", &size))
298 vl_set_global_memory_size (size * (1ULL << 20));
299 else if (unformat (input, "global-size %lldG", &size))
300 vl_set_global_memory_size (size * (1ULL << 30));
301 else if (unformat (input, "global-size %lld", &size))
302 vl_set_global_memory_size (size);
303 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
304 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
305 else if (unformat (input, "global-pvt-heap-size size %lld",
307 vl_set_global_pvt_heap_size (pvt_heap_size);
308 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
309 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
310 else if (unformat (input, "api-pvt-heap-size size %lld",
312 vl_set_api_pvt_heap_size (pvt_heap_size);
313 else if (unformat (input, "api-size %lldM", &size))
314 vl_set_api_memory_size (size * (1ULL << 20));
315 else if (unformat (input, "api-size %lldG", &size))
316 vl_set_api_memory_size (size * (1ULL << 30));
317 else if (unformat (input, "api-size %lld", &size))
318 vl_set_api_memory_size (size);
319 else if (unformat (input, "uid %s", &s))
321 /* lookup the username */
324 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
325 && (vec_len (buf) <= max_buf_size))
327 vec_resize (buf, vec_len (buf) * 2);
331 e = clib_error_return_code (0, rv,
332 CLIB_ERROR_ERRNO_VALID |
334 "cannot fetch username %s", s);
342 clib_error_return_fatal (0, "username %s does not exist", s);
348 vl_set_memory_uid (pw->pw_uid);
350 else if (unformat (input, "gid %s", &s))
352 /* lookup the group name */
355 getgrnam_r (s, &_grp, buf, vec_len (buf),
357 && (vec_len (buf) <= max_buf_size))
359 vec_resize (buf, vec_len (buf) * 2);
363 e = clib_error_return_code (0, rv,
364 CLIB_ERROR_ERRNO_VALID |
366 "cannot fetch group %s", s);
373 e = clib_error_return_fatal (0, "group %s does not exist", s);
380 vl_set_memory_gid (grp->gr_gid);
383 return clib_error_return (0, "unknown input `%U'",
384 format_unformat_error, input);
389 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
392 get_unformat_vnet_sw_interface (void)
394 return (void *) &unformat_vnet_sw_interface;
398 * fd.io coding-style-patch-verification: ON
401 * eval: (c-set-style "gnu")