From patchwork Thu Aug 31 13:22:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371572 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 F18781172B for ; Thu, 31 Aug 2023 13:22:36 +0000 (UTC) Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EC9CCFE for ; Thu, 31 Aug 2023 06:22:35 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-31c5a2e8501so631404f8f.0 for ; Thu, 31 Aug 2023 06:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488153; x=1694092953; 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=rKFtUbKKPKW7K/rcbBsZRAnuPjgGSYr12XFfzjq1ADs=; b=kf9nhnqPzkeNzCAbW6WzAFcMpXYoiMis+gHqbRxs+F/sEzWiuXtptQctqrZwaCTjcs ip/JkyuCvpsw7xGvM77H64GflYmP5mqQzj+3J5lqkCuuw17O+Kg07ZvWt3nzlPgsxDDy 3hzKMlt/9zoskaFcgeW/k7T80QLohSUkpuUBWPTcuhglVkTwUbreT86ABer2fZ1DhMrm UAF7dEVMyabPzpeRKZgXQIgTke+MLNcWXXQNKgK8fnCC/Eyb6On4jnZ6CWwcLVAa+p/m +NQxioonNEjepKxT/YtUMuIAoVAsxOQxCLZzFnrDucZQ3yv/sQg49YWhLGgcdn9EZLvS Abqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488153; x=1694092953; 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=rKFtUbKKPKW7K/rcbBsZRAnuPjgGSYr12XFfzjq1ADs=; b=jEExvSsjBYW5A6qTTfbf3iC4HcHhRy3Db2Tx0Io0l6z+5NwHF3pwiGUnjVoL1+nsDN HGol9ZREFhurVVjY/Y7RDUswIuk2gbEJxAmGlnfale96pwkiidRuH4P+23TEXzM/kEdy Icj6fbKAOx3lccqJShXfu2+JbtSOpI/aYD2MvFTXdQ25dxNuWvwUTLJXl5QVqiBGnoyq sepHBjFKj0FpSxtdVUSGejlowFkI7ABXScDD9UKK4O4Fe56ASJei//mC2Q1dW0M4ttPr 4hZySxSDLFRSNkSMHWTzNNA9MG1LNKBJjwxYUVGTKahYYU7WSZk7tF3dqfqLrgL5A/6E NDqQ== X-Gm-Message-State: AOJu0YzkhBYXB0ZC+N9nWhRxqzuCbu56QXtk1h37IYOS6GZgHgSLeevV z6aTUFX7y7CuUeHEst350qaxfKb7y4HCdEzXR0s= X-Google-Smtp-Source: AGHT+IGQxikohTMZlQVymFsctJxHER4F/gFHQpesrDcskYVnmXCenhIilohbeF5Fb56mEQCoHBQnow== X-Received: by 2002:adf:e7cc:0:b0:31a:ccc6:b8de with SMTP id e12-20020adfe7cc000000b0031accc6b8demr4253729wrn.50.1693488153686; Thu, 31 Aug 2023 06:22:33 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id i5-20020adfefc5000000b0031759e6b43fsm2241102wrp.39.2023.08.31.06.22.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:33 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 1/6] devlink: move DL_OPT_SB into required options Date: Thu, 31 Aug 2023 15:22:24 +0200 Message-ID: <20230831132229.471693-2-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko This is basically a cosmetic change. The SB index is not required to be passed by user and implicitly index 0 is used. This is ensured by special treating at the end of dl_argv_parse(). Move this option from optional to required options. Signed-off-by: Jiri Pirko --- devlink/devlink.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 616720a9051f..713ac1f201e2 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -2271,7 +2271,7 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, opts->present = o_found; - if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) { + if ((o_required & DL_OPT_SB) && !(o_found & DL_OPT_SB)) { opts->sb_index = 0; opts->present |= DL_OPT_SB; } @@ -5721,7 +5721,7 @@ static int cmd_sb_show(struct dl *dl) flags |= NLM_F_DUMP; } else { - err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB, 0); if (err) return err; } @@ -5800,8 +5800,8 @@ static int cmd_sb_pool_show(struct dl *dl) flags |= NLM_F_DUMP; } else { - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB_POOL, - DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB | + DL_OPT_SB_POOL, 0); if (err) return err; } @@ -5821,8 +5821,8 @@ static int cmd_sb_pool_set(struct dl *dl) struct nlmsghdr *nlh; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB_POOL | - DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB | DL_OPT_SB_POOL | + DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, 0); if (err) return err; @@ -5889,8 +5889,8 @@ static int cmd_sb_port_pool_show(struct dl *dl) flags |= NLM_F_DUMP; } else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL, - DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | + DL_OPT_SB_POOL, 0); if (err) return err; } @@ -5910,8 +5910,8 @@ static int cmd_sb_port_pool_set(struct dl *dl) struct nlmsghdr *nlh; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL | DL_OPT_SB_TH, - DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | DL_OPT_SB_POOL | + DL_OPT_SB_TH, 0); if (err) return err; @@ -5996,8 +5996,8 @@ static int cmd_sb_tc_bind_show(struct dl *dl) flags |= NLM_F_DUMP; } else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | - DL_OPT_SB_TYPE, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | DL_OPT_SB_TC | + DL_OPT_SB_TYPE, 0); if (err) return err; } @@ -6017,9 +6017,8 @@ static int cmd_sb_tc_bind_set(struct dl *dl) struct nlmsghdr *nlh; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | - DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH, - DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | DL_OPT_SB_TC | + DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH, 0); if (err) return err; @@ -6338,7 +6337,7 @@ static int cmd_sb_occ_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP | DL_OPT_SB, 0); if (err) return err; @@ -6374,7 +6373,7 @@ static int cmd_sb_occ_snapshot(struct dl *dl) struct nlmsghdr *nlh; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB, 0); if (err) return err; @@ -6391,7 +6390,7 @@ static int cmd_sb_occ_clearmax(struct dl *dl) struct nlmsghdr *nlh; int err; - err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_SB); + err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB, 0); if (err) return err; From patchwork Thu Aug 31 13:22:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371573 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 3EA151172B for ; Thu, 31 Aug 2023 13:22:38 +0000 (UTC) Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0781CEB for ; Thu, 31 Aug 2023 06:22:36 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3ff7d73a6feso7338015e9.1 for ; Thu, 31 Aug 2023 06:22:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488155; x=1694092955; 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=BNGjPi8Kz3iWxuqrnIa6y+7h6yi5Yp1+x9gNKGAxkDk=; b=AN9HIyke08Tr9OifM9Gn8TOkBwe5yPsclZngGODBnhq4+UuflpbDFcIADjt33oHAWo vz5tLQvOo/V+9HkVTiPcwsNrlK8TeZ+Qvqdopa/ylc36cqLyz74vWSq9xr/AOrVJLv5V rBIGdrs+fU2CWwbK/iJuP4dwdz+ve5JwlsAAqrNN+RAiqUq16/9t5kMya1BbV+6DUrZV XW2KIprNM3kwfl0hAxch7ST1XKHyv1ytKS4oPhTyd2JuM1Lj6Y79Oj9YnRiaGwsZMuG7 1+xIfFY2vI/f1UgxHwY+yIuOHWgJLweK2VmLcVFAvA8K9gbgvdqNOWTYSGNx/1CfKa8a Nb4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488155; x=1694092955; 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=BNGjPi8Kz3iWxuqrnIa6y+7h6yi5Yp1+x9gNKGAxkDk=; b=EnQUkwXi3b24NAHiMMzN5IxzeE7lAabXtACBFYVU9qzYa/geuMUmkC2eg7hTKwXFqs mStO65JKAQc2qU6UD6g0n9EvBq6vfJSQomypFEfXXkQyOqGSSYbtk70xQ92mMwyEUZKK TiwP6t2hnpUfMNYrjf9YO4JgdaeRyiTe4WiRbCPf7FoGvg0z/vA8vNDrAvw9zbyW4rdT aHsUzO09mj1Gqcxk38ymaNbrqKhNKOlR+cRhjsOvKdxXiV2A81cQ+1agRlw8w9mNXE/7 r5ctlQtH2yCH/T67fQd+Le8/GJxV60F+mg+x3FY5mZKomC3T5UMDHj4BM5hyHMlfvq1O TmnQ== X-Gm-Message-State: AOJu0YygKPvng14b6pHIQpxHEmAHEYYolxsmv0BB/sN3VwksxKtwDjXv uYybb+xCZi8FS2yd4ZSYy8iaIRtUyUsVK10EszY= X-Google-Smtp-Source: AGHT+IEWA4SZ02tkNm/xGuUUWeoNYfBL+JiuaQjw00f4DRehp//L26ZwswRjwWRy+7s2bknIyRNnEA== X-Received: by 2002:a05:600c:3798:b0:3f5:fff8:d4f3 with SMTP id o24-20020a05600c379800b003f5fff8d4f3mr4177435wmr.7.1693488155246; Thu, 31 Aug 2023 06:22:35 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id l3-20020a1ced03000000b003feae747ff2sm5191368wmh.35.2023.08.31.06.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:34 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 2/6] devlink: make parsing of handle non-destructive to argv Date: Thu, 31 Aug 2023 15:22:25 +0200 Message-ID: <20230831132229.471693-3-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Currently, handle parsing is destructive as the "\0" string ends are being put in certain positions during parsing. That prevents it from being used repeatedly. This is problematic with the follow-up patch implementing dry-parsing. Fix by making a copy of handle argv during parsing. Signed-off-by: Jiri Pirko --- devlink/devlink.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 713ac1f201e2..e7b5b788863a 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -378,6 +378,7 @@ struct dl { struct list_head ifname_map_list; int argc; char **argv; + char *handle_argv; bool no_nice_names; struct dl_opts opts; bool json_output; @@ -1066,9 +1067,8 @@ static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name) return 0; } -static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name) +static int dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name) { - char *str = dl_argv_next(dl); int err; err = ident_str_validate(str, 1); @@ -1121,10 +1121,9 @@ static int __dl_argv_handle_port_ifname(struct dl *dl, char *str, return 0; } -static int dl_argv_handle_port(struct dl *dl, char **p_bus_name, +static int dl_argv_handle_port(struct dl *dl, char *str, char **p_bus_name, char **p_dev_name, uint32_t *p_port_index) { - char *str = dl_argv_next(dl); unsigned int slash_count; if (!str) { @@ -1146,11 +1145,10 @@ static int dl_argv_handle_port(struct dl *dl, char **p_bus_name, } } -static int dl_argv_handle_both(struct dl *dl, char **p_bus_name, +static int dl_argv_handle_both(struct dl *dl, char *str, char **p_bus_name, char **p_dev_name, uint32_t *p_port_index, uint64_t *p_handle_bit) { - char *str = dl_argv_next(dl); unsigned int slash_count; int err; @@ -1199,10 +1197,9 @@ static int __dl_argv_handle_name(char *str, char **p_bus_name, return str_split_by_char(handlestr, p_bus_name, p_dev_name, '/'); } -static int dl_argv_handle_region(struct dl *dl, char **p_bus_name, +static int dl_argv_handle_region(char *str, char **p_bus_name, char **p_dev_name, char **p_region) { - char *str = dl_argv_next(dl); int err; err = ident_str_validate(str, 2); @@ -1218,10 +1215,9 @@ static int dl_argv_handle_region(struct dl *dl, char **p_bus_name, } -static int dl_argv_handle_rate_node(struct dl *dl, char **p_bus_name, +static int dl_argv_handle_rate_node(char *str, char **p_bus_name, char **p_dev_name, char **p_node) { - char *str = dl_argv_next(dl); int err; err = ident_str_validate(str, 2); @@ -1244,11 +1240,10 @@ static int dl_argv_handle_rate_node(struct dl *dl, char **p_bus_name, return err; } -static int dl_argv_handle_rate(struct dl *dl, char **p_bus_name, +static int dl_argv_handle_rate(char *str, char **p_bus_name, char **p_dev_name, uint32_t *p_port_index, char **p_node_name, uint64_t *p_handle_bit) { - char *str = dl_argv_next(dl); char *identifier; int err; @@ -1698,14 +1693,24 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, { struct dl_opts *opts = &dl->opts; uint64_t o_all = o_required | o_optional; + char *str = dl_argv_next(dl); uint64_t o_found = 0; int err; + if (str) { + str = strdup(str); + if (!str) + return -ENOMEM; + free(dl->handle_argv); + dl->handle_argv = str; + } + if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) { uint64_t handle_bit; - err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name, - &opts->port_index, &handle_bit); + err = dl_argv_handle_both(dl, str, &opts->bus_name, + &opts->dev_name, &opts->port_index, + &handle_bit); if (err) return err; o_required &= ~(DL_OPT_HANDLE | DL_OPT_HANDLEP) | handle_bit; @@ -1714,7 +1719,7 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, o_required & DL_OPT_PORT_FN_RATE_NODE_NAME) { uint64_t handle_bit; - err = dl_argv_handle_rate(dl, &opts->bus_name, &opts->dev_name, + err = dl_argv_handle_rate(str, &opts->bus_name, &opts->dev_name, &opts->port_index, &opts->rate_node_name, &handle_bit); @@ -1724,25 +1729,25 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, handle_bit; o_found |= handle_bit; } else if (o_required & DL_OPT_HANDLE) { - err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name); + err = dl_argv_handle(str, &opts->bus_name, &opts->dev_name); if (err) return err; o_found |= DL_OPT_HANDLE; } else if (o_required & DL_OPT_HANDLEP) { - err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name, - &opts->port_index); + err = dl_argv_handle_port(dl, str, &opts->bus_name, + &opts->dev_name, &opts->port_index); if (err) return err; o_found |= DL_OPT_HANDLEP; } else if (o_required & DL_OPT_HANDLE_REGION) { - err = dl_argv_handle_region(dl, &opts->bus_name, + err = dl_argv_handle_region(str, &opts->bus_name, &opts->dev_name, &opts->region_name); if (err) return err; o_found |= DL_OPT_HANDLE_REGION; } else if (o_required & DL_OPT_PORT_FN_RATE_NODE_NAME) { - err = dl_argv_handle_rate_node(dl, &opts->bus_name, + err = dl_argv_handle_rate_node(str, &opts->bus_name, &opts->dev_name, &opts->rate_node_name); if (err) @@ -9902,6 +9907,7 @@ static struct dl *dl_alloc(void) static void dl_free(struct dl *dl) { + free(dl->handle_argv); free(dl); } From patchwork Thu Aug 31 13:22:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371574 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 C188411C82 for ; Thu, 31 Aug 2023 13:22:39 +0000 (UTC) Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BC09CEB for ; Thu, 31 Aug 2023 06:22:38 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-401da71b7faso8193215e9.2 for ; Thu, 31 Aug 2023 06:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488157; x=1694092957; 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=fO1qCKwrMIuDKlrQa6pAbTr3sD/BPmYH7vzrt4lfo9o=; b=jW4bLm8RExVM/v3A5SR+xzOzAhCwWQx4pL0d8ds4Tqu/s3J/x3FwRR3m5PDMWZReaU PYj6mBSf8Mc5l2rCx6bov5VlkEyOS2hbYpLzSWWKvVQVFFyjc8mxCvQoM0FW44KjViZc kpFYMSK4+CjTKPCGlotI4B99+WRNzLOa4kBzjMlpiChlwYMxdhKseufad9Y/NHrKMXyA VdqqvtRqjUVg2MY22OSaJudoGffKNe6+4ItheukCb2Cd22aPj4Fm9Hvt72BvXl0mX0vC o/B/mYwVOeUv4SY0zXArbbBj9hHAlZjLkEMPeanyw8Y0c2MG0UVXyqNe8jTn2lxDgH2p Pf8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488157; x=1694092957; 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=fO1qCKwrMIuDKlrQa6pAbTr3sD/BPmYH7vzrt4lfo9o=; b=hqt9K1xen+DG6k0pr9sPFMCuBh7pt28Vs+pKPy5J5B/ACyO2MNQiWJSir77jN0Vo23 IGiRYPx6jxFohI7zCd0GbVId2llCNQhko+fAgXCiovRobHHOuUzBE4k/fTK0H+YYaM1r u9o5ebDi3OWnZHguG/Uu9fcI8H2EP/X/SU7fqkkziYgJ+r+cHIM0pFaKBm+MzWDthT/X 2ejVq7DqSmzo6Zn9IdthnR2mVOd8goypQfmgBUtnN+lAZL2i07KSBaesSrwkn5RG8PMU hNA5Z+5qHcCH8Cd5M/ENb0wM2zTJ+vPLXlzk2sd8raoTVj5idEQAZTcJKmMZqm6vbsEW Z6Cg== X-Gm-Message-State: AOJu0YwHkmyJxw1Ky/x6lU7Sc3C4FVcXHVzgPIpoNKnlQylMaquRRoZX +8F/d9LDFLiqhDlJwPF9QUImhOw45CyKtadvUtI= X-Google-Smtp-Source: AGHT+IEnkZkoiP7m5NenJYYYcSu3pgmvitG/W5ttddAtcuyLs5XlHd+CMeXNzCJCDNeC3hmLrypk6Q== X-Received: by 2002:a1c:7718:0:b0:400:57d1:4914 with SMTP id t24-20020a1c7718000000b0040057d14914mr4086176wmi.5.1693488156990; Thu, 31 Aug 2023 06:22:36 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id m9-20020a7bca49000000b003fed1ba0b8esm1981983wml.8.2023.08.31.06.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:36 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 3/6] devlink: implement command line args dry parsing Date: Thu, 31 Aug 2023 15:22:26 +0200 Message-ID: <20230831132229.471693-4-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko In preparation to the follow-up dump selector patch, introduce function dl_argv_dry_parse() which allows to do dry parsing of command line arguments without printing out any error messages to the user. Signed-off-by: Jiri Pirko --- devlink/devlink.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/devlink/devlink.c b/devlink/devlink.c index e7b5b788863a..8d2424f58cc2 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -64,6 +64,7 @@ static int g_new_line_count; static int g_indent_level; static bool g_indent_newline; +static bool g_err_suspended; #define INDENT_STR_STEP 2 #define INDENT_STR_MAXLEN 32 @@ -74,6 +75,8 @@ pr_err(const char *fmt, ...) { va_list ap; + if (g_err_suspended) + return; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); @@ -2284,6 +2287,21 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, return dl_args_finding_required_validate(o_required, o_found); } +static int dl_argv_dry_parse(struct dl *dl, uint64_t o_required, + uint64_t o_optional) +{ + char **argv = dl->argv; + int argc = dl->argc; + int err; + + g_err_suspended = true; + err = dl_argv_parse(dl, o_required, o_optional); + g_err_suspended = false; + dl->argv = argv; + dl->argc = argc; + return err; +} + static void dl_function_attr_put(struct nlmsghdr *nlh, const struct dl_opts *opts) { From patchwork Thu Aug 31 13:22:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371575 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 8466811C82 for ; Thu, 31 Aug 2023 13:22:41 +0000 (UTC) Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E47ECEB for ; Thu, 31 Aug 2023 06:22:40 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-401d67434daso7800745e9.2 for ; Thu, 31 Aug 2023 06:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488159; x=1694092959; 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=hLucJPRDjS5JeAlu2s6d3E9cWHltRgyGs33l+Nj7uBY=; b=B+6TiTHNXnw/fHpI8Wywez/bBdBSvuIRE50uMbVm1bUA1WL2FqMnwR1RBnkg/C6oH9 cPyD0CdTXcF3v7UkqXR0+1FxvaYxuFn+uCzJRX0NjVLtJA/Jr00OX1MsOVjWRWHPUym1 uc3L85cZlD5F5s0AHfHPrvEFSe7eEv8J34eI9wFNpsTkLLaMw60u2+IjD/+YEYijr1Jf hPO66Cg4gtVgHLUBWu33/HIzpat3doTbYzL+7G1uMHrqqGvcibQgCKS6Q8ZY0appP2Gy gUG+qkB76GJr2P7Tfr9invMk4PlcoHjf9fixG+7aoRpGebZbXyOty+7Ut4I71kmpYhHU a9IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488159; x=1694092959; 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=hLucJPRDjS5JeAlu2s6d3E9cWHltRgyGs33l+Nj7uBY=; b=A+nvBdyrVdLq+8ZdckkHBZIahp5Spgw8I18ZVLvszH9wvtsEcITXypD+BaS3fWLTmm T9AP4pG9riS2avDb5lB7GcDu9M3fsbvHGxDQa4cESMZLNkhOMfeeHYSj+8T5rIL+cHMR Tt/1U1ITfektiRQqNJrQ4TfGIKYOkKDfP58TcpZto4qJedV5ELy4CHEY6MTDKAuMjFtN tqpfebuFEry8V9TWbU+st0cPYer4INwiTM3JC46bbqjD1HG0YTwzx3fWMVTI8tnq4Czy ZN3jGMIQSXKcJc9r74Jx+0ejGz0uhLEcQ+2cFPhn4N7hpgyHtZdUBLBrDlceo1du1/0x O9MQ== X-Gm-Message-State: AOJu0Yz1h9ZDnaebzB3bU2hu9GUm6SJYmangEC8i5z8VDYQ7fMCpd3Y9 1nruYrwbpXLwYSsiVRyFq+7ag2wsntf4Br2xXHc= X-Google-Smtp-Source: AGHT+IGPFmpt8rnQmejMFmsgnH49il6+s+yQc51+YjF/XOlzs0Ym7FYbjGSz03rrNQNrEymwhjAqgg== X-Received: by 2002:a05:600c:446:b0:401:bbeb:97c4 with SMTP id s6-20020a05600c044600b00401bbeb97c4mr4508873wmb.37.1693488158693; Thu, 31 Aug 2023 06:22:38 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id i9-20020a05600011c900b0031c77c010e1sm2191511wrx.96.2023.08.31.06.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:38 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 4/6] devlink: return -ENOENT if argument is missing Date: Thu, 31 Aug 2023 15:22:27 +0200 Message-ID: <20230831132229.471693-5-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko In preparation to the follow-up dump selector patch, make sure that the command line arguments parsing function returns -ENOENT in case the option is missing so the caller can distinguish. Signed-off-by: Jiri Pirko --- devlink/devlink.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 8d2424f58cc2..6a46a4ecf648 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -1048,7 +1048,7 @@ static int strtobool(const char *str, bool *p_val) static int ident_str_validate(char *str, unsigned int expected) { if (!str) - return -EINVAL; + return -ENOENT; if (get_str_char_count(str, '/') != expected) { pr_err("Wrong identification string format.\n"); @@ -1131,7 +1131,7 @@ static int dl_argv_handle_port(struct dl *dl, char *str, char **p_bus_name, if (!str) { pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n"); - return -EINVAL; + return -ENOENT; } slash_count = get_str_char_count(str, '/'); switch (slash_count) { @@ -1159,7 +1159,7 @@ static int dl_argv_handle_both(struct dl *dl, char *str, char **p_bus_name, pr_err("One of following identifications expected:\n" "Devlink identification (\"bus_name/dev_name\")\n" "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n"); - return -EINVAL; + return -ENOENT; } slash_count = get_str_char_count(str, '/'); if (slash_count == 1) { @@ -1681,7 +1681,7 @@ static int dl_args_finding_required_validate(uint64_t o_required, o_flag = dl_args_required[i].o_flag; if ((o_required & o_flag) && !(o_found & o_flag)) { pr_err("%s\n", dl_args_required[i].err_msg); - return -EINVAL; + return -ENOENT; } } if (o_required & ~o_found) { From patchwork Thu Aug 31 13:22:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371592 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 4DE36125A8 for ; Thu, 31 Aug 2023 13:22:43 +0000 (UTC) Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1FDACEB for ; Thu, 31 Aug 2023 06:22:41 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-3110ab7110aso645824f8f.3 for ; Thu, 31 Aug 2023 06:22:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488160; x=1694092960; 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=K+nkXB1sUqI8gd72PD8v1L11a4OBB90zkZ3AR6jPFao=; b=rU7zVX/ZKLiC6O7/VUTAIN7WJW5kAVcjQ21oyIUnoV6YidTalZSnjUaAgnEYtsY/66 2UnfVbUydotwxkON0qmplGRKj04fsWwb7QCyJC+pnvTb6fo5t7TrrJS/Oav7UZa1f8nS HDAqwvg87CuKrJ8qvswqxMue7pd3oT9Fvidma7lO/XpKlCzoJURIJOnNBhFxjQ5RhHvt v3McVigXrvHEtVAB2+Gnz3ueNn6WH1CpZnBe7McJxJCwZMaf4Moo34dp2s9uP1xoo7Pl 2ny2OyX+xxlQpR8ZpacwAZZkOmOZt8Y4Vh8tW6Nzy1k1joaVcaPtLPFv25R606YFe9fu AHxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488160; x=1694092960; 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=K+nkXB1sUqI8gd72PD8v1L11a4OBB90zkZ3AR6jPFao=; b=SgUW5acnW+ptKPl5zUnSl5WHypIYCEoJT5VndC/oq/Sqp+m6oJw0VoOUwTykjEtW82 VB2twp4VjyvdwKy9TlKgTouQ6CIoDviri5Fk+NLayDiknHU3Vnz2wrA3cxXWpSHQxyBe ObooNz1GFpA2R6Oi5M8tnrD1zb0zlYB3QrFisZBxC661fkyyuqzu/0KZ+B6vb0CYhD4T aBE5Ns1D3YmTeUZlBsfRWFcoLabE/K7oMgmadtklq9aDzeOFZT4u4Bbp+RSZjNV2aK2Z WSSfVLYLYtpWg7+hRC5L9AA67pBEj7zE6lcSthXodXYDchhenHy1NVOQXxVB0O0ckF9b y+JA== X-Gm-Message-State: AOJu0YzaqHh4MX9DPvaHPArX9e1DHk3X4VUINJk8KyHoJTFFyFAZG/sc QcniDR9WUzLrfMufNsa8CLAEnZerOto8DAEmNzc= X-Google-Smtp-Source: AGHT+IEiJAYbYFlqAJC1UCGPl8Kgmouljt1mLvqyixT712mERuSLKLNvVk+4YS+hyp03/jKklZa+Xw== X-Received: by 2002:a5d:414f:0:b0:317:67bf:3376 with SMTP id c15-20020a5d414f000000b0031767bf3376mr4026312wrq.57.1693488160372; Thu, 31 Aug 2023 06:22:40 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id d4-20020adfe884000000b0030ae53550f5sm2206437wrm.51.2023.08.31.06.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:39 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 5/6] mnl_utils: introduce a helper to check if dump policy exists for command Date: Thu, 31 Aug 2023 15:22:28 +0200 Message-ID: <20230831132229.471693-6-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Benefit from GET_POLICY command of ctrl netlink and introduce a helper that dumps policies and finds out, if there is a separate policy specified for dump op of specified command. Signed-off-by: Jiri Pirko --- include/mnl_utils.h | 1 + lib/mnl_utils.c | 121 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/include/mnl_utils.h b/include/mnl_utils.h index 2193934849e1..76fe1dfec938 100644 --- a/include/mnl_utils.h +++ b/include/mnl_utils.h @@ -30,5 +30,6 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz mnl_cb_t cb, void *data); int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb, void *data); +int mnlu_gen_cmd_dump_policy(struct mnlu_gen_socket *nlg, uint8_t cmd); #endif /* __MNL_UTILS_H__ */ diff --git a/lib/mnl_utils.c b/lib/mnl_utils.c index f8e07d2f467f..1c78222828ff 100644 --- a/lib/mnl_utils.c +++ b/lib/mnl_utils.c @@ -110,7 +110,7 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz return err; } -static int get_family_attrs_cb(const struct nlattr *attr, void *data) +static int ctrl_attrs_cb(const struct nlattr *attr, void *data) { int type = mnl_attr_get_type(attr); const struct nlattr **tb = data; @@ -124,6 +124,12 @@ static int get_family_attrs_cb(const struct nlattr *attr, void *data) if (type == CTRL_ATTR_MAXATTR && mnl_attr_validate(attr, MNL_TYPE_U32) < 0) return MNL_CB_ERROR; + if (type == CTRL_ATTR_POLICY && + mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) + return MNL_CB_ERROR; + if (type == CTRL_ATTR_OP_POLICY && + mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) + return MNL_CB_ERROR; tb[type] = attr; return MNL_CB_OK; } @@ -134,7 +140,7 @@ static int get_family_cb(const struct nlmsghdr *nlh, void *data) struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; struct mnlu_gen_socket *nlg = data; - mnl_attr_parse(nlh, sizeof(*genl), get_family_attrs_cb, tb); + mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb); if (!tb[CTRL_ATTR_FAMILY_ID]) return MNL_CB_ERROR; if (!tb[CTRL_ATTR_MAXATTR]) @@ -252,3 +258,114 @@ int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb, MNL_SOCKET_BUFFER_SIZE, cb, data); } + +static int ctrl_policy_attrs_cb(const struct nlattr *attr, void *data) +{ + int type = mnl_attr_get_type(attr); + const struct nlattr **tb = data; + + if (mnl_attr_type_valid(attr, CTRL_ATTR_POLICY_DUMP_MAX) < 0) + return MNL_CB_ERROR; + + if (type == CTRL_ATTR_POLICY_DO && + mnl_attr_validate(attr, MNL_TYPE_U32) < 0) + return MNL_CB_ERROR; + if (type == CTRL_ATTR_POLICY_DUMP && + mnl_attr_validate(attr, MNL_TYPE_U32) < 0) + return MNL_CB_ERROR; + + tb[type] = attr; + return MNL_CB_OK; +} + +struct cmd_dump_policy_ctx { + uint8_t cmd; + uint8_t do_policy_idx_found:1, + dump_policy_idx_found:1; + uint32_t do_policy_idx; + uint32_t dump_policy_idx; + uint32_t dump_policy_attr_count; +}; + +static void process_dump_op_policy_nest(const struct nlattr *op_policy_nest, + struct cmd_dump_policy_ctx *ctx) +{ + struct nlattr *tb[CTRL_ATTR_POLICY_DUMP_MAX + 1] = {}; + const struct nlattr *attr; + int err; + + mnl_attr_for_each_nested(attr, op_policy_nest) { + if (ctx->cmd != (attr->nla_type & ~NLA_F_NESTED)) + continue; + err = mnl_attr_parse_nested(attr, ctrl_policy_attrs_cb, tb); + if (err != MNL_CB_OK) + continue; + if (tb[CTRL_ATTR_POLICY_DO]) { + ctx->do_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DO]); + ctx->do_policy_idx_found = true; + } + if (tb[CTRL_ATTR_POLICY_DUMP]) { + ctx->dump_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DUMP]); + ctx->dump_policy_idx_found = true; + } + break; + } +} + +static void process_dump_policy_nest(const struct nlattr *policy_nest, + struct cmd_dump_policy_ctx *ctx) +{ + const struct nlattr *attr; + + if (!ctx->dump_policy_idx_found) + return; + + mnl_attr_for_each_nested(attr, policy_nest) + if (ctx->dump_policy_idx == (attr->nla_type & ~NLA_F_NESTED)) + ctx->dump_policy_attr_count++; +} + +static int cmd_dump_policy_cb(const struct nlmsghdr *nlh, void *data) +{ + struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); + struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; + struct cmd_dump_policy_ctx *ctx = data; + + mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb); + if (!tb[CTRL_ATTR_FAMILY_ID]) + return MNL_CB_OK; + + if (tb[CTRL_ATTR_OP_POLICY]) + process_dump_op_policy_nest(tb[CTRL_ATTR_OP_POLICY], ctx); + + if (tb[CTRL_ATTR_POLICY]) + process_dump_policy_nest(tb[CTRL_ATTR_POLICY], ctx); + + return MNL_CB_OK; +} + +int mnlu_gen_cmd_dump_policy(struct mnlu_gen_socket *nlg, uint8_t cmd) +{ + struct cmd_dump_policy_ctx ctx = { + .cmd = cmd, + }; + struct nlmsghdr *nlh; + int err; + + nlh = _mnlu_gen_socket_cmd_prepare(nlg, CTRL_CMD_GETPOLICY, + NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP, + GENL_ID_CTRL, 1); + + mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->family); + + err = mnlu_gen_socket_sndrcv(nlg, nlh, cmd_dump_policy_cb, &ctx); + if (err) + return err; + + if (!ctx.dump_policy_idx_found || !ctx.do_policy_idx_found || + ctx.do_policy_idx == ctx.dump_policy_idx || + !ctx.dump_policy_attr_count) + return -ENOTSUP; + + return 0; +} From patchwork Thu Aug 31 13:22:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13371593 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 775FD125A8 for ; Thu, 31 Aug 2023 13:22:46 +0000 (UTC) Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D05C1A2 for ; Thu, 31 Aug 2023 06:22:44 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-401d67434daso7801485e9.2 for ; Thu, 31 Aug 2023 06:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1693488163; x=1694092963; 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=LcZbvPceAAvMnma0aYg1MIMzCOkiihvnqxJnmWALDQ4=; b=MKWehGyfQlnw85AJ7I7H45j27YqwL+gK0u8+rbRCY3O2rHYt4e1biqumctt4ZVcylf NMbv3UW/ixYL5WTRcb5zr3QyUw7Pq763f15LVOsEN5t52RgoJNwF7xNPofRi74LIFQl2 kYv4cklBwVHEdbM+I2CcHZxb/Mm3XRTA6YX/0JTJzHnCQElms9+5+j0FE5nXkF9nThZB imWhn2wiKUofjnYIsiNb1eteQcKKNJglHqc/F5veuQpPEG0LNyjhH0j9SHBvnnrRJWMK k265f09dul6Acc2qdrqREliREZUBRIR3D5a64WeiC7rMbi41OXGvczJH0V16P4ggQuvo JXdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693488163; x=1694092963; 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=LcZbvPceAAvMnma0aYg1MIMzCOkiihvnqxJnmWALDQ4=; b=AondChbwRXlSPSvGgDu9i0/E5MvUWHCMYBjWwGSWv3ofDN7bZ9HS2GsEEmn6uR6hlY iwfE0FNdRkk21ArRqPbrmtXUUlHBb/gLIPwZumGohrnAzwMD79KRMsqu2nV9HxFJQCVF /j57uXlxU4jslZrJ7b/pVIICgLFsm10imqudLOVcHSeAmMFk6Dog7USHuWAJwFL5qFlp 4Wggg4jETSLenEzz+ki9oseEOrntxhmxO5lE7XyZ0opx4HqTd3nbBmnPxzgyoEtQtoNz m1kqtIQyVgHHW7PQLb+ro3RojV76fO66BW4CGTxsI1bYTixbKhqfvDV1zBso1tYjkC7t 01Rw== X-Gm-Message-State: AOJu0Yy9Ert4qaJrsn00oS/3wXvsq6D+pRO+6VFdUYQWGu7TpuHPshwh aa8QfVbUguc/D003tKaxJMrGW8MU7kNwDkRTMrw= X-Google-Smtp-Source: AGHT+IEyBcPCwvQEmRmWCZ8s7ZLnru6F/thYtjVT0LMAXXUYzy/mg0UdWFxs+b1SjpqziCbpLNdOWQ== X-Received: by 2002:a5d:595f:0:b0:317:5b32:b2c3 with SMTP id e31-20020a5d595f000000b003175b32b2c3mr4270405wri.6.1693488162890; Thu, 31 Aug 2023 06:22:42 -0700 (PDT) Received: from localhost ([212.23.236.67]) by smtp.gmail.com with ESMTPSA id g10-20020a7bc4ca000000b003fe1a092925sm1978809wmk.19.2023.08.31.06.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:22:42 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [patch iproute2-next 6/6] devlink: implement dump selector for devlink objects show commands Date: Thu, 31 Aug 2023 15:22:29 +0200 Message-ID: <20230831132229.471693-7-jiri@resnulli.us> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us> References: <20230831132229.471693-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: dsahern@gmail.com From: Jiri Pirko Introduce a new helper dl_argv_parse_with_selector() to be used by show() functions instead of dl_argv(). Implement it to check if all needed options got get commands are specified. In case they are not, ask kernel for dump passing only the options (attributes) that are present, creating sort of partial key to instruct kernel to do partial dump. Signed-off-by: Jiri Pirko --- devlink/devlink.c | 283 +++++++++++++++++++++++++++------------------- 1 file changed, 164 insertions(+), 119 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 6a46a4ecf648..066aab12c6f8 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -2302,6 +2302,88 @@ static int dl_argv_dry_parse(struct dl *dl, uint64_t o_required, return err; } +/* List of extented handles with two slashes. */ +static const uint64_t dl_opt_extended_handle[] = { + DL_OPT_HANDLEP, + DL_OPT_HANDLE_REGION, + DL_OPT_PORT_FN_RATE_NODE_NAME, +}; + +static int dl_argv_parse_with_selector(struct dl *dl, uint16_t *flags, + uint8_t cmd, + uint64_t o_required, + uint64_t o_optional, + uint64_t o_dump_required, + uint64_t o_dump_optional) +{ + int err; + int i; + + if (dl_no_arg(dl)) + goto flag_set; + + /* In case the handle suggests it, do dry parsing first + * to see if all required options are there. Proceed with + * dump selector in case there are missing options on the + * command line. That means user provided partial + * object identification. + */ + + if ((o_required & (DL_OPT_HANDLE | DL_OPT_HANDLEP)) == + (DL_OPT_HANDLE | DL_OPT_HANDLEP)) { + /* Handle case when both devlink handle and port handle + * are allowed. Try both alone, if parsing of either + * is successful, we have a do parse case. + */ + err = dl_argv_dry_parse(dl, o_required & ~DL_OPT_HANDLEP, + o_optional); + if (err == -ENOENT) + goto dump_parse; + else if (!err) + goto do_parse; + err = dl_argv_dry_parse(dl, o_required & ~DL_OPT_HANDLE, + o_optional); + if (err == -ENOENT) + goto dump_parse; + goto do_parse; + } + + for (i = 0; i < ARRAY_SIZE(dl_opt_extended_handle); i++) { + uint64_t handle = dl_opt_extended_handle[i]; + + if ((o_required & handle) == handle) { + err = dl_argv_dry_parse(dl, (o_required & ~handle) | + DL_OPT_HANDLE, + o_optional); + if (err == -ENOENT || !err) + goto dump_parse; + goto do_parse; + } + } + + err = dl_argv_dry_parse(dl, o_required, o_optional); + if (err == -ENOENT) + goto dump_parse; + +do_parse: + return dl_argv_parse(dl, o_required, o_optional); + +dump_parse: + err = mnlu_gen_cmd_dump_policy(&dl->nlg, cmd); + if (err) { + pr_err("Dump selectors are not supported by kernel for this command\n"); + return -ENOTSUP; + } + + err = dl_argv_parse(dl, o_dump_required, o_dump_optional); + if (err) + return err; + +flag_set: + *flags |= NLM_F_DUMP; + return 0; +} + static void dl_function_attr_put(struct nlmsghdr *nlh, const struct dl_opts *opts) { @@ -3580,13 +3662,11 @@ static int cmd_dev_param_show(struct dl *dl) struct nlmsghdr *nlh; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } else { - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_PARAM_NAME, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_PARAM_GET, + DL_OPT_HANDLE | DL_OPT_PARAM_NAME, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_PARAM_GET, flags); @@ -4784,14 +4864,11 @@ static int cmd_port_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_PORT_GET, + DL_OPT_HANDLEP, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_PORT_GET, flags); @@ -4860,14 +4937,12 @@ static int cmd_port_param_show(struct dl *dl) struct nlmsghdr *nlh; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_PARAM_NAME, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_PORT_PARAM_GET, + DL_OPT_HANDLE | DL_OPT_PARAM_NAME, 0, + DL_OPT_HANDLE | DL_OPT_HANDLEP, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_PORT_PARAM_GET, flags); @@ -5234,16 +5309,9 @@ static int cmd_port_fn_rate_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, - DL_OPT_HANDLEP | DL_OPT_PORT_FN_RATE_NODE_NAME, - 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_RATE_GET, + DL_OPT_HANDLEP | DL_OPT_PORT_FN_RATE_NODE_NAME, + 0, DL_OPT_HANDLE, 0); nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_RATE_GET, flags); @@ -5622,14 +5690,11 @@ static int cmd_linecard_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_LINECARD); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_LINECARD_GET, + DL_OPT_HANDLE | DL_OPT_LINECARD, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_LINECARD_GET, flags); @@ -5740,14 +5805,11 @@ static int cmd_sb_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_SB_GET, + DL_OPT_HANDLE | DL_OPT_SB, 0, + DL_OPT_HANDLE, DL_OPT_SB); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_SB_GET, flags); @@ -5819,15 +5881,12 @@ static int cmd_sb_pool_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_SB | - DL_OPT_SB_POOL, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_SB_POOL_GET, + DL_OPT_HANDLE | DL_OPT_SB | + DL_OPT_SB_POOL, 0, + DL_OPT_HANDLE, DL_OPT_SB); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_SB_POOL_GET, flags); @@ -5908,15 +5967,13 @@ static int cmd_sb_port_pool_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | - DL_OPT_SB_POOL, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_SB_PORT_POOL_GET, + DL_OPT_HANDLEP | DL_OPT_SB | + DL_OPT_SB_POOL, 0, + DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags); @@ -6015,15 +6072,14 @@ static int cmd_sb_tc_bind_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLEP | DL_OPT_SB | DL_OPT_SB_TC | - DL_OPT_SB_TYPE, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_SB_TC_POOL_BIND_GET, + DL_OPT_HANDLEP | DL_OPT_SB | + DL_OPT_SB_TC | DL_OPT_SB_TYPE, 0, + DL_OPT_HANDLE | DL_OPT_HANDLEP, + DL_OPT_SB); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags); @@ -8697,14 +8753,11 @@ static int cmd_region_show(struct dl *dl) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLE_REGION, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_REGION_GET, + DL_OPT_HANDLE_REGION, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_REGION_GET, flags); @@ -9388,19 +9441,20 @@ static int __cmd_health_show(struct dl *dl, bool show_device, bool show_port) uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } else { + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_HEALTH_REPORTER_GET, + DL_OPT_HANDLE | DL_OPT_HANDLEP | + DL_OPT_HEALTH_REPORTER_NAME, 0, + DL_OPT_HANDLE | DL_OPT_HANDLEP, 0); + if (err) + return err; + + if (!(flags & NLM_F_DUMP)) ctx.show_port = true; - err = dl_argv_parse(dl, - DL_OPT_HANDLE | DL_OPT_HANDLEP | - DL_OPT_HEALTH_REPORTER_NAME, 0); - if (err) - return err; - } nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_GET, flags); + dl_opts_put(nlh, dl); pr_out_section_start(dl, "health"); @@ -9582,14 +9636,11 @@ static int cmd_trap_show(struct dl *dl) struct nlmsghdr *nlh; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_TRAP_NAME, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_TRAP_GET, + DL_OPT_HANDLE | DL_OPT_TRAP_NAME, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_TRAP_GET, flags); @@ -9660,15 +9711,12 @@ static int cmd_trap_group_show(struct dl *dl) struct nlmsghdr *nlh; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, - DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_TRAP_GROUP_GET, + DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_TRAP_GROUP_GET, flags); @@ -9759,15 +9807,12 @@ static int cmd_trap_policer_show(struct dl *dl) struct nlmsghdr *nlh; int err; - if (dl_no_arg(dl)) { - flags |= NLM_F_DUMP; - } - else { - err = dl_argv_parse(dl, - DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID, 0); - if (err) - return err; - } + err = dl_argv_parse_with_selector(dl, &flags, + DEVLINK_CMD_TRAP_POLICER_GET, + DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID, 0, + DL_OPT_HANDLE, 0); + if (err) + return err; nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_TRAP_POLICER_GET, flags);