domain_conf: Introduce chardev hotplug helpers

For now, only these three helpers are needed:
virDomainChrFind - to find a duplicate chardev within VM def
virDomainChrInsert - wrapper for inserting a new chardev into VM def
virDomainChrRemove - wrapper for removing chardev from VM def

There is, however, one internal helper as well:
virDomainChrGetDomainPtrs which sets given pointers to one of
vmdef->{parallels,serials,consoles,channels} based on passed
chardev type.
This commit is contained in:
Michal Privoznik 2013-03-12 15:55:07 +01:00
parent 273745b431
commit 8d4c3c3f81
3 changed files with 149 additions and 0 deletions

View File

@ -10055,6 +10055,136 @@ virDomainLeaseRemove(virDomainDefPtr def,
return virDomainLeaseRemoveAt(def, idx); return virDomainLeaseRemoveAt(def, idx);
} }
static bool
virDomainChrEquals(virDomainChrDefPtr src,
virDomainChrDefPtr tgt)
{
if (!src || !tgt)
return src == tgt;
if (src->deviceType != tgt->deviceType ||
!virDomainChrSourceDefIsEqual(&src->source, &tgt->source))
return false;
switch ((enum virDomainChrDeviceType) src->deviceType) {
case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
if (src->targetType != tgt->targetType)
return false;
switch ((enum virDomainChrChannelTargetType) src->targetType) {
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
return STREQ_NULLABLE(src->target.name, tgt->target.name);
break;
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD:
if (!src->target.addr || !tgt->target.addr)
return src->target.addr == tgt->target.addr;
return memcmp(src->target.addr, tgt->target.addr,
sizeof(*src->target.addr)) == 0;
break;
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_NONE:
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_LAST:
/* shouldn't happen */
break;
}
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
if (src->targetTypeAttr != tgt->targetTypeAttr)
return false;
case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
return src->target.port == tgt->target.port;
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
/* shouldn't happen */
break;
}
return false;
}
virDomainChrDefPtr
virDomainChrFind(virDomainDefPtr def,
virDomainChrDefPtr target)
{
virDomainChrDefPtr chr, **arrPtr;
size_t i, *cntPtr;
virDomainChrGetDomainPtrs(def, target, &arrPtr, &cntPtr);
for (i = 0; i < *cntPtr; i++) {
chr = (*arrPtr)[i];
if (virDomainChrEquals(chr, target))
return chr;
}
return NULL;
}
void
virDomainChrGetDomainPtrs(virDomainDefPtr vmdef,
virDomainChrDefPtr chr,
virDomainChrDefPtr ***arrPtr,
size_t **cntPtr)
{
switch ((enum virDomainChrDeviceType) chr->deviceType) {
case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
*arrPtr = &vmdef->parallels;
*cntPtr = &vmdef->nparallels;
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
*arrPtr = &vmdef->serials;
*cntPtr = &vmdef->nserials;
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
*arrPtr = &vmdef->consoles;
*cntPtr = &vmdef->nconsoles;
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
*arrPtr = &vmdef->channels;
*cntPtr = &vmdef->nchannels;
break;
case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
break;
}
}
int
virDomainChrInsert(virDomainDefPtr vmdef,
virDomainChrDefPtr chr)
{
virDomainChrDefPtr **arrPtr;
size_t *cntPtr;
virDomainChrGetDomainPtrs(vmdef, chr, &arrPtr, &cntPtr);
return VIR_APPEND_ELEMENT(*arrPtr, *cntPtr, chr);
}
virDomainChrDefPtr
virDomainChrRemove(virDomainDefPtr vmdef,
virDomainChrDefPtr chr)
{
virDomainChrDefPtr ret, **arrPtr;
size_t i, *cntPtr;
virDomainChrGetDomainPtrs(vmdef, chr, &arrPtr, &cntPtr);
for (i = 0; i < *cntPtr; i++) {
ret = (*arrPtr)[i];
if (virDomainChrEquals(ret, chr))
break;
}
if (i == *cntPtr)
return NULL;
VIR_DELETE_ELEMENT(*arrPtr, i, *cntPtr);
return ret;
}
char * char *
virDomainDefGetDefaultEmulator(virDomainDefPtr def, virDomainDefGetDefaultEmulator(virDomainDefPtr def,

View File

@ -2391,6 +2391,21 @@ virDomainLeaseDefPtr
virDomainLeaseRemove(virDomainDefPtr def, virDomainLeaseRemove(virDomainDefPtr def,
virDomainLeaseDefPtr lease); virDomainLeaseDefPtr lease);
void
virDomainChrGetDomainPtrs(virDomainDefPtr vmdef,
virDomainChrDefPtr chr,
virDomainChrDefPtr ***arrPtr,
size_t **cntPtr);
virDomainChrDefPtr
virDomainChrFind(virDomainDefPtr def,
virDomainChrDefPtr target);
int
virDomainChrInsert(virDomainDefPtr vmdef,
virDomainChrDefPtr chr);
virDomainChrDefPtr
virDomainChrRemove(virDomainDefPtr vmdef,
virDomainChrDefPtr chr);
int virDomainSaveXML(const char *configDir, int virDomainSaveXML(const char *configDir,
virDomainDefPtr def, virDomainDefPtr def,
const char *xml); const char *xml);

View File

@ -114,6 +114,10 @@ virDomainChrConsoleTargetTypeToString;
virDomainChrDefForeach; virDomainChrDefForeach;
virDomainChrDefFree; virDomainChrDefFree;
virDomainChrDefNew; virDomainChrDefNew;
virDomainChrFind;
virDomainChrGetDomainPtrs;
virDomainChrInsert;
virDomainChrRemove;
virDomainChrSerialTargetTypeFromString; virDomainChrSerialTargetTypeFromString;
virDomainChrSerialTargetTypeToString; virDomainChrSerialTargetTypeToString;
virDomainChrSourceDefCopy; virDomainChrSourceDefCopy;