From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:55829) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDCJF-0001BB-Pn for qemu-devel@nongnu.org; Mon, 10 Oct 2011 05:38:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RDCJD-0000wG-FH for qemu-devel@nongnu.org; Mon, 10 Oct 2011 05:38:09 -0400 Received: from mail-bw0-f45.google.com ([209.85.214.45]:57071) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDCJD-0000ul-A6 for qemu-devel@nongnu.org; Mon, 10 Oct 2011 05:38:07 -0400 Received: by mail-bw0-f45.google.com with SMTP id zv15so8235447bkb.4 for ; Mon, 10 Oct 2011 02:38:06 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Mon, 10 Oct 2011 11:37:46 +0200 Message-Id: <1318239477-31451-5-git-send-email-pbonzini@redhat.com> In-Reply-To: <1318239477-31451-1-git-send-email-pbonzini@redhat.com> References: <1318239477-31451-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 04/15] qemu-nbd: simplify nbd_trip List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Use TCP_CORK to remove a violation of encapsulation, that would later require nbd_trip to know too much about an NBD reply. We could also switch to sendmsg (qemu_co_sendv) later, it is even easier once coroutines are in. Signed-off-by: Paolo Bonzini --- nbd.c | 25 ++++++++----------------- 1 files changed, 8 insertions(+), 17 deletions(-) diff --git a/nbd.c b/nbd.c index 59c47ee..252fa7c 100644 --- a/nbd.c +++ b/nbd.c @@ -598,9 +598,9 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, if (nbd_receive_request(csock, &request) == -1) return -1; - if (request.len + NBD_REPLY_SIZE > NBD_BUFFER_SIZE) { + if (request.len > NBD_BUFFER_SIZE) { LOG("len (%u) is larger than max len (%u)", - request.len + NBD_REPLY_SIZE, NBD_BUFFER_SIZE); + request.len, NBD_BUFFER_SIZE); errno = EINVAL; return -1; } @@ -631,8 +631,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, TRACE("Request type is READ"); ret = bdrv_read(bs, (request.from + dev_offset) / 512, - data + NBD_REPLY_SIZE, - request.len / 512); + data, request.len / 512); if (ret < 0) { LOG("reading from file failed"); reply.error = -ret; @@ -640,26 +639,18 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, } TRACE("Read %u byte(s)", request.len); - - /* Reply - [ 0 .. 3] magic (NBD_REPLY_MAGIC) - [ 4 .. 7] error (0 == no error) - [ 7 .. 15] handle - */ - - cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC); - cpu_to_be32w((uint32_t*)(data + 4), reply.error); - cpu_to_be64w((uint64_t*)(data + 8), reply.handle); + socket_set_cork(csock, 1); + if (nbd_send_reply(csock, &reply) == -1) + return -1; TRACE("Sending data to client"); - if (write_sync(csock, data, - request.len + NBD_REPLY_SIZE) != - request.len + NBD_REPLY_SIZE) { + if (write_sync(csock, data, request.len) != request.len) { LOG("writing to socket failed"); errno = EINVAL; return -1; } + socket_set_cork(csock, 0); break; case NBD_CMD_WRITE: TRACE("Request type is WRITE"); -- 1.7.6