blob: a88dfe065512e3fe4ecbc689ab7d79bc74b3ed20 [file] [log] [blame]
Nikolay Nikolaevd314f582014-05-27 15:06:29 +03001/*
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
Peter Maydell2744d922016-01-29 17:50:00 +000011#include "qemu/osdep.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030012#include "clients.h"
13#include "net/vhost_net.h"
14#include "net/vhost-user.h"
15#include "sysemu/char.h"
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030016#include "qemu/config-file.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030017#include "qemu/error-report.h"
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080018#include "qmp-commands.h"
Marc-André Lureau69b32a62015-10-09 17:17:30 +020019#include "trace.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030020
21typedef struct VhostUserState {
22 NetClientState nc;
23 CharDriverState *chr;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030024 VHostNetState *vhost_net;
Paolo Bonzini6f1de6b2016-06-20 15:02:40 +020025 guint watch;
Marc-André Lureaua4632152016-06-06 18:45:05 +020026 uint64_t acked_features;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030027} VhostUserState;
28
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030029typedef struct VhostUserChardevProps {
30 bool is_socket;
31 bool is_unix;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030032} VhostUserChardevProps;
33
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030034VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35{
36 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030037 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030038 return s->vhost_net;
39}
40
Marc-André Lureaua4632152016-06-06 18:45:05 +020041uint64_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
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030048static int vhost_user_running(VhostUserState *s)
49{
50 return (s->vhost_net) ? 1 : 0;
51}
52
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080053static void vhost_user_stop(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030054{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080055 VhostUserState *s;
56 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030057
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080058 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) {
Marc-André Lureaua4632152016-06-06 18:45:05 +020067 /* save acked features */
68 s->acked_features = vhost_net_get_acked_features(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080069 vhost_net_cleanup(s->vhost_net);
70 s->vhost_net = NULL;
71 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030072 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030073}
74
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080075static int vhost_user_start(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030076{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080077 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;
Jason Wang69e87b32016-07-06 09:57:55 +080094 options.busyloop_timeout = 0;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080095 s->vhost_net = vhost_net_init(&options);
96 if (!s->vhost_net) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010097 error_report("failed to init vhost_net for queue %d", i);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080098 goto err;
99 }
100
101 if (i == 0) {
102 max_queues = vhost_net_get_max_queues(s->vhost_net);
103 if (queues > max_queues) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100104 error_report("you are asking more queues than supported: %d",
105 max_queues);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800106 goto err;
107 }
108 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300109 }
110
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800111 return 0;
112
113err:
114 vhost_user_stop(i + 1, ncs);
115 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300116}
117
Thibaut Colletf6f56292015-10-09 17:17:31 +0200118static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
119 size_t size)
120{
Thibaut Collet3e866362015-10-09 17:17:32 +0200121 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
122 This fake RARP will be sent by backend only for guest
123 without GUEST_ANNOUNCE capability.
Thibaut Colletf6f56292015-10-09 17:17:31 +0200124 */
Thibaut Collet3e866362015-10-09 17:17:32 +0200125 if (size == 60) {
126 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
127 int r;
128 static int display_rarp_failure = 1;
129 char mac_addr[6];
130
131 /* extract guest mac address from the RARP message */
132 memcpy(mac_addr, &buf[6], 6);
133
134 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
135
136 if ((r != 0) && (display_rarp_failure)) {
137 fprintf(stderr,
138 "Vhost user backend fails to broadcast fake RARP\n");
139 fflush(stderr);
140 display_rarp_failure = 0;
141 }
142 }
143
Thibaut Colletf6f56292015-10-09 17:17:31 +0200144 return size;
145}
146
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300147static void vhost_user_cleanup(NetClientState *nc)
148{
149 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
150
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800151 if (s->vhost_net) {
152 vhost_net_cleanup(s->vhost_net);
153 s->vhost_net = NULL;
154 }
Paolo Bonzini25f0d2a2016-06-29 15:15:33 +0200155 if (s->chr) {
156 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
157 qemu_chr_fe_release(s->chr);
158 s->chr = NULL;
159 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800160
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300161 qemu_purge_queued_packets(nc);
162}
163
164static bool vhost_user_has_vnet_hdr(NetClientState *nc)
165{
166 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
167
168 return true;
169}
170
171static bool vhost_user_has_ufo(NetClientState *nc)
172{
173 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
174
175 return true;
176}
177
178static NetClientInfo net_vhost_user_info = {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300179 .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300180 .size = sizeof(VhostUserState),
Thibaut Colletf6f56292015-10-09 17:17:31 +0200181 .receive = vhost_user_receive,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300182 .cleanup = vhost_user_cleanup,
183 .has_vnet_hdr = vhost_user_has_vnet_hdr,
184 .has_ufo = vhost_user_has_ufo,
185};
186
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200187static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
188 void *opaque)
189{
190 VhostUserState *s = opaque;
191 uint8_t buf[1];
192
193 /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be
194 * raised as a side-effect of the read.
195 */
196 qemu_chr_fe_read_all(s->chr, buf, sizeof(buf));
197
198 return FALSE;
199}
200
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300201static void net_vhost_user_event(void *opaque, int event)
202{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800203 const char *name = opaque;
204 NetClientState *ncs[MAX_QUEUE_NUM];
205 VhostUserState *s;
206 Error *err = NULL;
207 int queues;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300208
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800209 queues = qemu_find_net_clients_except(name, ncs,
210 NET_CLIENT_OPTIONS_KIND_NIC,
211 MAX_QUEUE_NUM);
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100212 assert(queues < MAX_QUEUE_NUM);
213
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800214 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
Marc-André Lureau69b32a62015-10-09 17:17:30 +0200215 trace_vhost_user_event(s->chr->label, event);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300216 switch (event) {
217 case CHR_EVENT_OPENED:
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200218 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
219 net_vhost_user_watch, s);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800220 if (vhost_user_start(queues, ncs) < 0) {
Marc-André Lureau0d572af2016-06-06 18:45:03 +0200221 qemu_chr_disconnect(s->chr);
222 return;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800223 }
224 qmp_set_link(name, true, &err);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300225 break;
226 case CHR_EVENT_CLOSED:
Wen Congyangd39c87d2015-11-11 14:53:29 +0800227 qmp_set_link(name, false, &err);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800228 vhost_user_stop(queues, ncs);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200229 g_source_remove(s->watch);
230 s->watch = 0;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300231 break;
232 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800233
234 if (err) {
235 error_report_err(err);
236 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300237}
238
239static int net_vhost_user_init(NetClientState *peer, const char *device,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800240 const char *name, CharDriverState *chr,
241 int queues)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300242{
243 NetClientState *nc;
244 VhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800245 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300246
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100247 assert(name);
248 assert(queues > 0);
249
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800250 for (i = 0; i < queues; i++) {
251 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300252
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800253 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
254 i, chr->label);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300255
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800256 nc->queue_index = i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300257
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800258 s = DO_UPCAST(VhostUserState, nc, nc);
259 s->chr = chr;
260 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300261
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100262 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
Michael S. Tsirkind345ed22015-07-15 13:47:31 +0300263
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300264 return 0;
265}
266
Markus Armbruster71df1d82015-03-12 08:40:25 +0100267static int net_vhost_chardev_opts(void *opaque,
268 const char *name, const char *value,
269 Error **errp)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300270{
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300271 VhostUserChardevProps *props = opaque;
272
273 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
274 props->is_socket = true;
275 } else if (strcmp(name, "path") == 0) {
276 props->is_unix = true;
277 } else if (strcmp(name, "server") == 0) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300278 } else {
Markus Armbruster81904832015-03-13 14:17:16 +0100279 error_setg(errp,
280 "vhost-user does not support a chardev with option %s=%s",
281 name, value);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300282 return -1;
283 }
284 return 0;
285}
286
Markus Armbruster81904832015-03-13 14:17:16 +0100287static CharDriverState *net_vhost_parse_chardev(
288 const NetdevVhostUserOptions *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300289{
290 CharDriverState *chr = qemu_chr_find(opts->chardev);
291 VhostUserChardevProps props;
292
293 if (chr == NULL) {
Markus Armbruster81904832015-03-13 14:17:16 +0100294 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300295 return NULL;
296 }
297
298 /* inspect chardev opts */
299 memset(&props, 0, sizeof(props));
Markus Armbruster81904832015-03-13 14:17:16 +0100300 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300301 return NULL;
302 }
303
304 if (!props.is_socket || !props.is_unix) {
Markus Armbruster81904832015-03-13 14:17:16 +0100305 error_setg(errp, "chardev \"%s\" is not a unix socket",
306 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300307 return NULL;
308 }
309
310 qemu_chr_fe_claim_no_fail(chr);
311
312 return chr;
313}
314
Markus Armbruster28d0de72015-03-13 13:35:14 +0100315static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300316{
317 const char *name = opaque;
318 const char *driver, *netdev;
319 const char virtio_name[] = "virtio-net-";
320
321 driver = qemu_opt_get(opts, "driver");
322 netdev = qemu_opt_get(opts, "netdev");
323
324 if (!driver || !netdev) {
325 return 0;
326 }
327
328 if (strcmp(netdev, name) == 0 &&
329 strncmp(driver, virtio_name, strlen(virtio_name)) != 0) {
Markus Armbruster81904832015-03-13 14:17:16 +0100330 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300331 return -1;
332 }
333
334 return 0;
335}
336
337int net_init_vhost_user(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200338 NetClientState *peer, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300339{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800340 int queues;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300341 const NetdevVhostUserOptions *vhost_user_opts;
342 CharDriverState *chr;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300343
Eric Blake8d0bcba2015-10-26 16:34:56 -0600344 assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
Eric Blake32bafa82016-03-17 16:48:37 -0600345 vhost_user_opts = opts->u.vhost_user.data;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300346
Markus Armbruster81904832015-03-13 14:17:16 +0100347 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300348 if (!chr) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300349 return -1;
350 }
351
352 /* verify net frontend */
353 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
Markus Armbruster81904832015-03-13 14:17:16 +0100354 (char *)name, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300355 return -1;
356 }
357
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800358 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300359 if (queues < 1 || queues > MAX_QUEUE_NUM) {
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200360 error_setg(errp,
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300361 "vhost-user number of queues must be in range [1, %d]",
362 MAX_QUEUE_NUM);
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200363 return -1;
364 }
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300365
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800366 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300367}