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 ol_flags |= PKT_TX_TCP_CKSUM;
145 tcp_hdr->cksum = get_psd_sum(l3_hdr, ethertype,
147 if (tso_segsz != 0) {
148 ol_flags |= PKT_TX_TCP_SEG;
149 info->tso_segsz = tso_segsz;
150 info->l4_len = sizeof(struct tcp_hdr);
153 } else if (l4_proto == IPPROTO_SCTP) {
154 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
156 ol_flags |= PKT_TX_SCTP_CKSUM;
163 decapsulation(struct rte_mbuf *pkt)
165 uint8_t l4_proto = 0;
166 uint16_t outer_header_len;
167 struct udp_hdr *udp_hdr;
168 union tunnel_offload_info info = { .data = 0 };
169 struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
171 parse_ethernet(phdr, &info, &l4_proto);
173 if (l4_proto != IPPROTO_UDP)
176 udp_hdr = (struct udp_hdr *)((char *)phdr +
177 info.outer_l2_len + info.outer_l3_len);
179 /** check udp destination port, 4789 is the default vxlan port
180 * (rfc7348) or that the rx offload flag is set (i40e only
182 if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
183 (pkt->packet_type & RTE_PTYPE_TUNNEL_MASK) == 0)
185 outer_header_len = info.outer_l2_len + info.outer_l3_len
186 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
188 rte_pktmbuf_adj(pkt, outer_header_len);
194 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
197 uint64_t ol_flags = 0;
198 uint32_t old_len = m->pkt_len, hash;
199 union tunnel_offload_info tx_offload = { .data = 0 };
200 struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
202 /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
203 struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
204 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
205 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
207 struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
208 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
209 struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
211 /* convert TX queue ID to vport ID */
212 vport_id = queue_id - 1;
214 /* replace original Ethernet header with ours */
215 pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
216 sizeof(struct ether_hdr));
218 /* copy in IP header */
219 ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
220 sizeof(struct ipv4_hdr));
221 ip->total_length = rte_cpu_to_be_16(m->data_len
222 - sizeof(struct ether_hdr));
224 /* outer IP checksum */
225 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
226 ip->hdr_checksum = 0;
228 /* inner IP checksum offload */
230 ol_flags |= process_inner_cksums(phdr, &tx_offload);
231 m->l2_len = tx_offload.l2_len;
232 m->l3_len = tx_offload.l3_len;
233 m->l4_len = tx_offload.l4_len;
234 m->l2_len += ETHER_VXLAN_HLEN;
237 m->outer_l2_len = sizeof(struct ether_hdr);
238 m->outer_l3_len = sizeof(struct ipv4_hdr);
240 m->ol_flags |= ol_flags;
241 m->tso_segsz = tx_offload.tso_segsz;
244 vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
245 vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
248 udp->dgram_cksum = 0;
249 udp->dgram_len = rte_cpu_to_be_16(old_len
250 + sizeof(struct udp_hdr)
251 + sizeof(struct vxlan_hdr));
253 udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
254 hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
255 udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)