From patchwork Mon Jan 7 20:41:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 1021586 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="LzAsGSSj"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43YS5L6BVTz9sCr for ; Tue, 8 Jan 2019 07:43:02 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726811AbfAGUnB (ORCPT ); Mon, 7 Jan 2019 15:43:01 -0500 Received: from mail.kernel.org ([198.145.29.99]:54486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726689AbfAGUnB (ORCPT ); Mon, 7 Jan 2019 15:43:01 -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 8D96E2087F; Mon, 7 Jan 2019 20:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1546893780; bh=D/GKTCRfTcNbyvhDR7A6A1plpuTda8SvXuapy/wvCmw=; h=From:To:Cc:Subject:Date:From; b=LzAsGSSjxMZ/GiIEsitvyINg6H665U9iZMxYI56AHY5CDnfhesfWxliVAMiplT9lu lrbRfUqZ0+BL5bXt8R3rjcOO1iSQhm+sC9mDm2e1lbIKamZtkKyDwKQgRyepcw3ewb HDcePKHL0V5+5/K1hewKRol5sQHrnstgHH2mC6xk= From: David Ahern To: stephen@networkplumber.org Cc: netdev@vger.kernel.org, David Ahern Subject: [PATCH iproute2-next] Improve batch times by caching link lookups Date: Mon, 7 Jan 2019 12:41:30 -0800 Message-Id: <20190107204130.32144-1-dsahern@kernel.org> X-Mailer: git-send-email 2.11.0 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 to convert the user given device name to an index. At the moment ll_name_to_index uses if_nametoindex which is ioctl based and does not cache the result. When using a batch file this means the same device lookups can be done repeatedly adding unnecessary overhead (socket + ioctl call 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. With this change the time to install routes via a batch file is reduced from 30.7 seconds to 17.6 seconds (720,022 routes with 2 ecmp nexthops where the nexthop device is given). Signed-off-by: David Ahern --- lib/ll_map.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/ll_map.c b/lib/ll_map.c index 1ab8ef0758ac..4fbd9fa70dd7 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -192,6 +192,46 @@ int ll_index_to_flags(unsigned idx) return im ? im->flags : -1; } +static int ll_link_get(const char *name) +{ + 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, + }; + __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); + 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; +} + unsigned ll_name_to_index(const char *name) { const struct ll_cache *im; @@ -204,7 +244,9 @@ unsigned ll_name_to_index(const char *name) if (im) return im->index; - idx = if_nametoindex(name); + idx = ll_link_get(name); + if (idx == 0) + idx = if_nametoindex(name); if (idx == 0) idx = ll_idx_a2n(name); return idx;