xref: /freebsd/sys/net/if_epair.c (revision 9823d52705ad71f19ef2205aa729547ac396e3eb)
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>
5598c230c8SBjoern A. Zeeb #include <sys/mbuf.h>
5698c230c8SBjoern A. Zeeb #include <sys/module.h>
5798c230c8SBjoern A. Zeeb #include <sys/refcount.h>
5898c230c8SBjoern A. Zeeb #include <sys/queue.h>
59d0ea4743SBjoern A. Zeeb #include <sys/smp.h>
6098c230c8SBjoern A. Zeeb #include <sys/socket.h>
6198c230c8SBjoern A. Zeeb #include <sys/sockio.h>
6298c230c8SBjoern A. Zeeb #include <sys/sysctl.h>
6398c230c8SBjoern A. Zeeb #include <sys/types.h>
6498c230c8SBjoern A. Zeeb 
6598c230c8SBjoern A. Zeeb #include <net/bpf.h>
6698c230c8SBjoern A. Zeeb #include <net/ethernet.h>
6798c230c8SBjoern A. Zeeb #include <net/if.h>
6898c230c8SBjoern A. Zeeb #include <net/if_clone.h>
692dccdd45SMarko Zec #include <net/if_media.h>
7098c230c8SBjoern A. Zeeb #include <net/if_var.h>
7198c230c8SBjoern A. Zeeb #include <net/if_types.h>
7298c230c8SBjoern A. Zeeb #include <net/netisr.h>
73530c0060SRobert Watson #include <net/vnet.h>
7498c230c8SBjoern A. Zeeb 
7598c230c8SBjoern A. Zeeb #define	EPAIRNAME	"epair"
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 
103d0ea4743SBjoern A. Zeeb /* Netisr realted definitions and sysctl. */
104d0ea4743SBjoern A. Zeeb static struct netisr_handler epair_nh = {
105d0ea4743SBjoern A. Zeeb 	.nh_name	= EPAIRNAME,
106d0ea4743SBjoern A. Zeeb 	.nh_proto	= NETISR_EPAIR,
107d0ea4743SBjoern A. Zeeb 	.nh_policy	= NETISR_POLICY_CPU,
108d0ea4743SBjoern A. Zeeb 	.nh_handler	= epair_nh_sintr,
109d0ea4743SBjoern A. Zeeb 	.nh_m2cpuid	= epair_nh_m2cpuid,
110d0ea4743SBjoern A. Zeeb 	.nh_drainedcpu	= epair_nh_drainedcpu,
111d0ea4743SBjoern A. Zeeb };
112d0ea4743SBjoern A. Zeeb 
113d0ea4743SBjoern A. Zeeb static int
114d0ea4743SBjoern A. Zeeb sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
115d0ea4743SBjoern A. Zeeb {
116d0ea4743SBjoern A. Zeeb 	int error, qlimit;
117d0ea4743SBjoern A. Zeeb 
118d0ea4743SBjoern A. Zeeb 	netisr_getqlimit(&epair_nh, &qlimit);
119d0ea4743SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
120d0ea4743SBjoern A. Zeeb 	if (error || !req->newptr)
121d0ea4743SBjoern A. Zeeb 		return (error);
122d0ea4743SBjoern A. Zeeb 	if (qlimit < 1)
123d0ea4743SBjoern A. Zeeb 		return (EINVAL);
124d0ea4743SBjoern A. Zeeb 	return (netisr_setqlimit(&epair_nh, qlimit));
125d0ea4743SBjoern A. Zeeb }
126d0ea4743SBjoern A. Zeeb SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
127d0ea4743SBjoern A. Zeeb     0, 0, sysctl_epair_netisr_maxqlen, "I",
128d0ea4743SBjoern A. Zeeb     "Maximum if_epair(4) netisr \"hw\" queue length");
129d0ea4743SBjoern A. Zeeb 
130d0ea4743SBjoern A. Zeeb struct epair_softc {
131d0ea4743SBjoern A. Zeeb 	struct ifnet	*ifp;		/* This ifp. */
132d0ea4743SBjoern A. Zeeb 	struct ifnet	*oifp;		/* other ifp of pair. */
1332dccdd45SMarko Zec 	struct ifmedia	media;		/* Media config (fake). */
134d0ea4743SBjoern A. Zeeb 	u_int		refcount;	/* # of mbufs in flight. */
135d0ea4743SBjoern A. Zeeb 	u_int		cpuid;		/* CPU ID assigned upon creation. */
136d0ea4743SBjoern A. Zeeb 	void		(*if_qflush)(struct ifnet *);
137d0ea4743SBjoern A. Zeeb 					/* Original if_qflush routine. */
138d0ea4743SBjoern A. Zeeb };
139d0ea4743SBjoern A. Zeeb 
140d0ea4743SBjoern A. Zeeb /*
141d0ea4743SBjoern A. Zeeb  * Per-CPU list of ifps with data in the ifq that needs to be flushed
142d0ea4743SBjoern A. Zeeb  * to the netisr ``hw'' queue before we allow any further direct queuing
143d0ea4743SBjoern A. Zeeb  * to the ``hw'' queue.
144d0ea4743SBjoern A. Zeeb  */
145d0ea4743SBjoern A. Zeeb struct epair_ifp_drain {
146d0ea4743SBjoern A. Zeeb 	STAILQ_ENTRY(epair_ifp_drain)	ifp_next;
147d0ea4743SBjoern A. Zeeb 	struct ifnet			*ifp;
148d0ea4743SBjoern A. Zeeb };
149d0ea4743SBjoern A. Zeeb STAILQ_HEAD(eid_list, epair_ifp_drain);
150d0ea4743SBjoern A. Zeeb 
151d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_INIT(dpcpu)		mtx_init(&(dpcpu)->if_epair_mtx, \
152d0ea4743SBjoern A. Zeeb 					    "if_epair", NULL, MTX_DEF)
153d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_DESTROY(dpcpu)	mtx_destroy(&(dpcpu)->if_epair_mtx)
154d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_ASSERT(dpcpu)	mtx_assert(&(dpcpu)->if_epair_mtx, \
155d0ea4743SBjoern A. Zeeb 					    MA_OWNED)
156d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK(dpcpu)		mtx_lock(&(dpcpu)->if_epair_mtx)
157d0ea4743SBjoern A. Zeeb #define	EPAIR_UNLOCK(dpcpu)		mtx_unlock(&(dpcpu)->if_epair_mtx)
158d0ea4743SBjoern A. Zeeb 
159d0ea4743SBjoern A. Zeeb #ifdef INVARIANTS
160d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)	refcount_init((r), (v))
161d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)	refcount_acquire((r))
162d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)	refcount_release((r))
163d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)	KASSERT(a, p)
164d0ea4743SBjoern A. Zeeb #else
165d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)
166d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)
167d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)
168d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)
169d0ea4743SBjoern A. Zeeb #endif
170d0ea4743SBjoern A. Zeeb 
171d0ea4743SBjoern A. Zeeb static MALLOC_DEFINE(M_EPAIR, EPAIRNAME,
172d0ea4743SBjoern A. Zeeb     "Pair of virtual cross-over connected Ethernet-like interfaces");
17398c230c8SBjoern A. Zeeb 
17498c230c8SBjoern A. Zeeb static struct if_clone epair_cloner = IFC_CLONE_INITIALIZER(
17598c230c8SBjoern A. Zeeb     EPAIRNAME, NULL, IF_MAXUNIT,
17698c230c8SBjoern A. Zeeb     NULL, epair_clone_match, epair_clone_create, epair_clone_destroy);
17798c230c8SBjoern A. Zeeb 
178d0ea4743SBjoern A. Zeeb /*
179d0ea4743SBjoern A. Zeeb  * DPCPU area and functions.
180d0ea4743SBjoern A. Zeeb  */
181d0ea4743SBjoern A. Zeeb struct epair_dpcpu {
182d0ea4743SBjoern A. Zeeb 	struct mtx	if_epair_mtx;		/* Per-CPU locking. */
183d0ea4743SBjoern A. Zeeb 	int		epair_drv_flags;	/* Per-CPU ``hw'' drv flags. */
184d0ea4743SBjoern A. Zeeb 	struct eid_list	epair_ifp_drain_list;	/* Per-CPU list of ifps with
185d0ea4743SBjoern A. Zeeb 						 * data in the ifq. */
186d0ea4743SBjoern A. Zeeb };
187d0ea4743SBjoern A. Zeeb DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
188d0ea4743SBjoern A. Zeeb 
189d0ea4743SBjoern A. Zeeb static void
190d0ea4743SBjoern A. Zeeb epair_dpcpu_init(void)
191d0ea4743SBjoern A. Zeeb {
192d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
193d0ea4743SBjoern A. Zeeb 	struct eid_list *s;
194d0ea4743SBjoern A. Zeeb 	u_int cpuid;
195d0ea4743SBjoern A. Zeeb 
1963aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
197d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
198d0ea4743SBjoern A. Zeeb 
199d0ea4743SBjoern A. Zeeb 		/* Initialize per-cpu lock. */
200d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_INIT(epair_dpcpu);
201d0ea4743SBjoern A. Zeeb 
202d0ea4743SBjoern A. Zeeb 		/* Driver flags are per-cpu as are our netisr "hw" queues. */
203d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags = 0;
204d0ea4743SBjoern A. Zeeb 
205d0ea4743SBjoern A. Zeeb 		/*
206d0ea4743SBjoern A. Zeeb 		 * Initialize per-cpu drain list.
207d0ea4743SBjoern A. Zeeb 		 * Manually do what STAILQ_HEAD_INITIALIZER would do.
208d0ea4743SBjoern A. Zeeb 		 */
209d0ea4743SBjoern A. Zeeb 		s = &epair_dpcpu->epair_ifp_drain_list;
210d0ea4743SBjoern A. Zeeb 		s->stqh_first = NULL;
211d0ea4743SBjoern A. Zeeb 		s->stqh_last = &s->stqh_first;
212d0ea4743SBjoern A. Zeeb 	}
213d0ea4743SBjoern A. Zeeb }
214d0ea4743SBjoern A. Zeeb 
215d0ea4743SBjoern A. Zeeb static void
216d0ea4743SBjoern A. Zeeb epair_dpcpu_detach(void)
217d0ea4743SBjoern A. Zeeb {
218d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
219d0ea4743SBjoern A. Zeeb 	u_int cpuid;
220d0ea4743SBjoern A. Zeeb 
2213aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
222d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
223d0ea4743SBjoern A. Zeeb 
224d0ea4743SBjoern A. Zeeb 		/* Destroy per-cpu lock. */
225d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_DESTROY(epair_dpcpu);
226d0ea4743SBjoern A. Zeeb 	}
227d0ea4743SBjoern A. Zeeb }
228d0ea4743SBjoern A. Zeeb 
229d0ea4743SBjoern A. Zeeb /*
230d0ea4743SBjoern A. Zeeb  * Helper functions.
231d0ea4743SBjoern A. Zeeb  */
232d0ea4743SBjoern A. Zeeb static u_int
233d0ea4743SBjoern A. Zeeb cpuid_from_ifp(struct ifnet *ifp)
234d0ea4743SBjoern A. Zeeb {
235d0ea4743SBjoern A. Zeeb 	struct epair_softc *sc;
236d0ea4743SBjoern A. Zeeb 
237d0ea4743SBjoern A. Zeeb 	if (ifp == NULL)
238d0ea4743SBjoern A. Zeeb 		return (0);
239d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
240d0ea4743SBjoern A. Zeeb 
241d0ea4743SBjoern A. Zeeb 	return (sc->cpuid);
242d0ea4743SBjoern A. Zeeb }
24398c230c8SBjoern A. Zeeb 
24498c230c8SBjoern A. Zeeb /*
24598c230c8SBjoern A. Zeeb  * Netisr handler functions.
24698c230c8SBjoern A. Zeeb  */
24798c230c8SBjoern A. Zeeb static void
248d0ea4743SBjoern A. Zeeb epair_nh_sintr(struct mbuf *m)
24998c230c8SBjoern A. Zeeb {
25098c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
25198c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
25298c230c8SBjoern A. Zeeb 
25398c230c8SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
25498c230c8SBjoern A. Zeeb 	(*ifp->if_input)(ifp, m);
25598c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
256d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_RELEASE(&sc->refcount);
257eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
258eea3faf7SBjoern A. Zeeb 	    ("%s: ifp=%p sc->refcount not >= 1: %d",
259eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc->refcount));
26098c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
26198c230c8SBjoern A. Zeeb }
26298c230c8SBjoern A. Zeeb 
263d0ea4743SBjoern A. Zeeb static struct mbuf *
264d0ea4743SBjoern A. Zeeb epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
26598c230c8SBjoern A. Zeeb {
266d0ea4743SBjoern A. Zeeb 
267d0ea4743SBjoern A. Zeeb 	*cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
268d0ea4743SBjoern A. Zeeb 
269d0ea4743SBjoern A. Zeeb 	return (m);
270d0ea4743SBjoern A. Zeeb }
271d0ea4743SBjoern A. Zeeb 
272d0ea4743SBjoern A. Zeeb static void
273d0ea4743SBjoern A. Zeeb epair_nh_drainedcpu(u_int cpuid)
274d0ea4743SBjoern A. Zeeb {
275d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
27698c230c8SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
27798c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
27898c230c8SBjoern A. Zeeb 
279d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
280d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
28198c230c8SBjoern A. Zeeb 	/*
28298c230c8SBjoern A. Zeeb 	 * Assume our "hw" queue and possibly ifq will be emptied
28398c230c8SBjoern A. Zeeb 	 * again. In case we will overflow the "hw" queue while
28498c230c8SBjoern A. Zeeb 	 * draining, epair_start_locked will set IFF_DRV_OACTIVE
28598c230c8SBjoern A. Zeeb 	 * again and we will stop and return.
28698c230c8SBjoern A. Zeeb 	 */
287d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
288d0ea4743SBjoern A. Zeeb 	    ifp_next, tvar) {
28998c230c8SBjoern A. Zeeb 		ifp = elm->ifp;
290d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
29198c230c8SBjoern A. Zeeb 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
29298c230c8SBjoern A. Zeeb 		epair_start_locked(ifp);
29398c230c8SBjoern A. Zeeb 
29498c230c8SBjoern A. Zeeb 		IFQ_LOCK(&ifp->if_snd);
29598c230c8SBjoern A. Zeeb 		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
296eea3faf7SBjoern A. Zeeb 			struct epair_softc *sc;
297eea3faf7SBjoern A. Zeeb 
298d0ea4743SBjoern A. Zeeb 			STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
299d0ea4743SBjoern A. Zeeb 			    elm, epair_ifp_drain, ifp_next);
300eea3faf7SBjoern A. Zeeb 			/* The cached ifp goes off the list. */
301eea3faf7SBjoern A. Zeeb 			sc = ifp->if_softc;
302eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
303eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
304eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
305eea3faf7SBjoern A. Zeeb 			    __func__, ifp, sc->refcount));
30698c230c8SBjoern A. Zeeb 			free(elm, M_EPAIR);
30798c230c8SBjoern A. Zeeb 		}
30898c230c8SBjoern A. Zeeb 		IFQ_UNLOCK(&ifp->if_snd);
30998c230c8SBjoern A. Zeeb 
31098c230c8SBjoern A. Zeeb 		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
31198c230c8SBjoern A. Zeeb 			/* Our "hw"q overflew again. */
3122c8b047cSBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
31398c230c8SBjoern A. Zeeb 			DPRINTF("hw queue length overflow at %u\n",
314d0ea4743SBjoern A. Zeeb 			    epair_nh.nh_qlimit);
31598c230c8SBjoern A. Zeeb 			break;
31698c230c8SBjoern A. Zeeb 		}
31798c230c8SBjoern A. Zeeb 	}
318d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
31998c230c8SBjoern A. Zeeb }
32098c230c8SBjoern A. Zeeb 
32198c230c8SBjoern A. Zeeb /*
32298c230c8SBjoern A. Zeeb  * Network interface (`if') related functions.
32398c230c8SBjoern A. Zeeb  */
324eea3faf7SBjoern A. Zeeb static void
325eea3faf7SBjoern A. Zeeb epair_remove_ifp_from_draining(struct ifnet *ifp)
326eea3faf7SBjoern A. Zeeb {
327eea3faf7SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
328eea3faf7SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
329eea3faf7SBjoern A. Zeeb 	u_int cpuid;
330eea3faf7SBjoern A. Zeeb 
3313aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
332eea3faf7SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
333eea3faf7SBjoern A. Zeeb 		EPAIR_LOCK(epair_dpcpu);
334eea3faf7SBjoern A. Zeeb 		STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
335eea3faf7SBjoern A. Zeeb 		    ifp_next, tvar) {
336eea3faf7SBjoern A. Zeeb 			if (ifp == elm->ifp) {
337eea3faf7SBjoern A. Zeeb 				struct epair_softc *sc;
338eea3faf7SBjoern A. Zeeb 
339eea3faf7SBjoern A. Zeeb 				STAILQ_REMOVE(
340eea3faf7SBjoern A. Zeeb 				    &epair_dpcpu->epair_ifp_drain_list, elm,
341eea3faf7SBjoern A. Zeeb 				    epair_ifp_drain, ifp_next);
342eea3faf7SBjoern A. Zeeb 				/* The cached ifp goes off the list. */
343eea3faf7SBjoern A. Zeeb 				sc = ifp->if_softc;
344eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_RELEASE(&sc->refcount);
345eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
346eea3faf7SBjoern A. Zeeb 				    ("%s: ifp=%p sc->refcount not >= 1: %d",
347eea3faf7SBjoern A. Zeeb 				    __func__, ifp, sc->refcount));
348eea3faf7SBjoern A. Zeeb 				free(elm, M_EPAIR);
349eea3faf7SBjoern A. Zeeb 			}
350eea3faf7SBjoern A. Zeeb 		}
351eea3faf7SBjoern A. Zeeb 		EPAIR_UNLOCK(epair_dpcpu);
352eea3faf7SBjoern A. Zeeb 	}
353eea3faf7SBjoern A. Zeeb }
354eea3faf7SBjoern A. Zeeb 
355d0ea4743SBjoern A. Zeeb static int
356d0ea4743SBjoern A. Zeeb epair_add_ifp_for_draining(struct ifnet *ifp)
357d0ea4743SBjoern A. Zeeb {
358d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
359eea3faf7SBjoern A. Zeeb 	struct epair_softc *sc;
360d0ea4743SBjoern A. Zeeb 	struct epair_ifp_drain *elm = NULL;
361d0ea4743SBjoern A. Zeeb 
362eea3faf7SBjoern A. Zeeb 	sc = ifp->if_softc;
363d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
364eea3faf7SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
365d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
366d0ea4743SBjoern A. Zeeb 		if (elm->ifp == ifp)
367d0ea4743SBjoern A. Zeeb 			break;
3683c20163aSBjoern A. Zeeb 	/* If the ifp is there already, return success. */
369d0ea4743SBjoern A. Zeeb 	if (elm != NULL)
370d0ea4743SBjoern A. Zeeb 		return (0);
371d0ea4743SBjoern A. Zeeb 
372d0ea4743SBjoern A. Zeeb 	elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
373d0ea4743SBjoern A. Zeeb 	if (elm == NULL)
374d0ea4743SBjoern A. Zeeb 		return (ENOMEM);
375d0ea4743SBjoern A. Zeeb 
376d0ea4743SBjoern A. Zeeb 	elm->ifp = ifp;
377eea3faf7SBjoern A. Zeeb 	/* Add a reference for the ifp pointer on the list. */
378eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
379d0ea4743SBjoern A. Zeeb 	STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
380d0ea4743SBjoern A. Zeeb 
381d0ea4743SBjoern A. Zeeb 	return (0);
382d0ea4743SBjoern A. Zeeb }
383d0ea4743SBjoern A. Zeeb 
38498c230c8SBjoern A. Zeeb static void
38598c230c8SBjoern A. Zeeb epair_start_locked(struct ifnet *ifp)
38698c230c8SBjoern A. Zeeb {
387d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
38898c230c8SBjoern A. Zeeb 	struct mbuf *m;
38998c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
39098c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
39198c230c8SBjoern A. Zeeb 	int error;
39298c230c8SBjoern A. Zeeb 
39398c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
394d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
395d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
396d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
39798c230c8SBjoern A. Zeeb 
39898c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
39998c230c8SBjoern A. Zeeb 		return;
40098c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0)
40198c230c8SBjoern A. Zeeb 		return;
40298c230c8SBjoern A. Zeeb 
40398c230c8SBjoern A. Zeeb 	/*
40498c230c8SBjoern A. Zeeb 	 * We get patckets here from ether_output via if_handoff()
40598c230c8SBjoern A. Zeeb 	 * and ned to put them into the input queue of the oifp
40698c230c8SBjoern A. Zeeb 	 * and call oifp->if_input() via netisr/epair_sintr().
40798c230c8SBjoern A. Zeeb 	 */
40898c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
40998c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
41098c230c8SBjoern A. Zeeb 	for (;;) {
41198c230c8SBjoern A. Zeeb 		IFQ_DEQUEUE(&ifp->if_snd, m);
41298c230c8SBjoern A. Zeeb 		if (m == NULL)
41398c230c8SBjoern A. Zeeb 			break;
41498c230c8SBjoern A. Zeeb 		BPF_MTAP(ifp, m);
41598c230c8SBjoern A. Zeeb 
41698c230c8SBjoern A. Zeeb 		/*
41798c230c8SBjoern A. Zeeb 		 * In case the outgoing interface is not usable,
41898c230c8SBjoern A. Zeeb 		 * drop the packet.
41998c230c8SBjoern A. Zeeb 		 */
42098c230c8SBjoern A. Zeeb 		if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
42198c230c8SBjoern A. Zeeb 		    (oifp->if_flags & IFF_UP) ==0) {
42298c230c8SBjoern A. Zeeb 			ifp->if_oerrors++;
42398c230c8SBjoern A. Zeeb 			m_freem(m);
42498c230c8SBjoern A. Zeeb 			continue;
42598c230c8SBjoern A. Zeeb 		}
42698c230c8SBjoern A. Zeeb 		DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
42798c230c8SBjoern A. Zeeb 
42898c230c8SBjoern A. Zeeb 		/*
42998c230c8SBjoern A. Zeeb 		 * Add a reference so the interface cannot go while the
43098c230c8SBjoern A. Zeeb 		 * packet is in transit as we rely on rcvif to stay valid.
43198c230c8SBjoern A. Zeeb 		 */
432d0ea4743SBjoern A. Zeeb 		EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
43398c230c8SBjoern A. Zeeb 		m->m_pkthdr.rcvif = oifp;
43498c230c8SBjoern A. Zeeb 		CURVNET_SET_QUIET(oifp->if_vnet);
43598c230c8SBjoern A. Zeeb 		error = netisr_queue(NETISR_EPAIR, m);
43698c230c8SBjoern A. Zeeb 		CURVNET_RESTORE();
43798c230c8SBjoern A. Zeeb 		if (!error) {
43898c230c8SBjoern A. Zeeb 			ifp->if_opackets++;
43998c230c8SBjoern A. Zeeb 			/* Someone else received the packet. */
44098c230c8SBjoern A. Zeeb 			oifp->if_ipackets++;
44198c230c8SBjoern A. Zeeb 		} else {
442eea3faf7SBjoern A. Zeeb 			/* The packet was freed already. */
443d0ea4743SBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
44498c230c8SBjoern A. Zeeb 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
445eea3faf7SBjoern A. Zeeb 			(void) epair_add_ifp_for_draining(ifp);
446d0ea4743SBjoern A. Zeeb 			ifp->if_oerrors++;
447d0ea4743SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
448eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
449eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
450eea3faf7SBjoern A. Zeeb 			    __func__, oifp, sc->refcount));
45198c230c8SBjoern A. Zeeb 		}
45298c230c8SBjoern A. Zeeb 	}
45398c230c8SBjoern A. Zeeb }
45498c230c8SBjoern A. Zeeb 
45598c230c8SBjoern A. Zeeb static void
45698c230c8SBjoern A. Zeeb epair_start(struct ifnet *ifp)
45798c230c8SBjoern A. Zeeb {
458d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
45998c230c8SBjoern A. Zeeb 
460d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
461d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
46298c230c8SBjoern A. Zeeb 	epair_start_locked(ifp);
463d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
46498c230c8SBjoern A. Zeeb }
46598c230c8SBjoern A. Zeeb 
46698c230c8SBjoern A. Zeeb static int
46798c230c8SBjoern A. Zeeb epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
46898c230c8SBjoern A. Zeeb {
469d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
47098c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
47198c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
47298c230c8SBjoern A. Zeeb 	int error, len;
47398c230c8SBjoern A. Zeeb 	short mflags;
47498c230c8SBjoern A. Zeeb 
47598c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p m=%p\n", ifp, m);
476d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
477d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
478d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
47998c230c8SBjoern A. Zeeb 
48098c230c8SBjoern A. Zeeb 	if (m == NULL)
48198c230c8SBjoern A. Zeeb 		return (0);
48298c230c8SBjoern A. Zeeb 
48398c230c8SBjoern A. Zeeb 	/*
48498c230c8SBjoern A. Zeeb 	 * We are not going to use the interface en/dequeue mechanism
48598c230c8SBjoern A. Zeeb 	 * on the TX side. We are called from ether_output_frame()
48698c230c8SBjoern A. Zeeb 	 * and will put the packet into the incoming queue of the
48798c230c8SBjoern A. Zeeb 	 * other interface of our pair via the netsir.
48898c230c8SBjoern A. Zeeb 	 */
48998c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
49098c230c8SBjoern A. Zeeb 		m_freem(m);
49198c230c8SBjoern A. Zeeb 		return (ENXIO);
49298c230c8SBjoern A. Zeeb 	}
49398c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0) {
49498c230c8SBjoern A. Zeeb 		m_freem(m);
49598c230c8SBjoern A. Zeeb 		return (ENETDOWN);
49698c230c8SBjoern A. Zeeb 	}
49798c230c8SBjoern A. Zeeb 
49898c230c8SBjoern A. Zeeb 	BPF_MTAP(ifp, m);
49998c230c8SBjoern A. Zeeb 
50098c230c8SBjoern A. Zeeb 	/*
50198c230c8SBjoern A. Zeeb 	 * In case the outgoing interface is not usable,
50298c230c8SBjoern A. Zeeb 	 * drop the packet.
50398c230c8SBjoern A. Zeeb 	 */
50498c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
50598c230c8SBjoern A. Zeeb 	if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
50698c230c8SBjoern A. Zeeb 	    (oifp->if_flags & IFF_UP) ==0) {
50798c230c8SBjoern A. Zeeb 		ifp->if_oerrors++;
50898c230c8SBjoern A. Zeeb 		m_freem(m);
50998c230c8SBjoern A. Zeeb 		return (0);
51098c230c8SBjoern A. Zeeb 	}
51198c230c8SBjoern A. Zeeb 	len = m->m_pkthdr.len;
51298c230c8SBjoern A. Zeeb 	mflags = m->m_flags;
51398c230c8SBjoern A. Zeeb 	DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
51498c230c8SBjoern A. Zeeb 
51598c230c8SBjoern A. Zeeb #ifdef ALTQ
51698c230c8SBjoern A. Zeeb 	/* Support ALTQ via the clasic if_start() path. */
51798c230c8SBjoern A. Zeeb 	IF_LOCK(&ifp->if_snd);
51898c230c8SBjoern A. Zeeb 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
51998c230c8SBjoern A. Zeeb 		ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
52098c230c8SBjoern A. Zeeb 		if (error)
52198c230c8SBjoern A. Zeeb 			ifp->if_snd.ifq_drops++;
52298c230c8SBjoern A. Zeeb 		IF_UNLOCK(&ifp->if_snd);
52398c230c8SBjoern A. Zeeb 		if (!error) {
52498c230c8SBjoern A. Zeeb 			ifp->if_obytes += len;
52598c230c8SBjoern A. Zeeb 			if (mflags & (M_BCAST|M_MCAST))
52698c230c8SBjoern A. Zeeb 				ifp->if_omcasts++;
52798c230c8SBjoern A. Zeeb 
52898c230c8SBjoern A. Zeeb 			if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
52998c230c8SBjoern A. Zeeb 				epair_start_locked(ifp);
53098c230c8SBjoern A. Zeeb 			else
531d0ea4743SBjoern A. Zeeb 				(void)epair_add_ifp_for_draining(ifp);
53298c230c8SBjoern A. Zeeb 		}
53398c230c8SBjoern A. Zeeb 		return (error);
53498c230c8SBjoern A. Zeeb 	}
53598c230c8SBjoern A. Zeeb 	IF_UNLOCK(&ifp->if_snd);
53698c230c8SBjoern A. Zeeb #endif
53798c230c8SBjoern A. Zeeb 
538d0ea4743SBjoern A. Zeeb 	if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
53998c230c8SBjoern A. Zeeb 		/*
54098c230c8SBjoern A. Zeeb 		 * Our hardware queue is full, try to fall back
54198c230c8SBjoern A. Zeeb 		 * queuing to the ifq but do not call ifp->if_start.
54298c230c8SBjoern A. Zeeb 		 * Either we are lucky or the packet is gone.
54398c230c8SBjoern A. Zeeb 		 */
54498c230c8SBjoern A. Zeeb 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
54598c230c8SBjoern A. Zeeb 		if (!error)
546d0ea4743SBjoern A. Zeeb 			(void)epair_add_ifp_for_draining(ifp);
54798c230c8SBjoern A. Zeeb 		return (error);
54898c230c8SBjoern A. Zeeb 	}
54998c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
55098c230c8SBjoern A. Zeeb 	/*
55198c230c8SBjoern A. Zeeb 	 * Add a reference so the interface cannot go while the
55298c230c8SBjoern A. Zeeb 	 * packet is in transit as we rely on rcvif to stay valid.
55398c230c8SBjoern A. Zeeb 	 */
554d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
55598c230c8SBjoern A. Zeeb 	m->m_pkthdr.rcvif = oifp;
55698c230c8SBjoern A. Zeeb 	CURVNET_SET_QUIET(oifp->if_vnet);
55798c230c8SBjoern A. Zeeb 	error = netisr_queue(NETISR_EPAIR, m);
55898c230c8SBjoern A. Zeeb 	CURVNET_RESTORE();
55998c230c8SBjoern A. Zeeb 	if (!error) {
56098c230c8SBjoern A. Zeeb 		ifp->if_opackets++;
56198c230c8SBjoern A. Zeeb 		/*
56298c230c8SBjoern A. Zeeb 		 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
56398c230c8SBjoern A. Zeeb 		 * but as we bypass all this we have to duplicate
56498c230c8SBjoern A. Zeeb 		 * the logic another time.
56598c230c8SBjoern A. Zeeb 		 */
56698c230c8SBjoern A. Zeeb 		ifp->if_obytes += len;
56798c230c8SBjoern A. Zeeb 		if (mflags & (M_BCAST|M_MCAST))
56898c230c8SBjoern A. Zeeb 			ifp->if_omcasts++;
56998c230c8SBjoern A. Zeeb 		/* Someone else received the packet. */
57098c230c8SBjoern A. Zeeb 		oifp->if_ipackets++;
57198c230c8SBjoern A. Zeeb 	} else {
57298c230c8SBjoern A. Zeeb 		/* The packet was freed already. */
573d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
57498c230c8SBjoern A. Zeeb 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
575eea3faf7SBjoern A. Zeeb 		ifp->if_oerrors++;
576eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_RELEASE(&sc->refcount);
577eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
578eea3faf7SBjoern A. Zeeb 		    ("%s: ifp=%p sc->refcount not >= 1: %d",
579eea3faf7SBjoern A. Zeeb 		    __func__, oifp, sc->refcount));
58098c230c8SBjoern A. Zeeb 	}
58198c230c8SBjoern A. Zeeb 
58298c230c8SBjoern A. Zeeb 	return (error);
58398c230c8SBjoern A. Zeeb }
58498c230c8SBjoern A. Zeeb 
58598c230c8SBjoern A. Zeeb static int
58698c230c8SBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m)
58798c230c8SBjoern A. Zeeb {
588d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
58998c230c8SBjoern A. Zeeb 	int error;
59098c230c8SBjoern A. Zeeb 
591d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
592d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
59398c230c8SBjoern A. Zeeb 	error = epair_transmit_locked(ifp, m);
594d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
59598c230c8SBjoern A. Zeeb 	return (error);
59698c230c8SBjoern A. Zeeb }
59798c230c8SBjoern A. Zeeb 
59898c230c8SBjoern A. Zeeb static void
59998c230c8SBjoern A. Zeeb epair_qflush(struct ifnet *ifp)
60098c230c8SBjoern A. Zeeb {
60198c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
60298c230c8SBjoern A. Zeeb 
60398c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
604eea3faf7SBjoern A. Zeeb 	KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
605eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc));
60698c230c8SBjoern A. Zeeb 	/*
607eea3faf7SBjoern A. Zeeb 	 * Remove this ifp from all backpointer lists. The interface will not
608eea3faf7SBjoern A. Zeeb 	 * usable for flushing anyway nor should it have anything to flush
609eea3faf7SBjoern A. Zeeb 	 * after if_qflush().
61098c230c8SBjoern A. Zeeb 	 */
611eea3faf7SBjoern A. Zeeb 	epair_remove_ifp_from_draining(ifp);
612eea3faf7SBjoern A. Zeeb 
61398c230c8SBjoern A. Zeeb 	if (sc->if_qflush)
61498c230c8SBjoern A. Zeeb 		sc->if_qflush(ifp);
61598c230c8SBjoern A. Zeeb }
61698c230c8SBjoern A. Zeeb 
61798c230c8SBjoern A. Zeeb static int
6182dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused)
6192dccdd45SMarko Zec {
6202dccdd45SMarko Zec 
6212dccdd45SMarko Zec 	/* Do nothing. */
6222dccdd45SMarko Zec 	return (0);
6232dccdd45SMarko Zec }
6242dccdd45SMarko Zec 
6252dccdd45SMarko Zec static void
6262dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
6272dccdd45SMarko Zec {
6282dccdd45SMarko Zec 
6292dccdd45SMarko Zec 	imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
6302dccdd45SMarko Zec 	imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
6312dccdd45SMarko Zec }
6322dccdd45SMarko Zec 
6332dccdd45SMarko Zec static int
63498c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
63598c230c8SBjoern A. Zeeb {
6362dccdd45SMarko Zec 	struct epair_softc *sc;
63798c230c8SBjoern A. Zeeb 	struct ifreq *ifr;
63898c230c8SBjoern A. Zeeb 	int error;
63998c230c8SBjoern A. Zeeb 
64098c230c8SBjoern A. Zeeb 	ifr = (struct ifreq *)data;
64198c230c8SBjoern A. Zeeb 	switch (cmd) {
64298c230c8SBjoern A. Zeeb 	case SIOCSIFFLAGS:
64398c230c8SBjoern A. Zeeb 	case SIOCADDMULTI:
64498c230c8SBjoern A. Zeeb 	case SIOCDELMULTI:
64598c230c8SBjoern A. Zeeb 		error = 0;
64698c230c8SBjoern A. Zeeb 		break;
64798c230c8SBjoern A. Zeeb 
6482dccdd45SMarko Zec 	case SIOCSIFMEDIA:
6492dccdd45SMarko Zec 	case SIOCGIFMEDIA:
6502dccdd45SMarko Zec 		sc = ifp->if_softc;
6512dccdd45SMarko Zec 		error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
6522dccdd45SMarko Zec 		break;
6532dccdd45SMarko Zec 
654d0ea4743SBjoern A. Zeeb 	case SIOCSIFMTU:
655d0ea4743SBjoern A. Zeeb 		/* We basically allow all kinds of MTUs. */
656d0ea4743SBjoern A. Zeeb 		ifp->if_mtu = ifr->ifr_mtu;
657d0ea4743SBjoern A. Zeeb 		error = 0;
658d0ea4743SBjoern A. Zeeb 		break;
659d0ea4743SBjoern A. Zeeb 
66098c230c8SBjoern A. Zeeb 	default:
66198c230c8SBjoern A. Zeeb 		/* Let the common ethernet handler process this. */
66298c230c8SBjoern A. Zeeb 		error = ether_ioctl(ifp, cmd, data);
66398c230c8SBjoern A. Zeeb 		break;
66498c230c8SBjoern A. Zeeb 	}
66598c230c8SBjoern A. Zeeb 
66698c230c8SBjoern A. Zeeb 	return (error);
66798c230c8SBjoern A. Zeeb }
66898c230c8SBjoern A. Zeeb 
66998c230c8SBjoern A. Zeeb static void
67098c230c8SBjoern A. Zeeb epair_init(void *dummy __unused)
67198c230c8SBjoern A. Zeeb {
67298c230c8SBjoern A. Zeeb }
67398c230c8SBjoern A. Zeeb 
67498c230c8SBjoern A. Zeeb 
67598c230c8SBjoern A. Zeeb /*
67698c230c8SBjoern A. Zeeb  * Interface cloning functions.
67798c230c8SBjoern A. Zeeb  * We use our private ones so that we can create/destroy our secondary
67898c230c8SBjoern A. Zeeb  * device along with the primary one.
67998c230c8SBjoern A. Zeeb  */
68098c230c8SBjoern A. Zeeb static int
68198c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name)
68298c230c8SBjoern A. Zeeb {
68398c230c8SBjoern A. Zeeb 	const char *cp;
68498c230c8SBjoern A. Zeeb 
68598c230c8SBjoern A. Zeeb 	DPRINTF("name='%s'\n", name);
68698c230c8SBjoern A. Zeeb 
68798c230c8SBjoern A. Zeeb 	/*
68898c230c8SBjoern A. Zeeb 	 * Our base name is epair.
68998c230c8SBjoern A. Zeeb 	 * Our interfaces will be named epair<n>[ab].
69098c230c8SBjoern A. Zeeb 	 * So accept anything of the following list:
69198c230c8SBjoern A. Zeeb 	 * - epair
69298c230c8SBjoern A. Zeeb 	 * - epair<n>
69398c230c8SBjoern A. Zeeb 	 * but not the epair<n>[ab] versions.
69498c230c8SBjoern A. Zeeb 	 */
69598c230c8SBjoern A. Zeeb 	if (strncmp(EPAIRNAME, name, sizeof(EPAIRNAME)-1) != 0)
69698c230c8SBjoern A. Zeeb 		return (0);
69798c230c8SBjoern A. Zeeb 
69898c230c8SBjoern A. Zeeb 	for (cp = name + sizeof(EPAIRNAME) - 1; *cp != '\0'; cp++) {
69998c230c8SBjoern A. Zeeb 		if (*cp < '0' || *cp > '9')
70098c230c8SBjoern A. Zeeb 			return (0);
70198c230c8SBjoern A. Zeeb 	}
70298c230c8SBjoern A. Zeeb 
70398c230c8SBjoern A. Zeeb 	return (1);
70498c230c8SBjoern A. Zeeb }
70598c230c8SBjoern A. Zeeb 
70698c230c8SBjoern A. Zeeb static int
70798c230c8SBjoern A. Zeeb epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
70898c230c8SBjoern A. Zeeb {
70998c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
71098c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
71198c230c8SBjoern A. Zeeb 	char *dp;
71298c230c8SBjoern A. Zeeb 	int error, unit, wildcard;
71398c230c8SBjoern A. Zeeb 	uint8_t eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
71498c230c8SBjoern A. Zeeb 
71598c230c8SBjoern A. Zeeb 	/*
71698c230c8SBjoern A. Zeeb 	 * We are abusing params to create our second interface.
71798c230c8SBjoern A. Zeeb 	 * Actually we already created it and called if_clone_createif()
71898c230c8SBjoern A. Zeeb 	 * for it to do the official insertion procedure the moment we knew
71998c230c8SBjoern A. Zeeb 	 * it cannot fail anymore. So just do attach it here.
72098c230c8SBjoern A. Zeeb 	 */
72198c230c8SBjoern A. Zeeb 	if (params) {
72298c230c8SBjoern A. Zeeb 		scb = (struct epair_softc *)params;
72398c230c8SBjoern A. Zeeb 		ifp = scb->ifp;
72498c230c8SBjoern A. Zeeb 		/* Assign a hopefully unique, locally administered etheraddr. */
72598c230c8SBjoern A. Zeeb 		eaddr[0] = 0x02;
72698c230c8SBjoern A. Zeeb 		eaddr[3] = (ifp->if_index >> 8) & 0xff;
72798c230c8SBjoern A. Zeeb 		eaddr[4] = ifp->if_index & 0xff;
72898c230c8SBjoern A. Zeeb 		eaddr[5] = 0x0b;
72998c230c8SBjoern A. Zeeb 		ether_ifattach(ifp, eaddr);
73098c230c8SBjoern A. Zeeb 		/* Correctly set the name for the cloner list. */
73198c230c8SBjoern A. Zeeb 		strlcpy(name, scb->ifp->if_xname, len);
73298c230c8SBjoern A. Zeeb 		return (0);
73398c230c8SBjoern A. Zeeb 	}
73498c230c8SBjoern A. Zeeb 
73598c230c8SBjoern A. Zeeb 	/* Try to see if a special unit was requested. */
73698c230c8SBjoern A. Zeeb 	error = ifc_name2unit(name, &unit);
73798c230c8SBjoern A. Zeeb 	if (error != 0)
73898c230c8SBjoern A. Zeeb 		return (error);
73998c230c8SBjoern A. Zeeb 	wildcard = (unit < 0);
74098c230c8SBjoern A. Zeeb 
74198c230c8SBjoern A. Zeeb 	error = ifc_alloc_unit(ifc, &unit);
74298c230c8SBjoern A. Zeeb 	if (error != 0)
74398c230c8SBjoern A. Zeeb 		return (error);
74498c230c8SBjoern A. Zeeb 
74598c230c8SBjoern A. Zeeb 	/*
74698c230c8SBjoern A. Zeeb 	 * If no unit had been given, we need to adjust the ifName.
74798c230c8SBjoern A. Zeeb 	 * Also make sure there is space for our extra [ab] suffix.
74898c230c8SBjoern A. Zeeb 	 */
74998c230c8SBjoern A. Zeeb 	for (dp = name; *dp != '\0'; dp++);
75098c230c8SBjoern A. Zeeb 	if (wildcard) {
75198c230c8SBjoern A. Zeeb 		error = snprintf(dp, len - (dp - name), "%d", unit);
75298c230c8SBjoern A. Zeeb 		if (error > len - (dp - name) - 1) {
75398c230c8SBjoern A. Zeeb 			/* ifName too long. */
75498c230c8SBjoern A. Zeeb 			ifc_free_unit(ifc, unit);
75598c230c8SBjoern A. Zeeb 			return (ENOSPC);
75698c230c8SBjoern A. Zeeb 		}
75798c230c8SBjoern A. Zeeb 		dp += error;
75898c230c8SBjoern A. Zeeb 	}
75998c230c8SBjoern A. Zeeb 	if (len - (dp - name) - 1 < 1) {
76098c230c8SBjoern A. Zeeb 		/* No space left for our [ab] suffix. */
76198c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
76298c230c8SBjoern A. Zeeb 		return (ENOSPC);
76398c230c8SBjoern A. Zeeb 	}
76498c230c8SBjoern A. Zeeb 	*dp = 'a';
76598c230c8SBjoern A. Zeeb 	/* Must not change dp so we can replace 'a' by 'b' later. */
76698c230c8SBjoern A. Zeeb 	*(dp+1) = '\0';
76798c230c8SBjoern A. Zeeb 
76898c230c8SBjoern A. Zeeb 	/* Allocate memory for both [ab] interfaces */
76998c230c8SBjoern A. Zeeb 	sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
770d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
77198c230c8SBjoern A. Zeeb 	sca->ifp = if_alloc(IFT_ETHER);
77298c230c8SBjoern A. Zeeb 	if (sca->ifp == NULL) {
77398c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
77498c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
77598c230c8SBjoern A. Zeeb 		return (ENOSPC);
77698c230c8SBjoern A. Zeeb 	}
77798c230c8SBjoern A. Zeeb 
77898c230c8SBjoern A. Zeeb 	scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
779d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
78098c230c8SBjoern A. Zeeb 	scb->ifp = if_alloc(IFT_ETHER);
78198c230c8SBjoern A. Zeeb 	if (scb->ifp == NULL) {
78298c230c8SBjoern A. Zeeb 		free(scb, M_EPAIR);
78398c230c8SBjoern A. Zeeb 		if_free(sca->ifp);
78498c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
78598c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
78698c230c8SBjoern A. Zeeb 		return (ENOSPC);
78798c230c8SBjoern A. Zeeb 	}
78898c230c8SBjoern A. Zeeb 
78998c230c8SBjoern A. Zeeb 	/*
79098c230c8SBjoern A. Zeeb 	 * Cross-reference the interfaces so we will be able to free both.
79198c230c8SBjoern A. Zeeb 	 */
79298c230c8SBjoern A. Zeeb 	sca->oifp = scb->ifp;
79398c230c8SBjoern A. Zeeb 	scb->oifp = sca->ifp;
79498c230c8SBjoern A. Zeeb 
795d0ea4743SBjoern A. Zeeb 	/*
796d0ea4743SBjoern A. Zeeb 	 * Calculate the cpuid for netisr queueing based on the
797d0ea4743SBjoern A. Zeeb 	 * ifIndex of the interfaces. As long as we cannot configure
798d0ea4743SBjoern A. Zeeb 	 * this or use cpuset information easily we cannot guarantee
799d0ea4743SBjoern A. Zeeb 	 * cache locality but we can at least allow parallelism.
800d0ea4743SBjoern A. Zeeb 	 */
801d0ea4743SBjoern A. Zeeb 	sca->cpuid =
802d0ea4743SBjoern A. Zeeb 	    netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
803d0ea4743SBjoern A. Zeeb 	scb->cpuid =
804d0ea4743SBjoern A. Zeeb 	    netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
805d0ea4743SBjoern A. Zeeb 
80698c230c8SBjoern A. Zeeb 	/* Finish initialization of interface <n>a. */
80798c230c8SBjoern A. Zeeb 	ifp = sca->ifp;
80898c230c8SBjoern A. Zeeb 	ifp->if_softc = sca;
80998c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
81098c230c8SBjoern A. Zeeb 	ifp->if_dname = ifc->ifc_name;
81198c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
81298c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8139f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8149f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
81598c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
81698c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
81798c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
81898c230c8SBjoern A. Zeeb 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
81998c230c8SBjoern A. Zeeb 	/* Assign a hopefully unique, locally administered etheraddr. */
82098c230c8SBjoern A. Zeeb 	eaddr[0] = 0x02;
82198c230c8SBjoern A. Zeeb 	eaddr[3] = (ifp->if_index >> 8) & 0xff;
82298c230c8SBjoern A. Zeeb 	eaddr[4] = ifp->if_index & 0xff;
82398c230c8SBjoern A. Zeeb 	eaddr[5] = 0x0a;
82498c230c8SBjoern A. Zeeb 	ether_ifattach(ifp, eaddr);
82598c230c8SBjoern A. Zeeb 	sca->if_qflush = ifp->if_qflush;
82698c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
82798c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
82898c230c8SBjoern A. Zeeb 	ifp->if_baudrate = IF_Gbps(10UL);	/* arbitrary maximum */
82998c230c8SBjoern A. Zeeb 
83098c230c8SBjoern A. Zeeb 	/* Swap the name and finish initialization of interface <n>b. */
83198c230c8SBjoern A. Zeeb 	*dp = 'b';
83298c230c8SBjoern A. Zeeb 
83398c230c8SBjoern A. Zeeb 	ifp = scb->ifp;
83498c230c8SBjoern A. Zeeb 	ifp->if_softc = scb;
83598c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
83698c230c8SBjoern A. Zeeb 	ifp->if_dname = ifc->ifc_name;
83798c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
83898c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8399f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8409f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
84198c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
84298c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
84398c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
84498c230c8SBjoern A. Zeeb 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
84598c230c8SBjoern A. Zeeb 	/* We need to play some tricks here for the second interface. */
84698c230c8SBjoern A. Zeeb 	strlcpy(name, EPAIRNAME, len);
84798c230c8SBjoern A. Zeeb 	error = if_clone_create(name, len, (caddr_t)scb);
84898c230c8SBjoern A. Zeeb 	if (error)
84998c230c8SBjoern A. Zeeb 		panic("%s: if_clone_createif() for our 2nd iface failed: %d",
85098c230c8SBjoern A. Zeeb 		    __func__, error);
85198c230c8SBjoern A. Zeeb 	scb->if_qflush = ifp->if_qflush;
85298c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
85398c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
85498c230c8SBjoern A. Zeeb 	ifp->if_baudrate = IF_Gbps(10UL);	/* arbitrary maximum */
85598c230c8SBjoern A. Zeeb 
85698c230c8SBjoern A. Zeeb 	/*
85798c230c8SBjoern A. Zeeb 	 * Restore name to <n>a as the ifp for this will go into the
85898c230c8SBjoern A. Zeeb 	 * cloner list for the initial call.
85998c230c8SBjoern A. Zeeb 	 */
86098c230c8SBjoern A. Zeeb 	strlcpy(name, sca->ifp->if_xname, len);
86198c230c8SBjoern A. Zeeb 	DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
86298c230c8SBjoern A. Zeeb 
8632dccdd45SMarko Zec 	/* Initialise pseudo media types. */
8642dccdd45SMarko Zec 	ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
8652dccdd45SMarko Zec 	ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
8662dccdd45SMarko Zec 	ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
8672dccdd45SMarko Zec 	ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
8682dccdd45SMarko Zec 	ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
8692dccdd45SMarko Zec 	ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
8702dccdd45SMarko Zec 
87198c230c8SBjoern A. Zeeb 	/* Tell the world, that we are ready to rock. */
87298c230c8SBjoern A. Zeeb 	sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
87398c230c8SBjoern A. Zeeb 	scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
874c7493539SBjoern A. Zeeb 	if_link_state_change(sca->ifp, LINK_STATE_UP);
875c7493539SBjoern A. Zeeb 	if_link_state_change(scb->ifp, LINK_STATE_UP);
87698c230c8SBjoern A. Zeeb 
87798c230c8SBjoern A. Zeeb 	return (0);
87898c230c8SBjoern A. Zeeb }
87998c230c8SBjoern A. Zeeb 
88098c230c8SBjoern A. Zeeb static int
88198c230c8SBjoern A. Zeeb epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
88298c230c8SBjoern A. Zeeb {
88398c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
88498c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
88598c230c8SBjoern A. Zeeb 	int unit, error;
88698c230c8SBjoern A. Zeeb 
88798c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
88898c230c8SBjoern A. Zeeb 
88998c230c8SBjoern A. Zeeb 	/*
89098c230c8SBjoern A. Zeeb 	 * In case we called into if_clone_destroyif() ourselves
89198c230c8SBjoern A. Zeeb 	 * again to remove the second interface, the softc will be
89298c230c8SBjoern A. Zeeb 	 * NULL. In that case so not do anything but return success.
89398c230c8SBjoern A. Zeeb 	 */
89498c230c8SBjoern A. Zeeb 	if (ifp->if_softc == NULL)
89598c230c8SBjoern A. Zeeb 		return (0);
89698c230c8SBjoern A. Zeeb 
89798c230c8SBjoern A. Zeeb 	unit = ifp->if_dunit;
89898c230c8SBjoern A. Zeeb 	sca = ifp->if_softc;
89998c230c8SBjoern A. Zeeb 	oifp = sca->oifp;
90098c230c8SBjoern A. Zeeb 	scb = oifp->if_softc;
90198c230c8SBjoern A. Zeeb 
90298c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
903c7493539SBjoern A. Zeeb 	if_link_state_change(ifp, LINK_STATE_DOWN);
904c7493539SBjoern A. Zeeb 	if_link_state_change(oifp, LINK_STATE_DOWN);
90598c230c8SBjoern A. Zeeb 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
90698c230c8SBjoern A. Zeeb 	oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
90798c230c8SBjoern A. Zeeb 
90898c230c8SBjoern A. Zeeb 	/*
9097edc3d88SMikolaj Golub 	 * Get rid of our second half. As the other of the two
9107edc3d88SMikolaj Golub 	 * interfaces may reside in a different vnet, we need to
9117edc3d88SMikolaj Golub 	 * switch before freeing them.
91298c230c8SBjoern A. Zeeb 	 */
9137edc3d88SMikolaj Golub 	CURVNET_SET_QUIET(oifp->if_vnet);
9147edc3d88SMikolaj Golub 	ether_ifdetach(oifp);
9157edc3d88SMikolaj Golub 	/*
9167edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9177edc3d88SMikolaj Golub 	 * The numbers can only go down as the interface is
9187edc3d88SMikolaj Golub 	 * detached so there is no need to use atomics.
9197edc3d88SMikolaj Golub 	 */
9207edc3d88SMikolaj Golub 	DPRINTF("scb refcnt=%u\n", scb->refcount);
9217edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
9227edc3d88SMikolaj Golub 	    ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
92398c230c8SBjoern A. Zeeb 	oifp->if_softc = NULL;
92498c230c8SBjoern A. Zeeb 	error = if_clone_destroyif(ifc, oifp);
92598c230c8SBjoern A. Zeeb 	if (error)
92698c230c8SBjoern A. Zeeb 		panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
92798c230c8SBjoern A. Zeeb 		    __func__, error);
92873e39d61SBjoern A. Zeeb 	if_free(oifp);
9292dccdd45SMarko Zec 	ifmedia_removeall(&scb->media);
93098c230c8SBjoern A. Zeeb 	free(scb, M_EPAIR);
9317edc3d88SMikolaj Golub 	CURVNET_RESTORE();
9327edc3d88SMikolaj Golub 
9337edc3d88SMikolaj Golub 	ether_ifdetach(ifp);
9347edc3d88SMikolaj Golub 	/*
9357edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9367edc3d88SMikolaj Golub 	 */
9377edc3d88SMikolaj Golub 	DPRINTF("sca refcnt=%u\n", sca->refcount);
9387edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
9397edc3d88SMikolaj Golub 	    ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
9407edc3d88SMikolaj Golub 	if_free(ifp);
9417edc3d88SMikolaj Golub 	ifmedia_removeall(&sca->media);
94298c230c8SBjoern A. Zeeb 	free(sca, M_EPAIR);
94398c230c8SBjoern A. Zeeb 	ifc_free_unit(ifc, unit);
94498c230c8SBjoern A. Zeeb 
94598c230c8SBjoern A. Zeeb 	return (0);
94698c230c8SBjoern A. Zeeb }
94798c230c8SBjoern A. Zeeb 
94898c230c8SBjoern A. Zeeb static int
94998c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data)
95098c230c8SBjoern A. Zeeb {
951d0ea4743SBjoern A. Zeeb 	int qlimit;
95298c230c8SBjoern A. Zeeb 
95398c230c8SBjoern A. Zeeb 	switch (type) {
95498c230c8SBjoern A. Zeeb 	case MOD_LOAD:
95598c230c8SBjoern A. Zeeb 		/* For now limit us to one global mutex and one inq. */
956d0ea4743SBjoern A. Zeeb 		epair_dpcpu_init();
957d0ea4743SBjoern A. Zeeb 		epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
958d0ea4743SBjoern A. Zeeb 		if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
959d0ea4743SBjoern A. Zeeb 		    epair_nh.nh_qlimit = qlimit;
960d0ea4743SBjoern A. Zeeb 		netisr_register(&epair_nh);
96198c230c8SBjoern A. Zeeb 		if_clone_attach(&epair_cloner);
96298c230c8SBjoern A. Zeeb 		if (bootverbose)
96398c230c8SBjoern A. Zeeb 			printf("%s initialized.\n", EPAIRNAME);
96498c230c8SBjoern A. Zeeb 		break;
96598c230c8SBjoern A. Zeeb 	case MOD_UNLOAD:
96698c230c8SBjoern A. Zeeb 		if_clone_detach(&epair_cloner);
967d0ea4743SBjoern A. Zeeb 		netisr_unregister(&epair_nh);
968d0ea4743SBjoern A. Zeeb 		epair_dpcpu_detach();
96998c230c8SBjoern A. Zeeb 		if (bootverbose)
97098c230c8SBjoern A. Zeeb 			printf("%s unloaded.\n", EPAIRNAME);
97198c230c8SBjoern A. Zeeb 		break;
97298c230c8SBjoern A. Zeeb 	default:
97398c230c8SBjoern A. Zeeb 		return (EOPNOTSUPP);
97498c230c8SBjoern A. Zeeb 	}
97598c230c8SBjoern A. Zeeb 	return (0);
97698c230c8SBjoern A. Zeeb }
97798c230c8SBjoern A. Zeeb 
97898c230c8SBjoern A. Zeeb static moduledata_t epair_mod = {
97998c230c8SBjoern A. Zeeb 	"if_epair",
98098c230c8SBjoern A. Zeeb 	epair_modevent,
981*9823d527SKevin Lo 	0
98298c230c8SBjoern A. Zeeb };
98398c230c8SBjoern A. Zeeb 
98498c230c8SBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
98598c230c8SBjoern A. Zeeb MODULE_VERSION(if_epair, 1);
986