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>
22 #include <vnet/format_fns.h>
23 #include <vmxnet3/vmxnet3.h>
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
28 /* define message IDs */
29 #include <vmxnet3/vmxnet3.api_enum.h>
30 #include <vmxnet3/vmxnet3.api_types.h>
32 #define REPLY_MSG_ID_BASE (vmxm->msg_id_base)
33 #include <vlibapi/api_helper_macros.h>
36 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
38 vlib_main_t *vm = vlib_get_main ();
39 vmxnet3_main_t *vmxm = &vmxnet3_main;
40 vl_api_vmxnet3_create_reply_t *rmp;
41 vmxnet3_create_if_args_t args;
44 clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
46 args.enable_elog = ntohl (mp->enable_elog);
47 args.addr.as_u32 = ntohl (mp->pci_addr);
48 args.rxq_size = ntohs (mp->rxq_size);
49 args.txq_size = ntohs (mp->txq_size);
50 args.txq_num = ntohs (mp->txq_num);
51 args.rxq_num = ntohs (mp->rxq_num);
53 args.enable_gso = mp->enable_gso;
55 vmxnet3_create_if (vm, &args);
59 REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY,
60 ({ rmp->sw_if_index = ntohl (args.sw_if_index); }));
65 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
67 vlib_main_t *vm = vlib_get_main ();
68 vnet_main_t *vnm = vnet_get_main ();
69 vmxnet3_main_t *vmxm = &vmxnet3_main;
70 vl_api_vmxnet3_delete_reply_t *rmp;
72 vnet_hw_interface_t *hw;
76 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
77 htonl (mp->sw_if_index));
78 if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
80 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
84 vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
86 vmxnet3_delete_if (vm, vd);
89 REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY);
93 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
94 vnet_sw_interface_t * swif, u8 * interface_name,
97 vl_api_vmxnet3_details_t *mp;
98 vnet_main_t *vnm = vnet_get_main ();
99 vmxnet3_main_t *vmxm = &vmxnet3_main;
100 vnet_hw_interface_t *hwif;
101 vmxnet3_rx_ring *ring;
104 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
106 mp = vl_msg_api_alloc (sizeof (*mp));
107 clib_memset (mp, 0, sizeof (*mp));
109 mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
110 mp->context = context;
112 mp->sw_if_index = htonl (swif->sw_if_index);
113 strncpy ((char *) mp->if_name,
114 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
116 if (hwif->hw_address)
117 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
119 mp->version = vd->version;
120 mp->pci_addr = ntohl (vd->pci_addr.as_u32);
121 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
123 mp->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX);
124 vec_foreach_index (qid, vd->rxqs)
126 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
127 vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
129 ASSERT (qid < VMXNET3_RXQ_MAX);
130 rx_list->rx_qsize = htons (rxq->size);
131 rx_list->rx_next = htons (rxq->rx_comp_ring.next);
132 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
134 ring = &rxq->rx_ring[rid];
135 rx_list->rx_fill[rid] = htons (ring->fill);
136 rx_list->rx_produce[rid] = htons (ring->produce);
137 rx_list->rx_consume[rid] = htons (ring->consume);
141 mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
142 vec_foreach_index (qid, vd->txqs)
144 vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
145 vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
147 ASSERT (qid < VMXNET3_TXQ_MAX);
148 tx_list->tx_qsize = htons (txq->size);
149 tx_list->tx_next = htons (txq->tx_comp_ring.next);
150 tx_list->tx_produce = htons (txq->tx_ring.produce);
151 tx_list->tx_consume = htons (txq->tx_ring.consume);
154 vl_api_send_msg (reg, (u8 *) mp);
158 * @brief Message handler for vmxnet3_dump API.
159 * @param mp vl_api_vmxnet3_dump_t * mp the api message
162 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
164 vmxnet3_main_t *vmxm = &vmxnet3_main;
165 vnet_main_t *vnm = vnet_get_main ();
166 vnet_sw_interface_t *swif;
167 vmxnet3_device_t *vd;
169 vl_api_registration_t *reg;
171 reg = vl_api_client_index_to_registration (mp->client_index);
176 pool_foreach (vd, vmxm->devices)
178 swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
179 if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
181 send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
182 _vec_len (if_name) = 0;
190 * @brief Message handler for vmxnet3_dump API.
191 * @param mp vl_api_vmxnet3_dump_t * mp the api message
193 static void vl_api_sw_vmxnet3_interface_dump_t_handler
194 (vl_api_sw_vmxnet3_interface_dump_t * mp)
196 vmxnet3_main_t *vmxm = &vmxnet3_main;
197 vnet_main_t *vnm = vnet_get_main ();
198 vnet_sw_interface_t *swif;
199 vmxnet3_device_t *vd;
201 vl_api_registration_t *reg;
202 u32 filter_sw_if_index;
204 reg = vl_api_client_index_to_registration (mp->client_index);
208 filter_sw_if_index = htonl (mp->sw_if_index);
209 if ((filter_sw_if_index != ~0) &&
210 (vnet_sw_interface_is_api_valid (vnm, filter_sw_if_index) == 0))
211 goto bad_sw_if_index;
214 pool_foreach (vd, vmxm->devices)
216 if ((filter_sw_if_index == ~0) ||
217 (vd->sw_if_index == filter_sw_if_index))
219 swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
220 if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
222 send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
223 _vec_len (if_name) = 0;
228 BAD_SW_IF_INDEX_LABEL;
232 /* set tup the API message handling tables */
233 #include <vmxnet3/vmxnet3.api.c>
235 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
237 vmxnet3_main_t *vmxm = &vmxnet3_main;
239 /* ask for a correctly-sized block of API message decode slots */
240 vmxm->msg_id_base = setup_message_id_table ();
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")