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