DPDK  20.05.0-rc0
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 {
61  uint8_t addr_bytes[RTE_ETHER_ADDR_LEN];
62 } __attribute__((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 *ea_words = (const uint16_t *)ea;
150 
151  return (ea_words[0] == 0xFFFF && ea_words[1] == 0xFFFF &&
152  ea_words[2] == 0xFFFF);
153 }
154 
165 static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
166 {
167  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
168 }
169 
180 static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
181 {
182  return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
183 }
184 
196 static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
197 {
199 }
200 
207 void
208 rte_eth_random_addr(uint8_t *addr);
209 
218 static inline void rte_ether_addr_copy(const struct rte_ether_addr *ea_from,
219  struct rte_ether_addr *ea_to)
220 {
221 #ifdef __INTEL_COMPILER
222  uint16_t *from_words = (uint16_t *)(ea_from->addr_bytes);
223  uint16_t *to_words = (uint16_t *)(ea_to->addr_bytes);
224 
225  to_words[0] = from_words[0];
226  to_words[1] = from_words[1];
227  to_words[2] = from_words[2];
228 #else
229  /*
230  * Use the common way, because of a strange gcc warning.
231  */
232  *ea_to = *ea_from;
233 #endif
234 }
235 
236 #define RTE_ETHER_ADDR_FMT_SIZE 18
237 
247 void
248 rte_ether_format_addr(char *buf, uint16_t size,
249  const struct rte_ether_addr *eth_addr);
264 __rte_experimental
265 int
266 rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
267 
275  uint16_t ether_type;
276 } __attribute__((aligned(2)));
277 
283 struct rte_vlan_hdr {
284  uint16_t vlan_tci;
285  uint16_t eth_proto;
286 } __attribute__((__packed__));
287 
288 
289 
290 /* Ethernet frame types */
291 #define RTE_ETHER_TYPE_IPV4 0x0800
292 #define RTE_ETHER_TYPE_IPV6 0x86DD
293 #define RTE_ETHER_TYPE_ARP 0x0806
294 #define RTE_ETHER_TYPE_RARP 0x8035
295 #define RTE_ETHER_TYPE_VLAN 0x8100
296 #define RTE_ETHER_TYPE_QINQ 0x88A8
297 #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
298 #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
299 #define RTE_ETHER_TYPE_ETAG 0x893F
300 #define RTE_ETHER_TYPE_1588 0x88F7
301 
302 #define RTE_ETHER_TYPE_SLOW 0x8809
303 #define RTE_ETHER_TYPE_TEB 0x6558
304 #define RTE_ETHER_TYPE_LLDP 0x88CC
305 #define RTE_ETHER_TYPE_MPLS 0x8847
306 #define RTE_ETHER_TYPE_MPLSM 0x8848
319 static inline int rte_vlan_strip(struct rte_mbuf *m)
320 {
321  struct rte_ether_hdr *eh
322  = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
323  struct rte_vlan_hdr *vh;
324 
326  return -1;
327 
328  vh = (struct rte_vlan_hdr *)(eh + 1);
329  m->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
330  m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
331 
332  /* Copy ether header over rather than moving whole packet */
333  memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
334  eh, 2 * RTE_ETHER_ADDR_LEN);
335 
336  return 0;
337 }
338 
351 static inline int rte_vlan_insert(struct rte_mbuf **m)
352 {
353  struct rte_ether_hdr *oh, *nh;
354  struct rte_vlan_hdr *vh;
355 
356  /* Can't insert header if mbuf is shared */
357  if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
358  return -EINVAL;
359 
360  oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
361  nh = (struct rte_ether_hdr *)
362  rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
363  if (nh == NULL)
364  return -ENOSPC;
365 
366  memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
368 
369  vh = (struct rte_vlan_hdr *) (nh + 1);
370  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
371 
372  (*m)->ol_flags &= ~(PKT_RX_VLAN_STRIPPED | PKT_TX_VLAN);
373 
374  if ((*m)->ol_flags & PKT_TX_TUNNEL_MASK)
375  (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
376  else
377  (*m)->l2_len += sizeof(struct rte_vlan_hdr);
378 
379  return 0;
380 }
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif /* _RTE_ETHER_H_ */
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:64
#define PKT_RX_VLAN
Definition: rte_mbuf_core.h:47
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:295
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:26
void rte_eth_random_addr(uint8_t *addr)
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
uint16_t vlan_tci
Definition: rte_ether.h:284
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:273
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:442
#define rte_pktmbuf_mtod(m, t)
#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:1645
#define RTE_MBUF_DIRECT(mb)
void rte_ether_format_addr(char *buf, uint16_t size, const struct rte_ether_addr *eth_addr)
uint16_t ether_type
Definition: rte_ether.h:275
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:351
struct rte_ether_addr s_addr
Definition: rte_ether.h:274
static void rte_ether_addr_copy(const struct rte_ether_addr *ea_from, struct rte_ether_addr *ea_to)
Definition: rte_ether.h:218
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:180
#define PKT_RX_VLAN_STRIPPED
Definition: rte_mbuf_core.h:82
uint16_t eth_proto
Definition: rte_ether.h:285
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:165
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1581
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:196
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