Statistics
| Branch: | Revision:

root / net / tap-bsd.c @ 3ee66dfa

History | View | Annotate | Download (3.3 kB)

1 e7e92325 Mark McLoughlin
/*
2 e7e92325 Mark McLoughlin
 * QEMU System Emulator
3 e7e92325 Mark McLoughlin
 *
4 e7e92325 Mark McLoughlin
 * Copyright (c) 2003-2008 Fabrice Bellard
5 e7e92325 Mark McLoughlin
 *
6 e7e92325 Mark McLoughlin
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 e7e92325 Mark McLoughlin
 * of this software and associated documentation files (the "Software"), to deal
8 e7e92325 Mark McLoughlin
 * in the Software without restriction, including without limitation the rights
9 e7e92325 Mark McLoughlin
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 e7e92325 Mark McLoughlin
 * copies of the Software, and to permit persons to whom the Software is
11 e7e92325 Mark McLoughlin
 * furnished to do so, subject to the following conditions:
12 e7e92325 Mark McLoughlin
 *
13 e7e92325 Mark McLoughlin
 * The above copyright notice and this permission notice shall be included in
14 e7e92325 Mark McLoughlin
 * all copies or substantial portions of the Software.
15 e7e92325 Mark McLoughlin
 *
16 e7e92325 Mark McLoughlin
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 e7e92325 Mark McLoughlin
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 e7e92325 Mark McLoughlin
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 e7e92325 Mark McLoughlin
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 e7e92325 Mark McLoughlin
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 e7e92325 Mark McLoughlin
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 e7e92325 Mark McLoughlin
 * THE SOFTWARE.
23 e7e92325 Mark McLoughlin
 */
24 e7e92325 Mark McLoughlin
25 e7e92325 Mark McLoughlin
#include "net/tap.h"
26 71f4effc Alexander Graf
#include "qemu-common.h"
27 71f4effc Alexander Graf
#include "sysemu.h"
28 73d96e29 Blue Swirl
#include "qemu-error.h"
29 e7e92325 Mark McLoughlin
30 e7e92325 Mark McLoughlin
#ifdef __NetBSD__
31 e7e92325 Mark McLoughlin
#include <net/if_tap.h>
32 e7e92325 Mark McLoughlin
#endif
33 e7e92325 Mark McLoughlin
34 a167ba50 Aurelien Jarno
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
35 e7e92325 Mark McLoughlin
#include <libutil.h>
36 e7e92325 Mark McLoughlin
#else
37 e7e92325 Mark McLoughlin
#include <util.h>
38 e7e92325 Mark McLoughlin
#endif
39 e7e92325 Mark McLoughlin
40 e7e92325 Mark McLoughlin
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
41 e7e92325 Mark McLoughlin
{
42 e7e92325 Mark McLoughlin
    int fd;
43 e7e92325 Mark McLoughlin
    char *dev;
44 e7e92325 Mark McLoughlin
    struct stat s;
45 e7e92325 Mark McLoughlin
46 a167ba50 Aurelien Jarno
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47 2f859a3c Juergen Lock
    /* if no ifname is given, always start the search from tap0. */
48 2f859a3c Juergen Lock
    int i;
49 2f859a3c Juergen Lock
    char dname[100];
50 2f859a3c Juergen Lock
51 2f859a3c Juergen Lock
    for (i = 0; i < 10; i++) {
52 2f859a3c Juergen Lock
        if (*ifname) {
53 2f859a3c Juergen Lock
            snprintf(dname, sizeof dname, "/dev/%s", ifname);
54 2f859a3c Juergen Lock
        } else {
55 2f859a3c Juergen Lock
            snprintf(dname, sizeof dname, "/dev/tap%d", i);
56 2f859a3c Juergen Lock
        }
57 2f859a3c Juergen Lock
        TFR(fd = open(dname, O_RDWR));
58 2f859a3c Juergen Lock
        if (fd >= 0) {
59 2f859a3c Juergen Lock
            break;
60 2f859a3c Juergen Lock
        }
61 2f859a3c Juergen Lock
        else if (errno == ENXIO || errno == ENOENT) {
62 2f859a3c Juergen Lock
            break;
63 2f859a3c Juergen Lock
        }
64 2f859a3c Juergen Lock
        if (*ifname) {
65 2f859a3c Juergen Lock
            break;
66 2f859a3c Juergen Lock
        }
67 2f859a3c Juergen Lock
    }
68 2f859a3c Juergen Lock
    if (fd < 0) {
69 1ecda02b Markus Armbruster
        error_report("warning: could not open %s (%s): no virtual network emulation",
70 1ecda02b Markus Armbruster
                   dname, strerror(errno));
71 2f859a3c Juergen Lock
        return -1;
72 2f859a3c Juergen Lock
    }
73 2f859a3c Juergen Lock
#else
74 e7e92325 Mark McLoughlin
    TFR(fd = open("/dev/tap", O_RDWR));
75 e7e92325 Mark McLoughlin
    if (fd < 0) {
76 e7e92325 Mark McLoughlin
        fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
77 e7e92325 Mark McLoughlin
        return -1;
78 e7e92325 Mark McLoughlin
    }
79 2f859a3c Juergen Lock
#endif
80 e7e92325 Mark McLoughlin
81 e7e92325 Mark McLoughlin
    fstat(fd, &s);
82 e7e92325 Mark McLoughlin
    dev = devname(s.st_rdev, S_IFCHR);
83 e7e92325 Mark McLoughlin
    pstrcpy(ifname, ifname_size, dev);
84 e7e92325 Mark McLoughlin
85 39ca4c08 Juergen Lock
    if (*vnet_hdr) {
86 39ca4c08 Juergen Lock
        /* BSD doesn't have IFF_VNET_HDR */
87 39ca4c08 Juergen Lock
        *vnet_hdr = 0;
88 39ca4c08 Juergen Lock
89 39ca4c08 Juergen Lock
        if (vnet_hdr_required && !*vnet_hdr) {
90 1ecda02b Markus Armbruster
            error_report("vnet_hdr=1 requested, but no kernel "
91 1ecda02b Markus Armbruster
                         "support for IFF_VNET_HDR available");
92 39ca4c08 Juergen Lock
            close(fd);
93 39ca4c08 Juergen Lock
            return -1;
94 39ca4c08 Juergen Lock
        }
95 39ca4c08 Juergen Lock
    }
96 e7e92325 Mark McLoughlin
    fcntl(fd, F_SETFL, O_NONBLOCK);
97 e7e92325 Mark McLoughlin
    return fd;
98 e7e92325 Mark McLoughlin
}
99 15ac913b Mark McLoughlin
100 15ac913b Mark McLoughlin
int tap_set_sndbuf(int fd, QemuOpts *opts)
101 15ac913b Mark McLoughlin
{
102 15ac913b Mark McLoughlin
    return 0;
103 15ac913b Mark McLoughlin
}
104 dc69004c Mark McLoughlin
105 dc69004c Mark McLoughlin
int tap_probe_vnet_hdr(int fd)
106 dc69004c Mark McLoughlin
{
107 dc69004c Mark McLoughlin
    return 0;
108 dc69004c Mark McLoughlin
}
109 1faac1f7 Mark McLoughlin
110 9c282718 Mark McLoughlin
int tap_probe_has_ufo(int fd)
111 9c282718 Mark McLoughlin
{
112 9c282718 Mark McLoughlin
    return 0;
113 9c282718 Mark McLoughlin
}
114 9c282718 Mark McLoughlin
115 445d892f Michael S. Tsirkin
int tap_probe_vnet_hdr_len(int fd, int len)
116 445d892f Michael S. Tsirkin
{
117 445d892f Michael S. Tsirkin
    return 0;
118 445d892f Michael S. Tsirkin
}
119 445d892f Michael S. Tsirkin
120 445d892f Michael S. Tsirkin
void tap_fd_set_vnet_hdr_len(int fd, int len)
121 445d892f Michael S. Tsirkin
{
122 445d892f Michael S. Tsirkin
}
123 445d892f Michael S. Tsirkin
124 1faac1f7 Mark McLoughlin
void tap_fd_set_offload(int fd, int csum, int tso4,
125 1faac1f7 Mark McLoughlin
                        int tso6, int ecn, int ufo)
126 1faac1f7 Mark McLoughlin
{
127 1faac1f7 Mark McLoughlin
}