]> git.proxmox.com Git - mirror_qemu.git/blame - net/vhost-user.c
char: fold qemu_chr_set_handlers in qemu_chr_fe_set_handlers
[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;
32a6ebec 23 CharBackend chr;
d314f586 24 VHostNetState *vhost_net;
6f1de6b7 25 guint watch;
a463215b 26 uint64_t acked_features;
c89804d6 27 bool started;
d314f586
NN
28} VhostUserState;
29
30VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
31{
32 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
f394b2e2 33 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
34 return s->vhost_net;
35}
36
a463215b
MAL
37uint64_t vhost_user_get_acked_features(NetClientState *nc)
38{
39 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
f394b2e2 40 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
a463215b
MAL
41 return s->acked_features;
42}
43
b931bfbf 44static void vhost_user_stop(int queues, NetClientState *ncs[])
d314f586 45{
b931bfbf
CO
46 VhostUserState *s;
47 int i;
d314f586 48
b931bfbf 49 for (i = 0; i < queues; i++) {
f394b2e2 50 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586 51
b931bfbf 52 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
d314f586 53
b931bfbf 54 if (s->vhost_net) {
a463215b 55 /* save acked features */
e6bcb1b6
MAL
56 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
57 if (features) {
58 s->acked_features = features;
59 }
b931bfbf 60 vhost_net_cleanup(s->vhost_net);
b931bfbf
CO
61 }
62 }
d314f586
NN
63}
64
b931bfbf 65static int vhost_user_start(int queues, NetClientState *ncs[])
d314f586 66{
b931bfbf 67 VhostNetOptions options;
e6bcb1b6 68 struct vhost_net *net = NULL;
b931bfbf
CO
69 VhostUserState *s;
70 int max_queues;
71 int i;
72
73 options.backend_type = VHOST_BACKEND_TYPE_USER;
74
75 for (i = 0; i < queues; i++) {
f394b2e2 76 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
b931bfbf
CO
77
78 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
b931bfbf
CO
79
80 options.net_backend = ncs[i];
5345fdb4 81 options.opaque = &s->chr;
69e87b32 82 options.busyloop_timeout = 0;
e6bcb1b6
MAL
83 net = vhost_net_init(&options);
84 if (!net) {
9af9e0fe 85 error_report("failed to init vhost_net for queue %d", i);
b931bfbf
CO
86 goto err;
87 }
88
89 if (i == 0) {
e6bcb1b6 90 max_queues = vhost_net_get_max_queues(net);
b931bfbf 91 if (queues > max_queues) {
9af9e0fe
MA
92 error_report("you are asking more queues than supported: %d",
93 max_queues);
b931bfbf
CO
94 goto err;
95 }
96 }
e6bcb1b6
MAL
97
98 if (s->vhost_net) {
99 vhost_net_cleanup(s->vhost_net);
100 g_free(s->vhost_net);
101 }
102 s->vhost_net = net;
d314f586
NN
103 }
104
b931bfbf
CO
105 return 0;
106
107err:
e6bcb1b6
MAL
108 if (net) {
109 vhost_net_cleanup(net);
110 }
111 vhost_user_stop(i, ncs);
b931bfbf 112 return -1;
d314f586
NN
113}
114
f6f56291
TC
115static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
116 size_t size)
117{
3e866365
TC
118 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
119 This fake RARP will be sent by backend only for guest
120 without GUEST_ANNOUNCE capability.
f6f56291 121 */
3e866365
TC
122 if (size == 60) {
123 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
124 int r;
125 static int display_rarp_failure = 1;
126 char mac_addr[6];
127
128 /* extract guest mac address from the RARP message */
129 memcpy(mac_addr, &buf[6], 6);
130
131 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
132
133 if ((r != 0) && (display_rarp_failure)) {
134 fprintf(stderr,
135 "Vhost user backend fails to broadcast fake RARP\n");
136 fflush(stderr);
137 display_rarp_failure = 0;
138 }
139 }
140
f6f56291
TC
141 return size;
142}
143
d314f586
NN
144static void vhost_user_cleanup(NetClientState *nc)
145{
146 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
147
b931bfbf
CO
148 if (s->vhost_net) {
149 vhost_net_cleanup(s->vhost_net);
e6bcb1b6 150 g_free(s->vhost_net);
b931bfbf
CO
151 s->vhost_net = NULL;
152 }
32a6ebec 153 if (s->chr.chr) {
5345fdb4 154 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL, NULL);
32a6ebec
MAL
155 qemu_chr_fe_release(s->chr.chr);
156 s->chr.chr = NULL;
25f0d2aa 157 }
b931bfbf 158
d314f586
NN
159 qemu_purge_queued_packets(nc);
160}
161
162static bool vhost_user_has_vnet_hdr(NetClientState *nc)
163{
f394b2e2 164 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
165
166 return true;
167}
168
169static bool vhost_user_has_ufo(NetClientState *nc)
170{
f394b2e2 171 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
172
173 return true;
174}
175
176static NetClientInfo net_vhost_user_info = {
f394b2e2 177 .type = NET_CLIENT_DRIVER_VHOST_USER,
d314f586 178 .size = sizeof(VhostUserState),
f6f56291 179 .receive = vhost_user_receive,
d314f586
NN
180 .cleanup = vhost_user_cleanup,
181 .has_vnet_hdr = vhost_user_has_vnet_hdr,
182 .has_ufo = vhost_user_has_ufo,
183};
184
a6553598
TM
185static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
186 void *opaque)
187{
188 VhostUserState *s = opaque;
a6553598 189
5345fdb4 190 qemu_chr_fe_disconnect(&s->chr);
a6553598
TM
191
192 return FALSE;
193}
194
d314f586
NN
195static void net_vhost_user_event(void *opaque, int event)
196{
b931bfbf
CO
197 const char *name = opaque;
198 NetClientState *ncs[MAX_QUEUE_NUM];
199 VhostUserState *s;
5345fdb4 200 CharDriverState *chr;
b931bfbf
CO
201 Error *err = NULL;
202 int queues;
d314f586 203
b931bfbf 204 queues = qemu_find_net_clients_except(name, ncs,
f394b2e2 205 NET_CLIENT_DRIVER_NIC,
b931bfbf 206 MAX_QUEUE_NUM);
c1bf3531
MAL
207 assert(queues < MAX_QUEUE_NUM);
208
b931bfbf 209 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
5345fdb4
MAL
210 chr = qemu_chr_fe_get_driver(&s->chr);
211 trace_vhost_user_event(chr->label, event);
d314f586
NN
212 switch (event) {
213 case CHR_EVENT_OPENED:
5345fdb4 214 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
a6553598 215 net_vhost_user_watch, s);
b931bfbf 216 if (vhost_user_start(queues, ncs) < 0) {
5345fdb4 217 qemu_chr_fe_disconnect(&s->chr);
0d572afd 218 return;
b931bfbf
CO
219 }
220 qmp_set_link(name, true, &err);
c89804d6 221 s->started = true;
d314f586
NN
222 break;
223 case CHR_EVENT_CLOSED:
d39c87d7 224 qmp_set_link(name, false, &err);
b931bfbf 225 vhost_user_stop(queues, ncs);
a6553598
TM
226 g_source_remove(s->watch);
227 s->watch = 0;
d314f586
NN
228 break;
229 }
b931bfbf
CO
230
231 if (err) {
232 error_report_err(err);
233 }
d314f586
NN
234}
235
236static int net_vhost_user_init(NetClientState *peer, const char *device,
b931bfbf
CO
237 const char *name, CharDriverState *chr,
238 int queues)
d314f586 239{
32a6ebec 240 Error *err = NULL;
c89804d6 241 NetClientState *nc, *nc0 = NULL;
d314f586 242 VhostUserState *s;
b931bfbf 243 int i;
d314f586 244
c1bf3531
MAL
245 assert(name);
246 assert(queues > 0);
247
b931bfbf
CO
248 for (i = 0; i < queues; i++) {
249 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
c89804d6
MAL
250 if (!nc0) {
251 nc0 = nc;
252 }
d314f586 253
b931bfbf
CO
254 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
255 i, chr->label);
d314f586 256
b931bfbf 257 nc->queue_index = i;
d314f586 258
b931bfbf 259 s = DO_UPCAST(VhostUserState, nc, nc);
5345fdb4 260
32a6ebec
MAL
261 if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
262 error_report_err(err);
263 return -1;
264 }
b931bfbf 265 }
d345ed2d 266
c89804d6
MAL
267 s = DO_UPCAST(VhostUserState, nc, nc0);
268 do {
5345fdb4 269 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
c89804d6
MAL
270 error_report_err(err);
271 return -1;
272 }
5345fdb4
MAL
273 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
274 net_vhost_user_event, nc0->name, NULL);
c89804d6 275 } while (!s->started);
d314f586 276
1a5b68ce
MAL
277 assert(s->vhost_net);
278
d314f586
NN
279 return 0;
280}
281
0a73336d 282static CharDriverState *net_vhost_claim_chardev(
81904831 283 const NetdevVhostUserOptions *opts, Error **errp)
03ce5744
NN
284{
285 CharDriverState *chr = qemu_chr_find(opts->chardev);
03ce5744
NN
286
287 if (chr == NULL) {
81904831 288 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
03ce5744
NN
289 return NULL;
290 }
291
0a73336d
DB
292 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
293 error_setg(errp, "chardev \"%s\" is not reconnectable",
294 opts->chardev);
03ce5744
NN
295 return NULL;
296 }
0a73336d
DB
297 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
298 error_setg(errp, "chardev \"%s\" does not support FD passing",
81904831 299 opts->chardev);
03ce5744
NN
300 return NULL;
301 }
302
303 qemu_chr_fe_claim_no_fail(chr);
304
305 return chr;
306}
307
28d0de7a 308static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
03ce5744
NN
309{
310 const char *name = opaque;
311 const char *driver, *netdev;
03ce5744
NN
312
313 driver = qemu_opt_get(opts, "driver");
314 netdev = qemu_opt_get(opts, "netdev");
315
316 if (!driver || !netdev) {
317 return 0;
318 }
319
320 if (strcmp(netdev, name) == 0 &&
d9d26114 321 !g_str_has_prefix(driver, "virtio-net-")) {
81904831 322 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
03ce5744
NN
323 return -1;
324 }
325
326 return 0;
327}
328
cebea510 329int net_init_vhost_user(const Netdev *netdev, const char *name,
a30ecde6 330 NetClientState *peer, Error **errp)
d314f586 331{
b931bfbf 332 int queues;
03ce5744
NN
333 const NetdevVhostUserOptions *vhost_user_opts;
334 CharDriverState *chr;
03ce5744 335
f394b2e2
EB
336 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
337 vhost_user_opts = &netdev->u.vhost_user;
03ce5744 338
0a73336d 339 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
03ce5744 340 if (!chr) {
03ce5744
NN
341 return -1;
342 }
343
344 /* verify net frontend */
345 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
81904831 346 (char *)name, errp)) {
03ce5744
NN
347 return -1;
348 }
349
b931bfbf 350 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
fff4e48e 351 if (queues < 1 || queues > MAX_QUEUE_NUM) {
6f6f9512 352 error_setg(errp,
fff4e48e
IM
353 "vhost-user number of queues must be in range [1, %d]",
354 MAX_QUEUE_NUM);
6f6f9512
VK
355 return -1;
356 }
03ce5744 357
b931bfbf 358 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
d314f586 359}