DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Olivier Matz <olivier.matz@6wind.com>
Subject: [RFC] rte_ether_unformat: accept more inputs
Date: Fri, 29 Sep 2023 09:36:11 -0700	[thread overview]
Message-ID: <20230929163611.62691-1-stephen@networkplumber.org> (raw)

This updates rte_ether_addr_unformat() to accept more types
of input. There have been requests to handle Windows and other formats.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
Marking this as RFC until unit tests are added.

 lib/net/rte_ether.c | 78 +++++++++++++++++++++++++++++++++++----------
 lib/net/rte_ether.h |  6 ++--
 2 files changed, 66 insertions(+), 18 deletions(-)

diff --git a/lib/net/rte_ether.c b/lib/net/rte_ether.c
index 66d9a9d0699a..5250353eb162 100644
--- a/lib/net/rte_ether.c
+++ b/lib/net/rte_ether.c
@@ -38,7 +38,8 @@ static int8_t get_xdigit(char ch)
 }
 
 /* Convert 00:11:22:33:44:55 to ethernet address */
-static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea)
+static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea,
+			    const char sep)
 {
 	const char *s = s0;
 	int i;
@@ -50,14 +51,17 @@ static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea)
 		if (x < 0)
 			return false;
 
-		ea->addr_bytes[i] = x << 4;
-		x = get_xdigit(*s++);
-		if (x < 0)
-			return false;
-		ea->addr_bytes[i] |= x;
+		ea->addr_bytes[i] = x;
+		if (*s != sep) {
+			x = get_xdigit(*s++);
+			if (x < 0)
+				return false;
+			ea->addr_bytes[i] <<= 4;
+			ea->addr_bytes[i] |= x;
+		}
 
 		if (i < RTE_ETHER_ADDR_LEN - 1 &&
-		    *s++ != ':')
+		    *s++ != sep)
 			return false;
 	}
 
@@ -66,7 +70,8 @@ static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea)
 }
 
 /* Convert 0011:2233:4455 to ethernet address */
-static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
+static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea,
+			    const char sep)
 {
 	int i, j;
 
@@ -80,12 +85,14 @@ static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
 			if (x < 0)
 				return false;
 			w = (w << 4) | x;
+			if (*s == sep)
+				break;
 		}
 		ea->addr_bytes[i] = w >> 8;
 		ea->addr_bytes[i + 1] = w & 0xff;
 
 		if (i < RTE_ETHER_ADDR_LEN - 2 &&
-		    *s++ != ':')
+		    *s++ != sep)
 			return false;
 	}
 
@@ -93,17 +100,56 @@ static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
 }
 
 /*
- * Like ether_aton_r but can handle either
- * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
- * and is more restrictive.
+ * Scan input to see if seperated by dash, colon or period
+ * Returns seperator and number of matches
+ * If seperators are mixed will return
+ */
+static unsigned int get_ether_sep(const char *s, char *sep)
+{
+	const char seperators[] = "-:.";
+	unsigned int count = 0;
+	const char *cp;
+
+	cp = strpbrk(s, seperators);
+	if (cp == NULL)
+		return 0;
+
+	*sep = *cp;
+	do {
+		++count;
+		/* find next instance of seperator */
+		cp = strchr(cp + 1, *sep);
+	} while (cp != NULL);
+
+	return count;
+}
+
+/*
+ * Be libreal in accepting a wide variety of notational formats
+ * for MAC address including:
+ *  - Linux format six groups of hexadecimal digits seperated by colon
+ *  - Windows format six groups seperated by hyphen
+ *  - two groups hexadecimal digits
  */
 int
 rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
 {
-	if (get_ether_addr6(s, ea))
-		return 0;
-	if (get_ether_addr3(s, ea))
-		return 0;
+	unsigned int count;
+	char sep = '\0';
+
+	count = get_ether_sep(s, &sep);
+	switch (count) {
+	case 5:	/* i.e 01:23:45:67:89:AB */
+		if (get_ether_addr6(s, ea, sep))
+			return 0;
+		break;
+	case 2: /* i.e 0123.4567.89AB */
+		if (get_ether_addr3(s, ea, sep))
+			return 0;
+		break;
+	default:
+		break;
+	}
 
 	rte_errno = EINVAL;
 	return -1;
diff --git a/lib/net/rte_ether.h b/lib/net/rte_ether.h
index b35c72c7b0e0..e9a4ba9b5860 100644
--- a/lib/net/rte_ether.h
+++ b/lib/net/rte_ether.h
@@ -254,8 +254,10 @@ rte_ether_format_addr(char *buf, uint16_t size,
  *
  * @param str
  *   A pointer to buffer contains the formatted MAC address.
- *   The supported formats are:
- *     XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
+ *   The example formats are:
+ *     XX:XX:XX:XX:XX:XX - Canonical form
+ *     XX-XX-XX-XX-XX-XX - Windows and IEEE 802
+ *     XXXX:XXXX:XXXX    - original DPDK
  *   where XX is a hex digit: 0-9, a-f, or A-F.
  * @param eth_addr
  *   A pointer to a ether_addr structure.
-- 
2.39.2


             reply	other threads:[~2023-09-29 16:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-29 16:36 Stephen Hemminger [this message]
2023-09-29 19:35 ` Morten Brørup
2023-09-29 23:08   ` Stephen Hemminger
2023-10-02 18:37 ` [PATCH v2 0/3] rte_ether_unformat_addr changes Stephen Hemminger
2023-10-02 18:37   ` [PATCH v2 1/3] test: remove some strings from cmdline_etheraddr tests Stephen Hemminger
2023-10-03 10:47     ` Ferruh Yigit
2023-10-03 10:59       ` Ferruh Yigit
2023-10-03 16:38         ` Stephen Hemminger
2023-10-03 16:36       ` Stephen Hemminger
2023-10-03 16:50         ` Ferruh Yigit
2023-10-03 17:18           ` Stephen Hemminger
2023-10-02 18:37   ` [PATCH v2 2/3] rte_ether_unformat: accept more inputs Stephen Hemminger
2023-10-02 18:37   ` [PATCH v2 3/3] test: add tests for rte_ether routines Stephen Hemminger
2023-10-03  6:17   ` [PATCH v2 0/3] rte_ether_unformat_addr changes Morten Brørup
2023-10-03 10:44   ` Ferruh Yigit
2023-10-03 16:27     ` Stephen Hemminger
2023-10-03 16:34     ` Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 0/4] rte_ether_unformat_addr related changes Stephen Hemminger
2023-10-03 20:29   ` [PATCH v3 1/4] test: remove some strings from cmdline_etheraddr tests Stephen Hemminger
2023-10-03 20:29   ` [PATCH v3 2/4] rte_ether_unformat: accept more inputs Stephen Hemminger
2023-10-03 20:29   ` [PATCH v3 3/4] test: add tests for rte_ether routines Stephen Hemminger
2023-10-03 20:29   ` [PATCH v3 4/4] net/tap: use rte_ether_unformat_address Stephen Hemminger
2023-10-11 14:25     ` Ferruh Yigit
2023-10-04 11:48   ` [PATCH v3 0/4] rte_ether_unformat_addr related changes Ferruh Yigit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230929163611.62691-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=olivier.matz@6wind.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).