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 53dd5760aSBjoern A. Zeeb * Copyright (c) 2009-2021 Bjoern A. Zeeb <bz@FreeBSD.org> 698c230c8SBjoern A. Zeeb * 798c230c8SBjoern A. Zeeb * This software was developed by CK Software GmbH under sponsorship 898c230c8SBjoern A. Zeeb * from the FreeBSD Foundation. 998c230c8SBjoern A. Zeeb * 1098c230c8SBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 1198c230c8SBjoern A. Zeeb * modification, are permitted provided that the following conditions 1298c230c8SBjoern A. Zeeb * are met: 1398c230c8SBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 1498c230c8SBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 1598c230c8SBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 1698c230c8SBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 1798c230c8SBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 1898c230c8SBjoern A. Zeeb * 1998c230c8SBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2098c230c8SBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2198c230c8SBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2298c230c8SBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2398c230c8SBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2498c230c8SBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2598c230c8SBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2698c230c8SBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2798c230c8SBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2898c230c8SBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2998c230c8SBjoern A. Zeeb * SUCH DAMAGE. 3098c230c8SBjoern A. Zeeb */ 3198c230c8SBjoern A. Zeeb 3298c230c8SBjoern A. Zeeb /* 33d0ea4743SBjoern A. Zeeb * A pair of virtual back-to-back connected ethernet like interfaces 34d0ea4743SBjoern A. Zeeb * (``two interfaces with a virtual cross-over cable''). 35d0ea4743SBjoern A. Zeeb * 3698c230c8SBjoern A. Zeeb * This is mostly intended to be used to provide connectivity between 3798c230c8SBjoern A. Zeeb * different virtual network stack instances. 3898c230c8SBjoern A. Zeeb */ 3998c230c8SBjoern A. Zeeb 4098c230c8SBjoern A. Zeeb #include <sys/cdefs.h> 4198c230c8SBjoern A. Zeeb __FBSDID("$FreeBSD$"); 4298c230c8SBjoern A. Zeeb 43*24f0bfbaSKristof Provost #include "opt_rss.h" 44*24f0bfbaSKristof Provost 4598c230c8SBjoern A. Zeeb #include <sys/param.h> 4611d41666SLuca Pizzamiglio #include <sys/hash.h> 4711d41666SLuca Pizzamiglio #include <sys/jail.h> 4898c230c8SBjoern A. Zeeb #include <sys/kernel.h> 4911d41666SLuca Pizzamiglio #include <sys/libkern.h> 508ec07310SGleb Smirnoff #include <sys/malloc.h> 5198c230c8SBjoern A. Zeeb #include <sys/mbuf.h> 5298c230c8SBjoern A. Zeeb #include <sys/module.h> 5311d41666SLuca Pizzamiglio #include <sys/proc.h> 5498c230c8SBjoern A. Zeeb #include <sys/queue.h> 55*24f0bfbaSKristof Provost #include <sys/sched.h> 56d0ea4743SBjoern A. Zeeb #include <sys/smp.h> 5798c230c8SBjoern A. Zeeb #include <sys/socket.h> 5898c230c8SBjoern A. Zeeb #include <sys/sockio.h> 59*24f0bfbaSKristof Provost #include <sys/taskqueue.h> 6098c230c8SBjoern A. Zeeb #include <sys/types.h> 613dd5760aSBjoern A. Zeeb #include <sys/buf_ring.h> 623dd5760aSBjoern A. Zeeb #include <sys/bus.h> 633dd5760aSBjoern A. Zeeb #include <sys/interrupt.h> 6498c230c8SBjoern A. Zeeb 6598c230c8SBjoern A. Zeeb #include <net/bpf.h> 6698c230c8SBjoern A. Zeeb #include <net/ethernet.h> 6798c230c8SBjoern A. Zeeb #include <net/if.h> 6876039bc8SGleb Smirnoff #include <net/if_var.h> 6998c230c8SBjoern A. Zeeb #include <net/if_clone.h> 702dccdd45SMarko Zec #include <net/if_media.h> 7198c230c8SBjoern A. Zeeb #include <net/if_var.h> 7298c230c8SBjoern A. Zeeb #include <net/if_types.h> 7398c230c8SBjoern A. Zeeb #include <net/netisr.h> 74*24f0bfbaSKristof Provost #ifdef RSS 75*24f0bfbaSKristof Provost #include <net/rss_config.h> 76*24f0bfbaSKristof Provost #include <netinet/in_rss.h> 77*24f0bfbaSKristof Provost #include <netinet6/in6_rss.h> 78*24f0bfbaSKristof Provost #endif 79530c0060SRobert Watson #include <net/vnet.h> 8098c230c8SBjoern A. Zeeb 8198c230c8SBjoern A. Zeeb static int epair_clone_match(struct if_clone *, const char *); 8298c230c8SBjoern A. Zeeb static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t); 8398c230c8SBjoern A. Zeeb static int epair_clone_destroy(struct if_clone *, struct ifnet *); 8498c230c8SBjoern A. Zeeb 8542a58907SGleb Smirnoff static const char epairname[] = "epair"; 863dd5760aSBjoern A. Zeeb #define RXRSIZE 4096 /* Probably overkill by 4-8x. */ 87d0ea4743SBjoern A. Zeeb 8842a58907SGleb Smirnoff static MALLOC_DEFINE(M_EPAIR, epairname, 89d0ea4743SBjoern A. Zeeb "Pair of virtual cross-over connected Ethernet-like interfaces"); 9098c230c8SBjoern A. Zeeb 915f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, epair_cloner); 923c3136b1SHiroki Sato #define V_epair_cloner VNET(epair_cloner) 9398c230c8SBjoern A. Zeeb 943dd5760aSBjoern A. Zeeb static unsigned int next_index = 0; 953dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \ 963dd5760aSBjoern A. Zeeb NULL, MTX_DEF) 973dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx) 983dd5760aSBjoern A. Zeeb #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx) 993dd5760aSBjoern A. Zeeb #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx) 1003dd5760aSBjoern A. Zeeb 101*24f0bfbaSKristof Provost struct epair_softc; 102*24f0bfbaSKristof Provost struct epair_queue { 103*24f0bfbaSKristof Provost int id; 104*24f0bfbaSKristof Provost struct buf_ring *rxring[2]; 105*24f0bfbaSKristof Provost volatile int ridx; /* 0 || 1 */ 106*24f0bfbaSKristof Provost struct task tx_task; 107*24f0bfbaSKristof Provost struct epair_softc *sc; 108*24f0bfbaSKristof Provost }; 1093dd5760aSBjoern A. Zeeb 1103dd5760aSBjoern A. Zeeb static struct mtx epair_n_index_mtx; 1113dd5760aSBjoern A. Zeeb struct epair_softc { 1123dd5760aSBjoern A. Zeeb struct ifnet *ifp; /* This ifp. */ 1133dd5760aSBjoern A. Zeeb struct ifnet *oifp; /* other ifp of pair. */ 114*24f0bfbaSKristof Provost int num_queues; 115*24f0bfbaSKristof Provost struct epair_queue *queues; 1163dd5760aSBjoern A. Zeeb struct ifmedia media; /* Media config (fake). */ 1173dd5760aSBjoern A. Zeeb STAILQ_ENTRY(epair_softc) entry; 118d0ea4743SBjoern A. Zeeb }; 119d0ea4743SBjoern A. Zeeb 120*24f0bfbaSKristof Provost struct epair_tasks_t { 121*24f0bfbaSKristof Provost int tasks; 122*24f0bfbaSKristof Provost struct taskqueue *tq[MAXCPU]; 123*24f0bfbaSKristof Provost }; 124*24f0bfbaSKristof Provost 125*24f0bfbaSKristof Provost static struct epair_tasks_t epair_tasks; 126*24f0bfbaSKristof Provost 127d0ea4743SBjoern A. Zeeb static void 12862d2dcafSKristof Provost epair_clear_mbuf(struct mbuf *m) 12962d2dcafSKristof Provost { 13062d2dcafSKristof Provost /* Remove any CSUM_SND_TAG as ether_input will barf. */ 13162d2dcafSKristof Provost if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 13262d2dcafSKristof Provost m_snd_tag_rele(m->m_pkthdr.snd_tag); 13362d2dcafSKristof Provost m->m_pkthdr.snd_tag = NULL; 13462d2dcafSKristof Provost m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG; 13562d2dcafSKristof Provost } 13662d2dcafSKristof Provost 13762d2dcafSKristof Provost m_tag_delete_nonpersistent(m); 13862d2dcafSKristof Provost } 13962d2dcafSKristof Provost 14062d2dcafSKristof Provost static void 141*24f0bfbaSKristof Provost epair_if_input(struct epair_softc *sc, struct epair_queue *q, int ridx) 142d0ea4743SBjoern A. Zeeb { 1433dd5760aSBjoern A. Zeeb struct ifnet *ifp; 1443dd5760aSBjoern A. Zeeb struct mbuf *m; 145d0ea4743SBjoern A. Zeeb 1463dd5760aSBjoern A. Zeeb ifp = sc->ifp; 147*24f0bfbaSKristof Provost CURVNET_SET(ifp->if_vnet); 148*24f0bfbaSKristof Provost while (! buf_ring_empty(q->rxring[ridx])) { 149*24f0bfbaSKristof Provost m = buf_ring_dequeue_mc(q->rxring[ridx]); 1503dd5760aSBjoern A. Zeeb if (m == NULL) 151*24f0bfbaSKristof Provost continue; 152d0ea4743SBjoern A. Zeeb 1533dd5760aSBjoern A. Zeeb MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 1543dd5760aSBjoern A. Zeeb (*ifp->if_input)(ifp, m); 155d0ea4743SBjoern A. Zeeb 156*24f0bfbaSKristof Provost } 157*24f0bfbaSKristof Provost CURVNET_RESTORE(); 158d0ea4743SBjoern A. Zeeb } 159d0ea4743SBjoern A. Zeeb 160d0ea4743SBjoern A. Zeeb static void 161*24f0bfbaSKristof Provost epair_tx_start_deferred(void *arg, int pending) 162d0ea4743SBjoern A. Zeeb { 163*24f0bfbaSKristof Provost struct epair_queue *q = (struct epair_queue *)arg; 164*24f0bfbaSKristof Provost struct epair_softc *sc = q->sc; 1653dd5760aSBjoern A. Zeeb int ridx, nidx; 166d0ea4743SBjoern A. Zeeb 1673dd5760aSBjoern A. Zeeb if_ref(sc->ifp); 168*24f0bfbaSKristof Provost ridx = atomic_load_int(&q->ridx); 1693dd5760aSBjoern A. Zeeb do { 1703dd5760aSBjoern A. Zeeb nidx = (ridx == 0) ? 1 : 0; 171*24f0bfbaSKristof Provost } while (!atomic_fcmpset_int(&q->ridx, &ridx, nidx)); 172*24f0bfbaSKristof Provost epair_if_input(sc, q, ridx); 173*24f0bfbaSKristof Provost 174*24f0bfbaSKristof Provost if (! buf_ring_empty(q->rxring[nidx])) 175*24f0bfbaSKristof Provost taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); 176d0ea4743SBjoern A. Zeeb 1773dd5760aSBjoern A. Zeeb if_rele(sc->ifp); 178d0ea4743SBjoern A. Zeeb } 179d0ea4743SBjoern A. Zeeb 180d0ea4743SBjoern A. Zeeb static int 1813dd5760aSBjoern A. Zeeb epair_menq(struct mbuf *m, struct epair_softc *osc) 182d0ea4743SBjoern A. Zeeb { 1833dd5760aSBjoern A. Zeeb struct ifnet *ifp, *oifp; 1843dd5760aSBjoern A. Zeeb int len, ret; 1853dd5760aSBjoern A. Zeeb int ridx; 1863dd5760aSBjoern A. Zeeb short mflags; 187*24f0bfbaSKristof Provost struct epair_queue *q = NULL; 188*24f0bfbaSKristof Provost uint32_t bucket; 1893dd5760aSBjoern A. Zeeb bool was_empty; 190*24f0bfbaSKristof Provost #ifdef RSS 191*24f0bfbaSKristof Provost struct ether_header *eh; 192*24f0bfbaSKristof Provost #endif 193d0ea4743SBjoern A. Zeeb 1943dd5760aSBjoern A. Zeeb /* 1953dd5760aSBjoern A. Zeeb * I know this looks weird. We pass the "other sc" as we need that one 1963dd5760aSBjoern A. Zeeb * and can get both ifps from it as well. 1973dd5760aSBjoern A. Zeeb */ 1983dd5760aSBjoern A. Zeeb oifp = osc->ifp; 1993dd5760aSBjoern A. Zeeb ifp = osc->oifp; 2003dd5760aSBjoern A. Zeeb 2013dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 2023dd5760aSBjoern A. Zeeb epair_clear_mbuf(m); 2033dd5760aSBjoern A. Zeeb if_setrcvif(m, oifp); 2043dd5760aSBjoern A. Zeeb M_SETFIB(m, oifp->if_fib); 2053dd5760aSBjoern A. Zeeb 2063dd5760aSBjoern A. Zeeb /* Save values as once the mbuf is queued, it's not ours anymore. */ 2073dd5760aSBjoern A. Zeeb len = m->m_pkthdr.len; 2083dd5760aSBjoern A. Zeeb mflags = m->m_flags; 2093dd5760aSBjoern A. Zeeb 2103dd5760aSBjoern A. Zeeb MPASS(m->m_nextpkt == NULL); 2113dd5760aSBjoern A. Zeeb MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 2123dd5760aSBjoern A. Zeeb 213*24f0bfbaSKristof Provost #ifdef RSS 214*24f0bfbaSKristof Provost ret = rss_m2bucket(m, &bucket); 215*24f0bfbaSKristof Provost if (ret) { 216*24f0bfbaSKristof Provost /* Actually hash the packet. */ 217*24f0bfbaSKristof Provost eh = mtod(m, struct ether_header *); 218*24f0bfbaSKristof Provost 219*24f0bfbaSKristof Provost switch (ntohs(eh->ether_type)) { 220*24f0bfbaSKristof Provost case ETHERTYPE_IP: 221*24f0bfbaSKristof Provost rss_soft_m2cpuid_v4(m, 0, &bucket); 222*24f0bfbaSKristof Provost break; 223*24f0bfbaSKristof Provost case ETHERTYPE_IPV6: 224*24f0bfbaSKristof Provost rss_soft_m2cpuid_v6(m, 0, &bucket); 225*24f0bfbaSKristof Provost break; 226*24f0bfbaSKristof Provost default: 227*24f0bfbaSKristof Provost bucket = 0; 228*24f0bfbaSKristof Provost break; 229*24f0bfbaSKristof Provost } 230*24f0bfbaSKristof Provost } 231*24f0bfbaSKristof Provost bucket %= osc->num_queues; 232*24f0bfbaSKristof Provost #else 233*24f0bfbaSKristof Provost bucket = 0; 234*24f0bfbaSKristof Provost #endif 235*24f0bfbaSKristof Provost q = &osc->queues[bucket]; 236*24f0bfbaSKristof Provost 237*24f0bfbaSKristof Provost ridx = atomic_load_int(&q->ridx); 238*24f0bfbaSKristof Provost was_empty = buf_ring_empty(q->rxring[ridx]); 239*24f0bfbaSKristof Provost ret = buf_ring_enqueue(q->rxring[ridx], m); 2403dd5760aSBjoern A. Zeeb if (ret != 0) { 2413dd5760aSBjoern A. Zeeb /* Ring is full. */ 242*24f0bfbaSKristof Provost if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 2433dd5760aSBjoern A. Zeeb m_freem(m); 244*24f0bfbaSKristof Provost goto done; 2453dd5760aSBjoern A. Zeeb } 246d0ea4743SBjoern A. Zeeb 2473dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2483dd5760aSBjoern A. Zeeb /* 2493dd5760aSBjoern A. Zeeb * IFQ_HANDOFF_ADJ/ip_handoff() update statistics, 2503dd5760aSBjoern A. Zeeb * but as we bypass all this we have to duplicate 2513dd5760aSBjoern A. Zeeb * the logic another time. 2523dd5760aSBjoern A. Zeeb */ 2533dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 2543dd5760aSBjoern A. Zeeb if (mflags & (M_BCAST|M_MCAST)) 2553dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 2563dd5760aSBjoern A. Zeeb /* Someone else received the packet. */ 2573dd5760aSBjoern A. Zeeb if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1); 258d0ea4743SBjoern A. Zeeb 259*24f0bfbaSKristof Provost done: 260*24f0bfbaSKristof Provost if (was_empty) 261*24f0bfbaSKristof Provost taskqueue_enqueue(epair_tasks.tq[bucket], &q->tx_task); 262d0ea4743SBjoern A. Zeeb 263d0ea4743SBjoern A. Zeeb return (0); 264d0ea4743SBjoern A. Zeeb } 265d0ea4743SBjoern A. Zeeb 26698c230c8SBjoern A. Zeeb static void 2673dd5760aSBjoern A. Zeeb epair_start(struct ifnet *ifp) 26898c230c8SBjoern A. Zeeb { 26998c230c8SBjoern A. Zeeb struct mbuf *m; 27098c230c8SBjoern A. Zeeb struct epair_softc *sc; 27198c230c8SBjoern A. Zeeb struct ifnet *oifp; 27298c230c8SBjoern A. Zeeb 27398c230c8SBjoern A. Zeeb /* 27459f35a82SPatrick Kelsey * We get packets here from ether_output via if_handoff() 27575580d58SPatrick Kelsey * and need to put them into the input queue of the oifp 2763dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the 2773dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair. 27898c230c8SBjoern A. Zeeb */ 2793dd5760aSBjoern A. Zeeb sc = ifp->if_softc; 28098c230c8SBjoern A. Zeeb oifp = sc->oifp; 28198c230c8SBjoern A. Zeeb sc = oifp->if_softc; 28298c230c8SBjoern A. Zeeb for (;;) { 28398c230c8SBjoern A. Zeeb IFQ_DEQUEUE(&ifp->if_snd, m); 28498c230c8SBjoern A. Zeeb if (m == NULL) 28598c230c8SBjoern A. Zeeb break; 2863dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 28798c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m); 28898c230c8SBjoern A. Zeeb 2893dd5760aSBjoern A. Zeeb /* In case either interface is not usable drop the packet. */ 2903dd5760aSBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2913dd5760aSBjoern A. Zeeb (ifp->if_flags & IFF_UP) == 0 || 2923dd5760aSBjoern A. Zeeb (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 29398c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) { 29498c230c8SBjoern A. Zeeb m_freem(m); 29598c230c8SBjoern A. Zeeb continue; 29698c230c8SBjoern A. Zeeb } 29798c230c8SBjoern A. Zeeb 2983dd5760aSBjoern A. Zeeb (void) epair_menq(m, sc); 29998c230c8SBjoern A. Zeeb } 30098c230c8SBjoern A. Zeeb } 30198c230c8SBjoern A. Zeeb 30298c230c8SBjoern A. Zeeb static int 3033dd5760aSBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m) 30498c230c8SBjoern A. Zeeb { 30598c230c8SBjoern A. Zeeb struct epair_softc *sc; 30698c230c8SBjoern A. Zeeb struct ifnet *oifp; 3072cedfc3fSMateusz Guzik int error; 3082cedfc3fSMateusz Guzik #ifdef ALTQ 3092cedfc3fSMateusz Guzik int len; 31098c230c8SBjoern A. Zeeb short mflags; 3112cedfc3fSMateusz Guzik #endif 31298c230c8SBjoern A. Zeeb 31398c230c8SBjoern A. Zeeb if (m == NULL) 31498c230c8SBjoern A. Zeeb return (0); 3153dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 31698c230c8SBjoern A. Zeeb 31798c230c8SBjoern A. Zeeb /* 31898c230c8SBjoern A. Zeeb * We are not going to use the interface en/dequeue mechanism 31998c230c8SBjoern A. Zeeb * on the TX side. We are called from ether_output_frame() 3203dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the 3213dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair. 32298c230c8SBjoern A. Zeeb */ 32398c230c8SBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 32498c230c8SBjoern A. Zeeb m_freem(m); 3253dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 32698c230c8SBjoern A. Zeeb return (ENXIO); 32798c230c8SBjoern A. Zeeb } 32898c230c8SBjoern A. Zeeb if ((ifp->if_flags & IFF_UP) == 0) { 32998c230c8SBjoern A. Zeeb m_freem(m); 3303dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 33198c230c8SBjoern A. Zeeb return (ENETDOWN); 33298c230c8SBjoern A. Zeeb } 33398c230c8SBjoern A. Zeeb 33498c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m); 33598c230c8SBjoern A. Zeeb 33698c230c8SBjoern A. Zeeb /* 33798c230c8SBjoern A. Zeeb * In case the outgoing interface is not usable, 33898c230c8SBjoern A. Zeeb * drop the packet. 33998c230c8SBjoern A. Zeeb */ 3403dd5760aSBjoern A. Zeeb sc = ifp->if_softc; 34198c230c8SBjoern A. Zeeb oifp = sc->oifp; 34298c230c8SBjoern A. Zeeb if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 34398c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) { 3443751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 34598c230c8SBjoern A. Zeeb m_freem(m); 34698c230c8SBjoern A. Zeeb return (0); 34798c230c8SBjoern A. Zeeb } 3482cedfc3fSMateusz Guzik 3492cedfc3fSMateusz Guzik #ifdef ALTQ 35098c230c8SBjoern A. Zeeb len = m->m_pkthdr.len; 35198c230c8SBjoern A. Zeeb mflags = m->m_flags; 35298c230c8SBjoern A. Zeeb 353a4641f4eSPedro F. Giffuni /* Support ALTQ via the classic if_start() path. */ 35498c230c8SBjoern A. Zeeb IF_LOCK(&ifp->if_snd); 35598c230c8SBjoern A. Zeeb if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 35698c230c8SBjoern A. Zeeb ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 35798c230c8SBjoern A. Zeeb if (error) 3583751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 35998c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd); 36098c230c8SBjoern A. Zeeb if (!error) { 3613751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 36298c230c8SBjoern A. Zeeb if (mflags & (M_BCAST|M_MCAST)) 3633751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 3643dd5760aSBjoern A. Zeeb epair_start(ifp); 36598c230c8SBjoern A. Zeeb } 36698c230c8SBjoern A. Zeeb return (error); 36798c230c8SBjoern A. Zeeb } 36898c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd); 36998c230c8SBjoern A. Zeeb #endif 37098c230c8SBjoern A. Zeeb 3713dd5760aSBjoern A. Zeeb error = epair_menq(m, oifp->if_softc); 37298c230c8SBjoern A. Zeeb return (error); 37398c230c8SBjoern A. Zeeb } 37462d2dcafSKristof Provost 37598c230c8SBjoern A. Zeeb static int 3762dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused) 3772dccdd45SMarko Zec { 3782dccdd45SMarko Zec 3792dccdd45SMarko Zec /* Do nothing. */ 3802dccdd45SMarko Zec return (0); 3812dccdd45SMarko Zec } 3822dccdd45SMarko Zec 3832dccdd45SMarko Zec static void 3842dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr) 3852dccdd45SMarko Zec { 3862dccdd45SMarko Zec 3872dccdd45SMarko Zec imr->ifm_status = IFM_AVALID | IFM_ACTIVE; 3882dccdd45SMarko Zec imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX; 3892dccdd45SMarko Zec } 3902dccdd45SMarko Zec 3912dccdd45SMarko Zec static int 39298c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 39398c230c8SBjoern A. Zeeb { 3942dccdd45SMarko Zec struct epair_softc *sc; 39598c230c8SBjoern A. Zeeb struct ifreq *ifr; 39698c230c8SBjoern A. Zeeb int error; 39798c230c8SBjoern A. Zeeb 39898c230c8SBjoern A. Zeeb ifr = (struct ifreq *)data; 39998c230c8SBjoern A. Zeeb switch (cmd) { 40098c230c8SBjoern A. Zeeb case SIOCSIFFLAGS: 40198c230c8SBjoern A. Zeeb case SIOCADDMULTI: 40298c230c8SBjoern A. Zeeb case SIOCDELMULTI: 40398c230c8SBjoern A. Zeeb error = 0; 40498c230c8SBjoern A. Zeeb break; 40598c230c8SBjoern A. Zeeb 4062dccdd45SMarko Zec case SIOCSIFMEDIA: 4072dccdd45SMarko Zec case SIOCGIFMEDIA: 4082dccdd45SMarko Zec sc = ifp->if_softc; 4092dccdd45SMarko Zec error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd); 4102dccdd45SMarko Zec break; 4112dccdd45SMarko Zec 412d0ea4743SBjoern A. Zeeb case SIOCSIFMTU: 413d0ea4743SBjoern A. Zeeb /* We basically allow all kinds of MTUs. */ 414d0ea4743SBjoern A. Zeeb ifp->if_mtu = ifr->ifr_mtu; 415d0ea4743SBjoern A. Zeeb error = 0; 416d0ea4743SBjoern A. Zeeb break; 417d0ea4743SBjoern A. Zeeb 41898c230c8SBjoern A. Zeeb default: 41998c230c8SBjoern A. Zeeb /* Let the common ethernet handler process this. */ 42098c230c8SBjoern A. Zeeb error = ether_ioctl(ifp, cmd, data); 42198c230c8SBjoern A. Zeeb break; 42298c230c8SBjoern A. Zeeb } 42398c230c8SBjoern A. Zeeb 42498c230c8SBjoern A. Zeeb return (error); 42598c230c8SBjoern A. Zeeb } 42698c230c8SBjoern A. Zeeb 42798c230c8SBjoern A. Zeeb static void 42898c230c8SBjoern A. Zeeb epair_init(void *dummy __unused) 42998c230c8SBjoern A. Zeeb { 43098c230c8SBjoern A. Zeeb } 43198c230c8SBjoern A. Zeeb 43298c230c8SBjoern A. Zeeb /* 43398c230c8SBjoern A. Zeeb * Interface cloning functions. 43498c230c8SBjoern A. Zeeb * We use our private ones so that we can create/destroy our secondary 43598c230c8SBjoern A. Zeeb * device along with the primary one. 43698c230c8SBjoern A. Zeeb */ 43798c230c8SBjoern A. Zeeb static int 43898c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name) 43998c230c8SBjoern A. Zeeb { 44098c230c8SBjoern A. Zeeb const char *cp; 44198c230c8SBjoern A. Zeeb 44298c230c8SBjoern A. Zeeb /* 44398c230c8SBjoern A. Zeeb * Our base name is epair. 44498c230c8SBjoern A. Zeeb * Our interfaces will be named epair<n>[ab]. 44598c230c8SBjoern A. Zeeb * So accept anything of the following list: 44698c230c8SBjoern A. Zeeb * - epair 44798c230c8SBjoern A. Zeeb * - epair<n> 44898c230c8SBjoern A. Zeeb * but not the epair<n>[ab] versions. 44998c230c8SBjoern A. Zeeb */ 45042a58907SGleb Smirnoff if (strncmp(epairname, name, sizeof(epairname)-1) != 0) 45198c230c8SBjoern A. Zeeb return (0); 45298c230c8SBjoern A. Zeeb 45342a58907SGleb Smirnoff for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) { 45498c230c8SBjoern A. Zeeb if (*cp < '0' || *cp > '9') 45598c230c8SBjoern A. Zeeb return (0); 45698c230c8SBjoern A. Zeeb } 45798c230c8SBjoern A. Zeeb 45898c230c8SBjoern A. Zeeb return (1); 45998c230c8SBjoern A. Zeeb } 46098c230c8SBjoern A. Zeeb 461b02fd8b7SKristof Provost static void 462b02fd8b7SKristof Provost epair_clone_add(struct if_clone *ifc, struct epair_softc *scb) 463b02fd8b7SKristof Provost { 464b02fd8b7SKristof Provost struct ifnet *ifp; 465b02fd8b7SKristof Provost uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 466b02fd8b7SKristof Provost 467b02fd8b7SKristof Provost ifp = scb->ifp; 468b02fd8b7SKristof Provost /* Copy epairNa etheraddr and change the last byte. */ 469b02fd8b7SKristof Provost memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN); 470b02fd8b7SKristof Provost eaddr[5] = 0x0b; 471b02fd8b7SKristof Provost ether_ifattach(ifp, eaddr); 472b02fd8b7SKristof Provost 473b02fd8b7SKristof Provost if_clone_addif(ifc, ifp); 474b02fd8b7SKristof Provost } 475b02fd8b7SKristof Provost 47698c230c8SBjoern A. Zeeb static int 47798c230c8SBjoern A. Zeeb epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 47898c230c8SBjoern A. Zeeb { 47998c230c8SBjoern A. Zeeb struct epair_softc *sca, *scb; 48098c230c8SBjoern A. Zeeb struct ifnet *ifp; 48198c230c8SBjoern A. Zeeb char *dp; 48298c230c8SBjoern A. Zeeb int error, unit, wildcard; 48311d41666SLuca Pizzamiglio uint64_t hostid; 48411d41666SLuca Pizzamiglio uint32_t key[3]; 48511d41666SLuca Pizzamiglio uint32_t hash; 48698c230c8SBjoern A. Zeeb uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 48798c230c8SBjoern A. Zeeb 48898c230c8SBjoern A. Zeeb /* Try to see if a special unit was requested. */ 48998c230c8SBjoern A. Zeeb error = ifc_name2unit(name, &unit); 49098c230c8SBjoern A. Zeeb if (error != 0) 49198c230c8SBjoern A. Zeeb return (error); 49298c230c8SBjoern A. Zeeb wildcard = (unit < 0); 49398c230c8SBjoern A. Zeeb 49498c230c8SBjoern A. Zeeb error = ifc_alloc_unit(ifc, &unit); 49598c230c8SBjoern A. Zeeb if (error != 0) 49698c230c8SBjoern A. Zeeb return (error); 49798c230c8SBjoern A. Zeeb 49898c230c8SBjoern A. Zeeb /* 49998c230c8SBjoern A. Zeeb * If no unit had been given, we need to adjust the ifName. 50098c230c8SBjoern A. Zeeb * Also make sure there is space for our extra [ab] suffix. 50198c230c8SBjoern A. Zeeb */ 50298c230c8SBjoern A. Zeeb for (dp = name; *dp != '\0'; dp++); 50398c230c8SBjoern A. Zeeb if (wildcard) { 50498c230c8SBjoern A. Zeeb error = snprintf(dp, len - (dp - name), "%d", unit); 50598c230c8SBjoern A. Zeeb if (error > len - (dp - name) - 1) { 50698c230c8SBjoern A. Zeeb /* ifName too long. */ 50798c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 50898c230c8SBjoern A. Zeeb return (ENOSPC); 50998c230c8SBjoern A. Zeeb } 51098c230c8SBjoern A. Zeeb dp += error; 51198c230c8SBjoern A. Zeeb } 51298c230c8SBjoern A. Zeeb if (len - (dp - name) - 1 < 1) { 51398c230c8SBjoern A. Zeeb /* No space left for our [ab] suffix. */ 51498c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 51598c230c8SBjoern A. Zeeb return (ENOSPC); 51698c230c8SBjoern A. Zeeb } 5173c3136b1SHiroki Sato *dp = 'b'; 51898c230c8SBjoern A. Zeeb /* Must not change dp so we can replace 'a' by 'b' later. */ 51998c230c8SBjoern A. Zeeb *(dp+1) = '\0'; 52098c230c8SBjoern A. Zeeb 5213c3136b1SHiroki Sato /* Check if 'a' and 'b' interfaces already exist. */ 5223c3136b1SHiroki Sato if (ifunit(name) != NULL) 5233c3136b1SHiroki Sato return (EEXIST); 5243c3136b1SHiroki Sato *dp = 'a'; 5253c3136b1SHiroki Sato if (ifunit(name) != NULL) 5263c3136b1SHiroki Sato return (EEXIST); 5273c3136b1SHiroki Sato 52898c230c8SBjoern A. Zeeb /* Allocate memory for both [ab] interfaces */ 52998c230c8SBjoern A. Zeeb sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO); 53098c230c8SBjoern A. Zeeb sca->ifp = if_alloc(IFT_ETHER); 531*24f0bfbaSKristof Provost sca->num_queues = epair_tasks.tasks; 53298c230c8SBjoern A. Zeeb if (sca->ifp == NULL) { 53398c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 53498c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 53598c230c8SBjoern A. Zeeb return (ENOSPC); 53698c230c8SBjoern A. Zeeb } 537*24f0bfbaSKristof Provost sca->queues = mallocarray(sca->num_queues, sizeof(struct epair_queue), 538*24f0bfbaSKristof Provost M_EPAIR, M_WAITOK); 539*24f0bfbaSKristof Provost for (int i = 0; i < sca->num_queues; i++) { 540*24f0bfbaSKristof Provost struct epair_queue *q = &sca->queues[i]; 541*24f0bfbaSKristof Provost q->id = i; 542*24f0bfbaSKristof Provost q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 543*24f0bfbaSKristof Provost q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 544*24f0bfbaSKristof Provost q->ridx = 0; 545*24f0bfbaSKristof Provost q->sc = sca; 546*24f0bfbaSKristof Provost NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); 547*24f0bfbaSKristof Provost } 54898c230c8SBjoern A. Zeeb 54998c230c8SBjoern A. Zeeb scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO); 55098c230c8SBjoern A. Zeeb scb->ifp = if_alloc(IFT_ETHER); 551*24f0bfbaSKristof Provost scb->num_queues = epair_tasks.tasks; 55298c230c8SBjoern A. Zeeb if (scb->ifp == NULL) { 55398c230c8SBjoern A. Zeeb free(scb, M_EPAIR); 55498c230c8SBjoern A. Zeeb if_free(sca->ifp); 55598c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 55698c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 55798c230c8SBjoern A. Zeeb return (ENOSPC); 55898c230c8SBjoern A. Zeeb } 559*24f0bfbaSKristof Provost scb->queues = mallocarray(scb->num_queues, sizeof(struct epair_queue), 560*24f0bfbaSKristof Provost M_EPAIR, M_WAITOK); 561*24f0bfbaSKristof Provost for (int i = 0; i < scb->num_queues; i++) { 562*24f0bfbaSKristof Provost struct epair_queue *q = &scb->queues[i]; 563*24f0bfbaSKristof Provost q->id = i; 564*24f0bfbaSKristof Provost q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 565*24f0bfbaSKristof Provost q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 566*24f0bfbaSKristof Provost q->ridx = 0; 567*24f0bfbaSKristof Provost q->sc = scb; 568*24f0bfbaSKristof Provost NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); 569*24f0bfbaSKristof Provost } 57098c230c8SBjoern A. Zeeb 57198c230c8SBjoern A. Zeeb /* 57298c230c8SBjoern A. Zeeb * Cross-reference the interfaces so we will be able to free both. 57398c230c8SBjoern A. Zeeb */ 57498c230c8SBjoern A. Zeeb sca->oifp = scb->ifp; 57598c230c8SBjoern A. Zeeb scb->oifp = sca->ifp; 57698c230c8SBjoern A. Zeeb 5773dd5760aSBjoern A. Zeeb EPAIR_LOCK(); 5783dd5760aSBjoern A. Zeeb #ifdef SMP 5793dd5760aSBjoern A. Zeeb /* Get an approximate distribution. */ 5803dd5760aSBjoern A. Zeeb hash = next_index % mp_ncpus; 5813dd5760aSBjoern A. Zeeb #else 5823dd5760aSBjoern A. Zeeb hash = 0; 5833dd5760aSBjoern A. Zeeb #endif 5843dd5760aSBjoern A. Zeeb EPAIR_UNLOCK(); 585d0ea4743SBjoern A. Zeeb 58618e199adSHiroki Sato /* Initialise pseudo media types. */ 58718e199adSHiroki Sato ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status); 58818e199adSHiroki Sato ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL); 58918e199adSHiroki Sato ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T); 59018e199adSHiroki Sato ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status); 59118e199adSHiroki Sato ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL); 59218e199adSHiroki Sato ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T); 59318e199adSHiroki Sato 59498c230c8SBjoern A. Zeeb /* Finish initialization of interface <n>a. */ 59598c230c8SBjoern A. Zeeb ifp = sca->ifp; 59698c230c8SBjoern A. Zeeb ifp->if_softc = sca; 59798c230c8SBjoern A. Zeeb strlcpy(ifp->if_xname, name, IFNAMSIZ); 59842a58907SGleb Smirnoff ifp->if_dname = epairname; 59998c230c8SBjoern A. Zeeb ifp->if_dunit = unit; 60098c230c8SBjoern A. Zeeb ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 6013dd5760aSBjoern A. Zeeb ifp->if_flags |= IFF_KNOWSEPOCH; 6029f8cab7fSMarko Zec ifp->if_capabilities = IFCAP_VLAN_MTU; 6039f8cab7fSMarko Zec ifp->if_capenable = IFCAP_VLAN_MTU; 60498c230c8SBjoern A. Zeeb ifp->if_start = epair_start; 60598c230c8SBjoern A. Zeeb ifp->if_ioctl = epair_ioctl; 60698c230c8SBjoern A. Zeeb ifp->if_init = epair_init; 6074e950412SErmal Luçi if_setsendqlen(ifp, ifqmaxlen); 6084e950412SErmal Luçi if_setsendqready(ifp); 60911d41666SLuca Pizzamiglio 61011d41666SLuca Pizzamiglio /* 61111d41666SLuca Pizzamiglio * Calculate the etheraddr hashing the hostid and the 612804771f5SEugene Grosbein * interface index. The result would be hopefully unique. 613804771f5SEugene Grosbein * Note that the "a" component of an epair instance may get moved 614804771f5SEugene Grosbein * to a different VNET after creation. In that case its index 615804771f5SEugene Grosbein * will be freed and the index can get reused by new epair instance. 616804771f5SEugene Grosbein * Make sure we do not create same etheraddr again. 61711d41666SLuca Pizzamiglio */ 61811d41666SLuca Pizzamiglio getcredhostid(curthread->td_ucred, (unsigned long *)&hostid); 61911d41666SLuca Pizzamiglio if (hostid == 0) 62011d41666SLuca Pizzamiglio arc4rand(&hostid, sizeof(hostid), 0); 621804771f5SEugene Grosbein 6223dd5760aSBjoern A. Zeeb EPAIR_LOCK(); 623804771f5SEugene Grosbein if (ifp->if_index > next_index) 624804771f5SEugene Grosbein next_index = ifp->if_index; 625804771f5SEugene Grosbein else 626804771f5SEugene Grosbein next_index++; 627804771f5SEugene Grosbein 628804771f5SEugene Grosbein key[0] = (uint32_t)next_index; 6293dd5760aSBjoern A. Zeeb EPAIR_UNLOCK(); 63011d41666SLuca Pizzamiglio key[1] = (uint32_t)(hostid & 0xffffffff); 63111d41666SLuca Pizzamiglio key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff); 63211d41666SLuca Pizzamiglio hash = jenkins_hash32(key, 3, 0); 63311d41666SLuca Pizzamiglio 63498c230c8SBjoern A. Zeeb eaddr[0] = 0x02; 63511d41666SLuca Pizzamiglio memcpy(&eaddr[1], &hash, 4); 63698c230c8SBjoern A. Zeeb eaddr[5] = 0x0a; 63798c230c8SBjoern A. Zeeb ether_ifattach(ifp, eaddr); 638b245f96cSGleb Smirnoff ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 6393dd5760aSBjoern A. Zeeb ifp->if_transmit = epair_transmit; 64098c230c8SBjoern A. Zeeb 64198c230c8SBjoern A. Zeeb /* Swap the name and finish initialization of interface <n>b. */ 64298c230c8SBjoern A. Zeeb *dp = 'b'; 64398c230c8SBjoern A. Zeeb 64498c230c8SBjoern A. Zeeb ifp = scb->ifp; 64598c230c8SBjoern A. Zeeb ifp->if_softc = scb; 64698c230c8SBjoern A. Zeeb strlcpy(ifp->if_xname, name, IFNAMSIZ); 64742a58907SGleb Smirnoff ifp->if_dname = epairname; 64898c230c8SBjoern A. Zeeb ifp->if_dunit = unit; 64998c230c8SBjoern A. Zeeb ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 65073d41cc7SZhenlei Huang ifp->if_flags |= IFF_KNOWSEPOCH; 6519f8cab7fSMarko Zec ifp->if_capabilities = IFCAP_VLAN_MTU; 6529f8cab7fSMarko Zec ifp->if_capenable = IFCAP_VLAN_MTU; 65398c230c8SBjoern A. Zeeb ifp->if_start = epair_start; 65498c230c8SBjoern A. Zeeb ifp->if_ioctl = epair_ioctl; 65598c230c8SBjoern A. Zeeb ifp->if_init = epair_init; 6564e950412SErmal Luçi if_setsendqlen(ifp, ifqmaxlen); 6574e950412SErmal Luçi if_setsendqready(ifp); 65898c230c8SBjoern A. Zeeb /* We need to play some tricks here for the second interface. */ 65942a58907SGleb Smirnoff strlcpy(name, epairname, len); 660b02fd8b7SKristof Provost 661b02fd8b7SKristof Provost /* Correctly set the name for the cloner list. */ 662b02fd8b7SKristof Provost strlcpy(name, scb->ifp->if_xname, len); 663b02fd8b7SKristof Provost epair_clone_add(ifc, scb); 664b02fd8b7SKristof Provost 665b245f96cSGleb Smirnoff ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 6663dd5760aSBjoern A. Zeeb ifp->if_transmit = epair_transmit; 66798c230c8SBjoern A. Zeeb 66898c230c8SBjoern A. Zeeb /* 66998c230c8SBjoern A. Zeeb * Restore name to <n>a as the ifp for this will go into the 67098c230c8SBjoern A. Zeeb * cloner list for the initial call. 67198c230c8SBjoern A. Zeeb */ 67298c230c8SBjoern A. Zeeb strlcpy(name, sca->ifp->if_xname, len); 67398c230c8SBjoern A. Zeeb 67498c230c8SBjoern A. Zeeb /* Tell the world, that we are ready to rock. */ 67598c230c8SBjoern A. Zeeb sca->ifp->if_drv_flags |= IFF_DRV_RUNNING; 676c7493539SBjoern A. Zeeb if_link_state_change(sca->ifp, LINK_STATE_UP); 6773dd5760aSBjoern A. Zeeb scb->ifp->if_drv_flags |= IFF_DRV_RUNNING; 678c7493539SBjoern A. Zeeb if_link_state_change(scb->ifp, LINK_STATE_UP); 67998c230c8SBjoern A. Zeeb 68098c230c8SBjoern A. Zeeb return (0); 68198c230c8SBjoern A. Zeeb } 68298c230c8SBjoern A. Zeeb 6833dd5760aSBjoern A. Zeeb static void 6843dd5760aSBjoern A. Zeeb epair_drain_rings(struct epair_softc *sc) 6853dd5760aSBjoern A. Zeeb { 6863dd5760aSBjoern A. Zeeb int ridx; 6873dd5760aSBjoern A. Zeeb struct mbuf *m; 6883dd5760aSBjoern A. Zeeb 6893dd5760aSBjoern A. Zeeb for (ridx = 0; ridx < 2; ridx++) { 690*24f0bfbaSKristof Provost for (int i = 0; i < sc->num_queues; i++) { 691*24f0bfbaSKristof Provost struct epair_queue *q = &sc->queues[i]; 6923dd5760aSBjoern A. Zeeb do { 693*24f0bfbaSKristof Provost m = buf_ring_dequeue_sc(q->rxring[ridx]); 6943dd5760aSBjoern A. Zeeb if (m == NULL) 6953dd5760aSBjoern A. Zeeb break; 6963dd5760aSBjoern A. Zeeb m_freem(m); 6973dd5760aSBjoern A. Zeeb } while (1); 6983dd5760aSBjoern A. Zeeb } 6993dd5760aSBjoern A. Zeeb } 700*24f0bfbaSKristof Provost } 7013dd5760aSBjoern A. Zeeb 70298c230c8SBjoern A. Zeeb static int 70398c230c8SBjoern A. Zeeb epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 70498c230c8SBjoern A. Zeeb { 70598c230c8SBjoern A. Zeeb struct ifnet *oifp; 70698c230c8SBjoern A. Zeeb struct epair_softc *sca, *scb; 70798c230c8SBjoern A. Zeeb int unit, error; 70898c230c8SBjoern A. Zeeb 70998c230c8SBjoern A. Zeeb /* 71098c230c8SBjoern A. Zeeb * In case we called into if_clone_destroyif() ourselves 71198c230c8SBjoern A. Zeeb * again to remove the second interface, the softc will be 71298c230c8SBjoern A. Zeeb * NULL. In that case so not do anything but return success. 71398c230c8SBjoern A. Zeeb */ 71498c230c8SBjoern A. Zeeb if (ifp->if_softc == NULL) 71598c230c8SBjoern A. Zeeb return (0); 71698c230c8SBjoern A. Zeeb 71798c230c8SBjoern A. Zeeb unit = ifp->if_dunit; 71898c230c8SBjoern A. Zeeb sca = ifp->if_softc; 71998c230c8SBjoern A. Zeeb oifp = sca->oifp; 72098c230c8SBjoern A. Zeeb scb = oifp->if_softc; 72198c230c8SBjoern A. Zeeb 7223dd5760aSBjoern A. Zeeb /* Frist get the interfaces down and detached. */ 723c7493539SBjoern A. Zeeb if_link_state_change(ifp, LINK_STATE_DOWN); 72498c230c8SBjoern A. Zeeb ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 7253dd5760aSBjoern A. Zeeb if_link_state_change(oifp, LINK_STATE_DOWN); 72698c230c8SBjoern A. Zeeb oifp->if_drv_flags &= ~IFF_DRV_RUNNING; 72798c230c8SBjoern A. Zeeb 7283dd5760aSBjoern A. Zeeb ether_ifdetach(ifp); 7297edc3d88SMikolaj Golub ether_ifdetach(oifp); 7303dd5760aSBjoern A. Zeeb 7313dd5760aSBjoern A. Zeeb /* Third free any queued packets and all the resources. */ 7323dd5760aSBjoern A. Zeeb CURVNET_SET_QUIET(oifp->if_vnet); 7333dd5760aSBjoern A. Zeeb epair_drain_rings(scb); 73498c230c8SBjoern A. Zeeb oifp->if_softc = NULL; 73598c230c8SBjoern A. Zeeb error = if_clone_destroyif(ifc, oifp); 73698c230c8SBjoern A. Zeeb if (error) 73798c230c8SBjoern A. Zeeb panic("%s: if_clone_destroyif() for our 2nd iface failed: %d", 73898c230c8SBjoern A. Zeeb __func__, error); 73973e39d61SBjoern A. Zeeb if_free(oifp); 7402dccdd45SMarko Zec ifmedia_removeall(&scb->media); 741*24f0bfbaSKristof Provost for (int i = 0; i < scb->num_queues; i++) { 742*24f0bfbaSKristof Provost struct epair_queue *q = &scb->queues[i]; 743*24f0bfbaSKristof Provost buf_ring_free(q->rxring[0], M_EPAIR); 744*24f0bfbaSKristof Provost buf_ring_free(q->rxring[1], M_EPAIR); 745*24f0bfbaSKristof Provost } 746*24f0bfbaSKristof Provost free(scb->queues, M_EPAIR); 74798c230c8SBjoern A. Zeeb free(scb, M_EPAIR); 7487edc3d88SMikolaj Golub CURVNET_RESTORE(); 7497edc3d88SMikolaj Golub 7503dd5760aSBjoern A. Zeeb epair_drain_rings(sca); 7517edc3d88SMikolaj Golub if_free(ifp); 7527edc3d88SMikolaj Golub ifmedia_removeall(&sca->media); 753*24f0bfbaSKristof Provost for (int i = 0; i < sca->num_queues; i++) { 754*24f0bfbaSKristof Provost struct epair_queue *q = &sca->queues[i]; 755*24f0bfbaSKristof Provost buf_ring_free(q->rxring[0], M_EPAIR); 756*24f0bfbaSKristof Provost buf_ring_free(q->rxring[1], M_EPAIR); 757*24f0bfbaSKristof Provost } 758*24f0bfbaSKristof Provost free(sca->queues, M_EPAIR); 75998c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 7603dd5760aSBjoern A. Zeeb 7613dd5760aSBjoern A. Zeeb /* Last free the cloner unit. */ 76298c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 76398c230c8SBjoern A. Zeeb 76498c230c8SBjoern A. Zeeb return (0); 76598c230c8SBjoern A. Zeeb } 76698c230c8SBjoern A. Zeeb 7673c3136b1SHiroki Sato static void 7683c3136b1SHiroki Sato vnet_epair_init(const void *unused __unused) 7693c3136b1SHiroki Sato { 7703c3136b1SHiroki Sato 7713c3136b1SHiroki Sato V_epair_cloner = if_clone_advanced(epairname, 0, 7723c3136b1SHiroki Sato epair_clone_match, epair_clone_create, epair_clone_destroy); 7733c3136b1SHiroki Sato } 77489856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 7753c3136b1SHiroki Sato vnet_epair_init, NULL); 7763c3136b1SHiroki Sato 7773c3136b1SHiroki Sato static void 7783c3136b1SHiroki Sato vnet_epair_uninit(const void *unused __unused) 7793c3136b1SHiroki Sato { 7803c3136b1SHiroki Sato 7813c3136b1SHiroki Sato if_clone_detach(V_epair_cloner); 7823c3136b1SHiroki Sato } 78389856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 7843c3136b1SHiroki Sato vnet_epair_uninit, NULL); 7853c3136b1SHiroki Sato 78698c230c8SBjoern A. Zeeb static int 787*24f0bfbaSKristof Provost epair_mod_init() 788*24f0bfbaSKristof Provost { 789*24f0bfbaSKristof Provost char name[32]; 790*24f0bfbaSKristof Provost epair_tasks.tasks = 0; 791*24f0bfbaSKristof Provost 792*24f0bfbaSKristof Provost #ifdef RSS 793*24f0bfbaSKristof Provost struct pcpu *pcpu; 794*24f0bfbaSKristof Provost int cpu; 795*24f0bfbaSKristof Provost 796*24f0bfbaSKristof Provost CPU_FOREACH(cpu) { 797*24f0bfbaSKristof Provost cpuset_t cpu_mask; 798*24f0bfbaSKristof Provost 799*24f0bfbaSKristof Provost /* Pin to this CPU so we get appropriate NUMA allocations. */ 800*24f0bfbaSKristof Provost pcpu = pcpu_find(cpu); 801*24f0bfbaSKristof Provost thread_lock(curthread); 802*24f0bfbaSKristof Provost sched_bind(curthread, cpu); 803*24f0bfbaSKristof Provost thread_unlock(curthread); 804*24f0bfbaSKristof Provost 805*24f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task_%d", cpu); 806*24f0bfbaSKristof Provost 807*24f0bfbaSKristof Provost epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK, 808*24f0bfbaSKristof Provost taskqueue_thread_enqueue, 809*24f0bfbaSKristof Provost &epair_tasks.tq[cpu]); 810*24f0bfbaSKristof Provost CPU_SETOF(cpu, &cpu_mask); 811*24f0bfbaSKristof Provost taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET, 812*24f0bfbaSKristof Provost &cpu_mask, "%s", name); 813*24f0bfbaSKristof Provost 814*24f0bfbaSKristof Provost epair_tasks.tasks++; 815*24f0bfbaSKristof Provost } 816*24f0bfbaSKristof Provost #else 817*24f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task"); 818*24f0bfbaSKristof Provost 819*24f0bfbaSKristof Provost epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK, 820*24f0bfbaSKristof Provost taskqueue_thread_enqueue, 821*24f0bfbaSKristof Provost &epair_tasks.tq[0]); 822*24f0bfbaSKristof Provost taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name); 823*24f0bfbaSKristof Provost 824*24f0bfbaSKristof Provost epair_tasks.tasks = 1; 825*24f0bfbaSKristof Provost #endif 826*24f0bfbaSKristof Provost 827*24f0bfbaSKristof Provost return (0); 828*24f0bfbaSKristof Provost } 829*24f0bfbaSKristof Provost 830*24f0bfbaSKristof Provost static void 831*24f0bfbaSKristof Provost epair_mod_cleanup() 832*24f0bfbaSKristof Provost { 833*24f0bfbaSKristof Provost 834*24f0bfbaSKristof Provost for (int i = 0; i < epair_tasks.tasks; i++) { 835*24f0bfbaSKristof Provost taskqueue_drain_all(epair_tasks.tq[i]); 836*24f0bfbaSKristof Provost taskqueue_free(epair_tasks.tq[i]); 837*24f0bfbaSKristof Provost } 838*24f0bfbaSKristof Provost } 839*24f0bfbaSKristof Provost 840*24f0bfbaSKristof Provost static int 84198c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data) 84298c230c8SBjoern A. Zeeb { 843*24f0bfbaSKristof Provost int ret; 84498c230c8SBjoern A. Zeeb 84598c230c8SBjoern A. Zeeb switch (type) { 84698c230c8SBjoern A. Zeeb case MOD_LOAD: 8473dd5760aSBjoern A. Zeeb EPAIR_LOCK_INIT(); 848*24f0bfbaSKristof Provost ret = epair_mod_init(); 849*24f0bfbaSKristof Provost if (ret != 0) 850*24f0bfbaSKristof Provost return (ret); 85198c230c8SBjoern A. Zeeb if (bootverbose) 8523dd5760aSBjoern A. Zeeb printf("%s: %s initialized.\n", __func__, epairname); 85398c230c8SBjoern A. Zeeb break; 85498c230c8SBjoern A. Zeeb case MOD_UNLOAD: 855*24f0bfbaSKristof Provost epair_mod_cleanup(); 8563dd5760aSBjoern A. Zeeb EPAIR_LOCK_DESTROY(); 8573dd5760aSBjoern A. Zeeb if (bootverbose) 8583dd5760aSBjoern A. Zeeb printf("%s: %s unloaded.\n", __func__, epairname); 85998c230c8SBjoern A. Zeeb break; 86098c230c8SBjoern A. Zeeb default: 86198c230c8SBjoern A. Zeeb return (EOPNOTSUPP); 86298c230c8SBjoern A. Zeeb } 86398c230c8SBjoern A. Zeeb return (0); 86498c230c8SBjoern A. Zeeb } 86598c230c8SBjoern A. Zeeb 86698c230c8SBjoern A. Zeeb static moduledata_t epair_mod = { 86798c230c8SBjoern A. Zeeb "if_epair", 86898c230c8SBjoern A. Zeeb epair_modevent, 8699823d527SKevin Lo 0 87098c230c8SBjoern A. Zeeb }; 87198c230c8SBjoern A. Zeeb 87289856f7eSBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 8733dd5760aSBjoern A. Zeeb MODULE_VERSION(if_epair, 3); 874