From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wg0-f42.google.com (mail-wg0-f42.google.com [74.125.82.42]) by dpdk.org (Postfix) with ESMTP id 3AA5068FD for ; Mon, 22 Sep 2014 10:32:13 +0200 (CEST) Received: by mail-wg0-f42.google.com with SMTP id a1so2262614wgh.13 for ; Mon, 22 Sep 2014 01:38:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kj28rfFoKX17Lk98XedqL9uziRFG0EmdZy6FIlXrrqg=; b=WY1984PA5VIY/QhRhXhecmxPIjAM/gKAc2mSIgl+Pe2juHujltVMlEWYdGvkipHBEx 4e6ue//4LhwAUObcw4SvTWioBje3UKga4ncgyPjqQlZ3lwwarP1Xt14ZK1sRH4g6anEP kvNO3JCcsd6Fr8Eid2CXRkSzufjZk0uer/cyzx4C8XrTzzEIPfOTT/CFTIs9JIdVrPV4 ZVieQIajX6zgX50H8CnssPgP9FiNvvQYUlz6Az0m6n9U6ocXcgDX2R1+fGV+H7+VxdZL +IaaRjMPI0AeeXwJnBKoFNkzrB6uyMLSIrtZo5AJLTK47CrXlFpgTNwaNMfjO+i2Lru8 oVAQ== X-Gm-Message-State: ALoCoQk+/TSbo/6Pk427R1EEBvMsIobmYeojCEK1tDzP1/L8EWYe2POxhTP+hRzQQCvLVc/Psxgh X-Received: by 10.180.96.161 with SMTP id dt1mr13852718wib.1.1411375096324; Mon, 22 Sep 2014 01:38:16 -0700 (PDT) Received: from alcyon.dev.6wind.com (guy78-3-82-239-227-177.fbx.proxad.net. [82.239.227.177]) by mx.google.com with ESMTPSA id js2sm11522334wjc.9.2014.09.22.01.38.15 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Sep 2014 01:38:15 -0700 (PDT) From: David Marchand To: dev@dpdk.org Date: Mon, 22 Sep 2014 10:38:00 +0200 Message-Id: <1411375081-27986-7-git-send-email-david.marchand@6wind.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1411375081-27986-1-git-send-email-david.marchand@6wind.com> References: <1411375081-27986-1-git-send-email-david.marchand@6wind.com> Subject: [dpdk-dev] [PATCH 6/7] eal: rework long options parsing X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Sep 2014 08:32:13 -0000 Identify all options through the getopt_long return value. This way, we only need a big switch/case. Indentation is broken to ease commit review (fixed in next commit). Suggested-by: Neil Horman Signed-off-by: David Marchand --- lib/librte_eal/bsdapp/eal/eal.c | 21 +++----- lib/librte_eal/common/eal_common_options.c | 77 ++++++++++++++++----------- lib/librte_eal/common/include/eal_options.h | 28 +++++++++- lib/librte_eal/linuxapp/eal/eal.c | 44 ++++++++------- 4 files changed, 105 insertions(+), 65 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal.c b/lib/librte_eal/bsdapp/eal/eal.c index 58f00fd..c4b75a4 100644 --- a/lib/librte_eal/bsdapp/eal/eal.c +++ b/lib/librte_eal/bsdapp/eal/eal.c @@ -354,8 +354,7 @@ eal_parse_args(int argc, char **argv) if (opt == '?') return -1; - ret = eal_parse_common_option(opt, optarg, option_index, - &internal_config); + ret = eal_parse_common_option(opt, optarg, &internal_config); /* common parser is not happy */ if (ret < 0) { eal_usage(prgname); @@ -371,21 +370,15 @@ eal_parse_args(int argc, char **argv) } switch (opt) { - /* long options */ - case 0: - { - RTE_LOG(ERR, EAL, "Option %s is not supported " - "on FreeBSD\n", - eal_long_options[option_index].name); - eal_usage(prgname); - return -1; - } - break; - default: - if (isprint(opt)) { + if (opt < OPT_LONG_MIN_NUM && isprint(opt)) { RTE_LOG(ERR, EAL, "Option %c is not supported " "on FreeBSD\n", opt); + } else if (opt >= OPT_LONG_MIN_NUM && + opt < OPT_LONG_MAX_NUM) { + RTE_LOG(ERR, EAL, "Option %s is not supported " + "on FreeBSD\n", + eal_long_options[option_index].name); } else { RTE_LOG(ERR, EAL, "Option %d is not supported " "on FreeBSD\n", opt); diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c index ead4300..4009f02 100644 --- a/lib/librte_eal/common/eal_common_options.c +++ b/lib/librte_eal/common/eal_common_options.c @@ -63,24 +63,24 @@ eal_short_options[] = const struct option eal_long_options[] = { - {OPT_HUGE_DIR, 1, 0, 0}, - {OPT_PROC_TYPE, 1, 0, 0}, - {OPT_NO_SHCONF, 0, 0, 0}, - {OPT_NO_HPET, 0, 0, 0}, - {OPT_VMWARE_TSC_MAP, 0, 0, 0}, - {OPT_NO_PCI, 0, 0, 0}, - {OPT_NO_HUGE, 0, 0, 0}, - {OPT_FILE_PREFIX, 1, 0, 0}, - {OPT_SOCKET_MEM, 1, 0, 0}, - {OPT_PCI_WHITELIST, 1, 0, 'w'}, - {OPT_PCI_BLACKLIST, 1, 0, 'b'}, - {OPT_VDEV, 1, 0, 0}, - {OPT_SYSLOG, 1, NULL, 0}, - {OPT_LOG_LEVEL, 1, NULL, 0}, - {OPT_BASE_VIRTADDR, 1, 0, 0}, - {OPT_XEN_DOM0, 0, 0, 0}, - {OPT_CREATE_UIO_DEV, 1, NULL, 0}, - {OPT_VFIO_INTR, 1, NULL, 0}, + {OPT_HUGE_DIR, 1, 0, OPT_HUGE_DIR_NUM}, + {OPT_PROC_TYPE, 1, 0, OPT_PROC_TYPE_NUM}, + {OPT_NO_SHCONF, 0, 0, OPT_NO_SHCONF_NUM}, + {OPT_NO_HPET, 0, 0, OPT_NO_HPET_NUM}, + {OPT_VMWARE_TSC_MAP, 0, 0, OPT_VMWARE_TSC_MAP_NUM}, + {OPT_NO_PCI, 0, 0, OPT_NO_PCI_NUM}, + {OPT_NO_HUGE, 0, 0, OPT_NO_HUGE_NUM}, + {OPT_FILE_PREFIX, 1, 0, OPT_FILE_PREFIX_NUM}, + {OPT_SOCKET_MEM, 1, 0, OPT_SOCKET_MEM_NUM}, + {OPT_PCI_WHITELIST, 1, 0, OPT_PCI_WHITELIST_NUM}, + {OPT_PCI_BLACKLIST, 1, 0, OPT_PCI_BLACKLIST_NUM}, + {OPT_VDEV, 1, 0, OPT_VDEV_NUM}, + {OPT_SYSLOG, 1, NULL, OPT_SYSLOG_NUM}, + {OPT_LOG_LEVEL, 1, NULL, OPT_LOG_LEVEL_NUM}, + {OPT_BASE_VIRTADDR, 1, 0, OPT_BASE_VIRTADDR_NUM}, + {OPT_XEN_DOM0, 0, 0, OPT_XEN_DOM0_NUM}, + {OPT_CREATE_UIO_DEV, 1, NULL, OPT_CREATE_UIO_DEV_NUM}, + {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM}, {0, 0, 0, 0} }; @@ -238,7 +238,7 @@ eal_parse_proc_type(const char *arg) } int -eal_parse_common_option(int opt, const char *optarg, int longindex, +eal_parse_common_option(int opt, const char *optarg, struct internal_config *conf) { switch (opt) { @@ -296,32 +296,46 @@ eal_parse_common_option(int opt, const char *optarg, int longindex, break; /* long options */ - case 0: - if (!strcmp(eal_long_options[longindex].name, OPT_NO_HUGE)) { + case OPT_NO_HUGE_NUM: conf->no_hugetlbfs = 1; - } else if (!strcmp(eal_long_options[longindex].name, OPT_NO_PCI)) { + break; + + case OPT_NO_PCI_NUM: conf->no_pci = 1; - } else if (!strcmp(eal_long_options[longindex].name, OPT_NO_HPET)) { + break; + + case OPT_NO_HPET_NUM: conf->no_hpet = 1; - } else if (!strcmp(eal_long_options[longindex].name, OPT_VMWARE_TSC_MAP)) { + break; + + case OPT_VMWARE_TSC_MAP_NUM: conf->vmware_tsc_map = 1; - } else if (!strcmp(eal_long_options[longindex].name, OPT_NO_SHCONF)) { + break; + + case OPT_NO_SHCONF_NUM: conf->no_shconf = 1; - } else if (!strcmp(eal_long_options[longindex].name, OPT_PROC_TYPE)) { + break; + + case OPT_PROC_TYPE_NUM: conf->process_type = eal_parse_proc_type(optarg); - } else if (!strcmp(eal_long_options[longindex].name, OPT_VDEV)) { + break; + + case OPT_VDEV_NUM: if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL, optarg) < 0) { return -1; } - } else if (!strcmp(eal_long_options[longindex].name, OPT_SYSLOG)) { + break; + + case OPT_SYSLOG_NUM: if (eal_parse_syslog(optarg, conf) < 0) { RTE_LOG(ERR, EAL, "invalid parameters for --" OPT_SYSLOG "\n"); return -1; } - } else if (!strcmp(eal_long_options[longindex].name, - OPT_LOG_LEVEL)) { + break; + + case OPT_LOG_LEVEL_NUM: { uint32_t log; if (eal_parse_log_level(optarg, &log) < 0) { @@ -331,9 +345,8 @@ eal_parse_common_option(int opt, const char *optarg, int longindex, return -1; } conf->log_level = log; - } - break; + } /* don't know what to do, leave this to caller */ default: diff --git a/lib/librte_eal/common/include/eal_options.h b/lib/librte_eal/common/include/eal_options.h index 4e6b90d..22819ec 100644 --- a/lib/librte_eal/common/include/eal_options.h +++ b/lib/librte_eal/common/include/eal_options.h @@ -30,29 +30,55 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +enum { + /* long options mapped to a short option */ #define OPT_PCI_WHITELIST "pci-whitelist" + OPT_PCI_WHITELIST_NUM = 'w', #define OPT_PCI_BLACKLIST "pci-blacklist" + OPT_PCI_BLACKLIST_NUM = 'b', + /* first long only option value must be >= 256, so that we won't + * conflict with short options */ + OPT_LONG_MIN_NUM = 256, #define OPT_HUGE_DIR "huge-dir" + OPT_HUGE_DIR_NUM = OPT_LONG_MIN_NUM, #define OPT_PROC_TYPE "proc-type" + OPT_PROC_TYPE_NUM, #define OPT_NO_SHCONF "no-shconf" + OPT_NO_SHCONF_NUM, #define OPT_NO_HPET "no-hpet" + OPT_NO_HPET_NUM, #define OPT_VMWARE_TSC_MAP "vmware-tsc-map" + OPT_VMWARE_TSC_MAP_NUM, #define OPT_NO_PCI "no-pci" + OPT_NO_PCI_NUM, #define OPT_NO_HUGE "no-huge" + OPT_NO_HUGE_NUM, #define OPT_FILE_PREFIX "file-prefix" + OPT_FILE_PREFIX_NUM, #define OPT_SOCKET_MEM "socket-mem" + OPT_SOCKET_MEM_NUM, #define OPT_VDEV "vdev" + OPT_VDEV_NUM, #define OPT_SYSLOG "syslog" + OPT_SYSLOG_NUM, #define OPT_LOG_LEVEL "log-level" + OPT_LOG_LEVEL_NUM, #define OPT_BASE_VIRTADDR "base-virtaddr" + OPT_BASE_VIRTADDR_NUM, #define OPT_XEN_DOM0 "xen-dom0" + OPT_XEN_DOM0_NUM, #define OPT_CREATE_UIO_DEV "create-uio-dev" + OPT_CREATE_UIO_DEV_NUM, #define OPT_VFIO_INTR "vfio-intr" + OPT_VFIO_INTR_NUM, + OPT_LONG_MAX_NUM +}; extern const char eal_short_options[]; extern const struct option eal_long_options[]; -int eal_parse_common_option(int opt, const char *argv, int longindex, +int eal_parse_common_option(int opt, const char *argv, struct internal_config *conf); void eal_common_usage(void); diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c index d82debf..1d468dd 100644 --- a/lib/librte_eal/linuxapp/eal/eal.c +++ b/lib/librte_eal/linuxapp/eal/eal.c @@ -553,8 +553,7 @@ eal_parse_args(int argc, char **argv) if (opt == '?') return -1; - ret = eal_parse_common_option(opt, optarg, option_index, - &internal_config); + ret = eal_parse_common_option(opt, optarg, &internal_config); /* common parser is not happy */ if (ret < 0) { eal_usage(prgname); @@ -584,8 +583,7 @@ eal_parse_args(int argc, char **argv) break; /* long options */ - case 0: - if (!strcmp(eal_long_options[option_index].name, OPT_XEN_DOM0)) { + case OPT_XEN_DOM0_NUM: #ifdef RTE_LIBRTE_XEN_DOM0 internal_config.xen_dom0_support = 1; #else @@ -594,46 +592,56 @@ eal_parse_args(int argc, char **argv) " RTE_LIBRTE_XEN_DOM0=y\n"); return -1; #endif - } else if (!strcmp(eal_long_options[option_index].name, OPT_HUGE_DIR)) { + break; + + case OPT_HUGE_DIR_NUM: internal_config.hugepage_dir = optarg; - } else if (!strcmp(eal_long_options[option_index].name, OPT_FILE_PREFIX)) { + break; + + case OPT_FILE_PREFIX_NUM: internal_config.hugefile_prefix = optarg; - } else if (!strcmp(eal_long_options[option_index].name, OPT_SOCKET_MEM)) { + break; + + case OPT_SOCKET_MEM_NUM: if (eal_parse_socket_mem(optarg) < 0) { RTE_LOG(ERR, EAL, "invalid parameters for --" OPT_SOCKET_MEM "\n"); eal_usage(prgname); return -1; } - } else if (!strcmp(eal_long_options[option_index].name, OPT_BASE_VIRTADDR)) { + break; + + case OPT_BASE_VIRTADDR_NUM: if (eal_parse_base_virtaddr(optarg) < 0) { RTE_LOG(ERR, EAL, "invalid parameter for --" OPT_BASE_VIRTADDR "\n"); eal_usage(prgname); return -1; } - } else if (!strcmp(eal_long_options[option_index].name, OPT_VFIO_INTR)) { + break; + + case OPT_VFIO_INTR_NUM: if (eal_parse_vfio_intr(optarg) < 0) { RTE_LOG(ERR, EAL, "invalid parameters for --" OPT_VFIO_INTR "\n"); eal_usage(prgname); return -1; } - } else if (!strcmp(eal_long_options[option_index].name, OPT_CREATE_UIO_DEV)) { + break; + + case OPT_CREATE_UIO_DEV_NUM: internal_config.create_uio_dev = 1; - } else { - RTE_LOG(ERR, EAL, "Option %s is not supported " - "on Linux\n", - eal_long_options[option_index].name); - eal_usage(prgname); - return -1; - } break; default: - if (isprint(opt)) { + if (opt < OPT_LONG_MIN_NUM && isprint(opt)) { RTE_LOG(ERR, EAL, "Option %c is not supported " "on Linux\n", opt); + } else if (opt >= OPT_LONG_MIN_NUM && + opt < OPT_LONG_MAX_NUM) { + RTE_LOG(ERR, EAL, "Option %s is not supported " + "on Linux\n", + eal_long_options[option_index].name); } else { RTE_LOG(ERR, EAL, "Option %d is not supported " "on Linux\n", opt); -- 1.7.10.4