[5/5] example:l3fwd: add option -t in command line

Message ID 1463663005-1392-1-git-send-email-forrest.shi@linaro.org
State New
Headers show

Commit Message

Forrest Shi May 19, 2016, 1:03 p.m.
From: Xuelin Shi <forrest.shi@linaro.org>

specify the number of threads to do forwarding.
support at most cpu-1 forwarding threads, one thread for each port.

Signed-off-by: Xuelin Shi <forrest.shi@linaro.org>
---
 example/l3fwd/odp_l3fwd.c | 66 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 55 insertions(+), 11 deletions(-)

Patch

diff --git a/example/l3fwd/odp_l3fwd.c b/example/l3fwd/odp_l3fwd.c
index 5598a5a..61e90b9 100644
--- a/example/l3fwd/odp_l3fwd.c
+++ b/example/l3fwd/odp_l3fwd.c
@@ -33,8 +33,9 @@ 
 
 typedef struct {
 	char *if_names[MAX_NB_PKTIO];
-	uint32_t if_count;
+	int if_count;
 	char *route_str[MAX_NB_ROUTE];
+	int worker_count;
 } app_args_t;
 
 struct l3fwd_pktio_s {
@@ -256,8 +257,16 @@  int main(int argc, char **argv)
 		printf("create pktio %s, mac %s\n", args->if_names[i], buf);
 	}
 
-	/* one thread for each port */
-	global.nb_worker = args->if_count;
+	/* at most cpu_count-1 threads, only 1 thread on 1 port.
+	 * TODO: assign more threads for each port
+	 */
+	if (args->worker_count == 0)
+		args->worker_count = args->if_count;
+	if (args->worker_count >= odp_cpu_count())
+		args->worker_count = odp_cpu_count() - 1;
+
+	global.nb_worker = args->worker_count;
+
 	memset(&thr_params, 0, sizeof(thr_params));
 	thr_params.start    = run_worker;
 	thr_params.thr_type = ODP_THREAD_WORKER;
@@ -298,8 +307,10 @@  static void print_usage(char *progname)
 	       "  -r, --route SubNet:Intf[:NextHopMAC]\n"
 	       "	NextHopMAC can be optional, in this case, zeroed mac\n"
 	       "\n"
-	       "Optional OPTIONS\n"
-	       "  -h, --help           Display help and exit.\n\n"
+	       "Optional OPTIONS:\n"
+	       "  -t, --thread number of threads to do forwarding\n"
+	       "	optional, default as cpu count\n"
+	       "  -h, --help   Display help and exit.\n\n"
 	       "\n", NO_PATH(progname), NO_PATH(progname)
 	    );
 }
@@ -315,18 +326,30 @@  static void parse_cmdline_args(int argc, char *argv[], app_args_t *args)
 	static struct option longopts[] = {
 		{"interface", required_argument, NULL, 'i'},	/* return 'i' */
 		{"route", required_argument, NULL, 'r'},	/* return 'r' */
+		{"thread", optional_argument, NULL, 't'},	/* return 't'*/
 		{"help", no_argument, NULL, 'h'},		/* return 'h' */
 		{NULL, 0, NULL, 0}
 	};
 
 	while (1) {
-		opt = getopt_long(argc, argv, "+i:r:h",
+		opt = getopt_long(argc, argv, "+t:i:r:h",
 				  longopts, &long_index);
 
 		if (opt == -1)
 			break;	/* No more options */
 
 		switch (opt) {
+		/* parse number of worker threads to be run*/
+		case 't':
+			i = odp_cpu_count();
+			args->worker_count = atoi(optarg);
+			if (args->worker_count > i) {
+				printf("Too many threads,"
+				       "truncate to cpu count: %d\n", i);
+				args->worker_count = i;
+			}
+
+			break;
 		/* parse packet-io interface names */
 		case 'i':
 			len = strlen(optarg);
@@ -352,8 +375,14 @@  static void parse_cmdline_args(int argc, char *argv[], app_args_t *args)
 			if (i == 0) {
 				print_usage(argv[0]);
 				exit(EXIT_FAILURE);
+			} else if ((i & 1) != 0) {
+				printf("even number of ports expected, "
+				       "got %u.\n", i);
+				exit(EXIT_FAILURE);
+			} else if (i > MAX_NB_PKTIO) {
+				printf("too many ports specified, "
+				       "truncated to max %d", MAX_NB_PKTIO);
 			}
-
 			args->if_count = i;
 
 			/* store the if names (reset names string) */
@@ -390,17 +419,32 @@  static void parse_cmdline_args(int argc, char *argv[], app_args_t *args)
 		}
 	}
 
-	if (args->if_count == 0 || mem_failure == 1) {
-		print_usage(argv[0]);
-		exit(EXIT_FAILURE);
+	/* checking arguments */
+	if (args->if_count == 0) {
+		printf("\nNo option -i specified.\n");
+		goto out;
+	}
+
+	if (args->route_str[0]== NULL) {
+		printf("\nNo option -r specified.\n");
+		goto out;
 	}
 
+	if (mem_failure == 1) {
+		printf("\nAllocate memory failure.\n");
+		goto out;
+	}
 	optind = 1;		/* reset 'extern optind' from the getopt lib */
+	return;
+
+out:
+	print_usage(argv[0]);
+	exit(EXIT_FAILURE);
 }
 
 static void print_info(char *progname, app_args_t *args)
 {
-	uint32_t i;
+	int i;
 
 	printf("\n"
 	       "ODP system info\n"