span: fix wrong next1 feature index in dual loop
[vpp.git] / plugins / flowperpkt-plugin / flowperpkt / flowperpkt_test.c
1 /*
2  * flowperpkt.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vlibsocket/api.h>
21 #include <vppinfra/error.h>
22
23 /**
24  * @file vpp_api_test plugin
25  */
26
27 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
28
29 /* Declare message IDs */
30 #include <flowperpkt/flowperpkt_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <flowperpkt/flowperpkt_all_api_h.h>
35 #undef vl_typedefs
36
37 /* declare message handlers for each api */
38
39 #define vl_endianfun            /* define message structures */
40 #include <flowperpkt/flowperpkt_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...)
45 #define vl_printfun
46 #include <flowperpkt/flowperpkt_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number. */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <flowperpkt/flowperpkt_all_api_h.h>
52 #undef vl_api_version
53
54 typedef struct
55 {
56     /** API message ID base */
57   u16 msg_id_base;
58     /** vat_main_t pointer */
59   vat_main_t *vat_main;
60 } flowperpkt_test_main_t;
61
62 flowperpkt_test_main_t flowperpkt_test_main;
63
64 #define foreach_standard_reply_retval_handler   \
65 _(flowperpkt_tx_interface_add_del_reply)
66
67 #define _(n)                                            \
68     static void vl_api_##n##_t_handler                  \
69     (vl_api_##n##_t * mp)                               \
70     {                                                   \
71         vat_main_t * vam = flowperpkt_test_main.vat_main;   \
72         i32 retval = ntohl(mp->retval);                 \
73         if (vam->async_mode) {                          \
74             vam->async_errors += (retval < 0);          \
75         } else {                                        \
76             vam->retval = retval;                       \
77             vam->result_ready = 1;                      \
78         }                                               \
79     }
80 foreach_standard_reply_retval_handler;
81 #undef _
82
83 /*
84  * Table of message reply handlers, must include boilerplate handlers
85  * we just generated
86  */
87 #define foreach_vpe_api_reply_msg               \
88 _(FLOWPERPKT_TX_INTERFACE_ADD_DEL_REPLY,        \
89   flowperpkt_tx_interface_add_del_reply)
90
91
92 /* M: construct, but don't yet send a message */
93
94 #define M(T,t)                                                  \
95 do {                                                            \
96     vam->result_ready = 0;                                      \
97     mp = vl_msg_api_alloc(sizeof(*mp));                         \
98     memset (mp, 0, sizeof (*mp));                               \
99     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
100     mp->client_index = vam->my_client_index;                    \
101 } while(0);
102
103 #define M2(T,t,n)                                               \
104 do {                                                            \
105     vam->result_ready = 0;                                      \
106     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
107     memset (mp, 0, sizeof (*mp));                               \
108     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
109     mp->client_index = vam->my_client_index;                    \
110 } while(0);
111
112 /* S: send a message */
113 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
114
115 /* W: wait for results, with timeout */
116 #define W                                       \
117 do {                                            \
118     timeout = vat_time_now (vam) + 1.0;         \
119                                                 \
120     while (vat_time_now (vam) < timeout) {      \
121         if (vam->result_ready == 1) {           \
122             return (vam->retval);               \
123         }                                       \
124     }                                           \
125     return -99;                                 \
126 } while(0);
127
128 static int
129 api_flowperpkt_tx_interface_add_del (vat_main_t * vam)
130 {
131   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
132   unformat_input_t *i = vam->input;
133   f64 timeout;
134   int enable_disable = 1;
135   u32 sw_if_index = ~0;
136   vl_api_flowperpkt_tx_interface_add_del_t *mp;
137
138   /* Parse args required to build the message */
139   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
140     {
141       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
142         ;
143       else if (unformat (i, "sw_if_index %d", &sw_if_index))
144         ;
145       else if (unformat (i, "disable"))
146         enable_disable = 0;
147       else
148         break;
149     }
150
151   if (sw_if_index == ~0)
152     {
153       errmsg ("missing interface name / explicit sw_if_index number \n");
154       return -99;
155     }
156
157   /* Construct the API message */
158   M (FLOWPERPKT_TX_INTERFACE_ADD_DEL, flowperpkt_tx_interface_add_del);
159   mp->sw_if_index = ntohl (sw_if_index);
160   mp->is_add = enable_disable;
161   mp->is_ipv6 = 0;              /* $$$$ */
162
163   /* send it... */
164   S;
165
166   /* Wait for a reply... */
167   W;
168 }
169
170 /*
171  * List of messages that the api test plugin sends,
172  * and that the data plane plugin processes
173  */
174 #define foreach_vpe_api_msg \
175 _(flowperpkt_tx_interface_add_del, "<intfc> [disable]")
176
177 void
178 vat_api_hookup (vat_main_t * vam)
179 {
180   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
181   /* Hook up handlers for replies from the data plane plug-in */
182 #define _(N,n)                                                  \
183     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
184                            #n,                                  \
185                            vl_api_##n##_t_handler,              \
186                            vl_noop_handler,                     \
187                            vl_api_##n##_t_endian,               \
188                            vl_api_##n##_t_print,                \
189                            sizeof(vl_api_##n##_t), 1);
190   foreach_vpe_api_reply_msg;
191 #undef _
192
193   /* API messages we can send */
194 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
195   foreach_vpe_api_msg;
196 #undef _
197
198   /* Help strings */
199 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
200   foreach_vpe_api_msg;
201 #undef _
202 }
203
204 clib_error_t *
205 vat_plugin_register (vat_main_t * vam)
206 {
207   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
208   u8 *name;
209
210   sm->vat_main = vam;
211
212   /* Ask the vpp engine for the first assigned message-id */
213   name = format (0, "flowperpkt_%08x%c", api_version, 0);
214   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
215
216   /* Don't attempt to hook up API messages if the data plane plugin is AWOL */
217   if (sm->msg_id_base != (u16) ~ 0)
218     vat_api_hookup (vam);
219
220   vec_free (name);
221
222   return 0;
223 }
224
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */