vmxnet3 device driver
[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   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
75   vmxnet3_create_if (vm, &args);
76   rv = args.rv;
77
78   /* *INDENT-OFF* */
79   REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
80     ({
81       rmp->sw_if_index = ntohl (args.sw_if_index);
82     }));
83   /* *INDENT-ON* */
84 }
85
86 static void
87 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
88 {
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;
93   vmxnet3_device_t *vd;
94   vnet_hw_interface_t *hw;
95   int rv = 0;
96
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)
99     {
100       rv = VNET_API_ERROR_INVALID_INTERFACE;
101       goto reply;
102     }
103
104   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
105
106   vmxnet3_delete_if (vm, vd);
107
108 reply:
109   REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
110 }
111
112 static void
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)
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;
124
125   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
126
127   mp = vl_msg_api_alloc (sizeof (*mp));
128   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
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++)
146     {
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);
151     }
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);
156
157   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
158
159   vl_api_send_msg (reg, (u8 *) mp);
160 }
161
162 /**
163  * @brief Message handler for vmxnet3_dump API.
164  * @param mp vl_api_vmxnet3_dump_t * mp the api message
165  */
166 static void
167 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
168 {
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;
173   u8 *if_name = 0;
174   vl_api_registration_t *reg;
175   vmxnet3_rxq_t *rxq;
176   vmxnet3_txq_t *txq;
177   u16 qid = 0;
178
179   reg = vl_api_client_index_to_registration (mp->client_index);
180   if (!reg)
181     return;
182
183   /* *INDENT-OFF* */
184   pool_foreach (vd, vmxm->devices,
185     ({
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,
188                         swif, 0);
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,
192                             mp->context);
193       _vec_len (if_name) = 0;
194     }));
195   /* *INDENT-ON* */
196
197   vec_free (if_name);
198 }
199
200 #define vl_msg_name_crc_list
201 #include <vmxnet3/vmxnet3_all_api_h.h>
202 #undef vl_msg_name_crc_list
203
204 static void
205 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
206 {
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;
210 #undef _
211 }
212
213 /* set tup the API message handling tables */
214 clib_error_t *
215 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
216 {
217   vmxnet3_main_t *vmxm = &vmxnet3_main;
218   api_main_t *am = &api_main;
219   u8 *name;
220
221   /* construct the API name */
222   name = format (0, "vmxnet3_%08x%c", api_version, 0);
223
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);
227
228 #define _(N,n)                                                  \
229     vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base),   \
230                            #n,                                  \
231                            vl_api_##n##_t_handler,              \
232                            vl_noop_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;
237 #undef _
238
239   /* set up the (msg_name, crc, message-id) table */
240   setup_message_id_table (vmxm, am);
241
242   vec_free (name);
243   return 0;
244 }
245
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */