xref: /dpdk/lib/gso/rte_gso.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_GSO_H_
699a2dd95SBruce Richardson #define _RTE_GSO_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  * Interface to GSO library
1199a2dd95SBruce Richardson  */
1299a2dd95SBruce Richardson 
13*719834a6SMattias Rönnblom #include <stdint.h>
14*719834a6SMattias Rönnblom #include <rte_mbuf.h>
15*719834a6SMattias Rönnblom 
1699a2dd95SBruce Richardson #ifdef __cplusplus
1799a2dd95SBruce Richardson extern "C" {
1899a2dd95SBruce Richardson #endif
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson /* Minimum GSO segment size for TCP based packets. */
2199a2dd95SBruce Richardson #define RTE_GSO_SEG_SIZE_MIN (sizeof(struct rte_ether_hdr) + \
2299a2dd95SBruce Richardson 		sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_tcp_hdr) + 1)
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson /* Minimum GSO segment size for UDP based packets. */
2599a2dd95SBruce Richardson #define RTE_GSO_UDP_SEG_SIZE_MIN (sizeof(struct rte_ether_hdr) + \
2699a2dd95SBruce Richardson 		sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_udp_hdr) + 1)
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /* GSO flags for rte_gso_ctx. */
2999a2dd95SBruce Richardson #define RTE_GSO_FLAG_IPID_FIXED (1ULL << 0)
3099a2dd95SBruce Richardson /**< Use fixed IP ids for output GSO segments. Setting
3199a2dd95SBruce Richardson  * 0 indicates using incremental IP ids.
3299a2dd95SBruce Richardson  */
3399a2dd95SBruce Richardson 
3499a2dd95SBruce Richardson /**
3599a2dd95SBruce Richardson  * GSO context structure.
3699a2dd95SBruce Richardson  */
3799a2dd95SBruce Richardson struct rte_gso_ctx {
3899a2dd95SBruce Richardson 	struct rte_mempool *direct_pool;
3999a2dd95SBruce Richardson 	/**< MBUF pool for allocating direct buffers, which are used
4099a2dd95SBruce Richardson 	 * to store packet headers for GSO segments.
4199a2dd95SBruce Richardson 	 */
4299a2dd95SBruce Richardson 	struct rte_mempool *indirect_pool;
4399a2dd95SBruce Richardson 	/**< MBUF pool for allocating indirect buffers, which are used
4499a2dd95SBruce Richardson 	 * to locate packet payloads for GSO segments. The indirect
4599a2dd95SBruce Richardson 	 * buffer doesn't contain any data, but simply points to an
4699a2dd95SBruce Richardson 	 * offset within the packet to segment.
4799a2dd95SBruce Richardson 	 */
4899a2dd95SBruce Richardson 	uint64_t flag;
4999a2dd95SBruce Richardson 	/**< flag that controls specific attributes of output segments,
5099a2dd95SBruce Richardson 	 * such as the type of IP ID generated (i.e. fixed or incremental).
5199a2dd95SBruce Richardson 	 */
5299a2dd95SBruce Richardson 	uint32_t gso_types;
5399a2dd95SBruce Richardson 	/**< the bit mask of required GSO types. The GSO library
5499a2dd95SBruce Richardson 	 * uses the same macros as that of describing device TX
55295968d1SFerruh Yigit 	 * offloading capabilities (i.e. RTE_ETH_TX_OFFLOAD_*_TSO) for
5699a2dd95SBruce Richardson 	 * gso_types.
5799a2dd95SBruce Richardson 	 *
5899a2dd95SBruce Richardson 	 * For example, if applications want to segment TCP/IPv4
59295968d1SFerruh Yigit 	 * packets, set RTE_ETH_TX_OFFLOAD_TCP_TSO in gso_types.
6099a2dd95SBruce Richardson 	 */
6199a2dd95SBruce Richardson 	uint16_t gso_size;
6299a2dd95SBruce Richardson 	/**< maximum size of an output GSO segment, including packet
6399a2dd95SBruce Richardson 	 * header and payload, measured in bytes. Must exceed
6499a2dd95SBruce Richardson 	 * RTE_GSO_SEG_SIZE_MIN.
6599a2dd95SBruce Richardson 	 */
6699a2dd95SBruce Richardson };
6799a2dd95SBruce Richardson 
6899a2dd95SBruce Richardson /**
6999a2dd95SBruce Richardson  * Segmentation function, which supports processing of both single- and
7099a2dd95SBruce Richardson  * multi- MBUF packets.
7199a2dd95SBruce Richardson  *
7299a2dd95SBruce Richardson  * Note that we refer to the packets that are segmented from the input
7399a2dd95SBruce Richardson  * packet as 'GSO segments'. rte_gso_segment() doesn't check if the
7499a2dd95SBruce Richardson  * input packet has correct checksums, and doesn't update checksums for
7599a2dd95SBruce Richardson  * output GSO segments. Additionally, it doesn't process IP fragment
7699a2dd95SBruce Richardson  * packets.
7799a2dd95SBruce Richardson  *
7899a2dd95SBruce Richardson  * Before calling rte_gso_segment(), applications must set proper ol_flags
7999a2dd95SBruce Richardson  * for the packet. The GSO library uses the same macros as that of TSO.
80daa02b5cSOlivier Matz  * For example, set RTE_MBUF_F_TX_TCP_SEG and RTE_MBUF_F_TX_IPV4 in ol_flags to segment
81daa02b5cSOlivier Matz  * a TCP/IPv4 packet. If rte_gso_segment() succeeds, the RTE_MBUF_F_TX_TCP_SEG
8299a2dd95SBruce Richardson  * flag is removed for all GSO segments and the input packet.
8399a2dd95SBruce Richardson  *
8499a2dd95SBruce Richardson  * Each of the newly-created GSO segments is organized as a two-segment
8599a2dd95SBruce Richardson  * MBUF, where the first segment is a standard MBUF, which stores a copy
8699a2dd95SBruce Richardson  * of packet header, and the second is an indirect MBUF which points to
8799a2dd95SBruce Richardson  * a section of data in the input packet. Since each GSO segment has
8899a2dd95SBruce Richardson  * multiple MBUFs (i.e. typically 2 MBUFs), the driver of the interface which
8999a2dd95SBruce Richardson  * the GSO segments are sent to should support transmission of multi-segment
9099a2dd95SBruce Richardson  * packets.
9199a2dd95SBruce Richardson  *
9299a2dd95SBruce Richardson  * If the input packet is GSO'd, all the indirect segments are attached to the
9399a2dd95SBruce Richardson  * input packet.
9499a2dd95SBruce Richardson  *
9599a2dd95SBruce Richardson  * rte_gso_segment() will not free the input packet no matter whether it is
9699a2dd95SBruce Richardson  * GSO'd or not, the application should free it after calling rte_gso_segment().
9799a2dd95SBruce Richardson  *
9899a2dd95SBruce Richardson  * If the memory space in pkts_out or MBUF pools is insufficient, this
9999a2dd95SBruce Richardson  * function fails, and it returns (-1) * errno. Otherwise, GSO succeeds,
10099a2dd95SBruce Richardson  * and this function returns the number of output GSO segments filled in
10199a2dd95SBruce Richardson  * pkts_out.
10299a2dd95SBruce Richardson  *
10399a2dd95SBruce Richardson  * @param pkt
10499a2dd95SBruce Richardson  *  The packet mbuf to segment.
10599a2dd95SBruce Richardson  * @param ctx
10699a2dd95SBruce Richardson  *  GSO context object pointer.
10799a2dd95SBruce Richardson  * @param pkts_out
10899a2dd95SBruce Richardson  *  Pointer array used to store the MBUF addresses of output GSO
10999a2dd95SBruce Richardson  *  segments, when rte_gso_segment() succeeds.
11099a2dd95SBruce Richardson  * @param nb_pkts_out
11199a2dd95SBruce Richardson  *  The max number of items that pkts_out can keep.
11299a2dd95SBruce Richardson  *
11399a2dd95SBruce Richardson  * @return
11499a2dd95SBruce Richardson  *  - The number of GSO segments filled in pkts_out on success.
11599a2dd95SBruce Richardson  *  - Return 0 if it does not need to be GSO'd.
11699a2dd95SBruce Richardson  *  - Return -ENOMEM if run out of memory in MBUF pools.
11717a2bf47SStephen Hemminger  *  - Return -ENOTSUP for protocols that can not be segmented.
11899a2dd95SBruce Richardson  *  - Return -EINVAL for invalid parameters.
11999a2dd95SBruce Richardson  */
12099a2dd95SBruce Richardson int rte_gso_segment(struct rte_mbuf *pkt,
12199a2dd95SBruce Richardson 		const struct rte_gso_ctx *ctx,
12299a2dd95SBruce Richardson 		struct rte_mbuf **pkts_out,
12399a2dd95SBruce Richardson 		uint16_t nb_pkts_out);
12499a2dd95SBruce Richardson #ifdef __cplusplus
12599a2dd95SBruce Richardson }
12699a2dd95SBruce Richardson #endif
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson #endif /* _RTE_GSO_H_ */
129