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

util: eliminate union in virNetDevVPortProfile

virNetDevVPortProfile has (had) a type field that can be set to one of
several values, and a union of several structs, one for each
type. When a domain's interface object is of type "network", the
domain config may not know beforehand which type of virtualport is
going to be provided in the actual device handed down from the network
driver at runtime, but may want to set some values in the virtualport
that may or may not be used, depending on the type. To support this
usage, this patch replaces the union of structs with toplevel fields
in the struct, making it possible for all of the fields to be set at
the same time.
This commit is contained in:
Laine Stump 2012-07-24 21:14:41 -04:00
parent 85d9c17d70
commit 21ea73e8f4
4 changed files with 47 additions and 50 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2009-2011 Red Hat, Inc. * Copyright (C) 2009-2012 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -100,7 +100,7 @@ virNetDevVPortProfileParse(xmlNodePtr node)
goto error; goto error;
} }
virtPort->u.virtPort8021Qbg.managerID = (uint8_t)val; virtPort->managerID = (uint8_t)val;
if (virStrToLong_ui(virtPortTypeID, NULL, 0, &val)) { if (virStrToLong_ui(virtPortTypeID, NULL, 0, &val)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
@ -114,7 +114,7 @@ virNetDevVPortProfileParse(xmlNodePtr node)
goto error; goto error;
} }
virtPort->u.virtPort8021Qbg.typeID = (uint32_t)val; virtPort->typeID = (uint32_t)val;
if (virStrToLong_ui(virtPortTypeIDVersion, NULL, 0, &val)) { if (virStrToLong_ui(virtPortTypeIDVersion, NULL, 0, &val)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
@ -128,17 +128,17 @@ virNetDevVPortProfileParse(xmlNodePtr node)
goto error; goto error;
} }
virtPort->u.virtPort8021Qbg.typeIDVersion = (uint8_t)val; virtPort->typeIDVersion = (uint8_t)val;
if (virtPortInstanceID != NULL) { if (virtPortInstanceID != NULL) {
if (virUUIDParse(virtPortInstanceID, if (virUUIDParse(virtPortInstanceID,
virtPort->u.virtPort8021Qbg.instanceID)) { virtPort->instanceID)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("cannot parse instanceid parameter as a uuid")); _("cannot parse instanceid parameter as a uuid"));
goto error; goto error;
} }
} else { } else {
if (virUUIDGenerate(virtPort->u.virtPort8021Qbg.instanceID)) { if (virUUIDGenerate(virtPort->instanceID)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("cannot generate a random uuid for instanceid")); _("cannot generate a random uuid for instanceid"));
goto error; goto error;
@ -156,7 +156,7 @@ virNetDevVPortProfileParse(xmlNodePtr node)
case VIR_NETDEV_VPORT_PROFILE_8021QBH: case VIR_NETDEV_VPORT_PROFILE_8021QBH:
if (virtPortProfileID != NULL) { if (virtPortProfileID != NULL) {
if (virStrcpyStatic(virtPort->u.virtPort8021Qbh.profileID, if (virStrcpyStatic(virtPort->profileID,
virtPortProfileID) != NULL) { virtPortProfileID) != NULL) {
virtPort->virtPortType = VIR_NETDEV_VPORT_PROFILE_8021QBH; virtPort->virtPortType = VIR_NETDEV_VPORT_PROFILE_8021QBH;
} else { } else {
@ -173,13 +173,13 @@ virNetDevVPortProfileParse(xmlNodePtr node)
case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH: case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
if (virtPortInterfaceID != NULL) { if (virtPortInterfaceID != NULL) {
if (virUUIDParse(virtPortInterfaceID, if (virUUIDParse(virtPortInterfaceID,
virtPort->u.openvswitch.interfaceID)) { virtPort->interfaceID)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("cannot parse interfaceid parameter as a uuid")); _("cannot parse interfaceid parameter as a uuid"));
goto error; goto error;
} }
} else { } else {
if (virUUIDGenerate(virtPort->u.openvswitch.interfaceID)) { if (virUUIDGenerate(virtPort->interfaceID)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("cannot generate a random uuid for interfaceid")); _("cannot generate a random uuid for interfaceid"));
goto error; goto error;
@ -187,14 +187,14 @@ virNetDevVPortProfileParse(xmlNodePtr node)
} }
/* profileid is not mandatory for Open vSwitch */ /* profileid is not mandatory for Open vSwitch */
if (virtPortProfileID != NULL) { if (virtPortProfileID != NULL) {
if (virStrcpyStatic(virtPort->u.openvswitch.profileID, if (virStrcpyStatic(virtPort->profileID,
virtPortProfileID) == NULL) { virtPortProfileID) == NULL) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("profileid parameter too long")); _("profileid parameter too long"));
goto error; goto error;
} }
} else { } else {
virtPort->u.openvswitch.profileID[0] = '\0'; virtPort->profileID[0] = '\0';
} }
break; break;
@ -234,33 +234,33 @@ virNetDevVPortProfileFormat(virNetDevVPortProfilePtr virtPort,
switch (virtPort->virtPortType) { switch (virtPort->virtPortType) {
case VIR_NETDEV_VPORT_PROFILE_8021QBG: case VIR_NETDEV_VPORT_PROFILE_8021QBG:
virUUIDFormat(virtPort->u.virtPort8021Qbg.instanceID, virUUIDFormat(virtPort->instanceID,
uuidstr); uuidstr);
virBufferAsprintf(buf, virBufferAsprintf(buf,
" <parameters managerid='%d' typeid='%d' " " <parameters managerid='%d' typeid='%d' "
"typeidversion='%d' instanceid='%s'/>\n", "typeidversion='%d' instanceid='%s'/>\n",
virtPort->u.virtPort8021Qbg.managerID, virtPort->managerID,
virtPort->u.virtPort8021Qbg.typeID, virtPort->typeID,
virtPort->u.virtPort8021Qbg.typeIDVersion, virtPort->typeIDVersion,
uuidstr); uuidstr);
break; break;
case VIR_NETDEV_VPORT_PROFILE_8021QBH: case VIR_NETDEV_VPORT_PROFILE_8021QBH:
virBufferAsprintf(buf, virBufferAsprintf(buf,
" <parameters profileid='%s'/>\n", " <parameters profileid='%s'/>\n",
virtPort->u.virtPort8021Qbh.profileID); virtPort->profileID);
break; break;
case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH: case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
virUUIDFormat(virtPort->u.openvswitch.interfaceID, virUUIDFormat(virtPort->interfaceID,
uuidstr); uuidstr);
if (virtPort->u.openvswitch.profileID[0] == '\0') { if (virtPort->profileID[0] == '\0') {
virBufferAsprintf(buf, " <parameters interfaceid='%s'/>\n", virBufferAsprintf(buf, " <parameters interfaceid='%s'/>\n",
uuidstr); uuidstr);
} else { } else {
virBufferAsprintf(buf, " <parameters interfaceid='%s' " virBufferAsprintf(buf, " <parameters interfaceid='%s' "
"profileid='%s'/>\n", uuidstr, "profileid='%s'/>\n", uuidstr,
virtPort->u.openvswitch.profileID); virtPort->profileID);
} }
break; break;

View File

@ -59,7 +59,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
char *vmid_ex_id = NULL; char *vmid_ex_id = NULL;
virMacAddrFormat(macaddr, macaddrstr); virMacAddrFormat(macaddr, macaddrstr);
virUUIDFormat(ovsport->u.openvswitch.interfaceID, ifuuidstr); virUUIDFormat(ovsport->interfaceID, ifuuidstr);
virUUIDFormat(vmuuid, vmuuidstr); virUUIDFormat(vmuuid, vmuuidstr);
if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"", if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"",
@ -71,14 +71,14 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"", if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
vmuuidstr) < 0) vmuuidstr) < 0)
goto out_of_memory; goto out_of_memory;
if (ovsport->u.openvswitch.profileID[0] != '\0') { if (ovsport->profileID[0] != '\0') {
if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"", if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"",
ovsport->u.openvswitch.profileID) < 0) ovsport->profileID) < 0)
goto out_of_memory; goto out_of_memory;
} }
cmd = virCommandNew(OVSVSCTL); cmd = virCommandNew(OVSVSCTL);
if (ovsport->u.openvswitch.profileID[0] == '\0') { if (ovsport->profileID[0] == '\0') {
virCommandAddArgList(cmd, "--", "--may-exist", "add-port", virCommandAddArgList(cmd, "--", "--may-exist", "add-port",
brname, ifname, brname, ifname,
"--", "set", "Interface", ifname, attachedmac_ex_id, "--", "set", "Interface", ifname, attachedmac_ex_id,

View File

@ -95,15 +95,15 @@ virNetDevVPortProfileEqual(virNetDevVPortProfilePtr a, virNetDevVPortProfilePtr
break; break;
case VIR_NETDEV_VPORT_PROFILE_8021QBG: case VIR_NETDEV_VPORT_PROFILE_8021QBG:
if (a->u.virtPort8021Qbg.managerID != b->u.virtPort8021Qbg.managerID || if (a->managerID != b->managerID ||
a->u.virtPort8021Qbg.typeID != b->u.virtPort8021Qbg.typeID || a->typeID != b->typeID ||
a->u.virtPort8021Qbg.typeIDVersion != b->u.virtPort8021Qbg.typeIDVersion || a->typeIDVersion != b->typeIDVersion ||
memcmp(a->u.virtPort8021Qbg.instanceID, b->u.virtPort8021Qbg.instanceID, VIR_UUID_BUFLEN) != 0) memcmp(a->instanceID, b->instanceID, VIR_UUID_BUFLEN) != 0)
return false; return false;
break; break;
case VIR_NETDEV_VPORT_PROFILE_8021QBH: case VIR_NETDEV_VPORT_PROFILE_8021QBH:
if (STRNEQ(a->u.virtPort8021Qbh.profileID, b->u.virtPort8021Qbh.profileID)) if (STRNEQ(a->profileID, b->profileID))
return false; return false;
break; break;
@ -637,8 +637,8 @@ virNetDevVPortProfileOp8021Qbg(const char *ifname,
int rc = -1; int rc = -1;
int op = PORT_REQUEST_ASSOCIATE; int op = PORT_REQUEST_ASSOCIATE;
struct ifla_port_vsi portVsi = { struct ifla_port_vsi portVsi = {
.vsi_mgr_id = virtPort->u.virtPort8021Qbg.managerID, .vsi_mgr_id = virtPort->managerID,
.vsi_type_version = virtPort->u.virtPort8021Qbg.typeIDVersion, .vsi_type_version = virtPort->typeIDVersion,
}; };
bool nltarget_kernel = false; bool nltarget_kernel = false;
int vlanid; int vlanid;
@ -658,9 +658,9 @@ virNetDevVPortProfileOp8021Qbg(const char *ifname,
if (vlanid < 0) if (vlanid < 0)
vlanid = 0; vlanid = 0;
portVsi.vsi_type_id[2] = virtPort->u.virtPort8021Qbg.typeID >> 16; portVsi.vsi_type_id[2] = virtPort->typeID >> 16;
portVsi.vsi_type_id[1] = virtPort->u.virtPort8021Qbg.typeID >> 8; portVsi.vsi_type_id[1] = virtPort->typeID >> 8;
portVsi.vsi_type_id[0] = virtPort->u.virtPort8021Qbg.typeID; portVsi.vsi_type_id[0] = virtPort->typeID;
switch (virtPortOp) { switch (virtPortOp) {
case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE: case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE:
@ -684,7 +684,7 @@ virNetDevVPortProfileOp8021Qbg(const char *ifname,
vlanid, vlanid,
NULL, NULL,
&portVsi, &portVsi,
virtPort->u.virtPort8021Qbg.instanceID, virtPort->instanceID,
NULL, NULL,
vf, vf,
op, op,
@ -749,7 +749,7 @@ virNetDevVPortProfileOp8021Qbh(const char *ifname,
nltarget_kernel, nltarget_kernel,
macaddr, macaddr,
vlanid, vlanid,
virtPort->u.virtPort8021Qbh.profileID, virtPort->profileID,
NULL, NULL,
vm_uuid, vm_uuid,
hostuuid, hostuuid,

View File

@ -59,21 +59,18 @@ typedef struct _virNetDevVPortProfile virNetDevVPortProfile;
typedef virNetDevVPortProfile *virNetDevVPortProfilePtr; typedef virNetDevVPortProfile *virNetDevVPortProfilePtr;
struct _virNetDevVPortProfile { struct _virNetDevVPortProfile {
enum virNetDevVPortProfile virtPortType; enum virNetDevVPortProfile virtPortType;
union { /* these members are used when virtPortType == 802.1Qbg */
struct {
uint8_t managerID; uint8_t managerID;
uint32_t typeID; /* 24 bit valid */ uint32_t typeID; /* 24 bit valid */
uint8_t typeIDVersion; uint8_t typeIDVersion;
unsigned char instanceID[VIR_UUID_BUFLEN]; unsigned char instanceID[VIR_UUID_BUFLEN];
} virtPort8021Qbg;
struct { /* this member is used when virtPortType == 802.1Qbh|openvswitch */
char profileID[LIBVIRT_IFLA_VF_PORT_PROFILE_MAX]; char profileID[LIBVIRT_IFLA_VF_PORT_PROFILE_MAX];
} virtPort8021Qbh;
struct { /* this member is used when virtPortType == openvswitch */
unsigned char interfaceID[VIR_UUID_BUFLEN]; unsigned char interfaceID[VIR_UUID_BUFLEN];
char profileID[LIBVIRT_IFLA_VF_PORT_PROFILE_MAX]; /* NB - if virtPortType == NONE, any/all of the items could be used */
} openvswitch;
} u;
}; };