From nobody Fri May 3 07:15:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1576661345; cv=none; d=zohomail.com; s=zohoarc; b=AMEcy5YP9iWv4ZFxeZT/cVUmm7p6Ibi4nBnXF7J650ZsIOYzrZ4n7FmzBE1s0AMSyJhbK89FzDxEbocPa+TPo6/qdy36lTxsm8dYG8hTRNDgiaaLyMihPoT17Zau+Zo2BfbQ6PURXi4Or8wXwZuedQ1DWHE0dFVdVgK5QEoAfb8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576661345; h=Content-Type:Content-Transfer-Encoding:Cc: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=B9/AfgK9i6Q7pHBZFSINDRMFqgOY+HG6M5xvvaF3vKE=; b=RfkQnuxCKBTza3dcguV3QS0BGGvUDP5gZcumT3EsBckTn0w17+OU5PVGI9AB/wqaAnow13mKIjUaqU1B/xb34C1hecqJJYac/5XtC05OFo3Bw+Z3Ct1e7XSuRwmXJEsZpH2YbCvZazMFnCLi55px88Qo6M4ffb+wvXHeUzUu/Z0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1576661345437788.2671701253945; Wed, 18 Dec 2019 01:29:05 -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-389-0Uhgh-2UOqGYB3hXCqigpw-1; Wed, 18 Dec 2019 04:29:02 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E9D62800D5F; Wed, 18 Dec 2019 09:28:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 753AB7D8E1; Wed, 18 Dec 2019 09:28:56 +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 F3B3318089CD; Wed, 18 Dec 2019 09:28:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xBI9Sq0A029111 for ; Wed, 18 Dec 2019 04:28:52 -0500 Received: by smtp.corp.redhat.com (Postfix) id 8CFD8F4975; Wed, 18 Dec 2019 09:28:52 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 87E0DF4979 for ; Wed, 18 Dec 2019 09:28:50 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8C4D285A302 for ; Wed, 18 Dec 2019 09:28:50 +0000 (UTC) Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-438-quro_6Q0Nhuz3LrDYYYFWw-1; Wed, 18 Dec 2019 04:28:48 -0500 Received: by mail-qk1-f196.google.com with SMTP id d71so1370905qkc.0; Wed, 18 Dec 2019 01:28:48 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:2f19:a73a:b6c3:c24a:90f3]) by smtp.gmail.com with ESMTPSA id o187sm480850qkf.26.2019.12.18.01.28.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2019 01:28:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576661344; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=B9/AfgK9i6Q7pHBZFSINDRMFqgOY+HG6M5xvvaF3vKE=; b=X6asX0QLyd/srSsoy4RYOJ88YOvUjqkbcPmrAd+6Hmke/39U4cGlGK4odgeSz3xELa202e gPpG+FtVO4e1EecDa3S8gVJ5FFySRzsEC9aq4YaKMynp1Kt+l1fVPDXz7djj0J+dFo93xY GlKtsUK9BdQzGpD9ByXsLVOm6RfNty8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HDAXkr0nd7eTdBYzO4uTfCeTr6G8wtuQDiUYaA6UWLs=; b=kI6OlbkEyQvHJ/4ZQTqQEwrP0j7FVIAO941/7vKnuz23C0+p8R0JaWTdv1TOh2wp/S VT163rUhcnERaBQcuseIGwDdduK8BJHzAhIQEfM7C5tkCR2Ytk5JmP+JFtNGrAnGzG4r q//fR6WKzrHMeUxxXArNwoAE9sU5BG7xJ01b0WUqNtVQSRb6juyJfreVM5fG+mWMawYw u/FezZYsmjyQrqdaUbs7NJNyLP6kGvmCkDdo+jQQW3eZlKe3WA8yFk2PlDZ6ZO+vlZc2 Ac69Kl50TqYr5YjiuXXoMJmXtWAfmkh9BlO3w4+LI2kxRhJZ0PLUDhaGjjh7lFw2bwfS Dt6w== X-Gm-Message-State: APjAAAUuFmc1lHhASntuuOVtI/BCJfiZ3A2sJOkTh6DNCG+9W1fbUPNo Ytvkt6/mQqj6Y/EhrZ0bichG3dh9 X-Google-Smtp-Source: APXvYqwo8mOCJNJfWIkIiNxlMBD1PspQb0tlU6f6TUHtlkTe3KjFuRd0yV3WhWCavHjeHYX+KvlNcw== X-Received: by 2002:ae9:f205:: with SMTP id m5mr1490551qkg.152.1576661328067; Wed, 18 Dec 2019 01:28:48 -0800 (PST) From: Daniel Henrique Barboza To: libvir-list@redhat.com Date: Wed, 18 Dec 2019 06:28:39 -0300 Message-Id: <20191218092839.4177625-2-danielhb413@gmail.com> In-Reply-To: <20191218092839.4177625-1-danielhb413@gmail.com> References: <20191218092839.4177625-1-danielhb413@gmail.com> MIME-Version: 1.0 X-MC-Unique: quro_6Q0Nhuz3LrDYYYFWw-1 X-MC-Unique: 0Uhgh-2UOqGYB3hXCqigpw-1 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id xBI9Sq0A029111 X-loop: libvir-list@redhat.com Cc: Daniel Henrique Barboza , jtomko@redhat.com Subject: [libvirt] [PATCH v3 1/1] qemu_command: tidy up qemuBuildHostdevCommandLine loop 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.13 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The current 'for' loop with 5 consecutive 'ifs' inside qemuBuildHostdevCommandLine can be a bit smarter: - all 5 'ifs' fails if hostdev->mode is not equal to VIR_DOMAIN_HOSTDEV_MODE_SUBSYS. This check can be moved to the start of the loop, failing to the next element immediately in case it fails; - all 5 'ifs' checks for a specific subsys->type to build the proper command line argument (virHostdevIsSCSIDevice and virHostdevIsMdevDevice do that but within a helper). Problem is that the code will keep checking for matches even if one was already found, and there is no way a hostdev will fit more than one 'if' (i.e. a hostdev can't have 2+ different types). This means that a SUBSYS_TYPE_USB will create its command line argument in the first 'if', then all other conditionals will surely fail but will end up being checked anyway. All of this can be avoided by moving the hostdev->mode comparing to the start of the loop and using a switch statement with subsys->type to execute the proper code for a given hostdev type. Suggested-by: J=C3=A1n Tomko Signed-off-by: Daniel Henrique Barboza --- src/qemu/qemu_command.c | 52 +++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 836057a4ff..948ef688f2 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -5309,23 +5309,31 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, for (i =3D 0; i < def->nhostdevs; i++) { virDomainHostdevDefPtr hostdev =3D def->hostdevs[i]; virDomainHostdevSubsysPtr subsys =3D &hostdev->source.subsys; + virDomainHostdevSubsysSCSIPtr scsisrc; + virDomainHostdevSubsysMediatedDevPtr mdevsrc; g_autofree char *devstr =3D NULL; + g_autofree char *drvstr =3D NULL; + g_autofree char *vhostfdName =3D NULL; + unsigned int bootIndex; + int vhostfd =3D -1; =20 - /* USB */ - if (hostdev->mode =3D=3D VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type =3D=3D VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) { + if (hostdev->mode !=3D VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) + continue; =20 + switch (subsys->type) { + /* USB */ + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: virCommandAddArg(cmd, "-device"); if (!(devstr =3D qemuBuildUSBHostdevDevStr(def, hostdev, qemuCaps))) return -1; virCommandAddArg(cmd, devstr); - } + + break; =20 /* PCI */ - if (hostdev->mode =3D=3D VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type =3D=3D VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) { - unsigned int bootIndex =3D hostdev->info->bootIndex; + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: + bootIndex =3D hostdev->info->bootIndex; =20 /* bootNet will be non-0 if boot order was set and no other * net devices were encountered @@ -5343,13 +5351,12 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, if (!devstr) return -1; virCommandAddArg(cmd, devstr); - } + + break; =20 /* SCSI */ - if (virHostdevIsSCSIDevice(hostdev)) { - virDomainHostdevSubsysSCSIPtr scsisrc =3D - &hostdev->source.subsys.u.scsi; - g_autofree char *drvstr =3D NULL; + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: + scsisrc =3D &hostdev->source.subsys.u.scsi; =20 if (scsisrc->protocol =3D=3D VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_= TYPE_ISCSI) { virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc =3D @@ -5372,15 +5379,13 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, if (!(devstr =3D qemuBuildSCSIHostdevDevStr(def, hostdev))) return -1; virCommandAddArg(cmd, devstr); - } + + break; =20 /* SCSI_host */ - if (hostdev->mode =3D=3D VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type =3D=3D VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST) { + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: if (hostdev->source.subsys.u.scsi_host.protocol =3D=3D VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST) { - g_autofree char *vhostfdName =3D NULL; - int vhostfd =3D -1; =20 if (virSCSIVHostOpenVhostSCSI(&vhostfd) < 0) return -1; @@ -5399,11 +5404,12 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, =20 virCommandAddArg(cmd, devstr); } - } + + break; =20 /* MDEV */ - if (virHostdevIsMdevDevice(hostdev)) { - virDomainHostdevSubsysMediatedDevPtr mdevsrc =3D &subsys->u.md= ev; + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: + mdevsrc =3D &subsys->u.mdev; =20 switch ((virMediatedDeviceModelType) mdevsrc->model) { case VIR_MDEV_MODEL_TYPE_VFIO_PCI: @@ -5422,6 +5428,12 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, qemuBuildHostdevMediatedDevStr(def, hostdev, qemuCaps))) return -1; virCommandAddArg(cmd, devstr); + + break; + + default: + virReportEnumRangeError(virDomainHostdevSubsysType, subsys->ty= pe); + return -1; } } =20 --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list