blob: 114d8e052f8a0a92313058ddb903b6507e151531 [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>
26#include <vppinfra/bihash_24_8.h>
27#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 {
65 // align by making this 4 octets even though its a 2 octets field
66 u32 xx_id;
67 ip4_address_t src;
68 ip4_address_t dst;
69 // align by making this 4 octets even though its a 2 octets field
70 u32 frag_id;
71 // align by making this 4 octets even though its a 1 octet field
72 u32 proto;
73 u32 unused;
74 };
75 u64 as_u64[3];
76 };
77} ip4_reass_key_t;
78
79always_inline u32
80ip4_reass_buffer_get_data_offset_no_check (vlib_buffer_t * b)
81{
82 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
83 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
84}
85
86always_inline u32
87ip4_reass_buffer_get_data_offset (vlib_buffer_t * b)
88{
89 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
90 ASSERT (vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first);
91 return ip4_reass_buffer_get_data_offset_no_check (b);
92}
93
94always_inline u16
95ip4_reass_buffer_get_data_len_no_check (vlib_buffer_t * b)
96{
97 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
98 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
99 (vnb->ip.reass.fragment_first + ip4_reass_buffer_get_data_offset (b)) + 1;
100}
101
102always_inline u16
103ip4_reass_buffer_get_data_len (vlib_buffer_t * b)
104{
105 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
106 ASSERT (vnb->ip.reass.range_last > vnb->ip.reass.fragment_first);
107 return ip4_reass_buffer_get_data_len_no_check (b);
108}
109
110typedef struct
111{
112 // hash table key
113 ip4_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200114 // time when last packet was received
115 f64 last_heard;
116 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100117 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200118 // buffer index of first buffer in this reassembly context
119 u32 first_bi;
120 // last octet of packet, ~0 until fragment without more_fragments arrives
121 u32 last_packet_octet;
122 // length of data collected so far
123 u32 data_len;
124 // trace operation counter
125 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100126 // next index - used by non-feature node
127 u8 next_index;
128 // minimum fragment length for this reassembly - used to estimate MTU
129 u16 min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200130} ip4_reass_t;
131
132typedef struct
133{
Klement Sekera4c533132018-02-22 11:41:12 +0100134 ip4_reass_t *pool;
135 u32 reass_n;
136 u32 buffers_n;
137 u32 id_counter;
138 clib_spinlock_t lock;
139} ip4_reass_per_thread_t;
140
141typedef struct
142{
Klement Sekera75e7d132017-09-20 08:26:30 +0200143 // IPv4 config
144 u32 timeout_ms;
145 f64 timeout;
146 u32 expire_walk_interval_ms;
147 u32 max_reass_n;
148
149 // IPv4 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200150 clib_bihash_24_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100151 // per-thread data
152 ip4_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200153
154 // convenience
155 vlib_main_t *vlib_main;
156 vnet_main_t *vnet_main;
157
158 // node index of ip4-drop node
159 u32 ip4_drop_idx;
160 u32 ip4_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200161} ip4_reass_main_t;
162
163ip4_reass_main_t ip4_reass_main;
164
165typedef enum
166{
167 IP4_REASSEMBLY_NEXT_INPUT,
168 IP4_REASSEMBLY_NEXT_DROP,
169 IP4_REASSEMBLY_N_NEXT,
170} ip4_reass_next_t;
171
172typedef enum
173{
174 RANGE_NEW,
175 RANGE_SHRINK,
176 RANGE_DISCARD,
177 RANGE_OVERLAP,
178 FINALIZE,
179} ip4_reass_trace_operation_e;
180
181typedef struct
182{
183 u16 range_first;
184 u16 range_last;
185 u32 range_bi;
186 i32 data_offset;
187 u32 data_len;
188 u32 first_bi;
189} ip4_reass_range_trace_t;
190
191typedef struct
192{
193 ip4_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200194 u32 reass_id;
195 ip4_reass_range_trace_t trace_range;
196 u32 size_diff;
197 u32 op_id;
198 u32 fragment_first;
199 u32 fragment_last;
200 u32 total_data_len;
201} ip4_reass_trace_t;
202
Klement Sekera4c533132018-02-22 11:41:12 +0100203static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200204ip4_reass_trace_details (vlib_main_t * vm, u32 bi,
205 ip4_reass_range_trace_t * trace)
206{
207 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
208 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
209 trace->range_first = vnb->ip.reass.range_first;
210 trace->range_last = vnb->ip.reass.range_last;
211 trace->data_offset = ip4_reass_buffer_get_data_offset_no_check (b);
212 trace->data_len = ip4_reass_buffer_get_data_len_no_check (b);
213 trace->range_bi = bi;
214}
215
Klement Sekera4c533132018-02-22 11:41:12 +0100216static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200217format_ip4_reass_range_trace (u8 * s, va_list * args)
218{
219 ip4_reass_range_trace_t *trace = va_arg (*args, ip4_reass_range_trace_t *);
220 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
221 trace->range_last, trace->data_offset, trace->data_len,
222 trace->range_bi);
223 return s;
224}
225
226u8 *
227format_ip4_reass_trace (u8 * s, va_list * args)
228{
229 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
230 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
231 ip4_reass_trace_t *t = va_arg (*args, ip4_reass_trace_t *);
232 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
233 u32 indent = format_get_indent (s);
234 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
235 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
236 t->fragment_last);
237 switch (t->action)
238 {
239 case RANGE_SHRINK:
240 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
241 format_ip4_reass_range_trace, &t->trace_range,
242 t->size_diff);
243 break;
244 case RANGE_DISCARD:
245 s = format (s, "\n%Udiscard %U", format_white_space, indent,
246 format_ip4_reass_range_trace, &t->trace_range);
247 break;
248 case RANGE_NEW:
249 s = format (s, "\n%Unew %U", format_white_space, indent,
250 format_ip4_reass_range_trace, &t->trace_range);
251 break;
252 case RANGE_OVERLAP:
253 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
254 format_ip4_reass_range_trace, &t->trace_range);
255 break;
256 case FINALIZE:
257 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
258 break;
259 }
260 return s;
261}
262
Klement Sekera4c533132018-02-22 11:41:12 +0100263static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200264ip4_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
265 ip4_reass_main_t * rm, ip4_reass_t * reass, u32 bi,
266 ip4_reass_trace_operation_e action, u32 size_diff)
267{
268 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
269 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100270 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
271 {
272 // this buffer's trace is gone
273 b->flags &= ~VLIB_BUFFER_IS_TRACED;
274 return;
275 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200276 ip4_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200277 t->reass_id = reass->id;
278 t->action = action;
279 ip4_reass_trace_details (vm, bi, &t->trace_range);
280 t->size_diff = size_diff;
281 t->op_id = reass->trace_op_counter;
282 ++reass->trace_op_counter;
283 t->fragment_first = vnb->ip.reass.fragment_first;
284 t->fragment_last = vnb->ip.reass.fragment_last;
285 t->trace_range.first_bi = reass->first_bi;
286 t->total_data_len = reass->data_len;
287#if 0
288 static u8 *s = NULL;
289 s = format (s, "%U", format_ip4_reass_trace, NULL, NULL, t);
290 printf ("%.*s\n", vec_len (s), s);
291 fflush (stdout);
292 vec_reset_length (s);
293#endif
294}
295
Klement Sekera4c533132018-02-22 11:41:12 +0100296always_inline void
297ip4_reass_free (ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
298 ip4_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200299{
300 clib_bihash_kv_24_8_t kv;
301 kv.key[0] = reass->key.as_u64[0];
302 kv.key[1] = reass->key.as_u64[1];
303 kv.key[2] = reass->key.as_u64[2];
304 clib_bihash_add_del_24_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100305 pool_put (rt->pool, reass);
306 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200307}
308
Klement Sekera4c533132018-02-22 11:41:12 +0100309always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200310ip4_reass_on_timeout (vlib_main_t * vm, ip4_reass_main_t * rm,
311 ip4_reass_t * reass, u32 ** vec_drop_timeout)
312{
313 u32 range_bi = reass->first_bi;
314 vlib_buffer_t *range_b;
315 vnet_buffer_opaque_t *range_vnb;
316 while (~0 != range_bi)
317 {
318 range_b = vlib_get_buffer (vm, range_bi);
319 range_vnb = vnet_buffer (range_b);
320 u32 bi = range_bi;
321 while (~0 != bi)
322 {
323 vec_add1 (*vec_drop_timeout, bi);
324 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
325 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
326 {
327 bi = b->next_buffer;
328 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
329 }
330 else
331 {
332 bi = ~0;
333 }
334 }
335 range_bi = range_vnb->ip.reass.next_range_bi;
336 }
337}
338
339ip4_reass_t *
340ip4_reass_find_or_create (vlib_main_t * vm, ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100341 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200342 ip4_reass_key_t * k, u32 ** vec_drop_timeout)
343{
344 ip4_reass_t *reass = NULL;
345 f64 now = vlib_time_now (rm->vlib_main);
346 clib_bihash_kv_24_8_t kv, value;
347 kv.key[0] = k->as_u64[0];
348 kv.key[1] = k->as_u64[1];
349 kv.key[2] = k->as_u64[2];
350
351 if (!clib_bihash_search_24_8 (&rm->hash, &kv, &value))
352 {
Klement Sekera4c533132018-02-22 11:41:12 +0100353 reass = pool_elt_at_index (rt->pool, value.value);
Klement Sekera75e7d132017-09-20 08:26:30 +0200354 if (now > reass->last_heard + rm->timeout)
355 {
356 ip4_reass_on_timeout (vm, rm, reass, vec_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100357 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200358 reass = NULL;
359 }
360 }
361
362 if (reass)
363 {
364 reass->last_heard = now;
365 return reass;
366 }
367
Klement Sekera4c533132018-02-22 11:41:12 +0100368 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200369 {
370 reass = NULL;
371 return reass;
372 }
373 else
374 {
Klement Sekera4c533132018-02-22 11:41:12 +0100375 pool_get (rt->pool, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200376 memset (reass, 0, sizeof (*reass));
Klement Sekera4c533132018-02-22 11:41:12 +0100377 reass->id =
378 ((u64) os_get_thread_index () * 1000000000) + rt->id_counter;
379 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200380 reass->first_bi = ~0;
381 reass->last_packet_octet = ~0;
382 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100383 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200384 }
385
386 reass->key.as_u64[0] = kv.key[0] = k->as_u64[0];
387 reass->key.as_u64[1] = kv.key[1] = k->as_u64[1];
388 reass->key.as_u64[2] = kv.key[2] = k->as_u64[2];
Klement Sekera4c533132018-02-22 11:41:12 +0100389 kv.value = reass - rt->pool;
Klement Sekera75e7d132017-09-20 08:26:30 +0200390 reass->last_heard = now;
391
392 if (clib_bihash_add_del_24_8 (&rm->hash, &kv, 1))
393 {
Klement Sekera4c533132018-02-22 11:41:12 +0100394 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200395 reass = NULL;
396 }
397
398 return reass;
399}
400
Klement Sekera4c533132018-02-22 11:41:12 +0100401always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200402ip4_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100403 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
404 ip4_reass_t * reass, u32 * bi0, u32 * next0,
405 vlib_error_t * error0, u32 ** vec_drop_compress,
406 u32 ** vec_drop_overlap, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200407{
408 ASSERT (~0 != reass->first_bi);
409 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
410 vlib_buffer_t *last_b = NULL;
411 u32 sub_chain_bi = reass->first_bi;
412 u32 total_length = 0;
413 u32 buf_cnt = 0;
414 u32 dropped_cnt = 0;
415 do
416 {
417 u32 tmp_bi = sub_chain_bi;
418 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
419 ip4_header_t *ip = vlib_buffer_get_current (tmp);
420 u32 data_len = ip4_reass_buffer_get_data_len (tmp);
421 u32 trim_front =
422 ip4_header_bytes (ip) + ip4_reass_buffer_get_data_offset (tmp);
423 u32 trim_end =
424 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
425 if (tmp_bi == reass->first_bi)
426 {
427 /* first buffer - keep ip4 header */
428 ASSERT (0 == ip4_reass_buffer_get_data_offset (tmp));
429 trim_front = 0;
430 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
431 ip4_header_bytes (ip);
Klement Sekera56e6f092018-02-02 16:17:55 +0100432 ASSERT (vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200433 }
434 u32 keep_data =
435 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
436 while (1)
437 {
438 ++buf_cnt;
439 if (trim_front)
440 {
441 if (trim_front > tmp->current_length)
442 {
443 /* drop whole buffer */
444 vec_add1 (*vec_drop_compress, tmp_bi);
445 ++dropped_cnt;
446 trim_front -= tmp->current_length;
447 ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
448 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
449 tmp_bi = tmp->next_buffer;
450 tmp = vlib_get_buffer (vm, tmp_bi);
451 continue;
452 }
453 else
454 {
455 vlib_buffer_advance (tmp, trim_front);
456 trim_front = 0;
457 }
458 }
459 if (keep_data)
460 {
461 if (last_b)
462 {
463 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
464 last_b->next_buffer = tmp_bi;
465 }
466 last_b = tmp;
467 if (keep_data <= tmp->current_length)
468 {
469 tmp->current_length = keep_data;
470 keep_data = 0;
471 }
472 else
473 {
474 keep_data -= tmp->current_length;
475 ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
476 }
477 total_length += tmp->current_length;
478 }
479 else
480 {
481 vec_add1 (*vec_drop_overlap, tmp_bi);
482 ASSERT (reass->first_bi != tmp_bi);
483 ++dropped_cnt;
484 }
485 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
486 {
487 tmp_bi = tmp->next_buffer;
488 tmp = vlib_get_buffer (vm, tmp->next_buffer);
489 }
490 else
491 {
492 break;
493 }
494 }
495 sub_chain_bi =
496 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
497 reass.next_range_bi;
498 }
499 while (~0 != sub_chain_bi);
500 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekera4c533132018-02-22 11:41:12 +0100501 ASSERT (rt->buffers_n >= (buf_cnt - dropped_cnt));
502 rt->buffers_n -= buf_cnt - dropped_cnt;
Klement Sekera75e7d132017-09-20 08:26:30 +0200503 ASSERT (total_length >= first_b->current_length);
504 total_length -= first_b->current_length;
505 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
506 first_b->total_length_not_including_first_buffer = total_length;
507 ip4_header_t *ip = vlib_buffer_get_current (first_b);
508 ip->flags_and_fragment_offset = 0;
509 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
510 ip->checksum = ip4_header_checksum (ip);
511 vlib_buffer_chain_compress (vm, first_b, vec_drop_compress);
512 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
513 {
514 ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
515#if 0
516 // following code does a hexdump of packet fragments to stdout ...
517 do
518 {
519 u32 bi = reass->first_bi;
520 u8 *s = NULL;
521 while (~0 != bi)
522 {
523 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
524 s = format (s, "%u: %U\n", bi, format_hexdump,
525 vlib_buffer_get_current (b), b->current_length);
526 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
527 {
528 bi = b->next_buffer;
529 }
530 else
531 {
532 break;
533 }
534 }
535 printf ("%.*s\n", vec_len (s), s);
536 fflush (stdout);
537 vec_free (s);
538 }
539 while (0);
540#endif
541 }
542 *bi0 = reass->first_bi;
Klement Sekera4c533132018-02-22 11:41:12 +0100543 if (is_feature)
544 {
545 *next0 = IP4_REASSEMBLY_NEXT_INPUT;
546 }
547 else
548 {
549 *next0 = reass->next_index;
550 }
551 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200552 *error0 = IP4_ERROR_NONE;
Klement Sekera4c533132018-02-22 11:41:12 +0100553 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200554 reass = NULL;
555}
556
Klement Sekera4c533132018-02-22 11:41:12 +0100557always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +0200558ip4_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b)
559{
560 u32 len = 0;
561 while (b)
562 {
563 ++len;
564 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
565 {
566 b = vlib_get_buffer (vm, b->next_buffer);
567 }
568 else
569 {
570 break;
571 }
572 }
573 return len;
574}
575
Klement Sekera4c533132018-02-22 11:41:12 +0100576always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200577ip4_reass_insert_range_in_chain (vlib_main_t * vm,
578 ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100579 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200580 ip4_reass_t * reass,
581 u32 prev_range_bi, u32 new_next_bi)
582{
583
584 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
585 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
586 if (~0 != prev_range_bi)
587 {
588 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
589 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
590 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
591 prev_vnb->ip.reass.next_range_bi = new_next_bi;
592 }
593 else
594 {
595 if (~0 != reass->first_bi)
596 {
597 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
598 }
599 reass->first_bi = new_next_bi;
600 }
601 reass->data_len += ip4_reass_buffer_get_data_len (new_next_b);
Klement Sekera4c533132018-02-22 11:41:12 +0100602 rt->buffers_n += ip4_reass_get_buffer_chain_length (vm, new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200603}
604
Klement Sekera4c533132018-02-22 11:41:12 +0100605always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200606ip4_reass_remove_range_from_chain (vlib_main_t * vm,
607 vlib_node_runtime_t * node,
608 ip4_reass_main_t * rm,
609 u32 ** vec_drop_overlap,
610 ip4_reass_t * reass, u32 prev_range_bi,
611 u32 discard_bi)
612{
613 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
614 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
615 if (~0 != prev_range_bi)
616 {
617 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
618 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
619 ASSERT (prev_vnb->ip.reass.next_range_bi == discard_bi);
620 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
621 }
622 else
623 {
624 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
625 }
626 reass->data_len -= ip4_reass_buffer_get_data_len (discard_b);
627 while (1)
628 {
629 vec_add1 (*vec_drop_overlap, discard_bi);
630 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
631 {
632 ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD,
633 0);
634 }
635 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
636 {
637 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
638 discard_bi = discard_b->next_buffer;
639 discard_b = vlib_get_buffer (vm, discard_bi);
640 }
641 else
642 {
643 break;
644 }
645 }
646}
647
Klement Sekera4c533132018-02-22 11:41:12 +0100648always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200649ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100650 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
651 ip4_reass_t * reass, u32 * bi0, u32 * next0,
652 vlib_error_t * error0, u32 ** vec_drop_overlap,
653 u32 ** vec_drop_compress, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200654{
655 int consumed = 0;
656 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
657 ip4_header_t *fip = vlib_buffer_get_current (fb);
658 ASSERT (fb->current_length >= sizeof (*fip));
659 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
660 u32 fragment_first = fvnb->ip.reass.fragment_first =
661 ip4_get_fragment_offset_bytes (fip);
662 u32 fragment_length =
663 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
664 u32 fragment_last = fvnb->ip.reass.fragment_last =
665 fragment_first + fragment_length - 1;
666 int more_fragments = ip4_get_fragment_more (fip);
667 u32 candidate_range_bi = reass->first_bi;
668 u32 prev_range_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100669 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200670 fvnb->ip.reass.range_first = fragment_first;
671 fvnb->ip.reass.range_last = fragment_last;
672 fvnb->ip.reass.next_range_bi = ~0;
673 if (!more_fragments)
674 {
675 reass->last_packet_octet = fragment_last;
676 }
677 if (~0 == reass->first_bi)
678 {
679 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100680 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
681 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200682 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
683 {
684 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
685 }
686 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100687 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekera75e7d132017-09-20 08:26:30 +0200688 return;
689 }
Klement Sekera4c533132018-02-22 11:41:12 +0100690 reass->min_fragment_length = clib_min (clib_net_to_host_u16 (fip->length),
691 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200692 while (~0 != candidate_range_bi)
693 {
694 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
695 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
696 if (fragment_first > candidate_vnb->ip.reass.range_last)
697 {
698 // this fragments starts after candidate range
699 prev_range_bi = candidate_range_bi;
700 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
701 if (candidate_vnb->ip.reass.range_last < fragment_last &&
702 ~0 == candidate_range_bi)
703 {
704 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100705 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
706 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200707 consumed = 1;
708 break;
709 }
710 continue;
711 }
712 if (fragment_last < candidate_vnb->ip.reass.range_first)
713 {
714 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100715 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200716 *bi0);
717 consumed = 1;
718 }
719 else
720 {
721 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
722 fragment_last <= candidate_vnb->ip.reass.range_last)
723 {
724 // this fragment is a (sub)part of existing range, ignore it
725 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
726 {
727 ip4_reass_add_trace (vm, node, rm, reass, *bi0,
728 RANGE_OVERLAP, 0);
729 }
730 break;
731 }
732 int discard_candidate = 0;
733 if (fragment_first < candidate_vnb->ip.reass.range_first)
734 {
735 u32 overlap =
736 fragment_last - candidate_vnb->ip.reass.range_first + 1;
737 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
738 {
739 candidate_vnb->ip.reass.range_first += overlap;
740 ASSERT (reass->data_len >= overlap);
741 reass->data_len -= overlap;
742 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
743 {
744 ip4_reass_add_trace (vm, node, rm, reass,
745 candidate_range_bi, RANGE_SHRINK,
746 overlap);
747 }
Klement Sekera4c533132018-02-22 11:41:12 +0100748 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200749 prev_range_bi, *bi0);
750 consumed = 1;
751 }
752 else
753 {
754 discard_candidate = 1;
755 }
756 }
757 else if (fragment_last > candidate_vnb->ip.reass.range_last)
758 {
759 u32 overlap =
760 candidate_vnb->ip.reass.range_last - fragment_first + 1;
761 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
762 {
763 fvnb->ip.reass.range_first += overlap;
764 if (~0 != candidate_vnb->ip.reass.next_range_bi)
765 {
766 prev_range_bi = candidate_range_bi;
767 candidate_range_bi =
768 candidate_vnb->ip.reass.next_range_bi;
769 continue;
770 }
771 else
772 {
773 // special case - last range discarded
Klement Sekera4c533132018-02-22 11:41:12 +0100774 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200775 candidate_range_bi,
776 *bi0);
777 consumed = 1;
778 }
779 }
780 else
781 {
782 discard_candidate = 1;
783 }
784 }
785 else
786 {
787 discard_candidate = 1;
788 }
789 if (discard_candidate)
790 {
791 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
792 // discard candidate range, probe next range
793 ip4_reass_remove_range_from_chain (vm, node, rm,
794 vec_drop_overlap, reass,
795 prev_range_bi,
796 candidate_range_bi);
797 if (~0 != next_range_bi)
798 {
799 candidate_range_bi = next_range_bi;
800 continue;
801 }
802 else
803 {
804 // special case - last range discarded
Klement Sekera4c533132018-02-22 11:41:12 +0100805 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
Klement Sekera75e7d132017-09-20 08:26:30 +0200806 prev_range_bi, *bi0);
807 consumed = 1;
808 }
809 }
810 }
811 break;
812 }
813 if (consumed)
814 {
815 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
816 {
817 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
818 }
819 }
820 if (~0 != reass->last_packet_octet &&
821 reass->data_len == reass->last_packet_octet + 1)
822 {
Klement Sekera4c533132018-02-22 11:41:12 +0100823 ip4_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
824 vec_drop_compress, vec_drop_overlap, is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200825 }
826 else
827 {
828 if (consumed)
829 {
830 *bi0 = ~0;
831 }
832 else
833 {
Klement Sekera4c533132018-02-22 11:41:12 +0100834 *next0 = IP4_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200835 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
836 }
837 }
838}
839
840always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100841ip4_reassembly_inline (vlib_main_t * vm,
842 vlib_node_runtime_t * node,
843 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200844{
845 u32 *from = vlib_frame_vector_args (frame);
846 u32 n_left_from, n_left_to_next, *to_next, next_index;
847 ip4_reass_main_t *rm = &ip4_reass_main;
Klement Sekera4c533132018-02-22 11:41:12 +0100848 ip4_reass_per_thread_t *rt = &rm->per_thread_data[os_get_thread_index ()];
849 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200850
851 n_left_from = frame->n_vectors;
852 next_index = node->cached_next_index;
853 static u32 *vec_drop_timeout = NULL; // indexes of buffers which timed out
854 static u32 *vec_drop_overlap = NULL; // indexes of buffers which were discarded due to overlap
855 static u32 *vec_drop_compress = NULL; // indexes of buffers dicarded due to buffer compression
856 while (n_left_from > 0 || vec_len (vec_drop_timeout) > 0 ||
Klement Sekera4c533132018-02-22 11:41:12 +0100857 vec_len (vec_drop_overlap) > 0 || vec_len (vec_drop_compress) > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200858 {
859 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
860
861 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
862 {
863 u32 bi = vec_pop (vec_drop_timeout);
864 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
865 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
866 to_next[0] = bi;
867 to_next += 1;
868 n_left_to_next -= 1;
869 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
870 n_left_to_next, bi,
871 IP4_REASSEMBLY_NEXT_DROP);
872 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100873 ASSERT (rt->buffers_n > 0);
874 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200875 }
876
877 while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0)
878 {
879 u32 bi = vec_pop (vec_drop_overlap);
880 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
881 b->error = node->errors[IP4_ERROR_REASS_DUPLICATE_FRAGMENT];
882 to_next[0] = bi;
883 to_next += 1;
884 n_left_to_next -= 1;
885 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
886 n_left_to_next, bi,
887 IP4_REASSEMBLY_NEXT_DROP);
888 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_duplicate_fragment);
Klement Sekera4c533132018-02-22 11:41:12 +0100889 ASSERT (rt->buffers_n > 0);
890 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200891 }
892
893 while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0)
894 {
895 u32 bi = vec_pop (vec_drop_compress);
896 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
897 b->error = node->errors[IP4_ERROR_NONE];
898 to_next[0] = bi;
899 to_next += 1;
900 n_left_to_next -= 1;
901 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
902 n_left_to_next, bi,
903 IP4_REASSEMBLY_NEXT_DROP);
904 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_compress);
Klement Sekera4c533132018-02-22 11:41:12 +0100905 ASSERT (rt->buffers_n > 0);
906 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200907 }
908
909 while (n_left_from > 0 && n_left_to_next > 0)
910 {
911 u32 bi0;
912 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +0100913 u32 next0;
914 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +0200915
916 bi0 = from[0];
917 b0 = vlib_get_buffer (vm, bi0);
918
919 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +0100920 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +0200921 {
Klement Sekera4c533132018-02-22 11:41:12 +0100922 // this is a whole packet - no fragmentation
923 if (is_feature)
924 {
925 next0 = IP4_REASSEMBLY_NEXT_INPUT;
926 }
927 else
928 {
929 next0 = vnet_buffer (b0)->ip.reass.next_index;
930 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200931 }
932 else
933 {
Klement Sekera4c533132018-02-22 11:41:12 +0100934 ip4_reass_key_t k;
935 k.src.as_u32 = ip0->src_address.as_u32;
936 k.dst.as_u32 = ip0->dst_address.as_u32;
937 k.xx_id = vnet_buffer (b0)->sw_if_index[VLIB_RX];
938 k.frag_id = ip0->fragment_id;
939 k.proto = ip0->protocol;
940 k.unused = 0;
941 ip4_reass_t *reass =
942 ip4_reass_find_or_create (vm, rm, rt, &k, &vec_drop_timeout);
Klement Sekera75e7d132017-09-20 08:26:30 +0200943
Klement Sekera4c533132018-02-22 11:41:12 +0100944 if (reass)
945 {
946 ip4_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
947 &error0, &vec_drop_overlap,
948 &vec_drop_compress, is_feature);
949 }
950 else
951 {
952 next0 = IP4_REASSEMBLY_NEXT_DROP;
953 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
954 }
955
956 b0->error = node->errors[error0];
957 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200958
959 if (bi0 != ~0)
960 {
961 to_next[0] = bi0;
962 to_next += 1;
963 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +0100964 if (is_feature && IP4_ERROR_NONE == error0)
965 {
966 vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_RX],
967 &next0, b0);
968 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200969 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
970 n_left_to_next, bi0, next0);
971 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
972 }
973
974 from += 1;
975 n_left_from -= 1;
976 }
977
978 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
979 }
980
Klement Sekera4c533132018-02-22 11:41:12 +0100981 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200982 return frame->n_vectors;
983}
984
985static char *ip4_reassembly_error_strings[] = {
986#define _(sym, string) string,
987 foreach_ip4_error
988#undef _
989};
990
Klement Sekera4c533132018-02-22 11:41:12 +0100991always_inline uword
992ip4_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node,
993 vlib_frame_t * frame)
994{
995 return ip4_reassembly_inline (vm, node, frame, false /* is_feature */ );
996}
997
Klement Sekera75e7d132017-09-20 08:26:30 +0200998/* *INDENT-OFF* */
999VLIB_REGISTER_NODE (ip4_reass_node, static) = {
1000 .function = ip4_reassembly,
1001 .name = "ip4-reassembly",
1002 .vector_size = sizeof (u32),
1003 .format_trace = format_ip4_reass_trace,
1004 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1005 .error_strings = ip4_reassembly_error_strings,
1006 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1007 .next_nodes =
1008 {
1009 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1010 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1011 },
1012};
1013/* *INDENT-ON* */
1014
Klement Sekera4c533132018-02-22 11:41:12 +01001015VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node, ip4_reassembly);
1016
1017always_inline uword
1018ip4_reassembly_feature (vlib_main_t * vm,
1019 vlib_node_runtime_t * node, vlib_frame_t * frame)
1020{
1021 return ip4_reassembly_inline (vm, node, frame, true /* is_feature */ );
1022}
1023
1024/* *INDENT-OFF* */
1025VLIB_REGISTER_NODE (ip4_reass_node_feature, static) = {
1026 .function = ip4_reassembly_feature,
1027 .name = "ip4-reassembly-feature",
1028 .vector_size = sizeof (u32),
1029 .format_trace = format_ip4_reass_trace,
1030 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1031 .error_strings = ip4_reassembly_error_strings,
1032 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1033 .next_nodes =
1034 {
1035 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1036 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1037 },
1038};
1039/* *INDENT-ON* */
1040
1041VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node_feature, ip4_reassembly_feature);
1042
1043/* *INDENT-OFF* */
1044VNET_FEATURE_INIT (ip4_reassembly_feature, static) = {
1045 .arc_name = "ip4-unicast",
1046 .node_name = "ip4-reassembly-feature",
1047 .runs_before = VNET_FEATURES ("ip4-lookup"),
1048 .runs_after = 0,
1049};
1050/* *INDENT-ON* */
1051
1052always_inline u32
1053ip4_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001054{
1055 ip4_reass_main_t *rm = &ip4_reass_main;
1056 u32 nbuckets;
1057 u8 i;
1058
1059 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1060
1061 for (i = 0; i < 31; i++)
1062 if ((1 << i) >= nbuckets)
1063 break;
1064 nbuckets = 1 << i;
1065
1066 return nbuckets;
1067}
1068
1069typedef enum
1070{
1071 IP4_EVENT_CONFIG_CHANGED = 1,
1072} ip4_reass_event_t;
1073
1074typedef struct
1075{
1076 int failure;
1077 clib_bihash_24_8_t *new_hash;
1078} ip4_rehash_cb_ctx;
1079
Klement Sekera4c533132018-02-22 11:41:12 +01001080static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001081ip4_rehash_cb (clib_bihash_kv_24_8_t * kv, void *_ctx)
1082{
1083 ip4_rehash_cb_ctx *ctx = _ctx;
1084 if (clib_bihash_add_del_24_8 (ctx->new_hash, kv, 1))
1085 {
1086 ctx->failure = 1;
1087 }
1088}
1089
Klement Sekera4c533132018-02-22 11:41:12 +01001090static void
1091ip4_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1092 u32 expire_walk_interval_ms)
1093{
1094 ip4_reass_main.timeout_ms = timeout_ms;
1095 ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1096 ip4_reass_main.max_reass_n = max_reassemblies;
1097 ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1098}
1099
Klement Sekera75e7d132017-09-20 08:26:30 +02001100vnet_api_error_t
1101ip4_reass_set (u32 timeout_ms, u32 max_reassemblies,
1102 u32 expire_walk_interval_ms)
1103{
1104 u32 old_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera4c533132018-02-22 11:41:12 +01001105 ip4_reass_set_params (timeout_ms, max_reassemblies,
1106 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001107 vlib_process_signal_event (ip4_reass_main.vlib_main,
1108 ip4_reass_main.ip4_reass_expire_node_idx,
1109 IP4_EVENT_CONFIG_CHANGED, 0);
1110 u32 new_nbuckets = ip4_reass_get_nbuckets ();
1111 if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > 1 &&
1112 new_nbuckets != old_nbuckets)
1113 {
1114 clib_bihash_24_8_t new_hash;
1115 memset (&new_hash, 0, sizeof (new_hash));
1116 ip4_rehash_cb_ctx ctx;
1117 ctx.failure = 0;
1118 ctx.new_hash = &new_hash;
1119 clib_bihash_init_24_8 (&new_hash, "ip4-reass", new_nbuckets,
1120 new_nbuckets * 1024);
1121 clib_bihash_foreach_key_value_pair_24_8 (&ip4_reass_main.hash,
1122 ip4_rehash_cb, &ctx);
1123 if (ctx.failure)
1124 {
1125 clib_bihash_free_24_8 (&new_hash);
1126 return -1;
1127 }
1128 else
1129 {
1130 clib_bihash_free_24_8 (&ip4_reass_main.hash);
1131 clib_memcpy (&ip4_reass_main.hash, &new_hash,
1132 sizeof (ip4_reass_main.hash));
1133 }
1134 }
1135 return 0;
1136}
1137
1138vnet_api_error_t
1139ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1140 u32 * expire_walk_interval_ms)
1141{
1142 *timeout_ms = ip4_reass_main.timeout_ms;
1143 *max_reassemblies = ip4_reass_main.max_reass_n;
1144 *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms;
1145 return 0;
1146}
1147
Klement Sekera4c533132018-02-22 11:41:12 +01001148static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001149ip4_reass_init_function (vlib_main_t * vm)
1150{
1151 ip4_reass_main_t *rm = &ip4_reass_main;
1152 clib_error_t *error = 0;
1153 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001154 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001155
1156 rm->vlib_main = vm;
1157 rm->vnet_main = vnet_get_main ();
1158
Klement Sekera4c533132018-02-22 11:41:12 +01001159 vec_validate (rm->per_thread_data, vlib_num_workers () + 1);
1160 ip4_reass_per_thread_t *rt;
1161 vec_foreach (rt, rm->per_thread_data)
1162 {
1163 clib_spinlock_init (&rt->lock);
1164 pool_alloc (rt->pool, rm->max_reass_n);
1165 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001166
1167 node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk");
1168 ASSERT (node);
1169 rm->ip4_reass_expire_node_idx = node->index;
1170
Klement Sekera75e7d132017-09-20 08:26:30 +02001171 nbuckets = ip4_reass_get_nbuckets ();
1172 clib_bihash_init_24_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024);
1173
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
1178 ip4_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1179 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1180 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1181
Klement Sekera75e7d132017-09-20 08:26:30 +02001182 return error;
1183}
1184
1185VLIB_INIT_FUNCTION (ip4_reass_init_function);
1186
1187static uword
1188ip4_reass_walk_expired (vlib_main_t * vm,
1189 vlib_node_runtime_t * node, vlib_frame_t * f)
1190{
1191 ip4_reass_main_t *rm = &ip4_reass_main;
1192 uword event_type, *event_data = 0;
1193
1194 while (true)
1195 {
1196 vlib_process_wait_for_event_or_clock (vm,
1197 (f64) rm->expire_walk_interval_ms
1198 / (f64) MSEC_PER_SEC);
1199 event_type = vlib_process_get_events (vm, &event_data);
1200
1201 switch (event_type)
1202 {
1203 case ~0: /* no events => timeout */
1204 /* nothing to do here */
1205 break;
1206 case IP4_EVENT_CONFIG_CHANGED:
1207 break;
1208 default:
1209 clib_warning ("BUG: event type 0x%wx", event_type);
1210 break;
1211 }
1212 f64 now = vlib_time_now (vm);
1213
1214 ip4_reass_t *reass;
1215 u32 *vec_drop_timeout = NULL;
1216 int *pool_indexes_to_free = NULL;
1217
Klement Sekera4c533132018-02-22 11:41:12 +01001218 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001219 int index;
Klement Sekera4c533132018-02-22 11:41:12 +01001220 const uword nthreads = os_get_nthreads ();
1221 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1222 {
1223 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1224 clib_spinlock_lock (&rt->lock);
1225
1226 vec_reset_length (pool_indexes_to_free);
1227 /* *INDENT-OFF* */
1228 pool_foreach_index (index, rt->pool, ({
1229 reass = pool_elt_at_index (rt->pool, index);
1230 if (now > reass->last_heard + rm->timeout)
1231 {
1232 vec_add1 (pool_indexes_to_free, index);
1233 }
1234 }));
1235 /* *INDENT-ON* */
1236 int *i;
1237 /* *INDENT-OFF* */
1238 vec_foreach (i, pool_indexes_to_free)
1239 {
1240 ip4_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1241 u32 before = vec_len (vec_drop_timeout);
1242 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1243 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1244 {
1245 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1246 b->trace_index))
1247 {
1248 /* the trace is gone, don't trace this buffer anymore */
1249 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1250 }
1251 }
1252 ip4_reass_on_timeout (vm, rm, reass, &vec_drop_timeout);
1253 u32 after = vec_len (vec_drop_timeout);
1254 ASSERT (rt->buffers_n >= (after - before));
1255 rt->buffers_n -= (after - before);
1256 ip4_reass_free (rm, rt, reass);
1257 }
1258 /* *INDENT-ON* */
1259
1260 clib_spinlock_unlock (&rt->lock);
1261 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001262
1263 while (vec_len (vec_drop_timeout) > 0)
1264 {
1265 vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip4_drop_idx);
1266 u32 *to_next = vlib_frame_vector_args (f);
1267 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001268 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001269 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
1270 {
1271 u32 bi = vec_pop (vec_drop_timeout);
1272 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1273 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1274 {
1275 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1276 b->trace_index))
1277 {
1278 /* the trace is gone, don't trace this buffer anymore */
1279 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1280 }
1281 else
1282 {
Klement Sekera4c533132018-02-22 11:41:12 +01001283 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001284 }
1285 }
1286 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
1287 to_next[0] = bi;
1288 ++f->n_vectors;
1289 to_next += 1;
1290 n_left_to_next -= 1;
1291 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout_walk);
Klement Sekera75e7d132017-09-20 08:26:30 +02001292 }
Klement Sekera4c533132018-02-22 11:41:12 +01001293 f->flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001294 vlib_put_frame_to_node (vm, rm->ip4_drop_idx, f);
1295 }
1296
1297 vec_free (pool_indexes_to_free);
1298 vec_free (vec_drop_timeout);
1299 if (event_data)
1300 {
1301 _vec_len (event_data) = 0;
1302 }
1303 }
1304
1305 return 0;
1306}
1307
1308static vlib_node_registration_t ip4_reass_expire_node;
1309
1310/* *INDENT-OFF* */
1311VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = {
1312 .function = ip4_reass_walk_expired,
1313 .type = VLIB_NODE_TYPE_PROCESS,
1314 .name = "ip4-reassembly-expire-walk",
1315 .format_trace = format_ip4_reass_trace,
1316 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1317 .error_strings = ip4_reassembly_error_strings,
1318
1319};
1320/* *INDENT-ON* */
1321
1322static u8 *
1323format_ip4_reass_key (u8 * s, va_list * args)
1324{
1325 ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *);
1326 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1327 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1328 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1329 return s;
1330}
1331
1332static u8 *
1333format_ip4_reass (u8 * s, va_list * args)
1334{
1335 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1336 ip4_reass_t *reass = va_arg (*args, ip4_reass_t *);
1337
Klement Sekera4c533132018-02-22 11:41:12 +01001338 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001339 "last_packet_octet: %u, trace_op_counter: %u\n",
1340 reass->id, format_ip4_reass_key, &reass->key, reass->first_bi,
1341 reass->data_len, reass->last_packet_octet,
1342 reass->trace_op_counter);
1343 u32 bi = reass->first_bi;
1344 u32 counter = 0;
1345 while (~0 != bi)
1346 {
1347 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1348 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1349 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1350 "fragment[%u, %u]\n",
1351 counter, vnb->ip.reass.range_first,
1352 vnb->ip.reass.range_last, bi,
1353 ip4_reass_buffer_get_data_offset_no_check (b),
1354 ip4_reass_buffer_get_data_len_no_check (b),
1355 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1356 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1357 {
1358 bi = b->next_buffer;
1359 }
1360 else
1361 {
1362 bi = ~0;
1363 }
1364 }
1365 return s;
1366}
1367
1368static clib_error_t *
1369show_ip4_reass (vlib_main_t * vm, unformat_input_t * input,
1370 CLIB_UNUSED (vlib_cli_command_t * lmd))
1371{
1372 ip4_reass_main_t *rm = &ip4_reass_main;
1373
1374 vlib_cli_output (vm, "---------------------");
1375 vlib_cli_output (vm, "IP4 reassembly status");
1376 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001377 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001378 if (unformat (input, "details"))
1379 {
Klement Sekera4c533132018-02-22 11:41:12 +01001380 details = true;
1381 }
1382
1383 u32 sum_reass_n = 0;
1384 u64 sum_buffers_n = 0;
1385 ip4_reass_t *reass;
1386 uword thread_index;
1387 const uword nthreads = os_get_nthreads ();
1388 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1389 {
1390 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1391 clib_spinlock_lock (&rt->lock);
1392 if (details)
1393 {
1394 /* *INDENT-OFF* */
1395 pool_foreach (reass, rt->pool, {
1396 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1397 });
1398 /* *INDENT-ON* */
1399 }
1400 sum_reass_n += rt->reass_n;
1401 sum_buffers_n += rt->buffers_n;
1402 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001403 }
1404 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001405 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1406 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001407 vlib_cli_output (vm,
Klement Sekera4c533132018-02-22 11:41:12 +01001408 "Maximum configured concurrent IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001409 (long unsigned) rm->max_reass_n);
1410 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001411 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001412 return 0;
1413}
1414
1415/* *INDENT-OFF* */
1416VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = {
1417 .path = "show ip4-reassembly",
1418 .short_help = "show ip4-reassembly [details]",
1419 .function = show_ip4_reass,
1420};
1421/* *INDENT-ON* */
1422
Klement Sekera4c533132018-02-22 11:41:12 +01001423vnet_api_error_t
1424ip4_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1425{
1426 return vnet_feature_enable_disable ("ip4-unicast", "ip4-reassembly-feature",
1427 sw_if_index, enable_disable, 0, 0);
1428}
1429
Klement Sekera75e7d132017-09-20 08:26:30 +02001430/*
1431 * fd.io coding-style-patch-verification: ON
1432 *
1433 * Local Variables:
1434 * eval: (c-set-style "gnu")
1435 * End:
1436 */