[libvirt] [libvirt-php PATCH 0/2] implement a few network API bindings

Hello, The followinig two patches add a few useful API bindings for dealing with libvirt networks: * virConnectListAllNetworks - allows for better control when filtering and returns array of handles to virNetworkPtr which can then be used to get other info for each network such as name, uuid, autostart etc. * virNetworkGetUUIDString, virNetworkGetUUID, virNetworkGetName to be able to easily get said info when working with virNetworkPtr handles. Dawid Zamirski (2): implement binding for virConnectListAllNetworks. implement additional network functions. src/libvirt-php.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 4 ++ 2 files changed, 153 insertions(+) -- 2.12.2

Some hypervisors (like Hyper-V) allow duplicate network names which makes existing libvirt_list_networks not very useful as it returns just names. Since virConnectListAllNetworks returns array of handles to virNetworkPtr, one can use e.g. virNetworkGetUUIDString to resolve such ambiguity issues. --- src/libvirt-php.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 1 + 2 files changed, 63 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 3a80a53..fa02457 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -737,6 +737,7 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optflags) PHP_FE(libvirt_list_domain_resources, arginfo_libvirt_conn) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optcap) + PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optflags) PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optflags) PHP_FE(libvirt_list_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) @@ -1814,6 +1815,13 @@ PHP_MINIT_FUNCTION(libvirt) REGISTER_LONG_CONSTANT("VIR_NETWORKS_INACTIVE", VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_NETWORKS_ALL", VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_INACTIVE", VIR_CONNECT_LIST_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_ACTIVE", VIR_CONNECT_LIST_NETWORKS_ACTIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_PERSISTENT", VIR_CONNECT_LIST_NETWORKS_PERSISTENT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_TRANSIENT", VIR_CONNECT_LIST_NETWORKS_TRANSIENT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_AUTOSTART", VIR_CONNECT_LIST_NETWORKS_AUTOSTART, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART", VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART, CONST_CS | CONST_PERSISTENT); + /* Credential constants */ REGISTER_LONG_CONSTANT("VIR_CRED_USERNAME", 1, CONST_CS | CONST_PERSISTENT); @@ -9160,6 +9168,60 @@ PHP_FUNCTION(libvirt_list_inactive_domains) } /* + * Function name: libvirt_list_all_networks + * Since version: 0.5.3 + * Description: Function is used to list networks on the connection + * Arguments: @res [resource]: libvirt connection resource + * @flags [int]: optional flags to filter the results for a smaller list of targetted networks (bitwise-OR VIR_CONNECT_LIST_NETWORKS_* constants) + * Returns: libvirt network resources array for the connection + */ +PHP_FUNCTION(libvirt_list_all_networks) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; +#if PHP_MAJOR_VERSION >= 7 + zval znetwork; +#else + zval *znetwork; +#endif + zend_long flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE | + VIR_CONNECT_LIST_NETWORKS_INACTIVE; + int count = -1; + size_t i = 0; + virNetworkPtr *nets = NULL; + virNetworkPtr network = NULL; + php_libvirt_network *res_network; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); + + if ((count = virConnectListAllNetworks(conn->conn, &nets, flags)) < 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d networks\n", PHPFUNC, count); + + array_init(return_value); + + for (i = 0; i < count; i++) { + network = nets[i]; + res_network = (php_libvirt_network *) emalloc(sizeof(php_libvirt_network)); + res_network->network = network; + res_network->conn = conn; + + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, + res_network->network, 1 TSRMLS_CC); +#if PHP_MAJOR_VERSION >= 7 + ZVAL_RES(&znetwork, zend_register_resource(res_network, + le_libvirt_network)); + add_next_index_zval(return_value, &znetwork); +#else + ALLOC_INIT_ZVAL(znetwork); + ZEND_REGISTER_RESOURCE(znetwork, res_network, le_libvirt_network); + add_next_index_zval(return_value, znetwork); +#endif + } +} + +/* * Function name: libvirt_list_networks * Since version: 0.4.1(-1) * Description: Function is used to list networks on the connection diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 9978e27..1a003f5 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -427,6 +427,7 @@ PHP_FUNCTION(libvirt_nodedev_get_xml_desc); PHP_FUNCTION(libvirt_nodedev_get_information); /* Listing functions */ PHP_FUNCTION(libvirt_list_nodedevs); +PHP_FUNCTION(libvirt_list_all_networks); PHP_FUNCTION(libvirt_list_networks); PHP_FUNCTION(libvirt_list_domains); PHP_FUNCTION(libvirt_list_domain_snapshots); -- 2.12.2

* add libvirt_network_get_uuid_string * add libvirt_network_get_uuid * add libvirt_network_get_name Those are useful when working with network resources returned from libvirt_list_all_networks. --- src/libvirt-php.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 3 ++ 2 files changed, 90 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index fa02457..bfc9b7d 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -721,6 +721,9 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_network_get_information, arginfo_libvirt_conn) PHP_FE(libvirt_network_get_active, arginfo_libvirt_conn) PHP_FE(libvirt_network_set_active, arginfo_libvirt_conn_flags) + PHP_FE(libvirt_network_get_uuid_string, arginfo_libvirt_conn) + PHP_FE(libvirt_network_get_uuid, arginfo_libvirt_conn) + PHP_FE(libvirt_network_get_name, arginfo_libvirt_conn) /* Node functions */ PHP_FE(libvirt_node_get_info, arginfo_libvirt_conn) PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_optcpunr) @@ -9915,6 +9918,90 @@ PHP_FUNCTION(libvirt_network_get_xml_desc) } /* + * Function name: libvirt_network_get_uuid_string + * Since version: 0.5.3 + * Description: Function is used to get network's UUID in string format + * Arguments: @res [resource]: libvirt network resource + * Returns: network UUID string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid_string) +{ + php_libvirt_network *network = NULL; + zval *znetwork; + char *uuid = NULL; + int ret = -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid = (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret = virNetworkGetUUIDString(network->network, uuid); + + DPRINTF("%s: virNetworkGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + network->network, ret, uuid); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETURN_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_uuid + * Since version: 0.5.3 + * Descirption: Function is used to get network's UUID in binary format + * Arguments: @res [resource]: libvirt netowrk resource + * Returns: network UUID in binary format or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid) +{ + php_libvirt_network *network = NULL; + zval *znetwork; + char *uuid = NULL; + int ret = -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid = (char *) emalloc(VIR_UUID_BUFLEN); + ret = virNetworkGetUUID(network->network, (unsigned char *)uuid); + + DPRINTF("%s: virNetworkGetUUID(%p, %p) returned %d\n", PHPFUNC, + network->network, uuid, ret); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_name + * Since version: 0.5.3 + * Description: Function is used to get network's name + * Arguments: @res [resource]: libvirt network resource + * Returns: network name string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_name) +{ + php_libvirt_network *network = NULL; + zval *znetwork; + const char *name = NULL; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + name = virNetworkGetName(network->network); + + DPRINTF("%s: virNetworkGetName(%p) returned %s\n", PHPFUNC, + network->network, name); + + if (name == NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as network resource */ + VIRT_RETURN_STRING(name); +} + +/* * Function name: libvirt_version * Since version: 0.4.1(-1) * Description: Function is used to get libvirt, driver and libvirt-php version numbers. Can be used for information purposes, for version checking please use libvirt_check_version() defined below diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 1a003f5..f87d180 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -420,6 +420,9 @@ PHP_FUNCTION(libvirt_network_get_bridge); PHP_FUNCTION(libvirt_network_get_information); PHP_FUNCTION(libvirt_network_get_active); PHP_FUNCTION(libvirt_network_set_active); +PHP_FUNCTION(libvirt_network_get_uuid_string); +PHP_FUNCTION(libvirt_network_get_uuid); +PHP_FUNCTION(libvirt_network_get_name); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); -- 2.12.2

On Mon, May 8, 2017 at 11:43 AM, Dawid Zamirski <dzamirski@datto.com> wrote:
Hello,
The followinig two patches add a few useful API bindings for dealing with libvirt networks:
* virConnectListAllNetworks - allows for better control when filtering and returns array of handles to virNetworkPtr which can then be used to get other info for each network such as name, uuid, autostart etc. * virNetworkGetUUIDString, virNetworkGetUUID, virNetworkGetName to be able to easily get said info when working with virNetworkPtr handles.
Dawid Zamirski (2): implement binding for virConnectListAllNetworks. implement additional network functions.
src/libvirt-php.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 4 ++ 2 files changed, 153 insertions(+)
@Dawid: This patch series looks okay to me, fwiw. @Michal: After merging these patches in, could you please make a new release? It's been a bit over a year, and there's been quite a few changes since last year's release. I'd like to pull in the release and push it out in Fedora and EPEL 7. Thanks in advance. -- 真実はいつも一つ!/ Always, there's only one truth!

On 05/10/2017 11:31 PM, Neal Gompa wrote:
On Mon, May 8, 2017 at 11:43 AM, Dawid Zamirski <dzamirski@datto.com> wrote:
Hello,
The followinig two patches add a few useful API bindings for dealing with libvirt networks:
* virConnectListAllNetworks - allows for better control when filtering and returns array of handles to virNetworkPtr which can then be used to get other info for each network such as name, uuid, autostart etc. * virNetworkGetUUIDString, virNetworkGetUUID, virNetworkGetName to be able to easily get said info when working with virNetworkPtr handles.
Dawid Zamirski (2): implement binding for virConnectListAllNetworks. implement additional network functions.
src/libvirt-php.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 4 ++ 2 files changed, 153 insertions(+)
@Dawid: This patch series looks okay to me, fwiw.
Agreed, pushed.
@Michal: After merging these patches in, could you please make a new release? It's been a bit over a year, and there's been quite a few changes since last year's release. I'd like to pull in the release and push it out in Fedora and EPEL 7.
Sure. Let me do that. Michal

On 05/08/2017 05:43 PM, Dawid Zamirski wrote:
Hello,
The followinig two patches add a few useful API bindings for dealing with libvirt networks:
* virConnectListAllNetworks - allows for better control when filtering and returns array of handles to virNetworkPtr which can then be used to get other info for each network such as name, uuid, autostart etc. * virNetworkGetUUIDString, virNetworkGetUUID, virNetworkGetName to be able to easily get said info when working with virNetworkPtr handles.
Dawid Zamirski (2): implement binding for virConnectListAllNetworks. implement additional network functions.
src/libvirt-php.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 4 ++ 2 files changed, 153 insertions(+)
Ah, sorry this has been around a while and I've missed it as I am buried under some other work. ACKed and pushed, thank you. Michal
participants (3)
-
Dawid Zamirski
-
Michal Privoznik
-
Neal Gompa