From nobody Mon Apr 29 14:08:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587362753; cv=none; d=zohomail.com; s=zohoarc; b=QiQ5Q5922iGi7RyDcLkE6h/F9O94ARfO1B+6FYmX9PqB7rMhC7wARuigHi4HQmc0zkfQcWDP1gs8Cq+dOpLNZduRJ2OTnFA5Pb5hKPUfSC5Pz1udJIXsKIQJhzudvQ4tZNBkPglbmcfeQevnZyRFRR54aBptaqnfN93lIViRV94= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587362753; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Z2sczx9VqFqCvlPrgyTWLjN7d4KpOZAH9GqdBj6t0VA=; b=fKlZCMBFT+NlcC/trYLzKcPOMH51iyXQhHgIrrF8nDYYwn+P0Uwx4XSaO+2KFdtb1YoOlLWWwuH9DIZBvMTmLyBFonKN++RB+1FR5MywyQaTQEmyJys197DUKdebJvSaq+2rk/2mynvn+tFlw4B93xMmN/bMKjvJEPIbatcbsjE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1587362753480888.4051629286018; Sun, 19 Apr 2020 23:05:53 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-195-VFhXdL2gObecfH0Wjt_JIw-1; Mon, 20 Apr 2020 02:05:49 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E1C04107ACC9; Mon, 20 Apr 2020 06:05:43 +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 AF82F5E001; Mon, 20 Apr 2020 06:05:43 +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 47E3593901; Mon, 20 Apr 2020 06:05:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 03K65g1j025197 for ; Mon, 20 Apr 2020 02:05:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6BF585DA83; Mon, 20 Apr 2020 06:05:42 +0000 (UTC) Received: from lisa.redhat.com (unknown [10.40.192.219]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFCAF5DA7C for ; Mon, 20 Apr 2020 06:05:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587362752; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Z2sczx9VqFqCvlPrgyTWLjN7d4KpOZAH9GqdBj6t0VA=; b=cux3Iet1dVLqYbUUFzwmpUzaTkogYrZzZaxFpi9L9CGlRSPX3LKY+7VBPCBs5mDzfKGuJ3 hN/Hj0VMKAvzJyfbe223g3cFj++Pa1ggZHxYr2p3OssfG6w08R2cmjzIfEWRJa7VrK7hmn FLJnSytqifYpCl0kwnmvTYUPJ0JgnjM= X-MC-Unique: VFhXdL2gObecfH0Wjt_JIw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/3] virNetDevGetFamilyId: Change signature Date: Mon, 20 Apr 2020 08:05:34 +0200 Message-Id: <47aa35a66e8314985ed675b1d761c9131164a3f6.1587362539.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Introduced in v3.8.0-rc1~96, the virNetDevGetFamilyId() gets netlink family ID for passed family name (even though it's used only for getting "devlink" ID). Nevertheless, the function returns 0 on an error or if no family ID was found. This makes it harder for a caller to distinguish these two. Change the retval so that a negative value is returned upon error, zero is no ID found (but no error encountered) and a positive value is returned on successful translation. Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/util/virnetdev.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index b465bdac2e..8c149c5fc5 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3057,11 +3057,15 @@ virNetDevGetEthtoolFeatures(virBitmapPtr bitmap, * This function supplies the devlink family id * * @family_name: the name of the family to query + * @family_id: family ID * - * Returns family id or 0 on failure. + * Returns: 0 if no family was found, + * 1 if family was found (@family_id is set), + * -1 othewise. */ -static uint32_t -virNetDevGetFamilyId(const char *family_name) +static int +virNetDevGetFamilyId(const char *family_name, + uint32_t *family_id) { struct nl_msg *nl_msg =3D NULL; struct nlmsghdr *resp =3D NULL; @@ -3072,7 +3076,7 @@ virNetDevGetFamilyId(const char *family_name) }; struct nlattr *tb[CTRL_ATTR_MAX + 1] =3D {NULL, }; unsigned int recvbuflen; - uint32_t family_id =3D 0; + int ret =3D -1; =20 if (!(nl_msg =3D nlmsg_alloc_simple(GENL_ID_CTRL, NLM_F_REQUEST | NLM_F_ACK))) { @@ -3098,15 +3102,18 @@ virNetDevGetFamilyId(const char *family_name) goto cleanup; } =20 - if (tb[CTRL_ATTR_FAMILY_ID] =3D=3D NULL) + if (tb[CTRL_ATTR_FAMILY_ID] =3D=3D NULL) { + ret =3D 0; goto cleanup; + } =20 - family_id =3D *(uint32_t *)RTA_DATA(tb[CTRL_ATTR_FAMILY_ID]); + *family_id =3D *(uint32_t *)RTA_DATA(tb[CTRL_ATTR_FAMILY_ID]); + ret =3D 1; =20 cleanup: nlmsg_free(nl_msg); VIR_FREE(resp); - return family_id; + return ret; } =20 =20 @@ -3140,7 +3147,7 @@ virNetDevSwitchdevFeature(const char *ifname, int ret =3D -1; uint32_t family_id; =20 - if ((family_id =3D virNetDevGetFamilyId(DEVLINK_GENL_NAME)) <=3D 0) + if (virNetDevGetFamilyId(DEVLINK_GENL_NAME, &family_id) <=3D 0) return ret; =20 if ((is_vf =3D virNetDevIsVirtualFunction(ifname)) < 0) --=20 2.25.3 From nobody Mon Apr 29 14:08:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587362757; cv=none; d=zohomail.com; s=zohoarc; b=KXjDYpL4d1lEKEJhsY04YL8j6oLk+x2xOCqf4Y1pMQon0gXcaUpGS7v43RwvPcX+293dh9+SpV++m/fMb6sH+jM+mAsILO/il/gVxL3f5peuKa2IThR9GCoTJQ2L9XlvhZL01LBO7ajjYDVWEdk7n/ocMYC5HsJCZp2qOVUw/aU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587362757; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fBws5Ym32Vfxoo6QPTNsFNZoXgECku23gkLFqxK7bLA=; b=jMgXo3ku6toTZ75DOVHm+xc0YbjqWNd42wIVuZsNYuuwzuXnyMS7LKv5sUsJeX/k8uYeg+jHYcSCELGiKfU0bfRcE8P9MTGz87ZLJX/6oWE31XGsyc/LGjjDuaH9lLes0P2xImLq66pTAyxnodPJMqAz2lRhjNRqDnKOJ93TYWc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1587362757751993.1635870261923; Sun, 19 Apr 2020 23:05:57 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-441-DmuUgm9JObyNpkucHzp2nA-1; Mon, 20 Apr 2020 02:05:54 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0C5538018A7; Mon, 20 Apr 2020 06:05:48 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D4EEC5C1C3; Mon, 20 Apr 2020 06:05:47 +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 851B418089D6; Mon, 20 Apr 2020 06:05:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 03K65hGr025202 for ; Mon, 20 Apr 2020 02:05:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4C5CF5DA83; Mon, 20 Apr 2020 06:05:43 +0000 (UTC) Received: from lisa.redhat.com (unknown [10.40.192.219]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF6DC5DA7C for ; Mon, 20 Apr 2020 06:05:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587362756; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=fBws5Ym32Vfxoo6QPTNsFNZoXgECku23gkLFqxK7bLA=; b=eBogARL4n54mLEcPlhzv7OHbijJfYM4iyRTq+LQ6KmeXpI+DHTWRW9+4QnSKjX9jYgayqM mrdl2obG5AJ7wSIFtv7Mud6v+x/1u2N3h34FTY9cNHJR3w8Jr9Dz+/+SFKyKU+HN9p3ifs AdxscbjaaP/3OkUrQzvFIcZsRL0jJO0= X-MC-Unique: DmuUgm9JObyNpkucHzp2nA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/3] virNetDevSwitchdevFeature: Make failure to get 'family_id' non-fatal Date: Mon, 20 Apr 2020 08:05:35 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , 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-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" I've just got a new machine and I'm still converging on the kernel config. Anyway, since I don't have enabled any of SRIO-V drivers, my kernel doesn't have NET_DEVLINK enabled (i.e. virNetDevGetFamilyId() returns 0). But this makes nodedev driver ignore all intefaces, because when enumerating all devices via udev, the control reaches virNetDevSwitchdevFeature() eventually and subsequently virNetDevGetFamilyId() which 'fails'. Well, it's not really a failure - the virNetDevSwitchdevFeature() stub simply returns 0. Also, move the call a few lines below, just around the place where it's needed. Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/util/virnetdev.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 8c149c5fc5..9bca8ce759 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3146,9 +3146,7 @@ virNetDevSwitchdevFeature(const char *ifname, int is_vf =3D -1; int ret =3D -1; uint32_t family_id; - - if (virNetDevGetFamilyId(DEVLINK_GENL_NAME, &family_id) <=3D 0) - return ret; + int rv; =20 if ((is_vf =3D virNetDevIsVirtualFunction(ifname)) < 0) return ret; @@ -3168,6 +3166,9 @@ virNetDevSwitchdevFeature(const char *ifname, goto cleanup; } =20 + if ((rv =3D virNetDevGetFamilyId(DEVLINK_GENL_NAME, &family_id)) <=3D = 0) + return rv; + if (!(nl_msg =3D nlmsg_alloc_simple(family_id, NLM_F_REQUEST | NLM_F_ACK))) { virReportOOMError(); --=20 2.25.3 From nobody Mon Apr 29 14:08:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587362756; cv=none; d=zohomail.com; s=zohoarc; b=c1IfOwncf+wTpWGGF+BiT95ip9UPKX6zupz7eoWa2biR6CFLmNT9Prg1k6gEHlM5n3U6zEtYxqrmMDzOlVZuLtAJESGrWxQrrDVf2gB/mSdnlKbCl8bMd18ZbckZda0QreNhF22t+dldeQYm2AOkM5vNmN+WrV0aqJ9faah6m5E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587362756; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3aY1D1VZEy2ek1aVlqcEH6vm9zRC5Plz/+rQI3Ih/3M=; b=hWDU/ufD5UnqULfwNA8gR8Z90WHHmRkY5WyN7rUVPsvW23q1M365QVt1Rgp7ROwAJ/M9PeOZwmLxeXo3VdyiQE0//9TnYpxE82RyUVan4r0gVFeRADKw+Y6hpyyuejD/eybaosUezaCQeRHB8keqwZ9Sc6pODpTQ6MunjHmiqLI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1587362756854848.6206279866913; Sun, 19 Apr 2020 23:05:56 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-298-edeU_hVyNgWgFBfw4dCySw-1; Mon, 20 Apr 2020 02:05:51 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8D2888017FC; Mon, 20 Apr 2020 06:05:46 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 669CF129F8D; Mon, 20 Apr 2020 06:05:46 +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 1CFFA18089CF; Mon, 20 Apr 2020 06:05:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 03K65iDw025215 for ; Mon, 20 Apr 2020 02:05:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 57BA15DA84; Mon, 20 Apr 2020 06:05:44 +0000 (UTC) Received: from lisa.redhat.com (unknown [10.40.192.219]) by smtp.corp.redhat.com (Postfix) with ESMTP id A02D65DA7C for ; Mon, 20 Apr 2020 06:05:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587362754; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=3aY1D1VZEy2ek1aVlqcEH6vm9zRC5Plz/+rQI3Ih/3M=; b=BDFG+cBd6rgyUXg14FHEA8Z+6eGQASflKJ6R/xgqEQC05WX3SIKSoEy9j4Zox6X1u/xOwC 7tcqtYn9eYKhEb+IP8V/0LNN+fo4P+cvsC9g3vu6DE+OVCyD18XcQ2ltqQ9EdFYiic53ml HD8fJMjUeKRn/lUFDfXbgvAGTfC4Hak= X-MC-Unique: edeU_hVyNgWgFBfw4dCySw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/3] virnetdev.c: Use g_auto*() Date: Mon, 20 Apr 2020 08:05:36 +0200 Message-Id: <3416b1ece56820fe2e563f1465ddd493699d0b05.1587362539.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" While I'm at it, use more g_autofree and g_autoptr() in this file. This also fixes a possible mem-leak in virNetDevGetVirtualFunctions(). Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/util/virnetdev.c | 384 ++++++++++++++++--------------------------- 1 file changed, 139 insertions(+), 245 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 9bca8ce759..650e81c456 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -505,17 +505,16 @@ int virNetDevSetMTUFromDevice(const char *ifname, */ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs) { - int ret =3D -1; - char *pid =3D NULL; - char *phy =3D NULL; - char *phy_path =3D NULL; + g_autofree char *pid =3D NULL; + g_autofree char *phy =3D NULL; + g_autofree char *phy_path =3D NULL; int len; =20 pid =3D g_strdup_printf("%lld", (long long) pidInNs); =20 /* The 802.11 wireless devices only move together with their PHY. */ if (virNetDevSysfsFile(&phy_path, ifname, "phy80211/name") < 0) - goto cleanup; + return -1; =20 if ((len =3D virFileReadAllQuiet(phy_path, 1024, &phy)) <=3D 0) { /* Not a wireless device. */ @@ -525,7 +524,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pid= InNs) =20 argv[5] =3D pid; if (virRun(argv, NULL) < 0) - goto cleanup; + return -1; =20 } else { const char *argv[] =3D { @@ -538,15 +537,10 @@ int virNetDevSetNamespace(const char *ifname, pid_t p= idInNs) argv[2] =3D phy; argv[5] =3D pid; if (virRun(argv, NULL) < 0) - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(phy_path); - VIR_FREE(phy); - VIR_FREE(pid); - return ret; + return 0; } =20 #if defined(SIOCSIFNAME) && defined(HAVE_STRUCT_IFREQ) @@ -913,25 +907,21 @@ int virNetDevGetIndex(const char *ifname G_GNUC_UNUSE= D, int virNetDevGetMaster(const char *ifname, char **master) { - int ret =3D -1; - void *nlData =3D NULL; + g_autofree void *nlData =3D NULL; struct nlattr *tb[IFLA_MAX + 1] =3D {NULL, }; =20 *master =3D NULL; =20 if (virNetlinkDumpLink(ifname, -1, &nlData, tb, 0, 0) < 0) - goto cleanup; + return -1; =20 if (tb[IFLA_MASTER]) { if (!(*master =3D virNetDevGetName(*(int *)RTA_DATA(tb[IFLA_MASTER= ])))) - goto cleanup; + return -1; } =20 VIR_DEBUG("IFLA_MASTER for %s is %s", ifname, *master ? *master : "(no= ne)"); - ret =3D 0; - cleanup: - VIR_FREE(nlData); - return ret; + return 0; } =20 =20 @@ -1090,57 +1080,44 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_lin= k, const char *ifname, static bool virNetDevIsPCIDevice(const char *devpath) { - char *subsys_link =3D NULL; - char *abs_path =3D NULL; + g_autofree char *subsys_link =3D NULL; + g_autofree char *abs_path =3D NULL; g_autofree char *subsys =3D NULL; - bool ret =3D false; =20 subsys_link =3D g_strdup_printf("%s/subsystem", devpath); =20 if (!virFileExists(subsys_link)) - goto cleanup; + return false; =20 if (virFileResolveLink(subsys_link, &abs_path) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to resolve device subsystem symlink %s"), subsys_link); - goto cleanup; + return false; } =20 subsys =3D g_path_get_basename(abs_path); - ret =3D STRPREFIX(subsys, "pci"); - - cleanup: - VIR_FREE(subsys_link); - VIR_FREE(abs_path); - return ret; + return STRPREFIX(subsys, "pci"); } =20 static virPCIDevicePtr virNetDevGetPCIDevice(const char *devName) { - char *vfSysfsDevicePath =3D NULL; - virPCIDeviceAddressPtr vfPCIAddr =3D NULL; - virPCIDevicePtr vfPCIDevice =3D NULL; + g_autofree char *vfSysfsDevicePath =3D NULL; + g_autoptr(virPCIDeviceAddress) vfPCIAddr =3D NULL; =20 if (virNetDevSysfsFile(&vfSysfsDevicePath, devName, "device") < 0) - goto cleanup; + return NULL; =20 if (!virNetDevIsPCIDevice(vfSysfsDevicePath)) - goto cleanup; + return NULL; =20 vfPCIAddr =3D virPCIGetDeviceAddressFromSysfsLink(vfSysfsDevicePath); if (!vfPCIAddr) - goto cleanup; + return NULL; =20 - vfPCIDevice =3D virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus, - vfPCIAddr->slot, vfPCIAddr->function); - - cleanup: - VIR_FREE(vfSysfsDevicePath); - VIR_FREE(vfPCIAddr); - - return vfPCIDevice; + return virPCIDeviceNew(vfPCIAddr->domain, vfPCIAddr->bus, + vfPCIAddr->slot, vfPCIAddr->function); } =20 =20 @@ -1162,25 +1139,20 @@ int virNetDevGetPhysPortID(const char *ifname, char **physPortID) { - int ret =3D -1; - char *physPortIDFile =3D NULL; + g_autofree char *physPortIDFile =3D NULL; =20 *physPortID =3D NULL; =20 if (virNetDevSysfsFile(&physPortIDFile, ifname, "phys_port_id") < 0) - goto cleanup; + return -1; =20 /* a failure to read just means the driver doesn't support * phys_port_id, so set success now and ignore the return from * virFileReadAllQuiet(). */ - ret =3D 0; =20 ignore_value(virFileReadAllQuiet(physPortIDFile, 1024, physPortID)); - - cleanup: - VIR_FREE(physPortIDFile); - return ret; + return 0; } =20 =20 @@ -1203,10 +1175,8 @@ virNetDevGetVirtualFunctions(const char *pfname, { int ret =3D -1; size_t i; - char *pf_sysfs_device_link =3D NULL; - char *pci_sysfs_device_link =3D NULL; - char *pciConfigAddr =3D NULL; - char *pfPhysPortID =3D NULL; + g_autofree char *pf_sysfs_device_link =3D NULL; + g_autofree char *pfPhysPortID =3D NULL; =20 *virt_fns =3D NULL; *n_vfname =3D 0; @@ -1226,6 +1196,9 @@ virNetDevGetVirtualFunctions(const char *pfname, goto cleanup; =20 for (i =3D 0; i < *n_vfname; i++) { + g_autofree char *pciConfigAddr =3D NULL; + g_autofree char *pci_sysfs_device_link =3D NULL; + if (!(pciConfigAddr =3D virPCIDeviceAddressAsString((*virt_fns)[i]= ))) goto cleanup; =20 @@ -1248,13 +1221,14 @@ virNetDevGetVirtualFunctions(const char *pfname, =20 cleanup: if (ret < 0) { - VIR_FREE(*vfname); + virStringListFreeCount(*vfname, *n_vfname); + + for (i =3D 0; i < *n_vfname; i++) + VIR_FREE((*virt_fns)[i]); VIR_FREE(*virt_fns); + *vfname =3D NULL; + *n_vfname =3D 0; } - VIR_FREE(pfPhysPortID); - VIR_FREE(pf_sysfs_device_link); - VIR_FREE(pci_sysfs_device_link); - VIR_FREE(pciConfigAddr); return ret; } =20 @@ -1270,17 +1244,12 @@ virNetDevGetVirtualFunctions(const char *pfname, int virNetDevIsVirtualFunction(const char *ifname) { - char *if_sysfs_device_link =3D NULL; - int ret =3D -1; + g_autofree char *if_sysfs_device_link =3D NULL; =20 if (virNetDevSysfsFile(&if_sysfs_device_link, ifname, "device") < 0) - return ret; + return -1; =20 - ret =3D virPCIIsVirtualFunction(if_sysfs_device_link); - - VIR_FREE(if_sysfs_device_link); - - return ret; + return virPCIIsVirtualFunction(if_sysfs_device_link); } =20 /** @@ -1298,25 +1267,18 @@ int virNetDevGetVirtualFunctionIndex(const char *pfname, const char *vfname, int *vf_index) { - char *pf_sysfs_device_link =3D NULL, *vf_sysfs_device_link =3D NULL; - int ret =3D -1; + g_autofree char *pf_sysfs_device_link =3D NULL; + g_autofree char *vf_sysfs_device_link =3D NULL; =20 if (virNetDevSysfsFile(&pf_sysfs_device_link, pfname, "device") < 0) - return ret; + return -1; =20 - if (virNetDevSysfsFile(&vf_sysfs_device_link, vfname, "device") < 0) { - VIR_FREE(pf_sysfs_device_link); - return ret; - } + if (virNetDevSysfsFile(&vf_sysfs_device_link, vfname, "device") < 0) + return -1; =20 - ret =3D virPCIGetVirtualFunctionIndex(pf_sysfs_device_link, - vf_sysfs_device_link, - vf_index); - - VIR_FREE(pf_sysfs_device_link); - VIR_FREE(vf_sysfs_device_link); - - return ret; + return virPCIGetVirtualFunctionIndex(pf_sysfs_device_link, + vf_sysfs_device_link, + vf_index); } =20 /** @@ -1332,19 +1294,18 @@ virNetDevGetVirtualFunctionIndex(const char *pfname= , const char *vfname, int virNetDevGetPhysicalFunction(const char *ifname, char **pfname) { - char *physfn_sysfs_path =3D NULL; - char *vfPhysPortID =3D NULL; - int ret =3D -1; + g_autofree char *physfn_sysfs_path =3D NULL; + g_autofree char *vfPhysPortID =3D NULL; =20 if (virNetDevGetPhysPortID(ifname, &vfPhysPortID) < 0) - goto cleanup; + return -1; =20 if (virNetDevSysfsDeviceFile(&physfn_sysfs_path, ifname, "physfn") < 0) - goto cleanup; + return -1; =20 if (virPCIGetNetName(physfn_sysfs_path, 0, vfPhysPortID, pfname) < 0) { - goto cleanup; + return -1; } =20 if (!*pfname) { @@ -1353,14 +1314,10 @@ virNetDevGetPhysicalFunction(const char *ifname, ch= ar **pfname) virReportError(VIR_ERR_INTERNAL_ERROR, _("The PF device for VF %s has no network device na= me"), ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(vfPhysPortID); - VIR_FREE(physfn_sysfs_path); - return ret; + return 0; } =20 =20 @@ -1384,17 +1341,16 @@ virNetDevGetPhysicalFunction(const char *ifname, ch= ar **pfname) int virNetDevPFGetVF(const char *pfname, int vf, char **vfname) { - char *virtfnName =3D NULL; - char *virtfnSysfsPath =3D NULL; - char *pfPhysPortID =3D NULL; - int ret =3D -1; + g_autofree char *virtfnName =3D NULL; + g_autofree char *virtfnSysfsPath =3D NULL; + g_autofree char *pfPhysPortID =3D NULL; =20 /* a VF may have multiple "ports", each one having its own netdev, * and each netdev having a different phys_port_id. Be sure we get * the VF netdev with a phys_port_id matchine that of pfname */ if (virNetDevGetPhysPortID(pfname, &pfPhysPortID) < 0) - goto cleanup; + return -1; =20 virtfnName =3D g_strdup_printf("virtfn%d", vf); =20 @@ -1402,7 +1358,7 @@ virNetDevPFGetVF(const char *pfname, int vf, char **v= fname) * e.g. "/sys/class/net/enp2s0f0/virtfn3" */ if (virNetDevSysfsDeviceFile(&virtfnSysfsPath, pfname, virtfnName) < 0) - goto cleanup; + return -1; =20 /* and this gets the netdev name associated with it, which is a * directory entry in [virtfnSysfsPath]/net, @@ -1411,14 +1367,7 @@ virNetDevPFGetVF(const char *pfname, int vf, char **= vfname) * isn't bound to a netdev driver, it won't have a netdev name, * and vfname will be NULL). */ - ret =3D virPCIGetNetName(virtfnSysfsPath, 0, pfPhysPortID, vfname); - - cleanup: - VIR_FREE(virtfnName); - VIR_FREE(virtfnSysfsPath); - VIR_FREE(pfPhysPortID); - - return ret; + return virPCIGetNetName(virtfnSysfsPath, 0, pfPhysPortID, vfname); } =20 =20 @@ -1559,7 +1508,7 @@ virNetDevSetVfConfig(const char *ifname, int vf, { int rc =3D -1; char macstr[VIR_MAC_STRING_BUFLEN]; - struct nlmsghdr *resp =3D NULL; + g_autofree struct nlmsghdr *resp =3D NULL; struct nlmsgerr *err; unsigned int recvbuflen =3D 0; struct nl_msg *nl_msg; @@ -1671,7 +1620,6 @@ virNetDevSetVfConfig(const char *ifname, int vf, vlanid, rc < 0 ? "Fail" : "Success"); =20 nlmsg_free(nl_msg); - VIR_FREE(resp); return rc; =20 malformed_resp: @@ -1743,20 +1691,14 @@ static int virNetDevGetVfConfig(const char *ifname, int vf, virMacAddrPtr mac, int *vlanid) { - int rc =3D -1; - void *nlData =3D NULL; + g_autofree void *nlData =3D NULL; struct nlattr *tb[IFLA_MAX + 1] =3D {NULL, }; int ifindex =3D -1; =20 - rc =3D virNetlinkDumpLink(ifname, ifindex, &nlData, tb, 0, 0); - if (rc < 0) - goto cleanup; + if (virNetlinkDumpLink(ifname, ifindex, &nlData, tb, 0, 0) < 0) + return -1; =20 - rc =3D virNetDevParseVfConfig(tb, vf, mac, vlanid); - - cleanup: - VIR_FREE(nlData); - return rc; + return virNetDevParseVfConfig(tb, vf, mac, vlanid); } =20 =20 @@ -1813,16 +1755,15 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, const char *stateDir, bool saveVlan) { - int ret =3D -1; const char *pfDevName =3D NULL; - char *pfDevOrig =3D NULL; - char *vfDevOrig =3D NULL; + g_autofree char *pfDevOrig =3D NULL; + g_autofree char *vfDevOrig =3D NULL; virMacAddr oldMAC; char MACStr[VIR_MAC_STRING_BUFLEN]; int oldVlanTag =3D -1; - char *filePath =3D NULL; - char *fileStr =3D NULL; - virJSONValuePtr configJSON =3D NULL; + g_autofree char *filePath =3D NULL; + g_autofree char *fileStr =3D NULL; + g_autoptr(virJSONValue) configJSON =3D NULL; =20 if (vf >=3D 0) { /* linkdev is the PF */ @@ -1830,7 +1771,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; @@ -1842,7 +1783,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, */ =20 if (virNetDevGetVirtualFunctionInfo(linkdev, &pfDevOrig, &vf) < 0) - goto cleanup; + return -1; pfDevName =3D pfDevOrig; } =20 @@ -1861,7 +1802,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, @@ -1870,7 +1811,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, "change host network config to put the " "PF online."), vf, pfDevName); - goto cleanup; + return -1; } } =20 @@ -1886,7 +1827,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, =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, @@ -1894,7 +1835,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, virJSONValueObjectAppendNumberInt(configJSON, VIR_NETDEV_KEYNAME_VLAN_TAG, oldVlanTag) < 0) { - goto cleanup; + return -1; } =20 } else { @@ -1903,33 +1844,26 @@ virNetDevSaveNetConfig(const char *linkdev, int vf, =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: - VIR_FREE(pfDevOrig); - VIR_FREE(vfDevOrig); - VIR_FREE(filePath); - VIR_FREE(fileStr); - virJSONValueFree(configJSON); - return ret; + return 0; } =20 =20 @@ -1963,11 +1897,11 @@ virNetDevReadNetConfig(const char *linkdev, int vf, { int ret =3D -1; const char *pfDevName =3D NULL; - char *pfDevOrig =3D NULL; - char *vfDevOrig =3D NULL; - char *filePath =3D NULL; - char *fileStr =3D NULL; - virJSONValuePtr configJSON =3D NULL; + g_autofree char *pfDevOrig =3D NULL; + g_autofree char *vfDevOrig =3D NULL; + g_autofree char *filePath =3D NULL; + g_autofree char *fileStr =3D NULL; + g_autoptr(virJSONValue) configJSON =3D NULL; const char *MACStr =3D NULL; const char *adminMACStr =3D NULL; int vlanTag =3D -1; @@ -2132,11 +2066,6 @@ virNetDevReadNetConfig(const char *linkdev, int vf, VIR_FREE(*vlan); } =20 - VIR_FREE(pfDevOrig); - VIR_FREE(vfDevOrig); - VIR_FREE(filePath); - VIR_FREE(fileStr); - virJSONValueFree(configJSON); return ret; } =20 @@ -2166,13 +2095,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; - char *pfDevOrig =3D NULL; - char *vfDevOrig =3D NULL; + g_autofree char *pfDevOrig =3D NULL; + g_autofree char *vfDevOrig =3D NULL; int vlanTag =3D -1; - virPCIDevicePtr vfPCIDevice =3D NULL; + g_autoptr(virPCIDevice) vfPCIDevice =3D NULL; =20 if (vf >=3D 0) { /* linkdev is the PF */ @@ -2180,7 +2108,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 @@ -2191,7 +2119,7 @@ virNetDevSetNetConfig(const char *linkdev, int vf, */ =20 if (virNetDevGetVirtualFunctionInfo(linkdev, &pfDevOrig, &vf)) - goto cleanup; + return -1; pfDevName =3D pfDevOrig; } =20 @@ -2204,14 +2132,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 { @@ -2220,14 +2148,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]; @@ -2245,7 +2173,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); @@ -2256,7 +2184,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 @@ -2270,18 +2198,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 @@ -2333,22 +2261,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: - VIR_FREE(pfDevOrig); - VIR_FREE(vfDevOrig); - virPCIDeviceFree(vfPCIDevice); - return ret; + return 0; } =20 =20 @@ -2428,28 +2351,27 @@ int virNetDevGetLinkInfo(const char *ifname, virNetDevIfLinkPtr lnk) { - int ret =3D -1; - char *path =3D NULL; - char *buf =3D NULL; + g_autofree char *path =3D NULL; + g_autofree char *buf =3D NULL; char *tmp; int tmp_state; unsigned int tmp_speed; =20 if (virNetDevSysfsFile(&path, ifname, "operstate") < 0) - goto cleanup; + return -1; =20 if (virFileReadAll(path, 1024, &buf) < 0) { virReportSystemError(errno, _("unable to read: %s"), path); - goto cleanup; + return -1; } =20 if (!(tmp =3D strchr(buf, '\n'))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to parse: %s"), buf); - goto cleanup; + return -1; } =20 *tmp =3D '\0'; @@ -2460,7 +2382,7 @@ virNetDevGetLinkInfo(const char *ifname, virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to parse: %s"), buf); - goto cleanup; + return -1; } =20 lnk->state =3D tmp_state; @@ -2471,26 +2393,23 @@ virNetDevGetLinkInfo(const char *ifname, * speed if that's the case. */ if (lnk->state !=3D VIR_NETDEV_IF_STATE_UP) { lnk->speed =3D 0; - ret =3D 0; - goto cleanup; + return 0; } =20 VIR_FREE(path); VIR_FREE(buf); =20 if (virNetDevSysfsFile(&path, ifname, "speed") < 0) - goto cleanup; + return -1; =20 if (virFileReadAllQuiet(path, 1024, &buf) < 0) { /* Some devices doesn't report speed, in which case we get EINVAL = */ - if (errno =3D=3D EINVAL) { - ret =3D 0; - goto cleanup; - } + if (errno =3D=3D EINVAL) + return 0; virReportSystemError(errno, _("unable to read: %s"), path); - goto cleanup; + return -1; } =20 if (virStrToLong_ui(buf, &tmp, 10, &tmp_speed) < 0 || @@ -2498,16 +2417,12 @@ virNetDevGetLinkInfo(const char *ifname, virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to parse: %s"), buf); - goto cleanup; + return -1; } =20 lnk->speed =3D tmp_speed; =20 - ret =3D 0; - cleanup: - VIR_FREE(buf); - VIR_FREE(path); - return ret; + return 0; } =20 #else @@ -2707,9 +2622,9 @@ static int virNetDevGetMcastList(const char *ifname, virNetDevMcastListPtr mcast) { char *cur =3D NULL; - char *buf =3D NULL; + g_autofree char *buf =3D NULL; char *next =3D NULL; - int ret =3D -1, len; + int len; g_autoptr(virNetDevMcastEntry) entry =3D NULL; =20 mcast->entries =3D NULL; @@ -2717,35 +2632,31 @@ static int virNetDevGetMcastList(const char *ifname, =20 /* Read entire multicast table into memory */ if ((len =3D virFileReadAll(PROC_NET_DEV_MCAST, MAX_MCAST_SIZE, &buf))= <=3D 0) - goto cleanup; + return -1; =20 cur =3D buf; while (cur) { if (!entry && VIR_ALLOC(entry) < 0) - goto cleanup; + return -1; =20 next =3D strchr(cur, '\n'); if (next) next++; if (virNetDevParseMcast(cur, entry)) - goto cleanup; + return -1; =20 /* Only return global multicast MAC addresses for * specified interface */ if (entry->global && STREQ(ifname, entry->name)) { if (VIR_APPEND_ELEMENT(mcast->entries, mcast->nentries, entry)) - goto cleanup; + return -1; } else { memset(entry, 0, sizeof(virNetDevMcastEntry)); } cur =3D next && ((next - buf) < len) ? next : NULL; } =20 - ret =3D 0; - cleanup: - VIR_FREE(buf); - - return ret; + return 0; } =20 =20 @@ -2883,10 +2794,8 @@ static int virNetDevRDMAFeature(const char *ifname, virBitmapPtr *out) { - char *eth_devpath =3D NULL; - char *ib_devpath =3D NULL; - char *eth_res_buf =3D NULL; - char *ib_res_buf =3D NULL; + g_autofree char *eth_devpath =3D NULL; + g_autofree char *eth_res_buf =3D NULL; DIR *dirp =3D NULL; struct dirent *dp; int ret =3D -1; @@ -2910,24 +2819,20 @@ virNetDevRDMAFeature(const char *ifname, goto cleanup; =20 while (virDirRead(dirp, &dp, SYSFS_INFINIBAND_DIR) > 0) { - ib_devpath =3D g_strdup_printf(SYSFS_INFINIBAND_DIR "%s/device/res= ource", - dp->d_name); + g_autofree char *ib_res_buf =3D NULL; + g_autofree char *ib_devpath =3D g_strdup_printf(SYSFS_INFINIBAND_D= IR "%s/device/resource", + dp->d_name); + if (virFileReadAll(ib_devpath, RESOURCE_FILE_LEN, &ib_res_buf) > 0= && STREQ(eth_res_buf, ib_res_buf)) { ignore_value(virBitmapSetBit(*out, VIR_NET_DEV_FEAT_RDMA)); break; } - VIR_FREE(ib_devpath); - VIR_FREE(ib_res_buf); } ret =3D 0; =20 cleanup: VIR_DIR_CLOSE(dirp); - VIR_FREE(eth_devpath); - VIR_FREE(ib_devpath); - VIR_FREE(eth_res_buf); - VIR_FREE(ib_res_buf); return ret; } =20 @@ -3068,7 +2973,7 @@ virNetDevGetFamilyId(const char *family_name, uint32_t *family_id) { struct nl_msg *nl_msg =3D NULL; - struct nlmsghdr *resp =3D NULL; + g_autofree struct nlmsghdr *resp =3D NULL; struct genlmsghdr gmsgh =3D { .cmd =3D CTRL_CMD_GETFAMILY, .version =3D DEVLINK_GENL_VERSION, @@ -3112,7 +3017,6 @@ virNetDevGetFamilyId(const char *family_name, =20 cleanup: nlmsg_free(nl_msg); - VIR_FREE(resp); return ret; } =20 @@ -3132,17 +3036,17 @@ virNetDevSwitchdevFeature(const char *ifname, virBitmapPtr *out) { struct nl_msg *nl_msg =3D NULL; - struct nlmsghdr *resp =3D NULL; + g_autofree struct nlmsghdr *resp =3D NULL; unsigned int recvbuflen; struct nlattr *tb[DEVLINK_ATTR_MAX + 1] =3D {NULL, }; - virPCIDevicePtr pci_device_ptr =3D NULL; + g_autoptr(virPCIDevice) pci_device_ptr =3D NULL; struct genlmsghdr gmsgh =3D { .cmd =3D DEVLINK_CMD_ESWITCH_GET, .version =3D DEVLINK_GENL_VERSION, .reserved =3D 0, }; const char *pci_name; - char *pfname =3D NULL; + g_autofree char *pfname =3D NULL; int is_vf =3D -1; int ret =3D -1; uint32_t family_id; @@ -3161,10 +3065,8 @@ virNetDevSwitchdevFeature(const char *ifname, pci_device_ptr =3D pfname ? virNetDevGetPCIDevice(pfname) : virNetDevGetPCIDevice(ifname); /* No PCI device, then no feature bit to check/add */ - if (pci_device_ptr =3D=3D NULL) { - ret =3D 0; - goto cleanup; - } + if (pci_device_ptr =3D=3D NULL) + return 0; =20 if ((rv =3D virNetDevGetFamilyId(DEVLINK_GENL_NAME, &family_id)) <=3D = 0) return rv; @@ -3205,9 +3107,6 @@ virNetDevSwitchdevFeature(const char *ifname, =20 cleanup: nlmsg_free(nl_msg); - virPCIDeviceFree(pci_device_ptr); - VIR_FREE(resp); - VIR_FREE(pfname); return ret; } # else @@ -3248,7 +3147,7 @@ virNetDevGetEthtoolGFeatures(virBitmapPtr bitmap, int fd, struct ifreq *ifr) { - struct ethtool_gfeatures *g_cmd; + g_autofree struct ethtool_gfeatures *g_cmd =3D NULL; =20 if (VIR_ALLOC_VAR(g_cmd, struct ethtool_get_features_block, GFEATURES_SIZE) <= 0) @@ -3258,7 +3157,6 @@ virNetDevGetEthtoolGFeatures(virBitmapPtr bitmap, g_cmd->size =3D GFEATURES_SIZE; if (virNetDevGFeatureAvailable(fd, ifr, g_cmd)) ignore_value(virBitmapSetBit(bitmap, VIR_NET_DEV_FEAT_TXUDPTNL)); - VIR_FREE(g_cmd); return 0; } # else @@ -3456,8 +3354,7 @@ int virNetDevSetCoalesce(const char *ifname, int virNetDevRunEthernetScript(const char *ifname, const char *script) { - virCommandPtr cmd; - int ret; + g_autoptr(virCommand) cmd =3D NULL; =20 /* Not a bug! Previously we did accept script=3D"" as a NO-OP. */ if (STREQ(script, "")) @@ -3471,8 +3368,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 2.25.3