1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

Move veth device management into virLXCControllerPtr object

Move the veth device name state into the virLXCControllerPtr
object and stop passing it around. Also use size_t instead
of unsigned int for the array length parameters.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-07-03 12:06:38 +01:00
parent d624ad6a3d
commit ba797c73e6
3 changed files with 59 additions and 34 deletions

View File

@ -93,7 +93,7 @@ typedef struct __lxc_child_argv lxc_child_argv_t;
struct __lxc_child_argv { struct __lxc_child_argv {
virDomainDefPtr config; virDomainDefPtr config;
virSecurityManagerPtr securityDriver; virSecurityManagerPtr securityDriver;
unsigned int nveths; size_t nveths;
char **veths; char **veths;
int monitor; int monitor;
char **ttyPaths; char **ttyPaths;
@ -262,15 +262,15 @@ int lxcContainerWaitForContinue(int control)
* Returns 0 on success or nonzero in case of error * Returns 0 on success or nonzero in case of error
*/ */
static int lxcContainerRenameAndEnableInterfaces(bool privNet, static int lxcContainerRenameAndEnableInterfaces(bool privNet,
unsigned int nveths, size_t nveths,
char **veths) char **veths)
{ {
int rc = 0; int rc = 0;
unsigned int i; size_t i;
char *newname = NULL; char *newname = NULL;
for (i = 0 ; i < nveths ; i++) { for (i = 0 ; i < nveths ; i++) {
if (virAsprintf(&newname, "eth%d", i) < 0) { if (virAsprintf(&newname, "eth%zu", i) < 0) {
virReportOOMError(); virReportOOMError();
rc = -1; rc = -1;
goto error_out; goto error_out;
@ -1775,7 +1775,7 @@ const char *lxcContainerGetAlt32bitArch(const char *arch)
*/ */
int lxcContainerStart(virDomainDefPtr def, int lxcContainerStart(virDomainDefPtr def,
virSecurityManagerPtr securityDriver, virSecurityManagerPtr securityDriver,
unsigned int nveths, size_t nveths,
char **veths, char **veths,
int control, int control,
int handshakefd, int handshakefd,

View File

@ -51,7 +51,7 @@ int lxcContainerWaitForContinue(int control);
int lxcContainerStart(virDomainDefPtr def, int lxcContainerStart(virDomainDefPtr def,
virSecurityManagerPtr securityDriver, virSecurityManagerPtr securityDriver,
unsigned int nveths, size_t nveths,
char **veths, char **veths,
int control, int control,
int handshakefd, int handshakefd,

View File

@ -85,6 +85,9 @@ typedef virLXCController *virLXCControllerPtr;
struct _virLXCController { struct _virLXCController {
char *name; char *name;
virDomainDefPtr def; virDomainDefPtr def;
size_t nveths;
char **veths;
}; };
static void virLXCControllerFree(virLXCControllerPtr ctrl); static void virLXCControllerFree(virLXCControllerPtr ctrl);
@ -129,15 +132,35 @@ error:
static void virLXCControllerFree(virLXCControllerPtr ctrl) static void virLXCControllerFree(virLXCControllerPtr ctrl)
{ {
size_t i;
if (!ctrl) if (!ctrl)
return; return;
for (i = 0 ; i < ctrl->nveths ; i++)
VIR_FREE(ctrl->veths[i]);
VIR_FREE(ctrl->veths);
virDomainDefFree(ctrl->def); virDomainDefFree(ctrl->def);
VIR_FREE(ctrl->name); VIR_FREE(ctrl->name);
VIR_FREE(ctrl); VIR_FREE(ctrl);
} }
static int virLXCControllerValidateNICs(virLXCControllerPtr ctrl)
{
if (ctrl->def->nnets != ctrl->nveths) {
lxcError(VIR_ERR_INTERNAL_ERROR,
_("expecting %d veths, but got %zu"),
ctrl->def->nnets, ctrl->nveths);
return -1;
}
return 0;
}
static int lxcGetLoopFD(char **dev_name) static int lxcGetLoopFD(char **dev_name)
{ {
int fd = -1; int fd = -1;
@ -1307,7 +1330,7 @@ cleanup2:
/** /**
* lxcControllerMoveInterfaces * virLXCControllerMoveInterfaces
* @nveths: number of interfaces * @nveths: number of interfaces
* @veths: interface names * @veths: interface names
* @container: pid of container * @container: pid of container
@ -1316,38 +1339,42 @@ cleanup2:
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
static int lxcControllerMoveInterfaces(unsigned int nveths, static int virLXCControllerMoveInterfaces(virLXCControllerPtr ctrl,
char **veths,
pid_t container) pid_t container)
{ {
unsigned int i; size_t i;
for (i = 0 ; i < nveths ; i++)
if (virNetDevSetNamespace(veths[i], container) < 0) for (i = 0 ; i < ctrl->nveths ; i++) {
if (virNetDevSetNamespace(ctrl->veths[i], container) < 0)
return -1; return -1;
}
return 0; return 0;
} }
/** /**
* lxcCleanupInterfaces: * virLXCControllerDeleteInterfaces:
* @nveths: number of interfaces * @ctrl: the LXC controller
* @veths: interface names
* *
* Cleans up the container interfaces by deleting the veth device pairs. * Cleans up the container interfaces by deleting the veth device pairs.
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
static int lxcControllerCleanupInterfaces(unsigned int nveths, static int virLXCControllerDeleteInterfaces(virLXCControllerPtr ctrl)
char **veths)
{ {
unsigned int i; size_t i;
for (i = 0 ; i < nveths ; i++) int ret = 0;
ignore_value(virNetDevVethDelete(veths[i]));
return 0; for (i = 0 ; i < ctrl->nveths ; i++) {
if (virNetDevVethDelete(ctrl->veths[i]) < 0)
ret = -1;
}
return ret;
} }
static int lxcSetPersonality(virDomainDefPtr def) static int lxcSetPersonality(virDomainDefPtr def)
{ {
struct utsname utsname; struct utsname utsname;
@ -1422,8 +1449,6 @@ cleanup:
static int static int
virLXCControllerRun(virLXCControllerPtr ctrl, virLXCControllerRun(virLXCControllerPtr ctrl,
virSecurityManagerPtr securityDriver, virSecurityManagerPtr securityDriver,
unsigned int nveths,
char **veths,
int monitor, int monitor,
int client, int client,
int *ttyFDs, int *ttyFDs,
@ -1588,8 +1613,8 @@ virLXCControllerRun(virLXCControllerPtr ctrl,
if ((container = lxcContainerStart(ctrl->def, if ((container = lxcContainerStart(ctrl->def,
securityDriver, securityDriver,
nveths, ctrl->nveths,
veths, ctrl->veths,
control[1], control[1],
containerhandshake[1], containerhandshake[1],
containerTtyPaths, containerTtyPaths,
@ -1598,7 +1623,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl,
VIR_FORCE_CLOSE(control[1]); VIR_FORCE_CLOSE(control[1]);
VIR_FORCE_CLOSE(containerhandshake[1]); VIR_FORCE_CLOSE(containerhandshake[1]);
if (lxcControllerMoveInterfaces(nveths, veths, container) < 0) if (virLXCControllerMoveInterfaces(ctrl, container) < 0)
goto cleanup; goto cleanup;
if (lxcContainerSendContinue(control[0]) < 0) { if (lxcContainerSendContinue(control[0]) < 0) {
@ -1682,7 +1707,7 @@ int main(int argc, char *argv[])
int rc = 1; int rc = 1;
int client; int client;
char *name = NULL; char *name = NULL;
int nveths = 0; size_t nveths = 0;
char **veths = NULL; char **veths = NULL;
int monitor = -1; int monitor = -1;
int handshakefd = -1; int handshakefd = -1;
@ -1831,11 +1856,11 @@ int main(int argc, char *argv[])
NULLSTR(ctrl->def->seclabel.label), NULLSTR(ctrl->def->seclabel.label),
NULLSTR(ctrl->def->seclabel.imagelabel)); NULLSTR(ctrl->def->seclabel.imagelabel));
if (ctrl->def->nnets != nveths) { ctrl->veths = veths;
fprintf(stderr, "%s: expecting %d veths, but got %d\n", ctrl->nveths = nveths;
argv[0], ctrl->def->nnets, nveths);
if (virLXCControllerValidateNICs(ctrl) < 0)
goto cleanup; goto cleanup;
}
if ((sockpath = lxcMonitorPath(ctrl)) == NULL) if ((sockpath = lxcMonitorPath(ctrl)) == NULL)
goto cleanup; goto cleanup;
@ -1883,12 +1908,12 @@ int main(int argc, char *argv[])
} }
rc = virLXCControllerRun(ctrl, securityDriver, rc = virLXCControllerRun(ctrl, securityDriver,
nveths, veths, monitor, client, monitor, client,
ttyFDs, nttyFDs, handshakefd); ttyFDs, nttyFDs, handshakefd);
cleanup: cleanup:
virPidFileDelete(LXC_STATE_DIR, name); virPidFileDelete(LXC_STATE_DIR, name);
lxcControllerCleanupInterfaces(nveths, veths); virLXCControllerDeleteInterfaces(ctrl);
if (sockpath) if (sockpath)
unlink(sockpath); unlink(sockpath);
VIR_FREE(sockpath); VIR_FREE(sockpath);