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