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 4324f0bfbaSKristof Provost #include "opt_rss.h" 44*52bcdc5bSSantiago Martinez #include "opt_inet.h" 45*52bcdc5bSSantiago Martinez #include "opt_inet6.h" 4624f0bfbaSKristof Provost 4798c230c8SBjoern A. Zeeb #include <sys/param.h> 4811d41666SLuca Pizzamiglio #include <sys/hash.h> 4911d41666SLuca Pizzamiglio #include <sys/jail.h> 5098c230c8SBjoern A. Zeeb #include <sys/kernel.h> 5111d41666SLuca Pizzamiglio #include <sys/libkern.h> 528ec07310SGleb Smirnoff #include <sys/malloc.h> 5398c230c8SBjoern A. Zeeb #include <sys/mbuf.h> 5498c230c8SBjoern A. Zeeb #include <sys/module.h> 5511d41666SLuca Pizzamiglio #include <sys/proc.h> 5698c230c8SBjoern A. Zeeb #include <sys/queue.h> 5724f0bfbaSKristof Provost #include <sys/sched.h> 58d0ea4743SBjoern A. Zeeb #include <sys/smp.h> 5998c230c8SBjoern A. Zeeb #include <sys/socket.h> 6098c230c8SBjoern A. Zeeb #include <sys/sockio.h> 6124f0bfbaSKristof Provost #include <sys/taskqueue.h> 6298c230c8SBjoern A. Zeeb #include <sys/types.h> 633dd5760aSBjoern A. Zeeb #include <sys/buf_ring.h> 643dd5760aSBjoern A. Zeeb #include <sys/bus.h> 653dd5760aSBjoern A. Zeeb #include <sys/interrupt.h> 6698c230c8SBjoern A. Zeeb 6798c230c8SBjoern A. Zeeb #include <net/bpf.h> 6898c230c8SBjoern A. Zeeb #include <net/ethernet.h> 6998c230c8SBjoern A. Zeeb #include <net/if.h> 7076039bc8SGleb Smirnoff #include <net/if_var.h> 7198c230c8SBjoern A. Zeeb #include <net/if_clone.h> 722dccdd45SMarko Zec #include <net/if_media.h> 7398c230c8SBjoern A. Zeeb #include <net/if_var.h> 7498c230c8SBjoern A. Zeeb #include <net/if_types.h> 7598c230c8SBjoern A. Zeeb #include <net/netisr.h> 7624f0bfbaSKristof Provost #ifdef RSS 7724f0bfbaSKristof Provost #include <net/rss_config.h> 78*52bcdc5bSSantiago Martinez #ifdef INET 7924f0bfbaSKristof Provost #include <netinet/in_rss.h> 80*52bcdc5bSSantiago Martinez #endif 81*52bcdc5bSSantiago Martinez #ifdef INET6 8224f0bfbaSKristof Provost #include <netinet6/in6_rss.h> 8324f0bfbaSKristof Provost #endif 84*52bcdc5bSSantiago Martinez #endif 85530c0060SRobert Watson #include <net/vnet.h> 8698c230c8SBjoern A. Zeeb 8798c230c8SBjoern A. Zeeb static int epair_clone_match(struct if_clone *, const char *); 8898c230c8SBjoern A. Zeeb static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t); 8998c230c8SBjoern A. Zeeb static int epair_clone_destroy(struct if_clone *, struct ifnet *); 9098c230c8SBjoern A. Zeeb 9142a58907SGleb Smirnoff static const char epairname[] = "epair"; 923dd5760aSBjoern A. Zeeb #define RXRSIZE 4096 /* Probably overkill by 4-8x. */ 93d0ea4743SBjoern A. Zeeb 9442a58907SGleb Smirnoff static MALLOC_DEFINE(M_EPAIR, epairname, 95d0ea4743SBjoern A. Zeeb "Pair of virtual cross-over connected Ethernet-like interfaces"); 9698c230c8SBjoern A. Zeeb 975f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, epair_cloner); 983c3136b1SHiroki Sato #define V_epair_cloner VNET(epair_cloner) 9998c230c8SBjoern A. Zeeb 1003dd5760aSBjoern A. Zeeb static unsigned int next_index = 0; 1013dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \ 1023dd5760aSBjoern A. Zeeb NULL, MTX_DEF) 1033dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx) 1043dd5760aSBjoern A. Zeeb #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx) 1053dd5760aSBjoern A. Zeeb #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx) 1063dd5760aSBjoern A. Zeeb 10724f0bfbaSKristof Provost struct epair_softc; 10824f0bfbaSKristof Provost struct epair_queue { 10924f0bfbaSKristof Provost int id; 11024f0bfbaSKristof Provost struct buf_ring *rxring[2]; 11124f0bfbaSKristof Provost volatile int ridx; /* 0 || 1 */ 11224f0bfbaSKristof Provost struct task tx_task; 11324f0bfbaSKristof Provost struct epair_softc *sc; 11424f0bfbaSKristof Provost }; 1153dd5760aSBjoern A. Zeeb 1163dd5760aSBjoern A. Zeeb static struct mtx epair_n_index_mtx; 1173dd5760aSBjoern A. Zeeb struct epair_softc { 1183dd5760aSBjoern A. Zeeb struct ifnet *ifp; /* This ifp. */ 1193dd5760aSBjoern A. Zeeb struct ifnet *oifp; /* other ifp of pair. */ 12024f0bfbaSKristof Provost int num_queues; 12124f0bfbaSKristof Provost struct epair_queue *queues; 1223dd5760aSBjoern A. Zeeb struct ifmedia media; /* Media config (fake). */ 1233dd5760aSBjoern A. Zeeb STAILQ_ENTRY(epair_softc) entry; 124d0ea4743SBjoern A. Zeeb }; 125d0ea4743SBjoern A. Zeeb 12624f0bfbaSKristof Provost struct epair_tasks_t { 12724f0bfbaSKristof Provost int tasks; 12824f0bfbaSKristof Provost struct taskqueue *tq[MAXCPU]; 12924f0bfbaSKristof Provost }; 13024f0bfbaSKristof Provost 13124f0bfbaSKristof Provost static struct epair_tasks_t epair_tasks; 13224f0bfbaSKristof Provost 133d0ea4743SBjoern A. Zeeb static void 13462d2dcafSKristof Provost epair_clear_mbuf(struct mbuf *m) 13562d2dcafSKristof Provost { 13662d2dcafSKristof Provost /* Remove any CSUM_SND_TAG as ether_input will barf. */ 13762d2dcafSKristof Provost if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) { 13862d2dcafSKristof Provost m_snd_tag_rele(m->m_pkthdr.snd_tag); 13962d2dcafSKristof Provost m->m_pkthdr.snd_tag = NULL; 14062d2dcafSKristof Provost m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG; 14162d2dcafSKristof Provost } 14262d2dcafSKristof Provost 14362d2dcafSKristof Provost m_tag_delete_nonpersistent(m); 14462d2dcafSKristof Provost } 14562d2dcafSKristof Provost 14662d2dcafSKristof Provost static void 14724f0bfbaSKristof Provost epair_if_input(struct epair_softc *sc, struct epair_queue *q, int ridx) 148d0ea4743SBjoern A. Zeeb { 1493dd5760aSBjoern A. Zeeb struct ifnet *ifp; 1503dd5760aSBjoern A. Zeeb struct mbuf *m; 151d0ea4743SBjoern A. Zeeb 1523dd5760aSBjoern A. Zeeb ifp = sc->ifp; 15324f0bfbaSKristof Provost CURVNET_SET(ifp->if_vnet); 15424f0bfbaSKristof Provost while (! buf_ring_empty(q->rxring[ridx])) { 15524f0bfbaSKristof Provost m = buf_ring_dequeue_mc(q->rxring[ridx]); 1563dd5760aSBjoern A. Zeeb if (m == NULL) 15724f0bfbaSKristof Provost continue; 158d0ea4743SBjoern A. Zeeb 1593dd5760aSBjoern A. Zeeb MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 1603dd5760aSBjoern A. Zeeb (*ifp->if_input)(ifp, m); 161d0ea4743SBjoern A. Zeeb 16224f0bfbaSKristof Provost } 16324f0bfbaSKristof Provost CURVNET_RESTORE(); 164d0ea4743SBjoern A. Zeeb } 165d0ea4743SBjoern A. Zeeb 166d0ea4743SBjoern A. Zeeb static void 16724f0bfbaSKristof Provost epair_tx_start_deferred(void *arg, int pending) 168d0ea4743SBjoern A. Zeeb { 16924f0bfbaSKristof Provost struct epair_queue *q = (struct epair_queue *)arg; 17024f0bfbaSKristof Provost struct epair_softc *sc = q->sc; 1713dd5760aSBjoern A. Zeeb int ridx, nidx; 172d0ea4743SBjoern A. Zeeb 1733dd5760aSBjoern A. Zeeb if_ref(sc->ifp); 17424f0bfbaSKristof Provost ridx = atomic_load_int(&q->ridx); 1753dd5760aSBjoern A. Zeeb do { 1763dd5760aSBjoern A. Zeeb nidx = (ridx == 0) ? 1 : 0; 17724f0bfbaSKristof Provost } while (!atomic_fcmpset_int(&q->ridx, &ridx, nidx)); 17824f0bfbaSKristof Provost epair_if_input(sc, q, ridx); 17924f0bfbaSKristof Provost 18024f0bfbaSKristof Provost if (! buf_ring_empty(q->rxring[nidx])) 18124f0bfbaSKristof Provost taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); 182d0ea4743SBjoern A. Zeeb 1833dd5760aSBjoern A. Zeeb if_rele(sc->ifp); 184d0ea4743SBjoern A. Zeeb } 185d0ea4743SBjoern A. Zeeb 186d0ea4743SBjoern A. Zeeb static int 1873dd5760aSBjoern A. Zeeb epair_menq(struct mbuf *m, struct epair_softc *osc) 188d0ea4743SBjoern A. Zeeb { 1893dd5760aSBjoern A. Zeeb struct ifnet *ifp, *oifp; 1903dd5760aSBjoern A. Zeeb int len, ret; 1913dd5760aSBjoern A. Zeeb int ridx; 1923dd5760aSBjoern A. Zeeb short mflags; 19324f0bfbaSKristof Provost struct epair_queue *q = NULL; 19424f0bfbaSKristof Provost uint32_t bucket; 1953dd5760aSBjoern A. Zeeb bool was_empty; 19624f0bfbaSKristof Provost #ifdef RSS 19724f0bfbaSKristof Provost struct ether_header *eh; 19824f0bfbaSKristof Provost #endif 199d0ea4743SBjoern A. Zeeb 2003dd5760aSBjoern A. Zeeb /* 2013dd5760aSBjoern A. Zeeb * I know this looks weird. We pass the "other sc" as we need that one 2023dd5760aSBjoern A. Zeeb * and can get both ifps from it as well. 2033dd5760aSBjoern A. Zeeb */ 2043dd5760aSBjoern A. Zeeb oifp = osc->ifp; 2053dd5760aSBjoern A. Zeeb ifp = osc->oifp; 2063dd5760aSBjoern A. Zeeb 2073dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 2083dd5760aSBjoern A. Zeeb epair_clear_mbuf(m); 2093dd5760aSBjoern A. Zeeb if_setrcvif(m, oifp); 2103dd5760aSBjoern A. Zeeb M_SETFIB(m, oifp->if_fib); 2113dd5760aSBjoern A. Zeeb 2123dd5760aSBjoern A. Zeeb /* Save values as once the mbuf is queued, it's not ours anymore. */ 2133dd5760aSBjoern A. Zeeb len = m->m_pkthdr.len; 2143dd5760aSBjoern A. Zeeb mflags = m->m_flags; 2153dd5760aSBjoern A. Zeeb 2163dd5760aSBjoern A. Zeeb MPASS(m->m_nextpkt == NULL); 2173dd5760aSBjoern A. Zeeb MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 2183dd5760aSBjoern A. Zeeb 21924f0bfbaSKristof Provost #ifdef RSS 22024f0bfbaSKristof Provost ret = rss_m2bucket(m, &bucket); 22124f0bfbaSKristof Provost if (ret) { 22224f0bfbaSKristof Provost /* Actually hash the packet. */ 22324f0bfbaSKristof Provost eh = mtod(m, struct ether_header *); 22424f0bfbaSKristof Provost 22524f0bfbaSKristof Provost switch (ntohs(eh->ether_type)) { 226*52bcdc5bSSantiago Martinez #ifdef INET 22724f0bfbaSKristof Provost case ETHERTYPE_IP: 22824f0bfbaSKristof Provost rss_soft_m2cpuid_v4(m, 0, &bucket); 22924f0bfbaSKristof Provost break; 230*52bcdc5bSSantiago Martinez #endif 231*52bcdc5bSSantiago Martinez #ifdef INET6 23224f0bfbaSKristof Provost case ETHERTYPE_IPV6: 23324f0bfbaSKristof Provost rss_soft_m2cpuid_v6(m, 0, &bucket); 23424f0bfbaSKristof Provost break; 235*52bcdc5bSSantiago Martinez #endif 23624f0bfbaSKristof Provost default: 23724f0bfbaSKristof Provost bucket = 0; 23824f0bfbaSKristof Provost break; 23924f0bfbaSKristof Provost } 24024f0bfbaSKristof Provost } 24124f0bfbaSKristof Provost bucket %= osc->num_queues; 24224f0bfbaSKristof Provost #else 24324f0bfbaSKristof Provost bucket = 0; 24424f0bfbaSKristof Provost #endif 24524f0bfbaSKristof Provost q = &osc->queues[bucket]; 24624f0bfbaSKristof Provost 24724f0bfbaSKristof Provost ridx = atomic_load_int(&q->ridx); 24824f0bfbaSKristof Provost was_empty = buf_ring_empty(q->rxring[ridx]); 24924f0bfbaSKristof Provost ret = buf_ring_enqueue(q->rxring[ridx], m); 2503dd5760aSBjoern A. Zeeb if (ret != 0) { 2513dd5760aSBjoern A. Zeeb /* Ring is full. */ 25224f0bfbaSKristof Provost if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 2533dd5760aSBjoern A. Zeeb m_freem(m); 25424f0bfbaSKristof Provost goto done; 2553dd5760aSBjoern A. Zeeb } 256d0ea4743SBjoern A. Zeeb 2573dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2583dd5760aSBjoern A. Zeeb /* 2593dd5760aSBjoern A. Zeeb * IFQ_HANDOFF_ADJ/ip_handoff() update statistics, 2603dd5760aSBjoern A. Zeeb * but as we bypass all this we have to duplicate 2613dd5760aSBjoern A. Zeeb * the logic another time. 2623dd5760aSBjoern A. Zeeb */ 2633dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 2643dd5760aSBjoern A. Zeeb if (mflags & (M_BCAST|M_MCAST)) 2653dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 2663dd5760aSBjoern A. Zeeb /* Someone else received the packet. */ 2673dd5760aSBjoern A. Zeeb if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1); 268d0ea4743SBjoern A. Zeeb 26924f0bfbaSKristof Provost done: 27024f0bfbaSKristof Provost if (was_empty) 27124f0bfbaSKristof Provost taskqueue_enqueue(epair_tasks.tq[bucket], &q->tx_task); 272d0ea4743SBjoern A. Zeeb 273d0ea4743SBjoern A. Zeeb return (0); 274d0ea4743SBjoern A. Zeeb } 275d0ea4743SBjoern A. Zeeb 27698c230c8SBjoern A. Zeeb static void 2773dd5760aSBjoern A. Zeeb epair_start(struct ifnet *ifp) 27898c230c8SBjoern A. Zeeb { 27998c230c8SBjoern A. Zeeb struct mbuf *m; 28098c230c8SBjoern A. Zeeb struct epair_softc *sc; 28198c230c8SBjoern A. Zeeb struct ifnet *oifp; 28298c230c8SBjoern A. Zeeb 28398c230c8SBjoern A. Zeeb /* 28459f35a82SPatrick Kelsey * We get packets here from ether_output via if_handoff() 28575580d58SPatrick Kelsey * and need to put them into the input queue of the oifp 2863dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the 2873dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair. 28898c230c8SBjoern A. Zeeb */ 2893dd5760aSBjoern A. Zeeb sc = ifp->if_softc; 29098c230c8SBjoern A. Zeeb oifp = sc->oifp; 29198c230c8SBjoern A. Zeeb sc = oifp->if_softc; 29298c230c8SBjoern A. Zeeb for (;;) { 29398c230c8SBjoern A. Zeeb IFQ_DEQUEUE(&ifp->if_snd, m); 29498c230c8SBjoern A. Zeeb if (m == NULL) 29598c230c8SBjoern A. Zeeb break; 2963dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 29798c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m); 29898c230c8SBjoern A. Zeeb 2993dd5760aSBjoern A. Zeeb /* In case either interface is not usable drop the packet. */ 3003dd5760aSBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 3013dd5760aSBjoern A. Zeeb (ifp->if_flags & IFF_UP) == 0 || 3023dd5760aSBjoern A. Zeeb (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 30398c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) { 30498c230c8SBjoern A. Zeeb m_freem(m); 30598c230c8SBjoern A. Zeeb continue; 30698c230c8SBjoern A. Zeeb } 30798c230c8SBjoern A. Zeeb 3083dd5760aSBjoern A. Zeeb (void) epair_menq(m, sc); 30998c230c8SBjoern A. Zeeb } 31098c230c8SBjoern A. Zeeb } 31198c230c8SBjoern A. Zeeb 31298c230c8SBjoern A. Zeeb static int 3133dd5760aSBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m) 31498c230c8SBjoern A. Zeeb { 31598c230c8SBjoern A. Zeeb struct epair_softc *sc; 31698c230c8SBjoern A. Zeeb struct ifnet *oifp; 3172cedfc3fSMateusz Guzik int error; 3182cedfc3fSMateusz Guzik #ifdef ALTQ 3192cedfc3fSMateusz Guzik int len; 32098c230c8SBjoern A. Zeeb short mflags; 3212cedfc3fSMateusz Guzik #endif 32298c230c8SBjoern A. Zeeb 32398c230c8SBjoern A. Zeeb if (m == NULL) 32498c230c8SBjoern A. Zeeb return (0); 3253dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m); 32698c230c8SBjoern A. Zeeb 32798c230c8SBjoern A. Zeeb /* 32898c230c8SBjoern A. Zeeb * We are not going to use the interface en/dequeue mechanism 32998c230c8SBjoern A. Zeeb * on the TX side. We are called from ether_output_frame() 3303dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the 3313dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair. 33298c230c8SBjoern A. Zeeb */ 33398c230c8SBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 33498c230c8SBjoern A. Zeeb m_freem(m); 3353dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 33698c230c8SBjoern A. Zeeb return (ENXIO); 33798c230c8SBjoern A. Zeeb } 33898c230c8SBjoern A. Zeeb if ((ifp->if_flags & IFF_UP) == 0) { 33998c230c8SBjoern A. Zeeb m_freem(m); 3403dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 34198c230c8SBjoern A. Zeeb return (ENETDOWN); 34298c230c8SBjoern A. Zeeb } 34398c230c8SBjoern A. Zeeb 34498c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m); 34598c230c8SBjoern A. Zeeb 34698c230c8SBjoern A. Zeeb /* 34798c230c8SBjoern A. Zeeb * In case the outgoing interface is not usable, 34898c230c8SBjoern A. Zeeb * drop the packet. 34998c230c8SBjoern A. Zeeb */ 3503dd5760aSBjoern A. Zeeb sc = ifp->if_softc; 35198c230c8SBjoern A. Zeeb oifp = sc->oifp; 35298c230c8SBjoern A. Zeeb if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 35398c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) { 3543751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 35598c230c8SBjoern A. Zeeb m_freem(m); 35698c230c8SBjoern A. Zeeb return (0); 35798c230c8SBjoern A. Zeeb } 3582cedfc3fSMateusz Guzik 3592cedfc3fSMateusz Guzik #ifdef ALTQ 36098c230c8SBjoern A. Zeeb len = m->m_pkthdr.len; 36198c230c8SBjoern A. Zeeb mflags = m->m_flags; 36298c230c8SBjoern A. Zeeb 363a4641f4eSPedro F. Giffuni /* Support ALTQ via the classic if_start() path. */ 36498c230c8SBjoern A. Zeeb IF_LOCK(&ifp->if_snd); 36598c230c8SBjoern A. Zeeb if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 36698c230c8SBjoern A. Zeeb ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 36798c230c8SBjoern A. Zeeb if (error) 3683751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 36998c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd); 37098c230c8SBjoern A. Zeeb if (!error) { 3713751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 37298c230c8SBjoern A. Zeeb if (mflags & (M_BCAST|M_MCAST)) 3733751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 3743dd5760aSBjoern A. Zeeb epair_start(ifp); 37598c230c8SBjoern A. Zeeb } 37698c230c8SBjoern A. Zeeb return (error); 37798c230c8SBjoern A. Zeeb } 37898c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd); 37998c230c8SBjoern A. Zeeb #endif 38098c230c8SBjoern A. Zeeb 3813dd5760aSBjoern A. Zeeb error = epair_menq(m, oifp->if_softc); 38298c230c8SBjoern A. Zeeb return (error); 38398c230c8SBjoern A. Zeeb } 38462d2dcafSKristof Provost 38598c230c8SBjoern A. Zeeb static int 3862dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused) 3872dccdd45SMarko Zec { 3882dccdd45SMarko Zec 3892dccdd45SMarko Zec /* Do nothing. */ 3902dccdd45SMarko Zec return (0); 3912dccdd45SMarko Zec } 3922dccdd45SMarko Zec 3932dccdd45SMarko Zec static void 3942dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr) 3952dccdd45SMarko Zec { 3962dccdd45SMarko Zec 3972dccdd45SMarko Zec imr->ifm_status = IFM_AVALID | IFM_ACTIVE; 3982dccdd45SMarko Zec imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX; 3992dccdd45SMarko Zec } 4002dccdd45SMarko Zec 4012dccdd45SMarko Zec static int 40298c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 40398c230c8SBjoern A. Zeeb { 4042dccdd45SMarko Zec struct epair_softc *sc; 40598c230c8SBjoern A. Zeeb struct ifreq *ifr; 40698c230c8SBjoern A. Zeeb int error; 40798c230c8SBjoern A. Zeeb 40898c230c8SBjoern A. Zeeb ifr = (struct ifreq *)data; 40998c230c8SBjoern A. Zeeb switch (cmd) { 41098c230c8SBjoern A. Zeeb case SIOCSIFFLAGS: 41198c230c8SBjoern A. Zeeb case SIOCADDMULTI: 41298c230c8SBjoern A. Zeeb case SIOCDELMULTI: 41398c230c8SBjoern A. Zeeb error = 0; 41498c230c8SBjoern A. Zeeb break; 41598c230c8SBjoern A. Zeeb 4162dccdd45SMarko Zec case SIOCSIFMEDIA: 4172dccdd45SMarko Zec case SIOCGIFMEDIA: 4182dccdd45SMarko Zec sc = ifp->if_softc; 4192dccdd45SMarko Zec error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd); 4202dccdd45SMarko Zec break; 4212dccdd45SMarko Zec 422d0ea4743SBjoern A. Zeeb case SIOCSIFMTU: 423d0ea4743SBjoern A. Zeeb /* We basically allow all kinds of MTUs. */ 424d0ea4743SBjoern A. Zeeb ifp->if_mtu = ifr->ifr_mtu; 425d0ea4743SBjoern A. Zeeb error = 0; 426d0ea4743SBjoern A. Zeeb break; 427d0ea4743SBjoern A. Zeeb 42898c230c8SBjoern A. Zeeb default: 42998c230c8SBjoern A. Zeeb /* Let the common ethernet handler process this. */ 43098c230c8SBjoern A. Zeeb error = ether_ioctl(ifp, cmd, data); 43198c230c8SBjoern A. Zeeb break; 43298c230c8SBjoern A. Zeeb } 43398c230c8SBjoern A. Zeeb 43498c230c8SBjoern A. Zeeb return (error); 43598c230c8SBjoern A. Zeeb } 43698c230c8SBjoern A. Zeeb 43798c230c8SBjoern A. Zeeb static void 43898c230c8SBjoern A. Zeeb epair_init(void *dummy __unused) 43998c230c8SBjoern A. Zeeb { 44098c230c8SBjoern A. Zeeb } 44198c230c8SBjoern A. Zeeb 44298c230c8SBjoern A. Zeeb /* 44398c230c8SBjoern A. Zeeb * Interface cloning functions. 44498c230c8SBjoern A. Zeeb * We use our private ones so that we can create/destroy our secondary 44598c230c8SBjoern A. Zeeb * device along with the primary one. 44698c230c8SBjoern A. Zeeb */ 44798c230c8SBjoern A. Zeeb static int 44898c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name) 44998c230c8SBjoern A. Zeeb { 45098c230c8SBjoern A. Zeeb const char *cp; 45198c230c8SBjoern A. Zeeb 45298c230c8SBjoern A. Zeeb /* 45398c230c8SBjoern A. Zeeb * Our base name is epair. 45498c230c8SBjoern A. Zeeb * Our interfaces will be named epair<n>[ab]. 45598c230c8SBjoern A. Zeeb * So accept anything of the following list: 45698c230c8SBjoern A. Zeeb * - epair 45798c230c8SBjoern A. Zeeb * - epair<n> 45898c230c8SBjoern A. Zeeb * but not the epair<n>[ab] versions. 45998c230c8SBjoern A. Zeeb */ 46042a58907SGleb Smirnoff if (strncmp(epairname, name, sizeof(epairname)-1) != 0) 46198c230c8SBjoern A. Zeeb return (0); 46298c230c8SBjoern A. Zeeb 46342a58907SGleb Smirnoff for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) { 46498c230c8SBjoern A. Zeeb if (*cp < '0' || *cp > '9') 46598c230c8SBjoern A. Zeeb return (0); 46698c230c8SBjoern A. Zeeb } 46798c230c8SBjoern A. Zeeb 46898c230c8SBjoern A. Zeeb return (1); 46998c230c8SBjoern A. Zeeb } 47098c230c8SBjoern A. Zeeb 471b02fd8b7SKristof Provost static void 472b02fd8b7SKristof Provost epair_clone_add(struct if_clone *ifc, struct epair_softc *scb) 473b02fd8b7SKristof Provost { 474b02fd8b7SKristof Provost struct ifnet *ifp; 475b02fd8b7SKristof Provost uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 476b02fd8b7SKristof Provost 477b02fd8b7SKristof Provost ifp = scb->ifp; 478b02fd8b7SKristof Provost /* Copy epairNa etheraddr and change the last byte. */ 479b02fd8b7SKristof Provost memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN); 480b02fd8b7SKristof Provost eaddr[5] = 0x0b; 481b02fd8b7SKristof Provost ether_ifattach(ifp, eaddr); 482b02fd8b7SKristof Provost 483b02fd8b7SKristof Provost if_clone_addif(ifc, ifp); 484b02fd8b7SKristof Provost } 485b02fd8b7SKristof Provost 48698c230c8SBjoern A. Zeeb static int 48798c230c8SBjoern A. Zeeb epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 48898c230c8SBjoern A. Zeeb { 48998c230c8SBjoern A. Zeeb struct epair_softc *sca, *scb; 49098c230c8SBjoern A. Zeeb struct ifnet *ifp; 49198c230c8SBjoern A. Zeeb char *dp; 49298c230c8SBjoern A. Zeeb int error, unit, wildcard; 49311d41666SLuca Pizzamiglio uint64_t hostid; 49411d41666SLuca Pizzamiglio uint32_t key[3]; 49511d41666SLuca Pizzamiglio uint32_t hash; 49698c230c8SBjoern A. Zeeb uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 49798c230c8SBjoern A. Zeeb 49898c230c8SBjoern A. Zeeb /* Try to see if a special unit was requested. */ 49998c230c8SBjoern A. Zeeb error = ifc_name2unit(name, &unit); 50098c230c8SBjoern A. Zeeb if (error != 0) 50198c230c8SBjoern A. Zeeb return (error); 50298c230c8SBjoern A. Zeeb wildcard = (unit < 0); 50398c230c8SBjoern A. Zeeb 50498c230c8SBjoern A. Zeeb error = ifc_alloc_unit(ifc, &unit); 50598c230c8SBjoern A. Zeeb if (error != 0) 50698c230c8SBjoern A. Zeeb return (error); 50798c230c8SBjoern A. Zeeb 50898c230c8SBjoern A. Zeeb /* 50998c230c8SBjoern A. Zeeb * If no unit had been given, we need to adjust the ifName. 51098c230c8SBjoern A. Zeeb * Also make sure there is space for our extra [ab] suffix. 51198c230c8SBjoern A. Zeeb */ 51298c230c8SBjoern A. Zeeb for (dp = name; *dp != '\0'; dp++); 51398c230c8SBjoern A. Zeeb if (wildcard) { 51498c230c8SBjoern A. Zeeb error = snprintf(dp, len - (dp - name), "%d", unit); 51598c230c8SBjoern A. Zeeb if (error > len - (dp - name) - 1) { 51698c230c8SBjoern A. Zeeb /* ifName too long. */ 51798c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 51898c230c8SBjoern A. Zeeb return (ENOSPC); 51998c230c8SBjoern A. Zeeb } 52098c230c8SBjoern A. Zeeb dp += error; 52198c230c8SBjoern A. Zeeb } 52298c230c8SBjoern A. Zeeb if (len - (dp - name) - 1 < 1) { 52398c230c8SBjoern A. Zeeb /* No space left for our [ab] suffix. */ 52498c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 52598c230c8SBjoern A. Zeeb return (ENOSPC); 52698c230c8SBjoern A. Zeeb } 5273c3136b1SHiroki Sato *dp = 'b'; 52898c230c8SBjoern A. Zeeb /* Must not change dp so we can replace 'a' by 'b' later. */ 52998c230c8SBjoern A. Zeeb *(dp+1) = '\0'; 53098c230c8SBjoern A. Zeeb 5313c3136b1SHiroki Sato /* Check if 'a' and 'b' interfaces already exist. */ 5323c3136b1SHiroki Sato if (ifunit(name) != NULL) 5333c3136b1SHiroki Sato return (EEXIST); 5343c3136b1SHiroki Sato *dp = 'a'; 5353c3136b1SHiroki Sato if (ifunit(name) != NULL) 5363c3136b1SHiroki Sato return (EEXIST); 5373c3136b1SHiroki Sato 53898c230c8SBjoern A. Zeeb /* Allocate memory for both [ab] interfaces */ 53998c230c8SBjoern A. Zeeb sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO); 54098c230c8SBjoern A. Zeeb sca->ifp = if_alloc(IFT_ETHER); 54124f0bfbaSKristof Provost sca->num_queues = epair_tasks.tasks; 54298c230c8SBjoern A. Zeeb if (sca->ifp == NULL) { 54398c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 54498c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 54598c230c8SBjoern A. Zeeb return (ENOSPC); 54698c230c8SBjoern A. Zeeb } 54724f0bfbaSKristof Provost sca->queues = mallocarray(sca->num_queues, sizeof(struct epair_queue), 54824f0bfbaSKristof Provost M_EPAIR, M_WAITOK); 54924f0bfbaSKristof Provost for (int i = 0; i < sca->num_queues; i++) { 55024f0bfbaSKristof Provost struct epair_queue *q = &sca->queues[i]; 55124f0bfbaSKristof Provost q->id = i; 55224f0bfbaSKristof Provost q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 55324f0bfbaSKristof Provost q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 55424f0bfbaSKristof Provost q->ridx = 0; 55524f0bfbaSKristof Provost q->sc = sca; 55624f0bfbaSKristof Provost NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); 55724f0bfbaSKristof Provost } 55898c230c8SBjoern A. Zeeb 55998c230c8SBjoern A. Zeeb scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO); 56098c230c8SBjoern A. Zeeb scb->ifp = if_alloc(IFT_ETHER); 56124f0bfbaSKristof Provost scb->num_queues = epair_tasks.tasks; 56298c230c8SBjoern A. Zeeb if (scb->ifp == NULL) { 56398c230c8SBjoern A. Zeeb free(scb, M_EPAIR); 56498c230c8SBjoern A. Zeeb if_free(sca->ifp); 56598c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 56698c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 56798c230c8SBjoern A. Zeeb return (ENOSPC); 56898c230c8SBjoern A. Zeeb } 56924f0bfbaSKristof Provost scb->queues = mallocarray(scb->num_queues, sizeof(struct epair_queue), 57024f0bfbaSKristof Provost M_EPAIR, M_WAITOK); 57124f0bfbaSKristof Provost for (int i = 0; i < scb->num_queues; i++) { 57224f0bfbaSKristof Provost struct epair_queue *q = &scb->queues[i]; 57324f0bfbaSKristof Provost q->id = i; 57424f0bfbaSKristof Provost q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 57524f0bfbaSKristof Provost q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 57624f0bfbaSKristof Provost q->ridx = 0; 57724f0bfbaSKristof Provost q->sc = scb; 57824f0bfbaSKristof Provost NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); 57924f0bfbaSKristof Provost } 58098c230c8SBjoern A. Zeeb 58198c230c8SBjoern A. Zeeb /* 58298c230c8SBjoern A. Zeeb * Cross-reference the interfaces so we will be able to free both. 58398c230c8SBjoern A. Zeeb */ 58498c230c8SBjoern A. Zeeb sca->oifp = scb->ifp; 58598c230c8SBjoern A. Zeeb scb->oifp = sca->ifp; 58698c230c8SBjoern A. Zeeb 5873dd5760aSBjoern A. Zeeb EPAIR_LOCK(); 5883dd5760aSBjoern A. Zeeb #ifdef SMP 5893dd5760aSBjoern A. Zeeb /* Get an approximate distribution. */ 5903dd5760aSBjoern A. Zeeb hash = next_index % mp_ncpus; 5913dd5760aSBjoern A. Zeeb #else 5923dd5760aSBjoern A. Zeeb hash = 0; 5933dd5760aSBjoern A. Zeeb #endif 5943dd5760aSBjoern A. Zeeb EPAIR_UNLOCK(); 595d0ea4743SBjoern A. Zeeb 59618e199adSHiroki Sato /* Initialise pseudo media types. */ 59718e199adSHiroki Sato ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status); 59818e199adSHiroki Sato ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL); 59918e199adSHiroki Sato ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T); 60018e199adSHiroki Sato ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status); 60118e199adSHiroki Sato ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL); 60218e199adSHiroki Sato ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T); 60318e199adSHiroki Sato 60498c230c8SBjoern A. Zeeb /* Finish initialization of interface <n>a. */ 60598c230c8SBjoern A. Zeeb ifp = sca->ifp; 60698c230c8SBjoern A. Zeeb ifp->if_softc = sca; 60798c230c8SBjoern A. Zeeb strlcpy(ifp->if_xname, name, IFNAMSIZ); 60842a58907SGleb Smirnoff ifp->if_dname = epairname; 60998c230c8SBjoern A. Zeeb ifp->if_dunit = unit; 61098c230c8SBjoern A. Zeeb ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 6113dd5760aSBjoern A. Zeeb ifp->if_flags |= IFF_KNOWSEPOCH; 6129f8cab7fSMarko Zec ifp->if_capabilities = IFCAP_VLAN_MTU; 6139f8cab7fSMarko Zec ifp->if_capenable = IFCAP_VLAN_MTU; 61498c230c8SBjoern A. Zeeb ifp->if_start = epair_start; 61598c230c8SBjoern A. Zeeb ifp->if_ioctl = epair_ioctl; 61698c230c8SBjoern A. Zeeb ifp->if_init = epair_init; 6174e950412SErmal Luçi if_setsendqlen(ifp, ifqmaxlen); 6184e950412SErmal Luçi if_setsendqready(ifp); 61911d41666SLuca Pizzamiglio 62011d41666SLuca Pizzamiglio /* 62111d41666SLuca Pizzamiglio * Calculate the etheraddr hashing the hostid and the 622804771f5SEugene Grosbein * interface index. The result would be hopefully unique. 623804771f5SEugene Grosbein * Note that the "a" component of an epair instance may get moved 624804771f5SEugene Grosbein * to a different VNET after creation. In that case its index 625804771f5SEugene Grosbein * will be freed and the index can get reused by new epair instance. 626804771f5SEugene Grosbein * Make sure we do not create same etheraddr again. 62711d41666SLuca Pizzamiglio */ 62811d41666SLuca Pizzamiglio getcredhostid(curthread->td_ucred, (unsigned long *)&hostid); 62911d41666SLuca Pizzamiglio if (hostid == 0) 63011d41666SLuca Pizzamiglio arc4rand(&hostid, sizeof(hostid), 0); 631804771f5SEugene Grosbein 6323dd5760aSBjoern A. Zeeb EPAIR_LOCK(); 633804771f5SEugene Grosbein if (ifp->if_index > next_index) 634804771f5SEugene Grosbein next_index = ifp->if_index; 635804771f5SEugene Grosbein else 636804771f5SEugene Grosbein next_index++; 637804771f5SEugene Grosbein 638804771f5SEugene Grosbein key[0] = (uint32_t)next_index; 6393dd5760aSBjoern A. Zeeb EPAIR_UNLOCK(); 64011d41666SLuca Pizzamiglio key[1] = (uint32_t)(hostid & 0xffffffff); 64111d41666SLuca Pizzamiglio key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff); 64211d41666SLuca Pizzamiglio hash = jenkins_hash32(key, 3, 0); 64311d41666SLuca Pizzamiglio 64498c230c8SBjoern A. Zeeb eaddr[0] = 0x02; 64511d41666SLuca Pizzamiglio memcpy(&eaddr[1], &hash, 4); 64698c230c8SBjoern A. Zeeb eaddr[5] = 0x0a; 64798c230c8SBjoern A. Zeeb ether_ifattach(ifp, eaddr); 648b245f96cSGleb Smirnoff ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 6493dd5760aSBjoern A. Zeeb ifp->if_transmit = epair_transmit; 65098c230c8SBjoern A. Zeeb 65198c230c8SBjoern A. Zeeb /* Swap the name and finish initialization of interface <n>b. */ 65298c230c8SBjoern A. Zeeb *dp = 'b'; 65398c230c8SBjoern A. Zeeb 65498c230c8SBjoern A. Zeeb ifp = scb->ifp; 65598c230c8SBjoern A. Zeeb ifp->if_softc = scb; 65698c230c8SBjoern A. Zeeb strlcpy(ifp->if_xname, name, IFNAMSIZ); 65742a58907SGleb Smirnoff ifp->if_dname = epairname; 65898c230c8SBjoern A. Zeeb ifp->if_dunit = unit; 65998c230c8SBjoern A. Zeeb ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 66073d41cc7SZhenlei Huang ifp->if_flags |= IFF_KNOWSEPOCH; 6619f8cab7fSMarko Zec ifp->if_capabilities = IFCAP_VLAN_MTU; 6629f8cab7fSMarko Zec ifp->if_capenable = IFCAP_VLAN_MTU; 66398c230c8SBjoern A. Zeeb ifp->if_start = epair_start; 66498c230c8SBjoern A. Zeeb ifp->if_ioctl = epair_ioctl; 66598c230c8SBjoern A. Zeeb ifp->if_init = epair_init; 6664e950412SErmal Luçi if_setsendqlen(ifp, ifqmaxlen); 6674e950412SErmal Luçi if_setsendqready(ifp); 66898c230c8SBjoern A. Zeeb /* We need to play some tricks here for the second interface. */ 66942a58907SGleb Smirnoff strlcpy(name, epairname, len); 670b02fd8b7SKristof Provost 671b02fd8b7SKristof Provost /* Correctly set the name for the cloner list. */ 672b02fd8b7SKristof Provost strlcpy(name, scb->ifp->if_xname, len); 673b02fd8b7SKristof Provost epair_clone_add(ifc, scb); 674b02fd8b7SKristof Provost 675b245f96cSGleb Smirnoff ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 6763dd5760aSBjoern A. Zeeb ifp->if_transmit = epair_transmit; 67798c230c8SBjoern A. Zeeb 67898c230c8SBjoern A. Zeeb /* 67998c230c8SBjoern A. Zeeb * Restore name to <n>a as the ifp for this will go into the 68098c230c8SBjoern A. Zeeb * cloner list for the initial call. 68198c230c8SBjoern A. Zeeb */ 68298c230c8SBjoern A. Zeeb strlcpy(name, sca->ifp->if_xname, len); 68398c230c8SBjoern A. Zeeb 68498c230c8SBjoern A. Zeeb /* Tell the world, that we are ready to rock. */ 68598c230c8SBjoern A. Zeeb sca->ifp->if_drv_flags |= IFF_DRV_RUNNING; 686c7493539SBjoern A. Zeeb if_link_state_change(sca->ifp, LINK_STATE_UP); 6873dd5760aSBjoern A. Zeeb scb->ifp->if_drv_flags |= IFF_DRV_RUNNING; 688c7493539SBjoern A. Zeeb if_link_state_change(scb->ifp, LINK_STATE_UP); 68998c230c8SBjoern A. Zeeb 69098c230c8SBjoern A. Zeeb return (0); 69198c230c8SBjoern A. Zeeb } 69298c230c8SBjoern A. Zeeb 6933dd5760aSBjoern A. Zeeb static void 6943dd5760aSBjoern A. Zeeb epair_drain_rings(struct epair_softc *sc) 6953dd5760aSBjoern A. Zeeb { 6963dd5760aSBjoern A. Zeeb int ridx; 6973dd5760aSBjoern A. Zeeb struct mbuf *m; 6983dd5760aSBjoern A. Zeeb 6993dd5760aSBjoern A. Zeeb for (ridx = 0; ridx < 2; ridx++) { 70024f0bfbaSKristof Provost for (int i = 0; i < sc->num_queues; i++) { 70124f0bfbaSKristof Provost struct epair_queue *q = &sc->queues[i]; 7023dd5760aSBjoern A. Zeeb do { 70324f0bfbaSKristof Provost m = buf_ring_dequeue_sc(q->rxring[ridx]); 7043dd5760aSBjoern A. Zeeb if (m == NULL) 7053dd5760aSBjoern A. Zeeb break; 7063dd5760aSBjoern A. Zeeb m_freem(m); 7073dd5760aSBjoern A. Zeeb } while (1); 7083dd5760aSBjoern A. Zeeb } 7093dd5760aSBjoern A. Zeeb } 71024f0bfbaSKristof Provost } 7113dd5760aSBjoern A. Zeeb 71298c230c8SBjoern A. Zeeb static int 71398c230c8SBjoern A. Zeeb epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 71498c230c8SBjoern A. Zeeb { 71598c230c8SBjoern A. Zeeb struct ifnet *oifp; 71698c230c8SBjoern A. Zeeb struct epair_softc *sca, *scb; 71798c230c8SBjoern A. Zeeb int unit, error; 71898c230c8SBjoern A. Zeeb 71998c230c8SBjoern A. Zeeb /* 72098c230c8SBjoern A. Zeeb * In case we called into if_clone_destroyif() ourselves 72198c230c8SBjoern A. Zeeb * again to remove the second interface, the softc will be 72298c230c8SBjoern A. Zeeb * NULL. In that case so not do anything but return success. 72398c230c8SBjoern A. Zeeb */ 72498c230c8SBjoern A. Zeeb if (ifp->if_softc == NULL) 72598c230c8SBjoern A. Zeeb return (0); 72698c230c8SBjoern A. Zeeb 72798c230c8SBjoern A. Zeeb unit = ifp->if_dunit; 72898c230c8SBjoern A. Zeeb sca = ifp->if_softc; 72998c230c8SBjoern A. Zeeb oifp = sca->oifp; 73098c230c8SBjoern A. Zeeb scb = oifp->if_softc; 73198c230c8SBjoern A. Zeeb 7323dd5760aSBjoern A. Zeeb /* Frist get the interfaces down and detached. */ 733c7493539SBjoern A. Zeeb if_link_state_change(ifp, LINK_STATE_DOWN); 73498c230c8SBjoern A. Zeeb ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 7353dd5760aSBjoern A. Zeeb if_link_state_change(oifp, LINK_STATE_DOWN); 73698c230c8SBjoern A. Zeeb oifp->if_drv_flags &= ~IFF_DRV_RUNNING; 73798c230c8SBjoern A. Zeeb 7383dd5760aSBjoern A. Zeeb ether_ifdetach(ifp); 7397edc3d88SMikolaj Golub ether_ifdetach(oifp); 7403dd5760aSBjoern A. Zeeb 7413dd5760aSBjoern A. Zeeb /* Third free any queued packets and all the resources. */ 7423dd5760aSBjoern A. Zeeb CURVNET_SET_QUIET(oifp->if_vnet); 7433dd5760aSBjoern A. Zeeb epair_drain_rings(scb); 74498c230c8SBjoern A. Zeeb oifp->if_softc = NULL; 74598c230c8SBjoern A. Zeeb error = if_clone_destroyif(ifc, oifp); 74698c230c8SBjoern A. Zeeb if (error) 74798c230c8SBjoern A. Zeeb panic("%s: if_clone_destroyif() for our 2nd iface failed: %d", 74898c230c8SBjoern A. Zeeb __func__, error); 74973e39d61SBjoern A. Zeeb if_free(oifp); 7502dccdd45SMarko Zec ifmedia_removeall(&scb->media); 75124f0bfbaSKristof Provost for (int i = 0; i < scb->num_queues; i++) { 75224f0bfbaSKristof Provost struct epair_queue *q = &scb->queues[i]; 75324f0bfbaSKristof Provost buf_ring_free(q->rxring[0], M_EPAIR); 75424f0bfbaSKristof Provost buf_ring_free(q->rxring[1], M_EPAIR); 75524f0bfbaSKristof Provost } 75624f0bfbaSKristof Provost free(scb->queues, M_EPAIR); 75798c230c8SBjoern A. Zeeb free(scb, M_EPAIR); 7587edc3d88SMikolaj Golub CURVNET_RESTORE(); 7597edc3d88SMikolaj Golub 7603dd5760aSBjoern A. Zeeb epair_drain_rings(sca); 7617edc3d88SMikolaj Golub if_free(ifp); 7627edc3d88SMikolaj Golub ifmedia_removeall(&sca->media); 76324f0bfbaSKristof Provost for (int i = 0; i < sca->num_queues; i++) { 76424f0bfbaSKristof Provost struct epair_queue *q = &sca->queues[i]; 76524f0bfbaSKristof Provost buf_ring_free(q->rxring[0], M_EPAIR); 76624f0bfbaSKristof Provost buf_ring_free(q->rxring[1], M_EPAIR); 76724f0bfbaSKristof Provost } 76824f0bfbaSKristof Provost free(sca->queues, M_EPAIR); 76998c230c8SBjoern A. Zeeb free(sca, M_EPAIR); 7703dd5760aSBjoern A. Zeeb 7713dd5760aSBjoern A. Zeeb /* Last free the cloner unit. */ 77298c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit); 77398c230c8SBjoern A. Zeeb 77498c230c8SBjoern A. Zeeb return (0); 77598c230c8SBjoern A. Zeeb } 77698c230c8SBjoern A. Zeeb 7773c3136b1SHiroki Sato static void 7783c3136b1SHiroki Sato vnet_epair_init(const void *unused __unused) 7793c3136b1SHiroki Sato { 7803c3136b1SHiroki Sato 7813c3136b1SHiroki Sato V_epair_cloner = if_clone_advanced(epairname, 0, 7823c3136b1SHiroki Sato epair_clone_match, epair_clone_create, epair_clone_destroy); 7833c3136b1SHiroki Sato } 78489856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 7853c3136b1SHiroki Sato vnet_epair_init, NULL); 7863c3136b1SHiroki Sato 7873c3136b1SHiroki Sato static void 7883c3136b1SHiroki Sato vnet_epair_uninit(const void *unused __unused) 7893c3136b1SHiroki Sato { 7903c3136b1SHiroki Sato 7913c3136b1SHiroki Sato if_clone_detach(V_epair_cloner); 7923c3136b1SHiroki Sato } 79389856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 7943c3136b1SHiroki Sato vnet_epair_uninit, NULL); 7953c3136b1SHiroki Sato 79698c230c8SBjoern A. Zeeb static int 7977442b632SLi-Wen Hsu epair_mod_init(void) 79824f0bfbaSKristof Provost { 79924f0bfbaSKristof Provost char name[32]; 80024f0bfbaSKristof Provost epair_tasks.tasks = 0; 80124f0bfbaSKristof Provost 80224f0bfbaSKristof Provost #ifdef RSS 80324f0bfbaSKristof Provost struct pcpu *pcpu; 80424f0bfbaSKristof Provost int cpu; 80524f0bfbaSKristof Provost 80624f0bfbaSKristof Provost CPU_FOREACH(cpu) { 80724f0bfbaSKristof Provost cpuset_t cpu_mask; 80824f0bfbaSKristof Provost 80924f0bfbaSKristof Provost /* Pin to this CPU so we get appropriate NUMA allocations. */ 81024f0bfbaSKristof Provost pcpu = pcpu_find(cpu); 81124f0bfbaSKristof Provost thread_lock(curthread); 81224f0bfbaSKristof Provost sched_bind(curthread, cpu); 81324f0bfbaSKristof Provost thread_unlock(curthread); 81424f0bfbaSKristof Provost 81524f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task_%d", cpu); 81624f0bfbaSKristof Provost 81724f0bfbaSKristof Provost epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK, 81824f0bfbaSKristof Provost taskqueue_thread_enqueue, 81924f0bfbaSKristof Provost &epair_tasks.tq[cpu]); 82024f0bfbaSKristof Provost CPU_SETOF(cpu, &cpu_mask); 82124f0bfbaSKristof Provost taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET, 82224f0bfbaSKristof Provost &cpu_mask, "%s", name); 82324f0bfbaSKristof Provost 82424f0bfbaSKristof Provost epair_tasks.tasks++; 82524f0bfbaSKristof Provost } 82624f0bfbaSKristof Provost #else 82724f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task"); 82824f0bfbaSKristof Provost 82924f0bfbaSKristof Provost epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK, 83024f0bfbaSKristof Provost taskqueue_thread_enqueue, 83124f0bfbaSKristof Provost &epair_tasks.tq[0]); 83224f0bfbaSKristof Provost taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name); 83324f0bfbaSKristof Provost 83424f0bfbaSKristof Provost epair_tasks.tasks = 1; 83524f0bfbaSKristof Provost #endif 83624f0bfbaSKristof Provost 83724f0bfbaSKristof Provost return (0); 83824f0bfbaSKristof Provost } 83924f0bfbaSKristof Provost 84024f0bfbaSKristof Provost static void 8417442b632SLi-Wen Hsu epair_mod_cleanup(void) 84224f0bfbaSKristof Provost { 84324f0bfbaSKristof Provost 84424f0bfbaSKristof Provost for (int i = 0; i < epair_tasks.tasks; i++) { 84524f0bfbaSKristof Provost taskqueue_drain_all(epair_tasks.tq[i]); 84624f0bfbaSKristof Provost taskqueue_free(epair_tasks.tq[i]); 84724f0bfbaSKristof Provost } 84824f0bfbaSKristof Provost } 84924f0bfbaSKristof Provost 85024f0bfbaSKristof Provost static int 85198c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data) 85298c230c8SBjoern A. Zeeb { 85324f0bfbaSKristof Provost int ret; 85498c230c8SBjoern A. Zeeb 85598c230c8SBjoern A. Zeeb switch (type) { 85698c230c8SBjoern A. Zeeb case MOD_LOAD: 8573dd5760aSBjoern A. Zeeb EPAIR_LOCK_INIT(); 85824f0bfbaSKristof Provost ret = epair_mod_init(); 85924f0bfbaSKristof Provost if (ret != 0) 86024f0bfbaSKristof Provost return (ret); 86198c230c8SBjoern A. Zeeb if (bootverbose) 8623dd5760aSBjoern A. Zeeb printf("%s: %s initialized.\n", __func__, epairname); 86398c230c8SBjoern A. Zeeb break; 86498c230c8SBjoern A. Zeeb case MOD_UNLOAD: 86524f0bfbaSKristof Provost epair_mod_cleanup(); 8663dd5760aSBjoern A. Zeeb EPAIR_LOCK_DESTROY(); 8673dd5760aSBjoern A. Zeeb if (bootverbose) 8683dd5760aSBjoern A. Zeeb printf("%s: %s unloaded.\n", __func__, epairname); 86998c230c8SBjoern A. Zeeb break; 87098c230c8SBjoern A. Zeeb default: 87198c230c8SBjoern A. Zeeb return (EOPNOTSUPP); 87298c230c8SBjoern A. Zeeb } 87398c230c8SBjoern A. Zeeb return (0); 87498c230c8SBjoern A. Zeeb } 87598c230c8SBjoern A. Zeeb 87698c230c8SBjoern A. Zeeb static moduledata_t epair_mod = { 87798c230c8SBjoern A. Zeeb "if_epair", 87898c230c8SBjoern A. Zeeb epair_modevent, 8799823d527SKevin Lo 0 88098c230c8SBjoern A. Zeeb }; 88198c230c8SBjoern A. Zeeb 88289856f7eSBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 8833dd5760aSBjoern A. Zeeb MODULE_VERSION(if_epair, 3); 884