lib/gro: add Generic Receive Offload API framework
[dpdk.git] / lib / librte_gro / rte_gro.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_malloc.h>
34 #include <rte_mbuf.h>
35
36 #include "rte_gro.h"
37
38 typedef void *(*gro_tbl_create_fn)(uint16_t socket_id,
39                 uint16_t max_flow_num,
40                 uint16_t max_item_per_flow);
41 typedef void (*gro_tbl_destroy_fn)(void *tbl);
42 typedef uint32_t (*gro_tbl_pkt_count_fn)(void *tbl);
43
44 static gro_tbl_create_fn tbl_create_fn[RTE_GRO_TYPE_MAX_NUM];
45 static gro_tbl_destroy_fn tbl_destroy_fn[RTE_GRO_TYPE_MAX_NUM];
46 static gro_tbl_pkt_count_fn tbl_pkt_count_fn[RTE_GRO_TYPE_MAX_NUM];
47
48 /*
49  * GRO context structure, which is used to merge packets. It keeps
50  * many reassembly tables of desired GRO types. Applications need to
51  * create GRO context objects before using rte_gro_reassemble to
52  * perform GRO.
53  */
54 struct gro_ctx {
55         /* GRO types to perform */
56         uint64_t gro_types;
57         /* reassembly tables */
58         void *tbls[RTE_GRO_TYPE_MAX_NUM];
59 };
60
61 void *
62 rte_gro_ctx_create(const struct rte_gro_param *param)
63 {
64         struct gro_ctx *gro_ctx;
65         gro_tbl_create_fn create_tbl_fn;
66         uint64_t gro_type_flag = 0;
67         uint64_t gro_types = 0;
68         uint8_t i;
69
70         gro_ctx = rte_zmalloc_socket(__func__,
71                         sizeof(struct gro_ctx),
72                         RTE_CACHE_LINE_SIZE,
73                         param->socket_id);
74         if (gro_ctx == NULL)
75                 return NULL;
76
77         for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
78                 gro_type_flag = 1 << i;
79                 if ((param->gro_types & gro_type_flag) == 0)
80                         continue;
81
82                 create_tbl_fn = tbl_create_fn[i];
83                 if (create_tbl_fn == NULL)
84                         continue;
85
86                 gro_ctx->tbls[i] = create_tbl_fn(param->socket_id,
87                                 param->max_flow_num,
88                                 param->max_item_per_flow);
89                 if (gro_ctx->tbls[i] == NULL) {
90                         /* destroy all created tables */
91                         gro_ctx->gro_types = gro_types;
92                         rte_gro_ctx_destroy(gro_ctx);
93                         return NULL;
94                 }
95                 gro_types |= gro_type_flag;
96         }
97         gro_ctx->gro_types = param->gro_types;
98
99         return gro_ctx;
100 }
101
102 void
103 rte_gro_ctx_destroy(void *ctx)
104 {
105         gro_tbl_destroy_fn destroy_tbl_fn;
106         struct gro_ctx *gro_ctx = ctx;
107         uint64_t gro_type_flag;
108         uint8_t i;
109
110         if (gro_ctx == NULL)
111                 return;
112         for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
113                 gro_type_flag = 1 << i;
114                 if ((gro_ctx->gro_types & gro_type_flag) == 0)
115                         continue;
116                 destroy_tbl_fn = tbl_destroy_fn[i];
117                 if (destroy_tbl_fn)
118                         destroy_tbl_fn(gro_ctx->tbls[i]);
119         }
120         rte_free(gro_ctx);
121 }
122
123 uint16_t
124 rte_gro_reassemble_burst(struct rte_mbuf **pkts __rte_unused,
125                 uint16_t nb_pkts,
126                 const struct rte_gro_param *param __rte_unused)
127 {
128         return nb_pkts;
129 }
130
131 uint16_t
132 rte_gro_reassemble(struct rte_mbuf **pkts __rte_unused,
133                 uint16_t nb_pkts,
134                 void *ctx __rte_unused)
135 {
136         return nb_pkts;
137 }
138
139 uint16_t
140 rte_gro_timeout_flush(void *ctx __rte_unused,
141                 uint64_t timeout_cycles __rte_unused,
142                 uint64_t gro_types __rte_unused,
143                 struct rte_mbuf **out __rte_unused,
144                 uint16_t max_nb_out __rte_unused)
145 {
146         return 0;
147 }
148
149 uint64_t
150 rte_gro_get_pkt_count(void *ctx)
151 {
152         struct gro_ctx *gro_ctx = ctx;
153         gro_tbl_pkt_count_fn pkt_count_fn;
154         uint64_t item_num = 0;
155         uint64_t gro_type_flag;
156         uint8_t i;
157
158         for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
159                 gro_type_flag = 1 << i;
160                 if ((gro_ctx->gro_types & gro_type_flag) == 0)
161                         continue;
162
163                 pkt_count_fn = tbl_pkt_count_fn[i];
164                 if (pkt_count_fn == NULL)
165                         continue;
166                 item_num += pkt_count_fn(gro_ctx->tbls[i]);
167         }
168         return item_num;
169 }