2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <vlib/vlib.h>
23 #include <vlibapi/api_types.h>
24 #include <vppinfra/hash.h>
25 #include <vppinfra/cJSON.h>
27 /* VPP API client includes */
28 #include <vpp-api/client/vppapiclient.h>
34 * Filter these messages as they are used to manage the API connection to VPP
36 char *filter_messages_strings[] = { "memclnt_create",
40 "memclnt_rx_thread_suspend",
41 "memclnt_read_timeout",
43 "trace_plugin_msg_ids",
47 filter_message (char *msgname)
49 char **p = filter_messages_strings;
53 if (strcmp (*p, msgname) == 0)
60 uword *function_by_name;
63 char *vat2_plugin_path;
65 vat2_find_plugin_path ()
67 char *p, path[PATH_MAX];
71 /* find executable path */
72 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
75 /* readlink doesn't provide null termination */
79 if ((p = strrchr (path, '/')) == 0)
84 if ((p = strrchr (path, '/')) == 0)
88 s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
90 vat2_plugin_path = (char *) s;
94 vac_callback (unsigned char *data, int len)
96 u16 result_msg_id = ntohs(*((u16 *)data));
97 DBG("Received something async: %d\n", result_msg_id);
100 int vat2_load_plugins (char *path, char *filter, int *loaded);
103 register_function (void)
107 vat2_find_plugin_path();
108 DBG("Plugin Path %s\n", vat2_plugin_path);
109 int rv = vat2_load_plugins(vat2_plugin_path, 0, &loaded);
110 DBG("Loaded %u plugins\n", loaded);
116 cJSON (*f) (cJSON *);
117 cJSON (*tojson) (void *);
121 struct apifuncs_s *apifuncs = 0;
124 vat2_register_function (char *name, cJSON (*f) (cJSON *),
125 cJSON (*tojson) (void *), u32 crc)
127 struct apifuncs_s funcs = { .f = f, .tojson = tojson, .crc = crc };
128 vec_add1 (apifuncs, funcs);
129 hash_set_mem (function_by_name, name, vec_len (apifuncs) - 1);
133 vat2_exec_command_by_name (char *msgname, cJSON *o)
136 if (filter_message (msgname))
139 cJSON *crc_obj = cJSON_GetObjectItem (o, "_crc");
142 char *crc_str = cJSON_GetStringValue (crc_obj);
143 crc = (u32) strtol (crc_str, NULL, 16);
146 uword *p = hash_get_mem (function_by_name, msgname);
149 fprintf (stderr, "No such command %s\n", msgname);
152 if (crc && crc != apifuncs[p[0]].crc)
154 fprintf (stderr, "API CRC does not match: %s!\n", msgname);
157 cJSON *(*fp) (cJSON *);
158 fp = (void *) apifuncs[p[0]].f;
159 cJSON *r = (*fp) (o);
163 char *output = cJSON_Print (r);
165 printf ("%s\n", output);
170 fprintf (stderr, "Call failed: %s\n", msgname);
177 vat2_exec_command (cJSON *o)
180 cJSON *msg_id_obj = cJSON_GetObjectItem (o, "_msgname");
183 fprintf (stderr, "Missing '_msgname' element!\n");
187 char *name = cJSON_GetStringValue (msg_id_obj);
189 return vat2_exec_command_by_name (name, o);
193 print_template (char *msgname)
195 uword *p = hash_get_mem (function_by_name, msgname);
199 cJSON *(*fp) (void *);
200 fp = (void *) apifuncs[p[0]].tojson;
204 void *scratch = malloc (2048);
208 memset (scratch, 0, 2048);
209 cJSON *t = fp (scratch);
213 char *output = cJSON_Print (t);
218 printf ("%s\n", output);
224 fprintf (stderr, "error printing template for: %s\n", msgname);
232 hash_foreach_mem (name, i, function_by_name, ({ printf ("%s\n", name); }));
239 "Usage: vat2 [OPTION] <message-name> <JSON object>\n"
240 "Send API message to VPP and print reply\n"
242 "-d, --debug Print additional information\n"
243 "-p, --prefix Specify shared memory prefix to connect to a given VPP "
245 "-f, --file File containing a JSON object with the arguments for "
246 "the message to send\n"
247 "--dump-apis List all APIs available in VAT2 (might not reflect "
249 "-t, --template Print a template JSON object for given API message\n"
251 printf ("%s", help_string);
254 int main (int argc, char **argv)
256 /* Create a heap of 64MB */
257 clib_mem_init (0, 64 << 20);
258 char *filename = 0, *prefix = 0;
263 int option_index = 0;
264 bool dump_api = false;
265 bool template = false;
267 static int debug_flag;
268 static struct option long_options[] = {
269 { "debug", no_argument, &debug_flag, 1 },
270 { "prefix", optional_argument, 0, 'p' },
271 { "file", required_argument, 0, 'f' },
272 { "dump-apis", no_argument, 0, 0 },
273 { "template", no_argument, 0, 't' },
277 while ((c = getopt_long (argc, argv, "hdp:f:", long_options,
278 &option_index)) != -1)
283 if (option_index == 3)
305 debug = debug_flag == 1 ? true : false;
306 DBG ("debug = %d, filename = %s shared memory prefix: %s\n", debug, filename,
309 for (index = optind; index < argc; index++)
310 DBG ("Non-option argument %s\n", argv[index]);
314 if (argc > index + 2)
316 fprintf (stderr, "%s: Too many arguments\n", argv[0]);
321 function_by_name = hash_create_string (0, sizeof (uword));
322 int res = register_function();
324 fprintf(stderr, "%s: loading plugins failed\n", argv[0]);
330 print_template (argv[index]);
340 /* Read message arguments from command line */
341 if (argc >= (index + 1))
343 msgname = argv[index];
345 if (argc == (index + 2)) {
346 o = cJSON_Parse(argv[index+1]);
348 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
353 if (!msgname && !filename)
359 /* Read message from file */
363 fprintf (stderr, "%s: Superfluous arguments when filename given\n",
368 FILE *f = fopen(filename, "r");
369 size_t chunksize, bufsize;
374 fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename);
378 chunksize = bufsize = 1024;
379 char *buf = malloc(bufsize);
380 while ((n = fread (buf + n_read, 1, chunksize, f)))
385 bufsize += chunksize;
386 buf = realloc (buf, bufsize);
391 o = cJSON_Parse(buf);
393 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
402 fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
406 if (vac_connect ("vat2", prefix, 0, 1024))
408 fprintf (stderr, "Failed connecting to VPP\n");
414 vat2_exec_command_by_name (msgname, o);
418 if (cJSON_IsArray (o))
420 size_t size = cJSON_GetArraySize (o);
421 for (int i = 0; i < size; i++)
422 vat2_exec_command (cJSON_GetArrayItem (o, i));