Rename ifaceCheck to virNetDevValidateConfig

Rename the ifaceCheck method to virNetDevValidateConfig and change
so that it always raises an error and returns -1 on error.

* src/util/interface.c, src/util/interface.h: Rename ifaceCheck
  to virNetDevValidateConfig
* src/nwfilter/nwfilter_gentech_driver.c,
  src/nwfilter/nwfilter_learnipaddr.c: Update for API rename
This commit is contained in:
Daniel P. Berrange 2011-11-03 12:28:17 +00:00
parent 8e6cd41418
commit 6e0c4dce0b
5 changed files with 55 additions and 47 deletions

View File

@ -575,7 +575,7 @@ virHookPresent;
# interface.h # interface.h
ifaceCheck; virNetDevValidateConfig;
virNetDevGetIndex; virNetDevGetIndex;
virNetDevGetIPv4Address; virNetDevGetIPv4Address;
ifaceGetNthParent; ifaceGetNthParent;

View File

@ -715,7 +715,8 @@ virNWFilterInstantiate(virConnectPtr conn,
if (teardownOld && rc == 0) if (teardownOld && rc == 0)
techdriver->tearOldRules(conn, ifname); techdriver->tearOldRules(conn, ifname);
if (rc == 0 && (ifaceCheck(false, ifname, NULL, ifindex) < 0)) { if (rc == 0 && (virNetDevValidateConfig(ifname, NULL, ifindex) <= 0)) {
virResetLastError();
/* interface changed/disppeared */ /* interface changed/disppeared */
techdriver->allTeardown(ifname); techdriver->allTeardown(ifname);
rc = 1; rc = 1;
@ -964,8 +965,9 @@ virNWFilterInstantiateFilterLate(virConnectPtr conn,
&foundNewFilter); &foundNewFilter);
if (rc) { if (rc) {
/* something went wrong... 'DOWN' the interface */ /* something went wrong... 'DOWN' the interface */
if ((ifaceCheck(false, ifname, NULL, ifindex) < 0) || if ((virNetDevValidateConfig(ifname, NULL, ifindex) <= 0) ||
(virNetDevSetOnline(ifname, false) < 0)) { (virNetDevSetOnline(ifname, false) < 0)) {
virResetLastError();
/* assuming interface disappeared... */ /* assuming interface disappeared... */
_virNWFilterTeardownFilter(ifname); _virNWFilterTeardownFilter(ifname);
} }

View File

@ -434,7 +434,8 @@ learnIPAddressThread(void *arg)
req->status = 0; req->status = 0;
/* anything change to the VM's interface -- check at least once */ /* anything change to the VM's interface -- check at least once */
if (ifaceCheck(false, req->ifname, NULL, req->ifindex) < 0) { if (virNetDevValidateConfig(req->ifname, NULL, req->ifindex) <= 0) {
virResetLastError();
req->status = ENODEV; req->status = ENODEV;
goto done; goto done;
} }
@ -504,7 +505,8 @@ learnIPAddressThread(void *arg)
} }
/* check whether VM's dev is still there */ /* check whether VM's dev is still there */
if (ifaceCheck(false, req->ifname, NULL, req->ifindex) < 0) { if (virNetDevValidateConfig(req->ifname, NULL, req->ifindex) <= 0) {
virResetLastError();
req->status = ENODEV; req->status = ENODEV;
showError = false; showError = false;
break; break;

View File

@ -56,9 +56,7 @@
__FUNCTION__, __LINE__, __VA_ARGS__) __FUNCTION__, __LINE__, __VA_ARGS__)
/** /**
* ifaceCheck * virNetDevValidateConfig:
*
* @reportError: whether to report errors or keep silent
* @ifname: Name of the interface * @ifname: Name of the interface
* @macaddr: expected MAC address of the interface; not checked if NULL * @macaddr: expected MAC address of the interface; not checked if NULL
* @ifindex: expected index of the interface; not checked if '-1' * @ifindex: expected index of the interface; not checked if '-1'
@ -67,78 +65,83 @@
* it must have the given MAC address and if an interface index is * it must have the given MAC address and if an interface index is
* passed, it must also match the interface index. * passed, it must also match the interface index.
* *
* Returns 0 on success, -errno on failure. * Returns 1 if the config matches, 0 if the config does not match, or interface does not exist, -1 on error
* -ENODEV : if interface with given name does not exist or its interface
* index is different than the one passed
* -EINVAL : if interface name is invalid (too long)
*/ */
#ifdef __linux__ #ifdef __linux__
int int virNetDevValidateConfig(const char *ifname,
ifaceCheck(bool reportError, const char *ifname,
const unsigned char *macaddr, int ifindex) const unsigned char *macaddr, int ifindex)
{ {
struct ifreq ifr;
int fd = -1; int fd = -1;
int rc = 0; int ret = -1;
struct ifreq ifr;
int idx; int idx;
int rc;
if ((rc = virNetDevExists(ifname)) < 0)
return -1;
if (rc == 0) {
ret = 0;
goto cleanup;
}
if (macaddr != NULL) { if (macaddr != NULL) {
fd = socket(PF_PACKET, SOCK_DGRAM, 0); 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(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
if (virStrncpy(ifr.ifr_name, if (virStrncpy(ifr.ifr_name,
ifname, strlen(ifname), sizeof(ifr.ifr_name)) == NULL) { ifname, strlen(ifname), sizeof(ifr.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, SIOCGIFHWADDR, &ifr) < 0) { if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
if (reportError) if (errno == ENODEV) {
ifaceError(VIR_ERR_INTERNAL_ERROR, ret = 0;
goto cleanup;
}
virReportSystemError(errno,
_("coud not get MAC address of interface %s"), _("coud not get MAC address of interface %s"),
ifname); ifname);
rc = -errno;
goto cleanup; goto cleanup;
} }
if (memcmp(&ifr.ifr_hwaddr.sa_data, macaddr, VIR_MAC_BUFLEN) != 0) { if (memcmp(&ifr.ifr_hwaddr.sa_data, macaddr, VIR_MAC_BUFLEN) != 0) {
rc = -ENODEV; ret = 0;
goto cleanup; goto cleanup;
} }
} }
if (ifindex != -1) { if (ifindex != -1) {
if (virNetDevGetIndex(ifname, &idx) < 0) if (virNetDevGetIndex(ifname, &idx) < 0)
virResetLastError(); goto cleanup;
else if (idx != ifindex) else if (idx != ifindex) {
rc = -ENODEV; ret = 0;
goto cleanup;
} }
}
ret = 1;
cleanup: cleanup:
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return ret;
return rc;
} }
#else /* ! __linux__ */
#else int virNetDevValidateConfig(const char *ifname ATTRIBUTE_UNUSED,
int
ifaceCheck(bool reportError ATTRIBUTE_UNUSED,
const char *ifname ATTRIBUTE_UNUSED,
const unsigned char *macaddr ATTRIBUTE_UNUSED, const unsigned char *macaddr ATTRIBUTE_UNUSED,
int ifindex ATTRIBUTE_UNUSED) int ifindex ATTRIBUTE_UNUSED)
{ {
return -ENOSYS; return -ENOSYS;
} }
#endif /* ! __linux__ */
#endif /* __linux__ */

View File

@ -30,8 +30,9 @@ struct nlattr;
# define NET_SYSFS "/sys/class/net/" # define NET_SYSFS "/sys/class/net/"
int ifaceCheck(bool reportError, const char *ifname, int virNetDevValidateConfig(const char *ifname,
const unsigned char *macaddr, int ifindex); const unsigned char *macaddr, int ifindex)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex, int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex,
struct nlattr **tb, unsigned char **recvbuf, struct nlattr **tb, unsigned char **recvbuf,