blob: 3d4f8829d9a42c923178fc809507334b6b289ec7 [file] [log] [blame]
Florin Corase86a8ed2018-01-05 03:20:25 -08001/*
Florin Corase86a8ed2018-01-05 03:20:25 -08002 * vlib_api.c VLIB API implementation
3 *
Florin Coras248210c2021-09-14 18:54:45 -07004 * Copyright (c) 2021 Cisco and/or its affiliates.
Florin Corase86a8ed2018-01-05 03:20:25 -08005 * 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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
Florin Corase86a8ed2018-01-05 03:20:25 -080016 */
17
Florin Corase86a8ed2018-01-05 03:20:25 -080018#include <vlibapi/api.h>
19#include <vlibmemory/api.h>
Florin Coras248210c2021-09-14 18:54:45 -070020#include <vnet/api_errno.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080021
Florin Coras248210c2021-09-14 18:54:45 -070022#include <vlibmemory/vlib.api_enum.h>
23#include <vlibmemory/vlib.api_types.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080024
Ole Troan3459ece2021-09-27 17:11:34 +020025static u16 msg_id_base;
Florin Coras248210c2021-09-14 18:54:45 -070026#define REPLY_MSG_ID_BASE msg_id_base
27#include <vlibapi/api_helper_macros.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080028
Florin Corase86a8ed2018-01-05 03:20:25 -080029static void
Florin Coras248210c2021-09-14 18:54:45 -070030shmem_cli_output (uword arg, u8 *buffer, uword buffer_bytes)
Florin Corase86a8ed2018-01-05 03:20:25 -080031{
Florin Coras248210c2021-09-14 18:54:45 -070032 u8 **shmem_vecp = (u8 **) arg;
33 u8 *shmem_vec;
34 void *oldheap;
35 u32 offset;
Florin Corase86a8ed2018-01-05 03:20:25 -080036
Florin Coras248210c2021-09-14 18:54:45 -070037 shmem_vec = *shmem_vecp;
Florin Corase86a8ed2018-01-05 03:20:25 -080038
Florin Coras248210c2021-09-14 18:54:45 -070039 offset = vec_len (shmem_vec);
Florin Corase86a8ed2018-01-05 03:20:25 -080040
Florin Coras248210c2021-09-14 18:54:45 -070041 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -080042
Florin Coras248210c2021-09-14 18:54:45 -070043 vec_validate (shmem_vec, offset + buffer_bytes - 1);
44
45 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
46
47 vl_msg_pop_heap (oldheap);
48
49 *shmem_vecp = shmem_vec;
Florin Corase86a8ed2018-01-05 03:20:25 -080050}
51
Florin Coras248210c2021-09-14 18:54:45 -070052static void
53vl_api_cli_t_handler (vl_api_cli_t *mp)
Florin Corase86a8ed2018-01-05 03:20:25 -080054{
Florin Coras248210c2021-09-14 18:54:45 -070055 vl_api_cli_reply_t *rp;
Florin Coras6c4dae22018-01-09 06:39:23 -080056 vl_api_registration_t *reg;
Florin Coras248210c2021-09-14 18:54:45 -070057 vlib_main_t *vm = vlib_get_main ();
58 unformat_input_t input;
59 u8 *shmem_vec = 0;
60 void *oldheap;
Florin Corase86a8ed2018-01-05 03:20:25 -080061
Florin Coras6c4dae22018-01-09 06:39:23 -080062 reg = vl_api_client_index_to_registration (mp->client_index);
63 if (!reg)
Florin Corase86a8ed2018-01-05 03:20:25 -080064 return;
Florin Coras248210c2021-09-14 18:54:45 -070065 ;
66
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;
70
71 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
72
73 vlib_cli_input (vm, &input, shmem_cli_output, (uword) &shmem_vec);
74
75 oldheap = vl_msg_push_heap ();
76 vec_add1 (shmem_vec, 0);
77 vl_msg_pop_heap (oldheap);
78
79 rp->reply_in_shmem = (uword) shmem_vec;
80
81 vl_api_send_msg (reg, (u8 *) rp);
82}
83
84static void
85inband_cli_output (uword arg, u8 *buffer, uword buffer_bytes)
86{
87 u8 **mem_vecp = (u8 **) arg;
88 u8 *mem_vec = *mem_vecp;
89 u32 offset = vec_len (mem_vec);
90
91 vec_validate (mem_vec, offset + buffer_bytes - 1);
92 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
93 *mem_vecp = mem_vec;
94}
95
96static void
97vl_api_cli_inband_t_handler (vl_api_cli_inband_t *mp)
98{
99 vl_api_cli_inband_reply_t *rmp;
100 int rv = 0;
101 vlib_main_t *vm = vlib_get_main ();
102 unformat_input_t input;
103 u8 *out_vec = 0;
104 u8 *cmd_vec = 0;
105
106 if (vl_msg_api_get_msg_length (mp) <
107 vl_api_string_len (&mp->cmd) + sizeof (*mp))
108 {
109 rv = -1;
110 goto error;
111 }
112
113 cmd_vec = vl_api_from_api_to_new_vec (mp, &mp->cmd);
114
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);
119
120error:
121 REPLY_MACRO3 (VL_API_CLI_INBAND_REPLY, vec_len (out_vec),
122 ({ vl_api_vec_to_api_string (out_vec, &rmp->reply); }));
123 vec_free (out_vec);
124 vec_free (cmd_vec);
125}
126
127static void
128vl_api_get_node_index_t_handler (vl_api_get_node_index_t *mp)
129{
130 vlib_main_t *vm = vlib_get_main ();
131 vl_api_get_node_index_reply_t *rmp;
132 vlib_node_t *n;
133 int rv = 0;
134 u32 node_index = ~0;
135
136 n = vlib_get_node_by_name (vm, mp->node_name);
137
138 if (n == 0)
139 rv = VNET_API_ERROR_NO_SUCH_NODE;
140 else
141 node_index = n->index;
142
143 REPLY_MACRO2 (VL_API_GET_NODE_INDEX_REPLY,
144 ({ rmp->node_index = htonl (node_index); }));
145}
146
147static void
148vl_api_add_node_next_t_handler (vl_api_add_node_next_t *mp)
149{
150 vlib_main_t *vm = vlib_get_main ();
151 vl_api_add_node_next_reply_t *rmp;
152 vlib_node_t *n, *next;
153 int rv = 0;
154 u32 next_index = ~0;
155
156 n = vlib_get_node_by_name (vm, mp->node_name);
157
158 if (n == 0)
159 {
160 rv = VNET_API_ERROR_NO_SUCH_NODE;
161 goto out;
162 }
163
164 next = vlib_get_node_by_name (vm, mp->next_name);
165
166 if (next == 0)
167 rv = VNET_API_ERROR_NO_SUCH_NODE2;
168 else
169 next_index = vlib_node_add_next (vm, n->index, next->index);
170
171out:
172 REPLY_MACRO2 (VL_API_ADD_NODE_NEXT_REPLY,
173 ({ rmp->next_index = htonl (next_index); }));
174}
175
176static void
177get_thread_data (vl_api_thread_data_t *td, int index)
178{
179 vlib_worker_thread_t *w = vlib_worker_threads + index;
180 td->id = htonl (index);
181 if (w->name)
182 strncpy ((char *) td->name, (char *) w->name, ARRAY_LEN (td->name) - 1);
183 if (w->registration)
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);
190}
191
192static void
193vl_api_show_threads_t_handler (vl_api_show_threads_t *mp)
194{
195 int count = 0;
196
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;
201 int i, msg_size = 0;
202 count = vec_len (vlib_worker_threads);
203 if (!count)
204 return;
205
206 msg_size = sizeof (*rmp) + sizeof (rmp->thread_data[0]) * count;
207 reg = vl_api_client_index_to_registration (mp->client_index);
208 if (!reg)
209 return;
Florin Corase86a8ed2018-01-05 03:20:25 -0800210
211 rmp = vl_msg_api_alloc (msg_size);
Dave Barachb7b92992018-10-17 10:38:51 -0400212 clib_memset (rmp, 0, msg_size);
Florin Coras248210c2021-09-14 18:54:45 -0700213 rmp->_vl_msg_id = htons (VL_API_SHOW_THREADS_REPLY + REPLY_MSG_ID_BASE);
Florin Corase86a8ed2018-01-05 03:20:25 -0800214 rmp->context = mp->context;
Florin Coras248210c2021-09-14 18:54:45 -0700215 rmp->count = htonl (count);
216 td = rmp->thread_data;
Florin Corase86a8ed2018-01-05 03:20:25 -0800217
Florin Coras248210c2021-09-14 18:54:45 -0700218 for (i = 0; i < count; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -0800219 {
Florin Coras248210c2021-09-14 18:54:45 -0700220 get_thread_data (&td[i], i);
Florin Corase86a8ed2018-01-05 03:20:25 -0800221 }
222
Florin Coras6c4dae22018-01-09 06:39:23 -0800223 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras248210c2021-09-14 18:54:45 -0700224#else
225
226 /* unimplemented support */
227 rv = -9;
228 clib_warning ("power pc does not support show threads api");
229 REPLY_MACRO2 (VL_API_SHOW_THREADS_REPLY, ({ rmp->count = htonl (count); }));
230#endif
Florin Corase86a8ed2018-01-05 03:20:25 -0800231}
232
Florin Corase86a8ed2018-01-05 03:20:25 -0800233static void
Florin Coras248210c2021-09-14 18:54:45 -0700234vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t *mp)
Florin Corase86a8ed2018-01-05 03:20:25 -0800235{
Florin Coras248210c2021-09-14 18:54:45 -0700236 int rv = 0;
237 u8 *vector = 0;
Florin Corase86a8ed2018-01-05 03:20:25 -0800238 vlib_main_t *vm = vlib_get_main ();
Florin Coras248210c2021-09-14 18:54:45 -0700239 void *oldheap;
240 vl_api_get_node_graph_reply_t *rmp;
241 static vlib_node_t ***node_dups;
242 static vlib_main_t **stat_vms;
Florin Corase86a8ed2018-01-05 03:20:25 -0800243
Florin Coras248210c2021-09-14 18:54:45 -0700244 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800245
Florin Coras248210c2021-09-14 18:54:45 -0700246 /*
247 * Keep the number of memcpy ops to a minimum (e.g. 1).
248 */
249 vec_validate (vector, 16384);
250 vec_reset_length (vector);
Florin Corase86a8ed2018-01-05 03:20:25 -0800251
Florin Coras248210c2021-09-14 18:54:45 -0700252 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 */);
Florin Corase86a8ed2018-01-05 03:20:25 -0800256
Florin Coras248210c2021-09-14 18:54:45 -0700257 vl_msg_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800258
Florin Coras248210c2021-09-14 18:54:45 -0700259 REPLY_MACRO2 (VL_API_GET_NODE_GRAPH_REPLY,
260 ({ rmp->reply_in_shmem = (uword) vector; }));
Florin Corase86a8ed2018-01-05 03:20:25 -0800261}
262
263static void
Florin Coras248210c2021-09-14 18:54:45 -0700264vl_api_get_next_index_t_handler (vl_api_get_next_index_t *mp)
Florin Corase86a8ed2018-01-05 03:20:25 -0800265{
Florin Corase86a8ed2018-01-05 03:20:25 -0800266 vlib_main_t *vm = vlib_get_main ();
Florin Coras248210c2021-09-14 18:54:45 -0700267 vl_api_get_next_index_reply_t *rmp;
268 vlib_node_t *node, *next_node;
269 int rv = 0;
270 u32 next_node_index = ~0, next_index = ~0;
Florin Corase86a8ed2018-01-05 03:20:25 -0800271 uword *p;
272
Florin Coras248210c2021-09-14 18:54:45 -0700273 node = vlib_get_node_by_name (vm, mp->node_name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800274
Florin Coras248210c2021-09-14 18:54:45 -0700275 if (node == 0)
276 {
277 rv = VNET_API_ERROR_NO_SUCH_NODE;
278 goto out;
279 }
280
281 next_node = vlib_get_node_by_name (vm, mp->next_name);
282
283 if (next_node == 0)
284 {
285 rv = VNET_API_ERROR_NO_SUCH_NODE2;
286 goto out;
287 }
288 else
289 next_node_index = next_node->index;
290
291 p = hash_get (node->next_slot_by_node, next_node_index);
292
Florin Corase86a8ed2018-01-05 03:20:25 -0800293 if (p == 0)
294 {
Florin Coras248210c2021-09-14 18:54:45 -0700295 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
296 goto out;
Florin Corase86a8ed2018-01-05 03:20:25 -0800297 }
Florin Coras248210c2021-09-14 18:54:45 -0700298 else
299 next_index = p[0];
Florin Corase86a8ed2018-01-05 03:20:25 -0800300
Florin Coras248210c2021-09-14 18:54:45 -0700301out:
302 REPLY_MACRO2 (VL_API_GET_NEXT_INDEX_REPLY,
303 ({ rmp->next_index = htonl (next_index); }));
Florin Corase86a8ed2018-01-05 03:20:25 -0800304}
305
Florin Coras248210c2021-09-14 18:54:45 -0700306static void
307vl_api_get_f64_endian_value_t_handler (vl_api_get_f64_endian_value_t *mp)
308{
309 int rv = 0;
310 f64 one = 1.0;
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;
Florin Corase86a8ed2018-01-05 03:20:25 -0800314
Florin Coras248210c2021-09-14 18:54:45 -0700315 REPLY_MACRO2 (VL_API_GET_F64_ENDIAN_VALUE_REPLY,
316 ({ rmp->f64_one_result = clib_host_to_net_f64 (one); }));
317}
Florin Corase86a8ed2018-01-05 03:20:25 -0800318
Florin Coras248210c2021-09-14 18:54:45 -0700319static void
320vl_api_get_f64_increment_by_one_t_handler (
321 vl_api_get_f64_increment_by_one_t *mp)
322{
323 int rv = 0;
324 vl_api_get_f64_increment_by_one_reply_t *rmp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800325
Florin Coras248210c2021-09-14 18:54:45 -0700326 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);
329 }));
330}
Florin Corase86a8ed2018-01-05 03:20:25 -0800331
Florin Coras248210c2021-09-14 18:54:45 -0700332#include <vlibmemory/vlib.api.c>
Florin Corase86a8ed2018-01-05 03:20:25 -0800333static clib_error_t *
Florin Coras248210c2021-09-14 18:54:45 -0700334vlib_apis_hookup (vlib_main_t *vm)
Florin Corase86a8ed2018-01-05 03:20:25 -0800335{
Dave Barach39d69112019-11-27 11:42:13 -0500336 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800337
Florin Coras248210c2021-09-14 18:54:45 -0700338 /*
339 * Set up the (msg_name, crc, message-id) table
340 */
341 msg_id_base = setup_message_id_table ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800342
Damjan Marioncada9eb2022-05-18 22:16:11 +0200343 vl_api_set_msg_thread_safe (am, VL_API_GET_NODE_GRAPH, 1);
Florin Corase86a8ed2018-01-05 03:20:25 -0800344 return 0;
345}
346
Florin Coras248210c2021-09-14 18:54:45 -0700347VLIB_API_INIT_FUNCTION (vlib_apis_hookup);
Florin Corase86a8ed2018-01-05 03:20:25 -0800348
349/*
350 * fd.io coding-style-patch-verification: ON
351 *
352 * Local Variables:
353 * eval: (c-set-style "gnu")
354 * End:
355 */