]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/pci_virtio_net.c
MFC r348834
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / pci_virtio_net.c
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 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
37 #endif
38 #include <sys/linker_set.h>
39 #include <sys/select.h>
40 #include <sys/uio.h>
41 #include <sys/ioctl.h>
42 #include <machine/atomic.h>
43 #include <net/ethernet.h>
44 #ifndef NETMAP_WITH_LIBS
45 #define NETMAP_WITH_LIBS
46 #endif
47 #include <net/netmap_user.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdint.h>
55 #include <string.h>
56 #include <strings.h>
57 #include <unistd.h>
58 #include <assert.h>
59 #include <md5.h>
60 #include <pthread.h>
61 #include <pthread_np.h>
62 #include <sysexits.h>
63
64 #include "bhyverun.h"
65 #include "pci_emul.h"
66 #include "mevent.h"
67 #include "virtio.h"
68
69 #define VTNET_RINGSZ    1024
70
71 #define VTNET_MAXSEGS   256
72
73 /*
74  * Host capabilities.  Note that we only offer a few of these.
75  */
76 #define VIRTIO_NET_F_CSUM       (1 <<  0) /* host handles partial cksum */
77 #define VIRTIO_NET_F_GUEST_CSUM (1 <<  1) /* guest handles partial cksum */
78 #define VIRTIO_NET_F_MAC        (1 <<  5) /* host supplies MAC */
79 #define VIRTIO_NET_F_GSO_DEPREC (1 <<  6) /* deprecated: host handles GSO */
80 #define VIRTIO_NET_F_GUEST_TSO4 (1 <<  7) /* guest can rcv TSOv4 */
81 #define VIRTIO_NET_F_GUEST_TSO6 (1 <<  8) /* guest can rcv TSOv6 */
82 #define VIRTIO_NET_F_GUEST_ECN  (1 <<  9) /* guest can rcv TSO with ECN */
83 #define VIRTIO_NET_F_GUEST_UFO  (1 << 10) /* guest can rcv UFO */
84 #define VIRTIO_NET_F_HOST_TSO4  (1 << 11) /* host can rcv TSOv4 */
85 #define VIRTIO_NET_F_HOST_TSO6  (1 << 12) /* host can rcv TSOv6 */
86 #define VIRTIO_NET_F_HOST_ECN   (1 << 13) /* host can rcv TSO with ECN */
87 #define VIRTIO_NET_F_HOST_UFO   (1 << 14) /* host can rcv UFO */
88 #define VIRTIO_NET_F_MRG_RXBUF  (1 << 15) /* host can merge RX buffers */
89 #define VIRTIO_NET_F_STATUS     (1 << 16) /* config status field available */
90 #define VIRTIO_NET_F_CTRL_VQ    (1 << 17) /* control channel available */
91 #define VIRTIO_NET_F_CTRL_RX    (1 << 18) /* control channel RX mode support */
92 #define VIRTIO_NET_F_CTRL_VLAN  (1 << 19) /* control channel VLAN filtering */
93 #define VIRTIO_NET_F_GUEST_ANNOUNCE \
94                                 (1 << 21) /* guest can send gratuitous pkts */
95
96 #define VTNET_S_HOSTCAPS      \
97   ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \
98     VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
99
100 /*
101  * PCI config-space "registers"
102  */
103 struct virtio_net_config {
104         uint8_t  mac[6];
105         uint16_t status;
106 } __packed;
107
108 /*
109  * Queue definitions.
110  */
111 #define VTNET_RXQ       0
112 #define VTNET_TXQ       1
113 #define VTNET_CTLQ      2       /* NB: not yet supported */
114
115 #define VTNET_MAXQ      3
116
117 /*
118  * Fixed network header size
119  */
120 struct virtio_net_rxhdr {
121         uint8_t         vrh_flags;
122         uint8_t         vrh_gso_type;
123         uint16_t        vrh_hdr_len;
124         uint16_t        vrh_gso_size;
125         uint16_t        vrh_csum_start;
126         uint16_t        vrh_csum_offset;
127         uint16_t        vrh_bufs;
128 } __packed;
129
130 /*
131  * Debug printf
132  */
133 static int pci_vtnet_debug;
134 #define DPRINTF(params) if (pci_vtnet_debug) printf params
135 #define WPRINTF(params) printf params
136
137 /*
138  * Per-device softc
139  */
140 struct pci_vtnet_softc {
141         struct virtio_softc vsc_vs;
142         struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
143         pthread_mutex_t vsc_mtx;
144         struct mevent   *vsc_mevp;
145
146         int             vsc_tapfd;
147         struct nm_desc  *vsc_nmd;
148
149         int             vsc_rx_ready;
150         int             resetting;      /* protected by tx_mtx */
151
152         uint64_t        vsc_features;   /* negotiated features */
153         
154         struct virtio_net_config vsc_config;
155
156         pthread_mutex_t rx_mtx;
157         int             rx_vhdrlen;
158         int             rx_merge;       /* merged rx bufs in use */
159
160         pthread_t       tx_tid;
161         pthread_mutex_t tx_mtx;
162         pthread_cond_t  tx_cond;
163         int             tx_in_progress;
164
165         void (*pci_vtnet_rx)(struct pci_vtnet_softc *sc);
166         void (*pci_vtnet_tx)(struct pci_vtnet_softc *sc, struct iovec *iov,
167                              int iovcnt, int len);
168 };
169
170 static void pci_vtnet_reset(void *);
171 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
172 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
173 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
174 static void pci_vtnet_neg_features(void *, uint64_t);
175
176 static struct virtio_consts vtnet_vi_consts = {
177         "vtnet",                /* our name */
178         VTNET_MAXQ - 1,         /* we currently support 2 virtqueues */
179         sizeof(struct virtio_net_config), /* config reg size */
180         pci_vtnet_reset,        /* reset */
181         NULL,                   /* device-wide qnotify -- not used */
182         pci_vtnet_cfgread,      /* read PCI config */
183         pci_vtnet_cfgwrite,     /* write PCI config */
184         pci_vtnet_neg_features, /* apply negotiated features */
185         VTNET_S_HOSTCAPS,       /* our capabilities */
186 };
187
188 static void
189 pci_vtnet_reset(void *vsc)
190 {
191         struct pci_vtnet_softc *sc = vsc;
192
193         DPRINTF(("vtnet: device reset requested !\n"));
194
195         /* Acquire the RX lock to block RX processing. */
196         pthread_mutex_lock(&sc->rx_mtx);
197
198         /* Set sc->resetting and give a chance to the TX thread to stop. */
199         pthread_mutex_lock(&sc->tx_mtx);
200         sc->resetting = 1;
201         while (sc->tx_in_progress) {
202                 pthread_mutex_unlock(&sc->tx_mtx);
203                 usleep(10000);
204                 pthread_mutex_lock(&sc->tx_mtx);
205         }
206
207         sc->vsc_rx_ready = 0;
208         sc->rx_merge = 1;
209         sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
210
211         /*
212          * Now reset rings, MSI-X vectors, and negotiated capabilities.
213          * Do that with the TX lock held, since we need to reset
214          * sc->resetting.
215          */
216         vi_reset_dev(&sc->vsc_vs);
217
218         sc->resetting = 0;
219         pthread_mutex_unlock(&sc->tx_mtx);
220         pthread_mutex_unlock(&sc->rx_mtx);
221 }
222
223 /*
224  * Called to send a buffer chain out to the tap device
225  */
226 static void
227 pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
228                  int len)
229 {
230         static char pad[60]; /* all zero bytes */
231
232         if (sc->vsc_tapfd == -1)
233                 return;
234
235         /*
236          * If the length is < 60, pad out to that and add the
237          * extra zero'd segment to the iov. It is guaranteed that
238          * there is always an extra iov available by the caller.
239          */
240         if (len < 60) {
241                 iov[iovcnt].iov_base = pad;
242                 iov[iovcnt].iov_len = 60 - len;
243                 iovcnt++;
244         }
245         (void) writev(sc->vsc_tapfd, iov, iovcnt);
246 }
247
248 /*
249  *  Called when there is read activity on the tap file descriptor.
250  * Each buffer posted by the guest is assumed to be able to contain
251  * an entire ethernet frame + rx header.
252  *  MP note: the dummybuf is only used for discarding frames, so there
253  * is no need for it to be per-vtnet or locked.
254  */
255 static uint8_t dummybuf[2048];
256
257 static __inline struct iovec *
258 rx_iov_trim(struct iovec *iov, int *niov, int tlen)
259 {
260         struct iovec *riov;
261
262         /* XXX short-cut: assume first segment is >= tlen */
263         assert(iov[0].iov_len >= tlen);
264
265         iov[0].iov_len -= tlen;
266         if (iov[0].iov_len == 0) {
267                 assert(*niov > 1);
268                 *niov -= 1;
269                 riov = &iov[1];
270         } else {
271                 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
272                 riov = &iov[0];
273         }
274
275         return (riov);
276 }
277
278 static void
279 pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
280 {
281         struct iovec iov[VTNET_MAXSEGS], *riov;
282         struct vqueue_info *vq;
283         void *vrx;
284         int len, n;
285         uint16_t idx;
286
287         /*
288          * Should never be called without a valid tap fd
289          */
290         assert(sc->vsc_tapfd != -1);
291
292         /*
293          * But, will be called when the rx ring hasn't yet
294          * been set up.
295          */
296         if (!sc->vsc_rx_ready) {
297                 /*
298                  * Drop the packet and try later.
299                  */
300                 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
301                 return;
302         }
303
304         /*
305          * Check for available rx buffers
306          */
307         vq = &sc->vsc_queues[VTNET_RXQ];
308         if (!vq_has_descs(vq)) {
309                 /*
310                  * Drop the packet and try later.  Interrupt on
311                  * empty, if that's negotiated.
312                  */
313                 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
314                 vq_endchains(vq, 1);
315                 return;
316         }
317
318         do {
319                 /*
320                  * Get descriptor chain.
321                  */
322                 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
323                 assert(n >= 1 && n <= VTNET_MAXSEGS);
324
325                 /*
326                  * Get a pointer to the rx header, and use the
327                  * data immediately following it for the packet buffer.
328                  */
329                 vrx = iov[0].iov_base;
330                 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
331
332                 len = readv(sc->vsc_tapfd, riov, n);
333
334                 if (len < 0 && errno == EWOULDBLOCK) {
335                         /*
336                          * No more packets, but still some avail ring
337                          * entries.  Interrupt if needed/appropriate.
338                          */
339                         vq_retchain(vq);
340                         vq_endchains(vq, 0);
341                         return;
342                 }
343
344                 /*
345                  * The only valid field in the rx packet header is the
346                  * number of buffers if merged rx bufs were negotiated.
347                  */
348                 memset(vrx, 0, sc->rx_vhdrlen);
349
350                 if (sc->rx_merge) {
351                         struct virtio_net_rxhdr *vrxh;
352
353                         vrxh = vrx;
354                         vrxh->vrh_bufs = 1;
355                 }
356
357                 /*
358                  * Release this chain and handle more chains.
359                  */
360                 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
361         } while (vq_has_descs(vq));
362
363         /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
364         vq_endchains(vq, 1);
365 }
366
367 static __inline int
368 pci_vtnet_netmap_writev(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
369 {
370         int r, i;
371         int len = 0;
372
373         for (r = nmd->cur_tx_ring; ; ) {
374                 struct netmap_ring *ring = NETMAP_TXRING(nmd->nifp, r);
375                 uint32_t cur, idx;
376                 char *buf;
377
378                 if (nm_ring_empty(ring)) {
379                         r++;
380                         if (r > nmd->last_tx_ring)
381                                 r = nmd->first_tx_ring;
382                         if (r == nmd->cur_tx_ring)
383                                 break;
384                         continue;
385                 }
386                 cur = ring->cur;
387                 idx = ring->slot[cur].buf_idx;
388                 buf = NETMAP_BUF(ring, idx);
389
390                 for (i = 0; i < iovcnt; i++) {
391                         if (len + iov[i].iov_len > 2048)
392                                 break;
393                         memcpy(&buf[len], iov[i].iov_base, iov[i].iov_len);
394                         len += iov[i].iov_len;
395                 }
396                 ring->slot[cur].len = len;
397                 ring->head = ring->cur = nm_ring_next(ring, cur);
398                 nmd->cur_tx_ring = r;
399                 ioctl(nmd->fd, NIOCTXSYNC, NULL);
400                 break;
401         }
402
403         return (len);
404 }
405
406 static __inline int
407 pci_vtnet_netmap_readv(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
408 {
409         int len = 0;
410         int i = 0;
411         int r;
412
413         for (r = nmd->cur_rx_ring; ; ) {
414                 struct netmap_ring *ring = NETMAP_RXRING(nmd->nifp, r);
415                 uint32_t cur, idx;
416                 char *buf;
417                 size_t left;
418
419                 if (nm_ring_empty(ring)) {
420                         r++;
421                         if (r > nmd->last_rx_ring)
422                                 r = nmd->first_rx_ring;
423                         if (r == nmd->cur_rx_ring)
424                                 break;
425                         continue;
426                 }
427                 cur = ring->cur;
428                 idx = ring->slot[cur].buf_idx;
429                 buf = NETMAP_BUF(ring, idx);
430                 left = ring->slot[cur].len;
431
432                 for (i = 0; i < iovcnt && left > 0; i++) {
433                         if (iov[i].iov_len > left)
434                                 iov[i].iov_len = left;
435                         memcpy(iov[i].iov_base, &buf[len], iov[i].iov_len);
436                         len += iov[i].iov_len;
437                         left -= iov[i].iov_len;
438                 }
439                 ring->head = ring->cur = nm_ring_next(ring, cur);
440                 nmd->cur_rx_ring = r;
441                 ioctl(nmd->fd, NIOCRXSYNC, NULL);
442                 break;
443         }
444         for (; i < iovcnt; i++)
445                 iov[i].iov_len = 0;
446
447         return (len);
448 }
449
450 /*
451  * Called to send a buffer chain out to the vale port
452  */
453 static void
454 pci_vtnet_netmap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
455                     int len)
456 {
457         static char pad[60]; /* all zero bytes */
458
459         if (sc->vsc_nmd == NULL)
460                 return;
461
462         /*
463          * If the length is < 60, pad out to that and add the
464          * extra zero'd segment to the iov. It is guaranteed that
465          * there is always an extra iov available by the caller.
466          */
467         if (len < 60) {
468                 iov[iovcnt].iov_base = pad;
469                 iov[iovcnt].iov_len = 60 - len;
470                 iovcnt++;
471         }
472         (void) pci_vtnet_netmap_writev(sc->vsc_nmd, iov, iovcnt);
473 }
474
475 static void
476 pci_vtnet_netmap_rx(struct pci_vtnet_softc *sc)
477 {
478         struct iovec iov[VTNET_MAXSEGS], *riov;
479         struct vqueue_info *vq;
480         void *vrx;
481         int len, n;
482         uint16_t idx;
483
484         /*
485          * Should never be called without a valid netmap descriptor
486          */
487         assert(sc->vsc_nmd != NULL);
488
489         /*
490          * But, will be called when the rx ring hasn't yet
491          * been set up.
492          */
493         if (!sc->vsc_rx_ready) {
494                 /*
495                  * Drop the packet and try later.
496                  */
497                 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
498                 return;
499         }
500
501         /*
502          * Check for available rx buffers
503          */
504         vq = &sc->vsc_queues[VTNET_RXQ];
505         if (!vq_has_descs(vq)) {
506                 /*
507                  * Drop the packet and try later.  Interrupt on
508                  * empty, if that's negotiated.
509                  */
510                 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
511                 vq_endchains(vq, 1);
512                 return;
513         }
514
515         do {
516                 /*
517                  * Get descriptor chain.
518                  */
519                 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
520                 assert(n >= 1 && n <= VTNET_MAXSEGS);
521
522                 /*
523                  * Get a pointer to the rx header, and use the
524                  * data immediately following it for the packet buffer.
525                  */
526                 vrx = iov[0].iov_base;
527                 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
528
529                 len = pci_vtnet_netmap_readv(sc->vsc_nmd, riov, n);
530
531                 if (len == 0) {
532                         /*
533                          * No more packets, but still some avail ring
534                          * entries.  Interrupt if needed/appropriate.
535                          */
536                         vq_retchain(vq);
537                         vq_endchains(vq, 0);
538                         return;
539                 }
540
541                 /*
542                  * The only valid field in the rx packet header is the
543                  * number of buffers if merged rx bufs were negotiated.
544                  */
545                 memset(vrx, 0, sc->rx_vhdrlen);
546
547                 if (sc->rx_merge) {
548                         struct virtio_net_rxhdr *vrxh;
549
550                         vrxh = vrx;
551                         vrxh->vrh_bufs = 1;
552                 }
553
554                 /*
555                  * Release this chain and handle more chains.
556                  */
557                 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
558         } while (vq_has_descs(vq));
559
560         /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
561         vq_endchains(vq, 1);
562 }
563
564 static void
565 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
566 {
567         struct pci_vtnet_softc *sc = param;
568
569         pthread_mutex_lock(&sc->rx_mtx);
570         sc->pci_vtnet_rx(sc);
571         pthread_mutex_unlock(&sc->rx_mtx);
572
573 }
574
575 static void
576 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
577 {
578         struct pci_vtnet_softc *sc = vsc;
579
580         /*
581          * A qnotify means that the rx process can now begin
582          */
583         if (sc->vsc_rx_ready == 0) {
584                 sc->vsc_rx_ready = 1;
585                 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
586         }
587 }
588
589 static void
590 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
591 {
592         struct iovec iov[VTNET_MAXSEGS + 1];
593         int i, n;
594         int plen, tlen;
595         uint16_t idx;
596
597         /*
598          * Obtain chain of descriptors.  The first one is
599          * really the header descriptor, so we need to sum
600          * up two lengths: packet length and transfer length.
601          */
602         n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
603         assert(n >= 1 && n <= VTNET_MAXSEGS);
604         plen = 0;
605         tlen = iov[0].iov_len;
606         for (i = 1; i < n; i++) {
607                 plen += iov[i].iov_len;
608                 tlen += iov[i].iov_len;
609         }
610
611         DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n));
612         sc->pci_vtnet_tx(sc, &iov[1], n - 1, plen);
613
614         /* chain is processed, release it and set tlen */
615         vq_relchain(vq, idx, tlen);
616 }
617
618 static void
619 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
620 {
621         struct pci_vtnet_softc *sc = vsc;
622
623         /*
624          * Any ring entries to process?
625          */
626         if (!vq_has_descs(vq))
627                 return;
628
629         /* Signal the tx thread for processing */
630         pthread_mutex_lock(&sc->tx_mtx);
631         vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
632         if (sc->tx_in_progress == 0)
633                 pthread_cond_signal(&sc->tx_cond);
634         pthread_mutex_unlock(&sc->tx_mtx);
635 }
636
637 /*
638  * Thread which will handle processing of TX desc
639  */
640 static void *
641 pci_vtnet_tx_thread(void *param)
642 {
643         struct pci_vtnet_softc *sc = param;
644         struct vqueue_info *vq;
645         int error;
646
647         vq = &sc->vsc_queues[VTNET_TXQ];
648
649         /*
650          * Let us wait till the tx queue pointers get initialised &
651          * first tx signaled
652          */
653         pthread_mutex_lock(&sc->tx_mtx);
654         error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
655         assert(error == 0);
656
657         for (;;) {
658                 /* note - tx mutex is locked here */
659                 while (sc->resetting || !vq_has_descs(vq)) {
660                         vq->vq_used->vu_flags &= ~VRING_USED_F_NO_NOTIFY;
661                         mb();
662                         if (!sc->resetting && vq_has_descs(vq))
663                                 break;
664
665                         sc->tx_in_progress = 0;
666                         error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
667                         assert(error == 0);
668                 }
669                 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
670                 sc->tx_in_progress = 1;
671                 pthread_mutex_unlock(&sc->tx_mtx);
672
673                 do {
674                         /*
675                          * Run through entries, placing them into
676                          * iovecs and sending when an end-of-packet
677                          * is found
678                          */
679                         pci_vtnet_proctx(sc, vq);
680                 } while (vq_has_descs(vq));
681
682                 /*
683                  * Generate an interrupt if needed.
684                  */
685                 vq_endchains(vq, 1);
686
687                 pthread_mutex_lock(&sc->tx_mtx);
688         }
689 }
690
691 #ifdef notyet
692 static void
693 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
694 {
695
696         DPRINTF(("vtnet: control qnotify!\n\r"));
697 }
698 #endif
699
700 static int
701 pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr)
702 {
703         struct ether_addr *ea;
704         char *tmpstr;
705         char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
706
707         tmpstr = strsep(&mac_str,"=");
708
709         if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
710                 ea = ether_aton(mac_str);
711
712                 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
713                     memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
714                         fprintf(stderr, "Invalid MAC %s\n", mac_str);
715                         return (EINVAL);
716                 } else
717                         memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
718         }
719
720         return (0);
721 }
722
723 static void
724 pci_vtnet_tap_setup(struct pci_vtnet_softc *sc, char *devname)
725 {
726         char tbuf[80];
727 #ifndef WITHOUT_CAPSICUM
728         cap_rights_t rights;
729 #endif
730
731         strcpy(tbuf, "/dev/");
732         strlcat(tbuf, devname, sizeof(tbuf));
733
734         sc->pci_vtnet_rx = pci_vtnet_tap_rx;
735         sc->pci_vtnet_tx = pci_vtnet_tap_tx;
736
737         sc->vsc_tapfd = open(tbuf, O_RDWR);
738         if (sc->vsc_tapfd == -1) {
739                 WPRINTF(("open of tap device %s failed\n", tbuf));
740                 return;
741         }
742
743         /*
744          * Set non-blocking and register for read
745          * notifications with the event loop
746          */
747         int opt = 1;
748         if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
749                 WPRINTF(("tap device O_NONBLOCK failed\n"));
750                 close(sc->vsc_tapfd);
751                 sc->vsc_tapfd = -1;
752         }
753
754 #ifndef WITHOUT_CAPSICUM
755         cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
756         if (cap_rights_limit(sc->vsc_tapfd, &rights) == -1 && errno != ENOSYS)
757                 errx(EX_OSERR, "Unable to apply rights for sandbox");
758 #endif
759
760         sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
761                                   EVF_READ,
762                                   pci_vtnet_rx_callback,
763                                   sc);
764         if (sc->vsc_mevp == NULL) {
765                 WPRINTF(("Could not register event\n"));
766                 close(sc->vsc_tapfd);
767                 sc->vsc_tapfd = -1;
768         }
769 }
770
771 static void
772 pci_vtnet_netmap_setup(struct pci_vtnet_softc *sc, char *ifname)
773 {
774         sc->pci_vtnet_rx = pci_vtnet_netmap_rx;
775         sc->pci_vtnet_tx = pci_vtnet_netmap_tx;
776
777         sc->vsc_nmd = nm_open(ifname, NULL, 0, 0);
778         if (sc->vsc_nmd == NULL) {
779                 WPRINTF(("open of netmap device %s failed\n", ifname));
780                 return;
781         }
782
783         sc->vsc_mevp = mevent_add(sc->vsc_nmd->fd,
784                                   EVF_READ,
785                                   pci_vtnet_rx_callback,
786                                   sc);
787         if (sc->vsc_mevp == NULL) {
788                 WPRINTF(("Could not register event\n"));
789                 nm_close(sc->vsc_nmd);
790                 sc->vsc_nmd = NULL;
791         }
792 }
793
794 static int
795 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
796 {
797         MD5_CTX mdctx;
798         unsigned char digest[16];
799         char nstr[80];
800         char tname[MAXCOMLEN + 1];
801         struct pci_vtnet_softc *sc;
802         char *devname;
803         char *vtopts;
804         int mac_provided;
805
806         sc = calloc(1, sizeof(struct pci_vtnet_softc));
807
808         pthread_mutex_init(&sc->vsc_mtx, NULL);
809
810         vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues);
811         sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
812
813         sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
814         sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
815         sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
816         sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
817 #ifdef notyet
818         sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
819         sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
820 #endif
821  
822         /*
823          * Attempt to open the tap device and read the MAC address
824          * if specified
825          */
826         mac_provided = 0;
827         sc->vsc_tapfd = -1;
828         sc->vsc_nmd = NULL;
829         if (opts != NULL) {
830                 int err;
831
832                 devname = vtopts = strdup(opts);
833                 (void) strsep(&vtopts, ",");
834
835                 if (vtopts != NULL) {
836                         err = pci_vtnet_parsemac(vtopts, sc->vsc_config.mac);
837                         if (err != 0) {
838                                 free(devname);
839                                 return (err);
840                         }
841                         mac_provided = 1;
842                 }
843
844                 if (strncmp(devname, "vale", 4) == 0)
845                         pci_vtnet_netmap_setup(sc, devname);
846                 if (strncmp(devname, "tap", 3) == 0 ||
847                     strncmp(devname, "vmnet", 5) == 0)
848                         pci_vtnet_tap_setup(sc, devname);
849
850                 free(devname);
851         }
852
853         /*
854          * The default MAC address is the standard NetApp OUI of 00-a0-98,
855          * followed by an MD5 of the PCI slot/func number and dev name
856          */
857         if (!mac_provided) {
858                 snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
859                     pi->pi_func, vmname);
860
861                 MD5Init(&mdctx);
862                 MD5Update(&mdctx, nstr, strlen(nstr));
863                 MD5Final(digest, &mdctx);
864
865                 sc->vsc_config.mac[0] = 0x00;
866                 sc->vsc_config.mac[1] = 0xa0;
867                 sc->vsc_config.mac[2] = 0x98;
868                 sc->vsc_config.mac[3] = digest[0];
869                 sc->vsc_config.mac[4] = digest[1];
870                 sc->vsc_config.mac[5] = digest[2];
871         }
872
873         /* initialize config space */
874         pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
875         pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
876         pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
877         pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
878         pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
879
880         /* Link is up if we managed to open tap device or vale port. */
881         sc->vsc_config.status = (opts == NULL || sc->vsc_tapfd >= 0 ||
882             sc->vsc_nmd != NULL);
883         
884         /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
885         if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
886                 return (1);
887
888         /* use BAR 0 to map config regs in IO space */
889         vi_set_io_bar(&sc->vsc_vs, 0);
890
891         sc->resetting = 0;
892
893         sc->rx_merge = 1;
894         sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
895         pthread_mutex_init(&sc->rx_mtx, NULL); 
896
897         /* 
898          * Initialize tx semaphore & spawn TX processing thread.
899          * As of now, only one thread for TX desc processing is
900          * spawned. 
901          */
902         sc->tx_in_progress = 0;
903         pthread_mutex_init(&sc->tx_mtx, NULL);
904         pthread_cond_init(&sc->tx_cond, NULL);
905         pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
906         snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
907             pi->pi_func);
908         pthread_set_name_np(sc->tx_tid, tname);
909
910         return (0);
911 }
912
913 static int
914 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
915 {
916         struct pci_vtnet_softc *sc = vsc;
917         void *ptr;
918
919         if (offset < 6) {
920                 assert(offset + size <= 6);
921                 /*
922                  * The driver is allowed to change the MAC address
923                  */
924                 ptr = &sc->vsc_config.mac[offset];
925                 memcpy(ptr, &value, size);
926         } else {
927                 /* silently ignore other writes */
928                 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
929         }
930
931         return (0);
932 }
933
934 static int
935 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
936 {
937         struct pci_vtnet_softc *sc = vsc;
938         void *ptr;
939
940         ptr = (uint8_t *)&sc->vsc_config + offset;
941         memcpy(retval, ptr, size);
942         return (0);
943 }
944
945 static void
946 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
947 {
948         struct pci_vtnet_softc *sc = vsc;
949
950         sc->vsc_features = negotiated_features;
951
952         if (!(sc->vsc_features & VIRTIO_NET_F_MRG_RXBUF)) {
953                 sc->rx_merge = 0;
954                 /* non-merge rx header is 2 bytes shorter */
955                 sc->rx_vhdrlen -= 2;
956         }
957 }
958
959 struct pci_devemu pci_de_vnet = {
960         .pe_emu =       "virtio-net",
961         .pe_init =      pci_vtnet_init,
962         .pe_barwrite =  vi_pci_write,
963         .pe_barread =   vi_pci_read
964 };
965 PCI_EMUL_SET(pci_de_vnet);