blob: d5f0b8a7e66e695785ca2bacea2efc94813abd2b [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
18 * @brief IPv4 Reassembly.
19 *
20 * This file contains the source code for IPv4 reassembly.
21 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
Klement Sekera8dcfed52018-06-28 11:16:15 +020026#include <vppinfra/bihash_16_8.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020027#include <vnet/ip/ip4_reassembly.h>
28
29#define MSEC_PER_SEC 1000
30#define IP4_REASS_TIMEOUT_DEFAULT_MS 100
31#define IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
Klement Sekera4c533132018-02-22 11:41:12 +010032#define IP4_REASS_MAX_REASSEMBLIES_DEFAULT 1024
Klement Sekera3a343d42019-05-16 14:35:46 +020033#define IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
Klement Sekera75e7d132017-09-20 08:26:30 +020034#define IP4_REASS_HT_LOAD_FACTOR (0.75)
35
36#define IP4_REASS_DEBUG_BUFFERS 0
37#if IP4_REASS_DEBUG_BUFFERS
38#define IP4_REASS_DEBUG_BUFFER(bi, what) \
39 do \
40 { \
41 u32 _bi = bi; \
42 printf (#what "buffer %u", _bi); \
43 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
44 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \
45 { \
46 _bi = _b->next_buffer; \
47 printf ("[%u]", _bi); \
48 _b = vlib_get_buffer (vm, _bi); \
49 } \
50 printf ("\n"); \
51 fflush (stdout); \
52 } \
53 while (0)
54#else
55#define IP4_REASS_DEBUG_BUFFER(...)
56#endif
57
Klement Sekerad0f70a32018-12-14 17:24:13 +010058typedef enum
59{
60 IP4_REASS_RC_OK,
Klement Sekera3a343d42019-05-16 14:35:46 +020061 IP4_REASS_RC_TOO_MANY_FRAGMENTS,
Klement Sekerad0f70a32018-12-14 17:24:13 +010062 IP4_REASS_RC_INTERNAL_ERROR,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010063 IP4_REASS_RC_NO_BUF,
Klement Sekerad0f70a32018-12-14 17:24:13 +010064} ip4_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020065
66typedef struct
67{
68 union
69 {
70 struct
71 {
Klement Sekera75e7d132017-09-20 08:26:30 +020072 u32 xx_id;
73 ip4_address_t src;
74 ip4_address_t dst;
Klement Sekera8dcfed52018-06-28 11:16:15 +020075 u16 frag_id;
76 u8 proto;
77 u8 unused;
Klement Sekera75e7d132017-09-20 08:26:30 +020078 };
Klement Sekera8dcfed52018-06-28 11:16:15 +020079 u64 as_u64[2];
Klement Sekera75e7d132017-09-20 08:26:30 +020080 };
81} ip4_reass_key_t;
82
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080083typedef union
84{
85 struct
86 {
87 u32 reass_index;
88 u32 thread_index;
89 };
90 u64 as_u64;
91} ip4_reass_val_t;
92
93typedef union
94{
95 struct
96 {
97 ip4_reass_key_t k;
98 ip4_reass_val_t v;
99 };
100 clib_bihash_kv_16_8_t kv;
101} ip4_reass_kv_t;
102
Klement Sekera75e7d132017-09-20 08:26:30 +0200103always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +0200104ip4_reass_buffer_get_data_offset (vlib_buffer_t * b)
105{
106 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100107 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +0200108}
109
110always_inline u16
111ip4_reass_buffer_get_data_len (vlib_buffer_t * b)
112{
113 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100114 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
115 (vnb->ip.reass.fragment_first + ip4_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +0200116}
117
118typedef struct
119{
120 // hash table key
121 ip4_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200122 // time when last packet was received
123 f64 last_heard;
124 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100125 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200126 // buffer index of first buffer in this reassembly context
127 u32 first_bi;
128 // last octet of packet, ~0 until fragment without more_fragments arrives
129 u32 last_packet_octet;
130 // length of data collected so far
131 u32 data_len;
132 // trace operation counter
133 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100134 // next index - used by non-feature node
Klement Sekera21aa8f12019-05-20 12:27:33 +0200135 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000136 // error next index - used by custom apps (~0 if not used)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200137 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100138 // minimum fragment length for this reassembly - used to estimate MTU
139 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200140 // number of fragments in this reassembly
141 u32 fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200142} ip4_reass_t;
143
144typedef struct
145{
Klement Sekera4c533132018-02-22 11:41:12 +0100146 ip4_reass_t *pool;
147 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100148 u32 id_counter;
149 clib_spinlock_t lock;
150} ip4_reass_per_thread_t;
151
152typedef struct
153{
Klement Sekera75e7d132017-09-20 08:26:30 +0200154 // IPv4 config
155 u32 timeout_ms;
156 f64 timeout;
157 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200158 // maximum number of fragments in one reassembly
159 u32 max_reass_len;
160 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200161 u32 max_reass_n;
162
163 // IPv4 runtime
Klement Sekera8dcfed52018-06-28 11:16:15 +0200164 clib_bihash_16_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100165 // per-thread data
166 ip4_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200167
168 // convenience
169 vlib_main_t *vlib_main;
170 vnet_main_t *vnet_main;
171
172 // node index of ip4-drop node
173 u32 ip4_drop_idx;
174 u32 ip4_reass_expire_node_idx;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800175
176 /** Worker handoff */
177 u32 fq_index;
178 u32 fq_feature_index;
179
Klement Sekera75e7d132017-09-20 08:26:30 +0200180} ip4_reass_main_t;
181
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700182extern ip4_reass_main_t ip4_reass_main;
183
184#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +0200185ip4_reass_main_t ip4_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700186#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200187
188typedef enum
189{
190 IP4_REASSEMBLY_NEXT_INPUT,
191 IP4_REASSEMBLY_NEXT_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800192 IP4_REASSEMBLY_NEXT_HANDOFF,
Klement Sekera75e7d132017-09-20 08:26:30 +0200193 IP4_REASSEMBLY_N_NEXT,
194} ip4_reass_next_t;
195
196typedef enum
197{
198 RANGE_NEW,
199 RANGE_SHRINK,
200 RANGE_DISCARD,
201 RANGE_OVERLAP,
202 FINALIZE,
203} ip4_reass_trace_operation_e;
204
205typedef struct
206{
207 u16 range_first;
208 u16 range_last;
209 u32 range_bi;
210 i32 data_offset;
211 u32 data_len;
212 u32 first_bi;
213} ip4_reass_range_trace_t;
214
215typedef struct
216{
217 ip4_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200218 u32 reass_id;
219 ip4_reass_range_trace_t trace_range;
220 u32 size_diff;
221 u32 op_id;
222 u32 fragment_first;
223 u32 fragment_last;
224 u32 total_data_len;
225} ip4_reass_trace_t;
226
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700227extern vlib_node_registration_t ip4_reass_node;
228extern vlib_node_registration_t ip4_reass_node_feature;
229
Klement Sekera4c533132018-02-22 11:41:12 +0100230static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200231ip4_reass_trace_details (vlib_main_t * vm, u32 bi,
232 ip4_reass_range_trace_t * trace)
233{
234 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
235 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
236 trace->range_first = vnb->ip.reass.range_first;
237 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100238 trace->data_offset = ip4_reass_buffer_get_data_offset (b);
239 trace->data_len = ip4_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200240 trace->range_bi = bi;
241}
242
Klement Sekera4c533132018-02-22 11:41:12 +0100243static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200244format_ip4_reass_range_trace (u8 * s, va_list * args)
245{
246 ip4_reass_range_trace_t *trace = va_arg (*args, ip4_reass_range_trace_t *);
247 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
248 trace->range_last, trace->data_offset, trace->data_len,
249 trace->range_bi);
250 return s;
251}
252
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700253static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200254format_ip4_reass_trace (u8 * s, va_list * args)
255{
256 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
257 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
258 ip4_reass_trace_t *t = va_arg (*args, ip4_reass_trace_t *);
259 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
260 u32 indent = format_get_indent (s);
261 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
262 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
263 t->fragment_last);
264 switch (t->action)
265 {
266 case RANGE_SHRINK:
267 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
268 format_ip4_reass_range_trace, &t->trace_range,
269 t->size_diff);
270 break;
271 case RANGE_DISCARD:
272 s = format (s, "\n%Udiscard %U", format_white_space, indent,
273 format_ip4_reass_range_trace, &t->trace_range);
274 break;
275 case RANGE_NEW:
276 s = format (s, "\n%Unew %U", format_white_space, indent,
277 format_ip4_reass_range_trace, &t->trace_range);
278 break;
279 case RANGE_OVERLAP:
280 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
281 format_ip4_reass_range_trace, &t->trace_range);
282 break;
283 case FINALIZE:
284 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
285 break;
286 }
287 return s;
288}
289
Klement Sekera4c533132018-02-22 11:41:12 +0100290static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200291ip4_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
292 ip4_reass_main_t * rm, ip4_reass_t * reass, u32 bi,
293 ip4_reass_trace_operation_e action, u32 size_diff)
294{
295 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
296 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
297 ip4_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200298 t->reass_id = reass->id;
299 t->action = action;
300 ip4_reass_trace_details (vm, bi, &t->trace_range);
301 t->size_diff = size_diff;
302 t->op_id = reass->trace_op_counter;
303 ++reass->trace_op_counter;
304 t->fragment_first = vnb->ip.reass.fragment_first;
305 t->fragment_last = vnb->ip.reass.fragment_last;
306 t->trace_range.first_bi = reass->first_bi;
307 t->total_data_len = reass->data_len;
308#if 0
309 static u8 *s = NULL;
310 s = format (s, "%U", format_ip4_reass_trace, NULL, NULL, t);
311 printf ("%.*s\n", vec_len (s), s);
312 fflush (stdout);
313 vec_reset_length (s);
314#endif
315}
316
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800317
Klement Sekera4c533132018-02-22 11:41:12 +0100318always_inline void
319ip4_reass_free (ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
320 ip4_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200321{
Klement Sekera8dcfed52018-06-28 11:16:15 +0200322 clib_bihash_kv_16_8_t kv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200323 kv.key[0] = reass->key.as_u64[0];
324 kv.key[1] = reass->key.as_u64[1];
Klement Sekera8dcfed52018-06-28 11:16:15 +0200325 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100326 pool_put (rt->pool, reass);
327 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200328}
329
Klement Sekera4c533132018-02-22 11:41:12 +0100330always_inline void
Klement Sekera21aa8f12019-05-20 12:27:33 +0200331ip4_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerae8498652019-06-17 12:23:15 +0000332 ip4_reass_main_t * rm, ip4_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200333{
334 u32 range_bi = reass->first_bi;
335 vlib_buffer_t *range_b;
336 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100337 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200338 while (~0 != range_bi)
339 {
340 range_b = vlib_get_buffer (vm, range_bi);
341 range_vnb = vnet_buffer (range_b);
342 u32 bi = range_bi;
343 while (~0 != bi)
344 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100345 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200346 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
347 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
348 {
349 bi = b->next_buffer;
350 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
351 }
352 else
353 {
354 bi = ~0;
355 }
356 }
357 range_bi = range_vnb->ip.reass.next_range_bi;
358 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200359 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000360 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200361 {
362 u32 n_left_to_next, *to_next, next_index;
363
364 next_index = reass->error_next_index;
365 u32 bi = ~0;
366
367 while (vec_len (to_free) > 0)
368 {
369 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
370
371 while (vec_len (to_free) > 0 && n_left_to_next > 0)
372 {
373 bi = vec_pop (to_free);
374
375 if (~0 != bi)
376 {
377 to_next[0] = bi;
378 to_next += 1;
379 n_left_to_next -= 1;
380 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
381 to_next, n_left_to_next,
382 bi, next_index);
383 }
384 }
385 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
386 }
387 }
388 else
389 {
390 vlib_buffer_free (vm, to_free, vec_len (to_free));
391 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200392}
393
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700394static ip4_reass_t *
Klement Sekera21aa8f12019-05-20 12:27:33 +0200395ip4_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
396 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
Klement Sekerae8498652019-06-17 12:23:15 +0000397 ip4_reass_kv_t * kv, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200398{
399 ip4_reass_t *reass = NULL;
400 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200401
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800402 if (!clib_bihash_search_16_8
403 (&rm->hash, (clib_bihash_kv_16_8_t *) kv, (clib_bihash_kv_16_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200404 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800405 if (vm->thread_index != kv->v.thread_index)
406 {
407 *do_handoff = 1;
408 return NULL;
409 }
410 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
411
Klement Sekera75e7d132017-09-20 08:26:30 +0200412 if (now > reass->last_heard + rm->timeout)
413 {
Klement Sekerae8498652019-06-17 12:23:15 +0000414 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100415 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200416 reass = NULL;
417 }
418 }
419
420 if (reass)
421 {
422 reass->last_heard = now;
423 return reass;
424 }
425
Klement Sekera4c533132018-02-22 11:41:12 +0100426 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200427 {
428 reass = NULL;
429 return reass;
430 }
431 else
432 {
Klement Sekera4c533132018-02-22 11:41:12 +0100433 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400434 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800435 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100436 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200437 reass->first_bi = ~0;
438 reass->last_packet_octet = ~0;
439 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000440 reass->next_index = ~0;
441 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100442 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200443 }
444
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800445 reass->key.as_u64[0] = ((clib_bihash_kv_16_8_t *) kv)->key[0];
446 reass->key.as_u64[1] = ((clib_bihash_kv_16_8_t *) kv)->key[1];
447 kv->v.reass_index = (reass - rt->pool);
448 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200449 reass->last_heard = now;
450
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800451 if (clib_bihash_add_del_16_8 (&rm->hash, (clib_bihash_kv_16_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200452 {
Klement Sekera4c533132018-02-22 11:41:12 +0100453 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200454 reass = NULL;
455 }
456
457 return reass;
458}
459
Klement Sekerad0f70a32018-12-14 17:24:13 +0100460always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200461ip4_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100462 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100463 ip4_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000464 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200465{
Klement Sekera75e7d132017-09-20 08:26:30 +0200466 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
467 vlib_buffer_t *last_b = NULL;
468 u32 sub_chain_bi = reass->first_bi;
469 u32 total_length = 0;
470 u32 buf_cnt = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200471 do
472 {
473 u32 tmp_bi = sub_chain_bi;
474 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
475 ip4_header_t *ip = vlib_buffer_get_current (tmp);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100476 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
477 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
478 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
479 {
480 return IP4_REASS_RC_INTERNAL_ERROR;
481 }
482
Klement Sekera75e7d132017-09-20 08:26:30 +0200483 u32 data_len = ip4_reass_buffer_get_data_len (tmp);
484 u32 trim_front =
485 ip4_header_bytes (ip) + ip4_reass_buffer_get_data_offset (tmp);
486 u32 trim_end =
487 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
488 if (tmp_bi == reass->first_bi)
489 {
490 /* first buffer - keep ip4 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100491 if (0 != ip4_reass_buffer_get_data_offset (tmp))
492 {
493 return IP4_REASS_RC_INTERNAL_ERROR;
494 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200495 trim_front = 0;
496 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
497 ip4_header_bytes (ip);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100498 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
499 {
500 return IP4_REASS_RC_INTERNAL_ERROR;
501 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200502 }
503 u32 keep_data =
504 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
505 while (1)
506 {
507 ++buf_cnt;
508 if (trim_front)
509 {
510 if (trim_front > tmp->current_length)
511 {
512 /* drop whole buffer */
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200513 u32 to_be_freed_bi = tmp_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200514 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100515 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
516 {
517 return IP4_REASS_RC_INTERNAL_ERROR;
518 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200519 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
520 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700521 tmp->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200522 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200523 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200524 continue;
525 }
526 else
527 {
528 vlib_buffer_advance (tmp, trim_front);
529 trim_front = 0;
530 }
531 }
532 if (keep_data)
533 {
534 if (last_b)
535 {
536 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
537 last_b->next_buffer = tmp_bi;
538 }
539 last_b = tmp;
540 if (keep_data <= tmp->current_length)
541 {
542 tmp->current_length = keep_data;
543 keep_data = 0;
544 }
545 else
546 {
547 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100548 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
549 {
550 return IP4_REASS_RC_INTERNAL_ERROR;
551 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200552 }
553 total_length += tmp->current_length;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200554 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
555 {
556 tmp_bi = tmp->next_buffer;
557 tmp = vlib_get_buffer (vm, tmp->next_buffer);
558 }
559 else
560 {
561 break;
562 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200563 }
564 else
565 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200566 u32 to_be_freed_bi = tmp_bi;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100567 if (reass->first_bi == tmp_bi)
568 {
569 return IP4_REASS_RC_INTERNAL_ERROR;
570 }
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200571 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
572 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700573 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200574 tmp_bi = tmp->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700575 tmp->next_buffer = 0;
576 tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200577 vlib_buffer_free_one (vm, to_be_freed_bi);
578 }
579 else
580 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700581 tmp->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200582 vlib_buffer_free_one (vm, to_be_freed_bi);
583 break;
584 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200585 }
586 }
587 sub_chain_bi =
588 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
589 reass.next_range_bi;
590 }
591 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700592
Klement Sekerad0f70a32018-12-14 17:24:13 +0100593 if (!last_b)
594 {
595 return IP4_REASS_RC_INTERNAL_ERROR;
596 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200597 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700598
Klement Sekerad0f70a32018-12-14 17:24:13 +0100599 if (total_length < first_b->current_length)
600 {
601 return IP4_REASS_RC_INTERNAL_ERROR;
602 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200603 total_length -= first_b->current_length;
604 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
605 first_b->total_length_not_including_first_buffer = total_length;
606 ip4_header_t *ip = vlib_buffer_get_current (first_b);
607 ip->flags_and_fragment_offset = 0;
608 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
609 ip->checksum = ip4_header_checksum (ip);
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100610 if (!vlib_buffer_chain_linearize (vm, first_b))
611 {
612 return IP4_REASS_RC_NO_BUF;
613 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700614 // reset to reconstruct the mbuf linking
615 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200616 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
617 {
618 ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
619#if 0
620 // following code does a hexdump of packet fragments to stdout ...
621 do
622 {
623 u32 bi = reass->first_bi;
624 u8 *s = NULL;
625 while (~0 != bi)
626 {
627 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
628 s = format (s, "%u: %U\n", bi, format_hexdump,
629 vlib_buffer_get_current (b), b->current_length);
630 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
631 {
632 bi = b->next_buffer;
633 }
634 else
635 {
636 break;
637 }
638 }
639 printf ("%.*s\n", vec_len (s), s);
640 fflush (stdout);
641 vec_free (s);
642 }
643 while (0);
644#endif
645 }
646 *bi0 = reass->first_bi;
Klement Sekerae8498652019-06-17 12:23:15 +0000647 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100648 {
649 *next0 = IP4_REASSEMBLY_NEXT_INPUT;
650 }
651 else
652 {
653 *next0 = reass->next_index;
654 }
655 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200656 *error0 = IP4_ERROR_NONE;
Klement Sekera4c533132018-02-22 11:41:12 +0100657 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200658 reass = NULL;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100659 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200660}
661
Klement Sekerad0f70a32018-12-14 17:24:13 +0100662always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200663ip4_reass_insert_range_in_chain (vlib_main_t * vm,
664 ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100665 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200666 ip4_reass_t * reass,
667 u32 prev_range_bi, u32 new_next_bi)
668{
Klement Sekera75e7d132017-09-20 08:26:30 +0200669 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
670 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
671 if (~0 != prev_range_bi)
672 {
673 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
674 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
675 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
676 prev_vnb->ip.reass.next_range_bi = new_next_bi;
677 }
678 else
679 {
680 if (~0 != reass->first_bi)
681 {
682 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
683 }
684 reass->first_bi = new_next_bi;
685 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100686 vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
687 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
688 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
689 {
690 return IP4_REASS_RC_INTERNAL_ERROR;
691 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200692 reass->data_len += ip4_reass_buffer_get_data_len (new_next_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100693 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200694}
695
Klement Sekerad0f70a32018-12-14 17:24:13 +0100696always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200697ip4_reass_remove_range_from_chain (vlib_main_t * vm,
698 vlib_node_runtime_t * node,
699 ip4_reass_main_t * rm,
Klement Sekera75e7d132017-09-20 08:26:30 +0200700 ip4_reass_t * reass, u32 prev_range_bi,
701 u32 discard_bi)
702{
703 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
704 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
705 if (~0 != prev_range_bi)
706 {
707 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
708 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100709 if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
710 {
711 return IP4_REASS_RC_INTERNAL_ERROR;
712 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200713 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
714 }
715 else
716 {
717 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
718 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100719 vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
720 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
721 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
722 {
723 return IP4_REASS_RC_INTERNAL_ERROR;
724 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200725 reass->data_len -= ip4_reass_buffer_get_data_len (discard_b);
726 while (1)
727 {
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200728 u32 to_be_freed_bi = discard_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200729 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
730 {
731 ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD,
732 0);
733 }
734 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
735 {
736 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
737 discard_bi = discard_b->next_buffer;
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700738 discard_b->next_buffer = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200739 discard_b = vlib_get_buffer (vm, discard_bi);
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200740 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200741 }
742 else
743 {
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700744 discard_b->next_buffer = 0;
Klement Sekeraf369e3a2019-04-30 13:01:08 +0200745 vlib_buffer_free_one (vm, to_be_freed_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200746 break;
747 }
748 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100749 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200750}
751
Klement Sekerad0f70a32018-12-14 17:24:13 +0100752always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200753ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100754 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100755 ip4_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000756 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200757{
Klement Sekerad0f70a32018-12-14 17:24:13 +0100758 ip4_reass_rc_t rc = IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200759 int consumed = 0;
760 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
761 ip4_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekera75e7d132017-09-20 08:26:30 +0200762 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000763 if (is_custom_app)
764 {
765 // store (error_)next_index before it's overwritten
766 reass->next_index = fvnb->ip.reass.next_index;
767 reass->error_next_index = fvnb->ip.reass.error_next_index;
768 }
Klement Sekera14d7e902018-12-10 13:46:09 +0100769 const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
770 const u32 fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200771 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
Klement Sekera14d7e902018-12-10 13:46:09 +0100772 const u32 fragment_last = fragment_first + fragment_length - 1;
773 fvnb->ip.reass.fragment_first = fragment_first;
774 fvnb->ip.reass.fragment_last = fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200775 int more_fragments = ip4_get_fragment_more (fip);
776 u32 candidate_range_bi = reass->first_bi;
777 u32 prev_range_bi = ~0;
778 fvnb->ip.reass.range_first = fragment_first;
779 fvnb->ip.reass.range_last = fragment_last;
780 fvnb->ip.reass.next_range_bi = ~0;
781 if (!more_fragments)
782 {
783 reass->last_packet_octet = fragment_last;
784 }
785 if (~0 == reass->first_bi)
786 {
787 // starting a new reassembly
Klement Sekerad0f70a32018-12-14 17:24:13 +0100788 rc =
789 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
790 *bi0);
791 if (IP4_REASS_RC_OK != rc)
792 {
793 return rc;
794 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200795 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
796 {
797 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
798 }
799 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100800 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekera3a343d42019-05-16 14:35:46 +0200801 reass->fragments_n = 1;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100802 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200803 }
Klement Sekera4c533132018-02-22 11:41:12 +0100804 reass->min_fragment_length = clib_min (clib_net_to_host_u16 (fip->length),
805 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200806 while (~0 != candidate_range_bi)
807 {
808 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
809 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
810 if (fragment_first > candidate_vnb->ip.reass.range_last)
811 {
812 // this fragments starts after candidate range
813 prev_range_bi = candidate_range_bi;
814 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
815 if (candidate_vnb->ip.reass.range_last < fragment_last &&
816 ~0 == candidate_range_bi)
817 {
818 // special case - this fragment falls beyond all known ranges
Klement Sekerad0f70a32018-12-14 17:24:13 +0100819 rc =
820 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
821 prev_range_bi, *bi0);
822 if (IP4_REASS_RC_OK != rc)
823 {
824 return rc;
825 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200826 consumed = 1;
827 break;
828 }
829 continue;
830 }
831 if (fragment_last < candidate_vnb->ip.reass.range_first)
832 {
833 // this fragment ends before candidate range without any overlap
Klement Sekerad0f70a32018-12-14 17:24:13 +0100834 rc =
835 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
836 *bi0);
837 if (IP4_REASS_RC_OK != rc)
838 {
839 return rc;
840 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200841 consumed = 1;
842 }
843 else
844 {
845 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
846 fragment_last <= candidate_vnb->ip.reass.range_last)
847 {
848 // this fragment is a (sub)part of existing range, ignore it
849 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
850 {
851 ip4_reass_add_trace (vm, node, rm, reass, *bi0,
852 RANGE_OVERLAP, 0);
853 }
854 break;
855 }
856 int discard_candidate = 0;
857 if (fragment_first < candidate_vnb->ip.reass.range_first)
858 {
859 u32 overlap =
860 fragment_last - candidate_vnb->ip.reass.range_first + 1;
861 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
862 {
863 candidate_vnb->ip.reass.range_first += overlap;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100864 if (reass->data_len < overlap)
865 {
866 return IP4_REASS_RC_INTERNAL_ERROR;
867 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200868 reass->data_len -= overlap;
869 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
870 {
871 ip4_reass_add_trace (vm, node, rm, reass,
872 candidate_range_bi, RANGE_SHRINK,
873 overlap);
874 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100875 rc =
876 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
877 prev_range_bi, *bi0);
878 if (IP4_REASS_RC_OK != rc)
879 {
880 return rc;
881 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200882 consumed = 1;
883 }
884 else
885 {
886 discard_candidate = 1;
887 }
888 }
889 else if (fragment_last > candidate_vnb->ip.reass.range_last)
890 {
891 u32 overlap =
892 candidate_vnb->ip.reass.range_last - fragment_first + 1;
893 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
894 {
895 fvnb->ip.reass.range_first += overlap;
896 if (~0 != candidate_vnb->ip.reass.next_range_bi)
897 {
898 prev_range_bi = candidate_range_bi;
899 candidate_range_bi =
900 candidate_vnb->ip.reass.next_range_bi;
901 continue;
902 }
903 else
904 {
905 // special case - last range discarded
Klement Sekerad0f70a32018-12-14 17:24:13 +0100906 rc =
907 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
908 candidate_range_bi,
909 *bi0);
910 if (IP4_REASS_RC_OK != rc)
911 {
912 return rc;
913 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200914 consumed = 1;
915 }
916 }
917 else
918 {
919 discard_candidate = 1;
920 }
921 }
922 else
923 {
924 discard_candidate = 1;
925 }
926 if (discard_candidate)
927 {
928 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
929 // discard candidate range, probe next range
Klement Sekerad0f70a32018-12-14 17:24:13 +0100930 rc =
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100931 ip4_reass_remove_range_from_chain (vm, node, rm, reass,
Klement Sekerad0f70a32018-12-14 17:24:13 +0100932 prev_range_bi,
933 candidate_range_bi);
934 if (IP4_REASS_RC_OK != rc)
935 {
936 return rc;
937 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200938 if (~0 != next_range_bi)
939 {
940 candidate_range_bi = next_range_bi;
941 continue;
942 }
943 else
944 {
945 // special case - last range discarded
Klement Sekerad0f70a32018-12-14 17:24:13 +0100946 rc =
947 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
948 prev_range_bi, *bi0);
949 if (IP4_REASS_RC_OK != rc)
950 {
951 return rc;
952 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200953 consumed = 1;
954 }
955 }
956 }
957 break;
958 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200959 ++reass->fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200960 if (consumed)
961 {
962 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
963 {
964 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
965 }
966 }
967 if (~0 != reass->last_packet_octet &&
968 reass->data_len == reass->last_packet_octet + 1)
969 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100970 return ip4_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000971 is_custom_app);
Klement Sekera75e7d132017-09-20 08:26:30 +0200972 }
973 else
974 {
975 if (consumed)
976 {
977 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200978 if (reass->fragments_n > rm->max_reass_len)
979 {
980 rc = IP4_REASS_RC_TOO_MANY_FRAGMENTS;
981 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200982 }
983 else
984 {
Klement Sekera4c533132018-02-22 11:41:12 +0100985 *next0 = IP4_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200986 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
987 }
988 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100989 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +0200990}
991
992always_inline uword
Klement Sekerae8498652019-06-17 12:23:15 +0000993ip4_reassembly_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
994 vlib_frame_t * frame, bool is_feature,
995 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200996{
997 u32 *from = vlib_frame_vector_args (frame);
998 u32 n_left_from, n_left_to_next, *to_next, next_index;
999 ip4_reass_main_t *rm = &ip4_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001000 ip4_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001001 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001002
1003 n_left_from = frame->n_vectors;
1004 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001005 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001006 {
1007 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1008
Klement Sekera75e7d132017-09-20 08:26:30 +02001009 while (n_left_from > 0 && n_left_to_next > 0)
1010 {
1011 u32 bi0;
1012 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +01001013 u32 next0;
1014 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +02001015
1016 bi0 = from[0];
1017 b0 = vlib_get_buffer (vm, bi0);
1018
1019 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001020 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +02001021 {
Klement Sekera4c533132018-02-22 11:41:12 +01001022 // this is a whole packet - no fragmentation
Klement Sekerae8498652019-06-17 12:23:15 +00001023 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +01001024 {
1025 next0 = IP4_REASSEMBLY_NEXT_INPUT;
1026 }
1027 else
1028 {
1029 next0 = vnet_buffer (b0)->ip.reass.next_index;
1030 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001031 }
1032 else
1033 {
Klement Sekera4ee633e2018-12-14 12:00:44 +01001034 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
Klement Sekera14d7e902018-12-10 13:46:09 +01001035 const u32 fragment_length =
Klement Sekera4ee633e2018-12-14 12:00:44 +01001036 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
Klement Sekera14d7e902018-12-10 13:46:09 +01001037 const u32 fragment_last = fragment_first + fragment_length - 1;
Klement Sekera4ee633e2018-12-14 12:00:44 +01001038 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
Klement Sekera4c533132018-02-22 11:41:12 +01001039 {
Klement Sekera14d7e902018-12-10 13:46:09 +01001040 next0 = IP4_REASSEMBLY_NEXT_DROP;
1041 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
Klement Sekera4c533132018-02-22 11:41:12 +01001042 }
1043 else
1044 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001045 ip4_reass_kv_t kv;
1046 u8 do_handoff = 0;
1047
1048 kv.k.as_u64[0] =
1049 (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
1050 vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
Klement Sekerad0f70a32018-12-14 17:24:13 +01001051 (u64) ip0->src_address.as_u32 << 32;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001052 kv.k.as_u64[1] =
Klement Sekerad0f70a32018-12-14 17:24:13 +01001053 (u64) ip0->dst_address.as_u32 |
1054 (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
Klement Sekera14d7e902018-12-10 13:46:09 +01001055
1056 ip4_reass_t *reass =
Klement Sekera21aa8f12019-05-20 12:27:33 +02001057 ip4_reass_find_or_create (vm, node, rm, rt, &kv,
Klement Sekerae8498652019-06-17 12:23:15 +00001058 &do_handoff);
Klement Sekera14d7e902018-12-10 13:46:09 +01001059
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001060 if (PREDICT_FALSE (do_handoff))
1061 {
1062 next0 = IP4_REASSEMBLY_NEXT_HANDOFF;
1063 if (is_feature)
1064 vnet_buffer (b0)->ip.
1065 reass.owner_feature_thread_index =
1066 kv.v.thread_index;
1067 else
1068 vnet_buffer (b0)->ip.reass.owner_thread_index =
1069 kv.v.thread_index;
1070 }
1071 else if (reass)
Klement Sekera14d7e902018-12-10 13:46:09 +01001072 {
Klement Sekerad0f70a32018-12-14 17:24:13 +01001073 switch (ip4_reass_update
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001074 (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekerae8498652019-06-17 12:23:15 +00001075 &error0, is_custom_app))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001076 {
1077 case IP4_REASS_RC_OK:
1078 /* nothing to do here */
1079 break;
Klement Sekera3a343d42019-05-16 14:35:46 +02001080 case IP4_REASS_RC_TOO_MANY_FRAGMENTS:
1081 vlib_node_increment_counter (vm, node->node_index,
1082 IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1083 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001084 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001085 ip4_reass_free (rm, rt, reass);
1086 goto next_packet;
1087 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001088 case IP4_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001089 vlib_node_increment_counter (vm, node->node_index,
1090 IP4_ERROR_REASS_NO_BUF,
1091 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001092 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001093 ip4_reass_free (rm, rt, reass);
1094 goto next_packet;
1095 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001096 case IP4_REASS_RC_INTERNAL_ERROR:
Klement Sekera21aa8f12019-05-20 12:27:33 +02001097 /* drop everything and start with a clean slate */
Klement Sekera3a343d42019-05-16 14:35:46 +02001098 vlib_node_increment_counter (vm, node->node_index,
1099 IP4_ERROR_REASS_INTERNAL_ERROR,
1100 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001101 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001102 ip4_reass_free (rm, rt, reass);
1103 goto next_packet;
1104 break;
1105 }
Klement Sekera14d7e902018-12-10 13:46:09 +01001106 }
1107 else
1108 {
1109 next0 = IP4_REASSEMBLY_NEXT_DROP;
1110 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
1111 }
Klement Sekera4c533132018-02-22 11:41:12 +01001112 }
1113
1114 b0->error = node->errors[error0];
1115 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001116
1117 if (bi0 != ~0)
1118 {
1119 to_next[0] = bi0;
1120 to_next += 1;
1121 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001122 if (is_feature && IP4_ERROR_NONE == error0)
1123 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001124 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001125 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001126 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001127 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1128 to_next, n_left_to_next,
1129 bi0, next0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001130 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1131 }
1132
Klement Sekerad0f70a32018-12-14 17:24:13 +01001133 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001134 from += 1;
1135 n_left_from -= 1;
1136 }
1137
1138 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1139 }
1140
Klement Sekera4c533132018-02-22 11:41:12 +01001141 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001142 return frame->n_vectors;
1143}
1144
1145static char *ip4_reassembly_error_strings[] = {
1146#define _(sym, string) string,
1147 foreach_ip4_error
1148#undef _
1149};
1150
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001151VLIB_NODE_FN (ip4_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1152 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001153{
Klement Sekerae8498652019-06-17 12:23:15 +00001154 return ip4_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1155 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001156}
1157
Klement Sekera75e7d132017-09-20 08:26:30 +02001158/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001159VLIB_REGISTER_NODE (ip4_reass_node) = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001160 .name = "ip4-reassembly",
1161 .vector_size = sizeof (u32),
1162 .format_trace = format_ip4_reass_trace,
1163 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1164 .error_strings = ip4_reassembly_error_strings,
1165 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1166 .next_nodes =
1167 {
1168 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1169 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001170 [IP4_REASSEMBLY_NEXT_HANDOFF] = "ip4-reassembly-handoff",
1171
Klement Sekera75e7d132017-09-20 08:26:30 +02001172 },
1173};
1174/* *INDENT-ON* */
1175
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001176VLIB_NODE_FN (ip4_reass_node_feature) (vlib_main_t * vm,
1177 vlib_node_runtime_t * node,
1178 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001179{
Klement Sekerae8498652019-06-17 12:23:15 +00001180 return ip4_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1181 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001182}
1183
1184/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001185VLIB_REGISTER_NODE (ip4_reass_node_feature) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001186 .name = "ip4-reassembly-feature",
1187 .vector_size = sizeof (u32),
1188 .format_trace = format_ip4_reass_trace,
1189 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1190 .error_strings = ip4_reassembly_error_strings,
1191 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1192 .next_nodes =
1193 {
1194 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1195 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001196 [IP4_REASSEMBLY_NEXT_HANDOFF] = "ip4-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001197 },
1198};
1199/* *INDENT-ON* */
1200
Klement Sekera4c533132018-02-22 11:41:12 +01001201/* *INDENT-OFF* */
1202VNET_FEATURE_INIT (ip4_reassembly_feature, static) = {
1203 .arc_name = "ip4-unicast",
1204 .node_name = "ip4-reassembly-feature",
1205 .runs_before = VNET_FEATURES ("ip4-lookup"),
1206 .runs_after = 0,
1207};
1208/* *INDENT-ON* */
1209
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001210#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001211always_inline u32
1212ip4_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001213{
1214 ip4_reass_main_t *rm = &ip4_reass_main;
1215 u32 nbuckets;
1216 u8 i;
1217
1218 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1219
1220 for (i = 0; i < 31; i++)
1221 if ((1 << i) >= nbuckets)
1222 break;
1223 nbuckets = 1 << i;
1224
1225 return nbuckets;
1226}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001227#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001228
1229typedef enum
1230{
1231 IP4_EVENT_CONFIG_CHANGED = 1,
1232} ip4_reass_event_t;
1233
1234typedef struct
1235{
1236 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001237 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001238} ip4_rehash_cb_ctx;
1239
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001240#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001241static void
Klement Sekera8dcfed52018-06-28 11:16:15 +02001242ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001243{
1244 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001245 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001246 {
1247 ctx->failure = 1;
1248 }
1249}
1250
Klement Sekera4c533132018-02-22 11:41:12 +01001251static void
1252ip4_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001253 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001254{
1255 ip4_reass_main.timeout_ms = timeout_ms;
1256 ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1257 ip4_reass_main.max_reass_n = max_reassemblies;
Klement Sekera3a343d42019-05-16 14:35:46 +02001258 ip4_reass_main.max_reass_len = max_reassembly_length;
Klement Sekera4c533132018-02-22 11:41:12 +01001259 ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1260}
1261
Klement Sekera75e7d132017-09-20 08:26:30 +02001262vnet_api_error_t
1263ip4_reass_set (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001264 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001265{
1266 u32 old_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3a343d42019-05-16 14:35:46 +02001267 ip4_reass_set_params (timeout_ms, max_reassemblies, max_reassembly_length,
Klement Sekera4c533132018-02-22 11:41:12 +01001268 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001269 vlib_process_signal_event (ip4_reass_main.vlib_main,
1270 ip4_reass_main.ip4_reass_expire_node_idx,
1271 IP4_EVENT_CONFIG_CHANGED, 0);
1272 u32 new_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001273 if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001274 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001275 clib_bihash_16_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001276 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001277 ip4_rehash_cb_ctx ctx;
1278 ctx.failure = 0;
1279 ctx.new_hash = &new_hash;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001280 clib_bihash_init_16_8 (&new_hash, "ip4-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001281 new_nbuckets * 1024);
Klement Sekera8dcfed52018-06-28 11:16:15 +02001282 clib_bihash_foreach_key_value_pair_16_8 (&ip4_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001283 ip4_rehash_cb, &ctx);
1284 if (ctx.failure)
1285 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001286 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001287 return -1;
1288 }
1289 else
1290 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001291 clib_bihash_free_16_8 (&ip4_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001292 clib_memcpy_fast (&ip4_reass_main.hash, &new_hash,
1293 sizeof (ip4_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001294 }
1295 }
1296 return 0;
1297}
1298
1299vnet_api_error_t
1300ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001301 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001302{
1303 *timeout_ms = ip4_reass_main.timeout_ms;
1304 *max_reassemblies = ip4_reass_main.max_reass_n;
Klement Sekera3a343d42019-05-16 14:35:46 +02001305 *max_reassembly_length = ip4_reass_main.max_reass_len;
Klement Sekera75e7d132017-09-20 08:26:30 +02001306 *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms;
1307 return 0;
1308}
1309
Klement Sekera4c533132018-02-22 11:41:12 +01001310static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001311ip4_reass_init_function (vlib_main_t * vm)
1312{
1313 ip4_reass_main_t *rm = &ip4_reass_main;
1314 clib_error_t *error = 0;
1315 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001316 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001317
1318 rm->vlib_main = vm;
1319 rm->vnet_main = vnet_get_main ();
1320
Juraj Slobodacd806922018-10-10 10:15:54 +02001321 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001322 ip4_reass_per_thread_t *rt;
1323 vec_foreach (rt, rm->per_thread_data)
1324 {
1325 clib_spinlock_init (&rt->lock);
1326 pool_alloc (rt->pool, rm->max_reass_n);
1327 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001328
1329 node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk");
1330 ASSERT (node);
1331 rm->ip4_reass_expire_node_idx = node->index;
1332
Klement Sekera3ecc2212018-03-27 10:34:43 +02001333 ip4_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1334 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
Klement Sekera3a343d42019-05-16 14:35:46 +02001335 IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
Klement Sekera3ecc2212018-03-27 10:34:43 +02001336 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1337
Klement Sekera75e7d132017-09-20 08:26:30 +02001338 nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera8dcfed52018-06-28 11:16:15 +02001339 clib_bihash_init_16_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001340
Dave Barach1403fcd2018-02-05 09:45:43 -05001341 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001342 ASSERT (node);
1343 rm->ip4_drop_idx = node->index;
Klement Sekera4c533132018-02-22 11:41:12 +01001344
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001345 rm->fq_index = vlib_frame_queue_main_init (ip4_reass_node.index, 0);
1346 rm->fq_feature_index =
1347 vlib_frame_queue_main_init (ip4_reass_node_feature.index, 0);
1348
1349
Klement Sekera75e7d132017-09-20 08:26:30 +02001350 return error;
1351}
1352
1353VLIB_INIT_FUNCTION (ip4_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001354#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001355
1356static uword
1357ip4_reass_walk_expired (vlib_main_t * vm,
1358 vlib_node_runtime_t * node, vlib_frame_t * f)
1359{
1360 ip4_reass_main_t *rm = &ip4_reass_main;
1361 uword event_type, *event_data = 0;
1362
1363 while (true)
1364 {
1365 vlib_process_wait_for_event_or_clock (vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001366 (f64)
1367 rm->expire_walk_interval_ms /
1368 (f64) MSEC_PER_SEC);
Klement Sekera75e7d132017-09-20 08:26:30 +02001369 event_type = vlib_process_get_events (vm, &event_data);
1370
1371 switch (event_type)
1372 {
1373 case ~0: /* no events => timeout */
1374 /* nothing to do here */
1375 break;
1376 case IP4_EVENT_CONFIG_CHANGED:
1377 break;
1378 default:
1379 clib_warning ("BUG: event type 0x%wx", event_type);
1380 break;
1381 }
1382 f64 now = vlib_time_now (vm);
1383
1384 ip4_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001385 int *pool_indexes_to_free = NULL;
1386
Klement Sekera4c533132018-02-22 11:41:12 +01001387 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001388 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001389 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001390 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1391 {
1392 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1393 clib_spinlock_lock (&rt->lock);
1394
1395 vec_reset_length (pool_indexes_to_free);
1396 /* *INDENT-OFF* */
1397 pool_foreach_index (index, rt->pool, ({
1398 reass = pool_elt_at_index (rt->pool, index);
1399 if (now > reass->last_heard + rm->timeout)
1400 {
1401 vec_add1 (pool_indexes_to_free, index);
1402 }
1403 }));
1404 /* *INDENT-ON* */
1405 int *i;
1406 /* *INDENT-OFF* */
1407 vec_foreach (i, pool_indexes_to_free)
1408 {
1409 ip4_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekerae8498652019-06-17 12:23:15 +00001410 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +01001411 ip4_reass_free (rm, rt, reass);
1412 }
1413 /* *INDENT-ON* */
1414
1415 clib_spinlock_unlock (&rt->lock);
1416 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001417
Klement Sekera75e7d132017-09-20 08:26:30 +02001418 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001419 if (event_data)
1420 {
1421 _vec_len (event_data) = 0;
1422 }
1423 }
1424
1425 return 0;
1426}
1427
Klement Sekera75e7d132017-09-20 08:26:30 +02001428/* *INDENT-OFF* */
1429VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = {
1430 .function = ip4_reass_walk_expired,
1431 .type = VLIB_NODE_TYPE_PROCESS,
1432 .name = "ip4-reassembly-expire-walk",
1433 .format_trace = format_ip4_reass_trace,
1434 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1435 .error_strings = ip4_reassembly_error_strings,
1436
1437};
1438/* *INDENT-ON* */
1439
1440static u8 *
1441format_ip4_reass_key (u8 * s, va_list * args)
1442{
1443 ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *);
1444 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1445 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1446 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1447 return s;
1448}
1449
1450static u8 *
1451format_ip4_reass (u8 * s, va_list * args)
1452{
1453 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1454 ip4_reass_t *reass = va_arg (*args, ip4_reass_t *);
1455
Klement Sekera4c533132018-02-22 11:41:12 +01001456 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001457 "last_packet_octet: %u, trace_op_counter: %u\n",
1458 reass->id, format_ip4_reass_key, &reass->key, reass->first_bi,
1459 reass->data_len, reass->last_packet_octet,
1460 reass->trace_op_counter);
1461 u32 bi = reass->first_bi;
1462 u32 counter = 0;
1463 while (~0 != bi)
1464 {
1465 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1466 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1467 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1468 "fragment[%u, %u]\n",
1469 counter, vnb->ip.reass.range_first,
1470 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001471 ip4_reass_buffer_get_data_offset (b),
1472 ip4_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001473 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1474 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1475 {
1476 bi = b->next_buffer;
1477 }
1478 else
1479 {
1480 bi = ~0;
1481 }
1482 }
1483 return s;
1484}
1485
1486static clib_error_t *
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001487show_ip4_reass (vlib_main_t * vm,
1488 unformat_input_t * input,
Klement Sekera75e7d132017-09-20 08:26:30 +02001489 CLIB_UNUSED (vlib_cli_command_t * lmd))
1490{
1491 ip4_reass_main_t *rm = &ip4_reass_main;
1492
1493 vlib_cli_output (vm, "---------------------");
1494 vlib_cli_output (vm, "IP4 reassembly status");
1495 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001496 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001497 if (unformat (input, "details"))
1498 {
Klement Sekera4c533132018-02-22 11:41:12 +01001499 details = true;
1500 }
1501
1502 u32 sum_reass_n = 0;
Klement Sekera4c533132018-02-22 11:41:12 +01001503 ip4_reass_t *reass;
1504 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001505 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001506 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1507 {
1508 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1509 clib_spinlock_lock (&rt->lock);
1510 if (details)
1511 {
1512 /* *INDENT-OFF* */
1513 pool_foreach (reass, rt->pool, {
1514 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1515 });
1516 /* *INDENT-ON* */
1517 }
1518 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001519 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001520 }
1521 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001522 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1523 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001524 vlib_cli_output (vm,
Klement Sekera4c533132018-02-22 11:41:12 +01001525 "Maximum configured concurrent IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001526 (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001527 return 0;
1528}
1529
1530/* *INDENT-OFF* */
1531VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = {
1532 .path = "show ip4-reassembly",
1533 .short_help = "show ip4-reassembly [details]",
1534 .function = show_ip4_reass,
1535};
1536/* *INDENT-ON* */
1537
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001538#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001539vnet_api_error_t
1540ip4_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1541{
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001542 return vnet_feature_enable_disable ("ip4-unicast",
1543 "ip4-reassembly-feature", sw_if_index,
1544 enable_disable, 0, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001545}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001546#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001547
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001548
1549#define foreach_ip4_reassembly_handoff_error \
1550_(CONGESTION_DROP, "congestion drop")
1551
1552
1553typedef enum
1554{
1555#define _(sym,str) IP4_REASSEMBLY_HANDOFF_ERROR_##sym,
1556 foreach_ip4_reassembly_handoff_error
1557#undef _
1558 IP4_REASSEMBLY_HANDOFF_N_ERROR,
1559} ip4_reassembly_handoff_error_t;
1560
1561static char *ip4_reassembly_handoff_error_strings[] = {
1562#define _(sym,string) string,
1563 foreach_ip4_reassembly_handoff_error
1564#undef _
1565};
1566
1567typedef struct
1568{
1569 u32 next_worker_index;
1570} ip4_reassembly_handoff_trace_t;
1571
1572static u8 *
1573format_ip4_reassembly_handoff_trace (u8 * s, va_list * args)
1574{
1575 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1576 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1577 ip4_reassembly_handoff_trace_t *t =
1578 va_arg (*args, ip4_reassembly_handoff_trace_t *);
1579
1580 s =
1581 format (s, "ip4-reassembly-handoff: next-worker %d",
1582 t->next_worker_index);
1583
1584 return s;
1585}
1586
1587always_inline uword
1588ip4_reassembly_handoff_node_inline (vlib_main_t * vm,
1589 vlib_node_runtime_t * node,
1590 vlib_frame_t * frame, bool is_feature)
1591{
1592 ip4_reass_main_t *rm = &ip4_reass_main;
1593
1594 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1595 u32 n_enq, n_left_from, *from;
1596 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1597 u32 fq_index;
1598
1599 from = vlib_frame_vector_args (frame);
1600 n_left_from = frame->n_vectors;
1601 vlib_get_buffers (vm, from, bufs, n_left_from);
1602
1603 b = bufs;
1604 ti = thread_indices;
1605
1606 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1607
1608 while (n_left_from > 0)
1609 {
1610 ti[0] =
1611 (is_feature) ? vnet_buffer (b[0])->ip.
1612 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1613 reass.owner_thread_index;
1614
1615 if (PREDICT_FALSE
1616 ((node->flags & VLIB_NODE_FLAG_TRACE)
1617 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1618 {
1619 ip4_reassembly_handoff_trace_t *t =
1620 vlib_add_trace (vm, node, b[0], sizeof (*t));
1621 t->next_worker_index = ti[0];
1622 }
1623
1624 n_left_from -= 1;
1625 ti += 1;
1626 b += 1;
1627 }
1628 n_enq =
1629 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1630 frame->n_vectors, 1);
1631
1632 if (n_enq < frame->n_vectors)
1633 vlib_node_increment_counter (vm, node->node_index,
1634 IP4_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1635 frame->n_vectors - n_enq);
1636 return frame->n_vectors;
1637}
1638
1639VLIB_NODE_FN (ip4_reassembly_handoff_node) (vlib_main_t * vm,
1640 vlib_node_runtime_t * node,
1641 vlib_frame_t * frame)
1642{
1643 return ip4_reassembly_handoff_node_inline (vm, node, frame,
1644 false /* is_feature */ );
1645}
1646
1647
1648/* *INDENT-OFF* */
1649VLIB_REGISTER_NODE (ip4_reassembly_handoff_node) = {
1650 .name = "ip4-reassembly-handoff",
1651 .vector_size = sizeof (u32),
1652 .n_errors = ARRAY_LEN(ip4_reassembly_handoff_error_strings),
1653 .error_strings = ip4_reassembly_handoff_error_strings,
1654 .format_trace = format_ip4_reassembly_handoff_trace,
1655
1656 .n_next_nodes = 1,
1657
1658 .next_nodes = {
1659 [0] = "error-drop",
1660 },
1661};
1662/* *INDENT-ON* */
1663
1664
1665/* *INDENT-OFF* */
1666VLIB_NODE_FN (ip4_reassembly_feature_handoff_node) (vlib_main_t * vm,
1667 vlib_node_runtime_t *
1668 node,
1669 vlib_frame_t * frame)
1670{
1671 return ip4_reassembly_handoff_node_inline (vm, node, frame,
1672 true /* is_feature */ );
1673}
1674/* *INDENT-ON* */
1675
1676
1677/* *INDENT-OFF* */
1678VLIB_REGISTER_NODE (ip4_reassembly_feature_handoff_node) = {
1679 .name = "ip4-reass-feature-hoff",
1680 .vector_size = sizeof (u32),
1681 .n_errors = ARRAY_LEN(ip4_reassembly_handoff_error_strings),
1682 .error_strings = ip4_reassembly_handoff_error_strings,
1683 .format_trace = format_ip4_reassembly_handoff_trace,
1684
1685 .n_next_nodes = 1,
1686
1687 .next_nodes = {
1688 [0] = "error-drop",
1689 },
1690};
1691/* *INDENT-ON* */
1692
Klement Sekera75e7d132017-09-20 08:26:30 +02001693/*
1694 * fd.io coding-style-patch-verification: ON
1695 *
1696 * Local Variables:
1697 * eval: (c-set-style "gnu")
1698 * End:
1699 */