Module libvirt-network from libvirt

Provides APIs for the management of networks

Table of Contents

Macros

#define VIR_NETWORK_EVENT_CALLBACK

Types

typedef enum virConnectListAllNetworksFlags
typedef enum virIPAddrType
typedef struct _virNetwork virNetwork
typedef struct _virNetworkDHCPLease virNetworkDHCPLease
typedef virNetworkDHCPLease * virNetworkDHCPLeasePtr
typedef enum virNetworkEventID
typedef enum virNetworkEventLifecycleType
typedef virNetwork * virNetworkPtr
typedef enum virNetworkUpdateCommand
typedef enum virNetworkUpdateFlags
typedef enum virNetworkUpdateSection
typedef enum virNetworkXMLFlags

Functions

int	virConnectListAllNetworks	(virConnectPtr conn, 
virNetworkPtr ** nets,
unsigned int flags) int virConnectListDefinedNetworks (virConnectPtr conn,
char ** const names,
int maxnames) int virConnectListNetworks (virConnectPtr conn,
char ** const names,
int maxnames) int virConnectNetworkEventDeregisterAny (virConnectPtr conn,
int callbackID) typedef virConnectNetworkEventGenericCallback void virConnectNetworkEventGenericCallback (virConnectPtr conn,
virNetworkPtr net,
void * opaque) typedef virConnectNetworkEventLifecycleCallback void virConnectNetworkEventLifecycleCallback (virConnectPtr conn,
virNetworkPtr net,
int event,
int detail,
void * opaque) int virConnectNetworkEventRegisterAny (virConnectPtr conn,
virNetworkPtr net,
int eventID,
virConnectNetworkEventGenericCallback cb,
void * opaque,
virFreeCallback freecb) int virConnectNumOfDefinedNetworks (virConnectPtr conn) int virConnectNumOfNetworks (virConnectPtr conn) int virNetworkCreate (virNetworkPtr network) virNetworkPtr virNetworkCreateXML (virConnectPtr conn,
const char * xmlDesc) void virNetworkDHCPLeaseFree (virNetworkDHCPLeasePtr lease) virNetworkPtr virNetworkDefineXML (virConnectPtr conn,
const char * xml) int virNetworkDestroy (virNetworkPtr network) int virNetworkFree (virNetworkPtr network) int virNetworkGetAutostart (virNetworkPtr network,
int * autostart) char * virNetworkGetBridgeName (virNetworkPtr network) virConnectPtr virNetworkGetConnect (virNetworkPtr net) int virNetworkGetDHCPLeases (virNetworkPtr network,
const char * mac,
virNetworkDHCPLeasePtr ** leases,
unsigned int flags) const char * virNetworkGetName (virNetworkPtr network) int virNetworkGetUUID (virNetworkPtr network,
unsigned char * uuid) int virNetworkGetUUIDString (virNetworkPtr network,
char * buf) char * virNetworkGetXMLDesc (virNetworkPtr network,
unsigned int flags) int virNetworkIsActive (virNetworkPtr net) int virNetworkIsPersistent (virNetworkPtr net) virNetworkPtr virNetworkLookupByName (virConnectPtr conn,
const char * name) virNetworkPtr virNetworkLookupByUUID (virConnectPtr conn,
const unsigned char * uuid) virNetworkPtr virNetworkLookupByUUIDString (virConnectPtr conn,
const char * uuidstr) int virNetworkRef (virNetworkPtr network) int virNetworkSetAutostart (virNetworkPtr network,
int autostart) int virNetworkUndefine (virNetworkPtr network) int virNetworkUpdate (virNetworkPtr network,
unsigned int command,
unsigned int section,
int parentIndex,
const char * xml,
unsigned int flags)

Description

Macros

VIR_NETWORK_EVENT_CALLBACK

#define VIR_NETWORK_EVENT_CALLBACK

Used to cast the event specific callback into the generic one for use for virConnectNetworkEventRegisterAny()

Types

virConnectListAllNetworksFlags

virIPAddrType

virNetwork

struct virNetwork {
The content of this structure is not made public by the API
}

virNetworkDHCPLease

struct virNetworkDHCPLease {
char * iface
Network interface name
long long expirytime
Seconds since epoch
int type
char * mac
MAC address
char * iaid
IAID
char * ipaddr
IP address
unsigned int prefix
IP address prefix
char * hostname
Hostname
char * clientid
Client ID or DUID
}

virNetworkEventID

enum virNetworkEventID {
VIR_NETWORK_EVENT_ID_LIFECYCLE = 0
VIR_NETWORK_EVENT_ID_LAST = 1
NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last event ID supported by this version of the libvirt API.
}

virNetworkEventLifecycleType

virNetworkUpdateCommand

enum virNetworkUpdateCommand {
VIR_NETWORK_UPDATE_COMMAND_NONE = 0
(invalid)
VIR_NETWORK_UPDATE_COMMAND_MODIFY = 1
modify an existing element
VIR_NETWORK_UPDATE_COMMAND_DELETE = 2
delete an existing element
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST = 3
add an element at end of list
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST = 4
add an element at start of list
VIR_NETWORK_UPDATE_COMMAND_LAST = 5
}

virNetworkUpdateFlags

enum virNetworkUpdateFlags {
VIR_NETWORK_UPDATE_AFFECT_CURRENT = 0
affect live if network is active, config if it's not active
VIR_NETWORK_UPDATE_AFFECT_LIVE = 1
affect live state of network only
VIR_NETWORK_UPDATE_AFFECT_CONFIG = 2
affect persistent config only
}

virNetworkUpdateSection

enum virNetworkUpdateSection {
VIR_NETWORK_SECTION_NONE = 0
(invalid)
VIR_NETWORK_SECTION_BRIDGE = 1
<bridge>
VIR_NETWORK_SECTION_DOMAIN = 2
<domain>
VIR_NETWORK_SECTION_IP = 3
<ip>
VIR_NETWORK_SECTION_IP_DHCP_HOST = 4
<ip>/<dhcp>/<host>
VIR_NETWORK_SECTION_IP_DHCP_RANGE = 5
<ip>/<dhcp>/<range>
VIR_NETWORK_SECTION_FORWARD = 6
<forward>
VIR_NETWORK_SECTION_FORWARD_INTERFACE = 7
<forward>/<interface>
VIR_NETWORK_SECTION_FORWARD_PF = 8
<forward>/<pf>
VIR_NETWORK_SECTION_PORTGROUP = 9
<portgroup>
VIR_NETWORK_SECTION_DNS_HOST = 10
<dns>/<host>
VIR_NETWORK_SECTION_DNS_TXT = 11
<dns>/<txt>
VIR_NETWORK_SECTION_DNS_SRV = 12
<dns>/<srv>
VIR_NETWORK_SECTION_LAST = 13
}

virNetworkXMLFlags

enum virNetworkXMLFlags {
VIR_NETWORK_XML_INACTIVE = 1
dump inactive network information
}

Functions

virConnectListAllNetworks

int	virConnectListAllNetworks	(virConnectPtr conn,
					 virNetworkPtr ** nets,
					 unsigned int flags)

Collect the list of networks, and allocate an array to store those objects. This API solves the race inherent between virConnectListNetworks and virConnectListDefinedNetworks.

Normally, all networks are returned; however, @flags can be used to filter the results for a smaller list of targeted networks. The valid flags are divided into groups, where each group contains bits that describe mutually exclusive attributes of a network, and where all bits within a group describe all possible networks.

The first group of @flags is VIR_CONNECT_LIST_NETWORKS_ACTIVE (up) and VIR_CONNECT_LIST_NETWORKS_INACTIVE (down) to filter the networks by state.

The second group of @flags is VIR_CONNECT_LIST_NETWORKS_PERSISTENT (defined) and VIR_CONNECT_LIST_NETWORKS_TRANSIENT (running but not defined), to filter the networks by whether they have persistent config or not.

The third group of @flags is VIR_CONNECT_LIST_NETWORKS_AUTOSTART and VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART, to filter the networks by whether they are marked as autostart or not.

conn
Pointer to the hypervisor connection.
nets
Pointer to a variable to store the array containing the network objects or NULL if the list is not required (just returns number of networks).
flags
bitwise-OR of virConnectListAllNetworksFlags.
Returns
the number of networks found or -1 and sets @nets to NULL in case of error. On success, the array stored into @nets is guaranteed to have an extra allocated element set to NULL but not included in the return count, to make iteration easier. The caller is responsible for calling virNetworkFree() on each array element, then calling free() on @nets.

virConnectListDefinedNetworks

int	virConnectListDefinedNetworks	(virConnectPtr conn,
					 char ** const names,
					 int maxnames)

list the inactive networks, stores the pointers to the names in @names

For more control over the results, see virConnectListAllNetworks().

conn
pointer to the hypervisor connection
names
pointer to an array to store the names
maxnames
size of the array
Returns
the number of names provided in the array or -1 in case of error. Note that this command is inherently racy; a network can be defined between a call to virConnectNumOfDefinedNetworks() and this call; you are only guaranteed that all currently defined networks were listed if the return is less than @maxnames. The client must call free() on each returned name.

virConnectListNetworks

int	virConnectListNetworks		(virConnectPtr conn,
					 char ** const names,
					 int maxnames)

Collect the list of active networks, and store their names in @names

For more control over the results, see virConnectListAllNetworks().

conn
pointer to the hypervisor connection
names
array to collect the list of names of active networks
maxnames
size of @names
Returns
the number of networks found or -1 in case of error. Note that this command is inherently racy; a network can be started between a call to virConnectNumOfNetworks() and this call; you are only guaranteed that all currently active networks were listed if the return is less than @maxnames. The client must call free() on each returned name.

virConnectNetworkEventDeregisterAny

int	virConnectNetworkEventDeregisterAny	(virConnectPtr conn,
						 int callbackID)

Removes an event callback. The callbackID parameter should be the value obtained from a previous virConnectNetworkEventRegisterAny() method.

conn
pointer to the connection
callbackID
the callback identifier
Returns
0 on success, -1 on failure

virConnectNetworkEventGenericCallback

typedef void	(*virConnectNetworkEventGenericCallback)	(virConnectPtr conn,
						 virNetworkPtr net,
						 void * opaque)

A generic network event callback handler, for use with virConnectNetworkEventRegisterAny(). Specific events usually have a customization with extra parameters, often with @opaque being passed in a different parameter position; use VIR_NETWORK_EVENT_CALLBACK() when registering an appropriate handler.

conn
the connection pointer
net
the network pointer
opaque
application specified data

virConnectNetworkEventLifecycleCallback

typedef void	(*virConnectNetworkEventLifecycleCallback)	(virConnectPtr conn,
						 virNetworkPtr net,
						 int event,
						 int detail,
						 void * opaque)

This callback occurs when the network is started or stopped.

The callback signature to use when registering for an event of type VIR_NETWORK_EVENT_ID_LIFECYCLE with virConnectNetworkEventRegisterAny()

conn
connection object
net
network on which the event occurred
event
The specific virNetworkEventLifeCycleType which occurred
detail
contains some details on the reason of the event. It will be 0 for the while.
opaque
application specified data

virConnectNetworkEventRegisterAny

int	virConnectNetworkEventRegisterAny	(virConnectPtr conn,
						 virNetworkPtr net,
						 int eventID,
						 virConnectNetworkEventGenericCallback cb,
						 void * opaque,
						 virFreeCallback freecb)

Adds a callback to receive notifications of arbitrary network events occurring on a network. This function requires that an event loop has been previously registered with virEventRegisterImpl() or virEventRegisterDefaultImpl().

If @net is NULL, then events will be monitored for any network. If @net is non-NULL, then only the specific network will be monitored.

Most types of event have a callback providing a custom set of parameters for the event. When registering an event, it is thus necessary to use the VIR_NETWORK_EVENT_CALLBACK() macro to cast the supplied function pointer to match the signature of this method.

The virNetworkPtr object handle passed into the callback upon delivery of an event is only valid for the duration of execution of the callback. If the callback wishes to keep the network object after the callback returns, it shall take a reference to it, by calling virNetworkRef(). The reference can be released once the object is no longer required by calling virNetworkFree().

The return value from this method is a positive integer identifier for the callback. To unregister a callback, this callback ID should be passed to the virConnectNetworkEventDeregisterAny() method.

conn
pointer to the connection
net
pointer to the network
eventID
the event type to receive
cb
callback to the function handling network events
opaque
opaque data to pass on to the callback
freecb
optional function to deallocate opaque when not used anymore
Returns
a callback identifier on success, -1 on failure.

virConnectNumOfDefinedNetworks

int	virConnectNumOfDefinedNetworks	(virConnectPtr conn)

Provides the number of inactive networks.

conn
pointer to the hypervisor connection
Returns
the number of networks found or -1 in case of error

virConnectNumOfNetworks

int	virConnectNumOfNetworks		(virConnectPtr conn)

Provides the number of active networks.

conn
pointer to the hypervisor connection
Returns
the number of network found or -1 in case of error

virNetworkCreate

int	virNetworkCreate		(virNetworkPtr network)

Create and start a defined network. If the call succeed the network moves from the defined to the running networks pools.

network
pointer to a defined network
Returns
0 in case of success, -1 in case of error

virNetworkCreateXML

virNetworkPtr	virNetworkCreateXML	(virConnectPtr conn,
					 const char * xmlDesc)

Create and start a new virtual network, based on an XML description similar to the one returned by virNetworkGetXMLDesc()

virNetworkFree should be used to free the resources after the network object is no longer needed.

conn
pointer to the hypervisor connection
xmlDesc
an XML description of the network
Returns
a new network object or NULL in case of failure

virNetworkDHCPLeaseFree

void	virNetworkDHCPLeaseFree		(virNetworkDHCPLeasePtr lease)

Frees all the memory occupied by @lease.

lease
pointer to a leases object

virNetworkDefineXML

virNetworkPtr	virNetworkDefineXML	(virConnectPtr conn,
					 const char * xml)

Define an inactive persistent virtual network or modify an existing persistent one from the XML description.

virNetworkFree should be used to free the resources after the network object is no longer needed.

conn
pointer to the hypervisor connection
xml
the XML description for the network, preferably in UTF-8
Returns
NULL in case of error, a pointer to the network otherwise

virNetworkDestroy

int	virNetworkDestroy		(virNetworkPtr network)

Destroy the network object. The running instance is shutdown if not down already and all resources used by it are given back to the hypervisor. This does not free the associated virNetworkPtr object. This function may require privileged access

network
a network object
Returns
0 in case of success and -1 in case of failure.

virNetworkFree

int	virNetworkFree			(virNetworkPtr network)

Free the network object. The running instance is kept alive. The data structure is freed and should not be used thereafter.

network
a network object
Returns
0 in case of success and -1 in case of failure.

virNetworkGetAutostart

int	virNetworkGetAutostart		(virNetworkPtr network,
					 int * autostart)

Provides a boolean value indicating whether the network configured to be automatically started when the host machine boots.

network
a network object
autostart
the value returned
Returns
-1 in case of error, 0 in case of success

virNetworkGetBridgeName

char *	virNetworkGetBridgeName		(virNetworkPtr network)

Provides a bridge interface name to which a domain may connect a network interface in order to join the network.

network
a network object
Returns
a 0 terminated interface name, or NULL in case of error. the caller must free() the returned value.

virNetworkGetConnect

virConnectPtr	virNetworkGetConnect	(virNetworkPtr net)

Provides the connection pointer associated with a network. The reference counter on the connection is not increased by this call.

net
pointer to a network
Returns
the virConnectPtr or NULL in case of failure.

virNetworkGetDHCPLeases

int	virNetworkGetDHCPLeases		(virNetworkPtr network,
					 const char * mac,
					 virNetworkDHCPLeasePtr ** leases,
					 unsigned int flags)

For DHCPv4, the information returned: - Network Interface Name - Expiry Time - MAC address - IAID (NULL) - IPv4 address (with type and prefix) - Hostname (can be NULL) - Client ID (can be NULL)

For DHCPv6, the information returned: - Network Interface Name - Expiry Time - MAC address - IAID (can be NULL, only in rare cases) - IPv6 address (with type and prefix) - Hostname (can be NULL) - Client DUID

Note: @mac, @iaid, @ipaddr, @clientid are in ASCII form, not raw bytes. Note: @expirytime can 0, in case the lease is for infinite time.

The API fetches leases info of guests in the specified network. If the optional parameter @mac is specified, the returned list will contain only lease info about a specific guest interface with @mac. There can be multiple leases for a single @mac because this API supports DHCPv6 too.

network
Pointer to network object
mac
Optional ASCII formatted MAC address of an interface
leases
Pointer to a variable to store the array containing details on obtained leases, or NULL if the list is not required (just returns number of leases).
flags
Extra flags, not used yet, so callers should always pass 0
Returns
the number of leases found or -1 and sets @leases to NULL in case of error. On success, the array stored into @leases is guaranteed to have an extra allocated element set to NULL but not included in the return count, to make iteration easier. The caller is responsible for calling virNetworkDHCPLeaseFree() on each array element, then calling free() on @leases. See also virNetworkGetDHCPLeasesForMAC() as a convenience for filtering the list to a single MAC address. Example of usage: virNetworkDHCPLeasePtr *leases = NULL; virNetworkPtr network = ... obtain a network pointer here ...; size_t i; int nleases; unsigned int flags = 0; nleases = virNetworkGetDHCPLeases(network, NULL, &leases, flags); if (nleases < 0) error(); ... do something with returned values, for example: for (i = 0; i < nleases; i++) { virNetworkDHCPLeasePtr lease = leases[i]; printf("Time(epoch): %lu, MAC address: %s, " "IP address: %s, Hostname: %s, ClientID: %s\n", lease->expirytime, lease->mac, lease->ipaddr, lease->hostname, lease->clientid); virNetworkDHCPLeaseFree(leases[i]); } free(leases);

virNetworkGetName

const char *	virNetworkGetName	(virNetworkPtr network)

Get the public name for that network

network
a network object
Returns
a pointer to the name or NULL, the string need not be deallocated its lifetime will be the same as the network object.

virNetworkGetUUID

int	virNetworkGetUUID		(virNetworkPtr network,
					 unsigned char * uuid)

Get the UUID for a network

network
a network object
uuid
pointer to a VIR_UUID_BUFLEN bytes array
Returns
-1 in case of error, 0 in case of success

virNetworkGetUUIDString

int	virNetworkGetUUIDString		(virNetworkPtr network,
					 char * buf)

Get the UUID for a network as string. For more information about UUID see RFC4122.

network
a network object
buf
pointer to a VIR_UUID_STRING_BUFLEN bytes array
Returns
-1 in case of error, 0 in case of success

virNetworkGetXMLDesc

char *	virNetworkGetXMLDesc		(virNetworkPtr network,
					 unsigned int flags)

Provide an XML description of the network. The description may be reused later to relaunch the network with virNetworkCreateXML().

Normally, if a network included a physical function, the output includes all virtual functions tied to that physical interface. If @flags includes VIR_NETWORK_XML_INACTIVE, then the expansion of virtual interfaces is not performed.

network
a network object
flags
bitwise-OR of virNetworkXMLFlags
Returns
a 0 terminated UTF-8 encoded XML instance, or NULL in case of error. the caller must free() the returned value.

virNetworkIsActive

int	virNetworkIsActive		(virNetworkPtr net)

Determine if the network is currently running

net
pointer to the network object
Returns
1 if running, 0 if inactive, -1 on error

virNetworkIsPersistent

int	virNetworkIsPersistent		(virNetworkPtr net)

Determine if the network has a persistent configuration which means it will still exist after shutting down

net
pointer to the network object
Returns
1 if persistent, 0 if transient, -1 on error

virNetworkLookupByName

virNetworkPtr	virNetworkLookupByName	(virConnectPtr conn,
					 const char * name)

Try to lookup a network on the given hypervisor based on its name.

virNetworkFree should be used to free the resources after the network object is no longer needed.

conn
pointer to the hypervisor connection
name
name for the network
Returns
a new network object or NULL in case of failure. If the network cannot be found, then VIR_ERR_NO_NETWORK error is raised.

virNetworkLookupByUUID

virNetworkPtr	virNetworkLookupByUUID	(virConnectPtr conn,
					 const unsigned char * uuid)

Try to lookup a network on the given hypervisor based on its UUID.

virNetworkFree should be used to free the resources after the network object is no longer needed.

conn
pointer to the hypervisor connection
uuid
the raw UUID for the network
Returns
a new network object or NULL in case of failure. If the network cannot be found, then VIR_ERR_NO_NETWORK error is raised.

virNetworkLookupByUUIDString

virNetworkPtr	virNetworkLookupByUUIDString	(virConnectPtr conn,
						 const char * uuidstr)

Try to lookup a network on the given hypervisor based on its UUID.

conn
pointer to the hypervisor connection
uuidstr
the string UUID for the network
Returns
a new network object or NULL in case of failure. If the network cannot be found, then VIR_ERR_NO_NETWORK error is raised.

virNetworkRef

int	virNetworkRef			(virNetworkPtr network)

Increment the reference count on the network. For each additional call to this method, there shall be a corresponding call to virNetworkFree to release the reference count, once the caller no longer needs the reference to this object.

This method is typically useful for applications where multiple threads are using a connection, and it is required that the connection remain open until all threads have finished using it. ie, each new thread using a network would increment the reference count.

network
the network to hold a reference on
Returns
0 in case of success, -1 in case of failure.

virNetworkSetAutostart

int	virNetworkSetAutostart		(virNetworkPtr network,
					 int autostart)

Configure the network to be automatically started when the host machine boots.

network
a network object
autostart
whether the network should be automatically started 0 or 1
Returns
-1 in case of error, 0 in case of success

virNetworkUndefine

int	virNetworkUndefine		(virNetworkPtr network)

Undefine a network but does not stop it if it is running

network
pointer to a defined network
Returns
0 in case of success, -1 in case of error

virNetworkUpdate

int	virNetworkUpdate		(virNetworkPtr network,
					 unsigned int command,
					 unsigned int section,
					 int parentIndex,
					 const char * xml,
					 unsigned int flags)

Update the definition of an existing network, either its live running state, its persistent configuration, or both.

network
pointer to a defined network
command
what action to perform (add/delete/modify) (see virNetworkUpdateCommand for descriptions)
section
which section of the network to update (see virNetworkUpdateSection for descriptions)
parentIndex
which parent element, if there are multiple parents of the same type (e.g. which <ip> element when modifying a <dhcp>/<host> element), or "-1" for "don't care" or "automatically find appropriate one".
xml
the XML description for the network, preferably in UTF-8
flags
bitwise OR of virNetworkUpdateFlags.
Returns
0 in case of success, -1 in case of error virNetworkUpdateCommand virNetworkUpdateSection