Statistics
| Branch: | Revision:

root / net / tap-solaris.c @ a9899996

History | View | Annotate | Download (6.2 kB)

1 966ea5ec Mark McLoughlin
/*
2 966ea5ec Mark McLoughlin
 * QEMU System Emulator
3 966ea5ec Mark McLoughlin
 *
4 966ea5ec Mark McLoughlin
 * Copyright (c) 2003-2008 Fabrice Bellard
5 966ea5ec Mark McLoughlin
 *
6 966ea5ec Mark McLoughlin
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 966ea5ec Mark McLoughlin
 * of this software and associated documentation files (the "Software"), to deal
8 966ea5ec Mark McLoughlin
 * in the Software without restriction, including without limitation the rights
9 966ea5ec Mark McLoughlin
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 966ea5ec Mark McLoughlin
 * copies of the Software, and to permit persons to whom the Software is
11 966ea5ec Mark McLoughlin
 * furnished to do so, subject to the following conditions:
12 966ea5ec Mark McLoughlin
 *
13 966ea5ec Mark McLoughlin
 * The above copyright notice and this permission notice shall be included in
14 966ea5ec Mark McLoughlin
 * all copies or substantial portions of the Software.
15 966ea5ec Mark McLoughlin
 *
16 966ea5ec Mark McLoughlin
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 966ea5ec Mark McLoughlin
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 966ea5ec Mark McLoughlin
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 966ea5ec Mark McLoughlin
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 966ea5ec Mark McLoughlin
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 966ea5ec Mark McLoughlin
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 966ea5ec Mark McLoughlin
 * THE SOFTWARE.
23 966ea5ec Mark McLoughlin
 */
24 966ea5ec Mark McLoughlin
25 966ea5ec Mark McLoughlin
#include "net/tap.h"
26 ee7139c3 Andreas Färber
#include "sysemu.h"
27 966ea5ec Mark McLoughlin
28 966ea5ec Mark McLoughlin
#include <sys/stat.h>
29 966ea5ec Mark McLoughlin
#include <sys/ethernet.h>
30 966ea5ec Mark McLoughlin
#include <sys/sockio.h>
31 966ea5ec Mark McLoughlin
#include <netinet/arp.h>
32 966ea5ec Mark McLoughlin
#include <netinet/in.h>
33 966ea5ec Mark McLoughlin
#include <netinet/in_systm.h>
34 966ea5ec Mark McLoughlin
#include <netinet/ip.h>
35 966ea5ec Mark McLoughlin
#include <netinet/ip_icmp.h> // must come after ip.h
36 966ea5ec Mark McLoughlin
#include <netinet/udp.h>
37 966ea5ec Mark McLoughlin
#include <netinet/tcp.h>
38 966ea5ec Mark McLoughlin
#include <net/if.h>
39 966ea5ec Mark McLoughlin
#include <syslog.h>
40 966ea5ec Mark McLoughlin
#include <stropts.h>
41 3690cec8 Blue Swirl
#include "qemu-error.h"
42 966ea5ec Mark McLoughlin
43 966ea5ec Mark McLoughlin
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
44 966ea5ec Mark McLoughlin
{
45 966ea5ec Mark McLoughlin
    struct strbuf sbuf;
46 966ea5ec Mark McLoughlin
    int f = 0;
47 966ea5ec Mark McLoughlin
48 966ea5ec Mark McLoughlin
    sbuf.maxlen = maxlen;
49 966ea5ec Mark McLoughlin
    sbuf.buf = (char *)buf;
50 966ea5ec Mark McLoughlin
51 966ea5ec Mark McLoughlin
    return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
52 966ea5ec Mark McLoughlin
}
53 966ea5ec Mark McLoughlin
54 966ea5ec Mark McLoughlin
#define TUNNEWPPA       (('T'<<16) | 0x0001)
55 966ea5ec Mark McLoughlin
/*
56 966ea5ec Mark McLoughlin
 * Allocate TAP device, returns opened fd.
57 966ea5ec Mark McLoughlin
 * Stores dev name in the first arg(must be large enough).
58 966ea5ec Mark McLoughlin
 */
59 966ea5ec Mark McLoughlin
static int tap_alloc(char *dev, size_t dev_size)
60 966ea5ec Mark McLoughlin
{
61 966ea5ec Mark McLoughlin
    int tap_fd, if_fd, ppa = -1;
62 966ea5ec Mark McLoughlin
    static int ip_fd = 0;
63 966ea5ec Mark McLoughlin
    char *ptr;
64 966ea5ec Mark McLoughlin
65 966ea5ec Mark McLoughlin
    static int arp_fd = 0;
66 966ea5ec Mark McLoughlin
    int ip_muxid, arp_muxid;
67 966ea5ec Mark McLoughlin
    struct strioctl  strioc_if, strioc_ppa;
68 966ea5ec Mark McLoughlin
    int link_type = I_PLINK;;
69 966ea5ec Mark McLoughlin
    struct lifreq ifr;
70 966ea5ec Mark McLoughlin
    char actual_name[32] = "";
71 966ea5ec Mark McLoughlin
72 966ea5ec Mark McLoughlin
    memset(&ifr, 0x0, sizeof(ifr));
73 966ea5ec Mark McLoughlin
74 966ea5ec Mark McLoughlin
    if( *dev ){
75 966ea5ec Mark McLoughlin
       ptr = dev;
76 966ea5ec Mark McLoughlin
       while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
77 966ea5ec Mark McLoughlin
       ppa = atoi(ptr);
78 966ea5ec Mark McLoughlin
    }
79 966ea5ec Mark McLoughlin
80 966ea5ec Mark McLoughlin
    /* Check if IP device was opened */
81 966ea5ec Mark McLoughlin
    if( ip_fd )
82 966ea5ec Mark McLoughlin
       close(ip_fd);
83 966ea5ec Mark McLoughlin
84 966ea5ec Mark McLoughlin
    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
85 966ea5ec Mark McLoughlin
    if (ip_fd < 0) {
86 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
87 966ea5ec Mark McLoughlin
       return -1;
88 966ea5ec Mark McLoughlin
    }
89 966ea5ec Mark McLoughlin
90 966ea5ec Mark McLoughlin
    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
91 966ea5ec Mark McLoughlin
    if (tap_fd < 0) {
92 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/tap");
93 966ea5ec Mark McLoughlin
       return -1;
94 966ea5ec Mark McLoughlin
    }
95 966ea5ec Mark McLoughlin
96 966ea5ec Mark McLoughlin
    /* Assign a new PPA and get its unit number. */
97 966ea5ec Mark McLoughlin
    strioc_ppa.ic_cmd = TUNNEWPPA;
98 966ea5ec Mark McLoughlin
    strioc_ppa.ic_timout = 0;
99 966ea5ec Mark McLoughlin
    strioc_ppa.ic_len = sizeof(ppa);
100 966ea5ec Mark McLoughlin
    strioc_ppa.ic_dp = (char *)&ppa;
101 966ea5ec Mark McLoughlin
    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
102 966ea5ec Mark McLoughlin
       syslog (LOG_ERR, "Can't assign new interface");
103 966ea5ec Mark McLoughlin
104 966ea5ec Mark McLoughlin
    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
105 966ea5ec Mark McLoughlin
    if (if_fd < 0) {
106 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/tap (2)");
107 966ea5ec Mark McLoughlin
       return -1;
108 966ea5ec Mark McLoughlin
    }
109 966ea5ec Mark McLoughlin
    if(ioctl(if_fd, I_PUSH, "ip") < 0){
110 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't push IP module");
111 966ea5ec Mark McLoughlin
       return -1;
112 966ea5ec Mark McLoughlin
    }
113 966ea5ec Mark McLoughlin
114 966ea5ec Mark McLoughlin
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
115 966ea5ec Mark McLoughlin
        syslog(LOG_ERR, "Can't get flags\n");
116 966ea5ec Mark McLoughlin
117 966ea5ec Mark McLoughlin
    snprintf (actual_name, 32, "tap%d", ppa);
118 966ea5ec Mark McLoughlin
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
119 966ea5ec Mark McLoughlin
120 966ea5ec Mark McLoughlin
    ifr.lifr_ppa = ppa;
121 966ea5ec Mark McLoughlin
    /* Assign ppa according to the unit number returned by tun device */
122 966ea5ec Mark McLoughlin
123 966ea5ec Mark McLoughlin
    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
124 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't set PPA %d", ppa);
125 966ea5ec Mark McLoughlin
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
126 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't get flags\n");
127 966ea5ec Mark McLoughlin
    /* Push arp module to if_fd */
128 966ea5ec Mark McLoughlin
    if (ioctl (if_fd, I_PUSH, "arp") < 0)
129 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't push ARP module (2)");
130 966ea5ec Mark McLoughlin
131 966ea5ec Mark McLoughlin
    /* Push arp module to ip_fd */
132 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, I_POP, NULL) < 0)
133 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "I_POP failed\n");
134 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
135 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't push ARP module (3)\n");
136 966ea5ec Mark McLoughlin
    /* Open arp_fd */
137 966ea5ec Mark McLoughlin
    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
138 966ea5ec Mark McLoughlin
    if (arp_fd < 0)
139 966ea5ec Mark McLoughlin
       syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
140 966ea5ec Mark McLoughlin
141 966ea5ec Mark McLoughlin
    /* Set ifname to arp */
142 966ea5ec Mark McLoughlin
    strioc_if.ic_cmd = SIOCSLIFNAME;
143 966ea5ec Mark McLoughlin
    strioc_if.ic_timout = 0;
144 966ea5ec Mark McLoughlin
    strioc_if.ic_len = sizeof(ifr);
145 966ea5ec Mark McLoughlin
    strioc_if.ic_dp = (char *)&ifr;
146 966ea5ec Mark McLoughlin
    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
147 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't set ifname to arp\n");
148 966ea5ec Mark McLoughlin
    }
149 966ea5ec Mark McLoughlin
150 966ea5ec Mark McLoughlin
    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
151 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't link TAP device to IP");
152 966ea5ec Mark McLoughlin
       return -1;
153 966ea5ec Mark McLoughlin
    }
154 966ea5ec Mark McLoughlin
155 966ea5ec Mark McLoughlin
    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
156 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't link TAP device to ARP");
157 966ea5ec Mark McLoughlin
158 966ea5ec Mark McLoughlin
    close (if_fd);
159 966ea5ec Mark McLoughlin
160 966ea5ec Mark McLoughlin
    memset(&ifr, 0x0, sizeof(ifr));
161 966ea5ec Mark McLoughlin
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
162 966ea5ec Mark McLoughlin
    ifr.lifr_ip_muxid  = ip_muxid;
163 966ea5ec Mark McLoughlin
    ifr.lifr_arp_muxid = arp_muxid;
164 966ea5ec Mark McLoughlin
165 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
166 966ea5ec Mark McLoughlin
    {
167 966ea5ec Mark McLoughlin
      ioctl (ip_fd, I_PUNLINK , arp_muxid);
168 966ea5ec Mark McLoughlin
      ioctl (ip_fd, I_PUNLINK, ip_muxid);
169 966ea5ec Mark McLoughlin
      syslog (LOG_ERR, "Can't set multiplexor id");
170 966ea5ec Mark McLoughlin
    }
171 966ea5ec Mark McLoughlin
172 966ea5ec Mark McLoughlin
    snprintf(dev, dev_size, "tap%d", ppa);
173 966ea5ec Mark McLoughlin
    return tap_fd;
174 966ea5ec Mark McLoughlin
}
175 966ea5ec Mark McLoughlin
176 966ea5ec Mark McLoughlin
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
177 966ea5ec Mark McLoughlin
{
178 966ea5ec Mark McLoughlin
    char  dev[10]="";
179 966ea5ec Mark McLoughlin
    int fd;
180 966ea5ec Mark McLoughlin
    if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
181 966ea5ec Mark McLoughlin
       fprintf(stderr, "Cannot allocate TAP device\n");
182 966ea5ec Mark McLoughlin
       return -1;
183 966ea5ec Mark McLoughlin
    }
184 966ea5ec Mark McLoughlin
    pstrcpy(ifname, ifname_size, dev);
185 f5c5e381 Mark McLoughlin
    if (*vnet_hdr) {
186 f5c5e381 Mark McLoughlin
        /* Solaris doesn't have IFF_VNET_HDR */
187 f5c5e381 Mark McLoughlin
        *vnet_hdr = 0;
188 f5c5e381 Mark McLoughlin
189 f5c5e381 Mark McLoughlin
        if (vnet_hdr_required && !*vnet_hdr) {
190 1ecda02b Markus Armbruster
            error_report("vnet_hdr=1 requested, but no kernel "
191 1ecda02b Markus Armbruster
                         "support for IFF_VNET_HDR available");
192 f5c5e381 Mark McLoughlin
            close(fd);
193 f5c5e381 Mark McLoughlin
            return -1;
194 f5c5e381 Mark McLoughlin
        }
195 f5c5e381 Mark McLoughlin
    }
196 966ea5ec Mark McLoughlin
    fcntl(fd, F_SETFL, O_NONBLOCK);
197 966ea5ec Mark McLoughlin
    return fd;
198 966ea5ec Mark McLoughlin
}
199 15ac913b Mark McLoughlin
200 15ac913b Mark McLoughlin
int tap_set_sndbuf(int fd, QemuOpts *opts)
201 15ac913b Mark McLoughlin
{
202 15ac913b Mark McLoughlin
    return 0;
203 15ac913b Mark McLoughlin
}
204 dc69004c Mark McLoughlin
205 dc69004c Mark McLoughlin
int tap_probe_vnet_hdr(int fd)
206 dc69004c Mark McLoughlin
{
207 dc69004c Mark McLoughlin
    return 0;
208 dc69004c Mark McLoughlin
}
209 1faac1f7 Mark McLoughlin
210 9c282718 Mark McLoughlin
int tap_probe_has_ufo(int fd)
211 9c282718 Mark McLoughlin
{
212 9c282718 Mark McLoughlin
    return 0;
213 9c282718 Mark McLoughlin
}
214 9c282718 Mark McLoughlin
215 445d892f Michael S. Tsirkin
int tap_probe_vnet_hdr_len(int fd, int len)
216 445d892f Michael S. Tsirkin
{
217 445d892f Michael S. Tsirkin
    return 0;
218 445d892f Michael S. Tsirkin
}
219 445d892f Michael S. Tsirkin
220 445d892f Michael S. Tsirkin
void tap_fd_set_vnet_hdr_len(int fd, int len)
221 445d892f Michael S. Tsirkin
{
222 445d892f Michael S. Tsirkin
}
223 445d892f Michael S. Tsirkin
224 1faac1f7 Mark McLoughlin
void tap_fd_set_offload(int fd, int csum, int tso4,
225 1faac1f7 Mark McLoughlin
                        int tso6, int ecn, int ufo)
226 1faac1f7 Mark McLoughlin
{
227 1faac1f7 Mark McLoughlin
}