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