DM: add new monitor module

This monitor module is to initialize socket intance, register
handlers to handle command from socket message, close socket,
free socket instance:
init_cmd_monitor: initialize socket intance and register handlers
to handle command.
deinit_cmd_monitor: close socket and free socket instance.

In this patch DM makefile is updated to build command monitor.

v1--v2:
	Update socket path and update log message format.
	Parse JSON format command message using libcjson lib APIs.
v2-->v3:
	Use socket path length MACRO.
	Update JSON format command message to {"command": "xxx"}.

Tracked-On: #5921

Signed-off-by: Xiangyang Wu <xiangyang.wu@intel.com>
Acked-by: Wang, Yu1 <yu1.wang@intel.com>
This commit is contained in:
Xiangyang Wu 2022-01-20 09:51:17 +08:00 committed by acrnsi-robot
parent b448567784
commit 02e94b1537
3 changed files with 131 additions and 0 deletions

View File

@ -171,6 +171,10 @@ SRCS += core/main.c
SRCS += core/hugetlb.c
SRCS += core/vrpmb.c
SRCS += core/timer.c
SRCS += core/cmd_monitor/socket.c
SRCS += core/cmd_monitor/command.c
SRCS += core/cmd_monitor/command_handler.c
SRCS += core/cmd_monitor/cmd_monitor.c
# arch
SRCS += arch/x86/pm.c

View File

@ -0,0 +1,117 @@
/*
* Copyright (C) 2022 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <cjson/cJSON.h>
#include "socket.h"
#include "command.h"
#include "vmmapi.h"
#include "cmd_monitor.h"
#include "log.h"
#include "dm.h"
#include "command_handler.h"
struct socket_dev *sock_server; /* socket server instance */
static char socket_path[UNIX_SOCKET_PATH_MAX];
static struct command *parse_command(char *cmd_msg, int fd)
{
struct command *cmd = NULL;
const cJSON *execute;
const cJSON *arguments;
cJSON *cmd_json;
cmd_json = cJSON_Parse(cmd_msg);
if (cmd_json == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
fprintf(stderr, "Error before: %s\n", error_ptr);
}
return NULL;
}
execute = cJSON_GetObjectItemCaseSensitive(cmd_json, "command");
if (cJSON_IsString(execute) && (execute->valuestring != NULL)) {
pr_info("Command name: \"%s\"\n", execute->valuestring);
cmd = find_command(execute->valuestring);
if (cmd != NULL) {
cmd->para.fd = fd;
arguments = cJSON_GetObjectItemCaseSensitive(cmd_json, "arguments");
if (cJSON_IsString(arguments) && (arguments->valuestring != NULL)) {
pr_info("Command arguments: \"%s\"\n", arguments->valuestring);
strncpy(cmd->para.option, arguments->valuestring, CMD_ARG_MAX - 1U);
}
} else {
pr_err("Command [%s] is not supported.\n", execute->valuestring);
}
}
cJSON_Delete(cmd_json);
return cmd;
}
static void monitor_cmd_dispatch(char *cmd_msg, int fd)
{
struct command *cmd;
cmd = parse_command(cmd_msg, fd);
if (cmd != NULL) {
dispatch_command_handlers(cmd);
}
return;
}
int init_socket_server(void)
{
int ret = 0;
if (strnlen(socket_path, UNIX_SOCKET_PATH_MAX) == 0) {
pr_err("Failed to initialize command monitor due to invalid socket path.\n");
}
sock_server = init_socket(socket_path);
if (sock_server == NULL)
return -1;
ret = open_socket(sock_server, monitor_cmd_dispatch);
if (ret < 0)
return ret;
return ret;
}
static void register_socket_message_handlers(struct vmctx *ctx)
{
struct handler_args arg;
arg.channel_arg = sock_server;
arg.ctx_arg = ctx;
register_command_handler(user_vm_destroy_handler, &arg, DESTROY);
register_command_handler(user_vm_blkrescan_handler, &arg, BLKRESCAN);
}
int init_cmd_monitor(struct vmctx *ctx)
{
int ret;
ret = init_socket_server();
register_socket_message_handlers(ctx);
return ret;
}
void deinit_cmd_monitor(void)
{
if (sock_server != NULL) {
close_socket(sock_server);
deinit_socket(sock_server);
}
}

View File

@ -0,0 +1,10 @@
/*
* Copyright (C) 2022 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _CMD_MONITOR_H_
#define _CMD_MONITOR_H_
int init_cmd_monitor(struct vmctx *ctx);
void deinit_cmd_monitor(void);
#endif