From patchwork Thu Sep 30 11:38:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12528071 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 BF5A4C433F5 for ; Thu, 30 Sep 2021 11:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9CB28611BD for ; Thu, 30 Sep 2021 11:39:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350548AbhI3LlN (ORCPT ); Thu, 30 Sep 2021 07:41:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350531AbhI3LlJ (ORCPT ); Thu, 30 Sep 2021 07:41:09 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72CC2C06176A for ; Thu, 30 Sep 2021 04:39:26 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id g7so20814773edv.1 for ; Thu, 30 Sep 2021 04:39:26 -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=fRlfhMNcAPDAO0dQu+cgQV57tXluBTj2+du1H2IdIJujIORYA96akpqPif5YRnt7ld NUUgLFDvfR2+/eS/p/mtPHyNWedrJDUg4aomRMwvH8DL5U5phReSogpM8ylnB4357csb LeuhssLFr6mTi0OCgo0AQ4LcqJDQF5favtUGYCUQQWpvwqQQs25ypIFJybmx6k5EER6S Ozf6AIU+X2M9Aan8z7FHhDiZMvI2WKtS+tyqJqT5PGSCesOO8vsmwum8FKqp/L0xJU9a vg1C5qw+QoHZ4OYZV1bmkY46PjH0NPWwGT60+l2HTp2QitfPTadsQIeWUOPxIVx92A2/ FFfw== 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=qdO9XNHeys6ldH4wiss6AD7SJ/5wQ6hgno6qug+0TNRoVErkX3x1viG2tEwwEk9bcv Ooj+slLTiTfTlTcbHqpl8L6SGBg7rKRtC6Oa0yMdeGd2Nl/U8YH+nF8vFjcxW4/jge5c 5yN8ce/KHRmzbrK+Akwi4fI4z8vbB+UR/1V04Q34qZYsFPtFVi1llyTK8mv8ShW5MeF6 iCZ6knpou+E2Xlv8fSovKwJ9cjcPkq2+SHGhndwYcbxEI0F8wJIoXUT2tITMSo2foHAc Qqw0oDttPR/MPN0bNLmrDIaFbrErk5lr40FcfxllknWxj0V7Zdermmv61vS4jcOU6VCR Ih3A== X-Gm-Message-State: AOAM533pI13X4+8kuWKvMvHtGqTeCu01+LZyUxHm4LLt/D6yt4n2wZ1G 2T5SOFsCZt/TRalLcOSE76cZJn2Japbu/gZp X-Google-Smtp-Source: ABdhPJxHl+Ae0MeTzD8q1cM5lA6i58tlGshZed9OejdBxMSQYZwbxqa7oairhl4pipor37oadSrVKw== X-Received: by 2002:a17:906:c005:: with SMTP id e5mr6007140ejz.480.1633001964618; Thu, 30 Sep 2021 04:39:24 -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 b27sm1277704ejq.34.2021.09.30.04.39.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:24 -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: [PATCH iproute2-next 01/12] ip: print_rta_if takes ifindex as device argument instead of attribute Date: Thu, 30 Sep 2021 14:38:33 +0300 Message-Id: <20210930113844.1829373-2-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 Thu Sep 30 11:38:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12528073 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 7A623C433F5 for ; Thu, 30 Sep 2021 11:39:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6185161555 for ; Thu, 30 Sep 2021 11:39:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350570AbhI3LlS (ORCPT ); Thu, 30 Sep 2021 07:41:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350447AbhI3LlJ (ORCPT ); Thu, 30 Sep 2021 07:41:09 -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 43616C06176C for ; Thu, 30 Sep 2021 04:39:27 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id l8so21219946edw.2 for ; Thu, 30 Sep 2021 04:39:27 -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=h6evyg7kFns6vckrf2GQZNpAbZWAf8DXuKoTZdoeWdw642ZKHtPYis2oGl20QJGIwL 5/GDCW2Inyfddtv63p27sSllSEFrFW6RI8/euJYYIsUbU7VK3CPutDqus8Ta2aBpdcb9 +2l58HnkZr51WNjtMYAGeSkukcWuNtkuxKCXAdPfkt2BH2AMS7+tI0A+EyMXX6IhcX8M MflHibPuT5hY8Ab0nzjjiBdYSYJElX8l9ShbVvLk98t5zqTQiK3YZlqzLhRRigeltaIV OTJuApKXH2T0aGb0V/lqAtHpP5yVCyM1E5eUz73UjWuvwcYMlfdSdXTx/vFvDe/7M2bl lRYQ== 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=Pacnm8hvdpd6HAo2oOzNqm2K8NxojeBSoOvQs5MIeXMfuHXa4yhuXnWZdUV5pcfwpA 5mvYx/qdhhYK8YppnmKPJ7NhvgxwJkg2TRB/0y6Bxl5T4/WWNwochrVdima0uQvD31bb BM1ajcFpDkBZV1WVjlrCLPG+iAEhcveHpkIxMeydZ/T13HAy3KDMBHJ/chKJGeKyBkpS INq1vfdanMoRZGWykNzSggxYTJxF/YJvAB8LPchnXCH9rNPAyBqvQwWL+G89yJwsV7Al 1I1afJNBhpw2QPcozcOYCXvVcSM5spOWYQEVmSQRVLSF7hsEJznInaJA5cQxIp26cpIF IqLQ== X-Gm-Message-State: AOAM532RkKa+t0P++XCOAWeEU9tHnchR2n9lCuPcFN4nMULn6c0Zeoo8 lldR6vUmOo40zbWUpnteCn79Ppu399FlPy1+ X-Google-Smtp-Source: ABdhPJz3pCPBo1T6AP4HOER9TIXEXwBPp86QgatObIY+nzjmbLc/cW7tOQ3rCgDu7J4aBwH1sBRlRA== X-Received: by 2002:a17:906:5908:: with SMTP id h8mr6134618ejq.508.1633001965508; Thu, 30 Sep 2021 04:39:25 -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 b27sm1277704ejq.34.2021.09.30.04.39.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:25 -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: [PATCH iproute2-next 02/12] ip: export print_rta_gateway version which outputs prepared gateway string Date: Thu, 30 Sep 2021 14:38:34 +0300 Message-Id: <20210930113844.1829373-3-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 Thu Sep 30 11:38:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12528069 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 E38D9C433FE for ; Thu, 30 Sep 2021 11:39:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C614261555 for ; Thu, 30 Sep 2021 11:39:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350533AbhI3LlL (ORCPT ); Thu, 30 Sep 2021 07:41:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350532AbhI3LlK (ORCPT ); Thu, 30 Sep 2021 07:41:10 -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 4623CC06176D for ; Thu, 30 Sep 2021 04:39:28 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id dn26so20828127edb.13 for ; Thu, 30 Sep 2021 04:39:28 -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=4sGP3Guz9xCU5aSJe4hbX7plLMt6o+uQc/sFIGpykNs=; b=lVTl0VuLqcnuGzZrsw3T+eGWVh5SZ4sYwi2NWh02dCgPiqRj7O4ID77II+ViQHHrAb o2kXiK2BreR8nRscHOb6LqJ91fBF/s24DkQtQolKg1CvvuSt6fI0Zl3wkfAgCt4e86+l 9wqATRO4IX18gjzfdv7zV8l0ikiNHTezHozl1ZG60+0pUGJyh0Po5y4bglCGUwfL0Fb4 t0hYxIWs1BDyr9VJhgtfANwIPdPJkTOh6zHgAjg/N2J/omiNTPxupZ0EnLAO0Gc4tDK/ A7ClmILcDO89DRdGrtLDQiZhFB7tYyKsAOscwPzwbK7OV047KXnK9v/JLaMQ0oFGoTr2 pYZg== 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=4sGP3Guz9xCU5aSJe4hbX7plLMt6o+uQc/sFIGpykNs=; b=KbzdT4PQrMe2oZSNxSFCnwI3OAfJ+az/RzUAjjZyXsqcIC932/ppTn+ag1RfqAwFzR 7PJtd/jI8DArKg4d8wuMf+CH9sQdhodPl0lM7WJgCsXx0HmSAmmzBR10XyNEhm3xiziK Vk6f/+ZHhUN5MiniTNSPoSGaA5GlPWRmQQyc8P+u7RQr1dLevIogQraWp9A/0tnG1dXa jO5ULTrWWACnwmG9wKRXa7LlTfQZW/755o8TVBKJEBCN64nUBWQ/1RSaRJ1Zay3OVqUy amXTkRk3JFIGxM5yQdDbnB2l/uos5gl15+n1KBJSdBdy9FBTqjebsG/23MUk/Q7LlMe7 c+8A== X-Gm-Message-State: AOAM532XTcTb44elT7ACQDjMnVYslxQaPJxm6UUrJq++GBDTsWIhF/29 +LOADLw1XJgvim+iNJAXy3KJTi152mQOxOhH X-Google-Smtp-Source: ABdhPJzhN4SuWWv3LN/v/Qiyy4aAb3Nb4Aq0pnbaWvT7U3Z0tU4xFy7LYhdFKqhKk/2NEKZ9I5P8Lg== X-Received: by 2002:a05:6402:42d4:: with SMTP id i20mr6632560edc.348.1633001966556; Thu, 30 Sep 2021 04:39:26 -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 b27sm1277704ejq.34.2021.09.30.04.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:26 -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: [PATCH iproute2-next 03/12] ip: nexthop: add resilient group structure Date: Thu, 30 Sep 2021 14:38:35 +0300 Message-Id: <20210930113844.1829373-4-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Add a structure which describes a resilient nexthop group. It will be later used for parsing. Signed-off-by: Nikolay Aleksandrov --- ip/nh_common.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ip/nh_common.h diff --git a/ip/nh_common.h b/ip/nh_common.h new file mode 100644 index 000000000000..f747244cbcd0 --- /dev/null +++ b/ip/nh_common.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NH_COMMON_H__ +#define __NH_COMMON_H__ 1 + +struct nha_res_grp { + __u16 buckets; + __u32 idle_timer; + __u32 unbalanced_timer; + __u64 unbalanced_time; +}; + +#endif /* __NH_COMMON_H__ */ From patchwork Thu Sep 30 11:38:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12528075 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 9FD20C433EF for ; Thu, 30 Sep 2021 11:39:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 88480615E2 for ; Thu, 30 Sep 2021 11:39:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350575AbhI3LlT (ORCPT ); Thu, 30 Sep 2021 07:41:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350546AbhI3LlL (ORCPT ); Thu, 30 Sep 2021 07:41:11 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34134C06176A for ; Thu, 30 Sep 2021 04:39:29 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id dj4so21371517edb.5 for ; Thu, 30 Sep 2021 04:39:29 -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=XYvV0L1uHbDbInpFf1SHlMgNFXX7h4Ao7TwGFHRSCDs=; b=qZc13XjtQwbdfSO9dvJ/Zi+ajD/psY1uX8bTjZkQhSUKsskxFGp1kYlgwgx6P1xbm2 qMu+gZubETfwjtxTAAc6bYOQ9BGk4jXuFN++pwPBW/anGpq1FtwpExSxJ+9eL2FMuyZt R1BByBf4GMhLA4typ32F8bccvFIy3rwDH2LUWU6Q/f3qnokJii4yiWac/vhvhCWksQ9S 4D+raOkOc3QHlySSFSUwOsGaXs6QsjObPmx8hAZof006wxeOLrstwlTCM8PaMsDQf1ed fwgFFthSwGsZX4MO0JB/VeXM62zZDdoSONqaKiziWRoVnCnOufMYfm8X1curjaisGp9M gL9Q== 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=XYvV0L1uHbDbInpFf1SHlMgNFXX7h4Ao7TwGFHRSCDs=; b=cYwPPkHDJROT21J7pDpCGSNhSPWqCeRj0ke3Z4Wfd5Wgovs+eV7UCYObysGyG96wQS 1cr0N4jlCvAhQ3/z9mZLFg1GEoW+pekzMxX0j0GUEG1BRWvYA0e3LM5lZxSPeRaDOdBB sbf/XkN05sncXP0DcCBT05xqV8d46m+huhWnBCTBmTyZmUttl9sgISwhCM/6JJGLivOF wxb8+jyQb0JLq4i80bYMWMfAXv+OtBh6/DNk2FiHcyfRM/9LE0hFUv2AsuvR1n2kfx3w Q+QCXW2C8q1aB2rcoZrlqIrnZplBrNT+gp/m19sJjJS5N0zzu5V6EmdOTvBfDIoqvtZM V9VQ== X-Gm-Message-State: AOAM5306VUj09wj4a0B7mpNwUAOGPkeFWnhC9RS1C5rq8kjr+KFnwq4n 0GzMcrlJZz33gL3+LWyb9SHqPHyjlRUhzzHm X-Google-Smtp-Source: ABdhPJxt+9q54wqdGfEtcuw41Q06Db3nQzdS/t4rOxZr/7mFzk+YI1NiIQAkJMJPuz4b1z7T1lJrFw== X-Received: by 2002:a17:907:e91:: with SMTP id ho17mr6259205ejc.287.1633001967450; Thu, 30 Sep 2021 04:39:27 -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 b27sm1277704ejq.34.2021.09.30.04.39.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:27 -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: [PATCH iproute2-next 04/12] ip: nexthop: split print_nh_res_group into parse and print parts Date: Thu, 30 Sep 2021 14:38:36 +0300 Message-Id: <20210930113844.1829373-5-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Now that we have resilient group structure split print_nh_res_group into a parse and print functions, print_nexthop calls the parse function first to parse the attributes into the structure and then uses the print function to print the parsed structure. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index a4048d803325..7094d6cbb5e6 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; @@ -264,39 +265,50 @@ 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 print_nh_res_group(FILE *fp, const struct rtattr *res_grp_attr) +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; - struct timeval tv; + memset(res_grp, 0, sizeof(*res_grp)); 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])); + 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]; - __jiffies_to_tv(&tv, rta_getattr_u32(rta)); - print_tv(PRINT_ANY, "idle_timer", "idle_timer %g ", &tv); + res_grp->idle_timer = rta_getattr_u32(rta); } 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); + res_grp->unbalanced_timer = rta_getattr_u32(rta); } 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); + res_grp->unbalanced_time = rta_getattr_u64(rta); } +} + +static void print_nh_res_group(const struct nha_res_grp *res_grp) +{ + struct timeval tv; + + open_json_object("resilient_args"); + + print_uint(PRINT_ANY, "buckets", "buckets %u ", res_grp->buckets); + + __jiffies_to_tv(&tv, res_grp->idle_timer); + print_tv(PRINT_ANY, "idle_timer", "idle_timer %g ", &tv); + + __jiffies_to_tv(&tv, res_grp->unbalanced_timer); + print_tv(PRINT_ANY, "unbalanced_timer", "unbalanced_timer %g ", &tv); + + __jiffies_to_tv(&tv, res_grp->unbalanced_time); + print_tv(PRINT_ANY, "unbalanced_time", "unbalanced_time %g ", &tv); close_json_object(); } @@ -371,8 +383,12 @@ int print_nexthop(struct nlmsghdr *n, void *arg) if (tb[NHA_GROUP_TYPE]) print_nh_group_type(fp, tb[NHA_GROUP_TYPE]); - if (tb[NHA_RES_GROUP]) - print_nh_res_group(fp, tb[NHA_RES_GROUP]); + if (tb[NHA_RES_GROUP]) { + struct nha_res_grp res_grp; + + parse_nh_res_group_rta(tb[NHA_RES_GROUP], &res_grp); + print_nh_res_group(&res_grp); + } if (tb[NHA_ENCAP]) lwt_print_encap(fp, tb[NHA_ENCAP_TYPE], tb[NHA_ENCAP]); From patchwork Thu Sep 30 11:38:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 12528077 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 9ACBDC433EF for ; Thu, 30 Sep 2021 11:39:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 807B0611BD for ; Thu, 30 Sep 2021 11:39:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350583AbhI3LlV (ORCPT ); Thu, 30 Sep 2021 07:41:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350553AbhI3LlM (ORCPT ); Thu, 30 Sep 2021 07:41:12 -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 16AB1C06176F for ; Thu, 30 Sep 2021 04:39:30 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id v18so20650028edc.11 for ; Thu, 30 Sep 2021 04:39:30 -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=SgkYHibwTe58Khm5JgG7/EGU8NHLAlFwGU/k53gw75U=; b=HUFSow4i9QZcwPiHcL4rNDHv+JHqAmg1gzIylgwqU4vv2uAODtObHhTOQRnkfAadbC ARSb9rKkBM5NvKx7f6rf4IM9guiu+KgaONXt9SdXMQHAPEflFAoWa2iNvIZo62UNWdIQ tny+flu9k15SclQW7ODWG3p87lZF/kWNKBKaxynO6iy1sSNE3bYLWM1y+kujoxGcGJ+x K6rc/bVQugJNeYGCsIGqZGQUpmtUA+QTD94wSPCAGvMCohmXpYaPU0eRuQj+KWQKXfMR cKxxRjdF/YjOyNvTkrye3i8s+oP+0ZeoKTDCNrop51rTo/ifwmaeD8mpqV1IlKkG9TZ3 +JFw== 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=SgkYHibwTe58Khm5JgG7/EGU8NHLAlFwGU/k53gw75U=; b=V/PXSPUoVU9mzjnVApxWG68UyBsZaNOxtDyoEPkySqHErMg8WeM0p5RpSZWLU6NbM1 vHxIymD5i+NUQu+XY6zucyFnpCtfbH7OYJP59wv+Bp0xEDUhXpRruQs56JobojzHMhQZ oUvgzLXULg3FiaiNEAwl8WfCaNpdytfW5bsaEgwBAtMgy8lGrWDDnmxARGj55t6Tpi5K TtDAMccAMyxWPxRIZI8VrYj6S+A7ZEVjZ8f2e9QiHlRe8sgiZeM8uUymuKklcXWPrgqL I049eaSXUE2C9hN+MFzJye1wW/gII3xEyQKzw0UvOv0cltGaSMmNUl4KK+V6/rgN2e1A N25A== X-Gm-Message-State: AOAM5335VOgHRLK6FiN4N2ckWLfcaVA2EG2kNY+S4Gi6Idds9coWuRxH uRMa7QgnATsBDzrM7puLJlGZuba4XzV1KCug X-Google-Smtp-Source: ABdhPJz+ccfx01qIcvHnFD4TeH4EtMPThEMuGXnqzwiImaFaRTLI+02L3DVpAHCFmkVAq2rVQ8dVLg== X-Received: by 2002:a05:6402:19ad:: with SMTP id o13mr6624888edz.109.1633001968389; Thu, 30 Sep 2021 04:39:28 -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 b27sm1277704ejq.34.2021.09.30.04.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:28 -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: [PATCH iproute2-next 05/12] ip: nexthop: add nh entry structure Date: Thu, 30 Sep 2021 14:38:37 +0300 Message-Id: <20210930113844.1829373-6-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Add a structure which describes a nexthop, it will be later used to parse, print and cache nexthops. Signed-off-by: Nikolay Aleksandrov --- ip/nh_common.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ip/nh_common.h b/ip/nh_common.h index f747244cbcd0..d9730f45c6fb 100644 --- a/ip/nh_common.h +++ b/ip/nh_common.h @@ -9,4 +9,35 @@ struct nha_res_grp { __u64 unbalanced_time; }; +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; + + bool nh_has_res_grp; + struct nha_res_grp nh_res_grp; + + int nh_groups_cnt; + struct nexthop_grp *nh_groups; +}; + #endif /* __NH_COMMON_H__ */ From patchwork Thu Sep 30 11:38: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: 12528081 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 C8688C433FE for ; Thu, 30 Sep 2021 11:39:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B2014611BD for ; Thu, 30 Sep 2021 11:39:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350586AbhI3LlW (ORCPT ); Thu, 30 Sep 2021 07:41:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350531AbhI3LlN (ORCPT ); Thu, 30 Sep 2021 07:41:13 -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 0173BC06176A for ; Thu, 30 Sep 2021 04:39:31 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id y35so20936680ede.3 for ; Thu, 30 Sep 2021 04:39:30 -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=iuVLJJIvg962GHVOqJeAReROnxYdjRumeXo15IjKdno=; b=LgKaPTP27D00VTclH0FPPbGS4E2XAWD74DvM2C49StgZ+PokmhYuVoU2TLViB9ZAip lL2FGlgduEtcZlZ4lUwHgkjCVkjKXJAJzciH4eXjHwgq7o3lWt5jU3YUeli5iklBDMqb HG6JylxrZ+Tn9gHglm/jX+twB+FrPtTZK9/1kex9zPPoPLIREDn70HaYEtP36Vv+Iu2D rusfq8W3W24+95apLB6H8n7M/0UsbqeUHk7DBqJpxy3s3MCcdTqEDYJ33k+WMOlFGz4t HbcTsJkgiUgXZClMLBoDEcZrlCTPLRHroDzGPBTdGG1bVBBRj6BUihmEG4YbcR+zHCVv bj/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=iuVLJJIvg962GHVOqJeAReROnxYdjRumeXo15IjKdno=; b=1JsBlig2c7BUn/O7jug8rtQ/Ngg1A4q4ZzPSoUoqOsx4dMqpbxXPy7szZ5xvNjEAWD qrVUbfd+KzAnyPRHrcu7jSH/CM0yOSJHoWOytjU7GcGIX2mZxr/WwZ0Ia8pYJ88VeXDg CEr58t2/r3MwGPOsuQHaPdOeMsP39ey1kLRDMQd9FoJknXoAnIC2nC4KL6jZFND+ZWto ATND+IZmcRyqRjjCkUo0GCRd/ZwrKnlbE2dDmyj75N1le7UOKWvbNqSU1Y39rH+alGv5 rgRozxikzFqsiL3+ccsZeEGoK7DnNboz1mZlA9xPz2jwJ+t7k4x+2fzXjZE2cPl/RZB8 zUhw== X-Gm-Message-State: AOAM5305NkA+LDWHRBAIrL0Btt78+fn6F6ugHDbgeZKCUKz2RkkT8kx0 ZiGjwHBteDnN/Uu/KmkDNhNCrhE0sukPXo3q X-Google-Smtp-Source: ABdhPJxe3Hp2gnQOhCeYgmZP8yfF5P08KtmKX6ALqp+iIrDSHpv0u4dFwOcG1cPYECL+U1twdksOYA== X-Received: by 2002:a17:906:4ac3:: with SMTP id u3mr1824936ejt.72.1633001969320; Thu, 30 Sep 2021 04:39:29 -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 b27sm1277704ejq.34.2021.09.30.04.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:28 -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: [PATCH iproute2-next 06/12] ip: nexthop: parse attributes into nh entry structure before printing Date: Thu, 30 Sep 2021 14:38:38 +0300 Message-Id: <20210930113844.1829373-7-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Factor out the nexthop attribute parsing and parse attributes into a nexthop entry structure which is then used to print. Signed-off-by: Nikolay Aleksandrov --- ip/ipnexthop.c | 186 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 139 insertions(+), 47 deletions(-) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 7094d6cbb5e6..0edb3c265b6f 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -213,28 +213,29 @@ out: return rc; } -static void print_nh_group(FILE *fp, const struct rtattr *grps_attr) +static bool __valid_nh_group_attr(const struct rtattr *g_attr) { - struct nexthop_grp *nhg = RTA_DATA(grps_attr); - int num = RTA_PAYLOAD(grps_attr) / sizeof(*nhg); - int i; + int num = RTA_PAYLOAD(g_attr) / sizeof(struct nexthop_grp); - if (!num || num * sizeof(*nhg) != RTA_PAYLOAD(grps_attr)) { - fprintf(fp, ""); - return; - } + return num && num * sizeof(struct nexthop_grp) == RTA_PAYLOAD(g_attr); +} + +static void print_nh_group(const struct nh_entry *nhe) +{ + int i; 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(); } @@ -254,15 +255,13 @@ 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, @@ -340,12 +339,104 @@ 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])); + } + + 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]; + + 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); - struct rtattr *tb[NHA_MAX+1]; FILE *fp = (FILE *)arg; - int len; + struct nh_entry nhe; + int len, err; SPRINT_BUF(b1); @@ -366,60 +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 = ipnh_parse_nhmsg(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]) { - struct nha_res_grp res_grp; + if (nhe.nh_has_res_grp) + print_nh_res_group(&nhe.nh_res_grp); - parse_nh_res_group_rta(tb[NHA_RES_GROUP], &res_grp); - print_nh_res_group(&res_grp); - } + if (nhe.nh_encap) + lwt_print_encap(fp, &nhe.nh_encap_type.rta, nhe.nh_encap); - if (tb[NHA_ENCAP]) - lwt_print_encap(fp, tb[NHA_ENCAP_TYPE], tb[NHA_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 (tb[NHA_GATEWAY]) - print_rta_gateway(fp, nhm->nh_family, tb[NHA_GATEWAY]); + if (nhe.nh_oif) + print_rta_ifidx(fp, nhe.nh_oif, "dev"); - if (tb[NHA_OIF]) - print_rta_ifidx(fp, rta_getattr_u32(tb[NHA_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); + ipnh_destroy_entry(&nhe); return 0; } From patchwork Thu Sep 30 11:38: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: 12528079 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 42B13C433F5 for ; Thu, 30 Sep 2021 11:39:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2781361555 for ; Thu, 30 Sep 2021 11:39:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350593AbhI3LlY (ORCPT ); Thu, 30 Sep 2021 07:41:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350557AbhI3LlO (ORCPT ); Thu, 30 Sep 2021 07:41:14 -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 D901EC06176A for ; Thu, 30 Sep 2021 04:39:31 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id b26so21119559edt.0 for ; Thu, 30 Sep 2021 04:39:31 -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=vI94Rz9w3cwEX8AnqfIcA0ZYpgtmdoARIPrkq5Dkh3c=; b=IiWjTqyxmf04XtloLnhZdOhdo6OiWEPYzQ7cYvy9sDLkRE+gw7mDMbMR8qOFZFC6Jp GxyUjXqmUJyJ4kYlle9wVg8AuYk1E1Q0s0RWE6BjLSVsEwCBtDTeQGkHGbdfbT+JH8cT GrGUoOGIEuHEQalKApFvy3jDM/4z0jS9bPGZxPFVN6rA4Xe+YnLkPbaNxEFba+qFLYPZ p+SIF7VSlEKYPkS9n/SbEnvlwekRFT/ifRVRfFvJBMTk2HpYxSXE1B1DALA+uIlyh4Ml /BvF1YbU8ZB47sEgG9Jl61R0+8bpz6wtVbDmiOxq1MFcY9CgEuivTGU52NwBw5zlOVQO WW/g== 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=vI94Rz9w3cwEX8AnqfIcA0ZYpgtmdoARIPrkq5Dkh3c=; b=y/TnmPb/zudEUhM/m/3TDhwXZYtHbWP9kM4FvMjF+ksK9lkRzCHUn38k2OyhQOjWhf bGcrLFXd2uLjhTwUzCGLJ+r254jrfkLiFE8C7gowztYFPCY45oFMesJ3azncSKww4ZEV m5qB0ZCAi1mOpmW858jtczcqc1WdZit/le/SKVuYO6GdSm5vUE/zFDPunEx8vTawFsEY hCyLQx5ITR7doN/VgaXd+KWO73NfqGttxXbh1hdTRxRSW6XsSNK9mGbkWoLDW1B4k4da k1yQHOqSA+7uzH/bB2677De5RYzmlYfhXpjYSGTDJmhpqNTs/9R0LbOtGGNNHTrEZJFk RWww== X-Gm-Message-State: AOAM531yUPet7nPoa7dVrm2Qvu9QnhZGPOEqx2/23+hE5Rur8oRz7tnf pIV5Em5KzsBnj+At1pJMKvpwk5Jzj2zV0ZtA X-Google-Smtp-Source: ABdhPJxeeBIJI/UiGqgKfvGZuAjeybMxDAXfs3Bc5+SRs6QNnmF0yUjekPt+DOtr/iA4397DrEGSXg== X-Received: by 2002:a17:906:7ce:: with SMTP id m14mr84157ejc.192.1633001970222; Thu, 30 Sep 2021 04:39:30 -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 b27sm1277704ejq.34.2021.09.30.04.39.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:29 -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: [PATCH iproute2-next 07/12] ip: nexthop: factor out print_nexthop's nh entry printing Date: Thu, 30 Sep 2021 14:38:39 +0300 Message-Id: <20210930113844.1829373-8-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Factor out nexthop entry structure printing from print_nexthop, effectively splitting it into parse and print parts. 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 0edb3c265b6f..a589febca605 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -431,6 +431,60 @@ out_err: return err; } +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); @@ -438,8 +492,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", @@ -463,53 +515,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 Thu Sep 30 11:38: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: 12528083 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 06B3BC433EF for ; Thu, 30 Sep 2021 11:39:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E07FD615E2 for ; Thu, 30 Sep 2021 11:39:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350550AbhI3Ll2 (ORCPT ); Thu, 30 Sep 2021 07:41:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350546AbhI3LlT (ORCPT ); Thu, 30 Sep 2021 07:41:19 -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 D73F3C061770 for ; Thu, 30 Sep 2021 04:39:32 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id s17so20661361edd.8 for ; Thu, 30 Sep 2021 04:39:32 -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=9GUl3vBWCG+v9Gc/Z+HIfOOWjGV7nOvafJfxxKjlTWA=; b=Gfkz6ee/6XkEgg2ig3bdsAWBeNcIjUwwTOI8iP7l8x1c4AhsKycwbBNmGtFf7TamJO vApvCJQf9j154A5llvraU948tZhu26miQBvidMv9jMaVCcQQiB1CU/l8z9EnZI0WdStO L1P7XrMdXWvl65a3UCfcNEsff2g5wkK8r8eIVV7lbR/ywbcnZ1TjjK34yXQJ9cFM8FYs KQVxwzZN6VtytA4CZYVY34OIjpCW9MGQ335Qn1fuKvu7Bm6T3JMDOUmbX+UziA4PFBA/ Gu1R4fOghdJyqVJGqHuNGnsFNpnlfCV9euted1//0gCPV+Bup4uDDU2iFd2JvCojog+4 LDHA== 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=9GUl3vBWCG+v9Gc/Z+HIfOOWjGV7nOvafJfxxKjlTWA=; b=kN3nMPKLbLQJTlU5gYL/GtKbA3C7A/eRzwIdVjNvFEbR/V8zu5SmeEUq+75rlPEfvf Ne0v5wRRRoGvFhfUIEfG5TDGnWGR1WY6Fin56iOdNSI1zPjjGzgcMztVMku3C7SvF5XF 2cDlOpTvCvJjMdgj/7aZoNKhp4fTik8kAm/4wgmx5eX9S40fcFquAveqXE0+rz03GgVs cPDH8qS9NFxwdt2R738SYVStVrpIj1bw6xNorehSRwkmP+E8nHlDLcsUctd5LIWxKk76 kSoWq4ve6xD4sjW3qDZINPQohOyrnKLKmGaRPpKR5xV6EdVdOwa/mBVUE+GagZJUzE/4 6tNw== X-Gm-Message-State: AOAM531CITBuDfAOb7XzAqAaOfY2hNDJcket+AS92h9VfOJrqYADxBYw w/BbK0T8zAvw2lWBL/G/KOHzmdZQHbJy8hsH X-Google-Smtp-Source: ABdhPJxq7Dn/gzmOk0GjmzxanVdl58zr9WLDyfFDWjNuthXhV9zgEYkF2w1zX+vIfp4GZZH0MYqACw== X-Received: by 2002:a17:906:7811:: with SMTP id u17mr6034720ejm.562.1633001971183; Thu, 30 Sep 2021 04:39:31 -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 b27sm1277704ejq.34.2021.09.30.04.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:30 -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: [PATCH iproute2-next 08/12] ip: nexthop: factor out ipnh_get_id rtnl talk into a helper Date: Thu, 30 Sep 2021 14:38:40 +0300 Message-Id: <20210930113844.1829373-9-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 From: Nikolay Aleksandrov Factor out 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 a589febca605..454c7416e30f 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -485,6 +485,25 @@ static void __print_nexthop_entry(FILE *fp, const char *jsobj, close_json_object(); } +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); @@ -827,21 +846,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 Thu Sep 30 11:38: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: 12528085 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 75076C433FE for ; Thu, 30 Sep 2021 11:39:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 59FAF611BD for ; Thu, 30 Sep 2021 11:39:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350595AbhI3Ll3 (ORCPT ); Thu, 30 Sep 2021 07:41:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350581AbhI3LlT (ORCPT ); Thu, 30 Sep 2021 07:41:19 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1037C061775 for ; Thu, 30 Sep 2021 04:39:33 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id g7so20815984edv.1 for ; Thu, 30 Sep 2021 04:39:33 -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=k1RmNcVoX+kLSBgaMA/S8z2XYkL/ljTubxIHEdK9yg0=; b=1xU8VUvg2r28WWiPPxFf46ucRcsPrLHxwQ34kr+zOmaFJb3BYAycqzsZN34jJaq1g2 7GRVg6eoR7CO2E77NtQbOO6xB4hLGD6is1NGi9lZ/6OPdLEaRG3/2Y3cndgjkDKgVHfs N8axEuoanQvknxI9mye4Lr5qk+//9wbazd9xnEr+wywxtvo98SaNeGWO8/8EvMlG8cH3 U9JzV2414LrMQ0EfzB/vD+IsWtrgQm1lZYq+58e6qukYsvDEqrtebnsiJ5mubhkJcdQe 6cQB9yT8QqJxIHey4e09jbi5eHggaQpQieJRgpnHMiAHNtCP+TFuOkp698KI1LeouHlc 8Evw== 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=k1RmNcVoX+kLSBgaMA/S8z2XYkL/ljTubxIHEdK9yg0=; b=qRGyvJvDS7CUVARfk2hOjXvw0em7NBaoRC/9A3HDgGrxWrnCgrqPDbZZarQ8vZWyGc nKQlaOwbSL2gZkQPV/74+cdPutYTw7wyY2QU8xqCnAYKO5g18vuqj33VUjsfqaRA4V53 CfO9S8sFuiNTpIlwydYaSJYKayQJFCbqDldJ91HpT5bre5m2XQ1PJftC9oHSP2Wsrtec a5DEwuUQTX/T6Pq6AhAQVJA6rKB4VpPITcITPSqZFgi7OYHvr6e1cK07ltY2XPwiMiSP 5Kg34lGqwuY3kT8CoGxMYvsqCp9/D7vQwUtjZAfkIQdfF6otJZJgDT0QHJzXYwGj+UYg 58og== X-Gm-Message-State: AOAM531xiIprjQw25QQCvQwhtB5ZU5I1kC0YJ0s2oSlvXh/2fgXYNAJY 0msvbgvm46/wC1fr7nyEILWgIAz5QKnsGHjb X-Google-Smtp-Source: ABdhPJw3MQ3g4brwksZDNiBu7UJsb6K3jtcdRzg//+oh/RCWL5xSn7IS22+/QwS1f3r5n3uoxHq1yg== X-Received: by 2002:a17:907:d23:: with SMTP id gn35mr6338919ejc.556.1633001972020; Thu, 30 Sep 2021 04:39:32 -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 b27sm1277704ejq.34.2021.09.30.04.39.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:31 -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: [PATCH iproute2-next 09/12] ip: nexthop: add cache helpers Date: Thu, 30 Sep 2021 14:38:41 +0300 Message-Id: <20210930113844.1829373-10-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ ip/nh_common.h | 6 ++++ 2 files changed, 104 insertions(+) diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c index 454c7416e30f..e0f0f78460c9 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) @@ -504,6 +506,102 @@ static int __ipnh_get_id(struct rtnl_handle *rthp, __u32 nh_id, return rtnl_talk(rthp, &req.n, answer); } +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; +} + +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); diff --git a/ip/nh_common.h b/ip/nh_common.h index d9730f45c6fb..ee84d968d8dd 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 Thu Sep 30 11:38: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: 12528087 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 C309FC433EF for ; Thu, 30 Sep 2021 11:39:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4D91611BD for ; Thu, 30 Sep 2021 11:39:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350624AbhI3Llb (ORCPT ); Thu, 30 Sep 2021 07:41:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350526AbhI3Ll0 (ORCPT ); Thu, 30 Sep 2021 07:41:26 -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 A9828C061776 for ; Thu, 30 Sep 2021 04:39:34 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id r18so20711985edv.12 for ; Thu, 30 Sep 2021 04:39:34 -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=3oPkwLF07ucODw09XipvnMRYPpppZlPu8H5ouu9mekQ=; b=fbKvs8whjINoqUBfIPh/BawFh9MHAZD6GYmMZL8+PGOZv4yXBWEoasphsS5VMiiFjG IoRNtSCkLiHWo6Gxg/II+azKA5F4H+jr0WZ0mkD01G1WJlEBvpbH8+vtm+Uu2FdcYJJC M5KpgjkgoXT6dkhB9DgUOLl5gpfb+eKf1+m1opEBAq7YS9plWp4vyvD7Wo1dsc3+xMbR LR+Ryn+ODKWLJtpsiCd0FsUY90V1tZiVud5O+SHTWS6BT5km43NwjfDh6mHgE9Hd9Nte WvM38F/UQCE/l1t7Xo8gtHA4bBfAoGTRm606Yv5S1PXm7SazQo0TVEv2zL3vGn71YtVE x8pQ== 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=3oPkwLF07ucODw09XipvnMRYPpppZlPu8H5ouu9mekQ=; b=Eoa1SB1l0igqs8byzBq7rWWAQu/odlab4fwrZ/nvXpMIZz6HXvO52f9TPXNDJ3s4Pi DW0db5vGK3cPIW5H/9qBlEjG604NIt8IyfRpJWrTRbdgqDc/7hignVpvS/H3OwmjDnIB Zl4pmloc/r4WN/zvPtupidUSwrKn3ghVC9bFGn8SthQghNHOCZWHnBFrUVzdzoYGmbdS tfcd3HHdj1rWtPxzDdcdeIZTyrrH6HhoUIMxEYZsNEVlN9nUpf4njJiA0yxOg4NyCW8v BsxSfy3thc0JhePxegApvPnamWM30FTKnGP5kVUe31VKuGSzy3Z5X/hPrJ3uIA3MvYNE 2ByQ== X-Gm-Message-State: AOAM532kiSCIkDkhn2ZYcgbqutIVkMEWoakTNL8WZNEW979EHw96MSo7 x3c9ibOaLMKCuQ+1wa0/vW6+3P7K9BFbF0fL X-Google-Smtp-Source: ABdhPJy2mErOZde4ivAYTtUaMMZ+2HaFajWz1IHObjAG0fb1TREHyqWbsTxax+yrp05ueIYSO/polA== X-Received: by 2002:a17:906:8510:: with SMTP id i16mr6242087ejx.442.1633001972917; Thu, 30 Sep 2021 04:39:32 -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 b27sm1277704ejq.34.2021.09.30.04.39.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:32 -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: [PATCH iproute2-next 10/12] ip: nexthop: add a helper which retrieves and prints cached nh entry Date: Thu, 30 Sep 2021 14:38:42 +0300 Message-Id: <20210930113844.1829373-11-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 e0f0f78460c9..31462c57d299 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -602,6 +602,22 @@ static void ipnh_cache_del(struct nh_entry *nhe) free(nhe); } +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(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); diff --git a/ip/nh_common.h b/ip/nh_common.h index ee84d968d8dd..b448f1b5530b 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 Thu Sep 30 11:38: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: 12528089 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 A4774C433F5 for ; Thu, 30 Sep 2021 11:39:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FCF2611BD for ; Thu, 30 Sep 2021 11:39:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350628AbhI3Lld (ORCPT ); Thu, 30 Sep 2021 07:41:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350594AbhI3Ll0 (ORCPT ); Thu, 30 Sep 2021 07:41:26 -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 87995C061777 for ; Thu, 30 Sep 2021 04:39:35 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id l8so21221345edw.2 for ; Thu, 30 Sep 2021 04:39:35 -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=hz3Jady3OVGlfHpvZHNUoiZmWgUBHYUqZoRmjXAQBRz+wYkm9+sej2tbdZ2Ejm0Pho NRS9KwvKcICtV1kUCxth0Qg1qlAm4z3y1SpImbFfmeGnhz6sd1vDblcYdoXD/VZnAAH1 8RQ1AKd4FT9XUB+xNxOBm4DJauo4LD/sqnB6gfeSdXgY5qehVje1MgVQdAv5YU1hmaBd Ura74OmAc0gnRp3MYXZGcqowuvXwvSWVGnMDZOJcv7f6kzHjxv23kziQZ6Bbd20zn8P5 H57X0gdxQE3qnMd4bedKJbuB3pISBWFCbQgV6MR+hrOT+SzJlNnC4h8JceG8QMswtqOP lZOA== 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=boXS/2tzmJOpwFWh4r4TdRcNeFU79cLsZcoq+SyXy+bijNS+LkdWo3Nm/dkZADdKO5 ZfJq7S4AblhLQtMtkeMCFRGmXSEe6bM0cZR88tvb6boTCBnDWYKzeMo7Tqxe8oNMhRzS Fc+6tSbvfUgF4N7nKlkwLvezLVDszVXIU1iIm+SvbfE3uq1L3G8KVr/pRI9Xr4y8WJF+ gXDqJ2oTBL4SQQrw3XfjYOeg507e1biQf+0evwhzMemQPswOj+Jsi2fQFgcDgs5KZpfN q3iKOIt29Q37bUaQAx55IvsceIYcStbwsyNII0nXwb5diuOSYbhOl1+jI/0sQWDjxKyM a36A== X-Gm-Message-State: AOAM532hB+BNPKDWh8eoCDYANPVSjnMblLN3Vr/hhumP2xwKoTSwa8lH YqZvmF34wgeZP21TCus65U9gsQvVUWHY/JqB X-Google-Smtp-Source: ABdhPJyuM4wQCjR8ZV4BKAc6IZalZWX1KKXL1+e8TsuMULpNDLvnRxtRsrnVTfChVGCBRvp+7GYSpA== X-Received: by 2002:a05:6402:5206:: with SMTP id s6mr6629156edd.135.1633001973765; Thu, 30 Sep 2021 04:39:33 -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 b27sm1277704ejq.34.2021.09.30.04.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:33 -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: [PATCH iproute2-next 11/12] ip: route: print and cache detailed nexthop information when requested Date: Thu, 30 Sep 2021 14:38:43 +0300 Message-Id: <20210930113844.1829373-12-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 Thu Sep 30 11:38: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: 12528091 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 D2926C433EF for ; Thu, 30 Sep 2021 11:39:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BD5F5615E2 for ; Thu, 30 Sep 2021 11:39:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350633AbhI3Lle (ORCPT ); Thu, 30 Sep 2021 07:41:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350565AbhI3Ll1 (ORCPT ); Thu, 30 Sep 2021 07:41:27 -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 527E1C06177B for ; Thu, 30 Sep 2021 04:39:36 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id bd28so20768634edb.9 for ; Thu, 30 Sep 2021 04:39:36 -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=wJO5oPftinL12JBvf/5XpcqwmvPfZ919ITvh8uqRXRk=; b=2Y/zjZWSDfT0/f58BorM2T5rijLq9g66eO2SSTtiiI6+ifmF/cy7Bmy7F8bWvb+aqg d9rksMx6cWJi5umhik442WC5gLgcXhF+Thfa0BxDVA5SD+HzkoyCuoFpGgxjJanbRqCz ITSeHEl/5r+x4vtft2Y1EWgtwasLAeOnqDLSYz4sQzVajYQvD5TlxpqhwuqQcLnxSsZT ieLaY2WjMVhbZu7c//E473OADJXondlmIQxzk2989wI/pajdj/UruA2VZaQl+74+HZ9F KjRLlm8hx3LOC2xE7YBJGzJbYyoIK3aja8d1aEJphv7cLmVq0iM0l2bdLap5eR6gWyri 0BbA== 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=wJO5oPftinL12JBvf/5XpcqwmvPfZ919ITvh8uqRXRk=; b=TAdAYIBt75g8sPfHid6VqUwtNf3/35P9wUncW55QQB144ip3IwwC5MN/f8cwukXS/H OVmwHN4QSNwEzJNSMevhA0ambbiQuzZmRBZnKrjlnhqgw8wgz+fUEN7ijpyR8W+88K6D U4AAm0roSVAgHOsqQIhg8umHzgbG+t8DV2HurZANHVfOCjKCKPo1Jsho5WOZvvm0h+Oi sUmF9g2fLBFGx/X/Obmi+Z3TtZMREpUy/U0mjC8jAWGTQ6q4VyfarRkKdlOOSRcOEGSW qflaMWNq8xoVDBBBBlCCOK/1LbwmdaH5c9phDuMke5zmXn8cwafYnAYYPIorWuTpcCDg 1wxw== X-Gm-Message-State: AOAM530AtrnLVIJj+0cZOjNEyd65/USVW0t3kLPz3ISBoob+6kjhbhr/ faJNlCQJzeQsKW40HzPCLvP1pX2b31KnF5TK X-Google-Smtp-Source: ABdhPJxa84K3hTGg/sVs9coQDZ97IgBtlgH9Omk/GSGWTEWRlEk1yBXjbhzeF8ttZLIfgSSb7hzNIA== X-Received: by 2002:a50:da48:: with SMTP id a8mr6310954edk.155.1633001974606; Thu, 30 Sep 2021 04:39:34 -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 b27sm1277704ejq.34.2021.09.30.04.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 04:39:34 -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: [PATCH iproute2-next 12/12] ip: nexthop: add print_cache_nexthop which prints and manages the nh cache Date: Thu, 30 Sep 2021 14:38:44 +0300 Message-Id: <20210930113844.1829373-13-razor@blackwall.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210930113844.1829373-1-razor@blackwall.org> References: <20210930113844.1829373-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 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 31462c57d299..b4d44a86429c 100644 --- a/ip/ipnexthop.c +++ b/ip/ipnexthop.c @@ -602,6 +602,42 @@ static void ipnh_cache_del(struct nh_entry *nhe) free(nhe); } +/* 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; +} + void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, __u32 nh_id) { @@ -618,7 +654,7 @@ void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj, __print_nexthop_entry(fp, jsobj, nhe, false); } -int print_nexthop(struct nlmsghdr *n, void *arg) +int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache) { struct nhmsg *nhm = NLMSG_DATA(n); FILE *fp = (FILE *)arg; @@ -651,11 +687,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); +} + int print_nexthop_bucket(struct nlmsghdr *n, void *arg) { struct nhmsg *nhm = NLMSG_DATA(n); @@ -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 b448f1b5530b..4d6677e62e33 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__ */