2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #ifndef WITHOUT_CAPSICUM
34 #include <sys/capsicum.h>
36 #include <sys/linker_set.h>
37 #include <sys/select.h>
39 #include <sys/ioctl.h>
40 #include <machine/atomic.h>
41 #include <net/ethernet.h>
42 #ifndef NETMAP_WITH_LIBS
43 #define NETMAP_WITH_LIBS
45 #include <net/netmap_user.h>
59 #include <pthread_np.h>
67 #define VTNET_RINGSZ 1024
69 #define VTNET_MAXSEGS 256
72 * Host capabilities. Note that we only offer a few of these.
74 #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */
75 #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */
76 #define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */
77 #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */
78 #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */
79 #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */
80 #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */
81 #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */
82 #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */
83 #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */
84 #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */
85 #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */
86 #define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */
87 #define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */
88 #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */
89 #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */
90 #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */
91 #define VIRTIO_NET_F_GUEST_ANNOUNCE \
92 (1 << 21) /* guest can send gratuitous pkts */
94 #define VTNET_S_HOSTCAPS \
95 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \
96 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
99 * PCI config-space "registers"
101 struct virtio_net_config {
111 #define VTNET_CTLQ 2 /* NB: not yet supported */
116 * Fixed network header size
118 struct virtio_net_rxhdr {
120 uint8_t vrh_gso_type;
121 uint16_t vrh_hdr_len;
122 uint16_t vrh_gso_size;
123 uint16_t vrh_csum_start;
124 uint16_t vrh_csum_offset;
131 static int pci_vtnet_debug;
132 #define DPRINTF(params) if (pci_vtnet_debug) printf params
133 #define WPRINTF(params) printf params
138 struct pci_vtnet_softc {
139 struct virtio_softc vsc_vs;
140 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
141 pthread_mutex_t vsc_mtx;
142 struct mevent *vsc_mevp;
145 struct nm_desc *vsc_nmd;
148 volatile int resetting; /* set and checked outside lock */
150 uint64_t vsc_features; /* negotiated features */
152 struct virtio_net_config vsc_config;
154 pthread_mutex_t rx_mtx;
157 int rx_merge; /* merged rx bufs in use */
160 pthread_mutex_t tx_mtx;
161 pthread_cond_t tx_cond;
164 void (*pci_vtnet_rx)(struct pci_vtnet_softc *sc);
165 void (*pci_vtnet_tx)(struct pci_vtnet_softc *sc, struct iovec *iov,
166 int iovcnt, int len);
169 static void pci_vtnet_reset(void *);
170 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
171 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
172 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
173 static void pci_vtnet_neg_features(void *, uint64_t);
175 static struct virtio_consts vtnet_vi_consts = {
176 "vtnet", /* our name */
177 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
178 sizeof(struct virtio_net_config), /* config reg size */
179 pci_vtnet_reset, /* reset */
180 NULL, /* device-wide qnotify -- not used */
181 pci_vtnet_cfgread, /* read PCI config */
182 pci_vtnet_cfgwrite, /* write PCI config */
183 pci_vtnet_neg_features, /* apply negotiated features */
184 VTNET_S_HOSTCAPS, /* our capabilities */
188 * If the transmit thread is active then stall until it is done.
191 pci_vtnet_txwait(struct pci_vtnet_softc *sc)
194 pthread_mutex_lock(&sc->tx_mtx);
195 while (sc->tx_in_progress) {
196 pthread_mutex_unlock(&sc->tx_mtx);
198 pthread_mutex_lock(&sc->tx_mtx);
200 pthread_mutex_unlock(&sc->tx_mtx);
204 * If the receive thread is active then stall until it is done.
207 pci_vtnet_rxwait(struct pci_vtnet_softc *sc)
210 pthread_mutex_lock(&sc->rx_mtx);
211 while (sc->rx_in_progress) {
212 pthread_mutex_unlock(&sc->rx_mtx);
214 pthread_mutex_lock(&sc->rx_mtx);
216 pthread_mutex_unlock(&sc->rx_mtx);
220 pci_vtnet_reset(void *vsc)
222 struct pci_vtnet_softc *sc = vsc;
224 DPRINTF(("vtnet: device reset requested !\n"));
229 * Wait for the transmit and receive threads to finish their
232 pci_vtnet_txwait(sc);
233 pci_vtnet_rxwait(sc);
235 sc->vsc_rx_ready = 0;
237 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
239 /* now reset rings, MSI-X vectors, and negotiated capabilities */
240 vi_reset_dev(&sc->vsc_vs);
246 * Called to send a buffer chain out to the tap device
249 pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
252 static char pad[60]; /* all zero bytes */
254 if (sc->vsc_tapfd == -1)
258 * If the length is < 60, pad out to that and add the
259 * extra zero'd segment to the iov. It is guaranteed that
260 * there is always an extra iov available by the caller.
263 iov[iovcnt].iov_base = pad;
264 iov[iovcnt].iov_len = 60 - len;
267 (void) writev(sc->vsc_tapfd, iov, iovcnt);
271 * Called when there is read activity on the tap file descriptor.
272 * Each buffer posted by the guest is assumed to be able to contain
273 * an entire ethernet frame + rx header.
274 * MP note: the dummybuf is only used for discarding frames, so there
275 * is no need for it to be per-vtnet or locked.
277 static uint8_t dummybuf[2048];
279 static __inline struct iovec *
280 rx_iov_trim(struct iovec *iov, int *niov, int tlen)
284 /* XXX short-cut: assume first segment is >= tlen */
285 assert(iov[0].iov_len >= tlen);
287 iov[0].iov_len -= tlen;
288 if (iov[0].iov_len == 0) {
293 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
301 pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
303 struct iovec iov[VTNET_MAXSEGS], *riov;
304 struct vqueue_info *vq;
310 * Should never be called without a valid tap fd
312 assert(sc->vsc_tapfd != -1);
315 * But, will be called when the rx ring hasn't yet
316 * been set up or the guest is resetting the device.
318 if (!sc->vsc_rx_ready || sc->resetting) {
320 * Drop the packet and try later.
322 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
327 * Check for available rx buffers
329 vq = &sc->vsc_queues[VTNET_RXQ];
330 if (!vq_has_descs(vq)) {
332 * Drop the packet and try later. Interrupt on
333 * empty, if that's negotiated.
335 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
342 * Get descriptor chain.
344 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
345 assert(n >= 1 && n <= VTNET_MAXSEGS);
348 * Get a pointer to the rx header, and use the
349 * data immediately following it for the packet buffer.
351 vrx = iov[0].iov_base;
352 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
354 len = readv(sc->vsc_tapfd, riov, n);
356 if (len < 0 && errno == EWOULDBLOCK) {
358 * No more packets, but still some avail ring
359 * entries. Interrupt if needed/appropriate.
367 * The only valid field in the rx packet header is the
368 * number of buffers if merged rx bufs were negotiated.
370 memset(vrx, 0, sc->rx_vhdrlen);
373 struct virtio_net_rxhdr *vrxh;
380 * Release this chain and handle more chains.
382 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
383 } while (vq_has_descs(vq));
385 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
390 pci_vtnet_netmap_writev(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
395 for (r = nmd->cur_tx_ring; ; ) {
396 struct netmap_ring *ring = NETMAP_TXRING(nmd->nifp, r);
400 if (nm_ring_empty(ring)) {
402 if (r > nmd->last_tx_ring)
403 r = nmd->first_tx_ring;
404 if (r == nmd->cur_tx_ring)
409 idx = ring->slot[cur].buf_idx;
410 buf = NETMAP_BUF(ring, idx);
412 for (i = 0; i < iovcnt; i++) {
413 if (len + iov[i].iov_len > 2048)
415 memcpy(&buf[len], iov[i].iov_base, iov[i].iov_len);
416 len += iov[i].iov_len;
418 ring->slot[cur].len = len;
419 ring->head = ring->cur = nm_ring_next(ring, cur);
420 nmd->cur_tx_ring = r;
421 ioctl(nmd->fd, NIOCTXSYNC, NULL);
429 pci_vtnet_netmap_readv(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
435 for (r = nmd->cur_rx_ring; ; ) {
436 struct netmap_ring *ring = NETMAP_RXRING(nmd->nifp, r);
441 if (nm_ring_empty(ring)) {
443 if (r > nmd->last_rx_ring)
444 r = nmd->first_rx_ring;
445 if (r == nmd->cur_rx_ring)
450 idx = ring->slot[cur].buf_idx;
451 buf = NETMAP_BUF(ring, idx);
452 left = ring->slot[cur].len;
454 for (i = 0; i < iovcnt && left > 0; i++) {
455 if (iov[i].iov_len > left)
456 iov[i].iov_len = left;
457 memcpy(iov[i].iov_base, &buf[len], iov[i].iov_len);
458 len += iov[i].iov_len;
459 left -= iov[i].iov_len;
461 ring->head = ring->cur = nm_ring_next(ring, cur);
462 nmd->cur_rx_ring = r;
463 ioctl(nmd->fd, NIOCRXSYNC, NULL);
466 for (; i < iovcnt; i++)
473 * Called to send a buffer chain out to the vale port
476 pci_vtnet_netmap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
479 static char pad[60]; /* all zero bytes */
481 if (sc->vsc_nmd == NULL)
485 * If the length is < 60, pad out to that and add the
486 * extra zero'd segment to the iov. It is guaranteed that
487 * there is always an extra iov available by the caller.
490 iov[iovcnt].iov_base = pad;
491 iov[iovcnt].iov_len = 60 - len;
494 (void) pci_vtnet_netmap_writev(sc->vsc_nmd, iov, iovcnt);
498 pci_vtnet_netmap_rx(struct pci_vtnet_softc *sc)
500 struct iovec iov[VTNET_MAXSEGS], *riov;
501 struct vqueue_info *vq;
507 * Should never be called without a valid netmap descriptor
509 assert(sc->vsc_nmd != NULL);
512 * But, will be called when the rx ring hasn't yet
513 * been set up or the guest is resetting the device.
515 if (!sc->vsc_rx_ready || sc->resetting) {
517 * Drop the packet and try later.
519 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
524 * Check for available rx buffers
526 vq = &sc->vsc_queues[VTNET_RXQ];
527 if (!vq_has_descs(vq)) {
529 * Drop the packet and try later. Interrupt on
530 * empty, if that's negotiated.
532 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
539 * Get descriptor chain.
541 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
542 assert(n >= 1 && n <= VTNET_MAXSEGS);
545 * Get a pointer to the rx header, and use the
546 * data immediately following it for the packet buffer.
548 vrx = iov[0].iov_base;
549 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
551 len = pci_vtnet_netmap_readv(sc->vsc_nmd, riov, n);
555 * No more packets, but still some avail ring
556 * entries. Interrupt if needed/appropriate.
564 * The only valid field in the rx packet header is the
565 * number of buffers if merged rx bufs were negotiated.
567 memset(vrx, 0, sc->rx_vhdrlen);
570 struct virtio_net_rxhdr *vrxh;
577 * Release this chain and handle more chains.
579 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
580 } while (vq_has_descs(vq));
582 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
587 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
589 struct pci_vtnet_softc *sc = param;
591 pthread_mutex_lock(&sc->rx_mtx);
592 sc->rx_in_progress = 1;
593 sc->pci_vtnet_rx(sc);
594 sc->rx_in_progress = 0;
595 pthread_mutex_unlock(&sc->rx_mtx);
600 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
602 struct pci_vtnet_softc *sc = vsc;
605 * A qnotify means that the rx process can now begin
607 if (sc->vsc_rx_ready == 0) {
608 sc->vsc_rx_ready = 1;
609 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
614 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
616 struct iovec iov[VTNET_MAXSEGS + 1];
622 * Obtain chain of descriptors. The first one is
623 * really the header descriptor, so we need to sum
624 * up two lengths: packet length and transfer length.
626 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
627 assert(n >= 1 && n <= VTNET_MAXSEGS);
629 tlen = iov[0].iov_len;
630 for (i = 1; i < n; i++) {
631 plen += iov[i].iov_len;
632 tlen += iov[i].iov_len;
635 DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n));
636 sc->pci_vtnet_tx(sc, &iov[1], n - 1, plen);
638 /* chain is processed, release it and set tlen */
639 vq_relchain(vq, idx, tlen);
643 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
645 struct pci_vtnet_softc *sc = vsc;
648 * Any ring entries to process?
650 if (!vq_has_descs(vq))
653 /* Signal the tx thread for processing */
654 pthread_mutex_lock(&sc->tx_mtx);
655 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
656 if (sc->tx_in_progress == 0)
657 pthread_cond_signal(&sc->tx_cond);
658 pthread_mutex_unlock(&sc->tx_mtx);
662 * Thread which will handle processing of TX desc
665 pci_vtnet_tx_thread(void *param)
667 struct pci_vtnet_softc *sc = param;
668 struct vqueue_info *vq;
671 vq = &sc->vsc_queues[VTNET_TXQ];
674 * Let us wait till the tx queue pointers get initialised &
677 pthread_mutex_lock(&sc->tx_mtx);
678 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
682 /* note - tx mutex is locked here */
683 while (sc->resetting || !vq_has_descs(vq)) {
684 vq->vq_used->vu_flags &= ~VRING_USED_F_NO_NOTIFY;
686 if (!sc->resetting && vq_has_descs(vq))
689 sc->tx_in_progress = 0;
690 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
693 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
694 sc->tx_in_progress = 1;
695 pthread_mutex_unlock(&sc->tx_mtx);
699 * Run through entries, placing them into
700 * iovecs and sending when an end-of-packet
703 pci_vtnet_proctx(sc, vq);
704 } while (vq_has_descs(vq));
707 * Generate an interrupt if needed.
711 pthread_mutex_lock(&sc->tx_mtx);
717 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
720 DPRINTF(("vtnet: control qnotify!\n\r"));
725 pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr)
727 struct ether_addr *ea;
729 char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
731 tmpstr = strsep(&mac_str,"=");
733 if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
734 ea = ether_aton(mac_str);
736 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
737 memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
738 fprintf(stderr, "Invalid MAC %s\n", mac_str);
741 memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
748 pci_vtnet_tap_setup(struct pci_vtnet_softc *sc, char *devname)
751 #ifndef WITHOUT_CAPSICUM
755 strcpy(tbuf, "/dev/");
756 strlcat(tbuf, devname, sizeof(tbuf));
758 sc->pci_vtnet_rx = pci_vtnet_tap_rx;
759 sc->pci_vtnet_tx = pci_vtnet_tap_tx;
761 sc->vsc_tapfd = open(tbuf, O_RDWR);
762 if (sc->vsc_tapfd == -1) {
763 WPRINTF(("open of tap device %s failed\n", tbuf));
768 * Set non-blocking and register for read
769 * notifications with the event loop
772 if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
773 WPRINTF(("tap device O_NONBLOCK failed\n"));
774 close(sc->vsc_tapfd);
778 #ifndef WITHOUT_CAPSICUM
779 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
780 if (cap_rights_limit(sc->vsc_tapfd, &rights) == -1 && errno != ENOSYS)
781 errx(EX_OSERR, "Unable to apply rights for sandbox");
784 sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
786 pci_vtnet_rx_callback,
788 if (sc->vsc_mevp == NULL) {
789 WPRINTF(("Could not register event\n"));
790 close(sc->vsc_tapfd);
796 pci_vtnet_netmap_setup(struct pci_vtnet_softc *sc, char *ifname)
798 sc->pci_vtnet_rx = pci_vtnet_netmap_rx;
799 sc->pci_vtnet_tx = pci_vtnet_netmap_tx;
801 sc->vsc_nmd = nm_open(ifname, NULL, 0, 0);
802 if (sc->vsc_nmd == NULL) {
803 WPRINTF(("open of netmap device %s failed\n", ifname));
807 sc->vsc_mevp = mevent_add(sc->vsc_nmd->fd,
809 pci_vtnet_rx_callback,
811 if (sc->vsc_mevp == NULL) {
812 WPRINTF(("Could not register event\n"));
813 nm_close(sc->vsc_nmd);
819 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
822 unsigned char digest[16];
824 char tname[MAXCOMLEN + 1];
825 struct pci_vtnet_softc *sc;
830 sc = calloc(1, sizeof(struct pci_vtnet_softc));
832 pthread_mutex_init(&sc->vsc_mtx, NULL);
834 vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues);
835 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
837 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
838 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
839 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
840 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
842 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
843 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
847 * Attempt to open the tap device and read the MAC address
856 devname = vtopts = strdup(opts);
857 (void) strsep(&vtopts, ",");
859 if (vtopts != NULL) {
860 err = pci_vtnet_parsemac(vtopts, sc->vsc_config.mac);
868 if (strncmp(devname, "vale", 4) == 0)
869 pci_vtnet_netmap_setup(sc, devname);
870 if (strncmp(devname, "tap", 3) == 0 ||
871 strncmp(devname, "vmnet", 5) == 0)
872 pci_vtnet_tap_setup(sc, devname);
878 * The default MAC address is the standard NetApp OUI of 00-a0-98,
879 * followed by an MD5 of the PCI slot/func number and dev name
882 snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
883 pi->pi_func, vmname);
886 MD5Update(&mdctx, nstr, strlen(nstr));
887 MD5Final(digest, &mdctx);
889 sc->vsc_config.mac[0] = 0x00;
890 sc->vsc_config.mac[1] = 0xa0;
891 sc->vsc_config.mac[2] = 0x98;
892 sc->vsc_config.mac[3] = digest[0];
893 sc->vsc_config.mac[4] = digest[1];
894 sc->vsc_config.mac[5] = digest[2];
897 /* initialize config space */
898 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
899 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
900 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
901 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
902 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
904 /* Link is up if we managed to open tap device or vale port. */
905 sc->vsc_config.status = (opts == NULL || sc->vsc_tapfd >= 0 ||
906 sc->vsc_nmd != NULL);
908 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
909 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
912 /* use BAR 0 to map config regs in IO space */
913 vi_set_io_bar(&sc->vsc_vs, 0);
918 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
919 sc->rx_in_progress = 0;
920 pthread_mutex_init(&sc->rx_mtx, NULL);
923 * Initialize tx semaphore & spawn TX processing thread.
924 * As of now, only one thread for TX desc processing is
927 sc->tx_in_progress = 0;
928 pthread_mutex_init(&sc->tx_mtx, NULL);
929 pthread_cond_init(&sc->tx_cond, NULL);
930 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
931 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
933 pthread_set_name_np(sc->tx_tid, tname);
939 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
941 struct pci_vtnet_softc *sc = vsc;
945 assert(offset + size <= 6);
947 * The driver is allowed to change the MAC address
949 ptr = &sc->vsc_config.mac[offset];
950 memcpy(ptr, &value, size);
952 /* silently ignore other writes */
953 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
960 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
962 struct pci_vtnet_softc *sc = vsc;
965 ptr = (uint8_t *)&sc->vsc_config + offset;
966 memcpy(retval, ptr, size);
971 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
973 struct pci_vtnet_softc *sc = vsc;
975 sc->vsc_features = negotiated_features;
977 if (!(sc->vsc_features & VIRTIO_NET_F_MRG_RXBUF)) {
979 /* non-merge rx header is 2 bytes shorter */
984 struct pci_devemu pci_de_vnet = {
985 .pe_emu = "virtio-net",
986 .pe_init = pci_vtnet_init,
987 .pe_barwrite = vi_pci_write,
988 .pe_barread = vi_pci_read
990 PCI_EMUL_SET(pci_de_vnet);