2024-09-09 12:14:49 +00:00
|
|
|
#include "lib/birdlib.h"
|
2024-09-10 10:04:44 +00:00
|
|
|
#include "lib/cbor.h"
|
2024-09-09 12:14:49 +00:00
|
|
|
#include "lib/string.h"
|
2024-09-10 09:16:49 +00:00
|
|
|
#include "lib/io-loop.h"
|
2024-09-09 12:14:49 +00:00
|
|
|
|
|
|
|
#include "flock/flock.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hand-written parser for a very simple CBOR protocol:
|
|
|
|
*
|
2024-10-07 13:05:51 +00:00
|
|
|
* - on toplevel always array of three elements:
|
|
|
|
* - the ID (u64)
|
|
|
|
* - the command saying what to expect in the third element
|
|
|
|
* - 0 with NULL (7-22) = shutdown the hypervisor
|
|
|
|
* - 1 with NULL = open a telnet listener
|
|
|
|
* - 2 with one string = create a machine of this name
|
|
|
|
* - 3 with array of strings = run the given command inside the hypervisor
|
2024-09-09 12:14:49 +00:00
|
|
|
*/
|
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
struct hcs_parser_stream {
|
2024-10-03 21:02:58 +00:00
|
|
|
struct cbor_parser_context *ctx;
|
2024-10-08 20:02:48 +00:00
|
|
|
struct hcs_parser_channel *channel;
|
2024-09-10 10:04:44 +00:00
|
|
|
sock *sock;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
|
|
|
u64 bytes_consumed;
|
|
|
|
u64 major_state;
|
2024-10-11 06:27:56 +00:00
|
|
|
|
2024-10-11 19:18:25 +00:00
|
|
|
CBOR_STREAM_EMBED(stream, 4);
|
2024-10-07 13:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hcs_parser_channel {
|
2024-10-11 19:18:25 +00:00
|
|
|
CBOR_CHANNEL_EMBED(cch, 4);
|
2024-10-11 18:27:43 +00:00
|
|
|
struct hcs_parser_stream *htx;
|
2024-10-07 13:05:51 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
HCS_CMD_SHUTDOWN = 1,
|
|
|
|
HCS_CMD_TELNET,
|
|
|
|
HCS_CMD_MACHINE_START,
|
|
|
|
HCS_CMD_MACHINE_STOP,
|
|
|
|
HCS_CMD__MAX,
|
|
|
|
} cmd;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-09-14 20:34:09 +00:00
|
|
|
union flock_machine_config cfg;
|
2024-09-09 12:14:49 +00:00
|
|
|
};
|
|
|
|
|
2024-10-07 13:05:51 +00:00
|
|
|
static void
|
2024-10-08 20:02:48 +00:00
|
|
|
hcs_request_poweroff(struct hcs_parser_channel *hpc)
|
2024-10-07 13:05:51 +00:00
|
|
|
{
|
|
|
|
log(L_INFO "Requested shutdown via CLI");
|
|
|
|
ev_send_loop(&main_birdloop, &poweroff_event);
|
|
|
|
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_REPLY(&hpc->cch, cw)
|
|
|
|
CBOR_PUT_MAP(cw)
|
|
|
|
{
|
|
|
|
cbor_put_int(cw, -1);
|
|
|
|
cbor_put_string(cw, "OK");
|
|
|
|
}
|
2024-10-07 16:57:09 +00:00
|
|
|
|
2024-10-11 19:18:25 +00:00
|
|
|
cbor_channel_done(&hpc->cch);
|
2024-10-07 13:05:51 +00:00
|
|
|
}
|
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
struct hcs_parser_stream *
|
2024-09-10 10:04:44 +00:00
|
|
|
hcs_parser_init(sock *s)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-11 18:27:43 +00:00
|
|
|
struct hcs_parser_stream *htx = mb_allocz(s->pool, sizeof *htx);
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-11 19:18:25 +00:00
|
|
|
CBOR_STREAM_INIT(htx, stream, cch, s->pool, struct hcs_parser_channel);
|
|
|
|
cbor_stream_attach(&htx->stream, s);
|
|
|
|
htx->stream.parse = hcs_parse;
|
|
|
|
htx->stream.cancel = hcs_parser_cleanup;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
return htx;
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
#define CBOR_PARSER_ERROR(...) do { \
|
|
|
|
log(L_ERR "Hypervisor ctl parse: " __VA_ARGS__); \
|
|
|
|
return CPR_ERROR; \
|
2024-10-03 21:02:58 +00:00
|
|
|
} while (0)
|
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
enum cbor_parse_result
|
|
|
|
hcs_parse(struct cbor_channel *cch, enum cbor_parse_result res)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-11 18:27:43 +00:00
|
|
|
SKIP_BACK_DECLARE(struct hcs_parser_channel, hpc, cch, cch);
|
|
|
|
SKIP_BACK_DECLARE(struct hcs_parser_stream, htx, stream, cch->stream);
|
|
|
|
struct cbor_parser_context *ctx = &htx->stream.parser;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
switch (res)
|
|
|
|
{
|
2024-10-03 21:02:58 +00:00
|
|
|
case CPR_MAJOR:
|
2024-09-09 12:14:49 +00:00
|
|
|
/* Check type acceptance */
|
2024-10-03 21:02:58 +00:00
|
|
|
switch (htx->major_state)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-08 20:02:48 +00:00
|
|
|
case 0: /* Command */
|
|
|
|
CBOR_PARSE_ONLY(ctx, POSINT, hpc->cmd);
|
|
|
|
if (hpc->cmd > HCS_CMD__MAX)
|
|
|
|
CBOR_PARSER_ERROR("Command key too high, got %lu", hpc->cmd);
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-08 20:02:48 +00:00
|
|
|
htx->major_state = hpc->cmd + 10;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-07 13:05:51 +00:00
|
|
|
case HCS_CMD_SHUTDOWN + 10: /* shutdown command: expected null */
|
2024-09-09 12:14:49 +00:00
|
|
|
if ((ctx->type != 7) || (ctx->value != 22))
|
|
|
|
CBOR_PARSER_ERROR("Expected null, got %u-%u", ctx->type, ctx->value);
|
|
|
|
|
2024-10-08 20:02:48 +00:00
|
|
|
hcs_request_poweroff(hpc);
|
2024-10-11 18:27:43 +00:00
|
|
|
htx->major_state = 3;
|
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-07 13:05:51 +00:00
|
|
|
case HCS_CMD_TELNET + 10: /* telnet listener open */
|
2024-09-27 19:38:56 +00:00
|
|
|
if ((ctx->type == 7) && (ctx->value == 22))
|
|
|
|
{
|
2024-10-11 18:27:43 +00:00
|
|
|
hexp_get_telnet(hpc);
|
|
|
|
htx->major_state = 3;
|
|
|
|
return CPR_MORE;
|
2024-09-27 19:38:56 +00:00
|
|
|
}
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-08 20:02:48 +00:00
|
|
|
else CBOR_PARSE_IF(ctx, TEXT, hpc->cfg.cf.name)
|
2024-10-05 13:47:49 +00:00
|
|
|
;
|
|
|
|
else
|
|
|
|
CBOR_PARSER_ERROR("Expected null or string, got %s", cbor_type_str(ctx->type));
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-07 13:05:51 +00:00
|
|
|
case HCS_CMD_MACHINE_START + 10: /* machine creation request */
|
2024-09-14 20:34:09 +00:00
|
|
|
if (ctx->type != 5)
|
|
|
|
CBOR_PARSER_ERROR("Expected mapping, got %u", ctx->type);
|
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 501;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-10-07 13:05:51 +00:00
|
|
|
case HCS_CMD_MACHINE_STOP + 1: /* machine shutdown request */
|
2024-09-27 18:09:51 +00:00
|
|
|
if (ctx->type != 5)
|
|
|
|
CBOR_PARSER_ERROR("Expecting mapping, got %u", ctx->type);
|
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 601;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-27 18:09:51 +00:00
|
|
|
|
|
|
|
case 7: /* process spawner */
|
2024-10-04 13:44:41 +00:00
|
|
|
bug("process spawner not implemented");
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-09-14 20:34:09 +00:00
|
|
|
case 501: /* machine creation argument */
|
2024-10-05 13:47:49 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, POSINT, htx->major_state);
|
2024-09-14 20:34:09 +00:00
|
|
|
|
|
|
|
if (ctx->value >= 5)
|
|
|
|
CBOR_PARSER_ERROR("Command key too high, got %lu", ctx->value);
|
|
|
|
|
2024-10-05 13:47:49 +00:00
|
|
|
htx->major_state += 502;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
|
|
|
case 502: /* machine creation argument 0: name */
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, TEXT, hpc->cfg.cf.name);
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
|
|
|
case 503: /* machine creation argument 1: type */
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, POSINT, hpc->cfg.cf.type);
|
2024-09-14 20:34:09 +00:00
|
|
|
|
|
|
|
if ((ctx->value < 1) && (ctx->value > 1) )
|
|
|
|
CBOR_PARSER_ERROR("Unexpected type, got %lu", ctx->value);
|
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 501;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-10-05 10:01:34 +00:00
|
|
|
case 504: /* machine creation argument 2: basedir */
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, BYTES, hpc->cfg.container.basedir);
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-10-05 10:01:34 +00:00
|
|
|
case 505: /* machine creation argument 3: workdir */
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, BYTES, hpc->cfg.container.workdir);
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-09-27 18:09:51 +00:00
|
|
|
case 601: /* machine shutdown argument */
|
2024-10-05 13:47:49 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, POSINT, htx->major_state);
|
2024-09-27 18:09:51 +00:00
|
|
|
|
2024-10-05 13:47:49 +00:00
|
|
|
if (ctx->value >= 5)
|
2024-09-27 18:09:51 +00:00
|
|
|
CBOR_PARSER_ERROR("Command key too high, got %lu", ctx->value);
|
|
|
|
|
2024-10-05 13:47:49 +00:00
|
|
|
htx->major_state += 602;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-27 18:09:51 +00:00
|
|
|
|
|
|
|
case 602: /* machine creation argument 0: name */
|
2024-10-08 20:02:48 +00:00
|
|
|
CBOR_PARSE_ONLY(ctx, TEXT, hpc->cfg.cf.name);
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-27 18:09:51 +00:00
|
|
|
|
2024-09-09 12:14:49 +00:00
|
|
|
default:
|
|
|
|
bug("invalid parser state");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
case CPR_STR_END:
|
|
|
|
/* Bytes read completely! */
|
|
|
|
switch (htx->major_state)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-08 20:02:48 +00:00
|
|
|
case HCS_CMD_TELNET + 10:
|
2024-10-11 18:27:43 +00:00
|
|
|
hexp_get_telnet(hpc);
|
2024-09-27 19:38:56 +00:00
|
|
|
break;
|
|
|
|
|
2024-09-14 20:34:09 +00:00
|
|
|
case 502:
|
|
|
|
case 504:
|
|
|
|
case 505:
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 501;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-09-27 18:09:51 +00:00
|
|
|
case 602:
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 601;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-27 18:09:51 +00:00
|
|
|
|
2024-09-09 12:14:49 +00:00
|
|
|
default:
|
|
|
|
bug("Unexpected state to end a (byte)string in");
|
|
|
|
/* Code to run at the end of a (byte)string */
|
|
|
|
}
|
2024-10-03 21:02:58 +00:00
|
|
|
break;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
case CPR_BLOCK_END:
|
2024-10-03 21:02:58 +00:00
|
|
|
switch (htx->major_state)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
|
|
|
/* Code to run at the end of the mapping */
|
|
|
|
case 0: /* toplevel item ended */
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = ~0ULL;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_BLOCK_END;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
|
|
|
case 1:
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 0;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
|
|
|
case 5:
|
|
|
|
/* Finalize the command to exec in hypervisor */
|
|
|
|
CBOR_PARSER_ERROR("NOT IMPLEMENTED YET");
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 1;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
|
2024-09-14 20:34:09 +00:00
|
|
|
case 501:
|
2024-10-11 06:27:56 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 1;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-14 20:34:09 +00:00
|
|
|
|
2024-09-27 18:09:51 +00:00
|
|
|
case 601:
|
2024-10-08 20:02:48 +00:00
|
|
|
/*
|
2024-10-03 21:02:58 +00:00
|
|
|
if (!htx->cfg.cf.name)
|
2024-09-27 18:09:51 +00:00
|
|
|
CBOR_PARSER_ERROR("Machine name not specified");
|
|
|
|
|
2024-10-03 21:02:58 +00:00
|
|
|
hypervisor_container_shutdown(htx->sock, htx->cfg.cf.name);
|
2024-10-08 20:02:48 +00:00
|
|
|
*/
|
2024-09-27 18:09:51 +00:00
|
|
|
|
2024-10-08 20:02:48 +00:00
|
|
|
hypervisor_container_shutdown(&hpc->cch, &hpc->cfg.container);
|
2024-10-03 21:02:58 +00:00
|
|
|
htx->major_state = 1;
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-27 18:09:51 +00:00
|
|
|
|
2024-09-09 12:14:49 +00:00
|
|
|
default:
|
|
|
|
bug("Unexpected state to end a mapping in");
|
|
|
|
}
|
2024-10-11 18:27:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CPR_ERROR:
|
|
|
|
case CPR_MORE:
|
|
|
|
CBOR_PARSER_ERROR("Invalid input");
|
|
|
|
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-11 18:27:43 +00:00
|
|
|
return CPR_MORE;
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2024-10-11 18:27:43 +00:00
|
|
|
hcs_complete(struct hcs_parser_stream *htx)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-03 21:02:58 +00:00
|
|
|
return htx->major_state == ~0ULL;
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2024-10-11 18:27:43 +00:00
|
|
|
hcs_error(struct hcs_parser_stream *htx)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-03 21:02:58 +00:00
|
|
|
return htx->ctx->error;
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-10-11 18:27:43 +00:00
|
|
|
hcs_parser_cleanup(struct hcs_parser_stream *htx)
|
2024-09-09 12:14:49 +00:00
|
|
|
{
|
2024-10-03 21:02:58 +00:00
|
|
|
cbor_parser_free(htx->ctx);
|
2024-09-09 12:14:49 +00:00
|
|
|
}
|