build: use static deps for pkg-config libs.private
[dpdk.git] / lib / librte_gso / rte_gso.c
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
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         struct rte_mbuf *pkt_seg;
34         uint64_t ol_flags;
35         uint16_t gso_size;
36         uint8_t ipid_delta;
37         int ret = 1;
38
39         if (pkt == NULL || pkts_out == NULL || gso_ctx == NULL ||
40                         nb_pkts_out < 1 ||
41                         (ILLEGAL_UDP_GSO_CTX(gso_ctx) &&
42                          ILLEGAL_TCP_GSO_CTX(gso_ctx)))
43                 return -EINVAL;
44
45         if (gso_ctx->gso_size >= pkt->pkt_len) {
46                 pkt->ol_flags &= (~(PKT_TX_TCP_SEG | PKT_TX_UDP_SEG));
47                 pkts_out[0] = pkt;
48                 return 1;
49         }
50
51         direct_pool = gso_ctx->direct_pool;
52         indirect_pool = gso_ctx->indirect_pool;
53         gso_size = gso_ctx->gso_size;
54         ipid_delta = (gso_ctx->flag != RTE_GSO_FLAG_IPID_FIXED);
55         ol_flags = pkt->ol_flags;
56
57         if ((IS_IPV4_VXLAN_TCP4(pkt->ol_flags) &&
58                         (gso_ctx->gso_types & DEV_TX_OFFLOAD_VXLAN_TNL_TSO)) ||
59                         ((IS_IPV4_GRE_TCP4(pkt->ol_flags) &&
60                          (gso_ctx->gso_types & DEV_TX_OFFLOAD_GRE_TNL_TSO)))) {
61                 pkt->ol_flags &= (~PKT_TX_TCP_SEG);
62                 ret = gso_tunnel_tcp4_segment(pkt, gso_size, ipid_delta,
63                                 direct_pool, indirect_pool,
64                                 pkts_out, nb_pkts_out);
65         } else if (IS_IPV4_TCP(pkt->ol_flags) &&
66                         (gso_ctx->gso_types & DEV_TX_OFFLOAD_TCP_TSO)) {
67                 pkt->ol_flags &= (~PKT_TX_TCP_SEG);
68                 ret = gso_tcp4_segment(pkt, gso_size, ipid_delta,
69                                 direct_pool, indirect_pool,
70                                 pkts_out, nb_pkts_out);
71         } else if (IS_IPV4_UDP(pkt->ol_flags) &&
72                         (gso_ctx->gso_types & DEV_TX_OFFLOAD_UDP_TSO)) {
73                 pkt->ol_flags &= (~PKT_TX_UDP_SEG);
74                 ret = gso_udp4_segment(pkt, gso_size, direct_pool,
75                                 indirect_pool, pkts_out, nb_pkts_out);
76         } else {
77                 /* unsupported packet, skip */
78                 pkts_out[0] = pkt;
79                 RTE_LOG(DEBUG, GSO, "Unsupported packet type\n");
80                 return 1;
81         }
82
83         if (ret > 1) {
84                 pkt_seg = pkt;
85                 while (pkt_seg) {
86                         rte_mbuf_refcnt_update(pkt_seg, -1);
87                         pkt_seg = pkt_seg->next;
88                 }
89         } else if (ret < 0) {
90                 /* Revert the ol_flags in the event of failure. */
91                 pkt->ol_flags = ol_flags;
92         }
93
94         return ret;
95 }