DPDK  23.11.0
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
62 } __rte_aligned(2);
63 
64 #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
65 #define RTE_ETHER_GROUP_ADDR 0x01
81 static inline int rte_is_same_ether_addr(const struct rte_ether_addr *ea1,
82  const struct rte_ether_addr *ea2)
83 {
84  const uint16_t *w1 = (const uint16_t *)ea1;
85  const uint16_t *w2 = (const uint16_t *)ea2;
86 
87  return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0;
88 }
89 
100 static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
101 {
102  const uint16_t *w = (const uint16_t *)ea;
103 
104  return (w[0] | w[1] | w[2]) == 0;
105 }
106 
117 static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
118 {
119  return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0;
120 }
121 
132 static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
133 {
134  return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR;
135 }
136 
147 static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
148 {
149  const uint16_t *w = (const uint16_t *)ea;
150 
151  return (w[0] & w[1] & w[2]) == 0xFFFF;
152 }
153 
164 static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
165 {
166  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
167 }
168 
179 static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
180 {
181  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
182 }
183 
195 static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
196 {
198 }
199 
206 void
207 rte_eth_random_addr(uint8_t *addr);
208 
217 static inline void
218 rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
219  struct rte_ether_addr *__restrict ea_to)
220 {
221  *ea_to = *ea_from;
222 }
223 
227 #define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
231 #define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
232  ((mac_addrs)->addr_bytes[1]), \
233  ((mac_addrs)->addr_bytes[2]), \
234  ((mac_addrs)->addr_bytes[3]), \
235  ((mac_addrs)->addr_bytes[4]), \
236  ((mac_addrs)->addr_bytes[5])
237 
238 #define RTE_ETHER_ADDR_FMT_SIZE 18
249 void
250 rte_ether_format_addr(char *buf, uint16_t size,
251  const struct rte_ether_addr *eth_addr);
272 int
273 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
274 
280  struct rte_ether_addr dst_addr;
281  struct rte_ether_addr src_addr;
283 } __rte_aligned(2);
284 
290 struct rte_vlan_hdr {
293 } __rte_packed;
294 
295 
296 
297 /* Ethernet frame types */
298 #define RTE_ETHER_TYPE_IPV4 0x0800
299 #define RTE_ETHER_TYPE_IPV6 0x86DD
300 #define RTE_ETHER_TYPE_ARP 0x0806
301 #define RTE_ETHER_TYPE_RARP 0x8035
302 #define RTE_ETHER_TYPE_VLAN 0x8100
303 #define RTE_ETHER_TYPE_QINQ 0x88A8
304 #define RTE_ETHER_TYPE_QINQ1 0x9100
305 #define RTE_ETHER_TYPE_QINQ2 0x9200
306 #define RTE_ETHER_TYPE_QINQ3 0x9300
307 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
308 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
309 #define RTE_ETHER_TYPE_ETAG 0x893F
310 #define RTE_ETHER_TYPE_1588 0x88F7
312 #define RTE_ETHER_TYPE_SLOW 0x8809
313 #define RTE_ETHER_TYPE_TEB 0x6558
314 #define RTE_ETHER_TYPE_LLDP 0x88CC
315 #define RTE_ETHER_TYPE_MPLS 0x8847
316 #define RTE_ETHER_TYPE_MPLSM 0x8848
317 #define RTE_ETHER_TYPE_ECPRI 0xAEFE
330 static inline int rte_vlan_strip(struct rte_mbuf *m)
331 {
332  struct rte_ether_hdr *eh
333  = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
334  struct rte_vlan_hdr *vh;
335 
337  return -1;
338 
339  vh = (struct rte_vlan_hdr *)(eh + 1);
342 
343  /* Copy ether header over rather than moving whole packet */
344  memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
345  eh, 2 * RTE_ETHER_ADDR_LEN);
346 
347  return 0;
348 }
349 
362 static inline int rte_vlan_insert(struct rte_mbuf **m)
363 {
364  struct rte_ether_hdr *oh, *nh;
365  struct rte_vlan_hdr *vh;
366 
367  /* Can't insert header if mbuf is shared */
368  if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
369  return -EINVAL;
370 
371  /* Can't insert header if the first segment is too short */
373  return -EINVAL;
374 
375  oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
376  nh = (struct rte_ether_hdr *)(void *)
377  rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
378  if (nh == NULL)
379  return -ENOSPC;
380 
381  memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
383 
384  vh = (struct rte_vlan_hdr *) (nh + 1);
385  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
386 
387  (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
388 
389  if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
390  (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
391  else
392  (*m)->l2_len += sizeof(struct rte_vlan_hdr);
393 
394  return 0;
395 }
396 
397 #ifdef __cplusplus
398 }
399 #endif
400 
401 #endif /* _RTE_ETHER_H_ */
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
uint16_t rte_be16_t
#define __rte_packed
Definition: rte_common.h:89
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:302
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:25
static int rte_is_same_ether_addr(const struct rte_ether_addr *ea1, const struct rte_ether_addr *ea2)
Definition: rte_ether.h:81
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:117
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:64
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:164
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:100
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:195
int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:65
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:330
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:179
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:362
void rte_ether_format_addr(char *buf, uint16_t size, const struct rte_ether_addr *eth_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:218
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:132
void rte_eth_random_addr(uint8_t *addr)
static int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:147
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1646
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1565
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:439
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1582
#define rte_pktmbuf_mtod(m, t)
#define RTE_MBUF_F_TX_VLAN
#define RTE_MBUF_DIRECT(mb)
#define RTE_MBUF_F_RX_VLAN_STRIPPED
Definition: rte_mbuf_core.h:70
#define RTE_MBUF_F_RX_VLAN
Definition: rte_mbuf_core.h:50
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]
Definition: rte_ether.h:61
rte_be16_t ether_type
Definition: rte_ether.h:282
struct rte_ether_addr src_addr
Definition: rte_ether.h:281
struct rte_ether_addr dst_addr
Definition: rte_ether.h:280
uint64_t ol_flags
uint16_t vlan_tci
rte_be16_t eth_proto
Definition: rte_ether.h:292
rte_be16_t vlan_tci
Definition: rte_ether.h:291