api: API trace improvements
Type: improvement * add support for JSON format in API trace * add ability to replay JSON API trace in both VPP and VAT2 * use CRC for backward compatibility check during JSON API replay * fix API trace CLI (and remove duplicits) * remove custom dump * remove vppapitrace.py * update docs accordingly Change-Id: I5294f68bebe6cbe738630f457f3a87720e06486b Signed-off-by: Filip Tehlar <ftehlar@cisco.com> Signed-off-by: Ole Troan <ot@cisco.com>
This commit is contained in:
@ -18,7 +18,6 @@ add_vpp_executable(vat2 ENABLE_EXPORTS
|
||||
SOURCES
|
||||
main.c
|
||||
plugin.c
|
||||
jsonconvert.c
|
||||
|
||||
DEPENDS api_headers
|
||||
|
||||
@ -41,7 +40,6 @@ vpp_generate_api_c_header (test/vat2_test.api)
|
||||
add_vpp_executable(test_vat2 ENABLE_EXPORTS NO_INSTALL
|
||||
SOURCES
|
||||
test/vat2_test.c
|
||||
jsonconvert.c
|
||||
|
||||
DEPENDS api_headers
|
||||
|
||||
@ -57,7 +55,6 @@ add_vpp_executable(test_vat2 ENABLE_EXPORTS NO_INSTALL
|
||||
|
||||
if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13" AND "${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
|
||||
set(TARGET_NAME test_vat2)
|
||||
set(COV_SOURCES ${CMAKE_SOURCE_DIR}/vat2/jsonconvert.c)
|
||||
|
||||
message("Building with llvm Code Coverage Tools ${TARGET_NAME}")
|
||||
target_compile_options(${TARGET_NAME} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
|
||||
@ -96,7 +93,6 @@ endif()
|
||||
##############################################################################
|
||||
install(
|
||||
FILES
|
||||
jsonconvert.h
|
||||
vat2_helpers.h
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vat2
|
||||
COMPONENT vpp-dev
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,105 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef included_json_convert_h
|
||||
#define included_json_convert_h
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <vppinfra/cJSON.h>
|
||||
#include <vnet/ethernet/mac_address.h>
|
||||
#include <vnet/ip/ip6_packet.h>
|
||||
#include <vnet/ip/ip_types.api_types.h>
|
||||
#include <vnet/ethernet/ethernet_types.api_types.h>
|
||||
|
||||
#define foreach_vat2_fromjson \
|
||||
_(i8) \
|
||||
_(u8) \
|
||||
_(i16) \
|
||||
_(u16) \
|
||||
_(i32) \
|
||||
_(u32) \
|
||||
_(u64) \
|
||||
_(f64)
|
||||
|
||||
#define _(T) \
|
||||
int vl_api_ ##T## _fromjson(cJSON *o, T *d);
|
||||
foreach_vat2_fromjson
|
||||
#undef _
|
||||
|
||||
/* Prototypes */
|
||||
int
|
||||
vl_api_bool_fromjson (cJSON *o, bool *d);
|
||||
int vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip4_address_t *a);
|
||||
int vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip4_prefix_t *a);
|
||||
int vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip4_prefix_t *a);
|
||||
int vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip6_address_t *a);
|
||||
int vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip6_prefix_t *a);
|
||||
int vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_ip6_prefix_t *a);
|
||||
int vl_api_address_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_address_t *a);
|
||||
int vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_prefix_t *a);
|
||||
int vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_prefix_t *a);
|
||||
int vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
|
||||
vl_api_mac_address_t *a);
|
||||
|
||||
uword unformat_ip4_address (unformat_input_t *input, va_list *args);
|
||||
uword unformat_ip6_address (unformat_input_t *input, va_list *args);
|
||||
u8 *format_ip6_address (u8 *s, va_list *args);
|
||||
uword unformat_mac_address (unformat_input_t *input, va_list *args);
|
||||
u8 *format_ip4_address (u8 *s, va_list *args);
|
||||
u8 *format_vl_api_interface_index_t (u8 *s, va_list *args);
|
||||
u8 *format_vl_api_timestamp_t (u8 *s, va_list *args);
|
||||
u8 *format_vl_api_timedelta_t (u8 *s, va_list *args);
|
||||
uword unformat_vl_api_timedelta_t (unformat_input_t *input, va_list *args);
|
||||
uword unformat_vl_api_timestamp_t (unformat_input_t *input, va_list *args);
|
||||
u8 *format_vl_api_gbp_scope_t (u8 *s, va_list *args);
|
||||
uword unformat_vl_api_gbp_scope_t (unformat_input_t *input, va_list *args);
|
||||
|
||||
int vl_api_c_string_to_api_string (const char *buf, vl_api_string_t *str);
|
||||
void vl_api_string_cJSON_AddToObject (cJSON *const object,
|
||||
const char *const name,
|
||||
vl_api_string_t *astr);
|
||||
|
||||
u8 *u8string_fromjson (cJSON *o, char *fieldname);
|
||||
int u8string_fromjson2 (cJSON *o, char *fieldname, u8 *data);
|
||||
int vl_api_u8_string_fromjson (cJSON *o, u8 *s, int len);
|
||||
|
||||
#define foreach_vat2_tojson \
|
||||
_(ip4_address) \
|
||||
_(ip4_prefix) \
|
||||
_(ip6_address) \
|
||||
_(ip6_prefix) \
|
||||
_(address) \
|
||||
_(prefix) \
|
||||
_(mac_address)
|
||||
|
||||
#define _(T) \
|
||||
cJSON *vl_api_ ##T## _t_tojson(vl_api_ ##T## _t *);
|
||||
foreach_vat2_tojson
|
||||
#undef _
|
||||
|
||||
cJSON *vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a);
|
||||
cJSON *vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a);
|
||||
cJSON *vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a);
|
||||
|
||||
#endif
|
@ -18,7 +18,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <getopt.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <vlib/vlib.h>
|
||||
#include <vlibapi/api_types.h>
|
||||
#include <vppinfra/hash.h>
|
||||
@ -30,6 +30,33 @@
|
||||
#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;
|
||||
|
||||
@ -89,15 +116,16 @@ 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 *))
|
||||
cJSON (*tojson) (void *), u32 crc)
|
||||
{
|
||||
struct apifuncs_s funcs = { .f = f, .tojson = tojson };
|
||||
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);
|
||||
}
|
||||
@ -105,12 +133,28 @@ vat2_register_function (char *name, cJSON (*f) (cJSON *),
|
||||
static int
|
||||
vat2_exec_command_by_name (char *msgname, cJSON *o)
|
||||
{
|
||||
if (filter_message (msgname))
|
||||
return 0;
|
||||
|
||||
cJSON *crc_obj = cJSON_GetObjectItem (o, "_crc");
|
||||
if (!crc_obj)
|
||||
{
|
||||
fprintf (stderr, "Missing '_crc' element!\n");
|
||||
return -1;
|
||||
}
|
||||
char *crc_str = cJSON_GetStringValue (crc_obj);
|
||||
u32 crc = (u32) strtol (crc_str, NULL, 16);
|
||||
|
||||
uword *p = hash_get_mem (function_by_name, msgname);
|
||||
if (!p)
|
||||
{
|
||||
fprintf (stderr, "No such command %s", msgname);
|
||||
fprintf (stderr, "No such command %s\n", msgname);
|
||||
return -1;
|
||||
}
|
||||
if (crc != apifuncs[p[0]].crc)
|
||||
{
|
||||
fprintf (stderr, "API CRC does not match: %s!\n", msgname);
|
||||
}
|
||||
|
||||
cJSON *(*fp) (cJSON *);
|
||||
fp = (void *) apifuncs[p[0]].f;
|
||||
@ -143,9 +187,10 @@ vat2_exec_command (cJSON *o)
|
||||
}
|
||||
|
||||
char *name = cJSON_GetStringValue (msg_id_obj);
|
||||
assert (name);
|
||||
|
||||
return vat2_exec_command_by_name (name, o);
|
||||
}
|
||||
|
||||
static void
|
||||
print_template (char *msgname)
|
||||
{
|
||||
@ -307,6 +352,12 @@ int main (int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (!msgname && !filename)
|
||||
{
|
||||
print_help ();
|
||||
exit (-1);
|
||||
}
|
||||
|
||||
/* Read message from file */
|
||||
if (filename) {
|
||||
if (argc > index)
|
||||
@ -325,6 +376,7 @@ int main (int argc, char **argv)
|
||||
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)))
|
||||
@ -339,17 +391,17 @@ int main (int argc, char **argv)
|
||||
fclose(f);
|
||||
if (n_read) {
|
||||
o = cJSON_Parse(buf);
|
||||
free(buf);
|
||||
if (!o) {
|
||||
fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
free (buf);
|
||||
}
|
||||
|
||||
if (!msgname && !filename)
|
||||
if (!o)
|
||||
{
|
||||
print_help ();
|
||||
fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
|
||||
exit (-1);
|
||||
}
|
||||
|
||||
|
@ -196,6 +196,7 @@ struct tests tests[] = {
|
||||
"[\"2001:db8::23\", \"2001:db8::23\"] }" },
|
||||
{ .s = "{\"_msgname\": \"test_empty\"}" },
|
||||
{ .s = "{\"_msgname\": \"test_interface\", \"sw_if_index\": 100 }" },
|
||||
{ .s = "{\"_msgname\": \"test_interface\", \"sw_if_index\": 4294967295 }" },
|
||||
};
|
||||
|
||||
int main (int argc, char **argv)
|
||||
|
Reference in New Issue
Block a user