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

Flock: more cbor-ing

This commit is contained in:
Maria Matejka 2024-10-08 22:02:48 +02:00
parent ffec1451a9
commit 393559ace7
3 changed files with 134 additions and 182 deletions

View File

@ -21,30 +21,25 @@
static struct hypervisor_container_forker { static struct hypervisor_container_forker {
sock *s; sock *s;
pool *p; pool *p;
struct cbor_stream *stream;
struct birdloop *loop; struct birdloop *loop;
HASH(struct container_runtime) hash; HASH(struct container_runtime) hash;
struct container_runtime *cur_crt; struct container_runtime *cur_crt;
int ctl[2]; /* socketpair filedescriptors */ int ctl[2]; /* socketpair filedescriptors */
} hcf; } hcf;
static struct container_config { struct container_fork_request {
const char *hostname; struct cbor_channel cch;
const char *workdir; struct cbor_channel *ctl_ch;
const char *basedir; struct container_runtime *crt;
} ccf; };
struct container_runtime { struct container_runtime {
struct container_runtime *next; struct container_runtime *next;
struct container_config ccf;
uint hash; uint hash;
pid_t pid; pid_t pid;
sock *s; sock *s;
struct container_operation_callback { char hostname[];
callback cb;
sock *s;
void *data;
} *ccc;
char data[];
}; };
#define CRT_KEY(c) c->ccf.hostname, c->hash #define CRT_KEY(c) c->ccf.hostname, c->hash
@ -289,7 +284,7 @@ container_init_logger(void)
} }
static void static void
container_mainloop(int fd) container_mainloop(int fd, struct flock_machine_container_config *ccf)
{ {
log(L_INFO "container mainloop with fd %d", fd); log(L_INFO "container mainloop with fd %d", fd);
@ -300,12 +295,12 @@ container_mainloop(int fd)
/* Move to the workdir */ /* Move to the workdir */
linpool *lp = lp_new(&root_pool); linpool *lp = lp_new(&root_pool);
if (strchr(ccf.basedir, ',') || if (strchr(ccf->basedir, ',') ||
strchr(ccf.basedir, '=') || strchr(ccf->basedir, '=') ||
strchr(ccf.basedir, '\\')) strchr(ccf->basedir, '\\'))
die("Refusing to work with paths containing chars: ,=\\"); die("Refusing to work with paths containing chars: ,=\\");
int wfd = GETDIR(lp_sprintf(lp, "%s%s", ccf.workdir[0] == '/' ? "" : "./", ccf.workdir)); int wfd = GETDIR(lp_sprintf(lp, "%s%s", ccf->workdir[0] == '/' ? "" : "./", ccf->workdir));
SYSCALL(fchdir, wfd); SYSCALL(fchdir, wfd);
close(wfd); wfd = -1; close(wfd); wfd = -1;
@ -313,16 +308,16 @@ container_mainloop(int fd)
close(GETDIR(lp_strdup(lp, "./tmp"))); close(GETDIR(lp_strdup(lp, "./tmp")));
close(GETDIR(lp_strdup(lp, "./root"))); close(GETDIR(lp_strdup(lp, "./root")));
bool cloneroot = !strcmp(ccf.basedir, "/"); bool cloneroot = !strcmp(ccf->basedir, "/");
bool clonedev = cloneroot; bool clonedev = cloneroot;
if (cloneroot) if (cloneroot)
{ {
ccf.basedir = "./lower"; ccf->basedir = "./lower";
close(GETDIR(lp_strdup(lp, "./lower"))); close(GETDIR(lp_strdup(lp, "./lower")));
} }
const char *overlay_mount_options = lp_sprintf(lp, "lowerdir=%s,upperdir=%s,workdir=%s", const char *overlay_mount_options = lp_sprintf(lp, "lowerdir=%s,upperdir=%s,workdir=%s",
ccf.basedir, "./upper", "./tmp"); ccf->basedir, "./upper", "./tmp");
SYSCALL(mount, "overlay", "./root", "overlay", 0, overlay_mount_options); SYSCALL(mount, "overlay", "./root", "overlay", 0, overlay_mount_options);
if (cloneroot) if (cloneroot)
@ -531,10 +526,10 @@ container_mainloop(int fd)
static uint container_counter = 0; static uint container_counter = 0;
static void static void
container_start(void) container_start(struct flock_machine_container_config *ccf)
{ {
log(L_INFO "Requested to start a container, name %s, base %s, work %s", log(L_INFO "Requested to start a container, name %s, base %s, work %s",
ccf.hostname, ccf.basedir, ccf.workdir); ccf->cf.name, ccf->basedir, ccf->workdir);
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) if (pid < 0)
@ -599,7 +594,7 @@ container_start(void)
ASSERT_DIE(container_counter < 0x6000); ASSERT_DIE(container_counter < 0x6000);
this_thread_id -= (container_counter << 1) + 0x3000 ; this_thread_id -= (container_counter << 1) + 0x3000 ;
container_mainloop(fds[1]); /* this never returns */ container_mainloop(fds[1], ccf); /* this never returns */
bug("container_mainloop has returned"); bug("container_mainloop has returned");
} }
@ -757,6 +752,7 @@ hypervisor_container_forker_err(sock *sk, int e UNUSED)
/* The child */ /* The child */
#if 0
static void static void
crt_err(sock *s, int err UNUSED) crt_err(sock *s, int err UNUSED)
{ {
@ -799,92 +795,54 @@ container_created(callback *cb)
mb_free(ccc); mb_free(ccc);
} }
#endif
void void
hypervisor_container_request(sock *s, const char *name, const char *basedir, const char *workdir) hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_container_config *ccf)
{ {
birdloop_enter(hcf.loop); birdloop_enter(hcf.loop);
// const char *name, const char *basedir, const char *workdir
const char *name = ccf->cf.name;
uint h = mem_hash(name, strlen(name)); uint h = mem_hash(name, strlen(name));
struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h); struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h);
if (crt) if (crt)
{ {
struct { CBOR_REPLY(cch, cw)
struct cbor_writer w; CBOR_PUT_MAP(cw)
struct cbor_writer_stack_item si[2]; {
} _cw; cbor_put_int(cw, -127);
cbor_put_string(cw, "Container not created: Already exists");
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize); }
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -127);
cbor_put_string(cw, "BAD: Already exists");
}
sk_send(s, cw->data.pos - cw->data.start);
cbor_done_channel(cch);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
return; return;
} }
uint nlen = strlen(name), uint nlen = strlen(name);
blen = strlen(basedir), crt = mb_allocz(hcf.p, sizeof *crt + nlen + 1);
wlen = strlen(workdir);
crt = mb_allocz(hcf.p, sizeof *crt + nlen + blen + wlen + 3);
char *pos = crt->data;
crt->ccf.hostname = pos;
memcpy(pos, name, nlen + 1);
pos += nlen + 1;
crt->ccf.workdir = pos;
memcpy(pos, workdir, wlen + 1);
pos += wlen + 1;
crt->ccf.basedir = pos;
memcpy(pos, basedir, blen + 1);
pos += blen + 1;
crt->hash = h; crt->hash = h;
memcpy(crt->hostname, name, nlen + 1);
struct container_operation_callback *ccc = mb_alloc(s->pool, sizeof *ccc);
*ccc = (struct container_operation_callback) {
.s = s,
.data = s->data,
};
callback_init(&ccc->cb, container_created, s->loop);
crt->ccc = ccc;
HASH_INSERT(hcf.hash, CRT, crt); HASH_INSERT(hcf.hash, CRT, crt);
ASSERT_DIE(hcf.cur_crt == NULL); log(L_INFO "requesting machine creation, name %s", name);
hcf.cur_crt = crt; SKIP_BACK_DECLARE(struct container_fork_request, cfr, cch, cbor_channel_new(hcf.stream));
cfr->crl_ch = cch;
cfr->crt = crt;
cfr->cch.parse = container_fork_request_reply;
log(L_INFO "requesting machine creation, socket %p", s); CBOR_REPLY(&cfr->cch, cw)
CBOR_PUT_MAP(cw)
struct { {
struct cbor_writer w; cbor_put_int(cw, 0);
struct cbor_writer_stack_item si[2]; cbor_put_string(cw, name);
} _cw; cbor_put_int(cw, 1);
cbor_put_string(cw, ccf->basedir);
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize); cbor_put_int(cw, 2);
CBOR_PUT_MAP(cw) cbor_put_string(cw, ccf->workdir);
{ }
cbor_put_int(cw, 0);
cbor_put_string(cw, name);
cbor_put_int(cw, 1);
cbor_put_string(cw, basedir);
cbor_put_int(cw, 2);
cbor_put_string(cw, workdir);
}
sk_send(s, cw->data.pos - cw->data.start);
s->err_paused = crt_err;
s->data = crt;
sk_pause_rx(s->loop, s);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
} }
@ -916,58 +874,45 @@ container_stopped(callback *cb)
} }
void void
hypervisor_container_shutdown(sock *s, const char *name) hypervisor_container_shutdown(struct cbor_channel *cch, struct flock_machine_container_config *ccf)
{ {
birdloop_enter(hcf.loop); birdloop_enter(hcf.loop);
const char *name = ccf->cf.name;
uint h = mem_hash(name, strlen(name)); uint h = mem_hash(name, strlen(name));
struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h); struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h);
if (!crt || !crt->s) if (!crt || !crt->s)
{ {
struct { CBOR_REPLY(cch, cw)
struct cbor_writer w; CBOR_PUT_MAP(cw)
struct cbor_writer_stack_item si[2]; {
} _cw; cbor_put_int(cw, -127);
cbor_put_string(cw, "BAD: Not found");
}
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize); cbor_done_channel(cch);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -127);
cbor_put_string(cw, "BAD: Not found");
}
sk_send(s, cw->data.pos - cw->data.start);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
return; return;
} }
struct { struct cbor_channel *xch = cbor_channel_new(crt->stream);
struct cbor_writer w; CBOR_REPLY(xch, cw)
struct cbor_writer_stack_item si[2]; CBOR_PUT_MAP(cw)
} _cw; {
cbor_put_int(cw, 0);
cbor_put_null(cw);
}
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize); struct container_operation_callback *ccc = mb_alloc(cch->pool, sizeof *ccc);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, 0);
cbor_put_null(cw);
}
sk_send(s, cw->data.pos - cw->data.start);
struct container_operation_callback *ccc = mb_alloc(s->pool, sizeof *ccc);
*ccc = (struct container_operation_callback) { *ccc = (struct container_operation_callback) {
.s = s, .cch = cch,
.data = s->data, .ccf = ccf,
.cancel = container_operation_hangup,
}; };
callback_init(&ccc->cb, container_stopped, s->loop); callback_init(&ccc->cb, container_stopped, s->loop);
crt->ccc = ccc; crt->ccc = ccc;
s->err_paused = crt_err;
s->data = crt;
sk_pause_rx(s->loop, s);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
} }

View File

@ -22,6 +22,7 @@
struct hcs_parser_context { struct hcs_parser_context {
struct cbor_parser_context *ctx; struct cbor_parser_context *ctx;
struct cbor_stream *stream; struct cbor_stream *stream;
struct hcs_parser_channel *channel;
sock *sock; sock *sock;
u64 bytes_consumed; u64 bytes_consumed;
@ -44,30 +45,25 @@ struct hcs_parser_channel {
}; };
static void static void
//hcs_request_poweroff(struct hcs_parser_channel *hpc) hcs_request_poweroff(struct hcs_parser_channel *hpc)
hcs_request_poweroff(struct hcs_parser_context *htx)
{ {
log(L_INFO "Requested shutdown via CLI"); log(L_INFO "Requested shutdown via CLI");
ev_send_loop(&main_birdloop, &poweroff_event); ev_send_loop(&main_birdloop, &poweroff_event);
struct { CBOR_REPLY(&hpc->cch, cw)
struct cbor_writer w; CBOR_PUT_MAP(cw)
struct cbor_writer_stack_item si[2]; {
} _cw; cbor_put_int(cw, -1);
cbor_put_string(cw, "OK");
}
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, htx->sock->tbuf, htx->sock->tbsize); cbor_done_channel(&hpc->cch);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -1);
cbor_put_string(cw, "OK");
}
sk_send(htx->sock, cw->data.pos - cw->data.start);
} }
static void static void
hcs_get_telnet(struct hcs_parser_context *htx) hcs_get_telnet(struct hcs_parser_channel *hpc)
{ {
// TODO: hexp_get_telnet();
} }
struct hcs_parser_context * struct hcs_parser_context *
@ -93,18 +89,21 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
{ {
ASSERT_DIE(size > 0); ASSERT_DIE(size > 0);
struct cbor_parser_context *ctx = htx->ctx; struct cbor_parser_context *ctx = htx->ctx;
struct hcs_parser_channel *hpc = htx->channel;
bool finish_cmd = false;
for (int pos = 0; pos < size; pos++) for (int pos = 0; pos < size; pos++)
{ {
/* if (!hpc)
if (!htx->channel)
{ {
htx->channel = cbor_parse_channel(ctx, htx->stream, buf[pos]); struct cbor_channel *cch = cbor_parse_channel(ctx, htx->stream, buf[pos]);
if (htx->channel == &cbor_channel_parse_error) if (cch == &cbor_channel_parse_error)
return -(htx->bytes_consumed + pos + 1); return -(htx->bytes_consumed + pos + 1);
else if (cch)
hpc = htx->channel = SKIP_BACK(struct hcs_parser_channel, cch, cch);
continue; continue;
} }
*/
switch (cbor_parse_byte(ctx, buf[pos])) switch (cbor_parse_byte(ctx, buf[pos]))
{ {
@ -120,42 +119,31 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
/* Check type acceptance */ /* Check type acceptance */
switch (htx->major_state) switch (htx->major_state)
{ {
case 0: /* toplevel */ case 0: /* Command */
if (ctx->type != 4) CBOR_PARSE_ONLY(ctx, POSINT, hpc->cmd);
CBOR_PARSER_ERROR("Expected array, got %u", ctx->type); if (hpc->cmd > HCS_CMD__MAX)
CBOR_PARSER_ERROR("Command key too high, got %lu", hpc->cmd);
if (ctx->value != 3) htx->major_state = hpc->cmd + 10;
CBOR_PARSER_ERROR("Expected array of length 1, got %u", ctx->value);
htx->major_state = 1;
break;
case 1: /* Command */
CBOR_PARSE_ONLY(ctx, POSINT, htx->cmd);
if (htx->cmd > HCS_CMD__MAX)
CBOR_PARSER_ERROR("Command key too high, got %lu", htx->cmd);
htx->major_state = htx->cmd + 10;
break; break;
case HCS_CMD_SHUTDOWN + 10: /* shutdown command: expected null */ case HCS_CMD_SHUTDOWN + 10: /* shutdown command: expected null */
if ((ctx->type != 7) || (ctx->value != 22)) if ((ctx->type != 7) || (ctx->value != 22))
CBOR_PARSER_ERROR("Expected null, got %u-%u", ctx->type, ctx->value); CBOR_PARSER_ERROR("Expected null, got %u-%u", ctx->type, ctx->value);
hcs_request_poweroff(htx); hcs_request_poweroff(hpc);
finish_cmd = true;
htx->major_state = 3;
break; break;
case HCS_CMD_TELNET + 10: /* telnet listener open */ case HCS_CMD_TELNET + 10: /* telnet listener open */
if ((ctx->type == 7) && (ctx->value == 22)) if ((ctx->type == 7) && (ctx->value == 22))
{ {
hcs_get_telnet(htx); hcs_get_telnet(hpc);
htx->major_state = 3; finish_cmd = true;
break; break;
} }
else CBOR_PARSE_IF(ctx, TEXT, htx->cfg.cf.name) else CBOR_PARSE_IF(ctx, TEXT, hpc->cfg.cf.name)
; ;
else else
CBOR_PARSER_ERROR("Expected null or string, got %s", cbor_type_str(ctx->type)); CBOR_PARSER_ERROR("Expected null or string, got %s", cbor_type_str(ctx->type));
@ -189,11 +177,11 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
break; break;
case 502: /* machine creation argument 0: name */ case 502: /* machine creation argument 0: name */
CBOR_PARSE_ONLY(ctx, TEXT, htx->cfg.cf.name); CBOR_PARSE_ONLY(ctx, TEXT, hpc->cfg.cf.name);
break; break;
case 503: /* machine creation argument 1: type */ case 503: /* machine creation argument 1: type */
CBOR_PARSE_ONLY(ctx, POSINT, htx->cfg.cf.type); CBOR_PARSE_ONLY(ctx, POSINT, hpc->cfg.cf.type);
if ((ctx->value < 1) && (ctx->value > 1) ) if ((ctx->value < 1) && (ctx->value > 1) )
CBOR_PARSER_ERROR("Unexpected type, got %lu", ctx->value); CBOR_PARSER_ERROR("Unexpected type, got %lu", ctx->value);
@ -202,11 +190,11 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
break; break;
case 504: /* machine creation argument 2: basedir */ case 504: /* machine creation argument 2: basedir */
CBOR_PARSE_ONLY(ctx, BYTES, htx->cfg.container.basedir); CBOR_PARSE_ONLY(ctx, BYTES, hpc->cfg.container.basedir);
break; break;
case 505: /* machine creation argument 3: workdir */ case 505: /* machine creation argument 3: workdir */
CBOR_PARSE_ONLY(ctx, BYTES, htx->cfg.container.workdir); CBOR_PARSE_ONLY(ctx, BYTES, hpc->cfg.container.workdir);
break; break;
case 601: /* machine shutdown argument */ case 601: /* machine shutdown argument */
@ -219,7 +207,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
break; break;
case 602: /* machine creation argument 0: name */ case 602: /* machine creation argument 0: name */
CBOR_PARSE_ONLY(ctx, TEXT, htx->cfg.cf.name); CBOR_PARSE_ONLY(ctx, TEXT, hpc->cfg.cf.name);
break; break;
default: default:
@ -231,9 +219,9 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
/* Bytes read completely! */ /* Bytes read completely! */
switch (htx->major_state) switch (htx->major_state)
{ {
case 3: case HCS_CMD_TELNET + 10:
hexp_get_telnet(htx->sock, htx->cfg.cf.name); hcs_get_telnet(hpc);
htx->major_state = 1; finish_cmd = true;
break; break;
case 502: case 502:
@ -253,6 +241,19 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
break; break;
} }
if (finish_cmd)
{
finish_cmd = false;
if (!cbor_parse_channel_finish(ctx, htx->stream))
CBOR_PARSER_ERROR("Failed to finish CBOR channel parser");
htx->channel = NULL;
htx->bytes_consumed = 0;
htx->major_state = 0;
continue;
}
/* End of array or map */ /* End of array or map */
while (cbor_parse_block_end(ctx)) while (cbor_parse_block_end(ctx))
{ {
@ -274,33 +275,39 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
break; break;
case 501: case 501:
if (!htx->cfg.cf.type) /*
if (!hpc->cfg.cf.type)
CBOR_PARSER_ERROR("Machine type not specified"); CBOR_PARSER_ERROR("Machine type not specified");
if (!htx->cfg.cf.name) if (!hpc->cfg.cf.name)
CBOR_PARSER_ERROR("Machine name not specified"); CBOR_PARSER_ERROR("Machine name not specified");
if (!htx->cfg.container.workdir) if (!hpc->cfg.container.workdir)
CBOR_PARSER_ERROR("Machine workdir not specified"); CBOR_PARSER_ERROR("Machine workdir not specified");
if (!htx->cfg.container.basedir) if (!hpc->cfg.container.basedir)
CBOR_PARSER_ERROR("Machine basedir not specified"); CBOR_PARSER_ERROR("Machine basedir not specified");
hypervisor_container_request( hypervisor_container_request(
htx->sock, htx->sock,
htx->cfg.cf.name, hpc->cfg.cf.name,
htx->cfg.container.basedir, hpc->cfg.container.basedir,
htx->cfg.container.workdir); hpc->cfg.container.workdir);
*/
hypervisor_container_start(&hpc->cch, &hpc->cfg.container);
htx->major_state = 1; htx->major_state = 1;
break; break;
case 601: case 601:
/*
if (!htx->cfg.cf.name) if (!htx->cfg.cf.name)
CBOR_PARSER_ERROR("Machine name not specified"); CBOR_PARSER_ERROR("Machine name not specified");
hypervisor_container_shutdown(htx->sock, htx->cfg.cf.name); hypervisor_container_shutdown(htx->sock, htx->cfg.cf.name);
*/
hypervisor_container_shutdown(&hpc->cch, &hpc->cfg.container);
htx->major_state = 1; htx->major_state = 1;
break; break;

View File

@ -5,6 +5,7 @@
#ifndef INCLUDE_FLOCK_H #ifndef INCLUDE_FLOCK_H
#define INCLUDE_FLOCK_H #define INCLUDE_FLOCK_H
#include "lib/birdlib.h" #include "lib/birdlib.h"
#include "lib/cbor.h"
#include "lib/event.h" #include "lib/event.h"
#include "lib/obstacle.h" #include "lib/obstacle.h"
#include "lib/resource.h" #include "lib/resource.h"
@ -45,9 +46,8 @@ union flock_machine_config {
} container; } container;
}; };
void hypervisor_container_start(struct cbor_channel *, struct flock_machine_container_config *);
void hypervisor_container_request(sock *s, const char *name, const char *basedir, const char *workdir); void hypervisor_container_shutdown(struct cbor_channel *, struct flock_machine_container_config *);
void hypervisor_container_shutdown(sock *s, const char *name);
int container_ctl_fd(const char *name); int container_ctl_fd(const char *name);
void hexp_cleanup_after_fork(void); void hexp_cleanup_after_fork(void);