From patchwork Wed Sep 29 15:28:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525821 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 703EEC433EF for ; Wed, 29 Sep 2021 15:29:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E7B9614C8 for ; Wed, 29 Sep 2021 15:29:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345306AbhI2Pb2 (ORCPT ); Wed, 29 Sep 2021 11:31:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345296AbhI2Pb0 (ORCPT ); Wed, 29 Sep 2021 11:31:26 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB41BC06161C for ; Wed, 29 Sep 2021 08:29:44 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id l8so10469950edw.2 for ; Wed, 29 Sep 2021 08:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EE38NogxNtaKTmKp2iIN72gRdSeWsh1uBV6CKHtm1Ls=; b=gwJUxHjAuWh4c70ATN/KX1zxDDKu3o8Nn/0ZW084x1s6QYF3ym4gdzJoT0v+jB51hO qRMrz73DORTn9mW677L/FWsvhElJ6riOE02/SUKvtgCty86OaIP5PICIc0LR18uXcJcF o5BlmAVIkGA8y00iKfXPajVd5IveH2AuDhIg3O0ci8I4pXaTLeZ6bSfhRjpMeKn6vb61 KowilHhb5lMfPBWcSWrn10OSb5SBADuDgMPUCW1FkYhK6KitFMwdMFCyb45lKgGB+cEl d1TySniPVnelUmDom7mArR2VTPcWNB6nujUIFSLm1cnaxdY6ncUi5I2Q55/U8zXryhSB NagQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EE38NogxNtaKTmKp2iIN72gRdSeWsh1uBV6CKHtm1Ls=; b=jf4VSIvfP3kWW+M/097PgmWArfW/Hh8uzkRB3KoETqCqPDQREVzOuFAFABR0usG8SM DyaCeh+xSR6rnyJhaOh24BeXtPHUp/WBDfPjxD7Of9Y14TX1VwRk8fdIBn2uLFiPJFHn XzulkZiS9XmAM65YxikVvkeKQ0OZEYJ3jrWG433o4n9SSlACgjyHiaoB01OLjxPlnZPX izVMtaLLRe0/E7mTEzKF2YEnwhG3KE7C+R3MvV754S6/0jBaLCv0zSCeFM6J6PLtrS1d StUQRgghiJZdIx7lEUs374HFe4fKwDT4yL5xruIxThpXtV/VAKOFMA8dC3zz119QSaCj rSaw== X-Gm-Message-State: AOAM531tyXQToe/m2QQmwPeVXD/6Z4j4wkDGKp7wprNCQmUEp+HDPbKa vTfkEtP6DjWwp5heeYjYRTJ4DERxSc56qmCG X-Google-Smtp-Source: ABdhPJyMeBvuACgzrwDBrqDCD4UeFEQKJ2EERxDLfvLmTl2J3Fa98iqqaahB16mGvneBhUIh+d2cQg== X-Received: by 2002:a17:906:6c83:: with SMTP id s3mr352314ejr.13.1632929333948; Wed, 29 Sep 2021 08:28:53 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:53 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 01/11] ip: print_rta_if takes ifindex as device argument instead of attribute Date: Wed, 29 Sep 2021 18:28:38 +0300 Message-Id: <20210929152848.1710552-2-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov We need print_rta_if() to take ifindex directly so later we can use it with cached converted nexthop objects. Signed-off-by: Nikolay Aleksandrov --- ip/ip_common.h | 2 +- ip/ipnexthop.c | 2 +- ip/iproute.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ip/ip_common.h b/ip/ip_common.h index ad018183eac0..d3d50cbca74d 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -168,7 +168,7 @@ int name_is_vrf(const char *name); void print_num(FILE *fp, unsigned int width, uint64_t count); void print_rt_flags(FILE *fp, unsigned int flags); -void print_rta_if(FILE *fp, const struct rtattr *rta, const char *prefix); +void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix); void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta); #endif /* _IP_COMMON_H_ */ diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 9478aa5298eb..a4048d803325 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -381,7 +381,7 @@ int print_nexthop(struct nlmsghdr *n, void *arg) print_rta_gateway(fp, nhm->nh_family, tb[NHA_GATEWAY]); if (tb[NHA_OIF]) - print_rta_if(fp, tb[NHA_OIF], "dev"); + print_rta_ifidx(fp, rta_getattr_u32(tb[NHA_OIF]), "dev"); if (nhm->nh_scope != RT_SCOPE_UNIVERSE || show_details > 0) { print_string(PRINT_ANY, "scope", "scope %s ", diff --git a/ip/iproute.c b/ip/iproute.c index 1e5e2002d2ed..f2bf4737b958 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -410,13 +410,13 @@ static void print_rt_pref(FILE *fp, unsigned int pref) } } -void print_rta_if(FILE *fp, const struct rtattr *rta, const char *prefix) +void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix) { - const char *ifname = ll_index_to_name(rta_getattr_u32(rta)); + const char *ifname = ll_index_to_name(ifidx); - if (is_json_context()) + if (is_json_context()) { print_string(PRINT_JSON, prefix, NULL, ifname); - else { + } else { fprintf(fp, "%s ", prefix); color_fprintf(fp, COLOR_IFNAME, "%s ", ifname); } @@ -862,7 +862,7 @@ int print_route(struct nlmsghdr *n, void *arg) print_rta_via(fp, tb[RTA_VIA]); if (tb[RTA_OIF] && filter.oifmask != -1) - print_rta_if(fp, tb[RTA_OIF], "dev"); + print_rta_ifidx(fp, rta_getattr_u32(tb[RTA_OIF]), "dev"); if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb) print_string(PRINT_ANY, @@ -946,7 +946,7 @@ int print_route(struct nlmsghdr *n, void *arg) print_rta_metrics(fp, tb[RTA_METRICS]); if (tb[RTA_IIF] && filter.iifmask != -1) - print_rta_if(fp, tb[RTA_IIF], "iif"); + print_rta_ifidx(fp, rta_getattr_u32(tb[RTA_IIF]), "iif"); if (tb[RTA_PREF]) print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF])); From patchwork Wed Sep 29 15:28:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525823 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0ADF5C433F5 for ; Wed, 29 Sep 2021 15:29:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DBFB66147F for ; Wed, 29 Sep 2021 15:29:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345309AbhI2Pbd (ORCPT ); Wed, 29 Sep 2021 11:31:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345296AbhI2Pbc (ORCPT ); Wed, 29 Sep 2021 11:31:32 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAAD5C06161C for ; Wed, 29 Sep 2021 08:29:51 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id y35so10243601ede.3 for ; Wed, 29 Sep 2021 08:29:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2i/Mqwrz2agOlxgqYa9F+/vKQmuJbhOqcjJMyfrNYyo=; b=C2IjEoffpGzSN986MX3VjrSwh4V5Sec4sf/GS1MI7lsHMdDQFYBqJUILzG3pbVrtnS Nlb8Pbm+FyVaQ0GBoiFLLzjg9cTjL4oqvoMq4I5/AAhsD63uwl5BU/Brm0wUFNOFkCxM 8/Aimz1NsSaUYxOlS3MYlzdQLWmhv7QYRgImIBWZE57FUxxKzE5oId1kW7fIY4w6ZVH5 zrPYCHuWKTdyfQIQ9YuNxSzIEQ/R0CVn8bMAIQeHB+Ao++L/gVt9ELDlZ0/q4oL6IakO vN79C9y6c9CBWvbC4+wQvmEszDwjnDjPxxnzPEFl1NKUHVjKODdWdzNVUUnEeuPxrjTm YkHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2i/Mqwrz2agOlxgqYa9F+/vKQmuJbhOqcjJMyfrNYyo=; b=oPHYQa8JhyTs6TBLf/NF2FfoBVF6cE60hkCQy7ybwSNbkKVYNjFY7xmO1s8cy0BDrV QwGskpdPapPSzn3c7PxgySZEqZdDIi3d6Qqk9TSrclhWUmwcnbXuYLSauYDbyF02x4uz wLHyUCLWFJq6C8eY/af4bcWgeVpQ1yb12Hv4R1hlKuMGJx/1gDHppxIz0TPkJz1/nKW+ Qg1Dwbfzhx44QHHHg+9sx9J7Y/jupi5NJkeCmkYDMl/GiuLfEzJG6EiH/wmX/vnLDJP7 pTJT/hnM3BIAqDqImypZ2t/rsRt4EJhNWhaxHCFIfpxZcwjQO2VDbWh+yTu1rlWKRMVt EE0A== X-Gm-Message-State: AOAM532+5x0qs0FFLdUBY/ugYIb9Pp0ExbHgrgWZ/DYt/yMkb1VgPtQB jXqqOQYtRvO5DEnEpGUxzdxGZJam45I/TAf7 X-Google-Smtp-Source: ABdhPJzdNekr0Zr/Ia4zabuqF/7qzdpcinQ481D36HK6nBIHxSFhbSBLy+lvZrrnEVE6Mc7K7C6vmw== X-Received: by 2002:a17:907:20c8:: with SMTP id qq8mr309526ejb.339.1632929334954; Wed, 29 Sep 2021 08:28:54 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:54 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 02/11] ip: export print_rta_gateway version which outputs prepared gateway string Date: Wed, 29 Sep 2021 18:28:39 +0300 Message-Id: <20210929152848.1710552-3-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Export a new __print_rta_gateway that takes a prepared gateway string to print which is also used by print_rta_gateway for consistent format. Signed-off-by: Nikolay Aleksandrov --- ip/ip_common.h | 1 + ip/iproute.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ip/ip_common.h b/ip/ip_common.h index d3d50cbca74d..a02a3b96f7fd 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -169,6 +169,7 @@ int name_is_vrf(const char *name); void print_num(FILE *fp, unsigned int width, uint64_t count); void print_rt_flags(FILE *fp, unsigned int flags); void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix); +void __print_rta_gateway(FILE *fp, unsigned char family, const char *gateway); void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta); #endif /* _IP_COMMON_H_ */ diff --git a/ip/iproute.c b/ip/iproute.c index f2bf4737b958..3c933df4dd29 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -547,13 +547,11 @@ static void print_rta_newdst(FILE *fp, const struct rtmsg *r, } } -void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta) +void __print_rta_gateway(FILE *fp, unsigned char family, const char *gateway) { - const char *gateway = format_host_rta(family, rta); - - if (is_json_context()) + if (is_json_context()) { print_string(PRINT_JSON, "gateway", NULL, gateway); - else { + } else { fprintf(fp, "via "); print_color_string(PRINT_FP, ifa_family_color(family), @@ -561,6 +559,13 @@ void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta) } } +void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta) +{ + const char *gateway = format_host_rta(family, rta); + + __print_rta_gateway(fp, family, gateway); +} + static void print_rta_via(FILE *fp, const struct rtattr *rta) { size_t len = RTA_PAYLOAD(rta) - 2; From patchwork Wed Sep 29 15:28:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525817 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EACDEC433F5 for ; Wed, 29 Sep 2021 15:29:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB0DE614C8 for ; Wed, 29 Sep 2021 15:29:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345295AbhI2PbZ (ORCPT ); Wed, 29 Sep 2021 11:31:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345157AbhI2PbY (ORCPT ); Wed, 29 Sep 2021 11:31:24 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33511C06161C for ; Wed, 29 Sep 2021 08:29:43 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id dj4so10523565edb.5 for ; Wed, 29 Sep 2021 08:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OZ3GkQ+8Nx5Bb569cVbmprs3JRZYrk6EPVO1lIAUa98=; b=zcHtwLnJ94rdBbd3lpnJNP6sznDnzfsmm1+J1Ep0DT0nioUU++4Varn7VnlGILHzE6 QzmCgZGqJrjm/eDYxaQceVQi1LmPCu7bqrihciTKRkYDJrHK4NDOxTJ6FXHh6UBWYnbp udtt6WDNCmuVYq4Eo9KVUNEv/g6eK1f/cB0Zgu2wkrRF9cwqtCTIrcFQB2v5GQdYaMfv +6EP0sv6F6KMapfbzRek1tKIE6RHDltC1SgLIOt/6/y8FWrdOGOW+NRBGEPIr096xk1V QAQWjEcMltUX4RtBqO/Agthzl22UUzwdRLXY3YwBztn9rwPoFDaUbz1WDCHPOZiw5bOL HaTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OZ3GkQ+8Nx5Bb569cVbmprs3JRZYrk6EPVO1lIAUa98=; b=OnXoT1SdPLZohQVQAiLDJkDpD/faJy7qXCiUxPERRlcx3tqVrIpRBGKLHloNnG9ouY Qr13n79Won2Jj3KUWu6kHPSt/eH4GxLhFR7z0W58kCgyC1Bjyfag+5SCRwbJ2MAgYyd8 pu9JDB9YKCzhkAjFwqXu8JqlDRsvOqxXkuhMfYFovFetAK0o9Mgf8Hxd1REbyaOx+YtY SZ+nSRsEK4it4Sv8YLgiBRq3JOgg7jsTEqPyLwfCcz6e5UE8pc9Vxs8cJD1nF6fLrESt 1bTQ2UeI2/eDNj3/WJxXRV8ONCPKDBqS6vzzniufhANBNg8M05UoaRZjV26CBxwtHSok Qx3g== X-Gm-Message-State: AOAM533YGlh1nAEQ+75focWEfXEnXxkugX2JbsI8hGHxACbZTqQWikTf +S4XruTfZpEuX34BZZ4ueuNZJLhjFGmO0LHe X-Google-Smtp-Source: ABdhPJyncIBz9kHeE3VAQYQnsh3OBqFe5vDbEsvpgCjMtRantIP4697QVa313Z94DfkjhBmkmYzvqA== X-Received: by 2002:a17:906:b052:: with SMTP id bj18mr343248ejb.55.1632929335981; Wed, 29 Sep 2021 08:28:55 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:55 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 03/11] ip: nexthop: add nh struct and a helper to parse nhmsg into it Date: Wed, 29 Sep 2021 18:28:40 +0300 Message-Id: <20210929152848.1710552-4-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Add a structure which describes a nexthop with all of its properties and a helper which parses a nhmsg into it. Note the LWT attribute is copied because there are too many different types with their own structures, having to follow them all for changes would be overkill. It's much better to copy the attribtue and pass it for decoding to the lwt code which is already well maintained. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++- ip/nh_common.h | 33 +++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 ip/nh_common.h diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index a4048d803325..be8541476fa6 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -13,6 +13,7 @@ #include "utils.h" #include "ip_common.h" +#include "nh_common.h" static struct { unsigned int flushed; @@ -212,13 +213,20 @@ out: return rc; } +static bool __valid_nh_group_attr(const struct rtattr *g_attr) +{ + int num = RTA_PAYLOAD(g_attr) / sizeof(struct nexthop_grp); + + return num && num * sizeof(struct nexthop_grp) == RTA_PAYLOAD(g_attr); +} + static void print_nh_group(FILE *fp, const struct rtattr *grps_attr) { struct nexthop_grp *nhg = RTA_DATA(grps_attr); int num = RTA_PAYLOAD(grps_attr) / sizeof(*nhg); int i; - if (!num || num * sizeof(*nhg) != RTA_PAYLOAD(grps_attr)) { + if (!__valid_nh_group_attr(grps_attr)) { fprintf(fp, ""); return; } @@ -328,6 +336,93 @@ static void print_nh_res_bucket(FILE *fp, const struct rtattr *res_bucket_attr) close_json_object(); } +static void ipnh_destroy_entry(struct nh_entry *nhe) +{ + if (nhe->nh_encap) + free(nhe->nh_encap); + if (nhe->nh_groups) + free(nhe->nh_groups); +} + +/* parse nhmsg into nexthop entry struct which must be destroyed by + * ipnh_destroy_enty when it's not needed anymore + */ +static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len, + struct nh_entry *nhe) +{ + struct rtattr *tb[NHA_MAX+1]; + int err = 0; + + memset(nhe, 0, sizeof(*nhe)); + parse_rtattr_flags(tb, NHA_MAX, RTM_NHA(nhm), len, NLA_F_NESTED); + + if (tb[NHA_ID]) + nhe->nh_id = rta_getattr_u32(tb[NHA_ID]); + + if (tb[NHA_OIF]) + nhe->nh_oif = rta_getattr_u32(tb[NHA_OIF]); + + if (tb[NHA_GROUP_TYPE]) + nhe->nh_grp_type = rta_getattr_u16(tb[NHA_GROUP_TYPE]); + + if (tb[NHA_GATEWAY]) { + if (RTA_PAYLOAD(tb[NHA_GATEWAY]) > sizeof(nhe->nh_gateway)) { + fprintf(fp, "\n", + nhe->nh_id, RTA_PAYLOAD(tb[NHA_GATEWAY])); + err = EINVAL; + goto out_err; + } + nhe->nh_gateway_len = RTA_PAYLOAD(tb[NHA_GATEWAY]); + memcpy(&nhe->nh_gateway, RTA_DATA(tb[NHA_GATEWAY]), + RTA_PAYLOAD(tb[NHA_GATEWAY])); + } + + if (tb[NHA_ENCAP]) { + nhe->nh_encap = malloc(RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP]))); + if (!nhe->nh_encap) { + err = ENOMEM; + goto out_err; + } + memcpy(nhe->nh_encap, tb[NHA_ENCAP], + RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP]))); + memcpy(&nhe->nh_encap_type, tb[NHA_ENCAP_TYPE], + sizeof(nhe->nh_encap_type)); + } + + if (tb[NHA_GROUP]) { + if (!__valid_nh_group_attr(tb[NHA_GROUP])) { + fprintf(fp, "", + nhe->nh_id); + err = EINVAL; + goto out_err; + } + + nhe->nh_groups = malloc(RTA_PAYLOAD(tb[NHA_GROUP])); + if (!nhe->nh_groups) { + err = ENOMEM; + goto out_err; + } + nhe->nh_groups_cnt = RTA_PAYLOAD(tb[NHA_GROUP]) / + sizeof(struct nexthop_grp); + memcpy(nhe->nh_groups, RTA_DATA(tb[NHA_GROUP]), + RTA_PAYLOAD(tb[NHA_GROUP])); + } + + nhe->nh_blackhole = !!tb[NHA_BLACKHOLE]; + nhe->nh_fdb = !!tb[NHA_FDB]; + + nhe->nh_family = nhm->nh_family; + nhe->nh_protocol = nhm->nh_protocol; + nhe->nh_scope = nhm->nh_scope; + nhe->nh_flags = nhm->nh_flags; + + return 0; + +out_err: + ipnh_destroy_entry(nhe); + return err; +} + int print_nexthop(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); diff --git a/ip/nh_common.h b/ip/nh_common.h new file mode 100644 index 000000000000..f2ff0e6532d3 --- /dev/null +++ b/ip/nh_common.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NH_COMMON_H__ +#define __NH_COMMON_H__ 1 + +struct nh_entry { + __u32 nh_id; + __u32 nh_oif; + __u32 nh_flags; + __u16 nh_grp_type; + __u8 nh_family; + __u8 nh_scope; + __u8 nh_protocol; + + bool nh_blackhole; + bool nh_fdb; + + int nh_gateway_len; + union { + __be32 ipv4; + struct in6_addr ipv6; + } nh_gateway; + + struct rtattr *nh_encap; + union { + struct rtattr rta; + __u8 _buf[RTA_LENGTH(sizeof(__u16))]; + } nh_encap_type; + + int nh_groups_cnt; + struct nexthop_grp *nh_groups; +}; + +#endif /* __NH_COMMON_H__ */ From patchwork Wed Sep 29 15:28:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525819 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EBC1C433FE for ; Wed, 29 Sep 2021 15:29:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB3F4614C8 for ; Wed, 29 Sep 2021 15:29:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345300AbhI2Pb0 (ORCPT ); Wed, 29 Sep 2021 11:31:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345289AbhI2PbZ (ORCPT ); Wed, 29 Sep 2021 11:31:25 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D401CC061760 for ; Wed, 29 Sep 2021 08:29:43 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id dj4so10523798edb.5 for ; Wed, 29 Sep 2021 08:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ykMBYZicswPirsTO842dgZBWABg6Nz1Qsrzpfqs2cKw=; b=2Q73nUTPU6KOKBCQqzKKgwOguIxp+4l/otHkA87IhTxBrpvy/WqF3SShTE+htx5CIH pHZa2tobq8U7pTyvlWwWEN15AWLqVSs4AxhtcZsgUtCbyUR9imn6CclPj0hq5njNPky2 TeU0/pV6ZpNs1SpG1YEgWZZx2t3FkH01sT37Yxi/+wahogdcjpW8SvzEcRpzeZH+R8k0 7k0ffWclsicGbeD7CgUfeAfl8rDZK6jsYnx4bR/qq8JUxhoFYmYY94Z2iwIQ8Wz27+pd DGgqm06KQOJSzeFkH3sUtsUtmvDrLLROIRnXUoOq7c5to0kh3LNNGIyzIgdNJrqOOTAx wouw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ykMBYZicswPirsTO842dgZBWABg6Nz1Qsrzpfqs2cKw=; b=lVhvFb4mWLRAlX6cjtKvJF7DYt1zAFBaFtMV/HExq2W5/hAPtqvzdZjG0rbngIsF7a XUjQNm2pEZZPFd39jZzqAKrx2qN5/uWTqeLs3NDvK2rzsuO6aMwOgSi179cT+710I3Gj ppZjELqmvXHJoPTxQrEYj45WNlpOzra9aLyiFuh0Gi6q6wzQ6gFvYUbw3G9J9pbxHHOZ sVfoKZyGNllg6/SxTCc/N0AEVUG++3Bn+w6ddhKkl/AtmOkspHtkrwRVlQLC8siqlhVl v+bfHTCKOFu7M9tV9Yv4EAHcE0fQqkEcYO2qJK4CFnvk5Q23yh8s1VdwvlmExux96RWa TouA== X-Gm-Message-State: AOAM531BFChzhk7oiv6rU2leJRCck2jJs1RWq8IhGq7aPK95TIM0h1En 5BN8PG+o8KqL+rPubvwOoKpPIo93TRbVKvdV X-Google-Smtp-Source: ABdhPJxjXUJbOZ4P2nySPKI32fl5daSh8SklM8YHY9pFCG4XoaQiaKlaC6xqIhjYGBYNQLScQoydDg== X-Received: by 2002:a17:906:1341:: with SMTP id x1mr318817ejb.277.1632929336880; Wed, 29 Sep 2021 08:28:56 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:56 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 04/11] ip: nexthop: parse resilient nexthop group attribute into structure Date: Wed, 29 Sep 2021 18:28:41 +0300 Message-Id: <20210929152848.1710552-5-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Add a structure which describes resilient nexthop groups and parse such attributes into it. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 32 ++++++++++++++++++++++++++++++++ ip/nh_common.h | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index be8541476fa6..9340d8941277 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -272,6 +272,33 @@ static void print_nh_group_type(FILE *fp, const struct rtattr *grp_type_attr) print_string(PRINT_ANY, "type", "type %s ", nh_group_type_name(type)); } +static void parse_nh_res_group_rta(const struct rtattr *res_grp_attr, + struct nha_res_grp *res_grp) +{ + struct rtattr *tb[NHA_RES_GROUP_MAX + 1]; + struct rtattr *rta; + + parse_rtattr_nested(tb, NHA_RES_GROUP_MAX, res_grp_attr); + + if (tb[NHA_RES_GROUP_BUCKETS]) + res_grp->buckets = rta_getattr_u16(tb[NHA_RES_GROUP_BUCKETS]); + + if (tb[NHA_RES_GROUP_IDLE_TIMER]) { + rta = tb[NHA_RES_GROUP_IDLE_TIMER]; + res_grp->idle_timer = rta_getattr_u32(rta); + } + + if (tb[NHA_RES_GROUP_UNBALANCED_TIMER]) { + rta = tb[NHA_RES_GROUP_UNBALANCED_TIMER]; + res_grp->unbalanced_timer = rta_getattr_u32(rta); + } + + if (tb[NHA_RES_GROUP_UNBALANCED_TIME]) { + rta = tb[NHA_RES_GROUP_UNBALANCED_TIME]; + res_grp->unbalanced_time = rta_getattr_u64(rta); + } +} + static void print_nh_res_group(FILE *fp, const struct rtattr *res_grp_attr) { struct rtattr *tb[NHA_RES_GROUP_MAX + 1]; @@ -408,6 +435,11 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len, RTA_PAYLOAD(tb[NHA_GROUP])); } + if (tb[NHA_RES_GROUP]) { + parse_nh_res_group_rta(tb[NHA_RES_GROUP], &nhe->nh_res_grp); + nhe->nh_has_res_grp = true; + } + nhe->nh_blackhole = !!tb[NHA_BLACKHOLE]; nhe->nh_fdb = !!tb[NHA_FDB]; diff --git a/ip/nh_common.h b/ip/nh_common.h index f2ff0e6532d3..8c96f9993562 100644 --- a/ip/nh_common.h +++ b/ip/nh_common.h @@ -2,6 +2,13 @@ #ifndef __NH_COMMON_H__ #define __NH_COMMON_H__ 1 +struct nha_res_grp { + __u16 buckets; + __u32 idle_timer; + __u32 unbalanced_timer; + __u64 unbalanced_time; +}; + struct nh_entry { __u32 nh_id; __u32 nh_oif; @@ -26,6 +33,9 @@ struct nh_entry { __u8 _buf[RTA_LENGTH(sizeof(__u16))]; } nh_encap_type; + bool nh_has_res_grp; + struct nha_res_grp nh_res_grp; + int nh_groups_cnt; struct nexthop_grp *nh_groups; }; From patchwork Wed Sep 29 15:28:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525827 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87304C433EF for ; Wed, 29 Sep 2021 15:30:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6586D61462 for ; Wed, 29 Sep 2021 15:30:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345317AbhI2Pbl (ORCPT ); Wed, 29 Sep 2021 11:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345318AbhI2Pbi (ORCPT ); Wed, 29 Sep 2021 11:31:38 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFCE8C061765 for ; Wed, 29 Sep 2021 08:29:56 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id x7so9944435edd.6 for ; Wed, 29 Sep 2021 08:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RNiD2ML6uyrZMgJvYNcGyQqKMQ9MGZdfaIwLCMa/gzQ=; b=cE2a3fzrvqb4gx+6K7mI3I8njCp0PjtOgYsQoC/o3QRWafMQSCqGc0mOYfTB9eAtW0 nWIqk80WkXoSDeU+BIbXZbhBxPYIY+NME5lmva4NvhzklRiDkpwbKuH3+0tQi7J+Kofv mFmdZ5xmHNa+QIRs9sSMF8BPV758cJep556XFH8Y3H7nLdsRSUWM+/fZTZWesJ8m9X7N X6F2rh39tenewUW1+LVMB4C0dkKWoFVuR6WAUHyRkOKlYhM4reqe9NsV4xNKAExX5lgh AEnvTDOgUvS9hAtpUrvX1HJL06FNAiJEZL1DHKkqj+zbiuu1O085rSO6ai5RT86TN37l ciug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RNiD2ML6uyrZMgJvYNcGyQqKMQ9MGZdfaIwLCMa/gzQ=; b=TZa9bBUSszKI6uwLdHYCX9KVZINbJZTTwUqssuMixym7jxevgWf7TThKmdUok7zdra VsKt9SFAqlkPELuPbP3fYJ8+XKNbTlpcMyeIVz0APpPTcY26l4lmGYUEjL+AeIf1rkBM fYahob1uCyCc4yd/1p2zqFTYeZk3ps0HDsgw0GS6JkDx1Nlsi5et97EwZcBF8fRvlRJI myCzjR54JuDi/JjhTVb4MOpKCMLy6awSiYsVyS49T7Afrn+tMNm9xE07b645TQx9xP9D N7ZgBvL/XS4CJmuULO1yOeRJTagJzgFUm8XFPXLIQINWVSHrqMZGUvIrl2NSYNda5RGp gC3w== X-Gm-Message-State: AOAM533EJBPaQ9W0cFy9wKitzgmaoHD7uduJWJ7XdQ8OtxPe8g2ZfTDr cuWxsOElGHUQdZ3uCydqlsRTpApyu/GiZo+0 X-Google-Smtp-Source: ABdhPJynVHlNTaPT5wru6r5TuAI9uEEGfIxM4j/jmNHXIV9hhtDnSMzsjgZIg0SI639YYeA+MMcoTg== X-Received: by 2002:a17:907:374:: with SMTP id rs20mr372413ejb.460.1632929337859; Wed, 29 Sep 2021 08:28:57 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:57 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 05/11] ip: nexthop: always parse attributes for printing Date: Wed, 29 Sep 2021 18:28:42 +0300 Message-Id: <20210929152848.1710552-6-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Always parse nexthop group attributes into structure for printing. Nexthop print helpers take structures and print them accordingly. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 117 +++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 68 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 9340d8941277..e334b852aa55 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -220,28 +220,22 @@ static bool __valid_nh_group_attr(const struct rtattr *g_attr) return num && num * sizeof(struct nexthop_grp) == RTA_PAYLOAD(g_attr); } -static void print_nh_group(FILE *fp, const struct rtattr *grps_attr) +static void print_nh_group(const struct nh_entry *nhe) { - struct nexthop_grp *nhg = RTA_DATA(grps_attr); - int num = RTA_PAYLOAD(grps_attr) / sizeof(*nhg); int i; - if (!__valid_nh_group_attr(grps_attr)) { - fprintf(fp, ""); - return; - } - open_json_array(PRINT_JSON, "group"); print_string(PRINT_FP, NULL, "%s", "group "); - for (i = 0; i < num; ++i) { + for (i = 0; i < nhe->nh_groups_cnt; ++i) { open_json_object(NULL); if (i) print_string(PRINT_FP, NULL, "%s", "/"); - print_uint(PRINT_ANY, "id", "%u", nhg[i].id); - if (nhg[i].weight) - print_uint(PRINT_ANY, "weight", ",%u", nhg[i].weight + 1); + print_uint(PRINT_ANY, "id", "%u", nhe->nh_groups[i].id); + if (nhe->nh_groups[i].weight) + print_uint(PRINT_ANY, "weight", ",%u", + nhe->nh_groups[i].weight + 1); close_json_object(); } @@ -261,15 +255,14 @@ static const char *nh_group_type_name(__u16 type) } } -static void print_nh_group_type(FILE *fp, const struct rtattr *grp_type_attr) +static void print_nh_group_type(__u16 nh_grp_type) { - __u16 type = rta_getattr_u16(grp_type_attr); - - if (type == NEXTHOP_GRP_TYPE_MPATH) + if (nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) /* Do not print type in order not to break existing output. */ return; - print_string(PRINT_ANY, "type", "type %s ", nh_group_type_name(type)); + print_string(PRINT_ANY, "type", "type %s ", + nh_group_type_name(nh_grp_type)); } static void parse_nh_res_group_rta(const struct rtattr *res_grp_attr, @@ -299,39 +292,22 @@ static void parse_nh_res_group_rta(const struct rtattr *res_grp_attr, } } -static void print_nh_res_group(FILE *fp, const struct rtattr *res_grp_attr) +static void print_nh_res_group(const struct nha_res_grp *res_grp) { - struct rtattr *tb[NHA_RES_GROUP_MAX + 1]; - struct rtattr *rta; struct timeval tv; - parse_rtattr_nested(tb, NHA_RES_GROUP_MAX, res_grp_attr); - open_json_object("resilient_args"); - if (tb[NHA_RES_GROUP_BUCKETS]) - print_uint(PRINT_ANY, "buckets", "buckets %u ", - rta_getattr_u16(tb[NHA_RES_GROUP_BUCKETS])); + print_uint(PRINT_ANY, "buckets", "buckets %u ", res_grp->buckets); - if (tb[NHA_RES_GROUP_IDLE_TIMER]) { - rta = tb[NHA_RES_GROUP_IDLE_TIMER]; - __jiffies_to_tv(&tv, rta_getattr_u32(rta)); - print_tv(PRINT_ANY, "idle_timer", "idle_timer %g ", &tv); - } + __jiffies_to_tv(&tv, res_grp->idle_timer); + print_tv(PRINT_ANY, "idle_timer", "idle_timer %g ", &tv); - if (tb[NHA_RES_GROUP_UNBALANCED_TIMER]) { - rta = tb[NHA_RES_GROUP_UNBALANCED_TIMER]; - __jiffies_to_tv(&tv, rta_getattr_u32(rta)); - print_tv(PRINT_ANY, "unbalanced_timer", "unbalanced_timer %g ", - &tv); - } + __jiffies_to_tv(&tv, res_grp->unbalanced_timer); + print_tv(PRINT_ANY, "unbalanced_timer", "unbalanced_timer %g ", &tv); - if (tb[NHA_RES_GROUP_UNBALANCED_TIME]) { - rta = tb[NHA_RES_GROUP_UNBALANCED_TIME]; - __jiffies_to_tv(&tv, rta_getattr_u32(rta)); - print_tv(PRINT_ANY, "unbalanced_time", "unbalanced_time %g ", - &tv); - } + __jiffies_to_tv(&tv, res_grp->unbalanced_time); + print_tv(PRINT_ANY, "unbalanced_time", "unbalanced_time %g ", &tv); close_json_object(); } @@ -458,9 +434,9 @@ out_err: int print_nexthop(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); - struct rtattr *tb[NHA_MAX+1]; FILE *fp = (FILE *)arg; - int len; + struct nh_entry nhe; + int len, err; SPRINT_BUF(b1); @@ -481,56 +457,61 @@ int print_nexthop(struct nlmsghdr *n, void *arg) if (filter.proto && filter.proto != nhm->nh_protocol) return 0; - parse_rtattr_flags(tb, NHA_MAX, RTM_NHA(nhm), len, NLA_F_NESTED); - + err = parse_nexthop_rta(fp, nhm, len, &nhe); + if (err) { + close_json_object(); + fprintf(stderr, "Error parsing nexthop: %s\n", strerror(err)); + return -1; + } open_json_object(NULL); if (n->nlmsg_type == RTM_DELNEXTHOP) print_bool(PRINT_ANY, "deleted", "Deleted ", true); - if (tb[NHA_ID]) - print_uint(PRINT_ANY, "id", "id %u ", - rta_getattr_u32(tb[NHA_ID])); + print_uint(PRINT_ANY, "id", "id %u ", nhe.nh_id); - if (tb[NHA_GROUP]) - print_nh_group(fp, tb[NHA_GROUP]); + if (nhe.nh_groups) + print_nh_group(&nhe); - if (tb[NHA_GROUP_TYPE]) - print_nh_group_type(fp, tb[NHA_GROUP_TYPE]); + print_nh_group_type(nhe.nh_grp_type); - if (tb[NHA_RES_GROUP]) - print_nh_res_group(fp, tb[NHA_RES_GROUP]); + if (nhe.nh_has_res_grp) + print_nh_res_group(&nhe.nh_res_grp); - if (tb[NHA_ENCAP]) - lwt_print_encap(fp, tb[NHA_ENCAP_TYPE], tb[NHA_ENCAP]); + if (nhe.nh_encap) + lwt_print_encap(fp, &nhe.nh_encap_type.rta, nhe.nh_encap); - if (tb[NHA_GATEWAY]) - print_rta_gateway(fp, nhm->nh_family, tb[NHA_GATEWAY]); + if (nhe.nh_gateway_len) + __print_rta_gateway(fp, nhe.nh_family, + format_host(nhe.nh_family, + nhe.nh_gateway_len, + &nhe.nh_gateway)); - if (tb[NHA_OIF]) - print_rta_ifidx(fp, rta_getattr_u32(tb[NHA_OIF]), "dev"); + if (nhe.nh_oif) + print_rta_ifidx(fp, nhe.nh_oif, "dev"); - if (nhm->nh_scope != RT_SCOPE_UNIVERSE || show_details > 0) { + if (nhe.nh_scope != RT_SCOPE_UNIVERSE || show_details > 0) { print_string(PRINT_ANY, "scope", "scope %s ", - rtnl_rtscope_n2a(nhm->nh_scope, b1, sizeof(b1))); + rtnl_rtscope_n2a(nhe.nh_scope, b1, sizeof(b1))); } - if (tb[NHA_BLACKHOLE]) + if (nhe.nh_blackhole) print_null(PRINT_ANY, "blackhole", "blackhole ", NULL); - if (nhm->nh_protocol != RTPROT_UNSPEC || show_details > 0) { + if (nhe.nh_protocol != RTPROT_UNSPEC || show_details > 0) { print_string(PRINT_ANY, "protocol", "proto %s ", - rtnl_rtprot_n2a(nhm->nh_protocol, b1, sizeof(b1))); + rtnl_rtprot_n2a(nhe.nh_protocol, b1, sizeof(b1))); } - print_rt_flags(fp, nhm->nh_flags); + print_rt_flags(fp, nhe.nh_flags); - if (tb[NHA_FDB]) + if (nhe.nh_fdb) print_null(PRINT_ANY, "fdb", "fdb", NULL); print_string(PRINT_FP, NULL, "%s", "\n"); close_json_object(); fflush(fp); + destroy_nexthop_entry(&nhe); return 0; } From patchwork Wed Sep 29 15:28:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525829 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A42AFC433F5 for ; Wed, 29 Sep 2021 15:30:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 89B9261462 for ; Wed, 29 Sep 2021 15:30:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345327AbhI2Pbo (ORCPT ); Wed, 29 Sep 2021 11:31:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345324AbhI2Pbj (ORCPT ); Wed, 29 Sep 2021 11:31:39 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DC0BC061768 for ; Wed, 29 Sep 2021 08:29:58 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id x7so9944687edd.6 for ; Wed, 29 Sep 2021 08:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e/qdt6GablJU0aPRCNH1n0DMgmQ27KxA9ufJVcdULMM=; b=a+/6vlJnjJqHl24+ccRSgF169VZmQ5c7oynx47AOxZXCpK65qpZ5+OodvGwpgj2TVW 3jRQPoRcz6OTW3td7vmnvPDK91lh/pq2wC3HgM28EvIJRRAQprUw62x7YQr7voSBfB1p x16VqJdIRPZLMTRPRmprIU3w4xspToFFMMBYmtemlw+xA4SGMbvMnKSPKV0opuPnVR7N Lg7JzosFm5as9JigVO22PN/uJjJG0xW19RO+yb6x60OfsDpeM1G/zK6r0F29ftYQsoRh WMay1X62qYQHnfkKYBVq4EKUwHhIP9yWhCbGkrSlGs2qD7PPyu4TksDZyLGv2GjcU3tG aO4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e/qdt6GablJU0aPRCNH1n0DMgmQ27KxA9ufJVcdULMM=; b=ZNj3yyUYrDsi99k+/Sirl9iQJxuIkAuURNUoDotHGH5uh6smi2d+aRHZdDFrVb3NcX h0U/cZRQftd/sxHq77xZ7feTTLm3Bc4B58EhRPJ0n4EU58qFq+X7xnEbfzS3pOo0Raqq zjKsGrt2/EELRWcPfPVDegIV8JlNNFziDfs7KriyqbCa+nohIm/idjgipEsxx0TbYj8D 5ENIyN2ZEzYT2iZGHzVp7I/7PfmY088x+rnA/imHgVuBL1NZlRdOqTgtyo8WVZ9n4Z+6 mVezhXtZcNSpfIYid6gJPiCK48Tw2ipGH5HlI5BL41r1f1l6MRCqaUGfmWzc0g9mgXLE jn1A== X-Gm-Message-State: AOAM530My+ZT9n6d5Ap52UkD1XSzTOPFX5VDeezDVP5+jyDA/T1z/uH2 hdRqrM6pYg/hTLngvxzlWgISygF90bo13Zia X-Google-Smtp-Source: ABdhPJykH7z4Ri1n0bTqSrPG2811HKmOZ3P7liIcPcsyvk00IV5wxbSswCbzrIuOcFCh4PvRc5B5zw== X-Received: by 2002:a17:906:585a:: with SMTP id h26mr339573ejs.31.1632929339168; Wed, 29 Sep 2021 08:28:59 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:58 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 06/11] ip: nexthop: pull ipnh_get_id rtnl talk into a helper Date: Wed, 29 Sep 2021 18:28:43 +0300 Message-Id: <20210929152848.1710552-7-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Pull ipnh_get_id's rtnl talk portion into a separate helper which will be reused later to retrieve nexthops for caching. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index e334b852aa55..0a08230fc278 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -431,6 +431,25 @@ out_err: return err; } +static int __ipnh_get_id(struct rtnl_handle *rthp, __u32 nh_id, + struct nlmsghdr **answer) +{ + struct { + struct nlmsghdr n; + struct nhmsg nhm; + char buf[1024]; + } req = { + .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)), + .n.nlmsg_flags = NLM_F_REQUEST, + .n.nlmsg_type = RTM_GETNEXTHOP, + .nhm.nh_family = preferred_family, + }; + + addattr32(&req.n, sizeof(req), NHA_ID, nh_id); + + return rtnl_talk(rthp, &req.n, answer); +} + int print_nexthop(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); @@ -820,21 +839,9 @@ static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv) static int ipnh_get_id(__u32 id) { - struct { - struct nlmsghdr n; - struct nhmsg nhm; - char buf[1024]; - } req = { - .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)), - .n.nlmsg_flags = NLM_F_REQUEST, - .n.nlmsg_type = RTM_GETNEXTHOP, - .nhm.nh_family = preferred_family, - }; struct nlmsghdr *answer; - addattr32(&req.n, sizeof(req), NHA_ID, id); - - if (rtnl_talk(&rth, &req.n, &answer) < 0) + if (__ipnh_get_id(&rth, id, &answer) < 0) return -2; new_json_obj(json); From patchwork Wed Sep 29 15:28:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525835 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C460C4332F for ; Wed, 29 Sep 2021 15:30:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 60F6A6147F for ; Wed, 29 Sep 2021 15:30:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345357AbhI2Pbt (ORCPT ); Wed, 29 Sep 2021 11:31:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345314AbhI2Pbm (ORCPT ); Wed, 29 Sep 2021 11:31:42 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA78FC06161C for ; Wed, 29 Sep 2021 08:30:00 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id r18so9980049edv.12 for ; Wed, 29 Sep 2021 08:30:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tgN79BvesPKY6SwuQbLpQKODnpT+35wR9t+40IBfPbY=; b=iN3HoSZD1wmNYmGPPDoJNwNrLQ7tOUheDnwvqEmvykzs3zTnXikf0IlQlYI0N3gBj1 G2B8dSqlvoI+wSbpaL/IOrUTsvEmE4REOAjXp33Y40UQyQjt7PWTTk4ucQyLahA7VfLR ot7Lu78Z4lNXfBZ8aiffQ81uA84ewLkBTsIbmoMSUAKH/xdEZpvrkhvHytctb6Omzf6i xX6BYZbb3Sb8Ejv5Qg5ATPgD0jerYQzTf/S8Tk7MhaZBc6WqwrEAYIImaYOSdoqGz89B q9Yi9Zu4JAVyoTZ72J0WhC/20Cx/Z42HQ1jaMwUQGZLUdSkFsm1UvU+dR99jlU2AMYKh EsSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tgN79BvesPKY6SwuQbLpQKODnpT+35wR9t+40IBfPbY=; b=GqD2360uu8OO3hySc2QoYtM/v9+x/yxg9WTInCHorD/m6ils+zKXfEOUTIg5vnC6e8 PiV75oWhV4hVucbP+eB08VFpYBU+KgoHPUgDpMkI50wa7rOhKPH32ZKDSw9i+zmbEm1z dK3EJSldH9ciqrbLkFeRsBrr4CWX43qri41dB+Mv8Oe4YC5P5dvX1VPq+gysPP87MVUb CgY6m2TCvu2cGZV16cguVaINx/uPBA2J3EnLUusXcZeEdMpVSOeiz+lyyY6kRuAUX6aS JWUPoU+8kiODqReqG3GPde48UpusQ7rn+GxFmJsW2HclKYJFTPTS4nskCo5IT5qMeBQM ZdmQ== X-Gm-Message-State: AOAM531lP60XNM/49LsIs2gklLoHP9RTCv4BXQGgWQFkmy5GKkyxTopf PMSa3dLHRzXOBs3SuNWW4JVPSgE9/Wugksdp X-Google-Smtp-Source: ABdhPJwl9CVd14MkBUo1H58daphPHlfNfU2jQCKud2GCJVPgZ+EDVDveDi8aZs6gU5Szb6x1jmbF+Q== X-Received: by 2002:a17:906:1299:: with SMTP id k25mr334079ejb.139.1632929340250; Wed, 29 Sep 2021 08:29:00 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:28:59 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 07/11] ip: nexthop: add cache helpers Date: Wed, 29 Sep 2021 18:28:44 +0300 Message-Id: <20210929152848.1710552-8-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Add a static nexthop cache in a hash with 1024 buckets and helpers to manage it (link, unlink, find, add nexthop, del nexthop). Adding new nexthops is done by creating a new rtnl handle and using it to retrieve the nexthop so the helper is safe to use while already reading a response (i.e. using the global rth). Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 112 +++++++++++++++++++++++++++++++++++++++++++++---- ip/nh_common.h | 6 +++ 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 0a08230fc278..6e5ea47ac927 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -34,6 +34,8 @@ enum { #define RTM_NHA(h) ((struct rtattr *)(((char *)(h)) + \ NLMSG_ALIGN(sizeof(struct nhmsg)))) +static struct hlist_head nh_cache[NH_CACHE_SIZE]; + static void usage(void) __attribute__((noreturn)); static void usage(void) @@ -347,6 +349,41 @@ static void ipnh_destroy_entry(struct nh_entry *nhe) free(nhe->nh_groups); } +static struct hlist_head *ipnh_cache_head(__u32 nh_id) +{ + nh_id ^= nh_id >> 20; + nh_id ^= nh_id >> 10; + + return &nh_cache[nh_id % NH_CACHE_SIZE]; +} + +static void ipnh_cache_link_entry(struct nh_entry *nhe) +{ + struct hlist_head *head = ipnh_cache_head(nhe->nh_id); + + hlist_add_head(&nhe->nh_hash, head); +} + +static void ipnh_cache_unlink_entry(struct nh_entry *nhe) +{ + hlist_del(&nhe->nh_hash); +} + +static struct nh_entry *ipnh_cache_get(__u32 nh_id) +{ + struct hlist_head *head = ipnh_cache_head(nh_id); + struct nh_entry *nhe; + struct hlist_node *n; + + hlist_for_each(n, head) { + nhe = container_of(n, struct nh_entry, nh_hash); + if (nhe->nh_id == nh_id) + return nhe; + } + + return NULL; +} + /* parse nhmsg into nexthop entry struct which must be destroyed by * ipnh_destroy_enty when it's not needed anymore */ @@ -372,7 +409,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len, if (RTA_PAYLOAD(tb[NHA_GATEWAY]) > sizeof(nhe->nh_gateway)) { fprintf(fp, "\n", nhe->nh_id, RTA_PAYLOAD(tb[NHA_GATEWAY])); - err = EINVAL; + err = -EINVAL; goto out_err; } nhe->nh_gateway_len = RTA_PAYLOAD(tb[NHA_GATEWAY]); @@ -383,7 +420,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len, if (tb[NHA_ENCAP]) { nhe->nh_encap = malloc(RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP]))); if (!nhe->nh_encap) { - err = ENOMEM; + err = -ENOMEM; goto out_err; } memcpy(nhe->nh_encap, tb[NHA_ENCAP], @@ -396,13 +433,13 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len, if (!__valid_nh_group_attr(tb[NHA_GROUP])) { fprintf(fp, "", nhe->nh_id); - err = EINVAL; + err = -EINVAL; goto out_err; } nhe->nh_groups = malloc(RTA_PAYLOAD(tb[NHA_GROUP])); if (!nhe->nh_groups) { - err = ENOMEM; + err = -ENOMEM; goto out_err; } nhe->nh_groups_cnt = RTA_PAYLOAD(tb[NHA_GROUP]) / @@ -450,6 +487,67 @@ static int __ipnh_get_id(struct rtnl_handle *rthp, __u32 nh_id, return rtnl_talk(rthp, &req.n, answer); } +static int __ipnh_cache_parse_nlmsg(const struct nlmsghdr *n, + struct nh_entry *nhe) +{ + int err, len; + + len = n->nlmsg_len - NLMSG_SPACE(sizeof(struct nhmsg)); + if (len < 0) { + fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); + return -EINVAL; + } + + err = ipnh_parse_nhmsg(stderr, NLMSG_DATA(n), len, nhe); + if (err) { + fprintf(stderr, "Error parsing nexthop: %s\n", strerror(-err)); + return err; + } + + return 0; +} + +static struct nh_entry *ipnh_cache_add(__u32 nh_id) +{ + struct rtnl_handle cache_rth = { .fd = -1 }; + struct nlmsghdr *answer = NULL; + struct nh_entry *nhe = NULL; + + if (rtnl_open(&cache_rth, 0) < 0) + goto out; + + if (__ipnh_get_id(&cache_rth, nh_id, &answer) < 0) + goto out; + + nhe = malloc(sizeof(*nhe)); + if (!nhe) + goto out; + + if (__ipnh_cache_parse_nlmsg(answer, nhe)) + goto out_free_nhe; + + ipnh_cache_link_entry(nhe); + +out: + if (answer) + free(answer); + rtnl_close(&cache_rth); + + return nhe; + +out_free_nhe: + free(nhe); + nhe = NULL; + goto out; +} + +static void ipnh_cache_del(struct nh_entry *nhe) +{ + ipnh_cache_unlink_entry(nhe); + ipnh_destroy_entry(nhe); + free(nhe); +} + int print_nexthop(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); @@ -476,10 +574,10 @@ int print_nexthop(struct nlmsghdr *n, void *arg) if (filter.proto && filter.proto != nhm->nh_protocol) return 0; - err = parse_nexthop_rta(fp, nhm, len, &nhe); + err = ipnh_parse_nhmsg(fp, nhm, len, &nhe); if (err) { close_json_object(); - fprintf(stderr, "Error parsing nexthop: %s\n", strerror(err)); + fprintf(stderr, "Error parsing nexthop: %s\n", strerror(-err)); return -1; } open_json_object(NULL); @@ -530,7 +628,7 @@ int print_nexthop(struct nlmsghdr *n, void *arg) print_string(PRINT_FP, NULL, "%s", "\n"); close_json_object(); fflush(fp); - destroy_nexthop_entry(&nhe); + ipnh_destroy_entry(&nhe); return 0; } diff --git a/ip/nh_common.h b/ip/nh_common.h index 8c96f9993562..a34b0d20916e 100644 --- a/ip/nh_common.h +++ b/ip/nh_common.h @@ -2,6 +2,10 @@ #ifndef __NH_COMMON_H__ #define __NH_COMMON_H__ 1 +#include + +#define NH_CACHE_SIZE 1024 + struct nha_res_grp { __u16 buckets; __u32 idle_timer; @@ -10,6 +14,8 @@ struct nha_res_grp { }; struct nh_entry { + struct hlist_node nh_hash; + __u32 nh_id; __u32 nh_oif; __u32 nh_flags; From patchwork Wed Sep 29 15:28:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525837 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2C4CC433F5 for ; Wed, 29 Sep 2021 15:30:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 873016147F for ; Wed, 29 Sep 2021 15:30:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345373AbhI2Pb4 (ORCPT ); Wed, 29 Sep 2021 11:31:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345323AbhI2Pbm (ORCPT ); Wed, 29 Sep 2021 11:31:42 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA873C061765 for ; Wed, 29 Sep 2021 08:30:00 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id ba1so10223429edb.4 for ; Wed, 29 Sep 2021 08:30:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iywxHLGLbQZJ2pqzj06yOx10EVmFHqmsnHs+I0KD2mw=; b=QxoIzQZP3CGImo8ESuMbOpEqN+njrknQFJRqJy6Dy5MLMlTgDbITT34bTUkLS4BoKY 9Zpa6Xus8MuwsT/u5FBFRVo2DwgL2Os+zouEdA82zo8uJ6yRTmX6nv95RkG7WzocahOb N2yN674vqNJBGDSaNnGRCdUgmw6Mkso/8LpG2JOYpil2M2e00tKKc1hMoWJIrKYqOn4Q AUStEFxl7XKmXhfGvNZ+w/fBII4QpLTAog7VjwXtP1nM0KcmugWxfb9UgIa0owPlPkMf UvL/h8yu/4fUEop+j9VCYFxs5qiWzrED2+7Qghing/WZ0pv8b/k6jEbmRosaF0GihKmG S/mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iywxHLGLbQZJ2pqzj06yOx10EVmFHqmsnHs+I0KD2mw=; b=XmlMp5cF9/m6zooWnxD10zo3Ko+SChePMhi6jnN3icgComPSfJr7tcHHDDidym7aRX 8dGpqGyq+vNn+da9CRFOg4rqVXcWCKVrsyIIdxd/M+ahdPwk5ao3clYvxQ6swSc+lk75 XdR0FBhhxdnSVKXZWgGhKhhoeFvpif4b9v7n42559qs/ieN7TPu/foGQYb9rXQNg2aPT 1OTF5yGE5gNWuUFc56Cf/cjNZCRqRhEtYYc+FnsGIKMuqeaQDgENsVit1h4eJsKVZVvj Sv6BCmifQ2IRpHEdIj1EtDVh7VpDgbht5ZhB0XY7KbidE6edbiTlCNwth2wv/U0cz0FE yyzw== X-Gm-Message-State: AOAM5316E7/AJK1NDqDZfhaB5KRtWCeBu9ZgAG31RbxU2gj5r1rga2qY nwSS++tdWwpwhK1hT86m1UmiE8/3/vQjw4MQ X-Google-Smtp-Source: ABdhPJwKbDsD3dsiYUPUkkPvGS8eUXgw0ImVP+0rokAp0AkJIxjgjSZiMloYlOnYqSn8jb7rAf3+Ow== X-Received: by 2002:a17:906:144e:: with SMTP id q14mr332891ejc.19.1632929341267; Wed, 29 Sep 2021 08:29:01 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.29.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:29:00 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 08/11] ip: nexthop: factor out entry printing Date: Wed, 29 Sep 2021 18:28:45 +0300 Message-Id: <20210929152848.1710552-9-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Factor out nexthop entry printing into a separate function. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 103 ++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 6e5ea47ac927..37b94d6702df 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -548,6 +548,60 @@ static void ipnh_cache_del(struct nh_entry *nhe) free(nhe); } +static void __print_nexthop_entry(FILE *fp, const char *jsobj, + struct nh_entry *nhe, + bool deleted) +{ + SPRINT_BUF(b1); + + open_json_object(jsobj); + + if (deleted) + print_bool(PRINT_ANY, "deleted", "Deleted ", true); + + print_uint(PRINT_ANY, "id", "id %u ", nhe->nh_id); + + if (nhe->nh_groups) + print_nh_group(nhe); + + print_nh_group_type(nhe->nh_grp_type); + + if (nhe->nh_has_res_grp) + print_nh_res_group(&nhe->nh_res_grp); + + if (nhe->nh_encap) + lwt_print_encap(fp, &nhe->nh_encap_type.rta, nhe->nh_encap); + + if (nhe->nh_gateway_len) + __print_rta_gateway(fp, nhe->nh_family, + format_host(nhe->nh_family, + nhe->nh_gateway_len, + &nhe->nh_gateway)); + + if (nhe->nh_oif) + print_rta_ifidx(fp, nhe->nh_oif, "dev"); + + if (nhe->nh_scope != RT_SCOPE_UNIVERSE || show_details > 0) { + print_string(PRINT_ANY, "scope", "scope %s ", + rtnl_rtscope_n2a(nhe->nh_scope, b1, sizeof(b1))); + } + + if (nhe->nh_blackhole) + print_null(PRINT_ANY, "blackhole", "blackhole ", NULL); + + if (nhe->nh_protocol != RTPROT_UNSPEC || show_details > 0) { + print_string(PRINT_ANY, "protocol", "proto %s ", + rtnl_rtprot_n2a(nhe->nh_protocol, b1, sizeof(b1))); + } + + print_rt_flags(fp, nhe->nh_flags); + + if (nhe->nh_fdb) + print_null(PRINT_ANY, "fdb", "fdb", NULL); + + close_json_object(); +} + int print_nexthop(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); @@ -555,8 +609,6 @@ int print_nexthop(struct nlmsghdr *n, void *arg) struct nh_entry nhe; int len, err; - SPRINT_BUF(b1); - if (n->nlmsg_type != RTM_DELNEXTHOP && n->nlmsg_type != RTM_NEWNEXTHOP) { fprintf(stderr, "Not a nexthop: %08x %08x %08x\n", @@ -580,53 +632,8 @@ int print_nexthop(struct nlmsghdr *n, void *arg) fprintf(stderr, "Error parsing nexthop: %s\n", strerror(-err)); return -1; } - open_json_object(NULL); - - if (n->nlmsg_type == RTM_DELNEXTHOP) - print_bool(PRINT_ANY, "deleted", "Deleted ", true); - - print_uint(PRINT_ANY, "id", "id %u ", nhe.nh_id); - - if (nhe.nh_groups) - print_nh_group(&nhe); - - print_nh_group_type(nhe.nh_grp_type); - - if (nhe.nh_has_res_grp) - print_nh_res_group(&nhe.nh_res_grp); - - if (nhe.nh_encap) - lwt_print_encap(fp, &nhe.nh_encap_type.rta, nhe.nh_encap); - - if (nhe.nh_gateway_len) - __print_rta_gateway(fp, nhe.nh_family, - format_host(nhe.nh_family, - nhe.nh_gateway_len, - &nhe.nh_gateway)); - - if (nhe.nh_oif) - print_rta_ifidx(fp, nhe.nh_oif, "dev"); - - if (nhe.nh_scope != RT_SCOPE_UNIVERSE || show_details > 0) { - print_string(PRINT_ANY, "scope", "scope %s ", - rtnl_rtscope_n2a(nhe.nh_scope, b1, sizeof(b1))); - } - - if (nhe.nh_blackhole) - print_null(PRINT_ANY, "blackhole", "blackhole ", NULL); - - if (nhe.nh_protocol != RTPROT_UNSPEC || show_details > 0) { - print_string(PRINT_ANY, "protocol", "proto %s ", - rtnl_rtprot_n2a(nhe.nh_protocol, b1, sizeof(b1))); - } - - print_rt_flags(fp, nhe.nh_flags); - - if (nhe.nh_fdb) - print_null(PRINT_ANY, "fdb", "fdb", NULL); - + __print_nexthop_entry(fp, NULL, &nhe, n->nlmsg_type == RTM_DELNEXTHOP); print_string(PRINT_FP, NULL, "%s", "\n"); - close_json_object(); fflush(fp); ipnh_destroy_entry(&nhe); From patchwork Wed Sep 29 15:28:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525831 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45042C433F5 for ; Wed, 29 Sep 2021 15:30:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C203614C8 for ; Wed, 29 Sep 2021 15:30:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345340AbhI2Pbq (ORCPT ); Wed, 29 Sep 2021 11:31:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345322AbhI2Pbl (ORCPT ); Wed, 29 Sep 2021 11:31:41 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 123FDC061760 for ; Wed, 29 Sep 2021 08:30:00 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id bd28so10042056edb.9 for ; Wed, 29 Sep 2021 08:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Oq58EzNy664aUWc2V8EL3IoGwOqdTRT7Eo3bwpve3h8=; b=0BGG03do6qXO8pppx8FP/TlbsJHv/Ez5W6BCOj/ncvfXhCksixoQEqTh36mUyyKa7l k/QfTNMjkewDjcNUIvdeTqkJ5hlfE/2EOvVUqdst598gO2C49U2I9WXfi04njO6k/0ro dDoeQMqLlxmvgbavBciYW2cNJnFRB6K0yGgiUUGsk9q4flDggCXf5OyNg5lekY2t5Ipa C1z+ds0YqGkdnmVbxr4Dzka9hG4OvPNMQb2umJtSMUvADFb1zzAZGBzsMdITwnHABqU5 ZzcV09A+Odget2xbHki60YbkTYLGyKJpd9y/sbfWVR0t4W9D7g+bdBE69elSwFwP5NEj opng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Oq58EzNy664aUWc2V8EL3IoGwOqdTRT7Eo3bwpve3h8=; b=7ZL2iYb3MdG/rGkZlrHi9RVBXiDzBe5MqyK3Qb4X6eonC3L4qSc0sSmnfLNHrWeb+s +4S00ExOvR63+PETYJ6vmtsi+UbZKwZ/u3IZyXDM7ka2CzRubdOdeO5wOuZmQGneW3hi 6HgZwgoU+y+7hZ6gzUK0SA8MgBC2ch2J5RE/uMI8HlqC5falUfZF9KJ4j6Bsb4/RtU2h PcPSR655qY2538y4XkbrnEPT0VhzToF78+Ri3nc48sRV6BPeUvJrhgAUnHZR/zawt7vz UG9evKulObEa4mWmwCyLpuEk4C5rIWm4r4rA/LM2VjvIElcsSYr5cgd8dV2aaI29go79 R3fQ== X-Gm-Message-State: AOAM533JqRrHh5P099d0FVvMLgPTkjMFHBlMEw8lXhbaTgwrc5xTVhNg 5AlQ2S4bWsSVQdYvlfQVsi/k1d9jI6rgeGQq X-Google-Smtp-Source: ABdhPJwilPpRXfgtzctrLEAd9V4D6nI6ey8+jV6MyNz2AadBXNQBv7XPJChhdo2LGemx1/cXvCnWmw== X-Received: by 2002:a50:dac2:: with SMTP id s2mr581558edj.141.1632929342630; Wed, 29 Sep 2021 08:29:02 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.29.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:29:02 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 09/11] ip: nexthop: add a helper which retrieves and prints cached nh entry Date: Wed, 29 Sep 2021 18:28:46 +0300 Message-Id: <20210929152848.1710552-10-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Add a helper which looks for a nexthop in the cache and if not found reads the entry from the kernel and caches it. Finally the entry is printed. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 16 ++++++++++++++++ ip/nh_common.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 37b94d6702df..fdd0d0926630 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -640,6 +640,22 @@ int print_nexthop(struct nlmsghdr *n, void *arg) return 0; } +void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, + __u32 nh_id) +{ + struct nh_entry *nhe = ipnh_cache_get(nh_id); + + if (!nhe) { + nhe = ipnh_cache_add(nh_id); + if (!nhe) + return; + } + + if (fp_prefix) + print_string(PRINT_FP, NULL, "%s", fp_prefix); + __print_nexthop_entry(fp, jsobj, nhe, false); +} + int print_nexthop_bucket(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); diff --git a/ip/nh_common.h b/ip/nh_common.h index a34b0d20916e..a672d658a9ea 100644 --- a/ip/nh_common.h +++ b/ip/nh_common.h @@ -46,4 +46,7 @@ struct nh_entry { struct nexthop_grp *nh_groups; }; +void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, + __u32 nh_id); + #endif /* __NH_COMMON_H__ */ From patchwork Wed Sep 29 15:28:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525839 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C816EC433F5 for ; Wed, 29 Sep 2021 15:30:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B11E36147F for ; Wed, 29 Sep 2021 15:30:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345381AbhI2PcB (ORCPT ); Wed, 29 Sep 2021 11:32:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345322AbhI2Pbr (ORCPT ); Wed, 29 Sep 2021 11:31:47 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0B47C061768 for ; Wed, 29 Sep 2021 08:30:03 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id dn26so10078207edb.13 for ; Wed, 29 Sep 2021 08:30:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ce3l0OvB2Gpoh3gPrIu9QlxGlLt5g5zxnFHEz7aNjwQ=; b=KN8K86dhnTe6GLUqnMgmAAjcZPVRvdDAgcBXCBKlaonSq6jIFHD/gJH98/dWGpzC4M 1gouCe9EqPUZLIPp33803YeXC6h3TG4P+uS08IvWiwN5p6qyL8KmCCafXBNF7UXMnamI 8RHvDrZG5EkcdjB1mu6Kn/nL0rvgoe5MUvOyCyUXXKSGcyc/3YFsUtdTncmUcD1FdQf3 BaduBe3sYyzQYJMxNxgeVumWqUTquHniueoyRgsF4IYtrVLhVAoGM4zXUiSQg39+5zxm Mj0FwwmtqytPp3+OwE6WIsuRd1DGnmMzNab0F7OPlNK9KK8fhgkNLvUnF+37B2SQo98f ZQ+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ce3l0OvB2Gpoh3gPrIu9QlxGlLt5g5zxnFHEz7aNjwQ=; b=uNsdSikD9UFruolYkCscaWMWaTcJSJrmVlmJCLo6zn6kgR9LVxfzRXRF7H1ld86tmb orWX9n4I5GnSvtNi10l5wRM40I1Ds+3D7ovM9lChuWzeKdeOytpPAYGr9uOKOS4ZafOS lyFKmhq7T9red761PCBuNUH2F54rU2fT1UgbgtbPgYow7otDgdW0nlUwVztceNGO6IB9 Jf42/MR2BnYPWLQjnVIvx+VyqOJX4kuhoR6e9xqbfqxITJVs2eAhVvBhhDO8QDvfk2EH mUShk6geTxIFRYD7X/3UFZh0ku84mDZGMLyRrA+2tYruwmX3UE9gtjYZzXBb8OWgX2Wh E4XQ== X-Gm-Message-State: AOAM532+i9IOZsacK9HASY3MI67cfO9lAlIghANZRv6WgGEhn0BcLo3o +r4cvtfzdlaXr1O68yUFsNu/3pFNg7lw7xUl X-Google-Smtp-Source: ABdhPJyd9H/46xsf5pxs0MzehG7hAIQnjmv/85Eo5ZA6dsLfrKtpLon77V+AXH0ZPM3+mWzbWsevJg== X-Received: by 2002:a50:cfc1:: with SMTP id i1mr536962edk.251.1632929344277; Wed, 29 Sep 2021 08:29:04 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.29.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:29:03 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 10/11] ip: route: print and cache detailed nexthop information when requested Date: Wed, 29 Sep 2021 18:28:47 +0300 Message-Id: <20210929152848.1710552-11-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov If -d (show_details) is used when printing/monitoring routes then print detailed nexthop information in the field "nh_info". The nexthop is also cached for future searches. Output looks like: unicast 198.51.100.0/24 nhid 103 table 3 proto boot scope global nh_info id 103 group 101/102 type resilient buckets 512 idle_timer 0 unbalanced_timer 0 unbalanced_time 0 scope global proto unspec nexthop via 169.254.2.22 dev veth2 weight 1 nexthop via 169.254.3.23 dev veth4 weight 1 The nh_info field has the same format as ip -d nexthop show would've had for the same nexthop id. For completeness the JSON version looks like: { "type": "unicast", "dst": "198.51.100.0/24", "nhid": 103, "table": "3", "protocol": "boot", "scope": "global", "flags": [ ], "nh_info": { "id": 103, "group": [ { "id": 101 },{ "id": 102 } ], "type": "resilient", "resilient_args": { "buckets": 512, "idle_timer": 0, "unbalanced_timer": 0, "unbalanced_time": 0 }, "scope": "global", "protocol": "unspec", "flags": [ ] }, "nexthops": [ { "gateway": "169.254.2.22", "dev": "veth2", "weight": 1, "flags": [ ] },{ "gateway": "169.254.3.23", "dev": "veth4", "weight": 1, "flags": [ ] } ] } Signed-off-by: Nikolay Aleksandrov --- ip/iproute.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ip/iproute.c b/ip/iproute.c index 3c933df4dd29..8532b5ce315e 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -28,6 +28,7 @@ #include "rt_names.h" #include "utils.h" #include "ip_common.h" +#include "nh_common.h" #ifndef RTAX_RTTVAR #define RTAX_RTTVAR RTAX_HOPS @@ -968,6 +969,10 @@ int print_route(struct nlmsghdr *n, void *arg) propagate ? "enabled" : "disabled"); } + if (tb[RTA_NH_ID] && show_details) + print_cache_nexthop_id(fp, "\n\tnh_info ", "nh_info", + rta_getattr_u32(tb[RTA_NH_ID])); + if (tb[RTA_MULTIPATH]) print_rta_multipath(fp, r, tb[RTA_MULTIPATH]); From patchwork Wed Sep 29 15:28:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12525825 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E74DC433F5 for ; Wed, 29 Sep 2021 15:29:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F159A6147F for ; Wed, 29 Sep 2021 15:29:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345321AbhI2Pbi (ORCPT ); Wed, 29 Sep 2021 11:31:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345310AbhI2Pbf (ORCPT ); Wed, 29 Sep 2021 11:31:35 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44A6FC06161C for ; Wed, 29 Sep 2021 08:29:53 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id dj4so10525855edb.5 for ; Wed, 29 Sep 2021 08:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=t8pP26/7fb5Wq+dpeoArozy4Jca0fDg3x4MgpY2CvXY=; b=w1dP7KQsV+0AZQp0DZAVlJ0jJ5clsLTFD5g2mjiy3f7J3suDb+Th1UOCHOb9KbIAZS g1ves4JPO74hBF8f9jfq3EiTDj12S+GE2PpKnWe6M+pUrqL8wjJbj/arAY80Qi1XPY7U nNNSZtq2uUeYZ5HbSziH3bRFxviLNEcXTMD3zD50tZT++jxxf5csd60fJedy2Qi9zl34 L/jaC6jRGTjhyUJZcokGvFExWBY1o8MCXQhOn7prNoA29ry12lu9PfuJasN+GU+JWwqs PFSGY31URbA/PHiAu2MqtF1Tf7u91PzzIGRTz2jLLXv3oPkrY0hAkwBVQdTFtJukN7c+ +sXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=t8pP26/7fb5Wq+dpeoArozy4Jca0fDg3x4MgpY2CvXY=; b=4vj0XLbg3X0C6FHS42Dl2wmGMKkcpyGVcAVj0KQmduN8KucINjVmvEE05oADp5OHSO kL3g3WKG3D6+gs/FriyAN1BfP9OK0oJd9hXUos68m3JaIeWe8o1BhA3EotXTnjLVX5zT omXGXmpVrjy704FRrKu6qkZMnQ7RabLpzUsaUvtPCOTuw4tZ49ova0imL8k5dkGDcfFE 9Bsf+f7zBdshwGFYP1Y0niD5Px464Ntk27+YEDBkimsGG7dA64KExh/gFRuXZ+V+lI4j fWnAlRxAqHRyqlmBR8R4guoGldH3Qer6mTgoc4nAtGNu+8DBIXRnlTY0ns/aiTtrVBPN YC8g== X-Gm-Message-State: AOAM533ozANULmtOcnEx0kmUhe03mvd5z0Iik0FLqV+QXcQ/91OXKUI0 U7eJi+uctjSwZz0DzBuwUXtMJxMStNSO/+ys X-Google-Smtp-Source: ABdhPJw8s24H0nJhtp72jqVkDQvgwQu2EK6pZgTHTDX53e+EeYpyM6EiQ+eu6NEL2OIrMOpDOR5AEg== X-Received: by 2002:a05:6402:16d8:: with SMTP id r24mr562317edx.47.1632929345861; Wed, 29 Sep 2021 08:29:05 -0700 (PDT) Received: from debil.vdiclient.nvidia.com (84-238-136-197.ip.btc-net.bg. [84.238.136.197]) by smtp.gmail.com with ESMTPSA id q12sm108434ejs.58.2021.09.29.08.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 08:29:05 -0700 (PDT) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: roopa@nvidia.com, donaldsharp72@gmail.com, dsahern@gmail.com, idosch@idosch.org, Nikolay Aleksandrov Subject: [RFC iproute2-next 11/11] ip: nexthop: add print_cache_nexthop which prints and manages the nh cache Date: Wed, 29 Sep 2021 18:28:48 +0300 Message-Id: <20210929152848.1710552-12-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210929152848.1710552-1-razor@blackwall.org> References: <20210929152848.1710552-1-razor@blackwall.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com X-Patchwork-State: RFC From: Nikolay Aleksandrov Add a new helper print_cache_nexthop replacing print_nexthop which can update the nexthop cache if the process_cache argument is true. It is used when monitoring netlink messages to keep the nexthop cache up to date with nexthop changes happening. For the old callers and anyone who's just dumping nexthops its _nocache version is used which is a wrapper for print_cache_nexthop. Signed-off-by: Nikolay Aleksandrov --- ip/ip_common.h | 1 - ip/ipmonitor.c | 3 ++- ip/ipnexthop.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++---- ip/nh_common.h | 1 + 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/ip/ip_common.h b/ip/ip_common.h index a02a3b96f7fd..ea04c8ff3dea 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -53,7 +53,6 @@ int print_prefix(struct nlmsghdr *n, void *arg); int print_rule(struct nlmsghdr *n, void *arg); int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg); -int print_nexthop(struct nlmsghdr *n, void *arg); int print_nexthop_bucket(struct nlmsghdr *n, void *arg); void netns_map_init(void); void netns_nsid_socket_init(void); diff --git a/ip/ipmonitor.c b/ip/ipmonitor.c index ab1af2ebd6df..0badda4e7812 100644 --- a/ip/ipmonitor.c +++ b/ip/ipmonitor.c @@ -22,6 +22,7 @@ #include "utils.h" #include "ip_common.h" +#include "nh_common.h" static void usage(void) __attribute__((noreturn)); static int prefix_banner; @@ -88,7 +89,7 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl, case RTM_NEWNEXTHOP: case RTM_DELNEXTHOP: print_headers(fp, "[NEXTHOP]", ctrl); - print_nexthop(n, arg); + print_cache_nexthop(n, arg, true); return 0; case RTM_NEWNEXTHOPBUCKET: diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index fdd0d0926630..43dc238c7725 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -602,7 +602,43 @@ static void __print_nexthop_entry(FILE *fp, const char *jsobj, close_json_object(); } -int print_nexthop(struct nlmsghdr *n, void *arg) +/* update, add or delete a nexthop entry based on nlmsghdr */ +static int ipnh_cache_process_nlmsg(const struct nlmsghdr *n, + struct nh_entry *new_nhe) +{ + struct nh_entry *nhe; + + nhe = ipnh_cache_get(new_nhe->nh_id); + switch (n->nlmsg_type) { + case RTM_DELNEXTHOP: + if (nhe) + ipnh_cache_del(nhe); + ipnh_destroy_entry(new_nhe); + break; + case RTM_NEWNEXTHOP: + if (!nhe) { + nhe = malloc(sizeof(*nhe)); + if (!nhe) { + ipnh_destroy_entry(new_nhe); + return -1; + } + } else { + /* this allows us to save 1 allocation on updates by + * reusing the old nh entry, but we need to cleanup its + * internal storage + */ + ipnh_cache_unlink_entry(nhe); + ipnh_destroy_entry(nhe); + } + memcpy(nhe, new_nhe, sizeof(*nhe)); + ipnh_cache_link_entry(nhe); + break; + } + + return 0; +} + +int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache) { struct nhmsg *nhm = NLMSG_DATA(n); FILE *fp = (FILE *)arg; @@ -635,11 +671,20 @@ int print_nexthop(struct nlmsghdr *n, void *arg) __print_nexthop_entry(fp, NULL, &nhe, n->nlmsg_type == RTM_DELNEXTHOP); print_string(PRINT_FP, NULL, "%s", "\n"); fflush(fp); - ipnh_destroy_entry(&nhe); + + if (process_cache) + ipnh_cache_process_nlmsg(n, &nhe); + else + ipnh_destroy_entry(&nhe); return 0; } +static int print_nexthop_nocache(struct nlmsghdr *n, void *arg) +{ + return print_cache_nexthop(n, arg, false); +} + void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, __u32 nh_id) { @@ -967,7 +1012,7 @@ static int ipnh_get_id(__u32 id) new_json_obj(json); - if (print_nexthop(answer, (void *)stdout) < 0) { + if (print_nexthop_nocache(answer, (void *)stdout) < 0) { free(answer); return -1; } @@ -1052,7 +1097,7 @@ static int ipnh_list_flush(int argc, char **argv, int action) new_json_obj(json); - if (rtnl_dump_filter(&rth, print_nexthop, stdout) < 0) { + if (rtnl_dump_filter(&rth, print_nexthop_nocache, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); return -2; } diff --git a/ip/nh_common.h b/ip/nh_common.h index a672d658a9ea..2ad8b134f891 100644 --- a/ip/nh_common.h +++ b/ip/nh_common.h @@ -48,5 +48,6 @@ struct nh_entry { void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, __u32 nh_id); +int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache); #endif /* __NH_COMMON_H__ */