2 * Copyright (c) 2008 The FreeBSD Foundation
3 * Copyright (c) 2009-2010 Bjoern A. Zeeb <bz@FreeBSD.org>
6 * This software was developed by CK Software GmbH under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * A pair of virtual back-to-back connected ethernet like interfaces
33 * (``two interfaces with a virtual cross-over cable'').
35 * This is mostly intended to be used to provide connectivity between
36 * different virtual network stack instances.
39 * Things to re-think once we have more experience:
40 * - ifp->if_reassign function once we can test with vimage. Depending on
41 * how if_vmove() is going to be improved.
42 * - Real random etheraddrs that are checked to be uniquish; we would need
43 * to re-do them in case we move the interface between network stacks
44 * in a private if_reassign function.
45 * In case we bridge to a real interface/network or between indepedent
46 * epairs on multiple stacks/machines, we may need this.
47 * For now let the user handle that case.
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
57 #include <sys/module.h>
58 #include <sys/refcount.h>
59 #include <sys/queue.h>
61 #include <sys/socket.h>
62 #include <sys/sockio.h>
63 #include <sys/sysctl.h>
64 #include <sys/types.h>
67 #include <net/ethernet.h>
69 #include <net/if_var.h>
70 #include <net/if_clone.h>
71 #include <net/if_media.h>
72 #include <net/if_var.h>
73 #include <net/if_types.h>
74 #include <net/netisr.h>
77 SYSCTL_DECL(_net_link);
78 static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
81 static int epair_debug = 0;
82 SYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
83 &epair_debug, 0, "if_epair(4) debugging.");
84 #define DPRINTF(fmt, arg...) \
86 printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
88 #define DPRINTF(fmt, arg...)
91 static void epair_nh_sintr(struct mbuf *);
92 static struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
93 static void epair_nh_drainedcpu(u_int);
95 static void epair_start_locked(struct ifnet *);
96 static int epair_media_change(struct ifnet *);
97 static void epair_media_status(struct ifnet *, struct ifmediareq *);
99 static int epair_clone_match(struct if_clone *, const char *);
100 static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
101 static int epair_clone_destroy(struct if_clone *, struct ifnet *);
103 static const char epairname[] = "epair";
105 /* Netisr related definitions and sysctl. */
106 static struct netisr_handler epair_nh = {
107 .nh_name = epairname,
108 .nh_proto = NETISR_EPAIR,
109 .nh_policy = NETISR_POLICY_CPU,
110 .nh_handler = epair_nh_sintr,
111 .nh_m2cpuid = epair_nh_m2cpuid,
112 .nh_drainedcpu = epair_nh_drainedcpu,
116 sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
120 netisr_getqlimit(&epair_nh, &qlimit);
121 error = sysctl_handle_int(oidp, &qlimit, 0, req);
122 if (error || !req->newptr)
126 return (netisr_setqlimit(&epair_nh, qlimit));
128 SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
129 0, 0, sysctl_epair_netisr_maxqlen, "I",
130 "Maximum if_epair(4) netisr \"hw\" queue length");
133 struct ifnet *ifp; /* This ifp. */
134 struct ifnet *oifp; /* other ifp of pair. */
135 struct ifmedia media; /* Media config (fake). */
136 u_int refcount; /* # of mbufs in flight. */
137 u_int cpuid; /* CPU ID assigned upon creation. */
138 void (*if_qflush)(struct ifnet *);
139 /* Original if_qflush routine. */
143 * Per-CPU list of ifps with data in the ifq that needs to be flushed
144 * to the netisr ``hw'' queue before we allow any further direct queuing
145 * to the ``hw'' queue.
147 struct epair_ifp_drain {
148 STAILQ_ENTRY(epair_ifp_drain) ifp_next;
151 STAILQ_HEAD(eid_list, epair_ifp_drain);
153 #define EPAIR_LOCK_INIT(dpcpu) mtx_init(&(dpcpu)->if_epair_mtx, \
154 "if_epair", NULL, MTX_DEF)
155 #define EPAIR_LOCK_DESTROY(dpcpu) mtx_destroy(&(dpcpu)->if_epair_mtx)
156 #define EPAIR_LOCK_ASSERT(dpcpu) mtx_assert(&(dpcpu)->if_epair_mtx, \
158 #define EPAIR_LOCK(dpcpu) mtx_lock(&(dpcpu)->if_epair_mtx)
159 #define EPAIR_UNLOCK(dpcpu) mtx_unlock(&(dpcpu)->if_epair_mtx)
162 #define EPAIR_REFCOUNT_INIT(r, v) refcount_init((r), (v))
163 #define EPAIR_REFCOUNT_AQUIRE(r) refcount_acquire((r))
164 #define EPAIR_REFCOUNT_RELEASE(r) refcount_release((r))
165 #define EPAIR_REFCOUNT_ASSERT(a, p) KASSERT(a, p)
167 #define EPAIR_REFCOUNT_INIT(r, v)
168 #define EPAIR_REFCOUNT_AQUIRE(r)
169 #define EPAIR_REFCOUNT_RELEASE(r)
170 #define EPAIR_REFCOUNT_ASSERT(a, p)
173 static MALLOC_DEFINE(M_EPAIR, epairname,
174 "Pair of virtual cross-over connected Ethernet-like interfaces");
176 static VNET_DEFINE(struct if_clone *, epair_cloner);
177 #define V_epair_cloner VNET(epair_cloner)
180 * DPCPU area and functions.
183 struct mtx if_epair_mtx; /* Per-CPU locking. */
184 int epair_drv_flags; /* Per-CPU ``hw'' drv flags. */
185 struct eid_list epair_ifp_drain_list; /* Per-CPU list of ifps with
186 * data in the ifq. */
188 DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
191 epair_dpcpu_init(void)
193 struct epair_dpcpu *epair_dpcpu;
198 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
200 /* Initialize per-cpu lock. */
201 EPAIR_LOCK_INIT(epair_dpcpu);
203 /* Driver flags are per-cpu as are our netisr "hw" queues. */
204 epair_dpcpu->epair_drv_flags = 0;
207 * Initialize per-cpu drain list.
208 * Manually do what STAILQ_HEAD_INITIALIZER would do.
210 s = &epair_dpcpu->epair_ifp_drain_list;
211 s->stqh_first = NULL;
212 s->stqh_last = &s->stqh_first;
217 epair_dpcpu_detach(void)
219 struct epair_dpcpu *epair_dpcpu;
223 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
225 /* Destroy per-cpu lock. */
226 EPAIR_LOCK_DESTROY(epair_dpcpu);
234 cpuid_from_ifp(struct ifnet *ifp)
236 struct epair_softc *sc;
246 * Netisr handler functions.
249 epair_nh_sintr(struct mbuf *m)
252 struct epair_softc *sc;
254 ifp = m->m_pkthdr.rcvif;
255 (*ifp->if_input)(ifp, m);
257 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
258 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
259 ("%s: ifp=%p sc->refcount not >= 1: %d",
260 __func__, ifp, sc->refcount));
261 DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
265 epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
268 *cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
274 epair_nh_drainedcpu(u_int cpuid)
276 struct epair_dpcpu *epair_dpcpu;
277 struct epair_ifp_drain *elm, *tvar;
280 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
281 EPAIR_LOCK(epair_dpcpu);
283 * Assume our "hw" queue and possibly ifq will be emptied
284 * again. In case we will overflow the "hw" queue while
285 * draining, epair_start_locked will set IFF_DRV_OACTIVE
286 * again and we will stop and return.
288 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
291 epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
292 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
293 epair_start_locked(ifp);
295 IFQ_LOCK(&ifp->if_snd);
296 if (IFQ_IS_EMPTY(&ifp->if_snd)) {
297 struct epair_softc *sc;
299 STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
300 elm, epair_ifp_drain, ifp_next);
301 /* The cached ifp goes off the list. */
303 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
304 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
305 ("%s: ifp=%p sc->refcount not >= 1: %d",
306 __func__, ifp, sc->refcount));
309 IFQ_UNLOCK(&ifp->if_snd);
311 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
312 /* Our "hw"q overflew again. */
313 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
314 DPRINTF("hw queue length overflow at %u\n",
319 EPAIR_UNLOCK(epair_dpcpu);
323 * Network interface (`if') related functions.
326 epair_remove_ifp_from_draining(struct ifnet *ifp)
328 struct epair_dpcpu *epair_dpcpu;
329 struct epair_ifp_drain *elm, *tvar;
333 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
334 EPAIR_LOCK(epair_dpcpu);
335 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
337 if (ifp == elm->ifp) {
338 struct epair_softc *sc;
341 &epair_dpcpu->epair_ifp_drain_list, elm,
342 epair_ifp_drain, ifp_next);
343 /* The cached ifp goes off the list. */
345 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
346 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
347 ("%s: ifp=%p sc->refcount not >= 1: %d",
348 __func__, ifp, sc->refcount));
352 EPAIR_UNLOCK(epair_dpcpu);
357 epair_add_ifp_for_draining(struct ifnet *ifp)
359 struct epair_dpcpu *epair_dpcpu;
360 struct epair_softc *sc;
361 struct epair_ifp_drain *elm = NULL;
364 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
365 EPAIR_LOCK_ASSERT(epair_dpcpu);
366 STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
369 /* If the ifp is there already, return success. */
373 elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
378 /* Add a reference for the ifp pointer on the list. */
379 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
380 STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
386 epair_start_locked(struct ifnet *ifp)
388 struct epair_dpcpu *epair_dpcpu;
390 struct epair_softc *sc;
394 DPRINTF("ifp=%p\n", ifp);
396 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
397 EPAIR_LOCK_ASSERT(epair_dpcpu);
399 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
401 if ((ifp->if_flags & IFF_UP) == 0)
405 * We get patckets here from ether_output via if_handoff()
406 * and ned to put them into the input queue of the oifp
407 * and call oifp->if_input() via netisr/epair_sintr().
412 IFQ_DEQUEUE(&ifp->if_snd, m);
418 * In case the outgoing interface is not usable,
421 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
422 (oifp->if_flags & IFF_UP) ==0) {
423 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
427 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
430 * Add a reference so the interface cannot go while the
431 * packet is in transit as we rely on rcvif to stay valid.
433 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
434 m->m_pkthdr.rcvif = oifp;
435 CURVNET_SET_QUIET(oifp->if_vnet);
436 error = netisr_queue(NETISR_EPAIR, m);
439 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
440 /* Someone else received the packet. */
441 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
443 /* The packet was freed already. */
444 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
445 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
446 (void) epair_add_ifp_for_draining(ifp);
447 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
448 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
449 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
450 ("%s: ifp=%p sc->refcount not >= 1: %d",
451 __func__, oifp, sc->refcount));
457 epair_start(struct ifnet *ifp)
459 struct epair_dpcpu *epair_dpcpu;
461 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
462 EPAIR_LOCK(epair_dpcpu);
463 epair_start_locked(ifp);
464 EPAIR_UNLOCK(epair_dpcpu);
468 epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
470 struct epair_dpcpu *epair_dpcpu;
471 struct epair_softc *sc;
476 DPRINTF("ifp=%p m=%p\n", ifp, m);
478 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
479 EPAIR_LOCK_ASSERT(epair_dpcpu);
485 * We are not going to use the interface en/dequeue mechanism
486 * on the TX side. We are called from ether_output_frame()
487 * and will put the packet into the incoming queue of the
488 * other interface of our pair via the netsir.
490 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
494 if ((ifp->if_flags & IFF_UP) == 0) {
502 * In case the outgoing interface is not usable,
506 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
507 (oifp->if_flags & IFF_UP) ==0) {
508 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
512 len = m->m_pkthdr.len;
514 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
517 /* Support ALTQ via the classic if_start() path. */
518 IF_LOCK(&ifp->if_snd);
519 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
520 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
522 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
523 IF_UNLOCK(&ifp->if_snd);
525 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
526 if (mflags & (M_BCAST|M_MCAST))
527 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
529 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
530 epair_start_locked(ifp);
532 (void)epair_add_ifp_for_draining(ifp);
536 IF_UNLOCK(&ifp->if_snd);
539 if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
541 * Our hardware queue is full, try to fall back
542 * queuing to the ifq but do not call ifp->if_start.
543 * Either we are lucky or the packet is gone.
545 IFQ_ENQUEUE(&ifp->if_snd, m, error);
547 (void)epair_add_ifp_for_draining(ifp);
552 * Add a reference so the interface cannot go while the
553 * packet is in transit as we rely on rcvif to stay valid.
555 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
556 m->m_pkthdr.rcvif = oifp;
557 CURVNET_SET_QUIET(oifp->if_vnet);
558 error = netisr_queue(NETISR_EPAIR, m);
561 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
563 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
564 * but as we bypass all this we have to duplicate
565 * the logic another time.
567 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
568 if (mflags & (M_BCAST|M_MCAST))
569 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
570 /* Someone else received the packet. */
571 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
573 /* The packet was freed already. */
574 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
575 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
576 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
577 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
578 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
579 ("%s: ifp=%p sc->refcount not >= 1: %d",
580 __func__, oifp, sc->refcount));
587 epair_transmit(struct ifnet *ifp, struct mbuf *m)
589 struct epair_dpcpu *epair_dpcpu;
592 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
593 EPAIR_LOCK(epair_dpcpu);
594 error = epair_transmit_locked(ifp, m);
595 EPAIR_UNLOCK(epair_dpcpu);
600 epair_qflush(struct ifnet *ifp)
602 struct epair_softc *sc;
605 KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
608 * Remove this ifp from all backpointer lists. The interface will not
609 * usable for flushing anyway nor should it have anything to flush
612 epair_remove_ifp_from_draining(ifp);
619 epair_media_change(struct ifnet *ifp __unused)
627 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
630 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
631 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
635 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
637 struct epair_softc *sc;
641 ifr = (struct ifreq *)data;
652 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
656 /* We basically allow all kinds of MTUs. */
657 ifp->if_mtu = ifr->ifr_mtu;
662 /* Let the common ethernet handler process this. */
663 error = ether_ioctl(ifp, cmd, data);
671 epair_init(void *dummy __unused)
677 * Interface cloning functions.
678 * We use our private ones so that we can create/destroy our secondary
679 * device along with the primary one.
682 epair_clone_match(struct if_clone *ifc, const char *name)
686 DPRINTF("name='%s'\n", name);
689 * Our base name is epair.
690 * Our interfaces will be named epair<n>[ab].
691 * So accept anything of the following list:
694 * but not the epair<n>[ab] versions.
696 if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
699 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
700 if (*cp < '0' || *cp > '9')
708 epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
710 struct epair_softc *sca, *scb;
713 int error, unit, wildcard;
714 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
717 * We are abusing params to create our second interface.
718 * Actually we already created it and called if_clone_create()
719 * for it to do the official insertion procedure the moment we knew
720 * it cannot fail anymore. So just do attach it here.
723 scb = (struct epair_softc *)params;
725 /* Assign a hopefully unique, locally administered etheraddr. */
727 eaddr[3] = (ifp->if_index >> 8) & 0xff;
728 eaddr[4] = ifp->if_index & 0xff;
730 ether_ifattach(ifp, eaddr);
731 /* Correctly set the name for the cloner list. */
732 strlcpy(name, scb->ifp->if_xname, len);
736 /* Try to see if a special unit was requested. */
737 error = ifc_name2unit(name, &unit);
740 wildcard = (unit < 0);
742 error = ifc_alloc_unit(ifc, &unit);
747 * If no unit had been given, we need to adjust the ifName.
748 * Also make sure there is space for our extra [ab] suffix.
750 for (dp = name; *dp != '\0'; dp++);
752 error = snprintf(dp, len - (dp - name), "%d", unit);
753 if (error > len - (dp - name) - 1) {
754 /* ifName too long. */
755 ifc_free_unit(ifc, unit);
760 if (len - (dp - name) - 1 < 1) {
761 /* No space left for our [ab] suffix. */
762 ifc_free_unit(ifc, unit);
766 /* Must not change dp so we can replace 'a' by 'b' later. */
769 /* Check if 'a' and 'b' interfaces already exist. */
770 if (ifunit(name) != NULL)
773 if (ifunit(name) != NULL)
776 /* Allocate memory for both [ab] interfaces */
777 sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
778 EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
779 sca->ifp = if_alloc(IFT_ETHER);
780 if (sca->ifp == NULL) {
782 ifc_free_unit(ifc, unit);
786 scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
787 EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
788 scb->ifp = if_alloc(IFT_ETHER);
789 if (scb->ifp == NULL) {
793 ifc_free_unit(ifc, unit);
798 * Cross-reference the interfaces so we will be able to free both.
800 sca->oifp = scb->ifp;
801 scb->oifp = sca->ifp;
804 * Calculate the cpuid for netisr queueing based on the
805 * ifIndex of the interfaces. As long as we cannot configure
806 * this or use cpuset information easily we cannot guarantee
807 * cache locality but we can at least allow parallelism.
810 netisr_get_cpuid(sca->ifp->if_index);
812 netisr_get_cpuid(scb->ifp->if_index);
814 /* Initialise pseudo media types. */
815 ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
816 ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
817 ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
818 ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
819 ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
820 ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
822 /* Finish initialization of interface <n>a. */
825 strlcpy(ifp->if_xname, name, IFNAMSIZ);
826 ifp->if_dname = epairname;
827 ifp->if_dunit = unit;
828 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
829 ifp->if_capabilities = IFCAP_VLAN_MTU;
830 ifp->if_capenable = IFCAP_VLAN_MTU;
831 ifp->if_start = epair_start;
832 ifp->if_ioctl = epair_ioctl;
833 ifp->if_init = epair_init;
834 ifp->if_snd.ifq_maxlen = ifqmaxlen;
835 /* Assign a hopefully unique, locally administered etheraddr. */
837 eaddr[3] = (ifp->if_index >> 8) & 0xff;
838 eaddr[4] = ifp->if_index & 0xff;
840 ether_ifattach(ifp, eaddr);
841 sca->if_qflush = ifp->if_qflush;
842 ifp->if_qflush = epair_qflush;
843 ifp->if_transmit = epair_transmit;
844 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
846 /* Swap the name and finish initialization of interface <n>b. */
851 strlcpy(ifp->if_xname, name, IFNAMSIZ);
852 ifp->if_dname = epairname;
853 ifp->if_dunit = unit;
854 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
855 ifp->if_capabilities = IFCAP_VLAN_MTU;
856 ifp->if_capenable = IFCAP_VLAN_MTU;
857 ifp->if_start = epair_start;
858 ifp->if_ioctl = epair_ioctl;
859 ifp->if_init = epair_init;
860 ifp->if_snd.ifq_maxlen = ifqmaxlen;
861 /* We need to play some tricks here for the second interface. */
862 strlcpy(name, epairname, len);
863 error = if_clone_create(name, len, (caddr_t)scb);
865 panic("%s: if_clone_create() for our 2nd iface failed: %d",
867 scb->if_qflush = ifp->if_qflush;
868 ifp->if_qflush = epair_qflush;
869 ifp->if_transmit = epair_transmit;
870 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
873 * Restore name to <n>a as the ifp for this will go into the
874 * cloner list for the initial call.
876 strlcpy(name, sca->ifp->if_xname, len);
877 DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
879 /* Tell the world, that we are ready to rock. */
880 sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
881 scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
882 if_link_state_change(sca->ifp, LINK_STATE_UP);
883 if_link_state_change(scb->ifp, LINK_STATE_UP);
889 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
892 struct epair_softc *sca, *scb;
895 DPRINTF("ifp=%p\n", ifp);
898 * In case we called into if_clone_destroyif() ourselves
899 * again to remove the second interface, the softc will be
900 * NULL. In that case so not do anything but return success.
902 if (ifp->if_softc == NULL)
905 unit = ifp->if_dunit;
908 scb = oifp->if_softc;
910 DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
911 if_link_state_change(ifp, LINK_STATE_DOWN);
912 if_link_state_change(oifp, LINK_STATE_DOWN);
913 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
914 oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
917 * Get rid of our second half. As the other of the two
918 * interfaces may reside in a different vnet, we need to
919 * switch before freeing them.
921 CURVNET_SET_QUIET(oifp->if_vnet);
922 ether_ifdetach(oifp);
924 * Wait for all packets to be dispatched to if_input.
925 * The numbers can only go down as the interface is
926 * detached so there is no need to use atomics.
928 DPRINTF("scb refcnt=%u\n", scb->refcount);
929 EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
930 ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
931 oifp->if_softc = NULL;
932 error = if_clone_destroyif(ifc, oifp);
934 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
937 ifmedia_removeall(&scb->media);
943 * Wait for all packets to be dispatched to if_input.
945 DPRINTF("sca refcnt=%u\n", sca->refcount);
946 EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
947 ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
949 ifmedia_removeall(&sca->media);
951 ifc_free_unit(ifc, unit);
957 vnet_epair_init(const void *unused __unused)
960 V_epair_cloner = if_clone_advanced(epairname, 0,
961 epair_clone_match, epair_clone_create, epair_clone_destroy);
963 netisr_register_vnet(&epair_nh);
966 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
967 vnet_epair_init, NULL);
970 vnet_epair_uninit(const void *unused __unused)
974 netisr_unregister_vnet(&epair_nh);
976 if_clone_detach(V_epair_cloner);
978 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
979 vnet_epair_uninit, NULL);
982 epair_modevent(module_t mod, int type, void *data)
988 /* For now limit us to one global mutex and one inq. */
990 epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
991 if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
992 epair_nh.nh_qlimit = qlimit;
993 netisr_register(&epair_nh);
995 printf("%s initialized.\n", epairname);
998 netisr_unregister(&epair_nh);
999 epair_dpcpu_detach();
1001 printf("%s unloaded.\n", epairname);
1004 return (EOPNOTSUPP);
1009 static moduledata_t epair_mod = {
1015 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
1016 MODULE_VERSION(if_epair, 1);