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