]> git.proxmox.com Git - mirror_qemu.git/blob - hw/xen/xen_backend.c
xen: Move xenstore cleanup and mkdir functions
[mirror_qemu.git] / hw / xen / xen_backend.c
1 /*
2 * xen backend driver infrastructure
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
19 */
20
21 /*
22 * TODO: add some xenbus / xenstore concepts overview here.
23 */
24
25 #include "qemu/osdep.h"
26 #include <sys/signal.h>
27
28 #include "hw/hw.h"
29 #include "hw/sysbus.h"
30 #include "sysemu/char.h"
31 #include "qemu/log.h"
32 #include "hw/xen/xen_backend.h"
33 #include "hw/xen/xen_pvdev.h"
34
35 #include <xen/grant_table.h>
36
37 #define TYPE_XENSYSDEV "xensysdev"
38
39 DeviceState *xen_sysdev;
40
41 /* ------------------------------------------------------------- */
42
43 /* public */
44 xc_interface *xen_xc = NULL;
45 xenforeignmemory_handle *xen_fmem = NULL;
46 struct xs_handle *xenstore = NULL;
47 const char *xen_protocol;
48
49 /* private */
50 static int debug;
51
52 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
53 {
54 return xenstore_write_str(xendev->be, node, val);
55 }
56
57 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
58 {
59 return xenstore_write_int(xendev->be, node, ival);
60 }
61
62 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
63 {
64 return xenstore_write_int64(xendev->be, node, ival);
65 }
66
67 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
68 {
69 return xenstore_read_str(xendev->be, node);
70 }
71
72 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
73 {
74 return xenstore_read_int(xendev->be, node, ival);
75 }
76
77 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
78 {
79 return xenstore_read_str(xendev->fe, node);
80 }
81
82 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
83 {
84 return xenstore_read_int(xendev->fe, node, ival);
85 }
86
87 int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node,
88 uint64_t *uval)
89 {
90 return xenstore_read_uint64(xendev->fe, node, uval);
91 }
92
93 /* ------------------------------------------------------------- */
94
95 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
96 {
97 int rc;
98
99 rc = xenstore_write_be_int(xendev, "state", state);
100 if (rc < 0) {
101 return rc;
102 }
103 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
104 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
105 xendev->be_state = state;
106 return 0;
107 }
108
109 /*
110 * get xen backend device, allocate a new one if it doesn't exist.
111 */
112 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
113 struct XenDevOps *ops)
114 {
115 struct XenDevice *xendev;
116
117 xendev = xen_be_find_xendev(type, dom, dev);
118 if (xendev) {
119 return xendev;
120 }
121
122 /* init new xendev */
123 xendev = g_malloc0(ops->size);
124 xendev->type = type;
125 xendev->dom = dom;
126 xendev->dev = dev;
127 xendev->ops = ops;
128
129 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
130 xendev->type, xendev->dom, xendev->dev);
131 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
132 xendev->type, xendev->dev);
133
134 xendev->debug = debug;
135 xendev->local_port = -1;
136
137 xendev->evtchndev = xenevtchn_open(NULL, 0);
138 if (xendev->evtchndev == NULL) {
139 xen_be_printf(NULL, 0, "can't open evtchn device\n");
140 g_free(xendev);
141 return NULL;
142 }
143 fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
144
145 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
146 xendev->gnttabdev = xengnttab_open(NULL, 0);
147 if (xendev->gnttabdev == NULL) {
148 xen_be_printf(NULL, 0, "can't open gnttab device\n");
149 xenevtchn_close(xendev->evtchndev);
150 g_free(xendev);
151 return NULL;
152 }
153 } else {
154 xendev->gnttabdev = NULL;
155 }
156
157 xen_pv_insert_xendev(xendev);
158
159 if (xendev->ops->alloc) {
160 xendev->ops->alloc(xendev);
161 }
162
163 return xendev;
164 }
165
166
167 /*
168 * Sync internal data structures on xenstore updates.
169 * Node specifies the changed field. node = NULL means
170 * update all fields (used for initialization).
171 */
172 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
173 {
174 if (node == NULL || strcmp(node, "online") == 0) {
175 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
176 xendev->online = 0;
177 }
178 }
179
180 if (node) {
181 xen_be_printf(xendev, 2, "backend update: %s\n", node);
182 if (xendev->ops->backend_changed) {
183 xendev->ops->backend_changed(xendev, node);
184 }
185 }
186 }
187
188 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
189 {
190 int fe_state;
191
192 if (node == NULL || strcmp(node, "state") == 0) {
193 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
194 fe_state = XenbusStateUnknown;
195 }
196 if (xendev->fe_state != fe_state) {
197 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
198 xenbus_strstate(xendev->fe_state),
199 xenbus_strstate(fe_state));
200 }
201 xendev->fe_state = fe_state;
202 }
203 if (node == NULL || strcmp(node, "protocol") == 0) {
204 g_free(xendev->protocol);
205 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
206 if (xendev->protocol) {
207 xen_be_printf(xendev, 1, "frontend protocol: %s\n",
208 xendev->protocol);
209 }
210 }
211
212 if (node) {
213 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
214 if (xendev->ops->frontend_changed) {
215 xendev->ops->frontend_changed(xendev, node);
216 }
217 }
218 }
219
220 /* ------------------------------------------------------------- */
221 /* Check for possible state transitions and perform them. */
222
223 /*
224 * Initial xendev setup. Read frontend path, register watch for it.
225 * Should succeed once xend finished setting up the backend device.
226 *
227 * Also sets initial state (-> Initializing) when done. Which
228 * only affects the xendev->be_state variable as xenbus should
229 * already be put into that state by xend.
230 */
231 static int xen_be_try_setup(struct XenDevice *xendev)
232 {
233 char token[XEN_BUFSIZE];
234 int be_state;
235
236 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
237 xen_be_printf(xendev, 0, "reading backend state failed\n");
238 return -1;
239 }
240
241 if (be_state != XenbusStateInitialising) {
242 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
243 xenbus_strstate(be_state));
244 return -1;
245 }
246
247 xendev->fe = xenstore_read_be_str(xendev, "frontend");
248 if (xendev->fe == NULL) {
249 xen_be_printf(xendev, 0, "reading frontend path failed\n");
250 return -1;
251 }
252
253 /* setup frontend watch */
254 snprintf(token, sizeof(token), "fe:%p", xendev);
255 if (!xs_watch(xenstore, xendev->fe, token)) {
256 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
257 xendev->fe);
258 return -1;
259 }
260 xen_be_set_state(xendev, XenbusStateInitialising);
261
262 xen_be_backend_changed(xendev, NULL);
263 xen_be_frontend_changed(xendev, NULL);
264 return 0;
265 }
266
267 /*
268 * Try initialize xendev. Prepare everything the backend can do
269 * without synchronizing with the frontend. Fakes hotplug-status. No
270 * hotplug involved here because this is about userspace drivers, thus
271 * there are kernel backend devices which could invoke hotplug.
272 *
273 * Goes to InitWait on success.
274 */
275 static int xen_be_try_init(struct XenDevice *xendev)
276 {
277 int rc = 0;
278
279 if (!xendev->online) {
280 xen_be_printf(xendev, 1, "not online\n");
281 return -1;
282 }
283
284 if (xendev->ops->init) {
285 rc = xendev->ops->init(xendev);
286 }
287 if (rc != 0) {
288 xen_be_printf(xendev, 1, "init() failed\n");
289 return rc;
290 }
291
292 xenstore_write_be_str(xendev, "hotplug-status", "connected");
293 xen_be_set_state(xendev, XenbusStateInitWait);
294 return 0;
295 }
296
297 /*
298 * Try to initialise xendev. Depends on the frontend being ready
299 * for it (shared ring and evtchn info in xenstore, state being
300 * Initialised or Connected).
301 *
302 * Goes to Connected on success.
303 */
304 static int xen_be_try_initialise(struct XenDevice *xendev)
305 {
306 int rc = 0;
307
308 if (xendev->fe_state != XenbusStateInitialised &&
309 xendev->fe_state != XenbusStateConnected) {
310 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
311 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
312 } else {
313 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
314 return -1;
315 }
316 }
317
318 if (xendev->ops->initialise) {
319 rc = xendev->ops->initialise(xendev);
320 }
321 if (rc != 0) {
322 xen_be_printf(xendev, 0, "initialise() failed\n");
323 return rc;
324 }
325
326 xen_be_set_state(xendev, XenbusStateConnected);
327 return 0;
328 }
329
330 /*
331 * Try to let xendev know that it is connected. Depends on the
332 * frontend being Connected. Note that this may be called more
333 * than once since the backend state is not modified.
334 */
335 static void xen_be_try_connected(struct XenDevice *xendev)
336 {
337 if (!xendev->ops->connected) {
338 return;
339 }
340
341 if (xendev->fe_state != XenbusStateConnected) {
342 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
343 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
344 } else {
345 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
346 return;
347 }
348 }
349
350 xendev->ops->connected(xendev);
351 }
352
353 /*
354 * Teardown connection.
355 *
356 * Goes to Closed when done.
357 */
358 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
359 {
360 if (xendev->be_state != XenbusStateClosing &&
361 xendev->be_state != XenbusStateClosed &&
362 xendev->ops->disconnect) {
363 xendev->ops->disconnect(xendev);
364 }
365 if (xendev->be_state != state) {
366 xen_be_set_state(xendev, state);
367 }
368 }
369
370 /*
371 * Try to reset xendev, for reconnection by another frontend instance.
372 */
373 static int xen_be_try_reset(struct XenDevice *xendev)
374 {
375 if (xendev->fe_state != XenbusStateInitialising) {
376 return -1;
377 }
378
379 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
380 xen_be_set_state(xendev, XenbusStateInitialising);
381 return 0;
382 }
383
384 /*
385 * state change dispatcher function
386 */
387 void xen_be_check_state(struct XenDevice *xendev)
388 {
389 int rc = 0;
390
391 /* frontend may request shutdown from almost anywhere */
392 if (xendev->fe_state == XenbusStateClosing ||
393 xendev->fe_state == XenbusStateClosed) {
394 xen_be_disconnect(xendev, xendev->fe_state);
395 return;
396 }
397
398 /* check for possible backend state transitions */
399 for (;;) {
400 switch (xendev->be_state) {
401 case XenbusStateUnknown:
402 rc = xen_be_try_setup(xendev);
403 break;
404 case XenbusStateInitialising:
405 rc = xen_be_try_init(xendev);
406 break;
407 case XenbusStateInitWait:
408 rc = xen_be_try_initialise(xendev);
409 break;
410 case XenbusStateConnected:
411 /* xendev->be_state doesn't change */
412 xen_be_try_connected(xendev);
413 rc = -1;
414 break;
415 case XenbusStateClosed:
416 rc = xen_be_try_reset(xendev);
417 break;
418 default:
419 rc = -1;
420 }
421 if (rc != 0) {
422 break;
423 }
424 }
425 }
426
427 /* ------------------------------------------------------------- */
428
429 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
430 {
431 struct XenDevice *xendev;
432 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
433 char **dev = NULL;
434 unsigned int cdev, j;
435
436 /* setup watch */
437 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
438 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
439 if (!xs_watch(xenstore, path, token)) {
440 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n",
441 path);
442 return -1;
443 }
444
445 /* look for backends */
446 dev = xs_directory(xenstore, 0, path, &cdev);
447 if (!dev) {
448 return 0;
449 }
450 for (j = 0; j < cdev; j++) {
451 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
452 if (xendev == NULL) {
453 continue;
454 }
455 xen_be_check_state(xendev);
456 }
457 free(dev);
458 return 0;
459 }
460
461 void xenstore_update_be(char *watch, char *type, int dom,
462 struct XenDevOps *ops)
463 {
464 struct XenDevice *xendev;
465 char path[XEN_BUFSIZE], *bepath;
466 unsigned int len, dev;
467
468 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
469 if (strncmp(path, watch, len) != 0) {
470 return;
471 }
472 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
473 strcpy(path, "");
474 if (sscanf(watch+len, "/%u", &dev) != 1) {
475 dev = -1;
476 }
477 }
478 if (dev == -1) {
479 return;
480 }
481
482 xendev = xen_be_get_xendev(type, dom, dev, ops);
483 if (xendev != NULL) {
484 bepath = xs_read(xenstore, 0, xendev->be, &len);
485 if (bepath == NULL) {
486 xen_be_del_xendev(xendev);
487 } else {
488 free(bepath);
489 xen_be_backend_changed(xendev, path);
490 xen_be_check_state(xendev);
491 }
492 }
493 }
494
495 void xenstore_update_fe(char *watch, struct XenDevice *xendev)
496 {
497 char *node;
498 unsigned int len;
499
500 len = strlen(xendev->fe);
501 if (strncmp(xendev->fe, watch, len) != 0) {
502 return;
503 }
504 if (watch[len] != '/') {
505 return;
506 }
507 node = watch + len + 1;
508
509 xen_be_frontend_changed(xendev, node);
510 xen_be_check_state(xendev);
511 }
512 /* -------------------------------------------------------------------- */
513
514 int xen_be_init(void)
515 {
516 xenstore = xs_daemon_open();
517 if (!xenstore) {
518 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
519 return -1;
520 }
521
522 qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
523
524 if (xen_xc == NULL || xen_fmem == NULL) {
525 /* Check if xen_init() have been called */
526 goto err;
527 }
528
529 xen_sysdev = qdev_create(NULL, TYPE_XENSYSDEV);
530 qdev_init_nofail(xen_sysdev);
531
532 return 0;
533
534 err:
535 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
536 xs_daemon_close(xenstore);
537 xenstore = NULL;
538
539 return -1;
540 }
541
542 int xen_be_register(const char *type, struct XenDevOps *ops)
543 {
544 char path[50];
545 int rc;
546
547 if (ops->backend_register) {
548 rc = ops->backend_register();
549 if (rc) {
550 return rc;
551 }
552 }
553
554 snprintf(path, sizeof(path), "device-model/%u/backends/%s", xen_domid,
555 type);
556 xenstore_mkdir(path, XS_PERM_NONE);
557
558 return xenstore_scan(type, xen_domid, ops);
559 }
560
561 void xen_be_register_common(void)
562 {
563 xen_be_register("console", &xen_console_ops);
564 xen_be_register("vkbd", &xen_kbdmouse_ops);
565 xen_be_register("qdisk", &xen_blkdev_ops);
566 #ifdef CONFIG_USB_LIBUSB
567 xen_be_register("qusb", &xen_usb_ops);
568 #endif
569 }
570
571 int xen_be_bind_evtchn(struct XenDevice *xendev)
572 {
573 if (xendev->local_port != -1) {
574 return 0;
575 }
576 xendev->local_port = xenevtchn_bind_interdomain
577 (xendev->evtchndev, xendev->dom, xendev->remote_port);
578 if (xendev->local_port == -1) {
579 xen_be_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
580 return -1;
581 }
582 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
583 qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
584 xen_be_evtchn_event, NULL, xendev);
585 return 0;
586 }
587
588
589 static int xen_sysdev_init(SysBusDevice *dev)
590 {
591 return 0;
592 }
593
594 static Property xen_sysdev_properties[] = {
595 {/* end of property list */},
596 };
597
598 static void xen_sysdev_class_init(ObjectClass *klass, void *data)
599 {
600 DeviceClass *dc = DEVICE_CLASS(klass);
601 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
602
603 k->init = xen_sysdev_init;
604 dc->props = xen_sysdev_properties;
605 }
606
607 static const TypeInfo xensysdev_info = {
608 .name = TYPE_XENSYSDEV,
609 .parent = TYPE_SYS_BUS_DEVICE,
610 .instance_size = sizeof(SysBusDevice),
611 .class_init = xen_sysdev_class_init,
612 };
613
614 static void xenbe_register_types(void)
615 {
616 type_register_static(&xensysdev_info);
617 }
618
619 type_init(xenbe_register_types);