vmxnet3: remove api boilerplate
[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.api_enum.h>
35 #include <vmxnet3/vmxnet3.api_types.h>
36 #include <vpp/api/vpe.api_types.h>
37
38 typedef struct
39 {
40   /* API message ID base */
41   u16 msg_id_base;
42   u32 ping_id;
43   vat_main_t *vat_main;
44 } vmxnet3_test_main_t;
45
46 vmxnet3_test_main_t vmxnet3_test_main;
47
48 /* vmxnet3 create API */
49 static int
50 api_vmxnet3_create (vat_main_t * vam)
51 {
52   unformat_input_t *i = vam->input;
53   vl_api_vmxnet3_create_t *mp;
54   vmxnet3_create_if_args_t args;
55   int ret;
56   u32 size;
57
58   clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
59
60   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
61     {
62       if (unformat (i, "%U", unformat_vlib_pci_addr, &args.addr))
63         ;
64       else if (unformat (i, "elog"))
65         args.enable_elog = 1;
66       else if (unformat (i, "bind"))
67         args.bind = 1;
68       else if (unformat (i, "gso"))
69         args.enable_gso = 1;
70       else if (unformat (i, "rx-queue-size %u", &size))
71         args.rxq_size = size;
72       else if (unformat (i, "tx-queue-size %u", &size))
73         args.txq_size = size;
74       else if (unformat (i, "num-tx-queues %u", &size))
75         args.txq_num = size;
76       else if (unformat (i, "num-rx-queues %u", &size))
77         args.rxq_num = size;
78       else
79         {
80           clib_warning ("unknown input '%U'", format_unformat_error, i);
81           return -99;
82         }
83     }
84
85   M (VMXNET3_CREATE, mp);
86
87   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
88   mp->enable_elog = clib_host_to_net_u16 (args.enable_elog);
89   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
90   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
91   mp->txq_num = clib_host_to_net_u16 (args.txq_num);
92   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
93   mp->bind = args.bind;
94   mp->enable_gso = args.enable_gso;
95
96   S (mp);
97   W (ret);
98
99   return ret;
100 }
101
102 /* vmxnet3-create reply handler */
103 static void
104 vl_api_vmxnet3_create_reply_t_handler (vl_api_vmxnet3_create_reply_t * mp)
105 {
106   vat_main_t *vam = vmxnet3_test_main.vat_main;
107   i32 retval = ntohl (mp->retval);
108
109   if (retval == 0)
110     {
111       fformat (vam->ofp, "created vmxnet3 with sw_if_index %d\n",
112                ntohl (mp->sw_if_index));
113     }
114
115   vam->retval = retval;
116   vam->result_ready = 1;
117   vam->regenerate_interface_table = 1;
118 }
119
120 /* vmxnet3 delete API */
121 static int
122 api_vmxnet3_delete (vat_main_t * vam)
123 {
124   unformat_input_t *i = vam->input;
125   vl_api_vmxnet3_delete_t *mp;
126   u32 sw_if_index = 0;
127   u8 index_defined = 0;
128   int ret;
129
130   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
131     {
132       if (unformat (i, "sw_if_index %u", &sw_if_index))
133         index_defined = 1;
134       else
135         {
136           clib_warning ("unknown input '%U'", format_unformat_error, i);
137           return -99;
138         }
139     }
140
141   if (!index_defined)
142     {
143       errmsg ("missing sw_if_index\n");
144       return -99;
145     }
146
147   M (VMXNET3_DELETE, mp);
148
149   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
150
151   S (mp);
152   W (ret);
153
154   return ret;
155 }
156
157 static int
158 api_vmxnet3_dump (vat_main_t * vam)
159 {
160   vmxnet3_test_main_t *vxm = &vmxnet3_test_main;
161   vl_api_vmxnet3_dump_t *mp;
162   vl_api_control_ping_t *mp_ping;
163   int ret;
164
165   if (vam->json_output)
166     {
167       clib_warning ("JSON output not supported for vmxnet3_dump");
168       return -99;
169     }
170
171   M (VMXNET3_DUMP, mp);
172   S (mp);
173
174   /* Use a control ping for synchronization */
175   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
176   mp_ping->_vl_msg_id = htons (vxm->ping_id);
177   mp_ping->client_index = vam->my_client_index;
178
179   fformat (vam->ofp, "Sending ping id=%d\n", vxm->ping_id);
180
181   vam->result_ready = 0;
182   S (mp_ping);
183
184   W (ret);
185   return ret;
186 }
187
188 static u8 *
189 format_pci_addr (u8 * s, va_list * va)
190 {
191   vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
192   return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
193                  addr->slot, addr->function);
194 }
195
196 static void
197 vl_api_vmxnet3_details_t_handler (vl_api_vmxnet3_details_t * mp)
198 {
199   vat_main_t *vam = vmxnet3_test_main.vat_main;
200   u32 pci_addr = ntohl (mp->pci_addr);
201   u16 qid;
202
203   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
204            "   version: %u\n"
205            "   PCI Address: %U\n"
206            "   state %s\n",
207            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
208            mp->hw_addr, mp->version,
209            format_pci_addr, &pci_addr, mp->admin_up_down ? "up" : "down");
210   for (qid = 0; qid < mp->rx_count; qid++)
211     {
212       vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
213       fformat (vam->ofp,
214                "   RX Queue %u\n"
215                "     RX completion next index %u\n"
216                "     ring 0 size %u fill %u consume %u produce %u\n"
217                "     ring 1 size %u fill %u consume %u produce %u\n",
218                qid,
219                ntohs (rx_list->rx_next),
220                ntohs (rx_list->rx_qsize), ntohs (rx_list->rx_fill[0]),
221                ntohs (rx_list->rx_consume[0]),
222                ntohs (rx_list->rx_produce[0]),
223                ntohs (rx_list->rx_qsize), ntohs (rx_list->rx_fill[1]),
224                ntohs (rx_list->rx_consume[1]),
225                ntohs (rx_list->rx_produce[1]));
226     }
227   for (qid = 0; qid < mp->tx_count; qid++)
228     {
229       vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
230       fformat (vam->ofp,
231                "   TX Queue %u\n"
232                "     TX completion next index %u\n"
233                "     size %u consume %u produce %u\n",
234                qid,
235                ntohs (tx_list->tx_next),
236                ntohs (tx_list->tx_qsize), ntohs (tx_list->tx_consume),
237                ntohs (tx_list->tx_produce));
238     }
239 }
240
241 #include <vmxnet3/vmxnet3.api_test.c>
242
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */