virtio: add virtio 1.1 api flags
[vpp.git] / src / vnet / devices / virtio / virtio_api.c
1 /*
2  *------------------------------------------------------------------
3  * virtio_api.c - vnet virtio pci device driver API support
4  *
5  * Copyright (c) 2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/devices/virtio/virtio.h>
27 #include <vnet/devices/virtio/pci.h>
28 #include <vlib/pci/pci_types_api.h>
29
30 #include <vnet/vnet_msg_enum.h>
31
32 #define vl_typedefs             /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_typedefs
35
36 #define vl_endianfun            /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_endianfun
39
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <vnet/vnet_all_api_h.h>
44 #undef vl_printfun
45
46 #include <vlibapi/api_helper_macros.h>
47
48 #define foreach_virtio_pci_api_msg                        \
49 _(VIRTIO_PCI_CREATE, virtio_pci_create)                   \
50 _(VIRTIO_PCI_CREATE_V2, virtio_pci_create_v2)             \
51 _(VIRTIO_PCI_DELETE, virtio_pci_delete)                   \
52 _(SW_INTERFACE_VIRTIO_PCI_DUMP, sw_interface_virtio_pci_dump)
53
54 /* It will be deprecated in 21.01 */
55 static void
56 vl_api_virtio_pci_create_t_handler (vl_api_virtio_pci_create_t * mp)
57 {
58   vlib_main_t *vm = vlib_get_main ();
59   vl_api_virtio_pci_create_reply_t *rmp;
60   vl_api_registration_t *reg;
61   virtio_pci_create_if_args_t _a, *ap = &_a;
62
63   clib_memset (ap, 0, sizeof (*ap));
64
65   pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
66   if (!mp->use_random_mac)
67     {
68       clib_memcpy (ap->mac_addr, mp->mac_address, 6);
69       ap->mac_addr_set = 1;
70     }
71   ap->sw_if_index = (u32) ~ 0;
72   if (mp->gso_enabled)
73     ap->gso_enabled = 1;
74   else
75     ap->gso_enabled = 0;
76   if (mp->checksum_offload_enabled)
77     ap->checksum_offload_enabled = 1;
78   else
79     ap->checksum_offload_enabled = 0;
80
81   ap->features = clib_net_to_host_u64 (mp->features);
82
83   virtio_pci_create_if (vm, ap);
84
85   reg = vl_api_client_index_to_registration (mp->client_index);
86   if (!reg)
87     return;;
88
89   rmp = vl_msg_api_alloc (sizeof (*rmp));
90   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY);
91   rmp->context = mp->context;
92   rmp->retval = htonl (ap->rv);
93   rmp->sw_if_index = htonl (ap->sw_if_index);
94
95   vl_api_send_msg (reg, (u8 *) rmp);
96 }
97
98 static void
99 vl_api_virtio_pci_create_v2_t_handler (vl_api_virtio_pci_create_v2_t * mp)
100 {
101   vlib_main_t *vm = vlib_get_main ();
102   vl_api_virtio_pci_create_v2_reply_t *rmp;
103   vl_api_registration_t *reg;
104   virtio_pci_create_if_args_t _a, *ap = &_a;
105
106   clib_memset (ap, 0, sizeof (*ap));
107
108   pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
109   if (!mp->use_random_mac)
110     {
111       clib_memcpy (ap->mac_addr, mp->mac_address, 6);
112       ap->mac_addr_set = 1;
113     }
114   ap->sw_if_index = (u32) ~ 0;
115
116   STATIC_ASSERT (((int) VIRTIO_API_FLAG_GSO == (int) VIRTIO_FLAG_GSO),
117                  "virtio gso api flag mismatch");
118   STATIC_ASSERT (((int) VIRTIO_API_FLAG_CSUM_OFFLOAD ==
119                   (int) VIRTIO_FLAG_CSUM_OFFLOAD),
120                  "virtio checksum offload api flag mismatch");
121   STATIC_ASSERT (((int) VIRTIO_API_FLAG_GRO_COALESCE ==
122                   (int) VIRTIO_FLAG_GRO_COALESCE),
123                  "virtio gro coalesce api flag mismatch");
124   STATIC_ASSERT (((int) VIRTIO_API_FLAG_PACKED == (int) VIRTIO_FLAG_PACKED),
125                  "virtio packed api flag mismatch");
126   STATIC_ASSERT (((int) VIRTIO_API_FLAG_IN_ORDER ==
127                   (int) VIRTIO_FLAG_IN_ORDER),
128                  "virtio in-order api flag mismatch");
129
130   ap->virtio_flags = clib_net_to_host_u32 (mp->virtio_flags);
131   ap->features = clib_net_to_host_u64 (mp->features);
132
133   if (ap->virtio_flags & VIRTIO_FLAG_GSO)
134     ap->gso_enabled = 1;
135   else
136     ap->gso_enabled = 0;
137   if (ap->virtio_flags & VIRTIO_FLAG_CSUM_OFFLOAD)
138     ap->checksum_offload_enabled = 1;
139   else
140     ap->checksum_offload_enabled = 0;
141
142   virtio_pci_create_if (vm, ap);
143
144   reg = vl_api_client_index_to_registration (mp->client_index);
145   if (!reg)
146     return;;
147
148   rmp = vl_msg_api_alloc (sizeof (*rmp));
149   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_V2_REPLY);
150   rmp->context = mp->context;
151   rmp->retval = htonl (ap->rv);
152   rmp->sw_if_index = htonl (ap->sw_if_index);
153
154   vl_api_send_msg (reg, (u8 *) rmp);
155 }
156
157 static void
158 vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp)
159 {
160   vnet_main_t *vnm = vnet_get_main ();
161   vlib_main_t *vm = vlib_get_main ();
162   virtio_main_t *vim = &virtio_main;
163   int rv = 0;
164   vnet_hw_interface_t *hw;
165   virtio_if_t *vif;
166   vl_api_virtio_pci_delete_reply_t *rmp;
167   vl_api_registration_t *reg;
168
169   hw =
170     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
171                                                    htonl (mp->sw_if_index));
172   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
173     {
174       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
175       goto reply;
176     }
177
178   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
179
180   rv = virtio_pci_delete_if (vm, vif);
181
182 reply:
183   reg = vl_api_client_index_to_registration (mp->client_index);
184   if (!reg)
185     return;
186
187   rmp = vl_msg_api_alloc (sizeof (*rmp));
188   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY);
189   rmp->context = mp->context;
190   rmp->retval = htonl (rv);
191
192   vl_api_send_msg (reg, (u8 *) rmp);
193 }
194
195 static void
196 virtio_pci_send_sw_interface_details (vpe_api_main_t * am,
197                                       vl_api_registration_t * reg,
198                                       virtio_if_t * vif, u32 context)
199 {
200   vl_api_sw_interface_virtio_pci_details_t *mp;
201   mp = vl_msg_api_alloc (sizeof (*mp));
202
203   clib_memset (mp, 0, sizeof (*mp));
204
205   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
206   pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
207                       &mp->pci_addr);
208   mp->sw_if_index = htonl (vif->sw_if_index);
209   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0);
210   mp->rx_ring_sz = htons (vring->size);
211   vring = vec_elt_at_index (vif->txq_vrings, 0);
212   mp->tx_ring_sz = htons (vring->size);
213   clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
214   mp->features = clib_host_to_net_u64 (vif->features);
215
216   mp->context = context;
217   vl_api_send_msg (reg, (u8 *) mp);
218 }
219
220 static void
221   vl_api_sw_interface_virtio_pci_dump_t_handler
222   (vl_api_sw_interface_virtio_pci_dump_t * mp)
223 {
224   vpe_api_main_t *am = &vpe_api_main;
225   vl_api_registration_t *reg;
226   virtio_main_t *vmx = &virtio_main;
227   virtio_if_t *vif;
228
229   reg = vl_api_client_index_to_registration (mp->client_index);
230   if (!reg)
231     return;
232
233   pool_foreach (vif, vmx->interfaces, (
234                                         {
235                                         if (vif->type == VIRTIO_IF_TYPE_PCI)
236                                         {
237                                         virtio_pci_send_sw_interface_details
238                                         (am, reg, vif, mp->context);}
239                                         }
240                 ));
241 }
242
243 #define vl_msg_name_crc_list
244 #include <vnet/vnet_all_api_h.h>
245 #undef vl_msg_name_crc_list
246
247 static void
248 setup_message_id_table (api_main_t * am)
249 {
250 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
251   foreach_vl_msg_name_crc_virtio;
252 #undef _
253 }
254
255 static clib_error_t *
256 virtio_pci_api_hookup (vlib_main_t * vm)
257 {
258   api_main_t *am = vlibapi_get_main ();
259
260 #define _(N,n)                                                  \
261     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
262                            vl_api_##n##_t_handler,              \
263                            vl_noop_handler,                     \
264                            vl_api_##n##_t_endian,               \
265                            vl_api_##n##_t_print,                \
266                            sizeof(vl_api_##n##_t), 1);
267   foreach_virtio_pci_api_msg;
268 #undef _
269
270   /*
271    * Set up the (msg_name, crc, message-id) table
272    */
273   setup_message_id_table (am);
274
275   return 0;
276 }
277
278 VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup);
279
280 /*
281  * fd.io coding-style-patch-verification: ON
282  *
283  * Local Variables:
284  * eval: (c-set-style "gnu")
285  * End:
286  */