Statistics
| Branch: | Revision:

root / net / tap.c @ 15ac913b

History | View | Annotate | Download (11.9 kB)

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>
34
#include <net/if.h>
35

    
36
#include "net.h"
37
#include "sysemu.h"
38
#include "qemu-char.h"
39
#include "qemu-common.h"
40

    
41
#include "net/tap-linux.h"
42

    
43
/* Maximum GSO packet size (64k) plus plenty of room for
44
 * the ethernet and virtio_net headers
45
 */
46
#define TAP_BUFSIZE (4096 + 65536)
47

    
48
typedef struct TAPState {
49
    VLANClientState *vc;
50
    int fd;
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;
59
} TAPState;
60

    
61
static int launch_script(const char *setup_script, const char *ifname, int fd);
62

    
63
static int tap_can_send(void *opaque);
64
static void tap_send(void *opaque);
65
static void tap_writable(void *opaque);
66

    
67
static void tap_update_fd_handler(TAPState *s)
68
{
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,
73
                         s);
74
}
75

    
76
static void tap_read_poll(TAPState *s, int enable)
77
{
78
    s->read_poll = !!enable;
79
    tap_update_fd_handler(s);
80
}
81

    
82
static void tap_write_poll(TAPState *s, int enable)
83
{
84
    s->write_poll = !!enable;
85
    tap_update_fd_handler(s);
86
}
87

    
88
static void tap_writable(void *opaque)
89
{
90
    TAPState *s = opaque;
91

    
92
    tap_write_poll(s, 0);
93

    
94
    qemu_flush_queued_packets(s->vc);
95
}
96

    
97
static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
98
{
99
    ssize_t len;
100

    
101
    do {
102
        len = writev(s->fd, iov, iovcnt);
103
    } while (len == -1 && errno == EINTR);
104

    
105
    if (len == -1 && errno == EAGAIN) {
106
        tap_write_poll(s, 1);
107
        return 0;
108
    }
109

    
110
    return len;
111
}
112

    
113
static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
114
                               int iovcnt)
115
{
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, };
120

    
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));
125
        iovp = iov_copy;
126
        iovcnt++;
127
    }
128

    
129
    return tap_write_packet(s, iovp, iovcnt);
130
}
131

    
132
static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
133
{
134
    TAPState *s = vc->opaque;
135
    struct iovec iov[2];
136
    int iovcnt = 0;
137
    struct virtio_net_hdr hdr = { 0, };
138

    
139
    if (s->has_vnet_hdr) {
140
        iov[iovcnt].iov_base = &hdr;
141
        iov[iovcnt].iov_len  = sizeof(hdr);
142
        iovcnt++;
143
    }
144

    
145
    iov[iovcnt].iov_base = (char *)buf;
146
    iov[iovcnt].iov_len  = size;
147
    iovcnt++;
148

    
149
    return tap_write_packet(s, iov, iovcnt);
150
}
151

    
152
static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
153
{
154
    TAPState *s = vc->opaque;
155
    struct iovec iov[1];
156

    
157
    if (s->has_vnet_hdr && !s->using_vnet_hdr) {
158
        return tap_receive_raw(vc, buf, size);
159
    }
160

    
161
    iov[0].iov_base = (char *)buf;
162
    iov[0].iov_len  = size;
163

    
164
    return tap_write_packet(s, iov, 1);
165
}
166

    
167
static int tap_can_send(void *opaque)
168
{
169
    TAPState *s = opaque;
170

    
171
    return qemu_can_send_packet(s->vc);
172
}
173

    
174
#ifndef __sun__
175
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
176
{
177
    return read(tapfd, buf, maxlen);
178
}
179
#endif
180

    
181
static void tap_send_completed(VLANClientState *vc, ssize_t len)
182
{
183
    TAPState *s = vc->opaque;
184
    tap_read_poll(s, 1);
185
}
186

    
187
static void tap_send(void *opaque)
188
{
189
    TAPState *s = opaque;
190
    int size;
191

    
192
    do {
193
        uint8_t *buf = s->buf;
194

    
195
        size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
196
        if (size <= 0) {
197
            break;
198
        }
199

    
200
        if (s->has_vnet_hdr && !s->using_vnet_hdr) {
201
            buf  += sizeof(struct virtio_net_hdr);
202
            size -= sizeof(struct virtio_net_hdr);
203
        }
204

    
205
        size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed);
206
        if (size == 0) {
207
            tap_read_poll(s, 0);
208
        }
209
    } while (size > 0);
210
}
211

    
212
int tap_has_ufo(VLANClientState *vc)
213
{
214
    TAPState *s = vc->opaque;
215

    
216
    assert(vc->type == NET_CLIENT_TYPE_TAP);
217

    
218
    return s->has_ufo;
219
}
220

    
221
int tap_has_vnet_hdr(VLANClientState *vc)
222
{
223
    TAPState *s = vc->opaque;
224

    
225
    assert(vc->type == NET_CLIENT_TYPE_TAP);
226

    
227
    return s->has_vnet_hdr;
228
}
229

    
230
void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr)
231
{
232
    TAPState *s = vc->opaque;
233

    
234
    using_vnet_hdr = using_vnet_hdr != 0;
235

    
236
    assert(vc->type == NET_CLIENT_TYPE_TAP);
237
    assert(s->has_vnet_hdr == using_vnet_hdr);
238

    
239
    s->using_vnet_hdr = using_vnet_hdr;
240
}
241

    
242
static int tap_probe_vnet_hdr(int fd)
243
{
244
    struct ifreq ifr;
245

    
246
    if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
247
        qemu_error("TUNGETIFF ioctl() failed: %s\n", strerror(errno));
248
        return 0;
249
    }
250

    
251
    return ifr.ifr_flags & IFF_VNET_HDR;
252
}
253

    
254
void tap_set_offload(VLANClientState *vc, int csum, int tso4,
255
                     int tso6, int ecn, int ufo)
256
{
257
    TAPState *s = vc->opaque;
258
    unsigned int offload = 0;
259

    
260
    if (csum) {
261
        offload |= TUN_F_CSUM;
262
        if (tso4)
263
            offload |= TUN_F_TSO4;
264
        if (tso6)
265
            offload |= TUN_F_TSO6;
266
        if ((tso4 || tso6) && ecn)
267
            offload |= TUN_F_TSO_ECN;
268
        if (ufo)
269
            offload |= TUN_F_UFO;
270
    }
271

    
272
    if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) {
273
        offload &= ~TUN_F_UFO;
274
        if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) {
275
            fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
276
                    strerror(errno));
277
        }
278
    }
279
}
280

    
281
static void tap_cleanup(VLANClientState *vc)
282
{
283
    TAPState *s = vc->opaque;
284

    
285
    qemu_purge_queued_packets(vc);
286

    
287
    if (s->down_script[0])
288
        launch_script(s->down_script, s->down_script_arg, s->fd);
289

    
290
    tap_read_poll(s, 0);
291
    tap_write_poll(s, 0);
292
    close(s->fd);
293
    qemu_free(s);
294
}
295

    
296
/* fd support */
297

    
298
static TAPState *net_tap_fd_init(VLANState *vlan,
299
                                 const char *model,
300
                                 const char *name,
301
                                 int fd,
302
                                 int vnet_hdr)
303
{
304
    TAPState *s;
305
    unsigned int offload;
306

    
307
    s = qemu_mallocz(sizeof(TAPState));
308
    s->fd = fd;
309
    s->has_vnet_hdr = vnet_hdr != 0;
310
    s->using_vnet_hdr = 0;
311
    s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_TAP,
312
                                 vlan, NULL, model, name, NULL,
313
                                 tap_receive, tap_receive_raw,
314
                                 tap_receive_iov, tap_cleanup, s);
315
    s->has_ufo = 0;
316
    /* Check if tap supports UFO */
317
    offload = TUN_F_CSUM | TUN_F_UFO;
318
    if (ioctl(s->fd, TUNSETOFFLOAD, offload) == 0)
319
       s->has_ufo = 1;
320
    tap_set_offload(s->vc, 0, 0, 0, 0, 0);
321
    tap_read_poll(s, 1);
322
    return s;
323
}
324

    
325
static int launch_script(const char *setup_script, const char *ifname, int fd)
326
{
327
    sigset_t oldmask, mask;
328
    int pid, status;
329
    char *args[3];
330
    char **parg;
331

    
332
    sigemptyset(&mask);
333
    sigaddset(&mask, SIGCHLD);
334
    sigprocmask(SIG_BLOCK, &mask, &oldmask);
335

    
336
    /* try to launch network script */
337
    pid = fork();
338
    if (pid == 0) {
339
        int open_max = sysconf(_SC_OPEN_MAX), i;
340

    
341
        for (i = 0; i < open_max; i++) {
342
            if (i != STDIN_FILENO &&
343
                i != STDOUT_FILENO &&
344
                i != STDERR_FILENO &&
345
                i != fd) {
346
                close(i);
347
            }
348
        }
349
        parg = args;
350
        *parg++ = (char *)setup_script;
351
        *parg++ = (char *)ifname;
352
        *parg++ = NULL;
353
        execv(setup_script, args);
354
        _exit(1);
355
    } else if (pid > 0) {
356
        while (waitpid(pid, &status, 0) != pid) {
357
            /* loop */
358
        }
359
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
360

    
361
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
362
            return 0;
363
        }
364
    }
365
    fprintf(stderr, "%s: could not launch network script\n", setup_script);
366
    return -1;
367
}
368

    
369
static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
370
{
371
    int fd, vnet_hdr_required;
372
    char ifname[128] = {0,};
373
    const char *setup_script;
374

    
375
    if (qemu_opt_get(opts, "ifname")) {
376
        pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
377
    }
378

    
379
    *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
380
    if (qemu_opt_get(opts, "vnet_hdr")) {
381
        vnet_hdr_required = *vnet_hdr;
382
    } else {
383
        vnet_hdr_required = 0;
384
    }
385

    
386
    TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
387
    if (fd < 0) {
388
        return -1;
389
    }
390

    
391
    setup_script = qemu_opt_get(opts, "script");
392
    if (setup_script &&
393
        setup_script[0] != '\0' &&
394
        strcmp(setup_script, "no") != 0 &&
395
        launch_script(setup_script, ifname, fd)) {
396
        close(fd);
397
        return -1;
398
    }
399

    
400
    qemu_opt_set(opts, "ifname", ifname);
401

    
402
    return fd;
403
}
404

    
405
int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
406
{
407
    TAPState *s;
408
    int fd, vnet_hdr;
409

    
410
    if (qemu_opt_get(opts, "fd")) {
411
        if (qemu_opt_get(opts, "ifname") ||
412
            qemu_opt_get(opts, "script") ||
413
            qemu_opt_get(opts, "downscript") ||
414
            qemu_opt_get(opts, "vnet_hdr")) {
415
            qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
416
            return -1;
417
        }
418

    
419
        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
420
        if (fd == -1) {
421
            return -1;
422
        }
423

    
424
        fcntl(fd, F_SETFL, O_NONBLOCK);
425

    
426
        vnet_hdr = tap_probe_vnet_hdr(fd);
427
    } else {
428
        if (!qemu_opt_get(opts, "script")) {
429
            qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
430
        }
431

    
432
        if (!qemu_opt_get(opts, "downscript")) {
433
            qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
434
        }
435

    
436
        fd = net_tap_init(opts, &vnet_hdr);
437
    }
438

    
439
    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
440
    if (!s) {
441
        close(fd);
442
        return -1;
443
    }
444

    
445
    if (tap_set_sndbuf(s->fd, opts) < 0) {
446
        return -1;
447
    }
448

    
449
    if (qemu_opt_get(opts, "fd")) {
450
        snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
451
    } else {
452
        const char *ifname, *script, *downscript;
453

    
454
        ifname     = qemu_opt_get(opts, "ifname");
455
        script     = qemu_opt_get(opts, "script");
456
        downscript = qemu_opt_get(opts, "downscript");
457

    
458
        snprintf(s->vc->info_str, sizeof(s->vc->info_str),
459
                 "ifname=%s,script=%s,downscript=%s",
460
                 ifname, script, downscript);
461

    
462
        if (strcmp(downscript, "no") != 0) {
463
            snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
464
            snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
465
        }
466
    }
467

    
468
    if (vlan) {
469
        vlan->nb_host_devs++;
470
    }
471

    
472
    return 0;
473
}