From patchwork Tue Aug 9 12:37:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Mrzyglod X-Patchwork-Id: 15164 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id E20A937AC; Tue, 9 Aug 2016 13:33:48 +0200 (CEST) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 261B73772 for ; Tue, 9 Aug 2016 13:33:46 +0200 (CEST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP; 09 Aug 2016 04:33:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.28,494,1464678000"; d="scan'208"; a="1032359344" Received: from gklab-246-025.igk.intel.com (HELO Sent) ([10.217.246.25]) by orsmga002.jf.intel.com with SMTP; 09 Aug 2016 04:33:44 -0700 Received: by Sent (sSMTP sendmail emulation); Tue, 09 Aug 2016 14:37:19 +0200 From: Daniel Mrzyglod To: ferruh.yigit@intel.com Cc: dev@dpdk.org, Daniel Mrzyglod Date: Tue, 9 Aug 2016 14:37:15 +0200 Message-Id: <1470746235-122818-1-git-send-email-danielx.t.mrzyglod@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470224651-105433-1-git-send-email-danielx.t.mrzyglod@intel.com> References: <1470224651-105433-1-git-send-email-danielx.t.mrzyglod@intel.com> Subject: [dpdk-dev] [PATCH v2] examples/exception_path: fix shift operation in lcore setup 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: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The operaton may have an undefined behavior or yield to an unexpected result. A bit shift operation has a shift amount which is too large or has a negative value. As was mentioned in mailing list core list was limited to 64 so i changed bitmask to core array Coverity issue: 30688 Fixes: ea977ff1cb0b ("examples/exception_path: fix shift operation in lcore setup") Signed-off-by: Daniel Mrzyglod --- examples/exception_path/main.c | 74 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/examples/exception_path/main.c b/examples/exception_path/main.c index e5eedcc..1493338 100644 --- a/examples/exception_path/main.c +++ b/examples/exception_path/main.c @@ -128,11 +128,11 @@ static struct rte_mempool * pktmbuf_pool = NULL; /* Mask of enabled ports */ static uint32_t ports_mask = 0; -/* Mask of cores that read from NIC and write to tap */ -static uint64_t input_cores_mask = 0; +/* Table of cores that read from NIC and write to tap */ +static uint8_t input_cores_table[RTE_MAX_LCORE]; -/* Mask of cores that read from tap and write to NIC */ -static uint64_t output_cores_mask = 0; +/* Table of cores that read from tap and write to NIC */ +static uint8_t output_cores_table[RTE_MAX_LCORE]; /* Array storing port_id that is associated with each lcore */ static uint8_t port_ids[RTE_MAX_LCORE]; @@ -224,7 +224,7 @@ main_loop(__attribute__((unused)) void *arg) char tap_name[IFNAMSIZ]; int tap_fd; - if ((1ULL << lcore_id) & input_cores_mask) { + if (input_cores_table[lcore_id]) { /* Create new tap interface */ snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id); tap_fd = tap_create(tap_name); @@ -257,7 +257,7 @@ main_loop(__attribute__((unused)) void *arg) } } } - else if ((1ULL << lcore_id) & output_cores_mask) { + else if (output_cores_table[lcore_id]) { /* Create new tap interface */ snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id); tap_fd = tap_create(tap_name); @@ -341,7 +341,7 @@ setup_port_lcore_affinities(void) /* Setup port_ids[] array, and check masks were ok */ RTE_LCORE_FOREACH(i) { - if (input_cores_mask & (1ULL << i)) { + if (input_cores_table[i]) { /* Skip ports that are not enabled */ while ((ports_mask & (1 << rx_port)) == 0) { rx_port++; @@ -350,7 +350,7 @@ setup_port_lcore_affinities(void) } port_ids[i] = rx_port++; - } else if (output_cores_mask & (1ULL << (i & 0x3f))) { + } else if (output_cores_table[i]) { /* Skip ports that are not enabled */ while ((ports_mask & (1 << tx_port)) == 0) { tx_port++; @@ -373,6 +373,54 @@ fail: FATAL_ERROR("Invalid core/port masks specified on command line"); } +static int parse_hex2coretable(const char *coremask_string, uint8_t *core_table, + int core_table_size) +{ + + char portmask[RTE_MAX_LCORE]; + + int coremask_string_len; + int i = 0, j = 0; + uint64_t num; + char tmp_char; + char *end = NULL; + + coremask_string_len = strlen(coremask_string); + if ((coremask_string_len > (RTE_MAX_LCORE / 4) + 2) + || (core_table_size > RTE_MAX_LCORE) || (coremask_string == NULL) + || (core_table == NULL)) + return -1; + + memset(portmask, '\0', sizeof(portmask)); + memset(core_table, '\0', sizeof(uint8_t) * core_table_size); + strncpy(portmask, coremask_string, coremask_string_len); + + if (coremask_string[i] == '0') { + ++i; + if (coremask_string[i] == 'X' || coremask_string[i] == 'x') + ++i; + } + + j = 0; + while (coremask_string_len - i > 0) { + + tmp_char = portmask[coremask_string_len - 1]; + end = NULL; + num = strtoull(&tmp_char, &end, 16); + if ((end == &tmp_char)) + return -1; + + for (int z = 0; z < 4; z++) + if (((num) & (1 << (z))) != 0) + core_table[z + j * 4] = 1; + + coremask_string_len--; + j++; + } + + return 0; +} + /* Parse the arguments given in the command line of the application */ static void parse_args(int argc, char **argv) @@ -382,15 +430,15 @@ parse_args(int argc, char **argv) /* Disable printing messages within getopt() */ opterr = 0; - + int reti = 0, reto = 0; /* Parse command line */ while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) { switch (opt) { case 'i': - input_cores_mask = parse_unsigned(optarg); + reti = parse_hex2coretable(optarg, input_cores_table, RTE_MAX_LCORE); break; case 'o': - output_cores_mask = parse_unsigned(optarg); + reto = parse_hex2coretable(optarg, output_cores_table, RTE_MAX_LCORE); break; case 'p': ports_mask = parse_unsigned(optarg); @@ -402,11 +450,11 @@ parse_args(int argc, char **argv) } /* Check that options were parsed ok */ - if (input_cores_mask == 0) { + if (reti != 0) { print_usage(prgname); FATAL_ERROR("IN_CORES not specified correctly"); } - if (output_cores_mask == 0) { + if (reto != 0) { print_usage(prgname); FATAL_ERROR("OUT_CORES not specified correctly"); }