network: set macvtap/hostdev networks active if their state file exists

libvirt attempts to determine at startup time which networks are
already active, and set their active flags. Previously it has done
this by assuming that all networks are inactive, then setting the
active flag if the network has a bridge device associated with it and
that bridge device exists. This is not useful for macvtap and hostdev
based networks, since they do not use a bridge device.

Of course the reason that such a check had to be done was that the
presence of a status file in the network "stateDir" couldn't be
trusted as an indicator of whether or not a network was active. This
was due to the network driver mistakenly using
/var/lib/libvirt/network to store the status files, rather than
/var/run/libvirt/network (similar to what is done by every other
libvirt driver that stores status xml for its objects). The difference
is that /var/run is cleared out when the host reboots, so you can be
assured that the state file you are seeing isn't just left over from a
previous boot of the host.

Now that the network driver has been switched to using
/var/run/libvirt/network for status, we can also modify it to assume
that any network with an existing status file is by definition active
- we do this when reading the status file. To fine tune the results,
networkFindActiveConfigs() is changed to networkUpdateAllState(),
and only sets active = 0 if the conditions for particular network
types are *not* met.

The result is that during the first run of libvirtd after the host
boots, there are no status files, so no networks are active. Any time
libvirtd is restarted, any network with a status file will be marked
as active (unless the network uses a bridge device and that device for
some reason doesn't exist).
This commit is contained in:
Laine Stump 2014-04-09 17:16:45 +03:00
parent b9e95491d1
commit 411c548668
2 changed files with 52 additions and 20 deletions

View File

@ -3074,6 +3074,7 @@ virNetworkLoadState(virNetworkObjListPtr nets,
net->floor_sum = floor_sum_val;
net->taint = taint;
net->active = 1; /* any network with a state file is by definition active */
cleanup:
VIR_FREE(configFile);

View File

@ -328,38 +328,69 @@ networkBridgeDummyNicName(const char *brname)
return nicname;
}
/* Update the internal status of all allegedly active networks
* according to external conditions on the host (i.e. anything that
* isn't stored directly in each network's state file). */
static void
networkFindActiveConfigs(virNetworkDriverStatePtr driver)
networkUpdateAllState(virNetworkDriverStatePtr driver)
{
size_t i;
for (i = 0; i < driver->networks.count; i++) {
virNetworkObjPtr obj = driver->networks.objs[i];
if (!obj->active)
continue;
virNetworkObjLock(obj);
/* If bridge exists, then mark it active */
if (obj->def->bridge &&
virNetDevExists(obj->def->bridge) == 1) {
obj->active = 1;
switch (obj->def->forward.type) {
case VIR_NETWORK_FORWARD_NONE:
case VIR_NETWORK_FORWARD_NAT:
case VIR_NETWORK_FORWARD_ROUTE:
/* If bridge doesn't exist, then mark it inactive */
if (!(obj->def->bridge && virNetDevExists(obj->def->bridge) == 1))
obj->active = 0;
break;
/* Try and read dnsmasq/radvd pids if any */
if (obj->def->ips && (obj->def->nips > 0)) {
char *radvdpidbase;
ignore_value(virPidFileReadIfAlive(driverState->pidDir, obj->def->name,
&obj->dnsmasqPid,
dnsmasqCapsGetBinaryPath(driver->dnsmasqCaps)));
if (!(radvdpidbase = networkRadvdPidfileBasename(obj->def->name)))
goto cleanup;
ignore_value(virPidFileReadIfAlive(driverState->pidDir, radvdpidbase,
&obj->radvdPid, RADVD));
VIR_FREE(radvdpidbase);
case VIR_NETWORK_FORWARD_BRIDGE:
if (obj->def->bridge) {
if (virNetDevExists(obj->def->bridge) != 1)
obj->active = 0;
break;
}
/* intentionally drop through to common case for all
* macvtap networks (forward='bridge' with no bridge
* device defined is macvtap using its 'bridge' mode)
*/
case VIR_NETWORK_FORWARD_PRIVATE:
case VIR_NETWORK_FORWARD_VEPA:
case VIR_NETWORK_FORWARD_PASSTHROUGH:
/* so far no extra checks */
break;
case VIR_NETWORK_FORWARD_HOSTDEV:
/* so far no extra checks */
break;
}
/* Try and read dnsmasq/radvd pids of active networks */
if (obj->active && obj->def->ips && (obj->def->nips > 0)) {
char *radvdpidbase;
ignore_value(virPidFileReadIfAlive(driverState->pidDir,
obj->def->name,
&obj->dnsmasqPid,
dnsmasqCapsGetBinaryPath(driver->dnsmasqCaps)));
radvdpidbase = networkRadvdPidfileBasename(obj->def->name);
if (!radvdpidbase)
break;
ignore_value(virPidFileReadIfAlive(driverState->pidDir,
radvdpidbase,
&obj->radvdPid, RADVD));
VIR_FREE(radvdpidbase);
}
cleanup:
virNetworkObjUnlock(obj);
}
@ -591,7 +622,7 @@ networkStateInitialize(bool privileged,
driverState->networkAutostartDir) < 0)
goto error;
networkFindActiveConfigs(driverState);
networkUpdateAllState(driverState);
networkReloadFirewallRules(driverState);
networkRefreshDaemons(driverState);