xref: /freebsd/sys/net/if_epair.c (revision 11d416666d2f58209d4e0f154be76ffc5bdce128)
198c230c8SBjoern A. Zeeb /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
498c230c8SBjoern A. Zeeb  * Copyright (c) 2008 The FreeBSD Foundation
5eea3faf7SBjoern A. Zeeb  * Copyright (c) 2009-2010 Bjoern A. Zeeb <bz@FreeBSD.org>
698c230c8SBjoern A. Zeeb  * All rights reserved.
798c230c8SBjoern A. Zeeb  *
898c230c8SBjoern A. Zeeb  * This software was developed by CK Software GmbH under sponsorship
998c230c8SBjoern A. Zeeb  * from the FreeBSD Foundation.
1098c230c8SBjoern A. Zeeb  *
1198c230c8SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
1298c230c8SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
1398c230c8SBjoern A. Zeeb  * are met:
1498c230c8SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
1598c230c8SBjoern A. Zeeb  * notice, this list of conditions and the following disclaimer.
1698c230c8SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
1798c230c8SBjoern A. Zeeb  * notice, this list of conditions and the following disclaimer in the
1898c230c8SBjoern A. Zeeb  * documentation and/or other materials provided with the distribution.
1998c230c8SBjoern A. Zeeb  *
2098c230c8SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2198c230c8SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2298c230c8SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2398c230c8SBjoern A. Zeeb  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2498c230c8SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2598c230c8SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2698c230c8SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2798c230c8SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2898c230c8SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2998c230c8SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3098c230c8SBjoern A. Zeeb  * SUCH DAMAGE.
3198c230c8SBjoern A. Zeeb  */
3298c230c8SBjoern A. Zeeb 
3398c230c8SBjoern A. Zeeb /*
34d0ea4743SBjoern A. Zeeb  * A pair of virtual back-to-back connected ethernet like interfaces
35d0ea4743SBjoern A. Zeeb  * (``two interfaces with a virtual cross-over cable'').
36d0ea4743SBjoern A. Zeeb  *
3798c230c8SBjoern A. Zeeb  * This is mostly intended to be used to provide connectivity between
3898c230c8SBjoern A. Zeeb  * different virtual network stack instances.
3998c230c8SBjoern A. Zeeb  */
4098c230c8SBjoern A. Zeeb /*
4198c230c8SBjoern A. Zeeb  * Things to re-think once we have more experience:
42d0ea4743SBjoern A. Zeeb  * - ifp->if_reassign function once we can test with vimage. Depending on
43530c0060SRobert Watson  *   how if_vmove() is going to be improved.
44d0ea4743SBjoern A. Zeeb  * - Real random etheraddrs that are checked to be uniquish; we would need
45d0ea4743SBjoern A. Zeeb  *   to re-do them in case we move the interface between network stacks
46d0ea4743SBjoern A. Zeeb  *   in a private if_reassign function.
47d0ea4743SBjoern A. Zeeb  *   In case we bridge to a real interface/network or between indepedent
48d0ea4743SBjoern A. Zeeb  *   epairs on multiple stacks/machines, we may need this.
49d0ea4743SBjoern A. Zeeb  *   For now let the user handle that case.
5098c230c8SBjoern A. Zeeb  */
5198c230c8SBjoern A. Zeeb 
5298c230c8SBjoern A. Zeeb #include <sys/cdefs.h>
5398c230c8SBjoern A. Zeeb __FBSDID("$FreeBSD$");
5498c230c8SBjoern A. Zeeb 
5598c230c8SBjoern A. Zeeb #include <sys/param.h>
56*11d41666SLuca Pizzamiglio #include <sys/hash.h>
57*11d41666SLuca Pizzamiglio #include <sys/jail.h>
5898c230c8SBjoern A. Zeeb #include <sys/kernel.h>
59*11d41666SLuca Pizzamiglio #include <sys/libkern.h>
608ec07310SGleb Smirnoff #include <sys/malloc.h>
6198c230c8SBjoern A. Zeeb #include <sys/mbuf.h>
6298c230c8SBjoern A. Zeeb #include <sys/module.h>
63*11d41666SLuca Pizzamiglio #include <sys/proc.h>
6498c230c8SBjoern A. Zeeb #include <sys/refcount.h>
6598c230c8SBjoern A. Zeeb #include <sys/queue.h>
66d0ea4743SBjoern A. Zeeb #include <sys/smp.h>
6798c230c8SBjoern A. Zeeb #include <sys/socket.h>
6898c230c8SBjoern A. Zeeb #include <sys/sockio.h>
6998c230c8SBjoern A. Zeeb #include <sys/sysctl.h>
7098c230c8SBjoern A. Zeeb #include <sys/types.h>
7198c230c8SBjoern A. Zeeb 
7298c230c8SBjoern A. Zeeb #include <net/bpf.h>
7398c230c8SBjoern A. Zeeb #include <net/ethernet.h>
7498c230c8SBjoern A. Zeeb #include <net/if.h>
7576039bc8SGleb Smirnoff #include <net/if_var.h>
7698c230c8SBjoern A. Zeeb #include <net/if_clone.h>
772dccdd45SMarko Zec #include <net/if_media.h>
7898c230c8SBjoern A. Zeeb #include <net/if_var.h>
7998c230c8SBjoern A. Zeeb #include <net/if_types.h>
8098c230c8SBjoern A. Zeeb #include <net/netisr.h>
81530c0060SRobert Watson #include <net/vnet.h>
8298c230c8SBjoern A. Zeeb 
8398c230c8SBjoern A. Zeeb SYSCTL_DECL(_net_link);
846472ac3dSEd Schouten static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
85d0ea4743SBjoern A. Zeeb 
86d0ea4743SBjoern A. Zeeb #ifdef EPAIR_DEBUG
87d0ea4743SBjoern A. Zeeb static int epair_debug = 0;
882c8b047cSBjoern A. Zeeb SYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
8998c230c8SBjoern A. Zeeb     &epair_debug, 0, "if_epair(4) debugging.");
90d0ea4743SBjoern A. Zeeb #define	DPRINTF(fmt, arg...)						\
91d0ea4743SBjoern A. Zeeb 	if (epair_debug)						\
9298c230c8SBjoern A. Zeeb 		printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
9398c230c8SBjoern A. Zeeb #else
9498c230c8SBjoern A. Zeeb #define	DPRINTF(fmt, arg...)
9598c230c8SBjoern A. Zeeb #endif
9698c230c8SBjoern A. Zeeb 
97d0ea4743SBjoern A. Zeeb static void epair_nh_sintr(struct mbuf *);
98d0ea4743SBjoern A. Zeeb static struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
99d0ea4743SBjoern A. Zeeb static void epair_nh_drainedcpu(u_int);
10098c230c8SBjoern A. Zeeb 
101d0ea4743SBjoern A. Zeeb static void epair_start_locked(struct ifnet *);
1022dccdd45SMarko Zec static int epair_media_change(struct ifnet *);
1032dccdd45SMarko Zec static void epair_media_status(struct ifnet *, struct ifmediareq *);
10498c230c8SBjoern A. Zeeb 
10598c230c8SBjoern A. Zeeb static int epair_clone_match(struct if_clone *, const char *);
10698c230c8SBjoern A. Zeeb static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
10798c230c8SBjoern A. Zeeb static int epair_clone_destroy(struct if_clone *, struct ifnet *);
10898c230c8SBjoern A. Zeeb 
10942a58907SGleb Smirnoff static const char epairname[] = "epair";
11042a58907SGleb Smirnoff 
1113c3136b1SHiroki Sato /* Netisr related definitions and sysctl. */
112d0ea4743SBjoern A. Zeeb static struct netisr_handler epair_nh = {
11342a58907SGleb Smirnoff 	.nh_name	= epairname,
114d0ea4743SBjoern A. Zeeb 	.nh_proto	= NETISR_EPAIR,
115d0ea4743SBjoern A. Zeeb 	.nh_policy	= NETISR_POLICY_CPU,
116d0ea4743SBjoern A. Zeeb 	.nh_handler	= epair_nh_sintr,
117d0ea4743SBjoern A. Zeeb 	.nh_m2cpuid	= epair_nh_m2cpuid,
118d0ea4743SBjoern A. Zeeb 	.nh_drainedcpu	= epair_nh_drainedcpu,
119d0ea4743SBjoern A. Zeeb };
120d0ea4743SBjoern A. Zeeb 
121d0ea4743SBjoern A. Zeeb static int
122d0ea4743SBjoern A. Zeeb sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
123d0ea4743SBjoern A. Zeeb {
124d0ea4743SBjoern A. Zeeb 	int error, qlimit;
125d0ea4743SBjoern A. Zeeb 
126d0ea4743SBjoern A. Zeeb 	netisr_getqlimit(&epair_nh, &qlimit);
127d0ea4743SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
128d0ea4743SBjoern A. Zeeb 	if (error || !req->newptr)
129d0ea4743SBjoern A. Zeeb 		return (error);
130d0ea4743SBjoern A. Zeeb 	if (qlimit < 1)
131d0ea4743SBjoern A. Zeeb 		return (EINVAL);
132d0ea4743SBjoern A. Zeeb 	return (netisr_setqlimit(&epair_nh, qlimit));
133d0ea4743SBjoern A. Zeeb }
134d0ea4743SBjoern A. Zeeb SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
135d0ea4743SBjoern A. Zeeb     0, 0, sysctl_epair_netisr_maxqlen, "I",
136d0ea4743SBjoern A. Zeeb     "Maximum if_epair(4) netisr \"hw\" queue length");
137d0ea4743SBjoern A. Zeeb 
138d0ea4743SBjoern A. Zeeb struct epair_softc {
139d0ea4743SBjoern A. Zeeb 	struct ifnet	*ifp;		/* This ifp. */
140d0ea4743SBjoern A. Zeeb 	struct ifnet	*oifp;		/* other ifp of pair. */
1412dccdd45SMarko Zec 	struct ifmedia	media;		/* Media config (fake). */
142d0ea4743SBjoern A. Zeeb 	u_int		refcount;	/* # of mbufs in flight. */
143d0ea4743SBjoern A. Zeeb 	u_int		cpuid;		/* CPU ID assigned upon creation. */
144d0ea4743SBjoern A. Zeeb 	void		(*if_qflush)(struct ifnet *);
145d0ea4743SBjoern A. Zeeb 					/* Original if_qflush routine. */
146d0ea4743SBjoern A. Zeeb };
147d0ea4743SBjoern A. Zeeb 
148d0ea4743SBjoern A. Zeeb /*
149d0ea4743SBjoern A. Zeeb  * Per-CPU list of ifps with data in the ifq that needs to be flushed
150d0ea4743SBjoern A. Zeeb  * to the netisr ``hw'' queue before we allow any further direct queuing
151d0ea4743SBjoern A. Zeeb  * to the ``hw'' queue.
152d0ea4743SBjoern A. Zeeb  */
153d0ea4743SBjoern A. Zeeb struct epair_ifp_drain {
154d0ea4743SBjoern A. Zeeb 	STAILQ_ENTRY(epair_ifp_drain)	ifp_next;
155d0ea4743SBjoern A. Zeeb 	struct ifnet			*ifp;
156d0ea4743SBjoern A. Zeeb };
157d0ea4743SBjoern A. Zeeb STAILQ_HEAD(eid_list, epair_ifp_drain);
158d0ea4743SBjoern A. Zeeb 
159d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_INIT(dpcpu)		mtx_init(&(dpcpu)->if_epair_mtx, \
160d0ea4743SBjoern A. Zeeb 					    "if_epair", NULL, MTX_DEF)
161d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_DESTROY(dpcpu)	mtx_destroy(&(dpcpu)->if_epair_mtx)
162d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK_ASSERT(dpcpu)	mtx_assert(&(dpcpu)->if_epair_mtx, \
163d0ea4743SBjoern A. Zeeb 					    MA_OWNED)
164d0ea4743SBjoern A. Zeeb #define	EPAIR_LOCK(dpcpu)		mtx_lock(&(dpcpu)->if_epair_mtx)
165d0ea4743SBjoern A. Zeeb #define	EPAIR_UNLOCK(dpcpu)		mtx_unlock(&(dpcpu)->if_epair_mtx)
166d0ea4743SBjoern A. Zeeb 
167d0ea4743SBjoern A. Zeeb #ifdef INVARIANTS
168d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)	refcount_init((r), (v))
169d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)	refcount_acquire((r))
170d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)	refcount_release((r))
171d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)	KASSERT(a, p)
172d0ea4743SBjoern A. Zeeb #else
173d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_INIT(r, v)
174d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_AQUIRE(r)
175d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_RELEASE(r)
176d0ea4743SBjoern A. Zeeb #define	EPAIR_REFCOUNT_ASSERT(a, p)
177d0ea4743SBjoern A. Zeeb #endif
178d0ea4743SBjoern A. Zeeb 
17942a58907SGleb Smirnoff static MALLOC_DEFINE(M_EPAIR, epairname,
180d0ea4743SBjoern A. Zeeb     "Pair of virtual cross-over connected Ethernet-like interfaces");
18198c230c8SBjoern A. Zeeb 
1823c3136b1SHiroki Sato static VNET_DEFINE(struct if_clone *, epair_cloner);
1833c3136b1SHiroki Sato #define	V_epair_cloner	VNET(epair_cloner)
18498c230c8SBjoern A. Zeeb 
185d0ea4743SBjoern A. Zeeb /*
186d0ea4743SBjoern A. Zeeb  * DPCPU area and functions.
187d0ea4743SBjoern A. Zeeb  */
188d0ea4743SBjoern A. Zeeb struct epair_dpcpu {
189d0ea4743SBjoern A. Zeeb 	struct mtx	if_epair_mtx;		/* Per-CPU locking. */
190d0ea4743SBjoern A. Zeeb 	int		epair_drv_flags;	/* Per-CPU ``hw'' drv flags. */
191d0ea4743SBjoern A. Zeeb 	struct eid_list	epair_ifp_drain_list;	/* Per-CPU list of ifps with
192d0ea4743SBjoern A. Zeeb 						 * data in the ifq. */
193d0ea4743SBjoern A. Zeeb };
194d0ea4743SBjoern A. Zeeb DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
195d0ea4743SBjoern A. Zeeb 
196d0ea4743SBjoern A. Zeeb static void
197d0ea4743SBjoern A. Zeeb epair_dpcpu_init(void)
198d0ea4743SBjoern A. Zeeb {
199d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
200d0ea4743SBjoern A. Zeeb 	struct eid_list *s;
201d0ea4743SBjoern A. Zeeb 	u_int cpuid;
202d0ea4743SBjoern A. Zeeb 
2033aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
204d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
205d0ea4743SBjoern A. Zeeb 
206d0ea4743SBjoern A. Zeeb 		/* Initialize per-cpu lock. */
207d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_INIT(epair_dpcpu);
208d0ea4743SBjoern A. Zeeb 
209d0ea4743SBjoern A. Zeeb 		/* Driver flags are per-cpu as are our netisr "hw" queues. */
210d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags = 0;
211d0ea4743SBjoern A. Zeeb 
212d0ea4743SBjoern A. Zeeb 		/*
213d0ea4743SBjoern A. Zeeb 		 * Initialize per-cpu drain list.
214d0ea4743SBjoern A. Zeeb 		 * Manually do what STAILQ_HEAD_INITIALIZER would do.
215d0ea4743SBjoern A. Zeeb 		 */
216d0ea4743SBjoern A. Zeeb 		s = &epair_dpcpu->epair_ifp_drain_list;
217d0ea4743SBjoern A. Zeeb 		s->stqh_first = NULL;
218d0ea4743SBjoern A. Zeeb 		s->stqh_last = &s->stqh_first;
219d0ea4743SBjoern A. Zeeb 	}
220d0ea4743SBjoern A. Zeeb }
221d0ea4743SBjoern A. Zeeb 
222d0ea4743SBjoern A. Zeeb static void
223d0ea4743SBjoern A. Zeeb epair_dpcpu_detach(void)
224d0ea4743SBjoern A. Zeeb {
225d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
226d0ea4743SBjoern A. Zeeb 	u_int cpuid;
227d0ea4743SBjoern A. Zeeb 
2283aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
229d0ea4743SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
230d0ea4743SBjoern A. Zeeb 
231d0ea4743SBjoern A. Zeeb 		/* Destroy per-cpu lock. */
232d0ea4743SBjoern A. Zeeb 		EPAIR_LOCK_DESTROY(epair_dpcpu);
233d0ea4743SBjoern A. Zeeb 	}
234d0ea4743SBjoern A. Zeeb }
235d0ea4743SBjoern A. Zeeb 
236d0ea4743SBjoern A. Zeeb /*
237d0ea4743SBjoern A. Zeeb  * Helper functions.
238d0ea4743SBjoern A. Zeeb  */
239d0ea4743SBjoern A. Zeeb static u_int
240d0ea4743SBjoern A. Zeeb cpuid_from_ifp(struct ifnet *ifp)
241d0ea4743SBjoern A. Zeeb {
242d0ea4743SBjoern A. Zeeb 	struct epair_softc *sc;
243d0ea4743SBjoern A. Zeeb 
244d0ea4743SBjoern A. Zeeb 	if (ifp == NULL)
245d0ea4743SBjoern A. Zeeb 		return (0);
246d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
247d0ea4743SBjoern A. Zeeb 
248d0ea4743SBjoern A. Zeeb 	return (sc->cpuid);
249d0ea4743SBjoern A. Zeeb }
25098c230c8SBjoern A. Zeeb 
25198c230c8SBjoern A. Zeeb /*
25298c230c8SBjoern A. Zeeb  * Netisr handler functions.
25398c230c8SBjoern A. Zeeb  */
25498c230c8SBjoern A. Zeeb static void
255d0ea4743SBjoern A. Zeeb epair_nh_sintr(struct mbuf *m)
25698c230c8SBjoern A. Zeeb {
25798c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
25846d0f824SMatt Macy 	struct epair_softc *sc __unused;
25998c230c8SBjoern A. Zeeb 
26098c230c8SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
26198c230c8SBjoern A. Zeeb 	(*ifp->if_input)(ifp, m);
26298c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
263d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_RELEASE(&sc->refcount);
264eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
265eea3faf7SBjoern A. Zeeb 	    ("%s: ifp=%p sc->refcount not >= 1: %d",
266eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc->refcount));
26798c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
26898c230c8SBjoern A. Zeeb }
26998c230c8SBjoern A. Zeeb 
270d0ea4743SBjoern A. Zeeb static struct mbuf *
271d0ea4743SBjoern A. Zeeb epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
27298c230c8SBjoern A. Zeeb {
273d0ea4743SBjoern A. Zeeb 
274d0ea4743SBjoern A. Zeeb 	*cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
275d0ea4743SBjoern A. Zeeb 
276d0ea4743SBjoern A. Zeeb 	return (m);
277d0ea4743SBjoern A. Zeeb }
278d0ea4743SBjoern A. Zeeb 
279d0ea4743SBjoern A. Zeeb static void
280d0ea4743SBjoern A. Zeeb epair_nh_drainedcpu(u_int cpuid)
281d0ea4743SBjoern A. Zeeb {
282d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
28398c230c8SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
28498c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
28598c230c8SBjoern A. Zeeb 
286d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
287d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
28898c230c8SBjoern A. Zeeb 	/*
28998c230c8SBjoern A. Zeeb 	 * Assume our "hw" queue and possibly ifq will be emptied
29098c230c8SBjoern A. Zeeb 	 * again. In case we will overflow the "hw" queue while
29198c230c8SBjoern A. Zeeb 	 * draining, epair_start_locked will set IFF_DRV_OACTIVE
29298c230c8SBjoern A. Zeeb 	 * again and we will stop and return.
29398c230c8SBjoern A. Zeeb 	 */
294d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
295d0ea4743SBjoern A. Zeeb 	    ifp_next, tvar) {
29698c230c8SBjoern A. Zeeb 		ifp = elm->ifp;
297d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
29898c230c8SBjoern A. Zeeb 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
29998c230c8SBjoern A. Zeeb 		epair_start_locked(ifp);
30098c230c8SBjoern A. Zeeb 
30198c230c8SBjoern A. Zeeb 		IFQ_LOCK(&ifp->if_snd);
30298c230c8SBjoern A. Zeeb 		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
30346d0f824SMatt Macy 			struct epair_softc *sc __unused;
304eea3faf7SBjoern A. Zeeb 
305d0ea4743SBjoern A. Zeeb 			STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
306d0ea4743SBjoern A. Zeeb 			    elm, epair_ifp_drain, ifp_next);
307eea3faf7SBjoern A. Zeeb 			/* The cached ifp goes off the list. */
308eea3faf7SBjoern A. Zeeb 			sc = ifp->if_softc;
309eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
310eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
311eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
312eea3faf7SBjoern A. Zeeb 			    __func__, ifp, sc->refcount));
31398c230c8SBjoern A. Zeeb 			free(elm, M_EPAIR);
31498c230c8SBjoern A. Zeeb 		}
31598c230c8SBjoern A. Zeeb 		IFQ_UNLOCK(&ifp->if_snd);
31698c230c8SBjoern A. Zeeb 
31798c230c8SBjoern A. Zeeb 		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
31898c230c8SBjoern A. Zeeb 			/* Our "hw"q overflew again. */
3192c8b047cSBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
32098c230c8SBjoern A. Zeeb 			DPRINTF("hw queue length overflow at %u\n",
321d0ea4743SBjoern A. Zeeb 			    epair_nh.nh_qlimit);
32298c230c8SBjoern A. Zeeb 			break;
32398c230c8SBjoern A. Zeeb 		}
32498c230c8SBjoern A. Zeeb 	}
325d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
32698c230c8SBjoern A. Zeeb }
32798c230c8SBjoern A. Zeeb 
32898c230c8SBjoern A. Zeeb /*
32998c230c8SBjoern A. Zeeb  * Network interface (`if') related functions.
33098c230c8SBjoern A. Zeeb  */
331eea3faf7SBjoern A. Zeeb static void
332eea3faf7SBjoern A. Zeeb epair_remove_ifp_from_draining(struct ifnet *ifp)
333eea3faf7SBjoern A. Zeeb {
334eea3faf7SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
335eea3faf7SBjoern A. Zeeb 	struct epair_ifp_drain *elm, *tvar;
336eea3faf7SBjoern A. Zeeb 	u_int cpuid;
337eea3faf7SBjoern A. Zeeb 
3383aa6d94eSJohn Baldwin 	CPU_FOREACH(cpuid) {
339eea3faf7SBjoern A. Zeeb 		epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
340eea3faf7SBjoern A. Zeeb 		EPAIR_LOCK(epair_dpcpu);
341eea3faf7SBjoern A. Zeeb 		STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
342eea3faf7SBjoern A. Zeeb 		    ifp_next, tvar) {
343eea3faf7SBjoern A. Zeeb 			if (ifp == elm->ifp) {
34446d0f824SMatt Macy 				struct epair_softc *sc __unused;
345eea3faf7SBjoern A. Zeeb 
346eea3faf7SBjoern A. Zeeb 				STAILQ_REMOVE(
347eea3faf7SBjoern A. Zeeb 				    &epair_dpcpu->epair_ifp_drain_list, elm,
348eea3faf7SBjoern A. Zeeb 				    epair_ifp_drain, ifp_next);
349eea3faf7SBjoern A. Zeeb 				/* The cached ifp goes off the list. */
350eea3faf7SBjoern A. Zeeb 				sc = ifp->if_softc;
351eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_RELEASE(&sc->refcount);
352eea3faf7SBjoern A. Zeeb 				EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
353eea3faf7SBjoern A. Zeeb 				    ("%s: ifp=%p sc->refcount not >= 1: %d",
354eea3faf7SBjoern A. Zeeb 				    __func__, ifp, sc->refcount));
355eea3faf7SBjoern A. Zeeb 				free(elm, M_EPAIR);
356eea3faf7SBjoern A. Zeeb 			}
357eea3faf7SBjoern A. Zeeb 		}
358eea3faf7SBjoern A. Zeeb 		EPAIR_UNLOCK(epair_dpcpu);
359eea3faf7SBjoern A. Zeeb 	}
360eea3faf7SBjoern A. Zeeb }
361eea3faf7SBjoern A. Zeeb 
362d0ea4743SBjoern A. Zeeb static int
363d0ea4743SBjoern A. Zeeb epair_add_ifp_for_draining(struct ifnet *ifp)
364d0ea4743SBjoern A. Zeeb {
365d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
366eea3faf7SBjoern A. Zeeb 	struct epair_softc *sc;
367d0ea4743SBjoern A. Zeeb 	struct epair_ifp_drain *elm = NULL;
368d0ea4743SBjoern A. Zeeb 
369eea3faf7SBjoern A. Zeeb 	sc = ifp->if_softc;
370d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
371eea3faf7SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
372d0ea4743SBjoern A. Zeeb 	STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
373d0ea4743SBjoern A. Zeeb 		if (elm->ifp == ifp)
374d0ea4743SBjoern A. Zeeb 			break;
3753c20163aSBjoern A. Zeeb 	/* If the ifp is there already, return success. */
376d0ea4743SBjoern A. Zeeb 	if (elm != NULL)
377d0ea4743SBjoern A. Zeeb 		return (0);
378d0ea4743SBjoern A. Zeeb 
379d0ea4743SBjoern A. Zeeb 	elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
380d0ea4743SBjoern A. Zeeb 	if (elm == NULL)
381d0ea4743SBjoern A. Zeeb 		return (ENOMEM);
382d0ea4743SBjoern A. Zeeb 
383d0ea4743SBjoern A. Zeeb 	elm->ifp = ifp;
384eea3faf7SBjoern A. Zeeb 	/* Add a reference for the ifp pointer on the list. */
385eea3faf7SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
386d0ea4743SBjoern A. Zeeb 	STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
387d0ea4743SBjoern A. Zeeb 
388d0ea4743SBjoern A. Zeeb 	return (0);
389d0ea4743SBjoern A. Zeeb }
390d0ea4743SBjoern A. Zeeb 
39198c230c8SBjoern A. Zeeb static void
39298c230c8SBjoern A. Zeeb epair_start_locked(struct ifnet *ifp)
39398c230c8SBjoern A. Zeeb {
394d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
39598c230c8SBjoern A. Zeeb 	struct mbuf *m;
39698c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
39798c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
39898c230c8SBjoern A. Zeeb 	int error;
39998c230c8SBjoern A. Zeeb 
40098c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
401d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
402d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
403d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
40498c230c8SBjoern A. Zeeb 
40598c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
40698c230c8SBjoern A. Zeeb 		return;
40798c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0)
40898c230c8SBjoern A. Zeeb 		return;
40998c230c8SBjoern A. Zeeb 
41098c230c8SBjoern A. Zeeb 	/*
41159f35a82SPatrick Kelsey 	 * We get packets here from ether_output via if_handoff()
41275580d58SPatrick Kelsey 	 * and need to put them into the input queue of the oifp
41398c230c8SBjoern A. Zeeb 	 * and call oifp->if_input() via netisr/epair_sintr().
41498c230c8SBjoern A. Zeeb 	 */
41598c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
41698c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
41798c230c8SBjoern A. Zeeb 	for (;;) {
41898c230c8SBjoern A. Zeeb 		IFQ_DEQUEUE(&ifp->if_snd, m);
41998c230c8SBjoern A. Zeeb 		if (m == NULL)
42098c230c8SBjoern A. Zeeb 			break;
42198c230c8SBjoern A. Zeeb 		BPF_MTAP(ifp, m);
42298c230c8SBjoern A. Zeeb 
42398c230c8SBjoern A. Zeeb 		/*
42498c230c8SBjoern A. Zeeb 		 * In case the outgoing interface is not usable,
42598c230c8SBjoern A. Zeeb 		 * drop the packet.
42698c230c8SBjoern A. Zeeb 		 */
42798c230c8SBjoern A. Zeeb 		if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
42898c230c8SBjoern A. Zeeb 		    (oifp->if_flags & IFF_UP) ==0) {
4293751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
43098c230c8SBjoern A. Zeeb 			m_freem(m);
43198c230c8SBjoern A. Zeeb 			continue;
43298c230c8SBjoern A. Zeeb 		}
43398c230c8SBjoern A. Zeeb 		DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
43498c230c8SBjoern A. Zeeb 
43598c230c8SBjoern A. Zeeb 		/*
43698c230c8SBjoern A. Zeeb 		 * Add a reference so the interface cannot go while the
43798c230c8SBjoern A. Zeeb 		 * packet is in transit as we rely on rcvif to stay valid.
43898c230c8SBjoern A. Zeeb 		 */
439d0ea4743SBjoern A. Zeeb 		EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
44098c230c8SBjoern A. Zeeb 		m->m_pkthdr.rcvif = oifp;
44198c230c8SBjoern A. Zeeb 		CURVNET_SET_QUIET(oifp->if_vnet);
44298c230c8SBjoern A. Zeeb 		error = netisr_queue(NETISR_EPAIR, m);
44398c230c8SBjoern A. Zeeb 		CURVNET_RESTORE();
44498c230c8SBjoern A. Zeeb 		if (!error) {
4453751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
44698c230c8SBjoern A. Zeeb 			/* Someone else received the packet. */
4473751dddbSGleb Smirnoff 			if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
44898c230c8SBjoern A. Zeeb 		} else {
449eea3faf7SBjoern A. Zeeb 			/* The packet was freed already. */
450d0ea4743SBjoern A. Zeeb 			epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
45198c230c8SBjoern A. Zeeb 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
452eea3faf7SBjoern A. Zeeb 			(void) epair_add_ifp_for_draining(ifp);
4533751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
454d0ea4743SBjoern A. Zeeb 			EPAIR_REFCOUNT_RELEASE(&sc->refcount);
455eea3faf7SBjoern A. Zeeb 			EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
456eea3faf7SBjoern A. Zeeb 			    ("%s: ifp=%p sc->refcount not >= 1: %d",
457eea3faf7SBjoern A. Zeeb 			    __func__, oifp, sc->refcount));
45898c230c8SBjoern A. Zeeb 		}
45998c230c8SBjoern A. Zeeb 	}
46098c230c8SBjoern A. Zeeb }
46198c230c8SBjoern A. Zeeb 
46298c230c8SBjoern A. Zeeb static void
46398c230c8SBjoern A. Zeeb epair_start(struct ifnet *ifp)
46498c230c8SBjoern A. Zeeb {
465d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
46698c230c8SBjoern A. Zeeb 
467d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
468d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
46998c230c8SBjoern A. Zeeb 	epair_start_locked(ifp);
470d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
47198c230c8SBjoern A. Zeeb }
47298c230c8SBjoern A. Zeeb 
47398c230c8SBjoern A. Zeeb static int
47498c230c8SBjoern A. Zeeb epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
47598c230c8SBjoern A. Zeeb {
476d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
47798c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
47898c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
47998c230c8SBjoern A. Zeeb 	int error, len;
48098c230c8SBjoern A. Zeeb 	short mflags;
48198c230c8SBjoern A. Zeeb 
48298c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p m=%p\n", ifp, m);
483d0ea4743SBjoern A. Zeeb 	sc = ifp->if_softc;
484d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
485d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK_ASSERT(epair_dpcpu);
48698c230c8SBjoern A. Zeeb 
48798c230c8SBjoern A. Zeeb 	if (m == NULL)
48898c230c8SBjoern A. Zeeb 		return (0);
48998c230c8SBjoern A. Zeeb 
49098c230c8SBjoern A. Zeeb 	/*
49198c230c8SBjoern A. Zeeb 	 * We are not going to use the interface en/dequeue mechanism
49298c230c8SBjoern A. Zeeb 	 * on the TX side. We are called from ether_output_frame()
49398c230c8SBjoern A. Zeeb 	 * and will put the packet into the incoming queue of the
49498c230c8SBjoern A. Zeeb 	 * other interface of our pair via the netsir.
49598c230c8SBjoern A. Zeeb 	 */
49698c230c8SBjoern A. Zeeb 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
49798c230c8SBjoern A. Zeeb 		m_freem(m);
49898c230c8SBjoern A. Zeeb 		return (ENXIO);
49998c230c8SBjoern A. Zeeb 	}
50098c230c8SBjoern A. Zeeb 	if ((ifp->if_flags & IFF_UP) == 0) {
50198c230c8SBjoern A. Zeeb 		m_freem(m);
50298c230c8SBjoern A. Zeeb 		return (ENETDOWN);
50398c230c8SBjoern A. Zeeb 	}
50498c230c8SBjoern A. Zeeb 
50598c230c8SBjoern A. Zeeb 	BPF_MTAP(ifp, m);
50698c230c8SBjoern A. Zeeb 
50798c230c8SBjoern A. Zeeb 	/*
50898c230c8SBjoern A. Zeeb 	 * In case the outgoing interface is not usable,
50998c230c8SBjoern A. Zeeb 	 * drop the packet.
51098c230c8SBjoern A. Zeeb 	 */
51198c230c8SBjoern A. Zeeb 	oifp = sc->oifp;
51298c230c8SBjoern A. Zeeb 	if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
51398c230c8SBjoern A. Zeeb 	    (oifp->if_flags & IFF_UP) ==0) {
5143751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
51598c230c8SBjoern A. Zeeb 		m_freem(m);
51698c230c8SBjoern A. Zeeb 		return (0);
51798c230c8SBjoern A. Zeeb 	}
51898c230c8SBjoern A. Zeeb 	len = m->m_pkthdr.len;
51998c230c8SBjoern A. Zeeb 	mflags = m->m_flags;
52098c230c8SBjoern A. Zeeb 	DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
52198c230c8SBjoern A. Zeeb 
52298c230c8SBjoern A. Zeeb #ifdef ALTQ
523a4641f4eSPedro F. Giffuni 	/* Support ALTQ via the classic if_start() path. */
52498c230c8SBjoern A. Zeeb 	IF_LOCK(&ifp->if_snd);
52598c230c8SBjoern A. Zeeb 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
52698c230c8SBjoern A. Zeeb 		ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
52798c230c8SBjoern A. Zeeb 		if (error)
5283751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
52998c230c8SBjoern A. Zeeb 		IF_UNLOCK(&ifp->if_snd);
53098c230c8SBjoern A. Zeeb 		if (!error) {
5313751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
53298c230c8SBjoern A. Zeeb 			if (mflags & (M_BCAST|M_MCAST))
5333751dddbSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
53498c230c8SBjoern A. Zeeb 
53598c230c8SBjoern A. Zeeb 			if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
53698c230c8SBjoern A. Zeeb 				epair_start_locked(ifp);
53798c230c8SBjoern A. Zeeb 			else
538d0ea4743SBjoern A. Zeeb 				(void)epair_add_ifp_for_draining(ifp);
53998c230c8SBjoern A. Zeeb 		}
54098c230c8SBjoern A. Zeeb 		return (error);
54198c230c8SBjoern A. Zeeb 	}
54298c230c8SBjoern A. Zeeb 	IF_UNLOCK(&ifp->if_snd);
54398c230c8SBjoern A. Zeeb #endif
54498c230c8SBjoern A. Zeeb 
545d0ea4743SBjoern A. Zeeb 	if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
54698c230c8SBjoern A. Zeeb 		/*
54798c230c8SBjoern A. Zeeb 		 * Our hardware queue is full, try to fall back
54898c230c8SBjoern A. Zeeb 		 * queuing to the ifq but do not call ifp->if_start.
54998c230c8SBjoern A. Zeeb 		 * Either we are lucky or the packet is gone.
55098c230c8SBjoern A. Zeeb 		 */
55198c230c8SBjoern A. Zeeb 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
55298c230c8SBjoern A. Zeeb 		if (!error)
553d0ea4743SBjoern A. Zeeb 			(void)epair_add_ifp_for_draining(ifp);
55498c230c8SBjoern A. Zeeb 		return (error);
55598c230c8SBjoern A. Zeeb 	}
55698c230c8SBjoern A. Zeeb 	sc = oifp->if_softc;
55798c230c8SBjoern A. Zeeb 	/*
55898c230c8SBjoern A. Zeeb 	 * Add a reference so the interface cannot go while the
55998c230c8SBjoern A. Zeeb 	 * packet is in transit as we rely on rcvif to stay valid.
56098c230c8SBjoern A. Zeeb 	 */
561d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
56298c230c8SBjoern A. Zeeb 	m->m_pkthdr.rcvif = oifp;
56398c230c8SBjoern A. Zeeb 	CURVNET_SET_QUIET(oifp->if_vnet);
56498c230c8SBjoern A. Zeeb 	error = netisr_queue(NETISR_EPAIR, m);
56598c230c8SBjoern A. Zeeb 	CURVNET_RESTORE();
56698c230c8SBjoern A. Zeeb 	if (!error) {
5673751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
56898c230c8SBjoern A. Zeeb 		/*
56998c230c8SBjoern A. Zeeb 		 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
57098c230c8SBjoern A. Zeeb 		 * but as we bypass all this we have to duplicate
57198c230c8SBjoern A. Zeeb 		 * the logic another time.
57298c230c8SBjoern A. Zeeb 		 */
5733751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
57498c230c8SBjoern A. Zeeb 		if (mflags & (M_BCAST|M_MCAST))
5753751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
57698c230c8SBjoern A. Zeeb 		/* Someone else received the packet. */
5773751dddbSGleb Smirnoff 		if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
57898c230c8SBjoern A. Zeeb 	} else {
57998c230c8SBjoern A. Zeeb 		/* The packet was freed already. */
580d0ea4743SBjoern A. Zeeb 		epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
58198c230c8SBjoern A. Zeeb 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5823751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
583eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_RELEASE(&sc->refcount);
584eea3faf7SBjoern A. Zeeb 		EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
585eea3faf7SBjoern A. Zeeb 		    ("%s: ifp=%p sc->refcount not >= 1: %d",
586eea3faf7SBjoern A. Zeeb 		    __func__, oifp, sc->refcount));
58798c230c8SBjoern A. Zeeb 	}
58898c230c8SBjoern A. Zeeb 
58998c230c8SBjoern A. Zeeb 	return (error);
59098c230c8SBjoern A. Zeeb }
59198c230c8SBjoern A. Zeeb 
59298c230c8SBjoern A. Zeeb static int
59398c230c8SBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m)
59498c230c8SBjoern A. Zeeb {
595d0ea4743SBjoern A. Zeeb 	struct epair_dpcpu *epair_dpcpu;
59698c230c8SBjoern A. Zeeb 	int error;
59798c230c8SBjoern A. Zeeb 
598d0ea4743SBjoern A. Zeeb 	epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
599d0ea4743SBjoern A. Zeeb 	EPAIR_LOCK(epair_dpcpu);
60098c230c8SBjoern A. Zeeb 	error = epair_transmit_locked(ifp, m);
601d0ea4743SBjoern A. Zeeb 	EPAIR_UNLOCK(epair_dpcpu);
60298c230c8SBjoern A. Zeeb 	return (error);
60398c230c8SBjoern A. Zeeb }
60498c230c8SBjoern A. Zeeb 
60598c230c8SBjoern A. Zeeb static void
60698c230c8SBjoern A. Zeeb epair_qflush(struct ifnet *ifp)
60798c230c8SBjoern A. Zeeb {
60898c230c8SBjoern A. Zeeb 	struct epair_softc *sc;
60998c230c8SBjoern A. Zeeb 
61098c230c8SBjoern A. Zeeb 	sc = ifp->if_softc;
611eea3faf7SBjoern A. Zeeb 	KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
612eea3faf7SBjoern A. Zeeb 	    __func__, ifp, sc));
61398c230c8SBjoern A. Zeeb 	/*
614eea3faf7SBjoern A. Zeeb 	 * Remove this ifp from all backpointer lists. The interface will not
615eea3faf7SBjoern A. Zeeb 	 * usable for flushing anyway nor should it have anything to flush
616eea3faf7SBjoern A. Zeeb 	 * after if_qflush().
61798c230c8SBjoern A. Zeeb 	 */
618eea3faf7SBjoern A. Zeeb 	epair_remove_ifp_from_draining(ifp);
619eea3faf7SBjoern A. Zeeb 
62098c230c8SBjoern A. Zeeb 	if (sc->if_qflush)
62198c230c8SBjoern A. Zeeb 		sc->if_qflush(ifp);
62298c230c8SBjoern A. Zeeb }
62398c230c8SBjoern A. Zeeb 
62498c230c8SBjoern A. Zeeb static int
6252dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused)
6262dccdd45SMarko Zec {
6272dccdd45SMarko Zec 
6282dccdd45SMarko Zec 	/* Do nothing. */
6292dccdd45SMarko Zec 	return (0);
6302dccdd45SMarko Zec }
6312dccdd45SMarko Zec 
6322dccdd45SMarko Zec static void
6332dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
6342dccdd45SMarko Zec {
6352dccdd45SMarko Zec 
6362dccdd45SMarko Zec 	imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
6372dccdd45SMarko Zec 	imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
6382dccdd45SMarko Zec }
6392dccdd45SMarko Zec 
6402dccdd45SMarko Zec static int
64198c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
64298c230c8SBjoern A. Zeeb {
6432dccdd45SMarko Zec 	struct epair_softc *sc;
64498c230c8SBjoern A. Zeeb 	struct ifreq *ifr;
64598c230c8SBjoern A. Zeeb 	int error;
64698c230c8SBjoern A. Zeeb 
64798c230c8SBjoern A. Zeeb 	ifr = (struct ifreq *)data;
64898c230c8SBjoern A. Zeeb 	switch (cmd) {
64998c230c8SBjoern A. Zeeb 	case SIOCSIFFLAGS:
65098c230c8SBjoern A. Zeeb 	case SIOCADDMULTI:
65198c230c8SBjoern A. Zeeb 	case SIOCDELMULTI:
65298c230c8SBjoern A. Zeeb 		error = 0;
65398c230c8SBjoern A. Zeeb 		break;
65498c230c8SBjoern A. Zeeb 
6552dccdd45SMarko Zec 	case SIOCSIFMEDIA:
6562dccdd45SMarko Zec 	case SIOCGIFMEDIA:
6572dccdd45SMarko Zec 		sc = ifp->if_softc;
6582dccdd45SMarko Zec 		error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
6592dccdd45SMarko Zec 		break;
6602dccdd45SMarko Zec 
661d0ea4743SBjoern A. Zeeb 	case SIOCSIFMTU:
662d0ea4743SBjoern A. Zeeb 		/* We basically allow all kinds of MTUs. */
663d0ea4743SBjoern A. Zeeb 		ifp->if_mtu = ifr->ifr_mtu;
664d0ea4743SBjoern A. Zeeb 		error = 0;
665d0ea4743SBjoern A. Zeeb 		break;
666d0ea4743SBjoern A. Zeeb 
66798c230c8SBjoern A. Zeeb 	default:
66898c230c8SBjoern A. Zeeb 		/* Let the common ethernet handler process this. */
66998c230c8SBjoern A. Zeeb 		error = ether_ioctl(ifp, cmd, data);
67098c230c8SBjoern A. Zeeb 		break;
67198c230c8SBjoern A. Zeeb 	}
67298c230c8SBjoern A. Zeeb 
67398c230c8SBjoern A. Zeeb 	return (error);
67498c230c8SBjoern A. Zeeb }
67598c230c8SBjoern A. Zeeb 
67698c230c8SBjoern A. Zeeb static void
67798c230c8SBjoern A. Zeeb epair_init(void *dummy __unused)
67898c230c8SBjoern A. Zeeb {
67998c230c8SBjoern A. Zeeb }
68098c230c8SBjoern A. Zeeb 
68198c230c8SBjoern A. Zeeb 
68298c230c8SBjoern A. Zeeb /*
68398c230c8SBjoern A. Zeeb  * Interface cloning functions.
68498c230c8SBjoern A. Zeeb  * We use our private ones so that we can create/destroy our secondary
68598c230c8SBjoern A. Zeeb  * device along with the primary one.
68698c230c8SBjoern A. Zeeb  */
68798c230c8SBjoern A. Zeeb static int
68898c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name)
68998c230c8SBjoern A. Zeeb {
69098c230c8SBjoern A. Zeeb 	const char *cp;
69198c230c8SBjoern A. Zeeb 
69298c230c8SBjoern A. Zeeb 	DPRINTF("name='%s'\n", name);
69398c230c8SBjoern A. Zeeb 
69498c230c8SBjoern A. Zeeb 	/*
69598c230c8SBjoern A. Zeeb 	 * Our base name is epair.
69698c230c8SBjoern A. Zeeb 	 * Our interfaces will be named epair<n>[ab].
69798c230c8SBjoern A. Zeeb 	 * So accept anything of the following list:
69898c230c8SBjoern A. Zeeb 	 * - epair
69998c230c8SBjoern A. Zeeb 	 * - epair<n>
70098c230c8SBjoern A. Zeeb 	 * but not the epair<n>[ab] versions.
70198c230c8SBjoern A. Zeeb 	 */
70242a58907SGleb Smirnoff 	if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
70398c230c8SBjoern A. Zeeb 		return (0);
70498c230c8SBjoern A. Zeeb 
70542a58907SGleb Smirnoff 	for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
70698c230c8SBjoern A. Zeeb 		if (*cp < '0' || *cp > '9')
70798c230c8SBjoern A. Zeeb 			return (0);
70898c230c8SBjoern A. Zeeb 	}
70998c230c8SBjoern A. Zeeb 
71098c230c8SBjoern A. Zeeb 	return (1);
71198c230c8SBjoern A. Zeeb }
71298c230c8SBjoern A. Zeeb 
71398c230c8SBjoern A. Zeeb static int
71498c230c8SBjoern A. Zeeb epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
71598c230c8SBjoern A. Zeeb {
71698c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
71798c230c8SBjoern A. Zeeb 	struct ifnet *ifp;
71898c230c8SBjoern A. Zeeb 	char *dp;
71998c230c8SBjoern A. Zeeb 	int error, unit, wildcard;
720*11d41666SLuca Pizzamiglio 	uint64_t hostid;
721*11d41666SLuca Pizzamiglio 	uint32_t key[3];
722*11d41666SLuca Pizzamiglio 	uint32_t hash;
72398c230c8SBjoern A. Zeeb 	uint8_t eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
72498c230c8SBjoern A. Zeeb 
72598c230c8SBjoern A. Zeeb 	/*
72698c230c8SBjoern A. Zeeb 	 * We are abusing params to create our second interface.
72742a58907SGleb Smirnoff 	 * Actually we already created it and called if_clone_create()
72898c230c8SBjoern A. Zeeb 	 * for it to do the official insertion procedure the moment we knew
72998c230c8SBjoern A. Zeeb 	 * it cannot fail anymore. So just do attach it here.
73098c230c8SBjoern A. Zeeb 	 */
73198c230c8SBjoern A. Zeeb 	if (params) {
73298c230c8SBjoern A. Zeeb 		scb = (struct epair_softc *)params;
73398c230c8SBjoern A. Zeeb 		ifp = scb->ifp;
734*11d41666SLuca Pizzamiglio 		/* Copy epairNa etheraddr and change the last byte. */
735*11d41666SLuca Pizzamiglio 		memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
73698c230c8SBjoern A. Zeeb 		eaddr[5] = 0x0b;
73798c230c8SBjoern A. Zeeb 		ether_ifattach(ifp, eaddr);
73898c230c8SBjoern A. Zeeb 		/* Correctly set the name for the cloner list. */
739*11d41666SLuca Pizzamiglio 		strlcpy(name, ifp->if_xname, len);
74098c230c8SBjoern A. Zeeb 		return (0);
74198c230c8SBjoern A. Zeeb 	}
74298c230c8SBjoern A. Zeeb 
74398c230c8SBjoern A. Zeeb 	/* Try to see if a special unit was requested. */
74498c230c8SBjoern A. Zeeb 	error = ifc_name2unit(name, &unit);
74598c230c8SBjoern A. Zeeb 	if (error != 0)
74698c230c8SBjoern A. Zeeb 		return (error);
74798c230c8SBjoern A. Zeeb 	wildcard = (unit < 0);
74898c230c8SBjoern A. Zeeb 
74998c230c8SBjoern A. Zeeb 	error = ifc_alloc_unit(ifc, &unit);
75098c230c8SBjoern A. Zeeb 	if (error != 0)
75198c230c8SBjoern A. Zeeb 		return (error);
75298c230c8SBjoern A. Zeeb 
75398c230c8SBjoern A. Zeeb 	/*
75498c230c8SBjoern A. Zeeb 	 * If no unit had been given, we need to adjust the ifName.
75598c230c8SBjoern A. Zeeb 	 * Also make sure there is space for our extra [ab] suffix.
75698c230c8SBjoern A. Zeeb 	 */
75798c230c8SBjoern A. Zeeb 	for (dp = name; *dp != '\0'; dp++);
75898c230c8SBjoern A. Zeeb 	if (wildcard) {
75998c230c8SBjoern A. Zeeb 		error = snprintf(dp, len - (dp - name), "%d", unit);
76098c230c8SBjoern A. Zeeb 		if (error > len - (dp - name) - 1) {
76198c230c8SBjoern A. Zeeb 			/* ifName too long. */
76298c230c8SBjoern A. Zeeb 			ifc_free_unit(ifc, unit);
76398c230c8SBjoern A. Zeeb 			return (ENOSPC);
76498c230c8SBjoern A. Zeeb 		}
76598c230c8SBjoern A. Zeeb 		dp += error;
76698c230c8SBjoern A. Zeeb 	}
76798c230c8SBjoern A. Zeeb 	if (len - (dp - name) - 1 < 1) {
76898c230c8SBjoern A. Zeeb 		/* No space left for our [ab] suffix. */
76998c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
77098c230c8SBjoern A. Zeeb 		return (ENOSPC);
77198c230c8SBjoern A. Zeeb 	}
7723c3136b1SHiroki Sato 	*dp = 'b';
77398c230c8SBjoern A. Zeeb 	/* Must not change dp so we can replace 'a' by 'b' later. */
77498c230c8SBjoern A. Zeeb 	*(dp+1) = '\0';
77598c230c8SBjoern A. Zeeb 
7763c3136b1SHiroki Sato 	/* Check if 'a' and 'b' interfaces already exist. */
7773c3136b1SHiroki Sato 	if (ifunit(name) != NULL)
7783c3136b1SHiroki Sato 		return (EEXIST);
7793c3136b1SHiroki Sato 	*dp = 'a';
7803c3136b1SHiroki Sato 	if (ifunit(name) != NULL)
7813c3136b1SHiroki Sato 		return (EEXIST);
7823c3136b1SHiroki Sato 
78398c230c8SBjoern A. Zeeb 	/* Allocate memory for both [ab] interfaces */
78498c230c8SBjoern A. Zeeb 	sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
785d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
78698c230c8SBjoern A. Zeeb 	sca->ifp = if_alloc(IFT_ETHER);
78798c230c8SBjoern A. Zeeb 	if (sca->ifp == NULL) {
78898c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
78998c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
79098c230c8SBjoern A. Zeeb 		return (ENOSPC);
79198c230c8SBjoern A. Zeeb 	}
79298c230c8SBjoern A. Zeeb 
79398c230c8SBjoern A. Zeeb 	scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
794d0ea4743SBjoern A. Zeeb 	EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
79598c230c8SBjoern A. Zeeb 	scb->ifp = if_alloc(IFT_ETHER);
79698c230c8SBjoern A. Zeeb 	if (scb->ifp == NULL) {
79798c230c8SBjoern A. Zeeb 		free(scb, M_EPAIR);
79898c230c8SBjoern A. Zeeb 		if_free(sca->ifp);
79998c230c8SBjoern A. Zeeb 		free(sca, M_EPAIR);
80098c230c8SBjoern A. Zeeb 		ifc_free_unit(ifc, unit);
80198c230c8SBjoern A. Zeeb 		return (ENOSPC);
80298c230c8SBjoern A. Zeeb 	}
80398c230c8SBjoern A. Zeeb 
80498c230c8SBjoern A. Zeeb 	/*
80598c230c8SBjoern A. Zeeb 	 * Cross-reference the interfaces so we will be able to free both.
80698c230c8SBjoern A. Zeeb 	 */
80798c230c8SBjoern A. Zeeb 	sca->oifp = scb->ifp;
80898c230c8SBjoern A. Zeeb 	scb->oifp = sca->ifp;
80998c230c8SBjoern A. Zeeb 
810d0ea4743SBjoern A. Zeeb 	/*
811d0ea4743SBjoern A. Zeeb 	 * Calculate the cpuid for netisr queueing based on the
812d0ea4743SBjoern A. Zeeb 	 * ifIndex of the interfaces. As long as we cannot configure
813d0ea4743SBjoern A. Zeeb 	 * this or use cpuset information easily we cannot guarantee
814d0ea4743SBjoern A. Zeeb 	 * cache locality but we can at least allow parallelism.
815d0ea4743SBjoern A. Zeeb 	 */
816d0ea4743SBjoern A. Zeeb 	sca->cpuid =
817fdf95c0bSAndrey V. Elsukov 	    netisr_get_cpuid(sca->ifp->if_index);
818d0ea4743SBjoern A. Zeeb 	scb->cpuid =
819fdf95c0bSAndrey V. Elsukov 	    netisr_get_cpuid(scb->ifp->if_index);
820d0ea4743SBjoern A. Zeeb 
82118e199adSHiroki Sato 	/* Initialise pseudo media types. */
82218e199adSHiroki Sato 	ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
82318e199adSHiroki Sato 	ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
82418e199adSHiroki Sato 	ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
82518e199adSHiroki Sato 	ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
82618e199adSHiroki Sato 	ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
82718e199adSHiroki Sato 	ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
82818e199adSHiroki Sato 
82998c230c8SBjoern A. Zeeb 	/* Finish initialization of interface <n>a. */
83098c230c8SBjoern A. Zeeb 	ifp = sca->ifp;
83198c230c8SBjoern A. Zeeb 	ifp->if_softc = sca;
83298c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
83342a58907SGleb Smirnoff 	ifp->if_dname = epairname;
83498c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
83598c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8369f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8379f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
83898c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
83998c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
84098c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
8414e950412SErmal Luçi 	if_setsendqlen(ifp, ifqmaxlen);
8424e950412SErmal Luçi 	if_setsendqready(ifp);
843*11d41666SLuca Pizzamiglio 
844*11d41666SLuca Pizzamiglio 	/*
845*11d41666SLuca Pizzamiglio 	 * Calculate the etheraddr hashing the hostid and the
846*11d41666SLuca Pizzamiglio 	 * interface index. The result would be hopefully unique
847*11d41666SLuca Pizzamiglio 	 */
848*11d41666SLuca Pizzamiglio 	getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
849*11d41666SLuca Pizzamiglio 	if (hostid == 0)
850*11d41666SLuca Pizzamiglio 		arc4rand(&hostid, sizeof(hostid), 0);
851*11d41666SLuca Pizzamiglio 	key[0] = (uint32_t)ifp->if_index;
852*11d41666SLuca Pizzamiglio 	key[1] = (uint32_t)(hostid & 0xffffffff);
853*11d41666SLuca Pizzamiglio 	key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
854*11d41666SLuca Pizzamiglio 	hash = jenkins_hash32(key, 3, 0);
855*11d41666SLuca Pizzamiglio 
85698c230c8SBjoern A. Zeeb 	eaddr[0] = 0x02;
857*11d41666SLuca Pizzamiglio 	memcpy(&eaddr[1], &hash, 4);
85898c230c8SBjoern A. Zeeb 	eaddr[5] = 0x0a;
85998c230c8SBjoern A. Zeeb 	ether_ifattach(ifp, eaddr);
86098c230c8SBjoern A. Zeeb 	sca->if_qflush = ifp->if_qflush;
86198c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
86298c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
863b245f96cSGleb Smirnoff 	ifp->if_baudrate = IF_Gbps(10);	/* arbitrary maximum */
86498c230c8SBjoern A. Zeeb 
86598c230c8SBjoern A. Zeeb 	/* Swap the name and finish initialization of interface <n>b. */
86698c230c8SBjoern A. Zeeb 	*dp = 'b';
86798c230c8SBjoern A. Zeeb 
86898c230c8SBjoern A. Zeeb 	ifp = scb->ifp;
86998c230c8SBjoern A. Zeeb 	ifp->if_softc = scb;
87098c230c8SBjoern A. Zeeb 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
87142a58907SGleb Smirnoff 	ifp->if_dname = epairname;
87298c230c8SBjoern A. Zeeb 	ifp->if_dunit = unit;
87398c230c8SBjoern A. Zeeb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
8749f8cab7fSMarko Zec 	ifp->if_capabilities = IFCAP_VLAN_MTU;
8759f8cab7fSMarko Zec 	ifp->if_capenable = IFCAP_VLAN_MTU;
87698c230c8SBjoern A. Zeeb 	ifp->if_start = epair_start;
87798c230c8SBjoern A. Zeeb 	ifp->if_ioctl = epair_ioctl;
87898c230c8SBjoern A. Zeeb 	ifp->if_init  = epair_init;
8794e950412SErmal Luçi 	if_setsendqlen(ifp, ifqmaxlen);
8804e950412SErmal Luçi 	if_setsendqready(ifp);
88198c230c8SBjoern A. Zeeb 	/* We need to play some tricks here for the second interface. */
88242a58907SGleb Smirnoff 	strlcpy(name, epairname, len);
88398c230c8SBjoern A. Zeeb 	error = if_clone_create(name, len, (caddr_t)scb);
88498c230c8SBjoern A. Zeeb 	if (error)
88542a58907SGleb Smirnoff 		panic("%s: if_clone_create() for our 2nd iface failed: %d",
88698c230c8SBjoern A. Zeeb 		    __func__, error);
88798c230c8SBjoern A. Zeeb 	scb->if_qflush = ifp->if_qflush;
88898c230c8SBjoern A. Zeeb 	ifp->if_qflush = epair_qflush;
88998c230c8SBjoern A. Zeeb 	ifp->if_transmit = epair_transmit;
890b245f96cSGleb Smirnoff 	ifp->if_baudrate = IF_Gbps(10);	/* arbitrary maximum */
89198c230c8SBjoern A. Zeeb 
89298c230c8SBjoern A. Zeeb 	/*
89398c230c8SBjoern A. Zeeb 	 * Restore name to <n>a as the ifp for this will go into the
89498c230c8SBjoern A. Zeeb 	 * cloner list for the initial call.
89598c230c8SBjoern A. Zeeb 	 */
89698c230c8SBjoern A. Zeeb 	strlcpy(name, sca->ifp->if_xname, len);
89798c230c8SBjoern A. Zeeb 	DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
89898c230c8SBjoern A. Zeeb 
89998c230c8SBjoern A. Zeeb 	/* Tell the world, that we are ready to rock. */
90098c230c8SBjoern A. Zeeb 	sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
90198c230c8SBjoern A. Zeeb 	scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
902c7493539SBjoern A. Zeeb 	if_link_state_change(sca->ifp, LINK_STATE_UP);
903c7493539SBjoern A. Zeeb 	if_link_state_change(scb->ifp, LINK_STATE_UP);
90498c230c8SBjoern A. Zeeb 
90598c230c8SBjoern A. Zeeb 	return (0);
90698c230c8SBjoern A. Zeeb }
90798c230c8SBjoern A. Zeeb 
90898c230c8SBjoern A. Zeeb static int
90998c230c8SBjoern A. Zeeb epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
91098c230c8SBjoern A. Zeeb {
91198c230c8SBjoern A. Zeeb 	struct ifnet *oifp;
91298c230c8SBjoern A. Zeeb 	struct epair_softc *sca, *scb;
91398c230c8SBjoern A. Zeeb 	int unit, error;
91498c230c8SBjoern A. Zeeb 
91598c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p\n", ifp);
91698c230c8SBjoern A. Zeeb 
91798c230c8SBjoern A. Zeeb 	/*
91898c230c8SBjoern A. Zeeb 	 * In case we called into if_clone_destroyif() ourselves
91998c230c8SBjoern A. Zeeb 	 * again to remove the second interface, the softc will be
92098c230c8SBjoern A. Zeeb 	 * NULL. In that case so not do anything but return success.
92198c230c8SBjoern A. Zeeb 	 */
92298c230c8SBjoern A. Zeeb 	if (ifp->if_softc == NULL)
92398c230c8SBjoern A. Zeeb 		return (0);
92498c230c8SBjoern A. Zeeb 
92598c230c8SBjoern A. Zeeb 	unit = ifp->if_dunit;
92698c230c8SBjoern A. Zeeb 	sca = ifp->if_softc;
92798c230c8SBjoern A. Zeeb 	oifp = sca->oifp;
92898c230c8SBjoern A. Zeeb 	scb = oifp->if_softc;
92998c230c8SBjoern A. Zeeb 
93098c230c8SBjoern A. Zeeb 	DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
931c7493539SBjoern A. Zeeb 	if_link_state_change(ifp, LINK_STATE_DOWN);
932c7493539SBjoern A. Zeeb 	if_link_state_change(oifp, LINK_STATE_DOWN);
93398c230c8SBjoern A. Zeeb 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
93498c230c8SBjoern A. Zeeb 	oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
93598c230c8SBjoern A. Zeeb 
93698c230c8SBjoern A. Zeeb 	/*
9377edc3d88SMikolaj Golub 	 * Get rid of our second half. As the other of the two
9387edc3d88SMikolaj Golub 	 * interfaces may reside in a different vnet, we need to
9397edc3d88SMikolaj Golub 	 * switch before freeing them.
94098c230c8SBjoern A. Zeeb 	 */
9417edc3d88SMikolaj Golub 	CURVNET_SET_QUIET(oifp->if_vnet);
9427edc3d88SMikolaj Golub 	ether_ifdetach(oifp);
9437edc3d88SMikolaj Golub 	/*
9447edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9457edc3d88SMikolaj Golub 	 * The numbers can only go down as the interface is
9467edc3d88SMikolaj Golub 	 * detached so there is no need to use atomics.
9477edc3d88SMikolaj Golub 	 */
9487edc3d88SMikolaj Golub 	DPRINTF("scb refcnt=%u\n", scb->refcount);
9497edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
9507edc3d88SMikolaj Golub 	    ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
95198c230c8SBjoern A. Zeeb 	oifp->if_softc = NULL;
95298c230c8SBjoern A. Zeeb 	error = if_clone_destroyif(ifc, oifp);
95398c230c8SBjoern A. Zeeb 	if (error)
95498c230c8SBjoern A. Zeeb 		panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
95598c230c8SBjoern A. Zeeb 		    __func__, error);
95673e39d61SBjoern A. Zeeb 	if_free(oifp);
9572dccdd45SMarko Zec 	ifmedia_removeall(&scb->media);
95898c230c8SBjoern A. Zeeb 	free(scb, M_EPAIR);
9597edc3d88SMikolaj Golub 	CURVNET_RESTORE();
9607edc3d88SMikolaj Golub 
9617edc3d88SMikolaj Golub 	ether_ifdetach(ifp);
9627edc3d88SMikolaj Golub 	/*
9637edc3d88SMikolaj Golub 	 * Wait for all packets to be dispatched to if_input.
9647edc3d88SMikolaj Golub 	 */
9657edc3d88SMikolaj Golub 	DPRINTF("sca refcnt=%u\n", sca->refcount);
9667edc3d88SMikolaj Golub 	EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
9677edc3d88SMikolaj Golub 	    ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
9687edc3d88SMikolaj Golub 	if_free(ifp);
9697edc3d88SMikolaj Golub 	ifmedia_removeall(&sca->media);
97098c230c8SBjoern A. Zeeb 	free(sca, M_EPAIR);
97198c230c8SBjoern A. Zeeb 	ifc_free_unit(ifc, unit);
97298c230c8SBjoern A. Zeeb 
97398c230c8SBjoern A. Zeeb 	return (0);
97498c230c8SBjoern A. Zeeb }
97598c230c8SBjoern A. Zeeb 
9763c3136b1SHiroki Sato static void
9773c3136b1SHiroki Sato vnet_epair_init(const void *unused __unused)
9783c3136b1SHiroki Sato {
9793c3136b1SHiroki Sato 
9803c3136b1SHiroki Sato 	V_epair_cloner = if_clone_advanced(epairname, 0,
9813c3136b1SHiroki Sato 	    epair_clone_match, epair_clone_create, epair_clone_destroy);
982484149deSBjoern A. Zeeb #ifdef VIMAGE
983484149deSBjoern A. Zeeb 	netisr_register_vnet(&epair_nh);
984484149deSBjoern A. Zeeb #endif
9853c3136b1SHiroki Sato }
98689856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
9873c3136b1SHiroki Sato     vnet_epair_init, NULL);
9883c3136b1SHiroki Sato 
9893c3136b1SHiroki Sato static void
9903c3136b1SHiroki Sato vnet_epair_uninit(const void *unused __unused)
9913c3136b1SHiroki Sato {
9923c3136b1SHiroki Sato 
993484149deSBjoern A. Zeeb #ifdef VIMAGE
994484149deSBjoern A. Zeeb 	netisr_unregister_vnet(&epair_nh);
995484149deSBjoern A. Zeeb #endif
9963c3136b1SHiroki Sato 	if_clone_detach(V_epair_cloner);
9973c3136b1SHiroki Sato }
99889856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
9993c3136b1SHiroki Sato     vnet_epair_uninit, NULL);
10003c3136b1SHiroki Sato 
100185f330e5SKristof Provost static void
100285f330e5SKristof Provost epair_uninit(const void *unused __unused)
100385f330e5SKristof Provost {
100485f330e5SKristof Provost 	netisr_unregister(&epair_nh);
100585f330e5SKristof Provost 	epair_dpcpu_detach();
100685f330e5SKristof Provost 	if (bootverbose)
100785f330e5SKristof Provost 		printf("%s unloaded.\n", epairname);
100885f330e5SKristof Provost }
100985f330e5SKristof Provost SYSUNINIT(epair_uninit, SI_SUB_INIT_IF, SI_ORDER_MIDDLE,
101085f330e5SKristof Provost     epair_uninit, NULL);
101185f330e5SKristof Provost 
101298c230c8SBjoern A. Zeeb static int
101398c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data)
101498c230c8SBjoern A. Zeeb {
1015d0ea4743SBjoern A. Zeeb 	int qlimit;
101698c230c8SBjoern A. Zeeb 
101798c230c8SBjoern A. Zeeb 	switch (type) {
101898c230c8SBjoern A. Zeeb 	case MOD_LOAD:
101998c230c8SBjoern A. Zeeb 		/* For now limit us to one global mutex and one inq. */
1020d0ea4743SBjoern A. Zeeb 		epair_dpcpu_init();
1021d0ea4743SBjoern A. Zeeb 		epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
1022d0ea4743SBjoern A. Zeeb 		if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
1023d0ea4743SBjoern A. Zeeb 		    epair_nh.nh_qlimit = qlimit;
1024d0ea4743SBjoern A. Zeeb 		netisr_register(&epair_nh);
102598c230c8SBjoern A. Zeeb 		if (bootverbose)
102642a58907SGleb Smirnoff 			printf("%s initialized.\n", epairname);
102798c230c8SBjoern A. Zeeb 		break;
102898c230c8SBjoern A. Zeeb 	case MOD_UNLOAD:
102985f330e5SKristof Provost 		/* Handled in epair_uninit() */
103098c230c8SBjoern A. Zeeb 		break;
103198c230c8SBjoern A. Zeeb 	default:
103298c230c8SBjoern A. Zeeb 		return (EOPNOTSUPP);
103398c230c8SBjoern A. Zeeb 	}
103498c230c8SBjoern A. Zeeb 	return (0);
103598c230c8SBjoern A. Zeeb }
103698c230c8SBjoern A. Zeeb 
103798c230c8SBjoern A. Zeeb static moduledata_t epair_mod = {
103898c230c8SBjoern A. Zeeb 	"if_epair",
103998c230c8SBjoern A. Zeeb 	epair_modevent,
10409823d527SKevin Lo 	0
104198c230c8SBjoern A. Zeeb };
104298c230c8SBjoern A. Zeeb 
104389856f7eSBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
104498c230c8SBjoern A. Zeeb MODULE_VERSION(if_epair, 1);
1045