-
Notifications
You must be signed in to change notification settings - Fork 4
/
xs_dev.c
executable file
·534 lines (425 loc) · 12 KB
/
xs_dev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* Helpers to read/write values via Xenbus were borrowed from Qemu:
* hw/xen/xen_pvdev.c
*
* (c) 2008 Gerd Hoffmann <[email protected]>
*
* Copyright (C) 2020 EPAM Systems Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>
*/
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <inttypes.h>
#include <sys/poll.h>
#include "xs_dev.h"
#include "kvm/kvm.h"
static char *xenstore_read_str(struct xs_handle *xsh,
const char *base, const char *node)
{
char abspath[XEN_BUFSIZE];
unsigned int len;
char *str, *ret = NULL;
snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
str = xs_read(xsh, 0, abspath, &len);
if (str != NULL) {
ret = strdup(str);
free(str);
}
return ret;
}
static int xenstore_write_str(struct xs_handle *xsh,
const char *base, const char *node, const char *val)
{
char abspath[XEN_BUFSIZE];
snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
if (!xs_write(xsh, 0, abspath, val, strlen(val))) {
return -1;
}
return 0;
}
static int xenstore_read_int(struct xs_handle *xsh,
const char *base, const char *node, int *ival)
{
char *val;
int rc = -1;
val = xenstore_read_str(xsh, base, node);
if (val && 1 == sscanf(val, "%d", ival)) {
rc = 0;
}
free(val);
return rc;
}
static int xenstore_write_int(struct xs_handle *xsh,
const char *base, const char *node, int ival)
{
char val[12];
snprintf(val, sizeof(val), "%d", ival);
return xenstore_write_str(xsh, base, node, val);
}
int xenstore_read_fe_int(struct xs_dev *dev, const char *node,
int *ival)
{
return xenstore_read_int(dev->xsh, dev->fe, node, ival);
}
char *xenstore_read_fe_str(struct xs_dev *dev, const char *node)
{
return xenstore_read_str(dev->xsh, dev->fe, node);
}
char *xenstore_read_be_str(struct xs_dev *dev, const char *node)
{
return xenstore_read_str(dev->xsh, dev->be, node);
}
int xenstore_read_be_int(struct xs_dev *dev, const char *node,
int *ival)
{
return xenstore_read_int(dev->xsh, dev->be, node, ival);
}
int xenstore_write_be_int(struct xs_dev *dev, const char *node,
int ival)
{
return xenstore_write_int(dev->xsh, dev->be, node, ival);
}
static int xenstore_set_be_state(struct xs_dev *dev, enum xenbus_state state)
{
int rc;
rc = xenstore_write_be_int(dev, "state", state);
if (rc < 0)
return rc;
dev->be_state = state;
return 0;
}
static int xenstore_wait_be_state(struct xs_dev *dev, int awaited)
{
unsigned int num;
int be_state;
char **vec;
awaited |= 1 << XenbusStateUnknown;
while (1) {
if (xenstore_read_be_int(dev, "state", &be_state) == -1)
return -1;
if ((1 << be_state) & awaited)
return be_state;
vec = xs_read_watch(dev->xsh, &num);
if (!vec)
return -1;
free(vec);
}
}
void xenstore_disconnect_dom(struct xs_dev *dev)
{
if (!dev)
return;
xenstore_set_be_state(dev, XenbusStateClosed);
if (dev->fe) {
xs_unwatch(dev->xsh, dev->fe, dev->fe);
free(dev->fe);
dev->fe = NULL;
}
xs_unwatch(dev->xsh, dev->be, dev->be);
xs_rm(dev->xsh, XBT_NULL, dev->be);
pr_info("disconnected from dom%d\n", dev->fe_domid);
dev->fe_domid = 0;
}
uint64_t xenstore_get_dom_mem(struct xs_dev *dev, domid_t domid)
{
char path[XEN_BUFSIZE];
char *memkb_str;
unsigned int len;
uint64_t memkb_val;
snprintf(path, sizeof(path), "/local/domain/%u/memory/static-max", domid);
memkb_str = xs_read(dev->xsh, XBT_NULL, path, &len);
if (!memkb_str)
return 0;
memkb_val = strtoull(memkb_str, NULL, 10);
free(memkb_str);
return memkb_val * 1024;
}
int xenstore_connect_dom(struct xs_dev *dev, domid_t be_domid, domid_t fe_domid,
int (*connected_cb)(void *data), void *data)
{
int state;
if (!dev)
return -1;
dev->be_domid = be_domid;
dev->fe_domid = fe_domid;
dev->connected_cb = connected_cb;
dev->data = data;
snprintf(dev->be, sizeof(dev->be), "backend/%s/%d/%d",
dev->type, dev->fe_domid, dev->devid);
if (xenstore_read_be_int(dev, "state", &state) == -1) {
pr_err("reading backend state failed\n");
goto err;
}
dev->be_state = state;
if (state != XenbusStateInitialising) {
pr_err("initial backend state is wrong (%d)\n", state);
goto err;
}
xenstore_set_be_state(dev, XenbusStateInitWait);
dev->fe = xenstore_read_be_str(dev, "frontend");
if (dev->fe == NULL) {
pr_err("reading frontend path failed\n");
goto err;
}
if (xenstore_read_fe_int(dev, "state", &state) == -1) {
pr_err("reading frontend state failed\n");
goto err;
}
if (state != XenbusStateInitialising) {
pr_err("initial frontend state is wrong (%d)\n", state);
goto err;
}
if (!xs_watch(dev->xsh, dev->be, dev->be)) {
pr_err("watching backend path (%s) failed\n", dev->be);
goto err;
}
if (!xs_watch(dev->xsh, dev->fe, dev->fe)) {
pr_err("watching frontend path (%s) failed\n", dev->fe);
goto err;
}
if (xenstore_wait_be_state(dev, 1 << XenbusStateInitWait) != XenbusStateInitWait) {
pr_err("waiting backend state failed (%d)\n", dev->be_state);
goto err;
}
if (dev->connected_cb && dev->connected_cb(dev->data) < 0)
goto err;
/*
* This is not mandatory as the otherend is not xenbus-aware at all.
* Moreover, this might lead to xl create command to fail.
* libxl expects backend state to be XenbusStateInitWait at the domain
* creation time. So, we cannot switch state to connected here.
*/
/*xenstore_set_be_state(dev, XenbusStateConnected);*/
pr_info("connected to dom%d\n", dev->fe_domid);
return 0;
err:
xenstore_disconnect_dom(dev);
return -1;
}
struct xs_dev *xenstore_create(char *type, char *devid_str)
{
struct xs_dev *dev;
dev = calloc(1, sizeof(struct xs_dev));
if (!dev)
return NULL;
dev->xsh = xs_open(0);
if (!dev->xsh) {
pr_err("failed to make connection to xenstore\n");
free(dev);
return NULL;
}
dev->type = type;
dev->devid_str = devid_str;
snprintf(dev->path, sizeof(dev->path), "backend/%s", dev->type);
return dev;
}
void xenstore_destroy(struct xs_dev *dev)
{
if (!dev)
return;
if (dev->xsh)
xs_close(dev->xsh);
free(dev);
}
int xenstore_get_be_domid(struct xs_dev *dev)
{
int domid;
unsigned int len;
char *val, *end;
if (!dev)
return -1;
val = xs_read(dev->xsh, XBT_NULL, "domid", &len);
if (val) {
domid = strtol(val, &end, 0);
if (*end != '\0') {
pr_err("invalid backend domid %s\n", val);
domid = -1;
}
free(val);
} else {
/* Assume lack of node means dom0 */
domid = 0;
}
return domid;
}
static bool xenstore_check_fe_exists(struct xs_dev *dev, domid_t domid)
{
char path[XEN_BUFSIZE];
unsigned int len;
char *val;
char **devid;
unsigned int num;
/*
* TODO: We need some sign that all devid subdirs are already written to
* Xenstore, so it's time to parse them. This delay although works, doesn't
* guarantee that.
*/
msleep(200);
snprintf(path, sizeof(path), "backend/%s/%d",
dev->type, domid);
devid = xs_directory(dev->xsh, XBT_NULL, path, &num);
if (!devid)
return false;
if (num > 1)
pr_warning("got %u devices, but only single device is supported\n", num);
/*
* Always choose the first devid if devid_str is not set. Otherwise
* make sure that specified devid_str is present.
*/
if (!dev->devid_str) {
dev->devid = atoi(devid[0]);
free(devid);
} else {
unsigned int i;
for (i = 0; i < num; i++) {
if (!strcmp(devid[i], dev->devid_str)) {
dev->devid = atoi(devid[i]);
break;
}
}
free(devid);
if (i == num) {
pr_err("devid %s is not found\n", dev->devid_str);
return false;
}
}
snprintf(path, sizeof(path), "/local/domain/%u/device/%s/%d",
domid, dev->type, dev->devid);
val = xs_read(dev->xsh, XBT_NULL, path, &len);
if (val) {
free(val);
return true;
}
dev->devid = 0;
return false;
}
static int xenstore_get_fe_domid(struct xs_dev *dev)
{
static domid_t curr_domid;
domid_t prev_domid = curr_domid;
char **domain;
unsigned int i, num;
domain = xs_directory(dev->xsh, XBT_NULL, dev->path, &num);
if (!domain)
return -1;
for (i = 0; i < num; i++) {
domid_t domid = atoi(domain[i]);
if (domid > curr_domid)
curr_domid = domid;
}
free(domain);
if (curr_domid > prev_domid && xenstore_check_fe_exists(dev, curr_domid))
return curr_domid;
return 0;
}
static int xenstore_poll_be_watch(struct xs_dev *dev)
{
unsigned int num;
char **vec;
int rc = 0;
vec = xs_read_watch(dev->xsh, &num);
if (!vec)
return -1;
if (!strcmp(vec[XS_WATCH_PATH], dev->path))
rc = xenstore_get_fe_domid(dev);
free(vec);
return rc;
}
int xenstore_wait_fe_domid(struct xs_dev *dev)
{
struct pollfd pfd;
int rc, domid = -1;
if (!dev)
return -1;
if (!xs_watch(dev->xsh, dev->path, dev->path))
return -1;
pfd.fd = xs_fileno(dev->xsh);
pfd.events = POLLIN | POLLERR | POLLHUP;
pfd.revents = 0;
while (domid < 0) {
rc = poll(&pfd, 1, 1000);
if (rc > 0 && pfd.revents & POLLIN) {
rc = xenstore_poll_be_watch(dev);
if (rc > 0)
domid = rc;
}
if (rc < 0 && errno != EINTR)
break;
}
xs_unwatch(dev->xsh, dev->path, dev->path);
return domid;
}
static int xenstore_fe_state_changed(struct xs_dev *dev)
{
int state;
if (xenstore_read_fe_int(dev, "state", &state) == -1) {
pr_err("reading frontend state failed\n");
return -1;
}
switch (state) {
case XenbusStateUnknown:
return -1;
case XenbusStateInitialising:
break;
default:
/* We expect only two states here */
BUG_ON(1);
return -1;
}
return 0;
}
static int xenstore_be_state_changed(struct xs_dev *dev)
{
int state;
if (xenstore_read_be_int(dev, "state", &state) == -1) {
pr_err("reading backend state failed\n");
return -1;
}
switch (state) {
case XenbusStateUnknown:
return -1;
default:
break;
}
return 0;
}
int xenstore_get_fd(struct xs_dev *dev)
{
return xs_fileno(dev->xsh);
}
int xenstore_poll_watches(struct xs_dev *dev)
{
unsigned int num;
char **vec;
int rc = 0;
if (!dev)
return -1;
vec = xs_read_watch(dev->xsh, &num);
if (!vec)
return -1;
if (!strcmp(vec[XS_WATCH_TOKEN], dev->be))
rc = xenstore_be_state_changed(dev);
if (!strcmp(vec[XS_WATCH_TOKEN], dev->fe))
rc = xenstore_fe_state_changed(dev);
free(vec);
return rc;
}