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