1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/linker_set.h> 36 #include <sys/select.h> 37 #include <sys/uio.h> 38 #include <sys/ioctl.h> 39 #include <net/ethernet.h> 40 #include <net/if.h> /* IFNAMSIZ */ 41 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <stdint.h> 48 #include <string.h> 49 #include <strings.h> 50 #include <unistd.h> 51 #include <assert.h> 52 #include <pthread.h> 53 #include <pthread_np.h> 54 55 #include "bhyverun.h" 56 #include "config.h" 57 #include "debug.h" 58 #include "pci_emul.h" 59 #include "mevent.h" 60 #include "virtio.h" 61 #include "net_utils.h" 62 #include "net_backends.h" 63 #include "iov.h" 64 65 #define VTNET_RINGSZ 1024 66 67 #define VTNET_MAXSEGS 256 68 69 #define VTNET_MAX_PKT_LEN (65536 + 64) 70 71 #define VTNET_MIN_MTU ETHERMIN 72 #define VTNET_MAX_MTU 65535 73 74 #define VTNET_S_HOSTCAPS \ 75 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \ 76 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC) 77 78 /* 79 * PCI config-space "registers" 80 */ 81 struct virtio_net_config { 82 uint8_t mac[6]; 83 uint16_t status; 84 uint16_t max_virtqueue_pairs; 85 uint16_t mtu; 86 } __packed; 87 88 /* 89 * Queue definitions. 90 */ 91 #define VTNET_RXQ 0 92 #define VTNET_TXQ 1 93 #define VTNET_CTLQ 2 /* NB: not yet supported */ 94 95 #define VTNET_MAXQ 3 96 97 /* 98 * Debug printf 99 */ 100 static int pci_vtnet_debug; 101 #define DPRINTF(params) if (pci_vtnet_debug) PRINTLN params 102 #define WPRINTF(params) PRINTLN params 103 104 /* 105 * Per-device softc 106 */ 107 struct pci_vtnet_softc { 108 struct virtio_softc vsc_vs; 109 struct vqueue_info vsc_queues[VTNET_MAXQ - 1]; 110 pthread_mutex_t vsc_mtx; 111 112 net_backend_t *vsc_be; 113 114 bool features_negotiated; /* protected by rx_mtx */ 115 116 int resetting; /* protected by tx_mtx */ 117 118 uint64_t vsc_features; /* negotiated features */ 119 120 pthread_mutex_t rx_mtx; 121 int rx_merge; /* merged rx bufs in use */ 122 123 pthread_t tx_tid; 124 pthread_mutex_t tx_mtx; 125 pthread_cond_t tx_cond; 126 int tx_in_progress; 127 128 size_t vhdrlen; 129 size_t be_vhdrlen; 130 131 struct virtio_net_config vsc_config; 132 struct virtio_consts vsc_consts; 133 }; 134 135 static void pci_vtnet_reset(void *); 136 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */ 137 static int pci_vtnet_cfgread(void *, int, int, uint32_t *); 138 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t); 139 static void pci_vtnet_neg_features(void *, uint64_t); 140 141 static struct virtio_consts vtnet_vi_consts = { 142 "vtnet", /* our name */ 143 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */ 144 sizeof(struct virtio_net_config), /* config reg size */ 145 pci_vtnet_reset, /* reset */ 146 NULL, /* device-wide qnotify -- not used */ 147 pci_vtnet_cfgread, /* read PCI config */ 148 pci_vtnet_cfgwrite, /* write PCI config */ 149 pci_vtnet_neg_features, /* apply negotiated features */ 150 VTNET_S_HOSTCAPS, /* our capabilities */ 151 }; 152 153 static void 154 pci_vtnet_reset(void *vsc) 155 { 156 struct pci_vtnet_softc *sc = vsc; 157 158 DPRINTF(("vtnet: device reset requested !")); 159 160 /* Acquire the RX lock to block RX processing. */ 161 pthread_mutex_lock(&sc->rx_mtx); 162 163 /* 164 * Make sure receive operation is disabled at least until we 165 * re-negotiate the features, since receive operation depends 166 * on the value of sc->rx_merge and the header length, which 167 * are both set in pci_vtnet_neg_features(). 168 * Receive operation will be enabled again once the guest adds 169 * the first receive buffers and kicks us. 170 */ 171 sc->features_negotiated = false; 172 netbe_rx_disable(sc->vsc_be); 173 174 /* Set sc->resetting and give a chance to the TX thread to stop. */ 175 pthread_mutex_lock(&sc->tx_mtx); 176 sc->resetting = 1; 177 while (sc->tx_in_progress) { 178 pthread_mutex_unlock(&sc->tx_mtx); 179 usleep(10000); 180 pthread_mutex_lock(&sc->tx_mtx); 181 } 182 183 /* 184 * Now reset rings, MSI-X vectors, and negotiated capabilities. 185 * Do that with the TX lock held, since we need to reset 186 * sc->resetting. 187 */ 188 vi_reset_dev(&sc->vsc_vs); 189 190 sc->resetting = 0; 191 pthread_mutex_unlock(&sc->tx_mtx); 192 pthread_mutex_unlock(&sc->rx_mtx); 193 } 194 195 static __inline struct iovec * 196 iov_trim_hdr(struct iovec *iov, int *iovcnt, unsigned int hlen) 197 { 198 struct iovec *riov; 199 200 if (iov[0].iov_len < hlen) { 201 /* 202 * Not enough header space in the first fragment. 203 * That's not ok for us. 204 */ 205 return NULL; 206 } 207 208 iov[0].iov_len -= hlen; 209 if (iov[0].iov_len == 0) { 210 *iovcnt -= 1; 211 if (*iovcnt == 0) { 212 /* 213 * Only space for the header. That's not 214 * enough for us. 215 */ 216 return NULL; 217 } 218 riov = &iov[1]; 219 } else { 220 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + hlen); 221 riov = &iov[0]; 222 } 223 224 return (riov); 225 } 226 227 struct virtio_mrg_rxbuf_info { 228 uint16_t idx; 229 uint16_t pad; 230 uint32_t len; 231 }; 232 233 static void 234 pci_vtnet_rx(struct pci_vtnet_softc *sc) 235 { 236 int prepend_hdr_len = sc->vhdrlen - sc->be_vhdrlen; 237 struct virtio_mrg_rxbuf_info info[VTNET_MAXSEGS]; 238 struct iovec iov[VTNET_MAXSEGS + 1]; 239 struct vqueue_info *vq; 240 241 vq = &sc->vsc_queues[VTNET_RXQ]; 242 243 /* Features must be negotiated */ 244 if (!sc->features_negotiated) { 245 return; 246 } 247 248 for (;;) { 249 struct virtio_net_rxhdr *hdr; 250 uint32_t riov_bytes; 251 struct iovec *riov; 252 uint32_t ulen; 253 int riov_len; 254 int n_chains; 255 ssize_t rlen; 256 ssize_t plen; 257 258 plen = netbe_peek_recvlen(sc->vsc_be); 259 if (plen <= 0) { 260 /* 261 * No more packets (plen == 0), or backend errored 262 * (plen < 0). Interrupt if needed and stop. 263 */ 264 vq_endchains(vq, /*used_all_avail=*/0); 265 return; 266 } 267 plen += prepend_hdr_len; 268 269 /* 270 * Get a descriptor chain to store the next ingress 271 * packet. In case of mergeable rx buffers, get as 272 * many chains as necessary in order to make room 273 * for plen bytes. 274 */ 275 riov_bytes = 0; 276 riov_len = 0; 277 riov = iov; 278 n_chains = 0; 279 do { 280 int n = vq_getchain(vq, &info[n_chains].idx, riov, 281 VTNET_MAXSEGS - riov_len, NULL); 282 283 if (n == 0) { 284 /* 285 * No rx buffers. Enable RX kicks and double 286 * check. 287 */ 288 vq_kick_enable(vq); 289 if (!vq_has_descs(vq)) { 290 /* 291 * Still no buffers. Return the unused 292 * chains (if any), interrupt if needed 293 * (including for NOTIFY_ON_EMPTY), and 294 * disable the backend until the next 295 * kick. 296 */ 297 vq_retchains(vq, n_chains); 298 vq_endchains(vq, /*used_all_avail=*/1); 299 netbe_rx_disable(sc->vsc_be); 300 return; 301 } 302 303 /* More rx buffers found, so keep going. */ 304 vq_kick_disable(vq); 305 continue; 306 } 307 #ifndef __FreeBSD__ 308 if (n == -1) { 309 /* 310 * An error from vq_getchain() means that 311 * an invalid descriptor was found. 312 */ 313 vq_retchains(vq, n_chains); 314 vq_endchains(vq, /*used_all_avail=*/0); 315 return; 316 } 317 #endif 318 assert(n >= 1 && riov_len + n <= VTNET_MAXSEGS); 319 riov_len += n; 320 if (!sc->rx_merge) { 321 n_chains = 1; 322 break; 323 } 324 #ifndef __FreeBSD__ 325 size_t c = count_iov(riov, n); 326 if (c > UINT32_MAX) { 327 vq_retchains(vq, n_chains); 328 vq_endchains(vq, /*used_all_avail=*/0); 329 return; 330 } 331 info[n_chains].len = (uint32_t)c; 332 #else 333 info[n_chains].len = (uint32_t)count_iov(riov, n); 334 #endif 335 riov_bytes += info[n_chains].len; 336 riov += n; 337 n_chains++; 338 } while (riov_bytes < plen && riov_len < VTNET_MAXSEGS); 339 340 riov = iov; 341 #ifdef __FreeBSD__ 342 hdr = riov[0].iov_base; 343 #else 344 hdr = (struct virtio_net_rxhdr *)riov[0].iov_base; 345 #endif 346 if (prepend_hdr_len > 0) { 347 /* 348 * The frontend uses a virtio-net header, but the 349 * backend does not. We need to prepend a zeroed 350 * header. 351 */ 352 riov = iov_trim_hdr(riov, &riov_len, prepend_hdr_len); 353 if (riov == NULL) { 354 /* 355 * The first collected chain is nonsensical, 356 * as it is not even enough to store the 357 * virtio-net header. Just drop it. 358 */ 359 vq_relchain(vq, info[0].idx, 0); 360 vq_retchains(vq, n_chains - 1); 361 continue; 362 } 363 memset(hdr, 0, prepend_hdr_len); 364 } 365 366 rlen = netbe_recv(sc->vsc_be, riov, riov_len); 367 if (rlen != plen - prepend_hdr_len) { 368 /* 369 * If this happens it means there is something 370 * wrong with the backend (e.g., some other 371 * process is stealing our packets). 372 */ 373 WPRINTF(("netbe_recv: expected %zd bytes, " 374 "got %zd", plen - prepend_hdr_len, rlen)); 375 vq_retchains(vq, n_chains); 376 continue; 377 } 378 379 ulen = (uint32_t)plen; 380 381 /* 382 * Publish the used buffers to the guest, reporting the 383 * number of bytes that we wrote. 384 */ 385 if (!sc->rx_merge) { 386 vq_relchain(vq, info[0].idx, ulen); 387 } else { 388 uint32_t iolen; 389 int i = 0; 390 391 do { 392 iolen = info[i].len; 393 if (iolen > ulen) { 394 iolen = ulen; 395 } 396 vq_relchain_prepare(vq, info[i].idx, iolen); 397 ulen -= iolen; 398 i++; 399 } while (ulen > 0); 400 401 hdr->vrh_bufs = i; 402 vq_relchain_publish(vq); 403 assert(i == n_chains); 404 } 405 } 406 407 } 408 409 /* 410 * Called when there is read activity on the backend file descriptor. 411 * Each buffer posted by the guest is assumed to be able to contain 412 * an entire ethernet frame + rx header. 413 */ 414 static void 415 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param) 416 { 417 struct pci_vtnet_softc *sc = param; 418 419 pthread_mutex_lock(&sc->rx_mtx); 420 pci_vtnet_rx(sc); 421 pthread_mutex_unlock(&sc->rx_mtx); 422 423 } 424 425 /* Called on RX kick. */ 426 static void 427 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq) 428 { 429 struct pci_vtnet_softc *sc = vsc; 430 431 /* 432 * A qnotify means that the rx process can now begin. 433 * Enable RX only if features are negotiated. 434 */ 435 pthread_mutex_lock(&sc->rx_mtx); 436 if (!sc->features_negotiated) { 437 pthread_mutex_unlock(&sc->rx_mtx); 438 return; 439 } 440 441 vq_kick_disable(vq); 442 netbe_rx_enable(sc->vsc_be); 443 pthread_mutex_unlock(&sc->rx_mtx); 444 } 445 446 /* TX virtqueue processing, called by the TX thread. */ 447 static void 448 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq) 449 { 450 struct iovec iov[VTNET_MAXSEGS + 1]; 451 struct iovec *siov = iov; 452 uint16_t idx; 453 ssize_t len; 454 int n; 455 456 /* 457 * Obtain chain of descriptors. The first descriptor also 458 * contains the virtio-net header. 459 */ 460 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL); 461 assert(n >= 1 && n <= VTNET_MAXSEGS); 462 463 if (sc->vhdrlen != sc->be_vhdrlen) { 464 /* 465 * The frontend uses a virtio-net header, but the backend 466 * does not. We simply strip the header and ignore it, as 467 * it should be zero-filled. 468 */ 469 siov = iov_trim_hdr(siov, &n, sc->vhdrlen); 470 } 471 472 if (siov == NULL) { 473 /* The chain is nonsensical. Just drop it. */ 474 len = 0; 475 } else { 476 len = netbe_send(sc->vsc_be, siov, n); 477 if (len < 0) { 478 /* 479 * If send failed, report that 0 bytes 480 * were read. 481 */ 482 len = 0; 483 } 484 } 485 486 /* 487 * Return the processed chain to the guest, reporting 488 * the number of bytes that we read. 489 */ 490 vq_relchain(vq, idx, len); 491 } 492 493 /* Called on TX kick. */ 494 static void 495 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq) 496 { 497 struct pci_vtnet_softc *sc = vsc; 498 499 /* 500 * Any ring entries to process? 501 */ 502 if (!vq_has_descs(vq)) 503 return; 504 505 /* Signal the tx thread for processing */ 506 pthread_mutex_lock(&sc->tx_mtx); 507 vq_kick_disable(vq); 508 if (sc->tx_in_progress == 0) 509 pthread_cond_signal(&sc->tx_cond); 510 pthread_mutex_unlock(&sc->tx_mtx); 511 } 512 513 /* 514 * Thread which will handle processing of TX desc 515 */ 516 static void * 517 pci_vtnet_tx_thread(void *param) 518 { 519 struct pci_vtnet_softc *sc = param; 520 struct vqueue_info *vq; 521 int error; 522 523 vq = &sc->vsc_queues[VTNET_TXQ]; 524 525 /* 526 * Let us wait till the tx queue pointers get initialised & 527 * first tx signaled 528 */ 529 pthread_mutex_lock(&sc->tx_mtx); 530 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx); 531 assert(error == 0); 532 533 for (;;) { 534 /* note - tx mutex is locked here */ 535 while (sc->resetting || !vq_has_descs(vq)) { 536 vq_kick_enable(vq); 537 if (!sc->resetting && vq_has_descs(vq)) 538 break; 539 540 sc->tx_in_progress = 0; 541 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx); 542 assert(error == 0); 543 } 544 vq_kick_disable(vq); 545 sc->tx_in_progress = 1; 546 pthread_mutex_unlock(&sc->tx_mtx); 547 548 do { 549 /* 550 * Run through entries, placing them into 551 * iovecs and sending when an end-of-packet 552 * is found 553 */ 554 pci_vtnet_proctx(sc, vq); 555 } while (vq_has_descs(vq)); 556 557 /* 558 * Generate an interrupt if needed. 559 */ 560 vq_endchains(vq, /*used_all_avail=*/1); 561 562 pthread_mutex_lock(&sc->tx_mtx); 563 } 564 #ifndef __FreeBSD__ 565 return (NULL); 566 #endif 567 } 568 569 #ifdef notyet 570 static void 571 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq) 572 { 573 574 DPRINTF(("vtnet: control qnotify!")); 575 } 576 #endif 577 578 static int 579 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) 580 { 581 struct pci_vtnet_softc *sc; 582 const char *value; 583 char tname[MAXCOMLEN + 1]; 584 unsigned long mtu = ETHERMTU; 585 int err; 586 587 /* 588 * Allocate data structures for further virtio initializations. 589 * sc also contains a copy of vtnet_vi_consts, since capabilities 590 * change depending on the backend. 591 */ 592 sc = calloc(1, sizeof(struct pci_vtnet_softc)); 593 594 sc->vsc_consts = vtnet_vi_consts; 595 pthread_mutex_init(&sc->vsc_mtx, NULL); 596 597 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ; 598 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq; 599 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ; 600 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq; 601 #ifdef notyet 602 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ; 603 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq; 604 #endif 605 606 value = get_config_value_node(nvl, "mac"); 607 if (value != NULL) { 608 err = net_parsemac(value, sc->vsc_config.mac); 609 if (err) { 610 free(sc); 611 return (err); 612 } 613 } else 614 net_genmac(pi, sc->vsc_config.mac); 615 616 value = get_config_value_node(nvl, "mtu"); 617 if (value != NULL) { 618 err = net_parsemtu(value, &mtu); 619 if (err) { 620 free(sc); 621 return (err); 622 } 623 624 if (mtu < VTNET_MIN_MTU || mtu > VTNET_MAX_MTU) { 625 err = EINVAL; 626 errno = EINVAL; 627 free(sc); 628 return (err); 629 } 630 sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MTU; 631 } 632 sc->vsc_config.mtu = mtu; 633 634 /* Permit interfaces without a configured backend. */ 635 if (get_config_value_node(nvl, "backend") != NULL) { 636 err = netbe_init(&sc->vsc_be, nvl, pci_vtnet_rx_callback, sc); 637 if (err) { 638 free(sc); 639 return (err); 640 } 641 #ifndef __FreeBSD__ 642 size_t buflen = sizeof (sc->vsc_config.mac); 643 644 err = netbe_get_mac(sc->vsc_be, sc->vsc_config.mac, &buflen); 645 if (err != 0) { 646 free(sc); 647 return (err); 648 } 649 #endif 650 } 651 652 sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MRG_RXBUF | 653 netbe_get_cap(sc->vsc_be); 654 655 /* 656 * Since we do not actually support multiqueue, 657 * set the maximum virtqueue pairs to 1. 658 */ 659 sc->vsc_config.max_virtqueue_pairs = 1; 660 661 /* initialize config space */ 662 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET); 663 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR); 664 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK); 665 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_NETWORK); 666 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR); 667 668 /* Link is always up. */ 669 sc->vsc_config.status = 1; 670 671 vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues); 672 sc->vsc_vs.vs_mtx = &sc->vsc_mtx; 673 674 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */ 675 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) { 676 free(sc); 677 return (1); 678 } 679 680 /* use BAR 0 to map config regs in IO space */ 681 vi_set_io_bar(&sc->vsc_vs, 0); 682 683 sc->resetting = 0; 684 685 sc->rx_merge = 0; 686 sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2; 687 pthread_mutex_init(&sc->rx_mtx, NULL); 688 689 /* 690 * Initialize tx semaphore & spawn TX processing thread. 691 * As of now, only one thread for TX desc processing is 692 * spawned. 693 */ 694 sc->tx_in_progress = 0; 695 pthread_mutex_init(&sc->tx_mtx, NULL); 696 pthread_cond_init(&sc->tx_cond, NULL); 697 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc); 698 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot, 699 pi->pi_func); 700 pthread_set_name_np(sc->tx_tid, tname); 701 702 return (0); 703 } 704 705 static int 706 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value) 707 { 708 struct pci_vtnet_softc *sc = vsc; 709 void *ptr; 710 711 if (offset < (int)sizeof(sc->vsc_config.mac)) { 712 assert(offset + size <= (int)sizeof(sc->vsc_config.mac)); 713 /* 714 * The driver is allowed to change the MAC address 715 */ 716 ptr = &sc->vsc_config.mac[offset]; 717 memcpy(ptr, &value, size); 718 } else { 719 /* silently ignore other writes */ 720 DPRINTF(("vtnet: write to readonly reg %d", offset)); 721 } 722 723 return (0); 724 } 725 726 static int 727 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval) 728 { 729 struct pci_vtnet_softc *sc = vsc; 730 void *ptr; 731 732 ptr = (uint8_t *)&sc->vsc_config + offset; 733 memcpy(retval, ptr, size); 734 return (0); 735 } 736 737 static void 738 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features) 739 { 740 struct pci_vtnet_softc *sc = vsc; 741 742 sc->vsc_features = negotiated_features; 743 744 if (negotiated_features & VIRTIO_NET_F_MRG_RXBUF) { 745 sc->vhdrlen = sizeof(struct virtio_net_rxhdr); 746 sc->rx_merge = 1; 747 } else { 748 /* 749 * Without mergeable rx buffers, virtio-net header is 2 750 * bytes shorter than sizeof(struct virtio_net_rxhdr). 751 */ 752 sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2; 753 sc->rx_merge = 0; 754 } 755 756 /* Tell the backend to enable some capabilities it has advertised. */ 757 netbe_set_cap(sc->vsc_be, negotiated_features, sc->vhdrlen); 758 sc->be_vhdrlen = netbe_get_vnet_hdr_len(sc->vsc_be); 759 assert(sc->be_vhdrlen == 0 || sc->be_vhdrlen == sc->vhdrlen); 760 761 pthread_mutex_lock(&sc->rx_mtx); 762 sc->features_negotiated = true; 763 pthread_mutex_unlock(&sc->rx_mtx); 764 } 765 766 static struct pci_devemu pci_de_vnet = { 767 .pe_emu = "virtio-net", 768 .pe_init = pci_vtnet_init, 769 .pe_legacy_config = netbe_legacy_config, 770 .pe_barwrite = vi_pci_write, 771 .pe_barread = vi_pci_read, 772 }; 773 PCI_EMUL_SET(pci_de_vnet); 774