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