From patchwork Thu Apr 15 14:10:53 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 50259 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 918CDB7CFA for ; Fri, 16 Apr 2010 00:54:44 +1000 (EST) Received: from localhost ([127.0.0.1]:56799 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2QSb-0000Kv-KR for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 10:54:29 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2Ptb-0003aF-RA for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:20 -0400 Received: from [140.186.70.92] (port=50430 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtW-0003MB-54 for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pmt-0000Fr-92 for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:33 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:49962) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pms-0000Em-9i for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:23 -0400 Received: from d28relay05.in.ibm.com (d28relay05.in.ibm.com [9.184.220.62]) by e28smtp07.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FEBI5n032233 for ; Thu, 15 Apr 2010 19:41:18 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay05.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBIkH3457072 for ; Thu, 15 Apr 2010 19:41:18 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3FEBInx030432 for ; Thu, 15 Apr 2010 19:41:18 +0530 Received: from skywalker.in.ibm.com ([9.77.204.187]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3FEBDa7030329; Thu, 15 Apr 2010 19:41:17 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:40:53 +0530 Message-Id: <1271340671-19558-4-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V5 03/21] virtio-9p: pdu processing support. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Anthony Liguori Add helpers to process the PDUs. [kiran@linux.vnet.ibm.com: malloc to qemu_malloc coversion] Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- 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 0685c2d..0b95eeb 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -73,6 +73,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) { @@ -137,6 +379,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); }