From patchwork Fri Jun 10 14:24:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 112655 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 25AFDA0553; Fri, 10 Jun 2022 16:24:22 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E55AC42802; Fri, 10 Jun 2022 16:24:18 +0200 (CEST) Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mails.dpdk.org (Postfix) with ESMTP id 7237B427F6 for ; Fri, 10 Jun 2022 16:24:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1654871057; x=1686407057; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vpgMxOVhel/h+DKQfVmpc33FcAVgmiobLizOcNMS7A0=; b=VKL4l+fNZW1HcQOM5m1xhhebbSYvkm8UWphxjbsFdcJURtjyqBA5NYWA 0vCVGGqv5Aq0lB9SndOxPeMUbvAZu2g1hH3Bxni2uY1WV7ERdeKUbNlvi H+8eGhaHvYN9DcQf9FH2717HQssgfYMBsUzbZ80BxJfuE9Xwwx81Z22uw lkwet4DAqKH/NbN5CstcIrCkc3WgRg/rRBRxkwvGCvYZ94rG1VNE1wf9T ugeDJz+JdDSZdHXnP4V1HorEGDUDGEoxA+D8+sLTFSGn0ZCOV/rDksN3p kvDoDWxF3YrsVO6ZX3v2MQVRpd7s1nm2sigSY+Uu1p+9BBboI14G9fLnx w==; X-IronPort-AV: E=McAfee;i="6400,9594,10373"; a="258079381" X-IronPort-AV: E=Sophos;i="5.91,290,1647327600"; d="scan'208";a="258079381" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jun 2022 07:24:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,290,1647327600"; d="scan'208";a="684568702" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.181]) by fmsmga002.fm.intel.com with ESMTP; 10 Jun 2022 07:24:15 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: olivier.matz@6wind.com, Bruce Richardson , Weiyuan Li Subject: [PATCH v3 1/2] cmdline: add function to verify valid commands Date: Fri, 10 Jun 2022 15:24:05 +0100 Message-Id: <20220610142406.580943-2-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220610142406.580943-1-bruce.richardson@intel.com> References: <20220520145631.137962-1-bruce.richardson@intel.com> <20220610142406.580943-1-bruce.richardson@intel.com> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it. To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only. Signed-off-by: Bruce Richardson Tested-by: Weiyuan Li Acked-by: Olivier Matz --- lib/cmdline/cmdline_parse.c | 28 ++++++++++++++++++++-------- lib/cmdline/cmdline_parse.h | 17 +++++++++++++++-- lib/cmdline/version.map | 3 +++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/cmdline/cmdline_parse.c b/lib/cmdline/cmdline_parse.c index 349ec87bd7..ba7f4b80cb 100644 --- a/lib/cmdline/cmdline_parse.c +++ b/lib/cmdline/cmdline_parse.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf, } -int -cmdline_parse(struct cmdline *cl, const char * buf) +static inline int +__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn) { unsigned int inst_num=0; cmdline_parse_inst_t *inst; @@ -282,20 +283,31 @@ cmdline_parse(struct cmdline *cl, const char * buf) inst = ctx[inst_num]; } - /* call func */ - if (f) { - f(result.buf, cl, data); - } - /* no match */ - else { + if (f == NULL) { debug_printf("No match err=%d\n", err); return err; } + /* call func if requested*/ + if (call_fn) + f(result.buf, cl, data); + return linelen; } +int +cmdline_parse(struct cmdline *cl, const char *buf) +{ + return __cmdline_parse(cl, buf, true); +} + +int +cmdline_parse_check(struct cmdline *cl, const char *buf) +{ + return __cmdline_parse(cl, buf, false); +} + int cmdline_complete(struct cmdline *cl, const char *buf, int *state, char *dst, unsigned int size) diff --git a/lib/cmdline/cmdline_parse.h b/lib/cmdline/cmdline_parse.h index e4d802fff7..f4241a68fe 100644 --- a/lib/cmdline/cmdline_parse.h +++ b/lib/cmdline/cmdline_parse.h @@ -7,6 +7,8 @@ #ifndef _CMDLINE_PARSE_H_ #define _CMDLINE_PARSE_H_ +#include + #ifdef __cplusplus extern "C" { #endif @@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; * argument buf must ends with "\n\0". The function returns * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated - * function (defined in the context) and returns 0 - * (CMDLINE_PARSE_SUCCESS). + * function (defined in the context) and returns the parsed line length (>= 0). */ int cmdline_parse(struct cmdline *cl, const char *buf); +/** + * Try to parse a buffer according to the specified context, but do not + * perform any function calls if parse is successful. + * + * The argument buf must ends with "\n\0". + * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or + * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0) + * on successful parse. + */ +__rte_experimental +int cmdline_parse_check(struct cmdline *cl, const char *buf); + /** * complete() must be called with *state==0 (try to complete) or * with *state==-1 (just display choices), then called without diff --git a/lib/cmdline/version.map b/lib/cmdline/version.map index b9bbb87510..fc7fdd6ea4 100644 --- a/lib/cmdline/version.map +++ b/lib/cmdline/version.map @@ -81,5 +81,8 @@ EXPERIMENTAL { rdline_get_history_buffer_size; rdline_get_opaque; + # added in 22.07 + cmdline_parse_check; + local: *; }; From patchwork Fri Jun 10 14:24:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 112656 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id DD97CA0553; Fri, 10 Jun 2022 16:24:29 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3808E427E9; Fri, 10 Jun 2022 16:24:21 +0200 (CEST) Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mails.dpdk.org (Postfix) with ESMTP id D808A4281A for ; Fri, 10 Jun 2022 16:24:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1654871060; x=1686407060; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RtERYlHE0ndwzh/8iBxgPrltN9s4LtBWzTm/nG+13nM=; b=Yt2HyDyJiqo5PDGy1KbWumQT3t4dJdEcCLeiD5uT6BYRjYUBWQIKqM1e EH7AyR0reWiAdKw2hX5mDvmTEQ37tRtvlRbpiyrSbhNuCfJkoZXYkor9x 3SKErezxx1Cit7c01RTU+eH4gss9Y2Rf5apO0p3MqdGuxR4IMckoOVh+C seBwm5FSvb8Y4qk1a1ZGEOSIopoLkvbKl0+hek/L/RvIWnnX440zUzvxs G48T9SVbY/vEoOpelI32wrWUfl5gog7xltD4nRR+uo1ZSKjkhlONF9ZOG u48PTkoi1eqQtW9edRH6gA0rLpa5ZtnDU07VVtbDkz7tLeyvjeXIdASHN w==; X-IronPort-AV: E=McAfee;i="6400,9594,10373"; a="258079392" X-IronPort-AV: E=Sophos;i="5.91,290,1647327600"; d="scan'208";a="258079392" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jun 2022 07:24:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,290,1647327600"; d="scan'208";a="684568727" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.181]) by fmsmga002.fm.intel.com with ESMTP; 10 Jun 2022 07:24:18 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: olivier.matz@6wind.com, Bruce Richardson Subject: [PATCH v3 2/2] test: use cmdline library to validate args Date: Fri, 10 Jun 2022 15:24:06 +0100 Message-Id: <20220610142406.580943-3-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220610142406.580943-1-bruce.richardson@intel.com> References: <20220520145631.137962-1-bruce.richardson@intel.com> <20220610142406.580943-1-bruce.richardson@intel.com> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org When passing in test names to run via either the DPDK_TEST environment variable or via extra argv parameters, the checks run on those commands can miss valid commands that are registered with the cmdline library in the initial context used to set it up. This is seen in the fact that the "dump_*" set of commands are not callable via argv parameters, but can be called manually. To fix this, just use the commandline library to validate each command before executing it, stopping execution when an error is encountered. This also has the benefit of not having the test binray drop to interactive mode if all commandline parameters given are invalid. Fixes: 9b848774a5dc ("test: use env variable to run tests") Fixes: ace2f054ed43 ("test: take test names from command line") Bugzilla ID: 1002 Signed-off-by: Bruce Richardson Acked-by: Olivier Matz --- app/test/commands.c | 11 ----------- app/test/test.c | 24 +++++++----------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/app/test/commands.c b/app/test/commands.c index 887cabad64..31259e5c21 100644 --- a/app/test/commands.c +++ b/app/test/commands.c @@ -378,14 +378,3 @@ int commands_init(void) cmd_autotest_autotest.string_data.str = commands; return 0; } - -int command_valid(const char *cmd) -{ - struct test_command *t; - - TAILQ_FOREACH(t, &commands_list, next) { - if (strcmp(t->command, cmd) == 0) - return 1; - } - return 0; -} diff --git a/app/test/test.c b/app/test/test.c index 8a7dddde9b..fb073ff795 100644 --- a/app/test/test.c +++ b/app/test/test.c @@ -186,22 +186,10 @@ main(int argc, char **argv) #ifdef RTE_LIB_CMDLINE char *dpdk_test = getenv("DPDK_TEST"); - if (dpdk_test && strlen(dpdk_test) == 0) - dpdk_test = NULL; - - if (dpdk_test && !command_valid(dpdk_test)) { - RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test); - dpdk_test = NULL; - } - - if (dpdk_test) + if (dpdk_test && strlen(dpdk_test) > 0) tests[test_count++] = dpdk_test; - for (i = 1; i < argc; i++) { - if (!command_valid(argv[i])) - RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]); - else - tests[test_count++] = argv[i]; - } + for (i = 1; i < argc; i++) + tests[test_count++] = argv[i]; if (test_count > 0) { char buf[1024]; @@ -214,9 +202,11 @@ main(int argc, char **argv) for (i = 0; i < test_count; i++) { snprintf(buf, sizeof(buf), "%s\n", tests[i]); - if (cmdline_in(cl, buf, strlen(buf)) < 0) { + if (cmdline_parse_check(cl, buf) < 0) { + printf("Error: invalid test command: '%s'\n", tests[i]); + ret = -1; + } else if (cmdline_in(cl, buf, strlen(buf)) < 0) { printf("error on cmdline input\n"); - ret = -1; } else ret = last_test_result;