From patchwork Thu Feb 14 00:22:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 1041738 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="tnbBfHpv"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 440HCx5Dycz9sMx for ; Thu, 14 Feb 2019 11:22:53 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392025AbfBNAWv (ORCPT ); Wed, 13 Feb 2019 19:22:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:44076 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726418AbfBNAWv (ORCPT ); Wed, 13 Feb 2019 19:22:51 -0500 Received: from kenny.it.cumulusnetworks.com. (fw.cumulusnetworks.com [216.129.126.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BAA64222CC; Thu, 14 Feb 2019 00:22:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550103770; bh=FHGlYEIdezpwIPdw6mobZ1DDMGW86n0+bZZ9b9dnkOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tnbBfHpvd9VG01B/yuzaImVsiNLu2qIVN84DpxhN1TLEXX9AVIR2HpMEGmXpnipB8 NPfhPezslOeLSiuqL54m0lT7bMf96sFzfwENyRjIKSccHIYgcvszU18p5jkjlp49lk E7N623rVHjngEBeiGKtpXKL9j7c9KGLbggrC4N3s= From: David Ahern To: stephen@networkplumber.org Cc: netdev@vger.kernel.org, David Ahern Subject: [PATCH iproute2-next v2 1/3] ll_map: Add function to remove link cache entry by index Date: Wed, 13 Feb 2019 16:22:47 -0800 Message-Id: <20190214002249.31866-2-dsahern@kernel.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190214002249.31866-1-dsahern@kernel.org> References: <20190214002249.31866-1-dsahern@kernel.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: David Ahern Add ll_drop_by_index to remove an entry from the link cache. Signed-off-by: David Ahern --- include/ll_map.h | 1 + lib/ll_map.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/ll_map.h b/include/ll_map.h index 511fe00b8567..4de1041e2746 100644 --- a/include/ll_map.h +++ b/include/ll_map.h @@ -9,6 +9,7 @@ unsigned ll_name_to_index(const char *name); const char *ll_index_to_name(unsigned idx); int ll_index_to_type(unsigned idx); int ll_index_to_flags(unsigned idx); +void ll_drop_by_index(unsigned index); unsigned namehash(const char *str); const char *ll_idx_n2a(unsigned int idx); diff --git a/lib/ll_map.c b/lib/ll_map.c index 1ab8ef0758ac..8e8a0b1e9c9d 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -210,6 +210,20 @@ unsigned ll_name_to_index(const char *name) return idx; } +void ll_drop_by_index(unsigned index) +{ + struct ll_cache *im; + + im = ll_get_by_index(index); + if (!im) + return; + + hlist_del(&im->idx_hash); + hlist_del(&im->name_hash); + + free(im); +} + void ll_init_map(struct rtnl_handle *rth) { static int initialized; From patchwork Thu Feb 14 00:22:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 1041741 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="p+T++pfB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 440HD12S9kz9sN1 for ; Thu, 14 Feb 2019 11:22:57 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392090AbfBNAW4 (ORCPT ); Wed, 13 Feb 2019 19:22:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:44080 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730819AbfBNAWv (ORCPT ); Wed, 13 Feb 2019 19:22:51 -0500 Received: from kenny.it.cumulusnetworks.com. (fw.cumulusnetworks.com [216.129.126.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EDED2222D0; Thu, 14 Feb 2019 00:22:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550103771; bh=Zta5Y4ZyMcH9JnhjZDeFrsDQbAopMCaX0eY0T1zyHrA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p+T++pfBDw0eaV9H5sZFL/JrM1eDlPP1DkVPWzt8b/8qXgqhl4XvcspGDkREuxAn8 kTrpIInEEY8coS3nPeQEuUmc9bVC9wRBHUzbayaCi8siK/W3yu+vfTrXULMHP9KO4V zlKhUP2o6Q52J1gEXXRrVo+5T0TMOGeIXVNZeDGA= From: David Ahern To: stephen@networkplumber.org Cc: netdev@vger.kernel.org, David Ahern Subject: [PATCH iproute2-next v2 2/3] ip link: Drop cache entry on any changes Date: Wed, 13 Feb 2019 16:22:48 -0800 Message-Id: <20190214002249.31866-3-dsahern@kernel.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190214002249.31866-1-dsahern@kernel.org> References: <20190214002249.31866-1-dsahern@kernel.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: David Ahern Remove any entry from the link cache when the link is modified. Signed-off-by: David Ahern --- ip/iplink.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ip/iplink.c b/ip/iplink.c index b5519201fef7..393cefdc89df 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -1083,6 +1083,9 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv) if (rtnl_talk(&rth, &req.n, NULL) < 0) return -2; + /* remove device from cache; next use can refresh with new data */ + ll_drop_by_index(req.i.ifi_index); + return 0; } From patchwork Thu Feb 14 00:22:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 1041739 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="jxzr3daD"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 440HCy4NKRz9sN1 for ; Thu, 14 Feb 2019 11:22:54 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726418AbfBNAWx (ORCPT ); Wed, 13 Feb 2019 19:22:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:44084 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731949AbfBNAWw (ORCPT ); Wed, 13 Feb 2019 19:22:52 -0500 Received: from kenny.it.cumulusnetworks.com. (fw.cumulusnetworks.com [216.129.126.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 36076222D2; Thu, 14 Feb 2019 00:22:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550103771; bh=e8aA5JFu4ULXL0RmcKyp+IBL8drWEJyP2yRz40UwyCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jxzr3daDfb9IZqkYVyRi3frReymSgzJdctCXBFicPoXYPyGhCNA8moQn0y3+Cc0Ap XZ9Rwew8DXcr+dMkoANQxnU6l37DDOBYBlb3F/G64fRExyWZPf2J7P+NaufDIK8EUa EeU71d71ndoee8NGrlk6sLCUCQMWaoF/9nypxCS8= From: David Ahern To: stephen@networkplumber.org Cc: netdev@vger.kernel.org, David Ahern Subject: [PATCH iproute2-next v2 3/3] Improve batch and dump times by caching link lookups Date: Wed, 13 Feb 2019 16:22:49 -0800 Message-Id: <20190214002249.31866-4-dsahern@kernel.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190214002249.31866-1-dsahern@kernel.org> References: <20190214002249.31866-1-dsahern@kernel.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: David Ahern ip route uses ll_name_to_index and ll_index_to_name to convert between device names and indices. At the moment both use for the ioctl based glibc functions if_nametoindex and if_indextoname and does not cache the result. When using a batch file or dumping large number of routes this means the same device lookups can be done repeatedly adding unnecessary overhead (socket + ioctl + close for each device lookup). Add a new function, ll_link_get, to send a netlink based RTM_GETLINK. If successful, cache the result in idx_head and name_head so future lookups can re-use the entry. Update ll_name_to_index and ll_index_to_name to use ll_link_get and only fallback to the glibc functions if it fails. With this change the time to install 720,022 routes with 2 ecmp nexthops where the nexthop device is given is reduced from 31.4 seconds to 19.2 seconds. A dump of those routes drops from 13.3 to 2.8 seconds. Signed-off-by: David Ahern --- lib/ll_map.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/ll_map.c b/lib/ll_map.c index 8e8a0b1e9c9d..2d7b65dcb8f7 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -152,6 +152,48 @@ static unsigned int ll_idx_a2n(const char *name) return idx; } +static int ll_link_get(const char *name, int index) +{ + struct { + struct nlmsghdr n; + struct ifinfomsg ifm; + char buf[1024]; + } req = { + .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), + .n.nlmsg_flags = NLM_F_REQUEST, + .n.nlmsg_type = RTM_GETLINK, + .ifm.ifi_index = index, + }; + __u32 filt_mask = RTEXT_FILTER_VF | RTEXT_FILTER_SKIP_STATS; + struct rtnl_handle rth = {}; + struct nlmsghdr *answer; + int rc = 0; + + if (rtnl_open(&rth, 0) < 0) + return 0; + + addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask); + if (name) + addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, + strlen(name) + 1); + + if (rtnl_talk(&rth, &req.n, &answer) < 0) + goto out; + + /* add entry to cache */ + rc = ll_remember_index(answer, NULL); + if (!rc) { + struct ifinfomsg *ifm = NLMSG_DATA(answer); + + rc = ifm->ifi_index; + } + + free(answer); +out: + rtnl_close(&rth); + return rc; +} + const char *ll_index_to_name(unsigned int idx) { static char buf[IFNAMSIZ]; @@ -164,6 +206,12 @@ const char *ll_index_to_name(unsigned int idx) if (im) return im->name; + if (ll_link_get(NULL, idx) == idx) { + im = ll_get_by_index(idx); + if (im) + return im->name; + } + if (if_indextoname(idx, buf) == NULL) snprintf(buf, IFNAMSIZ, "if%u", idx); @@ -204,7 +252,9 @@ unsigned ll_name_to_index(const char *name) if (im) return im->index; - idx = if_nametoindex(name); + idx = ll_link_get(name, 0); + if (idx == 0) + idx = if_nametoindex(name); if (idx == 0) idx = ll_idx_a2n(name); return idx;