ethdev: function to compare ethernet addresses
[dpdk.git] / lib / librte_ether / rte_ether.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_ETHER_H_
35 #define _RTE_ETHER_H_
36
37 /**
38  * @file
39  *
40  * Ethernet Helpers in RTE
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <stdint.h>
48
49 #include <rte_memcpy.h>
50 #include <rte_random.h>
51
52 #define ETHER_ADDR_LEN  6 /**< Length of Ethernet address. */
53 #define ETHER_TYPE_LEN  2 /**< Length of Ethernet type field. */
54 #define ETHER_CRC_LEN   4 /**< Length of Ethernet CRC. */
55 #define ETHER_HDR_LEN   \
56         (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) /**< Length of Ethernet header. */
57 #define ETHER_MIN_LEN   64    /**< Minimum frame len, including CRC. */
58 #define ETHER_MAX_LEN   1518  /**< Maximum frame len, including CRC. */
59 #define ETHER_MTU       \
60         (ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN) /**< Ethernet MTU. */
61
62 #define ETHER_MAX_VLAN_FRAME_LEN \
63         (ETHER_MAX_LEN + 4) /**< Maximum VLAN frame length, including CRC. */
64
65 #define ETHER_MAX_JUMBO_FRAME_LEN \
66         0x3F00 /**< Maximum Jumbo frame length, including CRC. */
67
68 #define ETHER_MAX_VLAN_ID  4095 /**< Maximum VLAN ID. */
69
70 /**
71  * Ethernet address:
72  * A universally administered address is uniquely assigned to a device by its
73  * manufacturer. The first three octets (in transmission order) contain the
74  * Organizationally Unique Identifier (OUI). The following three (MAC-48 and
75  * EUI-48) octets are assigned by that organization with the only constraint
76  * of uniqueness.
77  * A locally administered address is assigned to a device by a network
78  * administrator and does not contain OUIs.
79  * See http://standards.ieee.org/regauth/groupmac/tutorial.html
80  */
81 struct ether_addr {
82         uint8_t addr_bytes[ETHER_ADDR_LEN]; /**< Address bytes in transmission order */
83 } __attribute__((__packed__));
84
85 #define ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */
86 #define ETHER_GROUP_ADDR       0x01 /**< Multicast or broadcast Eth. address. */
87
88 /**
89  * Check if two Ethernet addresses are the same.
90  *
91  * @param ea1
92  *  A pointer to the first ether_addr structure containing
93  *  the ethernet address.
94  * @param ea2
95  *  A pointer to the second ether_addr structure containing
96  *  the ethernet address.
97  *
98  * @return
99  *  True  (1) if the given two ethernet address are the same;
100  *  False (0) otherwise.
101  */
102 static inline int is_same_ether_addr(const struct ether_addr *ea1,
103                                      const struct ether_addr *ea2)
104 {
105         int i;
106         for (i = 0; i < ETHER_ADDR_LEN; i++)
107                 if (ea1->addr_bytes[i] != ea2->addr_bytes[i])
108                         return 0;
109         return 1;
110 }
111
112 /**
113  * Check if an Ethernet address is filled with zeros.
114  *
115  * @param ea
116  *   A pointer to a ether_addr structure containing the ethernet address
117  *   to check.
118  * @return
119  *   True  (1) if the given ethernet address is filled with zeros;
120  *   false (0) otherwise.
121  */
122 static inline int is_zero_ether_addr(const struct ether_addr *ea)
123 {
124         int i;
125         for (i = 0; i < ETHER_ADDR_LEN; i++)
126                 if (ea->addr_bytes[i] != 0x00)
127                         return 0;
128         return 1;
129 }
130
131 /**
132  * Check if an Ethernet address is a unicast address.
133  *
134  * @param ea
135  *   A pointer to a ether_addr structure containing the ethernet address
136  *   to check.
137  * @return
138  *   True  (1) if the given ethernet address is a unicast address;
139  *   false (0) otherwise.
140  */
141 static inline int is_unicast_ether_addr(const struct ether_addr *ea)
142 {
143         return ((ea->addr_bytes[0] & ETHER_GROUP_ADDR) == 0);
144 }
145
146 /**
147  * Check if an Ethernet address is a multicast address.
148  *
149  * @param ea
150  *   A pointer to a ether_addr structure containing the ethernet address
151  *   to check.
152  * @return
153  *   True  (1) if the given ethernet address is a multicast address;
154  *   false (0) otherwise.
155  */
156 static inline int is_multicast_ether_addr(const struct ether_addr *ea)
157 {
158         return (ea->addr_bytes[0] & ETHER_GROUP_ADDR);
159 }
160
161 /**
162  * Check if an Ethernet address is a broadcast address.
163  *
164  * @param ea
165  *   A pointer to a ether_addr structure containing the ethernet address
166  *   to check.
167  * @return
168  *   True  (1) if the given ethernet address is a broadcast address;
169  *   false (0) otherwise.
170  */
171 static inline int is_broadcast_ether_addr(const struct ether_addr *ea)
172 {
173         const uint16_t *ea_words = (const uint16_t *)ea;
174
175         return (ea_words[0] == 0xFFFF && ea_words[1] == 0xFFFF &&
176                 ea_words[2] == 0xFFFF);
177 }
178
179 /**
180  * Check if an Ethernet address is a universally assigned address.
181  *
182  * @param ea
183  *   A pointer to a ether_addr structure containing the ethernet address
184  *   to check.
185  * @return
186  *   True  (1) if the given ethernet address is a universally assigned address;
187  *   false (0) otherwise.
188  */
189 static inline int is_universal_ether_addr(const struct ether_addr *ea)
190 {
191         return ((ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) == 0);
192 }
193
194 /**
195  * Check if an Ethernet address is a locally assigned address.
196  *
197  * @param ea
198  *   A pointer to a ether_addr structure containing the ethernet address
199  *   to check.
200  * @return
201  *   True  (1) if the given ethernet address is a locally assigned address;
202  *   false (0) otherwise.
203  */
204 static inline int is_local_admin_ether_addr(const struct ether_addr *ea)
205 {
206         return ((ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) == 1);
207 }
208
209 /**
210  * Check if an Ethernet address is a valid address. Checks that the address is a
211  * unicast address and is not filled with zeros.
212  *
213  * @param ea
214  *   A pointer to a ether_addr structure containing the ethernet address
215  *   to check.
216  * @return
217  *   True  (1) if the given ethernet address is valid;
218  *   false (0) otherwise.
219  */
220 static inline int is_valid_assigned_ether_addr(const struct ether_addr *ea)
221 {
222         return (is_unicast_ether_addr(ea) && (! is_zero_ether_addr(ea)));
223 }
224
225 /**
226  * Generate a random Ethernet address that is locally administered
227  * and not multicast.
228  * @param addr
229  *   A pointer to Ethernet address.
230  */
231 static inline void eth_random_addr(uint8_t *addr)
232 {
233         uint64_t rand = rte_rand();
234         uint8_t *p = (uint8_t*)&rand;
235
236         rte_memcpy(addr, p, ETHER_ADDR_LEN);
237         addr[0] &= ~ETHER_GROUP_ADDR;       /* clear multicast bit */
238         addr[0] |= ETHER_LOCAL_ADMIN_ADDR;  /* set local assignment bit */
239 }
240
241 /**
242  * Fast copy an Ethernet address.
243  *
244  * @param ea_from
245  *   A pointer to a ether_addr structure holding the Ethernet address to copy.
246  * @param ea_to
247  *   A pointer to a ether_addr structure where to copy the Ethernet address.
248  */
249 static inline void ether_addr_copy(const struct ether_addr *ea_from,
250                                    struct ether_addr *ea_to)
251 {
252 #ifdef __INTEL_COMPILER
253         uint16_t *from_words = (uint16_t *)(ea_from->addr_bytes);
254         uint16_t *to_words   = (uint16_t *)(ea_to->addr_bytes);
255
256         to_words[0] = from_words[0];
257         to_words[1] = from_words[1];
258         to_words[2] = from_words[2];
259 #else
260         /*
261          * Use the common way, because of a strange gcc warning.
262          */
263         *ea_to = *ea_from;
264 #endif
265 }
266
267 /**
268  * Ethernet header: Contains the destination address, source address
269  * and frame type.
270  */
271 struct ether_hdr {
272         struct ether_addr d_addr; /**< Destination address. */
273         struct ether_addr s_addr; /**< Source address. */
274         uint16_t ether_type;      /**< Frame type. */
275 } __attribute__((__packed__));
276
277 /**
278  * Ethernet VLAN Header.
279  * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet type
280  * of the encapsulated frame.
281  */
282 struct vlan_hdr {
283         uint16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */
284         uint16_t eth_proto;/**< Ethernet type of encapsulated frame. */
285 } __attribute__((__packed__));
286
287 /* Ethernet frame types */
288 #define ETHER_TYPE_IPv4 0x0800 /**< IPv4 Protocol. */
289 #define ETHER_TYPE_IPv6 0x86DD /**< IPv6 Protocol. */
290 #define ETHER_TYPE_ARP  0x0806 /**< Arp Protocol. */
291 #define ETHER_TYPE_RARP 0x8035 /**< Reverse Arp Protocol. */
292 #define ETHER_TYPE_VLAN 0x8100 /**< IEEE 802.1Q VLAN tagging. */
293 #define ETHER_TYPE_1588 0x88F7 /**< IEEE 802.1AS 1588 Precise Time Protocol. */
294
295 #ifdef __cplusplus
296 }
297 #endif
298
299 #endif /* _RTE_ETHER_H_ */