1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 rte_eth_random_addr(uint8_t *addr)
13 uint64_t rand = rte_rand();
14 uint8_t *p = (uint8_t *)&rand;
16 rte_memcpy(addr, p, RTE_ETHER_ADDR_LEN);
17 addr[0] &= (uint8_t)~RTE_ETHER_GROUP_ADDR; /* clear multicast bit */
18 addr[0] |= RTE_ETHER_LOCAL_ADMIN_ADDR; /* set local assignment bit */
22 rte_ether_format_addr(char *buf, uint16_t size,
23 const struct rte_ether_addr *eth_addr)
25 snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X",
26 eth_addr->addr_bytes[0],
27 eth_addr->addr_bytes[1],
28 eth_addr->addr_bytes[2],
29 eth_addr->addr_bytes[3],
30 eth_addr->addr_bytes[4],
31 eth_addr->addr_bytes[5]);
34 static int8_t get_xdigit(char ch)
36 if (ch >= '0' && ch <= '9')
38 if (ch >= 'a' && ch <= 'f')
40 if (ch >= 'A' && ch <= 'F')
45 /* Convert 00:11:22:33:44:55 to ethernet address */
46 static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea)
51 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
58 ea->addr_bytes[i] = x << 4;
62 ea->addr_bytes[i] |= x;
64 if (i < RTE_ETHER_ADDR_LEN - 1 &&
69 /* return true if at end of string */
73 /* Convert 0011:2233:4455 to ethernet address */
74 static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
78 for (i = 0; i < RTE_ETHER_ADDR_LEN; i += 2) {
81 for (j = 0; j < 4; j++) {
89 ea->addr_bytes[i] = w >> 8;
90 ea->addr_bytes[i + 1] = w & 0xff;
92 if (i < RTE_ETHER_ADDR_LEN - 2 &&
101 * Like ether_aton_r but can handle either
102 * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
103 * and is more restrictive.
106 rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
108 if (get_ether_addr6(s, ea))
110 if (get_ether_addr3(s, ea))