diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2011-09-19 13:48:43 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2011-12-22 11:53:58 +0100 |
commit | a478f6e595dab1801931d56d097623d65f5b6d1d (patch) | |
tree | 131836dcd6391f892936aff2890ef6ffd584c3d8 /nbd.c | |
parent | 128aa58947637b0989330c2e6a22a824d39e2193 (diff) |
qemu-nbd: simplify nbd_trip
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 <pbonzini@redhat.com>
Diffstat (limited to 'nbd.c')
-rw-r--r-- | nbd.c | 25 |
1 files changed, 8 insertions, 17 deletions
@@ -596,9 +596,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; } @@ -629,8 +629,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; @@ -638,26 +637,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"); |