Fix the way <sys/ioccom.h> is included throughout our tree.
[dragonfly.git] / sys / dev / virtual / net / if_vke.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/virtual/net/if_vke.c,v 1.10 2008/05/27 23:44:46 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/serialize.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46
47 #include <machine/md_var.h>
48
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/bpf.h>
52 #include <net/if_arp.h>
53 #include <net/ifq_var.h>
54
55 #include <netinet/in_var.h>
56
57 #include <sys/stat.h>
58 #include <net/tap/if_tap.h>
59 #include <errno.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <fcntl.h>
64
65 #define VKE_DEVNAME             "vke"
66
67 struct vke_softc {
68         struct arpcom           arpcom;
69         int                     sc_fd;
70         int                     sc_unit;
71
72         struct kqueue_info      *sc_kqueue;
73
74         void                    *sc_txbuf;
75         struct mbuf             *sc_rx_mbuf;
76
77         struct sysctl_ctx_list  sc_sysctl_ctx;
78         struct sysctl_oid       *sc_sysctl_tree;
79
80         int                     sc_tap_unit;    /* unit of backend tap(4) */
81         in_addr_t               sc_addr;        /* address */
82         in_addr_t               sc_mask;        /* netmask */
83 };
84
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 *);
88
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);
94
95 static void
96 vke_sysinit(void *arg __unused)
97 {
98         int i, unit;
99
100         KASSERT(NetifNum <= VKNETIF_MAX, ("too many netifs: %d\n", NetifNum));
101
102         unit = 0;
103         for (i = 0; i < NetifNum; ++i) {
104                 if (vke_attach(&NetifInfo[i], unit) == 0)
105                         ++unit;
106         }
107 }
108 SYSINIT(vke, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, vke_sysinit, NULL);
109
110 static void
111 vke_init(void *xsc)
112 {
113         struct vke_softc *sc = xsc;
114         struct ifnet *ifp = &sc->arpcom.ac_if;
115
116         ASSERT_SERIALIZED(ifp->if_serializer);
117
118         vke_stop(sc);
119
120         KKASSERT(sc->sc_kqueue == NULL);
121         sc->sc_kqueue = kqueue_add(sc->sc_fd, vke_intr, sc);
122         KKASSERT(sc->sc_kqueue != NULL);
123
124         ifp->if_flags |= IFF_RUNNING;
125         ifp->if_flags &= ~IFF_OACTIVE;
126
127         if (sc->sc_addr != 0) {
128                 in_addr_t addr, mask;
129
130                 addr = sc->sc_addr;
131                 mask = sc->sc_mask;
132
133                 /*
134                  * Make sure vkernel assigned
135                  * address will not be added
136                  * again.
137                  */
138                 sc->sc_addr = 0;
139                 sc->sc_mask = 0;
140
141                 vke_init_addr(ifp, addr, mask);
142         }
143
144         ifp->if_start(ifp);
145 }
146
147 static void
148 vke_start(struct ifnet *ifp)
149 {
150         struct vke_softc *sc = ifp->if_softc;
151         struct mbuf *m;
152
153         ASSERT_SERIALIZED(ifp->if_serializer);
154
155         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
156                 return;
157
158         while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) {
159                 /*
160                  * Copy the data into a single mbuf and write it out
161                  * non-blocking.
162                  */
163                 if (m->m_pkthdr.len <= MCLBYTES) {
164                         m_copydata(m, 0, m->m_pkthdr.len, sc->sc_txbuf);
165                         BPF_MTAP(ifp, m);
166                         if (write(sc->sc_fd, sc->sc_txbuf, m->m_pkthdr.len) < 0)
167                                 ifp->if_oerrors++;
168                         else
169                                 ifp->if_opackets++;
170                 } else {
171                         ifp->if_oerrors++;
172                 }
173                 m_freem(m);
174         }
175 }
176
177 static int
178 vke_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
179 {
180         struct vke_softc *sc = ifp->if_softc;
181         int error = 0;
182
183         ASSERT_SERIALIZED(ifp->if_serializer);
184
185         switch (cmd) {
186         case SIOCSIFFLAGS:
187                 if (ifp->if_flags & IFF_UP) {
188                         if ((ifp->if_flags & IFF_RUNNING) == 0)
189                                 vke_init(sc);
190                 } else {
191                         if (ifp->if_flags & IFF_RUNNING)
192                                 vke_stop(sc);
193                 }
194                 break;
195         case SIOCGIFMEDIA:
196         case SIOCSIFMEDIA:
197                 error = EOPNOTSUPP;
198                 /* TODO */
199                 break;
200         case SIOCGIFSTATUS: {
201                 struct ifstat *ifs = (struct ifstat *)data;
202                 int len;
203
204                 len = strlen(ifs->ascii);
205                 if (len < sizeof(ifs->ascii)) {
206                         ksnprintf(ifs->ascii + len, sizeof(ifs->ascii) - len,
207                                   "\tBacked by tap%d\n", sc->sc_tap_unit);
208                 }
209                 break;
210         }
211         case SIOCSIFADDR:
212                 if (((struct ifaddr *)data)->ifa_addr->sa_family == AF_INET) {
213                         /*
214                          * If we are explicitly requested to change address,
215                          * we should invalidate address/netmask passed in
216                          * from vkernel command line.
217                          */
218                         sc->sc_addr = 0;
219                         sc->sc_mask = 0;
220                 }
221                 /* FALL THROUGH */
222         default:
223                 error = ether_ioctl(ifp, cmd, data);
224                 break;
225         }
226         return error;
227 }
228
229 static int
230 vke_stop(struct vke_softc *sc)
231 {
232         struct ifnet *ifp = &sc->arpcom.ac_if;
233
234         ASSERT_SERIALIZED(ifp->if_serializer);
235
236         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
237         if (sc->sc_kqueue) {
238                 kqueue_del(sc->sc_kqueue);
239                 sc->sc_kqueue = NULL;
240         }
241
242         if (sc->sc_rx_mbuf != NULL) {
243                 m_freem(sc->sc_rx_mbuf);
244                 sc->sc_rx_mbuf = NULL;
245         }
246         return 0;
247 }
248
249 static void
250 vke_intr(void *xsc, struct intrframe *frame __unused)
251 {
252         struct vke_softc *sc = xsc;
253         struct ifnet *ifp = &sc->arpcom.ac_if;
254
255         ifnet_serialize_all(ifp);
256
257         if ((ifp->if_flags & IFF_RUNNING) == 0)
258                 goto back;
259
260         vke_rxeof(sc);
261
262         ifp->if_start(ifp);
263
264 back:
265         ifnet_deserialize_all(ifp);
266 }
267
268 static void
269 vke_rxeof(struct vke_softc *sc)
270 {
271         struct ifnet *ifp = &sc->arpcom.ac_if;
272         struct mbuf *m;
273
274         ASSERT_SERIALIZED(ifp->if_serializer);
275
276         for (;;) {
277                 int n;
278
279                 /*
280                  * Try to get an mbuf
281                  */
282                 if ((m = sc->sc_rx_mbuf) == NULL)
283                         m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
284                 else
285                         sc->sc_rx_mbuf = NULL;
286
287                 /*
288                  * Drain the interface whether we get an mbuf or not or
289                  * we might stop receiving interrupts.
290                  */
291                 if (m) {
292                         n = read(sc->sc_fd, mtod(m, void *), MCLBYTES);
293                 } else {
294                         n = read(sc->sc_fd, sc->sc_txbuf, MCLBYTES);
295                 }
296                 if (n < 0) {
297                         sc->sc_rx_mbuf = m;     /* We can use it next time */
298                         break;
299                 }
300                 if (m) {
301                         ifp->if_ipackets++;
302                         m->m_pkthdr.rcvif = ifp;
303                         m->m_pkthdr.len = m->m_len = n;
304                         ifp->if_input(ifp, m);
305                 } else {
306                         ifp->if_ierrors++;
307                 }
308         }
309 }
310
311 static int
312 vke_attach(const struct vknetif_info *info, int unit)
313 {
314         struct vke_softc *sc;
315         struct ifnet *ifp;
316         struct tapinfo tapinfo;
317         uint8_t enaddr[ETHER_ADDR_LEN];
318         int fd;
319
320         KKASSERT(info->tap_fd >= 0);
321         fd = info->tap_fd;
322
323         /*
324          * This is only a TAP device if tap_unit is non-zero.  If
325          * connecting to a virtual socket we generate a unique MAC.
326          */
327         if (info->tap_unit >= 0) {
328                 if (ioctl(fd, TAPGIFINFO, &tapinfo) < 0) {
329                         kprintf(VKE_DEVNAME "%d: ioctl(TAPGIFINFO) "
330                                 "failed: %s\n", unit, strerror(errno));
331                         return ENXIO;
332                 }
333
334                 if (ioctl(fd, SIOCGIFADDR, enaddr) < 0) {
335                         kprintf(VKE_DEVNAME "%d: ioctl(SIOCGIFADDR) "
336                                 "failed: %s\n", unit, strerror(errno));
337                         return ENXIO;
338                 }
339         } else {
340                 int fd = open("/dev/urandom", O_RDONLY);
341                 if (fd >= 0) {
342                         read(fd, enaddr + 2, 4);
343                         close(fd);
344                 }
345                 enaddr[4] = (int)getpid() >> 8;
346                 enaddr[5] = (int)getpid() & 255;
347                 
348         }
349         enaddr[1] += 1;
350
351         sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
352
353         sc->sc_txbuf = kmalloc(MCLBYTES, M_DEVBUF, M_WAITOK);
354         sc->sc_fd = fd;
355         sc->sc_unit = unit;
356         sc->sc_tap_unit = info->tap_unit;
357         sc->sc_addr = info->netif_addr;
358         sc->sc_mask = info->netif_mask;
359
360         ifp = &sc->arpcom.ac_if;
361         if_initname(ifp, VKE_DEVNAME, sc->sc_unit);
362
363         /* NB: after if_initname() */
364         sysctl_ctx_init(&sc->sc_sysctl_ctx);
365         sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
366                                              SYSCTL_STATIC_CHILDREN(_hw),
367                                              OID_AUTO, ifp->if_xname,
368                                              CTLFLAG_RD, 0, "");
369         if (sc->sc_sysctl_tree == NULL) {
370                 kprintf(VKE_DEVNAME "%d: can't add sysctl node\n", unit);
371         } else {
372                 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
373                                SYSCTL_CHILDREN(sc->sc_sysctl_tree),
374                                OID_AUTO, "tap_unit",
375                                CTLFLAG_RD, &sc->sc_tap_unit, 0,
376                                "Backend tap(4) unit");
377         }
378
379         ifp->if_softc = sc;
380         ifp->if_ioctl = vke_ioctl;
381         ifp->if_start = vke_start;
382         ifp->if_init = vke_init;
383         ifp->if_mtu = tapinfo.mtu;
384         ifp->if_baudrate = tapinfo.baudrate;
385         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
386         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
387         ifq_set_ready(&ifp->if_snd);
388
389         /* TODO: if_media */
390
391         ether_ifattach(ifp, enaddr, NULL);
392
393         if (bootverbose && sc->sc_addr != 0) {
394                 if_printf(ifp, "pre-configured "
395                           "address 0x%08x, netmask 0x%08x\n",
396                           ntohl(sc->sc_addr), ntohl(sc->sc_mask));
397         }
398
399         return 0;
400 }
401
402 static int
403 vke_init_addr(struct ifnet *ifp, in_addr_t addr, in_addr_t mask)
404 {
405         struct ifaliasreq ifra;
406         struct sockaddr_in *sin;
407         int ret;
408
409         ASSERT_SERIALIZED(ifp->if_serializer);
410
411         if (bootverbose) {
412                 if_printf(ifp, "add pre-configured "
413                           "address 0x%08x, netmask 0x%08x\n",
414                           ntohl(addr), ntohl(mask));
415         }
416
417         bzero(&ifra, sizeof(ifra));
418
419         /* NB: no need to set ifaliasreq.ifra_name */
420
421         sin = (struct sockaddr_in *)&ifra.ifra_addr;
422         sin->sin_family = AF_INET;
423         sin->sin_len = sizeof(*sin);
424         sin->sin_addr.s_addr = addr;
425
426         if (mask != 0) {
427                 sin = (struct sockaddr_in *)&ifra.ifra_mask;
428                 sin->sin_len = sizeof(*sin);
429                 sin->sin_addr.s_addr = mask;
430         }
431
432         /*
433          * Temporarily release serializer, in_control() will hold
434          * it again before calling ifnet.if_ioctl().
435          */
436         ifnet_deserialize_all(ifp);
437         ret = in_control(NULL, SIOCAIFADDR, (caddr_t)&ifra, ifp, NULL);
438         ifnet_serialize_all(ifp);
439
440         return ret;
441 }