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