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