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
35 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/serialize.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
45 #include <machine/md_var.h>
46 #include <machine/cothread.h>
48 #include <net/ethernet.h>
51 #include <net/if_arp.h>
52 #include <net/if_media.h>
53 #include <net/ifq_var.h>
54 #include <net/vlan/if_vlan_ether.h>
56 #include <netinet/in_var.h>
59 #include <net/tap/if_tap.h>
67 #define VKE_DEVNAME "vke"
69 #define VKE_CHUNK 8 /* number of mbufs to queue before interrupting */
71 #define NETFIFOINDEX(u, sc) ((u) & ((sc)->sc_ringsize - 1))
73 #define VKE_COTD_RUN 0
74 #define VKE_COTD_EXIT 1
75 #define VKE_COTD_DEAD 2
82 typedef struct vke_fifo *fifo_t;
84 /* Default value for a long time */
85 #define VKE_DEFAULT_RINGSIZE 256
86 static int vke_max_ringsize = 0;
87 TUNABLE_INT("hw.vke.max_ringsize", &vke_max_ringsize);
89 #define LOW_POW_2(n) (1 << (fls(n) - 1))
106 fifo_t sc_txfifo_done;
115 struct sysctl_ctx_list sc_sysctl_ctx;
116 struct sysctl_oid *sc_sysctl_tree;
118 int sc_tap_unit; /* unit of backend tap(4) */
119 in_addr_t sc_addr; /* address */
120 in_addr_t sc_mask; /* netmask */
122 struct ifmedia sc_media;
125 static void vke_start(struct ifnet *, struct ifaltq_subque *);
126 static void vke_init(void *);
127 static int vke_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
129 static int vke_media_change(struct ifnet *);
130 static void vke_media_status(struct ifnet *, struct ifmediareq *);
132 static int vke_attach(const struct vknetif_info *, int);
133 static int vke_stop(struct vke_softc *);
134 static int vke_init_addr(struct ifnet *, in_addr_t, in_addr_t);
135 static void vke_tx_intr(cothread_t cotd);
136 static void vke_tx_thread(cothread_t cotd);
137 static void vke_rx_intr(cothread_t cotd);
138 static void vke_rx_thread(cothread_t cotd);
140 static int vke_txfifo_enqueue(struct vke_softc *sc, struct mbuf *m);
141 static struct mbuf *vke_txfifo_dequeue(struct vke_softc *sc);
143 static int vke_txfifo_done_enqueue(struct vke_softc *sc, struct mbuf *m);
144 static struct mbuf * vke_txfifo_done_dequeue(struct vke_softc *sc, struct mbuf *nm);
146 static struct mbuf *vke_rxfifo_dequeue(struct vke_softc *sc, struct mbuf *nm);
147 static struct mbuf *vke_rxfifo_sniff(struct vke_softc *sc);
150 vke_sysinit(void *arg __unused)
154 KASSERT(NetifNum <= VKNETIF_MAX, ("too many netifs: %d", NetifNum));
157 for (i = 0; i < NetifNum; ++i) {
158 if (vke_attach(&NetifInfo[i], unit) == 0)
162 SYSINIT(vke, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, vke_sysinit, NULL);
165 * vke_txfifo_done_enqueue() - Add an mbuf to the transmit done fifo. Since
166 * the cothread cannot free transmit mbufs after processing we put them on
167 * the done fifo so the kernel can free them.
170 vke_txfifo_done_enqueue(struct vke_softc *sc, struct mbuf *m)
172 fifo_t fifo = sc->sc_txfifo_done;
174 while (NETFIFOINDEX(fifo->windex + 1, sc) == NETFIFOINDEX(fifo->rindex, sc)) {
178 fifo->array[NETFIFOINDEX(fifo->windex, sc)] = m;
185 * vke_txfifo_done_dequeue() - Remove an mbuf from the transmit done fifo.
188 vke_txfifo_done_dequeue(struct vke_softc *sc, struct mbuf *nm)
190 fifo_t fifo = sc->sc_txfifo_done;
193 if (NETFIFOINDEX(fifo->rindex, sc) == NETFIFOINDEX(fifo->windex, sc))
196 m = fifo->array[NETFIFOINDEX(fifo->rindex, sc)];
197 fifo->array[NETFIFOINDEX(fifo->rindex, sc)] = nm;
204 * vke_txfifo_enqueue() - Add an mbuf to the transmit fifo.
207 vke_txfifo_enqueue(struct vke_softc *sc, struct mbuf *m)
209 fifo_t fifo = sc->sc_txfifo;
211 if (NETFIFOINDEX(fifo->windex + 1, sc) == NETFIFOINDEX(fifo->rindex, sc))
214 fifo->array[NETFIFOINDEX(fifo->windex, sc)] = m;
222 * vke_txfifo_dequeue() - Return next mbuf on the transmit fifo if one
226 vke_txfifo_dequeue(struct vke_softc *sc)
228 fifo_t fifo = sc->sc_txfifo;
231 if (NETFIFOINDEX(fifo->rindex, sc) == NETFIFOINDEX(fifo->windex, sc))
234 m = fifo->array[NETFIFOINDEX(fifo->rindex, sc)];
235 fifo->array[NETFIFOINDEX(fifo->rindex, sc)] = NULL;
243 vke_txfifo_empty(struct vke_softc *sc)
245 fifo_t fifo = sc->sc_txfifo;
247 if (NETFIFOINDEX(fifo->rindex, sc) == NETFIFOINDEX(fifo->windex, sc))
253 * vke_rxfifo_dequeue() - Return next mbuf on the receice fifo if one
254 * exists replacing it with newm which should point to a newly allocated
258 vke_rxfifo_dequeue(struct vke_softc *sc, struct mbuf *newm)
260 fifo_t fifo = sc->sc_rxfifo;
263 if (NETFIFOINDEX(fifo->rindex, sc) == NETFIFOINDEX(fifo->windex, sc))
266 m = fifo->array[NETFIFOINDEX(fifo->rindex, sc)];
267 fifo->array[NETFIFOINDEX(fifo->rindex, sc)] = newm;
274 * Return the next mbuf if available but do NOT remove it from the FIFO.
277 vke_rxfifo_sniff(struct vke_softc *sc)
279 fifo_t fifo = sc->sc_rxfifo;
282 if (NETFIFOINDEX(fifo->rindex, sc) == NETFIFOINDEX(fifo->windex, sc))
285 m = fifo->array[NETFIFOINDEX(fifo->rindex, sc)];
293 struct vke_softc *sc = xsc;
294 struct ifnet *ifp = &sc->arpcom.ac_if;
295 size_t ringsize = sc->sc_ringsize * sizeof(struct mbuf *);
298 ASSERT_SERIALIZED(ifp->if_serializer);
302 ifp->if_flags |= IFF_RUNNING;
303 ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
306 * Allocate memory for FIFO structures and mbufs.
308 sc->sc_txfifo = kmalloc(sizeof(*sc->sc_txfifo),
309 M_DEVBUF, M_WAITOK | M_ZERO);
310 sc->sc_txfifo_done = kmalloc(sizeof(*sc->sc_txfifo_done),
311 M_DEVBUF, M_WAITOK | M_ZERO);
312 sc->sc_rxfifo = kmalloc(sizeof(*sc->sc_rxfifo),
313 M_DEVBUF, M_WAITOK | M_ZERO);
314 sc->sc_txfifo->array = kmalloc(ringsize, M_DEVBUF, M_WAITOK | M_ZERO);
315 sc->sc_txfifo_done->array = kmalloc(ringsize, M_DEVBUF, M_WAITOK | M_ZERO);
316 sc->sc_rxfifo->array = kmalloc(ringsize, M_DEVBUF, M_WAITOK | M_ZERO);
318 for (i = 0; i < sc->sc_ringsize; i++) {
319 sc->sc_rxfifo->array[i] = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
320 sc->sc_txfifo->array[i] = NULL;
321 sc->sc_txfifo_done->array[i] = NULL;
324 sc->cotd_tx_exit = sc->cotd_rx_exit = VKE_COTD_RUN;
325 sc->cotd_tx = cothread_create(vke_tx_thread, vke_tx_intr, sc, "vke_tx");
326 sc->cotd_rx = cothread_create(vke_rx_thread, vke_rx_intr, sc, "vke_rx");
328 if (sc->sc_addr != 0) {
329 in_addr_t addr, mask;
335 * Make sure vkernel assigned
336 * address will not be added
342 vke_init_addr(ifp, addr, mask);
348 * Called from kernel.
350 * NOTE: We can't make any kernel callbacks while holding cothread lock
351 * because the cothread lock is not governed by the kernel scheduler
352 * (so mplock, tokens, etc will not be released).
355 vke_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
357 struct vke_softc *sc = ifp->if_softc;
359 cothread_t cotd = sc->cotd_tx;
362 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
363 ASSERT_SERIALIZED(ifp->if_serializer);
365 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
369 while ((m = ifsq_dequeue(ifsq)) != NULL) {
370 if (vke_txfifo_enqueue(sc, m) != -1) {
371 ETHER_BPF_MTAP(ifp, m);
372 if (count++ == VKE_CHUNK) {
373 cothread_lock(cotd, 0);
374 cothread_signal(cotd);
375 cothread_unlock(cotd, 0);
383 cothread_lock(cotd, 0);
384 cothread_signal(cotd);
385 cothread_unlock(cotd, 0);
390 vke_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
392 struct vke_softc *sc = ifp->if_softc;
393 struct ifreq *ifr = (struct ifreq *)data;
396 ASSERT_SERIALIZED(ifp->if_serializer);
400 if (ifp->if_flags & IFF_UP) {
401 if ((ifp->if_flags & IFF_RUNNING) == 0)
404 if (ifp->if_flags & IFF_RUNNING)
410 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
412 case SIOCGIFSTATUS: {
413 struct ifstat *ifs = (struct ifstat *)data;
416 len = strlen(ifs->ascii);
417 if (len < sizeof(ifs->ascii)) {
418 if (sc->sc_tap_unit >= 0)
419 ksnprintf(ifs->ascii + len, sizeof(ifs->ascii) - len,
420 "\tBacked by tap%d\n", sc->sc_tap_unit);
425 if (((struct ifaddr *)data)->ifa_addr->sa_family == AF_INET) {
427 * If we are explicitly requested to change address,
428 * we should invalidate address/netmask passed in
429 * from vkernel command line.
436 error = ether_ioctl(ifp, cmd, data);
443 vke_stop(struct vke_softc *sc)
445 struct ifnet *ifp = &sc->arpcom.ac_if;
448 ASSERT_SERIALIZED(ifp->if_serializer);
450 ifp->if_flags &= ~IFF_RUNNING;
451 ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
455 cothread_lock(sc->cotd_tx, 0);
456 if (sc->cotd_tx_exit == VKE_COTD_RUN)
457 sc->cotd_tx_exit = VKE_COTD_EXIT;
458 cothread_signal(sc->cotd_tx);
459 cothread_unlock(sc->cotd_tx, 0);
460 cothread_delete(&sc->cotd_tx);
463 cothread_lock(sc->cotd_rx, 0);
464 if (sc->cotd_rx_exit == VKE_COTD_RUN)
465 sc->cotd_rx_exit = VKE_COTD_EXIT;
466 cothread_signal(sc->cotd_rx);
467 cothread_unlock(sc->cotd_rx, 0);
468 cothread_delete(&sc->cotd_rx);
471 for (i = 0; i < sc->sc_ringsize; i++) {
472 if (sc->sc_rxfifo && sc->sc_rxfifo->array[i]) {
473 m_freem(sc->sc_rxfifo->array[i]);
474 sc->sc_rxfifo->array[i] = NULL;
476 if (sc->sc_txfifo && sc->sc_txfifo->array[i]) {
477 m_freem(sc->sc_txfifo->array[i]);
478 sc->sc_txfifo->array[i] = NULL;
480 if (sc->sc_txfifo_done && sc->sc_txfifo_done->array[i]) {
481 m_freem(sc->sc_txfifo_done->array[i]);
482 sc->sc_txfifo_done->array[i] = NULL;
487 if (sc->sc_txfifo->array)
488 kfree(sc->sc_txfifo->array, M_DEVBUF);
489 kfree(sc->sc_txfifo, M_DEVBUF);
490 sc->sc_txfifo = NULL;
493 if (sc->sc_txfifo_done) {
494 if (sc->sc_txfifo_done->array)
495 kfree(sc->sc_txfifo_done->array, M_DEVBUF);
496 kfree(sc->sc_txfifo_done, M_DEVBUF);
497 sc->sc_txfifo_done = NULL;
501 if (sc->sc_rxfifo->array)
502 kfree(sc->sc_rxfifo->array, M_DEVBUF);
503 kfree(sc->sc_rxfifo, M_DEVBUF);
504 sc->sc_rxfifo = NULL;
513 * vke_rx_intr() is the interrupt function for the receive cothread.
516 vke_rx_intr(cothread_t cotd)
520 struct vke_softc *sc = cotd->arg;
521 struct ifnet *ifp = &sc->arpcom.ac_if;
522 static int count = 0;
524 ifnet_serialize_all(ifp);
525 cothread_lock(cotd, 0);
527 if (sc->cotd_rx_exit != VKE_COTD_RUN) {
528 cothread_unlock(cotd, 0);
529 ifnet_deserialize_all(ifp);
532 if (sc->cotd_ipackets) {
533 IFNET_STAT_INC(ifp, ipackets, 1);
534 sc->cotd_ipackets = 0;
536 cothread_unlock(cotd, 0);
538 while ((m = vke_rxfifo_sniff(sc)) != NULL) {
539 nm = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
541 vke_rxfifo_dequeue(sc, nm);
542 ifp->if_input(ifp, m, NULL, -1);
543 if (count++ == VKE_CHUNK) {
544 cothread_lock(cotd, 0);
545 cothread_signal(cotd);
546 cothread_unlock(cotd, 0);
550 vke_rxfifo_dequeue(sc, m);
555 cothread_lock(cotd, 0);
556 cothread_signal(cotd);
557 cothread_unlock(cotd, 0);
559 ifnet_deserialize_all(ifp);
563 * vke_tx_intr() is the interrupt function for the transmit cothread.
564 * Calls vke_start() to handle processing transmit mbufs.
567 vke_tx_intr(cothread_t cotd)
569 struct vke_softc *sc = cotd->arg;
570 struct ifnet *ifp = &sc->arpcom.ac_if;
573 ifnet_serialize_all(ifp);
574 cothread_lock(cotd, 0);
575 if (sc->cotd_tx_exit != VKE_COTD_RUN) {
576 cothread_unlock(cotd, 0);
577 ifnet_deserialize_all(ifp);
580 if (sc->cotd_opackets) {
581 IFNET_STAT_INC(ifp, opackets, 1);
582 sc->cotd_opackets = 0;
584 if (sc->cotd_oerrors) {
585 IFNET_STAT_INC(ifp, oerrors, 1);
586 sc->cotd_oerrors = 0;
588 cothread_unlock(cotd, 0);
591 * Free TX mbufs that have been processed before starting new
592 * ones going to be pipeline friendly.
594 while ((m = vke_txfifo_done_dequeue(sc, NULL)) != NULL) {
598 if ((ifp->if_flags & IFF_RUNNING) == 0)
601 ifnet_deserialize_all(ifp);
605 * vke_rx_thread() is the body of the receive cothread.
607 * WARNING! THIS IS A COTHREAD WHICH HAS NO PER-CPU GLOBALDATA!!!!!
610 vke_rx_thread(cothread_t cotd)
613 struct vke_softc *sc = cotd->arg;
614 struct ifnet *ifp = &sc->arpcom.ac_if;
615 fifo_t fifo = sc->sc_rxfifo;
621 /* Select timeout cannot be infinite since we need to check for
622 * the exit flag sc->cotd_rx_exit.
630 while (sc->cotd_rx_exit == VKE_COTD_RUN) {
632 * Wait for the RX FIFO to be loaded with
635 if (NETFIFOINDEX(fifo->windex + 1, sc) ==
636 NETFIFOINDEX(fifo->rindex, sc)) {
642 * Load data into the rx fifo
644 m = fifo->array[NETFIFOINDEX(fifo->windex, sc)];
647 n = read(sc->sc_fd, mtod(m, void *), MCLBYTES);
649 /* no mycpu in cothread */
650 /*IFNET_STAT_INC(ifp, ipackets, 1);*/
652 m->m_pkthdr.rcvif = ifp;
653 m->m_pkthdr.len = m->m_len = n;
656 if (count++ == VKE_CHUNK) {
665 FD_SET(sc->sc_fd, &fdset);
667 if (select(sc->sc_fd + 1, &fdset, NULL, NULL, &tv) == -1) {
669 VKE_DEVNAME "%d: select failed for "
670 "TAP device\n", sc->sc_unit);
676 sc->cotd_rx_exit = VKE_COTD_DEAD;
680 * vke_tx_thread() is the body of the transmit cothread.
682 * WARNING! THIS IS A COTHREAD WHICH HAS NO PER-CPU GLOBALDATA!!!!!
685 vke_tx_thread(cothread_t cotd)
688 struct vke_softc *sc = cotd->arg;
689 /*struct ifnet *ifp = &sc->arpcom.ac_if;*/
692 while (sc->cotd_tx_exit == VKE_COTD_RUN) {
694 * Write outgoing packets to the TAP interface
696 m = vke_txfifo_dequeue(sc);
698 if (m->m_pkthdr.len <= MCLBYTES) {
699 m_copydata(m, 0, m->m_pkthdr.len, sc->sc_txbuf);
700 sc->sc_txbuf_len = m->m_pkthdr.len;
702 if (write(sc->sc_fd, sc->sc_txbuf,
703 sc->sc_txbuf_len) < 0) {
704 /* no mycpu in cothread */
705 /*IFNET_STAT_INC(ifp, oerrors, 1);*/
708 /* no mycpu in cothread */
709 /*IFNET_STAT_INC(ifp, opackets, 1);*/
713 if (count++ == VKE_CHUNK) {
717 vke_txfifo_done_enqueue(sc, m);
723 cothread_lock(cotd, 1);
724 if (vke_txfifo_empty(sc))
726 cothread_unlock(cotd, 1);
730 sc->cotd_tx_exit = VKE_COTD_DEAD;
734 vke_attach(const struct vknetif_info *info, int unit)
736 struct vke_softc *sc;
738 struct tapinfo tapinfo;
739 uint8_t enaddr[ETHER_ADDR_LEN];
743 KKASSERT(info->tap_fd >= 0);
750 bcopy(info->enaddr, enaddr, ETHER_ADDR_LEN);
753 * This is only a TAP device if tap_unit is non-zero. If
754 * connecting to a virtual socket we generate a unique MAC.
756 * WARNING: enaddr[0] bit 0 is the multicast bit, when
757 * randomizing enaddr[] just leave the first
758 * two bytes 00 00 for now.
760 bzero(enaddr, sizeof(enaddr));
761 if (info->tap_unit >= 0) {
762 if (ioctl(fd, TAPGIFINFO, &tapinfo) < 0) {
763 kprintf(VKE_DEVNAME "%d: ioctl(TAPGIFINFO) "
764 "failed: %s\n", unit, strerror(errno));
768 if (ioctl(fd, SIOCGIFADDR, enaddr) < 0) {
769 kprintf(VKE_DEVNAME "%d: ioctl(SIOCGIFADDR) "
770 "failed: %s\n", unit, strerror(errno));
774 int fd = open("/dev/urandom", O_RDONLY);
776 read(fd, enaddr + 2, 4);
779 enaddr[4] = (int)getpid() >> 8;
780 enaddr[5] = (int)getpid() & 255;
785 if (ETHER_IS_MULTICAST(enaddr)) {
786 kprintf(VKE_DEVNAME "%d: illegal MULTICAST ether mac!\n", unit);
790 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
792 sc->sc_txbuf = kmalloc(MCLBYTES, M_DEVBUF, M_WAITOK);
795 sc->sc_tap_unit = info->tap_unit;
796 sc->sc_addr = info->netif_addr;
797 sc->sc_mask = info->netif_mask;
799 if (vke_max_ringsize == 0) {
800 nmbufs = nmbclusters / (NetifNum * 2);
801 sc->sc_ringsize = LOW_POW_2(nmbufs);
802 if (sc->sc_ringsize > VKE_DEFAULT_RINGSIZE)
803 sc->sc_ringsize = VKE_DEFAULT_RINGSIZE;
804 } else if (vke_max_ringsize >= VKE_CHUNK) { /* Tunable specified */
805 sc->sc_ringsize = LOW_POW_2(vke_max_ringsize);
807 sc->sc_ringsize = LOW_POW_2(VKE_CHUNK);
810 ifp = &sc->arpcom.ac_if;
811 if_initname(ifp, VKE_DEVNAME, sc->sc_unit);
813 /* NB: after if_initname() */
814 sysctl_ctx_init(&sc->sc_sysctl_ctx);
815 sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
816 SYSCTL_STATIC_CHILDREN(_hw),
817 OID_AUTO, ifp->if_xname,
819 if (sc->sc_sysctl_tree == NULL) {
820 kprintf(VKE_DEVNAME "%d: can't add sysctl node\n", unit);
822 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
823 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
824 OID_AUTO, "tap_unit",
825 CTLFLAG_RD, &sc->sc_tap_unit, 0,
826 "Backend tap(4) unit");
830 ifp->if_ioctl = vke_ioctl;
831 ifp->if_start = vke_start;
832 ifp->if_init = vke_init;
833 ifp->if_mtu = tapinfo.mtu;
834 ifp->if_baudrate = tapinfo.baudrate;
835 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
836 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
837 ifq_set_ready(&ifp->if_snd);
839 ifmedia_init(&sc->sc_media, 0, vke_media_change, vke_media_status);
840 /* We support as many media types as we please for
841 debugging purposes */
842 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T, 0, NULL);
843 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
844 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_2, 0, NULL);
845 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_5, 0, NULL);
846 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX, 0, NULL);
847 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
848 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_FX, 0, NULL);
849 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_T4, 0, NULL);
850 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_VG, 0, NULL);
851 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_T2, 0, NULL);
852 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_FX, 0, NULL);
853 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_STP, 0, NULL);
854 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_FL, 0, NULL);
855 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_SX, 0, NULL);
856 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_LX, 0, NULL);
857 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_CX, 0, NULL);
858 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_T, 0, NULL);
859 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
860 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_HPNA_1, 0, NULL);
861 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_LR, 0, NULL);
862 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_SR, 0, NULL);
863 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
864 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_2500_SX, 0, NULL);
865 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_TWINAX, 0, NULL);
866 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_TWINAX_LONG, 0, NULL);
867 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_LRM, 0, NULL);
868 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_T, 0, NULL);
869 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_40G_CR4, 0, NULL);
870 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_40G_SR4, 0, NULL);
871 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_40G_LR4, 0, NULL);
872 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
874 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
876 ifp->if_link_state = LINK_STATE_UP;
878 ether_ifattach(ifp, enaddr, NULL);
880 if (bootverbose && sc->sc_addr != 0) {
881 if_printf(ifp, "pre-configured "
882 "address 0x%08x, netmask 0x%08x, %d mbuf clusters\n",
883 ntohl(sc->sc_addr), ntohl(sc->sc_mask), sc->sc_ringsize);
890 vke_init_addr(struct ifnet *ifp, in_addr_t addr, in_addr_t mask)
892 struct ifaliasreq ifra;
893 struct sockaddr_in *sin;
896 ASSERT_SERIALIZED(ifp->if_serializer);
899 if_printf(ifp, "add pre-configured "
900 "address 0x%08x, netmask 0x%08x\n",
901 ntohl(addr), ntohl(mask));
904 bzero(&ifra, sizeof(ifra));
906 /* NB: no need to set ifaliasreq.ifra_name */
908 sin = (struct sockaddr_in *)&ifra.ifra_addr;
909 sin->sin_family = AF_INET;
910 sin->sin_len = sizeof(*sin);
911 sin->sin_addr.s_addr = addr;
914 sin = (struct sockaddr_in *)&ifra.ifra_mask;
915 sin->sin_len = sizeof(*sin);
916 sin->sin_addr.s_addr = mask;
920 * Temporarily release serializer, in_control() will hold
921 * it again before calling ifnet.if_ioctl().
923 ifnet_deserialize_all(ifp);
924 ret = in_control(SIOCAIFADDR, (caddr_t)&ifra, ifp, NULL);
925 ifnet_serialize_all(ifp);
930 static int vke_media_change(struct ifnet *ifp)
936 static void vke_media_status(struct ifnet *ifp, struct ifmediareq *imr)
938 struct vke_softc *sc = (struct vke_softc *)ifp->if_softc;
940 imr->ifm_status = IFM_AVALID;
941 imr->ifm_status |= IFM_ACTIVE;
943 if(sc->sc_media.ifm_cur) {
944 if(sc->sc_media.ifm_cur->ifm_media == IFM_ETHER) {
945 imr->ifm_active = IFM_ETHER | IFM_1000_T | IFM_FDX;
947 imr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
950 imr->ifm_active = IFM_ETHER | IFM_1000_T | IFM_FDX;