From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LweEg-0005Nw-Qm for qemu-devel@nongnu.org; Wed, 22 Apr 2009 11:19:42 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LweEg-0005NQ-4c for qemu-devel@nongnu.org; Wed, 22 Apr 2009 11:19:42 -0400 Received: from [199.232.76.173] (port=45051 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LweEf-0005NL-Pj for qemu-devel@nongnu.org; Wed, 22 Apr 2009 11:19:41 -0400 Received: from savannah.gnu.org ([199.232.41.3]:33617 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LweEf-0003zr-A6 for qemu-devel@nongnu.org; Wed, 22 Apr 2009 11:19:41 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1LweEe-00040g-Ms for qemu-devel@nongnu.org; Wed, 22 Apr 2009 15:19:40 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.69) (envelope-from ) id 1LweEe-00040J-8Z for qemu-devel@nongnu.org; Wed, 22 Apr 2009 15:19:40 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Wed, 22 Apr 2009 15:19:40 +0000 Subject: [Qemu-devel] [7225] xen: blk & nic configuration via cmd line. List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 7225 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7225 Author: aliguori Date: 2009-04-22 15:19:39 +0000 (Wed, 22 Apr 2009) Log Message: ----------- xen: blk & nic configuration via cmd line. (Gerd Hoffmann) 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 Signed-off-by: Anthony Liguori Modified Paths: -------------- trunk/Makefile.target trunk/hw/xen_backend.c trunk/hw/xen_backend.h trunk/hw/xen_machine_pv.c Added Paths: ----------- trunk/hw/xen_devconfig.c Modified: trunk/Makefile.target =================================================================== --- trunk/Makefile.target 2009-04-22 15:19:35 UTC (rev 7224) +++ trunk/Makefile.target 2009-04-22 15:19:39 UTC (rev 7225) @@ -561,7 +561,7 @@ endif # xen backend driver support -XEN_OBJS := xen_machine_pv.o xen_backend.o +XEN_OBJS := xen_machine_pv.o xen_backend.o xen_devconfig.o XEN_OBJS += xen_console.o xenfb.o xen_disk.o xen_nic.o ifeq ($(CONFIG_XEN), yes) OBJS += $(XEN_OBJS) Modified: trunk/hw/xen_backend.c =================================================================== --- trunk/hw/xen_backend.c 2009-04-22 15:19:35 UTC (rev 7224) +++ trunk/hw/xen_backend.c 2009-04-22 15:19:39 UTC (rev 7225) @@ -45,6 +45,7 @@ /* public */ int xen_xc; struct xs_handle *xenstore = NULL; +const char *xen_protocol; /* private */ static TAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = TAILQ_HEAD_INITIALIZER(xendevs); Modified: trunk/hw/xen_backend.h =================================================================== --- trunk/hw/xen_backend.h 2009-04-22 15:19:35 UTC (rev 7224) +++ trunk/hw/xen_backend.h 2009-04-22 15:19:39 UTC (rev 7225) @@ -3,6 +3,8 @@ #include "xen_common.h" #include "sysemu.h" +#include "net.h" +#include "block_int.h" /* ------------------------------------------------------------- */ @@ -56,6 +58,7 @@ /* variables */ extern int xen_xc; extern struct xs_handle *xenstore; +extern const char *xen_protocol; /* xenstore helper functions */ int xenstore_write_str(const char *base, const char *node, const char *val); @@ -93,4 +96,9 @@ void xen_init_display(int domid); +/* configuration (aka xenbus setup) */ +void xen_config_cleanup(void); +int xen_config_dev_blk(DriveInfo *disk); +int xen_config_dev_nic(NICInfo *nic); + #endif /* QEMU_HW_XEN_BACKEND_H */ Added: trunk/hw/xen_devconfig.c =================================================================== --- trunk/hw/xen_devconfig.c (rev 0) +++ trunk/hw/xen_devconfig.c 2009-04-22 15:19:39 UTC (rev 7225) @@ -0,0 +1,142 @@ +#include "xen_backend.h" + +/* ------------------------------------------------------------- */ + +struct xs_dirs { + char *xs_dir; + TAILQ_ENTRY(xs_dirs) list; +}; +static TAILQ_HEAD(xs_dirs_head, xs_dirs) xs_cleanup = TAILQ_HEAD_INITIALIZER(xs_cleanup); + +static void xen_config_cleanup_dir(char *dir) +{ + struct xs_dirs *d; + + d = qemu_malloc(sizeof(*d)); + d->xs_dir = dir; + TAILQ_INSERT_TAIL(&xs_cleanup, d, list); +} + +void xen_config_cleanup(void) +{ + struct xs_dirs *d; + + TAILQ_FOREACH(d, &xs_cleanup, list) { + xs_rm(xenstore, 0, d->xs_dir); + } +} + +/* ------------------------------------------------------------- */ + +static int xen_config_dev_mkdir(char *dev, int p) +{ + struct xs_permissions perms[2] = {{ + .id = 0, /* set owner: dom0 */ + },{ + .id = xen_domid, + .perms = p, + }}; + + if (!xs_mkdir(xenstore, 0, dev)) { + xen_be_printf(NULL, 0, "xs_mkdir %s: failed\n", dev); + return -1; + } + xen_config_cleanup_dir(qemu_strdup(dev)); + + if (!xs_set_permissions(xenstore, 0, dev, perms, 2)) { + xen_be_printf(NULL, 0, "xs_set_permissions %s: failed\n", dev); + return -1; + } + return 0; +} + +static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev, + char *fe, char *be, int len) +{ + char *dom; + + dom = xs_get_domain_path(xenstore, xen_domid); + snprintf(fe, len, "%s/device/%s/%d", dom, ftype, vdev); + free(dom); + + dom = xs_get_domain_path(xenstore, 0); + snprintf(be, len, "%s/backend/%s/%d/%d", dom, btype, xen_domid, vdev); + free(dom); + + xen_config_dev_mkdir(fe, XS_PERM_READ | 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 (xen_protocol) + xenstore_write_str(fe, "protocol", xen_protocol); + + 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; + const char *devtype = cdrom ? "cdrom" : "disk"; + const char *mode = cdrom ? "r" : "w"; + + snprintf(disk->bdrv->device_name, sizeof(disk->bdrv->device_name), + "xvd%c", 'a' + disk->unit); + xen_be_printf(NULL, 1, "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]); + xen_be_printf(NULL, 1, "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); +} Modified: trunk/hw/xen_machine_pv.c =================================================================== --- trunk/hw/xen_machine_pv.c 2009-04-22 15:19:35 UTC (rev 7224) +++ trunk/hw/xen_machine_pv.c 2009-04-22 15:19:39 UTC (rev 7225) @@ -39,6 +39,7 @@ const char *cpu_model) { CPUState *env; + int i, index; /* Initialize a dummy CPU */ if (cpu_model == NULL) { @@ -62,6 +63,24 @@ xen_be_register("qdisk", &xen_blkdev_ops); xen_be_register("qnic", &xen_netdev_ops); + /* 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); + /* setup framebuffer */ xen_init_display(xen_domid); }