DPDK 22.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
15extern "C" {
16#endif
17
18#include <stdint.h>
19#include <stdio.h>
20
21#include <rte_random.h>
22#include <rte_mbuf.h>
23#include <rte_byteorder.h>
24
25#define RTE_ETHER_ADDR_LEN 6
26#define RTE_ETHER_TYPE_LEN 2
27#define RTE_ETHER_CRC_LEN 4
28#define RTE_ETHER_HDR_LEN \
29 (RTE_ETHER_ADDR_LEN * 2 + \
30 RTE_ETHER_TYPE_LEN)
31#define RTE_ETHER_MIN_LEN 64
32#define RTE_ETHER_MAX_LEN 1518
33#define RTE_ETHER_MTU \
34 (RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \
35 RTE_ETHER_CRC_LEN)
37#define RTE_VLAN_HLEN 4
39#define RTE_ETHER_MAX_VLAN_FRAME_LEN \
40 (RTE_ETHER_MAX_LEN + RTE_VLAN_HLEN)
41
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
49/* VLAN header fields */
50#define RTE_VLAN_DEI_SHIFT 12
51#define RTE_VLAN_PRI_SHIFT 13
52#define RTE_VLAN_PRI_MASK 0xe000 /* Priority Code Point */
53#define RTE_VLAN_DEI_MASK 0x1000 /* Drop Eligible Indicator */
54#define RTE_VLAN_ID_MASK 0x0fff /* VLAN Identifier */
55
56#define RTE_VLAN_TCI_ID(vlan_tci) ((vlan_tci) & RTE_VLAN_ID_MASK)
57#define RTE_VLAN_TCI_PRI(vlan_tci) (((vlan_tci) & RTE_VLAN_PRI_MASK) >> RTE_VLAN_PRI_SHIFT)
58#define RTE_VLAN_TCI_DEI(vlan_tci) (((vlan_tci) & RTE_VLAN_DEI_MASK) >> RTE_VLAN_DEI_SHIFT)
59#define RTE_VLAN_TCI_MAKE(id, pri, dei) ((id) | \
60 ((pri) << RTE_VLAN_PRI_SHIFT) | \
61 ((dei) << RTE_VLAN_DEI_SHIFT))
62
76} __rte_aligned(2);
77
78#define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
79#define RTE_ETHER_GROUP_ADDR 0x01
95static inline int rte_is_same_ether_addr(const struct rte_ether_addr *ea1,
96 const struct rte_ether_addr *ea2)
97{
98 const uint16_t *w1 = (const uint16_t *)ea1;
99 const uint16_t *w2 = (const uint16_t *)ea2;
100
101 return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0;
102}
103
114static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
115{
116 const uint16_t *w = (const uint16_t *)ea;
117
118 return (w[0] | w[1] | w[2]) == 0;
119}
120
131static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
132{
133 return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0;
134}
135
146static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
147{
148 return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR;
149}
150
161static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
162{
163 const uint16_t *w = (const uint16_t *)ea;
164
165 return (w[0] & w[1] & w[2]) == 0xFFFF;
166}
167
178static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
179{
180 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
181}
182
193static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
194{
195 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
196}
197
209static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
210{
212}
213
220void
221rte_eth_random_addr(uint8_t *addr);
222
231static inline void
232rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
233 struct rte_ether_addr *__restrict ea_to)
234{
235 *ea_to = *ea_from;
236}
237
241#define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
245#define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
246 ((mac_addrs)->addr_bytes[1]), \
247 ((mac_addrs)->addr_bytes[2]), \
248 ((mac_addrs)->addr_bytes[3]), \
249 ((mac_addrs)->addr_bytes[4]), \
250 ((mac_addrs)->addr_bytes[5])
251
252#define RTE_ETHER_ADDR_FMT_SIZE 18
263void
264rte_ether_format_addr(char *buf, uint16_t size,
265 const struct rte_ether_addr *eth_addr);
280int
281rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
282
291} __rte_aligned(2);
292
302
303
304
305/* Ethernet frame types */
306#define RTE_ETHER_TYPE_IPV4 0x0800
307#define RTE_ETHER_TYPE_IPV6 0x86DD
308#define RTE_ETHER_TYPE_ARP 0x0806
309#define RTE_ETHER_TYPE_RARP 0x8035
310#define RTE_ETHER_TYPE_VLAN 0x8100
311#define RTE_ETHER_TYPE_QINQ 0x88A8
312#define RTE_ETHER_TYPE_QINQ1 0x9100
313#define RTE_ETHER_TYPE_QINQ2 0x9200
314#define RTE_ETHER_TYPE_QINQ3 0x9300
315#define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
316#define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
317#define RTE_ETHER_TYPE_ETAG 0x893F
318#define RTE_ETHER_TYPE_1588 0x88F7
320#define RTE_ETHER_TYPE_SLOW 0x8809
321#define RTE_ETHER_TYPE_TEB 0x6558
322#define RTE_ETHER_TYPE_LLDP 0x88CC
323#define RTE_ETHER_TYPE_MPLS 0x8847
324#define RTE_ETHER_TYPE_MPLSM 0x8848
325#define RTE_ETHER_TYPE_ECPRI 0xAEFE
338static inline int rte_vlan_strip(struct rte_mbuf *m)
339{
340 struct rte_ether_hdr *eh
341 = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
342 struct rte_vlan_hdr *vh;
343
345 return -1;
346
347 vh = (struct rte_vlan_hdr *)(eh + 1);
350
351 /* Copy ether header over rather than moving whole packet */
352 memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
353 eh, 2 * RTE_ETHER_ADDR_LEN);
354
355 return 0;
356}
357
370static inline int rte_vlan_insert(struct rte_mbuf **m)
371{
372 struct rte_ether_hdr *oh, *nh;
373 struct rte_vlan_hdr *vh;
374
375 /* Can't insert header if mbuf is shared */
376 if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
377 return -EINVAL;
378
379 /* Can't insert header if the first segment is too short */
381 return -EINVAL;
382
383 oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
384 nh = (struct rte_ether_hdr *)(void *)
385 rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
386 if (nh == NULL)
387 return -ENOSPC;
388
389 memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
391
392 vh = (struct rte_vlan_hdr *) (nh + 1);
393 vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
394
395 (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
396
397 if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
398 (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
399 else
400 (*m)->l2_len += sizeof(struct rte_vlan_hdr);
401
402 return 0;
403}
404
405#ifdef __cplusplus
406}
407#endif
408
409#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_packed
Definition: rte_common.h:83
__extension__ struct rte_eth_link __rte_aligned(8)
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:310
#define RTE_ETHER_ADDR_LEN
Definition: rte_ether.h:25
static int rte_is_same_ether_addr(const struct rte_ether_addr *ea1, const struct rte_ether_addr *ea2)
Definition: rte_ether.h:95
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:131
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:78
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:178
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:114
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:209
int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:79
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:338
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:193
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:370
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:232
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:146
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:161
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1569
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1586
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1650
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:69
#define RTE_MBUF_F_RX_VLAN
Definition: rte_mbuf_core.h:49
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]
Definition: rte_ether.h:75
rte_be16_t ether_type
Definition: rte_ether.h:290
struct rte_ether_addr src_addr
Definition: rte_ether.h:289
struct rte_ether_addr dst_addr
Definition: rte_ether.h:288
uint64_t ol_flags
uint16_t vlan_tci
rte_be16_t eth_proto
Definition: rte_ether.h:300
rte_be16_t vlan_tci
Definition: rte_ether.h:299