DPDK 25.03.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#include <assert.h>
15#include <stdalign.h>
16#include <stdint.h>
17#include <stdio.h>
18
19#include <rte_random.h>
20#include <rte_mbuf.h>
21#include <rte_byteorder.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define RTE_ETHER_ADDR_LEN 6
28#define RTE_ETHER_TYPE_LEN 2
29#define RTE_ETHER_CRC_LEN 4
30#define RTE_ETHER_HDR_LEN \
31 (RTE_ETHER_ADDR_LEN * 2 + \
32 RTE_ETHER_TYPE_LEN)
33#define RTE_ETHER_MIN_LEN 64
34#define RTE_ETHER_MAX_LEN 1518
35#define RTE_ETHER_MTU \
36 (RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \
37 RTE_ETHER_CRC_LEN)
39#define RTE_VLAN_HLEN 4
41#define RTE_ETHER_MAX_VLAN_FRAME_LEN \
42 (RTE_ETHER_MAX_LEN + RTE_VLAN_HLEN)
43
44#define RTE_ETHER_MAX_JUMBO_FRAME_LEN \
45 0x3F00
47#define RTE_ETHER_MAX_VLAN_ID 4095
49#define RTE_ETHER_MIN_MTU 68
51/* VLAN header fields */
52#define RTE_VLAN_DEI_SHIFT 12
53#define RTE_VLAN_PRI_SHIFT 13
54#define RTE_VLAN_PRI_MASK 0xe000 /* Priority Code Point */
55#define RTE_VLAN_DEI_MASK 0x1000 /* Drop Eligible Indicator */
56#define RTE_VLAN_ID_MASK 0x0fff /* VLAN Identifier */
57
58#define RTE_VLAN_TCI_ID(vlan_tci) ((vlan_tci) & RTE_VLAN_ID_MASK)
59#define RTE_VLAN_TCI_PRI(vlan_tci) (((vlan_tci) & RTE_VLAN_PRI_MASK) >> RTE_VLAN_PRI_SHIFT)
60#define RTE_VLAN_TCI_DEI(vlan_tci) (((vlan_tci) & RTE_VLAN_DEI_MASK) >> RTE_VLAN_DEI_SHIFT)
61#define RTE_VLAN_TCI_MAKE(id, pri, dei) ((id) | \
62 ((pri) << RTE_VLAN_PRI_SHIFT) | \
63 ((dei) << RTE_VLAN_DEI_SHIFT))
64
76struct __rte_aligned(2) rte_ether_addr {
77 uint8_t addr_bytes[RTE_ETHER_ADDR_LEN];
78};
79
80static_assert(sizeof(struct rte_ether_addr) == 6,
81 "sizeof(struct rte_ether_addr) == 6");
82static_assert(alignof(struct rte_ether_addr) == 2,
83 "alignof(struct rte_ether_addr) == 2");
84
85#define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
86#define RTE_ETHER_GROUP_ADDR 0x01
102static inline int rte_is_same_ether_addr(const struct rte_ether_addr *ea1,
103 const struct rte_ether_addr *ea2)
104{
105 const uint16_t *w1 = (const uint16_t *)ea1;
106 const uint16_t *w2 = (const uint16_t *)ea2;
107
108 return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0;
109}
110
121static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
122{
123 const uint16_t *w = (const uint16_t *)ea;
124
125 return (w[0] | w[1] | w[2]) == 0;
126}
127
138static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
139{
140 return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0;
141}
142
153static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
154{
155 return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR;
156}
157
168static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
169{
170 const uint16_t *w = (const uint16_t *)ea;
171
172 return (w[0] & w[1] & w[2]) == 0xFFFF;
173}
174
185static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
186{
187 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
188}
189
200static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
201{
202 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
203}
204
216static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
217{
219}
220
227void
228rte_eth_random_addr(uint8_t *addr);
229
238static inline void
239rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
240 struct rte_ether_addr *__restrict ea_to)
241{
242 *ea_to = *ea_from;
243}
244
248#define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
252#define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
253 ((mac_addrs)->addr_bytes[1]), \
254 ((mac_addrs)->addr_bytes[2]), \
255 ((mac_addrs)->addr_bytes[3]), \
256 ((mac_addrs)->addr_bytes[4]), \
257 ((mac_addrs)->addr_bytes[5])
258
259#define RTE_ETHER_ADDR_FMT_SIZE 18
270void
271rte_ether_format_addr(char *buf, uint16_t size,
272 const struct rte_ether_addr *eth_addr);
293int
294rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
295
301 struct rte_ether_addr dst_addr;
302 struct rte_ether_addr src_addr;
304};
305
306static_assert(sizeof(struct rte_ether_hdr) == 14,
307 "sizeof(struct rte_ether_hdr) == 14");
308static_assert(alignof(struct rte_ether_hdr) == 2,
309 "alignof(struct rte_ether_hdr) == 2");
310
319};
320
321static_assert(sizeof(struct rte_vlan_hdr) == 4,
322 "sizeof(struct rte_vlan_hdr) == 4");
323static_assert(alignof(struct rte_vlan_hdr) == 2,
324 "alignof(struct rte_vlan_hdr) == 2");
325
326
327
328/* Ethernet frame types */
329#define RTE_ETHER_TYPE_IPV4 0x0800
330#define RTE_ETHER_TYPE_IPV6 0x86DD
331#define RTE_ETHER_TYPE_ARP 0x0806
332#define RTE_ETHER_TYPE_RARP 0x8035
333#define RTE_ETHER_TYPE_VLAN 0x8100
334#define RTE_ETHER_TYPE_QINQ 0x88A8
335#define RTE_ETHER_TYPE_QINQ1 0x9100
336#define RTE_ETHER_TYPE_QINQ2 0x9200
337#define RTE_ETHER_TYPE_QINQ3 0x9300
338#define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
339#define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
340#define RTE_ETHER_TYPE_ETAG 0x893F
341#define RTE_ETHER_TYPE_1588 0x88F7
343#define RTE_ETHER_TYPE_SLOW 0x8809
344#define RTE_ETHER_TYPE_TEB 0x6558
345#define RTE_ETHER_TYPE_LLDP 0x88CC
346#define RTE_ETHER_TYPE_MPLS 0x8847
347#define RTE_ETHER_TYPE_MPLSM 0x8848
348#define RTE_ETHER_TYPE_ECPRI 0xAEFE
361static inline int rte_vlan_strip(struct rte_mbuf *m)
362{
363 struct rte_ether_hdr *eh
364 = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
365 struct rte_vlan_hdr *vh;
366
368 return -1;
369
370 vh = (struct rte_vlan_hdr *)(eh + 1);
373
374 /* Copy ether header over rather than moving whole packet */
375 memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
376 eh, 2 * RTE_ETHER_ADDR_LEN);
377
378 return 0;
379}
380
393static inline int rte_vlan_insert(struct rte_mbuf **m)
394{
395 struct rte_ether_hdr *oh, *nh;
396 struct rte_vlan_hdr *vh;
397
398 /* Can't insert header if mbuf is shared */
399 if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
400 return -EINVAL;
401
402 /* Can't insert header if the first segment is too short */
404 return -EINVAL;
405
406 oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
407 nh = (struct rte_ether_hdr *)(void *)
408 rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
409 if (nh == NULL)
410 return -ENOSPC;
411
412 memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
414
415 vh = (struct rte_vlan_hdr *) (nh + 1);
416 vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
417
418 (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
419
420 if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
421 (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
422 else
423 (*m)->l2_len += sizeof(struct rte_vlan_hdr);
424
425 return 0;
426}
427
428#ifdef __cplusplus
429}
430#endif
431
432#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_ETHER_TYPE_VLAN
Definition: rte_ether.h:333
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:27
static int rte_is_same_ether_addr(const struct rte_ether_addr *ea1, const struct rte_ether_addr *ea2)
Definition: rte_ether.h:102
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:138
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:85
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:185
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:121
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:216
struct __rte_aligned(2) rte_ether_addr
Definition: rte_ether.h:76
int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:86
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:361
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:200
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:393
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:239
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:153
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:168
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1571
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1588
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1652
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:439
#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:71
#define RTE_MBUF_F_RX_VLAN
Definition: rte_mbuf_core.h:51
rte_be16_t ether_type
Definition: rte_ether.h:303
struct rte_ether_addr src_addr
Definition: rte_ether.h:302
struct rte_ether_addr dst_addr
Definition: rte_ether.h:301
uint64_t ol_flags
uint16_t vlan_tci
rte_be16_t eth_proto
Definition: rte_ether.h:318
rte_be16_t vlan_tci
Definition: rte_ether.h:317