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