Statistics
| Branch: | Revision:

root / net / tap-solaris.c @ 42673936

History | View | Annotate | Download (6.1 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 966ea5ec Mark McLoughlin
42 966ea5ec Mark McLoughlin
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
43 966ea5ec Mark McLoughlin
{
44 966ea5ec Mark McLoughlin
    struct strbuf sbuf;
45 966ea5ec Mark McLoughlin
    int f = 0;
46 966ea5ec Mark McLoughlin
47 966ea5ec Mark McLoughlin
    sbuf.maxlen = maxlen;
48 966ea5ec Mark McLoughlin
    sbuf.buf = (char *)buf;
49 966ea5ec Mark McLoughlin
50 966ea5ec Mark McLoughlin
    return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
51 966ea5ec Mark McLoughlin
}
52 966ea5ec Mark McLoughlin
53 966ea5ec Mark McLoughlin
#define TUNNEWPPA       (('T'<<16) | 0x0001)
54 966ea5ec Mark McLoughlin
/*
55 966ea5ec Mark McLoughlin
 * Allocate TAP device, returns opened fd.
56 966ea5ec Mark McLoughlin
 * Stores dev name in the first arg(must be large enough).
57 966ea5ec Mark McLoughlin
 */
58 966ea5ec Mark McLoughlin
static int tap_alloc(char *dev, size_t dev_size)
59 966ea5ec Mark McLoughlin
{
60 966ea5ec Mark McLoughlin
    int tap_fd, if_fd, ppa = -1;
61 966ea5ec Mark McLoughlin
    static int ip_fd = 0;
62 966ea5ec Mark McLoughlin
    char *ptr;
63 966ea5ec Mark McLoughlin
64 966ea5ec Mark McLoughlin
    static int arp_fd = 0;
65 966ea5ec Mark McLoughlin
    int ip_muxid, arp_muxid;
66 966ea5ec Mark McLoughlin
    struct strioctl  strioc_if, strioc_ppa;
67 966ea5ec Mark McLoughlin
    int link_type = I_PLINK;;
68 966ea5ec Mark McLoughlin
    struct lifreq ifr;
69 966ea5ec Mark McLoughlin
    char actual_name[32] = "";
70 966ea5ec Mark McLoughlin
71 966ea5ec Mark McLoughlin
    memset(&ifr, 0x0, sizeof(ifr));
72 966ea5ec Mark McLoughlin
73 966ea5ec Mark McLoughlin
    if( *dev ){
74 966ea5ec Mark McLoughlin
       ptr = dev;
75 966ea5ec Mark McLoughlin
       while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
76 966ea5ec Mark McLoughlin
       ppa = atoi(ptr);
77 966ea5ec Mark McLoughlin
    }
78 966ea5ec Mark McLoughlin
79 966ea5ec Mark McLoughlin
    /* Check if IP device was opened */
80 966ea5ec Mark McLoughlin
    if( ip_fd )
81 966ea5ec Mark McLoughlin
       close(ip_fd);
82 966ea5ec Mark McLoughlin
83 966ea5ec Mark McLoughlin
    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
84 966ea5ec Mark McLoughlin
    if (ip_fd < 0) {
85 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
86 966ea5ec Mark McLoughlin
       return -1;
87 966ea5ec Mark McLoughlin
    }
88 966ea5ec Mark McLoughlin
89 966ea5ec Mark McLoughlin
    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
90 966ea5ec Mark McLoughlin
    if (tap_fd < 0) {
91 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/tap");
92 966ea5ec Mark McLoughlin
       return -1;
93 966ea5ec Mark McLoughlin
    }
94 966ea5ec Mark McLoughlin
95 966ea5ec Mark McLoughlin
    /* Assign a new PPA and get its unit number. */
96 966ea5ec Mark McLoughlin
    strioc_ppa.ic_cmd = TUNNEWPPA;
97 966ea5ec Mark McLoughlin
    strioc_ppa.ic_timout = 0;
98 966ea5ec Mark McLoughlin
    strioc_ppa.ic_len = sizeof(ppa);
99 966ea5ec Mark McLoughlin
    strioc_ppa.ic_dp = (char *)&ppa;
100 966ea5ec Mark McLoughlin
    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
101 966ea5ec Mark McLoughlin
       syslog (LOG_ERR, "Can't assign new interface");
102 966ea5ec Mark McLoughlin
103 966ea5ec Mark McLoughlin
    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
104 966ea5ec Mark McLoughlin
    if (if_fd < 0) {
105 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't open /dev/tap (2)");
106 966ea5ec Mark McLoughlin
       return -1;
107 966ea5ec Mark McLoughlin
    }
108 966ea5ec Mark McLoughlin
    if(ioctl(if_fd, I_PUSH, "ip") < 0){
109 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't push IP module");
110 966ea5ec Mark McLoughlin
       return -1;
111 966ea5ec Mark McLoughlin
    }
112 966ea5ec Mark McLoughlin
113 966ea5ec Mark McLoughlin
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
114 966ea5ec Mark McLoughlin
        syslog(LOG_ERR, "Can't get flags\n");
115 966ea5ec Mark McLoughlin
116 966ea5ec Mark McLoughlin
    snprintf (actual_name, 32, "tap%d", ppa);
117 966ea5ec Mark McLoughlin
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
118 966ea5ec Mark McLoughlin
119 966ea5ec Mark McLoughlin
    ifr.lifr_ppa = ppa;
120 966ea5ec Mark McLoughlin
    /* Assign ppa according to the unit number returned by tun device */
121 966ea5ec Mark McLoughlin
122 966ea5ec Mark McLoughlin
    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
123 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't set PPA %d", ppa);
124 966ea5ec Mark McLoughlin
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
125 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't get flags\n");
126 966ea5ec Mark McLoughlin
    /* Push arp module to if_fd */
127 966ea5ec Mark McLoughlin
    if (ioctl (if_fd, I_PUSH, "arp") < 0)
128 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't push ARP module (2)");
129 966ea5ec Mark McLoughlin
130 966ea5ec Mark McLoughlin
    /* Push arp module to ip_fd */
131 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, I_POP, NULL) < 0)
132 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "I_POP failed\n");
133 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
134 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't push ARP module (3)\n");
135 966ea5ec Mark McLoughlin
    /* Open arp_fd */
136 966ea5ec Mark McLoughlin
    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
137 966ea5ec Mark McLoughlin
    if (arp_fd < 0)
138 966ea5ec Mark McLoughlin
       syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
139 966ea5ec Mark McLoughlin
140 966ea5ec Mark McLoughlin
    /* Set ifname to arp */
141 966ea5ec Mark McLoughlin
    strioc_if.ic_cmd = SIOCSLIFNAME;
142 966ea5ec Mark McLoughlin
    strioc_if.ic_timout = 0;
143 966ea5ec Mark McLoughlin
    strioc_if.ic_len = sizeof(ifr);
144 966ea5ec Mark McLoughlin
    strioc_if.ic_dp = (char *)&ifr;
145 966ea5ec Mark McLoughlin
    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
146 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't set ifname to arp\n");
147 966ea5ec Mark McLoughlin
    }
148 966ea5ec Mark McLoughlin
149 966ea5ec Mark McLoughlin
    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
150 966ea5ec Mark McLoughlin
       syslog(LOG_ERR, "Can't link TAP device to IP");
151 966ea5ec Mark McLoughlin
       return -1;
152 966ea5ec Mark McLoughlin
    }
153 966ea5ec Mark McLoughlin
154 966ea5ec Mark McLoughlin
    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
155 966ea5ec Mark McLoughlin
        syslog (LOG_ERR, "Can't link TAP device to ARP");
156 966ea5ec Mark McLoughlin
157 966ea5ec Mark McLoughlin
    close (if_fd);
158 966ea5ec Mark McLoughlin
159 966ea5ec Mark McLoughlin
    memset(&ifr, 0x0, sizeof(ifr));
160 966ea5ec Mark McLoughlin
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
161 966ea5ec Mark McLoughlin
    ifr.lifr_ip_muxid  = ip_muxid;
162 966ea5ec Mark McLoughlin
    ifr.lifr_arp_muxid = arp_muxid;
163 966ea5ec Mark McLoughlin
164 966ea5ec Mark McLoughlin
    if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
165 966ea5ec Mark McLoughlin
    {
166 966ea5ec Mark McLoughlin
      ioctl (ip_fd, I_PUNLINK , arp_muxid);
167 966ea5ec Mark McLoughlin
      ioctl (ip_fd, I_PUNLINK, ip_muxid);
168 966ea5ec Mark McLoughlin
      syslog (LOG_ERR, "Can't set multiplexor id");
169 966ea5ec Mark McLoughlin
    }
170 966ea5ec Mark McLoughlin
171 966ea5ec Mark McLoughlin
    snprintf(dev, dev_size, "tap%d", ppa);
172 966ea5ec Mark McLoughlin
    return tap_fd;
173 966ea5ec Mark McLoughlin
}
174 966ea5ec Mark McLoughlin
175 966ea5ec Mark McLoughlin
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
176 966ea5ec Mark McLoughlin
{
177 966ea5ec Mark McLoughlin
    char  dev[10]="";
178 966ea5ec Mark McLoughlin
    int fd;
179 966ea5ec Mark McLoughlin
    if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
180 966ea5ec Mark McLoughlin
       fprintf(stderr, "Cannot allocate TAP device\n");
181 966ea5ec Mark McLoughlin
       return -1;
182 966ea5ec Mark McLoughlin
    }
183 966ea5ec Mark McLoughlin
    pstrcpy(ifname, ifname_size, dev);
184 f5c5e381 Mark McLoughlin
    if (*vnet_hdr) {
185 f5c5e381 Mark McLoughlin
        /* Solaris doesn't have IFF_VNET_HDR */
186 f5c5e381 Mark McLoughlin
        *vnet_hdr = 0;
187 f5c5e381 Mark McLoughlin
188 f5c5e381 Mark McLoughlin
        if (vnet_hdr_required && !*vnet_hdr) {
189 f5c5e381 Mark McLoughlin
            qemu_error("vnet_hdr=1 requested, but no kernel "
190 f5c5e381 Mark McLoughlin
                       "support for IFF_VNET_HDR available");
191 f5c5e381 Mark McLoughlin
            close(fd);
192 f5c5e381 Mark McLoughlin
            return -1;
193 f5c5e381 Mark McLoughlin
        }
194 f5c5e381 Mark McLoughlin
    }
195 966ea5ec Mark McLoughlin
    fcntl(fd, F_SETFL, O_NONBLOCK);
196 966ea5ec Mark McLoughlin
    return fd;
197 966ea5ec Mark McLoughlin
}
198 15ac913b Mark McLoughlin
199 15ac913b Mark McLoughlin
int tap_set_sndbuf(int fd, QemuOpts *opts)
200 15ac913b Mark McLoughlin
{
201 15ac913b Mark McLoughlin
    return 0;
202 15ac913b Mark McLoughlin
}
203 dc69004c Mark McLoughlin
204 dc69004c Mark McLoughlin
int tap_probe_vnet_hdr(int fd)
205 dc69004c Mark McLoughlin
{
206 dc69004c Mark McLoughlin
    return 0;
207 dc69004c Mark McLoughlin
}
208 1faac1f7 Mark McLoughlin
209 9c282718 Mark McLoughlin
int tap_probe_has_ufo(int fd)
210 9c282718 Mark McLoughlin
{
211 9c282718 Mark McLoughlin
    return 0;
212 9c282718 Mark McLoughlin
}
213 9c282718 Mark McLoughlin
214 1faac1f7 Mark McLoughlin
void tap_fd_set_offload(int fd, int csum, int tso4,
215 1faac1f7 Mark McLoughlin
                        int tso6, int ecn, int ufo)
216 1faac1f7 Mark McLoughlin
{
217 1faac1f7 Mark McLoughlin
}