Skip to content

Commit

Permalink
fix NBD_OPT_LIST
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Aug 30, 2021
1 parent b75de6f commit e03af9b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 75 deletions.
1 change: 1 addition & 0 deletions modules/network/lwnbdsvr/drivers/atad.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int atad_ctor(atad_driver *const me, int device)
strcpy(me->super.export_name, "hdd0");
me->super.blockshift = 9;
me->super.buffer = nbd_buffer;
me->super.eflags = NBD_FLAG_HAS_FLAGS;

if (dev_info != NULL && dev_info->exists) {
me->super.export_size = (uint64_t)dev_info->total_sectors << me->super.blockshift;
Expand Down
39 changes: 22 additions & 17 deletions modules/network/lwnbdsvr/lwNBD/nbd_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ uint8_t nbd_buffer[NBD_BUFFER_LEN] __attribute__((aligned(64)));
* @param client_socket
* @param ctx NBD callback struct
*/
nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)
nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs)
{
register int size;
uint32_t cflags, name_len, desc_len, len;
struct nbd_new_option new_opt;
struct nbd_export_name_option_reply handshake_finish;
struct nbd_fixed_new_option_reply fixed_new_option_reply;
struct nbd_new_handshake new_hs;
nbd_context **ptr_ctx = ctxs;

//temporary workaround
nbd_context *ctx = ctxs[0];
Expand All @@ -63,7 +64,7 @@ nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)

new_hs.nbdmagic = htonll(NBD_MAGIC);
new_hs.version = htonll(NBD_NEW_VERSION);
new_hs.gflags = 0; //htons(NBD_FLAG_FIXED_NEWSTYLE);
new_hs.gflags = htons(NBD_FLAG_FIXED_NEWSTYLE);
size = send(client_socket, &new_hs, sizeof(struct nbd_new_handshake),
0);
if (size < sizeof(struct nbd_new_handshake))
Expand All @@ -77,8 +78,6 @@ nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)
* https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md#client-flags
*/

ctx->eflags = NBD_FLAG_HAS_FLAGS;

while (1) {

/*** options haggling ***/
Expand All @@ -100,6 +99,8 @@ nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)
nbd_buffer[new_opt.optlen] = '\0';
}

printf("%d\n", new_opt.option);

switch (new_opt.option) {

case NBD_OPT_EXPORT_NAME:
Expand All @@ -125,22 +126,26 @@ nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)
// see nbdkit send_newstyle_option_reply_exportnames()
case NBD_OPT_LIST:

name_len = strlen(ctx->export_name);
desc_len = ctx->export_desc ? strlen(ctx->export_desc) : 0;
len = htonl(name_len);

//TODO : many export in a loop
fixed_new_option_reply.magic = htonll(NBD_REP_MAGIC);
fixed_new_option_reply.option = htonl(new_opt.option);
fixed_new_option_reply.reply = htonl(NBD_REP_SERVER);
fixed_new_option_reply.replylen = htonl(name_len + sizeof(len) +
desc_len);

while (*ptr_ctx) {
name_len = strlen(ctx->export_name);
desc_len = (*ptr_ctx)->export_desc ? strlen((*ptr_ctx)->export_desc) : 0;
len = htonl(name_len);
fixed_new_option_reply.replylen = htonl(name_len + sizeof(len) +
desc_len);

size = send(client_socket, &fixed_new_option_reply,
sizeof(struct nbd_fixed_new_option_reply), MSG_MORE);
size = send(client_socket, &len, sizeof len, MSG_MORE);
size = send(client_socket, (*ptr_ctx)->export_name, name_len, MSG_MORE);
size = send(client_socket, (*ptr_ctx)->export_desc, desc_len, MSG_MORE);
ptr_ctx++;
}
fixed_new_option_reply.reply = htonl(NBD_REP_ACK);
size = send(client_socket, &fixed_new_option_reply,
sizeof(struct nbd_fixed_new_option_reply), MSG_MORE);
size = send(client_socket, &len, sizeof len, MSG_MORE);
size = send(client_socket, ctx->export_name, name_len, MSG_MORE);
size = send(client_socket, ctx->export_desc, desc_len, 0);
sizeof(struct nbd_fixed_new_option_reply), 0);
break;
//TODO
// break;
Expand Down Expand Up @@ -181,7 +186,7 @@ nbd_context *negotiation_phase(int client_socket, nbd_context **ctxs)
* @param client_socket
* @param ctx NBD callback struct
*/
int transmission_phase(int client_socket, nbd_context *ctx)
int transmission_phase(const int client_socket, const nbd_context *ctx)
{
register int r, size, error = -1, retry = NBD_MAX_RETRIES, sendflag = 0;
register uint32_t blkremains = 0, byteread = 0, bufbklsz = 0;
Expand Down
57 changes: 0 additions & 57 deletions modules/network/lwnbdsvr/lwNBD/nbd_protocol.h

This file was deleted.

6 changes: 5 additions & 1 deletion modules/network/lwnbdsvr/lwNBD/nbd_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#define LWIP_HDR_APPS_NBD_SERVER_H

#include "nbd-protocol.h"
#include "nbd_protocol.h"
#include "nbd_opts.h"

#include <stdint.h>
Expand Down Expand Up @@ -167,6 +166,11 @@ struct nbd_context_Vtbl
int nbd_recv(int s, void *mem, size_t len, int flags);
int nbd_init(nbd_context **ctx);

// in nbd_protocol.c
//todo: const ctxs
nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs);
int transmission_phase(const int client_socket, const nbd_context *ctx);

static inline int nbd_read(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length)
{
return (*me->vptr->read)(me, buffer, offset, length);
Expand Down

0 comments on commit e03af9b

Please sign in to comment.