DPDK  24.03.0-rc4
rte_ether.h
Go to the documentation of this file.
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 
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
26 #define RTE_ETHER_TYPE_LEN 2
27 #define RTE_ETHER_CRC_LEN 4
28 #define RTE_ETHER_HDR_LEN \
29  (RTE_ETHER_ADDR_LEN * 2 + \
30  RTE_ETHER_TYPE_LEN)
31 #define RTE_ETHER_MIN_LEN 64
32 #define RTE_ETHER_MAX_LEN 1518
33 #define RTE_ETHER_MTU \
34  (RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \
35  RTE_ETHER_CRC_LEN)
37 #define RTE_VLAN_HLEN 4
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
45 #define RTE_ETHER_MAX_VLAN_ID 4095
47 #define RTE_ETHER_MIN_MTU 68
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 
74 struct __rte_aligned(2) rte_ether_addr {
75  uint8_t addr_bytes[RTE_ETHER_ADDR_LEN];
76 };
77 
78 #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
79 #define RTE_ETHER_GROUP_ADDR 0x01
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 
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 
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 
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 
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 
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 
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 
209 static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
210 {
212 }
213 
220 void
221 rte_eth_random_addr(uint8_t *addr);
222 
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 
241 #define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
242 
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 
263 void
264 rte_ether_format_addr(char *buf, uint16_t size,
265  const struct rte_ether_addr *eth_addr);
286 int
287 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
288 
293 struct __rte_aligned(2) rte_ether_hdr {
294  struct rte_ether_addr dst_addr;
295  struct rte_ether_addr src_addr;
296  rte_be16_t ether_type;
297 };
298 
304 struct rte_vlan_hdr {
307 } __rte_packed;
308 
309 
310 
311 /* Ethernet frame types */
312 #define RTE_ETHER_TYPE_IPV4 0x0800
313 #define RTE_ETHER_TYPE_IPV6 0x86DD
314 #define RTE_ETHER_TYPE_ARP 0x0806
315 #define RTE_ETHER_TYPE_RARP 0x8035
316 #define RTE_ETHER_TYPE_VLAN 0x8100
317 #define RTE_ETHER_TYPE_QINQ 0x88A8
318 #define RTE_ETHER_TYPE_QINQ1 0x9100
319 #define RTE_ETHER_TYPE_QINQ2 0x9200
320 #define RTE_ETHER_TYPE_QINQ3 0x9300
321 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
322 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
323 #define RTE_ETHER_TYPE_ETAG 0x893F
324 #define RTE_ETHER_TYPE_1588 0x88F7
325 
326 #define RTE_ETHER_TYPE_SLOW 0x8809
327 #define RTE_ETHER_TYPE_TEB 0x6558
328 #define RTE_ETHER_TYPE_LLDP 0x88CC
329 #define RTE_ETHER_TYPE_MPLS 0x8847
330 #define RTE_ETHER_TYPE_MPLSM 0x8848
331 #define RTE_ETHER_TYPE_ECPRI 0xAEFE
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);
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 
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 */
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_ */
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:78
#define RTE_MBUF_F_RX_VLAN
Definition: rte_mbuf_core.h:51
#define RTE_MBUF_F_TX_VLAN
static int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:161
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:316
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:25
void rte_eth_random_addr(uint8_t *addr)
static void rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from, struct rte_ether_addr *__restrict ea_to)
Definition: rte_ether.h:232
#define RTE_MBUF_F_RX_VLAN_STRIPPED
Definition: rte_mbuf_core.h:71
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:114
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:146
#define __rte_packed
Definition: rte_common.h:102
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:439
#define rte_pktmbuf_mtod(m, t)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:79
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1646
#define RTE_MBUF_DIRECT(mb)
rte_be16_t eth_proto
Definition: rte_ether.h:306
int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
void rte_ether_format_addr(char *buf, uint16_t size, const struct rte_ether_addr *eth_addr)
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:376
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:193
struct __rte_aligned(2) rte_ether_addr
Definition: rte_ether.h:74
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1565
rte_be16_t vlan_tci
Definition: rte_ether.h:305
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:178
uint16_t rte_be16_t
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1582
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:209
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:131