2 * vlib_api.c VLIB API implementation
4 * Copyright (c) 2021 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vnet/api_errno.h>
22 #include <vlibmemory/vlib.api_enum.h>
23 #include <vlibmemory/vlib.api_types.h>
25 static u16 msg_id_base;
26 #define REPLY_MSG_ID_BASE msg_id_base
27 #include <vlibapi/api_helper_macros.h>
30 shmem_cli_output (uword arg, u8 *buffer, uword buffer_bytes)
32 u8 **shmem_vecp = (u8 **) arg;
37 shmem_vec = *shmem_vecp;
39 offset = vec_len (shmem_vec);
41 oldheap = vl_msg_push_heap ();
43 vec_validate (shmem_vec, offset + buffer_bytes - 1);
45 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
47 vl_msg_pop_heap (oldheap);
49 *shmem_vecp = shmem_vec;
53 vl_api_cli_t_handler (vl_api_cli_t *mp)
55 vl_api_cli_reply_t *rp;
56 vl_api_registration_t *reg;
57 vlib_main_t *vm = vlib_get_main ();
58 unformat_input_t input;
62 reg = vl_api_client_index_to_registration (mp->client_index);
67 rp = vl_msg_api_alloc (sizeof (*rp));
68 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY + REPLY_MSG_ID_BASE);
69 rp->context = mp->context;
71 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
73 vlib_cli_input (vm, &input, shmem_cli_output, (uword) &shmem_vec);
75 oldheap = vl_msg_push_heap ();
76 vec_add1 (shmem_vec, 0);
77 vl_msg_pop_heap (oldheap);
79 rp->reply_in_shmem = (uword) shmem_vec;
81 vl_api_send_msg (reg, (u8 *) rp);
85 inband_cli_output (uword arg, u8 *buffer, uword buffer_bytes)
87 u8 **mem_vecp = (u8 **) arg;
88 u8 *mem_vec = *mem_vecp;
89 u32 offset = vec_len (mem_vec);
91 vec_validate (mem_vec, offset + buffer_bytes - 1);
92 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
97 vl_api_cli_inband_t_handler (vl_api_cli_inband_t *mp)
99 vl_api_cli_inband_reply_t *rmp;
101 vlib_main_t *vm = vlib_get_main ();
102 unformat_input_t input;
106 if (vl_msg_api_get_msg_length (mp) <
107 vl_api_string_len (&mp->cmd) + sizeof (*mp))
113 cmd_vec = vl_api_from_api_to_new_vec (mp, &mp->cmd);
115 unformat_init_string (&input, (char *) cmd_vec,
116 vl_api_string_len (&mp->cmd));
117 rv = vlib_cli_input (vm, &input, inband_cli_output, (uword) &out_vec);
118 unformat_free (&input);
121 REPLY_MACRO3 (VL_API_CLI_INBAND_REPLY, vec_len (out_vec),
122 ({ vl_api_vec_to_api_string (out_vec, &rmp->reply); }));
128 vl_api_get_node_index_t_handler (vl_api_get_node_index_t *mp)
130 vlib_main_t *vm = vlib_get_main ();
131 vl_api_get_node_index_reply_t *rmp;
136 n = vlib_get_node_by_name (vm, mp->node_name);
139 rv = VNET_API_ERROR_NO_SUCH_NODE;
141 node_index = n->index;
143 REPLY_MACRO2 (VL_API_GET_NODE_INDEX_REPLY,
144 ({ rmp->node_index = htonl (node_index); }));
148 vl_api_add_node_next_t_handler (vl_api_add_node_next_t *mp)
150 vlib_main_t *vm = vlib_get_main ();
151 vl_api_add_node_next_reply_t *rmp;
152 vlib_node_t *n, *next;
156 n = vlib_get_node_by_name (vm, mp->node_name);
160 rv = VNET_API_ERROR_NO_SUCH_NODE;
164 next = vlib_get_node_by_name (vm, mp->next_name);
167 rv = VNET_API_ERROR_NO_SUCH_NODE2;
169 next_index = vlib_node_add_next (vm, n->index, next->index);
172 REPLY_MACRO2 (VL_API_ADD_NODE_NEXT_REPLY,
173 ({ rmp->next_index = htonl (next_index); }));
177 get_thread_data (vl_api_thread_data_t *td, int index)
179 vlib_worker_thread_t *w = vlib_worker_threads + index;
180 td->id = htonl (index);
182 strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
184 strncpy ((char *) td->type, (char *) w->registration->name,
185 ARRAY_LEN (td->type) - 1);
186 td->pid = htonl (w->lwp);
187 td->cpu_id = htonl (w->cpu_id);
188 td->core = htonl (w->core_id);
189 td->cpu_socket = htonl (w->numa_id);
193 vl_api_show_threads_t_handler (vl_api_show_threads_t *mp)
197 #if !defined(__powerpc64__)
198 vl_api_registration_t *reg;
199 vl_api_show_threads_reply_t *rmp;
200 vl_api_thread_data_t *td;
202 count = vec_len (vlib_worker_threads);
206 msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
207 reg = vl_api_client_index_to_registration (mp->client_index);
211 rmp = vl_msg_api_alloc (msg_size);
212 clib_memset (rmp, 0, msg_size);
213 rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY + REPLY_MSG_ID_BASE);
214 rmp->context = mp->context;
215 rmp->count = htonl (count);
216 td = rmp->thread_data;
218 for (i = 0; i < count; i++)
220 get_thread_data (&td[i], i);
223 vl_api_send_msg (reg, (u8 *) rmp);
226 /* unimplemented support */
228 clib_warning ("power pc does not support show threads api");
229 REPLY_MACRO2 (VL_API_SHOW_THREADS_REPLY, ({ rmp->count = htonl (count); }));
234 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t *mp)
238 vlib_main_t *vm = vlib_get_main ();
240 vl_api_get_node_graph_reply_t *rmp;
241 static vlib_node_t ***node_dups;
242 static vlib_main_t **stat_vms;
244 oldheap = vl_msg_push_heap ();
247 * Keep the number of memcpy ops to a minimum (e.g. 1).
249 vec_validate (vector, 16384);
250 vec_reset_length (vector);
252 vlib_node_get_nodes (vm, 0 /* main threads */, 0 /* include stats */,
253 1 /* barrier sync */, &node_dups, &stat_vms);
254 vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */,
255 1 /* include stats */);
257 vl_msg_pop_heap (oldheap);
259 REPLY_MACRO2 (VL_API_GET_NODE_GRAPH_REPLY,
260 ({ rmp->reply_in_shmem = (uword) vector; }));
264 vl_api_get_next_index_t_handler (vl_api_get_next_index_t *mp)
266 vlib_main_t *vm = vlib_get_main ();
267 vl_api_get_next_index_reply_t *rmp;
268 vlib_node_t *node, *next_node;
270 u32 next_node_index = ~0, next_index = ~0;
273 node = vlib_get_node_by_name (vm, mp->node_name);
277 rv = VNET_API_ERROR_NO_SUCH_NODE;
281 next_node = vlib_get_node_by_name (vm, mp->next_name);
285 rv = VNET_API_ERROR_NO_SUCH_NODE2;
289 next_node_index = next_node->index;
291 p = hash_get (node->next_slot_by_node, next_node_index);
295 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
302 REPLY_MACRO2 (VL_API_GET_NEXT_INDEX_REPLY,
303 ({ rmp->next_index = htonl (next_index); }));
307 vl_api_get_f64_endian_value_t_handler (vl_api_get_f64_endian_value_t *mp)
311 vl_api_get_f64_endian_value_reply_t *rmp;
312 if (1.0 != clib_net_to_host_f64 (mp->f64_one))
313 rv = VNET_API_ERROR_API_ENDIAN_FAILED;
315 REPLY_MACRO2 (VL_API_GET_F64_ENDIAN_VALUE_REPLY,
316 ({ rmp->f64_one_result = clib_host_to_net_f64 (one); }));
320 vl_api_get_f64_increment_by_one_t_handler (
321 vl_api_get_f64_increment_by_one_t *mp)
324 vl_api_get_f64_increment_by_one_reply_t *rmp;
326 REPLY_MACRO2 (VL_API_GET_F64_INCREMENT_BY_ONE_REPLY, ({
327 rmp->f64_value = clib_host_to_net_f64 (
328 clib_net_to_host_f64 (mp->f64_value) + 1.0);
332 #include <vlibmemory/vlib.api.c>
333 static clib_error_t *
334 vlib_apis_hookup (vlib_main_t *vm)
336 api_main_t *am = vlibapi_get_main ();
339 * Set up the (msg_name, crc, message-id) table
341 msg_id_base = setup_message_id_table ();
343 /* Mark messages as mp safe */
344 vl_api_set_msg_thread_safe (am, msg_id_base + VL_API_GET_NODE_GRAPH, 1);
345 vl_api_set_msg_thread_safe (am, msg_id_base + VL_API_SHOW_THREADS, 1);
350 VLIB_API_INIT_FUNCTION (vlib_apis_hookup);
353 * fd.io coding-style-patch-verification: ON
356 * eval: (c-set-style "gnu")