virtio: add packet buffering on tx
[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   vlib_buffer_t buffer;
78   generic_header_offset_t gho;
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_always_inline void
103 virtio_tx_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
104                  virtio_if_type_t type, vlib_buffer_t * b0, u32 bi)
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 (type == VIRTIO_IF_TYPE_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_always_inline void
139 virtio_interface_drop_inline (vlib_main_t * vm, uword node_index,
140                               u32 * buffers, u16 n,
141                               virtio_tx_func_error_t error)
142 {
143   vlib_error_count (vm, node_index, error, n);
144   vlib_buffer_free (vm, buffers, n);
145 }
146
147 static_always_inline void
148 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
149 {
150   ASSERT (n_buffers <= ring_size);
151
152   if (PREDICT_TRUE (start + n_buffers <= ring_size))
153     {
154       clib_memset_u32 (ring + start, ~0, n_buffers);
155     }
156   else
157     {
158       clib_memset_u32 (ring + start, ~0, ring_size - start);
159       clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
160     }
161 }
162
163 static_always_inline void
164 virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
165                               uword node_index)
166 {
167   u16 used = vring->desc_in_use;
168   u16 sz = vring->size;
169   u16 mask = sz - 1;
170   u16 last = vring->last_used_idx;
171   u16 n_left = vring->used->idx - last;
172   u16 out_of_order_count = 0;
173
174   if (n_left == 0)
175     return;
176
177   while (n_left)
178     {
179       vring_used_elem_t *e = &vring->used->ring[last & mask];
180       u16 slot, n_buffers;
181       slot = n_buffers = e->id;
182
183       while (e->id == (n_buffers & mask))
184         {
185           n_left--;
186           last++;
187           n_buffers++;
188           vring_desc_t *d = &vring->desc[e->id];
189           u16 next;
190           while (d->flags & VRING_DESC_F_NEXT)
191             {
192               n_buffers++;
193               next = d->next;
194               d = &vring->desc[next];
195             }
196           if (n_left == 0)
197             break;
198           e = &vring->used->ring[last & mask];
199         }
200       vlib_buffer_free_from_ring (vm, vring->buffers, slot,
201                                   sz, (n_buffers - slot));
202       virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
203       used -= (n_buffers - slot);
204
205       if (n_left > 0)
206         {
207           vlib_buffer_free (vm, &vring->buffers[e->id], 1);
208           vring->buffers[e->id] = ~0;
209           used--;
210           last++;
211           n_left--;
212           out_of_order_count++;
213           vring->flags |= VRING_TX_OUT_OF_ORDER;
214         }
215     }
216
217   /*
218    * Some vhost-backends give buffers back in out-of-order fashion in used ring.
219    * It impacts the overall virtio-performance.
220    */
221   if (out_of_order_count)
222     vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
223                       out_of_order_count);
224
225   vring->desc_in_use = used;
226   vring->last_used_idx = last;
227 }
228
229 static_always_inline void
230 set_checksum_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2)
231 {
232   if (b->flags & VNET_BUFFER_F_IS_IP4)
233     {
234       ip4_header_t *ip4;
235       generic_header_offset_t gho = { 0 };
236       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
237                                          0 /* ip6 */ );
238       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
239       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
240       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
241         {
242           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
243         }
244       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
245         {
246           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
247         }
248
249       /*
250        * virtio devices do not support IP4 checksum offload. So driver takes care
251        * of it while doing tx.
252        */
253       ip4 =
254         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
255       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
256         ip4->checksum = ip4_header_checksum (ip4);
257     }
258   else if (b->flags & VNET_BUFFER_F_IS_IP6)
259     {
260       generic_header_offset_t gho = { 0 };
261       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
262                                          1 /* ip6 */ );
263       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
264       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
265       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
266         {
267           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
268         }
269       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
270         {
271           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
272         }
273     }
274 }
275
276 static_always_inline void
277 set_gso_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2)
278 {
279   if (b->flags & VNET_BUFFER_F_IS_IP4)
280     {
281       ip4_header_t *ip4;
282       generic_header_offset_t gho = { 0 };
283       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
284                                          0 /* ip6 */ );
285       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
286       hdr->gso_size = vnet_buffer2 (b)->gso_size;
287       hdr->hdr_len = gho.hdr_sz;
288       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
289       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
290       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
291       ip4 =
292         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
293       /*
294        * virtio devices do not support IP4 checksum offload. So driver takes care
295        * of it while doing tx.
296        */
297       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
298         ip4->checksum = ip4_header_checksum (ip4);
299     }
300   else if (b->flags & VNET_BUFFER_F_IS_IP6)
301     {
302       generic_header_offset_t gho = { 0 };
303       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
304                                          1 /* ip6 */ );
305       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
306       hdr->gso_size = vnet_buffer2 (b)->gso_size;
307       hdr->hdr_len = gho.hdr_sz;
308       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
309       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
310       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
311     }
312 }
313
314 static_always_inline u16
315 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
316                     virtio_if_type_t type, virtio_vring_t * vring,
317                     u32 bi, u16 free_desc_count,
318                     u16 avail, u16 next, u16 mask, int do_gso,
319                     int csum_offload, uword node_index)
320 {
321   u16 n_added = 0;
322   int hdr_sz = vif->virtio_net_hdr_sz;
323   vring_desc_t *d;
324   d = &vring->desc[next];
325   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
326   virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
327   int is_l2 = (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI));
328
329   clib_memset (hdr, 0, hdr_sz);
330
331   if (b->flags & VNET_BUFFER_F_GSO)
332     {
333       if (do_gso)
334         set_gso_offsets (b, hdr, is_l2);
335       else
336         {
337           virtio_interface_drop_inline (vm, node_index, &bi, 1,
338                                         VIRTIO_TX_ERROR_GSO_PACKET_DROP);
339           return n_added;
340         }
341     }
342   else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
343                        VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
344     {
345       if (csum_offload)
346         set_checksum_offsets (b, hdr, is_l2);
347       else
348         {
349           virtio_interface_drop_inline (vm, node_index, &bi, 1,
350                                         VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
351           return n_added;
352         }
353     }
354
355   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
356     {
357       d->addr =
358         ((type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
359                                                                     b) :
360          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
361       d->len = b->current_length + hdr_sz;
362       d->flags = 0;
363     }
364   else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
365     {
366       /*
367        * We are using single vlib_buffer_t for indirect descriptor(s)
368        * chain. Single descriptor is 16 bytes and vlib_buffer_t
369        * has 2048 bytes space. So maximum long chain can have 128
370        * (=2048/16) indirect descriptors.
371        * It can easily support 65535 bytes of Jumbo frames with
372        * each data buffer size of 512 bytes minimum.
373        */
374       u32 indirect_buffer = 0;
375       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
376         {
377           virtio_interface_drop_inline (vm, node_index, &bi, 1,
378                                         VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
379           return n_added;
380         }
381
382       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
383       indirect_desc->current_data = 0;
384       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
385       indirect_desc->next_buffer = bi;
386       bi = indirect_buffer;
387
388       vring_desc_t *id =
389         (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
390       u32 count = 1;
391       if (type == VIRTIO_IF_TYPE_PCI)
392         {
393           d->addr = vlib_physmem_get_pa (vm, id);
394           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
395
396           /*
397            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
398            * should be presented in separate descriptor and data will start
399            * from next descriptor.
400            */
401           if (PREDICT_TRUE
402               (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
403             id->len = b->current_length + hdr_sz;
404           else
405             {
406               id->len = hdr_sz;
407               id->flags = VRING_DESC_F_NEXT;
408               id->next = count;
409               count++;
410               id++;
411               id->addr = vlib_buffer_get_current_pa (vm, b);
412               id->len = b->current_length;
413             }
414           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
415             {
416               id->flags = VRING_DESC_F_NEXT;
417               id->next = count;
418               count++;
419               id++;
420               b = vlib_get_buffer (vm, b->next_buffer);
421               id->addr = vlib_buffer_get_current_pa (vm, b);
422               id->len = b->current_length;
423             }
424         }
425       else                      /* VIRTIO_IF_TYPE_[TAP | TUN] */
426         {
427           d->addr = pointer_to_uword (id);
428           /* first buffer in chain */
429           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
430           id->len = b->current_length + hdr_sz;
431
432           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
433             {
434               id->flags = VRING_DESC_F_NEXT;
435               id->next = count;
436               count++;
437               id++;
438               b = vlib_get_buffer (vm, b->next_buffer);
439               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
440               id->len = b->current_length;
441             }
442         }
443       id->flags = 0;
444       id->next = 0;
445       d->len = count * sizeof (vring_desc_t);
446       d->flags = VRING_DESC_F_INDIRECT;
447     }
448   else if (type == VIRTIO_IF_TYPE_PCI)
449     {
450       u16 count = next;
451       vlib_buffer_t *b_temp = b;
452       u16 n_buffers_in_chain = 1;
453
454       /*
455        * Check the length of the chain for the required number of
456        * descriptors. Return from here, retry to get more descriptors,
457        * if chain length is greater than available descriptors.
458        */
459       while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
460         {
461           n_buffers_in_chain++;
462           b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
463         }
464
465       if (n_buffers_in_chain > free_desc_count)
466         return n_buffers_in_chain;
467
468       d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
469       d->len = b->current_length + hdr_sz;
470
471       while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
472         {
473           d->flags = VRING_DESC_F_NEXT;
474           vring->buffers[count] = bi;
475           b->flags &=
476             ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
477           bi = b->next_buffer;
478           b->next_buffer = 0;
479           n_added++;
480           count = (count + 1) & mask;
481           d->next = count;
482           d = &vring->desc[count];
483           b = vlib_get_buffer (vm, bi);
484           d->addr = vlib_buffer_get_current_pa (vm, b);
485           d->len = b->current_length;
486         }
487       d->flags = 0;
488       vring->buffers[count] = bi;
489       vring->avail->ring[avail & mask] = next;
490       n_added++;
491       return n_added;
492     }
493   else
494     {
495       ASSERT (0);
496     }
497   vring->buffers[next] = bi;
498   vring->avail->ring[avail & mask] = next;
499   n_added++;
500   return n_added;
501 }
502
503 static_always_inline void
504 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
505                        u16 req, u16 next, u32 * first_free_desc_index,
506                        u16 * free_desc_count)
507 {
508   u16 start = 0;
509   /* next is used as hint: from where to start looking */
510   for (u16 i = 0; i < size; i++, next++)
511     {
512       if (vring->buffers[next & mask] == ~0)
513         {
514           if (*first_free_desc_index == ~0)
515             {
516               *first_free_desc_index = (next & mask);
517               start = i;
518               (*free_desc_count)++;
519               req--;
520               if (req == 0)
521                 break;
522             }
523           else
524             {
525               if (start + *free_desc_count == i)
526                 {
527                   (*free_desc_count)++;
528                   req--;
529                   if (req == 0)
530                     break;
531                 }
532               else
533                 break;
534             }
535         }
536     }
537 }
538
539 static_always_inline uword
540 virtio_interface_tx_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
541                                 vlib_frame_t * frame, virtio_if_t * vif,
542                                 virtio_if_type_t type, int do_gso,
543                                 int csum_offload, int do_gro)
544 {
545   u16 n_left = frame->n_vectors;
546   virtio_vring_t *vring;
547   u16 qid = vm->thread_index % vif->num_txqs;
548   vring = vec_elt_at_index (vif->txq_vrings, qid);
549   u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
550   u16 sz = vring->size;
551   u16 mask = sz - 1;
552   u16 retry_count = 2;
553   u32 *buffers = vlib_frame_vector_args (frame);
554   u32 to[GRO_TO_VECTOR_SIZE (n_left)];
555
556   clib_spinlock_lock_if_init (&vring->lockp);
557
558   if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0 &&
559       (vring->last_kick_avail_idx != vring->avail->idx))
560     virtio_kick (vm, vring, vif);
561
562   if (do_gro)
563     {
564       n_left = vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
565       buffers = to;
566     }
567
568 retry:
569   /* free consumed buffers */
570   virtio_free_used_device_desc (vm, vring, node->node_index);
571
572   used = vring->desc_in_use;
573   next = vring->desc_next;
574   avail = vring->avail->idx;
575
576   u16 free_desc_count = 0;
577
578   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
579     {
580       u32 first_free_desc_index = ~0;
581
582       virtio_find_free_desc (vring, sz, mask, n_left, next,
583                              &first_free_desc_index, &free_desc_count);
584
585       if (free_desc_count)
586         next = first_free_desc_index;
587     }
588   else
589     free_desc_count = sz - used;
590
591   if (vif->packet_buffering)
592     {
593       n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
594
595       while (n_buffers_left && free_desc_count)
596         {
597           u16 n_added = 0;
598
599           u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
600           if (bi == ~0)
601             break;
602           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
603           if (b0->flags & VLIB_BUFFER_IS_TRACED)
604             {
605               virtio_tx_trace (vm, node, type, b0, buffers[0]);
606             }
607           n_added =
608             add_buffer_to_slot (vm, vif, type, vring, bi, free_desc_count,
609                                 avail, next, mask, do_gso, csum_offload,
610                                 node->node_index);
611           if (PREDICT_FALSE (n_added == 0))
612             {
613               n_buffers_left--;
614               continue;
615             }
616           else if (PREDICT_FALSE (n_added > free_desc_count))
617             break;
618
619           avail++;
620           next = (next + n_added) & mask;
621           used += n_added;
622           n_buffers_left--;
623           free_desc_count -= n_added;
624         }
625     }
626
627   while (n_left && free_desc_count)
628     {
629       u16 n_added = 0;
630
631       vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
632       if (b0->flags & VLIB_BUFFER_IS_TRACED)
633         {
634           virtio_tx_trace (vm, node, type, b0, buffers[0]);
635         }
636       n_added =
637         add_buffer_to_slot (vm, vif, type, vring, buffers[0], free_desc_count,
638                             avail, next, mask, do_gso, csum_offload,
639                             node->node_index);
640
641       if (PREDICT_FALSE (n_added == 0))
642         {
643           buffers++;
644           n_left--;
645           continue;
646         }
647       else if (PREDICT_FALSE (n_added > free_desc_count))
648         break;
649
650       avail++;
651       next = (next + n_added) & mask;
652       used += n_added;
653       buffers++;
654       n_left--;
655       free_desc_count -= n_added;
656     }
657
658   if (n_left != frame->n_vectors || n_buffers != n_buffers_left)
659     {
660       CLIB_MEMORY_STORE_BARRIER ();
661       vring->avail->idx = avail;
662       vring->desc_next = next;
663       vring->desc_in_use = used;
664       if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0)
665         virtio_kick (vm, vring, vif);
666     }
667
668   if (n_left)
669     {
670       if (retry_count--)
671         goto retry;
672
673       if (vif->packet_buffering)
674         {
675
676           u16 n_buffered =
677             virtio_vring_buffering_store_packets (vring->buffering, buffers,
678                                                   n_left);
679           buffers += n_buffered;
680           n_left -= n_buffered;
681         }
682       if (n_left)
683         virtio_interface_drop_inline (vm, node->node_index,
684                                       buffers, n_left,
685                                       VIRTIO_TX_ERROR_NO_FREE_SLOTS);
686     }
687
688   clib_spinlock_unlock_if_init (&vring->lockp);
689
690   return frame->n_vectors - n_left;
691 }
692
693 static_always_inline uword
694 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
695                             vlib_frame_t * frame, virtio_if_t * vif,
696                             virtio_if_type_t type)
697 {
698   vnet_main_t *vnm = vnet_get_main ();
699   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
700
701   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
702     return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
703                                            1 /* do_gso */ ,
704                                            1 /* checksum offload */ ,
705                                            vif->packet_coalesce);
706   else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
707     return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
708                                            0 /* no do_gso */ ,
709                                            1 /* checksum offload */ ,
710                                            0 /* do_gro */ );
711   else
712     return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
713                                            0 /* no do_gso */ ,
714                                            0 /* no checksum offload */ ,
715                                            0 /* do_gro */ );
716 }
717
718 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
719                                                vlib_node_runtime_t * node,
720                                                vlib_frame_t * frame)
721 {
722   virtio_main_t *nm = &virtio_main;
723   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
724   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
725
726   if (vif->type == VIRTIO_IF_TYPE_TAP)
727     return virtio_interface_tx_inline (vm, node, frame, vif,
728                                        VIRTIO_IF_TYPE_TAP);
729   else if (vif->type == VIRTIO_IF_TYPE_PCI)
730     return virtio_interface_tx_inline (vm, node, frame, vif,
731                                        VIRTIO_IF_TYPE_PCI);
732   else if (vif->type == VIRTIO_IF_TYPE_TUN)
733     return virtio_interface_tx_inline (vm, node, frame, vif,
734                                        VIRTIO_IF_TYPE_TUN);
735   else
736     ASSERT (0);
737
738   return 0;
739 }
740
741 static void
742 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
743                                 u32 node_index)
744 {
745   virtio_main_t *apm = &virtio_main;
746   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
747   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
748
749   /* Shut off redirection */
750   if (node_index == ~0)
751     {
752       vif->per_interface_next_index = node_index;
753       return;
754     }
755
756   vif->per_interface_next_index =
757     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
758                         node_index);
759 }
760
761 static void
762 virtio_clear_hw_interface_counters (u32 instance)
763 {
764   /* Nothing for now */
765 }
766
767 static clib_error_t *
768 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
769                                  vnet_hw_interface_rx_mode mode)
770 {
771   virtio_main_t *mm = &virtio_main;
772   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
773   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
774   virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
775   virtio_vring_t *tx_vring = 0;
776
777   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
778     {
779       rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
780       return clib_error_return (0, "interrupt mode is not supported");
781     }
782
783   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
784     {
785       vec_foreach (tx_vring, vif->txq_vrings)
786       {
787         /* only enable packet coalesce in poll mode */
788         gro_flow_table_set_is_enable (tx_vring->flow_table, 1);
789         /* only enable packet buffering in poll mode */
790         virtio_vring_buffering_set_is_enable (tx_vring->buffering, 1);
791       }
792       rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
793     }
794   else
795     {
796       if (vif->packet_coalesce || vif->packet_buffering)
797         {
798           virtio_log_warning (vif,
799                               "interface %U is in interrupt mode, disabling packet coalescing or buffering",
800                               format_vnet_sw_if_index_name, vnet_get_main (),
801                               vif->sw_if_index);
802           vec_foreach (tx_vring, vif->txq_vrings)
803           {
804             gro_flow_table_set_is_enable (tx_vring->flow_table, 0);
805             virtio_vring_buffering_set_is_enable (tx_vring->buffering, 0);
806           }
807         }
808       rx_vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
809     }
810
811   return 0;
812 }
813
814 static clib_error_t *
815 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
816 {
817   virtio_main_t *mm = &virtio_main;
818   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
819   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
820
821   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
822     {
823       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
824       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
825                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
826     }
827   else
828     {
829       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
830       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
831     }
832   return 0;
833 }
834
835 static clib_error_t *
836 virtio_subif_add_del_function (vnet_main_t * vnm,
837                                u32 hw_if_index,
838                                struct vnet_sw_interface_t *st, int is_add)
839 {
840   /* Nothing for now */
841   return 0;
842 }
843
844 /* *INDENT-OFF* */
845 VNET_DEVICE_CLASS (virtio_device_class) = {
846   .name = "virtio",
847   .format_device_name = format_virtio_device_name,
848   .format_device = format_virtio_device,
849   .format_tx_trace = format_virtio_tx_trace,
850   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
851   .tx_function_error_strings = virtio_tx_func_error_strings,
852   .rx_redirect_to_node = virtio_set_interface_next_node,
853   .clear_counters = virtio_clear_hw_interface_counters,
854   .admin_up_down_function = virtio_interface_admin_up_down,
855   .subif_add_del_function = virtio_subif_add_del_function,
856   .rx_mode_change_function = virtio_interface_rx_mode_change,
857 };
858
859 /* *INDENT-ON* */
860
861 /*
862  * fd.io coding-style-patch-verification: ON
863  *
864  * Local Variables:
865  * eval: (c-set-style "gnu")
866  * End:
867  */