blob: 4afb314fde032bdd7ee15fa07047c8f7e4684438 [file] [log] [blame]
Mark McLoughlin5281d752009-10-22 17:49:07 +01001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "net/tap.h"
27
28#include "config-host.h"
29
30#include <signal.h>
31#include <sys/ioctl.h>
32#include <sys/stat.h>
33#include <sys/wait.h>
Alexander Graf71f4eff2009-10-30 22:27:00 +010034#include <sys/socket.h>
Mark McLoughlin5281d752009-10-22 17:49:07 +010035#include <net/if.h>
36
37#include "net.h"
38#include "sysemu.h"
39#include "qemu-char.h"
40#include "qemu-common.h"
Markus Armbruster2f792012010-02-18 16:24:31 +010041#include "qemu-error.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010042
Mark McLoughlin5281d752009-10-22 17:49:07 +010043#include "net/tap-linux.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010044
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020045#include "hw/vhost_net.h"
46
Mark McLoughlin5281d752009-10-22 17:49:07 +010047/* Maximum GSO packet size (64k) plus plenty of room for
48 * the ethernet and virtio_net headers
49 */
50#define TAP_BUFSIZE (4096 + 65536)
51
52typedef struct TAPState {
Mark McLoughlin3e35ba92009-11-25 18:49:04 +000053 VLANClientState nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +010054 int fd;
55 char down_script[1024];
56 char down_script_arg[128];
57 uint8_t buf[TAP_BUFSIZE];
58 unsigned int read_poll : 1;
59 unsigned int write_poll : 1;
Mark McLoughlin5281d752009-10-22 17:49:07 +010060 unsigned int using_vnet_hdr : 1;
61 unsigned int has_ufo: 1;
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020062 VHostNetState *vhost_net;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +030063 unsigned host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +010064} TAPState;
65
66static int launch_script(const char *setup_script, const char *ifname, int fd);
67
68static int tap_can_send(void *opaque);
69static void tap_send(void *opaque);
70static void tap_writable(void *opaque);
71
72static void tap_update_fd_handler(TAPState *s)
73{
74 qemu_set_fd_handler2(s->fd,
75 s->read_poll ? tap_can_send : NULL,
76 s->read_poll ? tap_send : NULL,
77 s->write_poll ? tap_writable : NULL,
78 s);
79}
80
81static void tap_read_poll(TAPState *s, int enable)
82{
83 s->read_poll = !!enable;
84 tap_update_fd_handler(s);
85}
86
87static void tap_write_poll(TAPState *s, int enable)
88{
89 s->write_poll = !!enable;
90 tap_update_fd_handler(s);
91}
92
93static void tap_writable(void *opaque)
94{
95 TAPState *s = opaque;
96
97 tap_write_poll(s, 0);
98
Mark McLoughlin3e35ba92009-11-25 18:49:04 +000099 qemu_flush_queued_packets(&s->nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100100}
101
102static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
103{
104 ssize_t len;
105
106 do {
107 len = writev(s->fd, iov, iovcnt);
108 } while (len == -1 && errno == EINTR);
109
110 if (len == -1 && errno == EAGAIN) {
111 tap_write_poll(s, 1);
112 return 0;
113 }
114
115 return len;
116}
117
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000118static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100119 int iovcnt)
120{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000121 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100122 const struct iovec *iovp = iov;
123 struct iovec iov_copy[iovcnt + 1];
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300124 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100125
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300126 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100127 iov_copy[0].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300128 iov_copy[0].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100129 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
130 iovp = iov_copy;
131 iovcnt++;
132 }
133
134 return tap_write_packet(s, iovp, iovcnt);
135}
136
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000137static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100138{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000139 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100140 struct iovec iov[2];
141 int iovcnt = 0;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300142 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100143
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300144 if (s->host_vnet_hdr_len) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100145 iov[iovcnt].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300146 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100147 iovcnt++;
148 }
149
150 iov[iovcnt].iov_base = (char *)buf;
151 iov[iovcnt].iov_len = size;
152 iovcnt++;
153
154 return tap_write_packet(s, iov, iovcnt);
155}
156
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000157static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100158{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000159 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100160 struct iovec iov[1];
161
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300162 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000163 return tap_receive_raw(nc, buf, size);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100164 }
165
166 iov[0].iov_base = (char *)buf;
167 iov[0].iov_len = size;
168
169 return tap_write_packet(s, iov, 1);
170}
171
172static int tap_can_send(void *opaque)
173{
174 TAPState *s = opaque;
175
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000176 return qemu_can_send_packet(&s->nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100177}
178
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100179#ifndef __sun__
180ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100181{
182 return read(tapfd, buf, maxlen);
183}
184#endif
185
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000186static void tap_send_completed(VLANClientState *nc, ssize_t len)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100187{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000188 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100189 tap_read_poll(s, 1);
190}
191
192static void tap_send(void *opaque)
193{
194 TAPState *s = opaque;
195 int size;
196
Mark McLoughlin5819c912009-10-27 18:16:39 +0000197 do {
198 uint8_t *buf = s->buf;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100199
Mark McLoughlin5819c912009-10-27 18:16:39 +0000200 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
201 if (size <= 0) {
202 break;
203 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100204
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300205 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
206 buf += s->host_vnet_hdr_len;
207 size -= s->host_vnet_hdr_len;
Mark McLoughlin5819c912009-10-27 18:16:39 +0000208 }
209
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000210 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
Mark McLoughlin5819c912009-10-27 18:16:39 +0000211 if (size == 0) {
212 tap_read_poll(s, 0);
213 }
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000214 } while (size > 0 && qemu_can_send_packet(&s->nc));
Mark McLoughlin5281d752009-10-22 17:49:07 +0100215}
216
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000217int tap_has_ufo(VLANClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100218{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000219 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100220
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000221 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100222
223 return s->has_ufo;
224}
225
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000226int tap_has_vnet_hdr(VLANClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100227{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000228 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100229
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000230 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100231
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300232 return !!s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100233}
234
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300235int tap_has_vnet_hdr_len(VLANClientState *nc, int len)
236{
237 TAPState *s = DO_UPCAST(TAPState, nc, nc);
238
239 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
240
241 return tap_probe_vnet_hdr_len(s->fd, len);
242}
243
244void tap_set_vnet_hdr_len(VLANClientState *nc, int len)
245{
246 TAPState *s = DO_UPCAST(TAPState, nc, nc);
247
248 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
249 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
250 len == sizeof(struct virtio_net_hdr));
251
252 tap_fd_set_vnet_hdr_len(s->fd, len);
253 s->host_vnet_hdr_len = len;
254}
255
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000256void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100257{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000258 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100259
260 using_vnet_hdr = using_vnet_hdr != 0;
261
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000262 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300263 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100264
265 s->using_vnet_hdr = using_vnet_hdr;
266}
267
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000268void tap_set_offload(VLANClientState *nc, int csum, int tso4,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100269 int tso6, int ecn, int ufo)
270{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000271 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100272
Mark McLoughlin1faac1f2009-10-22 17:49:15 +0100273 return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100274}
275
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000276static void tap_cleanup(VLANClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100277{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000278 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100279
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200280 if (s->vhost_net) {
281 vhost_net_cleanup(s->vhost_net);
282 }
283
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000284 qemu_purge_queued_packets(nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100285
286 if (s->down_script[0])
287 launch_script(s->down_script, s->down_script_arg, s->fd);
288
289 tap_read_poll(s, 0);
290 tap_write_poll(s, 0);
291 close(s->fd);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100292}
293
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200294static void tap_poll(VLANClientState *nc, bool enable)
295{
296 TAPState *s = DO_UPCAST(TAPState, nc, nc);
297 tap_read_poll(s, enable);
298 tap_write_poll(s, enable);
299}
300
Michael S. Tsirkin95d528a2010-03-17 13:07:50 +0200301int tap_get_fd(VLANClientState *nc)
302{
303 TAPState *s = DO_UPCAST(TAPState, nc, nc);
304 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
305 return s->fd;
306}
307
Mark McLoughlin5281d752009-10-22 17:49:07 +0100308/* fd support */
309
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000310static NetClientInfo net_tap_info = {
311 .type = NET_CLIENT_TYPE_TAP,
312 .size = sizeof(TAPState),
313 .receive = tap_receive,
314 .receive_raw = tap_receive_raw,
315 .receive_iov = tap_receive_iov,
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200316 .poll = tap_poll,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000317 .cleanup = tap_cleanup,
318};
319
Mark McLoughlin5281d752009-10-22 17:49:07 +0100320static TAPState *net_tap_fd_init(VLANState *vlan,
321 const char *model,
322 const char *name,
323 int fd,
324 int vnet_hdr)
325{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000326 VLANClientState *nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100327 TAPState *s;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100328
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000329 nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
330
331 s = DO_UPCAST(TAPState, nc, nc);
332
Mark McLoughlin5281d752009-10-22 17:49:07 +0100333 s->fd = fd;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300334 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100335 s->using_vnet_hdr = 0;
Mark McLoughlin9c282712009-10-22 17:49:16 +0100336 s->has_ufo = tap_probe_has_ufo(s->fd);
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000337 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100338 tap_read_poll(s, 1);
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200339 s->vhost_net = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100340 return s;
341}
342
Mark McLoughlin5281d752009-10-22 17:49:07 +0100343static int launch_script(const char *setup_script, const char *ifname, int fd)
344{
345 sigset_t oldmask, mask;
346 int pid, status;
347 char *args[3];
348 char **parg;
349
350 sigemptyset(&mask);
351 sigaddset(&mask, SIGCHLD);
352 sigprocmask(SIG_BLOCK, &mask, &oldmask);
353
354 /* try to launch network script */
355 pid = fork();
356 if (pid == 0) {
357 int open_max = sysconf(_SC_OPEN_MAX), i;
358
359 for (i = 0; i < open_max; i++) {
360 if (i != STDIN_FILENO &&
361 i != STDOUT_FILENO &&
362 i != STDERR_FILENO &&
363 i != fd) {
364 close(i);
365 }
366 }
367 parg = args;
368 *parg++ = (char *)setup_script;
369 *parg++ = (char *)ifname;
Blue Swirl9678d952010-04-25 18:35:52 +0000370 *parg = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100371 execv(setup_script, args);
372 _exit(1);
373 } else if (pid > 0) {
374 while (waitpid(pid, &status, 0) != pid) {
375 /* loop */
376 }
377 sigprocmask(SIG_SETMASK, &oldmask, NULL);
378
379 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
380 return 0;
381 }
382 }
383 fprintf(stderr, "%s: could not launch network script\n", setup_script);
384 return -1;
385}
386
387static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
388{
389 int fd, vnet_hdr_required;
390 char ifname[128] = {0,};
391 const char *setup_script;
392
393 if (qemu_opt_get(opts, "ifname")) {
394 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
395 }
396
397 *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
398 if (qemu_opt_get(opts, "vnet_hdr")) {
399 vnet_hdr_required = *vnet_hdr;
400 } else {
401 vnet_hdr_required = 0;
402 }
403
404 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
405 if (fd < 0) {
406 return -1;
407 }
408
409 setup_script = qemu_opt_get(opts, "script");
410 if (setup_script &&
411 setup_script[0] != '\0' &&
412 strcmp(setup_script, "no") != 0 &&
413 launch_script(setup_script, ifname, fd)) {
414 close(fd);
415 return -1;
416 }
417
418 qemu_opt_set(opts, "ifname", ifname);
419
420 return fd;
421}
422
423int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
424{
425 TAPState *s;
Mark McLoughlindf6c2a02009-11-25 18:49:36 +0000426 int fd, vnet_hdr = 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100427
428 if (qemu_opt_get(opts, "fd")) {
429 if (qemu_opt_get(opts, "ifname") ||
430 qemu_opt_get(opts, "script") ||
431 qemu_opt_get(opts, "downscript") ||
432 qemu_opt_get(opts, "vnet_hdr")) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100433 error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
Mark McLoughlin5281d752009-10-22 17:49:07 +0100434 return -1;
435 }
436
437 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
438 if (fd == -1) {
439 return -1;
440 }
441
442 fcntl(fd, F_SETFL, O_NONBLOCK);
443
444 vnet_hdr = tap_probe_vnet_hdr(fd);
445 } else {
446 if (!qemu_opt_get(opts, "script")) {
447 qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
448 }
449
450 if (!qemu_opt_get(opts, "downscript")) {
451 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
452 }
453
454 fd = net_tap_init(opts, &vnet_hdr);
Juergen Lock929fe492009-11-20 23:23:03 +0100455 if (fd == -1) {
456 return -1;
457 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100458 }
459
460 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
461 if (!s) {
462 close(fd);
463 return -1;
464 }
465
Mark McLoughlin15ac9132009-10-22 17:49:13 +0100466 if (tap_set_sndbuf(s->fd, opts) < 0) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100467 return -1;
468 }
469
470 if (qemu_opt_get(opts, "fd")) {
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000471 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100472 } else {
473 const char *ifname, *script, *downscript;
474
475 ifname = qemu_opt_get(opts, "ifname");
476 script = qemu_opt_get(opts, "script");
477 downscript = qemu_opt_get(opts, "downscript");
478
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000479 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin5281d752009-10-22 17:49:07 +0100480 "ifname=%s,script=%s,downscript=%s",
481 ifname, script, downscript);
482
483 if (strcmp(downscript, "no") != 0) {
484 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
485 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
486 }
487 }
488
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200489 if (qemu_opt_get_bool(opts, "vhost", !!qemu_opt_get(opts, "vhostfd"))) {
490 int vhostfd, r;
491 if (qemu_opt_get(opts, "vhostfd")) {
492 r = net_handle_fd_param(mon, qemu_opt_get(opts, "vhostfd"));
493 if (r == -1) {
494 return -1;
495 }
496 vhostfd = r;
497 } else {
498 vhostfd = -1;
499 }
500 s->vhost_net = vhost_net_init(&s->nc, vhostfd);
501 if (!s->vhost_net) {
502 error_report("vhost-net requested but could not be initialized");
503 return -1;
504 }
505 } else if (qemu_opt_get(opts, "vhostfd")) {
506 error_report("vhostfd= is not valid without vhost");
507 return -1;
508 }
509
Mark McLoughlin5281d752009-10-22 17:49:07 +0100510 return 0;
511}
Michael S. Tsirkinb2025542010-03-17 13:08:38 +0200512
513VHostNetState *tap_get_vhost_net(VLANClientState *nc)
514{
515 TAPState *s = DO_UPCAST(TAPState, nc, nc);
516 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
517 return s->vhost_net;
518}