From nobody Tue Feb 10 07:22:41 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; envelope-from=libvir-list-bounces@redhat.com; helo=mx4-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) by mx.zohomail.com with SMTPS id 1489178335336988.0856778725835; Fri, 10 Mar 2017 12:38:55 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v2AKZb4p005884; Fri, 10 Mar 2017 15:35:37 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v2AKZMn1018981 for ; Fri, 10 Mar 2017 15:35:22 -0500 Received: from vhost2.laine.org (ovpn-116-174.phx2.redhat.com [10.3.116.174]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2AKZHig012172 for ; Fri, 10 Mar 2017 15:35:21 -0500 From: Laine Stump To: libvir-list@redhat.com Date: Fri, 10 Mar 2017 15:35:02 -0500 Message-Id: <20170310203512.15478-10-laine@laine.org> In-Reply-To: <20170310203512.15478-1-laine@laine.org> References: <20170310203512.15478-1-laine@laine.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 09/19] util: new internal function to permit silent failure of virNetDevSetMAC() 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We will want to allow silent failure of virNetDevSetMAC() in the case that the SIOSIFHWADDR ioctl fails with errno =3D=3D EADDRNOTAVAIL. (Yes, that is very specific, but we really *do* want a logged failure in all other circumstances, and don't want to duplicate code in the caller for the other possibilities). This patch renames the 3 different virNetDevSetMAC() functions to virNetDevSetMACInternal(), adding a 3rd arg called "quiet" and making them static (because this extra control will only be needed within virnetdev.c). A new global virNetDevSetMAC() is defined that calls whichever of the three *Internal() functions gets compiled with quiet =3D false. Callers in virnetdev.c that want to notice a failure with errno =3D=3D EADDRNOTAVAIL and retry with a different strategy rather than immediately failing, can call virNetDevSetMACInternal(..., true). --- src/util/virnetdev.c | 51 +++++++++++++++++++++++++++++++++++++++++-------= --- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 766638d..ffc2fb4 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -222,17 +222,20 @@ int virNetDevExists(const char *ifname) #if defined(SIOCGIFHWADDR) && defined(SIOCSIFHWADDR) && \ defined(HAVE_STRUCT_IFREQ) /** - * virNetDevSetMAC: + * virNetDevSetMACInternal: * @ifname: interface name to set MTU for * @macaddr: MAC address + * @quiet: true if a failure to set MAC address with errno =3D=3D EADDRNOT= AVAIL + * should be silent (still returns error, but without log) * - * This function sets the @macaddr for a given interface @ifname. This - * gets rid of the kernel's automatically assigned random MAC. + * This function sets the @macaddr for a given interface @ifname. * * Returns 0 in case of success or -1 on failure */ -int virNetDevSetMAC(const char *ifname, - const virMacAddr *macaddr) +static int +virNetDevSetMACInternal(const char *ifname, + const virMacAddr *macaddr, + bool quiet) { int fd =3D -1; int ret =3D -1; @@ -254,6 +257,9 @@ int virNetDevSetMAC(const char *ifname, if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) { char macstr[VIR_MAC_STRING_BUFLEN]; =20 + if (quiet && errno =3D=3D EADDRNOTAVAIL) + goto cleanup; + virReportSystemError(errno, _("Cannot set interface MAC to %s on '%s'"), virMacAddrFormat(macaddr, macstr), ifname); @@ -266,10 +272,16 @@ int virNetDevSetMAC(const char *ifname, VIR_FORCE_CLOSE(fd); return ret; } -#elif defined(SIOCSIFLLADDR) && defined(HAVE_STRUCT_IFREQ) && \ + + +#elif defined(SIOCSIFLLADDR) && defined(HAVE_STRUCT_IFREQ) && \ HAVE_DECL_LINK_ADDR -int virNetDevSetMAC(const char *ifname, - const virMacAddr *macaddr) + + +static int +virNetDevSetMACInternal(const char *ifname, + const virMacAddr *macaddr, + bool quiet) { struct ifreq ifr; struct sockaddr_dl sdl; @@ -288,6 +300,9 @@ int virNetDevSetMAC(const char *ifname, ifr.ifr_addr.sa_len =3D VIR_MAC_BUFLEN; =20 if (ioctl(s, SIOCSIFLLADDR, &ifr) < 0) { + if (quiet && errno =3D=3D EADDRNOTAVAIL) + goto cleanup; + virReportSystemError(errno, _("Cannot set interface MAC to %s on '%s'= "), mac + 1, ifname); @@ -300,18 +315,34 @@ int virNetDevSetMAC(const char *ifname, =20 return ret; } + + #else -int virNetDevSetMAC(const char *ifname, - const virMacAddr *macaddr ATTRIBUTE_UNUSED) + + +static int +virNetDevSetMACInternal(const char *ifname, + const virMacAddr *macaddr ATTRIBUTE_UNUSED, + bool quiet ATTRIBUTE_UNUSED) { virReportSystemError(ENOSYS, _("Cannot set interface MAC on '%s'"), ifname); return -1; } + + #endif =20 =20 +int +virNetDevSetMAC(const char *ifname, + const virMacAddr *macaddr) +{ + return virNetDevSetMACInternal(ifname, macaddr, false); +} + + #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ) /** * virNetDevGetMAC: --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list