blob: 6234f64fcfbe29e42a37b16f2aa9abe2993f82f4 [file] [log] [blame]
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001/*
2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <fcntl.h>
17#include <sys/ioctl.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020018
19#include <vppinfra/types.h>
20#include <vlib/vlib.h>
21#include <vlib/pci/pci.h>
22#include <vnet/ethernet/ethernet.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020023#include <vnet/ip/ip4_packet.h>
24#include <vnet/ip/ip6_packet.h>
25#include <vnet/devices/virtio/virtio.h>
26#include <vnet/devices/virtio/pci.h>
Damjan Marion94100532020-11-06 23:25:57 +010027#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020028
29#define PCI_VENDOR_ID_VIRTIO 0x1af4
30#define PCI_DEVICE_ID_VIRTIO_NIC 0x1000
31/* Doesn't support modern device */
32#define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041
33
34#define PCI_CAPABILITY_LIST 0x34
35#define PCI_CAP_ID_VNDR 0x09
36#define PCI_CAP_ID_MSIX 0x11
37
38#define PCI_MSIX_ENABLE 0x8000
39
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020040static pci_device_id_t virtio_pci_device_ids[] = {
41 {
42 .vendor_id = PCI_VENDOR_ID_VIRTIO,
43 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
44 {
45 .vendor_id = PCI_VENDOR_ID_VIRTIO,
46 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
47 {0},
48};
49
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020050static u32
51virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
52 u32 flags)
53{
54 return 0;
55}
56
57static clib_error_t *
58virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
59{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020060 clib_error_t *error = 0;
61 u16 max_queue_pairs = 1;
62
63 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
64 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000065 max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020066 }
67
Damjan Marionf41244f2019-11-08 17:41:06 +010068 virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020069 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
Mohsin Kazmi57d73212020-03-05 12:22:41 +000070 return clib_error_return (error, "max queue pair is %x,"
71 " should be in range [1, 0x8000]",
72 max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020073
74 vif->max_queue_pairs = max_queue_pairs;
75 return error;
76}
77
78static void
79virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
80{
Mohsin Kazmia0a68332020-07-16 12:55:42 +000081 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
82 vif->virtio_pci_func->set_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020083}
84
85static u32
86virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
87{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000088 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020089 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000090 vif->virtio_pci_func->get_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020091 return 0;
92 }
93 return 1;
94}
95
96static u16
97virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
98{
99 /*
100 * Minimal driver: assumes link is up
101 */
102 u16 status = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000103 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000104 status = vif->virtio_pci_func->get_device_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200105 return status;
106}
107
108static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000109virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
110 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200111{
112 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000113 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200114 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000115 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000116 line--;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200117 u16 qid = line;
118
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000119 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion94100532020-11-06 23:25:57 +0100120 vnet_hw_if_rx_queue_set_int_pending (vnm, vring->queue_index);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200121}
122
123static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000124virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
125 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200126{
127 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000128 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200129 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000130 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200131
132 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
133 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200134 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
135 VNET_HW_INTERFACE_FLAG_LINK_UP);
136 }
137 else
138 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200139 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
140 }
141}
142
143static void
144virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
145{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000146 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200147 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000148 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200149 u8 isr = 0;
150 u16 line = 0;
151
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000152 isr = vif->virtio_pci_func->get_isr (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200153
154 /*
155 * If the lower bit is set: look through the used rings of
156 * all virtqueues for the device, to see if any progress has
157 * been made by the device which requires servicing.
158 */
159 if (isr & VIRTIO_PCI_ISR_INTR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000160 {
161 for (; line < vif->num_rxqs; line++)
162 virtio_pci_irq_queue_handler (vm, h, (line + 1));
163 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200164
165 if (isr & VIRTIO_PCI_ISR_CONFIG)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000166 virtio_pci_irq_config_handler (vm, h, line);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200167}
168
169inline void
170device_status (vlib_main_t * vm, virtio_if_t * vif)
171{
172 struct status_struct
173 {
174 u8 bit;
175 char *str;
176 };
177 struct status_struct *status_entry;
178 static struct status_struct status_array[] = {
179#define _(s,b) { .str = #s, .bit = b, },
180 foreach_virtio_config_status_flags
181#undef _
182 {.str = NULL}
183 };
184
185 vlib_cli_output (vm, " status 0x%x", vif->status);
186
187 status_entry = (struct status_struct *) &status_array;
188 while (status_entry->str)
189 {
190 if (vif->status & status_entry->bit)
191 vlib_cli_output (vm, " %s (%x)", status_entry->str,
192 status_entry->bit);
193 status_entry++;
194 }
195}
196
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000197static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100198virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif,
199 virtio_ctrl_msg_t * data, u32 len)
200{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000201 vnet_virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100202 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
203 virtio_ctrl_msg_t result;
204 u32 buffer_index;
205 vlib_buffer_t *b;
206 u16 used, next;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000207 u16 sz = vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100208 u16 flags = 0, first_desc_flags = 0;
209
210 used = vring->desc_in_use;
211 next = vring->desc_next;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000212 vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100213
214 if (vlib_buffer_alloc (vm, &buffer_index, 1))
215 b = vlib_get_buffer (vm, buffer_index);
216 else
217 return VIRTIO_NET_ERR;
218 /*
219 * current_data may not be initialized with 0 and may contain
220 * previous offset.
221 */
222 b->current_data = 0;
223 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
224
225 first_desc_flags = VRING_DESC_F_NEXT;
226 if (vring->avail_wrap_counter)
227 {
228 first_desc_flags |= VRING_DESC_F_AVAIL;
229 first_desc_flags &= ~VRING_DESC_F_USED;
230 }
231 else
232 {
233 first_desc_flags &= ~VRING_DESC_F_AVAIL;
234 first_desc_flags |= VRING_DESC_F_USED;
235 }
236 d->addr = vlib_buffer_get_current_pa (vm, b);
237 d->len = sizeof (virtio_net_ctrl_hdr_t);
238 d->id = next;
239
240 next++;
241 if (next >= sz)
242 {
243 next = 0;
244 vring->avail_wrap_counter ^= 1;
245 }
246 used++;
247
248 d = &vring->packed_desc[next];
249 flags = VRING_DESC_F_NEXT;
250 if (vring->avail_wrap_counter)
251 {
252 flags |= VRING_DESC_F_AVAIL;
253 flags &= ~VRING_DESC_F_USED;
254 }
255 else
256 {
257 flags &= ~VRING_DESC_F_AVAIL;
258 flags |= VRING_DESC_F_USED;
259 }
260 d->addr = vlib_buffer_get_current_pa (vm, b) +
261 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
262 d->len = len;
263 d->id = next;
264 d->flags = flags;
265
266 next++;
267 if (next >= sz)
268 {
269 next = 0;
270 vring->avail_wrap_counter ^= 1;
271 }
272 used++;
273
274 d = &vring->packed_desc[next];
275 flags = VRING_DESC_F_WRITE;
276 if (vring->avail_wrap_counter)
277 {
278 flags |= VRING_DESC_F_AVAIL;
279 flags &= ~VRING_DESC_F_USED;
280 }
281 else
282 {
283 flags &= ~VRING_DESC_F_AVAIL;
284 flags |= VRING_DESC_F_USED;
285 }
286 d->addr = vlib_buffer_get_current_pa (vm, b) +
287 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
288 d->len = sizeof (data->status);
289 d->id = next;
290 d->flags = flags;
291
292 next++;
293 if (next >= sz)
294 {
295 next = 0;
296 vring->avail_wrap_counter ^= 1;
297 }
298 used++;
299
300 CLIB_MEMORY_STORE_BARRIER ();
301 vring->packed_desc[vring->desc_next].flags = first_desc_flags;
302 vring->desc_next = next;
303 vring->desc_in_use = used;
304 CLIB_MEMORY_BARRIER ();
305 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
306 {
307 virtio_kick (vm, vring, vif);
308 }
309
310 u16 last = vring->last_used_idx;
311 d = &vring->packed_desc[last];
312 do
313 {
314 flags = d->flags;
315 }
316 while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7)
317 || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15));
318
319 last += 3;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000320 if (last >= vring->queue_size)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100321 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000322 last = last - vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100323 vring->used_wrap_counter ^= 1;
324 }
325 vring->desc_in_use -= 3;
326 vring->last_used_idx = last;
327
328 CLIB_MEMORY_BARRIER ();
329 clib_memcpy (&result, vlib_buffer_get_current (b),
330 sizeof (virtio_ctrl_msg_t));
331 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
332 status = result.status;
333 vlib_buffer_free (vm, &buffer_index, 1);
334 return status;
335}
336
337static int
338virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif,
339 virtio_ctrl_msg_t * data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000340{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000341 vnet_virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200342 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
343 virtio_ctrl_msg_t result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000344 u32 buffer_index;
345 vlib_buffer_t *b;
346 u16 used, next, avail;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000347 u16 sz = vring->queue_size;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000348 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000349
350 used = vring->desc_in_use;
351 next = vring->desc_next;
352 avail = vring->avail->idx;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000353 vnet_virtio_vring_desc_t *d = &vring->desc[next];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000354
355 if (vlib_buffer_alloc (vm, &buffer_index, 1))
356 b = vlib_get_buffer (vm, buffer_index);
357 else
358 return VIRTIO_NET_ERR;
359 /*
360 * current_data may not be initialized with 0 and may contain
361 * previous offset.
362 */
363 b->current_data = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200364 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000365 d->flags = VRING_DESC_F_NEXT;
366 d->addr = vlib_buffer_get_current_pa (vm, b);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200367 d->len = sizeof (virtio_net_ctrl_hdr_t);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000368 vring->avail->ring[avail & mask] = next;
369 avail++;
370 next = (next + 1) & mask;
371 d->next = next;
372 used++;
373
374 d = &vring->desc[next];
375 d->flags = VRING_DESC_F_NEXT;
376 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200377 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200378 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000379 next = (next + 1) & mask;
380 d->next = next;
381 used++;
382
383 d = &vring->desc[next];
384 d->flags = VRING_DESC_F_WRITE;
385 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200386 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200387 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000388 next = (next + 1) & mask;
389 used++;
390
391 CLIB_MEMORY_STORE_BARRIER ();
392 vring->avail->idx = avail;
393 vring->desc_next = next;
394 vring->desc_in_use = used;
395
396 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
397 {
398 virtio_kick (vm, vring, vif);
399 }
400
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000401 u16 last = vring->last_used_idx, n_left = 0;
402 n_left = vring->used->idx - last;
403
404 while (n_left)
405 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000406 vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000407 u16 slot = e->id;
408
409 d = &vring->desc[slot];
410 while (d->flags & VRING_DESC_F_NEXT)
411 {
412 used--;
413 slot = d->next;
414 d = &vring->desc[slot];
415 }
416 used--;
417 last++;
418 n_left--;
419 }
420 vring->desc_in_use = used;
421 vring->last_used_idx = last;
422
423 CLIB_MEMORY_BARRIER ();
424 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200425 sizeof (virtio_ctrl_msg_t));
Damjan Marionf41244f2019-11-08 17:41:06 +0100426 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000427 status = result.status;
428 vlib_buffer_free (vm, &buffer_index, 1);
429 return status;
430}
431
Mohsin Kazmi05493782019-05-01 14:26:17 +0200432static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100433virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
434 virtio_ctrl_msg_t * data, u32 len)
435{
436 if (vif->is_packed)
437 return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len);
438 else
439 return virtio_pci_send_ctrl_msg_split (vm, vif, data, len);
440}
441
442static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000443virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
444{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200445 virtio_ctrl_msg_t offload_hdr;
446 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000447
448 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
449 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
450 offload_hdr.status = VIRTIO_NET_ERR;
451 u64 offloads = 0ULL;
452 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
453
454 status =
455 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
456 virtio_log_debug (vif, "disable offloads");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000457 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
458 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000459 return status;
460}
461
462static int
463virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
464{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200465 virtio_ctrl_msg_t csum_offload_hdr;
466 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000467
468 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
469 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
470 csum_offload_hdr.status = VIRTIO_NET_ERR;
471 u64 offloads = 0ULL;
472 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
473 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
474
475 status =
476 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
477 virtio_log_debug (vif, "enable checksum offload");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000478 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
479 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000480 return status;
481}
482
483static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200484virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
485{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200486 virtio_ctrl_msg_t gso_hdr;
487 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200488
489 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
490 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
491 gso_hdr.status = VIRTIO_NET_ERR;
492 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
493 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000494 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200495 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
496
497 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100498 virtio_log_debug (vif, "enable gso");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000499 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
500 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200501 return status;
502}
503
504static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000505virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
506 int csum_offload_enabled)
507{
508 vnet_main_t *vnm = vnet_get_main ();
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000509 vnet_hw_if_caps_change_t cc = {};
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000510
511 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
512 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
513 {
514 if (gso_enabled
515 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
516 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
517 {
518 if (virtio_pci_enable_gso (vm, vif))
519 {
520 virtio_log_warning (vif, "gso is not enabled");
521 }
522 else
523 {
524 vif->gso_enabled = 1;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000525 vif->csum_offload_enabled = 1;
526 cc.val = cc.mask = VNET_HW_IF_CAP_TCP_GSO |
527 VNET_HW_IF_CAP_TX_TCP_CKSUM |
528 VNET_HW_IF_CAP_TX_UDP_CKSUM;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000529 }
530 }
531 else if (csum_offload_enabled
532 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
533 {
534 if (virtio_pci_enable_checksum_offload (vm, vif))
535 {
536 virtio_log_warning (vif, "checksum offload is not enabled");
537 }
538 else
539 {
540 vif->csum_offload_enabled = 1;
541 vif->gso_enabled = 0;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000542 cc.val =
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100543 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000544 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
545 VNET_HW_IF_CAP_TX_UDP_CKSUM;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000546 }
547 }
548 else
549 {
550 if (virtio_pci_disable_offload (vm, vif))
551 {
552 virtio_log_warning (vif, "offloads are not disabled");
553 }
554 else
555 {
556 vif->csum_offload_enabled = 0;
557 vif->gso_enabled = 0;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000558 cc.val = 0;
559 cc.mask = VNET_HW_IF_CAP_L4_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000560 }
561 }
562 }
563
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000564 if (cc.mask)
565 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
566
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000567 return 0;
568}
569
570static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200571virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
572 u16 num_queues)
573{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200574 virtio_ctrl_msg_t mq_hdr;
575 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200576
577 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
578 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
579 mq_hdr.status = VIRTIO_NET_ERR;
580 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
581
582 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100583 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200584 return status;
585}
586
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200587static u8
588virtio_pci_queue_size_valid (u16 qsz)
589{
590 if (qsz < 64 || qsz > 4096)
591 return 0;
592 if ((qsz % 64) != 0)
593 return 0;
594 return 1;
595}
596
597clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100598virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
599 u16 queue_num)
600{
601 clib_error_t *error = 0;
602 u16 queue_size = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000603 vnet_virtio_vring_t *vring;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100604 u32 i = 0;
605 void *ptr = NULL;
606
607 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
608
609 if (queue_size > 32768)
610 return clib_error_return (0, "ring size must be 32768 or lower");
611
612 if (queue_size == 0)
613 queue_size = 256;
614
615 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
616 vring = vec_elt_at_index (vif->cxq_vring, 0);
617
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000618 i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
619 sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN -
620 1) &
621 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) +
622 sizeof (vnet_virtio_vring_desc_event_t);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100623
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000624 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
625 vif->numa_node);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100626 if (!ptr)
627 return vlib_physmem_last_error (vm);
628 clib_memset (ptr, 0, i);
629
630 vring->packed_desc = ptr;
631
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000632 vring->driver_event =
633 ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100634 vring->driver_event->off_wrap = 0;
635 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
636
637 vring->device_event =
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000638 ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
639 sizeof (vnet_virtio_vring_desc_event_t) +
640 VNET_VIRTIO_PCI_VRING_ALIGN - 1) &
641 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100642 vring->device_event->off_wrap = 0;
643 vring->device_event->flags = 0;
644
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000645 vring->total_packets = 0;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100646 vring->queue_id = queue_num;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000647 vring->queue_size = queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100648 vring->avail_wrap_counter = 1;
649 vring->used_wrap_counter = 1;
650
651 ASSERT (vring->buffers == 0);
652
653 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
654 queue_size);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000655 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100656 vring->queue_notify_offset =
657 vif->notify_off_multiplier *
658 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
659 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
660 queue_num, vring->queue_notify_offset);
661 return error;
662}
663
664clib_error_t *
665virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
666 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200667{
668 clib_error_t *error = 0;
669 u16 queue_size = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000670 vnet_virtio_vring_t *vring;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200671 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000672 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200673
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000674 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200675 if (!virtio_pci_queue_size_valid (queue_size))
676 clib_warning ("queue size is not valid");
677
678 if (!is_pow2 (queue_size))
679 return clib_error_return (0, "ring size must be power of 2");
680
681 if (queue_size > 32768)
682 return clib_error_return (0, "ring size must be 32768 or lower");
683
684 if (queue_size == 0)
685 queue_size = 256;
686
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000687 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
688 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000689 i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN);
690 i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN);
691 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
692 vif->numa_node);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000693 if (!ptr)
694 return vlib_physmem_last_error (vm);
695 clib_memset (ptr, 0, i);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000696 vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000697 vring->queue_id = queue_num;
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000698 vring->total_packets = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000699
700 ASSERT (vring->buffers == 0);
Damjan Marionf41244f2019-11-08 17:41:06 +0100701 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000702 queue_size);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000703 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring);
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000704 vring->queue_notify_offset =
705 vif->notify_off_multiplier *
706 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
707 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
708 queue_num, vring->queue_notify_offset);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000709
710 return error;
711}
712
713clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100714virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
715 u16 queue_num)
716{
717 if (vif->is_packed)
718 return virtio_pci_control_vring_packed_init (vm, vif, queue_num);
719 else
720 return virtio_pci_control_vring_split_init (vm, vif, queue_num);
721}
722
723clib_error_t *
Mohsin Kazmia181eaa2023-08-29 09:18:20 +0000724virtio_pci_vring_split_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num,
725 u16 txq_size)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000726{
727 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000728 u16 queue_size = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000729 vnet_virtio_vring_t *vring;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000730 u32 i = 0;
731 void *ptr = NULL;
732
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000733 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000734 if (!virtio_pci_queue_size_valid (queue_size))
735 clib_warning ("queue size is not valid");
736
737 if (!is_pow2 (queue_size))
738 return clib_error_return (0, "ring size must be power of 2");
739
740 if (queue_size > 32768)
741 return clib_error_return (0, "ring size must be 32768 or lower");
742
743 if (queue_size == 0)
744 queue_size = 256;
745
746 if (queue_num % 2)
747 {
Mohsin Kazmia181eaa2023-08-29 09:18:20 +0000748 if (txq_size)
749 {
750 virtio_log_debug (vif, "tx-queue: number %u, default-size %u",
751 queue_num, queue_size);
752 vif->virtio_pci_func->set_queue_size (vm, vif, queue_num, txq_size);
753 queue_size =
754 vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
755 virtio_log_debug (vif, "tx-queue: number %u, new size %u", queue_num,
756 queue_size);
757 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000758 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
759 CLIB_CACHE_LINE_BYTES);
760 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000761 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000762 }
763 else
764 {
765 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
766 CLIB_CACHE_LINE_BYTES);
767 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
768 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000769 i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN);
770 i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN);
771 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
772 vif->numa_node);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000773 if (!ptr)
774 return vlib_physmem_last_error (vm);
775 clib_memset (ptr, 0, i);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000776 vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000777 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200778 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200779 vring->flow_table = 0;
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000780 vring->total_packets = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200781
782 ASSERT (vring->buffers == 0);
783 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000784 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200785 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100786 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000787 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100788 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200789 }
790 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000791 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100792 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000793 queue_size);
794 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000795 vring->queue_size = queue_size;
796 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200797 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200798
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000799 vring->queue_notify_offset =
800 vif->notify_off_multiplier *
801 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
802 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
803 queue_num, vring->queue_notify_offset);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200804 return error;
805}
806
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100807clib_error_t *
808virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
809 u16 queue_num)
810{
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100811 clib_error_t *error = 0;
812 u16 queue_size = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000813 vnet_virtio_vring_t *vring;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100814 u32 i = 0;
815 void *ptr = NULL;
816
817 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
818
819 if (queue_size > 32768)
820 return clib_error_return (0, "ring size must be 32768 or lower");
821
822 if (queue_size == 0)
823 queue_size = 256;
824
825 if (queue_num % 2)
826 {
827 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
828 CLIB_CACHE_LINE_BYTES);
829 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000830 clib_spinlock_init (&vring->lockp);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100831 }
832 else
833 {
834 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
835 CLIB_CACHE_LINE_BYTES);
836 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
837 }
838
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000839 i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
840 sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN -
841 1) &
842 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) +
843 sizeof (vnet_virtio_vring_desc_event_t);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100844
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000845 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
846 vif->numa_node);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100847 if (!ptr)
848 return vlib_physmem_last_error (vm);
849
850 clib_memset (ptr, 0, i);
851 vring->packed_desc = ptr;
852
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000853 vring->driver_event =
854 ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100855 vring->driver_event->off_wrap = 0;
856 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
857
858 vring->device_event =
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000859 ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
860 sizeof (vnet_virtio_vring_desc_event_t) +
861 VNET_VIRTIO_PCI_VRING_ALIGN - 1) &
862 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100863 vring->device_event->off_wrap = 0;
864 vring->device_event->flags = 0;
865
866 vring->queue_id = queue_num;
867
868 vring->avail_wrap_counter = 1;
869 vring->used_wrap_counter = 1;
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000870 vring->total_packets = 0;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100871
872 ASSERT (vring->buffers == 0);
873 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
874 if (queue_num % 2)
875 {
876 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
877 queue_size);
878 clib_memset_u32 (vring->buffers, ~0, queue_size);
879 }
880 else
881 {
882 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
883 queue_size);
884 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000885 vring->queue_size = queue_size;
886 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring))
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100887 return clib_error_return (0, "error in queue address setup");
888
889 vring->queue_notify_offset =
890 vif->notify_off_multiplier *
891 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
892 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
893 queue_num, vring->queue_notify_offset);
894
895 return error;
896}
897
898clib_error_t *
Mohsin Kazmia181eaa2023-08-29 09:18:20 +0000899virtio_pci_vring_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num,
900 u16 txq_size)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100901{
902 if (vif->is_packed)
903 return virtio_pci_vring_packed_init (vm, vif, queue_num);
904 else
Mohsin Kazmia181eaa2023-08-29 09:18:20 +0000905 return virtio_pci_vring_split_init (vm, vif, queue_num, txq_size);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100906}
907
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200908static void
909virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
910 u64 req_features)
911{
912 /*
913 * if features are not requested
914 * default: all supported features
915 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200916 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
917 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
918 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
919 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200920 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200921 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
922 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
923 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
924 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
925 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
926 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
927 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200928 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
929 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000930 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
931 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000932 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200933 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
934 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
935
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200936 if (vif->is_modern)
937 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
938
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100939 if (vif->is_packed)
940 {
941 supported_features |=
942 (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
943 VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
944 }
945
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200946 if (req_features == 0)
947 {
948 req_features = supported_features;
949 }
950
951 vif->features = req_features & vif->remote_features & supported_features;
952
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000953 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200954 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000955 u16 mtu = 0;
956 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
957
958 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200959 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
960 }
961
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100962 if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
963 vif->is_packed = 0;
964
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000965 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
966 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200967}
968
969void
970virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
971{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000972 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200973}
974
975int
976virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
977{
978 u8 status = 0;
979
980 /*
981 * Reset the device
982 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000983 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200984
985 /*
986 * Set the Acknowledge status bit
987 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000988 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200989
990 /*
991 * Set the Driver status bit
992 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000993 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200994
995 /*
996 * Read the status and verify it
997 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000998 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200999 if ((status & VIRTIO_CONFIG_STATUS_ACK)
1000 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
1001 vif->status = status;
1002 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001003 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001004
1005 return 0;
1006}
1007
1008clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001009virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001010{
1011 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001012 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001013 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001014 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1015
1016 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001017 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001018 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001019 return clib_error_return (error,
1020 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001021 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001022 while (pos)
1023 {
1024 if ((error =
1025 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1026 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001027 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001028 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001029 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001030 return clib_error_return (error,
1031 "error in reading the capability at [%2x]",
1032 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001033 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001034
1035 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1036 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001037 u16 flags, table_size, table_size_mask = 0x07FF;
1038
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001039 if ((error =
1040 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1041 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001042 return clib_error_return (error,
1043 "error in reading the capability at [%2x]",
1044 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001045
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001046 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +01001047 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001048 "msix interrupt vector table-size", table_size);
1049
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001050 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001051 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001052 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001053 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001054 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001055 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001056 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001057 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001058 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001059 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001060 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001061 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001062 }
1063
1064 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1065 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001066 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001067 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001068 goto next;
1069 }
1070
Damjan Marionf41244f2019-11-08 17:41:06 +01001071 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001072 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1073 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001074
Mohsin Kazmi0a507d72020-09-01 18:18:48 +02001075 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +02001076 {
1077 vif->bar = bar[cap.bar];
1078 vif->bar_id = cap.bar;
1079 }
1080 else
1081 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001082
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001083 switch (cap.cfg_type)
1084 {
1085 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001086 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001087 common_cfg = 1;
1088 break;
1089 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001090 if ((error =
1091 vlib_pci_read_write_config (vm, h, VLIB_READ,
1092 pos + sizeof (cap),
1093 &vif->notify_off_multiplier,
1094 sizeof
1095 (vif->notify_off_multiplier))))
1096 {
1097 virtio_log_error (vif, "notify off multiplier is not given");
1098 }
1099 else
1100 {
1101 virtio_log_debug (vif, "notify off multiplier is %u",
1102 vif->notify_off_multiplier);
1103 vif->notify_offset = cap.offset;
1104 notify = 1;
1105 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001106 break;
1107 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001108 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001109 dev_cfg = 1;
1110 break;
1111 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001112 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001113 isr = 1;
1114 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001115 case VIRTIO_PCI_CAP_PCI_CFG:
1116 if (cap.bar == 0)
1117 pci_cfg = 1;
1118 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001119 }
1120 next:
1121 pos = cap.cap_next;
1122 }
1123
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001124 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001125 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001126 vif->virtio_pci_func = &virtio_pci_legacy_func;
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001127 vif->notify_off_multiplier = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001128 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001129 return error;
1130 }
1131
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001132 vif->is_modern = 1;
1133 vif->virtio_pci_func = &virtio_pci_modern_func;
1134
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001135 if (!pci_cfg)
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001136 {
1137 virtio_log_debug (vif, "modern virtio pci device found");
1138 }
1139 else
1140 {
1141 virtio_log_debug (vif, "transitional virtio pci device found");
1142 }
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001143
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001144 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001145}
1146
1147static clib_error_t *
1148virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001149 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001150{
1151 clib_error_t *error = 0;
1152 u8 status = 0;
1153
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001154 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001155 {
1156 args->rv = VNET_API_ERROR_UNSUPPORTED;
1157 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001158 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001159 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001160
1161 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001162 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001163 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001164 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001165 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001166 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001167 /*
1168 * read device features and negotiate (user) requested features
1169 */
1170 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +00001171 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1172 0)
1173 {
1174 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1175 "support VIRTIO_RING_F_INDIRECT_DESC features");
1176 }
1177 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1178 {
1179 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1180 "support VIRTIO_NET_F_MRG_RXBUF features");
1181 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001182 virtio_negotiate_features (vm, vif, args->features);
1183
1184 /*
1185 * After FEATURE_OK, driver should not accept new feature bits
1186 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001187 vif->virtio_pci_func->set_status (vm, vif,
1188 VIRTIO_CONFIG_STATUS_FEATURES_OK);
1189 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001190 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001191 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001192 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001193 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001194 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001195 return clib_error_return (error,
1196 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001197 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001198 vif->status = status;
1199
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001200 /*
1201 * get or set the mac address
1202 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001203 if (virtio_pci_get_mac (vm, vif))
1204 {
1205 f64 now = vlib_time_now (vm);
1206 u32 rnd;
1207 rnd = (u32) (now * 1e6);
1208 rnd = random_u32 (&rnd);
1209
1210 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1211 vif->mac_addr[0] = 2;
1212 vif->mac_addr[1] = 0xfe;
1213 virtio_pci_set_mac (vm, vif);
1214 }
1215
1216 virtio_set_net_hdr_size (vif);
1217
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001218 /*
1219 * Initialize the virtqueues
1220 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001221 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001222 {
1223 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1224 goto err;
1225 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001226
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001227 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1228 {
1229 if (vif->msix_table_size <= vif->max_queue_pairs)
1230 {
1231 virtio_log_error (vif,
1232 "error MSIX lines (%u) <= Number of RXQs (%u)",
1233 vif->msix_table_size, vif->max_queue_pairs);
1234 return clib_error_return (error,
1235 "error MSIX lines (%u) <= Number of RXQs (%u)",
1236 vif->msix_table_size,
1237 vif->max_queue_pairs);
1238 }
1239 }
1240
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001241 for (int i = 0; i < vif->max_queue_pairs; i++)
1242 {
Mohsin Kazmia181eaa2023-08-29 09:18:20 +00001243 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i), 0)))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001244 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001245 args->rv = VNET_API_ERROR_INIT_FAILED;
1246 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1247 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001248 error =
1249 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1250 RX_QUEUE (i), "initialization");
1251 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001252 }
1253 else
1254 {
1255 vif->num_rxqs++;
1256 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001257
Mohsin Kazmia181eaa2023-08-29 09:18:20 +00001258 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i),
1259 args->tx_queue_size)))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001260 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001261 args->rv = VNET_API_ERROR_INIT_FAILED;
1262 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1263 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001264 error =
1265 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1266 TX_QUEUE (i), "initialization");
1267 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001268 }
1269 else
1270 {
1271 vif->num_txqs++;
1272 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001273 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001274
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001275 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1276 {
1277 if ((error =
1278 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1279 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001280 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001281 vif->max_queue_pairs * 2, "initialization");
1282 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1283 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1284 }
1285 }
1286 else
1287 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001288 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001289 vif->cxq_vring = NULL;
1290 }
1291
1292 /*
1293 * set the msix interrupts
1294 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001295 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001296 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001297 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001298 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001299 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001300 {
1301 virtio_log_warning (vif, "config vector 0 is not set");
1302 }
1303 else
1304 {
1305 virtio_log_debug (vif, "config msix vector is set at 0");
1306 }
1307 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1308 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001309 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1310 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001311 VIRTIO_MSI_NO_VECTOR)
1312 {
1313 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1314 RX_QUEUE (i), j);
1315 }
1316 else
1317 {
1318 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1319 RX_QUEUE (i), "msix vector is set at", j);
1320 }
1321 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001322 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001323
1324 /*
1325 * set the driver status OK
1326 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001327 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1328 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001329err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001330 return error;
1331}
1332
1333void
1334virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1335{
1336 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001337 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001338 virtio_if_t *vif;
1339 vlib_pci_dev_handle_t h;
1340 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001341 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001342
Damjan Marionb2c31b62020-12-13 21:47:40 +01001343 pool_foreach (vif, vim->interfaces) {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001344 if (vif->pci_addr.as_u32 == args->addr)
1345 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001346 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001347 args->error =
1348 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001349 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1350 format_vlib_pci_addr, &args->addr,
1351 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001352 return;
1353 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001354 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001355
Benoît Gannec04d8c42022-10-13 14:01:03 +02001356 if (args->bind)
1357 {
1358 vlib_pci_addr_t pci = { .as_u32 = args->addr };
1359 error = vlib_pci_bind_to_uio (vm, &pci, (char *) "auto",
1360 VIRTIO_BIND_FORCE == args->bind);
1361 if (error)
1362 {
1363 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1364 args->error =
1365 clib_error_return (error, "%U: %s", format_vlib_pci_addr, &pci,
1366 "error encountered on binding pci device");
1367 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1368 format_vlib_pci_addr, &pci,
1369 "error encountered on binding pci devicee");
1370 return;
1371 }
1372 }
1373
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001374 pool_get (vim->interfaces, vif);
1375 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001376 vif->per_interface_next_index = ~0;
1377 vif->pci_addr.as_u32 = args->addr;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001378 if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1379 vif->is_packed = 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001380
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001381 if ((error =
1382 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1383 virtio_pci_device_ids, &h)))
1384 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001385 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1386 args->error =
1387 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1388 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001389 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1390 format_vlib_pci_addr, &vif->pci_addr,
1391 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001392 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001393 return;
1394 }
1395 vif->pci_dev_handle = h;
1396 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001397 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001398 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001399
1400 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001401 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001402 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001403 goto error;
1404 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001405
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001406 void *bar[6];
1407 for (u32 i = 0; i <= 5; i++)
1408 {
1409
1410 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1411 {
1412 virtio_log_debug (vif, "no pci map region for bar %u", i);
1413 }
1414 else
1415 {
1416 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1417 bar[i]);
1418 }
1419 }
1420
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001421 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001422 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001423 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001424 goto error;
1425 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001426
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001427 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1428 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001429 {
1430 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001431 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001432 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001433 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001434 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001435 "error encountered on pci register msix handler 0");
1436 goto error;
1437 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001438
1439 if ((error =
1440 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1441 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001442 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001443 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001444 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001445 "error encountered on pci register msix handler 1");
1446 goto error;
1447 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001448
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001449 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001450 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001451 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001452 goto error;
1453 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001454 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001455 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001456 }
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001457 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001458 {
1459 /*
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001460 * WARN: performance will be sub-optimal.
1461 * Fall back to intX, if msix table-size is 1 or
1462 * if UIO driver is being used.
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001463 */
1464 if ((error =
1465 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1466 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001467 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001468 "error encountered on pci register interrupt handler");
1469 goto error;
1470 }
1471 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001472 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001473 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001474
1475 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001476 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001477 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001478 goto error;
1479 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001480
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001481 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001482 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001483 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001484 goto error;
1485 }
1486
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001487 /* create interface */
Damjan Marion5c954c42022-01-06 20:36:14 +01001488 vnet_eth_interface_registration_t eir = {};
1489 eir.dev_class_index = virtio_device_class.index;
1490 eir.dev_instance = vif->dev_instance;
1491 eir.address = vif->mac_addr;
1492 eir.cb.flag_change = virtio_pci_flag_change;
1493 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001494
1495 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1496 vif->sw_if_index = sw->sw_if_index;
1497 args->sw_if_index = sw->sw_if_index;
1498
Damjan Marionc48ec5d2022-01-10 19:38:57 +00001499 vnet_hw_if_set_caps (vnm, vif->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001500
1501 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1502 {
1503 error = virtio_set_packet_buffering (vif, args->buffering_size);
1504 if (error)
1505 {
1506 args->rv = VNET_API_ERROR_INIT_FAILED;
1507 virtio_log_error (vif,
1508 "error encountered during packet buffering init");
1509 goto error;
1510 }
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001511 /*
1512 * packet buffering flag needs to be set 1 before calling the
1513 * virtio_pre_input_node_enable but after the successful initialization
1514 * of buffering queues above.
1515 * Packet buffering flag set to 0 if there will be any error during
1516 * buffering initialization.
1517 */
1518 vif->packet_buffering = 1;
1519 virtio_pre_input_node_enable (vm, vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001520 }
1521
Damjan Marion94100532020-11-06 23:25:57 +01001522 virtio_vring_set_rx_queues (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001523 virtio_vring_set_tx_queues (vm, vif);
Damjan Marion94100532020-11-06 23:25:57 +01001524
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001525 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1526 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001527 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1528 VNET_HW_INTERFACE_FLAG_LINK_UP);
1529 }
1530 else
1531 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001532
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001533 virtio_pci_offloads (vm, vif, args->gso_enabled,
1534 args->checksum_offload_enabled);
1535
1536 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1537 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001538 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001539 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1540 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001541 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001542 return;
1543
1544error:
1545 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001546 if (args->rv == 0)
1547 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001548 args->error = error;
1549}
1550
1551int
1552virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1553{
1554 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001555 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001556 u32 i = 0;
1557
1558 if (vif->type != VIRTIO_IF_TYPE_PCI)
1559 return VNET_API_ERROR_INVALID_INTERFACE;
1560
1561 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1562
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001563 if (vif->virtio_pci_func)
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001564 {
1565 for (i = 0; i < vif->max_queue_pairs; i++)
1566 {
1567 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1568 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1569 }
1570
1571 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1572 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1573
1574 vif->virtio_pci_func->device_reset (vm, vif);
1575 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001576
1577 if (vif->hw_if_index)
1578 {
1579 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001580 ethernet_delete_interface (vnm, vif->hw_if_index);
1581 }
1582
1583 vlib_pci_device_close (vm, vif->pci_dev_handle);
1584
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001585 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001586 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001587 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001588 if (vring->used)
1589 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001590 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001591 }
1592 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001593 vlib_physmem_free (vm, vring->desc);
1594 }
1595
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001596 if (vif->packet_buffering)
1597 virtio_pre_input_node_disable (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001598
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001599 vec_foreach_index (i, vif->txq_vrings)
1600 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001601 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001602 if (vring->used)
1603 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001604 virtio_free_buffers (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001605 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001606 vec_free (vring->buffers);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001607 gro_flow_table_free (vring->flow_table);
1608 virtio_vring_buffering_free (vm, vring->buffering);
Steven Luonge5584962019-06-25 13:47:06 -07001609 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001610 vlib_physmem_free (vm, vring->desc);
1611 }
1612
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001613 if (vif->cxq_vring != NULL)
1614 {
1615 u16 last = vif->cxq_vring->last_used_idx;
1616 u16 n_left = vif->cxq_vring->used->idx - last;
1617 while (n_left)
1618 {
1619 last++;
1620 n_left--;
1621 }
1622
1623 vif->cxq_vring->last_used_idx = last;
1624 vlib_physmem_free (vm, vif->cxq_vring->desc);
1625 }
1626
1627 vec_free (vif->rxq_vrings);
1628 vec_free (vif->txq_vrings);
1629 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001630
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001631 clib_error_free (vif->error);
1632 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001633 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001634
1635 return 0;
1636}
1637
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001638int
1639virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1640 int gso_enabled,
1641 int checksum_offload_enabled,
1642 int offloads_disabled)
1643{
1644 if (vif->type != VIRTIO_IF_TYPE_PCI)
1645 return VNET_API_ERROR_INVALID_INTERFACE;
1646
1647 if (gso_enabled)
1648 virtio_pci_offloads (vm, vif, 1, 0);
1649 else if (checksum_offload_enabled)
1650 virtio_pci_offloads (vm, vif, 0, 1);
1651 else if (offloads_disabled)
1652 virtio_pci_offloads (vm, vif, 0, 0);
1653
1654 return 0;
1655}
1656
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001657/*
1658 * fd.io coding-style-patch-verification: ON
1659 *
1660 * Local Variables:
1661 * eval: (c-set-style "gnu")
1662 * End:
1663 */