all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Reiter <s.reiter@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC qemu-server 3/9] api: add /cpu/model/* get endpoint
Date: Thu, 28 Oct 2021 13:41:44 +0200	[thread overview]
Message-ID: <20211028114150.3245864-4-s.reiter@proxmox.com> (raw)
In-Reply-To: <20211028114150.3245864-1-s.reiter@proxmox.com>

For custom models, this returns the saved configuration, for default
models it returns the cputype, vendor and a list of flags that the guest
OS will see when running with this CPU type (using the -list-flags QEMU
parameter).

For custom models it also includes the digest of the whole config, in
case someone retrieved it to modify it with a PUT call afterwards.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
---
 PVE/API2/Qemu/CPU.pm        | 76 ++++++++++++++++++++++++++++++++++++-
 PVE/QemuServer/CPUConfig.pm |  7 ++++
 2 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Qemu/CPU.pm b/PVE/API2/Qemu/CPU.pm
index 610ffb9..9cc1d77 100644
--- a/PVE/API2/Qemu/CPU.pm
+++ b/PVE/API2/Qemu/CPU.pm
@@ -5,8 +5,10 @@ use warnings;
 
 use PVE::RESTHandler;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::INotify;
 use PVE::QemuServer;
 use PVE::QemuServer::CPUConfig;
+use PVE::Tools qw(run_command get_host_arch);
 
 use base qw(PVE::RESTHandler);
 
@@ -49,7 +51,7 @@ __PACKAGE__->register_method({
 		},
 	    },
 	},
-	links => [ { rel => 'child', href => '{name}' } ],
+	links => [ { rel => 'child', href => 'model/{name}' } ],
     },
     code => sub {
 	my $rpcenv = PVE::RPCEnvironment::get();
@@ -144,4 +146,76 @@ __PACKAGE__->register_method({
 	return $retval;
     }});
 
+__PACKAGE__->register_method({
+    name => 'info',
+    path => 'model/{cputype}',
+    method => 'GET',
+    description => 'Retrieve details about a specific CPU model.',
+    permissions => {
+	check => [ 'perm', '/nodes', ['Sys.Audit'] ],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    cputype => {
+		type => 'string',
+		description => "Name of the CPU model to query. Prefix with"
+			     . " '-custom' for custom models.",
+	    },
+	},
+    },
+    returns => {
+	type => 'object',
+	properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({
+	    vendor => {
+		type => 'string',
+		description => 'The CPU vendor reported to the guest OS. Only'
+			    . ' relevant for default models.',
+		optional => 1,
+	    },
+	    digest => get_standard_option('pve-config-digest'),
+	}),
+    },
+    code => sub {
+	my ($param) = @_;
+	my $cputype = $param->{cputype};
+
+	if ($cputype =~ m/^custom-/) {
+	    my $conf = PVE::QemuServer::CPUConfig::load_custom_model_conf();
+	    my $digest = $conf->{digest};
+	    my $retval = PVE::QemuServer::CPUConfig::get_custom_model($cputype);
+	    $retval->{digest} = $digest;
+	    return $retval;
+	}
+
+	# this correctly errors in case $cputype is unknown
+	my $vendor = PVE::QemuServer::CPUConfig::get_cpu_vendor($cputype);
+
+	my $retval = {
+	    cputype => $cputype,
+	    vendor => $vendor,
+	};
+
+	# for built-in models return all flags that QEMU assigns to this model
+	# by default (note that these flags might still differ very slightly
+	# with certain machine versions, but since this output is only intended
+	# for assisting a user in a GUI environment this should be fine)
+	my @flags;
+	my $qemu_binary = PVE::QemuServer::get_command_for_arch(get_host_arch());
+	my $retcode = run_command([$qemu_binary, '-list-flags', '-cpu', $cputype],
+	    outfunc => sub { @flags = split(/\s/, shift); }, noerr => 1);
+
+	if ($retcode || !@flags) {
+	    my $nodename = PVE::INotify::nodename();
+	    warn "warning: could not retrieve QEMU flags for built-in cpu type"
+	       . " '$cputype'. Make sure node '$nodename' has the latest"
+	       . " pve-qemu-kvm.\n";
+	} else {
+	    $retval->{flags} = join(";", map {"+$_"} @flags);
+	}
+
+	return $retval;
+    }});
+
 1;
diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index b9981c8..9be8022 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -320,6 +320,13 @@ sub is_custom_model {
     return $cputype =~ m/^custom-/;
 }
 
+sub get_cpu_vendor {
+    my ($cputype) = @_;
+    my $retval = $cpu_vendor_list->{$cputype} or
+	die "Built-in CPU type '$cputype' does not exist\n";
+    return $retval;
+}
+
 # Use this to get a single model in the format described by $cpu_fmt.
 # Allows names with and without custom- prefix.
 sub get_custom_model {
-- 
2.30.2





  parent reply	other threads:[~2021-10-28 11:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-28 11:41 [pve-devel] [RFC 0/9] Unfinished: Custom CPU type API and GUI Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC pve-qemu 1/9] Add -list-flags command line option Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC qemu-server 2/9] api: add recognized-flags and supported-flags endpoints Stefan Reiter
2021-10-28 11:41 ` Stefan Reiter [this message]
2021-10-28 11:41 ` [pve-devel] [RFC qemu-server 4/9] api: add /cpu/model/* get/create/delete/update endpoints Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC manager 5/9] gui: VMCPUFlagSelector: fix unknownFlags behaviour Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC manager 6/9] gui: CPUModelSelector: fix dirty state on default Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC manager 7/9] gui: CPUModelSelector: add 'allowCustom' Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC manager 8/9] gui: add basic custom CPU model editor Stefan Reiter
2021-10-28 11:41 ` [pve-devel] [RFC manager 9/9] Initial attempt at CPU flag editor for custom models Stefan Reiter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211028114150.3245864-4-s.reiter@proxmox.com \
    --to=s.reiter@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal