197f17497SC.J. Collier/*-
297f17497SC.J. Collier *   BSD LICENSE
397f17497SC.J. Collier *
497f17497SC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
597f17497SC.J. Collier *   All rights reserved.
697f17497SC.J. Collier *
797f17497SC.J. Collier *   Redistribution and use in source and binary forms, with or without
897f17497SC.J. Collier *   modification, are permitted provided that the following conditions
997f17497SC.J. Collier *   are met:
1097f17497SC.J. Collier *
1197f17497SC.J. Collier *     * Redistributions of source code must retain the above copyright
1297f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer.
1397f17497SC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
1497f17497SC.J. Collier *       notice, this list of conditions and the following disclaimer in
1597f17497SC.J. Collier *       the documentation and/or other materials provided with the
1697f17497SC.J. Collier *       distribution.
1797f17497SC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
1897f17497SC.J. Collier *       contributors may be used to endorse or promote products derived
1997f17497SC.J. Collier *       from this software without specific prior written permission.
2097f17497SC.J. Collier *
2197f17497SC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2297f17497SC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2397f17497SC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2497f17497SC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2597f17497SC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2697f17497SC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2797f17497SC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2897f17497SC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2997f17497SC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3097f17497SC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3197f17497SC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3297f17497SC.J. Collier */
3397f17497SC.J. Collier
3497f17497SC.J. Collier#ifndef _RTE_ETHER_H_
3597f17497SC.J. Collier#define _RTE_ETHER_H_
3697f17497SC.J. Collier
3797f17497SC.J. Collier/**
3897f17497SC.J. Collier * @file
3997f17497SC.J. Collier *
4097f17497SC.J. Collier * Ethernet Helpers in RTE
4197f17497SC.J. Collier */
4297f17497SC.J. Collier
4397f17497SC.J. Collier#ifdef __cplusplus
4497f17497SC.J. Collierextern "C" {
4597f17497SC.J. Collier#endif
4697f17497SC.J. Collier
4797f17497SC.J. Collier#include <stdint.h>
4897f17497SC.J. Collier#include <stdio.h>
4997f17497SC.J. Collier
5097f17497SC.J. Collier#include <rte_memcpy.h>
5197f17497SC.J. Collier#include <rte_random.h>
5297f17497SC.J. Collier#include <rte_mbuf.h>
5397f17497SC.J. Collier#include <rte_byteorder.h>
5497f17497SC.J. Collier
5597f17497SC.J. Collier#define ETHER_ADDR_LEN  6 /**< Length of Ethernet address. */
5697f17497SC.J. Collier#define ETHER_TYPE_LEN  2 /**< Length of Ethernet type field. */
5797f17497SC.J. Collier#define ETHER_CRC_LEN   4 /**< Length of Ethernet CRC. */
5897f17497SC.J. Collier#define ETHER_HDR_LEN   \
5997f17497SC.J. Collier	(ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) /**< Length of Ethernet header. */
6097f17497SC.J. Collier#define ETHER_MIN_LEN   64    /**< Minimum frame len, including CRC. */
6197f17497SC.J. Collier#define ETHER_MAX_LEN   1518  /**< Maximum frame len, including CRC. */
6297f17497SC.J. Collier#define ETHER_MTU       \
6397f17497SC.J. Collier	(ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN) /**< Ethernet MTU. */
6497f17497SC.J. Collier
6597f17497SC.J. Collier#define ETHER_MAX_VLAN_FRAME_LEN \
6697f17497SC.J. Collier	(ETHER_MAX_LEN + 4) /**< Maximum VLAN frame length, including CRC. */
6797f17497SC.J. Collier
6897f17497SC.J. Collier#define ETHER_MAX_JUMBO_FRAME_LEN \
6997f17497SC.J. Collier	0x3F00 /**< Maximum Jumbo frame length, including CRC. */
7097f17497SC.J. Collier
7197f17497SC.J. Collier#define ETHER_MAX_VLAN_ID  4095 /**< Maximum VLAN ID. */
7297f17497SC.J. Collier
7397f17497SC.J. Collier#define ETHER_MIN_MTU 68 /**< Minimum MTU for IPv4 packets, see RFC 791. */
7497f17497SC.J. Collier
7597f17497SC.J. Collier/**
7697f17497SC.J. Collier * Ethernet address:
7797f17497SC.J. Collier * A universally administered address is uniquely assigned to a device by its
7897f17497SC.J. Collier * manufacturer. The first three octets (in transmission order) contain the
7997f17497SC.J. Collier * Organizationally Unique Identifier (OUI). The following three (MAC-48 and
8097f17497SC.J. Collier * EUI-48) octets are assigned by that organization with the only constraint
8197f17497SC.J. Collier * of uniqueness.
8297f17497SC.J. Collier * A locally administered address is assigned to a device by a network
8397f17497SC.J. Collier * administrator and does not contain OUIs.
8497f17497SC.J. Collier * See http://standards.ieee.org/regauth/groupmac/tutorial.html
8597f17497SC.J. Collier */
8697f17497SC.J. Collierstruct ether_addr {
876b3e017eSChristian Ehrhardt	uint8_t addr_bytes[ETHER_ADDR_LEN]; /**< Addr bytes in tx order */
8897f17497SC.J. Collier} __attribute__((__packed__));
8997f17497SC.J. Collier
9097f17497SC.J. Collier#define ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */
9197f17497SC.J. Collier#define ETHER_GROUP_ADDR       0x01 /**< Multicast or broadcast Eth. address. */
9297f17497SC.J. Collier
9397f17497SC.J. Collier/**
9497f17497SC.J. Collier * Check if two Ethernet addresses are the same.
9597f17497SC.J. Collier *
9697f17497SC.J. Collier * @param ea1
9797f17497SC.J. Collier *  A pointer to the first ether_addr structure containing
9897f17497SC.J. Collier *  the ethernet address.
9997f17497SC.J. Collier * @param ea2
10097f17497SC.J. Collier *  A pointer to the second ether_addr structure containing
10197f17497SC.J. Collier *  the ethernet address.
10297f17497SC.J. Collier *
10397f17497SC.J. Collier * @return
10497f17497SC.J. Collier *  True  (1) if the given two ethernet address are the same;
10597f17497SC.J. Collier *  False (0) otherwise.
10697f17497SC.J. Collier */
10797f17497SC.J. Collierstatic inline int is_same_ether_addr(const struct ether_addr *ea1,
10897f17497SC.J. Collier				     const struct ether_addr *ea2)
10997f17497SC.J. Collier{
11097f17497SC.J. Collier	int i;
11197f17497SC.J. Collier	for (i = 0; i < ETHER_ADDR_LEN; i++)
11297f17497SC.J. Collier		if (ea1->addr_bytes[i] != ea2->addr_bytes[i])
11397f17497SC.J. Collier			return 0;
11497f17497SC.J. Collier	return 1;
11597f17497SC.J. Collier}
11697f17497SC.J. Collier
11797f17497SC.J. Collier/**
11897f17497SC.J. Collier * Check if an Ethernet address is filled with zeros.
11997f17497SC.J. Collier *
12097f17497SC.J. Collier * @param ea
12197f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
12297f17497SC.J. Collier *   to check.
12397f17497SC.J. Collier * @return
12497f17497SC.J. Collier *   True  (1) if the given ethernet address is filled with zeros;
12597f17497SC.J. Collier *   false (0) otherwise.
12697f17497SC.J. Collier */
12797f17497SC.J. Collierstatic inline int is_zero_ether_addr(const struct ether_addr *ea)
12897f17497SC.J. Collier{
12997f17497SC.J. Collier	int i;
13097f17497SC.J. Collier	for (i = 0; i < ETHER_ADDR_LEN; i++)
13197f17497SC.J. Collier		if (ea->addr_bytes[i] != 0x00)
13297f17497SC.J. Collier			return 0;
13397f17497SC.J. Collier	return 1;
13497f17497SC.J. Collier}
13597f17497SC.J. Collier
13697f17497SC.J. Collier/**
13797f17497SC.J. Collier * Check if an Ethernet address is a unicast address.
13897f17497SC.J. Collier *
13997f17497SC.J. Collier * @param ea
14097f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
14197f17497SC.J. Collier *   to check.
14297f17497SC.J. Collier * @return
14397f17497SC.J. Collier *   True  (1) if the given ethernet address is a unicast address;
14497f17497SC.J. Collier *   false (0) otherwise.
14597f17497SC.J. Collier */
14697f17497SC.J. Collierstatic inline int is_unicast_ether_addr(const struct ether_addr *ea)
14797f17497SC.J. Collier{
14897f17497SC.J. Collier	return (ea->addr_bytes[0] & ETHER_GROUP_ADDR) == 0;
14997f17497SC.J. Collier}
15097f17497SC.J. Collier
15197f17497SC.J. Collier/**
15297f17497SC.J. Collier * Check if an Ethernet address is a multicast address.
15397f17497SC.J. Collier *
15497f17497SC.J. Collier * @param ea
15597f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
15697f17497SC.J. Collier *   to check.
15797f17497SC.J. Collier * @return
15897f17497SC.J. Collier *   True  (1) if the given ethernet address is a multicast address;
15997f17497SC.J. Collier *   false (0) otherwise.
16097f17497SC.J. Collier */
16197f17497SC.J. Collierstatic inline int is_multicast_ether_addr(const struct ether_addr *ea)
16297f17497SC.J. Collier{
16397f17497SC.J. Collier	return ea->addr_bytes[0] & ETHER_GROUP_ADDR;
16497f17497SC.J. Collier}
16597f17497SC.J. Collier
16697f17497SC.J. Collier/**
16797f17497SC.J. Collier * Check if an Ethernet address is a broadcast address.
16897f17497SC.J. Collier *
16997f17497SC.J. Collier * @param ea
17097f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
17197f17497SC.J. Collier *   to check.
17297f17497SC.J. Collier * @return
17397f17497SC.J. Collier *   True  (1) if the given ethernet address is a broadcast address;
17497f17497SC.J. Collier *   false (0) otherwise.
17597f17497SC.J. Collier */
17697f17497SC.J. Collierstatic inline int is_broadcast_ether_addr(const struct ether_addr *ea)
17797f17497SC.J. Collier{
17897f17497SC.J. Collier	const unaligned_uint16_t *ea_words = (const unaligned_uint16_t *)ea;
17997f17497SC.J. Collier
18097f17497SC.J. Collier	return (ea_words[0] == 0xFFFF && ea_words[1] == 0xFFFF &&
18197f17497SC.J. Collier		ea_words[2] == 0xFFFF);
18297f17497SC.J. Collier}
18397f17497SC.J. Collier
18497f17497SC.J. Collier/**
18597f17497SC.J. Collier * Check if an Ethernet address is a universally assigned address.
18697f17497SC.J. Collier *
18797f17497SC.J. Collier * @param ea
18897f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
18997f17497SC.J. Collier *   to check.
19097f17497SC.J. Collier * @return
19197f17497SC.J. Collier *   True  (1) if the given ethernet address is a universally assigned address;
19297f17497SC.J. Collier *   false (0) otherwise.
19397f17497SC.J. Collier */
19497f17497SC.J. Collierstatic inline int is_universal_ether_addr(const struct ether_addr *ea)
19597f17497SC.J. Collier{
19697f17497SC.J. Collier	return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) == 0;
19797f17497SC.J. Collier}
19897f17497SC.J. Collier
19997f17497SC.J. Collier/**
20097f17497SC.J. Collier * Check if an Ethernet address is a locally assigned address.
20197f17497SC.J. Collier *
20297f17497SC.J. Collier * @param ea
20397f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
20497f17497SC.J. Collier *   to check.
20597f17497SC.J. Collier * @return
20697f17497SC.J. Collier *   True  (1) if the given ethernet address is a locally assigned address;
20797f17497SC.J. Collier *   false (0) otherwise.
20897f17497SC.J. Collier */
20997f17497SC.J. Collierstatic inline int is_local_admin_ether_addr(const struct ether_addr *ea)
21097f17497SC.J. Collier{
21197f17497SC.J. Collier	return (ea->addr_bytes[0] & ETHER_LOCAL_ADMIN_ADDR) != 0;
21297f17497SC.J. Collier}
21397f17497SC.J. Collier
21497f17497SC.J. Collier/**
21597f17497SC.J. Collier * Check if an Ethernet address is a valid address. Checks that the address is a
21697f17497SC.J. Collier * unicast address and is not filled with zeros.
21797f17497SC.J. Collier *
21897f17497SC.J. Collier * @param ea
21997f17497SC.J. Collier *   A pointer to a ether_addr structure containing the ethernet address
22097f17497SC.J. Collier *   to check.
22197f17497SC.J. Collier * @return
22297f17497SC.J. Collier *   True  (1) if the given ethernet address is valid;
22397f17497SC.J. Collier *   false (0) otherwise.
22497f17497SC.J. Collier */
22597f17497SC.J. Collierstatic inline int is_valid_assigned_ether_addr(const struct ether_addr *ea)
22697f17497SC.J. Collier{
2276b3e017eSChristian Ehrhardt	return is_unicast_ether_addr(ea) && (!is_zero_ether_addr(ea));
22897f17497SC.J. Collier}
22997f17497SC.J. Collier
23097f17497SC.J. Collier/**
23197f17497SC.J. Collier * Generate a random Ethernet address that is locally administered
23297f17497SC.J. Collier * and not multicast.
23397f17497SC.J. Collier * @param addr
23497f17497SC.J. Collier *   A pointer to Ethernet address.
23597f17497SC.J. Collier */
23697f17497SC.J. Collierstatic inline void eth_random_addr(uint8_t *addr)
23797f17497SC.J. Collier{
23897f17497SC.J. Collier	uint64_t rand = rte_rand();
2396b3e017eSChristian Ehrhardt	uint8_t *p = (uint8_t *)&rand;
24097f17497SC.J. Collier
24197f17497SC.J. Collier	rte_memcpy(addr, p, ETHER_ADDR_LEN);
24243192222SLuca Boccassi	addr[0] &= (uint8_t)~ETHER_GROUP_ADDR;       /* clear multicast bit */
24397f17497SC.J. Collier	addr[0] |= ETHER_LOCAL_ADMIN_ADDR;  /* set local assignment bit */
24497f17497SC.J. Collier}
24597f17497SC.J. Collier
24697f17497SC.J. Collier/**
24797f17497SC.J. Collier * Fast copy an Ethernet address.
24897f17497SC.J. Collier *
24997f17497SC.J. Collier * @param ea_from
25097f17497SC.J. Collier *   A pointer to a ether_addr structure holding the Ethernet address to copy.
25197f17497SC.J. Collier * @param ea_to
25297f17497SC.J. Collier *   A pointer to a ether_addr structure where to copy the Ethernet address.
25397f17497SC.J. Collier */
25497f17497SC.J. Collierstatic inline void ether_addr_copy(const struct ether_addr *ea_from,
25597f17497SC.J. Collier				   struct ether_addr *ea_to)
25697f17497SC.J. Collier{
25797f17497SC.J. Collier#ifdef __INTEL_COMPILER
25897f17497SC.J. Collier	uint16_t *from_words = (uint16_t *)(ea_from->addr_bytes);
25997f17497SC.J. Collier	uint16_t *to_words   = (uint16_t *)(ea_to->addr_bytes);
26097f17497SC.J. Collier
26197f17497SC.J. Collier	to_words[0] = from_words[0];
26297f17497SC.J. Collier	to_words[1] = from_words[1];
26397f17497SC.J. Collier	to_words[2] = from_words[2];
26497f17497SC.J. Collier#else
26597f17497SC.J. Collier	/*
26697f17497SC.J. Collier	 * Use the common way, because of a strange gcc warning.
26797f17497SC.J. Collier	 */
26897f17497SC.J. Collier	*ea_to = *ea_from;
26997f17497SC.J. Collier#endif
27097f17497SC.J. Collier}
27197f17497SC.J. Collier
27297f17497SC.J. Collier#define ETHER_ADDR_FMT_SIZE         18
27397f17497SC.J. Collier/**
27497f17497SC.J. Collier * Format 48bits Ethernet address in pattern xx:xx:xx:xx:xx:xx.
27597f17497SC.J. Collier *
27697f17497SC.J. Collier * @param buf
27797f17497SC.J. Collier *   A pointer to buffer contains the formatted MAC address.
27897f17497SC.J. Collier * @param size
27997f17497SC.J. Collier *   The format buffer size.
28097f17497SC.J. Collier * @param eth_addr
28197f17497SC.J. Collier *   A pointer to a ether_addr structure.
28297f17497SC.J. Collier */
28397f17497SC.J. Collierstatic inline void
28497f17497SC.J. Collierether_format_addr(char *buf, uint16_t size,
28597f17497SC.J. Collier		  const struct ether_addr *eth_addr)
28697f17497SC.J. Collier{
28797f17497SC.J. Collier	snprintf(buf, size, "%02X:%02X:%02X:%02X:%02X:%02X",
28897f17497SC.J. Collier		 eth_addr->addr_bytes[0],
28997f17497SC.J. Collier		 eth_addr->addr_bytes[1],
29097f17497SC.J. Collier		 eth_addr->addr_bytes[2],
29197f17497SC.J. Collier		 eth_addr->addr_bytes[3],
29297f17497SC.J. Collier		 eth_addr->addr_bytes[4],
29397f17497SC.J. Collier		 eth_addr->addr_bytes[5]);
29497f17497SC.J. Collier}
29597f17497SC.J. Collier
29697f17497SC.J. Collier/**
29797f17497SC.J. Collier * Ethernet header: Contains the destination address, source address
29897f17497SC.J. Collier * and frame type.
29997f17497SC.J. Collier */
30097f17497SC.J. Collierstruct ether_hdr {
30197f17497SC.J. Collier	struct ether_addr d_addr; /**< Destination address. */
30297f17497SC.J. Collier	struct ether_addr s_addr; /**< Source address. */
30397f17497SC.J. Collier	uint16_t ether_type;      /**< Frame type. */
30497f17497SC.J. Collier} __attribute__((__packed__));
30597f17497SC.J. Collier
30697f17497SC.J. Collier/**
30797f17497SC.J. Collier * Ethernet VLAN Header.
30897f17497SC.J. Collier * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet type
30997f17497SC.J. Collier * of the encapsulated frame.
31097f17497SC.J. Collier */
31197f17497SC.J. Collierstruct vlan_hdr {
31297f17497SC.J. Collier	uint16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */
31397f17497SC.J. Collier	uint16_t eth_proto;/**< Ethernet type of encapsulated frame. */
31497f17497SC.J. Collier} __attribute__((__packed__));
31597f17497SC.J. Collier
31697f17497SC.J. Collier/**
31797f17497SC.J. Collier * VXLAN protocol header.
31897f17497SC.J. Collier * Contains the 8-bit flag, 24-bit VXLAN Network Identifier and
31997f17497SC.J. Collier * Reserved fields (24 bits and 8 bits)
32097f17497SC.J. Collier */
32197f17497SC.J. Collierstruct vxlan_hdr {
32297f17497SC.J. Collier	uint32_t vx_flags; /**< flag (8) + Reserved (24). */
32397f17497SC.J. Collier	uint32_t vx_vni;   /**< VNI (24) + Reserved (8). */
32497f17497SC.J. Collier} __attribute__((__packed__));
32597f17497SC.J. Collier
32697f17497SC.J. Collier/* Ethernet frame types */
32797f17497SC.J. Collier#define ETHER_TYPE_IPv4 0x0800 /**< IPv4 Protocol. */
32897f17497SC.J. Collier#define ETHER_TYPE_IPv6 0x86DD /**< IPv6 Protocol. */
32997f17497SC.J. Collier#define ETHER_TYPE_ARP  0x0806 /**< Arp Protocol. */
33097f17497SC.J. Collier#define ETHER_TYPE_RARP 0x8035 /**< Reverse Arp Protocol. */
33197f17497SC.J. Collier#define ETHER_TYPE_VLAN 0x8100 /**< IEEE 802.1Q VLAN tagging. */
3326b3e017eSChristian Ehrhardt#define ETHER_TYPE_QINQ 0x88A8 /**< IEEE 802.1ad QinQ tagging. */
33397f17497SC.J. Collier#define ETHER_TYPE_1588 0x88F7 /**< IEEE 802.1AS 1588 Precise Time Protocol. */
33497f17497SC.J. Collier#define ETHER_TYPE_SLOW 0x8809 /**< Slow protocols (LACP and Marker). */
33597f17497SC.J. Collier#define ETHER_TYPE_TEB  0x6558 /**< Transparent Ethernet Bridging. */
33697f17497SC.J. Collier
33797f17497SC.J. Collier#define ETHER_VXLAN_HLEN (sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr))
33897f17497SC.J. Collier/**< VXLAN tunnel header length. */
33997f17497SC.J. Collier
34097f17497SC.J. Collier/**
34197f17497SC.J. Collier * Extract VLAN tag information into mbuf
34297f17497SC.J. Collier *
34397f17497SC.J. Collier * Software version of VLAN stripping
34497f17497SC.J. Collier *
34597f17497SC.J. Collier * @param m
34697f17497SC.J. Collier *   The packet mbuf.
34797f17497SC.J. Collier * @return
34897f17497SC.J. Collier *   - 0: Success
34997f17497SC.J. Collier *   - 1: not a vlan packet
35097f17497SC.J. Collier */
35197f17497SC.J. Collierstatic inline int rte_vlan_strip(struct rte_mbuf *m)
35297f17497SC.J. Collier{
35397f17497SC.J. Collier	struct ether_hdr *eh
35497f17497SC.J. Collier		 = rte_pktmbuf_mtod(m, struct ether_hdr *);
35543192222SLuca Boccassi	struct vlan_hdr *vh;
35697f17497SC.J. Collier
35797f17497SC.J. Collier	if (eh->ether_type != rte_cpu_to_be_16(ETHER_TYPE_VLAN))
35897f17497SC.J. Collier		return -1;
35997f17497SC.J. Collier
36043192222SLuca Boccassi	vh = (struct vlan_hdr *)(eh + 1);
361aab0c291SChristian Ehrhardt	m->ol_flags |= PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED;
36297f17497SC.J. Collier	m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
36397f17497SC.J. Collier
36497f17497SC.J. Collier	/* Copy ether header over rather than moving whole packet */
36597f17497SC.J. Collier	memmove(rte_pktmbuf_adj(m, sizeof(struct vlan_hdr)),
36697f17497SC.J. Collier		eh, 2 * ETHER_ADDR_LEN);
36797f17497SC.J. Collier
36897f17497SC.J. Collier	return 0;
36997f17497SC.J. Collier}
37097f17497SC.J. Collier
37197f17497SC.J. Collier/**
37297f17497SC.J. Collier * Insert VLAN tag into mbuf.
37397f17497SC.J. Collier *
37497f17497SC.J. Collier * Software version of VLAN unstripping
37597f17497SC.J. Collier *
37697f17497SC.J. Collier * @param m
37797f17497SC.J. Collier *   The packet mbuf.
37897f17497SC.J. Collier * @return
37997f17497SC.J. Collier *   - 0: On success
38097f17497SC.J. Collier *   -EPERM: mbuf is is shared overwriting would be unsafe
38197f17497SC.J. Collier *   -ENOSPC: not enough headroom in mbuf
38297f17497SC.J. Collier */
38397f17497SC.J. Collierstatic inline int rte_vlan_insert(struct rte_mbuf **m)
38497f17497SC.J. Collier{
38597f17497SC.J. Collier	struct ether_hdr *oh, *nh;
38697f17497SC.J. Collier	struct vlan_hdr *vh;
38797f17497SC.J. Collier
38897f17497SC.J. Collier	/* Can't insert header if mbuf is shared */
38997f17497SC.J. Collier	if (rte_mbuf_refcnt_read(*m) > 1) {
39097f17497SC.J. Collier		struct rte_mbuf *copy;
39197f17497SC.J. Collier
39297f17497SC.J. Collier		copy = rte_pktmbuf_clone(*m, (*m)->pool);
39397f17497SC.J. Collier		if (unlikely(copy == NULL))
39497f17497SC.J. Collier			return -ENOMEM;
39597f17497SC.J. Collier		rte_pktmbuf_free(*m);
39697f17497SC.J. Collier		*m = copy;
39797f17497SC.J. Collier	}
39897f17497SC.J. Collier
39997f17497SC.J. Collier	oh = rte_pktmbuf_mtod(*m, struct ether_hdr *);
40097f17497SC.J. Collier	nh = (struct ether_hdr *)
40197f17497SC.J. Collier		rte_pktmbuf_prepend(*m, sizeof(struct vlan_hdr));
40297f17497SC.J. Collier	if (nh == NULL)
40397f17497SC.J. Collier		return -ENOSPC;
40497f17497SC.J. Collier
40597f17497SC.J. Collier	memmove(nh, oh, 2 * ETHER_ADDR_LEN);
40697f17497SC.J. Collier	nh->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
40797f17497SC.J. Collier
40897f17497SC.J. Collier	vh = (struct vlan_hdr *) (nh + 1);
40997f17497SC.J. Collier	vh->vlan_tci = rte_cpu_to_be_16((*m)->vlan_tci);
41097f17497SC.J. Collier
411aab0c291SChristian Ehrhardt	(*m)->ol_flags &= ~PKT_RX_VLAN_STRIPPED;
412aab0c291SChristian Ehrhardt
41397f17497SC.J. Collier	return 0;
41497f17497SC.J. Collier}
41597f17497SC.J. Collier
41697f17497SC.J. Collier#ifdef __cplusplus
41797f17497SC.J. Collier}
41897f17497SC.J. Collier#endif
41997f17497SC.J. Collier
42097f17497SC.J. Collier#endif /* _RTE_ETHER_H_ */
421