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.
19 #include <vlib/vlib.h>
20 #include <vlibapi/api_types.h>
21 #include <vppinfra/cJSON.h>
23 /* VPP API client includes */
24 #include <vpp-api/client/vppapiclient.h>
29 uword *function_by_name;
32 char *vat2_plugin_path;
34 vat2_find_plugin_path ()
36 char *p, path[PATH_MAX];
40 /* find executable path */
41 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
44 /* readlink doesn't provide null termination */
48 if ((p = strrchr (path, '/')) == 0)
53 if ((p = strrchr (path, '/')) == 0)
57 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vat2_plugins:"
58 "%s/lib/vat2_plugins", path, path);
60 vat2_plugin_path = (char *) s;
64 vac_callback (unsigned char *data, int len)
66 u16 result_msg_id = ntohs(*((u16 *)data));
67 DBG("Received something async: %d\n", result_msg_id);
70 int vat2_load_plugins (char *path, char *filter, int *loaded);
73 register_function (void)
77 vat2_find_plugin_path();
78 DBG("Plugin Path %s\n", vat2_plugin_path);
79 int rv = vat2_load_plugins(vat2_plugin_path, 0, &loaded);
80 DBG("Loaded %u plugins\n", loaded);
85 vat2_register_function(char *name, cJSON (*f)(cJSON *))
87 hash_set_mem(function_by_name, name, f);
90 int main (int argc, char **argv)
92 /* Create a heap of 64MB */
93 clib_mem_init (0, 64 << 20);
101 while ((c = getopt (argc, argv, "df:")) != -1) {
111 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
112 else if (isprint (optopt))
113 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
116 "Unknown option character `\\x%x'.\n",
124 DBG("debug = %d, filename = %s\n", debug, filename);
126 for (index = optind; index < argc; index++)
127 DBG ("Non-option argument %s\n", argv[index]);
132 function_by_name = hash_create_string (0, sizeof (uword));
133 int res = register_function();
135 fprintf(stderr, "%s: loading plugins failed\n", argv[0]);
139 if (argc > index + 2) {
140 fprintf(stderr, "%s: Too many arguments\n", argv[0]);
144 /* Read JSON from stdin, command line or file */
145 if (argc >= (index + 1)) {
146 p = hash_get_mem (function_by_name, argv[index]);
148 fprintf(stderr, "%s: Unknown command: %s\n", argv[0], argv[index]);
153 if (argc == (index + 2)) {
154 o = cJSON_Parse(argv[index+1]);
156 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
162 if (argc > index + 1) {
163 fprintf(stderr, "%s: Superfluous arguments when filename given\n", argv[0]);
167 FILE *f = fopen(filename, "r");
168 size_t bufsize = 1024;
173 fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename);
176 char *buf = malloc(bufsize);
177 while ((n = fread(buf, 1, bufsize, f))) {
180 buf = realloc(buf, bufsize);
184 o = cJSON_Parse(buf);
187 fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
194 fprintf(stderr, "%s: Failed parsing JSON input\n", argv[0]);
198 if (vac_connect("vat2", 0, 0, 1024)) {
199 fprintf(stderr, "Failed connecting to VPP\n");
203 fprintf(stderr, "No such command\n");
207 cJSON * (*fp) (cJSON *);
209 cJSON *r = (*fp) (o);
215 char *output = cJSON_Print(r);
217 printf("%s\n", output);
220 fprintf(stderr, "Call failed\n");