From: Cunming Liang Date: Wed, 12 Nov 2014 06:24:32 +0000 (+0800) Subject: ether: new function to format mac address X-Git-Tag: spdx-start~10163 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=ec3d82db2dc13d3b96b7d97801d85dcbaaa1d1cb;p=dpdk.git ether: new function to format mac address Signed-off-by: Cunming Liang --- diff --git a/app/test-pmd/config.c b/app/test-pmd/config.c index 9bc08f434b..b102b72199 100644 --- a/app/test-pmd/config.c +++ b/app/test-pmd/config.c @@ -99,13 +99,9 @@ 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 diff --git a/app/test-pmd/icmpecho.c b/app/test-pmd/icmpecho.c index 7fd4b6df4f..774924e072 100644 --- a/app/test-pmd/icmpecho.c +++ b/app/test-pmd/icmpecho.c @@ -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); diff --git a/app/test-pmd/rxonly.c b/app/test-pmd/rxonly.c index 4410c3d71d..9ad1df6c41 100644 --- a/app/test-pmd/rxonly.c +++ b/app/test-pmd/rxonly.c @@ -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); } /* diff --git a/examples/dpdk_qat/main.c b/examples/dpdk_qat/main.c index 59a97bc483..127ee40cf8 100644 --- a/examples/dpdk_qat/main.c +++ b/examples/dpdk_qat/main.c @@ -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 diff --git a/examples/ip_fragmentation/main.c b/examples/ip_fragmentation/main.c index 65c4cf6849..0ad8fcac86 100644 --- a/examples/ip_fragmentation/main.c +++ b/examples/ip_fragmentation/main.c @@ -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 */ diff --git a/examples/ip_reassembly/main.c b/examples/ip_reassembly/main.c index 0d92b3caf6..39d60ec00a 100644 --- a/examples/ip_reassembly/main.c +++ b/examples/ip_reassembly/main.c @@ -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 */ diff --git a/examples/ipv4_multicast/main.c b/examples/ipv4_multicast/main.c index de5e6be21e..590d11a326 100644 --- a/examples/ipv4_multicast/main.c +++ b/examples/ipv4_multicast/main.c @@ -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 diff --git a/examples/l3fwd-acl/main.c b/examples/l3fwd-acl/main.c index e31a7d7b35..f12e5bb8a6 100644 --- a/examples/l3fwd-acl/main.c +++ b/examples/l3fwd-acl/main.c @@ -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 diff --git a/examples/l3fwd-power/main.c b/examples/l3fwd-power/main.c index 12e4fd6b66..76a512b75f 100644 --- a/examples/l3fwd-power/main.c +++ b/examples/l3fwd-power/main.c @@ -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) diff --git a/examples/l3fwd-vf/main.c b/examples/l3fwd-vf/main.c index 0288d99a3e..b560b7d9da 100644 --- a/examples/l3fwd-vf/main.c +++ b/examples/l3fwd-vf/main.c @@ -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) diff --git a/examples/l3fwd/main.c b/examples/l3fwd/main.c index 181d682895..46ac68e255 100644 --- a/examples/l3fwd/main.c +++ b/examples/l3fwd/main.c @@ -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) diff --git a/lib/librte_ether/rte_ether.h b/lib/librte_ether/rte_ether.h index f173509ea3..187608d539 100644 --- a/lib/librte_ether/rte_ether.h +++ b/lib/librte_ether/rte_ether.h @@ -45,6 +45,7 @@ extern "C" { #endif #include +#include #include #include @@ -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.