| 1 |
/* |
| 2 |
* Copyright (C) 2010 Citrix Ltd. |
| 3 |
* Author Vincent Hanquez <[email protected]> |
| 4 |
* Author Stefano Stabellini <[email protected]> |
| 5 |
* Author Gianni Tedesco <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published |
| 9 |
* by the Free Software Foundation; version 2.1 only. with the special |
| 10 |
* exception on linking described in file LICENSE. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "libxl_osdeps.h" /* must come before any other headers */ |
| 19 |
|
| 20 |
#include "libxl_internal.h" |
| 21 |
|
| 22 |
#include <xen/hvm/e820.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <pwd.h> |
| 25 |
#include <grp.h> |
| 26 |
|
| 27 |
static const char *libxl_tapif_script(libxl__gc *gc) |
| 28 |
{ |
| 29 |
#if defined(__linux__) || defined(__FreeBSD__) |
| 30 |
return libxl__strdup(gc, "no"); |
| 31 |
#else |
| 32 |
return GCSPRINTF("%s/qemu-ifup", libxl__xen_script_dir_path()); |
| 33 |
#endif |
| 34 |
} |
| 35 |
|
| 36 |
const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid) |
| 37 |
{ |
| 38 |
return GCSPRINTF(LIBXL_DEVICE_MODEL_SAVE_FILE".%d", domid); |
| 39 |
} |
| 40 |
|
| 41 |
static const char *qemu_xen_path(libxl__gc *gc) |
| 42 |
{ |
| 43 |
return QEMU_XEN_PATH; |
| 44 |
} |
| 45 |
|
| 46 |
static int libxl__create_qemu_logfile(libxl__gc *gc, char *name) |
| 47 |
{ |
| 48 |
char *logfile; |
| 49 |
int rc, logfile_w; |
| 50 |
|
| 51 |
rc = libxl_create_logfile(CTX, name, &logfile); |
| 52 |
if (rc) return rc; |
| 53 |
|
| 54 |
logfile_w = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0644); |
| 55 |
|
| 56 |
if (logfile_w < 0) { |
| 57 |
LOGE(ERROR, "unable to open Qemu logfile: %s", logfile); |
| 58 |
free(logfile); |
| 59 |
return ERROR_FAIL; |
| 60 |
} |
| 61 |
|
| 62 |
free(logfile); |
| 63 |
|
| 64 |
return logfile_w; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* userlookup_helper_getpwnam(libxl__gc*, const char *user, |
| 69 |
* struct passwd **pwd_r); |
| 70 |
* |
| 71 |
* userlookup_helper_getpwuid(libxl__gc*, uid_t uid, |
| 72 |
* struct passwd **pwd_r); |
| 73 |
* |
| 74 |
* If the user is found, return 0 and set *pwd_r to the appropriat |
| 75 |
* value. |
| 76 |
* |
| 77 |
* If the user is not found but there are no errors, return 0 |
| 78 |
* and set *pwd_r to NULL. |
| 79 |
* |
| 80 |
* On error, return a libxl-style error code. |
| 81 |
*/ |
| 82 |
#define DEFINE_USERLOOKUP_HELPER(NAME,SPEC_TYPE,STRUCTNAME,SYSCONF) \ |
| 83 |
int userlookup_helper_##NAME(libxl__gc *gc, \ |
| 84 |
SPEC_TYPE spec, \ |
| 85 |
struct STRUCTNAME *resultbuf, \ |
| 86 |
struct STRUCTNAME **out) \ |
| 87 |
{ \ |
| 88 |
struct STRUCTNAME *resultp = NULL; \ |
| 89 |
char *buf = NULL; \ |
| 90 |
long buf_size; \ |
| 91 |
int r; \ |
| 92 |
\ |
| 93 |
buf_size = sysconf(SYSCONF); \ |
| 94 |
if (buf_size < 0) { \ |
| 95 |
buf_size = 2048; \ |
| 96 |
LOG(DEBUG, \ |
| 97 |
"sysconf failed, setting the initial buffer size to %ld", \ |
| 98 |
buf_size); \ |
| 99 |
} \ |
| 100 |
\ |
| 101 |
while (1) { \ |
| 102 |
buf = libxl__realloc(gc, buf, buf_size); \ |
| 103 |
r = NAME##_r(spec, resultbuf, buf, buf_size, &resultp); \ |
| 104 |
if (r == ERANGE) { \ |
| 105 |
buf_size += 128; \ |
| 106 |
continue; \ |
| 107 |
} \ |
| 108 |
if (r != 0) { \ |
| 109 |
LOGEV(ERROR, r, "Looking up username/uid with " #NAME); \ |
| 110 |
return ERROR_FAIL; \ |
| 111 |
} \ |
| 112 |
*out = resultp; \ |
| 113 |
return 0; \ |
| 114 |
} \ |
| 115 |
} |
| 116 |
|
| 117 |
DEFINE_USERLOOKUP_HELPER(getpwnam, const char*, passwd, _SC_GETPW_R_SIZE_MAX); |
| 118 |
DEFINE_USERLOOKUP_HELPER(getpwuid, uid_t, passwd, _SC_GETPW_R_SIZE_MAX); |
| 119 |
|
| 120 |
static int libxl__domain_get_device_model_uid(libxl__gc *gc, |
| 121 |
libxl__dm_spawn_state *dmss) |
| 122 |
{ |
| 123 |
int guest_domid = dmss->guest_domid; |
| 124 |
libxl__domain_build_state *const state = dmss->build_state; |
| 125 |
const libxl_domain_build_info *b_info = &dmss->guest_config->b_info; |
| 126 |
|
| 127 |
struct passwd *user_base, user_pwbuf; |
| 128 |
int rc; |
| 129 |
char *user; |
| 130 |
uid_t intended_uid = -1; |
| 131 |
bool kill_by_uid; |
| 132 |
|
| 133 |
/* Only qemu-upstream can run as a different uid */ |
| 134 |
if (b_info->device_model_version != LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) |
| 135 |
return 0; |
| 136 |
|
| 137 |
/* |
| 138 |
* From this point onward, all paths should go through the `out` |
| 139 |
* label. The invariants should be: |
| 140 |
* - rc may be 0, or an error code. |
| 141 |
* - if rc is an error code, user and intended_uid are ignored. |
| 142 |
* - if rc is 0, user may be set or not set. |
| 143 |
* - if user is set, then intended_uid must be set to a UID matching |
| 144 |
* the username `user`, and kill_by_uid must be set to the appropriate |
| 145 |
* value. intended_uid will be checked for root (0). |
| 146 |
*/ |
| 147 |
|
| 148 |
/* |
| 149 |
* If device_model_user is present, set `-runas` even if |
| 150 |
* dm_restrict isn't in use |
| 151 |
*/ |
| 152 |
user = b_info->device_model_user; |
| 153 |
if (user) { |
| 154 |
rc = userlookup_helper_getpwnam(gc, user, &user_pwbuf, &user_base); |
| 155 |
if (rc) { |
| 156 |
kill_by_uid = false; |
| 157 |
goto out; |
| 158 |
} |
| 159 |
|
| 160 |
if (!user_base) { |
| 161 |
LOGD(ERROR, guest_domid, "Couldn't find device_model_user %s", |
| 162 |
user); |
| 163 |
rc = ERROR_INVAL; |
| 164 |
kill_by_uid = false; |
| 165 |
goto out; |
| 166 |
} |
| 167 |
|
| 168 |
intended_uid = user_base->pw_uid; |
| 169 |
kill_by_uid = true; |
| 170 |
rc = 0; |
| 171 |
goto out; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* If dm_restrict isn't set, and we don't have a specified user, don't |
| 176 |
* bother setting a `-runas` parameter. |
| 177 |
*/ |
| 178 |
if (!libxl_defbool_val(b_info->dm_restrict)) { |
| 179 |
LOGD(DEBUG, guest_domid, |
| 180 |
"dm_restrict disabled, starting QEMU as root"); |
| 181 |
user = NULL; /* Should already be null, but just in case */ |
| 182 |
kill_by_uid = false; /* Keep older versions of gcc happy */ |
| 183 |
rc = 0; |
| 184 |
goto out; |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* dm_restrict is set, but device_model_user isn't set; look for |
| 189 |
* QEMU_USER_BASE_RANGE |
| 190 |
*/ |
| 191 |
rc = userlookup_helper_getpwnam(gc, LIBXL_QEMU_USER_RANGE_BASE, |
| 192 |
&user_pwbuf, &user_base); |
| 193 |
if (rc) { |
| 194 |
kill_by_uid = false; |
| 195 |
goto out; |
| 196 |
} |
| 197 |
|
| 198 |
if (user_base) { |
| 199 |
struct passwd *user_clash, user_clash_pwbuf; |
| 200 |
|
| 201 |
intended_uid = user_base->pw_uid + guest_domid; |
| 202 |
rc = userlookup_helper_getpwuid(gc, intended_uid, |
| 203 |
&user_clash_pwbuf, &user_clash); |
| 204 |
if (rc) { |
| 205 |
kill_by_uid = false; |
| 206 |
goto out; |
| 207 |
} |
| 208 |
|
| 209 |
if (user_clash) { |
| 210 |
LOGD(ERROR, guest_domid, |
| 211 |
"wanted to use uid %ld (%s + %d) but that is user %s !", |
| 212 |
(long)intended_uid, LIBXL_QEMU_USER_RANGE_BASE, |
| 213 |
guest_domid, user_clash->pw_name); |
| 214 |
rc = ERROR_INVAL; |
| 215 |
kill_by_uid = false; |
| 216 |
goto out; |
| 217 |
} |
| 218 |
|
| 219 |
LOGD(DEBUG, guest_domid, "using uid %ld", (long)intended_uid); |
| 220 |
user = GCSPRINTF("%ld:%ld", (long)intended_uid, |
| 221 |
(long)user_base->pw_gid); |
| 222 |
kill_by_uid = true; |
| 223 |
rc = 0; |
| 224 |
goto out; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* We couldn't find QEMU_USER_BASE_RANGE; look for |
| 229 |
* QEMU_USER_SHARED. NB for QEMU_USER_SHARED, all QEMU will run |
| 230 |
* as the same UID, we can't kill by uid; therefore don't set uid. |
| 231 |
*/ |
| 232 |
user = LIBXL_QEMU_USER_SHARED; |
| 233 |
rc = userlookup_helper_getpwnam(gc, user, &user_pwbuf, &user_base); |
| 234 |
if (rc) { |
| 235 |
kill_by_uid = false; |
| 236 |
goto out; |
| 237 |
} |
| 238 |
|
| 239 |
if (user_base) { |
| 240 |
LOGD(WARN, guest_domid, "Could not find user %s, falling back to %s", |
| 241 |
LIBXL_QEMU_USER_RANGE_BASE, LIBXL_QEMU_USER_SHARED); |
| 242 |
intended_uid = user_base->pw_uid; |
| 243 |
kill_by_uid = false; |
| 244 |
rc = 0; |
| 245 |
goto out; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* dm_depriv is set, but we can't find a non-root uid to run as; |
| 250 |
* fail domain creation |
| 251 |
*/ |
| 252 |
LOGD(ERROR, guest_domid, |
| 253 |
"Could not find user %s or range base pseudo-user %s, cannot restrict", |
| 254 |
LIBXL_QEMU_USER_SHARED, LIBXL_QEMU_USER_RANGE_BASE); |
| 255 |
rc = ERROR_INVAL; |
| 256 |
kill_by_uid = false; |
| 257 |
|
| 258 |
out: |
| 259 |
/* First, do a root check if appropriate */ |
| 260 |
if (!rc) { |
| 261 |
if (user && intended_uid == 0) { |
| 262 |
LOGD(ERROR, guest_domid, "intended_uid is 0 (root)!"); |
| 263 |
rc = ERROR_INVAL; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/* Then do the final set, if still appropriate */ |
| 268 |
if (!rc && user) { |
| 269 |
state->dm_runas = user; |
| 270 |
if (kill_by_uid) |
| 271 |
state->dm_kill_uid = GCSPRINTF("%ld", (long)intended_uid); |
| 272 |
} |
| 273 |
|
| 274 |
return rc; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Look up "reaper UID". If present and non-root, returns 0 and sets |
| 279 |
* reaper_uid. Otherwise returns libxl-style error. |
| 280 |
*/ |
| 281 |
static int libxl__get_reaper_uid(libxl__gc *gc, uid_t *reaper_uid) |
| 282 |
{ |
| 283 |
struct passwd *user_base, user_pwbuf; |
| 284 |
int rc; |
| 285 |
|
| 286 |
rc = userlookup_helper_getpwnam(gc, LIBXL_QEMU_USER_REAPER, |
| 287 |
&user_pwbuf, &user_base); |
| 288 |
/* |
| 289 |
* Either there was an error, or we found a suitable user; stop |
| 290 |
* looking |
| 291 |
*/ |
| 292 |
if (rc || user_base) |
| 293 |
goto out; |
| 294 |
|
| 295 |
rc = userlookup_helper_getpwnam(gc, LIBXL_QEMU_USER_RANGE_BASE, |
| 296 |
&user_pwbuf, &user_base); |
| 297 |
if (rc || user_base) |
| 298 |
goto out; |
| 299 |
|
| 300 |
LOG(WARN, "Couldn't find uid for reaper process"); |
| 301 |
rc = ERROR_INVAL; |
| 302 |
|
| 303 |
out: |
| 304 |
/* First check to see if the discovered user maps to root */ |
| 305 |
if (!rc) { |
| 306 |
if (user_base->pw_uid == 0) { |
| 307 |
LOG(ERROR, "UID for reaper process maps to root!"); |
| 308 |
rc = ERROR_INVAL; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* If everything is OK, set reaper_uid as appropriate */ |
| 313 |
if (!rc) |
| 314 |
*reaper_uid = user_base->pw_uid; |
| 315 |
|
| 316 |
return rc; |
| 317 |
} |
| 318 |
|
| 319 |
const char *libxl__domain_device_model(libxl__gc *gc, |
| 320 |
const libxl_domain_build_info *info) |
| 321 |
{ |
| 322 |
const char *dm; |
| 323 |
|
| 324 |
if (libxl_defbool_val(info->device_model_stubdomain)) |
| 325 |
return NULL; |
| 326 |
|
| 327 |
if (info->device_model) { |
| 328 |
dm = libxl__strdup(gc, info->device_model); |
| 329 |
} else { |
| 330 |
switch (info->device_model_version) { |
| 331 |
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: |
| 332 |
dm = libxl__abs_path(gc, "qemu-dm", libxl__private_bindir_path()); |
| 333 |
break; |
| 334 |
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN: |
| 335 |
dm = qemu_xen_path(gc); |
| 336 |
break; |
| 337 |
default: |
| 338 |
LOG(ERROR, "invalid device model version %d", |
| 339 |
info->device_model_version); |
| 340 |
dm = NULL; |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
return dm; |
| 345 |
} |
| 346 |
|
| 347 |
static int |
| 348 |
libxl__xc_device_get_rdm(libxl__gc *gc, |
| 349 |
uint32_t flags, |
| 350 |
uint16_t seg, |
| 351 |
uint8_t bus, |
| 352 |
uint8_t devfn, |
| 353 |
unsigned int *nr_entries, |
| 354 |
struct xen_reserved_device_memory **xrdm) |
| 355 |
{ |
| 356 |
int rc = 0, r; |
| 357 |
|
| 358 |
/* |
| 359 |
* We really can't presume how many entries we can get in advance. |
| 360 |
*/ |
| 361 |
*nr_entries = 0; |
| 362 |
r = xc_reserved_device_memory_map(CTX->xch, flags, seg, bus, devfn, |
| 363 |
NULL, nr_entries); |
| 364 |
assert(r <= 0); |
| 365 |
/* "0" means we have no any rdm entry. */ |
| 366 |
if (!r) goto out; |
| 367 |
|
| 368 |
if (errno != ENOBUFS) { |
| 369 |
rc = ERROR_FAIL; |
| 370 |
goto out; |
| 371 |
} |
| 372 |
|
| 373 |
GCNEW_ARRAY(*xrdm, *nr_entries); |
| 374 |
r = xc_reserved_device_memory_map(CTX->xch, flags, seg, bus, devfn, |
| 375 |
*xrdm, nr_entries); |
| 376 |
if (r) |
| 377 |
rc = ERROR_FAIL; |
| 378 |
|
| 379 |
out: |
| 380 |
if (rc) { |
| 381 |
*nr_entries = 0; |
| 382 |
*xrdm = NULL; |
| 383 |
LOG(ERROR, "Could not get reserved device memory maps."); |
| 384 |
} |
| 385 |
return rc; |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
* Check whether there exists rdm hole in the specified memory range. |
| 390 |
* Returns true if exists, else returns false. |
| 391 |
*/ |
| 392 |
static bool overlaps_rdm(uint64_t start, uint64_t memsize, |
| 393 |
uint64_t rdm_start, uint64_t rdm_size) |
| 394 |
{ |
| 395 |
return (start + memsize > rdm_start) && (start < rdm_start + rdm_size); |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
add_rdm_entry(libxl__gc *gc, libxl_domain_config *d_config, |
| 400 |
uint64_t rdm_start, uint64_t rdm_size, int rdm_policy) |
| 401 |
{ |
| 402 |
d_config->rdms = libxl__realloc(NOGC, d_config->rdms, |
| 403 |
(d_config->num_rdms+1) * sizeof(libxl_device_rdm)); |
| 404 |
|
| 405 |
d_config->rdms[d_config->num_rdms].start = rdm_start; |
| 406 |
d_config->rdms[d_config->num_rdms].size = rdm_size; |
| 407 |
d_config->rdms[d_config->num_rdms].policy = rdm_policy; |
| 408 |
d_config->num_rdms++; |
| 409 |
} |
| 410 |
|
| 411 |
/* |
| 412 |
* Check reported RDM regions and handle potential gfn conflicts according |
| 413 |
* to user preferred policy. |
| 414 |
* |
| 415 |
* RDM can reside in address space beyond 4G theoretically, but we never |
| 416 |
* see this in real world. So in order to avoid breaking highmem layout |
| 417 |
* we don't solve highmem conflict. Note this means highmem rmrr could |
| 418 |
* still be supported if no conflict. |
| 419 |
* |
| 420 |
* But in the case of lowmem, RDM probably scatter the whole RAM space. |
| 421 |
* Especially multiple RDM entries would worsen this to lead a complicated |
| 422 |
* memory layout. And then its hard to extend hvm_info_table{} to work |
| 423 |
* hvmloader out. So here we're trying to figure out a simple solution to |
| 424 |
* avoid breaking existing layout. So when a conflict occurs, |
| 425 |
* |
| 426 |
* #1. Above a predefined boundary (default 2G) |
| 427 |
* - Move lowmem_end below reserved region to solve conflict; |
| 428 |
* |
| 429 |
* #2. Below a predefined boundary (default 2G) |
| 430 |
* - Check strict/relaxed policy. |
| 431 |
* "strict" policy leads to fail libxl. |
| 432 |
* "relaxed" policy issue a warning message and also mask this entry |
| 433 |
* INVALID to indicate we shouldn't expose this entry to hvmloader. |
| 434 |
* Note when both policies are specified on a given region, the per-device |
| 435 |
* policy should override the global policy. |
| 436 |
*/ |
| 437 |
int libxl__domain_device_construct_rdm(libxl__gc *gc, |
| 438 |
libxl_domain_config *d_config, |
| 439 |
uint64_t rdm_mem_boundary, |
| 440 |
struct xc_dom_image *dom) |
| 441 |
{ |
| 442 |
int i, j, conflict, rc; |
| 443 |
struct xen_reserved_device_memory *xrdm = NULL; |
| 444 |
uint32_t strategy = d_config->b_info.u.hvm.rdm.strategy; |
| 445 |
uint16_t seg; |
| 446 |
uint8_t bus, devfn; |
| 447 |
uint64_t rdm_start, rdm_size; |
| 448 |
uint64_t highmem_end = dom->highmem_end; |
| 449 |
|
| 450 |
/* |
| 451 |
* We just want to construct RDM once since RDM is specific to the |
| 452 |
* given platform, so this shouldn't change again. |
| 453 |
*/ |
| 454 |
if (d_config->num_rdms) |
| 455 |
return 0; |
| 456 |
|
| 457 |
/* Might not expose rdm. */ |
| 458 |
if (strategy == LIBXL_RDM_RESERVE_STRATEGY_IGNORE && |
| 459 |
!d_config->num_pcidevs) |
| 460 |
return 0; |
| 461 |
|
| 462 |
/* Query all RDM entries in this platform */ |
| 463 |
if (strategy == LIBXL_RDM_RESERVE_STRATEGY_HOST) { |
| 464 |
unsigned int nr_entries; |
| 465 |
|
| 466 |
/* Collect all rdm info if exist. */ |
| 467 |
rc = libxl__xc_device_get_rdm(gc, XENMEM_RDM_ALL, |
| 468 |
0, 0, 0, &nr_entries, &xrdm); |
| 469 |
if (rc) |
| 470 |
goto out; |
| 471 |
if (!nr_entries) |
| 472 |
return 0; |
| 473 |
|
| 474 |
assert(xrdm); |
| 475 |
|
| 476 |
for (i = 0; i < nr_entries; i++) |
| 477 |
{ |
| 478 |
add_rdm_entry(gc, d_config, |
| 479 |
pfn_to_paddr(xrdm[i].start_pfn), |
| 480 |
pfn_to_paddr(xrdm[i].nr_pages), |
| 481 |
d_config->b_info.u.hvm.rdm.policy); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
/* Query RDM entries per-device */ |
| 486 |
for (i = 0; i < d_config->num_pcidevs; i++) { |
| 487 |
unsigned int n, nr_entries; |
| 488 |
|
| 489 |
seg = d_config->pcidevs[i].domain; |
| 490 |
bus = d_config->pcidevs[i].bus; |
| 491 |
devfn = PCI_DEVFN(d_config->pcidevs[i].dev, |
| 492 |
d_config->pcidevs[i].func); |
| 493 |
nr_entries = 0; |
| 494 |
rc = libxl__xc_device_get_rdm(gc, 0, |
| 495 |
seg, bus, devfn, &nr_entries, &xrdm); |
| 496 |
if (rc) |
| 497 |
goto out; |
| 498 |
/* No RDM to associated with this device. */ |
| 499 |
if (!nr_entries) |
| 500 |
continue; |
| 501 |
|
| 502 |
assert(xrdm); |
| 503 |
|
| 504 |
rc = libxl__device_pci_setdefault(gc, DOMID_INVALID, |
| 505 |
&d_config->pcidevs[i], false); |
| 506 |
if (rc) |
| 507 |
goto out; |
| 508 |
|
| 509 |
for (n = 0; n < nr_entries; ++n) { |
| 510 |
bool new = true; |
| 511 |
|
| 512 |
/* |
| 513 |
* Need to check whether this entry is already saved in the |
| 514 |
* array. This could come from two cases: |
| 515 |
* |
| 516 |
* - user may configure to get all RDMs in this platform, |
| 517 |
* which is already queried before this point |
| 518 |
* - or two assigned devices may share one RDM entry |
| 519 |
* |
| 520 |
* Different policies may be configured on the same RDM due to |
| 521 |
* above two cases. But we don't allow to assign such a group |
| 522 |
* of devices right now so it doesn't come true in our case. |
| 523 |
*/ |
| 524 |
for (j = 0; j < d_config->num_rdms; j++) { |
| 525 |
if (d_config->rdms[j].start |
| 526 |
== pfn_to_paddr(xrdm[n].start_pfn)) |
| 527 |
{ |
| 528 |
/* |
| 529 |
* So the per-device policy always override the |
| 530 |
* global policy in this case. |
| 531 |
*/ |
| 532 |
d_config->rdms[j].policy |
| 533 |
= d_config->pcidevs[i].rdm_policy; |
| 534 |
new = false; |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
if (new) |
| 540 |
add_rdm_entry(gc, d_config, |
| 541 |
pfn_to_paddr(xrdm[n].start_pfn), |
| 542 |
pfn_to_paddr(xrdm[n].nr_pages), |
| 543 |
d_config->pcidevs[i].rdm_policy); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
/* |
| 548 |
* Next step is to check and avoid potential conflict between RDM |
| 549 |
* entries and guest RAM. To avoid intrusive impact to existing |
| 550 |
* memory layout {lowmem, mmio, highmem} which is passed around |
| 551 |
* various function blocks, below conflicts are not handled which |
| 552 |
* are rare and handling them would lead to a more scattered |
| 553 |
* layout: |
| 554 |
* - RDM in highmem area (>4G) |
| 555 |
* - RDM lower than a defined memory boundary (e.g. 2G) |
| 556 |
* Otherwise for conflicts between boundary and 4G, we'll simply |
| 557 |
* move lowmem end below reserved region to solve conflict. |
| 558 |
* |
| 559 |
* If a conflict is detected on a given RDM entry, an error will |
| 560 |
* be returned if 'strict' policy is specified. Instead, if |
| 561 |
* 'relaxed' policy specified, this conflict is treated just as a |
| 562 |
* warning, but we mark this RDM entry as INVALID to indicate that |
| 563 |
* this entry shouldn't be exposed to hvmloader. |
| 564 |
* |
| 565 |
* Firstly we should check the case of rdm < 4G because we may |
| 566 |
* need to expand highmem_end. |
| 567 |
*/ |
| 568 |
for (i = 0; i < d_config->num_rdms; i++) { |
| 569 |
rdm_start = d_config->rdms[i].start; |
| 570 |
rdm_size = d_config->rdms[i].size; |
| 571 |
conflict = overlaps_rdm(0, dom->lowmem_end, rdm_start, rdm_size); |
| 572 |
|
| 573 |
if (!conflict) |
| 574 |
continue; |
| 575 |
|
| 576 |
/* Just check if RDM > our memory boundary. */ |
| 577 |
if (rdm_start > rdm_mem_boundary) { |
| 578 |
/* |
| 579 |
* For HAP guests round down to a 2Mb boundary to allow use |
| 580 |
* of large page mappings. |
| 581 |
*/ |
| 582 |
if (libxl_defbool_val(d_config->c_info.hap) |
| 583 |
&& rdm_start > MB(2)) |
| 584 |
rdm_start &= ~(MB(2) - 1); |
| 585 |
/* |
| 586 |
* We will move downwards lowmem_end so we have to expand |
| 587 |
* highmem_end. |
| 588 |
*/ |
| 589 |
if (!highmem_end) |
| 590 |
highmem_end = 1ull << 32; |
| 591 |
highmem_end += (dom->lowmem_end - rdm_start); |
| 592 |
/* Now move downwards lowmem_end. */ |
| 593 |
dom->lowmem_end = rdm_start; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/* Sync highmem_end. */ |
| 598 |
dom->highmem_end = highmem_end; |
| 599 |
|
| 600 |
/* |
| 601 |
* Finally we can take same policy to check lowmem(< 2G) and |
| 602 |
* highmem adjusted above. |
| 603 |
*/ |
| 604 |
for (i = 0; i < d_config->num_rdms; i++) { |
| 605 |
rdm_start = d_config->rdms[i].start; |
| 606 |
rdm_size = d_config->rdms[i].size; |
| 607 |
/* Does this entry conflict with lowmem? */ |
| 608 |
conflict = overlaps_rdm(0, dom->lowmem_end, |
| 609 |
rdm_start, rdm_size); |
| 610 |
/* Does this entry conflict with highmem? */ |
| 611 |
if (highmem_end) |
| 612 |
conflict |= overlaps_rdm((1ULL << 32), |
| 613 |
highmem_end - (1ULL << 32), |
| 614 |
rdm_start, rdm_size); |
| 615 |
|
| 616 |
if (!conflict) |
| 617 |
continue; |
| 618 |
|
| 619 |
if (d_config->rdms[i].policy == LIBXL_RDM_RESERVE_POLICY_STRICT) { |
| 620 |
LOG(ERROR, "RDM conflict at 0x%"PRIx64".\n", |
| 621 |
d_config->rdms[i].start); |
| 622 |
goto out; |
| 623 |
} else { |
| 624 |
LOG(WARN, "Ignoring RDM conflict at 0x%"PRIx64".\n", |
| 625 |
d_config->rdms[i].start); |
| 626 |
|
| 627 |
/* |
| 628 |
* Then mask this INVALID to indicate we shouldn't expose this |
| 629 |
* to hvmloader. |
| 630 |
*/ |
| 631 |
d_config->rdms[i].policy = LIBXL_RDM_RESERVE_POLICY_INVALID; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
return 0; |
| 636 |
|
| 637 |
out: |
| 638 |
return ERROR_FAIL; |
| 639 |
} |
| 640 |
|
| 641 |
const libxl_vnc_info *libxl__dm_vnc(const libxl_domain_config *guest_config) |
| 642 |
{ |
| 643 |
const libxl_vnc_info *vnc = NULL; |
| 644 |
if (guest_config->b_info.type == LIBXL_DOMAIN_TYPE_HVM) { |
| 645 |
vnc = &guest_config->b_info.u.hvm.vnc; |
| 646 |
} else if (guest_config->num_vfbs > 0) { |
| 647 |
vnc = &guest_config->vfbs[0].vnc; |
| 648 |
} |
| 649 |
return vnc && libxl_defbool_val(vnc->enable) ? vnc : NULL; |
| 650 |
} |
| 651 |
|
| 652 |
static const libxl_sdl_info *dm_sdl(const libxl_domain_config *guest_config) |
| 653 |
{ |
| 654 |
const libxl_sdl_info *sdl = NULL; |
| 655 |
if (guest_config->b_info.type == LIBXL_DOMAIN_TYPE_HVM) { |
| 656 |
sdl = &guest_config->b_info.u.hvm.sdl; |
| 657 |
} else if (guest_config->num_vfbs > 0) { |
| 658 |
sdl = &guest_config->vfbs[0].sdl; |
| 659 |
} |
| 660 |
return sdl && libxl_defbool_val(sdl->enable) ? sdl : NULL; |
| 661 |
} |
| 662 |
|
| 663 |
static const char *dm_keymap(const libxl_domain_config *guest_config) |
| 664 |
{ |
| 665 |
if (guest_config->b_info.type == LIBXL_DOMAIN_TYPE_HVM) { |
| 666 |
return guest_config->b_info.u.hvm.keymap; |
| 667 |
} else if (guest_config->num_vfbs > 0) { |
| 668 |
return guest_config->vfbs[0].keymap; |
| 669 |
} else |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
|
| 673 |
static int libxl__build_device_model_args_old(libxl__gc *gc, |
| 674 |
const char *dm, int domid, |
| 675 |
const libxl_domain_config *guest_config, |
| 676 |
char ***args, char ***envs, |
| 677 |
const libxl__domain_build_state *state) |
| 678 |
{ |
| 679 |
const libxl_domain_create_info *c_info = &guest_config->c_info; |
| 680 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 681 |
const libxl_device_nic *nics = guest_config->nics; |
| 682 |
const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config); |
| 683 |
const libxl_sdl_info *sdl = dm_sdl(guest_config); |
| 684 |
const int num_nics = guest_config->num_nics; |
| 685 |
const char *keymap = dm_keymap(guest_config); |
| 686 |
int i; |
| 687 |
flexarray_t *dm_args, *dm_envs; |
| 688 |
dm_args = flexarray_make(gc, 16, 1); |
| 689 |
dm_envs = flexarray_make(gc, 16, 1); |
| 690 |
|
| 691 |
assert(state->dm_monitor_fd == -1); |
| 692 |
|
| 693 |
flexarray_vappend(dm_args, dm, |
| 694 |
"-d", GCSPRINTF("%d", domid), NULL); |
| 695 |
|
| 696 |
if (c_info->name) |
| 697 |
flexarray_vappend(dm_args, "-domain-name", c_info->name, NULL); |
| 698 |
|
| 699 |
if (vnc) { |
| 700 |
char *vncarg = NULL; |
| 701 |
|
| 702 |
flexarray_append(dm_args, "-vnc"); |
| 703 |
|
| 704 |
/* |
| 705 |
* If vnc->listen is present and contains a :, and |
| 706 |
* - vnc->display is 0, use vnc->listen |
| 707 |
* - vnc->display is non-zero, be confused |
| 708 |
* If vnc->listen is present but doesn't, use vnc->listen:vnc->display. |
| 709 |
* If vnc->listen is not present, use 127.0.0.1:vnc->display |
| 710 |
* (Remembering that vnc->display already defaults to 0.) |
| 711 |
*/ |
| 712 |
if (vnc->listen) { |
| 713 |
if (strchr(vnc->listen, ':') != NULL) { |
| 714 |
if (vnc->display) { |
| 715 |
LOGD(ERROR, domid, "vncdisplay set, vnclisten contains display"); |
| 716 |
return ERROR_INVAL; |
| 717 |
} |
| 718 |
vncarg = vnc->listen; |
| 719 |
} else { |
| 720 |
vncarg = GCSPRINTF("%s:%d", vnc->listen, vnc->display); |
| 721 |
} |
| 722 |
} else |
| 723 |
vncarg = GCSPRINTF("127.0.0.1:%d", vnc->display); |
| 724 |
|
| 725 |
if (vnc->passwd && vnc->passwd[0]) { |
| 726 |
vncarg = GCSPRINTF("%s,password", vncarg); |
| 727 |
} |
| 728 |
|
| 729 |
flexarray_append(dm_args, vncarg); |
| 730 |
|
| 731 |
if (libxl_defbool_val(vnc->findunused)) { |
| 732 |
flexarray_append(dm_args, "-vncunused"); |
| 733 |
} |
| 734 |
} else if (!sdl) { |
| 735 |
/* |
| 736 |
* VNC is not enabled by default by qemu-xen-traditional, |
| 737 |
* however skipping -vnc causes SDL to be |
| 738 |
* (unexpectedly) enabled by default. If undesired, disable graphics at |
| 739 |
* all. |
| 740 |
*/ |
| 741 |
flexarray_append(dm_args, "-nographic"); |
| 742 |
} |
| 743 |
|
| 744 |
if (sdl) { |
| 745 |
flexarray_append(dm_args, "-sdl"); |
| 746 |
if (!libxl_defbool_val(sdl->opengl)) { |
| 747 |
flexarray_append(dm_args, "-disable-opengl"); |
| 748 |
} |
| 749 |
if (sdl->display) |
| 750 |
flexarray_append_pair(dm_envs, "DISPLAY", sdl->display); |
| 751 |
if (sdl->xauthority) |
| 752 |
flexarray_append_pair(dm_envs, "XAUTHORITY", sdl->xauthority); |
| 753 |
} |
| 754 |
if (keymap) { |
| 755 |
flexarray_vappend(dm_args, "-k", keymap, NULL); |
| 756 |
} |
| 757 |
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) { |
| 758 |
int ioemu_nics = 0; |
| 759 |
int nr_set_cpus = 0; |
| 760 |
char *s; |
| 761 |
|
| 762 |
flexarray_append_pair(dm_envs, "XEN_DOMAIN_ID", GCSPRINTF("%d", domid)); |
| 763 |
|
| 764 |
if (b_info->kernel) { |
| 765 |
LOGD(ERROR, domid, "HVM direct kernel boot is not supported by " |
| 766 |
"qemu-xen-traditional"); |
| 767 |
return ERROR_INVAL; |
| 768 |
} |
| 769 |
|
| 770 |
if (b_info->u.hvm.serial || b_info->u.hvm.serial_list) { |
| 771 |
if ( b_info->u.hvm.serial && b_info->u.hvm.serial_list ) |
| 772 |
{ |
| 773 |
LOGD(ERROR, domid, "Both serial and serial_list set"); |
| 774 |
return ERROR_INVAL; |
| 775 |
} |
| 776 |
if (b_info->u.hvm.serial) { |
| 777 |
flexarray_vappend(dm_args, |
| 778 |
"-serial", b_info->u.hvm.serial, NULL); |
| 779 |
} else if (b_info->u.hvm.serial_list) { |
| 780 |
char **p; |
| 781 |
for (p = b_info->u.hvm.serial_list; |
| 782 |
*p; |
| 783 |
p++) { |
| 784 |
flexarray_vappend(dm_args, |
| 785 |
"-serial", |
| 786 |
*p, NULL); |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
if (libxl_defbool_val(b_info->u.hvm.nographic) && (!sdl && !vnc)) { |
| 792 |
flexarray_append(dm_args, "-nographic"); |
| 793 |
} |
| 794 |
|
| 795 |
if (b_info->video_memkb) { |
| 796 |
flexarray_vappend(dm_args, "-videoram", |
| 797 |
GCSPRINTF("%d", libxl__sizekb_to_mb(b_info->video_memkb)), |
| 798 |
NULL); |
| 799 |
} |
| 800 |
|
| 801 |
switch (b_info->u.hvm.vga.kind) { |
| 802 |
case LIBXL_VGA_INTERFACE_TYPE_STD: |
| 803 |
flexarray_append(dm_args, "-std-vga"); |
| 804 |
break; |
| 805 |
case LIBXL_VGA_INTERFACE_TYPE_CIRRUS: |
| 806 |
break; |
| 807 |
case LIBXL_VGA_INTERFACE_TYPE_NONE: |
| 808 |
flexarray_append_pair(dm_args, "-vga", "none"); |
| 809 |
break; |
| 810 |
case LIBXL_VGA_INTERFACE_TYPE_QXL: |
| 811 |
break; |
| 812 |
default: |
| 813 |
LOGD(ERROR, domid, "Invalid emulated video card specified"); |
| 814 |
return ERROR_INVAL; |
| 815 |
} |
| 816 |
|
| 817 |
if (b_info->u.hvm.boot) { |
| 818 |
flexarray_vappend(dm_args, "-boot", b_info->u.hvm.boot, NULL); |
| 819 |
} |
| 820 |
if (libxl_defbool_val(b_info->u.hvm.usb) |
| 821 |
|| b_info->u.hvm.usbdevice |
| 822 |
|| libxl_string_list_length(&b_info->u.hvm.usbdevice_list)) { |
| 823 |
if (b_info->u.hvm.usbdevice |
| 824 |
&& libxl_string_list_length(&b_info->u.hvm.usbdevice_list)) { |
| 825 |
LOGD(ERROR, domid, "Both usbdevice and usbdevice_list set"); |
| 826 |
return ERROR_INVAL; |
| 827 |
} |
| 828 |
flexarray_append(dm_args, "-usb"); |
| 829 |
if (b_info->u.hvm.usbdevice) { |
| 830 |
flexarray_vappend(dm_args, |
| 831 |
"-usbdevice", b_info->u.hvm.usbdevice, NULL); |
| 832 |
} else if (b_info->u.hvm.usbdevice_list) { |
| 833 |
char **p; |
| 834 |
for (p = b_info->u.hvm.usbdevice_list; |
| 835 |
*p; |
| 836 |
p++) { |
| 837 |
flexarray_vappend(dm_args, |
| 838 |
"-usbdevice", |
| 839 |
*p, NULL); |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
if (b_info->u.hvm.soundhw) { |
| 844 |
flexarray_vappend(dm_args, "-soundhw", b_info->u.hvm.soundhw, NULL); |
| 845 |
} |
| 846 |
if (libxl__acpi_defbool_val(b_info)) { |
| 847 |
flexarray_append(dm_args, "-acpi"); |
| 848 |
} |
| 849 |
if (b_info->max_vcpus > 1) { |
| 850 |
flexarray_vappend(dm_args, "-vcpus", |
| 851 |
GCSPRINTF("%d", b_info->max_vcpus), |
| 852 |
NULL); |
| 853 |
} |
| 854 |
|
| 855 |
nr_set_cpus = libxl_bitmap_count_set(&b_info->avail_vcpus); |
| 856 |
s = libxl_bitmap_to_hex_string(CTX, &b_info->avail_vcpus); |
| 857 |
flexarray_vappend(dm_args, "-vcpu_avail", |
| 858 |
GCSPRINTF("%s", s), NULL); |
| 859 |
free(s); |
| 860 |
|
| 861 |
for (i = 0; i < num_nics; i++) { |
| 862 |
if (nics[i].nictype == LIBXL_NIC_TYPE_VIF_IOEMU) { |
| 863 |
char *smac = GCSPRINTF( |
| 864 |
LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac)); |
| 865 |
const char *ifname = libxl__device_nic_devname(gc, |
| 866 |
domid, nics[i].devid, |
| 867 |
LIBXL_NIC_TYPE_VIF_IOEMU); |
| 868 |
flexarray_vappend(dm_args, |
| 869 |
"-net", |
| 870 |
GCSPRINTF( |
| 871 |
"nic,vlan=%d,macaddr=%s,model=%s", |
| 872 |
nics[i].devid, smac, nics[i].model), |
| 873 |
"-net", |
| 874 |
GCSPRINTF( |
| 875 |
"tap,vlan=%d,ifname=%s,bridge=%s," |
| 876 |
"script=%s,downscript=%s", |
| 877 |
nics[i].devid, ifname, nics[i].bridge, |
| 878 |
libxl_tapif_script(gc), |
| 879 |
libxl_tapif_script(gc)), |
| 880 |
NULL); |
| 881 |
ioemu_nics++; |
| 882 |
} |
| 883 |
} |
| 884 |
/* If we have no emulated nics, tell qemu not to create any */ |
| 885 |
if ( ioemu_nics == 0 ) { |
| 886 |
flexarray_vappend(dm_args, "-net", "none", NULL); |
| 887 |
} |
| 888 |
if (libxl_defbool_val(b_info->u.hvm.gfx_passthru)) { |
| 889 |
switch (b_info->u.hvm.gfx_passthru_kind) { |
| 890 |
case LIBXL_GFX_PASSTHRU_KIND_DEFAULT: |
| 891 |
case LIBXL_GFX_PASSTHRU_KIND_IGD: |
| 892 |
flexarray_append(dm_args, "-gfx_passthru"); |
| 893 |
break; |
| 894 |
default: |
| 895 |
LOGD(ERROR, domid, "unsupported gfx_passthru_kind."); |
| 896 |
return ERROR_INVAL; |
| 897 |
} |
| 898 |
} |
| 899 |
} else { |
| 900 |
if (!sdl && !vnc) |
| 901 |
flexarray_append(dm_args, "-nographic"); |
| 902 |
} |
| 903 |
|
| 904 |
if (libxl_defbool_val(b_info->dm_restrict)) { |
| 905 |
LOGD(ERROR, domid, |
| 906 |
"dm_restrict not supported by qemu-xen-traditional"); |
| 907 |
return ERROR_INVAL; |
| 908 |
} |
| 909 |
|
| 910 |
if (state->saved_state) { |
| 911 |
flexarray_vappend(dm_args, "-loadvm", state->saved_state, NULL); |
| 912 |
} |
| 913 |
for (i = 0; b_info->extra && b_info->extra[i] != NULL; i++) |
| 914 |
flexarray_append(dm_args, b_info->extra[i]); |
| 915 |
flexarray_append(dm_args, "-M"); |
| 916 |
switch (b_info->type) { |
| 917 |
case LIBXL_DOMAIN_TYPE_PVH: |
| 918 |
case LIBXL_DOMAIN_TYPE_PV: |
| 919 |
flexarray_append(dm_args, "xenpv"); |
| 920 |
for (i = 0; b_info->extra_pv && b_info->extra_pv[i] != NULL; i++) |
| 921 |
flexarray_append(dm_args, b_info->extra_pv[i]); |
| 922 |
break; |
| 923 |
case LIBXL_DOMAIN_TYPE_HVM: |
| 924 |
flexarray_append(dm_args, "xenfv"); |
| 925 |
for (i = 0; b_info->extra_hvm && b_info->extra_hvm[i] != NULL; i++) |
| 926 |
flexarray_append(dm_args, b_info->extra_hvm[i]); |
| 927 |
break; |
| 928 |
default: |
| 929 |
abort(); |
| 930 |
} |
| 931 |
flexarray_append(dm_args, NULL); |
| 932 |
*args = (char **) flexarray_contents(dm_args); |
| 933 |
flexarray_append(dm_envs, NULL); |
| 934 |
if (envs) |
| 935 |
*envs = (char **) flexarray_contents(dm_envs); |
| 936 |
return 0; |
| 937 |
} |
| 938 |
|
| 939 |
static char *dm_spice_options(libxl__gc *gc, |
| 940 |
const libxl_spice_info *spice) |
| 941 |
{ |
| 942 |
char *opt; |
| 943 |
|
| 944 |
if (!spice->port && !spice->tls_port) { |
| 945 |
LOG(ERROR, |
| 946 |
"at least one of the spiceport or tls_port must be provided"); |
| 947 |
return NULL; |
| 948 |
} |
| 949 |
|
| 950 |
if (!libxl_defbool_val(spice->disable_ticketing)) { |
| 951 |
if (!spice->passwd) { |
| 952 |
LOG(ERROR, "spice ticketing is enabled but missing password"); |
| 953 |
return NULL; |
| 954 |
} |
| 955 |
else if (!spice->passwd[0]) { |
| 956 |
LOG(ERROR, "spice password can't be empty"); |
| 957 |
return NULL; |
| 958 |
} |
| 959 |
} |
| 960 |
opt = GCSPRINTF("port=%d,tls-port=%d", spice->port, spice->tls_port); |
| 961 |
if (spice->host) |
| 962 |
opt = GCSPRINTF("%s,addr=%s", opt, spice->host); |
| 963 |
if (libxl_defbool_val(spice->disable_ticketing)) |
| 964 |
opt = GCSPRINTF("%s,disable-ticketing=on", opt); |
| 965 |
else |
| 966 |
opt = GCSPRINTF("%s,password=%s", opt, spice->passwd); |
| 967 |
opt = GCSPRINTF("%s,agent-mouse=%s", opt, |
| 968 |
libxl_defbool_val(spice->agent_mouse) ? "on" : "off"); |
| 969 |
|
| 970 |
if (!libxl_defbool_val(spice->clipboard_sharing)) |
| 971 |
opt = GCSPRINTF("%s,disable-copy-paste=on", opt); |
| 972 |
|
| 973 |
if (spice->image_compression) |
| 974 |
opt = GCSPRINTF("%s,image-compression=%s", opt, |
| 975 |
spice->image_compression); |
| 976 |
|
| 977 |
if (spice->streaming_video) |
| 978 |
opt = GCSPRINTF("%s,streaming-video=%s", opt, spice->streaming_video); |
| 979 |
|
| 980 |
return opt; |
| 981 |
} |
| 982 |
|
| 983 |
static enum libxl_gfx_passthru_kind |
| 984 |
libxl__detect_gfx_passthru_kind(libxl__gc *gc, |
| 985 |
const libxl_domain_config *guest_config) |
| 986 |
{ |
| 987 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 988 |
|
| 989 |
if (b_info->u.hvm.gfx_passthru_kind != LIBXL_GFX_PASSTHRU_KIND_DEFAULT) |
| 990 |
return b_info->u.hvm.gfx_passthru_kind; |
| 991 |
|
| 992 |
if (libxl__is_igd_vga_passthru(gc, guest_config)) { |
| 993 |
return LIBXL_GFX_PASSTHRU_KIND_IGD; |
| 994 |
} |
| 995 |
|
| 996 |
return LIBXL_GFX_PASSTHRU_KIND_DEFAULT; |
| 997 |
} |
| 998 |
|
| 999 |
/* colo mode */ |
| 1000 |
enum { |
| 1001 |
LIBXL__COLO_NONE = 0, |
| 1002 |
LIBXL__COLO_PRIMARY, |
| 1003 |
LIBXL__COLO_SECONDARY, |
| 1004 |
}; |
| 1005 |
|
| 1006 |
static char *qemu_disk_scsi_drive_string(libxl__gc *gc, const char *target_path, |
| 1007 |
int unit, const char *format, |
| 1008 |
const libxl_device_disk *disk, |
| 1009 |
int colo_mode, const char **id_ptr) |
| 1010 |
{ |
| 1011 |
char *drive = NULL; |
| 1012 |
char *common = GCSPRINTF("if=none,readonly=%s,cache=writeback", |
| 1013 |
disk->readwrite ? "off" : "on"); |
| 1014 |
const char *exportname = disk->colo_export; |
| 1015 |
const char *active_disk = disk->active_disk; |
| 1016 |
const char *hidden_disk = disk->hidden_disk; |
| 1017 |
const char *id; |
| 1018 |
|
| 1019 |
switch (colo_mode) { |
| 1020 |
case LIBXL__COLO_NONE: |
| 1021 |
id = GCSPRINTF("scsi0-hd%d", unit); |
| 1022 |
drive = GCSPRINTF("file=%s,id=%s,format=%s,%s", |
| 1023 |
target_path, id, format, common); |
| 1024 |
break; |
| 1025 |
case LIBXL__COLO_PRIMARY: |
| 1026 |
id = exportname; |
| 1027 |
drive = GCSPRINTF( |
| 1028 |
"%s,id=%s,driver=quorum," |
| 1029 |
"children.0.file.filename=%s," |
| 1030 |
"children.0.driver=%s," |
| 1031 |
"read-pattern=fifo," |
| 1032 |
"vote-threshold=1", |
| 1033 |
common, id, target_path, format); |
| 1034 |
break; |
| 1035 |
case LIBXL__COLO_SECONDARY: |
| 1036 |
id = "top-colo"; |
| 1037 |
drive = GCSPRINTF( |
| 1038 |
"%s,id=%s,driver=replication," |
| 1039 |
"mode=secondary," |
| 1040 |
"top-id=top-colo," |
| 1041 |
"file.driver=qcow2," |
| 1042 |
"file.file.filename=%s," |
| 1043 |
"file.backing.driver=qcow2," |
| 1044 |
"file.backing.file.filename=%s," |
| 1045 |
"file.backing.backing=%s", |
| 1046 |
common, id, active_disk, hidden_disk, exportname); |
| 1047 |
break; |
| 1048 |
default: |
| 1049 |
abort(); |
| 1050 |
} |
| 1051 |
|
| 1052 |
*id_ptr = id; |
| 1053 |
|
| 1054 |
return drive; |
| 1055 |
} |
| 1056 |
|
| 1057 |
static char *qemu_disk_ide_drive_string(libxl__gc *gc, const char *target_path, |
| 1058 |
int unit, const char *format, |
| 1059 |
const libxl_device_disk *disk, |
| 1060 |
int colo_mode) |
| 1061 |
{ |
| 1062 |
char *drive = NULL; |
| 1063 |
const char *exportname = disk->colo_export; |
| 1064 |
const char *active_disk = disk->active_disk; |
| 1065 |
const char *hidden_disk = disk->hidden_disk; |
| 1066 |
|
| 1067 |
assert(disk->readwrite); /* should have been checked earlier */ |
| 1068 |
|
| 1069 |
switch (colo_mode) { |
| 1070 |
case LIBXL__COLO_NONE: |
| 1071 |
drive = GCSPRINTF |
| 1072 |
("file=%s,if=ide,index=%d,media=disk,format=%s,cache=writeback", |
| 1073 |
target_path, unit, format); |
| 1074 |
break; |
| 1075 |
case LIBXL__COLO_PRIMARY: |
| 1076 |
/* |
| 1077 |
* primary: |
| 1078 |
* -dirve if=ide,index=x,media=disk,cache=writeback,driver=quorum,\ |
| 1079 |
* id=exportname,\ |
| 1080 |
* children.0.file.filename=target_path,\ |
| 1081 |
* children.0.driver=format,\ |
| 1082 |
* read-pattern=fifo,\ |
| 1083 |
* vote-threshold=1 |
| 1084 |
*/ |
| 1085 |
drive = GCSPRINTF( |
| 1086 |
"if=ide,index=%d,media=disk,cache=writeback,driver=quorum," |
| 1087 |
"id=%s," |
| 1088 |
"children.0.file.filename=%s," |
| 1089 |
"children.0.driver=%s," |
| 1090 |
"read-pattern=fifo," |
| 1091 |
"vote-threshold=1", |
| 1092 |
unit, exportname, target_path, format); |
| 1093 |
break; |
| 1094 |
case LIBXL__COLO_SECONDARY: |
| 1095 |
/* |
| 1096 |
* secondary: |
| 1097 |
* -drive if=ide,index=x,media=disk,cache=writeback,driver=replication,\ |
| 1098 |
* mode=secondary,\ |
| 1099 |
* file.driver=qcow2,\ |
| 1100 |
* file.file.filename=active_disk,\ |
| 1101 |
* file.backing.driver=qcow2,\ |
| 1102 |
* file.backing.file.filename=hidden_disk,\ |
| 1103 |
* file.backing.backing=exportname, |
| 1104 |
*/ |
| 1105 |
drive = GCSPRINTF( |
| 1106 |
"if=ide,index=%d,id=top-colo,media=disk,cache=writeback," |
| 1107 |
"driver=replication," |
| 1108 |
"mode=secondary," |
| 1109 |
"top-id=top-colo," |
| 1110 |
"file.driver=qcow2," |
| 1111 |
"file.file.filename=%s," |
| 1112 |
"file.backing.driver=qcow2," |
| 1113 |
"file.backing.file.filename=%s," |
| 1114 |
"file.backing.backing=%s", |
| 1115 |
unit, active_disk, hidden_disk, exportname); |
| 1116 |
break; |
| 1117 |
default: |
| 1118 |
abort(); |
| 1119 |
} |
| 1120 |
|
| 1121 |
return drive; |
| 1122 |
} |
| 1123 |
|
| 1124 |
static int libxl__pre_open_qmp_socket(libxl__gc *gc, libxl_domid domid, |
| 1125 |
int *fd_r) |
| 1126 |
{ |
| 1127 |
int rc, r; |
| 1128 |
int fd; |
| 1129 |
struct sockaddr_un un; |
| 1130 |
const char *path = libxl__qemu_qmp_path(gc, domid); |
| 1131 |
|
| 1132 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1133 |
if (fd < 0) { |
| 1134 |
LOGED(ERROR, domid, "socket() failed"); |
| 1135 |
return ERROR_FAIL; |
| 1136 |
} |
| 1137 |
|
| 1138 |
rc = libxl__prepare_sockaddr_un(gc, &un, path, "QEMU's QMP socket"); |
| 1139 |
if (rc) |
| 1140 |
goto out; |
| 1141 |
|
| 1142 |
rc = libxl__remove_file(gc, path); |
| 1143 |
if (rc) |
| 1144 |
goto out; |
| 1145 |
|
| 1146 |
r = bind(fd, (struct sockaddr *) &un, sizeof(un)); |
| 1147 |
if (r < 0) { |
| 1148 |
LOGED(ERROR, domid, "bind('%s') failed", path); |
| 1149 |
rc = ERROR_FAIL; |
| 1150 |
goto out; |
| 1151 |
} |
| 1152 |
|
| 1153 |
r = listen(fd, 1); |
| 1154 |
if (r < 0) { |
| 1155 |
LOGED(ERROR, domid, "listen() failed"); |
| 1156 |
rc = ERROR_FAIL; |
| 1157 |
goto out; |
| 1158 |
} |
| 1159 |
|
| 1160 |
*fd_r = fd; |
| 1161 |
rc = 0; |
| 1162 |
|
| 1163 |
out: |
| 1164 |
if (rc && fd >= 0) |
| 1165 |
close(fd); |
| 1166 |
return rc; |
| 1167 |
} |
| 1168 |
|
| 1169 |
static int libxl__build_device_model_args_new(libxl__gc *gc, |
| 1170 |
const char *dm, int guest_domid, |
| 1171 |
const libxl_domain_config *guest_config, |
| 1172 |
char ***args, char ***envs, |
| 1173 |
const libxl__domain_build_state *state, |
| 1174 |
const libxl__qemu_available_opts *qemu_opts, |
| 1175 |
int *dm_state_fd) |
| 1176 |
{ |
| 1177 |
const libxl_domain_create_info *c_info = &guest_config->c_info; |
| 1178 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 1179 |
const libxl_device_disk *disks = guest_config->disks; |
| 1180 |
const libxl_device_nic *nics = guest_config->nics; |
| 1181 |
const int num_disks = guest_config->num_disks; |
| 1182 |
const int num_nics = guest_config->num_nics; |
| 1183 |
const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config); |
| 1184 |
const libxl_sdl_info *sdl = dm_sdl(guest_config); |
| 1185 |
const char *keymap = dm_keymap(guest_config); |
| 1186 |
char *machinearg; |
| 1187 |
flexarray_t *dm_args, *dm_envs; |
| 1188 |
int i, connection, devid; |
| 1189 |
uint64_t ram_size; |
| 1190 |
const char *path, *chardev; |
| 1191 |
bool is_stubdom = libxl_defbool_val(b_info->device_model_stubdomain); |
| 1192 |
int rc; |
| 1193 |
|
| 1194 |
dm_args = flexarray_make(gc, 16, 1); |
| 1195 |
dm_envs = flexarray_make(gc, 16, 1); |
| 1196 |
|
| 1197 |
flexarray_vappend(dm_args, dm, |
| 1198 |
"-xen-domid", |
| 1199 |
GCSPRINTF("%d", guest_domid), NULL); |
| 1200 |
flexarray_append(dm_args, "-no-shutdown"); |
| 1201 |
|
| 1202 |
/* |
| 1203 |
* QMP access to qemu running in stubdomain is done over vchan. The |
| 1204 |
* stubdomain init script adds the appropriate monitor options for |
| 1205 |
* vchan-socket-proxy. |
| 1206 |
*/ |
| 1207 |
if (!is_stubdom) { |
| 1208 |
flexarray_append(dm_args, "-chardev"); |
| 1209 |
if (state->dm_monitor_fd >= 0) { |
| 1210 |
flexarray_append(dm_args, |
| 1211 |
GCSPRINTF("socket,id=libxl-cmd,fd=%d,server=on,wait=off", |
| 1212 |
state->dm_monitor_fd)); |
| 1213 |
|
| 1214 |
/* |
| 1215 |
* Start QEMU with its "CPU" paused, it will not start any emulation |
| 1216 |
* until the QMP command "cont" is used. This also prevent QEMU from |
| 1217 |
* writing "running" to the "state" xenstore node so we only use this |
| 1218 |
* flag when we have the QMP based startup notification. |
| 1219 |
* */ |
| 1220 |
flexarray_append(dm_args, "-S"); |
| 1221 |
} else { |
| 1222 |
flexarray_append(dm_args, |
| 1223 |
GCSPRINTF("socket,id=libxl-cmd," |
| 1224 |
"path=%s,server=on,wait=off", |
| 1225 |
libxl__qemu_qmp_path(gc, guest_domid))); |
| 1226 |
} |
| 1227 |
|
| 1228 |
flexarray_append(dm_args, "-mon"); |
| 1229 |
flexarray_append(dm_args, "chardev=libxl-cmd,mode=control"); |
| 1230 |
|
| 1231 |
flexarray_append(dm_args, "-chardev"); |
| 1232 |
flexarray_append(dm_args, |
| 1233 |
GCSPRINTF("socket,id=libxenstat-cmd," |
| 1234 |
"path=%s/qmp-libxenstat-%d,server=on,wait=off", |
| 1235 |
libxl__run_dir_path(), guest_domid)); |
| 1236 |
|
| 1237 |
flexarray_append(dm_args, "-mon"); |
| 1238 |
flexarray_append(dm_args, "chardev=libxenstat-cmd,mode=control"); |
| 1239 |
} |
| 1240 |
|
| 1241 |
for (i = 0; i < guest_config->num_channels; i++) { |
| 1242 |
connection = guest_config->channels[i].connection; |
| 1243 |
devid = guest_config->channels[i].devid; |
| 1244 |
switch (connection) { |
| 1245 |
case LIBXL_CHANNEL_CONNECTION_PTY: |
| 1246 |
chardev = GCSPRINTF("pty,id=libxl-channel%d", devid); |
| 1247 |
break; |
| 1248 |
case LIBXL_CHANNEL_CONNECTION_SOCKET: |
| 1249 |
path = guest_config->channels[i].u.socket.path; |
| 1250 |
chardev = GCSPRINTF("socket,id=libxl-channel%d,path=%s," |
| 1251 |
"server=on,wait=off", devid, path); |
| 1252 |
break; |
| 1253 |
default: |
| 1254 |
/* We've forgotten to add the clause */ |
| 1255 |
LOGD(ERROR, guest_domid, "%s: unknown channel connection %d", |
| 1256 |
__func__, connection); |
| 1257 |
return ERROR_INVAL; |
| 1258 |
} |
| 1259 |
flexarray_append(dm_args, "-chardev"); |
| 1260 |
flexarray_append(dm_args, (void*)chardev); |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* |
| 1264 |
* Remove default devices created by qemu. Qemu will create only devices |
| 1265 |
* defined by xen, since the devices not defined by xen are not usable. |
| 1266 |
*/ |
| 1267 |
flexarray_append(dm_args, "-nodefaults"); |
| 1268 |
|
| 1269 |
/* |
| 1270 |
* Do not use any of the user-provided config files in sysconfdir, |
| 1271 |
* avoiding unknown and uncontrolled configuration. |
| 1272 |
*/ |
| 1273 |
flexarray_append(dm_args, "-no-user-config"); |
| 1274 |
|
| 1275 |
if (b_info->type != LIBXL_DOMAIN_TYPE_HVM) { |
| 1276 |
flexarray_append(dm_args, "-xen-attach"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (c_info->name) { |
| 1280 |
flexarray_vappend(dm_args, "-name", c_info->name, NULL); |
| 1281 |
} |
| 1282 |
|
| 1283 |
if (vnc && !is_stubdom) { |
| 1284 |
char *vncarg = NULL; |
| 1285 |
|
| 1286 |
flexarray_append(dm_args, "-vnc"); |
| 1287 |
|
| 1288 |
/* |
| 1289 |
* If vnc->listen is present and contains a :, and |
| 1290 |
* - vnc->display is 0, use vnc->listen |
| 1291 |
* - vnc->display is non-zero, be confused |
| 1292 |
* If vnc->listen is present but doesn't, use vnc->listen:vnc->display. |
| 1293 |
* If vnc->listen is not present, use 127.0.0.1:vnc->display |
| 1294 |
* (Remembering that vnc->display already defaults to 0.) |
| 1295 |
*/ |
| 1296 |
if (vnc->listen) { |
| 1297 |
if (strchr(vnc->listen, ':') != NULL) { |
| 1298 |
if (vnc->display) { |
| 1299 |
LOGD(ERROR, guest_domid, |
| 1300 |
"vncdisplay set, vnclisten contains display"); |
| 1301 |
return ERROR_INVAL; |
| 1302 |
} |
| 1303 |
vncarg = vnc->listen; |
| 1304 |
} else { |
| 1305 |
vncarg = GCSPRINTF("%s:%d", vnc->listen, vnc->display); |
| 1306 |
} |
| 1307 |
} else |
| 1308 |
vncarg = GCSPRINTF("127.0.0.1:%d", vnc->display); |
| 1309 |
|
| 1310 |
if (vnc->passwd && vnc->passwd[0]) { |
| 1311 |
vncarg = GCSPRINTF("%s,password=on", vncarg); |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (libxl_defbool_val(vnc->findunused)) { |
| 1315 |
/* This option asks to QEMU to try this number of port before to |
| 1316 |
* give up. So QEMU will try ports between $display and $display + |
| 1317 |
* 99. This option needs to be the last one of the vnc options. */ |
| 1318 |
vncarg = GCSPRINTF("%s,to=99", vncarg); |
| 1319 |
} |
| 1320 |
|
| 1321 |
flexarray_append(dm_args, vncarg); |
| 1322 |
} else if (!is_stubdom) { |
| 1323 |
/* |
| 1324 |
* Ensure that by default no vnc server is created. |
| 1325 |
*/ |
| 1326 |
flexarray_append_pair(dm_args, "-vnc", "none"); |
| 1327 |
} |
| 1328 |
|
| 1329 |
/* |
| 1330 |
* Ensure that by default no display backend is created. Further |
| 1331 |
* options given below might then enable more. |
| 1332 |
*/ |
| 1333 |
flexarray_append_pair(dm_args, "-display", "none"); |
| 1334 |
|
| 1335 |
if (sdl && !is_stubdom) { |
| 1336 |
flexarray_append_pair(dm_args, "-display", "sdl"); |
| 1337 |
if (sdl->display) |
| 1338 |
flexarray_append_pair(dm_envs, "DISPLAY", sdl->display); |
| 1339 |
if (sdl->xauthority) |
| 1340 |
flexarray_append_pair(dm_envs, "XAUTHORITY", sdl->xauthority); |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (keymap) { |
| 1344 |
flexarray_vappend(dm_args, "-k", keymap, NULL); |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) { |
| 1348 |
int ioemu_nics = 0; |
| 1349 |
|
| 1350 |
if (b_info->kernel) |
| 1351 |
flexarray_vappend(dm_args, "-kernel", b_info->kernel, NULL); |
| 1352 |
|
| 1353 |
if (b_info->ramdisk) |
| 1354 |
flexarray_vappend(dm_args, "-initrd", b_info->ramdisk, NULL); |
| 1355 |
|
| 1356 |
if (b_info->cmdline) |
| 1357 |
flexarray_vappend(dm_args, "-append", b_info->cmdline, NULL); |
| 1358 |
|
| 1359 |
/* Find out early if one of the disk is on the scsi bus and add a scsi |
| 1360 |
* controller. This is done ahead to keep the same behavior as previous |
| 1361 |
* version of QEMU (have the controller on the same PCI slot). */ |
| 1362 |
for (i = 0; i < num_disks; i++) { |
| 1363 |
if (disks[i].is_cdrom) { |
| 1364 |
continue; |
| 1365 |
} |
| 1366 |
if (strncmp(disks[i].vdev, "sd", 2) == 0) { |
| 1367 |
flexarray_vappend(dm_args, "-device", "lsi53c895a", NULL); |
| 1368 |
break; |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (b_info->u.hvm.serial || b_info->u.hvm.serial_list) { |
| 1373 |
if ( b_info->u.hvm.serial && b_info->u.hvm.serial_list ) |
| 1374 |
{ |
| 1375 |
LOGD(ERROR, guest_domid, "Both serial and serial_list set"); |
| 1376 |
return ERROR_INVAL; |
| 1377 |
} else { |
| 1378 |
if (b_info->u.hvm.serial) { |
| 1379 |
if (is_stubdom) { |
| 1380 |
/* see spawn_stub_launch_dm() for connecting STUBDOM_CONSOLE_SERIAL */ |
| 1381 |
flexarray_vappend(dm_args, |
| 1382 |
"-serial", |
| 1383 |
GCSPRINTF("/dev/hvc%d", STUBDOM_CONSOLE_SERIAL), |
| 1384 |
NULL); |
| 1385 |
} else { |
| 1386 |
flexarray_vappend(dm_args, |
| 1387 |
"-serial", b_info->u.hvm.serial, NULL); |
| 1388 |
} |
| 1389 |
} else if (b_info->u.hvm.serial_list) { |
| 1390 |
char **p; |
| 1391 |
/* see spawn_stub_launch_dm() for connecting STUBDOM_CONSOLE_SERIAL */ |
| 1392 |
for (p = b_info->u.hvm.serial_list, i = 0; |
| 1393 |
*p; |
| 1394 |
p++, i++) { |
| 1395 |
if (is_stubdom) |
| 1396 |
flexarray_vappend(dm_args, |
| 1397 |
"-serial", |
| 1398 |
GCSPRINTF("/dev/hvc%d", STUBDOM_CONSOLE_SERIAL + i), |
| 1399 |
NULL); |
| 1400 |
else |
| 1401 |
flexarray_vappend(dm_args, |
| 1402 |
"-serial", |
| 1403 |
*p, NULL); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
if (libxl_defbool_val(b_info->u.hvm.nographic) && (!sdl && !vnc)) { |
| 1410 |
flexarray_append(dm_args, "-nographic"); |
| 1411 |
} |
| 1412 |
|
| 1413 |
if (libxl_defbool_val(b_info->u.hvm.spice.enable) && !is_stubdom) { |
| 1414 |
const libxl_spice_info *spice = &b_info->u.hvm.spice; |
| 1415 |
char *spiceoptions = dm_spice_options(gc, spice); |
| 1416 |
if (!spiceoptions) |
| 1417 |
return ERROR_INVAL; |
| 1418 |
|
| 1419 |
flexarray_append(dm_args, "-spice"); |
| 1420 |
flexarray_append(dm_args, spiceoptions); |
| 1421 |
if (libxl_defbool_val(b_info->u.hvm.spice.vdagent)) { |
| 1422 |
flexarray_vappend(dm_args, "-device", "virtio-serial", |
| 1423 |
"-chardev", "spicevmc,id=vdagent,name=vdagent", "-device", |
| 1424 |
"virtserialport,chardev=vdagent,name=com.redhat.spice.0", |
| 1425 |
NULL); |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
switch (b_info->u.hvm.vga.kind) { |
| 1430 |
case LIBXL_VGA_INTERFACE_TYPE_STD: |
| 1431 |
flexarray_append_pair(dm_args, "-device", |
| 1432 |
GCSPRINTF("VGA,vgamem_mb=%d", |
| 1433 |
libxl__sizekb_to_mb(b_info->video_memkb))); |
| 1434 |
break; |
| 1435 |
case LIBXL_VGA_INTERFACE_TYPE_CIRRUS: |
| 1436 |
flexarray_append_pair(dm_args, "-device", |
| 1437 |
GCSPRINTF("cirrus-vga,vgamem_mb=%d", |
| 1438 |
libxl__sizekb_to_mb(b_info->video_memkb))); |
| 1439 |
break; |
| 1440 |
case LIBXL_VGA_INTERFACE_TYPE_NONE: |
| 1441 |
break; |
| 1442 |
case LIBXL_VGA_INTERFACE_TYPE_QXL: |
| 1443 |
/* QXL have 2 ram regions, ram and vram */ |
| 1444 |
flexarray_append_pair(dm_args, "-device", |
| 1445 |
GCSPRINTF("qxl-vga,vram_size_mb=%"PRIu64",ram_size_mb=%"PRIu64, |
| 1446 |
(b_info->video_memkb/2/1024), (b_info->video_memkb/2/1024) ) ); |
| 1447 |
break; |
| 1448 |
default: |
| 1449 |
LOGD(ERROR, guest_domid, "Invalid emulated video card specified"); |
| 1450 |
return ERROR_INVAL; |
| 1451 |
} |
| 1452 |
|
| 1453 |
if (b_info->u.hvm.boot) { |
| 1454 |
flexarray_vappend(dm_args, "-boot", |
| 1455 |
GCSPRINTF("order=%s", b_info->u.hvm.boot), NULL); |
| 1456 |
} |
| 1457 |
if (libxl_defbool_val(b_info->u.hvm.usb) |
| 1458 |
|| b_info->u.hvm.usbdevice |
| 1459 |
|| libxl_string_list_length(&b_info->u.hvm.usbdevice_list)) { |
| 1460 |
if (b_info->u.hvm.usbdevice |
| 1461 |
&& libxl_string_list_length(&b_info->u.hvm.usbdevice_list)) { |
| 1462 |
LOGD(ERROR, guest_domid, "Both usbdevice and usbdevice_list set"); |
| 1463 |
return ERROR_INVAL; |
| 1464 |
} |
| 1465 |
flexarray_append(dm_args, "-usb"); |
| 1466 |
if (b_info->u.hvm.usbdevice) { |
| 1467 |
flexarray_vappend(dm_args, |
| 1468 |
"-usbdevice", b_info->u.hvm.usbdevice, NULL); |
| 1469 |
} else if (b_info->u.hvm.usbdevice_list) { |
| 1470 |
char **p; |
| 1471 |
for (p = b_info->u.hvm.usbdevice_list; |
| 1472 |
*p; |
| 1473 |
p++) { |
| 1474 |
flexarray_vappend(dm_args, |
| 1475 |
"-usbdevice", |
| 1476 |
*p, NULL); |
| 1477 |
} |
| 1478 |
} |
| 1479 |
} else if (b_info->u.hvm.usbversion) { |
| 1480 |
switch (b_info->u.hvm.usbversion) { |
| 1481 |
case 1: |
| 1482 |
flexarray_vappend(dm_args, |
| 1483 |
"-device", "piix3-usb-uhci,id=usb", NULL); |
| 1484 |
break; |
| 1485 |
case 2: |
| 1486 |
flexarray_append_pair(dm_args, "-device", |
| 1487 |
"ich9-usb-ehci1,id=usb,addr=0x1d.0x7,multifunction=on"); |
| 1488 |
for (i = 1; i < 4; i++) |
| 1489 |
flexarray_append_pair(dm_args, "-device", |
| 1490 |
GCSPRINTF("ich9-usb-uhci%d,masterbus=usb.0," |
| 1491 |
"firstport=%d,addr=0x1d.%#x,multifunction=on", |
| 1492 |
i, 2*(i-1), i-1)); |
| 1493 |
break; |
| 1494 |
case 3: |
| 1495 |
flexarray_vappend(dm_args, |
| 1496 |
"-device", "nec-usb-xhci,id=usb", NULL); |
| 1497 |
break; |
| 1498 |
default: |
| 1499 |
LOGD(ERROR, guest_domid, "usbversion parameter is invalid, " |
| 1500 |
"must be between 1 and 3"); |
| 1501 |
return ERROR_INVAL; |
| 1502 |
} |
| 1503 |
if (b_info->u.hvm.spice.usbredirection >= 0 && |
| 1504 |
b_info->u.hvm.spice.usbredirection < 5) { |
| 1505 |
for (i = 1; i <= b_info->u.hvm.spice.usbredirection; i++) |
| 1506 |
flexarray_vappend(dm_args, "-chardev", |
| 1507 |
GCSPRINTF("spicevmc,name=usbredir,id=usbrc%d", i), |
| 1508 |
"-device", |
| 1509 |
GCSPRINTF("usb-redir,chardev=usbrc%d," |
| 1510 |
"id=usbrc%d", i, i), NULL); |
| 1511 |
} else { |
| 1512 |
LOGD(ERROR, guest_domid, "usbredirection parameter is invalid, " |
| 1513 |
"it must be between 1 and 4"); |
| 1514 |
return ERROR_INVAL; |
| 1515 |
} |
| 1516 |
} |
| 1517 |
if (b_info->u.hvm.soundhw) { |
| 1518 |
libxl__qemu_soundhw soundhw; |
| 1519 |
|
| 1520 |
rc = libxl__qemu_soundhw_from_string(b_info->u.hvm.soundhw, &soundhw); |
| 1521 |
if (rc) { |
| 1522 |
LOGD(ERROR, guest_domid, "Unknown soundhw option '%s'", b_info->u.hvm.soundhw); |
| 1523 |
return ERROR_INVAL; |
| 1524 |
} |
| 1525 |
|
| 1526 |
switch (soundhw) { |
| 1527 |
case LIBXL__QEMU_SOUNDHW_HDA: |
| 1528 |
flexarray_vappend(dm_args, "-device", "intel-hda", |
| 1529 |
"-device", "hda-duplex", NULL); |
| 1530 |
break; |
| 1531 |
default: |
| 1532 |
flexarray_append_pair(dm_args, "-device", |
| 1533 |
(char*)libxl__qemu_soundhw_to_string(soundhw)); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
if (!libxl__acpi_defbool_val(b_info)) { |
| 1537 |
flexarray_append(dm_args, "-no-acpi"); |
| 1538 |
} |
| 1539 |
if (b_info->max_vcpus > 1) { |
| 1540 |
flexarray_append(dm_args, "-smp"); |
| 1541 |
if (b_info->avail_vcpus.size) { |
| 1542 |
int nr_set_cpus = 0; |
| 1543 |
nr_set_cpus = libxl_bitmap_count_set(&b_info->avail_vcpus); |
| 1544 |
|
| 1545 |
flexarray_append(dm_args, GCSPRINTF("%d,maxcpus=%d", |
| 1546 |
nr_set_cpus, |
| 1547 |
b_info->max_vcpus)); |
| 1548 |
} else |
| 1549 |
flexarray_append(dm_args, GCSPRINTF("%d", b_info->max_vcpus)); |
| 1550 |
} |
| 1551 |
for (i = 0; i < num_nics; i++) { |
| 1552 |
if (nics[i].nictype == LIBXL_NIC_TYPE_VIF_IOEMU) { |
| 1553 |
char *smac = GCSPRINTF(LIBXL_MAC_FMT, |
| 1554 |
LIBXL_MAC_BYTES(nics[i].mac)); |
| 1555 |
const char *ifname = libxl__device_nic_devname(gc, |
| 1556 |
guest_domid, nics[i].devid, |
| 1557 |
LIBXL_NIC_TYPE_VIF_IOEMU); |
| 1558 |
flexarray_append(dm_args, "-device"); |
| 1559 |
flexarray_append(dm_args, |
| 1560 |
GCSPRINTF("%s,id=nic%d,netdev=net%d,mac=%s", |
| 1561 |
nics[i].model, nics[i].devid, |
| 1562 |
nics[i].devid, smac)); |
| 1563 |
flexarray_append(dm_args, "-netdev"); |
| 1564 |
flexarray_append(dm_args, |
| 1565 |
GCSPRINTF("type=tap,id=net%d,ifname=%s," |
| 1566 |
"br=%s," |
| 1567 |
"script=%s,downscript=%s", |
| 1568 |
nics[i].devid, ifname, |
| 1569 |
nics[i].bridge, |
| 1570 |
libxl_tapif_script(gc), |
| 1571 |
libxl_tapif_script(gc))); |
| 1572 |
|
| 1573 |
/* Userspace COLO Proxy need this */ |
| 1574 |
#define APPEND_COLO_SOCK_SERVER(sock_id, sock_ip, sock_port) ({ \ |
| 1575 |
if (nics[i].colo_##sock_id && \ |
| 1576 |
nics[i].colo_##sock_ip && \ |
| 1577 |
nics[i].colo_##sock_port) { \ |
| 1578 |
flexarray_append(dm_args, "-chardev"); \ |
| 1579 |
flexarray_append(dm_args, \ |
| 1580 |
GCSPRINTF("socket,id=%s,host=%s,port=%s,server=on,wait=off", \ |
| 1581 |
nics[i].colo_##sock_id, \ |
| 1582 |
nics[i].colo_##sock_ip, \ |
| 1583 |
nics[i].colo_##sock_port)); \ |
| 1584 |
} \ |
| 1585 |
}) |
| 1586 |
|
| 1587 |
#define APPEND_COLO_SOCK_CLIENT(sock_id, sock_ip, sock_port) ({ \ |
| 1588 |
if (nics[i].colo_##sock_id && \ |
| 1589 |
nics[i].colo_##sock_ip && \ |
| 1590 |
nics[i].colo_##sock_port) { \ |
| 1591 |
flexarray_append(dm_args, "-chardev"); \ |
| 1592 |
flexarray_append(dm_args, \ |
| 1593 |
GCSPRINTF("socket,id=%s,host=%s,port=%s", \ |
| 1594 |
nics[i].colo_##sock_id, \ |
| 1595 |
nics[i].colo_##sock_ip, \ |
| 1596 |
nics[i].colo_##sock_port)); \ |
| 1597 |
} \ |
| 1598 |
}) |
| 1599 |
|
| 1600 |
if (state->saved_state) { |
| 1601 |
/* secondary colo run */ |
| 1602 |
|
| 1603 |
APPEND_COLO_SOCK_CLIENT(sock_sec_redirector0_id, |
| 1604 |
sock_sec_redirector0_ip, |
| 1605 |
sock_sec_redirector0_port); |
| 1606 |
|
| 1607 |
APPEND_COLO_SOCK_CLIENT(sock_sec_redirector1_id, |
| 1608 |
sock_sec_redirector1_ip, |
| 1609 |
sock_sec_redirector1_port); |
| 1610 |
|
| 1611 |
if (nics[i].colo_filter_sec_redirector0_queue && |
| 1612 |
nics[i].colo_filter_sec_redirector0_indev) { |
| 1613 |
flexarray_append(dm_args, "-object"); |
| 1614 |
flexarray_append(dm_args, |
| 1615 |
GCSPRINTF("filter-redirector,id=rs1,netdev=net%d,queue=%s,indev=%s", |
| 1616 |
nics[i].devid, |
| 1617 |
nics[i].colo_filter_sec_redirector0_queue, |
| 1618 |
nics[i].colo_filter_sec_redirector0_indev)); |
| 1619 |
} |
| 1620 |
if (nics[i].colo_filter_sec_redirector1_queue && |
| 1621 |
nics[i].colo_filter_sec_redirector1_outdev) { |
| 1622 |
flexarray_append(dm_args, "-object"); |
| 1623 |
flexarray_append(dm_args, |
| 1624 |
GCSPRINTF("filter-redirector,id=rs2,netdev=net%d,queue=%s,outdev=%s", |
| 1625 |
nics[i].devid, |
| 1626 |
nics[i].colo_filter_sec_redirector1_queue, |
| 1627 |
nics[i].colo_filter_sec_redirector1_outdev)); |
| 1628 |
} |
| 1629 |
if (nics[i].colo_filter_sec_rewriter0_queue) { |
| 1630 |
flexarray_append(dm_args, "-object"); |
| 1631 |
flexarray_append(dm_args, |
| 1632 |
GCSPRINTF("filter-rewriter,id=rs3,netdev=net%d,queue=%s", |
| 1633 |
nics[i].devid, |
| 1634 |
nics[i].colo_filter_sec_rewriter0_queue)); |
| 1635 |
} |
| 1636 |
} else { |
| 1637 |
/* primary colo run */ |
| 1638 |
|
| 1639 |
APPEND_COLO_SOCK_SERVER(sock_mirror_id, |
| 1640 |
sock_mirror_ip, |
| 1641 |
sock_mirror_port); |
| 1642 |
|
| 1643 |
APPEND_COLO_SOCK_SERVER(sock_compare_pri_in_id, |
| 1644 |
sock_compare_pri_in_ip, |
| 1645 |
sock_compare_pri_in_port); |
| 1646 |
|
| 1647 |
APPEND_COLO_SOCK_SERVER(sock_compare_sec_in_id, |
| 1648 |
sock_compare_sec_in_ip, |
| 1649 |
sock_compare_sec_in_port); |
| 1650 |
|
| 1651 |
APPEND_COLO_SOCK_SERVER(sock_compare_notify_id, |
| 1652 |
sock_compare_notify_ip, |
| 1653 |
sock_compare_notify_port); |
| 1654 |
|
| 1655 |
APPEND_COLO_SOCK_SERVER(sock_redirector0_id, |
| 1656 |
sock_redirector0_ip, |
| 1657 |
sock_redirector0_port); |
| 1658 |
|
| 1659 |
APPEND_COLO_SOCK_CLIENT(sock_redirector1_id, |
| 1660 |
sock_redirector1_ip, |
| 1661 |
sock_redirector1_port); |
| 1662 |
|
| 1663 |
APPEND_COLO_SOCK_CLIENT(sock_redirector2_id, |
| 1664 |
sock_redirector2_ip, |
| 1665 |
sock_redirector2_port); |
| 1666 |
|
| 1667 |
if (nics[i].colo_filter_mirror_queue && |
| 1668 |
nics[i].colo_filter_mirror_outdev) { |
| 1669 |
flexarray_append(dm_args, "-object"); |
| 1670 |
flexarray_append(dm_args, |
| 1671 |
GCSPRINTF("filter-mirror,id=m1,netdev=net%d,queue=%s,outdev=%s", |
| 1672 |
nics[i].devid, |
| 1673 |
nics[i].colo_filter_mirror_queue, |
| 1674 |
nics[i].colo_filter_mirror_outdev)); |
| 1675 |
} |
| 1676 |
if (nics[i].colo_filter_redirector0_queue && |
| 1677 |
nics[i].colo_filter_redirector0_indev) { |
| 1678 |
flexarray_append(dm_args, "-object"); |
| 1679 |
flexarray_append(dm_args, |
| 1680 |
GCSPRINTF("filter-redirector,id=r1,netdev=net%d,queue=%s,indev=%s", |
| 1681 |
nics[i].devid, |
| 1682 |
nics[i].colo_filter_redirector0_queue, |
| 1683 |
nics[i].colo_filter_redirector0_indev)); |
| 1684 |
} |
| 1685 |
if (nics[i].colo_filter_redirector1_queue && |
| 1686 |
nics[i].colo_filter_redirector1_outdev) { |
| 1687 |
flexarray_append(dm_args, "-object"); |
| 1688 |
flexarray_append(dm_args, |
| 1689 |
GCSPRINTF("filter-redirector,id=r2,netdev=net%d,queue=%s,outdev=%s", |
| 1690 |
nics[i].devid, |
| 1691 |
nics[i].colo_filter_redirector1_queue, |
| 1692 |
nics[i].colo_filter_redirector1_outdev)); |
| 1693 |
} |
| 1694 |
if (nics[i].colo_compare_pri_in && |
| 1695 |
nics[i].colo_compare_sec_in && |
| 1696 |
nics[i].colo_compare_out && |
| 1697 |
nics[i].colo_compare_notify_dev) { |
| 1698 |
flexarray_append(dm_args, "-object"); |
| 1699 |
flexarray_append(dm_args, "iothread,id=colo-compare-iothread-1"); |
| 1700 |
flexarray_append(dm_args, "-object"); |
| 1701 |
flexarray_append(dm_args, |
| 1702 |
GCSPRINTF("colo-compare,id=c1,primary_in=%s,secondary_in=%s,outdev=%s,notify_dev=%s,iothread=colo-compare-iothread-1", |
| 1703 |
nics[i].colo_compare_pri_in, |
| 1704 |
nics[i].colo_compare_sec_in, |
| 1705 |
nics[i].colo_compare_out, |
| 1706 |
nics[i].colo_compare_notify_dev)); |
| 1707 |
} |
| 1708 |
} |
| 1709 |
ioemu_nics++; |
| 1710 |
|
| 1711 |
#undef APPEND_COLO_SOCK_SERVER |
| 1712 |
#undef APPEND_COLO_SOCK_CLIENT |
| 1713 |
} |
| 1714 |
} |
| 1715 |
/* If we have no emulated nics, tell qemu not to create any */ |
| 1716 |
if ( ioemu_nics == 0 ) { |
| 1717 |
flexarray_append(dm_args, "-net"); |
| 1718 |
flexarray_append(dm_args, "none"); |
| 1719 |
} |
| 1720 |
} else { |
| 1721 |
if (!sdl && !vnc) { |
| 1722 |
flexarray_append(dm_args, "-nographic"); |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (libxl_defbool_val(b_info->dm_restrict)) { |
| 1727 |
char *chroot_dir = GCSPRINTF("%s/qemu-root-%d", |
| 1728 |
libxl__run_dir_path(), guest_domid); |
| 1729 |
int r; |
| 1730 |
|
| 1731 |
flexarray_append(dm_args, "-xen-domid-restrict"); |
| 1732 |
|
| 1733 |
/* |
| 1734 |
* Run QEMU in a chroot at XEN_RUN_DIR/qemu-root-<domid> |
| 1735 |
* |
| 1736 |
* There is no library function to do the equivalent of `rm |
| 1737 |
* -rf`. However deprivileged QEMU in theory shouldn't be |
| 1738 |
* able to write any files, as the chroot would be owned by |
| 1739 |
* root, but it would be running as an unprivileged process. |
| 1740 |
* So in theory, old chroots should always be empty. |
| 1741 |
* |
| 1742 |
* rmdir the directory before attempting to create |
| 1743 |
* it; if it returns anything other than ENOENT, fail domain |
| 1744 |
* creation. |
| 1745 |
*/ |
| 1746 |
r = rmdir(chroot_dir); |
| 1747 |
if (r != 0 && errno != ENOENT) { |
| 1748 |
LOGED(ERROR, guest_domid, |
| 1749 |
"failed to remove existing chroot dir %s", chroot_dir); |
| 1750 |
return ERROR_FAIL; |
| 1751 |
} |
| 1752 |
|
| 1753 |
for (;;) { |
| 1754 |
r = mkdir(chroot_dir, 0000); |
| 1755 |
if (!r) |
| 1756 |
break; |
| 1757 |
if (errno == EINTR) continue; |
| 1758 |
LOGED(ERROR, guest_domid, |
| 1759 |
"failed to create chroot dir %s", chroot_dir); |
| 1760 |
return ERROR_FAIL; |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* Add "-chroot [dir]" to command-line */ |
| 1764 |
if (qemu_opts->have_runwith_chroot) { |
| 1765 |
flexarray_append_pair(dm_args, "-run-with", |
| 1766 |
GCSPRINTF("chroot=%s", chroot_dir)); |
| 1767 |
} else { |
| 1768 |
flexarray_append(dm_args, "-chroot"); |
| 1769 |
flexarray_append(dm_args, chroot_dir); |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
if (state->saved_state) { |
| 1774 |
if (is_stubdom) { |
| 1775 |
/* Linux stubdomain must replace $STUBDOM_RESTORE_INCOMING_ARG |
| 1776 |
* with the approriate fd:$num argument for the |
| 1777 |
* STUBDOM_CONSOLE_RESTORE console 2. |
| 1778 |
*/ |
| 1779 |
flexarray_append(dm_args, "-incoming"); |
| 1780 |
flexarray_append(dm_args, "$STUBDOM_RESTORE_INCOMING_ARG"); |
| 1781 |
} else { |
| 1782 |
/* This file descriptor is meant to be used by QEMU */ |
| 1783 |
*dm_state_fd = open(state->saved_state, O_RDONLY); |
| 1784 |
flexarray_append(dm_args, "-incoming"); |
| 1785 |
flexarray_append(dm_args, GCSPRINTF("fd:%d",*dm_state_fd)); |
| 1786 |
} |
| 1787 |
} |
| 1788 |
for (i = 0; b_info->extra && b_info->extra[i] != NULL; i++) |
| 1789 |
flexarray_append(dm_args, b_info->extra[i]); |
| 1790 |
|
| 1791 |
flexarray_append(dm_args, "-machine"); |
| 1792 |
switch (b_info->type) { |
| 1793 |
case LIBXL_DOMAIN_TYPE_PVH: |
| 1794 |
case LIBXL_DOMAIN_TYPE_PV: |
| 1795 |
flexarray_append(dm_args, "xenpv"); |
| 1796 |
for (i = 0; b_info->extra_pv && b_info->extra_pv[i] != NULL; i++) |
| 1797 |
flexarray_append(dm_args, b_info->extra_pv[i]); |
| 1798 |
break; |
| 1799 |
case LIBXL_DOMAIN_TYPE_HVM: |
| 1800 |
if (!libxl_defbool_val(b_info->u.hvm.xen_platform_pci)) { |
| 1801 |
/* Switching here to the machine "pc" which does not add |
| 1802 |
* the xen-platform device instead of the default "xenfv" machine. |
| 1803 |
*/ |
| 1804 |
machinearg = libxl__strdup(gc, "pc,accel=xen,suppress-vmdesc=on"); |
| 1805 |
} else { |
| 1806 |
machinearg = libxl__strdup(gc, "xenfv,suppress-vmdesc=on"); |
| 1807 |
} |
| 1808 |
if (b_info->u.hvm.mmio_hole_memkb) { |
| 1809 |
uint64_t max_ram_below_4g = (1ULL << 32) - |
| 1810 |
(b_info->u.hvm.mmio_hole_memkb << 10); |
| 1811 |
|
| 1812 |
if (max_ram_below_4g > HVM_BELOW_4G_MMIO_START) { |
| 1813 |
LOGD(WARN, guest_domid, "mmio_hole_memkb=%"PRIu64 |
| 1814 |
" invalid ignored.\n", |
| 1815 |
b_info->u.hvm.mmio_hole_memkb); |
| 1816 |
} else { |
| 1817 |
machinearg = GCSPRINTF("%s,max-ram-below-4g=%"PRIu64, |
| 1818 |
machinearg, max_ram_below_4g); |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
if (libxl_defbool_val(b_info->u.hvm.gfx_passthru)) { |
| 1823 |
enum libxl_gfx_passthru_kind gfx_passthru_kind = |
| 1824 |
libxl__detect_gfx_passthru_kind(gc, guest_config); |
| 1825 |
switch (gfx_passthru_kind) { |
| 1826 |
case LIBXL_GFX_PASSTHRU_KIND_IGD: |
| 1827 |
machinearg = GCSPRINTF("%s,igd-passthru=on", machinearg); |
| 1828 |
break; |
| 1829 |
case LIBXL_GFX_PASSTHRU_KIND_DEFAULT: |
| 1830 |
LOGD(ERROR, guest_domid, "unable to detect required gfx_passthru_kind"); |
| 1831 |
return ERROR_FAIL; |
| 1832 |
default: |
| 1833 |
LOGD(ERROR, guest_domid, "invalid value for gfx_passthru_kind"); |
| 1834 |
return ERROR_INVAL; |
| 1835 |
} |
| 1836 |
} |
| 1837 |
|
| 1838 |
flexarray_append(dm_args, machinearg); |
| 1839 |
for (i = 0; b_info->extra_hvm && b_info->extra_hvm[i] != NULL; i++) |
| 1840 |
flexarray_append(dm_args, b_info->extra_hvm[i]); |
| 1841 |
break; |
| 1842 |
default: |
| 1843 |
abort(); |
| 1844 |
} |
| 1845 |
|
| 1846 |
ram_size = libxl__sizekb_to_mb(b_info->max_memkb - b_info->video_memkb); |
| 1847 |
flexarray_append(dm_args, "-m"); |
| 1848 |
flexarray_append(dm_args, GCSPRINTF("%"PRId64, ram_size)); |
| 1849 |
|
| 1850 |
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) { |
| 1851 |
flexarray_append_pair(dm_envs, "XEN_DOMAIN_ID", GCSPRINTF("%d", guest_domid)); |
| 1852 |
|
| 1853 |
if (b_info->u.hvm.hdtype == LIBXL_HDTYPE_AHCI) |
| 1854 |
flexarray_append_pair(dm_args, "-device", "ahci,id=ahci0"); |
| 1855 |
for (i = 0; i < num_disks; i++) { |
| 1856 |
int disk, part; |
| 1857 |
int dev_number = |
| 1858 |
libxl__device_disk_dev_number(disks[i].vdev, &disk, &part); |
| 1859 |
const char *format; |
| 1860 |
char *drive; |
| 1861 |
const char *target_path = NULL; |
| 1862 |
int colo_mode; |
| 1863 |
|
| 1864 |
if (dev_number == -1) { |
| 1865 |
LOGD(WARN, guest_domid, "unable to determine"" disk number for %s", |
| 1866 |
disks[i].vdev); |
| 1867 |
continue; |
| 1868 |
} |
| 1869 |
|
| 1870 |
/* |
| 1871 |
* If qemu isn't doing the interpreting, the parameter is |
| 1872 |
* always raw |
| 1873 |
*/ |
| 1874 |
if (libxl_defbool_val(b_info->device_model_stubdomain)) |
| 1875 |
format = "host_device"; |
| 1876 |
else if (disks[i].backend == LIBXL_DISK_BACKEND_QDISK) |
| 1877 |
format = libxl__qemu_disk_format_string(disks[i].format); |
| 1878 |
else |
| 1879 |
format = libxl__qemu_disk_format_string(LIBXL_DISK_FORMAT_RAW); |
| 1880 |
|
| 1881 |
if (disks[i].format == LIBXL_DISK_FORMAT_EMPTY) { |
| 1882 |
if (!disks[i].is_cdrom) { |
| 1883 |
LOGD(WARN, guest_domid, "Cannot support empty disk format for %s", |
| 1884 |
disks[i].vdev); |
| 1885 |
continue; |
| 1886 |
} |
| 1887 |
} else if (libxl_defbool_val(b_info->device_model_stubdomain)) { |
| 1888 |
if (disk > 'z' - 'a') { |
| 1889 |
LOGD(WARN, guest_domid, |
| 1890 |
"Emulation of only first %d disks is supported with qemu-xen in stubdomain.\n" |
| 1891 |
"Disk %d will be available via PV drivers but not as an emulated disk.", |
| 1892 |
'z' - 'a', |
| 1893 |
disk); |
| 1894 |
continue; |
| 1895 |
} |
| 1896 |
target_path = GCSPRINTF("/dev/xvd%c", 'a' + disk); |
| 1897 |
} else { |
| 1898 |
if (format == NULL) { |
| 1899 |
LOGD(WARN, guest_domid, |
| 1900 |
"Unable to determine disk image format: %s\n" |
| 1901 |
"Disk will be available via PV drivers but not as an emulated disk.", |
| 1902 |
disks[i].vdev); |
| 1903 |
continue; |
| 1904 |
} |
| 1905 |
|
| 1906 |
target_path = libxl__device_disk_find_local_path(gc, |
| 1907 |
guest_domid, &disks[i], true); |
| 1908 |
|
| 1909 |
if (!target_path) { |
| 1910 |
LOGD(WARN, guest_domid, "No way to get local access disk to image: %s\n" |
| 1911 |
"Disk will be available via PV drivers but not as an emulated disk.", |
| 1912 |
disks[i].vdev); |
| 1913 |
continue; |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
if (disks[i].is_cdrom) { |
| 1918 |
const char *drive_id; |
| 1919 |
if (disk > 4) { |
| 1920 |
LOGD(WARN, guest_domid, "Emulated CDROM can be only one of the first 4 disks.\n" |
| 1921 |
"Disk %s will be available via PV drivers but not as an " |
| 1922 |
"emulated disk.", |
| 1923 |
disks[i].vdev); |
| 1924 |
continue; |
| 1925 |
} |
| 1926 |
|
| 1927 |
drive_id = GCSPRINTF("ide-%i", dev_number); |
| 1928 |
drive = GCSPRINTF("if=none,readonly=on,id=%s", drive_id); |
| 1929 |
|
| 1930 |
if (target_path) |
| 1931 |
drive = libxl__sprintf(gc, "%s,file=%s,format=%s", |
| 1932 |
drive, target_path, format); |
| 1933 |
|
| 1934 |
flexarray_vappend(dm_args, |
| 1935 |
"-drive", drive, |
| 1936 |
"-device", |
| 1937 |
GCSPRINTF("ide-cd,id=%s,drive=%s,bus=ide.%u,unit=%u", |
| 1938 |
drive_id, drive_id, |
| 1939 |
disk / 2, disk % 2), |
| 1940 |
NULL); |
| 1941 |
continue; |
| 1942 |
} else { |
| 1943 |
/* |
| 1944 |
* Explicit sd disks are passed through as is. |
| 1945 |
* |
| 1946 |
* For other disks we translate devices 0..3 into |
| 1947 |
* hd[a-d] and ignore the rest. |
| 1948 |
*/ |
| 1949 |
|
| 1950 |
if (libxl_defbool_val(disks[i].colo_enable)) { |
| 1951 |
if (libxl_defbool_val(disks[i].colo_restore_enable)) |
| 1952 |
colo_mode = LIBXL__COLO_SECONDARY; |
| 1953 |
else |
| 1954 |
colo_mode = LIBXL__COLO_PRIMARY; |
| 1955 |
} else { |
| 1956 |
colo_mode = LIBXL__COLO_NONE; |
| 1957 |
} |
| 1958 |
|
| 1959 |
if (strncmp(disks[i].vdev, "sd", 2) == 0) { |
| 1960 |
const char *drive_id; |
| 1961 |
if (colo_mode == LIBXL__COLO_SECONDARY) { |
| 1962 |
drive = libxl__sprintf |
| 1963 |
(gc, "if=none,driver=%s,file=%s,id=%s,readonly=%s", |
| 1964 |
format, target_path, disks[i].colo_export, |
| 1965 |
disks[i].readwrite ? "off" : "on"); |
| 1966 |
|
| 1967 |
flexarray_append(dm_args, "-drive"); |
| 1968 |
flexarray_append(dm_args, drive); |
| 1969 |
} |
| 1970 |
drive = qemu_disk_scsi_drive_string(gc, target_path, disk, |
| 1971 |
format, |
| 1972 |
&disks[i], |
| 1973 |
colo_mode, |
| 1974 |
&drive_id), |
| 1975 |
flexarray_vappend(dm_args, |
| 1976 |
"-drive", drive, |
| 1977 |
"-device", GCSPRINTF("scsi-hd,drive=%s,scsi-id=%d", |
| 1978 |
drive_id, disk), |
| 1979 |
NULL); |
| 1980 |
continue; |
| 1981 |
} else if (disk < 6 && b_info->u.hvm.hdtype == LIBXL_HDTYPE_AHCI) { |
| 1982 |
if (!disks[i].readwrite) { |
| 1983 |
LOGD(ERROR, guest_domid, |
| 1984 |
"qemu-xen doesn't support read-only AHCI disk drivers"); |
| 1985 |
return ERROR_INVAL; |
| 1986 |
} |
| 1987 |
flexarray_vappend(dm_args, "-drive", |
| 1988 |
GCSPRINTF("file=%s,if=none,id=ahcidisk-%d,format=%s,cache=writeback", |
| 1989 |
target_path, disk, format), |
| 1990 |
"-device", GCSPRINTF("ide-hd,bus=ahci0.%d,unit=0,drive=ahcidisk-%d", |
| 1991 |
disk, disk), NULL); |
| 1992 |
continue; |
| 1993 |
} else if (disk < 4) { |
| 1994 |
if (!disks[i].readwrite) { |
| 1995 |
LOGD(ERROR, guest_domid, |
| 1996 |
"qemu-xen doesn't support read-only IDE disk drivers"); |
| 1997 |
return ERROR_INVAL; |
| 1998 |
} |
| 1999 |
if (colo_mode == LIBXL__COLO_SECONDARY) { |
| 2000 |
drive = libxl__sprintf |
| 2001 |
(gc, "if=none,driver=%s,file=%s,id=%s", |
| 2002 |
format, target_path, disks[i].colo_export); |
| 2003 |
|
| 2004 |
flexarray_append(dm_args, "-drive"); |
| 2005 |
flexarray_append(dm_args, drive); |
| 2006 |
} |
| 2007 |
drive = qemu_disk_ide_drive_string(gc, target_path, disk, |
| 2008 |
format, |
| 2009 |
&disks[i], |
| 2010 |
colo_mode); |
| 2011 |
} else { |
| 2012 |
LOGD(WARN, guest_domid, "Only 4 emulated IDE disks are supported.\n" |
| 2013 |
"Disk %s will be available via PV drivers but not as an " |
| 2014 |
"emulated disk.", |
| 2015 |
disks[i].vdev); |
| 2016 |
continue; /* Do not emulate this disk */ |
| 2017 |
} |
| 2018 |
|
| 2019 |
if (!drive) |
| 2020 |
continue; |
| 2021 |
} |
| 2022 |
|
| 2023 |
flexarray_append(dm_args, "-drive"); |
| 2024 |
flexarray_append(dm_args, drive); |
| 2025 |
} |
| 2026 |
|
| 2027 |
switch (b_info->u.hvm.vendor_device) { |
| 2028 |
case LIBXL_VENDOR_DEVICE_XENSERVER: |
| 2029 |
flexarray_append(dm_args, "-device"); |
| 2030 |
flexarray_append(dm_args, "xen-pvdevice,device-id=0xc000"); |
| 2031 |
break; |
| 2032 |
default: |
| 2033 |
break; |
| 2034 |
} |
| 2035 |
|
| 2036 |
if (state->dm_runas) { |
| 2037 |
if (qemu_opts->have_runwith_user) { |
| 2038 |
flexarray_append_pair(dm_args, "-run-with", |
| 2039 |
GCSPRINTF("user=%s", state->dm_runas)); |
| 2040 |
} else { |
| 2041 |
flexarray_append(dm_args, "-runas"); |
| 2042 |
flexarray_append(dm_args, state->dm_runas); |
| 2043 |
} |
| 2044 |
} |
| 2045 |
} |
| 2046 |
flexarray_append(dm_args, NULL); |
| 2047 |
*args = (char **) flexarray_contents(dm_args); |
| 2048 |
flexarray_append(dm_envs, NULL); |
| 2049 |
if (envs) |
| 2050 |
*envs = (char **) flexarray_contents(dm_envs); |
| 2051 |
return 0; |
| 2052 |
} |
| 2053 |
|
| 2054 |
static int libxl__build_device_model_args(libxl__gc *gc, |
| 2055 |
const char *dm, int guest_domid, |
| 2056 |
const libxl_domain_config *guest_config, |
| 2057 |
char ***args, char ***envs, |
| 2058 |
const libxl__domain_build_state *state, |
| 2059 |
const libxl__qemu_available_opts *qemu_opts, |
| 2060 |
int *dm_state_fd) |
| 2061 |
/* dm_state_fd may be NULL iff caller knows we are using stubdom |
| 2062 |
* and therefore will be passing a filename rather than a fd. */ |
| 2063 |
{ |
| 2064 |
switch (guest_config->b_info.device_model_version) { |
| 2065 |
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: |
| 2066 |
return libxl__build_device_model_args_old(gc, dm, |
| 2067 |
guest_domid, guest_config, |
| 2068 |
args, envs, |
| 2069 |
state); |
| 2070 |
case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN: |
| 2071 |
if (!libxl_defbool_val(guest_config->b_info.device_model_stubdomain)) { |
| 2072 |
assert(dm_state_fd != NULL); |
| 2073 |
assert(*dm_state_fd < 0); |
| 2074 |
} |
| 2075 |
return libxl__build_device_model_args_new(gc, dm, |
| 2076 |
guest_domid, guest_config, |
| 2077 |
args, envs, |
| 2078 |
state, |
| 2079 |
qemu_opts, |
| 2080 |
dm_state_fd); |
| 2081 |
default: |
| 2082 |
LOGED(ERROR, guest_domid, "unknown device model version %d", |
| 2083 |
guest_config->b_info.device_model_version); |
| 2084 |
return ERROR_INVAL; |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
static void libxl__dm_vifs_from_hvm_guest_config(libxl__gc *gc, |
| 2089 |
libxl_domain_config * const guest_config, |
| 2090 |
libxl_domain_config *dm_config) |
| 2091 |
{ |
| 2092 |
int i, nr = guest_config->num_nics; |
| 2093 |
|
| 2094 |
GCNEW_ARRAY(dm_config->nics, nr); |
| 2095 |
|
| 2096 |
for (i=0; i<nr; i++) { |
| 2097 |
dm_config->nics[i] = guest_config->nics[i]; |
| 2098 |
dm_config->nics[i].nictype = LIBXL_NIC_TYPE_VIF; |
| 2099 |
if (dm_config->nics[i].ifname) |
| 2100 |
dm_config->nics[i].ifname = GCSPRINTF("%s" TAP_DEVICE_SUFFIX, |
| 2101 |
dm_config->nics[i].ifname); |
| 2102 |
} |
| 2103 |
|
| 2104 |
dm_config->num_nics = nr; |
| 2105 |
} |
| 2106 |
|
| 2107 |
static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc, |
| 2108 |
const libxl_domain_config *guest_config, |
| 2109 |
libxl_device_vfb *vfb, |
| 2110 |
libxl_device_vkb *vkb) |
| 2111 |
{ |
| 2112 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 2113 |
|
| 2114 |
if (b_info->type != LIBXL_DOMAIN_TYPE_HVM) |
| 2115 |
return ERROR_INVAL; |
| 2116 |
|
| 2117 |
libxl_device_vfb_init(vfb); |
| 2118 |
libxl_device_vkb_init(vkb); |
| 2119 |
|
| 2120 |
vfb->backend_domid = 0; |
| 2121 |
vfb->devid = 0; |
| 2122 |
vfb->vnc = b_info->u.hvm.vnc; |
| 2123 |
vfb->keymap = b_info->u.hvm.keymap; |
| 2124 |
vfb->sdl = b_info->u.hvm.sdl; |
| 2125 |
|
| 2126 |
vkb->backend_domid = 0; |
| 2127 |
vkb->devid = 0; |
| 2128 |
|
| 2129 |
return 0; |
| 2130 |
} |
| 2131 |
|
| 2132 |
static int libxl__write_stub_dmargs(libxl__gc *gc, |
| 2133 |
int dm_domid, int guest_domid, |
| 2134 |
char **args, bool is_linux_stubdom) |
| 2135 |
{ |
| 2136 |
struct xs_permissions roperm[2]; |
| 2137 |
xs_transaction_t t = XBT_NULL; |
| 2138 |
char *dmargs = NULL; |
| 2139 |
int rc; |
| 2140 |
|
| 2141 |
roperm[0].id = 0; |
| 2142 |
roperm[0].perms = XS_PERM_NONE; |
| 2143 |
roperm[1].id = dm_domid; |
| 2144 |
roperm[1].perms = XS_PERM_READ; |
| 2145 |
|
| 2146 |
if (!is_linux_stubdom) { |
| 2147 |
int dmargs_size = 0; |
| 2148 |
int i = 0; |
| 2149 |
|
| 2150 |
while (args[i] != NULL) { |
| 2151 |
dmargs_size = dmargs_size + strlen(args[i]) + 1; |
| 2152 |
i++; |
| 2153 |
} |
| 2154 |
|
| 2155 |
dmargs_size++; |
| 2156 |
dmargs = (char *) libxl__malloc(gc, dmargs_size); |
| 2157 |
|
| 2158 |
i = 1; |
| 2159 |
dmargs[0] = '\0'; |
| 2160 |
while (args[i] != NULL) { |
| 2161 |
if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) { |
| 2162 |
strcat(dmargs, " "); |
| 2163 |
strcat(dmargs, args[i]); |
| 2164 |
} |
| 2165 |
i++; |
| 2166 |
} |
| 2167 |
} |
| 2168 |
|
| 2169 |
for (;;) { |
| 2170 |
const char *vm_path; |
| 2171 |
char *path; |
| 2172 |
|
| 2173 |
rc = libxl__xs_transaction_start(gc, &t); |
| 2174 |
if (rc) goto out; |
| 2175 |
|
| 2176 |
rc = libxl__xs_read_mandatory(gc, t, |
| 2177 |
GCSPRINTF("/local/domain/%d/vm", |
| 2178 |
guest_domid), |
| 2179 |
&vm_path); |
| 2180 |
if (rc) goto out; |
| 2181 |
|
| 2182 |
if (is_linux_stubdom) { |
| 2183 |
int i; |
| 2184 |
|
| 2185 |
path = GCSPRINTF("%s/image/dm-argv", vm_path); |
| 2186 |
|
| 2187 |
rc = libxl__xs_mknod(gc, t, path, roperm, ARRAY_SIZE(roperm)); |
| 2188 |
if (rc) goto out; |
| 2189 |
|
| 2190 |
for (i=1; args[i] != NULL; i++) { |
| 2191 |
rc = libxl__xs_write_checked(gc, t, |
| 2192 |
GCSPRINTF("%s/%03d", path, i), |
| 2193 |
args[i]); |
| 2194 |
if (rc) goto out; |
| 2195 |
} |
| 2196 |
} else { |
| 2197 |
path = GCSPRINTF("%s/image/dmargs", vm_path); |
| 2198 |
|
| 2199 |
rc = libxl__xs_mknod(gc, t, path, roperm, ARRAY_SIZE(roperm)); |
| 2200 |
if (rc) goto out; |
| 2201 |
|
| 2202 |
rc = libxl__xs_write_checked(gc, t, path, dmargs); |
| 2203 |
if (rc) goto out; |
| 2204 |
|
| 2205 |
rc = libxl__xs_mknod(gc, t, GCSPRINTF("%s/rtc/timeoffset", vm_path), |
| 2206 |
roperm, ARRAY_SIZE(roperm)); |
| 2207 |
if (rc) goto out; |
| 2208 |
} |
| 2209 |
|
| 2210 |
rc = libxl__xs_transaction_commit(gc, &t); |
| 2211 |
if (!rc) break; |
| 2212 |
if (rc<0) goto out; |
| 2213 |
} |
| 2214 |
|
| 2215 |
return 0; |
| 2216 |
|
| 2217 |
out: |
| 2218 |
libxl__xs_transaction_abort(gc, &t); |
| 2219 |
|
| 2220 |
return rc; |
| 2221 |
} |
| 2222 |
|
| 2223 |
static int libxl__store_libxl_entry(libxl__gc *gc, uint32_t domid, |
| 2224 |
const char *name, const char *value) |
| 2225 |
{ |
| 2226 |
char *path = NULL; |
| 2227 |
|
| 2228 |
path = libxl__xs_libxl_path(gc, domid); |
| 2229 |
path = libxl__sprintf(gc, "%s/%s", path, name); |
| 2230 |
return libxl__xs_printf(gc, XBT_NULL, path, "%s", value); |
| 2231 |
} |
| 2232 |
|
| 2233 |
static void dmss_init(libxl__dm_spawn_state *dmss) |
| 2234 |
{ |
| 2235 |
libxl__ev_qmp_init(&dmss->qmp); |
| 2236 |
libxl__ev_time_init(&dmss->timeout); |
| 2237 |
} |
| 2238 |
|
| 2239 |
static void dmss_dispose(libxl__gc *gc, libxl__dm_spawn_state *dmss) |
| 2240 |
{ |
| 2241 |
libxl__ev_qmp_dispose(gc, &dmss->qmp); |
| 2242 |
libxl__ev_time_deregister(gc, &dmss->timeout); |
| 2243 |
} |
| 2244 |
|
| 2245 |
static void spawn_stubdom_pvqemu_cb(libxl__egc *egc, |
| 2246 |
libxl__dm_spawn_state *stubdom_dmss, |
| 2247 |
int rc); |
| 2248 |
|
| 2249 |
static void spawn_stub_launch_dm(libxl__egc *egc, |
| 2250 |
libxl__multidev *aodevs, int ret); |
| 2251 |
|
| 2252 |
static void stubdom_pvqemu_cb(libxl__egc *egc, |
| 2253 |
libxl__multidev *aodevs, |
| 2254 |
int rc); |
| 2255 |
static void stubdom_pvqemu_unpaused(libxl__egc *egc, |
| 2256 |
libxl__dm_resume_state *dmrs, |
| 2257 |
int rc); |
| 2258 |
|
| 2259 |
static void stubdom_xswait_cb(libxl__egc *egc, libxl__xswait_state *xswait, |
| 2260 |
int rc, const char *p); |
| 2261 |
|
| 2262 |
static void spawn_qmp_proxy(libxl__egc *egc, |
| 2263 |
libxl__stub_dm_spawn_state *sdss); |
| 2264 |
|
| 2265 |
static void qmp_proxy_confirm(libxl__egc *egc, libxl__spawn_state *spawn, |
| 2266 |
const char *xsdata); |
| 2267 |
|
| 2268 |
static void qmp_proxy_startup_failed(libxl__egc *egc, |
| 2269 |
libxl__spawn_state *spawn, |
| 2270 |
int rc); |
| 2271 |
|
| 2272 |
static void qmp_proxy_detached(libxl__egc *egc, |
| 2273 |
libxl__spawn_state *spawn); |
| 2274 |
|
| 2275 |
static void qmp_proxy_spawn_outcome(libxl__egc *egc, |
| 2276 |
libxl__stub_dm_spawn_state *sdss, |
| 2277 |
int rc); |
| 2278 |
|
| 2279 |
char *libxl__stub_dm_name(libxl__gc *gc, const char *guest_name) |
| 2280 |
{ |
| 2281 |
return GCSPRINTF("%s-dm", guest_name); |
| 2282 |
} |
| 2283 |
|
| 2284 |
void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss) |
| 2285 |
{ |
| 2286 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2287 |
libxl_ctx *ctx = libxl__gc_owner(gc); |
| 2288 |
int ret; |
| 2289 |
libxl_device_vfb *vfb; |
| 2290 |
libxl_device_vkb *vkb; |
| 2291 |
char **args; |
| 2292 |
struct xs_permissions perm[2]; |
| 2293 |
xs_transaction_t t; |
| 2294 |
|
| 2295 |
/* convenience aliases */ |
| 2296 |
libxl_domain_config *const dm_config = &sdss->dm_config; |
| 2297 |
libxl_domain_config *const guest_config = sdss->dm.guest_config; |
| 2298 |
const int guest_domid = sdss->dm.guest_domid; |
| 2299 |
libxl__domain_build_state *const d_state = sdss->dm.build_state; |
| 2300 |
libxl__domain_build_state *const stubdom_state = &sdss->dm_state; |
| 2301 |
|
| 2302 |
/* Initialise private part of sdss */ |
| 2303 |
libxl__domain_build_state_init(stubdom_state); |
| 2304 |
dmss_init(&sdss->dm); |
| 2305 |
dmss_init(&sdss->pvqemu); |
| 2306 |
libxl__xswait_init(&sdss->xswait); |
| 2307 |
|
| 2308 |
assert(libxl_defbool_val(guest_config->b_info.device_model_stubdomain)); |
| 2309 |
|
| 2310 |
sdss->pvqemu.guest_domid = INVALID_DOMID; |
| 2311 |
|
| 2312 |
libxl_domain_create_info_init(&dm_config->c_info); |
| 2313 |
dm_config->c_info.type = LIBXL_DOMAIN_TYPE_PV; |
| 2314 |
dm_config->c_info.name = libxl__stub_dm_name(gc, |
| 2315 |
libxl__domid_to_name(gc, guest_domid)); |
| 2316 |
/* When we are here to launch stubdom, ssidref is a valid value |
| 2317 |
* already, no need to parse it again. |
| 2318 |
*/ |
| 2319 |
dm_config->c_info.ssidref = guest_config->b_info.device_model_ssidref; |
| 2320 |
dm_config->c_info.ssid_label = NULL; |
| 2321 |
|
| 2322 |
libxl_uuid_generate(&dm_config->c_info.uuid); |
| 2323 |
|
| 2324 |
libxl_domain_build_info_init(&dm_config->b_info); |
| 2325 |
libxl_domain_build_info_init_type(&dm_config->b_info, LIBXL_DOMAIN_TYPE_PV); |
| 2326 |
|
| 2327 |
dm_config->b_info.shadow_memkb = 0; |
| 2328 |
dm_config->b_info.max_vcpus = 1; |
| 2329 |
dm_config->b_info.max_memkb = guest_config->b_info.stubdomain_memkb; |
| 2330 |
dm_config->b_info.max_memkb += guest_config->b_info.video_memkb; |
| 2331 |
dm_config->b_info.target_memkb = dm_config->b_info.max_memkb; |
| 2332 |
|
| 2333 |
dm_config->b_info.max_grant_frames = guest_config->b_info.max_grant_frames; |
| 2334 |
dm_config->b_info.max_maptrack_frames = guest_config->b_info.max_maptrack_frames; |
| 2335 |
dm_config->b_info.max_grant_version = guest_config->b_info.max_grant_version; |
| 2336 |
|
| 2337 |
dm_config->b_info.u.pv.features = ""; |
| 2338 |
|
| 2339 |
dm_config->b_info.device_model_version = |
| 2340 |
guest_config->b_info.device_model_version; |
| 2341 |
dm_config->b_info.device_model = |
| 2342 |
guest_config->b_info.device_model; |
| 2343 |
dm_config->b_info.extra = guest_config->b_info.extra; |
| 2344 |
dm_config->b_info.extra_pv = guest_config->b_info.extra_pv; |
| 2345 |
dm_config->b_info.extra_hvm = guest_config->b_info.extra_hvm; |
| 2346 |
|
| 2347 |
dm_config->disks = guest_config->disks; |
| 2348 |
dm_config->num_disks = guest_config->num_disks; |
| 2349 |
|
| 2350 |
libxl__dm_vifs_from_hvm_guest_config(gc, guest_config, dm_config); |
| 2351 |
|
| 2352 |
dm_config->c_info.run_hotplug_scripts = |
| 2353 |
guest_config->c_info.run_hotplug_scripts; |
| 2354 |
|
| 2355 |
ret = libxl__domain_config_setdefault(gc, dm_config, guest_domid); |
| 2356 |
if (ret) goto out; |
| 2357 |
|
| 2358 |
if (libxl_defbool_val(guest_config->b_info.u.hvm.vnc.enable) |
| 2359 |
|| libxl_defbool_val(guest_config->b_info.u.hvm.spice.enable) |
| 2360 |
|| libxl_defbool_val(guest_config->b_info.u.hvm.sdl.enable)) { |
| 2361 |
GCNEW(vfb); |
| 2362 |
GCNEW(vkb); |
| 2363 |
libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb); |
| 2364 |
dm_config->vfbs = vfb; |
| 2365 |
dm_config->num_vfbs = 1; |
| 2366 |
dm_config->vkbs = vkb; |
| 2367 |
dm_config->num_vkbs = 1; |
| 2368 |
} |
| 2369 |
|
| 2370 |
if (guest_config->b_info.stubdomain_kernel && |
| 2371 |
access(guest_config->b_info.stubdomain_kernel, R_OK) != 0) { |
| 2372 |
LOGED(ERROR, guest_domid, "could not access stubdomain kernel %s", |
| 2373 |
guest_config->b_info.stubdomain_kernel); |
| 2374 |
ret = ERROR_INVAL; |
| 2375 |
goto out; |
| 2376 |
} |
| 2377 |
|
| 2378 |
if (guest_config->b_info.stubdomain_ramdisk && |
| 2379 |
access(guest_config->b_info.stubdomain_ramdisk, R_OK) != 0) { |
| 2380 |
LOGED(ERROR, guest_domid, "could not access stubdomain ramdisk %s", |
| 2381 |
guest_config->b_info.stubdomain_ramdisk); |
| 2382 |
ret = ERROR_INVAL; |
| 2383 |
goto out; |
| 2384 |
} |
| 2385 |
|
| 2386 |
stubdom_state->pv_kernel.path = guest_config->b_info.stubdomain_kernel; |
| 2387 |
stubdom_state->pv_cmdline = guest_config->b_info.stubdomain_cmdline; |
| 2388 |
stubdom_state->pv_ramdisk.path = guest_config->b_info.stubdomain_ramdisk; |
| 2389 |
|
| 2390 |
/* fixme: this function can leak the stubdom if it fails */ |
| 2391 |
ret = libxl__domain_make(gc, dm_config, stubdom_state, |
| 2392 |
&sdss->pvqemu.guest_domid, false); |
| 2393 |
if (ret) |
| 2394 |
goto out; |
| 2395 |
uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2396 |
ret = libxl__domain_build(gc, dm_config, dm_domid, stubdom_state); |
| 2397 |
if (ret) |
| 2398 |
goto out; |
| 2399 |
|
| 2400 |
ret = libxl__build_device_model_args(gc, "stubdom-dm", guest_domid, |
| 2401 |
guest_config, &args, NULL, |
| 2402 |
d_state, |
| 2403 |
&sdss->dm.qemu_opts, |
| 2404 |
NULL); |
| 2405 |
if (ret) { |
| 2406 |
ret = ERROR_FAIL; |
| 2407 |
goto out; |
| 2408 |
} |
| 2409 |
|
| 2410 |
libxl__store_libxl_entry(gc, guest_domid, "dm-version", |
| 2411 |
libxl_device_model_version_to_string(dm_config->b_info.device_model_version)); |
| 2412 |
|
| 2413 |
libxl__write_stub_dmargs(gc, dm_domid, guest_domid, args, |
| 2414 |
libxl__stubdomain_is_linux(&guest_config->b_info)); |
| 2415 |
libxl__xs_printf(gc, XBT_NULL, |
| 2416 |
GCSPRINTF("%s/image/device-model-domid", |
| 2417 |
libxl__xs_get_dompath(gc, guest_domid)), |
| 2418 |
"%d", dm_domid); |
| 2419 |
libxl__xs_printf(gc, XBT_NULL, |
| 2420 |
GCSPRINTF("%s/target", |
| 2421 |
libxl__xs_get_dompath(gc, dm_domid)), |
| 2422 |
"%d", guest_domid); |
| 2423 |
if (guest_config->b_info.device_model_version == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) { |
| 2424 |
/* qemu-xen is used as a dm in the stubdomain, so we set the bios |
| 2425 |
* accroding to this */ |
| 2426 |
libxl__xs_printf(gc, XBT_NULL, |
| 2427 |
libxl__sprintf(gc, "%s/hvmloader/bios", |
| 2428 |
libxl__xs_get_dompath(gc, guest_domid)), |
| 2429 |
"%s", |
| 2430 |
libxl_bios_type_to_string(guest_config->b_info.u.hvm.bios)); |
| 2431 |
} |
| 2432 |
/* Disable relocating memory to make the MMIO hole larger |
| 2433 |
* unless we're running qemu-traditional and vNUMA is not |
| 2434 |
* configured. */ |
| 2435 |
libxl__xs_printf(gc, XBT_NULL, |
| 2436 |
libxl__sprintf(gc, "%s/hvmloader/allow-memory-relocate", |
| 2437 |
libxl__xs_get_dompath(gc, guest_domid)), |
| 2438 |
"%d", |
| 2439 |
guest_config->b_info.device_model_version |
| 2440 |
== LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL && |
| 2441 |
!libxl__vnuma_configured(&guest_config->b_info)); |
| 2442 |
ret = xc_domain_set_target(ctx->xch, dm_domid, guest_domid); |
| 2443 |
if (ret<0) { |
| 2444 |
LOGED(ERROR, guest_domid, "setting target domain %d -> %d", |
| 2445 |
dm_domid, guest_domid); |
| 2446 |
ret = ERROR_FAIL; |
| 2447 |
goto out; |
| 2448 |
} |
| 2449 |
xs_set_target(ctx->xsh, dm_domid, guest_domid); |
| 2450 |
|
| 2451 |
perm[0].id = dm_domid; |
| 2452 |
perm[0].perms = XS_PERM_NONE; |
| 2453 |
perm[1].id = guest_domid; |
| 2454 |
perm[1].perms = XS_PERM_READ; |
| 2455 |
retry_transaction: |
| 2456 |
t = xs_transaction_start(ctx->xsh); |
| 2457 |
xs_mkdir(ctx->xsh, t, DEVICE_MODEL_XS_PATH(gc, dm_domid, guest_domid, "")); |
| 2458 |
xs_set_permissions(ctx->xsh, t, |
| 2459 |
DEVICE_MODEL_XS_PATH(gc, dm_domid, guest_domid, ""), |
| 2460 |
perm, ARRAY_SIZE(perm)); |
| 2461 |
if (!xs_transaction_end(ctx->xsh, t, 0)) |
| 2462 |
if (errno == EAGAIN) |
| 2463 |
goto retry_transaction; |
| 2464 |
|
| 2465 |
libxl__multidev_begin(ao, &sdss->multidev); |
| 2466 |
sdss->multidev.callback = spawn_stub_launch_dm; |
| 2467 |
libxl__add_disks(egc, ao, dm_domid, dm_config, &sdss->multidev); |
| 2468 |
libxl__multidev_prepared(egc, &sdss->multidev, 0); |
| 2469 |
|
| 2470 |
return; |
| 2471 |
|
| 2472 |
out: |
| 2473 |
assert(ret); |
| 2474 |
spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret); |
| 2475 |
} |
| 2476 |
|
| 2477 |
static void spawn_stub_launch_dm(libxl__egc *egc, |
| 2478 |
libxl__multidev *multidev, int ret) |
| 2479 |
{ |
| 2480 |
libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(multidev, *sdss, multidev); |
| 2481 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2482 |
libxl_ctx *ctx = libxl__gc_owner(gc); |
| 2483 |
int i, num_console = STUBDOM_SPECIAL_CONSOLES; |
| 2484 |
libxl__device_console *console; |
| 2485 |
|
| 2486 |
/* convenience aliases */ |
| 2487 |
libxl_domain_config *const dm_config = &sdss->dm_config; |
| 2488 |
libxl_domain_config *const guest_config = sdss->dm.guest_config; |
| 2489 |
const int guest_domid = sdss->dm.guest_domid; |
| 2490 |
libxl__domain_build_state *const d_state = sdss->dm.build_state; |
| 2491 |
libxl__domain_build_state *const stubdom_state = &sdss->dm_state; |
| 2492 |
uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2493 |
int need_qemu; |
| 2494 |
|
| 2495 |
if (ret) { |
| 2496 |
LOGD(ERROR, guest_domid, "error connecting disk devices"); |
| 2497 |
goto out; |
| 2498 |
} |
| 2499 |
|
| 2500 |
for (i = 0; i < dm_config->num_nics; i++) { |
| 2501 |
/* We have to init the nic here, because we still haven't |
| 2502 |
* called libxl_device_nic_add at this point, but qemu needs |
| 2503 |
* the nic information to be complete. |
| 2504 |
*/ |
| 2505 |
ret = libxl__nic_devtype.set_default(gc, dm_domid, &dm_config->nics[i], |
| 2506 |
false); |
| 2507 |
if (ret) |
| 2508 |
goto out; |
| 2509 |
} |
| 2510 |
if (dm_config->num_vfbs) { |
| 2511 |
ret = libxl__device_add(gc, dm_domid, &libxl__vfb_devtype, |
| 2512 |
&dm_config->vfbs[0]); |
| 2513 |
if (ret) goto out; |
| 2514 |
} |
| 2515 |
if (dm_config->num_vkbs) { |
| 2516 |
ret = libxl__device_add(gc, dm_domid, &libxl__vkb_devtype, |
| 2517 |
&dm_config->vkbs[0]); |
| 2518 |
if (ret) goto out; |
| 2519 |
} |
| 2520 |
|
| 2521 |
if (guest_config->b_info.u.hvm.serial) { |
| 2522 |
num_console++; |
| 2523 |
} else if (guest_config->b_info.u.hvm.serial_list) { |
| 2524 |
char **serial = guest_config->b_info.u.hvm.serial_list; |
| 2525 |
while (*(serial++)) |
| 2526 |
num_console++; |
| 2527 |
} |
| 2528 |
|
| 2529 |
console = libxl__calloc(gc, num_console, sizeof(libxl__device_console)); |
| 2530 |
|
| 2531 |
for (i = 0; i < num_console; i++) { |
| 2532 |
console[i].devid = i; |
| 2533 |
console[i].consback = LIBXL__CONSOLE_BACKEND_IOEMU; |
| 2534 |
/* STUBDOM_CONSOLE_LOGGING (console 0) is for minios logging |
| 2535 |
* STUBDOM_CONSOLE_SAVE (console 1) is for writing the save file |
| 2536 |
* STUBDOM_CONSOLE_RESTORE (console 2) is for reading the save file |
| 2537 |
*/ |
| 2538 |
switch (i) { |
| 2539 |
char *filename; |
| 2540 |
char *name; |
| 2541 |
case STUBDOM_CONSOLE_LOGGING: |
| 2542 |
name = GCSPRINTF("qemu-dm-%s", |
| 2543 |
libxl_domid_to_name(ctx, guest_domid)); |
| 2544 |
ret = libxl_create_logfile(ctx, name, &filename); |
| 2545 |
if (ret) goto out; |
| 2546 |
console[i].output = GCSPRINTF("file:%s", filename); |
| 2547 |
free(filename); |
| 2548 |
/* will be changed back to LIBXL__CONSOLE_BACKEND_IOEMU if qemu |
| 2549 |
* will be in use */ |
| 2550 |
console[i].consback = LIBXL__CONSOLE_BACKEND_XENCONSOLED; |
| 2551 |
break; |
| 2552 |
case STUBDOM_CONSOLE_SAVE: |
| 2553 |
console[i].output = GCSPRINTF("file:%s", |
| 2554 |
libxl__device_model_savefile(gc, guest_domid)); |
| 2555 |
break; |
| 2556 |
case STUBDOM_CONSOLE_RESTORE: |
| 2557 |
if (d_state->saved_state) |
| 2558 |
console[i].output = |
| 2559 |
GCSPRINTF("pipe:%s", d_state->saved_state); |
| 2560 |
break; |
| 2561 |
case STUBDOM_CONSOLE_SERIAL: |
| 2562 |
if (guest_config->b_info.u.hvm.serial) { |
| 2563 |
console[i].output = guest_config->b_info.u.hvm.serial; |
| 2564 |
break; |
| 2565 |
} |
| 2566 |
/* fall-through */ |
| 2567 |
default: |
| 2568 |
/* Serial_list is set, as otherwise num_consoles would be |
| 2569 |
* smaller and consoles 0-2 are handled above. */ |
| 2570 |
assert(guest_config->b_info.u.hvm.serial_list); |
| 2571 |
console[i].output = guest_config->b_info.u.hvm.serial_list[ |
| 2572 |
i-STUBDOM_CONSOLE_SERIAL]; |
| 2573 |
break; |
| 2574 |
} |
| 2575 |
} |
| 2576 |
|
| 2577 |
/* |
| 2578 |
* Until xenconsoled learns how to handle multiple consoles, require qemu |
| 2579 |
* in dom0 to serve consoles for a stubdomain - it require at least 3 of them. |
| 2580 |
*/ |
| 2581 |
need_qemu = 1 || libxl__need_xenpv_qemu(gc, &sdss->dm_config); |
| 2582 |
|
| 2583 |
for (i = 0; i < num_console; i++) { |
| 2584 |
libxl__device device; |
| 2585 |
if (need_qemu) |
| 2586 |
console[i].consback = LIBXL__CONSOLE_BACKEND_IOEMU; |
| 2587 |
ret = libxl__device_console_add(gc, dm_domid, &console[i], |
| 2588 |
i == STUBDOM_CONSOLE_LOGGING ? stubdom_state : NULL, |
| 2589 |
&device); |
| 2590 |
if (ret) |
| 2591 |
goto out; |
| 2592 |
} |
| 2593 |
|
| 2594 |
if (libxl__stubdomain_is_linux(&guest_config->b_info)) { |
| 2595 |
spawn_qmp_proxy(egc, sdss); |
| 2596 |
} else { |
| 2597 |
qmp_proxy_spawn_outcome(egc, sdss, 0); |
| 2598 |
} |
| 2599 |
|
| 2600 |
return; |
| 2601 |
|
| 2602 |
out: |
| 2603 |
assert(ret); |
| 2604 |
qmp_proxy_spawn_outcome(egc, sdss, ret); |
| 2605 |
} |
| 2606 |
|
| 2607 |
static void spawn_qmp_proxy(libxl__egc *egc, |
| 2608 |
libxl__stub_dm_spawn_state *sdss) |
| 2609 |
{ |
| 2610 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2611 |
const uint32_t guest_domid = sdss->dm.guest_domid; |
| 2612 |
const uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2613 |
const char *dom_path = libxl__xs_get_dompath(gc, dm_domid); |
| 2614 |
char **args; |
| 2615 |
int nr = 0; |
| 2616 |
int rc, logfile_w, null; |
| 2617 |
|
| 2618 |
if (access(STUBDOM_QMP_PROXY_PATH, X_OK) < 0) { |
| 2619 |
LOGED(ERROR, guest_domid, "qmp proxy %s is not executable", STUBDOM_QMP_PROXY_PATH); |
| 2620 |
rc = ERROR_FAIL; |
| 2621 |
goto out; |
| 2622 |
} |
| 2623 |
|
| 2624 |
sdss->qmp_proxy_spawn.ao = ao; |
| 2625 |
sdss->qmp_proxy_spawn.what = GCSPRINTF("domain %d device model qmp proxy", guest_domid); |
| 2626 |
sdss->qmp_proxy_spawn.pidpath = GCSPRINTF("%s/image/qmp-proxy-pid", dom_path); |
| 2627 |
sdss->qmp_proxy_spawn.xspath = DEVICE_MODEL_XS_PATH(gc, LIBXL_TOOLSTACK_DOMID, |
| 2628 |
dm_domid, "/qmp-proxy-state"); |
| 2629 |
sdss->qmp_proxy_spawn.timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000; |
| 2630 |
sdss->qmp_proxy_spawn.midproc_cb = libxl__spawn_record_pid; |
| 2631 |
sdss->qmp_proxy_spawn.confirm_cb = qmp_proxy_confirm; |
| 2632 |
sdss->qmp_proxy_spawn.failure_cb = qmp_proxy_startup_failed; |
| 2633 |
sdss->qmp_proxy_spawn.detached_cb = qmp_proxy_detached; |
| 2634 |
|
| 2635 |
const int arraysize = 6; |
| 2636 |
GCNEW_ARRAY(args, arraysize); |
| 2637 |
args[nr++] = STUBDOM_QMP_PROXY_PATH; |
| 2638 |
args[nr++] = GCSPRINTF("--state-path=%s", sdss->qmp_proxy_spawn.xspath); |
| 2639 |
args[nr++] = GCSPRINTF("%u", dm_domid); |
| 2640 |
args[nr++] = GCSPRINTF("%s/device-model/%u/qmp-vchan", dom_path, guest_domid); |
| 2641 |
args[nr++] = (char*)libxl__qemu_qmp_path(gc, guest_domid); |
| 2642 |
args[nr++] = NULL; |
| 2643 |
assert(nr == arraysize); |
| 2644 |
|
| 2645 |
logfile_w = libxl__create_qemu_logfile(gc, GCSPRINTF("qmp-proxy-%s", |
| 2646 |
sdss->dm_config.c_info.name)); |
| 2647 |
if (logfile_w < 0) { |
| 2648 |
rc = logfile_w; |
| 2649 |
goto out; |
| 2650 |
} |
| 2651 |
null = open("/dev/null", O_RDWR); |
| 2652 |
if (null < 0) { |
| 2653 |
LOGED(ERROR, guest_domid, "unable to open /dev/null"); |
| 2654 |
rc = ERROR_FAIL; |
| 2655 |
goto out_close; |
| 2656 |
} |
| 2657 |
|
| 2658 |
rc = libxl__spawn_spawn(egc, &sdss->qmp_proxy_spawn); |
| 2659 |
if (rc < 0) |
| 2660 |
goto out_close; |
| 2661 |
if (!rc) { /* inner child */ |
| 2662 |
setsid(); |
| 2663 |
libxl__exec(gc, null, null, logfile_w, STUBDOM_QMP_PROXY_PATH, args, NULL); |
| 2664 |
/* unreachable */ |
| 2665 |
} |
| 2666 |
|
| 2667 |
rc = 0; |
| 2668 |
|
| 2669 |
out_close: |
| 2670 |
if (logfile_w >= 0) |
| 2671 |
close(logfile_w); |
| 2672 |
if (null >= 0) |
| 2673 |
close(null); |
| 2674 |
out: |
| 2675 |
if (rc) |
| 2676 |
qmp_proxy_spawn_outcome(egc, sdss, rc); |
| 2677 |
} |
| 2678 |
|
| 2679 |
static void qmp_proxy_confirm(libxl__egc *egc, libxl__spawn_state *spawn, |
| 2680 |
const char *xsdata) |
| 2681 |
{ |
| 2682 |
STATE_AO_GC(spawn->ao); |
| 2683 |
|
| 2684 |
if (!xsdata) |
| 2685 |
return; |
| 2686 |
|
| 2687 |
if (strcmp(xsdata, "running")) |
| 2688 |
return; |
| 2689 |
|
| 2690 |
libxl__spawn_initiate_detach(gc, spawn); |
| 2691 |
} |
| 2692 |
|
| 2693 |
static void qmp_proxy_startup_failed(libxl__egc *egc, |
| 2694 |
libxl__spawn_state *spawn, |
| 2695 |
int rc) |
| 2696 |
{ |
| 2697 |
libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(spawn, *sdss, qmp_proxy_spawn); |
| 2698 |
qmp_proxy_spawn_outcome(egc, sdss, rc); |
| 2699 |
} |
| 2700 |
|
| 2701 |
static void qmp_proxy_detached(libxl__egc *egc, |
| 2702 |
libxl__spawn_state *spawn) |
| 2703 |
{ |
| 2704 |
libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(spawn, *sdss, qmp_proxy_spawn); |
| 2705 |
qmp_proxy_spawn_outcome(egc, sdss, 0); |
| 2706 |
} |
| 2707 |
|
| 2708 |
static void qmp_proxy_spawn_outcome(libxl__egc *egc, |
| 2709 |
libxl__stub_dm_spawn_state *sdss, |
| 2710 |
int rc) |
| 2711 |
{ |
| 2712 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2713 |
/* |
| 2714 |
* Until xenconsoled learns how to handle multiple consoles, require qemu |
| 2715 |
* in dom0 to serve consoles for a stubdomain - it require at least 3 of them. |
| 2716 |
*/ |
| 2717 |
int need_pvqemu = 1 || libxl__need_xenpv_qemu(gc, &sdss->dm_config); |
| 2718 |
|
| 2719 |
if (rc) goto out; |
| 2720 |
|
| 2721 |
if (need_pvqemu < 0) { |
| 2722 |
rc = need_pvqemu; |
| 2723 |
goto out; |
| 2724 |
} |
| 2725 |
|
| 2726 |
sdss->pvqemu.spawn.ao = ao; |
| 2727 |
sdss->pvqemu.guest_config = &sdss->dm_config; |
| 2728 |
sdss->pvqemu.build_state = &sdss->dm_state; |
| 2729 |
sdss->pvqemu.callback = spawn_stubdom_pvqemu_cb; |
| 2730 |
if (need_pvqemu) { |
| 2731 |
libxl__spawn_local_dm(egc, &sdss->pvqemu); |
| 2732 |
} else { |
| 2733 |
/* If dom0 qemu not needed, do not launch it */ |
| 2734 |
spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, 0); |
| 2735 |
} |
| 2736 |
|
| 2737 |
return; |
| 2738 |
|
| 2739 |
out: |
| 2740 |
assert(rc); |
| 2741 |
spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, rc); |
| 2742 |
} |
| 2743 |
|
| 2744 |
static void spawn_stubdom_pvqemu_cb(libxl__egc *egc, |
| 2745 |
libxl__dm_spawn_state *stubdom_dmss, |
| 2746 |
int rc) |
| 2747 |
{ |
| 2748 |
libxl__stub_dm_spawn_state *sdss = |
| 2749 |
CONTAINER_OF(stubdom_dmss, *sdss, pvqemu); |
| 2750 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2751 |
uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2752 |
libxl_domain_config *d_config = stubdom_dmss->guest_config; |
| 2753 |
|
| 2754 |
if (rc) goto out; |
| 2755 |
|
| 2756 |
if (d_config->num_nics > 0) { |
| 2757 |
libxl__multidev_begin(ao, &sdss->multidev); |
| 2758 |
sdss->multidev.callback = stubdom_pvqemu_cb; |
| 2759 |
libxl__add_nics(egc, ao, dm_domid, d_config, &sdss->multidev); |
| 2760 |
libxl__multidev_prepared(egc, &sdss->multidev, 0); |
| 2761 |
return; |
| 2762 |
} |
| 2763 |
|
| 2764 |
out: |
| 2765 |
stubdom_pvqemu_cb(egc, &sdss->multidev, rc); |
| 2766 |
} |
| 2767 |
|
| 2768 |
static void stubdom_pvqemu_cb(libxl__egc *egc, |
| 2769 |
libxl__multidev *multidev, |
| 2770 |
int rc) |
| 2771 |
{ |
| 2772 |
libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(multidev, *sdss, multidev); |
| 2773 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2774 |
uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2775 |
|
| 2776 |
if (rc) { |
| 2777 |
LOGED(ERROR, sdss->dm.guest_domid, |
| 2778 |
"error connecting nics devices"); |
| 2779 |
goto out; |
| 2780 |
} |
| 2781 |
|
| 2782 |
sdss->pvqemu.dmrs.ao = ao; |
| 2783 |
sdss->pvqemu.dmrs.domid = dm_domid; |
| 2784 |
sdss->pvqemu.dmrs.callback = stubdom_pvqemu_unpaused; |
| 2785 |
libxl__domain_unpause(egc, &sdss->pvqemu.dmrs); /* must be last */ |
| 2786 |
return; |
| 2787 |
out: |
| 2788 |
stubdom_pvqemu_unpaused(egc, &sdss->pvqemu.dmrs, rc); |
| 2789 |
} |
| 2790 |
|
| 2791 |
static void stubdom_pvqemu_unpaused(libxl__egc *egc, |
| 2792 |
libxl__dm_resume_state *dmrs, |
| 2793 |
int rc) |
| 2794 |
{ |
| 2795 |
libxl__stub_dm_spawn_state *sdss = |
| 2796 |
CONTAINER_OF(dmrs, *sdss, pvqemu.dmrs); |
| 2797 |
STATE_AO_GC(sdss->dm.spawn.ao); |
| 2798 |
uint32_t dm_domid = sdss->pvqemu.guest_domid; |
| 2799 |
|
| 2800 |
if (rc) goto out; |
| 2801 |
|
| 2802 |
sdss->xswait.ao = ao; |
| 2803 |
sdss->xswait.what = GCSPRINTF("Stubdom %u for %u startup", |
| 2804 |
dm_domid, sdss->dm.guest_domid); |
| 2805 |
sdss->xswait.path = DEVICE_MODEL_XS_PATH(gc, dm_domid, sdss->dm.guest_domid, |
| 2806 |
"/state"); |
| 2807 |
sdss->xswait.timeout_ms = LIBXL_STUBDOM_START_TIMEOUT * 1000; |
| 2808 |
sdss->xswait.callback = stubdom_xswait_cb; |
| 2809 |
rc = libxl__xswait_start(gc, &sdss->xswait); |
| 2810 |
if (rc) goto out; |
| 2811 |
|
| 2812 |
return; |
| 2813 |
|
| 2814 |
out: |
| 2815 |
stubdom_xswait_cb(egc, &sdss->xswait, rc, NULL); |
| 2816 |
} |
| 2817 |
|
| 2818 |
static void stubdom_xswait_cb(libxl__egc *egc, libxl__xswait_state *xswait, |
| 2819 |
int rc, const char *p) |
| 2820 |
{ |
| 2821 |
EGC_GC; |
| 2822 |
libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(xswait, *sdss, xswait); |
| 2823 |
|
| 2824 |
if (rc) { |
| 2825 |
if (rc == ERROR_TIMEDOUT) |
| 2826 |
LOGD(ERROR, sdss->dm.guest_domid, |
| 2827 |
"%s: startup timed out", xswait->what); |
| 2828 |
goto out; |
| 2829 |
} |
| 2830 |
|
| 2831 |
if (!p) return; |
| 2832 |
|
| 2833 |
if (strcmp(p, "running")) |
| 2834 |
return; |
| 2835 |
out: |
| 2836 |
libxl__domain_build_state_dispose(&sdss->dm_state); |
| 2837 |
libxl__xswait_stop(gc, xswait); |
| 2838 |
dmss_dispose(gc, &sdss->dm); |
| 2839 |
dmss_dispose(gc, &sdss->pvqemu); |
| 2840 |
sdss->callback(egc, &sdss->dm, rc); |
| 2841 |
} |
| 2842 |
|
| 2843 |
/* callbacks passed to libxl__spawn_spawn */ |
| 2844 |
static void device_model_confirm(libxl__egc *egc, libxl__spawn_state *spawn, |
| 2845 |
const char *xsdata); |
| 2846 |
static void device_model_startup_failed(libxl__egc *egc, |
| 2847 |
libxl__spawn_state *spawn, |
| 2848 |
int rc); |
| 2849 |
static void device_model_detached(libxl__egc *egc, |
| 2850 |
libxl__spawn_state *spawn); |
| 2851 |
static void device_model_qmp_cb(libxl__egc *egc, libxl__ev_qmp *ev, |
| 2852 |
const libxl__json_object *response, |
| 2853 |
int rc); |
| 2854 |
|
| 2855 |
/* our "next step" function, called from those callbacks and elsewhere */ |
| 2856 |
static void device_model_spawn_outcome(libxl__egc *egc, |
| 2857 |
libxl__dm_spawn_state *dmss, |
| 2858 |
int rc); |
| 2859 |
static void device_model_probe_startup_failed(libxl__egc *egc, |
| 2860 |
libxl__spawn_state *spawn, int rc); |
| 2861 |
static void device_model_probe_confirm(libxl__egc *egc, |
| 2862 |
libxl__spawn_state *spawn, const char *xsdata); |
| 2863 |
static void device_model_probe_detached(libxl__egc *egc, |
| 2864 |
libxl__spawn_state *spawn); |
| 2865 |
static void device_model_probe_cmdline(libxl__egc *egc, |
| 2866 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc); |
| 2867 |
static void device_model_probe_quit(libxl__egc *egc, |
| 2868 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc); |
| 2869 |
static void device_model_probe_spawn_outcome(libxl__egc *egc, |
| 2870 |
libxl__dm_spawn_state *dmss, int rc); |
| 2871 |
static void device_model_launch(libxl__egc *egc, |
| 2872 |
libxl__dm_spawn_state *dmss, int rc); |
| 2873 |
static void device_model_postconfig_chardev(libxl__egc *egc, |
| 2874 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc); |
| 2875 |
static void device_model_postconfig_vnc(libxl__egc *egc, |
| 2876 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc); |
| 2877 |
static void device_model_postconfig_vnc_passwd(libxl__egc *egc, |
| 2878 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc); |
| 2879 |
static void devise_model_postconfig_timeout(libxl__egc *egc, |
| 2880 |
libxl__ev_time *ev, const struct timeval *requested_abs, int rc); |
| 2881 |
static void device_model_postconfig_done(libxl__egc *egc, |
| 2882 |
libxl__dm_spawn_state *dmss, int rc); |
| 2883 |
|
| 2884 |
void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss) |
| 2885 |
{ |
| 2886 |
/* convenience aliases */ |
| 2887 |
const int domid = dmss->guest_domid; |
| 2888 |
libxl__spawn_state *const spawn = &dmss->spawn; |
| 2889 |
|
| 2890 |
STATE_AO_GC(dmss->spawn.ao); |
| 2891 |
|
| 2892 |
libxl_domain_config *guest_config = dmss->guest_config; |
| 2893 |
const libxl_domain_create_info *c_info = &guest_config->c_info; |
| 2894 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 2895 |
int rc; |
| 2896 |
const char *dm; |
| 2897 |
int logfile_w = -1, null = -1; |
| 2898 |
int qmp_probe_fd = -1; |
| 2899 |
bool probe_spawned = false; |
| 2900 |
|
| 2901 |
dmss_init(dmss); |
| 2902 |
|
| 2903 |
if (libxl_defbool_val(b_info->device_model_stubdomain)) { |
| 2904 |
abort(); |
| 2905 |
} |
| 2906 |
|
| 2907 |
dm = libxl__domain_device_model(gc, b_info); |
| 2908 |
if (!dm) { |
| 2909 |
rc = ERROR_FAIL; |
| 2910 |
goto out; |
| 2911 |
} |
| 2912 |
dmss->dm = dm; |
| 2913 |
if (access(dm, X_OK) < 0) { |
| 2914 |
LOGED(ERROR, domid, "device model %s is not executable", dm); |
| 2915 |
rc = ERROR_FAIL; |
| 2916 |
goto out; |
| 2917 |
} |
| 2918 |
|
| 2919 |
rc = libxl__domain_get_device_model_uid(gc, dmss); |
| 2920 |
if (rc) |
| 2921 |
goto out; |
| 2922 |
|
| 2923 |
/* probe QEMU's available command line options */ |
| 2924 |
if (b_info->device_model_version |
| 2925 |
== LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) { |
| 2926 |
|
| 2927 |
logfile_w = libxl__create_qemu_logfile( |
| 2928 |
gc, GCSPRINTF("qemu-probe-%s", c_info->name)); |
| 2929 |
if (logfile_w < 0) { |
| 2930 |
rc = logfile_w; |
| 2931 |
goto out; |
| 2932 |
} |
| 2933 |
null = open("/dev/null", O_RDONLY); |
| 2934 |
if (null < 0) { |
| 2935 |
LOGED(ERROR, domid, "unable to open /dev/null"); |
| 2936 |
rc = ERROR_FAIL; |
| 2937 |
goto out; |
| 2938 |
} |
| 2939 |
|
| 2940 |
rc = libxl__pre_open_qmp_socket(gc, domid, &qmp_probe_fd); |
| 2941 |
if (rc) goto out; |
| 2942 |
|
| 2943 |
flexarray_t *dm_args = flexarray_make(gc, 16, 1); |
| 2944 |
flexarray_vappend(dm_args, dm, |
| 2945 |
"-S", |
| 2946 |
"-no-user-config", |
| 2947 |
"-nodefaults", |
| 2948 |
"-nographic", |
| 2949 |
"-machine", "none,accel=xen", |
| 2950 |
NULL); |
| 2951 |
flexarray_vappend(dm_args, |
| 2952 |
"-chardev", |
| 2953 |
GCSPRINTF("socket,id=libxl-cmd,fd=%d,server=on,wait=off", |
| 2954 |
qmp_probe_fd), |
| 2955 |
"-mon", "chardev=libxl-cmd,mode=control", |
| 2956 |
NULL); |
| 2957 |
flexarray_append(dm_args, NULL); |
| 2958 |
char **exec_args = (char **) flexarray_contents(dm_args); |
| 2959 |
|
| 2960 |
const char *dom_path = libxl__xs_get_dompath(gc, domid); |
| 2961 |
|
| 2962 |
spawn->what = GCSPRINTF("domain %d qemu command line probe", domid); |
| 2963 |
spawn->xspath = "/dev/null"; /* No path to watch */ |
| 2964 |
spawn->timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000; |
| 2965 |
spawn->pidpath = GCSPRINTF("%s/image/device-model-pid", dom_path); |
| 2966 |
spawn->midproc_cb = libxl__spawn_record_pid; |
| 2967 |
spawn->confirm_cb = device_model_probe_confirm; |
| 2968 |
spawn->failure_cb = device_model_probe_startup_failed; |
| 2969 |
spawn->detached_cb = device_model_probe_detached; |
| 2970 |
|
| 2971 |
dmss->qmp.ao = ao; |
| 2972 |
dmss->qmp.callback = device_model_probe_cmdline; |
| 2973 |
dmss->qmp.domid = domid; |
| 2974 |
dmss->qmp.payload_fd = -1; |
| 2975 |
rc = libxl__ev_qmp_send(egc, &dmss->qmp, "query-command-line-options", NULL); |
| 2976 |
if (rc) goto out; |
| 2977 |
|
| 2978 |
rc = libxl__spawn_spawn(egc, spawn); |
| 2979 |
if (rc < 0) |
| 2980 |
goto out; |
| 2981 |
if (!rc) { /* inner child */ |
| 2982 |
setsid(); |
| 2983 |
libxl__exec(gc, null, logfile_w, logfile_w, dm, exec_args, NULL); |
| 2984 |
} |
| 2985 |
probe_spawned = true; |
| 2986 |
} else { |
| 2987 |
/* Continue with launching DM instead of probing it */ |
| 2988 |
probe_spawned = false; |
| 2989 |
} |
| 2990 |
rc = 0; |
| 2991 |
out: |
| 2992 |
if (qmp_probe_fd >= 0) |
| 2993 |
close(qmp_probe_fd); |
| 2994 |
if (null >= 0) |
| 2995 |
close(null); |
| 2996 |
if (logfile_w >= 0) |
| 2997 |
close(logfile_w); |
| 2998 |
if (rc || !probe_spawned) |
| 2999 |
device_model_launch(egc, dmss, rc); |
| 3000 |
} |
| 3001 |
|
| 3002 |
static void device_model_probe_startup_failed(libxl__egc *egc, |
| 3003 |
libxl__spawn_state *spawn, int rc) |
| 3004 |
{ |
| 3005 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(spawn, *dmss, spawn); |
| 3006 |
device_model_probe_spawn_outcome(egc, dmss, rc); |
| 3007 |
} |
| 3008 |
|
| 3009 |
static void device_model_probe_confirm(libxl__egc *egc, |
| 3010 |
libxl__spawn_state *spawn, const char *xsdata) |
| 3011 |
{ |
| 3012 |
/* Nothing to do, confirmation is done via QMP instead */ |
| 3013 |
} |
| 3014 |
|
| 3015 |
static void device_model_probe_detached(libxl__egc *egc, |
| 3016 |
libxl__spawn_state *spawn) |
| 3017 |
{ |
| 3018 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(spawn, *dmss, spawn); |
| 3019 |
device_model_probe_spawn_outcome(egc, dmss, 0); |
| 3020 |
} |
| 3021 |
|
| 3022 |
static void device_model_probe_cmdline(libxl__egc *egc, |
| 3023 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc) |
| 3024 |
{ |
| 3025 |
EGC_GC; |
| 3026 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(qmp, *dmss, qmp); |
| 3027 |
|
| 3028 |
if (rc) |
| 3029 |
goto out; |
| 3030 |
|
| 3031 |
/* |
| 3032 |
* query-command-line-options response: |
| 3033 |
* [ { 'option': 'str', 'parameters': [{ 'name': 'str', ... }] } ] |
| 3034 |
*/ |
| 3035 |
const libxl__json_object *option; |
| 3036 |
for (int i_option = 0; |
| 3037 |
(option = libxl__json_array_get(response, i_option)); |
| 3038 |
i_option++) { |
| 3039 |
const libxl__json_object *o; |
| 3040 |
const char *opt_str; |
| 3041 |
|
| 3042 |
o = libxl__json_map_get("option", option, JSON_STRING); |
| 3043 |
if (!o) { |
| 3044 |
rc = ERROR_QEMU_API; |
| 3045 |
goto out; |
| 3046 |
} |
| 3047 |
opt_str = libxl__json_object_get_string(o); |
| 3048 |
|
| 3049 |
if (!strcmp("run-with", opt_str)) { |
| 3050 |
const libxl__json_object *params; |
| 3051 |
const libxl__json_object *item; |
| 3052 |
|
| 3053 |
params = libxl__json_map_get("parameters", option, JSON_ARRAY); |
| 3054 |
for (int i = 0; (item = libxl__json_array_get(params, i)); i++) { |
| 3055 |
o = libxl__json_map_get("name", item, JSON_STRING); |
| 3056 |
if (!o) { |
| 3057 |
rc = ERROR_QEMU_API; |
| 3058 |
goto out; |
| 3059 |
} |
| 3060 |
if (!strcmp("chroot", libxl__json_object_get_string(o))) { |
| 3061 |
dmss->qemu_opts.have_runwith_chroot = true; |
| 3062 |
} |
| 3063 |
else if (!strcmp("user", libxl__json_object_get_string(o))) { |
| 3064 |
dmss->qemu_opts.have_runwith_user = true; |
| 3065 |
} |
| 3066 |
} |
| 3067 |
|
| 3068 |
/* |
| 3069 |
* No need to parse more options, we are only interested with |
| 3070 |
* -run-with at the moment. |
| 3071 |
*/ |
| 3072 |
break; |
| 3073 |
} |
| 3074 |
} |
| 3075 |
|
| 3076 |
qmp->callback = device_model_probe_quit; |
| 3077 |
rc = libxl__ev_qmp_send(egc, qmp, "quit", NULL); |
| 3078 |
if (rc) goto out; |
| 3079 |
return; |
| 3080 |
|
| 3081 |
out: |
| 3082 |
libxl__spawn_initiate_failure(egc, &dmss->spawn, rc); |
| 3083 |
} |
| 3084 |
|
| 3085 |
static void device_model_probe_quit(libxl__egc *egc, |
| 3086 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc) |
| 3087 |
{ |
| 3088 |
EGC_GC; |
| 3089 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(qmp, *dmss, qmp); |
| 3090 |
|
| 3091 |
libxl__ev_qmp_dispose(gc, qmp); |
| 3092 |
libxl__spawn_initiate_detach(gc, &dmss->spawn); |
| 3093 |
} |
| 3094 |
|
| 3095 |
static void device_model_probe_spawn_outcome(libxl__egc *egc, |
| 3096 |
libxl__dm_spawn_state *dmss, int rc) |
| 3097 |
{ |
| 3098 |
EGC_GC; |
| 3099 |
libxl__ev_qmp_dispose(gc, &dmss->qmp); |
| 3100 |
|
| 3101 |
/* Ensure our QEMU command line probe is killed. */ |
| 3102 |
rc = libxl__kill_xs_path(gc, dmss->spawn.pidpath, |
| 3103 |
"qemu command-line probe"); |
| 3104 |
if (rc) { |
| 3105 |
LOGD(WARN, dmss->guest_domid, |
| 3106 |
"Killing qemu command-line probe pid from path %s", |
| 3107 |
dmss->spawn.pidpath); |
| 3108 |
} |
| 3109 |
|
| 3110 |
/* |
| 3111 |
* Ignore all failure from the QEMU command line probe, start the |
| 3112 |
* device model in any case. |
| 3113 |
*/ |
| 3114 |
device_model_launch(egc, dmss, 0); |
| 3115 |
} |
| 3116 |
|
| 3117 |
static void device_model_launch(libxl__egc *egc, |
| 3118 |
libxl__dm_spawn_state *dmss, int rc) |
| 3119 |
{ |
| 3120 |
STATE_AO_GC(dmss->spawn.ao); |
| 3121 |
libxl_ctx *ctx = CTX; |
| 3122 |
libxl_domain_config *guest_config = dmss->guest_config; |
| 3123 |
const libxl_domain_create_info *c_info = &guest_config->c_info; |
| 3124 |
const libxl_domain_build_info *b_info = &guest_config->b_info; |
| 3125 |
const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config); |
| 3126 |
char *path; |
| 3127 |
int logfile_w, null; |
| 3128 |
char **args, **arg, **envs; |
| 3129 |
xs_transaction_t t; |
| 3130 |
char *vm_path; |
| 3131 |
char **pass_stuff; |
| 3132 |
int dm_state_fd = -1; |
| 3133 |
|
| 3134 |
/* convenience aliases */ |
| 3135 |
const int domid = dmss->guest_domid; |
| 3136 |
libxl__domain_build_state *const state = dmss->build_state; |
| 3137 |
libxl__spawn_state *const spawn = &dmss->spawn; |
| 3138 |
const char *const dm = dmss->dm; |
| 3139 |
|
| 3140 |
if (rc) |
| 3141 |
goto out; |
| 3142 |
|
| 3143 |
if (b_info->device_model_version |
| 3144 |
== LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN && |
| 3145 |
libxl_defbool_val(b_info->dm_restrict)) { |
| 3146 |
/* If we have to use dm_restrict, QEMU needs to be new enough |
| 3147 |
* and will have the new interface where we can pre-open the |
| 3148 |
* QMP socket. */ |
| 3149 |
rc = libxl__pre_open_qmp_socket(gc, domid, &state->dm_monitor_fd); |
| 3150 |
if (rc) goto out; |
| 3151 |
} |
| 3152 |
|
| 3153 |
rc = libxl__build_device_model_args(gc, dm, domid, guest_config, |
| 3154 |
&args, &envs, state, |
| 3155 |
&dmss->qemu_opts, |
| 3156 |
&dm_state_fd); |
| 3157 |
if (rc) |
| 3158 |
goto out; |
| 3159 |
|
| 3160 |
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) { |
| 3161 |
path = xs_get_domain_path(ctx->xsh, domid); |
| 3162 |
libxl__xs_printf(gc, XBT_NULL, |
| 3163 |
GCSPRINTF("%s/hvmloader/bios", path), |
| 3164 |
"%s", libxl_bios_type_to_string(b_info->u.hvm.bios)); |
| 3165 |
/* Disable relocating memory to make the MMIO hole larger |
| 3166 |
* unless we're running qemu-traditional and vNUMA is not |
| 3167 |
* configured. */ |
| 3168 |
libxl__xs_printf(gc, XBT_NULL, |
| 3169 |
GCSPRINTF("%s/hvmloader/allow-memory-relocate", path), |
| 3170 |
"%d", |
| 3171 |
b_info->device_model_version==LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL && |
| 3172 |
!libxl__vnuma_configured(b_info)); |
| 3173 |
free(path); |
| 3174 |
} |
| 3175 |
|
| 3176 |
path = DEVICE_MODEL_XS_PATH(gc, LIBXL_TOOLSTACK_DOMID, domid, ""); |
| 3177 |
xs_mkdir(ctx->xsh, XBT_NULL, path); |
| 3178 |
|
| 3179 |
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM && |
| 3180 |
b_info->device_model_version |
| 3181 |
== LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL) |
| 3182 |
libxl__xs_printf(gc, XBT_NULL, GCSPRINTF("%s/disable_pf", path), |
| 3183 |
"%d", !libxl_defbool_val(b_info->u.hvm.xen_platform_pci)); |
| 3184 |
|
| 3185 |
logfile_w = libxl__create_qemu_logfile(gc, GCSPRINTF("qemu-dm-%s", |
| 3186 |
c_info->name)); |
| 3187 |
if (logfile_w < 0) { |
| 3188 |
rc = logfile_w; |
| 3189 |
goto out; |
| 3190 |
} |
| 3191 |
null = open("/dev/null", O_RDONLY); |
| 3192 |
if (null < 0) { |
| 3193 |
LOGED(ERROR, domid, "unable to open /dev/null"); |
| 3194 |
rc = ERROR_FAIL; |
| 3195 |
goto out_close; |
| 3196 |
} |
| 3197 |
|
| 3198 |
const char *dom_path = libxl__xs_get_dompath(gc, domid); |
| 3199 |
|
| 3200 |
/* |
| 3201 |
* If we're starting the dm with a non-root UID, save the UID so |
| 3202 |
* that we can reliably kill it and any subprocesses |
| 3203 |
*/ |
| 3204 |
if (state->dm_kill_uid) |
| 3205 |
libxl__xs_printf(gc, XBT_NULL, |
| 3206 |
GCSPRINTF("%s/image/device-model-kill-uid", dom_path), |
| 3207 |
"%s", state->dm_kill_uid); |
| 3208 |
|
| 3209 |
if (vnc && vnc->passwd) { |
| 3210 |
/* This xenstore key will only be used by qemu-xen-traditionnal. |
| 3211 |
* The code to supply vncpasswd to qemu-xen is later. */ |
| 3212 |
retry_transaction: |
| 3213 |
/* Find uuid and the write the vnc password to xenstore for qemu. */ |
| 3214 |
t = xs_transaction_start(ctx->xsh); |
| 3215 |
vm_path = libxl__xs_read(gc,t,GCSPRINTF("%s/vm", dom_path)); |
| 3216 |
if (vm_path) { |
| 3217 |
/* Now write the vncpassword into it. */ |
| 3218 |
pass_stuff = libxl__calloc(gc, 3, sizeof(char *)); |
| 3219 |
pass_stuff[0] = "vncpasswd"; |
| 3220 |
pass_stuff[1] = vnc->passwd; |
| 3221 |
libxl__xs_writev(gc,t,vm_path,pass_stuff); |
| 3222 |
if (!xs_transaction_end(ctx->xsh, t, 0)) |
| 3223 |
if (errno == EAGAIN) |
| 3224 |
goto retry_transaction; |
| 3225 |
} |
| 3226 |
} |
| 3227 |
|
| 3228 |
LOGD(DEBUG, domid, "Spawning device-model %s with arguments:", dm); |
| 3229 |
for (arg = args; *arg; arg++) |
| 3230 |
LOGD(DEBUG, domid, " %s", *arg); |
| 3231 |
if (*envs) { |
| 3232 |
LOGD(DEBUG, domid, "Spawning device-model %s with additional environment:", dm); |
| 3233 |
for (arg = envs; *arg; arg += 2) |
| 3234 |
LOGD(DEBUG, domid, " %s=%s", arg[0], arg[1]); |
| 3235 |
} |
| 3236 |
|
| 3237 |
spawn->what = GCSPRINTF("domain %d device model", domid); |
| 3238 |
spawn->xspath = DEVICE_MODEL_XS_PATH(gc, LIBXL_TOOLSTACK_DOMID, domid, |
| 3239 |
"/state"); |
| 3240 |
spawn->timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000; |
| 3241 |
spawn->pidpath = GCSPRINTF("%s/image/device-model-pid", dom_path); |
| 3242 |
spawn->midproc_cb = libxl__spawn_record_pid; |
| 3243 |
spawn->confirm_cb = device_model_confirm; |
| 3244 |
spawn->failure_cb = device_model_startup_failed; |
| 3245 |
spawn->detached_cb = device_model_detached; |
| 3246 |
|
| 3247 |
if (state->dm_monitor_fd >= 0) { |
| 3248 |
/* There is a valid QMP socket available now, |
| 3249 |
* use it to find out when QEMU is ready */ |
| 3250 |
dmss->qmp.ao = ao; |
| 3251 |
dmss->qmp.callback = device_model_qmp_cb; |
| 3252 |
dmss->qmp.domid = domid; |
| 3253 |
dmss->qmp.payload_fd = -1; |
| 3254 |
rc = libxl__ev_qmp_send(egc, &dmss->qmp, "query-status", NULL); |
| 3255 |
if (rc) goto out_close; |
| 3256 |
} |
| 3257 |
|
| 3258 |
rc = libxl__spawn_spawn(egc, spawn); |
| 3259 |
if (rc < 0) |
| 3260 |
goto out_close; |
| 3261 |
if (!rc) { /* inner child */ |
| 3262 |
setsid(); |
| 3263 |
if (libxl_defbool_val(b_info->dm_restrict)) { |
| 3264 |
rc = libxl__local_dm_preexec_restrict(gc); |
| 3265 |
if (rc) |
| 3266 |
_exit(-1); |
| 3267 |
} |
| 3268 |
libxl__exec(gc, null, logfile_w, logfile_w, dm, args, envs); |
| 3269 |
} |
| 3270 |
|
| 3271 |
rc = 0; |
| 3272 |
|
| 3273 |
out_close: |
| 3274 |
if (null >= 0) close(null); |
| 3275 |
if (logfile_w >= 0) close(logfile_w); |
| 3276 |
out: |
| 3277 |
if (dm_state_fd >= 0) close(dm_state_fd); |
| 3278 |
if (rc) |
| 3279 |
device_model_spawn_outcome(egc, dmss, rc); |
| 3280 |
} |
| 3281 |
|
| 3282 |
bool libxl__query_qemu_backend(libxl__gc *gc, uint32_t domid, |
| 3283 |
uint32_t backend_id, const char *type, bool def) |
| 3284 |
{ |
| 3285 |
char *path; |
| 3286 |
char **dir; |
| 3287 |
unsigned int n; |
| 3288 |
|
| 3289 |
path = GCSPRINTF("%s/device-model/%u/backends", |
| 3290 |
libxl__xs_get_dompath(gc, backend_id), domid); |
| 3291 |
dir = libxl__xs_directory(gc, XBT_NULL, path, &n); |
| 3292 |
if (!dir) |
| 3293 |
return def; |
| 3294 |
|
| 3295 |
path = GCSPRINTF("%s/device-model/%u/backends/%s", |
| 3296 |
libxl__xs_get_dompath(gc, backend_id), domid, type); |
| 3297 |
dir = libxl__xs_directory(gc, XBT_NULL, path, &n); |
| 3298 |
|
| 3299 |
return !!dir; |
| 3300 |
} |
| 3301 |
|
| 3302 |
static void device_model_confirm(libxl__egc *egc, libxl__spawn_state *spawn, |
| 3303 |
const char *xsdata) |
| 3304 |
{ |
| 3305 |
STATE_AO_GC(spawn->ao); |
| 3306 |
|
| 3307 |
if (!xsdata) |
| 3308 |
return; |
| 3309 |
|
| 3310 |
if (strcmp(xsdata, "running")) |
| 3311 |
return; |
| 3312 |
|
| 3313 |
libxl__spawn_initiate_detach(gc, spawn); |
| 3314 |
} |
| 3315 |
|
| 3316 |
static void device_model_startup_failed(libxl__egc *egc, |
| 3317 |
libxl__spawn_state *spawn, |
| 3318 |
int rc) |
| 3319 |
{ |
| 3320 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(spawn, *dmss, spawn); |
| 3321 |
device_model_spawn_outcome(egc, dmss, rc); |
| 3322 |
} |
| 3323 |
|
| 3324 |
static void device_model_detached(libxl__egc *egc, |
| 3325 |
libxl__spawn_state *spawn) |
| 3326 |
{ |
| 3327 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(spawn, *dmss, spawn); |
| 3328 |
device_model_spawn_outcome(egc, dmss, 0); |
| 3329 |
} |
| 3330 |
|
| 3331 |
static void device_model_qmp_cb(libxl__egc *egc, libxl__ev_qmp *ev, |
| 3332 |
const libxl__json_object *response, |
| 3333 |
int rc) |
| 3334 |
{ |
| 3335 |
EGC_GC; |
| 3336 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(ev, *dmss, qmp); |
| 3337 |
const libxl__json_object *o; |
| 3338 |
const char *status; |
| 3339 |
const char *expected_state; |
| 3340 |
|
| 3341 |
libxl__ev_qmp_dispose(gc, ev); |
| 3342 |
|
| 3343 |
if (rc) |
| 3344 |
goto failed; |
| 3345 |
|
| 3346 |
o = libxl__json_map_get("status", response, JSON_STRING); |
| 3347 |
if (!o) { |
| 3348 |
LOGD(ERROR, ev->domid, |
| 3349 |
"Missing 'status' in response to 'query-status'"); |
| 3350 |
LOGD(DEBUG, ev->domid, ".. instead, got: %s", JSON(response)); |
| 3351 |
rc = ERROR_QEMU_API; |
| 3352 |
goto failed; |
| 3353 |
} |
| 3354 |
status = libxl__json_object_get_string(o); |
| 3355 |
if (!dmss->build_state->saved_state) |
| 3356 |
expected_state = "prelaunch"; |
| 3357 |
else |
| 3358 |
expected_state = "paused"; |
| 3359 |
if (strcmp(status, expected_state)) { |
| 3360 |
LOGD(ERROR, ev->domid, "Unexpected QEMU status: %s", status); |
| 3361 |
rc = ERROR_NOT_READY; |
| 3362 |
goto failed; |
| 3363 |
} |
| 3364 |
|
| 3365 |
libxl__spawn_initiate_detach(gc, &dmss->spawn); |
| 3366 |
return; |
| 3367 |
|
| 3368 |
failed: |
| 3369 |
LOGD(ERROR, ev->domid, "QEMU did not start properly, rc=%d", rc); |
| 3370 |
libxl__spawn_initiate_failure(egc, &dmss->spawn, rc); |
| 3371 |
} |
| 3372 |
|
| 3373 |
static void device_model_spawn_outcome(libxl__egc *egc, |
| 3374 |
libxl__dm_spawn_state *dmss, |
| 3375 |
int rc) |
| 3376 |
{ |
| 3377 |
STATE_AO_GC(dmss->spawn.ao); |
| 3378 |
int ret2; |
| 3379 |
|
| 3380 |
/* Convenience aliases */ |
| 3381 |
libxl_domain_config *const d_config = dmss->guest_config; |
| 3382 |
|
| 3383 |
if (rc) |
| 3384 |
LOGD(ERROR, dmss->guest_domid, |
| 3385 |
"%s: spawn failed (rc=%d)", dmss->spawn.what, rc); |
| 3386 |
|
| 3387 |
libxl__domain_build_state *state = dmss->build_state; |
| 3388 |
|
| 3389 |
if (state->saved_state) { |
| 3390 |
ret2 = unlink(state->saved_state); |
| 3391 |
if (ret2) { |
| 3392 |
LOGED(ERROR, dmss->guest_domid, "%s: failed to remove device-model state %s", |
| 3393 |
dmss->spawn.what, state->saved_state); |
| 3394 |
rc = ERROR_FAIL; |
| 3395 |
goto out; |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
/* Check if spawn failed */ |
| 3400 |
if (rc) goto out; |
| 3401 |
/* d_config is NULL for xl devd/libxl__spawn_qemu_xenpv_backend(). */ |
| 3402 |
if (d_config && d_config->b_info.device_model_version |
| 3403 |
== LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) { |
| 3404 |
rc = libxl__ev_time_register_rel(ao, &dmss->timeout, |
| 3405 |
devise_model_postconfig_timeout, |
| 3406 |
LIBXL_QMP_CMD_TIMEOUT * 1000); |
| 3407 |
if (rc) goto out; |
| 3408 |
dmss->qmp.ao = ao; |
| 3409 |
dmss->qmp.domid = dmss->guest_domid; |
| 3410 |
dmss->qmp.payload_fd = -1; |
| 3411 |
dmss->qmp.callback = device_model_postconfig_chardev; |
| 3412 |
rc = libxl__ev_qmp_send(egc, &dmss->qmp, "query-chardev", NULL); |
| 3413 |
if (rc) goto out; |
| 3414 |
return; |
| 3415 |
} |
| 3416 |
|
| 3417 |
out: |
| 3418 |
device_model_postconfig_done(egc, dmss, rc); /* must be last */ |
| 3419 |
} |
| 3420 |
|
| 3421 |
static void device_model_postconfig_chardev(libxl__egc *egc, |
| 3422 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc) |
| 3423 |
{ |
| 3424 |
EGC_GC; |
| 3425 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(qmp, *dmss, qmp); |
| 3426 |
const libxl_vnc_info *vnc = libxl__dm_vnc(dmss->guest_config); |
| 3427 |
const libxl__json_object *item = NULL; |
| 3428 |
const libxl__json_object *o = NULL; |
| 3429 |
int i = 0; |
| 3430 |
const char *label; |
| 3431 |
const char *filename; |
| 3432 |
int port; |
| 3433 |
char *endptr; |
| 3434 |
const char *dompath; |
| 3435 |
const char serial[] = "serial"; |
| 3436 |
const size_t seriall = sizeof(serial) - 1; |
| 3437 |
const char pty[] = "pty:"; |
| 3438 |
const size_t ptyl = sizeof(pty) - 1; |
| 3439 |
|
| 3440 |
if (rc) goto out; |
| 3441 |
|
| 3442 |
/* |
| 3443 |
* query-chardev response: |
| 3444 |
* [{ 'label': 'str', |
| 3445 |
* 'filename': 'str', |
| 3446 |
* 'frontend-open': 'bool' }, ...] |
| 3447 |
*/ |
| 3448 |
|
| 3449 |
for (i = 0; (item = libxl__json_array_get(response, i)); i++) { |
| 3450 |
o = libxl__json_map_get("label", item, JSON_STRING); |
| 3451 |
if (!o) goto protocol_error; |
| 3452 |
label = libxl__json_object_get_string(o); |
| 3453 |
|
| 3454 |
/* Check if the "label" start with "serial". */ |
| 3455 |
if (!label || strncmp(label, serial, seriall)) |
| 3456 |
continue; |
| 3457 |
port = strtol(label + seriall, &endptr, 10); |
| 3458 |
if (*(label + seriall) == '\0' || *endptr != '\0') { |
| 3459 |
LOGD(ERROR, qmp->domid, |
| 3460 |
"Invalid serial port number: %s", label); |
| 3461 |
rc = ERROR_QEMU_API; |
| 3462 |
goto out; |
| 3463 |
} |
| 3464 |
|
| 3465 |
o = libxl__json_map_get("filename", item, JSON_STRING); |
| 3466 |
if (!o) goto protocol_error; |
| 3467 |
filename = libxl__json_object_get_string(o); |
| 3468 |
|
| 3469 |
/* Check if filename start with "pty:" */ |
| 3470 |
if (!filename || strncmp(filename, pty, ptyl)) |
| 3471 |
continue; |
| 3472 |
|
| 3473 |
dompath = libxl__xs_get_dompath(gc, qmp->domid); |
| 3474 |
if (!dompath) { |
| 3475 |
rc = ERROR_FAIL; |
| 3476 |
goto out; |
| 3477 |
} |
| 3478 |
rc = libxl__xs_printf(gc, XBT_NULL, |
| 3479 |
GCSPRINTF("%s/serial/%d/tty", dompath, port), |
| 3480 |
"%s", filename + ptyl); |
| 3481 |
if (rc) goto out; |
| 3482 |
} |
| 3483 |
|
| 3484 |
if (!vnc) |
| 3485 |
goto out; |
| 3486 |
|
| 3487 |
qmp->callback = device_model_postconfig_vnc; |
| 3488 |
rc = libxl__ev_qmp_send(egc, qmp, "query-vnc", NULL); |
| 3489 |
if (rc) goto out; |
| 3490 |
return; |
| 3491 |
|
| 3492 |
protocol_error: |
| 3493 |
rc = ERROR_QEMU_API; |
| 3494 |
LOGD(ERROR, qmp->domid, |
| 3495 |
"unexpected response to QMP cmd 'query-chardev', received:\n%s", |
| 3496 |
JSON(response)); |
| 3497 |
out: |
| 3498 |
device_model_postconfig_done(egc, dmss, rc); /* must be last */ |
| 3499 |
} |
| 3500 |
|
| 3501 |
static void device_model_postconfig_vnc(libxl__egc *egc, |
| 3502 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc) |
| 3503 |
{ |
| 3504 |
EGC_GC; |
| 3505 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(qmp, *dmss, qmp); |
| 3506 |
const libxl_vnc_info *vnc = libxl__dm_vnc(dmss->guest_config); |
| 3507 |
const libxl__json_object *o; |
| 3508 |
libxl__json_object *args = NULL; |
| 3509 |
|
| 3510 |
if (rc) goto out; |
| 3511 |
|
| 3512 |
/* |
| 3513 |
* query-vnc response: |
| 3514 |
* { 'enabled': 'bool', '*host': 'str', '*service': 'str' } |
| 3515 |
*/ |
| 3516 |
|
| 3517 |
o = libxl__json_map_get("enabled", response, JSON_BOOL); |
| 3518 |
if (!o) goto protocol_error; |
| 3519 |
if (libxl__json_object_get_bool(o)) { |
| 3520 |
const char *addr, *port; |
| 3521 |
const char *dompath; |
| 3522 |
|
| 3523 |
o = libxl__json_map_get("host", response, JSON_STRING); |
| 3524 |
if (!o) goto protocol_error; |
| 3525 |
addr = libxl__json_object_get_string(o); |
| 3526 |
o = libxl__json_map_get("service", response, JSON_STRING); |
| 3527 |
if (!o) goto protocol_error; |
| 3528 |
port = libxl__json_object_get_string(o); |
| 3529 |
|
| 3530 |
dompath = libxl__xs_get_dompath(gc, qmp->domid); |
| 3531 |
if (!dompath) { |
| 3532 |
rc = ERROR_FAIL; |
| 3533 |
goto out; |
| 3534 |
} |
| 3535 |
rc = libxl__xs_printf(gc, XBT_NULL, |
| 3536 |
GCSPRINTF("%s/console/vnc-listen", dompath), |
| 3537 |
"%s", addr); |
| 3538 |
if (rc) goto out; |
| 3539 |
rc = libxl__xs_printf(gc, XBT_NULL, |
| 3540 |
GCSPRINTF("%s/console/vnc-port", dompath), |
| 3541 |
"%s", port); |
| 3542 |
if (rc) goto out; |
| 3543 |
} |
| 3544 |
|
| 3545 |
if (vnc && vnc->passwd && vnc->passwd[0]) { |
| 3546 |
qmp->callback = device_model_postconfig_vnc_passwd; |
| 3547 |
libxl__qmp_param_add_string(gc, &args, "password", vnc->passwd); |
| 3548 |
rc = libxl__ev_qmp_send(egc, qmp, "change-vnc-password", args); |
| 3549 |
if (rc) goto out; |
| 3550 |
return; |
| 3551 |
} |
| 3552 |
|
| 3553 |
rc = 0; |
| 3554 |
goto out; |
| 3555 |
|
| 3556 |
protocol_error: |
| 3557 |
rc = ERROR_QEMU_API; |
| 3558 |
LOGD(ERROR, qmp->domid, |
| 3559 |
"unexpected response to QMP cmd 'query-vnc', received:\n%s", |
| 3560 |
JSON(response)); |
| 3561 |
out: |
| 3562 |
device_model_postconfig_done(egc, dmss, rc); /* must be last */ |
| 3563 |
} |
| 3564 |
|
| 3565 |
static void device_model_postconfig_vnc_passwd(libxl__egc *egc, |
| 3566 |
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc) |
| 3567 |
{ |
| 3568 |
EGC_GC; |
| 3569 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(qmp, *dmss, qmp); |
| 3570 |
const libxl_vnc_info *vnc = libxl__dm_vnc(dmss->guest_config); |
| 3571 |
const char *dompath; |
| 3572 |
|
| 3573 |
if (rc) goto out; |
| 3574 |
|
| 3575 |
dompath = libxl__xs_get_dompath(gc, qmp->domid); |
| 3576 |
if (!dompath) { |
| 3577 |
rc = ERROR_FAIL; |
| 3578 |
goto out; |
| 3579 |
} |
| 3580 |
rc = libxl__xs_printf(gc, XBT_NULL, |
| 3581 |
GCSPRINTF("%s/console/vnc-pass", dompath), |
| 3582 |
"%s", vnc->passwd); |
| 3583 |
|
| 3584 |
out: |
| 3585 |
device_model_postconfig_done(egc, dmss, rc); /* must be last */ |
| 3586 |
} |
| 3587 |
|
| 3588 |
void devise_model_postconfig_timeout(libxl__egc *egc, libxl__ev_time *ev, |
| 3589 |
const struct timeval *requested_abs, |
| 3590 |
int rc) |
| 3591 |
{ |
| 3592 |
libxl__dm_spawn_state *dmss = CONTAINER_OF(ev, *dmss, timeout); |
| 3593 |
device_model_postconfig_done(egc, dmss, rc); /* must be last */ |
| 3594 |
} |
| 3595 |
|
| 3596 |
|
| 3597 |
static void device_model_postconfig_done(libxl__egc *egc, |
| 3598 |
libxl__dm_spawn_state *dmss, |
| 3599 |
int rc) |
| 3600 |
{ |
| 3601 |
EGC_GC; |
| 3602 |
|
| 3603 |
if (rc) |
| 3604 |
LOGD(ERROR, dmss->guest_domid, |
| 3605 |
"Post DM startup configs failed, rc=%d", rc); |
| 3606 |
dmss_dispose(gc, dmss); |
| 3607 |
dmss->callback(egc, dmss, rc); |
| 3608 |
} |
| 3609 |
|
| 3610 |
void libxl__spawn_qemu_xenpv_backend(libxl__egc *egc, |
| 3611 |
libxl__dm_spawn_state *dmss) |
| 3612 |
{ |
| 3613 |
STATE_AO_GC(dmss->spawn.ao); |
| 3614 |
flexarray_t *dm_args, *dm_envs; |
| 3615 |
char **args, **envs; |
| 3616 |
const char *dm; |
| 3617 |
int logfile_w, null = -1, rc; |
| 3618 |
uint32_t domid = dmss->guest_domid; |
| 3619 |
|
| 3620 |
dmss_init(dmss); |
| 3621 |
|
| 3622 |
/* Always use qemu-xen as device model */ |
| 3623 |
dm = qemu_xen_path(gc); |
| 3624 |
|
| 3625 |
dm_args = flexarray_make(gc, 15, 1); |
| 3626 |
dm_envs = flexarray_make(gc, 1, 1); |
| 3627 |
|
| 3628 |
flexarray_vappend(dm_args, dm, "-xen-domid", |
| 3629 |
GCSPRINTF("%d", domid), NULL); |
| 3630 |
flexarray_append(dm_args, "-xen-attach"); |
| 3631 |
flexarray_vappend(dm_args, "-name", |
| 3632 |
GCSPRINTF("domain-%u", domid), NULL); |
| 3633 |
flexarray_append(dm_args, "-nographic"); |
| 3634 |
flexarray_vappend(dm_args, "-M", "xenpv", NULL); |
| 3635 |
flexarray_vappend(dm_args, "-monitor", "/dev/null", NULL); |
| 3636 |
flexarray_vappend(dm_args, "-serial", "/dev/null", NULL); |
| 3637 |
flexarray_vappend(dm_args, "-parallel", "/dev/null", NULL); |
| 3638 |
flexarray_append(dm_args, NULL); |
| 3639 |
args = (char **) flexarray_contents(dm_args); |
| 3640 |
|
| 3641 |
envs = (char **) flexarray_contents(dm_envs); |
| 3642 |
|
| 3643 |
logfile_w = libxl__create_qemu_logfile(gc, GCSPRINTF("qdisk-%u", domid)); |
| 3644 |
if (logfile_w < 0) { |
| 3645 |
rc = logfile_w; |
| 3646 |
goto out; |
| 3647 |
} |
| 3648 |
null = open("/dev/null", O_RDONLY); |
| 3649 |
if (null < 0) { |
| 3650 |
rc = ERROR_FAIL; |
| 3651 |
goto out; |
| 3652 |
} |
| 3653 |
|
| 3654 |
dmss->guest_config = NULL; |
| 3655 |
/* |
| 3656 |
* Clearly specify Qemu not using a saved state, so |
| 3657 |
* device_model_spawn_outcome doesn't try to unlink it. |
| 3658 |
*/ |
| 3659 |
dmss->build_state = libxl__zalloc(gc, sizeof(*dmss->build_state)); |
| 3660 |
dmss->build_state->saved_state = 0; |
| 3661 |
|
| 3662 |
dmss->spawn.what = GCSPRINTF("domain %u Qdisk backend", domid); |
| 3663 |
dmss->spawn.xspath = GCSPRINTF("device-model/%u/state", domid); |
| 3664 |
dmss->spawn.timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000; |
| 3665 |
/* |
| 3666 |
* We cannot save Qemu pid anywhere in the xenstore guest dir, |
| 3667 |
* because we will call this from unprivileged driver domains, |
| 3668 |
* so save it in the current domain libxl private dir. |
| 3669 |
*/ |
| 3670 |
dmss->spawn.pidpath = GCSPRINTF("libxl/%u/qdisk-backend-pid", domid); |
| 3671 |
dmss->spawn.midproc_cb = libxl__spawn_record_pid; |
| 3672 |
dmss->spawn.confirm_cb = device_model_confirm; |
| 3673 |
dmss->spawn.failure_cb = device_model_startup_failed; |
| 3674 |
dmss->spawn.detached_cb = device_model_detached; |
| 3675 |
rc = libxl__spawn_spawn(egc, &dmss->spawn); |
| 3676 |
if (rc < 0) |
| 3677 |
goto out; |
| 3678 |
if (!rc) { /* inner child */ |
| 3679 |
setsid(); |
| 3680 |
libxl__exec(gc, null, logfile_w, logfile_w, dm, args, envs); |
| 3681 |
} |
| 3682 |
|
| 3683 |
rc = 0; |
| 3684 |
out: |
| 3685 |
dmss_dispose(gc, dmss); |
| 3686 |
if (logfile_w >= 0) close(logfile_w); |
| 3687 |
if (null >= 0) close(null); |
| 3688 |
/* callback on error only, success goes via dmss->spawn.*_cb */ |
| 3689 |
if (rc) dmss->callback(egc, dmss, rc); |
| 3690 |
return; |
| 3691 |
} |
| 3692 |
|
| 3693 |
/* Generic function to signal a Qemu instance to exit */ |
| 3694 |
static int kill_device_model(libxl__gc *gc, const char *xs_path_pid) |
| 3695 |
{ |
| 3696 |
return libxl__kill_xs_path(gc, xs_path_pid, "Device Model"); |
| 3697 |
} |
| 3698 |
|
| 3699 |
/* Helper to destroy a Qdisk backend */ |
| 3700 |
int libxl__destroy_qemu_xenpv_backend(libxl__gc *gc, uint32_t domid) |
| 3701 |
{ |
| 3702 |
char *pid_path; |
| 3703 |
int rc; |
| 3704 |
|
| 3705 |
pid_path = GCSPRINTF("libxl/%u/qdisk-backend-pid", domid); |
| 3706 |
|
| 3707 |
rc = kill_device_model(gc, pid_path); |
| 3708 |
if (rc) |
| 3709 |
goto out; |
| 3710 |
|
| 3711 |
libxl__xs_rm_checked(gc, XBT_NULL, pid_path); |
| 3712 |
libxl__xs_rm_checked(gc, XBT_NULL, |
| 3713 |
GCSPRINTF("device-model/%u", domid)); |
| 3714 |
|
| 3715 |
out: |
| 3716 |
return rc; |
| 3717 |
} |
| 3718 |
|
| 3719 |
/* Asynchronous device model destroy functions */ |
| 3720 |
|
| 3721 |
static int kill_device_model_uid_child(libxl__destroy_devicemodel_state *ddms, |
| 3722 |
const char *dm_kill_uid_str); |
| 3723 |
|
| 3724 |
static void kill_device_model_uid_cb(libxl__egc *egc, |
| 3725 |
libxl__ev_child *destroyer, |
| 3726 |
pid_t pid, int status); |
| 3727 |
|
| 3728 |
/* |
| 3729 |
* If we have a uid, we shouldn't kill by pid. This is because a |
| 3730 |
* hostile QEMU might have exited, in which case the pid we have may |
| 3731 |
* be that of another process. |
| 3732 |
* |
| 3733 |
* The running devicemodel has permission over a specific domain id; |
| 3734 |
* this means that ideally we wouldn't the domain in question (freeing |
| 3735 |
* up the domain id for reuse) until we're confident that we've killed |
| 3736 |
* the domain. |
| 3737 |
* |
| 3738 |
* In general, destroy as much as we can; but return an error if there |
| 3739 |
* are any errors, so that the domain destroy will be aborted, and the |
| 3740 |
* domain itself will remain, giving the admin an opportunity to fix |
| 3741 |
* any issues and re-try the domain destroy. |
| 3742 |
*/ |
| 3743 |
void libxl__destroy_device_model(libxl__egc *egc, |
| 3744 |
libxl__destroy_devicemodel_state *ddms) |
| 3745 |
{ |
| 3746 |
STATE_AO_GC(ddms->ao); |
| 3747 |
int rc; |
| 3748 |
int domid = ddms->domid; |
| 3749 |
char *path; |
| 3750 |
const char *dm_kill_uid_str = NULL; |
| 3751 |
int reaper_pid; |
| 3752 |
|
| 3753 |
ddms->rc = 0; |
| 3754 |
|
| 3755 |
path = DEVICE_MODEL_XS_PATH(gc, LIBXL_TOOLSTACK_DOMID, domid, ""); |
| 3756 |
rc = libxl__xs_rm_checked(gc, XBT_NULL, path); |
| 3757 |
if (rc) { |
| 3758 |
ACCUMULATE_RC(ddms->rc); |
| 3759 |
LOGD(ERROR, domid, "xs_rm failed for %s", path); |
| 3760 |
} |
| 3761 |
|
| 3762 |
/* |
| 3763 |
* See if we should try to kill by uid |
| 3764 |
*/ |
| 3765 |
path = GCSPRINTF("/local/domain/%d/image/device-model-kill-uid", domid); |
| 3766 |
rc = libxl__xs_read_checked(gc, XBT_NULL, path, &dm_kill_uid_str); |
| 3767 |
|
| 3768 |
/* |
| 3769 |
* If there was an error here, accumulate the error and fall back |
| 3770 |
* to killing by pid. |
| 3771 |
*/ |
| 3772 |
if (rc) { |
| 3773 |
/* |
| 3774 |
* Technically the state of the string passed to libxl__xs_read_checked() is |
| 3775 |
* "undefined" in the case rc == 0 (according to libxl_internal.h). Set it to |
| 3776 |
* NULL to prevent undefined behavior. |
| 3777 |
*/ |
| 3778 |
dm_kill_uid_str = NULL; |
| 3779 |
ACCUMULATE_RC(ddms->rc); |
| 3780 |
LOGD(ERROR, domid, "Reading dm UID path failed for %s", path); |
| 3781 |
} |
| 3782 |
|
| 3783 |
/* The DM has its own uid; Attempt to kill all processes with that UID */ |
| 3784 |
if (dm_kill_uid_str) { |
| 3785 |
LOGD(DEBUG, domid, "Found DM uid %s, destroying by uid", |
| 3786 |
dm_kill_uid_str); |
| 3787 |
|
| 3788 |
reaper_pid = libxl__ev_child_fork(gc, &ddms->destroyer, |
| 3789 |
kill_device_model_uid_cb); |
| 3790 |
if (reaper_pid < 0) { |
| 3791 |
rc = ERROR_FAIL; |
| 3792 |
ACCUMULATE_RC(ddms->rc); |
| 3793 |
/* |
| 3794 |
* Note that if this fails, we still don't kill by pid, to |
| 3795 |
* make sure that an untrusted DM has not "maliciously" |
| 3796 |
* exited (potentially causing us to kill an unrelated |
| 3797 |
* process which happened to get the same pid). |
| 3798 |
*/ |
| 3799 |
goto out; |
| 3800 |
} |
| 3801 |
|
| 3802 |
if (!reaper_pid) { /* child */ |
| 3803 |
rc = kill_device_model_uid_child(ddms, dm_kill_uid_str); |
| 3804 |
assert(rc <= 0 && rc >= -125); |
| 3805 |
_exit(-rc); |
| 3806 |
} |
| 3807 |
|
| 3808 |
/* |
| 3809 |
* Parent of successful fork; execution will pick up in |
| 3810 |
* kill_device_model_uid_cb when child exits |
| 3811 |
*/ |
| 3812 |
return; |
| 3813 |
} |
| 3814 |
|
| 3815 |
/* |
| 3816 |
* No uid to kill; attept to kill by pid. |
| 3817 |
*/ |
| 3818 |
LOGD(DEBUG, domid, "Didn't find dm UID; destroying by pid"); |
| 3819 |
|
| 3820 |
path = GCSPRINTF("/local/domain/%d/image/device-model-pid", domid); |
| 3821 |
rc = kill_device_model(gc, path); |
| 3822 |
|
| 3823 |
if (rc) { |
| 3824 |
ACCUMULATE_RC(ddms->rc); |
| 3825 |
LOGD(ERROR, domid, "Killing device model pid from path %s", path); |
| 3826 |
} |
| 3827 |
|
| 3828 |
out: |
| 3829 |
/* |
| 3830 |
* NB that we always pass '0' here for the "status of exited |
| 3831 |
* process"; since there is no process, it always "succeeds". |
| 3832 |
* Errors are accumulated in ddms->rc and will be handled |
| 3833 |
* correctly. |
| 3834 |
*/ |
| 3835 |
kill_device_model_uid_cb(egc, &ddms->destroyer, -1, 0); |
| 3836 |
return; |
| 3837 |
} |
| 3838 |
|
| 3839 |
/* |
| 3840 |
* Note that this attempts to grab a file lock, so must be called from |
| 3841 |
* a sub-process. |
| 3842 |
*/ |
| 3843 |
static int get_reaper_lock_and_uid(libxl__destroy_devicemodel_state *ddms, |
| 3844 |
uid_t *reaper_uid) |
| 3845 |
{ |
| 3846 |
STATE_AO_GC(ddms->ao); |
| 3847 |
int domid = ddms->domid; |
| 3848 |
int r; |
| 3849 |
const char * lockfile; |
| 3850 |
int fd; /* "leaked" into the general process context (even on failure) */ |
| 3851 |
|
| 3852 |
/* Try to lock the "reaper uid" */ |
| 3853 |
lockfile = GCSPRINTF("%s/dm-reaper-lock", libxl__run_dir_path()); |
| 3854 |
|
| 3855 |
/* |
| 3856 |
* NB that since we've just forked, we can't have any |
| 3857 |
* threads; so we don't need the libxl__carefd |
| 3858 |
* infrastructure here. |
| 3859 |
*/ |
| 3860 |
fd = open(lockfile, O_RDWR|O_CREAT, 0644); |
| 3861 |
if (fd < 0) { |
| 3862 |
LOGED(ERROR, domid, |
| 3863 |
"unexpected error while trying to open lockfile %s, errno=%d", |
| 3864 |
lockfile, errno); |
| 3865 |
return ERROR_FAIL; |
| 3866 |
} |
| 3867 |
|
| 3868 |
/* Try to lock the file, retrying on EINTR */ |
| 3869 |
for (;;) { |
| 3870 |
r = flock(fd, LOCK_EX); |
| 3871 |
if (!r) |
| 3872 |
break; |
| 3873 |
if (errno != EINTR) { |
| 3874 |
/* All other errno: EBADF, EINVAL, ENOLCK, EWOULDBLOCK */ |
| 3875 |
LOGED(ERROR, domid, |
| 3876 |
"unexpected error while trying to lock %s, fd=%d, errno=%d", |
| 3877 |
lockfile, fd, errno); |
| 3878 |
return ERROR_FAIL; |
| 3879 |
} |
| 3880 |
} |
| 3881 |
|
| 3882 |
/* |
| 3883 |
* Get reaper_uid. If we can't find such a uid, return an error. |
| 3884 |
*/ |
| 3885 |
return libxl__get_reaper_uid(gc, reaper_uid); |
| 3886 |
} |
| 3887 |
|
| 3888 |
|
| 3889 |
/* |
| 3890 |
* Destroy all processes of the given uid by setresuid to the |
| 3891 |
* specified uid and kill(-1). NB this MUST BE CALLED FROM A SEPARATE |
| 3892 |
* PROCESS from the normal libxl process, and should exit immediately |
| 3893 |
* after return. Returns a libxl-style error code that is guaranteed |
| 3894 |
* to be >= -125. |
| 3895 |
*/ |
| 3896 |
static int kill_device_model_uid_child(libxl__destroy_devicemodel_state *ddms, |
| 3897 |
const char *dm_kill_uid_str) { |
| 3898 |
STATE_AO_GC(ddms->ao); |
| 3899 |
int domid = ddms->domid; |
| 3900 |
int r, rc = 0; |
| 3901 |
uid_t dm_kill_uid = atoi(dm_kill_uid_str); |
| 3902 |
uid_t reaper_uid; |
| 3903 |
|
| 3904 |
/* |
| 3905 |
* Try to kill the devicemodel by uid. The safest way to do this |
| 3906 |
* is to set euid == dm_uid, but the ruid to something else. If |
| 3907 |
* we can't get a separate ruid, carry on trying to kill the |
| 3908 |
* process anyway using dm_uid for the ruid. This is racy (the dm |
| 3909 |
* may be able to kill(-1) us before we kill them), but worth |
| 3910 |
* trying. |
| 3911 |
* |
| 3912 |
* NB: Even if we don't have a separate reaper_uid, the parent can |
| 3913 |
* know whether we won the race by looking at the status variable; |
| 3914 |
* so we don't strictly need to return failure in this case. But |
| 3915 |
* if there's a misconfiguration, it's better to alert the |
| 3916 |
* administator sooner rather than later; so if we fail to get a |
| 3917 |
* reaper uid, report an error even if the kill succeeds. |
| 3918 |
*/ |
| 3919 |
rc = get_reaper_lock_and_uid(ddms, &reaper_uid); |
| 3920 |
if (rc) { |
| 3921 |
reaper_uid = dm_kill_uid; |
| 3922 |
LOGD(WARN, domid, "Couldn't get separate reaper uid;" |
| 3923 |
"carrying on with unsafe kill"); |
| 3924 |
} |
| 3925 |
|
| 3926 |
/* |
| 3927 |
* Should never happen; but if it does, better to have the |
| 3928 |
* toolstack crash with an error than nuking dom0. |
| 3929 |
*/ |
| 3930 |
assert(reaper_uid); |
| 3931 |
assert(dm_kill_uid); |
| 3932 |
|
| 3933 |
LOGD(DEBUG, domid, "DM reaper: calling setresuid(%d, %d, 0)", |
| 3934 |
reaper_uid, dm_kill_uid); |
| 3935 |
r = libxl__setresuid(reaper_uid, dm_kill_uid, 0); |
| 3936 |
if (r) { |
| 3937 |
LOGED(ERROR, domid, "setresuid to (%d, %d, 0)", |
| 3938 |
reaper_uid, dm_kill_uid); |
| 3939 |
rc = rc ?: ERROR_FAIL; |
| 3940 |
goto out; |
| 3941 |
} |
| 3942 |
|
| 3943 |
/* |
| 3944 |
* And kill everyone but me. |
| 3945 |
* |
| 3946 |
* NB that it's not clear from either POSIX or the Linux man page |
| 3947 |
* that ESRCH would be returned with a pid value of -1, but it |
| 3948 |
* doesn't hurt to check. |
| 3949 |
*/ |
| 3950 |
r = kill(-1, 9); |
| 3951 |
if (r && errno != ESRCH) { |
| 3952 |
LOGED(ERROR, domid, "kill(-1,9)"); |
| 3953 |
rc = rc ?: ERROR_FAIL; |
| 3954 |
goto out; |
| 3955 |
} |
| 3956 |
|
| 3957 |
rc = 0; |
| 3958 |
|
| 3959 |
out: |
| 3960 |
return rc; |
| 3961 |
} |
| 3962 |
|
| 3963 |
static void kill_device_model_uid_cb(libxl__egc *egc, |
| 3964 |
libxl__ev_child *destroyer, |
| 3965 |
pid_t pid, int status) |
| 3966 |
{ |
| 3967 |
libxl__destroy_devicemodel_state *ddms = CONTAINER_OF(destroyer, *ddms, destroyer); |
| 3968 |
STATE_AO_GC(ddms->ao); |
| 3969 |
|
| 3970 |
if (status) { |
| 3971 |
int rc = ERROR_FAIL; |
| 3972 |
|
| 3973 |
if (WIFEXITED(status) && WEXITSTATUS(status) <= 125) |
| 3974 |
rc = -WEXITSTATUS(status); |
| 3975 |
|
| 3976 |
ACCUMULATE_RC(ddms->rc); |
| 3977 |
libxl_report_child_exitstatus(CTX, XTL_ERROR, |
| 3978 |
"async domain destroy", pid, status); |
| 3979 |
} |
| 3980 |
|
| 3981 |
/* Always try to clean up qmp, even if something went wrong */ |
| 3982 |
libxl__qmp_cleanup(gc, ddms->domid); |
| 3983 |
|
| 3984 |
ddms->callback(egc, ddms, ddms->rc); |
| 3985 |
} |
| 3986 |
|
| 3987 |
/* Return 0 if no dm needed, 1 if needed and <0 if error. */ |
| 3988 |
int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config) |
| 3989 |
{ |
| 3990 |
int idx, i, ret, num; |
| 3991 |
uint32_t domid; |
| 3992 |
const libxl__device_type *dt; |
| 3993 |
|
| 3994 |
ret = libxl__get_domid(gc, &domid); |
| 3995 |
if (ret) { |
| 3996 |
LOG(ERROR, "unable to get domain id"); |
| 3997 |
goto out; |
| 3998 |
} |
| 3999 |
|
| 4000 |
if (d_config->num_vfbs > 0) { |
| 4001 |
ret = 1; |
| 4002 |
goto out; |
| 4003 |
} |
| 4004 |
|
| 4005 |
for (idx = 0;; idx++) { |
| 4006 |
dt = device_type_tbl[idx]; |
| 4007 |
if (!dt) |
| 4008 |
break; |
| 4009 |
|
| 4010 |
num = *libxl__device_type_get_num(dt, d_config); |
| 4011 |
if (!dt->dm_needed || !num) |
| 4012 |
continue; |
| 4013 |
|
| 4014 |
for (i = 0; i < num; i++) { |
| 4015 |
if (dt->dm_needed(libxl__device_type_get_elem(dt, d_config, i), |
| 4016 |
domid)) { |
| 4017 |
ret = 1; |
| 4018 |
goto out; |
| 4019 |
} |
| 4020 |
} |
| 4021 |
} |
| 4022 |
|
| 4023 |
for (i = 0; i < d_config->num_channels; i++) { |
| 4024 |
if (d_config->channels[i].backend_domid == domid) { |
| 4025 |
/* xenconsoled is limited to the first console only. |
| 4026 |
Until this restriction is removed we must use qemu for |
| 4027 |
secondary consoles which includes all channels. */ |
| 4028 |
ret = 1; |
| 4029 |
goto out; |
| 4030 |
} |
| 4031 |
} |
| 4032 |
|
| 4033 |
out: |
| 4034 |
return ret; |
| 4035 |
} |
| 4036 |
|
| 4037 |
int libxl__dm_active(libxl__gc *gc, uint32_t domid) |
| 4038 |
{ |
| 4039 |
char *pid, *dm_domid, *path; |
| 4040 |
|
| 4041 |
path = GCSPRINTF("/local/domain/%d/image/device-model-pid", domid); |
| 4042 |
pid = libxl__xs_read(gc, XBT_NULL, path); |
| 4043 |
|
| 4044 |
if (pid) |
| 4045 |
return true; |
| 4046 |
|
| 4047 |
path = GCSPRINTF("/local/domain/%d/image/device-model-domid", domid); |
| 4048 |
dm_domid = libxl__xs_read(gc, XBT_NULL, path); |
| 4049 |
|
| 4050 |
return dm_domid != NULL; |
| 4051 |
} |
| 4052 |
|
| 4053 |
int libxl__dm_check_start(libxl__gc *gc, libxl_domain_config *d_config, |
| 4054 |
uint32_t domid) |
| 4055 |
{ |
| 4056 |
int rc; |
| 4057 |
|
| 4058 |
if (libxl__dm_active(gc, domid)) |
| 4059 |
return 0; |
| 4060 |
|
| 4061 |
rc = libxl__need_xenpv_qemu(gc, d_config); |
| 4062 |
if (rc < 0) |
| 4063 |
goto out; |
| 4064 |
|
| 4065 |
if (!rc) |
| 4066 |
return 0; |
| 4067 |
|
| 4068 |
LOGD(ERROR, domid, "device model required but not running"); |
| 4069 |
rc = ERROR_FAIL; |
| 4070 |
|
| 4071 |
out: |
| 4072 |
return rc; |
| 4073 |
} |
| 4074 |
|
| 4075 |
/* |
| 4076 |
* Local variables: |
| 4077 |
* mode: C |
| 4078 |
* c-basic-offset: 4 |
| 4079 |
* indent-tabs-mode: nil |
| 4080 |
* End: |
| 4081 |
*/ |