[v3,2/6] net/ether: add rte_eth_unformat_addr

Message ID 20190605010852.28395-3-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers
Series net/ether: improvements and optimizations |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail Compilation issues

Commit Message

Stephen Hemminger June 5, 2019, 1:08 a.m. UTC
  Make a function that coresponds with eth_aton_r which can
be used to convert string to rte_ether_addr.

This also allows rte_ethdev to no longer depend on the
cmdline library.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_net/rte_ether.c         | 47 ++++++++++++++++++++++++++++++
 lib/librte_net/rte_ether.h         | 14 +++++++++
 lib/librte_net/rte_net_version.map |  1 +
 3 files changed, 62 insertions(+)
  

Comments

Andrew Rybchenko June 5, 2019, 8:55 a.m. UTC | #1
On 6/5/19 4:08 AM, Stephen Hemminger wrote:
> Make a function that coresponds with eth_aton_r which can

I guess ether_aton_r

> be used to convert string to rte_ether_addr.
>
> This also allows rte_ethdev to no longer depend on the
> cmdline library.
>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>   lib/librte_net/rte_ether.c         | 47 ++++++++++++++++++++++++++++++
>   lib/librte_net/rte_ether.h         | 14 +++++++++
>   lib/librte_net/rte_net_version.map |  1 +
>   3 files changed, 62 insertions(+)
>
> diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c
> index 974fe815b335..acc8a0e938c5 100644
> --- a/lib/librte_net/rte_ether.c
> +++ b/lib/librte_net/rte_ether.c
> @@ -27,3 +27,50 @@ rte_ether_format_addr(char *buf, uint16_t size,
>   		 eth_addr->addr_bytes[4],
>   		 eth_addr->addr_bytes[5]);
>   }
> +
> +/*
> + * Like ether_aton_r but can handle either
> + * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
> + */
> +int
> +rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
> +{
> +	unsigned int o0, o1, o2, o3, o4, o5;
> +	int n;
> +
> +	n = sscanf(s, "%x:%x:%x:%x:%x:%x",
> +		    &o0, &o1, &o2, &o3, &o4, &o5);

Is it intended that the following input will pass?
  "af:bb:03:65:dc:ddobar" or  "af:bb:03:65:dc:dd foobar"

I guess yes since ether_aton() accepts it. If so,
Reviewed-by: Andrew Rybchenko <arybchenko@solarflare.com>
  
Stephen Hemminger June 5, 2019, 4:31 p.m. UTC | #2
On Wed, 5 Jun 2019 11:55:21 +0300
Andrew Rybchenko <arybchenko@solarflare.com> wrote:

> On 6/5/19 4:08 AM, Stephen Hemminger wrote:
> > Make a function that coresponds with eth_aton_r which can  
> 
> I guess ether_aton_r
> 
> > be used to convert string to rte_ether_addr.
> >
> > This also allows rte_ethdev to no longer depend on the
> > cmdline library.
> >
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> >   lib/librte_net/rte_ether.c         | 47 ++++++++++++++++++++++++++++++
> >   lib/librte_net/rte_ether.h         | 14 +++++++++
> >   lib/librte_net/rte_net_version.map |  1 +
> >   3 files changed, 62 insertions(+)
> >
> > diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c
> > index 974fe815b335..acc8a0e938c5 100644
> > --- a/lib/librte_net/rte_ether.c
> > +++ b/lib/librte_net/rte_ether.c
> > @@ -27,3 +27,50 @@ rte_ether_format_addr(char *buf, uint16_t size,
> >   		 eth_addr->addr_bytes[4],
> >   		 eth_addr->addr_bytes[5]);
> >   }
> > +
> > +/*
> > + * Like ether_aton_r but can handle either
> > + * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
> > + */
> > +int
> > +rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
> > +{
> > +	unsigned int o0, o1, o2, o3, o4, o5;
> > +	int n;
> > +
> > +	n = sscanf(s, "%x:%x:%x:%x:%x:%x",
> > +		    &o0, &o1, &o2, &o3, &o4, &o5);  
> 
> Is it intended that the following input will pass?
>   "af:bb:03:65:dc:ddobar" or  "af:bb:03:65:dc:dd foobar"
> 
> I guess yes since ether_aton() accepts it. If so,
> Reviewed-by: Andrew Rybchenko <arybchenko@solarflare.com>

you are right ether_aton has same logic.
doing something more complex is possible but not really needed.
  

Patch

diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c
index 974fe815b335..acc8a0e938c5 100644
--- a/lib/librte_net/rte_ether.c
+++ b/lib/librte_net/rte_ether.c
@@ -27,3 +27,50 @@  rte_ether_format_addr(char *buf, uint16_t size,
 		 eth_addr->addr_bytes[4],
 		 eth_addr->addr_bytes[5]);
 }
+
+/*
+ * Like ether_aton_r but can handle either
+ * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
+ */
+int
+rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
+{
+	unsigned int o0, o1, o2, o3, o4, o5;
+	int n;
+
+	n = sscanf(s, "%x:%x:%x:%x:%x:%x",
+		    &o0, &o1, &o2, &o3, &o4, &o5);
+
+	if (n == 6) {
+		if (o0 > UINT8_MAX || o1 > UINT8_MAX || o2 > UINT8_MAX ||
+		    o3 > UINT8_MAX || o4 > UINT8_MAX || o5 > UINT8_MAX)
+			return -1;
+
+		ea->addr_bytes[0] = o0;
+		ea->addr_bytes[1] = o1;
+		ea->addr_bytes[2] = o2;
+		ea->addr_bytes[3] = o3;
+		ea->addr_bytes[4] = o4;
+		ea->addr_bytes[5] = o5;
+
+		return 0;
+	}
+
+	/* Support the format XXXX:XXXX:XXXX */
+	n = sscanf(s, "%x:%x:%x", &o0, &o1, &o2);
+	if (n == 3) {
+		if (o0 > UINT16_MAX || o1 > UINT16_MAX || o2 > UINT16_MAX)
+			return -1;
+
+		ea->addr_bytes[0] = o0 >> 8;
+		ea->addr_bytes[1] = o0 & 0xff;
+		ea->addr_bytes[2] = o1 >> 8;
+		ea->addr_bytes[3] = o1 & 0xff;
+		ea->addr_bytes[4] = o2 >> 8;
+		ea->addr_bytes[5] = o2 & 0xff;
+		return 0;
+	}
+	/* unknown format */
+
+	return -1;
+}
diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h
index 3caae0d98f6d..8edc7e217b25 100644
--- a/lib/librte_net/rte_ether.h
+++ b/lib/librte_net/rte_ether.h
@@ -250,6 +250,20 @@  static inline void rte_ether_addr_copy(const struct rte_ether_addr *ea_from,
 void
 rte_ether_format_addr(char *buf, uint16_t size,
 		      const struct rte_ether_addr *eth_addr);
+/**
+ * Convert string with Ethernet address to an ether_addr.
+ *
+ * @param str
+ *   A pointer to buffer contains the formatted MAC address.
+ * @param eth_addr
+ *   A pointer to a ether_addr structure.
+ * @return
+ *   0 if successful
+ *   -1 and sets rte_errno if invalid string
+ */
+int __rte_experimental
+rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
+
 /**
  * Ethernet header: Contains the destination address, source address
  * and frame type.
diff --git a/lib/librte_net/rte_net_version.map b/lib/librte_net/rte_net_version.map
index dae6e420cded..8776534b731d 100644
--- a/lib/librte_net/rte_net_version.map
+++ b/lib/librte_net/rte_net_version.map
@@ -25,4 +25,5 @@  EXPERIMENTAL {
 
 	rte_net_make_rarp_packet;
 	rte_net_skip_ip6_ext;
+	rte_eth_unformat_addr;
 };