DPDK  21.11.7-rc1
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_memcpy.h>
22 #include <rte_random.h>
23 #include <rte_mbuf.h>
24 #include <rte_byteorder.h>
25 
26 #define RTE_ETHER_ADDR_LEN 6
27 #define RTE_ETHER_TYPE_LEN 2
28 #define RTE_ETHER_CRC_LEN 4
29 #define RTE_ETHER_HDR_LEN \
30  (RTE_ETHER_ADDR_LEN * 2 + \
31  RTE_ETHER_TYPE_LEN)
32 #define RTE_ETHER_MIN_LEN 64
33 #define RTE_ETHER_MAX_LEN 1518
34 #define RTE_ETHER_MTU \
35  (RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \
36  RTE_ETHER_CRC_LEN)
38 #define RTE_VLAN_HLEN 4
40 #define RTE_ETHER_MAX_VLAN_FRAME_LEN \
41  (RTE_ETHER_MAX_LEN + RTE_VLAN_HLEN)
42 
43 #define RTE_ETHER_MAX_JUMBO_FRAME_LEN \
44  0x3F00
46 #define RTE_ETHER_MAX_VLAN_ID 4095
48 #define RTE_ETHER_MIN_MTU 68
50 /* VLAN header fields */
51 #define RTE_VLAN_DEI_SHIFT 12
52 #define RTE_VLAN_PRI_SHIFT 13
53 #define RTE_VLAN_PRI_MASK 0xe000 /* Priority Code Point */
54 #define RTE_VLAN_DEI_MASK 0x1000 /* Drop Eligible Indicator */
55 #define RTE_VLAN_ID_MASK 0x0fff /* VLAN Identifier */
56 
57 #define RTE_VLAN_TCI_ID(vlan_tci) ((vlan_tci) & RTE_VLAN_ID_MASK)
58 #define RTE_VLAN_TCI_PRI(vlan_tci) (((vlan_tci) & RTE_VLAN_PRI_MASK) >> RTE_VLAN_PRI_SHIFT)
59 #define RTE_VLAN_TCI_DEI(vlan_tci) (((vlan_tci) & RTE_VLAN_DEI_MASK) >> RTE_VLAN_DEI_SHIFT)
60 #define RTE_VLAN_TCI_MAKE(id, pri, dei) ((id) | \
61  ((pri) << RTE_VLAN_PRI_SHIFT) | \
62  ((dei) << RTE_VLAN_DEI_SHIFT))
63 
77 } __rte_aligned(2);
78 
79 #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
80 #define RTE_ETHER_GROUP_ADDR 0x01
96 static inline int rte_is_same_ether_addr(const struct rte_ether_addr *ea1,
97  const struct rte_ether_addr *ea2)
98 {
99  const uint16_t *w1 = (const uint16_t *)ea1;
100  const uint16_t *w2 = (const uint16_t *)ea2;
101 
102  return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0;
103 }
104 
115 static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
116 {
117  const uint16_t *w = (const uint16_t *)ea;
118 
119  return (w[0] | w[1] | w[2]) == 0;
120 }
121 
132 static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
133 {
134  return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0;
135 }
136 
147 static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
148 {
149  return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR;
150 }
151 
162 static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
163 {
164  const uint16_t *w = (const uint16_t *)ea;
165 
166  return (w[0] & w[1] & w[2]) == 0xFFFF;
167 }
168 
179 static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
180 {
181  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
182 }
183 
194 static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
195 {
196  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
197 }
198 
210 static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
211 {
213 }
214 
221 void
222 rte_eth_random_addr(uint8_t *addr);
223 
232 static inline void
233 rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
234  struct rte_ether_addr *__restrict ea_to)
235 {
236  *ea_to = *ea_from;
237 }
238 
242 #define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
243 
246 #define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
247  ((mac_addrs)->addr_bytes[1]), \
248  ((mac_addrs)->addr_bytes[2]), \
249  ((mac_addrs)->addr_bytes[3]), \
250  ((mac_addrs)->addr_bytes[4]), \
251  ((mac_addrs)->addr_bytes[5])
252 
253 #define RTE_ETHER_ADDR_FMT_SIZE 18
254 
264 void
265 rte_ether_format_addr(char *buf, uint16_t size,
266  const struct rte_ether_addr *eth_addr);
281 int
282 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
292 } __rte_aligned(2);
293 
299 struct rte_vlan_hdr {
302 } __rte_packed;
303 
304 
305 
306 /* Ethernet frame types */
307 #define RTE_ETHER_TYPE_IPV4 0x0800
308 #define RTE_ETHER_TYPE_IPV6 0x86DD
309 #define RTE_ETHER_TYPE_ARP 0x0806
310 #define RTE_ETHER_TYPE_RARP 0x8035
311 #define RTE_ETHER_TYPE_VLAN 0x8100
312 #define RTE_ETHER_TYPE_QINQ 0x88A8
313 #define RTE_ETHER_TYPE_QINQ1 0x9100
314 #define RTE_ETHER_TYPE_QINQ2 0x9200
315 #define RTE_ETHER_TYPE_QINQ3 0x9300
316 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
317 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
318 #define RTE_ETHER_TYPE_ETAG 0x893F
319 #define RTE_ETHER_TYPE_1588 0x88F7
320 
321 #define RTE_ETHER_TYPE_SLOW 0x8809
322 #define RTE_ETHER_TYPE_TEB 0x6558
323 #define RTE_ETHER_TYPE_LLDP 0x88CC
324 #define RTE_ETHER_TYPE_MPLS 0x8847
325 #define RTE_ETHER_TYPE_MPLSM 0x8848
326 #define RTE_ETHER_TYPE_ECPRI 0xAEFE
339 static inline int rte_vlan_strip(struct rte_mbuf *m)
340 {
341  struct rte_ether_hdr *eh
342  = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
343  struct rte_vlan_hdr *vh;
344 
346  return -1;
347 
348  vh = (struct rte_vlan_hdr *)(eh + 1);
350  m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
351 
352  /* Copy ether header over rather than moving whole packet */
353  memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
354  eh, 2 * RTE_ETHER_ADDR_LEN);
355 
356  return 0;
357 }
358 
371 static inline int rte_vlan_insert(struct rte_mbuf **m)
372 {
373  struct rte_ether_hdr *oh, *nh;
374  struct rte_vlan_hdr *vh;
375 
376  /* Can't insert header if mbuf is shared */
377  if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
378  return -EINVAL;
379 
380  /* Can't insert header if the first segment is too short */
382  return -EINVAL;
383 
384  oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
385  nh = (struct rte_ether_hdr *)(void *)
386  rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
387  if (nh == NULL)
388  return -ENOSPC;
389 
390  memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
392 
393  vh = (struct rte_vlan_hdr *) (nh + 1);
394  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
395 
396  (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
397 
398  if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
399  (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
400  else
401  (*m)->l2_len += sizeof(struct rte_vlan_hdr);
402 
403  return 0;
404 }
405 
406 #ifdef __cplusplus
407 }
408 #endif
409 
410 #endif /* _RTE_ETHER_H_ */
#define __rte_packed
Definition: rte_common.h:86
struct rte_ether_addr src_addr
Definition: rte_ether.h:284
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:79
#define RTE_MBUF_F_RX_VLAN
Definition: rte_mbuf_core.h:50
#define RTE_MBUF_F_TX_VLAN
static int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:162
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:311
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:26
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:233
#define RTE_MBUF_F_RX_VLAN_STRIPPED
Definition: rte_mbuf_core.h:75
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:115
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:147
struct rte_ether_addr dst_addr
Definition: rte_ether.h:283
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:404
#define rte_pktmbuf_mtod(m, t)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:80
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1612
#define RTE_MBUF_DIRECT(mb)
rte_be16_t eth_proto
Definition: rte_ether.h:301
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:371
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:194
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1531
rte_be16_t vlan_tci
Definition: rte_ether.h:300
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:179
uint16_t rte_be16_t
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1548
__extension__ struct rte_eth_link __rte_aligned(8)
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:210
rte_be16_t ether_type
Definition: rte_ether.h:291
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]
Definition: rte_ether.h:76
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:132