cxgbe/t4_tom: Read the chip's DDP page sizes and save them in a
[freebsd-src.git] / usr.sbin / bhyve / pci_virtio_net.c
blob9f220d17284bcbf2c10c8c5bd787ff6e3ce8d8ea
1 /*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD$
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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/atomic.h>
38 #include <net/ethernet.h>
39 #ifndef NETMAP_WITH_LIBS
40 #define NETMAP_WITH_LIBS
41 #endif
42 #include <net/netmap_user.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 <md5.h>
54 #include <pthread.h>
55 #include <pthread_np.h>
57 #include "bhyverun.h"
58 #include "pci_emul.h"
59 #include "mevent.h"
60 #include "virtio.h"
62 #define VTNET_RINGSZ 1024
64 #define VTNET_MAXSEGS 256
67 * Host capabilities. Note that we only offer a few of these.
69 #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */
70 #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */
71 #define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */
72 #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */
73 #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */
74 #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */
75 #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */
76 #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */
77 #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */
78 #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */
79 #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */
80 #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */
81 #define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */
82 #define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */
83 #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */
84 #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */
85 #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */
86 #define VIRTIO_NET_F_GUEST_ANNOUNCE \
87 (1 << 21) /* guest can send gratuitous pkts */
89 #define VTNET_S_HOSTCAPS \
90 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \
91 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
94 * PCI config-space "registers"
96 struct virtio_net_config {
97 uint8_t mac[6];
98 uint16_t status;
99 } __packed;
102 * Queue definitions.
104 #define VTNET_RXQ 0
105 #define VTNET_TXQ 1
106 #define VTNET_CTLQ 2 /* NB: not yet supported */
108 #define VTNET_MAXQ 3
111 * Fixed network header size
113 struct virtio_net_rxhdr {
114 uint8_t vrh_flags;
115 uint8_t vrh_gso_type;
116 uint16_t vrh_hdr_len;
117 uint16_t vrh_gso_size;
118 uint16_t vrh_csum_start;
119 uint16_t vrh_csum_offset;
120 uint16_t vrh_bufs;
121 } __packed;
124 * Debug printf
126 static int pci_vtnet_debug;
127 #define DPRINTF(params) if (pci_vtnet_debug) printf params
128 #define WPRINTF(params) printf params
131 * Per-device softc
133 struct pci_vtnet_softc {
134 struct virtio_softc vsc_vs;
135 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
136 pthread_mutex_t vsc_mtx;
137 struct mevent *vsc_mevp;
139 int vsc_tapfd;
140 struct nm_desc *vsc_nmd;
142 int vsc_rx_ready;
143 volatile int resetting; /* set and checked outside lock */
145 uint64_t vsc_features; /* negotiated features */
147 struct virtio_net_config vsc_config;
149 pthread_mutex_t rx_mtx;
150 int rx_in_progress;
151 int rx_vhdrlen;
152 int rx_merge; /* merged rx bufs in use */
154 pthread_t tx_tid;
155 pthread_mutex_t tx_mtx;
156 pthread_cond_t tx_cond;
157 int tx_in_progress;
159 void (*pci_vtnet_rx)(struct pci_vtnet_softc *sc);
160 void (*pci_vtnet_tx)(struct pci_vtnet_softc *sc, struct iovec *iov,
161 int iovcnt, int len);
164 static void pci_vtnet_reset(void *);
165 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
166 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
167 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
168 static void pci_vtnet_neg_features(void *, uint64_t);
170 static struct virtio_consts vtnet_vi_consts = {
171 "vtnet", /* our name */
172 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
173 sizeof(struct virtio_net_config), /* config reg size */
174 pci_vtnet_reset, /* reset */
175 NULL, /* device-wide qnotify -- not used */
176 pci_vtnet_cfgread, /* read PCI config */
177 pci_vtnet_cfgwrite, /* write PCI config */
178 pci_vtnet_neg_features, /* apply negotiated features */
179 VTNET_S_HOSTCAPS, /* our capabilities */
183 * If the transmit thread is active then stall until it is done.
185 static void
186 pci_vtnet_txwait(struct pci_vtnet_softc *sc)
189 pthread_mutex_lock(&sc->tx_mtx);
190 while (sc->tx_in_progress) {
191 pthread_mutex_unlock(&sc->tx_mtx);
192 usleep(10000);
193 pthread_mutex_lock(&sc->tx_mtx);
195 pthread_mutex_unlock(&sc->tx_mtx);
199 * If the receive thread is active then stall until it is done.
201 static void
202 pci_vtnet_rxwait(struct pci_vtnet_softc *sc)
205 pthread_mutex_lock(&sc->rx_mtx);
206 while (sc->rx_in_progress) {
207 pthread_mutex_unlock(&sc->rx_mtx);
208 usleep(10000);
209 pthread_mutex_lock(&sc->rx_mtx);
211 pthread_mutex_unlock(&sc->rx_mtx);
214 static void
215 pci_vtnet_reset(void *vsc)
217 struct pci_vtnet_softc *sc = vsc;
219 DPRINTF(("vtnet: device reset requested !\n"));
221 sc->resetting = 1;
224 * Wait for the transmit and receive threads to finish their
225 * processing.
227 pci_vtnet_txwait(sc);
228 pci_vtnet_rxwait(sc);
230 sc->vsc_rx_ready = 0;
231 sc->rx_merge = 1;
232 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
234 /* now reset rings, MSI-X vectors, and negotiated capabilities */
235 vi_reset_dev(&sc->vsc_vs);
237 sc->resetting = 0;
241 * Called to send a buffer chain out to the tap device
243 static void
244 pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
245 int len)
247 static char pad[60]; /* all zero bytes */
249 if (sc->vsc_tapfd == -1)
250 return;
253 * If the length is < 60, pad out to that and add the
254 * extra zero'd segment to the iov. It is guaranteed that
255 * there is always an extra iov available by the caller.
257 if (len < 60) {
258 iov[iovcnt].iov_base = pad;
259 iov[iovcnt].iov_len = 60 - len;
260 iovcnt++;
262 (void) writev(sc->vsc_tapfd, iov, iovcnt);
266 * Called when there is read activity on the tap file descriptor.
267 * Each buffer posted by the guest is assumed to be able to contain
268 * an entire ethernet frame + rx header.
269 * MP note: the dummybuf is only used for discarding frames, so there
270 * is no need for it to be per-vtnet or locked.
272 static uint8_t dummybuf[2048];
274 static __inline struct iovec *
275 rx_iov_trim(struct iovec *iov, int *niov, int tlen)
277 struct iovec *riov;
279 /* XXX short-cut: assume first segment is >= tlen */
280 assert(iov[0].iov_len >= tlen);
282 iov[0].iov_len -= tlen;
283 if (iov[0].iov_len == 0) {
284 assert(*niov > 1);
285 *niov -= 1;
286 riov = &iov[1];
287 } else {
288 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
289 riov = &iov[0];
292 return (riov);
295 static void
296 pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
298 struct iovec iov[VTNET_MAXSEGS], *riov;
299 struct vqueue_info *vq;
300 void *vrx;
301 int len, n;
302 uint16_t idx;
305 * Should never be called without a valid tap fd
307 assert(sc->vsc_tapfd != -1);
310 * But, will be called when the rx ring hasn't yet
311 * been set up or the guest is resetting the device.
313 if (!sc->vsc_rx_ready || sc->resetting) {
315 * Drop the packet and try later.
317 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
318 return;
322 * Check for available rx buffers
324 vq = &sc->vsc_queues[VTNET_RXQ];
325 if (!vq_has_descs(vq)) {
327 * Drop the packet and try later. Interrupt on
328 * empty, if that's negotiated.
330 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
331 vq_endchains(vq, 1);
332 return;
335 do {
337 * Get descriptor chain.
339 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
340 assert(n >= 1 && n <= VTNET_MAXSEGS);
343 * Get a pointer to the rx header, and use the
344 * data immediately following it for the packet buffer.
346 vrx = iov[0].iov_base;
347 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
349 len = readv(sc->vsc_tapfd, riov, n);
351 if (len < 0 && errno == EWOULDBLOCK) {
353 * No more packets, but still some avail ring
354 * entries. Interrupt if needed/appropriate.
356 vq_retchain(vq);
357 vq_endchains(vq, 0);
358 return;
362 * The only valid field in the rx packet header is the
363 * number of buffers if merged rx bufs were negotiated.
365 memset(vrx, 0, sc->rx_vhdrlen);
367 if (sc->rx_merge) {
368 struct virtio_net_rxhdr *vrxh;
370 vrxh = vrx;
371 vrxh->vrh_bufs = 1;
375 * Release this chain and handle more chains.
377 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
378 } while (vq_has_descs(vq));
380 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
381 vq_endchains(vq, 1);
384 static __inline int
385 pci_vtnet_netmap_writev(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
387 int r, i;
388 int len = 0;
390 for (r = nmd->cur_tx_ring; ; ) {
391 struct netmap_ring *ring = NETMAP_TXRING(nmd->nifp, r);
392 uint32_t cur, idx;
393 char *buf;
395 if (nm_ring_empty(ring)) {
396 r++;
397 if (r > nmd->last_tx_ring)
398 r = nmd->first_tx_ring;
399 if (r == nmd->cur_tx_ring)
400 break;
401 continue;
403 cur = ring->cur;
404 idx = ring->slot[cur].buf_idx;
405 buf = NETMAP_BUF(ring, idx);
407 for (i = 0; i < iovcnt; i++) {
408 if (len + iov[i].iov_len > 2048)
409 break;
410 memcpy(&buf[len], iov[i].iov_base, iov[i].iov_len);
411 len += iov[i].iov_len;
413 ring->slot[cur].len = len;
414 ring->head = ring->cur = nm_ring_next(ring, cur);
415 nmd->cur_tx_ring = r;
416 ioctl(nmd->fd, NIOCTXSYNC, NULL);
417 break;
420 return (len);
423 static __inline int
424 pci_vtnet_netmap_readv(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
426 int len = 0;
427 int i = 0;
428 int r;
430 for (r = nmd->cur_rx_ring; ; ) {
431 struct netmap_ring *ring = NETMAP_RXRING(nmd->nifp, r);
432 uint32_t cur, idx;
433 char *buf;
434 size_t left;
436 if (nm_ring_empty(ring)) {
437 r++;
438 if (r > nmd->last_rx_ring)
439 r = nmd->first_rx_ring;
440 if (r == nmd->cur_rx_ring)
441 break;
442 continue;
444 cur = ring->cur;
445 idx = ring->slot[cur].buf_idx;
446 buf = NETMAP_BUF(ring, idx);
447 left = ring->slot[cur].len;
449 for (i = 0; i < iovcnt && left > 0; i++) {
450 if (iov[i].iov_len > left)
451 iov[i].iov_len = left;
452 memcpy(iov[i].iov_base, &buf[len], iov[i].iov_len);
453 len += iov[i].iov_len;
454 left -= iov[i].iov_len;
456 ring->head = ring->cur = nm_ring_next(ring, cur);
457 nmd->cur_rx_ring = r;
458 ioctl(nmd->fd, NIOCRXSYNC, NULL);
459 break;
461 for (; i < iovcnt; i++)
462 iov[i].iov_len = 0;
464 return (len);
468 * Called to send a buffer chain out to the vale port
470 static void
471 pci_vtnet_netmap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
472 int len)
474 static char pad[60]; /* all zero bytes */
476 if (sc->vsc_nmd == NULL)
477 return;
480 * If the length is < 60, pad out to that and add the
481 * extra zero'd segment to the iov. It is guaranteed that
482 * there is always an extra iov available by the caller.
484 if (len < 60) {
485 iov[iovcnt].iov_base = pad;
486 iov[iovcnt].iov_len = 60 - len;
487 iovcnt++;
489 (void) pci_vtnet_netmap_writev(sc->vsc_nmd, iov, iovcnt);
492 static void
493 pci_vtnet_netmap_rx(struct pci_vtnet_softc *sc)
495 struct iovec iov[VTNET_MAXSEGS], *riov;
496 struct vqueue_info *vq;
497 void *vrx;
498 int len, n;
499 uint16_t idx;
502 * Should never be called without a valid netmap descriptor
504 assert(sc->vsc_nmd != NULL);
507 * But, will be called when the rx ring hasn't yet
508 * been set up or the guest is resetting the device.
510 if (!sc->vsc_rx_ready || sc->resetting) {
512 * Drop the packet and try later.
514 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
515 return;
519 * Check for available rx buffers
521 vq = &sc->vsc_queues[VTNET_RXQ];
522 if (!vq_has_descs(vq)) {
524 * Drop the packet and try later. Interrupt on
525 * empty, if that's negotiated.
527 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
528 vq_endchains(vq, 1);
529 return;
532 do {
534 * Get descriptor chain.
536 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
537 assert(n >= 1 && n <= VTNET_MAXSEGS);
540 * Get a pointer to the rx header, and use the
541 * data immediately following it for the packet buffer.
543 vrx = iov[0].iov_base;
544 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
546 len = pci_vtnet_netmap_readv(sc->vsc_nmd, riov, n);
548 if (len == 0) {
550 * No more packets, but still some avail ring
551 * entries. Interrupt if needed/appropriate.
553 vq_retchain(vq);
554 vq_endchains(vq, 0);
555 return;
559 * The only valid field in the rx packet header is the
560 * number of buffers if merged rx bufs were negotiated.
562 memset(vrx, 0, sc->rx_vhdrlen);
564 if (sc->rx_merge) {
565 struct virtio_net_rxhdr *vrxh;
567 vrxh = vrx;
568 vrxh->vrh_bufs = 1;
572 * Release this chain and handle more chains.
574 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
575 } while (vq_has_descs(vq));
577 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
578 vq_endchains(vq, 1);
581 static void
582 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
584 struct pci_vtnet_softc *sc = param;
586 pthread_mutex_lock(&sc->rx_mtx);
587 sc->rx_in_progress = 1;
588 sc->pci_vtnet_rx(sc);
589 sc->rx_in_progress = 0;
590 pthread_mutex_unlock(&sc->rx_mtx);
594 static void
595 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
597 struct pci_vtnet_softc *sc = vsc;
600 * A qnotify means that the rx process can now begin
602 if (sc->vsc_rx_ready == 0) {
603 sc->vsc_rx_ready = 1;
604 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
608 static void
609 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
611 struct iovec iov[VTNET_MAXSEGS + 1];
612 int i, n;
613 int plen, tlen;
614 uint16_t idx;
617 * Obtain chain of descriptors. The first one is
618 * really the header descriptor, so we need to sum
619 * up two lengths: packet length and transfer length.
621 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
622 assert(n >= 1 && n <= VTNET_MAXSEGS);
623 plen = 0;
624 tlen = iov[0].iov_len;
625 for (i = 1; i < n; i++) {
626 plen += iov[i].iov_len;
627 tlen += iov[i].iov_len;
630 DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n));
631 sc->pci_vtnet_tx(sc, &iov[1], n - 1, plen);
633 /* chain is processed, release it and set tlen */
634 vq_relchain(vq, idx, tlen);
637 static void
638 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
640 struct pci_vtnet_softc *sc = vsc;
643 * Any ring entries to process?
645 if (!vq_has_descs(vq))
646 return;
648 /* Signal the tx thread for processing */
649 pthread_mutex_lock(&sc->tx_mtx);
650 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
651 if (sc->tx_in_progress == 0)
652 pthread_cond_signal(&sc->tx_cond);
653 pthread_mutex_unlock(&sc->tx_mtx);
657 * Thread which will handle processing of TX desc
659 static void *
660 pci_vtnet_tx_thread(void *param)
662 struct pci_vtnet_softc *sc = param;
663 struct vqueue_info *vq;
664 int error;
666 vq = &sc->vsc_queues[VTNET_TXQ];
669 * Let us wait till the tx queue pointers get initialised &
670 * first tx signaled
672 pthread_mutex_lock(&sc->tx_mtx);
673 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
674 assert(error == 0);
676 for (;;) {
677 /* note - tx mutex is locked here */
678 while (sc->resetting || !vq_has_descs(vq)) {
679 vq->vq_used->vu_flags &= ~VRING_USED_F_NO_NOTIFY;
680 mb();
681 if (!sc->resetting && vq_has_descs(vq))
682 break;
684 sc->tx_in_progress = 0;
685 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
686 assert(error == 0);
688 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
689 sc->tx_in_progress = 1;
690 pthread_mutex_unlock(&sc->tx_mtx);
692 do {
694 * Run through entries, placing them into
695 * iovecs and sending when an end-of-packet
696 * is found
698 pci_vtnet_proctx(sc, vq);
699 } while (vq_has_descs(vq));
702 * Generate an interrupt if needed.
704 vq_endchains(vq, 1);
706 pthread_mutex_lock(&sc->tx_mtx);
710 #ifdef notyet
711 static void
712 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
715 DPRINTF(("vtnet: control qnotify!\n\r"));
717 #endif
719 static int
720 pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr)
722 struct ether_addr *ea;
723 char *tmpstr;
724 char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
726 tmpstr = strsep(&mac_str,"=");
728 if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
729 ea = ether_aton(mac_str);
731 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
732 memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
733 fprintf(stderr, "Invalid MAC %s\n", mac_str);
734 return (EINVAL);
735 } else
736 memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
739 return (0);
742 static void
743 pci_vtnet_tap_setup(struct pci_vtnet_softc *sc, char *devname)
745 char tbuf[80];
747 strcpy(tbuf, "/dev/");
748 strlcat(tbuf, devname, sizeof(tbuf));
750 sc->pci_vtnet_rx = pci_vtnet_tap_rx;
751 sc->pci_vtnet_tx = pci_vtnet_tap_tx;
753 sc->vsc_tapfd = open(tbuf, O_RDWR);
754 if (sc->vsc_tapfd == -1) {
755 WPRINTF(("open of tap device %s failed\n", tbuf));
756 return;
760 * Set non-blocking and register for read
761 * notifications with the event loop
763 int opt = 1;
764 if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
765 WPRINTF(("tap device O_NONBLOCK failed\n"));
766 close(sc->vsc_tapfd);
767 sc->vsc_tapfd = -1;
770 sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
771 EVF_READ,
772 pci_vtnet_rx_callback,
773 sc);
774 if (sc->vsc_mevp == NULL) {
775 WPRINTF(("Could not register event\n"));
776 close(sc->vsc_tapfd);
777 sc->vsc_tapfd = -1;
781 static void
782 pci_vtnet_netmap_setup(struct pci_vtnet_softc *sc, char *ifname)
784 sc->pci_vtnet_rx = pci_vtnet_netmap_rx;
785 sc->pci_vtnet_tx = pci_vtnet_netmap_tx;
787 sc->vsc_nmd = nm_open(ifname, NULL, 0, 0);
788 if (sc->vsc_nmd == NULL) {
789 WPRINTF(("open of netmap device %s failed\n", ifname));
790 return;
793 sc->vsc_mevp = mevent_add(sc->vsc_nmd->fd,
794 EVF_READ,
795 pci_vtnet_rx_callback,
796 sc);
797 if (sc->vsc_mevp == NULL) {
798 WPRINTF(("Could not register event\n"));
799 nm_close(sc->vsc_nmd);
800 sc->vsc_nmd = NULL;
804 static int
805 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
807 MD5_CTX mdctx;
808 unsigned char digest[16];
809 char nstr[80];
810 char tname[MAXCOMLEN + 1];
811 struct pci_vtnet_softc *sc;
812 char *devname;
813 char *vtopts;
814 int mac_provided;
816 sc = calloc(1, sizeof(struct pci_vtnet_softc));
818 pthread_mutex_init(&sc->vsc_mtx, NULL);
820 vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues);
821 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
823 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
824 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
825 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
826 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
827 #ifdef notyet
828 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
829 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
830 #endif
833 * Attempt to open the tap device and read the MAC address
834 * if specified
836 mac_provided = 0;
837 sc->vsc_tapfd = -1;
838 sc->vsc_nmd = NULL;
839 if (opts != NULL) {
840 int err;
842 devname = vtopts = strdup(opts);
843 (void) strsep(&vtopts, ",");
845 if (vtopts != NULL) {
846 err = pci_vtnet_parsemac(vtopts, sc->vsc_config.mac);
847 if (err != 0) {
848 free(devname);
849 return (err);
851 mac_provided = 1;
854 if (strncmp(devname, "vale", 4) == 0)
855 pci_vtnet_netmap_setup(sc, devname);
856 if (strncmp(devname, "tap", 3) == 0 ||
857 strncmp(devname, "vmnet", 5) == 0)
858 pci_vtnet_tap_setup(sc, devname);
860 free(devname);
864 * The default MAC address is the standard NetApp OUI of 00-a0-98,
865 * followed by an MD5 of the PCI slot/func number and dev name
867 if (!mac_provided) {
868 snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
869 pi->pi_func, vmname);
871 MD5Init(&mdctx);
872 MD5Update(&mdctx, nstr, strlen(nstr));
873 MD5Final(digest, &mdctx);
875 sc->vsc_config.mac[0] = 0x00;
876 sc->vsc_config.mac[1] = 0xa0;
877 sc->vsc_config.mac[2] = 0x98;
878 sc->vsc_config.mac[3] = digest[0];
879 sc->vsc_config.mac[4] = digest[1];
880 sc->vsc_config.mac[5] = digest[2];
883 /* initialize config space */
884 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
885 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
886 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
887 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
888 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
890 /* Link is up if we managed to open tap device or vale port. */
891 sc->vsc_config.status = (opts == NULL || sc->vsc_tapfd >= 0 ||
892 sc->vsc_nmd != NULL);
894 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
895 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
896 return (1);
898 /* use BAR 0 to map config regs in IO space */
899 vi_set_io_bar(&sc->vsc_vs, 0);
901 sc->resetting = 0;
903 sc->rx_merge = 1;
904 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
905 sc->rx_in_progress = 0;
906 pthread_mutex_init(&sc->rx_mtx, NULL);
909 * Initialize tx semaphore & spawn TX processing thread.
910 * As of now, only one thread for TX desc processing is
911 * spawned.
913 sc->tx_in_progress = 0;
914 pthread_mutex_init(&sc->tx_mtx, NULL);
915 pthread_cond_init(&sc->tx_cond, NULL);
916 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
917 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
918 pi->pi_func);
919 pthread_set_name_np(sc->tx_tid, tname);
921 return (0);
924 static int
925 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
927 struct pci_vtnet_softc *sc = vsc;
928 void *ptr;
930 if (offset < 6) {
931 assert(offset + size <= 6);
933 * The driver is allowed to change the MAC address
935 ptr = &sc->vsc_config.mac[offset];
936 memcpy(ptr, &value, size);
937 } else {
938 /* silently ignore other writes */
939 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
942 return (0);
945 static int
946 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
948 struct pci_vtnet_softc *sc = vsc;
949 void *ptr;
951 ptr = (uint8_t *)&sc->vsc_config + offset;
952 memcpy(retval, ptr, size);
953 return (0);
956 static void
957 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
959 struct pci_vtnet_softc *sc = vsc;
961 sc->vsc_features = negotiated_features;
963 if (!(sc->vsc_features & VIRTIO_NET_F_MRG_RXBUF)) {
964 sc->rx_merge = 0;
965 /* non-merge rx header is 2 bytes shorter */
966 sc->rx_vhdrlen -= 2;
970 struct pci_devemu pci_de_vnet = {
971 .pe_emu = "virtio-net",
972 .pe_init = pci_vtnet_init,
973 .pe_barwrite = vi_pci_write,
974 .pe_barread = vi_pci_read
976 PCI_EMUL_SET(pci_de_vnet);