4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_hash_crc.h>
36 #include <rte_byteorder.h>
45 * Parse an ethernet header to fill the ethertype, outer_l2_len, outer_l3_len and
46 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
50 parse_ethernet(struct ether_hdr *eth_hdr, union tunnel_offload_info *info,
53 struct ipv4_hdr *ipv4_hdr;
54 struct ipv6_hdr *ipv6_hdr;
57 info->outer_l2_len = sizeof(struct ether_hdr);
58 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
60 if (ethertype == ETHER_TYPE_VLAN) {
61 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
62 info->outer_l2_len += sizeof(struct vlan_hdr);
63 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
68 ipv4_hdr = (struct ipv4_hdr *)
69 ((char *)eth_hdr + info->outer_l2_len);
70 info->outer_l3_len = sizeof(struct ipv4_hdr);
71 *l4_proto = ipv4_hdr->next_proto_id;
74 ipv6_hdr = (struct ipv6_hdr *)
75 ((char *)eth_hdr + info->outer_l2_len);
76 info->outer_l3_len = sizeof(struct ipv6_hdr);
77 *l4_proto = ipv6_hdr->proto;
80 info->outer_l3_len = 0;
87 decapsulation(struct rte_mbuf *pkt)
90 uint16_t outer_header_len;
91 struct udp_hdr *udp_hdr;
92 union tunnel_offload_info info = { .data = 0 };
93 struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
95 parse_ethernet(phdr, &info, &l4_proto);
96 if (l4_proto != IPPROTO_UDP)
99 udp_hdr = (struct udp_hdr *)((char *)phdr +
100 info.outer_l2_len + info.outer_l3_len);
102 /** check udp destination port, 4789 is the default vxlan port
103 * (rfc7348) or that the rx offload flag is set (i40e only
105 if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
106 (pkt->ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
107 PKT_RX_TUNNEL_IPV6_HDR)) == 0)
109 outer_header_len = info.outer_l2_len + info.outer_l3_len
110 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
112 rte_pktmbuf_adj(pkt, outer_header_len);
118 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
121 uint64_t ol_flags = 0;
122 uint32_t old_len = m->pkt_len, hash;
123 struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
125 /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
126 struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
127 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
128 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
130 struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
131 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
132 struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
134 /* convert TX queue ID to vport ID */
135 vport_id = queue_id - 1;
137 /* replace original Ethernet header with ours */
138 pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
139 sizeof(struct ether_hdr));
141 /* copy in IP header */
142 ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
143 sizeof(struct ipv4_hdr));
144 ip->total_length = rte_cpu_to_be_16(m->data_len
145 - sizeof(struct ether_hdr));
147 /* outer IP checksum */
148 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
149 ip->hdr_checksum = 0;
151 m->outer_l2_len = sizeof(struct ether_hdr);
152 m->outer_l3_len = sizeof(struct ipv4_hdr);
154 m->ol_flags |= ol_flags;
157 vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
158 vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
161 udp->dgram_cksum = 0;
162 udp->dgram_len = rte_cpu_to_be_16(old_len
163 + sizeof(struct udp_hdr)
164 + sizeof(struct vxlan_hdr));
166 udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
167 hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
168 udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)