1194927Sbz/*-
2194927Sbz * Copyright (c) 2008 The FreeBSD Foundation
3204805Sbz * Copyright (c) 2009-2010 Bjoern A. Zeeb <bz@FreeBSD.org>
4194927Sbz * All rights reserved.
5194927Sbz *
6194927Sbz * This software was developed by CK Software GmbH under sponsorship
7194927Sbz * from the FreeBSD Foundation.
8194927Sbz *
9194927Sbz * Redistribution and use in source and binary forms, with or without
10194927Sbz * modification, are permitted provided that the following conditions
11194927Sbz * are met:
12194927Sbz * 1. Redistributions of source code must retain the above copyright
13194927Sbz * notice, this list of conditions and the following disclaimer.
14194927Sbz * 2. Redistributions in binary form must reproduce the above copyright
15194927Sbz * notice, this list of conditions and the following disclaimer in the
16194927Sbz * documentation and/or other materials provided with the distribution.
17194927Sbz *
18194927Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19194927Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20194927Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21194927Sbz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22194927Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23194927Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24194927Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25194927Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26194927Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27194927Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28194927Sbz * SUCH DAMAGE.
29194927Sbz */
30194927Sbz
31194927Sbz/*
32195892Sbz * A pair of virtual back-to-back connected ethernet like interfaces
33195892Sbz * (``two interfaces with a virtual cross-over cable'').
34195892Sbz *
35194927Sbz * This is mostly intended to be used to provide connectivity between
36194927Sbz * different virtual network stack instances.
37194927Sbz */
38194927Sbz/*
39194927Sbz * Things to re-think once we have more experience:
40195892Sbz * - ifp->if_reassign function once we can test with vimage. Depending on
41196019Srwatson *   how if_vmove() is going to be improved.
42195892Sbz * - Real random etheraddrs that are checked to be uniquish; we would need
43195892Sbz *   to re-do them in case we move the interface between network stacks
44195892Sbz *   in a private if_reassign function.
45195892Sbz *   In case we bridge to a real interface/network or between indepedent
46195892Sbz *   epairs on multiple stacks/machines, we may need this.
47195892Sbz *   For now let the user handle that case.
48194927Sbz */
49194927Sbz
50194927Sbz#include <sys/cdefs.h>
51194927Sbz__FBSDID("$FreeBSD$");
52194927Sbz
53194927Sbz#include <sys/param.h>
54194927Sbz#include <sys/kernel.h>
55194927Sbz#include <sys/mbuf.h>
56194927Sbz#include <sys/module.h>
57194927Sbz#include <sys/refcount.h>
58194927Sbz#include <sys/queue.h>
59195892Sbz#include <sys/smp.h>
60194927Sbz#include <sys/socket.h>
61194927Sbz#include <sys/sockio.h>
62194927Sbz#include <sys/sysctl.h>
63194927Sbz#include <sys/types.h>
64194927Sbz
65194927Sbz#include <net/bpf.h>
66194927Sbz#include <net/ethernet.h>
67194927Sbz#include <net/if.h>
68194927Sbz#include <net/if_clone.h>
69222246Szec#include <net/if_media.h>
70194927Sbz#include <net/if_var.h>
71194927Sbz#include <net/if_types.h>
72194927Sbz#include <net/netisr.h>
73196019Srwatson#include <net/vnet.h>
74194927Sbz
75194927SbzSYSCTL_DECL(_net_link);
76227309Sedstatic SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
77195892Sbz
78195892Sbz#ifdef EPAIR_DEBUG
79195892Sbzstatic int epair_debug = 0;
80215641SbzSYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
81194927Sbz    &epair_debug, 0, "if_epair(4) debugging.");
82195892Sbz#define	DPRINTF(fmt, arg...)						\
83195892Sbz	if (epair_debug)						\
84195892Sbz		printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
85194927Sbz#else
86194927Sbz#define	DPRINTF(fmt, arg...)
87194927Sbz#endif
88194927Sbz
89195892Sbzstatic void epair_nh_sintr(struct mbuf *);
90195892Sbzstatic struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
91195892Sbzstatic void epair_nh_drainedcpu(u_int);
92195892Sbz
93195892Sbzstatic void epair_start_locked(struct ifnet *);
94222246Szecstatic int epair_media_change(struct ifnet *);
95222246Szecstatic void epair_media_status(struct ifnet *, struct ifmediareq *);
96195892Sbz
97195892Sbzstatic int epair_clone_match(struct if_clone *, const char *);
98195892Sbzstatic int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
99195892Sbzstatic int epair_clone_destroy(struct if_clone *, struct ifnet *);
100195892Sbz
101241610Sglebiusstatic const char epairname[] = "epair";
102241610Sglebius
103195892Sbz/* Netisr realted definitions and sysctl. */
104195892Sbzstatic struct netisr_handler epair_nh = {
105241610Sglebius	.nh_name	= epairname,
106195892Sbz	.nh_proto	= NETISR_EPAIR,
107195892Sbz	.nh_policy	= NETISR_POLICY_CPU,
108195892Sbz	.nh_handler	= epair_nh_sintr,
109195892Sbz	.nh_m2cpuid	= epair_nh_m2cpuid,
110195892Sbz	.nh_drainedcpu	= epair_nh_drainedcpu,
111195892Sbz};
112195892Sbz
113195892Sbzstatic int
114195892Sbzsysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
115195892Sbz{
116195892Sbz	int error, qlimit;
117195892Sbz
118195892Sbz	netisr_getqlimit(&epair_nh, &qlimit);
119195892Sbz	error = sysctl_handle_int(oidp, &qlimit, 0, req);
120195892Sbz	if (error || !req->newptr)
121195892Sbz		return (error);
122195892Sbz	if (qlimit < 1)
123195892Sbz		return (EINVAL);
124195892Sbz	return (netisr_setqlimit(&epair_nh, qlimit));
125195892Sbz}
126195892SbzSYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
127195892Sbz    0, 0, sysctl_epair_netisr_maxqlen, "I",
128195892Sbz    "Maximum if_epair(4) netisr \"hw\" queue length");
129195892Sbz
130194927Sbzstruct epair_softc {
131195892Sbz	struct ifnet	*ifp;		/* This ifp. */
132195892Sbz	struct ifnet	*oifp;		/* other ifp of pair. */
133222246Szec	struct ifmedia	media;		/* Media config (fake). */
134195892Sbz	u_int		refcount;	/* # of mbufs in flight. */
135195892Sbz	u_int		cpuid;		/* CPU ID assigned upon creation. */
136194927Sbz	void		(*if_qflush)(struct ifnet *);
137195892Sbz					/* Original if_qflush routine. */
138194927Sbz};
139194927Sbz
140195892Sbz/*
141195892Sbz * Per-CPU list of ifps with data in the ifq that needs to be flushed
142195892Sbz * to the netisr ``hw'' queue before we allow any further direct queuing
143195892Sbz * to the ``hw'' queue.
144195892Sbz */
145194927Sbzstruct epair_ifp_drain {
146194927Sbz	STAILQ_ENTRY(epair_ifp_drain)	ifp_next;
147194927Sbz	struct ifnet			*ifp;
148194927Sbz};
149195892SbzSTAILQ_HEAD(eid_list, epair_ifp_drain);
150194927Sbz
151195892Sbz#define	EPAIR_LOCK_INIT(dpcpu)		mtx_init(&(dpcpu)->if_epair_mtx, \
152195892Sbz					    "if_epair", NULL, MTX_DEF)
153195892Sbz#define	EPAIR_LOCK_DESTROY(dpcpu)	mtx_destroy(&(dpcpu)->if_epair_mtx)
154195892Sbz#define	EPAIR_LOCK_ASSERT(dpcpu)	mtx_assert(&(dpcpu)->if_epair_mtx, \
155195892Sbz					    MA_OWNED)
156195892Sbz#define	EPAIR_LOCK(dpcpu)		mtx_lock(&(dpcpu)->if_epair_mtx)
157195892Sbz#define	EPAIR_UNLOCK(dpcpu)		mtx_unlock(&(dpcpu)->if_epair_mtx)
158194927Sbz
159195892Sbz#ifdef INVARIANTS
160195892Sbz#define	EPAIR_REFCOUNT_INIT(r, v)	refcount_init((r), (v))
161195892Sbz#define	EPAIR_REFCOUNT_AQUIRE(r)	refcount_acquire((r))
162195892Sbz#define	EPAIR_REFCOUNT_RELEASE(r)	refcount_release((r))
163195892Sbz#define	EPAIR_REFCOUNT_ASSERT(a, p)	KASSERT(a, p)
164195892Sbz#else
165195892Sbz#define	EPAIR_REFCOUNT_INIT(r, v)
166195892Sbz#define	EPAIR_REFCOUNT_AQUIRE(r)
167195892Sbz#define	EPAIR_REFCOUNT_RELEASE(r)
168195892Sbz#define	EPAIR_REFCOUNT_ASSERT(a, p)
169195892Sbz#endif
170194927Sbz
171241610Sglebiusstatic MALLOC_DEFINE(M_EPAIR, epairname,
172194927Sbz    "Pair of virtual cross-over connected Ethernet-like interfaces");
173194927Sbz
174241610Sglebiusstatic struct if_clone *epair_cloner;
175194927Sbz
176195892Sbz/*
177195892Sbz * DPCPU area and functions.
178195892Sbz */
179195892Sbzstruct epair_dpcpu {
180195892Sbz	struct mtx	if_epair_mtx;		/* Per-CPU locking. */
181195892Sbz	int		epair_drv_flags;	/* Per-CPU ``hw'' drv flags. */
182195892Sbz	struct eid_list	epair_ifp_drain_list;	/* Per-CPU list of ifps with
183195892Sbz						 * data in the ifq. */
184195892Sbz};
185195892SbzDPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
186194927Sbz
187195892Sbzstatic void
188195892Sbzepair_dpcpu_init(void)
189195892Sbz{
190195892Sbz	struct epair_dpcpu *epair_dpcpu;
191195892Sbz	struct eid_list *s;
192195892Sbz	u_int cpuid;
193195892Sbz
194209059Sjhb	CPU_FOREACH(cpuid) {
195195892Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
196195892Sbz
197195892Sbz		/* Initialize per-cpu lock. */
198195892Sbz		EPAIR_LOCK_INIT(epair_dpcpu);
199195892Sbz
200195892Sbz		/* Driver flags are per-cpu as are our netisr "hw" queues. */
201195892Sbz		epair_dpcpu->epair_drv_flags = 0;
202195892Sbz
203195892Sbz		/*
204195892Sbz		 * Initialize per-cpu drain list.
205195892Sbz		 * Manually do what STAILQ_HEAD_INITIALIZER would do.
206195892Sbz		 */
207195892Sbz		s = &epair_dpcpu->epair_ifp_drain_list;
208195892Sbz		s->stqh_first = NULL;
209195892Sbz		s->stqh_last = &s->stqh_first;
210195892Sbz	}
211195892Sbz}
212195892Sbz
213195892Sbzstatic void
214195892Sbzepair_dpcpu_detach(void)
215195892Sbz{
216195892Sbz	struct epair_dpcpu *epair_dpcpu;
217195892Sbz	u_int cpuid;
218195892Sbz
219209059Sjhb	CPU_FOREACH(cpuid) {
220195892Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
221195892Sbz
222195892Sbz		/* Destroy per-cpu lock. */
223195892Sbz		EPAIR_LOCK_DESTROY(epair_dpcpu);
224195892Sbz	}
225195892Sbz}
226195892Sbz
227194927Sbz/*
228195892Sbz * Helper functions.
229195892Sbz */
230195892Sbzstatic u_int
231195892Sbzcpuid_from_ifp(struct ifnet *ifp)
232195892Sbz{
233195892Sbz	struct epair_softc *sc;
234195892Sbz
235195892Sbz	if (ifp == NULL)
236195892Sbz		return (0);
237195892Sbz	sc = ifp->if_softc;
238195892Sbz
239195892Sbz	return (sc->cpuid);
240195892Sbz}
241195892Sbz
242195892Sbz/*
243194927Sbz * Netisr handler functions.
244194927Sbz */
245194927Sbzstatic void
246195892Sbzepair_nh_sintr(struct mbuf *m)
247194927Sbz{
248194927Sbz	struct ifnet *ifp;
249194927Sbz	struct epair_softc *sc;
250194927Sbz
251194927Sbz	ifp = m->m_pkthdr.rcvif;
252194927Sbz	(*ifp->if_input)(ifp, m);
253194927Sbz	sc = ifp->if_softc;
254195892Sbz	EPAIR_REFCOUNT_RELEASE(&sc->refcount);
255204805Sbz	EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
256204805Sbz	    ("%s: ifp=%p sc->refcount not >= 1: %d",
257204805Sbz	    __func__, ifp, sc->refcount));
258194927Sbz	DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
259194927Sbz}
260194927Sbz
261195892Sbzstatic struct mbuf *
262195892Sbzepair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
263195892Sbz{
264195892Sbz
265195892Sbz	*cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
266195892Sbz
267195892Sbz	return (m);
268195892Sbz}
269195892Sbz
270194927Sbzstatic void
271195892Sbzepair_nh_drainedcpu(u_int cpuid)
272194927Sbz{
273195892Sbz	struct epair_dpcpu *epair_dpcpu;
274194927Sbz	struct epair_ifp_drain *elm, *tvar;
275194927Sbz	struct ifnet *ifp;
276194927Sbz
277195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
278195892Sbz	EPAIR_LOCK(epair_dpcpu);
279194927Sbz	/*
280194927Sbz	 * Assume our "hw" queue and possibly ifq will be emptied
281194927Sbz	 * again. In case we will overflow the "hw" queue while
282194927Sbz	 * draining, epair_start_locked will set IFF_DRV_OACTIVE
283194927Sbz	 * again and we will stop and return.
284194927Sbz	 */
285195892Sbz	STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
286195892Sbz	    ifp_next, tvar) {
287194927Sbz		ifp = elm->ifp;
288195892Sbz		epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
289194927Sbz		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
290194927Sbz		epair_start_locked(ifp);
291194927Sbz
292194927Sbz		IFQ_LOCK(&ifp->if_snd);
293194927Sbz		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
294204805Sbz			struct epair_softc *sc;
295204805Sbz
296195892Sbz			STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
297195892Sbz			    elm, epair_ifp_drain, ifp_next);
298204805Sbz			/* The cached ifp goes off the list. */
299204805Sbz			sc = ifp->if_softc;
300204805Sbz			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
301204805Sbz			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
302204805Sbz			    ("%s: ifp=%p sc->refcount not >= 1: %d",
303204805Sbz			    __func__, ifp, sc->refcount));
304194927Sbz			free(elm, M_EPAIR);
305194927Sbz		}
306194927Sbz		IFQ_UNLOCK(&ifp->if_snd);
307194927Sbz
308194927Sbz		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
309194927Sbz			/* Our "hw"q overflew again. */
310215641Sbz			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
311194927Sbz			DPRINTF("hw queue length overflow at %u\n",
312195892Sbz			    epair_nh.nh_qlimit);
313194927Sbz			break;
314194927Sbz		}
315194927Sbz	}
316195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
317194927Sbz}
318194927Sbz
319194927Sbz/*
320194927Sbz * Network interface (`if') related functions.
321194927Sbz */
322204805Sbzstatic void
323204805Sbzepair_remove_ifp_from_draining(struct ifnet *ifp)
324204805Sbz{
325204805Sbz	struct epair_dpcpu *epair_dpcpu;
326204805Sbz	struct epair_ifp_drain *elm, *tvar;
327204805Sbz	u_int cpuid;
328204805Sbz
329209059Sjhb	CPU_FOREACH(cpuid) {
330204805Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
331204805Sbz		EPAIR_LOCK(epair_dpcpu);
332204805Sbz		STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
333204805Sbz		    ifp_next, tvar) {
334204805Sbz			if (ifp == elm->ifp) {
335204805Sbz				struct epair_softc *sc;
336204805Sbz
337204805Sbz				STAILQ_REMOVE(
338204805Sbz				    &epair_dpcpu->epair_ifp_drain_list, elm,
339204805Sbz				    epair_ifp_drain, ifp_next);
340204805Sbz				/* The cached ifp goes off the list. */
341204805Sbz				sc = ifp->if_softc;
342204805Sbz				EPAIR_REFCOUNT_RELEASE(&sc->refcount);
343204805Sbz				EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
344204805Sbz				    ("%s: ifp=%p sc->refcount not >= 1: %d",
345204805Sbz				    __func__, ifp, sc->refcount));
346204805Sbz				free(elm, M_EPAIR);
347204805Sbz			}
348204805Sbz		}
349204805Sbz		EPAIR_UNLOCK(epair_dpcpu);
350204805Sbz	}
351204805Sbz}
352204805Sbz
353195892Sbzstatic int
354195892Sbzepair_add_ifp_for_draining(struct ifnet *ifp)
355195892Sbz{
356195892Sbz	struct epair_dpcpu *epair_dpcpu;
357204805Sbz	struct epair_softc *sc;
358195892Sbz	struct epair_ifp_drain *elm = NULL;
359195892Sbz
360204805Sbz	sc = ifp->if_softc;
361195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
362204805Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
363195892Sbz	STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
364195892Sbz		if (elm->ifp == ifp)
365195892Sbz			break;
366201995Sbz	/* If the ifp is there already, return success. */
367195892Sbz	if (elm != NULL)
368195892Sbz		return (0);
369195892Sbz
370195892Sbz	elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
371195892Sbz	if (elm == NULL)
372195892Sbz		return (ENOMEM);
373195892Sbz
374195892Sbz	elm->ifp = ifp;
375204805Sbz	/* Add a reference for the ifp pointer on the list. */
376204805Sbz	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
377195892Sbz	STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
378195892Sbz
379195892Sbz	return (0);
380195892Sbz}
381195892Sbz
382194927Sbzstatic void
383194927Sbzepair_start_locked(struct ifnet *ifp)
384194927Sbz{
385195892Sbz	struct epair_dpcpu *epair_dpcpu;
386194927Sbz	struct mbuf *m;
387194927Sbz	struct epair_softc *sc;
388194927Sbz	struct ifnet *oifp;
389194927Sbz	int error;
390194927Sbz
391194927Sbz	DPRINTF("ifp=%p\n", ifp);
392195892Sbz	sc = ifp->if_softc;
393195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
394195892Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
395194927Sbz
396194927Sbz	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
397194927Sbz		return;
398194927Sbz	if ((ifp->if_flags & IFF_UP) == 0)
399194927Sbz		return;
400194927Sbz
401194927Sbz	/*
402194927Sbz	 * We get patckets here from ether_output via if_handoff()
403194927Sbz	 * and ned to put them into the input queue of the oifp
404194927Sbz	 * and call oifp->if_input() via netisr/epair_sintr().
405194927Sbz	 */
406194927Sbz	oifp = sc->oifp;
407194927Sbz	sc = oifp->if_softc;
408194927Sbz	for (;;) {
409194927Sbz		IFQ_DEQUEUE(&ifp->if_snd, m);
410194927Sbz		if (m == NULL)
411194927Sbz			break;
412194927Sbz		BPF_MTAP(ifp, m);
413194927Sbz
414194927Sbz		/*
415194927Sbz		 * In case the outgoing interface is not usable,
416194927Sbz		 * drop the packet.
417194927Sbz		 */
418194927Sbz		if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
419194927Sbz		    (oifp->if_flags & IFF_UP) ==0) {
420194927Sbz			ifp->if_oerrors++;
421194927Sbz			m_freem(m);
422194927Sbz			continue;
423194927Sbz		}
424194927Sbz		DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
425194927Sbz
426194927Sbz		/*
427194927Sbz		 * Add a reference so the interface cannot go while the
428194927Sbz		 * packet is in transit as we rely on rcvif to stay valid.
429194927Sbz		 */
430195892Sbz		EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
431194927Sbz		m->m_pkthdr.rcvif = oifp;
432194927Sbz		CURVNET_SET_QUIET(oifp->if_vnet);
433194927Sbz		error = netisr_queue(NETISR_EPAIR, m);
434194927Sbz		CURVNET_RESTORE();
435194927Sbz		if (!error) {
436194927Sbz			ifp->if_opackets++;
437194927Sbz			/* Someone else received the packet. */
438194927Sbz			oifp->if_ipackets++;
439194927Sbz		} else {
440204805Sbz			/* The packet was freed already. */
441195892Sbz			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
442194927Sbz			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
443204805Sbz			(void) epair_add_ifp_for_draining(ifp);
444204805Sbz			ifp->if_oerrors++;
445195892Sbz			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
446204805Sbz			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
447204805Sbz			    ("%s: ifp=%p sc->refcount not >= 1: %d",
448204805Sbz			    __func__, oifp, sc->refcount));
449194927Sbz		}
450194927Sbz	}
451194927Sbz}
452194927Sbz
453194927Sbzstatic void
454194927Sbzepair_start(struct ifnet *ifp)
455194927Sbz{
456195892Sbz	struct epair_dpcpu *epair_dpcpu;
457194927Sbz
458195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
459195892Sbz	EPAIR_LOCK(epair_dpcpu);
460194927Sbz	epair_start_locked(ifp);
461195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
462194927Sbz}
463194927Sbz
464194927Sbzstatic int
465194927Sbzepair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
466194927Sbz{
467195892Sbz	struct epair_dpcpu *epair_dpcpu;
468194927Sbz	struct epair_softc *sc;
469194927Sbz	struct ifnet *oifp;
470194927Sbz	int error, len;
471194927Sbz	short mflags;
472194927Sbz
473194927Sbz	DPRINTF("ifp=%p m=%p\n", ifp, m);
474195892Sbz	sc = ifp->if_softc;
475195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
476195892Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
477194927Sbz
478194927Sbz	if (m == NULL)
479194927Sbz		return (0);
480194927Sbz
481194927Sbz	/*
482194927Sbz	 * We are not going to use the interface en/dequeue mechanism
483194927Sbz	 * on the TX side. We are called from ether_output_frame()
484194927Sbz	 * and will put the packet into the incoming queue of the
485194927Sbz	 * other interface of our pair via the netsir.
486194927Sbz	 */
487194927Sbz	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
488194927Sbz		m_freem(m);
489194927Sbz		return (ENXIO);
490194927Sbz	}
491194927Sbz	if ((ifp->if_flags & IFF_UP) == 0) {
492194927Sbz		m_freem(m);
493194927Sbz		return (ENETDOWN);
494194927Sbz	}
495194927Sbz
496194927Sbz	BPF_MTAP(ifp, m);
497194927Sbz
498194927Sbz	/*
499194927Sbz	 * In case the outgoing interface is not usable,
500194927Sbz	 * drop the packet.
501194927Sbz	 */
502194927Sbz	oifp = sc->oifp;
503194927Sbz	if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
504194927Sbz	    (oifp->if_flags & IFF_UP) ==0) {
505194927Sbz		ifp->if_oerrors++;
506194927Sbz		m_freem(m);
507194927Sbz		return (0);
508194927Sbz	}
509194927Sbz	len = m->m_pkthdr.len;
510194927Sbz	mflags = m->m_flags;
511194927Sbz	DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
512194927Sbz
513194927Sbz#ifdef ALTQ
514194927Sbz	/* Support ALTQ via the clasic if_start() path. */
515194927Sbz	IF_LOCK(&ifp->if_snd);
516194927Sbz	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
517194927Sbz		ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
518194927Sbz		if (error)
519194927Sbz			ifp->if_snd.ifq_drops++;
520194927Sbz		IF_UNLOCK(&ifp->if_snd);
521194927Sbz		if (!error) {
522194927Sbz			ifp->if_obytes += len;
523194927Sbz			if (mflags & (M_BCAST|M_MCAST))
524194927Sbz				ifp->if_omcasts++;
525194927Sbz
526194927Sbz			if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
527194927Sbz				epair_start_locked(ifp);
528194927Sbz			else
529195892Sbz				(void)epair_add_ifp_for_draining(ifp);
530194927Sbz		}
531194927Sbz		return (error);
532194927Sbz	}
533194927Sbz	IF_UNLOCK(&ifp->if_snd);
534194927Sbz#endif
535194927Sbz
536195892Sbz	if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
537194927Sbz		/*
538194927Sbz		 * Our hardware queue is full, try to fall back
539194927Sbz		 * queuing to the ifq but do not call ifp->if_start.
540194927Sbz		 * Either we are lucky or the packet is gone.
541194927Sbz		 */
542194927Sbz		IFQ_ENQUEUE(&ifp->if_snd, m, error);
543194927Sbz		if (!error)
544195892Sbz			(void)epair_add_ifp_for_draining(ifp);
545194927Sbz		return (error);
546194927Sbz	}
547194927Sbz	sc = oifp->if_softc;
548194927Sbz	/*
549194927Sbz	 * Add a reference so the interface cannot go while the
550194927Sbz	 * packet is in transit as we rely on rcvif to stay valid.
551194927Sbz	 */
552195892Sbz	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
553194927Sbz	m->m_pkthdr.rcvif = oifp;
554194927Sbz	CURVNET_SET_QUIET(oifp->if_vnet);
555194927Sbz	error = netisr_queue(NETISR_EPAIR, m);
556194927Sbz	CURVNET_RESTORE();
557194927Sbz	if (!error) {
558194927Sbz		ifp->if_opackets++;
559194927Sbz		/*
560194927Sbz		 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
561194927Sbz		 * but as we bypass all this we have to duplicate
562194927Sbz		 * the logic another time.
563194927Sbz		 */
564194927Sbz		ifp->if_obytes += len;
565194927Sbz		if (mflags & (M_BCAST|M_MCAST))
566194927Sbz			ifp->if_omcasts++;
567194927Sbz		/* Someone else received the packet. */
568194927Sbz		oifp->if_ipackets++;
569194927Sbz	} else {
570194927Sbz		/* The packet was freed already. */
571195892Sbz		epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
572194927Sbz		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
573204805Sbz		ifp->if_oerrors++;
574204805Sbz		EPAIR_REFCOUNT_RELEASE(&sc->refcount);
575204805Sbz		EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
576204805Sbz		    ("%s: ifp=%p sc->refcount not >= 1: %d",
577204805Sbz		    __func__, oifp, sc->refcount));
578194927Sbz	}
579194927Sbz
580194927Sbz	return (error);
581194927Sbz}
582194927Sbz
583194927Sbzstatic int
584194927Sbzepair_transmit(struct ifnet *ifp, struct mbuf *m)
585194927Sbz{
586195892Sbz	struct epair_dpcpu *epair_dpcpu;
587194927Sbz	int error;
588194927Sbz
589195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
590195892Sbz	EPAIR_LOCK(epair_dpcpu);
591194927Sbz	error = epair_transmit_locked(ifp, m);
592195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
593194927Sbz	return (error);
594194927Sbz}
595194927Sbz
596194927Sbzstatic void
597194927Sbzepair_qflush(struct ifnet *ifp)
598194927Sbz{
599194927Sbz	struct epair_softc *sc;
600194927Sbz
601194927Sbz	sc = ifp->if_softc;
602204805Sbz	KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
603204805Sbz	    __func__, ifp, sc));
604194927Sbz	/*
605204805Sbz	 * Remove this ifp from all backpointer lists. The interface will not
606204805Sbz	 * usable for flushing anyway nor should it have anything to flush
607204805Sbz	 * after if_qflush().
608194927Sbz	 */
609204805Sbz	epair_remove_ifp_from_draining(ifp);
610204805Sbz
611194927Sbz	if (sc->if_qflush)
612194927Sbz		sc->if_qflush(ifp);
613194927Sbz}
614194927Sbz
615194927Sbzstatic int
616222246Szecepair_media_change(struct ifnet *ifp __unused)
617222246Szec{
618222246Szec
619222246Szec	/* Do nothing. */
620222246Szec	return (0);
621222246Szec}
622222246Szec
623222246Szecstatic void
624222246Szecepair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
625222246Szec{
626222246Szec
627222246Szec	imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
628222246Szec	imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
629222246Szec}
630222246Szec
631222246Szecstatic int
632194927Sbzepair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
633194927Sbz{
634222246Szec	struct epair_softc *sc;
635194927Sbz	struct ifreq *ifr;
636194927Sbz	int error;
637194927Sbz
638194927Sbz	ifr = (struct ifreq *)data;
639194927Sbz	switch (cmd) {
640194927Sbz	case SIOCSIFFLAGS:
641194927Sbz	case SIOCADDMULTI:
642194927Sbz	case SIOCDELMULTI:
643194927Sbz		error = 0;
644194927Sbz		break;
645194927Sbz
646222246Szec	case SIOCSIFMEDIA:
647222246Szec	case SIOCGIFMEDIA:
648222246Szec		sc = ifp->if_softc;
649222246Szec		error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
650222246Szec		break;
651222246Szec
652195892Sbz	case SIOCSIFMTU:
653195892Sbz		/* We basically allow all kinds of MTUs. */
654195892Sbz		ifp->if_mtu = ifr->ifr_mtu;
655195892Sbz		error = 0;
656195892Sbz		break;
657195892Sbz
658194927Sbz	default:
659194927Sbz		/* Let the common ethernet handler process this. */
660194927Sbz		error = ether_ioctl(ifp, cmd, data);
661194927Sbz		break;
662194927Sbz	}
663194927Sbz
664194927Sbz	return (error);
665194927Sbz}
666194927Sbz
667194927Sbzstatic void
668194927Sbzepair_init(void *dummy __unused)
669194927Sbz{
670194927Sbz}
671194927Sbz
672194927Sbz
673194927Sbz/*
674194927Sbz * Interface cloning functions.
675194927Sbz * We use our private ones so that we can create/destroy our secondary
676194927Sbz * device along with the primary one.
677194927Sbz */
678194927Sbzstatic int
679194927Sbzepair_clone_match(struct if_clone *ifc, const char *name)
680194927Sbz{
681194927Sbz	const char *cp;
682194927Sbz
683194927Sbz	DPRINTF("name='%s'\n", name);
684194927Sbz
685194927Sbz	/*
686194927Sbz	 * Our base name is epair.
687194927Sbz	 * Our interfaces will be named epair<n>[ab].
688194927Sbz	 * So accept anything of the following list:
689194927Sbz	 * - epair
690194927Sbz	 * - epair<n>
691194927Sbz	 * but not the epair<n>[ab] versions.
692194927Sbz	 */
693241610Sglebius	if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
694194927Sbz		return (0);
695194927Sbz
696241610Sglebius	for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
697194927Sbz		if (*cp < '0' || *cp > '9')
698194927Sbz			return (0);
699194927Sbz	}
700194927Sbz
701194927Sbz	return (1);
702194927Sbz}
703194927Sbz
704194927Sbzstatic int
705194927Sbzepair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
706194927Sbz{
707194927Sbz	struct epair_softc *sca, *scb;
708194927Sbz	struct ifnet *ifp;
709194927Sbz	char *dp;
710194927Sbz	int error, unit, wildcard;
711194927Sbz	uint8_t eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
712194927Sbz
713194927Sbz	/*
714194927Sbz	 * We are abusing params to create our second interface.
715241610Sglebius	 * Actually we already created it and called if_clone_create()
716194927Sbz	 * for it to do the official insertion procedure the moment we knew
717194927Sbz	 * it cannot fail anymore. So just do attach it here.
718194927Sbz	 */
719194927Sbz	if (params) {
720194927Sbz		scb = (struct epair_softc *)params;
721194927Sbz		ifp = scb->ifp;
722194927Sbz		/* Assign a hopefully unique, locally administered etheraddr. */
723194927Sbz		eaddr[0] = 0x02;
724194927Sbz		eaddr[3] = (ifp->if_index >> 8) & 0xff;
725194927Sbz		eaddr[4] = ifp->if_index & 0xff;
726194927Sbz		eaddr[5] = 0x0b;
727194927Sbz		ether_ifattach(ifp, eaddr);
728194927Sbz		/* Correctly set the name for the cloner list. */
729194927Sbz		strlcpy(name, scb->ifp->if_xname, len);
730194927Sbz		return (0);
731194927Sbz	}
732194927Sbz
733194927Sbz	/* Try to see if a special unit was requested. */
734194927Sbz	error = ifc_name2unit(name, &unit);
735194927Sbz	if (error != 0)
736194927Sbz		return (error);
737194927Sbz	wildcard = (unit < 0);
738194927Sbz
739194927Sbz	error = ifc_alloc_unit(ifc, &unit);
740194927Sbz	if (error != 0)
741194927Sbz		return (error);
742194927Sbz
743194927Sbz	/*
744194927Sbz	 * If no unit had been given, we need to adjust the ifName.
745194927Sbz	 * Also make sure there is space for our extra [ab] suffix.
746194927Sbz	 */
747194927Sbz	for (dp = name; *dp != '\0'; dp++);
748194927Sbz	if (wildcard) {
749194927Sbz		error = snprintf(dp, len - (dp - name), "%d", unit);
750194927Sbz		if (error > len - (dp - name) - 1) {
751194927Sbz			/* ifName too long. */
752194927Sbz			ifc_free_unit(ifc, unit);
753194927Sbz			return (ENOSPC);
754194927Sbz		}
755194927Sbz		dp += error;
756194927Sbz	}
757194927Sbz	if (len - (dp - name) - 1 < 1) {
758194927Sbz		/* No space left for our [ab] suffix. */
759194927Sbz		ifc_free_unit(ifc, unit);
760194927Sbz		return (ENOSPC);
761194927Sbz	}
762194927Sbz	*dp = 'a';
763194927Sbz	/* Must not change dp so we can replace 'a' by 'b' later. */
764194927Sbz	*(dp+1) = '\0';
765194927Sbz
766194927Sbz	/* Allocate memory for both [ab] interfaces */
767194927Sbz	sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
768195892Sbz	EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
769194927Sbz	sca->ifp = if_alloc(IFT_ETHER);
770194927Sbz	if (sca->ifp == NULL) {
771194927Sbz		free(sca, M_EPAIR);
772194927Sbz		ifc_free_unit(ifc, unit);
773194927Sbz		return (ENOSPC);
774194927Sbz	}
775194927Sbz
776194927Sbz	scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
777195892Sbz	EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
778194927Sbz	scb->ifp = if_alloc(IFT_ETHER);
779194927Sbz	if (scb->ifp == NULL) {
780194927Sbz		free(scb, M_EPAIR);
781194927Sbz		if_free(sca->ifp);
782194927Sbz		free(sca, M_EPAIR);
783194927Sbz		ifc_free_unit(ifc, unit);
784194927Sbz		return (ENOSPC);
785194927Sbz	}
786194927Sbz
787194927Sbz	/*
788194927Sbz	 * Cross-reference the interfaces so we will be able to free both.
789194927Sbz	 */
790194927Sbz	sca->oifp = scb->ifp;
791194927Sbz	scb->oifp = sca->ifp;
792195892Sbz
793195892Sbz	/*
794195892Sbz	 * Calculate the cpuid for netisr queueing based on the
795195892Sbz	 * ifIndex of the interfaces. As long as we cannot configure
796195892Sbz	 * this or use cpuset information easily we cannot guarantee
797195892Sbz	 * cache locality but we can at least allow parallelism.
798195892Sbz	 */
799195892Sbz	sca->cpuid =
800195892Sbz	    netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
801195892Sbz	scb->cpuid =
802195892Sbz	    netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
803194927Sbz
804194927Sbz	/* Finish initialization of interface <n>a. */
805194927Sbz	ifp = sca->ifp;
806194927Sbz	ifp->if_softc = sca;
807194927Sbz	strlcpy(ifp->if_xname, name, IFNAMSIZ);
808241610Sglebius	ifp->if_dname = epairname;
809194927Sbz	ifp->if_dunit = unit;
810194927Sbz	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
811222247Szec	ifp->if_capabilities = IFCAP_VLAN_MTU;
812222247Szec	ifp->if_capenable = IFCAP_VLAN_MTU;
813194927Sbz	ifp->if_start = epair_start;
814194927Sbz	ifp->if_ioctl = epair_ioctl;
815194927Sbz	ifp->if_init  = epair_init;
816194927Sbz	ifp->if_snd.ifq_maxlen = ifqmaxlen;
817194927Sbz	/* Assign a hopefully unique, locally administered etheraddr. */
818194927Sbz	eaddr[0] = 0x02;
819194927Sbz	eaddr[3] = (ifp->if_index >> 8) & 0xff;
820194927Sbz	eaddr[4] = ifp->if_index & 0xff;
821194927Sbz	eaddr[5] = 0x0a;
822194927Sbz	ether_ifattach(ifp, eaddr);
823194927Sbz	sca->if_qflush = ifp->if_qflush;
824194927Sbz	ifp->if_qflush = epair_qflush;
825194927Sbz	ifp->if_transmit = epair_transmit;
826241677Sglebius	if_initbaudrate(ifp, IF_Gbps(10));	/* arbitrary maximum */
827194927Sbz
828194927Sbz	/* Swap the name and finish initialization of interface <n>b. */
829194927Sbz	*dp = 'b';
830194927Sbz
831194927Sbz	ifp = scb->ifp;
832194927Sbz	ifp->if_softc = scb;
833194927Sbz	strlcpy(ifp->if_xname, name, IFNAMSIZ);
834241610Sglebius	ifp->if_dname = epairname;
835194927Sbz	ifp->if_dunit = unit;
836194927Sbz	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
837222247Szec	ifp->if_capabilities = IFCAP_VLAN_MTU;
838222247Szec	ifp->if_capenable = IFCAP_VLAN_MTU;
839194927Sbz	ifp->if_start = epair_start;
840194927Sbz	ifp->if_ioctl = epair_ioctl;
841194927Sbz	ifp->if_init  = epair_init;
842194927Sbz	ifp->if_snd.ifq_maxlen = ifqmaxlen;
843194927Sbz	/* We need to play some tricks here for the second interface. */
844241610Sglebius	strlcpy(name, epairname, len);
845194927Sbz	error = if_clone_create(name, len, (caddr_t)scb);
846194927Sbz	if (error)
847241610Sglebius		panic("%s: if_clone_create() for our 2nd iface failed: %d",
848194927Sbz		    __func__, error);
849194927Sbz	scb->if_qflush = ifp->if_qflush;
850194927Sbz	ifp->if_qflush = epair_qflush;
851194927Sbz	ifp->if_transmit = epair_transmit;
852241677Sglebius	if_initbaudrate(ifp, IF_Gbps(10));	/* arbitrary maximum */
853194927Sbz
854194927Sbz	/*
855194927Sbz	 * Restore name to <n>a as the ifp for this will go into the
856194927Sbz	 * cloner list for the initial call.
857194927Sbz	 */
858194927Sbz	strlcpy(name, sca->ifp->if_xname, len);
859194927Sbz	DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
860194927Sbz
861222246Szec	/* Initialise pseudo media types. */
862222246Szec	ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
863222246Szec	ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
864222246Szec	ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
865222246Szec	ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
866222246Szec	ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
867222246Szec	ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
868222246Szec
869194927Sbz	/* Tell the world, that we are ready to rock. */
870194927Sbz	sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
871194927Sbz	scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
872211904Sbz	if_link_state_change(sca->ifp, LINK_STATE_UP);
873211904Sbz	if_link_state_change(scb->ifp, LINK_STATE_UP);
874194927Sbz
875194927Sbz	return (0);
876194927Sbz}
877194927Sbz
878194927Sbzstatic int
879194927Sbzepair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
880194927Sbz{
881194927Sbz	struct ifnet *oifp;
882194927Sbz	struct epair_softc *sca, *scb;
883194927Sbz	int unit, error;
884194927Sbz
885194927Sbz	DPRINTF("ifp=%p\n", ifp);
886194927Sbz
887194927Sbz	/*
888194927Sbz	 * In case we called into if_clone_destroyif() ourselves
889194927Sbz	 * again to remove the second interface, the softc will be
890194927Sbz	 * NULL. In that case so not do anything but return success.
891194927Sbz	 */
892194927Sbz	if (ifp->if_softc == NULL)
893194927Sbz		return (0);
894194927Sbz
895194927Sbz	unit = ifp->if_dunit;
896194927Sbz	sca = ifp->if_softc;
897194927Sbz	oifp = sca->oifp;
898194927Sbz	scb = oifp->if_softc;
899194927Sbz
900194927Sbz	DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
901211904Sbz	if_link_state_change(ifp, LINK_STATE_DOWN);
902211904Sbz	if_link_state_change(oifp, LINK_STATE_DOWN);
903194927Sbz	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
904194927Sbz	oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
905238309Strociny
906238309Strociny	/*
907238309Strociny	 * Get rid of our second half. As the other of the two
908238309Strociny	 * interfaces may reside in a different vnet, we need to
909238309Strociny	 * switch before freeing them.
910238309Strociny	 */
911238309Strociny	CURVNET_SET_QUIET(oifp->if_vnet);
912194927Sbz	ether_ifdetach(oifp);
913194927Sbz	/*
914194927Sbz	 * Wait for all packets to be dispatched to if_input.
915238309Strociny	 * The numbers can only go down as the interface is
916194927Sbz	 * detached so there is no need to use atomics.
917194927Sbz	 */
918238309Strociny	DPRINTF("scb refcnt=%u\n", scb->refcount);
919238309Strociny	EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
920238309Strociny	    ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
921194927Sbz	oifp->if_softc = NULL;
922194927Sbz	error = if_clone_destroyif(ifc, oifp);
923194927Sbz	if (error)
924194927Sbz		panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
925194927Sbz		    __func__, error);
926238309Strociny	if_free(oifp);
927238309Strociny	ifmedia_removeall(&scb->media);
928238309Strociny	free(scb, M_EPAIR);
929238309Strociny	CURVNET_RESTORE();
930194927Sbz
931238309Strociny	ether_ifdetach(ifp);
932195892Sbz	/*
933238309Strociny	 * Wait for all packets to be dispatched to if_input.
934195892Sbz	 */
935238309Strociny	DPRINTF("sca refcnt=%u\n", sca->refcount);
936238309Strociny	EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
937238309Strociny	    ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
938212152Sbz	if_free(ifp);
939222246Szec	ifmedia_removeall(&sca->media);
940194927Sbz	free(sca, M_EPAIR);
941194927Sbz	ifc_free_unit(ifc, unit);
942194927Sbz
943194927Sbz	return (0);
944194927Sbz}
945194927Sbz
946194927Sbzstatic int
947194927Sbzepair_modevent(module_t mod, int type, void *data)
948194927Sbz{
949195892Sbz	int qlimit;
950194927Sbz
951194927Sbz	switch (type) {
952194927Sbz	case MOD_LOAD:
953194927Sbz		/* For now limit us to one global mutex and one inq. */
954195892Sbz		epair_dpcpu_init();
955195892Sbz		epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
956195892Sbz		if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
957195892Sbz		    epair_nh.nh_qlimit = qlimit;
958195892Sbz		netisr_register(&epair_nh);
959241610Sglebius		epair_cloner = if_clone_advanced(epairname, 0,
960241610Sglebius		    epair_clone_match, epair_clone_create, epair_clone_destroy);
961194927Sbz		if (bootverbose)
962241610Sglebius			printf("%s initialized.\n", epairname);
963194927Sbz		break;
964194927Sbz	case MOD_UNLOAD:
965241610Sglebius		if_clone_detach(epair_cloner);
966195892Sbz		netisr_unregister(&epair_nh);
967195892Sbz		epair_dpcpu_detach();
968194927Sbz		if (bootverbose)
969241610Sglebius			printf("%s unloaded.\n", epairname);
970194927Sbz		break;
971194927Sbz	default:
972194927Sbz		return (EOPNOTSUPP);
973194927Sbz	}
974194927Sbz	return (0);
975194927Sbz}
976194927Sbz
977194927Sbzstatic moduledata_t epair_mod = {
978194927Sbz	"if_epair",
979194927Sbz	epair_modevent,
980241394Skevlo	0
981194927Sbz};
982194927Sbz
983194927SbzDECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
984194927SbzMODULE_VERSION(if_epair, 1);
985