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 #include <vmxnet3/vmxnet3_all_api_h.h>
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>
52 #include <vlibapi/api_helper_macros.h>
54 #define foreach_vmxnet3_plugin_api_msg \
55 _(VMXNET3_CREATE, vmxnet3_create) \
56 _(VMXNET3_DELETE, vmxnet3_delete) \
57 _(VMXNET3_DUMP, vmxnet3_dump)
60 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
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;
68 memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
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);
75 vmxnet3_create_if (vm, &args);
79 REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
81 rmp->sw_if_index = ntohl (args.sw_if_index);
87 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
89 vlib_main_t *vm = vlib_get_main ();
90 vnet_main_t *vnm = vnet_get_main ();
91 vmxnet3_main_t *vmxm = &vmxnet3_main;
92 vl_api_vmxnet3_delete_reply_t *rmp;
94 vnet_hw_interface_t *hw;
97 hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
98 if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
100 rv = VNET_API_ERROR_INVALID_INTERFACE;
104 vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
106 vmxnet3_delete_if (vm, vd);
109 REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
113 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
114 u16 rx_qid, vmxnet3_rxq_t * rxq, u16 tx_qid,
115 vmxnet3_txq_t * txq, vnet_sw_interface_t * swif,
116 u8 * interface_name, u32 context)
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;
125 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
127 mp = vl_msg_api_alloc (sizeof (*mp));
128 memset (mp, 0, sizeof (*mp));
130 mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
131 mp->context = context;
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);
137 if (hwif->hw_address)
138 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
140 mp->version = vd->version;
141 mp->pci_addr = ntohl (vd->pci_addr.as_u32);
143 mp->rx_qsize = htons (rxq->size);
144 mp->rx_next = htons (rxq->rx_comp_ring.next);
145 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
147 ring = &rxq->rx_ring[rid];
148 mp->rx_fill[rid] = htons (ring->fill);
149 mp->rx_produce[rid] = htons (ring->produce);
150 mp->rx_consume[rid] = htons (ring->consume);
152 mp->tx_qsize = htons (txq->size);
153 mp->tx_next = htons (txq->tx_comp_ring.next);
154 mp->tx_produce = htons (txq->tx_ring.produce);
155 mp->tx_consume = htons (txq->tx_ring.consume);
157 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
159 vl_api_send_msg (reg, (u8 *) mp);
163 * @brief Message handler for vmxnet3_dump API.
164 * @param mp vl_api_vmxnet3_dump_t * mp the api message
167 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
169 vmxnet3_main_t *vmxm = &vmxnet3_main;
170 vnet_main_t *vnm = vnet_get_main ();
171 vnet_sw_interface_t *swif;
172 vmxnet3_device_t *vd;
174 vl_api_registration_t *reg;
179 reg = vl_api_client_index_to_registration (mp->client_index);
184 pool_foreach (vd, vmxm->devices,
186 swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
187 if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
189 rxq = vec_elt_at_index (vd->rxqs, qid);
190 txq = vec_elt_at_index (vd->txqs, qid);
191 send_vmxnet3_details (reg, vd, qid, rxq, qid, txq, swif, if_name,
193 _vec_len (if_name) = 0;
200 #define vl_msg_name_crc_list
201 #include <vmxnet3/vmxnet3_all_api_h.h>
202 #undef vl_msg_name_crc_list
205 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
207 #define _(id,n,crc) \
208 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
209 foreach_vl_msg_name_crc_vmxnet3;
213 /* set tup the API message handling tables */
215 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
217 vmxnet3_main_t *vmxm = &vmxnet3_main;
218 api_main_t *am = &api_main;
221 /* construct the API name */
222 name = format (0, "vmxnet3_%08x%c", api_version, 0);
224 /* ask for a correctly-sized block of API message decode slots */
225 vmxm->msg_id_base = vl_msg_api_get_msg_ids
226 ((char *) name, VL_MSG_FIRST_AVAILABLE);
229 vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base), \
231 vl_api_##n##_t_handler, \
233 vl_api_##n##_t_endian, \
234 vl_api_##n##_t_print, \
235 sizeof(vl_api_##n##_t), 1);
236 foreach_vmxnet3_plugin_api_msg;
239 /* set up the (msg_name, crc, message-id) table */
240 setup_message_id_table (vmxm, am);
247 * fd.io coding-style-patch-verification: ON
250 * eval: (c-set-style "gnu")