Skip to content

Commit

Permalink
lwnbd: move IOP specific code in platform-ps2.h
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Sep 14, 2021
1 parent 3844868 commit a23f293
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 171 deletions.
2 changes: 1 addition & 1 deletion modules/network/lwnbdsvr/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
IOP_BIN = lwnbdsvr.irx
IOP_OBJS = lwnbdsvr.o imports.o exports.o lwnbd.o nbd_protocol.o drivers/atad_d.o drivers/ioman_d.o
IOP_INCS += -I. -I../../iopcore/common -I../../../include/
IOP_INCS += -I. -I../../iopcore/common -I../../../include/ -include platform-ps2.h -DAPP_NAME=\"lwnbdsvr\"

ifeq ($(DEBUG),1)
IOP_CFLAGS += -DDEBUG -DNBD_DEBUG
Expand Down
37 changes: 19 additions & 18 deletions modules/network/lwnbdsvr/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# lwNBD

* Description : A Lightweight NBD server
* Official repository : <https://github.com/bignaux/lwNBD>
* Author : Ronan Bignaux
* Licence : BSD
* Socket API: lwIP 2.0.0 and Linux supported.
* Description : A Lightweight NBD server
* Official repository : <https://github.com/bignaux/lwNBD>
* Author : Ronan Bignaux
* Licence : BSD
* Socket API: lwIP 2.0.0 and Linux supported.

Targeting first the use on Playstation 2 IOP, a 37.5 MHz MIPS processor
and 2 MB of RAM, lwNBD is designed to run on bare metal or OS embedded system.
With modulararity and portability in mind, it is developed according to several
code standards, including :

* [Object-Oriented Programming in C](https://github.com/QuantumLeaps/OOP-in-C/)
* nbdkit implementation naming, using a verbatim copy of their nbd-protocol.h
* [Object-Oriented Programming in C](https://github.com/QuantumLeaps/OOP-in-C/)
* nbdkit implementation naming, using a verbatim copy of their nbd-protocol.h

There are 2 examples provided :

* lwnbd_linux.c is a simple nbd server, that use stdio_d.c driver to
serve a list of files as command line parameters.
* lwnbdsvr.c an IRX module for [Open-PS2-Loader](https://github.com/ps2homebrew/Open-PS2-Loader).
It can export hdd drive.
* lwnbd_linux.c is a simple nbd server, that use stdio_d.c driver to
serve a list of files as command line parameters.

* lwnbdsvr.c an IRX module for [Open-PS2-Loader](https://github.com/ps2homebrew/Open-PS2-Loader).
It can export hdd drive.

## History

Expand All @@ -42,13 +43,13 @@ i publish this "AS-IS".

Known supported clients :

* nbdfuse (provided by libnbd), works on windows with WSL2.
* nbd-client
* Ceph for Windows (wnbd-client.exe)
* nbdfuse (provided by libnbd), works on windows with WSL2.
* nbd-client
* Ceph for Windows (wnbd-client.exe)

## TODO

* event loop based on select()
* provide a clean API
* fix NBD_FLAG_FIXED_NEWSTYLE negotiation
* NBD_OPT_INFO/NBD_OPT_GO, the server is not yet able to serve many export or change blocksize.
* event loop based on select()
* provide a clean API
* fix NBD_FLAG_FIXED_NEWSTYLE negotiation
* NBD_OPT_INFO/NBD_OPT_GO
9 changes: 4 additions & 5 deletions modules/network/lwnbdsvr/lwnbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ nbd_context *nbd_context_getDefaultExportByName(nbd_context **nbd_contexts, cons
*/
uint32_t nbd_recv(int s, void *mem, size_t len, int flags)
{
uint32_t bytesRead = 0;
ssize_t bytesRead;
uint32_t left = len;
uint32_t totalRead = 0;

Expand All @@ -114,7 +114,7 @@ uint32_t nbd_recv(int s, void *mem, size_t len, int flags)
do {
bytesRead = recv(s, mem + totalRead, left, flags);
// dbgLOG("bytesRead = %u\n", bytesRead);
if (bytesRead <= 0)
if (bytesRead <= 0) // if (bytesRead == -1) failed for nbdfuse, seems it not send NBD_CMD_DISC
break;

left -= bytesRead;
Expand Down Expand Up @@ -164,11 +164,11 @@ int nbd_init(nbd_context **ctx)

LOG("a client connected.\n");
r = negotiation_phase(client_socket, ctx, &nego_ctx);
if (r == 0) {
if (r == NBD_OPT_EXPORT_NAME) {
r = transmission_phase(client_socket, nego_ctx);
if (r == -1)
LOG("an error occured during transmission phase.\n");
} else if (r == -2) {
} else if (r == -1) {
LOG("an error occured during negotiation_phase phase.\n");
}
close(client_socket);
Expand All @@ -178,6 +178,5 @@ int nbd_init(nbd_context **ctx)
error:
LOG("failed to init server.");
close(tcp_socket);

return 0;
}
116 changes: 24 additions & 92 deletions modules/network/lwnbdsvr/lwnbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@
#ifndef LWIP_HDR_APPS_NBD_SERVER_H
#define LWIP_HDR_APPS_NBD_SERVER_H

#include <stdio.h>
#include "nbd-protocol.h"
#include "nbd_opts.h"

#include <stdint.h>
#include <stdio.h>

#define LOG(format, args...) \
printf(APP_NAME ": " format, ##args)

Expand All @@ -58,68 +56,6 @@
} while (0)
#endif

//#include "lwip/apps/nbd_opts.h"
//#include "lwip/err.h"
//#include "lwip/pbuf.h"
//#include "lwip/mem.h"

#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <endian.h>
#include <unistd.h>

#include <assert.h> //todo: move in .c

typedef signed char err_t;
//TODO : manage endianess
#define htonll(x) htobe64(x)
#define ntohll(x) be64toh(x)
#endif

#ifdef _IOP
#include <ps2ip.h>
#include <sysclib.h>

#define assert(expr) \
((expr) || \
dbgprintf(F_NUM, __LINE__))

//#include <errno.h>
//#include <malloc.h>

//why not provide lwip/sockets.h ?
// #define send(a, b, c, d) lwip_send(a, b, c, d)
#define close(x) lwip_close(x)

//TODO: Missing <byteswap.h> in PS2SDK
// pickup from https://gist.github.com/jtbr/7a43e6281e6cca353b33ee501421860c
static inline uint64_t bswap64(uint64_t x)
{
return (((x & 0xff00000000000000ull) >> 56) | ((x & 0x00ff000000000000ull) >> 40) | ((x & 0x0000ff0000000000ull) >> 24) | ((x & 0x000000ff00000000ull) >> 8) | ((x & 0x00000000ff000000ull) << 8) | ((x & 0x0000000000ff0000ull) << 24) | ((x & 0x000000000000ff00ull) << 40) | ((x & 0x00000000000000ffull) << 56));
}

//TODO: Missing in PS2SK's "common/include/tcpip.h"
#if __BIG_ENDIAN__
#define htonll(x) (x)
#define ntohll(x) (x)
#else
#define htonll(x) bswap64(x)
#define ntohll(x) bswap64(x)
#endif

// Missing in stddef.h
#ifndef offsetof
#define offsetof(st, m) \
((size_t)((char *)&((st *)0)->m - (char *)0))
#endif

//TODO: Missing in PS2SK's <stdint.h> , needed for "nbd-protocol.h"
// https://en.cppreference.com/w/c/types/integer
#define UINT64_MAX 0xffffffffffffffff
#define UINT64_C(x) ((x) + (UINT64_MAX - UINT64_MAX))
#endif /* _IOP */

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -134,55 +70,51 @@ struct lwnbd_operations;
typedef struct nbd_context
{
struct lwnbd_operations const *vptr;

char export_desc[64];
char export_name[32];
uint64_t export_size; /* size of export in byte */
uint16_t eflags; /* per-export flags */
uint16_t blocksize; /* in power of 2 for bit shifting - log2(blocksize) */
uint16_t blocksize;
uint8_t *buffer;

} nbd_context;

struct lwnbd_operations
{

/**
* Close block device handle
* @param handle File handle returned by open()
*/
* Close block device handle
* @param handle File handle returned by open()
*/
// void (*close)(nbd_context *me);
/**
* Read from block device
* @param
* @param buffer Target buffer to copy read data to
* @param offset Offset in block to copy read data to
* @param length Number of blocks to copy to buffer
* @returns &gt;= 0: Success; &lt; 0: Error
*/
* Read from block device
* @param
* @param buffer Target buffer to copy read data to
* @param offset Offset in block to copy read data to
* @param length Number of blocks to copy to buffer
* @returns &gt;= 0: Success; &lt; 0: Error
*/
int (*read)(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length);
/**
* Write to block device
* @param me ()
* @param buffer Target buffer to copy write data to
* @param offset Offset in block to copy write data to
* @param length Number of blocks to copy to buffer
* @returns &gt;= 0: Success; &lt; 0: Error
*/
* Write to block device
* @param me ()
* @param buffer Target buffer to copy write data to
* @param offset Offset in block to copy write data to
* @param length Number of blocks to copy to buffer
* @returns &gt;= 0: Success; &lt; 0: Error
*/
int (*write)(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length);
/**
* Flush to block device
* @param me ()
* @returns &gt;= 0: Success; &lt; 0: Error
*/
* Flush to block device
* @param me ()
* @returns &gt;= 0: Success; &lt; 0: Error
*/
int (*flush)(nbd_context const *const me);
};

uint32_t nbd_recv(int s, void *mem, size_t len, int flags);
int nbd_init(nbd_context **ctx);


// in nbd_protocol.c
//********************* nbd_protocol.c *********************
//todo: const ctxs
err_t negotiation_phase(const int client_socket, nbd_context **ctxs, nbd_context **ctx);
err_t transmission_phase(const int client_socket, nbd_context *ctx);
Expand Down
2 changes: 0 additions & 2 deletions modules/network/lwnbdsvr/lwnbdsvr.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#define APP_NAME "lwnbdsvr"

#include "irx_imports.h"
#include <lwnbd.h>
#include "drivers/atad_d.h"
Expand Down
17 changes: 0 additions & 17 deletions modules/network/lwnbdsvr/nbd_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,12 @@
#ifndef LWIP_HDR_APPS_NBD_OPTS_H
#define LWIP_HDR_APPS_NBD_OPTS_H

#ifdef __linux__
#include <sys/socket.h>
#endif

// #ifndef _IOP
// #include "lwip/opt.h"
// #else
// #include "tcpip.h"
// #endif

/**
* @defgroup NBD_opts Options
* @ingroup NBD
* @{
*/

/**
*
*/
#if !defined APP_NAME || defined __DOXYGEN__
#define APP_NAME "lwNBD"
#endif

/**
* Enable NBD debug messages
*/
Expand Down

0 comments on commit a23f293

Please sign in to comment.