2 * Copyright (c) 2008 The FreeBSD Foundation
3 * Copyright (c) 2009-2010 Bjoern A. Zeeb <bz@FreeBSD.org>
6 * This software was developed by CK Software GmbH under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * A pair of virtual back-to-back connected ethernet like interfaces
33 * (``two interfaces with a virtual cross-over cable'').
35 * This is mostly intended to be used to provide connectivity between
36 * different virtual network stack instances.
39 * Things to re-think once we have more experience:
40 * - ifp->if_reassign function once we can test with vimage. Depending on
41 * how if_vmove() is going to be improved.
42 * - Real random etheraddrs that are checked to be uniquish; we would need
43 * to re-do them in case we move the interface between network stacks
44 * in a private if_reassign function.
45 * In case we bridge to a real interface/network or between indepedent
46 * epairs on multiple stacks/machines, we may need this.
47 * For now let the user handle that case.
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/kernel.h>
56 #include <sys/module.h>
57 #include <sys/refcount.h>
58 #include <sys/queue.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/sysctl.h>
63 #include <sys/types.h>
66 #include <net/ethernet.h>
68 #include <net/if_clone.h>
69 #include <net/if_media.h>
70 #include <net/if_var.h>
71 #include <net/if_types.h>
72 #include <net/netisr.h>
75 SYSCTL_DECL(_net_link);
76 static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
79 static int epair_debug = 0;
80 SYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
81 &epair_debug, 0, "if_epair(4) debugging.");
82 #define DPRINTF(fmt, arg...) \
84 printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
86 #define DPRINTF(fmt, arg...)
89 static void epair_nh_sintr(struct mbuf *);
90 static struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
91 static void epair_nh_drainedcpu(u_int);
93 static void epair_start_locked(struct ifnet *);
94 static int epair_media_change(struct ifnet *);
95 static void epair_media_status(struct ifnet *, struct ifmediareq *);
97 static int epair_clone_match(struct if_clone *, const char *);
98 static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
99 static int epair_clone_destroy(struct if_clone *, struct ifnet *);
101 static const char epairname[] = "epair";
103 /* Netisr related definitions and sysctl. */
104 static struct netisr_handler epair_nh = {
105 .nh_name = epairname,
106 .nh_proto = NETISR_EPAIR,
107 .nh_policy = NETISR_POLICY_CPU,
108 .nh_handler = epair_nh_sintr,
109 .nh_m2cpuid = epair_nh_m2cpuid,
110 .nh_drainedcpu = epair_nh_drainedcpu,
114 sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
118 netisr_getqlimit(&epair_nh, &qlimit);
119 error = sysctl_handle_int(oidp, &qlimit, 0, req);
120 if (error || !req->newptr)
124 return (netisr_setqlimit(&epair_nh, qlimit));
126 SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
127 0, 0, sysctl_epair_netisr_maxqlen, "I",
128 "Maximum if_epair(4) netisr \"hw\" queue length");
131 struct ifnet *ifp; /* This ifp. */
132 struct ifnet *oifp; /* other ifp of pair. */
133 struct ifmedia media; /* Media config (fake). */
134 u_int refcount; /* # of mbufs in flight. */
135 u_int cpuid; /* CPU ID assigned upon creation. */
136 void (*if_qflush)(struct ifnet *);
137 /* Original if_qflush routine. */
141 * Per-CPU list of ifps with data in the ifq that needs to be flushed
142 * to the netisr ``hw'' queue before we allow any further direct queuing
143 * to the ``hw'' queue.
145 struct epair_ifp_drain {
146 STAILQ_ENTRY(epair_ifp_drain) ifp_next;
149 STAILQ_HEAD(eid_list, epair_ifp_drain);
151 #define EPAIR_LOCK_INIT(dpcpu) mtx_init(&(dpcpu)->if_epair_mtx, \
152 "if_epair", NULL, MTX_DEF)
153 #define EPAIR_LOCK_DESTROY(dpcpu) mtx_destroy(&(dpcpu)->if_epair_mtx)
154 #define EPAIR_LOCK_ASSERT(dpcpu) mtx_assert(&(dpcpu)->if_epair_mtx, \
156 #define EPAIR_LOCK(dpcpu) mtx_lock(&(dpcpu)->if_epair_mtx)
157 #define EPAIR_UNLOCK(dpcpu) mtx_unlock(&(dpcpu)->if_epair_mtx)
160 #define EPAIR_REFCOUNT_INIT(r, v) refcount_init((r), (v))
161 #define EPAIR_REFCOUNT_AQUIRE(r) refcount_acquire((r))
162 #define EPAIR_REFCOUNT_RELEASE(r) refcount_release((r))
163 #define EPAIR_REFCOUNT_ASSERT(a, p) KASSERT(a, p)
165 #define EPAIR_REFCOUNT_INIT(r, v)
166 #define EPAIR_REFCOUNT_AQUIRE(r)
167 #define EPAIR_REFCOUNT_RELEASE(r)
168 #define EPAIR_REFCOUNT_ASSERT(a, p)
171 static MALLOC_DEFINE(M_EPAIR, epairname,
172 "Pair of virtual cross-over connected Ethernet-like interfaces");
174 static VNET_DEFINE(struct if_clone *, epair_cloner);
175 #define V_epair_cloner VNET(epair_cloner)
178 * DPCPU area and functions.
181 struct mtx if_epair_mtx; /* Per-CPU locking. */
182 int epair_drv_flags; /* Per-CPU ``hw'' drv flags. */
183 struct eid_list epair_ifp_drain_list; /* Per-CPU list of ifps with
184 * data in the ifq. */
186 DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
189 epair_dpcpu_init(void)
191 struct epair_dpcpu *epair_dpcpu;
196 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
198 /* Initialize per-cpu lock. */
199 EPAIR_LOCK_INIT(epair_dpcpu);
201 /* Driver flags are per-cpu as are our netisr "hw" queues. */
202 epair_dpcpu->epair_drv_flags = 0;
205 * Initialize per-cpu drain list.
206 * Manually do what STAILQ_HEAD_INITIALIZER would do.
208 s = &epair_dpcpu->epair_ifp_drain_list;
209 s->stqh_first = NULL;
210 s->stqh_last = &s->stqh_first;
215 epair_dpcpu_detach(void)
217 struct epair_dpcpu *epair_dpcpu;
221 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
223 /* Destroy per-cpu lock. */
224 EPAIR_LOCK_DESTROY(epair_dpcpu);
232 cpuid_from_ifp(struct ifnet *ifp)
234 struct epair_softc *sc;
244 * Netisr handler functions.
247 epair_nh_sintr(struct mbuf *m)
250 struct epair_softc *sc;
252 ifp = m->m_pkthdr.rcvif;
253 (*ifp->if_input)(ifp, m);
255 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
256 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
257 ("%s: ifp=%p sc->refcount not >= 1: %d",
258 __func__, ifp, sc->refcount));
259 DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
263 epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
266 *cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
272 epair_nh_drainedcpu(u_int cpuid)
274 struct epair_dpcpu *epair_dpcpu;
275 struct epair_ifp_drain *elm, *tvar;
278 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
279 EPAIR_LOCK(epair_dpcpu);
281 * Assume our "hw" queue and possibly ifq will be emptied
282 * again. In case we will overflow the "hw" queue while
283 * draining, epair_start_locked will set IFF_DRV_OACTIVE
284 * again and we will stop and return.
286 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
289 epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
290 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
291 epair_start_locked(ifp);
293 IFQ_LOCK(&ifp->if_snd);
294 if (IFQ_IS_EMPTY(&ifp->if_snd)) {
295 struct epair_softc *sc;
297 STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
298 elm, epair_ifp_drain, ifp_next);
299 /* The cached ifp goes off the list. */
301 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
302 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
303 ("%s: ifp=%p sc->refcount not >= 1: %d",
304 __func__, ifp, sc->refcount));
307 IFQ_UNLOCK(&ifp->if_snd);
309 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
310 /* Our "hw"q overflew again. */
311 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
312 DPRINTF("hw queue length overflow at %u\n",
317 EPAIR_UNLOCK(epair_dpcpu);
321 * Network interface (`if') related functions.
324 epair_remove_ifp_from_draining(struct ifnet *ifp)
326 struct epair_dpcpu *epair_dpcpu;
327 struct epair_ifp_drain *elm, *tvar;
331 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
332 EPAIR_LOCK(epair_dpcpu);
333 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
335 if (ifp == elm->ifp) {
336 struct epair_softc *sc;
339 &epair_dpcpu->epair_ifp_drain_list, elm,
340 epair_ifp_drain, ifp_next);
341 /* The cached ifp goes off the list. */
343 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
344 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
345 ("%s: ifp=%p sc->refcount not >= 1: %d",
346 __func__, ifp, sc->refcount));
350 EPAIR_UNLOCK(epair_dpcpu);
355 epair_add_ifp_for_draining(struct ifnet *ifp)
357 struct epair_dpcpu *epair_dpcpu;
358 struct epair_softc *sc;
359 struct epair_ifp_drain *elm = NULL;
362 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
363 EPAIR_LOCK_ASSERT(epair_dpcpu);
364 STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
367 /* If the ifp is there already, return success. */
371 elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
376 /* Add a reference for the ifp pointer on the list. */
377 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
378 STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
384 epair_start_locked(struct ifnet *ifp)
386 struct epair_dpcpu *epair_dpcpu;
388 struct epair_softc *sc;
392 DPRINTF("ifp=%p\n", ifp);
394 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
395 EPAIR_LOCK_ASSERT(epair_dpcpu);
397 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
399 if ((ifp->if_flags & IFF_UP) == 0)
403 * We get patckets here from ether_output via if_handoff()
404 * and ned to put them into the input queue of the oifp
405 * and call oifp->if_input() via netisr/epair_sintr().
410 IFQ_DEQUEUE(&ifp->if_snd, m);
416 * In case the outgoing interface is not usable,
419 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
420 (oifp->if_flags & IFF_UP) ==0) {
425 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
428 * Add a reference so the interface cannot go while the
429 * packet is in transit as we rely on rcvif to stay valid.
431 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
432 m->m_pkthdr.rcvif = oifp;
433 CURVNET_SET_QUIET(oifp->if_vnet);
434 error = netisr_queue(NETISR_EPAIR, m);
438 /* Someone else received the packet. */
441 /* The packet was freed already. */
442 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
443 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
444 (void) epair_add_ifp_for_draining(ifp);
446 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
447 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
448 ("%s: ifp=%p sc->refcount not >= 1: %d",
449 __func__, oifp, sc->refcount));
455 epair_start(struct ifnet *ifp)
457 struct epair_dpcpu *epair_dpcpu;
459 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
460 EPAIR_LOCK(epair_dpcpu);
461 epair_start_locked(ifp);
462 EPAIR_UNLOCK(epair_dpcpu);
466 epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
468 struct epair_dpcpu *epair_dpcpu;
469 struct epair_softc *sc;
474 DPRINTF("ifp=%p m=%p\n", ifp, m);
476 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
477 EPAIR_LOCK_ASSERT(epair_dpcpu);
483 * We are not going to use the interface en/dequeue mechanism
484 * on the TX side. We are called from ether_output_frame()
485 * and will put the packet into the incoming queue of the
486 * other interface of our pair via the netsir.
488 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
492 if ((ifp->if_flags & IFF_UP) == 0) {
500 * In case the outgoing interface is not usable,
504 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
505 (oifp->if_flags & IFF_UP) ==0) {
510 len = m->m_pkthdr.len;
512 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
515 /* Support ALTQ via the clasic if_start() path. */
516 IF_LOCK(&ifp->if_snd);
517 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
518 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
520 ifp->if_snd.ifq_drops++;
521 IF_UNLOCK(&ifp->if_snd);
523 ifp->if_obytes += len;
524 if (mflags & (M_BCAST|M_MCAST))
527 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
528 epair_start_locked(ifp);
530 (void)epair_add_ifp_for_draining(ifp);
534 IF_UNLOCK(&ifp->if_snd);
537 if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
539 * Our hardware queue is full, try to fall back
540 * queuing to the ifq but do not call ifp->if_start.
541 * Either we are lucky or the packet is gone.
543 IFQ_ENQUEUE(&ifp->if_snd, m, error);
545 (void)epair_add_ifp_for_draining(ifp);
550 * Add a reference so the interface cannot go while the
551 * packet is in transit as we rely on rcvif to stay valid.
553 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
554 m->m_pkthdr.rcvif = oifp;
555 CURVNET_SET_QUIET(oifp->if_vnet);
556 error = netisr_queue(NETISR_EPAIR, m);
561 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
562 * but as we bypass all this we have to duplicate
563 * the logic another time.
565 ifp->if_obytes += len;
566 if (mflags & (M_BCAST|M_MCAST))
568 /* Someone else received the packet. */
571 /* The packet was freed already. */
572 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
573 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
575 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
576 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
577 ("%s: ifp=%p sc->refcount not >= 1: %d",
578 __func__, oifp, sc->refcount));
585 epair_transmit(struct ifnet *ifp, struct mbuf *m)
587 struct epair_dpcpu *epair_dpcpu;
590 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
591 EPAIR_LOCK(epair_dpcpu);
592 error = epair_transmit_locked(ifp, m);
593 EPAIR_UNLOCK(epair_dpcpu);
598 epair_qflush(struct ifnet *ifp)
600 struct epair_softc *sc;
603 KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
606 * Remove this ifp from all backpointer lists. The interface will not
607 * usable for flushing anyway nor should it have anything to flush
610 epair_remove_ifp_from_draining(ifp);
617 epair_media_change(struct ifnet *ifp __unused)
625 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
628 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
629 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
633 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
635 struct epair_softc *sc;
639 ifr = (struct ifreq *)data;
650 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
654 /* We basically allow all kinds of MTUs. */
655 ifp->if_mtu = ifr->ifr_mtu;
660 /* Let the common ethernet handler process this. */
661 error = ether_ioctl(ifp, cmd, data);
669 epair_init(void *dummy __unused)
675 * Interface cloning functions.
676 * We use our private ones so that we can create/destroy our secondary
677 * device along with the primary one.
680 epair_clone_match(struct if_clone *ifc, const char *name)
684 DPRINTF("name='%s'\n", name);
687 * Our base name is epair.
688 * Our interfaces will be named epair<n>[ab].
689 * So accept anything of the following list:
692 * but not the epair<n>[ab] versions.
694 if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
697 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
698 if (*cp < '0' || *cp > '9')
706 epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
708 struct epair_softc *sca, *scb;
711 int error, unit, wildcard;
712 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
715 * We are abusing params to create our second interface.
716 * Actually we already created it and called if_clone_create()
717 * for it to do the official insertion procedure the moment we knew
718 * it cannot fail anymore. So just do attach it here.
721 scb = (struct epair_softc *)params;
723 /* Assign a hopefully unique, locally administered etheraddr. */
725 eaddr[3] = (ifp->if_index >> 8) & 0xff;
726 eaddr[4] = ifp->if_index & 0xff;
728 ether_ifattach(ifp, eaddr);
729 /* Correctly set the name for the cloner list. */
730 strlcpy(name, scb->ifp->if_xname, len);
734 /* Try to see if a special unit was requested. */
735 error = ifc_name2unit(name, &unit);
738 wildcard = (unit < 0);
740 error = ifc_alloc_unit(ifc, &unit);
745 * If no unit had been given, we need to adjust the ifName.
746 * Also make sure there is space for our extra [ab] suffix.
748 for (dp = name; *dp != '\0'; dp++);
750 error = snprintf(dp, len - (dp - name), "%d", unit);
751 if (error > len - (dp - name) - 1) {
752 /* ifName too long. */
753 ifc_free_unit(ifc, unit);
758 if (len - (dp - name) - 1 < 1) {
759 /* No space left for our [ab] suffix. */
760 ifc_free_unit(ifc, unit);
764 /* Must not change dp so we can replace 'a' by 'b' later. */
767 /* Check if 'a' and 'b' interfaces already exist. */
768 if (ifunit(name) != NULL)
771 if (ifunit(name) != NULL)
774 /* Allocate memory for both [ab] interfaces */
775 sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
776 EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
777 sca->ifp = if_alloc(IFT_ETHER);
778 if (sca->ifp == NULL) {
780 ifc_free_unit(ifc, unit);
784 scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
785 EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
786 scb->ifp = if_alloc(IFT_ETHER);
787 if (scb->ifp == NULL) {
791 ifc_free_unit(ifc, unit);
796 * Cross-reference the interfaces so we will be able to free both.
798 sca->oifp = scb->ifp;
799 scb->oifp = sca->ifp;
802 * Calculate the cpuid for netisr queueing based on the
803 * ifIndex of the interfaces. As long as we cannot configure
804 * this or use cpuset information easily we cannot guarantee
805 * cache locality but we can at least allow parallelism.
808 netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
810 netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
812 /* Initialise pseudo media types. */
813 ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
814 ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
815 ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
816 ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
817 ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
818 ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
820 /* Finish initialization of interface <n>a. */
823 strlcpy(ifp->if_xname, name, IFNAMSIZ);
824 ifp->if_dname = epairname;
825 ifp->if_dunit = unit;
826 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
827 ifp->if_capabilities = IFCAP_VLAN_MTU;
828 ifp->if_capenable = IFCAP_VLAN_MTU;
829 ifp->if_start = epair_start;
830 ifp->if_ioctl = epair_ioctl;
831 ifp->if_init = epair_init;
832 ifp->if_snd.ifq_maxlen = ifqmaxlen;
833 /* Assign a hopefully unique, locally administered etheraddr. */
835 eaddr[3] = (ifp->if_index >> 8) & 0xff;
836 eaddr[4] = ifp->if_index & 0xff;
838 ether_ifattach(ifp, eaddr);
839 sca->if_qflush = ifp->if_qflush;
840 ifp->if_qflush = epair_qflush;
841 ifp->if_transmit = epair_transmit;
842 if_initbaudrate(ifp, IF_Gbps(10)); /* arbitrary maximum */
844 /* Swap the name and finish initialization of interface <n>b. */
849 strlcpy(ifp->if_xname, name, IFNAMSIZ);
850 ifp->if_dname = epairname;
851 ifp->if_dunit = unit;
852 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
853 ifp->if_capabilities = IFCAP_VLAN_MTU;
854 ifp->if_capenable = IFCAP_VLAN_MTU;
855 ifp->if_start = epair_start;
856 ifp->if_ioctl = epair_ioctl;
857 ifp->if_init = epair_init;
858 ifp->if_snd.ifq_maxlen = ifqmaxlen;
859 /* We need to play some tricks here for the second interface. */
860 strlcpy(name, epairname, len);
861 error = if_clone_create(name, len, (caddr_t)scb);
863 panic("%s: if_clone_create() for our 2nd iface failed: %d",
865 scb->if_qflush = ifp->if_qflush;
866 ifp->if_qflush = epair_qflush;
867 ifp->if_transmit = epair_transmit;
868 if_initbaudrate(ifp, IF_Gbps(10)); /* arbitrary maximum */
871 * Restore name to <n>a as the ifp for this will go into the
872 * cloner list for the initial call.
874 strlcpy(name, sca->ifp->if_xname, len);
875 DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
877 /* Tell the world, that we are ready to rock. */
878 sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
879 scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
880 if_link_state_change(sca->ifp, LINK_STATE_UP);
881 if_link_state_change(scb->ifp, LINK_STATE_UP);
887 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
890 struct epair_softc *sca, *scb;
893 DPRINTF("ifp=%p\n", ifp);
896 * In case we called into if_clone_destroyif() ourselves
897 * again to remove the second interface, the softc will be
898 * NULL. In that case so not do anything but return success.
900 if (ifp->if_softc == NULL)
903 unit = ifp->if_dunit;
906 scb = oifp->if_softc;
908 DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
909 if_link_state_change(ifp, LINK_STATE_DOWN);
910 if_link_state_change(oifp, LINK_STATE_DOWN);
911 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
912 oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
915 * Get rid of our second half. As the other of the two
916 * interfaces may reside in a different vnet, we need to
917 * switch before freeing them.
919 CURVNET_SET_QUIET(oifp->if_vnet);
920 ether_ifdetach(oifp);
922 * Wait for all packets to be dispatched to if_input.
923 * The numbers can only go down as the interface is
924 * detached so there is no need to use atomics.
926 DPRINTF("scb refcnt=%u\n", scb->refcount);
927 EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
928 ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
929 oifp->if_softc = NULL;
930 error = if_clone_destroyif(ifc, oifp);
932 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
935 ifmedia_removeall(&scb->media);
941 * Wait for all packets to be dispatched to if_input.
943 DPRINTF("sca refcnt=%u\n", sca->refcount);
944 EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
945 ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
947 ifmedia_removeall(&sca->media);
949 ifc_free_unit(ifc, unit);
955 vnet_epair_init(const void *unused __unused)
958 V_epair_cloner = if_clone_advanced(epairname, 0,
959 epair_clone_match, epair_clone_create, epair_clone_destroy);
961 VNET_SYSINIT(vnet_epair_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
962 vnet_epair_init, NULL);
965 vnet_epair_uninit(const void *unused __unused)
968 if_clone_detach(V_epair_cloner);
970 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
971 vnet_epair_uninit, NULL);
974 epair_modevent(module_t mod, int type, void *data)
980 /* For now limit us to one global mutex and one inq. */
982 epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
983 if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
984 epair_nh.nh_qlimit = qlimit;
985 netisr_register(&epair_nh);
987 printf("%s initialized.\n", epairname);
990 netisr_unregister(&epair_nh);
991 epair_dpcpu_detach();
993 printf("%s unloaded.\n", epairname);
1001 static moduledata_t epair_mod = {
1007 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1008 MODULE_VERSION(if_epair, 1);