blob: b82bafeac54ab20550ee7e39776fd4655185bba1 [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",
Neale Ranns14046982019-07-29 14:49:52 +00001205 .runs_before = VNET_FEATURES ("ip4-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001206 "ipsec4-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001207 .runs_after = 0,
1208};
1209/* *INDENT-ON* */
1210
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001211#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001212always_inline u32
1213ip4_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001214{
1215 ip4_reass_main_t *rm = &ip4_reass_main;
1216 u32 nbuckets;
1217 u8 i;
1218
1219 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1220
1221 for (i = 0; i < 31; i++)
1222 if ((1 << i) >= nbuckets)
1223 break;
1224 nbuckets = 1 << i;
1225
1226 return nbuckets;
1227}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001228#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001229
1230typedef enum
1231{
1232 IP4_EVENT_CONFIG_CHANGED = 1,
1233} ip4_reass_event_t;
1234
1235typedef struct
1236{
1237 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001238 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001239} ip4_rehash_cb_ctx;
1240
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001241#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001242static void
Klement Sekera8dcfed52018-06-28 11:16:15 +02001243ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001244{
1245 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001246 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001247 {
1248 ctx->failure = 1;
1249 }
1250}
1251
Klement Sekera4c533132018-02-22 11:41:12 +01001252static void
1253ip4_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001254 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001255{
1256 ip4_reass_main.timeout_ms = timeout_ms;
1257 ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1258 ip4_reass_main.max_reass_n = max_reassemblies;
Klement Sekera3a343d42019-05-16 14:35:46 +02001259 ip4_reass_main.max_reass_len = max_reassembly_length;
Klement Sekera4c533132018-02-22 11:41:12 +01001260 ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1261}
1262
Klement Sekera75e7d132017-09-20 08:26:30 +02001263vnet_api_error_t
1264ip4_reass_set (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001265 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001266{
1267 u32 old_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3a343d42019-05-16 14:35:46 +02001268 ip4_reass_set_params (timeout_ms, max_reassemblies, max_reassembly_length,
Klement Sekera4c533132018-02-22 11:41:12 +01001269 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001270 vlib_process_signal_event (ip4_reass_main.vlib_main,
1271 ip4_reass_main.ip4_reass_expire_node_idx,
1272 IP4_EVENT_CONFIG_CHANGED, 0);
1273 u32 new_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001274 if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001275 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001276 clib_bihash_16_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001277 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001278 ip4_rehash_cb_ctx ctx;
1279 ctx.failure = 0;
1280 ctx.new_hash = &new_hash;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001281 clib_bihash_init_16_8 (&new_hash, "ip4-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001282 new_nbuckets * 1024);
Klement Sekera8dcfed52018-06-28 11:16:15 +02001283 clib_bihash_foreach_key_value_pair_16_8 (&ip4_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001284 ip4_rehash_cb, &ctx);
1285 if (ctx.failure)
1286 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001287 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001288 return -1;
1289 }
1290 else
1291 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001292 clib_bihash_free_16_8 (&ip4_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001293 clib_memcpy_fast (&ip4_reass_main.hash, &new_hash,
1294 sizeof (ip4_reass_main.hash));
Dave Barach32dcd3b2019-07-08 12:25:38 -04001295 clib_bihash_copied (&ip4_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001296 }
1297 }
1298 return 0;
1299}
1300
1301vnet_api_error_t
1302ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001303 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001304{
1305 *timeout_ms = ip4_reass_main.timeout_ms;
1306 *max_reassemblies = ip4_reass_main.max_reass_n;
Klement Sekera3a343d42019-05-16 14:35:46 +02001307 *max_reassembly_length = ip4_reass_main.max_reass_len;
Klement Sekera75e7d132017-09-20 08:26:30 +02001308 *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms;
1309 return 0;
1310}
1311
Klement Sekera4c533132018-02-22 11:41:12 +01001312static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001313ip4_reass_init_function (vlib_main_t * vm)
1314{
1315 ip4_reass_main_t *rm = &ip4_reass_main;
1316 clib_error_t *error = 0;
1317 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001318 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001319
1320 rm->vlib_main = vm;
1321 rm->vnet_main = vnet_get_main ();
1322
Juraj Slobodacd806922018-10-10 10:15:54 +02001323 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001324 ip4_reass_per_thread_t *rt;
1325 vec_foreach (rt, rm->per_thread_data)
1326 {
1327 clib_spinlock_init (&rt->lock);
1328 pool_alloc (rt->pool, rm->max_reass_n);
1329 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001330
1331 node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk");
1332 ASSERT (node);
1333 rm->ip4_reass_expire_node_idx = node->index;
1334
Klement Sekera3ecc2212018-03-27 10:34:43 +02001335 ip4_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1336 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
Klement Sekera3a343d42019-05-16 14:35:46 +02001337 IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
Klement Sekera3ecc2212018-03-27 10:34:43 +02001338 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1339
Klement Sekera75e7d132017-09-20 08:26:30 +02001340 nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera8dcfed52018-06-28 11:16:15 +02001341 clib_bihash_init_16_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001342
Dave Barach1403fcd2018-02-05 09:45:43 -05001343 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001344 ASSERT (node);
1345 rm->ip4_drop_idx = node->index;
Klement Sekera4c533132018-02-22 11:41:12 +01001346
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001347 rm->fq_index = vlib_frame_queue_main_init (ip4_reass_node.index, 0);
1348 rm->fq_feature_index =
1349 vlib_frame_queue_main_init (ip4_reass_node_feature.index, 0);
1350
1351
Klement Sekera75e7d132017-09-20 08:26:30 +02001352 return error;
1353}
1354
1355VLIB_INIT_FUNCTION (ip4_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001356#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001357
1358static uword
1359ip4_reass_walk_expired (vlib_main_t * vm,
1360 vlib_node_runtime_t * node, vlib_frame_t * f)
1361{
1362 ip4_reass_main_t *rm = &ip4_reass_main;
1363 uword event_type, *event_data = 0;
1364
1365 while (true)
1366 {
1367 vlib_process_wait_for_event_or_clock (vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001368 (f64)
1369 rm->expire_walk_interval_ms /
1370 (f64) MSEC_PER_SEC);
Klement Sekera75e7d132017-09-20 08:26:30 +02001371 event_type = vlib_process_get_events (vm, &event_data);
1372
1373 switch (event_type)
1374 {
1375 case ~0: /* no events => timeout */
1376 /* nothing to do here */
1377 break;
1378 case IP4_EVENT_CONFIG_CHANGED:
1379 break;
1380 default:
1381 clib_warning ("BUG: event type 0x%wx", event_type);
1382 break;
1383 }
1384 f64 now = vlib_time_now (vm);
1385
1386 ip4_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001387 int *pool_indexes_to_free = NULL;
1388
Klement Sekera4c533132018-02-22 11:41:12 +01001389 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001390 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001391 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001392 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1393 {
1394 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1395 clib_spinlock_lock (&rt->lock);
1396
1397 vec_reset_length (pool_indexes_to_free);
1398 /* *INDENT-OFF* */
1399 pool_foreach_index (index, rt->pool, ({
1400 reass = pool_elt_at_index (rt->pool, index);
1401 if (now > reass->last_heard + rm->timeout)
1402 {
1403 vec_add1 (pool_indexes_to_free, index);
1404 }
1405 }));
1406 /* *INDENT-ON* */
1407 int *i;
1408 /* *INDENT-OFF* */
1409 vec_foreach (i, pool_indexes_to_free)
1410 {
1411 ip4_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekerae8498652019-06-17 12:23:15 +00001412 ip4_reass_drop_all (vm, node, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +01001413 ip4_reass_free (rm, rt, reass);
1414 }
1415 /* *INDENT-ON* */
1416
1417 clib_spinlock_unlock (&rt->lock);
1418 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001419
Klement Sekera75e7d132017-09-20 08:26:30 +02001420 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001421 if (event_data)
1422 {
1423 _vec_len (event_data) = 0;
1424 }
1425 }
1426
1427 return 0;
1428}
1429
Klement Sekera75e7d132017-09-20 08:26:30 +02001430/* *INDENT-OFF* */
1431VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = {
1432 .function = ip4_reass_walk_expired,
1433 .type = VLIB_NODE_TYPE_PROCESS,
1434 .name = "ip4-reassembly-expire-walk",
1435 .format_trace = format_ip4_reass_trace,
1436 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1437 .error_strings = ip4_reassembly_error_strings,
1438
1439};
1440/* *INDENT-ON* */
1441
1442static u8 *
1443format_ip4_reass_key (u8 * s, va_list * args)
1444{
1445 ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *);
1446 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1447 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1448 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1449 return s;
1450}
1451
1452static u8 *
1453format_ip4_reass (u8 * s, va_list * args)
1454{
1455 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1456 ip4_reass_t *reass = va_arg (*args, ip4_reass_t *);
1457
Klement Sekera4c533132018-02-22 11:41:12 +01001458 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001459 "last_packet_octet: %u, trace_op_counter: %u\n",
1460 reass->id, format_ip4_reass_key, &reass->key, reass->first_bi,
1461 reass->data_len, reass->last_packet_octet,
1462 reass->trace_op_counter);
1463 u32 bi = reass->first_bi;
1464 u32 counter = 0;
1465 while (~0 != bi)
1466 {
1467 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1468 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1469 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1470 "fragment[%u, %u]\n",
1471 counter, vnb->ip.reass.range_first,
1472 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001473 ip4_reass_buffer_get_data_offset (b),
1474 ip4_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001475 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1476 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1477 {
1478 bi = b->next_buffer;
1479 }
1480 else
1481 {
1482 bi = ~0;
1483 }
1484 }
1485 return s;
1486}
1487
1488static clib_error_t *
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001489show_ip4_reass (vlib_main_t * vm,
1490 unformat_input_t * input,
Klement Sekera75e7d132017-09-20 08:26:30 +02001491 CLIB_UNUSED (vlib_cli_command_t * lmd))
1492{
1493 ip4_reass_main_t *rm = &ip4_reass_main;
1494
1495 vlib_cli_output (vm, "---------------------");
1496 vlib_cli_output (vm, "IP4 reassembly status");
1497 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001498 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 if (unformat (input, "details"))
1500 {
Klement Sekera4c533132018-02-22 11:41:12 +01001501 details = true;
1502 }
1503
1504 u32 sum_reass_n = 0;
Klement Sekera4c533132018-02-22 11:41:12 +01001505 ip4_reass_t *reass;
1506 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001507 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001508 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1509 {
1510 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1511 clib_spinlock_lock (&rt->lock);
1512 if (details)
1513 {
1514 /* *INDENT-OFF* */
1515 pool_foreach (reass, rt->pool, {
1516 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1517 });
1518 /* *INDENT-ON* */
1519 }
1520 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001521 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001522 }
1523 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001524 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1525 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001526 vlib_cli_output (vm,
Klement Sekera4c533132018-02-22 11:41:12 +01001527 "Maximum configured concurrent IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001528 (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001529 return 0;
1530}
1531
1532/* *INDENT-OFF* */
1533VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = {
1534 .path = "show ip4-reassembly",
1535 .short_help = "show ip4-reassembly [details]",
1536 .function = show_ip4_reass,
1537};
1538/* *INDENT-ON* */
1539
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001540#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001541vnet_api_error_t
1542ip4_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1543{
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001544 return vnet_feature_enable_disable ("ip4-unicast",
1545 "ip4-reassembly-feature", sw_if_index,
1546 enable_disable, 0, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001547}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001548#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001549
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001550
1551#define foreach_ip4_reassembly_handoff_error \
1552_(CONGESTION_DROP, "congestion drop")
1553
1554
1555typedef enum
1556{
1557#define _(sym,str) IP4_REASSEMBLY_HANDOFF_ERROR_##sym,
1558 foreach_ip4_reassembly_handoff_error
1559#undef _
1560 IP4_REASSEMBLY_HANDOFF_N_ERROR,
1561} ip4_reassembly_handoff_error_t;
1562
1563static char *ip4_reassembly_handoff_error_strings[] = {
1564#define _(sym,string) string,
1565 foreach_ip4_reassembly_handoff_error
1566#undef _
1567};
1568
1569typedef struct
1570{
1571 u32 next_worker_index;
1572} ip4_reassembly_handoff_trace_t;
1573
1574static u8 *
1575format_ip4_reassembly_handoff_trace (u8 * s, va_list * args)
1576{
1577 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1578 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1579 ip4_reassembly_handoff_trace_t *t =
1580 va_arg (*args, ip4_reassembly_handoff_trace_t *);
1581
1582 s =
1583 format (s, "ip4-reassembly-handoff: next-worker %d",
1584 t->next_worker_index);
1585
1586 return s;
1587}
1588
1589always_inline uword
1590ip4_reassembly_handoff_node_inline (vlib_main_t * vm,
1591 vlib_node_runtime_t * node,
1592 vlib_frame_t * frame, bool is_feature)
1593{
1594 ip4_reass_main_t *rm = &ip4_reass_main;
1595
1596 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1597 u32 n_enq, n_left_from, *from;
1598 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1599 u32 fq_index;
1600
1601 from = vlib_frame_vector_args (frame);
1602 n_left_from = frame->n_vectors;
1603 vlib_get_buffers (vm, from, bufs, n_left_from);
1604
1605 b = bufs;
1606 ti = thread_indices;
1607
1608 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1609
1610 while (n_left_from > 0)
1611 {
1612 ti[0] =
1613 (is_feature) ? vnet_buffer (b[0])->ip.
1614 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1615 reass.owner_thread_index;
1616
1617 if (PREDICT_FALSE
1618 ((node->flags & VLIB_NODE_FLAG_TRACE)
1619 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1620 {
1621 ip4_reassembly_handoff_trace_t *t =
1622 vlib_add_trace (vm, node, b[0], sizeof (*t));
1623 t->next_worker_index = ti[0];
1624 }
1625
1626 n_left_from -= 1;
1627 ti += 1;
1628 b += 1;
1629 }
1630 n_enq =
1631 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1632 frame->n_vectors, 1);
1633
1634 if (n_enq < frame->n_vectors)
1635 vlib_node_increment_counter (vm, node->node_index,
1636 IP4_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1637 frame->n_vectors - n_enq);
1638 return frame->n_vectors;
1639}
1640
1641VLIB_NODE_FN (ip4_reassembly_handoff_node) (vlib_main_t * vm,
1642 vlib_node_runtime_t * node,
1643 vlib_frame_t * frame)
1644{
1645 return ip4_reassembly_handoff_node_inline (vm, node, frame,
1646 false /* is_feature */ );
1647}
1648
1649
1650/* *INDENT-OFF* */
1651VLIB_REGISTER_NODE (ip4_reassembly_handoff_node) = {
1652 .name = "ip4-reassembly-handoff",
1653 .vector_size = sizeof (u32),
1654 .n_errors = ARRAY_LEN(ip4_reassembly_handoff_error_strings),
1655 .error_strings = ip4_reassembly_handoff_error_strings,
1656 .format_trace = format_ip4_reassembly_handoff_trace,
1657
1658 .n_next_nodes = 1,
1659
1660 .next_nodes = {
1661 [0] = "error-drop",
1662 },
1663};
1664/* *INDENT-ON* */
1665
1666
1667/* *INDENT-OFF* */
1668VLIB_NODE_FN (ip4_reassembly_feature_handoff_node) (vlib_main_t * vm,
1669 vlib_node_runtime_t *
1670 node,
1671 vlib_frame_t * frame)
1672{
1673 return ip4_reassembly_handoff_node_inline (vm, node, frame,
1674 true /* is_feature */ );
1675}
1676/* *INDENT-ON* */
1677
1678
1679/* *INDENT-OFF* */
1680VLIB_REGISTER_NODE (ip4_reassembly_feature_handoff_node) = {
1681 .name = "ip4-reass-feature-hoff",
1682 .vector_size = sizeof (u32),
1683 .n_errors = ARRAY_LEN(ip4_reassembly_handoff_error_strings),
1684 .error_strings = ip4_reassembly_handoff_error_strings,
1685 .format_trace = format_ip4_reassembly_handoff_trace,
1686
1687 .n_next_nodes = 1,
1688
1689 .next_nodes = {
1690 [0] = "error-drop",
1691 },
1692};
1693/* *INDENT-ON* */
1694
Klement Sekera75e7d132017-09-20 08:26:30 +02001695/*
1696 * fd.io coding-style-patch-verification: ON
1697 *
1698 * Local Variables:
1699 * eval: (c-set-style "gnu")
1700 * End:
1701 */