dpdk: Add support for Mellanox ConnectX-4 devices
[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   u8 which = 0;                 /* ipv4 by default */
136   u32 sw_if_index = ~0;
137   vl_api_flowperpkt_tx_interface_add_del_t *mp;
138
139   /* Parse args required to build the message */
140   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
141     {
142       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
143         ;
144       else if (unformat (i, "sw_if_index %d", &sw_if_index))
145         ;
146       else if (unformat (i, "disable"))
147         enable_disable = 0;
148       else if (unformat (i, "l2"))
149         which = 1;
150       else
151         break;
152     }
153
154   if (sw_if_index == ~0)
155     {
156       errmsg ("missing interface name / explicit sw_if_index number \n");
157       return -99;
158     }
159
160   /* Construct the API message */
161   M (FLOWPERPKT_TX_INTERFACE_ADD_DEL, flowperpkt_tx_interface_add_del);
162   mp->sw_if_index = ntohl (sw_if_index);
163   mp->is_add = enable_disable;
164   mp->which = which;
165
166   /* send it... */
167   S;
168
169   /* Wait for a reply... */
170   W;
171 }
172
173 /*
174  * List of messages that the api test plugin sends,
175  * and that the data plane plugin processes
176  */
177 #define foreach_vpe_api_msg \
178 _(flowperpkt_tx_interface_add_del, "<intfc> [disable]")
179
180 void
181 vat_api_hookup (vat_main_t * vam)
182 {
183   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
184   /* Hook up handlers for replies from the data plane plug-in */
185 #define _(N,n)                                                  \
186     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
187                            #n,                                  \
188                            vl_api_##n##_t_handler,              \
189                            vl_noop_handler,                     \
190                            vl_api_##n##_t_endian,               \
191                            vl_api_##n##_t_print,                \
192                            sizeof(vl_api_##n##_t), 1);
193   foreach_vpe_api_reply_msg;
194 #undef _
195
196   /* API messages we can send */
197 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
198   foreach_vpe_api_msg;
199 #undef _
200
201   /* Help strings */
202 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
203   foreach_vpe_api_msg;
204 #undef _
205 }
206
207 clib_error_t *
208 vat_plugin_register (vat_main_t * vam)
209 {
210   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
211   u8 *name;
212
213   sm->vat_main = vam;
214
215   /* Ask the vpp engine for the first assigned message-id */
216   name = format (0, "flowperpkt_%08x%c", api_version, 0);
217   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
218
219   /* Don't attempt to hook up API messages if the data plane plugin is AWOL */
220   if (sm->msg_id_base != (u16) ~ 0)
221     vat_api_hookup (vam);
222
223   vec_free (name);
224
225   return 0;
226 }
227
228 /*
229  * fd.io coding-style-patch-verification: ON
230  *
231  * Local Variables:
232  * eval: (c-set-style "gnu")
233  * End:
234  */