Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * vhost.c - vhost-user |
| 4 | * |
| 5 | * Copyright (c) 2014 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> |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 39 | #include <vnet/devices/devices.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 40 | #include <vnet/feature/feature.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
| 42 | #include <vnet/devices/virtio/vhost-user.h> |
| 43 | |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 44 | /** |
| 45 | * @file |
| 46 | * @brief vHost User Device Driver. |
| 47 | * |
| 48 | * This file contains the source code for vHost User interface. |
| 49 | */ |
| 50 | |
| 51 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 52 | #define VHOST_DEBUG_VQ 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 54 | #define DBG_SOCK(args...) \ |
| 55 | { \ |
| 56 | vhost_user_main_t *_vum = &vhost_user_main; \ |
| 57 | if (_vum->debug) \ |
| 58 | clib_warning(args); \ |
| 59 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 61 | #if VHOST_DEBUG_VQ == 1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | #define DBG_VQ(args...) clib_warning(args); |
| 63 | #else |
| 64 | #define DBG_VQ(args...) |
| 65 | #endif |
| 66 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 67 | /* |
| 68 | * When an RX queue is down but active, received packets |
| 69 | * must be discarded. This value controls up to how many |
| 70 | * packets will be discarded during each round. |
| 71 | */ |
| 72 | #define VHOST_USER_DOWN_DISCARD_COUNT 256 |
| 73 | |
| 74 | /* |
| 75 | * When the number of available buffers gets under this threshold, |
| 76 | * RX node will start discarding packets. |
| 77 | */ |
| 78 | #define VHOST_USER_RX_BUFFER_STARVATION 32 |
| 79 | |
| 80 | /* |
| 81 | * On the receive side, the host should free descriptors as soon |
| 82 | * as possible in order to avoid TX drop in the VM. |
| 83 | * This value controls the number of copy operations that are stacked |
| 84 | * before copy is done for all and descriptors are given back to |
| 85 | * the guest. |
| 86 | * The value 64 was obtained by testing (48 and 128 were not as good). |
| 87 | */ |
| 88 | #define VHOST_USER_RX_COPY_THRESHOLD 64 |
Steven | d772753 | 2017-06-09 18:49:17 -0700 | [diff] [blame] | 89 | /* |
| 90 | * On the transmit side, we keep processing the buffers from vlib in the while |
| 91 | * loop and prepare the copy order to be executed later. However, the static |
| 92 | * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N |
| 93 | * entries. In order to not corrupt memory, we have to do the copy when the |
| 94 | * static array reaches the copy threshold. We subtract 40 in case the code |
| 95 | * goes into the inner loop for a maximum of 64k frames which may require |
| 96 | * more array entries. |
| 97 | */ |
| 98 | #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 40) |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 99 | |
Haiyang Tan | 2ae5135 | 2018-02-06 11:16:48 -0500 | [diff] [blame] | 100 | #define UNIX_GET_FD(unixfd_idx) ({ \ |
| 101 | typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \ |
| 102 | (__unixfd_idx != ~0) ? \ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 103 | pool_elt_at_index (file_main.file_pool, \ |
Haiyang Tan | 2ae5135 | 2018-02-06 11:16:48 -0500 | [diff] [blame] | 104 | __unixfd_idx)->file_descriptor : -1; }) |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 105 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 106 | #define foreach_virtio_trace_flags \ |
| 107 | _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \ |
| 108 | _ (SINGLE_DESC, 1, "Single descriptor packet") \ |
| 109 | _ (INDIRECT, 2, "Indirect descriptor") \ |
| 110 | _ (MAP_ERROR, 4, "Memory mapping error") |
| 111 | |
| 112 | typedef enum |
| 113 | { |
| 114 | #define _(n,i,s) VIRTIO_TRACE_F_##n, |
| 115 | foreach_virtio_trace_flags |
| 116 | #undef _ |
| 117 | } virtio_trace_flag_t; |
| 118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | vlib_node_registration_t vhost_user_input_node; |
| 120 | |
| 121 | #define foreach_vhost_user_tx_func_error \ |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 122 | _(NONE, "no error") \ |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 123 | _(NOT_READY, "vhost vring not ready") \ |
| 124 | _(DOWN, "vhost interface is down") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \ |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 126 | _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \ |
| 127 | _(MMAP_FAIL, "mmap failure") \ |
| 128 | _(INDIRECT_OVERFLOW, "indirect descriptor table overflow") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 130 | typedef enum |
| 131 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f, |
| 133 | foreach_vhost_user_tx_func_error |
| 134 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 135 | VHOST_USER_TX_FUNC_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | } vhost_user_tx_func_error_t; |
| 137 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 138 | static char *vhost_user_tx_func_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 140 | foreach_vhost_user_tx_func_error |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | #undef _ |
| 142 | }; |
| 143 | |
| 144 | #define foreach_vhost_user_input_func_error \ |
| 145 | _(NO_ERROR, "no error") \ |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 146 | _(NO_BUFFER, "no available buffer") \ |
| 147 | _(MMAP_FAIL, "mmap failure") \ |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 148 | _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \ |
| 149 | _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \ |
| 150 | _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 152 | typedef enum |
| 153 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f, |
| 155 | foreach_vhost_user_input_func_error |
| 156 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 157 | VHOST_USER_INPUT_FUNC_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | } vhost_user_input_func_error_t; |
| 159 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 160 | static char *vhost_user_input_func_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 162 | foreach_vhost_user_input_func_error |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | #undef _ |
| 164 | }; |
| 165 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 166 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | static vhost_user_main_t vhost_user_main = { |
| 168 | .mtu_bytes = 1518, |
| 169 | }; |
| 170 | |
| 171 | VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = { |
| 172 | .name = "vhost-user", |
| 173 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 174 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 176 | static u8 * |
| 177 | format_vhost_user_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | { |
| 179 | u32 i = va_arg (*args, u32); |
| 180 | u32 show_dev_instance = ~0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 181 | vhost_user_main_t *vum = &vhost_user_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
| 183 | if (i < vec_len (vum->show_dev_instance_by_real_dev_instance)) |
| 184 | show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i]; |
| 185 | |
| 186 | if (show_dev_instance != ~0) |
| 187 | i = show_dev_instance; |
| 188 | |
| 189 | s = format (s, "VirtualEthernet0/0/%d", i); |
| 190 | return s; |
| 191 | } |
| 192 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 193 | static int |
| 194 | vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 196 | // FIXME: check if the new dev instance is already used |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 197 | vhost_user_main_t *vum = &vhost_user_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 199 | hi->dev_instance, ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 201 | vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | new_dev_instance; |
| 203 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 204 | DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d", |
| 205 | hi->dev_instance, new_dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 210 | static_always_inline void * |
| 211 | map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | { |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 213 | int i = *hint; |
| 214 | if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) && |
| 215 | ((vui->regions[i].guest_phys_addr + |
| 216 | vui->regions[i].memory_size) > addr))) |
| 217 | { |
| 218 | return (void *) (vui->region_mmap_addr[i] + addr - |
| 219 | vui->regions[i].guest_phys_addr); |
| 220 | } |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 221 | #if __SSE4_2__ |
| 222 | __m128i rl, rh, al, ah, r; |
| 223 | al = _mm_set1_epi64x (addr + 1); |
| 224 | ah = _mm_set1_epi64x (addr); |
| 225 | |
| 226 | rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]); |
| 227 | rl = _mm_cmpgt_epi64 (al, rl); |
| 228 | rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]); |
| 229 | rh = _mm_cmpgt_epi64 (rh, ah); |
| 230 | r = _mm_and_si128 (rl, rh); |
| 231 | |
| 232 | rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]); |
| 233 | rl = _mm_cmpgt_epi64 (al, rl); |
| 234 | rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]); |
| 235 | rh = _mm_cmpgt_epi64 (rh, ah); |
| 236 | r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22); |
| 237 | |
| 238 | rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]); |
| 239 | rl = _mm_cmpgt_epi64 (al, rl); |
| 240 | rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]); |
| 241 | rh = _mm_cmpgt_epi64 (rh, ah); |
| 242 | r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44); |
| 243 | |
| 244 | rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]); |
| 245 | rl = _mm_cmpgt_epi64 (al, rl); |
| 246 | rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]); |
| 247 | rh = _mm_cmpgt_epi64 (rh, ah); |
| 248 | r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88); |
| 249 | |
| 250 | r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800)); |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 251 | i = count_trailing_zeros (_mm_movemask_epi8 (r) | |
| 252 | (1 << VHOST_MEMORY_MAX_NREGIONS)); |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 253 | |
| 254 | if (i < vui->nregions) |
| 255 | { |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 256 | *hint = i; |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 257 | return (void *) (vui->region_mmap_addr[i] + addr - |
| 258 | vui->regions[i].guest_phys_addr); |
| 259 | } |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 260 | #elif __aarch64__ && __ARM_NEON |
| 261 | uint64x2_t al, ah, rl, rh, r; |
| 262 | uint32_t u32 = 0; |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 263 | |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 264 | al = vdupq_n_u64 (addr + 1); |
| 265 | ah = vdupq_n_u64 (addr); |
| 266 | |
| 267 | /*First Iteration */ |
| 268 | rl = vld1q_u64 (&vui->region_guest_addr_lo[0]); |
| 269 | rl = vcgtq_u64 (al, rl); |
| 270 | rh = vld1q_u64 (&vui->region_guest_addr_hi[0]); |
| 271 | rh = vcgtq_u64 (rh, ah); |
| 272 | r = vandq_u64 (rl, rh); |
| 273 | u32 |= (vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1); |
| 274 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 1); |
| 275 | |
| 276 | if (u32) |
| 277 | { |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 278 | i = count_trailing_zeros (u32); |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 279 | goto vhost_map_guest_mem_done; |
| 280 | } |
| 281 | |
| 282 | /*Second Iteration */ |
| 283 | rl = vld1q_u64 (&vui->region_guest_addr_lo[2]); |
| 284 | rl = vcgtq_u64 (al, rl); |
| 285 | rh = vld1q_u64 (&vui->region_guest_addr_hi[2]); |
| 286 | rh = vcgtq_u64 (rh, ah); |
| 287 | r = vandq_u64 (rl, rh); |
| 288 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 2); |
| 289 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 3); |
| 290 | |
| 291 | if (u32) |
| 292 | { |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 293 | i = count_trailing_zeros (u32); |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 294 | goto vhost_map_guest_mem_done; |
| 295 | } |
| 296 | |
| 297 | /*Third Iteration */ |
| 298 | rl = vld1q_u64 (&vui->region_guest_addr_lo[4]); |
| 299 | rl = vcgtq_u64 (al, rl); |
| 300 | rh = vld1q_u64 (&vui->region_guest_addr_hi[4]); |
| 301 | rh = vcgtq_u64 (rh, ah); |
| 302 | r = vandq_u64 (rl, rh); |
| 303 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 4); |
| 304 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 5); |
| 305 | |
| 306 | if (u32) |
| 307 | { |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 308 | i = count_trailing_zeros (u32); |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 309 | goto vhost_map_guest_mem_done; |
| 310 | } |
| 311 | |
| 312 | /*Fourth Iteration */ |
| 313 | rl = vld1q_u64 (&vui->region_guest_addr_lo[6]); |
| 314 | rl = vcgtq_u64 (al, rl); |
| 315 | rh = vld1q_u64 (&vui->region_guest_addr_hi[6]); |
| 316 | rh = vcgtq_u64 (rh, ah); |
| 317 | r = vandq_u64 (rl, rh); |
| 318 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 6); |
| 319 | u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 7); |
| 320 | |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 321 | i = count_trailing_zeros (u32 | (1 << VHOST_MEMORY_MAX_NREGIONS)); |
Nitin Saxena | d3cb7ba | 2018-02-07 11:32:00 +0000 | [diff] [blame] | 322 | |
| 323 | vhost_map_guest_mem_done: |
| 324 | if (i < vui->nregions) |
| 325 | { |
| 326 | *hint = i; |
| 327 | return (void *) (vui->region_mmap_addr[i] + addr - |
| 328 | vui->regions[i].guest_phys_addr); |
| 329 | } |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 330 | #else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 331 | for (i = 0; i < vui->nregions; i++) |
| 332 | { |
| 333 | if ((vui->regions[i].guest_phys_addr <= addr) && |
| 334 | ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > |
| 335 | addr)) |
| 336 | { |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 337 | *hint = i; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 338 | return (void *) (vui->region_mmap_addr[i] + addr - |
| 339 | vui->regions[i].guest_phys_addr); |
| 340 | } |
| 341 | } |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 342 | #endif |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 343 | DBG_VQ ("failed to map guest mem addr %llx", addr); |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 344 | *hint = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 348 | static inline void * |
| 349 | map_user_mem (vhost_user_intf_t * vui, uword addr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | { |
| 351 | int i; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 352 | for (i = 0; i < vui->nregions; i++) |
| 353 | { |
| 354 | if ((vui->regions[i].userspace_addr <= addr) && |
| 355 | ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > |
| 356 | addr)) |
| 357 | { |
| 358 | return (void *) (vui->region_mmap_addr[i] + addr - |
| 359 | vui->regions[i].userspace_addr); |
| 360 | } |
| 361 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 365 | static long |
| 366 | get_huge_page_size (int fd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | { |
| 368 | struct statfs s; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 369 | fstatfs (fd, &s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | return s.f_bsize; |
| 371 | } |
| 372 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 373 | static void |
| 374 | unmap_all_mem_regions (vhost_user_intf_t * vui) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 376 | int i, r; |
| 377 | for (i = 0; i < vui->nregions; i++) |
| 378 | { |
Haiyang Tan | 7b0933a | 2018-01-20 04:48:53 -0500 | [diff] [blame] | 379 | if (vui->region_mmap_addr[i] != MAP_FAILED) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 380 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 382 | long page_sz = get_huge_page_size (vui->region_mmap_fd[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 384 | ssize_t map_sz = (vui->regions[i].memory_size + |
| 385 | vui->regions[i].mmap_offset + |
Pierre Pfister | bed5489 | 2017-04-20 15:34:00 +0200 | [diff] [blame] | 386 | page_sz - 1) & ~(page_sz - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 388 | r = |
| 389 | munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, |
| 390 | map_sz); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 392 | DBG_SOCK |
| 393 | ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i, |
| 394 | vui->region_mmap_addr[i], map_sz, page_sz); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Haiyang Tan | 7b0933a | 2018-01-20 04:48:53 -0500 | [diff] [blame] | 396 | vui->region_mmap_addr[i] = MAP_FAILED; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 398 | if (r == -1) |
| 399 | { |
| 400 | clib_warning ("failed to unmap memory region (errno %d)", |
| 401 | errno); |
| 402 | } |
| 403 | close (vui->region_mmap_fd[i]); |
| 404 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | vui->nregions = 0; |
| 407 | } |
| 408 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 409 | static void |
| 410 | vhost_user_tx_thread_placement (vhost_user_intf_t * vui) |
| 411 | { |
| 412 | //Let's try to assign one queue to each thread |
| 413 | u32 qid = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 414 | u32 thread_index = 0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 415 | vui->use_tx_spinlock = 0; |
| 416 | while (1) |
| 417 | { |
| 418 | for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++) |
| 419 | { |
| 420 | vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; |
| 421 | if (!rxvq->started || !rxvq->enabled) |
| 422 | continue; |
| 423 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 424 | vui->per_cpu_tx_qid[thread_index] = qid; |
| 425 | thread_index++; |
| 426 | if (thread_index == vlib_get_thread_main ()->n_vlib_mains) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | //We need to loop, meaning the spinlock has to be used |
| 430 | vui->use_tx_spinlock = 1; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 431 | if (thread_index == 0) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 432 | { |
| 433 | //Could not find a single valid one |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 434 | for (thread_index = 0; |
| 435 | thread_index < vlib_get_thread_main ()->n_vlib_mains; |
| 436 | thread_index++) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 437 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 438 | vui->per_cpu_tx_qid[thread_index] = 0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 439 | } |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 445 | /** |
| 446 | * @brief Unassign existing interface/queue to thread mappings and re-assign |
| 447 | * new interface/queue to thread mappings |
| 448 | */ |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 449 | static void |
| 450 | vhost_user_rx_thread_placement () |
| 451 | { |
| 452 | vhost_user_main_t *vum = &vhost_user_main; |
| 453 | vhost_user_intf_t *vui; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 454 | vhost_user_vring_t *txvq; |
| 455 | vnet_main_t *vnm = vnet_get_main (); |
| 456 | u32 qid; |
| 457 | int rv; |
| 458 | u16 *queue; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 459 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 460 | // Scrap all existing mappings for all interfaces/queues |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 461 | /* *INDENT-OFF* */ |
| 462 | pool_foreach (vui, vum->vhost_user_interfaces, { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 463 | vec_foreach (queue, vui->rx_queues) |
| 464 | { |
| 465 | rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index, |
| 466 | *queue); |
| 467 | if (rv) |
| 468 | clib_warning ("Warning: unable to unassign interface %d, " |
| 469 | "queue %d: rc=%d", vui->hw_if_index, *queue, rv); |
| 470 | } |
| 471 | vec_reset_length (vui->rx_queues); |
| 472 | }); |
| 473 | /* *INDENT-ON* */ |
| 474 | |
| 475 | // Create the rx_queues for all interfaces |
| 476 | /* *INDENT-OFF* */ |
| 477 | pool_foreach (vui, vum->vhost_user_interfaces, { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 478 | for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++) |
| 479 | { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 480 | txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
| 481 | if (txvq->started) |
| 482 | { |
| 483 | if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN) |
| 484 | /* Set polling as the default */ |
| 485 | txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING; |
| 486 | vec_add1 (vui->rx_queues, qid); |
| 487 | } |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 488 | } |
| 489 | }); |
| 490 | /* *INDENT-ON* */ |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 491 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 492 | // Assign new mappings for all interfaces/queues |
| 493 | /* *INDENT-OFF* */ |
| 494 | pool_foreach (vui, vum->vhost_user_interfaces, { |
| 495 | vnet_hw_interface_set_input_node (vnm, vui->hw_if_index, |
| 496 | vhost_user_input_node.index); |
| 497 | vec_foreach (queue, vui->rx_queues) |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 498 | { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 499 | vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue, |
| 500 | ~0); |
| 501 | txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)]; |
| 502 | rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue, |
| 503 | txvq->mode); |
| 504 | if (rv) |
| 505 | clib_warning ("Warning: unable to set rx mode for interface %d, " |
| 506 | "queue %d: rc=%d", vui->hw_if_index, *queue, rv); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 507 | } |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 508 | }); |
| 509 | /* *INDENT-ON* */ |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** @brief Returns whether at least one TX and one RX vring are enabled */ |
| 513 | int |
| 514 | vhost_user_intf_ready (vhost_user_intf_t * vui) |
| 515 | { |
| 516 | int i, found[2] = { }; //RX + TX |
| 517 | |
| 518 | for (i = 0; i < VHOST_VRING_MAX_N; i++) |
| 519 | if (vui->vrings[i].started && vui->vrings[i].enabled) |
| 520 | found[i & 1] = 1; |
| 521 | |
| 522 | return found[0] && found[1]; |
| 523 | } |
| 524 | |
| 525 | static void |
| 526 | vhost_user_update_iface_state (vhost_user_intf_t * vui) |
| 527 | { |
| 528 | /* if we have pointers to descriptor table, go up */ |
| 529 | int is_up = vhost_user_intf_ready (vui); |
| 530 | if (is_up != vui->is_up) |
| 531 | { |
| 532 | DBG_SOCK ("interface %d %s", vui->sw_if_index, |
| 533 | is_up ? "ready" : "down"); |
| 534 | vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index, |
| 535 | is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : |
| 536 | 0); |
| 537 | vui->is_up = is_up; |
| 538 | } |
| 539 | vhost_user_rx_thread_placement (); |
| 540 | vhost_user_tx_thread_placement (vui); |
| 541 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 543 | static void |
| 544 | vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq) |
| 545 | { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 546 | u32 qid; |
| 547 | vnet_main_t *vnm = vnet_get_main (); |
Steven | e4dcba8 | 2017-04-04 16:56:54 -0700 | [diff] [blame] | 548 | |
| 549 | qid = ifq & 0xff; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 550 | if ((qid & 1) == 0) |
| 551 | /* Only care about the odd number, or TX, virtqueue */ |
Steven | e4dcba8 | 2017-04-04 16:56:54 -0700 | [diff] [blame] | 552 | return; |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 553 | |
| 554 | if (vhost_user_intf_ready (vui)) |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 555 | // qid >> 1 is to convert virtqueue number to vring queue index |
| 556 | vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 559 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 560 | vhost_user_callfd_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 562 | __attribute__ ((unused)) int n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | u8 buff[8]; |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 564 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 565 | n = read (uf->file_descriptor, ((char *) &buff), 8); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 566 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 570 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 571 | vhost_user_kickfd_read_ready (clib_file_t * uf) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 572 | { |
| 573 | __attribute__ ((unused)) int n; |
| 574 | u8 buff[8]; |
| 575 | vhost_user_intf_t *vui = |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 576 | pool_elt_at_index (vhost_user_main.vhost_user_interfaces, |
| 577 | uf->private_data >> 8); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 578 | u32 qid = uf->private_data & 0xff; |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 579 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 580 | n = read (uf->file_descriptor, ((char *) &buff), 8); |
| 581 | DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 582 | if (!vui->vrings[qid].started || |
| 583 | (vhost_user_intf_ready (vui) != vui->is_up)) |
| 584 | { |
Steven | e4dcba8 | 2017-04-04 16:56:54 -0700 | [diff] [blame] | 585 | vlib_worker_thread_barrier_sync (vlib_get_main ()); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 586 | vui->vrings[qid].started = 1; |
| 587 | vhost_user_update_iface_state (vui); |
Steven | e4dcba8 | 2017-04-04 16:56:54 -0700 | [diff] [blame] | 588 | vlib_worker_thread_barrier_release (vlib_get_main ()); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 589 | } |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 590 | |
| 591 | vhost_user_set_interrupt_pending (vui, uf->private_data); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @brief Try once to lock the vring |
| 597 | * @return 0 on success, non-zero on failure. |
| 598 | */ |
| 599 | static inline int |
| 600 | vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid) |
| 601 | { |
| 602 | return __sync_lock_test_and_set (vui->vring_locks[qid], 1); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * @brief Spin until the vring is successfully locked |
| 607 | */ |
| 608 | static inline void |
| 609 | vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid) |
| 610 | { |
| 611 | while (vhost_user_vring_try_lock (vui, qid)) |
| 612 | ; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * @brief Unlock the vring lock |
| 617 | */ |
| 618 | static inline void |
| 619 | vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid) |
| 620 | { |
| 621 | *vui->vring_locks[qid] = 0; |
| 622 | } |
| 623 | |
| 624 | static inline void |
| 625 | vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid) |
| 626 | { |
| 627 | vhost_user_vring_t *vring = &vui->vrings[qid]; |
| 628 | memset (vring, 0, sizeof (*vring)); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 629 | vring->kickfd_idx = ~0; |
| 630 | vring->callfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 631 | vring->errfd = -1; |
| 632 | |
| 633 | /* |
| 634 | * We have a bug with some qemu 2.5, and this may be a fix. |
| 635 | * Feel like interpretation holy text, but this is from vhost-user.txt. |
| 636 | * " |
| 637 | * One queue pair is enabled initially. More queues are enabled |
| 638 | * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE. |
| 639 | * " |
| 640 | * Don't know who's right, but this is what DPDK does. |
| 641 | */ |
| 642 | if (qid == 0 || qid == 1) |
| 643 | vring->enabled = 1; |
| 644 | } |
| 645 | |
| 646 | static inline void |
| 647 | vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid) |
| 648 | { |
| 649 | vhost_user_vring_t *vring = &vui->vrings[qid]; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 650 | if (vring->kickfd_idx != ~0) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 651 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 652 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 653 | vring->kickfd_idx); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 654 | clib_file_del (&file_main, uf); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 655 | vring->kickfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 656 | } |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 657 | if (vring->callfd_idx != ~0) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 658 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 659 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 660 | vring->callfd_idx); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 661 | clib_file_del (&file_main, uf); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 662 | vring->callfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 663 | } |
| 664 | if (vring->errfd != -1) |
Steven | f6dae05 | 2017-03-09 23:49:32 -0800 | [diff] [blame] | 665 | { |
| 666 | close (vring->errfd); |
| 667 | vring->errfd = -1; |
| 668 | } |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 669 | vhost_user_vring_init (vui, qid); |
| 670 | } |
| 671 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 672 | static inline void |
| 673 | vhost_user_if_disconnect (vhost_user_intf_t * vui) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 675 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 676 | int q; |
| 677 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 678 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 679 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 680 | if (vui->clib_file_index != ~0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 681 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 682 | clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); |
| 683 | vui->clib_file_index = ~0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 684 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | vui->is_up = 0; |
Steve Shin | 4448957 | 2016-09-22 12:08:55 -0700 | [diff] [blame] | 687 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 688 | for (q = 0; q < VHOST_VRING_MAX_N; q++) |
| 689 | vhost_user_vring_close (vui, q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 690 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 691 | unmap_all_mem_regions (vui); |
| 692 | DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 695 | #define VHOST_LOG_PAGE 0x1000 |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 696 | static_always_inline void |
| 697 | vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui, |
| 698 | u64 addr, u64 len, u8 is_host_address) |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 699 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 700 | if (PREDICT_TRUE (vui->log_base_addr == 0 |
| 701 | || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL)))) |
| 702 | { |
| 703 | return; |
| 704 | } |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 705 | if (is_host_address) |
| 706 | { |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 707 | addr = pointer_to_uword (map_user_mem (vui, (uword) addr)); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 708 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 709 | if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size)) |
| 710 | { |
| 711 | DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n"); |
| 712 | return; |
| 713 | } |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 714 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 715 | CLIB_MEMORY_BARRIER (); |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 716 | u64 page = addr / VHOST_LOG_PAGE; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 717 | while (page * VHOST_LOG_PAGE < addr + len) |
| 718 | { |
| 719 | ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8; |
| 720 | page++; |
| 721 | } |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 724 | static_always_inline void |
| 725 | vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len) |
| 726 | { |
| 727 | vhost_user_log_dirty_pages_2 (vui, addr, len, 0); |
| 728 | } |
| 729 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 730 | #define vhost_user_log_dirty_ring(vui, vq, member) \ |
Yoann Desmouceaux | fe2da0e | 2016-03-08 14:54:28 +0100 | [diff] [blame] | 731 | if (PREDICT_FALSE(vq->log_used)) { \ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 732 | vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \ |
Yoann Desmouceaux | fe2da0e | 2016-03-08 14:54:28 +0100 | [diff] [blame] | 733 | sizeof(vq->used->member)); \ |
| 734 | } |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 735 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 736 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 737 | vhost_user_socket_read (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | { |
| 739 | int n, i; |
| 740 | int fd, number_of_fds = 0; |
| 741 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
| 742 | vhost_user_msg_t msg; |
| 743 | struct msghdr mh; |
| 744 | struct iovec iov[1]; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 745 | vhost_user_main_t *vum = &vhost_user_main; |
| 746 | vhost_user_intf_t *vui; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | struct cmsghdr *cmsg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 748 | u8 q; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 749 | clib_file_t template = { 0 }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 750 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 751 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 752 | vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 754 | char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 756 | memset (&mh, 0, sizeof (mh)); |
| 757 | memset (control, 0, sizeof (control)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 759 | for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++) |
Damjan Marion | a290d7c | 2016-08-16 12:37:24 +0200 | [diff] [blame] | 760 | fds[i] = -1; |
| 761 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | /* set the payload */ |
| 763 | iov[0].iov_base = (void *) &msg; |
| 764 | iov[0].iov_len = VHOST_USER_MSG_HDR_SZ; |
| 765 | |
| 766 | mh.msg_iov = iov; |
| 767 | mh.msg_iovlen = 1; |
| 768 | mh.msg_control = control; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 769 | mh.msg_controllen = sizeof (control); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 771 | n = recvmsg (uf->file_descriptor, &mh, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 773 | /* Stop workers to avoid end of the world */ |
| 774 | vlib_worker_thread_barrier_sync (vlib_get_main ()); |
| 775 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | if (n != VHOST_USER_MSG_HDR_SZ) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 777 | { |
| 778 | if (n == -1) |
| 779 | { |
| 780 | DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno)); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)", |
| 785 | n, VHOST_USER_MSG_HDR_SZ); |
| 786 | } |
| 787 | goto close_socket; |
| 788 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 790 | if (mh.msg_flags & MSG_CTRUNC) |
| 791 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 792 | DBG_SOCK ("MSG_CTRUNC is set"); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 793 | goto close_socket; |
| 794 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 796 | cmsg = CMSG_FIRSTHDR (&mh); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | |
| 798 | if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) && |
| 799 | (cmsg->cmsg_type == SCM_RIGHTS) && |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 800 | (cmsg->cmsg_len - CMSG_LEN (0) <= |
| 801 | VHOST_MEMORY_MAX_NREGIONS * sizeof (int))) |
| 802 | { |
| 803 | number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int); |
| 804 | clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int)); |
| 805 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 806 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 807 | /* version 1, no reply bit set */ |
| 808 | if ((msg.flags & 7) != 1) |
| 809 | { |
| 810 | DBG_SOCK ("malformed message received. closing socket"); |
| 811 | goto close_socket; |
| 812 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | |
| 814 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 815 | int rv; |
| 816 | rv = |
| 817 | read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ, |
| 818 | msg.size); |
| 819 | if (rv < 0) |
| 820 | { |
| 821 | DBG_SOCK ("read failed %s", strerror (errno)); |
| 822 | goto close_socket; |
| 823 | } |
| 824 | else if (rv != msg.size) |
| 825 | { |
| 826 | DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size); |
| 827 | goto close_socket; |
| 828 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 831 | switch (msg.request) |
| 832 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 833 | case VHOST_USER_GET_FEATURES: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 834 | msg.flags |= 4; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 835 | msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) | |
| 836 | (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) | |
| 837 | (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) | |
| 838 | (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) | |
| 839 | (1ULL << FEAT_VHOST_F_LOG_ALL) | |
| 840 | (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) | |
| 841 | (1ULL << FEAT_VIRTIO_NET_F_MQ) | |
| 842 | (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) | |
| 843 | (1ULL << FEAT_VIRTIO_F_VERSION_1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | msg.u64 &= vui->feature_mask; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 845 | msg.size = sizeof (msg.u64); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 846 | DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx", |
| 847 | vui->hw_if_index, msg.u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 848 | break; |
| 849 | |
| 850 | case VHOST_USER_SET_FEATURES: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 851 | DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx", |
| 852 | vui->hw_if_index, msg.u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 853 | |
| 854 | vui->features = msg.u64; |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 855 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 856 | if (vui->features & |
| 857 | ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) | |
| 858 | (1ULL << FEAT_VIRTIO_F_VERSION_1))) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 859 | vui->virtio_net_hdr_sz = 12; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 861 | vui->virtio_net_hdr_sz = 10; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 862 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 863 | vui->is_any_layout = |
| 864 | (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 865 | |
| 866 | ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 867 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | vui->is_up = 0; |
| 869 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 870 | /*for (q = 0; q < VHOST_VRING_MAX_N; q++) |
| 871 | vhost_user_vring_close(&vui->vrings[q]); */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 872 | |
| 873 | break; |
| 874 | |
| 875 | case VHOST_USER_SET_MEM_TABLE: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 876 | DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d", |
| 877 | vui->hw_if_index, msg.memory.nregions); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 878 | |
| 879 | if ((msg.memory.nregions < 1) || |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 880 | (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) |
| 881 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 883 | DBG_SOCK ("number of mem regions must be between 1 and %i", |
| 884 | VHOST_MEMORY_MAX_NREGIONS); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 885 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 886 | goto close_socket; |
| 887 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 888 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 889 | if (msg.memory.nregions != number_of_fds) |
| 890 | { |
| 891 | DBG_SOCK ("each memory region must have FD"); |
| 892 | goto close_socket; |
| 893 | } |
| 894 | unmap_all_mem_regions (vui); |
| 895 | for (i = 0; i < msg.memory.nregions; i++) |
| 896 | { |
| 897 | clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i], |
| 898 | sizeof (vhost_user_memory_region_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 899 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 900 | long page_sz = get_huge_page_size (fds[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
Haiyang Tan | 7b0933a | 2018-01-20 04:48:53 -0500 | [diff] [blame] | 902 | /* align size to page */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 903 | ssize_t map_sz = (vui->regions[i].memory_size + |
| 904 | vui->regions[i].mmap_offset + |
Pierre Pfister | bed5489 | 2017-04-20 15:34:00 +0200 | [diff] [blame] | 905 | page_sz - 1) & ~(page_sz - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 906 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 907 | vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE, |
| 908 | MAP_SHARED, fds[i], 0); |
Damjan Marion | 3762370 | 2016-09-20 11:25:27 +0200 | [diff] [blame] | 909 | vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr; |
| 910 | vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr + |
| 911 | vui->regions[i].memory_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 913 | DBG_SOCK |
| 914 | ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx " |
| 915 | "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i], |
| 916 | page_sz); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 918 | if (vui->region_mmap_addr[i] == MAP_FAILED) |
| 919 | { |
| 920 | clib_warning ("failed to map memory. errno is %d", errno); |
| 921 | goto close_socket; |
| 922 | } |
| 923 | vui->region_mmap_addr[i] += vui->regions[i].mmap_offset; |
| 924 | vui->region_mmap_fd[i] = fds[i]; |
Haiyang Tan | 352ecd9 | 2018-01-20 04:01:28 -0500 | [diff] [blame] | 925 | |
| 926 | vui->nregions++; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 927 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 928 | break; |
| 929 | |
| 930 | case VHOST_USER_SET_VRING_NUM: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 931 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d", |
| 932 | vui->hw_if_index, msg.state.index, msg.state.num); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 933 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 934 | if ((msg.state.num > 32768) || /* maximum ring size is 32768 */ |
| 935 | (msg.state.num == 0) || /* it cannot be zero */ |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 936 | ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 937 | goto close_socket; |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 938 | vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | break; |
| 940 | |
| 941 | case VHOST_USER_SET_VRING_ADDR: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 942 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d", |
| 943 | vui->hw_if_index, msg.state.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 945 | if (msg.state.index >= VHOST_VRING_MAX_N) |
| 946 | { |
| 947 | DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:" |
| 948 | " %d >= %d", msg.state.index, VHOST_VRING_MAX_N); |
| 949 | goto close_socket; |
| 950 | } |
| 951 | |
| 952 | if (msg.size < sizeof (msg.addr)) |
| 953 | { |
| 954 | DBG_SOCK ("vhost message is too short (%d < %d)", |
| 955 | msg.size, sizeof (msg.addr)); |
| 956 | goto close_socket; |
| 957 | } |
| 958 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 959 | vui->vrings[msg.state.index].desc = (vring_desc_t *) |
| 960 | map_user_mem (vui, msg.addr.desc_user_addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 961 | vui->vrings[msg.state.index].used = (vring_used_t *) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 962 | map_user_mem (vui, msg.addr.used_user_addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 963 | vui->vrings[msg.state.index].avail = (vring_avail_t *) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 964 | map_user_mem (vui, msg.addr.avail_user_addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 965 | |
| 966 | if ((vui->vrings[msg.state.index].desc == NULL) || |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 967 | (vui->vrings[msg.state.index].used == NULL) || |
| 968 | (vui->vrings[msg.state.index].avail == NULL)) |
| 969 | { |
| 970 | DBG_SOCK ("failed to map user memory for hw_if_index %d", |
| 971 | vui->hw_if_index); |
| 972 | goto close_socket; |
| 973 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 975 | vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr; |
Yoann Desmouceaux | fe2da0e | 2016-03-08 14:54:28 +0100 | [diff] [blame] | 976 | vui->vrings[msg.state.index].log_used = |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 977 | (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0; |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 978 | |
| 979 | /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 980 | the ring is initialized in an enabled state. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 981 | if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES))) |
| 982 | { |
| 983 | vui->vrings[msg.state.index].enabled = 1; |
| 984 | } |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 985 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 986 | vui->vrings[msg.state.index].last_used_idx = |
Damjan Marion | 10eb1ea | 2016-10-13 10:02:19 +0200 | [diff] [blame] | 987 | vui->vrings[msg.state.index].last_avail_idx = |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 988 | vui->vrings[msg.state.index].used->idx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 989 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 990 | /* tell driver that we don't want interrupts */ |
| 991 | vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 992 | break; |
| 993 | |
| 994 | case VHOST_USER_SET_OWNER: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 995 | DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 996 | break; |
| 997 | |
| 998 | case VHOST_USER_RESET_OWNER: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 999 | DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1000 | break; |
| 1001 | |
| 1002 | case VHOST_USER_SET_VRING_CALL: |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1003 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL %d", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1004 | vui->hw_if_index, msg.u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1005 | |
| 1006 | q = (u8) (msg.u64 & 0xFF); |
| 1007 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1008 | /* if there is old fd, delete and close it */ |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1009 | if (vui->vrings[q].callfd_idx != ~0) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1010 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1011 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1012 | vui->vrings[q].callfd_idx); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1013 | clib_file_del (&file_main, uf); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1014 | vui->vrings[q].callfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Steven | 49a04b9 | 2017-07-29 08:56:08 -0700 | [diff] [blame] | 1017 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1018 | { |
| 1019 | if (number_of_fds != 1) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1020 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1021 | DBG_SOCK ("More than one fd received !"); |
| 1022 | goto close_socket; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1023 | } |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1024 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1025 | template.read_function = vhost_user_callfd_read_ready; |
| 1026 | template.file_descriptor = fds[0]; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1027 | template.private_data = |
| 1028 | ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1029 | vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1030 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1031 | else |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1032 | vui->vrings[q].callfd_idx = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1033 | break; |
| 1034 | |
| 1035 | case VHOST_USER_SET_VRING_KICK: |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1036 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK %d", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1037 | vui->hw_if_index, msg.u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1038 | |
| 1039 | q = (u8) (msg.u64 & 0xFF); |
| 1040 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1041 | if (vui->vrings[q].kickfd_idx != ~0) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1042 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1043 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1044 | vui->vrings[q].kickfd_idx); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1045 | clib_file_del (&file_main, uf); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1046 | vui->vrings[q].kickfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Steven | 49a04b9 | 2017-07-29 08:56:08 -0700 | [diff] [blame] | 1049 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1050 | { |
| 1051 | if (number_of_fds != 1) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1052 | { |
| 1053 | DBG_SOCK ("More than one fd received !"); |
| 1054 | goto close_socket; |
| 1055 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1056 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1057 | template.read_function = vhost_user_kickfd_read_ready; |
| 1058 | template.file_descriptor = fds[0]; |
| 1059 | template.private_data = |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1060 | (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) + |
| 1061 | q; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1062 | vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1063 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1064 | else |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1065 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1066 | //When no kickfd is set, the queue is initialized as started |
| 1067 | vui->vrings[q].kickfd_idx = ~0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1068 | vui->vrings[q].started = 1; |
| 1069 | } |
| 1070 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1071 | break; |
| 1072 | |
| 1073 | case VHOST_USER_SET_VRING_ERR: |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1074 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR %d", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1075 | vui->hw_if_index, msg.u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1076 | |
| 1077 | q = (u8) (msg.u64 & 0xFF); |
| 1078 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1079 | if (vui->vrings[q].errfd != -1) |
| 1080 | close (vui->vrings[q].errfd); |
| 1081 | |
Steven | 49a04b9 | 2017-07-29 08:56:08 -0700 | [diff] [blame] | 1082 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1083 | { |
| 1084 | if (number_of_fds != 1) |
| 1085 | goto close_socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1086 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1087 | vui->vrings[q].errfd = fds[0]; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1088 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1089 | else |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1090 | vui->vrings[q].errfd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1091 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1092 | break; |
| 1093 | |
| 1094 | case VHOST_USER_SET_VRING_BASE: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1095 | DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d", |
| 1096 | vui->hw_if_index, msg.state.index, msg.state.num); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1097 | |
| 1098 | vui->vrings[msg.state.index].last_avail_idx = msg.state.num; |
| 1099 | break; |
| 1100 | |
| 1101 | case VHOST_USER_GET_VRING_BASE: |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1102 | if (msg.state.index >= VHOST_VRING_MAX_N) |
| 1103 | { |
| 1104 | DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:" |
| 1105 | " %d >= %d", msg.state.index, VHOST_VRING_MAX_N); |
| 1106 | goto close_socket; |
| 1107 | } |
| 1108 | |
Steven | f6dae05 | 2017-03-09 23:49:32 -0800 | [diff] [blame] | 1109 | /* |
| 1110 | * Copy last_avail_idx from the vring before closing it because |
| 1111 | * closing the vring also initializes the vring last_avail_idx |
| 1112 | */ |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1113 | msg.state.num = vui->vrings[msg.state.index].last_avail_idx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1114 | msg.flags |= 4; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1115 | msg.size = sizeof (msg.state); |
Steven | f6dae05 | 2017-03-09 23:49:32 -0800 | [diff] [blame] | 1116 | |
| 1117 | /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */ |
| 1118 | vhost_user_vring_close (vui, msg.state.index); |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1119 | DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d", |
| 1120 | vui->hw_if_index, msg.state.index, msg.state.num); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1121 | break; |
| 1122 | |
| 1123 | case VHOST_USER_NONE: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1124 | DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1125 | |
| 1126 | break; |
| 1127 | |
| 1128 | case VHOST_USER_SET_LOG_BASE: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1129 | { |
| 1130 | DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1131 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1132 | if (msg.size != sizeof (msg.log)) |
| 1133 | { |
| 1134 | DBG_SOCK |
| 1135 | ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d", |
| 1136 | msg.size, sizeof (msg.log)); |
| 1137 | goto close_socket; |
| 1138 | } |
| 1139 | |
| 1140 | if (! |
| 1141 | (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) |
| 1142 | { |
| 1143 | DBG_SOCK |
| 1144 | ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received"); |
| 1145 | goto close_socket; |
| 1146 | } |
| 1147 | |
| 1148 | fd = fds[0]; |
Haiyang Tan | 7b0933a | 2018-01-20 04:48:53 -0500 | [diff] [blame] | 1149 | /* align size to page */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1150 | long page_sz = get_huge_page_size (fd); |
| 1151 | ssize_t map_sz = |
Pierre Pfister | bed5489 | 2017-04-20 15:34:00 +0200 | [diff] [blame] | 1152 | (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1153 | |
| 1154 | vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE, |
| 1155 | MAP_SHARED, fd, 0); |
| 1156 | |
| 1157 | DBG_SOCK |
| 1158 | ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx", |
| 1159 | map_sz, msg.log.offset, fd, vui->log_base_addr); |
| 1160 | |
| 1161 | if (vui->log_base_addr == MAP_FAILED) |
| 1162 | { |
| 1163 | clib_warning ("failed to map memory. errno is %d", errno); |
| 1164 | goto close_socket; |
| 1165 | } |
| 1166 | |
| 1167 | vui->log_base_addr += msg.log.offset; |
| 1168 | vui->log_size = msg.log.size; |
| 1169 | |
| 1170 | msg.flags |= 4; |
| 1171 | msg.size = sizeof (msg.u64); |
| 1172 | |
| 1173 | break; |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1176 | case VHOST_USER_SET_LOG_FD: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1177 | DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1178 | |
| 1179 | break; |
| 1180 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1181 | case VHOST_USER_GET_PROTOCOL_FEATURES: |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1182 | msg.flags |= 4; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1183 | msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | |
| 1184 | (1 << VHOST_USER_PROTOCOL_F_MQ); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1185 | msg.size = sizeof (msg.u64); |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1186 | DBG_SOCK |
| 1187 | ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - reply 0x%016llx", |
| 1188 | vui->hw_if_index, msg.u64); |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | case VHOST_USER_SET_PROTOCOL_FEATURES: |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1192 | DBG_SOCK |
| 1193 | ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%016llx", |
| 1194 | vui->hw_if_index, msg.u64); |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1195 | |
| 1196 | vui->protocol_features = msg.u64; |
| 1197 | |
| 1198 | break; |
| 1199 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1200 | case VHOST_USER_GET_QUEUE_NUM: |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1201 | msg.flags |= 4; |
| 1202 | msg.u64 = VHOST_VRING_MAX_N; |
| 1203 | msg.size = sizeof (msg.u64); |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 1204 | DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d", |
| 1205 | vui->hw_if_index, msg.u64); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1206 | break; |
| 1207 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1208 | case VHOST_USER_SET_VRING_ENABLE: |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1209 | DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d", |
| 1210 | vui->hw_if_index, msg.state.num ? "enable" : "disable", |
| 1211 | msg.state.index); |
| 1212 | if (msg.state.index >= VHOST_VRING_MAX_N) |
| 1213 | { |
| 1214 | DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:" |
| 1215 | " %d >= %d", msg.state.index, VHOST_VRING_MAX_N); |
| 1216 | goto close_socket; |
| 1217 | } |
| 1218 | |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1219 | vui->vrings[msg.state.index].enabled = msg.state.num; |
| 1220 | break; |
| 1221 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1222 | default: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1223 | DBG_SOCK ("unknown vhost-user message %d received. closing socket", |
| 1224 | msg.request); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1225 | goto close_socket; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1226 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1227 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1228 | /* if we need to reply */ |
| 1229 | if (msg.flags & 4) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1230 | { |
| 1231 | n = |
| 1232 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1233 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1234 | { |
| 1235 | DBG_SOCK ("could not send message response"); |
| 1236 | goto close_socket; |
| 1237 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1238 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1240 | vhost_user_update_iface_state (vui); |
| 1241 | vlib_worker_thread_barrier_release (vlib_get_main ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1242 | return 0; |
| 1243 | |
| 1244 | close_socket: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1245 | vhost_user_if_disconnect (vui); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1246 | vhost_user_update_iface_state (vui); |
| 1247 | vlib_worker_thread_barrier_release (vlib_get_main ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1251 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1252 | vhost_user_socket_error (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1253 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1254 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1255 | vhost_user_main_t *vum = &vhost_user_main; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1256 | vhost_user_intf_t *vui = |
| 1257 | pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1258 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1259 | DBG_SOCK ("socket error on if %d", vui->sw_if_index); |
| 1260 | vlib_worker_thread_barrier_sync (vm); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1261 | vhost_user_if_disconnect (vui); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1262 | vhost_user_rx_thread_placement (); |
| 1263 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1267 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1268 | vhost_user_socksvr_accept_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1269 | { |
| 1270 | int client_fd, client_len; |
| 1271 | struct sockaddr_un client; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1272 | clib_file_t template = { 0 }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1273 | vhost_user_main_t *vum = &vhost_user_main; |
| 1274 | vhost_user_intf_t *vui; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1275 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1276 | vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1277 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1278 | client_len = sizeof (client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1279 | client_fd = accept (uf->file_descriptor, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1280 | (struct sockaddr *) &client, |
| 1281 | (socklen_t *) & client_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (client_fd < 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1284 | return clib_error_return_unix (0, "accept"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1285 | |
Haiyang Tan | 2ae5135 | 2018-02-06 11:16:48 -0500 | [diff] [blame] | 1286 | if (vui->clib_file_index != ~0) |
| 1287 | { |
| 1288 | DBG_SOCK ("Close client socket for vhost interface %d, fd %d", |
| 1289 | vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index)); |
| 1290 | clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); |
| 1291 | } |
| 1292 | |
| 1293 | DBG_SOCK ("New client socket for vhost interface %d, fd %d", |
| 1294 | vui->sw_if_index, client_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1295 | template.read_function = vhost_user_socket_read; |
| 1296 | template.error_function = vhost_user_socket_error; |
| 1297 | template.file_descriptor = client_fd; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1298 | template.private_data = vui - vhost_user_main.vhost_user_interfaces; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1299 | vui->clib_file_index = clib_file_add (&file_main, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static clib_error_t * |
| 1304 | vhost_user_init (vlib_main_t * vm) |
| 1305 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1306 | clib_error_t *error; |
| 1307 | vhost_user_main_t *vum = &vhost_user_main; |
| 1308 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1309 | |
| 1310 | error = vlib_call_init_function (vm, ip4_init); |
| 1311 | if (error) |
| 1312 | return error; |
| 1313 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1314 | vum->coalesce_frames = 32; |
| 1315 | vum->coalesce_time = 1e-3; |
| 1316 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1317 | vec_validate (vum->cpus, tm->n_vlib_mains - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1318 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1319 | vhost_cpu_t *cpu; |
| 1320 | vec_foreach (cpu, vum->cpus) |
| 1321 | { |
| 1322 | /* This is actually not necessary as validate already zeroes it |
| 1323 | * Just keeping the loop here for later because I am lazy. */ |
| 1324 | cpu->rx_buffers_len = 0; |
| 1325 | } |
| 1326 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1327 | vum->random = random_default_seed (); |
| 1328 | |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 1329 | mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword)); |
| 1330 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | VLIB_INIT_FUNCTION (vhost_user_init); |
| 1335 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1336 | static u8 * |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1337 | format_vhost_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1338 | { |
| 1339 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 1340 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1341 | CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main (); |
| 1342 | vhost_user_main_t *vum = &vhost_user_main; |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1343 | vhost_trace_t *t = va_arg (*va, vhost_trace_t *); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1344 | vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces, |
| 1345 | t->device_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1346 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1347 | vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1348 | |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 1349 | u32 indent = format_get_indent (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1350 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1351 | s = format (s, "%U %U queue %d\n", format_white_space, indent, |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1352 | format_vnet_sw_interface_name, vnm, sw, t->qid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1353 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1354 | s = format (s, "%U virtio flags:\n", format_white_space, indent); |
| 1355 | #define _(n,i,st) \ |
| 1356 | if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \ |
| 1357 | s = format (s, "%U %s %s\n", format_white_space, indent, #n, st); |
| 1358 | foreach_virtio_trace_flags |
| 1359 | #undef _ |
| 1360 | s = format (s, "%U virtio_net_hdr first_desc_len %u\n", |
| 1361 | format_white_space, indent, t->first_desc_len); |
| 1362 | |
| 1363 | s = format (s, "%U flags 0x%02x gso_type %u\n", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1364 | format_white_space, indent, |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1365 | t->hdr.hdr.flags, t->hdr.hdr.gso_type); |
| 1366 | |
| 1367 | if (vui->virtio_net_hdr_sz == 12) |
| 1368 | s = format (s, "%U num_buff %u", |
| 1369 | format_white_space, indent, t->hdr.num_buffers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1370 | |
| 1371 | return s; |
| 1372 | } |
| 1373 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1374 | void |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1375 | vhost_user_rx_trace (vhost_trace_t * t, |
| 1376 | vhost_user_intf_t * vui, u16 qid, |
| 1377 | vlib_buffer_t * b, vhost_user_vring_t * txvq) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1378 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1379 | vhost_user_main_t *vum = &vhost_user_main; |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1380 | u32 last_avail_idx = txvq->last_avail_idx; |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1381 | u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask]; |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1382 | vring_desc_t *hdr_desc = 0; |
| 1383 | virtio_net_hdr_mrg_rxbuf_t *hdr; |
| 1384 | u32 hint = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1385 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1386 | memset (t, 0, sizeof (*t)); |
| 1387 | t->device_index = vui - vum->vhost_user_interfaces; |
| 1388 | t->qid = qid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1389 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1390 | hdr_desc = &txvq->desc[desc_current]; |
| 1391 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1392 | { |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1393 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1394 | /* Header is the first here */ |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1395 | hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint); |
| 1396 | } |
| 1397 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) |
| 1398 | { |
| 1399 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; |
| 1400 | } |
| 1401 | if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) && |
| 1402 | !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)) |
| 1403 | { |
| 1404 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; |
| 1405 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1406 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1407 | t->first_desc_len = hdr_desc ? hdr_desc->len : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1408 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1409 | if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) |
| 1410 | { |
| 1411 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; |
| 1412 | } |
| 1413 | else |
| 1414 | { |
| 1415 | u32 len = vui->virtio_net_hdr_sz; |
| 1416 | memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1417 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1420 | static inline void |
| 1421 | vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1422 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1423 | vhost_user_main_t *vum = &vhost_user_main; |
| 1424 | u64 x = 1; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1425 | int fd = UNIX_GET_FD (vq->callfd_idx); |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1426 | int rv; |
| 1427 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 1428 | rv = write (fd, &x, sizeof (x)); |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1429 | if (rv <= 0) |
| 1430 | { |
| 1431 | clib_unix_warning |
| 1432 | ("Error: Could not write to unix socket for callfd %d", fd); |
| 1433 | return; |
| 1434 | } |
| 1435 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1436 | vq->n_since_last_int = 0; |
| 1437 | vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1440 | static_always_inline u32 |
| 1441 | vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, |
| 1442 | u16 copy_len, u32 * map_hint) |
| 1443 | { |
| 1444 | void *src0, *src1, *src2, *src3; |
| 1445 | if (PREDICT_TRUE (copy_len >= 4)) |
| 1446 | { |
| 1447 | if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint)))) |
| 1448 | return 1; |
| 1449 | if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint)))) |
| 1450 | return 1; |
| 1451 | |
| 1452 | while (PREDICT_TRUE (copy_len >= 4)) |
| 1453 | { |
| 1454 | src0 = src2; |
| 1455 | src1 = src3; |
| 1456 | |
| 1457 | if (PREDICT_FALSE |
| 1458 | (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint)))) |
| 1459 | return 1; |
| 1460 | if (PREDICT_FALSE |
| 1461 | (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint)))) |
| 1462 | return 1; |
| 1463 | |
| 1464 | CLIB_PREFETCH (src2, 64, LOAD); |
| 1465 | CLIB_PREFETCH (src3, 64, LOAD); |
| 1466 | |
| 1467 | clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len); |
| 1468 | clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len); |
| 1469 | copy_len -= 2; |
| 1470 | cpy += 2; |
| 1471 | } |
| 1472 | } |
| 1473 | while (copy_len) |
| 1474 | { |
| 1475 | if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) |
| 1476 | return 1; |
| 1477 | clib_memcpy ((void *) cpy->dst, src0, cpy->len); |
| 1478 | copy_len -= 1; |
| 1479 | cpy += 1; |
| 1480 | } |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * Try to discard packets from the tx ring (VPP RX path). |
| 1486 | * Returns the number of discarded packets. |
| 1487 | */ |
| 1488 | u32 |
| 1489 | vhost_user_rx_discard_packet (vlib_main_t * vm, |
| 1490 | vhost_user_intf_t * vui, |
| 1491 | vhost_user_vring_t * txvq, u32 discard_max) |
| 1492 | { |
| 1493 | /* |
| 1494 | * On the RX side, each packet corresponds to one descriptor |
| 1495 | * (it is the same whether it is a shallow descriptor, chained, or indirect). |
| 1496 | * Therefore, discarding a packet is like discarding a descriptor. |
| 1497 | */ |
| 1498 | u32 discarded_packets = 0; |
| 1499 | u32 avail_idx = txvq->avail->idx; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1500 | while (discarded_packets != discard_max) |
| 1501 | { |
| 1502 | if (avail_idx == txvq->last_avail_idx) |
| 1503 | goto out; |
| 1504 | |
| 1505 | u16 desc_chain_head = |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1506 | txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1507 | txvq->last_avail_idx++; |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1508 | txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id = |
| 1509 | desc_chain_head; |
| 1510 | txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1511 | vhost_user_log_dirty_ring (vui, txvq, |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1512 | ring[txvq->last_used_idx & txvq->qsz_mask]); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1513 | txvq->last_used_idx++; |
| 1514 | discarded_packets++; |
| 1515 | } |
| 1516 | |
| 1517 | out: |
| 1518 | CLIB_MEMORY_BARRIER (); |
| 1519 | txvq->used->idx = txvq->last_used_idx; |
| 1520 | vhost_user_log_dirty_ring (vui, txvq, idx); |
| 1521 | return discarded_packets; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * In case of overflow, we need to rewind the array of allocated buffers. |
| 1526 | */ |
| 1527 | static void |
| 1528 | vhost_user_input_rewind_buffers (vlib_main_t * vm, |
| 1529 | vhost_cpu_t * cpu, vlib_buffer_t * b_head) |
| 1530 | { |
| 1531 | u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 1532 | vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current); |
| 1533 | b_current->current_length = 0; |
| 1534 | b_current->flags = 0; |
| 1535 | while (b_current != b_head) |
| 1536 | { |
| 1537 | cpu->rx_buffers_len++; |
| 1538 | bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; |
| 1539 | b_current = vlib_get_buffer (vm, bi_current); |
| 1540 | b_current->current_length = 0; |
| 1541 | b_current->flags = 0; |
| 1542 | } |
Steven | 95827e4 | 2017-05-18 21:22:00 -0700 | [diff] [blame] | 1543 | cpu->rx_buffers_len++; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1544 | } |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 1545 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1546 | static u32 |
| 1547 | vhost_user_if_input (vlib_main_t * vm, |
| 1548 | vhost_user_main_t * vum, |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1549 | vhost_user_intf_t * vui, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1550 | u16 qid, vlib_node_runtime_t * node, |
| 1551 | vnet_hw_interface_rx_mode mode) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1552 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1553 | vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1554 | u16 n_rx_packets = 0; |
| 1555 | u32 n_rx_bytes = 0; |
| 1556 | u16 n_left; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1557 | u32 n_left_to_next, *to_next; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1558 | u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 1559 | u32 n_trace = vlib_get_trace_count (vm, node); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1560 | u32 map_hint = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1561 | u16 thread_index = vlib_get_thread_index (); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1562 | u16 copy_len = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1563 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1564 | { |
| 1565 | /* do we have pending interrupts ? */ |
| 1566 | vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; |
| 1567 | f64 now = vlib_time_now (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1568 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1569 | if ((txvq->n_since_last_int) && (txvq->int_deadline < now)) |
| 1570 | vhost_user_send_call (vm, txvq); |
| 1571 | |
| 1572 | if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now)) |
| 1573 | vhost_user_send_call (vm, rxvq); |
| 1574 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1575 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1576 | /* |
| 1577 | * For adaptive mode, it is optimized to reduce interrupts. |
| 1578 | * If the scheduler switches the input node to polling due |
| 1579 | * to burst of traffic, we tell the driver no interrupt. |
| 1580 | * When the traffic subsides, the scheduler switches the node back to |
| 1581 | * interrupt mode. We must tell the driver we want interrupt. |
| 1582 | */ |
| 1583 | if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) |
| 1584 | { |
| 1585 | if ((node->flags & |
| 1586 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || |
| 1587 | !(node->flags & |
| 1588 | VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) |
| 1589 | /* Tell driver we want notification */ |
| 1590 | txvq->used->flags = 0; |
| 1591 | else |
| 1592 | /* Tell driver we don't want notification */ |
| 1593 | txvq->used->flags = VRING_USED_F_NO_NOTIFY; |
| 1594 | } |
| 1595 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1596 | if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1597 | return 0; |
| 1598 | |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1599 | n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx); |
| 1600 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1601 | /* nothing to do */ |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1602 | if (PREDICT_FALSE (n_left == 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1603 | return 0; |
| 1604 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1605 | if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled))) |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1606 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1607 | /* |
| 1608 | * Discard input packet if interface is admin down or vring is not |
| 1609 | * enabled. |
| 1610 | * "For example, for a networking device, in the disabled state |
| 1611 | * client must not supply any new RX packets, but must process |
| 1612 | * and discard any TX packets." |
| 1613 | */ |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1614 | vhost_user_rx_discard_packet (vm, vui, txvq, |
| 1615 | VHOST_USER_DOWN_DISCARD_COUNT); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1618 | |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1619 | if (PREDICT_FALSE (n_left == (txvq->qsz_mask + 1))) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1620 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1621 | /* |
| 1622 | * Informational error logging when VPP is not |
| 1623 | * receiving packets fast enough. |
| 1624 | */ |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1625 | vlib_error_count (vm, node->node_index, |
| 1626 | VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1); |
| 1627 | } |
| 1628 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1629 | if (n_left > VLIB_FRAME_SIZE) |
| 1630 | n_left = VLIB_FRAME_SIZE; |
| 1631 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1632 | /* |
| 1633 | * For small packets (<2kB), we will not need more than one vlib buffer |
| 1634 | * per packet. In case packets are bigger, we will just yeld at some point |
| 1635 | * in the loop and come back later. This is not an issue as for big packet, |
| 1636 | * processing cost really comes from the memory copy. |
Pierre Pfister | 56a8684 | 2017-10-05 14:24:05 +0200 | [diff] [blame] | 1637 | * The assumption is that big packets will fit in 40 buffers. |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1638 | */ |
Pierre Pfister | 56a8684 | 2017-10-05 14:24:05 +0200 | [diff] [blame] | 1639 | if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 || |
| 1640 | vum->cpus[thread_index].rx_buffers_len < 40)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1641 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1642 | u32 curr_len = vum->cpus[thread_index].rx_buffers_len; |
| 1643 | vum->cpus[thread_index].rx_buffers_len += |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1644 | vlib_buffer_alloc_from_free_list (vm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1645 | vum->cpus[thread_index].rx_buffers + |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1646 | curr_len, |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1647 | VHOST_USER_RX_BUFFERS_N - curr_len, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1648 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1649 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1650 | if (PREDICT_FALSE |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1651 | (vum->cpus[thread_index].rx_buffers_len < |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1652 | VHOST_USER_RX_BUFFER_STARVATION)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1653 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1654 | /* In case of buffer starvation, discard some packets from the queue |
| 1655 | * and log the event. |
| 1656 | * We keep doing best effort for the remaining packets. */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1657 | u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ? |
| 1658 | n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1659 | flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush); |
| 1660 | |
| 1661 | n_left -= flush; |
| 1662 | vlib_increment_simple_counter (vnet_main. |
| 1663 | interface_main.sw_if_counters + |
| 1664 | VNET_INTERFACE_COUNTER_DROP, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1665 | vlib_get_thread_index (), |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1666 | vui->sw_if_index, flush); |
| 1667 | |
| 1668 | vlib_error_count (vm, vhost_user_input_node.index, |
| 1669 | VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1670 | } |
| 1671 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1672 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1673 | while (n_left > 0) |
| 1674 | { |
| 1675 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1676 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1677 | while (n_left > 0 && n_left_to_next > 0) |
| 1678 | { |
| 1679 | vlib_buffer_t *b_head, *b_current; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1680 | u32 bi_current; |
| 1681 | u16 desc_current; |
| 1682 | u32 desc_data_offset; |
| 1683 | vring_desc_t *desc_table = txvq->desc; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1684 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1685 | if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1)) |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1686 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1687 | /* Not enough rx_buffers |
| 1688 | * Note: We yeld on 1 so we don't need to do an additional |
| 1689 | * check for the next buffer prefetch. |
| 1690 | */ |
| 1691 | n_left = 0; |
| 1692 | break; |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1695 | desc_current = |
| 1696 | txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask]; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1697 | vum->cpus[thread_index].rx_buffers_len--; |
| 1698 | bi_current = (vum->cpus[thread_index].rx_buffers) |
| 1699 | [vum->cpus[thread_index].rx_buffers_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1700 | b_head = b_current = vlib_get_buffer (vm, bi_current); |
| 1701 | to_next[0] = bi_current; //We do that now so we can forget about bi_current |
| 1702 | to_next++; |
| 1703 | n_left_to_next--; |
| 1704 | |
| 1705 | vlib_prefetch_buffer_with_index (vm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1706 | (vum-> |
| 1707 | cpus[thread_index].rx_buffers) |
| 1708 | [vum->cpus[thread_index]. |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1709 | rx_buffers_len - 1], LOAD); |
| 1710 | |
| 1711 | /* Just preset the used descriptor id and length for later */ |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1712 | txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id = |
| 1713 | desc_current; |
| 1714 | txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1715 | vhost_user_log_dirty_ring (vui, txvq, |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1716 | ring[txvq->last_used_idx & |
| 1717 | txvq->qsz_mask]); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1718 | |
| 1719 | /* The buffer should already be initialized */ |
| 1720 | b_head->total_length_not_including_first_buffer = 0; |
| 1721 | b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1722 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1723 | if (PREDICT_FALSE (n_trace)) |
| 1724 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1725 | //TODO: next_index is not exactly known at that point |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1726 | vlib_trace_buffer (vm, node, next_index, b_head, |
| 1727 | /* follow_chain */ 0); |
| 1728 | vhost_trace_t *t0 = |
| 1729 | vlib_add_trace (vm, node, b_head, sizeof (t0[0])); |
| 1730 | vhost_user_rx_trace (t0, vui, qid, b_head, txvq); |
| 1731 | n_trace--; |
| 1732 | vlib_set_trace_count (vm, node, n_trace); |
| 1733 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1734 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1735 | /* This depends on the setup but is very consistent |
| 1736 | * So I think the CPU branch predictor will make a pretty good job |
| 1737 | * at optimizing the decision. */ |
| 1738 | if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT) |
| 1739 | { |
| 1740 | desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr, |
| 1741 | &map_hint); |
| 1742 | desc_current = 0; |
| 1743 | if (PREDICT_FALSE (desc_table == 0)) |
| 1744 | { |
Steven | 95827e4 | 2017-05-18 21:22:00 -0700 | [diff] [blame] | 1745 | vlib_error_count (vm, node->node_index, |
| 1746 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1747 | goto out; |
| 1748 | } |
| 1749 | } |
| 1750 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1751 | if (PREDICT_TRUE (vui->is_any_layout) || |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1752 | (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT))) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1753 | { |
| 1754 | /* ANYLAYOUT or single buffer */ |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1755 | desc_data_offset = vui->virtio_net_hdr_sz; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1756 | } |
| 1757 | else |
| 1758 | { |
| 1759 | /* CSR case without ANYLAYOUT, skip 1st buffer */ |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1760 | desc_data_offset = desc_table[desc_current].len; |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1763 | while (1) |
| 1764 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1765 | /* Get more input if necessary. Or end of packet. */ |
| 1766 | if (desc_data_offset == desc_table[desc_current].len) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1767 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1768 | if (PREDICT_FALSE (desc_table[desc_current].flags & |
| 1769 | VIRTQ_DESC_F_NEXT)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1770 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1771 | desc_current = desc_table[desc_current].next; |
| 1772 | desc_data_offset = 0; |
| 1773 | } |
| 1774 | else |
| 1775 | { |
| 1776 | goto out; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1777 | } |
| 1778 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1779 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1780 | /* Get more output if necessary. Or end of packet. */ |
| 1781 | if (PREDICT_FALSE |
| 1782 | (b_current->current_length == VLIB_BUFFER_DATA_SIZE)) |
| 1783 | { |
| 1784 | if (PREDICT_FALSE |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1785 | (vum->cpus[thread_index].rx_buffers_len == 0)) |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1786 | { |
Steven | 62411e7 | 2017-02-03 09:30:37 -0800 | [diff] [blame] | 1787 | /* Cancel speculation */ |
| 1788 | to_next--; |
| 1789 | n_left_to_next++; |
| 1790 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1791 | /* |
| 1792 | * Checking if there are some left buffers. |
| 1793 | * If not, just rewind the used buffers and stop. |
| 1794 | * Note: Scheduled copies are not cancelled. This is |
| 1795 | * not an issue as they would still be valid. Useless, |
| 1796 | * but valid. |
| 1797 | */ |
| 1798 | vhost_user_input_rewind_buffers (vm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1799 | &vum->cpus |
| 1800 | [thread_index], |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1801 | b_head); |
| 1802 | n_left = 0; |
| 1803 | goto stop; |
| 1804 | } |
| 1805 | |
| 1806 | /* Get next output */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1807 | vum->cpus[thread_index].rx_buffers_len--; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1808 | u32 bi_next = |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1809 | (vum->cpus[thread_index].rx_buffers)[vum->cpus |
| 1810 | [thread_index].rx_buffers_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1811 | b_current->next_buffer = bi_next; |
| 1812 | b_current->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1813 | bi_current = bi_next; |
| 1814 | b_current = vlib_get_buffer (vm, bi_current); |
| 1815 | } |
| 1816 | |
| 1817 | /* Prepare a copy order executed later for the data */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1818 | vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1819 | copy_len++; |
| 1820 | u32 desc_data_l = |
| 1821 | desc_table[desc_current].len - desc_data_offset; |
| 1822 | cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length; |
| 1823 | cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; |
Steven | 025d415 | 2017-05-16 21:26:13 -0700 | [diff] [blame] | 1824 | cpy->dst = (uword) (vlib_buffer_get_current (b_current) + |
| 1825 | b_current->current_length); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1826 | cpy->src = desc_table[desc_current].addr + desc_data_offset; |
| 1827 | |
| 1828 | desc_data_offset += cpy->len; |
| 1829 | |
| 1830 | b_current->current_length += cpy->len; |
| 1831 | b_head->total_length_not_including_first_buffer += cpy->len; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1832 | } |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1833 | |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 1834 | out: |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1835 | CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD); |
| 1836 | |
| 1837 | n_rx_bytes += b_head->total_length_not_including_first_buffer; |
| 1838 | n_rx_packets++; |
| 1839 | |
| 1840 | b_head->total_length_not_including_first_buffer -= |
| 1841 | b_head->current_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1842 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1843 | /* consume the descriptor and return it as used */ |
| 1844 | txvq->last_avail_idx++; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1845 | txvq->last_used_idx++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1846 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1847 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1848 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1849 | vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; |
| 1850 | vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1851 | b_head->error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1852 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1853 | { |
| 1854 | u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1855 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1856 | /* redirect if feature path enabled */ |
| 1857 | vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0, |
Damjan Marion | 35af9e5 | 2017-03-06 12:02:50 +0100 | [diff] [blame] | 1858 | b_head); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 1859 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1860 | u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro |
| 1861 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 1862 | to_next, n_left_to_next, |
| 1863 | bi, next0); |
| 1864 | } |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 1865 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1866 | n_left--; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * Although separating memory copies from virtio ring parsing |
| 1870 | * is beneficial, we can offer to perform the copies from time |
| 1871 | * to time in order to free some space in the ring. |
| 1872 | */ |
| 1873 | if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1874 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1875 | if (PREDICT_FALSE |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1876 | (vhost_user_input_copy (vui, vum->cpus[thread_index].copy, |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1877 | copy_len, &map_hint))) |
| 1878 | { |
Steven | 95827e4 | 2017-05-18 21:22:00 -0700 | [diff] [blame] | 1879 | vlib_error_count (vm, node->node_index, |
| 1880 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1881 | } |
| 1882 | copy_len = 0; |
| 1883 | |
| 1884 | /* give buffers back to driver */ |
| 1885 | CLIB_MEMORY_BARRIER (); |
| 1886 | txvq->used->idx = txvq->last_used_idx; |
| 1887 | vhost_user_log_dirty_ring (vui, txvq, idx); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1888 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1889 | } |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1890 | stop: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1891 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1894 | /* Do the memory copies */ |
| 1895 | if (PREDICT_FALSE |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1896 | (vhost_user_input_copy (vui, vum->cpus[thread_index].copy, |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1897 | copy_len, &map_hint))) |
| 1898 | { |
Steven | 95827e4 | 2017-05-18 21:22:00 -0700 | [diff] [blame] | 1899 | vlib_error_count (vm, node->node_index, |
| 1900 | VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1901 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1902 | |
| 1903 | /* give buffers back to driver */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1904 | CLIB_MEMORY_BARRIER (); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1905 | txvq->used->idx = txvq->last_used_idx; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1906 | vhost_user_log_dirty_ring (vui, txvq, idx); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1907 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1908 | /* interrupt (call) handling */ |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 1909 | if ((txvq->callfd_idx != ~0) && |
| 1910 | !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1911 | { |
| 1912 | txvq->n_since_last_int += n_rx_packets; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1913 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1914 | if (txvq->n_since_last_int > vum->coalesce_frames) |
| 1915 | vhost_user_send_call (vm, txvq); |
| 1916 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1917 | |
| 1918 | /* increase rx counters */ |
| 1919 | vlib_increment_combined_counter |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1920 | (vnet_main.interface_main.combined_sw_if_counters |
| 1921 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1922 | vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1923 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1924 | vnet_device_increment_rx_packets (thread_index, n_rx_packets); |
Damjan Marion | b3bb101 | 2017-02-28 21:55:28 +0100 | [diff] [blame] | 1925 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1926 | return n_rx_packets; |
| 1927 | } |
| 1928 | |
| 1929 | static uword |
| 1930 | vhost_user_input (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1931 | vlib_node_runtime_t * node, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1932 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1933 | vhost_user_main_t *vum = &vhost_user_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1934 | uword n_rx_packets = 0; |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 1935 | vhost_user_intf_t *vui; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1936 | vnet_device_input_runtime_t *rt = |
| 1937 | (vnet_device_input_runtime_t *) node->runtime_data; |
| 1938 | vnet_device_and_queue_t *dq; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 1939 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1940 | vec_foreach (dq, rt->devices_and_queues) |
| 1941 | { |
| 1942 | if (clib_smp_swap (&dq->interrupt_pending, 0) || |
| 1943 | (node->state == VLIB_NODE_STATE_POLLING)) |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 1944 | { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1945 | vui = |
| 1946 | pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance); |
| 1947 | n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node, |
| 1948 | dq->mode); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 1949 | } |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1950 | } |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 1951 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1952 | return n_rx_packets; |
| 1953 | } |
| 1954 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1955 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1956 | VLIB_REGISTER_NODE (vhost_user_input_node) = { |
| 1957 | .function = vhost_user_input, |
| 1958 | .type = VLIB_NODE_TYPE_INPUT, |
| 1959 | .name = "vhost-user-input", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 1960 | .sibling_of = "device-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1961 | |
| 1962 | /* Will be enabled if/when hardware is detected. */ |
| 1963 | .state = VLIB_NODE_STATE_DISABLED, |
| 1964 | |
| 1965 | .format_buffer = format_ethernet_header_with_length, |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1966 | .format_trace = format_vhost_trace, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1967 | |
| 1968 | .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR, |
| 1969 | .error_strings = vhost_user_input_func_error_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1970 | }; |
| 1971 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1972 | VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 1973 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1974 | |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1975 | |
| 1976 | void |
| 1977 | vhost_user_tx_trace (vhost_trace_t * t, |
| 1978 | vhost_user_intf_t * vui, u16 qid, |
| 1979 | vlib_buffer_t * b, vhost_user_vring_t * rxvq) |
| 1980 | { |
| 1981 | vhost_user_main_t *vum = &vhost_user_main; |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1982 | u32 last_avail_idx = rxvq->last_avail_idx; |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 1983 | u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask]; |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1984 | vring_desc_t *hdr_desc = 0; |
| 1985 | u32 hint = 0; |
| 1986 | |
| 1987 | memset (t, 0, sizeof (*t)); |
| 1988 | t->device_index = vui - vum->vhost_user_interfaces; |
| 1989 | t->qid = qid; |
| 1990 | |
| 1991 | hdr_desc = &rxvq->desc[desc_current]; |
| 1992 | if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT) |
| 1993 | { |
| 1994 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 1995 | /* Header is the first here */ |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 1996 | hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint); |
| 1997 | } |
| 1998 | if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) |
| 1999 | { |
| 2000 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; |
| 2001 | } |
| 2002 | if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) && |
| 2003 | !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)) |
| 2004 | { |
| 2005 | t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; |
| 2006 | } |
| 2007 | |
| 2008 | t->first_desc_len = hdr_desc ? hdr_desc->len : 0; |
| 2009 | } |
| 2010 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2011 | static_always_inline u32 |
| 2012 | vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, |
| 2013 | u16 copy_len, u32 * map_hint) |
| 2014 | { |
| 2015 | void *dst0, *dst1, *dst2, *dst3; |
| 2016 | if (PREDICT_TRUE (copy_len >= 4)) |
| 2017 | { |
| 2018 | if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint)))) |
| 2019 | return 1; |
| 2020 | if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint)))) |
| 2021 | return 1; |
| 2022 | while (PREDICT_TRUE (copy_len >= 4)) |
| 2023 | { |
| 2024 | dst0 = dst2; |
| 2025 | dst1 = dst3; |
| 2026 | |
| 2027 | if (PREDICT_FALSE |
| 2028 | (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint)))) |
| 2029 | return 1; |
| 2030 | if (PREDICT_FALSE |
| 2031 | (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint)))) |
| 2032 | return 1; |
| 2033 | |
| 2034 | CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD); |
| 2035 | CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD); |
| 2036 | |
| 2037 | clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len); |
| 2038 | clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len); |
| 2039 | |
| 2040 | vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1); |
| 2041 | vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1); |
| 2042 | copy_len -= 2; |
| 2043 | cpy += 2; |
| 2044 | } |
| 2045 | } |
| 2046 | while (copy_len) |
| 2047 | { |
| 2048 | if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint)))) |
| 2049 | return 1; |
| 2050 | clib_memcpy (dst0, (void *) cpy->src, cpy->len); |
| 2051 | vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1); |
| 2052 | copy_len -= 1; |
| 2053 | cpy += 1; |
| 2054 | } |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2059 | static uword |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2060 | vhost_user_tx (vlib_main_t * vm, |
| 2061 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2062 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2063 | u32 *buffers = vlib_frame_args (frame); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2064 | u32 n_left = frame->n_vectors; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2065 | vhost_user_main_t *vum = &vhost_user_main; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2066 | vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; |
| 2067 | vhost_user_intf_t *vui = |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2068 | pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2069 | u32 qid = ~0; |
| 2070 | vhost_user_vring_t *rxvq; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2071 | u8 error; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2072 | u32 thread_index = vlib_get_thread_index (); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2073 | u32 map_hint = 0; |
| 2074 | u8 retry = 8; |
| 2075 | u16 copy_len; |
| 2076 | u16 tx_headers_len; |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 2077 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2078 | if (PREDICT_FALSE (!vui->admin_up)) |
| 2079 | { |
| 2080 | error = VHOST_USER_TX_FUNC_ERROR_DOWN; |
| 2081 | goto done3; |
| 2082 | } |
| 2083 | |
| 2084 | if (PREDICT_FALSE (!vui->is_up)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2085 | { |
| 2086 | error = VHOST_USER_TX_FUNC_ERROR_NOT_READY; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2087 | goto done3; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2088 | } |
Damjan Marion | 920ecc2 | 2016-01-12 18:34:24 +0100 | [diff] [blame] | 2089 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2090 | qid = |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2091 | VHOST_VRING_IDX_RX (*vec_elt_at_index |
Steven | d772753 | 2017-06-09 18:49:17 -0700 | [diff] [blame] | 2092 | (vui->per_cpu_tx_qid, thread_index)); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2093 | rxvq = &vui->vrings[qid]; |
| 2094 | if (PREDICT_FALSE (vui->use_tx_spinlock)) |
| 2095 | vhost_user_vring_lock (vui, qid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2096 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2097 | retry: |
| 2098 | error = VHOST_USER_TX_FUNC_ERROR_NONE; |
| 2099 | tx_headers_len = 0; |
| 2100 | copy_len = 0; |
| 2101 | while (n_left > 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2102 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2103 | vlib_buffer_t *b0, *current_b0; |
| 2104 | u16 desc_head, desc_index, desc_len; |
| 2105 | vring_desc_t *desc_table; |
| 2106 | uword buffer_map_addr; |
| 2107 | u32 buffer_len; |
| 2108 | u16 bytes_left; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2109 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2110 | if (PREDICT_TRUE (n_left > 1)) |
| 2111 | vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD); |
| 2112 | |
| 2113 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 2114 | |
| 2115 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 2116 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2117 | vum->cpus[thread_index].current_trace = |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2118 | vlib_add_trace (vm, node, b0, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2119 | sizeof (*vum->cpus[thread_index].current_trace)); |
| 2120 | vhost_user_tx_trace (vum->cpus[thread_index].current_trace, |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2121 | vui, qid / 2, b0, rxvq); |
| 2122 | } |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 2123 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2124 | if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx)) |
| 2125 | { |
| 2126 | error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; |
| 2127 | goto done; |
| 2128 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2129 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2130 | desc_table = rxvq->desc; |
| 2131 | desc_head = desc_index = |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2132 | rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2133 | |
| 2134 | /* Go deeper in case of indirect descriptor |
| 2135 | * I don't know of any driver providing indirect for RX. */ |
| 2136 | if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT)) |
| 2137 | { |
| 2138 | if (PREDICT_FALSE |
| 2139 | (rxvq->desc[desc_head].len < sizeof (vring_desc_t))) |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 2140 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2141 | error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 2142 | goto done; |
| 2143 | } |
| 2144 | if (PREDICT_FALSE |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2145 | (!(desc_table = |
| 2146 | map_guest_mem (vui, rxvq->desc[desc_index].addr, |
| 2147 | &map_hint)))) |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 2148 | { |
| 2149 | error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; |
| 2150 | goto done; |
| 2151 | } |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2152 | desc_index = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2153 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2154 | |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2155 | desc_len = vui->virtio_net_hdr_sz; |
| 2156 | buffer_map_addr = desc_table[desc_index].addr; |
| 2157 | buffer_len = desc_table[desc_index].len; |
| 2158 | |
| 2159 | { |
| 2160 | // Get a header from the header array |
| 2161 | virtio_net_hdr_mrg_rxbuf_t *hdr = |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2162 | &vum->cpus[thread_index].tx_headers[tx_headers_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2163 | tx_headers_len++; |
| 2164 | hdr->hdr.flags = 0; |
| 2165 | hdr->hdr.gso_type = 0; |
| 2166 | hdr->num_buffers = 1; //This is local, no need to check |
| 2167 | |
| 2168 | // Prepare a copy order executed later for the header |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2169 | vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2170 | copy_len++; |
| 2171 | cpy->len = vui->virtio_net_hdr_sz; |
| 2172 | cpy->dst = buffer_map_addr; |
| 2173 | cpy->src = (uword) hdr; |
| 2174 | } |
| 2175 | |
| 2176 | buffer_map_addr += vui->virtio_net_hdr_sz; |
| 2177 | buffer_len -= vui->virtio_net_hdr_sz; |
| 2178 | bytes_left = b0->current_length; |
| 2179 | current_b0 = b0; |
| 2180 | while (1) |
| 2181 | { |
| 2182 | if (buffer_len == 0) |
| 2183 | { //Get new output |
| 2184 | if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT) |
| 2185 | { |
| 2186 | //Next one is chained |
| 2187 | desc_index = desc_table[desc_index].next; |
| 2188 | buffer_map_addr = desc_table[desc_index].addr; |
| 2189 | buffer_len = desc_table[desc_index].len; |
| 2190 | } |
| 2191 | else if (vui->virtio_net_hdr_sz == 12) //MRG is available |
| 2192 | { |
| 2193 | virtio_net_hdr_mrg_rxbuf_t *hdr = |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2194 | &vum->cpus[thread_index].tx_headers[tx_headers_len - 1]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2195 | |
| 2196 | //Move from available to used buffer |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2197 | rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2198 | desc_head; |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2199 | rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2200 | desc_len; |
| 2201 | vhost_user_log_dirty_ring (vui, rxvq, |
| 2202 | ring[rxvq->last_used_idx & |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2203 | rxvq->qsz_mask]); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2204 | |
| 2205 | rxvq->last_avail_idx++; |
| 2206 | rxvq->last_used_idx++; |
| 2207 | hdr->num_buffers++; |
| 2208 | desc_len = 0; |
| 2209 | |
| 2210 | if (PREDICT_FALSE |
| 2211 | (rxvq->last_avail_idx == rxvq->avail->idx)) |
| 2212 | { |
| 2213 | //Dequeue queued descriptors for this packet |
| 2214 | rxvq->last_used_idx -= hdr->num_buffers - 1; |
| 2215 | rxvq->last_avail_idx -= hdr->num_buffers - 1; |
| 2216 | error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; |
| 2217 | goto done; |
| 2218 | } |
| 2219 | |
| 2220 | desc_table = rxvq->desc; |
| 2221 | desc_head = desc_index = |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2222 | rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2223 | if (PREDICT_FALSE |
| 2224 | (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT)) |
| 2225 | { |
| 2226 | //It is seriously unlikely that a driver will put indirect descriptor |
| 2227 | //after non-indirect descriptor. |
| 2228 | if (PREDICT_FALSE |
| 2229 | (rxvq->desc[desc_head].len < sizeof (vring_desc_t))) |
| 2230 | { |
| 2231 | error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; |
| 2232 | goto done; |
| 2233 | } |
| 2234 | if (PREDICT_FALSE |
| 2235 | (!(desc_table = |
| 2236 | map_guest_mem (vui, |
| 2237 | rxvq->desc[desc_index].addr, |
| 2238 | &map_hint)))) |
| 2239 | { |
| 2240 | error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; |
| 2241 | goto done; |
| 2242 | } |
| 2243 | desc_index = 0; |
| 2244 | } |
| 2245 | buffer_map_addr = desc_table[desc_index].addr; |
| 2246 | buffer_len = desc_table[desc_index].len; |
| 2247 | } |
| 2248 | else |
| 2249 | { |
| 2250 | error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG; |
| 2251 | goto done; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2256 | vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2257 | copy_len++; |
| 2258 | cpy->len = bytes_left; |
| 2259 | cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len; |
| 2260 | cpy->dst = buffer_map_addr; |
| 2261 | cpy->src = (uword) vlib_buffer_get_current (current_b0) + |
| 2262 | current_b0->current_length - bytes_left; |
| 2263 | |
| 2264 | bytes_left -= cpy->len; |
| 2265 | buffer_len -= cpy->len; |
| 2266 | buffer_map_addr += cpy->len; |
| 2267 | desc_len += cpy->len; |
| 2268 | |
Pierre Pfister | 14ac801 | 2016-12-08 07:58:47 +0000 | [diff] [blame] | 2269 | CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | // Check if vlib buffer has more data. If not, get more or break. |
| 2273 | if (PREDICT_TRUE (!bytes_left)) |
| 2274 | { |
| 2275 | if (PREDICT_FALSE |
| 2276 | (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 2277 | { |
| 2278 | current_b0 = vlib_get_buffer (vm, current_b0->next_buffer); |
| 2279 | bytes_left = current_b0->current_length; |
| 2280 | } |
| 2281 | else |
| 2282 | { |
| 2283 | //End of packet |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | //Move from available to used ring |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2290 | rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head; |
| 2291 | rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2292 | vhost_user_log_dirty_ring (vui, rxvq, |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 2293 | ring[rxvq->last_used_idx & rxvq->qsz_mask]); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2294 | rxvq->last_avail_idx++; |
| 2295 | rxvq->last_used_idx++; |
| 2296 | |
| 2297 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 2298 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2299 | vum->cpus[thread_index].current_trace->hdr = |
| 2300 | vum->cpus[thread_index].tx_headers[tx_headers_len - 1]; |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | n_left--; //At the end for error counting when 'goto done' is invoked |
Steven | d772753 | 2017-06-09 18:49:17 -0700 | [diff] [blame] | 2304 | |
| 2305 | /* |
| 2306 | * Do the copy periodically to prevent |
| 2307 | * vum->cpus[thread_index].copy array overflow and corrupt memory |
| 2308 | */ |
| 2309 | if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD)) |
| 2310 | { |
| 2311 | if (PREDICT_FALSE |
| 2312 | (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy, |
| 2313 | copy_len, &map_hint))) |
| 2314 | { |
| 2315 | vlib_error_count (vm, node->node_index, |
| 2316 | VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); |
| 2317 | } |
| 2318 | copy_len = 0; |
| 2319 | |
| 2320 | /* give buffers back to driver */ |
| 2321 | CLIB_MEMORY_BARRIER (); |
| 2322 | rxvq->used->idx = rxvq->last_used_idx; |
| 2323 | vhost_user_log_dirty_ring (vui, rxvq, idx); |
| 2324 | } |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2325 | buffers++; |
| 2326 | } |
| 2327 | |
| 2328 | done: |
| 2329 | //Do the memory copies |
| 2330 | if (PREDICT_FALSE |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2331 | (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy, |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2332 | copy_len, &map_hint))) |
| 2333 | { |
Steven | 95827e4 | 2017-05-18 21:22:00 -0700 | [diff] [blame] | 2334 | vlib_error_count (vm, node->node_index, |
| 2335 | VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | CLIB_MEMORY_BARRIER (); |
| 2339 | rxvq->used->idx = rxvq->last_used_idx; |
| 2340 | vhost_user_log_dirty_ring (vui, rxvq, idx); |
| 2341 | |
| 2342 | /* |
| 2343 | * When n_left is set, error is always set to something too. |
| 2344 | * In case error is due to lack of remaining buffers, we go back up and |
| 2345 | * retry. |
| 2346 | * The idea is that it is better to waste some time on packets |
| 2347 | * that have been processed already than dropping them and get |
| 2348 | * more fresh packets with a good likelyhood that they will be dropped too. |
| 2349 | * This technique also gives more time to VM driver to pick-up packets. |
| 2350 | * In case the traffic flows from physical to virtual interfaces, this |
| 2351 | * technique will end-up leveraging the physical NIC buffer in order to |
| 2352 | * absorb the VM's CPU jitter. |
| 2353 | */ |
| 2354 | if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry) |
| 2355 | { |
| 2356 | retry--; |
| 2357 | goto retry; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2358 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2359 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2360 | /* interrupt (call) handling */ |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 2361 | if ((rxvq->callfd_idx != ~0) && |
| 2362 | !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2363 | { |
Pierre Pfister | d3eb90e | 2016-11-29 15:36:14 +0000 | [diff] [blame] | 2364 | rxvq->n_since_last_int += frame->n_vectors - n_left; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2365 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2366 | if (rxvq->n_since_last_int > vum->coalesce_frames) |
| 2367 | vhost_user_send_call (vm, rxvq); |
| 2368 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2369 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2370 | vhost_user_vring_unlock (vui, qid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2371 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2372 | done3: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2373 | if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) |
| 2374 | { |
| 2375 | vlib_error_count (vm, node->node_index, error, n_left); |
| 2376 | vlib_increment_simple_counter |
| 2377 | (vnet_main.interface_main.sw_if_counters |
| 2378 | + VNET_INTERFACE_COUNTER_DROP, |
Steven | d772753 | 2017-06-09 18:49:17 -0700 | [diff] [blame] | 2379 | thread_index, vui->sw_if_index, n_left); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2380 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 2381 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2382 | vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); |
| 2383 | return frame->n_vectors; |
| 2384 | } |
| 2385 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2386 | static uword |
| 2387 | vhost_user_send_interrupt_process (vlib_main_t * vm, |
| 2388 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 2389 | { |
| 2390 | vhost_user_intf_t *vui; |
| 2391 | f64 timeout = 3153600000.0 /* 100 years */ ; |
| 2392 | uword event_type, *event_data = 0; |
| 2393 | vhost_user_main_t *vum = &vhost_user_main; |
| 2394 | u16 *queue; |
| 2395 | f64 now, poll_time_remaining; |
| 2396 | f64 next_timeout; |
| 2397 | u8 stop_timer = 0; |
| 2398 | |
| 2399 | while (1) |
| 2400 | { |
| 2401 | poll_time_remaining = |
| 2402 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 2403 | event_type = vlib_process_get_events (vm, &event_data); |
| 2404 | vec_reset_length (event_data); |
| 2405 | |
| 2406 | /* |
| 2407 | * Use the remaining timeout if it is less than coalesce time to avoid |
| 2408 | * resetting the existing timer in the middle of expiration |
| 2409 | */ |
| 2410 | timeout = poll_time_remaining; |
| 2411 | if (vlib_process_suspend_time_is_zero (timeout) || |
| 2412 | (timeout > vum->coalesce_time)) |
| 2413 | timeout = vum->coalesce_time; |
| 2414 | |
| 2415 | now = vlib_time_now (vm); |
| 2416 | switch (event_type) |
| 2417 | { |
| 2418 | case VHOST_USER_EVENT_STOP_TIMER: |
| 2419 | stop_timer = 1; |
| 2420 | break; |
| 2421 | |
| 2422 | case VHOST_USER_EVENT_START_TIMER: |
| 2423 | stop_timer = 0; |
| 2424 | if (!vlib_process_suspend_time_is_zero (poll_time_remaining)) |
| 2425 | break; |
| 2426 | /* fall through */ |
| 2427 | |
| 2428 | case ~0: |
| 2429 | /* *INDENT-OFF* */ |
| 2430 | pool_foreach (vui, vum->vhost_user_interfaces, { |
| 2431 | next_timeout = timeout; |
| 2432 | vec_foreach (queue, vui->rx_queues) |
| 2433 | { |
| 2434 | vhost_user_vring_t *rxvq = |
| 2435 | &vui->vrings[VHOST_VRING_IDX_RX (*queue)]; |
| 2436 | vhost_user_vring_t *txvq = |
| 2437 | &vui->vrings[VHOST_VRING_IDX_TX (*queue)]; |
| 2438 | |
| 2439 | if (txvq->n_since_last_int) |
| 2440 | { |
| 2441 | if (now >= txvq->int_deadline) |
| 2442 | vhost_user_send_call (vm, txvq); |
| 2443 | else |
| 2444 | next_timeout = txvq->int_deadline - now; |
| 2445 | } |
| 2446 | |
| 2447 | if (rxvq->n_since_last_int) |
| 2448 | { |
| 2449 | if (now >= rxvq->int_deadline) |
| 2450 | vhost_user_send_call (vm, rxvq); |
| 2451 | else |
| 2452 | next_timeout = rxvq->int_deadline - now; |
| 2453 | } |
| 2454 | |
| 2455 | if ((next_timeout < timeout) && (next_timeout > 0.0)) |
| 2456 | timeout = next_timeout; |
| 2457 | } |
| 2458 | }); |
| 2459 | /* *INDENT-ON* */ |
| 2460 | break; |
| 2461 | |
| 2462 | default: |
| 2463 | clib_warning ("BUG: unhandled event type %d", event_type); |
| 2464 | break; |
| 2465 | } |
| 2466 | /* No less than 1 millisecond */ |
| 2467 | if (timeout < 1e-3) |
| 2468 | timeout = 1e-3; |
| 2469 | if (stop_timer) |
| 2470 | timeout = 3153600000.0; |
| 2471 | } |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
| 2475 | /* *INDENT-OFF* */ |
| 2476 | VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = { |
| 2477 | .function = vhost_user_send_interrupt_process, |
| 2478 | .type = VLIB_NODE_TYPE_PROCESS, |
| 2479 | .name = "vhost-user-send-interrupt-process", |
| 2480 | }; |
| 2481 | /* *INDENT-ON* */ |
| 2482 | |
| 2483 | static clib_error_t * |
| 2484 | vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, |
| 2485 | u32 qid, vnet_hw_interface_rx_mode mode) |
| 2486 | { |
| 2487 | vlib_main_t *vm = vnm->vlib_main; |
| 2488 | vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); |
| 2489 | vhost_user_main_t *vum = &vhost_user_main; |
| 2490 | vhost_user_intf_t *vui = |
| 2491 | pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance); |
| 2492 | vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; |
| 2493 | |
| 2494 | if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) || |
| 2495 | (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) |
| 2496 | { |
Steven | 49a04b9 | 2017-07-29 08:56:08 -0700 | [diff] [blame] | 2497 | if (txvq->kickfd_idx == ~0) |
| 2498 | { |
| 2499 | // We cannot support interrupt mode if the driver opts out |
| 2500 | return clib_error_return (0, "Driver does not support interrupt"); |
| 2501 | } |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2502 | if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING) |
| 2503 | { |
| 2504 | vum->ifq_count++; |
| 2505 | // Start the timer if this is the first encounter on interrupt |
| 2506 | // interface/queue |
| 2507 | if ((vum->ifq_count == 1) && |
| 2508 | (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0)) |
| 2509 | vlib_process_signal_event (vm, |
| 2510 | vhost_user_send_interrupt_node.index, |
| 2511 | VHOST_USER_EVENT_START_TIMER, 0); |
| 2512 | } |
| 2513 | } |
| 2514 | else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING) |
| 2515 | { |
| 2516 | if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) || |
| 2517 | (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) && |
| 2518 | vum->ifq_count) |
| 2519 | { |
| 2520 | vum->ifq_count--; |
| 2521 | // Stop the timer if there is no more interrupt interface/queue |
| 2522 | if ((vum->ifq_count == 0) && |
| 2523 | (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0)) |
| 2524 | vlib_process_signal_event (vm, |
| 2525 | vhost_user_send_interrupt_node.index, |
| 2526 | VHOST_USER_EVENT_STOP_TIMER, 0); |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | txvq->mode = mode; |
| 2531 | if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING) |
| 2532 | txvq->used->flags = VRING_USED_F_NO_NOTIFY; |
| 2533 | else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) || |
| 2534 | (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT)) |
| 2535 | txvq->used->flags = 0; |
| 2536 | else |
| 2537 | { |
| 2538 | clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode, |
| 2539 | hw_if_index, qid); |
| 2540 | return clib_error_return (0, "unsupported"); |
| 2541 | } |
| 2542 | |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2546 | static clib_error_t * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2547 | vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, |
| 2548 | u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2549 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2550 | vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2551 | vhost_user_main_t *vum = &vhost_user_main; |
| 2552 | vhost_user_intf_t *vui = |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2553 | pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance); |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 2554 | u32 hw_flags = 0; |
| 2555 | vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 2556 | hw_flags = vui->admin_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2557 | |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 2558 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, hw_flags); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2559 | |
| 2560 | return /* no error */ 0; |
| 2561 | } |
| 2562 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2563 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2564 | VNET_DEVICE_CLASS (vhost_user_dev_class,static) = { |
| 2565 | .name = "vhost-user", |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2566 | .tx_function = vhost_user_tx, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2567 | .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR, |
| 2568 | .tx_function_error_strings = vhost_user_tx_func_error_strings, |
| 2569 | .format_device_name = format_vhost_user_interface_name, |
| 2570 | .name_renumber = vhost_user_name_renumber, |
| 2571 | .admin_up_down_function = vhost_user_interface_admin_up_down, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2572 | .rx_mode_change_function = vhost_user_interface_rx_mode_change, |
Pierre Pfister | 116ea4b | 2016-11-08 15:49:28 +0000 | [diff] [blame] | 2573 | .format_tx_trace = format_vhost_trace, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2574 | }; |
| 2575 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 2576 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class, |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2577 | vhost_user_tx) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2578 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 2579 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2580 | static uword |
| 2581 | vhost_user_process (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2582 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2583 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2584 | vhost_user_main_t *vum = &vhost_user_main; |
| 2585 | vhost_user_intf_t *vui; |
| 2586 | struct sockaddr_un sun; |
| 2587 | int sockfd; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2588 | clib_file_t template = { 0 }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2589 | f64 timeout = 3153600000.0 /* 100 years */ ; |
| 2590 | uword *event_data = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2591 | |
Steven | 0d150bb | 2017-03-22 12:05:19 -0700 | [diff] [blame] | 2592 | sockfd = -1; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2593 | sun.sun_family = AF_UNIX; |
| 2594 | template.read_function = vhost_user_socket_read; |
| 2595 | template.error_function = vhost_user_socket_error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2596 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2597 | while (1) |
| 2598 | { |
| 2599 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 2600 | vlib_process_get_events (vm, &event_data); |
| 2601 | vec_reset_length (event_data); |
| 2602 | |
| 2603 | timeout = 3.0; |
| 2604 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2605 | /* *INDENT-OFF* */ |
| 2606 | pool_foreach (vui, vum->vhost_user_interfaces, { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2607 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2608 | if (vui->unix_server_index == ~0) { //Nothing to do for server sockets |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2609 | if (vui->clib_file_index == ~0) |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2610 | { |
Steven | 0d150bb | 2017-03-22 12:05:19 -0700 | [diff] [blame] | 2611 | if ((sockfd < 0) && |
| 2612 | ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)) |
| 2613 | { |
| 2614 | /* |
| 2615 | * 1st time error or new error for this interface, |
| 2616 | * spit out the message and record the error |
| 2617 | */ |
| 2618 | if (!vui->sock_errno || (vui->sock_errno != errno)) |
| 2619 | { |
| 2620 | clib_unix_warning |
| 2621 | ("Error: Could not open unix socket for %s", |
| 2622 | vui->sock_filename); |
| 2623 | vui->sock_errno = errno; |
| 2624 | } |
| 2625 | continue; |
| 2626 | } |
| 2627 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2628 | /* try to connect */ |
| 2629 | strncpy (sun.sun_path, (char *) vui->sock_filename, |
| 2630 | sizeof (sun.sun_path) - 1); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2631 | |
Andrew Yourtchenko | 0c3d467 | 2017-01-03 16:52:22 +0000 | [diff] [blame] | 2632 | /* Avoid hanging VPP if the other end does not accept */ |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 2633 | if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) |
| 2634 | clib_unix_warning ("fcntl"); |
| 2635 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2636 | if (connect (sockfd, (struct sockaddr *) &sun, |
| 2637 | sizeof (struct sockaddr_un)) == 0) |
| 2638 | { |
Andrew Yourtchenko | 0c3d467 | 2017-01-03 16:52:22 +0000 | [diff] [blame] | 2639 | /* Set the socket to blocking as it was before */ |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 2640 | if (fcntl(sockfd, F_SETFL, 0) < 0) |
| 2641 | clib_unix_warning ("fcntl2"); |
| 2642 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2643 | vui->sock_errno = 0; |
| 2644 | template.file_descriptor = sockfd; |
| 2645 | template.private_data = |
| 2646 | vui - vhost_user_main.vhost_user_interfaces; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2647 | vui->clib_file_index = clib_file_add (&file_main, &template); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2648 | |
Steven | 0d150bb | 2017-03-22 12:05:19 -0700 | [diff] [blame] | 2649 | /* This sockfd is considered consumed */ |
| 2650 | sockfd = -1; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2651 | } |
| 2652 | else |
| 2653 | { |
| 2654 | vui->sock_errno = errno; |
| 2655 | } |
| 2656 | } |
| 2657 | else |
| 2658 | { |
| 2659 | /* check if socket is alive */ |
| 2660 | int error = 0; |
| 2661 | socklen_t len = sizeof (error); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2662 | int fd = UNIX_GET_FD(vui->clib_file_index); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2663 | int retval = |
| 2664 | getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2665 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2666 | if (retval) |
| 2667 | { |
| 2668 | DBG_SOCK ("getsockopt returned %d", retval); |
| 2669 | vhost_user_if_disconnect (vui); |
| 2670 | } |
| 2671 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2672 | } |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2673 | }); |
| 2674 | /* *INDENT-ON* */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2675 | } |
| 2676 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2677 | } |
| 2678 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2679 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2680 | VLIB_REGISTER_NODE (vhost_user_process_node,static) = { |
| 2681 | .function = vhost_user_process, |
| 2682 | .type = VLIB_NODE_TYPE_PROCESS, |
| 2683 | .name = "vhost-user-process", |
| 2684 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2685 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2686 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2687 | /** |
| 2688 | * Disables and reset interface structure. |
| 2689 | * It can then be either init again, or removed from used interfaces. |
| 2690 | */ |
| 2691 | static void |
| 2692 | vhost_user_term_if (vhost_user_intf_t * vui) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2693 | { |
Ole Troan | 553a411 | 2017-01-10 10:07:04 +0100 | [diff] [blame] | 2694 | int q; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2695 | vhost_user_main_t *vum = &vhost_user_main; |
Ole Troan | 553a411 | 2017-01-10 10:07:04 +0100 | [diff] [blame] | 2696 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2697 | // disconnect interface sockets |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2698 | vhost_user_if_disconnect (vui); |
Pierre Pfister | fbb2ef6 | 2016-11-16 02:43:29 +0000 | [diff] [blame] | 2699 | vhost_user_update_iface_state (vui); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2700 | |
Ole Troan | 553a411 | 2017-01-10 10:07:04 +0100 | [diff] [blame] | 2701 | for (q = 0; q < VHOST_VRING_MAX_N; q++) |
| 2702 | { |
| 2703 | clib_mem_free ((void *) vui->vring_locks[q]); |
| 2704 | } |
| 2705 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2706 | if (vui->unix_server_index != ~0) |
| 2707 | { |
| 2708 | //Close server socket |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2709 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2710 | vui->unix_server_index); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2711 | clib_file_del (&file_main, uf); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2712 | vui->unix_server_index = ~0; |
Steven | 5312942 | 2017-04-21 13:31:50 -0700 | [diff] [blame] | 2713 | unlink (vui->sock_filename); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2714 | } |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2715 | |
| 2716 | mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename, |
| 2717 | &vui->if_index); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2718 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2719 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2720 | int |
| 2721 | vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index) |
| 2722 | { |
| 2723 | vhost_user_main_t *vum = &vhost_user_main; |
| 2724 | vhost_user_intf_t *vui; |
| 2725 | int rv = 0; |
| 2726 | vnet_hw_interface_t *hwif; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2727 | u16 *queue; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2728 | |
| 2729 | if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) || |
| 2730 | hwif->dev_class_index != vhost_user_dev_class.index) |
| 2731 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 2732 | |
| 2733 | DBG_SOCK ("Deleting vhost-user interface %s (instance %d)", |
| 2734 | hwif->name, hwif->dev_instance); |
| 2735 | |
| 2736 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); |
| 2737 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2738 | vec_foreach (queue, vui->rx_queues) |
| 2739 | { |
| 2740 | vhost_user_vring_t *txvq; |
| 2741 | |
| 2742 | txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)]; |
| 2743 | if ((vum->ifq_count > 0) && |
| 2744 | ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) || |
| 2745 | (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))) |
| 2746 | { |
| 2747 | vum->ifq_count--; |
| 2748 | // Stop the timer if there is no more interrupt interface/queue |
| 2749 | if ((vum->ifq_count == 0) && |
| 2750 | (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0)) |
| 2751 | { |
| 2752 | vlib_process_signal_event (vm, |
| 2753 | vhost_user_send_interrupt_node.index, |
| 2754 | VHOST_USER_EVENT_STOP_TIMER, 0); |
| 2755 | break; |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2760 | // Disable and reset interface |
| 2761 | vhost_user_term_if (vui); |
| 2762 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2763 | // Reset renumbered iface |
| 2764 | if (hwif->dev_instance < |
| 2765 | vec_len (vum->show_dev_instance_by_real_dev_instance)) |
| 2766 | vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0; |
| 2767 | |
| 2768 | // Delete ethernet interface |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2769 | ethernet_delete_interface (vnm, vui->hw_if_index); |
Wojciech Dec | d8e4787 | 2017-01-17 21:45:11 +0100 | [diff] [blame] | 2770 | |
| 2771 | // Back to pool |
| 2772 | pool_put (vum->vhost_user_interfaces, vui); |
| 2773 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2774 | return rv; |
| 2775 | } |
| 2776 | |
Steven | 5312942 | 2017-04-21 13:31:50 -0700 | [diff] [blame] | 2777 | static clib_error_t * |
| 2778 | vhost_user_exit (vlib_main_t * vm) |
| 2779 | { |
| 2780 | vnet_main_t *vnm = vnet_get_main (); |
| 2781 | vhost_user_main_t *vum = &vhost_user_main; |
| 2782 | vhost_user_intf_t *vui; |
| 2783 | |
Steven | 4174886 | 2017-04-25 13:49:51 -0700 | [diff] [blame] | 2784 | vlib_worker_thread_barrier_sync (vlib_get_main ()); |
Steven | 5312942 | 2017-04-21 13:31:50 -0700 | [diff] [blame] | 2785 | /* *INDENT-OFF* */ |
| 2786 | pool_foreach (vui, vum->vhost_user_interfaces, { |
| 2787 | vhost_user_delete_if (vnm, vm, vui->sw_if_index); |
| 2788 | }); |
| 2789 | /* *INDENT-ON* */ |
Steven | 4174886 | 2017-04-25 13:49:51 -0700 | [diff] [blame] | 2790 | vlib_worker_thread_barrier_release (vlib_get_main ()); |
Steven | 5312942 | 2017-04-21 13:31:50 -0700 | [diff] [blame] | 2791 | return 0; |
| 2792 | } |
| 2793 | |
| 2794 | VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit); |
| 2795 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2796 | /** |
| 2797 | * Open server unix socket on specified sock_filename. |
| 2798 | */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2799 | static int |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2800 | vhost_user_init_server_sock (const char *sock_filename, int *sock_fd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2801 | { |
Pierre Pfister | 5afccb2 | 2016-07-25 14:32:02 +0100 | [diff] [blame] | 2802 | int rv = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2803 | struct sockaddr_un un = { }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2804 | int fd; |
| 2805 | /* create listening socket */ |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2806 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 2807 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2808 | |
| 2809 | un.sun_family = AF_UNIX; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2810 | strncpy ((char *) un.sun_path, (char *) sock_filename, |
| 2811 | sizeof (un.sun_path) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2812 | |
| 2813 | /* remove if exists */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2814 | unlink ((char *) sock_filename); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2815 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2816 | if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1) |
| 2817 | { |
| 2818 | rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 2819 | goto error; |
| 2820 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2821 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2822 | if (listen (fd, 1) == -1) |
| 2823 | { |
| 2824 | rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 2825 | goto error; |
| 2826 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2827 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2828 | *sock_fd = fd; |
| 2829 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2830 | |
| 2831 | error: |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2832 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2833 | return rv; |
| 2834 | } |
| 2835 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2836 | /** |
| 2837 | * Create ethernet interface for vhost user interface. |
| 2838 | */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2839 | static void |
| 2840 | vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm, |
| 2841 | vhost_user_intf_t * vui, u8 * hwaddress) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2842 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2843 | vhost_user_main_t *vum = &vhost_user_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2844 | u8 hwaddr[6]; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2845 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2846 | |
| 2847 | /* create hw and sw interface */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2848 | if (hwaddress) |
| 2849 | { |
| 2850 | clib_memcpy (hwaddr, hwaddress, 6); |
| 2851 | } |
| 2852 | else |
| 2853 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2854 | random_u32 (&vum->random); |
| 2855 | clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random)); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2856 | hwaddr[0] = 2; |
| 2857 | hwaddr[1] = 0xfe; |
| 2858 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2859 | |
| 2860 | error = ethernet_register_interface |
| 2861 | (vnm, |
| 2862 | vhost_user_dev_class.index, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2863 | vui - vum->vhost_user_interfaces /* device instance */ , |
| 2864 | hwaddr /* ethernet address */ , |
| 2865 | &vui->hw_if_index, 0 /* flag change */ ); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2866 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2867 | if (error) |
| 2868 | clib_error_report (error); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 2869 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 2870 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index); |
| 2871 | hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2874 | /* |
| 2875 | * Initialize vui with specified attributes |
| 2876 | */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2877 | static void |
| 2878 | vhost_user_vui_init (vnet_main_t * vnm, |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2879 | vhost_user_intf_t * vui, |
| 2880 | int server_sock_fd, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2881 | const char *sock_filename, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2882 | u64 feature_mask, u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2883 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2884 | vnet_sw_interface_t *sw; |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 2885 | int q; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2886 | vhost_user_main_t *vum = &vhost_user_main; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2887 | vnet_hw_interface_t *hw; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2888 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2889 | hw = vnet_get_hw_interface (vnm, vui->hw_if_index); |
| 2890 | sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2891 | if (server_sock_fd != -1) |
| 2892 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2893 | clib_file_t template = { 0 }; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2894 | template.read_function = vhost_user_socksvr_accept_ready; |
| 2895 | template.file_descriptor = server_sock_fd; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2896 | template.private_data = vui - vum->vhost_user_interfaces; //hw index |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2897 | vui->unix_server_index = clib_file_add (&file_main, &template); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2898 | } |
| 2899 | else |
| 2900 | { |
| 2901 | vui->unix_server_index = ~0; |
| 2902 | } |
| 2903 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2904 | vui->sw_if_index = sw->sw_if_index; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2905 | strncpy (vui->sock_filename, sock_filename, |
| 2906 | ARRAY_LEN (vui->sock_filename) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2907 | vui->sock_errno = 0; |
| 2908 | vui->is_up = 0; |
| 2909 | vui->feature_mask = feature_mask; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2910 | vui->clib_file_index = ~0; |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 2911 | vui->log_base_addr = 0; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2912 | vui->if_index = vui - vum->vhost_user_interfaces; |
| 2913 | mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename, |
| 2914 | &vui->if_index, 0); |
Yoann Desmouceaux | 4667c22 | 2016-02-24 22:51:00 +0100 | [diff] [blame] | 2915 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2916 | for (q = 0; q < VHOST_VRING_MAX_N; q++) |
| 2917 | vhost_user_vring_init (vui, q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2918 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2919 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2920 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2921 | |
| 2922 | if (sw_if_index) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2923 | *sw_if_index = vui->sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2924 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2925 | for (q = 0; q < VHOST_VRING_MAX_N; q++) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2926 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2927 | vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, |
| 2928 | CLIB_CACHE_LINE_BYTES); |
| 2929 | memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2930 | } |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 2931 | |
| 2932 | vec_validate (vui->per_cpu_tx_qid, |
| 2933 | vlib_get_thread_main ()->n_vlib_mains - 1); |
| 2934 | vhost_user_tx_thread_placement (vui); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2937 | int |
| 2938 | vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm, |
| 2939 | const char *sock_filename, |
| 2940 | u8 is_server, |
| 2941 | u32 * sw_if_index, |
| 2942 | u64 feature_mask, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2943 | u8 renumber, u32 custom_dev_instance, u8 * hwaddr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2944 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2945 | vhost_user_intf_t *vui = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2946 | u32 sw_if_idx = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2947 | int rv = 0; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2948 | int server_sock_fd = -1; |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 2949 | vhost_user_main_t *vum = &vhost_user_main; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2950 | uword *if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2951 | |
Wojciech Dec | 3cd9eed | 2017-01-03 10:38:37 +0100 | [diff] [blame] | 2952 | if (sock_filename == NULL || !(strlen (sock_filename) > 0)) |
| 2953 | { |
| 2954 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 2955 | } |
| 2956 | |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 2957 | if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename); |
| 2958 | if (if_index) |
| 2959 | { |
| 2960 | if (sw_if_index) |
| 2961 | { |
| 2962 | vui = &vum->vhost_user_interfaces[*if_index]; |
| 2963 | *sw_if_index = vui->sw_if_index; |
| 2964 | } |
| 2965 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 2966 | } |
| 2967 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2968 | if (is_server) |
| 2969 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2970 | if ((rv = |
| 2971 | vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2972 | { |
| 2973 | return rv; |
| 2974 | } |
| 2975 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2976 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2977 | pool_get (vhost_user_main.vhost_user_interfaces, vui); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2978 | |
Pierre Pfister | ef65cb0 | 2016-02-19 13:52:44 +0000 | [diff] [blame] | 2979 | vhost_user_create_ethernet (vnm, vm, vui, hwaddr); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2980 | vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 2981 | feature_mask, &sw_if_idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2982 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2983 | if (renumber) |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2984 | vnet_interface_name_renumber (sw_if_idx, custom_dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2985 | |
| 2986 | if (sw_if_index) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2987 | *sw_if_index = sw_if_idx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2988 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 2989 | // Process node must connect |
| 2990 | vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 2991 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2992 | return rv; |
| 2993 | } |
| 2994 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 2995 | int |
| 2996 | vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm, |
| 2997 | const char *sock_filename, |
| 2998 | u8 is_server, |
| 2999 | u32 sw_if_index, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3000 | u64 feature_mask, u8 renumber, u32 custom_dev_instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3001 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3002 | vhost_user_main_t *vum = &vhost_user_main; |
| 3003 | vhost_user_intf_t *vui = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3004 | u32 sw_if_idx = ~0; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3005 | int server_sock_fd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3006 | int rv = 0; |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3007 | vnet_hw_interface_t *hwif; |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 3008 | uword *if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3009 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3010 | if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) || |
| 3011 | hwif->dev_class_index != vhost_user_dev_class.index) |
| 3012 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3013 | |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 3014 | if (sock_filename == NULL || !(strlen (sock_filename) > 0)) |
| 3015 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 3016 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3017 | vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3018 | |
Steven | 5445f5f | 2017-04-25 16:16:00 -0700 | [diff] [blame] | 3019 | /* |
| 3020 | * Disallow changing the interface to have the same path name |
| 3021 | * as other interface |
| 3022 | */ |
| 3023 | if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename); |
| 3024 | if (if_index && (*if_index != vui->if_index)) |
| 3025 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 3026 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3027 | // First try to open server socket |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3028 | if (is_server) |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3029 | if ((rv = vhost_user_init_server_sock (sock_filename, |
| 3030 | &server_sock_fd)) != 0) |
| 3031 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3032 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3033 | vhost_user_term_if (vui); |
| 3034 | vhost_user_vui_init (vnm, vui, server_sock_fd, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3035 | sock_filename, feature_mask, &sw_if_idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3036 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3037 | if (renumber) |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3038 | vnet_interface_name_renumber (sw_if_idx, custom_dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3039 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3040 | // Process node must connect |
| 3041 | vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 3042 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3043 | return rv; |
| 3044 | } |
| 3045 | |
| 3046 | clib_error_t * |
| 3047 | vhost_user_connect_command_fn (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3048 | unformat_input_t * input, |
| 3049 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3050 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3051 | unformat_input_t _line_input, *line_input = &_line_input; |
| 3052 | u8 *sock_filename = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3053 | u32 sw_if_index; |
| 3054 | u8 is_server = 0; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3055 | u64 feature_mask = (u64) ~ (0ULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3056 | u8 renumber = 0; |
| 3057 | u32 custom_dev_instance = ~0; |
Pierre Pfister | ef65cb0 | 2016-02-19 13:52:44 +0000 | [diff] [blame] | 3058 | u8 hwaddr[6]; |
| 3059 | u8 *hw = NULL; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3060 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3061 | |
| 3062 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3063 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3064 | return 0; |
| 3065 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3066 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3067 | { |
| 3068 | if (unformat (line_input, "socket %s", &sock_filename)) |
| 3069 | ; |
| 3070 | else if (unformat (line_input, "server")) |
| 3071 | is_server = 1; |
| 3072 | else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask)) |
| 3073 | ; |
| 3074 | else |
| 3075 | if (unformat |
| 3076 | (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr)) |
| 3077 | hw = hwaddr; |
| 3078 | else if (unformat (line_input, "renumber %d", &custom_dev_instance)) |
| 3079 | { |
| 3080 | renumber = 1; |
| 3081 | } |
| 3082 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3083 | { |
| 3084 | error = clib_error_return (0, "unknown input `%U'", |
| 3085 | format_unformat_error, line_input); |
| 3086 | goto done; |
| 3087 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3088 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3089 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3090 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3091 | |
Pierre Pfister | 5afccb2 | 2016-07-25 14:32:02 +0100 | [diff] [blame] | 3092 | int rv; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3093 | if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename, |
| 3094 | is_server, &sw_if_index, feature_mask, |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3095 | renumber, custom_dev_instance, hw))) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3096 | { |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3097 | error = clib_error_return (0, "vhost_user_create_if returned %d", rv); |
| 3098 | goto done; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3099 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3100 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3101 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), |
| 3102 | sw_if_index); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3103 | |
| 3104 | done: |
| 3105 | vec_free (sock_filename); |
| 3106 | unformat_free (line_input); |
| 3107 | |
| 3108 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | clib_error_t * |
| 3112 | vhost_user_delete_command_fn (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3113 | unformat_input_t * input, |
| 3114 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3115 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3116 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3117 | u32 sw_if_index = ~0; |
Pierre Pfister | ece983d | 2016-11-21 12:52:22 +0000 | [diff] [blame] | 3118 | vnet_main_t *vnm = vnet_get_main (); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3119 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3120 | |
| 3121 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3122 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3123 | return 0; |
| 3124 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3125 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3126 | { |
| 3127 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 3128 | ; |
Pierre Pfister | ece983d | 2016-11-21 12:52:22 +0000 | [diff] [blame] | 3129 | else if (unformat |
| 3130 | (line_input, "%U", unformat_vnet_sw_interface, vnm, |
| 3131 | &sw_if_index)) |
| 3132 | { |
| 3133 | vnet_hw_interface_t *hwif = |
| 3134 | vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 3135 | if (hwif == NULL || |
| 3136 | vhost_user_dev_class.index != hwif->dev_class_index) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3137 | { |
| 3138 | error = clib_error_return (0, "Not a vhost interface"); |
| 3139 | goto done; |
| 3140 | } |
Pierre Pfister | ece983d | 2016-11-21 12:52:22 +0000 | [diff] [blame] | 3141 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3142 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3143 | { |
| 3144 | error = clib_error_return (0, "unknown input `%U'", |
| 3145 | format_unformat_error, line_input); |
| 3146 | goto done; |
| 3147 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3148 | } |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3149 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3150 | vhost_user_delete_if (vnm, vm, sw_if_index); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3151 | |
| 3152 | done: |
| 3153 | unformat_free (line_input); |
| 3154 | |
| 3155 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3158 | int |
| 3159 | vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm, |
| 3160 | vhost_user_intf_details_t ** out_vuids) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3161 | { |
| 3162 | int rv = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3163 | vhost_user_main_t *vum = &vhost_user_main; |
| 3164 | vhost_user_intf_t *vui; |
| 3165 | vhost_user_intf_details_t *r_vuids = NULL; |
| 3166 | vhost_user_intf_details_t *vuid = NULL; |
| 3167 | u32 *hw_if_indices = 0; |
| 3168 | vnet_hw_interface_t *hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3169 | u8 *s = NULL; |
| 3170 | int i; |
| 3171 | |
| 3172 | if (!out_vuids) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3173 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3174 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3175 | pool_foreach (vui, vum->vhost_user_interfaces, |
| 3176 | vec_add1 (hw_if_indices, vui->hw_if_index); |
| 3177 | ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3178 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3179 | for (i = 0; i < vec_len (hw_if_indices); i++) |
| 3180 | { |
| 3181 | hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3182 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3183 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3184 | vec_add2 (r_vuids, vuid, 1); |
| 3185 | vuid->sw_if_index = vui->sw_if_index; |
| 3186 | vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz; |
| 3187 | vuid->features = vui->features; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3188 | vuid->num_regions = vui->nregions; |
Marek Gradzki | 0578cd1 | 2017-02-13 14:19:51 +0100 | [diff] [blame] | 3189 | vuid->is_server = vui->unix_server_index != ~0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3190 | vuid->sock_errno = vui->sock_errno; |
| 3191 | strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename, |
| 3192 | ARRAY_LEN (vuid->sock_filename) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3193 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3194 | s = format (s, "%v%c", hi->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3195 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3196 | strncpy ((char *) vuid->if_name, (char *) s, |
| 3197 | ARRAY_LEN (vuid->if_name) - 1); |
| 3198 | _vec_len (s) = 0; |
| 3199 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3200 | |
| 3201 | vec_free (s); |
| 3202 | vec_free (hw_if_indices); |
| 3203 | |
| 3204 | *out_vuids = r_vuids; |
| 3205 | |
| 3206 | return rv; |
| 3207 | } |
| 3208 | |
| 3209 | clib_error_t * |
| 3210 | show_vhost_user_command_fn (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3211 | unformat_input_t * input, |
| 3212 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3213 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3214 | clib_error_t *error = 0; |
| 3215 | vnet_main_t *vnm = vnet_get_main (); |
| 3216 | vhost_user_main_t *vum = &vhost_user_main; |
| 3217 | vhost_user_intf_t *vui; |
| 3218 | u32 hw_if_index, *hw_if_indices = 0; |
| 3219 | vnet_hw_interface_t *hi; |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3220 | u16 *queue; |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3221 | u32 ci; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3222 | int i, j, q; |
| 3223 | int show_descr = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3224 | struct feat_struct |
| 3225 | { |
| 3226 | u8 bit; |
| 3227 | char *str; |
| 3228 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3229 | struct feat_struct *feat_entry; |
| 3230 | |
| 3231 | static struct feat_struct feat_array[] = { |
| 3232 | #define _(s,b) { .str = #s, .bit = b, }, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3233 | foreach_virtio_net_feature |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3234 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3235 | {.str = NULL} |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3236 | }; |
| 3237 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3238 | #define foreach_protocol_feature \ |
| 3239 | _(VHOST_USER_PROTOCOL_F_MQ) \ |
| 3240 | _(VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
| 3241 | |
| 3242 | static struct feat_struct proto_feat_array[] = { |
| 3243 | #define _(s) { .str = #s, .bit = s}, |
| 3244 | foreach_protocol_feature |
| 3245 | #undef _ |
| 3246 | {.str = NULL} |
| 3247 | }; |
| 3248 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3249 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3250 | { |
| 3251 | if (unformat |
| 3252 | (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) |
| 3253 | { |
Steven | 583f158 | 2018-04-17 08:50:28 -0700 | [diff] [blame] | 3254 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 3255 | if (vhost_user_dev_class.index != hi->dev_class_index) |
| 3256 | { |
| 3257 | error = clib_error_return (0, "unknown input `%U'", |
| 3258 | format_unformat_error, input); |
| 3259 | goto done; |
| 3260 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3261 | vec_add1 (hw_if_indices, hw_if_index); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3262 | } |
| 3263 | else if (unformat (input, "descriptors") || unformat (input, "desc")) |
| 3264 | show_descr = 1; |
| 3265 | else |
| 3266 | { |
| 3267 | error = clib_error_return (0, "unknown input `%U'", |
| 3268 | format_unformat_error, input); |
| 3269 | goto done; |
| 3270 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3271 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3272 | if (vec_len (hw_if_indices) == 0) |
| 3273 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3274 | pool_foreach (vui, vum->vhost_user_interfaces, |
| 3275 | vec_add1 (hw_if_indices, vui->hw_if_index); |
| 3276 | ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3277 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3278 | vlib_cli_output (vm, "Virtio vhost-user interfaces"); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3279 | vlib_cli_output (vm, "Global:\n coalesce frames %d time %e", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3280 | vum->coalesce_frames, vum->coalesce_time); |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3281 | vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d", |
| 3282 | vum->ifq_count); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3283 | |
| 3284 | for (i = 0; i < vec_len (hw_if_indices); i++) |
| 3285 | { |
| 3286 | hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3287 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3288 | vlib_cli_output (vm, "Interface: %s (ifindex %d)", |
| 3289 | hi->name, hw_if_indices[i]); |
| 3290 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3291 | vlib_cli_output (vm, "virtio_net_hdr_sz %d\n" |
| 3292 | " features mask (0x%llx): \n" |
| 3293 | " features (0x%llx): \n", |
| 3294 | vui->virtio_net_hdr_sz, vui->feature_mask, |
| 3295 | vui->features); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3296 | |
| 3297 | feat_entry = (struct feat_struct *) &feat_array; |
| 3298 | while (feat_entry->str) |
| 3299 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3300 | if (vui->features & (1ULL << feat_entry->bit)) |
| 3301 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 3302 | feat_entry->bit); |
| 3303 | feat_entry++; |
| 3304 | } |
| 3305 | |
| 3306 | vlib_cli_output (vm, " protocol features (0x%llx)", |
| 3307 | vui->protocol_features); |
| 3308 | feat_entry = (struct feat_struct *) &proto_feat_array; |
| 3309 | while (feat_entry->str) |
| 3310 | { |
| 3311 | if (vui->protocol_features & (1ULL << feat_entry->bit)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3312 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 3313 | feat_entry->bit); |
| 3314 | feat_entry++; |
| 3315 | } |
| 3316 | |
| 3317 | vlib_cli_output (vm, "\n"); |
| 3318 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3319 | vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n", |
| 3320 | vui->sock_filename, |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3321 | (vui->unix_server_index != ~0) ? "server" : "client", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3322 | strerror (vui->sock_errno)); |
| 3323 | |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3324 | vlib_cli_output (vm, " rx placement: "); |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3325 | |
| 3326 | vec_foreach (queue, vui->rx_queues) |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3327 | { |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3328 | vnet_main_t *vnm = vnet_get_main (); |
| 3329 | uword thread_index; |
| 3330 | vnet_hw_interface_rx_mode mode; |
| 3331 | |
| 3332 | thread_index = vnet_get_device_input_thread_index (vnm, |
| 3333 | vui->hw_if_index, |
| 3334 | *queue); |
| 3335 | vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode); |
| 3336 | vlib_cli_output (vm, " thread %d on vring %d, %U\n", |
| 3337 | thread_index, VHOST_VRING_IDX_TX (*queue), |
| 3338 | format_vnet_hw_interface_rx_mode, mode); |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | vlib_cli_output (vm, " tx placement: %s\n", |
| 3342 | vui->use_tx_spinlock ? "spin-lock" : "lock-free"); |
| 3343 | |
| 3344 | vec_foreach_index (ci, vui->per_cpu_tx_qid) |
| 3345 | { |
| 3346 | vlib_cli_output (vm, " thread %d on vring %d\n", ci, |
| 3347 | VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci])); |
| 3348 | } |
| 3349 | |
| 3350 | vlib_cli_output (vm, "\n"); |
| 3351 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3352 | vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions); |
| 3353 | |
| 3354 | if (vui->nregions) |
| 3355 | { |
| 3356 | vlib_cli_output (vm, |
| 3357 | " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n"); |
| 3358 | vlib_cli_output (vm, |
| 3359 | " ====== ===== ================== ================== ================== ================== ==================\n"); |
| 3360 | } |
| 3361 | for (j = 0; j < vui->nregions; j++) |
| 3362 | { |
| 3363 | vlib_cli_output (vm, |
| 3364 | " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", |
| 3365 | j, vui->region_mmap_fd[j], |
| 3366 | vui->regions[j].guest_phys_addr, |
| 3367 | vui->regions[j].memory_size, |
| 3368 | vui->regions[j].userspace_addr, |
| 3369 | vui->regions[j].mmap_offset, |
| 3370 | pointer_to_uword (vui->region_mmap_addr[j])); |
| 3371 | } |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3372 | for (q = 0; q < VHOST_VRING_MAX_N; q++) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3373 | { |
Pierre Pfister | e21c528 | 2016-09-21 08:04:59 +0100 | [diff] [blame] | 3374 | if (!vui->vrings[q].started) |
| 3375 | continue; |
| 3376 | |
| 3377 | vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q, |
| 3378 | (q & 1) ? "RX" : "TX", |
| 3379 | vui->vrings[q].enabled ? "" : " disabled"); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3380 | |
| 3381 | vlib_cli_output (vm, |
| 3382 | " qsz %d last_avail_idx %d last_used_idx %d\n", |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 3383 | vui->vrings[q].qsz_mask + 1, |
| 3384 | vui->vrings[q].last_avail_idx, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3385 | vui->vrings[q].last_used_idx); |
| 3386 | |
| 3387 | if (vui->vrings[q].avail && vui->vrings[q].used) |
| 3388 | vlib_cli_output (vm, |
| 3389 | " avail.flags %x avail.idx %d used.flags %x used.idx %d\n", |
| 3390 | vui->vrings[q].avail->flags, |
| 3391 | vui->vrings[q].avail->idx, |
| 3392 | vui->vrings[q].used->flags, |
| 3393 | vui->vrings[q].used->idx); |
| 3394 | |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3395 | int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx); |
| 3396 | int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3397 | vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3398 | kickfd, callfd, vui->vrings[q].errfd); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3399 | |
| 3400 | if (show_descr) |
| 3401 | { |
| 3402 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 3403 | vlib_cli_output (vm, |
| 3404 | " id addr len flags next user_addr\n"); |
| 3405 | vlib_cli_output (vm, |
| 3406 | " ===== ================== ===== ====== ===== ==================\n"); |
Steven | 9787889 | 2017-08-29 09:23:26 -0700 | [diff] [blame] | 3407 | for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3408 | { |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 3409 | u32 mem_hint = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3410 | vlib_cli_output (vm, |
| 3411 | " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", |
| 3412 | j, vui->vrings[q].desc[j].addr, |
| 3413 | vui->vrings[q].desc[j].len, |
| 3414 | vui->vrings[q].desc[j].flags, |
| 3415 | vui->vrings[q].desc[j].next, |
| 3416 | pointer_to_uword (map_guest_mem |
| 3417 | (vui, |
Pierre Pfister | ba1d046 | 2016-07-27 16:38:20 +0100 | [diff] [blame] | 3418 | vui->vrings[q].desc[j]. |
Pierre Pfister | 11f9205 | 2016-09-21 08:08:55 +0100 | [diff] [blame] | 3419 | addr, &mem_hint))); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | vlib_cli_output (vm, "\n"); |
| 3424 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3425 | done: |
| 3426 | vec_free (hw_if_indices); |
| 3427 | return error; |
| 3428 | } |
| 3429 | |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3430 | /* |
| 3431 | * CLI functions |
| 3432 | */ |
| 3433 | |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3434 | /*? |
| 3435 | * Create a vHost User interface. Once created, a new virtual interface |
| 3436 | * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>' |
| 3437 | * is the next free index. |
| 3438 | * |
| 3439 | * There are several parameters associated with a vHost interface: |
| 3440 | * |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3441 | * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor |
| 3442 | * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will |
| 3443 | * create the socket if it does not already exist. If in '<em>client</em>' mode, |
| 3444 | * hypervisor will create the socket if it does not already exist. The VPP code |
| 3445 | * is indifferent to the file location. However, if SELinux is enabled, then the |
| 3446 | * socket needs to be created in '<em>/var/run/vpp/</em>'. |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3447 | * |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3448 | * - <b>server</b> - Optional flag to indicate that VPP should be the server for |
| 3449 | * the linux socket. If not provided, VPP will be the client. In '<em>server</em>' |
| 3450 | * mode, the VM can be reset without tearing down the vHost Interface. In |
| 3451 | * '<em>client</em>' mode, VPP can be reset without bringing down the VM and |
| 3452 | * tearing down the vHost Interface. |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3453 | * |
| 3454 | * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3455 | * startup. <b>This is intended for degugging only.</b> It is recommended that this |
| 3456 | * parameter not be used except by experienced users. By default, all supported |
| 3457 | * features will be advertised. Otherwise, provide the set of features desired. |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3458 | * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF |
| 3459 | * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ |
| 3460 | * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE |
| 3461 | * - 0x000400000 (22) - VIRTIO_NET_F_MQ |
| 3462 | * - 0x004000000 (26) - VHOST_F_LOG_ALL |
| 3463 | * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT |
| 3464 | * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC |
| 3465 | * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES |
| 3466 | * - 0x100000000 (32) - VIRTIO_F_VERSION_1 |
| 3467 | * |
| 3468 | * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either |
| 3469 | * X:X:X:X:X:X unix or X.X.X cisco format. |
| 3470 | * |
| 3471 | * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance |
| 3472 | * in the name to be specified. If instance already exists, name will be used |
| 3473 | * anyway and multiple instances will have the same name. Use with caution. |
| 3474 | * |
| 3475 | * @cliexpar |
| 3476 | * Example of how to create a vhost interface with VPP as the client and all features enabled: |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3477 | * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock} |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3478 | * VirtualEthernet0/0/0 |
| 3479 | * @cliexend |
| 3480 | * Example of how to create a vhost interface with VPP as the server and with just |
| 3481 | * multiple queues enabled: |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3482 | * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000} |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3483 | * VirtualEthernet0/0/1 |
| 3484 | * @cliexend |
| 3485 | * Once the vHost interface is created, enable the interface using: |
| 3486 | * @cliexcmd{set interface state VirtualEthernet0/0/0 up} |
| 3487 | ?*/ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3488 | /* *INDENT-OFF* */ |
| 3489 | VLIB_CLI_COMMAND (vhost_user_connect_command, static) = { |
| 3490 | .path = "create vhost-user", |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 3491 | .short_help = "create vhost-user socket <socket-filename> [server] " |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 3492 | "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ", |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3493 | .function = vhost_user_connect_command_fn, |
| 3494 | }; |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3495 | /* *INDENT-ON* */ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3496 | |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3497 | /*? |
| 3498 | * Delete a vHost User interface using the interface name or the |
Dave Barach | 13ad1f0 | 2017-03-26 19:36:18 -0400 | [diff] [blame] | 3499 | * software interface index. Use the '<em>show interface</em>' |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3500 | * command to determine the software interface index. On deletion, |
| 3501 | * the linux socket will not be deleted. |
| 3502 | * |
| 3503 | * @cliexpar |
| 3504 | * Example of how to delete a vhost interface by name: |
| 3505 | * @cliexcmd{delete vhost-user VirtualEthernet0/0/1} |
| 3506 | * Example of how to delete a vhost interface by software interface index: |
| 3507 | * @cliexcmd{delete vhost-user sw_if_index 1} |
| 3508 | ?*/ |
| 3509 | /* *INDENT-OFF* */ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3510 | VLIB_CLI_COMMAND (vhost_user_delete_command, static) = { |
| 3511 | .path = "delete vhost-user", |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3512 | .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}", |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3513 | .function = vhost_user_delete_command_fn, |
| 3514 | }; |
| 3515 | |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3516 | /*? |
| 3517 | * Display the attributes of a single vHost User interface (provide interface |
| 3518 | * name), multiple vHost User interfaces (provide a list of interface names seperated |
| 3519 | * by spaces) or all Vhost User interfaces (omit an interface name to display all |
| 3520 | * vHost interfaces). |
| 3521 | * |
| 3522 | * @cliexpar |
| 3523 | * @parblock |
| 3524 | * Example of how to display a vhost interface: |
| 3525 | * @cliexstart{show vhost-user VirtualEthernet0/0/0} |
| 3526 | * Virtio vhost-user interfaces |
| 3527 | * Global: |
| 3528 | * coalesce frames 32 time 1e-3 |
| 3529 | * Interface: VirtualEthernet0/0/0 (ifindex 1) |
| 3530 | * virtio_net_hdr_sz 12 |
| 3531 | * features mask (0xffffffffffffffff): |
| 3532 | * features (0x50408000): |
| 3533 | * VIRTIO_NET_F_MRG_RXBUF (15) |
| 3534 | * VIRTIO_NET_F_MQ (22) |
| 3535 | * VIRTIO_F_INDIRECT_DESC (28) |
| 3536 | * VHOST_USER_F_PROTOCOL_FEATURES (30) |
| 3537 | * protocol features (0x3) |
| 3538 | * VHOST_USER_PROTOCOL_F_MQ (0) |
| 3539 | * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1) |
| 3540 | * |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3541 | * socket filename /var/run/vpp/vhost1.sock type client errno "Success" |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3542 | * |
| 3543 | * rx placement: |
| 3544 | * thread 1 on vring 1 |
| 3545 | * thread 1 on vring 5 |
| 3546 | * thread 2 on vring 3 |
| 3547 | * thread 2 on vring 7 |
| 3548 | * tx placement: spin-lock |
| 3549 | * thread 0 on vring 0 |
| 3550 | * thread 1 on vring 2 |
| 3551 | * thread 2 on vring 0 |
| 3552 | * |
| 3553 | * Memory regions (total 2) |
| 3554 | * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr |
| 3555 | * ====== ===== ================== ================== ================== ================== ================== |
| 3556 | * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000 |
| 3557 | * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000 |
| 3558 | * |
| 3559 | * Virtqueue 0 (TX) |
| 3560 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3561 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 3562 | * kickfd 62 callfd 64 errfd -1 |
| 3563 | * |
| 3564 | * Virtqueue 1 (RX) |
| 3565 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3566 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3567 | * kickfd 65 callfd 66 errfd -1 |
| 3568 | * |
| 3569 | * Virtqueue 2 (TX) |
| 3570 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3571 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 3572 | * kickfd 63 callfd 70 errfd -1 |
| 3573 | * |
| 3574 | * Virtqueue 3 (RX) |
| 3575 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3576 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3577 | * kickfd 72 callfd 74 errfd -1 |
| 3578 | * |
| 3579 | * Virtqueue 4 (TX disabled) |
| 3580 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3581 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3582 | * kickfd 76 callfd 78 errfd -1 |
| 3583 | * |
| 3584 | * Virtqueue 5 (RX disabled) |
| 3585 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3586 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3587 | * kickfd 80 callfd 82 errfd -1 |
| 3588 | * |
| 3589 | * Virtqueue 6 (TX disabled) |
| 3590 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3591 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3592 | * kickfd 84 callfd 86 errfd -1 |
| 3593 | * |
| 3594 | * Virtqueue 7 (RX disabled) |
| 3595 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3596 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 3597 | * kickfd 88 callfd 90 errfd -1 |
| 3598 | * |
| 3599 | * @cliexend |
| 3600 | * |
| 3601 | * The optional '<em>descriptors</em>' parameter will display the same output as |
| 3602 | * the previous example but will include the descriptor table for each queue. |
| 3603 | * The output is truncated below: |
| 3604 | * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors} |
| 3605 | * Virtio vhost-user interfaces |
| 3606 | * Global: |
| 3607 | * coalesce frames 32 time 1e-3 |
| 3608 | * Interface: VirtualEthernet0/0/0 (ifindex 1) |
| 3609 | * virtio_net_hdr_sz 12 |
| 3610 | * features mask (0xffffffffffffffff): |
| 3611 | * features (0x50408000): |
| 3612 | * VIRTIO_NET_F_MRG_RXBUF (15) |
| 3613 | * VIRTIO_NET_F_MQ (22) |
| 3614 | * : |
| 3615 | * Virtqueue 0 (TX) |
| 3616 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3617 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 3618 | * kickfd 62 callfd 64 errfd -1 |
| 3619 | * |
| 3620 | * descriptor table: |
| 3621 | * id addr len flags next user_addr |
| 3622 | * ===== ================== ===== ====== ===== ================== |
| 3623 | * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974 |
| 3624 | * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034 |
| 3625 | * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4 |
| 3626 | * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4 |
| 3627 | * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474 |
| 3628 | * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34 |
| 3629 | * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4 |
| 3630 | * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4 |
| 3631 | * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74 |
| 3632 | * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634 |
| 3633 | * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4 |
| 3634 | * : |
| 3635 | * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000 |
| 3636 | * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000 |
| 3637 | * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000 |
| 3638 | * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000 |
| 3639 | * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000 |
| 3640 | * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000 |
| 3641 | * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000 |
| 3642 | * |
| 3643 | * Virtqueue 1 (RX) |
| 3644 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 3645 | * : |
| 3646 | * @cliexend |
| 3647 | * @endparblock |
| 3648 | ?*/ |
| 3649 | /* *INDENT-OFF* */ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3650 | VLIB_CLI_COMMAND (show_vhost_user_command, static) = { |
| 3651 | .path = "show vhost-user", |
Billy McFall | a92501a | 2016-11-23 12:45:29 -0500 | [diff] [blame] | 3652 | .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]", |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3653 | .function = show_vhost_user_command_fn, |
| 3654 | }; |
| 3655 | /* *INDENT-ON* */ |
Damjan Marion | 8d281b3 | 2016-08-24 14:32:39 +0200 | [diff] [blame] | 3656 | |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3657 | clib_error_t * |
| 3658 | debug_vhost_user_command_fn (vlib_main_t * vm, |
| 3659 | unformat_input_t * input, |
| 3660 | vlib_cli_command_t * cmd) |
| 3661 | { |
| 3662 | unformat_input_t _line_input, *line_input = &_line_input; |
| 3663 | clib_error_t *error = NULL; |
| 3664 | vhost_user_main_t *vum = &vhost_user_main; |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3665 | u8 onoff = 0; |
| 3666 | u8 input_found = 0; |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3667 | |
| 3668 | /* Get a line of input. */ |
| 3669 | if (!unformat_user (input, unformat_line_input, line_input)) |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3670 | return clib_error_return (0, "missing argument"); |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3671 | |
| 3672 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3673 | { |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3674 | if (input_found) |
| 3675 | { |
| 3676 | error = clib_error_return (0, "unknown input `%U'", |
| 3677 | format_unformat_error, line_input); |
| 3678 | goto done; |
| 3679 | } |
| 3680 | |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3681 | if (unformat (line_input, "on")) |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3682 | { |
| 3683 | input_found = 1; |
| 3684 | onoff = 1; |
| 3685 | } |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3686 | else if (unformat (line_input, "off")) |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3687 | { |
| 3688 | input_found = 1; |
| 3689 | onoff = 0; |
| 3690 | } |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3691 | else |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3692 | { |
| 3693 | error = clib_error_return (0, "unknown input `%U'", |
| 3694 | format_unformat_error, line_input); |
| 3695 | goto done; |
| 3696 | } |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Steven | 2ee2d57 | 2017-07-21 16:38:41 -0700 | [diff] [blame] | 3699 | vum->debug = onoff; |
| 3700 | |
| 3701 | done: |
Steven | 388e51a | 2017-06-01 12:49:23 -0700 | [diff] [blame] | 3702 | unformat_free (line_input); |
| 3703 | |
| 3704 | return error; |
| 3705 | } |
| 3706 | |
| 3707 | /* *INDENT-OFF* */ |
| 3708 | VLIB_CLI_COMMAND (debug_vhost_user_command, static) = { |
| 3709 | .path = "debug vhost-user", |
| 3710 | .short_help = "debug vhost-user <on | off>", |
| 3711 | .function = debug_vhost_user_command_fn, |
| 3712 | }; |
| 3713 | /* *INDENT-ON* */ |
| 3714 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3715 | static clib_error_t * |
| 3716 | vhost_user_config (vlib_main_t * vm, unformat_input_t * input) |
| 3717 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3718 | vhost_user_main_t *vum = &vhost_user_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3719 | |
| 3720 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3721 | { |
| 3722 | if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3723 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3724 | else if (unformat (input, "coalesce-time %f", &vum->coalesce_time)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3725 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3726 | else if (unformat (input, "dont-dump-memory")) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3727 | vum->dont_dump_vhost_user_memory = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3728 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3729 | return clib_error_return (0, "unknown input `%U'", |
| 3730 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | return 0; |
| 3734 | } |
| 3735 | |
| 3736 | /* vhost-user { ... } configuration. */ |
| 3737 | VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user"); |
| 3738 | |
| 3739 | void |
| 3740 | vhost_user_unmap_all (void) |
| 3741 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3742 | vhost_user_main_t *vum = &vhost_user_main; |
| 3743 | vhost_user_intf_t *vui; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3744 | |
| 3745 | if (vum->dont_dump_vhost_user_memory) |
| 3746 | { |
Pierre Pfister | dbb3c25 | 2016-11-22 10:33:34 +0000 | [diff] [blame] | 3747 | pool_foreach (vui, vum->vhost_user_interfaces, |
| 3748 | unmap_all_mem_regions (vui); |
| 3749 | ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3750 | } |
| 3751 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 3752 | |
| 3753 | /* |
| 3754 | * fd.io coding-style-patch-verification: ON |
| 3755 | * |
| 3756 | * Local Variables: |
| 3757 | * eval: (c-set-style "gnu") |
| 3758 | * End: |
| 3759 | */ |