vmxnet3: auto bind support
[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   clib_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   args.txq_num = ntohs (mp->txq_num);
75   args.rxq_num = ntohs (mp->rxq_num);
76   args.bind = mp->bind;
77
78   vmxnet3_create_if (vm, &args);
79   rv = args.rv;
80
81   /* *INDENT-OFF* */
82   REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
83     ({
84       rmp->sw_if_index = ntohl (args.sw_if_index);
85     }));
86   /* *INDENT-ON* */
87 }
88
89 static void
90 vl_api_vmxnet3_delete_t_handler (vl_api_vmxnet3_delete_t * mp)
91 {
92   vlib_main_t *vm = vlib_get_main ();
93   vnet_main_t *vnm = vnet_get_main ();
94   vmxnet3_main_t *vmxm = &vmxnet3_main;
95   vl_api_vmxnet3_delete_reply_t *rmp;
96   vmxnet3_device_t *vd;
97   vnet_hw_interface_t *hw;
98   int rv = 0;
99
100   hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
101   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
102     {
103       rv = VNET_API_ERROR_INVALID_INTERFACE;
104       goto reply;
105     }
106
107   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
108
109   vmxnet3_delete_if (vm, vd);
110
111 reply:
112   REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
113 }
114
115 static void
116 send_vmxnet3_details (vl_api_registration_t * reg, vmxnet3_device_t * vd,
117                       vnet_sw_interface_t * swif, u8 * interface_name,
118                       u32 context)
119 {
120   vl_api_vmxnet3_details_t *mp;
121   vnet_main_t *vnm = vnet_get_main ();
122   vmxnet3_main_t *vmxm = &vmxnet3_main;
123   vnet_hw_interface_t *hwif;
124   vmxnet3_rx_ring *ring;
125   u16 rid, qid;
126
127   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
128
129   mp = vl_msg_api_alloc (sizeof (*mp));
130   clib_memset (mp, 0, sizeof (*mp));
131
132   mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
133   mp->context = context;
134
135   mp->sw_if_index = htonl (swif->sw_if_index);
136   strncpy ((char *) mp->if_name,
137            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
138
139   if (hwif->hw_address)
140     memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
141
142   mp->version = vd->version;
143   mp->pci_addr = ntohl (vd->pci_addr.as_u32);
144   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
145
146   mp->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX);
147   vec_foreach_index (qid, vd->rxqs)
148   {
149     vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
150     vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid];
151
152     ASSERT (qid < VMXNET3_RXQ_MAX);
153     rx_list->rx_qsize = htons (rxq->size);
154     rx_list->rx_next = htons (rxq->rx_comp_ring.next);
155     for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
156       {
157         ring = &rxq->rx_ring[rid];
158         rx_list->rx_fill[rid] = htons (ring->fill);
159         rx_list->rx_produce[rid] = htons (ring->produce);
160         rx_list->rx_consume[rid] = htons (ring->consume);
161       }
162   }
163
164   mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX);
165   vec_foreach_index (qid, vd->txqs)
166   {
167     vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
168     vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid];
169
170     ASSERT (qid < VMXNET3_TXQ_MAX);
171     tx_list->tx_qsize = htons (txq->size);
172     tx_list->tx_next = htons (txq->tx_comp_ring.next);
173     tx_list->tx_produce = htons (txq->tx_ring.produce);
174     tx_list->tx_consume = htons (txq->tx_ring.consume);
175   }
176
177   vl_api_send_msg (reg, (u8 *) mp);
178 }
179
180 /**
181  * @brief Message handler for vmxnet3_dump API.
182  * @param mp vl_api_vmxnet3_dump_t * mp the api message
183  */
184 static void
185 vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp)
186 {
187   vmxnet3_main_t *vmxm = &vmxnet3_main;
188   vnet_main_t *vnm = vnet_get_main ();
189   vnet_sw_interface_t *swif;
190   vmxnet3_device_t *vd;
191   u8 *if_name = 0;
192   vl_api_registration_t *reg;
193
194   reg = vl_api_client_index_to_registration (mp->client_index);
195   if (!reg)
196     return;
197
198   /* *INDENT-OFF* */
199   pool_foreach (vd, vmxm->devices,
200     ({
201       swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
202       if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
203                         swif, 0);
204       send_vmxnet3_details (reg, vd, swif, if_name, mp->context);
205       _vec_len (if_name) = 0;
206     }));
207   /* *INDENT-ON* */
208
209   vec_free (if_name);
210 }
211
212 #define vl_msg_name_crc_list
213 #include <vmxnet3/vmxnet3_all_api_h.h>
214 #undef vl_msg_name_crc_list
215
216 static void
217 setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am)
218 {
219 #define _(id,n,crc) \
220   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
221   foreach_vl_msg_name_crc_vmxnet3;
222 #undef _
223 }
224
225 /* set tup the API message handling tables */
226 clib_error_t *
227 vmxnet3_plugin_api_hookup (vlib_main_t * vm)
228 {
229   vmxnet3_main_t *vmxm = &vmxnet3_main;
230   api_main_t *am = &api_main;
231   u8 *name;
232
233   /* construct the API name */
234   name = format (0, "vmxnet3_%08x%c", api_version, 0);
235
236   /* ask for a correctly-sized block of API message decode slots */
237   vmxm->msg_id_base = vl_msg_api_get_msg_ids
238     ((char *) name, VL_MSG_FIRST_AVAILABLE);
239
240 #define _(N,n)                                                  \
241     vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base),   \
242                            #n,                                  \
243                            vl_api_##n##_t_handler,              \
244                            vl_noop_handler,                     \
245                            vl_api_##n##_t_endian,               \
246                            vl_api_##n##_t_print,                \
247                            sizeof(vl_api_##n##_t), 1);
248   foreach_vmxnet3_plugin_api_msg;
249 #undef _
250
251   /* set up the (msg_name, crc, message-id) table */
252   setup_message_id_table (vmxm, am);
253
254   vec_free (name);
255   return 0;
256 }
257
258 /*
259  * fd.io coding-style-patch-verification: ON
260  *
261  * Local Variables:
262  * eval: (c-set-style "gnu")
263  * End:
264  */