]> xenbits.xensource.com Git - libvirt.git/commitdiff
Rename ifaceGetIndex and ifaceGetVLAN
authorDaniel P. Berrange <berrange@redhat.com>
Thu, 3 Nov 2011 09:21:35 +0000 (09:21 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 18 Nov 2011 16:10:02 +0000 (16:10 +0000)
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

src/libvirt_private.syms
src/nwfilter/nwfilter_gentech_driver.c
src/nwfilter/nwfilter_learnipaddr.c
src/util/interface.c
src/util/interface.h
src/util/virnetdevmacvlan.c
src/util/virnetdevvportprofile.c

index dfc1f10967b08755a30cacc6045abaed011f803f..8885b1dc8de7c4b055df2f0b7be01c9f3fea642a 100644 (file)
@@ -576,12 +576,12 @@ virHookPresent;
 
 # interface.h
 ifaceCheck;
-ifaceGetIndex;
+virNetDevGetIndex;
 ifaceGetIPAddress;
 ifaceGetNthParent;
 ifaceGetPhysicalFunction;
 ifaceGetVirtualFunctionIndex;
-ifaceGetVlanID;
+virNetDevGetVLanID;
 ifaceIsVirtualFunction;
 virNetDevMacVLanCreate;
 virNetDevMacVLanDelete;
index 899bd322fdc985be0ad98099ac926e6965312646..608e3940a8c7a5559a4ba29ca386a8061239d476 100644 (file)
@@ -903,9 +903,11 @@ _virNWFilterInstantiateFilter(virConnectPtr conn,
     /* after grabbing the filter update lock check for the interface; if
        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 */
-    if (ifaceGetIndex(false, net->ifname, &ifindex) < 0) {
+    if (virNetDevExists(net->ifname) != 1 ||
+        virNetDevGetIndex(net->ifname, &ifindex) < 0) {
         /* interfaces / VMs can disappear during filter instantiation;
            don't mark it as an error */
+        virResetLastError();
         rc = 0;
         goto cleanup;
     }
@@ -1021,8 +1023,9 @@ int virNWFilterRollbackUpdateFilter(virConnectPtr conn,
     }
 
     /* don't tear anything while the address is being learned */
-    if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 &&
-        virNWFilterLookupLearnReq(ifindex) != NULL)
+    if (virNetDevGetIndex(net->ifname, &ifindex) < 0)
+        virResetLastError();
+    else if (virNWFilterLookupLearnReq(ifindex) != NULL)
         return 0;
 
     return techdriver->tearNewRules(conn, net->ifname);
@@ -1047,8 +1050,9 @@ virNWFilterTearOldFilter(virConnectPtr conn,
     }
 
     /* don't tear anything while the address is being learned */
-    if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 &&
-        virNWFilterLookupLearnReq(ifindex) != NULL)
+    if (virNetDevGetIndex(net->ifname, &ifindex) < 0)
+        virResetLastError();
+    else if (virNWFilterLookupLearnReq(ifindex) != NULL)
         return 0;
 
     return techdriver->tearOldRules(conn, net->ifname);
index 68bdcfcb1d1317f8cc4c2156bfb2dd144526838f..9a51fc278a4ef5c3f78f8861f0860199c2be2941 100644 (file)
@@ -252,21 +252,23 @@ virNWFilterTerminateLearnReq(const char *ifname) {
     int ifindex;
     virNWFilterIPAddrLearnReqPtr req;
 
-    if (ifaceGetIndex(false, ifname, &ifindex) == 0) {
-
-        IFINDEX2STR(ifindex_str, ifindex);
+    if (virNetDevGetIndex(ifname, &ifindex) < 0) {
+        virResetLastError();
+        return rc;
+    }
 
-        virMutexLock(&pendingLearnReqLock);
+    IFINDEX2STR(ifindex_str, ifindex);
 
-        req = virHashLookup(pendingLearnReq, ifindex_str);
-        if (req) {
-            rc = 0;
-            req->terminate = true;
-        }
+    virMutexLock(&pendingLearnReqLock);
 
-        virMutexUnlock(&pendingLearnReqLock);
+    req = virHashLookup(pendingLearnReq, ifindex_str);
+    if (req) {
+        rc = 0;
+        req->terminate = true;
     }
 
+    virMutexUnlock(&pendingLearnReqLock);
+
     return rc;
 }
 
index ebe537e3cce707023d76870797c49b81a8ef608e..af1def23cd676a4b9fbeba57ec29ef9daaa2baaf 100644 (file)
@@ -115,8 +115,9 @@ ifaceCheck(bool reportError, const char *ifname,
     }
 
     if (ifindex != -1) {
-        rc = ifaceGetIndex(reportError, ifname, &idx);
-        if (rc == 0 && idx != ifindex)
+        if (virNetDevGetIndex(ifname, &idx) < 0)
+            virResetLastError();
+        else if (idx != ifindex)
             rc = -ENODEV;
     }
 
@@ -141,114 +142,112 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED,
 
 
 /**
- * ifaceGetIndex
- *
- * @reportError: whether to report errors or keep silent
+ * virNetDevGetIndex
  * @ifname : Name of the interface whose index is to be found
  * @ifindex: Pointer to int where the index will be written into
  *
  * Get the index of an interface given its name.
  *
- * Returns 0 on success, -errno on failure.
- *   -ENODEV : if interface with given name does not exist
- *   -EINVAL : if interface name is invalid (too long)
+ * Returns 0 on success, -1 on failure
  */
 #ifdef __linux__
 int
-ifaceGetIndex(bool reportError, const char *ifname, int *ifindex)
+virNetDevGetIndex(const char *ifname, int *ifindex)
 {
-    int rc = 0;
+    int ret = -1;
     struct ifreq ifreq;
     int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
 
-    if (fd < 0)
-        return -errno;
+    if (fd < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to open control socket"));
+        return -1;
+    }
 
     memset(&ifreq, 0, sizeof(ifreq));
 
     if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname),
                    sizeof(ifreq.ifr_name)) == NULL) {
-        if (reportError)
-            ifaceError(VIR_ERR_INTERNAL_ERROR,
-                       _("invalid interface name %s"),
-                       ifname);
-        rc = -EINVAL;
+        virReportSystemError(ERANGE,
+                             _("invalid interface name %s"),
+                             ifname);
         goto cleanup;
     }
 
-    if (ioctl(fd, SIOCGIFINDEX, &ifreq) >= 0)
-        *ifindex = ifreq.ifr_ifindex;
-    else {
-        if (reportError)
-            ifaceError(VIR_ERR_INTERNAL_ERROR,
-                       _("interface %s does not exist"),
-                       ifname);
-        rc = -ENODEV;
+    if (ioctl(fd, SIOCGIFINDEX, &ifreq) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to get index for interface %s"), ifname);
+        goto cleanup;
     }
 
+    *ifindex = ifreq.ifr_ifindex;
+    ret = 0;
+
 cleanup:
     VIR_FORCE_CLOSE(fd);
-
-    return rc;
+    return ret;
 }
 
 #else
 
 int
-ifaceGetIndex(bool reportError,
-              const char *ifname ATTRIBUTE_UNUSED,
-              int *ifindex ATTRIBUTE_UNUSED)
+virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
+                  int *ifindex ATTRIBUTE_UNUSED)
 {
-    if (reportError) {
-        ifaceError(VIR_ERR_INTERNAL_ERROR, "%s",
-                   _("ifaceGetIndex is not supported on non-linux platforms"));
-    }
-
-    return -ENOSYS;
+    virReportSystemError(ENOSYS, "%s",
+                         _("Unable to get interface index on this platform"));
+    return -1;
 }
 
 #endif /* __linux__ */
 
 #ifdef __linux__
 int
-ifaceGetVlanID(const char *vlanifname, int *vlanid) {
+virNetDevGetVLanID(const char *ifname, int *vlanid)
+{
     struct vlan_ioctl_args vlanargs = {
       .cmd = GET_VLAN_VID_CMD,
     };
-    int rc = 0;
+    int ret = -1;
     int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
 
-    if (fd < 0)
-        return -errno;
+    if (fd < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to open control socket"));
+        return -1;
+    }
 
-    if (virStrcpyStatic(vlanargs.device1, vlanifname) == NULL) {
-        rc = -EINVAL;
+    if (virStrcpyStatic(vlanargs.device1, ifname) == NULL) {
+        virReportSystemError(ERANGE,
+                             _("invalid interface name %s"),
+                             ifname);
         goto cleanup;
     }
 
     if (ioctl(fd, SIOCGIFVLAN, &vlanargs) != 0) {
-        rc = -errno;
+        virReportSystemError(errno,
+                             _("Unable to get VLAN for interface %s"), ifname);
         goto cleanup;
     }
 
     *vlanid = vlanargs.u.VID;
+    ret = 0;
 
  cleanup:
     VIR_FORCE_CLOSE(fd);
 
-    return rc;
+    return ret;
 }
 
 #else
 
 int
-ifaceGetVlanID(const char *vlanifname ATTRIBUTE_UNUSED,
-               int *vlanid ATTRIBUTE_UNUSED) {
-
-    ifaceError(VIR_ERR_INTERNAL_ERROR, "%s",
-               _("ifaceGetVlanID is not supported on non-linux platforms"));
-
-    return -ENOSYS;
+virNetDevGetVLanID(const char *ifname ATTRIBUTE_UNUSED,
+                   int *vlanid ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Unable to get VLAN on this platform"));
+    return -1;
 }
 #endif /* __linux__ */
 
@@ -496,7 +495,7 @@ ifaceGetNthParent(int ifindex, const char *ifname, unsigned int nthParent,
 
     *nth = 0;
 
-    if (ifindex <= 0 && ifaceGetIndex(true, ifname, &ifindex) < 0)
+    if (ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
         return -1;
 
     while (!end && i <= nthParent) {
index e322a2128fea8e62cd68049c0327866f1915c668..51d5c28e07c044dd143c169b2bf82bc021cfb357 100644 (file)
@@ -33,9 +33,11 @@ struct nlattr;
 int ifaceCheck(bool reportError, const char *ifname,
                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);
 
index 8e2e7370516a2caac6b772ee2d2f11c9797c465c..e3f90dd8e63c87a58393d7b6b39b368e8d2ebaa6 100644 (file)
@@ -107,7 +107,7 @@ virNetDevMacVLanCreate(const char *ifname,
     struct nl_msg *nl_msg;
     struct nlattr *linkinfo, *info_data;
 
-    if (ifaceGetIndex(true, srcdev, &ifindex) < 0)
+    if (virNetDevGetIndex(srcdev, &ifindex) < 0)
         return -1;
 
     *retry = 0;
@@ -481,7 +481,7 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
     int retries, do_retry = 0;
     uint32_t macvtapMode;
     const char *cr_ifname;
-    int ifindex;
+    int ret;
 
     macvtapMode = modeMap[mode];
 
@@ -502,13 +502,16 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
     }
 
     if (tgifname) {
-        if(ifaceGetIndex(false, tgifname, &ifindex) == 0) {
+        if ((ret = virNetDevExists(tgifname)) < 0)
+            return -1;
+
+        if (ret) {
             if (STRPREFIX(tgifname,
                           MACVTAP_NAME_PREFIX)) {
                 goto create_name;
             }
-            virReportSystemError(errno,
-                                 _("Interface %s already exists"), tgifname);
+            virReportSystemError(EEXIST,
+                                 _("Unable to create macvlan device %s"), tgifname);
             return -1;
         }
         cr_ifname = tgifname;
@@ -521,7 +524,9 @@ create_name:
         retries = 5;
         for (c = 0; c < 8192; 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,
                                             macvtapMode, &do_retry);
                 if (rc == 0)
index ec86e2c4e1176d2f1ac031d467cc938d50935dd1..c49d5146edf416b1f4641a74ef9003c05d73486a 100644 (file)
@@ -544,8 +544,10 @@ virNetDevVPortProfileGetPhysdevAndVlan(const char *ifname, int *root_ifindex, ch
         if (nth == 0)
             break;
         if (*vlanid == -1) {
-            if (ifaceGetVlanID(root_ifname, vlanid) < 0)
+            if (virNetDevGetVLanID(root_ifname, vlanid) < 0) {
+                virResetLastError();
                 *vlanid = -1;
+            }
         }
 
         ifindex = *root_ifindex;
@@ -676,7 +678,7 @@ virNetDevVPortProfileOp8021Qbh(const char *ifname,
     if (rc < 0)
         goto err_exit;
 
-    rc = ifaceGetIndex(true, physfndev, &ifindex);
+    rc = virNetDevGetIndex(physfndev, &ifindex);
     if (rc < 0)
         goto err_exit;