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 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
47 if (ethertype == ETHER_TYPE_IPv4)
48 return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
49 else /* assume ethertype == ETHER_TYPE_IPv6 */
50 return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
54 * Parse an ethernet header to fill the ethertype, outer_l2_len, outer_l3_len and
55 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
59 parse_ethernet(struct ether_hdr *eth_hdr, union tunnel_offload_info *info,
62 struct ipv4_hdr *ipv4_hdr;
63 struct ipv6_hdr *ipv6_hdr;
66 info->outer_l2_len = sizeof(struct ether_hdr);
67 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
69 if (ethertype == ETHER_TYPE_VLAN) {
70 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
71 info->outer_l2_len += sizeof(struct vlan_hdr);
72 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
77 ipv4_hdr = (struct ipv4_hdr *)
78 ((char *)eth_hdr + info->outer_l2_len);
79 info->outer_l3_len = sizeof(struct ipv4_hdr);
80 *l4_proto = ipv4_hdr->next_proto_id;
83 ipv6_hdr = (struct ipv6_hdr *)
84 ((char *)eth_hdr + info->outer_l2_len);
85 info->outer_l3_len = sizeof(struct ipv6_hdr);
86 *l4_proto = ipv6_hdr->proto;
89 info->outer_l3_len = 0;
96 * Calculate the checksum of a packet in hardware
99 process_inner_cksums(struct ether_hdr *eth_hdr, union tunnel_offload_info *info)
104 struct ipv4_hdr *ipv4_hdr;
105 struct ipv6_hdr *ipv6_hdr;
106 struct udp_hdr *udp_hdr;
107 struct tcp_hdr *tcp_hdr;
108 struct sctp_hdr *sctp_hdr;
109 uint64_t ol_flags = 0;
111 info->l2_len = sizeof(struct ether_hdr);
112 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
114 if (ethertype == ETHER_TYPE_VLAN) {
115 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
116 info->l2_len += sizeof(struct vlan_hdr);
117 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
120 l3_hdr = (char *)eth_hdr + info->l2_len;
122 if (ethertype == ETHER_TYPE_IPv4) {
123 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
124 ipv4_hdr->hdr_checksum = 0;
125 ol_flags |= PKT_TX_IPV4;
126 ol_flags |= PKT_TX_IP_CKSUM;
127 info->l3_len = sizeof(struct ipv4_hdr);
128 l4_proto = ipv4_hdr->next_proto_id;
129 } else if (ethertype == ETHER_TYPE_IPv6) {
130 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
131 info->l3_len = sizeof(struct ipv6_hdr);
132 l4_proto = ipv6_hdr->proto;
133 ol_flags |= PKT_TX_IPV6;
135 return 0; /* packet type not supported, nothing to do */
137 if (l4_proto == IPPROTO_UDP) {
138 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
139 ol_flags |= PKT_TX_UDP_CKSUM;
140 udp_hdr->dgram_cksum = get_psd_sum(l3_hdr,
141 ethertype, ol_flags);
142 } else if (l4_proto == IPPROTO_TCP) {
143 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
144 /* Put PKT_TX_TCP_SEG bit setting before get_psd_sum(), because
145 * it depends on PKT_TX_TCP_SEG to calculate pseudo-header
148 if (tso_segsz != 0) {
149 ol_flags |= PKT_TX_TCP_SEG;
150 info->tso_segsz = tso_segsz;
151 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
153 ol_flags |= PKT_TX_TCP_CKSUM;
154 tcp_hdr->cksum = get_psd_sum(l3_hdr, ethertype, ol_flags);
156 } else if (l4_proto == IPPROTO_SCTP) {
157 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
159 ol_flags |= PKT_TX_SCTP_CKSUM;
166 decapsulation(struct rte_mbuf *pkt)
168 uint8_t l4_proto = 0;
169 uint16_t outer_header_len;
170 struct udp_hdr *udp_hdr;
171 union tunnel_offload_info info = { .data = 0 };
172 struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
174 parse_ethernet(phdr, &info, &l4_proto);
176 if (l4_proto != IPPROTO_UDP)
179 udp_hdr = (struct udp_hdr *)((char *)phdr +
180 info.outer_l2_len + info.outer_l3_len);
182 /** check udp destination port, 4789 is the default vxlan port
183 * (rfc7348) or that the rx offload flag is set (i40e only
185 if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
186 (pkt->packet_type & RTE_PTYPE_TUNNEL_MASK) == 0)
188 outer_header_len = info.outer_l2_len + info.outer_l3_len
189 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
191 rte_pktmbuf_adj(pkt, outer_header_len);
197 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
200 uint64_t ol_flags = 0;
201 uint32_t old_len = m->pkt_len, hash;
202 union tunnel_offload_info tx_offload = { .data = 0 };
203 struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
205 /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
206 struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
207 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
208 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
210 struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
211 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
212 struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
214 /* convert TX queue ID to vport ID */
215 vport_id = queue_id - 1;
217 /* replace original Ethernet header with ours */
218 pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
219 sizeof(struct ether_hdr));
221 /* copy in IP header */
222 ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
223 sizeof(struct ipv4_hdr));
224 ip->total_length = rte_cpu_to_be_16(m->pkt_len
225 - sizeof(struct ether_hdr));
227 /* outer IP checksum */
228 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
229 ip->hdr_checksum = 0;
231 /* inner IP checksum offload */
233 ol_flags |= process_inner_cksums(phdr, &tx_offload);
234 m->l2_len = tx_offload.l2_len;
235 m->l3_len = tx_offload.l3_len;
236 m->l4_len = tx_offload.l4_len;
237 m->l2_len += ETHER_VXLAN_HLEN;
240 m->outer_l2_len = sizeof(struct ether_hdr);
241 m->outer_l3_len = sizeof(struct ipv4_hdr);
243 ol_flags |= PKT_TX_TUNNEL_VXLAN;
245 m->ol_flags |= ol_flags;
246 m->tso_segsz = tx_offload.tso_segsz;
249 vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
250 vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
253 udp->dgram_cksum = 0;
254 udp->dgram_len = rte_cpu_to_be_16(old_len
255 + sizeof(struct udp_hdr)
256 + sizeof(struct vxlan_hdr));
258 udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
259 hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
260 udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)