blob: 2510e2e4ff454ea3b65d9cdbff2822c8e7b4f50c [file] [log] [blame]
aliguorid94f9482009-04-22 15:19:15 +00001/*
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
Blue Swirl8167ee82009-07-16 20:47:01 +000015 * with this program; if not, see <http://www.gnu.org/licenses/>.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010016 *
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.
aliguorid94f9482009-04-22 15:19:15 +000019 */
20
21/*
22 * TODO: add some xenbus / xenstore concepts overview here.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <inttypes.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/mman.h>
35#include <sys/signal.h>
36
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010037#include "hw/hw.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020038#include "sysemu/char.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/log.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010040#include "hw/xen/xen_backend.h"
aliguorid94f9482009-04-22 15:19:15 +000041
Anthony PERARDb41f6712012-06-21 11:43:59 +000042#include <xen/grant_table.h>
43
aliguorid94f9482009-04-22 15:19:15 +000044/* ------------------------------------------------------------- */
45
46/* public */
Anthony PERARDd5b93dd2011-02-25 16:20:34 +000047XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
aliguorid94f9482009-04-22 15:19:15 +000048struct xs_handle *xenstore = NULL;
aliguori2c8b24a2009-04-22 15:19:39 +000049const char *xen_protocol;
aliguorid94f9482009-04-22 15:19:15 +000050
51/* private */
Blue Swirl72cf2d42009-09-12 07:36:22 +000052static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
aliguorid94f9482009-04-22 15:19:15 +000053static int debug = 0;
54
55/* ------------------------------------------------------------- */
56
57int xenstore_write_str(const char *base, const char *node, const char *val)
58{
59 char abspath[XEN_BUFSIZE];
60
61 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
Anthony PERARD209cd7a2010-09-23 12:28:45 +010062 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
63 return -1;
64 }
aliguorid94f9482009-04-22 15:19:15 +000065 return 0;
66}
67
68char *xenstore_read_str(const char *base, const char *node)
69{
70 char abspath[XEN_BUFSIZE];
71 unsigned int len;
72 char *str, *ret = NULL;
73
74 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
75 str = xs_read(xenstore, 0, abspath, &len);
76 if (str != NULL) {
77 /* move to qemu-allocated memory to make sure
Anthony Liguori7267c092011-08-20 22:09:37 -050078 * callers can savely g_free() stuff. */
79 ret = g_strdup(str);
aliguorid94f9482009-04-22 15:19:15 +000080 free(str);
81 }
82 return ret;
83}
84
85int xenstore_write_int(const char *base, const char *node, int ival)
86{
Felipe Franciosi10bb3c62013-04-05 15:37:32 +000087 char val[12];
aliguorid94f9482009-04-22 15:19:15 +000088
89 snprintf(val, sizeof(val), "%d", ival);
90 return xenstore_write_str(base, node, val);
91}
92
Felipe Franciosi10bb3c62013-04-05 15:37:32 +000093int xenstore_write_int64(const char *base, const char *node, int64_t ival)
94{
95 char val[21];
96
97 snprintf(val, sizeof(val), "%"PRId64, ival);
98 return xenstore_write_str(base, node, val);
99}
100
aliguorid94f9482009-04-22 15:19:15 +0000101int xenstore_read_int(const char *base, const char *node, int *ival)
102{
103 char *val;
104 int rc = -1;
105
106 val = xenstore_read_str(base, node);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100107 if (val && 1 == sscanf(val, "%d", ival)) {
108 rc = 0;
109 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500110 g_free(val);
aliguorid94f9482009-04-22 15:19:15 +0000111 return rc;
112}
113
Stefano Stabellini4aba9eb2013-12-18 19:17:31 +0000114int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
115{
116 char *val;
117 int rc = -1;
118
119 val = xenstore_read_str(base, node);
120 if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
121 rc = 0;
122 }
123 g_free(val);
124 return rc;
125}
126
aliguorid94f9482009-04-22 15:19:15 +0000127int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
128{
129 return xenstore_write_str(xendev->be, node, val);
130}
131
132int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
133{
134 return xenstore_write_int(xendev->be, node, ival);
135}
136
Felipe Franciosi10bb3c62013-04-05 15:37:32 +0000137int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
138{
139 return xenstore_write_int64(xendev->be, node, ival);
140}
141
aliguorid94f9482009-04-22 15:19:15 +0000142char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
143{
144 return xenstore_read_str(xendev->be, node);
145}
146
147int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
148{
149 return xenstore_read_int(xendev->be, node, ival);
150}
151
152char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
153{
154 return xenstore_read_str(xendev->fe, node);
155}
156
157int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
158{
159 return xenstore_read_int(xendev->fe, node, ival);
160}
161
Stefano Stabellini4aba9eb2013-12-18 19:17:31 +0000162int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node, uint64_t *uval)
163{
164 return xenstore_read_uint64(xendev->fe, node, uval);
165}
166
aliguorid94f9482009-04-22 15:19:15 +0000167/* ------------------------------------------------------------- */
168
169const char *xenbus_strstate(enum xenbus_state state)
170{
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100171 static const char *const name[] = {
172 [ XenbusStateUnknown ] = "Unknown",
173 [ XenbusStateInitialising ] = "Initialising",
174 [ XenbusStateInitWait ] = "InitWait",
175 [ XenbusStateInitialised ] = "Initialised",
176 [ XenbusStateConnected ] = "Connected",
177 [ XenbusStateClosing ] = "Closing",
178 [ XenbusStateClosed ] = "Closed",
179 };
180 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
aliguorid94f9482009-04-22 15:19:15 +0000181}
182
183int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
184{
185 int rc;
186
187 rc = xenstore_write_be_int(xendev, "state", state);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100188 if (rc < 0) {
189 return rc;
190 }
aliguorid94f9482009-04-22 15:19:15 +0000191 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100192 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
aliguorid94f9482009-04-22 15:19:15 +0000193 xendev->be_state = state;
194 return 0;
195}
196
197/* ------------------------------------------------------------- */
198
199struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
200{
201 struct XenDevice *xendev;
202
Blue Swirl72cf2d42009-09-12 07:36:22 +0000203 QTAILQ_FOREACH(xendev, &xendevs, next) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100204 if (xendev->dom != dom) {
205 continue;
206 }
207 if (xendev->dev != dev) {
208 continue;
209 }
210 if (strcmp(xendev->type, type) != 0) {
211 continue;
212 }
213 return xendev;
aliguorid94f9482009-04-22 15:19:15 +0000214 }
215 return NULL;
216}
217
218/*
219 * get xen backend device, allocate a new one if it doesn't exist.
220 */
221static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
222 struct XenDevOps *ops)
223{
224 struct XenDevice *xendev;
aliguorid94f9482009-04-22 15:19:15 +0000225
226 xendev = xen_be_find_xendev(type, dom, dev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100227 if (xendev) {
228 return xendev;
229 }
aliguorid94f9482009-04-22 15:19:15 +0000230
231 /* init new xendev */
Anthony Liguori7267c092011-08-20 22:09:37 -0500232 xendev = g_malloc0(ops->size);
aliguorid94f9482009-04-22 15:19:15 +0000233 xendev->type = type;
234 xendev->dom = dom;
235 xendev->dev = dev;
236 xendev->ops = ops;
237
Roger Pau Monné33876df2013-10-10 14:25:52 +0000238 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
239 xendev->type, xendev->dom, xendev->dev);
aliguorid94f9482009-04-22 15:19:15 +0000240 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100241 xendev->type, xendev->dev);
aliguorid94f9482009-04-22 15:19:15 +0000242
243 xendev->debug = debug;
244 xendev->local_port = -1;
245
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000246 xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
247 if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100248 xen_be_printf(NULL, 0, "can't open evtchn device\n");
Anthony Liguori7267c092011-08-20 22:09:37 -0500249 g_free(xendev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100250 return NULL;
aliguorid94f9482009-04-22 15:19:15 +0000251 }
252 fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
253
254 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000255 xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
256 if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100257 xen_be_printf(NULL, 0, "can't open gnttab device\n");
258 xc_evtchn_close(xendev->evtchndev);
Anthony Liguori7267c092011-08-20 22:09:37 -0500259 g_free(xendev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100260 return NULL;
261 }
aliguorid94f9482009-04-22 15:19:15 +0000262 } else {
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000263 xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
aliguorid94f9482009-04-22 15:19:15 +0000264 }
265
Blue Swirl72cf2d42009-09-12 07:36:22 +0000266 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
aliguorid94f9482009-04-22 15:19:15 +0000267
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100268 if (xendev->ops->alloc) {
269 xendev->ops->alloc(xendev);
270 }
aliguorid94f9482009-04-22 15:19:15 +0000271
272 return xendev;
273}
274
275/*
276 * release xen backend device.
277 */
278static struct XenDevice *xen_be_del_xendev(int dom, int dev)
279{
280 struct XenDevice *xendev, *xnext;
281
282 /*
Blue Swirl72cf2d42009-09-12 07:36:22 +0000283 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
aliguorid94f9482009-04-22 15:19:15 +0000284 * we save the next pointer in xnext because we might free xendev.
285 */
286 xnext = xendevs.tqh_first;
287 while (xnext) {
288 xendev = xnext;
289 xnext = xendev->next.tqe_next;
290
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100291 if (xendev->dom != dom) {
292 continue;
293 }
294 if (xendev->dev != dev && dev != -1) {
295 continue;
296 }
aliguorid94f9482009-04-22 15:19:15 +0000297
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100298 if (xendev->ops->free) {
299 xendev->ops->free(xendev);
300 }
aliguorid94f9482009-04-22 15:19:15 +0000301
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100302 if (xendev->fe) {
303 char token[XEN_BUFSIZE];
304 snprintf(token, sizeof(token), "fe:%p", xendev);
305 xs_unwatch(xenstore, xendev->fe, token);
Anthony Liguori7267c092011-08-20 22:09:37 -0500306 g_free(xendev->fe);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100307 }
aliguorid94f9482009-04-22 15:19:15 +0000308
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000309 if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100310 xc_evtchn_close(xendev->evtchndev);
311 }
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000312 if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100313 xc_gnttab_close(xendev->gnttabdev);
314 }
aliguorid94f9482009-04-22 15:19:15 +0000315
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100316 QTAILQ_REMOVE(&xendevs, xendev, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500317 g_free(xendev);
aliguorid94f9482009-04-22 15:19:15 +0000318 }
319 return NULL;
320}
321
322/*
323 * Sync internal data structures on xenstore updates.
324 * Node specifies the changed field. node = NULL means
325 * update all fields (used for initialization).
326 */
327static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
328{
329 if (node == NULL || strcmp(node, "online") == 0) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100330 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
331 xendev->online = 0;
332 }
aliguorid94f9482009-04-22 15:19:15 +0000333 }
334
335 if (node) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100336 xen_be_printf(xendev, 2, "backend update: %s\n", node);
337 if (xendev->ops->backend_changed) {
338 xendev->ops->backend_changed(xendev, node);
339 }
aliguorid94f9482009-04-22 15:19:15 +0000340 }
341}
342
343static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
344{
345 int fe_state;
346
347 if (node == NULL || strcmp(node, "state") == 0) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100348 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
349 fe_state = XenbusStateUnknown;
350 }
351 if (xendev->fe_state != fe_state) {
352 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
353 xenbus_strstate(xendev->fe_state),
354 xenbus_strstate(fe_state));
355 }
356 xendev->fe_state = fe_state;
aliguorid94f9482009-04-22 15:19:15 +0000357 }
358 if (node == NULL || strcmp(node, "protocol") == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500359 g_free(xendev->protocol);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100360 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
361 if (xendev->protocol) {
362 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
363 }
aliguorid94f9482009-04-22 15:19:15 +0000364 }
365
366 if (node) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100367 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
368 if (xendev->ops->frontend_changed) {
369 xendev->ops->frontend_changed(xendev, node);
370 }
aliguorid94f9482009-04-22 15:19:15 +0000371 }
372}
373
374/* ------------------------------------------------------------- */
375/* Check for possible state transitions and perform them. */
376
377/*
378 * Initial xendev setup. Read frontend path, register watch for it.
379 * Should succeed once xend finished setting up the backend device.
380 *
381 * Also sets initial state (-> Initializing) when done. Which
382 * only affects the xendev->be_state variable as xenbus should
383 * already be put into that state by xend.
384 */
385static int xen_be_try_setup(struct XenDevice *xendev)
386{
387 char token[XEN_BUFSIZE];
388 int be_state;
389
390 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100391 xen_be_printf(xendev, 0, "reading backend state failed\n");
392 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000393 }
394
395 if (be_state != XenbusStateInitialising) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100396 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
397 xenbus_strstate(be_state));
398 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000399 }
400
401 xendev->fe = xenstore_read_be_str(xendev, "frontend");
402 if (xendev->fe == NULL) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100403 xen_be_printf(xendev, 0, "reading frontend path failed\n");
404 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000405 }
406
407 /* setup frontend watch */
408 snprintf(token, sizeof(token), "fe:%p", xendev);
409 if (!xs_watch(xenstore, xendev->fe, token)) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100410 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
411 xendev->fe);
412 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000413 }
414 xen_be_set_state(xendev, XenbusStateInitialising);
415
416 xen_be_backend_changed(xendev, NULL);
417 xen_be_frontend_changed(xendev, NULL);
418 return 0;
419}
420
421/*
422 * Try initialize xendev. Prepare everything the backend can do
423 * without synchronizing with the frontend. Fakes hotplug-status. No
424 * hotplug involved here because this is about userspace drivers, thus
425 * there are kernel backend devices which could invoke hotplug.
426 *
427 * Goes to InitWait on success.
428 */
429static int xen_be_try_init(struct XenDevice *xendev)
430{
431 int rc = 0;
432
433 if (!xendev->online) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100434 xen_be_printf(xendev, 1, "not online\n");
435 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000436 }
437
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100438 if (xendev->ops->init) {
439 rc = xendev->ops->init(xendev);
440 }
aliguorid94f9482009-04-22 15:19:15 +0000441 if (rc != 0) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100442 xen_be_printf(xendev, 1, "init() failed\n");
443 return rc;
aliguorid94f9482009-04-22 15:19:15 +0000444 }
445
446 xenstore_write_be_str(xendev, "hotplug-status", "connected");
447 xen_be_set_state(xendev, XenbusStateInitWait);
448 return 0;
449}
450
451/*
John Haxby384087b2011-06-17 12:15:35 +0000452 * Try to initialise xendev. Depends on the frontend being ready
aliguorid94f9482009-04-22 15:19:15 +0000453 * for it (shared ring and evtchn info in xenstore, state being
454 * Initialised or Connected).
455 *
456 * Goes to Connected on success.
457 */
John Haxby384087b2011-06-17 12:15:35 +0000458static int xen_be_try_initialise(struct XenDevice *xendev)
aliguorid94f9482009-04-22 15:19:15 +0000459{
460 int rc = 0;
461
462 if (xendev->fe_state != XenbusStateInitialised &&
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100463 xendev->fe_state != XenbusStateConnected) {
464 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
465 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
466 } else {
467 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
468 return -1;
469 }
aliguorid94f9482009-04-22 15:19:15 +0000470 }
471
John Haxby384087b2011-06-17 12:15:35 +0000472 if (xendev->ops->initialise) {
473 rc = xendev->ops->initialise(xendev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100474 }
aliguorid94f9482009-04-22 15:19:15 +0000475 if (rc != 0) {
John Haxby384087b2011-06-17 12:15:35 +0000476 xen_be_printf(xendev, 0, "initialise() failed\n");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100477 return rc;
aliguorid94f9482009-04-22 15:19:15 +0000478 }
479
480 xen_be_set_state(xendev, XenbusStateConnected);
481 return 0;
482}
483
484/*
John Haxby384087b2011-06-17 12:15:35 +0000485 * Try to let xendev know that it is connected. Depends on the
486 * frontend being Connected. Note that this may be called more
487 * than once since the backend state is not modified.
488 */
489static void xen_be_try_connected(struct XenDevice *xendev)
490{
491 if (!xendev->ops->connected) {
492 return;
493 }
494
495 if (xendev->fe_state != XenbusStateConnected) {
496 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
497 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
498 } else {
499 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
500 return;
501 }
502 }
503
504 xendev->ops->connected(xendev);
505}
506
507/*
aliguorid94f9482009-04-22 15:19:15 +0000508 * Teardown connection.
509 *
510 * Goes to Closed when done.
511 */
512static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
513{
514 if (xendev->be_state != XenbusStateClosing &&
515 xendev->be_state != XenbusStateClosed &&
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100516 xendev->ops->disconnect) {
517 xendev->ops->disconnect(xendev);
518 }
519 if (xendev->be_state != state) {
aliguorid94f9482009-04-22 15:19:15 +0000520 xen_be_set_state(xendev, state);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100521 }
aliguorid94f9482009-04-22 15:19:15 +0000522}
523
524/*
525 * Try to reset xendev, for reconnection by another frontend instance.
526 */
527static int xen_be_try_reset(struct XenDevice *xendev)
528{
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100529 if (xendev->fe_state != XenbusStateInitialising) {
aliguorid94f9482009-04-22 15:19:15 +0000530 return -1;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100531 }
aliguorid94f9482009-04-22 15:19:15 +0000532
533 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
534 xen_be_set_state(xendev, XenbusStateInitialising);
535 return 0;
536}
537
538/*
539 * state change dispatcher function
540 */
541void xen_be_check_state(struct XenDevice *xendev)
542{
543 int rc = 0;
544
545 /* frontend may request shutdown from almost anywhere */
546 if (xendev->fe_state == XenbusStateClosing ||
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100547 xendev->fe_state == XenbusStateClosed) {
548 xen_be_disconnect(xendev, xendev->fe_state);
549 return;
aliguorid94f9482009-04-22 15:19:15 +0000550 }
551
552 /* check for possible backend state transitions */
553 for (;;) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100554 switch (xendev->be_state) {
555 case XenbusStateUnknown:
556 rc = xen_be_try_setup(xendev);
557 break;
558 case XenbusStateInitialising:
559 rc = xen_be_try_init(xendev);
560 break;
561 case XenbusStateInitWait:
John Haxby384087b2011-06-17 12:15:35 +0000562 rc = xen_be_try_initialise(xendev);
563 break;
564 case XenbusStateConnected:
565 /* xendev->be_state doesn't change */
566 xen_be_try_connected(xendev);
567 rc = -1;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100568 break;
aliguorid94f9482009-04-22 15:19:15 +0000569 case XenbusStateClosed:
570 rc = xen_be_try_reset(xendev);
571 break;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100572 default:
573 rc = -1;
574 }
575 if (rc != 0) {
576 break;
577 }
aliguorid94f9482009-04-22 15:19:15 +0000578 }
579}
580
581/* ------------------------------------------------------------- */
582
583static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
584{
585 struct XenDevice *xendev;
586 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
Roger Pau Monné33876df2013-10-10 14:25:52 +0000587 char **dev = NULL;
aliguorid94f9482009-04-22 15:19:15 +0000588 unsigned int cdev, j;
589
590 /* setup watch */
aliguorid94f9482009-04-22 15:19:15 +0000591 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
Roger Pau Monné33876df2013-10-10 14:25:52 +0000592 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
aliguorid94f9482009-04-22 15:19:15 +0000593 if (!xs_watch(xenstore, path, token)) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100594 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
595 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000596 }
597
598 /* look for backends */
599 dev = xs_directory(xenstore, 0, path, &cdev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100600 if (!dev) {
601 return 0;
602 }
aliguorid94f9482009-04-22 15:19:15 +0000603 for (j = 0; j < cdev; j++) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100604 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
605 if (xendev == NULL) {
606 continue;
607 }
608 xen_be_check_state(xendev);
aliguorid94f9482009-04-22 15:19:15 +0000609 }
610 free(dev);
611 return 0;
612}
613
614static void xenstore_update_be(char *watch, char *type, int dom,
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100615 struct XenDevOps *ops)
aliguorid94f9482009-04-22 15:19:15 +0000616{
617 struct XenDevice *xendev;
Roger Pau Monné33876df2013-10-10 14:25:52 +0000618 char path[XEN_BUFSIZE], *bepath;
aliguorid94f9482009-04-22 15:19:15 +0000619 unsigned int len, dev;
620
Roger Pau Monné33876df2013-10-10 14:25:52 +0000621 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100622 if (strncmp(path, watch, len) != 0) {
623 return;
aliguorid94f9482009-04-22 15:19:15 +0000624 }
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100625 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
626 strcpy(path, "");
627 if (sscanf(watch+len, "/%u", &dev) != 1) {
628 dev = -1;
629 }
630 }
631 if (dev == -1) {
632 return;
633 }
aliguorid94f9482009-04-22 15:19:15 +0000634
aliguorid94f9482009-04-22 15:19:15 +0000635 xendev = xen_be_get_xendev(type, dom, dev, ops);
636 if (xendev != NULL) {
Stefano Stabellini77ba8fe2012-03-30 14:33:03 +0000637 bepath = xs_read(xenstore, 0, xendev->be, &len);
638 if (bepath == NULL) {
639 xen_be_del_xendev(dom, dev);
640 } else {
641 free(bepath);
642 xen_be_backend_changed(xendev, path);
643 xen_be_check_state(xendev);
644 }
aliguorid94f9482009-04-22 15:19:15 +0000645 }
646}
647
648static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
649{
650 char *node;
651 unsigned int len;
652
653 len = strlen(xendev->fe);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100654 if (strncmp(xendev->fe, watch, len) != 0) {
655 return;
656 }
657 if (watch[len] != '/') {
658 return;
659 }
aliguorid94f9482009-04-22 15:19:15 +0000660 node = watch + len + 1;
661
662 xen_be_frontend_changed(xendev, node);
663 xen_be_check_state(xendev);
664}
665
666static void xenstore_update(void *unused)
667{
668 char **vec = NULL;
669 intptr_t type, ops, ptr;
670 unsigned int dom, count;
671
672 vec = xs_read_watch(xenstore, &count);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100673 if (vec == NULL) {
674 goto cleanup;
675 }
aliguorid94f9482009-04-22 15:19:15 +0000676
677 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100678 &type, &dom, &ops) == 3) {
679 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
680 }
681 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
682 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
683 }
aliguorid94f9482009-04-22 15:19:15 +0000684
685cleanup:
Jean-Christophe DUBOIS834fac72009-11-15 19:18:19 +0100686 free(vec);
aliguorid94f9482009-04-22 15:19:15 +0000687}
688
689static void xen_be_evtchn_event(void *opaque)
690{
691 struct XenDevice *xendev = opaque;
692 evtchn_port_t port;
693
694 port = xc_evtchn_pending(xendev->evtchndev);
695 if (port != xendev->local_port) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100696 xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
697 port, xendev->local_port);
698 return;
aliguorid94f9482009-04-22 15:19:15 +0000699 }
700 xc_evtchn_unmask(xendev->evtchndev, port);
701
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100702 if (xendev->ops->event) {
703 xendev->ops->event(xendev);
704 }
aliguorid94f9482009-04-22 15:19:15 +0000705}
706
707/* -------------------------------------------------------------------- */
708
709int xen_be_init(void)
710{
711 xenstore = xs_daemon_open();
712 if (!xenstore) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100713 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
714 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000715 }
716
Fam Zheng6b5166f2015-06-04 14:45:22 +0800717 qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
aliguorid94f9482009-04-22 15:19:15 +0000718
Anthony PERARDd5b93dd2011-02-25 16:20:34 +0000719 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
Anthony PERARD3285cf42010-08-19 12:27:56 +0100720 /* Check if xen_init() have been called */
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100721 goto err;
aliguorid94f9482009-04-22 15:19:15 +0000722 }
723 return 0;
724
725err:
726 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
727 xs_daemon_close(xenstore);
728 xenstore = NULL;
729
730 return -1;
731}
732
733int xen_be_register(const char *type, struct XenDevOps *ops)
734{
735 return xenstore_scan(type, xen_domid, ops);
736}
737
738int xen_be_bind_evtchn(struct XenDevice *xendev)
739{
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100740 if (xendev->local_port != -1) {
741 return 0;
742 }
aliguorid94f9482009-04-22 15:19:15 +0000743 xendev->local_port = xc_evtchn_bind_interdomain
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100744 (xendev->evtchndev, xendev->dom, xendev->remote_port);
aliguorid94f9482009-04-22 15:19:15 +0000745 if (xendev->local_port == -1) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100746 xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
747 return -1;
aliguorid94f9482009-04-22 15:19:15 +0000748 }
749 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
750 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100751 xen_be_evtchn_event, NULL, xendev);
aliguorid94f9482009-04-22 15:19:15 +0000752 return 0;
753}
754
755void xen_be_unbind_evtchn(struct XenDevice *xendev)
756{
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100757 if (xendev->local_port == -1) {
758 return;
759 }
aliguorid94f9482009-04-22 15:19:15 +0000760 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
761 xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
762 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
763 xendev->local_port = -1;
764}
765
766int xen_be_send_notify(struct XenDevice *xendev)
767{
768 return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
769}
770
771/*
772 * msg_level:
773 * 0 == errors (stderr + logfile).
774 * 1 == informative debug messages (logfile only).
775 * 2 == noisy debug messages (logfile only).
776 * 3 == will flood your log (logfile only).
777 */
778void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
779{
780 va_list args;
781
782 if (xendev) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100783 if (msg_level > xendev->debug) {
aliguorid94f9482009-04-22 15:19:15 +0000784 return;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100785 }
aliguorid94f9482009-04-22 15:19:15 +0000786 qemu_log("xen be: %s: ", xendev->name);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100787 if (msg_level == 0) {
aliguorid94f9482009-04-22 15:19:15 +0000788 fprintf(stderr, "xen be: %s: ", xendev->name);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100789 }
aliguorid94f9482009-04-22 15:19:15 +0000790 } else {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100791 if (msg_level > debug) {
aliguorid94f9482009-04-22 15:19:15 +0000792 return;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100793 }
aliguorid94f9482009-04-22 15:19:15 +0000794 qemu_log("xen be core: ");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100795 if (msg_level == 0) {
aliguorid94f9482009-04-22 15:19:15 +0000796 fprintf(stderr, "xen be core: ");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100797 }
aliguorid94f9482009-04-22 15:19:15 +0000798 }
799 va_start(args, fmt);
800 qemu_log_vprintf(fmt, args);
801 va_end(args);
802 if (msg_level == 0) {
803 va_start(args, fmt);
804 vfprintf(stderr, fmt, args);
805 va_end(args);
806 }
807 qemu_log_flush();
808}