xref: /dpdk/lib/gso/rte_gso.c (revision 17a2bf47c6956d765aa3b202771628097a937879)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <errno.h>
699a2dd95SBruce Richardson 
799a2dd95SBruce Richardson #include <rte_log.h>
899a2dd95SBruce Richardson #include <rte_ethdev.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include "rte_gso.h"
1199a2dd95SBruce Richardson #include "gso_common.h"
1299a2dd95SBruce Richardson #include "gso_tcp4.h"
1399a2dd95SBruce Richardson #include "gso_tunnel_tcp4.h"
1499a2dd95SBruce Richardson #include "gso_tunnel_udp4.h"
1599a2dd95SBruce Richardson #include "gso_udp4.h"
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson #define ILLEGAL_UDP_GSO_CTX(ctx) \
18295968d1SFerruh Yigit 	((((ctx)->gso_types & RTE_ETH_TX_OFFLOAD_UDP_TSO) == 0) || \
1999a2dd95SBruce Richardson 	 (ctx)->gso_size < RTE_GSO_UDP_SEG_SIZE_MIN)
2099a2dd95SBruce Richardson 
2199a2dd95SBruce Richardson #define ILLEGAL_TCP_GSO_CTX(ctx) \
22295968d1SFerruh Yigit 	((((ctx)->gso_types & (RTE_ETH_TX_OFFLOAD_TCP_TSO | \
23295968d1SFerruh Yigit 		RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO | \
24295968d1SFerruh Yigit 		RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO)) == 0) || \
2599a2dd95SBruce Richardson 		(ctx)->gso_size < RTE_GSO_SEG_SIZE_MIN)
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson int
rte_gso_segment(struct rte_mbuf * pkt,const struct rte_gso_ctx * gso_ctx,struct rte_mbuf ** pkts_out,uint16_t nb_pkts_out)2899a2dd95SBruce Richardson rte_gso_segment(struct rte_mbuf *pkt,
2999a2dd95SBruce Richardson 		const struct rte_gso_ctx *gso_ctx,
3099a2dd95SBruce Richardson 		struct rte_mbuf **pkts_out,
3199a2dd95SBruce Richardson 		uint16_t nb_pkts_out)
3299a2dd95SBruce Richardson {
3399a2dd95SBruce Richardson 	struct rte_mempool *direct_pool, *indirect_pool;
3499a2dd95SBruce Richardson 	uint64_t ol_flags;
3599a2dd95SBruce Richardson 	uint16_t gso_size;
3699a2dd95SBruce Richardson 	uint8_t ipid_delta;
3799a2dd95SBruce Richardson 	int ret = 1;
3899a2dd95SBruce Richardson 
3999a2dd95SBruce Richardson 	if (pkt == NULL || pkts_out == NULL || gso_ctx == NULL ||
4099a2dd95SBruce Richardson 			nb_pkts_out < 1 ||
4199a2dd95SBruce Richardson 			(ILLEGAL_UDP_GSO_CTX(gso_ctx) &&
4299a2dd95SBruce Richardson 			 ILLEGAL_TCP_GSO_CTX(gso_ctx)))
4399a2dd95SBruce Richardson 		return -EINVAL;
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson 	if (gso_ctx->gso_size >= pkt->pkt_len) {
46daa02b5cSOlivier Matz 		pkt->ol_flags &= (~(RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_UDP_SEG));
4799a2dd95SBruce Richardson 		return 0;
4899a2dd95SBruce Richardson 	}
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson 	direct_pool = gso_ctx->direct_pool;
5199a2dd95SBruce Richardson 	indirect_pool = gso_ctx->indirect_pool;
5299a2dd95SBruce Richardson 	gso_size = gso_ctx->gso_size;
5399a2dd95SBruce Richardson 	ipid_delta = (gso_ctx->flag != RTE_GSO_FLAG_IPID_FIXED);
5499a2dd95SBruce Richardson 	ol_flags = pkt->ol_flags;
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson 	if ((IS_IPV4_VXLAN_TCP4(pkt->ol_flags) &&
57295968d1SFerruh Yigit 			(gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO)) ||
5899a2dd95SBruce Richardson 			((IS_IPV4_GRE_TCP4(pkt->ol_flags) &&
59295968d1SFerruh Yigit 			 (gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO)))) {
60daa02b5cSOlivier Matz 		pkt->ol_flags &= (~RTE_MBUF_F_TX_TCP_SEG);
6199a2dd95SBruce Richardson 		ret = gso_tunnel_tcp4_segment(pkt, gso_size, ipid_delta,
6299a2dd95SBruce Richardson 				direct_pool, indirect_pool,
6399a2dd95SBruce Richardson 				pkts_out, nb_pkts_out);
6499a2dd95SBruce Richardson 	} else if (IS_IPV4_VXLAN_UDP4(pkt->ol_flags) &&
65295968d1SFerruh Yigit 			(gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO) &&
66295968d1SFerruh Yigit 			(gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_UDP_TSO)) {
67daa02b5cSOlivier Matz 		pkt->ol_flags &= (~RTE_MBUF_F_TX_UDP_SEG);
6899a2dd95SBruce Richardson 		ret = gso_tunnel_udp4_segment(pkt, gso_size,
6999a2dd95SBruce Richardson 				direct_pool, indirect_pool,
7099a2dd95SBruce Richardson 				pkts_out, nb_pkts_out);
7199a2dd95SBruce Richardson 	} else if (IS_IPV4_TCP(pkt->ol_flags) &&
72295968d1SFerruh Yigit 			(gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_TCP_TSO)) {
73daa02b5cSOlivier Matz 		pkt->ol_flags &= (~RTE_MBUF_F_TX_TCP_SEG);
7499a2dd95SBruce Richardson 		ret = gso_tcp4_segment(pkt, gso_size, ipid_delta,
7599a2dd95SBruce Richardson 				direct_pool, indirect_pool,
7699a2dd95SBruce Richardson 				pkts_out, nb_pkts_out);
7799a2dd95SBruce Richardson 	} else if (IS_IPV4_UDP(pkt->ol_flags) &&
78295968d1SFerruh Yigit 			(gso_ctx->gso_types & RTE_ETH_TX_OFFLOAD_UDP_TSO)) {
79daa02b5cSOlivier Matz 		pkt->ol_flags &= (~RTE_MBUF_F_TX_UDP_SEG);
8099a2dd95SBruce Richardson 		ret = gso_udp4_segment(pkt, gso_size, direct_pool,
8199a2dd95SBruce Richardson 				indirect_pool, pkts_out, nb_pkts_out);
8299a2dd95SBruce Richardson 	} else {
83*17a2bf47SStephen Hemminger 		ret = -ENOTSUP;	/* only UDP or TCP allowed */
8499a2dd95SBruce Richardson 	}
8599a2dd95SBruce Richardson 
8699a2dd95SBruce Richardson 	if (ret < 0) {
8799a2dd95SBruce Richardson 		/* Revert the ol_flags in the event of failure. */
8899a2dd95SBruce Richardson 		pkt->ol_flags = ol_flags;
8999a2dd95SBruce Richardson 	}
9099a2dd95SBruce Richardson 
9199a2dd95SBruce Richardson 	return ret;
9299a2dd95SBruce Richardson }
93