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