vmxnet3: multiple TX queues support
[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 #define vl_printfun
44 #include <vmxnet3/vmxnet3_all_api_h.h>
45 #undef vl_printfun
46
47 /* get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <vmxnet3/vmxnet3_all_api_h.h>
50 #undef vl_api_version
51
52 #include <vlibapi/api_helper_macros.h>
53
54 #define foreach_vmxnet3_plugin_api_msg  \
55 _(VMXNET3_CREATE, vmxnet3_create)       \
56 _(VMXNET3_DELETE, vmxnet3_delete)       \
57 _(VMXNET3_DUMP, vmxnet3_dump)
58
59 static void
60 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
61 {
62   vlib_main_t *vm = vlib_get_main ();
63   vmxnet3_main_t *vmxm = &vmxnet3_main;
64   vl_api_vmxnet3_create_reply_t *rmp;
65   vmxnet3_create_if_args_t args;
66   int rv;
67
68   clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
69
70   args.enable_elog = ntohl (mp->enable_elog);
71   args.addr.as_u32 = ntohl (mp->pci_addr);
72   args.rxq_size = ntohs (mp->rxq_size);
73   args.txq_size = ntohs (mp->txq_size);
74   args.txq_num = ntohs (mp->txq_num);
75
76   vmxnet3_create_if (vm, &args);
77   rv = args.rv;
78
79   /* *INDENT-OFF* */
80   REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
81     ({
82       rmp->sw_if_index = ntohl (args.sw_if_index);
83     }));
84   /* *INDENT-ON* */
85 }
86
87 static void
88 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
89 {
90   vlib_main_t *vm = vlib_get_main ();
91   vnet_main_t *vnm = vnet_get_main ();
92   vmxnet3_main_t *vmxm = &vmxnet3_main;
93   vl_api_vmxnet3_delete_reply_t *rmp;
94   vmxnet3_device_t *vd;
95   vnet_hw_interface_t *hw;
96   int rv = 0;
97
98   hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
99   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
100     {
101       rv = VNET_API_ERROR_INVALID_INTERFACE;
102       goto reply;
103     }
104
105   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
106
107   vmxnet3_delete_if (vm, vd);
108
109 reply:
110   REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
111 }
112
113 static void
114 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
115                       vmxnet3_rxq_t * rxq, vnet_sw_interface_t * swif,
116                       u8 * interface_name, u32 context)
117 {
118   vl_api_vmxnet3_details_t *mp;
119   vnet_main_t *vnm = vnet_get_main ();
120   vmxnet3_main_t *vmxm = &vmxnet3_main;
121   vnet_hw_interface_t *hwif;
122   vmxnet3_rx_ring *ring;
123   u16 rid, qid;
124
125   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
126
127   mp = vl_msg_api_alloc (sizeof (*mp));
128   clib_memset (mp, 0, sizeof (*mp));
129
130   mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
131   mp->context = context;
132
133   mp->sw_if_index = htonl (swif->sw_if_index);
134   strncpy ((char *) mp->if_name,
135            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
136
137   if (hwif->hw_address)
138     memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
139
140   mp->version = vd->version;
141   mp->pci_addr = ntohl (vd->pci_addr.as_u32);
142   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
143
144   mp->rx_qsize = htons (rxq->size);
145   mp->rx_next = htons (rxq->rx_comp_ring.next);
146   for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
147     {
148       ring = &rxq->rx_ring[rid];
149       mp->rx_fill[rid] = htons (ring->fill);
150       mp->rx_produce[rid] = htons (ring->produce);
151       mp->rx_consume[rid] = htons (ring->consume);
152     }
153
154   mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
155   vec_foreach_index (qid, vd->txqs)
156   {
157     vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
158     vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
159
160     ASSERT (qid < VMXNET3_TXQ_MAX);
161     tx_list->tx_qsize = htons (txq->size);
162     tx_list->tx_next = htons (txq->tx_comp_ring.next);
163     tx_list->tx_produce = htons (txq->tx_ring.produce);
164     tx_list->tx_consume = htons (txq->tx_ring.consume);
165   }
166
167   vl_api_send_msg (reg, (u8 *) mp);
168 }
169
170 /**
171  * @brief Message handler for vmxnet3_dump API.
172  * @param mp vl_api_vmxnet3_dump_t * mp the api message
173  */
174 static void
175 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
176 {
177   vmxnet3_main_t *vmxm = &vmxnet3_main;
178   vnet_main_t *vnm = vnet_get_main ();
179   vnet_sw_interface_t *swif;
180   vmxnet3_device_t *vd;
181   u8 *if_name = 0;
182   vl_api_registration_t *reg;
183   vmxnet3_rxq_t *rxq;
184   u16 qid = 0;
185
186   reg = vl_api_client_index_to_registration (mp->client_index);
187   if (!reg)
188     return;
189
190   /* *INDENT-OFF* */
191   pool_foreach (vd, vmxm->devices,
192     ({
193       swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
194       if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
195                         swif, 0);
196       rxq = vec_elt_at_index (vd->rxqs, qid);
197       send_vmxnet3_details (reg, vd, rxq, swif, if_name, mp->context);
198       _vec_len (if_name) = 0;
199     }));
200   /* *INDENT-ON* */
201
202   vec_free (if_name);
203 }
204
205 #define vl_msg_name_crc_list
206 #include <vmxnet3/vmxnet3_all_api_h.h>
207 #undef vl_msg_name_crc_list
208
209 static void
210 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
211 {
212 #define _(id,n,crc) \
213   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
214   foreach_vl_msg_name_crc_vmxnet3;
215 #undef _
216 }
217
218 /* set tup the API message handling tables */
219 clib_error_t *
220 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
221 {
222   vmxnet3_main_t *vmxm = &vmxnet3_main;
223   api_main_t *am = &api_main;
224   u8 *name;
225
226   /* construct the API name */
227   name = format (0, "vmxnet3_%08x%c", api_version, 0);
228
229   /* ask for a correctly-sized block of API message decode slots */
230   vmxm->msg_id_base = vl_msg_api_get_msg_ids
231     ((char *) name, VL_MSG_FIRST_AVAILABLE);
232
233 #define _(N,n)                                                  \
234     vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base),   \
235                            #n,                                  \
236                            vl_api_##n##_t_handler,              \
237                            vl_noop_handler,                     \
238                            vl_api_##n##_t_endian,               \
239                            vl_api_##n##_t_print,                \
240                            sizeof(vl_api_##n##_t), 1);
241   foreach_vmxnet3_plugin_api_msg;
242 #undef _
243
244   /* set up the (msg_name, crc, message-id) table */
245   setup_message_id_table (vmxm, am);
246
247   vec_free (name);
248   return 0;
249 }
250
251 /*
252  * fd.io coding-style-patch-verification: ON
253  *
254  * Local Variables:
255  * eval: (c-set-style "gnu")
256  * End:
257  */