FD.io VPP  v19.08.3-2-gbabecb413
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 
45 
46 /*
47  * When an RX queue is down but active, received packets
48  * must be discarded. This value controls up to how many
49  * packets will be discarded during each round.
50  */
51 #define VHOST_USER_DOWN_DISCARD_COUNT 256
52 
53 /*
54  * When the number of available buffers gets under this threshold,
55  * RX node will start discarding packets.
56  */
57 #define VHOST_USER_RX_BUFFER_STARVATION 32
58 
59 /*
60  * On the receive side, the host should free descriptors as soon
61  * as possible in order to avoid TX drop in the VM.
62  * This value controls the number of copy operations that are stacked
63  * before copy is done for all and descriptors are given back to
64  * the guest.
65  * The value 64 was obtained by testing (48 and 128 were not as good).
66  */
67 #define VHOST_USER_RX_COPY_THRESHOLD 64
68 
70 
71 #define foreach_vhost_user_input_func_error \
72  _(NO_ERROR, "no error") \
73  _(NO_BUFFER, "no available buffer") \
74  _(MMAP_FAIL, "mmap failure") \
75  _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
76  _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
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 & VIRTQ_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 & VIRTQ_DESC_F_NEXT)
117  {
118  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
119  }
120  if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
121  !(txvq->desc[desc_current].flags & VIRTQ_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 
253  if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
254  {
255  u8 l4_proto = 0;
256  ethernet_header_t *eh = (ethernet_header_t *) b0_data;
257  u16 ethertype = clib_net_to_host_u16 (eh->type);
258  u16 l2hdr_sz = sizeof (ethernet_header_t);
259 
260  if (ethernet_frame_is_tagged (ethertype))
261  {
262  ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
263 
264  ethertype = clib_net_to_host_u16 (vlan->type);
265  l2hdr_sz += sizeof (*vlan);
266  if (ethertype == ETHERNET_TYPE_VLAN)
267  {
268  vlan++;
269  ethertype = clib_net_to_host_u16 (vlan->type);
270  l2hdr_sz += sizeof (*vlan);
271  }
272  }
273  vnet_buffer (b0)->l2_hdr_offset = 0;
274  vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
275  vnet_buffer (b0)->l4_hdr_offset = hdr->csum_start;
276  b0->flags |= (VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
277  VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
278  VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
279 
280  if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
281  {
282  ip4_header_t *ip4 = (ip4_header_t *) (b0_data + l2hdr_sz);
283  l4_proto = ip4->protocol;
284  b0->flags |= (VNET_BUFFER_F_IS_IP4 |
285  VNET_BUFFER_F_OFFLOAD_IP_CKSUM);
286  }
287  else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
288  {
289  ip6_header_t *ip6 = (ip6_header_t *) (b0_data + l2hdr_sz);
290  l4_proto = ip6->protocol;
291  b0->flags |= VNET_BUFFER_F_IS_IP6;
292  }
293 
294  if (l4_proto == IP_PROTOCOL_TCP)
295  {
296  tcp_header_t *tcp = (tcp_header_t *)
297  (b0_data + vnet_buffer (b0)->l4_hdr_offset);
298  l4_hdr_sz = tcp_header_bytes (tcp);
299  b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
300  }
301  else if (l4_proto == IP_PROTOCOL_UDP)
302  {
303  l4_hdr_sz = sizeof (udp_header_t);
304  b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
305  }
306  }
307 
308  if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP)
309  {
310  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
311  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
312  b0->flags |= VNET_BUFFER_F_GSO;
313  }
314  else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4)
315  {
316  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
317  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
318  b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4);
319  }
320  else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6)
321  {
322  vnet_buffer2 (b0)->gso_size = hdr->gso_size;
323  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
324  b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6);
325  }
326 }
327 
330  vhost_user_main_t * vum,
331  vhost_user_intf_t * vui,
332  u16 qid, vlib_node_runtime_t * node,
333  vnet_hw_interface_rx_mode mode, u8 enable_csum)
334 {
335  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
337  u16 n_rx_packets = 0;
338  u32 n_rx_bytes = 0;
339  u16 n_left;
340  u32 n_left_to_next, *to_next;
342  u32 n_trace = vlib_get_trace_count (vm, node);
343  u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
344  u32 map_hint = 0;
345  vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
346  u16 copy_len = 0;
347  u8 feature_arc_idx = fm->device_input_feature_arc_index;
348  u32 current_config_index = ~(u32) 0;
349  u16 mask = txvq->qsz_mask;
350 
351  /* The descriptor table is not ready yet */
352  if (PREDICT_FALSE (txvq->avail == 0))
353  goto done;
354 
355  {
356  /* do we have pending interrupts ? */
357  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
358  f64 now = vlib_time_now (vm);
359 
360  if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
361  vhost_user_send_call (vm, txvq);
362 
363  if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
364  vhost_user_send_call (vm, rxvq);
365  }
366 
367  /*
368  * For adaptive mode, it is optimized to reduce interrupts.
369  * If the scheduler switches the input node to polling due
370  * to burst of traffic, we tell the driver no interrupt.
371  * When the traffic subsides, the scheduler switches the node back to
372  * interrupt mode. We must tell the driver we want interrupt.
373  */
375  {
376  if ((node->flags &
378  !(node->flags &
380  /* Tell driver we want notification */
381  txvq->used->flags = 0;
382  else
383  /* Tell driver we don't want notification */
385  }
386 
387  if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
388  goto done;
389 
390  n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
391 
392  /* nothing to do */
393  if (PREDICT_FALSE (n_left == 0))
394  goto done;
395 
396  if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
397  {
398  /*
399  * Discard input packet if interface is admin down or vring is not
400  * enabled.
401  * "For example, for a networking device, in the disabled state
402  * client must not supply any new RX packets, but must process
403  * and discard any TX packets."
404  */
405  vhost_user_rx_discard_packet (vm, vui, txvq,
407  goto done;
408  }
409 
410  if (PREDICT_FALSE (n_left == (mask + 1)))
411  {
412  /*
413  * Informational error logging when VPP is not
414  * receiving packets fast enough.
415  */
416  vlib_error_count (vm, node->node_index,
417  VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
418  }
419 
420  if (n_left > VLIB_FRAME_SIZE)
421  n_left = VLIB_FRAME_SIZE;
422 
423  /*
424  * For small packets (<2kB), we will not need more than one vlib buffer
425  * per packet. In case packets are bigger, we will just yeld at some point
426  * in the loop and come back later. This is not an issue as for big packet,
427  * processing cost really comes from the memory copy.
428  * The assumption is that big packets will fit in 40 buffers.
429  */
430  if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 ||
431  cpu->rx_buffers_len < 40))
432  {
433  u32 curr_len = cpu->rx_buffers_len;
434  cpu->rx_buffers_len +=
435  vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len,
436  VHOST_USER_RX_BUFFERS_N - curr_len);
437 
438  if (PREDICT_FALSE
440  {
441  /* In case of buffer starvation, discard some packets from the queue
442  * and log the event.
443  * We keep doing best effort for the remaining packets. */
444  u32 flush = (n_left + 1 > cpu->rx_buffers_len) ?
445  n_left + 1 - cpu->rx_buffers_len : 1;
446  flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
447 
448  n_left -= flush;
450  interface_main.sw_if_counters +
452  vm->thread_index, vui->sw_if_index,
453  flush);
454 
456  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
457  }
458  }
459 
460  if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index)))
461  {
463  cm = &fm->feature_config_mains[feature_arc_idx];
464  current_config_index = vec_elt (cm->config_index_by_sw_if_index,
465  vui->sw_if_index);
466  vnet_get_config_data (&cm->config_main, &current_config_index,
467  &next_index, 0);
468  }
469 
470  u16 last_avail_idx = txvq->last_avail_idx;
471  u16 last_used_idx = txvq->last_used_idx;
472 
473  vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
474 
475  if (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT)
476  {
477  /* give some hints to ethernet-input */
478  vlib_next_frame_t *nf;
479  vlib_frame_t *f;
481  nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
482  f = vlib_get_frame (vm, nf->frame);
484 
485  ef = vlib_frame_scalar_args (f);
486  ef->sw_if_index = vui->sw_if_index;
487  ef->hw_if_index = vui->hw_if_index;
489  }
490 
491  while (n_left > 0)
492  {
493  vlib_buffer_t *b_head, *b_current;
494  u32 bi_current;
495  u16 desc_current;
496  u32 desc_data_offset;
497  vring_desc_t *desc_table = txvq->desc;
498 
499  if (PREDICT_FALSE (cpu->rx_buffers_len <= 1))
500  {
501  /* Not enough rx_buffers
502  * Note: We yeld on 1 so we don't need to do an additional
503  * check for the next buffer prefetch.
504  */
505  n_left = 0;
506  break;
507  }
508 
509  desc_current = txvq->avail->ring[last_avail_idx & mask];
510  cpu->rx_buffers_len--;
511  bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
512  b_head = b_current = vlib_get_buffer (vm, bi_current);
513  to_next[0] = bi_current; //We do that now so we can forget about bi_current
514  to_next++;
515  n_left_to_next--;
516 
518  (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD);
519 
520  /* Just preset the used descriptor id and length for later */
521  txvq->used->ring[last_used_idx & mask].id = desc_current;
522  txvq->used->ring[last_used_idx & mask].len = 0;
523  vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]);
524 
525  /* The buffer should already be initialized */
527  b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
528 
529  if (PREDICT_FALSE (n_trace))
530  {
531  vlib_trace_buffer (vm, node, next_index, b_head,
532  /* follow_chain */ 0);
533  vhost_trace_t *t0 =
534  vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
535  vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx);
536  n_trace--;
537  vlib_set_trace_count (vm, node, n_trace);
538  }
539 
540  /* This depends on the setup but is very consistent
541  * So I think the CPU branch predictor will make a pretty good job
542  * at optimizing the decision. */
543  if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
544  {
545  desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
546  &map_hint);
547  desc_current = 0;
548  if (PREDICT_FALSE (desc_table == 0))
549  {
550  vlib_error_count (vm, node->node_index,
551  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
552  goto out;
553  }
554  }
555 
556  desc_data_offset = vui->virtio_net_hdr_sz;
557 
558  if (enable_csum)
559  {
560  virtio_net_hdr_mrg_rxbuf_t *hdr;
561  u8 *b_data;
562  u16 current;
563 
564  hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint);
565  if (PREDICT_FALSE (hdr == 0))
566  {
567  vlib_error_count (vm, node->node_index,
568  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
569  goto out;
570  }
571  if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
572  {
573  if ((desc_data_offset == desc_table[desc_current].len) &&
574  (desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT))
575  {
576  current = desc_table[desc_current].next;
577  b_data = map_guest_mem (vui, desc_table[current].addr,
578  &map_hint);
579  if (PREDICT_FALSE (b_data == 0))
580  {
581  vlib_error_count (vm, node->node_index,
582  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL,
583  1);
584  goto out;
585  }
586  }
587  else
588  b_data = (u8 *) hdr + desc_data_offset;
589 
590  vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
591  }
592  }
593 
594  while (1)
595  {
596  /* Get more input if necessary. Or end of packet. */
597  if (desc_data_offset == desc_table[desc_current].len)
598  {
599  if (PREDICT_FALSE (desc_table[desc_current].flags &
601  {
602  desc_current = desc_table[desc_current].next;
603  desc_data_offset = 0;
604  }
605  else
606  {
607  goto out;
608  }
609  }
610 
611  /* Get more output if necessary. Or end of packet. */
612  if (PREDICT_FALSE (b_current->current_length == buffer_data_size))
613  {
614  if (PREDICT_FALSE (cpu->rx_buffers_len == 0))
615  {
616  /* Cancel speculation */
617  to_next--;
618  n_left_to_next++;
619 
620  /*
621  * Checking if there are some left buffers.
622  * If not, just rewind the used buffers and stop.
623  * Note: Scheduled copies are not cancelled. This is
624  * not an issue as they would still be valid. Useless,
625  * but valid.
626  */
627  vhost_user_input_rewind_buffers (vm, cpu, b_head);
628  n_left = 0;
629  goto stop;
630  }
631 
632  /* Get next output */
633  cpu->rx_buffers_len--;
634  u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len];
635  b_current->next_buffer = bi_next;
636  b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
637  bi_current = bi_next;
638  b_current = vlib_get_buffer (vm, bi_current);
639  }
640 
641  /* Prepare a copy order executed later for the data */
642  ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
643  vhost_copy_t *cpy = &cpu->copy[copy_len];
644  copy_len++;
645  u32 desc_data_l = desc_table[desc_current].len - desc_data_offset;
646  cpy->len = buffer_data_size - b_current->current_length;
647  cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
648  cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
649  b_current->current_length);
650  cpy->src = desc_table[desc_current].addr + desc_data_offset;
651 
652  desc_data_offset += cpy->len;
653 
654  b_current->current_length += cpy->len;
656  }
657 
658  out:
659 
660  n_rx_bytes += b_head->total_length_not_including_first_buffer;
661  n_rx_packets++;
662 
664  b_head->current_length;
665 
666  /* consume the descriptor and return it as used */
667  last_avail_idx++;
668  last_used_idx++;
669 
671 
672  vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
673  vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
674  b_head->error = 0;
675 
676  if (current_config_index != ~(u32) 0)
677  {
678  b_head->current_config_index = current_config_index;
679  vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
680  }
681 
682  n_left--;
683 
684  /*
685  * Although separating memory copies from virtio ring parsing
686  * is beneficial, we can offer to perform the copies from time
687  * to time in order to free some space in the ring.
688  */
690  {
691  if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy,
692  copy_len, &map_hint)))
693  {
694  vlib_error_count (vm, node->node_index,
695  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
696  }
697  copy_len = 0;
698 
699  /* give buffers back to driver */
701  txvq->used->idx = last_used_idx;
702  vhost_user_log_dirty_ring (vui, txvq, idx);
703  }
704  }
705 stop:
706  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
707 
708  txvq->last_used_idx = last_used_idx;
709  txvq->last_avail_idx = last_avail_idx;
710 
711  /* Do the memory copies */
712  if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len,
713  &map_hint)))
714  {
715  vlib_error_count (vm, node->node_index,
716  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
717  }
718 
719  /* give buffers back to driver */
721  txvq->used->idx = txvq->last_used_idx;
722  vhost_user_log_dirty_ring (vui, txvq, idx);
723 
724  /* interrupt (call) handling */
725  if ((txvq->callfd_idx != ~0) &&
727  {
728  txvq->n_since_last_int += n_rx_packets;
729 
730  if (txvq->n_since_last_int > vum->coalesce_frames)
731  vhost_user_send_call (vm, txvq);
732  }
733 
734  /* increase rx counters */
738  n_rx_packets, n_rx_bytes);
739 
741 
742 done:
743  return n_rx_packets;
744 }
745 
747  vlib_node_runtime_t * node,
748  vlib_frame_t * frame)
749 {
751  uword n_rx_packets = 0;
752  vhost_user_intf_t *vui;
754  (vnet_device_input_runtime_t *) node->runtime_data;
756 
758  {
759  if ((node->state == VLIB_NODE_STATE_POLLING) ||
760  clib_atomic_swap_acq_n (&dq->interrupt_pending, 0))
761  {
762  vui =
763  pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
764  if (vui->features & (1ULL << FEAT_VIRTIO_NET_F_CSUM))
765  n_rx_packets +=
766  vhost_user_if_input (vm, vum, vui, dq->queue_id, node, dq->mode,
767  1);
768  else
769  n_rx_packets +=
770  vhost_user_if_input (vm, vum, vui, dq->queue_id, node, dq->mode,
771  0);
772  }
773  }
774 
775  return n_rx_packets;
776 }
777 
778 /* *INDENT-OFF* */
780  .type = VLIB_NODE_TYPE_INPUT,
781  .name = "vhost-user-input",
782  .sibling_of = "device-input",
784 
785  /* Will be enabled if/when hardware is detected. */
786  .state = VLIB_NODE_STATE_DISABLED,
787 
788  .format_buffer = format_ethernet_header_with_length,
789  .format_trace = format_vhost_trace,
790 
791  .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
792  .error_strings = vhost_user_input_func_error_strings,
793 };
794 /* *INDENT-ON* */
795 
796 /*
797  * fd.io coding-style-patch-verification: ON
798  *
799  * Local Variables:
800  * eval: (c-set-style "gnu")
801  * End:
802  */
vnet_config_main_t config_main
Definition: feature.h:82
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
u32 len
Definition: pci.h:212
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
u32 flags
Definition: vhost_user.h:141
vring_desc_t * desc
Definition: vhost_user.h:263
u32 virtio_ring_flags
The device index.
Definition: vhost_user.h:348
virtio_net_hdr_mrg_rxbuf_t hdr
Length of the first data descriptor.
Definition: vhost_user.h:350
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:187
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:381
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
#define vnet_buffer2(b)
Definition: buffer.h:424
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define PREDICT_TRUE(x)
Definition: clib.h:113
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:119
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: pci.h:235
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:306
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N]
Definition: vhost_user.h:363
u16 next
Definition: pci.h:214
vring_avail_t * avail
Definition: vhost_user.h:264
u32 thread_index
Definition: main.h:218
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:216
#define foreach_vhost_user_input_func_error
vl_api_address_t src
Definition: gre.api:51
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: vhost_user.h:46
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 int vnet_have_features(u8 arc, u32 sw_if_index)
Definition: feature.h:248
u16 idx
Definition: pci.h:220
#define VHOST_USER_DOWN_DISCARD_COUNT
#define VLIB_NODE_FN(node)
Definition: node.h:202
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 flags
Definition: pci.h:233
struct _tcp_header tcp_header_t
vring_used_t * used
Definition: vhost_user.h:265
vhost_vring_addr_t addr
Definition: vhost_user.h:147
unsigned char u8
Definition: types.h:56
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:130
u64 addr
Definition: pci.h:211
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 static_always_inline
Definition: clib.h:100
#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:440
#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:846
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:24
#define VRING_USED_F_NO_NOTIFY
Definition: vhost_user.h:45
#define vlib_get_new_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:343
#define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE
Definition: node.h:304
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:378
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:264
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
vnet_crypto_main_t * cm
Definition: quic_crypto.c:41
#define VHOST_USER_COPY_ARRAY_N
Definition: vhost_user.h:355
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:514
#define VHOST_USER_RX_BUFFER_STARVATION
unsigned short u16
Definition: types.h:57
#define VIRTQ_DESC_F_INDIRECT
Definition: vhost_user.h:28
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:122
static_always_inline void vhost_user_handle_rx_offload(vlib_buffer_t *b0, u8 *b0_data, virtio_net_hdr_t *hdr)
#define PREDICT_FALSE(x)
Definition: clib.h:112
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:496
vl_api_address_t dst
Definition: gre.api:52
u8 len
Definition: ip_types.api:90
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:23
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:347
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:374
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:323
static_always_inline void vhost_user_input_rewind_buffers(vlib_main_t *vm, vhost_cpu_t *cpu, vlib_buffer_t *b_head)
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:258
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:456
u16 first_desc_len
Runtime queue flags.
Definition: vhost_user.h:349
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
u16 flags
Definition: pci.h:219
u32 rx_buffers[VHOST_USER_RX_BUFFERS_N]
Definition: vhost_user.h:360
static __clib_unused char * vhost_user_input_func_error_strings[]
#define ASSERT(truth)
#define VHOST_USER_RX_BUFFERS_N
Definition: vhost_user.h:354
vlib_frame_t * frame
Definition: node.h:406
#define clib_atomic_swap_acq_n(a, b)
Definition: atomics.h:51
#define VIRTQ_DESC_F_NEXT
Definition: vhost_user.h:27
u16 flags
Definition: node.h:388
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)
static_always_inline int ethernet_frame_is_tagged(u16 type)
Definition: ethernet.h:78
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
#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: pci.h:221
#define VHOST_USER_RX_COPY_THRESHOLD
struct _vlib_node_registration vlib_node_registration_t
Definition: defs.h:47
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
vhost_user_vring_t vrings[VHOST_VRING_MAX_N]
Definition: vhost_user.h:321
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:489
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)
#define vhost_user_log_dirty_ring(vui, vq, member)
u32 rx_buffers_len
Definition: vhost_user.h:359
u16 idx
Definition: pci.h:234
#define vnet_buffer(b)
Definition: buffer.h:365
static int tcp_header_bytes(tcp_header_t *t)
Definition: tcp_packet.h:93
u16 flags
Definition: pci.h:213
#define vec_foreach(var, vec)
Vector iterator.
u16 flags
Copy of main node flags.
Definition: node.h:509
static void vlib_frame_no_append(vlib_frame_t *f)
Definition: node_funcs.h:224
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:203
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
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:612
vnet_feature_main_t feature_main
Definition: feature.c:19
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:305
Definition: defs.h:46