virtio: add simple drop counter for interface
[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/vnet.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/gso/gro_func.h>
27 #include <vnet/gso/hdr_offset_parser.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/tcp/tcp_packet.h>
31 #include <vnet/udp/udp_packet.h>
32 #include <vnet/devices/virtio/virtio.h>
33
34 #define foreach_virtio_tx_func_error           \
35 _(NO_FREE_SLOTS, "no free tx slots")           \
36 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
37 _(PENDING_MSGS, "pending msgs in tx ring") \
38 _(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
39 _(OUT_OF_ORDER, "out-of-order buffers in used ring") \
40 _(GSO_PACKET_DROP, "gso disabled on itf  -- gso packet drop") \
41 _(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
42
43 typedef enum
44 {
45 #define _(f,s) VIRTIO_TX_ERROR_##f,
46   foreach_virtio_tx_func_error
47 #undef _
48     VIRTIO_TX_N_ERROR,
49 } virtio_tx_func_error_t;
50
51 static char *virtio_tx_func_error_strings[] = {
52 #define _(n,s) s,
53   foreach_virtio_tx_func_error
54 #undef _
55 };
56
57 static u8 *
58 format_virtio_device (u8 * s, va_list * args)
59 {
60   u32 dev_instance = va_arg (*args, u32);
61   int verbose = va_arg (*args, int);
62   u32 indent = format_get_indent (s);
63
64   s = format (s, "VIRTIO interface");
65   if (verbose)
66     {
67       s = format (s, "\n%U instance %u", format_white_space, indent + 2,
68                   dev_instance);
69     }
70   return s;
71 }
72
73 typedef struct
74 {
75   u32 buffer_index;
76   u32 sw_if_index;
77   generic_header_offset_t gho;
78   vlib_buffer_t buffer;
79 } virtio_tx_trace_t;
80
81 static u8 *
82 format_virtio_tx_trace (u8 * s, va_list * va)
83 {
84   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
85   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
86   virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *);
87   u32 indent = format_get_indent (s);
88
89   s = format (s, "%Ubuffer 0x%x: %U\n",
90               format_white_space, indent,
91               t->buffer_index, format_vnet_buffer, &t->buffer);
92   s =
93     format (s, "%U%U\n", format_white_space, indent,
94             format_generic_header_offset, &t->gho);
95   s =
96     format (s, "%U%U", format_white_space, indent,
97             format_ethernet_header_with_length, t->buffer.pre_data,
98             sizeof (t->buffer.pre_data));
99   return s;
100 }
101
102 static void
103 virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0,
104                  u32 bi, int is_tun)
105 {
106   virtio_tx_trace_t *t;
107   t = vlib_add_trace (vm, node, b0, sizeof (t[0]));
108   t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
109   t->buffer_index = bi;
110   if (is_tun)
111     {
112       int is_ip4 = 0, is_ip6 = 0;
113
114       switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
115         {
116         case 0x40:
117           is_ip4 = 1;
118           break;
119         case 0x60:
120           is_ip6 = 1;
121           break;
122         default:
123           break;
124         }
125       vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6);
126     }
127   else
128     vnet_generic_header_offset_parser (b0, &t->gho, 1,
129                                        b0->flags &
130                                        VNET_BUFFER_F_IS_IP4,
131                                        b0->flags & VNET_BUFFER_F_IS_IP6);
132
133   clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data));
134   clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0),
135                     sizeof (t->buffer.pre_data));
136 }
137
138 static void
139 virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif,
140                               uword node_index, u32 *buffers, u16 n,
141                               virtio_tx_func_error_t error)
142 {
143   vlib_error_count (vm, node_index, error, n);
144   vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
145                                    VNET_INTERFACE_COUNTER_DROP,
146                                  vm->thread_index, vif->sw_if_index, n);
147   vlib_buffer_free (vm, buffers, n);
148 }
149
150 static void
151 virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
152 {
153   ASSERT (n_buffers <= ring_size);
154
155   if (PREDICT_TRUE (start + n_buffers <= ring_size))
156     {
157       clib_memset_u32 (ring + start, ~0, n_buffers);
158     }
159   else
160     {
161       clib_memset_u32 (ring + start, ~0, ring_size - start);
162       clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
163     }
164 }
165
166 static void
167 virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring,
168                                     uword node_index)
169 {
170   u16 used = vring->desc_in_use;
171   u16 sz = vring->size;
172   u16 mask = sz - 1;
173   u16 last = vring->last_used_idx;
174   u16 n_left = vring->used->idx - last;
175   u16 out_of_order_count = 0;
176
177   if (n_left == 0)
178     return;
179
180   while (n_left)
181     {
182       vring_used_elem_t *e = &vring->used->ring[last & mask];
183       u16 slot, n_buffers;
184       slot = n_buffers = e->id;
185
186       while (e->id == (n_buffers & mask))
187         {
188           n_left--;
189           last++;
190           n_buffers++;
191           vring_desc_t *d = &vring->desc[e->id];
192           u16 next;
193           while (d->flags & VRING_DESC_F_NEXT)
194             {
195               n_buffers++;
196               next = d->next;
197               d = &vring->desc[next];
198             }
199           if (n_left == 0)
200             break;
201           e = &vring->used->ring[last & mask];
202         }
203       vlib_buffer_free_from_ring (vm, vring->buffers, slot,
204                                   sz, (n_buffers - slot));
205       virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
206       used -= (n_buffers - slot);
207
208       if (n_left > 0)
209         {
210           vlib_buffer_free (vm, &vring->buffers[e->id], 1);
211           vring->buffers[e->id] = ~0;
212           used--;
213           last++;
214           n_left--;
215           out_of_order_count++;
216           vring->flags |= VRING_TX_OUT_OF_ORDER;
217         }
218     }
219
220   /*
221    * Some vhost-backends give buffers back in out-of-order fashion in used ring.
222    * It impacts the overall virtio-performance.
223    */
224   if (out_of_order_count)
225     vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
226                       out_of_order_count);
227
228   vring->desc_in_use = used;
229   vring->last_used_idx = last;
230 }
231
232 static void
233 virtio_free_used_device_desc_packed (vlib_main_t *vm, virtio_vring_t *vring,
234                                      uword node_index)
235 {
236   vring_packed_desc_t *d;
237   u16 sz = vring->size;
238   u16 last = vring->last_used_idx;
239   u16 n_buffers = 0, start;
240   u16 flags;
241
242   if (vring->desc_in_use == 0)
243     return;
244
245   d = &vring->packed_desc[last];
246   flags = d->flags;
247   start = d->id;
248
249   while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
250          (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
251     {
252       last++;
253       n_buffers++;
254
255       if (last >= sz)
256         {
257           last = 0;
258           vring->used_wrap_counter ^= 1;
259         }
260       d = &vring->packed_desc[last];
261       flags = d->flags;
262     }
263
264   if (n_buffers)
265     {
266       vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
267       virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
268       vring->desc_in_use -= n_buffers;
269       vring->last_used_idx = last;
270     }
271 }
272
273 static void
274 virtio_free_used_device_desc (vlib_main_t *vm, virtio_vring_t *vring,
275                               uword node_index, int packed)
276 {
277   if (packed)
278     virtio_free_used_device_desc_packed (vm, vring, node_index);
279   else
280     virtio_free_used_device_desc_split (vm, vring, node_index);
281
282 }
283
284 static void
285 set_checksum_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr,
286                       const int is_l2)
287 {
288   u32 oflags = vnet_buffer2 (b)->oflags;
289
290   if (b->flags & VNET_BUFFER_F_IS_IP4)
291     {
292       ip4_header_t *ip4;
293       generic_header_offset_t gho = { 0 };
294       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
295                                          0 /* ip6 */ );
296       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
297       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
298       if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
299         {
300           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
301         }
302       else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
303         {
304           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
305         }
306
307       /*
308        * virtio devices do not support IP4 checksum offload. So driver takes care
309        * of it while doing tx.
310        */
311       ip4 =
312         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
313       if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
314         ip4->checksum = ip4_header_checksum (ip4);
315     }
316   else if (b->flags & VNET_BUFFER_F_IS_IP6)
317     {
318       generic_header_offset_t gho = { 0 };
319       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
320                                          1 /* ip6 */ );
321       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
322       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
323       if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
324         {
325           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
326         }
327       else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
328         {
329           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
330         }
331     }
332 }
333
334 static void
335 set_gso_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, const int is_l2)
336 {
337   u32 oflags = vnet_buffer2 (b)->oflags;
338
339   if (b->flags & VNET_BUFFER_F_IS_IP4)
340     {
341       ip4_header_t *ip4;
342       generic_header_offset_t gho = { 0 };
343       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
344                                          0 /* ip6 */ );
345       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
346       hdr->gso_size = vnet_buffer2 (b)->gso_size;
347       hdr->hdr_len = gho.hdr_sz;
348       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
349       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
350       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
351       ip4 =
352         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
353       /*
354        * virtio devices do not support IP4 checksum offload. So driver takes care
355        * of it while doing tx.
356        */
357       if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
358         ip4->checksum = ip4_header_checksum (ip4);
359     }
360   else if (b->flags & VNET_BUFFER_F_IS_IP6)
361     {
362       generic_header_offset_t gho = { 0 };
363       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
364                                          1 /* ip6 */ );
365       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
366       hdr->gso_size = vnet_buffer2 (b)->gso_size;
367       hdr->hdr_len = gho.hdr_sz;
368       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
369       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
370       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
371     }
372 }
373
374 static u16
375 add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node,
376                     virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
377                     u16 free_desc_count, u16 avail, u16 next, u16 mask,
378                     int hdr_sz, int do_gso, int csum_offload, int is_pci,
379                     int is_tun, int is_indirect, int is_any_layout)
380 {
381   u16 n_added = 0;
382   vring_desc_t *d;
383   int is_l2 = !is_tun;
384   d = &vring->desc[next];
385   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
386   virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
387   u32 drop_inline = ~0;
388
389   clib_memset_u8 (hdr, 0, hdr_sz);
390
391   if (b->flags & VNET_BUFFER_F_GSO)
392     {
393       if (do_gso)
394         set_gso_offsets (b, hdr, is_l2);
395       else
396         {
397           drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
398           goto done;
399         }
400     }
401   else if (b->flags & VNET_BUFFER_F_OFFLOAD)
402     {
403       if (csum_offload)
404         set_checksum_offsets (b, hdr, is_l2);
405       else
406         {
407           drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
408           goto done;
409         }
410     }
411
412   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
413     {
414       virtio_tx_trace (vm, node, b, bi, is_tun);
415     }
416
417   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
418     {
419       d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
420                  pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
421       d->len = b->current_length + hdr_sz;
422       d->flags = 0;
423     }
424   else if (is_indirect)
425     {
426       /*
427        * We are using single vlib_buffer_t for indirect descriptor(s)
428        * chain. Single descriptor is 16 bytes and vlib_buffer_t
429        * has 2048 bytes space. So maximum long chain can have 128
430        * (=2048/16) indirect descriptors.
431        * It can easily support 65535 bytes of Jumbo frames with
432        * each data buffer size of 512 bytes minimum.
433        */
434       u32 indirect_buffer = 0;
435       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
436         {
437           drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
438           goto done;
439         }
440
441       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
442       indirect_desc->current_data = 0;
443       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
444       indirect_desc->next_buffer = bi;
445       bi = indirect_buffer;
446
447       vring_desc_t *id =
448         (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
449       u32 count = 1;
450       if (is_pci)
451         {
452           d->addr = vlib_physmem_get_pa (vm, id);
453           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
454
455           /*
456            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
457            * should be presented in separate descriptor and data will start
458            * from next descriptor.
459            */
460           if (is_any_layout)
461             id->len = b->current_length + hdr_sz;
462           else
463             {
464               id->len = hdr_sz;
465               id->flags = VRING_DESC_F_NEXT;
466               id->next = count;
467               count++;
468               id++;
469               id->addr = vlib_buffer_get_current_pa (vm, b);
470               id->len = b->current_length;
471             }
472           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
473             {
474               id->flags = VRING_DESC_F_NEXT;
475               id->next = count;
476               count++;
477               id++;
478               b = vlib_get_buffer (vm, b->next_buffer);
479               id->addr = vlib_buffer_get_current_pa (vm, b);
480               id->len = b->current_length;
481             }
482         }
483       else                      /* VIRTIO_IF_TYPE_[TAP | TUN] */
484         {
485           d->addr = pointer_to_uword (id);
486           /* first buffer in chain */
487           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
488           id->len = b->current_length + hdr_sz;
489
490           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
491             {
492               id->flags = VRING_DESC_F_NEXT;
493               id->next = count;
494               count++;
495               id++;
496               b = vlib_get_buffer (vm, b->next_buffer);
497               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
498               id->len = b->current_length;
499             }
500         }
501       id->flags = 0;
502       id->next = 0;
503       d->len = count * sizeof (vring_desc_t);
504       d->flags = VRING_DESC_F_INDIRECT;
505     }
506   else if (is_pci)
507     {
508       u16 count = next;
509       vlib_buffer_t *b_temp = b;
510       u16 n_buffers_in_chain = 1;
511
512       /*
513        * Check the length of the chain for the required number of
514        * descriptors. Return from here, retry to get more descriptors,
515        * if chain length is greater than available descriptors.
516        */
517       while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
518         {
519           n_buffers_in_chain++;
520           b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
521         }
522
523       if (n_buffers_in_chain > free_desc_count)
524         return n_buffers_in_chain;
525
526       d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
527       d->len = b->current_length + hdr_sz;
528
529       while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
530         {
531           d->flags = VRING_DESC_F_NEXT;
532           vring->buffers[count] = bi;
533           b->flags &=
534             ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
535           bi = b->next_buffer;
536           b->next_buffer = 0;
537           n_added++;
538           count = (count + 1) & mask;
539           d->next = count;
540           d = &vring->desc[count];
541           b = vlib_get_buffer (vm, bi);
542           d->addr = vlib_buffer_get_current_pa (vm, b);
543           d->len = b->current_length;
544         }
545       d->flags = 0;
546       vring->buffers[count] = bi;
547       vring->avail->ring[avail & mask] = next;
548       n_added++;
549       return n_added;
550     }
551   else
552     {
553       ASSERT (0);
554     }
555   vring->buffers[next] = bi;
556   vring->avail->ring[avail & mask] = next;
557   n_added++;
558
559 done:
560   if (drop_inline != ~0)
561     virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
562                                   drop_inline);
563
564   return n_added;
565 }
566
567 static u16
568 add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node,
569                            virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
570                            u16 next, int hdr_sz, int do_gso, int csum_offload,
571                            int is_pci, int is_tun, int is_indirect,
572                            int is_any_layout)
573 {
574   u16 n_added = 0, flags = 0;
575   int is_l2 = !is_tun;
576   vring_packed_desc_t *d = &vring->packed_desc[next];
577   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
578   virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
579   u32 drop_inline = ~0;
580
581   clib_memset (hdr, 0, hdr_sz);
582
583   if (b->flags & VNET_BUFFER_F_GSO)
584     {
585       if (do_gso)
586         set_gso_offsets (b, hdr, is_l2);
587       else
588         {
589           drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
590           goto done;
591         }
592     }
593   else if (b->flags & VNET_BUFFER_F_OFFLOAD)
594     {
595       if (csum_offload)
596         set_checksum_offsets (b, hdr, is_l2);
597       else
598         {
599           drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
600           goto done;
601         }
602     }
603   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
604     {
605       virtio_tx_trace (vm, node, b, bi, is_tun);
606     }
607
608   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
609     {
610       d->addr =
611         ((is_pci) ? vlib_buffer_get_current_pa (vm,
612                                                 b) :
613          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
614       d->len = b->current_length + hdr_sz;
615     }
616   else if (is_indirect)
617     {
618       /*
619        * We are using single vlib_buffer_t for indirect descriptor(s)
620        * chain. Single descriptor is 16 bytes and vlib_buffer_t
621        * has 2048 bytes space. So maximum long chain can have 128
622        * (=2048/16) indirect descriptors.
623        * It can easily support 65535 bytes of Jumbo frames with
624        * each data buffer size of 512 bytes minimum.
625        */
626       u32 indirect_buffer = 0;
627       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
628         {
629           drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
630           goto done;
631         }
632
633       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
634       indirect_desc->current_data = 0;
635       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
636       indirect_desc->next_buffer = bi;
637       bi = indirect_buffer;
638
639       vring_packed_desc_t *id =
640         (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc);
641       u32 count = 1;
642       if (is_pci)
643         {
644           d->addr = vlib_physmem_get_pa (vm, id);
645           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
646
647           /*
648            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
649            * should be presented in separate descriptor and data will start
650            * from next descriptor.
651            */
652           if (is_any_layout)
653             id->len = b->current_length + hdr_sz;
654           else
655             {
656               id->len = hdr_sz;
657               id->flags = 0;
658               id->id = 0;
659               count++;
660               id++;
661               id->addr = vlib_buffer_get_current_pa (vm, b);
662               id->len = b->current_length;
663             }
664           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
665             {
666               id->flags = 0;
667               id->id = 0;
668               count++;
669               id++;
670               b = vlib_get_buffer (vm, b->next_buffer);
671               id->addr = vlib_buffer_get_current_pa (vm, b);
672               id->len = b->current_length;
673             }
674         }
675       id->flags = 0;
676       id->id = 0;
677       d->len = count * sizeof (vring_packed_desc_t);
678       flags = VRING_DESC_F_INDIRECT;
679     }
680   else
681     {
682       ASSERT (0);
683     }
684   if (vring->avail_wrap_counter)
685     {
686       flags |= VRING_DESC_F_AVAIL;
687       flags &= ~VRING_DESC_F_USED;
688     }
689   else
690     {
691       flags &= ~VRING_DESC_F_AVAIL;
692       flags |= VRING_DESC_F_USED;
693     }
694
695   d->id = next;
696   d->flags = flags;
697   vring->buffers[next] = bi;
698   n_added++;
699
700 done:
701   if (drop_inline != ~0)
702     virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
703                                   drop_inline);
704
705   return n_added;
706 }
707
708 static uword
709 virtio_interface_tx_packed_gso_inline (vlib_main_t *vm,
710                                        vlib_node_runtime_t *node,
711                                        virtio_if_t *vif, virtio_if_type_t type,
712                                        virtio_vring_t *vring, u32 *buffers,
713                                        u16 n_left, const int do_gso,
714                                        const int csum_offload)
715 {
716   int is_pci = (type == VIRTIO_IF_TYPE_PCI);
717   int is_tun = (type == VIRTIO_IF_TYPE_TUN);
718   int is_indirect =
719     ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
720   int is_any_layout =
721     ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
722   const int hdr_sz = vif->virtio_net_hdr_sz;
723   u16 sz = vring->size;
724   u16 used, next, n_buffers = 0, n_buffers_left = 0;
725   u16 n_vectors = n_left;
726
727
728   used = vring->desc_in_use;
729   next = vring->desc_next;
730
731   if (vif->packet_buffering)
732     {
733       n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
734
735       while (n_buffers_left && used < sz)
736         {
737           u16 n_added = 0;
738
739           u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
740           if (bi == ~0)
741             break;
742           n_added = add_buffer_to_slot_packed (
743             vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload,
744             is_pci, is_tun, is_indirect, is_any_layout);
745           n_buffers_left--;
746           if (PREDICT_FALSE (n_added == 0))
747             continue;
748
749           used++;
750           next++;
751           if (next >= sz)
752             {
753               next = 0;
754               vring->avail_wrap_counter ^= 1;
755             }
756         }
757     }
758
759   while (n_left && used < sz)
760     {
761       u16 n_added = 0;
762
763       n_added = add_buffer_to_slot_packed (
764         vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload,
765         is_pci, is_tun, is_indirect, is_any_layout);
766       buffers++;
767       n_left--;
768       if (PREDICT_FALSE (n_added == 0))
769         continue;
770
771       used++;
772       next++;
773       if (next >= sz)
774         {
775           next = 0;
776           vring->avail_wrap_counter ^= 1;
777         }
778     }
779
780   if (n_left != n_vectors || n_buffers != n_buffers_left)
781     {
782       CLIB_MEMORY_STORE_BARRIER ();
783       vring->desc_next = next;
784       vring->desc_in_use = used;
785       CLIB_MEMORY_BARRIER ();
786       if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
787         virtio_kick (vm, vring, vif);
788     }
789
790   return n_left;
791 }
792
793 static void
794 virtio_find_free_desc (virtio_vring_t *vring, u16 size, u16 mask, u16 req,
795                        u16 next, u32 *first_free_desc_index,
796                        u16 *free_desc_count)
797 {
798   u16 start = 0;
799   /* next is used as hint: from where to start looking */
800   for (u16 i = 0; i < size; i++, next++)
801     {
802       if (vring->buffers[next & mask] == ~0)
803         {
804           if (*first_free_desc_index == ~0)
805             {
806               *first_free_desc_index = (next & mask);
807               start = i;
808               (*free_desc_count)++;
809               req--;
810               if (req == 0)
811                 break;
812             }
813           else
814             {
815               if (start + *free_desc_count == i)
816                 {
817                   (*free_desc_count)++;
818                   req--;
819                   if (req == 0)
820                     break;
821                 }
822               else
823                 break;
824             }
825         }
826     }
827 }
828
829 static u16
830 virtio_interface_tx_split_gso_inline (vlib_main_t *vm,
831                                       vlib_node_runtime_t *node,
832                                       virtio_if_t *vif, virtio_if_type_t type,
833                                       virtio_vring_t *vring, u32 *buffers,
834                                       u16 n_left, int do_gso, int csum_offload)
835 {
836   u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
837   int is_pci = (type == VIRTIO_IF_TYPE_PCI);
838   int is_tun = (type == VIRTIO_IF_TYPE_TUN);
839   int is_indirect =
840     ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
841   int is_any_layout =
842     ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
843   u16 sz = vring->size;
844   int hdr_sz = vif->virtio_net_hdr_sz;
845   u16 mask = sz - 1;
846   u16 n_vectors = n_left;
847
848   used = vring->desc_in_use;
849   next = vring->desc_next;
850   avail = vring->avail->idx;
851
852   u16 free_desc_count = 0;
853
854   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
855     {
856       u32 first_free_desc_index = ~0;
857
858       virtio_find_free_desc (vring, sz, mask, n_left, next,
859                              &first_free_desc_index, &free_desc_count);
860
861       if (free_desc_count)
862         next = first_free_desc_index;
863     }
864   else
865     free_desc_count = sz - used;
866
867   if (vif->packet_buffering)
868     {
869       n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
870
871       while (n_buffers_left && free_desc_count)
872         {
873           u16 n_added = 0;
874
875           u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
876           if (bi == ~0)
877             break;
878
879           n_added = add_buffer_to_slot (vm, node, vif, vring, bi,
880                                         free_desc_count, avail, next, mask,
881                                         hdr_sz, do_gso, csum_offload, is_pci,
882                                         is_tun, is_indirect, is_any_layout);
883           if (PREDICT_FALSE (n_added == 0))
884             {
885               n_buffers_left--;
886               continue;
887             }
888           else if (PREDICT_FALSE (n_added > free_desc_count))
889             break;
890
891           avail++;
892           next = (next + n_added) & mask;
893           used += n_added;
894           n_buffers_left--;
895           free_desc_count -= n_added;
896         }
897     }
898
899   while (n_left && free_desc_count)
900     {
901       u16 n_added = 0;
902
903       n_added =
904         add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count,
905                             avail, next, mask, hdr_sz, do_gso, csum_offload,
906                             is_pci, is_tun, is_indirect, is_any_layout);
907
908       if (PREDICT_FALSE (n_added == 0))
909         {
910           buffers++;
911           n_left--;
912           continue;
913         }
914       else if (PREDICT_FALSE (n_added > free_desc_count))
915         break;
916
917       avail++;
918       next = (next + n_added) & mask;
919       used += n_added;
920       buffers++;
921       n_left--;
922       free_desc_count -= n_added;
923     }
924
925   if (n_left != n_vectors || n_buffers != n_buffers_left)
926     {
927       clib_atomic_store_seq_cst (&vring->avail->idx, avail);
928       vring->desc_next = next;
929       vring->desc_in_use = used;
930       if ((clib_atomic_load_seq_cst (&vring->used->flags) &
931            VRING_USED_F_NO_NOTIFY) == 0)
932         virtio_kick (vm, vring, vif);
933     }
934
935   return n_left;
936 }
937
938 static u16
939 virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
940                                 virtio_if_t *vif, virtio_if_type_t type,
941                                 virtio_vring_t *vring, u32 *buffers,
942                                 u16 n_left, int packed, int do_gso,
943                                 int csum_offload)
944 {
945   if (packed)
946     return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
947                                                   buffers, n_left,
948                                                   do_gso, csum_offload);
949   else
950     return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
951                                                  buffers, n_left,
952                                                  do_gso, csum_offload);
953 }
954
955 static u16
956 virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
957                             virtio_if_t *vif, virtio_vring_t *vring,
958                             virtio_if_type_t type, u32 *buffers, u16 n_left,
959                             int packed)
960 {
961   vnet_main_t *vnm = vnet_get_main ();
962   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
963
964   if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO)
965     return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
966                                            buffers, n_left, packed,
967                                            1 /* do_gso */ ,
968                                            1 /* checksum offload */ );
969   else if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM)
970     return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
971                                            buffers, n_left, packed,
972                                            0 /* no do_gso */ ,
973                                            1 /* checksum offload */ );
974   else
975     return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
976                                            buffers, n_left, packed,
977                                            0 /* no do_gso */ ,
978                                            0 /* no checksum offload */ );
979 }
980
981 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
982                                                vlib_node_runtime_t * node,
983                                                vlib_frame_t * frame)
984 {
985   virtio_main_t *nm = &virtio_main;
986   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
987   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
988   u16 qid = vm->thread_index % vif->num_txqs;
989   virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
990   u16 n_left = frame->n_vectors;
991   u32 *buffers = vlib_frame_vector_args (frame);
992   u32 to[GRO_TO_VECTOR_SIZE (n_left)];
993   int packed = vif->is_packed;
994   u16 n_vectors = frame->n_vectors;
995
996   clib_spinlock_lock_if_init (&vring->lockp);
997
998   if (vif->packet_coalesce)
999     {
1000       n_vectors = n_left =
1001         vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
1002       buffers = to;
1003     }
1004
1005   u16 retry_count = 2;
1006
1007 retry:
1008   /* free consumed buffers */
1009   virtio_free_used_device_desc (vm, vring, node->node_index, packed);
1010
1011   if (vif->type == VIRTIO_IF_TYPE_TAP)
1012     n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1013                                          VIRTIO_IF_TYPE_TAP,
1014                                          &buffers[n_vectors - n_left],
1015                                          n_left, packed);
1016   else if (vif->type == VIRTIO_IF_TYPE_PCI)
1017     n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1018                                          VIRTIO_IF_TYPE_PCI,
1019                                          &buffers[n_vectors - n_left],
1020                                          n_left, packed);
1021   else if (vif->type == VIRTIO_IF_TYPE_TUN)
1022     n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1023                                          VIRTIO_IF_TYPE_TUN,
1024                                          &buffers[n_vectors - n_left],
1025                                          n_left, packed);
1026   else
1027     ASSERT (0);
1028
1029   if (n_left && retry_count--)
1030     goto retry;
1031
1032   if (vif->packet_buffering && n_left)
1033     {
1034       u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1035                                                              &buffers
1036                                                              [n_vectors
1037                                                               - n_left],
1038                                                              n_left);
1039       n_left -= n_buffered;
1040     }
1041   if (n_left)
1042     virtio_interface_drop_inline (vm, vif, node->node_index,
1043                                   &buffers[n_vectors - n_left], n_left,
1044                                   VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1045
1046   clib_spinlock_unlock_if_init (&vring->lockp);
1047
1048   return frame->n_vectors - n_left;
1049 }
1050
1051 static void
1052 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1053                                 u32 node_index)
1054 {
1055   virtio_main_t *apm = &virtio_main;
1056   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1057   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1058
1059   /* Shut off redirection */
1060   if (node_index == ~0)
1061     {
1062       vif->per_interface_next_index = node_index;
1063       return;
1064     }
1065
1066   vif->per_interface_next_index =
1067     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1068                         node_index);
1069 }
1070
1071 static void
1072 virtio_clear_hw_interface_counters (u32 instance)
1073 {
1074   /* Nothing for now */
1075 }
1076
1077 static void
1078 virtio_set_rx_interrupt (virtio_if_t *vif, virtio_vring_t *vring)
1079 {
1080   if (vif->is_packed)
1081     vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1082   else
1083     vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1084 }
1085
1086 static void
1087 virtio_set_rx_polling (virtio_if_t *vif, virtio_vring_t *vring)
1088 {
1089   if (vif->is_packed)
1090     vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1091   else
1092     vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1093 }
1094
1095 static clib_error_t *
1096 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
1097                                  vnet_hw_if_rx_mode mode)
1098 {
1099   vlib_main_t *vm = vnm->vlib_main;
1100   virtio_main_t *mm = &virtio_main;
1101   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1102   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1103   virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
1104
1105   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1106     {
1107       virtio_set_rx_polling (vif, rx_vring);
1108       return clib_error_return (0, "interrupt mode is not supported");
1109     }
1110
1111   if (mode == VNET_HW_IF_RX_MODE_POLLING)
1112     {
1113       if (vif->packet_coalesce || vif->packet_buffering)
1114         {
1115           if (mm->interrupt_queues_count > 0)
1116             mm->interrupt_queues_count--;
1117           if (mm->interrupt_queues_count == 0)
1118             vlib_process_signal_event (vm,
1119                                        virtio_send_interrupt_node.index,
1120                                        VIRTIO_EVENT_STOP_TIMER, 0);
1121         }
1122       virtio_set_rx_polling (vif, rx_vring);
1123     }
1124   else
1125     {
1126       if (vif->packet_coalesce || vif->packet_buffering)
1127         {
1128           mm->interrupt_queues_count++;
1129           if (mm->interrupt_queues_count == 1)
1130             vlib_process_signal_event (vm,
1131                                        virtio_send_interrupt_node.index,
1132                                        VIRTIO_EVENT_START_TIMER, 0);
1133         }
1134       virtio_set_rx_interrupt (vif, rx_vring);
1135     }
1136
1137   rx_vring->mode = mode;
1138
1139   return 0;
1140 }
1141
1142 static clib_error_t *
1143 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1144 {
1145   virtio_main_t *mm = &virtio_main;
1146   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1147   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1148
1149   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1150     {
1151       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1152       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1153                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1154     }
1155   else
1156     {
1157       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1158       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1159     }
1160   return 0;
1161 }
1162
1163 static clib_error_t *
1164 virtio_subif_add_del_function (vnet_main_t * vnm,
1165                                u32 hw_if_index,
1166                                struct vnet_sw_interface_t *st, int is_add)
1167 {
1168   /* Nothing for now */
1169   return 0;
1170 }
1171
1172 /* *INDENT-OFF* */
1173 VNET_DEVICE_CLASS (virtio_device_class) = {
1174   .name = "virtio",
1175   .format_device_name = format_virtio_device_name,
1176   .format_device = format_virtio_device,
1177   .format_tx_trace = format_virtio_tx_trace,
1178   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
1179   .tx_function_error_strings = virtio_tx_func_error_strings,
1180   .rx_redirect_to_node = virtio_set_interface_next_node,
1181   .clear_counters = virtio_clear_hw_interface_counters,
1182   .admin_up_down_function = virtio_interface_admin_up_down,
1183   .subif_add_del_function = virtio_subif_add_del_function,
1184   .rx_mode_change_function = virtio_interface_rx_mode_change,
1185 };
1186
1187 /* *INDENT-ON* */
1188
1189 /*
1190  * fd.io coding-style-patch-verification: ON
1191  *
1192  * Local Variables:
1193  * eval: (c-set-style "gnu")
1194  * End:
1195  */