From: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: ericvh@gmail.com, aliguori@us.ibm.com, jvrao@linux.vnet.ibm.com,
aneesh.kumar@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH -V6 03/21] virtio-9p: pdu processing support.
Date: Thu, 29 Apr 2010 17:44:45 +0530 [thread overview]
Message-ID: <1272543303-9830-4-git-send-email-aneesh.kumar@linux.vnet.ibm.com> (raw)
In-Reply-To: <1272543303-9830-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com>
From: Anthony Liguori <aliguori@us.ibm.com>
Add helpers to process the PDUs.
[kiran@linux.vnet.ibm.com: malloc to qemu_malloc coversion]
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
---
hw/virtio-9p-debug.h | 7 ++
hw/virtio-9p.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 250 insertions(+), 0 deletions(-)
create mode 100644 hw/virtio-9p-debug.h
diff --git a/hw/virtio-9p-debug.h b/hw/virtio-9p-debug.h
new file mode 100644
index 0000000..0104be5
--- /dev/null
+++ b/hw/virtio-9p-debug.h
@@ -0,0 +1,7 @@
+#ifndef _QEMU_VIRTIO_9P_DEBUG_H
+#define _QEMU_VIRTIO_9P_DEBUG_H
+
+extern int dotu;
+void pprint_pdu(V9fsPDU *pdu);
+
+#endif
diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c
index abd01c1..10d832a 100644
--- a/hw/virtio-9p.c
+++ b/hw/virtio-9p.c
@@ -71,6 +71,248 @@ size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
return copied;
}
+static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
+{
+ return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
+ offset, size, 0);
+}
+
+static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
+ size_t size)
+{
+ return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
+ offset, size, 1);
+}
+
+static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
+{
+ size_t pos = 0;
+ int i, j;
+ struct iovec *src_sg;
+ unsigned int num;
+
+ if (rx) {
+ src_sg = pdu->elem.in_sg;
+ num = pdu->elem.in_num;
+ } else {
+ src_sg = pdu->elem.out_sg;
+ num = pdu->elem.out_num;
+ }
+
+ j = 0;
+ for (i = 0; i < num; i++) {
+ if (offset <= pos) {
+ sg[j].iov_base = src_sg[i].iov_base;
+ sg[j].iov_len = src_sg[i].iov_len;
+ j++;
+ } else if (offset < (src_sg[i].iov_len + pos)) {
+ sg[j].iov_base = src_sg[i].iov_base;
+ sg[j].iov_len = src_sg[i].iov_len;
+ sg[j].iov_base += (offset - pos);
+ sg[j].iov_len -= (offset - pos);
+ j++;
+ }
+ pos += src_sg[i].iov_len;
+ }
+
+ return j;
+}
+
+static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
+{
+ size_t old_offset = offset;
+ va_list ap;
+ int i;
+
+ va_start(ap, fmt);
+ for (i = 0; fmt[i]; i++) {
+ switch (fmt[i]) {
+ case 'b': {
+ uint8_t *valp = va_arg(ap, uint8_t *);
+ offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
+ break;
+ }
+ case 'w': {
+ uint16_t val, *valp;
+ valp = va_arg(ap, uint16_t *);
+ val = le16_to_cpupu(valp);
+ offset += pdu_unpack(&val, pdu, offset, sizeof(val));
+ *valp = val;
+ break;
+ }
+ case 'd': {
+ uint32_t val, *valp;
+ valp = va_arg(ap, uint32_t *);
+ val = le32_to_cpupu(valp);
+ offset += pdu_unpack(&val, pdu, offset, sizeof(val));
+ *valp = val;
+ break;
+ }
+ case 'q': {
+ uint64_t val, *valp;
+ valp = va_arg(ap, uint64_t *);
+ val = le64_to_cpup(valp);
+ offset += pdu_unpack(&val, pdu, offset, sizeof(val));
+ *valp = val;
+ break;
+ }
+ case 'v': {
+ struct iovec *iov = va_arg(ap, struct iovec *);
+ int *iovcnt = va_arg(ap, int *);
+ *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
+ break;
+ }
+ case 's': {
+ V9fsString *str = va_arg(ap, V9fsString *);
+ offset += pdu_unmarshal(pdu, offset, "w", &str->size);
+ /* FIXME: sanity check str->size */
+ str->data = qemu_malloc(str->size + 1);
+ offset += pdu_unpack(str->data, pdu, offset, str->size);
+ str->data[str->size] = 0;
+ break;
+ }
+ case 'Q': {
+ V9fsQID *qidp = va_arg(ap, V9fsQID *);
+ offset += pdu_unmarshal(pdu, offset, "bdq",
+ &qidp->type, &qidp->version, &qidp->path);
+ break;
+ }
+ case 'S': {
+ V9fsStat *statp = va_arg(ap, V9fsStat *);
+ offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
+ &statp->size, &statp->type, &statp->dev,
+ &statp->qid, &statp->mode, &statp->atime,
+ &statp->mtime, &statp->length,
+ &statp->name, &statp->uid, &statp->gid,
+ &statp->muid, &statp->extension,
+ &statp->n_uid, &statp->n_gid,
+ &statp->n_muid);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ va_end(ap);
+
+ return offset - old_offset;
+}
+
+static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
+{
+ size_t old_offset = offset;
+ va_list ap;
+ int i;
+
+ va_start(ap, fmt);
+ for (i = 0; fmt[i]; i++) {
+ switch (fmt[i]) {
+ case 'b': {
+ uint8_t val = va_arg(ap, int);
+ offset += pdu_pack(pdu, offset, &val, sizeof(val));
+ break;
+ }
+ case 'w': {
+ uint16_t val;
+ cpu_to_le16w(&val, va_arg(ap, int));
+ offset += pdu_pack(pdu, offset, &val, sizeof(val));
+ break;
+ }
+ case 'd': {
+ uint32_t val;
+ cpu_to_le32w(&val, va_arg(ap, uint32_t));
+ offset += pdu_pack(pdu, offset, &val, sizeof(val));
+ break;
+ }
+ case 'q': {
+ uint64_t val;
+ cpu_to_le64w(&val, va_arg(ap, uint64_t));
+ offset += pdu_pack(pdu, offset, &val, sizeof(val));
+ break;
+ }
+ case 'v': {
+ struct iovec *iov = va_arg(ap, struct iovec *);
+ int *iovcnt = va_arg(ap, int *);
+ *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
+ break;
+ }
+ case 's': {
+ V9fsString *str = va_arg(ap, V9fsString *);
+ offset += pdu_marshal(pdu, offset, "w", str->size);
+ offset += pdu_pack(pdu, offset, str->data, str->size);
+ break;
+ }
+ case 'Q': {
+ V9fsQID *qidp = va_arg(ap, V9fsQID *);
+ offset += pdu_marshal(pdu, offset, "bdq",
+ qidp->type, qidp->version, qidp->path);
+ break;
+ }
+ case 'S': {
+ V9fsStat *statp = va_arg(ap, V9fsStat *);
+ offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
+ statp->size, statp->type, statp->dev,
+ &statp->qid, statp->mode, statp->atime,
+ statp->mtime, statp->length, &statp->name,
+ &statp->uid, &statp->gid, &statp->muid,
+ &statp->extension, statp->n_uid,
+ statp->n_gid, statp->n_muid);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ va_end(ap);
+
+ return offset - old_offset;
+}
+
+static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
+{
+ int8_t id = pdu->id + 1; /* Response */
+
+ if (len < 0) {
+ V9fsString str;
+ int err = -len;
+
+ str.data = strerror(err);
+ str.size = strlen(str.data);
+
+ len = 7;
+ len += pdu_marshal(pdu, len, "s", &str);
+ if (dotu) {
+ len += pdu_marshal(pdu, len, "d", err);
+ }
+
+ id = P9_RERROR;
+ }
+
+ /* fill out the header */
+ pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
+
+ /* keep these in sync */
+ pdu->size = len;
+ pdu->id = id;
+
+ /* push onto queue and notify */
+ virtqueue_push(s->vq, &pdu->elem, len);
+
+ /* FIXME: we should batch these completions */
+ virtio_notify(&s->vdev, s->vq);
+
+ free_pdu(s, pdu);
+}
+
+static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu)
+{
+ /* Note: The following have been added to prevent GCC from complaining
+ * They will be removed in the subsequent patches */
+ (void)pdu_unmarshal;
+ (void) complete_pdu;
+
+}
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
{
if (debug_9p_pdu) {
@@ -135,6 +377,7 @@ static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
{
+ v9fs_dummy(s, pdu);
if (debug_9p_pdu) {
pprint_pdu(pdu);
}
--
1.7.0.4.360.g11766c
next prev parent reply other threads:[~2010-04-29 12:15 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-29 12:14 [Qemu-devel] [PATCH -V6 00/21] virtio-9p: paravirtual file system passthrough Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 01/21] virtio-9p: Create a commandline option -fsdev Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 02/21] virtio-9p: Add a virtio 9p device to qemu Aneesh Kumar K.V
2010-04-29 12:14 ` Aneesh Kumar K.V [this message]
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 04/21] virtio-9p: Add string manipulation support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 05/21] virtio-9p: Add minimal set of FileOperations Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 06/21] virtio-9p: Add fid and qid management support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 07/21] virtio-9p: Add stat and mode related helper functions Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 08/21] virtio-9p: Add sg " Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 09/21] virtio-9p: Add P9_TVERSION support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 10/21] virtio-9p: Add P9_TATTACH support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 11/21] virtio-9p: Add P9_TSTAT support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 12/21] virtio-9p: Add P9_TWALK support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 13/21] virtio-9p: Add P9_TOPEN support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 14/21] virtio-9p: Add P9_TREAD support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 15/21] virtio-9p: Add P9_TCLUNK support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 16/21] virtio-9p: Add P9_TWRITE support Aneesh Kumar K.V
2010-04-29 12:14 ` [Qemu-devel] [PATCH -V6 17/21] virtio-9p: Add P9_TCREATE support Aneesh Kumar K.V
2010-04-29 12:15 ` [Qemu-devel] [PATCH -V6 18/21] virtio-9p: Add P9_TWSTAT support Aneesh Kumar K.V
2010-04-29 12:15 ` [Qemu-devel] [PATCH -V6 19/21] virtio-9p: Add P9_TREMOVE support Aneesh Kumar K.V
2010-04-29 12:15 ` [Qemu-devel] [PATCH -V6 20/21] virtio-9p: Add P9_TFLUSH support Aneesh Kumar K.V
2010-04-29 12:15 ` [Qemu-devel] [PATCH -V6 21/21] virtio-9p: Create a syntactic shortcut for the file-system pass-thru Aneesh Kumar K.V
2010-05-03 17:29 ` [Qemu-devel] [PATCH -V6 00/21] virtio-9p: paravirtual file system passthrough Anthony Liguori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1272543303-9830-4-git-send-email-aneesh.kumar@linux.vnet.ibm.com \
--to=aneesh.kumar@linux.vnet.ibm.com \
--cc=aliguori@us.ibm.com \
--cc=ericvh@gmail.com \
--cc=jvrao@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).