[v9,03/12] app/graph: support parser utility APIs

Message ID 20231018063347.68081-4-skori@marvell.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series add CLI based graph application |

Checks

Context Check Description
ci/checkpatch warning coding style issues

Commit Message

Sunil Kumar Kori Oct. 18, 2023, 6:33 a.m. UTC
  From: Sunil Kumar Kori <skori@marvell.com>

Adds some helper functions to parse IPv4, IPv6 and MAC addresses
string into respective datatype.

Signed-off-by: Sunil Kumar Kori <skori@marvell.com>
Signed-off-by: Rakesh Kudurumalla <rkudurumalla@marvell.com>
Acked-by: Jerin Jacob <jerinj@marvell.com>
---
 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
  

Patch

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 = 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 <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <rte_common.h>
+
+#include "module_api.h"
+
+#define white_spaces_skip(pos)			\
+({						\
+	__typeof__(pos) _p = (pos);		\
+	for ( ; isspace(*_p); _p++)		\
+		;				\
+	_p;					\
+})
+
+static void
+hex_string_to_uint64(uint64_t *dst, const char *hexs)
+{
+	char buf[2] = {0};
+	uint8_t shift = 4;
+	int iter = 0;
+	char c;
+
+	while ((c = *hexs++)) {
+		buf[0] = c;
+		*dst |= (strtol(buf, NULL, 16) << shift);
+		shift -= 4;
+		iter++;
+		if (iter == 2) {
+			iter = 0;
+			shift = 4;
+			dst++;
+		}
+	}
+}
+
+int
+parser_uint64_read(uint64_t *value, const char *p)
+{
+	char *next;
+	uint64_t val;
+
+	p = white_spaces_skip(p);
+	if (!isdigit(*p))
+		return -EINVAL;
+
+	val = strtoul(p, &next, 0);
+	if (p == next)
+		return -EINVAL;
+
+	p = next;
+	switch (*p) {
+	case 'T':
+		val *= 1024ULL;
+		/* fall through */
+	case 'G':
+		val *= 1024ULL;
+		/* fall through */
+	case 'M':
+		val *= 1024ULL;
+		/* fall through */
+	case 'k':
+	case 'K':
+		val *= 1024ULL;
+		p++;
+		break;
+	}
+
+	p = white_spaces_skip(p);
+	if (*p != '\0')
+		return -EINVAL;
+
+	*value = val;
+	return 0;
+}
+
+int
+parser_uint32_read(uint32_t *value, const char *p)
+{
+	uint64_t val = 0;
+	int rc = parser_uint64_read(&val, p);
+
+	if (rc < 0)
+		return rc;
+
+	if (val > UINT32_MAX)
+		return -ERANGE;
+
+	*value = val;
+	return 0;
+}
+
+int
+parser_ip4_read(uint32_t *value, char *p)
+{
+	uint8_t shift = 24;
+	uint32_t ip = 0;
+	char *token;
+
+	token = strtok(p, ".");
+	while (token != NULL) {
+		ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
+		token = strtok(NULL, ".");
+		shift -= 8;
+	}
+
+	*value = ip;
+
+	return 0;
+}
+
+int
+parser_ip6_read(uint8_t *value, char *p)
+{
+	uint64_t val = 0;
+	char *token;
+
+	token = strtok(p, ":");
+	while (token != NULL) {
+		hex_string_to_uint64(&val, token);
+		*value = val;
+		token = strtok(NULL, ":");
+		value++;
+		val = 0;
+	}
+
+	return 0;
+}
+
+int
+parser_mac_read(uint64_t *value, char *p)
+{
+	uint64_t mac = 0, val = 0;
+	uint8_t shift = 40;
+	char *token;
+
+	token = strtok(p, ":");
+	while (token != NULL) {
+		hex_string_to_uint64(&val, token);
+		mac |= val << shift;
+		token = strtok(NULL, ":");
+		shift -= 8;
+		val = 0;
+	}
+
+	*value = 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