xref: /freebsd/usr.sbin/bhyve/pci_virtio_net.c (revision 27c43fe1f3795622c5bd4bbfc465a29a800c0799)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $FreeBSD$
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 <net/ethernet.h>
38 
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <strings.h>
46 #include <unistd.h>
47 #include <assert.h>
48 #include <md5.h>
49 #include <pthread.h>
50 #include <pthread_np.h>
51 
52 #include "bhyverun.h"
53 #include "pci_emul.h"
54 #include "mevent.h"
55 #include "virtio.h"
56 
57 #define VTNET_RINGSZ	1024
58 
59 #define VTNET_MAXSEGS	32
60 
61 /*
62  * Host capabilities.  Note that we only offer a few of these.
63  */
64 #define	VIRTIO_NET_F_CSUM	(1 <<  0) /* host handles partial cksum */
65 #define	VIRTIO_NET_F_GUEST_CSUM	(1 <<  1) /* guest handles partial cksum */
66 #define	VIRTIO_NET_F_MAC	(1 <<  5) /* host supplies MAC */
67 #define	VIRTIO_NET_F_GSO_DEPREC	(1 <<  6) /* deprecated: host handles GSO */
68 #define	VIRTIO_NET_F_GUEST_TSO4	(1 <<  7) /* guest can rcv TSOv4 */
69 #define	VIRTIO_NET_F_GUEST_TSO6	(1 <<  8) /* guest can rcv TSOv6 */
70 #define	VIRTIO_NET_F_GUEST_ECN	(1 <<  9) /* guest can rcv TSO with ECN */
71 #define	VIRTIO_NET_F_GUEST_UFO	(1 << 10) /* guest can rcv UFO */
72 #define	VIRTIO_NET_F_HOST_TSO4	(1 << 11) /* host can rcv TSOv4 */
73 #define	VIRTIO_NET_F_HOST_TSO6	(1 << 12) /* host can rcv TSOv6 */
74 #define	VIRTIO_NET_F_HOST_ECN	(1 << 13) /* host can rcv TSO with ECN */
75 #define	VIRTIO_NET_F_HOST_UFO	(1 << 14) /* host can rcv UFO */
76 #define	VIRTIO_NET_F_MRG_RXBUF	(1 << 15) /* host can merge RX buffers */
77 #define	VIRTIO_NET_F_STATUS	(1 << 16) /* config status field available */
78 #define	VIRTIO_NET_F_CTRL_VQ	(1 << 17) /* control channel available */
79 #define	VIRTIO_NET_F_CTRL_RX	(1 << 18) /* control channel RX mode support */
80 #define	VIRTIO_NET_F_CTRL_VLAN	(1 << 19) /* control channel VLAN filtering */
81 #define	VIRTIO_NET_F_GUEST_ANNOUNCE \
82 				(1 << 21) /* guest can send gratuitous pkts */
83 
84 #define VTNET_S_HOSTCAPS      \
85   ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \
86     VIRTIO_F_NOTIFY_ON_EMPTY)
87 
88 /*
89  * PCI config-space "registers"
90  */
91 struct virtio_net_config {
92 	uint8_t  mac[6];
93 	uint16_t status;
94 } __packed;
95 
96 /*
97  * Queue definitions.
98  */
99 #define VTNET_RXQ	0
100 #define VTNET_TXQ	1
101 #define VTNET_CTLQ	2	/* NB: not yet supported */
102 
103 #define VTNET_MAXQ	3
104 
105 /*
106  * Fixed network header size
107  */
108 struct virtio_net_rxhdr {
109 	uint8_t		vrh_flags;
110 	uint8_t		vrh_gso_type;
111 	uint16_t	vrh_hdr_len;
112 	uint16_t	vrh_gso_size;
113 	uint16_t	vrh_csum_start;
114 	uint16_t	vrh_csum_offset;
115 	uint16_t	vrh_bufs;
116 } __packed;
117 
118 /*
119  * Debug printf
120  */
121 static int pci_vtnet_debug;
122 #define DPRINTF(params) if (pci_vtnet_debug) printf params
123 #define WPRINTF(params) printf params
124 
125 /*
126  * Per-device softc
127  */
128 struct pci_vtnet_softc {
129 	struct virtio_softc vsc_vs;
130 	struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
131 	pthread_mutex_t vsc_mtx;
132 	struct mevent	*vsc_mevp;
133 
134 	int		vsc_tapfd;
135 	int		vsc_rx_ready;
136 	volatile int	resetting;	/* set and checked outside lock */
137 
138 	uint32_t	vsc_features;
139 	struct virtio_net_config vsc_config;
140 
141 	pthread_mutex_t	rx_mtx;
142 	int		rx_in_progress;
143 
144 	pthread_t 	tx_tid;
145 	pthread_mutex_t	tx_mtx;
146 	pthread_cond_t	tx_cond;
147 	int		tx_in_progress;
148 };
149 
150 static void pci_vtnet_reset(void *);
151 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
152 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
153 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
154 
155 static struct virtio_consts vtnet_vi_consts = {
156 	"vtnet",		/* our name */
157 	VTNET_MAXQ - 1,		/* we currently support 2 virtqueues */
158 	sizeof(struct virtio_net_config), /* config reg size */
159 	pci_vtnet_reset,	/* reset */
160 	NULL,			/* device-wide qnotify -- not used */
161 	pci_vtnet_cfgread,	/* read PCI config */
162 	pci_vtnet_cfgwrite,	/* write PCI config */
163 	VTNET_S_HOSTCAPS,	/* our capabilities */
164 };
165 
166 /*
167  * If the transmit thread is active then stall until it is done.
168  */
169 static void
170 pci_vtnet_txwait(struct pci_vtnet_softc *sc)
171 {
172 
173 	pthread_mutex_lock(&sc->tx_mtx);
174 	while (sc->tx_in_progress) {
175 		pthread_mutex_unlock(&sc->tx_mtx);
176 		usleep(10000);
177 		pthread_mutex_lock(&sc->tx_mtx);
178 	}
179 	pthread_mutex_unlock(&sc->tx_mtx);
180 }
181 
182 /*
183  * If the receive thread is active then stall until it is done.
184  */
185 static void
186 pci_vtnet_rxwait(struct pci_vtnet_softc *sc)
187 {
188 
189 	pthread_mutex_lock(&sc->rx_mtx);
190 	while (sc->rx_in_progress) {
191 		pthread_mutex_unlock(&sc->rx_mtx);
192 		usleep(10000);
193 		pthread_mutex_lock(&sc->rx_mtx);
194 	}
195 	pthread_mutex_unlock(&sc->rx_mtx);
196 }
197 
198 static void
199 pci_vtnet_reset(void *vsc)
200 {
201 	struct pci_vtnet_softc *sc = vsc;
202 
203 	DPRINTF(("vtnet: device reset requested !\n"));
204 
205 	sc->resetting = 1;
206 
207 	/*
208 	 * Wait for the transmit and receive threads to finish their
209 	 * processing.
210 	 */
211 	pci_vtnet_txwait(sc);
212 	pci_vtnet_rxwait(sc);
213 
214 	sc->vsc_rx_ready = 0;
215 
216 	/* now reset rings, MSI-X vectors, and negotiated capabilities */
217 	vi_reset_dev(&sc->vsc_vs);
218 
219 	sc->resetting = 0;
220 }
221 
222 /*
223  * Called to send a buffer chain out to the tap device
224  */
225 static void
226 pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
227 		 int len)
228 {
229 	static char pad[60]; /* all zero bytes */
230 
231 	if (sc->vsc_tapfd == -1)
232 		return;
233 
234 	/*
235 	 * If the length is < 60, pad out to that and add the
236 	 * extra zero'd segment to the iov. It is guaranteed that
237 	 * there is always an extra iov available by the caller.
238 	 */
239 	if (len < 60) {
240 		iov[iovcnt].iov_base = pad;
241 		iov[iovcnt].iov_len = 60 - len;
242 		iovcnt++;
243 	}
244 	(void) writev(sc->vsc_tapfd, iov, iovcnt);
245 }
246 
247 /*
248  *  Called when there is read activity on the tap file descriptor.
249  * Each buffer posted by the guest is assumed to be able to contain
250  * an entire ethernet frame + rx header.
251  *  MP note: the dummybuf is only used for discarding frames, so there
252  * is no need for it to be per-vtnet or locked.
253  */
254 static uint8_t dummybuf[2048];
255 
256 static void
257 pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
258 {
259 	struct vqueue_info *vq;
260 	struct virtio_net_rxhdr *vrx;
261 	uint8_t *buf;
262 	int len;
263 	struct iovec iov;
264 
265 	/*
266 	 * Should never be called without a valid tap fd
267 	 */
268 	assert(sc->vsc_tapfd != -1);
269 
270 	/*
271 	 * But, will be called when the rx ring hasn't yet
272 	 * been set up or the guest is resetting the device.
273 	 */
274 	if (!sc->vsc_rx_ready || sc->resetting) {
275 		/*
276 		 * Drop the packet and try later.
277 		 */
278 		(void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
279 		return;
280 	}
281 
282 	/*
283 	 * Check for available rx buffers
284 	 */
285 	vq = &sc->vsc_queues[VTNET_RXQ];
286 	vq_startchains(vq);
287 	if (!vq_has_descs(vq)) {
288 		/*
289 		 * Drop the packet and try later.  Interrupt on
290 		 * empty, if that's negotiated.
291 		 */
292 		(void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
293 		vq_endchains(vq, 1);
294 		return;
295 	}
296 
297 	do {
298 		/*
299 		 * Get descriptor chain, which should have just
300 		 * one descriptor in it.
301 		 * ??? allow guests to use multiple descs?
302 		 */
303 		assert(vq_getchain(vq, &iov, 1, NULL) == 1);
304 
305 		/*
306 		 * Get a pointer to the rx header, and use the
307 		 * data immediately following it for the packet buffer.
308 		 */
309 		vrx = iov.iov_base;
310 		buf = (uint8_t *)(vrx + 1);
311 
312 		len = read(sc->vsc_tapfd, buf,
313 			   iov.iov_len - sizeof(struct virtio_net_rxhdr));
314 
315 		if (len < 0 && errno == EWOULDBLOCK) {
316 			/*
317 			 * No more packets, but still some avail ring
318 			 * entries.  Interrupt if needed/appropriate.
319 			 */
320 			vq_endchains(vq, 0);
321 			return;
322 		}
323 
324 		/*
325 		 * The only valid field in the rx packet header is the
326 		 * number of buffers, which is always 1 without TSO
327 		 * support.
328 		 */
329 		memset(vrx, 0, sizeof(struct virtio_net_rxhdr));
330 		vrx->vrh_bufs = 1;
331 
332 		/*
333 		 * Release this chain and handle more chains.
334 		 */
335 		vq_relchain(vq, len + sizeof(struct virtio_net_rxhdr));
336 	} while (vq_has_descs(vq));
337 
338 	/* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
339 	vq_endchains(vq, 1);
340 }
341 
342 static void
343 pci_vtnet_tap_callback(int fd, enum ev_type type, void *param)
344 {
345 	struct pci_vtnet_softc *sc = param;
346 
347 	pthread_mutex_lock(&sc->rx_mtx);
348 	sc->rx_in_progress = 1;
349 	pci_vtnet_tap_rx(sc);
350 	sc->rx_in_progress = 0;
351 	pthread_mutex_unlock(&sc->rx_mtx);
352 
353 }
354 
355 static void
356 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
357 {
358 	struct pci_vtnet_softc *sc = vsc;
359 
360 	/*
361 	 * A qnotify means that the rx process can now begin
362 	 */
363 	if (sc->vsc_rx_ready == 0) {
364 		sc->vsc_rx_ready = 1;
365 	}
366 }
367 
368 static void
369 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
370 {
371 	struct iovec iov[VTNET_MAXSEGS + 1];
372 	int i, n;
373 	int plen, tlen;
374 
375 	/*
376 	 * Obtain chain of descriptors.  The first one is
377 	 * really the header descriptor, so we need to sum
378 	 * up two lengths: packet length and transfer length.
379 	 */
380 	n = vq_getchain(vq, iov, VTNET_MAXSEGS, NULL);
381 	assert(n >= 1 && n <= VTNET_MAXSEGS);
382 	plen = 0;
383 	tlen = iov[0].iov_len;
384 	for (i = 1; i < n; i++) {
385 		plen += iov[i].iov_len;
386 		tlen += iov[i].iov_len;
387 	}
388 
389 	DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n));
390 	pci_vtnet_tap_tx(sc, &iov[1], n - 1, plen);
391 
392 	/* chain is processed, release it and set tlen */
393 	vq_relchain(vq, tlen);
394 }
395 
396 static void
397 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
398 {
399 	struct pci_vtnet_softc *sc = vsc;
400 
401 	/*
402 	 * Any ring entries to process?
403 	 */
404 	if (!vq_has_descs(vq))
405 		return;
406 
407 	/* Signal the tx thread for processing */
408 	pthread_mutex_lock(&sc->tx_mtx);
409 	if (sc->tx_in_progress == 0)
410 		pthread_cond_signal(&sc->tx_cond);
411 	pthread_mutex_unlock(&sc->tx_mtx);
412 }
413 
414 /*
415  * Thread which will handle processing of TX desc
416  */
417 static void *
418 pci_vtnet_tx_thread(void *param)
419 {
420 	struct pci_vtnet_softc *sc = param;
421 	struct vqueue_info *vq;
422 	int have_work, error;
423 
424 	vq = &sc->vsc_queues[VTNET_TXQ];
425 
426 	/*
427 	 * Let us wait till the tx queue pointers get initialised &
428 	 * first tx signaled
429 	 */
430 	pthread_mutex_lock(&sc->tx_mtx);
431 	error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
432 	assert(error == 0);
433 
434 	for (;;) {
435 		/* note - tx mutex is locked here */
436 		do {
437 			if (sc->resetting)
438 				have_work = 0;
439 			else
440 				have_work = vq_has_descs(vq);
441 
442 			if (!have_work) {
443 				sc->tx_in_progress = 0;
444 				error = pthread_cond_wait(&sc->tx_cond,
445 							  &sc->tx_mtx);
446 				assert(error == 0);
447 			}
448 		} while (!have_work);
449 		sc->tx_in_progress = 1;
450 		pthread_mutex_unlock(&sc->tx_mtx);
451 
452 		vq_startchains(vq);
453 		do {
454 			/*
455 			 * Run through entries, placing them into
456 			 * iovecs and sending when an end-of-packet
457 			 * is found
458 			 */
459 			pci_vtnet_proctx(sc, vq);
460 		} while (vq_has_descs(vq));
461 
462 		/*
463 		 * Generate an interrupt if needed.
464 		 */
465 		vq_endchains(vq, 1);
466 
467 		pthread_mutex_lock(&sc->tx_mtx);
468 	}
469 }
470 
471 #ifdef notyet
472 static void
473 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
474 {
475 
476 	DPRINTF(("vtnet: control qnotify!\n\r"));
477 }
478 #endif
479 
480 static int
481 pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr)
482 {
483         struct ether_addr *ea;
484         char *tmpstr;
485         char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
486 
487         tmpstr = strsep(&mac_str,"=");
488 
489         if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
490                 ea = ether_aton(mac_str);
491 
492                 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
493                     memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
494 			fprintf(stderr, "Invalid MAC %s\n", mac_str);
495                         return (EINVAL);
496                 } else
497                         memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
498         }
499 
500         return (0);
501 }
502 
503 
504 static int
505 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
506 {
507 	MD5_CTX mdctx;
508 	unsigned char digest[16];
509 	char nstr[80];
510 	char tname[MAXCOMLEN + 1];
511 	struct pci_vtnet_softc *sc;
512 	char *devname;
513 	char *vtopts;
514 	int mac_provided;
515 
516 	sc = malloc(sizeof(struct pci_vtnet_softc));
517 	memset(sc, 0, sizeof(struct pci_vtnet_softc));
518 
519 	pthread_mutex_init(&sc->vsc_mtx, NULL);
520 
521 	vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues);
522 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
523 
524 	sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
525 	sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
526 	sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
527 	sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
528 #ifdef notyet
529 	sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
530         sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
531 #endif
532 
533 	/*
534 	 * Attempt to open the tap device and read the MAC address
535 	 * if specified
536 	 */
537 	mac_provided = 0;
538 	sc->vsc_tapfd = -1;
539 	if (opts != NULL) {
540 		char tbuf[80];
541 		int err;
542 
543 		devname = vtopts = strdup(opts);
544 		(void) strsep(&vtopts, ",");
545 
546 		if (vtopts != NULL) {
547 			err = pci_vtnet_parsemac(vtopts, sc->vsc_config.mac);
548 			if (err != 0) {
549 				free(devname);
550 				return (err);
551 			}
552 			mac_provided = 1;
553 		}
554 
555 		strcpy(tbuf, "/dev/");
556 		strlcat(tbuf, devname, sizeof(tbuf));
557 
558 		free(devname);
559 
560 		sc->vsc_tapfd = open(tbuf, O_RDWR);
561 		if (sc->vsc_tapfd == -1) {
562 			WPRINTF(("open of tap device %s failed\n", tbuf));
563 		} else {
564 			/*
565 			 * Set non-blocking and register for read
566 			 * notifications with the event loop
567 			 */
568 			int opt = 1;
569 			if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
570 				WPRINTF(("tap device O_NONBLOCK failed\n"));
571 				close(sc->vsc_tapfd);
572 				sc->vsc_tapfd = -1;
573 			}
574 
575 			sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
576 						  EVF_READ,
577 						  pci_vtnet_tap_callback,
578 						  sc);
579 			if (sc->vsc_mevp == NULL) {
580 				WPRINTF(("Could not register event\n"));
581 				close(sc->vsc_tapfd);
582 				sc->vsc_tapfd = -1;
583 			}
584 		}
585 	}
586 
587 	/*
588 	 * The default MAC address is the standard NetApp OUI of 00-a0-98,
589 	 * followed by an MD5 of the PCI slot/func number and dev name
590 	 */
591 	if (!mac_provided) {
592 		snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
593 		    pi->pi_func, vmname);
594 
595 		MD5Init(&mdctx);
596 		MD5Update(&mdctx, nstr, strlen(nstr));
597 		MD5Final(digest, &mdctx);
598 
599 		sc->vsc_config.mac[0] = 0x00;
600 		sc->vsc_config.mac[1] = 0xa0;
601 		sc->vsc_config.mac[2] = 0x98;
602 		sc->vsc_config.mac[3] = digest[0];
603 		sc->vsc_config.mac[4] = digest[1];
604 		sc->vsc_config.mac[5] = digest[2];
605 	}
606 
607 	/* initialize config space */
608 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
609 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
610 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
611 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
612 
613 	pci_lintr_request(pi);
614 
615 	/* link always up */
616 	sc->vsc_config.status = 1;
617 
618 	/* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
619 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
620 		return (1);
621 
622 	/* use BAR 0 to map config regs in IO space */
623 	vi_set_io_bar(&sc->vsc_vs, 0);
624 
625 	sc->resetting = 0;
626 
627 	sc->rx_in_progress = 0;
628 	pthread_mutex_init(&sc->rx_mtx, NULL);
629 
630 	/*
631 	 * Initialize tx semaphore & spawn TX processing thread.
632 	 * As of now, only one thread for TX desc processing is
633 	 * spawned.
634 	 */
635 	sc->tx_in_progress = 0;
636 	pthread_mutex_init(&sc->tx_mtx, NULL);
637 	pthread_cond_init(&sc->tx_cond, NULL);
638 	pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
639 	snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
640 	    pi->pi_func);
641         pthread_set_name_np(sc->tx_tid, tname);
642 
643 	return (0);
644 }
645 
646 static int
647 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
648 {
649 	struct pci_vtnet_softc *sc = vsc;
650 	void *ptr;
651 
652 	if (offset < 6) {
653 		assert(offset + size <= 6);
654 		/*
655 		 * The driver is allowed to change the MAC address
656 		 */
657 		ptr = &sc->vsc_config.mac[offset];
658 		memcpy(ptr, &value, size);
659 	} else {
660 		DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
661 		return (1);
662 	}
663 	return (0);
664 }
665 
666 static int
667 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
668 {
669 	struct pci_vtnet_softc *sc = vsc;
670 	void *ptr;
671 
672 	ptr = (uint8_t *)&sc->vsc_config + offset;
673 	memcpy(retval, ptr, size);
674 	return (0);
675 }
676 
677 struct pci_devemu pci_de_vnet = {
678 	.pe_emu = 	"virtio-net",
679 	.pe_init =	pci_vtnet_init,
680 	.pe_barwrite =	vi_pci_write,
681 	.pe_barread =	vi_pci_read
682 };
683 PCI_EMUL_SET(pci_de_vnet);
684