#
# Libraries that we need to build
#
-LIBS = libv3_user_host_dev.a libv3_user_keyed_stream.a
+LIBS = libv3_user_host_dev.a libv3_user_keyed_stream.a libv3_ctrl.a
BUILD_EXECS = $(BASE_EXECS) $(EXAMPLE_EXECS) $(EXPERIMENTAL_EXECS)
BUILD_LIBS = $(LIBS)
all: $(BUILD_EXECS) $(BUILD_LIBS) $(COPIED_EXECS)
+libv3_ctrl.a : v3_ctrl.c v3_ctrl.h
+ $(CC) $(CFLAGS) -I../linux_module -c v3_ctrl.c
+ $(AR) ruv libv3_ctrl.a v3_ctrl.o
+ rm -rf v3_ctrl.o
+
#
# Most tools compile in a straightforward way
#
-% :: %.c v3_ctrl.h
- $(CC) $(CFLAGS) $< -o $@
+% :: %.c v3_ctrl.h libv3_ctrl.a
+ $(CC) $(CFLAGS) $< -lv3_ctrl -L. -o $@
#
v3_cons_sc: v3_cons_sc.c
$(CC) $(CFLAGS) $(CURSES_CFLAGS) $< -lcurses -o $@
-v3_create: v3_create.c ezxml.c
- $(CC) $(CFLAGS) $^ -o $@
+v3_create: v3_create.c ezxml.c libv3_ctrl.a
+ $(CC) $(CFLAGS) $^ -lv3_ctrl -L. -o $@
#include "v3_ctrl.h"
#include "ezxml.h"
-struct cfg_value {
- char * tag;
- char * value;
-};
-
-struct xml_option {
- char * tag;
- ezxml_t location;
- struct xml_option * next;
-};
-
-
-struct file_info {
- int size;
- char filename[2048];
- char id[256];
-};
#define MAX_FILES 256
unsigned long long num_files = 0;
struct file_info files[MAX_FILES];
-int read_file(int fd, int size, unsigned char * buf);
-
-
-
int create_vm(char * vm_name, void * img_data, unsigned int img_size) {
struct v3_guest_img guest_img;
int v3_fd = 0;
guest_img.guest_data = img_data;
strncpy(guest_img.name, vm_name, 127);
-
- v3_fd = open(v3_dev, O_RDONLY);
-
- if (v3_fd == -1) {
- printf("Error opening V3Vee control device\n");
- return -1;
- }
-
- dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img);
-
+ dev_idx = v3_dev_ioctl(V3_CREATE_GUEST, &guest_img);
if (dev_idx < 0) {
printf("Error (%d) creating VM\n", dev_idx);
}
printf("VM (%s) created at /dev/v3-vm%d\n", vm_name, dev_idx);
-
- /* Close the file descriptor. */
- close(v3_fd);
-
return 0;
}
// load guest image into user memory
img_data = malloc(img_size);
- read_file(guest_fd, img_size, img_data);
+ v3_read_file(guest_fd, img_size, img_data);
close(guest_fd);
return -1;
}
- read_file(fd, files[i].size, (unsigned char *)(guest_img_data + offset));
+ v3_read_file(fd, files[i].size, (unsigned char *)(guest_img_data + offset));
close(fd);
-
-
int main(int argc, char** argv) {
char * filename = NULL;
char * name = NULL;
}
}
- if (argc - optind + 1 < 3) {
- printf("usage: v3_create [-b] <guest_img> <vm name> [cfg options]\n");
- return -1;
- }
+ if (argc - optind + 1 < 3)
+ v3_usage("[-b] <guest_img> <vm_name> [cfg options]\n");
filename = argv[optind];
name = argv[optind + 1];
return 0;
}
-
-
-
-int read_file(int fd, int size, unsigned char * buf) {
- int left_to_read = size;
- int have_read = 0;
-
- while (left_to_read != 0) {
- int bytes_read = read(fd, buf + have_read, left_to_read);
-
- if (bytes_read <= 0) {
- break;
- }
-
- have_read += bytes_read;
- left_to_read -= bytes_read;
- }
-
- if (left_to_read != 0) {
- printf("Error could not finish reading file\n");
- return -1;
- }
-
- return 0;
-}
/*
- * V3 Control utility
+ * V3 Control Library
* (c) Jack lange, 2010
+ * Kyle Hale, 2012
*/
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include "v3_ctrl.h"
-int read_file(int fd, int size, unsigned char * buf);
-int main(int argc, char* argv[]) {
- char * filename = argv[1];
- char * name = argv[2];
- int guest_fd = 0;
- int v3_fd = 0;
- struct v3_guest_img guest_img;
- struct stat guest_stats;
-
- memset(&guest_img, 0, sizeof(struct v3_guest_img));
+/*
+ * create a file-backed memory region
+ */
+void * v3_mmap_file (const char * filename, int prot, int flags) {
+ int fd;
+ struct stat st;
+ void * m;
+
+ fd = open(filename, O_RDONLY);
+ if (!fd) {
+ fprintf(stderr, "Error opening file for mapping: %s\n", filename);
+ return NULL;
+ }
- if (argc <= 2) {
- printf("usage: v3_ctrl <guest_img> <vm name>\n");
- return -1;
+ fstat(fd, &st);
+
+ if ((m = mmap(NULL, st.st_size, prot, flags, fd, 0)) == MAP_FAILED) {
+ fprintf(stderr, "Error mapping file (%s)\n", filename);
+ close(fd);
+ return NULL;
}
- printf("Launching guest: %s\n", filename);
+ close(fd);
+ return m;
+}
- guest_fd = open(filename, O_RDONLY);
- if (guest_fd == -1) {
- printf("Error Opening guest image: %s\n", filename);
- return -1;
+/*
+ * read a file into a buffer
+ */
+int v3_read_file (int fd, int size, unsigned char * buf) {
+ int left_to_read = size;
+ int have_read = 0;
+
+ while (left_to_read != 0) {
+ int bytes_read = read(fd, buf + have_read, left_to_read);
+
+ if (bytes_read <= 0) {
+ break;
+ }
+
+ have_read += bytes_read;
+ left_to_read -= bytes_read;
}
- if (fstat(guest_fd, &guest_stats) == -1) {
- printf("ERROR: Could not stat guest image file -- %s\n", filename);
+ if (left_to_read != 0) {
+ fprintf(stderr, "Error could not finish reading file\n");
return -1;
}
-
-
- guest_img.size = guest_stats.st_size;
- // load guest image into user memory
- guest_img.guest_data = malloc(guest_img.size);
- if (!guest_img.guest_data) {
- printf("ERROR: Could not allocate memory for guest image\n");
+ return 0;
+}
+
+
+/*
+ * perform an ioctl on v3vee device
+ */
+int v3_dev_ioctl (int request, void * arg) {
+ int fd, ret;
+
+ fd = open(v3_dev, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "Error opening V3Vee control device\n");
return -1;
}
- read_file(guest_fd, guest_img.size, guest_img.guest_data);
-
- close(guest_fd);
+ ret = ioctl(fd, request, arg);
+
+ if (ret < 0)
+ fprintf(stderr, "IOCTL error on V3Vee control device (%d)\n", ret);
- printf("Loaded guest image. Launching to V3Vee\n");
-
- strncpy(guest_img.name, name, 127);
+ close(fd);
+ return ret;
+}
- v3_fd = open(v3_dev, O_RDONLY);
- if (v3_fd == -1) {
- printf("Error opening V3Vee control device\n");
- return -1;
+/*
+ * perform an ioctl on arbitrary VM device
+ */
+int v3_vm_ioctl (const char * filename,
+ int request,
+ void * arg) {
+ int fd, ret;
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "Error opening V3Vee VM device: %s\n", filename);
+ return -1;
}
- ioctl(v3_fd, V3_START_GUEST, &guest_img);
+ ret = ioctl(fd, request, arg);
+ if (ret < 0)
+ fprintf(stderr, "IOCTL error on device %s (%d)\n", filename, ret);
+ close(fd);
+ return ret;
+}
- /* Close the file descriptor. */
- close(v3_fd);
-
+/*
+ * launch a VM with VM device path
+ */
+int launch_vm (const char * filename) {
+ int err;
- return 0;
-}
+ printf("Launching VM (%s)\n", filename);
+ err = v3_vm_ioctl(filename, V3_VM_LAUNCH, NULL);
+ if (err < 0) {
+ fprintf(stderr, "Error launching VM (%s)\n", filename);
+ return -1;
+ }
+ return 0;
+}
-int read_file(int fd, int size, unsigned char * buf) {
- int left_to_read = size;
- int have_read = 0;
- while (left_to_read != 0) {
- int bytes_read = read(fd, buf + have_read, left_to_read);
+/*
+ * stop a VM with VM device path
+ */
+int stop_vm (const char * filename) {
+ int err;
- if (bytes_read <= 0) {
- break;
- }
+ printf("Stopping VM (%s)\n", filename);
- have_read += bytes_read;
- left_to_read -= bytes_read;
- }
+ if (v3_vm_ioctl(filename, V3_VM_STOP, NULL) < 0)
+ return -1;
- if (left_to_read != 0) {
- printf("Error could not finish reading file\n");
- return -1;
- }
-
return 0;
}
#ifndef _v3_ctrl_h
#define _v3_ctrl_h
+#include <stdio.h>
+#include <stdlib.h>
+#include "ezxml.h"
/* Global Control IOCTLs */
#define V3_CREATE_GUEST 12
#define V3_FREE_GUEST 13
#define V3_ADD_MEMORY 50
+#define V3_RESET_MEMORY 51
#define V3_ADD_PCI_HW_DEV 55
#define V3_ADD_PCI_USER_DEV 56
unsigned int func;
} __attribute__((packed));
+#define V3VEE_STR "\n\n" \
+ "The V3Vee Project (c) 2012\n" \
+ "\thttp://v3vee.org\n" \
+ "\n\n"
+
+#define v3_usage(fmt, args...) \
+{ \
+ printf(("\nUsage: %s " fmt V3VEE_STR), argv[0], ##args); \
+ exit(0); \
+}
+
+
+int v3_dev_ioctl (int req, void * arg);
+int v3_vm_ioctl (const char * filename,
+ int req,
+ void * arg);
+void * v3_mmap_file (const char * filename, int prot, int flags);
+int v3_read_file (int fd, int size, unsigned char * buf);
+
+int launch_vm (const char * filename);
+int stop_vm (const char * filename);
+
+
+/* XML-related structs */
+struct cfg_value {
+ char * tag;
+ char * value;
+};
+
+struct xml_option {
+ char * tag;
+ ezxml_t location;
+ struct xml_option * next;
+};
+
+
+struct file_info {
+ int size;
+ char filename[2048];
+ char id[256];
+};
#endif
* (c) Jack lange, 2011
*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
#include <string.h>
#include "v3_ctrl.h"
int main(int argc, char* argv[]) {
- int vm_fd = 0;
unsigned long vm_idx = 0;
int ret;
-
- if (argc <= 1) {
- printf("usage: v3_free <vm-dev-idx>\n");
- return -1;
- }
+ if (argc <= 1)
+ v3_usage("<vm-dev-idx>\n");
vm_idx = strtol(argv[1], NULL, 0);
printf("Freeing VM %d\n", vm_idx);
- vm_fd = open("/dev/v3vee", O_RDONLY);
-
- if (vm_fd == -1) {
- printf("Error opening V3Vee VM device\n");
- return -1;
- }
-
- ret = ioctl(vm_fd, V3_FREE_GUEST, vm_idx);
- if (ret < 0) {
- printf("Error freeing VM %d\n", vm_idx);
+ if (v3_dev_ioctl(V3_FREE_GUEST, vm_idx) < 0) {
+ fprintf(stderr, "Error freeing VM %d\n", vm_idx);
return -1;
}
- /* Close the file descriptor. */
- close(vm_fd);
-
return 0;
}
* (c) Jack lange, 2010
*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <string.h>
-
#include "v3_ctrl.h"
int main(int argc, char* argv[]) {
- int vm_fd = 0;
char * filename = argv[1];
- int err;
-
- if (argc <= 1) {
- printf("usage: v3_launch <vm-device>\n");
- return -1;
- }
-
- printf("Launching VM (%s)\n", filename);
-
- vm_fd = open(filename, O_RDONLY);
-
- if (vm_fd == -1) {
- printf("Error opening V3Vee VM device\n");
- return -1;
- }
-
- err = ioctl(vm_fd, V3_VM_LAUNCH, NULL);
- if (err < 0) {
- printf("Error launching VM\n");
- return -1;
- }
+ if (argc <= 1)
+ v3_usage("<vm-device>\n");
-
- /* Close the file descriptor. */
- close(vm_fd);
-
-
+ launch_vm(filename);
return 0;
}
-
-
* V3 Control utility
* (c) Jack lange, 2010
*/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <string.h>
-
#include "v3_ctrl.h"
int main(int argc, char* argv[]) {
- int vm_fd = 0;
char * filename = argv[1];
- if (argc <= 1) {
- printf("usage: v3_stop <vm-device>\n");
- return -1;
- }
+ if (argc <= 1)
+ v3_usage("<vm-device>\n");
- printf("Stopping VM (%s)\n", filename);
-
- vm_fd = open(filename, O_RDONLY);
-
- if (vm_fd == -1) {
- printf("Error opening V3Vee VM device\n");
- return -1;
+ if (stop_vm(filename) < 0) {
+ fprintf(stderr, "Error stopping VM (%s)\n", filename);
+ return -1;
}
- ioctl(vm_fd, V3_VM_STOP, NULL);
-
-
-
- /* Close the file descriptor. */
- close(vm_fd);
-
-
-
return 0;
}