DPDK  21.05.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_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_ETHER_MAX_VLAN_FRAME_LEN \
39  (RTE_ETHER_MAX_LEN + 4)
40 
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
60 struct rte_ether_addr {
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 
224 #define RTE_ETHER_ADDR_FMT_SIZE 18
225 
235 void
236 rte_ether_format_addr(char *buf, uint16_t size,
237  const struct rte_ether_addr *eth_addr);
252 __rte_experimental
253 int
254 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
256 /* Windows Sockets headers contain `#define s_addr S_un.S_addr`.
257  * Temporarily disable this macro to avoid conflict at definition.
258  * Place source MAC address in both `s_addr` and `S_un.S_addr` fields,
259  * so that access works either directly or through the macro.
260  */
261 #pragma push_macro("s_addr")
262 #ifdef s_addr
263 #undef s_addr
264 #endif
265 
273  union {
275  struct {
276  struct rte_ether_addr S_addr;
277  } S_un;
278  };
280 } __rte_aligned(2);
281 
282 #pragma pop_macro("s_addr")
283 
289 struct rte_vlan_hdr {
292 } __rte_packed;
293 
294 
295 
296 /* Ethernet frame types */
297 #define RTE_ETHER_TYPE_IPV4 0x0800
298 #define RTE_ETHER_TYPE_IPV6 0x86DD
299 #define RTE_ETHER_TYPE_ARP 0x0806
300 #define RTE_ETHER_TYPE_RARP 0x8035
301 #define RTE_ETHER_TYPE_VLAN 0x8100
302 #define RTE_ETHER_TYPE_QINQ 0x88A8
303 #define RTE_ETHER_TYPE_QINQ1 0x9100
304 #define RTE_ETHER_TYPE_QINQ2 0x9200
305 #define RTE_ETHER_TYPE_QINQ3 0x9300
306 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
307 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
308 #define RTE_ETHER_TYPE_ETAG 0x893F
309 #define RTE_ETHER_TYPE_1588 0x88F7
310 
311 #define RTE_ETHER_TYPE_SLOW 0x8809
312 #define RTE_ETHER_TYPE_TEB 0x6558
313 #define RTE_ETHER_TYPE_LLDP 0x88CC
314 #define RTE_ETHER_TYPE_MPLS 0x8847
315 #define RTE_ETHER_TYPE_MPLSM 0x8848
316 #define RTE_ETHER_TYPE_ECPRI 0xAEFE
329 static inline int rte_vlan_strip(struct rte_mbuf *m)
330 {
331  struct rte_ether_hdr *eh
332  = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
333  struct rte_vlan_hdr *vh;
334 
336  return -1;
337 
338  vh = (struct rte_vlan_hdr *)(eh + 1);
339  m->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
340  m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
341 
342  /* Copy ether header over rather than moving whole packet */
343  memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
344  eh, 2 * RTE_ETHER_ADDR_LEN);
345 
346  return 0;
347 }
348 
361 static inline int rte_vlan_insert(struct rte_mbuf **m)
362 {
363  struct rte_ether_hdr *oh, *nh;
364  struct rte_vlan_hdr *vh;
365 
366  /* Can't insert header if mbuf is shared */
367  if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
368  return -EINVAL;
369 
370  /* Can't insert header if the first segment is too short */
372  return -EINVAL;
373 
374  oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
375  nh = (struct rte_ether_hdr *)
376  rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
377  if (nh == NULL)
378  return -ENOSPC;
379 
380  memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
382 
383  vh = (struct rte_vlan_hdr *) (nh + 1);
384  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
385 
386  (*m)->ol_flags &= ~(PKT_RX_VLAN_STRIPPED | PKT_TX_VLAN);
387 
388  if ((*m)->ol_flags & PKT_TX_TUNNEL_MASK)
389  (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
390  else
391  (*m)->l2_len += sizeof(struct rte_vlan_hdr);
392 
393  return 0;
394 }
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 
400 #endif /* _RTE_ETHER_H_ */
#define __rte_packed
Definition: rte_common.h:86
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:64
#define PKT_RX_VLAN
Definition: rte_mbuf_core.h:50
static int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:147
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:301
#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:218
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:100
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:132
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
__rte_experimental int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
struct rte_ether_addr d_addr
Definition: rte_ether.h:271
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:428
#define rte_pktmbuf_mtod(m, t)
struct rte_ether_hdr::@296::@298 S_un
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:65
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1642
#define RTE_MBUF_DIRECT(mb)
rte_be16_t eth_proto
Definition: rte_ether.h:291
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:361
struct rte_ether_addr s_addr
Definition: rte_ether.h:274
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:179
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1561
#define PKT_RX_VLAN_STRIPPED
Definition: rte_mbuf_core.h:95
#define RTE_STD_C11
Definition: rte_common.h:42
rte_be16_t vlan_tci
Definition: rte_ether.h:290
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:164
uint16_t rte_be16_t
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]
Definition: rte_ether.h:34
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1578
__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:195
rte_be16_t ether_type
Definition: rte_ether.h:279
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]
Definition: rte_ether.h:61
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:117
#define PKT_TX_VLAN