interface: rx queue infra rework, part one
[vpp.git] / src / vnet / devices / virtio / vhost_user_input.c
1 /*
2  *------------------------------------------------------------------
3  * vhost-user-input
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 <fcntl.h>              /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/uio.h>            /* for iovec */
27 #include <netinet/in.h>
28 #include <sys/vfs.h>
29
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
32
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 #include <vnet/udp/udp_packet.h>
40 #include <vnet/interface/rx_queue_funcs.h>
41
42 #include <vnet/devices/virtio/vhost_user.h>
43 #include <vnet/devices/virtio/vhost_user_inline.h>
44
45 /*
46  * When an RX queue is down but active, received packets
47  * must be discarded. This value controls up to how many
48  * packets will be discarded during each round.
49  */
50 #define VHOST_USER_DOWN_DISCARD_COUNT 256
51
52 /*
53  * When the number of available buffers gets under this threshold,
54  * RX node will start discarding packets.
55  */
56 #define VHOST_USER_RX_BUFFER_STARVATION 32
57
58 /*
59  * On the receive side, the host should free descriptors as soon
60  * as possible in order to avoid TX drop in the VM.
61  * This value controls the number of copy operations that are stacked
62  * before copy is done for all and descriptors are given back to
63  * the guest.
64  * The value 64 was obtained by testing (48 and 128 were not as good).
65  */
66 #define VHOST_USER_RX_COPY_THRESHOLD 64
67
68 extern vlib_node_registration_t vhost_user_input_node;
69
70 #define foreach_vhost_user_input_func_error      \
71   _(NO_ERROR, "no error")  \
72   _(NO_BUFFER, "no available buffer")  \
73   _(MMAP_FAIL, "mmap failure")  \
74   _(INDIRECT_OVERFLOW, "indirect descriptor overflows table")  \
75   _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
76   _(NOT_READY, "vhost interface not ready or down") \
77   _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
78
79 typedef enum
80 {
81 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
82   foreach_vhost_user_input_func_error
83 #undef _
84     VHOST_USER_INPUT_FUNC_N_ERROR,
85 } vhost_user_input_func_error_t;
86
87 static __clib_unused char *vhost_user_input_func_error_strings[] = {
88 #define _(n,s) s,
89   foreach_vhost_user_input_func_error
90 #undef _
91 };
92
93 static_always_inline void
94 vhost_user_rx_trace (vhost_trace_t * t,
95                      vhost_user_intf_t * vui, u16 qid,
96                      vlib_buffer_t * b, vhost_user_vring_t * txvq,
97                      u16 last_avail_idx)
98 {
99   vhost_user_main_t *vum = &vhost_user_main;
100   u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask];
101   vring_desc_t *hdr_desc = 0;
102   virtio_net_hdr_mrg_rxbuf_t *hdr;
103   u32 hint = 0;
104
105   clib_memset (t, 0, sizeof (*t));
106   t->device_index = vui - vum->vhost_user_interfaces;
107   t->qid = qid;
108
109   hdr_desc = &txvq->desc[desc_current];
110   if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
111     {
112       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
113       /* Header is the first here */
114       hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
115     }
116   if (txvq->desc[desc_current].flags & VRING_DESC_F_NEXT)
117     {
118       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
119     }
120   if (!(txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) &&
121       !(txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT))
122     {
123       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
124     }
125
126   t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
127
128   if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
129     {
130       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
131     }
132   else
133     {
134       u32 len = vui->virtio_net_hdr_sz;
135       memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
136     }
137 }
138
139 static_always_inline u32
140 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
141                        u16 copy_len, u32 * map_hint)
142 {
143   void *src0, *src1, *src2, *src3;
144   if (PREDICT_TRUE (copy_len >= 4))
145     {
146       if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
147         return 1;
148       if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
149         return 1;
150
151       while (PREDICT_TRUE (copy_len >= 4))
152         {
153           src0 = src2;
154           src1 = src3;
155
156           if (PREDICT_FALSE
157               (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
158             return 1;
159           if (PREDICT_FALSE
160               (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
161             return 1;
162
163           CLIB_PREFETCH (src2, 64, LOAD);
164           CLIB_PREFETCH (src3, 64, LOAD);
165
166           clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len);
167           clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len);
168           copy_len -= 2;
169           cpy += 2;
170         }
171     }
172   while (copy_len)
173     {
174       if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
175         return 1;
176       clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len);
177       copy_len -= 1;
178       cpy += 1;
179     }
180   return 0;
181 }
182
183 /**
184  * Try to discard packets from the tx ring (VPP RX path).
185  * Returns the number of discarded packets.
186  */
187 static_always_inline u32
188 vhost_user_rx_discard_packet (vlib_main_t * vm,
189                               vhost_user_intf_t * vui,
190                               vhost_user_vring_t * txvq, u32 discard_max)
191 {
192   /*
193    * On the RX side, each packet corresponds to one descriptor
194    * (it is the same whether it is a shallow descriptor, chained, or indirect).
195    * Therefore, discarding a packet is like discarding a descriptor.
196    */
197   u32 discarded_packets = 0;
198   u32 avail_idx = txvq->avail->idx;
199   u16 mask = txvq->qsz_mask;
200   u16 last_avail_idx = txvq->last_avail_idx;
201   u16 last_used_idx = txvq->last_used_idx;
202   while (discarded_packets != discard_max)
203     {
204       if (avail_idx == last_avail_idx)
205         goto out;
206
207       u16 desc_chain_head = txvq->avail->ring[last_avail_idx & mask];
208       last_avail_idx++;
209       txvq->used->ring[last_used_idx & mask].id = desc_chain_head;
210       txvq->used->ring[last_used_idx & mask].len = 0;
211       vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]);
212       last_used_idx++;
213       discarded_packets++;
214     }
215
216 out:
217   txvq->last_avail_idx = last_avail_idx;
218   txvq->last_used_idx = last_used_idx;
219   CLIB_MEMORY_STORE_BARRIER ();
220   txvq->used->idx = txvq->last_used_idx;
221   vhost_user_log_dirty_ring (vui, txvq, idx);
222   return discarded_packets;
223 }
224
225 /*
226  * In case of overflow, we need to rewind the array of allocated buffers.
227  */
228 static_always_inline void
229 vhost_user_input_rewind_buffers (vlib_main_t * vm,
230                                  vhost_cpu_t * cpu, vlib_buffer_t * b_head)
231 {
232   u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
233   vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
234   b_current->current_length = 0;
235   b_current->flags = 0;
236   while (b_current != b_head)
237     {
238       cpu->rx_buffers_len++;
239       bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
240       b_current = vlib_get_buffer (vm, bi_current);
241       b_current->current_length = 0;
242       b_current->flags = 0;
243     }
244   cpu->rx_buffers_len++;
245 }
246
247 static_always_inline void
248 vhost_user_handle_rx_offload (vlib_buffer_t * b0, u8 * b0_data,
249                               virtio_net_hdr_t * hdr)
250 {
251   u8 l4_hdr_sz = 0;
252   u8 l4_proto = 0;
253   ethernet_header_t *eh = (ethernet_header_t *) b0_data;
254   u16 ethertype = clib_net_to_host_u16 (eh->type);
255   u16 l2hdr_sz = sizeof (ethernet_header_t);
256
257   if (ethernet_frame_is_tagged (ethertype))
258     {
259       ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
260
261       ethertype = clib_net_to_host_u16 (vlan->type);
262       l2hdr_sz += sizeof (*vlan);
263       if (ethertype == ETHERNET_TYPE_VLAN)
264         {
265           vlan++;
266           ethertype = clib_net_to_host_u16 (vlan->type);
267           l2hdr_sz += sizeof (*vlan);
268         }
269     }
270   vnet_buffer (b0)->l2_hdr_offset = 0;
271   vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
272   vnet_buffer (b0)->l4_hdr_offset = hdr->csum_start;
273   b0->flags |= (VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
274                 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
275                 VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
276
277   if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
278     {
279       ip4_header_t *ip4 = (ip4_header_t *) (b0_data + l2hdr_sz);
280       l4_proto = ip4->protocol;
281       b0->flags |= VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
282     }
283   else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
284     {
285       ip6_header_t *ip6 = (ip6_header_t *) (b0_data + l2hdr_sz);
286       l4_proto = ip6->protocol;
287       b0->flags |= VNET_BUFFER_F_IS_IP6;
288     }
289
290   if (l4_proto == IP_PROTOCOL_TCP)
291     {
292       tcp_header_t *tcp = (tcp_header_t *)
293         (b0_data + vnet_buffer (b0)->l4_hdr_offset);
294       l4_hdr_sz = tcp_header_bytes (tcp);
295       b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
296     }
297   else if (l4_proto == IP_PROTOCOL_UDP)
298     {
299       l4_hdr_sz = sizeof (udp_header_t);
300       b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
301     }
302
303   if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP)
304     {
305       vnet_buffer2 (b0)->gso_size = hdr->gso_size;
306       vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
307       b0->flags |= VNET_BUFFER_F_GSO;
308     }
309   else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4)
310     {
311       vnet_buffer2 (b0)->gso_size = hdr->gso_size;
312       vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
313       b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4);
314     }
315   else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6)
316     {
317       vnet_buffer2 (b0)->gso_size = hdr->gso_size;
318       vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
319       b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6);
320     }
321 }
322
323 static_always_inline void
324 vhost_user_input_do_interrupt (vlib_main_t * vm, vhost_user_intf_t * vui,
325                                vhost_user_vring_t * txvq,
326                                vhost_user_vring_t * rxvq)
327 {
328   f64 now = vlib_time_now (vm);
329
330   if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
331     vhost_user_send_call (vm, vui, txvq);
332
333   if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
334     vhost_user_send_call (vm, vui, rxvq);
335 }
336
337 static_always_inline void
338 vhost_user_input_setup_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
339                               vhost_user_intf_t * vui,
340                               u32 * current_config_index, u32 * next_index,
341                               u32 ** to_next, u32 * n_left_to_next)
342 {
343   vnet_feature_main_t *fm = &feature_main;
344   u8 feature_arc_idx = fm->device_input_feature_arc_index;
345
346   if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index)))
347     {
348       vnet_feature_config_main_t *cm;
349       cm = &fm->feature_config_mains[feature_arc_idx];
350       *current_config_index = vec_elt (cm->config_index_by_sw_if_index,
351                                        vui->sw_if_index);
352       vnet_get_config_data (&cm->config_main, current_config_index,
353                             next_index, 0);
354     }
355
356   vlib_get_new_next_frame (vm, node, *next_index, *to_next, *n_left_to_next);
357
358   if (*next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT)
359     {
360       /* give some hints to ethernet-input */
361       vlib_next_frame_t *nf;
362       vlib_frame_t *f;
363       ethernet_input_frame_t *ef;
364       nf = vlib_node_runtime_get_next_frame (vm, node, *next_index);
365       f = vlib_get_frame (vm, nf->frame);
366       f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
367
368       ef = vlib_frame_scalar_args (f);
369       ef->sw_if_index = vui->sw_if_index;
370       ef->hw_if_index = vui->hw_if_index;
371       vlib_frame_no_append (f);
372     }
373 }
374
375 static_always_inline u32
376 vhost_user_if_input (vlib_main_t *vm, vhost_user_main_t *vum,
377                      vhost_user_intf_t *vui, u16 qid,
378                      vlib_node_runtime_t *node, u8 enable_csum)
379 {
380   vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
381   vnet_feature_main_t *fm = &feature_main;
382   u16 n_rx_packets = 0;
383   u32 n_rx_bytes = 0;
384   u16 n_left;
385   u32 n_left_to_next, *to_next;
386   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
387   u32 n_trace = vlib_get_trace_count (vm, node);
388   u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
389   u32 map_hint = 0;
390   vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
391   u16 copy_len = 0;
392   u8 feature_arc_idx = fm->device_input_feature_arc_index;
393   u32 current_config_index = ~(u32) 0;
394   u16 mask = txvq->qsz_mask;
395
396   /* The descriptor table is not ready yet */
397   if (PREDICT_FALSE (txvq->avail == 0))
398     goto done;
399
400   {
401     /* do we have pending interrupts ? */
402     vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
403     vhost_user_input_do_interrupt (vm, vui, txvq, rxvq);
404   }
405
406   /*
407    * For adaptive mode, it is optimized to reduce interrupts.
408    * If the scheduler switches the input node to polling due
409    * to burst of traffic, we tell the driver no interrupt.
410    * When the traffic subsides, the scheduler switches the node back to
411    * interrupt mode. We must tell the driver we want interrupt.
412    */
413   if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
414     {
415       if ((node->flags &
416            VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
417           !(node->flags &
418             VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
419         /* Tell driver we want notification */
420         txvq->used->flags = 0;
421       else
422         /* Tell driver we don't want notification */
423         txvq->used->flags = VRING_USED_F_NO_NOTIFY;
424     }
425
426   if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
427     goto done;
428
429   n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
430
431   /* nothing to do */
432   if (PREDICT_FALSE (n_left == 0))
433     goto done;
434
435   if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
436     {
437       /*
438        * Discard input packet if interface is admin down or vring is not
439        * enabled.
440        * "For example, for a networking device, in the disabled state
441        * client must not supply any new RX packets, but must process
442        * and discard any TX packets."
443        */
444       vhost_user_rx_discard_packet (vm, vui, txvq,
445                                     VHOST_USER_DOWN_DISCARD_COUNT);
446       goto done;
447     }
448
449   if (PREDICT_FALSE (n_left == (mask + 1)))
450     {
451       /*
452        * Informational error logging when VPP is not
453        * receiving packets fast enough.
454        */
455       vlib_error_count (vm, node->node_index,
456                         VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
457     }
458
459   if (n_left > VLIB_FRAME_SIZE)
460     n_left = VLIB_FRAME_SIZE;
461
462   /*
463    * For small packets (<2kB), we will not need more than one vlib buffer
464    * per packet. In case packets are bigger, we will just yield at some point
465    * in the loop and come back later. This is not an issue as for big packet,
466    * processing cost really comes from the memory copy.
467    * The assumption is that big packets will fit in 40 buffers.
468    */
469   if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 ||
470                      cpu->rx_buffers_len < 40))
471     {
472       u32 curr_len = cpu->rx_buffers_len;
473       cpu->rx_buffers_len +=
474         vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len,
475                            VHOST_USER_RX_BUFFERS_N - curr_len);
476
477       if (PREDICT_FALSE
478           (cpu->rx_buffers_len < VHOST_USER_RX_BUFFER_STARVATION))
479         {
480           /* In case of buffer starvation, discard some packets from the queue
481            * and log the event.
482            * We keep doing best effort for the remaining packets. */
483           u32 flush = (n_left + 1 > cpu->rx_buffers_len) ?
484             n_left + 1 - cpu->rx_buffers_len : 1;
485           flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
486
487           n_left -= flush;
488           vlib_increment_simple_counter (vnet_main.
489                                          interface_main.sw_if_counters +
490                                          VNET_INTERFACE_COUNTER_DROP,
491                                          vm->thread_index, vui->sw_if_index,
492                                          flush);
493
494           vlib_error_count (vm, vhost_user_input_node.index,
495                             VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
496         }
497     }
498
499   vhost_user_input_setup_frame (vm, node, vui, &current_config_index,
500                                 &next_index, &to_next, &n_left_to_next);
501
502   u16 last_avail_idx = txvq->last_avail_idx;
503   u16 last_used_idx = txvq->last_used_idx;
504
505   while (n_left > 0)
506     {
507       vlib_buffer_t *b_head, *b_current;
508       u32 bi_current;
509       u16 desc_current;
510       u32 desc_data_offset;
511       vring_desc_t *desc_table = txvq->desc;
512
513       if (PREDICT_FALSE (cpu->rx_buffers_len <= 1))
514         {
515           /* Not enough rx_buffers
516            * Note: We yeld on 1 so we don't need to do an additional
517            * check for the next buffer prefetch.
518            */
519           n_left = 0;
520           break;
521         }
522
523       desc_current = txvq->avail->ring[last_avail_idx & mask];
524       cpu->rx_buffers_len--;
525       bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
526       b_head = b_current = vlib_get_buffer (vm, bi_current);
527       to_next[0] = bi_current;  //We do that now so we can forget about bi_current
528       to_next++;
529       n_left_to_next--;
530
531       vlib_prefetch_buffer_with_index
532         (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD);
533
534       /* Just preset the used descriptor id and length for later */
535       txvq->used->ring[last_used_idx & mask].id = desc_current;
536       txvq->used->ring[last_used_idx & mask].len = 0;
537       vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]);
538
539       /* The buffer should already be initialized */
540       b_head->total_length_not_including_first_buffer = 0;
541       b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
542
543       if (PREDICT_FALSE
544           (n_trace > 0 && vlib_trace_buffer (vm, node, next_index, b_head,
545                                              /* follow_chain */ 0)))
546         {
547           vhost_trace_t *t0 =
548             vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
549           vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx);
550           n_trace--;
551           vlib_set_trace_count (vm, node, n_trace);
552         }
553
554       /* This depends on the setup but is very consistent
555        * So I think the CPU branch predictor will make a pretty good job
556        * at optimizing the decision. */
557       if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
558         {
559           desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
560                                       &map_hint);
561           desc_current = 0;
562           if (PREDICT_FALSE (desc_table == 0))
563             {
564               vlib_error_count (vm, node->node_index,
565                                 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
566               goto out;
567             }
568         }
569
570       desc_data_offset = vui->virtio_net_hdr_sz;
571
572       if (enable_csum)
573         {
574           virtio_net_hdr_mrg_rxbuf_t *hdr;
575           u8 *b_data;
576           u16 current;
577
578           hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint);
579           if (PREDICT_FALSE (hdr == 0))
580             {
581               vlib_error_count (vm, node->node_index,
582                                 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
583               goto out;
584             }
585           if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
586             {
587               if ((desc_data_offset == desc_table[desc_current].len) &&
588                   (desc_table[desc_current].flags & VRING_DESC_F_NEXT))
589                 {
590                   current = desc_table[desc_current].next;
591                   b_data = map_guest_mem (vui, desc_table[current].addr,
592                                           &map_hint);
593                   if (PREDICT_FALSE (b_data == 0))
594                     {
595                       vlib_error_count (vm, node->node_index,
596                                         VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL,
597                                         1);
598                       goto out;
599                     }
600                 }
601               else
602                 b_data = (u8 *) hdr + desc_data_offset;
603
604               vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
605             }
606         }
607
608       while (1)
609         {
610           /* Get more input if necessary. Or end of packet. */
611           if (desc_data_offset == desc_table[desc_current].len)
612             {
613               if (PREDICT_FALSE (desc_table[desc_current].flags &
614                                  VRING_DESC_F_NEXT))
615                 {
616                   desc_current = desc_table[desc_current].next;
617                   desc_data_offset = 0;
618                 }
619               else
620                 {
621                   goto out;
622                 }
623             }
624
625           /* Get more output if necessary. Or end of packet. */
626           if (PREDICT_FALSE (b_current->current_length == buffer_data_size))
627             {
628               if (PREDICT_FALSE (cpu->rx_buffers_len == 0))
629                 {
630                   /* Cancel speculation */
631                   to_next--;
632                   n_left_to_next++;
633
634                   /*
635                    * Checking if there are some left buffers.
636                    * If not, just rewind the used buffers and stop.
637                    * Note: Scheduled copies are not cancelled. This is
638                    * not an issue as they would still be valid. Useless,
639                    * but valid.
640                    */
641                   vhost_user_input_rewind_buffers (vm, cpu, b_head);
642                   n_left = 0;
643                   goto stop;
644                 }
645
646               /* Get next output */
647               cpu->rx_buffers_len--;
648               u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len];
649               b_current->next_buffer = bi_next;
650               b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
651               bi_current = bi_next;
652               b_current = vlib_get_buffer (vm, bi_current);
653             }
654
655           /* Prepare a copy order executed later for the data */
656           ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
657           vhost_copy_t *cpy = &cpu->copy[copy_len];
658           copy_len++;
659           u32 desc_data_l = desc_table[desc_current].len - desc_data_offset;
660           cpy->len = buffer_data_size - b_current->current_length;
661           cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
662           cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
663                               b_current->current_length);
664           cpy->src = desc_table[desc_current].addr + desc_data_offset;
665
666           desc_data_offset += cpy->len;
667
668           b_current->current_length += cpy->len;
669           b_head->total_length_not_including_first_buffer += cpy->len;
670         }
671
672     out:
673
674       n_rx_bytes += b_head->total_length_not_including_first_buffer;
675       n_rx_packets++;
676
677       b_head->total_length_not_including_first_buffer -=
678         b_head->current_length;
679
680       /* consume the descriptor and return it as used */
681       last_avail_idx++;
682       last_used_idx++;
683
684       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
685
686       vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
687       vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
688       b_head->error = 0;
689
690       if (current_config_index != ~(u32) 0)
691         {
692           b_head->current_config_index = current_config_index;
693           vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
694         }
695
696       n_left--;
697
698       /*
699        * Although separating memory copies from virtio ring parsing
700        * is beneficial, we can offer to perform the copies from time
701        * to time in order to free some space in the ring.
702        */
703       if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
704         {
705           if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy,
706                                                     copy_len, &map_hint)))
707             {
708               vlib_error_count (vm, node->node_index,
709                                 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
710             }
711           copy_len = 0;
712
713           /* give buffers back to driver */
714           CLIB_MEMORY_STORE_BARRIER ();
715           txvq->used->idx = last_used_idx;
716           vhost_user_log_dirty_ring (vui, txvq, idx);
717         }
718     }
719 stop:
720   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
721
722   txvq->last_used_idx = last_used_idx;
723   txvq->last_avail_idx = last_avail_idx;
724
725   /* Do the memory copies */
726   if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len,
727                                             &map_hint)))
728     {
729       vlib_error_count (vm, node->node_index,
730                         VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
731     }
732
733   /* give buffers back to driver */
734   CLIB_MEMORY_STORE_BARRIER ();
735   txvq->used->idx = txvq->last_used_idx;
736   vhost_user_log_dirty_ring (vui, txvq, idx);
737
738   /* interrupt (call) handling */
739   if ((txvq->callfd_idx != ~0) &&
740       !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
741     {
742       txvq->n_since_last_int += n_rx_packets;
743
744       if (txvq->n_since_last_int > vum->coalesce_frames)
745         vhost_user_send_call (vm, vui, txvq);
746     }
747
748   /* increase rx counters */
749   vlib_increment_combined_counter
750     (vnet_main.interface_main.combined_sw_if_counters
751      + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index,
752      n_rx_packets, n_rx_bytes);
753
754   vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets);
755
756 done:
757   return n_rx_packets;
758 }
759
760 static_always_inline void
761 vhost_user_mark_desc_consumed (vhost_user_intf_t * vui,
762                                vhost_user_vring_t * txvq, u16 desc_head,
763                                u16 n_descs_processed)
764 {
765   vring_packed_desc_t *desc_table = txvq->packed_desc;
766   u16 desc_idx;
767   u16 mask = txvq->qsz_mask;
768
769   for (desc_idx = 0; desc_idx < n_descs_processed; desc_idx++)
770     {
771       if (txvq->used_wrap_counter)
772         desc_table[(desc_head + desc_idx) & mask].flags |=
773           (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
774       else
775         desc_table[(desc_head + desc_idx) & mask].flags &=
776           ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
777       vhost_user_advance_last_used_idx (txvq);
778     }
779 }
780
781 static_always_inline void
782 vhost_user_rx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui,
783                             u16 qid, vhost_user_vring_t * txvq,
784                             u16 desc_current)
785 {
786   vhost_user_main_t *vum = &vhost_user_main;
787   vring_packed_desc_t *hdr_desc;
788   virtio_net_hdr_mrg_rxbuf_t *hdr;
789   u32 hint = 0;
790
791   clib_memset (t, 0, sizeof (*t));
792   t->device_index = vui - vum->vhost_user_interfaces;
793   t->qid = qid;
794
795   hdr_desc = &txvq->packed_desc[desc_current];
796   if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)
797     {
798       t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
799       /* Header is the first here */
800       hdr_desc = map_guest_mem (vui, txvq->packed_desc[desc_current].addr,
801                                 &hint);
802     }
803   if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT)
804     t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
805
806   if (!(txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) &&
807       !(txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT))
808     t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
809
810   t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
811
812   if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
813     t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
814   else
815     {
816       u32 len = vui->virtio_net_hdr_sz;
817       clib_memcpy_fast (&t->hdr, hdr,
818                         len > hdr_desc->len ? hdr_desc->len : len);
819     }
820 }
821
822 static_always_inline u32
823 vhost_user_rx_discard_packet_packed (vlib_main_t * vm,
824                                      vhost_user_intf_t * vui,
825                                      vhost_user_vring_t * txvq,
826                                      u32 discard_max)
827 {
828   u32 discarded_packets = 0;
829   u16 mask = txvq->qsz_mask;
830   u16 desc_current, desc_head;
831
832   desc_head = desc_current = txvq->last_used_idx & mask;
833
834   /*
835    * On the RX side, each packet corresponds to one descriptor
836    * (it is the same whether it is a shallow descriptor, chained, or indirect).
837    * Therefore, discarding a packet is like discarding a descriptor.
838    */
839   while ((discarded_packets != discard_max) &&
840          vhost_user_packed_desc_available (txvq, desc_current))
841     {
842       vhost_user_advance_last_avail_idx (txvq);
843       discarded_packets++;
844       desc_current = (desc_current + 1) & mask;
845     }
846
847   if (PREDICT_TRUE (discarded_packets))
848     vhost_user_mark_desc_consumed (vui, txvq, desc_head, discarded_packets);
849   return (discarded_packets);
850 }
851
852 static_always_inline u32
853 vhost_user_input_copy_packed (vhost_user_intf_t * vui, vhost_copy_t * cpy,
854                               u16 copy_len, u32 * map_hint)
855 {
856   void *src0, *src1, *src2, *src3, *src4, *src5, *src6, *src7;
857   u8 bad;
858   u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
859
860   if (PREDICT_TRUE (copy_len >= 8))
861     {
862       src4 = map_guest_mem (vui, cpy[0].src, map_hint);
863       src5 = map_guest_mem (vui, cpy[1].src, map_hint);
864       src6 = map_guest_mem (vui, cpy[2].src, map_hint);
865       src7 = map_guest_mem (vui, cpy[3].src, map_hint);
866       bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
867       if (PREDICT_FALSE (bad))
868         goto one_by_one;
869       CLIB_PREFETCH (src4, 64, LOAD);
870       CLIB_PREFETCH (src5, 64, LOAD);
871       CLIB_PREFETCH (src6, 64, LOAD);
872       CLIB_PREFETCH (src7, 64, LOAD);
873
874       while (PREDICT_TRUE (copy_len >= 8))
875         {
876           src0 = src4;
877           src1 = src5;
878           src2 = src6;
879           src3 = src7;
880
881           src4 = map_guest_mem (vui, cpy[4].src, map_hint);
882           src5 = map_guest_mem (vui, cpy[5].src, map_hint);
883           src6 = map_guest_mem (vui, cpy[6].src, map_hint);
884           src7 = map_guest_mem (vui, cpy[7].src, map_hint);
885           bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
886           if (PREDICT_FALSE (bad))
887             break;
888
889           CLIB_PREFETCH (src4, 64, LOAD);
890           CLIB_PREFETCH (src5, 64, LOAD);
891           CLIB_PREFETCH (src6, 64, LOAD);
892           CLIB_PREFETCH (src7, 64, LOAD);
893
894           clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len);
895           clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len);
896           clib_memcpy_fast ((void *) cpy[2].dst, src2, cpy[2].len);
897           clib_memcpy_fast ((void *) cpy[3].dst, src3, cpy[3].len);
898           copy_len -= 4;
899           cpy += 4;
900         }
901     }
902
903 one_by_one:
904   while (copy_len)
905     {
906       if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
907         {
908           rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
909           break;
910         }
911       clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len);
912       copy_len -= 1;
913       cpy += 1;
914     }
915   return rc;
916 }
917
918 static_always_inline u32
919 vhost_user_do_offload (vhost_user_intf_t * vui,
920                        vring_packed_desc_t * desc_table, u16 desc_current,
921                        u16 mask, vlib_buffer_t * b_head, u32 * map_hint)
922 {
923   u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
924   virtio_net_hdr_mrg_rxbuf_t *hdr;
925   u8 *b_data;
926   u32 desc_data_offset = vui->virtio_net_hdr_sz;
927
928   hdr = map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
929   if (PREDICT_FALSE (hdr == 0))
930     rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
931   else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
932     {
933       if (desc_data_offset == desc_table[desc_current].len)
934         {
935           desc_current = (desc_current + 1) & mask;
936           b_data =
937             map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
938           if (PREDICT_FALSE (b_data == 0))
939             rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
940           else
941             vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
942         }
943       else
944         {
945           b_data = (u8 *) hdr + desc_data_offset;
946           vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
947         }
948     }
949
950   return rc;
951 }
952
953 static_always_inline u32
954 vhost_user_compute_buffers_required (u32 desc_len, u32 buffer_data_size)
955 {
956   div_t result;
957   u32 buffers_required;
958
959   if (PREDICT_TRUE (buffer_data_size == 2048))
960     {
961       buffers_required = desc_len >> 11;
962       if ((desc_len & 2047) != 0)
963         buffers_required++;
964       return (buffers_required);
965     }
966
967   if (desc_len < buffer_data_size)
968     return 1;
969
970   result = div (desc_len, buffer_data_size);
971   if (result.rem)
972     buffers_required = result.quot + 1;
973   else
974     buffers_required = result.quot;
975
976   return (buffers_required);
977 }
978
979 static_always_inline u32
980 vhost_user_compute_indirect_desc_len (vhost_user_intf_t * vui,
981                                       vhost_user_vring_t * txvq,
982                                       u32 buffer_data_size, u16 desc_current,
983                                       u32 * map_hint)
984 {
985   vring_packed_desc_t *desc_table = txvq->packed_desc;
986   u32 desc_len = 0;
987   u16 desc_data_offset = vui->virtio_net_hdr_sz;
988   u16 desc_idx = desc_current;
989   u32 n_descs;
990
991   n_descs = desc_table[desc_idx].len >> 4;
992   desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, map_hint);
993   if (PREDICT_FALSE (desc_table == 0))
994     return 0;
995
996   for (desc_idx = 0; desc_idx < n_descs; desc_idx++)
997     desc_len += desc_table[desc_idx].len;
998
999   if (PREDICT_TRUE (desc_len > desc_data_offset))
1000     desc_len -= desc_data_offset;
1001
1002   return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1003 }
1004
1005 static_always_inline u32
1006 vhost_user_compute_chained_desc_len (vhost_user_intf_t * vui,
1007                                      vhost_user_vring_t * txvq,
1008                                      u32 buffer_data_size, u16 * current,
1009                                      u16 * n_left)
1010 {
1011   vring_packed_desc_t *desc_table = txvq->packed_desc;
1012   u32 desc_len = 0;
1013   u16 mask = txvq->qsz_mask;
1014
1015   while (desc_table[*current].flags & VRING_DESC_F_NEXT)
1016     {
1017       desc_len += desc_table[*current].len;
1018       (*n_left)++;
1019       *current = (*current + 1) & mask;
1020       vhost_user_advance_last_avail_idx (txvq);
1021     }
1022   desc_len += desc_table[*current].len;
1023   (*n_left)++;
1024   *current = (*current + 1) & mask;
1025   vhost_user_advance_last_avail_idx (txvq);
1026
1027   if (PREDICT_TRUE (desc_len > vui->virtio_net_hdr_sz))
1028     desc_len -= vui->virtio_net_hdr_sz;
1029
1030   return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1031 }
1032
1033 static_always_inline void
1034 vhost_user_assemble_packet (vring_packed_desc_t * desc_table,
1035                             u16 * desc_idx, vlib_buffer_t * b_head,
1036                             vlib_buffer_t ** b_current, u32 ** next,
1037                             vlib_buffer_t *** b, u32 * bi_current,
1038                             vhost_cpu_t * cpu, u16 * copy_len,
1039                             u32 * buffers_used, u32 buffers_required,
1040                             u32 * desc_data_offset, u32 buffer_data_size,
1041                             u16 mask)
1042 {
1043   u32 desc_data_l;
1044
1045   while (*desc_data_offset < desc_table[*desc_idx].len)
1046     {
1047       /* Get more output if necessary. Or end of packet. */
1048       if (PREDICT_FALSE ((*b_current)->current_length == buffer_data_size))
1049         {
1050           /* Get next output */
1051           u32 bi_next = **next;
1052           (*next)++;
1053           (*b_current)->next_buffer = bi_next;
1054           (*b_current)->flags |= VLIB_BUFFER_NEXT_PRESENT;
1055           *bi_current = bi_next;
1056           *b_current = **b;
1057           (*b)++;
1058           (*buffers_used)++;
1059           ASSERT (*buffers_used <= buffers_required);
1060         }
1061
1062       /* Prepare a copy order executed later for the data */
1063       ASSERT (*copy_len < VHOST_USER_COPY_ARRAY_N);
1064       vhost_copy_t *cpy = &cpu->copy[*copy_len];
1065       (*copy_len)++;
1066       desc_data_l = desc_table[*desc_idx].len - *desc_data_offset;
1067       cpy->len = buffer_data_size - (*b_current)->current_length;
1068       cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1069       cpy->dst = (uword) (vlib_buffer_get_current (*b_current) +
1070                           (*b_current)->current_length);
1071       cpy->src = desc_table[*desc_idx].addr + *desc_data_offset;
1072
1073       *desc_data_offset += cpy->len;
1074
1075       (*b_current)->current_length += cpy->len;
1076       b_head->total_length_not_including_first_buffer += cpy->len;
1077     }
1078   *desc_idx = (*desc_idx + 1) & mask;;
1079   *desc_data_offset = 0;
1080 }
1081
1082 static_always_inline u32
1083 vhost_user_if_input_packed (vlib_main_t *vm, vhost_user_main_t *vum,
1084                             vhost_user_intf_t *vui, u16 qid,
1085                             vlib_node_runtime_t *node, u8 enable_csum)
1086 {
1087   vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1088   vnet_feature_main_t *fm = &feature_main;
1089   u8 feature_arc_idx = fm->device_input_feature_arc_index;
1090   u16 n_rx_packets = 0;
1091   u32 n_rx_bytes = 0;
1092   u16 n_left = 0;
1093   u32 buffers_required = 0;
1094   u32 n_left_to_next, *to_next;
1095   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1096   u32 n_trace = vlib_get_trace_count (vm, node);
1097   u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
1098   u32 map_hint = 0;
1099   vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
1100   u16 copy_len = 0;
1101   u32 current_config_index = ~0;
1102   u16 mask = txvq->qsz_mask;
1103   u16 desc_current, desc_head, last_used_idx;
1104   vring_packed_desc_t *desc_table = 0;
1105   u32 n_descs_processed = 0;
1106   u32 rv;
1107   vlib_buffer_t **b;
1108   u32 *next;
1109   u32 buffers_used = 0;
1110   u16 current, n_descs_to_process;
1111
1112   /* The descriptor table is not ready yet */
1113   if (PREDICT_FALSE (txvq->packed_desc == 0))
1114     goto done;
1115
1116   /* do we have pending interrupts ? */
1117   vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1118   vhost_user_input_do_interrupt (vm, vui, txvq, rxvq);
1119
1120   /*
1121    * For adaptive mode, it is optimized to reduce interrupts.
1122    * If the scheduler switches the input node to polling due
1123    * to burst of traffic, we tell the driver no interrupt.
1124    * When the traffic subsides, the scheduler switches the node back to
1125    * interrupt mode. We must tell the driver we want interrupt.
1126    */
1127   if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
1128     {
1129       if ((node->flags &
1130            VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
1131           !(node->flags &
1132             VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1133         /* Tell driver we want notification */
1134         txvq->used_event->flags = 0;
1135       else
1136         /* Tell driver we don't want notification */
1137         txvq->used_event->flags = VRING_EVENT_F_DISABLE;
1138     }
1139
1140   last_used_idx = txvq->last_used_idx & mask;
1141   desc_head = desc_current = last_used_idx;
1142
1143   if (vhost_user_packed_desc_available (txvq, desc_current) == 0)
1144     goto done;
1145
1146   if (PREDICT_FALSE (!vui->admin_up || !vui->is_ready || !(txvq->enabled)))
1147     {
1148       /*
1149        * Discard input packet if interface is admin down or vring is not
1150        * enabled.
1151        * "For example, for a networking device, in the disabled state
1152        * client must not supply any new RX packets, but must process
1153        * and discard any TX packets."
1154        */
1155       rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq,
1156                                                 VHOST_USER_DOWN_DISCARD_COUNT);
1157       vlib_error_count (vm, vhost_user_input_node.index,
1158                         VHOST_USER_INPUT_FUNC_ERROR_NOT_READY, rv);
1159       goto done;
1160     }
1161
1162   vhost_user_input_setup_frame (vm, node, vui, &current_config_index,
1163                                 &next_index, &to_next, &n_left_to_next);
1164
1165   /*
1166    * Compute n_left and total buffers needed
1167    */
1168   desc_table = txvq->packed_desc;
1169   current = desc_current;
1170   while (vhost_user_packed_desc_available (txvq, current) &&
1171          (n_left < VLIB_FRAME_SIZE))
1172     {
1173       if (desc_table[current].flags & VRING_DESC_F_INDIRECT)
1174         {
1175           buffers_required +=
1176             vhost_user_compute_indirect_desc_len (vui, txvq, buffer_data_size,
1177                                                   current, &map_hint);
1178           n_left++;
1179           current = (current + 1) & mask;
1180           vhost_user_advance_last_avail_idx (txvq);
1181         }
1182       else
1183         {
1184           buffers_required +=
1185             vhost_user_compute_chained_desc_len (vui, txvq, buffer_data_size,
1186                                                  &current, &n_left);
1187         }
1188     }
1189
1190   /* Something is broken if we need more than 10000 buffers */
1191   if (PREDICT_FALSE ((buffers_required == 0) || (buffers_required > 10000)))
1192     {
1193       rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1194       vlib_error_count (vm, vhost_user_input_node.index,
1195                         VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1196       goto done;
1197     }
1198
1199   vec_validate (cpu->to_next_list, buffers_required);
1200   rv = vlib_buffer_alloc (vm, cpu->to_next_list, buffers_required);
1201   if (PREDICT_FALSE (rv != buffers_required))
1202     {
1203       vlib_buffer_free (vm, cpu->to_next_list, rv);
1204       rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1205       vlib_error_count (vm, vhost_user_input_node.index,
1206                         VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1207       goto done;
1208     }
1209
1210   next = cpu->to_next_list;
1211   vec_validate (cpu->rx_buffers_pdesc, buffers_required);
1212   vlib_get_buffers (vm, next, cpu->rx_buffers_pdesc, buffers_required);
1213   b = cpu->rx_buffers_pdesc;
1214   n_descs_processed = n_left;
1215
1216   while (n_left)
1217     {
1218       vlib_buffer_t *b_head, *b_current;
1219       u32 bi_current;
1220       u32 desc_data_offset;
1221       u16 desc_idx = desc_current;
1222       u32 n_descs;
1223
1224       desc_table = txvq->packed_desc;
1225       to_next[0] = bi_current = next[0];
1226       b_head = b_current = b[0];
1227       b++;
1228       buffers_used++;
1229       ASSERT (buffers_used <= buffers_required);
1230       to_next++;
1231       next++;
1232       n_left_to_next--;
1233
1234       /* The buffer should already be initialized */
1235       b_head->total_length_not_including_first_buffer = 0;
1236       b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1237       desc_data_offset = vui->virtio_net_hdr_sz;
1238       n_descs_to_process = 1;
1239
1240       if (desc_table[desc_idx].flags & VRING_DESC_F_INDIRECT)
1241         {
1242           n_descs = desc_table[desc_idx].len >> 4;
1243           desc_table = map_guest_mem (vui, desc_table[desc_idx].addr,
1244                                       &map_hint);
1245           desc_idx = 0;
1246           if (PREDICT_FALSE (desc_table == 0) ||
1247               (enable_csum &&
1248                (PREDICT_FALSE
1249                 (vhost_user_do_offload
1250                  (vui, desc_table, desc_idx, mask, b_head,
1251                   &map_hint) != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))))
1252             {
1253               vlib_error_count (vm, node->node_index,
1254                                 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1255               to_next--;
1256               next--;
1257               n_left_to_next++;
1258               buffers_used--;
1259               b--;
1260               goto out;
1261             }
1262           while (n_descs)
1263             {
1264               vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1265                                           &b_current, &next, &b, &bi_current,
1266                                           cpu, &copy_len, &buffers_used,
1267                                           buffers_required, &desc_data_offset,
1268                                           buffer_data_size, mask);
1269               n_descs--;
1270             }
1271         }
1272       else
1273         {
1274           if (enable_csum)
1275             {
1276               rv = vhost_user_do_offload (vui, desc_table, desc_idx, mask,
1277                                           b_head, &map_hint);
1278               if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1279                 {
1280                   vlib_error_count (vm, node->node_index, rv, 1);
1281                   to_next--;
1282                   next--;
1283                   n_left_to_next++;
1284                   buffers_used--;
1285                   b--;
1286                   goto out;
1287                 }
1288             }
1289           /*
1290            * For chained descriptor, we process all chains in a single while
1291            * loop. So count how many descriptors in the chain.
1292            */
1293           n_descs_to_process = 1;
1294           while (desc_table[desc_idx].flags & VRING_DESC_F_NEXT)
1295             {
1296               vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1297                                           &b_current, &next, &b, &bi_current,
1298                                           cpu, &copy_len, &buffers_used,
1299                                           buffers_required, &desc_data_offset,
1300                                           buffer_data_size, mask);
1301               n_descs_to_process++;
1302             }
1303           vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1304                                       &b_current, &next, &b, &bi_current,
1305                                       cpu, &copy_len, &buffers_used,
1306                                       buffers_required, &desc_data_offset,
1307                                       buffer_data_size, mask);
1308         }
1309
1310       n_rx_bytes += b_head->total_length_not_including_first_buffer;
1311       n_rx_packets++;
1312
1313       b_head->total_length_not_including_first_buffer -=
1314         b_head->current_length;
1315
1316       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1317
1318       vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1319       vnet_buffer (b_head)->sw_if_index[VLIB_TX] = ~0;
1320       b_head->error = 0;
1321
1322       if (current_config_index != ~0)
1323         {
1324           b_head->current_config_index = current_config_index;
1325           vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
1326         }
1327
1328     out:
1329       ASSERT (n_left >= n_descs_to_process);
1330       n_left -= n_descs_to_process;
1331
1332       /* advance to next descrptor */
1333       desc_current = (desc_current + n_descs_to_process) & mask;
1334
1335       /*
1336        * Although separating memory copies from virtio ring parsing
1337        * is beneficial, we can offer to perform the copies from time
1338        * to time in order to free some space in the ring.
1339        */
1340       if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1341         {
1342           rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len,
1343                                              &map_hint);
1344           if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1345             vlib_error_count (vm, node->node_index, rv, 1);
1346           copy_len = 0;
1347         }
1348     }
1349   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1350
1351   /* Do the memory copies */
1352   rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, &map_hint);
1353   if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1354     vlib_error_count (vm, node->node_index, rv, 1);
1355
1356   /* Must do the tracing before giving buffers back to driver */
1357   if (PREDICT_FALSE (n_trace))
1358     {
1359       u32 left = n_rx_packets;
1360
1361       b = cpu->rx_buffers_pdesc;
1362       while (n_trace && left)
1363         {
1364           if (PREDICT_TRUE
1365               (vlib_trace_buffer
1366                (vm, node, next_index, b[0], /* follow_chain */ 0)))
1367             {
1368               vhost_trace_t *t0;
1369               t0 = vlib_add_trace (vm, node, b[0], sizeof (t0[0]));
1370               vhost_user_rx_trace_packed (t0, vui, qid, txvq, last_used_idx);
1371               last_used_idx = (last_used_idx + 1) & mask;
1372               n_trace--;
1373               vlib_set_trace_count (vm, node, n_trace);
1374             }
1375           left--;
1376           b++;
1377         }
1378     }
1379
1380   /*
1381    * Give buffers back to driver.
1382    */
1383   vhost_user_mark_desc_consumed (vui, txvq, desc_head, n_descs_processed);
1384
1385   /* interrupt (call) handling */
1386   if ((txvq->callfd_idx != ~0) &&
1387       (txvq->avail_event->flags != VRING_EVENT_F_DISABLE))
1388     {
1389       txvq->n_since_last_int += n_rx_packets;
1390       if (txvq->n_since_last_int > vum->coalesce_frames)
1391         vhost_user_send_call (vm, vui, txvq);
1392     }
1393
1394   /* increase rx counters */
1395   vlib_increment_combined_counter
1396     (vnet_main.interface_main.combined_sw_if_counters
1397      + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index,
1398      n_rx_packets, n_rx_bytes);
1399
1400   vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets);
1401
1402   if (PREDICT_FALSE (buffers_used < buffers_required))
1403     vlib_buffer_free (vm, next, buffers_required - buffers_used);
1404
1405 done:
1406   return n_rx_packets;
1407 }
1408
1409 VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm,
1410                                       vlib_node_runtime_t * node,
1411                                       vlib_frame_t * frame)
1412 {
1413   vhost_user_main_t *vum = &vhost_user_main;
1414   uword n_rx_packets = 0;
1415   vhost_user_intf_t *vui;
1416   vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
1417   vnet_hw_if_rxq_poll_vector_t *pve;
1418
1419   vec_foreach (pve, pv)
1420     {
1421       vui = pool_elt_at_index (vum->vhost_user_interfaces, pve->dev_instance);
1422       if (vhost_user_is_packed_ring_supported (vui))
1423         {
1424           if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1425             n_rx_packets += vhost_user_if_input_packed (
1426               vm, vum, vui, pve->queue_id, node, 1);
1427           else
1428             n_rx_packets += vhost_user_if_input_packed (
1429               vm, vum, vui, pve->queue_id, node, 0);
1430         }
1431       else
1432         {
1433           if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1434             n_rx_packets +=
1435               vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 1);
1436           else
1437             n_rx_packets +=
1438               vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 0);
1439         }
1440     }
1441
1442   return n_rx_packets;
1443 }
1444
1445 /* *INDENT-OFF* */
1446 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1447   .type = VLIB_NODE_TYPE_INPUT,
1448   .name = "vhost-user-input",
1449   .sibling_of = "device-input",
1450   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
1451
1452   /* Will be enabled if/when hardware is detected. */
1453   .state = VLIB_NODE_STATE_DISABLED,
1454
1455   .format_buffer = format_ethernet_header_with_length,
1456   .format_trace = format_vhost_trace,
1457
1458   .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1459   .error_strings = vhost_user_input_func_error_strings,
1460 };
1461 /* *INDENT-ON* */
1462
1463 /*
1464  * fd.io coding-style-patch-verification: ON
1465  *
1466  * Local Variables:
1467  * eval: (c-set-style "gnu")
1468  * End:
1469  */