Statistics
| Branch: | Revision:

root / net / tap.c @ 1ecda02b

History | View | Annotate | Download (11.5 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 <sys/socket.h>
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
#include "qemu-error.h"
42

    
43
#include "net/tap-linux.h"
44

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

    
50
typedef struct TAPState {
51
    VLANClientState nc;
52
    int fd;
53
    char down_script[1024];
54
    char down_script_arg[128];
55
    uint8_t buf[TAP_BUFSIZE];
56
    unsigned int read_poll : 1;
57
    unsigned int write_poll : 1;
58
    unsigned int has_vnet_hdr : 1;
59
    unsigned int using_vnet_hdr : 1;
60
    unsigned int has_ufo: 1;
61
} TAPState;
62

    
63
static int launch_script(const char *setup_script, const char *ifname, int fd);
64

    
65
static int tap_can_send(void *opaque);
66
static void tap_send(void *opaque);
67
static void tap_writable(void *opaque);
68

    
69
static void tap_update_fd_handler(TAPState *s)
70
{
71
    qemu_set_fd_handler2(s->fd,
72
                         s->read_poll  ? tap_can_send : NULL,
73
                         s->read_poll  ? tap_send     : NULL,
74
                         s->write_poll ? tap_writable : NULL,
75
                         s);
76
}
77

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

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

    
90
static void tap_writable(void *opaque)
91
{
92
    TAPState *s = opaque;
93

    
94
    tap_write_poll(s, 0);
95

    
96
    qemu_flush_queued_packets(&s->nc);
97
}
98

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

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

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

    
112
    return len;
113
}
114

    
115
static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
116
                               int iovcnt)
117
{
118
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
119
    const struct iovec *iovp = iov;
120
    struct iovec iov_copy[iovcnt + 1];
121
    struct virtio_net_hdr hdr = { 0, };
122

    
123
    if (s->has_vnet_hdr && !s->using_vnet_hdr) {
124
        iov_copy[0].iov_base = &hdr;
125
        iov_copy[0].iov_len =  sizeof(hdr);
126
        memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
127
        iovp = iov_copy;
128
        iovcnt++;
129
    }
130

    
131
    return tap_write_packet(s, iovp, iovcnt);
132
}
133

    
134
static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
135
{
136
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
137
    struct iovec iov[2];
138
    int iovcnt = 0;
139
    struct virtio_net_hdr hdr = { 0, };
140

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

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

    
151
    return tap_write_packet(s, iov, iovcnt);
152
}
153

    
154
static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
155
{
156
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
157
    struct iovec iov[1];
158

    
159
    if (s->has_vnet_hdr && !s->using_vnet_hdr) {
160
        return tap_receive_raw(nc, buf, size);
161
    }
162

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

    
166
    return tap_write_packet(s, iov, 1);
167
}
168

    
169
static int tap_can_send(void *opaque)
170
{
171
    TAPState *s = opaque;
172

    
173
    return qemu_can_send_packet(&s->nc);
174
}
175

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

    
183
static void tap_send_completed(VLANClientState *nc, ssize_t len)
184
{
185
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
186
    tap_read_poll(s, 1);
187
}
188

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

    
194
    do {
195
        uint8_t *buf = s->buf;
196

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

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

    
207
        size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
208
        if (size == 0) {
209
            tap_read_poll(s, 0);
210
        }
211
    } while (size > 0 && qemu_can_send_packet(&s->nc));
212
}
213

    
214
int tap_has_ufo(VLANClientState *nc)
215
{
216
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
217

    
218
    assert(nc->info->type == NET_CLIENT_TYPE_TAP);
219

    
220
    return s->has_ufo;
221
}
222

    
223
int tap_has_vnet_hdr(VLANClientState *nc)
224
{
225
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
226

    
227
    assert(nc->info->type == NET_CLIENT_TYPE_TAP);
228

    
229
    return s->has_vnet_hdr;
230
}
231

    
232
void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
233
{
234
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
235

    
236
    using_vnet_hdr = using_vnet_hdr != 0;
237

    
238
    assert(nc->info->type == NET_CLIENT_TYPE_TAP);
239
    assert(s->has_vnet_hdr == using_vnet_hdr);
240

    
241
    s->using_vnet_hdr = using_vnet_hdr;
242
}
243

    
244
void tap_set_offload(VLANClientState *nc, int csum, int tso4,
245
                     int tso6, int ecn, int ufo)
246
{
247
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
248

    
249
    return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
250
}
251

    
252
static void tap_cleanup(VLANClientState *nc)
253
{
254
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
255

    
256
    qemu_purge_queued_packets(nc);
257

    
258
    if (s->down_script[0])
259
        launch_script(s->down_script, s->down_script_arg, s->fd);
260

    
261
    tap_read_poll(s, 0);
262
    tap_write_poll(s, 0);
263
    close(s->fd);
264
}
265

    
266
static void tap_poll(VLANClientState *nc, bool enable)
267
{
268
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
269
    tap_read_poll(s, enable);
270
    tap_write_poll(s, enable);
271
}
272

    
273
/* fd support */
274

    
275
static NetClientInfo net_tap_info = {
276
    .type = NET_CLIENT_TYPE_TAP,
277
    .size = sizeof(TAPState),
278
    .receive = tap_receive,
279
    .receive_raw = tap_receive_raw,
280
    .receive_iov = tap_receive_iov,
281
    .poll = tap_poll,
282
    .cleanup = tap_cleanup,
283
};
284

    
285
static TAPState *net_tap_fd_init(VLANState *vlan,
286
                                 const char *model,
287
                                 const char *name,
288
                                 int fd,
289
                                 int vnet_hdr)
290
{
291
    VLANClientState *nc;
292
    TAPState *s;
293

    
294
    nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
295

    
296
    s = DO_UPCAST(TAPState, nc, nc);
297

    
298
    s->fd = fd;
299
    s->has_vnet_hdr = vnet_hdr != 0;
300
    s->using_vnet_hdr = 0;
301
    s->has_ufo = tap_probe_has_ufo(s->fd);
302
    tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
303
    tap_read_poll(s, 1);
304
    return s;
305
}
306

    
307
static int launch_script(const char *setup_script, const char *ifname, int fd)
308
{
309
    sigset_t oldmask, mask;
310
    int pid, status;
311
    char *args[3];
312
    char **parg;
313

    
314
    sigemptyset(&mask);
315
    sigaddset(&mask, SIGCHLD);
316
    sigprocmask(SIG_BLOCK, &mask, &oldmask);
317

    
318
    /* try to launch network script */
319
    pid = fork();
320
    if (pid == 0) {
321
        int open_max = sysconf(_SC_OPEN_MAX), i;
322

    
323
        for (i = 0; i < open_max; i++) {
324
            if (i != STDIN_FILENO &&
325
                i != STDOUT_FILENO &&
326
                i != STDERR_FILENO &&
327
                i != fd) {
328
                close(i);
329
            }
330
        }
331
        parg = args;
332
        *parg++ = (char *)setup_script;
333
        *parg++ = (char *)ifname;
334
        *parg++ = NULL;
335
        execv(setup_script, args);
336
        _exit(1);
337
    } else if (pid > 0) {
338
        while (waitpid(pid, &status, 0) != pid) {
339
            /* loop */
340
        }
341
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
342

    
343
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
344
            return 0;
345
        }
346
    }
347
    fprintf(stderr, "%s: could not launch network script\n", setup_script);
348
    return -1;
349
}
350

    
351
static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
352
{
353
    int fd, vnet_hdr_required;
354
    char ifname[128] = {0,};
355
    const char *setup_script;
356

    
357
    if (qemu_opt_get(opts, "ifname")) {
358
        pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
359
    }
360

    
361
    *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
362
    if (qemu_opt_get(opts, "vnet_hdr")) {
363
        vnet_hdr_required = *vnet_hdr;
364
    } else {
365
        vnet_hdr_required = 0;
366
    }
367

    
368
    TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
369
    if (fd < 0) {
370
        return -1;
371
    }
372

    
373
    setup_script = qemu_opt_get(opts, "script");
374
    if (setup_script &&
375
        setup_script[0] != '\0' &&
376
        strcmp(setup_script, "no") != 0 &&
377
        launch_script(setup_script, ifname, fd)) {
378
        close(fd);
379
        return -1;
380
    }
381

    
382
    qemu_opt_set(opts, "ifname", ifname);
383

    
384
    return fd;
385
}
386

    
387
int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
388
{
389
    TAPState *s;
390
    int fd, vnet_hdr = 0;
391

    
392
    if (qemu_opt_get(opts, "fd")) {
393
        if (qemu_opt_get(opts, "ifname") ||
394
            qemu_opt_get(opts, "script") ||
395
            qemu_opt_get(opts, "downscript") ||
396
            qemu_opt_get(opts, "vnet_hdr")) {
397
            error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
398
            return -1;
399
        }
400

    
401
        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
402
        if (fd == -1) {
403
            return -1;
404
        }
405

    
406
        fcntl(fd, F_SETFL, O_NONBLOCK);
407

    
408
        vnet_hdr = tap_probe_vnet_hdr(fd);
409
    } else {
410
        if (!qemu_opt_get(opts, "script")) {
411
            qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
412
        }
413

    
414
        if (!qemu_opt_get(opts, "downscript")) {
415
            qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
416
        }
417

    
418
        fd = net_tap_init(opts, &vnet_hdr);
419
        if (fd == -1) {
420
            return -1;
421
        }
422
    }
423

    
424
    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
425
    if (!s) {
426
        close(fd);
427
        return -1;
428
    }
429

    
430
    if (tap_set_sndbuf(s->fd, opts) < 0) {
431
        return -1;
432
    }
433

    
434
    if (qemu_opt_get(opts, "fd")) {
435
        snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
436
    } else {
437
        const char *ifname, *script, *downscript;
438

    
439
        ifname     = qemu_opt_get(opts, "ifname");
440
        script     = qemu_opt_get(opts, "script");
441
        downscript = qemu_opt_get(opts, "downscript");
442

    
443
        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
444
                 "ifname=%s,script=%s,downscript=%s",
445
                 ifname, script, downscript);
446

    
447
        if (strcmp(downscript, "no") != 0) {
448
            snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
449
            snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
450
        }
451
    }
452

    
453
    return 0;
454
}