ether: new function to format mac address
authorCunming Liang <cunming.liang@intel.com>
Wed, 12 Nov 2014 06:24:32 +0000 (14:24 +0800)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Wed, 12 Nov 2014 23:48:16 +0000 (00:48 +0100)
Signed-off-by: Cunming Liang <cunming.liang@intel.com>
12 files changed:
app/test-pmd/config.c
app/test-pmd/icmpecho.c
app/test-pmd/rxonly.c
examples/dpdk_qat/main.c
examples/ip_fragmentation/main.c
examples/ip_reassembly/main.c
examples/ipv4_multicast/main.c
examples/l3fwd-acl/main.c
examples/l3fwd-power/main.c
examples/l3fwd-vf/main.c
examples/l3fwd/main.c
lib/librte_ether/rte_ether.h

index 9bc08f4..b102b72 100644 (file)
 static void
 print_ethaddr(const char *name, struct ether_addr *eth_addr)
 {
-       printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-              (unsigned int)eth_addr->addr_bytes[0],
-              (unsigned int)eth_addr->addr_bytes[1],
-              (unsigned int)eth_addr->addr_bytes[2],
-              (unsigned int)eth_addr->addr_bytes[3],
-              (unsigned int)eth_addr->addr_bytes[4],
-              (unsigned int)eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 void
index 7fd4b6d..774924e 100644 (file)
@@ -235,18 +235,6 @@ ip_proto_name(uint8_t ip_proto)
        return "UNASSIGNED";
 }
 
-static void
-ether_addr_to_hexa(const struct ether_addr *ea, char *buf)
-{
-       sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
-               ea->addr_bytes[0],
-               ea->addr_bytes[1],
-               ea->addr_bytes[2],
-               ea->addr_bytes[3],
-               ea->addr_bytes[4],
-               ea->addr_bytes[5]);
-}
-
 static void
 ipv4_addr_to_dot(uint32_t be_ipv4_addr, char *buf)
 {
@@ -261,9 +249,9 @@ ipv4_addr_to_dot(uint32_t be_ipv4_addr, char *buf)
 static void
 ether_addr_dump(const char *what, const struct ether_addr *ea)
 {
-       char buf[18];
+       char buf[ETHER_ADDR_FMT_SIZE];
 
-       ether_addr_to_hexa(ea, buf);
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, ea);
        if (what)
                printf("%s", what);
        printf("%s", buf);
index 4410c3d..9ad1df6 100644 (file)
@@ -94,14 +94,9 @@ static const char *pkt_rx_flag_names[MAX_PKT_RX_FLAGS] = {
 static inline void
 print_ether_addr(const char *what, struct ether_addr *eth_addr)
 {
-       printf("%s%02X:%02X:%02X:%02X:%02X:%02X",
-              what,
-              eth_addr->addr_bytes[0],
-              eth_addr->addr_bytes[1],
-              eth_addr->addr_bytes[2],
-              eth_addr->addr_bytes[3],
-              eth_addr->addr_bytes[4],
-              eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", what, buf);
 }
 
 /*
index 59a97bc..127ee40 100644 (file)
@@ -592,13 +592,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 static int
index 65c4cf6..0ad8fca 100644 (file)
@@ -600,13 +600,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, struct ether_addr *eth_addr)
 {
-       printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-              eth_addr->addr_bytes[0],
-              eth_addr->addr_bytes[1],
-              eth_addr->addr_bytes[2],
-              eth_addr->addr_bytes[3],
-              eth_addr->addr_bytes[4],
-              eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 /* Check the link status of all ports in up to 9s, and print them finally */
index 0d92b3c..39d60ec 100644 (file)
@@ -729,13 +729,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 /* Check the link status of all ports in up to 9s, and print them finally */
index de5e6be..590d11a 100644 (file)
@@ -586,13 +586,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, struct ether_addr *eth_addr)
 {
-       printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-              eth_addr->addr_bytes[0],
-              eth_addr->addr_bytes[1],
-              eth_addr->addr_bytes[2],
-              eth_addr->addr_bytes[3],
-              eth_addr->addr_bytes[4],
-              eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 static int
index e31a7d7..f12e5bb 100644 (file)
@@ -1820,13 +1820,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 static int
index 12e4fd6..76a512b 100644 (file)
@@ -1240,13 +1240,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
index 0288d99..b560b7d 100644 (file)
@@ -826,13 +826,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
index 181d682..46ac68e 100644 (file)
@@ -1976,13 +1976,9 @@ parse_args(int argc, char **argv)
 static void
 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
 {
-       printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
-               eth_addr->addr_bytes[0],
-               eth_addr->addr_bytes[1],
-               eth_addr->addr_bytes[2],
-               eth_addr->addr_bytes[3],
-               eth_addr->addr_bytes[4],
-               eth_addr->addr_bytes[5]);
+       char buf[ETHER_ADDR_FMT_SIZE];
+       ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
+       printf("%s%s", name, buf);
 }
 
 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
index f173509..187608d 100644 (file)
@@ -45,6 +45,7 @@ extern "C" {
 #endif
 
 #include <stdint.h>
+#include <stdio.h>
 
 #include <rte_memcpy.h>
 #include <rte_random.h>
@@ -266,6 +267,30 @@ static inline void ether_addr_copy(const struct ether_addr *ea_from,
 #endif
 }
 
+#define ETHER_ADDR_FMT_SIZE         18
+/**
+ * Format 48bits Ethernet address in pattern xx:xx:xx:xx:xx:xx.
+ *
+ * @param buf
+ *   A pointer to buffer contains the formatted MAC address.
+ * @param size
+ *   The format buffer size.
+ * @param ea_to
+ *   A pointer to a ether_addr structure.
+ */
+static inline void
+ether_format_addr(char *buf, uint16_t size,
+                 const struct ether_addr *eth_addr)
+{
+       snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X",
+                eth_addr->addr_bytes[0],
+                eth_addr->addr_bytes[1],
+                eth_addr->addr_bytes[2],
+                eth_addr->addr_bytes[3],
+                eth_addr->addr_bytes[4],
+                eth_addr->addr_bytes[5]);
+}
+
 /**
  * Ethernet header: Contains the destination address, source address
  * and frame type.