Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tools/hvisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ static void __attribute__((noreturn)) help(int exit_status) {
printf(" zone start <config.json> Initialize an isolation zone\n");
printf(" zone shutdown -id <zone_id> Terminate a zone by ID\n");
printf(" zone list List all active zones\n");
printf(" virtio start <virtio.json> Activate virtio devices\n\n");
printf(" virtio start <virtio.json> Activate virtio devices\n");
printf(" virtio add <virtio.json> Add virtio devices to daemon\n\n");
printf("Options:\n");
printf(" --id <zone_id> Specify zone ID for shutdown\n");
printf(" --help Show this help message\n\n");
Expand Down Expand Up @@ -1128,6 +1129,8 @@ int main(int argc, char *argv[]) {

if (strcmp(argv[2], "start") == 0) {
err = virtio_start(argc, argv);
} else if (strcmp(argv[2], "add") == 0) {
err = virtio_add(argc, argv);
} else {
help(1);
}
Expand Down
17 changes: 14 additions & 3 deletions tools/virtio/devices/blk/virtio_blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -337,6 +338,7 @@ static BlkDev *init_blk_dev(VirtIODevice *vdev) {
vdev->dev = NULL;
return NULL;
}
dev->mutex_initialized = true;

if (pthread_cond_init(&dev->cond, NULL) != 0) {
log_error("failed to init blk cond");
Expand All @@ -345,6 +347,7 @@ static BlkDev *init_blk_dev(VirtIODevice *vdev) {
vdev->dev = NULL;
return NULL;
}
dev->cond_initialized = true;

return dev;
}
Expand Down Expand Up @@ -464,11 +467,15 @@ static void virtio_blk_close(VirtIODevice *vdev) {
pthread_cond_signal(&dev->cond);
pthread_mutex_unlock(&dev->mtx);
pthread_join(dev->tid, NULL);
dev->thread_started = false;
}
pthread_mutex_destroy(&dev->mtx);
pthread_cond_destroy(&dev->cond);
if (dev->cond_initialized)
pthread_cond_destroy(&dev->cond);
if (dev->mutex_initialized)
pthread_mutex_destroy(&dev->mtx);
if (dev->img_fd >= 0)
close(dev->img_fd);
dev->img_fd = -1;
free(dev);
vdev->dev = NULL;
}
Expand Down Expand Up @@ -512,7 +519,11 @@ static int virtio_blk_parse_params(const cJSON *json, void **out) {
free(p);
return -EINVAL;
}
p->img_path = img->valuestring;
if (strlen(img->valuestring) >= sizeof(p->img_path)) {
free(p);
return -ENAMETOOLONG;
}
memcpy(p->img_path, img->valuestring, strlen(img->valuestring) + 1);
*out = p;
return 0;
}
Expand Down
12 changes: 11 additions & 1 deletion tools/virtio/devices/console/virtio_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ static uint8_t trashbuf[1024];

static ConsoleDev *init_console_dev() {
ConsoleDev *dev = (ConsoleDev *)malloc(sizeof(ConsoleDev));
if (!dev)
return NULL;
dev->config.cols = 80;
dev->config.rows = 25;
dev->master_fd = -1;
Expand Down Expand Up @@ -96,25 +98,33 @@ static int virtio_console_init(VirtIODevice *vdev) {
master_fd = posix_openpt(O_RDWR | O_NOCTTY);
if (master_fd < 0) {
log_error("Failed to open master pty, errno is %d", errno);
return -1;
}
if (grantpt(master_fd) < 0) {
log_error("Failed to grant pty, errno is %d", errno);
close(master_fd);
return -1;
}
if (unlockpt(master_fd) < 0) {
log_error("Failed to unlock pty, errno is %d", errno);
close(master_fd);
return -1;
}
dev->master_fd = master_fd;

slave_name = ptsname(master_fd);
if (slave_name == NULL) {
log_error("Failed to get slave name, errno is %d", errno);
close(master_fd);
return -1;
}
log_info("char device redirected to %s", slave_name);
// Open and keep a slave fd in this process. Without a slave peer,
// master poll/epoll keeps reporting EPOLLHUP and can cause busy loops.
slave_fd = open(slave_name, O_RDWR);
if (slave_fd < 0) {
log_error("Failed to open slave pty, errno is %d", errno);
close(master_fd);
return -1;
}

Expand Down Expand Up @@ -247,4 +257,4 @@ static void virtio_console_free_params(void *params) { (void)params; }
const struct virtio_config_ops virtio_console_config_ops = {
.parse = virtio_console_parse_params,
.free = virtio_console_free_params,
};
};
18 changes: 14 additions & 4 deletions tools/virtio/devices/net/virtio_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <unistd.h>

static NetDev *init_net_dev(const uint8_t mac[]) {
NetDev *dev = malloc(sizeof(NetDev));
NetDev *dev = calloc(1, sizeof(NetDev));
if (!dev)
return NULL;
memcpy(dev->config.mac, mac, sizeof(dev->config.mac));
dev->config.status = VIRTIO_NET_S_LINK_UP;
dev->tapfd = -1;
Expand Down Expand Up @@ -321,8 +323,12 @@ static void virtio_net_close(VirtIODevice *vdev) {
if (dev) {
if (dev->tapfd >= 0)
close(dev->tapfd);
remove_event(dev->event);
free(dev->event);
dev->tapfd = -1;
if (dev->event) {
remove_event(dev->event);
free(dev->event);
dev->event = NULL;
}
free(dev->in_iov);
free(dev->out_iov);
free(dev);
Expand Down Expand Up @@ -369,7 +375,11 @@ static int virtio_net_parse_params(const cJSON *json, void **out) {
free(p);
return -EINVAL;
}
p->tap = tap->valuestring;
if (strlen(tap->valuestring) >= sizeof(p->tap)) {
free(p);
return -ENAMETOOLONG;
}
memcpy(p->tap, tap->valuestring, strlen(tap->valuestring) + 1);

cJSON *mac_json = cJSON_GetObjectItem(json, "mac");
if (cJSON_GetArraySize(mac_json) != 6) {
Expand Down
4 changes: 2 additions & 2 deletions tools/virtio/include/virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ void handle_virtio_requests();

int virtio_init();

int create_virtio_device_from_json(const cJSON *device_json, int zone_id);

int virtio_start_from_json(char *json_path);

int virtio_start(int argc, char *argv[]);

int virtio_add(int argc, char *argv[]);

#endif /* __HVISOR_VIRTIO_H */
5 changes: 4 additions & 1 deletion tools/virtio/include/virtio_blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifndef _HVISOR_VIRTIO_BLK_H
#define _HVISOR_VIRTIO_BLK_H
#include "virtio.h"
#include <limits.h>
#include <linux/virtio_blk.h>
#include <pthread.h>
#include <stdbool.h>
Expand Down Expand Up @@ -39,14 +40,16 @@ typedef struct virtio_blk_dev {
pthread_cond_t cond;
bool close;
bool thread_started;
bool mutex_initialized;
bool cond_initialized;
bool reset; // Device reset in progress: worker must not touch the vq
bool worker_paused; // Worker parked in reset wait; vq not touched
struct iovec out_buf[VIRTQUEUE_BLK_MAX_SIZE];
struct iovec in_buf[VIRTQUEUE_BLK_MAX_SIZE];
} BlkDev;

struct virtio_blk_init_params {
const char *img_path;
char img_path[PATH_MAX];
};

extern const struct virtio_device_ops virtio_blk_ops;
Expand Down
3 changes: 2 additions & 1 deletion tools/virtio/include/virtio_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "event_monitor.h"
#include "virtio.h"
#include <linux/virtio_net.h>
#include <net/if.h>

// Queue idx for virtio net.
#define NET_QUEUE_RX 0
Expand All @@ -25,7 +26,7 @@

struct virtio_net_init_params {
uint8_t mac[6];
const char *tap;
char tap[IFNAMSIZ];
};

// Max iov entries for a single descriptor chain. Each descriptor in the
Expand Down
Loading
Loading