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