2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 The FreeBSD Foundation
5 * Copyright (c) 2009-2021 Bjoern A. Zeeb <bz@FreeBSD.org>
7 * This software was developed by CK Software GmbH under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * A pair of virtual back-to-back connected ethernet like interfaces
34 * (``two interfaces with a virtual cross-over cable'').
36 * This is mostly intended to be used to provide connectivity between
37 * different virtual network stack instances.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/libkern.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
52 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/types.h>
58 #include <sys/buf_ring.h>
60 #include <sys/interrupt.h>
63 #include <net/ethernet.h>
65 #include <net/if_var.h>
66 #include <net/if_clone.h>
67 #include <net/if_media.h>
68 #include <net/if_var.h>
69 #include <net/if_types.h>
70 #include <net/netisr.h>
73 static int epair_clone_match(struct if_clone *, const char *);
74 static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
75 static int epair_clone_destroy(struct if_clone *, struct ifnet *);
77 static const char epairname[] = "epair";
78 #define RXRSIZE 4096 /* Probably overkill by 4-8x. */
80 static MALLOC_DEFINE(M_EPAIR, epairname,
81 "Pair of virtual cross-over connected Ethernet-like interfaces");
83 VNET_DEFINE_STATIC(struct if_clone *, epair_cloner);
84 #define V_epair_cloner VNET(epair_cloner)
86 static unsigned int next_index = 0;
87 #define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \
89 #define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx)
90 #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx)
91 #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx)
93 static void *swi_cookie[MAXCPU]; /* swi(9). */
94 static STAILQ_HEAD(, epair_softc) swi_sc[MAXCPU];
96 static struct mtx epair_n_index_mtx;
98 struct ifnet *ifp; /* This ifp. */
99 struct ifnet *oifp; /* other ifp of pair. */
100 void *swi_cookie; /* swi(9). */
101 struct buf_ring *rxring[2];
102 volatile int ridx; /* 0 || 1 */
103 struct ifmedia media; /* Media config (fake). */
105 STAILQ_ENTRY(epair_softc) entry;
109 epair_clear_mbuf(struct mbuf *m)
111 /* Remove any CSUM_SND_TAG as ether_input will barf. */
112 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
113 m_snd_tag_rele(m->m_pkthdr.snd_tag);
114 m->m_pkthdr.snd_tag = NULL;
115 m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
118 m_tag_delete_nonpersistent(m);
122 epair_if_input(struct epair_softc *sc, int ridx)
124 struct epoch_tracker et;
131 m = buf_ring_dequeue_sc(sc->rxring[ridx]);
135 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
136 (*ifp->if_input)(ifp, m);
143 epair_sintr(struct epair_softc *sc)
150 nidx = (ridx == 0) ? 1 : 0;
151 } while (!atomic_cmpset_int(&sc->ridx, ridx, nidx));
152 epair_if_input(sc, ridx);
158 epair_intr(void *arg)
160 struct epair_softc *sc;
163 cpuidx = (uintptr_t)arg;
164 /* If this is a problem, this is a read-mostly situation. */
166 STAILQ_FOREACH(sc, &swi_sc[cpuidx], entry) {
167 /* Do this lockless. */
168 if (buf_ring_empty(sc->rxring[sc->ridx]))
178 epair_menq(struct mbuf *m, struct epair_softc *osc)
180 struct ifnet *ifp, *oifp;
187 * I know this looks weird. We pass the "other sc" as we need that one
188 * and can get both ifps from it as well.
195 if_setrcvif(m, oifp);
196 M_SETFIB(m, oifp->if_fib);
198 /* Save values as once the mbuf is queued, it's not ours anymore. */
199 len = m->m_pkthdr.len;
202 MPASS(m->m_nextpkt == NULL);
203 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
205 ridx = atomic_load_int(&osc->ridx);
206 was_empty = buf_ring_empty(osc->rxring[ridx]);
207 ret = buf_ring_enqueue(osc->rxring[ridx], m);
214 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
216 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
217 * but as we bypass all this we have to duplicate
218 * the logic another time.
220 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
221 if (mflags & (M_BCAST|M_MCAST))
222 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
223 /* Someone else received the packet. */
224 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
226 /* Kick the interrupt handler for the first packet. */
227 if (was_empty && osc->swi_cookie != NULL)
228 swi_sched(osc->swi_cookie, 0);
234 epair_start(struct ifnet *ifp)
237 struct epair_softc *sc;
241 * We get packets here from ether_output via if_handoff()
242 * and need to put them into the input queue of the oifp
243 * and will put the packet into the receive-queue (rxq) of the
244 * other interface (oifp) of our pair.
250 IFQ_DEQUEUE(&ifp->if_snd, m);
256 /* In case either interface is not usable drop the packet. */
257 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
258 (ifp->if_flags & IFF_UP) == 0 ||
259 (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
260 (oifp->if_flags & IFF_UP) == 0) {
265 (void) epair_menq(m, sc);
270 epair_transmit(struct ifnet *ifp, struct mbuf *m)
272 struct epair_softc *sc;
285 * We are not going to use the interface en/dequeue mechanism
286 * on the TX side. We are called from ether_output_frame()
287 * and will put the packet into the receive-queue (rxq) of the
288 * other interface (oifp) of our pair.
290 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
292 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
295 if ((ifp->if_flags & IFF_UP) == 0) {
297 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
304 * In case the outgoing interface is not usable,
309 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
310 (oifp->if_flags & IFF_UP) == 0) {
311 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
317 len = m->m_pkthdr.len;
320 /* Support ALTQ via the classic if_start() path. */
321 IF_LOCK(&ifp->if_snd);
322 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
323 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
325 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
326 IF_UNLOCK(&ifp->if_snd);
328 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
329 if (mflags & (M_BCAST|M_MCAST))
330 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
335 IF_UNLOCK(&ifp->if_snd);
338 error = epair_menq(m, oifp->if_softc);
343 epair_media_change(struct ifnet *ifp __unused)
351 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
354 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
355 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
359 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
361 struct epair_softc *sc;
365 ifr = (struct ifreq *)data;
376 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
380 /* We basically allow all kinds of MTUs. */
381 ifp->if_mtu = ifr->ifr_mtu;
386 /* Let the common ethernet handler process this. */
387 error = ether_ioctl(ifp, cmd, data);
395 epair_init(void *dummy __unused)
400 * Interface cloning functions.
401 * We use our private ones so that we can create/destroy our secondary
402 * device along with the primary one.
405 epair_clone_match(struct if_clone *ifc, const char *name)
410 * Our base name is epair.
411 * Our interfaces will be named epair<n>[ab].
412 * So accept anything of the following list:
415 * but not the epair<n>[ab] versions.
417 if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
420 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
421 if (*cp < '0' || *cp > '9')
429 epair_clone_add(struct if_clone *ifc, struct epair_softc *scb)
432 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
435 /* Copy epairNa etheraddr and change the last byte. */
436 memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
438 ether_ifattach(ifp, eaddr);
440 if_clone_addif(ifc, ifp);
444 epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
446 struct epair_softc *sca, *scb;
449 int error, unit, wildcard;
453 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
455 /* Try to see if a special unit was requested. */
456 error = ifc_name2unit(name, &unit);
459 wildcard = (unit < 0);
461 error = ifc_alloc_unit(ifc, &unit);
466 * If no unit had been given, we need to adjust the ifName.
467 * Also make sure there is space for our extra [ab] suffix.
469 for (dp = name; *dp != '\0'; dp++);
471 error = snprintf(dp, len - (dp - name), "%d", unit);
472 if (error > len - (dp - name) - 1) {
473 /* ifName too long. */
474 ifc_free_unit(ifc, unit);
479 if (len - (dp - name) - 1 < 1) {
480 /* No space left for our [ab] suffix. */
481 ifc_free_unit(ifc, unit);
485 /* Must not change dp so we can replace 'a' by 'b' later. */
488 /* Check if 'a' and 'b' interfaces already exist. */
489 if (ifunit(name) != NULL)
492 if (ifunit(name) != NULL)
495 /* Allocate memory for both [ab] interfaces */
496 sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
497 sca->ifp = if_alloc(IFT_ETHER);
498 if (sca->ifp == NULL) {
500 ifc_free_unit(ifc, unit);
503 sca->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK,NULL);
504 sca->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
506 scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
507 scb->ifp = if_alloc(IFT_ETHER);
508 if (scb->ifp == NULL) {
512 ifc_free_unit(ifc, unit);
515 scb->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
516 scb->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
519 * Cross-reference the interfaces so we will be able to free both.
521 sca->oifp = scb->ifp;
522 scb->oifp = sca->ifp;
526 /* Get an approximate distribution. */
527 hash = next_index % mp_ncpus;
531 if (swi_cookie[hash] == NULL) {
535 error = swi_add(NULL, epairname,
536 epair_intr, (void *)(uintptr_t)hash,
537 SWI_NET, INTR_MPSAFE, &cookie);
539 buf_ring_free(scb->rxring[0], M_EPAIR);
540 buf_ring_free(scb->rxring[1], M_EPAIR);
543 buf_ring_free(sca->rxring[0], M_EPAIR);
544 buf_ring_free(sca->rxring[1], M_EPAIR);
547 ifc_free_unit(ifc, unit);
551 /* Recheck under lock even though a race is very unlikely. */
552 if (swi_cookie[hash] == NULL) {
553 swi_cookie[hash] = cookie;
556 (void) swi_remove(cookie);
561 STAILQ_INSERT_TAIL(&swi_sc[hash], sca, entry);
562 sca->swi_cookie = swi_cookie[hash];
564 STAILQ_INSERT_TAIL(&swi_sc[hash], scb, entry);
565 scb->swi_cookie = swi_cookie[hash];
568 /* Initialise pseudo media types. */
569 ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
570 ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
571 ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
572 ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
573 ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
574 ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
576 /* Finish initialization of interface <n>a. */
579 strlcpy(ifp->if_xname, name, IFNAMSIZ);
580 ifp->if_dname = epairname;
581 ifp->if_dunit = unit;
582 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
583 ifp->if_flags |= IFF_KNOWSEPOCH;
584 ifp->if_capabilities = IFCAP_VLAN_MTU;
585 ifp->if_capenable = IFCAP_VLAN_MTU;
586 ifp->if_start = epair_start;
587 ifp->if_ioctl = epair_ioctl;
588 ifp->if_init = epair_init;
589 if_setsendqlen(ifp, ifqmaxlen);
590 if_setsendqready(ifp);
593 * Calculate the etheraddr hashing the hostid and the
594 * interface index. The result would be hopefully unique.
595 * Note that the "a" component of an epair instance may get moved
596 * to a different VNET after creation. In that case its index
597 * will be freed and the index can get reused by new epair instance.
598 * Make sure we do not create same etheraddr again.
600 getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
602 arc4rand(&hostid, sizeof(hostid), 0);
605 if (ifp->if_index > next_index)
606 next_index = ifp->if_index;
610 key[0] = (uint32_t)next_index;
612 key[1] = (uint32_t)(hostid & 0xffffffff);
613 key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
614 hash = jenkins_hash32(key, 3, 0);
617 memcpy(&eaddr[1], &hash, 4);
619 ether_ifattach(ifp, eaddr);
620 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
621 ifp->if_transmit = epair_transmit;
623 /* Swap the name and finish initialization of interface <n>b. */
628 strlcpy(ifp->if_xname, name, IFNAMSIZ);
629 ifp->if_dname = epairname;
630 ifp->if_dunit = unit;
631 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
632 ifp->if_flags |= IFF_KNOWSEPOCH;
633 ifp->if_capabilities = IFCAP_VLAN_MTU;
634 ifp->if_capenable = IFCAP_VLAN_MTU;
635 ifp->if_start = epair_start;
636 ifp->if_ioctl = epair_ioctl;
637 ifp->if_init = epair_init;
638 if_setsendqlen(ifp, ifqmaxlen);
639 if_setsendqready(ifp);
640 /* We need to play some tricks here for the second interface. */
641 strlcpy(name, epairname, len);
643 /* Correctly set the name for the cloner list. */
644 strlcpy(name, scb->ifp->if_xname, len);
645 epair_clone_add(ifc, scb);
647 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
648 ifp->if_transmit = epair_transmit;
651 * Restore name to <n>a as the ifp for this will go into the
652 * cloner list for the initial call.
654 strlcpy(name, sca->ifp->if_xname, len);
656 /* Tell the world, that we are ready to rock. */
657 sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
658 if_link_state_change(sca->ifp, LINK_STATE_UP);
659 scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
660 if_link_state_change(scb->ifp, LINK_STATE_UP);
666 epair_drain_rings(struct epair_softc *sc)
671 for (ridx = 0; ridx < 2; ridx++) {
673 m = buf_ring_dequeue_sc(sc->rxring[ridx]);
682 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
685 struct epair_softc *sca, *scb;
689 * In case we called into if_clone_destroyif() ourselves
690 * again to remove the second interface, the softc will be
691 * NULL. In that case so not do anything but return success.
693 if (ifp->if_softc == NULL)
696 unit = ifp->if_dunit;
699 scb = oifp->if_softc;
701 /* Frist get the interfaces down and detached. */
702 if_link_state_change(ifp, LINK_STATE_DOWN);
703 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
704 if_link_state_change(oifp, LINK_STATE_DOWN);
705 oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
708 ether_ifdetach(oifp);
710 /* Second stop interrupt handler. */
712 STAILQ_REMOVE(&swi_sc[sca->cpuidx], sca, epair_softc, entry);
713 STAILQ_REMOVE(&swi_sc[scb->cpuidx], scb, epair_softc, entry);
715 sca->swi_cookie = NULL;
716 scb->swi_cookie = NULL;
718 /* Third free any queued packets and all the resources. */
719 CURVNET_SET_QUIET(oifp->if_vnet);
720 epair_drain_rings(scb);
721 oifp->if_softc = NULL;
722 error = if_clone_destroyif(ifc, oifp);
724 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
727 ifmedia_removeall(&scb->media);
728 buf_ring_free(scb->rxring[0], M_EPAIR);
729 buf_ring_free(scb->rxring[1], M_EPAIR);
733 epair_drain_rings(sca);
735 ifmedia_removeall(&sca->media);
736 buf_ring_free(sca->rxring[0], M_EPAIR);
737 buf_ring_free(sca->rxring[1], M_EPAIR);
740 /* Last free the cloner unit. */
741 ifc_free_unit(ifc, unit);
747 vnet_epair_init(const void *unused __unused)
750 V_epair_cloner = if_clone_advanced(epairname, 0,
751 epair_clone_match, epair_clone_create, epair_clone_destroy);
753 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
754 vnet_epair_init, NULL);
757 vnet_epair_uninit(const void *unused __unused)
760 if_clone_detach(V_epair_cloner);
762 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
763 vnet_epair_uninit, NULL);
766 epair_modevent(module_t mod, int type, void *data)
772 for (i = 0; i < MAXCPU; i++) {
773 swi_cookie[i] = NULL;
774 STAILQ_INIT(&swi_sc[i]);
778 printf("%s: %s initialized.\n", __func__, epairname);
782 for (i = 0; i < MAXCPU; i++) {
783 if (!STAILQ_EMPTY(&swi_sc[i])) {
784 printf("%s: swi_sc[%d] active\n", __func__, i);
790 for (i = 0; i < MAXCPU; i++)
791 if (swi_cookie[i] != NULL)
792 (void) swi_remove(swi_cookie[i]);
793 EPAIR_LOCK_DESTROY();
795 printf("%s: %s unloaded.\n", __func__, epairname);
803 static moduledata_t epair_mod = {
809 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
810 MODULE_VERSION(if_epair, 3);