0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

second socket does not work

This commit is contained in:
Katerina Kubecova 2023-11-24 13:57:27 +01:00
parent af8a604b43
commit 10afb161bd
8 changed files with 161 additions and 32 deletions

View File

@ -5,8 +5,8 @@
struct cbor_writer { struct cbor_writer {
int pt; // where will next byte go int pt; // where will next byte go
int capacity; int capacity;
struct linpool *lp;
int8_t *cbor; int8_t *cbor;
struct linpool *lp;
}; };
void write_item(struct cbor_writer *writer, int8_t major, int num); 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 *cbor_init(byte *buff, uint capacity, struct linpool *lp) {
struct cbor_writer *writer = (struct cbor_writer *) lp_alloc(lp, sizeof(struct cbor_writer)); 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->cbor = buff;
writer->capacity = size_guess; writer->capacity = capacity;
writer->lp = lp;
writer->pt =0; writer->pt =0;
writer->lp = lp;
return writer; 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) { void check_memory(struct cbor_writer *writer, int add_size) {
if (writer->capacity - writer->pt-add_size < 0) { if (writer->capacity - writer->pt-add_size < 0) {
int8_t *a = writer->cbor; bug("There is not enough space for cbor response in given buffer");
writer->cbor = lp_alloc(writer->lp, writer->capacity*2);
memcpy(writer->cbor, a, writer->pt);
} }
} }

View File

@ -62,6 +62,8 @@
* first one) and schedules cli.event . * first one) and schedules cli.event .
* *
*/ */
#include <stdio.h>
#include <stdlib.h>
#include "nest/bird.h" #include "nest/bird.h"
#include "nest/cli.h" #include "nest/cli.h"
@ -69,6 +71,7 @@
#include "lib/string.h" #include "lib/string.h"
pool *cli_pool; pool *cli_pool;
pool *yi_pool;
static byte * static byte *
cli_alloc_out(cli *c, int size) cli_alloc_out(cli *c, int size)
@ -332,9 +335,17 @@ cli_kick(cli *c)
ev_schedule(c->event); 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 list cli_log_hooks;
static int cli_log_inited; static int cli_log_inited;
static list yi_log_hooks;
static int yi_log_inited;
void void
cli_set_log_echo(cli *c, uint mask, uint size) cli_set_log_echo(cli *c, uint mask, uint size)
{ {
@ -429,3 +440,11 @@ cli_init(void)
init_list(&cli_log_hooks); init_list(&cli_log_hooks);
cli_log_inited = 1; 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;
}

View File

@ -49,6 +49,7 @@ typedef struct cli {
extern pool *cli_pool; extern pool *cli_pool;
extern struct cli *this_cli; /* Used during parsing */ extern struct cli *this_cli; /* Used during parsing */
extern pool *yi_pool;
#define CLI_ASYNC_CODE 10000 #define CLI_ASYNC_CODE 10000
@ -65,8 +66,10 @@ static inline void cli_separator(cli *c)
cli *cli_new(void *); cli *cli_new(void *);
void cli_init(void); void cli_init(void);
void yi_init(void);
void cli_free(cli *); void cli_free(cli *);
void cli_kick(cli *); void cli_kick(cli *);
uint yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize);
void cli_written(cli *); void cli_written(cli *);
void cli_echo(uint class, byte *msg); void cli_echo(uint class, byte *msg);

View File

@ -45,9 +45,12 @@ cmd_show_status(void)
cli_msg(13, "Daemon is up and running"); 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()); tm_format_time(time, &config->tf_base, current_time());
struct cbor_writer *w = cbor_init(lp_new(proto_pool), 1000); 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_open_block_with_length(w, 3);
cbor_string_string(w, "BIRD", BIRD_VERSION); cbor_string_string(w, "BIRD", BIRD_VERSION);
cbor_add_string(w, "body"); cbor_add_string(w, "body");
@ -70,7 +73,7 @@ cmd_show_status(void)
cbor_add_string(w, "Reconfiguration in progress"); cbor_add_string(w, "Reconfiguration in progress");
else else
cbor_add_string(w, "Daemon is up and running"); cbor_add_string(w, "Daemon is up and running");
cbor_write_to_file(w, "test.cbor"); cbor_write_to_file(w, "test.cbor");*/
} }
void void
@ -156,9 +159,12 @@ cmd_show_memory(void)
#endif #endif
print_size("Total:", total); print_size("Total:", total);
cli_msg(0, ""); 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_open_block_with_length(w, 1);
cbor_add_string(w, "show_memory:message"); 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_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"); cbor_write_to_file(w, "show_memory.cbor");
return w->pt;
} }
void void

View File

@ -16,6 +16,7 @@ struct f_inst;
void cmd_show_status(void); void cmd_show_status(void);
void cmd_show_symbols(struct sym_show_data *sym); void cmd_show_symbols(struct sym_show_data *sym);
void cmd_show_memory(void); void cmd_show_memory(void);
uint cmd_show_memory_cbor(byte *tbuf, uint capacity);
struct f_line; struct f_line;
void cmd_eval(const struct f_line *expr); void cmd_eval(const struct f_line *expr);

View File

@ -1810,6 +1810,7 @@ sk_send_full(sock *s, unsigned len, struct iface *ifa,
static void static void
call_rx_hook(sock *s, int size) call_rx_hook(sock *s, int size)
{ {
log(L_WARN "soc rx_hook");
if (s->rx_hook(s, size)) if (s->rx_hook(s, size))
{ {
/* We need to be careful since the socket could have been deleted by the hook */ /* We need to be careful since the socket could have been deleted by the hook */

View File

@ -396,7 +396,9 @@ cmd_reconfig_status(void)
*/ */
static sock *cli_sk; static sock *cli_sk;
static sock *yi_sk;
static char *path_control_socket = PATH_CONTROL_SOCKET; static char *path_control_socket = PATH_CONTROL_SOCKET;
static char *path_control_socket_yi = NULL;
static void static void
@ -476,10 +478,23 @@ cli_get_command(cli *c)
static int static int
cli_rx(sock *s, uint size UNUSED) cli_rx(sock *s, uint size UNUSED)
{ {
log("rcli_rx");
cli_kick(s->data); cli_kick(s->data);
return 0; 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 static void
cli_err(sock *s, int err) 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)); 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 static int
cli_connect(sock *s, uint size UNUSED) cli_connect(sock *s, uint size UNUSED)
{ {
cli *c; cli *c;
if (config->cli_debug) //if (config->cli_debug)
log(L_INFO "CLI connect"); log(L_INFO "CLI connect");
s->rx_hook = cli_rx; s->rx_hook = cli_rx;
s->tx_hook = cli_tx; s->tx_hook = cli_tx;
@ -519,6 +542,56 @@ cli_connect(sock *s, uint size UNUSED)
return 1; 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 static void
cli_init_unix(uid_t use_uid, gid_t use_gid) 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"); die("chmod: %m");
} }
/* /*
* PID file * PID file
*/ */
@ -695,7 +769,7 @@ signal_init(void)
* Parsing of command-line arguments * 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; int parse_and_exit;
char *bird_name; char *bird_name;
static char *use_user; static char *use_user;
@ -838,6 +912,9 @@ parse_args(int argc, char **argv)
path_control_socket = optarg; path_control_socket = optarg;
socket_changed = 1; socket_changed = 1;
break; break;
case 'Y':
path_control_socket_yi = optarg;
break;
case 'P': case 'P':
pid_file = optarg; pid_file = optarg;
break; break;
@ -907,6 +984,15 @@ main(int argc, char **argv)
{ {
test_old_bird(path_control_socket); test_old_bird(path_control_socket);
cli_init_unix(use_uid, use_gid); 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) if (use_gid)
@ -939,7 +1025,7 @@ main(int argc, char **argv)
dup2(0, 1); dup2(0, 1);
dup2(0, 2); dup2(0, 2);
} }
log("before main thread init");
main_thread_init(); main_thread_init();
write_pid_file(); write_pid_file();

View File

@ -1,16 +1,31 @@
{ {
"show_status:message": { "show_memory:message": {
"header": { "header": "BIRD memory usage",
"bird": "BIRD ", "body": {
"version": "2.14" "routing_tables": {
}, "effective": 34604,
"body": { "overhead": 2848
"router_id": "35496", },
"hostname": "bird", "route_attributes": {
"server_time": "2020-10-10T10:10:10+00:00" , "effective": 26826,
"last_reboot": "2020-10-10T10:10:10+00:00" , "overhead": 13448
"last_reconfiguration": "2020-10-10T10:10:10+00:00" },
}, "protocols": {
"state": "Daemon is up and running" "effective": 63564,
"overhead": 15312
},
"current_config": {
"effective": 299744,
"overhead": 2152
},
"standby_memory": {
"effective": 0,
"overhead": 348160
},
"total": {
"effective": 506754,
"overhead": 388960
}
}
} }
} }