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_flags |= IFF_KNOWSEPOCH; 547 ifp->if_capabilities = IFCAP_VLAN_MTU; 548 ifp->if_capenable = IFCAP_VLAN_MTU; 549 ifp->if_transmit = epair_transmit; 550 ifp->if_qflush = epair_qflush; 551 ifp->if_start = epair_start; 552 ifp->if_ioctl = epair_ioctl; 553 ifp->if_init = epair_init; 554 if_setsendqlen(ifp, ifqmaxlen); 555 if_setsendqready(ifp); 556 557 ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */ 558 } 559 560 static void 561 epair_generate_mac(struct epair_softc *sc, uint8_t *eaddr) 562 { 563 uint32_t key[3]; 564 uint32_t hash; 565 uint64_t hostid; 566 567 EPAIR_LOCK(); 568 #ifdef SMP 569 /* Get an approximate distribution. */ 570 hash = next_index % mp_ncpus; 571 #else 572 hash = 0; 573 #endif 574 EPAIR_UNLOCK(); 575 576 /* 577 * Calculate the etheraddr hashing the hostid and the 578 * interface index. The result would be hopefully unique. 579 * Note that the "a" component of an epair instance may get moved 580 * to a different VNET after creation. In that case its index 581 * will be freed and the index can get reused by new epair instance. 582 * Make sure we do not create same etheraddr again. 583 */ 584 getcredhostid(curthread->td_ucred, (unsigned long *)&hostid); 585 if (hostid == 0) 586 arc4rand(&hostid, sizeof(hostid), 0); 587 588 struct ifnet *ifp = sc->ifp; 589 EPAIR_LOCK(); 590 if (ifp->if_index > next_index) 591 next_index = ifp->if_index; 592 else 593 next_index++; 594 595 key[0] = (uint32_t)next_index; 596 EPAIR_UNLOCK(); 597 key[1] = (uint32_t)(hostid & 0xffffffff); 598 key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff); 599 hash = jenkins_hash32(key, 3, 0); 600 601 eaddr[0] = 0x02; 602 memcpy(&eaddr[1], &hash, 4); 603 eaddr[5] = 0x0a; 604 } 605 606 static void 607 epair_free_sc(struct epair_softc *sc) 608 { 609 if (sc == NULL) 610 return; 611 612 if_free(sc->ifp); 613 ifmedia_removeall(&sc->media); 614 for (int i = 0; i < sc->num_queues; i++) { 615 struct epair_queue *q = &sc->queues[i]; 616 mtx_destroy(&q->mtx); 617 } 618 free(sc->queues, M_EPAIR); 619 free(sc, M_EPAIR); 620 } 621 622 static void 623 epair_set_state(struct ifnet *ifp, bool running) 624 { 625 if (running) { 626 ifp->if_drv_flags |= IFF_DRV_RUNNING; 627 if_link_state_change(ifp, LINK_STATE_UP); 628 } else { 629 if_link_state_change(ifp, LINK_STATE_DOWN); 630 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 631 } 632 } 633 634 static int 635 epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit) 636 { 637 int error = 0, unit, wildcard; 638 char *dp; 639 640 /* Try to see if a special unit was requested. */ 641 error = ifc_name2unit(name, &unit); 642 if (error != 0) 643 return (error); 644 wildcard = (unit < 0); 645 646 error = ifc_alloc_unit(ifc, &unit); 647 if (error != 0) 648 return (error); 649 650 /* 651 * If no unit had been given, we need to adjust the ifName. 652 * Also make sure there is space for our extra [ab] suffix. 653 */ 654 for (dp = name; *dp != '\0'; dp++); 655 if (wildcard) { 656 int slen = snprintf(dp, len - (dp - name), "%d", unit); 657 if (slen > len - (dp - name) - 1) { 658 /* ifName too long. */ 659 error = ENOSPC; 660 goto done; 661 } 662 dp += slen; 663 } 664 if (len - (dp - name) - 1 < 1) { 665 /* No space left for our [ab] suffix. */ 666 error = ENOSPC; 667 goto done; 668 } 669 *dp = 'b'; 670 /* Must not change dp so we can replace 'a' by 'b' later. */ 671 *(dp+1) = '\0'; 672 673 /* Check if 'a' and 'b' interfaces already exist. */ 674 if (ifunit(name) != NULL) { 675 error = EEXIST; 676 goto done; 677 } 678 679 *dp = 'a'; 680 if (ifunit(name) != NULL) { 681 error = EEXIST; 682 goto done; 683 } 684 *punit = unit; 685 done: 686 if (error != 0) 687 ifc_free_unit(ifc, unit); 688 689 return (error); 690 } 691 692 static int 693 epair_clone_create(struct if_clone *ifc, char *name, size_t len, 694 struct ifc_data *ifd, struct ifnet **ifpp) 695 { 696 struct epair_softc *sca, *scb; 697 struct ifnet *ifp; 698 char *dp; 699 int error, unit; 700 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 701 702 error = epair_handle_unit(ifc, name, len, &unit); 703 if (error != 0) 704 return (error); 705 706 /* Allocate memory for both [ab] interfaces */ 707 sca = epair_alloc_sc(ifc); 708 scb = epair_alloc_sc(ifc); 709 if (sca == NULL || scb == NULL) { 710 epair_free_sc(sca); 711 epair_free_sc(scb); 712 ifc_free_unit(ifc, unit); 713 return (ENOSPC); 714 } 715 716 /* 717 * Cross-reference the interfaces so we will be able to free both. 718 */ 719 sca->oifp = scb->ifp; 720 scb->oifp = sca->ifp; 721 722 /* Finish initialization of interface <n>a. */ 723 ifp = sca->ifp; 724 epair_setup_ifp(sca, name, unit); 725 epair_generate_mac(sca, eaddr); 726 727 ether_ifattach(ifp, eaddr); 728 729 /* Swap the name and finish initialization of interface <n>b. */ 730 dp = name + strlen(name) - 1; 731 *dp = 'b'; 732 733 epair_setup_ifp(scb, name, unit); 734 735 ifp = scb->ifp; 736 /* We need to play some tricks here for the second interface. */ 737 strlcpy(name, epairname, len); 738 /* Correctly set the name for the cloner list. */ 739 strlcpy(name, scb->ifp->if_xname, len); 740 741 epair_clone_add(ifc, scb); 742 743 /* 744 * Restore name to <n>a as the ifp for this will go into the 745 * cloner list for the initial call. 746 */ 747 strlcpy(name, sca->ifp->if_xname, len); 748 749 /* Tell the world, that we are ready to rock. */ 750 epair_set_state(sca->ifp, true); 751 epair_set_state(scb->ifp, true); 752 753 *ifpp = sca->ifp; 754 755 return (0); 756 } 757 758 static void 759 epair_drain_rings(struct epair_softc *sc) 760 { 761 for (int i = 0; i < sc->num_queues; i++) { 762 struct epair_queue *q; 763 struct mbuf *m, *n; 764 765 q = &sc->queues[i]; 766 mtx_lock(&q->mtx); 767 m = mbufq_flush(&q->q); 768 mtx_unlock(&q->mtx); 769 770 for (; m != NULL; m = n) { 771 n = m->m_nextpkt; 772 m_freem(m); 773 } 774 } 775 } 776 777 static int 778 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 779 { 780 struct ifnet *oifp; 781 struct epair_softc *sca, *scb; 782 int unit, error; 783 784 /* 785 * In case we called into if_clone_destroyif() ourselves 786 * again to remove the second interface, the softc will be 787 * NULL. In that case so not do anything but return success. 788 */ 789 if (ifp->if_softc == NULL) 790 return (0); 791 792 unit = ifp->if_dunit; 793 sca = ifp->if_softc; 794 oifp = sca->oifp; 795 scb = oifp->if_softc; 796 797 /* Frist get the interfaces down and detached. */ 798 epair_set_state(ifp, false); 799 epair_set_state(oifp, false); 800 801 ether_ifdetach(ifp); 802 ether_ifdetach(oifp); 803 804 /* Third free any queued packets and all the resources. */ 805 CURVNET_SET_QUIET(oifp->if_vnet); 806 epair_drain_rings(scb); 807 oifp->if_softc = NULL; 808 error = if_clone_destroyif(ifc, oifp); 809 if (error) 810 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d", 811 __func__, error); 812 epair_free_sc(scb); 813 CURVNET_RESTORE(); 814 815 epair_drain_rings(sca); 816 epair_free_sc(sca); 817 818 /* Last free the cloner unit. */ 819 ifc_free_unit(ifc, unit); 820 821 return (0); 822 } 823 824 static void 825 vnet_epair_init(const void *unused __unused) 826 { 827 struct if_clone_addreq req = { 828 .match_f = epair_clone_match, 829 .create_f = epair_clone_create, 830 .destroy_f = epair_clone_destroy, 831 }; 832 V_epair_cloner = ifc_attach_cloner(epairname, &req); 833 } 834 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 835 vnet_epair_init, NULL); 836 837 static void 838 vnet_epair_uninit(const void *unused __unused) 839 { 840 841 ifc_detach_cloner(V_epair_cloner); 842 } 843 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 844 vnet_epair_uninit, NULL); 845 846 static int 847 epair_mod_init(void) 848 { 849 char name[32]; 850 epair_tasks.tasks = 0; 851 852 #ifdef RSS 853 int cpu; 854 855 CPU_FOREACH(cpu) { 856 cpuset_t cpu_mask; 857 858 /* Pin to this CPU so we get appropriate NUMA allocations. */ 859 thread_lock(curthread); 860 sched_bind(curthread, cpu); 861 thread_unlock(curthread); 862 863 snprintf(name, sizeof(name), "epair_task_%d", cpu); 864 865 epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK, 866 taskqueue_thread_enqueue, 867 &epair_tasks.tq[cpu]); 868 CPU_SETOF(cpu, &cpu_mask); 869 taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET, 870 &cpu_mask, "%s", name); 871 872 epair_tasks.tasks++; 873 } 874 thread_lock(curthread); 875 sched_unbind(curthread); 876 thread_unlock(curthread); 877 #else 878 snprintf(name, sizeof(name), "epair_task"); 879 880 epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK, 881 taskqueue_thread_enqueue, 882 &epair_tasks.tq[0]); 883 taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name); 884 885 epair_tasks.tasks = 1; 886 #endif 887 888 return (0); 889 } 890 891 static void 892 epair_mod_cleanup(void) 893 { 894 895 for (int i = 0; i < epair_tasks.tasks; i++) { 896 taskqueue_drain_all(epair_tasks.tq[i]); 897 taskqueue_free(epair_tasks.tq[i]); 898 } 899 } 900 901 static int 902 epair_modevent(module_t mod, int type, void *data) 903 { 904 int ret; 905 906 switch (type) { 907 case MOD_LOAD: 908 EPAIR_LOCK_INIT(); 909 ret = epair_mod_init(); 910 if (ret != 0) 911 return (ret); 912 if (bootverbose) 913 printf("%s: %s initialized.\n", __func__, epairname); 914 break; 915 case MOD_UNLOAD: 916 epair_mod_cleanup(); 917 EPAIR_LOCK_DESTROY(); 918 if (bootverbose) 919 printf("%s: %s unloaded.\n", __func__, epairname); 920 break; 921 default: 922 return (EOPNOTSUPP); 923 } 924 return (0); 925 } 926 927 static moduledata_t epair_mod = { 928 "if_epair", 929 epair_modevent, 930 0 931 }; 932 933 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 934 MODULE_VERSION(if_epair, 3); 935