8395f158c9a770b5d747e30d75481eb520da98af
[vpp.git] / src / plugins / vmxnet3 / vmxnet3_api.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 <vmxnet3/vmxnet3.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27
28 /* define message IDs */
29 #include <vmxnet3/vmxnet3_msg_enum.h>
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <vmxnet3/vmxnet3_all_api_h.h>
34 #undef vl_typedefs
35
36 /* define generated endian-swappers */
37 #define vl_endianfun
38 #include <vmxnet3/vmxnet3_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43
44 /* get the API version number */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <vmxnet3/vmxnet3_all_api_h.h>
47 #undef vl_api_version
48
49 /* Macro to finish up custom dump fns */
50 #define FINISH                                  \
51     vec_add1 (s, 0);                            \
52     vl_print (handle, (char *)s);               \
53     vec_free (s);                               \
54     return handle;
55
56 #include <vlibapi/api_helper_macros.h>
57
58 #define foreach_vmxnet3_plugin_api_msg  \
59 _(VMXNET3_CREATE, vmxnet3_create)       \
60 _(VMXNET3_DELETE, vmxnet3_delete)       \
61 _(VMXNET3_DUMP, vmxnet3_dump)
62
63 static void
64 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
65 {
66   vlib_main_t *vm = vlib_get_main ();
67   vmxnet3_main_t *vmxm = &vmxnet3_main;
68   vl_api_vmxnet3_create_reply_t *rmp;
69   vmxnet3_create_if_args_t args;
70   int rv;
71
72   clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
73
74   args.enable_elog = ntohl (mp->enable_elog);
75   args.addr.as_u32 = ntohl (mp->pci_addr);
76   args.rxq_size = ntohs (mp->rxq_size);
77   args.txq_size = ntohs (mp->txq_size);
78   args.txq_num = ntohs (mp->txq_num);
79   args.rxq_num = ntohs (mp->rxq_num);
80   args.bind = mp->bind;
81
82   vmxnet3_create_if (vm, &args);
83   rv = args.rv;
84
85   /* *INDENT-OFF* */
86   REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
87     ({
88       rmp->sw_if_index = ntohl (args.sw_if_index);
89     }));
90   /* *INDENT-ON* */
91 }
92
93 static void *
94 vl_api_vmxnet3_create_t_print (vl_api_vmxnet3_create_t * mp, void *handle)
95 {
96   u8 *s;
97   u32 pci_addr = ntohl (mp->pci_addr);
98
99   s = format (0, "SCRIPT: vmxnet3_create ");
100   s = format (s, "%U ", format_vlib_pci_addr, &pci_addr);
101   if (mp->enable_elog)
102     s = format (s, "elog ");
103   if (mp->bind)
104     s = format (s, "bind ");
105   if (mp->rxq_size)
106     s = format (s, "rx-queue-size %u ", ntohs (mp->rxq_size));
107   if (mp->txq_size)
108     s = format (s, "tx-queue-size %u ", ntohs (mp->txq_size));
109   if (mp->rxq_num)
110     s = format (s, "num-rx-queues %u ", ntohs (mp->rxq_num));
111   if (mp->txq_num)
112     s = format (s, "num-tx-queues %u ", ntohs (mp->txq_num));
113
114   FINISH;
115 }
116
117 static void
118 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
119 {
120   vlib_main_t *vm = vlib_get_main ();
121   vnet_main_t *vnm = vnet_get_main ();
122   vmxnet3_main_t *vmxm = &vmxnet3_main;
123   vl_api_vmxnet3_delete_reply_t *rmp;
124   vmxnet3_device_t *vd;
125   vnet_hw_interface_t *hw;
126   int rv = 0;
127
128   hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
129   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
130     {
131       rv = VNET_API_ERROR_INVALID_INTERFACE;
132       goto reply;
133     }
134
135   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
136
137   vmxnet3_delete_if (vm, vd);
138
139 reply:
140   REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
141 }
142
143 static void *
144 vl_api_vmxnet3_delete_t_print (vl_api_vmxnet3_delete_t * mp, void *handle)
145 {
146   u8 *s;
147
148   s = format (0, "SCRIPT: vmxnet3_delete ");
149   s = format (s, "sw_if_index %d ", ntohl (mp->sw_if_index));
150
151   FINISH;
152 }
153
154 static void
155 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
156                       vnet_sw_interface_t * swif, u8 * interface_name,
157                       u32 context)
158 {
159   vl_api_vmxnet3_details_t *mp;
160   vnet_main_t *vnm = vnet_get_main ();
161   vmxnet3_main_t *vmxm = &vmxnet3_main;
162   vnet_hw_interface_t *hwif;
163   vmxnet3_rx_ring *ring;
164   u16 rid, qid;
165
166   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
167
168   mp = vl_msg_api_alloc (sizeof (*mp));
169   clib_memset (mp, 0, sizeof (*mp));
170
171   mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
172   mp->context = context;
173
174   mp->sw_if_index = htonl (swif->sw_if_index);
175   strncpy ((char *) mp->if_name,
176            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
177
178   if (hwif->hw_address)
179     memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
180
181   mp->version = vd->version;
182   mp->pci_addr = ntohl (vd->pci_addr.as_u32);
183   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
184
185   mp->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX);
186   vec_foreach_index (qid, vd->rxqs)
187   {
188     vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
189     vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
190
191     ASSERT (qid < VMXNET3_RXQ_MAX);
192     rx_list->rx_qsize = htons (rxq->size);
193     rx_list->rx_next = htons (rxq->rx_comp_ring.next);
194     for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
195       {
196         ring = &rxq->rx_ring[rid];
197         rx_list->rx_fill[rid] = htons (ring->fill);
198         rx_list->rx_produce[rid] = htons (ring->produce);
199         rx_list->rx_consume[rid] = htons (ring->consume);
200       }
201   }
202
203   mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
204   vec_foreach_index (qid, vd->txqs)
205   {
206     vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
207     vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
208
209     ASSERT (qid < VMXNET3_TXQ_MAX);
210     tx_list->tx_qsize = htons (txq->size);
211     tx_list->tx_next = htons (txq->tx_comp_ring.next);
212     tx_list->tx_produce = htons (txq->tx_ring.produce);
213     tx_list->tx_consume = htons (txq->tx_ring.consume);
214   }
215
216   vl_api_send_msg (reg, (u8 *) mp);
217 }
218
219 /**
220  * @brief Message handler for vmxnet3_dump API.
221  * @param mp vl_api_vmxnet3_dump_t * mp the api message
222  */
223 static void
224 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
225 {
226   vmxnet3_main_t *vmxm = &vmxnet3_main;
227   vnet_main_t *vnm = vnet_get_main ();
228   vnet_sw_interface_t *swif;
229   vmxnet3_device_t *vd;
230   u8 *if_name = 0;
231   vl_api_registration_t *reg;
232
233   reg = vl_api_client_index_to_registration (mp->client_index);
234   if (!reg)
235     return;
236
237   /* *INDENT-OFF* */
238   pool_foreach (vd, vmxm->devices,
239     ({
240       swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
241       if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
242                         swif, 0);
243       send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
244       _vec_len (if_name) = 0;
245     }));
246   /* *INDENT-ON* */
247
248   vec_free (if_name);
249 }
250
251 static void *
252 vl_api_vmxnet3_dump_t_print (vl_api_vmxnet3_create_t * mp, void *handle)
253 {
254   u8 *s;
255
256   s = format (0, "SCRIPT: vmxnet3_dump ");
257
258   FINISH;
259 }
260
261 #define vl_msg_name_crc_list
262 #include <vmxnet3/vmxnet3_all_api_h.h>
263 #undef vl_msg_name_crc_list
264
265 static void
266 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
267 {
268 #define _(id,n,crc) \
269   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
270   foreach_vl_msg_name_crc_vmxnet3;
271 #undef _
272 }
273
274 static void
275 plugin_custom_dump_configure (vmxnet3_main_t * vmxm)
276 {
277 #define _(n,f) api_main.msg_print_handlers \
278   [VL_API_##n + vmxm->msg_id_base]                \
279     = (void *) vl_api_##f##_t_print;
280   foreach_vmxnet3_plugin_api_msg;
281 #undef _
282 }
283
284 /* set tup the API message handling tables */
285 clib_error_t *
286 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
287 {
288   vmxnet3_main_t *vmxm = &vmxnet3_main;
289   api_main_t *am = &api_main;
290   u8 *name;
291
292   /* construct the API name */
293   name = format (0, "vmxnet3_%08x%c", api_version, 0);
294
295   /* ask for a correctly-sized block of API message decode slots */
296   vmxm->msg_id_base = vl_msg_api_get_msg_ids
297     ((char *) name, VL_MSG_FIRST_AVAILABLE);
298
299 #define _(N,n)                                                  \
300     vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base),   \
301                            #n,                                  \
302                            vl_api_##n##_t_handler,              \
303                            vl_noop_handler,                     \
304                            vl_api_##n##_t_endian,               \
305                            vl_api_##n##_t_print,                \
306                            sizeof(vl_api_##n##_t), 1);
307   foreach_vmxnet3_plugin_api_msg;
308 #undef _
309
310   /* set up the (msg_name, crc, message-id) table */
311   setup_message_id_table (vmxm, am);
312
313   plugin_custom_dump_configure (vmxm);
314
315   vec_free (name);
316   return 0;
317 }
318
319 /*
320  * fd.io coding-style-patch-verification: ON
321  *
322  * Local Variables:
323  * eval: (c-set-style "gnu")
324  * End:
325  */