From nobody Tue Feb 10 07:41:21 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=1627058482; cv=none; d=zohomail.com; s=zohoarc; b=Q9OLRzlUsSOFIgjNcgNmYe1abCXokcpB2Ohpap1HHmpOuhAVtB0nsMhRgU1PhuRKRzHD+7tP1J9QJoF8mxk6W/+a0AFFCMJLUMQPg5Kaia/gBpBePc98WEBtmXwJHFWldgIRmUpmbzwJ4JdlUwzdle7oBTyP7BJIMDxvC62itMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627058482; 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=TBqXPGjPQM4E43ttyNTdC+zCxPTLfe3/cTb4k4xprLM=; b=mB3vcH24wpXpQcYvor8pNSCYQ1grpjbGxvDQAiXv+bRaS+mMSgdwqsiRbwie3ADgZtKL83etAfHWg/8PZ48ydueRiLNN/th537IXoQ6u50Cwy5kZLAzEf/EN1E+kZgPl6Nb6IQReqBU+VO4VNSseQLsyo5TP1nwqAvARcqWAbrQ= 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) 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 1627058482124408.08741412280096; Fri, 23 Jul 2021 09:41:22 -0700 (PDT) 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-579-2LFhrhnuP9qaeTGSdg-Aqg-1; Fri, 23 Jul 2021 12:41:19 -0400 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 3729A34834; Fri, 23 Jul 2021 16:41:08 +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 625FF1ABDF; Fri, 23 Jul 2021 16:41:07 +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 2DB2E4A706; Fri, 23 Jul 2021 16:41:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 16NGem0Q001309 for ; Fri, 23 Jul 2021 12:40:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0E8BF6091B; Fri, 23 Jul 2021 16:40:48 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-114-96.phx2.redhat.com [10.3.114.96]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C6A9D6A05B for ; Fri, 23 Jul 2021 16:40:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627058481; 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=TBqXPGjPQM4E43ttyNTdC+zCxPTLfe3/cTb4k4xprLM=; b=GkyEKj/OKBvsdL52nV+4Thma3AOtOncsRxNSP+EeIrB0353NLZgQXffURlj8iQDpma/+1A /gJt4TREcxrJgm1mxPTOS1wiZSkAxvW9lPgZygeLY0NIDEzcDJEnC5j56cXXkT89qE8bOi xtFGeRMYvjwoaw3xq8VXz/lFcusd0mY= X-MC-Unique: 2LFhrhnuP9qaeTGSdg-Aqg-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH 2/7] nodedev: fix xml output for mdev parents in test suite Date: Fri, 23 Jul 2021 11:40:37 -0500 Message-Id: <20210723164042.1334799-3-jjongsma@redhat.com> In-Reply-To: <20210723164042.1334799-1-jjongsma@redhat.com> References: <20210723164042.1334799-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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) X-ZM-MESSAGEID: 1627058483135100001 Content-Type: text/plain; charset="utf-8" Commit 51fbbfdce8 attempted to get the proper nodedev name for the parent of an defined mdev by traversing the filesystem and looking for a device that had the appropriate sysfs path. This works, but it would be cleaner to to avoid mucking around in the filesystem and instead just just examine the list of devices we have in memory. We already had a function nodeDeviceFindAddressByName() which constructs an address for parent device in a format that can be used with mdevctl. So if we refactor this function into a a function that simply formats an address for an arbitrary virNodeDeviceObj*, then we can use this function as a predicate for our new virNodeDeviceObjListFind() function from the previous commit. This will search our list of devices for one whose address matches the address we get from mdevctl. One nice benefit of this approach is that our test cases will now display xml output with the proper parent name for mdevs (assuming that we've added the appropriate mock parent devices to the test driver). Previously they just displayed 'computer' for the parent because the alternative would have required specially constructing a mock filesystem environment with a sysfs that mapped to the appropriate parent. Signed-off-by: Jonathon Jongsma --- src/node_device/node_device_driver.c | 55 ++++++++++--------- .../mdevctl-list-multiple.out.xml | 8 +-- tests/nodedevmdevctltest.c | 28 +++++++++- 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index 6f8968f1f0..a3a261f08b 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -648,17 +648,11 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDef *def, c= har **buf) =20 =20 static char * -nodeDeviceFindAddressByName(const char *name) +nodeDeviceObjFormatAddress(virNodeDeviceObj *obj) { - virNodeDeviceDef *def =3D NULL; virNodeDevCapsDef *caps =3D NULL; char *addr =3D NULL; - virNodeDeviceObj *dev =3D virNodeDeviceObjListFindByName(driver->devs,= name); - - if (!dev) - return NULL; - - def =3D virNodeDeviceObjGetDef(dev); + virNodeDeviceDef *def =3D virNodeDeviceObjGetDef(obj); for (caps =3D def->caps; caps !=3D NULL; caps =3D caps->next) { switch (caps->data.type) { case VIR_NODE_DEV_CAP_PCI_DEV: { @@ -714,8 +708,6 @@ nodeDeviceFindAddressByName(const char *name) break; } =20 - virNodeDeviceObjEndAPI(&dev); - return addr; } =20 @@ -730,6 +722,7 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, g_autoptr(virCommand) cmd =3D NULL; const char *subcommand =3D virMdevctlCommandTypeToString(cmd_type); g_autofree char *inbuf =3D NULL; + virNodeDeviceObj *parent_obj =3D NULL; =20 switch (cmd_type) { case MDEVCTL_CMD_CREATE: @@ -754,7 +747,10 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, switch (cmd_type) { case MDEVCTL_CMD_CREATE: case MDEVCTL_CMD_DEFINE: - parent_addr =3D nodeDeviceFindAddressByName(def->parent); + if ((parent_obj =3D nodeDeviceObjFindByName(def->parent))) { + parent_addr =3D nodeDeviceObjFormatAddress(parent_obj); + virNodeDeviceObjEndAPI(&parent_obj); + } =20 if (!parent_addr) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -1042,6 +1038,21 @@ static void mdevGenerateDeviceName(virNodeDeviceDef = *dev) } =20 =20 +static bool +matchDeviceAddress(virNodeDeviceObj *obj, + const void *opaque) +{ + g_autofree char *addr =3D NULL; + bool want =3D false; + + virObjectLock(obj); + addr =3D nodeDeviceObjFormatAddress(obj); + want =3D STREQ_NULLABLE(addr, opaque); + virObjectUnlock(obj); + return want; +} + + static virNodeDeviceDef* nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *json) @@ -1051,7 +1062,7 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *props; virJSONValue *attrs; g_autoptr(virNodeDeviceDef) child =3D g_new0(virNodeDeviceDef, 1); - g_autofree char *parent_sysfs_path =3D NULL; + virNodeDeviceObj *parent_obj; =20 /* the child object should have a single key equal to its uuid. * The value is an object describing the properties of the mdev */ @@ -1064,20 +1075,12 @@ nodeDeviceParseMdevctlChildDevice(const char *paren= t, /* Look up id of parent device. mdevctl supports defining mdevs for pa= rent * devices that are not present on the system (to support starting mde= vs on * hotplug, etc) so the parent may not actually exist. */ - parent_sysfs_path =3D g_strdup_printf("/sys/class/mdev_bus/%s", parent= ); - if (virFileExists(parent_sysfs_path)) { - g_autofree char *canon_syspath =3D virFileCanonicalizePath(parent_= sysfs_path); - virNodeDeviceObj *parentobj =3D NULL; - - if ((parentobj =3D virNodeDeviceObjListFindBySysfsPath(driver->dev= s, - canon_syspath= ))) { - virNodeDeviceDef *parentdef =3D virNodeDeviceObjGetDef(parento= bj); - child->parent =3D g_strdup(parentdef->name); - virNodeDeviceObjEndAPI(&parentobj); - - child->parent_sysfs_path =3D g_steal_pointer(&canon_syspath); - } - } + if ((parent_obj =3D virNodeDeviceObjListFind(driver->devs, matchDevice= Address, + (void *)parent))) { + virNodeDeviceDef *parentdef =3D virNodeDeviceObjGetDef(parent_obj); + child->parent =3D g_strdup(parentdef->name); + virNodeDeviceObjEndAPI(&parent_obj); + }; if (!child->parent) child->parent =3D g_strdup("computer"); child->caps =3D g_new0(virNodeDevCapsDef, 1); diff --git a/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml b/tests= /nodedevmdevctldata/mdevctl-list-multiple.out.xml index eead6f2a40..c23a3130c1 100644 --- a/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml +++ b/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml @@ -1,6 +1,6 @@ mdev_200f228a_c80a_4d50_bfb7_f5a0e4e34045 - computer + pci_0000_00_02_0 200f228a-c80a-4d50-bfb7-f5a0e4e34045 @@ -9,7 +9,7 @@ mdev_de807ffc_1923_4d5f_b6c9_b20ecebc6d4b - computer + pci_0000_00_02_0 de807ffc-1923-4d5f-b6c9-b20ecebc6d4b @@ -18,7 +18,7 @@ mdev_435722ea_5f43_468a_874f_da34f1217f13 - computer + pci_0000_00_02_0 435722ea-5f43-468a-874f-da34f1217f13 @@ -28,7 +28,7 @@ mdev_783e6dbb_ea0e_411f_94e2_717eaad438bf - computer + matrix_matrix 783e6dbb-ea0e-411f-94e2-717eaad438bf diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index e246de4d87..7af89ab5f1 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -231,7 +231,7 @@ fakeRootDevice(void) * parent of the mdev, and it needs a PCI address */ static virNodeDeviceDef * -fakeParentDevice(void) +fakePCIDevice(void) { virNodeDeviceDef *def =3D NULL; virNodeDevCapPCIDev *pci_dev; @@ -252,6 +252,29 @@ fakeParentDevice(void) return def; } =20 + +/* Add a fake matrix device that can be used as a parent device for mediat= ed + * devices. For our purposes, it only needs to have a name that matches the + * parent of the mdev, and it needs the proper name + */ +static virNodeDeviceDef * +fakeMatrixDevice(void) +{ + virNodeDeviceDef *def =3D NULL; + virNodeDevCapAPMatrix *cap; + + def =3D g_new0(virNodeDeviceDef, 1); + def->caps =3D g_new0(virNodeDevCapsDef, 1); + + def->name =3D g_strdup("matrix_matrix"); + def->parent =3D g_strdup("computer"); + + def->caps->data.type =3D VIR_NODE_DEV_CAP_AP_MATRIX; + cap =3D &def->caps->data.ap_matrix; + cap->addr =3D g_strdup("matrix"); + + return def; +} static int addDevice(virNodeDeviceDef *def) { @@ -274,7 +297,8 @@ static int nodedevTestDriverAddTestDevices(void) { if (addDevice(fakeRootDevice()) < 0 || - addDevice(fakeParentDevice()) < 0) + addDevice(fakePCIDevice()) < 0 || + addDevice(fakeMatrixDevice()) < 0) return -1; =20 return 0; --=20 2.31.1