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/ip/ip.h>
39 #include <vnet/ethernet/ethernet.h>
40 #include <vnet/devices/devices.h>
41 #include <vnet/feature/feature.h>
43 #include <vnet/devices/virtio/vhost_user.h>
44 #include <vnet/devices/virtio/vhost_user_inline.h>
46 #include <vnet/gso/hdr_offset_parser.h>
48 * On the transmit side, we keep processing the buffers from vlib in the while
49 * loop and prepare the copy order to be executed later. However, the static
50 * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
51 * entries. In order to not corrupt memory, we have to do the copy when the
52 * static array reaches the copy threshold. We subtract 40 in case the code
53 * goes into the inner loop for a maximum of 64k frames which may require
54 * more array entries. We subtract 200 because our default buffer size is
55 * 2048 and the default desc len is likely 1536. While it takes less than 40
56 * vlib buffers for the jumbo frame, it may take twice as much descriptors
57 * for the same jumbo frame. Use 200 for the extra head room.
59 #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 200)
61 extern vnet_device_class_t vhost_user_device_class;
63 #define foreach_vhost_user_tx_func_error \
65 _(NOT_READY, "vhost vring not ready") \
66 _(DOWN, "vhost interface is down") \
67 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
68 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
69 _(MMAP_FAIL, "mmap failure") \
70 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
74 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
75 foreach_vhost_user_tx_func_error
77 VHOST_USER_TX_FUNC_N_ERROR,
78 } vhost_user_tx_func_error_t;
80 static __clib_unused char *vhost_user_tx_func_error_strings[] = {
82 foreach_vhost_user_tx_func_error
86 static __clib_unused u8 *
87 format_vhost_user_interface_name (u8 * s, va_list * args)
89 u32 i = va_arg (*args, u32);
90 u32 show_dev_instance = ~0;
91 vhost_user_main_t *vum = &vhost_user_main;
93 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
94 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
96 if (show_dev_instance != ~0)
97 i = show_dev_instance;
99 s = format (s, "VirtualEthernet0/0/%d", i);
103 static __clib_unused int
104 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
106 // FIXME: check if the new dev instance is already used
107 vhost_user_main_t *vum = &vhost_user_main;
108 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
111 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
112 hi->dev_instance, ~0);
114 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
117 vu_log_debug (vui, "renumbered vhost-user interface dev_instance %d to %d",
118 hi->dev_instance, new_dev_instance);
124 * @brief Try once to lock the vring
125 * @return 0 on success, non-zero on failure.
127 static_always_inline int
128 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
130 return clib_atomic_test_and_set (vui->vring_locks[qid]);
134 * @brief Spin until the vring is successfully locked
136 static_always_inline void
137 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
139 while (vhost_user_vring_try_lock (vui, qid))
144 * @brief Unlock the vring lock
146 static_always_inline void
147 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
149 clib_atomic_release (vui->vring_locks[qid]);
152 static_always_inline void
153 vhost_user_tx_trace (vhost_trace_t * t,
154 vhost_user_intf_t * vui, u16 qid,
155 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
157 vhost_user_main_t *vum = &vhost_user_main;
158 u32 last_avail_idx = rxvq->last_avail_idx;
159 u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
160 vring_desc_t *hdr_desc = 0;
163 clib_memset (t, 0, sizeof (*t));
164 t->device_index = vui - vum->vhost_user_interfaces;
167 hdr_desc = &rxvq->desc[desc_current];
168 if (rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
170 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
171 /* Header is the first here */
172 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
174 if (rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT)
176 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
178 if (!(rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT) &&
179 !(rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT))
181 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
184 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
187 static_always_inline u32
188 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
189 u16 copy_len, u32 * map_hint)
191 void *dst0, *dst1, *dst2, *dst3;
192 if (PREDICT_TRUE (copy_len >= 4))
194 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
196 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
198 while (PREDICT_TRUE (copy_len >= 4))
204 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
207 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
210 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
211 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
213 clib_memcpy_fast (dst0, (void *) cpy[0].src, cpy[0].len);
214 clib_memcpy_fast (dst1, (void *) cpy[1].src, cpy[1].len);
216 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
217 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
224 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
226 clib_memcpy_fast (dst0, (void *) cpy->src, cpy->len);
227 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
234 static_always_inline void
235 vhost_user_handle_tx_offload (vhost_user_intf_t * vui, vlib_buffer_t * b,
236 virtio_net_hdr_t * hdr)
238 generic_header_offset_t gho = { 0 };
239 int is_ip4 = b->flags & VNET_BUFFER_F_IS_IP4;
240 int is_ip6 = b->flags & VNET_BUFFER_F_IS_IP6;
242 ASSERT (!(is_ip4 && is_ip6));
243 vnet_generic_header_offset_parser (b, &gho, 1 /* l2 */ , is_ip4, is_ip6);
244 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
249 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
250 ip4->checksum = ip4_header_checksum (ip4);
253 /* checksum offload */
254 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
256 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
257 hdr->csum_start = gho.l4_hdr_offset;
258 hdr->csum_offset = offsetof (udp_header_t, checksum);
260 (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
263 else if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
265 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
266 hdr->csum_start = gho.l4_hdr_offset;
267 hdr->csum_offset = offsetof (tcp_header_t, checksum);
269 (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
274 if (b->flags & VNET_BUFFER_F_GSO)
276 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
279 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)))
281 hdr->gso_size = vnet_buffer2 (b)->gso_size;
282 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
285 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)))
287 hdr->gso_size = vnet_buffer2 (b)->gso_size;
288 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
291 else if ((vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)) &&
292 (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
294 hdr->gso_size = vnet_buffer2 (b)->gso_size;
295 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
300 static_always_inline void
301 vhost_user_mark_desc_available (vlib_main_t * vm, vhost_user_vring_t * rxvq,
302 u16 * n_descs_processed, u8 chained,
303 vlib_frame_t * frame, u32 n_left)
306 vring_packed_desc_t *desc_table = rxvq->packed_desc;
307 u16 last_used_idx = rxvq->last_used_idx;
309 if (PREDICT_FALSE (*n_descs_processed == 0))
312 if (rxvq->used_wrap_counter)
313 flags = desc_table[last_used_idx & rxvq->qsz_mask].flags |
314 (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
316 flags = desc_table[last_used_idx & rxvq->qsz_mask].flags &
317 ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
319 vhost_user_advance_last_used_idx (rxvq);
321 for (desc_idx = 1; desc_idx < *n_descs_processed; desc_idx++)
323 if (rxvq->used_wrap_counter)
324 desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags |=
325 (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
327 desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags &=
328 ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
329 vhost_user_advance_last_used_idx (rxvq);
332 desc_table[last_used_idx & rxvq->qsz_mask].flags = flags;
334 *n_descs_processed = 0;
338 vring_packed_desc_t *desc_table = rxvq->packed_desc;
340 while (desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags &
342 vhost_user_advance_last_used_idx (rxvq);
344 /* Advance past the current chained table entries */
345 vhost_user_advance_last_used_idx (rxvq);
348 /* interrupt (call) handling */
349 if ((rxvq->callfd_idx != ~0) &&
350 (rxvq->avail_event->flags != VRING_EVENT_F_DISABLE))
352 vhost_user_main_t *vum = &vhost_user_main;
354 rxvq->n_since_last_int += frame->n_vectors - n_left;
355 if (rxvq->n_since_last_int > vum->coalesce_frames)
356 vhost_user_send_call (vm, rxvq);
360 static_always_inline void
361 vhost_user_tx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui,
362 u16 qid, vlib_buffer_t * b,
363 vhost_user_vring_t * rxvq)
365 vhost_user_main_t *vum = &vhost_user_main;
366 u32 last_avail_idx = rxvq->last_avail_idx;
367 u32 desc_current = last_avail_idx & rxvq->qsz_mask;
368 vring_packed_desc_t *hdr_desc = 0;
371 clib_memset (t, 0, sizeof (*t));
372 t->device_index = vui - vum->vhost_user_interfaces;
375 hdr_desc = &rxvq->packed_desc[desc_current];
376 if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)
378 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
379 /* Header is the first here */
380 hdr_desc = map_guest_mem (vui, rxvq->packed_desc[desc_current].addr,
383 if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT)
385 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
387 if (!(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) &&
388 !(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT))
390 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
393 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
396 static_always_inline uword
397 vhost_user_device_class_packed (vlib_main_t * vm, vlib_node_runtime_t * node,
398 vlib_frame_t * frame)
400 u32 *buffers = vlib_frame_vector_args (frame);
401 u32 n_left = frame->n_vectors;
402 vhost_user_main_t *vum = &vhost_user_main;
403 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
404 vhost_user_intf_t *vui =
405 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
407 vhost_user_vring_t *rxvq;
409 u32 thread_index = vm->thread_index;
410 vhost_cpu_t *cpu = &vum->cpus[thread_index];
415 vring_packed_desc_t *desc_table;
417 u16 desc_head, desc_index, desc_len;
418 u16 n_descs_processed;
419 u8 indirect, chained;
421 qid = VHOST_VRING_IDX_RX (*vec_elt_at_index (vui->per_cpu_tx_qid,
423 rxvq = &vui->vrings[qid];
426 error = VHOST_USER_TX_FUNC_ERROR_NONE;
429 n_descs_processed = 0;
433 vlib_buffer_t *b0, *current_b0;
434 uword buffer_map_addr;
437 u32 total_desc_len = 0;
442 if (PREDICT_TRUE (n_left > 1))
443 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
445 b0 = vlib_get_buffer (vm, buffers[0]);
446 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
448 cpu->current_trace = vlib_add_trace (vm, node, b0,
449 sizeof (*cpu->current_trace));
450 vhost_user_tx_trace_packed (cpu->current_trace, vui, qid / 2, b0,
454 desc_table = rxvq->packed_desc;
455 desc_head = desc_index = rxvq->last_avail_idx & rxvq->qsz_mask;
456 if (PREDICT_FALSE (!vhost_user_packed_desc_available (rxvq, desc_head)))
458 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
462 * Go deeper in case of indirect descriptor.
463 * To test it, turn off mrg_rxbuf.
465 if (desc_table[desc_head].flags & VRING_DESC_F_INDIRECT)
468 if (PREDICT_FALSE (desc_table[desc_head].len <
469 sizeof (vring_packed_desc_t)))
471 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
474 n_entries = desc_table[desc_head].len >> 4;
475 desc_table = map_guest_mem (vui, desc_table[desc_index].addr,
477 if (PREDICT_FALSE (desc_table == 0))
479 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
484 else if (rxvq->packed_desc[desc_head].flags & VRING_DESC_F_NEXT)
487 desc_len = vui->virtio_net_hdr_sz;
488 buffer_map_addr = desc_table[desc_index].addr;
489 buffer_len = desc_table[desc_index].len;
491 /* Get a header from the header array */
492 virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len];
495 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
496 hdr->num_buffers = 1;
498 or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) ||
499 (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) ||
500 (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM);
502 /* Guest supports csum offload and buffer requires checksum offload? */
504 (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)))
505 vhost_user_handle_tx_offload (vui, b0, &hdr->hdr);
507 /* Prepare a copy order executed later for the header */
508 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
509 vhost_copy_t *cpy = &cpu->copy[copy_len];
511 cpy->len = vui->virtio_net_hdr_sz;
512 cpy->dst = buffer_map_addr;
513 cpy->src = (uword) hdr;
515 buffer_map_addr += vui->virtio_net_hdr_sz;
516 buffer_len -= vui->virtio_net_hdr_sz;
517 bytes_left = b0->current_length;
527 * Next one is chained
528 * Test it with both indirect and mrg_rxbuf off
530 if (PREDICT_FALSE (!(desc_table[desc_index].flags &
534 * Last descriptor in chain.
535 * Dequeue queued descriptors for this packet
537 vhost_user_dequeue_chained_descs (rxvq,
539 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
542 vhost_user_advance_last_avail_idx (rxvq);
543 desc_index = rxvq->last_avail_idx & rxvq->qsz_mask;
545 buffer_map_addr = desc_table[desc_index].addr;
546 buffer_len = desc_table[desc_index].len;
547 total_desc_len += desc_len;
554 * Test it with mrg_rxnuf off
556 if (PREDICT_TRUE (n_entries > 0))
560 /* Dequeue queued descriptors for this packet */
561 vhost_user_dequeue_chained_descs (rxvq,
563 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
566 total_desc_len += desc_len;
567 desc_index = (desc_index + 1) & rxvq->qsz_mask;
568 buffer_map_addr = desc_table[desc_index].addr;
569 buffer_len = desc_table[desc_index].len;
572 else if (vui->virtio_net_hdr_sz == 12)
576 * This is the default setting for the guest VM
578 virtio_net_hdr_mrg_rxbuf_t *hdr =
579 &cpu->tx_headers[tx_headers_len - 1];
581 desc_table[desc_index].len = desc_len;
582 vhost_user_advance_last_avail_idx (rxvq);
583 desc_head = desc_index =
584 rxvq->last_avail_idx & rxvq->qsz_mask;
589 if (PREDICT_FALSE (!vhost_user_packed_desc_available
592 /* Dequeue queued descriptors for this packet */
593 vhost_user_dequeue_descs (rxvq, hdr,
595 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
599 buffer_map_addr = desc_table[desc_index].addr;
600 buffer_len = desc_table[desc_index].len;
604 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
609 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
610 vhost_copy_t *cpy = &cpu->copy[copy_len];
612 cpy->len = bytes_left;
613 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
614 cpy->dst = buffer_map_addr;
615 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
616 current_b0->current_length - bytes_left;
618 bytes_left -= cpy->len;
619 buffer_len -= cpy->len;
620 buffer_map_addr += cpy->len;
621 desc_len += cpy->len;
623 CLIB_PREFETCH (&rxvq->packed_desc, CLIB_CACHE_LINE_BYTES, LOAD);
625 /* Check if vlib buffer has more data. If not, get more or break */
626 if (PREDICT_TRUE (!bytes_left))
629 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
631 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
632 bytes_left = current_b0->current_length;
642 /* Move from available to used ring */
643 total_desc_len += desc_len;
644 rxvq->packed_desc[desc_head].len = total_desc_len;
646 vhost_user_advance_last_avail_table_idx (vui, rxvq, chained);
649 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
650 cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1];
655 * Do the copy periodically to prevent
656 * cpu->copy array overflow and corrupt memory
658 if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD) || chained)
660 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
662 vlib_error_count (vm, node->node_index,
663 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
666 /* give buffers back to driver */
667 vhost_user_mark_desc_available (vm, rxvq, &n_descs_processed,
668 chained, frame, n_left);
675 if (PREDICT_TRUE (copy_len))
677 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
679 vlib_error_count (vm, node->node_index,
680 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
682 vhost_user_mark_desc_available (vm, rxvq, &n_descs_processed, chained,
687 * When n_left is set, error is always set to something too.
688 * In case error is due to lack of remaining buffers, we go back up and
690 * The idea is that it is better to waste some time on packets
691 * that have been processed already than dropping them and get
692 * more fresh packets with a good likelyhood that they will be dropped too.
693 * This technique also gives more time to VM driver to pick-up packets.
694 * In case the traffic flows from physical to virtual interfaces, this
695 * technique will end-up leveraging the physical NIC buffer in order to
696 * absorb the VM's CPU jitter.
698 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
704 vhost_user_vring_unlock (vui, qid);
706 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
708 vlib_error_count (vm, node->node_index, error, n_left);
709 vlib_increment_simple_counter
710 (vnet_main.interface_main.sw_if_counters +
711 VNET_INTERFACE_COUNTER_DROP, thread_index, vui->sw_if_index, n_left);
714 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
715 return frame->n_vectors;
718 VNET_DEVICE_CLASS_TX_FN (vhost_user_device_class) (vlib_main_t * vm,
719 vlib_node_runtime_t *
720 node, vlib_frame_t * frame)
722 u32 *buffers = vlib_frame_vector_args (frame);
723 u32 n_left = frame->n_vectors;
724 vhost_user_main_t *vum = &vhost_user_main;
725 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
726 vhost_user_intf_t *vui =
727 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
729 vhost_user_vring_t *rxvq;
731 u32 thread_index = vm->thread_index;
732 vhost_cpu_t *cpu = &vum->cpus[thread_index];
739 if (PREDICT_FALSE (!vui->admin_up))
741 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
745 if (PREDICT_FALSE (!vui->is_ready))
747 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
751 qid = VHOST_VRING_IDX_RX (*vec_elt_at_index (vui->per_cpu_tx_qid,
753 rxvq = &vui->vrings[qid];
754 if (PREDICT_FALSE (rxvq->avail == 0))
756 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
760 if (PREDICT_FALSE (vui->use_tx_spinlock))
761 vhost_user_vring_lock (vui, qid);
763 if (vhost_user_is_packed_ring_supported (vui))
764 return (vhost_user_device_class_packed (vm, node, frame));
767 error = VHOST_USER_TX_FUNC_ERROR_NONE;
772 vlib_buffer_t *b0, *current_b0;
773 u16 desc_head, desc_index, desc_len;
774 vring_desc_t *desc_table;
775 uword buffer_map_addr;
779 if (PREDICT_TRUE (n_left > 1))
780 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
782 b0 = vlib_get_buffer (vm, buffers[0]);
784 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
786 cpu->current_trace = vlib_add_trace (vm, node, b0,
787 sizeof (*cpu->current_trace));
788 vhost_user_tx_trace (cpu->current_trace, vui, qid / 2, b0, rxvq);
791 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
793 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
797 desc_table = rxvq->desc;
798 desc_head = desc_index =
799 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
801 /* Go deeper in case of indirect descriptor
802 * I don't know of any driver providing indirect for RX. */
803 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT))
806 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
808 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
813 map_guest_mem (vui, rxvq->desc[desc_index].addr,
816 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
822 desc_len = vui->virtio_net_hdr_sz;
823 buffer_map_addr = desc_table[desc_index].addr;
824 buffer_len = desc_table[desc_index].len;
827 // Get a header from the header array
828 virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len];
831 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
832 hdr->num_buffers = 1; //This is local, no need to check
834 or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) ||
835 (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) ||
836 (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM);
838 /* Guest supports csum offload and buffer requires checksum offload? */
840 && (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)))
841 vhost_user_handle_tx_offload (vui, b0, &hdr->hdr);
843 // Prepare a copy order executed later for the header
844 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
845 vhost_copy_t *cpy = &cpu->copy[copy_len];
847 cpy->len = vui->virtio_net_hdr_sz;
848 cpy->dst = buffer_map_addr;
849 cpy->src = (uword) hdr;
852 buffer_map_addr += vui->virtio_net_hdr_sz;
853 buffer_len -= vui->virtio_net_hdr_sz;
854 bytes_left = b0->current_length;
860 if (desc_table[desc_index].flags & VRING_DESC_F_NEXT)
862 //Next one is chained
863 desc_index = desc_table[desc_index].next;
864 buffer_map_addr = desc_table[desc_index].addr;
865 buffer_len = desc_table[desc_index].len;
867 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
869 virtio_net_hdr_mrg_rxbuf_t *hdr =
870 &cpu->tx_headers[tx_headers_len - 1];
872 //Move from available to used buffer
873 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
875 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
877 vhost_user_log_dirty_ring (vui, rxvq,
878 ring[rxvq->last_used_idx &
881 rxvq->last_avail_idx++;
882 rxvq->last_used_idx++;
887 (rxvq->last_avail_idx == rxvq->avail->idx))
889 //Dequeue queued descriptors for this packet
890 rxvq->last_used_idx -= hdr->num_buffers - 1;
891 rxvq->last_avail_idx -= hdr->num_buffers - 1;
892 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
896 desc_table = rxvq->desc;
897 desc_head = desc_index =
898 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
900 (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT))
902 //It is seriously unlikely that a driver will put indirect descriptor
903 //after non-indirect descriptor.
905 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
907 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
913 rxvq->desc[desc_index].addr,
916 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
921 buffer_map_addr = desc_table[desc_index].addr;
922 buffer_len = desc_table[desc_index].len;
926 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
932 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
933 vhost_copy_t *cpy = &cpu->copy[copy_len];
935 cpy->len = bytes_left;
936 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
937 cpy->dst = buffer_map_addr;
938 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
939 current_b0->current_length - bytes_left;
941 bytes_left -= cpy->len;
942 buffer_len -= cpy->len;
943 buffer_map_addr += cpy->len;
944 desc_len += cpy->len;
946 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
949 // Check if vlib buffer has more data. If not, get more or break.
950 if (PREDICT_TRUE (!bytes_left))
953 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
955 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
956 bytes_left = current_b0->current_length;
966 //Move from available to used ring
967 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
968 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
969 vhost_user_log_dirty_ring (vui, rxvq,
970 ring[rxvq->last_used_idx & rxvq->qsz_mask]);
971 rxvq->last_avail_idx++;
972 rxvq->last_used_idx++;
974 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
976 cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1];
979 n_left--; //At the end for error counting when 'goto done' is invoked
982 * Do the copy periodically to prevent
983 * cpu->copy array overflow and corrupt memory
985 if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
987 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
990 vlib_error_count (vm, node->node_index,
991 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
995 /* give buffers back to driver */
996 CLIB_MEMORY_BARRIER ();
997 rxvq->used->idx = rxvq->last_used_idx;
998 vhost_user_log_dirty_ring (vui, rxvq, idx);
1004 //Do the memory copies
1005 if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len,
1008 vlib_error_count (vm, node->node_index,
1009 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1012 CLIB_MEMORY_BARRIER ();
1013 rxvq->used->idx = rxvq->last_used_idx;
1014 vhost_user_log_dirty_ring (vui, rxvq, idx);
1017 * When n_left is set, error is always set to something too.
1018 * In case error is due to lack of remaining buffers, we go back up and
1020 * The idea is that it is better to waste some time on packets
1021 * that have been processed already than dropping them and get
1022 * more fresh packets with a good likelihood that they will be dropped too.
1023 * This technique also gives more time to VM driver to pick-up packets.
1024 * In case the traffic flows from physical to virtual interfaces, this
1025 * technique will end-up leveraging the physical NIC buffer in order to
1026 * absorb the VM's CPU jitter.
1028 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
1034 /* interrupt (call) handling */
1035 if ((rxvq->callfd_idx != ~0) &&
1036 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1038 rxvq->n_since_last_int += frame->n_vectors - n_left;
1040 if (rxvq->n_since_last_int > vum->coalesce_frames)
1041 vhost_user_send_call (vm, rxvq);
1044 vhost_user_vring_unlock (vui, qid);
1047 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
1049 vlib_error_count (vm, node->node_index, error, n_left);
1050 vlib_increment_simple_counter
1051 (vnet_main.interface_main.sw_if_counters
1052 + VNET_INTERFACE_COUNTER_DROP,
1053 thread_index, vui->sw_if_index, n_left);
1056 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
1057 return frame->n_vectors;
1060 static __clib_unused clib_error_t *
1061 vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
1062 u32 qid, vnet_hw_if_rx_mode mode)
1064 vlib_main_t *vm = vnm->vlib_main;
1065 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1066 vhost_user_main_t *vum = &vhost_user_main;
1067 vhost_user_intf_t *vui =
1068 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1069 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1071 if ((mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1072 (mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
1074 if (txvq->kickfd_idx == ~0)
1076 // We cannot support interrupt mode if the driver opts out
1077 return clib_error_return (0, "Driver does not support interrupt");
1079 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
1082 // Start the timer if this is the first encounter on interrupt
1084 if ((vum->ifq_count == 1) &&
1085 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1086 vlib_process_signal_event (vm,
1087 vhost_user_send_interrupt_node.index,
1088 VHOST_USER_EVENT_START_TIMER, 0);
1091 else if (mode == VNET_HW_IF_RX_MODE_POLLING)
1093 if (((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1094 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) && vum->ifq_count)
1097 // Stop the timer if there is no more interrupt interface/queue
1098 if ((vum->ifq_count == 0) &&
1099 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1100 vlib_process_signal_event (vm,
1101 vhost_user_send_interrupt_node.index,
1102 VHOST_USER_EVENT_STOP_TIMER, 0);
1107 if (mode == VNET_HW_IF_RX_MODE_POLLING)
1108 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1109 else if ((mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
1110 (mode == VNET_HW_IF_RX_MODE_INTERRUPT))
1111 txvq->used->flags = 0;
1114 vu_log_err (vui, "unhandled mode %d changed for if %d queue %d", mode,
1116 return clib_error_return (0, "unsupported");
1122 static __clib_unused clib_error_t *
1123 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
1126 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1127 vhost_user_main_t *vum = &vhost_user_main;
1128 vhost_user_intf_t *vui =
1129 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1130 u8 link_old, link_new;
1132 link_old = vui_is_link_up (vui);
1134 vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1136 link_new = vui_is_link_up (vui);
1138 if (link_old != link_new)
1139 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, link_new ?
1140 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1142 return /* no error */ 0;
1146 VNET_DEVICE_CLASS (vhost_user_device_class) = {
1147 .name = "vhost-user",
1148 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1149 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1150 .format_device_name = format_vhost_user_interface_name,
1151 .name_renumber = vhost_user_name_renumber,
1152 .admin_up_down_function = vhost_user_interface_admin_up_down,
1153 .rx_mode_change_function = vhost_user_interface_rx_mode_change,
1154 .format_tx_trace = format_vhost_trace,
1160 * fd.io coding-style-patch-verification: ON
1163 * eval: (c-set-style "gnu")