blob: 4bf39144ddb0f7dd724b0a1e2c105f72d7ad05c5 [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
Klement Sekerad0f70a32018-12-14 17:24:13 +010057typedef enum
58{
59 IP4_REASS_RC_OK,
60 IP4_REASS_RC_INTERNAL_ERROR,
61} ip4_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020062
63typedef struct
64{
65 union
66 {
67 struct
68 {
Klement Sekera75e7d132017-09-20 08:26:30 +020069 u32 xx_id;
70 ip4_address_t src;
71 ip4_address_t dst;
Klement Sekera8dcfed52018-06-28 11:16:15 +020072 u16 frag_id;
73 u8 proto;
74 u8 unused;
Klement Sekera75e7d132017-09-20 08:26:30 +020075 };
Klement Sekera8dcfed52018-06-28 11:16:15 +020076 u64 as_u64[2];
Klement Sekera75e7d132017-09-20 08:26:30 +020077 };
78} ip4_reass_key_t;
79
80always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +020081ip4_reass_buffer_get_data_offset (vlib_buffer_t * b)
82{
83 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010084 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020085}
86
87always_inline u16
88ip4_reass_buffer_get_data_len (vlib_buffer_t * b)
89{
90 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010091 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
92 (vnb->ip.reass.fragment_first + ip4_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020093}
94
95typedef struct
96{
97 // hash table key
98 ip4_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +020099 // time when last packet was received
100 f64 last_heard;
101 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100102 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200103 // buffer index of first buffer in this reassembly context
104 u32 first_bi;
105 // last octet of packet, ~0 until fragment without more_fragments arrives
106 u32 last_packet_octet;
107 // length of data collected so far
108 u32 data_len;
109 // trace operation counter
110 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100111 // next index - used by non-feature node
112 u8 next_index;
113 // minimum fragment length for this reassembly - used to estimate MTU
114 u16 min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200115} ip4_reass_t;
116
117typedef struct
118{
Klement Sekera4c533132018-02-22 11:41:12 +0100119 ip4_reass_t *pool;
120 u32 reass_n;
121 u32 buffers_n;
122 u32 id_counter;
123 clib_spinlock_t lock;
124} ip4_reass_per_thread_t;
125
126typedef struct
127{
Klement Sekera75e7d132017-09-20 08:26:30 +0200128 // IPv4 config
129 u32 timeout_ms;
130 f64 timeout;
131 u32 expire_walk_interval_ms;
132 u32 max_reass_n;
133
134 // IPv4 runtime
Klement Sekera8dcfed52018-06-28 11:16:15 +0200135 clib_bihash_16_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100136 // per-thread data
137 ip4_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200138
139 // convenience
140 vlib_main_t *vlib_main;
141 vnet_main_t *vnet_main;
142
143 // node index of ip4-drop node
144 u32 ip4_drop_idx;
145 u32 ip4_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200146} ip4_reass_main_t;
147
148ip4_reass_main_t ip4_reass_main;
149
150typedef enum
151{
152 IP4_REASSEMBLY_NEXT_INPUT,
153 IP4_REASSEMBLY_NEXT_DROP,
154 IP4_REASSEMBLY_N_NEXT,
155} ip4_reass_next_t;
156
157typedef enum
158{
159 RANGE_NEW,
160 RANGE_SHRINK,
161 RANGE_DISCARD,
162 RANGE_OVERLAP,
163 FINALIZE,
164} ip4_reass_trace_operation_e;
165
166typedef struct
167{
168 u16 range_first;
169 u16 range_last;
170 u32 range_bi;
171 i32 data_offset;
172 u32 data_len;
173 u32 first_bi;
174} ip4_reass_range_trace_t;
175
176typedef struct
177{
178 ip4_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200179 u32 reass_id;
180 ip4_reass_range_trace_t trace_range;
181 u32 size_diff;
182 u32 op_id;
183 u32 fragment_first;
184 u32 fragment_last;
185 u32 total_data_len;
186} ip4_reass_trace_t;
187
Klement Sekera4c533132018-02-22 11:41:12 +0100188static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200189ip4_reass_trace_details (vlib_main_t * vm, u32 bi,
190 ip4_reass_range_trace_t * trace)
191{
192 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
193 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
194 trace->range_first = vnb->ip.reass.range_first;
195 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100196 trace->data_offset = ip4_reass_buffer_get_data_offset (b);
197 trace->data_len = ip4_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200198 trace->range_bi = bi;
199}
200
Klement Sekera4c533132018-02-22 11:41:12 +0100201static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200202format_ip4_reass_range_trace (u8 * s, va_list * args)
203{
204 ip4_reass_range_trace_t *trace = va_arg (*args, ip4_reass_range_trace_t *);
205 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
206 trace->range_last, trace->data_offset, trace->data_len,
207 trace->range_bi);
208 return s;
209}
210
211u8 *
212format_ip4_reass_trace (u8 * s, va_list * args)
213{
214 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
215 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
216 ip4_reass_trace_t *t = va_arg (*args, ip4_reass_trace_t *);
217 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
218 u32 indent = format_get_indent (s);
219 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
220 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
221 t->fragment_last);
222 switch (t->action)
223 {
224 case RANGE_SHRINK:
225 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
226 format_ip4_reass_range_trace, &t->trace_range,
227 t->size_diff);
228 break;
229 case RANGE_DISCARD:
230 s = format (s, "\n%Udiscard %U", format_white_space, indent,
231 format_ip4_reass_range_trace, &t->trace_range);
232 break;
233 case RANGE_NEW:
234 s = format (s, "\n%Unew %U", format_white_space, indent,
235 format_ip4_reass_range_trace, &t->trace_range);
236 break;
237 case RANGE_OVERLAP:
238 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
239 format_ip4_reass_range_trace, &t->trace_range);
240 break;
241 case FINALIZE:
242 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
243 break;
244 }
245 return s;
246}
247
Klement Sekera4c533132018-02-22 11:41:12 +0100248static void
Klement Sekera75e7d132017-09-20 08:26:30 +0200249ip4_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
250 ip4_reass_main_t * rm, ip4_reass_t * reass, u32 bi,
251 ip4_reass_trace_operation_e action, u32 size_diff)
252{
253 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
254 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100255 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
256 {
257 // this buffer's trace is gone
258 b->flags &= ~VLIB_BUFFER_IS_TRACED;
259 return;
260 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200261 ip4_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200262 t->reass_id = reass->id;
263 t->action = action;
264 ip4_reass_trace_details (vm, bi, &t->trace_range);
265 t->size_diff = size_diff;
266 t->op_id = reass->trace_op_counter;
267 ++reass->trace_op_counter;
268 t->fragment_first = vnb->ip.reass.fragment_first;
269 t->fragment_last = vnb->ip.reass.fragment_last;
270 t->trace_range.first_bi = reass->first_bi;
271 t->total_data_len = reass->data_len;
272#if 0
273 static u8 *s = NULL;
274 s = format (s, "%U", format_ip4_reass_trace, NULL, NULL, t);
275 printf ("%.*s\n", vec_len (s), s);
276 fflush (stdout);
277 vec_reset_length (s);
278#endif
279}
280
Klement Sekera4c533132018-02-22 11:41:12 +0100281always_inline void
282ip4_reass_free (ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
283 ip4_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200284{
Klement Sekera8dcfed52018-06-28 11:16:15 +0200285 clib_bihash_kv_16_8_t kv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200286 kv.key[0] = reass->key.as_u64[0];
287 kv.key[1] = reass->key.as_u64[1];
Klement Sekera8dcfed52018-06-28 11:16:15 +0200288 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100289 pool_put (rt->pool, reass);
290 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200291}
292
Klement Sekera4c533132018-02-22 11:41:12 +0100293always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200294ip4_reass_on_timeout (vlib_main_t * vm, ip4_reass_main_t * rm,
295 ip4_reass_t * reass, u32 ** vec_drop_timeout)
296{
297 u32 range_bi = reass->first_bi;
298 vlib_buffer_t *range_b;
299 vnet_buffer_opaque_t *range_vnb;
300 while (~0 != range_bi)
301 {
302 range_b = vlib_get_buffer (vm, range_bi);
303 range_vnb = vnet_buffer (range_b);
304 u32 bi = range_bi;
305 while (~0 != bi)
306 {
307 vec_add1 (*vec_drop_timeout, bi);
308 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
309 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
310 {
311 bi = b->next_buffer;
312 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
313 }
314 else
315 {
316 bi = ~0;
317 }
318 }
319 range_bi = range_vnb->ip.reass.next_range_bi;
320 }
321}
322
323ip4_reass_t *
324ip4_reass_find_or_create (vlib_main_t * vm, ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100325 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200326 ip4_reass_key_t * k, u32 ** vec_drop_timeout)
327{
328 ip4_reass_t *reass = NULL;
329 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera8dcfed52018-06-28 11:16:15 +0200330 clib_bihash_kv_16_8_t kv, value;
Klement Sekera75e7d132017-09-20 08:26:30 +0200331 kv.key[0] = k->as_u64[0];
332 kv.key[1] = k->as_u64[1];
Klement Sekera75e7d132017-09-20 08:26:30 +0200333
Klement Sekera8dcfed52018-06-28 11:16:15 +0200334 if (!clib_bihash_search_16_8 (&rm->hash, &kv, &value))
Klement Sekera75e7d132017-09-20 08:26:30 +0200335 {
Klement Sekera4c533132018-02-22 11:41:12 +0100336 reass = pool_elt_at_index (rt->pool, value.value);
Klement Sekera75e7d132017-09-20 08:26:30 +0200337 if (now > reass->last_heard + rm->timeout)
338 {
339 ip4_reass_on_timeout (vm, rm, reass, vec_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100340 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200341 reass = NULL;
342 }
343 }
344
345 if (reass)
346 {
347 reass->last_heard = now;
348 return reass;
349 }
350
Klement Sekera4c533132018-02-22 11:41:12 +0100351 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200352 {
353 reass = NULL;
354 return reass;
355 }
356 else
357 {
Klement Sekera4c533132018-02-22 11:41:12 +0100358 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400359 clib_memset (reass, 0, sizeof (*reass));
Klement Sekera4c533132018-02-22 11:41:12 +0100360 reass->id =
361 ((u64) os_get_thread_index () * 1000000000) + rt->id_counter;
362 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200363 reass->first_bi = ~0;
364 reass->last_packet_octet = ~0;
365 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100366 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200367 }
368
369 reass->key.as_u64[0] = kv.key[0] = k->as_u64[0];
370 reass->key.as_u64[1] = kv.key[1] = k->as_u64[1];
Klement Sekera4c533132018-02-22 11:41:12 +0100371 kv.value = reass - rt->pool;
Klement Sekera75e7d132017-09-20 08:26:30 +0200372 reass->last_heard = now;
373
Klement Sekera8dcfed52018-06-28 11:16:15 +0200374 if (clib_bihash_add_del_16_8 (&rm->hash, &kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200375 {
Klement Sekera4c533132018-02-22 11:41:12 +0100376 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200377 reass = NULL;
378 }
379
380 return reass;
381}
382
Klement Sekerad0f70a32018-12-14 17:24:13 +0100383always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200384ip4_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100385 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
386 ip4_reass_t * reass, u32 * bi0, u32 * next0,
Damjan Marionaa682a32018-04-26 22:45:40 +0200387 u32 * error0, u32 ** vec_drop_compress,
Klement Sekera4c533132018-02-22 11:41:12 +0100388 u32 ** vec_drop_overlap, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200389{
Klement Sekera75e7d132017-09-20 08:26:30 +0200390 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
391 vlib_buffer_t *last_b = NULL;
392 u32 sub_chain_bi = reass->first_bi;
393 u32 total_length = 0;
394 u32 buf_cnt = 0;
395 u32 dropped_cnt = 0;
396 do
397 {
398 u32 tmp_bi = sub_chain_bi;
399 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
400 ip4_header_t *ip = vlib_buffer_get_current (tmp);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100401 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
402 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
403 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
404 {
405 return IP4_REASS_RC_INTERNAL_ERROR;
406 }
407
Klement Sekera75e7d132017-09-20 08:26:30 +0200408 u32 data_len = ip4_reass_buffer_get_data_len (tmp);
409 u32 trim_front =
410 ip4_header_bytes (ip) + ip4_reass_buffer_get_data_offset (tmp);
411 u32 trim_end =
412 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
413 if (tmp_bi == reass->first_bi)
414 {
415 /* first buffer - keep ip4 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100416 if (0 != ip4_reass_buffer_get_data_offset (tmp))
417 {
418 return IP4_REASS_RC_INTERNAL_ERROR;
419 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200420 trim_front = 0;
421 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
422 ip4_header_bytes (ip);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100423 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
424 {
425 return IP4_REASS_RC_INTERNAL_ERROR;
426 }
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;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100441 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
442 {
443 return IP4_REASS_RC_INTERNAL_ERROR;
444 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200445 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
446 tmp_bi = tmp->next_buffer;
447 tmp = vlib_get_buffer (vm, tmp_bi);
448 continue;
449 }
450 else
451 {
452 vlib_buffer_advance (tmp, trim_front);
453 trim_front = 0;
454 }
455 }
456 if (keep_data)
457 {
458 if (last_b)
459 {
460 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
461 last_b->next_buffer = tmp_bi;
462 }
463 last_b = tmp;
464 if (keep_data <= tmp->current_length)
465 {
466 tmp->current_length = keep_data;
467 keep_data = 0;
468 }
469 else
470 {
471 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100472 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
473 {
474 return IP4_REASS_RC_INTERNAL_ERROR;
475 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200476 }
477 total_length += tmp->current_length;
478 }
479 else
480 {
481 vec_add1 (*vec_drop_overlap, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100482 if (reass->first_bi == tmp_bi)
483 {
484 return IP4_REASS_RC_INTERNAL_ERROR;
485 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200486 ++dropped_cnt;
487 }
488 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
489 {
490 tmp_bi = tmp->next_buffer;
491 tmp = vlib_get_buffer (vm, tmp->next_buffer);
492 }
493 else
494 {
495 break;
496 }
497 }
498 sub_chain_bi =
499 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
500 reass.next_range_bi;
501 }
502 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700503
Klement Sekerad0f70a32018-12-14 17:24:13 +0100504 if (!last_b)
505 {
506 return IP4_REASS_RC_INTERNAL_ERROR;
507 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200508 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Klement Sekera4c533132018-02-22 11:41:12 +0100509 rt->buffers_n -= buf_cnt - dropped_cnt;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100510 if (total_length < first_b->current_length)
511 {
512 return IP4_REASS_RC_INTERNAL_ERROR;
513 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200514 total_length -= first_b->current_length;
515 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
516 first_b->total_length_not_including_first_buffer = total_length;
517 ip4_header_t *ip = vlib_buffer_get_current (first_b);
518 ip->flags_and_fragment_offset = 0;
519 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
520 ip->checksum = ip4_header_checksum (ip);
Klement Sekera400f6d82018-12-13 14:35:48 +0100521 u32 before = vec_len (*vec_drop_compress);
Klement Sekera75e7d132017-09-20 08:26:30 +0200522 vlib_buffer_chain_compress (vm, first_b, vec_drop_compress);
Klement Sekera400f6d82018-12-13 14:35:48 +0100523 rt->buffers_n += vec_len (*vec_drop_compress) - before;
524
Klement Sekera75e7d132017-09-20 08:26:30 +0200525 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
526 {
527 ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
528#if 0
529 // following code does a hexdump of packet fragments to stdout ...
530 do
531 {
532 u32 bi = reass->first_bi;
533 u8 *s = NULL;
534 while (~0 != bi)
535 {
536 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
537 s = format (s, "%u: %U\n", bi, format_hexdump,
538 vlib_buffer_get_current (b), b->current_length);
539 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
540 {
541 bi = b->next_buffer;
542 }
543 else
544 {
545 break;
546 }
547 }
548 printf ("%.*s\n", vec_len (s), s);
549 fflush (stdout);
550 vec_free (s);
551 }
552 while (0);
553#endif
554 }
555 *bi0 = reass->first_bi;
Klement Sekera4c533132018-02-22 11:41:12 +0100556 if (is_feature)
557 {
558 *next0 = IP4_REASSEMBLY_NEXT_INPUT;
559 }
560 else
561 {
562 *next0 = reass->next_index;
563 }
564 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200565 *error0 = IP4_ERROR_NONE;
Klement Sekera4c533132018-02-22 11:41:12 +0100566 ip4_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200567 reass = NULL;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100568 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200569}
570
Klement Sekera4c533132018-02-22 11:41:12 +0100571always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +0200572ip4_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b)
573{
574 u32 len = 0;
575 while (b)
576 {
577 ++len;
578 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
579 {
580 b = vlib_get_buffer (vm, b->next_buffer);
581 }
582 else
583 {
584 break;
585 }
586 }
587 return len;
588}
589
Klement Sekerad0f70a32018-12-14 17:24:13 +0100590always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200591ip4_reass_insert_range_in_chain (vlib_main_t * vm,
592 ip4_reass_main_t * rm,
Klement Sekera4c533132018-02-22 11:41:12 +0100593 ip4_reass_per_thread_t * rt,
Klement Sekera75e7d132017-09-20 08:26:30 +0200594 ip4_reass_t * reass,
595 u32 prev_range_bi, u32 new_next_bi)
596{
Klement Sekera75e7d132017-09-20 08:26:30 +0200597 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
598 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
599 if (~0 != prev_range_bi)
600 {
601 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
602 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
603 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
604 prev_vnb->ip.reass.next_range_bi = new_next_bi;
605 }
606 else
607 {
608 if (~0 != reass->first_bi)
609 {
610 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
611 }
612 reass->first_bi = new_next_bi;
613 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100614 vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
615 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
616 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
617 {
618 return IP4_REASS_RC_INTERNAL_ERROR;
619 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200620 reass->data_len += ip4_reass_buffer_get_data_len (new_next_b);
Klement Sekera4c533132018-02-22 11:41:12 +0100621 rt->buffers_n += ip4_reass_get_buffer_chain_length (vm, new_next_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100622 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200623}
624
Klement Sekerad0f70a32018-12-14 17:24:13 +0100625always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200626ip4_reass_remove_range_from_chain (vlib_main_t * vm,
627 vlib_node_runtime_t * node,
628 ip4_reass_main_t * rm,
629 u32 ** vec_drop_overlap,
630 ip4_reass_t * reass, u32 prev_range_bi,
631 u32 discard_bi)
632{
633 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
634 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
635 if (~0 != prev_range_bi)
636 {
637 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
638 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100639 if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
640 {
641 return IP4_REASS_RC_INTERNAL_ERROR;
642 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200643 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
644 }
645 else
646 {
647 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
648 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100649 vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
650 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
651 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
652 {
653 return IP4_REASS_RC_INTERNAL_ERROR;
654 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200655 reass->data_len -= ip4_reass_buffer_get_data_len (discard_b);
656 while (1)
657 {
658 vec_add1 (*vec_drop_overlap, discard_bi);
659 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
660 {
661 ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD,
662 0);
663 }
664 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
665 {
666 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
667 discard_bi = discard_b->next_buffer;
668 discard_b = vlib_get_buffer (vm, discard_bi);
669 }
670 else
671 {
672 break;
673 }
674 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100675 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200676}
677
Klement Sekerad0f70a32018-12-14 17:24:13 +0100678always_inline ip4_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200679ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100680 ip4_reass_main_t * rm, ip4_reass_per_thread_t * rt,
681 ip4_reass_t * reass, u32 * bi0, u32 * next0,
Damjan Marionaa682a32018-04-26 22:45:40 +0200682 u32 * error0, u32 ** vec_drop_overlap,
Klement Sekera4c533132018-02-22 11:41:12 +0100683 u32 ** vec_drop_compress, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200684{
Klement Sekerad0f70a32018-12-14 17:24:13 +0100685 ip4_reass_rc_t rc = IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200686 int consumed = 0;
687 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
688 ip4_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekera75e7d132017-09-20 08:26:30 +0200689 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700690 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera14d7e902018-12-10 13:46:09 +0100691 const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
692 const u32 fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200693 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
Klement Sekera14d7e902018-12-10 13:46:09 +0100694 const u32 fragment_last = fragment_first + fragment_length - 1;
695 fvnb->ip.reass.fragment_first = fragment_first;
696 fvnb->ip.reass.fragment_last = fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200697 int more_fragments = ip4_get_fragment_more (fip);
698 u32 candidate_range_bi = reass->first_bi;
699 u32 prev_range_bi = ~0;
700 fvnb->ip.reass.range_first = fragment_first;
701 fvnb->ip.reass.range_last = fragment_last;
702 fvnb->ip.reass.next_range_bi = ~0;
703 if (!more_fragments)
704 {
705 reass->last_packet_octet = fragment_last;
706 }
707 if (~0 == reass->first_bi)
708 {
709 // starting a new reassembly
Klement Sekerad0f70a32018-12-14 17:24:13 +0100710 rc =
711 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
712 *bi0);
713 if (IP4_REASS_RC_OK != rc)
714 {
715 return rc;
716 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200717 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
718 {
719 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
720 }
721 *bi0 = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100722 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100723 return IP4_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200724 }
Klement Sekera4c533132018-02-22 11:41:12 +0100725 reass->min_fragment_length = clib_min (clib_net_to_host_u16 (fip->length),
726 fvnb->ip.reass.estimated_mtu);
Klement Sekera75e7d132017-09-20 08:26:30 +0200727 while (~0 != candidate_range_bi)
728 {
729 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
730 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
731 if (fragment_first > candidate_vnb->ip.reass.range_last)
732 {
733 // this fragments starts after candidate range
734 prev_range_bi = candidate_range_bi;
735 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
736 if (candidate_vnb->ip.reass.range_last < fragment_last &&
737 ~0 == candidate_range_bi)
738 {
739 // special case - this fragment falls beyond all known ranges
Klement Sekerad0f70a32018-12-14 17:24:13 +0100740 rc =
741 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
742 prev_range_bi, *bi0);
743 if (IP4_REASS_RC_OK != rc)
744 {
745 return rc;
746 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200747 consumed = 1;
748 break;
749 }
750 continue;
751 }
752 if (fragment_last < candidate_vnb->ip.reass.range_first)
753 {
754 // this fragment ends before candidate range without any overlap
Klement Sekerad0f70a32018-12-14 17:24:13 +0100755 rc =
756 ip4_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
757 *bi0);
758 if (IP4_REASS_RC_OK != rc)
759 {
760 return rc;
761 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200762 consumed = 1;
763 }
764 else
765 {
766 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
767 fragment_last <= candidate_vnb->ip.reass.range_last)
768 {
769 // this fragment is a (sub)part of existing range, ignore it
770 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
771 {
772 ip4_reass_add_trace (vm, node, rm, reass, *bi0,
773 RANGE_OVERLAP, 0);
774 }
775 break;
776 }
777 int discard_candidate = 0;
778 if (fragment_first < candidate_vnb->ip.reass.range_first)
779 {
780 u32 overlap =
781 fragment_last - candidate_vnb->ip.reass.range_first + 1;
782 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
783 {
784 candidate_vnb->ip.reass.range_first += overlap;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100785 if (reass->data_len < overlap)
786 {
787 return IP4_REASS_RC_INTERNAL_ERROR;
788 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200789 reass->data_len -= overlap;
790 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
791 {
792 ip4_reass_add_trace (vm, node, rm, reass,
793 candidate_range_bi, RANGE_SHRINK,
794 overlap);
795 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100796 rc =
797 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
798 prev_range_bi, *bi0);
799 if (IP4_REASS_RC_OK != rc)
800 {
801 return rc;
802 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200803 consumed = 1;
804 }
805 else
806 {
807 discard_candidate = 1;
808 }
809 }
810 else if (fragment_last > candidate_vnb->ip.reass.range_last)
811 {
812 u32 overlap =
813 candidate_vnb->ip.reass.range_last - fragment_first + 1;
814 if (overlap < ip4_reass_buffer_get_data_len (candidate_b))
815 {
816 fvnb->ip.reass.range_first += overlap;
817 if (~0 != candidate_vnb->ip.reass.next_range_bi)
818 {
819 prev_range_bi = candidate_range_bi;
820 candidate_range_bi =
821 candidate_vnb->ip.reass.next_range_bi;
822 continue;
823 }
824 else
825 {
826 // special case - last range discarded
Klement Sekerad0f70a32018-12-14 17:24:13 +0100827 rc =
828 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
829 candidate_range_bi,
830 *bi0);
831 if (IP4_REASS_RC_OK != rc)
832 {
833 return rc;
834 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200835 consumed = 1;
836 }
837 }
838 else
839 {
840 discard_candidate = 1;
841 }
842 }
843 else
844 {
845 discard_candidate = 1;
846 }
847 if (discard_candidate)
848 {
849 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
850 // discard candidate range, probe next range
Klement Sekerad0f70a32018-12-14 17:24:13 +0100851 rc =
852 ip4_reass_remove_range_from_chain (vm, node, rm,
853 vec_drop_overlap, reass,
854 prev_range_bi,
855 candidate_range_bi);
856 if (IP4_REASS_RC_OK != rc)
857 {
858 return rc;
859 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200860 if (~0 != next_range_bi)
861 {
862 candidate_range_bi = next_range_bi;
863 continue;
864 }
865 else
866 {
867 // special case - last range discarded
Klement Sekerad0f70a32018-12-14 17:24:13 +0100868 rc =
869 ip4_reass_insert_range_in_chain (vm, rm, rt, reass,
870 prev_range_bi, *bi0);
871 if (IP4_REASS_RC_OK != rc)
872 {
873 return rc;
874 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200875 consumed = 1;
876 }
877 }
878 }
879 break;
880 }
881 if (consumed)
882 {
883 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
884 {
885 ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
886 }
887 }
888 if (~0 != reass->last_packet_octet &&
889 reass->data_len == reass->last_packet_octet + 1)
890 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100891 return ip4_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
892 vec_drop_compress, vec_drop_overlap,
893 is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200894 }
895 else
896 {
897 if (consumed)
898 {
899 *bi0 = ~0;
900 }
901 else
902 {
Klement Sekera4c533132018-02-22 11:41:12 +0100903 *next0 = IP4_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200904 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
905 }
906 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100907 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +0200908}
909
910always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100911ip4_reassembly_inline (vlib_main_t * vm,
912 vlib_node_runtime_t * node,
913 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200914{
915 u32 *from = vlib_frame_vector_args (frame);
916 u32 n_left_from, n_left_to_next, *to_next, next_index;
917 ip4_reass_main_t *rm = &ip4_reass_main;
Klement Sekera4c533132018-02-22 11:41:12 +0100918 ip4_reass_per_thread_t *rt = &rm->per_thread_data[os_get_thread_index ()];
919 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200920
921 n_left_from = frame->n_vectors;
922 next_index = node->cached_next_index;
923 static u32 *vec_drop_timeout = NULL; // indexes of buffers which timed out
924 static u32 *vec_drop_overlap = NULL; // indexes of buffers which were discarded due to overlap
Klement Sekerad0f70a32018-12-14 17:24:13 +0100925 static u32 *vec_drop_internal_error = NULL; // indexes of buffers which were discarded due to internal errors
Klement Sekera75e7d132017-09-20 08:26:30 +0200926 static u32 *vec_drop_compress = NULL; // indexes of buffers dicarded due to buffer compression
Klement Sekerad0f70a32018-12-14 17:24:13 +0100927 while (n_left_from > 0 || vec_len (vec_drop_timeout) > 0
928 || vec_len (vec_drop_overlap) > 0 || vec_len (vec_drop_compress) > 0
929 || vec_len (vec_drop_internal_error) > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200930 {
931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
932
933 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
934 {
935 u32 bi = vec_pop (vec_drop_timeout);
936 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
937 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
938 to_next[0] = bi;
939 to_next += 1;
940 n_left_to_next -= 1;
941 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
942 n_left_to_next, bi,
943 IP4_REASSEMBLY_NEXT_DROP);
944 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout);
Klement Sekera4c533132018-02-22 11:41:12 +0100945 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200946 }
947
948 while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0)
949 {
950 u32 bi = vec_pop (vec_drop_overlap);
951 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
952 b->error = node->errors[IP4_ERROR_REASS_DUPLICATE_FRAGMENT];
953 to_next[0] = bi;
954 to_next += 1;
955 n_left_to_next -= 1;
956 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
957 n_left_to_next, bi,
958 IP4_REASSEMBLY_NEXT_DROP);
959 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_duplicate_fragment);
Klement Sekera4c533132018-02-22 11:41:12 +0100960 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200961 }
962
963 while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0)
964 {
965 u32 bi = vec_pop (vec_drop_compress);
966 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
967 b->error = node->errors[IP4_ERROR_NONE];
968 to_next[0] = bi;
969 to_next += 1;
970 n_left_to_next -= 1;
971 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
972 n_left_to_next, bi,
973 IP4_REASSEMBLY_NEXT_DROP);
974 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_compress);
Klement Sekera4c533132018-02-22 11:41:12 +0100975 --rt->buffers_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200976 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100977 while (vec_len (vec_drop_internal_error) > 0 && n_left_to_next > 0)
978 {
979 u32 bi = vec_pop (vec_drop_internal_error);
980 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
981 b->error = node->errors[IP4_ERROR_REASS_INTERNAL_ERROR];
982 to_next[0] = bi;
983 to_next += 1;
984 n_left_to_next -= 1;
985 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
986 n_left_to_next, bi,
987 IP4_REASSEMBLY_NEXT_DROP);
988 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_internal_error);
989 --rt->buffers_n;
990 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200991 while (n_left_from > 0 && n_left_to_next > 0)
992 {
993 u32 bi0;
994 vlib_buffer_t *b0;
Klement Sekera4c533132018-02-22 11:41:12 +0100995 u32 next0;
996 u32 error0 = IP4_ERROR_NONE;
Klement Sekera75e7d132017-09-20 08:26:30 +0200997
998 bi0 = from[0];
999 b0 = vlib_get_buffer (vm, bi0);
1000
1001 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001002 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
Klement Sekera75e7d132017-09-20 08:26:30 +02001003 {
Klement Sekera4c533132018-02-22 11:41:12 +01001004 // this is a whole packet - no fragmentation
1005 if (is_feature)
1006 {
1007 next0 = IP4_REASSEMBLY_NEXT_INPUT;
1008 }
1009 else
1010 {
1011 next0 = vnet_buffer (b0)->ip.reass.next_index;
1012 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001013 }
1014 else
1015 {
Klement Sekera4ee633e2018-12-14 12:00:44 +01001016 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
Klement Sekera14d7e902018-12-10 13:46:09 +01001017 const u32 fragment_length =
Klement Sekera4ee633e2018-12-14 12:00:44 +01001018 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
Klement Sekera14d7e902018-12-10 13:46:09 +01001019 const u32 fragment_last = fragment_first + fragment_length - 1;
Klement Sekera4ee633e2018-12-14 12:00:44 +01001020 if (fragment_first > fragment_last || fragment_first + fragment_length > UINT16_MAX - 20 || (fragment_length < 8 && ip4_get_fragment_more (ip0))) // 8 is minimum frag length per RFC 791
Klement Sekera4c533132018-02-22 11:41:12 +01001021 {
Klement Sekera14d7e902018-12-10 13:46:09 +01001022 next0 = IP4_REASSEMBLY_NEXT_DROP;
1023 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
Klement Sekera4c533132018-02-22 11:41:12 +01001024 }
1025 else
1026 {
Klement Sekera14d7e902018-12-10 13:46:09 +01001027 ip4_reass_key_t k;
1028 k.as_u64[0] =
Klement Sekerad0f70a32018-12-14 17:24:13 +01001029 (u64) vnet_buffer (b0)->sw_if_index[VLIB_RX] |
1030 (u64) ip0->src_address.as_u32 << 32;
Klement Sekera14d7e902018-12-10 13:46:09 +01001031 k.as_u64[1] =
Klement Sekerad0f70a32018-12-14 17:24:13 +01001032 (u64) ip0->dst_address.as_u32 |
1033 (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
Klement Sekera14d7e902018-12-10 13:46:09 +01001034
1035 ip4_reass_t *reass =
1036 ip4_reass_find_or_create (vm, rm, rt, &k,
1037 &vec_drop_timeout);
1038
1039 if (reass)
1040 {
Klement Sekerad0f70a32018-12-14 17:24:13 +01001041 switch (ip4_reass_update
1042 (vm, node, rm, rt, reass, &bi0, &next0, &error0,
1043 &vec_drop_overlap, &vec_drop_compress,
1044 is_feature))
1045 {
1046 case IP4_REASS_RC_OK:
1047 /* nothing to do here */
1048 break;
1049 case IP4_REASS_RC_INTERNAL_ERROR:
1050 /* drop everything and start with a clean slate */
1051 ip4_reass_on_timeout (vm, rm, reass,
1052 &vec_drop_internal_error);
1053 ip4_reass_free (rm, rt, reass);
1054 goto next_packet;
1055 break;
1056 }
Klement Sekera14d7e902018-12-10 13:46:09 +01001057 }
1058 else
1059 {
1060 next0 = IP4_REASSEMBLY_NEXT_DROP;
1061 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
1062 }
Klement Sekera4c533132018-02-22 11:41:12 +01001063 }
1064
1065 b0->error = node->errors[error0];
1066 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001067
1068 if (bi0 != ~0)
1069 {
1070 to_next[0] = bi0;
1071 to_next += 1;
1072 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001073 if (is_feature && IP4_ERROR_NONE == error0)
1074 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001075 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001076 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001077 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001078 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1079 n_left_to_next, bi0, next0);
1080 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1081 }
1082
Klement Sekerad0f70a32018-12-14 17:24:13 +01001083 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001084 from += 1;
1085 n_left_from -= 1;
1086 }
1087
1088 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1089 }
1090
Klement Sekera4c533132018-02-22 11:41:12 +01001091 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001092 return frame->n_vectors;
1093}
1094
1095static char *ip4_reassembly_error_strings[] = {
1096#define _(sym, string) string,
1097 foreach_ip4_error
1098#undef _
1099};
1100
Damjan Marion2574d9f2018-06-26 14:45:49 +02001101static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001102ip4_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node,
1103 vlib_frame_t * frame)
1104{
1105 return ip4_reassembly_inline (vm, node, frame, false /* is_feature */ );
1106}
1107
Klement Sekera75e7d132017-09-20 08:26:30 +02001108/* *INDENT-OFF* */
1109VLIB_REGISTER_NODE (ip4_reass_node, static) = {
1110 .function = ip4_reassembly,
1111 .name = "ip4-reassembly",
1112 .vector_size = sizeof (u32),
1113 .format_trace = format_ip4_reass_trace,
1114 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1115 .error_strings = ip4_reassembly_error_strings,
1116 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1117 .next_nodes =
1118 {
1119 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1120 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1121 },
1122};
1123/* *INDENT-ON* */
1124
Klement Sekera4c533132018-02-22 11:41:12 +01001125VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node, ip4_reassembly);
1126
Damjan Marion2574d9f2018-06-26 14:45:49 +02001127static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001128ip4_reassembly_feature (vlib_main_t * vm,
1129 vlib_node_runtime_t * node, vlib_frame_t * frame)
1130{
1131 return ip4_reassembly_inline (vm, node, frame, true /* is_feature */ );
1132}
1133
1134/* *INDENT-OFF* */
1135VLIB_REGISTER_NODE (ip4_reass_node_feature, static) = {
1136 .function = ip4_reassembly_feature,
1137 .name = "ip4-reassembly-feature",
1138 .vector_size = sizeof (u32),
1139 .format_trace = format_ip4_reass_trace,
1140 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1141 .error_strings = ip4_reassembly_error_strings,
1142 .n_next_nodes = IP4_REASSEMBLY_N_NEXT,
1143 .next_nodes =
1144 {
1145 [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input",
1146 [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop",
1147 },
1148};
1149/* *INDENT-ON* */
1150
1151VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node_feature, ip4_reassembly_feature);
1152
1153/* *INDENT-OFF* */
1154VNET_FEATURE_INIT (ip4_reassembly_feature, static) = {
1155 .arc_name = "ip4-unicast",
1156 .node_name = "ip4-reassembly-feature",
1157 .runs_before = VNET_FEATURES ("ip4-lookup"),
1158 .runs_after = 0,
1159};
1160/* *INDENT-ON* */
1161
1162always_inline u32
1163ip4_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001164{
1165 ip4_reass_main_t *rm = &ip4_reass_main;
1166 u32 nbuckets;
1167 u8 i;
1168
1169 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1170
1171 for (i = 0; i < 31; i++)
1172 if ((1 << i) >= nbuckets)
1173 break;
1174 nbuckets = 1 << i;
1175
1176 return nbuckets;
1177}
1178
1179typedef enum
1180{
1181 IP4_EVENT_CONFIG_CHANGED = 1,
1182} ip4_reass_event_t;
1183
1184typedef struct
1185{
1186 int failure;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001187 clib_bihash_16_8_t *new_hash;
Klement Sekera75e7d132017-09-20 08:26:30 +02001188} ip4_rehash_cb_ctx;
1189
Klement Sekera4c533132018-02-22 11:41:12 +01001190static void
Klement Sekera8dcfed52018-06-28 11:16:15 +02001191ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
Klement Sekera75e7d132017-09-20 08:26:30 +02001192{
1193 ip4_rehash_cb_ctx *ctx = _ctx;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001194 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +02001195 {
1196 ctx->failure = 1;
1197 }
1198}
1199
Klement Sekera4c533132018-02-22 11:41:12 +01001200static void
1201ip4_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1202 u32 expire_walk_interval_ms)
1203{
1204 ip4_reass_main.timeout_ms = timeout_ms;
1205 ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1206 ip4_reass_main.max_reass_n = max_reassemblies;
1207 ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1208}
1209
Klement Sekera75e7d132017-09-20 08:26:30 +02001210vnet_api_error_t
1211ip4_reass_set (u32 timeout_ms, u32 max_reassemblies,
1212 u32 expire_walk_interval_ms)
1213{
1214 u32 old_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera4c533132018-02-22 11:41:12 +01001215 ip4_reass_set_params (timeout_ms, max_reassemblies,
1216 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001217 vlib_process_signal_event (ip4_reass_main.vlib_main,
1218 ip4_reass_main.ip4_reass_expire_node_idx,
1219 IP4_EVENT_CONFIG_CHANGED, 0);
1220 u32 new_nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001221 if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001222 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001223 clib_bihash_16_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001224 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001225 ip4_rehash_cb_ctx ctx;
1226 ctx.failure = 0;
1227 ctx.new_hash = &new_hash;
Klement Sekera8dcfed52018-06-28 11:16:15 +02001228 clib_bihash_init_16_8 (&new_hash, "ip4-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001229 new_nbuckets * 1024);
Klement Sekera8dcfed52018-06-28 11:16:15 +02001230 clib_bihash_foreach_key_value_pair_16_8 (&ip4_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001231 ip4_rehash_cb, &ctx);
1232 if (ctx.failure)
1233 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001234 clib_bihash_free_16_8 (&new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001235 return -1;
1236 }
1237 else
1238 {
Klement Sekera8dcfed52018-06-28 11:16:15 +02001239 clib_bihash_free_16_8 (&ip4_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001240 clib_memcpy_fast (&ip4_reass_main.hash, &new_hash,
1241 sizeof (ip4_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001242 }
1243 }
1244 return 0;
1245}
1246
1247vnet_api_error_t
1248ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1249 u32 * expire_walk_interval_ms)
1250{
1251 *timeout_ms = ip4_reass_main.timeout_ms;
1252 *max_reassemblies = ip4_reass_main.max_reass_n;
1253 *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms;
1254 return 0;
1255}
1256
Klement Sekera4c533132018-02-22 11:41:12 +01001257static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001258ip4_reass_init_function (vlib_main_t * vm)
1259{
1260 ip4_reass_main_t *rm = &ip4_reass_main;
1261 clib_error_t *error = 0;
1262 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001263 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001264
1265 rm->vlib_main = vm;
1266 rm->vnet_main = vnet_get_main ();
1267
Juraj Slobodacd806922018-10-10 10:15:54 +02001268 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001269 ip4_reass_per_thread_t *rt;
1270 vec_foreach (rt, rm->per_thread_data)
1271 {
1272 clib_spinlock_init (&rt->lock);
1273 pool_alloc (rt->pool, rm->max_reass_n);
1274 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001275
1276 node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk");
1277 ASSERT (node);
1278 rm->ip4_reass_expire_node_idx = node->index;
1279
Klement Sekera3ecc2212018-03-27 10:34:43 +02001280 ip4_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1281 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1282 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1283
Klement Sekera75e7d132017-09-20 08:26:30 +02001284 nbuckets = ip4_reass_get_nbuckets ();
Klement Sekera8dcfed52018-06-28 11:16:15 +02001285 clib_bihash_init_16_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001286
Dave Barach1403fcd2018-02-05 09:45:43 -05001287 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001288 ASSERT (node);
1289 rm->ip4_drop_idx = node->index;
Klement Sekera4c533132018-02-22 11:41:12 +01001290
Klement Sekera75e7d132017-09-20 08:26:30 +02001291 return error;
1292}
1293
1294VLIB_INIT_FUNCTION (ip4_reass_init_function);
1295
1296static uword
1297ip4_reass_walk_expired (vlib_main_t * vm,
1298 vlib_node_runtime_t * node, vlib_frame_t * f)
1299{
1300 ip4_reass_main_t *rm = &ip4_reass_main;
1301 uword event_type, *event_data = 0;
1302
1303 while (true)
1304 {
1305 vlib_process_wait_for_event_or_clock (vm,
1306 (f64) rm->expire_walk_interval_ms
1307 / (f64) MSEC_PER_SEC);
1308 event_type = vlib_process_get_events (vm, &event_data);
1309
1310 switch (event_type)
1311 {
1312 case ~0: /* no events => timeout */
1313 /* nothing to do here */
1314 break;
1315 case IP4_EVENT_CONFIG_CHANGED:
1316 break;
1317 default:
1318 clib_warning ("BUG: event type 0x%wx", event_type);
1319 break;
1320 }
1321 f64 now = vlib_time_now (vm);
1322
1323 ip4_reass_t *reass;
1324 u32 *vec_drop_timeout = NULL;
1325 int *pool_indexes_to_free = NULL;
1326
Klement Sekera4c533132018-02-22 11:41:12 +01001327 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001328 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001329 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001330 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1331 {
1332 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1333 clib_spinlock_lock (&rt->lock);
1334
1335 vec_reset_length (pool_indexes_to_free);
1336 /* *INDENT-OFF* */
1337 pool_foreach_index (index, rt->pool, ({
1338 reass = pool_elt_at_index (rt->pool, index);
1339 if (now > reass->last_heard + rm->timeout)
1340 {
1341 vec_add1 (pool_indexes_to_free, index);
1342 }
1343 }));
1344 /* *INDENT-ON* */
1345 int *i;
1346 /* *INDENT-OFF* */
1347 vec_foreach (i, pool_indexes_to_free)
1348 {
1349 ip4_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1350 u32 before = vec_len (vec_drop_timeout);
1351 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1352 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1353 {
1354 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1355 b->trace_index))
1356 {
1357 /* the trace is gone, don't trace this buffer anymore */
1358 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1359 }
1360 }
1361 ip4_reass_on_timeout (vm, rm, reass, &vec_drop_timeout);
1362 u32 after = vec_len (vec_drop_timeout);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001363 rt->buffers_n -= (after - before);
Klement Sekera4c533132018-02-22 11:41:12 +01001364 ip4_reass_free (rm, rt, reass);
1365 }
1366 /* *INDENT-ON* */
1367
1368 clib_spinlock_unlock (&rt->lock);
1369 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001370
1371 while (vec_len (vec_drop_timeout) > 0)
1372 {
1373 vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip4_drop_idx);
1374 u32 *to_next = vlib_frame_vector_args (f);
1375 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001376 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001377 while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0)
1378 {
1379 u32 bi = vec_pop (vec_drop_timeout);
1380 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1381 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1382 {
1383 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1384 b->trace_index))
1385 {
1386 /* the trace is gone, don't trace this buffer anymore */
1387 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1388 }
1389 else
1390 {
Klement Sekera4c533132018-02-22 11:41:12 +01001391 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001392 }
1393 }
1394 b->error = node->errors[IP4_ERROR_REASS_TIMEOUT];
1395 to_next[0] = bi;
1396 ++f->n_vectors;
1397 to_next += 1;
1398 n_left_to_next -= 1;
1399 IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout_walk);
Klement Sekera75e7d132017-09-20 08:26:30 +02001400 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001401 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001402 vlib_put_frame_to_node (vm, rm->ip4_drop_idx, f);
1403 }
1404
1405 vec_free (pool_indexes_to_free);
1406 vec_free (vec_drop_timeout);
1407 if (event_data)
1408 {
1409 _vec_len (event_data) = 0;
1410 }
1411 }
1412
1413 return 0;
1414}
1415
1416static vlib_node_registration_t ip4_reass_expire_node;
1417
1418/* *INDENT-OFF* */
1419VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = {
1420 .function = ip4_reass_walk_expired,
1421 .type = VLIB_NODE_TYPE_PROCESS,
1422 .name = "ip4-reassembly-expire-walk",
1423 .format_trace = format_ip4_reass_trace,
1424 .n_errors = ARRAY_LEN (ip4_reassembly_error_strings),
1425 .error_strings = ip4_reassembly_error_strings,
1426
1427};
1428/* *INDENT-ON* */
1429
1430static u8 *
1431format_ip4_reass_key (u8 * s, va_list * args)
1432{
1433 ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *);
1434 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1435 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1436 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1437 return s;
1438}
1439
1440static u8 *
1441format_ip4_reass (u8 * s, va_list * args)
1442{
1443 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1444 ip4_reass_t *reass = va_arg (*args, ip4_reass_t *);
1445
Klement Sekera4c533132018-02-22 11:41:12 +01001446 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001447 "last_packet_octet: %u, trace_op_counter: %u\n",
1448 reass->id, format_ip4_reass_key, &reass->key, reass->first_bi,
1449 reass->data_len, reass->last_packet_octet,
1450 reass->trace_op_counter);
1451 u32 bi = reass->first_bi;
1452 u32 counter = 0;
1453 while (~0 != bi)
1454 {
1455 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1456 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1457 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1458 "fragment[%u, %u]\n",
1459 counter, vnb->ip.reass.range_first,
1460 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001461 ip4_reass_buffer_get_data_offset (b),
1462 ip4_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001463 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1464 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1465 {
1466 bi = b->next_buffer;
1467 }
1468 else
1469 {
1470 bi = ~0;
1471 }
1472 }
1473 return s;
1474}
1475
1476static clib_error_t *
1477show_ip4_reass (vlib_main_t * vm, unformat_input_t * input,
1478 CLIB_UNUSED (vlib_cli_command_t * lmd))
1479{
1480 ip4_reass_main_t *rm = &ip4_reass_main;
1481
1482 vlib_cli_output (vm, "---------------------");
1483 vlib_cli_output (vm, "IP4 reassembly status");
1484 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001485 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001486 if (unformat (input, "details"))
1487 {
Klement Sekera4c533132018-02-22 11:41:12 +01001488 details = true;
1489 }
1490
1491 u32 sum_reass_n = 0;
1492 u64 sum_buffers_n = 0;
1493 ip4_reass_t *reass;
1494 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001495 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001496 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1497 {
1498 ip4_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1499 clib_spinlock_lock (&rt->lock);
1500 if (details)
1501 {
1502 /* *INDENT-OFF* */
1503 pool_foreach (reass, rt->pool, {
1504 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1505 });
1506 /* *INDENT-ON* */
1507 }
1508 sum_reass_n += rt->reass_n;
1509 sum_buffers_n += rt->buffers_n;
1510 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001511 }
1512 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001513 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1514 (long unsigned) sum_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001515 vlib_cli_output (vm,
Klement Sekera4c533132018-02-22 11:41:12 +01001516 "Maximum configured concurrent IP4 reassemblies per worker-thread: %lu\n",
Klement Sekera75e7d132017-09-20 08:26:30 +02001517 (long unsigned) rm->max_reass_n);
1518 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001519 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001520 return 0;
1521}
1522
1523/* *INDENT-OFF* */
1524VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = {
1525 .path = "show ip4-reassembly",
1526 .short_help = "show ip4-reassembly [details]",
1527 .function = show_ip4_reass,
1528};
1529/* *INDENT-ON* */
1530
Klement Sekera4c533132018-02-22 11:41:12 +01001531vnet_api_error_t
1532ip4_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1533{
1534 return vnet_feature_enable_disable ("ip4-unicast", "ip4-reassembly-feature",
1535 sw_if_index, enable_disable, 0, 0);
1536}
1537
Klement Sekera75e7d132017-09-20 08:26:30 +02001538/*
1539 * fd.io coding-style-patch-verification: ON
1540 *
1541 * Local Variables:
1542 * eval: (c-set-style "gnu")
1543 * End:
1544 */