[v9] testpmd: cleanup cleanly from signal

Message ID 20230125183205.96554-1-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers
Series [v9] testpmd: cleanup cleanly from signal |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-mellanox-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/github-robot: build fail github build: failed
ci/iol-aarch64-compile-testing success Testing PASS

Commit Message

Stephen Hemminger Jan. 25, 2023, 6:32 p.m. UTC
  Do a clean shutdown of testpmd when a signal is received; instead of
having testpmd kill itself.  This fixes the problem where a signal could
be received in the middle of a PMD and then the signal handler would
call PMD's close routine leading to locking problems.

An added benefit is it gets rid of some Windows specific code.

Fixes: d9a191a00e81 ("app/testpmd: fix quitting in container")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
v9 - also handle the interactive case.
     use cmdine_poll() rather than signals and atexit

 app/test-pmd/cmdline.c | 28 +++++----------
 app/test-pmd/testpmd.c | 77 ++++++++++++++++++++----------------------
 app/test-pmd/testpmd.h |  1 +
 3 files changed, 46 insertions(+), 60 deletions(-)
  

Comments

Ferruh Yigit Jan. 30, 2023, 6:48 p.m. UTC | #1
On 1/25/2023 6:32 PM, Stephen Hemminger wrote:
> Do a clean shutdown of testpmd when a signal is received; instead of
> having testpmd kill itself.  This fixes the problem where a signal could
> be received in the middle of a PMD and then the signal handler would
> call PMD's close routine leading to locking problems.
> 
> An added benefit is it gets rid of some Windows specific code.
> 
> Fixes: d9a191a00e81 ("app/testpmd: fix quitting in container")
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Patch looks good to me, but './devtools/test-null.sh' hangs with change.

It is possible the fix by updating './devtools/test-null.sh', but my
concern is if this behavior change hit more consumers other than this
internal tool, and I am for keeping previous behavior.

'./devtools/test-null.sh' sends 'stop' command to testpmd but that seems
not really what makes testpmd quit, because following change still works
with original testpmd code:
 -(sleep 1 && echo stop) |
 +(sleep 1 && echo help) |
Somehow testpmd gets Ctrl-D or equivalent with above command.

And it seems because of 'cmdline_interact()' and 'cmdline_poll()'
difference behavior changes with above command.

It is possible to add something like  'cmdline_interact_one()' (non loop
version of 'cmdline_interact()'), but not really sure that is correct
way to fix the issue.


> ---
> v9 - also handle the interactive case.
>      use cmdine_poll() rather than signals and atexit
> 
>  app/test-pmd/cmdline.c | 28 +++++----------
>  app/test-pmd/testpmd.c | 77 ++++++++++++++++++++----------------------
>  app/test-pmd/testpmd.h |  1 +
>  3 files changed, 46 insertions(+), 60 deletions(-)
> 
> diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
> index b32dc8bfd445..fbe9ad694dee 100644
> --- a/app/test-pmd/cmdline.c
> +++ b/app/test-pmd/cmdline.c
> @@ -65,7 +65,6 @@
>  #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);
> @@ -12921,28 +12920,19 @@ cmdline_read_from_file(const char *filename)
>  void
>  prompt(void)
>  {
> -	int ret;
> +	struct cmdline *cl;
>  
> -	testpmd_cl = cmdline_stdin_new(main_ctx, "testpmd> ");
> -	if (testpmd_cl == NULL)
> +	cl = cmdline_stdin_new(main_ctx, "testpmd> ");
> +	if (cl == NULL)
>  		return;
>  
> -	ret = atexit(prompt_exit);
> -	if (ret != 0)
> -		fprintf(stderr, "Cannot set exit function for cmdline\n");
> -
> -	cmdline_interact(testpmd_cl);
> -	if (ret != 0)
> -		cmdline_stdin_exit(testpmd_cl);
> -}
> -
> -void
> -prompt_exit(void)
> -{
> -	if (testpmd_cl != NULL) {
> -		cmdline_quit(testpmd_cl);
> -		cmdline_stdin_exit(testpmd_cl);
> +	while (f_quit == 0 && cl_quit == 0) {
> +		if (cmdline_poll(cl) < 0)
> +			break;
>  	}
> +
> +	cmdline_quit(cl);
> +	cmdline_stdin_exit(cl);
>  }
>  
>  void
> diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
> index 134d79a55547..d01b6105b7de 100644
> --- a/app/test-pmd/testpmd.c
> +++ b/app/test-pmd/testpmd.c
> @@ -11,6 +11,7 @@
>  #include <fcntl.h>
>  #ifndef RTE_EXEC_ENV_WINDOWS
>  #include <sys/mman.h>
> +#include <sys/select.h>
>  #endif
>  #include <sys/types.h>
>  #include <errno.h>
> @@ -231,7 +232,7 @@ unsigned int xstats_display_num; /**< Size of extended statistics to show */
>   * In container, it cannot terminate the process which running with 'stats-period'
>   * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
>   */
> -static volatile uint8_t f_quit;
> +volatile uint8_t f_quit;
>  uint8_t cl_quit; /* Quit testpmd from cmdline. */
>  
>  /*
> @@ -4315,13 +4316,6 @@ init_port(void)
>  	memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
>  }
>  
> -static void
> -force_quit(void)
> -{
> -	pmd_test_exit();
> -	prompt_exit();
> -}
> -
>  static void
>  print_stats(void)
>  {
> @@ -4340,28 +4334,9 @@ print_stats(void)
>  }
>  
>  static void
> -signal_handler(int signum)
> +signal_handler(int signum __rte_unused)
>  {
> -	if (signum == SIGINT || signum == SIGTERM) {
> -		fprintf(stderr, "\nSignal %d received, preparing to exit...\n",
> -			signum);
> -#ifdef RTE_LIB_PDUMP
> -		/* uninitialize packet capture framework */
> -		rte_pdump_uninit();
> -#endif
> -#ifdef RTE_LIB_LATENCYSTATS
> -		if (latencystats_enabled != 0)
> -			rte_latencystats_uninit();
> -#endif
> -		force_quit();
> -		/* Set flag to indicate the force termination. */
> -		f_quit = 1;
> -		/* exit with the expected status */
> -#ifndef RTE_EXEC_ENV_WINDOWS
> -		signal(signum, SIG_DFL);
> -		kill(getpid(), signum);
> -#endif
> -	}
> +	f_quit = 1;
>  }
>  
>  int
> @@ -4536,15 +4511,9 @@ main(int argc, char** argv)
>  			start_packet_forwarding(0);
>  		}
>  		prompt();
> -		pmd_test_exit();
>  	} else
>  #endif
>  	{
> -		char c;
> -		int rc;
> -
> -		f_quit = 0;
> -
>  		printf("No commandline core given, start packet forwarding\n");
>  		start_packet_forwarding(tx_first);
>  		if (stats_period != 0) {
> @@ -4567,15 +4536,41 @@ main(int argc, char** argv)
>  				prev_time = cur_time;
>  				rte_delay_us_sleep(US_PER_S);
>  			}
> -		}
> +		} else {
> +			char c;
> +			fd_set fds;
> +
> +			printf("Press enter to exit\n");
>  
> -		printf("Press enter to exit\n");
> -		rc = read(0, &c, 1);
> -		pmd_test_exit();
> -		if (rc < 0)
> -			return 1;
> +			FD_ZERO(&fds);
> +			FD_SET(0, &fds);
> +
> +			/* wait for signal or enter */
> +			ret = select(1, &fds, NULL, NULL, NULL);
> +			if (ret < 0 && errno != EINTR)
> +				rte_exit(EXIT_FAILURE,
> +					 "Select failed: %s\n",
> +					 strerror(errno));
> +
> +			/* if got enter then consume it */
> +			if (ret == 1 && read(0, &c, 1) < 0)
> +				rte_exit(EXIT_FAILURE,
> +					 "Read failed: %s\n",
> +					 strerror(errno));
> +		}
>  	}
>  
> +	pmd_test_exit();
> +
> +#ifdef RTE_LIB_PDUMP
> +	/* uninitialize packet capture framework */
> +	rte_pdump_uninit();
> +#endif
> +#ifdef RTE_LIB_LATENCYSTATS
> +	if (latencystats_enabled != 0)
> +		rte_latencystats_uninit();
> +#endif
> +
>  	ret = rte_eal_cleanup();
>  	if (ret != 0)
>  		rte_exit(EXIT_FAILURE,
> diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
> index 7d24d25970d2..022210a7a964 100644
> --- a/app/test-pmd/testpmd.h
> +++ b/app/test-pmd/testpmd.h
> @@ -34,6 +34,7 @@
>  #define RTE_PORT_HANDLING       (uint16_t)3
>  
>  extern uint8_t cl_quit;
> +extern volatile uint8_t f_quit;
>  
>  /*
>   * It is used to allocate the memory for hash key.
  
Stephen Hemminger Jan. 30, 2023, 8:11 p.m. UTC | #2
On Mon, 30 Jan 2023 18:48:25 +0000
Ferruh Yigit <ferruh.yigit@amd.com> wrote:

> On 1/25/2023 6:32 PM, Stephen Hemminger wrote:
> > Do a clean shutdown of testpmd when a signal is received; instead of
> > having testpmd kill itself.  This fixes the problem where a signal could
> > be received in the middle of a PMD and then the signal handler would
> > call PMD's close routine leading to locking problems.
> > 
> > An added benefit is it gets rid of some Windows specific code.
> > 
> > Fixes: d9a191a00e81 ("app/testpmd: fix quitting in container")
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>  
> 
> Patch looks good to me, but './devtools/test-null.sh' hangs with change.
> 
> It is possible the fix by updating './devtools/test-null.sh', but my
> concern is if this behavior change hit more consumers other than this
> internal tool, and I am for keeping previous behavior.
> 
> './devtools/test-null.sh' sends 'stop' command to testpmd but that seems
> not really what makes testpmd quit, because following change still works
> with original testpmd code:
>  -(sleep 1 && echo stop) |
>  +(sleep 1 && echo help) |
> Somehow testpmd gets Ctrl-D or equivalent with above command.
> 
> And it seems because of 'cmdline_interact()' and 'cmdline_poll()'
> difference behavior changes with above command.
> 
> It is possible to add something like  'cmdline_interact_one()' (non loop
> version of 'cmdline_interact()'), but not really sure that is correct
> way to fix the issue.
> 

Thanks for the review.
Fixed the end-of-file handling in v10 of the patch.
The cmdline_poll() function doesn't handle EOF correctly.

The function cmdline_poll() documentation is problematic since it
references that the return value is an enum, but the enum is actually
private and not exported by the library!
  

Patch

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index b32dc8bfd445..fbe9ad694dee 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -65,7 +65,6 @@ 
 #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);
@@ -12921,28 +12920,19 @@  cmdline_read_from_file(const char *filename)
 void
 prompt(void)
 {
-	int ret;
+	struct cmdline *cl;
 
-	testpmd_cl = cmdline_stdin_new(main_ctx, "testpmd> ");
-	if (testpmd_cl == NULL)
+	cl = cmdline_stdin_new(main_ctx, "testpmd> ");
+	if (cl == NULL)
 		return;
 
-	ret = atexit(prompt_exit);
-	if (ret != 0)
-		fprintf(stderr, "Cannot set exit function for cmdline\n");
-
-	cmdline_interact(testpmd_cl);
-	if (ret != 0)
-		cmdline_stdin_exit(testpmd_cl);
-}
-
-void
-prompt_exit(void)
-{
-	if (testpmd_cl != NULL) {
-		cmdline_quit(testpmd_cl);
-		cmdline_stdin_exit(testpmd_cl);
+	while (f_quit == 0 && cl_quit == 0) {
+		if (cmdline_poll(cl) < 0)
+			break;
 	}
+
+	cmdline_quit(cl);
+	cmdline_stdin_exit(cl);
 }
 
 void
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 134d79a55547..d01b6105b7de 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -11,6 +11,7 @@ 
 #include <fcntl.h>
 #ifndef RTE_EXEC_ENV_WINDOWS
 #include <sys/mman.h>
+#include <sys/select.h>
 #endif
 #include <sys/types.h>
 #include <errno.h>
@@ -231,7 +232,7 @@  unsigned int xstats_display_num; /**< Size of extended statistics to show */
  * In container, it cannot terminate the process which running with 'stats-period'
  * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
  */
-static volatile uint8_t f_quit;
+volatile uint8_t f_quit;
 uint8_t cl_quit; /* Quit testpmd from cmdline. */
 
 /*
@@ -4315,13 +4316,6 @@  init_port(void)
 	memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
 }
 
-static void
-force_quit(void)
-{
-	pmd_test_exit();
-	prompt_exit();
-}
-
 static void
 print_stats(void)
 {
@@ -4340,28 +4334,9 @@  print_stats(void)
 }
 
 static void
-signal_handler(int signum)
+signal_handler(int signum __rte_unused)
 {
-	if (signum == SIGINT || signum == SIGTERM) {
-		fprintf(stderr, "\nSignal %d received, preparing to exit...\n",
-			signum);
-#ifdef RTE_LIB_PDUMP
-		/* uninitialize packet capture framework */
-		rte_pdump_uninit();
-#endif
-#ifdef RTE_LIB_LATENCYSTATS
-		if (latencystats_enabled != 0)
-			rte_latencystats_uninit();
-#endif
-		force_quit();
-		/* Set flag to indicate the force termination. */
-		f_quit = 1;
-		/* exit with the expected status */
-#ifndef RTE_EXEC_ENV_WINDOWS
-		signal(signum, SIG_DFL);
-		kill(getpid(), signum);
-#endif
-	}
+	f_quit = 1;
 }
 
 int
@@ -4536,15 +4511,9 @@  main(int argc, char** argv)
 			start_packet_forwarding(0);
 		}
 		prompt();
-		pmd_test_exit();
 	} else
 #endif
 	{
-		char c;
-		int rc;
-
-		f_quit = 0;
-
 		printf("No commandline core given, start packet forwarding\n");
 		start_packet_forwarding(tx_first);
 		if (stats_period != 0) {
@@ -4567,15 +4536,41 @@  main(int argc, char** argv)
 				prev_time = cur_time;
 				rte_delay_us_sleep(US_PER_S);
 			}
-		}
+		} else {
+			char c;
+			fd_set fds;
+
+			printf("Press enter to exit\n");
 
-		printf("Press enter to exit\n");
-		rc = read(0, &c, 1);
-		pmd_test_exit();
-		if (rc < 0)
-			return 1;
+			FD_ZERO(&fds);
+			FD_SET(0, &fds);
+
+			/* wait for signal or enter */
+			ret = select(1, &fds, NULL, NULL, NULL);
+			if (ret < 0 && errno != EINTR)
+				rte_exit(EXIT_FAILURE,
+					 "Select failed: %s\n",
+					 strerror(errno));
+
+			/* if got enter then consume it */
+			if (ret == 1 && read(0, &c, 1) < 0)
+				rte_exit(EXIT_FAILURE,
+					 "Read failed: %s\n",
+					 strerror(errno));
+		}
 	}
 
+	pmd_test_exit();
+
+#ifdef RTE_LIB_PDUMP
+	/* uninitialize packet capture framework */
+	rte_pdump_uninit();
+#endif
+#ifdef RTE_LIB_LATENCYSTATS
+	if (latencystats_enabled != 0)
+		rte_latencystats_uninit();
+#endif
+
 	ret = rte_eal_cleanup();
 	if (ret != 0)
 		rte_exit(EXIT_FAILURE,
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index 7d24d25970d2..022210a7a964 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -34,6 +34,7 @@ 
 #define RTE_PORT_HANDLING       (uint16_t)3
 
 extern uint8_t cl_quit;
+extern volatile uint8_t f_quit;
 
 /*
  * It is used to allocate the memory for hash key.