Statistics
| Branch: | Revision:

root / net / tap.c @ 9c282718

History | View | Annotate | Download (11 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
void tap_set_offload(VLANClientState *vc, int csum, int tso4,
243
                     int tso6, int ecn, int ufo)
244
{
245
    TAPState *s = vc->opaque;
246

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

    
250
static void tap_cleanup(VLANClientState *vc)
251
{
252
    TAPState *s = vc->opaque;
253

    
254
    qemu_purge_queued_packets(vc);
255

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

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

    
265
/* fd support */
266

    
267
static TAPState *net_tap_fd_init(VLANState *vlan,
268
                                 const char *model,
269
                                 const char *name,
270
                                 int fd,
271
                                 int vnet_hdr)
272
{
273
    TAPState *s;
274

    
275
    s = qemu_mallocz(sizeof(TAPState));
276
    s->fd = fd;
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);
285
    tap_read_poll(s, 1);
286
    return s;
287
}
288

    
289
static int launch_script(const char *setup_script, const char *ifname, int fd)
290
{
291
    sigset_t oldmask, mask;
292
    int pid, status;
293
    char *args[3];
294
    char **parg;
295

    
296
    sigemptyset(&mask);
297
    sigaddset(&mask, SIGCHLD);
298
    sigprocmask(SIG_BLOCK, &mask, &oldmask);
299

    
300
    /* try to launch network script */
301
    pid = fork();
302
    if (pid == 0) {
303
        int open_max = sysconf(_SC_OPEN_MAX), i;
304

    
305
        for (i = 0; i < open_max; i++) {
306
            if (i != STDIN_FILENO &&
307
                i != STDOUT_FILENO &&
308
                i != STDERR_FILENO &&
309
                i != fd) {
310
                close(i);
311
            }
312
        }
313
        parg = args;
314
        *parg++ = (char *)setup_script;
315
        *parg++ = (char *)ifname;
316
        *parg++ = NULL;
317
        execv(setup_script, args);
318
        _exit(1);
319
    } else if (pid > 0) {
320
        while (waitpid(pid, &status, 0) != pid) {
321
            /* loop */
322
        }
323
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
324

    
325
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
326
            return 0;
327
        }
328
    }
329
    fprintf(stderr, "%s: could not launch network script\n", setup_script);
330
    return -1;
331
}
332

    
333
static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
334
{
335
    int fd, vnet_hdr_required;
336
    char ifname[128] = {0,};
337
    const char *setup_script;
338

    
339
    if (qemu_opt_get(opts, "ifname")) {
340
        pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
341
    }
342

    
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;
346
    } else {
347
        vnet_hdr_required = 0;
348
    }
349

    
350
    TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
351
    if (fd < 0) {
352
        return -1;
353
    }
354

    
355
    setup_script = qemu_opt_get(opts, "script");
356
    if (setup_script &&
357
        setup_script[0] != '\0' &&
358
        strcmp(setup_script, "no") != 0 &&
359
        launch_script(setup_script, ifname, fd)) {
360
        close(fd);
361
        return -1;
362
    }
363

    
364
    qemu_opt_set(opts, "ifname", ifname);
365

    
366
    return fd;
367
}
368

    
369
int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
370
{
371
    TAPState *s;
372
    int fd, vnet_hdr;
373

    
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");
380
            return -1;
381
        }
382

    
383
        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
384
        if (fd == -1) {
385
            return -1;
386
        }
387

    
388
        fcntl(fd, F_SETFL, O_NONBLOCK);
389

    
390
        vnet_hdr = tap_probe_vnet_hdr(fd);
391
    } else {
392
        if (!qemu_opt_get(opts, "script")) {
393
            qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
394
        }
395

    
396
        if (!qemu_opt_get(opts, "downscript")) {
397
            qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
398
        }
399

    
400
        fd = net_tap_init(opts, &vnet_hdr);
401
    }
402

    
403
    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
404
    if (!s) {
405
        close(fd);
406
        return -1;
407
    }
408

    
409
    if (tap_set_sndbuf(s->fd, opts) < 0) {
410
        return -1;
411
    }
412

    
413
    if (qemu_opt_get(opts, "fd")) {
414
        snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
415
    } else {
416
        const char *ifname, *script, *downscript;
417

    
418
        ifname     = qemu_opt_get(opts, "ifname");
419
        script     = qemu_opt_get(opts, "script");
420
        downscript = qemu_opt_get(opts, "downscript");
421

    
422
        snprintf(s->vc->info_str, sizeof(s->vc->info_str),
423
                 "ifname=%s,script=%s,downscript=%s",
424
                 ifname, script, downscript);
425

    
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);
429
        }
430
    }
431

    
432
    if (vlan) {
433
        vlan->nb_host_devs++;
434
    }
435

    
436
    return 0;
437
}