DPDK 22.11.4
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
62} __rte_aligned(2);
63
64#define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02
65#define RTE_ETHER_GROUP_ADDR 0x01
81static 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
100static 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
117static 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
132static 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
147static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea)
148{
149 const uint16_t *w = (const uint16_t *)ea;
150
151 return (w[0] & w[1] & w[2]) == 0xFFFF;
152}
153
164static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
165{
166 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0;
167}
168
179static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
180{
181 return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0;
182}
183
195static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
196{
198}
199
206void
207rte_eth_random_addr(uint8_t *addr);
208
217static inline void
218rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from,
219 struct rte_ether_addr *__restrict ea_to)
220{
221 *ea_to = *ea_from;
222}
223
227#define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
231#define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \
232 ((mac_addrs)->addr_bytes[1]), \
233 ((mac_addrs)->addr_bytes[2]), \
234 ((mac_addrs)->addr_bytes[3]), \
235 ((mac_addrs)->addr_bytes[4]), \
236 ((mac_addrs)->addr_bytes[5])
237
238#define RTE_ETHER_ADDR_FMT_SIZE 18
249void
250rte_ether_format_addr(char *buf, uint16_t size,
251 const struct rte_ether_addr *eth_addr);
266int
267rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);
268
277} __rte_aligned(2);
278
288
289
290
291/* Ethernet frame types */
292#define RTE_ETHER_TYPE_IPV4 0x0800
293#define RTE_ETHER_TYPE_IPV6 0x86DD
294#define RTE_ETHER_TYPE_ARP 0x0806
295#define RTE_ETHER_TYPE_RARP 0x8035
296#define RTE_ETHER_TYPE_VLAN 0x8100
297#define RTE_ETHER_TYPE_QINQ 0x88A8
298#define RTE_ETHER_TYPE_QINQ1 0x9100
299#define RTE_ETHER_TYPE_QINQ2 0x9200
300#define RTE_ETHER_TYPE_QINQ3 0x9300
301#define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863
302#define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864
303#define RTE_ETHER_TYPE_ETAG 0x893F
304#define RTE_ETHER_TYPE_1588 0x88F7
306#define RTE_ETHER_TYPE_SLOW 0x8809
307#define RTE_ETHER_TYPE_TEB 0x6558
308#define RTE_ETHER_TYPE_LLDP 0x88CC
309#define RTE_ETHER_TYPE_MPLS 0x8847
310#define RTE_ETHER_TYPE_MPLSM 0x8848
311#define RTE_ETHER_TYPE_ECPRI 0xAEFE
324static inline int rte_vlan_strip(struct rte_mbuf *m)
325{
326 struct rte_ether_hdr *eh
327 = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
328 struct rte_vlan_hdr *vh;
329
331 return -1;
332
333 vh = (struct rte_vlan_hdr *)(eh + 1);
336
337 /* Copy ether header over rather than moving whole packet */
338 memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
339 eh, 2 * RTE_ETHER_ADDR_LEN);
340
341 return 0;
342}
343
356static inline int rte_vlan_insert(struct rte_mbuf **m)
357{
358 struct rte_ether_hdr *oh, *nh;
359 struct rte_vlan_hdr *vh;
360
361 /* Can't insert header if mbuf is shared */
362 if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
363 return -EINVAL;
364
365 /* Can't insert header if the first segment is too short */
367 return -EINVAL;
368
369 oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
370 nh = (struct rte_ether_hdr *)(void *)
371 rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
372 if (nh == NULL)
373 return -ENOSPC;
374
375 memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
377
378 vh = (struct rte_vlan_hdr *) (nh + 1);
379 vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
380
381 (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN);
382
383 if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
384 (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr);
385 else
386 (*m)->l2_len += sizeof(struct rte_vlan_hdr);
387
388 return 0;
389}
390
391#ifdef __cplusplus
392}
393#endif
394
395#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
#define RTE_ETHER_TYPE_VLAN
Definition: rte_ether.h:296
#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:81
static int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:117
#define RTE_ETHER_LOCAL_ADMIN_ADDR
Definition: rte_ether.h:64
static int rte_is_universal_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:164
static int rte_is_zero_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:100
static int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:195
int rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr)
#define RTE_ETHER_GROUP_ADDR
Definition: rte_ether.h:65
static int rte_vlan_strip(struct rte_mbuf *m)
Definition: rte_ether.h:324
static int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:179
static int rte_vlan_insert(struct rte_mbuf **m)
Definition: rte_ether.h:356
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:218
static int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea)
Definition: rte_ether.h:132
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:147
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1566
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1583
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1647
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:61
rte_be16_t ether_type
Definition: rte_ether.h:276
struct rte_ether_addr src_addr
Definition: rte_ether.h:275
struct rte_ether_addr dst_addr
Definition: rte_ether.h:274
uint64_t ol_flags
uint16_t vlan_tci
rte_be16_t eth_proto
Definition: rte_ether.h:286
rte_be16_t vlan_tci
Definition: rte_ether.h:285