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 struct epair_softc; 105 struct epair_queue { 106 struct mtx mtx; 107 struct mbufq q; 108 int id; 109 enum { 110 EPAIR_QUEUE_IDLE, 111 EPAIR_QUEUE_WAKING, 112 EPAIR_QUEUE_RUNNING, 113 } state; 114 struct task tx_task; 115 struct epair_softc *sc; 116 }; 117 118 static struct mtx epair_n_index_mtx; 119 struct epair_softc { 120 struct ifnet *ifp; /* This ifp. */ 121 struct ifnet *oifp; /* other ifp of pair. */ 122 int num_queues; 123 struct epair_queue *queues; 124 struct ifmedia media; /* Media config (fake). */ 125 STAILQ_ENTRY(epair_softc) entry; 126 }; 127 128 struct epair_tasks_t { 129 int tasks; 130 struct taskqueue *tq[MAXCPU]; 131 }; 132 133 static struct epair_tasks_t epair_tasks; 134 135 static void 136 epair_clear_mbuf(struct mbuf *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 m_tag_delete_nonpersistent(m); 146 } 147 148 static void 149 epair_tx_start_deferred(void *arg, int pending) 150 { 151 struct epair_queue *q = (struct epair_queue *)arg; 152 if_t ifp; 153 struct mbuf *m, *n; 154 bool resched; 155 156 ifp = q->sc->ifp; 157 158 if_ref(ifp); 159 CURVNET_SET(ifp->if_vnet); 160 161 mtx_lock(&q->mtx); 162 m = mbufq_flush(&q->q); 163 q->state = EPAIR_QUEUE_RUNNING; 164 mtx_unlock(&q->mtx); 165 166 while (m != NULL) { 167 n = STAILQ_NEXT(m, m_stailqpkt); 168 m->m_nextpkt = NULL; 169 if_input(ifp, m); 170 m = n; 171 } 172 173 /* 174 * Avoid flushing the queue more than once per task. We can otherwise 175 * end up starving ourselves in a multi-epair routing configuration. 176 */ 177 mtx_lock(&q->mtx); 178 if (mbufq_len(&q->q) > 0) { 179 resched = true; 180 q->state = EPAIR_QUEUE_WAKING; 181 } else { 182 resched = false; 183 q->state = EPAIR_QUEUE_IDLE; 184 } 185 mtx_unlock(&q->mtx); 186 187 if (resched) 188 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); 189 190 CURVNET_RESTORE(); 191 if_rele(ifp); 192 } 193 194 static struct epair_queue * 195 epair_select_queue(struct epair_softc *sc, struct mbuf *m) 196 { 197 uint32_t bucket; 198 #ifdef RSS 199 struct ether_header *eh; 200 int ret; 201 202 ret = rss_m2bucket(m, &bucket); 203 if (ret) { 204 /* Actually hash the packet. */ 205 eh = mtod(m, struct ether_header *); 206 207 switch (ntohs(eh->ether_type)) { 208 #ifdef INET 209 case ETHERTYPE_IP: 210 rss_soft_m2cpuid_v4(m, 0, &bucket); 211 break; 212 #endif 213 #ifdef INET6 214 case ETHERTYPE_IPV6: 215 rss_soft_m2cpuid_v6(m, 0, &bucket); 216 break; 217 #endif 218 default: 219 bucket = 0; 220 break; 221 } 222 } 223 bucket %= sc->num_queues; 224 #else 225 bucket = 0; 226 #endif 227 return (&sc->queues[bucket]); 228 } 229 230 static void 231 epair_prepare_mbuf(struct mbuf *m, struct ifnet *src_ifp) 232 { 233 M_ASSERTPKTHDR(m); 234 epair_clear_mbuf(m); 235 if_setrcvif(m, src_ifp); 236 M_SETFIB(m, src_ifp->if_fib); 237 238 MPASS(m->m_nextpkt == NULL); 239 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 240 } 241 242 static void 243 epair_menq(struct mbuf *m, struct epair_softc *osc) 244 { 245 struct epair_queue *q; 246 struct ifnet *ifp, *oifp; 247 int error, len; 248 bool mcast; 249 250 /* 251 * I know this looks weird. We pass the "other sc" as we need that one 252 * and can get both ifps from it as well. 253 */ 254 oifp = osc->ifp; 255 ifp = osc->oifp; 256 257 epair_prepare_mbuf(m, oifp); 258 259 /* Save values as once the mbuf is queued, it's not ours anymore. */ 260 len = m->m_pkthdr.len; 261 mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0; 262 263 q = epair_select_queue(osc, m); 264 265 mtx_lock(&q->mtx); 266 if (q->state == EPAIR_QUEUE_IDLE) { 267 q->state = EPAIR_QUEUE_WAKING; 268 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); 269 } 270 error = mbufq_enqueue(&q->q, m); 271 mtx_unlock(&q->mtx); 272 273 if (error != 0) { 274 m_freem(m); 275 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 276 } else { 277 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 278 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 279 if (mcast) 280 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 281 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1); 282 } 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 bool mcast; 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 mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0; 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 (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 < '0' || *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->state = EPAIR_QUEUE_IDLE; 518 mtx_init(&q->mtx, "epairq", NULL, MTX_DEF | MTX_NEW); 519 mbufq_init(&q->q, RXRSIZE); 520 q->sc = sc; 521 NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); 522 } 523 524 /* Initialise pseudo media types. */ 525 ifmedia_init(&sc->media, 0, epair_media_change, epair_media_status); 526 ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T, 0, NULL); 527 ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T); 528 529 return (sc); 530 } 531 532 static void 533 epair_setup_ifp(struct epair_softc *sc, char *name, int unit) 534 { 535 struct ifnet *ifp = sc->ifp; 536 537 ifp->if_softc = sc; 538 strlcpy(ifp->if_xname, name, IFNAMSIZ); 539 ifp->if_dname = epairname; 540 ifp->if_dunit = unit; 541 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 542 ifp->if_flags |= IFF_KNOWSEPOCH; 543 ifp->if_capabilities = IFCAP_VLAN_MTU; 544 ifp->if_capenable = IFCAP_VLAN_MTU; 545 ifp->if_transmit = epair_transmit; 546 ifp->if_qflush = epair_qflush; 547 ifp->if_start = epair_start; 548 ifp->if_ioctl = epair_ioctl; 549 ifp->if_init = epair_init; 550 if_setsendqlen(ifp, ifqmaxlen); 551 if_setsendqready(ifp); 552 553 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 554 } 555 556 static void 557 epair_generate_mac(struct epair_softc *sc, uint8_t *eaddr) 558 { 559 uint32_t key[3]; 560 uint32_t hash; 561 uint64_t hostid; 562 563 EPAIR_LOCK(); 564 #ifdef SMP 565 /* Get an approximate distribution. */ 566 hash = next_index % mp_ncpus; 567 #else 568 hash = 0; 569 #endif 570 EPAIR_UNLOCK(); 571 572 /* 573 * Calculate the etheraddr hashing the hostid and the 574 * interface index. The result would be hopefully unique. 575 * Note that the "a" component of an epair instance may get moved 576 * to a different VNET after creation. In that case its index 577 * will be freed and the index can get reused by new epair instance. 578 * Make sure we do not create same etheraddr again. 579 */ 580 getcredhostid(curthread->td_ucred, (unsigned long *)&hostid); 581 if (hostid == 0) 582 arc4rand(&hostid, sizeof(hostid), 0); 583 584 struct ifnet *ifp = sc->ifp; 585 EPAIR_LOCK(); 586 if (ifp->if_index > next_index) 587 next_index = ifp->if_index; 588 else 589 next_index++; 590 591 key[0] = (uint32_t)next_index; 592 EPAIR_UNLOCK(); 593 key[1] = (uint32_t)(hostid & 0xffffffff); 594 key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff); 595 hash = jenkins_hash32(key, 3, 0); 596 597 eaddr[0] = 0x02; 598 memcpy(&eaddr[1], &hash, 4); 599 eaddr[5] = 0x0a; 600 } 601 602 static void 603 epair_free_sc(struct epair_softc *sc) 604 { 605 if (sc == NULL) 606 return; 607 608 if_free(sc->ifp); 609 ifmedia_removeall(&sc->media); 610 for (int i = 0; i < sc->num_queues; i++) { 611 struct epair_queue *q = &sc->queues[i]; 612 mtx_destroy(&q->mtx); 613 } 614 free(sc->queues, M_EPAIR); 615 free(sc, M_EPAIR); 616 } 617 618 static void 619 epair_set_state(struct ifnet *ifp, bool running) 620 { 621 if (running) { 622 ifp->if_drv_flags |= IFF_DRV_RUNNING; 623 if_link_state_change(ifp, LINK_STATE_UP); 624 } else { 625 if_link_state_change(ifp, LINK_STATE_DOWN); 626 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 627 } 628 } 629 630 static int 631 epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit) 632 { 633 int error = 0, unit, wildcard; 634 char *dp; 635 636 /* Try to see if a special unit was requested. */ 637 error = ifc_name2unit(name, &unit); 638 if (error != 0) 639 return (error); 640 wildcard = (unit < 0); 641 642 error = ifc_alloc_unit(ifc, &unit); 643 if (error != 0) 644 return (error); 645 646 /* 647 * If no unit had been given, we need to adjust the ifName. 648 * Also make sure there is space for our extra [ab] suffix. 649 */ 650 for (dp = name; *dp != '\0'; dp++); 651 if (wildcard) { 652 int slen = snprintf(dp, len - (dp - name), "%d", unit); 653 if (slen > len - (dp - name) - 1) { 654 /* ifName too long. */ 655 error = ENOSPC; 656 goto done; 657 } 658 dp += slen; 659 } 660 if (len - (dp - name) - 1 < 1) { 661 /* No space left for our [ab] suffix. */ 662 error = ENOSPC; 663 goto done; 664 } 665 *dp = 'b'; 666 /* Must not change dp so we can replace 'a' by 'b' later. */ 667 *(dp+1) = '\0'; 668 669 /* Check if 'a' and 'b' interfaces already exist. */ 670 if (ifunit(name) != NULL) { 671 error = EEXIST; 672 goto done; 673 } 674 675 *dp = 'a'; 676 if (ifunit(name) != NULL) { 677 error = EEXIST; 678 goto done; 679 } 680 *punit = unit; 681 done: 682 if (error != 0) 683 ifc_free_unit(ifc, unit); 684 685 return (error); 686 } 687 688 static int 689 epair_clone_create(struct if_clone *ifc, char *name, size_t len, 690 struct ifc_data *ifd, struct ifnet **ifpp) 691 { 692 struct epair_softc *sca, *scb; 693 struct ifnet *ifp; 694 char *dp; 695 int error, unit; 696 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 697 698 error = epair_handle_unit(ifc, name, len, &unit); 699 if (error != 0) 700 return (error); 701 702 /* Allocate memory for both [ab] interfaces */ 703 sca = epair_alloc_sc(ifc); 704 scb = epair_alloc_sc(ifc); 705 if (sca == NULL || scb == NULL) { 706 epair_free_sc(sca); 707 epair_free_sc(scb); 708 ifc_free_unit(ifc, unit); 709 return (ENOSPC); 710 } 711 712 /* 713 * Cross-reference the interfaces so we will be able to free both. 714 */ 715 sca->oifp = scb->ifp; 716 scb->oifp = sca->ifp; 717 718 /* Finish initialization of interface <n>a. */ 719 ifp = sca->ifp; 720 epair_setup_ifp(sca, name, unit); 721 epair_generate_mac(sca, eaddr); 722 723 ether_ifattach(ifp, eaddr); 724 725 /* Swap the name and finish initialization of interface <n>b. */ 726 dp = name + strlen(name) - 1; 727 *dp = 'b'; 728 729 epair_setup_ifp(scb, name, unit); 730 731 ifp = scb->ifp; 732 /* We need to play some tricks here for the second interface. */ 733 strlcpy(name, epairname, len); 734 /* Correctly set the name for the cloner list. */ 735 strlcpy(name, scb->ifp->if_xname, len); 736 737 epair_clone_add(ifc, scb); 738 739 /* 740 * Restore name to <n>a as the ifp for this will go into the 741 * cloner list for the initial call. 742 */ 743 strlcpy(name, sca->ifp->if_xname, len); 744 745 /* Tell the world, that we are ready to rock. */ 746 epair_set_state(sca->ifp, true); 747 epair_set_state(scb->ifp, true); 748 749 *ifpp = sca->ifp; 750 751 return (0); 752 } 753 754 static void 755 epair_drain_rings(struct epair_softc *sc) 756 { 757 for (int i = 0; i < sc->num_queues; i++) { 758 struct epair_queue *q; 759 struct mbuf *m, *n; 760 761 q = &sc->queues[i]; 762 mtx_lock(&q->mtx); 763 m = mbufq_flush(&q->q); 764 mtx_unlock(&q->mtx); 765 766 for (; m != NULL; m = n) { 767 n = m->m_nextpkt; 768 m_freem(m); 769 } 770 } 771 } 772 773 static int 774 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 775 { 776 struct ifnet *oifp; 777 struct epair_softc *sca, *scb; 778 int unit, error; 779 780 /* 781 * In case we called into if_clone_destroyif() ourselves 782 * again to remove the second interface, the softc will be 783 * NULL. In that case so not do anything but return success. 784 */ 785 if (ifp->if_softc == NULL) 786 return (0); 787 788 unit = ifp->if_dunit; 789 sca = ifp->if_softc; 790 oifp = sca->oifp; 791 scb = oifp->if_softc; 792 793 /* Frist get the interfaces down and detached. */ 794 epair_set_state(ifp, false); 795 epair_set_state(oifp, false); 796 797 ether_ifdetach(ifp); 798 ether_ifdetach(oifp); 799 800 /* Third free any queued packets and all the resources. */ 801 CURVNET_SET_QUIET(oifp->if_vnet); 802 epair_drain_rings(scb); 803 oifp->if_softc = NULL; 804 error = if_clone_destroyif(ifc, oifp); 805 if (error) 806 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d", 807 __func__, error); 808 epair_free_sc(scb); 809 CURVNET_RESTORE(); 810 811 epair_drain_rings(sca); 812 epair_free_sc(sca); 813 814 /* Last free the cloner unit. */ 815 ifc_free_unit(ifc, unit); 816 817 return (0); 818 } 819 820 static void 821 vnet_epair_init(const void *unused __unused) 822 { 823 struct if_clone_addreq req = { 824 .match_f = epair_clone_match, 825 .create_f = epair_clone_create, 826 .destroy_f = epair_clone_destroy, 827 }; 828 V_epair_cloner = ifc_attach_cloner(epairname, &req); 829 } 830 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 831 vnet_epair_init, NULL); 832 833 static void 834 vnet_epair_uninit(const void *unused __unused) 835 { 836 837 ifc_detach_cloner(V_epair_cloner); 838 } 839 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 840 vnet_epair_uninit, NULL); 841 842 static int 843 epair_mod_init(void) 844 { 845 char name[32]; 846 epair_tasks.tasks = 0; 847 848 #ifdef RSS 849 int cpu; 850 851 CPU_FOREACH(cpu) { 852 cpuset_t cpu_mask; 853 854 /* Pin to this CPU so we get appropriate NUMA allocations. */ 855 thread_lock(curthread); 856 sched_bind(curthread, cpu); 857 thread_unlock(curthread); 858 859 snprintf(name, sizeof(name), "epair_task_%d", cpu); 860 861 epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK, 862 taskqueue_thread_enqueue, 863 &epair_tasks.tq[cpu]); 864 CPU_SETOF(cpu, &cpu_mask); 865 taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET, 866 &cpu_mask, "%s", name); 867 868 epair_tasks.tasks++; 869 } 870 thread_lock(curthread); 871 sched_unbind(curthread); 872 thread_unlock(curthread); 873 #else 874 snprintf(name, sizeof(name), "epair_task"); 875 876 epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK, 877 taskqueue_thread_enqueue, 878 &epair_tasks.tq[0]); 879 taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name); 880 881 epair_tasks.tasks = 1; 882 #endif 883 884 return (0); 885 } 886 887 static void 888 epair_mod_cleanup(void) 889 { 890 891 for (int i = 0; i < epair_tasks.tasks; i++) { 892 taskqueue_drain_all(epair_tasks.tq[i]); 893 taskqueue_free(epair_tasks.tq[i]); 894 } 895 } 896 897 static int 898 epair_modevent(module_t mod, int type, void *data) 899 { 900 int ret; 901 902 switch (type) { 903 case MOD_LOAD: 904 EPAIR_LOCK_INIT(); 905 ret = epair_mod_init(); 906 if (ret != 0) 907 return (ret); 908 if (bootverbose) 909 printf("%s: %s initialized.\n", __func__, epairname); 910 break; 911 case MOD_UNLOAD: 912 epair_mod_cleanup(); 913 EPAIR_LOCK_DESTROY(); 914 if (bootverbose) 915 printf("%s: %s unloaded.\n", __func__, epairname); 916 break; 917 default: 918 return (EOPNOTSUPP); 919 } 920 return (0); 921 } 922 923 static moduledata_t epair_mod = { 924 "if_epair", 925 epair_modevent, 926 0 927 }; 928 929 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 930 MODULE_VERSION(if_epair, 3); 931