xref: /netbsd-src/sys/rump/net/lib/libvirtif/if_virt.c (revision 96230fab84e26a6435963032070e916a951a8b2e)
1 /*	$NetBSD: if_virt.c,v 1.4 2008/10/16 09:25:30 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 /* XXXXX: this prototype needs to go elsewhere */
73 int rump_virtif_create(struct ifaliasreq *, struct ifnet **);
74 int
75 rump_virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
76 {
77 
78 	return virtif_create(ia, ifpp);
79 }
80 
81 /*
82  * Create a socket and call ifioctl() to configure the interface.
83  * This trickles down to virtif_ioctl().
84  */
85 static int
86 configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
87 {
88 	struct socket *so;
89 	int error;
90 
91 	strcpy(ia->ifra_name, ifp->if_xname);
92 	error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
93 	    0, curlwp, NULL);
94 	if (error)
95 		return error;
96 	error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
97 	soclose(so);
98 
99 	return error;
100 }
101 
102 static int
103 virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
104 {
105 	struct virtif_sc *sc;
106 	struct ifreq ifr;
107 	struct ifnet *ifp;
108 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
109 	int fd, rv, error;
110 	int mynum;
111 
112 	/*
113 	 * XXX: this is currently un-sane.  Need to figure out a way
114 	 * to configure this with a bridge into a sane network without
115 	 * hardcoding it.
116 	 */
117 	fd = rumpuser_open("/dev/tap0", O_RDWR, &error);
118 	if (fd == -1) {
119 		printf("virtif_create: can't open /dev/tap %d\n", error);
120 		return error;
121 	}
122 
123 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
124 	rv = rumpuser_ioctl(fd, TAPGIFNAME, &ifr, &error);
125 	if (rv == -1) {
126 		kmem_free(sc, sizeof(*sc));
127 		return error;
128 	}
129 	strlcpy(sc->sc_tapname, ifr.ifr_name, IFNAMSIZ);
130 	sc->sc_tapfd = fd;
131 
132 	ifp = &sc->sc_if;
133 	mynum = nunits++;
134 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, mynum);
135 	ifp->if_softc = sc;
136 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
137 	ifp->if_init = virtif_init;
138 	ifp->if_ioctl = virtif_ioctl;
139 	ifp->if_start = virtif_start;
140 	ifp->if_stop = virtif_stop;
141 
142 	if (rump_threads) {
143 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
144 		    NULL, "virtifi");
145 		if (rv) {
146 			kmem_free(sc, sizeof(*sc));
147 			return rv;
148 		}
149 	} else {
150 		printf("WARNING: threads not enabled, receive NOT working\n");
151 	}
152 
153 	if_attach(ifp);
154 	ether_ifattach(ifp, enaddr);
155 
156 	if (ia)
157 		configaddr(ifp, ia);
158 	if (ifpp)
159 		*ifpp = ifp;
160 
161 	return 0;
162 }
163 
164 static int
165 virtif_init(struct ifnet *ifp)
166 {
167 
168 	ifp->if_flags |= IFF_RUNNING;
169 
170 	return 0;
171 }
172 
173 static int
174 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
175 {
176 	int s, rv;
177 
178 	s = splnet();
179 	rv = ether_ioctl(ifp, cmd, data);
180 	splx(s);
181 
182 	return rv;
183 }
184 
185 /* just send everything in-context */
186 static void
187 virtif_start(struct ifnet *ifp)
188 {
189 	struct virtif_sc *sc = ifp->if_softc;
190 	struct iovec io[16];
191 	struct mbuf *m, *m0;
192 	int s, i, error;
193 
194 	s = splnet();
195 	for (;;) {
196 		IF_DEQUEUE(&ifp->if_snd, m0);
197 		if (!m0)
198 			break;
199 		splx(s);
200 
201 		m = m0;
202 		for (i = 0; i < 16 && m; i++) {
203 			io[i].iov_base = mtod(m, void *);
204 			io[i].iov_len = m->m_len;
205 			m = m->m_next;
206 		}
207 		if (i == 16)
208 			panic("lazy bum");
209 		rumpuser_writev(sc->sc_tapfd, io, i, &error);
210 		m_freem(m0);
211 		s = splnet();
212 	}
213 
214 	ifp->if_flags &= ~IFF_OACTIVE;
215 	splx(s);
216 }
217 
218 static void
219 virtif_stop(struct ifnet *ifp, int disable)
220 {
221 
222 	panic("%s: unimpl", __func__);
223 }
224 
225 static void
226 virtif_worker(void *arg)
227 {
228 	struct ifnet *ifp = arg;
229 	struct virtif_sc *sc = ifp->if_softc;
230 	struct mbuf *m;
231 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
232 	ssize_t n;
233 	int error;
234 
235 	for (;;) {
236 		m = m_gethdr(M_WAIT, MT_DATA);
237 		MEXTMALLOC(m, plen, M_WAIT);
238 
239 		n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
240 		KASSERT(n < ETHER_MAX_LEN_JUMBO);
241 		if (n <= 0) {
242 			m_freem(m);
243 			break;
244 		}
245 		m->m_len = m->m_pkthdr.len = n;
246 		m->m_pkthdr.rcvif = ifp;
247 		ether_input(ifp, m);
248 	}
249 
250 	panic("virtif_workin is a lazy boy %d\n", error);
251 }
252