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