-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathpci_virtio_9p.c.patch
More file actions
741 lines (710 loc) · 21 KB
/
pci_virtio_9p.c.patch
File metadata and controls
741 lines (710 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
diff --git a/src/lib/pci_virtio_9p.c b/src/lib/pci_virtio_9p.c
index ef23aa1..9df5bce 100644
--- a/src/lib/pci_virtio_9p.c
+++ b/src/lib/pci_virtio_9p.c
@@ -1,85 +1,115 @@
-#include <stdint.h>
-#include <stdlib.h>
+// +build lib9p
+
+/*-
+ * Copyright (c) 2016 Jakub Klama <jceel@FreeBSD.org>
+ * Copyright (c) 2016 iXsystems Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer
+ * in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * virtio filesystem passtrough using 9p protocol.
+ */
+
+#include <sys/cdefs.h>
+
+#include <sys/param.h>
+#include <sys/uio.h>
+
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <pthread.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <errno.h>
#include <assert.h>
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/uio.h>
-#include <arpa/inet.h>
-#include <xhyve/support/misc.h>
-#include <xhyve/support/linker_set.h>
+#include <pthread.h>
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wpadded"
+#include <lib9p.h>
+#pragma clang diagnostic pop
+
#include <xhyve/xhyve.h>
#include <xhyve/pci_emul.h>
#include <xhyve/virtio.h>
-#define VIRTIO_9P_MOUNT_TAG 1
+
+#define VT9P_RINGSZ 64
static int pci_vt9p_debug = 0;
#define DPRINTF(params) if (pci_vt9p_debug) printf params
-/* XXX issues with larger buffers elsewhere in stack */
-#define BUFSIZE (1 << 18)
-#define MAXDESC (BUFSIZE / 4096 + 4)
-#define VT9P_RINGSZ (BUFSIZE / 4096 * 4)
-
-struct virtio_9p_config {
- uint16_t tag_len;
- uint8_t tag[256];
-};
/*
* Per-device softc
*/
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wpadded"
-struct pci_vt9p_out {
- struct iovec wiov[MAXDESC];
- struct vqueue_info *vq;
- int inuse;
- uint16_t tag;
- uint16_t idx;
- uint16_t otag;
-};
struct pci_vt9p_softc {
- struct virtio_softc v9sc_vs;
- struct vqueue_info v9sc_vq;
- pthread_mutex_t v9sc_mtx;
- pthread_mutex_t v9sc_mtx2;
- pthread_t v9sc_thread;
- struct virtio_9p_config v9sc_cfg;
- struct pci_vt9p_out v9sc_out[VT9P_RINGSZ];
- /* -1 means not connected yet */
- int v9sc_sock;
- int v9sc_inflight;
- int port;
- char *path;
+ struct virtio_softc vsc_vs;
+ struct vqueue_info vsc_vq;
+ pthread_mutex_t vsc_mtx;
+ uint64_t vsc_cfg;
+ uint64_t vsc_features;
+ char * vsc_rootpath;
+ struct pci_vt9p_config * vsc_config;
+ struct l9p_backend * vsc_fs_backend;
+ struct l9p_server * vsc_server;
+ struct l9p_connection * vsc_conn;
+};
+
+struct pci_vt9p_request {
+ struct iovec * vsr_iov;
+ size_t vsr_niov;
+ size_t vsr_respidx;
+ size_t vsr_iolen;
+};
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
+struct pci_vt9p_config {
+ uint16_t tag_len;
+ char tag[0];
};
#pragma clang diagnostic pop
static void pci_vt9p_reset(void *);
static void pci_vt9p_notify(void *, struct vqueue_info *);
static int pci_vt9p_cfgread(void *, int, int, uint32_t *);
-static int pci_vt9p_cfgwrite(void *, int, int, uint32_t);
-static void pci_vt9p_lazy_initialise_socket(struct pci_vt9p_softc *sc);
-static void *pci_vt9p_thread(void *vsc);
+static void pci_vt9p_neg_features(void *, uint64_t);
static struct virtio_consts vt9p_vi_consts = {
- "vt9p", /* our name */
- 1, /* we support 1 virtqueue */
- 0, /* config reg size */
- pci_vt9p_reset, /* reset */
- pci_vt9p_notify, /* device-wide qnotify */
- pci_vt9p_cfgread, /* read virtio config */
- pci_vt9p_cfgwrite, /* write virtio config */
- NULL, /* apply negotiated features */
- VIRTIO_9P_MOUNT_TAG, /* our capabilities */
+ "vt9p", /* our name */
+ 1, /* we support 1 virtqueue */
+ 0, /* config reg size */
+ pci_vt9p_reset, /* reset */
+ pci_vt9p_notify, /* device-wide qnotify */
+ pci_vt9p_cfgread, /* read virtio config */
+ NULL, /* write virtio config */
+ pci_vt9p_neg_features, /* apply negotiated features */
+ (1 << 0), /* our capabilities */
};
+
static void
pci_vt9p_reset(void *vsc)
{
@@ -88,462 +118,161 @@ pci_vt9p_reset(void *vsc)
sc = vsc;
DPRINTF(("vt9p: device reset requested !\n"));
- vi_reset_dev(&sc->v9sc_vs);
+ vi_reset_dev(&sc->vsc_vs);
}
-/* Used to lazily initialise the socket */
static void
-pci_vt9p_lazy_initialise_socket(struct pci_vt9p_softc *sc)
+pci_vt9p_neg_features(void *vsc, uint64_t negotiated_features)
{
- struct sockaddr_in sa_in;
- struct sockaddr_un sa_un;
- int so;
- socklen_t sol = (socklen_t) sizeof(int);
-
- if (sc->v9sc_sock != -1)
- return;
- sa_in.sin_family = AF_INET;
- sa_in.sin_port = htons(sc->port);
- sa_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-
- sa_un.sun_family = AF_UNIX;
- memset(&sa_un, 0, sizeof(sa_un));
- strncpy(sa_un.sun_path, sc->path, sizeof(sa_un.sun_path)-1);
-
- int domain = (sc->port != -1)?AF_INET:AF_UNIX;
- struct sockaddr *sa = (sc->port != -1)?(struct sockaddr*)&sa_in:(struct sockaddr*)&sa_un;
- size_t sa_len = (sc->port != -1)?sizeof(sa_in):sizeof(sa_un);
-
- int max_attempts = 200; /* 200 * 50ms = 10s */
- do {
- sc->v9sc_sock = socket(domain, SOCK_STREAM, 0);
- if (sc->v9sc_sock == -1) {
- if (sc->port != -1) {
- fprintf(stderr, "virtio-9p: failed to connect to port %d: out of file descriptors\n", sc->port);
- } else {
- fprintf(stderr, "virtio-9p: failed to connect to path %s: out of file descriptors\n", sc->path);
- }
- /* The device won't work */
- return;
- }
-
- if (connect(sc->v9sc_sock, sa, (socklen_t)sa_len) == -1) {
- close(sc->v9sc_sock);
- sc->v9sc_sock = -1;
- usleep(50000);
- }
- } while ((sc->v9sc_sock == -1) && (--max_attempts > 0));
+ struct pci_vt9p_softc *sc = vsc;
- if (sc->v9sc_sock == -1) {
- if (sc->port != -1) {
- fprintf(stderr, "virtio-9p: failed to connect to port %d\n", sc->port);
- } else {
- fprintf(stderr, "virtio-9p: failed to connect to path %s\n", sc->path);
- }
- /* The device won't work */
- }
- if (getsockopt(sc->v9sc_sock, SOL_SOCKET, SO_SNDBUF, &so, &sol) != -1) {
- if (so < 2 * BUFSIZE) {
- so = 2 * BUFSIZE;
- (void)setsockopt(sc->v9sc_sock, SOL_SOCKET, SO_SNDBUF, &so, sol);
- (void)setsockopt(sc->v9sc_sock, SOL_SOCKET, SO_RCVBUF, &so, sol);
- }
- }
- if (pthread_create(&sc->v9sc_thread, NULL, pci_vt9p_thread, sc) == -1) {
- perror("pthread_create");
- /* The device won't work */
- }
+ sc->vsc_features = negotiated_features;
}
-static void
-pci_vt9p_notify(void *vsc, struct vqueue_info *vq)
+static int
+pci_vt9p_cfgread(void *vsc, int offset, int size, uint32_t *retval)
{
- struct iovec iov[MAXDESC];
- uint16_t flags[MAXDESC];
struct pci_vt9p_softc *sc = vsc;
- uint16_t idx;
- ssize_t n;
- int nvec, i, freevec;
- struct iovec *wiov;
- int nread, nwrite;
- size_t readbytes;
- uint16_t tag;
- uint32_t len;
- uint8_t command;
- uint16_t otag = 0;
- int used = 0;
-
- sc = vsc;
-
- pci_vt9p_lazy_initialise_socket(sc);
-
- /* will be a socket here */
- if (sc->v9sc_sock < 0) {
- DPRINTF(("vt9p socket invalid\r\n"));
- vq_endchains(vq, 0);
- return;
- }
+ void *ptr;
- nvec = vq_getchain(vq, &idx, iov, MAXDESC, flags);
+ ptr = (uint8_t *)sc->vsc_config + offset;
+ memcpy(retval, ptr, size);
+ return (0);
+}
- if (nvec == -1) {
- DPRINTF(("vt9p bad descriptors\r\n"));
- return; /* what to do? */
- }
+static int
+pci_vt9p_get_buffer(struct l9p_request *req, struct iovec *iov, size_t *niov,
+ void *arg __unused)
+{
+ struct pci_vt9p_request *preq = req->lr_aux;
+ size_t n = preq->vsr_niov - preq->vsr_respidx;
+
+ memcpy(iov, preq->vsr_iov + preq->vsr_respidx, n * sizeof(struct iovec));
+ *niov = n;
+ return (0);
+}
- if (nvec == 0) {
- DPRINTF(("vt9p got all the descriptors\r\n"));
- return;
- }
- DPRINTF(("vt9p got %d descriptors\r\n", nvec));
-
- wiov = NULL;
- nwrite = 0;
- nread = 0;
- readbytes = 0;
- tag = 0;
- freevec = -1;
-
- DPRINTF(("vtrnd: vt9p_notify(): %d count %d\r\n", (int)idx, nvec));
- for (i = 0; i < nvec; i++) {
- DPRINTF(("vt9p iovec %d len %d\r\n", i, (int)iov[i].iov_len));
- if (flags[i] & VRING_DESC_F_WRITE) {
- DPRINTF(("writeable\r\n"));
- nwrite++;
- } else {
- if (nwrite == 0) {
- nread++;
- readbytes += iov[i].iov_len;
- DPRINTF(("readable\r\n"));
- } else {
- DPRINTF(("ignoring readable buffers after writeable ones\r\n"));
- }
- }
- if (wiov == NULL && (flags[i] & VRING_DESC_F_WRITE)) {
- wiov = &iov[i];
- DPRINTF(("vt9p wiov is %p\r\n", (void*)wiov));
- }
- }
- /* do this properly */
- if (iov[0].iov_len >= 7) {
- uint8_t *ptr = (uint8_t *)iov[0].iov_base;
- len = (uint32_t)ptr[0] | ((uint32_t)ptr[1] << 8) | ((uint32_t)ptr[2] << 16) | ((uint32_t)ptr[3] << 24);
- command = ptr[4];
- tag = (uint16_t)((uint16_t)ptr[5] | ((uint16_t)ptr[6] << 8));
- DPRINTF(("vt9p len %d\r\n", (int)len));
- DPRINTF(("vt9p command %d\r\n", (int)command));
- DPRINTF(("vt9p tag %d\r\n", (int)tag));
- otag = 0;
- if (command == 108 && iov[0].iov_len >= 9) {
- otag = (uint16_t)((uint16_t)ptr[7] | ((uint16_t)ptr[8] << 8));
- DPRINTF(("TFlush otag %d\r\n", (int)otag));
- }
- /* Linux is buggy with writes over 1k, has a buggy zero copy codepath, fix up */
- if (command == 118 && iov[0].iov_len >= 23) {
- uint32_t wlen = (uint32_t)ptr[19] | ((uint32_t)ptr[20] << 8) | ((uint32_t)ptr[21] << 16) | ((uint32_t)ptr[22] << 24);
- DPRINTF(("Twrite wlen %d readbytes %d len %d\r\n", (int)wlen, (int)readbytes, (int)len));
- if (readbytes != len) {
- DPRINTF(("FIXUP! len\n"));
- ptr[0] = (uint8_t)(readbytes & 0xff);
- ptr[1] = (uint8_t)((readbytes >> 8) & 0xff);
- ptr[2] = (uint8_t)((readbytes >> 16) & 0xff);
- ptr[3] = (uint8_t)((readbytes >> 24) & 0xff);
- }
- /* XXX not sure seeing this now */
- if (wlen != readbytes - 23) {
- DPRINTF(("FIXUP! wlen\n"));
- wlen = (uint32_t) (readbytes - 23);
- ptr[19] = (uint8_t)(wlen & 0xff);
- ptr[20] = (uint8_t)((wlen >> 8) & 0xff);
- ptr[21] = (uint8_t)((wlen >> 16) & 0xff);
- ptr[22] = (uint8_t)((wlen >> 24) & 0xff);
- }
- }
- } else {
- DPRINTF(("vt9p oops split iovec for command - do this properly\r\n"));
- }
+static int
+pci_vt9p_send(struct l9p_request *req, const struct iovec *iov __unused,
+ const size_t niov __unused, const size_t iolen, void *arg __unused)
+{
+ struct pci_vt9p_request *preq = req->lr_aux;
- if (nwrite == 0) {
- DPRINTF(("Nowhere to write to!!\r\n"));
- }
- /* do something with request! */
- pthread_mutex_lock(&sc->v9sc_mtx2);
- sc->v9sc_inflight++;
- for (i = 0; i < VT9P_RINGSZ; i++) {
- if (sc->v9sc_out[i].inuse == 1) {
- used++;
- continue;
- }
- sc->v9sc_out[i].inuse = 1;
- memcpy(sc->v9sc_out[i].wiov, wiov, (size_t)(sizeof(struct iovec) * (size_t)nwrite));
- sc->v9sc_out[i].vq = vq;
- sc->v9sc_out[i].tag = tag;
- sc->v9sc_out[i].idx = idx;
- sc->v9sc_out[i].otag = otag;
- break;
- }
- if (used == VT9P_RINGSZ) {
- fprintf(stderr, "virtio-9p: Ring full!\n");
- _exit(1);
- }
- pthread_mutex_unlock(&sc->v9sc_mtx2);
-
- i = 0;
- while (readbytes) {
- n = writev(sc->v9sc_sock, &iov[i], nread);
- if (n <= 0) {
- fprintf(stderr, "virtio-9p: unexpected EOF writing to server-- did the 9P server crash?\n");
- /* Fatal error, crash VM, let us be restarted */
- _exit(1);
- }
- DPRINTF(("vt9p: wrote to sock %d bytes\r\n", (int)n));
- readbytes -= (size_t)n;
- if (readbytes != 0) {
- while ((size_t)n >= iov[i].iov_len) {
- n -= iov[i].iov_len;
- i++;
- }
- iov[i].iov_len -= (size_t) n;
- iov[i].iov_base = (char *) iov[i].iov_base + n;
- }
- }
+ preq->vsr_iolen = iolen;
+ return (0);
}
-static void *
-pci_vt9p_thread(void *vsc)
+static void
+pci_vt9p_notify(void *vsc, struct vqueue_info *vq)
{
- struct pci_vt9p_softc *sc = vsc;
- ssize_t ret;
- size_t n;
- size_t minlen = 7;
- uint32_t len;
- uint16_t tag, otag;
- uint8_t command;
- uint8_t *ptr;
- int i, ii, j;
- struct iovec *wiov;
- uint8_t *buf;
- char ident[16];
-
- snprintf(ident, sizeof(ident), "9p:%s", sc->v9sc_cfg.tag);
- pthread_setname_np(ident);
-
- buf = calloc(1, BUFSIZE);
- if (! buf) {
- fprintf(stderr, "virtio-p9: memory allocation failed\n");
- _exit(1);
- }
+ struct iovec iov[8];
+ struct pci_vt9p_softc *sc;
+ struct pci_vt9p_request preq;
+ uint16_t idx, i;
+ uint16_t flags[8];
+ int n;
- while (1) {
- ptr = buf;
- n = 0;
- while (n < minlen) {
- ret = read(sc->v9sc_sock, ptr, minlen - n);
- if (ret <= 0) {
- fprintf(stderr, "virtio-9p: unexpected EOF reading -- did the 9P server crash?\n");
- /* Fatal error, crash VM, let us be restarted */
- _exit(1);
- }
- n += (size_t) ret;
- ptr += ret;
- }
- len = (uint32_t)buf[0] | ((uint32_t)buf[1] << 8) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 24);
- command = buf[4];
- tag = (uint16_t)((uint16_t)buf[5] | ((uint16_t)buf[6] << 8));
- DPRINTF(("[thread]Got response for tag %d command %d len %d\r\n", (int)tag, (int)command, (int)len));
- n = (size_t)(len - minlen);
- ptr = buf + minlen;
- while (n) {
- assert(len <= BUFSIZE);
- ret = read(sc->v9sc_sock, ptr, n);
- if (ret <= 0) {
- fprintf(stderr, "virtio-9p: unexpected EOF reading-- did the 9P server crash?\n");
- /* Fatal error, crash the VM, let us be restarted */
- _exit(1);
- }
- n -= (size_t) ret;
- ptr += ret;
- }
- DPRINTF(("[thread]got complete response for tag %d len %d\r\n", (int)tag, (int)len));
- if (command == 107) {
- char msg[128];
- uint16_t slen = (uint16_t)((uint16_t)buf[7] | ((uint16_t)buf[8] << 8));
- memcpy(msg, &buf[9], slen);
- msg[slen] = 0;
- DPRINTF(("[thread]Rerror: %s\r\n", msg));
- }
- if (command == 109) { /* Rflush */
- for (i = 0; i < VT9P_RINGSZ; i++) {
- if (sc->v9sc_out[i].tag == tag) {
- otag = sc->v9sc_out[i].otag;
- for (j = 0; j < VT9P_RINGSZ; j++) {
- if (sc->v9sc_out[j].tag == otag && sc->v9sc_out[j].inuse) {
- pthread_mutex_lock(&sc->v9sc_mtx2);
- sc->v9sc_out[j].inuse = 0;
- sc->v9sc_inflight--;
- vq_relchain(sc->v9sc_out[j].vq, sc->v9sc_out[j].idx, ((uint32_t) 0));
- pthread_mutex_unlock(&sc->v9sc_mtx2);
- break;
- }
- }
- break;
- }
- }
- }
- for (i = 0; i < VT9P_RINGSZ; i++) {
- if (sc->v9sc_out[i].tag == tag) {
- wiov = sc->v9sc_out[i].wiov;
- ii = 0;
- ptr = buf;
- n = len;
- while (n) {
- size_t m = n;
- if (m > wiov[ii].iov_len)
- m = wiov[ii].iov_len;
- DPRINTF(("[thread]copy %d bytes to iov at %p\r\n", (int)m, wiov[ii].iov_base));
- memcpy(wiov[ii].iov_base, ptr, m);
- ptr += m;
- n -= (size_t)m;
- ii++;
- }
- DPRINTF(("[thread]release\r\n"));
- pthread_mutex_lock(&sc->v9sc_mtx2);
- vq_relchain(sc->v9sc_out[i].vq, sc->v9sc_out[i].idx, ((uint32_t) len));
- sc->v9sc_out[i].inuse = 0;
- sc->v9sc_inflight--;
- /* Generate interrupt even if some requests are outstanding, because
- if we're using a blocking poll then we expect one request to be
- permanently outstanding at all times. */
- DPRINTF(("[thread]endchain\r\n"));
- vq_endchains(sc->v9sc_out[i].vq, 1);
- pthread_mutex_unlock(&sc->v9sc_mtx2);
+ sc = vsc;
+
+ while (vq_has_descs(vq)) {
+ n = vq_getchain(vq, &idx, iov, 8, flags);
+ preq.vsr_iov = iov;
+ preq.vsr_niov = (size_t)n;
+ preq.vsr_respidx = 0;
+
+ /* Count readable descriptors */
+ for (i = 0; i < n; i++) {
+ if (flags[i] & VRING_DESC_F_WRITE)
break;
- }
+
+ preq.vsr_respidx++;
}
- }
- return NULL;
-}
+ for (i = 0; i < n; i++)
+ DPRINTF(("vt9p: vt9p_notify(): desc%d base=%p, len=%zu, flags=0x%04x\r\n", i, iov[i].iov_base, iov[i].iov_len, flags[i]));
-static char *
-copy_up_to_comma(const char *from)
-{
- char *comma = strchr(from, ',');
- char *tmp = NULL;
- if (comma == NULL) {
- tmp = strdup(from); /* rest of string */
- } else {
- size_t length = (size_t)(comma - from);
- tmp = strndup(from, length);
+ l9p_connection_recv(sc->vsc_conn, iov, preq.vsr_respidx, &preq);
+
+ /*
+ * Release this chain and handle more
+ */
+ vq_relchain(vq, idx, (uint32_t)preq.vsr_iolen);
}
- return tmp;
+ vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
}
+
static int
pci_vt9p_init(struct pci_devinst *pi, char *opts)
{
struct pci_vt9p_softc *sc;
+ char *opt;
+ char *sharename = NULL;
+ char *rootpath = NULL;
- int port = -1; /* if != -1, the port is valid. path is valid otherwise */
- char *path = "";
- char *tag = "plan9";
+ if (opts == NULL) {
+ printf("virtio-9p: share name and path required\n");
+ return (1);
+ }
sc = calloc(1, sizeof(struct pci_vt9p_softc));
- if (! sc) {
- return 1;
- }
- sc->v9sc_sock = -1;
- fprintf(stdout, "virtio-9p: initialising %s\n", opts);
-
- while (1) {
- char *next;
- if (! opts)
- break;
- next = strchr(opts, ',');
- if (next)
- next[0] = '\0';
- if (strncmp(opts, "port=", 5) == 0) {
- port = atoi(&opts[5]);
- if (port == 0) {
- fprintf(stderr, "bad port: %s\r\n", opts);
- return 1;
- }
- } else if (strncmp(opts, "path=", 5) == 0) {
- path = copy_up_to_comma(opts + 5);
- } else if (strncmp(opts, "tag=", 4) == 0) {
- tag = copy_up_to_comma(opts + 4);
- } else {
- fprintf(stderr, "invalid option: %s\r\n", opts);
- return 1;
+ sc->vsc_config = calloc(1, sizeof(struct pci_vt9p_config) + 128);
+
+ while ((opt = strsep(&opts, ",")) != NULL) {
+ if (sharename == NULL) {
+ sharename = strsep(&opt, "=");
+ rootpath = strdup(opt);
+ continue;
}
- if (! next)
- break;
- opts = &next[1];
+ if (strcmp(opt, "ro") == 0)
+ DPRINTF(("read-only mount requested\r\n"));
}
- if (!((port == -1) != (strcmp(path, "") == 0))) {
- fprintf(stderr, "Please pass *either* a port *or* a path. You must pass one, you must not pass both.\n");
- return 1;
+
+ sc->vsc_config->tag_len = (uint16_t)strlen(sharename);
+ strncpy(sc->vsc_config->tag, sharename, strlen(sharename));
+
+ if (l9p_backend_fs_init(&sc->vsc_fs_backend, rootpath) != 0) {
+ errno = ENXIO;
+ return (1);
}
- sc->port = port;
- sc->path = path;
- sc->v9sc_cfg.tag_len = (uint16_t) strlen(tag);
- if (sc->v9sc_cfg.tag_len > 256) {
- return 1;
+ if (l9p_server_init(&sc->vsc_server, sc->vsc_fs_backend) != 0) {
+ errno = ENXIO;
+ return (1);
}
- memcpy(sc->v9sc_cfg.tag, tag, sc->v9sc_cfg.tag_len);
- pthread_mutex_init(&sc->v9sc_mtx, NULL);
- pthread_mutex_init(&sc->v9sc_mtx2, NULL);
+ if (l9p_connection_init(sc->vsc_server, &sc->vsc_conn) != 0) {
+ errno = EIO;
+ return (1);
+ }
+ l9p_connection_on_send_response(sc->vsc_conn, pci_vt9p_send, NULL);
+ l9p_connection_on_get_response_buffer(sc->vsc_conn, pci_vt9p_get_buffer, NULL);
- vi_softc_linkup(&sc->v9sc_vs, &vt9p_vi_consts, sc, pi, &sc->v9sc_vq);
- sc->v9sc_vs.vs_mtx = &sc->v9sc_mtx;
+ vi_softc_linkup(&sc->vsc_vs, &vt9p_vi_consts, sc, pi, &sc->vsc_vq);
+ sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
- sc->v9sc_vq.vq_qsize = VT9P_RINGSZ;
+ sc->vsc_vq.vq_qsize = VT9P_RINGSZ;
/* initialize config space */
pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_9P);
pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
- pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_OTHER);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_9P);
pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
- if (vi_intr_init(&sc->v9sc_vs, 1, fbsdrun_virtio_msix()))
+ if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
return (1);
- vi_set_io_bar(&sc->v9sc_vs, 0);
+ vi_set_io_bar(&sc->vsc_vs, 0);
return (0);
}
-
-static int
-pci_vt9p_cfgwrite(UNUSED void *vsc, int offset, UNUSED int size,
- UNUSED uint32_t value)
-{
- DPRINTF(("vt9p: write to reg %d\n\r", offset));
- return 1;
-}
-
-static int
-pci_vt9p_cfgread(void *vsc, int offset, int size, uint32_t *retval)
-{
- struct pci_vt9p_softc *sc = vsc;
- void *ptr;
-
- DPRINTF(("vt9p: read to reg %d\n\r", offset));
- ptr = (uint8_t *)&sc->v9sc_cfg + offset;
- memcpy(retval, ptr, size);
-
- return 0;
-}
-
-
-static struct pci_devemu pci_de_v9p = {
- .pe_emu = "virtio-9p",
- .pe_init = pci_vt9p_init,
+static struct pci_devemu pci_dev_9p = {
+ .pe_emu = "virtio-9p",
+ .pe_init = pci_vt9p_init,
.pe_barwrite = vi_pci_write,
.pe_barread = vi_pci_read
};
-PCI_EMUL_SET(pci_de_v9p);
+PCI_EMUL_SET(pci_dev_9p);