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