1 #include "xen_backend.h"
3 /* ------------------------------------------------------------- */
7 TAILQ_ENTRY(xs_dirs
) list
;
9 static TAILQ_HEAD(xs_dirs_head
, xs_dirs
) xs_cleanup
= TAILQ_HEAD_INITIALIZER(xs_cleanup
);
11 static void xen_config_cleanup_dir(char *dir
)
15 d
= qemu_malloc(sizeof(*d
));
17 TAILQ_INSERT_TAIL(&xs_cleanup
, d
, list
);
20 void xen_config_cleanup(void)
24 TAILQ_FOREACH(d
, &xs_cleanup
, list
) {
25 xs_rm(xenstore
, 0, d
->xs_dir
);
29 /* ------------------------------------------------------------- */
31 static int xen_config_dev_mkdir(char *dev
, int p
)
33 struct xs_permissions perms
[2] = {{
34 .id
= 0, /* set owner: dom0 */
40 if (!xs_mkdir(xenstore
, 0, dev
)) {
41 xen_be_printf(NULL
, 0, "xs_mkdir %s: failed\n", dev
);
44 xen_config_cleanup_dir(qemu_strdup(dev
));
46 if (!xs_set_permissions(xenstore
, 0, dev
, perms
, 2)) {
47 xen_be_printf(NULL
, 0, "xs_set_permissions %s: failed\n", dev
);
53 static int xen_config_dev_dirs(const char *ftype
, const char *btype
, int vdev
,
54 char *fe
, char *be
, int len
)
58 dom
= xs_get_domain_path(xenstore
, xen_domid
);
59 snprintf(fe
, len
, "%s/device/%s/%d", dom
, ftype
, vdev
);
62 dom
= xs_get_domain_path(xenstore
, 0);
63 snprintf(be
, len
, "%s/backend/%s/%d/%d", dom
, btype
, xen_domid
, vdev
);
66 xen_config_dev_mkdir(fe
, XS_PERM_READ
| XS_PERM_WRITE
);
67 xen_config_dev_mkdir(be
, XS_PERM_READ
);
71 static int xen_config_dev_all(char *fe
, char *be
)
75 xenstore_write_str(fe
, "protocol", xen_protocol
);
77 xenstore_write_int(fe
, "state", XenbusStateInitialising
);
78 xenstore_write_int(fe
, "backend-id", 0);
79 xenstore_write_str(fe
, "backend", be
);
82 xenstore_write_str(be
, "domain", qemu_name
? qemu_name
: "no-name");
83 xenstore_write_int(be
, "online", 1);
84 xenstore_write_int(be
, "state", XenbusStateInitialising
);
85 xenstore_write_int(be
, "frontend-id", xen_domid
);
86 xenstore_write_str(be
, "frontend", fe
);
91 /* ------------------------------------------------------------- */
93 int xen_config_dev_blk(DriveInfo
*disk
)
95 char fe
[256], be
[256];
96 int vdev
= 202 * 256 + 16 * disk
->unit
;
97 int cdrom
= disk
->bdrv
->type
== BDRV_TYPE_CDROM
;
98 const char *devtype
= cdrom
? "cdrom" : "disk";
99 const char *mode
= cdrom
? "r" : "w";
101 snprintf(disk
->bdrv
->device_name
, sizeof(disk
->bdrv
->device_name
),
102 "xvd%c", 'a' + disk
->unit
);
103 xen_be_printf(NULL
, 1, "config disk %d [%s]: %s\n",
104 disk
->unit
, disk
->bdrv
->device_name
, disk
->bdrv
->filename
);
105 xen_config_dev_dirs("vbd", "qdisk", vdev
, fe
, be
, sizeof(fe
));
108 xenstore_write_int(fe
, "virtual-device", vdev
);
109 xenstore_write_str(fe
, "device-type", devtype
);
112 xenstore_write_str(be
, "dev", disk
->bdrv
->device_name
);
113 xenstore_write_str(be
, "type", "file");
114 xenstore_write_str(be
, "params", disk
->bdrv
->filename
);
115 xenstore_write_str(be
, "mode", mode
);
118 return xen_config_dev_all(fe
, be
);
121 int xen_config_dev_nic(NICInfo
*nic
)
123 char fe
[256], be
[256];
126 snprintf(mac
, sizeof(mac
), "%02x:%02x:%02x:%02x:%02x:%02x",
127 nic
->macaddr
[0], nic
->macaddr
[1], nic
->macaddr
[2],
128 nic
->macaddr
[3], nic
->macaddr
[4], nic
->macaddr
[5]);
129 xen_be_printf(NULL
, 1, "config nic %d: mac=\"%s\"\n", nic
->vlan
->id
, mac
);
130 xen_config_dev_dirs("vif", "qnic", nic
->vlan
->id
, fe
, be
, sizeof(fe
));
133 xenstore_write_int(fe
, "handle", nic
->vlan
->id
);
134 xenstore_write_str(fe
, "mac", mac
);
137 xenstore_write_int(be
, "handle", nic
->vlan
->id
);
138 xenstore_write_str(be
, "mac", mac
);
141 return xen_config_dev_all(fe
, be
);
144 int xen_config_dev_vfb(int vdev
, const char *type
)
146 char fe
[256], be
[256];
148 xen_config_dev_dirs("vfb", "vfb", vdev
, fe
, be
, sizeof(fe
));
151 xenstore_write_str(be
, "type", type
);
154 return xen_config_dev_all(fe
, be
);
157 int xen_config_dev_vkbd(int vdev
)
159 char fe
[256], be
[256];
161 xen_config_dev_dirs("vkbd", "vkbd", vdev
, fe
, be
, sizeof(fe
));
162 return xen_config_dev_all(fe
, be
);
165 int xen_config_dev_console(int vdev
)
167 char fe
[256], be
[256];
169 xen_config_dev_dirs("console", "console", vdev
, fe
, be
, sizeof(fe
));
170 return xen_config_dev_all(fe
, be
);