[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 7/7] xen: blk & nic configuration via cmd line.



This patch makes qemu create backend and frontend device entries in
xenstore for devices configured on the command line.  It will use
qdisk and qnic backend names, so the qemu internal backends will
be used.

Disks can be created using -drive if=xen,file=...
Nics can be created using -net nic,macaddr=...

Signed-off-by: Gerd Hoffmann <kraxel@xxxxxxxxxx>
---
 Makefile.target  |    2 +-
 hw/xen-backend.h |    7 +++
 hw/xen-config.c  |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/xen-machine.c |   22 ++++++++-
 4 files changed, 169 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen-config.c

diff --git a/Makefile.target b/Makefile.target
index 281d7fa..acb679d 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -516,7 +516,7 @@ LIBS += $(CONFIG_VNC_TLS_LIBS)
 endif
 
 # xen backend driver support
-XEN_OBJS := xen-machine.o xen-backend.o
+XEN_OBJS := xen-machine.o xen-backend.o xen-config.o
 XEN_OBJS += xen-console.o xen-framebuffer.o xen-disk.o xen-nic.o
 ifeq ($(CONFIG_XEN), yes)
   OBJS += $(XEN_OBJS)
diff --git a/hw/xen-backend.h b/hw/xen-backend.h
index 2db2c3c..2f21f84 100644
--- a/hw/xen-backend.h
+++ b/hw/xen-backend.h
@@ -11,6 +11,8 @@
 #include "hw.h"
 #include "xen.h"
 #include "sysemu.h"
+#include "net.h"
+#include "block_int.h"
 
 /*
  * tweaks needed to build with different xen versions
@@ -124,3 +126,8 @@ struct devops xen_netdev_ops;       /* xen_nic.c         */
 
 void xen_set_display(int domid, DisplayState *ds);
 
+/* configuration (aka xenbus setup) */
+void xen_config_cleanup(void);
+int xen_config_dev_blk(DriveInfo *disk);
+int xen_config_dev_nic(NICInfo *nic);
+
diff --git a/hw/xen-config.c b/hw/xen-config.c
new file mode 100644
index 0000000..112cfcf
--- /dev/null
+++ b/hw/xen-config.c
@@ -0,0 +1,140 @@
+#include "xen-backend.h"
+
+/* ------------------------------------------------------------- */
+
+struct xs_dirs {
+    char *xs_dir;
+    struct list_head list;
+};
+static LIST_HEAD (xs_cleanup);
+
+static int xen_config_cleanup_dir(char *dir)
+{
+    struct xs_dirs *d;
+
+    d = malloc(sizeof(*d));
+    d->xs_dir = dir;
+    list_add_tail(&d->list, &xs_cleanup);
+    return 0;
+}
+
+void xen_config_cleanup(void)
+{
+    struct list_head *item;
+    struct xs_dirs *d;
+
+    fprintf(stderr, "xen be: %s\n", __FUNCTION__);
+    list_for_each(item, &xs_cleanup) {
+       d = list_entry(item, struct xs_dirs, list);
+       xs_rm(xenstore, 0, d->xs_dir);
+    }
+}
+
+/* ------------------------------------------------------------- */
+
+static int xen_config_dev_mkdir(char *dev, int p)
+{
+    struct xs_permissions perms = {
+       .id    = xen_domid,
+       .perms = p,
+    };
+
+    if (!xs_mkdir(xenstore, 0, dev)) {
+       fprintf(stderr, "xs_mkdir %s: failed\n", dev);
+       return -1;
+    }
+    xen_config_cleanup_dir(strdup(dev));
+
+    if (!xs_set_permissions(xenstore, 0, dev, &perms, 1)) {
+       fprintf(stderr, "%s: xs_set_permissions failed\n", __FUNCTION__);
+       return -1;
+    }
+    return 0;
+}
+
+static int xen_config_dev_dirs(char *ftype, char *btype, int vdev,
+                              char *fe, char *be, int len)
+{
+    snprintf(fe, len, "/local/domain/%d/device/%s/%d",
+            xen_domid, ftype, vdev);
+    snprintf(be, len, "/local/domain/0/backend/%s/%d/%d",
+            btype, xen_domid, vdev);
+
+    xen_config_dev_mkdir(fe, XS_PERM_WRITE);
+    xen_config_dev_mkdir(be, XS_PERM_READ);
+    return 0;
+}
+
+static int xen_config_dev_all(char *fe, char *be)
+{
+    /* frontend */
+#if 0
+    xenstore_write_str(fe, "protocol",
+                           xen_config_dev_protocol(xen));
+#endif
+    xenstore_write_int(fe, "state",           XenbusStateInitialising);
+    xenstore_write_int(fe, "backend-id",      0);
+    xenstore_write_str(fe, "backend",         be);
+
+    /* backend */
+    xenstore_write_str(be, "domain",          qemu_name ? qemu_name : 
"no-name");
+    xenstore_write_int(be, "online",          1);
+    xenstore_write_int(be, "state",           XenbusStateInitialising);
+    xenstore_write_int(be, "frontend-id",     xen_domid);
+    xenstore_write_str(be, "frontend",        fe);
+
+    return 0;
+}
+
+/* ------------------------------------------------------------- */
+
+int xen_config_dev_blk(DriveInfo *disk)
+{
+    char fe[256], be[256];
+    int vdev = 202 * 256 + 16 * disk->unit;
+    int cdrom = disk->bdrv->type == BDRV_TYPE_CDROM;
+    char *devtype = cdrom ? "cdrom" : "disk";
+    char *mode    = cdrom ? "r"     : "w";
+
+    snprintf(disk->bdrv->device_name, sizeof(disk->bdrv->device_name),
+            "xvd%c", 'a' + disk->unit);
+    fprintf(stderr, "xen be: config disk %d [%s]: %s\n",
+           disk->unit, disk->bdrv->device_name, disk->bdrv->filename);
+    xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
+
+    /* frontend */
+    xenstore_write_int(fe, "virtual-device",  vdev);
+    xenstore_write_str(fe, "device-type",     devtype);
+
+    /* backend */
+    xenstore_write_str(be, "dev",             disk->bdrv->device_name);
+    xenstore_write_str(be, "type",            "file");
+    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "mode",            mode);
+
+    /* common stuff */
+    return xen_config_dev_all(fe, be);
+}
+
+int xen_config_dev_nic(NICInfo *nic)
+{
+    char fe[256], be[256];
+    char mac[20];
+
+    snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
+            nic->macaddr[0], nic->macaddr[1], nic->macaddr[2],
+            nic->macaddr[3], nic->macaddr[4], nic->macaddr[5]);
+    fprintf(stderr, "xen be: config nic %d: mac=\"%s\"\n", nic->vlan->id, mac);
+    xen_config_dev_dirs("vif", "qnic", nic->vlan->id, fe, be, sizeof(fe));
+
+    /* frontend */
+    xenstore_write_int(fe, "handle",     nic->vlan->id);
+    xenstore_write_str(fe, "mac",        mac);
+
+    /* backend */
+    xenstore_write_int(be, "handle",     nic->vlan->id);
+    xenstore_write_str(be, "mac",        mac);
+
+    /* common stuff */
+    return xen_config_dev_all(fe, be);
+}
diff --git a/hw/xen-machine.c b/hw/xen-machine.c
index 3fa4079..380a61b 100644
--- a/hw/xen-machine.c
+++ b/hw/xen-machine.c
@@ -43,6 +43,7 @@ static void xenpv_init(ram_addr_t ram_size, int vga_ram_size,
                       const char *cpu_model)
 {
     CPUState *env;
+    int i, index;
 
     /* create dummy cpu, halted */
     if (cpu_model == NULL) {
@@ -55,9 +56,28 @@ static void xenpv_init(ram_addr_t ram_size, int vga_ram_size,
     env = cpu_init(cpu_model);
     env->halted = 1;
 
-    /* setup xen backend handlers */
+    /* backend core init */
     xen_be_init();
 
+    /* configure disks */
+    for (i = 0; i < 16; i++) {
+        index = drive_get_index(IF_XEN, 0, i);
+       if (index == -1)
+           continue;
+       xen_config_dev_blk(drives_table + index);
+    }
+
+    /* configure nics */
+    for (i = 0; i < nb_nics; i++) {
+       if (nd_table[i].model && 0 != strcmp(nd_table[i].model, "xen"))
+           continue;
+        xen_config_dev_nic(nd_table + i);
+    }
+
+    /* config cleanup hook */
+    atexit(xen_config_cleanup);
+
+    /* backend driver init */
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("vfb", &xen_framebuffer_ops);
-- 
1.5.4.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.