mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 11:35:19 +00:00
util: new function virNetDevIPInfoAddToDev
This patch takes the code out of lxcContainerRenameAndEnableInterfaces() that adds all IP addresses and IP routes to the interface, and puts it into a utility function virNetDevIPInfoAddToDev() in virnetdevip.c so that it can be used by anyone. One small change in functionality - lxcContainerRenameAndEnableInterfaces() previously would add all IP addresses to the interface while it was still offline, then set the interface online, and then add the routes. Because I don't want the utility function to set the interface online, I've moved this up so the interface is first set online, then IP addresses and routes are added. This is the same order that the network service from initscripts (in ifup-ether) does it, so it shouldn't pose any problem (and hasn't, in the tests that I've run).
This commit is contained in:
parent
4ff9ec7dae
commit
f1e0d0da11
@ -1931,6 +1931,7 @@ virNetDevBridgeSetVlanFiltering;
|
|||||||
virNetDevIPAddrAdd;
|
virNetDevIPAddrAdd;
|
||||||
virNetDevIPAddrDel;
|
virNetDevIPAddrDel;
|
||||||
virNetDevIPAddrGet;
|
virNetDevIPAddrGet;
|
||||||
|
virNetDevIPInfoAddToDev;
|
||||||
virNetDevIPInfoClear;
|
virNetDevIPInfoClear;
|
||||||
virNetDevIPRouteAdd;
|
virNetDevIPRouteAdd;
|
||||||
virNetDevIPRouteFree;
|
virNetDevIPRouteFree;
|
||||||
|
@ -490,7 +490,7 @@ lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
|
|||||||
char **veths)
|
char **veths)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
size_t i, j;
|
size_t i;
|
||||||
const char *newname;
|
const char *newname;
|
||||||
virDomainNetDefPtr netDef;
|
virDomainNetDefPtr netDef;
|
||||||
bool privNet = vmDef->features[VIR_DOMAIN_FEATURE_PRIVNET] ==
|
bool privNet = vmDef->features[VIR_DOMAIN_FEATURE_PRIVNET] ==
|
||||||
@ -511,46 +511,21 @@ lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
|
|||||||
if (virNetDevSetName(veths[i], newname) < 0)
|
if (virNetDevSetName(veths[i], newname) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
for (j = 0; j < netDef->guestIP.nips; j++) {
|
/* Only enable this device if there is a reason to do so (either
|
||||||
virNetDevIPAddrPtr ip = netDef->guestIP.ips[j];
|
* at least one IP was specified, or link state was set to up in
|
||||||
int prefix;
|
* the config)
|
||||||
char *ipStr = virSocketAddrFormat(&ip->address);
|
*/
|
||||||
|
|
||||||
if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
|
|
||||||
NULL, ip->prefix)) < 0) {
|
|
||||||
ipStr = virSocketAddrFormat(&ip->address);
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("Failed to determine prefix for IP address '%s'"),
|
|
||||||
ipStr);
|
|
||||||
VIR_FREE(ipStr);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
VIR_FREE(ipStr);
|
|
||||||
|
|
||||||
if (virNetDevIPAddrAdd(newname, &ip->address, NULL, prefix) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (netDef->guestIP.nips ||
|
if (netDef->guestIP.nips ||
|
||||||
netDef->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP) {
|
netDef->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP) {
|
||||||
VIR_DEBUG("Enabling %s", newname);
|
VIR_DEBUG("Enabling %s", newname);
|
||||||
if (virNetDevSetOnline(newname, true) < 0)
|
if (virNetDevSetOnline(newname, true) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
/* Set the routes */
|
/* set IP addresses and routes */
|
||||||
for (j = 0; j < netDef->guestIP.nroutes; j++) {
|
if (virNetDevIPInfoAddToDev(newname, &netDef->guestIP) < 0)
|
||||||
virNetDevIPRoutePtr route = netDef->guestIP.routes[j];
|
|
||||||
|
|
||||||
if (virNetDevIPRouteAdd(newname,
|
|
||||||
virNetDevIPRouteGetAddress(route),
|
|
||||||
virNetDevIPRouteGetPrefix(route),
|
|
||||||
virNetDevIPRouteGetGateway(route),
|
|
||||||
virNetDevIPRouteGetMetric(route)) < 0) {
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* enable lo device only if there were other net devices */
|
/* enable lo device only if there were other net devices */
|
||||||
if ((veths || privNet) &&
|
if ((veths || privNet) &&
|
||||||
|
@ -886,3 +886,63 @@ virNetDevIPInfoClear(virNetDevIPInfoPtr ip)
|
|||||||
virNetDevIPRouteFree(ip->routes[i]);
|
virNetDevIPRouteFree(ip->routes[i]);
|
||||||
VIR_FREE(ip->routes);
|
VIR_FREE(ip->routes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virNetDevIPInfoAddToDev:
|
||||||
|
* @ifname: name of device to operate on
|
||||||
|
* @ipInfo: list of routes and IP addresses to add to this device
|
||||||
|
*
|
||||||
|
* All IP routes and IP addresses in ipInfo are added to the named device.
|
||||||
|
*
|
||||||
|
* Returns: 0 on success, -1 (and error reported) on failure.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
virNetDevIPInfoAddToDev(const char *ifname,
|
||||||
|
virNetDevIPInfo const *ipInfo)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
size_t i;
|
||||||
|
char *ipStr = NULL;
|
||||||
|
int prefix;
|
||||||
|
|
||||||
|
/* add all IP addresses */
|
||||||
|
for (i = 0; i < ipInfo->nips; i++) {
|
||||||
|
virNetDevIPAddrPtr ip = ipInfo->ips[i];
|
||||||
|
|
||||||
|
ipStr = virSocketAddrFormat(&ip->address);
|
||||||
|
if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
|
||||||
|
NULL, ip->prefix)) < 0) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("Failed to determine prefix for IP address '%s'"),
|
||||||
|
ipStr);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
if (virNetDevIPAddrAdd(ifname, &ip->address, NULL, prefix) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
VIR_FREE(ipStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* add all routes */
|
||||||
|
for (i = 0; i < ipInfo->nroutes; i++) {
|
||||||
|
virNetDevIPRoutePtr route = ipInfo->routes[i];
|
||||||
|
|
||||||
|
ipStr = virSocketAddrFormat(&route->address);
|
||||||
|
if ((prefix = virNetDevIPRouteGetPrefix(route)) < 0) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("Failed to determine prefix for route with destination '%s'"),
|
||||||
|
ipStr);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
if (virNetDevIPRouteAdd(ifname, &route->address, prefix,
|
||||||
|
&route->gateway,
|
||||||
|
virNetDevIPRouteGetMetric(route)) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
VIR_FREE(ipStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(ipStr);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -86,5 +86,7 @@ virSocketAddrPtr virNetDevIPRouteGetGateway(virNetDevIPRoutePtr def);
|
|||||||
|
|
||||||
/* virNetDevIPInfo object */
|
/* virNetDevIPInfo object */
|
||||||
void virNetDevIPInfoClear(virNetDevIPInfoPtr ip);
|
void virNetDevIPInfoClear(virNetDevIPInfoPtr ip);
|
||||||
|
int virNetDevIPInfoAddToDev(const char *ifname,
|
||||||
|
virNetDevIPInfo const *ipInfo);
|
||||||
|
|
||||||
#endif /* __VIR_NETDEVIP_H__ */
|
#endif /* __VIR_NETDEVIP_H__ */
|
||||||
|
Loading…
Reference in New Issue
Block a user