DPDK  18.11.11
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  const unaligned_uint16_t *ea_words = (const unaligned_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 is_universal_ether_addr(const struct ether_addr *ea)
166 {
167  return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) == 0;
168 }
169 
180 static inline int is_local_admin_ether_addr(const struct ether_addr *ea)
181 {
182  return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) != 0;
183 }
184 
196 static inline int is_valid_assigned_ether_addr(const struct ether_addr *ea)
197 {
198  return is_unicast_ether_addr(ea) && (!is_zero_ether_addr(ea));
199 }
200 
207 static inline void eth_random_addr(uint8_t *addr)
208 {
209  uint64_t rand = rte_rand();
210  uint8_t *p = (uint8_t *)&rand;
211 
212  rte_memcpy(addr, p, ETHER_ADDR_LEN);
213  addr[0] &= (uint8_t)~ETHER_GROUP_ADDR; /* clear multicast bit */
214  addr[0] |= ETHER_LOCAL_ADMIN_ADDR; /* set local assignment bit */
215 }
216 
225 static inline void ether_addr_copy(const struct ether_addr *ea_from,
226  struct ether_addr *ea_to)
227 {
228 #ifdef __INTEL_COMPILER
229  uint16_t *from_words = (uint16_t *)(ea_from->addr_bytes);
230  uint16_t *to_words = (uint16_t *)(ea_to->addr_bytes);
231 
232  to_words[0] = from_words[0];
233  to_words[1] = from_words[1];
234  to_words[2] = from_words[2];
235 #else
236  /*
237  * Use the common way, because of a strange gcc warning.
238  */
239  *ea_to = *ea_from;
240 #endif
241 }
242 
243 #define ETHER_ADDR_FMT_SIZE 18
244 
254 static inline void
255 ether_format_addr(char *buf, uint16_t size,
256  const struct ether_addr *eth_addr)
257 {
258  snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X",
259  eth_addr->addr_bytes[0],
260  eth_addr->addr_bytes[1],
261  eth_addr->addr_bytes[2],
262  eth_addr->addr_bytes[3],
263  eth_addr->addr_bytes[4],
264  eth_addr->addr_bytes[5]);
265 }
266 
271 struct ether_hdr {
274  uint16_t ether_type;
275 } __attribute__((__packed__));
276 
282 struct vlan_hdr {
283  uint16_t vlan_tci;
284  uint16_t eth_proto;
285 } __attribute__((__packed__));
286 
292 struct vxlan_hdr {
293  uint32_t vx_flags;
294  uint32_t vx_vni;
295 } __attribute__((__packed__));
296 
297 /* Ethernet frame types */
298 #define ETHER_TYPE_IPv4 0x0800
299 #define ETHER_TYPE_IPv6 0x86DD
300 #define ETHER_TYPE_ARP 0x0806
301 #define ETHER_TYPE_RARP 0x8035
302 #define ETHER_TYPE_VLAN 0x8100
303 #define ETHER_TYPE_QINQ 0x88A8
304 #define ETHER_TYPE_ETAG 0x893F
305 #define ETHER_TYPE_1588 0x88F7
306 #define ETHER_TYPE_SLOW 0x8809
307 #define ETHER_TYPE_TEB 0x6558
308 #define ETHER_TYPE_LLDP 0x88CC
309 #define ETHER_TYPE_MPLS 0x8847
310 #define ETHER_TYPE_MPLSM 0x8848
312 #define ETHER_VXLAN_HLEN (sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr))
313 
321  uint8_t vx_flags;
322  uint8_t reserved[2];
323  uint8_t proto;
324  uint32_t vx_vni;
325 } __attribute__((__packed__));
326 
327 /* VXLAN-GPE next protocol types */
328 #define VXLAN_GPE_TYPE_IPV4 1
329 #define VXLAN_GPE_TYPE_IPV6 2
330 #define VXLAN_GPE_TYPE_ETH 3
331 #define VXLAN_GPE_TYPE_NSH 4
332 #define VXLAN_GPE_TYPE_MPLS 5
333 #define VXLAN_GPE_TYPE_GBP 6
334 #define VXLAN_GPE_TYPE_VBNG 7
336 #define ETHER_VXLAN_GPE_HLEN (sizeof(struct udp_hdr) + \
337  sizeof(struct vxlan_gpe_hdr))
338 
351 static inline int rte_vlan_strip(struct rte_mbuf *m)
352 {
353  struct ether_hdr *eh
354  = rte_pktmbuf_mtod(m, struct ether_hdr *);
355  struct vlan_hdr *vh;
356 
358  return -1;
359 
360  vh = (struct vlan_hdr *)(eh + 1);
363 
364  /* Copy ether header over rather than moving whole packet */
365  memmove(rte_pktmbuf_adj(m, sizeof(struct vlan_hdr)),
366  eh, 2 * ETHER_ADDR_LEN);
367 
368  return 0;
369 }
370 
383 static inline int rte_vlan_insert(struct rte_mbuf **m)
384 {
385  struct ether_hdr *oh, *nh;
386  struct vlan_hdr *vh;
387 
388  /* Can't insert header if mbuf is shared */
389  if (rte_mbuf_refcnt_read(*m) > 1) {
390  struct rte_mbuf *copy;
391 
392  copy = rte_pktmbuf_clone(*m, (*m)->pool);
393  if (unlikely(copy == NULL))
394  return -ENOMEM;
395  rte_pktmbuf_free(*m);
396  *m = copy;
397  }
398 
399  oh = rte_pktmbuf_mtod(*m, struct ether_hdr *);
400  nh = (struct ether_hdr *)
401  rte_pktmbuf_prepend(*m, sizeof(struct vlan_hdr));
402  if (nh == NULL)
403  return -ENOSPC;
404 
405  memmove(nh, oh, 2 * ETHER_ADDR_LEN);
407 
408  vh = (struct vlan_hdr *) (nh + 1);
409  vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
410 
411  (*m)->ol_flags &= ~(PKT_RX_VLAN_STRIPPED | PKT_TX_VLAN);
412 
413  if ((*m)->ol_flags & PKT_TX_TUNNEL_MASK)
414  (*m)->outer_l2_len += sizeof(struct vlan_hdr);
415  else
416  (*m)->l2_len += sizeof(struct vlan_hdr);
417 
418  return 0;
419 }
420 
421 #ifdef __cplusplus
422 }
423 #endif
424 
425 #endif /* _RTE_ETHER_H_ */
uint16_t eth_proto
Definition: rte_ether.h:284
static void rte_pktmbuf_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:1747
uint16_t vlan_tci
Definition: rte_ether.h:283
static struct rte_mbuf * rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp)
Definition: rte_mbuf.h:1778
#define PKT_TX_VLAN
Definition: rte_mbuf.h:346
static uint64_t rte_rand(void)
Definition: rte_random.h:48
static int is_local_admin_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:180
struct ether_addr s_addr
Definition: rte_ether.h:273
static void ether_format_addr(char *buf, uint16_t size, const struct ether_addr *eth_addr)
Definition: rte_ether.h:255
static int is_universal_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:165
static int is_multicast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:132
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:351
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
#define unlikely(x)
uint8_t addr_bytes[ETHER_ADDR_LEN]
Definition: rte_ether.h:58
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:957
#define PKT_RX_VLAN
Definition: rte_mbuf.h:71
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:2038
#define ETHER_ADDR_LEN
Definition: rte_ether.h:26
static int is_zero_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:98
#define ETHER_GROUP_ADDR
Definition: rte_ether.h:62
uint8_t proto
Definition: rte_ether.h:323
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:383
uint64_t ol_flags
Definition: rte_mbuf.h:519
#define rte_pktmbuf_mtod(m, t)
Definition: rte_mbuf.h:1909
uint32_t vx_flags
Definition: rte_ether.h:293
uint16_t ether_type
Definition: rte_ether.h:274
static int is_valid_assigned_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:196
#define ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:61
struct rte_mempool * pool
Definition: rte_mbuf.h:624
uint8_t reserved[2]
Definition: rte_ether.h:322
struct ether_addr d_addr
Definition: rte_ether.h:272
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1974
uint32_t vx_vni
Definition: rte_ether.h:294
#define ETHER_TYPE_VLAN
Definition: rte_ether.h:302
static int is_broadcast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:147
uint32_t vx_vni
Definition: rte_ether.h:324
static void * rte_memcpy(void *dst, const void *src, size_t n)
#define PKT_RX_VLAN_STRIPPED
Definition: rte_mbuf.h:102
uint8_t vx_flags
Definition: rte_ether.h:321
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
uint16_t vlan_tci
Definition: rte_mbuf.h:561
static void ether_addr_copy(const struct ether_addr *ea_from, struct ether_addr *ea_to)
Definition: rte_ether.h:225
static int is_unicast_ether_addr(const struct ether_addr *ea)
Definition: rte_ether.h:117
static void eth_random_addr(uint8_t *addr)
Definition: rte_ether.h:207