Skip to content

Commit

Permalink
better nbd_context handling and debug
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Aug 31, 2021
1 parent 53751dd commit a0e9aa8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion modules/network/lwnbdsvr/drivers/atad.c
Expand Up @@ -29,7 +29,7 @@ int atad_ctor(atad_driver *const me, int device)
me->super.vptr = &vtbl; /* override the vptr */
// int ata_device_sce_identify_drive(int device, void *data);
strcpy(me->super.export_desc, "PlayStation 2 HDD via ATAD");
strcpy(me->super.export_name, "hdd0");
sprintf(me->super.export_name, "%s%d", "hdd", me->device);
me->super.blocksize = 512;
me->super.buffer = nbd_buffer;
me->super.eflags = NBD_FLAG_HAS_FLAGS;
Expand Down
1 change: 1 addition & 0 deletions modules/network/lwnbdsvr/imports.lst
Expand Up @@ -25,6 +25,7 @@ I_DeleteThread
thbase_IMPORTS_end

sysclib_IMPORTS_start
I_sprintf
I_strcpy
I_strlen
I_memset
Expand Down
39 changes: 33 additions & 6 deletions modules/network/lwnbdsvr/lwNBD/nbd_protocol.c
Expand Up @@ -58,7 +58,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs)
nbd_context **ptr_ctx = ctxs;

//temporary workaround
nbd_context *ctx = ctxs[0];
nbd_context const *ctx = ctxs[0];

/*** handshake ***/

Expand Down Expand Up @@ -99,7 +99,21 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs)
nbd_buffer[new_opt.optlen] = '\0';
}

printf("%d\n", new_opt.option);
#ifdef DEBUG
static const char *NBD_OPTIONS[] = {
NULL,
"NBD_OPT_EXPORT_NAME",
"NBD_OPT_ABORT",
"NBD_OPT_LIST",
"NBD_OPT_STARTTLS",
"NBD_OPT_INFO",
"NBD_OPT_GO",
"NBD_OPT_STRUCTURED_REPLY",
"NBD_OPT_LIST_META_CONTEXT",
"NBD_OPT_SET_META_CONTEXT",
};
printf("lwNBD: %s\n", NBD_OPTIONS[new_opt.option]);
#endif

switch (new_opt.option) {

Expand Down Expand Up @@ -144,6 +158,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs)
ptr_ctx++;
}
fixed_new_option_reply.reply = htonl(NBD_REP_ACK);
fixed_new_option_reply.replylen = 0;
size = send(client_socket, &fixed_new_option_reply,
sizeof(struct nbd_fixed_new_option_reply), 0);
break;
Expand Down Expand Up @@ -175,7 +190,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs)
}

abort:
return ctx;
return (nbd_context *)ctx;
soft_disconnect:
error:
return NULL;
Expand Down Expand Up @@ -203,15 +218,15 @@ int transmission_phase(const int client_socket, const nbd_context *ctx)
// TODO : blocking here if no proper NBD_CMD_DISC, bad threading design ?
size = nbd_recv(client_socket, &request, sizeof(struct nbd_request), 0);
if (size < sizeof(struct nbd_request)) {
printf("lwNBD : sizeof NOK\n");
printf("lwNBD: sizeof NOK\n");
goto error;
}

// printf("lwNBD : sizeof OK.\n");

request.magic = ntohl(request.magic);
if (request.magic != NBD_REQUEST_MAGIC) {
printf("lwNBD : wrong NBD_REQUEST_MAGIC\n");
printf("lwNBD: wrong NBD_REQUEST_MAGIC\n");
goto error;
}

Expand All @@ -222,7 +237,19 @@ int transmission_phase(const int client_socket, const nbd_context *ctx)

reply.handle = request.handle;

// printf("lwNBD: entering NBD_CMD %d.\n", request.type);
#ifdef DEBUG
static const char *NBD_CMD[] = {
"NBD_CMD_READ",
"NBD_CMD_WRITE",
"NBD_CMD_DISC",
"NBD_CMD_FLUSH",
"NBD_CMD_TRIM",
"NBD_CMD_CACHE",
"NBD_CMD_WRITE_ZEROES",
"NBD_CMD_BLOCK_STATUS",
};
printf("lwNBD: %s\n", NBD_CMD[request.type]);
#endif

switch (request.type) {

Expand Down
16 changes: 8 additions & 8 deletions modules/network/lwnbdsvr/lwNBD/nbd_server.h
Expand Up @@ -52,6 +52,14 @@
//#include "lwip/pbuf.h"
//#include "lwip/mem.h"

#ifdef DEBUG
#define dbgprintf(args...) printf(args)
#else
#define dbgprintf(args...) \
do { \
} while (0)
#endif

#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
Expand All @@ -73,14 +81,6 @@
// #define send(a, b, c, d) lwip_send(a, b, c, d)
#define close(x) lwip_close(x)

#ifdef DEBUG
#define dbgprintf(args...) printf(args)
#else
#define dbgprintf(args...) \
do { \
} while (0)
#endif

//TODO: Missing <byteswap.h> in PS2SDK
// pickup from https://gist.github.com/jtbr/7a43e6281e6cca353b33ee501421860c
static inline uint64_t bswap64(uint64_t x)
Expand Down
18 changes: 10 additions & 8 deletions modules/network/lwnbdsvr/lwnbdsvr.c
Expand Up @@ -9,20 +9,22 @@ static int nbd_tid;
extern struct irx_export_table _exp_lwnbdsvr;

//need to be global to be accessible from thread
atad_driver hdd;
nbd_context *nbd_contexts[] = {
&hdd.super,
NULL,
};
atad_driver hdd[2]; // could have 2 ATA disks
nbd_context *nbd_contexts[10];

int _start(int argc, char **argv)
{
iop_thread_t nbd_thread;
int ret, successed_exported_ctx = 0;

ret = atad_ctor(&hdd, 0);
if (ret == 0)
successed_exported_ctx = 1;
for (int i = 0; i < 2; i++) {
ret = atad_ctor(&hdd[i], i);
if (ret == 0) {
nbd_contexts[successed_exported_ctx] = &hdd[i].super;
successed_exported_ctx++;
}
}
nbd_contexts[successed_exported_ctx] = NULL;

if (!successed_exported_ctx) {
printf("lwnbdsvr: nothing to export.\n");
Expand Down

0 comments on commit a0e9aa8

Please sign in to comment.