DM: adapt dm-monitor and acrnctl to use the helpers

Adapt dm-monitor and acrnctl to use the helper functions and new message
definitions in acrn_mngr.h.
These jobs must be done in one commit to avoid build problems:
1. message transmission and callback registration code are moved
 to libacrn-mngr.a, so old functions in dm-monitor could be removed to
 make code clean;
2. remove unnecessary monior_msg.h;
3. minor changes to acrnctl accordingly.

Reviewed-by: Geoffroy Van Cutsem <geoffroy.vancutsem@intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Wang, Yu <yu1.wang@intel.com>
signed-off-by: Yan Like <like.yan@intel.com>
Signed-off-by: Tao Yuhong <yuhong.tao@intel.com>
This commit is contained in:
yuhong.tao@intel.com 2018-05-30 02:16:56 +08:00 committed by lijinxia
parent 53ecd932ad
commit eada59c934
7 changed files with 48 additions and 553 deletions

View File

@ -9,6 +9,7 @@ ROOT_OUT := $(shell mkdir -p $(O);cd $(O);pwd)
HV_OUT := $(ROOT_OUT)/hypervisor
DM_OUT := $(ROOT_OUT)/devicemodel
TOOLS_OUT := $(ROOT_OUT)/tools
export TOOLS_OUT
.PHONY: all hypervisor devicemodel tools
all: hypervisor devicemodel tools
@ -22,7 +23,7 @@ sbl-hypervisor:
make -C $(T)/hypervisor HV_OBJDIR=$(HV_OUT)-sbl PLATFORM=sbl RELEASE=$(RELEASE) clean
make -C $(T)/hypervisor HV_OBJDIR=$(HV_OUT)-sbl PLATFORM=sbl RELEASE=$(RELEASE)
devicemodel:
devicemodel: tools
make -C $(T)/devicemodel DM_OBJDIR=$(DM_OUT) clean
make -C $(T)/devicemodel DM_OBJDIR=$(DM_OUT)

View File

@ -20,6 +20,7 @@ CFLAGS += -Wformat -Wformat-security -fno-strict-aliasing
CFLAGS += -I$(BASEDIR)/include
CFLAGS += -I$(BASEDIR)/include/public
CFLAGS += -I$(TOOLS_OUT)
GCC_MAJOR=$(shell echo __GNUC__ | $(CC) -E -x c - | tail -n 1)
GCC_MINOR=$(shell echo __GNUC_MINOR__ | $(CC) -E -x c - | tail -n 1)
@ -41,6 +42,7 @@ endif
LDFLAGS += -Wl,-z,noexecstack
LDFLAGS += -Wl,-z,relro,-z,now
LDFLAGS += -L$(TOOLS_OUT)
LIBS = -lrt
LIBS += -lpthread
@ -49,6 +51,7 @@ LIBS += -lpciaccess
LIBS += -lz
LIBS += -luuid
LIBS += -lusb-1.0
LIBS += -lacrn-mngr
# hw
SRCS += hw/block_if.c

View File

@ -35,439 +35,72 @@
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include "dm.h"
#include "vmmapi.h"
#include "mevent.h"
#include "monitor.h"
#include "acrn_mngr.h"
/* Data structure and functions for processing received messages */
struct monitor_msg_handle {
struct vmm_msg msg;
void (*callback) (struct vmm_msg * msg, struct msg_sender * sender,
void *priv);
void *priv;
LIST_ENTRY(monitor_msg_handle) list;
};
static LIST_HEAD(mmh_list_struct, monitor_msg_handle) mmh_head;
static pthread_mutex_t mmh_mutex = PTHREAD_MUTEX_INITIALIZER;
static int can_register_handler = 0; /* Do not allow anyone add his handler,
untill we have added some researved ones */
static int monitor_add_handler(struct monitor_msg_handle *handle)
/* helpers */
/* Check if @path is a directory, and create if not exist */
static int check_dir(const char *path)
{
struct monitor_msg_handle *hp;
struct stat st;
pthread_mutex_lock(&mmh_mutex);
LIST_FOREACH(hp, &mmh_head, list)
if (hp->msg.msgid == handle->msg.msgid) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
pthread_mutex_unlock(&mmh_mutex);
return -1;
}
LIST_INSERT_HEAD(&mmh_head, handle, list);
pthread_mutex_unlock(&mmh_mutex);
return 0;
}
int monitor_register_handler(struct vmm_msg *msg,
void (*callback) (struct vmm_msg * msg,
struct msg_sender * client,
void *priv), void *priv)
{
struct monitor_msg_handle *handle;
int ret;
if (!can_register_handler)
return -1;
handle = calloc(1, sizeof(struct monitor_msg_handle));
if (!handle) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
return -1;
}
handle->msg.msgid = msg->msgid;
handle->callback = callback;
handle->priv = priv;
ret = monitor_add_handler(handle);
if (ret)
free(handle);
return ret;
}
/* messages handled by monitor */
static int write_msg_to(int fd, void *data, unsigned long timeout_usec)
{
struct vmm_msg *msg = data;
fd_set wfd;
struct timeval timeout;
int ret = 0;
if (msg->len < sizeof(struct vmm_msg)) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
return -1;
}
if (msg->msgid > MSGID_MAX) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
return -1;
}
if (msg->magic != VMM_MSG_MAGIC) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
msg->magic = VMM_MSG_MAGIC;
}
msg->timestamp = time(NULL);
FD_ZERO(&wfd);
FD_SET(fd, &wfd);
timeout.tv_sec = 0;
timeout.tv_usec = timeout_usec;
select(fd + 1, NULL, &wfd, NULL, &timeout);
if (FD_ISSET(fd, &wfd))
ret = write(fd, msg, msg->len);
return ret;
}
/* MSG_HANDSHAKE, handshake message handler*/
#define TIMEOUT_USEC 100000
static VMM_MSG_STR(handshake_badname, "Error: bad name!");
static VMM_MSG_STR(handshake_ok, "acrn-dm read you request");
static void handshake_acrn_dm(struct vmm_msg *msg, struct msg_sender *sender,
void *priv)
{
struct vmm_msg_handshake *hsk = (void *)msg;
int ret;
ret = strnlen(hsk->name, CLIENT_NAME_LEN);
if (ret >= CLIENT_NAME_LEN) {
write_msg_to(sender->fd, &handshake_badname, TIMEOUT_USEC);
return;
}
strncpy(sender->name, hsk->name, CLIENT_NAME_LEN);
sender->broadcast = hsk->broadcast;
write_msg_to(sender->fd, &handshake_ok, TIMEOUT_USEC);
}
static struct monitor_msg_handle handle_handshake = {
.msg = {.msgid = MSG_HANDSHAKE},
.callback = handshake_acrn_dm,
};
/* vm manager can comunicate with dm-monitor, use unix socket,
* the monitor is the server, and there may have many clients,
* a client send a message, trigger right msg handler. And msg handler
* should only reply to message sender.
*/
static struct sockaddr_un monitor_addr; /* one monitor */
static int monitor_fd;
struct vmm_client {
/* msg_sender will be seen/modify by msg handler */
struct msg_sender sender;
/* the rest should be invisible for msg_handler */
struct sockaddr_un addr;
int fd;
socklen_t addr_len;
void *buf;
int len; /* buf len */
struct mevent *mev;
LIST_ENTRY(vmm_client) list;
};
static LIST_HEAD(client_list_struct, vmm_client) client_head;
static int num_client = 0;
static pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
static void vmm_client_free_res(struct vmm_client *client)
{
mevent_delete(client->mev);
close(client->fd);
client->fd = -1;
free(client->buf);
client->buf = NULL;
free(client);
}
static void vmm_client_free(struct vmm_client *client)
{
pthread_mutex_lock(&client_mutex);
LIST_REMOVE(client, list);
num_client--;
pthread_mutex_unlock(&client_mutex);
vmm_client_free_res(client);
}
static VMM_MSG_STR(unsupported_msgid, "Error: unsupported msgid!");
static int monitor_parse_buf(struct vmm_client *client)
{
struct vmm_msg *msg;
struct monitor_msg_handle *handle;
size_t p = 0;
int handled = 0;
if (client->len < sizeof(struct vmm_msg))
return -1;
do {
msg = client->buf + p;
/* do we out-of-bounary? */
if (p + msg->len > client->len) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
break;
if (stat(path, &st)) {
if (mkdir(path, 0666)) {
perror(path);
return -1;
}
return 0;
}
LIST_FOREACH(handle, &mmh_head, list) {
if (msg->magic != VMM_MSG_MAGIC)
return -1;
if (handle->msg.msgid != msg->msgid)
continue;
client->sender.fd = client->fd;
handle->callback(msg, &client->sender, handle->priv);
handled = 1;
break;
}
p += msg->len;
} while (p < client->len);
if (S_ISDIR(st.st_mode))
return 0;
if (!handled)
write_msg_to(client->fd, &unsupported_msgid, TIMEOUT_USEC);
return 0;
fprintf(stderr, "%s exist, and not a directory!\n", path);
return -1;
}
static void mevent_read_func(int fd, enum ev_type type, void *param)
{
struct vmm_client *client = param;
client->len = read(fd, client->buf, VMM_MSG_MAX_LEN);
if (client->len <= 0) {
fprintf(stderr, "Disconnect(%d)!\r\n", client->fd);
vmm_client_free(client);
return;
}
if (client->len == VMM_MSG_MAX_LEN) {
fprintf(stderr, "TODO: buf overflow!\r\n");
return;
}
monitor_parse_buf(client);
}
static struct vmm_client *vmm_client_new(void)
{
struct vmm_client *client;
client = calloc(1, sizeof(struct vmm_client));
if (!client) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto alloc_client;
}
memset(client, 0, sizeof(struct vmm_client));
client->buf = calloc(1, VMM_MSG_MAX_LEN);
if (!client->buf) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto alloc_buf;
}
client->addr_len = sizeof(client->addr);
client->fd =
accept(monitor_fd, (struct sockaddr *)&client->addr, &client->addr_len);
if (client->fd < 0) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto accept_con;
}
client->mev =
mevent_add(client->fd, EVF_READ, mevent_read_func, client);
if (!client->mev) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto add_mev;
}
pthread_mutex_lock(&client_mutex);
LIST_INSERT_HEAD(&client_head, client, list);
num_client++;
pthread_mutex_unlock(&client_mutex);
return client;
add_mev:
close(client->fd);
client->fd = -1;
accept_con:
free(client->buf);
client->buf = NULL;
alloc_buf:
free(client);
alloc_client:
return NULL;
}
int monitor_broadcast(struct vmm_msg *msg)
{
struct vmm_client *client;
fd_set wfd;
int max_fd = 0;
struct timeval timeout;
int ret = 0;
if (msg->len < sizeof(struct vmm_msg)) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
return -1;
}
if (msg->msgid > MSGID_MAX) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
return -1;
}
if (msg->magic != VMM_MSG_MAGIC) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
msg->magic = VMM_MSG_MAGIC;
}
msg->timestamp = time(NULL);
pthread_mutex_lock(&client_mutex);
FD_ZERO(&wfd);
LIST_FOREACH(client, &client_head, list) {
if (!client->sender.broadcast)
continue;
FD_SET(client->fd, &wfd);
if (client->fd > max_fd)
max_fd = client->fd;
}
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
select(max_fd + 1, NULL, &wfd, NULL, &timeout);
LIST_FOREACH(client, &client_head, list) {
if (!client->sender.broadcast)
continue;
if (FD_ISSET(client->fd, &wfd)) {
ret = write(client->fd, msg->payload,
msg->len - sizeof(struct vmm_msg));
if (ret < 0)
continue;
}
}
pthread_mutex_unlock(&client_mutex);
return 0;
}
/* monitor thread */
static int monitor_running = 1;
static pthread_t monitor_thread;
static void *monitor_server_func(void *arg)
{
struct vmm_client *client;
while (monitor_running) {
client = vmm_client_new();
if (!client) {
usleep(10000);
continue;
}
fprintf(stderr, "Connected:%d\r\n", client->fd);
}
fprintf(stderr, "%s quit!\r\n", __FUNCTION__);
return NULL;
}
static int monitor_fd = -1;
int monitor_init(struct vmctx *ctx)
{
int ret;
char path[128] = { };
char path[128] = {};
ret = system("mkdir -p /run/acrn/");
ret = check_dir("/run/acrn/");
if (ret) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto socket_err;
goto dir_err;
}
memset(&monitor_addr, 0, sizeof(monitor_addr));
snprintf(path, sizeof(path), "/run/acrn/%s-monitor.socket", vmname);
unlink(path);
monitor_fd = socket(AF_UNIX, SOCK_STREAM, 0);
ret = check_dir("/run/acrn/mngr");
if (ret) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto dir_err;
}
snprintf(path, sizeof(path) - 1, "%s.monitor", vmname);
monitor_fd = mngr_open_un(path, MNGR_SERVER);
if (monitor_fd < 0) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto socket_err;
goto server_err;
}
monitor_addr.sun_family = AF_UNIX;
strncpy(monitor_addr.sun_path, path, sizeof(monitor_addr.sun_path));
ret = bind(monitor_fd, (struct sockaddr *)&monitor_addr, sizeof(monitor_addr));
if (ret < 0) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto bind_err;
}
listen(monitor_fd, 1);
ret = pthread_create(&monitor_thread, NULL, monitor_server_func, NULL);
if (ret) {
fprintf(stderr, "%s %d\r\n", __FUNCTION__, __LINE__);
goto thread_err;
}
/* Messages handled by monitor */
monitor_add_handler(&handle_handshake);
__sync_fetch_and_add(&can_register_handler, 1);
return 0;
thread_err:
monitor_thread = 0;
unlink(path);
bind_err:
close(monitor_fd);
socket_err:
server_err:
dir_err:
return -1;
}
void monitor_close(void)
{
struct vmm_client *client, *pclient;
if (!monitor_thread)
return;
shutdown(monitor_fd, SHUT_RDWR);
close(monitor_fd);
monitor_running = 0;
pthread_join(monitor_thread, NULL);
unlink(monitor_addr.sun_path);
/* client buf-mem and fd may be still in use by msg-handler */
/* which is handled by mevent */
pthread_mutex_lock(&client_mutex);
list_foreach_safe(client, &client_head, list, pclient) {
LIST_REMOVE(client, list);
vmm_client_free_res(client);
}
pthread_mutex_unlock(&client_mutex);
if (monitor_fd >= 0)
mngr_close(monitor_fd);
}

View File

@ -40,43 +40,7 @@
#ifndef MONITOR_H
#define MONITOR_H
#include "monitor_msg.h"
int monitor_init(struct vmctx *ctx);
void monitor_close(void);
/**
* monitor broadcast()
* Developer can use monitor_broadcast() inside acrn-dm, send vmm_msg to all client.
* @arguements:
* @msg: any valid vmm_msg data structure, which you want send
*/
int monitor_broadcast(struct vmm_msg *msg);
/* msg_sender will be seen/modify by msg handler */
struct msg_sender {
int fd; /* msg handler can replay to this fd */
char name[CLIENT_NAME_LEN]; /* client have a chance to name itsself */
int broadcast;
};
/**
* monitor_register_handler()
* Developer can add vmm_msg handler inside acrn-dm, that means, when a client send a
* vmm_msg to acrn-dm, the correspoding callback will be called
* @arguements:
* @msg: msg->msgid must be set, for which handler will be add.
* @callback: when a received message match msg->msgid, callback will be envoked.
* And these data are pass in to help developer: (a)msg, the received message, from
* socket. (b)sender, tell you who send this message, anything wite to sender->fd
* will be able to read out by client socket. Developer should only write valid
* vmm_msg. (c)priv, that is what you pass to monitor_add_msg_handler();
* @priv, callback will see this value.
*/
int monitor_register_handler(struct vmm_msg *msg,
void (*callback) (struct vmm_msg * msg,
struct msg_sender * sender,
void *priv), void *priv);
#endif

View File

@ -1,107 +0,0 @@
/*
* Project Acrn
* Acrn-dm-monitor
*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* Author: TaoYuhong <yuhong.tao@intel.com>
*/
/* this file will be shared, by anyone who want talk to acrn-dm */
#ifndef MONITOR_MSG_H
#define MONITOR_MSG_H
enum msgid {
REQ_STARTALL = 0, /* SOS lifecycle service -> VM Mngr */
REQ_PAUSEALL, /* SOS lifecycle service -> VM Mngr */
REQ_START, /* VM Mngr -> ACRN-DM(vm) */
REQ_STOP, /* VM Mngr -> ACRN-DM(vm) */
REQ_PAUSE,
REQ_RESUME, /* VM Mngr -> ACRN-DM(vm) */
REQ_RESET,
REQ_QUERY,
NTF_ALLSTOPPED, /* VM Mngr -> SOS lifecycle service */
NTF_ALLPAUSED, /* VM Mngr -> SOS lifecycle service */
NTF_STARTED, /* ACRN-DM -> VM Mngr */
NTF_STOPPED, /* ACRN-DM -> VM Mngr */
NTF_PAUSED, /* ACRN-DM -> VM Mngr */
NTF_RESUMED, /* ACRN-DM -> VM Mngr */
MSG_STR,
MSG_HANDSHAKE, /* handshake */
MSGID_MAX
};
#define VMM_MSG_MAGIC 0x67736d206d6d76 /* that is char[8] "vmm msg", on X86 */
#define VMM_MSG_MAX_LEN 4096
struct vmm_msg {
unsigned long long magic; /* Make sure you get a vmm_msg */
unsigned int msgid;
unsigned long timestamp;
size_t len; /* vmm_msg + payload size */
char payload[0];
};
/* practical messages, and helpers
* each message defined in msgid should have its own data structure,
* and shared by acrn-dm and vm-mngr. So that such that message data
* structure can be recognized by both sides of the communication
*/
/* For test, generate a message who carry a string
* eg., VMM_MSG_STR(hello_msg, "Hello\n") will create hello_msg,
* then you can write(sock_fd, hello_msg, sizeof(hello_msg))
*/
#define VMM_MSG_STR(var, str) \
struct vmm_msg_##var { \
struct vmm_msg vmsg; \
char raw[sizeof(str)]; \
} var = { \
.vmsg = { \
.magic = VMM_MSG_MAGIC, \
.msgid = MSG_STR, \
.len = sizeof(struct vmm_msg_##var), \
}, \
.raw = str, \
}
#define CLIENT_NAME_LEN 16
struct vmm_msg_handshake {
struct vmm_msg vmsg;
char name[CLIENT_NAME_LEN]; /* name should be a string, end with '\0' */
/* can be "acrnd", "vm-mngr" or "acrnctl" */
int broadcast; /* if set, allow acrn-dm send broadcast */
/* message to such client */
};
#endif

View File

@ -6,6 +6,7 @@ all: $(OUT_DIR)/libacrn-mngr.a $(OUT_DIR)/acrnctl
$(OUT_DIR)/libacrn-mngr.a: acrn_mngr.c acrn_mngr.h
$(CC) -c acrn_mngr.c -DMNGR_DEBUG -I../../devicemodel/include -Wall -g
ar -cr $@ acrn_mngr.o
cp ./acrn_mngr.h $(OUT_DIR)/
$(OUT_DIR)/acrnctl: acrnctl.c
$(CC) -o $(OUT_DIR)/acrnctl acrnctl.c -I../../devicemodel/include -Wall -g

View File

@ -42,7 +42,7 @@
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "monitor_msg.h"
#include "acrn_mngr.h"
#define ACRNCTL_OPT_ROOT "/opt/acrn/conf"
#define MAX_NAME_LEN (128)
@ -70,7 +70,7 @@ static int shell_cmd(const char *cmd, char *outbuf, int len)
return ret;
}
static void process_msg(struct vmm_msg *msg)
static void process_msg(struct mngr_msg *msg)
{
if (msg->len < sizeof(*msg))
return;
@ -508,7 +508,7 @@ static int send_stop_msg(char *vmname)
{
int fd, ret;
struct sockaddr_un addr;
struct vmm_msg msg;
struct mngr_msg msg;
struct timeval timeout;
fd_set rfd, wfd;
char buf[128];
@ -530,8 +530,8 @@ static int send_stop_msg(char *vmname)
goto connect_err;
}
msg.magic = VMM_MSG_MAGIC;
msg.msgid = REQ_STOP;
msg.magic = MNGR_MSG_MAGIC;
msg.msgid = DM_STOP;
msg.len = sizeof(msg);
timeout.tv_sec = 1; /* wait 1 second for read/write socket */