]> git.proxmox.com Git - mirror_qemu.git/blob - net/vhost-user.c
vhost user: add support of live migration
[mirror_qemu.git] / net / vhost-user.c
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
11 #include "clients.h"
12 #include "net/vhost_net.h"
13 #include "net/vhost-user.h"
14 #include "sysemu/char.h"
15 #include "qemu/config-file.h"
16 #include "qemu/error-report.h"
17 #include "qmp-commands.h"
18 #include "trace.h"
19
20 typedef struct VhostUserState {
21 NetClientState nc;
22 CharDriverState *chr;
23 VHostNetState *vhost_net;
24 } VhostUserState;
25
26 typedef struct VhostUserChardevProps {
27 bool is_socket;
28 bool is_unix;
29 bool is_server;
30 } VhostUserChardevProps;
31
32 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
33 {
34 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
35 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
36 return s->vhost_net;
37 }
38
39 static int vhost_user_running(VhostUserState *s)
40 {
41 return (s->vhost_net) ? 1 : 0;
42 }
43
44 static void vhost_user_stop(int queues, NetClientState *ncs[])
45 {
46 VhostUserState *s;
47 int i;
48
49 for (i = 0; i < queues; i++) {
50 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
51
52 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
53 if (!vhost_user_running(s)) {
54 continue;
55 }
56
57 if (s->vhost_net) {
58 vhost_net_cleanup(s->vhost_net);
59 s->vhost_net = NULL;
60 }
61 }
62 }
63
64 static int vhost_user_start(int queues, NetClientState *ncs[])
65 {
66 VhostNetOptions options;
67 VhostUserState *s;
68 int max_queues;
69 int i;
70
71 options.backend_type = VHOST_BACKEND_TYPE_USER;
72
73 for (i = 0; i < queues; i++) {
74 assert (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
75
76 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
77 if (vhost_user_running(s)) {
78 continue;
79 }
80
81 options.net_backend = ncs[i];
82 options.opaque = s->chr;
83 s->vhost_net = vhost_net_init(&options);
84 if (!s->vhost_net) {
85 error_report("failed to init vhost_net for queue %d\n", i);
86 goto err;
87 }
88
89 if (i == 0) {
90 max_queues = vhost_net_get_max_queues(s->vhost_net);
91 if (queues > max_queues) {
92 error_report("you are asking more queues than "
93 "supported: %d\n", max_queues);
94 goto err;
95 }
96 }
97 }
98
99 return 0;
100
101 err:
102 vhost_user_stop(i + 1, ncs);
103 return -1;
104 }
105
106 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
107 size_t size)
108 {
109 /* Discard the request that is received and managed by backend
110 * by an other way.
111 */
112 return size;
113 }
114
115 static void vhost_user_cleanup(NetClientState *nc)
116 {
117 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
118
119 if (s->vhost_net) {
120 vhost_net_cleanup(s->vhost_net);
121 s->vhost_net = NULL;
122 }
123
124 qemu_purge_queued_packets(nc);
125 }
126
127 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
128 {
129 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
130
131 return true;
132 }
133
134 static bool vhost_user_has_ufo(NetClientState *nc)
135 {
136 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
137
138 return true;
139 }
140
141 static NetClientInfo net_vhost_user_info = {
142 .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
143 .size = sizeof(VhostUserState),
144 .receive = vhost_user_receive,
145 .cleanup = vhost_user_cleanup,
146 .has_vnet_hdr = vhost_user_has_vnet_hdr,
147 .has_ufo = vhost_user_has_ufo,
148 };
149
150 static void net_vhost_user_event(void *opaque, int event)
151 {
152 const char *name = opaque;
153 NetClientState *ncs[MAX_QUEUE_NUM];
154 VhostUserState *s;
155 Error *err = NULL;
156 int queues;
157
158 queues = qemu_find_net_clients_except(name, ncs,
159 NET_CLIENT_OPTIONS_KIND_NIC,
160 MAX_QUEUE_NUM);
161 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
162 trace_vhost_user_event(s->chr->label, event);
163 switch (event) {
164 case CHR_EVENT_OPENED:
165 if (vhost_user_start(queues, ncs) < 0) {
166 exit(1);
167 }
168 qmp_set_link(name, true, &err);
169 break;
170 case CHR_EVENT_CLOSED:
171 qmp_set_link(name, true, &err);
172 vhost_user_stop(queues, ncs);
173 break;
174 }
175
176 if (err) {
177 error_report_err(err);
178 }
179 }
180
181 static int net_vhost_user_init(NetClientState *peer, const char *device,
182 const char *name, CharDriverState *chr,
183 int queues)
184 {
185 NetClientState *nc;
186 VhostUserState *s;
187 int i;
188
189 for (i = 0; i < queues; i++) {
190 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
191
192 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
193 i, chr->label);
194
195 nc->queue_index = i;
196
197 s = DO_UPCAST(VhostUserState, nc, nc);
198 s->chr = chr;
199 }
200
201 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, (void*)name);
202
203 return 0;
204 }
205
206 static int net_vhost_chardev_opts(void *opaque,
207 const char *name, const char *value,
208 Error **errp)
209 {
210 VhostUserChardevProps *props = opaque;
211
212 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
213 props->is_socket = true;
214 } else if (strcmp(name, "path") == 0) {
215 props->is_unix = true;
216 } else if (strcmp(name, "server") == 0) {
217 props->is_server = true;
218 } else {
219 error_setg(errp,
220 "vhost-user does not support a chardev with option %s=%s",
221 name, value);
222 return -1;
223 }
224 return 0;
225 }
226
227 static CharDriverState *net_vhost_parse_chardev(
228 const NetdevVhostUserOptions *opts, Error **errp)
229 {
230 CharDriverState *chr = qemu_chr_find(opts->chardev);
231 VhostUserChardevProps props;
232
233 if (chr == NULL) {
234 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
235 return NULL;
236 }
237
238 /* inspect chardev opts */
239 memset(&props, 0, sizeof(props));
240 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
241 return NULL;
242 }
243
244 if (!props.is_socket || !props.is_unix) {
245 error_setg(errp, "chardev \"%s\" is not a unix socket",
246 opts->chardev);
247 return NULL;
248 }
249
250 qemu_chr_fe_claim_no_fail(chr);
251
252 return chr;
253 }
254
255 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
256 {
257 const char *name = opaque;
258 const char *driver, *netdev;
259 const char virtio_name[] = "virtio-net-";
260
261 driver = qemu_opt_get(opts, "driver");
262 netdev = qemu_opt_get(opts, "netdev");
263
264 if (!driver || !netdev) {
265 return 0;
266 }
267
268 if (strcmp(netdev, name) == 0 &&
269 strncmp(driver, virtio_name, strlen(virtio_name)) != 0) {
270 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
271 return -1;
272 }
273
274 return 0;
275 }
276
277 int net_init_vhost_user(const NetClientOptions *opts, const char *name,
278 NetClientState *peer, Error **errp)
279 {
280 int queues;
281 const NetdevVhostUserOptions *vhost_user_opts;
282 CharDriverState *chr;
283
284 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
285 vhost_user_opts = opts->vhost_user;
286
287 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
288 if (!chr) {
289 return -1;
290 }
291
292 /* verify net frontend */
293 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
294 (char *)name, errp)) {
295 return -1;
296 }
297
298 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
299
300 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
301 }