blob: 85e6abdbbe56fd7608ff713bb9ece55cbffc1c97 [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 *
724virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
725 u16 queue_num)
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 Kazmi09a3bc52019-04-02 11:45:08 +0000748 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
749 CLIB_CACHE_LINE_BYTES);
750 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000751 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000752 }
753 else
754 {
755 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
756 CLIB_CACHE_LINE_BYTES);
757 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
758 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000759 i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN);
760 i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN);
761 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
762 vif->numa_node);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000763 if (!ptr)
764 return vlib_physmem_last_error (vm);
765 clib_memset (ptr, 0, i);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000766 vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000767 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200768 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200769 vring->flow_table = 0;
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000770 vring->total_packets = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200771
772 ASSERT (vring->buffers == 0);
773 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000774 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200775 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100776 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000777 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100778 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200779 }
780 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000781 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100782 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000783 queue_size);
784 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000785 vring->queue_size = queue_size;
786 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200787 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200788
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000789 vring->queue_notify_offset =
790 vif->notify_off_multiplier *
791 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
792 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
793 queue_num, vring->queue_notify_offset);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200794 return error;
795}
796
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100797clib_error_t *
798virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
799 u16 queue_num)
800{
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100801 clib_error_t *error = 0;
802 u16 queue_size = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000803 vnet_virtio_vring_t *vring;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100804 u32 i = 0;
805 void *ptr = NULL;
806
807 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
808
809 if (queue_size > 32768)
810 return clib_error_return (0, "ring size must be 32768 or lower");
811
812 if (queue_size == 0)
813 queue_size = 256;
814
815 if (queue_num % 2)
816 {
817 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
818 CLIB_CACHE_LINE_BYTES);
819 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000820 clib_spinlock_init (&vring->lockp);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100821 }
822 else
823 {
824 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
825 CLIB_CACHE_LINE_BYTES);
826 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
827 }
828
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000829 i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
830 sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN -
831 1) &
832 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) +
833 sizeof (vnet_virtio_vring_desc_event_t);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100834
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000835 ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN,
836 vif->numa_node);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100837 if (!ptr)
838 return vlib_physmem_last_error (vm);
839
840 clib_memset (ptr, 0, i);
841 vring->packed_desc = ptr;
842
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000843 vring->driver_event =
844 ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100845 vring->driver_event->off_wrap = 0;
846 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
847
848 vring->device_event =
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000849 ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) +
850 sizeof (vnet_virtio_vring_desc_event_t) +
851 VNET_VIRTIO_PCI_VRING_ALIGN - 1) &
852 ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1));
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100853 vring->device_event->off_wrap = 0;
854 vring->device_event->flags = 0;
855
856 vring->queue_id = queue_num;
857
858 vring->avail_wrap_counter = 1;
859 vring->used_wrap_counter = 1;
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000860 vring->total_packets = 0;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100861
862 ASSERT (vring->buffers == 0);
863 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
864 if (queue_num % 2)
865 {
866 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
867 queue_size);
868 clib_memset_u32 (vring->buffers, ~0, queue_size);
869 }
870 else
871 {
872 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
873 queue_size);
874 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000875 vring->queue_size = queue_size;
876 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring))
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100877 return clib_error_return (0, "error in queue address setup");
878
879 vring->queue_notify_offset =
880 vif->notify_off_multiplier *
881 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
882 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
883 queue_num, vring->queue_notify_offset);
884
885 return error;
886}
887
888clib_error_t *
889virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
890{
891 if (vif->is_packed)
892 return virtio_pci_vring_packed_init (vm, vif, queue_num);
893 else
894 return virtio_pci_vring_split_init (vm, vif, queue_num);
895}
896
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200897static void
898virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
899 u64 req_features)
900{
901 /*
902 * if features are not requested
903 * default: all supported features
904 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200905 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
906 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
907 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
908 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200909 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200910 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
911 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
912 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
913 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
914 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
915 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
916 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200917 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
918 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000919 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
920 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000921 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200922 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
923 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
924
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200925 if (vif->is_modern)
926 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
927
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100928 if (vif->is_packed)
929 {
930 supported_features |=
931 (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
932 VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
933 }
934
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200935 if (req_features == 0)
936 {
937 req_features = supported_features;
938 }
939
940 vif->features = req_features & vif->remote_features & supported_features;
941
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000942 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200943 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000944 u16 mtu = 0;
945 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
946
947 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200948 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
949 }
950
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100951 if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
952 vif->is_packed = 0;
953
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000954 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
955 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200956}
957
958void
959virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
960{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000961 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200962}
963
964int
965virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
966{
967 u8 status = 0;
968
969 /*
970 * Reset the device
971 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000972 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200973
974 /*
975 * Set the Acknowledge status bit
976 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000977 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200978
979 /*
980 * Set the Driver status bit
981 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000982 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200983
984 /*
985 * Read the status and verify it
986 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000987 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200988 if ((status & VIRTIO_CONFIG_STATUS_ACK)
989 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
990 vif->status = status;
991 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200992 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200993
994 return 0;
995}
996
997clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200998virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200999{
1000 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001001 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001002 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001003 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1004
1005 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001006 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001007 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001008 return clib_error_return (error,
1009 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001010 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001011 while (pos)
1012 {
1013 if ((error =
1014 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1015 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001016 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001017 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001018 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001019 return clib_error_return (error,
1020 "error in reading the capability at [%2x]",
1021 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001022 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001023
1024 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1025 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001026 u16 flags, table_size, table_size_mask = 0x07FF;
1027
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001028 if ((error =
1029 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1030 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001031 return clib_error_return (error,
1032 "error in reading the capability at [%2x]",
1033 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001034
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001035 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +01001036 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001037 "msix interrupt vector table-size", table_size);
1038
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001039 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001040 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001041 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001042 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001043 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001044 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001045 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001046 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001047 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001048 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001049 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001050 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001051 }
1052
1053 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1054 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001055 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001056 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001057 goto next;
1058 }
1059
Damjan Marionf41244f2019-11-08 17:41:06 +01001060 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001061 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1062 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001063
Mohsin Kazmi0a507d72020-09-01 18:18:48 +02001064 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +02001065 {
1066 vif->bar = bar[cap.bar];
1067 vif->bar_id = cap.bar;
1068 }
1069 else
1070 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001071
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001072 switch (cap.cfg_type)
1073 {
1074 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001075 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001076 common_cfg = 1;
1077 break;
1078 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001079 if ((error =
1080 vlib_pci_read_write_config (vm, h, VLIB_READ,
1081 pos + sizeof (cap),
1082 &vif->notify_off_multiplier,
1083 sizeof
1084 (vif->notify_off_multiplier))))
1085 {
1086 virtio_log_error (vif, "notify off multiplier is not given");
1087 }
1088 else
1089 {
1090 virtio_log_debug (vif, "notify off multiplier is %u",
1091 vif->notify_off_multiplier);
1092 vif->notify_offset = cap.offset;
1093 notify = 1;
1094 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001095 break;
1096 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001097 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001098 dev_cfg = 1;
1099 break;
1100 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001101 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001102 isr = 1;
1103 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001104 case VIRTIO_PCI_CAP_PCI_CFG:
1105 if (cap.bar == 0)
1106 pci_cfg = 1;
1107 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001108 }
1109 next:
1110 pos = cap.cap_next;
1111 }
1112
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001113 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001114 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001115 vif->virtio_pci_func = &virtio_pci_legacy_func;
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001116 vif->notify_off_multiplier = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001117 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001118 return error;
1119 }
1120
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001121 vif->is_modern = 1;
1122 vif->virtio_pci_func = &virtio_pci_modern_func;
1123
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001124 if (!pci_cfg)
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001125 {
1126 virtio_log_debug (vif, "modern virtio pci device found");
1127 }
1128 else
1129 {
1130 virtio_log_debug (vif, "transitional virtio pci device found");
1131 }
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001132
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001133 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001134}
1135
1136static clib_error_t *
1137virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001138 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001139{
1140 clib_error_t *error = 0;
1141 u8 status = 0;
1142
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001143 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001144 {
1145 args->rv = VNET_API_ERROR_UNSUPPORTED;
1146 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001147 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001148 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001149
1150 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001151 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001152 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001153 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001154 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001155 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001156 /*
1157 * read device features and negotiate (user) requested features
1158 */
1159 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +00001160 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1161 0)
1162 {
1163 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1164 "support VIRTIO_RING_F_INDIRECT_DESC features");
1165 }
1166 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1167 {
1168 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1169 "support VIRTIO_NET_F_MRG_RXBUF features");
1170 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001171 virtio_negotiate_features (vm, vif, args->features);
1172
1173 /*
1174 * After FEATURE_OK, driver should not accept new feature bits
1175 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001176 vif->virtio_pci_func->set_status (vm, vif,
1177 VIRTIO_CONFIG_STATUS_FEATURES_OK);
1178 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001179 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001180 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001181 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001182 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001183 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001184 return clib_error_return (error,
1185 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001186 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001187 vif->status = status;
1188
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001189 /*
1190 * get or set the mac address
1191 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001192 if (virtio_pci_get_mac (vm, vif))
1193 {
1194 f64 now = vlib_time_now (vm);
1195 u32 rnd;
1196 rnd = (u32) (now * 1e6);
1197 rnd = random_u32 (&rnd);
1198
1199 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1200 vif->mac_addr[0] = 2;
1201 vif->mac_addr[1] = 0xfe;
1202 virtio_pci_set_mac (vm, vif);
1203 }
1204
1205 virtio_set_net_hdr_size (vif);
1206
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001207 /*
1208 * Initialize the virtqueues
1209 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001210 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001211 {
1212 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1213 goto err;
1214 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001215
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001216 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1217 {
1218 if (vif->msix_table_size <= vif->max_queue_pairs)
1219 {
1220 virtio_log_error (vif,
1221 "error MSIX lines (%u) <= Number of RXQs (%u)",
1222 vif->msix_table_size, vif->max_queue_pairs);
1223 return clib_error_return (error,
1224 "error MSIX lines (%u) <= Number of RXQs (%u)",
1225 vif->msix_table_size,
1226 vif->max_queue_pairs);
1227 }
1228 }
1229
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001230 for (int i = 0; i < vif->max_queue_pairs; i++)
1231 {
1232 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001233 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001234 args->rv = VNET_API_ERROR_INIT_FAILED;
1235 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1236 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001237 error =
1238 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1239 RX_QUEUE (i), "initialization");
1240 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001241 }
1242 else
1243 {
1244 vif->num_rxqs++;
1245 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001246
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001247 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001248 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001249 args->rv = VNET_API_ERROR_INIT_FAILED;
1250 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1251 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001252 error =
1253 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1254 TX_QUEUE (i), "initialization");
1255 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001256 }
1257 else
1258 {
1259 vif->num_txqs++;
1260 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001261 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001262
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001263 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1264 {
1265 if ((error =
1266 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1267 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001268 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001269 vif->max_queue_pairs * 2, "initialization");
1270 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1271 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1272 }
1273 }
1274 else
1275 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001276 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001277 vif->cxq_vring = NULL;
1278 }
1279
1280 /*
1281 * set the msix interrupts
1282 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001283 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001284 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001285 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001286 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001287 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001288 {
1289 virtio_log_warning (vif, "config vector 0 is not set");
1290 }
1291 else
1292 {
1293 virtio_log_debug (vif, "config msix vector is set at 0");
1294 }
1295 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1296 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001297 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1298 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001299 VIRTIO_MSI_NO_VECTOR)
1300 {
1301 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1302 RX_QUEUE (i), j);
1303 }
1304 else
1305 {
1306 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1307 RX_QUEUE (i), "msix vector is set at", j);
1308 }
1309 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001310 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001311
1312 /*
1313 * set the driver status OK
1314 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001315 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1316 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001317err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001318 return error;
1319}
1320
1321void
1322virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1323{
1324 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001325 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001326 virtio_if_t *vif;
1327 vlib_pci_dev_handle_t h;
1328 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001329 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001330
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001331 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001332 pool_foreach (vif, vim->interfaces) {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001333 if (vif->pci_addr.as_u32 == args->addr)
1334 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001335 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001336 args->error =
1337 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001338 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1339 format_vlib_pci_addr, &args->addr,
1340 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001341 return;
1342 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001343 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001344 /* *INDENT-ON* */
1345
Benoît Gannec04d8c42022-10-13 14:01:03 +02001346 if (args->bind)
1347 {
1348 vlib_pci_addr_t pci = { .as_u32 = args->addr };
1349 error = vlib_pci_bind_to_uio (vm, &pci, (char *) "auto",
1350 VIRTIO_BIND_FORCE == args->bind);
1351 if (error)
1352 {
1353 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1354 args->error =
1355 clib_error_return (error, "%U: %s", format_vlib_pci_addr, &pci,
1356 "error encountered on binding pci device");
1357 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1358 format_vlib_pci_addr, &pci,
1359 "error encountered on binding pci devicee");
1360 return;
1361 }
1362 }
1363
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001364 pool_get (vim->interfaces, vif);
1365 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001366 vif->per_interface_next_index = ~0;
1367 vif->pci_addr.as_u32 = args->addr;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001368 if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1369 vif->is_packed = 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001370
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001371 if ((error =
1372 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1373 virtio_pci_device_ids, &h)))
1374 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001375 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1376 args->error =
1377 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1378 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001379 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1380 format_vlib_pci_addr, &vif->pci_addr,
1381 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001382 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001383 return;
1384 }
1385 vif->pci_dev_handle = h;
1386 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001387 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001388 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001389
1390 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001391 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001392 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001393 goto error;
1394 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001395
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001396 void *bar[6];
1397 for (u32 i = 0; i <= 5; i++)
1398 {
1399
1400 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1401 {
1402 virtio_log_debug (vif, "no pci map region for bar %u", i);
1403 }
1404 else
1405 {
1406 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1407 bar[i]);
1408 }
1409 }
1410
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001411 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001412 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001413 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001414 goto error;
1415 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001416
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001417 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1418 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001419 {
1420 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001421 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001422 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001423 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001424 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001425 "error encountered on pci register msix handler 0");
1426 goto error;
1427 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001428
1429 if ((error =
1430 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1431 &virtio_pci_irq_queue_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 1");
1436 goto error;
1437 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001438
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001439 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001440 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001441 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001442 goto error;
1443 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001444 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001445 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001446 }
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001447 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001448 {
1449 /*
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001450 * WARN: performance will be sub-optimal.
1451 * Fall back to intX, if msix table-size is 1 or
1452 * if UIO driver is being used.
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001453 */
1454 if ((error =
1455 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1456 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001457 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001458 "error encountered on pci register interrupt handler");
1459 goto error;
1460 }
1461 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001462 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001463 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001464
1465 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001466 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001467 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001468 goto error;
1469 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001470
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001471 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001472 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001473 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001474 goto error;
1475 }
1476
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001477 /* create interface */
Damjan Marion5c954c42022-01-06 20:36:14 +01001478 vnet_eth_interface_registration_t eir = {};
1479 eir.dev_class_index = virtio_device_class.index;
1480 eir.dev_instance = vif->dev_instance;
1481 eir.address = vif->mac_addr;
1482 eir.cb.flag_change = virtio_pci_flag_change;
1483 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001484
1485 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1486 vif->sw_if_index = sw->sw_if_index;
1487 args->sw_if_index = sw->sw_if_index;
1488
Damjan Marionc48ec5d2022-01-10 19:38:57 +00001489 vnet_hw_if_set_caps (vnm, vif->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001490
1491 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1492 {
1493 error = virtio_set_packet_buffering (vif, args->buffering_size);
1494 if (error)
1495 {
1496 args->rv = VNET_API_ERROR_INIT_FAILED;
1497 virtio_log_error (vif,
1498 "error encountered during packet buffering init");
1499 goto error;
1500 }
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001501 /*
1502 * packet buffering flag needs to be set 1 before calling the
1503 * virtio_pre_input_node_enable but after the successful initialization
1504 * of buffering queues above.
1505 * Packet buffering flag set to 0 if there will be any error during
1506 * buffering initialization.
1507 */
1508 vif->packet_buffering = 1;
1509 virtio_pre_input_node_enable (vm, vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001510 }
1511
Damjan Marion94100532020-11-06 23:25:57 +01001512 virtio_vring_set_rx_queues (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001513 virtio_vring_set_tx_queues (vm, vif);
Damjan Marion94100532020-11-06 23:25:57 +01001514
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001515 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1516 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001517 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1518 VNET_HW_INTERFACE_FLAG_LINK_UP);
1519 }
1520 else
1521 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001522
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001523 virtio_pci_offloads (vm, vif, args->gso_enabled,
1524 args->checksum_offload_enabled);
1525
1526 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1527 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001528 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001529 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1530 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001531 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001532 return;
1533
1534error:
1535 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001536 if (args->rv == 0)
1537 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001538 args->error = error;
1539}
1540
1541int
1542virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1543{
1544 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001545 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001546 u32 i = 0;
1547
1548 if (vif->type != VIRTIO_IF_TYPE_PCI)
1549 return VNET_API_ERROR_INVALID_INTERFACE;
1550
1551 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1552
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001553 if (vif->virtio_pci_func)
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001554 {
1555 for (i = 0; i < vif->max_queue_pairs; i++)
1556 {
1557 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1558 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1559 }
1560
1561 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1562 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1563
1564 vif->virtio_pci_func->device_reset (vm, vif);
1565 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001566
1567 if (vif->hw_if_index)
1568 {
1569 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001570 ethernet_delete_interface (vnm, vif->hw_if_index);
1571 }
1572
1573 vlib_pci_device_close (vm, vif->pci_dev_handle);
1574
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001575 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001576 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001577 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001578 if (vring->used)
1579 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001580 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001581 }
1582 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001583 vlib_physmem_free (vm, vring->desc);
1584 }
1585
Mohsin Kazmi562ac2f2023-05-24 11:25:39 +00001586 if (vif->packet_buffering)
1587 virtio_pre_input_node_disable (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001588
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001589 vec_foreach_index (i, vif->txq_vrings)
1590 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001591 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001592 if (vring->used)
1593 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001594 virtio_free_buffers (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001595 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001596 vec_free (vring->buffers);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001597 gro_flow_table_free (vring->flow_table);
1598 virtio_vring_buffering_free (vm, vring->buffering);
Steven Luonge5584962019-06-25 13:47:06 -07001599 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001600 vlib_physmem_free (vm, vring->desc);
1601 }
1602
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001603 if (vif->cxq_vring != NULL)
1604 {
1605 u16 last = vif->cxq_vring->last_used_idx;
1606 u16 n_left = vif->cxq_vring->used->idx - last;
1607 while (n_left)
1608 {
1609 last++;
1610 n_left--;
1611 }
1612
1613 vif->cxq_vring->last_used_idx = last;
1614 vlib_physmem_free (vm, vif->cxq_vring->desc);
1615 }
1616
1617 vec_free (vif->rxq_vrings);
1618 vec_free (vif->txq_vrings);
1619 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001620
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001621 clib_error_free (vif->error);
1622 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001623 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001624
1625 return 0;
1626}
1627
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001628int
1629virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1630 int gso_enabled,
1631 int checksum_offload_enabled,
1632 int offloads_disabled)
1633{
1634 if (vif->type != VIRTIO_IF_TYPE_PCI)
1635 return VNET_API_ERROR_INVALID_INTERFACE;
1636
1637 if (gso_enabled)
1638 virtio_pci_offloads (vm, vif, 1, 0);
1639 else if (checksum_offload_enabled)
1640 virtio_pci_offloads (vm, vif, 0, 1);
1641 else if (offloads_disabled)
1642 virtio_pci_offloads (vm, vif, 0, 0);
1643
1644 return 0;
1645}
1646
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001647/*
1648 * fd.io coding-style-patch-verification: ON
1649 *
1650 * Local Variables:
1651 * eval: (c-set-style "gnu")
1652 * End:
1653 */