virtio: split gso and checksum offload functionality
[vpp.git] / src / vnet / devices / virtio / virtio.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <net/if.h>
22 #include <linux/if_tun.h>
23 #include <sys/ioctl.h>
24 #include <linux/virtio_net.h>
25 #include <linux/vhost.h>
26 #include <sys/eventfd.h>
27
28 #include <vlib/vlib.h>
29 #include <vlib/pci/pci.h>
30 #include <vlib/unix/unix.h>
31 #include <vnet/ethernet/ethernet.h>
32 #include <vnet/ip/ip4_packet.h>
33 #include <vnet/ip/ip6_packet.h>
34 #include <vnet/devices/virtio/virtio.h>
35 #include <vnet/devices/virtio/pci.h>
36
37 virtio_main_t virtio_main;
38
39 #define _IOCTL(fd,a,...) \
40   if (ioctl (fd, a, __VA_ARGS__) < 0) \
41     { \
42       err = clib_error_return_unix (0, "ioctl(" #a ")"); \
43       goto error; \
44     }
45
46 static clib_error_t *
47 call_read_ready (clib_file_t * uf)
48 {
49   virtio_main_t *nm = &virtio_main;
50   vnet_main_t *vnm = vnet_get_main ();
51   u16 qid = uf->private_data & 0xFFFF;
52   virtio_if_t *vif =
53     vec_elt_at_index (nm->interfaces, uf->private_data >> 16);
54   u64 b;
55
56   CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
57   if ((qid & 1) == 0)
58     vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
59
60   return 0;
61 }
62
63
64 clib_error_t *
65 virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
66 {
67   virtio_vring_t *vring;
68   clib_file_t t = { 0 };
69   int i;
70
71   if (!is_pow2 (sz))
72     return clib_error_return (0, "ring size must be power of 2");
73
74   if (sz > 32768)
75     return clib_error_return (0, "ring size must be 32768 or lower");
76
77   if (sz == 0)
78     sz = 256;
79
80   if (idx % 2)
81     {
82       vlib_thread_main_t *thm = vlib_get_thread_main ();
83       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
84                             CLIB_CACHE_LINE_BYTES);
85       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
86       if (thm->n_vlib_mains > vif->num_txqs)
87         clib_spinlock_init (&vring->lockp);
88     }
89   else
90     {
91       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
92                             CLIB_CACHE_LINE_BYTES);
93       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
94     }
95   i = sizeof (struct vring_desc) * sz;
96   i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
97   vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
98   clib_memset (vring->desc, 0, i);
99
100   i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]);
101   i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
102   vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
103   clib_memset (vring->avail, 0, i);
104   // tell kernel that we don't need interrupt
105   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
106
107   i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem);
108   i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
109   vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
110   clib_memset (vring->used, 0, i);
111
112   vring->queue_id = idx;
113   ASSERT (vring->buffers == 0);
114   vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
115
116   vring->size = sz;
117   vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
118   vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
119   virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
120                     vring->size, vring->call_fd, vring->kick_fd);
121
122   t.read_function = call_read_ready;
123   t.file_descriptor = vring->call_fd;
124   t.private_data = vif->dev_instance << 16 | idx;
125   t.description = format (0, "%U vring %u", format_virtio_device_name,
126                           vif->dev_instance, idx);
127   vring->call_file_index = clib_file_add (&file_main, &t);
128
129   return 0;
130 }
131
132 inline void
133 virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
134 {
135   u16 used = vring->desc_in_use;
136   u16 last = vring->last_used_idx;
137   u16 mask = vring->size - 1;
138
139   while (used)
140     {
141       vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
142       last++;
143       used--;
144     }
145 }
146
147 clib_error_t *
148 virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
149 {
150   virtio_vring_t *vring =
151     vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
152
153   clib_file_del_by_index (&file_main, vring->call_file_index);
154   close (vring->kick_fd);
155   close (vring->call_fd);
156   if (vring->used)
157     {
158       virtio_free_rx_buffers (vm, vring);
159       clib_mem_free (vring->used);
160     }
161   if (vring->desc)
162     clib_mem_free (vring->desc);
163   if (vring->avail)
164     clib_mem_free (vring->avail);
165   vec_free (vring->buffers);
166   return 0;
167 }
168
169 inline void
170 virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
171 {
172   u16 used = vring->desc_in_use;
173   u16 sz = vring->size;
174   u16 mask = sz - 1;
175   u16 last = vring->last_used_idx;
176   u16 n_left = vring->used->idx - last;
177
178   if (n_left == 0)
179     return;
180
181   while (n_left)
182     {
183       struct vring_used_elem *e = &vring->used->ring[last & mask];
184       u16 slot = e->id;
185
186       vlib_buffer_free (vm, &vring->buffers[slot], 1);
187       used--;
188       last++;
189       n_left--;
190     }
191   vring->desc_in_use = used;
192   vring->last_used_idx = last;
193 }
194
195 clib_error_t *
196 virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
197 {
198   virtio_vring_t *vring =
199     vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
200
201   clib_file_del_by_index (&file_main, vring->call_file_index);
202   close (vring->kick_fd);
203   close (vring->call_fd);
204   if (vring->used)
205     {
206       virtio_free_used_desc (vm, vring);
207       clib_mem_free (vring->used);
208     }
209   if (vring->desc)
210     clib_mem_free (vring->desc);
211   if (vring->avail)
212     clib_mem_free (vring->avail);
213   vec_free (vring->buffers);
214   clib_spinlock_free (&vring->lockp);
215   return 0;
216 }
217
218 void
219 virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
220 {
221   vnet_main_t *vnm = vnet_get_main ();
222   u32 thread_index;
223   virtio_vring_t *vring =
224     vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
225   thread_index =
226     vnet_get_device_input_thread_index (vnm, vif->hw_if_index,
227                                         RX_QUEUE_ACCESS (idx));
228   vring->buffer_pool_index =
229     vlib_buffer_pool_get_default_for_numa (vm,
230                                            vlib_mains
231                                            [thread_index]->numa_node);
232 }
233
234 inline void
235 virtio_set_net_hdr_size (virtio_if_t * vif)
236 {
237   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
238       vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
239     vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr_v1);
240   else
241     vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr);
242 }
243
244 inline void
245 virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
246 {
247   u32 i, j, hw_if_index;
248   virtio_if_t *vif;
249   vnet_main_t *vnm = &vnet_main;
250   virtio_main_t *mm = &virtio_main;
251   virtio_vring_t *vring;
252   struct feat_struct
253   {
254     u8 bit;
255     char *str;
256   };
257   struct feat_struct *feat_entry;
258
259   static struct feat_struct feat_array[] = {
260 #define _(s,b) { .str = #s, .bit = b, },
261     foreach_virtio_net_features
262 #undef _
263     {.str = NULL}
264   };
265
266   struct feat_struct *flag_entry;
267   static struct feat_struct flags_array[] = {
268 #define _(b,e,s) { .bit = b, .str = s, },
269     foreach_virtio_if_flag
270 #undef _
271     {.str = NULL}
272   };
273
274   if (!hw_if_indices)
275     return;
276
277   for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
278     {
279       vnet_hw_interface_t *hi =
280         vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
281       vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
282       if (vif->type != type)
283         continue;
284       vlib_cli_output (vm, "Interface: %U (ifindex %d)",
285                        format_vnet_hw_if_index_name, vnm,
286                        hw_if_indices[hw_if_index], vif->hw_if_index);
287       if (type == VIRTIO_IF_TYPE_PCI)
288         {
289           vlib_cli_output (vm, "  PCI Address: %U", format_vlib_pci_addr,
290                            &vif->pci_addr);
291           vlib_cli_output (vm, "  csum-enabled %d",
292                            vif->csum_offload_enabled);
293         }
294       if (type == VIRTIO_IF_TYPE_TAP)
295         {
296           u8 *str = 0;
297           if (vif->host_if_name)
298             vlib_cli_output (vm, "  name \"%s\"", vif->host_if_name);
299           if (vif->net_ns)
300             vlib_cli_output (vm, "  host-ns \"%s\"", vif->net_ns);
301           if (vif->host_mtu_size)
302             vlib_cli_output (vm, "  host-mtu-size \"%d\"",
303                              vif->host_mtu_size);
304
305           vec_foreach_index (i, vif->vhost_fds)
306             str = format (str, " %d", vif->vhost_fds[i]);
307           vlib_cli_output (vm, "  vhost-fds%v", str);
308           vec_free (str);
309           vlib_cli_output (vm, "  tap-fd %d", vif->tap_fd);
310         }
311       vlib_cli_output (vm, "  gso-enabled %d", vif->gso_enabled);
312       vlib_cli_output (vm, "  Mac Address: %U", format_ethernet_address,
313                        vif->mac_addr);
314       vlib_cli_output (vm, "  Device instance: %u", vif->dev_instance);
315       vlib_cli_output (vm, "  flags 0x%x", vif->flags);
316       flag_entry = (struct feat_struct *) &flags_array;
317       while (flag_entry->str)
318         {
319           if (vif->flags & (1ULL << flag_entry->bit))
320             vlib_cli_output (vm, "    %s (%d)", flag_entry->str,
321                              flag_entry->bit);
322           flag_entry++;
323         }
324       if (type == VIRTIO_IF_TYPE_PCI)
325         {
326           device_status (vm, vif);
327         }
328       vlib_cli_output (vm, "  features 0x%lx", vif->features);
329       feat_entry = (struct feat_struct *) &feat_array;
330       while (feat_entry->str)
331         {
332           if (vif->features & (1ULL << feat_entry->bit))
333             vlib_cli_output (vm, "    %s (%d)", feat_entry->str,
334                              feat_entry->bit);
335           feat_entry++;
336         }
337       vlib_cli_output (vm, "  remote-features 0x%lx", vif->remote_features);
338       feat_entry = (struct feat_struct *) &feat_array;
339       while (feat_entry->str)
340         {
341           if (vif->remote_features & (1ULL << feat_entry->bit))
342             vlib_cli_output (vm, "    %s (%d)", feat_entry->str,
343                              feat_entry->bit);
344           feat_entry++;
345         }
346       vlib_cli_output (vm, "  Number of RX Virtqueue  %u", vif->num_rxqs);
347       vlib_cli_output (vm, "  Number of TX Virtqueue  %u", vif->num_txqs);
348       if (vif->cxq_vring != NULL
349           && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
350         vlib_cli_output (vm, "  Number of CTRL Virtqueue 1");
351       vec_foreach_index (i, vif->rxq_vrings)
352       {
353         vring = vec_elt_at_index (vif->rxq_vrings, i);
354         vlib_cli_output (vm, "  Virtqueue (RX) %d", vring->queue_id);
355         vlib_cli_output (vm,
356                          "    qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
357                          vring->size, vring->last_used_idx, vring->desc_next,
358                          vring->desc_in_use);
359         vlib_cli_output (vm,
360                          "    avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
361                          vring->avail->flags, vring->avail->idx,
362                          vring->used->flags, vring->used->idx);
363         if (type == VIRTIO_IF_TYPE_TAP)
364           {
365             vlib_cli_output (vm, "    kickfd %d, callfd %d", vring->kick_fd,
366                              vring->call_fd);
367           }
368         if (show_descr)
369           {
370             vlib_cli_output (vm, "\n  descriptor table:\n");
371             vlib_cli_output (vm,
372                              "   id          addr         len  flags  next      user_addr\n");
373             vlib_cli_output (vm,
374                              "  ===== ================== ===== ====== ===== ==================\n");
375             for (j = 0; j < vring->size; j++)
376               {
377                 struct vring_desc *desc = &vring->desc[j];
378                 vlib_cli_output (vm,
379                                  "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
380                                  j, desc->addr,
381                                  desc->len,
382                                  desc->flags, desc->next, desc->addr);
383               }
384           }
385       }
386       vec_foreach_index (i, vif->txq_vrings)
387       {
388         vring = vec_elt_at_index (vif->txq_vrings, i);
389         vlib_cli_output (vm, "  Virtqueue (TX) %d", vring->queue_id);
390         vlib_cli_output (vm,
391                          "    qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
392                          vring->size, vring->last_used_idx, vring->desc_next,
393                          vring->desc_in_use);
394         vlib_cli_output (vm,
395                          "    avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
396                          vring->avail->flags, vring->avail->idx,
397                          vring->used->flags, vring->used->idx);
398         if (type == VIRTIO_IF_TYPE_TAP)
399           {
400             vlib_cli_output (vm, "    kickfd %d, callfd %d", vring->kick_fd,
401                              vring->call_fd);
402           }
403         if (show_descr)
404           {
405             vlib_cli_output (vm, "\n  descriptor table:\n");
406             vlib_cli_output (vm,
407                              "   id          addr         len  flags  next      user_addr\n");
408             vlib_cli_output (vm,
409                              "  ===== ================== ===== ====== ===== ==================\n");
410             for (j = 0; j < vring->size; j++)
411               {
412                 struct vring_desc *desc = &vring->desc[j];
413                 vlib_cli_output (vm,
414                                  "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
415                                  j, desc->addr,
416                                  desc->len,
417                                  desc->flags, desc->next, desc->addr);
418               }
419           }
420       }
421       if (vif->cxq_vring != NULL
422           && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
423         {
424           vring = vif->cxq_vring;
425           vlib_cli_output (vm, "  Virtqueue (CTRL) %d", vring->queue_id);
426           vlib_cli_output (vm,
427                            "    qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
428                            vring->size, vring->last_used_idx,
429                            vring->desc_next, vring->desc_in_use);
430           vlib_cli_output (vm,
431                            "    avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
432                            vring->avail->flags, vring->avail->idx,
433                            vring->used->flags, vring->used->idx);
434           if (type == VIRTIO_IF_TYPE_TAP)
435             {
436               vlib_cli_output (vm, "    kickfd %d, callfd %d", vring->kick_fd,
437                                vring->call_fd);
438             }
439           if (show_descr)
440             {
441               vlib_cli_output (vm, "\n  descriptor table:\n");
442               vlib_cli_output (vm,
443                                "   id          addr         len  flags  next      user_addr\n");
444               vlib_cli_output (vm,
445                                "  ===== ================== ===== ====== ===== ==================\n");
446               for (j = 0; j < vring->size; j++)
447                 {
448                   struct vring_desc *desc = &vring->desc[j];
449                   vlib_cli_output (vm,
450                                    "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
451                                    j, desc->addr,
452                                    desc->len,
453                                    desc->flags, desc->next, desc->addr);
454                 }
455             }
456         }
457
458     }
459
460 }
461
462 static clib_error_t *
463 virtio_init (vlib_main_t * vm)
464 {
465   virtio_main_t *vim = &virtio_main;
466   clib_error_t *error = 0;
467
468   vim->log_default = vlib_log_register_class ("virtio", 0);
469   vlib_log_debug (vim->log_default, "initialized");
470
471   return error;
472 }
473
474 VLIB_INIT_FUNCTION (virtio_init);
475
476 /*
477  * fd.io coding-style-patch-verification: ON
478  *
479  * Local Variables:
480  * eval: (c-set-style "gnu")
481  * End:
482  */