Skip to content

Commit

Permalink
examples/lwnbd-server.c : fix conccurency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Sep 30, 2023
1 parent 7e6eec8 commit 4ef3ae9
Show file tree
Hide file tree
Showing 34 changed files with 262 additions and 197 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ LWNBD_DEBUG=1
NBD_URI=1
TARGET_IP=192.168.1.10
MAKE_SILENT=1
#MAKE_FATAL=1
WORKSPACE="../workspace/"
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
CC ?= gcc
# TODO: can't use -pedantic-errors yet due to LOG macros
#
CFLAGS = -Iinclude -std=c99 -Wall -Wfatal-errors
CFLAGS = -Iinclude -std=c99 -Wall
DEBUG ?= 0

APP_VERSION := $(shell git describe --always --tags)
Expand All @@ -15,9 +15,6 @@ TARGET ?= unix
RONN = ronn
MANPAGE = lwnbd.3

$(BIN): banner $(OBJ)
$(CC) $(CFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS) $(LIBS)

include .env
include src/Makefile

Expand Down Expand Up @@ -46,9 +43,16 @@ ifeq ($(MAKE_SILENT),1)
.SILENT: $(OBJ)
endif

ifeq ($(MAKE_FATAL),1)
CFLAGS += -Wfatal-errors
endif

$(MANPAGE): README.md
$(RONN) -r --pipe $< > $@

$(BIN): banner $(OBJ)
$(CC) $(CFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS) $(LIBS)

banner:
@echo " _ _ _ __ _ ______ ______"
@echo "| | | | | \\ | |_____] | \\"
Expand All @@ -58,7 +62,7 @@ banner:

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

nbdcleanup:
sudo lsof -t /dev/nbd* | sudo xargs -r kill -9
Expand Down
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BIN = local-reader \
local-shell \
lwnbd-server

CFLAGS = -I../include $(CFLAGS-$@) -g
CFLAGS = -I../include $(CFLAGS-$@) -DLWNBD_DEBUG
LWNBD_LIB := ../lwnbd.a
LDLIBS := $(LWNBD_LIB)
LDFLAGS += $(LDFLAGS-$@)
Expand Down
2 changes: 0 additions & 2 deletions examples/local-reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <stdlib.h>
#include <unistd.h>

extern struct lwnbd_plugin_t *file_plugin_init(void);

int main(int argc, const char **argv)
{
lwnbd_plugin_h fileplg;
Expand Down
53 changes: 47 additions & 6 deletions examples/local-shell.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
* An interactive shell using command plugin
*
*
*
*/

#define _GNU_SOURCE
Expand All @@ -10,9 +12,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <lwnbd-plugin.h>

extern struct lwnbd_plugin_t *command_plugin_init(void);

char *exportname, *prompt, *base;

Expand All @@ -37,13 +36,17 @@ static int set_exportname(int argc, char **argv, void *result, int64_t *size)

int main(int argc, char **argv)
{
lwnbd_plugin_h cmdplg;
lwnbd_plugin_h cmdplg, memplg;
lwnbd_context_t *ctx;
char *p, *buf;
char *p, *buf, memtest[512];
int errno, r = 0;
;
uint64_t size = 0;

/*
* set base URI, default exportname to 'shell' and prompt
*
*/

if (-1 == asprintf(&exportname, "shell"))
exit(EXIT_FAILURE);

Expand All @@ -53,6 +56,34 @@ int main(int argc, char **argv)
if (-1 == set_prompt(base, exportname))
exit(EXIT_FAILURE);

/*
* create a slice of memory to test memory query
*
*/

memplg = lwnbd_plugin_init(memory_plugin_init);

struct memory_config memh = {
.base = (uint64_t)memtest,
.name = "test",
.size = 512,
.desc = "",
};
lwnbd_plugin_new(memplg, &memh);

/*
* let's use query mechanism to set it
*/

if (-1 == asprintf(&buf, "test?memcpy=Example of shared memory.\n")) /* create the request */
exit(EXIT_FAILURE);

ctx = lwnbd_get_context(buf); /* GET */

/*
* create a command to change export, to be able to switch to it
*/

cmdplg = lwnbd_plugin_init(command_plugin_init);

struct lwnbd_command mycmd = {
Expand All @@ -63,6 +94,10 @@ int main(int argc, char **argv)

lwnbd_plugin_new(cmdplg, &mycmd);

/*
* main 'interactive' shell loop
*/

while (r == 0) {
int n;

Expand All @@ -84,6 +119,8 @@ int main(int argc, char **argv)
}

ctx = lwnbd_get_context(buf);
if (ctx == NULL)
continue;

if (size < ctx->exportsize) {
size = ctx->exportsize;
Expand All @@ -94,5 +131,9 @@ int main(int argc, char **argv)
printf("%s", buf);
}

free(buf);
free(exportname);
free(prompt);
free(base);
exit(EXIT_SUCCESS);
}
74 changes: 39 additions & 35 deletions examples/lwnbd-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,32 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <lwnbd/nbd.h>
#include <lwnbd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>

#include "../plugins/memory/memory.h"
#include "../servers/nbd/nbd.h"
typedef enum {
CLIENT_FREE,
CLIENT_INUSE,
} worker_state_t;

/* static glue, could be generate :
* #define LIST_ENTRY(x) x,
*
* */

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);
struct nbd_worker
{
uv_work_t req;
int client_id;
uv_stream_t *server;
};

#define MAX_CLIENTS 10
uv_work_t *client_reqs[MAX_CLIENTS];
#define MAX_CLIENTS 3
int gEnableWrite = 1;
int gHDDStartMode;

// plugin_init plugins_table[] = {
// file_plugin_init,
// NULL
// };
static worker_state_t client_states[MAX_CLIENTS];
struct nbd_worker *client_reqs[MAX_CLIENTS]; /* need to be accessible to signal handler */

void signal_handler(uv_signal_t *req, int signum)
{
Expand All @@ -66,17 +65,17 @@ int server_shutdown(int argc, char **argv, void *result, int64_t *size)
return 0;
}

int gEnableWrite = 1;
int gHDDStartMode;

void on_close(uv_handle_t *handle)
void on_after_work(uv_work_t *req, int status)
{
free(handle);
struct nbd_worker *client_reqs = (struct nbd_worker *)req;
client_states[client_reqs->client_id] = CLIENT_FREE;
free(req);
}

void on_after_work(uv_work_t *req, int status)
void on_close(uv_handle_t *handle)
{
free(req);
DEBUGLOG("on_close\n");
free(handle);
}

/*
Expand All @@ -86,7 +85,8 @@ void on_after_work(uv_work_t *req, int status)
void on_work(uv_work_t *req)
{
/* a bit of headache but avoid a baton */
uv_stream_t *server = (uv_stream_t *)req->data;
struct nbd_worker *client_reqs = (struct nbd_worker *)req;
uv_stream_t *server = (uv_stream_t *)client_reqs->server;
lwnbd_server_t *s = (lwnbd_server_t *)server->data;

uv_tcp_t *client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
Expand All @@ -101,8 +101,10 @@ void on_work(uv_work_t *req)

int sock;
uv_fileno((const uv_handle_t *)client, &sock);
DEBUGLOG("/++++++++++++++++++++++++++++++++++++++++++++++++++\n");
DEBUGLOG("Worker %d: Accepted fd %d\n", getpid(), sock);
lwnbd_server_run(*s, &sock); // the abstracted blocking loop
DEBUGLOG("+++++++++++++++++++++++++++++++++++++++++++++++++++/\n");
uv_close((uv_handle_t *)client, on_close);
}

Expand All @@ -116,19 +118,19 @@ void on_new_connection(uv_stream_t *server, int status)
}

for (i = 0; i < MAX_CLIENTS; i++) {
if (client_reqs[i] == NULL) {
client_reqs[i] = (uv_work_t *)malloc(sizeof(uv_work_t));
break;
DEBUGLOG("client_reqs[%d] = %p\n", i, client_reqs[i]);
if (client_states[i] == CLIENT_FREE) {
client_reqs[i] = (struct nbd_worker *)malloc(sizeof(struct nbd_worker));
client_states[i] = CLIENT_INUSE;
client_reqs[i]->client_id = i;
client_reqs[i]->server = server;
uv_queue_work(uv_default_loop(), (uv_work_t *)client_reqs[i], on_work, on_after_work);
return;
}
}

if (client_reqs[i] == NULL) {
LOG("New connection error : no slot available\n");
return;
}

client_reqs[i]->data = server;
uv_queue_work(uv_default_loop(), client_reqs[i], on_work, on_after_work);
LOG("New connection error : no slot available\n");
return;
}

int main(int argc, const char **argv)
Expand Down Expand Up @@ -234,5 +236,7 @@ int main(int argc, const char **argv)
uv_signal_init(loop, &sig);
uv_signal_start(&sig, signal_handler, SIGINT);

memset(client_states, CLIENT_FREE, sizeof(client_states));

return uv_run(loop, UV_RUN_DEFAULT);
}
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C" {

/* plugins options - useless here ? */

#define MAX_NUM_PLUGINS 5
#define MAX_NUM_PLUGINS 10
#define MAX_CONTEXTS 10

#ifdef __cplusplus
Expand Down
56 changes: 56 additions & 0 deletions include/lwnbd-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
*/

#ifndef INCLUDE_LWNBD_COMMON_H_
#define INCLUDE_LWNBD_COMMON_H_

#include "config.h"
#include <stdint.h>

/*
*
*/

/* experimental */
//#ifdef PLUGIN_COMMAND
extern struct lwnbd_plugin_t *command_plugin_init(void);
struct lwnbd_command
{
char *name;
char *desc;
int (*cmd)(int argc, char **argv, void *result, int64_t *size);
};
//#endif

//#ifdef PLUGIN_MEMORY
extern struct lwnbd_plugin_t *memory_plugin_init(void);
struct memory_config
{
intptr_t base;
intptr_t size;
char name[32];
char desc[64]; /* export description */
/* TODO readonly ? */
};
//#endif

//#ifdef PLUGIN_PCMSTREAM

struct pcmstream_config
{
char name[32];
char desc[64]; /* export description */
// char format[32];
int rate; /** output frequency in hz */
int bits; /** bits per sample (8, 16) */
int channels; /** output channels (1, 2) */
char volume;
/* input => readonly or output => writeonly */
};
//#endif

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

#endif /* INCLUDE_LWNBD_COMMON_H_ */
Loading

0 comments on commit 4ef3ae9

Please sign in to comment.