198c230c8SBjoern A. Zeeb /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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>
4124f0bfbaSKristof Provost #include "opt_rss.h"
4252bcdc5bSSantiago Martinez #include "opt_inet.h"
4352bcdc5bSSantiago Martinez #include "opt_inet6.h"
4424f0bfbaSKristof Provost
4598c230c8SBjoern A. Zeeb #include <sys/param.h>
4629c9b167SMark Johnston #include <sys/bus.h>
4711d41666SLuca Pizzamiglio #include <sys/hash.h>
4829c9b167SMark Johnston #include <sys/interrupt.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
6398c230c8SBjoern A. Zeeb #include <net/bpf.h>
6498c230c8SBjoern A. Zeeb #include <net/ethernet.h>
6598c230c8SBjoern A. Zeeb #include <net/if.h>
6676039bc8SGleb Smirnoff #include <net/if_var.h>
6798c230c8SBjoern A. Zeeb #include <net/if_clone.h>
682dccdd45SMarko Zec #include <net/if_media.h>
6998c230c8SBjoern A. Zeeb #include <net/if_var.h>
702c2b37adSJustin Hibbits #include <net/if_private.h>
7198c230c8SBjoern A. Zeeb #include <net/if_types.h>
7298c230c8SBjoern A. Zeeb #include <net/netisr.h>
7324f0bfbaSKristof Provost #ifdef RSS
7424f0bfbaSKristof Provost #include <net/rss_config.h>
7552bcdc5bSSantiago Martinez #ifdef INET
7624f0bfbaSKristof Provost #include <netinet/in_rss.h>
7752bcdc5bSSantiago Martinez #endif
7852bcdc5bSSantiago Martinez #ifdef INET6
7924f0bfbaSKristof Provost #include <netinet6/in6_rss.h>
8024f0bfbaSKristof Provost #endif
8152bcdc5bSSantiago Martinez #endif
82530c0060SRobert Watson #include <net/vnet.h>
8398c230c8SBjoern A. Zeeb
8442a58907SGleb Smirnoff static const char epairname[] = "epair";
853dd5760aSBjoern A. Zeeb #define RXRSIZE 4096 /* Probably overkill by 4-8x. */
86d0ea4743SBjoern A. Zeeb
8742a58907SGleb Smirnoff static MALLOC_DEFINE(M_EPAIR, epairname,
88d0ea4743SBjoern A. Zeeb "Pair of virtual cross-over connected Ethernet-like interfaces");
8998c230c8SBjoern A. Zeeb
905f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, epair_cloner);
913c3136b1SHiroki Sato #define V_epair_cloner VNET(epair_cloner)
9298c230c8SBjoern A. Zeeb
933dd5760aSBjoern A. Zeeb static unsigned int next_index = 0;
943dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \
953dd5760aSBjoern A. Zeeb NULL, MTX_DEF)
963dd5760aSBjoern A. Zeeb #define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx)
973dd5760aSBjoern A. Zeeb #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx)
983dd5760aSBjoern A. Zeeb #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx)
993dd5760aSBjoern A. Zeeb
10024f0bfbaSKristof Provost struct epair_softc;
10124f0bfbaSKristof Provost struct epair_queue {
102df7bbd8cSMark Johnston struct mtx mtx;
103df7bbd8cSMark Johnston struct mbufq q;
10424f0bfbaSKristof Provost int id;
105df7bbd8cSMark Johnston enum {
106df7bbd8cSMark Johnston EPAIR_QUEUE_IDLE,
107df7bbd8cSMark Johnston EPAIR_QUEUE_WAKING,
108df7bbd8cSMark Johnston EPAIR_QUEUE_RUNNING,
109df7bbd8cSMark Johnston } state;
11024f0bfbaSKristof Provost struct task tx_task;
11124f0bfbaSKristof Provost struct epair_softc *sc;
11224f0bfbaSKristof Provost };
1133dd5760aSBjoern A. Zeeb
1143dd5760aSBjoern A. Zeeb static struct mtx epair_n_index_mtx;
1153dd5760aSBjoern A. Zeeb struct epair_softc {
1163dd5760aSBjoern A. Zeeb struct ifnet *ifp; /* This ifp. */
1173dd5760aSBjoern A. Zeeb struct ifnet *oifp; /* other ifp of pair. */
11824f0bfbaSKristof Provost int num_queues;
11924f0bfbaSKristof Provost struct epair_queue *queues;
1203dd5760aSBjoern A. Zeeb struct ifmedia media; /* Media config (fake). */
1213dd5760aSBjoern A. Zeeb STAILQ_ENTRY(epair_softc) entry;
122d0ea4743SBjoern A. Zeeb };
123d0ea4743SBjoern A. Zeeb
12424f0bfbaSKristof Provost struct epair_tasks_t {
12524f0bfbaSKristof Provost int tasks;
12624f0bfbaSKristof Provost struct taskqueue *tq[MAXCPU];
12724f0bfbaSKristof Provost };
12824f0bfbaSKristof Provost
12924f0bfbaSKristof Provost static struct epair_tasks_t epair_tasks;
13024f0bfbaSKristof Provost
131d0ea4743SBjoern A. Zeeb static void
epair_clear_mbuf(struct mbuf * m)13262d2dcafSKristof Provost epair_clear_mbuf(struct mbuf *m)
13362d2dcafSKristof Provost {
134c69ae841SKristof Provost M_ASSERTPKTHDR(m);
135c69ae841SKristof 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
143c69ae841SKristof Provost /* Clear vlan information. */
144c69ae841SKristof Provost m->m_flags &= ~M_VLANTAG;
145c69ae841SKristof Provost m->m_pkthdr.ether_vtag = 0;
146c69ae841SKristof Provost
14762d2dcafSKristof Provost m_tag_delete_nonpersistent(m);
14862d2dcafSKristof Provost }
14962d2dcafSKristof Provost
15062d2dcafSKristof Provost static void
epair_tx_start_deferred(void * arg,int pending)15124f0bfbaSKristof Provost epair_tx_start_deferred(void *arg, int pending)
152d0ea4743SBjoern A. Zeeb {
15324f0bfbaSKristof Provost struct epair_queue *q = (struct epair_queue *)arg;
154df7bbd8cSMark Johnston if_t ifp;
155df7bbd8cSMark Johnston struct mbuf *m, *n;
156df7bbd8cSMark Johnston bool resched;
157d0ea4743SBjoern A. Zeeb
158df7bbd8cSMark Johnston ifp = q->sc->ifp;
15924f0bfbaSKristof Provost
160df7bbd8cSMark Johnston if_ref(ifp);
161df7bbd8cSMark Johnston CURVNET_SET(ifp->if_vnet);
162df7bbd8cSMark Johnston
163df7bbd8cSMark Johnston mtx_lock(&q->mtx);
164df7bbd8cSMark Johnston m = mbufq_flush(&q->q);
165df7bbd8cSMark Johnston q->state = EPAIR_QUEUE_RUNNING;
166df7bbd8cSMark Johnston mtx_unlock(&q->mtx);
167df7bbd8cSMark Johnston
168df7bbd8cSMark Johnston while (m != NULL) {
169df7bbd8cSMark Johnston n = STAILQ_NEXT(m, m_stailqpkt);
170df7bbd8cSMark Johnston m->m_nextpkt = NULL;
171df7bbd8cSMark Johnston if_input(ifp, m);
172df7bbd8cSMark Johnston m = n;
173df7bbd8cSMark Johnston }
174df7bbd8cSMark Johnston
175df7bbd8cSMark Johnston /*
176df7bbd8cSMark Johnston * Avoid flushing the queue more than once per task. We can otherwise
177df7bbd8cSMark Johnston * end up starving ourselves in a multi-epair routing configuration.
178df7bbd8cSMark Johnston */
179df7bbd8cSMark Johnston mtx_lock(&q->mtx);
180*8cb9b68fSJohn Baldwin if (!mbufq_empty(&q->q)) {
181df7bbd8cSMark Johnston resched = true;
182df7bbd8cSMark Johnston q->state = EPAIR_QUEUE_WAKING;
183df7bbd8cSMark Johnston } else {
184df7bbd8cSMark Johnston resched = false;
185df7bbd8cSMark Johnston q->state = EPAIR_QUEUE_IDLE;
186df7bbd8cSMark Johnston }
187df7bbd8cSMark Johnston mtx_unlock(&q->mtx);
188df7bbd8cSMark Johnston
189df7bbd8cSMark Johnston if (resched)
19024f0bfbaSKristof Provost taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
191d0ea4743SBjoern A. Zeeb
192df7bbd8cSMark Johnston CURVNET_RESTORE();
193df7bbd8cSMark Johnston if_rele(ifp);
194d0ea4743SBjoern A. Zeeb }
195d0ea4743SBjoern A. Zeeb
19604a32b80SAlexander V. Chernikov static struct epair_queue *
epair_select_queue(struct epair_softc * sc,struct mbuf * m)1978a299958SKristof Provost epair_select_queue(struct epair_softc *sc, struct mbuf *m)
198d0ea4743SBjoern A. Zeeb {
19924f0bfbaSKristof Provost uint32_t bucket;
20024f0bfbaSKristof Provost #ifdef RSS
20124f0bfbaSKristof Provost struct ether_header *eh;
2028a299958SKristof Provost int ret;
203d0ea4743SBjoern A. Zeeb
20424f0bfbaSKristof Provost ret = rss_m2bucket(m, &bucket);
20524f0bfbaSKristof Provost if (ret) {
20624f0bfbaSKristof Provost /* Actually hash the packet. */
20724f0bfbaSKristof Provost eh = mtod(m, struct ether_header *);
20824f0bfbaSKristof Provost
20924f0bfbaSKristof Provost switch (ntohs(eh->ether_type)) {
21052bcdc5bSSantiago Martinez #ifdef INET
21124f0bfbaSKristof Provost case ETHERTYPE_IP:
21224f0bfbaSKristof Provost rss_soft_m2cpuid_v4(m, 0, &bucket);
21324f0bfbaSKristof Provost break;
21452bcdc5bSSantiago Martinez #endif
21552bcdc5bSSantiago Martinez #ifdef INET6
21624f0bfbaSKristof Provost case ETHERTYPE_IPV6:
21724f0bfbaSKristof Provost rss_soft_m2cpuid_v6(m, 0, &bucket);
21824f0bfbaSKristof Provost break;
21952bcdc5bSSantiago Martinez #endif
22024f0bfbaSKristof Provost default:
22124f0bfbaSKristof Provost bucket = 0;
22224f0bfbaSKristof Provost break;
22324f0bfbaSKristof Provost }
22424f0bfbaSKristof Provost }
22504a32b80SAlexander V. Chernikov bucket %= sc->num_queues;
22624f0bfbaSKristof Provost #else
22724f0bfbaSKristof Provost bucket = 0;
22824f0bfbaSKristof Provost #endif
22904a32b80SAlexander V. Chernikov return (&sc->queues[bucket]);
23004a32b80SAlexander V. Chernikov }
23104a32b80SAlexander V. Chernikov
23204a32b80SAlexander V. Chernikov static void
epair_prepare_mbuf(struct mbuf * m,struct ifnet * src_ifp)23304a32b80SAlexander V. Chernikov epair_prepare_mbuf(struct mbuf *m, struct ifnet *src_ifp)
23404a32b80SAlexander V. Chernikov {
23504a32b80SAlexander V. Chernikov M_ASSERTPKTHDR(m);
23604a32b80SAlexander V. Chernikov epair_clear_mbuf(m);
23704a32b80SAlexander V. Chernikov if_setrcvif(m, src_ifp);
23804a32b80SAlexander V. Chernikov M_SETFIB(m, src_ifp->if_fib);
23904a32b80SAlexander V. Chernikov
24004a32b80SAlexander V. Chernikov MPASS(m->m_nextpkt == NULL);
24104a32b80SAlexander V. Chernikov MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
24204a32b80SAlexander V. Chernikov }
24304a32b80SAlexander V. Chernikov
24404a32b80SAlexander V. Chernikov static void
epair_menq(struct mbuf * m,struct epair_softc * osc)24504a32b80SAlexander V. Chernikov epair_menq(struct mbuf *m, struct epair_softc *osc)
24604a32b80SAlexander V. Chernikov {
247df7bbd8cSMark Johnston struct epair_queue *q;
24804a32b80SAlexander V. Chernikov struct ifnet *ifp, *oifp;
249df7bbd8cSMark Johnston int error, len;
25048227d1cSMark Johnston bool mcast;
25104a32b80SAlexander V. Chernikov
25204a32b80SAlexander V. Chernikov /*
25304a32b80SAlexander V. Chernikov * I know this looks weird. We pass the "other sc" as we need that one
25404a32b80SAlexander V. Chernikov * and can get both ifps from it as well.
25504a32b80SAlexander V. Chernikov */
25604a32b80SAlexander V. Chernikov oifp = osc->ifp;
25704a32b80SAlexander V. Chernikov ifp = osc->oifp;
25804a32b80SAlexander V. Chernikov
25904a32b80SAlexander V. Chernikov epair_prepare_mbuf(m, oifp);
26004a32b80SAlexander V. Chernikov
26104a32b80SAlexander V. Chernikov /* Save values as once the mbuf is queued, it's not ours anymore. */
26204a32b80SAlexander V. Chernikov len = m->m_pkthdr.len;
26348227d1cSMark Johnston mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
26404a32b80SAlexander V. Chernikov
265df7bbd8cSMark Johnston q = epair_select_queue(osc, m);
26624f0bfbaSKristof Provost
267df7bbd8cSMark Johnston mtx_lock(&q->mtx);
268df7bbd8cSMark Johnston if (q->state == EPAIR_QUEUE_IDLE) {
269df7bbd8cSMark Johnston q->state = EPAIR_QUEUE_WAKING;
270df7bbd8cSMark Johnston taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
2713dd5760aSBjoern A. Zeeb }
272df7bbd8cSMark Johnston error = mbufq_enqueue(&q->q, m);
273df7bbd8cSMark Johnston mtx_unlock(&q->mtx);
274d0ea4743SBjoern A. Zeeb
275df7bbd8cSMark Johnston if (error != 0) {
276df7bbd8cSMark Johnston m_freem(m);
277df7bbd8cSMark Johnston if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
278df7bbd8cSMark Johnston } else {
2793dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2803dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
28148227d1cSMark Johnston if (mcast)
2823dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
2833dd5760aSBjoern A. Zeeb if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
284df7bbd8cSMark Johnston }
285d0ea4743SBjoern A. Zeeb }
286d0ea4743SBjoern A. Zeeb
28798c230c8SBjoern A. Zeeb static void
epair_start(struct ifnet * ifp)2883dd5760aSBjoern A. Zeeb epair_start(struct ifnet *ifp)
28998c230c8SBjoern A. Zeeb {
29098c230c8SBjoern A. Zeeb struct mbuf *m;
29198c230c8SBjoern A. Zeeb struct epair_softc *sc;
29298c230c8SBjoern A. Zeeb struct ifnet *oifp;
29398c230c8SBjoern A. Zeeb
29498c230c8SBjoern A. Zeeb /*
29559f35a82SPatrick Kelsey * We get packets here from ether_output via if_handoff()
29675580d58SPatrick Kelsey * and need to put them into the input queue of the oifp
2973dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the
2983dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair.
29998c230c8SBjoern A. Zeeb */
3003dd5760aSBjoern A. Zeeb sc = ifp->if_softc;
30198c230c8SBjoern A. Zeeb oifp = sc->oifp;
30298c230c8SBjoern A. Zeeb sc = oifp->if_softc;
30398c230c8SBjoern A. Zeeb for (;;) {
30498c230c8SBjoern A. Zeeb IFQ_DEQUEUE(&ifp->if_snd, m);
30598c230c8SBjoern A. Zeeb if (m == NULL)
30698c230c8SBjoern A. Zeeb break;
3073dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m);
30898c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m);
30998c230c8SBjoern A. Zeeb
3103dd5760aSBjoern A. Zeeb /* In case either interface is not usable drop the packet. */
3113dd5760aSBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
3123dd5760aSBjoern A. Zeeb (ifp->if_flags & IFF_UP) == 0 ||
3133dd5760aSBjoern A. Zeeb (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
31498c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) {
31598c230c8SBjoern A. Zeeb m_freem(m);
31698c230c8SBjoern A. Zeeb continue;
31798c230c8SBjoern A. Zeeb }
31898c230c8SBjoern A. Zeeb
31904a32b80SAlexander V. Chernikov epair_menq(m, sc);
32098c230c8SBjoern A. Zeeb }
32198c230c8SBjoern A. Zeeb }
32298c230c8SBjoern A. Zeeb
32398c230c8SBjoern A. Zeeb static int
epair_transmit(struct ifnet * ifp,struct mbuf * m)3243dd5760aSBjoern A. Zeeb epair_transmit(struct ifnet *ifp, struct mbuf *m)
32598c230c8SBjoern A. Zeeb {
32698c230c8SBjoern A. Zeeb struct epair_softc *sc;
32798c230c8SBjoern A. Zeeb struct ifnet *oifp;
3282cedfc3fSMateusz Guzik #ifdef ALTQ
3292cedfc3fSMateusz Guzik int len;
33048227d1cSMark Johnston bool mcast;
3312cedfc3fSMateusz Guzik #endif
33298c230c8SBjoern A. Zeeb
33398c230c8SBjoern A. Zeeb if (m == NULL)
33498c230c8SBjoern A. Zeeb return (0);
3353dd5760aSBjoern A. Zeeb M_ASSERTPKTHDR(m);
33698c230c8SBjoern A. Zeeb
33798c230c8SBjoern A. Zeeb /*
338a9bfd080SKristof Provost * We could just transmit this, but it makes testing easier if we're a
339a9bfd080SKristof Provost * little bit more like real hardware.
340a9bfd080SKristof Provost * Allow just that little bit extra for ethernet (and vlan) headers.
341a9bfd080SKristof Provost */
342a9bfd080SKristof Provost if (m->m_pkthdr.len > (ifp->if_mtu + sizeof(struct ether_vlan_header))) {
343a9bfd080SKristof Provost m_freem(m);
344a9bfd080SKristof Provost if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
345a9bfd080SKristof Provost return (E2BIG);
346a9bfd080SKristof Provost }
347a9bfd080SKristof Provost
348a9bfd080SKristof Provost /*
34998c230c8SBjoern A. Zeeb * We are not going to use the interface en/dequeue mechanism
35098c230c8SBjoern A. Zeeb * on the TX side. We are called from ether_output_frame()
3513dd5760aSBjoern A. Zeeb * and will put the packet into the receive-queue (rxq) of the
3523dd5760aSBjoern A. Zeeb * other interface (oifp) of our pair.
35398c230c8SBjoern A. Zeeb */
35498c230c8SBjoern A. Zeeb if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
35598c230c8SBjoern A. Zeeb m_freem(m);
3563dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
35798c230c8SBjoern A. Zeeb return (ENXIO);
35898c230c8SBjoern A. Zeeb }
35998c230c8SBjoern A. Zeeb if ((ifp->if_flags & IFF_UP) == 0) {
36098c230c8SBjoern A. Zeeb m_freem(m);
3613dd5760aSBjoern A. Zeeb if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
36298c230c8SBjoern A. Zeeb return (ENETDOWN);
36398c230c8SBjoern A. Zeeb }
36498c230c8SBjoern A. Zeeb
36598c230c8SBjoern A. Zeeb BPF_MTAP(ifp, m);
36698c230c8SBjoern A. Zeeb
36798c230c8SBjoern A. Zeeb /*
36898c230c8SBjoern A. Zeeb * In case the outgoing interface is not usable,
36998c230c8SBjoern A. Zeeb * drop the packet.
37098c230c8SBjoern A. Zeeb */
3713dd5760aSBjoern A. Zeeb sc = ifp->if_softc;
37298c230c8SBjoern A. Zeeb oifp = sc->oifp;
37398c230c8SBjoern A. Zeeb if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
37498c230c8SBjoern A. Zeeb (oifp->if_flags & IFF_UP) == 0) {
3753751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
37698c230c8SBjoern A. Zeeb m_freem(m);
37798c230c8SBjoern A. Zeeb return (0);
37898c230c8SBjoern A. Zeeb }
3792cedfc3fSMateusz Guzik
3802cedfc3fSMateusz Guzik #ifdef ALTQ
38198c230c8SBjoern A. Zeeb len = m->m_pkthdr.len;
38248227d1cSMark Johnston mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
38304a32b80SAlexander V. Chernikov int error = 0;
38498c230c8SBjoern A. Zeeb
385a4641f4eSPedro F. Giffuni /* Support ALTQ via the classic if_start() path. */
38698c230c8SBjoern A. Zeeb IF_LOCK(&ifp->if_snd);
38798c230c8SBjoern A. Zeeb if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
38898c230c8SBjoern A. Zeeb ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
38998c230c8SBjoern A. Zeeb if (error)
3903751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
39198c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd);
39298c230c8SBjoern A. Zeeb if (!error) {
3933751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
39448227d1cSMark Johnston if (mcast)
3953751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3963dd5760aSBjoern A. Zeeb epair_start(ifp);
39798c230c8SBjoern A. Zeeb }
39898c230c8SBjoern A. Zeeb return (error);
39998c230c8SBjoern A. Zeeb }
40098c230c8SBjoern A. Zeeb IF_UNLOCK(&ifp->if_snd);
40198c230c8SBjoern A. Zeeb #endif
40298c230c8SBjoern A. Zeeb
40304a32b80SAlexander V. Chernikov epair_menq(m, oifp->if_softc);
40404a32b80SAlexander V. Chernikov return (0);
40598c230c8SBjoern A. Zeeb }
40662d2dcafSKristof Provost
40712aeeb91SAlexander V. Chernikov static void
epair_qflush(struct ifnet * ifp __unused)40812aeeb91SAlexander V. Chernikov epair_qflush(struct ifnet *ifp __unused)
40912aeeb91SAlexander V. Chernikov {
41012aeeb91SAlexander V. Chernikov }
41112aeeb91SAlexander V. Chernikov
41298c230c8SBjoern A. Zeeb static int
epair_media_change(struct ifnet * ifp __unused)4132dccdd45SMarko Zec epair_media_change(struct ifnet *ifp __unused)
4142dccdd45SMarko Zec {
4152dccdd45SMarko Zec
4162dccdd45SMarko Zec /* Do nothing. */
4172dccdd45SMarko Zec return (0);
4182dccdd45SMarko Zec }
4192dccdd45SMarko Zec
4202dccdd45SMarko Zec static void
epair_media_status(struct ifnet * ifp __unused,struct ifmediareq * imr)4212dccdd45SMarko Zec epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
4222dccdd45SMarko Zec {
4232dccdd45SMarko Zec
4242dccdd45SMarko Zec imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
4252dccdd45SMarko Zec imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
4262dccdd45SMarko Zec }
4272dccdd45SMarko Zec
4282dccdd45SMarko Zec static int
epair_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)42998c230c8SBjoern A. Zeeb epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
43098c230c8SBjoern A. Zeeb {
4312dccdd45SMarko Zec struct epair_softc *sc;
43298c230c8SBjoern A. Zeeb struct ifreq *ifr;
43398c230c8SBjoern A. Zeeb int error;
43498c230c8SBjoern A. Zeeb
43598c230c8SBjoern A. Zeeb ifr = (struct ifreq *)data;
43698c230c8SBjoern A. Zeeb switch (cmd) {
43798c230c8SBjoern A. Zeeb case SIOCSIFFLAGS:
43898c230c8SBjoern A. Zeeb case SIOCADDMULTI:
43998c230c8SBjoern A. Zeeb case SIOCDELMULTI:
44098c230c8SBjoern A. Zeeb error = 0;
44198c230c8SBjoern A. Zeeb break;
44298c230c8SBjoern A. Zeeb
4432dccdd45SMarko Zec case SIOCSIFMEDIA:
4442dccdd45SMarko Zec case SIOCGIFMEDIA:
4452dccdd45SMarko Zec sc = ifp->if_softc;
4462dccdd45SMarko Zec error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
4472dccdd45SMarko Zec break;
4482dccdd45SMarko Zec
449d0ea4743SBjoern A. Zeeb case SIOCSIFMTU:
450d0ea4743SBjoern A. Zeeb /* We basically allow all kinds of MTUs. */
451d0ea4743SBjoern A. Zeeb ifp->if_mtu = ifr->ifr_mtu;
452d0ea4743SBjoern A. Zeeb error = 0;
453d0ea4743SBjoern A. Zeeb break;
454d0ea4743SBjoern A. Zeeb
45598c230c8SBjoern A. Zeeb default:
45698c230c8SBjoern A. Zeeb /* Let the common ethernet handler process this. */
45798c230c8SBjoern A. Zeeb error = ether_ioctl(ifp, cmd, data);
45898c230c8SBjoern A. Zeeb break;
45998c230c8SBjoern A. Zeeb }
46098c230c8SBjoern A. Zeeb
46198c230c8SBjoern A. Zeeb return (error);
46298c230c8SBjoern A. Zeeb }
46398c230c8SBjoern A. Zeeb
46498c230c8SBjoern A. Zeeb static void
epair_init(void * dummy __unused)46598c230c8SBjoern A. Zeeb epair_init(void *dummy __unused)
46698c230c8SBjoern A. Zeeb {
46798c230c8SBjoern A. Zeeb }
46898c230c8SBjoern A. Zeeb
46998c230c8SBjoern A. Zeeb /*
47098c230c8SBjoern A. Zeeb * Interface cloning functions.
47198c230c8SBjoern A. Zeeb * We use our private ones so that we can create/destroy our secondary
47298c230c8SBjoern A. Zeeb * device along with the primary one.
47398c230c8SBjoern A. Zeeb */
47498c230c8SBjoern A. Zeeb static int
epair_clone_match(struct if_clone * ifc,const char * name)47598c230c8SBjoern A. Zeeb epair_clone_match(struct if_clone *ifc, const char *name)
47698c230c8SBjoern A. Zeeb {
47798c230c8SBjoern A. Zeeb const char *cp;
47898c230c8SBjoern A. Zeeb
47998c230c8SBjoern A. Zeeb /*
48098c230c8SBjoern A. Zeeb * Our base name is epair.
48198c230c8SBjoern A. Zeeb * Our interfaces will be named epair<n>[ab].
48298c230c8SBjoern A. Zeeb * So accept anything of the following list:
48398c230c8SBjoern A. Zeeb * - epair
48498c230c8SBjoern A. Zeeb * - epair<n>
48598c230c8SBjoern A. Zeeb * but not the epair<n>[ab] versions.
48698c230c8SBjoern A. Zeeb */
48742a58907SGleb Smirnoff if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
48898c230c8SBjoern A. Zeeb return (0);
48998c230c8SBjoern A. Zeeb
49042a58907SGleb Smirnoff for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
49198c230c8SBjoern A. Zeeb if (*cp < '0' || *cp > '9')
49298c230c8SBjoern A. Zeeb return (0);
49398c230c8SBjoern A. Zeeb }
49498c230c8SBjoern A. Zeeb
49598c230c8SBjoern A. Zeeb return (1);
49698c230c8SBjoern A. Zeeb }
49798c230c8SBjoern A. Zeeb
498b02fd8b7SKristof Provost static void
epair_clone_add(struct if_clone * ifc,struct epair_softc * scb)499b02fd8b7SKristof Provost epair_clone_add(struct if_clone *ifc, struct epair_softc *scb)
500b02fd8b7SKristof Provost {
501b02fd8b7SKristof Provost struct ifnet *ifp;
502b02fd8b7SKristof Provost uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
503b02fd8b7SKristof Provost
504b02fd8b7SKristof Provost ifp = scb->ifp;
505b02fd8b7SKristof Provost /* Copy epairNa etheraddr and change the last byte. */
506b02fd8b7SKristof Provost memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
507b02fd8b7SKristof Provost eaddr[5] = 0x0b;
508b02fd8b7SKristof Provost ether_ifattach(ifp, eaddr);
509b02fd8b7SKristof Provost
510b02fd8b7SKristof Provost if_clone_addif(ifc, ifp);
511b02fd8b7SKristof Provost }
512b02fd8b7SKristof Provost
51312aeeb91SAlexander V. Chernikov static struct epair_softc *
epair_alloc_sc(struct if_clone * ifc)51412aeeb91SAlexander V. Chernikov epair_alloc_sc(struct if_clone *ifc)
51512aeeb91SAlexander V. Chernikov {
51612aeeb91SAlexander V. Chernikov struct epair_softc *sc;
51712aeeb91SAlexander V. Chernikov
51812aeeb91SAlexander V. Chernikov struct ifnet *ifp = if_alloc(IFT_ETHER);
51912aeeb91SAlexander V. Chernikov sc = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
52012aeeb91SAlexander V. Chernikov sc->ifp = ifp;
52112aeeb91SAlexander V. Chernikov sc->num_queues = epair_tasks.tasks;
52212aeeb91SAlexander V. Chernikov sc->queues = mallocarray(sc->num_queues, sizeof(struct epair_queue),
52312aeeb91SAlexander V. Chernikov M_EPAIR, M_WAITOK);
52412aeeb91SAlexander V. Chernikov for (int i = 0; i < sc->num_queues; i++) {
52512aeeb91SAlexander V. Chernikov struct epair_queue *q = &sc->queues[i];
52612aeeb91SAlexander V. Chernikov q->id = i;
527df7bbd8cSMark Johnston q->state = EPAIR_QUEUE_IDLE;
528df7bbd8cSMark Johnston mtx_init(&q->mtx, "epairq", NULL, MTX_DEF | MTX_NEW);
529df7bbd8cSMark Johnston mbufq_init(&q->q, RXRSIZE);
53012aeeb91SAlexander V. Chernikov q->sc = sc;
53112aeeb91SAlexander V. Chernikov NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q);
53212aeeb91SAlexander V. Chernikov }
53312aeeb91SAlexander V. Chernikov
53412aeeb91SAlexander V. Chernikov /* Initialise pseudo media types. */
53512aeeb91SAlexander V. Chernikov ifmedia_init(&sc->media, 0, epair_media_change, epair_media_status);
53612aeeb91SAlexander V. Chernikov ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
53712aeeb91SAlexander V. Chernikov ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T);
53812aeeb91SAlexander V. Chernikov
53912aeeb91SAlexander V. Chernikov return (sc);
54012aeeb91SAlexander V. Chernikov }
54112aeeb91SAlexander V. Chernikov
54212aeeb91SAlexander V. Chernikov static void
epair_setup_ifp(struct epair_softc * sc,char * name,int unit)54312aeeb91SAlexander V. Chernikov epair_setup_ifp(struct epair_softc *sc, char *name, int unit)
54412aeeb91SAlexander V. Chernikov {
54512aeeb91SAlexander V. Chernikov struct ifnet *ifp = sc->ifp;
54612aeeb91SAlexander V. Chernikov
54712aeeb91SAlexander V. Chernikov ifp->if_softc = sc;
54812aeeb91SAlexander V. Chernikov strlcpy(ifp->if_xname, name, IFNAMSIZ);
54912aeeb91SAlexander V. Chernikov ifp->if_dname = epairname;
55012aeeb91SAlexander V. Chernikov ifp->if_dunit = unit;
55112aeeb91SAlexander V. Chernikov ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
55212aeeb91SAlexander V. Chernikov ifp->if_capabilities = IFCAP_VLAN_MTU;
55312aeeb91SAlexander V. Chernikov ifp->if_capenable = IFCAP_VLAN_MTU;
55412aeeb91SAlexander V. Chernikov ifp->if_transmit = epair_transmit;
55512aeeb91SAlexander V. Chernikov ifp->if_qflush = epair_qflush;
55612aeeb91SAlexander V. Chernikov ifp->if_start = epair_start;
55712aeeb91SAlexander V. Chernikov ifp->if_ioctl = epair_ioctl;
55812aeeb91SAlexander V. Chernikov ifp->if_init = epair_init;
55912aeeb91SAlexander V. Chernikov if_setsendqlen(ifp, ifqmaxlen);
56012aeeb91SAlexander V. Chernikov if_setsendqready(ifp);
56112aeeb91SAlexander V. Chernikov
56212aeeb91SAlexander V. Chernikov ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
56312aeeb91SAlexander V. Chernikov }
56412aeeb91SAlexander V. Chernikov
56512aeeb91SAlexander V. Chernikov static void
epair_generate_mac(struct epair_softc * sc,uint8_t * eaddr)56612aeeb91SAlexander V. Chernikov epair_generate_mac(struct epair_softc *sc, uint8_t *eaddr)
56712aeeb91SAlexander V. Chernikov {
56812aeeb91SAlexander V. Chernikov uint32_t key[3];
56912aeeb91SAlexander V. Chernikov uint32_t hash;
57012aeeb91SAlexander V. Chernikov uint64_t hostid;
57112aeeb91SAlexander V. Chernikov
57212aeeb91SAlexander V. Chernikov EPAIR_LOCK();
57312aeeb91SAlexander V. Chernikov #ifdef SMP
57412aeeb91SAlexander V. Chernikov /* Get an approximate distribution. */
57512aeeb91SAlexander V. Chernikov hash = next_index % mp_ncpus;
57612aeeb91SAlexander V. Chernikov #else
57712aeeb91SAlexander V. Chernikov hash = 0;
57812aeeb91SAlexander V. Chernikov #endif
57912aeeb91SAlexander V. Chernikov EPAIR_UNLOCK();
58012aeeb91SAlexander V. Chernikov
58112aeeb91SAlexander V. Chernikov /*
58212aeeb91SAlexander V. Chernikov * Calculate the etheraddr hashing the hostid and the
58312aeeb91SAlexander V. Chernikov * interface index. The result would be hopefully unique.
58412aeeb91SAlexander V. Chernikov * Note that the "a" component of an epair instance may get moved
58512aeeb91SAlexander V. Chernikov * to a different VNET after creation. In that case its index
58612aeeb91SAlexander V. Chernikov * will be freed and the index can get reused by new epair instance.
58712aeeb91SAlexander V. Chernikov * Make sure we do not create same etheraddr again.
58812aeeb91SAlexander V. Chernikov */
58912aeeb91SAlexander V. Chernikov getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
59012aeeb91SAlexander V. Chernikov if (hostid == 0)
59112aeeb91SAlexander V. Chernikov arc4rand(&hostid, sizeof(hostid), 0);
59212aeeb91SAlexander V. Chernikov
59312aeeb91SAlexander V. Chernikov struct ifnet *ifp = sc->ifp;
59412aeeb91SAlexander V. Chernikov EPAIR_LOCK();
59512aeeb91SAlexander V. Chernikov if (ifp->if_index > next_index)
59612aeeb91SAlexander V. Chernikov next_index = ifp->if_index;
59712aeeb91SAlexander V. Chernikov else
59812aeeb91SAlexander V. Chernikov next_index++;
59912aeeb91SAlexander V. Chernikov
60012aeeb91SAlexander V. Chernikov key[0] = (uint32_t)next_index;
60112aeeb91SAlexander V. Chernikov EPAIR_UNLOCK();
60212aeeb91SAlexander V. Chernikov key[1] = (uint32_t)(hostid & 0xffffffff);
60312aeeb91SAlexander V. Chernikov key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
60412aeeb91SAlexander V. Chernikov hash = jenkins_hash32(key, 3, 0);
60512aeeb91SAlexander V. Chernikov
60612aeeb91SAlexander V. Chernikov eaddr[0] = 0x02;
60712aeeb91SAlexander V. Chernikov memcpy(&eaddr[1], &hash, 4);
60812aeeb91SAlexander V. Chernikov eaddr[5] = 0x0a;
60912aeeb91SAlexander V. Chernikov }
61012aeeb91SAlexander V. Chernikov
61112aeeb91SAlexander V. Chernikov static void
epair_free_sc(struct epair_softc * sc)61212aeeb91SAlexander V. Chernikov epair_free_sc(struct epair_softc *sc)
61312aeeb91SAlexander V. Chernikov {
61412aeeb91SAlexander V. Chernikov
61512aeeb91SAlexander V. Chernikov if_free(sc->ifp);
61612aeeb91SAlexander V. Chernikov ifmedia_removeall(&sc->media);
61712aeeb91SAlexander V. Chernikov for (int i = 0; i < sc->num_queues; i++) {
61812aeeb91SAlexander V. Chernikov struct epair_queue *q = &sc->queues[i];
619df7bbd8cSMark Johnston mtx_destroy(&q->mtx);
62012aeeb91SAlexander V. Chernikov }
62112aeeb91SAlexander V. Chernikov free(sc->queues, M_EPAIR);
62212aeeb91SAlexander V. Chernikov free(sc, M_EPAIR);
62312aeeb91SAlexander V. Chernikov }
62412aeeb91SAlexander V. Chernikov
62504a32b80SAlexander V. Chernikov static void
epair_set_state(struct ifnet * ifp,bool running)62604a32b80SAlexander V. Chernikov epair_set_state(struct ifnet *ifp, bool running)
62798c230c8SBjoern A. Zeeb {
62804a32b80SAlexander V. Chernikov if (running) {
62904a32b80SAlexander V. Chernikov ifp->if_drv_flags |= IFF_DRV_RUNNING;
63004a32b80SAlexander V. Chernikov if_link_state_change(ifp, LINK_STATE_UP);
63104a32b80SAlexander V. Chernikov } else {
63204a32b80SAlexander V. Chernikov if_link_state_change(ifp, LINK_STATE_DOWN);
63304a32b80SAlexander V. Chernikov ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
63404a32b80SAlexander V. Chernikov }
63504a32b80SAlexander V. Chernikov }
63604a32b80SAlexander V. Chernikov
63704a32b80SAlexander V. Chernikov static int
epair_handle_unit(struct if_clone * ifc,char * name,size_t len,int * punit)63804a32b80SAlexander V. Chernikov epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
63904a32b80SAlexander V. Chernikov {
64004a32b80SAlexander V. Chernikov int error = 0, unit, wildcard;
64198c230c8SBjoern A. Zeeb char *dp;
64298c230c8SBjoern A. Zeeb
64398c230c8SBjoern A. Zeeb /* Try to see if a special unit was requested. */
64498c230c8SBjoern A. Zeeb error = ifc_name2unit(name, &unit);
64598c230c8SBjoern A. Zeeb if (error != 0)
64698c230c8SBjoern A. Zeeb return (error);
64798c230c8SBjoern A. Zeeb wildcard = (unit < 0);
64898c230c8SBjoern A. Zeeb
64998c230c8SBjoern A. Zeeb error = ifc_alloc_unit(ifc, &unit);
65098c230c8SBjoern A. Zeeb if (error != 0)
65198c230c8SBjoern A. Zeeb return (error);
65298c230c8SBjoern A. Zeeb
65398c230c8SBjoern A. Zeeb /*
65498c230c8SBjoern A. Zeeb * If no unit had been given, we need to adjust the ifName.
65598c230c8SBjoern A. Zeeb * Also make sure there is space for our extra [ab] suffix.
65698c230c8SBjoern A. Zeeb */
65798c230c8SBjoern A. Zeeb for (dp = name; *dp != '\0'; dp++);
65898c230c8SBjoern A. Zeeb if (wildcard) {
65904a32b80SAlexander V. Chernikov int slen = snprintf(dp, len - (dp - name), "%d", unit);
66004a32b80SAlexander V. Chernikov if (slen > len - (dp - name) - 1) {
66198c230c8SBjoern A. Zeeb /* ifName too long. */
66204a32b80SAlexander V. Chernikov error = ENOSPC;
66304a32b80SAlexander V. Chernikov goto done;
66498c230c8SBjoern A. Zeeb }
66504a32b80SAlexander V. Chernikov dp += slen;
66698c230c8SBjoern A. Zeeb }
66798c230c8SBjoern A. Zeeb if (len - (dp - name) - 1 < 1) {
66898c230c8SBjoern A. Zeeb /* No space left for our [ab] suffix. */
66904a32b80SAlexander V. Chernikov error = ENOSPC;
67004a32b80SAlexander V. Chernikov goto done;
67198c230c8SBjoern A. Zeeb }
6723c3136b1SHiroki Sato *dp = 'b';
67398c230c8SBjoern A. Zeeb /* Must not change dp so we can replace 'a' by 'b' later. */
67498c230c8SBjoern A. Zeeb *(dp+1) = '\0';
67598c230c8SBjoern A. Zeeb
6763c3136b1SHiroki Sato /* Check if 'a' and 'b' interfaces already exist. */
67704a32b80SAlexander V. Chernikov if (ifunit(name) != NULL) {
67804a32b80SAlexander V. Chernikov error = EEXIST;
67904a32b80SAlexander V. Chernikov goto done;
68004a32b80SAlexander V. Chernikov }
68104a32b80SAlexander V. Chernikov
6823c3136b1SHiroki Sato *dp = 'a';
68304a32b80SAlexander V. Chernikov if (ifunit(name) != NULL) {
68404a32b80SAlexander V. Chernikov error = EEXIST;
68504a32b80SAlexander V. Chernikov goto done;
68604a32b80SAlexander V. Chernikov }
68704a32b80SAlexander V. Chernikov *punit = unit;
68804a32b80SAlexander V. Chernikov done:
68904a32b80SAlexander V. Chernikov if (error != 0)
69004a32b80SAlexander V. Chernikov ifc_free_unit(ifc, unit);
69104a32b80SAlexander V. Chernikov
69204a32b80SAlexander V. Chernikov return (error);
69304a32b80SAlexander V. Chernikov }
69404a32b80SAlexander V. Chernikov
69504a32b80SAlexander V. Chernikov static int
epair_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)69604a32b80SAlexander V. Chernikov epair_clone_create(struct if_clone *ifc, char *name, size_t len,
69704a32b80SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp)
69804a32b80SAlexander V. Chernikov {
69904a32b80SAlexander V. Chernikov struct epair_softc *sca, *scb;
70004a32b80SAlexander V. Chernikov struct ifnet *ifp;
70104a32b80SAlexander V. Chernikov char *dp;
70204a32b80SAlexander V. Chernikov int error, unit;
70304a32b80SAlexander V. Chernikov uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
70404a32b80SAlexander V. Chernikov
70504a32b80SAlexander V. Chernikov error = epair_handle_unit(ifc, name, len, &unit);
70604a32b80SAlexander V. Chernikov if (error != 0)
70704a32b80SAlexander V. Chernikov return (error);
7083c3136b1SHiroki Sato
70998c230c8SBjoern A. Zeeb /* Allocate memory for both [ab] interfaces */
71012aeeb91SAlexander V. Chernikov sca = epair_alloc_sc(ifc);
71112aeeb91SAlexander V. Chernikov scb = epair_alloc_sc(ifc);
71298c230c8SBjoern A. Zeeb
71398c230c8SBjoern A. Zeeb /*
71498c230c8SBjoern A. Zeeb * Cross-reference the interfaces so we will be able to free both.
71598c230c8SBjoern A. Zeeb */
71698c230c8SBjoern A. Zeeb sca->oifp = scb->ifp;
71798c230c8SBjoern A. Zeeb scb->oifp = sca->ifp;
71898c230c8SBjoern A. Zeeb
71998c230c8SBjoern A. Zeeb /* Finish initialization of interface <n>a. */
72098c230c8SBjoern A. Zeeb ifp = sca->ifp;
72112aeeb91SAlexander V. Chernikov epair_setup_ifp(sca, name, unit);
72212aeeb91SAlexander V. Chernikov epair_generate_mac(sca, eaddr);
72311d41666SLuca Pizzamiglio
72498c230c8SBjoern A. Zeeb ether_ifattach(ifp, eaddr);
72598c230c8SBjoern A. Zeeb
72698c230c8SBjoern A. Zeeb /* Swap the name and finish initialization of interface <n>b. */
72704a32b80SAlexander V. Chernikov dp = name + strlen(name) - 1;
72898c230c8SBjoern A. Zeeb *dp = 'b';
72998c230c8SBjoern A. Zeeb
73012aeeb91SAlexander V. Chernikov epair_setup_ifp(scb, name, unit);
73112aeeb91SAlexander V. Chernikov
73298c230c8SBjoern A. Zeeb ifp = scb->ifp;
73398c230c8SBjoern A. Zeeb /* We need to play some tricks here for the second interface. */
73442a58907SGleb Smirnoff strlcpy(name, epairname, len);
735b02fd8b7SKristof Provost /* Correctly set the name for the cloner list. */
736b02fd8b7SKristof Provost strlcpy(name, scb->ifp->if_xname, len);
737b02fd8b7SKristof Provost
73812aeeb91SAlexander V. Chernikov epair_clone_add(ifc, scb);
73998c230c8SBjoern A. Zeeb
74098c230c8SBjoern A. Zeeb /*
74198c230c8SBjoern A. Zeeb * Restore name to <n>a as the ifp for this will go into the
74298c230c8SBjoern A. Zeeb * cloner list for the initial call.
74398c230c8SBjoern A. Zeeb */
74498c230c8SBjoern A. Zeeb strlcpy(name, sca->ifp->if_xname, len);
74598c230c8SBjoern A. Zeeb
74698c230c8SBjoern A. Zeeb /* Tell the world, that we are ready to rock. */
74704a32b80SAlexander V. Chernikov epair_set_state(sca->ifp, true);
74804a32b80SAlexander V. Chernikov epair_set_state(scb->ifp, true);
74998c230c8SBjoern A. Zeeb
75091ebcbe0SAlexander V. Chernikov *ifpp = sca->ifp;
75191ebcbe0SAlexander V. Chernikov
75298c230c8SBjoern A. Zeeb return (0);
75398c230c8SBjoern A. Zeeb }
75498c230c8SBjoern A. Zeeb
7553dd5760aSBjoern A. Zeeb static void
epair_drain_rings(struct epair_softc * sc)7563dd5760aSBjoern A. Zeeb epair_drain_rings(struct epair_softc *sc)
7573dd5760aSBjoern A. Zeeb {
75824f0bfbaSKristof Provost for (int i = 0; i < sc->num_queues; i++) {
759df7bbd8cSMark Johnston struct epair_queue *q;
760df7bbd8cSMark Johnston struct mbuf *m, *n;
761df7bbd8cSMark Johnston
762df7bbd8cSMark Johnston q = &sc->queues[i];
763df7bbd8cSMark Johnston mtx_lock(&q->mtx);
764df7bbd8cSMark Johnston m = mbufq_flush(&q->q);
765df7bbd8cSMark Johnston mtx_unlock(&q->mtx);
766df7bbd8cSMark Johnston
767df7bbd8cSMark Johnston for (; m != NULL; m = n) {
768df7bbd8cSMark Johnston n = m->m_nextpkt;
7693dd5760aSBjoern A. Zeeb m_freem(m);
7703dd5760aSBjoern A. Zeeb }
7713dd5760aSBjoern A. Zeeb }
77224f0bfbaSKristof Provost }
7733dd5760aSBjoern A. Zeeb
77498c230c8SBjoern A. Zeeb static int
epair_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)77591ebcbe0SAlexander V. Chernikov epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
77698c230c8SBjoern A. Zeeb {
77798c230c8SBjoern A. Zeeb struct ifnet *oifp;
77898c230c8SBjoern A. Zeeb struct epair_softc *sca, *scb;
77998c230c8SBjoern A. Zeeb int unit, error;
78098c230c8SBjoern A. Zeeb
78198c230c8SBjoern A. Zeeb /*
78298c230c8SBjoern A. Zeeb * In case we called into if_clone_destroyif() ourselves
78398c230c8SBjoern A. Zeeb * again to remove the second interface, the softc will be
78498c230c8SBjoern A. Zeeb * NULL. In that case so not do anything but return success.
78598c230c8SBjoern A. Zeeb */
78698c230c8SBjoern A. Zeeb if (ifp->if_softc == NULL)
78798c230c8SBjoern A. Zeeb return (0);
78898c230c8SBjoern A. Zeeb
78998c230c8SBjoern A. Zeeb unit = ifp->if_dunit;
79098c230c8SBjoern A. Zeeb sca = ifp->if_softc;
79198c230c8SBjoern A. Zeeb oifp = sca->oifp;
79298c230c8SBjoern A. Zeeb scb = oifp->if_softc;
79398c230c8SBjoern A. Zeeb
7943dd5760aSBjoern A. Zeeb /* Frist get the interfaces down and detached. */
79504a32b80SAlexander V. Chernikov epair_set_state(ifp, false);
79604a32b80SAlexander V. Chernikov epair_set_state(oifp, false);
79798c230c8SBjoern A. Zeeb
7983dd5760aSBjoern A. Zeeb ether_ifdetach(ifp);
7997edc3d88SMikolaj Golub ether_ifdetach(oifp);
8003dd5760aSBjoern A. Zeeb
8013dd5760aSBjoern A. Zeeb /* Third free any queued packets and all the resources. */
8023dd5760aSBjoern A. Zeeb CURVNET_SET_QUIET(oifp->if_vnet);
8033dd5760aSBjoern A. Zeeb epair_drain_rings(scb);
80498c230c8SBjoern A. Zeeb oifp->if_softc = NULL;
80598c230c8SBjoern A. Zeeb error = if_clone_destroyif(ifc, oifp);
80698c230c8SBjoern A. Zeeb if (error)
80798c230c8SBjoern A. Zeeb panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
80898c230c8SBjoern A. Zeeb __func__, error);
80912aeeb91SAlexander V. Chernikov epair_free_sc(scb);
8107edc3d88SMikolaj Golub CURVNET_RESTORE();
8117edc3d88SMikolaj Golub
8123dd5760aSBjoern A. Zeeb epair_drain_rings(sca);
81312aeeb91SAlexander V. Chernikov epair_free_sc(sca);
8143dd5760aSBjoern A. Zeeb
8153dd5760aSBjoern A. Zeeb /* Last free the cloner unit. */
81698c230c8SBjoern A. Zeeb ifc_free_unit(ifc, unit);
81798c230c8SBjoern A. Zeeb
81898c230c8SBjoern A. Zeeb return (0);
81998c230c8SBjoern A. Zeeb }
82098c230c8SBjoern A. Zeeb
8213c3136b1SHiroki Sato static void
vnet_epair_init(const void * unused __unused)8223c3136b1SHiroki Sato vnet_epair_init(const void *unused __unused)
8233c3136b1SHiroki Sato {
82491ebcbe0SAlexander V. Chernikov struct if_clone_addreq req = {
82591ebcbe0SAlexander V. Chernikov .match_f = epair_clone_match,
82691ebcbe0SAlexander V. Chernikov .create_f = epair_clone_create,
82791ebcbe0SAlexander V. Chernikov .destroy_f = epair_clone_destroy,
82891ebcbe0SAlexander V. Chernikov };
82991ebcbe0SAlexander V. Chernikov V_epair_cloner = ifc_attach_cloner(epairname, &req);
8303c3136b1SHiroki Sato }
83189856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
8323c3136b1SHiroki Sato vnet_epair_init, NULL);
8333c3136b1SHiroki Sato
8343c3136b1SHiroki Sato static void
vnet_epair_uninit(const void * unused __unused)8353c3136b1SHiroki Sato vnet_epair_uninit(const void *unused __unused)
8363c3136b1SHiroki Sato {
8373c3136b1SHiroki Sato
83891ebcbe0SAlexander V. Chernikov ifc_detach_cloner(V_epair_cloner);
8393c3136b1SHiroki Sato }
84089856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
8413c3136b1SHiroki Sato vnet_epair_uninit, NULL);
8423c3136b1SHiroki Sato
84398c230c8SBjoern A. Zeeb static int
epair_mod_init(void)8447442b632SLi-Wen Hsu epair_mod_init(void)
84524f0bfbaSKristof Provost {
84624f0bfbaSKristof Provost char name[32];
84724f0bfbaSKristof Provost epair_tasks.tasks = 0;
84824f0bfbaSKristof Provost
84924f0bfbaSKristof Provost #ifdef RSS
85024f0bfbaSKristof Provost int cpu;
85124f0bfbaSKristof Provost
85224f0bfbaSKristof Provost CPU_FOREACH(cpu) {
85324f0bfbaSKristof Provost cpuset_t cpu_mask;
85424f0bfbaSKristof Provost
85524f0bfbaSKristof Provost /* Pin to this CPU so we get appropriate NUMA allocations. */
85624f0bfbaSKristof Provost thread_lock(curthread);
85724f0bfbaSKristof Provost sched_bind(curthread, cpu);
85824f0bfbaSKristof Provost thread_unlock(curthread);
85924f0bfbaSKristof Provost
86024f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task_%d", cpu);
86124f0bfbaSKristof Provost
86224f0bfbaSKristof Provost epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK,
86324f0bfbaSKristof Provost taskqueue_thread_enqueue,
86424f0bfbaSKristof Provost &epair_tasks.tq[cpu]);
86524f0bfbaSKristof Provost CPU_SETOF(cpu, &cpu_mask);
86624f0bfbaSKristof Provost taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET,
86724f0bfbaSKristof Provost &cpu_mask, "%s", name);
86824f0bfbaSKristof Provost
86924f0bfbaSKristof Provost epair_tasks.tasks++;
87024f0bfbaSKristof Provost }
871cbbce423SKristof Provost thread_lock(curthread);
872cbbce423SKristof Provost sched_unbind(curthread);
873cbbce423SKristof Provost thread_unlock(curthread);
87424f0bfbaSKristof Provost #else
87524f0bfbaSKristof Provost snprintf(name, sizeof(name), "epair_task");
87624f0bfbaSKristof Provost
87724f0bfbaSKristof Provost epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK,
87824f0bfbaSKristof Provost taskqueue_thread_enqueue,
87924f0bfbaSKristof Provost &epair_tasks.tq[0]);
88024f0bfbaSKristof Provost taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name);
88124f0bfbaSKristof Provost
88224f0bfbaSKristof Provost epair_tasks.tasks = 1;
88324f0bfbaSKristof Provost #endif
88424f0bfbaSKristof Provost
88524f0bfbaSKristof Provost return (0);
88624f0bfbaSKristof Provost }
88724f0bfbaSKristof Provost
88824f0bfbaSKristof Provost static void
epair_mod_cleanup(void)8897442b632SLi-Wen Hsu epair_mod_cleanup(void)
89024f0bfbaSKristof Provost {
89124f0bfbaSKristof Provost
89224f0bfbaSKristof Provost for (int i = 0; i < epair_tasks.tasks; i++) {
89324f0bfbaSKristof Provost taskqueue_drain_all(epair_tasks.tq[i]);
89424f0bfbaSKristof Provost taskqueue_free(epair_tasks.tq[i]);
89524f0bfbaSKristof Provost }
89624f0bfbaSKristof Provost }
89724f0bfbaSKristof Provost
89824f0bfbaSKristof Provost static int
epair_modevent(module_t mod,int type,void * data)89998c230c8SBjoern A. Zeeb epair_modevent(module_t mod, int type, void *data)
90098c230c8SBjoern A. Zeeb {
90124f0bfbaSKristof Provost int ret;
90298c230c8SBjoern A. Zeeb
90398c230c8SBjoern A. Zeeb switch (type) {
90498c230c8SBjoern A. Zeeb case MOD_LOAD:
9053dd5760aSBjoern A. Zeeb EPAIR_LOCK_INIT();
90624f0bfbaSKristof Provost ret = epair_mod_init();
90724f0bfbaSKristof Provost if (ret != 0)
90824f0bfbaSKristof Provost return (ret);
90998c230c8SBjoern A. Zeeb if (bootverbose)
9103dd5760aSBjoern A. Zeeb printf("%s: %s initialized.\n", __func__, epairname);
91198c230c8SBjoern A. Zeeb break;
91298c230c8SBjoern A. Zeeb case MOD_UNLOAD:
91324f0bfbaSKristof Provost epair_mod_cleanup();
9143dd5760aSBjoern A. Zeeb EPAIR_LOCK_DESTROY();
9153dd5760aSBjoern A. Zeeb if (bootverbose)
9163dd5760aSBjoern A. Zeeb printf("%s: %s unloaded.\n", __func__, epairname);
91798c230c8SBjoern A. Zeeb break;
91898c230c8SBjoern A. Zeeb default:
91998c230c8SBjoern A. Zeeb return (EOPNOTSUPP);
92098c230c8SBjoern A. Zeeb }
92198c230c8SBjoern A. Zeeb return (0);
92298c230c8SBjoern A. Zeeb }
92398c230c8SBjoern A. Zeeb
92498c230c8SBjoern A. Zeeb static moduledata_t epair_mod = {
92598c230c8SBjoern A. Zeeb "if_epair",
92698c230c8SBjoern A. Zeeb epair_modevent,
9279823d527SKevin Lo 0
92898c230c8SBjoern A. Zeeb };
92998c230c8SBjoern A. Zeeb
93089856f7eSBjoern A. Zeeb DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
9313dd5760aSBjoern A. Zeeb MODULE_VERSION(if_epair, 3);
932