misc: move part of vpe apis to vlibmemory
[vpp.git] / src / vlibmemory / vlib_api.c
1 /*
2  * vlib_api.c VLIB API implementation
3  *
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:
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.
16  */
17
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vnet/api_errno.h>
21
22 #include <vlibmemory/vlib.api_enum.h>
23 #include <vlibmemory/vlib.api_types.h>
24
25 u16 msg_id_base;
26 #define REPLY_MSG_ID_BASE msg_id_base
27 #include <vlibapi/api_helper_macros.h>
28
29 static void
30 shmem_cli_output (uword arg, u8 *buffer, uword buffer_bytes)
31 {
32   u8 **shmem_vecp = (u8 **) arg;
33   u8 *shmem_vec;
34   void *oldheap;
35   u32 offset;
36
37   shmem_vec = *shmem_vecp;
38
39   offset = vec_len (shmem_vec);
40
41   oldheap = vl_msg_push_heap ();
42
43   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;
50 }
51
52 static void
53 vl_api_cli_t_handler (vl_api_cli_t *mp)
54 {
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;
59   u8 *shmem_vec = 0;
60   void *oldheap;
61
62   reg = vl_api_client_index_to_registration (mp->client_index);
63   if (!reg)
64     return;
65   ;
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
84 static void
85 inband_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
96 static void
97 vl_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
120 error:
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
127 static void
128 vl_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
147 static void
148 vl_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
171 out:
172   REPLY_MACRO2 (VL_API_ADD_NODE_NEXT_REPLY,
173                 ({ rmp->next_index = htonl (next_index); }));
174 }
175
176 static void
177 get_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
192 static void
193 vl_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;
210
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;
217
218   for (i = 0; i < count; i++)
219     {
220       get_thread_data (&td[i], i);
221     }
222
223   vl_api_send_msg (reg, (u8 *) rmp);
224 #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
231 }
232
233 static void
234 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t *mp)
235 {
236   int rv = 0;
237   u8 *vector = 0;
238   vlib_main_t *vm = vlib_get_main ();
239   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;
243
244   oldheap = vl_msg_push_heap ();
245
246   /*
247    * Keep the number of memcpy ops to a minimum (e.g. 1).
248    */
249   vec_validate (vector, 16384);
250   vec_reset_length (vector);
251
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 */);
256
257   vl_msg_pop_heap (oldheap);
258
259   REPLY_MACRO2 (VL_API_GET_NODE_GRAPH_REPLY,
260                 ({ rmp->reply_in_shmem = (uword) vector; }));
261 }
262
263 static void
264 vl_api_get_next_index_t_handler (vl_api_get_next_index_t *mp)
265 {
266   vlib_main_t *vm = vlib_get_main ();
267   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;
271   uword *p;
272
273   node = vlib_get_node_by_name (vm, mp->node_name);
274
275   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
293   if (p == 0)
294     {
295       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
296       goto out;
297     }
298   else
299     next_index = p[0];
300
301 out:
302   REPLY_MACRO2 (VL_API_GET_NEXT_INDEX_REPLY,
303                 ({ rmp->next_index = htonl (next_index); }));
304 }
305
306 static void
307 vl_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;
314
315   REPLY_MACRO2 (VL_API_GET_F64_ENDIAN_VALUE_REPLY,
316                 ({ rmp->f64_one_result = clib_host_to_net_f64 (one); }));
317 }
318
319 static void
320 vl_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;
325
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);
329                 }));
330 }
331
332 #include <vlibmemory/vlib.api.c>
333 static clib_error_t *
334 vlib_apis_hookup (vlib_main_t *vm)
335 {
336   api_main_t *am = vlibapi_get_main ();
337
338   /*
339    * Set up the (msg_name, crc, message-id) table
340    */
341   msg_id_base = setup_message_id_table ();
342
343   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
344
345   return 0;
346 }
347
348 VLIB_API_INIT_FUNCTION (vlib_apis_hookup);
349
350 /*
351  * fd.io coding-style-patch-verification: ON
352  *
353  * Local Variables:
354  * eval: (c-set-style "gnu")
355  * End:
356  */