FD.io VPP  v20.09-rc2-28-g3c5414029
Vector Packet Processing
vhost_user_input.c
Go to the documentation of this file.
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/ip/ip.h>
37 
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
41 
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 
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,
83 #undef _
86 
87 static __clib_unused char *vhost_user_input_func_error_strings[] = {
88 #define _(n,s) s,
90 #undef _
91 };
92 
95  vhost_user_intf_t * vui, u16 qid,
97  u16 last_avail_idx)
98 {
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 
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  */
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;
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  */
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 
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  tcp->checksum = 0;
296  b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
297  }
298  else if (l4_proto == IP_PROTOCOL_UDP)
299  {
300  udp_header_t *udp =
301  (udp_header_t *) (b0_data + vnet_buffer (b0)->l4_hdr_offset);
302  l4_hdr_sz = sizeof (*udp);
303  udp->checksum = 0;
304  b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
305  }
306 
307  if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP)
308  {
309  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
310  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
311  b0->flags |= VNET_BUFFER_F_GSO;
312  }
313  else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4)
314  {
315  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
316  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
317  b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4);
318  }
319  else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6)
320  {
321  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
322  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
323  b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6);
324  }
325 }
326 
329  vhost_user_vring_t * rxvq)
330 {
331  f64 now = vlib_time_now (vm);
332 
333  if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
334  vhost_user_send_call (vm, txvq);
335 
336  if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
337  vhost_user_send_call (vm, rxvq);
338 }
339 
342  vhost_user_intf_t * vui,
343  u32 * current_config_index, u32 * next_index,
344  u32 ** to_next, u32 * n_left_to_next)
345 {
347  u8 feature_arc_idx = fm->device_input_feature_arc_index;
348 
349  if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index)))
350  {
352  cm = &fm->feature_config_mains[feature_arc_idx];
353  *current_config_index = vec_elt (cm->config_index_by_sw_if_index,
354  vui->sw_if_index);
355  vnet_get_config_data (&cm->config_main, current_config_index,
356  next_index, 0);
357  }
358 
359  vlib_get_new_next_frame (vm, node, *next_index, *to_next, *n_left_to_next);
360 
361  if (*next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT)
362  {
363  /* give some hints to ethernet-input */
364  vlib_next_frame_t *nf;
365  vlib_frame_t *f;
367  nf = vlib_node_runtime_get_next_frame (vm, node, *next_index);
368  f = vlib_get_frame (vm, nf->frame);
370 
371  ef = vlib_frame_scalar_args (f);
372  ef->sw_if_index = vui->sw_if_index;
373  ef->hw_if_index = vui->hw_if_index;
375  }
376 }
377 
380  vhost_user_main_t * vum,
381  vhost_user_intf_t * vui,
383  vnet_hw_interface_rx_mode mode, u8 enable_csum)
384 {
385  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
387  u16 n_rx_packets = 0;
388  u32 n_rx_bytes = 0;
389  u16 n_left;
390  u32 n_left_to_next, *to_next;
392  u32 n_trace = vlib_get_trace_count (vm, node);
393  u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
394  u32 map_hint = 0;
395  vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
396  u16 copy_len = 0;
397  u8 feature_arc_idx = fm->device_input_feature_arc_index;
398  u32 current_config_index = ~(u32) 0;
399  u16 mask = txvq->qsz_mask;
400 
401  /* The descriptor table is not ready yet */
402  if (PREDICT_FALSE (txvq->avail == 0))
403  goto done;
404 
405  {
406  /* do we have pending interrupts ? */
407  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
408  vhost_user_input_do_interrupt (vm, txvq, rxvq);
409  }
410 
411  /*
412  * For adaptive mode, it is optimized to reduce interrupts.
413  * If the scheduler switches the input node to polling due
414  * to burst of traffic, we tell the driver no interrupt.
415  * When the traffic subsides, the scheduler switches the node back to
416  * interrupt mode. We must tell the driver we want interrupt.
417  */
419  {
420  if ((node->flags &
422  !(node->flags &
424  /* Tell driver we want notification */
425  txvq->used->flags = 0;
426  else
427  /* Tell driver we don't want notification */
429  }
430 
431  if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
432  goto done;
433 
434  n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
435 
436  /* nothing to do */
437  if (PREDICT_FALSE (n_left == 0))
438  goto done;
439 
440  if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
441  {
442  /*
443  * Discard input packet if interface is admin down or vring is not
444  * enabled.
445  * "For example, for a networking device, in the disabled state
446  * client must not supply any new RX packets, but must process
447  * and discard any TX packets."
448  */
449  vhost_user_rx_discard_packet (vm, vui, txvq,
451  goto done;
452  }
453 
454  if (PREDICT_FALSE (n_left == (mask + 1)))
455  {
456  /*
457  * Informational error logging when VPP is not
458  * receiving packets fast enough.
459  */
460  vlib_error_count (vm, node->node_index,
461  VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
462  }
463 
464  if (n_left > VLIB_FRAME_SIZE)
465  n_left = VLIB_FRAME_SIZE;
466 
467  /*
468  * For small packets (<2kB), we will not need more than one vlib buffer
469  * per packet. In case packets are bigger, we will just yield at some point
470  * in the loop and come back later. This is not an issue as for big packet,
471  * processing cost really comes from the memory copy.
472  * The assumption is that big packets will fit in 40 buffers.
473  */
474  if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 ||
475  cpu->rx_buffers_len < 40))
476  {
477  u32 curr_len = cpu->rx_buffers_len;
478  cpu->rx_buffers_len +=
479  vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len,
480  VHOST_USER_RX_BUFFERS_N - curr_len);
481 
482  if (PREDICT_FALSE
484  {
485  /* In case of buffer starvation, discard some packets from the queue
486  * and log the event.
487  * We keep doing best effort for the remaining packets. */
488  u32 flush = (n_left + 1 > cpu->rx_buffers_len) ?
489  n_left + 1 - cpu->rx_buffers_len : 1;
490  flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
491 
492  n_left -= flush;
494  interface_main.sw_if_counters +
496  vm->thread_index, vui->sw_if_index,
497  flush);
498 
500  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
501  }
502  }
503 
504  vhost_user_input_setup_frame (vm, node, vui, &current_config_index,
505  &next_index, &to_next, &n_left_to_next);
506 
507  u16 last_avail_idx = txvq->last_avail_idx;
508  u16 last_used_idx = txvq->last_used_idx;
509 
510  while (n_left > 0)
511  {
512  vlib_buffer_t *b_head, *b_current;
513  u32 bi_current;
514  u16 desc_current;
515  u32 desc_data_offset;
516  vring_desc_t *desc_table = txvq->desc;
517 
518  if (PREDICT_FALSE (cpu->rx_buffers_len <= 1))
519  {
520  /* Not enough rx_buffers
521  * Note: We yeld on 1 so we don't need to do an additional
522  * check for the next buffer prefetch.
523  */
524  n_left = 0;
525  break;
526  }
527 
528  desc_current = txvq->avail->ring[last_avail_idx & mask];
529  cpu->rx_buffers_len--;
530  bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
531  b_head = b_current = vlib_get_buffer (vm, bi_current);
532  to_next[0] = bi_current; //We do that now so we can forget about bi_current
533  to_next++;
534  n_left_to_next--;
535 
537  (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD);
538 
539  /* Just preset the used descriptor id and length for later */
540  txvq->used->ring[last_used_idx & mask].id = desc_current;
541  txvq->used->ring[last_used_idx & mask].len = 0;
542  vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]);
543 
544  /* The buffer should already be initialized */
546  b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
547 
548  if (PREDICT_FALSE (n_trace))
549  {
550  vlib_trace_buffer (vm, node, next_index, b_head,
551  /* follow_chain */ 0);
552  vhost_trace_t *t0 =
553  vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
554  vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx);
555  n_trace--;
556  vlib_set_trace_count (vm, node, n_trace);
557  }
558 
559  /* This depends on the setup but is very consistent
560  * So I think the CPU branch predictor will make a pretty good job
561  * at optimizing the decision. */
562  if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
563  {
564  desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
565  &map_hint);
566  desc_current = 0;
567  if (PREDICT_FALSE (desc_table == 0))
568  {
569  vlib_error_count (vm, node->node_index,
570  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
571  goto out;
572  }
573  }
574 
575  desc_data_offset = vui->virtio_net_hdr_sz;
576 
577  if (enable_csum)
578  {
579  virtio_net_hdr_mrg_rxbuf_t *hdr;
580  u8 *b_data;
581  u16 current;
582 
583  hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint);
584  if (PREDICT_FALSE (hdr == 0))
585  {
586  vlib_error_count (vm, node->node_index,
587  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
588  goto out;
589  }
590  if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
591  {
592  if ((desc_data_offset == desc_table[desc_current].len) &&
593  (desc_table[desc_current].flags & VRING_DESC_F_NEXT))
594  {
595  current = desc_table[desc_current].next;
596  b_data = map_guest_mem (vui, desc_table[current].addr,
597  &map_hint);
598  if (PREDICT_FALSE (b_data == 0))
599  {
600  vlib_error_count (vm, node->node_index,
601  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL,
602  1);
603  goto out;
604  }
605  }
606  else
607  b_data = (u8 *) hdr + desc_data_offset;
608 
609  vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
610  }
611  }
612 
613  while (1)
614  {
615  /* Get more input if necessary. Or end of packet. */
616  if (desc_data_offset == desc_table[desc_current].len)
617  {
618  if (PREDICT_FALSE (desc_table[desc_current].flags &
620  {
621  desc_current = desc_table[desc_current].next;
622  desc_data_offset = 0;
623  }
624  else
625  {
626  goto out;
627  }
628  }
629 
630  /* Get more output if necessary. Or end of packet. */
631  if (PREDICT_FALSE (b_current->current_length == buffer_data_size))
632  {
633  if (PREDICT_FALSE (cpu->rx_buffers_len == 0))
634  {
635  /* Cancel speculation */
636  to_next--;
637  n_left_to_next++;
638 
639  /*
640  * Checking if there are some left buffers.
641  * If not, just rewind the used buffers and stop.
642  * Note: Scheduled copies are not cancelled. This is
643  * not an issue as they would still be valid. Useless,
644  * but valid.
645  */
646  vhost_user_input_rewind_buffers (vm, cpu, b_head);
647  n_left = 0;
648  goto stop;
649  }
650 
651  /* Get next output */
652  cpu->rx_buffers_len--;
653  u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len];
654  b_current->next_buffer = bi_next;
655  b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
656  bi_current = bi_next;
657  b_current = vlib_get_buffer (vm, bi_current);
658  }
659 
660  /* Prepare a copy order executed later for the data */
661  ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
662  vhost_copy_t *cpy = &cpu->copy[copy_len];
663  copy_len++;
664  u32 desc_data_l = desc_table[desc_current].len - desc_data_offset;
665  cpy->len = buffer_data_size - b_current->current_length;
666  cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
667  cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
668  b_current->current_length);
669  cpy->src = desc_table[desc_current].addr + desc_data_offset;
670 
671  desc_data_offset += cpy->len;
672 
673  b_current->current_length += cpy->len;
675  }
676 
677  out:
678 
679  n_rx_bytes += b_head->total_length_not_including_first_buffer;
680  n_rx_packets++;
681 
683  b_head->current_length;
684 
685  /* consume the descriptor and return it as used */
686  last_avail_idx++;
687  last_used_idx++;
688 
690 
691  vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
692  vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
693  b_head->error = 0;
694 
695  if (current_config_index != ~(u32) 0)
696  {
697  b_head->current_config_index = current_config_index;
698  vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
699  }
700 
701  n_left--;
702 
703  /*
704  * Although separating memory copies from virtio ring parsing
705  * is beneficial, we can offer to perform the copies from time
706  * to time in order to free some space in the ring.
707  */
709  {
710  if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy,
711  copy_len, &map_hint)))
712  {
713  vlib_error_count (vm, node->node_index,
714  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
715  }
716  copy_len = 0;
717 
718  /* give buffers back to driver */
720  txvq->used->idx = last_used_idx;
721  vhost_user_log_dirty_ring (vui, txvq, idx);
722  }
723  }
724 stop:
725  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
726 
727  txvq->last_used_idx = last_used_idx;
728  txvq->last_avail_idx = last_avail_idx;
729 
730  /* Do the memory copies */
731  if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len,
732  &map_hint)))
733  {
734  vlib_error_count (vm, node->node_index,
735  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
736  }
737 
738  /* give buffers back to driver */
740  txvq->used->idx = txvq->last_used_idx;
741  vhost_user_log_dirty_ring (vui, txvq, idx);
742 
743  /* interrupt (call) handling */
744  if ((txvq->callfd_idx != ~0) &&
746  {
747  txvq->n_since_last_int += n_rx_packets;
748 
749  if (txvq->n_since_last_int > vum->coalesce_frames)
750  vhost_user_send_call (vm, txvq);
751  }
752 
753  /* increase rx counters */
757  n_rx_packets, n_rx_bytes);
758 
760 
761 done:
762  return n_rx_packets;
763 }
764 
767  vhost_user_vring_t * txvq, u16 desc_head,
768  u16 n_descs_processed)
769 {
770  vring_packed_desc_t *desc_table = txvq->packed_desc;
771  u16 desc_idx;
772  u16 mask = txvq->qsz_mask;
773 
774  for (desc_idx = 0; desc_idx < n_descs_processed; desc_idx++)
775  {
776  if (txvq->used_wrap_counter)
777  desc_table[(desc_head + desc_idx) & mask].flags |=
779  else
780  desc_table[(desc_head + desc_idx) & mask].flags &=
783  }
784 }
785 
788  u16 qid, vhost_user_vring_t * txvq,
789  u16 desc_current)
790 {
792  vring_packed_desc_t *hdr_desc;
793  virtio_net_hdr_mrg_rxbuf_t *hdr;
794  u32 hint = 0;
795 
796  clib_memset (t, 0, sizeof (*t));
797  t->device_index = vui - vum->vhost_user_interfaces;
798  t->qid = qid;
799 
800  hdr_desc = &txvq->packed_desc[desc_current];
801  if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)
802  {
803  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
804  /* Header is the first here */
805  hdr_desc = map_guest_mem (vui, txvq->packed_desc[desc_current].addr,
806  &hint);
807  }
808  if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT)
809  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
810 
811  if (!(txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) &&
812  !(txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT))
813  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
814 
815  t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
816 
817  if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
818  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
819  else
820  {
821  u32 len = vui->virtio_net_hdr_sz;
822  clib_memcpy_fast (&t->hdr, hdr,
823  len > hdr_desc->len ? hdr_desc->len : len);
824  }
825 }
826 
829  vhost_user_intf_t * vui,
830  vhost_user_vring_t * txvq,
831  u32 discard_max)
832 {
833  u32 discarded_packets = 0;
834  u16 mask = txvq->qsz_mask;
835  u16 desc_current, desc_head;
836 
837  desc_head = desc_current = txvq->last_used_idx & mask;
838 
839  /*
840  * On the RX side, each packet corresponds to one descriptor
841  * (it is the same whether it is a shallow descriptor, chained, or indirect).
842  * Therefore, discarding a packet is like discarding a descriptor.
843  */
844  while ((discarded_packets != discard_max) &&
845  vhost_user_packed_desc_available (txvq, desc_current))
846  {
848  discarded_packets++;
849  desc_current = (desc_current + 1) & mask;
850  }
851 
852  if (PREDICT_TRUE (discarded_packets))
853  vhost_user_mark_desc_consumed (vui, txvq, desc_head, discarded_packets);
854  return (discarded_packets);
855 }
856 
859  u16 copy_len, u32 * map_hint)
860 {
861  void *src0, *src1, *src2, *src3, *src4, *src5, *src6, *src7;
862  u8 bad;
863  u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
864 
865  if (PREDICT_TRUE (copy_len >= 8))
866  {
867  src4 = map_guest_mem (vui, cpy[0].src, map_hint);
868  src5 = map_guest_mem (vui, cpy[1].src, map_hint);
869  src6 = map_guest_mem (vui, cpy[2].src, map_hint);
870  src7 = map_guest_mem (vui, cpy[3].src, map_hint);
871  bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
872  if (PREDICT_FALSE (bad))
873  goto one_by_one;
874  CLIB_PREFETCH (src4, 64, LOAD);
875  CLIB_PREFETCH (src5, 64, LOAD);
876  CLIB_PREFETCH (src6, 64, LOAD);
877  CLIB_PREFETCH (src7, 64, LOAD);
878 
879  while (PREDICT_TRUE (copy_len >= 8))
880  {
881  src0 = src4;
882  src1 = src5;
883  src2 = src6;
884  src3 = src7;
885 
886  src4 = map_guest_mem (vui, cpy[4].src, map_hint);
887  src5 = map_guest_mem (vui, cpy[5].src, map_hint);
888  src6 = map_guest_mem (vui, cpy[6].src, map_hint);
889  src7 = map_guest_mem (vui, cpy[7].src, map_hint);
890  bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
891  if (PREDICT_FALSE (bad))
892  break;
893 
894  CLIB_PREFETCH (src4, 64, LOAD);
895  CLIB_PREFETCH (src5, 64, LOAD);
896  CLIB_PREFETCH (src6, 64, LOAD);
897  CLIB_PREFETCH (src7, 64, LOAD);
898 
899  clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len);
900  clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len);
901  clib_memcpy_fast ((void *) cpy[2].dst, src2, cpy[2].len);
902  clib_memcpy_fast ((void *) cpy[3].dst, src3, cpy[3].len);
903  copy_len -= 4;
904  cpy += 4;
905  }
906  }
907 
908 one_by_one:
909  while (copy_len)
910  {
911  if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
912  {
913  rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
914  break;
915  }
916  clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len);
917  copy_len -= 1;
918  cpy += 1;
919  }
920  return rc;
921 }
922 
925  vring_packed_desc_t * desc_table, u16 desc_current,
926  u16 mask, vlib_buffer_t * b_head, u32 * map_hint)
927 {
928  u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
929  virtio_net_hdr_mrg_rxbuf_t *hdr;
930  u8 *b_data;
931  u32 desc_data_offset = vui->virtio_net_hdr_sz;
932 
933  hdr = map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
934  if (PREDICT_FALSE (hdr == 0))
935  rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
936  else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
937  {
938  if (desc_data_offset == desc_table[desc_current].len)
939  {
940  desc_current = (desc_current + 1) & mask;
941  b_data =
942  map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
943  if (PREDICT_FALSE (b_data == 0))
944  rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
945  else
946  vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
947  }
948  else
949  {
950  b_data = (u8 *) hdr + desc_data_offset;
951  vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
952  }
953  }
954 
955  return rc;
956 }
957 
959 vhost_user_compute_buffers_required (u32 desc_len, u32 buffer_data_size)
960 {
961  div_t result;
962  u32 buffers_required;
963 
964  if (PREDICT_TRUE (buffer_data_size == 2048))
965  {
966  buffers_required = desc_len >> 11;
967  if ((desc_len & 2047) != 0)
968  buffers_required++;
969  return (buffers_required);
970  }
971 
972  if (desc_len < buffer_data_size)
973  return 1;
974 
975  result = div (desc_len, buffer_data_size);
976  if (result.rem)
977  buffers_required = result.quot + 1;
978  else
979  buffers_required = result.quot;
980 
981  return (buffers_required);
982 }
983 
986  vhost_user_vring_t * txvq,
987  u32 buffer_data_size, u16 desc_current,
988  u32 * map_hint)
989 {
990  vring_packed_desc_t *desc_table = txvq->packed_desc;
991  u32 desc_len = 0;
992  u16 desc_data_offset = vui->virtio_net_hdr_sz;
993  u16 desc_idx = desc_current;
994  u32 n_descs;
995 
996  n_descs = desc_table[desc_idx].len >> 4;
997  desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, map_hint);
998  if (PREDICT_FALSE (desc_table == 0))
999  return 0;
1000 
1001  for (desc_idx = 0; desc_idx < n_descs; desc_idx++)
1002  desc_len += desc_table[desc_idx].len;
1003 
1004  if (PREDICT_TRUE (desc_len > desc_data_offset))
1005  desc_len -= desc_data_offset;
1006 
1007  return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1008 }
1009 
1012  vhost_user_vring_t * txvq,
1013  u32 buffer_data_size, u16 * current,
1014  u16 * n_left)
1015 {
1016  vring_packed_desc_t *desc_table = txvq->packed_desc;
1017  u32 desc_len = 0;
1018  u16 mask = txvq->qsz_mask;
1019 
1020  while (desc_table[*current].flags & VRING_DESC_F_NEXT)
1021  {
1022  desc_len += desc_table[*current].len;
1023  (*n_left)++;
1024  *current = (*current + 1) & mask;
1026  }
1027  desc_len += desc_table[*current].len;
1028  (*n_left)++;
1029  *current = (*current + 1) & mask;
1031 
1032  if (PREDICT_TRUE (desc_len > vui->virtio_net_hdr_sz))
1033  desc_len -= vui->virtio_net_hdr_sz;
1034 
1035  return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1036 }
1037 
1039 vhost_user_assemble_packet (vring_packed_desc_t * desc_table,
1040  u16 * desc_idx, vlib_buffer_t * b_head,
1041  vlib_buffer_t ** b_current, u32 ** next,
1042  vlib_buffer_t *** b, u32 * bi_current,
1043  vhost_cpu_t * cpu, u16 * copy_len,
1044  u32 * buffers_used, u32 buffers_required,
1045  u32 * desc_data_offset, u32 buffer_data_size,
1046  u16 mask)
1047 {
1048  u32 desc_data_l;
1049 
1050  while (*desc_data_offset < desc_table[*desc_idx].len)
1051  {
1052  /* Get more output if necessary. Or end of packet. */
1053  if (PREDICT_FALSE ((*b_current)->current_length == buffer_data_size))
1054  {
1055  /* Get next output */
1056  u32 bi_next = **next;
1057  (*next)++;
1058  (*b_current)->next_buffer = bi_next;
1059  (*b_current)->flags |= VLIB_BUFFER_NEXT_PRESENT;
1060  *bi_current = bi_next;
1061  *b_current = **b;
1062  (*b)++;
1063  (*buffers_used)++;
1064  ASSERT (*buffers_used <= buffers_required);
1065  }
1066 
1067  /* Prepare a copy order executed later for the data */
1068  ASSERT (*copy_len < VHOST_USER_COPY_ARRAY_N);
1069  vhost_copy_t *cpy = &cpu->copy[*copy_len];
1070  (*copy_len)++;
1071  desc_data_l = desc_table[*desc_idx].len - *desc_data_offset;
1072  cpy->len = buffer_data_size - (*b_current)->current_length;
1073  cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1074  cpy->dst = (uword) (vlib_buffer_get_current (*b_current) +
1075  (*b_current)->current_length);
1076  cpy->src = desc_table[*desc_idx].addr + *desc_data_offset;
1077 
1078  *desc_data_offset += cpy->len;
1079 
1080  (*b_current)->current_length += cpy->len;
1082  }
1083  *desc_idx = (*desc_idx + 1) & mask;;
1084  *desc_data_offset = 0;
1085 }
1086 
1089  vhost_user_intf_t * vui, u16 qid,
1091  vnet_hw_interface_rx_mode mode, u8 enable_csum)
1092 {
1093  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1095  u8 feature_arc_idx = fm->device_input_feature_arc_index;
1096  u16 n_rx_packets = 0;
1097  u32 n_rx_bytes = 0;
1098  u16 n_left = 0;
1099  u32 buffers_required = 0;
1100  u32 n_left_to_next, *to_next;
1102  u32 n_trace = vlib_get_trace_count (vm, node);
1103  u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
1104  u32 map_hint = 0;
1105  vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
1106  u16 copy_len = 0;
1107  u32 current_config_index = ~0;
1108  u16 mask = txvq->qsz_mask;
1109  u16 desc_current, desc_head, last_used_idx;
1110  vring_packed_desc_t *desc_table = 0;
1111  u32 n_descs_processed = 0;
1112  u32 rv;
1113  vlib_buffer_t **b;
1114  u32 *next;
1115  u32 buffers_used = 0;
1116  u16 current, n_descs_to_process;
1117 
1118  /* The descriptor table is not ready yet */
1119  if (PREDICT_FALSE (txvq->packed_desc == 0))
1120  goto done;
1121 
1122  /* do we have pending interrupts ? */
1123  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1124  vhost_user_input_do_interrupt (vm, txvq, rxvq);
1125 
1126  /*
1127  * For adaptive mode, it is optimized to reduce interrupts.
1128  * If the scheduler switches the input node to polling due
1129  * to burst of traffic, we tell the driver no interrupt.
1130  * When the traffic subsides, the scheduler switches the node back to
1131  * interrupt mode. We must tell the driver we want interrupt.
1132  */
1134  {
1135  if ((node->flags &
1137  !(node->flags &
1139  /* Tell driver we want notification */
1140  txvq->used_event->flags = 0;
1141  else
1142  /* Tell driver we don't want notification */
1143  txvq->used_event->flags = VRING_EVENT_F_DISABLE;
1144  }
1145 
1146  last_used_idx = txvq->last_used_idx & mask;
1147  desc_head = desc_current = last_used_idx;
1148 
1149  if (vhost_user_packed_desc_available (txvq, desc_current) == 0)
1150  goto done;
1151 
1152  if (PREDICT_FALSE (!vui->admin_up || !vui->is_ready || !(txvq->enabled)))
1153  {
1154  /*
1155  * Discard input packet if interface is admin down or vring is not
1156  * enabled.
1157  * "For example, for a networking device, in the disabled state
1158  * client must not supply any new RX packets, but must process
1159  * and discard any TX packets."
1160  */
1161  rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq,
1164  VHOST_USER_INPUT_FUNC_ERROR_NOT_READY, rv);
1165  goto done;
1166  }
1167 
1168  vhost_user_input_setup_frame (vm, node, vui, &current_config_index,
1169  &next_index, &to_next, &n_left_to_next);
1170 
1171  /*
1172  * Compute n_left and total buffers needed
1173  */
1174  desc_table = txvq->packed_desc;
1175  current = desc_current;
1176  while (vhost_user_packed_desc_available (txvq, current) &&
1177  (n_left < VLIB_FRAME_SIZE))
1178  {
1179  if (desc_table[current].flags & VRING_DESC_F_INDIRECT)
1180  {
1181  buffers_required +=
1182  vhost_user_compute_indirect_desc_len (vui, txvq, buffer_data_size,
1183  current, &map_hint);
1184  n_left++;
1185  current = (current + 1) & mask;
1187  }
1188  else
1189  {
1190  buffers_required +=
1191  vhost_user_compute_chained_desc_len (vui, txvq, buffer_data_size,
1192  &current, &n_left);
1193  }
1194  }
1195 
1196  /* Something is broken if we need more than 10000 buffers */
1197  if (PREDICT_FALSE ((buffers_required == 0) || (buffers_required > 10000)))
1198  {
1199  rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1201  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1202  goto done;
1203  }
1204 
1205  vec_validate (cpu->to_next_list, buffers_required);
1206  rv = vlib_buffer_alloc (vm, cpu->to_next_list, buffers_required);
1207  if (PREDICT_FALSE (rv != buffers_required))
1208  {
1209  vlib_buffer_free (vm, cpu->to_next_list, rv);
1210  rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1212  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1213  goto done;
1214  }
1215 
1216  next = cpu->to_next_list;
1217  vec_validate (cpu->rx_buffers_pdesc, buffers_required);
1218  vlib_get_buffers (vm, next, cpu->rx_buffers_pdesc, buffers_required);
1219  b = cpu->rx_buffers_pdesc;
1220  n_descs_processed = n_left;
1221 
1222  while (n_left)
1223  {
1224  vlib_buffer_t *b_head, *b_current;
1225  u32 bi_current;
1226  u32 desc_data_offset;
1227  u16 desc_idx = desc_current;
1228  u32 n_descs;
1229 
1230  desc_table = txvq->packed_desc;
1231  to_next[0] = bi_current = next[0];
1232  b_head = b_current = b[0];
1233  b++;
1234  buffers_used++;
1235  ASSERT (buffers_used <= buffers_required);
1236  to_next++;
1237  next++;
1238  n_left_to_next--;
1239 
1240  /* The buffer should already be initialized */
1242  b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1243  desc_data_offset = vui->virtio_net_hdr_sz;
1244  n_descs_to_process = 1;
1245 
1246  if (desc_table[desc_idx].flags & VRING_DESC_F_INDIRECT)
1247  {
1248  n_descs = desc_table[desc_idx].len >> 4;
1249  desc_table = map_guest_mem (vui, desc_table[desc_idx].addr,
1250  &map_hint);
1251  desc_idx = 0;
1252  if (PREDICT_FALSE (desc_table == 0) ||
1253  (enable_csum &&
1254  (PREDICT_FALSE
1256  (vui, desc_table, desc_idx, mask, b_head,
1257  &map_hint) != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))))
1258  {
1259  vlib_error_count (vm, node->node_index,
1260  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1261  to_next--;
1262  next--;
1263  n_left_to_next++;
1264  buffers_used--;
1265  b--;
1266  goto out;
1267  }
1268  while (n_descs)
1269  {
1270  vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1271  &b_current, &next, &b, &bi_current,
1272  cpu, &copy_len, &buffers_used,
1273  buffers_required, &desc_data_offset,
1274  buffer_data_size, mask);
1275  n_descs--;
1276  }
1277  }
1278  else
1279  {
1280  if (enable_csum)
1281  {
1282  rv = vhost_user_do_offload (vui, desc_table, desc_idx, mask,
1283  b_head, &map_hint);
1284  if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1285  {
1286  vlib_error_count (vm, node->node_index, rv, 1);
1287  to_next--;
1288  next--;
1289  n_left_to_next++;
1290  buffers_used--;
1291  b--;
1292  goto out;
1293  }
1294  }
1295  /*
1296  * For chained descriptor, we process all chains in a single while
1297  * loop. So count how many descriptors in the chain.
1298  */
1299  n_descs_to_process = 1;
1300  while (desc_table[desc_idx].flags & VRING_DESC_F_NEXT)
1301  {
1302  vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1303  &b_current, &next, &b, &bi_current,
1304  cpu, &copy_len, &buffers_used,
1305  buffers_required, &desc_data_offset,
1306  buffer_data_size, mask);
1307  n_descs_to_process++;
1308  }
1309  vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1310  &b_current, &next, &b, &bi_current,
1311  cpu, &copy_len, &buffers_used,
1312  buffers_required, &desc_data_offset,
1313  buffer_data_size, mask);
1314  }
1315 
1316  n_rx_bytes += b_head->total_length_not_including_first_buffer;
1317  n_rx_packets++;
1318 
1320  b_head->current_length;
1321 
1323 
1324  vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1325  vnet_buffer (b_head)->sw_if_index[VLIB_TX] = ~0;
1326  b_head->error = 0;
1327 
1328  if (current_config_index != ~0)
1329  {
1330  b_head->current_config_index = current_config_index;
1331  vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
1332  }
1333 
1334  out:
1335  ASSERT (n_left >= n_descs_to_process);
1336  n_left -= n_descs_to_process;
1337 
1338  /* advance to next descrptor */
1339  desc_current = (desc_current + n_descs_to_process) & mask;
1340 
1341  /*
1342  * Although separating memory copies from virtio ring parsing
1343  * is beneficial, we can offer to perform the copies from time
1344  * to time in order to free some space in the ring.
1345  */
1346  if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1347  {
1348  rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len,
1349  &map_hint);
1350  if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1351  vlib_error_count (vm, node->node_index, rv, 1);
1352  copy_len = 0;
1353  }
1354  }
1355  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1356 
1357  /* Do the memory copies */
1358  rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, &map_hint);
1359  if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1360  vlib_error_count (vm, node->node_index, rv, 1);
1361 
1362  /* Must do the tracing before giving buffers back to driver */
1363  if (PREDICT_FALSE (n_trace))
1364  {
1365  u32 left = n_rx_packets;
1366 
1367  b = cpu->rx_buffers_pdesc;
1368  while (n_trace && left)
1369  {
1370  vhost_trace_t *t0;
1371 
1372  vlib_trace_buffer (vm, node, next_index, b[0],
1373  /* follow_chain */ 0);
1374  t0 = vlib_add_trace (vm, node, b[0], sizeof (t0[0]));
1375  b++;
1376  vhost_user_rx_trace_packed (t0, vui, qid, txvq, last_used_idx);
1377  last_used_idx = (last_used_idx + 1) & mask;
1378  n_trace--;
1379  left--;
1380  vlib_set_trace_count (vm, node, n_trace);
1381  }
1382  }
1383 
1384  /*
1385  * Give buffers back to driver.
1386  */
1387  vhost_user_mark_desc_consumed (vui, txvq, desc_head, n_descs_processed);
1388 
1389  /* interrupt (call) handling */
1390  if ((txvq->callfd_idx != ~0) &&
1391  (txvq->avail_event->flags != VRING_EVENT_F_DISABLE))
1392  {
1393  txvq->n_since_last_int += n_rx_packets;
1394  if (txvq->n_since_last_int > vum->coalesce_frames)
1395  vhost_user_send_call (vm, txvq);
1396  }
1397 
1398  /* increase rx counters */
1402  n_rx_packets, n_rx_bytes);
1403 
1404  vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets);
1405 
1406  if (PREDICT_FALSE (buffers_used < buffers_required))
1407  vlib_buffer_free (vm, next, buffers_required - buffers_used);
1408 
1409 done:
1410  return n_rx_packets;
1411 }
1412 
1415  vlib_frame_t * frame)
1416 {
1418  uword n_rx_packets = 0;
1419  vhost_user_intf_t *vui;
1421  (vnet_device_input_runtime_t *) node->runtime_data;
1423 
1425  {
1426  if ((node->state == VLIB_NODE_STATE_POLLING) ||
1427  clib_atomic_swap_acq_n (&dq->interrupt_pending, 0))
1428  {
1429  vui =
1430  pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
1432  {
1433  if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1434  n_rx_packets += vhost_user_if_input_packed (vm, vum, vui,
1435  dq->queue_id, node,
1436  dq->mode, 1);
1437  else
1438  n_rx_packets += vhost_user_if_input_packed (vm, vum, vui,
1439  dq->queue_id, node,
1440  dq->mode, 0);
1441  }
1442  else
1443  {
1444  if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1445  n_rx_packets += vhost_user_if_input (vm, vum, vui, dq->queue_id,
1446  node, dq->mode, 1);
1447  else
1448  n_rx_packets += vhost_user_if_input (vm, vum, vui, dq->queue_id,
1449  node, dq->mode, 0);
1450  }
1451  }
1452  }
1453 
1454  return n_rx_packets;
1455 }
1456 
1457 /* *INDENT-OFF* */
1459  .type = VLIB_NODE_TYPE_INPUT,
1460  .name = "vhost-user-input",
1461  .sibling_of = "device-input",
1463 
1464  /* Will be enabled if/when hardware is detected. */
1465  .state = VLIB_NODE_STATE_DISABLED,
1466 
1467  .format_buffer = format_ethernet_header_with_length,
1468  .format_trace = format_vhost_trace,
1469 
1470  .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1471  .error_strings = vhost_user_input_func_error_strings,
1472 };
1473 /* *INDENT-ON* */
1474 
1475 /*
1476  * fd.io coding-style-patch-verification: ON
1477  *
1478  * Local Variables:
1479  * eval: (c-set-style "gnu")
1480  * End:
1481  */
vnet_config_main_t config_main
Definition: feature.h:82
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
#define VRING_EVENT_F_DISABLE
Definition: virtio_std.h:81
u32 * to_next_list
Definition: vhost_user.h:292
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
static void vnet_device_increment_rx_packets(u32 thread_index, u64 count)
Definition: devices.h:110
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
vring_desc_t * desc
Definition: vhost_user.h:170
u32 virtio_ring_flags
The device index.
Definition: vhost_user.h:272
virtio_net_hdr_mrg_rxbuf_t hdr
Length of the first data descriptor.
Definition: vhost_user.h:274
vhost_user_input_func_error_t
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:193
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:307
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:937
vring_desc_event_t * used_event
Definition: vhost_user.h:181
#define VIRTIO_NET_HDR_F_NEEDS_CSUM
Definition: virtio_std.h:134
static_always_inline void vhost_user_assemble_packet(vring_packed_desc_t *desc_table, u16 *desc_idx, vlib_buffer_t *b_head, vlib_buffer_t **b_current, u32 **next, vlib_buffer_t ***b, u32 *bi_current, vhost_cpu_t *cpu, u16 *copy_len, u32 *buffers_used, u32 buffers_required, u32 *desc_data_offset, u32 buffer_data_size, u16 mask)
#define vnet_buffer2(b)
Definition: buffer.h:482
vnet_interface_main_t interface_main
Definition: vnet.h:59
#define PREDICT_TRUE(x)
Definition: clib.h:121
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:135
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
vring_used_elem_t ring[0]
Definition: virtio_std.h:113
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:305
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:333
vring_packed_desc_t * packed_desc
Definition: vhost_user.h:171
vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N]
Definition: vhost_user.h:286
vring_avail_t * avail
Definition: vhost_user.h:175
u32 thread_index
Definition: main.h:249
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
static vlib_frame_t * vlib_get_frame(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:269
#define foreach_vhost_user_input_func_error
vl_api_address_t src
Definition: gre.api:54
vlib_main_t * vm
Definition: in2out_ed.c:1582
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:78
static_always_inline void vhost_user_advance_last_avail_idx(vhost_user_vring_t *vring)
static_always_inline int vnet_have_features(u8 arc, u32 sw_if_index)
Definition: feature.h:251
#define VHOST_USER_DOWN_DISCARD_COUNT
#define VLIB_NODE_FN(node)
Definition: node.h:202
static_always_inline u32 vhost_user_compute_buffers_required(u32 desc_len, u32 buffer_data_size)
static_always_inline u32 vhost_user_rx_discard_packet(vlib_main_t *vm, vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u32 discard_max)
Try to discard packets from the tx ring (VPP RX path).
u16 mask
Definition: flow_types.api:52
struct _tcp_header tcp_header_t
vring_used_t * used
Definition: vhost_user.h:180
vhost_vring_addr_t addr
Definition: vhost_user.h:111
unsigned char u8
Definition: types.h:56
#define VIRTIO_FEATURE(X)
Definition: virtio_std.h:69
double f64
Definition: types.h:142
#define fm
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:136
static_always_inline u32 vhost_user_if_input(vlib_main_t *vm, vhost_user_main_t *vum, vhost_user_intf_t *vui, u16 qid, vlib_node_runtime_t *node, vnet_hw_interface_rx_mode mode, u8 enable_csum)
vnet_hw_interface_rx_mode
Definition: interface.h:53
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: virtio_std.h:85
static_always_inline u32 vhost_user_compute_chained_desc_len(vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u32 buffer_data_size, u16 *current, u16 *n_left)
#define static_always_inline
Definition: clib.h:108
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
Definition: buffer_funcs.h:476
static_always_inline void vhost_user_rx_trace_packed(vhost_trace_t *t, vhost_user_intf_t *vui, u16 qid, vhost_user_vring_t *txvq, u16 desc_current)
vl_api_ip6_address_t ip6
Definition: one.api:424
#define ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX
Definition: ethernet.h:52
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:879
static_always_inline void * map_guest_mem(vhost_user_intf_t *vui, uword addr, u32 *hint)
#define VHOST_VRING_IDX_TX(qid)
Definition: vhost_user.h:27
#define vlib_get_new_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:396
#define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE
Definition: node.h:303
static_always_inline u8 * format_vhost_trace(u8 *s, va_list *va)
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:377
static vlib_next_frame_t * vlib_node_runtime_get_next_frame(vlib_main_t *vm, vlib_node_runtime_t *n, u32 next_index)
Definition: node_funcs.h:317
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
#define VRING_USED_F_NO_NOTIFY
Definition: virtio_std.h:84
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
#define VHOST_USER_COPY_ARRAY_N
Definition: vhost_user.h:278
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
#define VHOST_USER_RX_BUFFER_STARVATION
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:123
static_always_inline void vhost_user_handle_rx_offload(vlib_buffer_t *b0, u8 *b0_data, virtio_net_hdr_t *hdr)
static_always_inline void vhost_user_mark_desc_consumed(vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u16 desc_head, u16 n_descs_processed)
#define PREDICT_FALSE(x)
Definition: clib.h:120
vl_api_ip4_address_t ip4
Definition: one.api:376
vhost_user_main_t vhost_user_main
Definition: vhost_user.c:56
vnet_main_t vnet_main
Definition: misc.c:43
u32 node_index
Node index.
Definition: node.h:487
vl_api_address_t dst
Definition: gre.api:55
#define VIRTIO_NET_HDR_GSO_TCPV4
Definition: virtio_std.h:138
vl_api_tunnel_mode_t mode
Definition: gre.api:48
u8 len
Definition: ip_types.api:92
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost_user.h:26
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
u16 device_index
The interface queue index (Not the virtio vring idx)
Definition: vhost_user.h:271
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:300
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static_always_inline void vhost_user_input_rewind_buffers(vlib_main_t *vm, vhost_cpu_t *cpu, vlib_buffer_t *b_head)
static_always_inline u64 vhost_user_is_packed_ring_supported(vhost_user_intf_t *vui)
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:311
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:147
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
u16 first_desc_len
Runtime queue flags.
Definition: vhost_user.h:273
static_always_inline u8 vhost_user_packed_desc_available(vhost_user_vring_t *vring, u16 idx)
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
static_always_inline u32 vhost_user_compute_indirect_desc_len(vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u32 buffer_data_size, u16 desc_current, u32 *map_hint)
u32 rx_buffers[VHOST_USER_RX_BUFFERS_N]
Definition: vhost_user.h:283
static __clib_unused char * vhost_user_input_func_error_strings[]
#define ASSERT(truth)
#define VHOST_USER_RX_BUFFERS_N
Definition: vhost_user.h:277
static_always_inline u32 vhost_user_rx_discard_packet_packed(vlib_main_t *vm, vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u32 discard_max)
vlib_frame_t * frame
Definition: node.h:405
#define clib_atomic_swap_acq_n(a, b)
Definition: atomics.h:51
u16 flags
Definition: node.h:387
static_always_inline void vhost_user_send_call(vlib_main_t *vm, vhost_user_vring_t *vq)
static_always_inline u32 vhost_user_input_copy(vhost_user_intf_t *vui, vhost_copy_t *cpy, u16 copy_len, u32 *map_hint)
vlib_buffer_t ** rx_buffers_pdesc
Definition: vhost_user.h:293
static_always_inline int ethernet_frame_is_tagged(u16 type)
Definition: ethernet.h:78
vring_desc_event_t * avail_event
Definition: vhost_user.h:176
#define VRING_DESC_F_NEXT
Definition: virtio_std.h:73
#define vec_elt(v, i)
Get vector value at index i.
u8 device_input_feature_arc_index
Feature arc index for device-input.
Definition: feature.h:112
u16 ring[0]
Definition: virtio_std.h:99
#define VHOST_USER_RX_COPY_THRESHOLD
struct _vlib_node_registration vlib_node_registration_t
Definition: defs.h:47
static_always_inline u32 vhost_user_do_offload(vhost_user_intf_t *vui, vring_packed_desc_t *desc_table, u16 desc_current, u16 mask, vlib_buffer_t *b_head, u32 *map_hint)
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
static_always_inline void vhost_user_input_setup_frame(vlib_main_t *vm, vlib_node_runtime_t *node, vhost_user_intf_t *vui, u32 *current_config_index, u32 *next_index, u32 **to_next, u32 *n_left_to_next)
vhost_user_vring_t vrings[VHOST_VRING_MAX_N]
Definition: vhost_user.h:241
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
static_always_inline u32 vhost_user_input_copy_packed(vhost_user_intf_t *vui, vhost_copy_t *cpy, u16 copy_len, u32 *map_hint)
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:492
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static_always_inline void vhost_user_rx_trace(vhost_trace_t *t, vhost_user_intf_t *vui, u16 qid, vlib_buffer_t *b, vhost_user_vring_t *txvq, u16 last_avail_idx)
static_always_inline void vhost_user_input_do_interrupt(vlib_main_t *vm, vhost_user_vring_t *txvq, vhost_user_vring_t *rxvq)
left
#define vhost_user_log_dirty_ring(vui, vq, member)
u32 rx_buffers_len
Definition: vhost_user.h:282
#define vnet_buffer(b)
Definition: buffer.h:417
static_always_inline u32 vhost_user_if_input_packed(vlib_main_t *vm, vhost_user_main_t *vum, vhost_user_intf_t *vui, u16 qid, vlib_node_runtime_t *node, vnet_hw_interface_rx_mode mode, u8 enable_csum)
static int tcp_header_bytes(tcp_header_t *t)
Definition: tcp_packet.h:93
#define VRING_DESC_F_USED
Definition: virtio_std.h:78
#define vec_foreach(var, vec)
Vector iterator.
u16 flags
Copy of main node flags.
Definition: node.h:500
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:577
static_always_inline void vhost_user_advance_last_used_idx(vhost_user_vring_t *vring)
static void vlib_frame_no_append(vlib_frame_t *f)
Definition: node_funcs.h:277
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:209
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:280
vnet_feature_config_main_t * feature_config_mains
feature config main objects
Definition: feature.h:100
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
#define VIRTIO_NET_HDR_GSO_UDP
Definition: virtio_std.h:139
vnet_feature_main_t feature_main
Definition: feature.c:18
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
#define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE
Definition: node.h:304
Definition: defs.h:46
#define VRING_DESC_F_INDIRECT
Definition: virtio_std.h:75
#define VIRTIO_NET_HDR_GSO_TCPV6
Definition: virtio_std.h:140
#define VRING_DESC_F_AVAIL
Definition: virtio_std.h:77