From nobody Tue Feb 10 09:24:50 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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 170.10.133.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=1629498888; cv=none; d=zohomail.com; s=zohoarc; b=RCqqazfSgdCI4kbIrIFnIHv2+ovlV6c1xuN6maHAl4tMGCtW7zkFXqCytRJRzh7HFVBLCM/YDSDoQnFG/CRJvZ0pcaK4ALlNQgeB0PnlrBIuTPepvhEXu6HW7BUK61ZZ0oL4nUeTi/JtXZMdHSUTHmL3vhZOO4OT1hKJ+ejRGj0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629498888; 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=e5TDpanF1goXMzfROILLvxAPP7RCExB23e8ojX/RDPg=; b=U3Qff8mW4jQPgldsVQz0ilzDN4qlRoMMO+RbNtvdlknITrqCWLb0q6orBSsHFsJ7x9AtaHsHKKiORHrxQvbJZkj0EfCm7LpdCg9VImekoarwI44FHQuQFll1bauVlf9AwWnBmT+gru1TZPQzobSn7tgZNoD3yblwZ0w2DJc2WJ8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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 [170.10.133.124]) by mx.zohomail.com with SMTPS id 1629498888058563.463347017599; Fri, 20 Aug 2021 15:34:48 -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-340-yvuIwHu-MqmZUMdMsQGphA-1; Fri, 20 Aug 2021 18:34:44 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B08EE1008064; Fri, 20 Aug 2021 22:34:39 +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 84A79E2C6; Fri, 20 Aug 2021 22:34:39 +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 D1600181A0F7; Fri, 20 Aug 2021 22:34:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 17KMTfJm028149 for ; Fri, 20 Aug 2021 18:29:41 -0400 Received: by smtp.corp.redhat.com (Postfix) id 52E4E1971B; Fri, 20 Aug 2021 22:29:41 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-114-57.phx2.redhat.com [10.3.114.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F0931F5 for ; Fri, 20 Aug 2021 22:29:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1629498887; 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=e5TDpanF1goXMzfROILLvxAPP7RCExB23e8ojX/RDPg=; b=DrXF4UOJybBcS5riaUab9Lv/hvynJ0u8yYbmylSvH5RYzlAs3aHzWWU78iXPyiU5CZL8TC HsuWd+xFuPFoQOpXLv4Pq/3Huxb6TyB9RpCSJNrBGJyfwca6le5TLc3oof4Xv2pt2O0LpL 12ABi++E27dU7EWgjVccnaB/wxvjdCQ= X-MC-Unique: yvuIwHu-MqmZUMdMsQGphA-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH v3 2/7] nodedev: implement virNodeDevice(Get|Set)Autostart() Date: Fri, 20 Aug 2021 17:29:30 -0500 Message-Id: <20210820222935.3414029-3-jjongsma@redhat.com> In-Reply-To: <20210820222935.3414029-1-jjongsma@redhat.com> References: <20210820222935.3414029-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.84 on 10.5.11.23 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: 1629498889687100001 Content-Type: text/plain; charset="utf-8" Implement autostart functionality for mediated devices. Signed-off-by: Jonathon Jongsma --- src/conf/node_device_conf.h | 1 + src/conf/virnodedeviceobj.c | 16 +++ src/conf/virnodedeviceobj.h | 6 ++ src/libvirt_private.syms | 2 + src/node_device/node_device_driver.c | 139 ++++++++++++++++++++++++++- src/node_device/node_device_driver.h | 13 +++ src/node_device/node_device_udev.c | 12 ++- 7 files changed, 185 insertions(+), 4 deletions(-) diff --git a/src/conf/node_device_conf.h b/src/conf/node_device_conf.h index 786de85060..5a4d9c7a55 100644 --- a/src/conf/node_device_conf.h +++ b/src/conf/node_device_conf.h @@ -154,6 +154,7 @@ struct _virNodeDevCapMdev { virMediatedDeviceAttr **attributes; size_t nattributes; char *parent_addr; + bool autostart; }; =20 typedef struct _virNodeDevCapPCIDev virNodeDevCapPCIDev; diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index 87e180f3ef..9a9841576a 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -41,6 +41,7 @@ struct _virNodeDeviceObj { used by testdriver */ bool active; bool persistent; + bool autostart; }; =20 struct _virNodeDeviceObjList { @@ -1034,6 +1035,21 @@ virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj, } =20 =20 +bool +virNodeDeviceObjIsAutostart(virNodeDeviceObj *obj) +{ + return obj->autostart; +} + + +void +virNodeDeviceObjSetAutostart(virNodeDeviceObj *obj, + bool autostart) +{ + obj->autostart =3D autostart; +} + + typedef struct _PredicateHelperData PredicateHelperData; struct _PredicateHelperData { virNodeDeviceObjListPredicate predicate; diff --git a/src/conf/virnodedeviceobj.h b/src/conf/virnodedeviceobj.h index 068c7c6f34..5f6d22e1f6 100644 --- a/src/conf/virnodedeviceobj.h +++ b/src/conf/virnodedeviceobj.h @@ -137,6 +137,12 @@ virNodeDeviceObjIsPersistent(virNodeDeviceObj *obj); void virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj, bool persistent); +bool +virNodeDeviceObjIsAutostart(virNodeDeviceObj *obj); + +void +virNodeDeviceObjSetAutostart(virNodeDeviceObj *obj, + bool autostart); =20 typedef bool (*virNodeDeviceObjListPredicate)(virNodeDeviceObj *obj, const void *opaque); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 090ac80691..e78a8c8631 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1287,6 +1287,7 @@ virNetworkPortDefSaveStatus; virNodeDeviceObjEndAPI; virNodeDeviceObjGetDef; virNodeDeviceObjIsActive; +virNodeDeviceObjIsAutostart; virNodeDeviceObjIsPersistent; virNodeDeviceObjListAssignDef; virNodeDeviceObjListExport; @@ -1304,6 +1305,7 @@ virNodeDeviceObjListNumOfDevices; virNodeDeviceObjListRemove; virNodeDeviceObjListRemoveLocked; virNodeDeviceObjSetActive; +virNodeDeviceObjSetAutostart; virNodeDeviceObjSetPersistent; =20 =20 diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index db619aa2d8..bb18b24e53 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -621,11 +621,12 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDef *def, c= har **buf) size_t i; virNodeDevCapMdev *mdev =3D &def->caps->data.mdev; g_autoptr(virJSONValue) json =3D virJSONValueNewObject(); + const char *startval =3D mdev->autostart ? "auto" : "manual"; =20 if (virJSONValueObjectAppendString(json, "mdev_type", mdev->type) < 0) return -1; =20 - if (virJSONValueObjectAppendString(json, "start", "manual") < 0) + if (virJSONValueObjectAppendString(json, "start", startval) < 0) return -1; =20 if (mdev->attributes) { @@ -1014,6 +1015,46 @@ virMdevctlStart(virNodeDeviceDef *def) } =20 =20 +/* gets a virCommand object that executes a mdevctl command to set the + * 'autostart' property of the device to the specified value + */ +virCommand* +nodeDeviceGetMdevctlSetAutostartCommand(virNodeDeviceDef *def, + bool autostart, + char **errmsg) +{ + virCommand *cmd =3D virCommandNewArgList(MDEVCTL, + "modify", + "--uuid", + def->caps->data.mdev.uuid, + NULL); + + if (autostart) + virCommandAddArg(cmd, "--auto"); + else + virCommandAddArg(cmd, "--manual"); + + virCommandSetErrorBuffer(cmd, errmsg); + + return cmd; +} + + +static int +virMdevctlSetAutostart(virNodeDeviceDef *def, bool autostart, char **errms= g) +{ + int status; + g_autoptr(virCommand) cmd =3D NULL; + + cmd =3D nodeDeviceGetMdevctlSetAutostartCommand(def, autostart, errmsg= ); + + if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + return -1; + + return 0; +} + + virCommand* nodeDeviceGetMdevctlListCommand(bool defined, char **output, @@ -1066,6 +1107,7 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *attrs; g_autoptr(virNodeDeviceDef) child =3D g_new0(virNodeDeviceDef, 1); virNodeDeviceObj *parent_obj; + const char *start =3D NULL; =20 /* the child object should have a single key equal to its uuid. * The value is an object describing the properties of the mdev */ @@ -1094,6 +1136,8 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, mdev->parent_addr =3D g_strdup(parent); mdev->type =3D g_strdup(virJSONValueObjectGetString(props, "mdev_type")); + start =3D virJSONValueObjectGetString(props, "start"); + mdev->autostart =3D STREQ_NULLABLE(start, "auto"); =20 attrs =3D virJSONValueObjectGet(props, "attrs"); =20 @@ -1346,6 +1390,7 @@ nodeDeviceUpdateMediatedDevice(virNodeDeviceDef *def) =20 /* all devices returned by virMdevctlListDefined() are persistent */ virNodeDeviceObjSetPersistent(obj, true); + virNodeDeviceObjSetAutostart(obj, def->caps->data.mdev.autostart); =20 if (!defined) event =3D virNodeDeviceEventLifecycleNew(name, @@ -1649,10 +1694,12 @@ removeMissingPersistentMdev(virNodeDeviceObj *obj, =20 /* The device is active, but no longer defined by mdevctl. Keep the de= vice * in the list, but mark it as non-persistent */ - if (virNodeDeviceObjIsActive(obj)) + if (virNodeDeviceObjIsActive(obj)) { + virNodeDeviceObjSetAutostart(obj, false); virNodeDeviceObjSetPersistent(obj, false); - else + } else { remove =3D true; + } =20 virObjectEventStateQueue(driver->nodeDeviceEventState, event); =20 @@ -1762,6 +1809,92 @@ nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, if (virMediatedDeviceAttrsCopy(dstmdev, srcmdev)) ret =3D true; =20 + if (dstmdev->autostart !=3D srcmdev->autostart) { + ret =3D true; + dstmdev->autostart =3D srcmdev->autostart; + } + + return ret; +} + + +int +nodeDeviceSetAutostart(virNodeDevice *device, + int autostart) +{ + int ret =3D -1; + virNodeDeviceObj *obj =3D NULL; + virNodeDeviceDef *def =3D NULL; + + if (nodeDeviceInitWait() < 0) + return -1; + + if (!(obj =3D nodeDeviceObjFindByName(device->name))) + return -1; + def =3D virNodeDeviceObjGetDef(obj); + + if (virNodeDeviceSetAutostartEnsureACL(device->conn, def) < 0) + goto cleanup; + + if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) { + if (!virNodeDeviceObjIsPersistent(obj)) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("cannot set autostart for transient dev= ice")); + goto cleanup; + } + + if (autostart !=3D virNodeDeviceObjIsAutostart(obj)) { + g_autofree char *errmsg =3D NULL; + + if (virMdevctlSetAutostart(def, autostart, &errmsg) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to set autostart on '%s': %s"), + def->name, + errmsg && errmsg[0] !=3D '\0' ? errmsg : _(= "Unknown Error")); + goto cleanup; + } + /* Due to mdevctl performance issues, it may take several seco= nds + * to re-query mdevctl for the defined devices. Because the md= evctl + * command returned without an error status, assume it was + * successful and set the object status directly here rather t= han + * waiting for the next query */ + virNodeDeviceObjSetAutostart(obj, autostart); + } + ret =3D 0; + } else { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("Unsupported device type")); + } + + cleanup: + virNodeDeviceObjEndAPI(&obj); + return ret; +} + + +int +nodeDeviceGetAutostart(virNodeDevice *device, + int *autostart) +{ + virNodeDeviceObj *obj =3D NULL; + virNodeDeviceDef *def =3D NULL; + int ret =3D -1; + + if (nodeDeviceInitWait() < 0) + return -1; + + if (!(obj =3D nodeDeviceObjFindByName(device->name))) + return -1; + def =3D virNodeDeviceObjGetDef(obj); + + if (virNodeDeviceGetAutostartEnsureACL(device->conn, def) < 0) + goto cleanup; + + *autostart =3D virNodeDeviceObjIsAutostart(obj); + ret =3D 0; + + cleanup: + virNodeDeviceObjEndAPI(&obj); return ret; } =20 diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_de= vice_driver.h index fdc92b8aef..17c7473d85 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -177,3 +177,16 @@ int nodeDeviceDefPostParse(virNodeDeviceDef *def, =20 int nodeDeviceDefValidate(virNodeDeviceDef *def, void *opaque); + +int +nodeDeviceSetAutostart(virNodeDevice *dev, + int autostart); + +int +nodeDeviceGetAutostart(virNodeDevice *dev, + int *autostart); + +virCommand* +nodeDeviceGetMdevctlSetAutostartCommand(virNodeDeviceDef *def, + bool autostart, + char **errmsg); diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index 90a64f16b0..73334c1cc7 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -1504,6 +1504,8 @@ udevAddOneDevice(struct udev_device *device) bool new_device =3D true; int ret =3D -1; bool was_persistent =3D false; + bool autostart =3D false; + bool is_mdev; =20 def =3D g_new0(virNodeDeviceDef, 1); =20 @@ -1525,12 +1527,16 @@ udevAddOneDevice(struct udev_device *device) if (udevSetParent(device, def) !=3D 0) goto cleanup; =20 + is_mdev =3D def->caps->data.type =3D=3D VIR_NODE_DEV_CAP_MDEV; + if ((obj =3D virNodeDeviceObjListFindByName(driver->devs, def->name)))= { objdef =3D virNodeDeviceObjGetDef(obj); =20 - if (def->caps->data.type =3D=3D VIR_NODE_DEV_CAP_MDEV) + if (is_mdev) nodeDeviceDefCopyFromMdevctl(def, objdef); was_persistent =3D virNodeDeviceObjIsPersistent(obj); + autostart =3D virNodeDeviceObjIsAutostart(obj); + /* If the device was defined by mdevctl and was never instantiated= , it * won't have a sysfs path. We need to emit a CREATED event... */ new_device =3D (objdef->sysfs_path =3D=3D NULL); @@ -1543,6 +1549,7 @@ udevAddOneDevice(struct udev_device *device) if (!(obj =3D virNodeDeviceObjListAssignDef(driver->devs, def))) goto cleanup; virNodeDeviceObjSetPersistent(obj, was_persistent); + virNodeDeviceObjSetAutostart(obj, autostart); objdef =3D virNodeDeviceObjGetDef(obj); =20 if (new_device) @@ -1946,6 +1953,7 @@ udevSetupSystemDev(void) goto cleanup; =20 virNodeDeviceObjSetActive(obj, true); + virNodeDeviceObjSetAutostart(obj, true); =20 virNodeDeviceObjEndAPI(&obj); =20 @@ -2350,6 +2358,8 @@ static virNodeDeviceDriver udevNodeDeviceDriver =3D { .nodeDeviceDefineXML =3D nodeDeviceDefineXML, /* 7.3.0 */ .nodeDeviceUndefine =3D nodeDeviceUndefine, /* 7.3.0 */ .nodeDeviceCreate =3D nodeDeviceCreate, /* 7.3.0 */ + .nodeDeviceSetAutostart =3D nodeDeviceSetAutostart, /* 7.7.0 */ + .nodeDeviceGetAutostart =3D nodeDeviceGetAutostart, /* 7.7.0 */ }; =20 =20 --=20 2.31.1