1 /* 2 * vhost-user.c 3 * 4 * Copyright (c) 2013 Virtual Open Systems Sarl. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 * 9 */ 10 11 #include "qemu/osdep.h" 12 #include "clients.h" 13 #include "net/vhost_net.h" 14 #include "net/vhost-user.h" 15 #include "sysemu/char.h" 16 #include "qemu/config-file.h" 17 #include "qemu/error-report.h" 18 #include "qmp-commands.h" 19 #include "trace.h" 20 21 typedef struct VhostUserState { 22 NetClientState nc; 23 CharDriverState *chr; 24 VHostNetState *vhost_net; 25 int watch; 26 uint64_t acked_features; 27 } VhostUserState; 28 29 typedef struct VhostUserChardevProps { 30 bool is_socket; 31 bool is_unix; 32 } VhostUserChardevProps; 33 34 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc) 35 { 36 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); 37 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 38 return s->vhost_net; 39 } 40 41 uint64_t vhost_user_get_acked_features(NetClientState *nc) 42 { 43 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); 44 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 45 return s->acked_features; 46 } 47 48 static int vhost_user_running(VhostUserState *s) 49 { 50 return (s->vhost_net) ? 1 : 0; 51 } 52 53 static void vhost_user_stop(int queues, NetClientState *ncs[]) 54 { 55 VhostUserState *s; 56 int i; 57 58 for (i = 0; i < queues; i++) { 59 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 60 61 s = DO_UPCAST(VhostUserState, nc, ncs[i]); 62 if (!vhost_user_running(s)) { 63 continue; 64 } 65 66 if (s->vhost_net) { 67 /* save acked features */ 68 s->acked_features = vhost_net_get_acked_features(s->vhost_net); 69 vhost_net_cleanup(s->vhost_net); 70 s->vhost_net = NULL; 71 } 72 } 73 } 74 75 static int vhost_user_start(int queues, NetClientState *ncs[]) 76 { 77 VhostNetOptions options; 78 VhostUserState *s; 79 int max_queues; 80 int i; 81 82 options.backend_type = VHOST_BACKEND_TYPE_USER; 83 84 for (i = 0; i < queues; i++) { 85 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 86 87 s = DO_UPCAST(VhostUserState, nc, ncs[i]); 88 if (vhost_user_running(s)) { 89 continue; 90 } 91 92 options.net_backend = ncs[i]; 93 options.opaque = s->chr; 94 s->vhost_net = vhost_net_init(&options); 95 if (!s->vhost_net) { 96 error_report("failed to init vhost_net for queue %d", i); 97 goto err; 98 } 99 100 if (i == 0) { 101 max_queues = vhost_net_get_max_queues(s->vhost_net); 102 if (queues > max_queues) { 103 error_report("you are asking more queues than supported: %d", 104 max_queues); 105 goto err; 106 } 107 } 108 } 109 110 return 0; 111 112 err: 113 vhost_user_stop(i + 1, ncs); 114 return -1; 115 } 116 117 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf, 118 size_t size) 119 { 120 /* In case of RARP (message size is 60) notify backup to send a fake RARP. 121 This fake RARP will be sent by backend only for guest 122 without GUEST_ANNOUNCE capability. 123 */ 124 if (size == 60) { 125 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); 126 int r; 127 static int display_rarp_failure = 1; 128 char mac_addr[6]; 129 130 /* extract guest mac address from the RARP message */ 131 memcpy(mac_addr, &buf[6], 6); 132 133 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr); 134 135 if ((r != 0) && (display_rarp_failure)) { 136 fprintf(stderr, 137 "Vhost user backend fails to broadcast fake RARP\n"); 138 fflush(stderr); 139 display_rarp_failure = 0; 140 } 141 } 142 143 return size; 144 } 145 146 static void vhost_user_cleanup(NetClientState *nc) 147 { 148 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); 149 150 if (s->vhost_net) { 151 vhost_net_cleanup(s->vhost_net); 152 s->vhost_net = NULL; 153 } 154 155 qemu_purge_queued_packets(nc); 156 } 157 158 static bool vhost_user_has_vnet_hdr(NetClientState *nc) 159 { 160 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 161 162 return true; 163 } 164 165 static bool vhost_user_has_ufo(NetClientState *nc) 166 { 167 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 168 169 return true; 170 } 171 172 static NetClientInfo net_vhost_user_info = { 173 .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER, 174 .size = sizeof(VhostUserState), 175 .receive = vhost_user_receive, 176 .cleanup = vhost_user_cleanup, 177 .has_vnet_hdr = vhost_user_has_vnet_hdr, 178 .has_ufo = vhost_user_has_ufo, 179 }; 180 181 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond, 182 void *opaque) 183 { 184 VhostUserState *s = opaque; 185 uint8_t buf[1]; 186 187 /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be 188 * raised as a side-effect of the read. 189 */ 190 qemu_chr_fe_read_all(s->chr, buf, sizeof(buf)); 191 192 return FALSE; 193 } 194 195 static void net_vhost_user_event(void *opaque, int event) 196 { 197 const char *name = opaque; 198 NetClientState *ncs[MAX_QUEUE_NUM]; 199 VhostUserState *s; 200 Error *err = NULL; 201 int queues; 202 203 queues = qemu_find_net_clients_except(name, ncs, 204 NET_CLIENT_OPTIONS_KIND_NIC, 205 MAX_QUEUE_NUM); 206 assert(queues < MAX_QUEUE_NUM); 207 208 s = DO_UPCAST(VhostUserState, nc, ncs[0]); 209 trace_vhost_user_event(s->chr->label, event); 210 switch (event) { 211 case CHR_EVENT_OPENED: 212 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP, 213 net_vhost_user_watch, s); 214 if (vhost_user_start(queues, ncs) < 0) { 215 qemu_chr_disconnect(s->chr); 216 return; 217 } 218 qmp_set_link(name, true, &err); 219 break; 220 case CHR_EVENT_CLOSED: 221 qmp_set_link(name, false, &err); 222 vhost_user_stop(queues, ncs); 223 g_source_remove(s->watch); 224 s->watch = 0; 225 break; 226 } 227 228 if (err) { 229 error_report_err(err); 230 } 231 } 232 233 static int net_vhost_user_init(NetClientState *peer, const char *device, 234 const char *name, CharDriverState *chr, 235 int queues) 236 { 237 NetClientState *nc; 238 VhostUserState *s; 239 int i; 240 241 assert(name); 242 assert(queues > 0); 243 244 for (i = 0; i < queues; i++) { 245 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name); 246 247 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s", 248 i, chr->label); 249 250 nc->queue_index = i; 251 252 s = DO_UPCAST(VhostUserState, nc, nc); 253 s->chr = chr; 254 } 255 256 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name); 257 258 return 0; 259 } 260 261 static int net_vhost_chardev_opts(void *opaque, 262 const char *name, const char *value, 263 Error **errp) 264 { 265 VhostUserChardevProps *props = opaque; 266 267 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) { 268 props->is_socket = true; 269 } else if (strcmp(name, "path") == 0) { 270 props->is_unix = true; 271 } else if (strcmp(name, "server") == 0) { 272 } else { 273 error_setg(errp, 274 "vhost-user does not support a chardev with option %s=%s", 275 name, value); 276 return -1; 277 } 278 return 0; 279 } 280 281 static CharDriverState *net_vhost_parse_chardev( 282 const NetdevVhostUserOptions *opts, Error **errp) 283 { 284 CharDriverState *chr = qemu_chr_find(opts->chardev); 285 VhostUserChardevProps props; 286 287 if (chr == NULL) { 288 error_setg(errp, "chardev \"%s\" not found", opts->chardev); 289 return NULL; 290 } 291 292 /* inspect chardev opts */ 293 memset(&props, 0, sizeof(props)); 294 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) { 295 return NULL; 296 } 297 298 if (!props.is_socket || !props.is_unix) { 299 error_setg(errp, "chardev \"%s\" is not a unix socket", 300 opts->chardev); 301 return NULL; 302 } 303 304 qemu_chr_fe_claim_no_fail(chr); 305 306 return chr; 307 } 308 309 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp) 310 { 311 const char *name = opaque; 312 const char *driver, *netdev; 313 const char virtio_name[] = "virtio-net-"; 314 315 driver = qemu_opt_get(opts, "driver"); 316 netdev = qemu_opt_get(opts, "netdev"); 317 318 if (!driver || !netdev) { 319 return 0; 320 } 321 322 if (strcmp(netdev, name) == 0 && 323 strncmp(driver, virtio_name, strlen(virtio_name)) != 0) { 324 error_setg(errp, "vhost-user requires frontend driver virtio-net-*"); 325 return -1; 326 } 327 328 return 0; 329 } 330 331 int net_init_vhost_user(const NetClientOptions *opts, const char *name, 332 NetClientState *peer, Error **errp) 333 { 334 int queues; 335 const NetdevVhostUserOptions *vhost_user_opts; 336 CharDriverState *chr; 337 338 assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); 339 vhost_user_opts = opts->u.vhost_user.data; 340 341 chr = net_vhost_parse_chardev(vhost_user_opts, errp); 342 if (!chr) { 343 return -1; 344 } 345 346 /* verify net frontend */ 347 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net, 348 (char *)name, errp)) { 349 return -1; 350 } 351 352 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1; 353 if (queues < 1 || queues > MAX_QUEUE_NUM) { 354 error_setg(errp, 355 "vhost-user number of queues must be in range [1, %d]", 356 MAX_QUEUE_NUM); 357 return -1; 358 } 359 360 return net_vhost_user_init(peer, "vhost_user", name, chr, queues); 361 } 362