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>
33 uword *function_by_name;
36 char *vat2_plugin_path;
38 vat2_find_plugin_path ()
40 char *p, path[PATH_MAX];
44 /* find executable path */
45 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
48 /* readlink doesn't provide null termination */
52 if ((p = strrchr (path, '/')) == 0)
57 if ((p = strrchr (path, '/')) == 0)
61 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vat2_plugins:"
62 "%s/lib/vat2_plugins", path, path);
64 vat2_plugin_path = (char *) s;
68 vac_callback (unsigned char *data, int len)
70 u16 result_msg_id = ntohs(*((u16 *)data));
71 DBG("Received something async: %d\n", result_msg_id);
74 int vat2_load_plugins (char *path, char *filter, int *loaded);
77 register_function (void)
81 vat2_find_plugin_path();
82 DBG("Plugin Path %s\n", vat2_plugin_path);
83 int rv = vat2_load_plugins(vat2_plugin_path, 0, &loaded);
84 DBG("Loaded %u plugins\n", loaded);
91 cJSON (*tojson) (void *);
94 struct apifuncs_s *apifuncs = 0;
97 vat2_register_function (char *name, cJSON (*f) (cJSON *),
98 cJSON (*tojson) (void *))
100 struct apifuncs_s funcs = { .f = f, .tojson = tojson };
101 vec_add1 (apifuncs, funcs);
102 hash_set_mem (function_by_name, name, vec_len (apifuncs) - 1);
106 vat2_exec_command_by_name (char *msgname, cJSON *o)
108 uword *p = hash_get_mem (function_by_name, msgname);
111 fprintf (stderr, "No such command %s", msgname);
115 cJSON *(*fp) (cJSON *);
116 fp = (void *) apifuncs[p[0]].f;
117 cJSON *r = (*fp) (o);
121 char *output = cJSON_Print (r);
123 printf ("%s\n", output);
128 fprintf (stderr, "Call failed: %s\n", msgname);
135 vat2_exec_command (cJSON *o)
138 cJSON *msg_id_obj = cJSON_GetObjectItem (o, "_msgname");
141 fprintf (stderr, "Missing '_msgname' element!\n");
145 char *name = cJSON_GetStringValue (msg_id_obj);
147 return vat2_exec_command_by_name (name, o);
150 print_template (char *msgname)
152 uword *p = hash_get_mem (function_by_name, msgname);
155 fprintf (stderr, "no such message: %s", msgname);
157 cJSON *(*fp) (void *);
158 fp = (void *) apifuncs[p[0]].tojson;
160 void *scratch = malloc (2048);
161 memset (scratch, 0, 2048);
162 cJSON *t = fp (scratch);
164 char *output = cJSON_Print (t);
166 printf ("%s\n", output);
174 hash_foreach_mem (name, i, function_by_name, ({ printf ("%s\n", name); }));
181 "Usage: vat2 [OPTION] <message-name> <JSON object>\n"
182 "Send API message to VPP and print reply\n"
184 "-d, --debug Print additional information\n"
185 "-p, --prefix Specify shared memory prefix to connect to a given VPP "
187 "-f, --file File containing a JSON object with the arguments for "
188 "the message to send\n"
189 "--dump-apis List all APIs available in VAT2 (might not reflect "
191 "-t, --template Print a template JSON object for given API message\n"
193 printf ("%s", help_string);
196 int main (int argc, char **argv)
198 /* Create a heap of 64MB */
199 clib_mem_init (0, 64 << 20);
200 char *filename = 0, *prefix = 0;
205 int option_index = 0;
206 bool dump_api = false;
207 bool template = false;
209 static int debug_flag;
210 static struct option long_options[] = {
211 { "debug", no_argument, &debug_flag, 1 },
212 { "prefix", optional_argument, 0, 'p' },
213 { "file", required_argument, 0, 'f' },
214 { "dump-apis", no_argument, 0, 0 },
215 { "template", no_argument, 0, 't' },
219 while ((c = getopt_long (argc, argv, "hdp:f:", long_options,
220 &option_index)) != -1)
225 if (option_index == 3)
247 debug = debug_flag == 1 ? true : false;
248 DBG ("debug = %d, filename = %s shared memory prefix: %s\n", debug, filename,
251 for (index = optind; index < argc; index++)
252 DBG ("Non-option argument %s\n", argv[index]);
256 if (argc > index + 2)
258 fprintf (stderr, "%s: Too many arguments\n", argv[0]);
263 function_by_name = hash_create_string (0, sizeof (uword));
264 int res = register_function();
266 fprintf(stderr, "%s: loading plugins failed\n", argv[0]);
272 print_template (argv[index]);
282 /* Read message arguments from command line */
283 if (argc >= (index + 1))
285 msgname = argv[index];
287 if (argc == (index + 2)) {
288 o = cJSON_Parse(argv[index+1]);
290 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
295 /* Read message from file */
299 fprintf (stderr, "%s: Superfluous arguments when filename given\n",
304 FILE *f = fopen(filename, "r");
305 size_t chunksize, bufsize;
310 fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename);
313 chunksize = bufsize = 1024;
314 char *buf = malloc(bufsize);
315 while ((n = fread (buf + n_read, 1, chunksize, f)))
320 bufsize += chunksize;
321 buf = realloc (buf, bufsize);
326 o = cJSON_Parse(buf);
329 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
335 if (!msgname && !filename)
341 if (vac_connect ("vat2", prefix, 0, 1024))
343 fprintf (stderr, "Failed connecting to VPP\n");
349 vat2_exec_command_by_name (msgname, o);
353 if (cJSON_IsArray (o))
355 size_t size = cJSON_GetArraySize (o);
356 for (int i = 0; i < size; i++)
357 vat2_exec_command (cJSON_GetArrayItem (o, i));