2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
21 #include <fcntl.h> /* for open */
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <sys/uio.h> /* for iovec */
28 #include <netinet/in.h>
31 #include <linux/if_arp.h>
32 #include <linux/if_tun.h>
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
37 #include <vnet/ethernet/ethernet.h>
38 #include <vnet/devices/devices.h>
39 #include <vnet/feature/feature.h>
41 #include <vnet/devices/virtio/vhost_user.h>
42 #include <vnet/devices/virtio/vhost_user_inline.h>
44 #include <vnet/gso/hdr_offset_parser.h>
46 * On the transmit side, we keep processing the buffers from vlib in the while
47 * loop and prepare the copy order to be executed later. However, the static
48 * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
49 * entries. In order to not corrupt memory, we have to do the copy when the
50 * static array reaches the copy threshold. We subtract 40 in case the code
51 * goes into the inner loop for a maximum of 64k frames which may require
52 * more array entries. We subtract 200 because our default buffer size is
53 * 2048 and the default desc len is likely 1536. While it takes less than 40
54 * vlib buffers for the jumbo frame, it may take twice as much descriptors
55 * for the same jumbo frame. Use 200 for the extra head room.
57 #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 200)
59 extern vnet_device_class_t vhost_user_device_class;
61 #define foreach_vhost_user_tx_func_error \
63 _(NOT_READY, "vhost vring not ready") \
64 _(DOWN, "vhost interface is down") \
65 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
66 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
67 _(MMAP_FAIL, "mmap failure") \
68 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
72 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
73 foreach_vhost_user_tx_func_error
75 VHOST_USER_TX_FUNC_N_ERROR,
76 } vhost_user_tx_func_error_t;
78 static __clib_unused char *vhost_user_tx_func_error_strings[] = {
80 foreach_vhost_user_tx_func_error
84 static __clib_unused u8 *
85 format_vhost_user_interface_name (u8 * s, va_list * args)
87 u32 i = va_arg (*args, u32);
88 u32 show_dev_instance = ~0;
89 vhost_user_main_t *vum = &vhost_user_main;
91 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
92 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
94 if (show_dev_instance != ~0)
95 i = show_dev_instance;
97 s = format (s, "VirtualEthernet0/0/%d", i);
101 static __clib_unused int
102 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
104 // FIXME: check if the new dev instance is already used
105 vhost_user_main_t *vum = &vhost_user_main;
106 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
109 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
110 hi->dev_instance, ~0);
112 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
115 vu_log_debug (vui, "renumbered vhost-user interface dev_instance %d to %d",
116 hi->dev_instance, new_dev_instance);
122 * @brief Spin until the vring is successfully locked
124 static_always_inline void
125 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
127 clib_spinlock_lock_if_init (&vui->vrings[qid].vring_lock);
131 * @brief Unlock the vring lock
133 static_always_inline void
134 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
136 clib_spinlock_unlock_if_init (&vui->vrings[qid].vring_lock);
139 static_always_inline void
140 vhost_user_tx_trace (vhost_trace_t * t,
141 vhost_user_intf_t * vui, u16 qid,
142 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
144 vhost_user_main_t *vum = &vhost_user_main;
145 u32 last_avail_idx = rxvq->last_avail_idx;
146 u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
147 vring_desc_t *hdr_desc = 0;
150 clib_memset (t, 0, sizeof (*t));
151 t->device_index = vui - vum->vhost_user_interfaces;
154 hdr_desc = &rxvq->desc[desc_current];
155 if (rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
157 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
158 /* Header is the first here */
159 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
161 if (rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT)
163 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
165 if (!(rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT) &&
166 !(rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT))
168 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
171 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
174 static_always_inline u32
175 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
176 u16 copy_len, u32 * map_hint)
178 void *dst0, *dst1, *dst2, *dst3;
179 if (PREDICT_TRUE (copy_len >= 4))
181 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
183 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
185 while (PREDICT_TRUE (copy_len >= 4))
191 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
194 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
197 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
198 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
200 clib_memcpy_fast (dst0, (void *) cpy[0].src, cpy[0].len);
201 clib_memcpy_fast (dst1, (void *) cpy[1].src, cpy[1].len);
203 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
204 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
211 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
213 clib_memcpy_fast (dst0, (void *) cpy->src, cpy->len);
214 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
221 static_always_inline void
222 vhost_user_handle_tx_offload (vhost_user_intf_t * vui, vlib_buffer_t * b,
223 virtio_net_hdr_t * hdr)
225 generic_header_offset_t gho = { 0 };
226 int is_ip4 = b->flags & VNET_BUFFER_F_IS_IP4;
227 int is_ip6 = b->flags & VNET_BUFFER_F_IS_IP6;
229 ASSERT (!(is_ip4 && is_ip6));
230 vnet_generic_header_offset_parser (b, &gho, 1 /* l2 */ , is_ip4, is_ip6);
231 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
236 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
237 ip4->checksum = ip4_header_checksum (ip4);
240 /* checksum offload */
241 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
243 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
244 hdr->csum_start = gho.l4_hdr_offset;
245 hdr->csum_offset = offsetof (udp_header_t, checksum);
247 else if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
249 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
250 hdr->csum_start = gho.l4_hdr_offset;
251 hdr->csum_offset = offsetof (tcp_header_t, checksum);
255 if (b->flags & VNET_BUFFER_F_GSO)
257 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
260 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)))
262 hdr->gso_size = vnet_buffer2 (b)->gso_size;
263 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
266 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)))
268 hdr->gso_size = vnet_buffer2 (b)->gso_size;
269 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
272 else if ((vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)) &&
273 (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
275 hdr->gso_size = vnet_buffer2 (b)->gso_size;
276 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
281 static_always_inline void
282 vhost_user_mark_desc_available (vlib_main_t * vm, vhost_user_vring_t * rxvq,
283 u16 * n_descs_processed, u8 chained,
284 vlib_frame_t * frame, u32 n_left)
287 vring_packed_desc_t *desc_table = rxvq->packed_desc;
288 u16 last_used_idx = rxvq->last_used_idx;
290 if (PREDICT_FALSE (*n_descs_processed == 0))
293 if (rxvq->used_wrap_counter)
294 flags = desc_table[last_used_idx & rxvq->qsz_mask].flags |
295 (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
297 flags = desc_table[last_used_idx & rxvq->qsz_mask].flags &
298 ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
300 vhost_user_advance_last_used_idx (rxvq);
302 for (desc_idx = 1; desc_idx < *n_descs_processed; desc_idx++)
304 if (rxvq->used_wrap_counter)
305 desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags |=
306 (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
308 desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags &=
309 ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
310 vhost_user_advance_last_used_idx (rxvq);
313 desc_table[last_used_idx & rxvq->qsz_mask].flags = flags;
315 *n_descs_processed = 0;
319 vring_packed_desc_t *desc_table = rxvq->packed_desc;
321 while (desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags &
323 vhost_user_advance_last_used_idx (rxvq);
325 /* Advance past the current chained table entries */
326 vhost_user_advance_last_used_idx (rxvq);
329 /* interrupt (call) handling */
330 if ((rxvq->callfd_idx != ~0) &&
331 (rxvq->avail_event->flags != VRING_EVENT_F_DISABLE))
333 vhost_user_main_t *vum = &vhost_user_main;
335 rxvq->n_since_last_int += frame->n_vectors - n_left;
336 if (rxvq->n_since_last_int > vum->coalesce_frames)
337 vhost_user_send_call (vm, rxvq);
341 static_always_inline void
342 vhost_user_tx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui,
343 u16 qid, vlib_buffer_t * b,
344 vhost_user_vring_t * rxvq)
346 vhost_user_main_t *vum = &vhost_user_main;
347 u32 last_avail_idx = rxvq->last_avail_idx;
348 u32 desc_current = last_avail_idx & rxvq->qsz_mask;
349 vring_packed_desc_t *hdr_desc = 0;
352 clib_memset (t, 0, sizeof (*t));
353 t->device_index = vui - vum->vhost_user_interfaces;
356 hdr_desc = &rxvq->packed_desc[desc_current];
357 if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)
359 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
360 /* Header is the first here */
361 hdr_desc = map_guest_mem (vui, rxvq->packed_desc[desc_current].addr,
364 if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT)
366 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
368 if (!(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) &&
369 !(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT))
371 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
374 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
377 static_always_inline uword
378 vhost_user_device_class_packed (vlib_main_t * vm, vlib_node_runtime_t * node,
379 vlib_frame_t * frame)
381 u32 *buffers = vlib_frame_vector_args (frame);
382 u32 n_left = frame->n_vectors;
383 vhost_user_main_t *vum = &vhost_user_main;
384 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
385 vhost_user_intf_t *vui =
386 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
388 vhost_user_vring_t *rxvq;
390 u32 thread_index = vm->thread_index;
391 vhost_cpu_t *cpu = &vum->cpus[thread_index];
396 vring_packed_desc_t *desc_table;
398 u16 desc_head, desc_index, desc_len;
399 u16 n_descs_processed;
400 u8 indirect, chained;
402 qid = VHOST_VRING_IDX_RX (*vec_elt_at_index (vui->per_cpu_tx_qid,
404 rxvq = &vui->vrings[qid];
407 error = VHOST_USER_TX_FUNC_ERROR_NONE;
410 n_descs_processed = 0;
414 vlib_buffer_t *b0, *current_b0;
415 uword buffer_map_addr;
418 u32 total_desc_len = 0;
423 if (PREDICT_TRUE (n_left > 1))
424 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
426 b0 = vlib_get_buffer (vm, buffers[0]);
427 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
429 cpu->current_trace = vlib_add_trace (vm, node, b0,
430 sizeof (*cpu->current_trace));
431 vhost_user_tx_trace_packed (cpu->current_trace, vui, qid / 2, b0,
435 desc_table = rxvq->packed_desc;
436 desc_head = desc_index = rxvq->last_avail_idx & rxvq->qsz_mask;
437 if (PREDICT_FALSE (!vhost_user_packed_desc_available (rxvq, desc_head)))
439 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
443 * Go deeper in case of indirect descriptor.
444 * To test it, turn off mrg_rxbuf.
446 if (desc_table[desc_head].flags & VRING_DESC_F_INDIRECT)
449 if (PREDICT_FALSE (desc_table[desc_head].len <
450 sizeof (vring_packed_desc_t)))
452 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
455 n_entries = desc_table[desc_head].len >> 4;
456 desc_table = map_guest_mem (vui, desc_table[desc_index].addr,
458 if (PREDICT_FALSE (desc_table == 0))
460 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
465 else if (rxvq->packed_desc[desc_head].flags & VRING_DESC_F_NEXT)
468 desc_len = vui->virtio_net_hdr_sz;
469 buffer_map_addr = desc_table[desc_index].addr;
470 buffer_len = desc_table[desc_index].len;
472 /* Get a header from the header array */
473 virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len];
476 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
477 hdr->num_buffers = 1;
479 or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) ||
480 (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) ||
481 (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM);
483 /* Guest supports csum offload and buffer requires checksum offload? */
485 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)))
486 vhost_user_handle_tx_offload (vui, b0, &hdr->hdr);
488 /* Prepare a copy order executed later for the header */
489 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
490 vhost_copy_t *cpy = &cpu->copy[copy_len];
492 cpy->len = vui->virtio_net_hdr_sz;
493 cpy->dst = buffer_map_addr;
494 cpy->src = (uword) hdr;
496 buffer_map_addr += vui->virtio_net_hdr_sz;
497 buffer_len -= vui->virtio_net_hdr_sz;
498 bytes_left = b0->current_length;
508 * Next one is chained
509 * Test it with both indirect and mrg_rxbuf off
511 if (PREDICT_FALSE (!(desc_table[desc_index].flags &
515 * Last descriptor in chain.
516 * Dequeue queued descriptors for this packet
518 vhost_user_dequeue_chained_descs (rxvq,
520 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
523 vhost_user_advance_last_avail_idx (rxvq);
524 desc_index = rxvq->last_avail_idx & rxvq->qsz_mask;
526 buffer_map_addr = desc_table[desc_index].addr;
527 buffer_len = desc_table[desc_index].len;
528 total_desc_len += desc_len;
535 * Test it with mrg_rxnuf off
537 if (PREDICT_TRUE (n_entries > 0))
541 /* Dequeue queued descriptors for this packet */
542 vhost_user_dequeue_chained_descs (rxvq,
544 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
547 total_desc_len += desc_len;
548 desc_index = (desc_index + 1) & rxvq->qsz_mask;
549 buffer_map_addr = desc_table[desc_index].addr;
550 buffer_len = desc_table[desc_index].len;
553 else if (vui->virtio_net_hdr_sz == 12)
557 * This is the default setting for the guest VM
559 virtio_net_hdr_mrg_rxbuf_t *hdr =
560 &cpu->tx_headers[tx_headers_len - 1];
562 desc_table[desc_index].len = desc_len;
563 vhost_user_advance_last_avail_idx (rxvq);
564 desc_head = desc_index =
565 rxvq->last_avail_idx & rxvq->qsz_mask;
570 if (PREDICT_FALSE (!vhost_user_packed_desc_available
573 /* Dequeue queued descriptors for this packet */
574 vhost_user_dequeue_descs (rxvq, hdr,
576 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
580 buffer_map_addr = desc_table[desc_index].addr;
581 buffer_len = desc_table[desc_index].len;
585 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
590 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
591 vhost_copy_t *cpy = &cpu->copy[copy_len];
593 cpy->len = bytes_left;
594 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
595 cpy->dst = buffer_map_addr;
596 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
597 current_b0->current_length - bytes_left;
599 bytes_left -= cpy->len;
600 buffer_len -= cpy->len;
601 buffer_map_addr += cpy->len;
602 desc_len += cpy->len;
604 CLIB_PREFETCH (&rxvq->packed_desc, CLIB_CACHE_LINE_BYTES, LOAD);
606 /* Check if vlib buffer has more data. If not, get more or break */
607 if (PREDICT_TRUE (!bytes_left))
610 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
612 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
613 bytes_left = current_b0->current_length;
623 /* Move from available to used ring */
624 total_desc_len += desc_len;
625 rxvq->packed_desc[desc_head].len = total_desc_len;
627 vhost_user_advance_last_avail_table_idx (vui, rxvq, chained);
630 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
631 cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1];
636 * Do the copy periodically to prevent
637 * cpu->copy array overflow and corrupt memory
639 if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD) || chained)
641 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
643 vlib_error_count (vm, node->node_index,
644 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
647 /* give buffers back to driver */
648 vhost_user_mark_desc_available (vm, rxvq, &n_descs_processed,
649 chained, frame, n_left);
656 if (PREDICT_TRUE (copy_len))
658 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
660 vlib_error_count (vm, node->node_index,
661 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
663 vhost_user_mark_desc_available (vm, rxvq, &n_descs_processed, chained,
668 * When n_left is set, error is always set to something too.
669 * In case error is due to lack of remaining buffers, we go back up and
671 * The idea is that it is better to waste some time on packets
672 * that have been processed already than dropping them and get
673 * more fresh packets with a good likelyhood that they will be dropped too.
674 * This technique also gives more time to VM driver to pick-up packets.
675 * In case the traffic flows from physical to virtual interfaces, this
676 * technique will end-up leveraging the physical NIC buffer in order to
677 * absorb the VM's CPU jitter.
679 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
685 vhost_user_vring_unlock (vui, qid);
687 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
689 vlib_error_count (vm, node->node_index, error, n_left);
690 vlib_increment_simple_counter
691 (vnet_main.interface_main.sw_if_counters +
692 VNET_INTERFACE_COUNTER_DROP, thread_index, vui->sw_if_index, n_left);
695 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
696 return frame->n_vectors;
699 VNET_DEVICE_CLASS_TX_FN (vhost_user_device_class) (vlib_main_t * vm,
700 vlib_node_runtime_t *
701 node, vlib_frame_t * frame)
703 u32 *buffers = vlib_frame_vector_args (frame);
704 u32 n_left = frame->n_vectors;
705 vhost_user_main_t *vum = &vhost_user_main;
706 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
707 vhost_user_intf_t *vui =
708 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
710 vhost_user_vring_t *rxvq;
712 u32 thread_index = vm->thread_index;
713 vhost_cpu_t *cpu = &vum->cpus[thread_index];
720 if (PREDICT_FALSE (!vui->admin_up))
722 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
726 if (PREDICT_FALSE (!vui->is_ready))
728 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
732 qid = VHOST_VRING_IDX_RX (*vec_elt_at_index (vui->per_cpu_tx_qid,
734 rxvq = &vui->vrings[qid];
735 if (PREDICT_FALSE (rxvq->avail == 0))
737 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
741 if (PREDICT_FALSE (vui->use_tx_spinlock))
742 vhost_user_vring_lock (vui, qid);
744 if (vhost_user_is_packed_ring_supported (vui))
745 return (vhost_user_device_class_packed (vm, node, frame));
748 error = VHOST_USER_TX_FUNC_ERROR_NONE;
753 vlib_buffer_t *b0, *current_b0;
754 u16 desc_head, desc_index, desc_len;
755 vring_desc_t *desc_table;
756 uword buffer_map_addr;
760 if (PREDICT_TRUE (n_left > 1))
761 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
763 b0 = vlib_get_buffer (vm, buffers[0]);
765 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
767 cpu->current_trace = vlib_add_trace (vm, node, b0,
768 sizeof (*cpu->current_trace));
769 vhost_user_tx_trace (cpu->current_trace, vui, qid / 2, b0, rxvq);
772 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
774 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
778 desc_table = rxvq->desc;
779 desc_head = desc_index =
780 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
782 /* Go deeper in case of indirect descriptor
783 * I don't know of any driver providing indirect for RX. */
784 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT))
787 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
789 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
794 map_guest_mem (vui, rxvq->desc[desc_index].addr,
797 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
803 desc_len = vui->virtio_net_hdr_sz;
804 buffer_map_addr = desc_table[desc_index].addr;
805 buffer_len = desc_table[desc_index].len;
808 // Get a header from the header array
809 virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len];
812 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
813 hdr->num_buffers = 1; //This is local, no need to check
815 or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) ||
816 (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) ||
817 (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM);
819 /* Guest supports csum offload and buffer requires checksum offload? */
821 && (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)))
822 vhost_user_handle_tx_offload (vui, b0, &hdr->hdr);
824 // Prepare a copy order executed later for the header
825 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
826 vhost_copy_t *cpy = &cpu->copy[copy_len];
828 cpy->len = vui->virtio_net_hdr_sz;
829 cpy->dst = buffer_map_addr;
830 cpy->src = (uword) hdr;
833 buffer_map_addr += vui->virtio_net_hdr_sz;
834 buffer_len -= vui->virtio_net_hdr_sz;
835 bytes_left = b0->current_length;
841 if (desc_table[desc_index].flags & VRING_DESC_F_NEXT)
843 //Next one is chained
844 desc_index = desc_table[desc_index].next;
845 buffer_map_addr = desc_table[desc_index].addr;
846 buffer_len = desc_table[desc_index].len;
848 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
850 virtio_net_hdr_mrg_rxbuf_t *hdr =
851 &cpu->tx_headers[tx_headers_len - 1];
853 //Move from available to used buffer
854 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
856 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
858 vhost_user_log_dirty_ring (vui, rxvq,
859 ring[rxvq->last_used_idx &
862 rxvq->last_avail_idx++;
863 rxvq->last_used_idx++;
868 (rxvq->last_avail_idx == rxvq->avail->idx))
870 //Dequeue queued descriptors for this packet
871 rxvq->last_used_idx -= hdr->num_buffers - 1;
872 rxvq->last_avail_idx -= hdr->num_buffers - 1;
873 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
877 desc_table = rxvq->desc;
878 desc_head = desc_index =
879 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
881 (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT))
883 //It is seriously unlikely that a driver will put indirect descriptor
884 //after non-indirect descriptor.
886 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
888 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
894 rxvq->desc[desc_index].addr,
897 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
902 buffer_map_addr = desc_table[desc_index].addr;
903 buffer_len = desc_table[desc_index].len;
907 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
913 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
914 vhost_copy_t *cpy = &cpu->copy[copy_len];
916 cpy->len = bytes_left;
917 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
918 cpy->dst = buffer_map_addr;
919 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
920 current_b0->current_length - bytes_left;
922 bytes_left -= cpy->len;
923 buffer_len -= cpy->len;
924 buffer_map_addr += cpy->len;
925 desc_len += cpy->len;
927 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
930 // Check if vlib buffer has more data. If not, get more or break.
931 if (PREDICT_TRUE (!bytes_left))
934 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
936 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
937 bytes_left = current_b0->current_length;
947 //Move from available to used ring
948 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
949 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
950 vhost_user_log_dirty_ring (vui, rxvq,
951 ring[rxvq->last_used_idx & rxvq->qsz_mask]);
952 rxvq->last_avail_idx++;
953 rxvq->last_used_idx++;
955 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
957 cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1];
960 n_left--; //At the end for error counting when 'goto done' is invoked
963 * Do the copy periodically to prevent
964 * cpu->copy array overflow and corrupt memory
966 if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
968 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
971 vlib_error_count (vm, node->node_index,
972 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
976 /* give buffers back to driver */
977 CLIB_MEMORY_BARRIER ();
978 rxvq->used->idx = rxvq->last_used_idx;
979 vhost_user_log_dirty_ring (vui, rxvq, idx);
985 //Do the memory copies
986 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
989 vlib_error_count (vm, node->node_index,
990 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
993 CLIB_MEMORY_BARRIER ();
994 rxvq->used->idx = rxvq->last_used_idx;
995 vhost_user_log_dirty_ring (vui, rxvq, idx);
998 * When n_left is set, error is always set to something too.
999 * In case error is due to lack of remaining buffers, we go back up and
1001 * The idea is that it is better to waste some time on packets
1002 * that have been processed already than dropping them and get
1003 * more fresh packets with a good likelihood that they will be dropped too.
1004 * This technique also gives more time to VM driver to pick-up packets.
1005 * In case the traffic flows from physical to virtual interfaces, this
1006 * technique will end-up leveraging the physical NIC buffer in order to
1007 * absorb the VM's CPU jitter.
1009 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
1015 /* interrupt (call) handling */
1016 if ((rxvq->callfd_idx != ~0) &&
1017 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1019 rxvq->n_since_last_int += frame->n_vectors - n_left;
1021 if (rxvq->n_since_last_int > vum->coalesce_frames)
1022 vhost_user_send_call (vm, rxvq);
1025 vhost_user_vring_unlock (vui, qid);
1028 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
1030 vlib_error_count (vm, node->node_index, error, n_left);
1031 vlib_increment_simple_counter
1032 (vnet_main.interface_main.sw_if_counters
1033 + VNET_INTERFACE_COUNTER_DROP,
1034 thread_index, vui->sw_if_index, n_left);
1037 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
1038 return frame->n_vectors;
1041 static __clib_unused clib_error_t *
1042 vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
1043 u32 qid, vnet_hw_if_rx_mode mode)
1045 vlib_main_t *vm = vnm->vlib_main;
1046 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1047 vhost_user_main_t *vum = &vhost_user_main;
1048 vhost_user_intf_t *vui =
1049 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1050 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1052 if ((mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1053 (mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
1055 if (txvq->kickfd_idx == ~0)
1057 // We cannot support interrupt mode if the driver opts out
1058 return clib_error_return (0, "Driver does not support interrupt");
1060 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
1063 // Start the timer if this is the first encounter on interrupt
1065 if ((vum->ifq_count == 1) &&
1066 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1067 vlib_process_signal_event (vm,
1068 vhost_user_send_interrupt_node.index,
1069 VHOST_USER_EVENT_START_TIMER, 0);
1072 else if (mode == VNET_HW_IF_RX_MODE_POLLING)
1074 if (((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1075 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) && vum->ifq_count)
1078 // Stop the timer if there is no more interrupt interface/queue
1079 if ((vum->ifq_count == 0) &&
1080 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1081 vlib_process_signal_event (vm,
1082 vhost_user_send_interrupt_node.index,
1083 VHOST_USER_EVENT_STOP_TIMER, 0);
1088 if (mode == VNET_HW_IF_RX_MODE_POLLING)
1089 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1090 else if ((mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
1091 (mode == VNET_HW_IF_RX_MODE_INTERRUPT))
1092 txvq->used->flags = 0;
1095 vu_log_err (vui, "unhandled mode %d changed for if %d queue %d", mode,
1097 return clib_error_return (0, "unsupported");
1103 static __clib_unused clib_error_t *
1104 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
1107 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1108 vhost_user_main_t *vum = &vhost_user_main;
1109 vhost_user_intf_t *vui =
1110 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1111 u8 link_old, link_new;
1113 link_old = vui_is_link_up (vui);
1115 vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1117 link_new = vui_is_link_up (vui);
1119 if (link_old != link_new)
1120 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, link_new ?
1121 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1123 return /* no error */ 0;
1127 VNET_DEVICE_CLASS (vhost_user_device_class) = {
1128 .name = "vhost-user",
1129 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1130 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1131 .format_device_name = format_vhost_user_interface_name,
1132 .name_renumber = vhost_user_name_renumber,
1133 .admin_up_down_function = vhost_user_interface_admin_up_down,
1134 .rx_mode_change_function = vhost_user_interface_rx_mode_change,
1135 .format_tx_trace = format_vhost_trace,
1141 * fd.io coding-style-patch-verification: ON
1144 * eval: (c-set-style "gnu")