Rename ifaceGetIndex and ifaceGetVLAN

Rename the ifaceGetIndex method to virNetDevGetIndex and
ifaceGetVlanID to virNetDevGetVLanID. Also change the error
reporting behaviour to always raise errors and return -1 on
failure

* util/interface.c, util/interface.h: Rename ifaceGetIndex
  and ifaceGetVLAN
* nwfilter/nwfilter_gentech_driver.c, nwfilter/nwfilter_learnipaddr.c,
  nwfilter/nwfilter_learnipaddr.c, util/virnetdevvportprofile.c: Update
  for API renames and error handling changes
This commit is contained in:
Daniel P. Berrange 2011-11-03 09:21:35 +00:00
parent 255917f516
commit ebbb6bd11f
7 changed files with 96 additions and 82 deletions

View File

@ -576,12 +576,12 @@ virHookPresent;
# interface.h # interface.h
ifaceCheck; ifaceCheck;
ifaceGetIndex; virNetDevGetIndex;
ifaceGetIPAddress; ifaceGetIPAddress;
ifaceGetNthParent; ifaceGetNthParent;
ifaceGetPhysicalFunction; ifaceGetPhysicalFunction;
ifaceGetVirtualFunctionIndex; ifaceGetVirtualFunctionIndex;
ifaceGetVlanID; virNetDevGetVLanID;
ifaceIsVirtualFunction; ifaceIsVirtualFunction;
virNetDevMacVLanCreate; virNetDevMacVLanCreate;
virNetDevMacVLanDelete; virNetDevMacVLanDelete;

View File

@ -903,9 +903,11 @@ _virNWFilterInstantiateFilter(virConnectPtr conn,
/* after grabbing the filter update lock check for the interface; if /* after grabbing the filter update lock check for the interface; if
it's not there anymore its filters will be or are being removed it's not there anymore its filters will be or are being removed
(while holding the lock) and we don't want to build new ones */ (while holding the lock) and we don't want to build new ones */
if (ifaceGetIndex(false, net->ifname, &ifindex) < 0) { if (virNetDevExists(net->ifname) != 1 ||
virNetDevGetIndex(net->ifname, &ifindex) < 0) {
/* interfaces / VMs can disappear during filter instantiation; /* interfaces / VMs can disappear during filter instantiation;
don't mark it as an error */ don't mark it as an error */
virResetLastError();
rc = 0; rc = 0;
goto cleanup; goto cleanup;
} }
@ -1021,8 +1023,9 @@ int virNWFilterRollbackUpdateFilter(virConnectPtr conn,
} }
/* don't tear anything while the address is being learned */ /* don't tear anything while the address is being learned */
if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 && if (virNetDevGetIndex(net->ifname, &ifindex) < 0)
virNWFilterLookupLearnReq(ifindex) != NULL) virResetLastError();
else if (virNWFilterLookupLearnReq(ifindex) != NULL)
return 0; return 0;
return techdriver->tearNewRules(conn, net->ifname); return techdriver->tearNewRules(conn, net->ifname);
@ -1047,8 +1050,9 @@ virNWFilterTearOldFilter(virConnectPtr conn,
} }
/* don't tear anything while the address is being learned */ /* don't tear anything while the address is being learned */
if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 && if (virNetDevGetIndex(net->ifname, &ifindex) < 0)
virNWFilterLookupLearnReq(ifindex) != NULL) virResetLastError();
else if (virNWFilterLookupLearnReq(ifindex) != NULL)
return 0; return 0;
return techdriver->tearOldRules(conn, net->ifname); return techdriver->tearOldRules(conn, net->ifname);

View File

@ -252,7 +252,10 @@ virNWFilterTerminateLearnReq(const char *ifname) {
int ifindex; int ifindex;
virNWFilterIPAddrLearnReqPtr req; virNWFilterIPAddrLearnReqPtr req;
if (ifaceGetIndex(false, ifname, &ifindex) == 0) { if (virNetDevGetIndex(ifname, &ifindex) < 0) {
virResetLastError();
return rc;
}
IFINDEX2STR(ifindex_str, ifindex); IFINDEX2STR(ifindex_str, ifindex);
@ -265,7 +268,6 @@ virNWFilterTerminateLearnReq(const char *ifname) {
} }
virMutexUnlock(&pendingLearnReqLock); virMutexUnlock(&pendingLearnReqLock);
}
return rc; return rc;
} }

View File

@ -115,8 +115,9 @@ ifaceCheck(bool reportError, const char *ifname,
} }
if (ifindex != -1) { if (ifindex != -1) {
rc = ifaceGetIndex(reportError, ifname, &idx); if (virNetDevGetIndex(ifname, &idx) < 0)
if (rc == 0 && idx != ifindex) virResetLastError();
else if (idx != ifindex)
rc = -ENODEV; rc = -ENODEV;
} }
@ -141,114 +142,112 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED,
/** /**
* ifaceGetIndex * virNetDevGetIndex
*
* @reportError: whether to report errors or keep silent
* @ifname : Name of the interface whose index is to be found * @ifname : Name of the interface whose index is to be found
* @ifindex: Pointer to int where the index will be written into * @ifindex: Pointer to int where the index will be written into
* *
* Get the index of an interface given its name. * Get the index of an interface given its name.
* *
* Returns 0 on success, -errno on failure. * Returns 0 on success, -1 on failure
* -ENODEV : if interface with given name does not exist
* -EINVAL : if interface name is invalid (too long)
*/ */
#ifdef __linux__ #ifdef __linux__
int int
ifaceGetIndex(bool reportError, const char *ifname, int *ifindex) virNetDevGetIndex(const char *ifname, int *ifindex)
{ {
int rc = 0; int ret = -1;
struct ifreq ifreq; struct ifreq ifreq;
int fd = socket(PF_PACKET, SOCK_DGRAM, 0); int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (fd < 0) if (fd < 0) {
return -errno; virReportSystemError(errno, "%s",
_("Unable to open control socket"));
return -1;
}
memset(&ifreq, 0, sizeof(ifreq)); memset(&ifreq, 0, sizeof(ifreq));
if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname), if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname),
sizeof(ifreq.ifr_name)) == NULL) { sizeof(ifreq.ifr_name)) == NULL) {
if (reportError) virReportSystemError(ERANGE,
ifaceError(VIR_ERR_INTERNAL_ERROR,
_("invalid interface name %s"), _("invalid interface name %s"),
ifname); ifname);
rc = -EINVAL;
goto cleanup; goto cleanup;
} }
if (ioctl(fd, SIOCGIFINDEX, &ifreq) >= 0) if (ioctl(fd, SIOCGIFINDEX, &ifreq) < 0) {
*ifindex = ifreq.ifr_ifindex; virReportSystemError(errno,
else { _("Unable to get index for interface %s"), ifname);
if (reportError) goto cleanup;
ifaceError(VIR_ERR_INTERNAL_ERROR,
_("interface %s does not exist"),
ifname);
rc = -ENODEV;
} }
*ifindex = ifreq.ifr_ifindex;
ret = 0;
cleanup: cleanup:
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return ret;
return rc;
} }
#else #else
int int
ifaceGetIndex(bool reportError, virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
const char *ifname ATTRIBUTE_UNUSED,
int *ifindex ATTRIBUTE_UNUSED) int *ifindex ATTRIBUTE_UNUSED)
{ {
if (reportError) { virReportSystemError(ENOSYS, "%s",
ifaceError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to get interface index on this platform"));
_("ifaceGetIndex is not supported on non-linux platforms")); return -1;
}
return -ENOSYS;
} }
#endif /* __linux__ */ #endif /* __linux__ */
#ifdef __linux__ #ifdef __linux__
int int
ifaceGetVlanID(const char *vlanifname, int *vlanid) { virNetDevGetVLanID(const char *ifname, int *vlanid)
{
struct vlan_ioctl_args vlanargs = { struct vlan_ioctl_args vlanargs = {
.cmd = GET_VLAN_VID_CMD, .cmd = GET_VLAN_VID_CMD,
}; };
int rc = 0; int ret = -1;
int fd = socket(PF_PACKET, SOCK_DGRAM, 0); int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (fd < 0) if (fd < 0) {
return -errno; virReportSystemError(errno, "%s",
_("Unable to open control socket"));
return -1;
}
if (virStrcpyStatic(vlanargs.device1, vlanifname) == NULL) { if (virStrcpyStatic(vlanargs.device1, ifname) == NULL) {
rc = -EINVAL; virReportSystemError(ERANGE,
_("invalid interface name %s"),
ifname);
goto cleanup; goto cleanup;
} }
if (ioctl(fd, SIOCGIFVLAN, &vlanargs) != 0) { if (ioctl(fd, SIOCGIFVLAN, &vlanargs) != 0) {
rc = -errno; virReportSystemError(errno,
_("Unable to get VLAN for interface %s"), ifname);
goto cleanup; goto cleanup;
} }
*vlanid = vlanargs.u.VID; *vlanid = vlanargs.u.VID;
ret = 0;
cleanup: cleanup:
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return rc; return ret;
} }
#else #else
int int
ifaceGetVlanID(const char *vlanifname ATTRIBUTE_UNUSED, virNetDevGetVLanID(const char *ifname ATTRIBUTE_UNUSED,
int *vlanid ATTRIBUTE_UNUSED) { int *vlanid ATTRIBUTE_UNUSED)
{
ifaceError(VIR_ERR_INTERNAL_ERROR, "%s", virReportSystemError(ENOSYS, "%s",
_("ifaceGetVlanID is not supported on non-linux platforms")); _("Unable to get VLAN on this platform"));
return -1;
return -ENOSYS;
} }
#endif /* __linux__ */ #endif /* __linux__ */
@ -496,7 +495,7 @@ ifaceGetNthParent(int ifindex, const char *ifname, unsigned int nthParent,
*nth = 0; *nth = 0;
if (ifindex <= 0 && ifaceGetIndex(true, ifname, &ifindex) < 0) if (ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
return -1; return -1;
while (!end && i <= nthParent) { while (!end && i <= nthParent) {

View File

@ -33,9 +33,11 @@ struct nlattr;
int ifaceCheck(bool reportError, const char *ifname, int ifaceCheck(bool reportError, const char *ifname,
const unsigned char *macaddr, int ifindex); const unsigned char *macaddr, int ifindex);
int ifaceGetIndex(bool reportError, const char *ifname, int *ifindex); int virNetDevGetIndex(const char *ifname, int *ifindex)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int ifaceGetVlanID(const char *vlanifname, int *vlanid); int virNetDevGetVLanID(const char *ifname, int *vlanid)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int ifaceGetIPAddress(const char *ifname, virSocketAddrPtr addr); int ifaceGetIPAddress(const char *ifname, virSocketAddrPtr addr);

View File

@ -107,7 +107,7 @@ virNetDevMacVLanCreate(const char *ifname,
struct nl_msg *nl_msg; struct nl_msg *nl_msg;
struct nlattr *linkinfo, *info_data; struct nlattr *linkinfo, *info_data;
if (ifaceGetIndex(true, srcdev, &ifindex) < 0) if (virNetDevGetIndex(srcdev, &ifindex) < 0)
return -1; return -1;
*retry = 0; *retry = 0;
@ -481,7 +481,7 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
int retries, do_retry = 0; int retries, do_retry = 0;
uint32_t macvtapMode; uint32_t macvtapMode;
const char *cr_ifname; const char *cr_ifname;
int ifindex; int ret;
macvtapMode = modeMap[mode]; macvtapMode = modeMap[mode];
@ -502,13 +502,16 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
} }
if (tgifname) { if (tgifname) {
if(ifaceGetIndex(false, tgifname, &ifindex) == 0) { if ((ret = virNetDevExists(tgifname)) < 0)
return -1;
if (ret) {
if (STRPREFIX(tgifname, if (STRPREFIX(tgifname,
MACVTAP_NAME_PREFIX)) { MACVTAP_NAME_PREFIX)) {
goto create_name; goto create_name;
} }
virReportSystemError(errno, virReportSystemError(EEXIST,
_("Interface %s already exists"), tgifname); _("Unable to create macvlan device %s"), tgifname);
return -1; return -1;
} }
cr_ifname = tgifname; cr_ifname = tgifname;
@ -521,7 +524,9 @@ create_name:
retries = 5; retries = 5;
for (c = 0; c < 8192; c++) { for (c = 0; c < 8192; c++) {
snprintf(ifname, sizeof(ifname), MACVTAP_NAME_PATTERN, c); snprintf(ifname, sizeof(ifname), MACVTAP_NAME_PATTERN, c);
if (ifaceGetIndex(false, ifname, &ifindex) == -ENODEV) { if ((ret = virNetDevExists(ifname)) < 0)
return -1;
if (!ret) {
rc = virNetDevMacVLanCreate(ifname, type, macaddress, linkdev, rc = virNetDevMacVLanCreate(ifname, type, macaddress, linkdev,
macvtapMode, &do_retry); macvtapMode, &do_retry);
if (rc == 0) if (rc == 0)

View File

@ -544,9 +544,11 @@ virNetDevVPortProfileGetPhysdevAndVlan(const char *ifname, int *root_ifindex, ch
if (nth == 0) if (nth == 0)
break; break;
if (*vlanid == -1) { if (*vlanid == -1) {
if (ifaceGetVlanID(root_ifname, vlanid) < 0) if (virNetDevGetVLanID(root_ifname, vlanid) < 0) {
virResetLastError();
*vlanid = -1; *vlanid = -1;
} }
}
ifindex = *root_ifindex; ifindex = *root_ifindex;
ifname = NULL; ifname = NULL;
@ -676,7 +678,7 @@ virNetDevVPortProfileOp8021Qbh(const char *ifname,
if (rc < 0) if (rc < 0)
goto err_exit; goto err_exit;
rc = ifaceGetIndex(true, physfndev, &ifindex); rc = virNetDevGetIndex(physfndev, &ifindex);
if (rc < 0) if (rc < 0)
goto err_exit; goto err_exit;