aboutsummaryrefslogtreecommitdiffstats
path: root/src/vat2
diff options
context:
space:
mode:
Diffstat (limited to 'src/vat2')
-rw-r--r--src/vat2/CMakeLists.txt16
-rw-r--r--src/vat2/jsonconvert.c531
-rw-r--r--src/vat2/jsonconvert.h105
-rw-r--r--src/vat2/main.c253
-rw-r--r--src/vat2/plugin.c7
-rw-r--r--src/vat2/test/vat2_test.c1
-rw-r--r--src/vat2/vat2.h10
-rw-r--r--src/vat2/vat2_helpers.h6
8 files changed, 187 insertions, 742 deletions
diff --git a/src/vat2/CMakeLists.txt b/src/vat2/CMakeLists.txt
index 2afb95e6920..6f843c34661 100644
--- a/src/vat2/CMakeLists.txt
+++ b/src/vat2/CMakeLists.txt
@@ -18,7 +18,6 @@ add_vpp_executable(vat2 ENABLE_EXPORTS
SOURCES
main.c
plugin.c
- jsonconvert.c
DEPENDS api_headers
@@ -28,7 +27,7 @@ add_vpp_executable(vat2 ENABLE_EXPORTS
vppinfra
vppapiclient
Threads::Threads
- rt m dl crypto
+ dl
)
#
@@ -41,23 +40,21 @@ 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
LINK_LIBRARIES
+ vppinfra
vlibmemoryclient
+ vlibapi
svm
- vppinfra
vppapiclient
Threads::Threads
- rt m dl crypto
+ dl
)
#target_link_options(test_vat2 PUBLIC "LINKER:-fsanitize=address")
-
-if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13" AND "${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
+if(VPP_BUILD_TESTS_WITH_COVERAGE)
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,8 +93,7 @@ endif()
##############################################################################
install(
FILES
- jsonconvert.h
vat2_helpers.h
- DESTINATION include/vat2
+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vat2
COMPONENT vpp-dev
)
diff --git a/src/vat2/jsonconvert.c b/src/vat2/jsonconvert.c
deleted file mode 100644
index 1437b90688a..00000000000
--- a/src/vat2/jsonconvert.c
+++ /dev/null
@@ -1,531 +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.
- */
-
-#include <vppinfra/cJSON.h>
-#include <vnet/ethernet/mac_address.h>
-#include <vnet/ip/ip6_packet.h>
-#include <vnet/ip/ip_format_fns.h>
-#include <vpp/api/types.h>
-#include "jsonconvert.h"
-
-#define _(T) \
-int vl_api_ ##T## _fromjson(cJSON *o, T *d) \
-{ \
- if (!cJSON_IsNumber(o)) return -1; \
- memcpy(d, &o->valueint, sizeof(T)); \
- return 0; \
-}
- foreach_vat2_fromjson
-#undef _
-
-int vl_api_bool_fromjson(cJSON *o, bool *d)
-{
- if (!cJSON_IsBool(o)) return -1;
- *d = o->valueint ? true : false;
- return 0;
-}
-
-int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
-{
- unformat_input_t input;
- char *p = cJSON_GetStringValue(o);
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "0x%U", unformat_hex_string, s))
- return -1;
- return 0;
-}
-
-u8 *
-u8string_fromjson(cJSON *o, char *fieldname)
-{
- u8 *s = 0;
- unformat_input_t input;
- cJSON *item = cJSON_GetObjectItem(o, fieldname);
- if (!item) {
- printf("Illegal JSON, no such fieldname %s\n", fieldname);
- return 0;
- }
-
- char *p = cJSON_GetStringValue(item);
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "0x%U", unformat_hex_string, &s))
- return 0;
- return s;
-}
-
-int
-u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
-{
- u8 *s = u8string_fromjson(o, fieldname);
- if (!s)
- return -1;
- memcpy(data, s, vec_len(s));
- vec_free(s);
- return 0;
-}
-
-/* Parse an IP4 address %d.%d.%d.%d. */
-uword
-unformat_ip4_address (unformat_input_t * input, va_list * args)
-{
- u8 *result = va_arg (*args, u8 *);
- unsigned a[4];
-
- if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
- return 0;
-
- if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
- return 0;
-
- result[0] = a[0];
- result[1] = a[1];
- result[2] = a[2];
- result[3] = a[3];
-
- return 1;
-}
-
-/* Parse an IP6 address. */
-uword
-unformat_ip6_address (unformat_input_t * input, va_list * args)
-{
- ip6_address_t *result = va_arg (*args, ip6_address_t *);
- u16 hex_quads[8];
- uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
- uword c, n_colon, double_colon_index;
-
- n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
- double_colon_index = ARRAY_LEN (hex_quads);
- while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
- {
- hex_digit = 16;
- if (c >= '0' && c <= '9')
- hex_digit = c - '0';
- else if (c >= 'a' && c <= 'f')
- hex_digit = c + 10 - 'a';
- else if (c >= 'A' && c <= 'F')
- hex_digit = c + 10 - 'A';
- else if (c == ':' && n_colon < 2)
- n_colon++;
- else
- {
- unformat_put_input (input);
- break;
- }
-
- /* Too many hex quads. */
- if (n_hex_quads >= ARRAY_LEN (hex_quads))
- return 0;
-
- if (hex_digit < 16)
- {
- hex_quad = (hex_quad << 4) | hex_digit;
-
- /* Hex quad must fit in 16 bits. */
- if (n_hex_digits >= 4)
- return 0;
-
- n_colon = 0;
- n_hex_digits++;
- }
-
- /* Save position of :: */
- if (n_colon == 2)
- {
- /* More than one :: ? */
- if (double_colon_index < ARRAY_LEN (hex_quads))
- return 0;
- double_colon_index = n_hex_quads;
- }
-
- if (n_colon > 0 && n_hex_digits > 0)
- {
- hex_quads[n_hex_quads++] = hex_quad;
- hex_quad = 0;
- n_hex_digits = 0;
- }
- }
-
- if (n_hex_digits > 0)
- hex_quads[n_hex_quads++] = hex_quad;
-
-
- {
- word i;
-
- /* Expand :: to appropriate number of zero hex quads. */
- if (double_colon_index < ARRAY_LEN (hex_quads))
- {
- word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
-
- for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
- hex_quads[n_zero + i] = hex_quads[i];
-
- for (i = 0; i < n_zero; i++)
- {
- ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
- hex_quads[double_colon_index + i] = 0;
- }
-
- n_hex_quads = ARRAY_LEN (hex_quads);
- }
-
- /* Too few hex quads given. */
- if (n_hex_quads < ARRAY_LEN (hex_quads))
- return 0;
-
- for (i = 0; i < ARRAY_LEN (hex_quads); i++)
- result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
-
- return 1;
- }
-}
-
-u8 *
-format_ip6_address (u8 * s, va_list * args)
-{
- ip6_address_t *a = va_arg (*args, ip6_address_t *);
- u32 max_zero_run = 0, this_zero_run = 0;
- int max_zero_run_index = -1, this_zero_run_index = 0;
- int in_zero_run = 0, i;
- int last_double_colon = 0;
-
- /* Ugh, this is a pain. Scan forward looking for runs of 0's */
- for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
- {
- if (a->as_u16[i] == 0)
- {
- if (in_zero_run)
- this_zero_run++;
- else
- {
- in_zero_run = 1;
- this_zero_run = 1;
- this_zero_run_index = i;
- }
- }
- else
- {
- if (in_zero_run)
- {
- /* offer to compress the biggest run of > 1 zero */
- if (this_zero_run > max_zero_run && this_zero_run > 1)
- {
- max_zero_run_index = this_zero_run_index;
- max_zero_run = this_zero_run;
- }
- }
- in_zero_run = 0;
- this_zero_run = 0;
- }
- }
-
- if (in_zero_run)
- {
- if (this_zero_run > max_zero_run && this_zero_run > 1)
- {
- max_zero_run_index = this_zero_run_index;
- max_zero_run = this_zero_run;
- }
- }
-
- for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
- {
- if (i == max_zero_run_index)
- {
- s = format (s, "::");
- i += max_zero_run - 1;
- last_double_colon = 1;
- }
- else
- {
- s = format (s, "%s%x",
- (last_double_colon || i == 0) ? "" : ":",
- clib_net_to_host_u16 (a->as_u16[i]));
- last_double_colon = 0;
- }
- }
-
- return s;
-}
-
-int
-vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip4_address_t *a)
-{
- unformat_input_t input;
- char *p = cJSON_GetStringValue(o);
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "%U", unformat_ip4_address, a))
- return -1;
- return 0;
-}
-
-int
-vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip4_prefix_t *a)
-{
- unformat_input_t input;
- char *p = cJSON_GetStringValue(o);
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address,
- &a->len))
- return -1;
- return 0;
-}
-
-int
-vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip4_prefix_t *a)
-{
- return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
-}
-int
-vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip6_address_t *a)
-{
- unformat_input_t input;
- char *p = cJSON_GetStringValue(o);
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "%U", unformat_ip6_address, a))
- return -1;
- return 0;
-}
-
-int
-vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip6_prefix_t *a)
-{
- unformat_input_t input;
- char *p = cJSON_GetStringValue(o);
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len))
- return -1;
- return 0;
-}
-
-int
-vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_ip6_prefix_t *a)
-{
- return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
-}
-
-int
-vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
-{
- unformat_input_t input;
-
- char *p = cJSON_GetStringValue(o);
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
- a->af = ADDRESS_IP4;
- else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
- a->af = ADDRESS_IP6;
- else
- return -1;
- return 0;
-}
-
-int
-vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
-{
- unformat_input_t input;
-
- char *p = cJSON_GetStringValue(o);
-
- if (!p)
- return -1;
- unformat_init_string (&input, p, strlen(p));
- int plen;
- if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
- a->address.af = ADDRESS_IP4;
- else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
- a->address.af = ADDRESS_IP6;
- else
- return -1;
- a->len = plen;
- return 0;
-}
-
-int
-vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_prefix_t *a)
-{
- return vl_api_prefix_t_fromjson(mp, len, o, a);
-}
-
-uword
-unformat_mac_address (unformat_input_t * input, va_list * args)
-{
- mac_address_t *mac = va_arg (*args, mac_address_t *);
- u32 i, a[3];
-
- if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
- 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
- 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
- return (1);
- else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
- {
- for (i = 0; i < ARRAY_LEN (a); i++)
- if (a[i] >= (1 << 16))
- return 0;
-
- mac->bytes[0] = (a[0] >> 8) & 0xff;
- mac->bytes[1] = (a[0] >> 0) & 0xff;
- mac->bytes[2] = (a[1] >> 8) & 0xff;
- mac->bytes[3] = (a[1] >> 0) & 0xff;
- mac->bytes[4] = (a[2] >> 8) & 0xff;
- mac->bytes[5] = (a[2] >> 0) & 0xff;
-
- return (1);
- }
- return (0);
-}
-
-int
-vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
- vl_api_mac_address_t *a)
-{
- unformat_input_t input;
-
- char *p = cJSON_GetStringValue(o);
- unformat_init_string (&input, p, strlen(p));
- if (!unformat (&input, "%U", unformat_mac_address, a))
- return -1;
- return 0;
-}
-
-/* Format an IP4 address. */
-u8 *
-format_ip4_address (u8 * s, va_list * args)
-{
- u8 *a = va_arg (*args, u8 *);
- return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
-}
-
-int
-vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
-{
- /* copy without nul terminator */
- u32 len = strlen (buf);
- if (len > 0)
- clib_memcpy_fast (str->buf, buf, len);
- str->length = htonl (len);
- return len + sizeof (u32);
-}
-
-u8 *
-format_vl_api_interface_index_t (u8 *s, va_list *args)
-{
- u32 *a = va_arg (*args, u32 *);
- return format (s, "%u", *a);
-}
-
-void
-vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
-{
-
- if (astr == 0) return;
- u32 length = clib_net_to_host_u32 (astr->length);
-
- char *cstr = malloc(length + 1);
- memcpy(cstr, astr->buf, length);
- cstr[length] = '\0';
- cJSON_AddStringToObject(object, name, cstr);
- free(cstr);
-}
-
-u8 *
-format_vl_api_timestamp_t(u8 * s, va_list * args)
-{
- f64 timestamp = va_arg (*args, f64);
- struct tm *tm;
- word msec;
-
- time_t t = timestamp;
- tm = gmtime (&t);
- msec = 1e6 * (timestamp - t);
- return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
- 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
- tm->tm_sec, msec);
-}
-
-u8 *
-format_vl_api_timedelta_t(u8 * s, va_list * args)
-{
- return format_vl_api_timestamp_t(s, args);
-}
-
-uword
-unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
-{
- return 0;
-}
-
-uword
-unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
-{
- return 0;
-}
-u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
-{
- return 0;
-}
-uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
-{
- return 0;
-}
-
-cJSON *
-vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
- return vl_api_ip4_prefix_t_tojson (a);
-}
-cJSON *
-vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
- return vl_api_ip6_prefix_t_tojson (a);
-}
-cJSON *
-vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
- return vl_api_prefix_t_tojson (a);
-}
-u8 *
-format_vl_api_mac_address_t (u8 * s, va_list * args)
-{
- const mac_address_t *mac = va_arg (*args, mac_address_t *);
-
- return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
- mac->bytes[0], mac->bytes[1], mac->bytes[2],
- mac->bytes[3], mac->bytes[4], mac->bytes[5]);
-}
-#define _(T) \
- cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
- u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
- cJSON *o = cJSON_CreateString((char *)s); \
- vec_free(s); \
- return o; \
- }
-foreach_vat2_tojson
-#undef _
diff --git a/src/vat2/jsonconvert.h b/src/vat2/jsonconvert.h
deleted file mode 100644
index 038ad74ac0e..00000000000
--- a/src/vat2/jsonconvert.h
+++ /dev/null
@@ -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
diff --git a/src/vat2/main.c b/src/vat2/main.c
index 208e0c5d2c2..bf415854db1 100644
--- a/src/vat2/main.c
+++ b/src/vat2/main.c
@@ -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,12 +30,40 @@
#include <limits.h>
#include "vat2.h"
+bool vat2_debug;
+
+/*
+ * 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 ()
+static u8 *
+vat2_find_plugin_path (void)
{
char *p, path[PATH_MAX];
int rv;
@@ -43,45 +71,56 @@ vat2_find_plugin_path ()
/* find executable path */
if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
- return;
+ return 0;
/* readlink doesn't provide null termination */
path[rv] = 0;
/* strip filename */
if ((p = strrchr (path, '/')) == 0)
- return;
+ return 0;
*p = 0;
/* strip bin/ */
if ((p = strrchr (path, '/')) == 0)
- return;
+ return 0;
*p = 0;
- s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vat2_plugins:"
- "%s/lib/vat2_plugins", path, path);
+ s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
vec_add1 (s, 0);
- vat2_plugin_path = (char *) s;
+ return 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);
+ 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);
+int vat2_load_plugins (u8 *path, char *filter, int *loaded);
static int
-register_function (void)
+register_function (char *pluginpath)
{
int loaded;
+ u8 *vat2_plugin_path = 0;
+
+ if (pluginpath == 0)
+ {
+ vat2_plugin_path = vat2_find_plugin_path ();
+ }
+ else
+ {
+ vat2_plugin_path = format (0, "%s", pluginpath);
+ vec_add1 (vat2_plugin_path, 0);
+ }
+ DBG ("Plugin Path %s\n", vat2_plugin_path);
+ int rv = vat2_load_plugins (vat2_plugin_path, 0, &loaded);
+ DBG ("Loaded %u plugins\n", loaded);
+
+ vec_free (vat2_plugin_path);
- 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;
}
@@ -89,15 +128,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 +145,27 @@ vat2_register_function (char *name, cJSON (*f) (cJSON *),
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", msgname);
+ 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;
@@ -143,9 +198,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)
{
@@ -196,42 +252,44 @@ print_help (void)
"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 Specify shared memory prefix to connect to a given VPP "
- "instance\n"
- "-f, --file File containing a JSON object with the arguments for "
- "the message to send\n"
- "--dump-apis List all APIs available in VAT2 (might not reflect "
- "running VPP)\n"
- "-t, --template Print a template JSON object for given API message\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"
+ "--plugin-path Pluing path"
"\n";
printf ("%s", help_string);
}
-int main (int argc, char **argv)
+int
+main (int argc, char **argv)
{
/* Create a heap of 64MB */
clib_mem_init (0, 64 << 20);
- char *filename = 0, *prefix = 0;
+ char *filename = 0, *prefix = 0, *template = 0, *pluginpath = 0;
int index;
int c;
opterr = 0;
cJSON *o = 0;
int option_index = 0;
bool dump_api = false;
- bool template = false;
char *msgname = 0;
- static int debug_flag;
static struct option long_options[] = {
- { "debug", no_argument, &debug_flag, 1 },
- { "prefix", optional_argument, 0, 'p' },
+ { "debug", no_argument, 0, 'd' },
+ { "prefix", required_argument, 0, 's' },
{ "file", required_argument, 0, 'f' },
{ "dump-apis", no_argument, 0, 0 },
- { "template", no_argument, 0, 't' },
+ { "template", required_argument, 0, 't' },
+ { "plugin-path", required_argument, 0, 'p' },
{ 0, 0, 0, 0 }
};
- while ((c = getopt_long (argc, argv, "hdp:f:", long_options,
+ while ((c = getopt_long (argc, argv, "hdp:f:t:", long_options,
&option_index)) != -1)
{
switch (c)
@@ -241,17 +299,20 @@ int main (int argc, char **argv)
dump_api = true;
break;
case 'd':
- debug = true;
+ vat2_debug = true;
break;
case 't':
- template = true;
+ template = optarg;
break;
- case 'p':
+ case 's':
prefix = optarg;
break;
case 'f':
filename = optarg;
break;
+ case 'p':
+ pluginpath = optarg;
+ break;
case '?':
print_help ();
return 1;
@@ -259,9 +320,8 @@ int main (int argc, char **argv)
abort ();
}
}
- debug = debug_flag == 1 ? true : false;
- DBG ("debug = %d, filename = %s shared memory prefix: %s\n", debug, filename,
- prefix);
+ DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n",
+ vat2_debug, filename, template, prefix);
for (index = optind; index < argc; index++)
DBG ("Non-option argument %s\n", argv[index]);
@@ -276,15 +336,16 @@ int main (int argc, char **argv)
/* 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);
- }
+ int res = register_function (pluginpath);
+ if (res < 0)
+ {
+ fprintf (stderr, "%s: loading plugins failed\n", argv[0]);
+ exit (-1);
+ }
if (template)
{
- print_template (argv[index]);
+ print_template (template);
exit (0);
}
@@ -299,16 +360,26 @@ int main (int argc, char **argv)
{
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 (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 (filename)
+ {
if (argc > index)
{
fprintf (stderr, "%s: Superfluous arguments when filename given\n",
@@ -316,40 +387,45 @@ int main (int argc, char **argv)
exit (-1);
}
- FILE *f = fopen(filename, "r");
- size_t chunksize, bufsize;
- size_t n_read = 0;
- size_t n;
+ 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);
- free(buf);
- if (!o) {
- fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
- exit(-1);
- }
+ 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 (!msgname && !filename)
+ if (!o)
{
- print_help ();
+ fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
exit (-1);
}
@@ -373,7 +449,6 @@ int main (int argc, char **argv)
}
}
cJSON_Delete (o);
- vac_disconnect();
+ vac_disconnect ();
exit (0);
-
}
diff --git a/src/vat2/plugin.c b/src/vat2/plugin.c
index 6b6d55ac9b0..aaaf6940ef3 100644
--- a/src/vat2/plugin.c
+++ b/src/vat2/plugin.c
@@ -76,8 +76,9 @@ load_one_plugin (plugin_info_t * pi)
return 0;
}
+/* Takes a vector as argument */
static u8 **
-split_plugin_path (char *plugin_path)
+split_plugin_path (u8 *plugin_path)
{
int i;
u8 **rv = 0;
@@ -104,7 +105,7 @@ split_plugin_path (char *plugin_path)
}
int
-vat2_load_plugins (char *path, char *filter, int *loaded)
+vat2_load_plugins (u8 *path, char *filter, int *loaded)
{
DIR *dp;
struct dirent *entry;
@@ -165,7 +166,7 @@ vat2_load_plugins (char *path, char *filter, int *loaded)
{
res = -1;
vec_free (plugin_name);
- _vec_len (plugin_info) = vec_len (plugin_info) - 1;
+ vec_set_len (plugin_info, vec_len (plugin_info) - 1);
continue;
}
clib_memset (pi, 0, sizeof (*pi));
diff --git a/src/vat2/test/vat2_test.c b/src/vat2/test/vat2_test.c
index 1ac46527b3c..7aa5e71296e 100644
--- a/src/vat2/test/vat2_test.c
+++ b/src/vat2/test/vat2_test.c
@@ -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)
diff --git a/src/vat2/vat2.h b/src/vat2/vat2.h
index d477b7279b3..acdb85368cc 100644
--- a/src/vat2/vat2.h
+++ b/src/vat2/vat2.h
@@ -3,9 +3,15 @@
#include <stdbool.h>
-extern bool debug;
+extern bool vat2_debug;
-#define DBG(fmt, args...) do {if (debug) fprintf(stderr, fmt, ## args); } while(0)
+#define DBG(fmt, args...) \
+ do \
+ { \
+ if (vat2_debug) \
+ fprintf (stderr, fmt, ##args); \
+ } \
+ while (0)
#define ERR(fmt, args...) fprintf(stderr, "VAT2: %s:%d:%s(): " fmt, \
__FILE__, __LINE__, __func__, ##args)
diff --git a/src/vat2/vat2_helpers.h b/src/vat2/vat2_helpers.h
index 929c012485f..d9ce2af6b35 100644
--- a/src/vat2/vat2_helpers.h
+++ b/src/vat2/vat2_helpers.h
@@ -16,9 +16,11 @@
#ifndef included_vat2_helpers_h
#define included_vat2_helpers_h
+#include <vlibmemory/vlib.api_types.h>
+
/* For control ping */
#define vl_endianfun
-#include <vpp/api/vpe.api.h>
+#include <vlibmemory/memclnt.api.h>
#undef vl_endianfun
static inline void
@@ -27,7 +29,7 @@ vat2_control_ping (u32 context)
vl_api_control_ping_t mp = {0};
mp._vl_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_CRC);
mp.context = context;
- vl_api_control_ping_t_endian(&mp);
+ vl_api_control_ping_t_endian (&mp, 1 /* to network */);
vac_write((char *)&mp, sizeof(mp));
}