blob: d2069c0876c0c4e3bf9b41047a5b51cec4b353f4 [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
32#define IP4_REASS_TIMEOUT_DEFAULT_MS 100
33#define IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
Klement Sekera4c533132018-02-22 11:41:12 +010034#define IP4_REASS_MAX_REASSEMBLIES_DEFAULT 1024
Klement Sekera3a343d42019-05-16 14:35:46 +020035#define IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
Klement Sekera75e7d132017-09-20 08:26:30 +020036#define IP4_REASS_HT_LOAD_FACTOR (0.75)
37
38#define IP4_REASS_DEBUG_BUFFERS 0
39#if IP4_REASS_DEBUG_BUFFERS
40#define IP4_REASS_DEBUG_BUFFER(bi, what) \
41 do \
42 { \
43 u32 _bi = bi; \
44 printf (#what "buffer %u", _bi); \
45 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
46 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \
47 { \
48 _bi = _b->next_buffer; \
49 printf ("[%u]", _bi); \
50 _b = vlib_get_buffer (vm, _bi); \
51 } \
52 printf ("\n"); \
53 fflush (stdout); \
54 } \
55 while (0)
56#else
57#define IP4_REASS_DEBUG_BUFFER(...)
58#endif
59
Klement Sekerad0f70a32018-12-14 17:24:13 +010060typedef enum
61{
62 IP4_REASS_RC_OK,
Klement Sekera3a343d42019-05-16 14:35:46 +020063 IP4_REASS_RC_TOO_MANY_FRAGMENTS,
Klement Sekerad0f70a32018-12-14 17:24:13 +010064 IP4_REASS_RC_INTERNAL_ERROR,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010065 IP4_REASS_RC_NO_BUF,
Klement Sekera630ab582019-07-19 09:14:19 +000066 IP4_REASS_RC_HANDOFF,
Klement Sekera896c8962019-06-24 11:52:49 +000067} ip4_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020068
69typedef struct
70{
71 union
72 {
73 struct
74 {
Klement Sekera75e7d132017-09-20 08:26:30 +020075 u32 xx_id;
76 ip4_address_t src;
77 ip4_address_t dst;
Klement Sekera8dcfed52018-06-28 11:16:15 +020078 u16 frag_id;
79 u8 proto;
80 u8 unused;
Klement Sekera75e7d132017-09-20 08:26:30 +020081 };
Klement Sekera8dcfed52018-06-28 11:16:15 +020082 u64 as_u64[2];
Klement Sekera75e7d132017-09-20 08:26:30 +020083 };
Klement Sekera896c8962019-06-24 11:52:49 +000084} ip4_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020085
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080086typedef union
87{
88 struct
89 {
90 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000091 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080092 };
93 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000094} ip4_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080095
96typedef union
97{
98 struct
99 {
Klement Sekera896c8962019-06-24 11:52:49 +0000100 ip4_full_reass_key_t k;
101 ip4_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800102 };
103 clib_bihash_kv_16_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +0000104} ip4_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800105
Klement Sekera75e7d132017-09-20 08:26:30 +0200106always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +0000107ip4_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +0200108{
109 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100110 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +0200111}
112
113always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +0000114ip4_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +0200115{
116 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100117 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +0000118 (vnb->ip.reass.fragment_first +
119 ip4_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +0200120}
121
122typedef struct
123{
124 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000125 ip4_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200126 // time when last packet was received
127 f64 last_heard;
128 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100129 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200130 // buffer index of first buffer in this reassembly context
131 u32 first_bi;
132 // last octet of packet, ~0 until fragment without more_fragments arrives
133 u32 last_packet_octet;
134 // length of data collected so far
135 u32 data_len;
136 // trace operation counter
137 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100138 // next index - used by non-feature node
Klement Sekera21aa8f12019-05-20 12:27:33 +0200139 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000140 // error next index - used by custom apps (~0 if not used)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200141 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100142 // minimum fragment length for this reassembly - used to estimate MTU
143 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200144 // number of fragments in this reassembly
145 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000146 // thread owning memory for this context (whose pool contains this ctx)
147 u32 memory_owner_thread_index;
148 // thread which received fragment with offset 0 and which sends out the
149 // completed reassembly
150 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000151} ip4_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200152
153typedef struct
154{
Klement Sekera896c8962019-06-24 11:52:49 +0000155 ip4_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100156 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100157 u32 id_counter;
158 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000159} ip4_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100160
161typedef struct
162{
Klement Sekera75e7d132017-09-20 08:26:30 +0200163 // IPv4 config
164 u32 timeout_ms;
165 f64 timeout;
166 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200167 // maximum number of fragments in one reassembly
168 u32 max_reass_len;
169 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200170 u32 max_reass_n;
171
172 // IPv4 runtime
Klement Sekera8dcfed52018-06-28 11:16:15 +0200173 clib_bihash_16_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100174 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000175 ip4_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200176
177 // convenience
178 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200179
180 // node index of ip4-drop node
181 u32 ip4_drop_idx;
Klement Sekera896c8962019-06-24 11:52:49 +0000182 u32 ip4_full_reass_expire_node_idx;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800183
184 /** Worker handoff */
185 u32 fq_index;
186 u32 fq_feature_index;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000187 u32 fq_custom_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200188
Klement Sekera7b2e9fb2019-10-01 13:00:22 +0000189 // reference count for enabling/disabling feature - per interface
190 u32 *feature_use_refcount_per_intf;
Klement Sekera896c8962019-06-24 11:52:49 +0000191} ip4_full_reass_main_t;
192
193extern ip4_full_reass_main_t ip4_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700194
195#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000196ip4_full_reass_main_t ip4_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700197#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200198
199typedef enum
200{
Klement Sekera896c8962019-06-24 11:52:49 +0000201 IP4_FULL_REASS_NEXT_INPUT,
202 IP4_FULL_REASS_NEXT_DROP,
203 IP4_FULL_REASS_NEXT_HANDOFF,
204 IP4_FULL_REASS_N_NEXT,
205} ip4_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200206
207typedef enum
208{
Klement Sekerafe8371f2020-09-10 12:03:54 +0000209 NORMAL,
210 FEATURE,
211 CUSTOM
212} ip4_full_reass_node_type_t;
213
214typedef enum
215{
Klement Sekera75e7d132017-09-20 08:26:30 +0200216 RANGE_NEW,
217 RANGE_SHRINK,
218 RANGE_DISCARD,
219 RANGE_OVERLAP,
220 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000221 HANDOFF,
Klement Sekera896c8962019-06-24 11:52:49 +0000222} ip4_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200223
224typedef struct
225{
226 u16 range_first;
227 u16 range_last;
228 u32 range_bi;
229 i32 data_offset;
230 u32 data_len;
231 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000232} ip4_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200233
234typedef struct
235{
Klement Sekera896c8962019-06-24 11:52:49 +0000236 ip4_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200237 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000238 ip4_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200239 u32 size_diff;
240 u32 op_id;
Klement Sekera630ab582019-07-19 09:14:19 +0000241 u32 thread_id;
242 u32 thread_id_to;
Klement Sekera75e7d132017-09-20 08:26:30 +0200243 u32 fragment_first;
244 u32 fragment_last;
245 u32 total_data_len;
Klement Sekera8563cb32019-10-10 17:03:57 +0000246 bool is_after_handoff;
247 ip4_header_t ip4_header;
Klement Sekera896c8962019-06-24 11:52:49 +0000248} ip4_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200249
Klement Sekera896c8962019-06-24 11:52:49 +0000250extern vlib_node_registration_t ip4_full_reass_node;
251extern vlib_node_registration_t ip4_full_reass_node_feature;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000252extern vlib_node_registration_t ip4_full_reass_node_custom;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700253
Klement Sekera4c533132018-02-22 11:41:12 +0100254static void
Klement Sekera896c8962019-06-24 11:52:49 +0000255ip4_full_reass_trace_details (vlib_main_t * vm, u32 bi,
256 ip4_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200257{
258 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
259 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
260 trace->range_first = vnb->ip.reass.range_first;
261 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000262 trace->data_offset = ip4_full_reass_buffer_get_data_offset (b);
263 trace->data_len = ip4_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200264 trace->range_bi = bi;
265}
266
Klement Sekera4c533132018-02-22 11:41:12 +0100267static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000268format_ip4_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200269{
Klement Sekera896c8962019-06-24 11:52:49 +0000270 ip4_full_reass_range_trace_t *trace =
271 va_arg (*args, ip4_full_reass_range_trace_t *);
272 s =
273 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
274 trace->range_last, trace->data_offset, trace->data_len,
275 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200276 return s;
277}
278
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700279static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000280format_ip4_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200281{
282 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
283 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000284 ip4_full_reass_trace_t *t = va_arg (*args, ip4_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000285 u32 indent = 0;
286 if (~0 != t->reass_id)
287 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000288 if (t->is_after_handoff)
289 {
290 s =
291 format (s, "%U\n", format_ip4_header, &t->ip4_header,
292 sizeof (t->ip4_header));
293 indent = 2;
294 }
295 s =
296 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
297 t->reass_id, t->op_id);
Klement Sekera630ab582019-07-19 09:14:19 +0000298 indent = format_get_indent (s);
299 s =
300 format (s,
301 "first bi: %u, data len: %u, ip/fragment[%u, %u]",
302 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
303 t->fragment_last);
304 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200305 switch (t->action)
306 {
307 case RANGE_SHRINK:
308 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000309 format_ip4_full_reass_range_trace, &t->trace_range,
Klement Sekera75e7d132017-09-20 08:26:30 +0200310 t->size_diff);
311 break;
312 case RANGE_DISCARD:
313 s = format (s, "\n%Udiscard %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000314 format_ip4_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200315 break;
316 case RANGE_NEW:
317 s = format (s, "\n%Unew %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 break;
320 case RANGE_OVERLAP:
321 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000322 format_ip4_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200323 break;
324 case FINALIZE:
325 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
326 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000327 case HANDOFF:
328 s =
329 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
330 t->thread_id_to);
331 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200332 }
333 return s;
334}
335
Klement Sekera4c533132018-02-22 11:41:12 +0100336static void
Klement Sekera896c8962019-06-24 11:52:49 +0000337ip4_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000338 ip4_full_reass_t * reass, u32 bi,
339 ip4_full_reass_trace_operation_e action,
340 u32 size_diff, u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200341{
342 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
343 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera53be16d2020-12-15 21:47:36 +0100344 if (pool_is_free_index
345 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
346 {
347 // this buffer's trace is gone
348 b->flags &= ~VLIB_BUFFER_IS_TRACED;
349 return;
350 }
Klement Sekera8563cb32019-10-10 17:03:57 +0000351 bool is_after_handoff = false;
352 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
353 {
354 is_after_handoff = true;
355 }
Klement Sekera896c8962019-06-24 11:52:49 +0000356 ip4_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000357 t->is_after_handoff = is_after_handoff;
358 if (t->is_after_handoff)
359 {
360 clib_memcpy (&t->ip4_header, vlib_buffer_get_current (b),
361 clib_min (sizeof (t->ip4_header), b->current_length));
362 }
Klement Sekera896c8962019-06-24 11:52:49 +0000363 if (reass)
364 {
365 t->reass_id = reass->id;
366 t->op_id = reass->trace_op_counter;
367 t->trace_range.first_bi = reass->first_bi;
368 t->total_data_len = reass->data_len;
369 ++reass->trace_op_counter;
370 }
371 else
372 {
373 t->reass_id = ~0;
374 t->op_id = 0;
375 t->trace_range.first_bi = 0;
376 t->total_data_len = 0;
377 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200378 t->action = action;
Klement Sekera896c8962019-06-24 11:52:49 +0000379 ip4_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200380 t->size_diff = size_diff;
Klement Sekera630ab582019-07-19 09:14:19 +0000381 t->thread_id = vm->thread_index;
382 t->thread_id_to = thread_id_to;
Klement Sekera75e7d132017-09-20 08:26:30 +0200383 t->fragment_first = vnb->ip.reass.fragment_first;
384 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200385#if 0
386 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000387 s = format (s, "%U", format_ip4_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200388 printf ("%.*s\n", vec_len (s), s);
389 fflush (stdout);
390 vec_reset_length (s);
391#endif
392}
393
Klement Sekera630ab582019-07-19 09:14:19 +0000394always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000395ip4_full_reass_free_ctx (ip4_full_reass_per_thread_t * rt,
396 ip4_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000397{
398 pool_put (rt->pool, reass);
399 --rt->reass_n;
400}
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800401
Klement Sekera4c533132018-02-22 11:41:12 +0100402always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000403ip4_full_reass_free (ip4_full_reass_main_t * rm,
404 ip4_full_reass_per_thread_t * rt,
405 ip4_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200406{
Klement Sekera8dcfed52018-06-28 11:16:15 +0200407 clib_bihash_kv_16_8_t kv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200408 kv.key[0] = reass->key.as_u64[0];
409 kv.key[1] = reass->key.as_u64[1];
Klement Sekera8dcfed52018-06-28 11:16:15 +0200410 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000411 return ip4_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200412}
413
Klement Sekera4c533132018-02-22 11:41:12 +0100414always_inline void
Klement Sekera42cec0e2021-08-02 16:14:15 +0200415ip4_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
416 ip4_full_reass_t *reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200417{
418 u32 range_bi = reass->first_bi;
419 vlib_buffer_t *range_b;
420 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100421 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200422 while (~0 != range_bi)
423 {
424 range_b = vlib_get_buffer (vm, range_bi);
425 range_vnb = vnet_buffer (range_b);
426 u32 bi = range_bi;
427 while (~0 != bi)
428 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100429 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200430 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
431 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
432 {
433 bi = b->next_buffer;
434 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
435 }
436 else
437 {
438 bi = ~0;
439 }
440 }
441 range_bi = range_vnb->ip.reass.next_range_bi;
442 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200443 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000444 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200445 {
446 u32 n_left_to_next, *to_next, next_index;
447
448 next_index = reass->error_next_index;
449 u32 bi = ~0;
450
451 while (vec_len (to_free) > 0)
452 {
453 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
454
455 while (vec_len (to_free) > 0 && n_left_to_next > 0)
456 {
457 bi = vec_pop (to_free);
458
459 if (~0 != bi)
460 {
461 to_next[0] = bi;
462 to_next += 1;
463 n_left_to_next -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200464 }
465 }
466 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
467 }
468 }
469 else
470 {
471 vlib_buffer_free (vm, to_free, vec_len (to_free));
472 }
barryxie01e94db2020-12-04 19:21:23 +0800473 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200474}
475
Klement Sekera896c8962019-06-24 11:52:49 +0000476always_inline void
477ip4_full_reass_init (ip4_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200478{
Klement Sekera896c8962019-06-24 11:52:49 +0000479 reass->first_bi = ~0;
480 reass->last_packet_octet = ~0;
481 reass->data_len = 0;
482 reass->next_index = ~0;
483 reass->error_next_index = ~0;
484}
485
486always_inline ip4_full_reass_t *
487ip4_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
488 ip4_full_reass_main_t * rm,
489 ip4_full_reass_per_thread_t * rt,
490 ip4_full_reass_kv_t * kv, u8 * do_handoff)
491{
492 ip4_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000493 f64 now;
Klement Sekera75e7d132017-09-20 08:26:30 +0200494
Klement Sekera630ab582019-07-19 09:14:19 +0000495again:
496
497 reass = NULL;
498 now = vlib_time_now (vm);
Klement Sekerac99c0252019-12-18 12:17:06 +0000499 if (!clib_bihash_search_16_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200500 {
Gao Feng9165e032020-04-26 09:57:18 +0800501 if (vm->thread_index != kv->v.memory_owner_thread_index)
502 {
503 *do_handoff = 1;
504 return NULL;
505 }
Klement Sekera630ab582019-07-19 09:14:19 +0000506 reass =
507 pool_elt_at_index (rm->per_thread_data
508 [kv->v.memory_owner_thread_index].pool,
509 kv->v.reass_index);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800510
Klement Sekera75e7d132017-09-20 08:26:30 +0200511 if (now > reass->last_heard + rm->timeout)
512 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200513 ip4_full_reass_drop_all (vm, node, reass);
Klement Sekera896c8962019-06-24 11:52:49 +0000514 ip4_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200515 reass = NULL;
516 }
517 }
518
519 if (reass)
520 {
521 reass->last_heard = now;
522 return reass;
523 }
524
Klement Sekera4c533132018-02-22 11:41:12 +0100525 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200526 {
527 reass = NULL;
528 return reass;
529 }
530 else
531 {
Klement Sekera4c533132018-02-22 11:41:12 +0100532 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400533 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800534 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera630ab582019-07-19 09:14:19 +0000535 reass->memory_owner_thread_index = vm->thread_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100536 ++rt->id_counter;
Klement Sekera896c8962019-06-24 11:52:49 +0000537 ip4_full_reass_init (reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100538 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200539 }
540
Klement Sekerac99c0252019-12-18 12:17:06 +0000541 reass->key.as_u64[0] = kv->kv.key[0];
542 reass->key.as_u64[1] = kv->kv.key[1];
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800543 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000544 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200545 reass->last_heard = now;
546
Klement Sekerac99c0252019-12-18 12:17:06 +0000547 int rv = clib_bihash_add_del_16_8 (&rm->hash, &kv->kv, 2);
Klement Sekera630ab582019-07-19 09:14:19 +0000548 if (rv)
Klement Sekera75e7d132017-09-20 08:26:30 +0200549 {
Klement Sekera896c8962019-06-24 11:52:49 +0000550 ip4_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200551 reass = NULL;
Klement Sekera630ab582019-07-19 09:14:19 +0000552 // if other worker created a context already work with the other copy
553 if (-2 == rv)
554 goto again;
Klement Sekera75e7d132017-09-20 08:26:30 +0200555 }
556
557 return reass;
558}
559
Klement Sekera896c8962019-06-24 11:52:49 +0000560always_inline ip4_full_reass_rc_t
561ip4_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
562 ip4_full_reass_main_t * rm,
563 ip4_full_reass_per_thread_t * rt,
564 ip4_full_reass_t * reass, u32 * bi0,
Klement Sekerafe8371f2020-09-10 12:03:54 +0000565 u32 * next0, u32 * error0, bool is_custom)
Klement Sekera75e7d132017-09-20 08:26:30 +0200566{
Klement Sekera75e7d132017-09-20 08:26:30 +0200567 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
568 vlib_buffer_t *last_b = NULL;
569 u32 sub_chain_bi = reass->first_bi;
570 u32 total_length = 0;
571 u32 buf_cnt = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200572 do
573 {
574 u32 tmp_bi = sub_chain_bi;
575 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
576 ip4_header_t *ip = vlib_buffer_get_current (tmp);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100577 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
578 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
579 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
580 {
581 return IP4_REASS_RC_INTERNAL_ERROR;
582 }
583
Klement Sekera896c8962019-06-24 11:52:49 +0000584 u32 data_len = ip4_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200585 u32 trim_front =
Klement Sekera896c8962019-06-24 11:52:49 +0000586 ip4_header_bytes (ip) + ip4_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200587 u32 trim_end =
588 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
589 if (tmp_bi == reass->first_bi)
590 {
591 /* first buffer - keep ip4 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000592 if (0 != ip4_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100593 {
594 return IP4_REASS_RC_INTERNAL_ERROR;
595 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200596 trim_front = 0;
597 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
598 ip4_header_bytes (ip);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100599 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
600 {
601 return IP4_REASS_RC_INTERNAL_ERROR;
602 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200603 }
604 u32 keep_data =
605 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
606 while (1)
607 {
608 ++buf_cnt;
609 if (trim_front)
610 {
611 if (trim_front > tmp->current_length)
612 {
613 /* drop whole buffer */
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200614 u32 to_be_freed_bi = tmp_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200615 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100616 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
617 {
618 return IP4_REASS_RC_INTERNAL_ERROR;
619 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200620 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
621 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700622 tmp->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200623 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200624 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200625 continue;
626 }
627 else
628 {
629 vlib_buffer_advance (tmp, trim_front);
630 trim_front = 0;
631 }
632 }
633 if (keep_data)
634 {
635 if (last_b)
636 {
637 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
638 last_b->next_buffer = tmp_bi;
639 }
640 last_b = tmp;
641 if (keep_data <= tmp->current_length)
642 {
643 tmp->current_length = keep_data;
644 keep_data = 0;
645 }
646 else
647 {
648 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100649 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
650 {
651 return IP4_REASS_RC_INTERNAL_ERROR;
652 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200653 }
654 total_length += tmp->current_length;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200655 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
656 {
657 tmp_bi = tmp->next_buffer;
658 tmp = vlib_get_buffer (vm, tmp->next_buffer);
659 }
660 else
661 {
662 break;
663 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200664 }
665 else
666 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200667 u32 to_be_freed_bi = tmp_bi;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100668 if (reass->first_bi == tmp_bi)
669 {
670 return IP4_REASS_RC_INTERNAL_ERROR;
671 }
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200672 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
673 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700674 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200675 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700676 tmp->next_buffer = 0;
677 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200678 vlib_buffer_free_one (vm, to_be_freed_bi);
679 }
680 else
681 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700682 tmp->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200683 vlib_buffer_free_one (vm, to_be_freed_bi);
684 break;
685 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200686 }
687 }
688 sub_chain_bi =
689 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
690 reass.next_range_bi;
691 }
692 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700693
Klement Sekerad0f70a32018-12-14 17:24:13 +0100694 if (!last_b)
695 {
696 return IP4_REASS_RC_INTERNAL_ERROR;
697 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200698 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700699
Klement Sekerad0f70a32018-12-14 17:24:13 +0100700 if (total_length < first_b->current_length)
701 {
702 return IP4_REASS_RC_INTERNAL_ERROR;
703 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200704 total_length -= first_b->current_length;
705 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
706 first_b->total_length_not_including_first_buffer = total_length;
707 ip4_header_t *ip = vlib_buffer_get_current (first_b);
708 ip->flags_and_fragment_offset = 0;
709 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
710 ip->checksum = ip4_header_checksum (ip);
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100711 if (!vlib_buffer_chain_linearize (vm, first_b))
712 {
713 return IP4_REASS_RC_NO_BUF;
714 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700715 // reset to reconstruct the mbuf linking
716 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200717 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
718 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200719 ip4_full_reass_add_trace (vm, node, reass, reass->first_bi, FINALIZE, 0,
720 ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200721#if 0
722 // following code does a hexdump of packet fragments to stdout ...
723 do
724 {
725 u32 bi = reass->first_bi;
726 u8 *s = NULL;
727 while (~0 != bi)
728 {
729 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
730 s = format (s, "%u: %U\n", bi, format_hexdump,
731 vlib_buffer_get_current (b), b->current_length);
732 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
733 {
734 bi = b->next_buffer;
735 }
736 else
737 {
738 break;
739 }
740 }
741 printf ("%.*s\n", vec_len (s), s);
742 fflush (stdout);
743 vec_free (s);
744 }
745 while (0);
746#endif
747 }
748 *bi0 = reass->first_bi;
Klement Sekerafe8371f2020-09-10 12:03:54 +0000749 if (!is_custom)
Klement Sekera4c533132018-02-22 11:41:12 +0100750 {
Klement Sekera896c8962019-06-24 11:52:49 +0000751 *next0 = IP4_FULL_REASS_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100752 }
753 else
754 {
755 *next0 = reass->next_index;
756 }
757 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200758 *error0 = IP4_ERROR_NONE;
Klement Sekera896c8962019-06-24 11:52:49 +0000759 ip4_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200760 reass = NULL;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100761 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200762}
763
Klement Sekera896c8962019-06-24 11:52:49 +0000764always_inline ip4_full_reass_rc_t
765ip4_full_reass_insert_range_in_chain (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +0000766 ip4_full_reass_t * reass,
767 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200768{
Klement Sekera75e7d132017-09-20 08:26:30 +0200769 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
770 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
771 if (~0 != prev_range_bi)
772 {
773 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
774 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
775 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
776 prev_vnb->ip.reass.next_range_bi = new_next_bi;
777 }
778 else
779 {
780 if (~0 != reass->first_bi)
781 {
782 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
783 }
784 reass->first_bi = new_next_bi;
785 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100786 vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
787 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
788 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
789 {
790 return IP4_REASS_RC_INTERNAL_ERROR;
791 }
Klement Sekera896c8962019-06-24 11:52:49 +0000792 reass->data_len += ip4_full_reass_buffer_get_data_len (new_next_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100793 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200794}
795
Klement Sekera896c8962019-06-24 11:52:49 +0000796always_inline ip4_full_reass_rc_t
797ip4_full_reass_remove_range_from_chain (vlib_main_t * vm,
798 vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000799 ip4_full_reass_t * reass,
800 u32 prev_range_bi, u32 discard_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200801{
802 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
803 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
804 if (~0 != prev_range_bi)
805 {
806 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
807 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100808 if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
809 {
810 return IP4_REASS_RC_INTERNAL_ERROR;
811 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200812 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
813 }
814 else
815 {
816 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
817 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100818 vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
819 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
820 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
821 {
822 return IP4_REASS_RC_INTERNAL_ERROR;
823 }
Klement Sekera896c8962019-06-24 11:52:49 +0000824 reass->data_len -= ip4_full_reass_buffer_get_data_len (discard_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200825 while (1)
826 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200827 u32 to_be_freed_bi = discard_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200828 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
829 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200830 ip4_full_reass_add_trace (vm, node, reass, discard_bi, RANGE_DISCARD,
831 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200832 }
833 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
834 {
835 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
836 discard_bi = discard_b->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700837 discard_b->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200838 discard_b = vlib_get_buffer (vm, discard_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200839 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200840 }
841 else
842 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700843 discard_b->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200844 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200845 break;
846 }
847 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100848 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200849}
850
Klement Sekera896c8962019-06-24 11:52:49 +0000851always_inline ip4_full_reass_rc_t
852ip4_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
853 ip4_full_reass_main_t * rm,
854 ip4_full_reass_per_thread_t * rt,
855 ip4_full_reass_t * reass, u32 * bi0, u32 * next0,
Klement Sekerafe8371f2020-09-10 12:03:54 +0000856 u32 * error0, bool is_custom, u32 * handoff_thread_idx)
Klement Sekera75e7d132017-09-20 08:26:30 +0200857{
Klement Sekera75e7d132017-09-20 08:26:30 +0200858 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200859 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerafe8371f2020-09-10 12:03:54 +0000860 if (is_custom)
Klement Sekerae8498652019-06-17 12:23:15 +0000861 {
862 // store (error_)next_index before it's overwritten
863 reass->next_index = fvnb->ip.reass.next_index;
864 reass->error_next_index = fvnb->ip.reass.error_next_index;
865 }
Klement Sekera896c8962019-06-24 11:52:49 +0000866 ip4_full_reass_rc_t rc = IP4_REASS_RC_OK;
867 int consumed = 0;
868 ip4_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekera14d7e902018-12-10 13:46:09 +0100869 const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
870 const u32 fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200871 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
Klement Sekera14d7e902018-12-10 13:46:09 +0100872 const u32 fragment_last = fragment_first + fragment_length - 1;
873 fvnb->ip.reass.fragment_first = fragment_first;
874 fvnb->ip.reass.fragment_last = fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200875 int more_fragments = ip4_get_fragment_more (fip);
876 u32 candidate_range_bi = reass->first_bi;
877 u32 prev_range_bi = ~0;
878 fvnb->ip.reass.range_first = fragment_first;
879 fvnb->ip.reass.range_last = fragment_last;
880 fvnb->ip.reass.next_range_bi = ~0;
881 if (!more_fragments)
882 {
883 reass->last_packet_octet = fragment_last;
884 }
885 if (~0 == reass->first_bi)
886 {
887 // starting a new reassembly
Klement Sekerad0f70a32018-12-14 17:24:13 +0100888 rc =
Klement Sekera42cec0e2021-08-02 16:14:15 +0200889 ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100890 if (IP4_REASS_RC_OK != rc)
891 {
892 return rc;
893 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200894 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
895 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200896 ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200897 }
898 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100899 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekera3a343d42019-05-16 14:35:46 +0200900 reass->fragments_n = 1;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100901 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200902 }
Klement Sekera896c8962019-06-24 11:52:49 +0000903 reass->min_fragment_length =
904 clib_min (clib_net_to_host_u16 (fip->length),
905 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200906 while (~0 != candidate_range_bi)
907 {
908 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
909 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
910 if (fragment_first > candidate_vnb->ip.reass.range_last)
911 {
912 // this fragments starts after candidate range
913 prev_range_bi = candidate_range_bi;
914 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
915 if (candidate_vnb->ip.reass.range_last < fragment_last &&
916 ~0 == candidate_range_bi)
917 {
918 // special case - this fragment falls beyond all known ranges
Klement Sekera42cec0e2021-08-02 16:14:15 +0200919 rc = ip4_full_reass_insert_range_in_chain (vm, reass,
920 prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100921 if (IP4_REASS_RC_OK != rc)
922 {
923 return rc;
924 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200925 consumed = 1;
926 break;
927 }
928 continue;
929 }
930 if (fragment_last < candidate_vnb->ip.reass.range_first)
931 {
932 // this fragment ends before candidate range without any overlap
Klement Sekera42cec0e2021-08-02 16:14:15 +0200933 rc = ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
934 *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100935 if (IP4_REASS_RC_OK != rc)
936 {
937 return rc;
938 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200939 consumed = 1;
940 }
941 else
942 {
943 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
944 fragment_last <= candidate_vnb->ip.reass.range_last)
945 {
946 // this fragment is a (sub)part of existing range, ignore it
947 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
948 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200949 ip4_full_reass_add_trace (vm, node, reass, *bi0,
Klement Sekera896c8962019-06-24 11:52:49 +0000950 RANGE_OVERLAP, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200951 }
952 break;
953 }
954 int discard_candidate = 0;
955 if (fragment_first < candidate_vnb->ip.reass.range_first)
956 {
957 u32 overlap =
958 fragment_last - candidate_vnb->ip.reass.range_first + 1;
Klement Sekera896c8962019-06-24 11:52:49 +0000959 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
Klement Sekera75e7d132017-09-20 08:26:30 +0200960 {
961 candidate_vnb->ip.reass.range_first += overlap;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100962 if (reass->data_len < overlap)
963 {
964 return IP4_REASS_RC_INTERNAL_ERROR;
965 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200966 reass->data_len -= overlap;
967 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
968 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200969 ip4_full_reass_add_trace (vm, node, reass,
Klement Sekera896c8962019-06-24 11:52:49 +0000970 candidate_range_bi,
971 RANGE_SHRINK, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200972 }
Klement Sekera42cec0e2021-08-02 16:14:15 +0200973 rc = ip4_full_reass_insert_range_in_chain (
974 vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100975 if (IP4_REASS_RC_OK != rc)
976 {
977 return rc;
978 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200979 consumed = 1;
980 }
981 else
982 {
983 discard_candidate = 1;
984 }
985 }
986 else if (fragment_last > candidate_vnb->ip.reass.range_last)
987 {
988 u32 overlap =
989 candidate_vnb->ip.reass.range_last - fragment_first + 1;
Klement Sekera896c8962019-06-24 11:52:49 +0000990 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
Klement Sekera75e7d132017-09-20 08:26:30 +0200991 {
992 fvnb->ip.reass.range_first += overlap;
993 if (~0 != candidate_vnb->ip.reass.next_range_bi)
994 {
995 prev_range_bi = candidate_range_bi;
996 candidate_range_bi =
997 candidate_vnb->ip.reass.next_range_bi;
998 continue;
999 }
1000 else
1001 {
1002 // special case - last range discarded
Klement Sekera42cec0e2021-08-02 16:14:15 +02001003 rc = ip4_full_reass_insert_range_in_chain (
1004 vm, reass, candidate_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001005 if (IP4_REASS_RC_OK != rc)
1006 {
1007 return rc;
1008 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001009 consumed = 1;
1010 }
1011 }
1012 else
1013 {
1014 discard_candidate = 1;
1015 }
1016 }
1017 else
1018 {
1019 discard_candidate = 1;
1020 }
1021 if (discard_candidate)
1022 {
1023 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
1024 // discard candidate range, probe next range
Klement Sekera42cec0e2021-08-02 16:14:15 +02001025 rc = ip4_full_reass_remove_range_from_chain (
1026 vm, node, reass, prev_range_bi, candidate_range_bi);
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 if (~0 != next_range_bi)
1032 {
1033 candidate_range_bi = next_range_bi;
1034 continue;
1035 }
1036 else
1037 {
1038 // special case - last range discarded
Klement Sekera42cec0e2021-08-02 16:14:15 +02001039 rc = ip4_full_reass_insert_range_in_chain (
1040 vm, reass, prev_range_bi, *bi0);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001041 if (IP4_REASS_RC_OK != rc)
1042 {
1043 return rc;
1044 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001045 consumed = 1;
1046 }
1047 }
1048 }
1049 break;
1050 }
Klement Sekera3a343d42019-05-16 14:35:46 +02001051 ++reass->fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +02001052 if (consumed)
1053 {
1054 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1055 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001056 ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001057 }
1058 }
1059 if (~0 != reass->last_packet_octet &&
1060 reass->data_len == reass->last_packet_octet + 1)
1061 {
Klement Sekera630ab582019-07-19 09:14:19 +00001062 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001063 int handoff =
1064 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera630ab582019-07-19 09:14:19 +00001065 rc =
Klement Sekera896c8962019-06-24 11:52:49 +00001066 ip4_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001067 is_custom);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001068 if (IP4_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +00001069 {
1070 rc = IP4_REASS_RC_HANDOFF;
1071 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001072 }
1073 else
1074 {
1075 if (consumed)
1076 {
1077 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +02001078 if (reass->fragments_n > rm->max_reass_len)
1079 {
1080 rc = IP4_REASS_RC_TOO_MANY_FRAGMENTS;
1081 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001082 }
1083 else
1084 {
Klement Sekera896c8962019-06-24 11:52:49 +00001085 *next0 = IP4_FULL_REASS_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +02001086 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
1087 }
1088 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001089 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +02001090}
1091
1092always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001093ip4_full_reass_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001094 vlib_frame_t * frame, ip4_full_reass_node_type_t type)
Klement Sekera75e7d132017-09-20 08:26:30 +02001095{
1096 u32 *from = vlib_frame_vector_args (frame);
1097 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001098 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1099 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001100 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001101
1102 n_left_from = frame->n_vectors;
1103 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001104 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001105 {
1106 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1107
Klement Sekera75e7d132017-09-20 08:26:30 +02001108 while (n_left_from > 0 && n_left_to_next > 0)
1109 {
1110 u32 bi0;
1111 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +01001112 u32 next0;
1113 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +02001114
1115 bi0 = from[0];
1116 b0 = vlib_get_buffer (vm, bi0);
1117
1118 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001119 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +02001120 {
Klement Sekera4c533132018-02-22 11:41:12 +01001121 // this is a whole packet - no fragmentation
Klement Sekerafe8371f2020-09-10 12:03:54 +00001122 if (CUSTOM != type)
Klement Sekera4c533132018-02-22 11:41:12 +01001123 {
Klement Sekera896c8962019-06-24 11:52:49 +00001124 next0 = IP4_FULL_REASS_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +01001125 }
1126 else
1127 {
1128 next0 = vnet_buffer (b0)->ip.reass.next_index;
1129 }
Klement Sekera896c8962019-06-24 11:52:49 +00001130 goto packet_enqueue;
1131 }
1132 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1133 const u32 fragment_length =
1134 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
1135 const u32 fragment_last = fragment_first + fragment_length - 1;
1136 if (fragment_first > fragment_last || fragment_first + fragment_length > UINT16_MAX - 20 || (fragment_length < 8 && ip4_get_fragment_more (ip0))) // 8 is minimum frag length per RFC 791
1137 {
1138 next0 = IP4_FULL_REASS_NEXT_DROP;
1139 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
1140 goto packet_enqueue;
1141 }
1142 ip4_full_reass_kv_t kv;
1143 u8 do_handoff = 0;
1144
1145 kv.k.as_u64[0] =
1146 (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
1147 vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
1148 (u64) ip0->src_address.as_u32 << 32;
1149 kv.k.as_u64[1] =
1150 (u64) ip0->dst_address.
1151 as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
1152
1153 ip4_full_reass_t *reass =
1154 ip4_full_reass_find_or_create (vm, node, rm, rt, &kv,
1155 &do_handoff);
1156
1157 if (reass)
1158 {
1159 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1160 if (0 == fragment_first)
1161 {
1162 reass->sendout_thread_index = vm->thread_index;
1163 }
1164 }
1165
1166 if (PREDICT_FALSE (do_handoff))
1167 {
1168 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001169 vnet_buffer (b0)->ip.reass.owner_thread_index =
1170 kv.v.memory_owner_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001171 }
1172 else if (reass)
1173 {
1174 u32 handoff_thread_idx;
1175 switch (ip4_full_reass_update
1176 (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001177 &error0, CUSTOM == type, &handoff_thread_idx))
Klement Sekera896c8962019-06-24 11:52:49 +00001178 {
1179 case IP4_REASS_RC_OK:
1180 /* nothing to do here */
1181 break;
1182 case IP4_REASS_RC_HANDOFF:
1183 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1184 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001185 vnet_buffer (b0)->ip.reass.owner_thread_index =
1186 handoff_thread_idx;
Klement Sekera896c8962019-06-24 11:52:49 +00001187 break;
1188 case IP4_REASS_RC_TOO_MANY_FRAGMENTS:
1189 vlib_node_increment_counter (vm, node->node_index,
1190 IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1191 1);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001192 ip4_full_reass_drop_all (vm, node, reass);
Klement Sekera896c8962019-06-24 11:52:49 +00001193 ip4_full_reass_free (rm, rt, reass);
1194 goto next_packet;
1195 break;
1196 case IP4_REASS_RC_NO_BUF:
1197 vlib_node_increment_counter (vm, node->node_index,
1198 IP4_ERROR_REASS_NO_BUF, 1);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001199 ip4_full_reass_drop_all (vm, node, reass);
Klement Sekera896c8962019-06-24 11:52:49 +00001200 ip4_full_reass_free (rm, rt, reass);
1201 goto next_packet;
1202 break;
1203 case IP4_REASS_RC_INTERNAL_ERROR:
1204 /* drop everything and start with a clean slate */
1205 vlib_node_increment_counter (vm, node->node_index,
1206 IP4_ERROR_REASS_INTERNAL_ERROR,
1207 1);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001208 ip4_full_reass_drop_all (vm, node, reass);
Klement Sekera896c8962019-06-24 11:52:49 +00001209 ip4_full_reass_free (rm, rt, reass);
1210 goto next_packet;
1211 break;
1212 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001213 }
1214 else
1215 {
Klement Sekera896c8962019-06-24 11:52:49 +00001216 next0 = IP4_FULL_REASS_NEXT_DROP;
1217 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
Klement Sekera4c533132018-02-22 11:41:12 +01001218 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001219
Klement Sekera896c8962019-06-24 11:52:49 +00001220
1221 packet_enqueue:
Klement Sekera896c8962019-06-24 11:52:49 +00001222
Klement Sekera75e7d132017-09-20 08:26:30 +02001223 if (bi0 != ~0)
1224 {
1225 to_next[0] = bi0;
1226 to_next += 1;
1227 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001228
1229 /* bi0 might have been updated by reass_finalize, reload */
1230 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001231 if (IP4_ERROR_NONE != error0)
1232 {
1233 b0->error = node->errors[error0];
1234 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001235
Klement Sekera896c8962019-06-24 11:52:49 +00001236 if (next0 == IP4_FULL_REASS_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001237 {
1238 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1239 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001240 ip4_full_reass_add_trace (
1241 vm, node, NULL, bi0, HANDOFF, 0,
1242 vnet_buffer (b0)->ip.reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001243 }
1244 }
Klement Sekerafe8371f2020-09-10 12:03:54 +00001245 else if (FEATURE == type && IP4_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001246 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001247 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001248 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001249 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1250 to_next, n_left_to_next,
1251 bi0, next0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001252 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1253 }
1254
Klement Sekerad0f70a32018-12-14 17:24:13 +01001255 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001256 from += 1;
1257 n_left_from -= 1;
1258 }
1259
1260 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1261 }
1262
Klement Sekera4c533132018-02-22 11:41:12 +01001263 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001264 return frame->n_vectors;
1265}
1266
Klement Sekera896c8962019-06-24 11:52:49 +00001267static char *ip4_full_reass_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001268#define _(sym, string) string,
1269 foreach_ip4_error
1270#undef _
1271};
1272
Klement Sekera896c8962019-06-24 11:52:49 +00001273VLIB_NODE_FN (ip4_full_reass_node) (vlib_main_t * vm,
1274 vlib_node_runtime_t * node,
1275 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001276{
Klement Sekerafe8371f2020-09-10 12:03:54 +00001277 return ip4_full_reass_inline (vm, node, frame, NORMAL);
Klement Sekera4c533132018-02-22 11:41:12 +01001278}
1279
Klement Sekera896c8962019-06-24 11:52:49 +00001280VLIB_REGISTER_NODE (ip4_full_reass_node) = {
1281 .name = "ip4-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001282 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001283 .format_trace = format_ip4_full_reass_trace,
1284 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1285 .error_strings = ip4_full_reass_error_strings,
1286 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001287 .next_nodes =
1288 {
Klement Sekera896c8962019-06-24 11:52:49 +00001289 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1290 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1291 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001292
Klement Sekera75e7d132017-09-20 08:26:30 +02001293 },
1294};
Klement Sekera75e7d132017-09-20 08:26:30 +02001295
Klement Sekera896c8962019-06-24 11:52:49 +00001296VLIB_NODE_FN (ip4_full_reass_node_feature) (vlib_main_t * vm,
1297 vlib_node_runtime_t * node,
1298 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001299{
Klement Sekerafe8371f2020-09-10 12:03:54 +00001300 return ip4_full_reass_inline (vm, node, frame, FEATURE);
Klement Sekera4c533132018-02-22 11:41:12 +01001301}
1302
Klement Sekera896c8962019-06-24 11:52:49 +00001303VLIB_REGISTER_NODE (ip4_full_reass_node_feature) = {
1304 .name = "ip4-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001305 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001306 .format_trace = format_ip4_full_reass_trace,
1307 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1308 .error_strings = ip4_full_reass_error_strings,
1309 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001310 .next_nodes =
1311 {
Klement Sekera896c8962019-06-24 11:52:49 +00001312 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1313 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1314 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001315 },
1316};
Klement Sekera4c533132018-02-22 11:41:12 +01001317
Klement Sekera896c8962019-06-24 11:52:49 +00001318VNET_FEATURE_INIT (ip4_full_reass_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001319 .arc_name = "ip4-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001320 .node_name = "ip4-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001321 .runs_before = VNET_FEATURES ("ip4-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001322 "ipsec4-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001323 .runs_after = 0,
1324};
Klement Sekera4c533132018-02-22 11:41:12 +01001325
Klement Sekerafe8371f2020-09-10 12:03:54 +00001326VLIB_NODE_FN (ip4_full_reass_node_custom) (vlib_main_t * vm,
1327 vlib_node_runtime_t * node,
1328 vlib_frame_t * frame)
1329{
1330 return ip4_full_reass_inline (vm, node, frame, CUSTOM);
1331}
1332
Klement Sekerafe8371f2020-09-10 12:03:54 +00001333VLIB_REGISTER_NODE (ip4_full_reass_node_custom) = {
1334 .name = "ip4-full-reassembly-custom",
1335 .vector_size = sizeof (u32),
1336 .format_trace = format_ip4_full_reass_trace,
1337 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1338 .error_strings = ip4_full_reass_error_strings,
1339 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1340 .next_nodes =
1341 {
1342 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1343 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1344 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-custom-hoff",
1345 },
1346};
Klement Sekerafe8371f2020-09-10 12:03:54 +00001347
Klement Sekerafe8371f2020-09-10 12:03:54 +00001348VNET_FEATURE_INIT (ip4_full_reass_custom, static) = {
1349 .arc_name = "ip4-unicast",
1350 .node_name = "ip4-full-reassembly-feature",
1351 .runs_before = VNET_FEATURES ("ip4-lookup",
1352 "ipsec4-input-feature"),
1353 .runs_after = 0,
1354};
1355
Klement Sekerafe8371f2020-09-10 12:03:54 +00001356
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001357#ifndef CLIB_MARCH_VARIANT
Klement Sekerafe8371f2020-09-10 12:03:54 +00001358uword
1359ip4_full_reass_custom_register_next_node (uword node_index)
1360{
1361 return vlib_node_add_next (vlib_get_main (),
1362 ip4_full_reass_node_custom.index, node_index);
1363}
1364
Klement Sekera4c533132018-02-22 11:41:12 +01001365always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +00001366ip4_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001367{
Klement Sekera896c8962019-06-24 11:52:49 +00001368 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001369 u32 nbuckets;
1370 u8 i;
1371
1372 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1373
1374 for (i = 0; i < 31; i++)
1375 if ((1 << i) >= nbuckets)
1376 break;
1377 nbuckets = 1 << i;
1378
1379 return nbuckets;
1380}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001381#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001382
1383typedef enum
1384{
1385 IP4_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001386} ip4_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001387
1388typedef struct
1389{
1390 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001391 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001392} ip4_rehash_cb_ctx;
1393
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001394#ifndef CLIB_MARCH_VARIANT
Neale Rannsf50bac12019-12-06 05:53:17 +00001395static int
Klement Sekera8dcfed52018-06-28 11:16:15 +02001396ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001397{
1398 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001399 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001400 {
1401 ctx->failure = 1;
1402 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001403 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001404}
1405
Klement Sekera4c533132018-02-22 11:41:12 +01001406static void
Klement Sekera896c8962019-06-24 11:52:49 +00001407ip4_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1408 u32 max_reassembly_length,
1409 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001410{
Klement Sekera896c8962019-06-24 11:52:49 +00001411 ip4_full_reass_main.timeout_ms = timeout_ms;
1412 ip4_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1413 ip4_full_reass_main.max_reass_n = max_reassemblies;
1414 ip4_full_reass_main.max_reass_len = max_reassembly_length;
1415 ip4_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001416}
1417
Klement Sekera75e7d132017-09-20 08:26:30 +02001418vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001419ip4_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1420 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001421{
Klement Sekera896c8962019-06-24 11:52:49 +00001422 u32 old_nbuckets = ip4_full_reass_get_nbuckets ();
1423 ip4_full_reass_set_params (timeout_ms, max_reassemblies,
1424 max_reassembly_length, expire_walk_interval_ms);
1425 vlib_process_signal_event (ip4_full_reass_main.vlib_main,
1426 ip4_full_reass_main.ip4_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001427 IP4_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001428 u32 new_nbuckets = ip4_full_reass_get_nbuckets ();
1429 if (ip4_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001430 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001431 clib_bihash_16_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001432 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001433 ip4_rehash_cb_ctx ctx;
1434 ctx.failure = 0;
1435 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001436 clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001437 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001438 clib_bihash_foreach_key_value_pair_16_8 (&ip4_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001439 ip4_rehash_cb, &ctx);
1440 if (ctx.failure)
1441 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001442 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001443 return -1;
1444 }
1445 else
1446 {
Klement Sekera896c8962019-06-24 11:52:49 +00001447 clib_bihash_free_16_8 (&ip4_full_reass_main.hash);
1448 clib_memcpy_fast (&ip4_full_reass_main.hash, &new_hash,
1449 sizeof (ip4_full_reass_main.hash));
1450 clib_bihash_copied (&ip4_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001451 }
1452 }
1453 return 0;
1454}
1455
1456vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001457ip4_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1458 u32 * max_reassembly_length,
1459 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001460{
Klement Sekera896c8962019-06-24 11:52:49 +00001461 *timeout_ms = ip4_full_reass_main.timeout_ms;
1462 *max_reassemblies = ip4_full_reass_main.max_reass_n;
1463 *max_reassembly_length = ip4_full_reass_main.max_reass_len;
1464 *expire_walk_interval_ms = ip4_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001465 return 0;
1466}
1467
Klement Sekera4c533132018-02-22 11:41:12 +01001468static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001469ip4_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001470{
Klement Sekera896c8962019-06-24 11:52:49 +00001471 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001472 clib_error_t *error = 0;
1473 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001474 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001475
1476 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001477
Juraj Slobodacd806922018-10-10 10:15:54 +02001478 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001479 ip4_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001480 vec_foreach (rt, rm->per_thread_data)
1481 {
1482 clib_spinlock_init (&rt->lock);
1483 pool_alloc (rt->pool, rm->max_reass_n);
1484 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001485
Klement Sekera896c8962019-06-24 11:52:49 +00001486 node = vlib_get_node_by_name (vm, (u8 *) "ip4-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001487 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001488 rm->ip4_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001489
Klement Sekera896c8962019-06-24 11:52:49 +00001490 ip4_full_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1491 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1492 IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1493 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001494
Klement Sekera896c8962019-06-24 11:52:49 +00001495 nbuckets = ip4_full_reass_get_nbuckets ();
1496 clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001497
Dave Barach1403fcd2018-02-05 09:45:43 -05001498 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 ASSERT (node);
1500 rm->ip4_drop_idx = node->index;
Klement Sekera4c533132018-02-22 11:41:12 +01001501
Klement Sekera896c8962019-06-24 11:52:49 +00001502 rm->fq_index = vlib_frame_queue_main_init (ip4_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001503 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001504 vlib_frame_queue_main_init (ip4_full_reass_node_feature.index, 0);
Klement Sekerafe8371f2020-09-10 12:03:54 +00001505 rm->fq_custom_index =
1506 vlib_frame_queue_main_init (ip4_full_reass_node_custom.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001507
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001508 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001509 return error;
1510}
1511
Klement Sekera896c8962019-06-24 11:52:49 +00001512VLIB_INIT_FUNCTION (ip4_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001513#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001514
1515static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001516ip4_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1517 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekera75e7d132017-09-20 08:26:30 +02001518{
Klement Sekera896c8962019-06-24 11:52:49 +00001519 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001520 uword event_type, *event_data = 0;
1521
1522 while (true)
1523 {
1524 vlib_process_wait_for_event_or_clock (vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001525 (f64)
1526 rm->expire_walk_interval_ms /
1527 (f64) MSEC_PER_SEC);
Klement Sekera75e7d132017-09-20 08:26:30 +02001528 event_type = vlib_process_get_events (vm, &event_data);
1529
1530 switch (event_type)
1531 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001532 case ~0:
1533 /* no events => timeout */
1534 /* fallthrough */
Klement Sekera75e7d132017-09-20 08:26:30 +02001535 case IP4_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001536 /* nothing to do here */
Klement Sekera75e7d132017-09-20 08:26:30 +02001537 break;
1538 default:
1539 clib_warning ("BUG: event type 0x%wx", event_type);
1540 break;
1541 }
1542 f64 now = vlib_time_now (vm);
1543
Klement Sekera896c8962019-06-24 11:52:49 +00001544 ip4_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001545 int *pool_indexes_to_free = NULL;
1546
Klement Sekera4c533132018-02-22 11:41:12 +01001547 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001548 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001549 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001550 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1551 {
Klement Sekera896c8962019-06-24 11:52:49 +00001552 ip4_full_reass_per_thread_t *rt =
1553 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001554 clib_spinlock_lock (&rt->lock);
1555
1556 vec_reset_length (pool_indexes_to_free);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001557 pool_foreach_index (index, rt->pool) {
Klement Sekera4c533132018-02-22 11:41:12 +01001558 reass = pool_elt_at_index (rt->pool, index);
1559 if (now > reass->last_heard + rm->timeout)
1560 {
1561 vec_add1 (pool_indexes_to_free, index);
1562 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001563 }
Klement Sekera4c533132018-02-22 11:41:12 +01001564 int *i;
Klement Sekera4c533132018-02-22 11:41:12 +01001565 vec_foreach (i, pool_indexes_to_free)
1566 {
Klement Sekera896c8962019-06-24 11:52:49 +00001567 ip4_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001568 ip4_full_reass_drop_all (vm, node, reass);
1569 ip4_full_reass_free (rm, rt, reass);
1570 }
Klement Sekera4c533132018-02-22 11:41:12 +01001571
1572 clib_spinlock_unlock (&rt->lock);
1573 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001574
Klement Sekera75e7d132017-09-20 08:26:30 +02001575 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001576 if (event_data)
1577 {
1578 _vec_len (event_data) = 0;
1579 }
1580 }
1581
1582 return 0;
1583}
1584
Klement Sekera896c8962019-06-24 11:52:49 +00001585VLIB_REGISTER_NODE (ip4_full_reass_expire_node) = {
1586 .function = ip4_full_reass_walk_expired,
Klement Sekera75e7d132017-09-20 08:26:30 +02001587 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001588 .name = "ip4-full-reassembly-expire-walk",
1589 .format_trace = format_ip4_full_reass_trace,
1590 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1591 .error_strings = ip4_full_reass_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001592
1593};
Klement Sekera75e7d132017-09-20 08:26:30 +02001594
1595static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001596format_ip4_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001597{
Klement Sekera896c8962019-06-24 11:52:49 +00001598 ip4_full_reass_key_t *key = va_arg (*args, ip4_full_reass_key_t *);
1599 s =
1600 format (s,
1601 "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1602 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1603 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
Klement Sekera75e7d132017-09-20 08:26:30 +02001604 return s;
1605}
1606
1607static u8 *
1608format_ip4_reass (u8 * s, va_list * args)
1609{
1610 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001611 ip4_full_reass_t *reass = va_arg (*args, ip4_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001612
Klement Sekera4c533132018-02-22 11:41:12 +01001613 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001614 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001615 reass->id, format_ip4_full_reass_key, &reass->key,
1616 reass->first_bi, reass->data_len,
1617 reass->last_packet_octet, reass->trace_op_counter);
1618
Klement Sekera75e7d132017-09-20 08:26:30 +02001619 u32 bi = reass->first_bi;
1620 u32 counter = 0;
1621 while (~0 != bi)
1622 {
1623 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1624 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera896c8962019-06-24 11:52:49 +00001625 s =
1626 format (s,
1627 " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1628 "fragment[%u, %u]\n", counter, vnb->ip.reass.range_first,
1629 vnb->ip.reass.range_last, bi,
1630 ip4_full_reass_buffer_get_data_offset (b),
1631 ip4_full_reass_buffer_get_data_len (b),
1632 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
Klement Sekera75e7d132017-09-20 08:26:30 +02001633 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1634 {
1635 bi = b->next_buffer;
1636 }
1637 else
1638 {
1639 bi = ~0;
1640 }
1641 }
1642 return s;
1643}
1644
1645static clib_error_t *
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001646show_ip4_reass (vlib_main_t * vm,
1647 unformat_input_t * input,
Klement Sekera75e7d132017-09-20 08:26:30 +02001648 CLIB_UNUSED (vlib_cli_command_t * lmd))
1649{
Klement Sekera896c8962019-06-24 11:52:49 +00001650 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001651
1652 vlib_cli_output (vm, "---------------------");
1653 vlib_cli_output (vm, "IP4 reassembly status");
1654 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001655 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001656 if (unformat (input, "details"))
1657 {
Klement Sekera4c533132018-02-22 11:41:12 +01001658 details = true;
1659 }
1660
1661 u32 sum_reass_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001662 ip4_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001663 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001664 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001665 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1666 {
Klement Sekera896c8962019-06-24 11:52:49 +00001667 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001668 clib_spinlock_lock (&rt->lock);
1669 if (details)
1670 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001671 pool_foreach (reass, rt->pool) {
Klement Sekera4c533132018-02-22 11:41:12 +01001672 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001673 }
Klement Sekera4c533132018-02-22 11:41:12 +01001674 }
1675 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001676 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001677 }
1678 vlib_cli_output (vm, "---------------------");
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001679 vlib_cli_output (vm, "Current full IP4 reassemblies count: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001680 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001681 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001682 "Maximum configured concurrent full IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001683 (long unsigned) rm->max_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001684 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001685 "Maximum configured amount of fragments "
1686 "per full IP4 reassembly: %lu\n",
1687 (long unsigned) rm->max_reass_len);
1688 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001689 "Maximum configured full IP4 reassembly timeout: %lums\n",
1690 (long unsigned) rm->timeout_ms);
1691 vlib_cli_output (vm,
1692 "Maximum configured full IP4 reassembly expire walk interval: %lums\n",
1693 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001694 return 0;
1695}
1696
Klement Sekera896c8962019-06-24 11:52:49 +00001697VLIB_CLI_COMMAND (show_ip4_full_reass_cmd, static) = {
1698 .path = "show ip4-full-reassembly",
1699 .short_help = "show ip4-full-reassembly [details]",
Klement Sekera75e7d132017-09-20 08:26:30 +02001700 .function = show_ip4_reass,
1701};
Klement Sekera75e7d132017-09-20 08:26:30 +02001702
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001703#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001704vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001705ip4_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001706{
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001707 return vnet_feature_enable_disable ("ip4-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001708 "ip4-full-reassembly-feature",
1709 sw_if_index, enable_disable, 0, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001710}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001711#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001712
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001713
Klement Sekera896c8962019-06-24 11:52:49 +00001714#define foreach_ip4_full_reass_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001715_(CONGESTION_DROP, "congestion drop")
1716
1717
1718typedef enum
1719{
Klement Sekera896c8962019-06-24 11:52:49 +00001720#define _(sym,str) IP4_FULL_REASS_HANDOFF_ERROR_##sym,
1721 foreach_ip4_full_reass_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001722#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001723 IP4_FULL_REASS_HANDOFF_N_ERROR,
1724} ip4_full_reass_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001725
Klement Sekera896c8962019-06-24 11:52:49 +00001726static char *ip4_full_reass_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001727#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001728 foreach_ip4_full_reass_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001729#undef _
1730};
1731
1732typedef struct
1733{
1734 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001735} ip4_full_reass_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001736
1737static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001738format_ip4_full_reass_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001739{
1740 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1741 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001742 ip4_full_reass_handoff_trace_t *t =
1743 va_arg (*args, ip4_full_reass_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001744
1745 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001746 format (s, "ip4-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001747 t->next_worker_index);
1748
1749 return s;
1750}
1751
1752always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001753ip4_full_reass_handoff_node_inline (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001754 vlib_node_runtime_t * node,
Klement Sekerafe8371f2020-09-10 12:03:54 +00001755 vlib_frame_t * frame,
1756 ip4_full_reass_node_type_t type)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001757{
Klement Sekera896c8962019-06-24 11:52:49 +00001758 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001759
1760 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1761 u32 n_enq, n_left_from, *from;
1762 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1763 u32 fq_index;
1764
1765 from = vlib_frame_vector_args (frame);
1766 n_left_from = frame->n_vectors;
1767 vlib_get_buffers (vm, from, bufs, n_left_from);
1768
1769 b = bufs;
1770 ti = thread_indices;
1771
Klement Sekerafe8371f2020-09-10 12:03:54 +00001772 switch (type)
1773 {
1774 case NORMAL:
1775 fq_index = rm->fq_index;
1776 break;
1777 case FEATURE:
1778 fq_index = rm->fq_feature_index;
1779 break;
1780 case CUSTOM:
1781 fq_index = rm->fq_custom_index;
1782 break;
1783 default:
1784 clib_warning ("Unexpected `type' (%d)!", type);
1785 ASSERT (0);
1786 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001787
1788 while (n_left_from > 0)
1789 {
Klement Sekerade34c352019-06-25 11:19:22 +00001790 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001791
1792 if (PREDICT_FALSE
1793 ((node->flags & VLIB_NODE_FLAG_TRACE)
1794 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1795 {
Klement Sekera896c8962019-06-24 11:52:49 +00001796 ip4_full_reass_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001797 vlib_add_trace (vm, node, b[0], sizeof (*t));
1798 t->next_worker_index = ti[0];
1799 }
1800
1801 n_left_from -= 1;
1802 ti += 1;
1803 b += 1;
1804 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001805 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1806 thread_indices, frame->n_vectors, 1);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001807
1808 if (n_enq < frame->n_vectors)
1809 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001810 IP4_FULL_REASS_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001811 frame->n_vectors - n_enq);
1812 return frame->n_vectors;
1813}
1814
Klement Sekera896c8962019-06-24 11:52:49 +00001815VLIB_NODE_FN (ip4_full_reass_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001816 vlib_node_runtime_t * node,
1817 vlib_frame_t * frame)
1818{
Klement Sekerafe8371f2020-09-10 12:03:54 +00001819 return ip4_full_reass_handoff_node_inline (vm, node, frame, NORMAL);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001820}
1821
1822
Klement Sekera896c8962019-06-24 11:52:49 +00001823VLIB_REGISTER_NODE (ip4_full_reass_handoff_node) = {
1824 .name = "ip4-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001825 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001826 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1827 .error_strings = ip4_full_reass_handoff_error_strings,
1828 .format_trace = format_ip4_full_reass_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001829
1830 .n_next_nodes = 1,
1831
1832 .next_nodes = {
1833 [0] = "error-drop",
1834 },
1835};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001836
1837
Klement Sekera896c8962019-06-24 11:52:49 +00001838VLIB_NODE_FN (ip4_full_reass_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001839 vlib_node_runtime_t *
1840 node,
1841 vlib_frame_t * frame)
1842{
Klement Sekerafe8371f2020-09-10 12:03:54 +00001843 return ip4_full_reass_handoff_node_inline (vm, node, frame, FEATURE);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001844}
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001845
1846
Klement Sekera896c8962019-06-24 11:52:49 +00001847VLIB_REGISTER_NODE (ip4_full_reass_feature_handoff_node) = {
1848 .name = "ip4-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001849 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001850 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1851 .error_strings = ip4_full_reass_handoff_error_strings,
1852 .format_trace = format_ip4_full_reass_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001853
1854 .n_next_nodes = 1,
1855
1856 .next_nodes = {
1857 [0] = "error-drop",
1858 },
1859};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001860
Klement Sekerafe8371f2020-09-10 12:03:54 +00001861VLIB_NODE_FN (ip4_full_reass_custom_handoff_node) (vlib_main_t * vm,
1862 vlib_node_runtime_t *
1863 node,
1864 vlib_frame_t * frame)
1865{
1866 return ip4_full_reass_handoff_node_inline (vm, node, frame, CUSTOM);
1867}
Klement Sekerafe8371f2020-09-10 12:03:54 +00001868
1869
Klement Sekerafe8371f2020-09-10 12:03:54 +00001870VLIB_REGISTER_NODE (ip4_full_reass_custom_handoff_node) = {
1871 .name = "ip4-full-reass-custom-hoff",
1872 .vector_size = sizeof (u32),
1873 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1874 .error_strings = ip4_full_reass_handoff_error_strings,
1875 .format_trace = format_ip4_full_reass_handoff_trace,
1876
1877 .n_next_nodes = 1,
1878
1879 .next_nodes = {
1880 [0] = "error-drop",
1881 },
1882};
Klement Sekerafe8371f2020-09-10 12:03:54 +00001883
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001884#ifndef CLIB_MARCH_VARIANT
1885int
1886ip4_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1887{
1888 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1889 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1890 if (is_enable)
1891 {
1892 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1893 {
1894 ++rm->feature_use_refcount_per_intf[sw_if_index];
1895 return vnet_feature_enable_disable ("ip4-unicast",
1896 "ip4-full-reassembly-feature",
1897 sw_if_index, 1, 0, 0);
1898 }
1899 ++rm->feature_use_refcount_per_intf[sw_if_index];
1900 }
1901 else
1902 {
1903 --rm->feature_use_refcount_per_intf[sw_if_index];
1904 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1905 return vnet_feature_enable_disable ("ip4-unicast",
1906 "ip4-full-reassembly-feature",
1907 sw_if_index, 0, 0, 0);
1908 }
1909 return -1;
1910}
1911#endif
1912
Klement Sekera75e7d132017-09-20 08:26:30 +02001913/*
1914 * fd.io coding-style-patch-verification: ON
1915 *
1916 * Local Variables:
1917 * eval: (c-set-style "gnu")
1918 * End:
1919 */