blob: a39f9c9974dba5905e989e5a309fb1772241cdef [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"
Tiwei Bie4d0cf552018-05-24 18:33:33 +080015#include "hw/virtio/vhost-user.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040016#include "chardev/char-fe.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010017#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010018#include "qapi/qapi-commands-net.h"
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030019#include "qemu/config-file.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030020#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010021#include "qemu/option.h"
Marc-André Lureau69b32a62015-10-09 17:17:30 +020022#include "trace.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030023
Tiwei Bie703878e2018-04-12 23:12:27 +080024typedef struct NetVhostUserState {
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030025 NetClientState nc;
Marc-André Lureau5d300162016-10-22 12:52:57 +030026 CharBackend chr; /* only queue index 0 */
Tiwei Bie4d0cf552018-05-24 18:33:33 +080027 VhostUserState *vhost_user;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030028 VHostNetState *vhost_net;
Paolo Bonzini6f1de6b2016-06-20 15:02:40 +020029 guint watch;
Marc-André Lureaua4632152016-06-06 18:45:05 +020030 uint64_t acked_features;
Marc-André Lureauc89804d2016-07-27 01:15:19 +040031 bool started;
Tiwei Bie703878e2018-04-12 23:12:27 +080032} NetVhostUserState;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030033
34VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35{
Tiwei Bie703878e2018-04-12 23:12:27 +080036 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060037 assert(nc->info->type == NET_CLIENT_DRIVER_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{
Tiwei Bie703878e2018-04-12 23:12:27 +080043 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060044 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Marc-André Lureaua4632152016-06-06 18:45:05 +020045 return s->acked_features;
46}
47
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080048static void vhost_user_stop(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030049{
Tiwei Bie703878e2018-04-12 23:12:27 +080050 NetVhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080051 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030052
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080053 for (i = 0; i < queues; i++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060054 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080055
Tiwei Bie703878e2018-04-12 23:12:27 +080056 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080057
58 if (s->vhost_net) {
Marc-André Lureaua4632152016-06-06 18:45:05 +020059 /* save acked features */
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040060 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
61 if (features) {
62 s->acked_features = features;
63 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080064 vhost_net_cleanup(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080065 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030066 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030067}
68
Tiwei Bie4d0cf552018-05-24 18:33:33 +080069static int vhost_user_start(int queues, NetClientState *ncs[],
70 VhostUserState *be)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030071{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080072 VhostNetOptions options;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040073 struct vhost_net *net = NULL;
Tiwei Bie703878e2018-04-12 23:12:27 +080074 NetVhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080075 int max_queues;
76 int i;
77
78 options.backend_type = VHOST_BACKEND_TYPE_USER;
79
80 for (i = 0; i < queues; i++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060081 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080082
Tiwei Bie703878e2018-04-12 23:12:27 +080083 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080084
85 options.net_backend = ncs[i];
Marc-André Lureau5d300162016-10-22 12:52:57 +030086 options.opaque = be;
Jason Wang69e87b32016-07-06 09:57:55 +080087 options.busyloop_timeout = 0;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040088 net = vhost_net_init(&options);
89 if (!net) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010090 error_report("failed to init vhost_net for queue %d", i);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080091 goto err;
92 }
93
94 if (i == 0) {
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040095 max_queues = vhost_net_get_max_queues(net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080096 if (queues > max_queues) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010097 error_report("you are asking more queues than supported: %d",
98 max_queues);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080099 goto err;
100 }
101 }
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400102
103 if (s->vhost_net) {
104 vhost_net_cleanup(s->vhost_net);
105 g_free(s->vhost_net);
106 }
107 s->vhost_net = net;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300108 }
109
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800110 return 0;
111
112err:
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400113 if (net) {
114 vhost_net_cleanup(net);
linzhechenga38a4982018-02-13 13:08:37 +0800115 g_free(net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400116 }
117 vhost_user_stop(i, ncs);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800118 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300119}
120
Thibaut Colletf6f56292015-10-09 17:17:31 +0200121static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
122 size_t size)
123{
Thibaut Collet3e866362015-10-09 17:17:32 +0200124 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
125 This fake RARP will be sent by backend only for guest
126 without GUEST_ANNOUNCE capability.
Thibaut Colletf6f56292015-10-09 17:17:31 +0200127 */
Thibaut Collet3e866362015-10-09 17:17:32 +0200128 if (size == 60) {
Tiwei Bie703878e2018-04-12 23:12:27 +0800129 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
Thibaut Collet3e866362015-10-09 17:17:32 +0200130 int r;
131 static int display_rarp_failure = 1;
132 char mac_addr[6];
133
134 /* extract guest mac address from the RARP message */
135 memcpy(mac_addr, &buf[6], 6);
136
137 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
138
139 if ((r != 0) && (display_rarp_failure)) {
140 fprintf(stderr,
141 "Vhost user backend fails to broadcast fake RARP\n");
142 fflush(stderr);
143 display_rarp_failure = 0;
144 }
145 }
146
Thibaut Colletf6f56292015-10-09 17:17:31 +0200147 return size;
148}
149
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800150static void net_vhost_user_cleanup(NetClientState *nc)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300151{
Tiwei Bie703878e2018-04-12 23:12:27 +0800152 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300153
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800154 if (s->vhost_net) {
155 vhost_net_cleanup(s->vhost_net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400156 g_free(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800157 s->vhost_net = NULL;
158 }
Marc-André Lureauc39860e2016-10-22 12:52:58 +0300159 if (nc->queue_index == 0) {
Yunjian Wang41d4e5e2017-07-28 09:50:53 +0800160 if (s->watch) {
161 g_source_remove(s->watch);
162 s->watch = 0;
163 }
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400164 qemu_chr_fe_deinit(&s->chr, true);
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800165 if (s->vhost_user) {
166 vhost_user_cleanup(s->vhost_user);
167 g_free(s->vhost_user);
168 s->vhost_user = NULL;
169 }
Paolo Bonzini25f0d2a2016-06-29 15:15:33 +0200170 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800171
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300172 qemu_purge_queued_packets(nc);
173}
174
175static bool vhost_user_has_vnet_hdr(NetClientState *nc)
176{
Eric Blakef394b2e2016-07-13 21:50:23 -0600177 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300178
179 return true;
180}
181
182static bool vhost_user_has_ufo(NetClientState *nc)
183{
Eric Blakef394b2e2016-07-13 21:50:23 -0600184 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300185
186 return true;
187}
188
189static NetClientInfo net_vhost_user_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600190 .type = NET_CLIENT_DRIVER_VHOST_USER,
Tiwei Bie703878e2018-04-12 23:12:27 +0800191 .size = sizeof(NetVhostUserState),
Thibaut Colletf6f56292015-10-09 17:17:31 +0200192 .receive = vhost_user_receive,
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800193 .cleanup = net_vhost_user_cleanup,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300194 .has_vnet_hdr = vhost_user_has_vnet_hdr,
195 .has_ufo = vhost_user_has_ufo,
196};
197
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200198static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
199 void *opaque)
200{
Tiwei Bie703878e2018-04-12 23:12:27 +0800201 NetVhostUserState *s = opaque;
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200202
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300203 qemu_chr_fe_disconnect(&s->chr);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200204
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400205 return TRUE;
206}
207
208static void net_vhost_user_event(void *opaque, int event);
209
210static void chr_closed_bh(void *opaque)
211{
212 const char *name = opaque;
213 NetClientState *ncs[MAX_QUEUE_NUM];
Tiwei Bie703878e2018-04-12 23:12:27 +0800214 NetVhostUserState *s;
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400215 Error *err = NULL;
216 int queues;
217
218 queues = qemu_find_net_clients_except(name, ncs,
219 NET_CLIENT_DRIVER_NIC,
220 MAX_QUEUE_NUM);
221 assert(queues < MAX_QUEUE_NUM);
222
Tiwei Bie703878e2018-04-12 23:12:27 +0800223 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400224
225 qmp_set_link(name, false, &err);
226 vhost_user_stop(queues, ncs);
227
228 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300229 NULL, opaque, NULL, true);
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400230
231 if (err) {
232 error_report_err(err);
233 }
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200234}
235
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300236static void net_vhost_user_event(void *opaque, int event)
237{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800238 const char *name = opaque;
239 NetClientState *ncs[MAX_QUEUE_NUM];
Tiwei Bie703878e2018-04-12 23:12:27 +0800240 NetVhostUserState *s;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300241 Chardev *chr;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800242 Error *err = NULL;
243 int queues;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300244
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800245 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600246 NET_CLIENT_DRIVER_NIC,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800247 MAX_QUEUE_NUM);
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100248 assert(queues < MAX_QUEUE_NUM);
249
Tiwei Bie703878e2018-04-12 23:12:27 +0800250 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300251 chr = qemu_chr_fe_get_driver(&s->chr);
252 trace_vhost_user_event(chr->label, event);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300253 switch (event) {
254 case CHR_EVENT_OPENED:
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800255 if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300256 qemu_chr_fe_disconnect(&s->chr);
Marc-André Lureau0d572af2016-06-06 18:45:03 +0200257 return;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800258 }
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400259 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
260 net_vhost_user_watch, s);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800261 qmp_set_link(name, true, &err);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400262 s->started = true;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300263 break;
264 case CHR_EVENT_CLOSED:
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400265 /* a close event may happen during a read/write, but vhost
266 * code assumes the vhost_dev remains setup, so delay the
267 * stop & clear to idle.
268 * FIXME: better handle failure in vhost code, remove bh
269 */
270 if (s->watch) {
271 AioContext *ctx = qemu_get_current_aio_context();
272
273 g_source_remove(s->watch);
274 s->watch = 0;
Anton Nefedov81517ba2017-07-06 15:08:49 +0300275 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
Marc-André Lureaue7c83a82017-02-27 14:49:56 +0400276 NULL, NULL, false);
277
278 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
279 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300280 break;
281 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800282
283 if (err) {
284 error_report_err(err);
285 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300286}
287
288static int net_vhost_user_init(NetClientState *peer, const char *device,
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300289 const char *name, Chardev *chr,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800290 int queues)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300291{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300292 Error *err = NULL;
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400293 NetClientState *nc, *nc0 = NULL;
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800294 VhostUserState *user = NULL;
295 NetVhostUserState *s = NULL;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800296 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300297
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100298 assert(name);
299 assert(queues > 0);
300
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800301 user = vhost_user_init();
302 if (!user) {
303 error_report("failed to init vhost_user");
304 goto err;
305 }
306
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800307 for (i = 0; i < queues; i++) {
308 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800309 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
310 i, chr->label);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800311 nc->queue_index = i;
Marc-André Lureau5d300162016-10-22 12:52:57 +0300312 if (!nc0) {
313 nc0 = nc;
Tiwei Bie703878e2018-04-12 23:12:27 +0800314 s = DO_UPCAST(NetVhostUserState, nc, nc);
Marc-André Lureau5d300162016-10-22 12:52:57 +0300315 if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
316 error_report_err(err);
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800317 goto err;
Marc-André Lureau5d300162016-10-22 12:52:57 +0300318 }
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800319 user->chr = &s->chr;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300320 }
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800321 s = DO_UPCAST(NetVhostUserState, nc, nc);
322 s->vhost_user = user;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800323 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300324
Tiwei Bie703878e2018-04-12 23:12:27 +0800325 s = DO_UPCAST(NetVhostUserState, nc, nc0);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400326 do {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300327 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400328 error_report_err(err);
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800329 goto err;
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400330 }
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300331 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300332 net_vhost_user_event, NULL, nc0->name, NULL,
333 true);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400334 } while (!s->started);
Michael S. Tsirkind345ed22015-07-15 13:47:31 +0300335
Marc-André Lureau1a5b68c2016-07-27 01:15:13 +0400336 assert(s->vhost_net);
337
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300338 return 0;
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800339
340err:
341 if (user) {
342 vhost_user_cleanup(user);
343 g_free(user);
344 if (s) {
345 s->vhost_user = NULL;
346 }
347 }
linzhechengc67daf42018-06-12 10:24:45 +0800348 if (nc0) {
349 qemu_del_net_client(nc0);
350 }
Tiwei Bie4d0cf552018-05-24 18:33:33 +0800351
352 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300353}
354
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300355static Chardev *net_vhost_claim_chardev(
Markus Armbruster81904832015-03-13 14:17:16 +0100356 const NetdevVhostUserOptions *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300357{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300358 Chardev *chr = qemu_chr_find(opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300359
360 if (chr == NULL) {
Markus Armbruster81904832015-03-13 14:17:16 +0100361 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300362 return NULL;
363 }
364
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100365 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
366 error_setg(errp, "chardev \"%s\" is not reconnectable",
367 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300368 return NULL;
369 }
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100370 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
371 error_setg(errp, "chardev \"%s\" does not support FD passing",
Markus Armbruster81904832015-03-13 14:17:16 +0100372 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300373 return NULL;
374 }
375
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300376 return chr;
377}
378
Markus Armbruster28d0de72015-03-13 13:35:14 +0100379static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300380{
381 const char *name = opaque;
382 const char *driver, *netdev;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300383
384 driver = qemu_opt_get(opts, "driver");
385 netdev = qemu_opt_get(opts, "netdev");
386
387 if (!driver || !netdev) {
388 return 0;
389 }
390
391 if (strcmp(netdev, name) == 0 &&
Marc-André Lureaud9d26112016-07-27 01:14:56 +0400392 !g_str_has_prefix(driver, "virtio-net-")) {
Markus Armbruster81904832015-03-13 14:17:16 +0100393 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300394 return -1;
395 }
396
397 return 0;
398}
399
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600400int net_init_vhost_user(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200401 NetClientState *peer, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300402{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800403 int queues;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300404 const NetdevVhostUserOptions *vhost_user_opts;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300405 Chardev *chr;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300406
Eric Blakef394b2e2016-07-13 21:50:23 -0600407 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
408 vhost_user_opts = &netdev->u.vhost_user;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300409
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100410 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300411 if (!chr) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300412 return -1;
413 }
414
415 /* verify net frontend */
416 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
Markus Armbruster81904832015-03-13 14:17:16 +0100417 (char *)name, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300418 return -1;
419 }
420
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800421 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300422 if (queues < 1 || queues > MAX_QUEUE_NUM) {
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200423 error_setg(errp,
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300424 "vhost-user number of queues must be in range [1, %d]",
425 MAX_QUEUE_NUM);
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200426 return -1;
427 }
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300428
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800429 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300430}