199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef _RTE_ETHER_H_ 699a2dd95SBruce Richardson #define _RTE_ETHER_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson /** 999a2dd95SBruce Richardson * @file 1099a2dd95SBruce Richardson * 1199a2dd95SBruce Richardson * Ethernet Helpers in RTE 1299a2dd95SBruce Richardson */ 1399a2dd95SBruce Richardson 14*e214d58eSMorten Brørup #include <assert.h> 15*e214d58eSMorten Brørup #include <stdalign.h> 1699a2dd95SBruce Richardson #include <stdint.h> 1799a2dd95SBruce Richardson #include <stdio.h> 1899a2dd95SBruce Richardson 1999a2dd95SBruce Richardson #include <rte_random.h> 2099a2dd95SBruce Richardson #include <rte_mbuf.h> 2199a2dd95SBruce Richardson #include <rte_byteorder.h> 2299a2dd95SBruce Richardson 23719834a6SMattias Rönnblom #ifdef __cplusplus 24719834a6SMattias Rönnblom extern "C" { 25719834a6SMattias Rönnblom #endif 26719834a6SMattias Rönnblom 2799a2dd95SBruce Richardson #define RTE_ETHER_ADDR_LEN 6 /**< Length of Ethernet address. */ 2899a2dd95SBruce Richardson #define RTE_ETHER_TYPE_LEN 2 /**< Length of Ethernet type field. */ 2999a2dd95SBruce Richardson #define RTE_ETHER_CRC_LEN 4 /**< Length of Ethernet CRC. */ 3099a2dd95SBruce Richardson #define RTE_ETHER_HDR_LEN \ 3199a2dd95SBruce Richardson (RTE_ETHER_ADDR_LEN * 2 + \ 3299a2dd95SBruce Richardson RTE_ETHER_TYPE_LEN) /**< Length of Ethernet header. */ 3399a2dd95SBruce Richardson #define RTE_ETHER_MIN_LEN 64 /**< Minimum frame len, including CRC. */ 3499a2dd95SBruce Richardson #define RTE_ETHER_MAX_LEN 1518 /**< Maximum frame len, including CRC. */ 3599a2dd95SBruce Richardson #define RTE_ETHER_MTU \ 3699a2dd95SBruce Richardson (RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN - \ 3799a2dd95SBruce Richardson RTE_ETHER_CRC_LEN) /**< Ethernet MTU. */ 3899a2dd95SBruce Richardson 3925cf2630SFerruh Yigit #define RTE_VLAN_HLEN 4 /**< VLAN (IEEE 802.1Q) header length. */ 4025cf2630SFerruh Yigit /** Maximum VLAN frame length (excluding QinQ), including CRC. */ 4199a2dd95SBruce Richardson #define RTE_ETHER_MAX_VLAN_FRAME_LEN \ 4225cf2630SFerruh Yigit (RTE_ETHER_MAX_LEN + RTE_VLAN_HLEN) 4399a2dd95SBruce Richardson 4499a2dd95SBruce Richardson #define RTE_ETHER_MAX_JUMBO_FRAME_LEN \ 4599a2dd95SBruce Richardson 0x3F00 /**< Maximum Jumbo frame length, including CRC. */ 4699a2dd95SBruce Richardson 4799a2dd95SBruce Richardson #define RTE_ETHER_MAX_VLAN_ID 4095 /**< Maximum VLAN ID. */ 4899a2dd95SBruce Richardson 4999a2dd95SBruce Richardson #define RTE_ETHER_MIN_MTU 68 /**< Minimum MTU for IPv4 packets, see RFC 791. */ 5099a2dd95SBruce Richardson 51b74087f1SAlan Elder /* VLAN header fields */ 52b74087f1SAlan Elder #define RTE_VLAN_DEI_SHIFT 12 53b74087f1SAlan Elder #define RTE_VLAN_PRI_SHIFT 13 54b74087f1SAlan Elder #define RTE_VLAN_PRI_MASK 0xe000 /* Priority Code Point */ 55b74087f1SAlan Elder #define RTE_VLAN_DEI_MASK 0x1000 /* Drop Eligible Indicator */ 56b74087f1SAlan Elder #define RTE_VLAN_ID_MASK 0x0fff /* VLAN Identifier */ 57b74087f1SAlan Elder 58b74087f1SAlan Elder #define RTE_VLAN_TCI_ID(vlan_tci) ((vlan_tci) & RTE_VLAN_ID_MASK) 59b74087f1SAlan Elder #define RTE_VLAN_TCI_PRI(vlan_tci) (((vlan_tci) & RTE_VLAN_PRI_MASK) >> RTE_VLAN_PRI_SHIFT) 60b74087f1SAlan Elder #define RTE_VLAN_TCI_DEI(vlan_tci) (((vlan_tci) & RTE_VLAN_DEI_MASK) >> RTE_VLAN_DEI_SHIFT) 61b74087f1SAlan Elder #define RTE_VLAN_TCI_MAKE(id, pri, dei) ((id) | \ 62b74087f1SAlan Elder ((pri) << RTE_VLAN_PRI_SHIFT) | \ 63b74087f1SAlan Elder ((dei) << RTE_VLAN_DEI_SHIFT)) 64b74087f1SAlan Elder 6599a2dd95SBruce Richardson /** 6699a2dd95SBruce Richardson * Ethernet address: 6799a2dd95SBruce Richardson * A universally administered address is uniquely assigned to a device by its 6899a2dd95SBruce Richardson * manufacturer. The first three octets (in transmission order) contain the 6999a2dd95SBruce Richardson * Organizationally Unique Identifier (OUI). The following three (MAC-48 and 7099a2dd95SBruce Richardson * EUI-48) octets are assigned by that organization with the only constraint 7199a2dd95SBruce Richardson * of uniqueness. 7299a2dd95SBruce Richardson * A locally administered address is assigned to a device by a network 7399a2dd95SBruce Richardson * administrator and does not contain OUIs. 7499a2dd95SBruce Richardson * See http://standards.ieee.org/regauth/groupmac/tutorial.html 7599a2dd95SBruce Richardson */ 76c6552d9aSTyler Retzlaff struct __rte_aligned(2) rte_ether_addr { 7799a2dd95SBruce Richardson uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]; /**< Addr bytes in tx order */ 78c6552d9aSTyler Retzlaff }; 7999a2dd95SBruce Richardson 80*e214d58eSMorten Brørup static_assert(sizeof(struct rte_ether_addr) == 6, 81*e214d58eSMorten Brørup "sizeof(struct rte_ether_addr) == 6"); 82*e214d58eSMorten Brørup static_assert(alignof(struct rte_ether_addr) == 2, 83*e214d58eSMorten Brørup "alignof(struct rte_ether_addr) == 2"); 84*e214d58eSMorten Brørup 8599a2dd95SBruce Richardson #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */ 8699a2dd95SBruce Richardson #define RTE_ETHER_GROUP_ADDR 0x01 /**< Multicast or broadcast Eth. address. */ 8799a2dd95SBruce Richardson 8899a2dd95SBruce Richardson /** 8999a2dd95SBruce Richardson * Check if two Ethernet addresses are the same. 9099a2dd95SBruce Richardson * 9199a2dd95SBruce Richardson * @param ea1 9299a2dd95SBruce Richardson * A pointer to the first ether_addr structure containing 9399a2dd95SBruce Richardson * the ethernet address. 9499a2dd95SBruce Richardson * @param ea2 9599a2dd95SBruce Richardson * A pointer to the second ether_addr structure containing 9699a2dd95SBruce Richardson * the ethernet address. 9799a2dd95SBruce Richardson * 9899a2dd95SBruce Richardson * @return 9999a2dd95SBruce Richardson * True (1) if the given two ethernet address are the same; 10099a2dd95SBruce Richardson * False (0) otherwise. 10199a2dd95SBruce Richardson */ 10299a2dd95SBruce Richardson static inline int rte_is_same_ether_addr(const struct rte_ether_addr *ea1, 10399a2dd95SBruce Richardson const struct rte_ether_addr *ea2) 10499a2dd95SBruce Richardson { 10599a2dd95SBruce Richardson const uint16_t *w1 = (const uint16_t *)ea1; 10699a2dd95SBruce Richardson const uint16_t *w2 = (const uint16_t *)ea2; 10799a2dd95SBruce Richardson 10899a2dd95SBruce Richardson return ((w1[0] ^ w2[0]) | (w1[1] ^ w2[1]) | (w1[2] ^ w2[2])) == 0; 10999a2dd95SBruce Richardson } 11099a2dd95SBruce Richardson 11199a2dd95SBruce Richardson /** 11299a2dd95SBruce Richardson * Check if an Ethernet address is filled with zeros. 11399a2dd95SBruce Richardson * 11499a2dd95SBruce Richardson * @param ea 11599a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 11699a2dd95SBruce Richardson * to check. 11799a2dd95SBruce Richardson * @return 11899a2dd95SBruce Richardson * True (1) if the given ethernet address is filled with zeros; 11999a2dd95SBruce Richardson * false (0) otherwise. 12099a2dd95SBruce Richardson */ 12199a2dd95SBruce Richardson static inline int rte_is_zero_ether_addr(const struct rte_ether_addr *ea) 12299a2dd95SBruce Richardson { 12399a2dd95SBruce Richardson const uint16_t *w = (const uint16_t *)ea; 12499a2dd95SBruce Richardson 12599a2dd95SBruce Richardson return (w[0] | w[1] | w[2]) == 0; 12699a2dd95SBruce Richardson } 12799a2dd95SBruce Richardson 12899a2dd95SBruce Richardson /** 12999a2dd95SBruce Richardson * Check if an Ethernet address is a unicast address. 13099a2dd95SBruce Richardson * 13199a2dd95SBruce Richardson * @param ea 13299a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 13399a2dd95SBruce Richardson * to check. 13499a2dd95SBruce Richardson * @return 13599a2dd95SBruce Richardson * True (1) if the given ethernet address is a unicast address; 13699a2dd95SBruce Richardson * false (0) otherwise. 13799a2dd95SBruce Richardson */ 13899a2dd95SBruce Richardson static inline int rte_is_unicast_ether_addr(const struct rte_ether_addr *ea) 13999a2dd95SBruce Richardson { 14099a2dd95SBruce Richardson return (ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR) == 0; 14199a2dd95SBruce Richardson } 14299a2dd95SBruce Richardson 14399a2dd95SBruce Richardson /** 14499a2dd95SBruce Richardson * Check if an Ethernet address is a multicast address. 14599a2dd95SBruce Richardson * 14699a2dd95SBruce Richardson * @param ea 14799a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 14899a2dd95SBruce Richardson * to check. 14999a2dd95SBruce Richardson * @return 15099a2dd95SBruce Richardson * True (1) if the given ethernet address is a multicast address; 15199a2dd95SBruce Richardson * false (0) otherwise. 15299a2dd95SBruce Richardson */ 15399a2dd95SBruce Richardson static inline int rte_is_multicast_ether_addr(const struct rte_ether_addr *ea) 15499a2dd95SBruce Richardson { 15599a2dd95SBruce Richardson return ea->addr_bytes[0] & RTE_ETHER_GROUP_ADDR; 15699a2dd95SBruce Richardson } 15799a2dd95SBruce Richardson 15899a2dd95SBruce Richardson /** 15999a2dd95SBruce Richardson * Check if an Ethernet address is a broadcast address. 16099a2dd95SBruce Richardson * 16199a2dd95SBruce Richardson * @param ea 16299a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 16399a2dd95SBruce Richardson * to check. 16499a2dd95SBruce Richardson * @return 16599a2dd95SBruce Richardson * True (1) if the given ethernet address is a broadcast address; 16699a2dd95SBruce Richardson * false (0) otherwise. 16799a2dd95SBruce Richardson */ 16899a2dd95SBruce Richardson static inline int rte_is_broadcast_ether_addr(const struct rte_ether_addr *ea) 16999a2dd95SBruce Richardson { 17099a2dd95SBruce Richardson const uint16_t *w = (const uint16_t *)ea; 17199a2dd95SBruce Richardson 17299a2dd95SBruce Richardson return (w[0] & w[1] & w[2]) == 0xFFFF; 17399a2dd95SBruce Richardson } 17499a2dd95SBruce Richardson 17599a2dd95SBruce Richardson /** 17699a2dd95SBruce Richardson * Check if an Ethernet address is a universally assigned address. 17799a2dd95SBruce Richardson * 17899a2dd95SBruce Richardson * @param ea 17999a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 18099a2dd95SBruce Richardson * to check. 18199a2dd95SBruce Richardson * @return 18299a2dd95SBruce Richardson * True (1) if the given ethernet address is a universally assigned address; 18399a2dd95SBruce Richardson * false (0) otherwise. 18499a2dd95SBruce Richardson */ 18599a2dd95SBruce Richardson static inline int rte_is_universal_ether_addr(const struct rte_ether_addr *ea) 18699a2dd95SBruce Richardson { 18799a2dd95SBruce Richardson return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) == 0; 18899a2dd95SBruce Richardson } 18999a2dd95SBruce Richardson 19099a2dd95SBruce Richardson /** 19199a2dd95SBruce Richardson * Check if an Ethernet address is a locally assigned address. 19299a2dd95SBruce Richardson * 19399a2dd95SBruce Richardson * @param ea 19499a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 19599a2dd95SBruce Richardson * to check. 19699a2dd95SBruce Richardson * @return 19799a2dd95SBruce Richardson * True (1) if the given ethernet address is a locally assigned address; 19899a2dd95SBruce Richardson * false (0) otherwise. 19999a2dd95SBruce Richardson */ 20099a2dd95SBruce Richardson static inline int rte_is_local_admin_ether_addr(const struct rte_ether_addr *ea) 20199a2dd95SBruce Richardson { 20299a2dd95SBruce Richardson return (ea->addr_bytes[0] & RTE_ETHER_LOCAL_ADMIN_ADDR) != 0; 20399a2dd95SBruce Richardson } 20499a2dd95SBruce Richardson 20599a2dd95SBruce Richardson /** 20699a2dd95SBruce Richardson * Check if an Ethernet address is a valid address. Checks that the address is a 20799a2dd95SBruce Richardson * unicast address and is not filled with zeros. 20899a2dd95SBruce Richardson * 20999a2dd95SBruce Richardson * @param ea 21099a2dd95SBruce Richardson * A pointer to a ether_addr structure containing the ethernet address 21199a2dd95SBruce Richardson * to check. 21299a2dd95SBruce Richardson * @return 21399a2dd95SBruce Richardson * True (1) if the given ethernet address is valid; 21499a2dd95SBruce Richardson * false (0) otherwise. 21599a2dd95SBruce Richardson */ 21699a2dd95SBruce Richardson static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *ea) 21799a2dd95SBruce Richardson { 21899a2dd95SBruce Richardson return rte_is_unicast_ether_addr(ea) && (!rte_is_zero_ether_addr(ea)); 21999a2dd95SBruce Richardson } 22099a2dd95SBruce Richardson 22199a2dd95SBruce Richardson /** 22299a2dd95SBruce Richardson * Generate a random Ethernet address that is locally administered 22399a2dd95SBruce Richardson * and not multicast. 22499a2dd95SBruce Richardson * @param addr 22599a2dd95SBruce Richardson * A pointer to Ethernet address. 22699a2dd95SBruce Richardson */ 22799a2dd95SBruce Richardson void 22899a2dd95SBruce Richardson rte_eth_random_addr(uint8_t *addr); 22999a2dd95SBruce Richardson 23099a2dd95SBruce Richardson /** 23199a2dd95SBruce Richardson * Copy an Ethernet address. 23299a2dd95SBruce Richardson * 23399a2dd95SBruce Richardson * @param ea_from 23499a2dd95SBruce Richardson * A pointer to a ether_addr structure holding the Ethernet address to copy. 23599a2dd95SBruce Richardson * @param ea_to 23699a2dd95SBruce Richardson * A pointer to a ether_addr structure where to copy the Ethernet address. 23799a2dd95SBruce Richardson */ 23899a2dd95SBruce Richardson static inline void 23999a2dd95SBruce Richardson rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from, 24099a2dd95SBruce Richardson struct rte_ether_addr *__restrict ea_to) 24199a2dd95SBruce Richardson { 24299a2dd95SBruce Richardson *ea_to = *ea_from; 24399a2dd95SBruce Richardson } 24499a2dd95SBruce Richardson 245c2c4f87bSAman Deep Singh /** 246c2c4f87bSAman Deep Singh * Macro to print six-bytes of MAC address in hex format 247c2c4f87bSAman Deep Singh */ 248c2c4f87bSAman Deep Singh #define RTE_ETHER_ADDR_PRT_FMT "%02X:%02X:%02X:%02X:%02X:%02X" 249a7db3afcSAman Deep Singh /** 250a7db3afcSAman Deep Singh * Macro to extract the MAC address bytes from rte_ether_addr struct 251a7db3afcSAman Deep Singh */ 252a7db3afcSAman Deep Singh #define RTE_ETHER_ADDR_BYTES(mac_addrs) ((mac_addrs)->addr_bytes[0]), \ 253a7db3afcSAman Deep Singh ((mac_addrs)->addr_bytes[1]), \ 254a7db3afcSAman Deep Singh ((mac_addrs)->addr_bytes[2]), \ 255a7db3afcSAman Deep Singh ((mac_addrs)->addr_bytes[3]), \ 256a7db3afcSAman Deep Singh ((mac_addrs)->addr_bytes[4]), \ 257a7db3afcSAman Deep Singh ((mac_addrs)->addr_bytes[5]) 258c2c4f87bSAman Deep Singh 25999a2dd95SBruce Richardson #define RTE_ETHER_ADDR_FMT_SIZE 18 26099a2dd95SBruce Richardson /** 26199a2dd95SBruce Richardson * Format 48bits Ethernet address in pattern xx:xx:xx:xx:xx:xx. 26299a2dd95SBruce Richardson * 26399a2dd95SBruce Richardson * @param buf 26499a2dd95SBruce Richardson * A pointer to buffer contains the formatted MAC address. 26599a2dd95SBruce Richardson * @param size 26699a2dd95SBruce Richardson * The format buffer size. 26799a2dd95SBruce Richardson * @param eth_addr 26899a2dd95SBruce Richardson * A pointer to a ether_addr structure. 26999a2dd95SBruce Richardson */ 27099a2dd95SBruce Richardson void 27199a2dd95SBruce Richardson rte_ether_format_addr(char *buf, uint16_t size, 27299a2dd95SBruce Richardson const struct rte_ether_addr *eth_addr); 27399a2dd95SBruce Richardson /** 27499a2dd95SBruce Richardson * Convert string with Ethernet address to an ether_addr. 27599a2dd95SBruce Richardson * 27699a2dd95SBruce Richardson * @param str 27799a2dd95SBruce Richardson * A pointer to buffer contains the formatted MAC address. 278df6e6ddeSStephen Hemminger * Accepts either byte or word format separated by colon, 279df6e6ddeSStephen Hemminger * hyphen or period. 280df6e6ddeSStephen Hemminger * 281df6e6ddeSStephen Hemminger * The example formats are: 282df6e6ddeSStephen Hemminger * XX:XX:XX:XX:XX:XX - Canonical form 283df6e6ddeSStephen Hemminger * XX-XX-XX-XX-XX-XX - Windows and IEEE 802 284df6e6ddeSStephen Hemminger * XXXX.XXXX.XXXX - Cisco 28599a2dd95SBruce Richardson * where XX is a hex digit: 0-9, a-f, or A-F. 286df6e6ddeSStephen Hemminger * In the byte format, leading zeros are optional. 28799a2dd95SBruce Richardson * @param eth_addr 28899a2dd95SBruce Richardson * A pointer to a ether_addr structure. 28999a2dd95SBruce Richardson * @return 29099a2dd95SBruce Richardson * 0 if successful 29199a2dd95SBruce Richardson * -1 and sets rte_errno if invalid string 29299a2dd95SBruce Richardson */ 29399a2dd95SBruce Richardson int 29499a2dd95SBruce Richardson rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr); 29599a2dd95SBruce Richardson 29699a2dd95SBruce Richardson /** 29799a2dd95SBruce Richardson * Ethernet header: Contains the destination address, source address 29899a2dd95SBruce Richardson * and frame type. 29999a2dd95SBruce Richardson */ 300*e214d58eSMorten Brørup struct rte_ether_hdr { 30104d43857SDmitry Kozlyuk struct rte_ether_addr dst_addr; /**< Destination address. */ 30204d43857SDmitry Kozlyuk struct rte_ether_addr src_addr; /**< Source address. */ 303dc2c712fSDavid Marchand rte_be16_t ether_type; /**< Frame type. */ 304c6552d9aSTyler Retzlaff }; 30599a2dd95SBruce Richardson 306*e214d58eSMorten Brørup static_assert(sizeof(struct rte_ether_hdr) == 14, 307*e214d58eSMorten Brørup "sizeof(struct rte_ether_hdr) == 14"); 308*e214d58eSMorten Brørup static_assert(alignof(struct rte_ether_hdr) == 2, 309*e214d58eSMorten Brørup "alignof(struct rte_ether_hdr) == 2"); 310*e214d58eSMorten Brørup 31199a2dd95SBruce Richardson /** 31299a2dd95SBruce Richardson * Ethernet VLAN Header. 31399a2dd95SBruce Richardson * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet type 31499a2dd95SBruce Richardson * of the encapsulated frame. 31599a2dd95SBruce Richardson */ 31699a2dd95SBruce Richardson struct rte_vlan_hdr { 317dc2c712fSDavid Marchand rte_be16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */ 318dc2c712fSDavid Marchand rte_be16_t eth_proto; /**< Ethernet type of encapsulated frame. */ 319*e214d58eSMorten Brørup }; 320*e214d58eSMorten Brørup 321*e214d58eSMorten Brørup static_assert(sizeof(struct rte_vlan_hdr) == 4, 322*e214d58eSMorten Brørup "sizeof(struct rte_vlan_hdr) == 4"); 323*e214d58eSMorten Brørup static_assert(alignof(struct rte_vlan_hdr) == 2, 324*e214d58eSMorten Brørup "alignof(struct rte_vlan_hdr) == 2"); 32599a2dd95SBruce Richardson 32699a2dd95SBruce Richardson 32799a2dd95SBruce Richardson 32899a2dd95SBruce Richardson /* Ethernet frame types */ 32999a2dd95SBruce Richardson #define RTE_ETHER_TYPE_IPV4 0x0800 /**< IPv4 Protocol. */ 33099a2dd95SBruce Richardson #define RTE_ETHER_TYPE_IPV6 0x86DD /**< IPv6 Protocol. */ 33199a2dd95SBruce Richardson #define RTE_ETHER_TYPE_ARP 0x0806 /**< Arp Protocol. */ 33299a2dd95SBruce Richardson #define RTE_ETHER_TYPE_RARP 0x8035 /**< Reverse Arp Protocol. */ 33399a2dd95SBruce Richardson #define RTE_ETHER_TYPE_VLAN 0x8100 /**< IEEE 802.1Q VLAN tagging. */ 33499a2dd95SBruce Richardson #define RTE_ETHER_TYPE_QINQ 0x88A8 /**< IEEE 802.1ad QinQ tagging. */ 33599a2dd95SBruce Richardson #define RTE_ETHER_TYPE_QINQ1 0x9100 /**< Deprecated QinQ VLAN. */ 33699a2dd95SBruce Richardson #define RTE_ETHER_TYPE_QINQ2 0x9200 /**< Deprecated QinQ VLAN. */ 33799a2dd95SBruce Richardson #define RTE_ETHER_TYPE_QINQ3 0x9300 /**< Deprecated QinQ VLAN. */ 33899a2dd95SBruce Richardson #define RTE_ETHER_TYPE_PPPOE_DISCOVERY 0x8863 /**< PPPoE Discovery Stage. */ 33999a2dd95SBruce Richardson #define RTE_ETHER_TYPE_PPPOE_SESSION 0x8864 /**< PPPoE Session Stage. */ 34099a2dd95SBruce Richardson #define RTE_ETHER_TYPE_ETAG 0x893F /**< IEEE 802.1BR E-Tag. */ 34199a2dd95SBruce Richardson #define RTE_ETHER_TYPE_1588 0x88F7 34299a2dd95SBruce Richardson /**< IEEE 802.1AS 1588 Precise Time Protocol. */ 34399a2dd95SBruce Richardson #define RTE_ETHER_TYPE_SLOW 0x8809 /**< Slow protocols (LACP and Marker). */ 34499a2dd95SBruce Richardson #define RTE_ETHER_TYPE_TEB 0x6558 /**< Transparent Ethernet Bridging. */ 34599a2dd95SBruce Richardson #define RTE_ETHER_TYPE_LLDP 0x88CC /**< LLDP Protocol. */ 34699a2dd95SBruce Richardson #define RTE_ETHER_TYPE_MPLS 0x8847 /**< MPLS ethertype. */ 34799a2dd95SBruce Richardson #define RTE_ETHER_TYPE_MPLSM 0x8848 /**< MPLS multicast ethertype. */ 34899a2dd95SBruce Richardson #define RTE_ETHER_TYPE_ECPRI 0xAEFE /**< eCPRI ethertype (.1Q supported). */ 34999a2dd95SBruce Richardson 35099a2dd95SBruce Richardson /** 35199a2dd95SBruce Richardson * Extract VLAN tag information into mbuf 35299a2dd95SBruce Richardson * 35399a2dd95SBruce Richardson * Software version of VLAN stripping 35499a2dd95SBruce Richardson * 35599a2dd95SBruce Richardson * @param m 35699a2dd95SBruce Richardson * The packet mbuf. 35799a2dd95SBruce Richardson * @return 35899a2dd95SBruce Richardson * - 0: Success 35999a2dd95SBruce Richardson * - 1: not a vlan packet 36099a2dd95SBruce Richardson */ 36199a2dd95SBruce Richardson static inline int rte_vlan_strip(struct rte_mbuf *m) 36299a2dd95SBruce Richardson { 36399a2dd95SBruce Richardson struct rte_ether_hdr *eh 36499a2dd95SBruce Richardson = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); 36599a2dd95SBruce Richardson struct rte_vlan_hdr *vh; 36699a2dd95SBruce Richardson 36799a2dd95SBruce Richardson if (eh->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) 36899a2dd95SBruce Richardson return -1; 36999a2dd95SBruce Richardson 37099a2dd95SBruce Richardson vh = (struct rte_vlan_hdr *)(eh + 1); 371daa02b5cSOlivier Matz m->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED; 37299a2dd95SBruce Richardson m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci); 37399a2dd95SBruce Richardson 37499a2dd95SBruce Richardson /* Copy ether header over rather than moving whole packet */ 37599a2dd95SBruce Richardson memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)), 37699a2dd95SBruce Richardson eh, 2 * RTE_ETHER_ADDR_LEN); 37799a2dd95SBruce Richardson 37899a2dd95SBruce Richardson return 0; 37999a2dd95SBruce Richardson } 38099a2dd95SBruce Richardson 38199a2dd95SBruce Richardson /** 38299a2dd95SBruce Richardson * Insert VLAN tag into mbuf. 38399a2dd95SBruce Richardson * 38499a2dd95SBruce Richardson * Software version of VLAN unstripping 38599a2dd95SBruce Richardson * 38699a2dd95SBruce Richardson * @param m 38799a2dd95SBruce Richardson * The packet mbuf. 38899a2dd95SBruce Richardson * @return 38999a2dd95SBruce Richardson * - 0: On success 39023f3dac4SStephen Hemminger * -EPERM: mbuf is shared overwriting would be unsafe 39199a2dd95SBruce Richardson * -ENOSPC: not enough headroom in mbuf 39299a2dd95SBruce Richardson */ 39399a2dd95SBruce Richardson static inline int rte_vlan_insert(struct rte_mbuf **m) 39499a2dd95SBruce Richardson { 39599a2dd95SBruce Richardson struct rte_ether_hdr *oh, *nh; 39699a2dd95SBruce Richardson struct rte_vlan_hdr *vh; 39799a2dd95SBruce Richardson 39899a2dd95SBruce Richardson /* Can't insert header if mbuf is shared */ 39999a2dd95SBruce Richardson if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1) 40099a2dd95SBruce Richardson return -EINVAL; 40199a2dd95SBruce Richardson 40299a2dd95SBruce Richardson /* Can't insert header if the first segment is too short */ 40399a2dd95SBruce Richardson if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN) 40499a2dd95SBruce Richardson return -EINVAL; 40599a2dd95SBruce Richardson 40699a2dd95SBruce Richardson oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *); 407a3f8d058SEli Britstein nh = (struct rte_ether_hdr *)(void *) 40899a2dd95SBruce Richardson rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr)); 40999a2dd95SBruce Richardson if (nh == NULL) 41099a2dd95SBruce Richardson return -ENOSPC; 41199a2dd95SBruce Richardson 41299a2dd95SBruce Richardson memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN); 41399a2dd95SBruce Richardson nh->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN); 41499a2dd95SBruce Richardson 41599a2dd95SBruce Richardson vh = (struct rte_vlan_hdr *) (nh + 1); 41699a2dd95SBruce Richardson vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci); 41799a2dd95SBruce Richardson 418daa02b5cSOlivier Matz (*m)->ol_flags &= ~(RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_TX_VLAN); 41999a2dd95SBruce Richardson 420daa02b5cSOlivier Matz if ((*m)->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) 42199a2dd95SBruce Richardson (*m)->outer_l2_len += sizeof(struct rte_vlan_hdr); 42299a2dd95SBruce Richardson else 42399a2dd95SBruce Richardson (*m)->l2_len += sizeof(struct rte_vlan_hdr); 42499a2dd95SBruce Richardson 42599a2dd95SBruce Richardson return 0; 42699a2dd95SBruce Richardson } 42799a2dd95SBruce Richardson 42899a2dd95SBruce Richardson #ifdef __cplusplus 42999a2dd95SBruce Richardson } 43099a2dd95SBruce Richardson #endif 43199a2dd95SBruce Richardson 43299a2dd95SBruce Richardson #endif /* _RTE_ETHER_H_ */ 433