Skip to content

Commit

Permalink
general improvement on doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Sep 26, 2023
1 parent deb073c commit a464c9f
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 58 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ banner:
@echo -e "library version $(APP_VERSION) - BSD 2-Clause License\n"

clean:
rm -f $(BIN) $(MANPAGE) $(OBJ) *~ core
rm -f $(BIN) $(MANPAGE) $(OBJ) *~ core
find -iname "*.o" -or -iname "*.a" -exec rm {} \;

nbdcleanup:
sudo lsof -t /dev/nbd* | sudo xargs -r kill -9
Expand Down
16 changes: 7 additions & 9 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
EXAMPLES = local-reader lwnbd-server
CFLAGS = -I../include
LDFLAGS := ../lwnbd.a
BIN = local-reader lwnbd-server
CFLAGS = -I../include $(CFLAGS-$@)
LDLIBS := ../lwnbd.a
LDFLAGS += $(LDFLAGS-$@)
APP_VERSION := $(shell git describe --always --tags)
CC_VERSION := "$(CC) $(shell $(CC) -dumpversion)"
CFLAGS += $(CFLAGS-$@)

all: $(EXAMPLES)
all: $(BIN)

local-reader: local-reader.o
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

CFLAGS-lwnbd-server.o += $(shell pkg-config --cflags libuv) \
-DAPP_VERSION=\"$(APP_VERSION)\" \
-DCC_VERSION=\"$(CC_VERSION)\" \
-DAPP_NAME=\"lwnbd-server\"

LDFLAGS-lwnbd-server += $(shell pkg-config --libs libuv)
lwnbd-server: lwnbd-server.o
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(shell pkg-config --libs libuv)

clean:
rm -f $(EXAMPLES) *.o
rm -f $(BIN) *.o
4 changes: 2 additions & 2 deletions examples/local-reader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* gcc -I../include local-read.c -o local-read ../lwnbd.a
* A demonstration of the methods currently available to access an export locally.
*/

#include <lwnbd.h>
Expand Down Expand Up @@ -34,7 +34,7 @@ int main(int argc, const char **argv)
ctx = lwnbd_get_context(argv[1]);

// while (r == 0) {
// r = plugin_pread(ctx, buf, 1, offset++, 0); /* we are limited to wrapped func in server.c */
// r = plugin_pread(ctx, buf, 1, offset++, 0); /* we are limited to wrapped func in server.h */
// printf("%c", *buf);
// fflush(stdout);
// usleep(1e4);
Expand Down
34 changes: 24 additions & 10 deletions examples/lwnbd-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@
*
* */

extern lwnbd_plugin_t *command_plugin_init(void);
extern lwnbd_plugin_t *memory_plugin_init(void);
extern lwnbd_plugin_t *file_plugin_init(void);
extern struct lwnbd_server *nbd_server_init(void);

// plugin_init plugins_table[] = {
// file_plugin_init,
// NULL
// };

extern struct lwnbd_server *nbd_server_init(void);

// void signal_callback_handler(int signum)
//{
// exit(signum);
// }
void coucou()
int greeter(int argc, char **argv, void *result, int64_t *size)
{
printf("exit\n");
printf("greeter !!\n");
}

int gEnableWrite = 1;
Expand Down Expand Up @@ -104,13 +105,13 @@ void on_new_connection(uv_stream_t *server, int status)
int main(int argc, const char **argv)
{
lwnbd_server_t nbdsrv;
lwnbd_plugin_h fileplg, memplg;
lwnbd_plugin_h fileplg, memplg, cmdplg;

int i = atexit(coucou);
if (i != 0) {
fprintf(stderr, "cannot set exit function\n");
exit(EXIT_FAILURE);
}
// int i = atexit(coucou);
// if (i != 0) {
// fprintf(stderr, "cannot set exit function\n");
// exit(EXIT_FAILURE);
// }

// if (argc < 2) {
// fprintf(stderr, "Usage: %s <files>\n", argv[0]);
Expand Down Expand Up @@ -143,7 +144,20 @@ int main(int argc, const char **argv)

fileplg = lwnbd_plugin_init(file_plugin_init);

/*
*
*/
struct lwnbd_command mycmd = {
.name = "greeter",
.cmd = greeter,
};
cmdplg = lwnbd_plugin_init(command_plugin_init);
lwnbd_plugin_new(cmdplg, &mycmd);
DEBUGLOG("-------------\n");


/* assuming no user input error */
/* todo : lwnbd_plugin_news() */
for (int i = 1; i < argc; i++) {
lwnbd_plugin_new(fileplg, argv[i]);
}
Expand Down
48 changes: 34 additions & 14 deletions examples/remoteshell.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
#!/usr/bin/python3

# Sample to demonstrate usage of URI with query as a way
# to send command remotely
#
# usage:
# export TARGET_IP=192.168.1.5
# python remoteshell.py motd%3fmemset=p
# ./remoteshell.py dumpmem offset=0x200 size=0x200
#
# On Alpine :
# echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing/" >> /etc/apk/repositories && apk update
# apk add libnbd@testing

#!/usr/bin/python3
import argparse
import nbd
import os
import sys
from urllib.parse import urlunsplit, urlencode, quote, parse_qs

# TODO read from cli first [-h hostname]
host = os.environ['TARGET_IP']
if not host:
print("no host")
p = argparse.ArgumentParser(description='remote command for lwNBD',
epilog='This software is part of lwNBD project https://github.com/bignaux/lwNBD')
p.add_argument('query', default='list', nargs='*')
p.add_argument('-t', '--target', default=os.environ['TARGET_IP'], help='hostname or IP of target')
p.add_argument('-e', '--exportname', default='shell', help='name of export')
p.add_argument('-v', '--verbose', action='store_true', help='increase output verbosity')
args = p.parse_args()

n = len(sys.argv)
if n > 1:
query = sys.argv[1]
if not args.target:
sys.exit("You should provide a target via export TARGET_IP or --target argument")

# would need encoding URI
### not very happy with that parsing
if args.verbose: print(args.query)
query = parse_qs('&'.join(args.query), keep_blank_values=True)
if args.verbose: print(query)
args.query = urlencode(query, doseq=True, quote_via=quote)
if args.verbose: print(args.query)
# query = args.command + '=' + ' '.join(args.arguments)
uri = urlunsplit(('nbd', args.target, args.exportname, args.query, ''))
if args.verbose: print('Clear URI:', uri)
encoded_url = quote(uri, safe=':/%=')
if args.verbose: print('Request:', encoded_url)

h = nbd.NBD()
uri = "nbd://" + host + '/' + query

h.connect_uri(uri)
ret = h.pread(512, 0)
h.connect_uri(encoded_url)
size = h.get_size()
if args.verbose:
print('Response:', size, 'bytes')
ret = h.pread(size, 0)
s = str(ret, 'utf-8')
print(s)
h.shutdown()
9 changes: 3 additions & 6 deletions include/lwnbd-plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ typedef struct
/* experimental */
struct lwnbd_command
{
char *name;
int (*cmd)(int argc, char **argv, void *result, int64_t *size);
int argc;
char **argv;
void *result; /* return of cmd() */
int64_t size; /* size of result */
};

/* A struct to hold the query string parameter values. */
struct query_param
struct query_t
{
char *key;
char *val;
Expand Down Expand Up @@ -78,7 +75,7 @@ typedef struct lwnbd_plugin_t

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 (*query)(void *handle, struct query_t *params,
int nb_params); /* experimental */

} lwnbd_plugin_t;
Expand Down
2 changes: 2 additions & 0 deletions plugins/command/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OBJ += plugins/command/command.o
PLUGIN_COMMAND = 1
77 changes: 69 additions & 8 deletions plugins/command/command.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
#include <lwnbd-plugin.h>
#include <string.h>
#include "config.h"

#define PLUGIN_NAME command
#define PLUGIN_NAME command
#define CMD_DRIVER_MAX 100

/*
* Here we don't want an export by command,
* but one export to be reachable by lwnbd_get_context()
*/

static struct lwnbd_command commands[CMD_DRIVER_MAX];
static int commands_cnt = 0;

static inline int command_pread(void *handle, void *buf, uint32_t count,
uint64_t offset, uint32_t flags)
{
struct lwnbd_command *h = handle;
h->cmd(h->argc, h->argc, h->result, &h->size);
memcpy(buf, h->result, h->size);
// struct lwnbd_command *h = handle;
// h->cmd(h->argc, h->argc, h->result, &h->size);
// memcpy(buf, h->result, h->size);
return 0;
}

static inline int command_pwrite(void *handle, const void *buf, uint32_t count,
uint64_t offset, uint32_t flags)
{
// struct memory_config *h = handle;
// intptr_t addr = h->base + offset;
// memcpy(&addr, buf, count);
return 0;
}

static int64_t command_get_size(void *handle)
{
struct lwnbd_command *h = handle;
return h->size;
// struct lwnbd_command *h = handle;
// return h->size;
return 0;
}

static int command_block_size(void *handle,
Expand All @@ -24,12 +45,52 @@ static int command_block_size(void *handle,
return 0;
}

/*
* ?name=argv
*/
static int command_query(void *handle, struct query_t *params, int nb_params)
{
int cnt = 0;
// get_commands();
while (cnt < commands_cnt) {
if (0 == strcmp(params[0].key, commands[cnt].name)) {
void *result;
int64_t size;
commands[cnt].cmd(1, params[0].val, result, &size);
break;
}
cnt++;
}
return 0;
}

/*
* look like register RPC or callback
*/
static int command_ctor(const void *pconfig, lwnbd_export_t *e)
{
struct lwnbd_command *h;

if (commands_cnt == CMD_DRIVER_MAX)
return -1;

DEBUGLOG("coucou\n");
h = &commands[commands_cnt++];
memcpy(h, pconfig, sizeof(struct lwnbd_command));
e->handle = NULL;
return 0;
}

static lwnbd_plugin_t plugin = {
.name = "command",
.version = PACKAGE_VERSION,
.name = "shell",
.longname = "lwnbd generic command plugin",
.version = "0",
.pread = command_pread,
.pwrite = command_pwrite,
.get_size = command_get_size,
.block_size = command_block_size,
.query = command_query,
.ctor = command_ctor,
};

NBDKIT_REGISTER_PLUGIN(plugin)
8 changes: 7 additions & 1 deletion plugins/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static int memory_block_size(void *handle,
return 0;
}

static int memory_query(void *handle, struct query_param *params, int nb_params)
static int memory_query(void *handle, struct query_t *params, int nb_params)
{
struct memory_config *h = handle;
while (nb_params-- > 0) {
Expand All @@ -84,6 +84,12 @@ static int memory_query(void *handle, struct query_param *params, int nb_params)
LOG("val = %s\n", params[nb_params].val);
memset((char *)h->base, params[nb_params].val[0], h->size);
}
} else if (0 == strcmp(params[nb_params].key, "memcpy")) {
if (params[nb_params].val != NULL) {
LOG("val = %s\n", params[nb_params].val);
bzero((char *)h->base, h->size);
memcpy((char *)h->base, params[nb_params].val, strlen(params[nb_params].val));
}
}
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ports/playstation2/lwnbd-playstation2-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ BDM is another tentative to make a common interface for PS2 block device, acting

## idea, c'mon pick one (or more!)

Open-PS2-Loader/modules/network/httpclient to plugin content

make oneliner like nbdcopy nbd://.../cmd/poweroff available like a bus , see @vates/nbd-client node stuff for bundle apps on phone ?
mem/spu2 ... mc/1/filename.
close Remotely Launch games https://github.com/ps2homebrew/Open-PS2-Loader/issues/418
Expand Down
1 change: 1 addition & 0 deletions ports/unix.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CFLAGS += -DAPP_NAME=\"lwNBD\" -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L
include servers/nbd/Makefile
include plugins/file/Makefile
include plugins/memory/Makefile
include plugins/command/Makefile

%.a: $(OBJ)
ar r $@ $^
Expand Down
5 changes: 3 additions & 2 deletions servers/sifrpc/lwnbd-sifrpc-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ EE->>IOP: SifCallRPC()
IOP->>IOP: Process
IOP-->>EE
```
Currently, it only contains essential RPC to control the lwnbd irx from EE. The strategy here is to gradually build up the glue as you need it.

The final purpose of this plugin is to provide a way to speak transparently between EE and IOP without using SIF wrappers, but using lwnbd_plugin_t interface and other lwnbd mecanism. The advantage of such a mechanism is that it avoids the systematic addition of an SIF wrapper for any IOP-side library. These wrappers are often not synchronised with the IOP, are of variable quality and tend to develop functionalities that go beyond their role. This plugin avoids having to redo drivers on the EE side to switch the TCP server to the EE, which is more efficient in terms of transfer speed, and allows you to benefit from the power of the EE to manage compression, as in the case of TLS support. So it is indeed a wrapper for lwnbd, but it's kind of the final wrapper.

# The Library Hierarchy

Expand All @@ -21,8 +24,6 @@ The libraries that use the SIF to transfer data are arranged in a hierarchy :
* DMA
* [DMAC]

I'd like to create a way to speak transparently from EE to IOP without using SIF wrappers, using lwnbd_plugin_t interface and other lwnbd mecanism.

# TODO

* asyncronous using sceSifCheckStatRpc() SifGetNextRequest / SifExecRequest
Expand Down
4 changes: 3 additions & 1 deletion src/contexts.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ lwnbd_context_t *lwnbd_get_context_i(size_t i)
return &contexts[i];
}

/* search for context by name
/*
* like open() syscall
* search for context by name
* return NULL if not found any.
*/
lwnbd_context_t *lwnbd_get_context(const char *contextname)
Expand Down
Loading

0 comments on commit a464c9f

Please sign in to comment.