1
2
3
4
5
6
7
8
9
10
11#include "qemu/osdep.h"
12#include "clients.h"
13#include "net/vhost_net.h"
14#include "net/vhost-user.h"
15#include "hw/virtio/vhost-user.h"
16#include "chardev/char-fe.h"
17#include "qapi/error.h"
18#include "qapi/qapi-commands-net.h"
19#include "qemu/config-file.h"
20#include "qemu/error-report.h"
21#include "qemu/option.h"
22#include "trace.h"
23
24typedef struct NetVhostUserState {
25 NetClientState nc;
26 CharBackend chr;
27 VhostUserState *vhost_user;
28 VHostNetState *vhost_net;
29 guint watch;
30 uint64_t acked_features;
31 bool started;
32} NetVhostUserState;
33
34VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35{
36 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
37 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
38 return s->vhost_net;
39}
40
41uint64_t vhost_user_get_acked_features(NetClientState *nc)
42{
43 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
44 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
45 return s->acked_features;
46}
47
48static void vhost_user_stop(int queues, NetClientState *ncs[])
49{
50 NetVhostUserState *s;
51 int i;
52
53 for (i = 0; i < queues; i++) {
54 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
55
56 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
57
58 if (s->vhost_net) {
59
60 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
61 if (features) {
62 s->acked_features = features;
63 }
64 vhost_net_cleanup(s->vhost_net);
65 }
66 }
67}
68
69static int vhost_user_start(int queues, NetClientState *ncs[],
70 VhostUserState *be)
71{
72 VhostNetOptions options;
73 struct vhost_net *net = NULL;
74 NetVhostUserState *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++) {
81 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
82
83 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
84
85 options.net_backend = ncs[i];
86 options.opaque = be;
87 options.busyloop_timeout = 0;
88 net = vhost_net_init(&options);
89 if (!net) {
90 error_report("failed to init vhost_net for queue %d", i);
91 goto err;
92 }
93
94 if (i == 0) {
95 max_queues = vhost_net_get_max_queues(net);
96 if (queues > max_queues) {
97 error_report("you are asking more queues than supported: %d",
98 max_queues);
99 goto err;
100 }
101 }
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;
108 }
109
110 return 0;
111
112err:
113 if (net) {
114 vhost_net_cleanup(net);
115 g_free(net);
116 }
117 vhost_user_stop(i, ncs);
118 return -1;
119}
120
121static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
122 size_t size)
123{
124
125
126
127
128 if (size == 60) {
129 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
130 int r;
131 static int display_rarp_failure = 1;
132 char mac_addr[6];
133
134
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
147 return size;
148}
149
150static void net_vhost_user_cleanup(NetClientState *nc)
151{
152 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
153
154 if (s->vhost_net) {
155 vhost_net_cleanup(s->vhost_net);
156 g_free(s->vhost_net);
157 s->vhost_net = NULL;
158 }
159 if (nc->queue_index == 0) {
160 if (s->watch) {
161 g_source_remove(s->watch);
162 s->watch = 0;
163 }
164 qemu_chr_fe_deinit(&s->chr, true);
165 if (s->vhost_user) {
166 vhost_user_cleanup(s->vhost_user);
167 g_free(s->vhost_user);
168 s->vhost_user = NULL;
169 }
170 }
171
172 qemu_purge_queued_packets(nc);
173}
174
175static bool vhost_user_has_vnet_hdr(NetClientState *nc)
176{
177 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
178
179 return true;
180}
181
182static bool vhost_user_has_ufo(NetClientState *nc)
183{
184 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
185
186 return true;
187}
188
189static NetClientInfo net_vhost_user_info = {
190 .type = NET_CLIENT_DRIVER_VHOST_USER,
191 .size = sizeof(NetVhostUserState),
192 .receive = vhost_user_receive,
193 .cleanup = net_vhost_user_cleanup,
194 .has_vnet_hdr = vhost_user_has_vnet_hdr,
195 .has_ufo = vhost_user_has_ufo,
196};
197
198static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
199 void *opaque)
200{
201 NetVhostUserState *s = opaque;
202
203 qemu_chr_fe_disconnect(&s->chr);
204
205 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];
214 NetVhostUserState *s;
215 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
223 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
224
225 qmp_set_link(name, false, &err);
226
227 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
228 NULL, opaque, NULL, true);
229
230 if (err) {
231 error_report_err(err);
232 }
233}
234
235static void net_vhost_user_event(void *opaque, int event)
236{
237 const char *name = opaque;
238 NetClientState *ncs[MAX_QUEUE_NUM];
239 NetVhostUserState *s;
240 Chardev *chr;
241 Error *err = NULL;
242 int queues;
243
244 queues = qemu_find_net_clients_except(name, ncs,
245 NET_CLIENT_DRIVER_NIC,
246 MAX_QUEUE_NUM);
247 assert(queues < MAX_QUEUE_NUM);
248
249 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
250 chr = qemu_chr_fe_get_driver(&s->chr);
251 trace_vhost_user_event(chr->label, event);
252 switch (event) {
253 case CHR_EVENT_OPENED:
254 if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
255 qemu_chr_fe_disconnect(&s->chr);
256 return;
257 }
258 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
259 net_vhost_user_watch, s);
260 qmp_set_link(name, true, &err);
261 s->started = true;
262 break;
263 case CHR_EVENT_CLOSED:
264
265
266
267
268
269 if (s->watch) {
270 AioContext *ctx = qemu_get_current_aio_context();
271
272 g_source_remove(s->watch);
273 s->watch = 0;
274 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
275 NULL, NULL, false);
276
277 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
278 }
279 break;
280 }
281
282 if (err) {
283 error_report_err(err);
284 }
285}
286
287static int net_vhost_user_init(NetClientState *peer, const char *device,
288 const char *name, Chardev *chr,
289 int queues)
290{
291 Error *err = NULL;
292 NetClientState *nc, *nc0 = NULL;
293 VhostUserState *user = NULL;
294 NetVhostUserState *s = NULL;
295 int i;
296
297 assert(name);
298 assert(queues > 0);
299
300 user = vhost_user_init();
301 if (!user) {
302 error_report("failed to init vhost_user");
303 goto err;
304 }
305
306 for (i = 0; i < queues; i++) {
307 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
308 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
309 i, chr->label);
310 nc->queue_index = i;
311 if (!nc0) {
312 nc0 = nc;
313 s = DO_UPCAST(NetVhostUserState, nc, nc);
314 if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
315 error_report_err(err);
316 goto err;
317 }
318 user->chr = &s->chr;
319 }
320 s = DO_UPCAST(NetVhostUserState, nc, nc);
321 s->vhost_user = user;
322 }
323
324 s = DO_UPCAST(NetVhostUserState, nc, nc0);
325 do {
326 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
327 error_report_err(err);
328 goto err;
329 }
330 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
331 net_vhost_user_event, NULL, nc0->name, NULL,
332 true);
333 } while (!s->started);
334
335 assert(s->vhost_net);
336
337 return 0;
338
339err:
340 if (user) {
341 vhost_user_cleanup(user);
342 g_free(user);
343 if (s) {
344 s->vhost_user = NULL;
345 }
346 }
347 if (nc0) {
348 qemu_del_net_client(nc0);
349 }
350
351 return -1;
352}
353
354static Chardev *net_vhost_claim_chardev(
355 const NetdevVhostUserOptions *opts, Error **errp)
356{
357 Chardev *chr = qemu_chr_find(opts->chardev);
358
359 if (chr == NULL) {
360 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
361 return NULL;
362 }
363
364 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
365 error_setg(errp, "chardev \"%s\" is not reconnectable",
366 opts->chardev);
367 return NULL;
368 }
369 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
370 error_setg(errp, "chardev \"%s\" does not support FD passing",
371 opts->chardev);
372 return NULL;
373 }
374
375 return chr;
376}
377
378static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
379{
380 const char *name = opaque;
381 const char *driver, *netdev;
382
383 driver = qemu_opt_get(opts, "driver");
384 netdev = qemu_opt_get(opts, "netdev");
385
386 if (!driver || !netdev) {
387 return 0;
388 }
389
390 if (strcmp(netdev, name) == 0 &&
391 !g_str_has_prefix(driver, "virtio-net-")) {
392 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
393 return -1;
394 }
395
396 return 0;
397}
398
399int net_init_vhost_user(const Netdev *netdev, const char *name,
400 NetClientState *peer, Error **errp)
401{
402 int queues;
403 const NetdevVhostUserOptions *vhost_user_opts;
404 Chardev *chr;
405
406 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
407 vhost_user_opts = &netdev->u.vhost_user;
408
409 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
410 if (!chr) {
411 return -1;
412 }
413
414
415 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
416 (char *)name, errp)) {
417 return -1;
418 }
419
420 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
421 if (queues < 1 || queues > MAX_QUEUE_NUM) {
422 error_setg(errp,
423 "vhost-user number of queues must be in range [1, %d]",
424 MAX_QUEUE_NUM);
425 return -1;
426 }
427
428 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
429}
430