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

Flock: more container cbor

This commit is contained in:
Maria Matejka 2024-10-11 08:27:56 +02:00
parent d3e2fe4535
commit 2f4a08d5e8
3 changed files with 39 additions and 76 deletions

View File

@ -822,24 +822,28 @@ void
hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_container_config *ccf) 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
#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; 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)
{ FAIL(-127, "Container already exists");
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;
}
uint nlen = strlen(name); uint nlen = strlen(name);
crt = mb_allocz(hcf.p, sizeof *crt + nlen + 1); 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); cbor_put_string(cw, ccf->workdir);
} }
#undef FAIL
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
} }

View File

@ -21,12 +21,13 @@
struct hcs_parser_context { struct hcs_parser_context {
struct cbor_parser_context *ctx; struct cbor_parser_context *ctx;
struct cbor_stream *stream;
struct hcs_parser_channel *channel; struct hcs_parser_channel *channel;
sock *sock; sock *sock;
u64 bytes_consumed; u64 bytes_consumed;
u64 major_state; u64 major_state;
struct cbor_stream stream;
}; };
struct hcs_parser_channel { struct hcs_parser_channel {
@ -74,7 +75,7 @@ hcs_parser_init(sock *s)
htx->ctx = ctx; htx->ctx = ctx;
htx->sock = s; htx->sock = s;
htx->stream = cbor_stream_new(s->pool, 4); cbor_stream_init(&htx->stream, 3);
return htx; return htx;
} }
@ -94,7 +95,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
bool finish_cmd = false; bool finish_cmd = false;
for (int pos = 0; pos < size; pos++) for (int pos = 0; pos < size; pos++)
{ {// TODO here → convert to the new channel parser
if (!hpc) if (!hpc)
{ {
struct cbor_channel *cch = cbor_parse_channel(ctx, htx->stream, buf[pos]); 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; break;
case 501: case 501:
/* switch (hpc->cfg.cf.type)
if (!hpc->cfg.cf.type) {
CBOR_PARSER_ERROR("Machine type not specified"); case 1:
hypervisor_container_start(&hpc->cch, &hpc->cfg.container);
if (!hpc->cfg.cf.name) break;
CBOR_PARSER_ERROR("Machine name not specified"); default:
CBOR_PARSER_ERROR("Unknown machine type: %d", hpc->cfg.cf.type);
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);
htx->major_state = 1; htx->major_state = 1;
break; break;

View File

@ -417,49 +417,19 @@ hexp_get_telnet(sock *s, const char *name)
sk_pause_rx(s->loop, s); 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; if (hrt->name[0])
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])
{ {
/* Transferring the received listening socket to the container */ /* Transferring the received listening socket to the container */
int fd = container_ctl_fd(name); struct cbor_channel *ccc = container_get_channel(hrt->name);
/* TODO: unduplicate this code */ CBOR_REPLY(ccc, cw)
byte outbuf[128]; CBOR_PUT_MAP(cw) {
linpool *lp = lp_new(hcs_pool); cbor_put_int(cw, -2);
struct cbor_writer *cw = cbor_init(outbuf, sizeof outbuf, lp); cbor_put_null(cw);
cbor_open_block_with_length(cw, 1); ccc->stream->s->txfd = hrt->fd;
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);
int e = sendmsg(fd, &m, 0);
if (e < 0)
log(L_ERR "Failed to send socket: %m");
close(hrt->fd); close(hrt->fd);
} }