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
|
/* $OpenBSD: ldomd.c,v 1.6 2012/12/08 20:38:10 kettenis Exp $ */
/*
* Copyright (c) 2012 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "ds.h"
#include "hvctl.h"
#include "mdesc.h"
#include "util.h"
#include "ldomd.h"
TAILQ_HEAD(guest_head, guest) guests;
void add_guest(struct md_node *);
void map_domain_services(struct md *);
void frag_init(void);
void add_frag_mblock(struct md_node *);
void add_frag(uint64_t);
void delete_frag(uint64_t);
uint64_t alloc_frag(void);
void hv_update_md(struct guest *guest);
void hv_open(void);
void hv_close(void);
void hv_read(uint64_t, void *, size_t);
void hv_write(uint64_t, void *, size_t);
int hvctl_seq = 1;
int hvctl_fd;
void *hvmd_buf;
size_t hvmd_len;
struct md *hvmd;
uint64_t hv_mdpa;
__dead void usage(void);
void logit(int, const char *, ...);
void vlog(int, const char *, va_list);
void
log_init(int n_debug)
{
extern char *__progname;
debug = n_debug;
if (!debug)
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
tzset();
}
void
fatal(const char *emsg)
{
if (errno)
logit(LOG_CRIT, "fatal: %s: %s\n", emsg, strerror(errno));
else
logit(LOG_CRIT, "fatal: %s\n", emsg);
exit(EXIT_FAILURE);
}
void
logit(int pri, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlog(pri, fmt, ap);
va_end(ap);
}
void
vlog(int pri, const char *fmt, va_list ap)
{
char *nfmt;
if (debug) {
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s\n", fmt) == -1) {
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
} else {
vfprintf(stderr, nfmt, ap);
free(nfmt);
}
fflush(stderr);
} else
vsyslog(pri, fmt, ap);
}
int
main(int argc, char **argv)
{
struct hvctl_msg msg;
ssize_t nbytes;
struct md_header hdr;
struct md_node *node;
struct md_prop *prop;
struct guest *guest;
int debug = 0;
int ch;
int i;
log_init(1);
while ((ch = getopt(argc, argv, "d")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();
if (!debug)
if (daemon(0, 0))
fatal("daemon");
log_init(debug);
hv_open();
/*
* Request config.
*/
bzero(&msg, sizeof(msg));
msg.hdr.op = HVCTL_OP_GET_HVCONFIG;
msg.hdr.seq = hvctl_seq++;
nbytes = write(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("write");
bzero(&msg, sizeof(msg));
nbytes = read(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("read");
hv_mdpa = msg.msg.hvcnf.hvmdp;
hv_read(hv_mdpa, &hdr, sizeof(hdr));
hvmd_len = sizeof(hdr) + hdr.node_blk_sz + hdr.name_blk_sz +
hdr.data_blk_sz;
hvmd_buf = xmalloc(hvmd_len);
hv_read(hv_mdpa, hvmd_buf, hvmd_len);
hvmd = md_ingest(hvmd_buf, hvmd_len);
node = md_find_node(hvmd, "guests");
TAILQ_INIT(&guests);
TAILQ_FOREACH(prop, &node->prop_list, link) {
if (prop->tag == MD_PROP_ARC &&
strcmp(prop->name->str, "fwd") == 0)
add_guest(prop->d.arc.node);
}
frag_init();
TAILQ_FOREACH(guest, &guests, link) {
struct ds_conn *dc;
char path[64];
if (strcmp(guest->name, "primary") == 0)
continue;
snprintf(path, sizeof(path), "/dev/ldom-%s", guest->name);
dc = ds_conn_open(path, guest);
ds_conn_register_service(dc, &var_config_service);
}
hv_close();
/*
* Open all virtual disk server port device files. As long as
* we keep these device files open, the corresponding virtual
* disks will be available to the guest domains. For now we
* just keep them open until we exit, so there is not reason
* to keep track of the file descriptors.
*/
for (i = 0; i < 256; i++) {
char path[64];
snprintf(path, sizeof(path), "/dev/vdsp%d", i);
if (open(path, O_RDWR, 0) == -1)
break;
}
ds_conn_serve();
exit(EXIT_SUCCESS);
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-d]\n", __progname);
exit(EXIT_FAILURE);
}
void
add_guest(struct md_node *node)
{
struct guest *guest;
struct md_header hdr;
void *buf;
size_t len;
guest = xmalloc(sizeof(*guest));
if (!md_get_prop_str(hvmd, node, "name", &guest->name))
goto free;
if (!md_get_prop_val(hvmd, node, "gid", &guest->gid))
goto free;
if (!md_get_prop_val(hvmd, node, "mdpa", &guest->mdpa))
goto free;
hv_read(guest->mdpa, &hdr, sizeof(hdr));
len = sizeof(hdr) + hdr.node_blk_sz + hdr.name_blk_sz +
hdr.data_blk_sz;
buf = xmalloc(len);
hv_read(guest->mdpa, buf, len);
guest->node = node;
guest->md = md_ingest(buf, len);
if (strcmp(guest->name, "primary") == 0)
map_domain_services(guest->md);
TAILQ_INSERT_TAIL(&guests, guest, link);
return;
free:
free(guest);
}
void
map_domain_services(struct md *md)
{
struct md_node *node;
const char *name;
char source[64];
char target[64];
int unit = 0;
TAILQ_FOREACH(node, &md->node_list, link) {
if (strcmp(node->name->str, "virtual-device-port") != 0)
continue;
if (!md_get_prop_str(md, node, "vldc-svc-name", &name))
continue;
if (strncmp(name, "ldom-", 5) != 0 ||
strcmp(name, "ldom-primary") == 0)
continue;
snprintf(source, sizeof(source), "/dev/ldom%d", unit++);
snprintf(target, sizeof(target), "/dev/%s", name);
unlink(target);
symlink(source, target);
}
}
struct frag {
TAILQ_ENTRY(frag) link;
uint64_t base;
};
TAILQ_HEAD(frag_head, frag) free_frags;
uint64_t fragsize;
void
frag_init(void)
{
struct md_node *node;
struct md_prop *prop;
node = md_find_node(hvmd, "frag_space");
md_get_prop_val(hvmd, node, "fragsize", &fragsize);
TAILQ_INIT(&free_frags);
TAILQ_FOREACH(prop, &node->prop_list, link) {
if (prop->tag == MD_PROP_ARC &&
strcmp(prop->name->str, "fwd") == 0)
add_frag_mblock(prop->d.arc.node);
}
}
void
add_frag_mblock(struct md_node *node)
{
uint64_t base, size;
struct guest *guest;
md_get_prop_val(hvmd, node, "base", &base);
md_get_prop_val(hvmd, node, "size", &size);
while (size > fragsize) {
add_frag(base);
size -= fragsize;
base += fragsize;
}
delete_frag(hv_mdpa);
TAILQ_FOREACH(guest, &guests, link)
delete_frag(guest->mdpa);
}
void
add_frag(uint64_t base)
{
struct frag *frag;
frag = xmalloc(sizeof(*frag));
frag->base = base;
TAILQ_INSERT_TAIL(&free_frags, frag, link);
}
void
delete_frag(uint64_t base)
{
struct frag *frag;
struct frag *tmp;
TAILQ_FOREACH_SAFE(frag, &free_frags, link, tmp) {
if (frag->base == base) {
TAILQ_REMOVE(&free_frags, frag, link);
free(frag);
}
}
}
uint64_t
alloc_frag(void)
{
struct frag *frag;
uint64_t base;
frag = TAILQ_FIRST(&free_frags);
if (frag == NULL)
return -1;
TAILQ_REMOVE(&free_frags, frag, link);
base = frag->base;
free(frag);
return base;
}
void
hv_update_md(struct guest *guest)
{
struct hvctl_msg msg;
size_t nbytes;
void *buf;
size_t size;
uint64_t mdpa;
hv_open();
mdpa = alloc_frag();
size = md_exhume(guest->md, &buf);
hv_write(mdpa, buf, size);
add_frag(guest->mdpa);
guest->mdpa = mdpa;
free(buf);
md_set_prop_val(hvmd, guest->node, "mdpa", guest->mdpa);
mdpa = alloc_frag();
size = md_exhume(hvmd, &buf);
hv_write(mdpa, buf, size);
add_frag(hv_mdpa);
hv_mdpa = mdpa;
free(buf);
/* Update config. */
bzero(&msg, sizeof(msg));
msg.hdr.op = HVCTL_OP_RECONFIGURE;
msg.hdr.seq = hvctl_seq++;
msg.msg.reconfig.guestid = -1;
msg.msg.reconfig.hvmdp = hv_mdpa;
nbytes = write(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("write");
bzero(&msg, sizeof(msg));
nbytes = read(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("read");
hv_close();
if (msg.hdr.status != HVCTL_ST_OK)
logit(LOG_CRIT, "reconfigure failed: %d", msg.hdr.status);
}
void
hv_open(void)
{
struct hvctl_msg msg;
ssize_t nbytes;
uint64_t code;
hvctl_fd = open("/dev/hvctl", O_RDWR, 0);
if (hvctl_fd == -1)
fatal("cannot open /dev/hvctl");
/*
* Say "Hello".
*/
bzero(&msg, sizeof(msg));
msg.hdr.op = HVCTL_OP_HELLO;
msg.hdr.seq = hvctl_seq++;
msg.msg.hello.major = 1;
nbytes = write(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("write");
bzero(&msg, sizeof(msg));
nbytes = read(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("read");
code = msg.msg.clnge.code ^ 0xbadbeef20;
/*
* Respond to challenge.
*/
bzero(&msg, sizeof(msg));
msg.hdr.op = HVCTL_OP_RESPONSE;
msg.hdr.seq = hvctl_seq++;
msg.msg.clnge.code = code ^ 0x12cafe42a;
nbytes = write(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("write");
bzero(&msg, sizeof(msg));
nbytes = read(hvctl_fd, &msg, sizeof(msg));
if (nbytes != sizeof(msg))
fatal("read");
}
void
hv_close(void)
{
close(hvctl_fd);
hvctl_fd = -1;
}
void
hv_read(uint64_t addr, void *buf, size_t len)
{
struct hv_io hi;
hi.hi_cookie = addr;
hi.hi_addr = buf;
hi.hi_len = len;
if (ioctl(hvctl_fd, HVIOCREAD, &hi) == -1)
fatal("ioctl");
}
void
hv_write(uint64_t addr, void *buf, size_t len)
{
struct hv_io hi;
hi.hi_cookie = addr;
hi.hi_addr = buf;
hi.hi_len = len;
if (ioctl(hvctl_fd, HVIOCWRITE, &hi) == -1)
fatal("ioctl");
}
|