vmxnet3: custom dump and debug cli fix
[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 size;
106
107   clib_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, "%U", unformat_vlib_pci_addr, &args.addr))
112         ;
113       else if (unformat (i, "elog"))
114         args.enable_elog = 1;
115       else if (unformat (i, "bind"))
116         args.bind = 1;
117       else if (unformat (i, "rx-queue-size %u", &size))
118         args.rxq_size = size;
119       else if (unformat (i, "tx-queue-size %u", &size))
120         args.txq_size = size;
121       else if (unformat (i, "num-tx-queues %u", &size))
122         args.txq_num = size;
123       else if (unformat (i, "num-rx-queues %u", &size))
124         args.rxq_num = size;
125       else
126         {
127           clib_warning ("unknown input '%U'", format_unformat_error, i);
128           return -99;
129         }
130     }
131
132   M (VMXNET3_CREATE, mp);
133
134   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
135   mp->enable_elog = clib_host_to_net_u16 (args.enable_elog);
136   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
137   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
138   mp->txq_num = clib_host_to_net_u16 (args.txq_num);
139   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
140   mp->bind = args.bind;
141
142   S (mp);
143   W (ret);
144
145   return ret;
146 }
147
148 /* vmxnet3-create reply handler */
149 static void
150 vl_api_vmxnet3_create_reply_t_handler (vl_api_vmxnet3_create_reply_t * mp)
151 {
152   vat_main_t *vam = vmxnet3_test_main.vat_main;
153   i32 retval = ntohl (mp->retval);
154
155   if (retval == 0)
156     {
157       fformat (vam->ofp, "created vmxnet3 with sw_if_index %d\n",
158                ntohl (mp->sw_if_index));
159     }
160
161   vam->retval = retval;
162   vam->result_ready = 1;
163   vam->regenerate_interface_table = 1;
164 }
165
166 /* vmxnet3 delete API */
167 static int
168 api_vmxnet3_delete (vat_main_t * vam)
169 {
170   unformat_input_t *i = vam->input;
171   vl_api_vmxnet3_delete_t *mp;
172   u32 sw_if_index = 0;
173   u8 index_defined = 0;
174   int ret;
175
176   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
177     {
178       if (unformat (i, "sw_if_index %u", &sw_if_index))
179         index_defined = 1;
180       else
181         {
182           clib_warning ("unknown input '%U'", format_unformat_error, i);
183           return -99;
184         }
185     }
186
187   if (!index_defined)
188     {
189       errmsg ("missing sw_if_index\n");
190       return -99;
191     }
192
193   M (VMXNET3_DELETE, mp);
194
195   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
196
197   S (mp);
198   W (ret);
199
200   return ret;
201 }
202
203 static int
204 api_vmxnet3_dump (vat_main_t * vam)
205 {
206   vmxnet3_test_main_t *vxm = &vmxnet3_test_main;
207   vl_api_vmxnet3_dump_t *mp;
208   vl_api_control_ping_t *mp_ping;
209   int ret;
210
211   if (vam->json_output)
212     {
213       clib_warning ("JSON output not supported for vmxnet3_dump");
214       return -99;
215     }
216
217   M (VMXNET3_DUMP, mp);
218   S (mp);
219
220   /* Use a control ping for synchronization */
221   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
222   mp_ping->_vl_msg_id = htons (vxm->ping_id);
223   mp_ping->client_index = vam->my_client_index;
224
225   fformat (vam->ofp, "Sending ping id=%d\n", vxm->ping_id);
226
227   vam->result_ready = 0;
228   S (mp_ping);
229
230   W (ret);
231   return ret;
232 }
233
234 static u8 *
235 format_pci_addr (u8 * s, va_list * va)
236 {
237   vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
238   return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
239                  addr->slot, addr->function);
240 }
241
242 static void
243 vl_api_vmxnet3_details_t_handler (vl_api_vmxnet3_details_t * mp)
244 {
245   vat_main_t *vam = vmxnet3_test_main.vat_main;
246   u32 pci_addr = ntohl (mp->pci_addr);
247   u16 qid;
248
249   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
250            "   version: %u\n"
251            "   PCI Address: %U\n"
252            "   state %s\n",
253            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
254            mp->hw_addr, mp->version,
255            format_pci_addr, &pci_addr, mp->admin_up_down ? "up" : "down");
256   for (qid = 0; qid < mp->rx_count; qid++)
257     {
258       vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
259       fformat (vam->ofp,
260                "   RX Queue %u\n"
261                "     RX completion next index %u\n"
262                "     ring 0 size %u fill %u consume %u produce %u\n"
263                "     ring 1 size %u fill %u consume %u produce %u\n",
264                qid,
265                ntohs (rx_list->rx_next),
266                ntohs (rx_list->rx_qsize), ntohs (rx_list->rx_fill[0]),
267                ntohs (rx_list->rx_consume[0]),
268                ntohs (rx_list->rx_produce[0]),
269                ntohs (rx_list->rx_qsize), ntohs (rx_list->rx_fill[1]),
270                ntohs (rx_list->rx_consume[1]),
271                ntohs (rx_list->rx_produce[1]));
272     }
273   for (qid = 0; qid < mp->tx_count; qid++)
274     {
275       vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
276       fformat (vam->ofp,
277                "   TX Queue %u\n"
278                "     TX completion next index %u\n"
279                "     size %u consume %u produce %u\n",
280                qid,
281                ntohs (tx_list->tx_next),
282                ntohs (tx_list->tx_qsize), ntohs (tx_list->tx_consume),
283                ntohs (tx_list->tx_produce));
284     }
285 }
286
287 /*
288  * List of messages that the api test plugin sends,
289  * and that the data plane plugin processes
290  */
291 #define foreach_vpe_api_msg                                     \
292 _(vmxnet3_create, "<pci-address> [rx-queue-size <size>] "       \
293   "[tx-queue-size <size>] [num-tx-queues <num>]"                \
294   "[num-rx-queues <num>] [bind]")                               \
295 _(vmxnet3_delete, "sw_if_index <sw_if_index>")                  \
296 _(vmxnet3_dump, "")
297
298 static void
299 vmxnet3_vat_api_hookup (vat_main_t * vam)
300 {
301   vmxnet3_test_main_t *vxm __attribute__ ((unused)) = &vmxnet3_test_main;
302 #define _(N,n)                                                  \
303   vl_msg_api_set_handlers((VL_API_##N + vxm->msg_id_base),      \
304                           #n,                                   \
305                           vl_api_##n##_t_handler,               \
306                           vl_noop_handler,                      \
307                           vl_api_##n##_t_endian,                \
308                           vl_api_##n##_t_print,                 \
309                           sizeof(vl_api_##n##_t), 1);
310   foreach_vpe_api_reply_msg;
311 #undef _
312
313 #define _(n,h)                                                  \
314   hash_set_mem (vam->function_by_name, #n, api_##n);
315   foreach_vpe_api_msg;
316 #undef _
317
318 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
319   foreach_vpe_api_msg;
320 #undef _
321 }
322
323 clib_error_t *
324 vat_plugin_register (vat_main_t * vam)
325 {
326   vmxnet3_test_main_t *vxm = &vmxnet3_test_main;
327   u8 *name;
328
329   vxm->vat_main = vam;
330
331   name = format (0, "vmxnet3_%08x%c", api_version, 0);
332   vxm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
333
334   /* Get the control ping ID */
335 #define _(id,n,crc) \
336   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
337   foreach_vl_msg_name_crc_vpe;
338 #undef _
339   vxm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
340
341   if (vxm->msg_id_base != (u16) ~ 0)
342     vmxnet3_vat_api_hookup (vam);
343
344   vec_free (name);
345
346   return 0;
347 }
348
349 /*
350  * fd.io coding-style-patch-verification: ON
351  *
352  * Local Variables:
353  * eval: (c-set-style "gnu")
354  * End:
355  */