2 * SPDX-License-Identifier: BSD-2-Clause
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>
43 #include "opt_inet6.h"
45 #include <sys/param.h>
48 #include <sys/interrupt.h>
50 #include <sys/kernel.h>
51 #include <sys/libkern.h>
52 #include <sys/malloc.h>
54 #include <sys/module.h>
56 #include <sys/queue.h>
57 #include <sys/sched.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/taskqueue.h>
64 #include <net/ethernet.h>
66 #include <net/if_var.h>
67 #include <net/if_clone.h>
68 #include <net/if_media.h>
69 #include <net/if_var.h>
70 #include <net/if_private.h>
71 #include <net/if_types.h>
72 #include <net/netisr.h>
74 #include <net/rss_config.h>
76 #include <netinet/in_rss.h>
79 #include <netinet6/in6_rss.h>
84 static const char epairname[] = "epair";
85 #define RXRSIZE 4096 /* Probably overkill by 4-8x. */
87 static MALLOC_DEFINE(M_EPAIR, epairname,
88 "Pair of virtual cross-over connected Ethernet-like interfaces");
90 VNET_DEFINE_STATIC(struct if_clone *, epair_cloner);
91 #define V_epair_cloner VNET(epair_cloner)
93 static unsigned int next_index = 0;
94 #define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \
96 #define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx)
97 #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx)
98 #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx)
111 struct epair_softc *sc;
114 static struct mtx epair_n_index_mtx;
116 struct ifnet *ifp; /* This ifp. */
117 struct ifnet *oifp; /* other ifp of pair. */
119 struct epair_queue *queues;
120 struct ifmedia media; /* Media config (fake). */
121 STAILQ_ENTRY(epair_softc) entry;
124 struct epair_tasks_t {
126 struct taskqueue *tq[MAXCPU];
129 static struct epair_tasks_t epair_tasks;
132 epair_clear_mbuf(struct mbuf *m)
136 /* Remove any CSUM_SND_TAG as ether_input will barf. */
137 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
138 m_snd_tag_rele(m->m_pkthdr.snd_tag);
139 m->m_pkthdr.snd_tag = NULL;
140 m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
143 /* Clear vlan information. */
144 m->m_flags &= ~M_VLANTAG;
145 m->m_pkthdr.ether_vtag = 0;
147 m_tag_delete_nonpersistent(m);
151 epair_tx_start_deferred(void *arg, int pending)
153 struct epair_queue *q = (struct epair_queue *)arg;
161 CURVNET_SET(ifp->if_vnet);
164 m = mbufq_flush(&q->q);
165 q->state = EPAIR_QUEUE_RUNNING;
169 n = STAILQ_NEXT(m, m_stailqpkt);
176 * Avoid flushing the queue more than once per task. We can otherwise
177 * end up starving ourselves in a multi-epair routing configuration.
180 if (mbufq_len(&q->q) > 0) {
182 q->state = EPAIR_QUEUE_WAKING;
185 q->state = EPAIR_QUEUE_IDLE;
190 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
196 static struct epair_queue *
197 epair_select_queue(struct epair_softc *sc, struct mbuf *m)
201 struct ether_header *eh;
204 ret = rss_m2bucket(m, &bucket);
206 /* Actually hash the packet. */
207 eh = mtod(m, struct ether_header *);
209 switch (ntohs(eh->ether_type)) {
212 rss_soft_m2cpuid_v4(m, 0, &bucket);
217 rss_soft_m2cpuid_v6(m, 0, &bucket);
225 bucket %= sc->num_queues;
229 return (&sc->queues[bucket]);
233 epair_prepare_mbuf(struct mbuf *m, struct ifnet *src_ifp)
237 if_setrcvif(m, src_ifp);
238 M_SETFIB(m, src_ifp->if_fib);
240 MPASS(m->m_nextpkt == NULL);
241 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
245 epair_menq(struct mbuf *m, struct epair_softc *osc)
247 struct epair_queue *q;
248 struct ifnet *ifp, *oifp;
253 * I know this looks weird. We pass the "other sc" as we need that one
254 * and can get both ifps from it as well.
259 epair_prepare_mbuf(m, oifp);
261 /* Save values as once the mbuf is queued, it's not ours anymore. */
262 len = m->m_pkthdr.len;
263 mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
265 q = epair_select_queue(osc, m);
268 if (q->state == EPAIR_QUEUE_IDLE) {
269 q->state = EPAIR_QUEUE_WAKING;
270 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
272 error = mbufq_enqueue(&q->q, m);
277 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
279 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
280 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
282 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
283 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
288 epair_start(struct ifnet *ifp)
291 struct epair_softc *sc;
295 * We get packets here from ether_output via if_handoff()
296 * and need to put them into the input queue of the oifp
297 * and will put the packet into the receive-queue (rxq) of the
298 * other interface (oifp) of our pair.
304 IFQ_DEQUEUE(&ifp->if_snd, m);
310 /* In case either interface is not usable drop the packet. */
311 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
312 (ifp->if_flags & IFF_UP) == 0 ||
313 (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
314 (oifp->if_flags & IFF_UP) == 0) {
324 epair_transmit(struct ifnet *ifp, struct mbuf *m)
326 struct epair_softc *sc;
338 * We could just transmit this, but it makes testing easier if we're a
339 * little bit more like real hardware.
340 * Allow just that little bit extra for ethernet (and vlan) headers.
342 if (m->m_pkthdr.len > (ifp->if_mtu + sizeof(struct ether_vlan_header))) {
344 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
349 * We are not going to use the interface en/dequeue mechanism
350 * on the TX side. We are called from ether_output_frame()
351 * and will put the packet into the receive-queue (rxq) of the
352 * other interface (oifp) of our pair.
354 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
356 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
359 if ((ifp->if_flags & IFF_UP) == 0) {
361 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
368 * In case the outgoing interface is not usable,
373 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
374 (oifp->if_flags & IFF_UP) == 0) {
375 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
381 len = m->m_pkthdr.len;
382 mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
385 /* Support ALTQ via the classic if_start() path. */
386 IF_LOCK(&ifp->if_snd);
387 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
388 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
390 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
391 IF_UNLOCK(&ifp->if_snd);
393 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
395 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
400 IF_UNLOCK(&ifp->if_snd);
403 epair_menq(m, oifp->if_softc);
408 epair_qflush(struct ifnet *ifp __unused)
413 epair_media_change(struct ifnet *ifp __unused)
421 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
424 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
425 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
429 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
431 struct epair_softc *sc;
435 ifr = (struct ifreq *)data;
446 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
450 /* We basically allow all kinds of MTUs. */
451 ifp->if_mtu = ifr->ifr_mtu;
456 /* Let the common ethernet handler process this. */
457 error = ether_ioctl(ifp, cmd, data);
465 epair_init(void *dummy __unused)
470 * Interface cloning functions.
471 * We use our private ones so that we can create/destroy our secondary
472 * device along with the primary one.
475 epair_clone_match(struct if_clone *ifc, const char *name)
480 * Our base name is epair.
481 * Our interfaces will be named epair<n>[ab].
482 * So accept anything of the following list:
485 * but not the epair<n>[ab] versions.
487 if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
490 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
491 if (*cp < '0' || *cp > '9')
499 epair_clone_add(struct if_clone *ifc, struct epair_softc *scb)
502 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
505 /* Copy epairNa etheraddr and change the last byte. */
506 memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
508 ether_ifattach(ifp, eaddr);
510 if_clone_addif(ifc, ifp);
513 static struct epair_softc *
514 epair_alloc_sc(struct if_clone *ifc)
516 struct epair_softc *sc;
518 struct ifnet *ifp = if_alloc(IFT_ETHER);
522 sc = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
524 sc->num_queues = epair_tasks.tasks;
525 sc->queues = mallocarray(sc->num_queues, sizeof(struct epair_queue),
527 for (int i = 0; i < sc->num_queues; i++) {
528 struct epair_queue *q = &sc->queues[i];
530 q->state = EPAIR_QUEUE_IDLE;
531 mtx_init(&q->mtx, "epairq", NULL, MTX_DEF | MTX_NEW);
532 mbufq_init(&q->q, RXRSIZE);
534 NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q);
537 /* Initialise pseudo media types. */
538 ifmedia_init(&sc->media, 0, epair_media_change, epair_media_status);
539 ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
540 ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T);
546 epair_setup_ifp(struct epair_softc *sc, char *name, int unit)
548 struct ifnet *ifp = sc->ifp;
551 strlcpy(ifp->if_xname, name, IFNAMSIZ);
552 ifp->if_dname = epairname;
553 ifp->if_dunit = unit;
554 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
555 ifp->if_capabilities = IFCAP_VLAN_MTU;
556 ifp->if_capenable = IFCAP_VLAN_MTU;
557 ifp->if_transmit = epair_transmit;
558 ifp->if_qflush = epair_qflush;
559 ifp->if_start = epair_start;
560 ifp->if_ioctl = epair_ioctl;
561 ifp->if_init = epair_init;
562 if_setsendqlen(ifp, ifqmaxlen);
563 if_setsendqready(ifp);
565 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
569 epair_generate_mac(struct epair_softc *sc, uint8_t *eaddr)
577 /* Get an approximate distribution. */
578 hash = next_index % mp_ncpus;
585 * Calculate the etheraddr hashing the hostid and the
586 * interface index. The result would be hopefully unique.
587 * Note that the "a" component of an epair instance may get moved
588 * to a different VNET after creation. In that case its index
589 * will be freed and the index can get reused by new epair instance.
590 * Make sure we do not create same etheraddr again.
592 getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
594 arc4rand(&hostid, sizeof(hostid), 0);
596 struct ifnet *ifp = sc->ifp;
598 if (ifp->if_index > next_index)
599 next_index = ifp->if_index;
603 key[0] = (uint32_t)next_index;
605 key[1] = (uint32_t)(hostid & 0xffffffff);
606 key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
607 hash = jenkins_hash32(key, 3, 0);
610 memcpy(&eaddr[1], &hash, 4);
615 epair_free_sc(struct epair_softc *sc)
621 ifmedia_removeall(&sc->media);
622 for (int i = 0; i < sc->num_queues; i++) {
623 struct epair_queue *q = &sc->queues[i];
624 mtx_destroy(&q->mtx);
626 free(sc->queues, M_EPAIR);
631 epair_set_state(struct ifnet *ifp, bool running)
634 ifp->if_drv_flags |= IFF_DRV_RUNNING;
635 if_link_state_change(ifp, LINK_STATE_UP);
637 if_link_state_change(ifp, LINK_STATE_DOWN);
638 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
643 epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
645 int error = 0, unit, wildcard;
648 /* Try to see if a special unit was requested. */
649 error = ifc_name2unit(name, &unit);
652 wildcard = (unit < 0);
654 error = ifc_alloc_unit(ifc, &unit);
659 * If no unit had been given, we need to adjust the ifName.
660 * Also make sure there is space for our extra [ab] suffix.
662 for (dp = name; *dp != '\0'; dp++);
664 int slen = snprintf(dp, len - (dp - name), "%d", unit);
665 if (slen > len - (dp - name) - 1) {
666 /* ifName too long. */
672 if (len - (dp - name) - 1 < 1) {
673 /* No space left for our [ab] suffix. */
678 /* Must not change dp so we can replace 'a' by 'b' later. */
681 /* Check if 'a' and 'b' interfaces already exist. */
682 if (ifunit(name) != NULL) {
688 if (ifunit(name) != NULL) {
695 ifc_free_unit(ifc, unit);
701 epair_clone_create(struct if_clone *ifc, char *name, size_t len,
702 struct ifc_data *ifd, struct ifnet **ifpp)
704 struct epair_softc *sca, *scb;
708 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
710 error = epair_handle_unit(ifc, name, len, &unit);
714 /* Allocate memory for both [ab] interfaces */
715 sca = epair_alloc_sc(ifc);
716 scb = epair_alloc_sc(ifc);
717 if (sca == NULL || scb == NULL) {
720 ifc_free_unit(ifc, unit);
725 * Cross-reference the interfaces so we will be able to free both.
727 sca->oifp = scb->ifp;
728 scb->oifp = sca->ifp;
730 /* Finish initialization of interface <n>a. */
732 epair_setup_ifp(sca, name, unit);
733 epair_generate_mac(sca, eaddr);
735 ether_ifattach(ifp, eaddr);
737 /* Swap the name and finish initialization of interface <n>b. */
738 dp = name + strlen(name) - 1;
741 epair_setup_ifp(scb, name, unit);
744 /* We need to play some tricks here for the second interface. */
745 strlcpy(name, epairname, len);
746 /* Correctly set the name for the cloner list. */
747 strlcpy(name, scb->ifp->if_xname, len);
749 epair_clone_add(ifc, scb);
752 * Restore name to <n>a as the ifp for this will go into the
753 * cloner list for the initial call.
755 strlcpy(name, sca->ifp->if_xname, len);
757 /* Tell the world, that we are ready to rock. */
758 epair_set_state(sca->ifp, true);
759 epair_set_state(scb->ifp, true);
767 epair_drain_rings(struct epair_softc *sc)
769 for (int i = 0; i < sc->num_queues; i++) {
770 struct epair_queue *q;
775 m = mbufq_flush(&q->q);
778 for (; m != NULL; m = n) {
786 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
789 struct epair_softc *sca, *scb;
793 * In case we called into if_clone_destroyif() ourselves
794 * again to remove the second interface, the softc will be
795 * NULL. In that case so not do anything but return success.
797 if (ifp->if_softc == NULL)
800 unit = ifp->if_dunit;
803 scb = oifp->if_softc;
805 /* Frist get the interfaces down and detached. */
806 epair_set_state(ifp, false);
807 epair_set_state(oifp, false);
810 ether_ifdetach(oifp);
812 /* Third free any queued packets and all the resources. */
813 CURVNET_SET_QUIET(oifp->if_vnet);
814 epair_drain_rings(scb);
815 oifp->if_softc = NULL;
816 error = if_clone_destroyif(ifc, oifp);
818 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
823 epair_drain_rings(sca);
826 /* Last free the cloner unit. */
827 ifc_free_unit(ifc, unit);
833 vnet_epair_init(const void *unused __unused)
835 struct if_clone_addreq req = {
836 .match_f = epair_clone_match,
837 .create_f = epair_clone_create,
838 .destroy_f = epair_clone_destroy,
840 V_epair_cloner = ifc_attach_cloner(epairname, &req);
842 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
843 vnet_epair_init, NULL);
846 vnet_epair_uninit(const void *unused __unused)
849 ifc_detach_cloner(V_epair_cloner);
851 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
852 vnet_epair_uninit, NULL);
858 epair_tasks.tasks = 0;
866 /* Pin to this CPU so we get appropriate NUMA allocations. */
867 thread_lock(curthread);
868 sched_bind(curthread, cpu);
869 thread_unlock(curthread);
871 snprintf(name, sizeof(name), "epair_task_%d", cpu);
873 epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK,
874 taskqueue_thread_enqueue,
875 &epair_tasks.tq[cpu]);
876 CPU_SETOF(cpu, &cpu_mask);
877 taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET,
878 &cpu_mask, "%s", name);
882 thread_lock(curthread);
883 sched_unbind(curthread);
884 thread_unlock(curthread);
886 snprintf(name, sizeof(name), "epair_task");
888 epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK,
889 taskqueue_thread_enqueue,
891 taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name);
893 epair_tasks.tasks = 1;
900 epair_mod_cleanup(void)
903 for (int i = 0; i < epair_tasks.tasks; i++) {
904 taskqueue_drain_all(epair_tasks.tq[i]);
905 taskqueue_free(epair_tasks.tq[i]);
910 epair_modevent(module_t mod, int type, void *data)
917 ret = epair_mod_init();
921 printf("%s: %s initialized.\n", __func__, epairname);
925 EPAIR_LOCK_DESTROY();
927 printf("%s: %s unloaded.\n", __func__, epairname);
935 static moduledata_t epair_mod = {
941 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
942 MODULE_VERSION(if_epair, 3);