libxl: allow creation of domains with a specified or random domid

This patch adds a 'domid' field to libxl_domain_create_info and then
modifies libxl__domain_make() to have Xen use that value if it is valid.
If the domid value is invalid then Xen will choose the domid, as before,
unless the value is the new special RANDOM_DOMID value added to the API.
This value instructs libxl__domain_make() to choose a random domid value
for Xen to use.

If Xen determines that a domid specified to or chosen by
libxl__domain_make() co-incides with an existing domain then the create
operation will fail. In this case, if RANDOM_DOMID was specified to
libxl__domain_make() then a new random value will be chosen and the create
operation will be re-tried, otherwise libxl__domain_make() will fail.

After Xen has successfully created a new domain, libxl__domain_make() will
check whether its domid matches any recently used domid values. If it does
then the domain will be destroyed. If the domid used in creation was
specified to libxl__domain_make() then it will fail at this point,
otherwise the create operation will be re-tried with either a new random
or Xen-selected domid value.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
This commit is contained in:
Paul Durrant committed 2020-02-24 17:17:35 +00:00
1 parent 69ad573c2b
commit 4a3a25678d
3 files changed
+81 -5

No files matched your search

+9
View File
@@ -1268,6 +1268,14 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
*/
#define LIBXL_HAVE_DOMAIN_NEED_MEMORY_CONFIG
/*
* LIBXL_HAVE_CREATEINFO_DOMID
*
* libxl_domain_create_new() and libxl_domain_create_restore() will use
* a domid specified in libxl_domain_create_info().
*/
#define LIBXL_HAVE_CREATEINFO_DOMID
typedef char **libxl_string_list;
void libxl_string_list_dispose(libxl_string_list *sl);
int libxl_string_list_length(const libxl_string_list *sl);
@@ -1528,6 +1536,7 @@ int libxl_ctx_free(libxl_ctx *ctx /* 0 is OK */);
/* domain related functions */
#define INVALID_DOMID ~0
#define RANDOM_DOMID (INVALID_DOMID - 1)
/* If the result is ERROR_ABORTED, the domain may or may not exist
* (in a half-created state). *domid will be valid and will be the
+71 -5
View File
@@ -600,11 +600,77 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,
goto out;
}
ret = xc_domain_create(ctx->xch, domid, &create);
if (ret < 0) {
LOGED(ERROR, *domid, "domain creation fail");
rc = ERROR_FAIL;
goto out;
for (;;) {
uint32_t local_domid;
bool recent;
if (info->domid == RANDOM_DOMID) {
uint16_t v;
ret = libxl__random_bytes(gc, (void *)&v, sizeof(v));
if (ret < 0)
break;
v &= DOMID_MASK;
if (!libxl_domid_valid_guest(v))
continue;
local_domid = v;
} else {
local_domid = info->domid; /* May not be valid */
}
ret = xc_domain_create(ctx->xch, &local_domid, &create);
if (ret < 0) {
/*
* If we generated a random domid and creation failed
* because that domid already exists then simply try
* again.
*/
if (errno == EEXIST && info->domid == RANDOM_DOMID)
continue;
LOGED(ERROR, local_domid, "domain creation fail");
rc = ERROR_FAIL;
goto out;
}
/* A new domain now exists */
*domid = local_domid;
rc = libxl__is_domid_recent(gc, local_domid, &recent);
if (rc)
goto out;
/* The domid is not recent, so we're done */
if (!recent)
break;
/*
* If the domid was specified then there's no point in
* trying again.
*/
if (libxl_domid_valid_guest(info->domid)) {
LOGED(ERROR, local_domid, "domain id recently used");
rc = ERROR_FAIL;
goto out;
}
/*
* The domain is recent and so cannot be used. Clear domid
* here since, if xc_domain_destroy() fails below there is
* little point calling it again in the error path.
*/
*domid = INVALID_DOMID;
ret = xc_domain_destroy(ctx->xch, local_domid);
if (ret < 0) {
LOGED(ERROR, local_domid, "domain destroy fail");
rc = ERROR_FAIL;
goto out;
}
/* The domain was successfully destroyed, so we can try again */
}
rc = libxl__arch_domain_save_config(gc, d_config, state, &create);
+1
View File
@@ -409,6 +409,7 @@ libxl_domain_create_info = Struct("domain_create_info",[
("ssidref", uint32),
("ssid_label", string),
("name", string),
("domid", libxl_domid),
("uuid", libxl_uuid),
("xsdata", libxl_key_value_list),
("platformdata", libxl_key_value_list),
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.