root / hw / xen_devconfig.c @ 682aea0e
History | View | Annotate | Download (4.7 kB)
1 |
#include "xen_backend.h" |
---|---|
2 |
|
3 |
/* ------------------------------------------------------------- */
|
4 |
|
5 |
struct xs_dirs {
|
6 |
char *xs_dir;
|
7 |
TAILQ_ENTRY(xs_dirs) list; |
8 |
}; |
9 |
static TAILQ_HEAD(xs_dirs_head, xs_dirs) xs_cleanup = TAILQ_HEAD_INITIALIZER(xs_cleanup);
|
10 |
|
11 |
static void xen_config_cleanup_dir(char *dir) |
12 |
{ |
13 |
struct xs_dirs *d;
|
14 |
|
15 |
d = qemu_malloc(sizeof(*d));
|
16 |
d->xs_dir = dir; |
17 |
TAILQ_INSERT_TAIL(&xs_cleanup, d, list); |
18 |
} |
19 |
|
20 |
void xen_config_cleanup(void) |
21 |
{ |
22 |
struct xs_dirs *d;
|
23 |
|
24 |
TAILQ_FOREACH(d, &xs_cleanup, list) { |
25 |
xs_rm(xenstore, 0, d->xs_dir);
|
26 |
} |
27 |
} |
28 |
|
29 |
/* ------------------------------------------------------------- */
|
30 |
|
31 |
static int xen_config_dev_mkdir(char *dev, int p) |
32 |
{ |
33 |
struct xs_permissions perms[2] = {{ |
34 |
.id = 0, /* set owner: dom0 */ |
35 |
},{ |
36 |
.id = xen_domid, |
37 |
.perms = p, |
38 |
}}; |
39 |
|
40 |
if (!xs_mkdir(xenstore, 0, dev)) { |
41 |
xen_be_printf(NULL, 0, "xs_mkdir %s: failed\n", dev); |
42 |
return -1; |
43 |
} |
44 |
xen_config_cleanup_dir(qemu_strdup(dev)); |
45 |
|
46 |
if (!xs_set_permissions(xenstore, 0, dev, perms, 2)) { |
47 |
xen_be_printf(NULL, 0, "xs_set_permissions %s: failed\n", dev); |
48 |
return -1; |
49 |
} |
50 |
return 0; |
51 |
} |
52 |
|
53 |
static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev, |
54 |
char *fe, char *be, int len) |
55 |
{ |
56 |
char *dom;
|
57 |
|
58 |
dom = xs_get_domain_path(xenstore, xen_domid); |
59 |
snprintf(fe, len, "%s/device/%s/%d", dom, ftype, vdev);
|
60 |
free(dom); |
61 |
|
62 |
dom = xs_get_domain_path(xenstore, 0);
|
63 |
snprintf(be, len, "%s/backend/%s/%d/%d", dom, btype, xen_domid, vdev);
|
64 |
free(dom); |
65 |
|
66 |
xen_config_dev_mkdir(fe, XS_PERM_READ | XS_PERM_WRITE); |
67 |
xen_config_dev_mkdir(be, XS_PERM_READ); |
68 |
return 0; |
69 |
} |
70 |
|
71 |
static int xen_config_dev_all(char *fe, char *be) |
72 |
{ |
73 |
/* frontend */
|
74 |
if (xen_protocol)
|
75 |
xenstore_write_str(fe, "protocol", xen_protocol);
|
76 |
|
77 |
xenstore_write_int(fe, "state", XenbusStateInitialising);
|
78 |
xenstore_write_int(fe, "backend-id", 0); |
79 |
xenstore_write_str(fe, "backend", be);
|
80 |
|
81 |
/* backend */
|
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);
|
87 |
|
88 |
return 0; |
89 |
} |
90 |
|
91 |
/* ------------------------------------------------------------- */
|
92 |
|
93 |
int xen_config_dev_blk(DriveInfo *disk)
|
94 |
{ |
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"; |
100 |
|
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)); |
106 |
|
107 |
/* frontend */
|
108 |
xenstore_write_int(fe, "virtual-device", vdev);
|
109 |
xenstore_write_str(fe, "device-type", devtype);
|
110 |
|
111 |
/* backend */
|
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);
|
116 |
|
117 |
/* common stuff */
|
118 |
return xen_config_dev_all(fe, be);
|
119 |
} |
120 |
|
121 |
int xen_config_dev_nic(NICInfo *nic)
|
122 |
{ |
123 |
char fe[256], be[256]; |
124 |
char mac[20]; |
125 |
|
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)); |
131 |
|
132 |
/* frontend */
|
133 |
xenstore_write_int(fe, "handle", nic->vlan->id);
|
134 |
xenstore_write_str(fe, "mac", mac);
|
135 |
|
136 |
/* backend */
|
137 |
xenstore_write_int(be, "handle", nic->vlan->id);
|
138 |
xenstore_write_str(be, "mac", mac);
|
139 |
|
140 |
/* common stuff */
|
141 |
return xen_config_dev_all(fe, be);
|
142 |
} |
143 |
|
144 |
int xen_config_dev_vfb(int vdev, const char *type) |
145 |
{ |
146 |
char fe[256], be[256]; |
147 |
|
148 |
xen_config_dev_dirs("vfb", "vfb", vdev, fe, be, sizeof(fe)); |
149 |
|
150 |
/* backend */
|
151 |
xenstore_write_str(be, "type", type);
|
152 |
|
153 |
/* common stuff */
|
154 |
return xen_config_dev_all(fe, be);
|
155 |
} |
156 |
|
157 |
int xen_config_dev_vkbd(int vdev) |
158 |
{ |
159 |
char fe[256], be[256]; |
160 |
|
161 |
xen_config_dev_dirs("vkbd", "vkbd", vdev, fe, be, sizeof(fe)); |
162 |
return xen_config_dev_all(fe, be);
|
163 |
} |
164 |
|
165 |
int xen_config_dev_console(int vdev) |
166 |
{ |
167 |
char fe[256], be[256]; |
168 |
|
169 |
xen_config_dev_dirs("console", "console", vdev, fe, be, sizeof(fe)); |
170 |
return xen_config_dev_all(fe, be);
|
171 |
} |