if_virt.c revision 1.1 1 /* $NetBSD: if_virt.c,v 1.1 2008/10/06 00:30:32 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/condvar.h>
30 #include <sys/fcntl.h>
31 #include <sys/kmem.h>
32 #include <sys/kthread.h>
33 #include <sys/mutex.h>
34 #include <sys/sockio.h>
35 #include <sys/socketvar.h>
36
37 #include <net/if.h>
38 #include <net/if_ether.h>
39 #include <net/if_tap.h>
40
41 #include <netinet/in.h>
42 #include <netinet/in_var.h>
43
44 #include <rump/rump.h>
45 #include <rump/rumpuser.h>
46
47 #include "rump_private.h"
48
49 /*
50 * Virtual interface for userspace purposes. Uses tap(4) to
51 * interface with the kernel and just simply shovels data
52 * to/from /dev/tap.
53 */
54
55 #define VIRTIF_BASE "virt"
56 static int nunits;
57
58 static int virtif_init(struct ifnet *);
59 static int virtif_ioctl(struct ifnet *, u_long, void *);
60 static void virtif_start(struct ifnet *);
61 static void virtif_stop(struct ifnet *, int);
62
63 struct virtif_sc {
64 struct ifnet sc_if;
65 char sc_tapname[IFNAMSIZ];
66 int sc_tapfd;
67 };
68
69 static int virtif_create(struct ifaliasreq *, struct ifnet **);
70 static void virtif_worker(void *);
71
72 int
73 rump_virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
74 {
75
76 return virtif_create(ia, ifpp);
77 }
78
79 /*
80 * Create a socket and call ifioctl() to configure the interface.
81 * This trickles down to virtif_ioctl().
82 */
83 static int
84 configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
85 {
86 struct socket *so;
87 int error;
88
89 strcpy(ia->ifra_name, ifp->if_xname);
90 error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
91 0, curlwp, NULL);
92 if (error)
93 return error;
94 error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
95 soclose(so);
96
97 return error;
98 }
99
100 static int
101 virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
102 {
103 struct virtif_sc *sc;
104 struct ifreq ifr;
105 struct ifnet *ifp;
106 uint8_t enaddr[ETHER_ADDR_LEN] = { 0x0b, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
107 int fd, rv, error;
108 int mynum;
109
110 /*
111 * XXX: this is currently un-sane. Need to figure out a way
112 * to configure this with a bridge into a sane network without
113 * hardcoding it.
114 */
115 fd = rumpuser_open("/dev/tap0", O_RDWR, &error);
116 if (fd == -1) {
117 printf("virtif_create: can't open /dev/tap %d\n", error);
118 return error;
119 }
120
121 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
122 rv = rumpuser_ioctl(fd, TAPGIFNAME, &ifr, &error);
123 if (rv == -1) {
124 kmem_free(sc, sizeof(*sc));
125 return error;
126 }
127 strlcpy(sc->sc_tapname, ifr.ifr_name, IFNAMSIZ);
128 sc->sc_tapfd = fd;
129
130 ifp = &sc->sc_if;
131 mynum = nunits++;
132 sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, mynum);
133 ifp->if_softc = sc;
134 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
135 ifp->if_init = virtif_init;
136 ifp->if_ioctl = virtif_ioctl;
137 ifp->if_start = virtif_start;
138 ifp->if_stop = virtif_stop;
139
140 if (rump_threads) {
141 rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
142 NULL, "virtifi");
143 if (rv) {
144 kmem_free(sc, sizeof(*sc));
145 return rv;
146 }
147 } else {
148 printf("WARNING: threads not enabled, receive NOT working\n");
149 }
150
151 if_attach(ifp);
152 ether_ifattach(ifp, enaddr);
153
154 if (ia)
155 configaddr(ifp, ia);
156 if (ifpp)
157 *ifpp = ifp;
158
159 return 0;
160 }
161
162 static int
163 virtif_init(struct ifnet *ifp)
164 {
165
166 ifp->if_flags |= IFF_RUNNING;
167
168 return 0;
169 }
170
171 static int
172 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
173 {
174 int s, rv;
175
176 s = splnet();
177 rv = ether_ioctl(ifp, cmd, data);
178 splx(s);
179
180 return rv;
181 }
182
183 /* just send everything in-context */
184 static void
185 virtif_start(struct ifnet *ifp)
186 {
187 struct virtif_sc *sc = ifp->if_softc;
188 struct iovec io[16];
189 struct mbuf *m, *m0;
190 int s, i, error;
191
192 s = splnet();
193 for (;;) {
194 IF_DEQUEUE(&ifp->if_snd, m0);
195 if (!m0)
196 break;
197 splx(s);
198
199 m = m0;
200 for (i = 0; i < 16 && m; i++) {
201 io[i].iov_base = mtod(m, void *);
202 io[i].iov_len = m->m_len;
203 m = m->m_next;
204 }
205 if (i == 16)
206 panic("lazy bum");
207 rumpuser_writev(sc->sc_tapfd, io, i, &error);
208 m_freem(m0);
209 s = splnet();
210 }
211
212 ifp->if_flags &= ~IFF_OACTIVE;
213 splx(s);
214 }
215
216 static void
217 virtif_stop(struct ifnet *ifp, int disable)
218 {
219
220 panic("%s: unimpl", __func__);
221 }
222
223 static void
224 virtif_worker(void *arg)
225 {
226 struct ifnet *ifp = arg;
227 struct virtif_sc *sc = ifp->if_softc;
228 struct mbuf *m;
229 size_t plen = ETHER_MAX_LEN_JUMBO+1;
230 ssize_t n;
231 int error;
232
233 for (;;) {
234 m = m_gethdr(M_WAIT, MT_DATA);
235 MEXTMALLOC(m, plen, M_WAIT);
236
237 n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
238 KASSERT(n < ETHER_MAX_LEN_JUMBO);
239 if (n <= 0) {
240 m_freem(m);
241 break;
242 }
243 m->m_len = m->m_pkthdr.len = n;
244 m->m_pkthdr.rcvif = ifp;
245 ether_input(ifp, m);
246 }
247
248 panic("virtif_workin is a lazy boy %d\n", error);
249 }
250