From nobody Tue Feb 10 12:57:37 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=1618346420; cv=none; d=zohomail.com; s=zohoarc; b=Cun7pWfA+6GBoqjXt7lyW/x+qbC73bQLE7fgnLok0RWRCIGl2egsWgRhu40WslP6CHc1rX9H+ylcz4l6+6wp4SnElslWQWx+NDLwaQHPonGAfaN4ZBgCGbvWtGrfDYgbebw9531LE3w7FoG9qW3iC0gakqT+DRuTBK2JKk9MNW4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618346420; 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=8fcv2K50G8uezLJ3TtRMsnT3wC0ELyfPTjPblXUAkyo=; b=CvdoNVA92ZUsjK9KlUc6fTYNcnV+Kkm6GlV2y5+PgFBqUY9RuS/8Mk6/1oWmrJRIZp00AvsW04xYZPU1DqglLexfDPDAh7zh37lUjhCyAFVOSSQrRPTCOM2V/N9RYKkuN42LCmE1sJqTWiShqnXtK/xNWdVYw+alzryfFb5Ofy8= 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) header.from= 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 1618346420260751.4549858236601; Tue, 13 Apr 2021 13:40:20 -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-132-9SkcTseKOHC_woFvBV-CaQ-1; Tue, 13 Apr 2021 16:40:16 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 026EF1922963; Tue, 13 Apr 2021 20:40:12 +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 D044D5D9D0; Tue, 13 Apr 2021 20:40:11 +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 71AD2180B654; Tue, 13 Apr 2021 20:40:11 +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 13DKdtwe011706 for ; Tue, 13 Apr 2021 16:39:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 78E5A1975E; Tue, 13 Apr 2021 20:39:55 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-113-166.phx2.redhat.com [10.3.113.166]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 399B41972B; Tue, 13 Apr 2021 20:39:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618346419; 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=8fcv2K50G8uezLJ3TtRMsnT3wC0ELyfPTjPblXUAkyo=; b=AcDGoS7+zSB1TRYOiVdmsYg22DdRY1X5901GvOahc2ZZnfzW1HcJv8o86KgDtLE8v3Cljn G2xHRnEnaN4PkH1CAOP+ZaWGeD1gzcL5Y0zGHf1q2LlwZW2igolV2zyefEOtZA/XkDbX6d CLr7XzVOYCp6iqBgWc6espVmks98ClA= X-MC-Unique: 9SkcTseKOHC_woFvBV-CaQ-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH v2 07/12] nodedev: driver: Create a generic mdevctl command translator Date: Tue, 13 Apr 2021 15:39:43 -0500 Message-Id: <20210413203948.71864-8-jjongsma@redhat.com> In-Reply-To: <20210413203948.71864-1-jjongsma@redhat.com> References: <20210413203948.71864-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 Cc: Erik Skultety 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.14 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" From: Erik Skultety Currently there are dedicated wrappers to construct mdevctl command. These are mostly fine except for the one that translates both "start" and "define" commands, only because mdevctl takes the same set of arguments. Instead, keep the wrappers, but let them call a single global translator that handles all the mdevctl command differences and commonalities. Signed-off-by: Erik Skultety Signed-off-by: Jonathon Jongsma Reviewed-by: Laine Stump --- src/node_device/node_device_driver.c | 125 ++++++++++++++++----------- src/node_device/node_device_driver.h | 6 +- tests/nodedevmdevctltest.c | 4 +- 3 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index da55e386f0..bbb01c3967 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -702,42 +702,75 @@ nodeDeviceFindAddressByName(const char *name) } =20 =20 -/* the mdevctl 'start' and 'define' commands accept almost the exact same - * arguments, so provide a common implementation that can be wrapped by a = more - * specific function */ -static virCommand* -nodeDeviceGetMdevctlDefineCreateCommand(virNodeDeviceDef *def, - const char *subcommand, - char **uuid_out, - char **errmsg) +static virCommand * +nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, + virMdevctlCommand cmd_type, + char **outbuf, + char **errbuf) { - virCommand *cmd; - g_autofree char *json =3D NULL; - g_autofree char *parent_addr =3D nodeDeviceFindAddressByName(def->pare= nt); - - if (!parent_addr) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unable to find parent device '%s'"), def->parent= ); - return NULL; + g_autofree char *parent_addr =3D NULL; + virCommand *cmd =3D NULL; + const char *subcommand =3D virMdevctlCommandTypeToString(cmd_type); + g_autofree char *inbuf =3D NULL; + + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + /* now is the time to make sure "create" is replaced with "start" = on + * mdevctl cmdline */ + cmd =3D virCommandNewArgList(MDEVCTL, "start", NULL); + break; + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_START: + case MDEVCTL_CMD_DEFINE: + case MDEVCTL_CMD_UNDEFINE: + cmd =3D virCommandNewArgList(MDEVCTL, subcommand, NULL); + break; + case MDEVCTL_CMD_LAST: + default: + /* SHOULD NEVER HAPPEN */ + break; } =20 - if (nodeDeviceDefToMdevctlConfig(def, &json) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("couldn't convert node device def to mdevctl JSON= ")); - return NULL; - } + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + case MDEVCTL_CMD_DEFINE: + parent_addr =3D nodeDeviceFindAddressByName(def->parent); =20 - cmd =3D virCommandNewArgList(MDEVCTL, subcommand, NULL); - virCommandAddArgPair(cmd, "--parent", parent_addr); - virCommandAddArgPair(cmd, "--jsonfile", "/dev/stdin"); + if (!parent_addr) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to find parent device '%s'"), def->pa= rent); + return NULL; + } + + if (nodeDeviceDefToMdevctlConfig(def, &inbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("couldn't convert node device def to mdevctl = JSON")); + return NULL; + } =20 - virCommandSetInputBuffer(cmd, json); + virCommandAddArgPair(cmd, "--parent", parent_addr); + virCommandAddArgPair(cmd, "--jsonfile", "/dev/stdin"); + + virCommandSetInputBuffer(cmd, inbuf); + virCommandSetOutputBuffer(cmd, outbuf); + break; + + case MDEVCTL_CMD_UNDEFINE: + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_START: + /* No special handling here, we only need to pass UUID with these = */ + break; + case MDEVCTL_CMD_LAST: + default: + /* SHOULD NEVER HAPPEN */ + break; + } =20 + /* Fill in UUID for commands that need it */ if (def->caps->data.mdev.uuid) virCommandAddArgPair(cmd, "--uuid", def->caps->data.mdev.uuid); =20 - virCommandSetOutputBuffer(cmd, uuid_out); - virCommandSetErrorBuffer(cmd, errmsg); + virCommandSetErrorBuffer(cmd, errbuf); =20 return cmd; } @@ -747,8 +780,7 @@ nodeDeviceGetMdevctlCreateCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineCreateCommand(def, "start", uuid_out, - errmsg); + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_CREATE, uuid_out, = errmsg); } =20 virCommand* @@ -756,8 +788,7 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, char **uuid_out, char **errmsg) { - return nodeDeviceGetMdevctlDefineCreateCommand(def, "define", uuid_out, - errmsg); + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_DEFINE, uuid_out, = errmsg); } =20 =20 @@ -891,31 +922,24 @@ nodeDeviceCreateXML(virConnectPtr conn, =20 =20 virCommand * -nodeDeviceGetMdevctlStopCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDef *def, + char **errmsg) { - virCommand *cmd =3D virCommandNewArgList(MDEVCTL, "stop", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; - + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errmsg= ); } =20 virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDef *def, + char **errmsg) { - virCommand *cmd =3D virCommandNewArgList(MDEVCTL, "undefine", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, er= rmsg); } =20 virCommand * -nodeDeviceGetMdevctlStartCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, + char **errmsg) { - virCommand *cmd =3D virCommandNewArgList(MDEVCTL, "start", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errms= g); } =20 static int @@ -924,7 +948,7 @@ virMdevctlStop(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlStopCommand(def->caps->data.mdev.uuid, err= msg); + cmd =3D nodeDeviceGetMdevctlStopCommand(def, errmsg); =20 if (virCommandRun(cmd, &status) < 0 || status !=3D 0) return -1; @@ -939,8 +963,7 @@ virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlUndefineCommand(def->caps->data.mdev.uuid, - errmsg); + cmd =3D nodeDeviceGetMdevctlUndefineCommand(def, errmsg); =20 if (virCommandRun(cmd, &status) < 0 || status !=3D 0) return -1; @@ -955,7 +978,7 @@ virMdevctlStart(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlStartCommand(def->caps->data.mdev.uuid, er= rmsg); + cmd =3D nodeDeviceGetMdevctlStartCommand(def, errmsg); =20 if (virCommandRun(cmd, &status) < 0 || status !=3D 0) return -1; diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_de= vice_driver.h index d06efbf354..105a71dd93 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -152,11 +152,11 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *d= ef, char **errmsg); =20 virCommand * -nodeDeviceGetMdevctlStopCommand(const char *uuid, +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDef *def, char **errmsg); =20 virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDef *def, char **errmsg); =20 virCommand * @@ -181,7 +181,7 @@ bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, virNodeDeviceDef *src); =20 virCommand* -nodeDeviceGetMdevctlStartCommand(const char *uuid, +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, char **errmsg); =20 int diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index cf8de852a8..188bad6e53 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -115,7 +115,7 @@ testMdevctlCreateOrDefineHelper(const void *data) mdevxml, cmdlinefile, jsonfile); } =20 -typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **err= buf); +typedef virCommand* (*GetStopUndefineCmdFunc)(virNodeDeviceDef *def, char = **errbuf); struct UuidCommandTestInfo { const char *filename; virMdevctlCommand command; @@ -136,7 +136,7 @@ testMdevctlUuidCommand(GetStopUndefineCmdFunc func, if (!(def =3D virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEM= U"))) goto cleanup; =20 - cmd =3D func(def->caps->data.mdev.uuid, &errmsg); + cmd =3D func(def, &errmsg); =20 if (!cmd) goto cleanup; --=20 2.26.3