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>
56 #include <sys/module.h>
57 #include <sys/refcount.h>
58 #include <sys/queue.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/sysctl.h>
63 #include <sys/types.h>
66 #include <net/ethernet.h>
68 #include <net/if_clone.h>
69 #include <net/if_media.h>
70 #include <net/if_var.h>
71 #include <net/if_types.h>
72 #include <net/netisr.h>
75 SYSCTL_DECL(_net_link);
76 static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW, 0, "epair sysctl");
79 static int epair_debug = 0;
80 SYSCTL_INT(_net_link_epair, OID_AUTO, epair_debug, CTLFLAG_RW,
81 &epair_debug, 0, "if_epair(4) debugging.");
82 #define DPRINTF(fmt, arg...) \
84 printf("[%s:%d] " fmt, __func__, __LINE__, ##arg)
86 #define DPRINTF(fmt, arg...)
89 static void epair_nh_sintr(struct mbuf *);
90 static struct mbuf *epair_nh_m2cpuid(struct mbuf *, uintptr_t, u_int *);
91 static void epair_nh_drainedcpu(u_int);
93 static void epair_start_locked(struct ifnet *);
94 static int epair_media_change(struct ifnet *);
95 static void epair_media_status(struct ifnet *, struct ifmediareq *);
97 static int epair_clone_match(struct if_clone *, const char *);
98 static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
99 static int epair_clone_destroy(struct if_clone *, struct ifnet *);
101 static const char epairname[] = "epair";
103 /* Netisr realted definitions and sysctl. */
104 static struct netisr_handler epair_nh = {
105 .nh_name = epairname,
106 .nh_proto = NETISR_EPAIR,
107 .nh_policy = NETISR_POLICY_CPU,
108 .nh_handler = epair_nh_sintr,
109 .nh_m2cpuid = epair_nh_m2cpuid,
110 .nh_drainedcpu = epair_nh_drainedcpu,
114 sysctl_epair_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
118 netisr_getqlimit(&epair_nh, &qlimit);
119 error = sysctl_handle_int(oidp, &qlimit, 0, req);
120 if (error || !req->newptr)
124 return (netisr_setqlimit(&epair_nh, qlimit));
126 SYSCTL_PROC(_net_link_epair, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
127 0, 0, sysctl_epair_netisr_maxqlen, "I",
128 "Maximum if_epair(4) netisr \"hw\" queue length");
131 struct ifnet *ifp; /* This ifp. */
132 struct ifnet *oifp; /* other ifp of pair. */
133 struct ifmedia media; /* Media config (fake). */
134 u_int refcount; /* # of mbufs in flight. */
135 u_int cpuid; /* CPU ID assigned upon creation. */
136 void (*if_qflush)(struct ifnet *);
137 /* Original if_qflush routine. */
141 * Per-CPU list of ifps with data in the ifq that needs to be flushed
142 * to the netisr ``hw'' queue before we allow any further direct queuing
143 * to the ``hw'' queue.
145 struct epair_ifp_drain {
146 STAILQ_ENTRY(epair_ifp_drain) ifp_next;
149 STAILQ_HEAD(eid_list, epair_ifp_drain);
151 #define EPAIR_LOCK_INIT(dpcpu) mtx_init(&(dpcpu)->if_epair_mtx, \
152 "if_epair", NULL, MTX_DEF)
153 #define EPAIR_LOCK_DESTROY(dpcpu) mtx_destroy(&(dpcpu)->if_epair_mtx)
154 #define EPAIR_LOCK_ASSERT(dpcpu) mtx_assert(&(dpcpu)->if_epair_mtx, \
156 #define EPAIR_LOCK(dpcpu) mtx_lock(&(dpcpu)->if_epair_mtx)
157 #define EPAIR_UNLOCK(dpcpu) mtx_unlock(&(dpcpu)->if_epair_mtx)
160 #define EPAIR_REFCOUNT_INIT(r, v) refcount_init((r), (v))
161 #define EPAIR_REFCOUNT_AQUIRE(r) refcount_acquire((r))
162 #define EPAIR_REFCOUNT_RELEASE(r) refcount_release((r))
163 #define EPAIR_REFCOUNT_ASSERT(a, p) KASSERT(a, p)
165 #define EPAIR_REFCOUNT_INIT(r, v)
166 #define EPAIR_REFCOUNT_AQUIRE(r)
167 #define EPAIR_REFCOUNT_RELEASE(r)
168 #define EPAIR_REFCOUNT_ASSERT(a, p)
171 static MALLOC_DEFINE(M_EPAIR, epairname,
172 "Pair of virtual cross-over connected Ethernet-like interfaces");
174 static struct if_clone *epair_cloner;
177 * DPCPU area and functions.
180 struct mtx if_epair_mtx; /* Per-CPU locking. */
181 int epair_drv_flags; /* Per-CPU ``hw'' drv flags. */
182 struct eid_list epair_ifp_drain_list; /* Per-CPU list of ifps with
183 * data in the ifq. */
185 DPCPU_DEFINE(struct epair_dpcpu, epair_dpcpu);
188 epair_dpcpu_init(void)
190 struct epair_dpcpu *epair_dpcpu;
195 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
197 /* Initialize per-cpu lock. */
198 EPAIR_LOCK_INIT(epair_dpcpu);
200 /* Driver flags are per-cpu as are our netisr "hw" queues. */
201 epair_dpcpu->epair_drv_flags = 0;
204 * Initialize per-cpu drain list.
205 * Manually do what STAILQ_HEAD_INITIALIZER would do.
207 s = &epair_dpcpu->epair_ifp_drain_list;
208 s->stqh_first = NULL;
209 s->stqh_last = &s->stqh_first;
214 epair_dpcpu_detach(void)
216 struct epair_dpcpu *epair_dpcpu;
220 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
222 /* Destroy per-cpu lock. */
223 EPAIR_LOCK_DESTROY(epair_dpcpu);
231 cpuid_from_ifp(struct ifnet *ifp)
233 struct epair_softc *sc;
243 * Netisr handler functions.
246 epair_nh_sintr(struct mbuf *m)
249 struct epair_softc *sc;
251 ifp = m->m_pkthdr.rcvif;
252 (*ifp->if_input)(ifp, m);
254 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
255 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
256 ("%s: ifp=%p sc->refcount not >= 1: %d",
257 __func__, ifp, sc->refcount));
258 DPRINTF("ifp=%p refcount=%u\n", ifp, sc->refcount);
262 epair_nh_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid)
265 *cpuid = cpuid_from_ifp(m->m_pkthdr.rcvif);
271 epair_nh_drainedcpu(u_int cpuid)
273 struct epair_dpcpu *epair_dpcpu;
274 struct epair_ifp_drain *elm, *tvar;
277 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
278 EPAIR_LOCK(epair_dpcpu);
280 * Assume our "hw" queue and possibly ifq will be emptied
281 * again. In case we will overflow the "hw" queue while
282 * draining, epair_start_locked will set IFF_DRV_OACTIVE
283 * again and we will stop and return.
285 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
288 epair_dpcpu->epair_drv_flags &= ~IFF_DRV_OACTIVE;
289 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
290 epair_start_locked(ifp);
292 IFQ_LOCK(&ifp->if_snd);
293 if (IFQ_IS_EMPTY(&ifp->if_snd)) {
294 struct epair_softc *sc;
296 STAILQ_REMOVE(&epair_dpcpu->epair_ifp_drain_list,
297 elm, epair_ifp_drain, ifp_next);
298 /* The cached ifp goes off the list. */
300 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
301 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
302 ("%s: ifp=%p sc->refcount not >= 1: %d",
303 __func__, ifp, sc->refcount));
306 IFQ_UNLOCK(&ifp->if_snd);
308 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
309 /* Our "hw"q overflew again. */
310 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
311 DPRINTF("hw queue length overflow at %u\n",
316 EPAIR_UNLOCK(epair_dpcpu);
320 * Network interface (`if') related functions.
323 epair_remove_ifp_from_draining(struct ifnet *ifp)
325 struct epair_dpcpu *epair_dpcpu;
326 struct epair_ifp_drain *elm, *tvar;
330 epair_dpcpu = DPCPU_ID_PTR(cpuid, epair_dpcpu);
331 EPAIR_LOCK(epair_dpcpu);
332 STAILQ_FOREACH_SAFE(elm, &epair_dpcpu->epair_ifp_drain_list,
334 if (ifp == elm->ifp) {
335 struct epair_softc *sc;
338 &epair_dpcpu->epair_ifp_drain_list, elm,
339 epair_ifp_drain, ifp_next);
340 /* The cached ifp goes off the list. */
342 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
343 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
344 ("%s: ifp=%p sc->refcount not >= 1: %d",
345 __func__, ifp, sc->refcount));
349 EPAIR_UNLOCK(epair_dpcpu);
354 epair_add_ifp_for_draining(struct ifnet *ifp)
356 struct epair_dpcpu *epair_dpcpu;
357 struct epair_softc *sc;
358 struct epair_ifp_drain *elm = NULL;
361 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
362 EPAIR_LOCK_ASSERT(epair_dpcpu);
363 STAILQ_FOREACH(elm, &epair_dpcpu->epair_ifp_drain_list, ifp_next)
366 /* If the ifp is there already, return success. */
370 elm = malloc(sizeof(struct epair_ifp_drain), M_EPAIR, M_NOWAIT|M_ZERO);
375 /* Add a reference for the ifp pointer on the list. */
376 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
377 STAILQ_INSERT_TAIL(&epair_dpcpu->epair_ifp_drain_list, elm, ifp_next);
383 epair_start_locked(struct ifnet *ifp)
385 struct epair_dpcpu *epair_dpcpu;
387 struct epair_softc *sc;
391 DPRINTF("ifp=%p\n", ifp);
393 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
394 EPAIR_LOCK_ASSERT(epair_dpcpu);
396 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
398 if ((ifp->if_flags & IFF_UP) == 0)
402 * We get patckets here from ether_output via if_handoff()
403 * and ned to put them into the input queue of the oifp
404 * and call oifp->if_input() via netisr/epair_sintr().
409 IFQ_DEQUEUE(&ifp->if_snd, m);
415 * In case the outgoing interface is not usable,
418 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
419 (oifp->if_flags & IFF_UP) ==0) {
424 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
427 * Add a reference so the interface cannot go while the
428 * packet is in transit as we rely on rcvif to stay valid.
430 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
431 m->m_pkthdr.rcvif = oifp;
432 CURVNET_SET_QUIET(oifp->if_vnet);
433 error = netisr_queue(NETISR_EPAIR, m);
437 /* Someone else received the packet. */
440 /* The packet was freed already. */
441 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
442 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
443 (void) epair_add_ifp_for_draining(ifp);
445 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
446 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
447 ("%s: ifp=%p sc->refcount not >= 1: %d",
448 __func__, oifp, sc->refcount));
454 epair_start(struct ifnet *ifp)
456 struct epair_dpcpu *epair_dpcpu;
458 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
459 EPAIR_LOCK(epair_dpcpu);
460 epair_start_locked(ifp);
461 EPAIR_UNLOCK(epair_dpcpu);
465 epair_transmit_locked(struct ifnet *ifp, struct mbuf *m)
467 struct epair_dpcpu *epair_dpcpu;
468 struct epair_softc *sc;
473 DPRINTF("ifp=%p m=%p\n", ifp, m);
475 epair_dpcpu = DPCPU_ID_PTR(sc->cpuid, epair_dpcpu);
476 EPAIR_LOCK_ASSERT(epair_dpcpu);
482 * We are not going to use the interface en/dequeue mechanism
483 * on the TX side. We are called from ether_output_frame()
484 * and will put the packet into the incoming queue of the
485 * other interface of our pair via the netsir.
487 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
491 if ((ifp->if_flags & IFF_UP) == 0) {
499 * In case the outgoing interface is not usable,
503 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
504 (oifp->if_flags & IFF_UP) ==0) {
509 len = m->m_pkthdr.len;
511 DPRINTF("packet %s -> %s\n", ifp->if_xname, oifp->if_xname);
514 /* Support ALTQ via the clasic if_start() path. */
515 IF_LOCK(&ifp->if_snd);
516 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
517 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
519 ifp->if_snd.ifq_drops++;
520 IF_UNLOCK(&ifp->if_snd);
522 ifp->if_obytes += len;
523 if (mflags & (M_BCAST|M_MCAST))
526 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
527 epair_start_locked(ifp);
529 (void)epair_add_ifp_for_draining(ifp);
533 IF_UNLOCK(&ifp->if_snd);
536 if ((epair_dpcpu->epair_drv_flags & IFF_DRV_OACTIVE) != 0) {
538 * Our hardware queue is full, try to fall back
539 * queuing to the ifq but do not call ifp->if_start.
540 * Either we are lucky or the packet is gone.
542 IFQ_ENQUEUE(&ifp->if_snd, m, error);
544 (void)epair_add_ifp_for_draining(ifp);
549 * Add a reference so the interface cannot go while the
550 * packet is in transit as we rely on rcvif to stay valid.
552 EPAIR_REFCOUNT_AQUIRE(&sc->refcount);
553 m->m_pkthdr.rcvif = oifp;
554 CURVNET_SET_QUIET(oifp->if_vnet);
555 error = netisr_queue(NETISR_EPAIR, m);
560 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
561 * but as we bypass all this we have to duplicate
562 * the logic another time.
564 ifp->if_obytes += len;
565 if (mflags & (M_BCAST|M_MCAST))
567 /* Someone else received the packet. */
570 /* The packet was freed already. */
571 epair_dpcpu->epair_drv_flags |= IFF_DRV_OACTIVE;
572 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
574 EPAIR_REFCOUNT_RELEASE(&sc->refcount);
575 EPAIR_REFCOUNT_ASSERT((int)sc->refcount >= 1,
576 ("%s: ifp=%p sc->refcount not >= 1: %d",
577 __func__, oifp, sc->refcount));
584 epair_transmit(struct ifnet *ifp, struct mbuf *m)
586 struct epair_dpcpu *epair_dpcpu;
589 epair_dpcpu = DPCPU_ID_PTR(cpuid_from_ifp(ifp), epair_dpcpu);
590 EPAIR_LOCK(epair_dpcpu);
591 error = epair_transmit_locked(ifp, m);
592 EPAIR_UNLOCK(epair_dpcpu);
597 epair_qflush(struct ifnet *ifp)
599 struct epair_softc *sc;
602 KASSERT(sc != NULL, ("%s: ifp=%p, epair_softc gone? sc=%p\n",
605 * Remove this ifp from all backpointer lists. The interface will not
606 * usable for flushing anyway nor should it have anything to flush
609 epair_remove_ifp_from_draining(ifp);
616 epair_media_change(struct ifnet *ifp __unused)
624 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
627 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
628 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
632 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
634 struct epair_softc *sc;
638 ifr = (struct ifreq *)data;
649 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
653 /* We basically allow all kinds of MTUs. */
654 ifp->if_mtu = ifr->ifr_mtu;
659 /* Let the common ethernet handler process this. */
660 error = ether_ioctl(ifp, cmd, data);
668 epair_init(void *dummy __unused)
674 * Interface cloning functions.
675 * We use our private ones so that we can create/destroy our secondary
676 * device along with the primary one.
679 epair_clone_match(struct if_clone *ifc, const char *name)
683 DPRINTF("name='%s'\n", name);
686 * Our base name is epair.
687 * Our interfaces will be named epair<n>[ab].
688 * So accept anything of the following list:
691 * but not the epair<n>[ab] versions.
693 if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
696 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
697 if (*cp < '0' || *cp > '9')
705 epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
707 struct epair_softc *sca, *scb;
710 int error, unit, wildcard;
711 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
714 * We are abusing params to create our second interface.
715 * Actually we already created it and called if_clone_create()
716 * for it to do the official insertion procedure the moment we knew
717 * it cannot fail anymore. So just do attach it here.
720 scb = (struct epair_softc *)params;
722 /* Assign a hopefully unique, locally administered etheraddr. */
724 eaddr[3] = (ifp->if_index >> 8) & 0xff;
725 eaddr[4] = ifp->if_index & 0xff;
727 ether_ifattach(ifp, eaddr);
728 /* Correctly set the name for the cloner list. */
729 strlcpy(name, scb->ifp->if_xname, len);
733 /* Try to see if a special unit was requested. */
734 error = ifc_name2unit(name, &unit);
737 wildcard = (unit < 0);
739 error = ifc_alloc_unit(ifc, &unit);
744 * If no unit had been given, we need to adjust the ifName.
745 * Also make sure there is space for our extra [ab] suffix.
747 for (dp = name; *dp != '\0'; dp++);
749 error = snprintf(dp, len - (dp - name), "%d", unit);
750 if (error > len - (dp - name) - 1) {
751 /* ifName too long. */
752 ifc_free_unit(ifc, unit);
757 if (len - (dp - name) - 1 < 1) {
758 /* No space left for our [ab] suffix. */
759 ifc_free_unit(ifc, unit);
763 /* Must not change dp so we can replace 'a' by 'b' later. */
766 /* Allocate memory for both [ab] interfaces */
767 sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
768 EPAIR_REFCOUNT_INIT(&sca->refcount, 1);
769 sca->ifp = if_alloc(IFT_ETHER);
770 if (sca->ifp == NULL) {
772 ifc_free_unit(ifc, unit);
776 scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
777 EPAIR_REFCOUNT_INIT(&scb->refcount, 1);
778 scb->ifp = if_alloc(IFT_ETHER);
779 if (scb->ifp == NULL) {
783 ifc_free_unit(ifc, unit);
788 * Cross-reference the interfaces so we will be able to free both.
790 sca->oifp = scb->ifp;
791 scb->oifp = sca->ifp;
794 * Calculate the cpuid for netisr queueing based on the
795 * ifIndex of the interfaces. As long as we cannot configure
796 * this or use cpuset information easily we cannot guarantee
797 * cache locality but we can at least allow parallelism.
800 netisr_get_cpuid(sca->ifp->if_index % netisr_get_cpucount());
802 netisr_get_cpuid(scb->ifp->if_index % netisr_get_cpucount());
804 /* Finish initialization of interface <n>a. */
807 strlcpy(ifp->if_xname, name, IFNAMSIZ);
808 ifp->if_dname = epairname;
809 ifp->if_dunit = unit;
810 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
811 ifp->if_capabilities = IFCAP_VLAN_MTU;
812 ifp->if_capenable = IFCAP_VLAN_MTU;
813 ifp->if_start = epair_start;
814 ifp->if_ioctl = epair_ioctl;
815 ifp->if_init = epair_init;
816 ifp->if_snd.ifq_maxlen = ifqmaxlen;
817 /* Assign a hopefully unique, locally administered etheraddr. */
819 eaddr[3] = (ifp->if_index >> 8) & 0xff;
820 eaddr[4] = ifp->if_index & 0xff;
822 ether_ifattach(ifp, eaddr);
823 sca->if_qflush = ifp->if_qflush;
824 ifp->if_qflush = epair_qflush;
825 ifp->if_transmit = epair_transmit;
826 if_initbaudrate(ifp, IF_Gbps(10)); /* arbitrary maximum */
828 /* Swap the name and finish initialization of interface <n>b. */
833 strlcpy(ifp->if_xname, name, IFNAMSIZ);
834 ifp->if_dname = epairname;
835 ifp->if_dunit = unit;
836 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
837 ifp->if_capabilities = IFCAP_VLAN_MTU;
838 ifp->if_capenable = IFCAP_VLAN_MTU;
839 ifp->if_start = epair_start;
840 ifp->if_ioctl = epair_ioctl;
841 ifp->if_init = epair_init;
842 ifp->if_snd.ifq_maxlen = ifqmaxlen;
843 /* We need to play some tricks here for the second interface. */
844 strlcpy(name, epairname, len);
845 error = if_clone_create(name, len, (caddr_t)scb);
847 panic("%s: if_clone_create() for our 2nd iface failed: %d",
849 scb->if_qflush = ifp->if_qflush;
850 ifp->if_qflush = epair_qflush;
851 ifp->if_transmit = epair_transmit;
852 if_initbaudrate(ifp, IF_Gbps(10)); /* arbitrary maximum */
855 * Restore name to <n>a as the ifp for this will go into the
856 * cloner list for the initial call.
858 strlcpy(name, sca->ifp->if_xname, len);
859 DPRINTF("name='%s/%db' created sca=%p scb=%p\n", name, unit, sca, scb);
861 /* Initialise pseudo media types. */
862 ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
863 ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
864 ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
865 ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
866 ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
867 ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
869 /* Tell the world, that we are ready to rock. */
870 sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
871 scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
872 if_link_state_change(sca->ifp, LINK_STATE_UP);
873 if_link_state_change(scb->ifp, LINK_STATE_UP);
879 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
882 struct epair_softc *sca, *scb;
885 DPRINTF("ifp=%p\n", ifp);
888 * In case we called into if_clone_destroyif() ourselves
889 * again to remove the second interface, the softc will be
890 * NULL. In that case so not do anything but return success.
892 if (ifp->if_softc == NULL)
895 unit = ifp->if_dunit;
898 scb = oifp->if_softc;
900 DPRINTF("ifp=%p oifp=%p\n", ifp, oifp);
901 if_link_state_change(ifp, LINK_STATE_DOWN);
902 if_link_state_change(oifp, LINK_STATE_DOWN);
903 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
904 oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
907 * Get rid of our second half. As the other of the two
908 * interfaces may reside in a different vnet, we need to
909 * switch before freeing them.
911 CURVNET_SET_QUIET(oifp->if_vnet);
912 ether_ifdetach(oifp);
914 * Wait for all packets to be dispatched to if_input.
915 * The numbers can only go down as the interface is
916 * detached so there is no need to use atomics.
918 DPRINTF("scb refcnt=%u\n", scb->refcount);
919 EPAIR_REFCOUNT_ASSERT(scb->refcount == 1,
920 ("%s: ifp=%p scb->refcount!=1: %d", __func__, oifp, scb->refcount));
921 oifp->if_softc = NULL;
922 error = if_clone_destroyif(ifc, oifp);
924 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
927 ifmedia_removeall(&scb->media);
933 * Wait for all packets to be dispatched to if_input.
935 DPRINTF("sca refcnt=%u\n", sca->refcount);
936 EPAIR_REFCOUNT_ASSERT(sca->refcount == 1,
937 ("%s: ifp=%p sca->refcount!=1: %d", __func__, ifp, sca->refcount));
939 ifmedia_removeall(&sca->media);
941 ifc_free_unit(ifc, unit);
947 epair_modevent(module_t mod, int type, void *data)
953 /* For now limit us to one global mutex and one inq. */
955 epair_nh.nh_qlimit = 42 * ifqmaxlen; /* 42 shall be the number. */
956 if (TUNABLE_INT_FETCH("net.link.epair.netisr_maxqlen", &qlimit))
957 epair_nh.nh_qlimit = qlimit;
958 netisr_register(&epair_nh);
959 epair_cloner = if_clone_advanced(epairname, 0,
960 epair_clone_match, epair_clone_create, epair_clone_destroy);
962 printf("%s initialized.\n", epairname);
965 if_clone_detach(epair_cloner);
966 netisr_unregister(&epair_nh);
967 epair_dpcpu_detach();
969 printf("%s unloaded.\n", epairname);
977 static moduledata_t epair_mod = {
983 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
984 MODULE_VERSION(if_epair, 1);