From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id D7E1229CA for ; Fri, 23 Jun 2017 14:30:09 +0200 (CEST) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Jun 2017 05:30:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,378,1493708400"; d="scan'208";a="984407427" Received: from irsmsx151.ger.corp.intel.com ([163.33.192.59]) by orsmga003.jf.intel.com with ESMTP; 23 Jun 2017 05:30:06 -0700 Received: from irsmsx155.ger.corp.intel.com (163.33.192.3) by IRSMSX151.ger.corp.intel.com (163.33.192.59) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 23 Jun 2017 13:30:06 +0100 Received: from irsmsx102.ger.corp.intel.com ([169.254.2.211]) by irsmsx155.ger.corp.intel.com ([169.254.14.182]) with mapi id 14.03.0319.002; Fri, 23 Jun 2017 13:30:05 +0100 From: "Van Haaren, Harry" To: Jerin Jacob , "dev@dpdk.org" CC: "Richardson, Bruce" , "hemant.agrawal@nxp.com" , "Eads, Gage" , "nipun.gupta@nxp.com" , "Vangati, Narender" , "Rao, Nikhil" , "gprathyusha@caviumnetworks.com" Thread-Topic: [dpdk-dev] [PATCH 04/33] app/testeventdev: add string parsing helpers Thread-Index: AQHS1+0JI46/7pq9rkq5R70zQSJKGqIyiKuA Date: Fri, 23 Jun 2017 12:30:04 +0000 Message-ID: References: <20170528195854.6064-1-jerin.jacob@caviumnetworks.com> <20170528195854.6064-5-jerin.jacob@caviumnetworks.com> In-Reply-To: <20170528195854.6064-5-jerin.jacob@caviumnetworks.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiYjc0NDQwMWQtODU0MC00NWZiLWE0NDEtYmNjNWI3YWYxZWJjIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE2LjUuOS4zIiwiVHJ1c3RlZExhYmVsSGFzaCI6Iko3QWtFQ3FSVmpRZkp1dHU3MzBsaldKOFRZQ2JQTEQ0aytIRklxV3U4ZnM9In0= x-ctpclassification: CTP_IC dlp-product: dlpe-windows dlp-version: 10.0.102.7 dlp-reaction: no-action x-originating-ip: [163.33.239.180] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH 04/33] app/testeventdev: add string parsing helpers X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Jun 2017 12:30:10 -0000 > From: Jerin Jacob [mailto:jerin.jacob@caviumnetworks.com] > Sent: Sunday, May 28, 2017 8:58 PM > To: dev@dpdk.org > Cc: Richardson, Bruce ; Van Haaren, Harry > ; hemant.agrawal@nxp.com; Eads, Gage ; > nipun.gupta@nxp.com; Vangati, Narender ; Rao,= Nikhil > ; gprathyusha@caviumnetworks.com > Subject: [dpdk-dev] [PATCH 04/33] app/testeventdev: add string parsing he= lpers >=20 > From: Guduri Prathyusha >=20 > Add a couple of help functions that will allow parsing many types of > input parameters, i.e.: bool, 16, 32, 64 bits, hex and list of cores etc. >=20 > Derived from examples/ip_pipeline/parser.h >=20 > Signed-off-by: Guduri Prathyusha I'm not sure about the licenses, but here's an Ack for the code: Acked-by: Harry van Haaren > --- > app/test-eventdev/Makefile | 1 + > app/test-eventdev/parser.c | 432 +++++++++++++++++++++++++++++++++++++++= ++++++ > app/test-eventdev/parser.h | 79 +++++++++ > 3 files changed, 512 insertions(+) > create mode 100644 app/test-eventdev/parser.c > create mode 100644 app/test-eventdev/parser.h >=20 > diff --git a/app/test-eventdev/Makefile b/app/test-eventdev/Makefile > index 8f4fc5f45..2e552a084 100644 > --- a/app/test-eventdev/Makefile > +++ b/app/test-eventdev/Makefile > @@ -40,5 +40,6 @@ CFLAGS +=3D $(WERROR_FLAGS) > # > SRCS-y :=3D evt_main.c > SRCS-y +=3D evt_test.c > +SRCS-y +=3D parser.c >=20 > include $(RTE_SDK)/mk/rte.app.mk > diff --git a/app/test-eventdev/parser.c b/app/test-eventdev/parser.c > new file mode 100644 > index 000000000..d267447a2 > --- /dev/null > +++ b/app/test-eventdev/parser.c > @@ -0,0 +1,432 @@ > +/*- > + * BSD LICENSE > + * > + * Copyright(c) 2016 Intel Corporation. All rights reserved. > + * All rights reserved. > + * > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions > + * are met: > + * > + * * Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * * Redistributions in binary form must reproduce the above copyrig= ht > + * notice, this list of conditions and the following disclaimer in > + * the documentation and/or other materials provided with the > + * distribution. > + * * Neither the name of Intel Corporation nor the names of its > + * contributors may be used to endorse or promote products derived > + * from this software without specific prior written permission. > + * > + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS F= OR > + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGH= T > + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTA= L, > + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US= E, > + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A= NY > + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE U= SE > + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE= . > + */ > + > +/* > + * For my_ether_aton() function: > + * > + * Copyright (c) 2009, Olivier MATZ > + * All rights reserved. > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions ar= e met: > + * > + * * Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * * Redistributions in binary form must reproduce the above copyrig= ht > + * notice, this list of conditions and the following disclaimer in= the > + * documentation and/or other materials provided with the distribu= tion. > + * * Neither the name of the University of California, Berkeley nor = the > + * names of its contributors may be used to endorse or promote pro= ducts > + * derived from this software without specific prior written permi= ssion. > + * > + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' A= ND ANY > + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMP= LIED > + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AR= E > + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE = FOR ANY > + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DA= MAGES > + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SE= RVICES; > + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUS= ED AND > + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR= TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE= OF THIS > + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. > + */ > + > +/* > + * For inet_pton4() and inet_pton6() functions: > + * > + * Copyright (c) 1996 by Internet Software Consortium. > + * > + * Permission to use, copy, modify, and distribute this software for any > + * purpose with or without fee is hereby granted, provided that the abov= e > + * copyright notice and this permission notice appear in all copies. > + * > + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DIS= CLAIMS > + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WAR= RANTIES > + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE > + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENT= IAL > + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR > + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIO= US > + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE O= F THIS > + * SOFTWARE. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > + > +#include "parser.h" > + > +static uint32_t > +get_hex_val(char c) > +{ > + switch (c) { > + case '0': case '1': case '2': case '3': case '4': case '5': > + case '6': case '7': case '8': case '9': > + return c - '0'; > + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': > + return c - 'A' + 10; > + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': > + return c - 'a' + 10; > + default: > + return 0; > + } > +} > + > +int > +parser_read_arg_bool(const char *p) > +{ > + p =3D skip_white_spaces(p); > + int result =3D -EINVAL; > + > + if (((p[0] =3D=3D 'y') && (p[1] =3D=3D 'e') && (p[2] =3D=3D 's')) || > + ((p[0] =3D=3D 'Y') && (p[1] =3D=3D 'E') && (p[2] =3D=3D 'S'))) { > + p +=3D 3; > + result =3D 1; > + } > + > + if (((p[0] =3D=3D 'o') && (p[1] =3D=3D 'n')) || > + ((p[0] =3D=3D 'O') && (p[1] =3D=3D 'N'))) { > + p +=3D 2; > + result =3D 1; > + } > + > + if (((p[0] =3D=3D 'n') && (p[1] =3D=3D 'o')) || > + ((p[0] =3D=3D 'N') && (p[1] =3D=3D 'O'))) { > + p +=3D 2; > + result =3D 0; > + } > + > + if (((p[0] =3D=3D 'o') && (p[1] =3D=3D 'f') && (p[2] =3D=3D 'f')) || > + ((p[0] =3D=3D 'O') && (p[1] =3D=3D 'F') && (p[2] =3D=3D 'F'))) { > + p +=3D 3; > + result =3D 0; > + } > + > + p =3D skip_white_spaces(p); > + > + if (p[0] !=3D '\0') > + return -EINVAL; > + > + return result; > +} > + > +int > +parser_read_uint64(uint64_t *value, const char *p) > +{ > + char *next; > + uint64_t val; > + > + p =3D skip_white_spaces(p); > + if (!isdigit(*p)) > + return -EINVAL; > + > + val =3D strtoul(p, &next, 10); > + if (p =3D=3D next) > + return -EINVAL; > + > + p =3D next; > + switch (*p) { > + case 'T': > + val *=3D 1024ULL; > + /* fall through */ > + case 'G': > + val *=3D 1024ULL; > + /* fall through */ > + case 'M': > + val *=3D 1024ULL; > + /* fall through */ > + case 'k': > + case 'K': > + val *=3D 1024ULL; > + p++; > + break; > + } > + > + p =3D skip_white_spaces(p); > + if (*p !=3D '\0') > + return -EINVAL; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_int32(int32_t *value, const char *p) > +{ > + char *next; > + int32_t val; > + > + p =3D skip_white_spaces(p); > + if (!isdigit(*p)) > + return -EINVAL; > + > + val =3D strtol(p, &next, 10); > + if (p =3D=3D next) > + return -EINVAL; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint64_hex(uint64_t *value, const char *p) > +{ > + char *next; > + uint64_t val; > + > + p =3D skip_white_spaces(p); > + > + val =3D strtoul(p, &next, 16); > + if (p =3D=3D next) > + return -EINVAL; > + > + p =3D skip_white_spaces(next); > + if (*p !=3D '\0') > + return -EINVAL; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint32(uint32_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT32_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint32_hex(uint32_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64_hex(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT32_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint16(uint16_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT16_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint16_hex(uint16_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64_hex(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT16_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint8(uint8_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT8_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_read_uint8_hex(uint8_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int ret =3D parser_read_uint64_hex(&val, p); > + > + if (ret < 0) > + return ret; > + > + if (val > UINT8_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens) > +{ > + uint32_t i; > + > + if ((string =3D=3D NULL) || > + (tokens =3D=3D NULL) || > + (*n_tokens < 1)) > + return -EINVAL; > + > + for (i =3D 0; i < *n_tokens; i++) { > + tokens[i] =3D strtok_r(string, PARSE_DELIMITER, &string); > + if (tokens[i] =3D=3D NULL) > + break; > + } > + > + if ((i =3D=3D *n_tokens) && > + (strtok_r(string, PARSE_DELIMITER, &string) !=3D NULL)) > + return -E2BIG; > + > + *n_tokens =3D i; > + return 0; > +} > + > +int > +parse_hex_string(char *src, uint8_t *dst, uint32_t *size) > +{ > + char *c; > + uint32_t len, i; > + > + /* Check input parameters */ > + if ((src =3D=3D NULL) || > + (dst =3D=3D NULL) || > + (size =3D=3D NULL) || > + (*size =3D=3D 0)) > + return -1; > + > + len =3D strlen(src); > + if (((len & 3) !=3D 0) || > + (len > (*size) * 2)) > + return -1; > + *size =3D len / 2; > + > + for (c =3D src; *c !=3D 0; c++) { > + if ((((*c) >=3D '0') && ((*c) <=3D '9')) || > + (((*c) >=3D 'A') && ((*c) <=3D 'F')) || > + (((*c) >=3D 'a') && ((*c) <=3D 'f'))) > + continue; > + > + return -1; > + } > + > + /* Convert chars to bytes */ > + for (i =3D 0; i < *size; i++) > + dst[i] =3D get_hex_val(src[2 * i]) * 16 + > + get_hex_val(src[2 * i + 1]); > + > + return 0; > +} > + > +int > +parse_lcores_list(bool lcores[], const char *corelist) > +{ > + int i, idx =3D 0; > + int min, max; > + char *end =3D NULL; > + > + if (corelist =3D=3D NULL) > + return -1; > + while (isblank(*corelist)) > + corelist++; > + i =3D strlen(corelist); > + while ((i > 0) && isblank(corelist[i - 1])) > + i--; > + > + /* Get list of lcores */ > + min =3D RTE_MAX_LCORE; > + do { > + while (isblank(*corelist)) > + corelist++; > + if (*corelist =3D=3D '\0') > + return -1; > + idx =3D strtoul(corelist, &end, 10); > + > + if (end =3D=3D NULL) > + return -1; > + while (isblank(*end)) > + end++; > + if (*end =3D=3D '-') { > + min =3D idx; > + } else if ((*end =3D=3D ',') || (*end =3D=3D '\0')) { > + max =3D idx; > + if (min =3D=3D RTE_MAX_LCORE) > + min =3D idx; > + for (idx =3D min; idx <=3D max; idx++) > + lcores[idx] =3D 1; > + > + min =3D RTE_MAX_LCORE; > + } else > + return -1; > + corelist =3D end + 1; > + } while (*end !=3D '\0'); > + > + return 0; > +} > diff --git a/app/test-eventdev/parser.h b/app/test-eventdev/parser.h > new file mode 100644 > index 000000000..75a5a3b45 > --- /dev/null > +++ b/app/test-eventdev/parser.h > @@ -0,0 +1,79 @@ > +/*- > + * BSD LICENSE > + * > + * Copyright(c) 2010-2016 Intel Corporation. All rights reserved. > + * All rights reserved. > + * > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions > + * are met: > + * > + * * Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * * Redistributions in binary form must reproduce the above copyrig= ht > + * notice, this list of conditions and the following disclaimer in > + * the documentation and/or other materials provided with the > + * distribution. > + * * Neither the name of Intel Corporation nor the names of its > + * contributors may be used to endorse or promote products derived > + * from this software without specific prior written permission. > + * > + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS F= OR > + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGH= T > + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTA= L, > + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US= E, > + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A= NY > + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE U= SE > + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE= . > + */ > + > +#ifndef __INCLUDE_PARSER_H__ > +#define __INCLUDE_PARSER_H__ > + > +#include > + > +#define PARSE_DELIMITER " \f\n\r\t\v" > + > +#define skip_white_spaces(pos) \ > +({ \ > + __typeof__(pos) _p =3D (pos); \ > + for ( ; isspace(*_p); _p++) \ > + ; \ > + _p; \ > +}) > + > +static inline size_t > +skip_digits(const char *src) > +{ > + size_t i; > + > + for (i =3D 0; isdigit(src[i]); i++) > + ; > + > + return i; > +} > + > +int parser_read_arg_bool(const char *p); > + > +int parser_read_uint64(uint64_t *value, const char *p); > +int parser_read_uint32(uint32_t *value, const char *p); > +int parser_read_uint16(uint16_t *value, const char *p); > +int parser_read_uint8(uint8_t *value, const char *p); > + > +int parser_read_uint64_hex(uint64_t *value, const char *p); > +int parser_read_uint32_hex(uint32_t *value, const char *p); > +int parser_read_uint16_hex(uint16_t *value, const char *p); > +int parser_read_uint8_hex(uint8_t *value, const char *p); > + > +int parser_read_int32(int32_t *value, const char *p); > + > +int parse_hex_string(char *src, uint8_t *dst, uint32_t *size); > + > +int parse_tokenize_string(char *string, char *tokens[], uint32_t *n_toke= ns); > + > +int parse_lcores_list(bool lcores[], const char *corelist); > +#endif > -- > 2.13.0