From nobody Sun Feb 8 07:07:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532806870687961.9591153314936; Sat, 28 Jul 2018 12:41:10 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AAB1A30820DD; Sat, 28 Jul 2018 19:41:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5C0E56596C; Sat, 28 Jul 2018 19:41:07 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 154104A465; Sat, 28 Jul 2018 19:41:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6SJeplC020427 for ; Sat, 28 Jul 2018 15:40:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 100903001A51; Sat, 28 Jul 2018 19:40:51 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 081563083320 for ; Sat, 28 Jul 2018 19:40:49 +0000 (UTC) Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E87708553D for ; Sat, 28 Jul 2018 19:40:47 +0000 (UTC) Received: by mail-pg1-f182.google.com with SMTP id n7-v6so5080502pgq.4 for ; Sat, 28 Jul 2018 12:40:47 -0700 (PDT) Received: from centos.localdomain ([49.32.98.239]) by smtp.gmail.com with ESMTPSA id m26-v6sm20270426pfi.102.2018.07.28.12.40.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Jul 2018 12:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=F1zLvnffDga0+Jf6JheyciGJ76QwMN+gKGx3WpMVhXU=; b=Ud1VQhuC9veXnfmxOHoliF/AH862GUg5zXJR5ap/HbrNWN8rjtdzsKfVeKJsm6+mPf OYhL4ZtgkeSzz7rPvPCkhRkp/cyWt1vSSHbRv9OctgW+Q6Xbz9AWmz9L2uX9aYU3Io4U OISU6AY53FRAnN0CaEAqqtybvvGdvwJ3VavvX1EcR4d00CBluDHKDyOfmm07E7rYjL6C 6oPbYdta6e7zwnQVeHoB+EV/Zczk3mBmvK3qbx6hqJhMAWaebmySaLgfwc2Jhdgu7ft7 23W7WfDhZFUI4GhlozAp3XWbbqhXTEQ6Aefc/ze2S6wO+ZfKkxqPbdj/L60kNaeurxw5 k9Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=F1zLvnffDga0+Jf6JheyciGJ76QwMN+gKGx3WpMVhXU=; b=oplyf738QjJwtcfDfZB3OpTamHwJA1Cfr5CvpBPtZaULnF930hYlk4IJuJD0gcauWn YzovNLurYIXczPwAYFkUm519ogF0aB4J71NWpLGkWx+ORGKg/qHjp+m8H1cGll8BYfLb x59yUaQNKDLLmkianbotyc7xbEdm/ifQPPclKIWPfe5ALP9dAMwy5VyPyKRIZdmCT/BI 8Md0cJdQAKDgaaVLnGCOmmD8yI08/IhiS5xFLoF0Mi+8Qvylaxz0s/odQ5kLdC+Ix6Xq PXod2gEEmIdl/gNn3+Juw+ypaHaBuf70wndMqmnmdVT3NlryrLWdOpEAfyDoKSjAN7XO qVpw== X-Gm-Message-State: AOUpUlHFQTf5B7om2YUZZ9/kJNBhmGpYyUcZ6KIK6gnKI5URYv/XURUe cYWOGNiGEFrZ0lCx5i8KEaBBoEMB X-Google-Smtp-Source: AAOMgpceWmEa72UJYOsYxxeYbKpLeXjRIYW6nyN52TTUDdaSIuygF2DVdvux9XAZwyFF1Wd46cBKMg== X-Received: by 2002:a63:3686:: with SMTP id d128-v6mr10470408pga.18.1532806847198; Sat, 28 Jul 2018 12:40:47 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Sat, 28 Jul 2018 23:31:22 +0530 Message-Id: <1532800907-29115-8-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1532800907-29115-1-git-send-email-skrtbhtngr@gmail.com> References: <1532800907-29115-1-git-send-email-skrtbhtngr@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Sat, 28 Jul 2018 19:40:48 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Sat, 28 Jul 2018 19:40:48 +0000 (UTC) for IP:'209.85.215.182' DOMAIN:'mail-pg1-f182.google.com' HELO:'mail-pg1-f182.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.11 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS) 209.85.215.182 mail-pg1-f182.google.com 209.85.215.182 mail-pg1-f182.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v1 07/32] util: netdev: use VIR_AUTOPTR for aggregate types X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Sat, 28 Jul 2018 19:41:09 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" By making use of GNU C's cleanup attribute handled by the VIR_AUTOPTR macro for declaring aggregate pointer variables, majority of the calls to *Free functions can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar --- src/util/virnetdev.c | 129 ++++++++++++++++++++++-------------------------= ---- 1 file changed, 55 insertions(+), 74 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 7653f8b..c5871b4 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -1855,16 +1855,15 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, const char *stateDir, bool saveVlan) { - int ret =3D -1; const char *pfDevName =3D NULL; VIR_AUTOFREE(char *) pfDevOrig =3D NULL; VIR_AUTOFREE(char *) vfDevOrig =3D NULL; VIR_AUTOFREE(char *) filePath =3D NULL; VIR_AUTOFREE(char *) fileStr =3D NULL; + VIR_AUTOPTR(virJSONValue) configJSON =3D NULL; virMacAddr oldMAC; char MACStr[VIR_MAC_STRING_BUFLEN]; int oldVlanTag =3D -1; - virJSONValuePtr configJSON =3D NULL; =20 if (vf >=3D 0) { /* linkdev is the PF */ @@ -1872,7 +1871,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, =20 /* linkdev should get the VF's netdev name (or NULL if none) */ if (virNetDevPFGetVF(pfDevName, vf, &vfDevOrig) < 0) - goto cleanup; + return -1; =20 linkdev =3D vfDevOrig; saveVlan =3D true; @@ -1884,12 +1883,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, */ =20 if (virNetDevGetPhysicalFunction(linkdev, &pfDevOrig) < 0) - goto cleanup; + return -1; =20 pfDevName =3D pfDevOrig; =20 if (virNetDevGetVirtualFunctionIndex(pfDevName, linkdev, &vf) < 0) - goto cleanup; + return -1; } =20 if (pfDevName) { @@ -1907,7 +1906,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, * explicitly enable the PF in the host system network config. */ if (virNetDevGetOnline(pfDevName, &pfIsOnline) < 0) - goto cleanup; + return -1; =20 if (!pfIsOnline) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -1916,12 +1915,12 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, "change host network config to put the " "PF online."), vf, pfDevName); - goto cleanup; + return -1; } } =20 if (!(configJSON =3D virJSONValueNewObject())) - goto cleanup; + return -1; =20 /* if there is a PF, it's now in pfDevName, and linkdev is either * the VF's name, or NULL (if the VF isn't bound to a net driver @@ -1930,11 +1929,11 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, =20 if (pfDevName && saveVlan) { if (virAsprintf(&filePath, "%s/%s_vf%d", stateDir, pfDevName, vf) = < 0) - goto cleanup; + return -1; =20 /* get admin MAC and vlan tag */ if (virNetDevGetVfConfig(pfDevName, vf, &oldMAC, &oldVlanTag) < 0) - goto cleanup; + return -1; =20 if (virJSONValueObjectAppendString(configJSON, VIR_NETDEV_KEYNAME_ADMIN_MAC, @@ -1942,39 +1941,36 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, virJSONValueObjectAppendNumberInt(configJSON, VIR_NETDEV_KEYNAME_VLAN_TAG, oldVlanTag) < 0) { - goto cleanup; + return -1; } =20 } else { if (virAsprintf(&filePath, "%s/%s", stateDir, linkdev) < 0) - goto cleanup; + return -1; } =20 if (linkdev) { if (virNetDevGetMAC(linkdev, &oldMAC) < 0) - goto cleanup; + return -1; =20 /* for interfaces with no pfDevName (i.e. not a VF, this will * be the only value in the file. */ if (virJSONValueObjectAppendString(configJSON, VIR_NETDEV_KEYNAME_= MAC, virMacAddrFormat(&oldMAC, MACSt= r)) < 0) - goto cleanup; + return -1; } =20 if (!(fileStr =3D virJSONValueToString(configJSON, true))) - goto cleanup; + return -1; =20 if (virFileWriteStr(filePath, fileStr, O_CREAT|O_TRUNC|O_WRONLY) < 0) { virReportSystemError(errno, _("Unable to preserve mac/vlan tag " "for device =3D %s, vf =3D %d"), lin= kdev, vf); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - virJSONValueFree(configJSON); - return ret; + return 0; } =20 =20 @@ -2012,7 +2008,10 @@ virNetDevReadNetConfig(const char *linkdev, int vf, VIR_AUTOFREE(char *) vfDevOrig =3D NULL; VIR_AUTOFREE(char *) filePath =3D NULL; VIR_AUTOFREE(char *) fileStr =3D NULL; - virJSONValuePtr configJSON =3D NULL; + VIR_AUTOPTR(virJSONValue) configJSON =3D NULL; + VIR_AUTOPTR(virMacAddr) tempAdminMAC =3D NULL; + VIR_AUTOPTR(virMacAddr) tempMAC =3D NULL; + VIR_AUTOPTR(virNetDevVlan) tempVlan =3D NULL; const char *MACStr =3D NULL; const char *adminMACStr =3D NULL; int vlanTag =3D -1; @@ -2176,15 +2175,12 @@ virNetDevReadNetConfig(const char *linkdev, int vf, /* we won't need the file again */ ignore_value(unlink(filePath)); =20 - ret =3D 0; + return 0; cleanup: - if (ret < 0) { - VIR_FREE(*adminMAC); - VIR_FREE(*MAC); - VIR_FREE(*vlan); - } + VIR_STEAL_PTR(tempAdminMAC, *adminMAC); + VIR_STEAL_PTR(tempMAC, *MAC); + VIR_STEAL_PTR(tempVlan, *vlan); =20 - virJSONValueFree(configJSON); return ret; } =20 @@ -2214,13 +2210,12 @@ virNetDevSetNetConfig(const char *linkdev, int vf, const virMacAddr *MAC, bool setVlan) { - int ret =3D -1; char MACStr[VIR_MAC_STRING_BUFLEN]; const char *pfDevName =3D NULL; VIR_AUTOFREE(char *) pfDevOrig =3D NULL; VIR_AUTOFREE(char *) vfDevOrig =3D NULL; + VIR_AUTOPTR(virPCIDevice) vfPCIDevice =3D NULL; int vlanTag =3D -1; - virPCIDevicePtr vfPCIDevice =3D NULL; =20 if (vf >=3D 0) { /* linkdev is the PF */ @@ -2228,7 +2223,7 @@ virNetDevSetNetConfig(const char *linkdev, int vf, =20 /* linkdev should get the VF's netdev name (or NULL if none) */ if (virNetDevPFGetVF(pfDevName, vf, &vfDevOrig) < 0) - goto cleanup; + return -1; =20 linkdev =3D vfDevOrig; =20 @@ -2239,12 +2234,12 @@ virNetDevSetNetConfig(const char *linkdev, int vf, */ =20 if (virNetDevGetPhysicalFunction(linkdev, &pfDevOrig) < 0) - goto cleanup; + return -1; =20 pfDevName =3D pfDevOrig; =20 if (virNetDevGetVirtualFunctionIndex(pfDevName, linkdev, &vf) < 0) - goto cleanup; + return -1; } =20 =20 @@ -2256,14 +2251,14 @@ virNetDevSetNetConfig(const char *linkdev, int vf, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("admin MAC can only be set for SR-IOV VFs, bu= t " "%s is not a VF"), linkdev); - goto cleanup; + return -1; } =20 if (vlan) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("vlan can only be set for SR-IOV VFs, but " "%s is not a VF"), linkdev); - goto cleanup; + return -1; } =20 } else { @@ -2272,14 +2267,14 @@ virNetDevSetNetConfig(const char *linkdev, int vf, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("vlan trunking is not supported " "by SR-IOV network devices")); - goto cleanup; + return -1; } =20 if (!setVlan) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("vlan tag set for interface %s but " "caller requested it not be set")); - goto cleanup; + return -1; } =20 vlanTag =3D vlan->tag[0]; @@ -2297,7 +2292,7 @@ virNetDevSetNetConfig(const char *linkdev, int vf, _("VF %d of PF '%s' is not bound to a net drive= r, " "so its MAC address cannot be set to %s"), vf, pfDevName, virMacAddrFormat(MAC, MACStr)); - goto cleanup; + return -1; } =20 setMACrc =3D virNetDevSetMACInternal(linkdev, MAC, !!pfDevOrig); @@ -2308,7 +2303,7 @@ virNetDevSetNetConfig(const char *linkdev, int vf, /* if pfDevOrig =3D=3D NULL, this isn't a VF, so we've failed = */ if (!pfDevOrig || (errno !=3D EADDRNOTAVAIL && errno !=3D EPERM)) - goto cleanup; + return -1; =20 /* Otherwise this is a VF, and virNetDevSetMAC failed with * EADDRNOTAVAIL/EPERM, which could be due to the @@ -2322,18 +2317,18 @@ virNetDevSetNetConfig(const char *linkdev, int vf, =20 if (virNetDevSetVfConfig(pfDevName, vf, MAC, vlanTag, &allowRetry) < 0) { - goto cleanup; + return -1; } =20 /* admin MAC is set, now we need to construct a virPCIDevice * object so we can call virPCIDeviceRebind() */ if (!(vfPCIDevice =3D virNetDevGetPCIDevice(linkdev))) - goto cleanup; + return -1; =20 /* Rebind the device. This should set the proper MAC address */ if (virPCIDeviceRebind(vfPCIDevice) < 0) - goto cleanup; + return -1; =20 /* Wait until virNetDevGetIndex for the VF netdev returns succ= ess. * This indicates that the device is ready to be used. If we d= on't @@ -2385,20 +2380,17 @@ virNetDevSetNetConfig(const char *linkdev, int vf, * with the "locally administered" bit set. */ if (!allowRetry) - goto cleanup; + return -1; =20 allowRetry =3D false; if (virNetDevSetVfConfig(pfDevName, vf, &altZeroMAC, vlanTag, &allowRetry) < = 0) { - goto cleanup; + return -1; } } } =20 - ret =3D 0; - cleanup: - virPCIDeviceFree(vfPCIDevice); - return ret; + return 0; } =20 =20 @@ -2873,30 +2865,31 @@ virNetDevRxFilterFree(virNetDevRxFilterPtr filter) int virNetDevGetRxFilter(const char *ifname, virNetDevRxFilterPtr *filter) { - int ret =3D -1; bool receive =3D false; - virNetDevRxFilterPtr fil =3D virNetDevRxFilterNew(); + VIR_AUTOPTR(virNetDevRxFilter) fil =3D virNetDevRxFilterNew(); + + *filter =3D NULL; =20 if (!fil) - goto cleanup; + return -1; =20 if (virNetDevGetMAC(ifname, &fil->mac)) - goto cleanup; + return -1; =20 if (virNetDevGetMulticastTable(ifname, fil)) - goto cleanup; + return -1; =20 if (virNetDevGetPromiscuous(ifname, &fil->promiscuous)) - goto cleanup; + return -1; =20 if (virNetDevGetRcvAllMulti(ifname, &receive)) - goto cleanup; + return -1; =20 if (receive) { fil->multicast.mode =3D VIR_NETDEV_RX_FILTER_MODE_ALL; } else { if (virNetDevGetRcvMulti(ifname, &receive)) - goto cleanup; + return -1; =20 if (receive) fil->multicast.mode =3D VIR_NETDEV_RX_FILTER_MODE_NORMAL; @@ -2904,15 +2897,8 @@ int virNetDevGetRxFilter(const char *ifname, fil->multicast.mode =3D VIR_NETDEV_RX_FILTER_MODE_NONE; } =20 - ret =3D 0; - cleanup: - if (ret < 0) { - virNetDevRxFilterFree(fil); - fil =3D NULL; - } - - *filter =3D fil; - return ret; + VIR_STEAL_PTR(*filter, fil); + return 0; } =20 #if defined(SIOCETHTOOL) && defined(HAVE_STRUCT_IFREQ) @@ -3185,12 +3171,12 @@ virNetDevSwitchdevFeature(const char *ifname, { struct nl_msg *nl_msg =3D NULL; VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; + VIR_AUTOFREE(char *) pfname =3D NULL; + VIR_AUTOPTR(virPCIDevice) pci_device_ptr =3D NULL; unsigned int recvbuflen; struct nlattr *tb[DEVLINK_ATTR_MAX + 1] =3D {NULL, }; - virPCIDevicePtr pci_device_ptr =3D NULL; struct genlmsghdr* gmsgh =3D NULL; const char *pci_name; - VIR_AUTOFREE(char *) pfname =3D NULL; int is_vf =3D -1; int ret =3D -1; uint32_t family_id; @@ -3251,7 +3237,6 @@ virNetDevSwitchdevFeature(const char *ifname, =20 cleanup: nlmsg_free(nl_msg); - virPCIDeviceFree(pci_device_ptr); return ret; } # else @@ -3507,8 +3492,7 @@ int virNetDevSetCoalesce(const char *ifname, int virNetDevRunEthernetScript(const char *ifname, const char *script) { - virCommandPtr cmd; - int ret; + VIR_AUTOPTR(virCommand) cmd =3D NULL; =20 /* Not a bug! Previously we did accept script=3D"" as a NO-OP. */ if (STREQ(script, "")) @@ -3522,8 +3506,5 @@ virNetDevRunEthernetScript(const char *ifname, const = char *script) #endif virCommandAddEnvPassCommon(cmd); =20 - ret =3D virCommandRun(cmd, NULL); - - virCommandFree(cmd); - return ret; + return virCommandRun(cmd, NULL); } --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list