1 /* $NetBSD: if_virt.c,v 1.13 2009/10/14 17:29:20 pooka Exp $ */
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
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
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>
35 #include <sys/kthread.h>
36 #include <sys/mutex.h>
38 #include <sys/sockio.h>
39 #include <sys/socketvar.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
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);
68 struct ethercom sc_ec
;
74 static void virtif_worker(void *);
75 static void virtif_sender(void *);
79 * Create a socket and call ifioctl() to configure the interface.
80 * This trickles down to virtif_ioctl().
83 configaddr(struct ifnet
*ifp
, struct ifaliasreq
*ia
)
88 strcpy(ia
->ifra_name
, ifp
->if_xname
);
89 error
= socreate(ia
->ifra_addr
.sa_family
, &so
, SOCK_DGRAM
,
93 error
= ifioctl(so
, SIOCAIFADDR
, ia
, curlwp
);
101 rump_virtif_create(int num
)
103 struct virtif_sc
*sc
;
105 uint8_t enaddr
[ETHER_ADDR_LEN
] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
109 snprintf(tapdev
, sizeof(tapdev
), "/dev/tap%d", num
);
110 fd
= rumpuser_open(tapdev
, O_RDWR
, &error
);
112 printf("virtif_create: can't open /dev/tap %d\n", error
);
115 KASSERT(num
< 0x100);
116 enaddr
[2] = arc4random() & 0xff;
119 sc
= kmem_zalloc(sizeof(*sc
), KM_SLEEP
);
122 ifp
= &sc
->sc_ec
.ec_if
;
123 sprintf(ifp
->if_xname
, "%s%d", VIRTIF_BASE
, num
);
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");
135 ether_ifattach(ifp
, enaddr
);
141 virtif_init(struct ifnet
*ifp
)
146 rv
= kthread_create(PRI_NONE
, 0, NULL
, virtif_worker
, ifp
,
148 /* XXX: should do proper cleanup */
150 panic("if_virt: can't create worker");
152 rv
= kthread_create(PRI_NONE
, 0, NULL
, virtif_sender
, ifp
,
155 panic("if_virt: can't create sender");
158 printf("WARNING: threads not enabled, receive NOT working\n");
160 ifp
->if_flags
|= IFF_RUNNING
;
166 virtif_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
171 rv
= ether_ioctl(ifp
, cmd
, data
);
179 /* just send everything in-context */
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
);
191 virtif_stop(struct ifnet
*ifp
, int disable
)
194 panic("%s: unimpl", __func__
);
198 virtif_worker(void *arg
)
200 struct ifnet
*ifp
= arg
;
201 struct virtif_sc
*sc
= ifp
->if_softc
;
203 size_t plen
= ETHER_MAX_LEN_JUMBO
+1;
208 m
= m_gethdr(M_WAIT
, MT_DATA
);
209 MEXTMALLOC(m
, plen
, M_WAIT
);
212 n
= rumpuser_read(sc
->sc_tapfd
, mtod(m
, void *), plen
, &error
);
213 KASSERT(n
< ETHER_MAX_LEN_JUMBO
);
216 * work around tap bug: /dev/tap is opened in
217 * non-blocking mode if it previously was
220 if (n
== -1 && error
== EAGAIN
) {
223 pfd
.fd
= sc
->sc_tapfd
;
226 rumpuser_poll(&pfd
, 1, INFTIM
, &error
);
232 m
->m_len
= m
->m_pkthdr
.len
= n
;
233 m
->m_pkthdr
.rcvif
= ifp
;
237 panic("virtif_workin is a lazy boy %d\n", error
);
240 /* lazy bum stetson-harrison magic value */
243 virtif_sender(void *arg
)
245 struct ifnet
*ifp
= arg
;
246 struct virtif_sc
*sc
= ifp
->if_softc
;
248 struct rumpuser_iovec io
[LB_SH
];
251 mutex_enter(&sc
->sc_sendmtx
);
253 IF_DEQUEUE(&ifp
->if_snd
, m0
);
255 cv_wait(&sc
->sc_sendcv
, &sc
->sc_sendmtx
);
258 mutex_exit(&sc
->sc_sendmtx
);
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
;
268 rumpuser_writev(sc
->sc_tapfd
, io
, i
, &error
);
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
*);
285 rump_dummyif_create()
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
);
297 strlcpy(ifp
->if_xname
, "dummy0", sizeof(ifp
->if_xname
));
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
;
305 ether_ifattach(ifp
, enaddr
);
309 dummyif_init(struct ifnet
*ifp
)
312 ifp
->if_flags
|= IFF_RUNNING
;
317 dummyif_start(struct ifnet
*ifp
)