From: Erik Skultety <eskultet@xxxxxxxxxx> 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 <eskultet@xxxxxxxxxx> Signed-off-by: Jonathon Jongsma <jjongsma@xxxxxxxxxx> --- 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_device_driver.c index c25558cf05..0fddfdde86 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) } -/* 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(virNodeDeviceDefPtr def, + virMdevctlCommand cmd_type, + char **outbuf, + char **errbuf) { - virCommandPtr cmd; - g_autofree char *json = NULL; - g_autofree char *parent_addr = nodeDeviceFindAddressByName(def->parent); - - if (!parent_addr) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unable to find parent device '%s'"), def->parent); - return NULL; + g_autofree char *parent_addr = NULL; + virCommand *cmd = NULL; + const char *subcommand = virMdevctlCommandTypeToString(cmd_type); + g_autofree char *inbuf = NULL; + + switch (cmd_type) { + case MDEVCTL_CMD_CREATE: + /* now is the time to make sure "create" is replaced with "start" on + * mdevctl cmdline */ + cmd = virCommandNewArgList(MDEVCTL, "start", NULL); + break; + case MDEVCTL_CMD_STOP: + case MDEVCTL_CMD_START: + case MDEVCTL_CMD_DEFINE: + case MDEVCTL_CMD_UNDEFINE: + cmd = virCommandNewArgList(MDEVCTL, subcommand, NULL); + break; + case MDEVCTL_CMD_LAST: + default: + /* SHOULD NEVER HAPPEN */ + break; } - 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 = nodeDeviceFindAddressByName(def->parent); - cmd = 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->parent); + return NULL; + } + + if (nodeDeviceDefToMdevctlConfig(def, &inbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("couldn't convert node device def to mdevctl JSON")); + return NULL; + } - 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; + } + /* Fill in UUID for commands that need it */ if (def->caps->data.mdev.uuid) virCommandAddArgPair(cmd, "--uuid", def->caps->data.mdev.uuid); - virCommandSetOutputBuffer(cmd, uuid_out); - virCommandSetErrorBuffer(cmd, errmsg); + virCommandSetErrorBuffer(cmd, errbuf); 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); } 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); } @@ -891,31 +922,24 @@ nodeDeviceCreateXML(virConnectPtr conn, virCommandPtr -nodeDeviceGetMdevctlStopCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, + char **errmsg) { - virCommandPtr cmd = virCommandNewArgList(MDEVCTL, "stop", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; - + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errmsg); } virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, + char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, "undefine", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, errmsg); } virCommand* -nodeDeviceGetMdevctlStartCommand(const char *uuid, char **errmsg) +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDef *def, + char **errmsg) { - virCommand *cmd = virCommandNewArgList(MDEVCTL, "start", NULL); - virCommandAddArgPair(cmd, "--uuid", uuid); - virCommandSetErrorBuffer(cmd, errmsg); - return cmd; + return nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errmsg); } static int @@ -924,7 +948,7 @@ virMdevctlStop(virNodeDeviceDefPtr def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStopCommand(def->caps->data.mdev.uuid, errmsg); + cmd = nodeDeviceGetMdevctlStopCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -939,8 +963,7 @@ virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlUndefineCommand(def->caps->data.mdev.uuid, - errmsg); + cmd = nodeDeviceGetMdevctlUndefineCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; @@ -955,7 +978,7 @@ virMdevctlStart(virNodeDeviceDef *def, char **errmsg) int status; g_autoptr(virCommand) cmd = NULL; - cmd = nodeDeviceGetMdevctlStartCommand(def->caps->data.mdev.uuid, errmsg); + cmd = nodeDeviceGetMdevctlStartCommand(def, errmsg); if (virCommandRun(cmd, &status) < 0 || status != 0) return -1; diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h index aaeed588e8..0fea75a118 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -152,11 +152,11 @@ nodeDeviceGetMdevctlDefineCommand(virNodeDeviceDef *def, char **errmsg); virCommandPtr -nodeDeviceGetMdevctlStopCommand(const char *uuid, +nodeDeviceGetMdevctlStopCommand(virNodeDeviceDefPtr def, char **errmsg); virCommand * -nodeDeviceGetMdevctlUndefineCommand(const char *uuid, +nodeDeviceGetMdevctlUndefineCommand(virNodeDeviceDefPtr def, char **errmsg); virCommandPtr @@ -181,7 +181,7 @@ bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDef *dst, virNodeDeviceDef *src); virCommand* -nodeDeviceGetMdevctlStartCommand(const char *uuid, +nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def, char **errmsg); int diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c index 8a37dd2e80..e766ae8f34 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -130,7 +130,7 @@ testMdevctlCreateOrDefineHelper(const void *data) mdevxml, cmdlinefile, jsonfile); } -typedef virCommand* (*GetStopUndefineCmdFunc)(const char *uuid, char **errbuf); +typedef virCommand* (*GetStopUndefineCmdFunc)(virNodeDeviceDef *def, char **errbuf); struct UuidCommandTestInfo { const char *filename; virMdevctlCommand command; @@ -150,7 +150,7 @@ testMdevctlUuidCommand(GetStopUndefineCmdFunc func, if (!(def = virNodeDeviceDefParseFile(mdevxml, EXISTING_DEVICE, "QEMU"))) goto cleanup; - cmd = func(def->caps->data.mdev.uuid, &errmsg); + cmd = func(def, &errmsg); if (!cmd) goto cleanup; -- 2.26.3