From 2ca88ff97884ec9ed20a853b13cee6d86f9c9d0f Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Thu, 27 Jan 2022 16:25:43 +0100 Subject: vapi: support api clients within vpp process Add vapi_connect_from_vpp() and vapi_disconnect_from_vpp() calls to allow API clients from within VPP process. Add a new memclnt_create version that gives the user a knob to enable or disable dead client scans (keepalive). Type: feature Signed-off-by: Ole Troan Change-Id: Id0b7bb89308db3a3aed2d3fcbedf4e1282dcd03f Signed-off-by: Ole Troan --- src/plugins/unittest/CMakeLists.txt | 4 +- src/plugins/unittest/api_test.c | 101 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/plugins/unittest/api_test.c (limited to 'src/plugins') diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index 34e47fa24f4..faf55dfec2d 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -15,9 +15,10 @@ set(chacha20_poly1305) if (OPENSSL_VERSION VERSION_GREATER_EQUAL 1.1.0) set(chacha20_poly1305 crypto/chacha20_poly1305.c) endif() - +include_directories(${CMAKE_SOURCE_DIR}/vpp-api ${CMAKE_CURRENT_BINARY_DIR}/../../vpp-api) add_vpp_plugin(unittest SOURCES + api_test.c api_fuzz_test.c bier_test.c bihash_test.c @@ -60,4 +61,5 @@ add_vpp_plugin(unittest COMPONENT vpp-plugin-devtools + LINK_LIBRARIES vapiclient ) diff --git a/src/plugins/unittest/api_test.c b/src/plugins/unittest/api_test.c new file mode 100644 index 00000000000..4bed50c2969 --- /dev/null +++ b/src/plugins/unittest/api_test.c @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2022 Cisco Systems, Inc. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +/* + * Example of how to call the VPP binary API from an internal API client. + * Using the VAPI C language binding. + */ + +DEFINE_VAPI_MSG_IDS_VPE_API_JSON; + +/* + * Connect an VPP binary API client to VPP API + */ +static vapi_ctx_t +connect_to_vpp (void) +{ + vapi_ctx_t ctx; + if (vapi_ctx_alloc (&ctx) != VAPI_OK) + { + clib_warning ("ctx_alloc failed"); + return 0; + } + if (vapi_connect_from_vpp (ctx, "apifromplugin", 64, 32, VAPI_MODE_BLOCKING, + true) != VAPI_OK) + { + clib_warning ("vapi_connect failed"); + return 0; + } + return ctx; +} + +/* + * Gets called when the show_version_reply message is received + */ +vapi_error_e +show_version_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, + bool is_last, vapi_payload_show_version_reply *p) +{ + if (rv != VAPI_OK) + clib_warning ("Return value: %d", rv); + fformat ( + stdout, + "show_version_reply: program: `%s', version: `%s', build directory: " + "`%s', build date: `%s'\n", + p->program, p->version, p->build_directory, p->build_date); + return VAPI_OK; +} + +static void * +api_show_version_blocking_fn (void *args) +{ + vapi_ctx_t ctx; + + if ((ctx = connect_to_vpp ()) == 0) + return clib_error_return (0, "API connection failed"); + + int called; + vapi_msg_show_version *sv = vapi_alloc_show_version (ctx); + vapi_error_e vapi_rv = vapi_show_version (ctx, sv, show_version_cb, &called); + if (vapi_rv != VAPI_OK) + clib_warning ("call failed"); + + vapi_disconnect_from_vpp (ctx); + vapi_ctx_free (ctx); + + return 0; +} + +static clib_error_t * +test_api_test_command_fn (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd) +{ + /* Run call in a pthread */ + pthread_t thread; + int rv = pthread_create (&thread, NULL, api_show_version_blocking_fn, 0); + if (rv) + { + return clib_error_return (0, "API call failed"); + } + return 0; +} + +VLIB_CLI_COMMAND (test_api_command, static) = { + .path = "test api internal", + .short_help = "test internal api client", + .function = test_api_test_command_fn, +}; -- cgit 1.2.3-korg