From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B0111431DE; Mon, 23 Oct 2023 09:04:10 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A0DCA40608; Mon, 23 Oct 2023 09:04:10 +0200 (CEST) Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) by mails.dpdk.org (Postfix) with ESMTP id 5F45040608 for ; Mon, 23 Oct 2023 09:04:09 +0200 (CEST) Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-4a13374a1e8so1218391e0c.1 for ; Mon, 23 Oct 2023 00:04:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698044649; x=1698649449; darn=dpdk.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=42tDxNLeojOtWexJgjDtKfiWYlWR2b1oJ/l/HeAyAOQ=; b=k+9Ikl/j9Fp7JeOrDkTHJ+syWSCSeTQonLQJ9UdC9qZEv6/6kmk6I1mdBAil7GJynD 59sN/8XkI0tv5C0Tp+guP+VgiiuapAApWNUOtj7QaaIUINYuSJ2rHHVxfh5P2ColxJGZ WxPwA+MP2apiuYjsWL8dP1P8qviXKRPY0AoMFPPplQfLMMWePjiRuxf1v1BBEffqgc/b gcsPz7QDej3Mt6zAXr/s3MwS4z8jCRwPY5BrPtyKMTMcpgdtM5HYSYLhx2mJitTgMOFj 7ARMX10tkI3H1hl9jxf7Ne2n53/cj0P87wt2UZv6RQCEEYFZaQEtjENESazNH+wGYBkb Jl9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698044649; x=1698649449; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=42tDxNLeojOtWexJgjDtKfiWYlWR2b1oJ/l/HeAyAOQ=; b=QoABWno9spBDg+RMyDMNbmk0p3y87BDhLfGDURtXa6nKNU1d3xgrvdsqZn32d1DiUJ 2uv8ojbRM1boF5M0xBx428pUo1fRbJxWkwcjnIGE/H+CggWWNJeIzjimUsnh+btEvB/M KScyKgRm+8Q5KOQItIYj2ohQlcqKQRZOqaPpocOQHzLkjOGf5rwoN0dbtuhAStuBD+gP Uhm0ov2P1pPxufJq6N2XKldAhwJ1K2hIyafL6QptDoG7VxuNXVM4ZcwmpGNPS6Uk20oW fP4TgNzE3kCqcV44S+pet+V3yNZVoVtJQDyJtQ2zOE5tdZ7nVLIXgWNDgGz9vAbtaHr4 30oQ== X-Gm-Message-State: AOJu0YzZJIJZjEFIdnrQMarvyP64U7Pb68ZcvZnz0vriv5z7QZkCMD65 9Ufi4m6Q9onwbmNCTB7ur7lz4JO2dRIuASOlllBiZogW2kA= X-Google-Smtp-Source: AGHT+IFOrgq9MUTkiCKasIZJQSbt7uI51q/BJU1Oatw/PO1zmYSO24EjxYYDOCgteRdIwffoZxeZCdYtJ4Gd9e1oRx0= X-Received: by 2002:a05:6122:308b:b0:495:dcd0:471 with SMTP id cd11-20020a056122308b00b00495dcd00471mr8940644vkb.5.1698044648697; Mon, 23 Oct 2023 00:04:08 -0700 (PDT) MIME-Version: 1.0 References: <20231019105000.520914-13-skori@marvell.com> <20231019173011.1186656-1-skori@marvell.com> <20231019173011.1186656-4-skori@marvell.com> In-Reply-To: <20231019173011.1186656-4-skori@marvell.com> From: Nithin Dabilpuram Date: Mon, 23 Oct 2023 12:33:55 +0530 Message-ID: Subject: Re: [PATCH v11 03/12] app/graph: support parser utility APIs To: skori@marvell.com Cc: Rakesh Kudurumalla , dev@dpdk.org, Jerin Jacob Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Acked-By: Nithin Dabilpuram On Thu, Oct 19, 2023 at 11:00=E2=80=AFPM wrote: > > From: Sunil Kumar Kori > > Adds some helper functions to parse IPv4, IPv6 and MAC addresses > string into respective datatype. > > Signed-off-by: Sunil Kumar Kori > Signed-off-by: Rakesh Kudurumalla > Acked-by: Jerin Jacob > --- > app/graph/meson.build | 1 + > app/graph/module_api.h | 1 + > app/graph/utils.c | 156 +++++++++++++++++++++++++++++++++++++++++ > app/graph/utils.h | 14 ++++ > 4 files changed, 172 insertions(+) > create mode 100644 app/graph/utils.c > create mode 100644 app/graph/utils.h > > diff --git a/app/graph/meson.build b/app/graph/meson.build > index c8d2b41b69..fd71036a95 100644 > --- a/app/graph/meson.build > +++ b/app/graph/meson.build > @@ -13,4 +13,5 @@ sources =3D files( > 'cli.c', > 'conn.c', > 'main.c', > + 'utils.c', > ) > diff --git a/app/graph/module_api.h b/app/graph/module_api.h > index 9826303f0c..ad4fb50989 100644 > --- a/app/graph/module_api.h > +++ b/app/graph/module_api.h > @@ -10,6 +10,7 @@ > > #include "cli.h" > #include "conn.h" > +#include "utils.h" > /* > * Externs > */ > diff --git a/app/graph/utils.c b/app/graph/utils.c > new file mode 100644 > index 0000000000..c7b6ae83cf > --- /dev/null > +++ b/app/graph/utils.c > @@ -0,0 +1,156 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2023 Marvell. > + */ > + > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +#include "module_api.h" > + > +#define white_spaces_skip(pos) \ > +({ \ > + __typeof__(pos) _p =3D (pos); \ > + for ( ; isspace(*_p); _p++) \ > + ; \ > + _p; \ > +}) > + > +static void > +hex_string_to_uint64(uint64_t *dst, const char *hexs) > +{ > + char buf[2] =3D {0}; > + uint8_t shift =3D 4; > + int iter =3D 0; > + char c; > + > + while ((c =3D *hexs++)) { > + buf[0] =3D c; > + *dst |=3D (strtol(buf, NULL, 16) << shift); > + shift -=3D 4; > + iter++; > + if (iter =3D=3D 2) { > + iter =3D 0; > + shift =3D 4; > + dst++; > + } > + } > +} > + > +int > +parser_uint64_read(uint64_t *value, const char *p) > +{ > + char *next; > + uint64_t val; > + > + p =3D white_spaces_skip(p); > + if (!isdigit(*p)) > + return -EINVAL; > + > + val =3D strtoul(p, &next, 0); > + 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 white_spaces_skip(p); > + if (*p !=3D '\0') > + return -EINVAL; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_uint32_read(uint32_t *value, const char *p) > +{ > + uint64_t val =3D 0; > + int rc =3D parser_uint64_read(&val, p); > + > + if (rc < 0) > + return rc; > + > + if (val > UINT32_MAX) > + return -ERANGE; > + > + *value =3D val; > + return 0; > +} > + > +int > +parser_ip4_read(uint32_t *value, char *p) > +{ > + uint8_t shift =3D 24; > + uint32_t ip =3D 0; > + char *token; > + > + token =3D strtok(p, "."); > + while (token !=3D NULL) { > + ip |=3D (((uint32_t)strtoul(token, NULL, 10)) << shift); > + token =3D strtok(NULL, "."); > + shift -=3D 8; > + } > + > + *value =3D ip; > + > + return 0; > +} > + > +int > +parser_ip6_read(uint8_t *value, char *p) > +{ > + uint64_t val =3D 0; > + char *token; > + > + token =3D strtok(p, ":"); > + while (token !=3D NULL) { > + hex_string_to_uint64(&val, token); > + *value =3D val; > + token =3D strtok(NULL, ":"); > + value++; > + val =3D 0; > + } > + > + return 0; > +} > + > +int > +parser_mac_read(uint64_t *value, char *p) > +{ > + uint64_t mac =3D 0, val =3D 0; > + uint8_t shift =3D 40; > + char *token; > + > + token =3D strtok(p, ":"); > + while (token !=3D NULL) { > + hex_string_to_uint64(&val, token); > + mac |=3D val << shift; > + token =3D strtok(NULL, ":"); > + shift -=3D 8; > + val =3D 0; > + } > + > + *value =3D mac; > + > + return 0; > +} > diff --git a/app/graph/utils.h b/app/graph/utils.h > new file mode 100644 > index 0000000000..0ebb5de55a > --- /dev/null > +++ b/app/graph/utils.h > @@ -0,0 +1,14 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2023 Marvell. > + */ > + > +#ifndef APP_GRAPH_UTILS_H > +#define APP_GRAPH_UTILS_H > + > +int parser_uint64_read(uint64_t *value, const char *p); > +int parser_uint32_read(uint32_t *value, const char *p); > +int parser_ip4_read(uint32_t *value, char *p); > +int parser_ip6_read(uint8_t *value, char *p); > +int parser_mac_read(uint64_t *value, char *p); > + > +#endif > -- > 2.25.1 >