1e94bbdc0294d482048f2fa4ca3512da674b2349
[vpp.git] / src / vnet / devices / virtio / pci.c
1 /*
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>
18
19 #include <vppinfra/types.h>
20 #include <vlib/vlib.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
23 #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>
27
28 #define PCI_VENDOR_ID_VIRTIO                            0x1af4
29 #define PCI_DEVICE_ID_VIRTIO_NIC                        0x1000
30 /* Doesn't support modern device */
31 #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN                 0x1041
32
33 #define PCI_CAPABILITY_LIST     0x34
34 #define PCI_CAP_ID_VNDR         0x09
35 #define PCI_CAP_ID_MSIX         0x11
36
37 #define PCI_MSIX_ENABLE 0x8000
38
39 static pci_device_id_t virtio_pci_device_ids[] = {
40   {
41    .vendor_id = PCI_VENDOR_ID_VIRTIO,
42    .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
43   {
44    .vendor_id = PCI_VENDOR_ID_VIRTIO,
45    .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
46   {0},
47 };
48
49 static u32
50 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
51                         u32 flags)
52 {
53   return 0;
54 }
55
56 static clib_error_t *
57 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
58 {
59   clib_error_t *error = 0;
60   u16 max_queue_pairs = 1;
61
62   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
63     {
64       max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
65     }
66
67   virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
68   if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
69     return clib_error_return (error, "max queue pair is %x,"
70                               " should be in range [1, 0x8000]",
71                               max_queue_pairs);
72
73   vif->max_queue_pairs = max_queue_pairs;
74   return error;
75 }
76
77 static void
78 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
79 {
80   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
81     vif->virtio_pci_func->set_mac (vm, vif);
82 }
83
84 static u32
85 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
86 {
87   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
88     {
89       vif->virtio_pci_func->get_mac (vm, vif);
90       return 0;
91     }
92   return 1;
93 }
94
95 static u16
96 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
97 {
98   /*
99    * Minimal driver: assumes link is up
100    */
101   u16 status = 1;
102   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
103     status = vif->virtio_pci_func->get_device_status (vm, vif);
104   return status;
105 }
106
107 static void
108 virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
109                               u16 line)
110 {
111   vnet_main_t *vnm = vnet_get_main ();
112   virtio_main_t *vim = &virtio_main;
113   uword pd = vlib_pci_get_private_data (vm, h);
114   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
115   line--;
116   u16 qid = line;
117
118   vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
119 }
120
121 static void
122 virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
123                                u16 line)
124 {
125   vnet_main_t *vnm = vnet_get_main ();
126   virtio_main_t *vim = &virtio_main;
127   uword pd = vlib_pci_get_private_data (vm, h);
128   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
129
130   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
131     {
132       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
133       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
134                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
135     }
136   else
137     {
138       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
139       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
140     }
141 }
142
143 static void
144 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
145 {
146   virtio_main_t *vim = &virtio_main;
147   uword pd = vlib_pci_get_private_data (vm, h);
148   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
149   u8 isr = 0;
150   u16 line = 0;
151
152   isr = vif->virtio_pci_func->get_isr (vm, vif);
153
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)
160     {
161       for (; line < vif->num_rxqs; line++)
162         virtio_pci_irq_queue_handler (vm, h, (line + 1));
163     }
164
165   if (isr & VIRTIO_PCI_ISR_CONFIG)
166     virtio_pci_irq_config_handler (vm, h, line);
167 }
168
169 inline void
170 device_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
197 struct virtio_ctrl_msg
198 {
199   struct virtio_net_ctrl_hdr ctrl;
200   virtio_net_ctrl_ack status;
201   u8 data[1024];
202 };
203
204 static int
205 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
206                           struct virtio_ctrl_msg *data, u32 len)
207 {
208   virtio_vring_t *vring = vif->cxq_vring;
209   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
210   struct virtio_ctrl_msg result;
211   u32 buffer_index;
212   vlib_buffer_t *b;
213   u16 used, next, avail;
214   u16 sz = vring->size;
215   u16 mask = sz - 1;
216
217   used = vring->desc_in_use;
218   next = vring->desc_next;
219   avail = vring->avail->idx;
220   struct vring_desc *d = &vring->desc[next];
221
222   if (vlib_buffer_alloc (vm, &buffer_index, 1))
223     b = vlib_get_buffer (vm, buffer_index);
224   else
225     return VIRTIO_NET_ERR;
226   /*
227    * current_data may not be initialized with 0 and may contain
228    * previous offset.
229    */
230   b->current_data = 0;
231   clib_memcpy (vlib_buffer_get_current (b), data,
232                sizeof (struct virtio_ctrl_msg));
233   d->flags = VRING_DESC_F_NEXT;
234   d->addr = vlib_buffer_get_current_pa (vm, b);
235   d->len = sizeof (struct virtio_net_ctrl_hdr);
236   vring->avail->ring[avail & mask] = next;
237   avail++;
238   next = (next + 1) & mask;
239   d->next = next;
240   used++;
241
242   d = &vring->desc[next];
243   d->flags = VRING_DESC_F_NEXT;
244   d->addr = vlib_buffer_get_current_pa (vm, b) +
245     STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
246   d->len = len;
247   next = (next + 1) & mask;
248   d->next = next;
249   used++;
250
251   d = &vring->desc[next];
252   d->flags = VRING_DESC_F_WRITE;
253   d->addr = vlib_buffer_get_current_pa (vm, b) +
254     STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
255   d->len = sizeof (data->status);
256   next = (next + 1) & mask;
257   used++;
258
259   CLIB_MEMORY_STORE_BARRIER ();
260   vring->avail->idx = avail;
261   vring->desc_next = next;
262   vring->desc_in_use = used;
263
264   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
265     {
266       virtio_kick (vm, vring, vif);
267     }
268
269   u16 last = vring->last_used_idx, n_left = 0;
270   n_left = vring->used->idx - last;
271
272   while (n_left)
273     {
274       struct vring_used_elem *e = &vring->used->ring[last & mask];
275       u16 slot = e->id;
276
277       d = &vring->desc[slot];
278       while (d->flags & VRING_DESC_F_NEXT)
279         {
280           used--;
281           slot = d->next;
282           d = &vring->desc[slot];
283         }
284       used--;
285       last++;
286       n_left--;
287     }
288   vring->desc_in_use = used;
289   vring->last_used_idx = last;
290
291   CLIB_MEMORY_BARRIER ();
292   clib_memcpy (&result, vlib_buffer_get_current (b),
293                sizeof (struct virtio_ctrl_msg));
294   virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
295   status = result.status;
296   vlib_buffer_free (vm, &buffer_index, 1);
297   return status;
298 }
299
300 static int
301 virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
302 {
303   struct virtio_ctrl_msg offload_hdr;
304   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
305
306   offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
307   offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
308   offload_hdr.status = VIRTIO_NET_ERR;
309   u64 offloads = 0ULL;
310   clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
311
312   status =
313     virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
314   virtio_log_debug (vif, "disable offloads");
315   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
316   vif->virtio_pci_func->get_driver_features (vm, vif);
317   return status;
318 }
319
320 static int
321 virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
322 {
323   struct virtio_ctrl_msg csum_offload_hdr;
324   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
325
326   csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
327   csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
328   csum_offload_hdr.status = VIRTIO_NET_ERR;
329   u64 offloads = 0ULL;
330   offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
331   clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
332
333   status =
334     virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
335   virtio_log_debug (vif, "enable checksum offload");
336   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
337   vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
338   return status;
339 }
340
341 static int
342 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
343 {
344   struct virtio_ctrl_msg gso_hdr;
345   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
346
347   gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
348   gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
349   gso_hdr.status = VIRTIO_NET_ERR;
350   u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
351     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
352     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
353   clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
354
355   status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
356   virtio_log_debug (vif, "enable gso");
357   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
358   vif->virtio_pci_func->get_driver_features (vm, vif);
359   return status;
360 }
361
362 static int
363 virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
364                      int csum_offload_enabled)
365 {
366   vnet_main_t *vnm = vnet_get_main ();
367   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
368
369   if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
370       (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
371     {
372       if (gso_enabled
373           && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
374                                VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
375         {
376           if (virtio_pci_enable_gso (vm, vif))
377             {
378               virtio_log_warning (vif, "gso is not enabled");
379             }
380           else
381             {
382               vif->gso_enabled = 1;
383               vif->csum_offload_enabled = 0;
384               hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
385                 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
386             }
387         }
388       else if (csum_offload_enabled
389                && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
390         {
391           if (virtio_pci_enable_checksum_offload (vm, vif))
392             {
393               virtio_log_warning (vif, "checksum offload is not enabled");
394             }
395           else
396             {
397               vif->csum_offload_enabled = 1;
398               vif->gso_enabled = 0;
399               hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
400               hw->flags |=
401                 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
402             }
403         }
404       else
405         {
406           if (virtio_pci_disable_offload (vm, vif))
407             {
408               virtio_log_warning (vif, "offloads are not disabled");
409             }
410           else
411             {
412               vif->csum_offload_enabled = 0;
413               vif->gso_enabled = 0;
414               hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
415                              VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
416             }
417         }
418     }
419
420   return 0;
421 }
422
423 static int
424 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
425                               u16 num_queues)
426 {
427   struct virtio_ctrl_msg mq_hdr;
428   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
429
430   mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
431   mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
432   mq_hdr.status = VIRTIO_NET_ERR;
433   clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
434
435   status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
436   virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
437   return status;
438 }
439
440 static u8
441 virtio_pci_queue_size_valid (u16 qsz)
442 {
443   if (qsz < 64 || qsz > 4096)
444     return 0;
445   if ((qsz % 64) != 0)
446     return 0;
447   return 1;
448 }
449
450 clib_error_t *
451 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
452                                u16 queue_num)
453 {
454   clib_error_t *error = 0;
455   u16 queue_size = 0;
456   virtio_vring_t *vring;
457   struct vring vr;
458   u32 i = 0;
459   void *ptr = NULL;
460
461   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
462   if (!virtio_pci_queue_size_valid (queue_size))
463     clib_warning ("queue size is not valid");
464
465   if (!is_pow2 (queue_size))
466     return clib_error_return (0, "ring size must be power of 2");
467
468   if (queue_size > 32768)
469     return clib_error_return (0, "ring size must be 32768 or lower");
470
471   if (queue_size == 0)
472     queue_size = 256;
473
474   vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
475   vring = vec_elt_at_index (vif->cxq_vring, 0);
476   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
477   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
478   ptr =
479     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
480                                         vif->numa_node);
481   if (!ptr)
482     return vlib_physmem_last_error (vm);
483   clib_memset (ptr, 0, i);
484   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
485   vring->desc = vr.desc;
486   vring->avail = vr.avail;
487   vring->used = vr.used;
488   vring->queue_id = queue_num;
489   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
490
491   ASSERT (vring->buffers == 0);
492
493   vring->size = queue_size;
494   virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
495                     queue_size);
496   vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
497   vring->kick_fd = -1;
498
499   return error;
500 }
501
502 clib_error_t *
503 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
504 {
505   clib_error_t *error = 0;
506   u16 queue_size = 0;
507   virtio_vring_t *vring;
508   struct vring vr;
509   u32 i = 0;
510   void *ptr = NULL;
511
512   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
513   if (!virtio_pci_queue_size_valid (queue_size))
514     clib_warning ("queue size is not valid");
515
516   if (!is_pow2 (queue_size))
517     return clib_error_return (0, "ring size must be power of 2");
518
519   if (queue_size > 32768)
520     return clib_error_return (0, "ring size must be 32768 or lower");
521
522   if (queue_size == 0)
523     queue_size = 256;
524
525   if (queue_num % 2)
526     {
527       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
528                             CLIB_CACHE_LINE_BYTES);
529       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
530       clib_spinlock_init (&vring->lockp);
531     }
532   else
533     {
534       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
535                             CLIB_CACHE_LINE_BYTES);
536       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
537     }
538   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
539   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
540   ptr =
541     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
542                                         vif->numa_node);
543   if (!ptr)
544     return vlib_physmem_last_error (vm);
545   clib_memset (ptr, 0, i);
546   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
547   vring->desc = vr.desc;
548   vring->avail = vr.avail;
549   vring->used = vr.used;
550   vring->queue_id = queue_num;
551   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
552   vring->flow_table = 0;
553
554   ASSERT (vring->buffers == 0);
555   vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
556   if (queue_num % 2)
557     {
558       virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
559                         queue_size);
560       clib_memset_u32 (vring->buffers, ~0, queue_size);
561     }
562   else
563     {
564       virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
565                         queue_size);
566     }
567   vring->size = queue_size;
568   if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
569     return clib_error_return (0, "error in queue address setup");
570
571   vring->kick_fd = -1;
572   return error;
573 }
574
575 static void
576 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
577                            u64 req_features)
578 {
579   /*
580    * if features are not requested
581    * default: all supported features
582    */
583   u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
584     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
585     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
586     | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
587     | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
588     | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
589     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
590     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
591     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
592     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
593     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
594     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
595     | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
596     | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
597     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
598     | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
599     | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
600     | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
601     | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
602
603   if (vif->is_modern)
604     supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
605
606   if (req_features == 0)
607     {
608       req_features = supported_features;
609     }
610
611   vif->features = req_features & vif->remote_features & supported_features;
612
613   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
614     {
615       u16 mtu = 0;
616       mtu = vif->virtio_pci_func->get_mtu (vm, vif);
617
618       if (mtu < 64)
619         vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
620     }
621
622   vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
623   vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
624 }
625
626 void
627 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
628 {
629   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
630 }
631
632 int
633 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
634 {
635   u8 status = 0;
636
637   /*
638    * Reset the device
639    */
640   status = vif->virtio_pci_func->device_reset (vm, vif);
641
642   /*
643    * Set the Acknowledge status bit
644    */
645   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
646
647   /*
648    * Set the Driver status bit
649    */
650   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
651
652   /*
653    * Read the status and verify it
654    */
655   status = vif->virtio_pci_func->get_status (vm, vif);
656   if ((status & VIRTIO_CONFIG_STATUS_ACK)
657       && (status & VIRTIO_CONFIG_STATUS_DRIVER))
658     vif->status = status;
659   else
660     return -1;
661
662   return 0;
663 }
664
665 clib_error_t *
666 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
667 {
668   clib_error_t *error = 0;
669   struct virtio_pci_cap cap;
670   u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
671   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
672
673   if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
674     {
675       virtio_log_error (vif, "error in reading capabilty list position");
676       clib_error_return (error, "error in reading capabilty list position");
677     }
678   while (pos)
679     {
680       if ((error =
681            vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
682                                        sizeof (cap))))
683         {
684           virtio_log_error (vif, "%s [%2x]",
685                             "error in reading the capability at", pos);
686           clib_error_return (error,
687                              "error in reading the capability at [%2x]", pos);
688         }
689
690       if (cap.cap_vndr == PCI_CAP_ID_MSIX)
691         {
692           u16 flags, table_size, table_size_mask = 0x07FF;
693
694           if ((error =
695                vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
696                                            sizeof (flags))))
697             clib_error_return (error,
698                                "error in reading the capability at [%2x]",
699                                pos + 2);
700
701           table_size = flags & table_size_mask;
702           virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
703                             "msix interrupt vector table-size", table_size);
704
705           if (flags & PCI_MSIX_ENABLE)
706             {
707               virtio_log_debug (vif, "msix interrupt enabled");
708               vif->msix_enabled = VIRTIO_MSIX_ENABLED;
709               vif->msix_table_size = table_size;
710             }
711           else
712             {
713               virtio_log_debug (vif, "msix interrupt disabled");
714               vif->msix_enabled = VIRTIO_MSIX_DISABLED;
715               vif->msix_table_size = 0;
716             }
717         }
718
719       if (cap.cap_vndr != PCI_CAP_ID_VNDR)
720         {
721           virtio_log_debug (vif, "[%2x] %s %2x ", pos,
722                             "skipping non VNDR cap id:", cap.cap_vndr);
723           goto next;
724         }
725
726       virtio_log_debug (vif,
727                         "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
728                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
729
730       if (cap.bar >= 1 && cap.bar <= 5)
731         {
732           vif->bar = bar[cap.bar];
733           vif->bar_id = cap.bar;
734         }
735       else
736         return clib_error_return (error, "invalid bar %u", cap.bar);
737
738       switch (cap.cfg_type)
739         {
740         case VIRTIO_PCI_CAP_COMMON_CFG:
741           vif->common_offset = cap.offset;
742           common_cfg = 1;
743           break;
744         case VIRTIO_PCI_CAP_NOTIFY_CFG:
745           if ((error =
746                vlib_pci_read_write_config (vm, h, VLIB_READ,
747                                            pos + sizeof (cap),
748                                            &vif->notify_off_multiplier,
749                                            sizeof
750                                            (vif->notify_off_multiplier))))
751             {
752               virtio_log_error (vif, "notify off multiplier is not given");
753             }
754           else
755             {
756               virtio_log_debug (vif, "notify off multiplier is %u",
757                                 vif->notify_off_multiplier);
758               vif->notify_offset = cap.offset;
759               notify = 1;
760             }
761           break;
762         case VIRTIO_PCI_CAP_DEVICE_CFG:
763           vif->device_offset = cap.offset;
764           dev_cfg = 1;
765           break;
766         case VIRTIO_PCI_CAP_ISR_CFG:
767           vif->isr_offset = cap.offset;
768           isr = 1;
769           break;
770         case VIRTIO_PCI_CAP_PCI_CFG:
771           if (cap.bar == 0)
772             pci_cfg = 1;
773           break;
774         }
775     next:
776       pos = cap.cap_next;
777     }
778
779   if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
780     {
781       vif->virtio_pci_func = &virtio_pci_legacy_func;
782       virtio_log_debug (vif, "legacy virtio pci device found");
783       return error;
784     }
785
786   vif->is_modern = 1;
787   vif->virtio_pci_func = &virtio_pci_modern_func;
788
789   if (!pci_cfg)
790     virtio_log_debug (vif, "modern virtio pci device found");
791
792   virtio_log_debug (vif, "transitional virtio pci device found");
793   return error;
794 }
795
796 static clib_error_t *
797 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
798                         virtio_pci_create_if_args_t * args, void **bar)
799 {
800   clib_error_t *error = 0;
801   vlib_thread_main_t *vtm = vlib_get_thread_main ();
802   u8 status = 0;
803
804   if ((error = virtio_pci_read_caps (vm, vif, bar)))
805     {
806       args->rv = VNET_API_ERROR_UNSUPPORTED;
807       virtio_log_error (vif, "Device is not supported");
808       clib_error_return (error, "Device is not supported");
809     }
810
811   if (virtio_pci_reset_device (vm, vif) < 0)
812     {
813       args->rv = VNET_API_ERROR_INIT_FAILED;
814       virtio_log_error (vif, "Failed to reset the device");
815       clib_error_return (error, "Failed to reset the device");
816     }
817   /*
818    * read device features and negotiate (user) requested features
819    */
820   virtio_pci_read_device_feature (vm, vif);
821   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
822       0)
823     {
824       virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
825                           "support VIRTIO_RING_F_INDIRECT_DESC features");
826     }
827   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
828     {
829       virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
830                           "support VIRTIO_NET_F_MRG_RXBUF features");
831     }
832   virtio_negotiate_features (vm, vif, args->features);
833
834   /*
835    * After FEATURE_OK, driver should not accept new feature bits
836    */
837   vif->virtio_pci_func->set_status (vm, vif,
838                                     VIRTIO_CONFIG_STATUS_FEATURES_OK);
839   status = vif->virtio_pci_func->get_status (vm, vif);
840   if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
841     {
842       args->rv = VNET_API_ERROR_UNSUPPORTED;
843       virtio_log_error (vif,
844                         "error encountered: Device doesn't support requested features");
845       clib_error_return (error, "Device doesn't support requested features");
846     }
847   vif->status = status;
848
849   /*
850    * get or set the mac address
851    */
852   if (virtio_pci_get_mac (vm, vif))
853     {
854       f64 now = vlib_time_now (vm);
855       u32 rnd;
856       rnd = (u32) (now * 1e6);
857       rnd = random_u32 (&rnd);
858
859       memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
860       vif->mac_addr[0] = 2;
861       vif->mac_addr[1] = 0xfe;
862       virtio_pci_set_mac (vm, vif);
863     }
864
865   virtio_set_net_hdr_size (vif);
866
867   /*
868    * Initialize the virtqueues
869    */
870   if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
871     {
872       args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
873       goto err;
874     }
875
876   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
877     {
878       if (vif->msix_table_size <= vif->max_queue_pairs)
879         {
880           virtio_log_error (vif,
881                             "error MSIX lines (%u) <= Number of RXQs (%u)",
882                             vif->msix_table_size, vif->max_queue_pairs);
883           return clib_error_return (error,
884                                     "error MSIX lines (%u) <= Number of RXQs (%u)",
885                                     vif->msix_table_size,
886                                     vif->max_queue_pairs);
887         }
888     }
889
890   for (int i = 0; i < vif->max_queue_pairs; i++)
891     {
892       if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
893         {
894           args->rv = VNET_API_ERROR_INIT_FAILED;
895           virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
896                             RX_QUEUE (i), "initialization");
897           error =
898             clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
899                                RX_QUEUE (i), "initialization");
900           goto err;
901         }
902       else
903         {
904           vif->num_rxqs++;
905         }
906
907       if (i >= vtm->n_vlib_mains)
908         {
909           /*
910            * There is 1:1 mapping between tx queue and vpp worker thread.
911            * tx queue 0 is bind with thread index 0, tx queue 1 on thread
912            * index 1 and so on.
913            * Multiple worker threads can poll same tx queue when number of
914            * workers are more than tx queues. In this case, 1:N mapping
915            * between tx queue and vpp worker thread.
916            */
917           virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
918                             TX_QUEUE (i),
919                             "no VPP worker thread is available");
920           continue;
921         }
922
923       if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
924         {
925           args->rv = VNET_API_ERROR_INIT_FAILED;
926           virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
927                             TX_QUEUE (i), "initialization");
928           error =
929             clib_error_return (error, "%s (%u) %s", "error in txq-queue",
930                                TX_QUEUE (i), "initialization");
931           goto err;
932         }
933       else
934         {
935           vif->num_txqs++;
936         }
937     }
938
939   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
940     {
941       if ((error =
942            virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
943         {
944           virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
945                               vif->max_queue_pairs * 2, "initialization");
946           if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
947             vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
948         }
949     }
950   else
951     {
952       virtio_log_debug (vif, "control queue is not available");
953       vif->cxq_vring = NULL;
954     }
955
956   /*
957    * set the msix interrupts
958    */
959   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
960     {
961       int i, j;
962       if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
963           VIRTIO_MSI_NO_VECTOR)
964         {
965           virtio_log_warning (vif, "config vector 0 is not set");
966         }
967       else
968         {
969           virtio_log_debug (vif, "config msix vector is set at 0");
970         }
971       for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
972         {
973           if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
974                                                    RX_QUEUE (i)) ==
975               VIRTIO_MSI_NO_VECTOR)
976             {
977               virtio_log_warning (vif, "queue (%u) vector is not set at %u",
978                                   RX_QUEUE (i), j);
979             }
980           else
981             {
982               virtio_log_debug (vif, "%s (%u) %s %u", "queue",
983                                 RX_QUEUE (i), "msix vector is set at", j);
984             }
985         }
986     }
987
988   /*
989    * set the driver status OK
990    */
991   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
992   vif->status = vif->virtio_pci_func->get_status (vm, vif);
993 err:
994   return error;
995 }
996
997 void
998 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
999 {
1000   vnet_main_t *vnm = vnet_get_main ();
1001   virtio_main_t *vim = &virtio_main;
1002   virtio_if_t *vif;
1003   vlib_pci_dev_handle_t h;
1004   clib_error_t *error = 0;
1005   u32 interrupt_count = 0;
1006
1007   /* *INDENT-OFF* */
1008   pool_foreach (vif, vim->interfaces, ({
1009     if (vif->pci_addr.as_u32 == args->addr)
1010       {
1011         args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
1012         args->error =
1013           clib_error_return (error, "PCI address in use");
1014           vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1015                 format_vlib_pci_addr, &args->addr,
1016                 " PCI address in use");
1017         return;
1018       }
1019   }));
1020   /* *INDENT-ON* */
1021
1022   pool_get (vim->interfaces, vif);
1023   vif->dev_instance = vif - vim->interfaces;
1024   vif->per_interface_next_index = ~0;
1025   vif->pci_addr.as_u32 = args->addr;
1026
1027   if ((error =
1028        vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1029                              virtio_pci_device_ids, &h)))
1030     {
1031       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1032       args->error =
1033         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1034                            &vif->pci_addr);
1035       vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1036                 format_vlib_pci_addr, &vif->pci_addr,
1037                 "error encountered on pci device open");
1038       pool_put (vim->interfaces, vif);
1039       return;
1040     }
1041   vif->pci_dev_handle = h;
1042   vlib_pci_set_private_data (vm, h, vif->dev_instance);
1043   vif->numa_node = vlib_pci_get_numa_node (vm, h);
1044   vif->type = VIRTIO_IF_TYPE_PCI;
1045
1046   if ((error = vlib_pci_bus_master_enable (vm, h)))
1047     {
1048       virtio_log_error (vif, "error encountered on pci bus master enable");
1049       goto error;
1050     }
1051
1052   void *bar[6];
1053   for (u32 i = 0; i <= 5; i++)
1054     {
1055
1056       if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1057         {
1058           virtio_log_debug (vif, "no pci map region for bar %u", i);
1059         }
1060       else
1061         {
1062           virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1063                             bar[i]);
1064         }
1065     }
1066
1067   if ((error = vlib_pci_io_region (vm, h, 0)))
1068     {
1069       virtio_log_error (vif, "error encountered on pci io region");
1070       goto error;
1071     }
1072
1073   interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1074   if (interrupt_count > 1)
1075     {
1076       if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1077                                                    &virtio_pci_irq_config_handler)))
1078         {
1079           args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1080           virtio_log_error (vif,
1081                             "error encountered on pci register msix handler 0");
1082           goto error;
1083         }
1084
1085       if ((error =
1086            vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1087                                            &virtio_pci_irq_queue_handler)))
1088         {
1089           args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1090           virtio_log_error (vif,
1091                             "error encountered on pci register msix handler 1");
1092           goto error;
1093         }
1094
1095       if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
1096         {
1097           virtio_log_error (vif, "error encountered on pci enable msix irq");
1098           goto error;
1099         }
1100       vif->support_int_mode = 1;
1101       virtio_log_debug (vif, "device supports msix interrupts");
1102     }
1103   else if (interrupt_count == 1)
1104     {
1105       /*
1106        * if msix table-size is 1, fall back to intX.
1107        */
1108       if ((error =
1109            vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1110         {
1111           virtio_log_error (vif,
1112                             "error encountered on pci register interrupt handler");
1113           goto error;
1114         }
1115       vif->support_int_mode = 1;
1116       virtio_log_debug (vif, "pci register interrupt handler");
1117     }
1118   else
1119     {
1120       /*
1121        * WARN: intX is showing some weird behaviour.
1122        * Please don't use interrupt mode with UIO driver.
1123        */
1124       vif->support_int_mode = 0;
1125       virtio_log_debug (vif, "driver is configured in poll mode only");
1126     }
1127
1128   if ((error = vlib_pci_intr_enable (vm, h)))
1129     {
1130       virtio_log_error (vif, "error encountered on pci interrupt enable");
1131       goto error;
1132     }
1133
1134   if ((error = virtio_pci_device_init (vm, vif, args, bar)))
1135     {
1136       virtio_log_error (vif, "error encountered on device init");
1137       goto error;
1138     }
1139
1140   /* create interface */
1141   error = ethernet_register_interface (vnm, virtio_device_class.index,
1142                                        vif->dev_instance, vif->mac_addr,
1143                                        &vif->hw_if_index,
1144                                        virtio_pci_flag_change);
1145
1146   if (error)
1147     {
1148       args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1149       virtio_log_error (vif,
1150                         "error encountered on ethernet register interface");
1151       goto error;
1152     }
1153
1154   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1155   vif->sw_if_index = sw->sw_if_index;
1156   args->sw_if_index = sw->sw_if_index;
1157
1158   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1159   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1160   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1161                                     virtio_input_node.index);
1162   u32 i = 0;
1163   vec_foreach_index (i, vif->rxq_vrings)
1164   {
1165     vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1166     virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1167     /* Set default rx mode to POLLING */
1168     vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1169                                    VNET_HW_INTERFACE_RX_MODE_POLLING);
1170   }
1171   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1172     {
1173       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1174       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1175                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1176     }
1177   else
1178     vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1179
1180   virtio_pci_offloads (vm, vif, args->gso_enabled,
1181                        args->checksum_offload_enabled);
1182
1183   if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1184       (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1185     {
1186       if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1187         virtio_log_warning (vif, "multiqueue is not set");
1188     }
1189   return;
1190
1191 error:
1192   virtio_pci_delete_if (vm, vif);
1193   if (args->rv == 0)
1194     args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1195   args->error = error;
1196 }
1197
1198 int
1199 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1200 {
1201   vnet_main_t *vnm = vnet_get_main ();
1202   virtio_main_t *vim = &virtio_main;
1203   u32 i = 0;
1204
1205   if (vif->type != VIRTIO_IF_TYPE_PCI)
1206     return VNET_API_ERROR_INVALID_INTERFACE;
1207
1208   vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1209
1210   for (i = 0; i < vif->max_queue_pairs; i++)
1211     {
1212       vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1213       vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1214     }
1215
1216   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1217     vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1218
1219   vif->virtio_pci_func->device_reset (vm, vif);
1220
1221   if (vif->hw_if_index)
1222     {
1223       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1224       vec_foreach_index (i, vif->rxq_vrings)
1225       {
1226         vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1227       }
1228       ethernet_delete_interface (vnm, vif->hw_if_index);
1229     }
1230
1231   vlib_pci_device_close (vm, vif->pci_dev_handle);
1232
1233   vec_foreach_index (i, vif->rxq_vrings)
1234   {
1235     virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1236     if (vring->kick_fd != -1)
1237       close (vring->kick_fd);
1238     if (vring->used)
1239       {
1240         virtio_free_rx_buffers (vm, vring);
1241       }
1242     vec_free (vring->buffers);
1243     vlib_physmem_free (vm, vring->desc);
1244   }
1245
1246   vec_foreach_index (i, vif->txq_vrings)
1247   {
1248     virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1249     if (vring->kick_fd != -1)
1250       close (vring->kick_fd);
1251     if (vring->used)
1252       {
1253         virtio_free_used_desc (vm, vring);
1254       }
1255     vec_free (vring->buffers);
1256     clib_spinlock_free (&vring->lockp);
1257     vlib_physmem_free (vm, vring->desc);
1258   }
1259
1260   if (vif->cxq_vring != NULL)
1261     {
1262       u16 last = vif->cxq_vring->last_used_idx;
1263       u16 n_left = vif->cxq_vring->used->idx - last;
1264       while (n_left)
1265         {
1266           last++;
1267           n_left--;
1268         }
1269
1270       vif->cxq_vring->last_used_idx = last;
1271       vlib_physmem_free (vm, vif->cxq_vring->desc);
1272     }
1273
1274   vec_free (vif->rxq_vrings);
1275   vec_free (vif->txq_vrings);
1276   vec_free (vif->cxq_vring);
1277
1278   clib_error_free (vif->error);
1279   memset (vif, 0, sizeof (*vif));
1280   pool_put (vim->interfaces, vif);
1281
1282   return 0;
1283 }
1284
1285 int
1286 virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1287                                     int gso_enabled,
1288                                     int checksum_offload_enabled,
1289                                     int offloads_disabled)
1290 {
1291   if (vif->type != VIRTIO_IF_TYPE_PCI)
1292     return VNET_API_ERROR_INVALID_INTERFACE;
1293
1294   if (gso_enabled)
1295     virtio_pci_offloads (vm, vif, 1, 0);
1296   else if (checksum_offload_enabled)
1297     virtio_pci_offloads (vm, vif, 0, 1);
1298   else if (offloads_disabled)
1299     virtio_pci_offloads (vm, vif, 0, 0);
1300
1301   return 0;
1302 }
1303
1304 /*
1305  * fd.io coding-style-patch-verification: ON
1306  *
1307  * Local Variables:
1308  * eval: (c-set-style "gnu")
1309  * End:
1310  */