From patchwork Tue Nov 7 08:06:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448023 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBD54D30A for ; Tue, 7 Nov 2023 08:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="O7Q5dbNY" 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 A492BE8 for ; Tue, 7 Nov 2023 00:06:11 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-53db360294fso9174874a12.3 for ; Tue, 07 Nov 2023 00:06:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344370; x=1699949170; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EFfpucdTMFOCUhrnRBpfZGlldmu79VgbOj+QIwjR2pA=; b=O7Q5dbNYderP6+b0qcHXB7UTBEl2t1G8F86SNiOmgYJ7ZIo8BHikpPuNGsoM3OuBhX Bla3hg59FT73uiNwkM8OjhIbyOwcrs8izP3CuiVlHIzR0dpWfcA+zv3KTqHxLk75oV4W 9o+MNy9z6JNtfSD3jQ+S8KzSkmq0e8v04etMRxoIs8ym2PZET6CtkiMxUGyXA0k59N/d bybqe9uvLkjOTTqDA4WzHTgjZ7hFXVffH+aCAQ2N9Xn5pBwHlmzM7WLYqvoFiLb6aQw8 pD+0nIWMYIM5CiKqJGFSdTnPumk5+C7pHaNUodNc8rxRrXV/pwBxNjnnLs3ttthpbe0Y eE+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344370; x=1699949170; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EFfpucdTMFOCUhrnRBpfZGlldmu79VgbOj+QIwjR2pA=; b=CW/2XKYQwESxZCZjSSLa23Xw6TBmSXJ9jasbHRVgkMRcJOWMffPP6jWgJtVV/HRd6d EchJO3GMqRQCJcEcHDjSeQo6h2xc8o1V23ASTSgXsmGJwJEpWzMQLAFGnvHSBYxIzsdo MzsTx2HiiXDEi4trEaDOMPZZs5ZwCSxMeGbTNosi/UNAmYFpqA5BWO7xrJJX/F80l/aG dx30b2jkfPjfwaVvH9ko/znGG/40xOs1z80v3HiutoUTgnI6m94sBGM2pGqmqFfWrzfr cq+DnnyZNjV1jq7DfydLOcCDRiyY1gCzArqJJt3YNeAucfNsqxbxivH8C6LvNvXNqyrr Ep9A== X-Gm-Message-State: AOJu0YxJWIHaUMhJlPWWBHAA35dt8t5Z3Dumbnj6X1p0bAOxlsezOntE +GthvJYfIODZCOnjA4XVhaz7uOSc20MtQy3ohMA= X-Google-Smtp-Source: AGHT+IEC8Vd0FU5ay8COyjfOpVTOfK1irm9ToJ2tiS2WMjEGR04VZPqiRMFUcVaAfXUXVdADRnYuOw== X-Received: by 2002:a50:d693:0:b0:543:5b0b:8902 with SMTP id r19-20020a50d693000000b005435b0b8902mr19070175edi.24.1699344370024; Tue, 07 Nov 2023 00:06:10 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id q13-20020aa7d44d000000b00542df4a03d0sm5067764edr.16.2023.11.07.00.06.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:09 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 1/7] ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c Date: Tue, 7 Nov 2023 09:06:01 +0100 Message-ID: <20231107080607.190414-2-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko In order to be able to reuse get_netnsid_from_name() function outside of ip code, move the internals to lib/namespace.c to a new function called netns_id_from_name(). Signed-off-by: Jiri Pirko --- v4->v5: - added forgotten libnetlink.h include to silence dcb warning v3->v4: - removed namespace.h include v2->v3: - s/netns_netnsid_from_name/netns_id_from_name/ v1->v2: - new patch --- include/namespace.h | 3 +++ ip/ipnetns.c | 45 +---------------------------------------- lib/namespace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/include/namespace.h b/include/namespace.h index e47f9b5d49d1..2843f4bb5742 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifndef NETNS_RUN_DIR #define NETNS_RUN_DIR "/var/run/netns" @@ -58,4 +59,6 @@ struct netns_func { void *arg; }; +int netns_id_from_name(struct rtnl_handle *rtnl, const char *name); + #endif /* __NAMESPACE_H__ */ diff --git a/ip/ipnetns.c b/ip/ipnetns.c index 9d996832aef8..0ae46a874a0c 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -105,52 +105,9 @@ static int ipnetns_have_nsid(void) int get_netnsid_from_name(const char *name) { - struct { - struct nlmsghdr n; - struct rtgenmsg g; - char buf[1024]; - } req = { - .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), - .n.nlmsg_flags = NLM_F_REQUEST, - .n.nlmsg_type = RTM_GETNSID, - .g.rtgen_family = AF_UNSPEC, - }; - struct nlmsghdr *answer; - struct rtattr *tb[NETNSA_MAX + 1]; - struct rtgenmsg *rthdr; - int len, fd, ret = -1; - netns_nsid_socket_init(); - fd = netns_get_fd(name); - if (fd < 0) - return fd; - - addattr32(&req.n, 1024, NETNSA_FD, fd); - if (rtnl_talk(&rtnsh, &req.n, &answer) < 0) { - close(fd); - return -2; - } - close(fd); - - /* Validate message and parse attributes */ - if (answer->nlmsg_type == NLMSG_ERROR) - goto out; - - rthdr = NLMSG_DATA(answer); - len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr)); - if (len < 0) - goto out; - - parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len); - - if (tb[NETNSA_NSID]) { - ret = rta_getattr_s32(tb[NETNSA_NSID]); - } - -out: - free(answer); - return ret; + return netns_id_from_name(&rtnsh, name); } struct nsid_cache { diff --git a/lib/namespace.c b/lib/namespace.c index 1202fa85f97d..f03f4bbabceb 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -7,9 +7,11 @@ #include #include #include +#include #include "utils.h" #include "namespace.h" +#include "libnetlink.h" static void bind_etc(const char *name) { @@ -139,3 +141,50 @@ int netns_foreach(int (*func)(char *nsname, void *arg), void *arg) closedir(dir); return 0; } + +int netns_id_from_name(struct rtnl_handle *rtnl, const char *name) +{ + struct { + struct nlmsghdr n; + struct rtgenmsg g; + char buf[1024]; + } req = { + .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), + .n.nlmsg_flags = NLM_F_REQUEST, + .n.nlmsg_type = RTM_GETNSID, + .g.rtgen_family = AF_UNSPEC, + }; + struct nlmsghdr *answer; + struct rtattr *tb[NETNSA_MAX + 1]; + struct rtgenmsg *rthdr; + int len, fd, ret = -1; + + fd = netns_get_fd(name); + if (fd < 0) + return fd; + + addattr32(&req.n, 1024, NETNSA_FD, fd); + if (rtnl_talk(rtnl, &req.n, &answer) < 0) { + close(fd); + return -2; + } + close(fd); + + /* Validate message and parse attributes */ + if (answer->nlmsg_type == NLMSG_ERROR) + goto out; + + rthdr = NLMSG_DATA(answer); + len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr)); + if (len < 0) + goto out; + + parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len); + + if (tb[NETNSA_NSID]) + ret = rta_getattr_s32(tb[NETNSA_NSID]); + +out: + free(answer); + return ret; +} From patchwork Tue Nov 7 08:06:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448024 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D52DCDDB5 for ; Tue, 7 Nov 2023 08:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="d7Rs8sia" 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 43474114 for ; Tue, 7 Nov 2023 00:06:13 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-53df747cfe5so9181382a12.2 for ; Tue, 07 Nov 2023 00:06:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344371; x=1699949171; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rTqhdD/yxa3vJkiQbvPLc+KLXqkT95pVduQu/oE9MT0=; b=d7Rs8siaLndrLEzIEyF+3XjRKP4Ygb+ff/8WNUGyFCCrCfyTlQLd10zAEQxrvXVh9S /REmGT6DTsmk6vwSThREaQqgsVYzre7v+YOFGZ33QRHq75pM6tCMIJ3/xf1671EeA1eC oUqxJIMh4qUXdhN+hLXOMxDZOvFf1OvCHkDfs7+vOg/vGStp++mc+eXc3P4JWJ/CCCHr BRm0oPZWt1z29WIbZKD8DXw4eq6lfEEShBCwI9aqtWMV0p057DbLbGjERFzhzKMAekV+ 4jDoUDd0nL0Cc3V7BMB+HOoQWaK1gPxpP1zTBDrhfiT5mpa8RP7fw61KP1X8BhnoChma cqeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344371; x=1699949171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rTqhdD/yxa3vJkiQbvPLc+KLXqkT95pVduQu/oE9MT0=; b=EOMHo2WabT9zR/9NENcx2AU8OKWBqqgp9V/gV4pF3R5TBqovvxclhQepaN19Ue1R++ PJ8xZ1Qw4BTqzyvenMAuy3SAxt9i4M+0rzNQdKLWTOuTqxiCzEmPtr/z7hzSZddkFWXj gKvikqfrYkc8itICIcw6YGOrmj6Hnhn4dD2LgJsbCA4kqNTkV2KNCdoptz93l3Qs6IZ+ Et2Al2FYwHTRPOrl1C69uQibtY9UBEaIq5kT/P49KHAgEZg6Fwt0iyP2mA8vluMRxfRD 22xXHPzVuAlXNhY0N2o5AoZclqOl+mTdUzH1fKucfMqSdK2wrp9ER7yoCchxlzomRCgQ /LxQ== X-Gm-Message-State: AOJu0Yy7yZ/W7d95/TRZG1v7QtqzOERIcqhveM3dxH9N6irPWGjTvTbY isr7d5jKIOpNkMgd3bBdGMheUpB3KjaFX0mdmlc= X-Google-Smtp-Source: AGHT+IG0VEZZB4zTTQBPetYz02vGMps+8gRTuAcAlCjRt4p6QMJJji2BDzD7fggeDTOHezTtfKfcyQ== X-Received: by 2002:aa7:c1cb:0:b0:53e:3839:fc81 with SMTP id d11-20020aa7c1cb000000b0053e3839fc81mr23369086edp.32.1699344371665; Tue, 07 Nov 2023 00:06:11 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id g15-20020a50d0cf000000b0053e408aec8bsm5325585edf.6.2023.11.07.00.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:11 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 2/7] devlink: use snprintf instead of sprintf Date: Tue, 7 Nov 2023 09:06:02 +0100 Message-ID: <20231107080607.190414-3-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Use snprintf instead of sprintf to ensure only valid memory is printed to and the output string is properly terminated. Signed-off-by: Jiri Pirko --- v3->v4: - new patch --- devlink/devlink.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 3baad355759e..b711e92caaba 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -2761,8 +2761,9 @@ static void pr_out_nested_handle(struct nlattr *nla_nested_dl) !tb[DEVLINK_ATTR_DEV_NAME]) return; - sprintf(buf, "%s/%s", mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]), - mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME])); + snprintf(buf, sizeof(buf), "%s/%s", + mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]), + mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME])); print_string(PRINT_ANY, "nested_devlink", " nested_devlink %s", buf); } @@ -2773,7 +2774,7 @@ static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb, const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); char buf[64]; - sprintf(buf, "%s/%s", bus_name, dev_name); + snprintf(buf, sizeof(buf), "%s/%s", bus_name, dev_name); if (dl->json_output) { if (array) { @@ -2832,7 +2833,7 @@ static void pr_out_selftests_handle_start(struct dl *dl, struct nlattr **tb) const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); char buf[64]; - sprintf(buf, "%s/%s", bus_name, dev_name); + snprintf(buf, sizeof(buf), "%s/%s", bus_name, dev_name); if (dl->json_output) { if (should_arr_last_handle_end(dl, bus_name, dev_name)) @@ -2902,9 +2903,10 @@ static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name, if (dl->no_nice_names || !try_nice || ifname_map_rev_lookup(dl, bus_name, dev_name, port_index, &ifname) != 0) - sprintf(buf, "%s/%s/%d", bus_name, dev_name, port_index); + snprintf(buf, sizeof(buf), "%s/%s/%d", + bus_name, dev_name, port_index); else - sprintf(buf, "%s", ifname); + snprintf(buf, sizeof(buf), "%s", ifname); if (dl->json_output) { if (array) { @@ -5230,7 +5232,7 @@ pr_out_port_rate_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice) bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); node_name = mnl_attr_get_str(tb[DEVLINK_ATTR_RATE_NODE_NAME]); - sprintf(buf, "%s/%s/%s", bus_name, dev_name, node_name); + snprintf(buf, sizeof(buf), "%s/%s/%s", bus_name, dev_name, node_name); if (dl->json_output) open_json_object(buf); else @@ -6305,7 +6307,7 @@ static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label, open_json_object(label); list_for_each_entry(occ_item, list, list) { - sprintf(buf, "%u", occ_item->index); + snprintf(buf, sizeof(buf), "%u", occ_item->index); open_json_object(buf); if (bound_pool) print_uint(PRINT_JSON, "bound_pool", NULL, @@ -8674,7 +8676,7 @@ static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb) const char *region_name = mnl_attr_get_str(tb[DEVLINK_ATTR_REGION_NAME]); char buf[256]; - sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name); + snprintf(buf, sizeof(buf), "%s/%s/%s", bus_name, dev_name, region_name); if (dl->json_output) open_json_object(buf); else From patchwork Tue Nov 7 08:06:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448025 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 459FADF6B for ; Tue, 7 Nov 2023 08:06:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="w9Yc1WFC" 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 D82A0EA for ; Tue, 7 Nov 2023 00:06:14 -0800 (PST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-543456dbd7bso12516316a12.1 for ; Tue, 07 Nov 2023 00:06:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344373; x=1699949173; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=a8d5a3nShBIdHXY2ae+XSzbkdt1Sq3Zwcq9vEnxeuIQ=; b=w9Yc1WFC1/Xo/yWtHbJcdHYZQrEvwGB8FWhkZzcYzFWomp/pmuAS2h1PKn+loD9+Du mPyNsDcQNYGlUmkP0i3BIHN5nK1kukg1/5wcuSaH/wbPKG3vNQMHdN5qn53zpDm40n5F adTmMX6u9S2QLkxXle8cG3p9oJscQD46HX7IDUt8wtcIfvl5EZhvQc1ZbfGCvApNX7yh uNfzXaXCDilSokD2TgExUKM2aLEutf1v1NcP4xvYpeGY3c31v0M6KRfpHGAb5hm1qtcG PJ9fevIgfhDKFbe61gGqsvC6LCHmf5VypqxeOCy9duRn0kqPP5ROrtx+CjJIOUamuH2f qmFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344373; x=1699949173; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a8d5a3nShBIdHXY2ae+XSzbkdt1Sq3Zwcq9vEnxeuIQ=; b=Ngay7en3R5X66gi0W74eoGcaRoJMldBih223Y5osZrXPFwxuTHhtrYhnFAGpTz3FeM XTcLLjFY+JoTuGVZ+MbaoH/+xZy1J01SeTi13ADS3Qe8DaHSKcnAkFmZmSRhJSJViFxJ WFWUFuLTeY4dloaPC4TN+1exOc1RDDgFbWk40RTuDP5Mzhtuiwu/+qlF44rC0w9ydFAx +OC3ops2tCewiFtRhOtD8/4xxAOO15CD6fWLc0l+4Flmdm2s5NU0bavgBpwPIWyTNNo4 CFXD1orrS64kIO26OmKjIeW1UTcTpO+lVYPv32O6sGwSoVuyid4mgwctFdB95Xt/RUnB ZT3g== X-Gm-Message-State: AOJu0YwfYefVVAdaLht2lXFrLEE8AAi46j6Arn5K040YXo6MwMsYhqlK LfKk568esCn9dboP+4EvJUgQnC9gLuV2aukqzeQ= X-Google-Smtp-Source: AGHT+IHuATefY1MJtu6lHyPDOGGCh4qWEeePNQgnL9bpPGsICpQ4p6MLTGDFOVcP1l/37yv0cmDfvQ== X-Received: by 2002:aa7:cfd3:0:b0:53e:1207:5b69 with SMTP id r19-20020aa7cfd3000000b0053e12075b69mr1632369edy.10.1699344373131; Tue, 07 Nov 2023 00:06:13 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id d19-20020a056402001300b005402a0c9784sm5054940edu.40.2023.11.07.00.06.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:12 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 3/7] devlink: do conditional new line print in pr_out_port_handle_end() Date: Tue, 7 Nov 2023 09:06:03 +0100 Message-ID: <20231107080607.190414-4-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Instead of printing out new line unconditionally, use __pr_out_newline() to print it only when needed avoiding double prints. Signed-off-by: Jiri Pirko --- v2->v3: - new patch --- devlink/devlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index b711e92caaba..90f6f8ff90e2 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -2976,7 +2976,7 @@ static void pr_out_port_handle_end(struct dl *dl) if (dl->json_output) close_json_object(); else - pr_out("\n"); + __pr_out_newline(); } static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr) From patchwork Tue Nov 7 08:06:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448026 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2725D10A0D for ; Tue, 7 Nov 2023 08:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="ADWoy4mV" 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 D831911D for ; Tue, 7 Nov 2023 00:06:15 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-53de8fc1ad8so9026884a12.0 for ; Tue, 07 Nov 2023 00:06:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344374; x=1699949174; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JHwNAGhvr8qblRJ7IY3DURpvARsJQGPAY0lSUzSqs+I=; b=ADWoy4mV67WrrN1gBufZd12NqPAIh14UrFK6wmh3+Y4qzj9E1Oifg3iAa2mkscRrty NKRlxCnx8QIcki/kpItnf+wmy/duFWEqANGyV5TJHP+afxov+Pqt62oo1jbbkBMEyusY r+zd2sUglXp+e/gBrCb1uX3SD3/2byoqnExSrYIsT4i4rJ9ycPenAsFxWD8k5o8qjPIA 525weU+NS7Quah96FhDtetwSHgUDSZi1Dw/uqLO0M569pe+ChFvAMnU5Cdv24PF10IVD l7pxpBnkOmrT/3bzR6TUS2ZdL8FfwPG9YhytfsoEUJPYO9dd/DFLt8KokC6PasvdxBkn vLyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344374; x=1699949174; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JHwNAGhvr8qblRJ7IY3DURpvARsJQGPAY0lSUzSqs+I=; b=EAMbbPlBfrAh4TZVnozGO8R8+vmL36kLNuBW2MAIx70wGj7oWfovh2fVPxvh9WAT9e chKrP61fUIwtfj1D8nQTaB3g/kxamOERS51vrhb/9GiwU3R1jTCTBnMj0EIA5Zeg1GUt Olb5OZeazhnUE32odbFNzvp+h82rij3WTOVtThlMAXd8ASUztRtaRqfjv6HNaeC9f6O5 4529MUEnb09uXh3F3VZhtfyi68E0FnSh59KukbrZzVDRR0hSQPUeRcppodemfPLDHCZp VXgu2jJeMs4dPdtX1dgRtxqpaSLWg8UCc/nzuQfPA5Un7gf9blAgkxb6q2usjnICSaLk +8MQ== X-Gm-Message-State: AOJu0YzUj1nlqqhTXOq2g2FYDPnK9rhtVsYzsQoS7vcgwmyH/QLgqYLU 6vCYqZUwoTVJCjYb4EAqycV6jLJbk3F8QXXoFVc= X-Google-Smtp-Source: AGHT+IGUaQGl098XhmbHBQNanqyXrBoL1IX9Z2tbV5vUPgZ/33outJgtfYC6LC8xU4aUsneMhPDOiQ== X-Received: by 2002:a50:d483:0:b0:544:55c3:ccf9 with SMTP id s3-20020a50d483000000b0054455c3ccf9mr7155830edi.22.1699344374427; Tue, 07 Nov 2023 00:06:14 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id b15-20020a50cccf000000b00542da55a716sm5234993edj.90.2023.11.07.00.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:14 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 4/7] devlink: extend pr_out_nested_handle() to print object Date: Tue, 7 Nov 2023 09:06:04 +0100 Message-ID: <20231107080607.190414-5-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko For existing pr_out_nested_handle() user (line card), the output stays the same. For the new users, introduce __pr_out_nested_handle() to allow to print devlink instance as object allowing to carry attributes in it (like netns). Note that as __pr_out_handle_start() and pr_out_handle_end() are newly used, the function is moved below the definitions. Signed-off-by: Jiri Pirko --- v3->v4: - rebased on top of snprintf patch v2->v3: - new patch --- devlink/devlink.c | 53 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 90f6f8ff90e2..f06f3069e80a 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -2747,26 +2747,6 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name, !cmp_arr_last_handle(dl, bus_name, dev_name); } -static void pr_out_nested_handle(struct nlattr *nla_nested_dl) -{ - struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; - char buf[64]; - int err; - - err = mnl_attr_parse_nested(nla_nested_dl, attr_cb, tb); - if (err != MNL_CB_OK) - return; - - if (!tb[DEVLINK_ATTR_BUS_NAME] || - !tb[DEVLINK_ATTR_DEV_NAME]) - return; - - snprintf(buf, sizeof(buf), "%s/%s", - mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]), - mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME])); - print_string(PRINT_ANY, "nested_devlink", " nested_devlink %s", buf); -} - static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb, bool content, bool array) { @@ -2862,6 +2842,39 @@ static void pr_out_selftests_handle_end(struct dl *dl) __pr_out_newline(); } +static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, + bool is_object) +{ + struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; + int err; + + err = mnl_attr_parse_nested(nla_nested_dl, attr_cb, tb); + if (err != MNL_CB_OK) + return; + + if (!tb[DEVLINK_ATTR_BUS_NAME] || + !tb[DEVLINK_ATTR_DEV_NAME]) + return; + + if (!is_object) { + char buf[64]; + + snprintf(buf, sizeof(buf), "%s/%s", + mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]), + mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME])); + print_string(PRINT_ANY, "nested_devlink", " nested_devlink %s", buf); + return; + } + + __pr_out_handle_start(dl, tb, false, false); + pr_out_handle_end(dl); +} + +static void pr_out_nested_handle(struct nlattr *nla_nested_dl) +{ + __pr_out_nested_handle(NULL, nla_nested_dl, false); +} + static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name, const char *dev_name, uint32_t port_index) { From patchwork Tue Nov 7 08:06:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448027 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12AC212E4B for ; Tue, 7 Nov 2023 08:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="tk7TRw1x" Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28F9FE8 for ; Tue, 7 Nov 2023 00:06:17 -0800 (PST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-9bf86b77a2aso789348466b.0 for ; Tue, 07 Nov 2023 00:06:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344375; x=1699949175; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rny5/tZjlX9ASW7DpqQNMPQyvouS4P/pMU3Z38WXmC0=; b=tk7TRw1xpk55frkdgIPDUjsN8VyyKaq8W94+EvADUIsakM/z3yaphuH9sjwOWwEaoI xund4QlcqiHaLMNuHve4qfDzlJdmIqwRKmNDE9oqZjqHlD+B777y0eaDzejzzCP/kple gbO0z9ERSeXnGjpa/NCdSjVTh4ISzAKHcGLTJscjMAuAb3yho8aUDW/clRvtF938+21K 8ZYYLpCV3Xm7nfCNtl6eCBYEgAreNTK0YSR49YNX91cFtUtMchlFq7SY47OZGfRxJELH 0NUwhzw67usfF1IHG7f1jj3GzWXoN2cW0BKrU+EpCLJTm26IYIJdfwpb1QnbzpJA06dc VFXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344375; x=1699949175; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rny5/tZjlX9ASW7DpqQNMPQyvouS4P/pMU3Z38WXmC0=; b=ZZ/rGZzMDOdLBfDub5upbqgTWzn0q2cgaQ3gjPAUIfYC1BTZTYuKX5OEBqWbJUmTqF za6rd9BpTjhs0LOK+kwsv1cIC2SjQnPXKf8Axvwc7JYWFwexRPjWV2cV9PN1FlA3KO97 CfE7uE+uIiyVdVPscl8by+/i8LGoP7yS3U2E6MFd4607r0D1ldlE6etFOynH9C4AHLnU Wwbyiwk4YXKMQu3ob+rBfyg3GwIcEViuwirt2ewLphc2bNzFsSDWCn4+Tm1OgVyYxaLI VY7p+1ZBfKIoPwlmpUpkQLgY2bbN5TwocUuDm/mFC4R7FNv867eEfFFFfudMyiI9YZ3p dsOA== X-Gm-Message-State: AOJu0YzzTJEIOvVjWzJLkJn0ylEQ0WdtybXkt5qoo+6PTgbnOv9WePci 7j5kBLGlU2Ro9GGW087fZ4gieyKsmUul5R7dTRo= X-Google-Smtp-Source: AGHT+IFXx7cSK7PGOnNu/jgxOLgP7hSqGz02P4/IFvNg9sbK/aA5fm8pUYfyt3eXv9HDfa4ZiEqphw== X-Received: by 2002:a17:907:934c:b0:9d3:8d1e:ced with SMTP id bv12-20020a170907934c00b009d38d1e0cedmr16110083ejc.34.1699344375729; Tue, 07 Nov 2023 00:06:15 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id v12-20020a1709061dcc00b009ae3d711fd9sm744433ejh.69.2023.11.07.00.06.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:15 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 5/7] devlink: introduce support for netns id for nested handle Date: Tue, 7 Nov 2023 09:06:05 +0100 Message-ID: <20231107080607.190414-6-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Nested handle may contain DEVLINK_ATTR_NETNS_ID attribute that indicates the network namespace where the nested devlink instance resides. Process this converting to netns name if possible and print to user. Signed-off-by: Jiri Pirko --- v2->v3: - moved netns_name_by_id() into lib/namespace.c - s/netns_name_by_id/netns_name_from_id/ - rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object" v1->v2: - use previously introduced netns_netnsid_from_name() instead of code duplication for the same function. - s/nesns_name_by_id_func/netns_name_by_id_func/ --- devlink/devlink.c | 23 ++++++++++++++++++++++- include/namespace.h | 1 + lib/namespace.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index f06f3069e80a..f276026b9ba7 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -722,6 +723,7 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES] = MNL_TYPE_NESTED, [DEVLINK_ATTR_NESTED_DEVLINK] = MNL_TYPE_NESTED, [DEVLINK_ATTR_SELFTESTS] = MNL_TYPE_NESTED, + [DEVLINK_ATTR_NETNS_ID] = MNL_TYPE_U32, }; static const enum mnl_attr_data_type @@ -2866,7 +2868,26 @@ static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, return; } - __pr_out_handle_start(dl, tb, false, false); + __pr_out_handle_start(dl, tb, tb[DEVLINK_ATTR_NETNS_ID], false); + if (tb[DEVLINK_ATTR_NETNS_ID]) { + int32_t id = mnl_attr_get_u32(tb[DEVLINK_ATTR_NETNS_ID]); + + if (id >= 0) { + char *name = netns_name_from_id(id); + + if (name) { + print_string(PRINT_ANY, "netns", + " netns %s", name); + free(name); + } else { + print_int(PRINT_ANY, "netnsid", + " netnsid %d", id); + } + } else { + print_string(PRINT_FP, NULL, " netnsid %s", "unknown"); + print_int(PRINT_JSON, "netnsid", NULL, id); + } + } pr_out_handle_end(dl); } diff --git a/include/namespace.h b/include/namespace.h index 2843f4bb5742..86000543f99f 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -60,5 +60,6 @@ struct netns_func { }; int netns_id_from_name(struct rtnl_handle *rtnl, const char *name); +char *netns_name_from_id(int32_t id); #endif /* __NAMESPACE_H__ */ diff --git a/lib/namespace.c b/lib/namespace.c index f03f4bbabceb..d3aeb9658e73 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -188,3 +188,37 @@ out: free(answer); return ret; } + +struct netns_name_from_id_ctx { + int32_t id; + char *name; + struct rtnl_handle *rth; +}; + +static int netns_name_from_id_func(char *nsname, void *arg) +{ + struct netns_name_from_id_ctx *ctx = arg; + int32_t ret; + + ret = netns_id_from_name(ctx->rth, nsname); + if (ret < 0 || ret != ctx->id) + return 0; + ctx->name = strdup(nsname); + return 1; +} + +char *netns_name_from_id(int32_t id) +{ + struct rtnl_handle rth; + struct netns_name_from_id_ctx ctx = { + .id = id, + .rth = &rth, + }; + + if (rtnl_open(&rth, 0) < 0) + return NULL; + netns_foreach(netns_name_from_id_func, &ctx); + rtnl_close(&rth); + + return ctx.name; +} From patchwork Tue Nov 7 08:06:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448028 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D86E612E69 for ; Tue, 7 Nov 2023 08:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="hMJTJ/6n" 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 AA33E122 for ; Tue, 7 Nov 2023 00:06:18 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-53de0d1dc46so9152777a12.3 for ; Tue, 07 Nov 2023 00:06:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344377; x=1699949177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1/tu5XWHHR8usb5Jk8v5P3g4yLTlyEOFPX8/M/aZHvs=; b=hMJTJ/6nTjEXHnAaTvrM2El+5T01ixu0j8vOOgLsnnQeKzEkAFCa9RULgpnPmshC1H BXgT8M9NQTISMYstieRNPScGTaAf9zI9YqsCeeU5/q3StyuIujcJp4+AbDbfn+Qe1RDF eWH6s4dpiCS21uEgc/fBOTC50uJESJhsz9yHhFzXFQGqHmMj6GMeVJ7iJ3rTavs8QfJb 6jC7w+/kOgm5d/Hwc7JbezCJ2MxQUNvkvUr2l6Iv0TP5YyLEy6Db8pl60ni/IZGWucnw OaW3RnHn76Bg9UeZ5UuVelxi+w0dao1BQX3DoGMm+cyscTrai0FZV7PHqKOjXxgjPKUi o2kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344377; x=1699949177; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1/tu5XWHHR8usb5Jk8v5P3g4yLTlyEOFPX8/M/aZHvs=; b=Q45Zp18clO8/vwHs+UJ6J4MJ/JVrVsU/sV5Hp0mm7NJ7QaXXsQUerzGXHXehWeCvN0 iJQHGhvCZ5mRqrY2cy5QAR1lbCjCGgQyHWP3zU9DSVRed3U1GxKw0JT3bfNrNjFYceH3 4LPU+uiPq5DN6TCZhnA0Dyp1qBxgWhAoOM16vvFErnzBKl7hcut+e9PyCcD7adChdkoY +v4mkBWkl3GHauVQlzh0stLpH1kXejfB0/cPRGCdwkEGVHYXby8/vQJUJJXeU8AEcNvy S0koNbzi4oRo8Y0+XWEnNFM70/EulJ793QoyKJjVYhL9emXKhKcwtYR7HESt0BLWx7r3 BIDw== X-Gm-Message-State: AOJu0YzbE77VbosyMUDwV2wTKwnhTMen32L0zy7e45+Syily0y8rJ3Cw ll4d1FhwKxskjzgdr/lK/wAt8PBBzFEZFBNxjaY= X-Google-Smtp-Source: AGHT+IEpR3Al3MGh5toAtTlWDnHTG44FlOJ9ICNsYSr7lCLoRPHfLy6mLhTwWKWXKEkJiYmwdDdy8w== X-Received: by 2002:a50:d7d6:0:b0:543:5cfa:ba41 with SMTP id m22-20020a50d7d6000000b005435cfaba41mr17477705edj.25.1699344377060; Tue, 07 Nov 2023 00:06:17 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id i8-20020a0564020f0800b00533e915923asm5129618eda.49.2023.11.07.00.06.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:16 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 6/7] devlink: print nested handle for port function Date: Tue, 7 Nov 2023 09:06:06 +0100 Message-ID: <20231107080607.190414-7-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko If port function contains nested handle attribute, print it. Signed-off-by: Jiri Pirko --- v2->v3: - rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object" --- devlink/devlink.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/devlink/devlink.c b/devlink/devlink.c index f276026b9ba7..ae31e7cf34e3 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -772,6 +772,7 @@ static const enum mnl_attr_data_type devlink_function_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = { [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = MNL_TYPE_BINARY, [DEVLINK_PORT_FN_ATTR_STATE] = MNL_TYPE_U8, + [DEVLINK_PORT_FN_ATTR_DEVLINK] = MNL_TYPE_NESTED, }; static int function_attr_cb(const struct nlattr *attr, void *data) @@ -2896,6 +2897,22 @@ static void pr_out_nested_handle(struct nlattr *nla_nested_dl) __pr_out_nested_handle(NULL, nla_nested_dl, false); } +static void pr_out_nested_handle_obj(struct dl *dl, + struct nlattr *nla_nested_dl, + bool obj_start, bool obj_end) +{ + if (obj_start) { + pr_out_object_start(dl, "nested_devlink"); + check_indent_newline(dl); + } + __pr_out_nested_handle(dl, nla_nested_dl, true); + if (obj_end) { + if (!dl->json_output) + __pr_out_indent_dec(); + pr_out_object_end(dl); + } +} + static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name, const char *dev_name, uint32_t port_index) { @@ -4839,6 +4856,9 @@ static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port) port_fn_caps->value & DEVLINK_PORT_FN_CAP_IPSEC_PACKET ? "enable" : "disable"); } + if (tb[DEVLINK_PORT_FN_ATTR_DEVLINK]) + pr_out_nested_handle_obj(dl, tb[DEVLINK_PORT_FN_ATTR_DEVLINK], + true, true); if (!dl->json_output) __pr_out_indent_dec(); From patchwork Tue Nov 7 08:06:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13448029 X-Patchwork-Delegate: dsahern@gmail.com Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ADAE13AE9 for ; Tue, 7 Nov 2023 08:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="BES/P7q4" 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 CE606129 for ; Tue, 7 Nov 2023 00:06:19 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-53df747cfe5so9181550a12.2 for ; Tue, 07 Nov 2023 00:06:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1699344378; x=1699949178; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WVEm7APPSSSk+YJQkBaPKu7CTimG895Qex6fY+sz04k=; b=BES/P7q4r3D6JI66C4Rnx2yUj0hakfQ+8ffgu6A7zpEaky91TmXN/9n3JqQNnFd3/a iY+PEF68Fa9WMkYzYte2/ZEIAz/446DspOFEg8el04HvXE4McVf8j/j7OATVXLANnzfS NMRoX0P1Xdul8IOB+gcNqB+o55cIgyyqWejDHgFacbywnzRz7mSQUwgfCQN4/QtGCOG0 E9eFY/dJTypwf386lncqkTdNmiY6sTQG626SguM7JYClm1QSm72+2I5DNQNi1tcLUvhf CZVKAop4+ezy1/c9G50oDbsdiJJnDwzfzP/6j8uD7z39web2fdXPBxTX04Bx/dCOUBPX j5nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699344378; x=1699949178; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WVEm7APPSSSk+YJQkBaPKu7CTimG895Qex6fY+sz04k=; b=d4Y+uO2QDVnwbopwJxKanq+gSrYxpT5YrCu1T3vVM/fQEAqbnrf8cyp6UMhcio7XHG w7DbTD3QcEXyRmpWPAtOhMU8Dt+q7jtbs5WqVvI7NtGEdCv6FzCNkGUkHsA6LTBeYxZs 2nLmXj21HTNHgNidtJny3gLcOEB5wleJ1dO8vw3j4nJdyEEJG1IqiWQ5xPgOO/X+ITjz HyyLbi8cxs5/oYG4PvSA4hIaQLQnb5bcslREP97QD2AeD1GMsicNN5dHXj7V+ndWe6Eo OqveCdWmCIIPocGFvJfhsIQ+Db7DQb8N7EpuP2ehrNqI2LGsjJgrCvco3SR58mGX69t9 5D9g== X-Gm-Message-State: AOJu0YwgwqCndUf3Ix5t6WfLXhrln4bw8vu9FVO87NGlhUn7umQnNn2S a1hXeY5TrEoFseO/M4IOezQIZPrY8ezO+eXbWZQ= X-Google-Smtp-Source: AGHT+IGt+2lUSniG9n1WfTC+6eWFO1Jr7yjUgnEmgfGsp46sdzNP5/ZxAId7fCY8cRHUv0lkCdqkig== X-Received: by 2002:a17:906:fe41:b0:9b6:aac1:6fa5 with SMTP id wz1-20020a170906fe4100b009b6aac16fa5mr16218048ejb.55.1699344378446; Tue, 07 Nov 2023 00:06:18 -0800 (PST) Received: from localhost ([86.61.181.4]) by smtp.gmail.com with ESMTPSA id s18-20020a170906bc5200b009c503bf61c9sm734050ejv.165.2023.11.07.00.06.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 00:06:17 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch iproute2-next v5 7/7] devlink: print nested devlink handle for devlink dev Date: Tue, 7 Nov 2023 09:06:07 +0100 Message-ID: <20231107080607.190414-8-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231107080607.190414-1-jiri@resnulli.us> References: <20231107080607.190414-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Devlink dev may contain one or more nested devlink instances. Print them using previously introduced pr_out_nested_handle_obj() helper. Signed-off-by: Jiri Pirko --- v2->v3: - rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object" and previous patch to use pr_out_nested_handle_obj() --- devlink/devlink.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index ae31e7cf34e3..f999e5940c63 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -3860,13 +3860,35 @@ static void pr_out_reload_data(struct dl *dl, struct nlattr **tb) pr_out_object_end(dl); } +static void pr_out_dev_nested(struct dl *dl, const struct nlmsghdr *nlh) +{ + int i = 0, count = 0; + struct nlattr *attr; + + mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) { + if (mnl_attr_get_type(attr) == DEVLINK_ATTR_NESTED_DEVLINK) + count++; + } + if (!count) + return; + + mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) { + if (mnl_attr_get_type(attr) != DEVLINK_ATTR_NESTED_DEVLINK) + continue; + pr_out_nested_handle_obj(dl, attr, i == 0, i == count - 1); + i++; + } +} -static void pr_out_dev(struct dl *dl, struct nlattr **tb) +static void pr_out_dev(struct dl *dl, const struct nlmsghdr *nlh, + struct nlattr **tb) { if ((tb[DEVLINK_ATTR_RELOAD_FAILED] && mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED])) || - (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats)) { + (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats) || + tb[DEVLINK_ATTR_NESTED_DEVLINK]) { __pr_out_handle_start(dl, tb, true, false); pr_out_reload_data(dl, tb); + pr_out_dev_nested(dl, nlh); pr_out_handle_end(dl); } else { pr_out_handle(dl, tb); @@ -3883,7 +3905,7 @@ static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data) if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) return MNL_CB_ERROR; - pr_out_dev(dl, tb); + pr_out_dev(dl, nlh, tb); return MNL_CB_OK; } @@ -6810,7 +6832,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data) return MNL_CB_ERROR; pr_out_mon_header(genl->cmd); dl->stats = true; - pr_out_dev(dl, tb); + pr_out_dev(dl, nlh, tb); pr_out_mon_footer(); break; case DEVLINK_CMD_PORT_GET: /* fall through */