xref: /freebsd/sys/net/if_epair.c (revision a4641f4e)
198c230c8SBjoern A. Zeeb /*-
298c230c8SBjoern A. Zeeb  * Copyright (c) 2008 The FreeBSD Foundation
3eea3faf7SBjoern A. Zeeb  * Copyright (c) 2009-2010 Bjoern A. Zeeb <bz@FreeBSD.org>
498c230c8SBjoern A. Zeeb  * All rights reserved.
598c230c8SBjoern A. Zeeb  *
698c230c8SBjoern A. Zeeb  * This software was developed by CK Software GmbH under sponsorship
798c230c8SBjoern A. Zeeb  * from the FreeBSD Foundation.
898c230c8SBjoern A. Zeeb  *
998c230c8SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
1098c230c8SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
1198c230c8SBjoern A. Zeeb  * are met:
1298c230c8SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
1398c230c8SBjoern A. Zeeb  * notice, this list of conditions and the following disclaimer.
1498c230c8SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
1598c230c8SBjoern A. Zeeb  * notice, this list of conditions and the following disclaimer in the
1698c230c8SBjoern A. Zeeb  * documentation and/or other materials provided with the distribution.
1798c230c8SBjoern A. Zeeb  *
1898c230c8SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1998c230c8SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2098c230c8SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2198c230c8SBjoern A. Zeeb  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2298c230c8SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2398c230c8SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2498c230c8SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2598c230c8SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2698c230c8SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2798c230c8SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2898c230c8SBjoern A. Zeeb  * SUCH DAMAGE.
2998c230c8SBjoern A. Zeeb  */
3098c230c8SBjoern A. Zeeb 
3198c230c8SBjoern A. Zeeb /*
32d0ea4743SBjoern A. Zeeb  * A pair of virtual back-to-back connected ethernet like interfaces
33d0ea4743SBjoern A. Zeeb  * (``two interfaces with a virtual cross-over cable'').
34d0ea4743SBjoern A. Zeeb  *
3598c230c8SBjoern A. Zeeb  * This is mostly intended to be used to provide connectivity between
3698c230c8SBjoern A. Zeeb  * different virtual network stack instances.
3798c230c8SBjoern A. Zeeb  */
3898c230c8SBjoern A. Zeeb /*
3998c230c8SBjoern A. Zeeb  * Things to re-think once we have more experience:
40d0ea4743SBjoern A. Zeeb  * - ifp->if_reassign function once we can test with vimage. Depending on
41530c0060SRobert Watson  *   how if_vmove() is going to be improved.
42d0ea4743SBjoern A. Zeeb  * - Real random etheraddrs that are checked to be uniquish; we would need
43d0ea4743SBjoern A. Zeeb  *   to re-do them in case we move the interface between network stacks
44d0ea4743SBjoern A. Zeeb  *   in a private if_reassign function.
45d0ea4743SBjoern A. Zeeb  *   In case we bridge to a real interface/network or between indepedent
46d0ea4743SBjoern A. Zeeb  *   epairs on multiple stacks/machines, we may need this.
47d0ea4743SBjoern A. Zeeb  *   For now let the user handle that case.
4898c230c8SBjoern A. Zeeb  */
4998c230c8SBjoern A. Zeeb 
5098c230c8SBjoern A. Zeeb #include <sys/cdefs.h>
5198c230c8SBjoern A. Zeeb __FBSDID("$FreeBSD$");
5298c230c8SBjoern A. Zeeb 
5398c230c8SBjoern A. Zeeb #include <sys/param.h>
5498c230c8SBjoern A. Zeeb #include <sys/kernel.h>
558ec07310SGleb Smirnoff #include <sys/malloc.h>
5698c230c8SBjoern A. Zeeb #include <sys/mbuf.h>
5798c230c8SBjoern A. Zeeb #include <sys/module.h>
5898c230c8SBjoern A. Zeeb #include <sys/refcount.h>
5998c230c8SBjoern A. Zeeb #include <sys/queue.h>
60d0ea4743SBjoern A. Zeeb #include <sys/smp.h>
6198c230c8SBjoern A. Zeeb #include <sys/socket.h>
6298c230c8SBjoern A. Zeeb #include <sys/sockio.h>
6398c230c8SBjoern A. Zeeb #include <sys/sysctl.h>
6498c230c8SBjoern A. Zeeb #include <sys/types.h>
6598c230c8SBjoern A. Zeeb 
6698c230c8SBjoern A. Zeeb #include <net/bpf.h>
6798c230c8SBjoern A. Zeeb #include <net/ethernet.h>
6898c230c8SBjoern A. Zeeb #include <net/if.h>
6976039bc8SGleb Smirnoff #include <net/if_var.h>
7098c230c8SBjoern A. Zeeb #include <net/if_clone.h>
712dccdd45SMarko Zec #include <net/if_media.h>
7298c230c8SBjoern A. Zeeb #include <net/if_var.h>
7398c230c8SBjoern A. Zeeb #include <net/if_types.h>
7498c230c8SBjoern A. Zeeb #include <net/netisr.h>
75530c0060SRobert Watson #include <net/vnet.h>
7698c230c8SBjoern A. Zeeb 
7798c230c8SBjoern A. Zeeb SYSCTL_DECL(_net_link);
786472ac3dSEd Schouten static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
79d0ea4743SBjoern A. Zeeb 
80d0ea4743SBjoern A. Zeeb #ifdef EPAIR_DEBUG
81d0ea4743SBjoern A. Zeeb static int epair_debug = 0;
822c8b047cSBjoern A. Zeeb SYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
8398c230c8SBjoern A. Zeeb     &epair_debug, 0, "if_epair(4) debugging.");
84d0ea4743SBjoern A. Zeeb #define	DPRINTF(fmt, arg...)						\
85d0ea4743SBjoern A. Zeeb 	if (epair_debug)						\
8698c230c8SBjoern A. Zeeb 		printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
8798c230c8SBjoern A. Zeeb #else
8898c230c8SBjoern A. Zeeb #define	DPRINTF(fmt, arg...)
8998c230c8SBjoern A. Zeeb #endif
9098c230c8SBjoern A. Zeeb 
91d0ea4743SBjoern A. Zeeb static void epair_nh_sintr(struct mbuf *);
92d0ea4743SBjoern A. Zeeb static struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
93d0ea4743SBjoern A. Zeeb static void epair_nh_drainedcpu(u_int);
9498c230c8SBjoern A. Zeeb 
95d0ea4743SBjoern A. Zeeb static void epair_start_locked(struct ifnet *);
962dccdd45SMarko Zec static int epair_media_change(struct ifnet *);
972dccdd45SMarko Zec static void epair_media_status(struct ifnet *, struct ifmediareq *);
9898c230c8SBjoern A. Zeeb 
9998c230c8SBjoern A. Zeeb static int epair_clone_match(struct if_clone *, const char *);
10098c230c8SBjoern A. Zeeb static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
10198c230c8SBjoern A. Zeeb static int epair_clone_destroy(struct if_clone *, struct ifnet *);
10298c230c8SBjoern A. Zeeb 
10342a58907SGleb Smirnoff static const char epairname[] = "epair";
10442a58907SGleb Smirnoff 
1053c3136b1SHiroki Sato /* Netisr related definitions and sysctl. */
106d0ea4743SBjoern A. Zeeb static struct netisr_handler epair_nh = {
10742a58907SGleb Smirnoff 	.nh_name	= epairname,
108d0ea4743SBjoern A. Zeeb 	.nh_proto	= NETISR_EPAIR,
109d0ea4743SBjoern A. Zeeb 	.nh_policy	= NETISR_POLICY_CPU,
110d0ea4743SBjoern A. Zeeb 	.nh_handler	= epair_nh_sintr,
111d0ea4743SBjoern A. Zeeb 	.nh_m2cpuid	= epair_nh_m2cpuid,
112d0ea4743SBjoern A. Zeeb 	.nh_drainedcpu	= epair_nh_drainedcpu,
113d0ea4743SBjoern A. Zeeb };
114d0ea4743SBjoern A. Zeeb 
115d0ea4743SBjoern A. Zeeb static int
116d0ea4743SBjoern A. Zeeb sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
117d0ea4743SBjoern A. Zeeb {
118d0ea4743SBjoern A. Zeeb 	int error, qlimit;
119d0ea4743SBjoern A. Zeeb 
120d0ea4743SBjoern A. Zeeb 	netisr_getqlimit(&epair_nh, &qlimit);
121d0ea4743SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
122d0ea4743SBjoern A. Zeeb 	if (error || !req->newptr)
123d0ea4743SBjoern A. Zeeb 		return (error);
124d0ea4743SBjoern A. Zeeb 	if (qlimit < 1)
125d0ea4743SBjoern A. Zeeb 		return (EINVAL);
126d0ea4743SBjoern A. Zeeb 	return (netisr_setqlimit(&epair_nh, qlimit));
127d0ea4743SBjoern A. Zeeb }
128d0ea4743SBjoern A. Zeeb SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
129d0ea4743SBjoern A. Zeeb     0, 0, sysctl_epair_netisr_maxqlen, "I",
130d0ea4743SBjoern A. Zeeb     "Maximum if_epair(4) netisr \"hw\" queue length");
131d0ea4743SBjoern A. Zeeb 
132d0ea4743SBjoern A. Zeeb struct epair_softc {
133d0ea4743SBjoern A. Zeeb 	struct ifnet	*ifp;		/* This ifp. */
134d0ea4743SBjoern A. Zeeb 	struct ifnet	*oifp;		/* other ifp of pair. */
1352dccdd45SMarko Zec 	struct ifmedia	media;		/* Media config (fake). */
136d0ea4743SBjoern A. Zeeb 	u_int		refcount;	/* # of mbufs in flight. */
137d0ea4743SBjoern A. Zeeb 	u_int		cpuid;		/* CPU ID assigned upon creation. */
138d0ea4743SBjoern A. Zeeb 	void		(*if_qflush)(struct ifnet *);
139d0ea4743SBjoern A. Zeeb 					/* Original if_qflush routine. */
140d0ea4743SBjoern A. Zeeb };
141d0ea4743SBjoern A. Zeeb 
142d0ea4743SBjoern A. Zeeb /*
143d0ea4743SBjoern A. Zeeb  * Per-CPU list of ifps with data in the ifq that needs to be flushed
144d0ea4743SBjoern A. Zeeb  * to the netisr ``hw'' queue before we allow any further direct queuing
145d0ea4743SBjoern A. Zeeb  * to the ``hw'' queue.
146d0ea4743SBjoern A. Zeeb  */
147d0ea4743SBjoern A. Zeeb struct epair_ifp_drain {
148d0ea4743SBjoern A. Zeeb 	STAILQ_ENTRY(epair_ifp_drain)	ifp_next;
149d0ea4743SBjoern A. Zeeb 	struct ifnet			*ifp;
150d0ea4743SBjoern A. Zeeb };
151d0ea4743SBjoern A. Zeeb STAILQ_HEAD(eid_list, epair_ifp_drain);
152d0ea4743SBjoern A. Zeeb 
153d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_INIT(dpcpu)		mtx_init(&(dpcpu)->if_epair_mtx, \
154d0ea4743SBjoern A. Zeeb 					    "if_epair", NULL, MTX_DEF)
155d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_DESTROY(dpcpu)	mtx_destroy(&(dpcpu)->if_epair_mtx)
156d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_ASSERT(dpcpu)	mtx_assert(&(dpcpu)->if_epair_mtx, \
157d0ea4743SBjoern A. Zeeb 					    MA_OWNED)
158d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK(dpcpu)		mtx_lock(&(dpcpu)->if_epair_mtx)
159d0ea4743SBjoern A. Zeeb #define	EPAIR_UNLOCK(dpcpu)		mtx_unlock(&(dpcpu)->if_epair_mtx)
160d0ea4743SBjoern A. Zeeb 
161d0ea4743SBjoern A. Zeeb #ifdef INVARIANTS
162d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)	refcount_init((r), (v))
163d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)	refcount_acquire((r))
164d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)	refcount_release((r))
165d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)	KASSERT(a, p)
166d0ea4743SBjoern A. Zeeb #else
167d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)
168d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)
169d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)
170d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)
171d0ea4743SBjoern A. Zeeb #endif
172d0ea4743SBjoern A. Zeeb 
17342a58907SGleb Smirnoff static MALLOC_DEFINE(M_EPAIR, epairname,
174d0ea4743SBjoern A. Zeeb     "Pair of virtual cross-over connected Ethernet-like interfaces");
17598c230c8SBjoern A. Zeeb 
1763c3136b1SHiroki Sato static VNET_DEFINE(struct if_clone *, epair_cloner);
1773c3136b1SHiroki Sato #define	V_epair_cloner	VNET(epair_cloner)
17898c230c8SBjoern A. Zeeb 
179d0ea4743SBjoern A. Zeeb /*
180d0ea4743SBjoern A. Zeeb  * DPCPU area and functions.
181d0ea4743SBjoern A. Zeeb  */
182d0ea4743SBjoern A. Zeeb struct epair_dpcpu {
183d0ea4743SBjoern A. Zeeb 	struct mtx	if_epair_mtx;		/* Per-CPU locking. */
184d0ea4743SBjoern A. Zeeb 	int		epair_drv_flags;	/* Per-CPU ``hw'' drv flags. */
185d0ea4743SBjoern A. Zeeb 	struct eid_list	epair_ifp_drain_list;	/* Per-CPU list of ifps with
186d0ea4743SBjoern A. Zeeb 						 * data in the ifq. */
187d0ea4743SBjoern A. Zeeb };
188d0ea4743SBjoern A. Zeeb DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
189d0ea4743SBjoern A. Zeeb 
190d0ea4743SBjoern A. Zeeb static void
191d0ea4743SBjoern A. Zeeb epair_dpcpu_init(void)
192d0ea4743SBjoern A. Zeeb {
193d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
194d0ea4743SBjoern A. Zeeb 	struct eid_list *s;
195d0ea4743SBjoern A. Zeeb 	u_int cpuid;
196d0ea4743SBjoern A. Zeeb 
1973aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
198d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
199d0ea4743SBjoern A. Zeeb 
200d0ea4743SBjoern A. Zeeb 		/* Initialize per-cpu lock. */
201d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_INIT(epair_dpcpu);
202d0ea4743SBjoern A. Zeeb 
203d0ea4743SBjoern A. Zeeb 		/* Driver flags are per-cpu as are our netisr "hw" queues. */
204d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags = 0;
205d0ea4743SBjoern A. Zeeb 
206d0ea4743SBjoern A. Zeeb 		/*
207d0ea4743SBjoern A. Zeeb 		 * Initialize per-cpu drain list.
208d0ea4743SBjoern A. Zeeb 		 * Manually do what STAILQ_HEAD_INITIALIZER would do.
209d0ea4743SBjoern A. Zeeb 		 */
210d0ea4743SBjoern A. Zeeb 		s = &epair_dpcpu->epair_ifp_drain_list;
211d0ea4743SBjoern A. Zeeb 		s->stqh_first = NULL;
212d0ea4743SBjoern A. Zeeb 		s->stqh_last = &s->stqh_first;
213d0ea4743SBjoern A. Zeeb 	}
214d0ea4743SBjoern A. Zeeb }
215d0ea4743SBjoern A. Zeeb 
216d0ea4743SBjoern A. Zeeb static void
217d0ea4743SBjoern A. Zeeb epair_dpcpu_detach(void)
218d0ea4743SBjoern A. Zeeb {
219d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
220d0ea4743SBjoern A. Zeeb 	u_int cpuid;
221d0ea4743SBjoern A. Zeeb 
2223aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
223d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
224d0ea4743SBjoern A. Zeeb 
225d0ea4743SBjoern A. Zeeb 		/* Destroy per-cpu lock. */
226d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_DESTROY(epair_dpcpu);
227d0ea4743SBjoern A. Zeeb 	}
228d0ea4743SBjoern A. Zeeb }
229d0ea4743SBjoern A. Zeeb 
230d0ea4743SBjoern A. Zeeb /*
231d0ea4743SBjoern A. Zeeb  * Helper functions.
232d0ea4743SBjoern A. Zeeb  */
233d0ea4743SBjoern A. Zeeb static u_int
234d0ea4743SBjoern A. Zeeb cpuid_from_ifp(struct ifnet *ifp)
235d0ea4743SBjoern A. Zeeb {
236d0ea4743SBjoern A. Zeeb 	struct epair_softc *sc;
237d0ea4743SBjoern A. Zeeb 
238d0ea4743SBjoern A. Zeeb 	if (ifp == NULL)
239d0ea4743SBjoern A. Zeeb 		return (0);
240d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
241d0ea4743SBjoern A. Zeeb 
242d0ea4743SBjoern A. Zeeb 	return (sc->cpuid);
243d0ea4743SBjoern A. Zeeb }
24498c230c8SBjoern A. Zeeb 
24598c230c8SBjoern A. Zeeb /*
24698c230c8SBjoern A. Zeeb  * Netisr handler functions.
24798c230c8SBjoern A. Zeeb  */
24898c230c8SBjoern A. Zeeb static void
249d0ea4743SBjoern A. Zeeb epair_nh_sintr(struct mbuf *m)
25098c230c8SBjoern A. Zeeb {
25198c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
25298c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
25398c230c8SBjoern A. Zeeb 
25498c230c8SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
25598c230c8SBjoern A. Zeeb 	(*ifp->if_input)(ifp, m);
25698c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
257d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_RELEASE(&sc->refcount);
258eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
259eea3faf7SBjoern A. Zeeb 	    ("%s: ifp=%p sc->refcount not >= 1: %d",
260eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc->refcount));
26198c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
26298c230c8SBjoern A. Zeeb }
26398c230c8SBjoern A. Zeeb 
264d0ea4743SBjoern A. Zeeb static struct mbuf *
265d0ea4743SBjoern A. Zeeb epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
26698c230c8SBjoern A. Zeeb {
267d0ea4743SBjoern A. Zeeb 
268d0ea4743SBjoern A. Zeeb 	*cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
269d0ea4743SBjoern A. Zeeb 
270d0ea4743SBjoern A. Zeeb 	return (m);
271d0ea4743SBjoern A. Zeeb }
272d0ea4743SBjoern A. Zeeb 
273d0ea4743SBjoern A. Zeeb static void
274d0ea4743SBjoern A. Zeeb epair_nh_drainedcpu(u_int cpuid)
275d0ea4743SBjoern A. Zeeb {
276d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
27798c230c8SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
27898c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
27998c230c8SBjoern A. Zeeb 
280d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
281d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
28298c230c8SBjoern A. Zeeb 	/*
28398c230c8SBjoern A. Zeeb 	 * Assume our "hw" queue and possibly ifq will be emptied
28498c230c8SBjoern A. Zeeb 	 * again. In case we will overflow the "hw" queue while
28598c230c8SBjoern A. Zeeb 	 * draining, epair_start_locked will set IFF_DRV_OACTIVE
28698c230c8SBjoern A. Zeeb 	 * again and we will stop and return.
28798c230c8SBjoern A. Zeeb 	 */
288d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
289d0ea4743SBjoern A. Zeeb 	    ifp_next, tvar) {
29098c230c8SBjoern A. Zeeb 		ifp = elm->ifp;
291d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
29298c230c8SBjoern A. Zeeb 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
29398c230c8SBjoern A. Zeeb 		epair_start_locked(ifp);
29498c230c8SBjoern A. Zeeb 
29598c230c8SBjoern A. Zeeb 		IFQ_LOCK(&ifp->if_snd);
29698c230c8SBjoern A. Zeeb 		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
297eea3faf7SBjoern A. Zeeb 			struct epair_softc *sc;
298eea3faf7SBjoern A. Zeeb 
299d0ea4743SBjoern A. Zeeb 			STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
300d0ea4743SBjoern A. Zeeb 			    elm, epair_ifp_drain, ifp_next);
301eea3faf7SBjoern A. Zeeb 			/* The cached ifp goes off the list. */
302eea3faf7SBjoern A. Zeeb 			sc = ifp->if_softc;
303eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
304eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
305eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
306eea3faf7SBjoern A. Zeeb 			    __func__, ifp, sc->refcount));
30798c230c8SBjoern A. Zeeb 			free(elm, M_EPAIR);
30898c230c8SBjoern A. Zeeb 		}
30998c230c8SBjoern A. Zeeb 		IFQ_UNLOCK(&ifp->if_snd);
31098c230c8SBjoern A. Zeeb 
31198c230c8SBjoern A. Zeeb 		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
31298c230c8SBjoern A. Zeeb 			/* Our "hw"q overflew again. */
3132c8b047cSBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
31498c230c8SBjoern A. Zeeb 			DPRINTF("hw queue length overflow at %u\n",
315d0ea4743SBjoern A. Zeeb 			    epair_nh.nh_qlimit);
31698c230c8SBjoern A. Zeeb 			break;
31798c230c8SBjoern A. Zeeb 		}
31898c230c8SBjoern A. Zeeb 	}
319d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
32098c230c8SBjoern A. Zeeb }
32198c230c8SBjoern A. Zeeb 
32298c230c8SBjoern A. Zeeb /*
32398c230c8SBjoern A. Zeeb  * Network interface (`if') related functions.
32498c230c8SBjoern A. Zeeb  */
325eea3faf7SBjoern A. Zeeb static void
326eea3faf7SBjoern A. Zeeb epair_remove_ifp_from_draining(struct ifnet *ifp)
327eea3faf7SBjoern A. Zeeb {
328eea3faf7SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
329eea3faf7SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
330eea3faf7SBjoern A. Zeeb 	u_int cpuid;
331eea3faf7SBjoern A. Zeeb 
3323aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
333eea3faf7SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
334eea3faf7SBjoern A. Zeeb 		EPAIR_LOCK(epair_dpcpu);
335eea3faf7SBjoern A. Zeeb 		STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
336eea3faf7SBjoern A. Zeeb 		    ifp_next, tvar) {
337eea3faf7SBjoern A. Zeeb 			if (ifp == elm->ifp) {
338eea3faf7SBjoern A. Zeeb 				struct epair_softc *sc;
339eea3faf7SBjoern A. Zeeb 
340eea3faf7SBjoern A. Zeeb 				STAILQ_REMOVE(
341eea3faf7SBjoern A. Zeeb 				    &epair_dpcpu->epair_ifp_drain_list, elm,
342eea3faf7SBjoern A. Zeeb 				    epair_ifp_drain, ifp_next);
343eea3faf7SBjoern A. Zeeb 				/* The cached ifp goes off the list. */
344eea3faf7SBjoern A. Zeeb 				sc = ifp->if_softc;
345eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_RELEASE(&sc->refcount);
346eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
347eea3faf7SBjoern A. Zeeb 				    ("%s: ifp=%p sc->refcount not >= 1: %d",
348eea3faf7SBjoern A. Zeeb 				    __func__, ifp, sc->refcount));
349eea3faf7SBjoern A. Zeeb 				free(elm, M_EPAIR);
350eea3faf7SBjoern A. Zeeb 			}
351eea3faf7SBjoern A. Zeeb 		}
352eea3faf7SBjoern A. Zeeb 		EPAIR_UNLOCK(epair_dpcpu);
353eea3faf7SBjoern A. Zeeb 	}
354eea3faf7SBjoern A. Zeeb }
355eea3faf7SBjoern A. Zeeb 
356d0ea4743SBjoern A. Zeeb static int
357d0ea4743SBjoern A. Zeeb epair_add_ifp_for_draining(struct ifnet *ifp)
358d0ea4743SBjoern A. Zeeb {
359d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
360eea3faf7SBjoern A. Zeeb 	struct epair_softc *sc;
361d0ea4743SBjoern A. Zeeb 	struct epair_ifp_drain *elm = NULL;
362d0ea4743SBjoern A. Zeeb 
363eea3faf7SBjoern A. Zeeb 	sc = ifp->if_softc;
364d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
365eea3faf7SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
366d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
367d0ea4743SBjoern A. Zeeb 		if (elm->ifp == ifp)
368d0ea4743SBjoern A. Zeeb 			break;
3693c20163aSBjoern A. Zeeb 	/* If the ifp is there already, return success. */
370d0ea4743SBjoern A. Zeeb 	if (elm != NULL)
371d0ea4743SBjoern A. Zeeb 		return (0);
372d0ea4743SBjoern A. Zeeb 
373d0ea4743SBjoern A. Zeeb 	elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
374d0ea4743SBjoern A. Zeeb 	if (elm == NULL)
375d0ea4743SBjoern A. Zeeb 		return (ENOMEM);
376d0ea4743SBjoern A. Zeeb 
377d0ea4743SBjoern A. Zeeb 	elm->ifp = ifp;
378eea3faf7SBjoern A. Zeeb 	/* Add a reference for the ifp pointer on the list. */
379eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
380d0ea4743SBjoern A. Zeeb 	STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
381d0ea4743SBjoern A. Zeeb 
382d0ea4743SBjoern A. Zeeb 	return (0);
383d0ea4743SBjoern A. Zeeb }
384d0ea4743SBjoern A. Zeeb 
38598c230c8SBjoern A. Zeeb static void
38698c230c8SBjoern A. Zeeb epair_start_locked(struct ifnet *ifp)
38798c230c8SBjoern A. Zeeb {
388d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
38998c230c8SBjoern A. Zeeb 	struct mbuf *m;
39098c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
39198c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
39298c230c8SBjoern A. Zeeb 	int error;
39398c230c8SBjoern A. Zeeb 
39498c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
395d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
396d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
397d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
39898c230c8SBjoern A. Zeeb 
39998c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
40098c230c8SBjoern A. Zeeb 		return;
40198c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0)
40298c230c8SBjoern A. Zeeb 		return;
40398c230c8SBjoern A. Zeeb 
40498c230c8SBjoern A. Zeeb 	/*
40598c230c8SBjoern A. Zeeb 	 * We get patckets here from ether_output via if_handoff()
40698c230c8SBjoern A. Zeeb 	 * and ned to put them into the input queue of the oifp
40798c230c8SBjoern A. Zeeb 	 * and call oifp->if_input() via netisr/epair_sintr().
40898c230c8SBjoern A. Zeeb 	 */
40998c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
41098c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
41198c230c8SBjoern A. Zeeb 	for (;;) {
41298c230c8SBjoern A. Zeeb 		IFQ_DEQUEUE(&ifp->if_snd, m);
41398c230c8SBjoern A. Zeeb 		if (m == NULL)
41498c230c8SBjoern A. Zeeb 			break;
41598c230c8SBjoern A. Zeeb 		BPF_MTAP(ifp, m);
41698c230c8SBjoern A. Zeeb 
41798c230c8SBjoern A. Zeeb 		/*
41898c230c8SBjoern A. Zeeb 		 * In case the outgoing interface is not usable,
41998c230c8SBjoern A. Zeeb 		 * drop the packet.
42098c230c8SBjoern A. Zeeb 		 */
42198c230c8SBjoern A. Zeeb 		if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
42298c230c8SBjoern A. Zeeb 		    (oifp->if_flags & IFF_UP) ==0) {
4233751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
42498c230c8SBjoern A. Zeeb 			m_freem(m);
42598c230c8SBjoern A. Zeeb 			continue;
42698c230c8SBjoern A. Zeeb 		}
42798c230c8SBjoern A. Zeeb 		DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
42898c230c8SBjoern A. Zeeb 
42998c230c8SBjoern A. Zeeb 		/*
43098c230c8SBjoern A. Zeeb 		 * Add a reference so the interface cannot go while the
43198c230c8SBjoern A. Zeeb 		 * packet is in transit as we rely on rcvif to stay valid.
43298c230c8SBjoern A. Zeeb 		 */
433d0ea4743SBjoern A. Zeeb 		EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
43498c230c8SBjoern A. Zeeb 		m->m_pkthdr.rcvif = oifp;
43598c230c8SBjoern A. Zeeb 		CURVNET_SET_QUIET(oifp->if_vnet);
43698c230c8SBjoern A. Zeeb 		error = netisr_queue(NETISR_EPAIR, m);
43798c230c8SBjoern A. Zeeb 		CURVNET_RESTORE();
43898c230c8SBjoern A. Zeeb 		if (!error) {
4393751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
44098c230c8SBjoern A. Zeeb 			/* Someone else received the packet. */
4413751dddbSGleb Smirnoff 			if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
44298c230c8SBjoern A. Zeeb 		} else {
443eea3faf7SBjoern A. Zeeb 			/* The packet was freed already. */
444d0ea4743SBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
44598c230c8SBjoern A. Zeeb 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
446eea3faf7SBjoern A. Zeeb 			(void) epair_add_ifp_for_draining(ifp);
4473751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
448d0ea4743SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
449eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
450eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
451eea3faf7SBjoern A. Zeeb 			    __func__, oifp, sc->refcount));
45298c230c8SBjoern A. Zeeb 		}
45398c230c8SBjoern A. Zeeb 	}
45498c230c8SBjoern A. Zeeb }
45598c230c8SBjoern A. Zeeb 
45698c230c8SBjoern A. Zeeb static void
45798c230c8SBjoern A. Zeeb epair_start(struct ifnet *ifp)
45898c230c8SBjoern A. Zeeb {
459d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
46098c230c8SBjoern A. Zeeb 
461d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
462d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
46398c230c8SBjoern A. Zeeb 	epair_start_locked(ifp);
464d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
46598c230c8SBjoern A. Zeeb }
46698c230c8SBjoern A. Zeeb 
46798c230c8SBjoern A. Zeeb static int
46898c230c8SBjoern A. Zeeb epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
46998c230c8SBjoern A. Zeeb {
470d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
47198c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
47298c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
47398c230c8SBjoern A. Zeeb 	int error, len;
47498c230c8SBjoern A. Zeeb 	short mflags;
47598c230c8SBjoern A. Zeeb 
47698c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p m=%p\n", ifp, m);
477d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
478d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
479d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
48098c230c8SBjoern A. Zeeb 
48198c230c8SBjoern A. Zeeb 	if (m == NULL)
48298c230c8SBjoern A. Zeeb 		return (0);
48398c230c8SBjoern A. Zeeb 
48498c230c8SBjoern A. Zeeb 	/*
48598c230c8SBjoern A. Zeeb 	 * We are not going to use the interface en/dequeue mechanism
48698c230c8SBjoern A. Zeeb 	 * on the TX side. We are called from ether_output_frame()
48798c230c8SBjoern A. Zeeb 	 * and will put the packet into the incoming queue of the
48898c230c8SBjoern A. Zeeb 	 * other interface of our pair via the netsir.
48998c230c8SBjoern A. Zeeb 	 */
49098c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
49198c230c8SBjoern A. Zeeb 		m_freem(m);
49298c230c8SBjoern A. Zeeb 		return (ENXIO);
49398c230c8SBjoern A. Zeeb 	}
49498c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0) {
49598c230c8SBjoern A. Zeeb 		m_freem(m);
49698c230c8SBjoern A. Zeeb 		return (ENETDOWN);
49798c230c8SBjoern A. Zeeb 	}
49898c230c8SBjoern A. Zeeb 
49998c230c8SBjoern A. Zeeb 	BPF_MTAP(ifp, m);
50098c230c8SBjoern A. Zeeb 
50198c230c8SBjoern A. Zeeb 	/*
50298c230c8SBjoern A. Zeeb 	 * In case the outgoing interface is not usable,
50398c230c8SBjoern A. Zeeb 	 * drop the packet.
50498c230c8SBjoern A. Zeeb 	 */
50598c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
50698c230c8SBjoern A. Zeeb 	if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
50798c230c8SBjoern A. Zeeb 	    (oifp->if_flags & IFF_UP) ==0) {
5083751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
50998c230c8SBjoern A. Zeeb 		m_freem(m);
51098c230c8SBjoern A. Zeeb 		return (0);
51198c230c8SBjoern A. Zeeb 	}
51298c230c8SBjoern A. Zeeb 	len = m->m_pkthdr.len;
51398c230c8SBjoern A. Zeeb 	mflags = m->m_flags;
51498c230c8SBjoern A. Zeeb 	DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
51598c230c8SBjoern A. Zeeb 
51698c230c8SBjoern A. Zeeb #ifdef ALTQ
517a4641f4eSPedro F. Giffuni 	/* Support ALTQ via the classic if_start() path. */
51898c230c8SBjoern A. Zeeb 	IF_LOCK(&ifp->if_snd);
51998c230c8SBjoern A. Zeeb 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
52098c230c8SBjoern A. Zeeb 		ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
52198c230c8SBjoern A. Zeeb 		if (error)
5223751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
52398c230c8SBjoern A. Zeeb 		IF_UNLOCK(&ifp->if_snd);
52498c230c8SBjoern A. Zeeb 		if (!error) {
5253751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
52698c230c8SBjoern A. Zeeb 			if (mflags & (M_BCAST|M_MCAST))
5273751dddbSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
52898c230c8SBjoern A. Zeeb 
52998c230c8SBjoern A. Zeeb 			if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
53098c230c8SBjoern A. Zeeb 				epair_start_locked(ifp);
53198c230c8SBjoern A. Zeeb 			else
532d0ea4743SBjoern A. Zeeb 				(void)epair_add_ifp_for_draining(ifp);
53398c230c8SBjoern A. Zeeb 		}
53498c230c8SBjoern A. Zeeb 		return (error);
53598c230c8SBjoern A. Zeeb 	}
53698c230c8SBjoern A. Zeeb 	IF_UNLOCK(&ifp->if_snd);
53798c230c8SBjoern A. Zeeb #endif
53898c230c8SBjoern A. Zeeb 
539d0ea4743SBjoern A. Zeeb 	if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
54098c230c8SBjoern A. Zeeb 		/*
54198c230c8SBjoern A. Zeeb 		 * Our hardware queue is full, try to fall back
54298c230c8SBjoern A. Zeeb 		 * queuing to the ifq but do not call ifp->if_start.
54398c230c8SBjoern A. Zeeb 		 * Either we are lucky or the packet is gone.
54498c230c8SBjoern A. Zeeb 		 */
54598c230c8SBjoern A. Zeeb 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
54698c230c8SBjoern A. Zeeb 		if (!error)
547d0ea4743SBjoern A. Zeeb 			(void)epair_add_ifp_for_draining(ifp);
54898c230c8SBjoern A. Zeeb 		return (error);
54998c230c8SBjoern A. Zeeb 	}
55098c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
55198c230c8SBjoern A. Zeeb 	/*
55298c230c8SBjoern A. Zeeb 	 * Add a reference so the interface cannot go while the
55398c230c8SBjoern A. Zeeb 	 * packet is in transit as we rely on rcvif to stay valid.
55498c230c8SBjoern A. Zeeb 	 */
555d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
55698c230c8SBjoern A. Zeeb 	m->m_pkthdr.rcvif = oifp;
55798c230c8SBjoern A. Zeeb 	CURVNET_SET_QUIET(oifp->if_vnet);
55898c230c8SBjoern A. Zeeb 	error = netisr_queue(NETISR_EPAIR, m);
55998c230c8SBjoern A. Zeeb 	CURVNET_RESTORE();
56098c230c8SBjoern A. Zeeb 	if (!error) {
5613751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
56298c230c8SBjoern A. Zeeb 		/*
56398c230c8SBjoern A. Zeeb 		 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
56498c230c8SBjoern A. Zeeb 		 * but as we bypass all this we have to duplicate
56598c230c8SBjoern A. Zeeb 		 * the logic another time.
56698c230c8SBjoern A. Zeeb 		 */
5673751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
56898c230c8SBjoern A. Zeeb 		if (mflags & (M_BCAST|M_MCAST))
5693751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
57098c230c8SBjoern A. Zeeb 		/* Someone else received the packet. */
5713751dddbSGleb Smirnoff 		if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
57298c230c8SBjoern A. Zeeb 	} else {
57398c230c8SBjoern A. Zeeb 		/* The packet was freed already. */
574d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
57598c230c8SBjoern A. Zeeb 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5763751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
577eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_RELEASE(&sc->refcount);
578eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
579eea3faf7SBjoern A. Zeeb 		    ("%s: ifp=%p sc->refcount not >= 1: %d",
580eea3faf7SBjoern A. Zeeb 		    __func__, oifp, sc->refcount));
58198c230c8SBjoern A. Zeeb 	}
58298c230c8SBjoern A. Zeeb 
58398c230c8SBjoern A. Zeeb 	return (error);
58498c230c8SBjoern A. Zeeb }
58598c230c8SBjoern A. Zeeb 
58698c230c8SBjoern A. Zeeb static int
58798c230c8SBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m)
58898c230c8SBjoern A. Zeeb {
589d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
59098c230c8SBjoern A. Zeeb 	int error;
59198c230c8SBjoern A. Zeeb 
592d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
593d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
59498c230c8SBjoern A. Zeeb 	error = epair_transmit_locked(ifp, m);
595d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
59698c230c8SBjoern A. Zeeb 	return (error);
59798c230c8SBjoern A. Zeeb }
59898c230c8SBjoern A. Zeeb 
59998c230c8SBjoern A. Zeeb static void
60098c230c8SBjoern A. Zeeb epair_qflush(struct ifnet *ifp)
60198c230c8SBjoern A. Zeeb {
60298c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
60398c230c8SBjoern A. Zeeb 
60498c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
605eea3faf7SBjoern A. Zeeb 	KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
606eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc));
60798c230c8SBjoern A. Zeeb 	/*
608eea3faf7SBjoern A. Zeeb 	 * Remove this ifp from all backpointer lists. The interface will not
609eea3faf7SBjoern A. Zeeb 	 * usable for flushing anyway nor should it have anything to flush
610eea3faf7SBjoern A. Zeeb 	 * after if_qflush().
61198c230c8SBjoern A. Zeeb 	 */
612eea3faf7SBjoern A. Zeeb 	epair_remove_ifp_from_draining(ifp);
613eea3faf7SBjoern A. Zeeb 
61498c230c8SBjoern A. Zeeb 	if (sc->if_qflush)
61598c230c8SBjoern A. Zeeb 		sc->if_qflush(ifp);
61698c230c8SBjoern A. Zeeb }
61798c230c8SBjoern A. Zeeb 
61898c230c8SBjoern A. Zeeb static int
6192dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused)
6202dccdd45SMarko Zec {
6212dccdd45SMarko Zec 
6222dccdd45SMarko Zec 	/* Do nothing. */
6232dccdd45SMarko Zec 	return (0);
6242dccdd45SMarko Zec }
6252dccdd45SMarko Zec 
6262dccdd45SMarko Zec static void
6272dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
6282dccdd45SMarko Zec {
6292dccdd45SMarko Zec 
6302dccdd45SMarko Zec 	imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
6312dccdd45SMarko Zec 	imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
6322dccdd45SMarko Zec }
6332dccdd45SMarko Zec 
6342dccdd45SMarko Zec static int
63598c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
63698c230c8SBjoern A. Zeeb {
6372dccdd45SMarko Zec 	struct epair_softc *sc;
63898c230c8SBjoern A. Zeeb 	struct ifreq *ifr;
63998c230c8SBjoern A. Zeeb 	int error;
64098c230c8SBjoern A. Zeeb 
64198c230c8SBjoern A. Zeeb 	ifr = (struct ifreq *)data;
64298c230c8SBjoern A. Zeeb 	switch (cmd) {
64398c230c8SBjoern A. Zeeb 	case SIOCSIFFLAGS:
64498c230c8SBjoern A. Zeeb 	case SIOCADDMULTI:
64598c230c8SBjoern A. Zeeb 	case SIOCDELMULTI:
64698c230c8SBjoern A. Zeeb 		error = 0;
64798c230c8SBjoern A. Zeeb 		break;
64898c230c8SBjoern A. Zeeb 
6492dccdd45SMarko Zec 	case SIOCSIFMEDIA:
6502dccdd45SMarko Zec 	case SIOCGIFMEDIA:
6512dccdd45SMarko Zec 		sc = ifp->if_softc;
6522dccdd45SMarko Zec 		error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
6532dccdd45SMarko Zec 		break;
6542dccdd45SMarko Zec 
655d0ea4743SBjoern A. Zeeb 	case SIOCSIFMTU:
656d0ea4743SBjoern A. Zeeb 		/* We basically allow all kinds of MTUs. */
657d0ea4743SBjoern A. Zeeb 		ifp->if_mtu = ifr->ifr_mtu;
658d0ea4743SBjoern A. Zeeb 		error = 0;
659d0ea4743SBjoern A. Zeeb 		break;
660d0ea4743SBjoern A. Zeeb 
66198c230c8SBjoern A. Zeeb 	default:
66298c230c8SBjoern A. Zeeb 		/* Let the common ethernet handler process this. */
66398c230c8SBjoern A. Zeeb 		error = ether_ioctl(ifp, cmd, data);
66498c230c8SBjoern A. Zeeb 		break;
66598c230c8SBjoern A. Zeeb 	}
66698c230c8SBjoern A. Zeeb 
66798c230c8SBjoern A. Zeeb 	return (error);
66898c230c8SBjoern A. Zeeb }
66998c230c8SBjoern A. Zeeb 
67098c230c8SBjoern A. Zeeb static void
67198c230c8SBjoern A. Zeeb epair_init(void *dummy __unused)
67298c230c8SBjoern A. Zeeb {
67398c230c8SBjoern A. Zeeb }
67498c230c8SBjoern A. Zeeb 
67598c230c8SBjoern A. Zeeb 
67698c230c8SBjoern A. Zeeb /*
67798c230c8SBjoern A. Zeeb  * Interface cloning functions.
67898c230c8SBjoern A. Zeeb  * We use our private ones so that we can create/destroy our secondary
67998c230c8SBjoern A. Zeeb  * device along with the primary one.
68098c230c8SBjoern A. Zeeb  */
68198c230c8SBjoern A. Zeeb static int
68298c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name)
68398c230c8SBjoern A. Zeeb {
68498c230c8SBjoern A. Zeeb 	const char *cp;
68598c230c8SBjoern A. Zeeb 
68698c230c8SBjoern A. Zeeb 	DPRINTF("name='%s'\n", name);
68798c230c8SBjoern A. Zeeb 
68898c230c8SBjoern A. Zeeb 	/*
68998c230c8SBjoern A. Zeeb 	 * Our base name is epair.
69098c230c8SBjoern A. Zeeb 	 * Our interfaces will be named epair<n>[ab].
69198c230c8SBjoern A. Zeeb 	 * So accept anything of the following list:
69298c230c8SBjoern A. Zeeb 	 * - epair
69398c230c8SBjoern A. Zeeb 	 * - epair<n>
69498c230c8SBjoern A. Zeeb 	 * but not the epair<n>[ab] versions.
69598c230c8SBjoern A. Zeeb 	 */
69642a58907SGleb Smirnoff 	if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
69798c230c8SBjoern A. Zeeb 		return (0);
69898c230c8SBjoern A. Zeeb 
69942a58907SGleb Smirnoff 	for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
70098c230c8SBjoern A. Zeeb 		if (*cp < '0' || *cp > '9')
70198c230c8SBjoern A. Zeeb 			return (0);
70298c230c8SBjoern A. Zeeb 	}
70398c230c8SBjoern A. Zeeb 
70498c230c8SBjoern A. Zeeb 	return (1);
70598c230c8SBjoern A. Zeeb }
70698c230c8SBjoern A. Zeeb 
70798c230c8SBjoern A. Zeeb static int
70898c230c8SBjoern A. Zeeb epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
70998c230c8SBjoern A. Zeeb {
71098c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
71198c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
71298c230c8SBjoern A. Zeeb 	char *dp;
71398c230c8SBjoern A. Zeeb 	int error, unit, wildcard;
71498c230c8SBjoern A. Zeeb 	uint8_t eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
71598c230c8SBjoern A. Zeeb 
71698c230c8SBjoern A. Zeeb 	/*
71798c230c8SBjoern A. Zeeb 	 * We are abusing params to create our second interface.
71842a58907SGleb Smirnoff 	 * Actually we already created it and called if_clone_create()
71998c230c8SBjoern A. Zeeb 	 * for it to do the official insertion procedure the moment we knew
72098c230c8SBjoern A. Zeeb 	 * it cannot fail anymore. So just do attach it here.
72198c230c8SBjoern A. Zeeb 	 */
72298c230c8SBjoern A. Zeeb 	if (params) {
72398c230c8SBjoern A. Zeeb 		scb = (struct epair_softc *)params;
72498c230c8SBjoern A. Zeeb 		ifp = scb->ifp;
72598c230c8SBjoern A. Zeeb 		/* Assign a hopefully unique, locally administered etheraddr. */
72698c230c8SBjoern A. Zeeb 		eaddr[0] = 0x02;
72798c230c8SBjoern A. Zeeb 		eaddr[3] = (ifp->if_index >> 8) & 0xff;
72898c230c8SBjoern A. Zeeb 		eaddr[4] = ifp->if_index & 0xff;
72998c230c8SBjoern A. Zeeb 		eaddr[5] = 0x0b;
73098c230c8SBjoern A. Zeeb 		ether_ifattach(ifp, eaddr);
73198c230c8SBjoern A. Zeeb 		/* Correctly set the name for the cloner list. */
73298c230c8SBjoern A. Zeeb 		strlcpy(name, scb->ifp->if_xname, len);
73398c230c8SBjoern A. Zeeb 		return (0);
73498c230c8SBjoern A. Zeeb 	}
73598c230c8SBjoern A. Zeeb 
73698c230c8SBjoern A. Zeeb 	/* Try to see if a special unit was requested. */
73798c230c8SBjoern A. Zeeb 	error = ifc_name2unit(name, &unit);
73898c230c8SBjoern A. Zeeb 	if (error != 0)
73998c230c8SBjoern A. Zeeb 		return (error);
74098c230c8SBjoern A. Zeeb 	wildcard = (unit < 0);
74198c230c8SBjoern A. Zeeb 
74298c230c8SBjoern A. Zeeb 	error = ifc_alloc_unit(ifc, &unit);
74398c230c8SBjoern A. Zeeb 	if (error != 0)
74498c230c8SBjoern A. Zeeb 		return (error);
74598c230c8SBjoern A. Zeeb 
74698c230c8SBjoern A. Zeeb 	/*
74798c230c8SBjoern A. Zeeb 	 * If no unit had been given, we need to adjust the ifName.
74898c230c8SBjoern A. Zeeb 	 * Also make sure there is space for our extra [ab] suffix.
74998c230c8SBjoern A. Zeeb 	 */
75098c230c8SBjoern A. Zeeb 	for (dp = name; *dp != '\0'; dp++);
75198c230c8SBjoern A. Zeeb 	if (wildcard) {
75298c230c8SBjoern A. Zeeb 		error = snprintf(dp, len - (dp - name), "%d", unit);
75398c230c8SBjoern A. Zeeb 		if (error > len - (dp - name) - 1) {
75498c230c8SBjoern A. Zeeb 			/* ifName too long. */
75598c230c8SBjoern A. Zeeb 			ifc_free_unit(ifc, unit);
75698c230c8SBjoern A. Zeeb 			return (ENOSPC);
75798c230c8SBjoern A. Zeeb 		}
75898c230c8SBjoern A. Zeeb 		dp += error;
75998c230c8SBjoern A. Zeeb 	}
76098c230c8SBjoern A. Zeeb 	if (len - (dp - name) - 1 < 1) {
76198c230c8SBjoern A. Zeeb 		/* No space left for our [ab] suffix. */
76298c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
76398c230c8SBjoern A. Zeeb 		return (ENOSPC);
76498c230c8SBjoern A. Zeeb 	}
7653c3136b1SHiroki Sato 	*dp = 'b';
76698c230c8SBjoern A. Zeeb 	/* Must not change dp so we can replace 'a' by 'b' later. */
76798c230c8SBjoern A. Zeeb 	*(dp+1) = '\0';
76898c230c8SBjoern A. Zeeb 
7693c3136b1SHiroki Sato 	/* Check if 'a' and 'b' interfaces already exist. */
7703c3136b1SHiroki Sato 	if (ifunit(name) != NULL)
7713c3136b1SHiroki Sato 		return (EEXIST);
7723c3136b1SHiroki Sato 	*dp = 'a';
7733c3136b1SHiroki Sato 	if (ifunit(name) != NULL)
7743c3136b1SHiroki Sato 		return (EEXIST);
7753c3136b1SHiroki Sato 
77698c230c8SBjoern A. Zeeb 	/* Allocate memory for both [ab] interfaces */
77798c230c8SBjoern A. Zeeb 	sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
778d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
77998c230c8SBjoern A. Zeeb 	sca->ifp = if_alloc(IFT_ETHER);
78098c230c8SBjoern A. Zeeb 	if (sca->ifp == NULL) {
78198c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
78298c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
78398c230c8SBjoern A. Zeeb 		return (ENOSPC);
78498c230c8SBjoern A. Zeeb 	}
78598c230c8SBjoern A. Zeeb 
78698c230c8SBjoern A. Zeeb 	scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
787d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
78898c230c8SBjoern A. Zeeb 	scb->ifp = if_alloc(IFT_ETHER);
78998c230c8SBjoern A. Zeeb 	if (scb->ifp == NULL) {
79098c230c8SBjoern A. Zeeb 		free(scb, M_EPAIR);
79198c230c8SBjoern A. Zeeb 		if_free(sca->ifp);
79298c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
79398c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
79498c230c8SBjoern A. Zeeb 		return (ENOSPC);
79598c230c8SBjoern A. Zeeb 	}
79698c230c8SBjoern A. Zeeb 
79798c230c8SBjoern A. Zeeb 	/*
79898c230c8SBjoern A. Zeeb 	 * Cross-reference the interfaces so we will be able to free both.
79998c230c8SBjoern A. Zeeb 	 */
80098c230c8SBjoern A. Zeeb 	sca->oifp = scb->ifp;
80198c230c8SBjoern A. Zeeb 	scb->oifp = sca->ifp;
80298c230c8SBjoern A. Zeeb 
803d0ea4743SBjoern A. Zeeb 	/*
804d0ea4743SBjoern A. Zeeb 	 * Calculate the cpuid for netisr queueing based on the
805d0ea4743SBjoern A. Zeeb 	 * ifIndex of the interfaces. As long as we cannot configure
806d0ea4743SBjoern A. Zeeb 	 * this or use cpuset information easily we cannot guarantee
807d0ea4743SBjoern A. Zeeb 	 * cache locality but we can at least allow parallelism.
808d0ea4743SBjoern A. Zeeb 	 */
809d0ea4743SBjoern A. Zeeb 	sca->cpuid =
810d0ea4743SBjoern A. Zeeb 	    netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
811d0ea4743SBjoern A. Zeeb 	scb->cpuid =
812d0ea4743SBjoern A. Zeeb 	    netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
813d0ea4743SBjoern A. Zeeb 
81418e199adSHiroki Sato 	/* Initialise pseudo media types. */
81518e199adSHiroki Sato 	ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
81618e199adSHiroki Sato 	ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
81718e199adSHiroki Sato 	ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
81818e199adSHiroki Sato 	ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
81918e199adSHiroki Sato 	ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
82018e199adSHiroki Sato 	ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
82118e199adSHiroki Sato 
82298c230c8SBjoern A. Zeeb 	/* Finish initialization of interface <n>a. */
82398c230c8SBjoern A. Zeeb 	ifp = sca->ifp;
82498c230c8SBjoern A. Zeeb 	ifp->if_softc = sca;
82598c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
82642a58907SGleb Smirnoff 	ifp->if_dname = epairname;
82798c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
82898c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8299f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8309f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
83198c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
83298c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
83398c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
83498c230c8SBjoern A. Zeeb 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
83598c230c8SBjoern A. Zeeb 	/* Assign a hopefully unique, locally administered etheraddr. */
83698c230c8SBjoern A. Zeeb 	eaddr[0] = 0x02;
83798c230c8SBjoern A. Zeeb 	eaddr[3] = (ifp->if_index >> 8) & 0xff;
83898c230c8SBjoern A. Zeeb 	eaddr[4] = ifp->if_index & 0xff;
83998c230c8SBjoern A. Zeeb 	eaddr[5] = 0x0a;
84098c230c8SBjoern A. Zeeb 	ether_ifattach(ifp, eaddr);
84198c230c8SBjoern A. Zeeb 	sca->if_qflush = ifp->if_qflush;
84298c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
84398c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
844b245f96cSGleb Smirnoff 	ifp->if_baudrate = IF_Gbps(10);	/* arbitrary maximum */
84598c230c8SBjoern A. Zeeb 
84698c230c8SBjoern A. Zeeb 	/* Swap the name and finish initialization of interface <n>b. */
84798c230c8SBjoern A. Zeeb 	*dp = 'b';
84898c230c8SBjoern A. Zeeb 
84998c230c8SBjoern A. Zeeb 	ifp = scb->ifp;
85098c230c8SBjoern A. Zeeb 	ifp->if_softc = scb;
85198c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
85242a58907SGleb Smirnoff 	ifp->if_dname = epairname;
85398c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
85498c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8559f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8569f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
85798c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
85898c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
85998c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
86098c230c8SBjoern A. Zeeb 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
86198c230c8SBjoern A. Zeeb 	/* We need to play some tricks here for the second interface. */
86242a58907SGleb Smirnoff 	strlcpy(name, epairname, len);
86398c230c8SBjoern A. Zeeb 	error = if_clone_create(name, len, (caddr_t)scb);
86498c230c8SBjoern A. Zeeb 	if (error)
86542a58907SGleb Smirnoff 		panic("%s: if_clone_create() for our 2nd iface failed: %d",
86698c230c8SBjoern A. Zeeb 		    __func__, error);
86798c230c8SBjoern A. Zeeb 	scb->if_qflush = ifp->if_qflush;
86898c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
86998c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
870b245f96cSGleb Smirnoff 	ifp->if_baudrate = IF_Gbps(10);	/* arbitrary maximum */
87198c230c8SBjoern A. Zeeb 
87298c230c8SBjoern A. Zeeb 	/*
87398c230c8SBjoern A. Zeeb 	 * Restore name to <n>a as the ifp for this will go into the
87498c230c8SBjoern A. Zeeb 	 * cloner list for the initial call.
87598c230c8SBjoern A. Zeeb 	 */
87698c230c8SBjoern A. Zeeb 	strlcpy(name, sca->ifp->if_xname, len);
87798c230c8SBjoern A. Zeeb 	DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
87898c230c8SBjoern A. Zeeb 
87998c230c8SBjoern A. Zeeb 	/* Tell the world, that we are ready to rock. */
88098c230c8SBjoern A. Zeeb 	sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
88198c230c8SBjoern A. Zeeb 	scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
882c7493539SBjoern A. Zeeb 	if_link_state_change(sca->ifp, LINK_STATE_UP);
883c7493539SBjoern A. Zeeb 	if_link_state_change(scb->ifp, LINK_STATE_UP);
88498c230c8SBjoern A. Zeeb 
88598c230c8SBjoern A. Zeeb 	return (0);
88698c230c8SBjoern A. Zeeb }
88798c230c8SBjoern A. Zeeb 
88898c230c8SBjoern A. Zeeb static int
88998c230c8SBjoern A. Zeeb epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
89098c230c8SBjoern A. Zeeb {
89198c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
89298c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
89398c230c8SBjoern A. Zeeb 	int unit, error;
89498c230c8SBjoern A. Zeeb 
89598c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
89698c230c8SBjoern A. Zeeb 
89798c230c8SBjoern A. Zeeb 	/*
89898c230c8SBjoern A. Zeeb 	 * In case we called into if_clone_destroyif() ourselves
89998c230c8SBjoern A. Zeeb 	 * again to remove the second interface, the softc will be
90098c230c8SBjoern A. Zeeb 	 * NULL. In that case so not do anything but return success.
90198c230c8SBjoern A. Zeeb 	 */
90298c230c8SBjoern A. Zeeb 	if (ifp->if_softc == NULL)
90398c230c8SBjoern A. Zeeb 		return (0);
90498c230c8SBjoern A. Zeeb 
90598c230c8SBjoern A. Zeeb 	unit = ifp->if_dunit;
90698c230c8SBjoern A. Zeeb 	sca = ifp->if_softc;
90798c230c8SBjoern A. Zeeb 	oifp = sca->oifp;
90898c230c8SBjoern A. Zeeb 	scb = oifp->if_softc;
90998c230c8SBjoern A. Zeeb 
91098c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
911c7493539SBjoern A. Zeeb 	if_link_state_change(ifp, LINK_STATE_DOWN);
912c7493539SBjoern A. Zeeb 	if_link_state_change(oifp, LINK_STATE_DOWN);
91398c230c8SBjoern A. Zeeb 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
91498c230c8SBjoern A. Zeeb 	oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
91598c230c8SBjoern A. Zeeb 
91698c230c8SBjoern A. Zeeb 	/*
9177edc3d88SMikolaj Golub 	 * Get rid of our second half. As the other of the two
9187edc3d88SMikolaj Golub 	 * interfaces may reside in a different vnet, we need to
9197edc3d88SMikolaj Golub 	 * switch before freeing them.
92098c230c8SBjoern A. Zeeb 	 */
9217edc3d88SMikolaj Golub 	CURVNET_SET_QUIET(oifp->if_vnet);
9227edc3d88SMikolaj Golub 	ether_ifdetach(oifp);
9237edc3d88SMikolaj Golub 	/*
9247edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9257edc3d88SMikolaj Golub 	 * The numbers can only go down as the interface is
9267edc3d88SMikolaj Golub 	 * detached so there is no need to use atomics.
9277edc3d88SMikolaj Golub 	 */
9287edc3d88SMikolaj Golub 	DPRINTF("scb refcnt=%u\n", scb->refcount);
9297edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
9307edc3d88SMikolaj Golub 	    ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
93198c230c8SBjoern A. Zeeb 	oifp->if_softc = NULL;
93298c230c8SBjoern A. Zeeb 	error = if_clone_destroyif(ifc, oifp);
93398c230c8SBjoern A. Zeeb 	if (error)
93498c230c8SBjoern A. Zeeb 		panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
93598c230c8SBjoern A. Zeeb 		    __func__, error);
93673e39d61SBjoern A. Zeeb 	if_free(oifp);
9372dccdd45SMarko Zec 	ifmedia_removeall(&scb->media);
93898c230c8SBjoern A. Zeeb 	free(scb, M_EPAIR);
9397edc3d88SMikolaj Golub 	CURVNET_RESTORE();
9407edc3d88SMikolaj Golub 
9417edc3d88SMikolaj Golub 	ether_ifdetach(ifp);
9427edc3d88SMikolaj Golub 	/*
9437edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9447edc3d88SMikolaj Golub 	 */
9457edc3d88SMikolaj Golub 	DPRINTF("sca refcnt=%u\n", sca->refcount);
9467edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
9477edc3d88SMikolaj Golub 	    ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
9487edc3d88SMikolaj Golub 	if_free(ifp);
9497edc3d88SMikolaj Golub 	ifmedia_removeall(&sca->media);
95098c230c8SBjoern A. Zeeb 	free(sca, M_EPAIR);
95198c230c8SBjoern A. Zeeb 	ifc_free_unit(ifc, unit);
95298c230c8SBjoern A. Zeeb 
95398c230c8SBjoern A. Zeeb 	return (0);
95498c230c8SBjoern A. Zeeb }
95598c230c8SBjoern A. Zeeb 
9563c3136b1SHiroki Sato static void
9573c3136b1SHiroki Sato vnet_epair_init(const void *unused __unused)
9583c3136b1SHiroki Sato {
9593c3136b1SHiroki Sato 
9603c3136b1SHiroki Sato 	V_epair_cloner = if_clone_advanced(epairname, 0,
9613c3136b1SHiroki Sato 	    epair_clone_match, epair_clone_create, epair_clone_destroy);
9623c3136b1SHiroki Sato }
9633c3136b1SHiroki Sato VNET_SYSINIT(vnet_epair_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
9643c3136b1SHiroki Sato     vnet_epair_init, NULL);
9653c3136b1SHiroki Sato 
9663c3136b1SHiroki Sato static void
9673c3136b1SHiroki Sato vnet_epair_uninit(const void *unused __unused)
9683c3136b1SHiroki Sato {
9693c3136b1SHiroki Sato 
9703c3136b1SHiroki Sato 	if_clone_detach(V_epair_cloner);
9713c3136b1SHiroki Sato }
9723c3136b1SHiroki Sato VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
9733c3136b1SHiroki Sato     vnet_epair_uninit, NULL);
9743c3136b1SHiroki Sato 
97598c230c8SBjoern A. Zeeb static int
97698c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data)
97798c230c8SBjoern A. Zeeb {
978d0ea4743SBjoern A. Zeeb 	int qlimit;
97998c230c8SBjoern A. Zeeb 
98098c230c8SBjoern A. Zeeb 	switch (type) {
98198c230c8SBjoern A. Zeeb 	case MOD_LOAD:
98298c230c8SBjoern A. Zeeb 		/* For now limit us to one global mutex and one inq. */
983d0ea4743SBjoern A. Zeeb 		epair_dpcpu_init();
984d0ea4743SBjoern A. Zeeb 		epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
985d0ea4743SBjoern A. Zeeb 		if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
986d0ea4743SBjoern A. Zeeb 		    epair_nh.nh_qlimit = qlimit;
987d0ea4743SBjoern A. Zeeb 		netisr_register(&epair_nh);
98898c230c8SBjoern A. Zeeb 		if (bootverbose)
98942a58907SGleb Smirnoff 			printf("%s initialized.\n", epairname);
99098c230c8SBjoern A. Zeeb 		break;
99198c230c8SBjoern A. Zeeb 	case MOD_UNLOAD:
992d0ea4743SBjoern A. Zeeb 		netisr_unregister(&epair_nh);
993d0ea4743SBjoern A. Zeeb 		epair_dpcpu_detach();
99498c230c8SBjoern A. Zeeb 		if (bootverbose)
99542a58907SGleb Smirnoff 			printf("%s unloaded.\n", epairname);
99698c230c8SBjoern A. Zeeb 		break;
99798c230c8SBjoern A. Zeeb 	default:
99898c230c8SBjoern A. Zeeb 		return (EOPNOTSUPP);
99998c230c8SBjoern A. Zeeb 	}
100098c230c8SBjoern A. Zeeb 	return (0);
100198c230c8SBjoern A. Zeeb }
100298c230c8SBjoern A. Zeeb 
100398c230c8SBjoern A. Zeeb static moduledata_t epair_mod = {
100498c230c8SBjoern A. Zeeb 	"if_epair",
100598c230c8SBjoern A. Zeeb 	epair_modevent,
10069823d527SKevin Lo 	0
100798c230c8SBjoern A. Zeeb };
100898c230c8SBjoern A. Zeeb 
100998c230c8SBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
101098c230c8SBjoern A. Zeeb MODULE_VERSION(if_epair, 1);
1011