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>
36 * Filter these messages as they are used to manage the API connection to VPP
38 char *filter_messages_strings[] = { "memclnt_create",
42 "memclnt_rx_thread_suspend",
43 "memclnt_read_timeout",
45 "trace_plugin_msg_ids",
49 filter_message (char *msgname)
51 char **p = filter_messages_strings;
55 if (strcmp (*p, msgname) == 0)
62 uword *function_by_name;
66 vat2_find_plugin_path (void)
68 char *p, path[PATH_MAX];
72 /* find executable path */
73 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
76 /* readlink doesn't provide null termination */
80 if ((p = strrchr (path, '/')) == 0)
85 if ((p = strrchr (path, '/')) == 0)
89 s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
95 vac_callback (unsigned char *data, int len)
97 u16 result_msg_id = ntohs (*((u16 *) data));
98 DBG ("Received something async: %d\n", result_msg_id);
101 int vat2_load_plugins (u8 *path, char *filter, int *loaded);
104 register_function (char *pluginpath)
107 u8 *vat2_plugin_path = 0;
111 vat2_plugin_path = vat2_find_plugin_path ();
115 vat2_plugin_path = format (0, "%s", pluginpath);
116 vec_add1 (vat2_plugin_path, 0);
118 DBG ("Plugin Path %s\n", vat2_plugin_path);
119 int rv = vat2_load_plugins (vat2_plugin_path, 0, &loaded);
120 DBG ("Loaded %u plugins\n", loaded);
122 vec_free (vat2_plugin_path);
129 cJSON (*f) (cJSON *);
130 cJSON (*tojson) (void *);
134 struct apifuncs_s *apifuncs = 0;
137 vat2_register_function (char *name, cJSON (*f) (cJSON *),
138 cJSON (*tojson) (void *), u32 crc)
140 struct apifuncs_s funcs = { .f = f, .tojson = tojson, .crc = crc };
141 vec_add1 (apifuncs, funcs);
142 hash_set_mem (function_by_name, name, vec_len (apifuncs) - 1);
146 vat2_exec_command_by_name (char *msgname, cJSON *o)
149 if (filter_message (msgname))
152 cJSON *crc_obj = cJSON_GetObjectItem (o, "_crc");
155 char *crc_str = cJSON_GetStringValue (crc_obj);
156 crc = (u32) strtol (crc_str, NULL, 16);
159 uword *p = hash_get_mem (function_by_name, msgname);
162 fprintf (stderr, "No such command %s\n", msgname);
165 if (crc && crc != apifuncs[p[0]].crc)
167 fprintf (stderr, "API CRC does not match: %s!\n", msgname);
170 cJSON *(*fp) (cJSON *);
171 fp = (void *) apifuncs[p[0]].f;
172 cJSON *r = (*fp) (o);
176 char *output = cJSON_Print (r);
178 printf ("%s\n", output);
183 fprintf (stderr, "Call failed: %s\n", msgname);
190 vat2_exec_command (cJSON *o)
193 cJSON *msg_id_obj = cJSON_GetObjectItem (o, "_msgname");
196 fprintf (stderr, "Missing '_msgname' element!\n");
200 char *name = cJSON_GetStringValue (msg_id_obj);
202 return vat2_exec_command_by_name (name, o);
206 print_template (char *msgname)
208 uword *p = hash_get_mem (function_by_name, msgname);
212 cJSON *(*fp) (void *);
213 fp = (void *) apifuncs[p[0]].tojson;
217 void *scratch = malloc (2048);
221 memset (scratch, 0, 2048);
222 cJSON *t = fp (scratch);
226 char *output = cJSON_Print (t);
231 printf ("%s\n", output);
237 fprintf (stderr, "error printing template for: %s\n", msgname);
245 hash_foreach_mem (name, i, function_by_name, ({ printf ("%s\n", name); }));
252 "Usage: vat2 [OPTION] <message-name> <JSON object>\n"
253 "Send API message to VPP and print reply\n"
255 "-d, --debug Print additional information\n"
256 "-p, --prefix <prefix> Specify shared memory prefix to connect "
257 "to a given VPP instance\n"
258 "-f, --file <filename> File containing a JSON object with the "
259 "arguments for the message to send\n"
260 "-t, --template <message-name> Print a template JSON object for given API"
262 "--dump-apis List all APIs available in VAT2 (might "
263 "not reflect running VPP)\n"
264 "--plugin-path Pluing path"
266 printf ("%s", help_string);
270 main (int argc, char **argv)
272 /* Create a heap of 64MB */
273 clib_mem_init (0, 64 << 20);
274 char *filename = 0, *prefix = 0, *template = 0, *pluginpath = 0;
279 int option_index = 0;
280 bool dump_api = false;
282 static struct option long_options[] = {
283 { "debug", no_argument, 0, 'd' },
284 { "prefix", required_argument, 0, 's' },
285 { "file", required_argument, 0, 'f' },
286 { "dump-apis", no_argument, 0, 0 },
287 { "template", required_argument, 0, 't' },
288 { "plugin-path", required_argument, 0, 'p' },
292 while ((c = getopt_long (argc, argv, "hdp:f:t:", long_options,
293 &option_index)) != -1)
298 if (option_index == 3)
323 DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n",
324 vat2_debug, filename, template, prefix);
326 for (index = optind; index < argc; index++)
327 DBG ("Non-option argument %s\n", argv[index]);
331 if (argc > index + 2)
333 fprintf (stderr, "%s: Too many arguments\n", argv[0]);
338 function_by_name = hash_create_string (0, sizeof (uword));
339 int res = register_function (pluginpath);
342 fprintf (stderr, "%s: loading plugins failed\n", argv[0]);
348 print_template (template);
358 /* Read message arguments from command line */
359 if (argc >= (index + 1))
361 msgname = argv[index];
363 if (argc == (index + 2))
365 o = cJSON_Parse (argv[index + 1]);
368 fprintf (stderr, "%s: Failed parsing JSON input: %s\n", argv[0],
369 cJSON_GetErrorPtr ());
374 if (!msgname && !filename)
380 /* Read message from file */
385 fprintf (stderr, "%s: Superfluous arguments when filename given\n",
390 FILE *f = fopen (filename, "r");
391 size_t chunksize, bufsize;
397 fprintf (stderr, "%s: can't open file: %s\n", argv[0], filename);
401 chunksize = bufsize = 1024;
402 char *buf = malloc (bufsize);
403 while ((n = fread (buf + n_read, 1, chunksize, f)))
408 bufsize += chunksize;
409 buf = realloc (buf, bufsize);
415 o = cJSON_Parse (buf);
418 fprintf (stderr, "%s: Failed parsing JSON input: %s\n", argv[0],
419 cJSON_GetErrorPtr ());
428 fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
432 if (vac_connect ("vat2", prefix, 0, 1024))
434 fprintf (stderr, "Failed connecting to VPP\n");
440 vat2_exec_command_by_name (msgname, o);
444 if (cJSON_IsArray (o))
446 size_t size = cJSON_GetArraySize (o);
447 for (int i = 0; i < size; i++)
448 vat2_exec_command (cJSON_GetArrayItem (o, i));