DPDK  18.11.6
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 ETHER_ADDR_LEN 6
27 #define ETHER_TYPE_LEN 2
28 #define ETHER_CRC_LEN 4
29 #define ETHER_HDR_LEN \
30  (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
31 #define ETHER_MIN_LEN 64
32 #define ETHER_MAX_LEN 1518
33 #define ETHER_MTU \
34  (ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
36 #define ETHER_MAX_VLAN_FRAME_LEN \
37  (ETHER_MAX_LEN + 4)
39 #define ETHER_MAX_JUMBO_FRAME_LEN \
40  0x3F00
42 #define ETHER_MAX_VLAN_ID 4095
44 #define ETHER_MIN_MTU 68
57 struct ether_addr {
58  uint8_t addr_bytes[ETHER_ADDR_LEN];
59 } __attribute__((__packed__));
60 
61 #define ETHER_LOCAL_ADMIN_ADDR 0x02
62 #define ETHER_GROUP_ADDR 0x01
78 static inline int is_same_ether_addr(const struct ether_addr *ea1,
79  const struct ether_addr *ea2)
80 {
81  int i;
82  for (i = 0; i < ETHER_ADDR_LEN; i++)
83  if (ea1->addr_bytes[i] != ea2->addr_bytes[i])
84  return 0;
85  return 1;
86 }
87 
98 static inline int is_zero_ether_addr(const struct ether_addr *ea)
99 {
100  int i;
101  for (i = 0; i < ETHER_ADDR_LEN; i++)
102  if (ea->addr_bytes[i] != 0x00)
103  return 0;
104  return 1;
105 }
106 
117 static inline int is_unicast_ether_addr(const struct ether_addr *ea)
118 {
119  return (ea->addr_bytes[0] & ETHER_GROUP_ADDR) == 0;
120 }
121 
132 static inline int is_multicast_ether_addr(const struct ether_addr *ea)
133 {
134  return ea->addr_bytes[0] & ETHER_GROUP_ADDR;
135 }
136 
147 static inline int is_broadcast_ether_addr(const struct ether_addr *ea)
148 {
149  return (ea->addr_bytes[0] == 0xFF && ea->addr_bytes[1] == 0xFF &&
150  ea->addr_bytes[2] == 0xFF && ea->addr_bytes[3] == 0xFF &&
151  ea->addr_bytes[4] == 0xFF && ea->addr_bytes[5] == 0xFF);
152 }
153 
164 static inline int is_universal_ether_addr(const struct ether_addr *ea)
165 {
166  return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) == 0;
167 }
168 
179 static inline int is_local_admin_ether_addr(const struct ether_addr *ea)
180 {
181  return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) != 0;
182 }
183 
195 static inline int is_valid_assigned_ether_addr(const struct ether_addr *ea)
196 {
197  return is_unicast_ether_addr(ea) && (!is_zero_ether_addr(ea));
198 }
199 
206 static inline void eth_random_addr(uint8_t *addr)
207 {
208  uint64_t rand = rte_rand();
209  uint8_t *p = (uint8_t *)&rand;
210 
211  rte_memcpy(addr, p, ETHER_ADDR_LEN);
212  addr[0] &= (uint8_t)~ETHER_GROUP_ADDR; /* clear multicast bit */
213  addr[0] |= ETHER_LOCAL_ADMIN_ADDR; /* set local assignment bit */
214 }
215 
224 static inline void ether_addr_copy(const struct ether_addr *ea_from,
225  struct ether_addr *ea_to)
226 {
227 #ifdef __INTEL_COMPILER
228  uint16_t *from_words = (uint16_t *)(ea_from->addr_bytes);
229  uint16_t *to_words = (uint16_t *)(ea_to->addr_bytes);
230 
231  to_words[0] = from_words[0];
232  to_words[1] = from_words[1];
233  to_words[2] = from_words[2];
234 #else
235  /*
236  * Use the common way, because of a strange gcc warning.
237  */
238  *ea_to = *ea_from;
239 #endif
240 }
241 
242 #define ETHER_ADDR_FMT_SIZE 18
243 
253 static inline void
254 ether_format_addr(char *buf, uint16_t size,
255  const struct ether_addr *eth_addr)
256 {
257  snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X",
258  eth_addr->addr_bytes[0],
259  eth_addr->addr_bytes[1],
260  eth_addr->addr_bytes[2],
261  eth_addr->addr_bytes[3],
262  eth_addr->addr_bytes[4],
263  eth_addr->addr_bytes[5]);
264 }
265 
270 struct ether_hdr {
273  uint16_t ether_type;
274 } __attribute__((__packed__));
275 
281 struct vlan_hdr {
282  uint16_t vlan_tci;
283  uint16_t eth_proto;
284 } __attribute__((__packed__));
285 
291 struct vxlan_hdr {
292  uint32_t vx_flags;
293  uint32_t vx_vni;
294 } __attribute__((__packed__));
295 
296 /* Ethernet frame types */
297 #define ETHER_TYPE_IPv4 0x0800
298 #define ETHER_TYPE_IPv6 0x86DD
299 #define ETHER_TYPE_ARP 0x0806
300 #define ETHER_TYPE_RARP 0x8035
301 #define ETHER_TYPE_VLAN 0x8100
302 #define ETHER_TYPE_QINQ 0x88A8
303 #define ETHER_TYPE_ETAG 0x893F
304 #define ETHER_TYPE_1588 0x88F7
305 #define ETHER_TYPE_SLOW 0x8809
306 #define ETHER_TYPE_TEB 0x6558
307 #define ETHER_TYPE_LLDP 0x88CC
308 #define ETHER_TYPE_MPLS 0x8847
309 #define ETHER_TYPE_MPLSM 0x8848
311 #define ETHER_VXLAN_HLEN (sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr))
312 
320  uint8_t vx_flags;
321  uint8_t reserved[2];
322  uint8_t proto;
323  uint32_t vx_vni;
324 } __attribute__((__packed__));
325 
326 /* VXLAN-GPE next protocol types */
327 #define VXLAN_GPE_TYPE_IPV4 1
328 #define VXLAN_GPE_TYPE_IPV6 2
329 #define VXLAN_GPE_TYPE_ETH 3
330 #define VXLAN_GPE_TYPE_NSH 4
331 #define VXLAN_GPE_TYPE_MPLS 5
332 #define VXLAN_GPE_TYPE_GBP 6
333 #define VXLAN_GPE_TYPE_VBNG 7
335 #define ETHER_VXLAN_GPE_HLEN (sizeof(struct udp_hdr) + \
336  sizeof(struct vxlan_gpe_hdr))
337 
350 static inline int rte_vlan_strip(struct rte_mbuf *m)
351 {
352  struct ether_hdr *eh
353  = rte_pktmbuf_mtod(m, struct ether_hdr *);
354  struct vlan_hdr *vh;
355 
357  return -1;
358 
359  vh = (struct vlan_hdr *)(eh + 1);
362 
363  /* Copy ether header over rather than moving whole packet */
364  memmove(rte_pktmbuf_adj(m, sizeof(struct vlan_hdr)),
365  eh, 2 * ETHER_ADDR_LEN);
366 
367  return 0;
368 }
369 
382 static inline int rte_vlan_insert(struct rte_mbuf **m)
383 {
384  struct ether_hdr *oh, *nh;
385  struct vlan_hdr *vh;
386 
387  /* Can't insert header if mbuf is shared */
388  if (rte_mbuf_refcnt_read(*m) > 1) {
389  struct rte_mbuf *copy;
390 
391  copy = rte_pktmbuf_clone(*m, (*m)->pool);
392  if (unlikely(copy == NULL))
393  return -ENOMEM;
394  rte_pktmbuf_free(*m);
395  *m = copy;
396  }
397 
398  oh = rte_pktmbuf_mtod(*m, struct ether_hdr *);
399  nh = (struct ether_hdr *)
400  rte_pktmbuf_prepend(*m, sizeof(struct vlan_hdr));
401  if (nh == NULL)
402  return -ENOSPC;
403 
404  memmove(nh, oh, 2 * ETHER_ADDR_LEN);
406 
407  vh = (struct vlan_hdr *) (nh + 1);
408  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
409 
410  (*m)->ol_flags &= ~(PKT_RX_VLAN_STRIPPED | PKT_TX_VLAN);
411 
412  if ((*m)->ol_flags & PKT_TX_TUNNEL_MASK)
413  (*m)->outer_l2_len += sizeof(struct vlan_hdr);
414  else
415  (*m)->l2_len += sizeof(struct vlan_hdr);
416 
417  return 0;
418 }
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 
424 #endif /* _RTE_ETHER_H_ */
vxlan_hdr::vx_vni
uint32_t vx_vni
Definition: rte_ether.h:293
rte_cpu_to_be_16
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
unlikely
#define unlikely(x)
Definition: rte_branch_prediction.h:38
PKT_TX_VLAN
#define PKT_TX_VLAN
Definition: rte_mbuf.h:346
vlan_hdr::eth_proto
uint16_t eth_proto
Definition: rte_ether.h:283
ether_hdr::s_addr
struct ether_addr s_addr
Definition: rte_ether.h:272
is_broadcast_ether_addr
static int is_broadcast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:147
vlan_hdr::vlan_tci
uint16_t vlan_tci
Definition: rte_ether.h:282
rte_pktmbuf_clone
static struct rte_mbuf * rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp)
Definition: rte_mbuf.h:1778
eth_random_addr
static void eth_random_addr(uint8_t *addr)
Definition: rte_ether.h:206
is_zero_ether_addr
static int is_zero_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:98
vxlan_gpe_hdr::reserved
uint8_t reserved[2]
Definition: rte_ether.h:321
PKT_RX_VLAN
#define PKT_RX_VLAN
Definition: rte_mbuf.h:71
rte_vlan_insert
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:382
rte_memcpy
static void * rte_memcpy(void *dst, const void *src, size_t n)
rte_mbuf::pool
struct rte_mempool * pool
Definition: rte_mbuf.h:624
rte_mbuf
Definition: rte_mbuf.h:478
ether_addr
Definition: rte_ether.h:57
vxlan_hdr::vx_flags
uint32_t vx_flags
Definition: rte_ether.h:292
ETHER_TYPE_VLAN
#define ETHER_TYPE_VLAN
Definition: rte_ether.h:301
rte_be_to_cpu_16
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
rte_pktmbuf_mtod
#define rte_pktmbuf_mtod(m, t)
Definition: rte_mbuf.h:1909
ether_addr_copy
static void ether_addr_copy(const struct ether_addr *ea_from, struct ether_addr *ea_to)
Definition: rte_ether.h:224
is_valid_assigned_ether_addr
static int is_valid_assigned_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:195
rte_pktmbuf_adj
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:2038
rte_random.h
vlan_hdr
Definition: rte_ether.h:281
ETHER_LOCAL_ADMIN_ADDR
#define ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:61
ether_hdr::ether_type
uint16_t ether_type
Definition: rte_ether.h:273
ether_addr::addr_bytes
uint8_t addr_bytes[ETHER_ADDR_LEN]
Definition: rte_ether.h:58
rte_pktmbuf_prepend
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1974
vxlan_gpe_hdr
Definition: rte_ether.h:319
rte_memcpy.h
vxlan_gpe_hdr::vx_flags
uint8_t vx_flags
Definition: rte_ether.h:320
rte_mbuf::ol_flags
uint64_t ol_flags
Definition: rte_mbuf.h:519
rte_pktmbuf_free
static void rte_pktmbuf_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:1747
is_local_admin_ether_addr
static int is_local_admin_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:179
vxlan_hdr
Definition: rte_ether.h:291
is_unicast_ether_addr
static int is_unicast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:117
rte_vlan_strip
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:350
rte_mbuf.h
ETHER_ADDR_LEN
#define ETHER_ADDR_LEN
Definition: rte_ether.h:26
ETHER_GROUP_ADDR
#define ETHER_GROUP_ADDR
Definition: rte_ether.h:62
ether_format_addr
static void ether_format_addr(char *buf, uint16_t size, const struct ether_addr *eth_addr)
Definition: rte_ether.h:254
ether_hdr::d_addr
struct ether_addr d_addr
Definition: rte_ether.h:271
rte_mbuf::vlan_tci
uint16_t vlan_tci
Definition: rte_mbuf.h:561
is_universal_ether_addr
static int is_universal_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:164
ether_hdr
Definition: rte_ether.h:270
rte_mbuf_refcnt_read
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:957
rte_rand
static uint64_t rte_rand(void)
Definition: rte_random.h:48
vxlan_gpe_hdr::vx_vni
uint32_t vx_vni
Definition: rte_ether.h:323
rte_byteorder.h
vxlan_gpe_hdr::proto
uint8_t proto
Definition: rte_ether.h:322
is_multicast_ether_addr
static int is_multicast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:132
PKT_RX_VLAN_STRIPPED
#define PKT_RX_VLAN_STRIPPED
Definition: rte_mbuf.h:102