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 | |
| 36 | #include <vnet/ip/ip.h> |
| 37 | |
| 38 | #include <vnet/ethernet/ethernet.h> |
| 39 | #include <vnet/devices/devices.h> |
| 40 | #include <vnet/feature/feature.h> |
| 41 | |
| 42 | #include <vnet/devices/virtio/vhost_user.h> |
| 43 | #include <vnet/devices/virtio/vhost_user_inline.h> |
| 44 | |
| 45 | /* |
| 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. |
| 49 | */ |
| 50 | #define VHOST_USER_DOWN_DISCARD_COUNT 256 |
| 51 | |
| 52 | /* |
| 53 | * When the number of available buffers gets under this threshold, |
| 54 | * RX node will start discarding packets. |
| 55 | */ |
| 56 | #define VHOST_USER_RX_BUFFER_STARVATION 32 |
| 57 | |
| 58 | /* |
| 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 |
| 63 | * the guest. |
| 64 | * The value 64 was obtained by testing (48 and 128 were not as good). |
| 65 | */ |
| 66 | #define VHOST_USER_RX_COPY_THRESHOLD 64 |
| 67 | |
| 68 | vlib_node_registration_t vhost_user_input_node; |
| 69 | |
| 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 | _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)") |
| 77 | |
| 78 | typedef enum |
| 79 | { |
| 80 | #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f, |
| 81 | foreach_vhost_user_input_func_error |
| 82 | #undef _ |
| 83 | VHOST_USER_INPUT_FUNC_N_ERROR, |
| 84 | } vhost_user_input_func_error_t; |
| 85 | |
| 86 | static __clib_unused char *vhost_user_input_func_error_strings[] = { |
| 87 | #define _(n,s) s, |
| 88 | foreach_vhost_user_input_func_error |
| 89 | #undef _ |
| 90 | }; |
| 91 | |
| 92 | static_always_inline void |
| 93 | vhost_user_rx_trace (vhost_trace_t * t, |
| 94 | vhost_user_intf_t * vui, u16 qid, |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 95 | vlib_buffer_t * b, vhost_user_vring_t * txvq, |
| 96 | u16 last_avail_idx) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 97 | { |
| 98 | vhost_user_main_t *vum = &vhost_user_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 99 | u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask]; |
| 100 | vring_desc_t *hdr_desc = 0; |
| 101 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 102 | u32 hint = 0; |
| 103 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 104 | clib_memset (t, 0, sizeof (*t)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 105 | t->device_index = vui - vum->vhost_user_interfaces; |
| 106 | t->qid = qid; |
| 107 | |
| 108 | hdr_desc = &txvq->desc[desc_current]; |
| 109 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT) |
| 110 | { |
| 111 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; |
| 112 | /* Header is the first here */ |
| 113 | hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint); |
| 114 | } |
| 115 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) |
| 116 | { |
| 117 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; |
| 118 | } |
| 119 | if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) && |
| 120 | !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)) |
| 121 | { |
| 122 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; |
| 123 | } |
| 124 | |
| 125 | t->first_desc_len = hdr_desc ? hdr_desc->len : 0; |
| 126 | |
| 127 | if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) |
| 128 | { |
| 129 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | u32 len = vui->virtio_net_hdr_sz; |
| 134 | memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static_always_inline u32 |
| 139 | vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, |
| 140 | u16 copy_len, u32 * map_hint) |
| 141 | { |
| 142 | void *src0, *src1, *src2, *src3; |
| 143 | if (PREDICT_TRUE (copy_len >= 4)) |
| 144 | { |
| 145 | if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint)))) |
| 146 | return 1; |
| 147 | if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint)))) |
| 148 | return 1; |
| 149 | |
| 150 | while (PREDICT_TRUE (copy_len >= 4)) |
| 151 | { |
| 152 | src0 = src2; |
| 153 | src1 = src3; |
| 154 | |
| 155 | if (PREDICT_FALSE |
| 156 | (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint)))) |
| 157 | return 1; |
| 158 | if (PREDICT_FALSE |
| 159 | (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint)))) |
| 160 | return 1; |
| 161 | |
| 162 | CLIB_PREFETCH (src2, 64, LOAD); |
| 163 | CLIB_PREFETCH (src3, 64, LOAD); |
| 164 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 165 | clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len); |
| 166 | clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 167 | copy_len -= 2; |
| 168 | cpy += 2; |
| 169 | } |
| 170 | } |
| 171 | while (copy_len) |
| 172 | { |
| 173 | if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) |
| 174 | return 1; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 175 | clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 176 | copy_len -= 1; |
| 177 | cpy += 1; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Try to discard packets from the tx ring (VPP RX path). |
| 184 | * Returns the number of discarded packets. |
| 185 | */ |
| 186 | static_always_inline u32 |
| 187 | vhost_user_rx_discard_packet (vlib_main_t * vm, |
| 188 | vhost_user_intf_t * vui, |
| 189 | vhost_user_vring_t * txvq, u32 discard_max) |
| 190 | { |
| 191 | /* |
| 192 | * On the RX side, each packet corresponds to one descriptor |
| 193 | * (it is the same whether it is a shallow descriptor, chained, or indirect). |
| 194 | * Therefore, discarding a packet is like discarding a descriptor. |
| 195 | */ |
| 196 | u32 discarded_packets = 0; |
| 197 | u32 avail_idx = txvq->avail->idx; |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 198 | u16 mask = txvq->qsz_mask; |
| 199 | u16 last_avail_idx = txvq->last_avail_idx; |
| 200 | u16 last_used_idx = txvq->last_used_idx; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 201 | while (discarded_packets != discard_max) |
| 202 | { |
| 203 | if (avail_idx == txvq->last_avail_idx) |
| 204 | goto out; |
| 205 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 206 | u16 desc_chain_head = txvq->avail->ring[last_avail_idx & mask]; |
| 207 | last_avail_idx++; |
| 208 | txvq->used->ring[last_used_idx & mask].id = desc_chain_head; |
| 209 | txvq->used->ring[last_used_idx & mask].len = 0; |
| 210 | vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); |
| 211 | last_used_idx++; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 212 | discarded_packets++; |
| 213 | } |
| 214 | |
| 215 | out: |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 216 | txvq->last_avail_idx = last_avail_idx; |
| 217 | txvq->last_used_idx = last_used_idx; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 218 | CLIB_MEMORY_BARRIER (); |
| 219 | txvq->used->idx = txvq->last_used_idx; |
| 220 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 221 | return discarded_packets; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * In case of overflow, we need to rewind the array of allocated buffers. |
| 226 | */ |
Damjan Marion | 46bf866 | 2018-11-22 22:25:38 +0100 | [diff] [blame] | 227 | static_always_inline void |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 228 | vhost_user_input_rewind_buffers (vlib_main_t * vm, |
| 229 | vhost_cpu_t * cpu, vlib_buffer_t * b_head) |
| 230 | { |
| 231 | u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 232 | vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current); |
| 233 | b_current->current_length = 0; |
| 234 | b_current->flags = 0; |
| 235 | while (b_current != b_head) |
| 236 | { |
| 237 | cpu->rx_buffers_len++; |
| 238 | bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 239 | b_current = vlib_get_buffer (vm, bi_current); |
| 240 | b_current->current_length = 0; |
| 241 | b_current->flags = 0; |
| 242 | } |
| 243 | cpu->rx_buffers_len++; |
| 244 | } |
| 245 | |
Damjan Marion | 46bf866 | 2018-11-22 22:25:38 +0100 | [diff] [blame] | 246 | static_always_inline u32 |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 247 | vhost_user_if_input (vlib_main_t * vm, |
| 248 | vhost_user_main_t * vum, |
| 249 | vhost_user_intf_t * vui, |
| 250 | u16 qid, vlib_node_runtime_t * node, |
| 251 | vnet_hw_interface_rx_mode mode) |
| 252 | { |
| 253 | vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 254 | vnet_feature_main_t *fm = &feature_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 255 | u16 n_rx_packets = 0; |
| 256 | u32 n_rx_bytes = 0; |
| 257 | u16 n_left; |
| 258 | u32 n_left_to_next, *to_next; |
| 259 | u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 260 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 261 | u32 map_hint = 0; |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 262 | vhost_cpu_t *cpu = &vum->cpus[vm->thread_index]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 263 | u16 copy_len = 0; |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 264 | u8 feature_arc_idx = fm->device_input_feature_arc_index; |
| 265 | u32 current_config_index = ~(u32) 0; |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 266 | u16 mask = txvq->qsz_mask; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 267 | |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 268 | /* The descriptor table is not ready yet */ |
| 269 | if (PREDICT_FALSE (txvq->avail == 0)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 270 | goto done; |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 271 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 272 | { |
| 273 | /* do we have pending interrupts ? */ |
| 274 | vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; |
| 275 | f64 now = vlib_time_now (vm); |
| 276 | |
| 277 | if ((txvq->n_since_last_int) && (txvq->int_deadline < now)) |
| 278 | vhost_user_send_call (vm, txvq); |
| 279 | |
| 280 | if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now)) |
| 281 | vhost_user_send_call (vm, rxvq); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * For adaptive mode, it is optimized to reduce interrupts. |
| 286 | * If the scheduler switches the input node to polling due |
| 287 | * to burst of traffic, we tell the driver no interrupt. |
| 288 | * When the traffic subsides, the scheduler switches the node back to |
| 289 | * interrupt mode. We must tell the driver we want interrupt. |
| 290 | */ |
| 291 | if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) |
| 292 | { |
| 293 | if ((node->flags & |
| 294 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || |
| 295 | !(node->flags & |
| 296 | VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) |
| 297 | /* Tell driver we want notification */ |
| 298 | txvq->used->flags = 0; |
| 299 | else |
| 300 | /* Tell driver we don't want notification */ |
| 301 | txvq->used->flags = VRING_USED_F_NO_NOTIFY; |
| 302 | } |
| 303 | |
| 304 | if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 305 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 306 | |
| 307 | n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx); |
| 308 | |
| 309 | /* nothing to do */ |
| 310 | if (PREDICT_FALSE (n_left == 0)) |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 311 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 312 | |
| 313 | if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled))) |
| 314 | { |
| 315 | /* |
| 316 | * Discard input packet if interface is admin down or vring is not |
| 317 | * enabled. |
| 318 | * "For example, for a networking device, in the disabled state |
| 319 | * client must not supply any new RX packets, but must process |
| 320 | * and discard any TX packets." |
| 321 | */ |
| 322 | vhost_user_rx_discard_packet (vm, vui, txvq, |
| 323 | VHOST_USER_DOWN_DISCARD_COUNT); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 324 | goto done; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 327 | if (PREDICT_FALSE (n_left == (mask + 1))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 328 | { |
| 329 | /* |
| 330 | * Informational error logging when VPP is not |
| 331 | * receiving packets fast enough. |
| 332 | */ |
| 333 | vlib_error_count (vm, node->node_index, |
| 334 | VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1); |
| 335 | } |
| 336 | |
| 337 | if (n_left > VLIB_FRAME_SIZE) |
| 338 | n_left = VLIB_FRAME_SIZE; |
| 339 | |
| 340 | /* |
| 341 | * For small packets (<2kB), we will not need more than one vlib buffer |
| 342 | * per packet. In case packets are bigger, we will just yeld at some point |
| 343 | * in the loop and come back later. This is not an issue as for big packet, |
| 344 | * processing cost really comes from the memory copy. |
| 345 | * The assumption is that big packets will fit in 40 buffers. |
| 346 | */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 347 | if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 || |
| 348 | cpu->rx_buffers_len < 40)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 349 | { |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 350 | u32 curr_len = cpu->rx_buffers_len; |
| 351 | cpu->rx_buffers_len += |
| 352 | vlib_buffer_alloc_from_free_list (vm, cpu->rx_buffers + curr_len, |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 353 | VHOST_USER_RX_BUFFERS_N - curr_len, |
| 354 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 355 | |
| 356 | if (PREDICT_FALSE |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 357 | (cpu->rx_buffers_len < VHOST_USER_RX_BUFFER_STARVATION)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 358 | { |
| 359 | /* In case of buffer starvation, discard some packets from the queue |
| 360 | * and log the event. |
| 361 | * We keep doing best effort for the remaining packets. */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 362 | u32 flush = (n_left + 1 > cpu->rx_buffers_len) ? |
| 363 | n_left + 1 - cpu->rx_buffers_len : 1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 364 | flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush); |
| 365 | |
| 366 | n_left -= flush; |
| 367 | vlib_increment_simple_counter (vnet_main. |
| 368 | interface_main.sw_if_counters + |
| 369 | VNET_INTERFACE_COUNTER_DROP, |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 370 | vm->thread_index, vui->sw_if_index, |
| 371 | flush); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 372 | |
| 373 | vlib_error_count (vm, vhost_user_input_node.index, |
| 374 | VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush); |
| 375 | } |
| 376 | } |
| 377 | |
Damjan Marion | 9af4504 | 2018-11-21 09:51:42 +0100 | [diff] [blame] | 378 | if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index))) |
| 379 | { |
| 380 | vnet_feature_config_main_t *cm; |
| 381 | cm = &fm->feature_config_mains[feature_arc_idx]; |
| 382 | current_config_index = vec_elt (cm->config_index_by_sw_if_index, |
| 383 | vui->sw_if_index); |
| 384 | vnet_get_config_data (&cm->config_main, ¤t_config_index, |
| 385 | &next_index, 0); |
| 386 | } |
| 387 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 388 | u16 last_avail_idx = txvq->last_avail_idx; |
| 389 | u16 last_used_idx = txvq->last_used_idx; |
| 390 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 391 | vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 392 | |
| 393 | if (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT) |
| 394 | { |
| 395 | /* give some hints to ethernet-input */ |
| 396 | vlib_next_frame_t *nf; |
| 397 | vlib_frame_t *f; |
| 398 | ethernet_input_frame_t *ef; |
| 399 | nf = vlib_node_runtime_get_next_frame (vm, node, next_index); |
| 400 | f = vlib_get_frame (vm, nf->frame_index); |
| 401 | f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX; |
| 402 | |
| 403 | ef = vlib_frame_scalar_args (f); |
| 404 | ef->sw_if_index = vui->sw_if_index; |
| 405 | ef->hw_if_index = vui->hw_if_index; |
| 406 | } |
| 407 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 408 | while (n_left > 0) |
| 409 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 410 | vlib_buffer_t *b_head, *b_current; |
| 411 | u32 bi_current; |
| 412 | u16 desc_current; |
| 413 | u32 desc_data_offset; |
| 414 | vring_desc_t *desc_table = txvq->desc; |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 415 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 416 | if (PREDICT_FALSE (cpu->rx_buffers_len <= 1)) |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 417 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 418 | /* Not enough rx_buffers |
| 419 | * Note: We yeld on 1 so we don't need to do an additional |
| 420 | * check for the next buffer prefetch. |
| 421 | */ |
| 422 | n_left = 0; |
| 423 | break; |
Damjan Marion | 6a8bfd4 | 2018-11-21 09:54:41 +0100 | [diff] [blame] | 424 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 425 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 426 | desc_current = txvq->avail->ring[last_avail_idx & mask]; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 427 | cpu->rx_buffers_len--; |
| 428 | bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 429 | b_head = b_current = vlib_get_buffer (vm, bi_current); |
| 430 | to_next[0] = bi_current; //We do that now so we can forget about bi_current |
| 431 | to_next++; |
| 432 | n_left_to_next--; |
| 433 | |
| 434 | vlib_prefetch_buffer_with_index |
| 435 | (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD); |
| 436 | |
| 437 | /* Just preset the used descriptor id and length for later */ |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 438 | txvq->used->ring[last_used_idx & mask].id = desc_current; |
| 439 | txvq->used->ring[last_used_idx & mask].len = 0; |
| 440 | vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 441 | |
| 442 | /* The buffer should already be initialized */ |
| 443 | b_head->total_length_not_including_first_buffer = 0; |
| 444 | b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 445 | |
| 446 | if (PREDICT_FALSE (n_trace)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 447 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 448 | //TODO: next_index is not exactly known at that point |
| 449 | vlib_trace_buffer (vm, node, next_index, b_head, |
| 450 | /* follow_chain */ 0); |
| 451 | vhost_trace_t *t0 = |
| 452 | vlib_add_trace (vm, node, b_head, sizeof (t0[0])); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 453 | 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] | 454 | n_trace--; |
| 455 | vlib_set_trace_count (vm, node, n_trace); |
| 456 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 457 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 458 | /* This depends on the setup but is very consistent |
| 459 | * So I think the CPU branch predictor will make a pretty good job |
| 460 | * at optimizing the decision. */ |
| 461 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT) |
| 462 | { |
| 463 | desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr, |
| 464 | &map_hint); |
| 465 | desc_current = 0; |
| 466 | if (PREDICT_FALSE (desc_table == 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 467 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 468 | vlib_error_count (vm, node->node_index, |
| 469 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 470 | goto out; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 471 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 472 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 473 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 474 | if (PREDICT_TRUE (vui->is_any_layout) || |
| 475 | (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT))) |
| 476 | { |
| 477 | /* ANYLAYOUT or single buffer */ |
| 478 | desc_data_offset = vui->virtio_net_hdr_sz; |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | /* CSR case without ANYLAYOUT, skip 1st buffer */ |
| 483 | desc_data_offset = desc_table[desc_current].len; |
| 484 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 485 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 486 | while (1) |
| 487 | { |
| 488 | /* Get more input if necessary. Or end of packet. */ |
| 489 | if (desc_data_offset == desc_table[desc_current].len) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 490 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 491 | if (PREDICT_FALSE (desc_table[desc_current].flags & |
| 492 | VIRTQ_DESC_F_NEXT)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 493 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 494 | desc_current = desc_table[desc_current].next; |
| 495 | desc_data_offset = 0; |
| 496 | } |
| 497 | else |
| 498 | { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 499 | goto out; |
| 500 | } |
| 501 | } |
| 502 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 503 | /* Get more output if necessary. Or end of packet. */ |
| 504 | if (PREDICT_FALSE |
| 505 | (b_current->current_length == VLIB_BUFFER_DATA_SIZE)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 506 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 507 | if (PREDICT_FALSE (cpu->rx_buffers_len == 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 508 | { |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 509 | /* Cancel speculation */ |
| 510 | to_next--; |
| 511 | n_left_to_next++; |
| 512 | |
| 513 | /* |
| 514 | * Checking if there are some left buffers. |
| 515 | * If not, just rewind the used buffers and stop. |
| 516 | * Note: Scheduled copies are not cancelled. This is |
| 517 | * not an issue as they would still be valid. Useless, |
| 518 | * but valid. |
| 519 | */ |
| 520 | vhost_user_input_rewind_buffers (vm, cpu, b_head); |
| 521 | n_left = 0; |
| 522 | goto stop; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 525 | /* Get next output */ |
| 526 | cpu->rx_buffers_len--; |
| 527 | u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 528 | b_current->next_buffer = bi_next; |
| 529 | b_current->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 530 | bi_current = bi_next; |
| 531 | b_current = vlib_get_buffer (vm, bi_current); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 534 | /* Prepare a copy order executed later for the data */ |
| 535 | vhost_copy_t *cpy = &cpu->copy[copy_len]; |
| 536 | copy_len++; |
| 537 | u32 desc_data_l = desc_table[desc_current].len - desc_data_offset; |
| 538 | cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length; |
| 539 | cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; |
| 540 | cpy->dst = (uword) (vlib_buffer_get_current (b_current) + |
| 541 | b_current->current_length); |
| 542 | cpy->src = desc_table[desc_current].addr + desc_data_offset; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 543 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 544 | desc_data_offset += cpy->len; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 545 | |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 546 | b_current->current_length += cpy->len; |
| 547 | b_head->total_length_not_including_first_buffer += cpy->len; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 548 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 549 | |
| 550 | out: |
| 551 | |
| 552 | n_rx_bytes += b_head->total_length_not_including_first_buffer; |
| 553 | n_rx_packets++; |
| 554 | |
| 555 | b_head->total_length_not_including_first_buffer -= |
| 556 | b_head->current_length; |
| 557 | |
| 558 | /* consume the descriptor and return it as used */ |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 559 | last_avail_idx++; |
| 560 | last_used_idx++; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 561 | |
| 562 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head); |
| 563 | |
| 564 | vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; |
| 565 | vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 566 | b_head->error = 0; |
| 567 | |
| 568 | if (current_config_index != ~(u32) 0) |
| 569 | { |
| 570 | b_head->current_config_index = current_config_index; |
| 571 | vnet_buffer (b_head)->feature_arc_index = feature_arc_idx; |
| 572 | } |
| 573 | |
| 574 | n_left--; |
| 575 | |
| 576 | /* |
| 577 | * Although separating memory copies from virtio ring parsing |
| 578 | * is beneficial, we can offer to perform the copies from time |
| 579 | * to time in order to free some space in the ring. |
| 580 | */ |
| 581 | if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) |
| 582 | { |
| 583 | if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, |
| 584 | copy_len, &map_hint))) |
| 585 | { |
| 586 | vlib_error_count (vm, node->node_index, |
| 587 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 588 | } |
| 589 | copy_len = 0; |
| 590 | |
| 591 | /* give buffers back to driver */ |
| 592 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 593 | txvq->used->idx = last_used_idx; |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 594 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 595 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 596 | } |
Damjan Marion | 9282538 | 2018-11-21 10:03:44 +0100 | [diff] [blame] | 597 | stop: |
| 598 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 599 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 600 | txvq->last_used_idx = last_used_idx; |
| 601 | txvq->last_avail_idx = last_avail_idx; |
| 602 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 603 | /* Do the memory copies */ |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 604 | if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len, |
| 605 | &map_hint))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 606 | { |
| 607 | vlib_error_count (vm, node->node_index, |
| 608 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
| 609 | } |
| 610 | |
| 611 | /* give buffers back to driver */ |
| 612 | CLIB_MEMORY_BARRIER (); |
| 613 | txvq->used->idx = txvq->last_used_idx; |
| 614 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 615 | |
| 616 | /* interrupt (call) handling */ |
| 617 | if ((txvq->callfd_idx != ~0) && |
| 618 | !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) |
| 619 | { |
| 620 | txvq->n_since_last_int += n_rx_packets; |
| 621 | |
| 622 | if (txvq->n_since_last_int > vum->coalesce_frames) |
| 623 | vhost_user_send_call (vm, txvq); |
| 624 | } |
| 625 | |
| 626 | /* increase rx counters */ |
| 627 | vlib_increment_combined_counter |
| 628 | (vnet_main.interface_main.combined_sw_if_counters |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 629 | + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index, |
| 630 | n_rx_packets, n_rx_bytes); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 631 | |
Damjan Marion | 7e0b17d | 2018-11-20 21:07:03 +0100 | [diff] [blame] | 632 | vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 633 | |
Damjan Marion | ba1afaa | 2018-11-22 22:16:19 +0100 | [diff] [blame] | 634 | done: |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 635 | return n_rx_packets; |
| 636 | } |
| 637 | |
| 638 | VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm, |
| 639 | vlib_node_runtime_t * node, |
| 640 | vlib_frame_t * frame) |
| 641 | { |
| 642 | vhost_user_main_t *vum = &vhost_user_main; |
| 643 | uword n_rx_packets = 0; |
| 644 | vhost_user_intf_t *vui; |
| 645 | vnet_device_input_runtime_t *rt = |
| 646 | (vnet_device_input_runtime_t *) node->runtime_data; |
| 647 | vnet_device_and_queue_t *dq; |
| 648 | |
| 649 | vec_foreach (dq, rt->devices_and_queues) |
| 650 | { |
Sirshak Das | 5b718d5 | 2018-10-12 09:38:27 -0500 | [diff] [blame] | 651 | if ((node->state == VLIB_NODE_STATE_POLLING) || |
| 652 | clib_atomic_swap_acq_n (&dq->interrupt_pending, 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 653 | { |
| 654 | vui = |
| 655 | pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance); |
Damjan Marion | befe691 | 2018-11-22 15:45:54 +0100 | [diff] [blame] | 656 | n_rx_packets += vhost_user_if_input (vm, vum, vui, dq->queue_id, node, |
| 657 | dq->mode); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | return n_rx_packets; |
| 662 | } |
| 663 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 664 | /* *INDENT-OFF* */ |
| 665 | VLIB_REGISTER_NODE (vhost_user_input_node) = { |
| 666 | .type = VLIB_NODE_TYPE_INPUT, |
| 667 | .name = "vhost-user-input", |
| 668 | .sibling_of = "device-input", |
| 669 | |
| 670 | /* Will be enabled if/when hardware is detected. */ |
| 671 | .state = VLIB_NODE_STATE_DISABLED, |
| 672 | |
| 673 | .format_buffer = format_ethernet_header_with_length, |
| 674 | .format_trace = format_vhost_trace, |
| 675 | |
| 676 | .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR, |
| 677 | .error_strings = vhost_user_input_func_error_strings, |
| 678 | }; |
| 679 | /* *INDENT-ON* */ |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | * fd.io coding-style-patch-verification: ON |
| 683 | * |
| 684 | * Local Variables: |
| 685 | * eval: (c-set-style "gnu") |
| 686 | * End: |
| 687 | */ |