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
75194927Sbz#define	EPAIRNAME	"epair"
76194927Sbz
77194927SbzSYSCTL_DECL(_net_link);
78248085Smariusstatic SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
79195892Sbz
80195892Sbz#ifdef EPAIR_DEBUG
81195892Sbzstatic int epair_debug = 0;
82215641SbzSYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
83194927Sbz    &epair_debug, 0, "if_epair(4) debugging.");
84195892Sbz#define	DPRINTF(fmt, arg...)						\
85195892Sbz	if (epair_debug)						\
86195892Sbz		printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
87194927Sbz#else
88194927Sbz#define	DPRINTF(fmt, arg...)
89194927Sbz#endif
90194927Sbz
91195892Sbzstatic void epair_nh_sintr(struct mbuf *);
92195892Sbzstatic struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
93195892Sbzstatic void epair_nh_drainedcpu(u_int);
94195892Sbz
95195892Sbzstatic void epair_start_locked(struct ifnet *);
96222246Szecstatic int epair_media_change(struct ifnet *);
97222246Szecstatic void epair_media_status(struct ifnet *, struct ifmediareq *);
98195892Sbz
99195892Sbzstatic int epair_clone_match(struct if_clone *, const char *);
100195892Sbzstatic int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
101195892Sbzstatic int epair_clone_destroy(struct if_clone *, struct ifnet *);
102195892Sbz
103195892Sbz/* Netisr realted definitions and sysctl. */
104195892Sbzstatic struct netisr_handler epair_nh = {
105195892Sbz	.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
171194927Sbzstatic MALLOC_DEFINE(M_EPAIR, EPAIRNAME,
172194927Sbz    "Pair of virtual cross-over connected Ethernet-like interfaces");
173194927Sbz
174194927Sbzstatic struct if_clone epair_cloner = IFC_CLONE_INITIALIZER(
175194927Sbz    EPAIRNAME, NULL, IF_MAXUNIT,
176194927Sbz    NULL, epair_clone_match, epair_clone_create, epair_clone_destroy);
177194927Sbz
178195892Sbz/*
179195892Sbz * DPCPU area and functions.
180195892Sbz */
181195892Sbzstruct epair_dpcpu {
182195892Sbz	struct mtx	if_epair_mtx;		/* Per-CPU locking. */
183195892Sbz	int		epair_drv_flags;	/* Per-CPU ``hw'' drv flags. */
184195892Sbz	struct eid_list	epair_ifp_drain_list;	/* Per-CPU list of ifps with
185195892Sbz						 * data in the ifq. */
186195892Sbz};
187195892SbzDPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
188194927Sbz
189195892Sbzstatic void
190195892Sbzepair_dpcpu_init(void)
191195892Sbz{
192195892Sbz	struct epair_dpcpu *epair_dpcpu;
193195892Sbz	struct eid_list *s;
194195892Sbz	u_int cpuid;
195195892Sbz
196209059Sjhb	CPU_FOREACH(cpuid) {
197195892Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
198195892Sbz
199195892Sbz		/* Initialize per-cpu lock. */
200195892Sbz		EPAIR_LOCK_INIT(epair_dpcpu);
201195892Sbz
202195892Sbz		/* Driver flags are per-cpu as are our netisr "hw" queues. */
203195892Sbz		epair_dpcpu->epair_drv_flags = 0;
204195892Sbz
205195892Sbz		/*
206195892Sbz		 * Initialize per-cpu drain list.
207195892Sbz		 * Manually do what STAILQ_HEAD_INITIALIZER would do.
208195892Sbz		 */
209195892Sbz		s = &epair_dpcpu->epair_ifp_drain_list;
210195892Sbz		s->stqh_first = NULL;
211195892Sbz		s->stqh_last = &s->stqh_first;
212195892Sbz	}
213195892Sbz}
214195892Sbz
215195892Sbzstatic void
216195892Sbzepair_dpcpu_detach(void)
217195892Sbz{
218195892Sbz	struct epair_dpcpu *epair_dpcpu;
219195892Sbz	u_int cpuid;
220195892Sbz
221209059Sjhb	CPU_FOREACH(cpuid) {
222195892Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
223195892Sbz
224195892Sbz		/* Destroy per-cpu lock. */
225195892Sbz		EPAIR_LOCK_DESTROY(epair_dpcpu);
226195892Sbz	}
227195892Sbz}
228195892Sbz
229194927Sbz/*
230195892Sbz * Helper functions.
231195892Sbz */
232195892Sbzstatic u_int
233195892Sbzcpuid_from_ifp(struct ifnet *ifp)
234195892Sbz{
235195892Sbz	struct epair_softc *sc;
236195892Sbz
237195892Sbz	if (ifp == NULL)
238195892Sbz		return (0);
239195892Sbz	sc = ifp->if_softc;
240195892Sbz
241195892Sbz	return (sc->cpuid);
242195892Sbz}
243195892Sbz
244195892Sbz/*
245194927Sbz * Netisr handler functions.
246194927Sbz */
247194927Sbzstatic void
248195892Sbzepair_nh_sintr(struct mbuf *m)
249194927Sbz{
250194927Sbz	struct ifnet *ifp;
251194927Sbz	struct epair_softc *sc;
252194927Sbz
253194927Sbz	ifp = m->m_pkthdr.rcvif;
254194927Sbz	(*ifp->if_input)(ifp, m);
255194927Sbz	sc = ifp->if_softc;
256195892Sbz	EPAIR_REFCOUNT_RELEASE(&sc->refcount);
257204805Sbz	EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
258204805Sbz	    ("%s: ifp=%p sc->refcount not >= 1: %d",
259204805Sbz	    __func__, ifp, sc->refcount));
260194927Sbz	DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
261194927Sbz}
262194927Sbz
263195892Sbzstatic struct mbuf *
264195892Sbzepair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
265195892Sbz{
266195892Sbz
267195892Sbz	*cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
268195892Sbz
269195892Sbz	return (m);
270195892Sbz}
271195892Sbz
272194927Sbzstatic void
273195892Sbzepair_nh_drainedcpu(u_int cpuid)
274194927Sbz{
275195892Sbz	struct epair_dpcpu *epair_dpcpu;
276194927Sbz	struct epair_ifp_drain *elm, *tvar;
277194927Sbz	struct ifnet *ifp;
278194927Sbz
279195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
280195892Sbz	EPAIR_LOCK(epair_dpcpu);
281194927Sbz	/*
282194927Sbz	 * Assume our "hw" queue and possibly ifq will be emptied
283194927Sbz	 * again. In case we will overflow the "hw" queue while
284194927Sbz	 * draining, epair_start_locked will set IFF_DRV_OACTIVE
285194927Sbz	 * again and we will stop and return.
286194927Sbz	 */
287195892Sbz	STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
288195892Sbz	    ifp_next, tvar) {
289194927Sbz		ifp = elm->ifp;
290195892Sbz		epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
291194927Sbz		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
292194927Sbz		epair_start_locked(ifp);
293194927Sbz
294194927Sbz		IFQ_LOCK(&ifp->if_snd);
295194927Sbz		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
296204805Sbz			struct epair_softc *sc;
297204805Sbz
298195892Sbz			STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
299195892Sbz			    elm, epair_ifp_drain, ifp_next);
300204805Sbz			/* The cached ifp goes off the list. */
301204805Sbz			sc = ifp->if_softc;
302204805Sbz			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
303204805Sbz			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
304204805Sbz			    ("%s: ifp=%p sc->refcount not >= 1: %d",
305204805Sbz			    __func__, ifp, sc->refcount));
306194927Sbz			free(elm, M_EPAIR);
307194927Sbz		}
308194927Sbz		IFQ_UNLOCK(&ifp->if_snd);
309194927Sbz
310194927Sbz		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
311194927Sbz			/* Our "hw"q overflew again. */
312215641Sbz			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
313194927Sbz			DPRINTF("hw queue length overflow at %u\n",
314195892Sbz			    epair_nh.nh_qlimit);
315194927Sbz			break;
316194927Sbz		}
317194927Sbz	}
318195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
319194927Sbz}
320194927Sbz
321194927Sbz/*
322194927Sbz * Network interface (`if') related functions.
323194927Sbz */
324204805Sbzstatic void
325204805Sbzepair_remove_ifp_from_draining(struct ifnet *ifp)
326204805Sbz{
327204805Sbz	struct epair_dpcpu *epair_dpcpu;
328204805Sbz	struct epair_ifp_drain *elm, *tvar;
329204805Sbz	u_int cpuid;
330204805Sbz
331209059Sjhb	CPU_FOREACH(cpuid) {
332204805Sbz		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
333204805Sbz		EPAIR_LOCK(epair_dpcpu);
334204805Sbz		STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
335204805Sbz		    ifp_next, tvar) {
336204805Sbz			if (ifp == elm->ifp) {
337204805Sbz				struct epair_softc *sc;
338204805Sbz
339204805Sbz				STAILQ_REMOVE(
340204805Sbz				    &epair_dpcpu->epair_ifp_drain_list, elm,
341204805Sbz				    epair_ifp_drain, ifp_next);
342204805Sbz				/* The cached ifp goes off the list. */
343204805Sbz				sc = ifp->if_softc;
344204805Sbz				EPAIR_REFCOUNT_RELEASE(&sc->refcount);
345204805Sbz				EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
346204805Sbz				    ("%s: ifp=%p sc->refcount not >= 1: %d",
347204805Sbz				    __func__, ifp, sc->refcount));
348204805Sbz				free(elm, M_EPAIR);
349204805Sbz			}
350204805Sbz		}
351204805Sbz		EPAIR_UNLOCK(epair_dpcpu);
352204805Sbz	}
353204805Sbz}
354204805Sbz
355195892Sbzstatic int
356195892Sbzepair_add_ifp_for_draining(struct ifnet *ifp)
357195892Sbz{
358195892Sbz	struct epair_dpcpu *epair_dpcpu;
359204805Sbz	struct epair_softc *sc;
360195892Sbz	struct epair_ifp_drain *elm = NULL;
361195892Sbz
362204805Sbz	sc = ifp->if_softc;
363195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
364204805Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
365195892Sbz	STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
366195892Sbz		if (elm->ifp == ifp)
367195892Sbz			break;
368201995Sbz	/* If the ifp is there already, return success. */
369195892Sbz	if (elm != NULL)
370195892Sbz		return (0);
371195892Sbz
372195892Sbz	elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
373195892Sbz	if (elm == NULL)
374195892Sbz		return (ENOMEM);
375195892Sbz
376195892Sbz	elm->ifp = ifp;
377204805Sbz	/* Add a reference for the ifp pointer on the list. */
378204805Sbz	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
379195892Sbz	STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
380195892Sbz
381195892Sbz	return (0);
382195892Sbz}
383195892Sbz
384194927Sbzstatic void
385194927Sbzepair_start_locked(struct ifnet *ifp)
386194927Sbz{
387195892Sbz	struct epair_dpcpu *epair_dpcpu;
388194927Sbz	struct mbuf *m;
389194927Sbz	struct epair_softc *sc;
390194927Sbz	struct ifnet *oifp;
391194927Sbz	int error;
392194927Sbz
393194927Sbz	DPRINTF("ifp=%p\n", ifp);
394195892Sbz	sc = ifp->if_softc;
395195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
396195892Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
397194927Sbz
398194927Sbz	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
399194927Sbz		return;
400194927Sbz	if ((ifp->if_flags & IFF_UP) == 0)
401194927Sbz		return;
402194927Sbz
403194927Sbz	/*
404194927Sbz	 * We get patckets here from ether_output via if_handoff()
405194927Sbz	 * and ned to put them into the input queue of the oifp
406194927Sbz	 * and call oifp->if_input() via netisr/epair_sintr().
407194927Sbz	 */
408194927Sbz	oifp = sc->oifp;
409194927Sbz	sc = oifp->if_softc;
410194927Sbz	for (;;) {
411194927Sbz		IFQ_DEQUEUE(&ifp->if_snd, m);
412194927Sbz		if (m == NULL)
413194927Sbz			break;
414194927Sbz		BPF_MTAP(ifp, m);
415194927Sbz
416194927Sbz		/*
417194927Sbz		 * In case the outgoing interface is not usable,
418194927Sbz		 * drop the packet.
419194927Sbz		 */
420194927Sbz		if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
421194927Sbz		    (oifp->if_flags & IFF_UP) ==0) {
422194927Sbz			ifp->if_oerrors++;
423194927Sbz			m_freem(m);
424194927Sbz			continue;
425194927Sbz		}
426194927Sbz		DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
427194927Sbz
428194927Sbz		/*
429194927Sbz		 * Add a reference so the interface cannot go while the
430194927Sbz		 * packet is in transit as we rely on rcvif to stay valid.
431194927Sbz		 */
432195892Sbz		EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
433194927Sbz		m->m_pkthdr.rcvif = oifp;
434194927Sbz		CURVNET_SET_QUIET(oifp->if_vnet);
435194927Sbz		error = netisr_queue(NETISR_EPAIR, m);
436194927Sbz		CURVNET_RESTORE();
437194927Sbz		if (!error) {
438194927Sbz			ifp->if_opackets++;
439194927Sbz			/* Someone else received the packet. */
440194927Sbz			oifp->if_ipackets++;
441194927Sbz		} else {
442204805Sbz			/* The packet was freed already. */
443195892Sbz			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
444194927Sbz			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
445204805Sbz			(void) epair_add_ifp_for_draining(ifp);
446204805Sbz			ifp->if_oerrors++;
447195892Sbz			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
448204805Sbz			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
449204805Sbz			    ("%s: ifp=%p sc->refcount not >= 1: %d",
450204805Sbz			    __func__, oifp, sc->refcount));
451194927Sbz		}
452194927Sbz	}
453194927Sbz}
454194927Sbz
455194927Sbzstatic void
456194927Sbzepair_start(struct ifnet *ifp)
457194927Sbz{
458195892Sbz	struct epair_dpcpu *epair_dpcpu;
459194927Sbz
460195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
461195892Sbz	EPAIR_LOCK(epair_dpcpu);
462194927Sbz	epair_start_locked(ifp);
463195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
464194927Sbz}
465194927Sbz
466194927Sbzstatic int
467194927Sbzepair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
468194927Sbz{
469195892Sbz	struct epair_dpcpu *epair_dpcpu;
470194927Sbz	struct epair_softc *sc;
471194927Sbz	struct ifnet *oifp;
472194927Sbz	int error, len;
473194927Sbz	short mflags;
474194927Sbz
475194927Sbz	DPRINTF("ifp=%p m=%p\n", ifp, m);
476195892Sbz	sc = ifp->if_softc;
477195892Sbz	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
478195892Sbz	EPAIR_LOCK_ASSERT(epair_dpcpu);
479194927Sbz
480194927Sbz	if (m == NULL)
481194927Sbz		return (0);
482194927Sbz
483194927Sbz	/*
484194927Sbz	 * We are not going to use the interface en/dequeue mechanism
485194927Sbz	 * on the TX side. We are called from ether_output_frame()
486194927Sbz	 * and will put the packet into the incoming queue of the
487194927Sbz	 * other interface of our pair via the netsir.
488194927Sbz	 */
489194927Sbz	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
490194927Sbz		m_freem(m);
491194927Sbz		return (ENXIO);
492194927Sbz	}
493194927Sbz	if ((ifp->if_flags & IFF_UP) == 0) {
494194927Sbz		m_freem(m);
495194927Sbz		return (ENETDOWN);
496194927Sbz	}
497194927Sbz
498194927Sbz	BPF_MTAP(ifp, m);
499194927Sbz
500194927Sbz	/*
501194927Sbz	 * In case the outgoing interface is not usable,
502194927Sbz	 * drop the packet.
503194927Sbz	 */
504194927Sbz	oifp = sc->oifp;
505194927Sbz	if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
506194927Sbz	    (oifp->if_flags & IFF_UP) ==0) {
507194927Sbz		ifp->if_oerrors++;
508194927Sbz		m_freem(m);
509194927Sbz		return (0);
510194927Sbz	}
511194927Sbz	len = m->m_pkthdr.len;
512194927Sbz	mflags = m->m_flags;
513194927Sbz	DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
514194927Sbz
515194927Sbz#ifdef ALTQ
516194927Sbz	/* Support ALTQ via the clasic if_start() path. */
517194927Sbz	IF_LOCK(&ifp->if_snd);
518194927Sbz	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
519194927Sbz		ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
520194927Sbz		if (error)
521194927Sbz			ifp->if_snd.ifq_drops++;
522194927Sbz		IF_UNLOCK(&ifp->if_snd);
523194927Sbz		if (!error) {
524194927Sbz			ifp->if_obytes += len;
525194927Sbz			if (mflags & (M_BCAST|M_MCAST))
526194927Sbz				ifp->if_omcasts++;
527194927Sbz
528194927Sbz			if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
529194927Sbz				epair_start_locked(ifp);
530194927Sbz			else
531195892Sbz				(void)epair_add_ifp_for_draining(ifp);
532194927Sbz		}
533194927Sbz		return (error);
534194927Sbz	}
535194927Sbz	IF_UNLOCK(&ifp->if_snd);
536194927Sbz#endif
537194927Sbz
538195892Sbz	if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
539194927Sbz		/*
540194927Sbz		 * Our hardware queue is full, try to fall back
541194927Sbz		 * queuing to the ifq but do not call ifp->if_start.
542194927Sbz		 * Either we are lucky or the packet is gone.
543194927Sbz		 */
544194927Sbz		IFQ_ENQUEUE(&ifp->if_snd, m, error);
545194927Sbz		if (!error)
546195892Sbz			(void)epair_add_ifp_for_draining(ifp);
547194927Sbz		return (error);
548194927Sbz	}
549194927Sbz	sc = oifp->if_softc;
550194927Sbz	/*
551194927Sbz	 * Add a reference so the interface cannot go while the
552194927Sbz	 * packet is in transit as we rely on rcvif to stay valid.
553194927Sbz	 */
554195892Sbz	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
555194927Sbz	m->m_pkthdr.rcvif = oifp;
556194927Sbz	CURVNET_SET_QUIET(oifp->if_vnet);
557194927Sbz	error = netisr_queue(NETISR_EPAIR, m);
558194927Sbz	CURVNET_RESTORE();
559194927Sbz	if (!error) {
560194927Sbz		ifp->if_opackets++;
561194927Sbz		/*
562194927Sbz		 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
563194927Sbz		 * but as we bypass all this we have to duplicate
564194927Sbz		 * the logic another time.
565194927Sbz		 */
566194927Sbz		ifp->if_obytes += len;
567194927Sbz		if (mflags & (M_BCAST|M_MCAST))
568194927Sbz			ifp->if_omcasts++;
569194927Sbz		/* Someone else received the packet. */
570194927Sbz		oifp->if_ipackets++;
571194927Sbz	} else {
572194927Sbz		/* The packet was freed already. */
573195892Sbz		epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
574194927Sbz		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
575204805Sbz		ifp->if_oerrors++;
576204805Sbz		EPAIR_REFCOUNT_RELEASE(&sc->refcount);
577204805Sbz		EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
578204805Sbz		    ("%s: ifp=%p sc->refcount not >= 1: %d",
579204805Sbz		    __func__, oifp, sc->refcount));
580194927Sbz	}
581194927Sbz
582194927Sbz	return (error);
583194927Sbz}
584194927Sbz
585194927Sbzstatic int
586194927Sbzepair_transmit(struct ifnet *ifp, struct mbuf *m)
587194927Sbz{
588195892Sbz	struct epair_dpcpu *epair_dpcpu;
589194927Sbz	int error;
590194927Sbz
591195892Sbz	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
592195892Sbz	EPAIR_LOCK(epair_dpcpu);
593194927Sbz	error = epair_transmit_locked(ifp, m);
594195892Sbz	EPAIR_UNLOCK(epair_dpcpu);
595194927Sbz	return (error);
596194927Sbz}
597194927Sbz
598194927Sbzstatic void
599194927Sbzepair_qflush(struct ifnet *ifp)
600194927Sbz{
601194927Sbz	struct epair_softc *sc;
602194927Sbz
603194927Sbz	sc = ifp->if_softc;
604204805Sbz	KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
605204805Sbz	    __func__, ifp, sc));
606194927Sbz	/*
607204805Sbz	 * Remove this ifp from all backpointer lists. The interface will not
608204805Sbz	 * usable for flushing anyway nor should it have anything to flush
609204805Sbz	 * after if_qflush().
610194927Sbz	 */
611204805Sbz	epair_remove_ifp_from_draining(ifp);
612204805Sbz
613194927Sbz	if (sc->if_qflush)
614194927Sbz		sc->if_qflush(ifp);
615194927Sbz}
616194927Sbz
617194927Sbzstatic int
618222246Szecepair_media_change(struct ifnet *ifp __unused)
619222246Szec{
620222246Szec
621222246Szec	/* Do nothing. */
622222246Szec	return (0);
623222246Szec}
624222246Szec
625222246Szecstatic void
626222246Szecepair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
627222246Szec{
628222246Szec
629222246Szec	imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
630222246Szec	imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
631222246Szec}
632222246Szec
633222246Szecstatic int
634194927Sbzepair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
635194927Sbz{
636222246Szec	struct epair_softc *sc;
637194927Sbz	struct ifreq *ifr;
638194927Sbz	int error;
639194927Sbz
640194927Sbz	ifr = (struct ifreq *)data;
641194927Sbz	switch (cmd) {
642194927Sbz	case SIOCSIFFLAGS:
643194927Sbz	case SIOCADDMULTI:
644194927Sbz	case SIOCDELMULTI:
645194927Sbz		error = 0;
646194927Sbz		break;
647194927Sbz
648222246Szec	case SIOCSIFMEDIA:
649222246Szec	case SIOCGIFMEDIA:
650222246Szec		sc = ifp->if_softc;
651222246Szec		error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
652222246Szec		break;
653222246Szec
654195892Sbz	case SIOCSIFMTU:
655195892Sbz		/* We basically allow all kinds of MTUs. */
656195892Sbz		ifp->if_mtu = ifr->ifr_mtu;
657195892Sbz		error = 0;
658195892Sbz		break;
659195892Sbz
660194927Sbz	default:
661194927Sbz		/* Let the common ethernet handler process this. */
662194927Sbz		error = ether_ioctl(ifp, cmd, data);
663194927Sbz		break;
664194927Sbz	}
665194927Sbz
666194927Sbz	return (error);
667194927Sbz}
668194927Sbz
669194927Sbzstatic void
670194927Sbzepair_init(void *dummy __unused)
671194927Sbz{
672194927Sbz}
673194927Sbz
674194927Sbz
675194927Sbz/*
676194927Sbz * Interface cloning functions.
677194927Sbz * We use our private ones so that we can create/destroy our secondary
678194927Sbz * device along with the primary one.
679194927Sbz */
680194927Sbzstatic int
681194927Sbzepair_clone_match(struct if_clone *ifc, const char *name)
682194927Sbz{
683194927Sbz	const char *cp;
684194927Sbz
685194927Sbz	DPRINTF("name='%s'\n", name);
686194927Sbz
687194927Sbz	/*
688194927Sbz	 * Our base name is epair.
689194927Sbz	 * Our interfaces will be named epair<n>[ab].
690194927Sbz	 * So accept anything of the following list:
691194927Sbz	 * - epair
692194927Sbz	 * - epair<n>
693194927Sbz	 * but not the epair<n>[ab] versions.
694194927Sbz	 */
695194927Sbz	if (strncmp(EPAIRNAME, name, sizeof(EPAIRNAME)-1) != 0)
696194927Sbz		return (0);
697194927Sbz
698194927Sbz	for (cp = name + sizeof(EPAIRNAME) - 1; *cp != '\0'; cp++) {
699194927Sbz		if (*cp < '0' || *cp > '9')
700194927Sbz			return (0);
701194927Sbz	}
702194927Sbz
703194927Sbz	return (1);
704194927Sbz}
705194927Sbz
706194927Sbzstatic int
707194927Sbzepair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
708194927Sbz{
709194927Sbz	struct epair_softc *sca, *scb;
710194927Sbz	struct ifnet *ifp;
711194927Sbz	char *dp;
712194927Sbz	int error, unit, wildcard;
713194927Sbz	uint8_t eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
714194927Sbz
715194927Sbz	/*
716194927Sbz	 * We are abusing params to create our second interface.
717194927Sbz	 * Actually we already created it and called if_clone_createif()
718194927Sbz	 * for it to do the official insertion procedure the moment we knew
719194927Sbz	 * it cannot fail anymore. So just do attach it here.
720194927Sbz	 */
721194927Sbz	if (params) {
722194927Sbz		scb = (struct epair_softc *)params;
723194927Sbz		ifp = scb->ifp;
724194927Sbz		/* Assign a hopefully unique, locally administered etheraddr. */
725194927Sbz		eaddr[0] = 0x02;
726194927Sbz		eaddr[3] = (ifp->if_index >> 8) & 0xff;
727194927Sbz		eaddr[4] = ifp->if_index & 0xff;
728194927Sbz		eaddr[5] = 0x0b;
729194927Sbz		ether_ifattach(ifp, eaddr);
730194927Sbz		/* Correctly set the name for the cloner list. */
731194927Sbz		strlcpy(name, scb->ifp->if_xname, len);
732194927Sbz		return (0);
733194927Sbz	}
734194927Sbz
735194927Sbz	/* Try to see if a special unit was requested. */
736194927Sbz	error = ifc_name2unit(name, &unit);
737194927Sbz	if (error != 0)
738194927Sbz		return (error);
739194927Sbz	wildcard = (unit < 0);
740194927Sbz
741194927Sbz	error = ifc_alloc_unit(ifc, &unit);
742194927Sbz	if (error != 0)
743194927Sbz		return (error);
744194927Sbz
745194927Sbz	/*
746194927Sbz	 * If no unit had been given, we need to adjust the ifName.
747194927Sbz	 * Also make sure there is space for our extra [ab] suffix.
748194927Sbz	 */
749194927Sbz	for (dp = name; *dp != '\0'; dp++);
750194927Sbz	if (wildcard) {
751194927Sbz		error = snprintf(dp, len - (dp - name), "%d", unit);
752194927Sbz		if (error > len - (dp - name) - 1) {
753194927Sbz			/* ifName too long. */
754194927Sbz			ifc_free_unit(ifc, unit);
755194927Sbz			return (ENOSPC);
756194927Sbz		}
757194927Sbz		dp += error;
758194927Sbz	}
759194927Sbz	if (len - (dp - name) - 1 < 1) {
760194927Sbz		/* No space left for our [ab] suffix. */
761194927Sbz		ifc_free_unit(ifc, unit);
762194927Sbz		return (ENOSPC);
763194927Sbz	}
764194927Sbz	*dp = 'a';
765194927Sbz	/* Must not change dp so we can replace 'a' by 'b' later. */
766194927Sbz	*(dp+1) = '\0';
767194927Sbz
768194927Sbz	/* Allocate memory for both [ab] interfaces */
769194927Sbz	sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
770195892Sbz	EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
771194927Sbz	sca->ifp = if_alloc(IFT_ETHER);
772194927Sbz	if (sca->ifp == NULL) {
773194927Sbz		free(sca, M_EPAIR);
774194927Sbz		ifc_free_unit(ifc, unit);
775194927Sbz		return (ENOSPC);
776194927Sbz	}
777194927Sbz
778194927Sbz	scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
779195892Sbz	EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
780194927Sbz	scb->ifp = if_alloc(IFT_ETHER);
781194927Sbz	if (scb->ifp == NULL) {
782194927Sbz		free(scb, M_EPAIR);
783194927Sbz		if_free(sca->ifp);
784194927Sbz		free(sca, M_EPAIR);
785194927Sbz		ifc_free_unit(ifc, unit);
786194927Sbz		return (ENOSPC);
787194927Sbz	}
788194927Sbz
789194927Sbz	/*
790194927Sbz	 * Cross-reference the interfaces so we will be able to free both.
791194927Sbz	 */
792194927Sbz	sca->oifp = scb->ifp;
793194927Sbz	scb->oifp = sca->ifp;
794195892Sbz
795195892Sbz	/*
796195892Sbz	 * Calculate the cpuid for netisr queueing based on the
797195892Sbz	 * ifIndex of the interfaces. As long as we cannot configure
798195892Sbz	 * this or use cpuset information easily we cannot guarantee
799195892Sbz	 * cache locality but we can at least allow parallelism.
800195892Sbz	 */
801195892Sbz	sca->cpuid =
802195892Sbz	    netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
803195892Sbz	scb->cpuid =
804195892Sbz	    netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
805194927Sbz
806194927Sbz	/* Finish initialization of interface <n>a. */
807194927Sbz	ifp = sca->ifp;
808194927Sbz	ifp->if_softc = sca;
809194927Sbz	strlcpy(ifp->if_xname, name, IFNAMSIZ);
810194927Sbz	ifp->if_dname = ifc->ifc_name;
811194927Sbz	ifp->if_dunit = unit;
812194927Sbz	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
813222247Szec	ifp->if_capabilities = IFCAP_VLAN_MTU;
814222247Szec	ifp->if_capenable = IFCAP_VLAN_MTU;
815194927Sbz	ifp->if_start = epair_start;
816194927Sbz	ifp->if_ioctl = epair_ioctl;
817194927Sbz	ifp->if_init  = epair_init;
818194927Sbz	ifp->if_snd.ifq_maxlen = ifqmaxlen;
819194927Sbz	/* Assign a hopefully unique, locally administered etheraddr. */
820194927Sbz	eaddr[0] = 0x02;
821194927Sbz	eaddr[3] = (ifp->if_index >> 8) & 0xff;
822194927Sbz	eaddr[4] = ifp->if_index & 0xff;
823194927Sbz	eaddr[5] = 0x0a;
824194927Sbz	ether_ifattach(ifp, eaddr);
825194927Sbz	sca->if_qflush = ifp->if_qflush;
826194927Sbz	ifp->if_qflush = epair_qflush;
827194927Sbz	ifp->if_transmit = epair_transmit;
828194927Sbz	ifp->if_baudrate = IF_Gbps(10UL);	/* arbitrary maximum */
829194927Sbz
830194927Sbz	/* Swap the name and finish initialization of interface <n>b. */
831194927Sbz	*dp = 'b';
832194927Sbz
833194927Sbz	ifp = scb->ifp;
834194927Sbz	ifp->if_softc = scb;
835194927Sbz	strlcpy(ifp->if_xname, name, IFNAMSIZ);
836194927Sbz	ifp->if_dname = ifc->ifc_name;
837194927Sbz	ifp->if_dunit = unit;
838194927Sbz	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
839222247Szec	ifp->if_capabilities = IFCAP_VLAN_MTU;
840222247Szec	ifp->if_capenable = IFCAP_VLAN_MTU;
841194927Sbz	ifp->if_start = epair_start;
842194927Sbz	ifp->if_ioctl = epair_ioctl;
843194927Sbz	ifp->if_init  = epair_init;
844194927Sbz	ifp->if_snd.ifq_maxlen = ifqmaxlen;
845194927Sbz	/* We need to play some tricks here for the second interface. */
846194927Sbz	strlcpy(name, EPAIRNAME, len);
847194927Sbz	error = if_clone_create(name, len, (caddr_t)scb);
848194927Sbz	if (error)
849194927Sbz		panic("%s: if_clone_createif() for our 2nd iface failed: %d",
850194927Sbz		    __func__, error);
851194927Sbz	scb->if_qflush = ifp->if_qflush;
852194927Sbz	ifp->if_qflush = epair_qflush;
853194927Sbz	ifp->if_transmit = epair_transmit;
854194927Sbz	ifp->if_baudrate = IF_Gbps(10UL);	/* arbitrary maximum */
855194927Sbz
856194927Sbz	/*
857194927Sbz	 * Restore name to <n>a as the ifp for this will go into the
858194927Sbz	 * cloner list for the initial call.
859194927Sbz	 */
860194927Sbz	strlcpy(name, sca->ifp->if_xname, len);
861194927Sbz	DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
862194927Sbz
863222246Szec	/* Initialise pseudo media types. */
864222246Szec	ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
865222246Szec	ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
866222246Szec	ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
867222246Szec	ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
868222246Szec	ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
869222246Szec	ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
870222246Szec
871194927Sbz	/* Tell the world, that we are ready to rock. */
872194927Sbz	sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
873194927Sbz	scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
874211904Sbz	if_link_state_change(sca->ifp, LINK_STATE_UP);
875211904Sbz	if_link_state_change(scb->ifp, LINK_STATE_UP);
876194927Sbz
877194927Sbz	return (0);
878194927Sbz}
879194927Sbz
880194927Sbzstatic int
881194927Sbzepair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
882194927Sbz{
883194927Sbz	struct ifnet *oifp;
884194927Sbz	struct epair_softc *sca, *scb;
885194927Sbz	int unit, error;
886194927Sbz
887194927Sbz	DPRINTF("ifp=%p\n", ifp);
888194927Sbz
889194927Sbz	/*
890194927Sbz	 * In case we called into if_clone_destroyif() ourselves
891194927Sbz	 * again to remove the second interface, the softc will be
892194927Sbz	 * NULL. In that case so not do anything but return success.
893194927Sbz	 */
894194927Sbz	if (ifp->if_softc == NULL)
895194927Sbz		return (0);
896194927Sbz
897194927Sbz	unit = ifp->if_dunit;
898194927Sbz	sca = ifp->if_softc;
899194927Sbz	oifp = sca->oifp;
900194927Sbz	scb = oifp->if_softc;
901194927Sbz
902194927Sbz	DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
903211904Sbz	if_link_state_change(ifp, LINK_STATE_DOWN);
904211904Sbz	if_link_state_change(oifp, LINK_STATE_DOWN);
905194927Sbz	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
906194927Sbz	oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
907239980Strociny
908239980Strociny	/*
909239980Strociny	 * Get rid of our second half. As the other of the two
910239980Strociny	 * interfaces may reside in a different vnet, we need to
911239980Strociny	 * switch before freeing them.
912239980Strociny	 */
913239980Strociny	CURVNET_SET_QUIET(oifp->if_vnet);
914194927Sbz	ether_ifdetach(oifp);
915194927Sbz	/*
916194927Sbz	 * Wait for all packets to be dispatched to if_input.
917239980Strociny	 * The numbers can only go down as the interface is
918194927Sbz	 * detached so there is no need to use atomics.
919194927Sbz	 */
920239980Strociny	DPRINTF("scb refcnt=%u\n", scb->refcount);
921239980Strociny	EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
922239980Strociny	    ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
923194927Sbz	oifp->if_softc = NULL;
924194927Sbz	error = if_clone_destroyif(ifc, oifp);
925194927Sbz	if (error)
926194927Sbz		panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
927194927Sbz		    __func__, error);
928239980Strociny	if_free(oifp);
929239980Strociny	ifmedia_removeall(&scb->media);
930239980Strociny	free(scb, M_EPAIR);
931239980Strociny	CURVNET_RESTORE();
932194927Sbz
933239980Strociny	ether_ifdetach(ifp);
934195892Sbz	/*
935239980Strociny	 * Wait for all packets to be dispatched to if_input.
936195892Sbz	 */
937239980Strociny	DPRINTF("sca refcnt=%u\n", sca->refcount);
938239980Strociny	EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
939239980Strociny	    ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
940212152Sbz	if_free(ifp);
941222246Szec	ifmedia_removeall(&sca->media);
942194927Sbz	free(sca, M_EPAIR);
943194927Sbz	ifc_free_unit(ifc, unit);
944194927Sbz
945194927Sbz	return (0);
946194927Sbz}
947194927Sbz
948194927Sbzstatic int
949194927Sbzepair_modevent(module_t mod, int type, void *data)
950194927Sbz{
951195892Sbz	int qlimit;
952194927Sbz
953194927Sbz	switch (type) {
954194927Sbz	case MOD_LOAD:
955194927Sbz		/* For now limit us to one global mutex and one inq. */
956195892Sbz		epair_dpcpu_init();
957195892Sbz		epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
958195892Sbz		if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
959195892Sbz		    epair_nh.nh_qlimit = qlimit;
960195892Sbz		netisr_register(&epair_nh);
961194927Sbz		if_clone_attach(&epair_cloner);
962194927Sbz		if (bootverbose)
963194927Sbz			printf("%s initialized.\n", EPAIRNAME);
964194927Sbz		break;
965194927Sbz	case MOD_UNLOAD:
966194927Sbz		if_clone_detach(&epair_cloner);
967195892Sbz		netisr_unregister(&epair_nh);
968195892Sbz		epair_dpcpu_detach();
969194927Sbz		if (bootverbose)
970194927Sbz			printf("%s unloaded.\n", EPAIRNAME);
971194927Sbz		break;
972194927Sbz	default:
973194927Sbz		return (EOPNOTSUPP);
974194927Sbz	}
975194927Sbz	return (0);
976194927Sbz}
977194927Sbz
978194927Sbzstatic moduledata_t epair_mod = {
979194927Sbz	"if_epair",
980194927Sbz	epair_modevent,
981194927Sbz	0
982194927Sbz};
983194927Sbz
984194927SbzDECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
985194927SbzMODULE_VERSION(if_epair, 1);
986