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