Skip to content

Commit

Permalink
new experimental URI dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Sep 20, 2023
1 parent 5cf6d0d commit 30c1c32
Show file tree
Hide file tree
Showing 37 changed files with 707 additions and 167 deletions.
4 changes: 3 additions & 1 deletion .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
./include/nbd-protocol.h
./include/nbd-protocol.h
./src/yuarel.c
./src/yuarel.h
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#TARGET=iop
LWNBD_DEBUG=1
NBD_URI=1
#TARGET_IP=192.168.1.10
WORKSPACE="../workspace/"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CFLAGS = -Iinclude -std=c99 -Wall -Wfatal-errors
DEBUG ?= 0

APP_VERSION := $(shell git describe --always --tags)
CC_VERSION := "$(CC) $(shell $(CC) -dumpversion)"
TARGET ?= unix
RONN = ronn
MANPAGE = lwnbd.3
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ code standards, including :
* static-linking
* written in C99 (using -std=c99 compile-time flag), use standard library usage
* thread-safe synchronous NBD protocol implementation
* optional and experimental query support

The lwNBD API is broken down into 3:

Expand All @@ -34,10 +35,10 @@ There are 2 targets supported :

* GNU/Linux that use *file* plugin to
serve a list of files as command line parameters. For the time being, the main
purpose of the support is to facilitate development.
purpose of the support is to facilitate development. It uses libuv as event loop, and can serve multiple client.

* Playstation 2 IOP via an IRX module for [Open-PS2-Loader](https://github.com/ps2homebrew/Open-PS2-Loader).
It can export hdd drive (*atad* plugin), MemoryCard (*mcman* plugin), rom0 and IOP ram (*memory* plugin). PS2SDK use lwip [v2.0.3](https://github.com/ps2dev/lwip/tree/ps2-v2.0.3).
It can export hdd drive (*atad* plugin), MemoryCard (*mcman* plugin), rom0 and IOP ram (*memory* plugin). PS2SDK use lwip [v2.0.3](https://github.com/ps2dev/lwip/tree/ps2-v2.0.3). Read more about this on [Playstation 2 port](./ports/playstation2/lwnbd-playstation2-port.md)

## HISTORY

Expand Down
27 changes: 19 additions & 8 deletions include/lwnbd-context.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef LWNBD_CONTEXT_H
#define LWNBD_CONTEXT_H

#include "config.h"

#include <lwnbd.h>
#include <lwnbd-plugin.h>
//#include <stdlib.h>
Expand All @@ -16,27 +18,36 @@ typedef enum {
// CONTEXT_INUSE,
} context_state_t;

struct lwnbd_context
typedef struct
{
void *handle; /* Plugin handle. */
struct lwnbd_plugin *p; /* Plugin that provided handle. */
void *handle; /* Plugin handle. */
lwnbd_plugin_t *p; /* Plugin that provided handle. */
char name[32];
char description[64];
int64_t exportsize;
uint16_t eflags;
uint32_t minimum_block_size;
uint32_t preferred_block_size;
uint32_t maximum_block_size;
};
} lwnbd_context_t;

/* contexts.c */
size_t lwnbd_contexts_count();
int lwnbd_add_context(struct lwnbd_plugin *p, struct lwnbd_export *e);
struct lwnbd_context *lwnbd_get_context(const char *contextname);
struct lwnbd_context *lwnbd_get_context_uri(const char *uri, struct lwnbd_context *ctx);
struct lwnbd_context *lwnbd_get_context_i(size_t i);
int lwnbd_add_context(lwnbd_plugin_t *p, lwnbd_export_t *e);
lwnbd_context_t *lwnbd_get_context(const char *contextname);

lwnbd_context_t *lwnbd_get_context_i(size_t i);
void lwnbd_dump_contexts();

#ifndef NBD_URI
lwnbd_context_t *lwnbd_get_context_uri(const char *uri);
#else
static inline lwnbd_context_t *lwnbd_get_context_uri(const char *uri)
{
return lwnbd_get_context(uri);
};
#endif

#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 25 additions & 6 deletions include/lwnbd-plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
extern "C" {
#endif

struct lwnbd_export
typedef struct
{
char name[32];
char description[64]; /* optional */

/* lwnbd specific */
void *handle; /* Plugin handle. */
};
} lwnbd_export_t;

/* experimental */
struct lwnbd_command
Expand All @@ -28,7 +28,14 @@ struct lwnbd_command
int64_t size; /* size of result */
};

struct lwnbd_plugin
/* A struct to hold the query string parameter values. */
struct query_param
{
char *key;
char *val;
};

typedef struct
{
/* private */
uint64_t _struct_size;
Expand Down Expand Up @@ -64,20 +71,32 @@ struct lwnbd_plugin

/* lwnbd specific after nbdkit compat */

int (*ctor)(const void *pconfig, struct lwnbd_export *e); /* create new export from custom config */
int (*ctor)(const void *pconfig, lwnbd_export_t *e); /* create new export from custom config */
int (*ctrl)(void *handle, char *cmd, struct lwnbd_command *c); /* experimental */
};
int (*query)(void *handle, struct query_param *params,
int nb_params); /* experimental */

} lwnbd_plugin_t;

#define M1(x) x##_##plugin_init
#define FUNCINIT(x) M1(x)
#define NBDKIT_REGISTER_PLUGIN(plugin) \
struct lwnbd_plugin * \
lwnbd_plugin_t * \
FUNCINIT(PLUGIN_NAME)(void) \
{ \
(plugin)._struct_size = sizeof(plugin); \
return &(plugin); \
}

static inline int64_t stream_get_size(void *handle)
{
/**
* since NBD doesn't support stream, we need to lie about size
* to have enough for our session
*/
return 0x7FFFFFFFFFFFE00; // %512 = 0 for nbd-client compat
}

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 6 additions & 6 deletions include/lwnbd-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@

#include <stdint.h>
#include <lwnbd-context.h>
// extern struct lwnbd_context contexts;
// extern lwnbd_context_t contexts;
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

static inline int plugin_pread(struct lwnbd_context const *const me, void *buf, uint32_t count, uint64_t offset,
static inline int plugin_pread(lwnbd_context_t const *const me, void *buf, uint32_t count, uint64_t offset,
uint32_t flags)
{
return (*me->p->pread)(me->handle, buf, count, offset, flags);
}

static inline int plugin_pwrite(struct lwnbd_context const *const me, const void *buf, uint32_t count,
static inline int plugin_pwrite(lwnbd_context_t const *const me, const void *buf, uint32_t count,
uint64_t offset, uint32_t flags)
{
return (*me->p->pwrite)(me->handle, buf, count, offset, flags);
}

static inline int plugin_flush(struct lwnbd_context const *const me, uint32_t flags)
static inline int plugin_flush(lwnbd_context_t const *const me, uint32_t flags)
{
return (*me->p->flush)(me->handle, flags);
}

static inline int plugin_trim(struct lwnbd_context const *const me, uint32_t count, uint64_t offset, uint32_t flags)
static inline int plugin_trim(lwnbd_context_t const *const me, uint32_t count, uint64_t offset, uint32_t flags)
{
return (*me->p->trim)(me->handle, count, offset, flags);
}

static inline int plugin_zero(struct lwnbd_context const *const me, uint32_t count, uint64_t offset, uint32_t flags)
static inline int plugin_zero(lwnbd_context_t const *const me, uint32_t count, uint64_t offset, uint32_t flags)
{
return (*me->p->zero)(me->handle, count, offset, flags);
}
Expand Down
13 changes: 8 additions & 5 deletions include/lwnbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ extern "C" {

/* content plugins */

typedef uint32_t lwnbd_plugin_t;
typedef struct lwnbd_plugin *(*plugin_init)(void);
int lwnbd_plugin_config(lwnbd_plugin_t const plugin, const char *key, const char *value);
int lwnbd_plugin_new(lwnbd_plugin_t const plugin, const void *pconfig);
lwnbd_plugin_t lwnbd_plugin_init(plugin_init init);

#include "lwnbd-plugin.h" // temporary, fix with some lwnbd_handler_t stuff ...

typedef uint32_t lwnbd_plugin_h;
typedef lwnbd_plugin_t *(*plugin_init)(void);
int lwnbd_plugin_config(lwnbd_plugin_h const plugin, const char *key, const char *value);
int lwnbd_plugin_new(lwnbd_plugin_h const plugin, const void *pconfig);
lwnbd_plugin_h lwnbd_plugin_init(plugin_init init);

/* server plugins */

Expand Down
4 changes: 2 additions & 2 deletions plugins/atad/atad_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static inline int atad_flush(void *handle, uint32_t flags)
return ata_device_flush_cache(h->device);
}

static int atad_ctor(const void *pconfig, struct lwnbd_export *e)
static int atad_ctor(const void *pconfig, lwnbd_export_t *e)
{
int device = *(int *)pconfig;
ata_devinfo_t *dev_info = ata_get_devinfo(device);
Expand Down Expand Up @@ -99,7 +99,7 @@ static int atad_block_size(void *handle,
return 0;
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "atad",
.longname = "PlayStation 2 HDD via ATAD",
.version = PACKAGE_VERSION,
Expand Down
4 changes: 2 additions & 2 deletions plugins/bdm/bdm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static inline int bdm_flush(void *handle, uint32_t flags)
return 0;
}

static int bdm_ctor(const void *pconfig, struct lwnbd_export *e)
static int bdm_ctor(const void *pconfig, lwnbd_export_t *e)
{
struct block_device **pbd;
struct handle *h = &handles[0];
Expand Down Expand Up @@ -77,7 +77,7 @@ static int64_t bdm_get_size(void *handle)
return (int64_t)d->sectorSize * d->sectorCount;
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "bdm",
.longname = "PlayStation 2 BDM plugin",
.version = PACKAGE_VERSION,
Expand Down
2 changes: 1 addition & 1 deletion plugins/command/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static int command_block_size(void *handle,
return 0;
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "command",
.version = PACKAGE_VERSION,
.pread = command_pread,
Expand Down
4 changes: 2 additions & 2 deletions plugins/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static void file_open(void *handle, int readonly)
// return 0;
}

static int file_ctor(const void *pconfig, struct lwnbd_export *e)
static int file_ctor(const void *pconfig, lwnbd_export_t *e)
{
struct handle *h;
char *bname;
Expand Down Expand Up @@ -158,7 +158,7 @@ static int file_block_size(void *handle,
return 0;
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "file",
.longname =
"lwnbd file plugin",
Expand Down
4 changes: 2 additions & 2 deletions plugins/libhdd/hdd_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define PLUGIN_NAME libhdd
#define MAX_DEVICES 2
static unsigned char IOBuffer[1024] __attribute__((aligned(64)));
static struct lwnbd_plugin plugin;
static lwnbd_plugin_t plugin;

struct handle
{
Expand Down Expand Up @@ -80,7 +80,7 @@ void hdd_load(void)
}
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "libhdd",
.longname = "PlayStation 2 HDD via libhdd",
.version = PACKAGE_VERSION,
Expand Down
4 changes: 2 additions & 2 deletions plugins/mcman/mcman_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static int mcman_flush(void *handle, uint32_t flags)
return 0;
}

static int mcman_ctor(const void *pconfig, struct lwnbd_export *e)
static int mcman_ctor(const void *pconfig, lwnbd_export_t *e)
{
uint8_t device = *(uint8_t *)pconfig;

Expand Down Expand Up @@ -249,7 +249,7 @@ static int mcman_block_size(void *handle,
return 0;
}

static struct lwnbd_plugin plugin = {
static lwnbd_plugin_t plugin = {
.name = "mcman",
.longname = "PlayStation 2 MemoryCard via MCMAN",
.version = PACKAGE_VERSION,
Expand Down
29 changes: 27 additions & 2 deletions plugins/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static inline int memory_flush(void *handle, uint32_t flags)
return 0;
}

static int memory_ctor(const void *pconfig, struct lwnbd_export *e)
static int memory_ctor(const void *pconfig, lwnbd_export_t *e)
{
uint32_t i;
struct memory_config *h;
Expand Down Expand Up @@ -73,7 +73,31 @@ static int memory_block_size(void *handle,
return 0;
}

static struct lwnbd_plugin plugin = {
#include <ctype.h>
static char *strupr(char s[])
{
char *p;

for (p = s; *p; ++p)
*p = toupper(*p);

return (s);
}

static int memory_query(void *handle, struct query_param *params, int nb_params)
{
struct memory_config *h = handle;
while (nb_params-- > 0) {
LOG("%s\n", params[nb_params].key);
if (0 == strcmp(params[nb_params].key, "strupr")) {
LOG("strupr \n");
strupr((char *)h->base);
}
}
return 0;
}

static lwnbd_plugin_t plugin = {
.name = "memory",
.longname = "lwnbd generic memory plugin",
.version = PACKAGE_VERSION,
Expand All @@ -83,6 +107,7 @@ static struct lwnbd_plugin plugin = {
.flush = memory_flush,
.get_size = memory_get_size,
.block_size = memory_block_size,
.query = memory_query,
};

NBDKIT_REGISTER_PLUGIN(plugin)
2 changes: 2 additions & 0 deletions plugins/pcmstream/lwnbd-pcmstream-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ generally, a RTP (or icecast and so) is used. Let's try NBD as alternative.

### need more explanation ...

ffmpeg/ffplay

### Pulseaudio

1/ A solution can be create a sink with [module-null-sink](https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Modules/#module-null-sink), then relay it
Expand Down
Loading

0 comments on commit 30c1c32

Please sign in to comment.