DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Aman Singh <aman.deep.singh@intel.com>,
	Yuying Zhang <yuying.zhang@intel.com>,
	Olivier Matz <olivier.matz@6wind.com>,
	Ferruh Yigit <ferruh.yigit@amd.com>
Subject: [PATCH v5] testpmd: go back to using cmdline_interact
Date: Fri, 17 Mar 2023 09:59:41 -0700	[thread overview]
Message-ID: <20230317165941.62619-1-stephen@networkplumber.org> (raw)
In-Reply-To: <20230313171905.67720-1-stephen@networkplumber.org>

The cmdline_poll() function is broken and was
not fully tested, don't use it.

Instead, use sigaction() to cancel read character on Unix OS's
and a new helper to cancel I/O on Windows.

Fixes: 0fd1386c30c3 ("app/testpmd: cleanup cleanly from signal")
Bugzilla ID: 1180
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
v5 - Do not need to expose cmdline_cancel() as user api
     can make cmdline_quit() do it.

     Change to control C handling is separate issue and
     should be discussed more.

 app/test-pmd/cmdline.c           | 26 +++++++++++++-------------
 app/test-pmd/testpmd.c           | 11 +++++++++++
 lib/cmdline/cmdline.c            |  1 +
 lib/cmdline/cmdline_os_unix.c    |  6 ++++++
 lib/cmdline/cmdline_os_windows.c | 14 ++++++++++++++
 lib/cmdline/cmdline_private.h    |  5 ++++-
 6 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 6fa870dc329b..7b20bef4e9ba 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -66,6 +66,7 @@
 #include "cmdline_tm.h"
 #include "bpf_cmd.h"
 
+static struct cmdline *testpmd_cl;
 static cmdline_parse_ctx_t *main_ctx;
 static TAILQ_HEAD(, testpmd_driver_commands) driver_commands_head =
 	TAILQ_HEAD_INITIALIZER(driver_commands_head);
@@ -13028,26 +13029,25 @@ cmdline_read_from_file(const char *filename)
 	printf("Read CLI commands from %s\n", filename);
 }
 
+void
+prompt_exit(void)
+{
+	cmdline_quit(testpmd_cl);
+}
+
 /* prompt function, called from main on MAIN lcore */
 void
 prompt(void)
 {
-	struct cmdline *cl;
-
-	cl = cmdline_stdin_new(main_ctx, "testpmd> ");
-	if (cl == NULL)
+	testpmd_cl = cmdline_stdin_new(main_ctx, "testpmd> ");
+	if (testpmd_cl == NULL) {
+		fprintf(stderr,
+			"Failed to create stdin based cmdline context\n");
 		return;
-
-	/* loop until signal or quit command */
-	while (f_quit == 0 && cl_quit == 0) {
-		int status = cmdline_poll(cl);
-
-		if (status < 0 || status == RDLINE_EXITED)
-			break;
 	}
 
-	cmdline_quit(cl);
-	cmdline_stdin_exit(cl);
+	cmdline_interact(testpmd_cl);
+	cmdline_stdin_exit(testpmd_cl);
 }
 
 void
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 2ce19ed47ab4..5cb6f9252395 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -4469,6 +4469,7 @@ static void
 signal_handler(int signum __rte_unused)
 {
 	f_quit = 1;
+	prompt_exit();
 }
 
 int
@@ -4479,8 +4480,18 @@ main(int argc, char** argv)
 	uint16_t count;
 	int ret;
 
+#ifdef RTE_EXEC_ENV_WINDOWS
 	signal(SIGINT, signal_handler);
 	signal(SIGTERM, signal_handler);
+#else
+	/* Want read() not to be restarted on signal */
+	struct sigaction action = {
+		.sa_handler = signal_handler,
+	};
+
+	sigaction(SIGINT, &action, NULL);
+	sigaction(SIGTERM, &action, NULL);
+#endif
 
 	testpmd_logtype = rte_log_register("testpmd");
 	if (testpmd_logtype < 0)
diff --git a/lib/cmdline/cmdline.c b/lib/cmdline/cmdline.c
index 8ad0690d8533..355c7d8ca635 100644
--- a/lib/cmdline/cmdline.c
+++ b/lib/cmdline/cmdline.c
@@ -173,6 +173,7 @@ cmdline_quit(struct cmdline *cl)
 {
 	if (!cl)
 		return;
+	cmdline_cancel(cl);
 	rdline_quit(&cl->rdl);
 }
 
diff --git a/lib/cmdline/cmdline_os_unix.c b/lib/cmdline/cmdline_os_unix.c
index 64a945a34fb3..9a4ec4e33477 100644
--- a/lib/cmdline/cmdline_os_unix.c
+++ b/lib/cmdline/cmdline_os_unix.c
@@ -51,3 +51,9 @@ cmdline_vdprintf(int fd, const char *format, va_list op)
 {
 	return vdprintf(fd, format, op);
 }
+
+/* This function is not needed on Linux, instead use sigaction() */
+void
+cmdline_cancel(__rte_unused struct cmdline *cl)
+{
+}
diff --git a/lib/cmdline/cmdline_os_windows.c b/lib/cmdline/cmdline_os_windows.c
index 73ed9ba290b8..80863bfc8a00 100644
--- a/lib/cmdline/cmdline_os_windows.c
+++ b/lib/cmdline/cmdline_os_windows.c
@@ -203,3 +203,17 @@ cmdline_vdprintf(int fd, const char *format, va_list op)
 
 	return ret;
 }
+
+void
+cmdline_cancel(struct cmdline *cl)
+{
+	if (!cl)
+		return;
+
+	/* force the outstanding read on console to exit */
+	if (cl->oldterm.is_console_input) {
+		HANDLE handle = (HANDLE)_get_osfhandle(cl->s_in);
+
+		CancelIoEx(handle, NULL);
+	}
+}
diff --git a/lib/cmdline/cmdline_private.h b/lib/cmdline/cmdline_private.h
index a3271c76934a..86a46cdea61a 100644
--- a/lib/cmdline/cmdline_private.h
+++ b/lib/cmdline/cmdline_private.h
@@ -24,7 +24,7 @@
 #define RDLINE_HISTORY_MAX_LINE 64
 
 struct rdline {
-	enum rdline_status status;
+	volatile enum rdline_status status;
 	/* rdline bufs */
 	struct cirbuf left;
 	struct cirbuf right;
@@ -90,6 +90,9 @@ int cmdline_poll_char(struct cmdline *cl);
 /* Read one character from input. */
 ssize_t cmdline_read_char(struct cmdline *cl, char *c);
 
+/* Force current cmdline read to unblock. */
+void cmdline_cancel(struct cmdline *cl);
+
 /* vdprintf(3) */
 __rte_format_printf(2, 0)
 int cmdline_vdprintf(int fd, const char *format, va_list op);
-- 
2.39.2


  parent reply	other threads:[~2023-03-17 16:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-13 17:19 [RFT] test-pmd: " Stephen Hemminger
2023-03-13 21:38 ` [RFT v2] " Stephen Hemminger
2023-03-14  7:06   ` Ling, WeiX
     [not found]   ` <MW4PR12MB56684FA660218F925D64BE09A4BE9@MW4PR12MB5668.namprd12.prod.outlook.com>
2023-03-14 12:07     ` Pier Damouny
2023-03-14 17:36 ` [PATCH v3 0/3] testpmd control C fixes Stephen Hemminger
2023-03-14 17:36   ` [PATCH v3 1/3] testpmd: go back to using cmdline_interact Stephen Hemminger
2023-03-14 17:36   ` [PATCH v3 2/3] testpmd: use sig_atomic_t for flag Stephen Hemminger
2023-03-15 16:31     ` Stephen Hemminger
2023-03-14 17:36   ` [PATCH v3 3/3] testpmd: enable interrupt in interactive mode Stephen Hemminger
2023-03-14 22:40   ` [PATCH v3 0/3] testpmd control C fixes Stephen Hemminger
2023-03-15  9:46     ` David Marchand
2023-03-15 17:31 ` [PATCH v4 0/2] Fix testpmd interrupt regression Stephen Hemminger
2023-03-15 17:31   ` [PATCH v4 1/2] testpmd: go back to using cmdline_interact Stephen Hemminger
2023-03-17 16:20     ` Olivier Matz
2023-03-17 16:38       ` Stephen Hemminger
2023-03-15 17:31   ` [PATCH v4 2/2] testpmd: enable interrupt in interactive mode Stephen Hemminger
2023-03-17 16:20     ` Olivier Matz
2023-03-17 16:25       ` Stephen Hemminger
2023-03-16  8:16   ` [PATCH v4 0/2] Fix testpmd interrupt regression Pier Damouny
2023-03-16 12:20   ` Ferruh Yigit
2023-03-16 15:31     ` Stephen Hemminger
2023-03-16 17:01       ` Ferruh Yigit
2023-03-16 17:05         ` Thomas Monjalon
2023-03-16 17:36           ` Ferruh Yigit
2023-03-16 17:27   ` [PATCH] app/testpmd: revert cleanup cleanly from signal Ferruh Yigit
2023-03-17 16:59 ` Stephen Hemminger [this message]
2023-03-17 21:12   ` [PATCH v5] testpmd: go back to using cmdline_interact Olivier Matz
2023-03-19 23:28     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230317165941.62619-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=aman.deep.singh@intel.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=olivier.matz@6wind.com \
    --cc=yuying.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).