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