8d0cfd7ad7e4e456095b189e013daef338c988fd
[deb_dpdk.git] / lib / librte_gso / gso_tunnel_tcp4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "gso_common.h"
35 #include "gso_tunnel_tcp4.h"
36
37 static void
38 update_tunnel_ipv4_tcp_headers(struct rte_mbuf *pkt, uint8_t ipid_delta,
39                 struct rte_mbuf **segs, uint16_t nb_segs)
40 {
41         struct ipv4_hdr *ipv4_hdr;
42         struct tcp_hdr *tcp_hdr;
43         uint32_t sent_seq;
44         uint16_t outer_id, inner_id, tail_idx, i;
45         uint16_t outer_ipv4_offset, inner_ipv4_offset;
46         uint16_t udp_gre_offset, tcp_offset;
47         uint8_t update_udp_hdr;
48
49         outer_ipv4_offset = pkt->outer_l2_len;
50         udp_gre_offset = outer_ipv4_offset + pkt->outer_l3_len;
51         inner_ipv4_offset = udp_gre_offset + pkt->l2_len;
52         tcp_offset = inner_ipv4_offset + pkt->l3_len;
53
54         /* Outer IPv4 header. */
55         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
56                         outer_ipv4_offset);
57         outer_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
58
59         /* Inner IPv4 header. */
60         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
61                         inner_ipv4_offset);
62         inner_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
63
64         tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + pkt->l3_len);
65         sent_seq = rte_be_to_cpu_32(tcp_hdr->sent_seq);
66         tail_idx = nb_segs - 1;
67
68         /* Only update UDP header for VxLAN packets. */
69         update_udp_hdr = (pkt->ol_flags & PKT_TX_TUNNEL_VXLAN) ? 1 : 0;
70
71         for (i = 0; i < nb_segs; i++) {
72                 update_ipv4_header(segs[i], outer_ipv4_offset, outer_id);
73                 if (update_udp_hdr)
74                         update_udp_header(segs[i], udp_gre_offset);
75                 update_ipv4_header(segs[i], inner_ipv4_offset, inner_id);
76                 update_tcp_header(segs[i], tcp_offset, sent_seq, i < tail_idx);
77                 outer_id++;
78                 inner_id += ipid_delta;
79                 sent_seq += (segs[i]->pkt_len - segs[i]->data_len);
80         }
81 }
82
83 int
84 gso_tunnel_tcp4_segment(struct rte_mbuf *pkt,
85                 uint16_t gso_size,
86                 uint8_t ipid_delta,
87                 struct rte_mempool *direct_pool,
88                 struct rte_mempool *indirect_pool,
89                 struct rte_mbuf **pkts_out,
90                 uint16_t nb_pkts_out)
91 {
92         struct ipv4_hdr *inner_ipv4_hdr;
93         uint16_t pyld_unit_size, hdr_offset, frag_off;
94         int ret = 1;
95
96         hdr_offset = pkt->outer_l2_len + pkt->outer_l3_len + pkt->l2_len;
97         inner_ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
98                         hdr_offset);
99         /*
100          * Don't process the packet whose MF bit or offset in the inner
101          * IPv4 header are non-zero.
102          */
103         frag_off = rte_be_to_cpu_16(inner_ipv4_hdr->fragment_offset);
104         if (unlikely(IS_FRAGMENTED(frag_off))) {
105                 pkts_out[0] = pkt;
106                 return 1;
107         }
108
109         hdr_offset += pkt->l3_len + pkt->l4_len;
110         /* Don't process the packet without data */
111         if (hdr_offset >= pkt->pkt_len) {
112                 pkts_out[0] = pkt;
113                 return 1;
114         }
115         pyld_unit_size = gso_size - hdr_offset;
116
117         /* Segment the payload */
118         ret = gso_do_segment(pkt, hdr_offset, pyld_unit_size, direct_pool,
119                         indirect_pool, pkts_out, nb_pkts_out);
120         if (ret <= 1)
121                 return ret;
122
123         update_tunnel_ipv4_tcp_headers(pkt, ipid_delta, pkts_out, ret);
124
125         return ret;
126 }