vmxnet3 device driver
[vpp.git] / src / plugins / vmxnet3 / vmxnet3_test.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22
23 #include <vat/vat.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26
27 #include <vppinfra/error.h>
28 #include <vmxnet3/vmxnet3.h>
29
30 #define __plugin_msg_base vmxnet3_test_main.msg_id_base
31 #include <vlibapi/vat_helper_macros.h>
32
33 /* declare message IDs */
34 #include <vmxnet3/vmxnet3_msg_enum.h>
35
36 /* Get CRC codes of the messages defined outside of this plugin */
37 #define vl_msg_name_crc_list
38 #include <vpp/api/vpe_all_api_h.h>
39 #undef vl_msg_name_crc_list
40
41 /* define message structures */
42 #define vl_typedefs
43 #include <vpp/api/vpe_all_api_h.h>
44 #include <vmxnet3/vmxnet3_all_api_h.h>
45 #undef vl_typedefs
46
47 /* declare message handlers for each api */
48 #define vl_endianfun
49 #include <vmxnet3/vmxnet3_all_api_h.h>
50 #undef vl_endianfun
51
52 /* instantiate all the print functions we know about */
53 #define vl_print(handle, ...)
54 #define vl_printfun
55 #include <vmxnet3/vmxnet3_all_api_h.h>
56 #undef vl_printfun
57
58 /* get API version number */
59 #define vl_api_version(n,v) static u32 api_version=(v);
60 #include <vmxnet3/vmxnet3_all_api_h.h>
61 #undef vp_api_version
62
63 typedef struct
64 {
65   /* API message ID base */
66   u16 msg_id_base;
67   u32 ping_id;
68   vat_main_t *vat_main;
69 } vmxnet3_test_main_t;
70
71 vmxnet3_test_main_t vmxnet3_test_main;
72
73 #define foreach_standard_reply_retval_handler           \
74 _(vmxnet3_delete_reply)
75
76 #define _(n)                                            \
77     static void vl_api_##n##_t_handler                  \
78     (vl_api_##n##_t * mp)                               \
79     {                                                   \
80         vat_main_t * vam = vmxnet3_test_main.vat_main;  \
81         i32 retval = ntohl(mp->retval);                 \
82         if (vam->async_mode) {                          \
83             vam->async_errors += (retval < 0);          \
84         } else {                                        \
85             vam->retval = retval;                       \
86             vam->result_ready = 1;                      \
87         }                                               \
88     }
89 foreach_standard_reply_retval_handler;
90 #undef _
91
92 #define foreach_vpe_api_reply_msg                       \
93 _(VMXNET3_CREATE_REPLY, vmxnet3_create_reply)           \
94 _(VMXNET3_DELETE_REPLY, vmxnet3_delete_reply)           \
95 _(VMXNET3_DETAILS, vmxnet3_details)
96
97 /* vmxnet3 create API */
98 static int
99 api_vmxnet3_create (vat_main_t * vam)
100 {
101   unformat_input_t *i = vam->input;
102   vl_api_vmxnet3_create_t *mp;
103   vmxnet3_create_if_args_t args;
104   int ret;
105   u32 x[4];
106
107   memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
108
109   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
110     {
111       if (unformat (i, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
112         {
113           args.addr.domain = x[0];
114           args.addr.bus = x[1];
115           args.addr.slot = x[2];
116           args.addr.function = x[3];
117         }
118       else if (unformat (i, "elog"))
119         args.enable_elog = 1;
120       else if (unformat (i, "rx-queue-size %u", &args.rxq_size))
121         ;
122       else if (unformat (i, "tx-queue-size %u", &args.txq_size))
123         ;
124       else
125         {
126           clib_warning ("unknown input '%U'", format_unformat_error, i);
127           return -99;
128         }
129     }
130
131   M (VMXNET3_CREATE, mp);
132
133   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
134   mp->enable_elog = clib_host_to_net_u16 (args.enable_elog);
135   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
136   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
137
138   S (mp);
139   W (ret);
140
141   return ret;
142 }
143
144 /* vmxnet3-create reply handler */
145 static void
146 vl_api_vmxnet3_create_reply_t_handler (vl_api_vmxnet3_create_reply_t * mp)
147 {
148   vat_main_t *vam = vmxnet3_test_main.vat_main;
149   i32 retval = ntohl (mp->retval);
150
151   if (retval == 0)
152     {
153       fformat (vam->ofp, "created vmxnet3 with sw_if_index %d\n",
154                ntohl (mp->sw_if_index));
155     }
156
157   vam->retval = retval;
158   vam->result_ready = 1;
159   vam->regenerate_interface_table = 1;
160 }
161
162 /* vmxnet3 delete API */
163 static int
164 api_vmxnet3_delete (vat_main_t * vam)
165 {
166   unformat_input_t *i = vam->input;
167   vl_api_vmxnet3_delete_t *mp;
168   u32 sw_if_index = 0;
169   u8 index_defined = 0;
170   int ret;
171
172   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
173     {
174       if (unformat (i, "sw_if_index %u", &sw_if_index))
175         index_defined = 1;
176       else
177         {
178           clib_warning ("unknown input '%U'", format_unformat_error, i);
179           return -99;
180         }
181     }
182
183   if (!index_defined)
184     {
185       errmsg ("missing sw_if_index\n");
186       return -99;
187     }
188
189   M (VMXNET3_DELETE, mp);
190
191   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
192
193   S (mp);
194   W (ret);
195
196   return ret;
197 }
198
199 static int
200 api_vmxnet3_dump (vat_main_t * vam)
201 {
202   vmxnet3_test_main_t *vxm = &vmxnet3_test_main;
203   vl_api_vmxnet3_dump_t *mp;
204   vl_api_control_ping_t *mp_ping;
205   int ret;
206
207   if (vam->json_output)
208     {
209       clib_warning ("JSON output not supported for vmxnet3_dump");
210       return -99;
211     }
212
213   M (VMXNET3_DUMP, mp);
214   S (mp);
215
216   /* Use a control ping for synchronization */
217   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
218   mp_ping->_vl_msg_id = htons (vxm->ping_id);
219   mp_ping->client_index = vam->my_client_index;
220
221   fformat (vam->ofp, "Sending ping id=%d\n", vxm->ping_id);
222
223   vam->result_ready = 0;
224   S (mp_ping);
225
226   W (ret);
227   return ret;
228 }
229
230 static void
231 vl_api_vmxnet3_details_t_handler (vl_api_vmxnet3_details_t * mp)
232 {
233   vat_main_t *vam = vmxnet3_test_main.vat_main;
234   u32 pci_addr = ntohl (mp->pci_addr);
235
236   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
237            "   version: %u\n"
238            "   PCI Address: %U\n"
239            "   RX completion next index %u"
240            "   RX Queue %u\n"
241            "    ring 0 size %u fill %u consume %u produce %u\n"
242            "    ring 1 size %u fill %u consume %u produce %u\n"
243            "   TX completion next index %u"
244            "   TX Queue %u\n"
245            "    size %u consume %u produce %u\n"
246            "   state %s\n",
247            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
248            mp->hw_addr, mp->version,
249            format_vlib_pci_addr, &pci_addr,
250            ntohs (mp->rx_next),
251            ntohs (mp->rx_qid),
252            ntohs (mp->rx_qsize), ntohs (mp->rx_fill[0]),
253            ntohs (mp->rx_consume[0]),
254            ntohs (mp->rx_produce[0]),
255            ntohs (mp->rx_qsize), ntohs (mp->rx_fill[1]),
256            ntohs (mp->rx_consume[1]),
257            ntohs (mp->rx_produce[1]),
258            ntohs (mp->tx_next),
259            ntohs (mp->tx_qid),
260            ntohs (mp->tx_qsize), ntohs (mp->tx_consume),
261            ntohs (mp->tx_produce), mp->admin_up_down ? "up" : "down");
262 }
263
264 /*
265  * List of messages that the api test plugin sends,
266  * and that the data plane plugin processes
267  */
268 #define foreach_vpe_api_msg                                     \
269 _(vmxnet3_create, "<pci-address> [rx-queue-size <size>] "       \
270               "[tx-queue-size <size>]")                         \
271 _(vmxnet3_delete, "<sw_if_index>")                              \
272 _(vmxnet3_dump, "")
273
274 static void
275 vmxnet3_vat_api_hookup (vat_main_t * vam)
276 {
277   vmxnet3_test_main_t *vxm __attribute__ ((unused)) = &vmxnet3_test_main;
278 #define _(N,n)                                                  \
279   vl_msg_api_set_handlers((VL_API_##N + vxm->msg_id_base),      \
280                           #n,                                   \
281                           vl_api_##n##_t_handler,               \
282                           vl_noop_handler,                      \
283                           vl_api_##n##_t_endian,                \
284                           vl_api_##n##_t_print,                 \
285                           sizeof(vl_api_##n##_t), 1);
286   foreach_vpe_api_reply_msg;
287 #undef _
288
289 #define _(n,h)                                                  \
290   hash_set_mem (vam->function_by_name, #n, api_##n);
291   foreach_vpe_api_msg;
292 #undef _
293
294 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
295   foreach_vpe_api_msg;
296 #undef _
297 }
298
299 clib_error_t *
300 vat_plugin_register (vat_main_t * vam)
301 {
302   vmxnet3_test_main_t *vxm = &vmxnet3_test_main;
303   u8 *name;
304
305   vxm->vat_main = vam;
306
307   name = format (0, "vmxnet3_%08x%c", api_version, 0);
308   vxm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
309
310   /* Get the control ping ID */
311 #define _(id,n,crc) \
312   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
313   foreach_vl_msg_name_crc_vpe;
314 #undef _
315   vxm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
316
317   if (vxm->msg_id_base != (u16) ~ 0)
318     vmxnet3_vat_api_hookup (vam);
319
320   vec_free (name);
321
322   return 0;
323 }
324
325 /*
326  * fd.io coding-style-patch-verification: ON
327  *
328  * Local Variables:
329  * eval: (c-set-style "gnu")
330  * End:
331  */