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