]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_epair.c
unlink_nhgrp: Remove write-only variable.
[FreeBSD/FreeBSD.git] / sys / net / if_epair.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 The FreeBSD Foundation
5  * Copyright (c) 2009-2021 Bjoern A. Zeeb <bz@FreeBSD.org>
6  *
7  * This software was developed by CK Software GmbH under sponsorship
8  * from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * A pair of virtual back-to-back connected ethernet like interfaces
34  * (``two interfaces with a virtual cross-over cable'').
35  *
36  * This is mostly intended to be used to provide connectivity between
37  * different virtual network stack instances.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include "opt_rss.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46
47 #include <sys/param.h>
48 #include <sys/hash.h>
49 #include <sys/jail.h>
50 #include <sys/kernel.h>
51 #include <sys/libkern.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/module.h>
55 #include <sys/proc.h>
56 #include <sys/queue.h>
57 #include <sys/sched.h>
58 #include <sys/smp.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/taskqueue.h>
62 #include <sys/types.h>
63 #include <sys/buf_ring.h>
64 #include <sys/bus.h>
65 #include <sys/interrupt.h>
66
67 #include <net/bpf.h>
68 #include <net/ethernet.h>
69 #include <net/if.h>
70 #include <net/if_var.h>
71 #include <net/if_clone.h>
72 #include <net/if_media.h>
73 #include <net/if_var.h>
74 #include <net/if_types.h>
75 #include <net/netisr.h>
76 #ifdef RSS
77 #include <net/rss_config.h>
78 #ifdef INET
79 #include <netinet/in_rss.h>
80 #endif
81 #ifdef INET6
82 #include <netinet6/in6_rss.h>
83 #endif
84 #endif
85 #include <net/vnet.h>
86
87 static int epair_clone_match(struct if_clone *, const char *);
88 static int epair_clone_create(struct if_clone *, char *, size_t, caddr_t);
89 static int epair_clone_destroy(struct if_clone *, struct ifnet *);
90
91 static const char epairname[] = "epair";
92 #define RXRSIZE 4096    /* Probably overkill by 4-8x. */
93
94 static MALLOC_DEFINE(M_EPAIR, epairname,
95     "Pair of virtual cross-over connected Ethernet-like interfaces");
96
97 VNET_DEFINE_STATIC(struct if_clone *, epair_cloner);
98 #define V_epair_cloner  VNET(epair_cloner)
99
100 static unsigned int next_index = 0;
101 #define EPAIR_LOCK_INIT()               mtx_init(&epair_n_index_mtx, "epairidx", \
102                                             NULL, MTX_DEF)
103 #define EPAIR_LOCK_DESTROY()            mtx_destroy(&epair_n_index_mtx)
104 #define EPAIR_LOCK()                    mtx_lock(&epair_n_index_mtx)
105 #define EPAIR_UNLOCK()                  mtx_unlock(&epair_n_index_mtx)
106
107 #define BIT_QUEUE_TASK          0
108 #define BIT_MBUF_QUEUED         1
109
110 struct epair_softc;
111 struct epair_queue {
112         int                      id;
113         struct buf_ring         *rxring[2];
114         volatile int             ridx;          /* 0 || 1 */
115         volatile long            state;         /* taskqueue coordination */
116         struct task              tx_task;
117         struct epair_softc      *sc;
118 };
119
120 static struct mtx epair_n_index_mtx;
121 struct epair_softc {
122         struct ifnet            *ifp;           /* This ifp. */
123         struct ifnet            *oifp;          /* other ifp of pair. */
124         int                      num_queues;
125         struct epair_queue      *queues;
126         struct ifmedia           media;         /* Media config (fake). */
127         STAILQ_ENTRY(epair_softc) entry;
128 };
129
130 struct epair_tasks_t {
131         int                      tasks;
132         struct taskqueue         *tq[MAXCPU];
133 };
134
135 static struct epair_tasks_t epair_tasks;
136
137 static void
138 epair_clear_mbuf(struct mbuf *m)
139 {
140         /* Remove any CSUM_SND_TAG as ether_input will barf. */
141         if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
142                 m_snd_tag_rele(m->m_pkthdr.snd_tag);
143                 m->m_pkthdr.snd_tag = NULL;
144                 m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
145         }
146
147         m_tag_delete_nonpersistent(m);
148 }
149
150 static void
151 epair_if_input(struct epair_softc *sc, struct epair_queue *q, int ridx)
152 {
153         struct ifnet *ifp;
154         struct mbuf *m;
155
156         ifp = sc->ifp;
157         CURVNET_SET(ifp->if_vnet);
158         while (! buf_ring_empty(q->rxring[ridx])) {
159                 m = buf_ring_dequeue_mc(q->rxring[ridx]);
160                 if (m == NULL)
161                         continue;
162
163                 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
164                 (*ifp->if_input)(ifp, m);
165
166         }
167         CURVNET_RESTORE();
168 }
169
170 static void
171 epair_tx_start_deferred(void *arg, int pending)
172 {
173         struct epair_queue *q = (struct epair_queue *)arg;
174         struct epair_softc *sc = q->sc;
175         int ridx, nidx;
176
177         if_ref(sc->ifp);
178         ridx = atomic_load_int(&q->ridx);
179         do {
180                 nidx = (ridx == 0) ? 1 : 0;
181         } while (!atomic_fcmpset_int(&q->ridx, &ridx, nidx));
182         epair_if_input(sc, q, ridx);
183
184         atomic_clear_long(&q->state, (1 << BIT_QUEUE_TASK));
185         if (atomic_testandclear_long(&q->state, BIT_MBUF_QUEUED))
186                 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
187
188         if_rele(sc->ifp);
189 }
190
191 static int
192 epair_menq(struct mbuf *m, struct epair_softc *osc)
193 {
194         struct ifnet *ifp, *oifp;
195         int len, ret;
196         int ridx;
197         short mflags;
198         struct epair_queue *q = NULL;
199         uint32_t bucket;
200 #ifdef RSS
201         struct ether_header *eh;
202 #endif
203
204         /*
205          * I know this looks weird. We pass the "other sc" as we need that one
206          * and can get both ifps from it as well.
207          */
208         oifp = osc->ifp;
209         ifp = osc->oifp;
210
211         M_ASSERTPKTHDR(m);
212         epair_clear_mbuf(m);
213         if_setrcvif(m, oifp);
214         M_SETFIB(m, oifp->if_fib);
215
216         /* Save values as once the mbuf is queued, it's not ours anymore. */
217         len = m->m_pkthdr.len;
218         mflags = m->m_flags;
219
220         MPASS(m->m_nextpkt == NULL);
221         MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
222
223 #ifdef RSS
224         ret = rss_m2bucket(m, &bucket);
225         if (ret) {
226                 /* Actually hash the packet. */
227                 eh = mtod(m, struct ether_header *);
228
229                 switch (ntohs(eh->ether_type)) {
230 #ifdef INET
231                 case ETHERTYPE_IP:
232                         rss_soft_m2cpuid_v4(m, 0, &bucket);
233                         break;
234 #endif
235 #ifdef INET6
236                 case ETHERTYPE_IPV6:
237                         rss_soft_m2cpuid_v6(m, 0, &bucket);
238                         break;
239 #endif
240                 default:
241                         bucket = 0;
242                         break;
243                 }
244         }
245         bucket %= osc->num_queues;
246 #else
247         bucket = 0;
248 #endif
249         q = &osc->queues[bucket];
250
251         atomic_set_long(&q->state, (1 << BIT_MBUF_QUEUED));
252         ridx = atomic_load_int(&q->ridx);
253         ret = buf_ring_enqueue(q->rxring[ridx], m);
254         if (ret != 0) {
255                 /* Ring is full. */
256                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
257                 m_freem(m);
258                 return (0);
259         }
260
261         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
262         /*
263          * IFQ_HANDOFF_ADJ/ip_handoff() update statistics,
264          * but as we bypass all this we have to duplicate
265          * the logic another time.
266          */
267         if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
268         if (mflags & (M_BCAST|M_MCAST))
269                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
270         /* Someone else received the packet. */
271         if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
272
273         if (!atomic_testandset_long(&q->state, BIT_QUEUE_TASK))
274                 taskqueue_enqueue(epair_tasks.tq[bucket], &q->tx_task);
275
276         return (0);
277 }
278
279 static void
280 epair_start(struct ifnet *ifp)
281 {
282         struct mbuf *m;
283         struct epair_softc *sc;
284         struct ifnet *oifp;
285
286         /*
287          * We get packets here from ether_output via if_handoff()
288          * and need to put them into the input queue of the oifp
289          * and will put the packet into the receive-queue (rxq) of the
290          * other interface (oifp) of our pair.
291          */
292         sc = ifp->if_softc;
293         oifp = sc->oifp;
294         sc = oifp->if_softc;
295         for (;;) {
296                 IFQ_DEQUEUE(&ifp->if_snd, m);
297                 if (m == NULL)
298                         break;
299                 M_ASSERTPKTHDR(m);
300                 BPF_MTAP(ifp, m);
301
302                 /* In case either interface is not usable drop the packet. */
303                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
304                     (ifp->if_flags & IFF_UP) == 0 ||
305                     (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
306                     (oifp->if_flags & IFF_UP) == 0) {
307                         m_freem(m);
308                         continue;
309                 }
310
311                 (void) epair_menq(m, sc);
312         }
313 }
314
315 static int
316 epair_transmit(struct ifnet *ifp, struct mbuf *m)
317 {
318         struct epair_softc *sc;
319         struct ifnet *oifp;
320         int error, len;
321         short mflags;
322
323         if (m == NULL)
324                 return (0);
325         M_ASSERTPKTHDR(m);
326
327         /*
328          * We are not going to use the interface en/dequeue mechanism
329          * on the TX side. We are called from ether_output_frame()
330          * and will put the packet into the receive-queue (rxq) of the
331          * other interface (oifp) of our pair.
332          */
333         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
334                 m_freem(m);
335                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
336                 return (ENXIO);
337         }
338         if ((ifp->if_flags & IFF_UP) == 0) {
339                 m_freem(m);
340                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
341                 return (ENETDOWN);
342         }
343
344         BPF_MTAP(ifp, m);
345
346         /*
347          * In case the outgoing interface is not usable,
348          * drop the packet.
349          */
350         sc = ifp->if_softc;
351         oifp = sc->oifp;
352         if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
353             (oifp->if_flags & IFF_UP) == 0) {
354                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
355                 m_freem(m);
356                 return (0);
357         }
358         len = m->m_pkthdr.len;
359         mflags = m->m_flags;
360
361 #ifdef ALTQ
362         /* Support ALTQ via the classic if_start() path. */
363         IF_LOCK(&ifp->if_snd);
364         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
365                 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
366                 if (error)
367                         if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
368                 IF_UNLOCK(&ifp->if_snd);
369                 if (!error) {
370                         if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
371                         if (mflags & (M_BCAST|M_MCAST))
372                                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
373                         epair_start(ifp);
374                 }
375                 return (error);
376         }
377         IF_UNLOCK(&ifp->if_snd);
378 #endif
379
380         error = epair_menq(m, oifp->if_softc);
381         return (error);
382 }
383
384 static int
385 epair_media_change(struct ifnet *ifp __unused)
386 {
387
388         /* Do nothing. */
389         return (0);
390 }
391
392 static void
393 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
394 {
395
396         imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
397         imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
398 }
399
400 static int
401 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
402 {
403         struct epair_softc *sc;
404         struct ifreq *ifr;
405         int error;
406
407         ifr = (struct ifreq *)data;
408         switch (cmd) {
409         case SIOCSIFFLAGS:
410         case SIOCADDMULTI:
411         case SIOCDELMULTI:
412                 error = 0;
413                 break;
414
415         case SIOCSIFMEDIA:
416         case SIOCGIFMEDIA:
417                 sc = ifp->if_softc;
418                 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
419                 break;
420
421         case SIOCSIFMTU:
422                 /* We basically allow all kinds of MTUs. */
423                 ifp->if_mtu = ifr->ifr_mtu;
424                 error = 0;
425                 break;
426
427         default:
428                 /* Let the common ethernet handler process this. */
429                 error = ether_ioctl(ifp, cmd, data);
430                 break;
431         }
432
433         return (error);
434 }
435
436 static void
437 epair_init(void *dummy __unused)
438 {
439 }
440
441 /*
442  * Interface cloning functions.
443  * We use our private ones so that we can create/destroy our secondary
444  * device along with the primary one.
445  */
446 static int
447 epair_clone_match(struct if_clone *ifc, const char *name)
448 {
449         const char *cp;
450
451         /*
452          * Our base name is epair.
453          * Our interfaces will be named epair<n>[ab].
454          * So accept anything of the following list:
455          * - epair
456          * - epair<n>
457          * but not the epair<n>[ab] versions.
458          */
459         if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
460                 return (0);
461
462         for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
463                 if (*cp < '0' || *cp > '9')
464                         return (0);
465         }
466
467         return (1);
468 }
469
470 static void
471 epair_clone_add(struct if_clone *ifc, struct epair_softc *scb)
472 {
473         struct ifnet *ifp;
474         uint8_t eaddr[ETHER_ADDR_LEN];  /* 00:00:00:00:00:00 */
475
476         ifp = scb->ifp;
477         /* Copy epairNa etheraddr and change the last byte. */
478         memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
479         eaddr[5] = 0x0b;
480         ether_ifattach(ifp, eaddr);
481
482         if_clone_addif(ifc, ifp);
483 }
484
485 static int
486 epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
487 {
488         struct epair_softc *sca, *scb;
489         struct ifnet *ifp;
490         char *dp;
491         int error, unit, wildcard;
492         uint64_t hostid;
493         uint32_t key[3];
494         uint32_t hash;
495         uint8_t eaddr[ETHER_ADDR_LEN];  /* 00:00:00:00:00:00 */
496
497         /* Try to see if a special unit was requested. */
498         error = ifc_name2unit(name, &unit);
499         if (error != 0)
500                 return (error);
501         wildcard = (unit < 0);
502
503         error = ifc_alloc_unit(ifc, &unit);
504         if (error != 0)
505                 return (error);
506
507         /*
508          * If no unit had been given, we need to adjust the ifName.
509          * Also make sure there is space for our extra [ab] suffix.
510          */
511         for (dp = name; *dp != '\0'; dp++);
512         if (wildcard) {
513                 error = snprintf(dp, len - (dp - name), "%d", unit);
514                 if (error > len - (dp - name) - 1) {
515                         /* ifName too long. */
516                         ifc_free_unit(ifc, unit);
517                         return (ENOSPC);
518                 }
519                 dp += error;
520         }
521         if (len - (dp - name) - 1 < 1) {
522                 /* No space left for our [ab] suffix. */
523                 ifc_free_unit(ifc, unit);
524                 return (ENOSPC);
525         }
526         *dp = 'b';
527         /* Must not change dp so we can replace 'a' by 'b' later. */
528         *(dp+1) = '\0';
529
530         /* Check if 'a' and 'b' interfaces already exist. */ 
531         if (ifunit(name) != NULL)
532                 return (EEXIST);
533         *dp = 'a';
534         if (ifunit(name) != NULL)
535                 return (EEXIST);
536
537         /* Allocate memory for both [ab] interfaces */
538         sca = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
539         sca->ifp = if_alloc(IFT_ETHER);
540         sca->num_queues = epair_tasks.tasks;
541         if (sca->ifp == NULL) {
542                 free(sca, M_EPAIR);
543                 ifc_free_unit(ifc, unit);
544                 return (ENOSPC);
545         }
546         sca->queues = mallocarray(sca->num_queues, sizeof(struct epair_queue),
547             M_EPAIR, M_WAITOK);
548         for (int i = 0; i < sca->num_queues; i++) {
549                 struct epair_queue *q = &sca->queues[i];
550                 q->id = i;
551                 q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
552                 q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
553                 q->ridx = 0;
554                 q->state = 0;
555                 q->sc = sca;
556                 NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q);
557         }
558
559         scb = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
560         scb->ifp = if_alloc(IFT_ETHER);
561         scb->num_queues = epair_tasks.tasks;
562         if (scb->ifp == NULL) {
563                 free(scb, M_EPAIR);
564                 if_free(sca->ifp);
565                 free(sca, M_EPAIR);
566                 ifc_free_unit(ifc, unit);
567                 return (ENOSPC);
568         }
569         scb->queues = mallocarray(scb->num_queues, sizeof(struct epair_queue),
570             M_EPAIR, M_WAITOK);
571         for (int i = 0; i < scb->num_queues; i++) {
572                 struct epair_queue *q = &scb->queues[i];
573                 q->id = i;
574                 q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
575                 q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL);
576                 q->ridx = 0;
577                 q->state = 0;
578                 q->sc = scb;
579                 NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q);
580         }
581
582         /*
583          * Cross-reference the interfaces so we will be able to free both.
584          */
585         sca->oifp = scb->ifp;
586         scb->oifp = sca->ifp;
587
588         EPAIR_LOCK();
589 #ifdef SMP
590         /* Get an approximate distribution. */
591         hash = next_index % mp_ncpus;
592 #else
593         hash = 0;
594 #endif
595         EPAIR_UNLOCK();
596
597         /* Initialise pseudo media types. */
598         ifmedia_init(&sca->media, 0, epair_media_change, epair_media_status);
599         ifmedia_add(&sca->media, IFM_ETHER | IFM_10G_T, 0, NULL);
600         ifmedia_set(&sca->media, IFM_ETHER | IFM_10G_T);
601         ifmedia_init(&scb->media, 0, epair_media_change, epair_media_status);
602         ifmedia_add(&scb->media, IFM_ETHER | IFM_10G_T, 0, NULL);
603         ifmedia_set(&scb->media, IFM_ETHER | IFM_10G_T);
604
605         /* Finish initialization of interface <n>a. */
606         ifp = sca->ifp;
607         ifp->if_softc = sca;
608         strlcpy(ifp->if_xname, name, IFNAMSIZ);
609         ifp->if_dname = epairname;
610         ifp->if_dunit = unit;
611         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
612         ifp->if_flags |= IFF_KNOWSEPOCH;
613         ifp->if_capabilities = IFCAP_VLAN_MTU;
614         ifp->if_capenable = IFCAP_VLAN_MTU;
615         ifp->if_start = epair_start;
616         ifp->if_ioctl = epair_ioctl;
617         ifp->if_init  = epair_init;
618         if_setsendqlen(ifp, ifqmaxlen);
619         if_setsendqready(ifp);
620
621         /*
622          * Calculate the etheraddr hashing the hostid and the
623          * interface index. The result would be hopefully unique.
624          * Note that the "a" component of an epair instance may get moved
625          * to a different VNET after creation. In that case its index
626          * will be freed and the index can get reused by new epair instance.
627          * Make sure we do not create same etheraddr again.
628          */
629         getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
630         if (hostid == 0) 
631                 arc4rand(&hostid, sizeof(hostid), 0);
632
633         EPAIR_LOCK();
634         if (ifp->if_index > next_index)
635                 next_index = ifp->if_index;
636         else
637                 next_index++;
638
639         key[0] = (uint32_t)next_index;
640         EPAIR_UNLOCK();
641         key[1] = (uint32_t)(hostid & 0xffffffff);
642         key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
643         hash = jenkins_hash32(key, 3, 0);
644
645         eaddr[0] = 0x02;
646         memcpy(&eaddr[1], &hash, 4);
647         eaddr[5] = 0x0a;
648         ether_ifattach(ifp, eaddr);
649         ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
650         ifp->if_transmit = epair_transmit;
651
652         /* Swap the name and finish initialization of interface <n>b. */
653         *dp = 'b';
654
655         ifp = scb->ifp;
656         ifp->if_softc = scb;
657         strlcpy(ifp->if_xname, name, IFNAMSIZ);
658         ifp->if_dname = epairname;
659         ifp->if_dunit = unit;
660         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
661         ifp->if_flags |= IFF_KNOWSEPOCH;
662         ifp->if_capabilities = IFCAP_VLAN_MTU;
663         ifp->if_capenable = IFCAP_VLAN_MTU;
664         ifp->if_start = epair_start;
665         ifp->if_ioctl = epair_ioctl;
666         ifp->if_init  = epair_init;
667         if_setsendqlen(ifp, ifqmaxlen);
668         if_setsendqready(ifp);
669         /* We need to play some tricks here for the second interface. */
670         strlcpy(name, epairname, len);
671
672         /* Correctly set the name for the cloner list. */
673         strlcpy(name, scb->ifp->if_xname, len);
674         epair_clone_add(ifc, scb);
675
676         ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
677         ifp->if_transmit = epair_transmit;
678
679         /*
680          * Restore name to <n>a as the ifp for this will go into the
681          * cloner list for the initial call.
682          */
683         strlcpy(name, sca->ifp->if_xname, len);
684
685         /* Tell the world, that we are ready to rock. */
686         sca->ifp->if_drv_flags |= IFF_DRV_RUNNING;
687         if_link_state_change(sca->ifp, LINK_STATE_UP);
688         scb->ifp->if_drv_flags |= IFF_DRV_RUNNING;
689         if_link_state_change(scb->ifp, LINK_STATE_UP);
690
691         return (0);
692 }
693
694 static void
695 epair_drain_rings(struct epair_softc *sc)
696 {
697         int ridx;
698         struct mbuf *m;
699
700         for (ridx = 0; ridx < 2; ridx++) {
701                 for (int i = 0; i < sc->num_queues; i++) {
702                         struct epair_queue *q = &sc->queues[i];
703                         do {
704                                 m = buf_ring_dequeue_sc(q->rxring[ridx]);
705                                 if (m == NULL)
706                                         break;
707                                 m_freem(m);
708                         } while (1);
709                 }
710         }
711 }
712
713 static int
714 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
715 {
716         struct ifnet *oifp;
717         struct epair_softc *sca, *scb;
718         int unit, error;
719
720         /*
721          * In case we called into if_clone_destroyif() ourselves
722          * again to remove the second interface, the softc will be
723          * NULL. In that case so not do anything but return success.
724          */
725         if (ifp->if_softc == NULL)
726                 return (0);
727
728         unit = ifp->if_dunit;
729         sca = ifp->if_softc;
730         oifp = sca->oifp;
731         scb = oifp->if_softc;
732
733         /* Frist get the interfaces down and detached. */
734         if_link_state_change(ifp, LINK_STATE_DOWN);
735         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
736         if_link_state_change(oifp, LINK_STATE_DOWN);
737         oifp->if_drv_flags &= ~IFF_DRV_RUNNING;
738
739         ether_ifdetach(ifp);
740         ether_ifdetach(oifp);
741
742         /* Third free any queued packets and all the resources. */
743         CURVNET_SET_QUIET(oifp->if_vnet);
744         epair_drain_rings(scb);
745         oifp->if_softc = NULL;
746         error = if_clone_destroyif(ifc, oifp);
747         if (error)
748                 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
749                     __func__, error);
750         if_free(oifp);
751         ifmedia_removeall(&scb->media);
752         for (int i = 0; i < scb->num_queues; i++) {
753                 struct epair_queue *q = &scb->queues[i];
754                 buf_ring_free(q->rxring[0], M_EPAIR);
755                 buf_ring_free(q->rxring[1], M_EPAIR);
756         }
757         free(scb->queues, M_EPAIR);
758         free(scb, M_EPAIR);
759         CURVNET_RESTORE();
760
761         epair_drain_rings(sca);
762         if_free(ifp);
763         ifmedia_removeall(&sca->media);
764         for (int i = 0; i < sca->num_queues; i++) {
765                 struct epair_queue *q = &sca->queues[i];
766                 buf_ring_free(q->rxring[0], M_EPAIR);
767                 buf_ring_free(q->rxring[1], M_EPAIR);
768         }
769         free(sca->queues, M_EPAIR);
770         free(sca, M_EPAIR);
771
772         /* Last free the cloner unit. */
773         ifc_free_unit(ifc, unit);
774
775         return (0);
776 }
777
778 static void
779 vnet_epair_init(const void *unused __unused)
780 {
781
782         V_epair_cloner = if_clone_advanced(epairname, 0,
783             epair_clone_match, epair_clone_create, epair_clone_destroy);
784 }
785 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
786     vnet_epair_init, NULL);
787
788 static void
789 vnet_epair_uninit(const void *unused __unused)
790 {
791
792         if_clone_detach(V_epair_cloner);
793 }
794 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
795     vnet_epair_uninit, NULL);
796
797 static int
798 epair_mod_init()
799 {
800         char name[32];
801         epair_tasks.tasks = 0;
802
803 #ifdef RSS
804         int cpu;
805
806         CPU_FOREACH(cpu) {
807                 cpuset_t cpu_mask;
808
809                 /* Pin to this CPU so we get appropriate NUMA allocations. */
810                 thread_lock(curthread);
811                 sched_bind(curthread, cpu);
812                 thread_unlock(curthread);
813
814                 snprintf(name, sizeof(name), "epair_task_%d", cpu);
815
816                 epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK,
817                     taskqueue_thread_enqueue,
818                     &epair_tasks.tq[cpu]);
819                 CPU_SETOF(cpu, &cpu_mask);
820                 taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET,
821                     &cpu_mask, "%s", name);
822
823                 epair_tasks.tasks++;
824         }
825         thread_lock(curthread);
826         sched_unbind(curthread);
827         thread_unlock(curthread);
828 #else
829         snprintf(name, sizeof(name), "epair_task");
830
831         epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK,
832             taskqueue_thread_enqueue,
833             &epair_tasks.tq[0]);
834         taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name);
835
836         epair_tasks.tasks = 1;
837 #endif
838
839         return (0);
840 }
841
842 static void
843 epair_mod_cleanup()
844 {
845
846         for (int i = 0; i < epair_tasks.tasks; i++) {
847                 taskqueue_drain_all(epair_tasks.tq[i]);
848                 taskqueue_free(epair_tasks.tq[i]);
849         }
850 }
851
852 static int
853 epair_modevent(module_t mod, int type, void *data)
854 {
855         int ret;
856
857         switch (type) {
858         case MOD_LOAD:
859                 EPAIR_LOCK_INIT();
860                 ret = epair_mod_init();
861                 if (ret != 0)
862                         return (ret);
863                 if (bootverbose)
864                         printf("%s: %s initialized.\n", __func__, epairname);
865                 break;
866         case MOD_UNLOAD:
867                 epair_mod_cleanup();
868                 EPAIR_LOCK_DESTROY();
869                 if (bootverbose)
870                         printf("%s: %s unloaded.\n", __func__, epairname);
871                 break;
872         default:
873                 return (EOPNOTSUPP);
874         }
875         return (0);
876 }
877
878 static moduledata_t epair_mod = {
879         "if_epair",
880         epair_modevent,
881         0
882 };
883
884 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
885 MODULE_VERSION(if_epair, 3);