Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vhost-user-input |
| 4 | * |
| 5 | * Copyright (c) 2014-2018 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <fcntl.h> /* for open */ |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/uio.h> /* for iovec */ |
| 27 | #include <netinet/in.h> |
| 28 | #include <sys/vfs.h> |
| 29 | |
| 30 | #include <linux/if_arp.h> |
| 31 | #include <linux/if_tun.h> |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vlib/unix/unix.h> |
| 35 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 36 | #include <vnet/ethernet/ethernet.h> |
| 37 | #include <vnet/devices/devices.h> |
| 38 | #include <vnet/feature/feature.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 39 | #include <vnet/udp/udp_packet.h> |
Neale Ranns | f7040f0 | 2022-02-15 09:02:27 +0000 | [diff] [blame^] | 40 | #include <vnet/tcp/tcp_packet.h> |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 41 | #include <vnet/interface/rx_queue_funcs.h> |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 42 | |
| 43 | #include <vnet/devices/virtio/vhost_user.h> |
| 44 | #include <vnet/devices/virtio/vhost_user_inline.h> |
| 45 | |
Neale Ranns | 68d48d9 | 2021-06-03 14:59:47 +0000 | [diff] [blame] | 46 | #include <vnet/ip/ip4_packet.h> |
| 47 | #include <vnet/ip/ip6_packet.h> |
| 48 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 49 | /* |
| 50 | * When an RX queue is down but active, received packets |
| 51 | * must be discarded. This value controls up to how many |
| 52 | * packets will be discarded during each round. |
| 53 | */ |
| 54 | #define VHOST_USER_DOWN_DISCARD_COUNT 256 |
| 55 | |
| 56 | /* |
| 57 | * When the number of available buffers gets under this threshold, |
| 58 | * RX node will start discarding packets. |
| 59 | */ |
| 60 | #define VHOST_USER_RX_BUFFER_STARVATION 32 |
| 61 | |
| 62 | /* |
| 63 | * On the receive side, the host should free descriptors as soon |
| 64 | * as possible in order to avoid TX drop in the VM. |
| 65 | * This value controls the number of copy operations that are stacked |
| 66 | * before copy is done for all and descriptors are given back to |
| 67 | * the guest. |
| 68 | * The value 64 was obtained by testing (48 and 128 were not as good). |
| 69 | */ |
| 70 | #define VHOST_USER_RX_COPY_THRESHOLD 64 |
| 71 | |
Benoît Ganne | 47727c0 | 2019-02-12 13:35:08 +0100 | [diff] [blame] | 72 | extern vlib_node_registration_t vhost_user_input_node; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 73 | |
| 74 | #define foreach_vhost_user_input_func_error \ |
| 75 | _(NO_ERROR, "no error") \ |
| 76 | _(NO_BUFFER, "no available buffer") \ |
| 77 | _(MMAP_FAIL, "mmap failure") \ |
| 78 | _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \ |
| 79 | _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \ |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 80 | _(NOT_READY, "vhost interface not ready or down") \ |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 81 | _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)") |
| 82 | |
| 83 | typedef enum |
| 84 | { |
| 85 | #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f, |
| 86 | foreach_vhost_user_input_func_error |
| 87 | #undef _ |
| 88 | VHOST_USER_INPUT_FUNC_N_ERROR, |
| 89 | } vhost_user_input_func_error_t; |
| 90 | |
| 91 | static __clib_unused char *vhost_user_input_func_error_strings[] = { |
| 92 | #define _(n,s) s, |
| 93 | foreach_vhost_user_input_func_error |
| 94 | #undef _ |
| 95 | }; |
| 96 | |
| 97 | static_always_inline void |
| 98 | vhost_user_rx_trace (vhost_trace_t * t, |
| 99 | vhost_user_intf_t * vui, u16 qid, |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 100 | vlib_buffer_t * b, vhost_user_vring_t * txvq, |
| 101 | u16 last_avail_idx) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 102 | { |
| 103 | vhost_user_main_t *vum = &vhost_user_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 104 | u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask]; |
| 105 | vring_desc_t *hdr_desc = 0; |
| 106 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 107 | u32 hint = 0; |
| 108 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 109 | clib_memset (t, 0, sizeof (*t)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 110 | t->device_index = vui - vum->vhost_user_interfaces; |
| 111 | t->qid = qid; |
| 112 | |
| 113 | hdr_desc = &txvq->desc[desc_current]; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 114 | if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 115 | { |
| 116 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; |
| 117 | /* Header is the first here */ |
| 118 | hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint); |
| 119 | } |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 120 | if (txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 121 | { |
| 122 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; |
| 123 | } |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 124 | if (!(txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) && |
| 125 | !(txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 126 | { |
| 127 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; |
| 128 | } |
| 129 | |
| 130 | t->first_desc_len = hdr_desc ? hdr_desc->len : 0; |
| 131 | |
| 132 | if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) |
| 133 | { |
| 134 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | u32 len = vui->virtio_net_hdr_sz; |
| 139 | memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static_always_inline u32 |
| 144 | vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, |
| 145 | u16 copy_len, u32 * map_hint) |
| 146 | { |
| 147 | void *src0, *src1, *src2, *src3; |
| 148 | if (PREDICT_TRUE (copy_len >= 4)) |
| 149 | { |
| 150 | if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint)))) |
| 151 | return 1; |
| 152 | if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint)))) |
| 153 | return 1; |
| 154 | |
| 155 | while (PREDICT_TRUE (copy_len >= 4)) |
| 156 | { |
| 157 | src0 = src2; |
| 158 | src1 = src3; |
| 159 | |
| 160 | if (PREDICT_FALSE |
| 161 | (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint)))) |
| 162 | return 1; |
| 163 | if (PREDICT_FALSE |
| 164 | (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint)))) |
| 165 | return 1; |
| 166 | |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 167 | clib_prefetch_load (src2); |
| 168 | clib_prefetch_load (src3); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 169 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 170 | clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len); |
| 171 | clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 172 | copy_len -= 2; |
| 173 | cpy += 2; |
| 174 | } |
| 175 | } |
| 176 | while (copy_len) |
| 177 | { |
| 178 | if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) |
| 179 | return 1; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 180 | clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 181 | copy_len -= 1; |
| 182 | cpy += 1; |
| 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Try to discard packets from the tx ring (VPP RX path). |
| 189 | * Returns the number of discarded packets. |
| 190 | */ |
| 191 | static_always_inline u32 |
| 192 | vhost_user_rx_discard_packet (vlib_main_t * vm, |
| 193 | vhost_user_intf_t * vui, |
| 194 | vhost_user_vring_t * txvq, u32 discard_max) |
| 195 | { |
| 196 | /* |
| 197 | * On the RX side, each packet corresponds to one descriptor |
| 198 | * (it is the same whether it is a shallow descriptor, chained, or indirect). |
| 199 | * Therefore, discarding a packet is like discarding a descriptor. |
| 200 | */ |
| 201 | u32 discarded_packets = 0; |
| 202 | u32 avail_idx = txvq->avail->idx; |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 203 | u16 mask = txvq->qsz_mask; |
| 204 | u16 last_avail_idx = txvq->last_avail_idx; |
| 205 | u16 last_used_idx = txvq->last_used_idx; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 206 | while (discarded_packets != discard_max) |
| 207 | { |
Steven Luong | 7e5735d | 2019-03-12 21:35:42 -0700 | [diff] [blame] | 208 | if (avail_idx == last_avail_idx) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 209 | goto out; |
| 210 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 211 | u16 desc_chain_head = txvq->avail->ring[last_avail_idx & mask]; |
| 212 | last_avail_idx++; |
| 213 | txvq->used->ring[last_used_idx & mask].id = desc_chain_head; |
| 214 | txvq->used->ring[last_used_idx & mask].len = 0; |
| 215 | vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); |
| 216 | last_used_idx++; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 217 | discarded_packets++; |
| 218 | } |
| 219 | |
| 220 | out: |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 221 | txvq->last_avail_idx = last_avail_idx; |
| 222 | txvq->last_used_idx = last_used_idx; |
Damjan Marion | 96e8cd0 | 2018-11-23 14:56:55 +0100 | [diff] [blame] | 223 | CLIB_MEMORY_STORE_BARRIER (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 224 | txvq->used->idx = txvq->last_used_idx; |
| 225 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 226 | return discarded_packets; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * In case of overflow, we need to rewind the array of allocated buffers. |
| 231 | */ |
Damjan Marion | 46bf866 | 2018-11-22 22:25:38 +0100 | [diff] [blame] | 232 | static_always_inline void |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 233 | vhost_user_input_rewind_buffers (vlib_main_t * vm, |
| 234 | vhost_cpu_t * cpu, vlib_buffer_t * b_head) |
| 235 | { |
| 236 | u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 237 | vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current); |
| 238 | b_current->current_length = 0; |
| 239 | b_current->flags = 0; |
| 240 | while (b_current != b_head) |
| 241 | { |
| 242 | cpu->rx_buffers_len++; |
| 243 | bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 244 | b_current = vlib_get_buffer (vm, bi_current); |
| 245 | b_current->current_length = 0; |
| 246 | b_current->flags = 0; |
| 247 | } |
| 248 | cpu->rx_buffers_len++; |
| 249 | } |
| 250 | |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 251 | static_always_inline void |
| 252 | vhost_user_handle_rx_offload (vlib_buffer_t * b0, u8 * b0_data, |
| 253 | virtio_net_hdr_t * hdr) |
| 254 | { |
| 255 | u8 l4_hdr_sz = 0; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 256 | u8 l4_proto = 0; |
| 257 | ethernet_header_t *eh = (ethernet_header_t *) b0_data; |
| 258 | u16 ethertype = clib_net_to_host_u16 (eh->type); |
| 259 | u16 l2hdr_sz = sizeof (ethernet_header_t); |
Mohsin Kazmi | 36f7a6a | 2021-05-05 14:26:38 +0200 | [diff] [blame] | 260 | vnet_buffer_oflags_t oflags = 0; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 261 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 262 | if (ethernet_frame_is_tagged (ethertype)) |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 263 | { |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 264 | ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 265 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 266 | ethertype = clib_net_to_host_u16 (vlan->type); |
| 267 | l2hdr_sz += sizeof (*vlan); |
| 268 | if (ethertype == ETHERNET_TYPE_VLAN) |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 269 | { |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 270 | vlan++; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 271 | ethertype = clib_net_to_host_u16 (vlan->type); |
| 272 | l2hdr_sz += sizeof (*vlan); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 273 | } |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 274 | } |
| 275 | vnet_buffer (b0)->l2_hdr_offset = 0; |
| 276 | vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz; |
| 277 | vnet_buffer (b0)->l4_hdr_offset = hdr->csum_start; |
| 278 | b0->flags |= (VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 279 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | |
| 280 | VNET_BUFFER_F_L4_HDR_OFFSET_VALID); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 281 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 282 | if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4)) |
| 283 | { |
| 284 | ip4_header_t *ip4 = (ip4_header_t *) (b0_data + l2hdr_sz); |
| 285 | l4_proto = ip4->protocol; |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 286 | b0->flags |= VNET_BUFFER_F_IS_IP4; |
| 287 | oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 288 | } |
| 289 | else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6)) |
| 290 | { |
| 291 | ip6_header_t *ip6 = (ip6_header_t *) (b0_data + l2hdr_sz); |
| 292 | l4_proto = ip6->protocol; |
| 293 | b0->flags |= VNET_BUFFER_F_IS_IP6; |
| 294 | } |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 295 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 296 | if (l4_proto == IP_PROTOCOL_TCP) |
| 297 | { |
| 298 | tcp_header_t *tcp = (tcp_header_t *) |
| 299 | (b0_data + vnet_buffer (b0)->l4_hdr_offset); |
| 300 | l4_hdr_sz = tcp_header_bytes (tcp); |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 301 | oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 302 | } |
| 303 | else if (l4_proto == IP_PROTOCOL_UDP) |
| 304 | { |
Steven Luong | ac0f536 | 2020-10-12 10:43:28 -0700 | [diff] [blame] | 305 | l4_hdr_sz = sizeof (udp_header_t); |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 306 | oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP) |
| 310 | { |
| 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; |
| 314 | } |
| 315 | else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4) |
| 316 | { |
| 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_IP4); |
| 320 | } |
| 321 | else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6) |
| 322 | { |
| 323 | vnet_buffer2 (b0)->gso_size = hdr->gso_size; |
| 324 | vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz; |
| 325 | b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6); |
| 326 | } |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 327 | |
| 328 | if (oflags) |
| 329 | vnet_buffer_offload_flags_set (b0, oflags); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 332 | static_always_inline void |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 333 | vhost_user_input_do_interrupt (vlib_main_t * vm, vhost_user_intf_t * vui, |
| 334 | vhost_user_vring_t * txvq, |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 335 | vhost_user_vring_t * rxvq) |
| 336 | { |
| 337 | f64 now = vlib_time_now (vm); |
| 338 | |
| 339 | if ((txvq->n_since_last_int) && (txvq->int_deadline < now)) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 340 | vhost_user_send_call (vm, vui, txvq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 341 | |
| 342 | if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now)) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 343 | vhost_user_send_call (vm, vui, rxvq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static_always_inline void |
| 347 | vhost_user_input_setup_frame (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 348 | vhost_user_intf_t * vui, |
| 349 | u32 * current_config_index, u32 * next_index, |
| 350 | u32 ** to_next, u32 * n_left_to_next) |
| 351 | { |
| 352 | vnet_feature_main_t *fm = &feature_main; |
| 353 | u8 feature_arc_idx = fm->device_input_feature_arc_index; |
| 354 | |
| 355 | if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index))) |
| 356 | { |
| 357 | vnet_feature_config_main_t *cm; |
| 358 | cm = &fm->feature_config_mains[feature_arc_idx]; |
| 359 | *current_config_index = vec_elt (cm->config_index_by_sw_if_index, |
| 360 | vui->sw_if_index); |
| 361 | vnet_get_config_data (&cm->config_main, current_config_index, |
| 362 | next_index, 0); |
| 363 | } |
| 364 | |
| 365 | vlib_get_new_next_frame (vm, node, *next_index, *to_next, *n_left_to_next); |
| 366 | |
| 367 | if (*next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT) |
| 368 | { |
| 369 | /* give some hints to ethernet-input */ |
| 370 | vlib_next_frame_t *nf; |
| 371 | vlib_frame_t *f; |
| 372 | ethernet_input_frame_t *ef; |
| 373 | nf = vlib_node_runtime_get_next_frame (vm, node, *next_index); |
| 374 | f = vlib_get_frame (vm, nf->frame); |
| 375 | f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX; |
| 376 | |
| 377 | ef = vlib_frame_scalar_args (f); |
| 378 | ef->sw_if_index = vui->sw_if_index; |
| 379 | ef->hw_if_index = vui->hw_if_index; |
| 380 | vlib_frame_no_append (f); |
| 381 | } |
| 382 | } |
| 383 | |
Damjan Marion | 46bf866 | 2018-11-22 22:25:38 +0100 | [diff] [blame] | 384 | static_always_inline u32 |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 385 | vhost_user_if_input (vlib_main_t *vm, vhost_user_main_t *vum, |
| 386 | vhost_user_intf_t *vui, u16 qid, |
| 387 | vlib_node_runtime_t *node, u8 enable_csum) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 388 | { |
| 389 | vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 390 | vnet_feature_main_t *fm = &feature_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 391 | u16 n_rx_packets = 0; |
| 392 | u32 n_rx_bytes = 0; |
| 393 | u16 n_left; |
| 394 | u32 n_left_to_next, *to_next; |
| 395 | u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 396 | u32 n_trace = vlib_get_trace_count (vm, node); |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 397 | u32 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 398 | u32 map_hint = 0; |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 399 | vhost_cpu_t *cpu = &vum->cpus[vm->thread_index]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 400 | u16 copy_len = 0; |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 401 | u8 feature_arc_idx = fm->device_input_feature_arc_index; |
| 402 | u32 current_config_index = ~(u32) 0; |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 403 | u16 mask = txvq->qsz_mask; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 404 | |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 405 | /* The descriptor table is not ready yet */ |
| 406 | if (PREDICT_FALSE (txvq->avail == 0)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 407 | goto done; |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 408 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 409 | { |
| 410 | /* do we have pending interrupts ? */ |
| 411 | vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 412 | vhost_user_input_do_interrupt (vm, vui, txvq, rxvq); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * For adaptive mode, it is optimized to reduce interrupts. |
| 417 | * If the scheduler switches the input node to polling due |
| 418 | * to burst of traffic, we tell the driver no interrupt. |
| 419 | * When the traffic subsides, the scheduler switches the node back to |
| 420 | * interrupt mode. We must tell the driver we want interrupt. |
| 421 | */ |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 422 | if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 423 | { |
| 424 | if ((node->flags & |
| 425 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || |
| 426 | !(node->flags & |
| 427 | VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) |
| 428 | /* Tell driver we want notification */ |
| 429 | txvq->used->flags = 0; |
| 430 | else |
| 431 | /* Tell driver we don't want notification */ |
| 432 | txvq->used->flags = VRING_USED_F_NO_NOTIFY; |
| 433 | } |
| 434 | |
| 435 | if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 436 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 437 | |
| 438 | n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx); |
| 439 | |
| 440 | /* nothing to do */ |
| 441 | if (PREDICT_FALSE (n_left == 0)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 442 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 443 | |
| 444 | if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled))) |
| 445 | { |
| 446 | /* |
| 447 | * Discard input packet if interface is admin down or vring is not |
| 448 | * enabled. |
| 449 | * "For example, for a networking device, in the disabled state |
| 450 | * client must not supply any new RX packets, but must process |
| 451 | * and discard any TX packets." |
| 452 | */ |
| 453 | vhost_user_rx_discard_packet (vm, vui, txvq, |
| 454 | VHOST_USER_DOWN_DISCARD_COUNT); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 455 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 458 | if (PREDICT_FALSE (n_left == (mask + 1))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 459 | { |
| 460 | /* |
| 461 | * Informational error logging when VPP is not |
| 462 | * receiving packets fast enough. |
| 463 | */ |
| 464 | vlib_error_count (vm, node->node_index, |
| 465 | VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1); |
| 466 | } |
| 467 | |
| 468 | if (n_left > VLIB_FRAME_SIZE) |
| 469 | n_left = VLIB_FRAME_SIZE; |
| 470 | |
| 471 | /* |
| 472 | * For small packets (<2kB), we will not need more than one vlib buffer |
Paul Vinciguerra | 97c998c | 2019-10-29 16:11:09 -0400 | [diff] [blame] | 473 | * per packet. In case packets are bigger, we will just yield at some point |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 474 | * in the loop and come back later. This is not an issue as for big packet, |
| 475 | * processing cost really comes from the memory copy. |
| 476 | * The assumption is that big packets will fit in 40 buffers. |
| 477 | */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 478 | if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 || |
| 479 | cpu->rx_buffers_len < 40)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 480 | { |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 481 | u32 curr_len = cpu->rx_buffers_len; |
| 482 | cpu->rx_buffers_len += |
Damjan Marion | 671e60e | 2018-12-30 18:09:59 +0100 | [diff] [blame] | 483 | vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len, |
| 484 | VHOST_USER_RX_BUFFERS_N - curr_len); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 485 | |
| 486 | if (PREDICT_FALSE |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 487 | (cpu->rx_buffers_len < VHOST_USER_RX_BUFFER_STARVATION)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 488 | { |
| 489 | /* In case of buffer starvation, discard some packets from the queue |
| 490 | * and log the event. |
| 491 | * We keep doing best effort for the remaining packets. */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 492 | u32 flush = (n_left + 1 > cpu->rx_buffers_len) ? |
| 493 | n_left + 1 - cpu->rx_buffers_len : 1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 494 | flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush); |
| 495 | |
| 496 | n_left -= flush; |
| 497 | vlib_increment_simple_counter (vnet_main. |
| 498 | interface_main.sw_if_counters + |
| 499 | VNET_INTERFACE_COUNTER_DROP, |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 500 | vm->thread_index, vui->sw_if_index, |
| 501 | flush); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 502 | |
| 503 | vlib_error_count (vm, vhost_user_input_node.index, |
| 504 | VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush); |
| 505 | } |
| 506 | } |
| 507 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 508 | vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index, |
| 509 | &next_index, &to_next, &n_left_to_next); |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 510 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 511 | u16 last_avail_idx = txvq->last_avail_idx; |
| 512 | u16 last_used_idx = txvq->last_used_idx; |
| 513 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 514 | while (n_left > 0) |
| 515 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 516 | vlib_buffer_t *b_head, *b_current; |
| 517 | u32 bi_current; |
| 518 | u16 desc_current; |
| 519 | u32 desc_data_offset; |
| 520 | vring_desc_t *desc_table = txvq->desc; |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 521 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 522 | if (PREDICT_FALSE (cpu->rx_buffers_len <= 1)) |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 523 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 524 | /* Not enough rx_buffers |
| 525 | * Note: We yeld on 1 so we don't need to do an additional |
| 526 | * check for the next buffer prefetch. |
| 527 | */ |
| 528 | n_left = 0; |
| 529 | break; |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 530 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 531 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 532 | desc_current = txvq->avail->ring[last_avail_idx & mask]; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 533 | cpu->rx_buffers_len--; |
| 534 | bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 535 | b_head = b_current = vlib_get_buffer (vm, bi_current); |
| 536 | to_next[0] = bi_current; //We do that now so we can forget about bi_current |
| 537 | to_next++; |
| 538 | n_left_to_next--; |
| 539 | |
| 540 | vlib_prefetch_buffer_with_index |
| 541 | (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD); |
| 542 | |
| 543 | /* Just preset the used descriptor id and length for later */ |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 544 | txvq->used->ring[last_used_idx & mask].id = desc_current; |
| 545 | txvq->used->ring[last_used_idx & mask].len = 0; |
| 546 | vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 547 | |
| 548 | /* The buffer should already be initialized */ |
| 549 | b_head->total_length_not_including_first_buffer = 0; |
| 550 | b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 551 | |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 552 | if (PREDICT_FALSE |
| 553 | (n_trace > 0 && vlib_trace_buffer (vm, node, next_index, b_head, |
| 554 | /* follow_chain */ 0))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 555 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 556 | vhost_trace_t *t0 = |
| 557 | vlib_add_trace (vm, node, b_head, sizeof (t0[0])); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 558 | vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx); |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 559 | n_trace--; |
| 560 | vlib_set_trace_count (vm, node, n_trace); |
| 561 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 562 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 563 | /* This depends on the setup but is very consistent |
| 564 | * So I think the CPU branch predictor will make a pretty good job |
| 565 | * at optimizing the decision. */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 566 | if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT) |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 567 | { |
| 568 | desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr, |
| 569 | &map_hint); |
| 570 | desc_current = 0; |
| 571 | if (PREDICT_FALSE (desc_table == 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 572 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 573 | vlib_error_count (vm, node->node_index, |
| 574 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 575 | goto out; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 576 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 577 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 578 | |
Benoît Ganne | 5ecc1e4 | 2020-01-24 18:06:01 +0100 | [diff] [blame] | 579 | desc_data_offset = vui->virtio_net_hdr_sz; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 580 | |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 581 | if (enable_csum) |
| 582 | { |
| 583 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 584 | u8 *b_data; |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 585 | u16 current; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 586 | |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 587 | hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint); |
Steven Luong | 5dedae7 | 2019-07-31 16:01:14 -0700 | [diff] [blame] | 588 | if (PREDICT_FALSE (hdr == 0)) |
| 589 | { |
| 590 | vlib_error_count (vm, node->node_index, |
| 591 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 592 | goto out; |
| 593 | } |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 594 | if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) |
Steven Luong | 5dedae7 | 2019-07-31 16:01:14 -0700 | [diff] [blame] | 595 | { |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 596 | if ((desc_data_offset == desc_table[desc_current].len) && |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 597 | (desc_table[desc_current].flags & VRING_DESC_F_NEXT)) |
Steven Luong | 5dedae7 | 2019-07-31 16:01:14 -0700 | [diff] [blame] | 598 | { |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 599 | current = desc_table[desc_current].next; |
| 600 | b_data = map_guest_mem (vui, desc_table[current].addr, |
| 601 | &map_hint); |
| 602 | if (PREDICT_FALSE (b_data == 0)) |
| 603 | { |
| 604 | vlib_error_count (vm, node->node_index, |
| 605 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, |
| 606 | 1); |
| 607 | goto out; |
| 608 | } |
Steven Luong | 5dedae7 | 2019-07-31 16:01:14 -0700 | [diff] [blame] | 609 | } |
Steven Luong | b232d19 | 2020-03-17 09:01:30 -0700 | [diff] [blame] | 610 | else |
| 611 | b_data = (u8 *) hdr + desc_data_offset; |
| 612 | |
| 613 | vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); |
Steven Luong | 5dedae7 | 2019-07-31 16:01:14 -0700 | [diff] [blame] | 614 | } |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 617 | while (1) |
| 618 | { |
| 619 | /* Get more input if necessary. Or end of packet. */ |
| 620 | if (desc_data_offset == desc_table[desc_current].len) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 621 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 622 | if (PREDICT_FALSE (desc_table[desc_current].flags & |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 623 | VRING_DESC_F_NEXT)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 624 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 625 | desc_current = desc_table[desc_current].next; |
| 626 | desc_data_offset = 0; |
| 627 | } |
| 628 | else |
| 629 | { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 630 | goto out; |
| 631 | } |
| 632 | } |
| 633 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 634 | /* Get more output if necessary. Or end of packet. */ |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 635 | if (PREDICT_FALSE (b_current->current_length == buffer_data_size)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 636 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 637 | if (PREDICT_FALSE (cpu->rx_buffers_len == 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 638 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 639 | /* Cancel speculation */ |
| 640 | to_next--; |
| 641 | n_left_to_next++; |
| 642 | |
| 643 | /* |
| 644 | * Checking if there are some left buffers. |
| 645 | * If not, just rewind the used buffers and stop. |
| 646 | * Note: Scheduled copies are not cancelled. This is |
| 647 | * not an issue as they would still be valid. Useless, |
| 648 | * but valid. |
| 649 | */ |
| 650 | vhost_user_input_rewind_buffers (vm, cpu, b_head); |
| 651 | n_left = 0; |
| 652 | goto stop; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 655 | /* Get next output */ |
| 656 | cpu->rx_buffers_len--; |
| 657 | u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 658 | b_current->next_buffer = bi_next; |
| 659 | b_current->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 660 | bi_current = bi_next; |
| 661 | b_current = vlib_get_buffer (vm, bi_current); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 664 | /* Prepare a copy order executed later for the data */ |
Steven Luong | 7331005 | 2019-10-23 13:28:37 -0700 | [diff] [blame] | 665 | ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 666 | vhost_copy_t *cpy = &cpu->copy[copy_len]; |
| 667 | copy_len++; |
| 668 | u32 desc_data_l = desc_table[desc_current].len - desc_data_offset; |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 669 | cpy->len = buffer_data_size - b_current->current_length; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 670 | cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; |
| 671 | cpy->dst = (uword) (vlib_buffer_get_current (b_current) + |
| 672 | b_current->current_length); |
| 673 | cpy->src = desc_table[desc_current].addr + desc_data_offset; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 674 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 675 | desc_data_offset += cpy->len; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 676 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 677 | b_current->current_length += cpy->len; |
| 678 | b_head->total_length_not_including_first_buffer += cpy->len; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 679 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 680 | |
| 681 | out: |
| 682 | |
| 683 | n_rx_bytes += b_head->total_length_not_including_first_buffer; |
| 684 | n_rx_packets++; |
| 685 | |
| 686 | b_head->total_length_not_including_first_buffer -= |
| 687 | b_head->current_length; |
| 688 | |
| 689 | /* consume the descriptor and return it as used */ |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 690 | last_avail_idx++; |
| 691 | last_used_idx++; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 692 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 693 | vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; |
| 694 | vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 695 | b_head->error = 0; |
| 696 | |
| 697 | if (current_config_index != ~(u32) 0) |
| 698 | { |
| 699 | b_head->current_config_index = current_config_index; |
| 700 | vnet_buffer (b_head)->feature_arc_index = feature_arc_idx; |
| 701 | } |
| 702 | |
| 703 | n_left--; |
| 704 | |
| 705 | /* |
| 706 | * Although separating memory copies from virtio ring parsing |
| 707 | * is beneficial, we can offer to perform the copies from time |
| 708 | * to time in order to free some space in the ring. |
| 709 | */ |
| 710 | if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) |
| 711 | { |
| 712 | if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, |
| 713 | copy_len, &map_hint))) |
| 714 | { |
| 715 | vlib_error_count (vm, node->node_index, |
| 716 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 717 | } |
| 718 | copy_len = 0; |
| 719 | |
| 720 | /* give buffers back to driver */ |
Damjan Marion | 96e8cd0 | 2018-11-23 14:56:55 +0100 | [diff] [blame] | 721 | CLIB_MEMORY_STORE_BARRIER (); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 722 | txvq->used->idx = last_used_idx; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 723 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 724 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 725 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 726 | stop: |
| 727 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 728 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 729 | txvq->last_used_idx = last_used_idx; |
| 730 | txvq->last_avail_idx = last_avail_idx; |
| 731 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 732 | /* Do the memory copies */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 733 | if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len, |
| 734 | &map_hint))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 735 | { |
| 736 | vlib_error_count (vm, node->node_index, |
| 737 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 738 | } |
| 739 | |
| 740 | /* give buffers back to driver */ |
Damjan Marion | 96e8cd0 | 2018-11-23 14:56:55 +0100 | [diff] [blame] | 741 | CLIB_MEMORY_STORE_BARRIER (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 742 | txvq->used->idx = txvq->last_used_idx; |
| 743 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 744 | |
| 745 | /* interrupt (call) handling */ |
| 746 | if ((txvq->callfd_idx != ~0) && |
| 747 | !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) |
| 748 | { |
| 749 | txvq->n_since_last_int += n_rx_packets; |
| 750 | |
| 751 | if (txvq->n_since_last_int > vum->coalesce_frames) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 752 | vhost_user_send_call (vm, vui, txvq); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* increase rx counters */ |
| 756 | vlib_increment_combined_counter |
| 757 | (vnet_main.interface_main.combined_sw_if_counters |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 758 | + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index, |
| 759 | n_rx_packets, n_rx_bytes); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 760 | |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 761 | vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 762 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 763 | done: |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 764 | return n_rx_packets; |
| 765 | } |
| 766 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 767 | static_always_inline void |
| 768 | vhost_user_mark_desc_consumed (vhost_user_intf_t * vui, |
| 769 | vhost_user_vring_t * txvq, u16 desc_head, |
| 770 | u16 n_descs_processed) |
| 771 | { |
| 772 | vring_packed_desc_t *desc_table = txvq->packed_desc; |
| 773 | u16 desc_idx; |
| 774 | u16 mask = txvq->qsz_mask; |
| 775 | |
| 776 | for (desc_idx = 0; desc_idx < n_descs_processed; desc_idx++) |
| 777 | { |
| 778 | if (txvq->used_wrap_counter) |
| 779 | desc_table[(desc_head + desc_idx) & mask].flags |= |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 780 | (VRING_DESC_F_AVAIL | VRING_DESC_F_USED); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 781 | else |
| 782 | desc_table[(desc_head + desc_idx) & mask].flags &= |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 783 | ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 784 | vhost_user_advance_last_used_idx (txvq); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | static_always_inline void |
| 789 | vhost_user_rx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui, |
| 790 | u16 qid, vhost_user_vring_t * txvq, |
| 791 | u16 desc_current) |
| 792 | { |
| 793 | vhost_user_main_t *vum = &vhost_user_main; |
| 794 | vring_packed_desc_t *hdr_desc; |
| 795 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 796 | u32 hint = 0; |
| 797 | |
| 798 | clib_memset (t, 0, sizeof (*t)); |
| 799 | t->device_index = vui - vum->vhost_user_interfaces; |
| 800 | t->qid = qid; |
| 801 | |
| 802 | hdr_desc = &txvq->packed_desc[desc_current]; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 803 | if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 804 | { |
| 805 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; |
| 806 | /* Header is the first here */ |
| 807 | hdr_desc = map_guest_mem (vui, txvq->packed_desc[desc_current].addr, |
| 808 | &hint); |
| 809 | } |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 810 | if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 811 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; |
| 812 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 813 | if (!(txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) && |
| 814 | !(txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 815 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; |
| 816 | |
| 817 | t->first_desc_len = hdr_desc ? hdr_desc->len : 0; |
| 818 | |
| 819 | if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) |
| 820 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; |
| 821 | else |
| 822 | { |
| 823 | u32 len = vui->virtio_net_hdr_sz; |
| 824 | clib_memcpy_fast (&t->hdr, hdr, |
| 825 | len > hdr_desc->len ? hdr_desc->len : len); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | static_always_inline u32 |
| 830 | vhost_user_rx_discard_packet_packed (vlib_main_t * vm, |
| 831 | vhost_user_intf_t * vui, |
| 832 | vhost_user_vring_t * txvq, |
| 833 | u32 discard_max) |
| 834 | { |
| 835 | u32 discarded_packets = 0; |
| 836 | u16 mask = txvq->qsz_mask; |
| 837 | u16 desc_current, desc_head; |
| 838 | |
| 839 | desc_head = desc_current = txvq->last_used_idx & mask; |
| 840 | |
| 841 | /* |
| 842 | * On the RX side, each packet corresponds to one descriptor |
| 843 | * (it is the same whether it is a shallow descriptor, chained, or indirect). |
| 844 | * Therefore, discarding a packet is like discarding a descriptor. |
| 845 | */ |
| 846 | while ((discarded_packets != discard_max) && |
| 847 | vhost_user_packed_desc_available (txvq, desc_current)) |
| 848 | { |
| 849 | vhost_user_advance_last_avail_idx (txvq); |
| 850 | discarded_packets++; |
| 851 | desc_current = (desc_current + 1) & mask; |
| 852 | } |
| 853 | |
| 854 | if (PREDICT_TRUE (discarded_packets)) |
| 855 | vhost_user_mark_desc_consumed (vui, txvq, desc_head, discarded_packets); |
| 856 | return (discarded_packets); |
| 857 | } |
| 858 | |
| 859 | static_always_inline u32 |
| 860 | vhost_user_input_copy_packed (vhost_user_intf_t * vui, vhost_copy_t * cpy, |
| 861 | u16 copy_len, u32 * map_hint) |
| 862 | { |
| 863 | void *src0, *src1, *src2, *src3, *src4, *src5, *src6, *src7; |
| 864 | u8 bad; |
| 865 | u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR; |
| 866 | |
| 867 | if (PREDICT_TRUE (copy_len >= 8)) |
| 868 | { |
| 869 | src4 = map_guest_mem (vui, cpy[0].src, map_hint); |
| 870 | src5 = map_guest_mem (vui, cpy[1].src, map_hint); |
| 871 | src6 = map_guest_mem (vui, cpy[2].src, map_hint); |
| 872 | src7 = map_guest_mem (vui, cpy[3].src, map_hint); |
| 873 | bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0); |
| 874 | if (PREDICT_FALSE (bad)) |
| 875 | goto one_by_one; |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 876 | clib_prefetch_load (src4); |
| 877 | clib_prefetch_load (src5); |
| 878 | clib_prefetch_load (src6); |
| 879 | clib_prefetch_load (src7); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 880 | |
| 881 | while (PREDICT_TRUE (copy_len >= 8)) |
| 882 | { |
| 883 | src0 = src4; |
| 884 | src1 = src5; |
| 885 | src2 = src6; |
| 886 | src3 = src7; |
| 887 | |
| 888 | src4 = map_guest_mem (vui, cpy[4].src, map_hint); |
| 889 | src5 = map_guest_mem (vui, cpy[5].src, map_hint); |
| 890 | src6 = map_guest_mem (vui, cpy[6].src, map_hint); |
| 891 | src7 = map_guest_mem (vui, cpy[7].src, map_hint); |
| 892 | bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0); |
| 893 | if (PREDICT_FALSE (bad)) |
| 894 | break; |
| 895 | |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 896 | clib_prefetch_load (src4); |
| 897 | clib_prefetch_load (src5); |
| 898 | clib_prefetch_load (src6); |
| 899 | clib_prefetch_load (src7); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 900 | |
| 901 | clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len); |
| 902 | clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len); |
| 903 | clib_memcpy_fast ((void *) cpy[2].dst, src2, cpy[2].len); |
| 904 | clib_memcpy_fast ((void *) cpy[3].dst, src3, cpy[3].len); |
| 905 | copy_len -= 4; |
| 906 | cpy += 4; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | one_by_one: |
| 911 | while (copy_len) |
| 912 | { |
| 913 | if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) |
| 914 | { |
| 915 | rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; |
| 916 | break; |
| 917 | } |
| 918 | clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len); |
| 919 | copy_len -= 1; |
| 920 | cpy += 1; |
| 921 | } |
| 922 | return rc; |
| 923 | } |
| 924 | |
| 925 | static_always_inline u32 |
| 926 | vhost_user_do_offload (vhost_user_intf_t * vui, |
| 927 | vring_packed_desc_t * desc_table, u16 desc_current, |
| 928 | u16 mask, vlib_buffer_t * b_head, u32 * map_hint) |
| 929 | { |
| 930 | u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR; |
| 931 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 932 | u8 *b_data; |
| 933 | u32 desc_data_offset = vui->virtio_net_hdr_sz; |
| 934 | |
| 935 | hdr = map_guest_mem (vui, desc_table[desc_current].addr, map_hint); |
| 936 | if (PREDICT_FALSE (hdr == 0)) |
| 937 | rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; |
| 938 | else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) |
| 939 | { |
| 940 | if (desc_data_offset == desc_table[desc_current].len) |
| 941 | { |
| 942 | desc_current = (desc_current + 1) & mask; |
| 943 | b_data = |
| 944 | map_guest_mem (vui, desc_table[desc_current].addr, map_hint); |
| 945 | if (PREDICT_FALSE (b_data == 0)) |
| 946 | rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; |
| 947 | else |
| 948 | vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | b_data = (u8 *) hdr + desc_data_offset; |
| 953 | vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | return rc; |
| 958 | } |
| 959 | |
| 960 | static_always_inline u32 |
| 961 | vhost_user_compute_buffers_required (u32 desc_len, u32 buffer_data_size) |
| 962 | { |
| 963 | div_t result; |
| 964 | u32 buffers_required; |
| 965 | |
| 966 | if (PREDICT_TRUE (buffer_data_size == 2048)) |
| 967 | { |
| 968 | buffers_required = desc_len >> 11; |
| 969 | if ((desc_len & 2047) != 0) |
| 970 | buffers_required++; |
| 971 | return (buffers_required); |
| 972 | } |
| 973 | |
| 974 | if (desc_len < buffer_data_size) |
| 975 | return 1; |
| 976 | |
| 977 | result = div (desc_len, buffer_data_size); |
| 978 | if (result.rem) |
| 979 | buffers_required = result.quot + 1; |
| 980 | else |
| 981 | buffers_required = result.quot; |
| 982 | |
| 983 | return (buffers_required); |
| 984 | } |
| 985 | |
| 986 | static_always_inline u32 |
| 987 | vhost_user_compute_indirect_desc_len (vhost_user_intf_t * vui, |
| 988 | vhost_user_vring_t * txvq, |
| 989 | u32 buffer_data_size, u16 desc_current, |
| 990 | u32 * map_hint) |
| 991 | { |
| 992 | vring_packed_desc_t *desc_table = txvq->packed_desc; |
| 993 | u32 desc_len = 0; |
| 994 | u16 desc_data_offset = vui->virtio_net_hdr_sz; |
| 995 | u16 desc_idx = desc_current; |
| 996 | u32 n_descs; |
| 997 | |
| 998 | n_descs = desc_table[desc_idx].len >> 4; |
| 999 | desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, map_hint); |
| 1000 | if (PREDICT_FALSE (desc_table == 0)) |
| 1001 | return 0; |
| 1002 | |
| 1003 | for (desc_idx = 0; desc_idx < n_descs; desc_idx++) |
| 1004 | desc_len += desc_table[desc_idx].len; |
| 1005 | |
| 1006 | if (PREDICT_TRUE (desc_len > desc_data_offset)) |
| 1007 | desc_len -= desc_data_offset; |
| 1008 | |
| 1009 | return vhost_user_compute_buffers_required (desc_len, buffer_data_size); |
| 1010 | } |
| 1011 | |
| 1012 | static_always_inline u32 |
| 1013 | vhost_user_compute_chained_desc_len (vhost_user_intf_t * vui, |
| 1014 | vhost_user_vring_t * txvq, |
| 1015 | u32 buffer_data_size, u16 * current, |
| 1016 | u16 * n_left) |
| 1017 | { |
| 1018 | vring_packed_desc_t *desc_table = txvq->packed_desc; |
| 1019 | u32 desc_len = 0; |
| 1020 | u16 mask = txvq->qsz_mask; |
| 1021 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1022 | while (desc_table[*current].flags & VRING_DESC_F_NEXT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1023 | { |
| 1024 | desc_len += desc_table[*current].len; |
| 1025 | (*n_left)++; |
| 1026 | *current = (*current + 1) & mask; |
| 1027 | vhost_user_advance_last_avail_idx (txvq); |
| 1028 | } |
| 1029 | desc_len += desc_table[*current].len; |
| 1030 | (*n_left)++; |
| 1031 | *current = (*current + 1) & mask; |
| 1032 | vhost_user_advance_last_avail_idx (txvq); |
| 1033 | |
| 1034 | if (PREDICT_TRUE (desc_len > vui->virtio_net_hdr_sz)) |
| 1035 | desc_len -= vui->virtio_net_hdr_sz; |
| 1036 | |
| 1037 | return vhost_user_compute_buffers_required (desc_len, buffer_data_size); |
| 1038 | } |
| 1039 | |
| 1040 | static_always_inline void |
| 1041 | vhost_user_assemble_packet (vring_packed_desc_t * desc_table, |
| 1042 | u16 * desc_idx, vlib_buffer_t * b_head, |
| 1043 | vlib_buffer_t ** b_current, u32 ** next, |
| 1044 | vlib_buffer_t *** b, u32 * bi_current, |
| 1045 | vhost_cpu_t * cpu, u16 * copy_len, |
| 1046 | u32 * buffers_used, u32 buffers_required, |
| 1047 | u32 * desc_data_offset, u32 buffer_data_size, |
| 1048 | u16 mask) |
| 1049 | { |
| 1050 | u32 desc_data_l; |
| 1051 | |
| 1052 | while (*desc_data_offset < desc_table[*desc_idx].len) |
| 1053 | { |
| 1054 | /* Get more output if necessary. Or end of packet. */ |
| 1055 | if (PREDICT_FALSE ((*b_current)->current_length == buffer_data_size)) |
| 1056 | { |
| 1057 | /* Get next output */ |
| 1058 | u32 bi_next = **next; |
| 1059 | (*next)++; |
| 1060 | (*b_current)->next_buffer = bi_next; |
| 1061 | (*b_current)->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1062 | *bi_current = bi_next; |
| 1063 | *b_current = **b; |
| 1064 | (*b)++; |
| 1065 | (*buffers_used)++; |
| 1066 | ASSERT (*buffers_used <= buffers_required); |
| 1067 | } |
| 1068 | |
| 1069 | /* Prepare a copy order executed later for the data */ |
| 1070 | ASSERT (*copy_len < VHOST_USER_COPY_ARRAY_N); |
| 1071 | vhost_copy_t *cpy = &cpu->copy[*copy_len]; |
| 1072 | (*copy_len)++; |
| 1073 | desc_data_l = desc_table[*desc_idx].len - *desc_data_offset; |
| 1074 | cpy->len = buffer_data_size - (*b_current)->current_length; |
| 1075 | cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; |
| 1076 | cpy->dst = (uword) (vlib_buffer_get_current (*b_current) + |
| 1077 | (*b_current)->current_length); |
| 1078 | cpy->src = desc_table[*desc_idx].addr + *desc_data_offset; |
| 1079 | |
| 1080 | *desc_data_offset += cpy->len; |
| 1081 | |
| 1082 | (*b_current)->current_length += cpy->len; |
| 1083 | b_head->total_length_not_including_first_buffer += cpy->len; |
| 1084 | } |
| 1085 | *desc_idx = (*desc_idx + 1) & mask;; |
| 1086 | *desc_data_offset = 0; |
| 1087 | } |
| 1088 | |
| 1089 | static_always_inline u32 |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1090 | vhost_user_if_input_packed (vlib_main_t *vm, vhost_user_main_t *vum, |
| 1091 | vhost_user_intf_t *vui, u16 qid, |
| 1092 | vlib_node_runtime_t *node, u8 enable_csum) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1093 | { |
| 1094 | vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
| 1095 | vnet_feature_main_t *fm = &feature_main; |
| 1096 | u8 feature_arc_idx = fm->device_input_feature_arc_index; |
| 1097 | u16 n_rx_packets = 0; |
| 1098 | u32 n_rx_bytes = 0; |
| 1099 | u16 n_left = 0; |
| 1100 | u32 buffers_required = 0; |
| 1101 | u32 n_left_to_next, *to_next; |
| 1102 | u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 1103 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 1104 | u32 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 1105 | u32 map_hint = 0; |
| 1106 | vhost_cpu_t *cpu = &vum->cpus[vm->thread_index]; |
| 1107 | u16 copy_len = 0; |
| 1108 | u32 current_config_index = ~0; |
| 1109 | u16 mask = txvq->qsz_mask; |
| 1110 | u16 desc_current, desc_head, last_used_idx; |
| 1111 | vring_packed_desc_t *desc_table = 0; |
| 1112 | u32 n_descs_processed = 0; |
| 1113 | u32 rv; |
| 1114 | vlib_buffer_t **b; |
| 1115 | u32 *next; |
| 1116 | u32 buffers_used = 0; |
| 1117 | u16 current, n_descs_to_process; |
| 1118 | |
| 1119 | /* The descriptor table is not ready yet */ |
| 1120 | if (PREDICT_FALSE (txvq->packed_desc == 0)) |
| 1121 | goto done; |
| 1122 | |
| 1123 | /* do we have pending interrupts ? */ |
| 1124 | vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1125 | vhost_user_input_do_interrupt (vm, vui, txvq, rxvq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1126 | |
| 1127 | /* |
| 1128 | * For adaptive mode, it is optimized to reduce interrupts. |
| 1129 | * If the scheduler switches the input node to polling due |
| 1130 | * to burst of traffic, we tell the driver no interrupt. |
| 1131 | * When the traffic subsides, the scheduler switches the node back to |
| 1132 | * interrupt mode. We must tell the driver we want interrupt. |
| 1133 | */ |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1134 | if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1135 | { |
| 1136 | if ((node->flags & |
| 1137 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || |
| 1138 | !(node->flags & |
| 1139 | VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) |
| 1140 | /* Tell driver we want notification */ |
| 1141 | txvq->used_event->flags = 0; |
| 1142 | else |
| 1143 | /* Tell driver we don't want notification */ |
| 1144 | txvq->used_event->flags = VRING_EVENT_F_DISABLE; |
| 1145 | } |
| 1146 | |
| 1147 | last_used_idx = txvq->last_used_idx & mask; |
| 1148 | desc_head = desc_current = last_used_idx; |
| 1149 | |
| 1150 | if (vhost_user_packed_desc_available (txvq, desc_current) == 0) |
| 1151 | goto done; |
| 1152 | |
| 1153 | if (PREDICT_FALSE (!vui->admin_up || !vui->is_ready || !(txvq->enabled))) |
| 1154 | { |
| 1155 | /* |
| 1156 | * Discard input packet if interface is admin down or vring is not |
| 1157 | * enabled. |
| 1158 | * "For example, for a networking device, in the disabled state |
| 1159 | * client must not supply any new RX packets, but must process |
| 1160 | * and discard any TX packets." |
| 1161 | */ |
| 1162 | rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, |
| 1163 | VHOST_USER_DOWN_DISCARD_COUNT); |
| 1164 | vlib_error_count (vm, vhost_user_input_node.index, |
| 1165 | VHOST_USER_INPUT_FUNC_ERROR_NOT_READY, rv); |
| 1166 | goto done; |
| 1167 | } |
| 1168 | |
| 1169 | vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index, |
| 1170 | &next_index, &to_next, &n_left_to_next); |
| 1171 | |
| 1172 | /* |
| 1173 | * Compute n_left and total buffers needed |
| 1174 | */ |
| 1175 | desc_table = txvq->packed_desc; |
| 1176 | current = desc_current; |
| 1177 | while (vhost_user_packed_desc_available (txvq, current) && |
| 1178 | (n_left < VLIB_FRAME_SIZE)) |
| 1179 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1180 | if (desc_table[current].flags & VRING_DESC_F_INDIRECT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1181 | { |
| 1182 | buffers_required += |
| 1183 | vhost_user_compute_indirect_desc_len (vui, txvq, buffer_data_size, |
| 1184 | current, &map_hint); |
| 1185 | n_left++; |
| 1186 | current = (current + 1) & mask; |
| 1187 | vhost_user_advance_last_avail_idx (txvq); |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | buffers_required += |
| 1192 | vhost_user_compute_chained_desc_len (vui, txvq, buffer_data_size, |
| 1193 | ¤t, &n_left); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /* Something is broken if we need more than 10000 buffers */ |
| 1198 | if (PREDICT_FALSE ((buffers_required == 0) || (buffers_required > 10000))) |
| 1199 | { |
| 1200 | rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left); |
| 1201 | vlib_error_count (vm, vhost_user_input_node.index, |
| 1202 | VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv); |
| 1203 | goto done; |
| 1204 | } |
| 1205 | |
| 1206 | vec_validate (cpu->to_next_list, buffers_required); |
| 1207 | rv = vlib_buffer_alloc (vm, cpu->to_next_list, buffers_required); |
| 1208 | if (PREDICT_FALSE (rv != buffers_required)) |
| 1209 | { |
| 1210 | vlib_buffer_free (vm, cpu->to_next_list, rv); |
| 1211 | rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left); |
| 1212 | vlib_error_count (vm, vhost_user_input_node.index, |
| 1213 | VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv); |
| 1214 | goto done; |
| 1215 | } |
| 1216 | |
| 1217 | next = cpu->to_next_list; |
| 1218 | vec_validate (cpu->rx_buffers_pdesc, buffers_required); |
| 1219 | vlib_get_buffers (vm, next, cpu->rx_buffers_pdesc, buffers_required); |
| 1220 | b = cpu->rx_buffers_pdesc; |
| 1221 | n_descs_processed = n_left; |
| 1222 | |
| 1223 | while (n_left) |
| 1224 | { |
| 1225 | vlib_buffer_t *b_head, *b_current; |
| 1226 | u32 bi_current; |
| 1227 | u32 desc_data_offset; |
| 1228 | u16 desc_idx = desc_current; |
| 1229 | u32 n_descs; |
| 1230 | |
| 1231 | desc_table = txvq->packed_desc; |
| 1232 | to_next[0] = bi_current = next[0]; |
| 1233 | b_head = b_current = b[0]; |
| 1234 | b++; |
| 1235 | buffers_used++; |
| 1236 | ASSERT (buffers_used <= buffers_required); |
| 1237 | to_next++; |
| 1238 | next++; |
| 1239 | n_left_to_next--; |
| 1240 | |
| 1241 | /* The buffer should already be initialized */ |
| 1242 | b_head->total_length_not_including_first_buffer = 0; |
| 1243 | b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1244 | desc_data_offset = vui->virtio_net_hdr_sz; |
| 1245 | n_descs_to_process = 1; |
| 1246 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1247 | if (desc_table[desc_idx].flags & VRING_DESC_F_INDIRECT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1248 | { |
| 1249 | n_descs = desc_table[desc_idx].len >> 4; |
| 1250 | desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, |
| 1251 | &map_hint); |
| 1252 | desc_idx = 0; |
| 1253 | if (PREDICT_FALSE (desc_table == 0) || |
| 1254 | (enable_csum && |
| 1255 | (PREDICT_FALSE |
| 1256 | (vhost_user_do_offload |
| 1257 | (vui, desc_table, desc_idx, mask, b_head, |
| 1258 | &map_hint) != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)))) |
| 1259 | { |
| 1260 | vlib_error_count (vm, node->node_index, |
| 1261 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 1262 | to_next--; |
| 1263 | next--; |
| 1264 | n_left_to_next++; |
| 1265 | buffers_used--; |
| 1266 | b--; |
| 1267 | goto out; |
| 1268 | } |
| 1269 | while (n_descs) |
| 1270 | { |
| 1271 | vhost_user_assemble_packet (desc_table, &desc_idx, b_head, |
| 1272 | &b_current, &next, &b, &bi_current, |
| 1273 | cpu, ©_len, &buffers_used, |
| 1274 | buffers_required, &desc_data_offset, |
| 1275 | buffer_data_size, mask); |
| 1276 | n_descs--; |
| 1277 | } |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | if (enable_csum) |
| 1282 | { |
| 1283 | rv = vhost_user_do_offload (vui, desc_table, desc_idx, mask, |
| 1284 | b_head, &map_hint); |
| 1285 | if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) |
| 1286 | { |
| 1287 | vlib_error_count (vm, node->node_index, rv, 1); |
| 1288 | to_next--; |
| 1289 | next--; |
| 1290 | n_left_to_next++; |
| 1291 | buffers_used--; |
| 1292 | b--; |
| 1293 | goto out; |
| 1294 | } |
| 1295 | } |
| 1296 | /* |
| 1297 | * For chained descriptor, we process all chains in a single while |
| 1298 | * loop. So count how many descriptors in the chain. |
| 1299 | */ |
| 1300 | n_descs_to_process = 1; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1301 | while (desc_table[desc_idx].flags & VRING_DESC_F_NEXT) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1302 | { |
| 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); |
| 1308 | n_descs_to_process++; |
| 1309 | } |
| 1310 | vhost_user_assemble_packet (desc_table, &desc_idx, b_head, |
| 1311 | &b_current, &next, &b, &bi_current, |
| 1312 | cpu, ©_len, &buffers_used, |
| 1313 | buffers_required, &desc_data_offset, |
| 1314 | buffer_data_size, mask); |
| 1315 | } |
| 1316 | |
| 1317 | n_rx_bytes += b_head->total_length_not_including_first_buffer; |
| 1318 | n_rx_packets++; |
| 1319 | |
| 1320 | b_head->total_length_not_including_first_buffer -= |
| 1321 | b_head->current_length; |
| 1322 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1323 | vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; |
| 1324 | vnet_buffer (b_head)->sw_if_index[VLIB_TX] = ~0; |
| 1325 | b_head->error = 0; |
| 1326 | |
| 1327 | if (current_config_index != ~0) |
| 1328 | { |
| 1329 | b_head->current_config_index = current_config_index; |
| 1330 | vnet_buffer (b_head)->feature_arc_index = feature_arc_idx; |
| 1331 | } |
| 1332 | |
| 1333 | out: |
| 1334 | ASSERT (n_left >= n_descs_to_process); |
| 1335 | n_left -= n_descs_to_process; |
| 1336 | |
| 1337 | /* advance to next descrptor */ |
| 1338 | desc_current = (desc_current + n_descs_to_process) & mask; |
| 1339 | |
| 1340 | /* |
| 1341 | * Although separating memory copies from virtio ring parsing |
| 1342 | * is beneficial, we can offer to perform the copies from time |
| 1343 | * to time in order to free some space in the ring. |
| 1344 | */ |
| 1345 | if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) |
| 1346 | { |
| 1347 | rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, |
| 1348 | &map_hint); |
| 1349 | if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) |
| 1350 | vlib_error_count (vm, node->node_index, rv, 1); |
| 1351 | copy_len = 0; |
| 1352 | } |
| 1353 | } |
| 1354 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1355 | |
| 1356 | /* Do the memory copies */ |
| 1357 | rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, &map_hint); |
| 1358 | if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) |
| 1359 | vlib_error_count (vm, node->node_index, rv, 1); |
| 1360 | |
| 1361 | /* Must do the tracing before giving buffers back to driver */ |
| 1362 | if (PREDICT_FALSE (n_trace)) |
| 1363 | { |
| 1364 | u32 left = n_rx_packets; |
| 1365 | |
| 1366 | b = cpu->rx_buffers_pdesc; |
| 1367 | while (n_trace && left) |
| 1368 | { |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 1369 | if (PREDICT_TRUE |
| 1370 | (vlib_trace_buffer |
| 1371 | (vm, node, next_index, b[0], /* follow_chain */ 0))) |
| 1372 | { |
| 1373 | vhost_trace_t *t0; |
| 1374 | t0 = vlib_add_trace (vm, node, b[0], sizeof (t0[0])); |
| 1375 | vhost_user_rx_trace_packed (t0, vui, qid, txvq, last_used_idx); |
| 1376 | last_used_idx = (last_used_idx + 1) & mask; |
| 1377 | n_trace--; |
| 1378 | vlib_set_trace_count (vm, node, n_trace); |
| 1379 | } |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1380 | left--; |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 1381 | b++; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Give buffers back to driver. |
| 1387 | */ |
| 1388 | vhost_user_mark_desc_consumed (vui, txvq, desc_head, n_descs_processed); |
| 1389 | |
| 1390 | /* interrupt (call) handling */ |
| 1391 | if ((txvq->callfd_idx != ~0) && |
| 1392 | (txvq->avail_event->flags != VRING_EVENT_F_DISABLE)) |
| 1393 | { |
| 1394 | txvq->n_since_last_int += n_rx_packets; |
| 1395 | if (txvq->n_since_last_int > vum->coalesce_frames) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1396 | vhost_user_send_call (vm, vui, txvq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* increase rx counters */ |
| 1400 | vlib_increment_combined_counter |
| 1401 | (vnet_main.interface_main.combined_sw_if_counters |
| 1402 | + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index, |
| 1403 | n_rx_packets, n_rx_bytes); |
| 1404 | |
| 1405 | vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets); |
| 1406 | |
| 1407 | if (PREDICT_FALSE (buffers_used < buffers_required)) |
| 1408 | vlib_buffer_free (vm, next, buffers_required - buffers_used); |
| 1409 | |
| 1410 | done: |
| 1411 | return n_rx_packets; |
| 1412 | } |
| 1413 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1414 | VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm, |
| 1415 | vlib_node_runtime_t * node, |
| 1416 | vlib_frame_t * frame) |
| 1417 | { |
| 1418 | vhost_user_main_t *vum = &vhost_user_main; |
| 1419 | uword n_rx_packets = 0; |
| 1420 | vhost_user_intf_t *vui; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1421 | vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node); |
| 1422 | vnet_hw_if_rxq_poll_vector_t *pve; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1423 | |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1424 | vec_foreach (pve, pv) |
| 1425 | { |
| 1426 | vui = pool_elt_at_index (vum->vhost_user_interfaces, pve->dev_instance); |
| 1427 | if (vhost_user_is_packed_ring_supported (vui)) |
| 1428 | { |
| 1429 | if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)) |
| 1430 | n_rx_packets += vhost_user_if_input_packed ( |
| 1431 | vm, vum, vui, pve->queue_id, node, 1); |
| 1432 | else |
| 1433 | n_rx_packets += vhost_user_if_input_packed ( |
| 1434 | vm, vum, vui, pve->queue_id, node, 0); |
| 1435 | } |
| 1436 | else |
| 1437 | { |
| 1438 | if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)) |
| 1439 | n_rx_packets += |
| 1440 | vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 1); |
| 1441 | else |
| 1442 | n_rx_packets += |
| 1443 | vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 0); |
| 1444 | } |
| 1445 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1446 | |
| 1447 | return n_rx_packets; |
| 1448 | } |
| 1449 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1450 | /* *INDENT-OFF* */ |
| 1451 | VLIB_REGISTER_NODE (vhost_user_input_node) = { |
| 1452 | .type = VLIB_NODE_TYPE_INPUT, |
| 1453 | .name = "vhost-user-input", |
| 1454 | .sibling_of = "device-input", |
Damjan Marion | 7ca5aaa | 2019-09-24 18:10:49 +0200 | [diff] [blame] | 1455 | .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1456 | |
| 1457 | /* Will be enabled if/when hardware is detected. */ |
| 1458 | .state = VLIB_NODE_STATE_DISABLED, |
| 1459 | |
| 1460 | .format_buffer = format_ethernet_header_with_length, |
| 1461 | .format_trace = format_vhost_trace, |
| 1462 | |
| 1463 | .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR, |
| 1464 | .error_strings = vhost_user_input_func_error_strings, |
| 1465 | }; |
| 1466 | /* *INDENT-ON* */ |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1467 | |
| 1468 | /* |
| 1469 | * fd.io coding-style-patch-verification: ON |
| 1470 | * |
| 1471 | * Local Variables: |
| 1472 | * eval: (c-set-style "gnu") |
| 1473 | * End: |
| 1474 | */ |