summaryrefslogtreecommitdiffstats
path: root/src/vat2/main.c
blob: 1aea690ef24615f5eebb3640150e68371952c18d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <getopt.h>
#include <string.h>
#include <vlib/vlib.h>
#include <vlibapi/api_types.h>
#include <vppinfra/hash.h>
#include <vppinfra/cJSON.h>

/* VPP API client includes */
#include <vpp-api/client/vppapiclient.h>

#include <limits.h>
#include "vat2.h"

/*
 * Filter these messages as they are used to manage the API connection to VPP
 */
char *filter_messages_strings[] = { "memclnt_create",
				    "memclnt_delete",
				    "sockclnt_create",
				    "sockclnt_delete",
				    "memclnt_rx_thread_suspend",
				    "memclnt_read_timeout",
				    "rx_thread_exit",
				    "trace_plugin_msg_ids",
				    0 };

static bool
filter_message (char *msgname)
{
  char **p = filter_messages_strings;

  while (*p)
    {
      if (strcmp (*p, msgname) == 0)
	return true;
      p++;
    }
  return false;
}

uword *function_by_name;
bool debug = false;

char *vat2_plugin_path;
static void
vat2_find_plugin_path ()
{
  char *p, path[PATH_MAX];
  int rv;
  u8 *s;

  /* find executable path */
  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
    return;

  /* readlink doesn't provide null termination */
  path[rv] = 0;

  /* strip filename */
  if ((p = strrchr (path, '/')) == 0)
    return;
  *p = 0;

  /* strip bin/ */
  if ((p = strrchr (path, '/')) == 0)
    return;
  *p = 0;

  s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
  vec_add1 (s, 0);
  vat2_plugin_path = (char *) s;
}

void
vac_callback (unsigned char *data, int len)
{
  u16 result_msg_id = ntohs(*((u16 *)data));
  DBG("Received something async: %d\n", result_msg_id);
}

int vat2_load_plugins (char *path, char *filter, int *loaded);

static int
register_function (void)
{
  int loaded;

  vat2_find_plugin_path();
  DBG("Plugin Path %s\n", vat2_plugin_path);
  int rv = vat2_load_plugins(vat2_plugin_path, 0, &loaded);
  DBG("Loaded %u plugins\n", loaded);
  return rv;
}

struct apifuncs_s
{
  cJSON (*f) (cJSON *);
  cJSON (*tojson) (void *);
  u32 crc;
};

struct apifuncs_s *apifuncs = 0;

void
vat2_register_function (char *name, cJSON (*f) (cJSON *),
			cJSON (*tojson) (void *), u32 crc)
{
  struct apifuncs_s funcs = { .f = f, .tojson = tojson, .crc = crc };
  vec_add1 (apifuncs, funcs);
  hash_set_mem (function_by_name, name, vec_len (apifuncs) - 1);
}

static int
vat2_exec_command_by_name (char *msgname, cJSON *o)
{
  u32 crc = 0;
  if (filter_message (msgname))
    return 0;

  cJSON *crc_obj = cJSON_GetObjectItem (o, "_crc");
  if (crc_obj)
    {
      char *crc_str = cJSON_GetStringValue (crc_obj);
      crc = (u32) strtol (crc_str, NULL, 16);
    }

  uword *p = hash_get_mem (function_by_name, msgname);
  if (!p)
    {
      fprintf (stderr, "No such command %s\n", msgname);
      return -1;
    }
  if (crc && crc != apifuncs[p[0]].crc)
    {
      fprintf (stderr, "API CRC does not match: %s!\n", msgname);
    }

  cJSON *(*fp) (cJSON *);
  fp = (void *) apifuncs[p[0]].f;
  cJSON *r = (*fp) (o);

  if (r)
    {
      char *output = cJSON_Print (r);
      cJSON_Delete (r);
      printf ("%s\n", output);
      free (output);
    }
  else
    {
      fprintf (stderr, "Call failed: %s\n", msgname);
      return -1;
    }
  return 0;
}

static int
vat2_exec_command (cJSON *o)
{

  cJSON *msg_id_obj = cJSON_GetObjectItem (o, "_msgname");
  if (!msg_id_obj)
    {
      fprintf (stderr, "Missing '_msgname' element!\n");
      return -1;
    }

  char *name = cJSON_GetStringValue (msg_id_obj);

  return vat2_exec_command_by_name (name, o);
}

static void
print_template (char *msgname)
{
  uword *p = hash_get_mem (function_by_name, msgname);
  if (!p)
    goto error;

  cJSON *(*fp) (void *);
  fp = (void *) apifuncs[p[0]].tojson;
  if (!fp)
    goto error;

  void *scratch = malloc (2048);
  if (!scratch)
    goto error;

  memset (scratch, 0, 2048);
  cJSON *t = fp (scratch);
  if (!t)
    goto error;
  free (scratch);
  char *output = cJSON_Print (t);
  if (!output)
    goto error;

  cJSON_Delete (t);
  printf ("%s\n", output);
  free (output);

  return;

error:
  fprintf (stderr, "error printing template for: %s\n", msgname);
}

static void
dump_apis (void)
{
  char *name;
  u32 *i;
  hash_foreach_mem (name, i, function_by_name, ({ printf ("%s\n", name); }));
}

static void
print_help (void)
{
  char *help_string =
    "Usage: vat2 [OPTION] <message-name> <JSON object>\n"
    "Send API message to VPP and print reply\n"
    "\n"
    "-d, --debug                    Print additional information\n"
    "-p, --prefix <prefix>          Specify shared memory prefix to connect "
    "to a given VPP instance\n"
    "-f, --file <filename>          File containing a JSON object with the "
    "arguments for the message to send\n"
    "-t, --template <message-name>  Print a template JSON object for given API"
    " message\n"
    "--dump-apis                    List all APIs available in VAT2 (might "
    "not reflect running VPP)\n"
    "\n";
  printf ("%s", help_string);
}

int main (int argc, char **argv)
{
  /* Create a heap of 64MB */
  clib_mem_init (0, 64 << 20);
  char *filename = 0, *prefix = 0, *template = 0;
  int index;
  int c;
  opterr = 0;
  cJSON *o = 0;
  int option_index = 0;
  bool dump_api = false;
  bool debug = false;
  char *msgname = 0;
  static struct option long_options[] = {
    { "debug", no_argument, 0, 'd' },
    { "prefix", required_argument, 0, 'p' },
    { "file", required_argument, 0, 'f' },
    { "dump-apis", no_argument, 0, 0 },
    { "template", required_argument, 0, 't' },
    { 0, 0, 0, 0 }
  };

  while ((c = getopt_long (argc, argv, "hdp:f:t:", long_options,
			   &option_index)) != -1)
    {
      switch (c)
	{
	case 0:
	  if (option_index == 3)
	    dump_api = true;
	  break;
	case 'd':
	  debug = true;
	  break;
	case 't':
	  template = optarg;
	  break;
	case 'p':
	  prefix = optarg;
	  break;
	case 'f':
	  filename = optarg;
	  break;
	case '?':
	  print_help ();
	  return 1;
	default:
	  abort ();
	}
    }
  DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n",
       debug, filename, template, prefix);

  for (index = optind; index < argc; index++)
    DBG ("Non-option argument %s\n", argv[index]);

  index = optind;

  if (argc > index + 2)
    {
      fprintf (stderr, "%s: Too many arguments\n", argv[0]);
      exit (-1);
    }

  /* Load plugins */
  function_by_name = hash_create_string (0, sizeof (uword));
  int res = register_function();
  if (res < 0) {
    fprintf(stderr, "%s: loading plugins failed\n", argv[0]);
    exit(-1);
  }

  if (template)
    {
      print_template (template);
      exit (0);
    }

  if (dump_api)
    {
      dump_apis ();
      exit (0);
    }

  /* Read message arguments from command line */
  if (argc >= (index + 1))
    {
      msgname = argv[index];
    }
  if (argc == (index + 2)) {
    o = cJSON_Parse(argv[index+1]);
    if (!o) {
      fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
      exit(-1);
    }
  }

  if (!msgname && !filename)
    {
      print_help ();
      exit (-1);
    }

  /* Read message from file */
  if (filename) {
      if (argc > index)
	{
	  fprintf (stderr, "%s: Superfluous arguments when filename given\n",
		   argv[0]);
	  exit (-1);
	}

    FILE *f = fopen(filename, "r");
    size_t chunksize, bufsize;
    size_t n_read = 0;
    size_t n;

    if (!f) {
      fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename);
      exit(-1);
    }

    chunksize = bufsize = 1024;
    char *buf = malloc(bufsize);
    while ((n = fread (buf + n_read, 1, chunksize, f)))
      {
	n_read += n;
	if (n == chunksize)
	  {
	    bufsize += chunksize;
	    buf = realloc (buf, bufsize);
	  }
      }
    fclose(f);
    if (n_read) {
      o = cJSON_Parse(buf);
      if (!o) {
        fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
        exit(-1);
      }
    }
    free (buf);
  }

  if (!o)
    {
      fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
      exit (-1);
    }

  if (vac_connect ("vat2", prefix, 0, 1024))
    {
      fprintf (stderr, "Failed connecting to VPP\n");
      exit (-1);
    }

  if (msgname)
    {
      vat2_exec_command_by_name (msgname, o);
    }
  else
    {
      if (cJSON_IsArray (o))
	{
	  size_t size = cJSON_GetArraySize (o);
	  for (int i = 0; i < size; i++)
	    vat2_exec_command (cJSON_GetArrayItem (o, i));
	}
    }
  cJSON_Delete (o);
  vac_disconnect();
  exit (0);

}