From nobody Fri May 3 11:19:56 2024 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 1523202060859816.4838502257063; Sun, 8 Apr 2018 08:41:00 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 0ADFB4E918; Sun, 8 Apr 2018 15:40:58 +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 DDFEA65B40; Sun, 8 Apr 2018 15:40:56 +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 C6D074CA9C; Sun, 8 Apr 2018 15:40:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w38FepVq010088 for ; Sun, 8 Apr 2018 11:40:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7D27984E4; Sun, 8 Apr 2018 15:40:51 +0000 (UTC) Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 770D384E1 for ; Sun, 8 Apr 2018 15:40:46 +0000 (UTC) Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) (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 D2DC95F795 for ; Sun, 8 Apr 2018 15:40:44 +0000 (UTC) Received: by mail-wr0-f195.google.com with SMTP id l49so6057842wrl.4 for ; Sun, 08 Apr 2018 08:40:44 -0700 (PDT) Received: from kloomba.my.domain ([217.65.211.123]) by smtp.gmail.com with ESMTPSA id l1sm1635042ljj.30.2018.04.08.08.40.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Apr 2018 08:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=wMPOu0YSmF7D+FOQ/ddoivJnjXUgWQyMQj7/OtQVIAw=; b=LJCAWTkCi/CCFIuntO8gGcU4clLJfvdSCXQdepLf3u62jAuHij9AaHbsyp27q5kvY8 JYaTBZzSII0SgH3IUQ3ggtyFwe/RFVgOXZXHXOMPZ6/LUKVAQOvyf0qlv4zaaj1kCZoo 4VZqXiKbR3oXyIe82XZKXOrX7G1r3ZnQcFHGAM+ANnL7xB0eMiFyaD07Gzx+fr5DRyJL oAZ/thyKClCa8/bmK1Xh4kxv+5LXJzFPKpEwLypD0HGd+DbXM6/Kza2ER1DxH9yt9vDw 0WQDuTXTjwMTnySvFkYpFGR0zNdi+Ae7EBMKAr/XkgBN+82qG4NzyC6BGgcpyjvZXMxu WdgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=wMPOu0YSmF7D+FOQ/ddoivJnjXUgWQyMQj7/OtQVIAw=; b=WfccJ9maa8Xr6KXXixE3TG2gJvzVmJkr/KVudcm9P9UZVa40/Hcgy9w4z9keWfgzeO FXe+S/CpccruMWb3hv7hKXnOwF1tvByxM4nstCUrjyL71AejQ9N9ok6wSPZYAJtugYsR wnL+GLDbuN+IpgXteSHLS34ivRy/66Mjjw24aeWGIk1vgYS3W524EmS+IlpCPBkNSBPa M1Ap3v13KSdr4SltmSHo73tp+ET64Vh/ZYyO6obGganH7DcEjLNF4jXhUQulXhDVoVzR 2+fzaAyBzEd+AgJbT8AXpUrQygwFzAZgUgLir2Zw9tOdYDfgQevdQXeViaXOB1gdfEo1 wH3g== X-Gm-Message-State: ALQs6tAz2ms7Cj66k2A3CvmgjMAL0+tC7/t9dEdaKeLw2SIEImM8OU/w 0KlN0zgCup2dB8H9EacgSCeFcA== X-Google-Smtp-Source: AIpwx4+lO2tKkGLMdez8LqZ7yzcritAhgnG1ffpOHgb+cbMrKp6zNYiFKqMMIN5AbV150xHxxd4Zcg== X-Received: by 2002:a19:b588:: with SMTP id g8-v6mr20550947lfk.90.1523202043234; Sun, 08 Apr 2018 08:40:43 -0700 (PDT) From: Roman Bogorodskiy To: libvir-list@redhat.com Date: Sun, 8 Apr 2018 19:40:29 +0400 Message-Id: <20180408154029.90912-1-bogorodskiy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Sun, 08 Apr 2018 15:40:45 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Sun, 08 Apr 2018 15:40:45 +0000 (UTC) for IP:'209.85.128.195' DOMAIN:'mail-wr0-f195.google.com' HELO:'mail-wr0-f195.google.com' FROM:'bogorodskiy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.111 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.128.195 mail-wr0-f195.google.com 209.85.128.195 mail-wr0-f195.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Roman Bogorodskiy Subject: [libvirt] [PATCH] util: improve virNetDevTapGetRealDeviceName 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Sun, 08 Apr 2018 15:40:59 +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" virNetDevTapGetRealDeviceName() is used on FreeBSD because interface names (such as one sees in output of tools like ifconfig(8)) might not match their /dev entity names, and for bhyve we need the latter. Current implementation is not very efficient because in order to find /dev name, it goes through all /dev/tap* entries and tries to issue TAPGIFNAME ioctl on it. Not only this is slow, but also there's a bug in this implementation when more than one NIC is passed to a VM: once we find the tap interface we're looking for, we set its state to UP because opening it for issuing ioctl sets it DOWN, even if it was UP before. When we have more than 1 NIC for a VM, we have only last one UP because others remain DOWN after unsuccessful attempts to match interface name. New implementation just uses sysctl(3), so it should be faster and won't make interfaces go down to get name. --- src/util/virnetdevtap.c | 71 +++++++++++++++++++++------------------------= ---- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index a3ed59da8..afe4f0b3c 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -48,7 +48,8 @@ #ifdef __linux__ # include /* IFF_TUN, IFF_NO_PI */ #elif defined(__FreeBSD__) -# include +# include +# include #endif #if defined(HAVE_GETIFADDRS) && defined(AF_LINK) # include @@ -101,55 +102,43 @@ virNetDevTapGetName(int tapfd ATTRIBUTE_UNUSED, char = **ifname ATTRIBUTE_UNUSED) char* virNetDevTapGetRealDeviceName(char *ifname ATTRIBUTE_UNUSED) { -#ifdef TAPGIFNAME +#ifdef IFDATA_DRIVERNAME + int ifindex =3D 0; + int name[6]; + size_t len =3D 0; char *ret =3D NULL; - struct dirent *dp; - DIR *dirp =3D NULL; - char *devpath =3D NULL; - int fd; =20 - if (virDirOpen(&dirp, "/dev") < 0) + if ((ifindex =3D if_nametoindex(ifname)) =3D=3D 0) { + virReportSystemError(errno, + _("Unable to get interface index for '%s'"), + ifname); return NULL; + } =20 - while (virDirRead(dirp, &dp, "/dev") > 0) { - if (STRPREFIX(dp->d_name, "tap")) { - struct ifreq ifr; - if (virAsprintf(&devpath, "/dev/%s", dp->d_name) < 0) - goto cleanup; - if ((fd =3D open(devpath, O_RDWR)) < 0) { - if (errno =3D=3D EBUSY) { - VIR_FREE(devpath); - continue; - } + name[0] =3D CTL_NET; + name[1] =3D PF_LINK; + name[2] =3D NETLINK_GENERIC; + name[3] =3D IFMIB_IFDATA; + name[4] =3D ifindex; + name[5] =3D IFDATA_DRIVERNAME; =20 - virReportSystemError(errno, _("Unable to open '%s'"), devp= ath); - goto cleanup; - } - - if (ioctl(fd, TAPGIFNAME, (void *)&ifr) < 0) { - virReportSystemError(errno, "%s", - _("Unable to query tap interface name= ")); - goto cleanup; - } + if (sysctl(name, 6, NULL, &len, 0, 0) < 0) { + virReportSystemError(errno, + _("Unable to get driver name for '%s'"), + ifname); + return NULL; + } =20 - if (STREQ(ifname, ifr.ifr_name)) { - /* we can ignore the return value - * because we still have nothing - * to do but return; - */ - ignore_value(VIR_STRDUP(ret, dp->d_name)); - goto cleanup; - } + ret =3D malloc(len); =20 - VIR_FREE(devpath); - VIR_FORCE_CLOSE(fd); - } + if (sysctl(name, 6, ret, &len, 0, 0) < 0) { + virReportSystemError(errno, + _("Unable to get driver name for '%s'"), + ifname); + free(ret); + return NULL; } =20 - cleanup: - VIR_FREE(devpath); - VIR_FORCE_CLOSE(fd); - VIR_DIR_CLOSE(dirp); return ret; #else return NULL; --=20 2.16.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list