Statistics
| Branch: | Revision:

root / net / tap-bsd.c @ d82287de

History | View | Annotate | Download (3.9 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 1422e32d Paolo Bonzini
#include "tap_int.h"
26 71f4effc Alexander Graf
#include "qemu-common.h"
27 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
28 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
29 e7e92325 Mark McLoughlin
30 e7e92325 Mark McLoughlin
#ifdef __NetBSD__
31 45c245bf Manuel Bouyer
#include <sys/ioctl.h>
32 45c245bf Manuel Bouyer
#include <net/if.h>
33 e7e92325 Mark McLoughlin
#include <net/if_tap.h>
34 e7e92325 Mark McLoughlin
#endif
35 e7e92325 Mark McLoughlin
36 e7e92325 Mark McLoughlin
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
37 e7e92325 Mark McLoughlin
{
38 e7e92325 Mark McLoughlin
    int fd;
39 45c245bf Manuel Bouyer
#ifdef TAPGIFNAME
40 45c245bf Manuel Bouyer
    struct ifreq ifr;
41 45c245bf Manuel Bouyer
#else
42 e7e92325 Mark McLoughlin
    char *dev;
43 e7e92325 Mark McLoughlin
    struct stat s;
44 45c245bf Manuel Bouyer
#endif
45 e7e92325 Mark McLoughlin
46 5f668643 Brad
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
47 5f668643 Brad
    /* if no ifname is given, always start the search from tap0/tun0. */
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 5f668643 Brad
#if defined(__OpenBSD__)
56 5f668643 Brad
            snprintf(dname, sizeof dname, "/dev/tun%d", i);
57 5f668643 Brad
#else
58 2f859a3c Juergen Lock
            snprintf(dname, sizeof dname, "/dev/tap%d", i);
59 5f668643 Brad
#endif
60 2f859a3c Juergen Lock
        }
61 2f859a3c Juergen Lock
        TFR(fd = open(dname, O_RDWR));
62 2f859a3c Juergen Lock
        if (fd >= 0) {
63 2f859a3c Juergen Lock
            break;
64 2f859a3c Juergen Lock
        }
65 2f859a3c Juergen Lock
        else if (errno == ENXIO || errno == ENOENT) {
66 2f859a3c Juergen Lock
            break;
67 2f859a3c Juergen Lock
        }
68 2f859a3c Juergen Lock
        if (*ifname) {
69 2f859a3c Juergen Lock
            break;
70 2f859a3c Juergen Lock
        }
71 2f859a3c Juergen Lock
    }
72 2f859a3c Juergen Lock
    if (fd < 0) {
73 1ecda02b Markus Armbruster
        error_report("warning: could not open %s (%s): no virtual network emulation",
74 1ecda02b Markus Armbruster
                   dname, strerror(errno));
75 2f859a3c Juergen Lock
        return -1;
76 2f859a3c Juergen Lock
    }
77 2f859a3c Juergen Lock
#else
78 e7e92325 Mark McLoughlin
    TFR(fd = open("/dev/tap", O_RDWR));
79 e7e92325 Mark McLoughlin
    if (fd < 0) {
80 45c245bf Manuel Bouyer
        fprintf(stderr,
81 45c245bf Manuel Bouyer
            "warning: could not open /dev/tap: no virtual network emulation: %s\n",
82 45c245bf Manuel Bouyer
            strerror(errno));
83 e7e92325 Mark McLoughlin
        return -1;
84 e7e92325 Mark McLoughlin
    }
85 2f859a3c Juergen Lock
#endif
86 e7e92325 Mark McLoughlin
87 45c245bf Manuel Bouyer
#ifdef TAPGIFNAME
88 45c245bf Manuel Bouyer
    if (ioctl(fd, TAPGIFNAME, (void *)&ifr) < 0) {
89 45c245bf Manuel Bouyer
        fprintf(stderr, "warning: could not get tap name: %s\n",
90 45c245bf Manuel Bouyer
            strerror(errno));
91 45c245bf Manuel Bouyer
        return -1;
92 45c245bf Manuel Bouyer
    }
93 45c245bf Manuel Bouyer
    pstrcpy(ifname, ifname_size, ifr.ifr_name);
94 45c245bf Manuel Bouyer
#else
95 45c245bf Manuel Bouyer
    if (fstat(fd, &s) < 0) {
96 45c245bf Manuel Bouyer
        fprintf(stderr,
97 45c245bf Manuel Bouyer
            "warning: could not stat /dev/tap: no virtual network emulation: %s\n",
98 45c245bf Manuel Bouyer
            strerror(errno));
99 45c245bf Manuel Bouyer
        return -1;
100 45c245bf Manuel Bouyer
    }
101 e7e92325 Mark McLoughlin
    dev = devname(s.st_rdev, S_IFCHR);
102 e7e92325 Mark McLoughlin
    pstrcpy(ifname, ifname_size, dev);
103 45c245bf Manuel Bouyer
#endif
104 e7e92325 Mark McLoughlin
105 39ca4c08 Juergen Lock
    if (*vnet_hdr) {
106 39ca4c08 Juergen Lock
        /* BSD doesn't have IFF_VNET_HDR */
107 39ca4c08 Juergen Lock
        *vnet_hdr = 0;
108 39ca4c08 Juergen Lock
109 39ca4c08 Juergen Lock
        if (vnet_hdr_required && !*vnet_hdr) {
110 1ecda02b Markus Armbruster
            error_report("vnet_hdr=1 requested, but no kernel "
111 1ecda02b Markus Armbruster
                         "support for IFF_VNET_HDR available");
112 39ca4c08 Juergen Lock
            close(fd);
113 39ca4c08 Juergen Lock
            return -1;
114 39ca4c08 Juergen Lock
        }
115 39ca4c08 Juergen Lock
    }
116 e7e92325 Mark McLoughlin
    fcntl(fd, F_SETFL, O_NONBLOCK);
117 e7e92325 Mark McLoughlin
    return fd;
118 e7e92325 Mark McLoughlin
}
119 15ac913b Mark McLoughlin
120 08c573a8 Laszlo Ersek
int tap_set_sndbuf(int fd, const NetdevTapOptions *tap)
121 15ac913b Mark McLoughlin
{
122 15ac913b Mark McLoughlin
    return 0;
123 15ac913b Mark McLoughlin
}
124 dc69004c Mark McLoughlin
125 dc69004c Mark McLoughlin
int tap_probe_vnet_hdr(int fd)
126 dc69004c Mark McLoughlin
{
127 dc69004c Mark McLoughlin
    return 0;
128 dc69004c Mark McLoughlin
}
129 1faac1f7 Mark McLoughlin
130 9c282718 Mark McLoughlin
int tap_probe_has_ufo(int fd)
131 9c282718 Mark McLoughlin
{
132 9c282718 Mark McLoughlin
    return 0;
133 9c282718 Mark McLoughlin
}
134 9c282718 Mark McLoughlin
135 445d892f Michael S. Tsirkin
int tap_probe_vnet_hdr_len(int fd, int len)
136 445d892f Michael S. Tsirkin
{
137 445d892f Michael S. Tsirkin
    return 0;
138 445d892f Michael S. Tsirkin
}
139 445d892f Michael S. Tsirkin
140 445d892f Michael S. Tsirkin
void tap_fd_set_vnet_hdr_len(int fd, int len)
141 445d892f Michael S. Tsirkin
{
142 445d892f Michael S. Tsirkin
}
143 445d892f Michael S. Tsirkin
144 1faac1f7 Mark McLoughlin
void tap_fd_set_offload(int fd, int csum, int tso4,
145 1faac1f7 Mark McLoughlin
                        int tso6, int ecn, int ufo)
146 1faac1f7 Mark McLoughlin
{
147 1faac1f7 Mark McLoughlin
}