1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
6 #include <vnet/plugin/plugin.h>
8 #include <vlibapi/api.h>
9 #include <vlibmemory/api.h>
10 #include <vpp/app/version.h>
12 #include <vapi/vapi.h>
14 #include <vapi/memclnt.api.vapi.h>
15 #include <vapi/vlib.api.vapi.h>
16 #include <vapi/vpe.api.vapi.h>
19 * Example of how to call the VPP binary API from an internal API client.
20 * Using the VAPI C language binding.
23 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
26 * Connect an VPP binary API client to VPP API
32 if (vapi_ctx_alloc (&ctx) != VAPI_OK)
34 clib_warning ("ctx_alloc failed");
37 if (vapi_connect_from_vpp (ctx, "apifromplugin", 64, 32, VAPI_MODE_BLOCKING,
40 clib_warning ("vapi_connect failed");
48 * Gets called when the show_version_reply message is received
51 show_version_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv,
52 bool is_last, vapi_payload_show_version_reply *p)
55 clib_warning ("Return value: %d", rv);
58 "show_version_reply: program: `%s', version: `%s', build directory: "
59 "`%s', build date: `%s'\n",
60 p->program, p->version, p->build_directory, p->build_date);
65 api_show_version_blocking_fn (void *args)
69 if ((ctx = connect_to_vpp ()) == 0)
70 return clib_error_return (0, "API connection failed");
73 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
74 vapi_error_e vapi_rv = vapi_show_version (ctx, sv, show_version_cb, &called);
75 if (vapi_rv != VAPI_OK)
76 clib_warning ("call failed");
78 vapi_disconnect_from_vpp (ctx);
85 test_api_test_command_fn (vlib_main_t *vm, unformat_input_t *input,
86 vlib_cli_command_t *cmd)
88 /* Run call in a pthread */
90 int rv = pthread_create (&thread, NULL, api_show_version_blocking_fn, 0);
93 return clib_error_return (0, "API call failed");
98 VLIB_CLI_COMMAND (test_api_command, static) = {
99 .path = "test api internal",
100 .short_help = "test internal api client",
101 .function = test_api_test_command_fn,