xref: /f-stack/dpdk/lib/librte_gso/rte_gso.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <errno.h>
6 
7 #include <rte_log.h>
8 #include <rte_ethdev.h>
9 
10 #include "rte_gso.h"
11 #include "gso_common.h"
12 #include "gso_tcp4.h"
13 #include "gso_tunnel_tcp4.h"
14 #include "gso_udp4.h"
15 
16 #define ILLEGAL_UDP_GSO_CTX(ctx) \
17 	((((ctx)->gso_types & DEV_TX_OFFLOAD_UDP_TSO) == 0) || \
18 	 (ctx)->gso_size < RTE_GSO_UDP_SEG_SIZE_MIN)
19 
20 #define ILLEGAL_TCP_GSO_CTX(ctx) \
21 	((((ctx)->gso_types & (DEV_TX_OFFLOAD_TCP_TSO | \
22 		DEV_TX_OFFLOAD_VXLAN_TNL_TSO | \
23 		DEV_TX_OFFLOAD_GRE_TNL_TSO)) == 0) || \
24 		(ctx)->gso_size < RTE_GSO_SEG_SIZE_MIN)
25 
26 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)27 rte_gso_segment(struct rte_mbuf *pkt,
28 		const struct rte_gso_ctx *gso_ctx,
29 		struct rte_mbuf **pkts_out,
30 		uint16_t nb_pkts_out)
31 {
32 	struct rte_mempool *direct_pool, *indirect_pool;
33 	uint64_t ol_flags;
34 	uint16_t gso_size;
35 	uint8_t ipid_delta;
36 	int ret = 1;
37 
38 	if (pkt == NULL || pkts_out == NULL || gso_ctx == NULL ||
39 			nb_pkts_out < 1 ||
40 			(ILLEGAL_UDP_GSO_CTX(gso_ctx) &&
41 			 ILLEGAL_TCP_GSO_CTX(gso_ctx)))
42 		return -EINVAL;
43 
44 	if (gso_ctx->gso_size >= pkt->pkt_len) {
45 		pkt->ol_flags &= (~(PKT_TX_TCP_SEG | PKT_TX_UDP_SEG));
46 		return 0;
47 	}
48 
49 	direct_pool = gso_ctx->direct_pool;
50 	indirect_pool = gso_ctx->indirect_pool;
51 	gso_size = gso_ctx->gso_size;
52 	ipid_delta = (gso_ctx->flag != RTE_GSO_FLAG_IPID_FIXED);
53 	ol_flags = pkt->ol_flags;
54 
55 	if ((IS_IPV4_VXLAN_TCP4(pkt->ol_flags) &&
56 			(gso_ctx->gso_types & DEV_TX_OFFLOAD_VXLAN_TNL_TSO)) ||
57 			((IS_IPV4_GRE_TCP4(pkt->ol_flags) &&
58 			 (gso_ctx->gso_types & DEV_TX_OFFLOAD_GRE_TNL_TSO)))) {
59 		pkt->ol_flags &= (~PKT_TX_TCP_SEG);
60 		ret = gso_tunnel_tcp4_segment(pkt, gso_size, ipid_delta,
61 				direct_pool, indirect_pool,
62 				pkts_out, nb_pkts_out);
63 	} else if (IS_IPV4_TCP(pkt->ol_flags) &&
64 			(gso_ctx->gso_types & DEV_TX_OFFLOAD_TCP_TSO)) {
65 		pkt->ol_flags &= (~PKT_TX_TCP_SEG);
66 		ret = gso_tcp4_segment(pkt, gso_size, ipid_delta,
67 				direct_pool, indirect_pool,
68 				pkts_out, nb_pkts_out);
69 	} else if (IS_IPV4_UDP(pkt->ol_flags) &&
70 			(gso_ctx->gso_types & DEV_TX_OFFLOAD_UDP_TSO)) {
71 		pkt->ol_flags &= (~PKT_TX_UDP_SEG);
72 		ret = gso_udp4_segment(pkt, gso_size, direct_pool,
73 				indirect_pool, pkts_out, nb_pkts_out);
74 	} else {
75 		/* unsupported packet, skip */
76 		RTE_LOG(DEBUG, GSO, "Unsupported packet type\n");
77 		ret = 0;
78 	}
79 
80 	if (ret < 0) {
81 		/* Revert the ol_flags in the event of failure. */
82 		pkt->ol_flags = ol_flags;
83 	}
84 
85 	return ret;
86 }
87