Two new APIs to work with user defined metadata: - virNWFilterSetMetadata() - virNWFilterGetMetadata() Signed-off-by: K Shiva Kiran <shiva_kr@xxxxxxxxxx> --- include/libvirt/libvirt-nwfilter.h | 27 +++++ include/libvirt/virterror.h | 1 + src/driver-nwfilter.h | 15 +++ src/libvirt-nwfilter.c | 154 +++++++++++++++++++++++++++++ src/libvirt_public.syms | 6 ++ src/util/virerror.c | 3 + 6 files changed, 206 insertions(+) diff --git a/include/libvirt/libvirt-nwfilter.h b/include/libvirt/libvirt-nwfilter.h index 33b842b464..1bfe05d633 100644 --- a/include/libvirt/libvirt-nwfilter.h +++ b/include/libvirt/libvirt-nwfilter.h @@ -159,4 +159,31 @@ int virNWFilterBindingDelete(virNWFilterBindingPtr binding); int virNWFilterBindingRef(virNWFilterBindingPtr binding); int virNWFilterBindingFree(virNWFilterBindingPtr binding); +/** + * virNWFilterMetadataType: + * + * Since: 9.9.0 + */ +typedef enum { + VIR_NWFILTER_METADATA_DESCRIPTION = 0, /* Operate on <description> (Since: 9.9.0) */ + VIR_NWFILTER_METADATA_TITLE = 1, /* Operate on <title> (Since: 9.9.0) */ + VIR_NWFILTER_METADATA_ELEMENT = 2, /* Operate on <metadata> (Since: 9.9.0) */ + +# ifdef VIR_ENUM_SENTINELS + VIR_NWFILTER_METADATA_LAST /* (Since: 9.9.0) */ +# endif +} virNWFilterMetadataType; + +int virNWFilterSetMetadata(virNWFilterPtr nwfilter, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags); + +char * virNWFilterGetMetadata(virNWFilterPtr nwfilter, + int type, + const char *uri, + unsigned int flags); + #endif /* LIBVIRT_NWFILTER_H */ diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index 224eddc9e4..04d06b4bda 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -349,6 +349,7 @@ typedef enum { VIR_ERR_CHECKPOINT_INCONSISTENT = 109, /* checkpoint can't be used (Since: 6.10.0) */ VIR_ERR_MULTIPLE_DOMAINS = 110, /* more than one matching domain found (Since: 7.1.0) */ VIR_ERR_NO_NETWORK_METADATA = 111, /* Network metadata is not present (Since: 9.7.0) */ + VIR_ERR_NO_NWFILTER_METADATA = 112, /* NWFilter metadata is not present (Since: 9.9.0) */ # ifdef VIR_ENUM_SENTINELS VIR_ERR_NUMBER_LAST /* (Since: 5.0.0) */ diff --git a/src/driver-nwfilter.h b/src/driver-nwfilter.h index 1ec591ece9..8d38dbd23c 100644 --- a/src/driver-nwfilter.h +++ b/src/driver-nwfilter.h @@ -86,6 +86,19 @@ typedef int typedef int (*virDrvNWFilterBindingFree)(virNWFilterBindingPtr binding); +typedef int +(*virDrvNWFilterSetMetadata)(virNWFilterPtr nwfilter, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags); + +typedef char * +(*virDrvNWFilterGetMetadata)(virNWFilterPtr nwfilter, + int type, + const char *uri, + unsigned int flags); typedef struct _virNWFilterDriver virNWFilterDriver; @@ -111,4 +124,6 @@ struct _virNWFilterDriver { virDrvNWFilterBindingCreateXML nwfilterBindingCreateXML; virDrvNWFilterBindingDelete nwfilterBindingDelete; virDrvNWFilterBindingGetXMLDesc nwfilterBindingGetXMLDesc; + virDrvNWFilterSetMetadata nwfilterSetMetadata; + virDrvNWFilterGetMetadata nwfilterGetMetadata; }; diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c index ff82cc3b7f..a496bc9f0a 100644 --- a/src/libvirt-nwfilter.c +++ b/src/libvirt-nwfilter.c @@ -920,3 +920,157 @@ virNWFilterBindingRef(virNWFilterBindingPtr binding) virObjectRef(binding); return 0; } + + +/** + * virNWFilterSetMetadata: + * @nwfilter: a network filter object + * @type: type of metadata, from virNWFilterMetadataType + * @metadata: new metadata text + * @key: XML namespace key, or NULL + * @uri: XML namespace URI, or NULL + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Sets the appropriate nwfilter element given by @type to the + * value of @metadata. + * For options VIR_NWFILTER_METADATA_TITLE and VIR_NWFILTER_METADATA_DESCRIPTION + * @key and @uri are irrelevant and must be set to NULL. + * + * For type VIR_NWFILTER_METADATA_ELEMENT @metadata must be a well-formed + * XML belonging to namespace defined by @uri with local name @key. + * + * Passing NULL for @metadata says to remove that element from the + * network filter XML (passing the empty string leaves the element present). + * + * The resulting metadata will be present in virNWFilterGetXMLDesc(), + * as well as quick access through virNWFilterGetMetadata(). + * + * Returns 0 on success, -1 in case of failure. + * + * Since: 9.9.0 + */ +int +virNWFilterSetMetadata(virNWFilterPtr nwfilter, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("nwfilter=%p, type=%d, metadata='%s', key='%s', uri='%s', flags=0x%x", + nwfilter, type, NULLSTR(metadata), NULLSTR(key), NULLSTR(uri), + flags); + + virResetLastError(); + + virCheckNWFilterReturn(nwfilter, -1); + conn = nwfilter->conn; + + virCheckReadOnlyGoto(conn->flags, error); + + switch (type) { + case VIR_NWFILTER_METADATA_TITLE: + if (metadata && strchr(metadata, '\n')) { + virReportInvalidArg(metadata, "%s", + _("metadata title can't contain newlines")); + goto error; + } + G_GNUC_FALLTHROUGH; + case VIR_NWFILTER_METADATA_DESCRIPTION: + virCheckNullArgGoto(uri, error); + virCheckNullArgGoto(key, error); + break; + case VIR_NWFILTER_METADATA_ELEMENT: + virCheckNonNullArgGoto(uri, error); + if (metadata) + virCheckNonNullArgGoto(key, error); + break; + default: + /* For future expansion */ + break; + } + + if (conn->nwfilterDriver->nwfilterSetMetadata) { + int ret; + ret = conn->nwfilterDriver->nwfilterSetMetadata(nwfilter, type, metadata, key, uri, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(nwfilter->conn); + return -1; +} + + +/** + * virNWFilterGetMetadata: + * @nwfilter: a network filter object + * @type: type of metadata, from virNWFilterMetadataType + * @uri: XML namespace identifier + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Retrieves the appropriate network filter element given by @type. + * If VIR_NWFILTER_METADATA_ELEMENT is requested parameter @uri + * must be set to the name of the namespace the requested elements + * belong to, otherwise must be NULL. + * + * If an element of the network filter XML is not present, the resulting + * error will be VIR_ERR_NO_NWFILTER_METADATA. This method forms + * a shortcut for seeing information from virNWFilterSetMetadata() + * without having to go through virNWFilterGetXMLDesc(). + * + * Returns the metadata string on success (caller must free), + * or NULL in case of failure. + * + * Since: 9.9.0 + */ +char * +virNWFilterGetMetadata(virNWFilterPtr nwfilter, + int type, + const char *uri, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("nwfilter=%p, type=%d, uri='%s', flags=0x%x", + nwfilter, type, NULLSTR(uri), flags); + + virResetLastError(); + + virCheckNWFilterReturn(nwfilter, NULL); + + switch (type) { + case VIR_NWFILTER_METADATA_TITLE: + case VIR_NWFILTER_METADATA_DESCRIPTION: + virCheckNullArgGoto(uri, error); + break; + case VIR_NWFILTER_METADATA_ELEMENT: + virCheckNonNullArgGoto(uri, error); + break; + default: + /* For future expansion */ + break; + } + + conn = nwfilter->conn; + + if (conn->nwfilterDriver->nwfilterGetMetadata) { + char *ret; + if (!(ret = conn->nwfilterDriver->nwfilterGetMetadata(nwfilter, type, uri, flags))) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(nwfilter->conn); + return NULL; +} diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index bd1e916d2a..02d32567a4 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -938,4 +938,10 @@ LIBVIRT_9.7.0 { virNetworkSetMetadata; } LIBVIRT_9.0.0; +LIBVIRT_9.9.0 { + global: + virNWFilterGetMetadata; + virNWFilterSetMetadata; +} LIBVIRT_9.7.0; + # .... define new API here using predicted next version number .... diff --git a/src/util/virerror.c b/src/util/virerror.c index 227a182417..a1d0d73e5d 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1290,6 +1290,9 @@ static const virErrorMsgTuple virErrorMsgStrings[] = { [VIR_ERR_NO_NETWORK_METADATA] = { N_("metadata not found"), N_("metadata not found: %1$s") }, + [VIR_ERR_NO_NWFILTER_METADATA] = { + N_("metadata not found"), + N_("metadata not found: %1$s") }, }; G_STATIC_ASSERT(G_N_ELEMENTS(virErrorMsgStrings) == VIR_ERR_NUMBER_LAST); -- 2.42.0