mempool: fix slow allocation of large mempools
[dpdk.git] / lib / librte_net / rte_ether.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdbool.h>
6
7 #include <rte_ether.h>
8 #include <rte_errno.h>
9
10 void
11 rte_eth_random_addr(uint8_t *addr)
12 {
13         uint64_t rand = rte_rand();
14         uint8_t *p = (uint8_t *)&rand;
15
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 */
19 }
20
21 void
22 rte_ether_format_addr(char *buf, uint16_t size,
23                       const struct rte_ether_addr *eth_addr)
24 {
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]);
32 }
33
34 static int8_t get_xdigit(char ch)
35 {
36         if (ch >= '0' && ch <= '9')
37                 return ch - '0';
38         if (ch >= 'a' && ch <= 'f')
39                 return ch - 'a' + 10;
40         if (ch >= 'A' && ch <= 'F')
41                 return ch - 'A' + 10;
42         return -1;
43 }
44
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)
47 {
48         const char *s = s0;
49         int i;
50
51         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
52                 int8_t x;
53
54                 x = get_xdigit(*s++);
55                 if (x < 0)
56                         return false;
57
58                 ea->addr_bytes[i] = x << 4;
59                 x = get_xdigit(*s++);
60                 if (x < 0)
61                         return false;
62                 ea->addr_bytes[i] |= x;
63
64                 if (i < RTE_ETHER_ADDR_LEN - 1 &&
65                     *s++ != ':')
66                         return false;
67         }
68
69         /* return true if at end of string */
70         return *s == '\0';
71 }
72
73 /* Convert 0011:2233:4455 to ethernet address */
74 static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea)
75 {
76         int i, j;
77
78         for (i = 0; i < RTE_ETHER_ADDR_LEN; i += 2) {
79                 uint16_t w = 0;
80
81                 for (j = 0; j < 4; j++) {
82                         int8_t x;
83
84                         x = get_xdigit(*s++);
85                         if (x < 0)
86                                 return false;
87                         w = (w << 4) | x;
88                 }
89                 ea->addr_bytes[i] = w >> 8;
90                 ea->addr_bytes[i + 1] = w & 0xff;
91
92                 if (i < RTE_ETHER_ADDR_LEN - 2 &&
93                     *s++ != ':')
94                         return false;
95         }
96
97         return *s == '\0';
98 }
99
100 /*
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.
104  */
105 int
106 rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea)
107 {
108         if (get_ether_addr6(s, ea))
109                 return 0;
110         if (get_ether_addr3(s, ea))
111                 return 0;
112
113         rte_errno = EINVAL;
114         return -1;
115 }