virNetDevVPortProfilePtr
-virNetDevVPortProfileParse(xmlNodePtr node)
+virNetDevVPortProfileParse(xmlNodePtr node, unsigned int flags)
{
char *virtPortType;
char *virtPortManagerID = NULL;
return NULL;
}
- virtPortType = virXMLPropString(node, "type");
- if (!virtPortType) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("missing virtualportprofile type"));
+ if ((virtPortType = virXMLPropString(node, "type")) &&
+ (virtPort->virtPortType = virNetDevVPortTypeFromString(virtPortType)) <= 0) {
+ virReportError(VIR_ERR_XML_ERROR,
+ _("unknown virtualport type %s"), virtPortType);
goto error;
}
- if ((virtPort->virtPortType = virNetDevVPortTypeFromString(virtPortType)) <= 0) {
- virReportError(VIR_ERR_XML_ERROR,
- _("unknown virtualportprofile type %s"), virtPortType);
+ if ((virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE) &&
+ (flags & VIR_VPORT_XML_REQUIRE_TYPE)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("missing required virtualport type"));
goto error;
}
while (cur != NULL) {
if (xmlStrEqual(cur->name, BAD_CAST "parameters")) {
-
virtPortManagerID = virXMLPropString(cur, "managerid");
virtPortTypeID = virXMLPropString(cur, "typeid");
virtPortTypeIDVersion = virXMLPropString(cur, "typeidversion");
virtPortInterfaceID = virXMLPropString(cur, "interfaceid");
break;
}
-
cur = cur->next;
}
- switch (virtPort->virtPortType) {
- case VIR_NETDEV_VPORT_PROFILE_8021QBG:
- if (virtPortManagerID != NULL && virtPortTypeID != NULL &&
- virtPortTypeIDVersion != NULL) {
- unsigned int val;
-
- if (virStrToLong_ui(virtPortManagerID, NULL, 0, &val)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot parse value of managerid parameter"));
- goto error;
- }
-
- if (val > 0xff) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("value of managerid out of range"));
- goto error;
- }
-
- virtPort->managerID = (uint8_t)val;
-
- if (virStrToLong_ui(virtPortTypeID, NULL, 0, &val)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot parse value of typeid parameter"));
- goto error;
- }
+ if (virtPortManagerID) {
+ unsigned int val;
- if (val > 0xffffff) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("value for typeid out of range"));
- goto error;
- }
-
- virtPort->typeID = (uint32_t)val;
+ if (virStrToLong_ui(virtPortManagerID, NULL, 0, &val)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("cannot parse value of managerid parameter"));
+ goto error;
+ }
+ if (val > 0xff) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("value of managerid out of range"));
+ goto error;
+ }
+ virtPort->managerID = (uint8_t)val;
+ virtPort->managerID_specified = true;
+ }
- if (virStrToLong_ui(virtPortTypeIDVersion, NULL, 0, &val)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot parse value of typeidversion parameter"));
- goto error;
- }
+ if (virtPortTypeID) {
+ unsigned int val;
- if (val > 0xff) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("value of typeidversion out of range"));
- goto error;
- }
+ if (virStrToLong_ui(virtPortTypeID, NULL, 0, &val)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("cannot parse value of typeid parameter"));
+ goto error;
+ }
+ if (val > 0xffffff) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("value for typeid out of range"));
+ goto error;
+ }
+ virtPort->typeID = (uint32_t)val;
+ virtPort->typeID_specified = true;
+ }
- virtPort->typeIDVersion = (uint8_t)val;
-
- if (virtPortInstanceID != NULL) {
- if (virUUIDParse(virtPortInstanceID,
- virtPort->instanceID)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot parse instanceid parameter as a uuid"));
- goto error;
- }
- } else {
- if (virUUIDGenerate(virtPort->instanceID)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot generate a random uuid for instanceid"));
- goto error;
- }
- }
+ if (virtPortTypeIDVersion) {
+ unsigned int val;
- virtPort->virtPortType = VIR_NETDEV_VPORT_PROFILE_8021QBG;
+ if (virStrToLong_ui(virtPortTypeIDVersion, NULL, 0, &val)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("cannot parse value of typeidversion parameter"));
+ goto error;
+ }
+ if (val > 0xff) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("value of typeidversion out of range"));
+ goto error;
+ }
+ virtPort->typeIDVersion = (uint8_t)val;
+ virtPort->typeIDVersion_specified = true;
+ }
- } else {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("a parameter is missing for 802.1Qbg description"));
+ if (virtPortInstanceID) {
+ if (virUUIDParse(virtPortInstanceID, virtPort->instanceID) < 0) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("cannot parse instanceid parameter as a uuid"));
goto error;
}
- break;
-
- case VIR_NETDEV_VPORT_PROFILE_8021QBH:
- if (virtPortProfileID != NULL) {
- if (virStrcpyStatic(virtPort->profileID,
- virtPortProfileID) != NULL) {
- virtPort->virtPortType = VIR_NETDEV_VPORT_PROFILE_8021QBH;
- } else {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("profileid parameter too long"));
- goto error;
- }
- } else {
+ virtPort->instanceID_specified = true;
+ }
+
+ if (virtPortProfileID &&
+ !virStrcpyStatic(virtPort->profileID, virtPortProfileID)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("profileid parameter too long"));
+ goto error;
+ }
+
+ if (virtPortInterfaceID) {
+ if (virUUIDParse(virtPortInterfaceID, virtPort->interfaceID) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
- _("profileid parameter is missing for 802.1Qbh description"));
+ _("cannot parse interfaceid parameter as a uuid"));
goto error;
}
- break;
- case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
- if (virtPortInterfaceID != NULL) {
- if (virUUIDParse(virtPortInterfaceID,
- virtPort->interfaceID)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot parse interfaceid parameter as a uuid"));
- goto error;
- }
- } else {
- if (virUUIDGenerate(virtPort->interfaceID)) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("cannot generate a random uuid for interfaceid"));
+ virtPort->interfaceID_specified = true;
+ }
+
+ /* generate default instanceID/interfaceID if appropriate */
+ if (flags & VIR_VPORT_XML_GENERATE_MISSING_DEFAULTS) {
+ if (!virtPort->instanceID_specified &&
+ (virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
+ virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ if (virUUIDGenerate(virtPort->instanceID) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("cannot generate a random uuid for instanceid"));
goto error;
}
+ virtPort->instanceID_specified = true;
}
- /* profileid is not mandatory for Open vSwitch */
- if (virtPortProfileID != NULL) {
- if (virStrcpyStatic(virtPort->profileID,
- virtPortProfileID) == NULL) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("profileid parameter too long"));
+ if (!virtPort->interfaceID_specified &&
+ (virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH ||
+ virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ if (virUUIDGenerate(virtPort->interfaceID) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("cannot generate a random uuid for interfaceid"));
goto error;
}
- } else {
- virtPort->profileID[0] = '\0';
+ virtPort->interfaceID_specified = true;
}
- break;
+ }
- default:
- virReportError(VIR_ERR_XML_ERROR,
- _("unexpected virtualport type %d"), virtPort->virtPortType);
+ /* check for required/unsupported attributes */
+
+ if ((flags & VIR_VPORT_XML_REQUIRE_ALL_ATTRIBUTES) &&
+ (virNetDevVPortProfileCheckComplete(virtPort, false) < 0)) {
goto error;
}
+ if (virNetDevVPortProfileCheckNoExtras(virtPort) < 0)
+ goto error;
+
cleanup:
VIR_FREE(virtPortManagerID);
VIR_FREE(virtPortTypeID);
virNetDevVPortProfileFormat(virNetDevVPortProfilePtr virtPort,
virBufferPtr buf)
{
- char uuidstr[VIR_UUID_STRING_BUFLEN];
+ enum virNetDevVPortProfile type;
+ bool noParameters;
- if (!virtPort || virtPort->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)
+ if (!virtPort)
return 0;
- virBufferAsprintf(buf, "<virtualport type='%s'>\n",
- virNetDevVPortTypeToString(virtPort->virtPortType));
-
- switch (virtPort->virtPortType) {
- case VIR_NETDEV_VPORT_PROFILE_8021QBG:
- virUUIDFormat(virtPort->instanceID,
- uuidstr);
- virBufferAsprintf(buf,
- " <parameters managerid='%d' typeid='%d' "
- "typeidversion='%d' instanceid='%s'/>\n",
- virtPort->managerID,
- virtPort->typeID,
- virtPort->typeIDVersion,
- uuidstr);
- break;
-
- case VIR_NETDEV_VPORT_PROFILE_8021QBH:
- virBufferAsprintf(buf,
- " <parameters profileid='%s'/>\n",
- virtPort->profileID);
- break;
-
- case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
- virUUIDFormat(virtPort->interfaceID,
- uuidstr);
- if (virtPort->profileID[0] == '\0') {
- virBufferAsprintf(buf, " <parameters interfaceid='%s'/>\n",
- uuidstr);
+ noParameters = !(virtPort->managerID_specified ||
+ virtPort->typeID_specified ||
+ virtPort->typeIDVersion_specified ||
+ virtPort->instanceID_specified ||
+ virtPort->profileID[0] ||
+ virtPort->interfaceID_specified);
+
+ type = virtPort->virtPortType;
+ if (type == VIR_NETDEV_VPORT_PROFILE_NONE) {
+ if (noParameters)
+ return 0;
+ virBufferAddLit(buf, "<virtualport>\n");
+ } else {
+ if (noParameters) {
+ virBufferAsprintf(buf, "<virtualport type='%s'/>\n",
+ virNetDevVPortTypeToString(type));
+ return 0;
} else {
- virBufferAsprintf(buf, " <parameters interfaceid='%s' "
- "profileid='%s'/>\n", uuidstr,
- virtPort->profileID);
+ virBufferAsprintf(buf, "<virtualport type='%s'>\n",
+ virNetDevVPortTypeToString(type));
}
+ }
+ virBufferAddLit(buf, " <parameters");
- break;
+ if (virtPort->managerID_specified &&
+ (type == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ virBufferAsprintf(buf, " managerid='%d'", virtPort->managerID);
+ }
+ if (virtPort->typeID_specified &&
+ (type == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ virBufferAsprintf(buf, " typeid='%d'", virtPort->typeID);
+ }
+ if (virtPort->typeIDVersion_specified &&
+ (type == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ virBufferAsprintf(buf, " typeidversion='%d'",
+ virtPort->typeIDVersion);
+ }
+ if (virtPort->instanceID_specified &&
+ (type == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ char uuidstr[VIR_UUID_STRING_BUFLEN];
- default:
- virReportError(VIR_ERR_XML_ERROR,
- _("unexpected virtualport type %d"), virtPort->virtPortType);
- return -1;
+ virUUIDFormat(virtPort->instanceID, uuidstr);
+ virBufferAsprintf(buf, " instanceid='%s'", uuidstr);
+ }
+ if (virtPort->interfaceID_specified &&
+ (type == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+ virUUIDFormat(virtPort->interfaceID, uuidstr);
+ virBufferAsprintf(buf, " interfaceid='%s'", uuidstr);
+ }
+ if (virtPort->profileID[0] &&
+ (type == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH ||
+ type == VIR_NETDEV_VPORT_PROFILE_8021QBH ||
+ type == VIR_NETDEV_VPORT_PROFILE_NONE)) {
+ virBufferAsprintf(buf, " profileid='%s'", virtPort->profileID);
}
+ virBufferAddLit(buf, "/>\n");
virBufferAddLit(buf, "</virtualport>\n");
return 0;
}