blob: 8b7e98d5b958359da26545472da83a8ccc6c7a6f [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;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030023 CharBackend 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;
Marc-André Lureauc89804d2016-07-27 01:15:19 +040027 bool started;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030028} VhostUserState;
29
30VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
31{
32 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060033 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030034 return s->vhost_net;
35}
36
Marc-André Lureaua4632152016-06-06 18:45:05 +020037uint64_t vhost_user_get_acked_features(NetClientState *nc)
38{
39 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060040 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Marc-André Lureaua4632152016-06-06 18:45:05 +020041 return s->acked_features;
42}
43
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080044static void vhost_user_stop(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030045{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080046 VhostUserState *s;
47 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030048
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080049 for (i = 0; i < queues; i++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060050 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080051
52 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080053
54 if (s->vhost_net) {
Marc-André Lureaua4632152016-06-06 18:45:05 +020055 /* save acked features */
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040056 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
57 if (features) {
58 s->acked_features = features;
59 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080060 vhost_net_cleanup(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080061 }
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;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040068 struct vhost_net *net = NULL;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080069 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++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060076 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080077
78 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080079
80 options.net_backend = ncs[i];
Marc-André Lureau5345fdb2016-10-22 12:52:55 +030081 options.opaque = &s->chr;
Jason Wang69e87b32016-07-06 09:57:55 +080082 options.busyloop_timeout = 0;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040083 net = vhost_net_init(&options);
84 if (!net) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010085 error_report("failed to init vhost_net for queue %d", i);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080086 goto err;
87 }
88
89 if (i == 0) {
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040090 max_queues = vhost_net_get_max_queues(net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080091 if (queues > max_queues) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010092 error_report("you are asking more queues than supported: %d",
93 max_queues);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080094 goto err;
95 }
96 }
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040097
98 if (s->vhost_net) {
99 vhost_net_cleanup(s->vhost_net);
100 g_free(s->vhost_net);
101 }
102 s->vhost_net = net;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300103 }
104
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800105 return 0;
106
107err:
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400108 if (net) {
109 vhost_net_cleanup(net);
110 }
111 vhost_user_stop(i, ncs);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800112 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300113}
114
Thibaut Colletf6f56292015-10-09 17:17:31 +0200115static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
116 size_t size)
117{
Thibaut Collet3e866362015-10-09 17:17:32 +0200118 /* 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.
Thibaut Colletf6f56292015-10-09 17:17:31 +0200121 */
Thibaut Collet3e866362015-10-09 17:17:32 +0200122 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
Thibaut Colletf6f56292015-10-09 17:17:31 +0200141 return size;
142}
143
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300144static void vhost_user_cleanup(NetClientState *nc)
145{
146 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
147
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800148 if (s->vhost_net) {
149 vhost_net_cleanup(s->vhost_net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400150 g_free(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800151 s->vhost_net = NULL;
152 }
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300153 if (s->chr.chr) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300154 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL, NULL);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300155 qemu_chr_fe_release(s->chr.chr);
156 s->chr.chr = NULL;
Paolo Bonzini25f0d2a2016-06-29 15:15:33 +0200157 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800158
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300159 qemu_purge_queued_packets(nc);
160}
161
162static bool vhost_user_has_vnet_hdr(NetClientState *nc)
163{
Eric Blakef394b2e2016-07-13 21:50:23 -0600164 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300165
166 return true;
167}
168
169static bool vhost_user_has_ufo(NetClientState *nc)
170{
Eric Blakef394b2e2016-07-13 21:50:23 -0600171 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300172
173 return true;
174}
175
176static NetClientInfo net_vhost_user_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600177 .type = NET_CLIENT_DRIVER_VHOST_USER,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300178 .size = sizeof(VhostUserState),
Thibaut Colletf6f56292015-10-09 17:17:31 +0200179 .receive = vhost_user_receive,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300180 .cleanup = vhost_user_cleanup,
181 .has_vnet_hdr = vhost_user_has_vnet_hdr,
182 .has_ufo = vhost_user_has_ufo,
183};
184
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200185static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
186 void *opaque)
187{
188 VhostUserState *s = opaque;
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200189
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300190 qemu_chr_fe_disconnect(&s->chr);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200191
192 return FALSE;
193}
194
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300195static void net_vhost_user_event(void *opaque, int event)
196{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800197 const char *name = opaque;
198 NetClientState *ncs[MAX_QUEUE_NUM];
199 VhostUserState *s;
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300200 CharDriverState *chr;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800201 Error *err = NULL;
202 int queues;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300203
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800204 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600205 NET_CLIENT_DRIVER_NIC,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800206 MAX_QUEUE_NUM);
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100207 assert(queues < MAX_QUEUE_NUM);
208
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800209 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300210 chr = qemu_chr_fe_get_driver(&s->chr);
211 trace_vhost_user_event(chr->label, event);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300212 switch (event) {
213 case CHR_EVENT_OPENED:
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300214 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200215 net_vhost_user_watch, s);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800216 if (vhost_user_start(queues, ncs) < 0) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300217 qemu_chr_fe_disconnect(&s->chr);
Marc-André Lureau0d572af2016-06-06 18:45:03 +0200218 return;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800219 }
220 qmp_set_link(name, true, &err);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400221 s->started = true;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300222 break;
223 case CHR_EVENT_CLOSED:
Wen Congyangd39c87d2015-11-11 14:53:29 +0800224 qmp_set_link(name, false, &err);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800225 vhost_user_stop(queues, ncs);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200226 g_source_remove(s->watch);
227 s->watch = 0;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300228 break;
229 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800230
231 if (err) {
232 error_report_err(err);
233 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300234}
235
236static int net_vhost_user_init(NetClientState *peer, const char *device,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800237 const char *name, CharDriverState *chr,
238 int queues)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300239{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300240 Error *err = NULL;
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400241 NetClientState *nc, *nc0 = NULL;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300242 VhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800243 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300244
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100245 assert(name);
246 assert(queues > 0);
247
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800248 for (i = 0; i < queues; i++) {
249 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400250 if (!nc0) {
251 nc0 = nc;
252 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300253
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800254 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
255 i, chr->label);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300256
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800257 nc->queue_index = i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300258
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800259 s = DO_UPCAST(VhostUserState, nc, nc);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300260
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300261 if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
262 error_report_err(err);
263 return -1;
264 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800265 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300266
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400267 s = DO_UPCAST(VhostUserState, nc, nc0);
268 do {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300269 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400270 error_report_err(err);
271 return -1;
272 }
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300273 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
274 net_vhost_user_event, nc0->name, NULL);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400275 } while (!s->started);
Michael S. Tsirkind345ed22015-07-15 13:47:31 +0300276
Marc-André Lureau1a5b68c2016-07-27 01:15:13 +0400277 assert(s->vhost_net);
278
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300279 return 0;
280}
281
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100282static CharDriverState *net_vhost_claim_chardev(
Markus Armbruster81904832015-03-13 14:17:16 +0100283 const NetdevVhostUserOptions *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300284{
285 CharDriverState *chr = qemu_chr_find(opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300286
287 if (chr == NULL) {
Markus Armbruster81904832015-03-13 14:17:16 +0100288 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300289 return NULL;
290 }
291
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100292 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
293 error_setg(errp, "chardev \"%s\" is not reconnectable",
294 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300295 return NULL;
296 }
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100297 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
298 error_setg(errp, "chardev \"%s\" does not support FD passing",
Markus Armbruster81904832015-03-13 14:17:16 +0100299 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300300 return NULL;
301 }
302
303 qemu_chr_fe_claim_no_fail(chr);
304
305 return chr;
306}
307
Markus Armbruster28d0de72015-03-13 13:35:14 +0100308static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300309{
310 const char *name = opaque;
311 const char *driver, *netdev;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300312
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 &&
Marc-André Lureaud9d26112016-07-27 01:14:56 +0400321 !g_str_has_prefix(driver, "virtio-net-")) {
Markus Armbruster81904832015-03-13 14:17:16 +0100322 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300323 return -1;
324 }
325
326 return 0;
327}
328
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600329int net_init_vhost_user(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200330 NetClientState *peer, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300331{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800332 int queues;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300333 const NetdevVhostUserOptions *vhost_user_opts;
334 CharDriverState *chr;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300335
Eric Blakef394b2e2016-07-13 21:50:23 -0600336 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
337 vhost_user_opts = &netdev->u.vhost_user;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300338
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100339 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300340 if (!chr) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300341 return -1;
342 }
343
344 /* verify net frontend */
345 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
Markus Armbruster81904832015-03-13 14:17:16 +0100346 (char *)name, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300347 return -1;
348 }
349
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800350 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300351 if (queues < 1 || queues > MAX_QUEUE_NUM) {
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200352 error_setg(errp,
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300353 "vhost-user number of queues must be in range [1, %d]",
354 MAX_QUEUE_NUM);
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200355 return -1;
356 }
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300357
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800358 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300359}