xref: /dpdk/lib/net/rte_ether.h (revision e214d58eb87b8af488272f084220617ecabb510a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef _RTE_ETHER_H_
6 #define _RTE_ETHER_H_
7 
8 /**
9  * @file
10  *
11  * Ethernet Helpers in RTE
12  */
13 
14 #include <assert.h>
15 #include <stdalign.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 
19 #include <rte_random.h>
20 #include <rte_mbuf.h>
21 #include <rte_byteorder.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define RTE_ETHER_ADDR_LEN  6 /**< Length of Ethernet address. */
28 #define RTE_ETHER_TYPE_LEN  2 /**< Length of Ethernet type field. */
29 #define RTE_ETHER_CRC_LEN   4 /**< Length of Ethernet CRC. */
30 #define RTE_ETHER_HDR_LEN   \
31 	(RTE_ETHER_ADDR_LEN * 2 + \
32 		RTE_ETHER_TYPE_LEN) /**< Length of Ethernet header. */
33 #define RTE_ETHER_MIN_LEN   64    /**< Minimum frame len, including CRC. */
34 #define RTE_ETHER_MAX_LEN   1518  /**< Maximum frame len, including CRC. */
35 #define RTE_ETHER_MTU       \
36 	(RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \
37 		RTE_ETHER_CRC_LEN) /**< Ethernet MTU. */
38 
39 #define RTE_VLAN_HLEN       4  /**< VLAN (IEEE 802.1Q) header length. */
40 /** Maximum VLAN frame length (excluding QinQ), including CRC. */
41 #define RTE_ETHER_MAX_VLAN_FRAME_LEN \
42 	(RTE_ETHER_MAX_LEN + RTE_VLAN_HLEN)
43 
44 #define RTE_ETHER_MAX_JUMBO_FRAME_LEN \
45 	0x3F00 /**< Maximum Jumbo frame length, including CRC. */
46 
47 #define RTE_ETHER_MAX_VLAN_ID  4095 /**< Maximum VLAN ID. */
48 
49 #define RTE_ETHER_MIN_MTU 68 /**< Minimum MTU for IPv4 packets, see RFC 791. */
50 
51 /* VLAN header fields */
52 #define RTE_VLAN_DEI_SHIFT	12
53 #define RTE_VLAN_PRI_SHIFT	13
54 #define RTE_VLAN_PRI_MASK	0xe000 /* Priority Code Point */
55 #define RTE_VLAN_DEI_MASK	0x1000 /* Drop Eligible Indicator */
56 #define RTE_VLAN_ID_MASK	0x0fff /* VLAN Identifier */
57 
58 #define RTE_VLAN_TCI_ID(vlan_tci)	((vlan_tci) & RTE_VLAN_ID_MASK)
59 #define RTE_VLAN_TCI_PRI(vlan_tci)	(((vlan_tci) & RTE_VLAN_PRI_MASK) >> RTE_VLAN_PRI_SHIFT)
60 #define RTE_VLAN_TCI_DEI(vlan_tci)	(((vlan_tci) & RTE_VLAN_DEI_MASK) >> RTE_VLAN_DEI_SHIFT)
61 #define RTE_VLAN_TCI_MAKE(id, pri, dei)	((id) |					\
62 					 ((pri) << RTE_VLAN_PRI_SHIFT) |	\
63 					 ((dei) << RTE_VLAN_DEI_SHIFT))
64 
65 /**
66  * Ethernet address:
67  * A universally administered address is uniquely assigned to a device by its
68  * manufacturer. The first three octets (in transmission order) contain the
69  * Organizationally Unique Identifier (OUI). The following three (MAC-48 and
70  * EUI-48) octets are assigned by that organization with the only constraint
71  * of uniqueness.
72  * A locally administered address is assigned to a device by a network
73  * administrator and does not contain OUIs.
74  * See http://standards.ieee.org/regauth/groupmac/tutorial.html
75  */
76 struct __rte_aligned(2) rte_ether_addr {
77 	uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]; /**< Addr bytes in tx order */
78 };
79 
80 static_assert(sizeof(struct rte_ether_addr) == 6,
81 		"sizeof(struct rte_ether_addr) == 6");
82 static_assert(alignof(struct rte_ether_addr) == 2,
83 		"alignof(struct rte_ether_addr) == 2");
84 
85 #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */
86 #define RTE_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 rte_is_same_ether_addr(const struct rte_ether_addr *ea1,
103 				     const struct rte_ether_addr *ea2)
104 {
105 	const uint16_t *w1 = (const uint16_t *)ea1;
106 	const uint16_t *w2 = (const uint16_t *)ea2;
107 
108 	return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0;
109 }
110 
111 /**
112  * Check if an Ethernet address is filled with zeros.
113  *
114  * @param ea
115  *   A pointer to a ether_addr structure containing the ethernet address
116  *   to check.
117  * @return
118  *   True  (1) if the given ethernet address is filled with zeros;
119  *   false (0) otherwise.
120  */
121 static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
122 {
123 	const uint16_t *w = (const uint16_t *)ea;
124 
125 	return (w[0] | w[1] | w[2]) == 0;
126 }
127 
128 /**
129  * Check if an Ethernet address is a unicast address.
130  *
131  * @param ea
132  *   A pointer to a ether_addr structure containing the ethernet address
133  *   to check.
134  * @return
135  *   True  (1) if the given ethernet address is a unicast address;
136  *   false (0) otherwise.
137  */
138 static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
139 {
140 	return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0;
141 }
142 
143 /**
144  * Check if an Ethernet address is a multicast address.
145  *
146  * @param ea
147  *   A pointer to a ether_addr structure containing the ethernet address
148  *   to check.
149  * @return
150  *   True  (1) if the given ethernet address is a multicast address;
151  *   false (0) otherwise.
152  */
153 static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
154 {
155 	return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR;
156 }
157 
158 /**
159  * Check if an Ethernet address is a broadcast address.
160  *
161  * @param ea
162  *   A pointer to a ether_addr structure containing the ethernet address
163  *   to check.
164  * @return
165  *   True  (1) if the given ethernet address is a broadcast address;
166  *   false (0) otherwise.
167  */
168 static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
169 {
170 	const uint16_t *w = (const uint16_t *)ea;
171 
172 	return (w[0] & w[1] & w[2]) == 0xFFFF;
173 }
174 
175 /**
176  * Check if an Ethernet address is a universally assigned address.
177  *
178  * @param ea
179  *   A pointer to a ether_addr structure containing the ethernet address
180  *   to check.
181  * @return
182  *   True  (1) if the given ethernet address is a universally assigned address;
183  *   false (0) otherwise.
184  */
185 static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
186 {
187 	return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
188 }
189 
190 /**
191  * Check if an Ethernet address is a locally assigned address.
192  *
193  * @param ea
194  *   A pointer to a ether_addr structure containing the ethernet address
195  *   to check.
196  * @return
197  *   True  (1) if the given ethernet address is a locally assigned address;
198  *   false (0) otherwise.
199  */
200 static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
201 {
202 	return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
203 }
204 
205 /**
206  * Check if an Ethernet address is a valid address. Checks that the address is a
207  * unicast address and is not filled with zeros.
208  *
209  * @param ea
210  *   A pointer to a ether_addr structure containing the ethernet address
211  *   to check.
212  * @return
213  *   True  (1) if the given ethernet address is valid;
214  *   false (0) otherwise.
215  */
216 static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
217 {
218 	return rte_is_unicast_ether_addr(ea) && (!rte_is_zero_ether_addr(ea));
219 }
220 
221 /**
222  * Generate a random Ethernet address that is locally administered
223  * and not multicast.
224  * @param addr
225  *   A pointer to Ethernet address.
226  */
227 void
228 rte_eth_random_addr(uint8_t *addr);
229 
230 /**
231  * Copy an Ethernet address.
232  *
233  * @param ea_from
234  *   A pointer to a ether_addr structure holding the Ethernet address to copy.
235  * @param ea_to
236  *   A pointer to a ether_addr structure where to copy the Ethernet address.
237  */
238 static inline void
239 rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
240 		    struct rte_ether_addr *__restrict ea_to)
241 {
242 	*ea_to = *ea_from;
243 }
244 
245 /**
246  * Macro to print six-bytes of MAC address in hex format
247  */
248 #define RTE_ETHER_ADDR_PRT_FMT     "%02X:%02X:%02X:%02X:%02X:%02X"
249 /**
250  * Macro to extract the MAC address bytes from rte_ether_addr struct
251  */
252 #define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
253 					 ((mac_addrs)->addr_bytes[1]), \
254 					 ((mac_addrs)->addr_bytes[2]), \
255 					 ((mac_addrs)->addr_bytes[3]), \
256 					 ((mac_addrs)->addr_bytes[4]), \
257 					 ((mac_addrs)->addr_bytes[5])
258 
259 #define RTE_ETHER_ADDR_FMT_SIZE         18
260 /**
261  * Format 48bits Ethernet address in pattern xx:xx:xx:xx:xx:xx.
262  *
263  * @param buf
264  *   A pointer to buffer contains the formatted MAC address.
265  * @param size
266  *   The format buffer size.
267  * @param eth_addr
268  *   A pointer to a ether_addr structure.
269  */
270 void
271 rte_ether_format_addr(char *buf, uint16_t size,
272 		      const struct rte_ether_addr *eth_addr);
273 /**
274  * Convert string with Ethernet address to an ether_addr.
275  *
276  * @param str
277  *   A pointer to buffer contains the formatted MAC address.
278  *   Accepts either byte or word format separated by colon,
279  *   hyphen or period.
280  *
281  *   The example formats are:
282  *     XX:XX:XX:XX:XX:XX - Canonical form
283  *     XX-XX-XX-XX-XX-XX - Windows and IEEE 802
284  *     XXXX.XXXX.XXXX    - Cisco
285  *   where XX is a hex digit: 0-9, a-f, or A-F.
286  *   In the byte format, leading zeros are optional.
287  * @param eth_addr
288  *   A pointer to a ether_addr structure.
289  * @return
290  *   0 if successful
291  *   -1 and sets rte_errno if invalid string
292  */
293 int
294 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
295 
296 /**
297  * Ethernet header: Contains the destination address, source address
298  * and frame type.
299  */
300 struct rte_ether_hdr {
301 	struct rte_ether_addr dst_addr; /**< Destination address. */
302 	struct rte_ether_addr src_addr; /**< Source address. */
303 	rte_be16_t ether_type; /**< Frame type. */
304 };
305 
306 static_assert(sizeof(struct rte_ether_hdr) == 14,
307 		"sizeof(struct rte_ether_hdr) == 14");
308 static_assert(alignof(struct rte_ether_hdr) == 2,
309 		"alignof(struct rte_ether_hdr) == 2");
310 
311 /**
312  * Ethernet VLAN Header.
313  * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet type
314  * of the encapsulated frame.
315  */
316 struct rte_vlan_hdr {
317 	rte_be16_t vlan_tci;  /**< Priority (3) + CFI (1) + Identifier Code (12) */
318 	rte_be16_t eth_proto; /**< Ethernet type of encapsulated frame. */
319 };
320 
321 static_assert(sizeof(struct rte_vlan_hdr) == 4,
322 		"sizeof(struct rte_vlan_hdr) == 4");
323 static_assert(alignof(struct rte_vlan_hdr) == 2,
324 		"alignof(struct rte_vlan_hdr) == 2");
325 
326 
327 
328 /* Ethernet frame types */
329 #define RTE_ETHER_TYPE_IPV4 0x0800 /**< IPv4 Protocol. */
330 #define RTE_ETHER_TYPE_IPV6 0x86DD /**< IPv6 Protocol. */
331 #define RTE_ETHER_TYPE_ARP  0x0806 /**< Arp Protocol. */
332 #define RTE_ETHER_TYPE_RARP 0x8035 /**< Reverse Arp Protocol. */
333 #define RTE_ETHER_TYPE_VLAN 0x8100 /**< IEEE 802.1Q VLAN tagging. */
334 #define RTE_ETHER_TYPE_QINQ 0x88A8 /**< IEEE 802.1ad QinQ tagging. */
335 #define RTE_ETHER_TYPE_QINQ1 0x9100 /**< Deprecated QinQ VLAN. */
336 #define RTE_ETHER_TYPE_QINQ2 0x9200 /**< Deprecated QinQ VLAN. */
337 #define RTE_ETHER_TYPE_QINQ3 0x9300 /**< Deprecated QinQ VLAN. */
338 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863 /**< PPPoE Discovery Stage. */
339 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864 /**< PPPoE Session Stage. */
340 #define RTE_ETHER_TYPE_ETAG 0x893F /**< IEEE 802.1BR E-Tag. */
341 #define RTE_ETHER_TYPE_1588 0x88F7
342 	/**< IEEE 802.1AS 1588 Precise Time Protocol. */
343 #define RTE_ETHER_TYPE_SLOW 0x8809 /**< Slow protocols (LACP and Marker). */
344 #define RTE_ETHER_TYPE_TEB  0x6558 /**< Transparent Ethernet Bridging. */
345 #define RTE_ETHER_TYPE_LLDP 0x88CC /**< LLDP Protocol. */
346 #define RTE_ETHER_TYPE_MPLS 0x8847 /**< MPLS ethertype. */
347 #define RTE_ETHER_TYPE_MPLSM 0x8848 /**< MPLS multicast ethertype. */
348 #define RTE_ETHER_TYPE_ECPRI 0xAEFE /**< eCPRI ethertype (.1Q supported). */
349 
350 /**
351  * Extract VLAN tag information into mbuf
352  *
353  * Software version of VLAN stripping
354  *
355  * @param m
356  *   The packet mbuf.
357  * @return
358  *   - 0: Success
359  *   - 1: not a vlan packet
360  */
361 static inline int rte_vlan_strip(struct rte_mbuf *m)
362 {
363 	struct rte_ether_hdr *eh
364 		 = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
365 	struct rte_vlan_hdr *vh;
366 
367 	if (eh->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
368 		return -1;
369 
370 	vh = (struct rte_vlan_hdr *)(eh + 1);
371 	m->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
372 	m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
373 
374 	/* Copy ether header over rather than moving whole packet */
375 	memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
376 		eh, 2 * RTE_ETHER_ADDR_LEN);
377 
378 	return 0;
379 }
380 
381 /**
382  * Insert VLAN tag into mbuf.
383  *
384  * Software version of VLAN unstripping
385  *
386  * @param m
387  *   The packet mbuf.
388  * @return
389  *   - 0: On success
390  *   -EPERM: mbuf is shared overwriting would be unsafe
391  *   -ENOSPC: not enough headroom in mbuf
392  */
393 static inline int rte_vlan_insert(struct rte_mbuf **m)
394 {
395 	struct rte_ether_hdr *oh, *nh;
396 	struct rte_vlan_hdr *vh;
397 
398 	/* Can't insert header if mbuf is shared */
399 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
400 		return -EINVAL;
401 
402 	/* Can't insert header if the first segment is too short */
403 	if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN)
404 		return -EINVAL;
405 
406 	oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
407 	nh = (struct rte_ether_hdr *)(void *)
408 		rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
409 	if (nh == NULL)
410 		return -ENOSPC;
411 
412 	memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
413 	nh->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
414 
415 	vh = (struct rte_vlan_hdr *) (nh + 1);
416 	vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
417 
418 	(*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
419 
420 	if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
421 		(*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
422 	else
423 		(*m)->l2_len += sizeof(struct rte_vlan_hdr);
424 
425 	return 0;
426 }
427 
428 #ifdef __cplusplus
429 }
430 #endif
431 
432 #endif /* _RTE_ETHER_H_ */
433