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, RTE_ETHER_ADDR_PRT_FMT,
26 RTE_ETHER_ADDR_BYTES(eth_addr));
29 static int8_t get_xdigit(char ch)
31 if (ch >= '0' && ch <= '9')
33 if (ch >= 'a' && ch <= 'f')
35 if (ch >= 'A' && ch <= 'F')
40 /* Convert 00:11:22:33:44:55 to ethernet address */
41 static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea)
46 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
53 ea->addr_bytes[i] = x << 4;
57 ea->addr_bytes[i] |= x;
59 if (i < RTE_ETHER_ADDR_LEN - 1 &&
64 /* return true if at end of string */
68 /* Convert 0011:2233:4455 to ethernet address */
69 static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
73 for (i = 0; i < RTE_ETHER_ADDR_LEN; i += 2) {
76 for (j = 0; j < 4; j++) {
84 ea->addr_bytes[i] = w >> 8;
85 ea->addr_bytes[i + 1] = w & 0xff;
87 if (i < RTE_ETHER_ADDR_LEN - 2 &&
96 * Like ether_aton_r but can handle either
97 * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX
98 * and is more restrictive.
101 rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
103 if (get_ether_addr6(s, ea))
105 if (get_ether_addr3(s, ea))