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 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 #include <vnet/udp/udp_packet.h>
40 #include <vnet/interface/rx_queue_funcs.h>
42 #include <vnet/devices/virtio/vhost_user.h>
43 #include <vnet/devices/virtio/vhost_user_inline.h>
46 * When an RX queue is down but active, received packets
47 * must be discarded. This value controls up to how many
48 * packets will be discarded during each round.
50 #define VHOST_USER_DOWN_DISCARD_COUNT 256
53 * When the number of available buffers gets under this threshold,
54 * RX node will start discarding packets.
56 #define VHOST_USER_RX_BUFFER_STARVATION 32
59 * On the receive side, the host should free descriptors as soon
60 * as possible in order to avoid TX drop in the VM.
61 * This value controls the number of copy operations that are stacked
62 * before copy is done for all and descriptors are given back to
64 * The value 64 was obtained by testing (48 and 128 were not as good).
66 #define VHOST_USER_RX_COPY_THRESHOLD 64
68 extern vlib_node_registration_t vhost_user_input_node;
70 #define foreach_vhost_user_input_func_error \
71 _(NO_ERROR, "no error") \
72 _(NO_BUFFER, "no available buffer") \
73 _(MMAP_FAIL, "mmap failure") \
74 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
75 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
76 _(NOT_READY, "vhost interface not ready or down") \
77 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
81 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
82 foreach_vhost_user_input_func_error
84 VHOST_USER_INPUT_FUNC_N_ERROR,
85 } vhost_user_input_func_error_t;
87 static __clib_unused char *vhost_user_input_func_error_strings[] = {
89 foreach_vhost_user_input_func_error
93 static_always_inline void
94 vhost_user_rx_trace (vhost_trace_t * t,
95 vhost_user_intf_t * vui, u16 qid,
96 vlib_buffer_t * b, vhost_user_vring_t * txvq,
99 vhost_user_main_t *vum = &vhost_user_main;
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;
105 clib_memset (t, 0, sizeof (*t));
106 t->device_index = vui - vum->vhost_user_interfaces;
109 hdr_desc = &txvq->desc[desc_current];
110 if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
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);
116 if (txvq->desc[desc_current].flags & VRING_DESC_F_NEXT)
118 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
120 if (!(txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) &&
121 !(txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT))
123 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
126 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
128 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
130 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
134 u32 len = vui->virtio_net_hdr_sz;
135 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
139 static_always_inline u32
140 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
141 u16 copy_len, u32 * map_hint)
143 void *src0, *src1, *src2, *src3;
144 if (PREDICT_TRUE (copy_len >= 4))
146 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
148 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
151 while (PREDICT_TRUE (copy_len >= 4))
157 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
160 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
163 CLIB_PREFETCH (src2, 64, LOAD);
164 CLIB_PREFETCH (src3, 64, LOAD);
166 clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len);
167 clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len);
174 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
176 clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len);
184 * Try to discard packets from the tx ring (VPP RX path).
185 * Returns the number of discarded packets.
187 static_always_inline u32
188 vhost_user_rx_discard_packet (vlib_main_t * vm,
189 vhost_user_intf_t * vui,
190 vhost_user_vring_t * txvq, u32 discard_max)
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.
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)
204 if (avail_idx == last_avail_idx)
207 u16 desc_chain_head = txvq->avail->ring[last_avail_idx & mask];
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]);
217 txvq->last_avail_idx = last_avail_idx;
218 txvq->last_used_idx = last_used_idx;
219 CLIB_MEMORY_STORE_BARRIER ();
220 txvq->used->idx = txvq->last_used_idx;
221 vhost_user_log_dirty_ring (vui, txvq, idx);
222 return discarded_packets;
226 * In case of overflow, we need to rewind the array of allocated buffers.
228 static_always_inline void
229 vhost_user_input_rewind_buffers (vlib_main_t * vm,
230 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
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)
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;
244 cpu->rx_buffers_len++;
247 static_always_inline void
248 vhost_user_handle_rx_offload (vlib_buffer_t * b0, u8 * b0_data,
249 virtio_net_hdr_t * hdr)
253 ethernet_header_t *eh = (ethernet_header_t *) b0_data;
254 u16 ethertype = clib_net_to_host_u16 (eh->type);
255 u16 l2hdr_sz = sizeof (ethernet_header_t);
257 if (ethernet_frame_is_tagged (ethertype))
259 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
261 ethertype = clib_net_to_host_u16 (vlan->type);
262 l2hdr_sz += sizeof (*vlan);
263 if (ethertype == ETHERNET_TYPE_VLAN)
266 ethertype = clib_net_to_host_u16 (vlan->type);
267 l2hdr_sz += sizeof (*vlan);
270 vnet_buffer (b0)->l2_hdr_offset = 0;
271 vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
272 vnet_buffer (b0)->l4_hdr_offset = hdr->csum_start;
273 b0->flags |= (VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
274 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
275 VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
277 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
279 ip4_header_t *ip4 = (ip4_header_t *) (b0_data + l2hdr_sz);
280 l4_proto = ip4->protocol;
281 b0->flags |= VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
283 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
285 ip6_header_t *ip6 = (ip6_header_t *) (b0_data + l2hdr_sz);
286 l4_proto = ip6->protocol;
287 b0->flags |= VNET_BUFFER_F_IS_IP6;
290 if (l4_proto == IP_PROTOCOL_TCP)
292 tcp_header_t *tcp = (tcp_header_t *)
293 (b0_data + vnet_buffer (b0)->l4_hdr_offset);
294 l4_hdr_sz = tcp_header_bytes (tcp);
295 b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
297 else if (l4_proto == IP_PROTOCOL_UDP)
299 l4_hdr_sz = sizeof (udp_header_t);
300 b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
303 if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP)
305 vnet_buffer2 (b0)->gso_size = hdr->gso_size;
306 vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
307 b0->flags |= VNET_BUFFER_F_GSO;
309 else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4)
311 vnet_buffer2 (b0)->gso_size = hdr->gso_size;
312 vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
313 b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4);
315 else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6)
317 vnet_buffer2 (b0)->gso_size = hdr->gso_size;
318 vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
319 b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6);
323 static_always_inline void
324 vhost_user_input_do_interrupt (vlib_main_t * vm, vhost_user_intf_t * vui,
325 vhost_user_vring_t * txvq,
326 vhost_user_vring_t * rxvq)
328 f64 now = vlib_time_now (vm);
330 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
331 vhost_user_send_call (vm, vui, txvq);
333 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
334 vhost_user_send_call (vm, vui, rxvq);
337 static_always_inline void
338 vhost_user_input_setup_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
339 vhost_user_intf_t * vui,
340 u32 * current_config_index, u32 * next_index,
341 u32 ** to_next, u32 * n_left_to_next)
343 vnet_feature_main_t *fm = &feature_main;
344 u8 feature_arc_idx = fm->device_input_feature_arc_index;
346 if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index)))
348 vnet_feature_config_main_t *cm;
349 cm = &fm->feature_config_mains[feature_arc_idx];
350 *current_config_index = vec_elt (cm->config_index_by_sw_if_index,
352 vnet_get_config_data (&cm->config_main, current_config_index,
356 vlib_get_new_next_frame (vm, node, *next_index, *to_next, *n_left_to_next);
358 if (*next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT)
360 /* give some hints to ethernet-input */
361 vlib_next_frame_t *nf;
363 ethernet_input_frame_t *ef;
364 nf = vlib_node_runtime_get_next_frame (vm, node, *next_index);
365 f = vlib_get_frame (vm, nf->frame);
366 f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
368 ef = vlib_frame_scalar_args (f);
369 ef->sw_if_index = vui->sw_if_index;
370 ef->hw_if_index = vui->hw_if_index;
371 vlib_frame_no_append (f);
375 static_always_inline u32
376 vhost_user_if_input (vlib_main_t *vm, vhost_user_main_t *vum,
377 vhost_user_intf_t *vui, u16 qid,
378 vlib_node_runtime_t *node, u8 enable_csum)
380 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
381 vnet_feature_main_t *fm = &feature_main;
382 u16 n_rx_packets = 0;
385 u32 n_left_to_next, *to_next;
386 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
387 u32 n_trace = vlib_get_trace_count (vm, node);
388 u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
390 vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
392 u8 feature_arc_idx = fm->device_input_feature_arc_index;
393 u32 current_config_index = ~(u32) 0;
394 u16 mask = txvq->qsz_mask;
396 /* The descriptor table is not ready yet */
397 if (PREDICT_FALSE (txvq->avail == 0))
401 /* do we have pending interrupts ? */
402 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
403 vhost_user_input_do_interrupt (vm, vui, txvq, rxvq);
407 * For adaptive mode, it is optimized to reduce interrupts.
408 * If the scheduler switches the input node to polling due
409 * to burst of traffic, we tell the driver no interrupt.
410 * When the traffic subsides, the scheduler switches the node back to
411 * interrupt mode. We must tell the driver we want interrupt.
413 if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
416 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
418 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
419 /* Tell driver we want notification */
420 txvq->used->flags = 0;
422 /* Tell driver we don't want notification */
423 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
426 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
429 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
432 if (PREDICT_FALSE (n_left == 0))
435 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
438 * Discard input packet if interface is admin down or vring is not
440 * "For example, for a networking device, in the disabled state
441 * client must not supply any new RX packets, but must process
442 * and discard any TX packets."
444 vhost_user_rx_discard_packet (vm, vui, txvq,
445 VHOST_USER_DOWN_DISCARD_COUNT);
449 if (PREDICT_FALSE (n_left == (mask + 1)))
452 * Informational error logging when VPP is not
453 * receiving packets fast enough.
455 vlib_error_count (vm, node->node_index,
456 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
459 if (n_left > VLIB_FRAME_SIZE)
460 n_left = VLIB_FRAME_SIZE;
463 * For small packets (<2kB), we will not need more than one vlib buffer
464 * per packet. In case packets are bigger, we will just yield at some point
465 * in the loop and come back later. This is not an issue as for big packet,
466 * processing cost really comes from the memory copy.
467 * The assumption is that big packets will fit in 40 buffers.
469 if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 ||
470 cpu->rx_buffers_len < 40))
472 u32 curr_len = cpu->rx_buffers_len;
473 cpu->rx_buffers_len +=
474 vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len,
475 VHOST_USER_RX_BUFFERS_N - curr_len);
478 (cpu->rx_buffers_len < VHOST_USER_RX_BUFFER_STARVATION))
480 /* In case of buffer starvation, discard some packets from the queue
482 * We keep doing best effort for the remaining packets. */
483 u32 flush = (n_left + 1 > cpu->rx_buffers_len) ?
484 n_left + 1 - cpu->rx_buffers_len : 1;
485 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
488 vlib_increment_simple_counter (vnet_main.
489 interface_main.sw_if_counters +
490 VNET_INTERFACE_COUNTER_DROP,
491 vm->thread_index, vui->sw_if_index,
494 vlib_error_count (vm, vhost_user_input_node.index,
495 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
499 vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index,
500 &next_index, &to_next, &n_left_to_next);
502 u16 last_avail_idx = txvq->last_avail_idx;
503 u16 last_used_idx = txvq->last_used_idx;
507 vlib_buffer_t *b_head, *b_current;
510 u32 desc_data_offset;
511 vring_desc_t *desc_table = txvq->desc;
513 if (PREDICT_FALSE (cpu->rx_buffers_len <= 1))
515 /* Not enough rx_buffers
516 * Note: We yeld on 1 so we don't need to do an additional
517 * check for the next buffer prefetch.
523 desc_current = txvq->avail->ring[last_avail_idx & mask];
524 cpu->rx_buffers_len--;
525 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
526 b_head = b_current = vlib_get_buffer (vm, bi_current);
527 to_next[0] = bi_current; //We do that now so we can forget about bi_current
531 vlib_prefetch_buffer_with_index
532 (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD);
534 /* Just preset the used descriptor id and length for later */
535 txvq->used->ring[last_used_idx & mask].id = desc_current;
536 txvq->used->ring[last_used_idx & mask].len = 0;
537 vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]);
539 /* The buffer should already be initialized */
540 b_head->total_length_not_including_first_buffer = 0;
541 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
544 (n_trace > 0 && vlib_trace_buffer (vm, node, next_index, b_head,
545 /* follow_chain */ 0)))
548 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
549 vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx);
551 vlib_set_trace_count (vm, node, n_trace);
554 /* This depends on the setup but is very consistent
555 * So I think the CPU branch predictor will make a pretty good job
556 * at optimizing the decision. */
557 if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)
559 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
562 if (PREDICT_FALSE (desc_table == 0))
564 vlib_error_count (vm, node->node_index,
565 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
570 desc_data_offset = vui->virtio_net_hdr_sz;
574 virtio_net_hdr_mrg_rxbuf_t *hdr;
578 hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint);
579 if (PREDICT_FALSE (hdr == 0))
581 vlib_error_count (vm, node->node_index,
582 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
585 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
587 if ((desc_data_offset == desc_table[desc_current].len) &&
588 (desc_table[desc_current].flags & VRING_DESC_F_NEXT))
590 current = desc_table[desc_current].next;
591 b_data = map_guest_mem (vui, desc_table[current].addr,
593 if (PREDICT_FALSE (b_data == 0))
595 vlib_error_count (vm, node->node_index,
596 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL,
602 b_data = (u8 *) hdr + desc_data_offset;
604 vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
610 /* Get more input if necessary. Or end of packet. */
611 if (desc_data_offset == desc_table[desc_current].len)
613 if (PREDICT_FALSE (desc_table[desc_current].flags &
616 desc_current = desc_table[desc_current].next;
617 desc_data_offset = 0;
625 /* Get more output if necessary. Or end of packet. */
626 if (PREDICT_FALSE (b_current->current_length == buffer_data_size))
628 if (PREDICT_FALSE (cpu->rx_buffers_len == 0))
630 /* Cancel speculation */
635 * Checking if there are some left buffers.
636 * If not, just rewind the used buffers and stop.
637 * Note: Scheduled copies are not cancelled. This is
638 * not an issue as they would still be valid. Useless,
641 vhost_user_input_rewind_buffers (vm, cpu, b_head);
646 /* Get next output */
647 cpu->rx_buffers_len--;
648 u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len];
649 b_current->next_buffer = bi_next;
650 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
651 bi_current = bi_next;
652 b_current = vlib_get_buffer (vm, bi_current);
655 /* Prepare a copy order executed later for the data */
656 ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N);
657 vhost_copy_t *cpy = &cpu->copy[copy_len];
659 u32 desc_data_l = desc_table[desc_current].len - desc_data_offset;
660 cpy->len = buffer_data_size - b_current->current_length;
661 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
662 cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
663 b_current->current_length);
664 cpy->src = desc_table[desc_current].addr + desc_data_offset;
666 desc_data_offset += cpy->len;
668 b_current->current_length += cpy->len;
669 b_head->total_length_not_including_first_buffer += cpy->len;
674 n_rx_bytes += b_head->total_length_not_including_first_buffer;
677 b_head->total_length_not_including_first_buffer -=
678 b_head->current_length;
680 /* consume the descriptor and return it as used */
684 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
686 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
687 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
690 if (current_config_index != ~(u32) 0)
692 b_head->current_config_index = current_config_index;
693 vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
699 * Although separating memory copies from virtio ring parsing
700 * is beneficial, we can offer to perform the copies from time
701 * to time in order to free some space in the ring.
703 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
705 if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy,
706 copy_len, &map_hint)))
708 vlib_error_count (vm, node->node_index,
709 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
713 /* give buffers back to driver */
714 CLIB_MEMORY_STORE_BARRIER ();
715 txvq->used->idx = last_used_idx;
716 vhost_user_log_dirty_ring (vui, txvq, idx);
720 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
722 txvq->last_used_idx = last_used_idx;
723 txvq->last_avail_idx = last_avail_idx;
725 /* Do the memory copies */
726 if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len,
729 vlib_error_count (vm, node->node_index,
730 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
733 /* give buffers back to driver */
734 CLIB_MEMORY_STORE_BARRIER ();
735 txvq->used->idx = txvq->last_used_idx;
736 vhost_user_log_dirty_ring (vui, txvq, idx);
738 /* interrupt (call) handling */
739 if ((txvq->callfd_idx != ~0) &&
740 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
742 txvq->n_since_last_int += n_rx_packets;
744 if (txvq->n_since_last_int > vum->coalesce_frames)
745 vhost_user_send_call (vm, vui, txvq);
748 /* increase rx counters */
749 vlib_increment_combined_counter
750 (vnet_main.interface_main.combined_sw_if_counters
751 + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index,
752 n_rx_packets, n_rx_bytes);
754 vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets);
760 static_always_inline void
761 vhost_user_mark_desc_consumed (vhost_user_intf_t * vui,
762 vhost_user_vring_t * txvq, u16 desc_head,
763 u16 n_descs_processed)
765 vring_packed_desc_t *desc_table = txvq->packed_desc;
767 u16 mask = txvq->qsz_mask;
769 for (desc_idx = 0; desc_idx < n_descs_processed; desc_idx++)
771 if (txvq->used_wrap_counter)
772 desc_table[(desc_head + desc_idx) & mask].flags |=
773 (VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
775 desc_table[(desc_head + desc_idx) & mask].flags &=
776 ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED);
777 vhost_user_advance_last_used_idx (txvq);
781 static_always_inline void
782 vhost_user_rx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui,
783 u16 qid, vhost_user_vring_t * txvq,
786 vhost_user_main_t *vum = &vhost_user_main;
787 vring_packed_desc_t *hdr_desc;
788 virtio_net_hdr_mrg_rxbuf_t *hdr;
791 clib_memset (t, 0, sizeof (*t));
792 t->device_index = vui - vum->vhost_user_interfaces;
795 hdr_desc = &txvq->packed_desc[desc_current];
796 if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)
798 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
799 /* Header is the first here */
800 hdr_desc = map_guest_mem (vui, txvq->packed_desc[desc_current].addr,
803 if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT)
804 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
806 if (!(txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) &&
807 !(txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT))
808 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
810 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
812 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
813 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
816 u32 len = vui->virtio_net_hdr_sz;
817 clib_memcpy_fast (&t->hdr, hdr,
818 len > hdr_desc->len ? hdr_desc->len : len);
822 static_always_inline u32
823 vhost_user_rx_discard_packet_packed (vlib_main_t * vm,
824 vhost_user_intf_t * vui,
825 vhost_user_vring_t * txvq,
828 u32 discarded_packets = 0;
829 u16 mask = txvq->qsz_mask;
830 u16 desc_current, desc_head;
832 desc_head = desc_current = txvq->last_used_idx & mask;
835 * On the RX side, each packet corresponds to one descriptor
836 * (it is the same whether it is a shallow descriptor, chained, or indirect).
837 * Therefore, discarding a packet is like discarding a descriptor.
839 while ((discarded_packets != discard_max) &&
840 vhost_user_packed_desc_available (txvq, desc_current))
842 vhost_user_advance_last_avail_idx (txvq);
844 desc_current = (desc_current + 1) & mask;
847 if (PREDICT_TRUE (discarded_packets))
848 vhost_user_mark_desc_consumed (vui, txvq, desc_head, discarded_packets);
849 return (discarded_packets);
852 static_always_inline u32
853 vhost_user_input_copy_packed (vhost_user_intf_t * vui, vhost_copy_t * cpy,
854 u16 copy_len, u32 * map_hint)
856 void *src0, *src1, *src2, *src3, *src4, *src5, *src6, *src7;
858 u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
860 if (PREDICT_TRUE (copy_len >= 8))
862 src4 = map_guest_mem (vui, cpy[0].src, map_hint);
863 src5 = map_guest_mem (vui, cpy[1].src, map_hint);
864 src6 = map_guest_mem (vui, cpy[2].src, map_hint);
865 src7 = map_guest_mem (vui, cpy[3].src, map_hint);
866 bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
867 if (PREDICT_FALSE (bad))
869 CLIB_PREFETCH (src4, 64, LOAD);
870 CLIB_PREFETCH (src5, 64, LOAD);
871 CLIB_PREFETCH (src6, 64, LOAD);
872 CLIB_PREFETCH (src7, 64, LOAD);
874 while (PREDICT_TRUE (copy_len >= 8))
881 src4 = map_guest_mem (vui, cpy[4].src, map_hint);
882 src5 = map_guest_mem (vui, cpy[5].src, map_hint);
883 src6 = map_guest_mem (vui, cpy[6].src, map_hint);
884 src7 = map_guest_mem (vui, cpy[7].src, map_hint);
885 bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0);
886 if (PREDICT_FALSE (bad))
889 CLIB_PREFETCH (src4, 64, LOAD);
890 CLIB_PREFETCH (src5, 64, LOAD);
891 CLIB_PREFETCH (src6, 64, LOAD);
892 CLIB_PREFETCH (src7, 64, LOAD);
894 clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len);
895 clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len);
896 clib_memcpy_fast ((void *) cpy[2].dst, src2, cpy[2].len);
897 clib_memcpy_fast ((void *) cpy[3].dst, src3, cpy[3].len);
906 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
908 rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
911 clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len);
918 static_always_inline u32
919 vhost_user_do_offload (vhost_user_intf_t * vui,
920 vring_packed_desc_t * desc_table, u16 desc_current,
921 u16 mask, vlib_buffer_t * b_head, u32 * map_hint)
923 u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
924 virtio_net_hdr_mrg_rxbuf_t *hdr;
926 u32 desc_data_offset = vui->virtio_net_hdr_sz;
928 hdr = map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
929 if (PREDICT_FALSE (hdr == 0))
930 rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
931 else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
933 if (desc_data_offset == desc_table[desc_current].len)
935 desc_current = (desc_current + 1) & mask;
937 map_guest_mem (vui, desc_table[desc_current].addr, map_hint);
938 if (PREDICT_FALSE (b_data == 0))
939 rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
941 vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
945 b_data = (u8 *) hdr + desc_data_offset;
946 vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr);
953 static_always_inline u32
954 vhost_user_compute_buffers_required (u32 desc_len, u32 buffer_data_size)
957 u32 buffers_required;
959 if (PREDICT_TRUE (buffer_data_size == 2048))
961 buffers_required = desc_len >> 11;
962 if ((desc_len & 2047) != 0)
964 return (buffers_required);
967 if (desc_len < buffer_data_size)
970 result = div (desc_len, buffer_data_size);
972 buffers_required = result.quot + 1;
974 buffers_required = result.quot;
976 return (buffers_required);
979 static_always_inline u32
980 vhost_user_compute_indirect_desc_len (vhost_user_intf_t * vui,
981 vhost_user_vring_t * txvq,
982 u32 buffer_data_size, u16 desc_current,
985 vring_packed_desc_t *desc_table = txvq->packed_desc;
987 u16 desc_data_offset = vui->virtio_net_hdr_sz;
988 u16 desc_idx = desc_current;
991 n_descs = desc_table[desc_idx].len >> 4;
992 desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, map_hint);
993 if (PREDICT_FALSE (desc_table == 0))
996 for (desc_idx = 0; desc_idx < n_descs; desc_idx++)
997 desc_len += desc_table[desc_idx].len;
999 if (PREDICT_TRUE (desc_len > desc_data_offset))
1000 desc_len -= desc_data_offset;
1002 return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1005 static_always_inline u32
1006 vhost_user_compute_chained_desc_len (vhost_user_intf_t * vui,
1007 vhost_user_vring_t * txvq,
1008 u32 buffer_data_size, u16 * current,
1011 vring_packed_desc_t *desc_table = txvq->packed_desc;
1013 u16 mask = txvq->qsz_mask;
1015 while (desc_table[*current].flags & VRING_DESC_F_NEXT)
1017 desc_len += desc_table[*current].len;
1019 *current = (*current + 1) & mask;
1020 vhost_user_advance_last_avail_idx (txvq);
1022 desc_len += desc_table[*current].len;
1024 *current = (*current + 1) & mask;
1025 vhost_user_advance_last_avail_idx (txvq);
1027 if (PREDICT_TRUE (desc_len > vui->virtio_net_hdr_sz))
1028 desc_len -= vui->virtio_net_hdr_sz;
1030 return vhost_user_compute_buffers_required (desc_len, buffer_data_size);
1033 static_always_inline void
1034 vhost_user_assemble_packet (vring_packed_desc_t * desc_table,
1035 u16 * desc_idx, vlib_buffer_t * b_head,
1036 vlib_buffer_t ** b_current, u32 ** next,
1037 vlib_buffer_t *** b, u32 * bi_current,
1038 vhost_cpu_t * cpu, u16 * copy_len,
1039 u32 * buffers_used, u32 buffers_required,
1040 u32 * desc_data_offset, u32 buffer_data_size,
1045 while (*desc_data_offset < desc_table[*desc_idx].len)
1047 /* Get more output if necessary. Or end of packet. */
1048 if (PREDICT_FALSE ((*b_current)->current_length == buffer_data_size))
1050 /* Get next output */
1051 u32 bi_next = **next;
1053 (*b_current)->next_buffer = bi_next;
1054 (*b_current)->flags |= VLIB_BUFFER_NEXT_PRESENT;
1055 *bi_current = bi_next;
1059 ASSERT (*buffers_used <= buffers_required);
1062 /* Prepare a copy order executed later for the data */
1063 ASSERT (*copy_len < VHOST_USER_COPY_ARRAY_N);
1064 vhost_copy_t *cpy = &cpu->copy[*copy_len];
1066 desc_data_l = desc_table[*desc_idx].len - *desc_data_offset;
1067 cpy->len = buffer_data_size - (*b_current)->current_length;
1068 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1069 cpy->dst = (uword) (vlib_buffer_get_current (*b_current) +
1070 (*b_current)->current_length);
1071 cpy->src = desc_table[*desc_idx].addr + *desc_data_offset;
1073 *desc_data_offset += cpy->len;
1075 (*b_current)->current_length += cpy->len;
1076 b_head->total_length_not_including_first_buffer += cpy->len;
1078 *desc_idx = (*desc_idx + 1) & mask;;
1079 *desc_data_offset = 0;
1082 static_always_inline u32
1083 vhost_user_if_input_packed (vlib_main_t *vm, vhost_user_main_t *vum,
1084 vhost_user_intf_t *vui, u16 qid,
1085 vlib_node_runtime_t *node, u8 enable_csum)
1087 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1088 vnet_feature_main_t *fm = &feature_main;
1089 u8 feature_arc_idx = fm->device_input_feature_arc_index;
1090 u16 n_rx_packets = 0;
1093 u32 buffers_required = 0;
1094 u32 n_left_to_next, *to_next;
1095 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1096 u32 n_trace = vlib_get_trace_count (vm, node);
1097 u32 buffer_data_size = vlib_buffer_get_default_data_size (vm);
1099 vhost_cpu_t *cpu = &vum->cpus[vm->thread_index];
1101 u32 current_config_index = ~0;
1102 u16 mask = txvq->qsz_mask;
1103 u16 desc_current, desc_head, last_used_idx;
1104 vring_packed_desc_t *desc_table = 0;
1105 u32 n_descs_processed = 0;
1109 u32 buffers_used = 0;
1110 u16 current, n_descs_to_process;
1112 /* The descriptor table is not ready yet */
1113 if (PREDICT_FALSE (txvq->packed_desc == 0))
1116 /* do we have pending interrupts ? */
1117 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1118 vhost_user_input_do_interrupt (vm, vui, txvq, rxvq);
1121 * For adaptive mode, it is optimized to reduce interrupts.
1122 * If the scheduler switches the input node to polling due
1123 * to burst of traffic, we tell the driver no interrupt.
1124 * When the traffic subsides, the scheduler switches the node back to
1125 * interrupt mode. We must tell the driver we want interrupt.
1127 if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))
1130 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
1132 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1133 /* Tell driver we want notification */
1134 txvq->used_event->flags = 0;
1136 /* Tell driver we don't want notification */
1137 txvq->used_event->flags = VRING_EVENT_F_DISABLE;
1140 last_used_idx = txvq->last_used_idx & mask;
1141 desc_head = desc_current = last_used_idx;
1143 if (vhost_user_packed_desc_available (txvq, desc_current) == 0)
1146 if (PREDICT_FALSE (!vui->admin_up || !vui->is_ready || !(txvq->enabled)))
1149 * Discard input packet if interface is admin down or vring is not
1151 * "For example, for a networking device, in the disabled state
1152 * client must not supply any new RX packets, but must process
1153 * and discard any TX packets."
1155 rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq,
1156 VHOST_USER_DOWN_DISCARD_COUNT);
1157 vlib_error_count (vm, vhost_user_input_node.index,
1158 VHOST_USER_INPUT_FUNC_ERROR_NOT_READY, rv);
1162 vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index,
1163 &next_index, &to_next, &n_left_to_next);
1166 * Compute n_left and total buffers needed
1168 desc_table = txvq->packed_desc;
1169 current = desc_current;
1170 while (vhost_user_packed_desc_available (txvq, current) &&
1171 (n_left < VLIB_FRAME_SIZE))
1173 if (desc_table[current].flags & VRING_DESC_F_INDIRECT)
1176 vhost_user_compute_indirect_desc_len (vui, txvq, buffer_data_size,
1177 current, &map_hint);
1179 current = (current + 1) & mask;
1180 vhost_user_advance_last_avail_idx (txvq);
1185 vhost_user_compute_chained_desc_len (vui, txvq, buffer_data_size,
1190 /* Something is broken if we need more than 10000 buffers */
1191 if (PREDICT_FALSE ((buffers_required == 0) || (buffers_required > 10000)))
1193 rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1194 vlib_error_count (vm, vhost_user_input_node.index,
1195 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1199 vec_validate (cpu->to_next_list, buffers_required);
1200 rv = vlib_buffer_alloc (vm, cpu->to_next_list, buffers_required);
1201 if (PREDICT_FALSE (rv != buffers_required))
1203 vlib_buffer_free (vm, cpu->to_next_list, rv);
1204 rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left);
1205 vlib_error_count (vm, vhost_user_input_node.index,
1206 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv);
1210 next = cpu->to_next_list;
1211 vec_validate (cpu->rx_buffers_pdesc, buffers_required);
1212 vlib_get_buffers (vm, next, cpu->rx_buffers_pdesc, buffers_required);
1213 b = cpu->rx_buffers_pdesc;
1214 n_descs_processed = n_left;
1218 vlib_buffer_t *b_head, *b_current;
1220 u32 desc_data_offset;
1221 u16 desc_idx = desc_current;
1224 desc_table = txvq->packed_desc;
1225 to_next[0] = bi_current = next[0];
1226 b_head = b_current = b[0];
1229 ASSERT (buffers_used <= buffers_required);
1234 /* The buffer should already be initialized */
1235 b_head->total_length_not_including_first_buffer = 0;
1236 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1237 desc_data_offset = vui->virtio_net_hdr_sz;
1238 n_descs_to_process = 1;
1240 if (desc_table[desc_idx].flags & VRING_DESC_F_INDIRECT)
1242 n_descs = desc_table[desc_idx].len >> 4;
1243 desc_table = map_guest_mem (vui, desc_table[desc_idx].addr,
1246 if (PREDICT_FALSE (desc_table == 0) ||
1249 (vhost_user_do_offload
1250 (vui, desc_table, desc_idx, mask, b_head,
1251 &map_hint) != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))))
1253 vlib_error_count (vm, node->node_index,
1254 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1264 vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1265 &b_current, &next, &b, &bi_current,
1266 cpu, ©_len, &buffers_used,
1267 buffers_required, &desc_data_offset,
1268 buffer_data_size, mask);
1276 rv = vhost_user_do_offload (vui, desc_table, desc_idx, mask,
1278 if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1280 vlib_error_count (vm, node->node_index, rv, 1);
1290 * For chained descriptor, we process all chains in a single while
1291 * loop. So count how many descriptors in the chain.
1293 n_descs_to_process = 1;
1294 while (desc_table[desc_idx].flags & VRING_DESC_F_NEXT)
1296 vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1297 &b_current, &next, &b, &bi_current,
1298 cpu, ©_len, &buffers_used,
1299 buffers_required, &desc_data_offset,
1300 buffer_data_size, mask);
1301 n_descs_to_process++;
1303 vhost_user_assemble_packet (desc_table, &desc_idx, b_head,
1304 &b_current, &next, &b, &bi_current,
1305 cpu, ©_len, &buffers_used,
1306 buffers_required, &desc_data_offset,
1307 buffer_data_size, mask);
1310 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1313 b_head->total_length_not_including_first_buffer -=
1314 b_head->current_length;
1316 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1318 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1319 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = ~0;
1322 if (current_config_index != ~0)
1324 b_head->current_config_index = current_config_index;
1325 vnet_buffer (b_head)->feature_arc_index = feature_arc_idx;
1329 ASSERT (n_left >= n_descs_to_process);
1330 n_left -= n_descs_to_process;
1332 /* advance to next descrptor */
1333 desc_current = (desc_current + n_descs_to_process) & mask;
1336 * Although separating memory copies from virtio ring parsing
1337 * is beneficial, we can offer to perform the copies from time
1338 * to time in order to free some space in the ring.
1340 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1342 rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len,
1344 if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1345 vlib_error_count (vm, node->node_index, rv, 1);
1349 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1351 /* Do the memory copies */
1352 rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, &map_hint);
1353 if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1354 vlib_error_count (vm, node->node_index, rv, 1);
1356 /* Must do the tracing before giving buffers back to driver */
1357 if (PREDICT_FALSE (n_trace))
1359 u32 left = n_rx_packets;
1361 b = cpu->rx_buffers_pdesc;
1362 while (n_trace && left)
1366 (vm, node, next_index, b[0], /* follow_chain */ 0)))
1369 t0 = vlib_add_trace (vm, node, b[0], sizeof (t0[0]));
1370 vhost_user_rx_trace_packed (t0, vui, qid, txvq, last_used_idx);
1371 last_used_idx = (last_used_idx + 1) & mask;
1373 vlib_set_trace_count (vm, node, n_trace);
1381 * Give buffers back to driver.
1383 vhost_user_mark_desc_consumed (vui, txvq, desc_head, n_descs_processed);
1385 /* interrupt (call) handling */
1386 if ((txvq->callfd_idx != ~0) &&
1387 (txvq->avail_event->flags != VRING_EVENT_F_DISABLE))
1389 txvq->n_since_last_int += n_rx_packets;
1390 if (txvq->n_since_last_int > vum->coalesce_frames)
1391 vhost_user_send_call (vm, vui, txvq);
1394 /* increase rx counters */
1395 vlib_increment_combined_counter
1396 (vnet_main.interface_main.combined_sw_if_counters
1397 + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index,
1398 n_rx_packets, n_rx_bytes);
1400 vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets);
1402 if (PREDICT_FALSE (buffers_used < buffers_required))
1403 vlib_buffer_free (vm, next, buffers_required - buffers_used);
1406 return n_rx_packets;
1409 VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm,
1410 vlib_node_runtime_t * node,
1411 vlib_frame_t * frame)
1413 vhost_user_main_t *vum = &vhost_user_main;
1414 uword n_rx_packets = 0;
1415 vhost_user_intf_t *vui;
1416 vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
1417 vnet_hw_if_rxq_poll_vector_t *pve;
1419 vec_foreach (pve, pv)
1421 vui = pool_elt_at_index (vum->vhost_user_interfaces, pve->dev_instance);
1422 if (vhost_user_is_packed_ring_supported (vui))
1424 if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1425 n_rx_packets += vhost_user_if_input_packed (
1426 vm, vum, vui, pve->queue_id, node, 1);
1428 n_rx_packets += vhost_user_if_input_packed (
1429 vm, vum, vui, pve->queue_id, node, 0);
1433 if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))
1435 vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 1);
1438 vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 0);
1442 return n_rx_packets;
1446 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1447 .type = VLIB_NODE_TYPE_INPUT,
1448 .name = "vhost-user-input",
1449 .sibling_of = "device-input",
1450 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
1452 /* Will be enabled if/when hardware is detected. */
1453 .state = VLIB_NODE_STATE_DISABLED,
1455 .format_buffer = format_ethernet_header_with_length,
1456 .format_trace = format_vhost_trace,
1458 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1459 .error_strings = vhost_user_input_func_error_strings,
1464 * fd.io coding-style-patch-verification: ON
1467 * eval: (c-set-style "gnu")