tools: rework VM Generation ID
v7 cover tools: rework VM Generation ID
This series reworks the VM Generation ID to a) conform to the published spec from Microsoft; b) simplify the save/restore code; and
c) extend the libxl API to allow toolstacks to use this feature.
The VM Generation ID must be regenerated with a new random ID after certain VM operations. Since xl lacks infrastructure for tracking the life-cycle of snapshots and clones (etc), the safe option of always using a new generation ID is used.
You can download the spec from:
http://www.microsoft.com/en-us/download/details.aspx?id=30707
Patch status ([A]cked, [M]odified, [N]ew):
M libxl: allow a generation ID to be specified at domain A xl: generate a new random VM generation ID if requested A docs: update docs for the ~/platform/generation-id key
Changes in v7:
- Fix Ocaml binding for libxl_ms_vm_genid type.
Changes in v6:
- Add xc_hvm_param_get/set() instead of changing existing functions.
- Use new libxl_ms_vm_genid type.
- xl.cfg option is now 'ms_vm_genid = "generate"|"none"'.
Changes in v5:
- Rebase on staging to fix some conflicts.
- Add libxl__random_bytes()
- Rename field to ms_vm_genid.
- Toolstacks can generate a fully random generation ID with libxl_ms_vm_genid_generate().
Changes in v4:
- Allow xc_get/get_hvm_params() to get/set 64-bit values.
- Const-ify libxl_uuid_*() calls.
- Minor tweaks to libxl internals (gc as param, use GCSPRINTF()).
Changes in v3:
- Specify generation ID in libxl_domain_build_info instead of having to call a function to set it.
- Improve docs.
Changes in v2:
- Use libxl_uuid for the generation ID.
- Add "generation_id" option to xl domain configuration file and use this to set a random generation ID every time.
David
PATCH v7 1/3 libxl: allow a generation ID to be specified at domain creation
Toolstacks may specify a VM generation ID using the u.hvm.ms_vm_genid field in the libxl_domain_build_info structure, when creating a domain.
The toolstack is responsible for providing the correct generation ID according to the Microsoft specification (e.g., generating new random ones with libxl_ms_vm_genid_generate() as appropriate when restoring).
Although the specification requires that a ACPI Notify event is raised if the generation ID is changed, the generation ID is never changed when the domain is in a state to receive such an event (it's either newly created or suspended).
--- tools/libxl/Makefile | 1 + tools/libxl/gentest.py | 2 +- tools/libxl/libxl.h | 13 ++++ tools/libxl/libxl_dom.c | 10 +++ tools/libxl/libxl_genid.c | 111 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 6 ++ tools/libxl/libxl_json.c | 39 ++++++++++++ tools/libxl/libxl_json.h | 1 + tools/libxl/libxl_types.idl | 3 + tools/ocaml/libs/xl/genwrap.py | 1 + tools/ocaml/libs/xl/xenlight_stubs.c | 25 ++++++++ 11 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 tools/libxl/libxl_genid.c
tools/libxl/Makefile +1 -0
@@ -81,6 +81,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_json.o libxl_aoutils.o libxl_numa.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) +LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o LIBXL_TESTS += timedereg
tools/libxl/gentest.py +1 -1
@@ -60,7 +60,7 @@ def gen_rand_init(ty, v, indent = " ", parent = None): s += "%s(%s);\n" % (ty.rand_init, ty.pass_arg(v, isref=parent is None, passby=idl.PASS_BY_REFERENCE)) - elif ty.typename in ["libxl_uuid", "libxl_mac", "libxl_hwcap"]: + elif ty.typename in ["libxl_uuid", "libxl_mac", "libxl_hwcap", "libxl_ms_vm_genid"]: s += "rand_bytes((uint8_t *)%s, sizeof(*%s));\n" % (v,v) elif ty.typename in ["libxl_domid", "libxl_devid"] or isinstance(ty, idl.Number): s += "%s = rand() %% (sizeof(%s)*8);\n" % \
tools/libxl/libxl.h +13 -0
@@ -104,6 +104,11 @@#define LIBXL_HAVE_BUILDINFO_EVENT_CHANNELS 1/* +* libxl_domain_build_info has the u.hvm.ms_vm_genid field. +*/ +#define LIBXL_HAVE_BUILDINFO_HVM_MS_VM_GENID 1 + +/* * LIBXL_HAVE_VCPUINFO_SOFT_AFFINITY indicates that a 'cpumap_soft' * field (of libxl_bitmap type) is present in libxl_vcpuinfo, * containing the soft affinity of a vcpu.
@@ -639,6 +644,11 @@ const char *libxl_defbool_to_string(libxl_defbool b);#define LIBXL_TIMER_MODE_DEFAULT -1#define LIBXL_MEMKB_DEFAULT ~0ULL +#define LIBXL_MS_VM_GENID_LEN 16 +typedef struct { +uint8_t bytes[LIBXL_MS_VM_GENID_LEN ]; +}libxl_ms_vm_genid ; +#include "_libxl_types.h" const libxl_version_info *libxl_get_version_info (libxl_ctx *ctx);
@@ -1262,6 +1272,9 @@ int libxl_flask_getenforce(libxl_ctx *ctx);int libxl_flask_setenforce (libxl_ctx *ctx,int mode);int libxl_flask_loadpolicy (libxl_ctx *ctx,void *policy,uint32_t size); +int libxl_ms_vm_genid_generate (libxl_ctx *ctx,libxl_ms_vm_genid *id); +bool libxl_ms_vm_genid_is_zero (const libxl_ms_vm_genid *id); +/* misc */ /* Each of these sets or clears the flag according to whether the
tools/libxl/libxl_dom.c +10 -0
@@ -339,6 +339,16 @@ int libxl__build_post(libxl__gc *gc, uint32_t domid,if (info->cpuid !=NULL )libxl_cpuid_set (ctx, domid, info->cpuid); +if (info->type ==LIBXL_DOMAIN_TYPE_HVM + && !libxl_ms_vm_genid_is_zero (&info->u.hvm.ms_vm_genid)) { + rc =libxl__ms_vm_genid_set (gc, domid, + &info->u.hvm.ms_vm_genid); +if (rc) { +LOG (ERROR ,"Failed to set VM Generation ID" ); +return rc; + } + } + ents =libxl__calloc (gc,12 + (info->max_vcpus *2 ) +2 ,sizeof (char *)); ents[0 ] ="memory/static-max" ; ents[1 ] =GCSPRINTF ("%" PRId64, info->max_memkb);
tools/libxl/libxl_genid.c new file +111 -0
111 lines not on this page · raw message
tools/libxl/libxl_internal.h +6 -0
@@ -3086,6 +3086,10 @@ void libxl__numa_candidate_put_nodemap(libxl__gc *gc,libxl_bitmap_copy (CTX , &cndt->nodemap, nodemap); } +_hidden int libxl__ms_vm_genid_set(libxl__gc *gc, uint32_t domid, +const libxl_ms_vm_genid *id); + +/* Som handy macros for defbool type. */ #define LIBXL__DEFBOOL_DEFAULT (0)#define LIBXL__DEFBOOL_FALSE (-1)
@@ -3163,6 +3167,8 @@ int libxl_key_value_list_parse_json(libxl__gc *gc, libxl_key_value_list *p);int libxl_hwcap_parse_json (libxl__gc *gc,const libxl__json_object *o,libxl_hwcap *p); +int libxl_ms_vm_genid_parse_json (libxl__gc *gc,const libxl__json_object *o, +libxl_ms_vm_genid *p);int libxl__int_parse_json (libxl__gc *gc,const libxl__json_object *o,void *p); int libxl__uint8_parse_json(libxl__gc *gc, const libxl__json_object *o,
tools/libxl/libxl_json.c +39 -0
@@ -377,6 +377,45 @@ int libxl_hwcap_parse_json(libxl__gc *gc, const libxl__json_object *o,return 0 ; } +yajl_gen_status libxl_ms_vm_genid_gen_json (yajl_gen hand,libxl_ms_vm_genid *p) +{ +yajl_gen_status s; +int i; + + s =yajl_gen_array_open (hand); +if (s != yajl_gen_status_ok) +return s; + +for (i =0 ; i <LIBXL_MS_VM_GENID_LEN ; i++) { + s =yajl_gen_integer (hand, p->bytes[i]); +if (s != yajl_gen_status_ok) +return s; + } + +return yajl_gen_array_close (hand); +} + +int libxl_ms_vm_genid_parse_json (libxl__gc *gc,const libxl__json_object *o, +libxl_ms_vm_genid *p) +{ +unsigned int i; + +if (!libxl__json_object_is_array (o)) +return ERROR_FAIL ; + +for (i =0 ; i <LIBXL_MS_VM_GENID_LEN ; i++) { +const libxl__json_object *t; + + t =libxl__json_array_get (o, i); +if (!t || !libxl__json_object_is_integer (t)) +return ERROR_FAIL ; + + p->bytes[i] =libxl__json_object_get_integer (t); + } + +return 0 ; +} +yajl_gen_status libxl__string_gen_json (yajl_gen hand,const char *p) {
tools/libxl/libxl_json.h +1 -0
@@ -33,6 +33,7 @@ yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *p);yajl_gen_status libxl_key_value_list_gen_json (yajl_gen hand,libxl_key_value_list *p);yajl_gen_status libxl_hwcap_gen_json (yajl_gen hand,libxl_hwcap *p); +yajl_gen_status libxl_ms_vm_genid_gen_json (yajl_gen hand,libxl_ms_vm_genid *p);#include <_libxl_types_json.h>
tools/libxl/libxl_types.idl +3 -0
@@ -17,6 +17,7 @@ libxl_cpuid_policy_list = Builtin("cpuid_policy_list", dispose_fn="libxl_cpuid_d libxl_string_list = Builtin("string_list", dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE) libxl_key_value_list = Builtin("key_value_list", dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE) libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE) +libxl_ms_vm_genid = Builtin("ms_vm_genid", passby=PASS_BY_REFERENCE) # # Specific integer types
@@ -378,6 +379,8 @@ libxl_domain_build_info = Struct("domain_build_info",[ ("xen_platform_pci", libxl_defbool), ("usbdevice_list", libxl_string_list), ("vendor_device", libxl_vendor_device), + # See libxl_ms_vm_genid_generate() + ("ms_vm_genid", libxl_ms_vm_genid), ])), ("pv", Struct(None, [("kernel", string), ("slack_memkb", MemKB),
tools/ocaml/libs/xl/genwrap.py +1 -0
@@ -18,6 +18,7 @@ builtins = { "libxl_string_list": ("string list", "libxl_string_list_val(&%(c)s, %(o)s)", "Val_string_list(&%(c)s)"), "libxl_mac": ("int array", "Mac_val(&%(c)s, %(o)s)", "Val_mac(&%(c)s)"), "libxl_hwcap": ("int32 array", None, "Val_hwcap(&%(c)s)"), + "libxl_ms_vm_genid": ("int array", "Ms_vm_genid_val(&%(c)s, %(o)s)", "Val_ms_vm_genid(&%(c)s)"), # The following needs to be sorted out later "libxl_cpuid_policy_list": ("unit", "%(c)s = 0", "Val_unit"), }
tools/ocaml/libs/xl/xenlight_stubs.c +25 -0
@@ -370,6 +370,31 @@ static value Val_hwcap(libxl_hwcap *c_val)CAMLreturn (hwcap); } +static value Val_ms_vm_genid (libxl_ms_vm_genid *c_val) +{ +CAMLparam0 (); +CAMLlocal1 (v); +int i; + + v =caml_alloc_tuple (LIBXL_MS_VM_GENID_LEN ); + +for (i=0 ; i<LIBXL_MS_VM_GENID_LEN ; i++) +Store_field (v, i,Val_int (c_val->bytes[i])); + +CAMLreturn (v); +} + +static int Ms_vm_genid_val (libxl_ms_vm_genid *c_val,value v) +{ +CAMLparam1 (v); +int i; + +for (i=0 ; i<LIBXL_MS_VM_GENID_LEN ; i++) + c_val->bytes[i] =Int_val (Field (v, i)); + +CAMLreturn (0 ); +} +static value Val_string_option (const char *c_val) {CAMLparam0 ();
Signature · 1 line
1.7.10.4
PATCH v7 2/3 xl: generate a new random VM generation ID if requested
In reply to David VrabelIf the "generation_id" option is set in the domain configuration, generate and set a new random VM generation ID every time a domain is created or restored.
xl lacks the infrastructure to fully track the lifecycle of VM images as they are snapshotted and cloned (etc) so always using a new ID is the safe option and ensures that a new one will be used where it matters.
--- docs/man/xl.cfg.pod.5 | 29 +++++++++++++++++++++++++++++ tools/libxl/xl_cmdimpl.c | 15 +++++++++++++++ 2 files changed, 44 insertions(+)
docs/man/xl.cfg.pod.5 +29 -0
@@ -936,6 +936,35 @@ number of vendor defined SMBIOS structures (type 128 - 255). Since SMBIOS structures do not present their overall size, each entry in the file must be preceded by a 32b integer indicating the size of the next structure. +=item B<ms_vm_genid="OPTION"> + +Provide a VM generation ID to the guest. + +The VM generation ID as a 128-bit random number that a guest may use +to determine if the guest has been restored from an earlier snapshot +or cloned. + +This is required for Microsoft Windows Server 2012 (and later) domain +controllers. + +Valid options are: + +=over 4 + +=item B<"generate"> + +Generate a random VM generation ID every time the domain is created or +restored. + +=item B<"none"> + +Do not provide a VM generation ID. + +=back + +See also "Virtual Machine Generation ID" by Microsoft +(http://www.microsoft.com/en-us/download/details.aspx?id=30707). + =back =head3 Guest Virtual Time Controls
tools/libxl/xl_cmdimpl.c +15 -0
@@ -983,6 +983,21 @@ static void parse_config_data(const char *config_source, &b_info->u.hvm.smbios_firmware, 0);xlu_cfg_replace_string (config,"acpi_firmware" , &b_info->u.hvm.acpi_firmware,0 ); + +if (!xlu_cfg_get_string (config,"ms_vm_genid" , &buf,0 )) { +if (!strcmp (buf,"generate" )) { + e =libxl_ms_vm_genid_generate (ctx, &b_info->u.hvm.ms_vm_genid); +if (e) { +fprintf (stderr,"ERROR: failed to generate a VM Generation ID\n" ); +exit (1 ); + } + }else if (!strcmp (buf,"none" )) { + ; + }else { +fprintf (stderr,"ERROR: \"ms_vm_genid\" option must be \"generate\" or \"none\"\n" ); +exit (1 ); + } + }break ;case LIBXL_DOMAIN_TYPE_PV : {
Signature · 1 line
1.7.10.4
PATCH v7 3/3 docs: update docs for the ~/platform/generation-id key
In reply to David Vrabel--- docs/misc/xenstore-paths.markdown | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
docs/misc/xenstore-paths.markdown +16 -8
@@ -166,11 +166,6 @@ use the xenstore-based protocol instead (see ~/control/shutdown, below) even if the guest has advertised support for the event channel protocol. -#### ~/hvmloader/generation-id-address = ADDRESS [r,HVM,INTERNAL] - -The hexadecimal representation of the address of the domain's -"generation id". - #### ~/hvmloader/allow-memory-relocate = ("1"|"0") [HVM,INTERNAL] If the default low MMIO hole (below 4GiB) is not big enough for all
@@ -193,9 +188,22 @@ Various platform properties. #### ~/platform/generation-id = INTEGER ":" INTEGER [HVM,INTERNAL] -Two 64 bit values that represent the Windows Generation ID. -Is used by the BIOS initializer to get this value. -If not present or "0:0" (all zeroes) device will not be present to the machine. +The lower and upper 64-bit words of the 128-bit VM Generation ID. + +This key is used by hvmloader to create the ACPI VM Generation ID +device. It initialises a 16 octet region of guest memory with this +value. The guest physical address of this region is saved in the +HVM_PARAM_VM_GENERATION_ID_ADDR HVM parameter. + +If this key is not present, is empty, or is all-zeros ("0:0") then the +ACPI device is not created. + +When restoring a guest, the toolstack may (in certain circumstances) +need generate a new random generation ID and write it to guest memory +at the guest physical address in HVM_PARAM_VM_GENERATION_ID_ADDR. + +See Microsoft's "Virtual Machine Generation ID" specification for the +circumstances where the generation ID needs to be changed. ### Frontend device paths
Signature · 1 line
1.7.10.4
Quoted 15 lines · David Vrabel
This series reworks the VM Generation ID to a) conform to the published spec from Microsoft; b) simplify the save/restore code; and
c) extend the libxl API to allow toolstacks to use this feature.The VM Generation ID must be regenerated with a new random ID after certain VM operations. Since xl lacks infrastructure for tracking the life-cycle of snapshots and clones (etc), the safe option of always using a new generation ID is used.
You can download the spec from:
http://www.microsoft.com/en-us/download/details.aspx?id=30707
Patch status ([A]cked, [M]odified, [N]ew):
Quoted 7 lines
M libxl: allow a generation ID to be specified at domain A xl: generate a new random VM generation ID if requested A docs: update docs for the ~/platform/generation-id key
Changes in v7:
- Fix Ocaml binding for
libxl_ms_vm_genidtype.
That bit looked good to me. Acked + applied.