From patchwork Fri Oct 27 10:13:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13438492 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 C38B51799B for ; Fri, 27 Oct 2023 10:14: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="gQ3lu4w7" 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 DC4BD1A5 for ; Fri, 27 Oct 2023 03:14:09 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-540c54944c4so3875375a12.1 for ; Fri, 27 Oct 2023 03:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401648; x=1699006448; 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=pcI5z2eI3kUBo0BHAkZY7kIa+FkgUkPSSF4raygR3z8=; b=gQ3lu4w7SrC4UeqNjlr1sbySzGu8d5y2tdccbPJoxgG4bVY5DmeKXe/prZKDH41v60 lVLEddEmpSygurov3mrj0ehd6JAsZxssNioeojv6zkHpUx2AfRfbchj3m81BnNuokLaH bhShOsZor6ie+sfoTOoDqLHT4vt22v1aBSYIs8neYmsllV7TOHqzHK7vT+BJiEmugF4W koXYu9b0hIDVtyfhlnhlyAIzeTX3oTQF9vjJ7gH8spMr9CLEVu221dTPbTK8pVe++r/p imv2xuZVkac2qrYpLSVf8nmdrj2+KgFoTB8nM1P+vq3pH4NrBLvUDxyPepELUH/P26qL SnNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401648; x=1699006448; 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=pcI5z2eI3kUBo0BHAkZY7kIa+FkgUkPSSF4raygR3z8=; b=S8gQAdXwpekGeBxkXBnGR6x8A9ncE3JqtmJrWHoivUs0uZ39+mVtXp4OrnsWqV3EQ0 5XVqByOKewP8+wrsulRFmZ6oFPxuYaBwDtpw93ECogzNoS/fLH3wwWBtOtO90wugQbGm ShnSxFkd024bDdA0Iyn5a9IZHXw4JRZxirx0R7nDpm/C1f3kFmO4LeqjJqg8puDvLDyY WP+o/jKxtsW4tkLa9dDtD/9bMSi5CbSJWBdVVWKLBl/Rnl87duiSqWD5PH/xB+WFjTXi NtCGfUjQAirh84vswwje2DgpMY4NGPtL+JEu7+uRTAyP3Cn4btEpvCFhSmqmGPr2a04p hElA== X-Gm-Message-State: AOJu0Yw7b8ybzLcC9WTlDZWpb1+k37KZ9WQhELlQ3Ma17atgeE0Ze5jB TlfamFDr0LGMmiF8b3nLexwG7hG6XaYatpRPxUDDVw== X-Google-Smtp-Source: AGHT+IFKFqOgHq8UMxwaKe62NCvEyEKbUoQRgNDzbQE5jgxQzwT+XKr4eDAgA8d8HawvqFiqqw6YLw== X-Received: by 2002:a05:6402:2314:b0:53d:bf72:d586 with SMTP id l20-20020a056402231400b0053dbf72d586mr4449279eda.16.1698401648091; Fri, 27 Oct 2023 03:14:08 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id b1-20020a056402138100b0054026e95beesm984266edv.76.2023.10.27.03.14.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:07 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 1/7] ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c Date: Fri, 27 Oct 2023 12:13:57 +0200 Message-ID: <20231027101403.958745-2-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 --- v3->v4: - removed namespace.h include v2->v3: - s/netns_netnsid_from_name/netns_id_from_name/ v1->v2: - new patch --- include/namespace.h | 2 ++ ip/ipnetns.c | 45 +---------------------------------------- lib/namespace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/include/namespace.h b/include/namespace.h index e47f9b5d49d1..6483630b8082 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -58,4 +58,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 Fri Oct 27 10:13:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13438493 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 90E60179A5 for ; Fri, 27 Oct 2023 10:14:15 +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="x0EzoOrX" Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5AA81BC for ; Fri, 27 Oct 2023 03:14:11 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-9c603e2354fso385534366b.1 for ; Fri, 27 Oct 2023 03:14:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401650; x=1699006450; 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=x0EzoOrXM1BoRQ2fUHGHfl0gJW3CwmoDKR3paQWwWl/2/qLKPaD8Od9TRqfXUUdRNl YMOX9VDKT5W3h+Dcdp8SV50N1eiaOnNH1HyrX/fhbcN/IBSXHTpg9o4423KurdPSOBJF 7z2GoRXMOainu89vVKlKJHnPIrH0zqHIF4VQf42Nzgp2mrD6oci5uxhDo/dahibBCxbB 6rmQrx9oEV/yG7hKDkFfjoW+gUQuhS5zvHwLcDC4dxvfrsvd3ZW8d82z45Qbvslq0VUY 7whwy5unilA1u2ZiLu4s2P4VqOqPbv3Eg+/wnTT18KgHT7Bztcpz/Uy6QXtWd2hj7VOm ap5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401650; x=1699006450; 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=Zw0oikbt2jKNUBtsDhT/Eqg1q0fmS1MHXZaSBpzJjeDVzocl+tNnJ/sG7lN5iDLtiA 4ppGqjZ/llpgCtyNxyoMeH1+QLTjvESKbhInp2MrkNiAjjgvH5j+dnovYNnxNuR9I9Hp AtrmpZRIGj9HIIUm0RfhNJIggGgrbiqrZUMGKaJVRD0rl7VqTDeIXYvoQ6/slc+7/pt+ 78kVobTamIJaV8RaWMPOzsyOdbPOh+CNq00Q9/NFIrtvMr40LK/olbriaYR5Rg6q+pRN DMFNoYPWHcASJBff7bjKGYb8DCTtVT7i04mEDFScn7zuzpad3FBlEtvNmA3HdYjWXj4R hnEA== X-Gm-Message-State: AOJu0YwERHze11CzWKutjk1f+oUZMJAXNH7YSp3rNWU/ymSzpffJdw8l ELTfBU44R5zFFpWhcp0EtHJ1sslrwa+vxCLfvDfaTQ== X-Google-Smtp-Source: AGHT+IFXdSj8hsRoPw8/121dCNEMZXzJHnHtoEM9VT58iVsNRCpRZiFdSVnm13wTVYoMTlfQYbAo3Q== X-Received: by 2002:a17:907:9403:b0:9be:85c9:43f1 with SMTP id dk3-20020a170907940300b009be85c943f1mr1840016ejc.7.1698401649888; Fri, 27 Oct 2023 03:14:09 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id s9-20020a170906a18900b009ae6a6451fdsm963320ejy.35.2023.10.27.03.14.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:09 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 2/7] devlink: use snprintf instead of sprintf Date: Fri, 27 Oct 2023 12:13:58 +0200 Message-ID: <20231027101403.958745-3-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 Fri Oct 27 10:13:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13438494 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 B2D3B179B6 for ; Fri, 27 Oct 2023 10:14: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="LqyN0vlM" Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A61ED7 for ; Fri, 27 Oct 2023 03:14:14 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-533d31a8523so2840641a12.1 for ; Fri, 27 Oct 2023 03:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401651; x=1699006451; 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=LqyN0vlMjDlhBmRmZ3viiVud4sSVE656pmTZyBujla6cxUm3QYF1iYCUK4vn94NuUc 7b1nMX8qsyb3uBG3aATNuT3ARNKuQAcwDJS1w3lWak6/AXzQLUxZ7FKAU+16yd0mYA5+ dtw3T3I3J/6ACJmGse+DDk3p2AOWs8R0SVvQ/KuWhL+XhpMpJcCfwEipJGxuGxs+waBX bOgc8QYLnXGVMdDhEjSS+CBRRAYeULLVZ72oNPge6hknCqNxDYojCSgTN9g/Wwboh27y odf4Zjf6Y0Oz/ToTxmaDyZ3a34gPiM6OtuyOi391PEaOeD373D2PImuA921h7MQqZien 2KUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401651; x=1699006451; 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=oHY6skusL1dp1BWDPBTlI156PsQ1HLvxtAEdVfAc43foJ0xB3GJ6drhrEzHiLs/IpJ qJ0aK4K+Kd0gn49LzcXFA7YbzHSJ5HyiAvdWONmzz5YGf19ajFfvQ0vuHXVtrBj3N4OA EpycCNe1TCRJ00GU9adjvJYdMv1J4xmKNxXQViBFANMgLBxhuFhCujH4aPeNK9lY7xb/ y/UaGltjmTzyAb/MPQOKYBkV05O6ghaSX+raMz4Lc4XhEY/iX+2IwtgiERHk2exLjg1k pjHfCV0B83YbuetCI8BqOoO+f5hflv1TS2xUmxMe7jlO9MSk0QKKAUA8paMenws6qqD3 EtVQ== X-Gm-Message-State: AOJu0Yx40wfEPUi213aoPiMmZLBaLgW0WjsBy9zWYZtJHNKlMfPrgoOL 3xJdO2tnXYauO6iWK0vVu+bHQJqLBOGfPxRHfWMfPw== X-Google-Smtp-Source: AGHT+IFEIUGVPt9VM851+vExAKRYJBOaE+v/JCZlot4aRvbwQatVejtU2QuS3RKutEZ53N9R+3YMRw== X-Received: by 2002:a17:907:ea2:b0:9b2:abda:2543 with SMTP id ho34-20020a1709070ea200b009b2abda2543mr1913553ejc.65.1698401651444; Fri, 27 Oct 2023 03:14:11 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id rv4-20020a17090710c400b009b9a1714524sm979155ejb.12.2023.10.27.03.14.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:11 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 3/7] devlink: do conditional new line print in pr_out_port_handle_end() Date: Fri, 27 Oct 2023 12:13:59 +0200 Message-ID: <20231027101403.958745-4-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 Fri Oct 27 10:14:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13438495 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 E7E831802F for ; Fri, 27 Oct 2023 10:14: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="l6vh/qoA" 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 B0ACD1B6 for ; Fri, 27 Oct 2023 03:14:14 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-53e08e439c7so3217777a12.0 for ; Fri, 27 Oct 2023 03:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401653; x=1699006453; 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=l6vh/qoAuguvIyh/Wfoq8iHb6v9YYXhSmekWv1ZBtKeyGECHrZmYgFY4r/mNN6efkd yJ0MvVu7qWZQIT1PGJuDAumL+qgBBmZ8XbaJUKNJW48jSoLQ8WZV7HmvHPjVYBcMQ4ko TOM0/Rj0wgxrGluMtUlIYliqV5VSTJEFN0MKOvGEiLXLsDw0f9nPGZHTzzcMoum4C4ua m9Bj8x+2Usn9+NMsKKmFgY/7ynJ227wSf8bui6kP4gdNnNFFchI9R+nflAAOU/elK1Gj vv6abRKFOB5kqjyY3E9xyuFONZzmXvE823dEjWkt5BxFxdUhrFkNLiD9M6Oif9SyBIbJ Z9Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401653; x=1699006453; 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=NVxRfh60yYRshvtBxvbPtl7DrLLp1jwJwy0qB9OrCTjrz9ydYJAbm4ndpj9zs9VQTa qRgWGSz+X4C8U9SjiLRZHRIyNHPR+LGbcktUwClq9ljfcHOauAz8Bty74uEI7kRTtGaA k8CmSJlYl7G90atvz4J9a/U8mhWMSu/LUgF3Dtw5N3O2hfUluory52LVNmX90+9QcFKH 07Uyp6CaniU84UpN0Ayl0P6zHbWKZV+mCOziwWWZn+pBRIZUj9YiHW/GWEVohw8/Zqq0 4XMitPv06IaA/Ne8vdyVWHSkuU0GFqPJeY7GcZL3fOQ6G2GImKtCAn75OoPY8D85De8X MOXQ== X-Gm-Message-State: AOJu0YwcpvBdf6R9vV7vsvi+lwV4sUb+Jr2mpQPNkBvBXye+zWGAUVOb ePz0iitFKtyvTA8lfzo3w53BbCKBbfGWxRmR06MFwg== X-Google-Smtp-Source: AGHT+IGC1oK7t60uQgZw0mTW4OoGk2YkB8HtZBFetqLFcMwCKkM4EZ2lZLWjZvUGwnBc5jcIbHj8Ig== X-Received: by 2002:a17:907:608c:b0:9c4:41c9:6ac6 with SMTP id ht12-20020a170907608c00b009c441c96ac6mr1569954ejc.33.1698401653104; Fri, 27 Oct 2023 03:14:13 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id s8-20020a1709066c8800b009ad7fc17b2asm959132ejr.224.2023.10.27.03.14.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:12 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 4/7] devlink: extend pr_out_nested_handle() to print object Date: Fri, 27 Oct 2023 12:14:00 +0200 Message-ID: <20231027101403.958745-5-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 Fri Oct 27 10:14: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: 13438496 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 4BE3318049 for ; Fri, 27 Oct 2023 10:14:18 +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="ZE6Ixxrw" 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 43687194 for ; Fri, 27 Oct 2023 03:14:16 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-53dd752685fso2919643a12.3 for ; Fri, 27 Oct 2023 03:14:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401654; x=1699006454; 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=H/iH+OcilrDlqznkxaiZFecCShpMj3BOZHOw3HA1hkM=; b=ZE6IxxrwL4vFhFzcBh+cxu3rR3X9OyFFcRpZPV3N4SHaJY51cazmbFXqExKqsl553p J+2ivV8j+pXJ/oNGswgMnV+0mnsM9RpwYYxO8lXc4z/HNVE/3KjQmj1zjWnp7Mb/b5rT MxtgF//IccJBzOpYOFpCzSJn7pYm65IJffA5mF1tAc7Js2hVuCLCK6ado1IB3797UD+Q z59EL9l+OvAdVN4Qil6hAXCTPss6479PZUsvsDbOBak0iqKHlQaMlsB849iB6FA6tQXu GJtqF4diFeTrC/g94TqFg1qIl4vLTvps+9Hp10I7OznRO3zwcZBOt1Lmny/3b0NSOLK/ 2V8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401654; x=1699006454; 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=H/iH+OcilrDlqznkxaiZFecCShpMj3BOZHOw3HA1hkM=; b=SfuozUH0OEDen5+Z1WqOl3Q1+fgYENIiLJbtXfmlAV1LqbkxIg1TXu8TdYzMcYex1c fuyT3p4QHhz7jRnXCEKmfkvdZvAopeVXbi3huboJR1Le5hmJr1rrBOzwMcds1tbfgqWC wPySfdCJpLT/8mWbB6ucKTKUG25MfLT/rjXYcOdoHB/fIKki6FO23g4NemUodvzLSo8g 11q54Y5KNeGZ7Sv0ZE/rby+U/tV2l+3lmX/Phwkzr99RfnDZVCshGzD4wZxFhFkcdIlH nHulRRk294Jbjvv5pGq6wxNA2RWS+Vb7Pvz8nOIvYbcLe1eGC1quTkDVp9MHDTFJ7pAs d7Fw== X-Gm-Message-State: AOJu0YxH7PDs+J0UHlCkbZkLCFshiZpGRBoPdEQwMCzGvQ4U0RoLjMvq yea46UqVQ3NVbgDPhqI0XHbswTxi2/RwFPvvrhUhwg== X-Google-Smtp-Source: AGHT+IHGCGA6XN4wVXikAvl1hqDGROUKAduEMQImPHQqauOkxbGfFrQ7EDWufY4Fki0Uo+IAITAxGw== X-Received: by 2002:aa7:d4d3:0:b0:540:3286:d2e8 with SMTP id t19-20020aa7d4d3000000b005403286d2e8mr1877809edr.18.1698401654708; Fri, 27 Oct 2023 03:14:14 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id u30-20020a50a41e000000b0052febc781bfsm1034163edb.36.2023.10.27.03.14.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:14 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 5/7] devlink: introduce support for netns id for nested handle Date: Fri, 27 Oct 2023 12:14:01 +0200 Message-ID: <20231027101403.958745-6-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 6483630b8082..4ae294749962 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -59,5 +59,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 Fri Oct 27 10:14: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: 13438497 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 627B8182BB for ; Fri, 27 Oct 2023 10:14: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="dp7QUkX0" Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1AB31AC for ; Fri, 27 Oct 2023 03:14:17 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-9bf86b77a2aso266868366b.0 for ; Fri, 27 Oct 2023 03:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401656; x=1699006456; 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=dp7QUkX0FwHzkhbyKlAwAtXIgbnXH6o1ehQu/MaCcuum4ciQXqedSA8MmjhmJ4/lW7 aVA9KcqBlHxUdY67p+Jk/NDrbOFrrzKRC90WhotXSW+iGNbSPMK4set+7I9Gq8N+dIa+ 65hfrGJ+3ECyy2ZpxuGGN/ufcxg6VH30VvtHqDF9T5twrq5Bhd1LuO0LojXKXUsUxFr4 1lOnkNP55lnA4XdYteaEJTb8soDH975pvAHR6Prv3DHUkCbDFYyiXMJDTnpARrf118u2 ZLbG7BA7dDWNYnKDu5LRnIwCWRLTM7JiJeIjvhgCTxmSe4/BiGbruuMriHnJ7fR57M32 AMLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401656; x=1699006456; 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=igfjTMj09Brg7M460pgmUSiaUjOUhMjjda6bMBevJU3mW0WiBoa1dNMXCUKXt+ZXNd sM+lKvQAxiAOWt0u/EdVcvZGyE5Ib9Ix4yFyMjdJTLu5dnFs/4qtK/Gcri9RDs6DRAr4 LNuwT4xSO7sI6e/2U/Ttt54aTjZhOZbhcdWxiaMynHZysbswirqopgXTb6CjPWjnS6lN zfsNGGycFI/4oueyzplKMB1WEF+zWN0OAFjdkC0hSezEDUdQ2HdcD8ngMUKuqeHeyQ7l QkEk5OBm6p1f2NvaLSfrSVW9jybL7mqA08rJ6F4Ak9jdZfpBquz/VcKvfe7kOJcDMFYm cR+g== X-Gm-Message-State: AOJu0Yxb5gBxgcEW/WF9a9XFQQ/qP92cP5AFVPPKFO8+TkK5Rnqpb8OJ tbcW/wmYFti+Rkkv2QWXUprsUxNAvLUesrp9k9xX/Q== X-Google-Smtp-Source: AGHT+IFrp+nR/Itr9VaIcjK2yyMjgJCNyY6AH0G6u9MOqOBDS6VAk0xjpgqMQaBzUoDj70dHzbsygA== X-Received: by 2002:a17:907:3ea9:b0:9c5:45f8:c529 with SMTP id hs41-20020a1709073ea900b009c545f8c529mr2076606ejc.20.1698401656430; Fri, 27 Oct 2023 03:14:16 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id cw23-20020a170906c79700b009c3828fec06sm943856ejb.81.2023.10.27.03.14.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:15 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 6/7] devlink: print nested handle for port function Date: Fri, 27 Oct 2023 12:14:02 +0200 Message-ID: <20231027101403.958745-7-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 Fri Oct 27 10:14: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: 13438498 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 304F818AEF for ; Fri, 27 Oct 2023 10:14:21 +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="NDW/zTi/" Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7D1C111 for ; Fri, 27 Oct 2023 03:14:19 -0700 (PDT) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-99de884ad25so290043866b.3 for ; Fri, 27 Oct 2023 03:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1698401658; x=1699006458; 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=NDW/zTi/z91o5Ekwx5RicwfBPbNN7Q7R2j8LrV/pf+BRV9p9vKpkxcghCp97zRXUwe /eDXTJutKjqRMYbSY1CdnwHrQXEECuuW9hN84UUkfdZQ/DByD0zbs66gy0a/dOJKqF1g VUEPHb7VqNQHbDu06soPrrlmT/V4S5BJDwQR0PBAfify1IXRPy9VqfYRoFMgJDUJ8NLp b1QTzjy/6ejiBMrt1JPWkp8+NCBHQQmo+PFJVOPUjEQgPas6RpG/DShN05uB+vXcTGBE ALbuYnmV1LCb8iVfTIHsPD5FbBNYGFxk30yozndPZjAmgw8ObAOWHIPw1Zw1OaBtkfQf 5sFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698401658; x=1699006458; 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=Az+r5DvExoKz7b2Lpx9pficbcwqL8SE4v9VqSPs4QyhCduGi+/eFy40wbH1VPdOlhz Nm0anJZlKmzQrTqt5got9qlO+aidoc6RXpzNkyJl1tbSaI6+Ue0mD5Uwp/GMM/tABdAb tGzXds0MyMvQNf72X0nMt3LaLbOnF82IoESa91sOZj0cMbxIdOzSxJQzL6xjBvGik6G5 l1HAnh+FO/VesRJTVHFFmZU/kTXSe7p6t++Mxl/emlcHDF4gHZzbgnmpEhqT2nZyfY5l qLjXj3Z/9mR3l/n17Oy7vLmTMOi/e4ThzZeJq+hQ06Vm59Sa0vXJ6BUUoxaBcHbdA7eY eoow== X-Gm-Message-State: AOJu0YwVIsIK++K8Zn9LFOmNCUNgKuAcSUICDOAwyGOe+87Pf8Ihpz1g pnhvpfSaxPuZSDhOCA+l7NEGAxEDHIY+r05CbshCWA== X-Google-Smtp-Source: AGHT+IFfu8gSec0X8VNfECaNQ4pek2v8RK5jv+N8PPxIzx90sNZ3br0xG1wrLeVQqmMfq9SfqodsaQ== X-Received: by 2002:a17:907:9614:b0:9af:4561:591d with SMTP id gb20-20020a170907961400b009af4561591dmr1917582ejc.18.1698401658053; Fri, 27 Oct 2023 03:14:18 -0700 (PDT) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id 3-20020a170906208300b0099cd1c0cb21sm954886ejq.129.2023.10.27.03.14.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 03:14:17 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com, daniel.machon@microchip.com Subject: [patch net-next v4 7/7] devlink: print nested devlink handle for devlink dev Date: Fri, 27 Oct 2023 12:14:03 +0200 Message-ID: <20231027101403.958745-8-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231027101403.958745-1-jiri@resnulli.us> References: <20231027101403.958745-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: kuba@kernel.org 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 */