| 1 |
# -*- perl -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2006 Red Hat |
| 4 |
# Copyright (C) 2006-2007 Daniel P. Berrange |
| 5 |
# |
| 6 |
# This program is free software; You can redistribute it and/or modify |
| 7 |
# it under either: |
| 8 |
# |
| 9 |
# a) the GNU General Public License as published by the Free |
| 10 |
# Software Foundation; either version 2, or (at your option) any |
| 11 |
# later version, |
| 12 |
# |
| 13 |
# or |
| 14 |
# |
| 15 |
# b) the "Artistic License" |
| 16 |
# |
| 17 |
# The file "LICENSE" distributed along with this file provides full |
| 18 |
# details of the terms and conditions of the two licenses. |
| 19 |
|
| 20 |
=pod |
| 21 |
|
| 22 |
=head1 NAME |
| 23 |
|
| 24 |
Sys::Virt::Network - Represent & manage a libvirt virtual network |
| 25 |
|
| 26 |
=head1 DESCRIPTION |
| 27 |
|
| 28 |
The C<Sys::Virt::Network> module represents a virtual network managed |
| 29 |
by the virtual machine monitor. |
| 30 |
|
| 31 |
=head1 METHODS |
| 32 |
|
| 33 |
=over 4 |
| 34 |
|
| 35 |
=cut |
| 36 |
|
| 37 |
package Sys::Virt::Network; |
| 38 |
|
| 39 |
use strict; |
| 40 |
use warnings; |
| 41 |
|
| 42 |
|
| 43 |
sub _new { |
| 44 |
my $proto = shift; |
| 45 |
my $class = ref($proto) || $proto; |
| 46 |
my %params = @_; |
| 47 |
|
| 48 |
my $con = exists $params{connection} ? $params{connection} : die "connection parameter is required"; |
| 49 |
my $self; |
| 50 |
if (exists $params{name}) { |
| 51 |
$self = Sys::Virt::Network::_lookup_by_name($con, $params{name}); |
| 52 |
} elsif (exists $params{uuid}) { |
| 53 |
if (length($params{uuid}) == 16) { |
| 54 |
$self = Sys::Virt::Network::_lookup_by_uuid($con, $params{uuid}); |
| 55 |
} elsif (length($params{uuid}) == 32 || |
| 56 |
length($params{uuid}) == 36) { |
| 57 |
$self = Sys::Virt::Network::_lookup_by_uuid_string($con, $params{uuid}); |
| 58 |
} else { |
| 59 |
die "UUID must be either 16 unsigned bytes, or 32/36 hex characters long"; |
| 60 |
} |
| 61 |
} elsif (exists $params{xml}) { |
| 62 |
if ($params{nocreate}) { |
| 63 |
$self = Sys::Virt::Network::_define_xml($con, $params{xml}, $params{flags}); |
| 64 |
} else { |
| 65 |
$self = Sys::Virt::Network::_create_xml($con, $params{xml}, $params{flags}); |
| 66 |
} |
| 67 |
} else { |
| 68 |
die "address, id or uuid parameters are required"; |
| 69 |
} |
| 70 |
|
| 71 |
bless $self, $class; |
| 72 |
|
| 73 |
return $self; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
=item my $uuid = $net->get_uuid() |
| 78 |
|
| 79 |
Returns a 16 byte long string containing the raw globally unique identifier |
| 80 |
(UUID) for the network. |
| 81 |
|
| 82 |
=item my $uuid = $net->get_uuid_string() |
| 83 |
|
| 84 |
Returns a printable string representation of the raw UUID, in the format |
| 85 |
'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'. |
| 86 |
|
| 87 |
=item my $name = $net->get_name() |
| 88 |
|
| 89 |
Returns a string with a locally unique name of the network |
| 90 |
|
| 91 |
=item $net->is_active() |
| 92 |
|
| 93 |
Returns a true value if the network is currently running |
| 94 |
|
| 95 |
=item $net->is_persistent() |
| 96 |
|
| 97 |
Returns a true value if the network has a persistent configuration |
| 98 |
file defined |
| 99 |
|
| 100 |
=item my $xml = $net->get_xml_description() |
| 101 |
|
| 102 |
Returns an XML document containing a complete description of |
| 103 |
the network's configuration |
| 104 |
|
| 105 |
=item $net->create() |
| 106 |
|
| 107 |
Start a network whose configuration was previously defined using the |
| 108 |
C<define_network> method in L<Sys::Virt>. |
| 109 |
|
| 110 |
=item $net->undefine() |
| 111 |
|
| 112 |
Remove the configuration associated with a network previously defined |
| 113 |
with the C<define_network> method in L<Sys::Virt>. If the network is |
| 114 |
running, you probably want to use the C<shutdown> or C<destroy> |
| 115 |
methods instead. |
| 116 |
|
| 117 |
=item $net->destroy() |
| 118 |
|
| 119 |
Immediately terminate the machine, and remove it from the virtual |
| 120 |
machine monitor. The C<$net> handle is invalid after this call |
| 121 |
completes and should not be used again. |
| 122 |
|
| 123 |
=item $net->update($command, $section, $parentIndex, $xml, $flags=0) |
| 124 |
|
| 125 |
Update the network configuration with C<$xml>. The C<$section> parameter, |
| 126 |
which must be one of the XML SECTION CONSTANTS listed later, indicates |
| 127 |
what schema is used in C<$xml>. The C<$command> parameter determines |
| 128 |
what action is taken. Finally, the C<$flags> parameter can be use to |
| 129 |
control which config is affected. |
| 130 |
|
| 131 |
=item $net->get_bridge_name() |
| 132 |
|
| 133 |
Return the name of the bridge device associated with the virtual |
| 134 |
network |
| 135 |
|
| 136 |
=item $flag = $net->get_autostart(); |
| 137 |
|
| 138 |
Return a true value if the virtual network is configured to automatically |
| 139 |
start upon boot. Return false, otherwise |
| 140 |
|
| 141 |
=item $net->set_autostart($flag) |
| 142 |
|
| 143 |
Set the state of the autostart flag, which determines whether the |
| 144 |
virtual network will automatically start upon boot of the host OS. |
| 145 |
|
| 146 |
=item @leases = $net->get_dhcp_leases($mac=undef, $flags=0) |
| 147 |
|
| 148 |
Get a list of all active DHCP leases. If C<$mac> is undefined than |
| 149 |
leases for all VMs are returned, otherwise only leases for the |
| 150 |
matching MAC address are returned. The C<$flags> parameter is |
| 151 |
currently unused and defaults to zero. |
| 152 |
|
| 153 |
The elements in the returned array are hash references with |
| 154 |
the following fields |
| 155 |
|
| 156 |
=over 4 |
| 157 |
|
| 158 |
=item C<iface> |
| 159 |
|
| 160 |
Network interface name |
| 161 |
|
| 162 |
=item C<expirytime> |
| 163 |
|
| 164 |
Seconds since the epoch until the lease expires |
| 165 |
|
| 166 |
=item C<type> |
| 167 |
|
| 168 |
One of the Sys::Virt IP address type constants |
| 169 |
|
| 170 |
=item C<mac> |
| 171 |
|
| 172 |
The MAC address of the lease |
| 173 |
|
| 174 |
=item C<iaid> |
| 175 |
|
| 176 |
The IAID of the client |
| 177 |
|
| 178 |
=item C<ipaddr> |
| 179 |
|
| 180 |
The IP address of the lease |
| 181 |
|
| 182 |
=item C<prefix> |
| 183 |
|
| 184 |
The IP address prefix |
| 185 |
|
| 186 |
=item C<hostname> |
| 187 |
|
| 188 |
The optional hostname associated with the lease |
| 189 |
|
| 190 |
=item C<clientid> |
| 191 |
|
| 192 |
The client ID or DUID |
| 193 |
|
| 194 |
=back |
| 195 |
|
| 196 |
=item @port = $net->list_all_ports($flags=0) |
| 197 |
|
| 198 |
List all ports associated with the network. The return array elements |
| 199 |
are instances of the C<Sys::Virt::NetworkPort> class. |
| 200 |
|
| 201 |
=item $port = $net->create_port($xml, $flags=0) |
| 202 |
|
| 203 |
Create a new network port from the given C<$xml> description. The C<$flags> |
| 204 |
parameter can optionally taken one or more of the network port creation |
| 205 |
constants. The returned C<$port> object is an instance of the |
| 206 |
C<Sys::Virt::NetworkPort> class. |
| 207 |
|
| 208 |
=cut |
| 209 |
|
| 210 |
sub create_port { |
| 211 |
my $self = shift; |
| 212 |
my $xml = shift; |
| 213 |
my $flags = shift || 0; |
| 214 |
|
| 215 |
return Sys::Virt::NetworkPort->_new(network => $self, xml => $xml, flags => $flags); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
=item $port = $net->get_port_by_uuid($uuid); |
| 220 |
|
| 221 |
Lookup a network port from a raw or printable UUID. |
| 222 |
The returned C<$port> object is an instance of the C<Sys::Virt::NetworkPort> |
| 223 |
class. |
| 224 |
|
| 225 |
=cut |
| 226 |
|
| 227 |
sub get_port_by_uuid { |
| 228 |
my $self = shift; |
| 229 |
my $uuid = shift; |
| 230 |
|
| 231 |
return Sys::Virt::NetworkPort->_new(network => $self, uuid => $uuid); |
| 232 |
} |
| 233 |
|
| 234 |
=item my $str = $net->get_metadata($type, $uri, $flags =0) |
| 235 |
|
| 236 |
Returns the metadata element of type C<$type> associated |
| 237 |
with the network. If C<$type> is C<Sys::Virt::Network::METADATA_ELEMENT> |
| 238 |
then the C<$uri> parameter specifies the XML namespace to |
| 239 |
retrieve, otherwise C<$uri> should be C<undef>. The optional |
| 240 |
C<$flags> parameter takes one of the XML UPDATE FLAGS values, |
| 241 |
and defaults to zero. |
| 242 |
|
| 243 |
=item $net->set_metadata($type, $val, $key, $uri, $flags=0) |
| 244 |
|
| 245 |
Sets the metadata element of type C<$type> to hold the value |
| 246 |
C<$val>. If C<$type> is C<Sys::Virt::Network::METADATA_ELEMENT> |
| 247 |
then the C<$key> and C<$uri> elements specify an XML namespace |
| 248 |
to use, otherwise they should both be C<undef>. The optional |
| 249 |
C<$flags> parameter takes one of the XML UPDATE FLAGS values, |
| 250 |
and defaults to zero. |
| 251 |
|
| 252 |
=back |
| 253 |
|
| 254 |
=head1 CONSTANTS |
| 255 |
|
| 256 |
This section documents constants that are used with various |
| 257 |
APIs described above |
| 258 |
|
| 259 |
=head2 NETWORK CREATION CONSTANTS |
| 260 |
|
| 261 |
When creating networks zero or more of the following |
| 262 |
constants may be used |
| 263 |
|
| 264 |
=over 4 |
| 265 |
|
| 266 |
=item Sys::Virt::Network::CREATE_VALIDATE |
| 267 |
|
| 268 |
Validate the XML document against the XML schema |
| 269 |
|
| 270 |
=back |
| 271 |
|
| 272 |
=head2 LIST FILTERING |
| 273 |
|
| 274 |
The following constants are used to filter object lists |
| 275 |
|
| 276 |
=over 4 |
| 277 |
|
| 278 |
=item Sys::Virt::Network::LIST_ACTIVE |
| 279 |
|
| 280 |
Include networks which are active |
| 281 |
|
| 282 |
=item Sys::Virt::Network::LIST_INACTIVE |
| 283 |
|
| 284 |
Include networks which are not active |
| 285 |
|
| 286 |
=item Sys::Virt::Network::LIST_AUTOSTART |
| 287 |
|
| 288 |
Include networks which are set to autostart |
| 289 |
|
| 290 |
=item Sys::Virt::Network::LIST_NO_AUTOSTART |
| 291 |
|
| 292 |
Include networks which are not set to autostart |
| 293 |
|
| 294 |
=item Sys::Virt::Network::LIST_PERSISTENT |
| 295 |
|
| 296 |
Include networks which are persistent |
| 297 |
|
| 298 |
=item Sys::Virt::Network::LIST_TRANSIENT |
| 299 |
|
| 300 |
Include networks which are transient |
| 301 |
|
| 302 |
=back |
| 303 |
|
| 304 |
=head2 NETWORK DEFINE |
| 305 |
|
| 306 |
The following constants can be used to control the behaviour |
| 307 |
of network define operations |
| 308 |
|
| 309 |
=over 4 |
| 310 |
|
| 311 |
=item Sys::Virt::Network::DEFINE_VALIDATE |
| 312 |
|
| 313 |
Validate the XML document against the XML schema |
| 314 |
|
| 315 |
=back |
| 316 |
|
| 317 |
=head2 XML CONSTANTS |
| 318 |
|
| 319 |
The following constants are used when querying XML |
| 320 |
|
| 321 |
=over 4 |
| 322 |
|
| 323 |
=item Sys::Virt::Network::XML_INACTIVE |
| 324 |
|
| 325 |
Request the inactive XML, instead of the current possibly live config. |
| 326 |
|
| 327 |
=back |
| 328 |
|
| 329 |
=head1 XML SECTION CONSTANTS |
| 330 |
|
| 331 |
The following constants are used to refer to sections |
| 332 |
of the XML document |
| 333 |
|
| 334 |
=over 4 |
| 335 |
|
| 336 |
=item Sys::Virt::Network::SECTION_BRIDGE |
| 337 |
|
| 338 |
The bridge device element |
| 339 |
|
| 340 |
=item Sys::Virt::Network::SECTION_DNS_HOST |
| 341 |
|
| 342 |
The DNS host record section |
| 343 |
|
| 344 |
=item Sys::Virt::Network::SECTION_DNS_SRV |
| 345 |
|
| 346 |
The DNS SRV record section |
| 347 |
|
| 348 |
=item Sys::Virt::Network::SECTION_DNS_TXT |
| 349 |
|
| 350 |
The DNS TXT record section |
| 351 |
|
| 352 |
=item Sys::Virt::Network::SECTION_DOMAIN |
| 353 |
|
| 354 |
The domain name section |
| 355 |
|
| 356 |
=item Sys::Virt::Network::SECTION_FORWARD |
| 357 |
|
| 358 |
The forward device section |
| 359 |
|
| 360 |
=item Sys::Virt::Network::SECTION_FORWARD_INTERFACE |
| 361 |
|
| 362 |
The forward interface section |
| 363 |
|
| 364 |
=item Sys::Virt::Network::SECTION_FORWARD_PF |
| 365 |
|
| 366 |
The forward physical function section |
| 367 |
|
| 368 |
=item Sys::Virt::Network::SECTION_IP |
| 369 |
|
| 370 |
The IP address section |
| 371 |
|
| 372 |
=item Sys::Virt::Network::SECTION_IP_DHCP_HOST |
| 373 |
|
| 374 |
The IP address DHCP host section |
| 375 |
|
| 376 |
=item Sys::Virt::Network::SECTION_IP_DHCP_RANGE |
| 377 |
|
| 378 |
The IP address DHCP range section |
| 379 |
|
| 380 |
=item Sys::Virt::Network::SECTION_PORTGROUP |
| 381 |
|
| 382 |
The port group section |
| 383 |
|
| 384 |
=item Sys::Virt::Network::SECTION_NONE |
| 385 |
|
| 386 |
The top level domain element |
| 387 |
|
| 388 |
=back |
| 389 |
|
| 390 |
=head2 XML UPDATE FLAGS |
| 391 |
|
| 392 |
=over 4 |
| 393 |
|
| 394 |
=item Sys::Virt::Network::UPDATE_AFFECT_CURRENT |
| 395 |
|
| 396 |
Affect whatever the current object state is |
| 397 |
|
| 398 |
=item Sys::Virt::Network::UPDATE_AFFECT_CONFIG |
| 399 |
|
| 400 |
Always update the config file |
| 401 |
|
| 402 |
=item Sys::Virt::Network::UPDATE_AFFECT_LIVE |
| 403 |
|
| 404 |
Always update the live config |
| 405 |
|
| 406 |
=back |
| 407 |
|
| 408 |
=head2 XML UPDATE COMMANDS |
| 409 |
|
| 410 |
=over 4 |
| 411 |
|
| 412 |
=item Sys::Virt::Network::UPDATE_COMMAND_NONE |
| 413 |
|
| 414 |
No update |
| 415 |
|
| 416 |
=item Sys::Virt::Network::UPDATE_COMMAND_DELETE |
| 417 |
|
| 418 |
Remove the matching entry |
| 419 |
|
| 420 |
=item Sys::Virt::Network::UPDATE_COMMAND_MODIFY |
| 421 |
|
| 422 |
Modify the matching entry |
| 423 |
|
| 424 |
=item Sys::Virt::Network::UPDATE_COMMAND_ADD_FIRST |
| 425 |
|
| 426 |
Insert the matching entry at the start |
| 427 |
|
| 428 |
=item Sys::Virt::Network::UPDATE_COMMAND_ADD_LAST |
| 429 |
|
| 430 |
Insert the matching entry at the end |
| 431 |
|
| 432 |
=back |
| 433 |
|
| 434 |
=head2 EVENT ID CONSTANTS |
| 435 |
|
| 436 |
=over 4 |
| 437 |
|
| 438 |
=item Sys::Virt::Network::EVENT_ID_LIFECYCLE |
| 439 |
|
| 440 |
Network lifecycle events |
| 441 |
|
| 442 |
=item Sys::Virt::Network::EVENT_ID_METADATA_CHANGE |
| 443 |
|
| 444 |
The network metadata has changed |
| 445 |
|
| 446 |
=back |
| 447 |
|
| 448 |
=head2 LIFECYCLE CHANGE EVENTS |
| 449 |
|
| 450 |
The following constants allow network lifecycle change events to be |
| 451 |
interpreted. The events contain both a state change, and a |
| 452 |
reason though the reason is currently unused. |
| 453 |
|
| 454 |
=over 4 |
| 455 |
|
| 456 |
=item Sys::Virt::Network::EVENT_DEFINED |
| 457 |
|
| 458 |
Indicates that a persistent configuration has been defined for |
| 459 |
the network. |
| 460 |
|
| 461 |
=item Sys::Virt::Network::EVENT_STARTED |
| 462 |
|
| 463 |
The network has started running |
| 464 |
|
| 465 |
=item Sys::Virt::Network::EVENT_STOPPED |
| 466 |
|
| 467 |
The network has stopped running |
| 468 |
|
| 469 |
=item Sys::Virt::Network::EVENT_UNDEFINED |
| 470 |
|
| 471 |
The persistent configuration has gone away |
| 472 |
|
| 473 |
=back |
| 474 |
|
| 475 |
=head2 METADATA TYPE CONSTANTS |
| 476 |
|
| 477 |
The following constants control the type of metadata being |
| 478 |
accessed |
| 479 |
|
| 480 |
=over 4 |
| 481 |
|
| 482 |
=item Sys::Virt::Network::METADATA_TITLE |
| 483 |
|
| 484 |
The short human friendly title of the network |
| 485 |
|
| 486 |
=item Sys::Virt::Network::METADATA_DESCRIPTION |
| 487 |
|
| 488 |
The long free text description of the network |
| 489 |
|
| 490 |
=item Sys::Virt::Network::METADATA_ELEMENT |
| 491 |
|
| 492 |
The structured metadata elements for the network |
| 493 |
|
| 494 |
=back |
| 495 |
|
| 496 |
=cut |
| 497 |
|
| 498 |
|
| 499 |
1; |
| 500 |
|
| 501 |
|
| 502 |
=head1 AUTHORS |
| 503 |
|
| 504 |
Daniel P. Berrange <[email protected]> |
| 505 |
|
| 506 |
=head1 COPYRIGHT |
| 507 |
|
| 508 |
Copyright (C) 2006 Red Hat |
| 509 |
Copyright (C) 2006-2007 Daniel P. Berrange |
| 510 |
|
| 511 |
=head1 LICENSE |
| 512 |
|
| 513 |
This program is free software; you can redistribute it and/or modify |
| 514 |
it under the terms of either the GNU General Public License as published |
| 515 |
by the Free Software Foundation (either version 2 of the License, or at |
| 516 |
your option any later version), or, the Artistic License, as specified |
| 517 |
in the Perl README file. |
| 518 |
|
| 519 |
=head1 SEE ALSO |
| 520 |
|
| 521 |
L<Sys::Virt>, L<Sys::Virt::Error>, C<http://libvirt.org> |
| 522 |
|
| 523 |
=cut |