]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
second socket does not work
authorKaterina Kubecova <katerina.kubecova@nic.cz>
Fri, 24 Nov 2023 12:57:27 +0000 (13:57 +0100)
committerKaterina Kubecova <katerina.kubecova@nic.cz>
Fri, 24 Nov 2023 12:57:27 +0000 (13:57 +0100)
nest/cbor.c
nest/cli.c
nest/cli.h
nest/cmds.c
nest/cmds.h
sysdep/unix/io.c
sysdep/unix/main.c
yang/show_status.json

index 418d32f572b68cf3842e28edaaabb7a83e1ae3a7..b44e86815c33b79e8da893b9c89889a4cf045316 100644 (file)
@@ -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");
   }
 }
index b54a0d76dc26dcaf5cf07bff0b150058a1237061..5ed63964af08663fd02911bbdf6c970625705359 100644 (file)
@@ -62,6 +62,8 @@
  * first one) and schedules cli.event .
  *
  */
+#include <stdio.h>
+#include <stdlib.h>
 
 #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;
+}
index 3596e37ca777d504c9784bbd1683d84f59ef23b8..c0bf93d5e11e1f6b829c8520d9006e99a0a6ac9e 100644 (file)
@@ -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);
 
index d0a72242370bc80252217894c8705164c2bd9e6b..daa35bc09ac98b29de578430453b29d7fe81e62c 100644 (file)
@@ -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
index 194a9d7ffe49841372628bac9b08a6876f36c61e..3dd969c62e1c00a4c78dc5c718c77816a46e466b 100644 (file)
@@ -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);
index 4b3eef4862f195d6af59035402e17fd5bb68838e..4e8083595a417fab9dafc3380a82204dc2ae927a 100644 (file)
@@ -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 */
index 0d7788bb418d872ee5065527e1f4db59fed12914..095224b34831c62718dbd4779f99a49d766cfd2a 100644 (file)
@@ -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();
index 276d448a54c398ba36a6a64b1f26d8738e403b57..4f06d1d15f097ca56fa6e52018418a840de7757a 100644 (file)
@@ -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