76f85588ec8e7f55594affb4fe70c7fb7f73161f
[vpp.git] / src / vnet / devices / virtio / device.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/gso/gso.h>
26 #include <vnet/ip/ip4_packet.h>
27 #include <vnet/ip/ip6_packet.h>
28 #include <vnet/tcp/tcp_packet.h>
29 #include <vnet/udp/udp_packet.h>
30 #include <vnet/devices/virtio/virtio.h>
31
32 #define foreach_virtio_tx_func_error           \
33 _(NO_FREE_SLOTS, "no free tx slots")           \
34 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
35 _(PENDING_MSGS, "pending msgs in tx ring") \
36 _(NO_TX_QUEUES, "no tx queues") \
37 _(OUT_OF_ORDER, "out-of-order buffers in used ring")
38
39 typedef enum
40 {
41 #define _(f,s) VIRTIO_TX_ERROR_##f,
42   foreach_virtio_tx_func_error
43 #undef _
44     VIRTIO_TX_N_ERROR,
45 } virtio_tx_func_error_t;
46
47 static char *virtio_tx_func_error_strings[] = {
48 #define _(n,s) s,
49   foreach_virtio_tx_func_error
50 #undef _
51 };
52
53 static u8 *
54 format_virtio_device (u8 * s, va_list * args)
55 {
56   u32 dev_instance = va_arg (*args, u32);
57   int verbose = va_arg (*args, int);
58   u32 indent = format_get_indent (s);
59
60   s = format (s, "VIRTIO interface");
61   if (verbose)
62     {
63       s = format (s, "\n%U instance %u", format_white_space, indent + 2,
64                   dev_instance);
65     }
66   return s;
67 }
68
69 static u8 *
70 format_virtio_tx_trace (u8 * s, va_list * args)
71 {
72   s = format (s, "Unimplemented...");
73   return s;
74 }
75
76 static_always_inline void
77 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
78 {
79   ASSERT (n_buffers <= ring_size);
80
81   if (PREDICT_TRUE (start + n_buffers <= ring_size))
82     {
83       clib_memset_u32 (ring + start, ~0, n_buffers);
84     }
85   else
86     {
87       clib_memset_u32 (ring + start, ~0, ring_size - start);
88       clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
89     }
90 }
91
92 static_always_inline void
93 virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
94                               uword node_index)
95 {
96   u16 used = vring->desc_in_use;
97   u16 sz = vring->size;
98   u16 mask = sz - 1;
99   u16 last = vring->last_used_idx;
100   u16 n_left = vring->used->idx - last;
101   u16 out_of_order_count = 0;
102
103   if (n_left == 0)
104     return;
105
106   while (n_left)
107     {
108       struct vring_used_elem *e = &vring->used->ring[last & mask];
109       u16 slot, n_buffers;
110       slot = n_buffers = e->id;
111
112       while (e->id == (n_buffers & mask))
113         {
114           n_left--;
115           last++;
116           n_buffers++;
117           if (n_left == 0)
118             break;
119           e = &vring->used->ring[last & mask];
120         }
121       vlib_buffer_free_from_ring (vm, vring->buffers, slot,
122                                   sz, (n_buffers - slot));
123       virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
124       used -= (n_buffers - slot);
125
126       if (n_left > 0)
127         {
128           vlib_buffer_free (vm, &vring->buffers[e->id], 1);
129           vring->buffers[e->id] = ~0;
130           used--;
131           last++;
132           n_left--;
133           out_of_order_count++;
134           vring->flags |= VRING_TX_OUT_OF_ORDER;
135         }
136     }
137
138   /*
139    * Some vhost-backends give buffers back in out-of-order fashion in used ring.
140    * It impacts the overall virtio-performance.
141    */
142   if (out_of_order_count)
143     vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
144                       out_of_order_count);
145
146   vring->desc_in_use = used;
147   vring->last_used_idx = last;
148 }
149
150 static_always_inline void
151 set_checksum_offsets (vlib_main_t * vm, virtio_if_t * vif, vlib_buffer_t * b,
152                       struct virtio_net_hdr_v1 *hdr)
153 {
154   if (b->flags & VNET_BUFFER_F_IS_IP4)
155     {
156       ip4_header_t *ip4;
157       gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
158       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
159       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
160       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
161         {
162           tcp_header_t *tcp =
163             (tcp_header_t *) (vlib_buffer_get_current (b) +
164                               gho.l4_hdr_offset);
165           tcp->checksum = 0;
166           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
167         }
168       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
169         {
170           udp_header_t *udp =
171             (udp_header_t *) (vlib_buffer_get_current (b) +
172                               gho.l4_hdr_offset);
173           udp->checksum = 0;
174           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
175         }
176
177       /*
178        * virtio devices do not support IP4 checksum offload. So driver takes care
179        * of it while doing tx.
180        */
181       ip4 =
182         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
183       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
184         ip4->checksum = ip4_header_checksum (ip4);
185     }
186   else if (b->flags & VNET_BUFFER_F_IS_IP6)
187     {
188       gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
189       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
190       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
191       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
192         {
193           tcp_header_t *tcp =
194             (tcp_header_t *) (vlib_buffer_get_current (b) +
195                               gho.l4_hdr_offset);
196           tcp->checksum = 0;
197           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
198         }
199       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
200         {
201           udp_header_t *udp =
202             (udp_header_t *) (vlib_buffer_get_current (b) +
203                               gho.l4_hdr_offset);
204           udp->checksum = 0;
205           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
206         }
207     }
208 }
209
210 static_always_inline u16
211 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
212                     virtio_vring_t * vring, u32 bi, u16 avail, u16 next,
213                     u16 mask, int do_gso, int csum_offload)
214 {
215   u16 n_added = 0;
216   int hdr_sz = vif->virtio_net_hdr_sz;
217   struct vring_desc *d;
218   d = &vring->desc[next];
219   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
220   struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
221
222   clib_memset (hdr, 0, hdr_sz);
223
224   if (do_gso && (b->flags & VNET_BUFFER_F_GSO))
225     {
226       if (b->flags & VNET_BUFFER_F_IS_IP4)
227         {
228           ip4_header_t *ip4;
229           tcp_header_t *tcp;
230           gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
231           hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
232           hdr->gso_size = vnet_buffer2 (b)->gso_size;
233           hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
234           tcp =
235             (tcp_header_t *) (vlib_buffer_get_current (b) +
236                               gho.l4_hdr_offset);
237           tcp->checksum = 0;
238           hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
239           hdr->csum_start = gho.l4_hdr_offset;  // 0x22;
240           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
241           ip4 =
242             (ip4_header_t *) (vlib_buffer_get_current (b) +
243                               gho.l3_hdr_offset);
244           /*
245            * virtio devices do not support IP4 checksum offload. So driver takes care
246            * of it while doing tx.
247            */
248           if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
249             ip4->checksum = ip4_header_checksum (ip4);
250         }
251       else if (b->flags & VNET_BUFFER_F_IS_IP6)
252         {
253           tcp_header_t *tcp;
254           gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
255           hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
256           hdr->gso_size = vnet_buffer2 (b)->gso_size;
257           hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
258           hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
259           tcp =
260             (tcp_header_t *) (vlib_buffer_get_current (b) +
261                               gho.l4_hdr_offset);
262           tcp->checksum = 0;
263           hdr->csum_start = gho.l4_hdr_offset;  // 0x36;
264           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
265         }
266     }
267   else if (csum_offload
268            && (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
269                            VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)))
270     {
271       set_checksum_offsets (vm, vif, b, hdr);
272     }
273
274   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
275     {
276       d->addr =
277         ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
278                                                                          b) :
279          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
280       d->len = b->current_length + hdr_sz;
281       d->flags = 0;
282     }
283   else
284     {
285       /*
286        * We are using single vlib_buffer_t for indirect descriptor(s)
287        * chain. Single descriptor is 16 bytes and vlib_buffer_t
288        * has 2048 bytes space. So maximum long chain can have 128
289        * (=2048/16) indirect descriptors.
290        * It can easily support 65535 bytes of Jumbo frames with
291        * each data buffer size of 512 bytes minimum.
292        */
293       u32 indirect_buffer = 0;
294       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
295         return n_added;
296
297       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
298       indirect_desc->current_data = 0;
299       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
300       indirect_desc->next_buffer = bi;
301       bi = indirect_buffer;
302
303       struct vring_desc *id =
304         (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
305       u32 count = 1;
306       if (vif->type == VIRTIO_IF_TYPE_PCI)
307         {
308           d->addr = vlib_physmem_get_pa (vm, id);
309           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
310
311           /*
312            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
313            * should be presented in separate descriptor and data will start
314            * from next descriptor.
315            */
316           if (PREDICT_TRUE
317               (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
318             id->len = b->current_length + hdr_sz;
319           else
320             {
321               id->len = hdr_sz;
322               id->flags = VRING_DESC_F_NEXT;
323               id->next = count;
324               count++;
325               id++;
326               id->addr = vlib_buffer_get_current_pa (vm, b);
327               id->len = b->current_length;
328             }
329           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
330             {
331               id->flags = VRING_DESC_F_NEXT;
332               id->next = count;
333               count++;
334               id++;
335               b = vlib_get_buffer (vm, b->next_buffer);
336               id->addr = vlib_buffer_get_current_pa (vm, b);
337               id->len = b->current_length;
338             }
339         }
340       else                      /* VIRTIO_IF_TYPE_TAP */
341         {
342           d->addr = pointer_to_uword (id);
343           /* first buffer in chain */
344           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
345           id->len = b->current_length + hdr_sz;
346
347           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
348             {
349               id->flags = VRING_DESC_F_NEXT;
350               id->next = count;
351               count++;
352               id++;
353               b = vlib_get_buffer (vm, b->next_buffer);
354               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
355               id->len = b->current_length;
356             }
357         }
358       id->flags = 0;
359       id->next = 0;
360       d->len = count * sizeof (struct vring_desc);
361       d->flags = VRING_DESC_F_INDIRECT;
362     }
363   vring->buffers[next] = bi;
364   vring->avail->ring[avail & mask] = next;
365   n_added++;
366   return n_added;
367 }
368
369 static_always_inline void
370 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
371                        u16 req, u16 next, u32 * first_free_desc_index,
372                        u16 * free_desc_count)
373 {
374   u16 start = 0;
375   /* next is used as hint: from where to start looking */
376   for (u16 i = 0; i < size; i++, next++)
377     {
378       if (vring->buffers[next & mask] == ~0)
379         {
380           if (*first_free_desc_index == ~0)
381             {
382               *first_free_desc_index = (next & mask);
383               start = i;
384               (*free_desc_count)++;
385               req--;
386               if (req == 0)
387                 break;
388             }
389           else
390             {
391               if (start + *free_desc_count == i)
392                 {
393                   (*free_desc_count)++;
394                   req--;
395                   if (req == 0)
396                     break;
397                 }
398               else
399                 break;
400             }
401         }
402     }
403 }
404
405 static_always_inline uword
406 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
407                             vlib_frame_t * frame, virtio_if_t * vif,
408                             int do_gso, int csum_offload)
409 {
410   u16 n_left = frame->n_vectors;
411   virtio_vring_t *vring;
412   u16 qid = vm->thread_index % vif->num_txqs;
413   vring = vec_elt_at_index (vif->txq_vrings, qid);
414   u16 used, next, avail;
415   u16 sz = vring->size;
416   u16 mask = sz - 1;
417   u16 retry_count = 2;
418   u32 *buffers = vlib_frame_vector_args (frame);
419
420   clib_spinlock_lock_if_init (&vring->lockp);
421
422   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
423       (vring->last_kick_avail_idx != vring->avail->idx))
424     virtio_kick (vm, vring, vif);
425
426 retry:
427   /* free consumed buffers */
428   virtio_free_used_device_desc (vm, vring, node->node_index);
429
430   used = vring->desc_in_use;
431   next = vring->desc_next;
432   avail = vring->avail->idx;
433
434   u16 free_desc_count = 0;
435
436   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
437     {
438       u32 first_free_desc_index = ~0;
439
440       virtio_find_free_desc (vring, sz, mask, n_left, next,
441                              &first_free_desc_index, &free_desc_count);
442
443       if (free_desc_count)
444         next = first_free_desc_index;
445     }
446   else
447     free_desc_count = sz - used;
448
449   while (n_left && free_desc_count)
450     {
451       u16 n_added = 0;
452       n_added =
453         add_buffer_to_slot (vm, vif, vring, buffers[0], avail, next, mask,
454                             do_gso, csum_offload);
455       if (!n_added)
456         break;
457       avail += n_added;
458       next = (next + n_added) & mask;
459       used += n_added;
460       buffers++;
461       n_left--;
462       free_desc_count--;
463     }
464
465   if (n_left != frame->n_vectors)
466     {
467       CLIB_MEMORY_STORE_BARRIER ();
468       vring->avail->idx = avail;
469       vring->desc_next = next;
470       vring->desc_in_use = used;
471       if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
472         virtio_kick (vm, vring, vif);
473     }
474
475   if (n_left)
476     {
477       if (retry_count--)
478         goto retry;
479
480       vlib_error_count (vm, node->node_index, VIRTIO_TX_ERROR_NO_FREE_SLOTS,
481                         n_left);
482       vlib_buffer_free (vm, buffers, n_left);
483     }
484
485   clib_spinlock_unlock_if_init (&vring->lockp);
486
487   return frame->n_vectors - n_left;
488 }
489
490 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
491                                                vlib_node_runtime_t * node,
492                                                vlib_frame_t * frame)
493 {
494   vnet_main_t *vnm = vnet_get_main ();
495   virtio_main_t *nm = &virtio_main;
496   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
497   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
498   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
499
500   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
501     return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
502                                        1);
503   else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
504     return virtio_interface_tx_inline (vm, node, frame, vif,
505                                        0 /* no do_gso */ , 1);
506   else
507     return virtio_interface_tx_inline (vm, node, frame, vif,
508                                        0 /* no do_gso */ , 0);
509 }
510
511 static void
512 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
513                                 u32 node_index)
514 {
515   virtio_main_t *apm = &virtio_main;
516   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
517   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
518
519   /* Shut off redirection */
520   if (node_index == ~0)
521     {
522       vif->per_interface_next_index = node_index;
523       return;
524     }
525
526   vif->per_interface_next_index =
527     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
528                         node_index);
529 }
530
531 static void
532 virtio_clear_hw_interface_counters (u32 instance)
533 {
534   /* Nothing for now */
535 }
536
537 static clib_error_t *
538 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
539                                  vnet_hw_interface_rx_mode mode)
540 {
541   virtio_main_t *mm = &virtio_main;
542   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
543   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
544   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
545
546   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
547     {
548       vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
549       return clib_error_return (0, "interrupt mode is not supported");
550     }
551
552   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
553     vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
554   else
555     vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
556
557   return 0;
558 }
559
560 static clib_error_t *
561 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
562 {
563   virtio_main_t *mm = &virtio_main;
564   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
565   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
566
567   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
568     {
569       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
570       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
571                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
572     }
573   else
574     {
575       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
576       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
577     }
578   return 0;
579 }
580
581 static clib_error_t *
582 virtio_subif_add_del_function (vnet_main_t * vnm,
583                                u32 hw_if_index,
584                                struct vnet_sw_interface_t *st, int is_add)
585 {
586   /* Nothing for now */
587   return 0;
588 }
589
590 /* *INDENT-OFF* */
591 VNET_DEVICE_CLASS (virtio_device_class) = {
592   .name = "virtio",
593   .format_device_name = format_virtio_device_name,
594   .format_device = format_virtio_device,
595   .format_tx_trace = format_virtio_tx_trace,
596   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
597   .tx_function_error_strings = virtio_tx_func_error_strings,
598   .rx_redirect_to_node = virtio_set_interface_next_node,
599   .clear_counters = virtio_clear_hw_interface_counters,
600   .admin_up_down_function = virtio_interface_admin_up_down,
601   .subif_add_del_function = virtio_subif_add_del_function,
602   .rx_mode_change_function = virtio_interface_rx_mode_change,
603 };
604 /* *INDENT-ON* */
605
606 /*
607  * fd.io coding-style-patch-verification: ON
608  *
609  * Local Variables:
610  * eval: (c-set-style "gnu")
611  * End:
612  */