]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Flock: more container cbor
authorMaria Matejka <mq@ucw.cz>
Fri, 11 Oct 2024 06:27:56 +0000 (08:27 +0200)
committerMaria Matejka <mq@ucw.cz>
Sun, 23 Feb 2025 18:07:35 +0000 (19:07 +0100)
flock/container.c
flock/ctl.c
flock/hypervisor.c

index 568433ec4c5be19a059f3ec7d06967830640984b..476ef8ffa546b597d2401ec1e568c74c5958022e 100644 (file)
@@ -822,24 +822,28 @@ void
 hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_container_config *ccf)
 {
   birdloop_enter(hcf.loop);
-// const char *name, const char *basedir, const char *workdir
+
+#define FAIL(id, msg) do { \
+  CBOR_REPLY(cch, cw) CBOR_PUT_MAP(cw) { \
+    cbor_put_int(cw, id); cbor_put_string(cw, msg);\
+  } cbor_done_channel(cch); \
+  birdloop_leave(hcf.loop); \
+  return; } while (0)
+
+  if (!ccf->cf.name)
+    FAIL(-101, "Machine name not specified");
+
+  if (!ccf->workdir)
+    FAIL(-102, "Machine workdir not specified");
+
+  if (!ccf->basedir)
+    FAIL(-103, "Machine basedir not specified");
 
   const char *name = ccf->cf.name;
   uint h = mem_hash(name, strlen(name));
   struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h);
   if (crt)
-  {
-    CBOR_REPLY(cch, cw)
-      CBOR_PUT_MAP(cw)
-      {
-       cbor_put_int(cw, -127);
-       cbor_put_string(cw, "Container not created: Already exists");
-      }
-
-    cbor_done_channel(cch);
-    birdloop_leave(hcf.loop);
-    return;
-  }
+    FAIL(-127, "Container already exists");
 
   uint nlen = strlen(name);
   crt = mb_allocz(hcf.p, sizeof *crt + nlen + 1);
@@ -869,6 +873,7 @@ hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_contai
       cbor_put_string(cw, ccf->workdir);
     }
 
+#undef FAIL
   birdloop_leave(hcf.loop);
 }
 
index d633b7407d37bf6957a8d0cc34e4a0a22b2a7dcb..60db3d65f1fbc0e89664fef90f9540be5d0a80dc 100644 (file)
 
 struct hcs_parser_context {
   struct cbor_parser_context *ctx;
-  struct cbor_stream *stream;
   struct hcs_parser_channel *channel;
   sock *sock;
 
   u64 bytes_consumed;
   u64 major_state;
+
+  struct cbor_stream stream;
 };
 
 struct hcs_parser_channel {
@@ -74,7 +75,7 @@ hcs_parser_init(sock *s)
 
   htx->ctx = ctx;
   htx->sock = s;
-  htx->stream = cbor_stream_new(s->pool, 4);
+  cbor_stream_init(&htx->stream, 3);
 
   return htx;
 }
@@ -94,7 +95,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
   bool finish_cmd = false;
 
   for (int pos = 0; pos < size; pos++)
-  {
+  {// TODO here → convert to the new channel parser
     if (!hpc)
     {
       struct cbor_channel *cch = cbor_parse_channel(ctx, htx->stream, buf[pos]);
@@ -275,27 +276,14 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
          break;
 
        case 501:
-         /*
-         if (!hpc->cfg.cf.type)
-           CBOR_PARSER_ERROR("Machine type not specified");
-
-         if (!hpc->cfg.cf.name)
-           CBOR_PARSER_ERROR("Machine name not specified");
-
-         if (!hpc->cfg.container.workdir)
-           CBOR_PARSER_ERROR("Machine workdir not specified");
-
-         if (!hpc->cfg.container.basedir)
-           CBOR_PARSER_ERROR("Machine basedir not specified");
-
-         hypervisor_container_request(
-             htx->sock,
-             hpc->cfg.cf.name,
-             hpc->cfg.container.basedir,
-             hpc->cfg.container.workdir);
-             */
-
-         hypervisor_container_start(&hpc->cch, &hpc->cfg.container);
+         switch (hpc->cfg.cf.type)
+         {
+           case 1:
+             hypervisor_container_start(&hpc->cch, &hpc->cfg.container);
+             break;
+           default:
+             CBOR_PARSER_ERROR("Unknown machine type: %d", hpc->cfg.cf.type);
+         }
          htx->major_state = 1;
          break;
 
index 3c36b4501ef6899dbf548355ec52bf795a5ffda7..18d21a3124d75172e80f80a8507259b166d3f3f2 100644 (file)
@@ -417,49 +417,19 @@ hexp_get_telnet(sock *s, const char *name)
   sk_pause_rx(s->loop, s);
 }
 
-static void hexp_received_telnet(void *_data)
+static void hexp_received_telnet(struct hexp_received_telnet *hrt)
 {
-  struct hexp_received_telnet *hrt = _data;
-
-  ASSERT_DIE(he.port_name);
-  const char *name = he.port_name;
-  he.port_name = NULL;
-
-  sock *s = he.port_sreq;
-  he.port_sreq = NULL;
-
-  if (name[0])
+  if (hrt->name[0])
   {
     /* Transferring the received listening socket to the container */
-    int fd = container_ctl_fd(name);
-
-    /* TODO: unduplicate this code */
-    byte outbuf[128];
-    linpool *lp = lp_new(hcs_pool);
-    struct cbor_writer *cw = cbor_init(outbuf, sizeof outbuf, lp);
-    cbor_open_block_with_length(cw, 1);
-    cbor_add_int(cw, -2);
-    write_item(cw, 7, 22);
-    struct iovec v = {
-      .iov_base = outbuf,
-      .iov_len = cw->pt,
-    };
-    byte cbuf[CMSG_SPACE(sizeof hrt->fd)];
-    struct msghdr m = {
-      .msg_iov = &v,
-      .msg_iovlen = 1,
-      .msg_control = &cbuf,
-      .msg_controllen = sizeof cbuf,
-    };
-    struct cmsghdr *c = CMSG_FIRSTHDR(&m);
-    c->cmsg_level = SOL_SOCKET;
-    c->cmsg_type = SCM_RIGHTS;
-    c->cmsg_len = CMSG_LEN(sizeof hrt->fd);
-    memcpy(CMSG_DATA(c), &hrt->fd, sizeof hrt->fd);
+    struct cbor_channel *ccc = container_get_channel(hrt->name);
 
-    int e = sendmsg(fd, &m, 0);
-    if (e < 0)
-      log(L_ERR "Failed to send socket: %m");
+    CBOR_REPLY(ccc, cw)
+      CBOR_PUT_MAP(cw) {
+       cbor_put_int(cw, -2);
+       cbor_put_null(cw);
+       ccc->stream->s->txfd = hrt->fd;
+      }
 
     close(hrt->fd);
   }