2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 *------------------------------------------------------------------
17 * sample_test.c - test harness plugin
18 *------------------------------------------------------------------
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
27 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
29 /* Declare message IDs */
30 #include <sample/sample_msg_enum.h>
32 /* define message structures */
34 #include <sample/sample_all_api_h.h>
37 /* declare message handlers for each api */
39 #define vl_endianfun /* define message structures */
40 #include <sample/sample_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...)
46 #include <sample/sample_all_api_h.h>
49 /* Get the API version number. */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <sample/sample_all_api_h.h>
56 /* API message ID base */
61 sample_test_main_t sample_test_main;
63 #define foreach_standard_reply_retval_handler \
64 _(sample_macswap_enable_disable_reply)
67 static void vl_api_##n##_t_handler \
68 (vl_api_##n##_t * mp) \
70 vat_main_t * vam = sample_test_main.vat_main; \
71 i32 retval = ntohl(mp->retval); \
72 if (vam->async_mode) { \
73 vam->async_errors += (retval < 0); \
75 vam->retval = retval; \
76 vam->result_ready = 1; \
79 foreach_standard_reply_retval_handler;
83 * Table of message reply handlers, must include boilerplate handlers
86 #define foreach_vpe_api_reply_msg \
87 _(SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY, sample_macswap_enable_disable_reply)
90 /* M: construct, but don't yet send a message */
94 vam->result_ready = 0; \
95 mp = vl_msg_api_alloc(sizeof(*mp)); \
96 memset (mp, 0, sizeof (*mp)); \
97 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
98 mp->client_index = vam->my_client_index; \
103 vam->result_ready = 0; \
104 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
105 memset (mp, 0, sizeof (*mp)); \
106 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
107 mp->client_index = vam->my_client_index; \
110 /* S: send a message */
111 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
113 /* W: wait for results, with timeout */
116 timeout = vat_time_now (vam) + 1.0; \
118 while (vat_time_now (vam) < timeout) { \
119 if (vam->result_ready == 1) { \
120 return (vam->retval); \
126 static int api_sample_macswap_enable_disable (vat_main_t * vam)
128 sample_test_main_t * sm = &sample_test_main;
129 unformat_input_t * i = vam->input;
131 int enable_disable = 1;
132 u32 sw_if_index = ~0;
133 vl_api_sample_macswap_enable_disable_t * mp;
135 /* Parse args required to build the message */
136 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
137 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
139 else if (unformat (i, "sw_if_index %d", &sw_if_index))
141 else if (unformat (i, "disable"))
147 if (sw_if_index == ~0) {
148 errmsg ("missing interface name / explicit sw_if_index number \n");
152 /* Construct the API message */
153 M(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable);
154 mp->sw_if_index = ntohl (sw_if_index);
155 mp->enable_disable = enable_disable;
160 /* Wait for a reply... */
165 * List of messages that the api test plugin sends,
166 * and that the data plane plugin processes
168 #define foreach_vpe_api_msg \
169 _(sample_macswap_enable_disable, "<intfc> [disable]")
171 void vat_api_hookup (vat_main_t *vam)
173 sample_test_main_t * sm = &sample_test_main;
174 /* Hook up handlers for replies from the data plane plug-in */
176 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
178 vl_api_##n##_t_handler, \
180 vl_api_##n##_t_endian, \
181 vl_api_##n##_t_print, \
182 sizeof(vl_api_##n##_t), 1);
183 foreach_vpe_api_reply_msg;
186 /* API messages we can send */
187 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
192 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
197 clib_error_t * vat_plugin_register (vat_main_t *vam)
199 sample_test_main_t * sm = &sample_test_main;
204 name = format (0, "sample_%08x%c", api_version, 0);
205 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
207 if (sm->msg_id_base != (u16) ~0)
208 vat_api_hookup (vam);