blob: b3e71c760ac327019c69d93c641851ea851a97c4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 *------------------------------------------------------------------
17 * sample_test.c - test harness plugin
18 *------------------------------------------------------------------
19 */
20
21#include <vat/vat.h>
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vlibsocket/api.h>
25#include <vppinfra/error.h>
Dave Barach2d6b2d62017-01-25 16:32:08 -050026
27#define __plugin_msg_base sample_test_main.msg_id_base
Dave Barachfe6bdfd2017-01-20 19:50:09 -050028#include <vlibapi/vat_helper_macros.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
30uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
31
32/* Declare message IDs */
33#include <sample/sample_msg_enum.h>
34
35/* define message structures */
36#define vl_typedefs
37#include <sample/sample_all_api_h.h>
38#undef vl_typedefs
39
40/* declare message handlers for each api */
41
42#define vl_endianfun /* define message structures */
43#include <sample/sample_all_api_h.h>
44#undef vl_endianfun
45
46/* instantiate all the print functions we know about */
47#define vl_print(handle, ...)
48#define vl_printfun
49#include <sample/sample_all_api_h.h>
50#undef vl_printfun
51
52/* Get the API version number. */
53#define vl_api_version(n,v) static u32 api_version=(v);
54#include <sample/sample_all_api_h.h>
55#undef vl_api_version
56
57
58typedef struct {
59 /* API message ID base */
60 u16 msg_id_base;
61 vat_main_t *vat_main;
62} sample_test_main_t;
63
64sample_test_main_t sample_test_main;
65
66#define foreach_standard_reply_retval_handler \
67_(sample_macswap_enable_disable_reply)
68
69#define _(n) \
70 static void vl_api_##n##_t_handler \
71 (vl_api_##n##_t * mp) \
72 { \
73 vat_main_t * vam = sample_test_main.vat_main; \
74 i32 retval = ntohl(mp->retval); \
75 if (vam->async_mode) { \
76 vam->async_errors += (retval < 0); \
77 } else { \
78 vam->retval = retval; \
79 vam->result_ready = 1; \
80 } \
81 }
82foreach_standard_reply_retval_handler;
83#undef _
84
85/*
86 * Table of message reply handlers, must include boilerplate handlers
87 * we just generated
88 */
89#define foreach_vpe_api_reply_msg \
90_(SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY, sample_macswap_enable_disable_reply)
91
92
Ed Warnickecb9cada2015-12-08 15:45:58 -070093static int api_sample_macswap_enable_disable (vat_main_t * vam)
94{
95 sample_test_main_t * sm = &sample_test_main;
96 unformat_input_t * i = vam->input;
97 f64 timeout;
98 int enable_disable = 1;
99 u32 sw_if_index = ~0;
100 vl_api_sample_macswap_enable_disable_t * mp;
Jon Loeliger56c7b012017-02-01 12:31:41 -0600101 int ret;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 /* Parse args required to build the message */
104 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
105 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
106 ;
107 else if (unformat (i, "sw_if_index %d", &sw_if_index))
108 ;
109 else if (unformat (i, "disable"))
110 enable_disable = 0;
111 else
112 break;
113 }
114
115 if (sw_if_index == ~0) {
116 errmsg ("missing interface name / explicit sw_if_index number \n");
117 return -99;
118 }
119
120 /* Construct the API message */
Jon Loeliger8a2aea32017-01-31 13:19:40 -0600121 M(SAMPLE_MACSWAP_ENABLE_DISABLE, mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 mp->sw_if_index = ntohl (sw_if_index);
123 mp->enable_disable = enable_disable;
124
125 /* send it... */
Jon Loeliger7bc770c2017-01-31 14:03:33 -0600126 S(mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
128 /* Wait for a reply... */
Jon Loeliger56c7b012017-02-01 12:31:41 -0600129 W (ret);
130 return ret;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131}
132
133/*
134 * List of messages that the api test plugin sends,
135 * and that the data plane plugin processes
136 */
137#define foreach_vpe_api_msg \
138_(sample_macswap_enable_disable, "<intfc> [disable]")
139
Dave Barach99617f72017-03-04 08:35:48 -0500140static void sample_api_hookup (vat_main_t *vam)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141{
142 sample_test_main_t * sm = &sample_test_main;
143 /* Hook up handlers for replies from the data plane plug-in */
144#define _(N,n) \
145 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
146 #n, \
147 vl_api_##n##_t_handler, \
148 vl_noop_handler, \
149 vl_api_##n##_t_endian, \
150 vl_api_##n##_t_print, \
151 sizeof(vl_api_##n##_t), 1);
152 foreach_vpe_api_reply_msg;
153#undef _
154
155 /* API messages we can send */
156#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
157 foreach_vpe_api_msg;
158#undef _
159
160 /* Help strings */
161#define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
162 foreach_vpe_api_msg;
163#undef _
164}
165
166clib_error_t * vat_plugin_register (vat_main_t *vam)
167{
168 sample_test_main_t * sm = &sample_test_main;
169 u8 * name;
170
171 sm->vat_main = vam;
172
173 name = format (0, "sample_%08x%c", api_version, 0);
174 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
175
176 if (sm->msg_id_base != (u16) ~0)
Dave Barach99617f72017-03-04 08:35:48 -0500177 sample_api_hookup (vam);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
179 vec_free(name);
180
181 return 0;
182}