blob: a88ee2f3e8419acc03de6b7e87be8cf452a36c0c [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 Sekera75e7d132017-09-20 08:26:30 +020033#define IP4_REASS_HT_LOAD_FACTOR (0.75)
34
35#define IP4_REASS_DEBUG_BUFFERS 0
36#if IP4_REASS_DEBUG_BUFFERS
37#define IP4_REASS_DEBUG_BUFFER(bi, what) \
38 do \
39 { \
40 u32 _bi = bi; \
41 printf (#what "buffer %u", _bi); \
42 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
43 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \
44 { \
45 _bi = _b->next_buffer; \
46 printf ("[%u]", _bi); \
47 _b = vlib_get_buffer (vm, _bi); \
48 } \
49 printf ("\n"); \
50 fflush (stdout); \
51 } \
52 while (0)
53#else
54#define IP4_REASS_DEBUG_BUFFER(...)
55#endif
56
57static vlib_node_registration_t ip4_reass_node;
58
59typedef struct
60{
61 union
62 {
63 struct
64 {
Klement Sekera75e7d132017-09-20 08:26:30 +020065 u32 xx_id;
66 ip4_address_t src;
67 ip4_address_t dst;
Klement Sekera8dcfed52018-06-28 11:16:15 +020068 u16 frag_id;
69 u8 proto;
70 u8 unused;
Klement Sekera75e7d132017-09-20 08:26:30 +020071 };
Klement Sekera8dcfed52018-06-28 11:16:15 +020072 u64 as_u64[2];
Klement Sekera75e7d132017-09-20 08:26:30 +020073 };
74} ip4_reass_key_t;
75
76always_inline u32
77ip4_reass_buffer_get_data_offset_no_check (vlib_buffer_t * b)
78{
79 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
80 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
81}
82
83always_inline u32
84ip4_reass_buffer_get_data_offset (vlib_buffer_t * b)
85{
86 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
87 ASSERT (vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first);
88 return ip4_reass_buffer_get_data_offset_no_check (b);
89}
90
91always_inline u16
92ip4_reass_buffer_get_data_len_no_check (vlib_buffer_t * b)
93{
94 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
95 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
96 (vnb->ip.reass.fragment_first + ip4_reass_buffer_get_data_offset (b)) + 1;
97}
98
99always_inline u16
100ip4_reass_buffer_get_data_len (vlib_buffer_t * b)
101{
102 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
103 ASSERT (vnb->ip.reass.range_last > vnb->ip.reass.fragment_first);
104 return ip4_reass_buffer_get_data_len_no_check (b);
105}
106
107typedef struct
108{
109 // hash table key
110 ip4_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200111 // time when last packet was received
112 f64 last_heard;
113 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100114 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200115 // buffer index of first buffer in this reassembly context
116 u32 first_bi;
117 // last octet of packet, ~0 until fragment without more_fragments arrives
118 u32 last_packet_octet;
119 // length of data collected so far
120 u32 data_len;
121 // trace operation counter
122 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100123 // next index - used by non-feature node
124 u8 next_index;
125 // minimum fragment length for this reassembly - used to estimate MTU
126 u16 min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200127} ip4_reass_t;
128
129typedef struct
130{
Klement Sekera4c533132018-02-22 11:41:12 +0100131 ip4_reass_t *pool;
132 u32 reass_n;
133 u32 buffers_n;
134 u32 id_counter;
135 clib_spinlock_t lock;
136} ip4_reass_per_thread_t;
137
138typedef struct
139{
Klement Sekera75e7d132017-09-20 08:26:30 +0200140 // IPv4 config
141 u32 timeout_ms;
142 f64 timeout;
143 u32 expire_walk_interval_ms;
144 u32 max_reass_n;
145
146 // IPv4 runtime
Klement Sekera8dcfed52018-06-28 11:16:15 +0200147 clib_bihash_16_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100148 // per-thread data
149 ip4_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200150
151 // convenience
152 vlib_main_t *vlib_main;
153 vnet_main_t *vnet_main;
154
155 // node index of ip4-drop node
156 u32 ip4_drop_idx;
157 u32 ip4_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200158} ip4_reass_main_t;
159
160ip4_reass_main_t ip4_reass_main;
161
162typedef enum
163{
164 IP4_REASSEMBLY_NEXT_INPUT,
165 IP4_REASSEMBLY_NEXT_DROP,
166 IP4_REASSEMBLY_N_NEXT,
167} ip4_reass_next_t;
168
169typedef enum
170{
171 RANGE_NEW,
172 RANGE_SHRINK,
173 RANGE_DISCARD,
174 RANGE_OVERLAP,
175 FINALIZE,
176} ip4_reass_trace_operation_e;
177
178typedef struct
179{
180 u16 range_first;
181 u16 range_last;
182 u32 range_bi;
183 i32 data_offset;
184 u32 data_len;
185 u32 first_bi;
186} ip4_reass_range_trace_t;
187
188typedef struct
189{
190 ip4_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200191 u32 reass_id;
192 ip4_reass_range_trace_t trace_range;
193 u32 size_diff;
194 u32 op_id;
195 u32 fragment_first;
196 u32 fragment_last;
197 u32 total_data_len;
198} ip4_reass_trace_t;
199
Klement Sekera4c533132018-02-22 11:41:12 +0100200static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200201ip4_reass_trace_details (vlib_main_t * vm, u32 bi,
202 ip4_reass_range_trace_t * trace)
203{
204 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
205 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
206 trace->range_first = vnb->ip.reass.range_first;
207 trace->range_last = vnb->ip.reass.range_last;
208 trace->data_offset = ip4_reass_buffer_get_data_offset_no_check (b);
209 trace->data_len = ip4_reass_buffer_get_data_len_no_check (b);
210 trace->range_bi = bi;
211}
212
Klement Sekera4c533132018-02-22 11:41:12 +0100213static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200214format_ip4_reass_range_trace (u8 * s, va_list * args)
215{
216 ip4_reass_range_trace_t *trace = va_arg (*args, ip4_reass_range_trace_t *);
217 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
218 trace->range_last, trace->data_offset, trace->data_len,
219 trace->range_bi);
220 return s;
221}
222
223u8 *
224format_ip4_reass_trace (u8 * s, va_list * args)
225{
226 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
227 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
228 ip4_reass_trace_t *t = va_arg (*args, ip4_reass_trace_t *);
229 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
230 u32 indent = format_get_indent (s);
231 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
232 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
233 t->fragment_last);
234 switch (t->action)
235 {
236 case RANGE_SHRINK:
237 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
238 format_ip4_reass_range_trace, &t->trace_range,
239 t->size_diff);
240 break;
241 case RANGE_DISCARD:
242 s = format (s, "\n%Udiscard %U", format_white_space, indent,
243 format_ip4_reass_range_trace, &t->trace_range);
244 break;
245 case RANGE_NEW:
246 s = format (s, "\n%Unew %U", format_white_space, indent,
247 format_ip4_reass_range_trace, &t->trace_range);
248 break;
249 case RANGE_OVERLAP:
250 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
251 format_ip4_reass_range_trace, &t->trace_range);
252 break;
253 case FINALIZE:
254 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
255 break;
256 }
257 return s;
258}
259
Klement Sekera4c533132018-02-22 11:41:12 +0100260static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200261ip4_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
262 ip4_reass_main_t * rm, ip4_reass_t * reass, u32 bi,
263 ip4_reass_trace_operation_e action, u32 size_diff)
264{
265 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
266 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100267 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
268 {
269 // this buffer's trace is gone
270 b->flags &= ~VLIB_BUFFER_IS_TRACED;
271 return;
272 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200273 ip4_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200274 t->reass_id = reass->id;
275 t->action = action;
276 ip4_reass_trace_details (vm, bi, &t->trace_range);
277 t->size_diff = size_diff;
278 t->op_id = reass->trace_op_counter;
279 ++reass->trace_op_counter;
280 t->fragment_first = vnb->ip.reass.fragment_first;
281 t->fragment_last = vnb->ip.reass.fragment_last;
282 t->trace_range.first_bi = reass->first_bi;
283 t->total_data_len = reass->data_len;
284#if 0
285 static u8 *s = NULL;
286 s = format (s, "%U", format_ip4_reass_trace, NULL, NULL, t);
287 printf ("%.*s\n", vec_len (s), s);
288 fflush (stdout);
289 vec_reset_length (s);
290#endif
291}
292
Klement Sekera4c533132018-02-22 11:41:12 +0100293always_inline void
294ip4_reass_free (ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
295 ip4_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200296{
Klement Sekera8dcfed52018-06-28 11:16:15 +0200297 clib_bihash_kv_16_8_t kv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200298 kv.key[0] = reass->key.as_u64[0];
299 kv.key[1] = reass->key.as_u64[1];
Klement Sekera8dcfed52018-06-28 11:16:15 +0200300 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100301 pool_put (rt->pool, reass);
302 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200303}
304
Klement Sekera4c533132018-02-22 11:41:12 +0100305always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200306ip4_reass_on_timeout (vlib_main_t * vm, ip4_reass_main_t * rm,
307 ip4_reass_t * reass, u32 ** vec_drop_timeout)
308{
309 u32 range_bi = reass->first_bi;
310 vlib_buffer_t *range_b;
311 vnet_buffer_opaque_t *range_vnb;
312 while (~0 != range_bi)
313 {
314 range_b = vlib_get_buffer (vm, range_bi);
315 range_vnb = vnet_buffer (range_b);
316 u32 bi = range_bi;
317 while (~0 != bi)
318 {
319 vec_add1 (*vec_drop_timeout, bi);
320 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
321 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
322 {
323 bi = b->next_buffer;
324 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
325 }
326 else
327 {
328 bi = ~0;
329 }
330 }
331 range_bi = range_vnb->ip.reass.next_range_bi;
332 }
333}
334
335ip4_reass_t *
336ip4_reass_find_or_create (vlib_main_t * vm, ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100337 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200338 ip4_reass_key_t * k, u32 ** vec_drop_timeout)
339{
340 ip4_reass_t *reass = NULL;
341 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera8dcfed52018-06-28 11:16:15 +0200342 clib_bihash_kv_16_8_t kv, value;
Klement Sekera75e7d132017-09-20 08:26:30 +0200343 kv.key[0] = k->as_u64[0];
344 kv.key[1] = k->as_u64[1];
Klement Sekera75e7d132017-09-20 08:26:30 +0200345
Klement Sekera8dcfed52018-06-28 11:16:15 +0200346 if (!clib_bihash_search_16_8 (&rm->hash, &kv, &value))
Klement Sekera75e7d132017-09-20 08:26:30 +0200347 {
Klement Sekera4c533132018-02-22 11:41:12 +0100348 reass = pool_elt_at_index (rt->pool, value.value);
Klement Sekera75e7d132017-09-20 08:26:30 +0200349 if (now > reass->last_heard + rm->timeout)
350 {
351 ip4_reass_on_timeout (vm, rm, reass, vec_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100352 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200353 reass = NULL;
354 }
355 }
356
357 if (reass)
358 {
359 reass->last_heard = now;
360 return reass;
361 }
362
Klement Sekera4c533132018-02-22 11:41:12 +0100363 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200364 {
365 reass = NULL;
366 return reass;
367 }
368 else
369 {
Klement Sekera4c533132018-02-22 11:41:12 +0100370 pool_get (rt->pool, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200371 memset (reass, 0, sizeof (*reass));
Klement Sekera4c533132018-02-22 11:41:12 +0100372 reass->id =
373 ((u64) os_get_thread_index () * 1000000000) + rt->id_counter;
374 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200375 reass->first_bi = ~0;
376 reass->last_packet_octet = ~0;
377 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100378 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200379 }
380
381 reass->key.as_u64[0] = kv.key[0] = k->as_u64[0];
382 reass->key.as_u64[1] = kv.key[1] = k->as_u64[1];
Klement Sekera4c533132018-02-22 11:41:12 +0100383 kv.value = reass - rt->pool;
Klement Sekera75e7d132017-09-20 08:26:30 +0200384 reass->last_heard = now;
385
Klement Sekera8dcfed52018-06-28 11:16:15 +0200386 if (clib_bihash_add_del_16_8 (&rm->hash, &kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200387 {
Klement Sekera4c533132018-02-22 11:41:12 +0100388 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200389 reass = NULL;
390 }
391
392 return reass;
393}
394
Klement Sekera4c533132018-02-22 11:41:12 +0100395always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200396ip4_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100397 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
398 ip4_reass_t * reass, u32 * bi0, u32 * next0,
Damjan Marionaa682a32018-04-26 22:45:40 +0200399 u32 * error0, u32 ** vec_drop_compress,
Klement Sekera4c533132018-02-22 11:41:12 +0100400 u32 ** vec_drop_overlap, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200401{
402 ASSERT (~0 != reass->first_bi);
403 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
404 vlib_buffer_t *last_b = NULL;
405 u32 sub_chain_bi = reass->first_bi;
406 u32 total_length = 0;
407 u32 buf_cnt = 0;
408 u32 dropped_cnt = 0;
409 do
410 {
411 u32 tmp_bi = sub_chain_bi;
412 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
413 ip4_header_t *ip = vlib_buffer_get_current (tmp);
414 u32 data_len = ip4_reass_buffer_get_data_len (tmp);
415 u32 trim_front =
416 ip4_header_bytes (ip) + ip4_reass_buffer_get_data_offset (tmp);
417 u32 trim_end =
418 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
419 if (tmp_bi == reass->first_bi)
420 {
421 /* first buffer - keep ip4 header */
422 ASSERT (0 == ip4_reass_buffer_get_data_offset (tmp));
423 trim_front = 0;
424 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
425 ip4_header_bytes (ip);
Klement Sekera56e6f092018-02-02 16:17:55 +0100426 ASSERT (vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200427 }
428 u32 keep_data =
429 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
430 while (1)
431 {
432 ++buf_cnt;
433 if (trim_front)
434 {
435 if (trim_front > tmp->current_length)
436 {
437 /* drop whole buffer */
438 vec_add1 (*vec_drop_compress, tmp_bi);
439 ++dropped_cnt;
440 trim_front -= tmp->current_length;
441 ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
442 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
443 tmp_bi = tmp->next_buffer;
444 tmp = vlib_get_buffer (vm, tmp_bi);
445 continue;
446 }
447 else
448 {
449 vlib_buffer_advance (tmp, trim_front);
450 trim_front = 0;
451 }
452 }
453 if (keep_data)
454 {
455 if (last_b)
456 {
457 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
458 last_b->next_buffer = tmp_bi;
459 }
460 last_b = tmp;
461 if (keep_data <= tmp->current_length)
462 {
463 tmp->current_length = keep_data;
464 keep_data = 0;
465 }
466 else
467 {
468 keep_data -= tmp->current_length;
469 ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
470 }
471 total_length += tmp->current_length;
472 }
473 else
474 {
475 vec_add1 (*vec_drop_overlap, tmp_bi);
476 ASSERT (reass->first_bi != tmp_bi);
477 ++dropped_cnt;
478 }
479 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
480 {
481 tmp_bi = tmp->next_buffer;
482 tmp = vlib_get_buffer (vm, tmp->next_buffer);
483 }
484 else
485 {
486 break;
487 }
488 }
489 sub_chain_bi =
490 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
491 reass.next_range_bi;
492 }
493 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700494
495 ASSERT (last_b != NULL);
Klement Sekera75e7d132017-09-20 08:26:30 +0200496 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekera4c533132018-02-22 11:41:12 +0100497 ASSERT (rt->buffers_n >= (buf_cnt - dropped_cnt));
498 rt->buffers_n -= buf_cnt - dropped_cnt;
Klement Sekera75e7d132017-09-20 08:26:30 +0200499 ASSERT (total_length >= first_b->current_length);
500 total_length -= first_b->current_length;
501 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
502 first_b->total_length_not_including_first_buffer = total_length;
503 ip4_header_t *ip = vlib_buffer_get_current (first_b);
504 ip->flags_and_fragment_offset = 0;
505 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
506 ip->checksum = ip4_header_checksum (ip);
507 vlib_buffer_chain_compress (vm, first_b, vec_drop_compress);
508 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
509 {
510 ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
511#if 0
512 // following code does a hexdump of packet fragments to stdout ...
513 do
514 {
515 u32 bi = reass->first_bi;
516 u8 *s = NULL;
517 while (~0 != bi)
518 {
519 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
520 s = format (s, "%u: %U\n", bi, format_hexdump,
521 vlib_buffer_get_current (b), b->current_length);
522 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
523 {
524 bi = b->next_buffer;
525 }
526 else
527 {
528 break;
529 }
530 }
531 printf ("%.*s\n", vec_len (s), s);
532 fflush (stdout);
533 vec_free (s);
534 }
535 while (0);
536#endif
537 }
538 *bi0 = reass->first_bi;
Klement Sekera4c533132018-02-22 11:41:12 +0100539 if (is_feature)
540 {
541 *next0 = IP4_REASSEMBLY_NEXT_INPUT;
542 }
543 else
544 {
545 *next0 = reass->next_index;
546 }
547 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200548 *error0 = IP4_ERROR_NONE;
Klement Sekera4c533132018-02-22 11:41:12 +0100549 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200550 reass = NULL;
551}
552
Klement Sekera4c533132018-02-22 11:41:12 +0100553always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +0200554ip4_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b)
555{
556 u32 len = 0;
557 while (b)
558 {
559 ++len;
560 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
561 {
562 b = vlib_get_buffer (vm, b->next_buffer);
563 }
564 else
565 {
566 break;
567 }
568 }
569 return len;
570}
571
Klement Sekera4c533132018-02-22 11:41:12 +0100572always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200573ip4_reass_insert_range_in_chain (vlib_main_t * vm,
574 ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100575 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200576 ip4_reass_t * reass,
577 u32 prev_range_bi, u32 new_next_bi)
578{
579
580 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
581 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
582 if (~0 != prev_range_bi)
583 {
584 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
585 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
586 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
587 prev_vnb->ip.reass.next_range_bi = new_next_bi;
588 }
589 else
590 {
591 if (~0 != reass->first_bi)
592 {
593 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
594 }
595 reass->first_bi = new_next_bi;
596 }
597 reass->data_len += ip4_reass_buffer_get_data_len (new_next_b);
Klement Sekera4c533132018-02-22 11:41:12 +0100598 rt->buffers_n += ip4_reass_get_buffer_chain_length (vm, new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200599}
600
Klement Sekera4c533132018-02-22 11:41:12 +0100601always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200602ip4_reass_remove_range_from_chain (vlib_main_t * vm,
603 vlib_node_runtime_t * node,
604 ip4_reass_main_t * rm,
605 u32 ** vec_drop_overlap,
606 ip4_reass_t * reass, u32 prev_range_bi,
607 u32 discard_bi)
608{
609 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
610 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
611 if (~0 != prev_range_bi)
612 {
613 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
614 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
615 ASSERT (prev_vnb->ip.reass.next_range_bi == discard_bi);
616 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
617 }
618 else
619 {
620 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
621 }
622 reass->data_len -= ip4_reass_buffer_get_data_len (discard_b);
623 while (1)
624 {
625 vec_add1 (*vec_drop_overlap, discard_bi);
626 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
627 {
628 ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD,
629 0);
630 }
631 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
632 {
633 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
634 discard_bi = discard_b->next_buffer;
635 discard_b = vlib_get_buffer (vm, discard_bi);
636 }
637 else
638 {
639 break;
640 }
641 }
642}
643
Klement Sekera4c533132018-02-22 11:41:12 +0100644always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200645ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100646 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
647 ip4_reass_t * reass, u32 * bi0, u32 * next0,
Damjan Marionaa682a32018-04-26 22:45:40 +0200648 u32 * error0, u32 ** vec_drop_overlap,
Klement Sekera4c533132018-02-22 11:41:12 +0100649 u32 ** vec_drop_compress, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200650{
651 int consumed = 0;
652 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
653 ip4_header_t *fip = vlib_buffer_get_current (fb);
654 ASSERT (fb->current_length >= sizeof (*fip));
655 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700656 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200657 u32 fragment_first = fvnb->ip.reass.fragment_first =
658 ip4_get_fragment_offset_bytes (fip);
659 u32 fragment_length =
660 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
661 u32 fragment_last = fvnb->ip.reass.fragment_last =
662 fragment_first + fragment_length - 1;
663 int more_fragments = ip4_get_fragment_more (fip);
664 u32 candidate_range_bi = reass->first_bi;
665 u32 prev_range_bi = ~0;
666 fvnb->ip.reass.range_first = fragment_first;
667 fvnb->ip.reass.range_last = fragment_last;
668 fvnb->ip.reass.next_range_bi = ~0;
669 if (!more_fragments)
670 {
671 reass->last_packet_octet = fragment_last;
672 }
673 if (~0 == reass->first_bi)
674 {
675 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100676 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
677 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200678 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
679 {
680 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
681 }
682 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100683 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekera75e7d132017-09-20 08:26:30 +0200684 return;
685 }
Klement Sekera4c533132018-02-22 11:41:12 +0100686 reass->min_fragment_length = clib_min (clib_net_to_host_u16 (fip->length),
687 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200688 while (~0 != candidate_range_bi)
689 {
690 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
691 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
692 if (fragment_first > candidate_vnb->ip.reass.range_last)
693 {
694 // this fragments starts after candidate range
695 prev_range_bi = candidate_range_bi;
696 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
697 if (candidate_vnb->ip.reass.range_last < fragment_last &&
698 ~0 == candidate_range_bi)
699 {
700 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100701 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
702 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200703 consumed = 1;
704 break;
705 }
706 continue;
707 }
708 if (fragment_last < candidate_vnb->ip.reass.range_first)
709 {
710 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100711 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200712 *bi0);
713 consumed = 1;
714 }
715 else
716 {
717 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
718 fragment_last <= candidate_vnb->ip.reass.range_last)
719 {
720 // this fragment is a (sub)part of existing range, ignore it
721 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
722 {
723 ip4_reass_add_trace (vm, node, rm, reass, *bi0,
724 RANGE_OVERLAP, 0);
725 }
726 break;
727 }
728 int discard_candidate = 0;
729 if (fragment_first < candidate_vnb->ip.reass.range_first)
730 {
731 u32 overlap =
732 fragment_last - candidate_vnb->ip.reass.range_first + 1;
733 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
734 {
735 candidate_vnb->ip.reass.range_first += overlap;
736 ASSERT (reass->data_len >= overlap);
737 reass->data_len -= overlap;
738 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
739 {
740 ip4_reass_add_trace (vm, node, rm, reass,
741 candidate_range_bi, RANGE_SHRINK,
742 overlap);
743 }
Klement Sekera4c533132018-02-22 11:41:12 +0100744 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200745 prev_range_bi, *bi0);
746 consumed = 1;
747 }
748 else
749 {
750 discard_candidate = 1;
751 }
752 }
753 else if (fragment_last > candidate_vnb->ip.reass.range_last)
754 {
755 u32 overlap =
756 candidate_vnb->ip.reass.range_last - fragment_first + 1;
757 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
758 {
759 fvnb->ip.reass.range_first += overlap;
760 if (~0 != candidate_vnb->ip.reass.next_range_bi)
761 {
762 prev_range_bi = candidate_range_bi;
763 candidate_range_bi =
764 candidate_vnb->ip.reass.next_range_bi;
765 continue;
766 }
767 else
768 {
769 // special case - last range discarded
Klement Sekera4c533132018-02-22 11:41:12 +0100770 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200771 candidate_range_bi,
772 *bi0);
773 consumed = 1;
774 }
775 }
776 else
777 {
778 discard_candidate = 1;
779 }
780 }
781 else
782 {
783 discard_candidate = 1;
784 }
785 if (discard_candidate)
786 {
787 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
788 // discard candidate range, probe next range
789 ip4_reass_remove_range_from_chain (vm, node, rm,
790 vec_drop_overlap, reass,
791 prev_range_bi,
792 candidate_range_bi);
793 if (~0 != next_range_bi)
794 {
795 candidate_range_bi = next_range_bi;
796 continue;
797 }
798 else
799 {
800 // special case - last range discarded
Klement Sekera4c533132018-02-22 11:41:12 +0100801 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200802 prev_range_bi, *bi0);
803 consumed = 1;
804 }
805 }
806 }
807 break;
808 }
809 if (consumed)
810 {
811 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
812 {
813 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
814 }
815 }
816 if (~0 != reass->last_packet_octet &&
817 reass->data_len == reass->last_packet_octet + 1)
818 {
Klement Sekera4c533132018-02-22 11:41:12 +0100819 ip4_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
820 vec_drop_compress, vec_drop_overlap, is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200821 }
822 else
823 {
824 if (consumed)
825 {
826 *bi0 = ~0;
827 }
828 else
829 {
Klement Sekera4c533132018-02-22 11:41:12 +0100830 *next0 = IP4_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200831 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
832 }
833 }
834}
835
836always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100837ip4_reassembly_inline (vlib_main_t * vm,
838 vlib_node_runtime_t * node,
839 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200840{
841 u32 *from = vlib_frame_vector_args (frame);
842 u32 n_left_from, n_left_to_next, *to_next, next_index;
843 ip4_reass_main_t *rm = &ip4_reass_main;
Klement Sekera4c533132018-02-22 11:41:12 +0100844 ip4_reass_per_thread_t *rt = &rm->per_thread_data[os_get_thread_index ()];
845 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200846
847 n_left_from = frame->n_vectors;
848 next_index = node->cached_next_index;
849 static u32 *vec_drop_timeout = NULL; // indexes of buffers which timed out
850 static u32 *vec_drop_overlap = NULL; // indexes of buffers which were discarded due to overlap
851 static u32 *vec_drop_compress = NULL; // indexes of buffers dicarded due to buffer compression
852 while (n_left_from > 0 || vec_len (vec_drop_timeout) > 0 ||
Klement Sekera4c533132018-02-22 11:41:12 +0100853 vec_len (vec_drop_overlap) > 0 || vec_len (vec_drop_compress) > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200854 {
855 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
856
857 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
858 {
859 u32 bi = vec_pop (vec_drop_timeout);
860 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
861 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
862 to_next[0] = bi;
863 to_next += 1;
864 n_left_to_next -= 1;
865 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
866 n_left_to_next, bi,
867 IP4_REASSEMBLY_NEXT_DROP);
868 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100869 ASSERT (rt->buffers_n > 0);
870 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200871 }
872
873 while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0)
874 {
875 u32 bi = vec_pop (vec_drop_overlap);
876 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
877 b->error = node->errors[IP4_ERROR_REASS_DUPLICATE_FRAGMENT];
878 to_next[0] = bi;
879 to_next += 1;
880 n_left_to_next -= 1;
881 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
882 n_left_to_next, bi,
883 IP4_REASSEMBLY_NEXT_DROP);
884 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_duplicate_fragment);
Klement Sekera4c533132018-02-22 11:41:12 +0100885 ASSERT (rt->buffers_n > 0);
886 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200887 }
888
889 while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0)
890 {
891 u32 bi = vec_pop (vec_drop_compress);
892 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
893 b->error = node->errors[IP4_ERROR_NONE];
894 to_next[0] = bi;
895 to_next += 1;
896 n_left_to_next -= 1;
897 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
898 n_left_to_next, bi,
899 IP4_REASSEMBLY_NEXT_DROP);
900 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_compress);
Klement Sekera4c533132018-02-22 11:41:12 +0100901 ASSERT (rt->buffers_n > 0);
902 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200903 }
904
905 while (n_left_from > 0 && n_left_to_next > 0)
906 {
907 u32 bi0;
908 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +0100909 u32 next0;
910 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +0200911
912 bi0 = from[0];
913 b0 = vlib_get_buffer (vm, bi0);
914
915 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +0100916 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +0200917 {
Klement Sekera4c533132018-02-22 11:41:12 +0100918 // this is a whole packet - no fragmentation
919 if (is_feature)
920 {
921 next0 = IP4_REASSEMBLY_NEXT_INPUT;
922 }
923 else
924 {
925 next0 = vnet_buffer (b0)->ip.reass.next_index;
926 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200927 }
928 else
929 {
Klement Sekera4c533132018-02-22 11:41:12 +0100930 ip4_reass_key_t k;
Klement Sekera8dcfed52018-06-28 11:16:15 +0200931 k.as_u64[0] =
932 (u64) vnet_buffer (b0)->sw_if_index[VLIB_RX] << 32 | (u64)
933 ip0->src_address.as_u32;
934 k.as_u64[1] =
935 (u64) ip0->dst_address.
936 as_u32 << 32 | (u64) ip0->fragment_id << 16 | (u64) ip0->
937 protocol << 8;
938
Klement Sekera4c533132018-02-22 11:41:12 +0100939 ip4_reass_t *reass =
940 ip4_reass_find_or_create (vm, rm, rt, &k, &vec_drop_timeout);
Klement Sekera75e7d132017-09-20 08:26:30 +0200941
Klement Sekera4c533132018-02-22 11:41:12 +0100942 if (reass)
943 {
944 ip4_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
945 &error0, &vec_drop_overlap,
946 &vec_drop_compress, is_feature);
947 }
948 else
949 {
950 next0 = IP4_REASSEMBLY_NEXT_DROP;
951 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
952 }
953
954 b0->error = node->errors[error0];
955 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200956
957 if (bi0 != ~0)
958 {
959 to_next[0] = bi0;
960 to_next += 1;
961 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +0100962 if (is_feature && IP4_ERROR_NONE == error0)
963 {
Damjan Marion7d98a122018-07-19 20:42:08 +0200964 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +0100965 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200966 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
967 n_left_to_next, bi0, next0);
968 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
969 }
970
971 from += 1;
972 n_left_from -= 1;
973 }
974
975 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
976 }
977
Klement Sekera4c533132018-02-22 11:41:12 +0100978 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200979 return frame->n_vectors;
980}
981
982static char *ip4_reassembly_error_strings[] = {
983#define _(sym, string) string,
984 foreach_ip4_error
985#undef _
986};
987
Damjan Marion2574d9f2018-06-26 14:45:49 +0200988static uword
Klement Sekera4c533132018-02-22 11:41:12 +0100989ip4_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node,
990 vlib_frame_t * frame)
991{
992 return ip4_reassembly_inline (vm, node, frame, false /* is_feature */ );
993}
994
Klement Sekera75e7d132017-09-20 08:26:30 +0200995/* *INDENT-OFF* */
996VLIB_REGISTER_NODE (ip4_reass_node, static) = {
997 .function = ip4_reassembly,
998 .name = "ip4-reassembly",
999 .vector_size = sizeof (u32),
1000 .format_trace = format_ip4_reass_trace,
1001 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1002 .error_strings = ip4_reassembly_error_strings,
1003 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1004 .next_nodes =
1005 {
1006 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1007 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1008 },
1009};
1010/* *INDENT-ON* */
1011
Klement Sekera4c533132018-02-22 11:41:12 +01001012VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node, ip4_reassembly);
1013
Damjan Marion2574d9f2018-06-26 14:45:49 +02001014static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001015ip4_reassembly_feature (vlib_main_t * vm,
1016 vlib_node_runtime_t * node, vlib_frame_t * frame)
1017{
1018 return ip4_reassembly_inline (vm, node, frame, true /* is_feature */ );
1019}
1020
1021/* *INDENT-OFF* */
1022VLIB_REGISTER_NODE (ip4_reass_node_feature, static) = {
1023 .function = ip4_reassembly_feature,
1024 .name = "ip4-reassembly-feature",
1025 .vector_size = sizeof (u32),
1026 .format_trace = format_ip4_reass_trace,
1027 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1028 .error_strings = ip4_reassembly_error_strings,
1029 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1030 .next_nodes =
1031 {
1032 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1033 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1034 },
1035};
1036/* *INDENT-ON* */
1037
1038VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node_feature, ip4_reassembly_feature);
1039
1040/* *INDENT-OFF* */
1041VNET_FEATURE_INIT (ip4_reassembly_feature, static) = {
1042 .arc_name = "ip4-unicast",
1043 .node_name = "ip4-reassembly-feature",
1044 .runs_before = VNET_FEATURES ("ip4-lookup"),
1045 .runs_after = 0,
1046};
1047/* *INDENT-ON* */
1048
1049always_inline u32
1050ip4_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001051{
1052 ip4_reass_main_t *rm = &ip4_reass_main;
1053 u32 nbuckets;
1054 u8 i;
1055
1056 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1057
1058 for (i = 0; i < 31; i++)
1059 if ((1 << i) >= nbuckets)
1060 break;
1061 nbuckets = 1 << i;
1062
1063 return nbuckets;
1064}
1065
1066typedef enum
1067{
1068 IP4_EVENT_CONFIG_CHANGED = 1,
1069} ip4_reass_event_t;
1070
1071typedef struct
1072{
1073 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001074 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001075} ip4_rehash_cb_ctx;
1076
Klement Sekera4c533132018-02-22 11:41:12 +01001077static void
Klement Sekera8dcfed52018-06-28 11:16:15 +02001078ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001079{
1080 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001081 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001082 {
1083 ctx->failure = 1;
1084 }
1085}
1086
Klement Sekera4c533132018-02-22 11:41:12 +01001087static void
1088ip4_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1089 u32 expire_walk_interval_ms)
1090{
1091 ip4_reass_main.timeout_ms = timeout_ms;
1092 ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1093 ip4_reass_main.max_reass_n = max_reassemblies;
1094 ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1095}
1096
Klement Sekera75e7d132017-09-20 08:26:30 +02001097vnet_api_error_t
1098ip4_reass_set (u32 timeout_ms, u32 max_reassemblies,
1099 u32 expire_walk_interval_ms)
1100{
1101 u32 old_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera4c533132018-02-22 11:41:12 +01001102 ip4_reass_set_params (timeout_ms, max_reassemblies,
1103 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001104 vlib_process_signal_event (ip4_reass_main.vlib_main,
1105 ip4_reass_main.ip4_reass_expire_node_idx,
1106 IP4_EVENT_CONFIG_CHANGED, 0);
1107 u32 new_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001108 if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001109 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001110 clib_bihash_16_8_t new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001111 memset (&new_hash, 0, sizeof (new_hash));
1112 ip4_rehash_cb_ctx ctx;
1113 ctx.failure = 0;
1114 ctx.new_hash = &new_hash;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001115 clib_bihash_init_16_8 (&new_hash, "ip4-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001116 new_nbuckets * 1024);
Klement Sekera8dcfed52018-06-28 11:16:15 +02001117 clib_bihash_foreach_key_value_pair_16_8 (&ip4_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001118 ip4_rehash_cb, &ctx);
1119 if (ctx.failure)
1120 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001121 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001122 return -1;
1123 }
1124 else
1125 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001126 clib_bihash_free_16_8 (&ip4_reass_main.hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001127 clib_memcpy (&ip4_reass_main.hash, &new_hash,
1128 sizeof (ip4_reass_main.hash));
1129 }
1130 }
1131 return 0;
1132}
1133
1134vnet_api_error_t
1135ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1136 u32 * expire_walk_interval_ms)
1137{
1138 *timeout_ms = ip4_reass_main.timeout_ms;
1139 *max_reassemblies = ip4_reass_main.max_reass_n;
1140 *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms;
1141 return 0;
1142}
1143
Klement Sekera4c533132018-02-22 11:41:12 +01001144static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001145ip4_reass_init_function (vlib_main_t * vm)
1146{
1147 ip4_reass_main_t *rm = &ip4_reass_main;
1148 clib_error_t *error = 0;
1149 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001150 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001151
1152 rm->vlib_main = vm;
1153 rm->vnet_main = vnet_get_main ();
1154
Klement Sekera4c533132018-02-22 11:41:12 +01001155 vec_validate (rm->per_thread_data, vlib_num_workers () + 1);
1156 ip4_reass_per_thread_t *rt;
1157 vec_foreach (rt, rm->per_thread_data)
1158 {
1159 clib_spinlock_init (&rt->lock);
1160 pool_alloc (rt->pool, rm->max_reass_n);
1161 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001162
1163 node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk");
1164 ASSERT (node);
1165 rm->ip4_reass_expire_node_idx = node->index;
1166
Klement Sekera3ecc2212018-03-27 10:34:43 +02001167 ip4_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1168 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1169 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1170
Klement Sekera75e7d132017-09-20 08:26:30 +02001171 nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera8dcfed52018-06-28 11:16:15 +02001172 clib_bihash_init_16_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001173
Dave Barach1403fcd2018-02-05 09:45:43 -05001174 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001175 ASSERT (node);
1176 rm->ip4_drop_idx = node->index;
Klement Sekera4c533132018-02-22 11:41:12 +01001177
Klement Sekera75e7d132017-09-20 08:26:30 +02001178 return error;
1179}
1180
1181VLIB_INIT_FUNCTION (ip4_reass_init_function);
1182
1183static uword
1184ip4_reass_walk_expired (vlib_main_t * vm,
1185 vlib_node_runtime_t * node, vlib_frame_t * f)
1186{
1187 ip4_reass_main_t *rm = &ip4_reass_main;
1188 uword event_type, *event_data = 0;
1189
1190 while (true)
1191 {
1192 vlib_process_wait_for_event_or_clock (vm,
1193 (f64) rm->expire_walk_interval_ms
1194 / (f64) MSEC_PER_SEC);
1195 event_type = vlib_process_get_events (vm, &event_data);
1196
1197 switch (event_type)
1198 {
1199 case ~0: /* no events => timeout */
1200 /* nothing to do here */
1201 break;
1202 case IP4_EVENT_CONFIG_CHANGED:
1203 break;
1204 default:
1205 clib_warning ("BUG: event type 0x%wx", event_type);
1206 break;
1207 }
1208 f64 now = vlib_time_now (vm);
1209
1210 ip4_reass_t *reass;
1211 u32 *vec_drop_timeout = NULL;
1212 int *pool_indexes_to_free = NULL;
1213
Klement Sekera4c533132018-02-22 11:41:12 +01001214 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001215 int index;
Klement Sekera4c533132018-02-22 11:41:12 +01001216 const uword nthreads = os_get_nthreads ();
1217 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1218 {
1219 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1220 clib_spinlock_lock (&rt->lock);
1221
1222 vec_reset_length (pool_indexes_to_free);
1223 /* *INDENT-OFF* */
1224 pool_foreach_index (index, rt->pool, ({
1225 reass = pool_elt_at_index (rt->pool, index);
1226 if (now > reass->last_heard + rm->timeout)
1227 {
1228 vec_add1 (pool_indexes_to_free, index);
1229 }
1230 }));
1231 /* *INDENT-ON* */
1232 int *i;
1233 /* *INDENT-OFF* */
1234 vec_foreach (i, pool_indexes_to_free)
1235 {
1236 ip4_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1237 u32 before = vec_len (vec_drop_timeout);
1238 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1239 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1240 {
1241 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1242 b->trace_index))
1243 {
1244 /* the trace is gone, don't trace this buffer anymore */
1245 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1246 }
1247 }
1248 ip4_reass_on_timeout (vm, rm, reass, &vec_drop_timeout);
1249 u32 after = vec_len (vec_drop_timeout);
1250 ASSERT (rt->buffers_n >= (after - before));
1251 rt->buffers_n -= (after - before);
1252 ip4_reass_free (rm, rt, reass);
1253 }
1254 /* *INDENT-ON* */
1255
1256 clib_spinlock_unlock (&rt->lock);
1257 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001258
1259 while (vec_len (vec_drop_timeout) > 0)
1260 {
1261 vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip4_drop_idx);
1262 u32 *to_next = vlib_frame_vector_args (f);
1263 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001264 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001265 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
1266 {
1267 u32 bi = vec_pop (vec_drop_timeout);
1268 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1269 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1270 {
1271 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1272 b->trace_index))
1273 {
1274 /* the trace is gone, don't trace this buffer anymore */
1275 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1276 }
1277 else
1278 {
Klement Sekera4c533132018-02-22 11:41:12 +01001279 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001280 }
1281 }
1282 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
1283 to_next[0] = bi;
1284 ++f->n_vectors;
1285 to_next += 1;
1286 n_left_to_next -= 1;
1287 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout_walk);
Klement Sekera75e7d132017-09-20 08:26:30 +02001288 }
Klement Sekera4c533132018-02-22 11:41:12 +01001289 f->flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001290 vlib_put_frame_to_node (vm, rm->ip4_drop_idx, f);
1291 }
1292
1293 vec_free (pool_indexes_to_free);
1294 vec_free (vec_drop_timeout);
1295 if (event_data)
1296 {
1297 _vec_len (event_data) = 0;
1298 }
1299 }
1300
1301 return 0;
1302}
1303
1304static vlib_node_registration_t ip4_reass_expire_node;
1305
1306/* *INDENT-OFF* */
1307VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = {
1308 .function = ip4_reass_walk_expired,
1309 .type = VLIB_NODE_TYPE_PROCESS,
1310 .name = "ip4-reassembly-expire-walk",
1311 .format_trace = format_ip4_reass_trace,
1312 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1313 .error_strings = ip4_reassembly_error_strings,
1314
1315};
1316/* *INDENT-ON* */
1317
1318static u8 *
1319format_ip4_reass_key (u8 * s, va_list * args)
1320{
1321 ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *);
1322 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1323 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1324 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1325 return s;
1326}
1327
1328static u8 *
1329format_ip4_reass (u8 * s, va_list * args)
1330{
1331 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1332 ip4_reass_t *reass = va_arg (*args, ip4_reass_t *);
1333
Klement Sekera4c533132018-02-22 11:41:12 +01001334 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001335 "last_packet_octet: %u, trace_op_counter: %u\n",
1336 reass->id, format_ip4_reass_key, &reass->key, reass->first_bi,
1337 reass->data_len, reass->last_packet_octet,
1338 reass->trace_op_counter);
1339 u32 bi = reass->first_bi;
1340 u32 counter = 0;
1341 while (~0 != bi)
1342 {
1343 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1344 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1345 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1346 "fragment[%u, %u]\n",
1347 counter, vnb->ip.reass.range_first,
1348 vnb->ip.reass.range_last, bi,
1349 ip4_reass_buffer_get_data_offset_no_check (b),
1350 ip4_reass_buffer_get_data_len_no_check (b),
1351 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1352 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1353 {
1354 bi = b->next_buffer;
1355 }
1356 else
1357 {
1358 bi = ~0;
1359 }
1360 }
1361 return s;
1362}
1363
1364static clib_error_t *
1365show_ip4_reass (vlib_main_t * vm, unformat_input_t * input,
1366 CLIB_UNUSED (vlib_cli_command_t * lmd))
1367{
1368 ip4_reass_main_t *rm = &ip4_reass_main;
1369
1370 vlib_cli_output (vm, "---------------------");
1371 vlib_cli_output (vm, "IP4 reassembly status");
1372 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001373 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001374 if (unformat (input, "details"))
1375 {
Klement Sekera4c533132018-02-22 11:41:12 +01001376 details = true;
1377 }
1378
1379 u32 sum_reass_n = 0;
1380 u64 sum_buffers_n = 0;
1381 ip4_reass_t *reass;
1382 uword thread_index;
1383 const uword nthreads = os_get_nthreads ();
1384 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1385 {
1386 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1387 clib_spinlock_lock (&rt->lock);
1388 if (details)
1389 {
1390 /* *INDENT-OFF* */
1391 pool_foreach (reass, rt->pool, {
1392 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1393 });
1394 /* *INDENT-ON* */
1395 }
1396 sum_reass_n += rt->reass_n;
1397 sum_buffers_n += rt->buffers_n;
1398 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001399 }
1400 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001401 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1402 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001403 vlib_cli_output (vm,
Klement Sekera4c533132018-02-22 11:41:12 +01001404 "Maximum configured concurrent IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001405 (long unsigned) rm->max_reass_n);
1406 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001407 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001408 return 0;
1409}
1410
1411/* *INDENT-OFF* */
1412VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = {
1413 .path = "show ip4-reassembly",
1414 .short_help = "show ip4-reassembly [details]",
1415 .function = show_ip4_reass,
1416};
1417/* *INDENT-ON* */
1418
Klement Sekera4c533132018-02-22 11:41:12 +01001419vnet_api_error_t
1420ip4_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1421{
1422 return vnet_feature_enable_disable ("ip4-unicast", "ip4-reassembly-feature",
1423 sw_if_index, enable_disable, 0, 0);
1424}
1425
Klement Sekera75e7d132017-09-20 08:26:30 +02001426/*
1427 * fd.io coding-style-patch-verification: ON
1428 *
1429 * Local Variables:
1430 * eval: (c-set-style "gnu")
1431 * End:
1432 */