4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
28 #include "config-host.h"
31 #include <sys/ioctl.h>
38 #include "qemu-char.h"
39 #include "qemu-common.h"
41 #include "net/tap-linux.h"
43 /* Maximum GSO packet size (64k) plus plenty of room for
44 * the ethernet and virtio_net headers
46 #define TAP_BUFSIZE (4096 + 65536)
48 typedef struct TAPState
{
51 char down_script
[1024];
52 char down_script_arg
[128];
53 uint8_t buf
[TAP_BUFSIZE
];
54 unsigned int read_poll
: 1;
55 unsigned int write_poll
: 1;
56 unsigned int has_vnet_hdr
: 1;
57 unsigned int using_vnet_hdr
: 1;
58 unsigned int has_ufo
: 1;
61 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
63 static int tap_can_send(void *opaque
);
64 static void tap_send(void *opaque
);
65 static void tap_writable(void *opaque
);
67 static void tap_update_fd_handler(TAPState
*s
)
69 qemu_set_fd_handler2(s
->fd
,
70 s
->read_poll
? tap_can_send
: NULL
,
71 s
->read_poll
? tap_send
: NULL
,
72 s
->write_poll
? tap_writable
: NULL
,
76 static void tap_read_poll(TAPState
*s
, int enable
)
78 s
->read_poll
= !!enable
;
79 tap_update_fd_handler(s
);
82 static void tap_write_poll(TAPState
*s
, int enable
)
84 s
->write_poll
= !!enable
;
85 tap_update_fd_handler(s
);
88 static void tap_writable(void *opaque
)
94 qemu_flush_queued_packets(s
->vc
);
97 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
102 len
= writev(s
->fd
, iov
, iovcnt
);
103 } while (len
== -1 && errno
== EINTR
);
105 if (len
== -1 && errno
== EAGAIN
) {
106 tap_write_poll(s
, 1);
113 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
116 TAPState
*s
= vc
->opaque
;
117 const struct iovec
*iovp
= iov
;
118 struct iovec iov_copy
[iovcnt
+ 1];
119 struct virtio_net_hdr hdr
= { 0, };
121 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
122 iov_copy
[0].iov_base
= &hdr
;
123 iov_copy
[0].iov_len
= sizeof(hdr
);
124 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
129 return tap_write_packet(s
, iovp
, iovcnt
);
132 static ssize_t
tap_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
134 TAPState
*s
= vc
->opaque
;
137 struct virtio_net_hdr hdr
= { 0, };
139 if (s
->has_vnet_hdr
) {
140 iov
[iovcnt
].iov_base
= &hdr
;
141 iov
[iovcnt
].iov_len
= sizeof(hdr
);
145 iov
[iovcnt
].iov_base
= (char *)buf
;
146 iov
[iovcnt
].iov_len
= size
;
149 return tap_write_packet(s
, iov
, iovcnt
);
152 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
154 TAPState
*s
= vc
->opaque
;
157 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
158 return tap_receive_raw(vc
, buf
, size
);
161 iov
[0].iov_base
= (char *)buf
;
162 iov
[0].iov_len
= size
;
164 return tap_write_packet(s
, iov
, 1);
167 static int tap_can_send(void *opaque
)
169 TAPState
*s
= opaque
;
171 return qemu_can_send_packet(s
->vc
);
175 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
177 return read(tapfd
, buf
, maxlen
);
181 static void tap_send_completed(VLANClientState
*vc
, ssize_t len
)
183 TAPState
*s
= vc
->opaque
;
187 static void tap_send(void *opaque
)
189 TAPState
*s
= opaque
;
193 uint8_t *buf
= s
->buf
;
195 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
200 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
201 buf
+= sizeof(struct virtio_net_hdr
);
202 size
-= sizeof(struct virtio_net_hdr
);
205 size
= qemu_send_packet_async(s
->vc
, buf
, size
, tap_send_completed
);
212 int tap_has_ufo(VLANClientState
*vc
)
214 TAPState
*s
= vc
->opaque
;
216 assert(vc
->type
== NET_CLIENT_TYPE_TAP
);
221 int tap_has_vnet_hdr(VLANClientState
*vc
)
223 TAPState
*s
= vc
->opaque
;
225 assert(vc
->type
== NET_CLIENT_TYPE_TAP
);
227 return s
->has_vnet_hdr
;
230 void tap_using_vnet_hdr(VLANClientState
*vc
, int using_vnet_hdr
)
232 TAPState
*s
= vc
->opaque
;
234 using_vnet_hdr
= using_vnet_hdr
!= 0;
236 assert(vc
->type
== NET_CLIENT_TYPE_TAP
);
237 assert(s
->has_vnet_hdr
== using_vnet_hdr
);
239 s
->using_vnet_hdr
= using_vnet_hdr
;
242 void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
,
243 int tso6
, int ecn
, int ufo
)
245 TAPState
*s
= vc
->opaque
;
247 return tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
250 static void tap_cleanup(VLANClientState
*vc
)
252 TAPState
*s
= vc
->opaque
;
254 qemu_purge_queued_packets(vc
);
256 if (s
->down_script
[0])
257 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
260 tap_write_poll(s
, 0);
267 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
275 s
= qemu_mallocz(sizeof(TAPState
));
277 s
->has_vnet_hdr
= vnet_hdr
!= 0;
278 s
->using_vnet_hdr
= 0;
279 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_TAP
,
280 vlan
, NULL
, model
, name
, NULL
,
281 tap_receive
, tap_receive_raw
,
282 tap_receive_iov
, tap_cleanup
, s
);
283 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
284 tap_set_offload(s
->vc
, 0, 0, 0, 0, 0);
289 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
291 sigset_t oldmask
, mask
;
297 sigaddset(&mask
, SIGCHLD
);
298 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
300 /* try to launch network script */
303 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
305 for (i
= 0; i
< open_max
; i
++) {
306 if (i
!= STDIN_FILENO
&&
307 i
!= STDOUT_FILENO
&&
308 i
!= STDERR_FILENO
&&
314 *parg
++ = (char *)setup_script
;
315 *parg
++ = (char *)ifname
;
317 execv(setup_script
, args
);
319 } else if (pid
> 0) {
320 while (waitpid(pid
, &status
, 0) != pid
) {
323 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
325 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
329 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
333 static int net_tap_init(QemuOpts
*opts
, int *vnet_hdr
)
335 int fd
, vnet_hdr_required
;
336 char ifname
[128] = {0,};
337 const char *setup_script
;
339 if (qemu_opt_get(opts
, "ifname")) {
340 pstrcpy(ifname
, sizeof(ifname
), qemu_opt_get(opts
, "ifname"));
343 *vnet_hdr
= qemu_opt_get_bool(opts
, "vnet_hdr", 1);
344 if (qemu_opt_get(opts
, "vnet_hdr")) {
345 vnet_hdr_required
= *vnet_hdr
;
347 vnet_hdr_required
= 0;
350 TFR(fd
= tap_open(ifname
, sizeof(ifname
), vnet_hdr
, vnet_hdr_required
));
355 setup_script
= qemu_opt_get(opts
, "script");
357 setup_script
[0] != '\0' &&
358 strcmp(setup_script
, "no") != 0 &&
359 launch_script(setup_script
, ifname
, fd
)) {
364 qemu_opt_set(opts
, "ifname", ifname
);
369 int net_init_tap(QemuOpts
*opts
, Monitor
*mon
, const char *name
, VLANState
*vlan
)
374 if (qemu_opt_get(opts
, "fd")) {
375 if (qemu_opt_get(opts
, "ifname") ||
376 qemu_opt_get(opts
, "script") ||
377 qemu_opt_get(opts
, "downscript") ||
378 qemu_opt_get(opts
, "vnet_hdr")) {
379 qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
383 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
388 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
390 vnet_hdr
= tap_probe_vnet_hdr(fd
);
392 if (!qemu_opt_get(opts
, "script")) {
393 qemu_opt_set(opts
, "script", DEFAULT_NETWORK_SCRIPT
);
396 if (!qemu_opt_get(opts
, "downscript")) {
397 qemu_opt_set(opts
, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT
);
400 fd
= net_tap_init(opts
, &vnet_hdr
);
403 s
= net_tap_fd_init(vlan
, "tap", name
, fd
, vnet_hdr
);
409 if (tap_set_sndbuf(s
->fd
, opts
) < 0) {
413 if (qemu_opt_get(opts
, "fd")) {
414 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
416 const char *ifname
, *script
, *downscript
;
418 ifname
= qemu_opt_get(opts
, "ifname");
419 script
= qemu_opt_get(opts
, "script");
420 downscript
= qemu_opt_get(opts
, "downscript");
422 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
423 "ifname=%s,script=%s,downscript=%s",
424 ifname
, script
, downscript
);
426 if (strcmp(downscript
, "no") != 0) {
427 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
428 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
433 vlan
->nb_host_devs
++;