2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_gro_h
17 #define included_gro_h
19 #include <vlib/vlib.h>
20 #include <vppinfra/error.h>
21 #include <vnet/ip/ip46_address.h>
23 #define GRO_FLOW_TABLE_MAX_SIZE 16
24 #define GRO_FLOW_TABLE_FLUSH 1e-5
25 #define GRO_FLOW_N_BUFFERS 64
26 #define GRO_FLOW_TIMEOUT 1e-5 /* 10 micro-seconds */
27 #define GRO_TO_VECTOR_SIZE(X) (X + GRO_FLOW_TABLE_MAX_SIZE)
33 u32 sw_if_index[VLIB_N_RX_TX];
34 ip46_address_t src_address;
35 ip46_address_t dst_address;
46 gro_flow_key_t flow_key;
62 gro_flow_t gro_flow[GRO_FLOW_TABLE_MAX_SIZE];
65 static_always_inline void
66 gro_flow_set_flow_key (gro_flow_t * to, gro_flow_key_t * from)
68 to->flow_key.flow_data[0] = from->flow_data[0];
69 to->flow_key.flow_data[1] = from->flow_data[1];
70 to->flow_key.flow_data[2] = from->flow_data[2];
71 to->flow_key.flow_data[3] = from->flow_data[3];
72 to->flow_key.flow_data[4] = from->flow_data[4];
73 to->flow_key.flow_data_u32 = from->flow_data_u32;
76 static_always_inline u8
77 gro_flow_is_equal (gro_flow_key_t * first, gro_flow_key_t * second)
79 if (first->flow_data[0] == second->flow_data[0] &&
80 first->flow_data[1] == second->flow_data[1] &&
81 first->flow_data[2] == second->flow_data[2] &&
82 first->flow_data[3] == second->flow_data[3] &&
83 first->flow_data[4] == second->flow_data[4] &&
84 first->flow_data_u32 == second->flow_data_u32)
91 * timeout_expire is in between 3 to 10 microseconds
94 static_always_inline void
95 gro_flow_set_timeout (vlib_main_t * vm, gro_flow_t * gro_flow,
98 gro_flow->next_timeout_ts = vlib_time_now (vm) + timeout_expire;
101 static_always_inline u8
102 gro_flow_is_timeout (vlib_main_t * vm, gro_flow_t * gro_flow)
104 if (gro_flow->next_timeout_ts < vlib_time_now (vm))
109 static_always_inline void
110 gro_flow_store_packet (gro_flow_t * gro_flow, u32 bi0)
112 if (gro_flow->n_buffers == 0)
114 gro_flow->buffer_index = bi0;
116 gro_flow->n_buffers++;
119 static_always_inline u32
120 gro_flow_table_init (gro_flow_table_t ** flow_table, u8 is_l2, u32 node_index)
125 gro_flow_table_t *flow_table_temp = 0;
127 (gro_flow_table_t *) clib_mem_alloc (sizeof (gro_flow_table_t));
128 if (!flow_table_temp)
130 clib_memset (flow_table_temp, 0, sizeof (gro_flow_table_t));
131 flow_table_temp->node_index = node_index;
132 flow_table_temp->is_enable = 1;
133 flow_table_temp->is_l2 = is_l2;
134 *flow_table = flow_table_temp;
138 static_always_inline void
139 gro_flow_table_set_timeout (vlib_main_t * vm, gro_flow_table_t * flow_table,
143 flow_table->timeout_ts = vlib_time_now (vm) + timeout_expire;
146 static_always_inline u8
147 gro_flow_table_is_timeout (vlib_main_t * vm, gro_flow_table_t * flow_table)
149 if (flow_table && (flow_table->timeout_ts < vlib_time_now (vm)))
154 static_always_inline u8
155 gro_flow_table_is_enable (gro_flow_table_t * flow_table)
158 return flow_table->is_enable;
163 static_always_inline void
164 gro_flow_table_set_is_enable (gro_flow_table_t * flow_table, u8 is_enable)
170 flow_table->is_enable = 1;
174 flow_table->is_enable = 0;
179 static_always_inline void
180 gro_flow_table_free (gro_flow_table_t * flow_table)
183 clib_mem_free (flow_table);
186 static_always_inline void
187 gro_flow_table_set_node_index (gro_flow_table_t * flow_table, u32 node_index)
190 flow_table->node_index = node_index;
193 static_always_inline gro_flow_t *
194 gro_flow_table_new_flow (gro_flow_table_t * flow_table)
196 if (PREDICT_TRUE (flow_table->flow_table_size < GRO_FLOW_TABLE_MAX_SIZE))
198 gro_flow_t *gro_flow;
200 while (i < GRO_FLOW_TABLE_MAX_SIZE)
202 gro_flow = &flow_table->gro_flow[i];
203 if (gro_flow->n_buffers == 0)
205 flow_table->flow_table_size++;
215 static_always_inline gro_flow_t *
216 gro_flow_table_get_flow (gro_flow_table_t * flow_table,
217 gro_flow_key_t * flow_key)
219 gro_flow_t *gro_flow = 0;
221 while (i < GRO_FLOW_TABLE_MAX_SIZE)
223 gro_flow = &flow_table->gro_flow[i];
224 if (gro_flow_is_equal (flow_key, &gro_flow->flow_key))
231 static_always_inline gro_flow_t *
232 gro_flow_table_find_or_add_flow (gro_flow_table_t * flow_table,
233 gro_flow_key_t * flow_key)
235 gro_flow_t *gro_flow = 0;
237 gro_flow = gro_flow_table_get_flow (flow_table, flow_key);
241 gro_flow = gro_flow_table_new_flow (flow_table);
245 gro_flow_set_flow_key (gro_flow, flow_key);
252 static_always_inline void
253 gro_flow_table_reset_flow (gro_flow_table_t * flow_table,
254 gro_flow_t * gro_flow)
256 if (PREDICT_TRUE (flow_table->flow_table_size > 0))
258 clib_memset (gro_flow, 0, sizeof (gro_flow_t));
259 flow_table->flow_table_size--;
263 static_always_inline u8 *
264 gro_flow_table_format (u8 * s, va_list * args)
266 gro_flow_table_t *flow_table = va_arg (*args, gro_flow_table_t *);
270 "flow-table: size %u gro-total-vectors %lu gro-n-vectors %u",
271 flow_table->flow_table_size, flow_table->total_vectors,
272 flow_table->n_vectors);
273 if (flow_table->n_vectors)
275 double average_rate =
276 (double) flow_table->total_vectors / (double) flow_table->n_vectors;
277 s = format (s, " gro-average-rate %.2f", average_rate);
280 s = format (s, " gro-average-rate 0.00");
284 #endif /* included_gro_h */
287 * fd.io coding-style-patch-verification: ON
290 * eval: (c-set-style "gnu")