F #7331: Add support for memory encryption

This commit adds support for memory encryption. It includes:

* A new VM template attribute to set memory encryption
* A host probe to gather the memory extensions available (SEV,
  SEV-ES...)
* When selected AUTOMATIC_REQUIREMENTS include memory encryption as a
  requirement
* Domain XML generation has been updated to include memory encryption
  attributes
* A kvm.conf attribute MEMORY_TUNE_OFFSET to add an additional memory
  offset for encryption.

Author: MarioRobres <mrobres@opennebula.io>
Author: Daniel Clavijo Coca <dclavijo@opennebula.io>
This commit is contained in:
Ruben S. Montero committed 2025-12-17 09:46:23 +01:00
1 parent af892bbd9a
commit bebf710f8a
11 files changed
+253 -3

No files matched your search

@@ -134,9 +134,14 @@ def hostname
"HOSTNAME=#{ARGV[2]}\n"
end
def memory_encryption
"MEMORY_ENCRYPTION=\"SEV\"\n"
end
result = ''
result << system
result << memory_encryption
result << hostname
result << topology(2, 8, [2048, 1048576], 4, 16777216)
result << pci
@@ -0,0 +1,53 @@
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'open3'
require 'rexml/document'
cmd = 'virsh -c qemu:///system domcapabilities'
domcapabilities, _e, s = Open3.capture3(cmd)
exit 0 unless s.success?
begin
domcap_xml = REXML::Document.new(domcapabilities)
domcap_xml = domcap_xml.root
features = REXML::XPath.first(domcap_xml, '/domainCapabilities/features')
memory_encryption = 'NONE'
if (sev = features.elements['sev'])
if sev['supported'] == 'yes'
if (snp = sev.elements['sevsnp']) && snp['supported'] == 'yes'
memory_encryption = 'SEV-SNP'
else
es_guests = sev.elements['maxESGuests'].text
memory_encryption = es_guests == '0' ? 'SEV' : 'SEV-ES'
end
end
elsif (tdx = features.elements['tdx'])
memory_encryption = 'TDX' if tdx['supported'] == 'yes'
end
rescue StandardError
puts "MEMORY_ENCRYPTION=NONE"
exit 0
end
puts "MEMORY_ENCRYPTION=#{memory_encryption}"
+43
View File
@@ -1063,6 +1063,22 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
goto error_os;
}
// ------------------------------------------------------------------------
// Parse TPM attribute
// ------------------------------------------------------------------------
if (parse_tpm(error_str) != 0)
{
goto error_common;
}
// ------------------------------------------------------------------------
// Parse MEMORY_ENCRYPTION attribute
// ------------------------------------------------------------------------
if (parse_memory_encryption(error_str) != 0)
{
goto error_common;
}
// ------------------------------------------------------------------------
// Check the CPU Model attribute
// ------------------------------------------------------------------------
@@ -1765,6 +1781,33 @@ int VirtualMachine::automatic_requirements(set<int>& cluster_ids,
}
}
const VectorAttribute * memory_encryption = obj_template->get("MEMORY_ENCRYPTION");
if (memory_encryption != nullptr)
{
std::string type = memory_encryption->vector_value("TYPE");
if (!type.empty())
{
std::string requirement;
if (type == "SEV")
{
requirement = "SEV*";
}
else if (type == "SEV-ES")
{
requirement = "SEV-*";
}
else // SEV-SNP, TDX
{
requirement = type;
}
oss << " & (MEMORY_ENCRYPTION = \"" << requirement << "\")";
}
}
obj_template->add("AUTOMATIC_REQUIREMENTS", oss.str());
oss.str("");
+62 -2
View File
@@ -657,8 +657,7 @@ void VirtualMachine::parse_well_known_attributes()
"TOPOLOGY",
"NUMA_NODE",
"HYPERV_OPTIONS",
"SPICE_OPTIONS",
"TPM"
"SPICE_OPTIONS"
};
for (auto it = names.begin(); it != names.end() ; ++it)
@@ -816,6 +815,67 @@ int VirtualMachine::parse_cpu_model(Template * tmpl)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_vector_attribute(const std::string& name,
const std::vector<std::string>& required_keys,
std::string& error)
{
std::vector<VectorAttribute*> v_attr;
bool fail = false;
user_obj_template->remove(name, v_attr);
for (auto& attr_ptr : v_attr)
{
for (const auto& key : required_keys)
{
if (attr_ptr->vector_value(key).empty())
{
error = name + " attribute is missing required key: " + key;
fail = true;
break;
}
}
if ( fail ) break;
}
if (fail)
{
for (VectorAttribute* p : v_attr)
{
delete p;
}
return -1;
}
for (VectorAttribute* attr_ptr : v_attr)
{
obj_template->set(attr_ptr);
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_tpm(std::string& error)
{
return parse_vector_attribute("TPM", {"MODEL"}, error);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_memory_encryption(std::string& error)
{
return parse_vector_attribute("MEMORY_ENCRYPTION", {"TYPE"}, error);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_topology(Template * tmpl, std::string &error)
{
/**
+48
View File
@@ -655,6 +655,8 @@ int LibVirtDriver::deployment_description_kvm(
string virtio_scsi_queues;
string hyperv_options;
vector<const VectorAttribute *> memory_encryption;
vector<const VectorAttribute *> raw;
string default_raw;
string data;
@@ -2442,6 +2444,52 @@ int LibVirtDriver::deployment_description_kvm(
<< "\t</devices>" << endl;
}
// ------------------------------------------------------------------------
// Memory encryption
// ------------------------------------------------------------------------
num = vm->get_template_attribute("MEMORY_ENCRYPTION", memory_encryption);
for (int i = 0; i < num; i++)
{
type = memory_encryption[i]->vector_value("TYPE");
// AMD SEV/SEV-ES
if (type.find("SEV") != string::npos && !one_util::icasecmp(type, "SEV-SNP"))
{
// https://libvirt.org/formatdomain.html#launch-security.
// Setup policy bitmask, debug disallowed, key_sharing_disallowed
uint32_t policy = POLICY_DEBUG_DISALLOWED | POLICY_KEY_SHARING_DISALLOWED;
if (one_util::icasecmp(type, "SEV-ES"))
{
policy |= POLICY_SEV_ES_REQUIRED;
}
file << "\t<launchSecurity type='sev'>" << endl
<< "\t\t<policy>0x"
<< hex << setw(8) << setfill('0') << policy
<< "</policy>" << endl
<< "\t</launchSecurity>" << endl;
// Prevent qemu failing to allocate memory
int mem_tune_offset = 256;
get_attribute(vm, nullptr, nullptr, "MEMORY_TUNE_OFFSET", mem_tune_offset);
file << "\t<memtune>\n"
<< "\t\t<hard_limit unit='MiB'>"
<< (memory + mem_tune_offset)
<< "</hard_limit>\n"
<< "\t</memtune>\n";
}
else // SEV-SNP, Intel TDX...
{
vm->log("VMM", Log::WARNING,
"Memory encryption " + type + " is not supported or undefined, ignored.");
}
}
// ------------------------------------------------------------------------
// Raw KVM attributes
// ------------------------------------------------------------------------
@@ -76,6 +76,7 @@
# * HYPERV_OPTIONS: options used for FEATURES = [ HYPERV = yes ]
# * HYPERV_TIMERS: timers added when HYPERV is set to yes in FEATURES
# * SPICE_OPTIONS
# * MEMORY_TUNE_OFFSET: Offset in MiB to add to VM memory for memory encryption
#
# The following attributes can be overridden at Cluster and Host level,
# but not within individual VM configuration:
@@ -174,6 +175,8 @@ SPICE_OPTIONS = "
<redirdev bus='usb' type='spicevmc'/>
<redirdev bus='usb' type='spicevmc'/>"
MEMORY_TUNE_OFFSET = 256
Q35_ROOT_PORTS = 16
Q35_NUMA_PCIE = "YES"
-1
View File
@@ -107,7 +107,6 @@ if [ -d "$DEP_FILE_LOCATION/tpm" ]; then
(sudo -l | grep -q vtpm_setup) && sudo /var/tmp/one/vtpm_setup recover "$DOM_UUID" "$DEP_FILE_LOCATION"
fi
DATA=`virsh --connect $LIBVIRT_URI create $DEP_FILE`
if [ "x$?" != "x0" ]; then