[dpdk-dev] [PATCH 2/8] examples/l3fwd-acl: enhance getopt_long usage

Ibtisam Tariq ibtisam.tariq at emumba.com
Thu Oct 29 13:53:33 CET 2020


Instead of using getopt_long return value, strcmp was used to
compare the input parameters with the struct option array. This
patch get rid of all those strcmp by directly binding each longopt
with an int enum.

Bugzilla ID: 238
Fixes: 361b2e9559 ("acl: new sample l3fwd-acl")
Cc: konstantin.ananyev at intel.com

Reported-by: David Marchand <david.marchand at redhat.com>
Signed-off-by: Ibtisam Tariq <ibtisam.tariq at emumba.com>
---
 examples/l3fwd-acl/main.c | 189 +++++++++++++++++++-------------------
 1 file changed, 97 insertions(+), 92 deletions(-)

diff --git a/examples/l3fwd-acl/main.c b/examples/l3fwd-acl/main.c
index 961594f5f..5c8de1fc0 100644
--- a/examples/l3fwd-acl/main.c
+++ b/examples/l3fwd-acl/main.c
@@ -195,13 +195,24 @@ send_single_packet(struct rte_mbuf *m, uint16_t port);
 #define ACL_LEAD_CHAR		('@')
 #define ROUTE_LEAD_CHAR		('R')
 #define COMMENT_LEAD_CHAR	('#')
+
+enum{
 #define OPTION_CONFIG		"config"
+	OPTION_CONFIG_NUM = 256,
 #define OPTION_NONUMA		"no-numa"
+	OPTION_NONUMA_NUM,
 #define OPTION_ENBJMO		"enable-jumbo"
+	OPTION_ENBJMO_NUM,
 #define OPTION_RULE_IPV4	"rule_ipv4"
+	OPTION_RULE_IPV4_NUM,
 #define OPTION_RULE_IPV6	"rule_ipv6"
+	OPTION_RULE_IPV6_NUM,
 #define OPTION_ALG		"alg"
+	OPTION_ALG_NUM,
 #define OPTION_ETH_DEST		"eth-dest"
+	OPTION_ETH_DEST_NUM,
+};
+
 #define ACL_DENY_SIGNATURE	0xf0000000
 #define RTE_LOGTYPE_L3FWDACL	RTE_LOGTYPE_USER3
 #define acl_log(format, ...)	RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
@@ -1747,13 +1758,13 @@ parse_args(int argc, char **argv)
 	int option_index;
 	char *prgname = argv[0];
 	static struct option lgopts[] = {
-		{OPTION_CONFIG, 1, 0, 0},
-		{OPTION_NONUMA, 0, 0, 0},
-		{OPTION_ENBJMO, 0, 0, 0},
-		{OPTION_RULE_IPV4, 1, 0, 0},
-		{OPTION_RULE_IPV6, 1, 0, 0},
-		{OPTION_ALG, 1, 0, 0},
-		{OPTION_ETH_DEST, 1, 0, 0},
+		{OPTION_CONFIG, 1, NULL, OPTION_CONFIG_NUM},
+		{OPTION_NONUMA, 0, NULL, OPTION_NONUMA_NUM},
+		{OPTION_ENBJMO, 0, NULL, OPTION_ENBJMO_NUM},
+		{OPTION_RULE_IPV4, 1, NULL, OPTION_RULE_IPV4_NUM},
+		{OPTION_RULE_IPV6, 1, NULL, OPTION_RULE_IPV6_NUM},
+		{OPTION_ALG, 1, NULL, OPTION_ALG_NUM},
+		{OPTION_ETH_DEST, 1, NULL, OPTION_ETH_DEST_NUM},
 		{NULL, 0, 0, 0}
 	};
 
@@ -1778,98 +1789,92 @@ parse_args(int argc, char **argv)
 			break;
 
 		/* long options */
-		case 0:
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_CONFIG,
-					sizeof(OPTION_CONFIG))) {
-				ret = parse_config(optarg);
-				if (ret) {
-					printf("invalid config\n");
-					print_usage(prgname);
-					return -1;
-				}
-			}
-
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_NONUMA,
-					sizeof(OPTION_NONUMA))) {
-				printf("numa is disabled\n");
-				numa_on = 0;
-			}
-
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
-				struct option lenopts = {
-					"max-pkt-len",
-					required_argument,
-					0,
-					0
-				};
-
-				printf("jumbo frame is enabled\n");
-				port_conf.rxmode.offloads |=
-						DEV_RX_OFFLOAD_JUMBO_FRAME;
-				port_conf.txmode.offloads |=
-						DEV_TX_OFFLOAD_MULTI_SEGS;
-
-				/*
-				 * if no max-pkt-len set, then use the
-				 * default value RTE_ETHER_MAX_LEN
-				 */
-				if (0 == getopt_long(argc, argvopt, "",
-						&lenopts, &option_index)) {
-					ret = parse_max_pkt_len(optarg);
-					if ((ret < 64) ||
-						(ret > MAX_JUMBO_PKT_LEN)) {
-						printf("invalid packet "
-							"length\n");
-						print_usage(prgname);
-						return -1;
-					}
-					port_conf.rxmode.max_rx_pkt_len = ret;
-				}
-				printf("set jumbo frame max packet length "
-					"to %u\n",
-					(unsigned int)
-					port_conf.rxmode.max_rx_pkt_len);
-			}
-
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_RULE_IPV4,
-					sizeof(OPTION_RULE_IPV4)))
-				parm_config.rule_ipv4_name = optarg;
-
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_RULE_IPV6,
-					sizeof(OPTION_RULE_IPV6))) {
-				parm_config.rule_ipv6_name = optarg;
+		case OPTION_CONFIG_NUM:
+		{
+			ret = parse_config(optarg);
+			if (ret) {
+				printf("invalid config\n");
+				print_usage(prgname);
+				return -1;
 			}
-
-			if (!strncmp(lgopts[option_index].name,
-					OPTION_ALG, sizeof(OPTION_ALG))) {
-				parm_config.alg = parse_acl_alg(optarg);
-				if (parm_config.alg ==
-						RTE_ACL_CLASSIFY_DEFAULT) {
-					printf("unknown %s value:\"%s\"\n",
-						OPTION_ALG, optarg);
+			break;
+		}
+		case OPTION_NONUMA_NUM:
+		{
+			printf("numa is disabled\n");
+			numa_on = 0;
+			break;
+		}
+		case OPTION_ENBJMO_NUM:
+		{
+			struct option lenopts = {
+				"max-pkt-len",
+				required_argument,
+				0,
+				0
+			};
+
+			printf("jumbo frame is enabled\n");
+			port_conf.rxmode.offloads |=
+					DEV_RX_OFFLOAD_JUMBO_FRAME;
+			port_conf.txmode.offloads |=
+					DEV_TX_OFFLOAD_MULTI_SEGS;
+
+			/*
+			 * if no max-pkt-len set, then use the
+			 * default value RTE_ETHER_MAX_LEN
+			 */
+			if (0 == getopt_long(argc, argvopt, "",
+					&lenopts, &option_index)) {
+				ret = parse_max_pkt_len(optarg);
+				if ((ret < 64) ||
+					(ret > MAX_JUMBO_PKT_LEN)) {
+					printf("invalid packet "
+						"length\n");
 					print_usage(prgname);
 					return -1;
 				}
+				port_conf.rxmode.max_rx_pkt_len = ret;
 			}
-
-			if (!strncmp(lgopts[option_index].name, OPTION_ETH_DEST,
-					sizeof(OPTION_ETH_DEST))) {
-				const char *serr = parse_eth_dest(optarg);
-				if (serr != NULL) {
-					printf("invalid %s value:\"%s\": %s\n",
-						OPTION_ETH_DEST, optarg, serr);
-					print_usage(prgname);
-					return -1;
-				}
+			printf("set jumbo frame max packet length "
+				"to %u\n",
+				(unsigned int)
+				port_conf.rxmode.max_rx_pkt_len);
+			break;
+		}
+		case OPTION_RULE_IPV4_NUM:
+		{
+			parm_config.rule_ipv4_name = optarg;
+			break;
+		}
+		case OPTION_RULE_IPV6_NUM:
+		{
+			parm_config.rule_ipv6_name = optarg;
+			break;
+		}
+		case OPTION_ALG_NUM:
+		{
+			parm_config.alg = parse_acl_alg(optarg);
+			if (parm_config.alg ==
+					RTE_ACL_CLASSIFY_DEFAULT) {
+				printf("unknown %s value:\"%s\"\n",
+					OPTION_ALG, optarg);
+				print_usage(prgname);
+				return -1;
 			}
-
 			break;
-
+		}
+		case OPTION_ETH_DEST_NUM:
+		{
+			const char *serr = parse_eth_dest(optarg);
+			if (serr != NULL) {
+				printf("invalid %s value:\"%s\": %s\n",
+					OPTION_ETH_DEST, optarg, serr);
+				print_usage(prgname);
+				return -1;
+			}
+			break;
+		}
 		default:
 			print_usage(prgname);
 			return -1;
-- 
2.17.1



More information about the dev mailing list