hs-test: updated api calls
[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 #include <vnet/format_fns.h>
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.api_enum.h>
30 #include <vmxnet3/vmxnet3.api_types.h>
31
32 #define REPLY_MSG_ID_BASE (vmxm->msg_id_base)
33 #include <vlibapi/api_helper_macros.h>
34
35 static void
36 vl_api_vmxnet3_create_t_handler (vl_api_vmxnet3_create_t * mp)
37 {
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;
42   int rv;
43
44   clib_memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
45
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);
52   args.bind = mp->bind;
53   args.enable_gso = mp->enable_gso;
54
55   vmxnet3_create_if (vm, &args);
56   rv = args.rv;
57
58   REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY,
59                 ({ rmp->sw_if_index = ntohl (args.sw_if_index); }));
60 }
61
62 static void
63 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
64 {
65   vlib_main_t *vm = vlib_get_main ();
66   vnet_main_t *vnm = vnet_get_main ();
67   vmxnet3_main_t *vmxm = &vmxnet3_main;
68   vl_api_vmxnet3_delete_reply_t *rmp;
69   vmxnet3_device_t *vd;
70   vnet_hw_interface_t *hw;
71   int rv = 0;
72
73   hw =
74     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
75                                                    htonl (mp->sw_if_index));
76   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
77     {
78       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
79       goto reply;
80     }
81
82   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
83
84   vmxnet3_delete_if (vm, vd);
85
86 reply:
87   REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY);
88 }
89
90 static void
91 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
92                       vnet_sw_interface_t * swif, u8 * interface_name,
93                       u32 context)
94 {
95   vl_api_vmxnet3_details_t *mp;
96   vnet_main_t *vnm = vnet_get_main ();
97   vmxnet3_main_t *vmxm = &vmxnet3_main;
98   vnet_hw_interface_t *hwif;
99   vmxnet3_rx_ring *ring;
100   u16 rid, qid;
101
102   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
103
104   mp = vl_msg_api_alloc (sizeof (*mp));
105   clib_memset (mp, 0, sizeof (*mp));
106
107   mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
108   mp->context = context;
109
110   mp->sw_if_index = htonl (swif->sw_if_index);
111   strncpy ((char *) mp->if_name,
112            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
113
114   if (hwif->hw_address)
115     memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
116
117   mp->version = vd->version;
118   mp->pci_addr = ntohl (vd->pci_addr.as_u32);
119   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
120
121   mp->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX);
122   vec_foreach_index (qid, vd->rxqs)
123   {
124     vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
125     vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
126
127     ASSERT (qid < VMXNET3_RXQ_MAX);
128     rx_list->rx_qsize = htons (rxq->size);
129     rx_list->rx_next = htons (rxq->rx_comp_ring.next);
130     for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
131       {
132         ring = &rxq->rx_ring[rid];
133         rx_list->rx_fill[rid] = htons (ring->fill);
134         rx_list->rx_produce[rid] = htons (ring->produce);
135         rx_list->rx_consume[rid] = htons (ring->consume);
136       }
137   }
138
139   mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
140   vec_foreach_index (qid, vd->txqs)
141   {
142     vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
143     vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
144
145     ASSERT (qid < VMXNET3_TXQ_MAX);
146     tx_list->tx_qsize = htons (txq->size);
147     tx_list->tx_next = htons (txq->tx_comp_ring.next);
148     tx_list->tx_produce = htons (txq->tx_ring.produce);
149     tx_list->tx_consume = htons (txq->tx_ring.consume);
150   }
151
152   vl_api_send_msg (reg, (u8 *) mp);
153 }
154
155 /**
156  * @brief Message handler for vmxnet3_dump API.
157  * @param mp vl_api_vmxnet3_dump_t * mp the api message
158  */
159 static void
160 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
161 {
162   vmxnet3_main_t *vmxm = &vmxnet3_main;
163   vnet_main_t *vnm = vnet_get_main ();
164   vnet_sw_interface_t *swif;
165   vmxnet3_device_t *vd;
166   u8 *if_name = 0;
167   vl_api_registration_t *reg;
168
169   reg = vl_api_client_index_to_registration (mp->client_index);
170   if (!reg)
171     return;
172
173   pool_foreach (vd, vmxm->devices)
174      {
175       swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
176       if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
177                         swif, 0);
178       send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
179       vec_set_len (if_name, 0);
180     }
181
182   vec_free (if_name);
183 }
184
185 /**
186  * @brief Message handler for vmxnet3_dump API.
187  * @param mp vl_api_vmxnet3_dump_t * mp the api message
188  */
189 static void vl_api_sw_vmxnet3_interface_dump_t_handler
190   (vl_api_sw_vmxnet3_interface_dump_t * mp)
191 {
192   vmxnet3_main_t *vmxm = &vmxnet3_main;
193   vnet_main_t *vnm = vnet_get_main ();
194   vnet_sw_interface_t *swif;
195   vmxnet3_device_t *vd;
196   u8 *if_name = 0;
197   vl_api_registration_t *reg;
198   u32 filter_sw_if_index;
199
200   reg = vl_api_client_index_to_registration (mp->client_index);
201   if (!reg)
202     return;
203
204   filter_sw_if_index = htonl (mp->sw_if_index);
205   if ((filter_sw_if_index != ~0) &&
206       (vnet_sw_interface_is_api_valid (vnm, filter_sw_if_index) == 0))
207     goto bad_sw_if_index;
208
209   pool_foreach (vd, vmxm->devices)
210      {
211       if ((filter_sw_if_index == ~0) ||
212           (vd->sw_if_index == filter_sw_if_index))
213         {
214           swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
215           if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
216                             swif, 0);
217           send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
218           vec_set_len (if_name, 0);
219         }
220     }
221
222   BAD_SW_IF_INDEX_LABEL;
223   vec_free (if_name);
224 }
225
226 /* set tup the API message handling tables */
227 #include <vmxnet3/vmxnet3.api.c>
228 clib_error_t *
229 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
230 {
231   vmxnet3_main_t *vmxm = &vmxnet3_main;
232
233   /* ask for a correctly-sized block of API message decode slots */
234   vmxm->msg_id_base = setup_message_id_table ();
235   return 0;
236 }
237
238 /*
239  * fd.io coding-style-patch-verification: ON
240  *
241  * Local Variables:
242  * eval: (c-set-style "gnu")
243  * End:
244  */