2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/dev/virtual/net/if_vke.c,v 1.7 2007/07/02 05:38:28 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/serialize.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
47 #include <machine/md_var.h>
49 #include <net/ethernet.h>
52 #include <net/if_arp.h>
53 #include <net/ifq_var.h>
55 #include <netinet/in_var.h>
58 #include <sys/ioccom.h>
59 #include <net/tap/if_tap.h>
65 #define VKE_DEVNAME "vke"
72 struct kqueue_info *sc_kqueue;
75 struct mbuf *sc_rx_mbuf;
77 struct sysctl_ctx_list sc_sysctl_ctx;
78 struct sysctl_oid *sc_sysctl_tree;
80 int sc_tap_unit; /* unit of backend tap(4) */
81 in_addr_t sc_addr; /* address */
82 in_addr_t sc_mask; /* netmask */
85 static void vke_start(struct ifnet *);
86 static void vke_init(void *);
87 static int vke_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
89 static int vke_attach(const struct vknetif_info *, int);
90 static void vke_intr(void *, struct intrframe *);
91 static int vke_stop(struct vke_softc *);
92 static void vke_rxeof(struct vke_softc *);
93 static int vke_init_addr(struct ifnet *, in_addr_t, in_addr_t);
96 vke_sysinit(void *arg __unused)
100 KASSERT(NetifNum <= VKNETIF_MAX, ("too many netifs: %d\n", NetifNum));
103 for (i = 0; i < NetifNum; ++i) {
104 if (vke_attach(&NetifInfo[i], unit) == 0)
108 SYSINIT(vke, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, vke_sysinit, NULL);
113 struct vke_softc *sc = xsc;
114 struct ifnet *ifp = &sc->arpcom.ac_if;
116 ASSERT_SERIALIZED(ifp->if_serializer);
120 KKASSERT(sc->sc_kqueue == NULL);
121 sc->sc_kqueue = kqueue_add(sc->sc_fd, vke_intr, sc);
122 KKASSERT(sc->sc_kqueue != NULL);
124 ifp->if_flags |= IFF_RUNNING;
125 ifp->if_flags &= ~IFF_OACTIVE;
127 if (sc->sc_addr != 0) {
128 in_addr_t addr, mask;
134 * Make sure vkernel assigned
135 * address will not be added
141 vke_init_addr(ifp, addr, mask);
148 vke_start(struct ifnet *ifp)
150 struct vke_softc *sc = ifp->if_softc;
153 ASSERT_SERIALIZED(ifp->if_serializer);
155 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
158 while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) {
159 if (m->m_pkthdr.len <= MCLBYTES) {
160 m_copydata(m, 0, m->m_pkthdr.len, sc->sc_txbuf);
162 write(sc->sc_fd, sc->sc_txbuf, m->m_pkthdr.len);
172 vke_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
174 struct vke_softc *sc = ifp->if_softc;
177 ASSERT_SERIALIZED(ifp->if_serializer);
181 if (ifp->if_flags & IFF_UP) {
182 if ((ifp->if_flags & IFF_RUNNING) == 0)
185 if (ifp->if_flags & IFF_RUNNING)
194 case SIOCGIFSTATUS: {
195 struct ifstat *ifs = (struct ifstat *)data;
198 len = strlen(ifs->ascii);
199 if (len < sizeof(ifs->ascii)) {
200 ksnprintf(ifs->ascii + len, sizeof(ifs->ascii) - len,
201 "\tBacked by tap%d\n", sc->sc_tap_unit);
206 if (((struct ifaddr *)data)->ifa_addr->sa_family == AF_INET) {
208 * If we are explicitly requested to change address,
209 * we should invalidate address/netmask passed in
210 * from vkernel command line.
217 error = ether_ioctl(ifp, cmd, data);
224 vke_stop(struct vke_softc *sc)
226 struct ifnet *ifp = &sc->arpcom.ac_if;
228 ASSERT_SERIALIZED(ifp->if_serializer);
230 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
232 kqueue_del(sc->sc_kqueue);
233 sc->sc_kqueue = NULL;
236 if (sc->sc_rx_mbuf != NULL) {
237 m_freem(sc->sc_rx_mbuf);
238 sc->sc_rx_mbuf = NULL;
244 vke_intr(void *xsc, struct intrframe *frame __unused)
246 struct vke_softc *sc = xsc;
247 struct ifnet *ifp = &sc->arpcom.ac_if;
249 lwkt_serialize_enter(ifp->if_serializer);
251 if ((ifp->if_flags & IFF_RUNNING) == 0)
259 lwkt_serialize_exit(ifp->if_serializer);
263 vke_rxeof(struct vke_softc *sc)
265 struct ifnet *ifp = &sc->arpcom.ac_if;
268 ASSERT_SERIALIZED(ifp->if_serializer);
276 if ((m = sc->sc_rx_mbuf) == NULL)
277 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
279 sc->sc_rx_mbuf = NULL;
282 * Drain the interface whether we get an mbuf or not or
283 * we might stop receiving interrupts.
286 n = read(sc->sc_fd, mtod(m, void *), MCLBYTES);
288 n = read(sc->sc_fd, sc->sc_txbuf, MCLBYTES);
291 sc->sc_rx_mbuf = m; /* We can use it next time */
296 m->m_pkthdr.rcvif = ifp;
297 m->m_pkthdr.len = m->m_len = n;
298 ifp->if_input(ifp, m);
306 vke_attach(const struct vknetif_info *info, int unit)
308 struct vke_softc *sc;
310 struct tapinfo tapinfo;
311 uint8_t enaddr[ETHER_ADDR_LEN];
314 KKASSERT(info->tap_fd >= 0);
317 if (ioctl(fd, TAPGIFINFO, &tapinfo) < 0) {
318 kprintf(VKE_DEVNAME "%d: ioctl(TAPGIFINFO) failed: %s\n",
319 unit, strerror(errno));
323 if (ioctl(fd, SIOCGIFADDR, enaddr) < 0) {
324 kprintf(VKE_DEVNAME "%d: ioctl(SIOCGIFADDR) failed: %s\n",
325 unit, strerror(errno));
330 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
332 sc->sc_txbuf = kmalloc(MCLBYTES, M_DEVBUF, M_WAITOK);
335 sc->sc_tap_unit = info->tap_unit;
336 sc->sc_addr = info->netif_addr;
337 sc->sc_mask = info->netif_mask;
339 ifp = &sc->arpcom.ac_if;
340 if_initname(ifp, VKE_DEVNAME, sc->sc_unit);
342 /* NB: after if_initname() */
343 sysctl_ctx_init(&sc->sc_sysctl_ctx);
344 sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
345 SYSCTL_STATIC_CHILDREN(_hw),
346 OID_AUTO, ifp->if_xname,
348 if (sc->sc_sysctl_tree == NULL) {
349 kprintf(VKE_DEVNAME "%d: can't add sysctl node\n", unit);
351 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
352 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
353 OID_AUTO, "tap_unit",
354 CTLFLAG_RD, &sc->sc_tap_unit, 0,
355 "Backend tap(4) unit");
359 ifp->if_ioctl = vke_ioctl;
360 ifp->if_start = vke_start;
361 ifp->if_init = vke_init;
362 ifp->if_mtu = tapinfo.mtu;
363 ifp->if_baudrate = tapinfo.baudrate;
364 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
365 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
366 ifq_set_ready(&ifp->if_snd);
370 ether_ifattach(ifp, enaddr, NULL);
372 if (bootverbose && sc->sc_addr != 0) {
373 if_printf(ifp, "pre-configured "
374 "address 0x%08x, netmask 0x%08x\n",
375 ntohl(sc->sc_addr), ntohl(sc->sc_mask));
382 vke_init_addr(struct ifnet *ifp, in_addr_t addr, in_addr_t mask)
384 struct ifaliasreq ifra;
385 struct sockaddr_in *sin;
388 ASSERT_SERIALIZED(ifp->if_serializer);
391 if_printf(ifp, "add pre-configured "
392 "address 0x%08x, netmask 0x%08x\n",
393 ntohl(addr), ntohl(mask));
396 bzero(&ifra, sizeof(ifra));
398 /* NB: no need to set ifaliasreq.ifra_name */
400 sin = (struct sockaddr_in *)&ifra.ifra_addr;
401 sin->sin_family = AF_INET;
402 sin->sin_len = sizeof(*sin);
403 sin->sin_addr.s_addr = addr;
406 sin = (struct sockaddr_in *)&ifra.ifra_mask;
407 sin->sin_len = sizeof(*sin);
408 sin->sin_addr.s_addr = mask;
412 * Temporarily release serializer, in_control() will hold
413 * it again before calling ifnet.if_ioctl().
415 lwkt_serialize_exit(ifp->if_serializer);
416 ret = in_control(NULL, SIOCAIFADDR, (caddr_t)&ifra, ifp, NULL);
417 lwkt_serialize_enter(ifp->if_serializer);