blob: becfc460450356adadfd1ca6e8da5024e86072af [file] [log] [blame]
Klement Sekera75e7d132017-09-20 08:26:30 +02001/*
2 * Copyright (c) 2017 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16/**
17 * @file
Klement Sekera896c8962019-06-24 11:52:49 +000018 * @brief IPv4 Full Reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020019 *
Klement Sekera896c8962019-06-24 11:52:49 +000020 * This file contains the source code for IPv4 full reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020021 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
Klement Sekera896c8962019-06-24 11:52:49 +000026#include <vppinfra/fifo.h>
Klement Sekera8dcfed52018-06-28 11:16:15 +020027#include <vppinfra/bihash_16_8.h>
Klement Sekera896c8962019-06-24 11:52:49 +000028#include <vnet/ip/reass/ip4_full_reass.h>
Klement Sekera630ab582019-07-19 09:14:19 +000029#include <stddef.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020030
31#define MSEC_PER_SEC 1000
Vijayabhaskar Katamreddy8b874fc2022-05-13 13:07:19 +000032#define IP4_REASS_TIMEOUT_DEFAULT_MS 200
33
34/* As there are only 1024 reass context per thread, either the DDOS attacks
35 * or fractions of real timeouts, would consume these contexts quickly and
36 * running out context space and unable to perform reassembly */
37#define IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 50 // 50 ms default
Klement Sekera4c533132018-02-22 11:41:12 +010038#define IP4_REASS_MAX_REASSEMBLIES_DEFAULT 1024
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +000039#define IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
Klement Sekera75e7d132017-09-20 08:26:30 +020040#define IP4_REASS_HT_LOAD_FACTOR (0.75)
41
42#define IP4_REASS_DEBUG_BUFFERS 0
43#if IP4_REASS_DEBUG_BUFFERS
44#define IP4_REASS_DEBUG_BUFFER(bi, what) \
45 do \
46 { \
47 u32 _bi = bi; \
48 printf (#what "buffer %u", _bi); \
49 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
50 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \
51 { \
52 _bi = _b->next_buffer; \
53 printf ("[%u]", _bi); \
54 _b = vlib_get_buffer (vm, _bi); \
55 } \
56 printf ("\n"); \
57 fflush (stdout); \
58 } \
59 while (0)
60#else
61#define IP4_REASS_DEBUG_BUFFER(...)
62#endif
63
Klement Sekerad0f70a32018-12-14 17:24:13 +010064typedef enum
65{
66 IP4_REASS_RC_OK,
Klement Sekera3a343d42019-05-16 14:35:46 +020067 IP4_REASS_RC_TOO_MANY_FRAGMENTS,
Klement Sekerad0f70a32018-12-14 17:24:13 +010068 IP4_REASS_RC_INTERNAL_ERROR,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010069 IP4_REASS_RC_NO_BUF,
Klement Sekera630ab582019-07-19 09:14:19 +000070 IP4_REASS_RC_HANDOFF,
Klement Sekera896c8962019-06-24 11:52:49 +000071} ip4_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020072
73typedef struct
74{
75 union
76 {
77 struct
78 {
Klement Sekera75e7d132017-09-20 08:26:30 +020079 u32 xx_id;
80 ip4_address_t src;
81 ip4_address_t dst;
Klement Sekera8dcfed52018-06-28 11:16:15 +020082 u16 frag_id;
83 u8 proto;
84 u8 unused;
Klement Sekera75e7d132017-09-20 08:26:30 +020085 };
Klement Sekera8dcfed52018-06-28 11:16:15 +020086 u64 as_u64[2];
Klement Sekera75e7d132017-09-20 08:26:30 +020087 };
Klement Sekera896c8962019-06-24 11:52:49 +000088} ip4_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020089
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080090typedef union
91{
92 struct
93 {
94 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000095 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080096 };
97 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000098} ip4_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080099
100typedef union
101{
102 struct
103 {
Klement Sekera896c8962019-06-24 11:52:49 +0000104 ip4_full_reass_key_t k;
105 ip4_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800106 };
107 clib_bihash_kv_16_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +0000108} ip4_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800109
Klement Sekera75e7d132017-09-20 08:26:30 +0200110always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +0000111ip4_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +0200112{
113 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100114 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +0200115}
116
117always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +0000118ip4_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +0200119{
120 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100121 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +0000122 (vnb->ip.reass.fragment_first +
123 ip4_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +0200124}
125
126typedef struct
127{
128 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000129 ip4_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200130 // time when last packet was received
131 f64 last_heard;
132 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100133 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200134 // buffer index of first buffer in this reassembly context
135 u32 first_bi;
136 // last octet of packet, ~0 until fragment without more_fragments arrives
137 u32 last_packet_octet;
138 // length of data collected so far
139 u32 data_len;
140 // trace operation counter
141 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100142 // next index - used by non-feature node
Klement Sekera21aa8f12019-05-20 12:27:33 +0200143 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000144 // error next index - used by custom apps (~0 if not used)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200145 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100146 // minimum fragment length for this reassembly - used to estimate MTU
147 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200148 // number of fragments in this reassembly
149 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000150 // thread owning memory for this context (whose pool contains this ctx)
151 u32 memory_owner_thread_index;
152 // thread which received fragment with offset 0 and which sends out the
153 // completed reassembly
154 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000155} ip4_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200156
157typedef struct
158{
Klement Sekera896c8962019-06-24 11:52:49 +0000159 ip4_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100160 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100161 u32 id_counter;
Vijayabhaskar Katamreddy8b874fc2022-05-13 13:07:19 +0000162 // for pacing the main thread timeouts
163 u32 last_id;
Klement Sekera4c533132018-02-22 11:41:12 +0100164 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000165} ip4_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100166
167typedef struct
168{
Klement Sekera75e7d132017-09-20 08:26:30 +0200169 // IPv4 config
170 u32 timeout_ms;
171 f64 timeout;
172 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200173 // maximum number of fragments in one reassembly
174 u32 max_reass_len;
175 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200176 u32 max_reass_n;
177
178 // IPv4 runtime
Klement Sekera8dcfed52018-06-28 11:16:15 +0200179 clib_bihash_16_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100180 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000181 ip4_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200182
183 // convenience
184 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200185
Klement Sekera896c8962019-06-24 11:52:49 +0000186 u32 ip4_full_reass_expire_node_idx;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800187
188 /** Worker handoff */
189 u32 fq_index;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000190 u32 fq_local_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800191 u32 fq_feature_index;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000192 u32 fq_custom_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200193
Klement Sekera7b2e9fb2019-10-01 13:00:22 +0000194 // reference count for enabling/disabling feature - per interface
195 u32 *feature_use_refcount_per_intf;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000196
197 // whether local fragmented packets are reassembled or not
198 int is_local_reass_enabled;
Klement Sekera896c8962019-06-24 11:52:49 +0000199} ip4_full_reass_main_t;
200
201extern ip4_full_reass_main_t ip4_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700202
203#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000204ip4_full_reass_main_t ip4_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700205#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200206
207typedef enum
208{
Klement Sekera896c8962019-06-24 11:52:49 +0000209 IP4_FULL_REASS_NEXT_INPUT,
210 IP4_FULL_REASS_NEXT_DROP,
211 IP4_FULL_REASS_NEXT_HANDOFF,
212 IP4_FULL_REASS_N_NEXT,
213} ip4_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200214
215typedef enum
216{
Klement Sekerafe8371f2020-09-10 12:03:54 +0000217 NORMAL,
218 FEATURE,
219 CUSTOM
220} ip4_full_reass_node_type_t;
221
222typedef enum
223{
Klement Sekera75e7d132017-09-20 08:26:30 +0200224 RANGE_NEW,
225 RANGE_SHRINK,
226 RANGE_DISCARD,
227 RANGE_OVERLAP,
228 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000229 HANDOFF,
Klement Sekera01c1fa42021-12-14 18:25:11 +0000230 PASSTHROUGH,
Klement Sekera896c8962019-06-24 11:52:49 +0000231} ip4_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200232
233typedef struct
234{
235 u16 range_first;
236 u16 range_last;
237 u32 range_bi;
238 i32 data_offset;
239 u32 data_len;
240 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000241} ip4_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200242
243typedef struct
244{
Klement Sekera896c8962019-06-24 11:52:49 +0000245 ip4_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200246 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000247 ip4_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200248 u32 size_diff;
249 u32 op_id;
Klement Sekera630ab582019-07-19 09:14:19 +0000250 u32 thread_id;
251 u32 thread_id_to;
Klement Sekera75e7d132017-09-20 08:26:30 +0200252 u32 fragment_first;
253 u32 fragment_last;
254 u32 total_data_len;
Klement Sekera8563cb32019-10-10 17:03:57 +0000255 bool is_after_handoff;
256 ip4_header_t ip4_header;
Klement Sekera896c8962019-06-24 11:52:49 +0000257} ip4_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200258
Klement Sekera896c8962019-06-24 11:52:49 +0000259extern vlib_node_registration_t ip4_full_reass_node;
260extern vlib_node_registration_t ip4_full_reass_node_feature;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000261extern vlib_node_registration_t ip4_full_reass_node_custom;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700262
Klement Sekera4c533132018-02-22 11:41:12 +0100263static void
Klement Sekera896c8962019-06-24 11:52:49 +0000264ip4_full_reass_trace_details (vlib_main_t * vm, u32 bi,
265 ip4_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200266{
267 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
268 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
269 trace->range_first = vnb->ip.reass.range_first;
270 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000271 trace->data_offset = ip4_full_reass_buffer_get_data_offset (b);
272 trace->data_len = ip4_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200273 trace->range_bi = bi;
274}
275
Klement Sekera4c533132018-02-22 11:41:12 +0100276static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000277format_ip4_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200278{
Klement Sekera896c8962019-06-24 11:52:49 +0000279 ip4_full_reass_range_trace_t *trace =
280 va_arg (*args, ip4_full_reass_range_trace_t *);
281 s =
282 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
283 trace->range_last, trace->data_offset, trace->data_len,
284 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200285 return s;
286}
287
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700288static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000289format_ip4_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200290{
291 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
292 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000293 ip4_full_reass_trace_t *t = va_arg (*args, ip4_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000294 u32 indent = 0;
295 if (~0 != t->reass_id)
296 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000297 if (t->is_after_handoff)
298 {
299 s =
300 format (s, "%U\n", format_ip4_header, &t->ip4_header,
301 sizeof (t->ip4_header));
302 indent = 2;
303 }
304 s =
305 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
306 t->reass_id, t->op_id);
Klement Sekera630ab582019-07-19 09:14:19 +0000307 indent = format_get_indent (s);
308 s =
309 format (s,
310 "first bi: %u, data len: %u, ip/fragment[%u, %u]",
311 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
312 t->fragment_last);
313 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200314 switch (t->action)
315 {
316 case RANGE_SHRINK:
317 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000318 format_ip4_full_reass_range_trace, &t->trace_range,
Klement Sekera75e7d132017-09-20 08:26:30 +0200319 t->size_diff);
320 break;
321 case RANGE_DISCARD:
322 s = format (s, "\n%Udiscard %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000323 format_ip4_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200324 break;
325 case RANGE_NEW:
326 s = format (s, "\n%Unew %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000327 format_ip4_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200328 break;
329 case RANGE_OVERLAP:
330 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000331 format_ip4_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200332 break;
333 case FINALIZE:
334 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
335 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000336 case HANDOFF:
337 s =
338 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
339 t->thread_id_to);
340 break;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000341 case PASSTHROUGH:
342 s = format (s, "passthrough - not a fragment");
343 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200344 }
345 return s;
346}
347
Klement Sekera4c533132018-02-22 11:41:12 +0100348static void
Klement Sekera896c8962019-06-24 11:52:49 +0000349ip4_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000350 ip4_full_reass_t * reass, u32 bi,
351 ip4_full_reass_trace_operation_e action,
352 u32 size_diff, u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200353{
354 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
355 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera53be16d2020-12-15 21:47:36 +0100356 if (pool_is_free_index
357 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
358 {
359 // this buffer's trace is gone
360 b->flags &= ~VLIB_BUFFER_IS_TRACED;
361 return;
362 }
Klement Sekera8563cb32019-10-10 17:03:57 +0000363 bool is_after_handoff = false;
364 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
365 {
366 is_after_handoff = true;
367 }
Klement Sekera896c8962019-06-24 11:52:49 +0000368 ip4_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000369 t->is_after_handoff = is_after_handoff;
370 if (t->is_after_handoff)
371 {
372 clib_memcpy (&t->ip4_header, vlib_buffer_get_current (b),
373 clib_min (sizeof (t->ip4_header), b->current_length));
374 }
Klement Sekera896c8962019-06-24 11:52:49 +0000375 if (reass)
376 {
377 t->reass_id = reass->id;
378 t->op_id = reass->trace_op_counter;
379 t->trace_range.first_bi = reass->first_bi;
380 t->total_data_len = reass->data_len;
381 ++reass->trace_op_counter;
382 }
383 else
384 {
385 t->reass_id = ~0;
386 t->op_id = 0;
387 t->trace_range.first_bi = 0;
388 t->total_data_len = 0;
389 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200390 t->action = action;
Klement Sekera896c8962019-06-24 11:52:49 +0000391 ip4_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200392 t->size_diff = size_diff;
Klement Sekera630ab582019-07-19 09:14:19 +0000393 t->thread_id = vm->thread_index;
394 t->thread_id_to = thread_id_to;
Klement Sekera75e7d132017-09-20 08:26:30 +0200395 t->fragment_first = vnb->ip.reass.fragment_first;
396 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200397#if 0
398 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000399 s = format (s, "%U", format_ip4_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200400 printf ("%.*s\n", vec_len (s), s);
401 fflush (stdout);
402 vec_reset_length (s);
403#endif
404}
405
Klement Sekera630ab582019-07-19 09:14:19 +0000406always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000407ip4_full_reass_free_ctx (ip4_full_reass_per_thread_t * rt,
408 ip4_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000409{
410 pool_put (rt->pool, reass);
411 --rt->reass_n;
412}
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800413
Klement Sekera4c533132018-02-22 11:41:12 +0100414always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000415ip4_full_reass_free (ip4_full_reass_main_t * rm,
416 ip4_full_reass_per_thread_t * rt,
417 ip4_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200418{
Klement Sekera8dcfed52018-06-28 11:16:15 +0200419 clib_bihash_kv_16_8_t kv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200420 kv.key[0] = reass->key.as_u64[0];
421 kv.key[1] = reass->key.as_u64[1];
Klement Sekera8dcfed52018-06-28 11:16:15 +0200422 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000423 return ip4_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200424}
425
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000426/* n_left_to_next, and to_next are taken as input params, as this function
427 * could be called from a graphnode, where its managing local copy of these
428 * variables, and ignoring those and still trying to enqueue the buffers
429 * with local variables would cause either buffer leak or corruption */
Klement Sekera4c533132018-02-22 11:41:12 +0100430always_inline void
Klement Sekera42cec0e2021-08-02 16:14:15 +0200431ip4_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000432 ip4_full_reass_t *reass, u32 *n_left_to_next,
433 u32 **to_next)
Klement Sekera75e7d132017-09-20 08:26:30 +0200434{
435 u32 range_bi = reass->first_bi;
436 vlib_buffer_t *range_b;
437 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100438 u32 *to_free = NULL;
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000439
Klement Sekera75e7d132017-09-20 08:26:30 +0200440 while (~0 != range_bi)
441 {
442 range_b = vlib_get_buffer (vm, range_bi);
443 range_vnb = vnet_buffer (range_b);
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000444
445 if (~0 != range_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200446 {
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000447 vec_add1 (to_free, range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200448 }
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000449
Klement Sekera75e7d132017-09-20 08:26:30 +0200450 range_bi = range_vnb->ip.reass.next_range_bi;
451 }
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000452
Klement Sekera21aa8f12019-05-20 12:27:33 +0200453 /* send to next_error_index */
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000454 if (~0 != reass->error_next_index &&
455 reass->error_next_index < node->n_next_nodes)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200456 {
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000457 u32 next_index;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200458
459 next_index = reass->error_next_index;
460 u32 bi = ~0;
461
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +0000462 /* record number of packets sent to custom app */
463 vlib_node_increment_counter (vm, node->node_index,
464 IP4_ERROR_REASS_TO_CUSTOM_APP,
465 vec_len (to_free));
466
Klement Sekera21aa8f12019-05-20 12:27:33 +0200467 while (vec_len (to_free) > 0)
468 {
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000469 vlib_get_next_frame (vm, node, next_index, *to_next,
470 (*n_left_to_next));
Klement Sekera21aa8f12019-05-20 12:27:33 +0200471
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000472 while (vec_len (to_free) > 0 && (*n_left_to_next) > 0)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200473 {
474 bi = vec_pop (to_free);
475
476 if (~0 != bi)
477 {
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000478 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
479 if ((b->flags & VLIB_BUFFER_IS_TRACED))
480 {
481 ip4_full_reass_add_trace (vm, node, reass, bi,
482 RANGE_DISCARD, 0, ~0);
483 }
484 *to_next[0] = bi;
485 (*to_next) += 1;
486 (*n_left_to_next) -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200487 }
488 }
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000489 vlib_put_next_frame (vm, node, next_index, (*n_left_to_next));
Klement Sekera21aa8f12019-05-20 12:27:33 +0200490 }
491 }
492 else
493 {
494 vlib_buffer_free (vm, to_free, vec_len (to_free));
495 }
barryxie01e94db2020-12-04 19:21:23 +0800496 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200497}
498
Klement Sekera896c8962019-06-24 11:52:49 +0000499always_inline void
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000500sanitize_reass_buffers_add_missing (vlib_main_t *vm, ip4_full_reass_t *reass,
501 u32 *bi0)
502{
503 u32 range_bi = reass->first_bi;
504 vlib_buffer_t *range_b;
505 vnet_buffer_opaque_t *range_vnb;
506
507 while (~0 != range_bi)
508 {
509 range_b = vlib_get_buffer (vm, range_bi);
510 range_vnb = vnet_buffer (range_b);
511 u32 bi = range_bi;
512 if (~0 != bi)
513 {
514 if (bi == *bi0)
515 *bi0 = ~0;
516 if (range_b->flags & VLIB_BUFFER_NEXT_PRESENT)
517 {
518 u32 _bi = bi;
519 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi);
520 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)
521 {
522 if (_b->next_buffer != range_vnb->ip.reass.next_range_bi)
523 {
524 _bi = _b->next_buffer;
525 _b = vlib_get_buffer (vm, _bi);
526 }
527 else
528 {
529 _b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
530 break;
531 }
532 }
533 }
534 range_bi = range_vnb->ip.reass.next_range_bi;
535 }
536 }
537 if (*bi0 != ~0)
538 {
539 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
540 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
541 if (~0 != reass->first_bi)
542 {
543 fvnb->ip.reass.next_range_bi = reass->first_bi;
544 reass->first_bi = *bi0;
545 }
546 else
547 {
548 reass->first_bi = *bi0;
549 fvnb->ip.reass.next_range_bi = ~0;
550 }
551 *bi0 = ~0;
552 }
553}
554
555always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000556ip4_full_reass_init (ip4_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200557{
Klement Sekera896c8962019-06-24 11:52:49 +0000558 reass->first_bi = ~0;
559 reass->last_packet_octet = ~0;
560 reass->data_len = 0;
561 reass->next_index = ~0;
562 reass->error_next_index = ~0;
563}
564
565always_inline ip4_full_reass_t *
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000566ip4_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
567 ip4_full_reass_main_t *rm,
568 ip4_full_reass_per_thread_t *rt,
569 ip4_full_reass_kv_t *kv, u8 *do_handoff,
570 u32 *n_left_to_next, u32 **to_next)
Klement Sekera896c8962019-06-24 11:52:49 +0000571{
572 ip4_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000573 f64 now;
Klement Sekera75e7d132017-09-20 08:26:30 +0200574
Klement Sekera630ab582019-07-19 09:14:19 +0000575again:
576
577 reass = NULL;
578 now = vlib_time_now (vm);
Klement Sekerac99c0252019-12-18 12:17:06 +0000579 if (!clib_bihash_search_16_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200580 {
Gao Feng9165e032020-04-26 09:57:18 +0800581 if (vm->thread_index != kv->v.memory_owner_thread_index)
582 {
583 *do_handoff = 1;
584 return NULL;
585 }
Klement Sekera630ab582019-07-19 09:14:19 +0000586 reass =
587 pool_elt_at_index (rm->per_thread_data
588 [kv->v.memory_owner_thread_index].pool,
589 kv->v.reass_index);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800590
Klement Sekera75e7d132017-09-20 08:26:30 +0200591 if (now > reass->last_heard + rm->timeout)
592 {
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +0000593 vlib_node_increment_counter (vm, node->node_index,
594 IP4_ERROR_REASS_TIMEOUT, 1);
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000595 ip4_full_reass_drop_all (vm, node, reass, n_left_to_next, to_next);
Klement Sekera896c8962019-06-24 11:52:49 +0000596 ip4_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200597 reass = NULL;
598 }
599 }
600
601 if (reass)
602 {
603 reass->last_heard = now;
604 return reass;
605 }
606
Klement Sekera4c533132018-02-22 11:41:12 +0100607 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200608 {
609 reass = NULL;
610 return reass;
611 }
612 else
613 {
Klement Sekera4c533132018-02-22 11:41:12 +0100614 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400615 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800616 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera630ab582019-07-19 09:14:19 +0000617 reass->memory_owner_thread_index = vm->thread_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100618 ++rt->id_counter;
Klement Sekera896c8962019-06-24 11:52:49 +0000619 ip4_full_reass_init (reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100620 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200621 }
622
Klement Sekerac99c0252019-12-18 12:17:06 +0000623 reass->key.as_u64[0] = kv->kv.key[0];
624 reass->key.as_u64[1] = kv->kv.key[1];
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800625 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000626 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200627 reass->last_heard = now;
628
Klement Sekerac99c0252019-12-18 12:17:06 +0000629 int rv = clib_bihash_add_del_16_8 (&rm->hash, &kv->kv, 2);
Klement Sekera630ab582019-07-19 09:14:19 +0000630 if (rv)
Klement Sekera75e7d132017-09-20 08:26:30 +0200631 {
Klement Sekera896c8962019-06-24 11:52:49 +0000632 ip4_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200633 reass = NULL;
Klement Sekera630ab582019-07-19 09:14:19 +0000634 // if other worker created a context already work with the other copy
635 if (-2 == rv)
636 goto again;
Klement Sekera75e7d132017-09-20 08:26:30 +0200637 }
638
639 return reass;
640}
641
Klement Sekera896c8962019-06-24 11:52:49 +0000642always_inline ip4_full_reass_rc_t
643ip4_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
644 ip4_full_reass_main_t * rm,
645 ip4_full_reass_per_thread_t * rt,
646 ip4_full_reass_t * reass, u32 * bi0,
Klement Sekerafe8371f2020-09-10 12:03:54 +0000647 u32 * next0, u32 * error0, bool is_custom)
Klement Sekera75e7d132017-09-20 08:26:30 +0200648{
Klement Sekera75e7d132017-09-20 08:26:30 +0200649 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
650 vlib_buffer_t *last_b = NULL;
651 u32 sub_chain_bi = reass->first_bi;
652 u32 total_length = 0;
653 u32 buf_cnt = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200654 do
655 {
656 u32 tmp_bi = sub_chain_bi;
657 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
658 ip4_header_t *ip = vlib_buffer_get_current (tmp);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100659 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
660 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
661 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
662 {
663 return IP4_REASS_RC_INTERNAL_ERROR;
664 }
665
Klement Sekera896c8962019-06-24 11:52:49 +0000666 u32 data_len = ip4_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200667 u32 trim_front =
Klement Sekera896c8962019-06-24 11:52:49 +0000668 ip4_header_bytes (ip) + ip4_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200669 u32 trim_end =
670 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
671 if (tmp_bi == reass->first_bi)
672 {
673 /* first buffer - keep ip4 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000674 if (0 != ip4_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100675 {
676 return IP4_REASS_RC_INTERNAL_ERROR;
677 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200678 trim_front = 0;
679 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
680 ip4_header_bytes (ip);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100681 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
682 {
683 return IP4_REASS_RC_INTERNAL_ERROR;
684 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200685 }
686 u32 keep_data =
687 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
688 while (1)
689 {
690 ++buf_cnt;
691 if (trim_front)
692 {
693 if (trim_front > tmp->current_length)
694 {
695 /* drop whole buffer */
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200696 u32 to_be_freed_bi = tmp_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200697 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100698 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
699 {
700 return IP4_REASS_RC_INTERNAL_ERROR;
701 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200702 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
703 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700704 tmp->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200705 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200706 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200707 continue;
708 }
709 else
710 {
711 vlib_buffer_advance (tmp, trim_front);
712 trim_front = 0;
713 }
714 }
715 if (keep_data)
716 {
717 if (last_b)
718 {
719 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
720 last_b->next_buffer = tmp_bi;
721 }
722 last_b = tmp;
723 if (keep_data <= tmp->current_length)
724 {
725 tmp->current_length = keep_data;
726 keep_data = 0;
727 }
728 else
729 {
730 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100731 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
732 {
733 return IP4_REASS_RC_INTERNAL_ERROR;
734 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200735 }
736 total_length += tmp->current_length;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200737 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
738 {
739 tmp_bi = tmp->next_buffer;
740 tmp = vlib_get_buffer (vm, tmp->next_buffer);
741 }
742 else
743 {
744 break;
745 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200746 }
747 else
748 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200749 u32 to_be_freed_bi = tmp_bi;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100750 if (reass->first_bi == tmp_bi)
751 {
752 return IP4_REASS_RC_INTERNAL_ERROR;
753 }
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200754 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
755 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700756 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200757 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700758 tmp->next_buffer = 0;
759 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200760 vlib_buffer_free_one (vm, to_be_freed_bi);
761 }
762 else
763 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700764 tmp->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200765 vlib_buffer_free_one (vm, to_be_freed_bi);
766 break;
767 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200768 }
769 }
770 sub_chain_bi =
771 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
772 reass.next_range_bi;
773 }
774 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700775
Klement Sekerad0f70a32018-12-14 17:24:13 +0100776 if (!last_b)
777 {
778 return IP4_REASS_RC_INTERNAL_ERROR;
779 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200780 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700781
Klement Sekerad0f70a32018-12-14 17:24:13 +0100782 if (total_length < first_b->current_length)
783 {
784 return IP4_REASS_RC_INTERNAL_ERROR;
785 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200786 total_length -= first_b->current_length;
787 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
788 first_b->total_length_not_including_first_buffer = total_length;
789 ip4_header_t *ip = vlib_buffer_get_current (first_b);
790 ip->flags_and_fragment_offset = 0;
791 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
792 ip->checksum = ip4_header_checksum (ip);
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100793 if (!vlib_buffer_chain_linearize (vm, first_b))
794 {
795 return IP4_REASS_RC_NO_BUF;
796 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700797 // reset to reconstruct the mbuf linking
798 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200799 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
800 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200801 ip4_full_reass_add_trace (vm, node, reass, reass->first_bi, FINALIZE, 0,
802 ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200803#if 0
804 // following code does a hexdump of packet fragments to stdout ...
805 do
806 {
807 u32 bi = reass->first_bi;
808 u8 *s = NULL;
809 while (~0 != bi)
810 {
811 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
812 s = format (s, "%u: %U\n", bi, format_hexdump,
813 vlib_buffer_get_current (b), b->current_length);
814 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
815 {
816 bi = b->next_buffer;
817 }
818 else
819 {
820 break;
821 }
822 }
823 printf ("%.*s\n", vec_len (s), s);
824 fflush (stdout);
825 vec_free (s);
826 }
827 while (0);
828#endif
829 }
830 *bi0 = reass->first_bi;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000831 if (!is_custom)
Klement Sekera4c533132018-02-22 11:41:12 +0100832 {
Klement Sekera896c8962019-06-24 11:52:49 +0000833 *next0 = IP4_FULL_REASS_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100834 }
835 else
836 {
837 *next0 = reass->next_index;
838 }
839 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +0000840
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +0000841 /* Keep track of number of successfully reassembled packets and number of
842 * fragments reassembled */
843 vlib_node_increment_counter (vm, node->node_index, IP4_ERROR_REASS_SUCCESS,
844 1);
845
846 vlib_node_increment_counter (vm, node->node_index,
847 IP4_ERROR_REASS_FRAGMENTS_REASSEMBLED,
848 reass->fragments_n);
849
Klement Sekera75e7d132017-09-20 08:26:30 +0200850 *error0 = IP4_ERROR_NONE;
Klement Sekera896c8962019-06-24 11:52:49 +0000851 ip4_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200852 reass = NULL;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100853 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200854}
855
Klement Sekera896c8962019-06-24 11:52:49 +0000856always_inline ip4_full_reass_rc_t
857ip4_full_reass_insert_range_in_chain (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +0000858 ip4_full_reass_t * reass,
859 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200860{
Klement Sekera75e7d132017-09-20 08:26:30 +0200861 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
862 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
863 if (~0 != prev_range_bi)
864 {
865 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
866 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
867 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
868 prev_vnb->ip.reass.next_range_bi = new_next_bi;
869 }
870 else
871 {
872 if (~0 != reass->first_bi)
873 {
874 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
875 }
876 reass->first_bi = new_next_bi;
877 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100878 vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
879 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
880 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
881 {
882 return IP4_REASS_RC_INTERNAL_ERROR;
883 }
Klement Sekera896c8962019-06-24 11:52:49 +0000884 reass->data_len += ip4_full_reass_buffer_get_data_len (new_next_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100885 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200886}
887
Klement Sekera896c8962019-06-24 11:52:49 +0000888always_inline ip4_full_reass_rc_t
889ip4_full_reass_remove_range_from_chain (vlib_main_t * vm,
890 vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000891 ip4_full_reass_t * reass,
892 u32 prev_range_bi, u32 discard_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200893{
894 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
895 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
896 if (~0 != prev_range_bi)
897 {
898 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
899 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100900 if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
901 {
902 return IP4_REASS_RC_INTERNAL_ERROR;
903 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200904 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
905 }
906 else
907 {
908 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
909 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100910 vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
911 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
912 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
913 {
914 return IP4_REASS_RC_INTERNAL_ERROR;
915 }
Klement Sekera896c8962019-06-24 11:52:49 +0000916 reass->data_len -= ip4_full_reass_buffer_get_data_len (discard_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200917 while (1)
918 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200919 u32 to_be_freed_bi = discard_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200920 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
921 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200922 ip4_full_reass_add_trace (vm, node, reass, discard_bi, RANGE_DISCARD,
923 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200924 }
925 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
926 {
927 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
928 discard_bi = discard_b->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700929 discard_b->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200930 discard_b = vlib_get_buffer (vm, discard_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200931 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200932 }
933 else
934 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700935 discard_b->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200936 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200937 break;
938 }
939 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100940 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200941}
942
Klement Sekera896c8962019-06-24 11:52:49 +0000943always_inline ip4_full_reass_rc_t
944ip4_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
945 ip4_full_reass_main_t * rm,
946 ip4_full_reass_per_thread_t * rt,
947 ip4_full_reass_t * reass, u32 * bi0, u32 * next0,
Klement Sekerafe8371f2020-09-10 12:03:54 +0000948 u32 * error0, bool is_custom, u32 * handoff_thread_idx)
Klement Sekera75e7d132017-09-20 08:26:30 +0200949{
Klement Sekera75e7d132017-09-20 08:26:30 +0200950 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200951 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerafe8371f2020-09-10 12:03:54 +0000952 if (is_custom)
Klement Sekerae8498652019-06-17 12:23:15 +0000953 {
954 // store (error_)next_index before it's overwritten
955 reass->next_index = fvnb->ip.reass.next_index;
956 reass->error_next_index = fvnb->ip.reass.error_next_index;
957 }
Klement Sekera896c8962019-06-24 11:52:49 +0000958 ip4_full_reass_rc_t rc = IP4_REASS_RC_OK;
959 int consumed = 0;
960 ip4_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekera14d7e902018-12-10 13:46:09 +0100961 const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
962 const u32 fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200963 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
Klement Sekera14d7e902018-12-10 13:46:09 +0100964 const u32 fragment_last = fragment_first + fragment_length - 1;
965 fvnb->ip.reass.fragment_first = fragment_first;
966 fvnb->ip.reass.fragment_last = fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200967 int more_fragments = ip4_get_fragment_more (fip);
968 u32 candidate_range_bi = reass->first_bi;
969 u32 prev_range_bi = ~0;
970 fvnb->ip.reass.range_first = fragment_first;
971 fvnb->ip.reass.range_last = fragment_last;
972 fvnb->ip.reass.next_range_bi = ~0;
973 if (!more_fragments)
974 {
975 reass->last_packet_octet = fragment_last;
976 }
977 if (~0 == reass->first_bi)
978 {
979 // starting a new reassembly
Klement Sekerad0f70a32018-12-14 17:24:13 +0100980 rc =
Klement Sekera42cec0e2021-08-02 16:14:15 +0200981 ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100982 if (IP4_REASS_RC_OK != rc)
983 {
984 return rc;
985 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200986 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
987 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200988 ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200989 }
990 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100991 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekera3a343d42019-05-16 14:35:46 +0200992 reass->fragments_n = 1;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100993 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200994 }
Klement Sekera896c8962019-06-24 11:52:49 +0000995 reass->min_fragment_length =
996 clib_min (clib_net_to_host_u16 (fip->length),
997 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200998 while (~0 != candidate_range_bi)
999 {
1000 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
1001 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
1002 if (fragment_first > candidate_vnb->ip.reass.range_last)
1003 {
1004 // this fragments starts after candidate range
1005 prev_range_bi = candidate_range_bi;
1006 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
1007 if (candidate_vnb->ip.reass.range_last < fragment_last &&
1008 ~0 == candidate_range_bi)
1009 {
1010 // special case - this fragment falls beyond all known ranges
Klement Sekera42cec0e2021-08-02 16:14:15 +02001011 rc = ip4_full_reass_insert_range_in_chain (vm, reass,
1012 prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001013 if (IP4_REASS_RC_OK != rc)
1014 {
1015 return rc;
1016 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001017 consumed = 1;
1018 break;
1019 }
1020 continue;
1021 }
1022 if (fragment_last < candidate_vnb->ip.reass.range_first)
1023 {
1024 // this fragment ends before candidate range without any overlap
Klement Sekera42cec0e2021-08-02 16:14:15 +02001025 rc = ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1026 *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001027 if (IP4_REASS_RC_OK != rc)
1028 {
1029 return rc;
1030 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001031 consumed = 1;
1032 }
1033 else
1034 {
1035 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
1036 fragment_last <= candidate_vnb->ip.reass.range_last)
1037 {
1038 // this fragment is a (sub)part of existing range, ignore it
1039 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1040 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001041 ip4_full_reass_add_trace (vm, node, reass, *bi0,
Klement Sekera896c8962019-06-24 11:52:49 +00001042 RANGE_OVERLAP, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001043 }
1044 break;
1045 }
1046 int discard_candidate = 0;
1047 if (fragment_first < candidate_vnb->ip.reass.range_first)
1048 {
1049 u32 overlap =
1050 fragment_last - candidate_vnb->ip.reass.range_first + 1;
Klement Sekera896c8962019-06-24 11:52:49 +00001051 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
Klement Sekera75e7d132017-09-20 08:26:30 +02001052 {
1053 candidate_vnb->ip.reass.range_first += overlap;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001054 if (reass->data_len < overlap)
1055 {
1056 return IP4_REASS_RC_INTERNAL_ERROR;
1057 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001058 reass->data_len -= overlap;
1059 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1060 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001061 ip4_full_reass_add_trace (vm, node, reass,
Klement Sekera896c8962019-06-24 11:52:49 +00001062 candidate_range_bi,
1063 RANGE_SHRINK, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001064 }
Klement Sekera42cec0e2021-08-02 16:14:15 +02001065 rc = ip4_full_reass_insert_range_in_chain (
1066 vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001067 if (IP4_REASS_RC_OK != rc)
1068 {
1069 return rc;
1070 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001071 consumed = 1;
1072 }
1073 else
1074 {
1075 discard_candidate = 1;
1076 }
1077 }
1078 else if (fragment_last > candidate_vnb->ip.reass.range_last)
1079 {
1080 u32 overlap =
1081 candidate_vnb->ip.reass.range_last - fragment_first + 1;
Klement Sekera896c8962019-06-24 11:52:49 +00001082 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
Klement Sekera75e7d132017-09-20 08:26:30 +02001083 {
1084 fvnb->ip.reass.range_first += overlap;
1085 if (~0 != candidate_vnb->ip.reass.next_range_bi)
1086 {
1087 prev_range_bi = candidate_range_bi;
1088 candidate_range_bi =
1089 candidate_vnb->ip.reass.next_range_bi;
1090 continue;
1091 }
1092 else
1093 {
1094 // special case - last range discarded
Klement Sekera42cec0e2021-08-02 16:14:15 +02001095 rc = ip4_full_reass_insert_range_in_chain (
1096 vm, reass, candidate_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001097 if (IP4_REASS_RC_OK != rc)
1098 {
1099 return rc;
1100 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001101 consumed = 1;
1102 }
1103 }
1104 else
1105 {
1106 discard_candidate = 1;
1107 }
1108 }
1109 else
1110 {
1111 discard_candidate = 1;
1112 }
1113 if (discard_candidate)
1114 {
1115 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
1116 // discard candidate range, probe next range
Klement Sekera42cec0e2021-08-02 16:14:15 +02001117 rc = ip4_full_reass_remove_range_from_chain (
1118 vm, node, reass, prev_range_bi, candidate_range_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001119 if (IP4_REASS_RC_OK != rc)
1120 {
1121 return rc;
1122 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001123 if (~0 != next_range_bi)
1124 {
1125 candidate_range_bi = next_range_bi;
1126 continue;
1127 }
1128 else
1129 {
1130 // special case - last range discarded
Klement Sekera42cec0e2021-08-02 16:14:15 +02001131 rc = ip4_full_reass_insert_range_in_chain (
1132 vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001133 if (IP4_REASS_RC_OK != rc)
1134 {
1135 return rc;
1136 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001137 consumed = 1;
1138 }
1139 }
1140 }
1141 break;
1142 }
Klement Sekera3a343d42019-05-16 14:35:46 +02001143 ++reass->fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +02001144 if (consumed)
1145 {
1146 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1147 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001148 ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001149 }
1150 }
1151 if (~0 != reass->last_packet_octet &&
1152 reass->data_len == reass->last_packet_octet + 1)
1153 {
Klement Sekera630ab582019-07-19 09:14:19 +00001154 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001155 int handoff =
1156 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera630ab582019-07-19 09:14:19 +00001157 rc =
Klement Sekera896c8962019-06-24 11:52:49 +00001158 ip4_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001159 is_custom);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001160 if (IP4_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +00001161 {
1162 rc = IP4_REASS_RC_HANDOFF;
1163 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001164 }
1165 else
1166 {
1167 if (consumed)
1168 {
1169 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +02001170 if (reass->fragments_n > rm->max_reass_len)
1171 {
1172 rc = IP4_REASS_RC_TOO_MANY_FRAGMENTS;
1173 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001174 }
1175 else
1176 {
Klement Sekera896c8962019-06-24 11:52:49 +00001177 *next0 = IP4_FULL_REASS_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +02001178 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
1179 }
1180 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001181 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +02001182}
1183
1184always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00001185ip4_full_reass_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1186 vlib_frame_t *frame, ip4_full_reass_node_type_t type,
1187 bool is_local)
Klement Sekera75e7d132017-09-20 08:26:30 +02001188{
1189 u32 *from = vlib_frame_vector_args (frame);
1190 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001191 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1192 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001193 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001194
1195 n_left_from = frame->n_vectors;
1196 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001197 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001198 {
1199 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1200
Klement Sekera75e7d132017-09-20 08:26:30 +02001201 while (n_left_from > 0 && n_left_to_next > 0)
1202 {
1203 u32 bi0;
1204 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +01001205 u32 next0;
1206 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +02001207
1208 bi0 = from[0];
1209 b0 = vlib_get_buffer (vm, bi0);
1210
1211 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001212 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +02001213 {
Klement Sekera4c533132018-02-22 11:41:12 +01001214 // this is a whole packet - no fragmentation
Klement Sekerafe8371f2020-09-10 12:03:54 +00001215 if (CUSTOM != type)
Klement Sekera4c533132018-02-22 11:41:12 +01001216 {
Klement Sekera896c8962019-06-24 11:52:49 +00001217 next0 = IP4_FULL_REASS_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +01001218 }
1219 else
1220 {
1221 next0 = vnet_buffer (b0)->ip.reass.next_index;
1222 }
Klement Sekera01c1fa42021-12-14 18:25:11 +00001223 ip4_full_reass_add_trace (vm, node, NULL, bi0, PASSTHROUGH, 0,
1224 ~0);
Klement Sekera896c8962019-06-24 11:52:49 +00001225 goto packet_enqueue;
1226 }
Klement Sekera01c1fa42021-12-14 18:25:11 +00001227
1228 if (is_local && !rm->is_local_reass_enabled)
1229 {
1230 next0 = IP4_FULL_REASS_NEXT_DROP;
1231 goto packet_enqueue;
1232 }
1233
Klement Sekera896c8962019-06-24 11:52:49 +00001234 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1235 const u32 fragment_length =
1236 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
1237 const u32 fragment_last = fragment_first + fragment_length - 1;
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001238
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +00001239 /* Keep track of received fragments */
1240 vlib_node_increment_counter (vm, node->node_index,
1241 IP4_ERROR_REASS_FRAGMENTS_RCVD, 1);
1242
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001243 if (fragment_first > fragment_last ||
1244 fragment_first + fragment_length > UINT16_MAX - 20 ||
1245 (fragment_length < 8 && // 8 is minimum frag length per RFC 791
1246 ip4_get_fragment_more (ip0)))
Klement Sekera896c8962019-06-24 11:52:49 +00001247 {
1248 next0 = IP4_FULL_REASS_NEXT_DROP;
1249 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
1250 goto packet_enqueue;
1251 }
1252 ip4_full_reass_kv_t kv;
1253 u8 do_handoff = 0;
1254
1255 kv.k.as_u64[0] =
1256 (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
1257 vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
1258 (u64) ip0->src_address.as_u32 << 32;
1259 kv.k.as_u64[1] =
1260 (u64) ip0->dst_address.
1261 as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
1262
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001263 ip4_full_reass_t *reass = ip4_full_reass_find_or_create (
1264 vm, node, rm, rt, &kv, &do_handoff, &n_left_to_next, &to_next);
Klement Sekera896c8962019-06-24 11:52:49 +00001265
1266 if (reass)
1267 {
1268 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1269 if (0 == fragment_first)
1270 {
1271 reass->sendout_thread_index = vm->thread_index;
1272 }
1273 }
1274
1275 if (PREDICT_FALSE (do_handoff))
1276 {
1277 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001278 vnet_buffer (b0)->ip.reass.owner_thread_index =
1279 kv.v.memory_owner_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001280 }
1281 else if (reass)
1282 {
1283 u32 handoff_thread_idx;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001284 u32 counter = ~0;
Klement Sekera896c8962019-06-24 11:52:49 +00001285 switch (ip4_full_reass_update
1286 (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001287 &error0, CUSTOM == type, &handoff_thread_idx))
Klement Sekera896c8962019-06-24 11:52:49 +00001288 {
1289 case IP4_REASS_RC_OK:
1290 /* nothing to do here */
1291 break;
1292 case IP4_REASS_RC_HANDOFF:
1293 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1294 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001295 vnet_buffer (b0)->ip.reass.owner_thread_index =
1296 handoff_thread_idx;
Klement Sekera896c8962019-06-24 11:52:49 +00001297 break;
1298 case IP4_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001299 counter = IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
Klement Sekera896c8962019-06-24 11:52:49 +00001300 break;
1301 case IP4_REASS_RC_NO_BUF:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001302 counter = IP4_ERROR_REASS_NO_BUF;
Klement Sekera896c8962019-06-24 11:52:49 +00001303 break;
1304 case IP4_REASS_RC_INTERNAL_ERROR:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001305 counter = IP4_ERROR_REASS_INTERNAL_ERROR;
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001306 /* Sanitization is needed in internal error cases only, as
1307 * the incoming packet is already dropped in other cases,
1308 * also adding bi0 back to the reassembly list, fixes the
1309 * leaking of buffers during internal errors.
1310 *
1311 * Also it doesnt make sense to send these buffers custom
1312 * app, these fragments are with internal errors */
1313 sanitize_reass_buffers_add_missing (vm, reass, &bi0);
1314 reass->error_next_index = ~0;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001315 break;
1316 }
1317
1318 if (~0 != counter)
1319 {
1320 vlib_node_increment_counter (vm, node->node_index, counter,
Klement Sekera896c8962019-06-24 11:52:49 +00001321 1);
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001322 ip4_full_reass_drop_all (vm, node, reass, &n_left_to_next,
1323 &to_next);
Klement Sekera896c8962019-06-24 11:52:49 +00001324 ip4_full_reass_free (rm, rt, reass);
1325 goto next_packet;
Klement Sekera896c8962019-06-24 11:52:49 +00001326 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001327 }
1328 else
1329 {
Klement Sekera896c8962019-06-24 11:52:49 +00001330 next0 = IP4_FULL_REASS_NEXT_DROP;
1331 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
Klement Sekera4c533132018-02-22 11:41:12 +01001332 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001333
Klement Sekera896c8962019-06-24 11:52:49 +00001334
1335 packet_enqueue:
Klement Sekera896c8962019-06-24 11:52:49 +00001336
Klement Sekera75e7d132017-09-20 08:26:30 +02001337 if (bi0 != ~0)
1338 {
1339 to_next[0] = bi0;
1340 to_next += 1;
1341 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001342
1343 /* bi0 might have been updated by reass_finalize, reload */
1344 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001345 if (IP4_ERROR_NONE != error0)
1346 {
1347 b0->error = node->errors[error0];
1348 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001349
Klement Sekera896c8962019-06-24 11:52:49 +00001350 if (next0 == IP4_FULL_REASS_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001351 {
1352 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1353 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001354 ip4_full_reass_add_trace (
1355 vm, node, NULL, bi0, HANDOFF, 0,
1356 vnet_buffer (b0)->ip.reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001357 }
1358 }
Klement Sekerafe8371f2020-09-10 12:03:54 +00001359 else if (FEATURE == type && IP4_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001360 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001361 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001362 }
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001363
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +00001364 /* Increment the counter to-custom-app also as this fragment is
1365 * also going to application */
1366 if (CUSTOM == type)
1367 {
1368 vlib_node_increment_counter (
1369 vm, node->node_index, IP4_ERROR_REASS_TO_CUSTOM_APP, 1);
1370 }
1371
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001372 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1373 to_next, n_left_to_next,
1374 bi0, next0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001375 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1376 }
1377
Klement Sekerad0f70a32018-12-14 17:24:13 +01001378 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001379 from += 1;
1380 n_left_from -= 1;
1381 }
1382
1383 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1384 }
1385
Klement Sekera4c533132018-02-22 11:41:12 +01001386 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001387 return frame->n_vectors;
1388}
1389
Klement Sekera896c8962019-06-24 11:52:49 +00001390static char *ip4_full_reass_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001391#define _(sym, string) string,
1392 foreach_ip4_error
1393#undef _
1394};
1395
Klement Sekera896c8962019-06-24 11:52:49 +00001396VLIB_NODE_FN (ip4_full_reass_node) (vlib_main_t * vm,
1397 vlib_node_runtime_t * node,
1398 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001399{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001400 return ip4_full_reass_inline (vm, node, frame, NORMAL, false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001401}
1402
Klement Sekera896c8962019-06-24 11:52:49 +00001403VLIB_REGISTER_NODE (ip4_full_reass_node) = {
1404 .name = "ip4-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001405 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001406 .format_trace = format_ip4_full_reass_trace,
1407 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1408 .error_strings = ip4_full_reass_error_strings,
1409 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001410 .next_nodes =
1411 {
Klement Sekera896c8962019-06-24 11:52:49 +00001412 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1413 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1414 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001415
Klement Sekera75e7d132017-09-20 08:26:30 +02001416 },
1417};
Klement Sekera75e7d132017-09-20 08:26:30 +02001418
Klement Sekera01c1fa42021-12-14 18:25:11 +00001419VLIB_NODE_FN (ip4_local_full_reass_node)
1420(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1421{
1422 return ip4_full_reass_inline (vm, node, frame, NORMAL, true /* is_local */);
1423}
1424
1425VLIB_REGISTER_NODE (ip4_local_full_reass_node) = {
1426 .name = "ip4-local-full-reassembly",
1427 .vector_size = sizeof (u32),
1428 .format_trace = format_ip4_full_reass_trace,
1429 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1430 .error_strings = ip4_full_reass_error_strings,
1431 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1432 .next_nodes =
1433 {
1434 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1435 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1436 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-local-full-reassembly-handoff",
1437
1438 },
1439};
1440
Klement Sekera896c8962019-06-24 11:52:49 +00001441VLIB_NODE_FN (ip4_full_reass_node_feature) (vlib_main_t * vm,
1442 vlib_node_runtime_t * node,
1443 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001444{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001445 return ip4_full_reass_inline (vm, node, frame, FEATURE,
1446 false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001447}
1448
Klement Sekera896c8962019-06-24 11:52:49 +00001449VLIB_REGISTER_NODE (ip4_full_reass_node_feature) = {
1450 .name = "ip4-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001451 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001452 .format_trace = format_ip4_full_reass_trace,
1453 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1454 .error_strings = ip4_full_reass_error_strings,
1455 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001456 .next_nodes =
1457 {
Klement Sekera896c8962019-06-24 11:52:49 +00001458 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1459 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1460 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001461 },
1462};
Klement Sekera4c533132018-02-22 11:41:12 +01001463
Klement Sekera896c8962019-06-24 11:52:49 +00001464VNET_FEATURE_INIT (ip4_full_reass_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001465 .arc_name = "ip4-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001466 .node_name = "ip4-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001467 .runs_before = VNET_FEATURES ("ip4-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001468 "ipsec4-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001469 .runs_after = 0,
1470};
Klement Sekera4c533132018-02-22 11:41:12 +01001471
Klement Sekerafe8371f2020-09-10 12:03:54 +00001472VLIB_NODE_FN (ip4_full_reass_node_custom) (vlib_main_t * vm,
1473 vlib_node_runtime_t * node,
1474 vlib_frame_t * frame)
1475{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001476 return ip4_full_reass_inline (vm, node, frame, CUSTOM, false /* is_local */);
Klement Sekerafe8371f2020-09-10 12:03:54 +00001477}
1478
Klement Sekerafe8371f2020-09-10 12:03:54 +00001479VLIB_REGISTER_NODE (ip4_full_reass_node_custom) = {
1480 .name = "ip4-full-reassembly-custom",
1481 .vector_size = sizeof (u32),
1482 .format_trace = format_ip4_full_reass_trace,
1483 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1484 .error_strings = ip4_full_reass_error_strings,
1485 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1486 .next_nodes =
1487 {
1488 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1489 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1490 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-custom-hoff",
1491 },
1492};
Klement Sekerafe8371f2020-09-10 12:03:54 +00001493
Klement Sekerafe8371f2020-09-10 12:03:54 +00001494VNET_FEATURE_INIT (ip4_full_reass_custom, static) = {
1495 .arc_name = "ip4-unicast",
1496 .node_name = "ip4-full-reassembly-feature",
1497 .runs_before = VNET_FEATURES ("ip4-lookup",
1498 "ipsec4-input-feature"),
1499 .runs_after = 0,
1500};
1501
Klement Sekerafe8371f2020-09-10 12:03:54 +00001502
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001503#ifndef CLIB_MARCH_VARIANT
Klement Sekerafe8371f2020-09-10 12:03:54 +00001504uword
1505ip4_full_reass_custom_register_next_node (uword node_index)
1506{
1507 return vlib_node_add_next (vlib_get_main (),
1508 ip4_full_reass_node_custom.index, node_index);
1509}
1510
Klement Sekera4c533132018-02-22 11:41:12 +01001511always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +00001512ip4_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001513{
Klement Sekera896c8962019-06-24 11:52:49 +00001514 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001515 u32 nbuckets;
1516 u8 i;
1517
Vijayabhaskar Katamreddy3fda2002022-05-16 22:18:37 +00001518 /* need more mem with more workers */
1519 nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
1520 IP4_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001521
1522 for (i = 0; i < 31; i++)
1523 if ((1 << i) >= nbuckets)
1524 break;
1525 nbuckets = 1 << i;
1526
1527 return nbuckets;
1528}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001529#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001530
1531typedef enum
1532{
1533 IP4_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001534} ip4_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001535
1536typedef struct
1537{
1538 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001539 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001540} ip4_rehash_cb_ctx;
1541
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001542#ifndef CLIB_MARCH_VARIANT
Neale Rannsf50bac12019-12-06 05:53:17 +00001543static int
Klement Sekera8dcfed52018-06-28 11:16:15 +02001544ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001545{
1546 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001547 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001548 {
1549 ctx->failure = 1;
1550 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001551 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001552}
1553
Klement Sekera4c533132018-02-22 11:41:12 +01001554static void
Klement Sekera896c8962019-06-24 11:52:49 +00001555ip4_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1556 u32 max_reassembly_length,
1557 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001558{
Klement Sekera896c8962019-06-24 11:52:49 +00001559 ip4_full_reass_main.timeout_ms = timeout_ms;
1560 ip4_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1561 ip4_full_reass_main.max_reass_n = max_reassemblies;
1562 ip4_full_reass_main.max_reass_len = max_reassembly_length;
1563 ip4_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001564}
1565
Klement Sekera75e7d132017-09-20 08:26:30 +02001566vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001567ip4_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1568 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001569{
Klement Sekera896c8962019-06-24 11:52:49 +00001570 u32 old_nbuckets = ip4_full_reass_get_nbuckets ();
1571 ip4_full_reass_set_params (timeout_ms, max_reassemblies,
1572 max_reassembly_length, expire_walk_interval_ms);
1573 vlib_process_signal_event (ip4_full_reass_main.vlib_main,
1574 ip4_full_reass_main.ip4_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001575 IP4_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001576 u32 new_nbuckets = ip4_full_reass_get_nbuckets ();
1577 if (ip4_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001578 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001579 clib_bihash_16_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001580 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001581 ip4_rehash_cb_ctx ctx;
1582 ctx.failure = 0;
1583 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001584 clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001585 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001586 clib_bihash_foreach_key_value_pair_16_8 (&ip4_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001587 ip4_rehash_cb, &ctx);
1588 if (ctx.failure)
1589 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001590 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001591 return -1;
1592 }
1593 else
1594 {
Klement Sekera896c8962019-06-24 11:52:49 +00001595 clib_bihash_free_16_8 (&ip4_full_reass_main.hash);
1596 clib_memcpy_fast (&ip4_full_reass_main.hash, &new_hash,
1597 sizeof (ip4_full_reass_main.hash));
1598 clib_bihash_copied (&ip4_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001599 }
1600 }
1601 return 0;
1602}
1603
1604vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001605ip4_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1606 u32 * max_reassembly_length,
1607 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001608{
Klement Sekera896c8962019-06-24 11:52:49 +00001609 *timeout_ms = ip4_full_reass_main.timeout_ms;
1610 *max_reassemblies = ip4_full_reass_main.max_reass_n;
1611 *max_reassembly_length = ip4_full_reass_main.max_reass_len;
1612 *expire_walk_interval_ms = ip4_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001613 return 0;
1614}
1615
Klement Sekera4c533132018-02-22 11:41:12 +01001616static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001617ip4_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001618{
Klement Sekera896c8962019-06-24 11:52:49 +00001619 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001620 clib_error_t *error = 0;
1621 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001622 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001623
1624 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001625
Juraj Slobodacd806922018-10-10 10:15:54 +02001626 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001627 ip4_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001628 vec_foreach (rt, rm->per_thread_data)
1629 {
1630 clib_spinlock_init (&rt->lock);
1631 pool_alloc (rt->pool, rm->max_reass_n);
1632 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001633
Klement Sekera896c8962019-06-24 11:52:49 +00001634 node = vlib_get_node_by_name (vm, (u8 *) "ip4-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001635 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001636 rm->ip4_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001637
Klement Sekera896c8962019-06-24 11:52:49 +00001638 ip4_full_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1639 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1640 IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1641 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001642
Klement Sekera896c8962019-06-24 11:52:49 +00001643 nbuckets = ip4_full_reass_get_nbuckets ();
1644 clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001645
Klement Sekera896c8962019-06-24 11:52:49 +00001646 rm->fq_index = vlib_frame_queue_main_init (ip4_full_reass_node.index, 0);
Klement Sekera01c1fa42021-12-14 18:25:11 +00001647 rm->fq_local_index =
1648 vlib_frame_queue_main_init (ip4_local_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001649 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001650 vlib_frame_queue_main_init (ip4_full_reass_node_feature.index, 0);
Klement Sekerafe8371f2020-09-10 12:03:54 +00001651 rm->fq_custom_index =
1652 vlib_frame_queue_main_init (ip4_full_reass_node_custom.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001653
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001654 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera01c1fa42021-12-14 18:25:11 +00001655 rm->is_local_reass_enabled = 1;
1656
Klement Sekera75e7d132017-09-20 08:26:30 +02001657 return error;
1658}
1659
Klement Sekera896c8962019-06-24 11:52:49 +00001660VLIB_INIT_FUNCTION (ip4_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001661#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001662
1663static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001664ip4_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1665 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekera75e7d132017-09-20 08:26:30 +02001666{
Klement Sekera896c8962019-06-24 11:52:49 +00001667 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001668 uword event_type, *event_data = 0;
1669
1670 while (true)
1671 {
1672 vlib_process_wait_for_event_or_clock (vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001673 (f64)
1674 rm->expire_walk_interval_ms /
1675 (f64) MSEC_PER_SEC);
Klement Sekera75e7d132017-09-20 08:26:30 +02001676 event_type = vlib_process_get_events (vm, &event_data);
1677
1678 switch (event_type)
1679 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001680 case ~0:
1681 /* no events => timeout */
1682 /* fallthrough */
Klement Sekera75e7d132017-09-20 08:26:30 +02001683 case IP4_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001684 /* nothing to do here */
Klement Sekera75e7d132017-09-20 08:26:30 +02001685 break;
1686 default:
1687 clib_warning ("BUG: event type 0x%wx", event_type);
1688 break;
1689 }
1690 f64 now = vlib_time_now (vm);
1691
Klement Sekera896c8962019-06-24 11:52:49 +00001692 ip4_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001693 int *pool_indexes_to_free = NULL;
1694
Klement Sekera4c533132018-02-22 11:41:12 +01001695 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001696 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001697 const uword nthreads = vlib_num_workers () + 1;
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001698 u32 n_left_to_next, *to_next;
1699
Klement Sekera4c533132018-02-22 11:41:12 +01001700 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1701 {
Klement Sekera896c8962019-06-24 11:52:49 +00001702 ip4_full_reass_per_thread_t *rt =
1703 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001704 clib_spinlock_lock (&rt->lock);
1705
1706 vec_reset_length (pool_indexes_to_free);
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001707
Vijayabhaskar Katamreddy8b874fc2022-05-13 13:07:19 +00001708 /* Pace the number of timeouts handled per thread,to avoid barrier
1709 * sync issues in real world scenarios */
1710
1711 u32 beg = rt->last_id;
1712 /* to ensure we walk at least once per sec per context */
1713 u32 end =
1714 beg + (IP4_REASS_MAX_REASSEMBLIES_DEFAULT *
1715 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS / MSEC_PER_SEC +
1716 1);
1717 if (end > vec_len (rt->pool))
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001718 {
Vijayabhaskar Katamreddy8b874fc2022-05-13 13:07:19 +00001719 end = vec_len (rt->pool);
1720 rt->last_id = 0;
1721 }
1722 else
1723 {
1724 rt->last_id = end;
1725 }
1726
Vijayabhaskar Katamreddybeafecf2022-05-19 17:07:22 +00001727 pool_foreach_stepping_index (index, beg, end, rt->pool)
1728 {
1729 reass = pool_elt_at_index (rt->pool, index);
1730 if (now > reass->last_heard + rm->timeout)
1731 {
1732 vec_add1 (pool_indexes_to_free, index);
1733 }
1734 }
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001735
Vijayabhaskar Katamreddy14a74422022-05-18 14:31:03 +00001736 if (vec_len (pool_indexes_to_free))
1737 vlib_node_increment_counter (vm, node->node_index,
1738 IP4_ERROR_REASS_TIMEOUT,
1739 vec_len (pool_indexes_to_free));
Klement Sekera4c533132018-02-22 11:41:12 +01001740 int *i;
Klement Sekera4c533132018-02-22 11:41:12 +01001741 vec_foreach (i, pool_indexes_to_free)
1742 {
Klement Sekera896c8962019-06-24 11:52:49 +00001743 ip4_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Vijayabhaskar Katamreddye0f901a2022-05-09 14:13:07 +00001744 ip4_full_reass_drop_all (vm, node, reass, &n_left_to_next,
1745 &to_next);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001746 ip4_full_reass_free (rm, rt, reass);
1747 }
Klement Sekera4c533132018-02-22 11:41:12 +01001748
1749 clib_spinlock_unlock (&rt->lock);
1750 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001751
Klement Sekera75e7d132017-09-20 08:26:30 +02001752 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001753 if (event_data)
1754 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001755 vec_set_len (event_data, 0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001756 }
1757 }
1758
1759 return 0;
1760}
1761
Klement Sekera896c8962019-06-24 11:52:49 +00001762VLIB_REGISTER_NODE (ip4_full_reass_expire_node) = {
1763 .function = ip4_full_reass_walk_expired,
Klement Sekera75e7d132017-09-20 08:26:30 +02001764 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001765 .name = "ip4-full-reassembly-expire-walk",
1766 .format_trace = format_ip4_full_reass_trace,
1767 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1768 .error_strings = ip4_full_reass_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001769
1770};
Klement Sekera75e7d132017-09-20 08:26:30 +02001771
1772static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001773format_ip4_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001774{
Klement Sekera896c8962019-06-24 11:52:49 +00001775 ip4_full_reass_key_t *key = va_arg (*args, ip4_full_reass_key_t *);
1776 s =
1777 format (s,
1778 "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1779 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1780 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
Klement Sekera75e7d132017-09-20 08:26:30 +02001781 return s;
1782}
1783
1784static u8 *
1785format_ip4_reass (u8 * s, va_list * args)
1786{
1787 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001788 ip4_full_reass_t *reass = va_arg (*args, ip4_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001789
Klement Sekera4c533132018-02-22 11:41:12 +01001790 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001791 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001792 reass->id, format_ip4_full_reass_key, &reass->key,
1793 reass->first_bi, reass->data_len,
1794 reass->last_packet_octet, reass->trace_op_counter);
1795
Klement Sekera75e7d132017-09-20 08:26:30 +02001796 u32 bi = reass->first_bi;
1797 u32 counter = 0;
1798 while (~0 != bi)
1799 {
1800 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1801 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera896c8962019-06-24 11:52:49 +00001802 s =
1803 format (s,
1804 " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1805 "fragment[%u, %u]\n", counter, vnb->ip.reass.range_first,
1806 vnb->ip.reass.range_last, bi,
1807 ip4_full_reass_buffer_get_data_offset (b),
1808 ip4_full_reass_buffer_get_data_len (b),
1809 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
Klement Sekera75e7d132017-09-20 08:26:30 +02001810 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1811 {
1812 bi = b->next_buffer;
1813 }
1814 else
1815 {
1816 bi = ~0;
1817 }
1818 }
1819 return s;
1820}
1821
1822static clib_error_t *
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001823show_ip4_reass (vlib_main_t * vm,
1824 unformat_input_t * input,
Klement Sekera75e7d132017-09-20 08:26:30 +02001825 CLIB_UNUSED (vlib_cli_command_t * lmd))
1826{
Klement Sekera896c8962019-06-24 11:52:49 +00001827 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001828
1829 vlib_cli_output (vm, "---------------------");
1830 vlib_cli_output (vm, "IP4 reassembly status");
1831 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001832 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001833 if (unformat (input, "details"))
1834 {
Klement Sekera4c533132018-02-22 11:41:12 +01001835 details = true;
1836 }
1837
1838 u32 sum_reass_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001839 ip4_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001840 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001841 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001842 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1843 {
Klement Sekera896c8962019-06-24 11:52:49 +00001844 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001845 clib_spinlock_lock (&rt->lock);
1846 if (details)
1847 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001848 pool_foreach (reass, rt->pool) {
Klement Sekera4c533132018-02-22 11:41:12 +01001849 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001850 }
Klement Sekera4c533132018-02-22 11:41:12 +01001851 }
1852 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001853 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001854 }
1855 vlib_cli_output (vm, "---------------------");
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001856 vlib_cli_output (vm, "Current full IP4 reassemblies count: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001857 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001858 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001859 "Maximum configured concurrent full IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001860 (long unsigned) rm->max_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001861 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001862 "Maximum configured amount of fragments "
1863 "per full IP4 reassembly: %lu\n",
1864 (long unsigned) rm->max_reass_len);
1865 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001866 "Maximum configured full IP4 reassembly timeout: %lums\n",
1867 (long unsigned) rm->timeout_ms);
1868 vlib_cli_output (vm,
1869 "Maximum configured full IP4 reassembly expire walk interval: %lums\n",
1870 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001871 return 0;
1872}
1873
Klement Sekera896c8962019-06-24 11:52:49 +00001874VLIB_CLI_COMMAND (show_ip4_full_reass_cmd, static) = {
1875 .path = "show ip4-full-reassembly",
1876 .short_help = "show ip4-full-reassembly [details]",
Klement Sekera75e7d132017-09-20 08:26:30 +02001877 .function = show_ip4_reass,
1878};
Klement Sekera75e7d132017-09-20 08:26:30 +02001879
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001880#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001881vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001882ip4_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001883{
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001884 return vnet_feature_enable_disable ("ip4-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001885 "ip4-full-reassembly-feature",
1886 sw_if_index, enable_disable, 0, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001887}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001888#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001889
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001890
Klement Sekera896c8962019-06-24 11:52:49 +00001891#define foreach_ip4_full_reass_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001892_(CONGESTION_DROP, "congestion drop")
1893
1894
1895typedef enum
1896{
Klement Sekera896c8962019-06-24 11:52:49 +00001897#define _(sym,str) IP4_FULL_REASS_HANDOFF_ERROR_##sym,
1898 foreach_ip4_full_reass_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001899#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001900 IP4_FULL_REASS_HANDOFF_N_ERROR,
1901} ip4_full_reass_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001902
Klement Sekera896c8962019-06-24 11:52:49 +00001903static char *ip4_full_reass_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001904#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001905 foreach_ip4_full_reass_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001906#undef _
1907};
1908
1909typedef struct
1910{
1911 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001912} ip4_full_reass_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001913
1914static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001915format_ip4_full_reass_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001916{
1917 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1918 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001919 ip4_full_reass_handoff_trace_t *t =
1920 va_arg (*args, ip4_full_reass_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001921
1922 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001923 format (s, "ip4-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001924 t->next_worker_index);
1925
1926 return s;
1927}
1928
1929always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00001930ip4_full_reass_handoff_node_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1931 vlib_frame_t *frame,
1932 ip4_full_reass_node_type_t type,
1933 bool is_local)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001934{
Klement Sekera896c8962019-06-24 11:52:49 +00001935 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001936
1937 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1938 u32 n_enq, n_left_from, *from;
1939 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1940 u32 fq_index;
1941
1942 from = vlib_frame_vector_args (frame);
1943 n_left_from = frame->n_vectors;
1944 vlib_get_buffers (vm, from, bufs, n_left_from);
1945
1946 b = bufs;
1947 ti = thread_indices;
1948
Klement Sekerafe8371f2020-09-10 12:03:54 +00001949 switch (type)
1950 {
1951 case NORMAL:
Klement Sekera01c1fa42021-12-14 18:25:11 +00001952 if (is_local)
1953 {
1954 fq_index = rm->fq_local_index;
1955 }
1956 else
1957 {
1958 fq_index = rm->fq_index;
1959 }
Klement Sekerafe8371f2020-09-10 12:03:54 +00001960 break;
1961 case FEATURE:
1962 fq_index = rm->fq_feature_index;
1963 break;
1964 case CUSTOM:
1965 fq_index = rm->fq_custom_index;
1966 break;
1967 default:
1968 clib_warning ("Unexpected `type' (%d)!", type);
Klement Sekerafe8371f2020-09-10 12:03:54 +00001969 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001970
1971 while (n_left_from > 0)
1972 {
Klement Sekerade34c352019-06-25 11:19:22 +00001973 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001974
1975 if (PREDICT_FALSE
1976 ((node->flags & VLIB_NODE_FLAG_TRACE)
1977 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1978 {
Klement Sekera896c8962019-06-24 11:52:49 +00001979 ip4_full_reass_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001980 vlib_add_trace (vm, node, b[0], sizeof (*t));
1981 t->next_worker_index = ti[0];
1982 }
1983
1984 n_left_from -= 1;
1985 ti += 1;
1986 b += 1;
1987 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001988 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1989 thread_indices, frame->n_vectors, 1);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001990
1991 if (n_enq < frame->n_vectors)
1992 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001993 IP4_FULL_REASS_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001994 frame->n_vectors - n_enq);
1995 return frame->n_vectors;
1996}
1997
Klement Sekera896c8962019-06-24 11:52:49 +00001998VLIB_NODE_FN (ip4_full_reass_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001999 vlib_node_runtime_t * node,
2000 vlib_frame_t * frame)
2001{
Klement Sekera01c1fa42021-12-14 18:25:11 +00002002 return ip4_full_reass_handoff_node_inline (vm, node, frame, NORMAL,
2003 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002004}
2005
2006
Klement Sekera896c8962019-06-24 11:52:49 +00002007VLIB_REGISTER_NODE (ip4_full_reass_handoff_node) = {
2008 .name = "ip4-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002009 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00002010 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
2011 .error_strings = ip4_full_reass_handoff_error_strings,
2012 .format_trace = format_ip4_full_reass_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002013
2014 .n_next_nodes = 1,
2015
2016 .next_nodes = {
2017 [0] = "error-drop",
2018 },
2019};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002020
Klement Sekera01c1fa42021-12-14 18:25:11 +00002021VLIB_NODE_FN (ip4_local_full_reass_handoff_node)
2022(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2023{
2024 return ip4_full_reass_handoff_node_inline (vm, node, frame, NORMAL,
2025 true /* is_local */);
2026}
2027
2028VLIB_REGISTER_NODE (ip4_local_full_reass_handoff_node) = {
2029 .name = "ip4-local-full-reassembly-handoff",
2030 .vector_size = sizeof (u32),
2031 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
2032 .error_strings = ip4_full_reass_handoff_error_strings,
2033 .format_trace = format_ip4_full_reass_handoff_trace,
2034
2035 .n_next_nodes = 1,
2036
2037 .next_nodes = {
2038 [0] = "error-drop",
2039 },
2040};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002041
Klement Sekera896c8962019-06-24 11:52:49 +00002042VLIB_NODE_FN (ip4_full_reass_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002043 vlib_node_runtime_t *
2044 node,
2045 vlib_frame_t * frame)
2046{
Klement Sekera01c1fa42021-12-14 18:25:11 +00002047 return ip4_full_reass_handoff_node_inline (vm, node, frame, FEATURE,
2048 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002049}
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002050
Klement Sekera896c8962019-06-24 11:52:49 +00002051VLIB_REGISTER_NODE (ip4_full_reass_feature_handoff_node) = {
2052 .name = "ip4-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002053 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00002054 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
2055 .error_strings = ip4_full_reass_handoff_error_strings,
2056 .format_trace = format_ip4_full_reass_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002057
2058 .n_next_nodes = 1,
2059
2060 .next_nodes = {
2061 [0] = "error-drop",
2062 },
2063};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002064
Klement Sekerafe8371f2020-09-10 12:03:54 +00002065VLIB_NODE_FN (ip4_full_reass_custom_handoff_node) (vlib_main_t * vm,
2066 vlib_node_runtime_t *
2067 node,
2068 vlib_frame_t * frame)
2069{
Klement Sekera01c1fa42021-12-14 18:25:11 +00002070 return ip4_full_reass_handoff_node_inline (vm, node, frame, CUSTOM,
2071 false /* is_local */);
Klement Sekerafe8371f2020-09-10 12:03:54 +00002072}
Klement Sekerafe8371f2020-09-10 12:03:54 +00002073
Klement Sekerafe8371f2020-09-10 12:03:54 +00002074VLIB_REGISTER_NODE (ip4_full_reass_custom_handoff_node) = {
2075 .name = "ip4-full-reass-custom-hoff",
2076 .vector_size = sizeof (u32),
2077 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
2078 .error_strings = ip4_full_reass_handoff_error_strings,
2079 .format_trace = format_ip4_full_reass_handoff_trace,
2080
2081 .n_next_nodes = 1,
2082
2083 .next_nodes = {
2084 [0] = "error-drop",
2085 },
2086};
Klement Sekerafe8371f2020-09-10 12:03:54 +00002087
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002088#ifndef CLIB_MARCH_VARIANT
2089int
2090ip4_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
2091{
2092 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
2093 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
2094 if (is_enable)
2095 {
2096 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2097 {
2098 ++rm->feature_use_refcount_per_intf[sw_if_index];
2099 return vnet_feature_enable_disable ("ip4-unicast",
2100 "ip4-full-reassembly-feature",
2101 sw_if_index, 1, 0, 0);
2102 }
2103 ++rm->feature_use_refcount_per_intf[sw_if_index];
2104 }
2105 else
2106 {
2107 --rm->feature_use_refcount_per_intf[sw_if_index];
2108 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2109 return vnet_feature_enable_disable ("ip4-unicast",
2110 "ip4-full-reassembly-feature",
2111 sw_if_index, 0, 0, 0);
2112 }
2113 return -1;
2114}
Klement Sekera01c1fa42021-12-14 18:25:11 +00002115
2116void
2117ip4_local_full_reass_enable_disable (int enable)
2118{
2119 if (enable)
2120 {
2121 ip4_full_reass_main.is_local_reass_enabled = 1;
2122 }
2123 else
2124 {
2125 ip4_full_reass_main.is_local_reass_enabled = 0;
2126 }
2127}
2128
2129int
2130ip4_local_full_reass_enabled ()
2131{
2132 return ip4_full_reass_main.is_local_reass_enabled;
2133}
2134
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002135#endif
2136
Klement Sekera75e7d132017-09-20 08:26:30 +02002137/*
2138 * fd.io coding-style-patch-verification: ON
2139 *
2140 * Local Variables:
2141 * eval: (c-set-style "gnu")
2142 * End:
2143 */