gso: add vxlan tunnel support
[vpp.git] / src / vnet / devices / virtio / vhost_user_output.c
1 /*
2  *------------------------------------------------------------------
3  * vhost-user-output
4  *
5  * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <stddef.h>
21 #include <fcntl.h>              /* for open */
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/uio.h>            /* for iovec */
28 #include <netinet/in.h>
29 #include <sys/vfs.h>
30
31 #include <linux/if_arp.h>
32 #include <linux/if_tun.h>
33
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36
37 #include <vnet/ip/ip.h>
38
39 #include <vnet/ethernet/ethernet.h>
40 #include <vnet/devices/devices.h>
41 #include <vnet/feature/feature.h>
42
43 #include <vnet/devices/virtio/virtio.h>
44 #include <vnet/devices/virtio/vhost_user.h>
45 #include <vnet/devices/virtio/vhost_user_inline.h>
46
47 #include <vnet/gso/hdr_offset_parser.h>
48 /*
49  * On the transmit side, we keep processing the buffers from vlib in the while
50  * loop and prepare the copy order to be executed later. However, the static
51  * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
52  * entries. In order to not corrupt memory, we have to do the copy when the
53  * static array reaches the copy threshold. We subtract 40 in case the code
54  * goes into the inner loop for a maximum of 64k frames which may require
55  * more array entries. We subtract 200 because our default buffer size is
56  * 2048 and the default desc len is likely 1536. While it takes less than 40
57  * vlib buffers for the jumbo frame, it may take twice as much descriptors
58  * for the same jumbo frame. Use 200 for the extra head room.
59  */
60 #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 200)
61
62 extern vnet_device_class_t vhost_user_device_class;
63
64 #define foreach_vhost_user_tx_func_error      \
65   _(NONE, "no error")  \
66   _(NOT_READY, "vhost vring not ready")  \
67   _(DOWN, "vhost interface is down")  \
68   _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)")  \
69   _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)")  \
70   _(MMAP_FAIL, "mmap failure") \
71   _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
72
73 typedef enum
74 {
75 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
76   foreach_vhost_user_tx_func_error
77 #undef _
78     VHOST_USER_TX_FUNC_N_ERROR,
79 } vhost_user_tx_func_error_t;
80
81 static __clib_unused char *vhost_user_tx_func_error_strings[] = {
82 #define _(n,s) s,
83   foreach_vhost_user_tx_func_error
84 #undef _
85 };
86
87 static __clib_unused u8 *
88 format_vhost_user_interface_name (u8 * s, va_list * args)
89 {
90   u32 i = va_arg (*args, u32);
91   u32 show_dev_instance = ~0;
92   vhost_user_main_t *vum = &vhost_user_main;
93
94   if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
95     show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
96
97   if (show_dev_instance != ~0)
98     i = show_dev_instance;
99
100   s = format (s, "VirtualEthernet0/0/%d", i);
101   return s;
102 }
103
104 static __clib_unused int
105 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
106 {
107   // FIXME: check if the new dev instance is already used
108   vhost_user_main_t *vum = &vhost_user_main;
109   vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
110                                               hi->dev_instance);
111
112   vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
113                            hi->dev_instance, ~0);
114
115   vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
116     new_dev_instance;
117
118   vu_log_debug (vui, "renumbered vhost-user interface dev_instance %d to %d",
119                 hi->dev_instance, new_dev_instance);
120
121   return 0;
122 }
123
124 /**
125  * @brief Try once to lock the vring
126  * @return 0 on success, non-zero on failure.
127  */
128 static_always_inline int
129 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
130 {
131   return clib_atomic_test_and_set (vui->vring_locks[qid]);
132 }
133
134 /**
135  * @brief Spin until the vring is successfully locked
136  */
137 static_always_inline void
138 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
139 {
140   while (vhost_user_vring_try_lock (vui, qid))
141     ;
142 }
143
144 /**
145  * @brief Unlock the vring lock
146  */
147 static_always_inline void
148 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
149 {
150   clib_atomic_release (vui->vring_locks[qid]);
151 }
152
153 static_always_inline void
154 vhost_user_tx_trace (vhost_trace_t * t,
155                      vhost_user_intf_t * vui, u16 qid,
156                      vlib_buffer_t * b, vhost_user_vring_t * rxvq)
157 {
158   vhost_user_main_t *vum = &vhost_user_main;
159   u32 last_avail_idx = rxvq->last_avail_idx;
160   u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
161   vring_desc_t *hdr_desc = 0;
162   u32 hint = 0;
163
164   clib_memset (t, 0, sizeof (*t));
165   t->device_index = vui - vum->vhost_user_interfaces;
166   t->qid = qid;
167
168   hdr_desc = &rxvq->desc[desc_current];
169   if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
170     {
171       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
172       /* Header is the first here */
173       hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
174     }
175   if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
176     {
177       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
178     }
179   if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
180       !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
181     {
182       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
183     }
184
185   t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
186 }
187
188 static_always_inline u32
189 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
190                     u16 copy_len, u32 * map_hint)
191 {
192   void *dst0, *dst1, *dst2, *dst3;
193   if (PREDICT_TRUE (copy_len >= 4))
194     {
195       if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
196         return 1;
197       if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
198         return 1;
199       while (PREDICT_TRUE (copy_len >= 4))
200         {
201           dst0 = dst2;
202           dst1 = dst3;
203
204           if (PREDICT_FALSE
205               (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
206             return 1;
207           if (PREDICT_FALSE
208               (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
209             return 1;
210
211           CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
212           CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
213
214           clib_memcpy_fast (dst0, (void *) cpy[0].src, cpy[0].len);
215           clib_memcpy_fast (dst1, (void *) cpy[1].src, cpy[1].len);
216
217           vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
218           vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
219           copy_len -= 2;
220           cpy += 2;
221         }
222     }
223   while (copy_len)
224     {
225       if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
226         return 1;
227       clib_memcpy_fast (dst0, (void *) cpy->src, cpy->len);
228       vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
229       copy_len -= 1;
230       cpy += 1;
231     }
232   return 0;
233 }
234
235 static_always_inline void
236 vhost_user_handle_tx_offload (vhost_user_intf_t * vui, vlib_buffer_t * b,
237                               virtio_net_hdr_t * hdr)
238 {
239   generic_header_offset_t gho = { 0 };
240   vnet_generic_header_offset_parser (b, &gho);
241   if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
242     {
243       ip4_header_t *ip4;
244
245       ip4 =
246         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
247       ip4->checksum = ip4_header_checksum (ip4);
248     }
249
250   /* checksum offload */
251   if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
252     {
253       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
254       hdr->csum_start = gho.l4_hdr_offset;
255       hdr->csum_offset = offsetof (udp_header_t, checksum);
256       udp_header_t *udp =
257         (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
258       udp->checksum = 0;
259     }
260   else if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
261     {
262       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
263       hdr->csum_start = gho.l4_hdr_offset;
264       hdr->csum_offset = offsetof (tcp_header_t, checksum);
265       tcp_header_t *tcp =
266         (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
267       tcp->checksum = 0;
268     }
269
270   /* GSO offload */
271   if (b->flags & VNET_BUFFER_F_GSO)
272     {
273       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
274         {
275           if ((b->flags & VNET_BUFFER_F_IS_IP4) &&
276               (vui->features & (1ULL << FEAT_VIRTIO_NET_F_GUEST_TSO4)))
277             {
278               hdr->gso_size = vnet_buffer2 (b)->gso_size;
279               hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
280             }
281           else if ((b->flags & VNET_BUFFER_F_IS_IP6) &&
282                    (vui->features & (1ULL << FEAT_VIRTIO_NET_F_GUEST_TSO6)))
283             {
284               hdr->gso_size = vnet_buffer2 (b)->gso_size;
285               hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
286             }
287         }
288       else if ((vui->features & (1ULL << FEAT_VIRTIO_NET_F_GUEST_UFO)) &&
289                (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
290         {
291           hdr->gso_size = vnet_buffer2 (b)->gso_size;
292           hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
293         }
294     }
295 }
296
297 VNET_DEVICE_CLASS_TX_FN (vhost_user_device_class) (vlib_main_t * vm,
298                                                    vlib_node_runtime_t *
299                                                    node, vlib_frame_t * frame)
300 {
301   u32 *buffers = vlib_frame_vector_args (frame);
302   u32 n_left = frame->n_vectors;
303   vhost_user_main_t *vum = &vhost_user_main;
304   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
305   vhost_user_intf_t *vui =
306     pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
307   u32 qid = ~0;
308   vhost_user_vring_t *rxvq;
309   u8 error;
310   u32 thread_index = vm->thread_index;
311   vhost_cpu_t *cpu = &vum->cpus[thread_index];
312   u32 map_hint = 0;
313   u8 retry = 8;
314   u16 copy_len;
315   u16 tx_headers_len;
316   u32 or_flags;
317
318   if (PREDICT_FALSE (!vui->admin_up))
319     {
320       error = VHOST_USER_TX_FUNC_ERROR_DOWN;
321       goto done3;
322     }
323
324   if (PREDICT_FALSE (!vui->is_ready))
325     {
326       error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
327       goto done3;
328     }
329
330   qid = VHOST_VRING_IDX_RX (*vec_elt_at_index (vui->per_cpu_tx_qid,
331                                                thread_index));
332   rxvq = &vui->vrings[qid];
333   if (PREDICT_FALSE (rxvq->avail == 0))
334     {
335       error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
336       goto done3;
337     }
338
339   if (PREDICT_FALSE (vui->use_tx_spinlock))
340     vhost_user_vring_lock (vui, qid);
341
342 retry:
343   error = VHOST_USER_TX_FUNC_ERROR_NONE;
344   tx_headers_len = 0;
345   copy_len = 0;
346   while (n_left > 0)
347     {
348       vlib_buffer_t *b0, *current_b0;
349       u16 desc_head, desc_index, desc_len;
350       vring_desc_t *desc_table;
351       uword buffer_map_addr;
352       u32 buffer_len;
353       u16 bytes_left;
354
355       if (PREDICT_TRUE (n_left > 1))
356         vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
357
358       b0 = vlib_get_buffer (vm, buffers[0]);
359
360       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
361         {
362           cpu->current_trace = vlib_add_trace (vm, node, b0,
363                                                sizeof (*cpu->current_trace));
364           vhost_user_tx_trace (cpu->current_trace, vui, qid / 2, b0, rxvq);
365         }
366
367       if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
368         {
369           error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
370           goto done;
371         }
372
373       desc_table = rxvq->desc;
374       desc_head = desc_index =
375         rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
376
377       /* Go deeper in case of indirect descriptor
378        * I don't know of any driver providing indirect for RX. */
379       if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
380         {
381           if (PREDICT_FALSE
382               (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
383             {
384               error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
385               goto done;
386             }
387           if (PREDICT_FALSE
388               (!(desc_table =
389                  map_guest_mem (vui, rxvq->desc[desc_index].addr,
390                                 &map_hint))))
391             {
392               error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
393               goto done;
394             }
395           desc_index = 0;
396         }
397
398       desc_len = vui->virtio_net_hdr_sz;
399       buffer_map_addr = desc_table[desc_index].addr;
400       buffer_len = desc_table[desc_index].len;
401
402       {
403         // Get a header from the header array
404         virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len];
405         tx_headers_len++;
406         hdr->hdr.flags = 0;
407         hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
408         hdr->num_buffers = 1;   //This is local, no need to check
409
410         or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) ||
411           (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) ||
412           (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM);
413
414         /* Guest supports csum offload and buffer requires checksum offload? */
415         if (or_flags
416             && (vui->features & (1ULL << FEAT_VIRTIO_NET_F_GUEST_CSUM)))
417           vhost_user_handle_tx_offload (vui, b0, &hdr->hdr);
418
419         // Prepare a copy order executed later for the header
420         ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
421         vhost_copy_t *cpy = &cpu->copy[copy_len];
422         copy_len++;
423         cpy->len = vui->virtio_net_hdr_sz;
424         cpy->dst = buffer_map_addr;
425         cpy->src = (uword) hdr;
426       }
427
428       buffer_map_addr += vui->virtio_net_hdr_sz;
429       buffer_len -= vui->virtio_net_hdr_sz;
430       bytes_left = b0->current_length;
431       current_b0 = b0;
432       while (1)
433         {
434           if (buffer_len == 0)
435             {                   //Get new output
436               if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
437                 {
438                   //Next one is chained
439                   desc_index = desc_table[desc_index].next;
440                   buffer_map_addr = desc_table[desc_index].addr;
441                   buffer_len = desc_table[desc_index].len;
442                 }
443               else if (vui->virtio_net_hdr_sz == 12)    //MRG is available
444                 {
445                   virtio_net_hdr_mrg_rxbuf_t *hdr =
446                     &cpu->tx_headers[tx_headers_len - 1];
447
448                   //Move from available to used buffer
449                   rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
450                     desc_head;
451                   rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
452                     desc_len;
453                   vhost_user_log_dirty_ring (vui, rxvq,
454                                              ring[rxvq->last_used_idx &
455                                                   rxvq->qsz_mask]);
456
457                   rxvq->last_avail_idx++;
458                   rxvq->last_used_idx++;
459                   hdr->num_buffers++;
460                   desc_len = 0;
461
462                   if (PREDICT_FALSE
463                       (rxvq->last_avail_idx == rxvq->avail->idx))
464                     {
465                       //Dequeue queued descriptors for this packet
466                       rxvq->last_used_idx -= hdr->num_buffers - 1;
467                       rxvq->last_avail_idx -= hdr->num_buffers - 1;
468                       error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
469                       goto done;
470                     }
471
472                   desc_table = rxvq->desc;
473                   desc_head = desc_index =
474                     rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
475                   if (PREDICT_FALSE
476                       (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
477                     {
478                       //It is seriously unlikely that a driver will put indirect descriptor
479                       //after non-indirect descriptor.
480                       if (PREDICT_FALSE
481                           (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
482                         {
483                           error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
484                           goto done;
485                         }
486                       if (PREDICT_FALSE
487                           (!(desc_table =
488                              map_guest_mem (vui,
489                                             rxvq->desc[desc_index].addr,
490                                             &map_hint))))
491                         {
492                           error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
493                           goto done;
494                         }
495                       desc_index = 0;
496                     }
497                   buffer_map_addr = desc_table[desc_index].addr;
498                   buffer_len = desc_table[desc_index].len;
499                 }
500               else
501                 {
502                   error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
503                   goto done;
504                 }
505             }
506
507           {
508             ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
509             vhost_copy_t *cpy = &cpu->copy[copy_len];
510             copy_len++;
511             cpy->len = bytes_left;
512             cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
513             cpy->dst = buffer_map_addr;
514             cpy->src = (uword) vlib_buffer_get_current (current_b0) +
515               current_b0->current_length - bytes_left;
516
517             bytes_left -= cpy->len;
518             buffer_len -= cpy->len;
519             buffer_map_addr += cpy->len;
520             desc_len += cpy->len;
521
522             CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
523           }
524
525           // Check if vlib buffer has more data. If not, get more or break.
526           if (PREDICT_TRUE (!bytes_left))
527             {
528               if (PREDICT_FALSE
529                   (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
530                 {
531                   current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
532                   bytes_left = current_b0->current_length;
533                 }
534               else
535                 {
536                   //End of packet
537                   break;
538                 }
539             }
540         }
541
542       //Move from available to used ring
543       rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
544       rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
545       vhost_user_log_dirty_ring (vui, rxvq,
546                                  ring[rxvq->last_used_idx & rxvq->qsz_mask]);
547       rxvq->last_avail_idx++;
548       rxvq->last_used_idx++;
549
550       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
551         {
552           cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1];
553         }
554
555       n_left--;                 //At the end for error counting when 'goto done' is invoked
556
557       /*
558        * Do the copy periodically to prevent
559        * cpu->copy array overflow and corrupt memory
560        */
561       if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
562         {
563           if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
564                                                  &map_hint)))
565             {
566               vlib_error_count (vm, node->node_index,
567                                 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
568             }
569           copy_len = 0;
570
571           /* give buffers back to driver */
572           CLIB_MEMORY_BARRIER ();
573           rxvq->used->idx = rxvq->last_used_idx;
574           vhost_user_log_dirty_ring (vui, rxvq, idx);
575         }
576       buffers++;
577     }
578
579 done:
580   //Do the memory copies
581   if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
582                                          &map_hint)))
583     {
584       vlib_error_count (vm, node->node_index,
585                         VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
586     }
587
588   CLIB_MEMORY_BARRIER ();
589   rxvq->used->idx = rxvq->last_used_idx;
590   vhost_user_log_dirty_ring (vui, rxvq, idx);
591
592   /*
593    * When n_left is set, error is always set to something too.
594    * In case error is due to lack of remaining buffers, we go back up and
595    * retry.
596    * The idea is that it is better to waste some time on packets
597    * that have been processed already than dropping them and get
598    * more fresh packets with a good likelihood that they will be dropped too.
599    * This technique also gives more time to VM driver to pick-up packets.
600    * In case the traffic flows from physical to virtual interfaces, this
601    * technique will end-up leveraging the physical NIC buffer in order to
602    * absorb the VM's CPU jitter.
603    */
604   if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
605     {
606       retry--;
607       goto retry;
608     }
609
610   /* interrupt (call) handling */
611   if ((rxvq->callfd_idx != ~0) &&
612       !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
613     {
614       rxvq->n_since_last_int += frame->n_vectors - n_left;
615
616       if (rxvq->n_since_last_int > vum->coalesce_frames)
617         vhost_user_send_call (vm, rxvq);
618     }
619
620   vhost_user_vring_unlock (vui, qid);
621
622 done3:
623   if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
624     {
625       vlib_error_count (vm, node->node_index, error, n_left);
626       vlib_increment_simple_counter
627         (vnet_main.interface_main.sw_if_counters
628          + VNET_INTERFACE_COUNTER_DROP,
629          thread_index, vui->sw_if_index, n_left);
630     }
631
632   vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
633   return frame->n_vectors;
634 }
635
636 static __clib_unused clib_error_t *
637 vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
638                                      u32 qid, vnet_hw_interface_rx_mode mode)
639 {
640   vlib_main_t *vm = vnm->vlib_main;
641   vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
642   vhost_user_main_t *vum = &vhost_user_main;
643   vhost_user_intf_t *vui =
644     pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
645   vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
646
647   if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
648       (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
649     {
650       if (txvq->kickfd_idx == ~0)
651         {
652           // We cannot support interrupt mode if the driver opts out
653           return clib_error_return (0, "Driver does not support interrupt");
654         }
655       if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
656         {
657           vum->ifq_count++;
658           // Start the timer if this is the first encounter on interrupt
659           // interface/queue
660           if ((vum->ifq_count == 1) &&
661               (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
662             vlib_process_signal_event (vm,
663                                        vhost_user_send_interrupt_node.index,
664                                        VHOST_USER_EVENT_START_TIMER, 0);
665         }
666     }
667   else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
668     {
669       if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
670            (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
671           vum->ifq_count)
672         {
673           vum->ifq_count--;
674           // Stop the timer if there is no more interrupt interface/queue
675           if ((vum->ifq_count == 0) &&
676               (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
677             vlib_process_signal_event (vm,
678                                        vhost_user_send_interrupt_node.index,
679                                        VHOST_USER_EVENT_STOP_TIMER, 0);
680         }
681     }
682
683   txvq->mode = mode;
684   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
685     txvq->used->flags = VRING_USED_F_NO_NOTIFY;
686   else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
687            (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
688     txvq->used->flags = 0;
689   else
690     {
691       vu_log_err (vui, "unhandled mode %d changed for if %d queue %d", mode,
692                   hw_if_index, qid);
693       return clib_error_return (0, "unsupported");
694     }
695
696   return 0;
697 }
698
699 static __clib_unused clib_error_t *
700 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
701                                     u32 flags)
702 {
703   vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
704   vhost_user_main_t *vum = &vhost_user_main;
705   vhost_user_intf_t *vui =
706     pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
707   u8 link_old, link_new;
708
709   link_old = vui_is_link_up (vui);
710
711   vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
712
713   link_new = vui_is_link_up (vui);
714
715   if (link_old != link_new)
716     vnet_hw_interface_set_flags (vnm, vui->hw_if_index, link_new ?
717                                  VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
718
719   return /* no error */ 0;
720 }
721
722 /* *INDENT-OFF* */
723 VNET_DEVICE_CLASS (vhost_user_device_class) = {
724   .name = "vhost-user",
725   .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
726   .tx_function_error_strings = vhost_user_tx_func_error_strings,
727   .format_device_name = format_vhost_user_interface_name,
728   .name_renumber = vhost_user_name_renumber,
729   .admin_up_down_function = vhost_user_interface_admin_up_down,
730   .rx_mode_change_function = vhost_user_interface_rx_mode_change,
731   .format_tx_trace = format_vhost_trace,
732 };
733
734 /* *INDENT-ON* */
735
736 /*
737  * fd.io coding-style-patch-verification: ON
738  *
739  * Local Variables:
740  * eval: (c-set-style "gnu")
741  * End:
742  */