]> git.proxmox.com Git - mirror_qemu.git/blame - net/tap.c
virtio-net: split the has_buffers() logic from can_receive()
[mirror_qemu.git] / net / tap.c
CommitLineData
5281d757
MM
1/*
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>
71f4effc 34#include <sys/socket.h>
5281d757
MM
35#include <net/if.h>
36
37#include "net.h"
38#include "sysemu.h"
39#include "qemu-char.h"
40#include "qemu-common.h"
41
5281d757 42#include "net/tap-linux.h"
5281d757 43
5281d757
MM
44/* Maximum GSO packet size (64k) plus plenty of room for
45 * the ethernet and virtio_net headers
46 */
47#define TAP_BUFSIZE (4096 + 65536)
48
49typedef struct TAPState {
50 VLANClientState *vc;
51 int fd;
52 char down_script[1024];
53 char down_script_arg[128];
54 uint8_t buf[TAP_BUFSIZE];
55 unsigned int read_poll : 1;
56 unsigned int write_poll : 1;
57 unsigned int has_vnet_hdr : 1;
58 unsigned int using_vnet_hdr : 1;
59 unsigned int has_ufo: 1;
60} TAPState;
61
62static int launch_script(const char *setup_script, const char *ifname, int fd);
63
64static int tap_can_send(void *opaque);
65static void tap_send(void *opaque);
66static void tap_writable(void *opaque);
67
68static void tap_update_fd_handler(TAPState *s)
69{
70 qemu_set_fd_handler2(s->fd,
71 s->read_poll ? tap_can_send : NULL,
72 s->read_poll ? tap_send : NULL,
73 s->write_poll ? tap_writable : NULL,
74 s);
75}
76
77static void tap_read_poll(TAPState *s, int enable)
78{
79 s->read_poll = !!enable;
80 tap_update_fd_handler(s);
81}
82
83static void tap_write_poll(TAPState *s, int enable)
84{
85 s->write_poll = !!enable;
86 tap_update_fd_handler(s);
87}
88
89static void tap_writable(void *opaque)
90{
91 TAPState *s = opaque;
92
93 tap_write_poll(s, 0);
94
95 qemu_flush_queued_packets(s->vc);
96}
97
98static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
99{
100 ssize_t len;
101
102 do {
103 len = writev(s->fd, iov, iovcnt);
104 } while (len == -1 && errno == EINTR);
105
106 if (len == -1 && errno == EAGAIN) {
107 tap_write_poll(s, 1);
108 return 0;
109 }
110
111 return len;
112}
113
114static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
115 int iovcnt)
116{
117 TAPState *s = vc->opaque;
118 const struct iovec *iovp = iov;
119 struct iovec iov_copy[iovcnt + 1];
120 struct virtio_net_hdr hdr = { 0, };
121
122 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
123 iov_copy[0].iov_base = &hdr;
124 iov_copy[0].iov_len = sizeof(hdr);
125 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
126 iovp = iov_copy;
127 iovcnt++;
128 }
129
130 return tap_write_packet(s, iovp, iovcnt);
131}
132
133static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
134{
135 TAPState *s = vc->opaque;
136 struct iovec iov[2];
137 int iovcnt = 0;
138 struct virtio_net_hdr hdr = { 0, };
139
140 if (s->has_vnet_hdr) {
141 iov[iovcnt].iov_base = &hdr;
142 iov[iovcnt].iov_len = sizeof(hdr);
143 iovcnt++;
144 }
145
146 iov[iovcnt].iov_base = (char *)buf;
147 iov[iovcnt].iov_len = size;
148 iovcnt++;
149
150 return tap_write_packet(s, iov, iovcnt);
151}
152
153static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
154{
155 TAPState *s = vc->opaque;
156 struct iovec iov[1];
157
158 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
159 return tap_receive_raw(vc, buf, size);
160 }
161
162 iov[0].iov_base = (char *)buf;
163 iov[0].iov_len = size;
164
165 return tap_write_packet(s, iov, 1);
166}
167
168static int tap_can_send(void *opaque)
169{
170 TAPState *s = opaque;
171
172 return qemu_can_send_packet(s->vc);
173}
174
966ea5ec
MM
175#ifndef __sun__
176ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
5281d757
MM
177{
178 return read(tapfd, buf, maxlen);
179}
180#endif
181
182static void tap_send_completed(VLANClientState *vc, ssize_t len)
183{
184 TAPState *s = vc->opaque;
185 tap_read_poll(s, 1);
186}
187
188static void tap_send(void *opaque)
189{
190 TAPState *s = opaque;
be1636b3 191 uint8_t *buf = s->buf;
5281d757
MM
192 int size;
193
be1636b3
MM
194 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
195 if (size <= 0) {
196 return;
197 }
198
199 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
200 buf += sizeof(struct virtio_net_hdr);
201 size -= sizeof(struct virtio_net_hdr);
202 }
203
204 size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed);
205 if (size == 0) {
206 tap_read_poll(s, 0);
207 }
5281d757
MM
208}
209
5281d757
MM
210int tap_has_ufo(VLANClientState *vc)
211{
212 TAPState *s = vc->opaque;
213
214 assert(vc->type == NET_CLIENT_TYPE_TAP);
215
216 return s->has_ufo;
217}
218
219int tap_has_vnet_hdr(VLANClientState *vc)
220{
221 TAPState *s = vc->opaque;
222
223 assert(vc->type == NET_CLIENT_TYPE_TAP);
224
225 return s->has_vnet_hdr;
226}
227
228void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr)
229{
230 TAPState *s = vc->opaque;
231
232 using_vnet_hdr = using_vnet_hdr != 0;
233
234 assert(vc->type == NET_CLIENT_TYPE_TAP);
235 assert(s->has_vnet_hdr == using_vnet_hdr);
236
237 s->using_vnet_hdr = using_vnet_hdr;
238}
239
5281d757
MM
240void tap_set_offload(VLANClientState *vc, int csum, int tso4,
241 int tso6, int ecn, int ufo)
242{
243 TAPState *s = vc->opaque;
5281d757 244
1faac1f7 245 return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
5281d757
MM
246}
247
248static void tap_cleanup(VLANClientState *vc)
249{
250 TAPState *s = vc->opaque;
251
252 qemu_purge_queued_packets(vc);
253
254 if (s->down_script[0])
255 launch_script(s->down_script, s->down_script_arg, s->fd);
256
257 tap_read_poll(s, 0);
258 tap_write_poll(s, 0);
259 close(s->fd);
260 qemu_free(s);
261}
262
263/* fd support */
264
265static TAPState *net_tap_fd_init(VLANState *vlan,
266 const char *model,
267 const char *name,
268 int fd,
269 int vnet_hdr)
270{
271 TAPState *s;
5281d757
MM
272
273 s = qemu_mallocz(sizeof(TAPState));
274 s->fd = fd;
275 s->has_vnet_hdr = vnet_hdr != 0;
276 s->using_vnet_hdr = 0;
277 s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_TAP,
278 vlan, NULL, model, name, NULL,
279 tap_receive, tap_receive_raw,
280 tap_receive_iov, tap_cleanup, s);
9c282718 281 s->has_ufo = tap_probe_has_ufo(s->fd);
5281d757
MM
282 tap_set_offload(s->vc, 0, 0, 0, 0, 0);
283 tap_read_poll(s, 1);
284 return s;
285}
286
5281d757
MM
287static int launch_script(const char *setup_script, const char *ifname, int fd)
288{
289 sigset_t oldmask, mask;
290 int pid, status;
291 char *args[3];
292 char **parg;
293
294 sigemptyset(&mask);
295 sigaddset(&mask, SIGCHLD);
296 sigprocmask(SIG_BLOCK, &mask, &oldmask);
297
298 /* try to launch network script */
299 pid = fork();
300 if (pid == 0) {
301 int open_max = sysconf(_SC_OPEN_MAX), i;
302
303 for (i = 0; i < open_max; i++) {
304 if (i != STDIN_FILENO &&
305 i != STDOUT_FILENO &&
306 i != STDERR_FILENO &&
307 i != fd) {
308 close(i);
309 }
310 }
311 parg = args;
312 *parg++ = (char *)setup_script;
313 *parg++ = (char *)ifname;
314 *parg++ = NULL;
315 execv(setup_script, args);
316 _exit(1);
317 } else if (pid > 0) {
318 while (waitpid(pid, &status, 0) != pid) {
319 /* loop */
320 }
321 sigprocmask(SIG_SETMASK, &oldmask, NULL);
322
323 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
324 return 0;
325 }
326 }
327 fprintf(stderr, "%s: could not launch network script\n", setup_script);
328 return -1;
329}
330
331static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
332{
333 int fd, vnet_hdr_required;
334 char ifname[128] = {0,};
335 const char *setup_script;
336
337 if (qemu_opt_get(opts, "ifname")) {
338 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
339 }
340
341 *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
342 if (qemu_opt_get(opts, "vnet_hdr")) {
343 vnet_hdr_required = *vnet_hdr;
344 } else {
345 vnet_hdr_required = 0;
346 }
347
348 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
349 if (fd < 0) {
350 return -1;
351 }
352
353 setup_script = qemu_opt_get(opts, "script");
354 if (setup_script &&
355 setup_script[0] != '\0' &&
356 strcmp(setup_script, "no") != 0 &&
357 launch_script(setup_script, ifname, fd)) {
358 close(fd);
359 return -1;
360 }
361
362 qemu_opt_set(opts, "ifname", ifname);
363
364 return fd;
365}
366
367int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
368{
369 TAPState *s;
370 int fd, vnet_hdr;
371
372 if (qemu_opt_get(opts, "fd")) {
373 if (qemu_opt_get(opts, "ifname") ||
374 qemu_opt_get(opts, "script") ||
375 qemu_opt_get(opts, "downscript") ||
376 qemu_opt_get(opts, "vnet_hdr")) {
377 qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
378 return -1;
379 }
380
381 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
382 if (fd == -1) {
383 return -1;
384 }
385
386 fcntl(fd, F_SETFL, O_NONBLOCK);
387
388 vnet_hdr = tap_probe_vnet_hdr(fd);
389 } else {
390 if (!qemu_opt_get(opts, "script")) {
391 qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
392 }
393
394 if (!qemu_opt_get(opts, "downscript")) {
395 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
396 }
397
398 fd = net_tap_init(opts, &vnet_hdr);
399 }
400
401 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
402 if (!s) {
403 close(fd);
404 return -1;
405 }
406
15ac913b 407 if (tap_set_sndbuf(s->fd, opts) < 0) {
5281d757
MM
408 return -1;
409 }
410
411 if (qemu_opt_get(opts, "fd")) {
412 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
413 } else {
414 const char *ifname, *script, *downscript;
415
416 ifname = qemu_opt_get(opts, "ifname");
417 script = qemu_opt_get(opts, "script");
418 downscript = qemu_opt_get(opts, "downscript");
419
420 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
421 "ifname=%s,script=%s,downscript=%s",
422 ifname, script, downscript);
423
424 if (strcmp(downscript, "no") != 0) {
425 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
426 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
427 }
428 }
429
430 if (vlan) {
431 vlan->nb_host_devs++;
432 }
433
434 return 0;
435}