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 struct epair_queue * 188 epair_select_queue(struct epair_softc *sc, struct mbuf *m) 189 { 190 uint32_t bucket; 191 #ifdef RSS 192 struct ether_header *eh; 193 int ret; 194 195 ret = rss_m2bucket(m, &bucket); 196 if (ret) { 197 /* Actually hash the packet. */ 198 eh = mtod(m, struct ether_header *); 199 200 switch (ntohs(eh->ether_type)) { 201 #ifdef INET 202 case ETHERTYPE_IP: 203 rss_soft_m2cpuid_v4(m, 0, &bucket); 204 break; 205 #endif 206 #ifdef INET6 207 case ETHERTYPE_IPV6: 208 rss_soft_m2cpuid_v6(m, 0, &bucket); 209 break; 210 #endif 211 default: 212 bucket = 0; 213 break; 214 } 215 } 216 bucket %= sc->num_queues; 217 #else 218 bucket = 0; 219 #endif 220 return (&sc->queues[bucket]); 221 } 222 223 static void 224 epair_prepare_mbuf(struct mbuf *m, struct ifnet *src_ifp) 225 { 226 M_ASSERTPKTHDR(m); 227 epair_clear_mbuf(m); 228 if_setrcvif(m, src_ifp); 229 M_SETFIB(m, src_ifp->if_fib); 230 231 MPASS(m->m_nextpkt == NULL); 232 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 233 } 234 235 static void 236 epair_menq(struct mbuf *m, struct epair_softc *osc) 237 { 238 struct ifnet *ifp, *oifp; 239 int len, ret; 240 int ridx; 241 short mflags; 242 243 /* 244 * I know this looks weird. We pass the "other sc" as we need that one 245 * and can get both ifps from it as well. 246 */ 247 oifp = osc->ifp; 248 ifp = osc->oifp; 249 250 epair_prepare_mbuf(m, oifp); 251 252 /* Save values as once the mbuf is queued, it's not ours anymore. */ 253 len = m->m_pkthdr.len; 254 mflags = m->m_flags; 255 256 struct epair_queue *q = epair_select_queue(osc, m); 257 258 atomic_set_long(&q->state, (1 << BIT_MBUF_QUEUED)); 259 ridx = atomic_load_int(&q->ridx); 260 ret = buf_ring_enqueue(q->rxring[ridx], m); 261 if (ret != 0) { 262 /* Ring is full. */ 263 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 264 m_freem(m); 265 return; 266 } 267 268 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 269 /* 270 * IFQ_HANDOFF_ADJ/ip_handoff() update statistics, 271 * but as we bypass all this we have to duplicate 272 * the logic another time. 273 */ 274 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 275 if (mflags & (M_BCAST|M_MCAST)) 276 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 277 /* Someone else received the packet. */ 278 if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1); 279 280 if (!atomic_testandset_long(&q->state, BIT_QUEUE_TASK)) 281 taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); 282 } 283 284 static void 285 epair_start(struct ifnet *ifp) 286 { 287 struct mbuf *m; 288 struct epair_softc *sc; 289 struct ifnet *oifp; 290 291 /* 292 * We get packets here from ether_output via if_handoff() 293 * and need to put them into the input queue of the oifp 294 * and will put the packet into the receive-queue (rxq) of the 295 * other interface (oifp) of our pair. 296 */ 297 sc = ifp->if_softc; 298 oifp = sc->oifp; 299 sc = oifp->if_softc; 300 for (;;) { 301 IFQ_DEQUEUE(&ifp->if_snd, m); 302 if (m == NULL) 303 break; 304 M_ASSERTPKTHDR(m); 305 BPF_MTAP(ifp, m); 306 307 /* In case either interface is not usable drop the packet. */ 308 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 309 (ifp->if_flags & IFF_UP) == 0 || 310 (oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 311 (oifp->if_flags & IFF_UP) == 0) { 312 m_freem(m); 313 continue; 314 } 315 316 epair_menq(m, sc); 317 } 318 } 319 320 static int 321 epair_transmit(struct ifnet *ifp, struct mbuf *m) 322 { 323 struct epair_softc *sc; 324 struct ifnet *oifp; 325 #ifdef ALTQ 326 int len; 327 short mflags; 328 #endif 329 330 if (m == NULL) 331 return (0); 332 M_ASSERTPKTHDR(m); 333 334 /* 335 * We are not going to use the interface en/dequeue mechanism 336 * on the TX side. We are called from ether_output_frame() 337 * and will put the packet into the receive-queue (rxq) of the 338 * other interface (oifp) of our pair. 339 */ 340 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 341 m_freem(m); 342 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 343 return (ENXIO); 344 } 345 if ((ifp->if_flags & IFF_UP) == 0) { 346 m_freem(m); 347 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 348 return (ENETDOWN); 349 } 350 351 BPF_MTAP(ifp, m); 352 353 /* 354 * In case the outgoing interface is not usable, 355 * drop the packet. 356 */ 357 sc = ifp->if_softc; 358 oifp = sc->oifp; 359 if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 360 (oifp->if_flags & IFF_UP) == 0) { 361 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 362 m_freem(m); 363 return (0); 364 } 365 366 #ifdef ALTQ 367 len = m->m_pkthdr.len; 368 mflags = m->m_flags; 369 int error = 0; 370 371 /* Support ALTQ via the classic if_start() path. */ 372 IF_LOCK(&ifp->if_snd); 373 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 374 ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 375 if (error) 376 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 377 IF_UNLOCK(&ifp->if_snd); 378 if (!error) { 379 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 380 if (mflags & (M_BCAST|M_MCAST)) 381 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 382 epair_start(ifp); 383 } 384 return (error); 385 } 386 IF_UNLOCK(&ifp->if_snd); 387 #endif 388 389 epair_menq(m, oifp->if_softc); 390 return (0); 391 } 392 393 static void 394 epair_qflush(struct ifnet *ifp __unused) 395 { 396 } 397 398 static int 399 epair_media_change(struct ifnet *ifp __unused) 400 { 401 402 /* Do nothing. */ 403 return (0); 404 } 405 406 static void 407 epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr) 408 { 409 410 imr->ifm_status = IFM_AVALID | IFM_ACTIVE; 411 imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX; 412 } 413 414 static int 415 epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 416 { 417 struct epair_softc *sc; 418 struct ifreq *ifr; 419 int error; 420 421 ifr = (struct ifreq *)data; 422 switch (cmd) { 423 case SIOCSIFFLAGS: 424 case SIOCADDMULTI: 425 case SIOCDELMULTI: 426 error = 0; 427 break; 428 429 case SIOCSIFMEDIA: 430 case SIOCGIFMEDIA: 431 sc = ifp->if_softc; 432 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd); 433 break; 434 435 case SIOCSIFMTU: 436 /* We basically allow all kinds of MTUs. */ 437 ifp->if_mtu = ifr->ifr_mtu; 438 error = 0; 439 break; 440 441 default: 442 /* Let the common ethernet handler process this. */ 443 error = ether_ioctl(ifp, cmd, data); 444 break; 445 } 446 447 return (error); 448 } 449 450 static void 451 epair_init(void *dummy __unused) 452 { 453 } 454 455 /* 456 * Interface cloning functions. 457 * We use our private ones so that we can create/destroy our secondary 458 * device along with the primary one. 459 */ 460 static int 461 epair_clone_match(struct if_clone *ifc, const char *name) 462 { 463 const char *cp; 464 465 /* 466 * Our base name is epair. 467 * Our interfaces will be named epair<n>[ab]. 468 * So accept anything of the following list: 469 * - epair 470 * - epair<n> 471 * but not the epair<n>[ab] versions. 472 */ 473 if (strncmp(epairname, name, sizeof(epairname)-1) != 0) 474 return (0); 475 476 for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) { 477 if (*cp < '0' || *cp > '9') 478 return (0); 479 } 480 481 return (1); 482 } 483 484 static void 485 epair_clone_add(struct if_clone *ifc, struct epair_softc *scb) 486 { 487 struct ifnet *ifp; 488 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 489 490 ifp = scb->ifp; 491 /* Copy epairNa etheraddr and change the last byte. */ 492 memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN); 493 eaddr[5] = 0x0b; 494 ether_ifattach(ifp, eaddr); 495 496 if_clone_addif(ifc, ifp); 497 } 498 499 static struct epair_softc * 500 epair_alloc_sc(struct if_clone *ifc) 501 { 502 struct epair_softc *sc; 503 504 struct ifnet *ifp = if_alloc(IFT_ETHER); 505 if (ifp == NULL) 506 return (NULL); 507 508 sc = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO); 509 sc->ifp = ifp; 510 sc->num_queues = epair_tasks.tasks; 511 sc->queues = mallocarray(sc->num_queues, sizeof(struct epair_queue), 512 M_EPAIR, M_WAITOK); 513 for (int i = 0; i < sc->num_queues; i++) { 514 struct epair_queue *q = &sc->queues[i]; 515 q->id = i; 516 q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 517 q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); 518 q->ridx = 0; 519 q->state = 0; 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 buf_ring_free(q->rxring[0], M_EPAIR); 613 buf_ring_free(q->rxring[1], M_EPAIR); 614 } 615 free(sc->queues, M_EPAIR); 616 free(sc, M_EPAIR); 617 } 618 619 static void 620 epair_set_state(struct ifnet *ifp, bool running) 621 { 622 if (running) { 623 ifp->if_drv_flags |= IFF_DRV_RUNNING; 624 if_link_state_change(ifp, LINK_STATE_UP); 625 } else { 626 if_link_state_change(ifp, LINK_STATE_DOWN); 627 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 628 } 629 } 630 631 static int 632 epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit) 633 { 634 int error = 0, unit, wildcard; 635 char *dp; 636 637 /* Try to see if a special unit was requested. */ 638 error = ifc_name2unit(name, &unit); 639 if (error != 0) 640 return (error); 641 wildcard = (unit < 0); 642 643 error = ifc_alloc_unit(ifc, &unit); 644 if (error != 0) 645 return (error); 646 647 /* 648 * If no unit had been given, we need to adjust the ifName. 649 * Also make sure there is space for our extra [ab] suffix. 650 */ 651 for (dp = name; *dp != '\0'; dp++); 652 if (wildcard) { 653 int slen = snprintf(dp, len - (dp - name), "%d", unit); 654 if (slen > len - (dp - name) - 1) { 655 /* ifName too long. */ 656 error = ENOSPC; 657 goto done; 658 } 659 dp += slen; 660 } 661 if (len - (dp - name) - 1 < 1) { 662 /* No space left for our [ab] suffix. */ 663 error = ENOSPC; 664 goto done; 665 } 666 *dp = 'b'; 667 /* Must not change dp so we can replace 'a' by 'b' later. */ 668 *(dp+1) = '\0'; 669 670 /* Check if 'a' and 'b' interfaces already exist. */ 671 if (ifunit(name) != NULL) { 672 error = EEXIST; 673 goto done; 674 } 675 676 *dp = 'a'; 677 if (ifunit(name) != NULL) { 678 error = EEXIST; 679 goto done; 680 } 681 *punit = unit; 682 done: 683 if (error != 0) 684 ifc_free_unit(ifc, unit); 685 686 return (error); 687 } 688 689 static int 690 epair_clone_create(struct if_clone *ifc, char *name, size_t len, 691 struct ifc_data *ifd, struct ifnet **ifpp) 692 { 693 struct epair_softc *sca, *scb; 694 struct ifnet *ifp; 695 char *dp; 696 int error, unit; 697 uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 698 699 error = epair_handle_unit(ifc, name, len, &unit); 700 if (error != 0) 701 return (error); 702 703 /* Allocate memory for both [ab] interfaces */ 704 sca = epair_alloc_sc(ifc); 705 scb = epair_alloc_sc(ifc); 706 if (sca == NULL || scb == NULL) { 707 epair_free_sc(sca); 708 epair_free_sc(scb); 709 ifc_free_unit(ifc, unit); 710 return (ENOSPC); 711 } 712 713 /* 714 * Cross-reference the interfaces so we will be able to free both. 715 */ 716 sca->oifp = scb->ifp; 717 scb->oifp = sca->ifp; 718 719 /* Finish initialization of interface <n>a. */ 720 ifp = sca->ifp; 721 epair_setup_ifp(sca, name, unit); 722 epair_generate_mac(sca, eaddr); 723 724 ether_ifattach(ifp, eaddr); 725 726 /* Swap the name and finish initialization of interface <n>b. */ 727 dp = name + strlen(name) - 1; 728 *dp = 'b'; 729 730 epair_setup_ifp(scb, name, unit); 731 732 ifp = scb->ifp; 733 /* We need to play some tricks here for the second interface. */ 734 strlcpy(name, epairname, len); 735 /* Correctly set the name for the cloner list. */ 736 strlcpy(name, scb->ifp->if_xname, len); 737 738 epair_clone_add(ifc, scb); 739 740 /* 741 * Restore name to <n>a as the ifp for this will go into the 742 * cloner list for the initial call. 743 */ 744 strlcpy(name, sca->ifp->if_xname, len); 745 746 /* Tell the world, that we are ready to rock. */ 747 epair_set_state(sca->ifp, true); 748 epair_set_state(scb->ifp, true); 749 750 *ifpp = sca->ifp; 751 752 return (0); 753 } 754 755 static void 756 epair_drain_rings(struct epair_softc *sc) 757 { 758 int ridx; 759 struct mbuf *m; 760 761 for (ridx = 0; ridx < 2; ridx++) { 762 for (int i = 0; i < sc->num_queues; i++) { 763 struct epair_queue *q = &sc->queues[i]; 764 do { 765 m = buf_ring_dequeue_sc(q->rxring[ridx]); 766 if (m == NULL) 767 break; 768 m_freem(m); 769 } while (1); 770 } 771 } 772 } 773 774 static int 775 epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 776 { 777 struct ifnet *oifp; 778 struct epair_softc *sca, *scb; 779 int unit, error; 780 781 /* 782 * In case we called into if_clone_destroyif() ourselves 783 * again to remove the second interface, the softc will be 784 * NULL. In that case so not do anything but return success. 785 */ 786 if (ifp->if_softc == NULL) 787 return (0); 788 789 unit = ifp->if_dunit; 790 sca = ifp->if_softc; 791 oifp = sca->oifp; 792 scb = oifp->if_softc; 793 794 /* Frist get the interfaces down and detached. */ 795 epair_set_state(ifp, false); 796 epair_set_state(oifp, false); 797 798 ether_ifdetach(ifp); 799 ether_ifdetach(oifp); 800 801 /* Third free any queued packets and all the resources. */ 802 CURVNET_SET_QUIET(oifp->if_vnet); 803 epair_drain_rings(scb); 804 oifp->if_softc = NULL; 805 error = if_clone_destroyif(ifc, oifp); 806 if (error) 807 panic("%s: if_clone_destroyif() for our 2nd iface failed: %d", 808 __func__, error); 809 epair_free_sc(scb); 810 CURVNET_RESTORE(); 811 812 epair_drain_rings(sca); 813 epair_free_sc(sca); 814 815 /* Last free the cloner unit. */ 816 ifc_free_unit(ifc, unit); 817 818 return (0); 819 } 820 821 static void 822 vnet_epair_init(const void *unused __unused) 823 { 824 struct if_clone_addreq req = { 825 .match_f = epair_clone_match, 826 .create_f = epair_clone_create, 827 .destroy_f = epair_clone_destroy, 828 }; 829 V_epair_cloner = ifc_attach_cloner(epairname, &req); 830 } 831 VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 832 vnet_epair_init, NULL); 833 834 static void 835 vnet_epair_uninit(const void *unused __unused) 836 { 837 838 ifc_detach_cloner(V_epair_cloner); 839 } 840 VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 841 vnet_epair_uninit, NULL); 842 843 static int 844 epair_mod_init(void) 845 { 846 char name[32]; 847 epair_tasks.tasks = 0; 848 849 #ifdef RSS 850 int cpu; 851 852 CPU_FOREACH(cpu) { 853 cpuset_t cpu_mask; 854 855 /* Pin to this CPU so we get appropriate NUMA allocations. */ 856 thread_lock(curthread); 857 sched_bind(curthread, cpu); 858 thread_unlock(curthread); 859 860 snprintf(name, sizeof(name), "epair_task_%d", cpu); 861 862 epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK, 863 taskqueue_thread_enqueue, 864 &epair_tasks.tq[cpu]); 865 CPU_SETOF(cpu, &cpu_mask); 866 taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET, 867 &cpu_mask, "%s", name); 868 869 epair_tasks.tasks++; 870 } 871 thread_lock(curthread); 872 sched_unbind(curthread); 873 thread_unlock(curthread); 874 #else 875 snprintf(name, sizeof(name), "epair_task"); 876 877 epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK, 878 taskqueue_thread_enqueue, 879 &epair_tasks.tq[0]); 880 taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name); 881 882 epair_tasks.tasks = 1; 883 #endif 884 885 return (0); 886 } 887 888 static void 889 epair_mod_cleanup(void) 890 { 891 892 for (int i = 0; i < epair_tasks.tasks; i++) { 893 taskqueue_drain_all(epair_tasks.tq[i]); 894 taskqueue_free(epair_tasks.tq[i]); 895 } 896 } 897 898 static int 899 epair_modevent(module_t mod, int type, void *data) 900 { 901 int ret; 902 903 switch (type) { 904 case MOD_LOAD: 905 EPAIR_LOCK_INIT(); 906 ret = epair_mod_init(); 907 if (ret != 0) 908 return (ret); 909 if (bootverbose) 910 printf("%s: %s initialized.\n", __func__, epairname); 911 break; 912 case MOD_UNLOAD: 913 epair_mod_cleanup(); 914 EPAIR_LOCK_DESTROY(); 915 if (bootverbose) 916 printf("%s: %s unloaded.\n", __func__, epairname); 917 break; 918 default: 919 return (EOPNOTSUPP); 920 } 921 return (0); 922 } 923 924 static moduledata_t epair_mod = { 925 "if_epair", 926 epair_modevent, 927 0 928 }; 929 930 DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 931 MODULE_VERSION(if_epair, 3); 932