blob: 4a7fd5fbd5e1c750b2a7258d53125b8cefaae1b6 [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;
Tetsuya Mukawaa6553592016-06-06 18:44:59 +020025 int watch;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030026} VhostUserState;
27
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030028typedef struct VhostUserChardevProps {
29 bool is_socket;
30 bool is_unix;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030031} VhostUserChardevProps;
32
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030033VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
34{
35 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030036 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030037 return s->vhost_net;
38}
39
40static int vhost_user_running(VhostUserState *s)
41{
42 return (s->vhost_net) ? 1 : 0;
43}
44
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080045static void vhost_user_stop(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030046{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080047 VhostUserState *s;
48 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030049
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080050 for (i = 0; i < queues; i++) {
51 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
52
53 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
54 if (!vhost_user_running(s)) {
55 continue;
56 }
57
58 if (s->vhost_net) {
59 vhost_net_cleanup(s->vhost_net);
60 s->vhost_net = NULL;
61 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030062 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030063}
64
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080065static int vhost_user_start(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030066{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080067 VhostNetOptions options;
68 VhostUserState *s;
69 int max_queues;
70 int i;
71
72 options.backend_type = VHOST_BACKEND_TYPE_USER;
73
74 for (i = 0; i < queues; i++) {
75 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
76
77 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
78 if (vhost_user_running(s)) {
79 continue;
80 }
81
82 options.net_backend = ncs[i];
83 options.opaque = s->chr;
84 s->vhost_net = vhost_net_init(&options);
85 if (!s->vhost_net) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010086 error_report("failed to init vhost_net for queue %d", i);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080087 goto err;
88 }
89
90 if (i == 0) {
91 max_queues = vhost_net_get_max_queues(s->vhost_net);
92 if (queues > max_queues) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010093 error_report("you are asking more queues than supported: %d",
94 max_queues);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080095 goto err;
96 }
97 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030098 }
99
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800100 return 0;
101
102err:
103 vhost_user_stop(i + 1, ncs);
104 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300105}
106
Thibaut Colletf6f56292015-10-09 17:17:31 +0200107static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
108 size_t size)
109{
Thibaut Collet3e866362015-10-09 17:17:32 +0200110 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
111 This fake RARP will be sent by backend only for guest
112 without GUEST_ANNOUNCE capability.
Thibaut Colletf6f56292015-10-09 17:17:31 +0200113 */
Thibaut Collet3e866362015-10-09 17:17:32 +0200114 if (size == 60) {
115 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
116 int r;
117 static int display_rarp_failure = 1;
118 char mac_addr[6];
119
120 /* extract guest mac address from the RARP message */
121 memcpy(mac_addr, &buf[6], 6);
122
123 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
124
125 if ((r != 0) && (display_rarp_failure)) {
126 fprintf(stderr,
127 "Vhost user backend fails to broadcast fake RARP\n");
128 fflush(stderr);
129 display_rarp_failure = 0;
130 }
131 }
132
Thibaut Colletf6f56292015-10-09 17:17:31 +0200133 return size;
134}
135
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300136static void vhost_user_cleanup(NetClientState *nc)
137{
138 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
139
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800140 if (s->vhost_net) {
141 vhost_net_cleanup(s->vhost_net);
142 s->vhost_net = NULL;
143 }
144
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300145 qemu_purge_queued_packets(nc);
146}
147
148static bool vhost_user_has_vnet_hdr(NetClientState *nc)
149{
150 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
151
152 return true;
153}
154
155static bool vhost_user_has_ufo(NetClientState *nc)
156{
157 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
158
159 return true;
160}
161
162static NetClientInfo net_vhost_user_info = {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300163 .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300164 .size = sizeof(VhostUserState),
Thibaut Colletf6f56292015-10-09 17:17:31 +0200165 .receive = vhost_user_receive,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300166 .cleanup = vhost_user_cleanup,
167 .has_vnet_hdr = vhost_user_has_vnet_hdr,
168 .has_ufo = vhost_user_has_ufo,
169};
170
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200171static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
172 void *opaque)
173{
174 VhostUserState *s = opaque;
175 uint8_t buf[1];
176
177 /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be
178 * raised as a side-effect of the read.
179 */
180 qemu_chr_fe_read_all(s->chr, buf, sizeof(buf));
181
182 return FALSE;
183}
184
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300185static void net_vhost_user_event(void *opaque, int event)
186{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800187 const char *name = opaque;
188 NetClientState *ncs[MAX_QUEUE_NUM];
189 VhostUserState *s;
190 Error *err = NULL;
191 int queues;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300192
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800193 queues = qemu_find_net_clients_except(name, ncs,
194 NET_CLIENT_OPTIONS_KIND_NIC,
195 MAX_QUEUE_NUM);
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100196 assert(queues < MAX_QUEUE_NUM);
197
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800198 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
Marc-André Lureau69b32a62015-10-09 17:17:30 +0200199 trace_vhost_user_event(s->chr->label, event);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300200 switch (event) {
201 case CHR_EVENT_OPENED:
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200202 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
203 net_vhost_user_watch, s);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800204 if (vhost_user_start(queues, ncs) < 0) {
205 exit(1);
206 }
207 qmp_set_link(name, true, &err);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300208 break;
209 case CHR_EVENT_CLOSED:
Wen Congyangd39c87d2015-11-11 14:53:29 +0800210 qmp_set_link(name, false, &err);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800211 vhost_user_stop(queues, ncs);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200212 g_source_remove(s->watch);
213 s->watch = 0;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300214 break;
215 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800216
217 if (err) {
218 error_report_err(err);
219 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300220}
221
222static int net_vhost_user_init(NetClientState *peer, const char *device,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800223 const char *name, CharDriverState *chr,
224 int queues)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300225{
226 NetClientState *nc;
227 VhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800228 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300229
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100230 assert(name);
231 assert(queues > 0);
232
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800233 for (i = 0; i < queues; i++) {
234 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300235
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800236 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
237 i, chr->label);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300238
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800239 nc->queue_index = i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300240
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800241 s = DO_UPCAST(VhostUserState, nc, nc);
242 s->chr = chr;
243 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300244
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100245 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
Michael S. Tsirkind345ed22015-07-15 13:47:31 +0300246
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300247 return 0;
248}
249
Markus Armbruster71df1d82015-03-12 08:40:25 +0100250static int net_vhost_chardev_opts(void *opaque,
251 const char *name, const char *value,
252 Error **errp)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300253{
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300254 VhostUserChardevProps *props = opaque;
255
256 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
257 props->is_socket = true;
258 } else if (strcmp(name, "path") == 0) {
259 props->is_unix = true;
260 } else if (strcmp(name, "server") == 0) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300261 } else {
Markus Armbruster81904832015-03-13 14:17:16 +0100262 error_setg(errp,
263 "vhost-user does not support a chardev with option %s=%s",
264 name, value);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300265 return -1;
266 }
267 return 0;
268}
269
Markus Armbruster81904832015-03-13 14:17:16 +0100270static CharDriverState *net_vhost_parse_chardev(
271 const NetdevVhostUserOptions *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300272{
273 CharDriverState *chr = qemu_chr_find(opts->chardev);
274 VhostUserChardevProps props;
275
276 if (chr == NULL) {
Markus Armbruster81904832015-03-13 14:17:16 +0100277 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300278 return NULL;
279 }
280
281 /* inspect chardev opts */
282 memset(&props, 0, sizeof(props));
Markus Armbruster81904832015-03-13 14:17:16 +0100283 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300284 return NULL;
285 }
286
287 if (!props.is_socket || !props.is_unix) {
Markus Armbruster81904832015-03-13 14:17:16 +0100288 error_setg(errp, "chardev \"%s\" is not a unix socket",
289 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300290 return NULL;
291 }
292
293 qemu_chr_fe_claim_no_fail(chr);
294
295 return chr;
296}
297
Markus Armbruster28d0de72015-03-13 13:35:14 +0100298static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300299{
300 const char *name = opaque;
301 const char *driver, *netdev;
302 const char virtio_name[] = "virtio-net-";
303
304 driver = qemu_opt_get(opts, "driver");
305 netdev = qemu_opt_get(opts, "netdev");
306
307 if (!driver || !netdev) {
308 return 0;
309 }
310
311 if (strcmp(netdev, name) == 0 &&
312 strncmp(driver, virtio_name, strlen(virtio_name)) != 0) {
Markus Armbruster81904832015-03-13 14:17:16 +0100313 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300314 return -1;
315 }
316
317 return 0;
318}
319
320int net_init_vhost_user(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200321 NetClientState *peer, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300322{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800323 int queues;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300324 const NetdevVhostUserOptions *vhost_user_opts;
325 CharDriverState *chr;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300326
Eric Blake8d0bcba2015-10-26 16:34:56 -0600327 assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
Eric Blake32bafa82016-03-17 16:48:37 -0600328 vhost_user_opts = opts->u.vhost_user.data;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300329
Markus Armbruster81904832015-03-13 14:17:16 +0100330 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300331 if (!chr) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300332 return -1;
333 }
334
335 /* verify net frontend */
336 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
Markus Armbruster81904832015-03-13 14:17:16 +0100337 (char *)name, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300338 return -1;
339 }
340
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800341 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300342 if (queues < 1 || queues > MAX_QUEUE_NUM) {
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200343 error_setg(errp,
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300344 "vhost-user number of queues must be in range [1, %d]",
345 MAX_QUEUE_NUM);
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200346 return -1;
347 }
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300348
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800349 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300350}