]> git.proxmox.com Git - mirror_qemu.git/blame - net/vhost-user.c
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20160706' into staging
[mirror_qemu.git] / net / vhost-user.c
CommitLineData
d314f586
NN
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
2744d920 11#include "qemu/osdep.h"
d314f586
NN
12#include "clients.h"
13#include "net/vhost_net.h"
14#include "net/vhost-user.h"
15#include "sysemu/char.h"
03ce5744 16#include "qemu/config-file.h"
d314f586 17#include "qemu/error-report.h"
b931bfbf 18#include "qmp-commands.h"
69b32a6c 19#include "trace.h"
d314f586
NN
20
21typedef struct VhostUserState {
22 NetClientState nc;
23 CharDriverState *chr;
d314f586 24 VHostNetState *vhost_net;
6f1de6b7 25 guint watch;
a463215b 26 uint64_t acked_features;
d314f586
NN
27} VhostUserState;
28
03ce5744
NN
29typedef struct VhostUserChardevProps {
30 bool is_socket;
31 bool is_unix;
03ce5744
NN
32} VhostUserChardevProps;
33
d314f586
NN
34VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35{
36 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
03ce5744 37 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
d314f586
NN
38 return s->vhost_net;
39}
40
a463215b
MAL
41uint64_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
d314f586
NN
48static int vhost_user_running(VhostUserState *s)
49{
50 return (s->vhost_net) ? 1 : 0;
51}
52
b931bfbf 53static void vhost_user_stop(int queues, NetClientState *ncs[])
d314f586 54{
b931bfbf
CO
55 VhostUserState *s;
56 int i;
d314f586 57
b931bfbf
CO
58 for (i = 0; i < queues; i++) {
59 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
d314f586 60
b931bfbf
CO
61 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
62 if (!vhost_user_running(s)) {
63 continue;
64 }
d314f586 65
b931bfbf 66 if (s->vhost_net) {
a463215b
MAL
67 /* save acked features */
68 s->acked_features = vhost_net_get_acked_features(s->vhost_net);
b931bfbf
CO
69 vhost_net_cleanup(s->vhost_net);
70 s->vhost_net = NULL;
71 }
72 }
d314f586
NN
73}
74
b931bfbf 75static int vhost_user_start(int queues, NetClientState *ncs[])
d314f586 76{
b931bfbf
CO
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) {
9af9e0fe 96 error_report("failed to init vhost_net for queue %d", i);
b931bfbf
CO
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) {
9af9e0fe
MA
103 error_report("you are asking more queues than supported: %d",
104 max_queues);
b931bfbf
CO
105 goto err;
106 }
107 }
d314f586
NN
108 }
109
b931bfbf
CO
110 return 0;
111
112err:
113 vhost_user_stop(i + 1, ncs);
114 return -1;
d314f586
NN
115}
116
f6f56291
TC
117static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
118 size_t size)
119{
3e866365
TC
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.
f6f56291 123 */
3e866365
TC
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
f6f56291
TC
143 return size;
144}
145
d314f586
NN
146static void vhost_user_cleanup(NetClientState *nc)
147{
148 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
149
b931bfbf
CO
150 if (s->vhost_net) {
151 vhost_net_cleanup(s->vhost_net);
152 s->vhost_net = NULL;
153 }
25f0d2aa
PB
154 if (s->chr) {
155 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
156 qemu_chr_fe_release(s->chr);
157 s->chr = NULL;
158 }
b931bfbf 159
d314f586
NN
160 qemu_purge_queued_packets(nc);
161}
162
163static bool vhost_user_has_vnet_hdr(NetClientState *nc)
164{
165 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
166
167 return true;
168}
169
170static bool vhost_user_has_ufo(NetClientState *nc)
171{
172 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
173
174 return true;
175}
176
177static NetClientInfo net_vhost_user_info = {
03ce5744 178 .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
d314f586 179 .size = sizeof(VhostUserState),
f6f56291 180 .receive = vhost_user_receive,
d314f586
NN
181 .cleanup = vhost_user_cleanup,
182 .has_vnet_hdr = vhost_user_has_vnet_hdr,
183 .has_ufo = vhost_user_has_ufo,
184};
185
a6553598
TM
186static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
187 void *opaque)
188{
189 VhostUserState *s = opaque;
190 uint8_t buf[1];
191
192 /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be
193 * raised as a side-effect of the read.
194 */
195 qemu_chr_fe_read_all(s->chr, buf, sizeof(buf));
196
197 return FALSE;
198}
199
d314f586
NN
200static void net_vhost_user_event(void *opaque, int event)
201{
b931bfbf
CO
202 const char *name = opaque;
203 NetClientState *ncs[MAX_QUEUE_NUM];
204 VhostUserState *s;
205 Error *err = NULL;
206 int queues;
d314f586 207
b931bfbf
CO
208 queues = qemu_find_net_clients_except(name, ncs,
209 NET_CLIENT_OPTIONS_KIND_NIC,
210 MAX_QUEUE_NUM);
c1bf3531
MAL
211 assert(queues < MAX_QUEUE_NUM);
212
b931bfbf 213 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
69b32a6c 214 trace_vhost_user_event(s->chr->label, event);
d314f586
NN
215 switch (event) {
216 case CHR_EVENT_OPENED:
a6553598
TM
217 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
218 net_vhost_user_watch, s);
b931bfbf 219 if (vhost_user_start(queues, ncs) < 0) {
0d572afd
MAL
220 qemu_chr_disconnect(s->chr);
221 return;
b931bfbf
CO
222 }
223 qmp_set_link(name, true, &err);
d314f586
NN
224 break;
225 case CHR_EVENT_CLOSED:
d39c87d7 226 qmp_set_link(name, false, &err);
b931bfbf 227 vhost_user_stop(queues, ncs);
a6553598
TM
228 g_source_remove(s->watch);
229 s->watch = 0;
d314f586
NN
230 break;
231 }
b931bfbf
CO
232
233 if (err) {
234 error_report_err(err);
235 }
d314f586
NN
236}
237
238static int net_vhost_user_init(NetClientState *peer, const char *device,
b931bfbf
CO
239 const char *name, CharDriverState *chr,
240 int queues)
d314f586
NN
241{
242 NetClientState *nc;
243 VhostUserState *s;
b931bfbf 244 int i;
d314f586 245
c1bf3531
MAL
246 assert(name);
247 assert(queues > 0);
248
b931bfbf
CO
249 for (i = 0; i < queues; i++) {
250 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
d314f586 251
b931bfbf
CO
252 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
253 i, chr->label);
d314f586 254
b931bfbf 255 nc->queue_index = i;
d314f586 256
b931bfbf
CO
257 s = DO_UPCAST(VhostUserState, nc, nc);
258 s->chr = chr;
259 }
d345ed2d 260
c1bf3531 261 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
d314f586
NN
262
263 return 0;
264}
265
71df1d83
MA
266static int net_vhost_chardev_opts(void *opaque,
267 const char *name, const char *value,
268 Error **errp)
03ce5744
NN
269{
270 VhostUserChardevProps *props = opaque;
271
272 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
273 props->is_socket = true;
274 } else if (strcmp(name, "path") == 0) {
275 props->is_unix = true;
276 } else if (strcmp(name, "server") == 0) {
03ce5744 277 } else {
81904831
MA
278 error_setg(errp,
279 "vhost-user does not support a chardev with option %s=%s",
280 name, value);
03ce5744
NN
281 return -1;
282 }
283 return 0;
284}
285
81904831
MA
286static CharDriverState *net_vhost_parse_chardev(
287 const NetdevVhostUserOptions *opts, Error **errp)
03ce5744
NN
288{
289 CharDriverState *chr = qemu_chr_find(opts->chardev);
290 VhostUserChardevProps props;
291
292 if (chr == NULL) {
81904831 293 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
03ce5744
NN
294 return NULL;
295 }
296
297 /* inspect chardev opts */
298 memset(&props, 0, sizeof(props));
81904831 299 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
03ce5744
NN
300 return NULL;
301 }
302
303 if (!props.is_socket || !props.is_unix) {
81904831
MA
304 error_setg(errp, "chardev \"%s\" is not a unix socket",
305 opts->chardev);
03ce5744
NN
306 return NULL;
307 }
308
309 qemu_chr_fe_claim_no_fail(chr);
310
311 return chr;
312}
313
28d0de7a 314static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
03ce5744
NN
315{
316 const char *name = opaque;
317 const char *driver, *netdev;
318 const char virtio_name[] = "virtio-net-";
319
320 driver = qemu_opt_get(opts, "driver");
321 netdev = qemu_opt_get(opts, "netdev");
322
323 if (!driver || !netdev) {
324 return 0;
325 }
326
327 if (strcmp(netdev, name) == 0 &&
328 strncmp(driver, virtio_name, strlen(virtio_name)) != 0) {
81904831 329 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
03ce5744
NN
330 return -1;
331 }
332
333 return 0;
334}
335
d314f586 336int net_init_vhost_user(const NetClientOptions *opts, const char *name,
a30ecde6 337 NetClientState *peer, Error **errp)
d314f586 338{
b931bfbf 339 int queues;
03ce5744
NN
340 const NetdevVhostUserOptions *vhost_user_opts;
341 CharDriverState *chr;
03ce5744 342
8d0bcba8 343 assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
32bafa8f 344 vhost_user_opts = opts->u.vhost_user.data;
03ce5744 345
81904831 346 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
03ce5744 347 if (!chr) {
03ce5744
NN
348 return -1;
349 }
350
351 /* verify net frontend */
352 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
81904831 353 (char *)name, errp)) {
03ce5744
NN
354 return -1;
355 }
356
b931bfbf 357 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
fff4e48e 358 if (queues < 1 || queues > MAX_QUEUE_NUM) {
6f6f9512 359 error_setg(errp,
fff4e48e
IM
360 "vhost-user number of queues must be in range [1, %d]",
361 MAX_QUEUE_NUM);
6f6f9512
VK
362 return -1;
363 }
03ce5744 364
b931bfbf 365 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
d314f586 366}