gso: support TCP/IPv4 GSO
[dpdk.git] / lib / librte_gso / gso_common.h
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 #ifndef _GSO_COMMON_H_
35 #define _GSO_COMMON_H_
36
37 #include <stdint.h>
38
39 #include <rte_mbuf.h>
40 #include <rte_ip.h>
41 #include <rte_tcp.h>
42
43 #define IS_FRAGMENTED(frag_off) (((frag_off) & IPV4_HDR_OFFSET_MASK) != 0 \
44                 || ((frag_off) & IPV4_HDR_MF_FLAG) == IPV4_HDR_MF_FLAG)
45
46 #define TCP_HDR_PSH_MASK ((uint8_t)0x08)
47 #define TCP_HDR_FIN_MASK ((uint8_t)0x01)
48
49 #define IS_IPV4_TCP(flag) (((flag) & (PKT_TX_TCP_SEG | PKT_TX_IPV4)) == \
50                 (PKT_TX_TCP_SEG | PKT_TX_IPV4))
51
52 /**
53  * Internal function which updates the TCP header of a packet, following
54  * segmentation. This is required to update the header's 'sent' sequence
55  * number, and also to clear 'PSH' and 'FIN' flags for non-tail segments.
56  *
57  * @param pkt
58  *  The packet containing the TCP header.
59  * @param l4_offset
60  *  The offset of the TCP header from the start of the packet.
61  * @param sent_seq
62  *  The sent sequence number.
63  * @param non-tail
64  *  Indicates whether or not this is a tail segment.
65  */
66 static inline void
67 update_tcp_header(struct rte_mbuf *pkt, uint16_t l4_offset, uint32_t sent_seq,
68                 uint8_t non_tail)
69 {
70         struct tcp_hdr *tcp_hdr;
71
72         tcp_hdr = (struct tcp_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
73                         l4_offset);
74         tcp_hdr->sent_seq = rte_cpu_to_be_32(sent_seq);
75         if (likely(non_tail))
76                 tcp_hdr->tcp_flags &= (~(TCP_HDR_PSH_MASK |
77                                         TCP_HDR_FIN_MASK));
78 }
79
80 /**
81  * Internal function which updates the IPv4 header of a packet, following
82  * segmentation. This is required to update the header's 'total_length' field,
83  * to reflect the reduced length of the now-segmented packet. Furthermore, the
84  * header's 'packet_id' field must be updated to reflect the new ID of the
85  * now-segmented packet.
86  *
87  * @param pkt
88  *  The packet containing the IPv4 header.
89  * @param l3_offset
90  *  The offset of the IPv4 header from the start of the packet.
91  * @param id
92  *  The new ID of the packet.
93  */
94 static inline void
95 update_ipv4_header(struct rte_mbuf *pkt, uint16_t l3_offset, uint16_t id)
96 {
97         struct ipv4_hdr *ipv4_hdr;
98
99         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
100                         l3_offset);
101         ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len - l3_offset);
102         ipv4_hdr->packet_id = rte_cpu_to_be_16(id);
103 }
104
105 /**
106  * Internal function which divides the input packet into small segments.
107  * Each of the newly-created segments is organized as a two-segment MBUF,
108  * where the first segment is a standard mbuf, which stores a copy of
109  * packet header, and the second is an indirect mbuf which points to a
110  * section of data in the input packet.
111  *
112  * @param pkt
113  *  Packet to segment.
114  * @param pkt_hdr_offset
115  *  Packet header offset, measured in bytes.
116  * @param pyld_unit_size
117  *  The max payload length of a GSO segment.
118  * @param direct_pool
119  *  MBUF pool used for allocating direct buffers for output segments.
120  * @param indirect_pool
121  *  MBUF pool used for allocating indirect buffers for output segments.
122  * @param pkts_out
123  *  Pointer array used to keep the mbuf addresses of output segments. If
124  *  the memory space in pkts_out is insufficient, gso_do_segment() fails
125  *  and returns -EINVAL.
126  * @param nb_pkts_out
127  *  The max number of items that pkts_out can keep.
128  *
129  * @return
130  *  - The number of segments created in the event of success.
131  *  - Return -ENOMEM if run out of memory in MBUF pools.
132  *  - Return -EINVAL for invalid parameters.
133  */
134 int gso_do_segment(struct rte_mbuf *pkt,
135                 uint16_t pkt_hdr_offset,
136                 uint16_t pyld_unit_size,
137                 struct rte_mempool *direct_pool,
138                 struct rte_mempool *indirect_pool,
139                 struct rte_mbuf **pkts_out,
140                 uint16_t nb_pkts_out);
141 #endif