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