From nobody Mon Feb 9 19:55:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1606999100; cv=none; d=zohomail.com; s=zohoarc; b=Ci8iqz6bBc5m4g0t3BPoi5az/4WPBIidAZJVOwvVAATKKBnaBt7VfWJUPyKX1dMacoqXVCZqP4kIBCQkYYZgXVzMMrfFYPKS5rORJz0xs18FRezT0/jWiNGeOXR9qztjgzv3DiGc5ZeHM52p/fC0w0BXJcfnaLB07+vuczlheNQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606999100; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vShzL2LjSSDuLOMScGvFa8s1wS9KDVi1idEXaIVG8cs=; b=V88qSqlwQK42mMKbL6NVym0inOLmhz4qXdR4ilCyPBgK/BjTIzPWdmhEtq0+Uj+zxxw00oe8Dcz2w8qkXFT5lHGoZPh1kCXtd2c+6EeQcL7XtacyT4hNj5xsQHUWbsfg40xtfMfSzLBiZSC0j8quNtqektVx3apmYOgkHH4n7f8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1606999100227244.4724739302277; Thu, 3 Dec 2020 04:38:20 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-77-4W9ZynDyMCiGdMFn15z4fA-1; Thu, 03 Dec 2020 07:38:16 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EDA8E1007474; Thu, 3 Dec 2020 12:38:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CB5B05D735; Thu, 3 Dec 2020 12:38:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9944C5003E; Thu, 3 Dec 2020 12:38:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0B3CbhFO003491 for ; Thu, 3 Dec 2020 07:37:43 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6E5A25C1D0; Thu, 3 Dec 2020 12:37:43 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id E3B775C1C2 for ; Thu, 3 Dec 2020 12:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606999099; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=vShzL2LjSSDuLOMScGvFa8s1wS9KDVi1idEXaIVG8cs=; b=C0WRz8saK7Fjfz3x0fP8G4JnRHC6TEs/U2FzGrx/VZ92h2JwD6tqeIiogVFyMmcu+0zG7Y isOlRnV/rHLbZprEUisppTEcTeyEF/xwf4ORt5gKRrBYHckYIa/L7/rEpzfzbJUx1ZO08z IqN0BQdlXJP2qTHWlVih3pvbra/uiIc= X-MC-Unique: 4W9ZynDyMCiGdMFn15z4fA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 26/27] virsh: Introduce --virtio to setmem Date: Thu, 3 Dec 2020 13:36:29 +0100 Message-Id: <34aacfbdd6c4afca54ccab6787929012581e8ef8.1606998426.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Provide an user friendly way of modifying 'requested-size' of a virtio-mem device. New --node and --alias arguments are introduced but they are needed only if it is not clear which virtio-mem does user mean. Signed-off-by: Michal Privoznik --- docs/manpages/virsh.rst | 6 ++ tools/virsh-domain.c | 126 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 10 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 9ef6b68422..c3c8c27a18 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -4098,6 +4098,7 @@ setmem :: =20 setmem domain size [[--config] [--live] | [--current]] + [--virtio [--node NODE] | [--alias ALIAS]] =20 Change the memory allocation for a guest domain. If *--live* is specified, perform a memory balloon of a running guest. @@ -4107,6 +4108,11 @@ If *--current* is specified, it is equivalent to eit= her *--live* or Both *--live* and *--config* flags may be given, but *--current* is exclusive. If no flag is specified, behavior is different depending on hypervisor. +If *--virtio* is specified then instead of changing the memory allocation = for +whole domain the individual memory device with virtio model is changed. If +there is more than one such memory device then *--node* or *--alias* must = be +used to specify the device uniquely. *NODE* refers to the guest NUMA node = to +which the memory device is attached to and *ALIAS* is the device alias. =20 *size* is a scaled integer (see ``NOTES`` above); it defaults to kibibytes (blocks of 1024 bytes) unless you provide a suffix (and the older option diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index eeeeaa8639..7d8546d806 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -8992,12 +8992,101 @@ static const vshCmdOptDef opts_setmem[] =3D { .flags =3D VSH_OFLAG_REQ, .help =3D N_("new memory size, as scaled integer (default KiB)") }, + {.name =3D "virtio", + .type =3D VSH_OT_BOOL, + .help =3D N_("modify virtio memory instead of top level domain memory= ") + }, + {.name =3D "node", + .type =3D VSH_OT_INT, + .help =3D N_("guest numa node"), + }, + {.name =3D "alias", + .type =3D VSH_OT_STRING, + .help =3D N_("memory device alias"), + }, VIRSH_COMMON_OPT_DOMAIN_CONFIG, VIRSH_COMMON_OPT_DOMAIN_LIVE, VIRSH_COMMON_OPT_DOMAIN_CURRENT, {.name =3D NULL} }; =20 + +static int +virshDomainSetmemVirtio(vshControl *ctl, + virDomainPtr dom, + unsigned long kibibytes, + int numaNode, + const char *alias, + unsigned int flags) +{ + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + g_autofree char *xpath =3D NULL; + int nmems; + g_autofree xmlNodePtr *mems =3D NULL; + xmlNodePtr requestedSizeNode =3D NULL; + g_autofree char *kibibytesStr =3D NULL; + g_autoptr(xmlBuffer) xmlbuf =3D NULL; + unsigned int domainXMLFlags =3D 0; + const char *updatedMemoryXML =3D NULL; + + if (flags & VIR_DOMAIN_AFFECT_CONFIG) + domainXMLFlags |=3D VIR_DOMAIN_XML_INACTIVE; + + if (virshDomainGetXMLFromDom(ctl, dom, domainXMLFlags, &doc, &ctxt) < = 0) + return -1; + + if (alias) { + xpath =3D g_strdup_printf("/domain/devices/memory[./alias/@name=3D= '%s']", alias); + } else if (numaNode >=3D 0) { + xpath =3D g_strdup_printf("/domain/devices/memory[./target/node/te= xt()=3D%d]", numaNode); + } else { + xpath =3D g_strdup("/domain/devices/memory[@model=3D'virtio' and n= ot(./source/pmem)]"); + } + + nmems =3D virXPathNodeSet(xpath, ctxt, &mems); + if (nmems < 0) { + vshSaveLibvirtError(); + return -1; + } else if (nmems =3D=3D 0) { + vshError(ctl, _("no virtio-mem device found")); + return -1; + } else if (nmems > 1) { + vshError(ctl, _("multiple virtio-mem devices found")); + return -1; + } + + ctxt->node =3D mems[0]; + + requestedSizeNode =3D virXPathNode("./target/requested", ctxt); + + if (!requestedSizeNode) { + vshError(ctl, _("virtio-mem device is missing ")); + return -1; + } + + kibibytesStr =3D g_strdup_printf("%lu", kibibytes); + xmlNodeSetContent(requestedSizeNode, BAD_CAST kibibytesStr); + + if (!(xmlbuf =3D xmlBufferCreate())) { + vshError(ctl, _("unable to allocate XML buffer")); + return -1; + } + + if (xmlNodeDump(xmlbuf, doc, mems[0], 0, 1) < 0) { + vshError(ctl, _("unable to format new node")); + return -1; + } + + updatedMemoryXML =3D (const char *)xmlBufferContent(xmlbuf); + + if (virDomainUpdateDeviceFlags(dom, updatedMemoryXML, flags) < 0) + return -1; + + return 0; +} + + static bool cmdSetmem(vshControl *ctl, const vshCmd *cmd) { @@ -9005,7 +9094,7 @@ cmdSetmem(vshControl *ctl, const vshCmd *cmd) unsigned long long bytes =3D 0; unsigned long long max; unsigned long kibibytes =3D 0; - bool ret =3D true; + bool ret =3D false; bool config =3D vshCommandOptBool(cmd, "config"); bool live =3D vshCommandOptBool(cmd, "live"); bool current =3D vshCommandOptBool(cmd, "current"); @@ -9013,6 +9102,7 @@ cmdSetmem(vshControl *ctl, const vshCmd *cmd) =20 VSH_EXCLUSIVE_OPTIONS_VAR(current, live); VSH_EXCLUSIVE_OPTIONS_VAR(current, config); + VSH_EXCLUSIVE_OPTIONS("node", "alias"); =20 if (config) flags |=3D VIR_DOMAIN_AFFECT_CONFIG; @@ -9028,20 +9118,36 @@ cmdSetmem(vshControl *ctl, const vshCmd *cmd) max =3D 1024ull * ULONG_MAX; else max =3D ULONG_MAX; - if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) { - virshDomainFree(dom); - return false; - } + if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) + goto cleanup; + kibibytes =3D VIR_DIV_UP(bytes, 1024); =20 - if (flags =3D=3D 0) { - if (virDomainSetMemory(dom, kibibytes) !=3D 0) - ret =3D false; + if (vshCommandOptBool(cmd, "virtio")) { + int numaNode =3D -1; + const char *alias =3D NULL; + + if (vshCommandOptInt(ctl, cmd, "node", &numaNode) < 0) + goto cleanup; + + if (vshCommandOptStringReq(ctl, cmd, "alias", &alias) < 0) + goto cleanup; + + if (virshDomainSetmemVirtio(ctl, dom, kibibytes, + numaNode, alias, flags) < 0) + goto cleanup; } else { - if (virDomainSetMemoryFlags(dom, kibibytes, flags) < 0) - ret =3D false; + if (flags =3D=3D 0) { + if (virDomainSetMemory(dom, kibibytes) !=3D 0) + goto cleanup; + } else { + if (virDomainSetMemoryFlags(dom, kibibytes, flags) < 0) + goto cleanup; + } } =20 + ret =3D true; + cleanup: virshDomainFree(dom); return ret; } --=20 2.26.2