Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / rump / net / lib / libvirtif / if_virt.c
blob3e7eea42e9e945a60d1aefe0c333280da9ba3885
1 /* $NetBSD: if_virt.c,v 1.13 2009/10/14 17:29:20 pooka Exp $ */
3 /*
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
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.
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.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.13 2009/10/14 17:29:20 pooka Exp $");
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/fcntl.h>
34 #include <sys/kmem.h>
35 #include <sys/kthread.h>
36 #include <sys/mutex.h>
37 #include <sys/poll.h>
38 #include <sys/sockio.h>
39 #include <sys/socketvar.h>
41 #include <net/if.h>
42 #include <net/if_ether.h>
43 #include <net/if_tap.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
48 #include <rump/rump.h>
49 #include <rump/rumpuser.h>
51 #include "rump_private.h"
52 #include "rump_net_private.h"
55 * Virtual interface for userspace purposes. Uses tap(4) to
56 * interface with the kernel and just simply shovels data
57 * to/from /dev/tap.
60 #define VIRTIF_BASE "virt"
62 static int virtif_init(struct ifnet *);
63 static int virtif_ioctl(struct ifnet *, u_long, void *);
64 static void virtif_start(struct ifnet *);
65 static void virtif_stop(struct ifnet *, int);
67 struct virtif_sc {
68 struct ethercom sc_ec;
69 int sc_tapfd;
70 kmutex_t sc_sendmtx;
71 kcondvar_t sc_sendcv;
74 static void virtif_worker(void *);
75 static void virtif_sender(void *);
77 #if 0
79 * Create a socket and call ifioctl() to configure the interface.
80 * This trickles down to virtif_ioctl().
82 static int
83 configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
85 struct socket *so;
86 int error;
88 strcpy(ia->ifra_name, ifp->if_xname);
89 error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
90 0, curlwp, NULL);
91 if (error)
92 return error;
93 error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
94 soclose(so);
96 return error;
98 #endif
101 rump_virtif_create(int num)
103 struct virtif_sc *sc;
104 struct ifnet *ifp;
105 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
106 char tapdev[16];
107 int fd, error;
109 snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num);
110 fd = rumpuser_open(tapdev, O_RDWR, &error);
111 if (fd == -1) {
112 printf("virtif_create: can't open /dev/tap %d\n", error);
113 return error;
115 KASSERT(num < 0x100);
116 enaddr[2] = arc4random() & 0xff;
117 enaddr[5] = num;
119 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
120 sc->sc_tapfd = fd;
122 ifp = &sc->sc_ec.ec_if;
123 sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
124 ifp->if_softc = sc;
125 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
126 ifp->if_init = virtif_init;
127 ifp->if_ioctl = virtif_ioctl;
128 ifp->if_start = virtif_start;
129 ifp->if_stop = virtif_stop;
131 mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE);
132 cv_init(&sc->sc_sendcv, "virtsnd");
134 if_attach(ifp);
135 ether_ifattach(ifp, enaddr);
137 return 0;
140 static int
141 virtif_init(struct ifnet *ifp)
143 int rv;
145 if (rump_threads) {
146 rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
147 NULL, "virtifi");
148 /* XXX: should do proper cleanup */
149 if (rv) {
150 panic("if_virt: can't create worker");
152 rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp,
153 NULL, "virtifs");
154 if (rv) {
155 panic("if_virt: can't create sender");
157 } else {
158 printf("WARNING: threads not enabled, receive NOT working\n");
160 ifp->if_flags |= IFF_RUNNING;
162 return 0;
165 static int
166 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
168 int s, rv;
170 s = splnet();
171 rv = ether_ioctl(ifp, cmd, data);
172 if (rv == ENETRESET)
173 rv = 0;
174 splx(s);
176 return rv;
179 /* just send everything in-context */
180 static void
181 virtif_start(struct ifnet *ifp)
183 struct virtif_sc *sc = ifp->if_softc;
185 mutex_enter(&sc->sc_sendmtx);
186 cv_signal(&sc->sc_sendcv);
187 mutex_exit(&sc->sc_sendmtx);
190 static void
191 virtif_stop(struct ifnet *ifp, int disable)
194 panic("%s: unimpl", __func__);
197 static void
198 virtif_worker(void *arg)
200 struct ifnet *ifp = arg;
201 struct virtif_sc *sc = ifp->if_softc;
202 struct mbuf *m;
203 size_t plen = ETHER_MAX_LEN_JUMBO+1;
204 ssize_t n;
205 int error;
207 for (;;) {
208 m = m_gethdr(M_WAIT, MT_DATA);
209 MEXTMALLOC(m, plen, M_WAIT);
211 again:
212 n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
213 KASSERT(n < ETHER_MAX_LEN_JUMBO);
214 if (n <= 0) {
216 * work around tap bug: /dev/tap is opened in
217 * non-blocking mode if it previously was
218 * non-blocking.
220 if (n == -1 && error == EAGAIN) {
221 struct pollfd pfd;
223 pfd.fd = sc->sc_tapfd;
224 pfd.events = POLLIN;
226 rumpuser_poll(&pfd, 1, INFTIM, &error);
227 goto again;
229 m_freem(m);
230 break;
232 m->m_len = m->m_pkthdr.len = n;
233 m->m_pkthdr.rcvif = ifp;
234 ether_input(ifp, m);
237 panic("virtif_workin is a lazy boy %d\n", error);
240 /* lazy bum stetson-harrison magic value */
241 #define LB_SH 32
242 static void
243 virtif_sender(void *arg)
245 struct ifnet *ifp = arg;
246 struct virtif_sc *sc = ifp->if_softc;
247 struct mbuf *m, *m0;
248 struct rumpuser_iovec io[LB_SH];
249 int i, error;
251 mutex_enter(&sc->sc_sendmtx);
252 for (;;) {
253 IF_DEQUEUE(&ifp->if_snd, m0);
254 if (!m0) {
255 cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx);
256 continue;
258 mutex_exit(&sc->sc_sendmtx);
260 m = m0;
261 for (i = 0; i < LB_SH && m; i++) {
262 io[i].iov_base = mtod(m, void *);
263 io[i].iov_len = m->m_len;
264 m = m->m_next;
266 if (i == LB_SH)
267 panic("lazy bum");
268 rumpuser_writev(sc->sc_tapfd, io, i, &error);
269 m_freem(m0);
270 mutex_enter(&sc->sc_sendmtx);
273 mutex_exit(softnet_lock);
277 * dummyif is a nada-interface.
278 * As it requires nothing external, it can be used for testing
279 * interface configuration.
281 static int dummyif_init(struct ifnet *);
282 static void dummyif_start(struct ifnet *);
284 void
285 rump_dummyif_create()
287 struct ifnet *ifp;
288 struct ethercom *ec;
289 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
291 enaddr[2] = arc4random() & 0xff;
292 enaddr[5] = arc4random() & 0xff;
294 ec = kmem_zalloc(sizeof(*ec), KM_SLEEP);
296 ifp = &ec->ec_if;
297 strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname));
298 ifp->if_softc = ifp;
299 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
300 ifp->if_init = dummyif_init;
301 ifp->if_ioctl = virtif_ioctl;
302 ifp->if_start = dummyif_start;
304 if_attach(ifp);
305 ether_ifattach(ifp, enaddr);
308 static int
309 dummyif_init(struct ifnet *ifp)
312 ifp->if_flags |= IFF_RUNNING;
313 return 0;
316 static void
317 dummyif_start(struct ifnet *ifp)