[libvirt] [PATCH] macvtap: Fix error return value convention/inconsistencies

Roopa Prabhu roprabhu at cisco.com
Fri Oct 28 22:04:07 UTC 2011


From: Roopa Prabhu <roprabhu at cisco.com>

- changed some return 1's to return -1
- changed if (rc) error checks to if (rc < 0)
- fixed some other minor convention violations

I might have missed some. Can fix in another patch or can respin

Signed-off-by: Roopa Prabhu <roprabhu at cisco.com>
Reported-by: Eric Blake <eblake at redhat.com>
Reported-by: Laine Stump <laine at laine.org>
---
 src/util/interface.c |    8 +++---
 src/util/macvtap.c   |   64 +++++++++++++++++++++++++++-----------------------
 src/util/pci.c       |    6 ++---
 3 files changed, 41 insertions(+), 37 deletions(-)


diff --git a/src/util/interface.c b/src/util/interface.c
index 5d473b7..4ab74b5 100644
--- a/src/util/interface.c
+++ b/src/util/interface.c
@@ -1244,7 +1244,7 @@ ifaceIsVirtualFunction(const char *ifname)
     char *if_sysfs_device_link = NULL;
     int ret = -1;
 
-    if (ifaceSysfsFile(&if_sysfs_device_link, ifname, "device"))
+    if (ifaceSysfsFile(&if_sysfs_device_link, ifname, "device") < 0)
         return ret;
 
     ret = pciDeviceIsVirtualFunction(if_sysfs_device_link);
@@ -1272,10 +1272,10 @@ ifaceGetVirtualFunctionIndex(const char *pfname, const char *vfname,
     char *pf_sysfs_device_link = NULL, *vf_sysfs_device_link = NULL;
     int ret = -1;
 
-    if (ifaceSysfsFile(&pf_sysfs_device_link, pfname, "device"))
+    if (ifaceSysfsFile(&pf_sysfs_device_link, pfname, "device") < 0)
         return ret;
 
-    if (ifaceSysfsFile(&vf_sysfs_device_link, vfname, "device")) {
+    if (ifaceSysfsFile(&vf_sysfs_device_link, vfname, "device") < 0) {
         VIR_FREE(pf_sysfs_device_link);
         return ret;
     }
@@ -1306,7 +1306,7 @@ ifaceGetPhysicalFunction(const char *ifname, char **pfname)
     char *physfn_sysfs_path = NULL;
     int ret = -1;
 
-    if (ifaceSysfsDeviceFile(&physfn_sysfs_path, ifname, "physfn"))
+    if (ifaceSysfsDeviceFile(&physfn_sysfs_path, ifname, "physfn") < 0)
         return ret;
 
     ret = pciDeviceNetName(physfn_sysfs_path, pfname);
diff --git a/src/util/macvtap.c b/src/util/macvtap.c
index f8b9d55..78e30f8 100644
--- a/src/util/macvtap.c
+++ b/src/util/macvtap.c
@@ -210,8 +210,11 @@ configMacvtapTap(int tapfd, int vnet_hdr)
         rc_on_fail = -1;
         errmsg = _("cannot clean IFF_VNET_HDR flag on macvtap tap");
     } else if ((ifreq.ifr_flags & IFF_VNET_HDR) == 0 && vnet_hdr) {
-        if (ioctl(tapfd, TUNGETFEATURES, &features) != 0)
-            return errno;
+        if (ioctl(tapfd, TUNGETFEATURES, &features) < 0) {
+            virReportSystemError(errno, "%s",
+                   _("cannot get feature flags on macvtap tap"));
+            return -1;
+	}
         if ((features & IFF_VNET_HDR)) {
             new_flags = ifreq.ifr_flags | IFF_VNET_HDR;
             errmsg = _("cannot set IFF_VNET_HDR flag on macvtap tap");
@@ -335,7 +338,7 @@ create_name:
                                  macaddress,
                                  linkdev,
                                  virtPortProfile,
-                                 vmuuid, vmOp) != 0) {
+                                 vmuuid, vmOp) < 0) {
         rc = -1;
         goto link_del_exit;
     }
@@ -352,7 +355,6 @@ create_name:
     }
 
     rc = openTap(cr_ifname, 10);
-
     if (rc >= 0) {
         if (configMacvtapTap(rc, vnet_hdr) < 0) {
             VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
@@ -552,6 +554,8 @@ getPortProfileStatus(struct nlattr **tb, int32_t vf,
         }
     }
 
+    return rc;
+
 err_exit:
     if (msg)
         macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", msg);
@@ -572,7 +576,7 @@ doPortProfileOpSetLink(bool nltarget_kernel,
                        int32_t vf,
                        uint8_t op)
 {
-    int rc = 0;
+    int rc = -1;
     struct nlmsghdr *resp;
     struct nlmsgerr *err;
     struct ifinfomsg ifinfo = {
@@ -588,7 +592,7 @@ doPortProfileOpSetLink(bool nltarget_kernel,
     nl_msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST);
     if (!nl_msg) {
         virReportOOMError();
-        return -1;
+        return rc;
     }
 
     if (nlmsg_append(nl_msg,  &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
@@ -690,16 +694,12 @@ doPortProfileOpSetLink(bool nltarget_kernel,
 
     if (!nltarget_kernel) {
         pid = getLldpadPid();
-        if (pid == 0) {
-            rc = -1;
+        if (pid == 0)
             goto err_exit;
-        }
     }
 
-    if (nlComm(nl_msg, &recvbuf, &recvbuflen, pid) < 0) {
-        rc = -1;
+    if (nlComm(nl_msg, &recvbuf, &recvbuflen, pid) < 0)
         goto err_exit;
-    }
 
     if (recvbuflen < NLMSG_LENGTH(0) || recvbuf == NULL)
         goto malformed_resp;
@@ -716,7 +716,7 @@ doPortProfileOpSetLink(bool nltarget_kernel,
             virReportSystemError(-err->error,
                 _("error during virtual port configuration of ifindex %d"),
                 ifindex);
-            rc = -1;
+            goto err_exit;
         }
         break;
 
@@ -727,6 +727,8 @@ doPortProfileOpSetLink(bool nltarget_kernel,
         goto malformed_resp;
     }
 
+    rc = 0;
+
 err_exit:
     nlmsg_free(nl_msg);
 
@@ -740,14 +742,14 @@ malformed_resp:
     macvtapError(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("malformed netlink response message"));
     VIR_FREE(recvbuf);
-    return -1;
+    return rc;
 
 buffer_too_small:
     nlmsg_free(nl_msg);
 
     macvtapError(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("allocated netlink buffer is too small"));
-    return -1;
+    return rc;
 }
 
 
@@ -780,8 +782,7 @@ doPortProfileOpCommon(bool nltarget_kernel,
                                 hostUUID,
                                 vf,
                                 op);
-
-    if (rc) {
+    if (rc < 0) {
         macvtapError(VIR_ERR_INTERNAL_ERROR, "%s",
                      _("sending of PortProfileRequest failed."));
         return rc;
@@ -790,11 +791,12 @@ doPortProfileOpCommon(bool nltarget_kernel,
     while (--repeats >= 0) {
         rc = ifaceMacvtapLinkDump(nltarget_kernel, NULL, ifindex, tb,
                                   &recvbuf, getLldpadPid);
-        if (rc)
+        if (rc < 0)
             goto err_exit;
+
         rc = getPortProfileStatus(tb, vf, instanceId, nltarget_kernel,
                                   is8021Qbg, &status);
-        if (rc)
+        if (rc < 0)
             goto err_exit;
         if (status == PORT_PROFILE_RESPONSE_SUCCESS ||
             status == PORT_VDP_RESPONSE_SUCCESS) {
@@ -818,7 +820,7 @@ doPortProfileOpCommon(bool nltarget_kernel,
     if (status == PORT_PROFILE_RESPONSE_INPROGRESS) {
         macvtapError(VIR_ERR_INTERNAL_ERROR, "%s",
                      _("port-profile setlink timed out"));
-        rc = -ETIMEDOUT;
+        rc = -2;
     }
 
 err_exit:
@@ -843,7 +845,7 @@ getPhysdevAndVlan(const char *ifname, int *root_ifindex, char *root_ifname,
     *vlanid = -1;
     while (1) {
         if ((ret = ifaceGetNthParent(ifindex, ifname, 1,
-                                     root_ifindex, root_ifname, &nth)))
+                                     root_ifindex, root_ifname, &nth)) < 0)
             return ret;
         if (nth == 0)
             break;
@@ -892,7 +894,7 @@ doPortProfileOp8021Qbg(const char *ifname,
     int vf = PORT_SELF_VF;
 
     if (getPhysdevAndVlan(ifname, &physdev_ifindex, physdev_ifname,
-                          &vlanid) != 0)
+                          &vlanid) < 0)
         goto err_exit;
 
     if (vlanid < 0)
@@ -943,10 +945,9 @@ getPhysfnDev(const char *linkdev,
              int32_t *vf,
              char **physfndev)
 {
-    int rc = 0;
-
-    if (ifaceIsVirtualFunction(linkdev)) {
+    int rc = -1;
 
+    if (ifaceIsVirtualFunction(linkdev) == 1) {
         /* if linkdev is SR-IOV VF, then set vf = VF index */
         /* and set linkdev = PF device */
 
@@ -963,10 +964,13 @@ getPhysfnDev(const char *linkdev,
         *physfndev = strdup(linkdev);
         if (!*physfndev) {
             virReportOOMError();
-            rc = -1;
-        }
+            goto err_exit;
+	}
+	rc = 0;
     }
 
+err_exit:
+
     return rc;
 }
 # endif /* IFLA_VF_PORT_MAX */
@@ -1000,7 +1004,7 @@ doPortProfileOp8021Qbh(const char *ifname,
     int vlanid = -1;
 
     rc = getPhysfnDev(ifname, &vf, &physfndev);
-    if (rc)
+    if (rc < 0)
         goto err_exit;
 
     rc = ifaceGetIndex(true, physfndev, &ifindex);
@@ -1011,7 +1015,7 @@ doPortProfileOp8021Qbh(const char *ifname,
     case PREASSOCIATE_RR:
     case ASSOCIATE:
         rc = virGetHostUUID(hostuuid);
-        if (rc)
+        if (rc < 0)
             goto err_exit;
 
         rc = doPortProfileOpCommon(nltarget_kernel, NULL, ifindex,
@@ -1025,7 +1029,7 @@ doPortProfileOp8021Qbh(const char *ifname,
                                    (virtPortOp == PREASSOCIATE_RR) ?
                                     PORT_REQUEST_PREASSOCIATE_RR
                                     : PORT_REQUEST_ASSOCIATE);
-        if (rc == -ETIMEDOUT)
+        if (rc == -2)
             /* Association timed out, disassociate */
             doPortProfileOpCommon(nltarget_kernel, NULL, ifindex,
                                   NULL,
diff --git a/src/util/pci.c b/src/util/pci.c
index 33b4b0e..077f3a2 100644
--- a/src/util/pci.c
+++ b/src/util/pci.c
@@ -1959,11 +1959,11 @@ pciGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
     struct pci_config_address **virt_fns = NULL;
 
     if (pciGetPciConfigAddressFromSysfsDeviceLink(vf_sysfs_device_link,
-        &vf_bdf))
+        &vf_bdf) < 0)
         return ret;
 
     if (pciGetVirtualFunctions(pf_sysfs_device_link, &virt_fns,
-        &num_virt_fns)) {
+        &num_virt_fns) < 0) {
         pciReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Error getting physical function's '%s' "
                       "virtual_functions"), pf_sysfs_device_link);
@@ -1971,7 +1971,7 @@ pciGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
     }
 
     for (i = 0; i < num_virt_fns; i++) {
-         if (pciConfigAddressEqual(vf_bdf, virt_fns[i])) {
+         if (pciConfigAddressEqual(vf_bdf, virt_fns[i]) == 1) {
              *vf_index = i;
              ret = 0;
              break;




More information about the libvir-list mailing list