virtio: fix the tcp/udp checksum offloads
[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           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
163         }
164       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
165         {
166           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
167         }
168
169       /*
170        * virtio devices do not support IP4 checksum offload. So driver takes care
171        * of it while doing tx.
172        */
173       ip4 =
174         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
175       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
176         ip4->checksum = ip4_header_checksum (ip4);
177     }
178   else if (b->flags & VNET_BUFFER_F_IS_IP6)
179     {
180       gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
181       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
182       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
183       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
184         {
185           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
186         }
187       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
188         {
189           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
190         }
191     }
192 }
193
194 static_always_inline u16
195 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
196                     virtio_vring_t * vring, u32 bi, u16 avail, u16 next,
197                     u16 mask, int do_gso, int csum_offload)
198 {
199   u16 n_added = 0;
200   int hdr_sz = vif->virtio_net_hdr_sz;
201   struct vring_desc *d;
202   d = &vring->desc[next];
203   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
204   struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
205
206   clib_memset (hdr, 0, hdr_sz);
207
208   if (do_gso && (b->flags & VNET_BUFFER_F_GSO))
209     {
210       if (b->flags & VNET_BUFFER_F_IS_IP4)
211         {
212           ip4_header_t *ip4;
213           gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
214           hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
215           hdr->gso_size = vnet_buffer2 (b)->gso_size;
216           hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
217           hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
218           hdr->csum_start = gho.l4_hdr_offset;  // 0x22;
219           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
220           ip4 =
221             (ip4_header_t *) (vlib_buffer_get_current (b) +
222                               gho.l3_hdr_offset);
223           /*
224            * virtio devices do not support IP4 checksum offload. So driver takes care
225            * of it while doing tx.
226            */
227           if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
228             ip4->checksum = ip4_header_checksum (ip4);
229         }
230       else if (b->flags & VNET_BUFFER_F_IS_IP6)
231         {
232           gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
233           hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
234           hdr->gso_size = vnet_buffer2 (b)->gso_size;
235           hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
236           hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
237           hdr->csum_start = gho.l4_hdr_offset;  // 0x36;
238           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
239         }
240     }
241   else if (csum_offload
242            && (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
243                            VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)))
244     {
245       set_checksum_offsets (vm, vif, b, hdr);
246     }
247
248   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
249     {
250       d->addr =
251         ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
252                                                                          b) :
253          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
254       d->len = b->current_length + hdr_sz;
255       d->flags = 0;
256     }
257   else
258     {
259       /*
260        * We are using single vlib_buffer_t for indirect descriptor(s)
261        * chain. Single descriptor is 16 bytes and vlib_buffer_t
262        * has 2048 bytes space. So maximum long chain can have 128
263        * (=2048/16) indirect descriptors.
264        * It can easily support 65535 bytes of Jumbo frames with
265        * each data buffer size of 512 bytes minimum.
266        */
267       u32 indirect_buffer = 0;
268       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
269         return n_added;
270
271       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
272       indirect_desc->current_data = 0;
273       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
274       indirect_desc->next_buffer = bi;
275       bi = indirect_buffer;
276
277       struct vring_desc *id =
278         (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
279       u32 count = 1;
280       if (vif->type == VIRTIO_IF_TYPE_PCI)
281         {
282           d->addr = vlib_physmem_get_pa (vm, id);
283           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
284
285           /*
286            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
287            * should be presented in separate descriptor and data will start
288            * from next descriptor.
289            */
290           if (PREDICT_TRUE
291               (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
292             id->len = b->current_length + hdr_sz;
293           else
294             {
295               id->len = hdr_sz;
296               id->flags = VRING_DESC_F_NEXT;
297               id->next = count;
298               count++;
299               id++;
300               id->addr = vlib_buffer_get_current_pa (vm, b);
301               id->len = b->current_length;
302             }
303           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
304             {
305               id->flags = VRING_DESC_F_NEXT;
306               id->next = count;
307               count++;
308               id++;
309               b = vlib_get_buffer (vm, b->next_buffer);
310               id->addr = vlib_buffer_get_current_pa (vm, b);
311               id->len = b->current_length;
312             }
313         }
314       else                      /* VIRTIO_IF_TYPE_TAP */
315         {
316           d->addr = pointer_to_uword (id);
317           /* first buffer in chain */
318           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
319           id->len = b->current_length + hdr_sz;
320
321           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
322             {
323               id->flags = VRING_DESC_F_NEXT;
324               id->next = count;
325               count++;
326               id++;
327               b = vlib_get_buffer (vm, b->next_buffer);
328               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
329               id->len = b->current_length;
330             }
331         }
332       id->flags = 0;
333       id->next = 0;
334       d->len = count * sizeof (struct vring_desc);
335       d->flags = VRING_DESC_F_INDIRECT;
336     }
337   vring->buffers[next] = bi;
338   vring->avail->ring[avail & mask] = next;
339   n_added++;
340   return n_added;
341 }
342
343 static_always_inline void
344 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
345                        u16 req, u16 next, u32 * first_free_desc_index,
346                        u16 * free_desc_count)
347 {
348   u16 start = 0;
349   /* next is used as hint: from where to start looking */
350   for (u16 i = 0; i < size; i++, next++)
351     {
352       if (vring->buffers[next & mask] == ~0)
353         {
354           if (*first_free_desc_index == ~0)
355             {
356               *first_free_desc_index = (next & mask);
357               start = i;
358               (*free_desc_count)++;
359               req--;
360               if (req == 0)
361                 break;
362             }
363           else
364             {
365               if (start + *free_desc_count == i)
366                 {
367                   (*free_desc_count)++;
368                   req--;
369                   if (req == 0)
370                     break;
371                 }
372               else
373                 break;
374             }
375         }
376     }
377 }
378
379 static_always_inline uword
380 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
381                             vlib_frame_t * frame, virtio_if_t * vif,
382                             int do_gso, int csum_offload)
383 {
384   u16 n_left = frame->n_vectors;
385   virtio_vring_t *vring;
386   u16 qid = vm->thread_index % vif->num_txqs;
387   vring = vec_elt_at_index (vif->txq_vrings, qid);
388   u16 used, next, avail;
389   u16 sz = vring->size;
390   u16 mask = sz - 1;
391   u16 retry_count = 2;
392   u32 *buffers = vlib_frame_vector_args (frame);
393
394   clib_spinlock_lock_if_init (&vring->lockp);
395
396   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
397       (vring->last_kick_avail_idx != vring->avail->idx))
398     virtio_kick (vm, vring, vif);
399
400 retry:
401   /* free consumed buffers */
402   virtio_free_used_device_desc (vm, vring, node->node_index);
403
404   used = vring->desc_in_use;
405   next = vring->desc_next;
406   avail = vring->avail->idx;
407
408   u16 free_desc_count = 0;
409
410   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
411     {
412       u32 first_free_desc_index = ~0;
413
414       virtio_find_free_desc (vring, sz, mask, n_left, next,
415                              &first_free_desc_index, &free_desc_count);
416
417       if (free_desc_count)
418         next = first_free_desc_index;
419     }
420   else
421     free_desc_count = sz - used;
422
423   while (n_left && free_desc_count)
424     {
425       u16 n_added = 0;
426       n_added =
427         add_buffer_to_slot (vm, vif, vring, buffers[0], avail, next, mask,
428                             do_gso, csum_offload);
429       if (!n_added)
430         break;
431       avail += n_added;
432       next = (next + n_added) & mask;
433       used += n_added;
434       buffers++;
435       n_left--;
436       free_desc_count--;
437     }
438
439   if (n_left != frame->n_vectors)
440     {
441       CLIB_MEMORY_STORE_BARRIER ();
442       vring->avail->idx = avail;
443       vring->desc_next = next;
444       vring->desc_in_use = used;
445       if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
446         virtio_kick (vm, vring, vif);
447     }
448
449   if (n_left)
450     {
451       if (retry_count--)
452         goto retry;
453
454       vlib_error_count (vm, node->node_index, VIRTIO_TX_ERROR_NO_FREE_SLOTS,
455                         n_left);
456       vlib_buffer_free (vm, buffers, n_left);
457     }
458
459   clib_spinlock_unlock_if_init (&vring->lockp);
460
461   return frame->n_vectors - n_left;
462 }
463
464 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
465                                                vlib_node_runtime_t * node,
466                                                vlib_frame_t * frame)
467 {
468   vnet_main_t *vnm = vnet_get_main ();
469   virtio_main_t *nm = &virtio_main;
470   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
471   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
472   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
473
474   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
475     return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
476                                        1);
477   else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
478     return virtio_interface_tx_inline (vm, node, frame, vif,
479                                        0 /* no do_gso */ , 1);
480   else
481     return virtio_interface_tx_inline (vm, node, frame, vif,
482                                        0 /* no do_gso */ , 0);
483 }
484
485 static void
486 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
487                                 u32 node_index)
488 {
489   virtio_main_t *apm = &virtio_main;
490   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
491   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
492
493   /* Shut off redirection */
494   if (node_index == ~0)
495     {
496       vif->per_interface_next_index = node_index;
497       return;
498     }
499
500   vif->per_interface_next_index =
501     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
502                         node_index);
503 }
504
505 static void
506 virtio_clear_hw_interface_counters (u32 instance)
507 {
508   /* Nothing for now */
509 }
510
511 static clib_error_t *
512 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
513                                  vnet_hw_interface_rx_mode mode)
514 {
515   virtio_main_t *mm = &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 (mm->interfaces, hw->dev_instance);
518   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
519
520   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
521     {
522       vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
523       return clib_error_return (0, "interrupt mode is not supported");
524     }
525
526   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
527     vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
528   else
529     vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
530
531   return 0;
532 }
533
534 static clib_error_t *
535 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
536 {
537   virtio_main_t *mm = &virtio_main;
538   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
539   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
540
541   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
542     {
543       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
544       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
545                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
546     }
547   else
548     {
549       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
550       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
551     }
552   return 0;
553 }
554
555 static clib_error_t *
556 virtio_subif_add_del_function (vnet_main_t * vnm,
557                                u32 hw_if_index,
558                                struct vnet_sw_interface_t *st, int is_add)
559 {
560   /* Nothing for now */
561   return 0;
562 }
563
564 /* *INDENT-OFF* */
565 VNET_DEVICE_CLASS (virtio_device_class) = {
566   .name = "virtio",
567   .format_device_name = format_virtio_device_name,
568   .format_device = format_virtio_device,
569   .format_tx_trace = format_virtio_tx_trace,
570   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
571   .tx_function_error_strings = virtio_tx_func_error_strings,
572   .rx_redirect_to_node = virtio_set_interface_next_node,
573   .clear_counters = virtio_clear_hw_interface_counters,
574   .admin_up_down_function = virtio_interface_admin_up_down,
575   .subif_add_del_function = virtio_subif_add_del_function,
576   .rx_mode_change_function = virtio_interface_rx_mode_change,
577 };
578 /* *INDENT-ON* */
579
580 /*
581  * fd.io coding-style-patch-verification: ON
582  *
583  * Local Variables:
584  * eval: (c-set-style "gnu")
585  * End:
586  */