From 10afb161bd3c2c173f56607484ccec4ea96f546c Mon Sep 17 00:00:00 2001 From: Katerina Kubecova Date: Fri, 24 Nov 2023 13:57:27 +0100 Subject: [PATCH] second socket does not work --- nest/cbor.c | 16 ++++---- nest/cli.c | 19 +++++++++ nest/cli.h | 3 ++ nest/cmds.c | 18 ++++++--- nest/cmds.h | 1 + sysdep/unix/io.c | 1 + sysdep/unix/main.c | 92 +++++++++++++++++++++++++++++++++++++++++-- yang/show_status.json | 43 +++++++++++++------- 8 files changed, 161 insertions(+), 32 deletions(-) diff --git a/nest/cbor.c b/nest/cbor.c index 418d32f57..b44e86815 100644 --- a/nest/cbor.c +++ b/nest/cbor.c @@ -5,8 +5,8 @@ struct cbor_writer { int pt; // where will next byte go int capacity; - struct linpool *lp; int8_t *cbor; + struct linpool *lp; }; void write_item(struct cbor_writer *writer, int8_t major, int num); @@ -14,12 +14,12 @@ void check_memory(struct cbor_writer *writer, int add_size); -struct cbor_writer *cbor_init(struct linpool *lp, int size_guess) { - struct cbor_writer *writer = (struct cbor_writer *) lp_alloc(lp, sizeof(struct cbor_writer)); - writer->cbor = lp_alloc(lp, size_guess * sizeof(int8_t)); - writer->capacity = size_guess; - writer->lp = lp; +struct cbor_writer *cbor_init(byte *buff, uint capacity, struct linpool *lp) { + struct cbor_writer *writer = (struct cbor_writer*)lp_alloc(lp, sizeof(struct cbor_writer)); + writer->cbor = buff; + writer->capacity = capacity; writer->pt =0; + writer->lp = lp; return writer; } @@ -107,8 +107,6 @@ void write_item(struct cbor_writer *writer, int8_t major, int num) { void check_memory(struct cbor_writer *writer, int add_size) { if (writer->capacity - writer->pt-add_size < 0) { - int8_t *a = writer->cbor; - writer->cbor = lp_alloc(writer->lp, writer->capacity*2); - memcpy(writer->cbor, a, writer->pt); + bug("There is not enough space for cbor response in given buffer"); } } diff --git a/nest/cli.c b/nest/cli.c index b54a0d76d..5ed63964a 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -62,6 +62,8 @@ * first one) and schedules cli.event . * */ +#include +#include #include "nest/bird.h" #include "nest/cli.h" @@ -69,6 +71,7 @@ #include "lib/string.h" pool *cli_pool; +pool *yi_pool; static byte * cli_alloc_out(cli *c, int size) @@ -332,9 +335,17 @@ cli_kick(cli *c) ev_schedule(c->event); } +uint +yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize) { + return cmd_show_memory_cbor(tbuf, tbsize); +} + static list cli_log_hooks; static int cli_log_inited; +static list yi_log_hooks; +static int yi_log_inited; + void cli_set_log_echo(cli *c, uint mask, uint size) { @@ -429,3 +440,11 @@ cli_init(void) init_list(&cli_log_hooks); cli_log_inited = 1; } + +void +yi_init(void) +{ + yi_pool = rp_new(&root_pool, "YI"); + init_list(&yi_log_hooks); /*we may be do not need this*/ + yi_log_inited = 1; +} diff --git a/nest/cli.h b/nest/cli.h index 3596e37ca..c0bf93d5e 100644 --- a/nest/cli.h +++ b/nest/cli.h @@ -49,6 +49,7 @@ typedef struct cli { extern pool *cli_pool; extern struct cli *this_cli; /* Used during parsing */ +extern pool *yi_pool; #define CLI_ASYNC_CODE 10000 @@ -65,8 +66,10 @@ static inline void cli_separator(cli *c) cli *cli_new(void *); void cli_init(void); +void yi_init(void); void cli_free(cli *); void cli_kick(cli *); +uint yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize); void cli_written(cli *); void cli_echo(uint class, byte *msg); diff --git a/nest/cmds.c b/nest/cmds.c index d0a722423..daa35bc09 100644 --- a/nest/cmds.c +++ b/nest/cmds.c @@ -45,9 +45,12 @@ cmd_show_status(void) cli_msg(13, "Daemon is up and running"); - byte time[TM_DATETIME_BUFFER_SIZE]; + /*byte time[TM_DATETIME_BUFFER_SIZE]; tm_format_time(time, &config->tf_base, current_time()); struct cbor_writer *w = cbor_init(lp_new(proto_pool), 1000); + cbor_open_block_with_length(w, 1); + cbor_add_string(w, "show_status:message"); + cbor_open_block_with_length(w, 3); cbor_string_string(w, "BIRD", BIRD_VERSION); cbor_add_string(w, "body"); @@ -70,7 +73,7 @@ cmd_show_status(void) cbor_add_string(w, "Reconfiguration in progress"); else cbor_add_string(w, "Daemon is up and running"); - cbor_write_to_file(w, "test.cbor"); + cbor_write_to_file(w, "test.cbor");*/ } void @@ -156,9 +159,12 @@ cmd_show_memory(void) #endif print_size("Total:", total); cli_msg(0, ""); - - - struct cbor_writer *w = cbor_init(lp_new(proto_pool), 1000); +} + +uint +cmd_show_memory_cbor(byte *tbuf, uint capacity) { + bug("in cmd_show_memory_cbor"); + struct cbor_writer *w = cbor_init(tbuf, capacity, lp_new(proto_pool)); cbor_open_block_with_length(w, 1); cbor_add_string(w, "show_memory:message"); @@ -191,7 +197,7 @@ cmd_show_memory(void) cbor_close_block_or_list(w); // we do not know for sure, that standby memory will be printed, so we do not know number of block items. If we know that, we open the block for 6 (or 5) items and we do not close anything cbor_write_to_file(w, "show_memory.cbor"); - + return w->pt; } void diff --git a/nest/cmds.h b/nest/cmds.h index 194a9d7ff..3dd969c62 100644 --- a/nest/cmds.h +++ b/nest/cmds.h @@ -16,6 +16,7 @@ struct f_inst; void cmd_show_status(void); void cmd_show_symbols(struct sym_show_data *sym); void cmd_show_memory(void); +uint cmd_show_memory_cbor(byte *tbuf, uint capacity); struct f_line; void cmd_eval(const struct f_line *expr); diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 4b3eef486..4e8083595 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -1810,6 +1810,7 @@ sk_send_full(sock *s, unsigned len, struct iface *ifa, static void call_rx_hook(sock *s, int size) { + log(L_WARN "soc rx_hook"); if (s->rx_hook(s, size)) { /* We need to be careful since the socket could have been deleted by the hook */ diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 0d7788bb4..095224b34 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -396,7 +396,9 @@ cmd_reconfig_status(void) */ static sock *cli_sk; +static sock *yi_sk; static char *path_control_socket = PATH_CONTROL_SOCKET; +static char *path_control_socket_yi = NULL; static void @@ -476,10 +478,23 @@ cli_get_command(cli *c) static int cli_rx(sock *s, uint size UNUSED) { + log("rcli_rx"); cli_kick(s->data); return 0; } + +static int +yi_rx(sock *s, uint size) +{ + /* zpracuj data délky len začínající na s->rbuf */ + /* zapiš výsledek do s->tbuf */ + log("size tbuf %ui", s->tbsize); + uint tx_len = yi_process(size, s->rbuf, s->tbuf, s->tbsize); + sk_send(s, tx_len); + return 1; +} + static void cli_err(sock *s, int err) { @@ -501,12 +516,20 @@ cli_connect_err(sock *s UNUSED, int err) log(L_INFO "Failed to accept CLI connection: %s", strerror(err)); } +static void +yi_connect_err(sock *s UNUSED, int err) +{ + ASSERT_DIE(err); + if (config->cli_debug) + log(L_INFO "Failed to accept YI connection: %s", strerror(err)); +} + static int cli_connect(sock *s, uint size UNUSED) { cli *c; - if (config->cli_debug) + //if (config->cli_debug) log(L_INFO "CLI connect"); s->rx_hook = cli_rx; s->tx_hook = cli_tx; @@ -519,6 +542,56 @@ cli_connect(sock *s, uint size UNUSED) return 1; } +static int +yi_connect(sock *s, uint size UNUSED) +{ + cli *c; + log("YI connect"); + //if (config->cli_debug) + log(L_INFO "YANG connect"); + s->rx_hook = yi_rx; + s->tx_hook = cli_tx; + s->err_hook = cli_err; + s->data = c = cli_new(s); + s->pool = c->pool; + s->fast_rx = 1; + c->rx_pos = c->rx_buf; + rmove(s, c->pool); + log("connect ok"); + return 1; +} + + +static void +yi_init_unix(uid_t use_uid, gid_t use_gid) +{ + sock *s; + log("yi_init_unix before"); + yi_init(); + s = yi_sk = sk_new(yi_pool); + s->type = SK_UNIX_PASSIVE; + s->rx_hook = yi_connect; + s->err_hook = yi_connect_err; + s->rbsize = 1024; + s->tbsize = 16384; + s->fast_rx = 1; + log("yi_init_unix after set"); + /* Return value intentionally ignored */ + unlink(path_control_socket_yi); + + if (sk_open_unix(s, path_control_socket_yi) < 0) + die("Cannot create control socket %s: %m", path_control_socket_yi); + + if (use_uid || use_gid) + if (chown(path_control_socket_yi, use_uid, use_gid) < 0) + die("chown: %m"); + + if (chmod(path_control_socket_yi, 0660) < 0) + die("chmod: %m"); + log("yi_init_unix after fc"); +} + + static void cli_init_unix(uid_t use_uid, gid_t use_gid) { @@ -546,6 +619,7 @@ cli_init_unix(uid_t use_uid, gid_t use_gid) die("chmod: %m"); } + /* * PID file */ @@ -695,7 +769,7 @@ signal_init(void) * Parsing of command-line arguments */ -static char *opt_list = "bc:dD:ps:P:u:g:flRh"; +static char *opt_list = "bc:dD:ps:Y:P:u:g:flRh"; int parse_and_exit; char *bird_name; static char *use_user; @@ -838,6 +912,9 @@ parse_args(int argc, char **argv) path_control_socket = optarg; socket_changed = 1; break; + case 'Y': + path_control_socket_yi = optarg; + break; case 'P': pid_file = optarg; break; @@ -907,6 +984,15 @@ main(int argc, char **argv) { test_old_bird(path_control_socket); cli_init_unix(use_uid, use_gid); + if (path_control_socket_yi) + { + yi_init_unix(use_uid, use_gid); + } + else { //todo delete + path_control_socket_yi = "bird-yang.ctl"; + log(L_INFO "before function"); + yi_init_unix(use_uid, use_gid); + } } if (use_gid) @@ -939,7 +1025,7 @@ main(int argc, char **argv) dup2(0, 1); dup2(0, 2); } - + log("before main thread init"); main_thread_init(); write_pid_file(); diff --git a/yang/show_status.json b/yang/show_status.json index 276d448a5..4f06d1d15 100644 --- a/yang/show_status.json +++ b/yang/show_status.json @@ -1,16 +1,31 @@ { - "show_status:message": { - "header": { - "bird": "BIRD ", - "version": "2.14" - }, - "body": { - "router_id": "35496", - "hostname": "bird", - "server_time": "2020-10-10T10:10:10+00:00" , - "last_reboot": "2020-10-10T10:10:10+00:00" , - "last_reconfiguration": "2020-10-10T10:10:10+00:00" - }, - "state": "Daemon is up and running" + "show_memory:message": { + "header": "BIRD memory usage", + "body": { + "routing_tables": { + "effective": 34604, + "overhead": 2848 + }, + "route_attributes": { + "effective": 26826, + "overhead": 13448 + }, + "protocols": { + "effective": 63564, + "overhead": 15312 + }, + "current_config": { + "effective": 299744, + "overhead": 2152 + }, + "standby_memory": { + "effective": 0, + "overhead": 348160 + }, + "total": { + "effective": 506754, + "overhead": 388960 + } + } } -} +} \ No newline at end of file -- 2.47.2