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