4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_malloc.h>
35 #include <rte_cycles.h>
36 #include <rte_ethdev.h>
41 typedef void *(*gro_tbl_create_fn)(uint16_t socket_id,
42 uint16_t max_flow_num,
43 uint16_t max_item_per_flow);
44 typedef void (*gro_tbl_destroy_fn)(void *tbl);
45 typedef uint32_t (*gro_tbl_pkt_count_fn)(void *tbl);
47 static gro_tbl_create_fn tbl_create_fn[RTE_GRO_TYPE_MAX_NUM] = {
48 gro_tcp4_tbl_create, NULL};
49 static gro_tbl_destroy_fn tbl_destroy_fn[RTE_GRO_TYPE_MAX_NUM] = {
50 gro_tcp4_tbl_destroy, NULL};
51 static gro_tbl_pkt_count_fn tbl_pkt_count_fn[RTE_GRO_TYPE_MAX_NUM] = {
52 gro_tcp4_tbl_pkt_count, NULL};
55 * GRO context structure, which is used to merge packets. It keeps
56 * many reassembly tables of desired GRO types. Applications need to
57 * create GRO context objects before using rte_gro_reassemble to
61 /* GRO types to perform */
63 /* reassembly tables */
64 void *tbls[RTE_GRO_TYPE_MAX_NUM];
68 rte_gro_ctx_create(const struct rte_gro_param *param)
70 struct gro_ctx *gro_ctx;
71 gro_tbl_create_fn create_tbl_fn;
72 uint64_t gro_type_flag = 0;
73 uint64_t gro_types = 0;
76 gro_ctx = rte_zmalloc_socket(__func__,
77 sizeof(struct gro_ctx),
83 for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
84 gro_type_flag = 1ULL << i;
85 if ((param->gro_types & gro_type_flag) == 0)
88 create_tbl_fn = tbl_create_fn[i];
89 if (create_tbl_fn == NULL)
92 gro_ctx->tbls[i] = create_tbl_fn(param->socket_id,
94 param->max_item_per_flow);
95 if (gro_ctx->tbls[i] == NULL) {
96 /* destroy all created tables */
97 gro_ctx->gro_types = gro_types;
98 rte_gro_ctx_destroy(gro_ctx);
101 gro_types |= gro_type_flag;
103 gro_ctx->gro_types = param->gro_types;
109 rte_gro_ctx_destroy(void *ctx)
111 gro_tbl_destroy_fn destroy_tbl_fn;
112 struct gro_ctx *gro_ctx = ctx;
113 uint64_t gro_type_flag;
118 for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
119 gro_type_flag = 1ULL << i;
120 if ((gro_ctx->gro_types & gro_type_flag) == 0)
122 destroy_tbl_fn = tbl_destroy_fn[i];
124 destroy_tbl_fn(gro_ctx->tbls[i]);
130 rte_gro_reassemble_burst(struct rte_mbuf **pkts,
132 const struct rte_gro_param *param)
135 uint16_t nb_after_gro = nb_pkts;
138 /* allocate a reassembly table for TCP/IPv4 GRO */
139 struct gro_tcp4_tbl tcp_tbl;
140 struct gro_tcp4_key tcp_keys[RTE_GRO_MAX_BURST_ITEM_NUM];
141 struct gro_tcp4_item tcp_items[RTE_GRO_MAX_BURST_ITEM_NUM] = {{0} };
143 struct rte_mbuf *unprocess_pkts[nb_pkts];
144 uint16_t unprocess_num = 0;
146 uint64_t current_time;
148 if ((param->gro_types & RTE_GRO_TCP_IPV4) == 0)
151 /* get the actual number of packets */
152 item_num = RTE_MIN(nb_pkts, (param->max_flow_num *
153 param->max_item_per_flow));
154 item_num = RTE_MIN(item_num, RTE_GRO_MAX_BURST_ITEM_NUM);
156 for (i = 0; i < item_num; i++)
157 tcp_keys[i].start_index = INVALID_ARRAY_INDEX;
159 tcp_tbl.keys = tcp_keys;
160 tcp_tbl.items = tcp_items;
162 tcp_tbl.item_num = 0;
163 tcp_tbl.max_key_num = item_num;
164 tcp_tbl.max_item_num = item_num;
166 current_time = rte_rdtsc();
168 for (i = 0; i < nb_pkts; i++) {
169 if ((pkts[i]->packet_type & (RTE_PTYPE_L3_IPV4 |
170 RTE_PTYPE_L4_TCP)) ==
171 (RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP)) {
172 ret = gro_tcp4_reassemble(pkts[i],
176 /* merge successfully */
179 unprocess_pkts[unprocess_num++] =
183 unprocess_pkts[unprocess_num++] = pkts[i];
186 /* re-arrange GROed packets */
187 if (nb_after_gro < nb_pkts) {
188 i = gro_tcp4_tbl_timeout_flush(&tcp_tbl, current_time,
190 if (unprocess_num > 0) {
191 memcpy(&pkts[i], unprocess_pkts,
192 sizeof(struct rte_mbuf *) *
201 rte_gro_reassemble(struct rte_mbuf **pkts,
205 uint16_t i, unprocess_num = 0;
206 struct rte_mbuf *unprocess_pkts[nb_pkts];
207 struct gro_ctx *gro_ctx = ctx;
208 uint64_t current_time;
210 if ((gro_ctx->gro_types & RTE_GRO_TCP_IPV4) == 0)
213 current_time = rte_rdtsc();
215 for (i = 0; i < nb_pkts; i++) {
216 if ((pkts[i]->packet_type & (RTE_PTYPE_L3_IPV4 |
217 RTE_PTYPE_L4_TCP)) ==
218 (RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP)) {
219 if (gro_tcp4_reassemble(pkts[i],
221 [RTE_GRO_TCP_IPV4_INDEX],
223 unprocess_pkts[unprocess_num++] = pkts[i];
225 unprocess_pkts[unprocess_num++] = pkts[i];
227 if (unprocess_num > 0) {
228 memcpy(pkts, unprocess_pkts,
229 sizeof(struct rte_mbuf *) *
233 return unprocess_num;
237 rte_gro_timeout_flush(void *ctx,
238 uint64_t timeout_cycles,
240 struct rte_mbuf **out,
243 struct gro_ctx *gro_ctx = ctx;
244 uint64_t flush_timestamp;
246 gro_types = gro_types & gro_ctx->gro_types;
247 flush_timestamp = rte_rdtsc() - timeout_cycles;
249 if (gro_types & RTE_GRO_TCP_IPV4) {
250 return gro_tcp4_tbl_timeout_flush(
251 gro_ctx->tbls[RTE_GRO_TCP_IPV4_INDEX],
259 rte_gro_get_pkt_count(void *ctx)
261 struct gro_ctx *gro_ctx = ctx;
262 gro_tbl_pkt_count_fn pkt_count_fn;
263 uint64_t item_num = 0;
264 uint64_t gro_type_flag;
267 for (i = 0; i < RTE_GRO_TYPE_MAX_NUM; i++) {
268 gro_type_flag = 1ULL << i;
269 if ((gro_ctx->gro_types & gro_type_flag) == 0)
272 pkt_count_fn = tbl_pkt_count_fn[i];
273 if (pkt_count_fn == NULL)
275 item_num += pkt_count_fn(gro_ctx->tbls[i]);