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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
23 #include <vmxnet3/vmxnet3.h>
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
28 /* define message IDs */
29 #include <vmxnet3/vmxnet3_msg_enum.h>
31 /* define message structures */
33 #include <vmxnet3/vmxnet3_all_api_h.h>
36 /* define generated endian-swappers */
38 #include <vmxnet3/vmxnet3_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
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>
49 /* Macro to finish up custom dump fns */
52 vl_print (handle, (char *)s); \
56 #include <vlibapi/api_helper_macros.h>
58 #define foreach_vmxnet3_plugin_api_msg \
59 _(VMXNET3_CREATE, vmxnet3_create) \
60 _(VMXNET3_DELETE, vmxnet3_delete) \
61 _(VMXNET3_DUMP, vmxnet3_dump)
64 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
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;
72 clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
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);
81 args.enable_gso = mp->enable_gso;
83 vmxnet3_create_if (vm, &args);
87 REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
89 rmp->sw_if_index = ntohl (args.sw_if_index);
95 vl_api_vmxnet3_create_t_print (vl_api_vmxnet3_create_t * mp, void *handle)
98 u32 pci_addr = ntohl (mp->pci_addr);
100 s = format (0, "SCRIPT: vmxnet3_create ");
101 s = format (s, "%U ", format_vlib_pci_addr, &pci_addr);
103 s = format (s, "elog ");
105 s = format (s, "bind ");
107 s = format (s, "gso ");
109 s = format (s, "rx-queue-size %u ", ntohs (mp->rxq_size));
111 s = format (s, "tx-queue-size %u ", ntohs (mp->txq_size));
113 s = format (s, "num-rx-queues %u ", ntohs (mp->rxq_num));
115 s = format (s, "num-tx-queues %u ", ntohs (mp->txq_num));
121 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
123 vlib_main_t *vm = vlib_get_main ();
124 vnet_main_t *vnm = vnet_get_main ();
125 vmxnet3_main_t *vmxm = &vmxnet3_main;
126 vl_api_vmxnet3_delete_reply_t *rmp;
127 vmxnet3_device_t *vd;
128 vnet_hw_interface_t *hw;
132 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
133 htonl (mp->sw_if_index));
134 if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
136 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
140 vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
142 vmxnet3_delete_if (vm, vd);
145 REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
149 vl_api_vmxnet3_delete_t_print (vl_api_vmxnet3_delete_t * mp, void *handle)
153 s = format (0, "SCRIPT: vmxnet3_delete ");
154 s = format (s, "sw_if_index %d ", ntohl (mp->sw_if_index));
160 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
161 vnet_sw_interface_t * swif, u8 * interface_name,
164 vl_api_vmxnet3_details_t *mp;
165 vnet_main_t *vnm = vnet_get_main ();
166 vmxnet3_main_t *vmxm = &vmxnet3_main;
167 vnet_hw_interface_t *hwif;
168 vmxnet3_rx_ring *ring;
171 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
173 mp = vl_msg_api_alloc (sizeof (*mp));
174 clib_memset (mp, 0, sizeof (*mp));
176 mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
177 mp->context = context;
179 mp->sw_if_index = htonl (swif->sw_if_index);
180 strncpy ((char *) mp->if_name,
181 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
183 if (hwif->hw_address)
184 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
186 mp->version = vd->version;
187 mp->pci_addr = ntohl (vd->pci_addr.as_u32);
188 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
190 mp->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX);
191 vec_foreach_index (qid, vd->rxqs)
193 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
194 vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
196 ASSERT (qid < VMXNET3_RXQ_MAX);
197 rx_list->rx_qsize = htons (rxq->size);
198 rx_list->rx_next = htons (rxq->rx_comp_ring.next);
199 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
201 ring = &rxq->rx_ring[rid];
202 rx_list->rx_fill[rid] = htons (ring->fill);
203 rx_list->rx_produce[rid] = htons (ring->produce);
204 rx_list->rx_consume[rid] = htons (ring->consume);
208 mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
209 vec_foreach_index (qid, vd->txqs)
211 vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
212 vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
214 ASSERT (qid < VMXNET3_TXQ_MAX);
215 tx_list->tx_qsize = htons (txq->size);
216 tx_list->tx_next = htons (txq->tx_comp_ring.next);
217 tx_list->tx_produce = htons (txq->tx_ring.produce);
218 tx_list->tx_consume = htons (txq->tx_ring.consume);
221 vl_api_send_msg (reg, (u8 *) mp);
225 * @brief Message handler for vmxnet3_dump API.
226 * @param mp vl_api_vmxnet3_dump_t * mp the api message
229 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
231 vmxnet3_main_t *vmxm = &vmxnet3_main;
232 vnet_main_t *vnm = vnet_get_main ();
233 vnet_sw_interface_t *swif;
234 vmxnet3_device_t *vd;
236 vl_api_registration_t *reg;
238 reg = vl_api_client_index_to_registration (mp->client_index);
243 pool_foreach (vd, vmxm->devices,
245 swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
246 if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
248 send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
249 _vec_len (if_name) = 0;
257 vl_api_vmxnet3_dump_t_print (vl_api_vmxnet3_dump_t * mp, void *handle)
261 s = format (0, "SCRIPT: vmxnet3_dump ");
266 #define vl_msg_name_crc_list
267 #include <vmxnet3/vmxnet3_all_api_h.h>
268 #undef vl_msg_name_crc_list
271 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
273 #define _(id,n,crc) \
274 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
275 foreach_vl_msg_name_crc_vmxnet3;
280 plugin_custom_dump_configure (vmxnet3_main_t * vmxm)
282 #define _(n,f) api_main.msg_print_handlers \
283 [VL_API_##n + vmxm->msg_id_base] \
284 = (void *) vl_api_##f##_t_print;
285 foreach_vmxnet3_plugin_api_msg;
289 /* set tup the API message handling tables */
291 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
293 vmxnet3_main_t *vmxm = &vmxnet3_main;
294 api_main_t *am = &api_main;
297 /* construct the API name */
298 name = format (0, "vmxnet3_%08x%c", api_version, 0);
300 /* ask for a correctly-sized block of API message decode slots */
301 vmxm->msg_id_base = vl_msg_api_get_msg_ids
302 ((char *) name, VL_MSG_FIRST_AVAILABLE);
305 vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base), \
307 vl_api_##n##_t_handler, \
309 vl_api_##n##_t_endian, \
310 vl_api_##n##_t_print, \
311 sizeof(vl_api_##n##_t), 1);
312 foreach_vmxnet3_plugin_api_msg;
315 /* set up the (msg_name, crc, message-id) table */
316 setup_message_id_table (vmxm, am);
318 plugin_custom_dump_configure (vmxm);
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")