From 956a60247533e6a25d71b8803b5f41dbfb718ccd Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Tue, 13 Sep 2016 21:35:46 +0100 Subject: [PATCH] Simplified NBD handle comparisons 8 bytes, therefore a uing64_t to compare to, no need for memcmp() Signed-off-by: Michel Pollet --- src/common/nbdtypes.c | 8 ++++---- src/common/nbdtypes.h | 13 +++++++++---- src/common/readwrite.c | 6 ++---- src/proxy/proxy.c | 2 +- src/server/client.c | 6 +++--- src/server/mirror.c | 4 ++-- tests/unit/check_readwrite.c | 2 +- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/common/nbdtypes.c b/src/common/nbdtypes.c index 8403e73..18bd419 100644 --- a/src/common/nbdtypes.c +++ b/src/common/nbdtypes.c @@ -27,7 +27,7 @@ void nbd_r2h_request( struct nbd_request_raw *from, struct nbd_request * to ) { to->magic = htobe32( from->magic ); to->type = htobe32( from->type ); - memcpy( to->handle, from->handle, 8 ); + to->handle.w = from->handle.w; to->from = htobe64( from->from ); to->len = htobe32( from->len ); } @@ -36,7 +36,7 @@ void nbd_h2r_request( struct nbd_request * from, struct nbd_request_raw * to ) { to->magic = be32toh( from->magic ); to->type = be32toh( from->type ); - memcpy( to->handle, from->handle, 8 ); + to->handle.w = from->handle.w; to->from = be64toh( from->from ); to->len = be32toh( from->len ); } @@ -46,13 +46,13 @@ void nbd_r2h_reply( struct nbd_reply_raw * from, struct nbd_reply * to ) { to->magic = htobe32( from->magic ); to->error = htobe32( from->error ); - memcpy( to->handle, from->handle, 8 ); + to->handle.w = from->handle.w; } void nbd_h2r_reply( struct nbd_reply * from, struct nbd_reply_raw * to ) { to->magic = be32toh( from->magic ); to->error = be32toh( from->error ); - memcpy( to->handle, from->handle, 8 ); + to->handle.w = from->handle.w; } diff --git a/src/common/nbdtypes.h b/src/common/nbdtypes.h index 3f086dd..49139c4 100644 --- a/src/common/nbdtypes.h +++ b/src/common/nbdtypes.h @@ -24,6 +24,11 @@ #include #include +typedef union nbd_handle_t { + uint8_t b[8]; + uint64_t w; +} nbd_handle_t; + /* The _raw types are the types as they appear on the wire. Non-_raw * types are in host-format. * Conversion functions are _r2h_ for converting raw to host, and _h2r_ @@ -39,7 +44,7 @@ struct nbd_init_raw { struct nbd_request_raw { __be32 magic; __be32 type; /* == READ || == WRITE */ - char handle[8]; + nbd_handle_t handle; __be64 from; __be32 len; } __attribute__((packed)); @@ -47,7 +52,7 @@ struct nbd_request_raw { struct nbd_reply_raw { __be32 magic; __be32 error; /* 0 = ok, else error */ - char handle[8]; /* handle you got from request */ + nbd_handle_t handle; /* handle you got from request */ }; @@ -62,7 +67,7 @@ struct nbd_init { struct nbd_request { uint32_t magic; uint32_t type; /* == READ || == WRITE || == DISCONNECT */ - char handle[8]; + nbd_handle_t handle; uint64_t from; uint32_t len; } __attribute__((packed)); @@ -70,7 +75,7 @@ struct nbd_request { struct nbd_reply { uint32_t magic; uint32_t error; /* 0 = ok, else error */ - char handle[8]; /* handle you got from request */ + nbd_handle_t handle; /* handle you got from request */ }; void nbd_r2h_init( struct nbd_init_raw * from, struct nbd_init * to ); diff --git a/src/common/readwrite.c b/src/common/readwrite.c index cf065e5..30f63f6 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -105,9 +105,7 @@ void fill_request(struct nbd_request *request, int type, uint64_t from, uint32_t { request->magic = htobe32(REQUEST_MAGIC); request->type = htobe32(type); - uint32_t * randa = (uint32_t*)request->handle; - randa[0] = rand(); - randa[1] = rand(); + request->handle.w = (((uint64_t)rand()) << 32) | ((uint64_t)rand()); request->from = htobe64(from); request->len = htobe32(len); } @@ -127,7 +125,7 @@ void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply) if (reply->error != 0) { error("Server replied with error %d", reply->error); } - if (strncmp(request->handle, reply->handle, 8) != 0) { + if (request->handle.w != reply->handle.w) { error("Did not reply with correct handle"); } } diff --git a/src/proxy/proxy.c b/src/proxy/proxy.c index 58213be..31e3ff4 100644 --- a/src/proxy/proxy.c +++ b/src/proxy/proxy.c @@ -709,7 +709,7 @@ void proxy_session( struct proxier* proxy ) state_started = monotonic_time_ms(); debug( - "State transitition from %s to %s", + "State transition from %s to %s", proxy_session_state_names[old_state], proxy_session_state_names[state] ); diff --git a/src/server/client.c b/src/server/client.c index 37d0654..8b57b47 100644 --- a/src/server/client.c +++ b/src/server/client.c @@ -252,14 +252,14 @@ int client_read_request( struct client * client , struct nbd_request *out_reques return 1; } -int fd_write_reply( int fd, char *handle, int error ) +int fd_write_reply( int fd, uint64_t handle, int error ) { struct nbd_reply reply; struct nbd_reply_raw reply_raw; reply.magic = REPLY_MAGIC; reply.error = error; - memcpy( reply.handle, handle, 8 ); + reply.handle.w = handle; nbd_h2r_reply( &reply, &reply_raw ); debug( "Replying with handle=0x%08X, error=%"PRIu32, handle, error ); @@ -291,7 +291,7 @@ int fd_write_reply( int fd, char *handle, int error ) */ int client_write_reply( struct client * client, struct nbd_request *request, int error ) { - return fd_write_reply( client->socket, request->handle, error); + return fd_write_reply( client->socket, request->handle.w, error); } diff --git a/src/server/mirror.c b/src/server/mirror.c index 896399f..b3d22d5 100644 --- a/src/server/mirror.c +++ b/src/server/mirror.c @@ -412,7 +412,7 @@ int mirror_setup_next_xfer( struct mirror_ctrl *ctrl ) struct nbd_request req = { .magic = REQUEST_MAGIC, .type = REQUEST_WRITE, - .handle = ".MIRROR.", + .handle.b = ".MIRROR.", .from = current, .len = run }; @@ -568,7 +568,7 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents ) return; } - if ( memcmp( ".MIRROR.", &rsp.handle[0], 8 ) != 0 ) { + if ( memcmp( ".MIRROR.", rsp.handle.b, 8 ) != 0 ) { warn( "Bad handle returned from listener" ); ev_break( loop, EVBREAK_ONE ); return; diff --git a/tests/unit/check_readwrite.c b/tests/unit/check_readwrite.c index 88a0ece..91c48d1 100644 --- a/tests/unit/check_readwrite.c +++ b/tests/unit/check_readwrite.c @@ -57,7 +57,7 @@ void * responder( void *respond_uncast ) fd_write_reply( sock_fd, wrong_handle, 0 ); } else { - fd_write_reply( sock_fd, resp->received.handle, 0 ); + fd_write_reply( sock_fd, (char*)resp->received.handle.b, 0 ); } write( sock_fd, "12345678", 8 ); }