root / hw / xen / xen_backend.c @ 49ab747f
History | View | Annotate | Download (21.5 kB)
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 <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 |
|
37 |
#include "hw/hw.h" |
38 |
#include "char/char.h" |
39 |
#include "qemu/log.h" |
40 |
#include "hw/xen/xen_backend.h" |
41 |
|
42 |
#include <xen/grant_table.h> |
43 |
|
44 |
/* ------------------------------------------------------------- */
|
45 |
|
46 |
/* public */
|
47 |
XenXC xen_xc = XC_HANDLER_INITIAL_VALUE; |
48 |
XenGnttab xen_xcg = XC_HANDLER_INITIAL_VALUE; |
49 |
struct xs_handle *xenstore = NULL; |
50 |
const char *xen_protocol; |
51 |
|
52 |
/* private */
|
53 |
static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
|
54 |
static int debug = 0; |
55 |
|
56 |
/* ------------------------------------------------------------- */
|
57 |
|
58 |
int xenstore_write_str(const char *base, const char *node, const char *val) |
59 |
{ |
60 |
char abspath[XEN_BUFSIZE];
|
61 |
|
62 |
snprintf(abspath, sizeof(abspath), "%s/%s", base, node); |
63 |
if (!xs_write(xenstore, 0, abspath, val, strlen(val))) { |
64 |
return -1; |
65 |
} |
66 |
return 0; |
67 |
} |
68 |
|
69 |
char *xenstore_read_str(const char *base, const char *node) |
70 |
{ |
71 |
char abspath[XEN_BUFSIZE];
|
72 |
unsigned int len; |
73 |
char *str, *ret = NULL; |
74 |
|
75 |
snprintf(abspath, sizeof(abspath), "%s/%s", base, node); |
76 |
str = xs_read(xenstore, 0, abspath, &len);
|
77 |
if (str != NULL) { |
78 |
/* move to qemu-allocated memory to make sure
|
79 |
* callers can savely g_free() stuff. */
|
80 |
ret = g_strdup(str); |
81 |
free(str); |
82 |
} |
83 |
return ret;
|
84 |
} |
85 |
|
86 |
int xenstore_write_int(const char *base, const char *node, int ival) |
87 |
{ |
88 |
char val[12]; |
89 |
|
90 |
snprintf(val, sizeof(val), "%d", ival); |
91 |
return xenstore_write_str(base, node, val);
|
92 |
} |
93 |
|
94 |
int xenstore_write_int64(const char *base, const char *node, int64_t ival) |
95 |
{ |
96 |
char val[21]; |
97 |
|
98 |
snprintf(val, sizeof(val), "%"PRId64, ival); |
99 |
return xenstore_write_str(base, node, val);
|
100 |
} |
101 |
|
102 |
int xenstore_read_int(const char *base, const char *node, int *ival) |
103 |
{ |
104 |
char *val;
|
105 |
int rc = -1; |
106 |
|
107 |
val = xenstore_read_str(base, node); |
108 |
if (val && 1 == sscanf(val, "%d", ival)) { |
109 |
rc = 0;
|
110 |
} |
111 |
g_free(val); |
112 |
return rc;
|
113 |
} |
114 |
|
115 |
int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val) |
116 |
{ |
117 |
return xenstore_write_str(xendev->be, node, val);
|
118 |
} |
119 |
|
120 |
int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival) |
121 |
{ |
122 |
return xenstore_write_int(xendev->be, node, ival);
|
123 |
} |
124 |
|
125 |
int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival) |
126 |
{ |
127 |
return xenstore_write_int64(xendev->be, node, ival);
|
128 |
} |
129 |
|
130 |
char *xenstore_read_be_str(struct XenDevice *xendev, const char *node) |
131 |
{ |
132 |
return xenstore_read_str(xendev->be, node);
|
133 |
} |
134 |
|
135 |
int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival) |
136 |
{ |
137 |
return xenstore_read_int(xendev->be, node, ival);
|
138 |
} |
139 |
|
140 |
char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node) |
141 |
{ |
142 |
return xenstore_read_str(xendev->fe, node);
|
143 |
} |
144 |
|
145 |
int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival) |
146 |
{ |
147 |
return xenstore_read_int(xendev->fe, node, ival);
|
148 |
} |
149 |
|
150 |
/* ------------------------------------------------------------- */
|
151 |
|
152 |
const char *xenbus_strstate(enum xenbus_state state) |
153 |
{ |
154 |
static const char *const name[] = { |
155 |
[ XenbusStateUnknown ] = "Unknown",
|
156 |
[ XenbusStateInitialising ] = "Initialising",
|
157 |
[ XenbusStateInitWait ] = "InitWait",
|
158 |
[ XenbusStateInitialised ] = "Initialised",
|
159 |
[ XenbusStateConnected ] = "Connected",
|
160 |
[ XenbusStateClosing ] = "Closing",
|
161 |
[ XenbusStateClosed ] = "Closed",
|
162 |
}; |
163 |
return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID"; |
164 |
} |
165 |
|
166 |
int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state) |
167 |
{ |
168 |
int rc;
|
169 |
|
170 |
rc = xenstore_write_be_int(xendev, "state", state);
|
171 |
if (rc < 0) { |
172 |
return rc;
|
173 |
} |
174 |
xen_be_printf(xendev, 1, "backend state: %s -> %s\n", |
175 |
xenbus_strstate(xendev->be_state), xenbus_strstate(state)); |
176 |
xendev->be_state = state; |
177 |
return 0; |
178 |
} |
179 |
|
180 |
/* ------------------------------------------------------------- */
|
181 |
|
182 |
struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev) |
183 |
{ |
184 |
struct XenDevice *xendev;
|
185 |
|
186 |
QTAILQ_FOREACH(xendev, &xendevs, next) { |
187 |
if (xendev->dom != dom) {
|
188 |
continue;
|
189 |
} |
190 |
if (xendev->dev != dev) {
|
191 |
continue;
|
192 |
} |
193 |
if (strcmp(xendev->type, type) != 0) { |
194 |
continue;
|
195 |
} |
196 |
return xendev;
|
197 |
} |
198 |
return NULL; |
199 |
} |
200 |
|
201 |
/*
|
202 |
* get xen backend device, allocate a new one if it doesn't exist.
|
203 |
*/
|
204 |
static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev, |
205 |
struct XenDevOps *ops)
|
206 |
{ |
207 |
struct XenDevice *xendev;
|
208 |
char *dom0;
|
209 |
|
210 |
xendev = xen_be_find_xendev(type, dom, dev); |
211 |
if (xendev) {
|
212 |
return xendev;
|
213 |
} |
214 |
|
215 |
/* init new xendev */
|
216 |
xendev = g_malloc0(ops->size); |
217 |
xendev->type = type; |
218 |
xendev->dom = dom; |
219 |
xendev->dev = dev; |
220 |
xendev->ops = ops; |
221 |
|
222 |
dom0 = xs_get_domain_path(xenstore, 0);
|
223 |
snprintf(xendev->be, sizeof(xendev->be), "%s/backend/%s/%d/%d", |
224 |
dom0, xendev->type, xendev->dom, xendev->dev); |
225 |
snprintf(xendev->name, sizeof(xendev->name), "%s-%d", |
226 |
xendev->type, xendev->dev); |
227 |
free(dom0); |
228 |
|
229 |
xendev->debug = debug; |
230 |
xendev->local_port = -1;
|
231 |
|
232 |
xendev->evtchndev = xen_xc_evtchn_open(NULL, 0); |
233 |
if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
|
234 |
xen_be_printf(NULL, 0, "can't open evtchn device\n"); |
235 |
g_free(xendev); |
236 |
return NULL; |
237 |
} |
238 |
fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC); |
239 |
|
240 |
if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
|
241 |
xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0); |
242 |
if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
|
243 |
xen_be_printf(NULL, 0, "can't open gnttab device\n"); |
244 |
xc_evtchn_close(xendev->evtchndev); |
245 |
g_free(xendev); |
246 |
return NULL; |
247 |
} |
248 |
} else {
|
249 |
xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE; |
250 |
} |
251 |
|
252 |
QTAILQ_INSERT_TAIL(&xendevs, xendev, next); |
253 |
|
254 |
if (xendev->ops->alloc) {
|
255 |
xendev->ops->alloc(xendev); |
256 |
} |
257 |
|
258 |
return xendev;
|
259 |
} |
260 |
|
261 |
/*
|
262 |
* release xen backend device.
|
263 |
*/
|
264 |
static struct XenDevice *xen_be_del_xendev(int dom, int dev) |
265 |
{ |
266 |
struct XenDevice *xendev, *xnext;
|
267 |
|
268 |
/*
|
269 |
* This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
|
270 |
* we save the next pointer in xnext because we might free xendev.
|
271 |
*/
|
272 |
xnext = xendevs.tqh_first; |
273 |
while (xnext) {
|
274 |
xendev = xnext; |
275 |
xnext = xendev->next.tqe_next; |
276 |
|
277 |
if (xendev->dom != dom) {
|
278 |
continue;
|
279 |
} |
280 |
if (xendev->dev != dev && dev != -1) { |
281 |
continue;
|
282 |
} |
283 |
|
284 |
if (xendev->ops->free) {
|
285 |
xendev->ops->free(xendev); |
286 |
} |
287 |
|
288 |
if (xendev->fe) {
|
289 |
char token[XEN_BUFSIZE];
|
290 |
snprintf(token, sizeof(token), "fe:%p", xendev); |
291 |
xs_unwatch(xenstore, xendev->fe, token); |
292 |
g_free(xendev->fe); |
293 |
} |
294 |
|
295 |
if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
|
296 |
xc_evtchn_close(xendev->evtchndev); |
297 |
} |
298 |
if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
|
299 |
xc_gnttab_close(xendev->gnttabdev); |
300 |
} |
301 |
|
302 |
QTAILQ_REMOVE(&xendevs, xendev, next); |
303 |
g_free(xendev); |
304 |
} |
305 |
return NULL; |
306 |
} |
307 |
|
308 |
/*
|
309 |
* Sync internal data structures on xenstore updates.
|
310 |
* Node specifies the changed field. node = NULL means
|
311 |
* update all fields (used for initialization).
|
312 |
*/
|
313 |
static void xen_be_backend_changed(struct XenDevice *xendev, const char *node) |
314 |
{ |
315 |
if (node == NULL || strcmp(node, "online") == 0) { |
316 |
if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) { |
317 |
xendev->online = 0;
|
318 |
} |
319 |
} |
320 |
|
321 |
if (node) {
|
322 |
xen_be_printf(xendev, 2, "backend update: %s\n", node); |
323 |
if (xendev->ops->backend_changed) {
|
324 |
xendev->ops->backend_changed(xendev, node); |
325 |
} |
326 |
} |
327 |
} |
328 |
|
329 |
static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node) |
330 |
{ |
331 |
int fe_state;
|
332 |
|
333 |
if (node == NULL || strcmp(node, "state") == 0) { |
334 |
if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) { |
335 |
fe_state = XenbusStateUnknown; |
336 |
} |
337 |
if (xendev->fe_state != fe_state) {
|
338 |
xen_be_printf(xendev, 1, "frontend state: %s -> %s\n", |
339 |
xenbus_strstate(xendev->fe_state), |
340 |
xenbus_strstate(fe_state)); |
341 |
} |
342 |
xendev->fe_state = fe_state; |
343 |
} |
344 |
if (node == NULL || strcmp(node, "protocol") == 0) { |
345 |
g_free(xendev->protocol); |
346 |
xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
|
347 |
if (xendev->protocol) {
|
348 |
xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol); |
349 |
} |
350 |
} |
351 |
|
352 |
if (node) {
|
353 |
xen_be_printf(xendev, 2, "frontend update: %s\n", node); |
354 |
if (xendev->ops->frontend_changed) {
|
355 |
xendev->ops->frontend_changed(xendev, node); |
356 |
} |
357 |
} |
358 |
} |
359 |
|
360 |
/* ------------------------------------------------------------- */
|
361 |
/* Check for possible state transitions and perform them. */
|
362 |
|
363 |
/*
|
364 |
* Initial xendev setup. Read frontend path, register watch for it.
|
365 |
* Should succeed once xend finished setting up the backend device.
|
366 |
*
|
367 |
* Also sets initial state (-> Initializing) when done. Which
|
368 |
* only affects the xendev->be_state variable as xenbus should
|
369 |
* already be put into that state by xend.
|
370 |
*/
|
371 |
static int xen_be_try_setup(struct XenDevice *xendev) |
372 |
{ |
373 |
char token[XEN_BUFSIZE];
|
374 |
int be_state;
|
375 |
|
376 |
if (xenstore_read_be_int(xendev, "state", &be_state) == -1) { |
377 |
xen_be_printf(xendev, 0, "reading backend state failed\n"); |
378 |
return -1; |
379 |
} |
380 |
|
381 |
if (be_state != XenbusStateInitialising) {
|
382 |
xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n", |
383 |
xenbus_strstate(be_state)); |
384 |
return -1; |
385 |
} |
386 |
|
387 |
xendev->fe = xenstore_read_be_str(xendev, "frontend");
|
388 |
if (xendev->fe == NULL) { |
389 |
xen_be_printf(xendev, 0, "reading frontend path failed\n"); |
390 |
return -1; |
391 |
} |
392 |
|
393 |
/* setup frontend watch */
|
394 |
snprintf(token, sizeof(token), "fe:%p", xendev); |
395 |
if (!xs_watch(xenstore, xendev->fe, token)) {
|
396 |
xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n", |
397 |
xendev->fe); |
398 |
return -1; |
399 |
} |
400 |
xen_be_set_state(xendev, XenbusStateInitialising); |
401 |
|
402 |
xen_be_backend_changed(xendev, NULL);
|
403 |
xen_be_frontend_changed(xendev, NULL);
|
404 |
return 0; |
405 |
} |
406 |
|
407 |
/*
|
408 |
* Try initialize xendev. Prepare everything the backend can do
|
409 |
* without synchronizing with the frontend. Fakes hotplug-status. No
|
410 |
* hotplug involved here because this is about userspace drivers, thus
|
411 |
* there are kernel backend devices which could invoke hotplug.
|
412 |
*
|
413 |
* Goes to InitWait on success.
|
414 |
*/
|
415 |
static int xen_be_try_init(struct XenDevice *xendev) |
416 |
{ |
417 |
int rc = 0; |
418 |
|
419 |
if (!xendev->online) {
|
420 |
xen_be_printf(xendev, 1, "not online\n"); |
421 |
return -1; |
422 |
} |
423 |
|
424 |
if (xendev->ops->init) {
|
425 |
rc = xendev->ops->init(xendev); |
426 |
} |
427 |
if (rc != 0) { |
428 |
xen_be_printf(xendev, 1, "init() failed\n"); |
429 |
return rc;
|
430 |
} |
431 |
|
432 |
xenstore_write_be_str(xendev, "hotplug-status", "connected"); |
433 |
xen_be_set_state(xendev, XenbusStateInitWait); |
434 |
return 0; |
435 |
} |
436 |
|
437 |
/*
|
438 |
* Try to initialise xendev. Depends on the frontend being ready
|
439 |
* for it (shared ring and evtchn info in xenstore, state being
|
440 |
* Initialised or Connected).
|
441 |
*
|
442 |
* Goes to Connected on success.
|
443 |
*/
|
444 |
static int xen_be_try_initialise(struct XenDevice *xendev) |
445 |
{ |
446 |
int rc = 0; |
447 |
|
448 |
if (xendev->fe_state != XenbusStateInitialised &&
|
449 |
xendev->fe_state != XenbusStateConnected) { |
450 |
if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
|
451 |
xen_be_printf(xendev, 2, "frontend not ready, ignoring\n"); |
452 |
} else {
|
453 |
xen_be_printf(xendev, 2, "frontend not ready (yet)\n"); |
454 |
return -1; |
455 |
} |
456 |
} |
457 |
|
458 |
if (xendev->ops->initialise) {
|
459 |
rc = xendev->ops->initialise(xendev); |
460 |
} |
461 |
if (rc != 0) { |
462 |
xen_be_printf(xendev, 0, "initialise() failed\n"); |
463 |
return rc;
|
464 |
} |
465 |
|
466 |
xen_be_set_state(xendev, XenbusStateConnected); |
467 |
return 0; |
468 |
} |
469 |
|
470 |
/*
|
471 |
* Try to let xendev know that it is connected. Depends on the
|
472 |
* frontend being Connected. Note that this may be called more
|
473 |
* than once since the backend state is not modified.
|
474 |
*/
|
475 |
static void xen_be_try_connected(struct XenDevice *xendev) |
476 |
{ |
477 |
if (!xendev->ops->connected) {
|
478 |
return;
|
479 |
} |
480 |
|
481 |
if (xendev->fe_state != XenbusStateConnected) {
|
482 |
if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
|
483 |
xen_be_printf(xendev, 2, "frontend not ready, ignoring\n"); |
484 |
} else {
|
485 |
xen_be_printf(xendev, 2, "frontend not ready (yet)\n"); |
486 |
return;
|
487 |
} |
488 |
} |
489 |
|
490 |
xendev->ops->connected(xendev); |
491 |
} |
492 |
|
493 |
/*
|
494 |
* Teardown connection.
|
495 |
*
|
496 |
* Goes to Closed when done.
|
497 |
*/
|
498 |
static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state) |
499 |
{ |
500 |
if (xendev->be_state != XenbusStateClosing &&
|
501 |
xendev->be_state != XenbusStateClosed && |
502 |
xendev->ops->disconnect) { |
503 |
xendev->ops->disconnect(xendev); |
504 |
} |
505 |
if (xendev->be_state != state) {
|
506 |
xen_be_set_state(xendev, state); |
507 |
} |
508 |
} |
509 |
|
510 |
/*
|
511 |
* Try to reset xendev, for reconnection by another frontend instance.
|
512 |
*/
|
513 |
static int xen_be_try_reset(struct XenDevice *xendev) |
514 |
{ |
515 |
if (xendev->fe_state != XenbusStateInitialising) {
|
516 |
return -1; |
517 |
} |
518 |
|
519 |
xen_be_printf(xendev, 1, "device reset (for re-connect)\n"); |
520 |
xen_be_set_state(xendev, XenbusStateInitialising); |
521 |
return 0; |
522 |
} |
523 |
|
524 |
/*
|
525 |
* state change dispatcher function
|
526 |
*/
|
527 |
void xen_be_check_state(struct XenDevice *xendev) |
528 |
{ |
529 |
int rc = 0; |
530 |
|
531 |
/* frontend may request shutdown from almost anywhere */
|
532 |
if (xendev->fe_state == XenbusStateClosing ||
|
533 |
xendev->fe_state == XenbusStateClosed) { |
534 |
xen_be_disconnect(xendev, xendev->fe_state); |
535 |
return;
|
536 |
} |
537 |
|
538 |
/* check for possible backend state transitions */
|
539 |
for (;;) {
|
540 |
switch (xendev->be_state) {
|
541 |
case XenbusStateUnknown:
|
542 |
rc = xen_be_try_setup(xendev); |
543 |
break;
|
544 |
case XenbusStateInitialising:
|
545 |
rc = xen_be_try_init(xendev); |
546 |
break;
|
547 |
case XenbusStateInitWait:
|
548 |
rc = xen_be_try_initialise(xendev); |
549 |
break;
|
550 |
case XenbusStateConnected:
|
551 |
/* xendev->be_state doesn't change */
|
552 |
xen_be_try_connected(xendev); |
553 |
rc = -1;
|
554 |
break;
|
555 |
case XenbusStateClosed:
|
556 |
rc = xen_be_try_reset(xendev); |
557 |
break;
|
558 |
default:
|
559 |
rc = -1;
|
560 |
} |
561 |
if (rc != 0) { |
562 |
break;
|
563 |
} |
564 |
} |
565 |
} |
566 |
|
567 |
/* ------------------------------------------------------------- */
|
568 |
|
569 |
static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops) |
570 |
{ |
571 |
struct XenDevice *xendev;
|
572 |
char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
|
573 |
char **dev = NULL, *dom0; |
574 |
unsigned int cdev, j; |
575 |
|
576 |
/* setup watch */
|
577 |
dom0 = xs_get_domain_path(xenstore, 0);
|
578 |
snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops); |
579 |
snprintf(path, sizeof(path), "%s/backend/%s/%d", dom0, type, dom); |
580 |
free(dom0); |
581 |
if (!xs_watch(xenstore, path, token)) {
|
582 |
xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path); |
583 |
return -1; |
584 |
} |
585 |
|
586 |
/* look for backends */
|
587 |
dev = xs_directory(xenstore, 0, path, &cdev);
|
588 |
if (!dev) {
|
589 |
return 0; |
590 |
} |
591 |
for (j = 0; j < cdev; j++) { |
592 |
xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops); |
593 |
if (xendev == NULL) { |
594 |
continue;
|
595 |
} |
596 |
xen_be_check_state(xendev); |
597 |
} |
598 |
free(dev); |
599 |
return 0; |
600 |
} |
601 |
|
602 |
static void xenstore_update_be(char *watch, char *type, int dom, |
603 |
struct XenDevOps *ops)
|
604 |
{ |
605 |
struct XenDevice *xendev;
|
606 |
char path[XEN_BUFSIZE], *dom0, *bepath;
|
607 |
unsigned int len, dev; |
608 |
|
609 |
dom0 = xs_get_domain_path(xenstore, 0);
|
610 |
len = snprintf(path, sizeof(path), "%s/backend/%s/%d", dom0, type, dom); |
611 |
free(dom0); |
612 |
if (strncmp(path, watch, len) != 0) { |
613 |
return;
|
614 |
} |
615 |
if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) { |
616 |
strcpy(path, "");
|
617 |
if (sscanf(watch+len, "/%u", &dev) != 1) { |
618 |
dev = -1;
|
619 |
} |
620 |
} |
621 |
if (dev == -1) { |
622 |
return;
|
623 |
} |
624 |
|
625 |
xendev = xen_be_get_xendev(type, dom, dev, ops); |
626 |
if (xendev != NULL) { |
627 |
bepath = xs_read(xenstore, 0, xendev->be, &len);
|
628 |
if (bepath == NULL) { |
629 |
xen_be_del_xendev(dom, dev); |
630 |
} else {
|
631 |
free(bepath); |
632 |
xen_be_backend_changed(xendev, path); |
633 |
xen_be_check_state(xendev); |
634 |
} |
635 |
} |
636 |
} |
637 |
|
638 |
static void xenstore_update_fe(char *watch, struct XenDevice *xendev) |
639 |
{ |
640 |
char *node;
|
641 |
unsigned int len; |
642 |
|
643 |
len = strlen(xendev->fe); |
644 |
if (strncmp(xendev->fe, watch, len) != 0) { |
645 |
return;
|
646 |
} |
647 |
if (watch[len] != '/') { |
648 |
return;
|
649 |
} |
650 |
node = watch + len + 1;
|
651 |
|
652 |
xen_be_frontend_changed(xendev, node); |
653 |
xen_be_check_state(xendev); |
654 |
} |
655 |
|
656 |
static void xenstore_update(void *unused) |
657 |
{ |
658 |
char **vec = NULL; |
659 |
intptr_t type, ops, ptr; |
660 |
unsigned int dom, count; |
661 |
|
662 |
vec = xs_read_watch(xenstore, &count); |
663 |
if (vec == NULL) { |
664 |
goto cleanup;
|
665 |
} |
666 |
|
667 |
if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR, |
668 |
&type, &dom, &ops) == 3) {
|
669 |
xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops); |
670 |
} |
671 |
if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) { |
672 |
xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
|
673 |
} |
674 |
|
675 |
cleanup:
|
676 |
free(vec); |
677 |
} |
678 |
|
679 |
static void xen_be_evtchn_event(void *opaque) |
680 |
{ |
681 |
struct XenDevice *xendev = opaque;
|
682 |
evtchn_port_t port; |
683 |
|
684 |
port = xc_evtchn_pending(xendev->evtchndev); |
685 |
if (port != xendev->local_port) {
|
686 |
xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n", |
687 |
port, xendev->local_port); |
688 |
return;
|
689 |
} |
690 |
xc_evtchn_unmask(xendev->evtchndev, port); |
691 |
|
692 |
if (xendev->ops->event) {
|
693 |
xendev->ops->event(xendev); |
694 |
} |
695 |
} |
696 |
|
697 |
/* -------------------------------------------------------------------- */
|
698 |
|
699 |
int xen_be_init(void) |
700 |
{ |
701 |
xenstore = xs_daemon_open(); |
702 |
if (!xenstore) {
|
703 |
xen_be_printf(NULL, 0, "can't connect to xenstored\n"); |
704 |
return -1; |
705 |
} |
706 |
|
707 |
if (qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL) < 0) { |
708 |
goto err;
|
709 |
} |
710 |
|
711 |
if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
|
712 |
/* Check if xen_init() have been called */
|
713 |
goto err;
|
714 |
} |
715 |
return 0; |
716 |
|
717 |
err:
|
718 |
qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL); |
719 |
xs_daemon_close(xenstore); |
720 |
xenstore = NULL;
|
721 |
|
722 |
return -1; |
723 |
} |
724 |
|
725 |
int xen_be_register(const char *type, struct XenDevOps *ops) |
726 |
{ |
727 |
return xenstore_scan(type, xen_domid, ops);
|
728 |
} |
729 |
|
730 |
int xen_be_bind_evtchn(struct XenDevice *xendev) |
731 |
{ |
732 |
if (xendev->local_port != -1) { |
733 |
return 0; |
734 |
} |
735 |
xendev->local_port = xc_evtchn_bind_interdomain |
736 |
(xendev->evtchndev, xendev->dom, xendev->remote_port); |
737 |
if (xendev->local_port == -1) { |
738 |
xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n"); |
739 |
return -1; |
740 |
} |
741 |
xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port); |
742 |
qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), |
743 |
xen_be_evtchn_event, NULL, xendev);
|
744 |
return 0; |
745 |
} |
746 |
|
747 |
void xen_be_unbind_evtchn(struct XenDevice *xendev) |
748 |
{ |
749 |
if (xendev->local_port == -1) { |
750 |
return;
|
751 |
} |
752 |
qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL); |
753 |
xc_evtchn_unbind(xendev->evtchndev, xendev->local_port); |
754 |
xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port); |
755 |
xendev->local_port = -1;
|
756 |
} |
757 |
|
758 |
int xen_be_send_notify(struct XenDevice *xendev) |
759 |
{ |
760 |
return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
|
761 |
} |
762 |
|
763 |
/*
|
764 |
* msg_level:
|
765 |
* 0 == errors (stderr + logfile).
|
766 |
* 1 == informative debug messages (logfile only).
|
767 |
* 2 == noisy debug messages (logfile only).
|
768 |
* 3 == will flood your log (logfile only).
|
769 |
*/
|
770 |
void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...) |
771 |
{ |
772 |
va_list args; |
773 |
|
774 |
if (xendev) {
|
775 |
if (msg_level > xendev->debug) {
|
776 |
return;
|
777 |
} |
778 |
qemu_log("xen be: %s: ", xendev->name);
|
779 |
if (msg_level == 0) { |
780 |
fprintf(stderr, "xen be: %s: ", xendev->name);
|
781 |
} |
782 |
} else {
|
783 |
if (msg_level > debug) {
|
784 |
return;
|
785 |
} |
786 |
qemu_log("xen be core: ");
|
787 |
if (msg_level == 0) { |
788 |
fprintf(stderr, "xen be core: ");
|
789 |
} |
790 |
} |
791 |
va_start(args, fmt); |
792 |
qemu_log_vprintf(fmt, args); |
793 |
va_end(args); |
794 |
if (msg_level == 0) { |
795 |
va_start(args, fmt); |
796 |
vfprintf(stderr, fmt, args); |
797 |
va_end(args); |
798 |
} |
799 |
qemu_log_flush(); |
800 |
} |