blob: 658fe25eeb1a990b1cde4c4758becf5d7d830baf [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 IPv6 Reassembly.
19 *
20 * This file contains the source code for IPv6 reassembly.
21 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
26#include <vppinfra/bihash_48_8.h>
27#include <vnet/ip/ip6_reassembly.h>
28
29#define MSEC_PER_SEC 1000
30#define IP6_REASS_TIMEOUT_DEFAULT_MS 100
31#define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
Klement Sekera4c533132018-02-22 11:41:12 +010032#define IP6_REASS_MAX_REASSEMBLIES_DEFAULT 1024
Klement Sekera75e7d132017-09-20 08:26:30 +020033#define IP6_REASS_HT_LOAD_FACTOR (0.75)
34
Klement Sekerad0f70a32018-12-14 17:24:13 +010035typedef enum
36{
37 IP6_REASS_RC_OK,
38 IP6_REASS_RC_INTERNAL_ERROR,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010039 IP6_REASS_RC_NO_BUF,
Klement Sekerad0f70a32018-12-14 17:24:13 +010040} ip6_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020041
42typedef struct
43{
44 union
45 {
46 struct
47 {
48 ip6_address_t src;
49 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020050 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020051 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020052 u8 unused[7];
53 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020054 };
55 u64 as_u64[6];
56 };
57} ip6_reass_key_t;
58
59always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +020060ip6_reass_buffer_get_data_offset (vlib_buffer_t * b)
61{
62 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010063 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020064}
65
66always_inline u16
67ip6_reass_buffer_get_data_len (vlib_buffer_t * b)
68{
69 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010070 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
71 (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020072}
73
74typedef struct
75{
76 // hash table key
77 ip6_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +020078 // time when last packet was received
79 f64 last_heard;
80 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +010081 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +020082 // buffer index of first buffer in this reassembly context
83 u32 first_bi;
84 // last octet of packet, ~0 until fragment without more_fragments arrives
85 u32 last_packet_octet;
86 // length of data collected so far
87 u32 data_len;
88 // trace operation counter
89 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +010090 // next index - used by non-feature node
91 u8 next_index;
92 // minimum fragment length for this reassembly - used to estimate MTU
93 u16 min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +020094} ip6_reass_t;
95
96typedef struct
97{
Klement Sekera4c533132018-02-22 11:41:12 +010098 ip6_reass_t *pool;
99 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100100 u32 id_counter;
101 clib_spinlock_t lock;
102} ip6_reass_per_thread_t;
103
104typedef struct
105{
Klement Sekera75e7d132017-09-20 08:26:30 +0200106 // IPv6 config
107 u32 timeout_ms;
108 f64 timeout;
109 u32 expire_walk_interval_ms;
110 u32 max_reass_n;
111
112 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200113 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100114
115 // per-thread data
116 ip6_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200117
118 // convenience
119 vlib_main_t *vlib_main;
120 vnet_main_t *vnet_main;
121
122 // node index of ip6-drop node
123 u32 ip6_drop_idx;
124 u32 ip6_icmp_error_idx;
125 u32 ip6_reass_expire_node_idx;
126
127} ip6_reass_main_t;
128
129ip6_reass_main_t ip6_reass_main;
130
131typedef enum
132{
133 IP6_REASSEMBLY_NEXT_INPUT,
134 IP6_REASSEMBLY_NEXT_DROP,
135 IP6_REASSEMBLY_NEXT_ICMP_ERROR,
136 IP6_REASSEMBLY_N_NEXT,
137} ip6_reass_next_t;
138
139typedef enum
140{
141 RANGE_NEW,
142 RANGE_OVERLAP,
143 ICMP_ERROR_RT_EXCEEDED,
144 ICMP_ERROR_FL_TOO_BIG,
145 ICMP_ERROR_FL_NOT_MULT_8,
146 FINALIZE,
147} ip6_reass_trace_operation_e;
148
149typedef struct
150{
151 u16 range_first;
152 u16 range_last;
153 u32 range_bi;
154 i32 data_offset;
155 u32 data_len;
156 u32 first_bi;
157} ip6_reass_range_trace_t;
158
159typedef struct
160{
161 ip6_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200162 u32 reass_id;
163 ip6_reass_range_trace_t trace_range;
164 u32 size_diff;
165 u32 op_id;
166 u32 fragment_first;
167 u32 fragment_last;
168 u32 total_data_len;
169} ip6_reass_trace_t;
170
171static void
172ip6_reass_trace_details (vlib_main_t * vm, u32 bi,
173 ip6_reass_range_trace_t * trace)
174{
175 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
176 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
177 trace->range_first = vnb->ip.reass.range_first;
178 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100179 trace->data_offset = ip6_reass_buffer_get_data_offset (b);
180 trace->data_len = ip6_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200181 trace->range_bi = bi;
182}
183
Klement Sekera4c533132018-02-22 11:41:12 +0100184static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200185format_ip6_reass_range_trace (u8 * s, va_list * args)
186{
187 ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *);
188 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
189 trace->range_last, trace->data_offset, trace->data_len,
190 trace->range_bi);
191 return s;
192}
193
Klement Sekera4c533132018-02-22 11:41:12 +0100194static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200195format_ip6_reass_trace (u8 * s, va_list * args)
196{
197 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
198 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
199 ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
200 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
201 u32 indent = format_get_indent (s);
202 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
203 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
204 t->fragment_last);
205 switch (t->action)
206 {
207 case RANGE_NEW:
208 s = format (s, "\n%Unew %U", format_white_space, indent,
209 format_ip6_reass_range_trace, &t->trace_range);
210 break;
211 case RANGE_OVERLAP:
212 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
213 format_ip6_reass_range_trace, &t->trace_range);
214 break;
215 case ICMP_ERROR_FL_TOO_BIG:
216 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
217 format_white_space, indent, format_ip6_reass_range_trace,
218 &t->trace_range);
219 break;
220 case ICMP_ERROR_FL_NOT_MULT_8:
221 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
222 format_white_space, indent, format_ip6_reass_range_trace,
223 &t->trace_range);
224 break;
225 case ICMP_ERROR_RT_EXCEEDED:
226 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
227 format_white_space, indent);
228 break;
229 case FINALIZE:
230 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
231 break;
232 }
233 return s;
234}
235
236static void
237ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
238 ip6_reass_main_t * rm, ip6_reass_t * reass,
239 u32 bi, ip6_reass_trace_operation_e action,
240 u32 size_diff)
241{
242 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
243 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100244 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
245 {
246 // this buffer's trace is gone
247 b->flags &= ~VLIB_BUFFER_IS_TRACED;
248 return;
249 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200250 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200251 t->reass_id = reass->id;
252 t->action = action;
253 ip6_reass_trace_details (vm, bi, &t->trace_range);
254 t->size_diff = size_diff;
255 t->op_id = reass->trace_op_counter;
256 ++reass->trace_op_counter;
257 t->fragment_first = vnb->ip.reass.fragment_first;
258 t->fragment_last = vnb->ip.reass.fragment_last;
259 t->trace_range.first_bi = reass->first_bi;
260 t->total_data_len = reass->data_len;
261#if 0
262 static u8 *s = NULL;
263 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
264 printf ("%.*s\n", vec_len (s), s);
265 fflush (stdout);
266 vec_reset_length (s);
267#endif
268}
269
Klement Sekera4c533132018-02-22 11:41:12 +0100270always_inline void
271ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
272 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200273{
274 clib_bihash_kv_48_8_t kv;
275 kv.key[0] = reass->key.as_u64[0];
276 kv.key[1] = reass->key.as_u64[1];
277 kv.key[2] = reass->key.as_u64[2];
278 kv.key[3] = reass->key.as_u64[3];
279 kv.key[4] = reass->key.as_u64[4];
280 kv.key[5] = reass->key.as_u64[5];
281 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100282 pool_put (rt->pool, reass);
283 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200284}
285
Klement Sekera4c533132018-02-22 11:41:12 +0100286always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200287ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100288 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200289{
290 u32 range_bi = reass->first_bi;
291 vlib_buffer_t *range_b;
292 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100293 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200294 while (~0 != range_bi)
295 {
296 range_b = vlib_get_buffer (vm, range_bi);
297 range_vnb = vnet_buffer (range_b);
298 u32 bi = range_bi;
299 while (~0 != bi)
300 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100301 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200302 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
303 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
304 {
305 bi = b->next_buffer;
306 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
307 }
308 else
309 {
310 bi = ~0;
311 }
312 }
313 range_bi = range_vnb->ip.reass.next_range_bi;
314 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100315 vlib_buffer_free (vm, to_free, vec_len (to_free));
316 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200317}
318
Klement Sekera4c533132018-02-22 11:41:12 +0100319always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200320ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100321 ip6_reass_main_t * rm, ip6_reass_t * reass,
322 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200323{
324 if (~0 == reass->first_bi)
325 {
326 return;
327 }
328 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
329 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
330 {
331 *icmp_bi = reass->first_bi;
332 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
333 {
334 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
335 ICMP_ERROR_RT_EXCEEDED, 0);
336 }
337 // fragment with offset zero received - send icmp message back
338 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
339 {
340 // separate first buffer from chain and steer it towards icmp node
341 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
342 reass->first_bi = b->next_buffer;
343 }
344 else
345 {
346 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
347 }
348 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
349 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
350 0);
351 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100352 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200353}
354
Klement Sekera4c533132018-02-22 11:41:12 +0100355always_inline ip6_reass_t *
356ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
357 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100358 ip6_reass_key_t * k, u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200359{
360 ip6_reass_t *reass = NULL;
361 f64 now = vlib_time_now (rm->vlib_main);
362 clib_bihash_kv_48_8_t kv, value;
363 kv.key[0] = k->as_u64[0];
364 kv.key[1] = k->as_u64[1];
365 kv.key[2] = k->as_u64[2];
366 kv.key[3] = k->as_u64[3];
367 kv.key[4] = k->as_u64[4];
368 kv.key[5] = k->as_u64[5];
369
370 if (!clib_bihash_search_48_8 (&rm->hash, &kv, &value))
371 {
Klement Sekera4c533132018-02-22 11:41:12 +0100372 reass = pool_elt_at_index (rt->pool, value.value);
Klement Sekera75e7d132017-09-20 08:26:30 +0200373 if (now > reass->last_heard + rm->timeout)
374 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100375 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100376 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200377 reass = NULL;
378 }
379 }
380
381 if (reass)
382 {
383 reass->last_heard = now;
384 return reass;
385 }
386
Klement Sekera4c533132018-02-22 11:41:12 +0100387 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200388 {
389 reass = NULL;
390 return reass;
391 }
392 else
393 {
Klement Sekera4c533132018-02-22 11:41:12 +0100394 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400395 clib_memset (reass, 0, sizeof (*reass));
Klement Sekera4c533132018-02-22 11:41:12 +0100396 reass->id =
397 ((u64) os_get_thread_index () * 1000000000) + rt->id_counter;
398 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200399 reass->first_bi = ~0;
400 reass->last_packet_octet = ~0;
401 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100402 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200403 }
404
405 reass->key.as_u64[0] = kv.key[0] = k->as_u64[0];
406 reass->key.as_u64[1] = kv.key[1] = k->as_u64[1];
407 reass->key.as_u64[2] = kv.key[2] = k->as_u64[2];
408 reass->key.as_u64[3] = kv.key[3] = k->as_u64[3];
409 reass->key.as_u64[4] = kv.key[4] = k->as_u64[4];
410 reass->key.as_u64[5] = kv.key[5] = k->as_u64[5];
Klement Sekera4c533132018-02-22 11:41:12 +0100411 kv.value = reass - rt->pool;
Klement Sekera75e7d132017-09-20 08:26:30 +0200412 reass->last_heard = now;
413
414 if (clib_bihash_add_del_48_8 (&rm->hash, &kv, 1))
415 {
Klement Sekera4c533132018-02-22 11:41:12 +0100416 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200417 reass = NULL;
418 }
419
420 return reass;
421}
422
Klement Sekerad0f70a32018-12-14 17:24:13 +0100423always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200424ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100425 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100426 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
427 bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200428{
Klement Sekera75e7d132017-09-20 08:26:30 +0200429 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200430 *error0 = IP6_ERROR_NONE;
431 ip6_frag_hdr_t *frag_hdr;
432 vlib_buffer_t *last_b = NULL;
433 u32 sub_chain_bi = reass->first_bi;
434 u32 total_length = 0;
435 u32 buf_cnt = 0;
436 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100437 u32 *vec_drop_compress = NULL;
438 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200439 do
440 {
441 u32 tmp_bi = sub_chain_bi;
442 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100443 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
444 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
445 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
446 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100447 rv = IP6_REASS_RC_INTERNAL_ERROR;
448 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100449 }
450
Klement Sekera75e7d132017-09-20 08:26:30 +0200451 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
452 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
453 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
454 u32 trim_end =
455 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
456 if (tmp_bi == reass->first_bi)
457 {
458 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100459 if (0 != ip6_reass_buffer_get_data_offset (tmp))
460 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100461 rv = IP6_REASS_RC_INTERNAL_ERROR;
462 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100463 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200464 trim_front = 0;
465 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
466 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
467 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100468 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
469 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100470 rv = IP6_REASS_RC_INTERNAL_ERROR;
471 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100472 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200473 }
474 u32 keep_data =
475 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
476 while (1)
477 {
478 ++buf_cnt;
479 if (trim_front)
480 {
481 if (trim_front > tmp->current_length)
482 {
483 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100484 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200485 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100486 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
487 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100488 rv = IP6_REASS_RC_INTERNAL_ERROR;
489 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100490 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200491 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
492 tmp_bi = tmp->next_buffer;
493 tmp = vlib_get_buffer (vm, tmp_bi);
494 continue;
495 }
496 else
497 {
498 vlib_buffer_advance (tmp, trim_front);
499 trim_front = 0;
500 }
501 }
502 if (keep_data)
503 {
504 if (last_b)
505 {
506 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
507 last_b->next_buffer = tmp_bi;
508 }
509 last_b = tmp;
510 if (keep_data <= tmp->current_length)
511 {
512 tmp->current_length = keep_data;
513 keep_data = 0;
514 }
515 else
516 {
517 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100518 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
519 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100520 rv = IP6_REASS_RC_INTERNAL_ERROR;
521 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100522 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200523 }
524 total_length += tmp->current_length;
525 }
526 else
527 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100528 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100529 if (reass->first_bi == tmp_bi)
530 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100531 rv = IP6_REASS_RC_INTERNAL_ERROR;
532 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100533 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200534 ++dropped_cnt;
535 }
536 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
537 {
538 tmp_bi = tmp->next_buffer;
539 tmp = vlib_get_buffer (vm, tmp->next_buffer);
540 }
541 else
542 {
543 break;
544 }
545 }
546 sub_chain_bi =
547 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
548 reass.next_range_bi;
549 }
550 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700551
Klement Sekerad0f70a32018-12-14 17:24:13 +0100552 if (!last_b)
553 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100554 rv = IP6_REASS_RC_INTERNAL_ERROR;
555 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100556 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200557 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
558 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100559 if (total_length < first_b->current_length)
560 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100561 rv = IP6_REASS_RC_INTERNAL_ERROR;
562 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100563 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200564 total_length -= first_b->current_length;
565 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
566 first_b->total_length_not_including_first_buffer = total_length;
567 // drop fragment header
568 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
569 ip6_header_t *ip = vlib_buffer_get_current (first_b);
570 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
571 ip6_ext_header_t *prev_hdr;
572 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
573 IP_PROTOCOL_IPV6_FRAGMENTATION);
574 if (prev_hdr)
575 {
576 prev_hdr->next_hdr = frag_hdr->next_hdr;
577 }
578 else
579 {
580 ip->protocol = frag_hdr->next_hdr;
581 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100582 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
583 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100584 rv = IP6_REASS_RC_INTERNAL_ERROR;
585 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100586 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200587 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
588 first_b->current_length - ip6_frag_hdr_offset -
589 sizeof (ip6_frag_hdr_t));
590 first_b->current_length -= sizeof (*frag_hdr);
591 ip->payload_length =
592 clib_host_to_net_u16 (total_length + first_b->current_length -
593 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100594 if (!vlib_buffer_chain_linearize (vm, first_b))
595 {
596 rv = IP6_REASS_RC_NO_BUF;
597 goto free_buffers_and_return;
598 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200599 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
600 {
601 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
602#if 0
603 // following code does a hexdump of packet fragments to stdout ...
604 do
605 {
606 u32 bi = reass->first_bi;
607 u8 *s = NULL;
608 while (~0 != bi)
609 {
610 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
611 s = format (s, "%u: %U\n", bi, format_hexdump,
612 vlib_buffer_get_current (b), b->current_length);
613 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
614 {
615 bi = b->next_buffer;
616 }
617 else
618 {
619 break;
620 }
621 }
622 printf ("%.*s\n", vec_len (s), s);
623 fflush (stdout);
624 vec_free (s);
625 }
626 while (0);
627#endif
628 }
Klement Sekera4c533132018-02-22 11:41:12 +0100629 if (is_feature)
630 {
631 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
632 }
633 else
634 {
635 *next0 = reass->next_index;
636 }
637 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
638 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200639 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100640free_buffers_and_return:
641 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
642 vec_free (vec_drop_compress);
643 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200644}
645
Klement Sekera4c533132018-02-22 11:41:12 +0100646always_inline void
647ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
648 ip6_reass_per_thread_t * rt,
649 ip6_reass_t * reass, u32 prev_range_bi,
650 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200651{
652
653 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
654 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
655 if (~0 != prev_range_bi)
656 {
657 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
658 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
659 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
660 prev_vnb->ip.reass.next_range_bi = new_next_bi;
661 }
662 else
663 {
664 if (~0 != reass->first_bi)
665 {
666 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
667 }
668 reass->first_bi = new_next_bi;
669 }
670 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200671}
672
Klement Sekerad0f70a32018-12-14 17:24:13 +0100673always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200674ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100675 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100676 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
677 ip6_frag_hdr_t * frag_hdr, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200678{
679 int consumed = 0;
680 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
681 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700682 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200683 fvnb->ip.reass.ip6_frag_hdr_offset =
684 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
685 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100686 if (fb->current_length < sizeof (*fip) ||
687 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
688 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
689 {
690 return IP6_REASS_RC_INTERNAL_ERROR;
691 }
692
Klement Sekera75e7d132017-09-20 08:26:30 +0200693 u32 fragment_first = fvnb->ip.reass.fragment_first =
694 ip6_frag_hdr_offset_bytes (frag_hdr);
695 u32 fragment_length =
696 vlib_buffer_length_in_chain (vm, fb) -
697 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
698 u32 fragment_last = fvnb->ip.reass.fragment_last =
699 fragment_first + fragment_length - 1;
700 int more_fragments = ip6_frag_hdr_more (frag_hdr);
701 u32 candidate_range_bi = reass->first_bi;
702 u32 prev_range_bi = ~0;
703 fvnb->ip.reass.range_first = fragment_first;
704 fvnb->ip.reass.range_last = fragment_last;
705 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200706 if (!more_fragments)
707 {
708 reass->last_packet_octet = fragment_last;
709 }
710 if (~0 == reass->first_bi)
711 {
712 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100713 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
714 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100715 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100716 consumed = 1;
717 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200718 }
Klement Sekera4c533132018-02-22 11:41:12 +0100719 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200720 clib_min (clib_net_to_host_u16 (fip->payload_length),
721 fvnb->ip.reass.estimated_mtu);
722 while (~0 != candidate_range_bi)
723 {
724 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
725 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
726 if (fragment_first > candidate_vnb->ip.reass.range_last)
727 {
728 // this fragments starts after candidate range
729 prev_range_bi = candidate_range_bi;
730 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
731 if (candidate_vnb->ip.reass.range_last < fragment_last &&
732 ~0 == candidate_range_bi)
733 {
734 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100735 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
736 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200737 consumed = 1;
738 break;
739 }
740 continue;
741 }
742 if (fragment_last < candidate_vnb->ip.reass.range_first)
743 {
744 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100745 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200746 *bi0);
747 consumed = 1;
748 }
749 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
750 fragment_last == candidate_vnb->ip.reass.range_last)
751 {
752 // duplicate fragment - ignore
753 }
754 else
755 {
756 // overlapping fragment - not allowed by RFC 8200
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100757 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100758 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200759 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
760 {
761 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
762 0);
763 }
Klement Sekera4c533132018-02-22 11:41:12 +0100764 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200765 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100766 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200767 }
768 break;
769 }
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100770check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200771 if (consumed)
772 {
773 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
774 {
775 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
776 }
777 }
778 if (~0 != reass->last_packet_octet &&
779 reass->data_len == reass->last_packet_octet + 1)
780 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100781 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100782 is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200783 }
784 else
785 {
786 if (consumed)
787 {
788 *bi0 = ~0;
789 }
790 else
791 {
Klement Sekera4c533132018-02-22 11:41:12 +0100792 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200793 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
794 }
795 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100796 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200797}
798
Klement Sekera4c533132018-02-22 11:41:12 +0100799always_inline bool
800ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
801 vlib_buffer_t * b,
802 ip6_frag_hdr_t * frag_hdr)
803{
804 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
805 while (ip6_ext_hdr (tmp->next_hdr))
806 {
807 tmp = ip6_ext_next_header (tmp);
808 }
809 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
810 {
811 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
812 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
813 0);
814 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
815
816 return false;
817 }
818 return true;
819}
820
821always_inline bool
822ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
823 vlib_node_runtime_t * node,
824 vlib_buffer_t * b,
825 ip6_frag_hdr_t * frag_hdr)
826{
827 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
828 ip6_header_t *ip = vlib_buffer_get_current (b);
829 int more_fragments = ip6_frag_hdr_more (frag_hdr);
830 u32 fragment_length =
831 vlib_buffer_length_in_chain (vm, b) -
832 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
833 if (more_fragments && 0 != fragment_length % 8)
834 {
835 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
836 ICMP6_parameter_problem_erroneous_header_field,
837 (u8 *) & ip->payload_length - (u8 *) ip);
838 return false;
839 }
840 return true;
841}
842
843always_inline bool
844ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
845 vlib_node_runtime_t * node,
846 vlib_buffer_t * b,
847 ip6_frag_hdr_t * frag_hdr)
848{
849 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
850 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
851 u32 fragment_length =
852 vlib_buffer_length_in_chain (vm, b) -
853 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
854 if (fragment_first + fragment_length > 65535)
855 {
856 ip6_header_t *ip0 = vlib_buffer_get_current (b);
857 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
858 ICMP6_parameter_problem_erroneous_header_field,
859 (u8 *) & frag_hdr->fragment_offset_and_more
860 - (u8 *) ip0);
861 return false;
862 }
863 return true;
864}
865
Klement Sekera75e7d132017-09-20 08:26:30 +0200866always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100867ip6_reassembly_inline (vlib_main_t * vm,
868 vlib_node_runtime_t * node,
869 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200870{
871 u32 *from = vlib_frame_vector_args (frame);
872 u32 n_left_from, n_left_to_next, *to_next, next_index;
873 ip6_reass_main_t *rm = &ip6_reass_main;
Klement Sekera4c533132018-02-22 11:41:12 +0100874 ip6_reass_per_thread_t *rt = &rm->per_thread_data[os_get_thread_index ()];
875 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200876
877 n_left_from = frame->n_vectors;
878 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100879 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200880 {
881 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
882
Klement Sekera75e7d132017-09-20 08:26:30 +0200883 while (n_left_from > 0 && n_left_to_next > 0)
884 {
885 u32 bi0;
886 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100887 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100888 u32 error0 = IP6_ERROR_NONE;
889 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200890
891 bi0 = from[0];
892 b0 = vlib_get_buffer (vm, bi0);
893
894 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200895 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200896 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200897 if (ip6_ext_hdr (ip0->protocol))
898 {
899 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
900 IP_PROTOCOL_IPV6_FRAGMENTATION);
901 }
Klement Sekera4c533132018-02-22 11:41:12 +0100902 if (!frag_hdr)
903 {
904 // this is a regular packet - no fragmentation
905 next0 = IP6_REASSEMBLY_NEXT_INPUT;
906 goto skip_reass;
907 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200908 if (0 == ip6_frag_hdr_offset (frag_hdr))
909 {
910 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100911 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +0200912 {
Klement Sekera4c533132018-02-22 11:41:12 +0100913 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
914 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200915 }
Klement Sekera4c533132018-02-22 11:41:12 +0100916 }
917 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
918 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
919 frag_hdr))
920 {
921 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
922 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200923 }
924 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
925 (u8 *) frag_hdr - (u8 *) ip0;
926
927 ip6_reass_key_t k;
Klement Sekera8dcfed52018-06-28 11:16:15 +0200928 k.as_u64[0] = ip0->src_address.as_u64[0];
929 k.as_u64[1] = ip0->src_address.as_u64[1];
930 k.as_u64[2] = ip0->dst_address.as_u64[0];
931 k.as_u64[3] = ip0->dst_address.as_u64[1];
932 k.as_u64[4] =
933 (u64) vnet_buffer (b0)->
934 sw_if_index[VLIB_RX] << 32 | frag_hdr->identification;
935 k.as_u64[5] = ip0->protocol;
Klement Sekera75e7d132017-09-20 08:26:30 +0200936 ip6_reass_t *reass =
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100937 ip6_reass_find_or_create (vm, node, rm, rt, &k, &icmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200938
Klement Sekera75e7d132017-09-20 08:26:30 +0200939 if (reass)
940 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100941 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100942 &error0, frag_hdr, is_feature))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100943 {
944 case IP6_REASS_RC_OK:
945 /* nothing to do here */
946 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100947 case IP6_REASS_RC_NO_BUF:
948 /* fallthrough */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100949 case IP6_REASS_RC_INTERNAL_ERROR:
950 /* drop everything and start with a clean slate */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100951 ip6_reass_drop_all (vm, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100952 ip6_reass_free (rm, rt, reass);
953 goto next_packet;
954 break;
955 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200956 }
957 else
958 {
959 next0 = IP6_REASSEMBLY_NEXT_DROP;
960 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
961 }
962
963 b0->error = node->errors[error0];
964
965 if (~0 != bi0)
966 {
Klement Sekera4c533132018-02-22 11:41:12 +0100967 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +0200968 to_next[0] = bi0;
969 to_next += 1;
970 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +0100971 if (is_feature && IP6_ERROR_NONE == error0)
972 {
Kingwel Xiea0060652018-09-26 04:59:52 -0400973 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +0200974 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +0100975 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200976 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
977 n_left_to_next, bi0, next0);
978 }
979
980 if (~0 != icmp_bi)
981 {
982 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
983 to_next[0] = icmp_bi;
984 to_next += 1;
985 n_left_to_next -= 1;
986 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
987 n_left_to_next, icmp_bi,
988 next0);
989 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100990 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +0200991 from += 1;
992 n_left_from -= 1;
993 }
994
995 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
996 }
997
Klement Sekera4c533132018-02-22 11:41:12 +0100998 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200999 return frame->n_vectors;
1000}
1001
1002static char *ip6_reassembly_error_strings[] = {
1003#define _(sym, string) string,
1004 foreach_ip6_error
1005#undef _
1006};
1007
Damjan Marion2574d9f2018-06-26 14:45:49 +02001008static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001009ip6_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node,
1010 vlib_frame_t * frame)
1011{
1012 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1013}
1014
Klement Sekera75e7d132017-09-20 08:26:30 +02001015/* *INDENT-OFF* */
1016VLIB_REGISTER_NODE (ip6_reass_node, static) = {
1017 .function = ip6_reassembly,
1018 .name = "ip6-reassembly",
1019 .vector_size = sizeof (u32),
1020 .format_trace = format_ip6_reass_trace,
1021 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1022 .error_strings = ip6_reassembly_error_strings,
1023 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1024 .next_nodes =
1025 {
1026 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1027 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1028 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1029 },
1030};
1031/* *INDENT-ON* */
1032
Klement Sekera4c533132018-02-22 11:41:12 +01001033VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node, ip6_reassembly);
1034
Damjan Marion2574d9f2018-06-26 14:45:49 +02001035static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001036ip6_reassembly_feature (vlib_main_t * vm,
1037 vlib_node_runtime_t * node, vlib_frame_t * frame)
1038{
1039 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1040}
1041
1042/* *INDENT-OFF* */
1043VLIB_REGISTER_NODE (ip6_reass_node_feature, static) = {
1044 .function = ip6_reassembly_feature,
1045 .name = "ip6-reassembly-feature",
1046 .vector_size = sizeof (u32),
1047 .format_trace = format_ip6_reass_trace,
1048 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1049 .error_strings = ip6_reassembly_error_strings,
1050 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1051 .next_nodes =
1052 {
1053 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1054 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1055 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1056 },
1057};
1058/* *INDENT-ON* */
1059
1060VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node_feature, ip6_reassembly_feature);
1061
1062/* *INDENT-OFF* */
1063VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1064 .arc_name = "ip6-unicast",
1065 .node_name = "ip6-reassembly-feature",
1066 .runs_before = VNET_FEATURES ("ip6-lookup"),
1067 .runs_after = 0,
1068};
1069/* *INDENT-ON* */
1070
1071static u32
1072ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001073{
1074 ip6_reass_main_t *rm = &ip6_reass_main;
1075 u32 nbuckets;
1076 u8 i;
1077
1078 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1079
1080 for (i = 0; i < 31; i++)
1081 if ((1 << i) >= nbuckets)
1082 break;
1083 nbuckets = 1 << i;
1084
1085 return nbuckets;
1086}
1087
1088typedef enum
1089{
1090 IP6_EVENT_CONFIG_CHANGED = 1,
1091} ip6_reass_event_t;
1092
1093typedef struct
1094{
1095 int failure;
1096 clib_bihash_48_8_t *new_hash;
1097} ip6_rehash_cb_ctx;
1098
Klement Sekera4c533132018-02-22 11:41:12 +01001099static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001100ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1101{
1102 ip6_rehash_cb_ctx *ctx = _ctx;
1103 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1104 {
1105 ctx->failure = 1;
1106 }
1107}
1108
Klement Sekera4c533132018-02-22 11:41:12 +01001109static void
1110ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1111 u32 expire_walk_interval_ms)
1112{
1113 ip6_reass_main.timeout_ms = timeout_ms;
1114 ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1115 ip6_reass_main.max_reass_n = max_reassemblies;
1116 ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1117}
1118
Klement Sekera75e7d132017-09-20 08:26:30 +02001119vnet_api_error_t
1120ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
1121 u32 expire_walk_interval_ms)
1122{
1123 u32 old_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera4c533132018-02-22 11:41:12 +01001124 ip6_reass_set_params (timeout_ms, max_reassemblies,
1125 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001126 vlib_process_signal_event (ip6_reass_main.vlib_main,
1127 ip6_reass_main.ip6_reass_expire_node_idx,
1128 IP6_EVENT_CONFIG_CHANGED, 0);
1129 u32 new_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001130 if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001131 {
1132 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001133 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001134 ip6_rehash_cb_ctx ctx;
1135 ctx.failure = 0;
1136 ctx.new_hash = &new_hash;
1137 clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1138 new_nbuckets * 1024);
1139 clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1140 ip6_rehash_cb, &ctx);
1141 if (ctx.failure)
1142 {
1143 clib_bihash_free_48_8 (&new_hash);
1144 return -1;
1145 }
1146 else
1147 {
1148 clib_bihash_free_48_8 (&ip6_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001149 clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1150 sizeof (ip6_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001151 }
1152 }
1153 return 0;
1154}
1155
1156vnet_api_error_t
1157ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1158 u32 * expire_walk_interval_ms)
1159{
1160 *timeout_ms = ip6_reass_main.timeout_ms;
1161 *max_reassemblies = ip6_reass_main.max_reass_n;
1162 *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1163 return 0;
1164}
1165
Klement Sekera4c533132018-02-22 11:41:12 +01001166static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001167ip6_reass_init_function (vlib_main_t * vm)
1168{
1169 ip6_reass_main_t *rm = &ip6_reass_main;
1170 clib_error_t *error = 0;
1171 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001172 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001173
1174 rm->vlib_main = vm;
1175 rm->vnet_main = vnet_get_main ();
1176
Juraj Slobodacd806922018-10-10 10:15:54 +02001177 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001178 ip6_reass_per_thread_t *rt;
1179 vec_foreach (rt, rm->per_thread_data)
1180 {
1181 clib_spinlock_init (&rt->lock);
1182 pool_alloc (rt->pool, rm->max_reass_n);
1183 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001184
1185 node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1186 ASSERT (node);
1187 rm->ip6_reass_expire_node_idx = node->index;
1188
Klement Sekera3ecc2212018-03-27 10:34:43 +02001189 ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS,
1190 IP6_REASS_MAX_REASSEMBLIES_DEFAULT,
1191 IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1192
Klement Sekera75e7d132017-09-20 08:26:30 +02001193 nbuckets = ip6_reass_get_nbuckets ();
1194 clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1195
Dave Barach1403fcd2018-02-05 09:45:43 -05001196 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001197 ASSERT (node);
1198 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001199 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1200 ASSERT (node);
1201 rm->ip6_icmp_error_idx = node->index;
1202
1203 if ((error = vlib_call_init_function (vm, ip_main_init)))
1204 return error;
1205 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1206 ip6_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001207
Klement Sekera75e7d132017-09-20 08:26:30 +02001208 return error;
1209}
1210
1211VLIB_INIT_FUNCTION (ip6_reass_init_function);
1212
1213static uword
1214ip6_reass_walk_expired (vlib_main_t * vm,
1215 vlib_node_runtime_t * node, vlib_frame_t * f)
1216{
1217 ip6_reass_main_t *rm = &ip6_reass_main;
1218 uword event_type, *event_data = 0;
1219
1220 while (true)
1221 {
1222 vlib_process_wait_for_event_or_clock (vm,
1223 (f64) rm->expire_walk_interval_ms
1224 / (f64) MSEC_PER_SEC);
1225 event_type = vlib_process_get_events (vm, &event_data);
1226
1227 switch (event_type)
1228 {
1229 case ~0: /* no events => timeout */
1230 /* nothing to do here */
1231 break;
1232 case IP6_EVENT_CONFIG_CHANGED:
1233 break;
1234 default:
1235 clib_warning ("BUG: event type 0x%wx", event_type);
1236 break;
1237 }
1238 f64 now = vlib_time_now (vm);
1239
1240 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001241 int *pool_indexes_to_free = NULL;
1242
Klement Sekera4c533132018-02-22 11:41:12 +01001243 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001244 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001245 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001246 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001247 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1248 {
1249 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1250 clib_spinlock_lock (&rt->lock);
1251
1252 vec_reset_length (pool_indexes_to_free);
1253 /* *INDENT-OFF* */
1254 pool_foreach_index (index, rt->pool, ({
1255 reass = pool_elt_at_index (rt->pool, index);
1256 if (now > reass->last_heard + rm->timeout)
1257 {
1258 vec_add1 (pool_indexes_to_free, index);
1259 }
1260 }));
1261 /* *INDENT-ON* */
1262 int *i;
1263 /* *INDENT-OFF* */
1264 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001265 {
Klement Sekera4c533132018-02-22 11:41:12 +01001266 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1267 u32 icmp_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +01001268 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1269 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1270 {
1271 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1272 b->trace_index))
1273 {
1274 /* the trace is gone, don't trace this buffer anymore */
1275 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1276 }
1277 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001278 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001279 if (~0 != icmp_bi)
1280 {
1281 vec_add1 (vec_icmp_bi, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001282 }
1283 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001284 }
Klement Sekera4c533132018-02-22 11:41:12 +01001285 /* *INDENT-ON* */
1286
1287 clib_spinlock_unlock (&rt->lock);
1288 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001289
Klement Sekera75e7d132017-09-20 08:26:30 +02001290 while (vec_len (vec_icmp_bi) > 0)
1291 {
1292 vlib_frame_t *f =
1293 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1294 u32 *to_next = vlib_frame_vector_args (f);
1295 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001296 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001297 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1298 {
1299 u32 bi = vec_pop (vec_icmp_bi);
1300 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1301 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1302 {
1303 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1304 b->trace_index))
1305 {
1306 /* the trace is gone, don't trace this buffer anymore */
1307 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1308 }
1309 else
1310 {
Klement Sekera4c533132018-02-22 11:41:12 +01001311 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001312 }
1313 }
1314 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1315 to_next[0] = bi;
1316 ++f->n_vectors;
1317 to_next += 1;
1318 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001319 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001320 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001321 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1322 }
1323
1324 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001325 vec_free (vec_icmp_bi);
1326 if (event_data)
1327 {
1328 _vec_len (event_data) = 0;
1329 }
1330 }
1331
1332 return 0;
1333}
1334
1335static vlib_node_registration_t ip6_reass_expire_node;
1336
1337/* *INDENT-OFF* */
1338VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1339 .function = ip6_reass_walk_expired,
1340 .format_trace = format_ip6_reass_trace,
1341 .type = VLIB_NODE_TYPE_PROCESS,
1342 .name = "ip6-reassembly-expire-walk",
1343
1344 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1345 .error_strings = ip6_reassembly_error_strings,
1346
1347};
1348/* *INDENT-ON* */
1349
1350static u8 *
1351format_ip6_reass_key (u8 * s, va_list * args)
1352{
1353 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1354 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1355 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1356 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1357 return s;
1358}
1359
1360static u8 *
1361format_ip6_reass (u8 * s, va_list * args)
1362{
1363 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1364 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1365
Klement Sekera4c533132018-02-22 11:41:12 +01001366 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001367 "last_packet_octet: %u, trace_op_counter: %u\n",
1368 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1369 reass->data_len, reass->last_packet_octet,
1370 reass->trace_op_counter);
1371 u32 bi = reass->first_bi;
1372 u32 counter = 0;
1373 while (~0 != bi)
1374 {
1375 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1376 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1377 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1378 "fragment[%u, %u]\n",
1379 counter, vnb->ip.reass.range_first,
1380 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001381 ip6_reass_buffer_get_data_offset (b),
1382 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001383 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1384 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1385 {
1386 bi = b->next_buffer;
1387 }
1388 else
1389 {
1390 bi = ~0;
1391 }
1392 }
1393 return s;
1394}
1395
1396static clib_error_t *
1397show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1398 CLIB_UNUSED (vlib_cli_command_t * lmd))
1399{
1400 ip6_reass_main_t *rm = &ip6_reass_main;
1401
1402 vlib_cli_output (vm, "---------------------");
1403 vlib_cli_output (vm, "IP6 reassembly status");
1404 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001405 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001406 if (unformat (input, "details"))
1407 {
Klement Sekera4c533132018-02-22 11:41:12 +01001408 details = true;
1409 }
1410
1411 u32 sum_reass_n = 0;
1412 u64 sum_buffers_n = 0;
1413 ip6_reass_t *reass;
1414 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001415 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001416 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1417 {
1418 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1419 clib_spinlock_lock (&rt->lock);
1420 if (details)
1421 {
1422 /* *INDENT-OFF* */
1423 pool_foreach (reass, rt->pool, {
1424 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1425 });
1426 /* *INDENT-ON* */
1427 }
1428 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001429 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001430 }
1431 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001432 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1433 (long unsigned) sum_reass_n);
1434 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1435 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001436 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001437 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001438 return 0;
1439}
1440
1441/* *INDENT-OFF* */
1442VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1443 .path = "show ip6-reassembly",
1444 .short_help = "show ip6-reassembly [details]",
1445 .function = show_ip6_reass,
1446};
1447/* *INDENT-ON* */
1448
Klement Sekera4c533132018-02-22 11:41:12 +01001449vnet_api_error_t
1450ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1451{
1452 return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1453 sw_if_index, enable_disable, 0, 0);
1454}
1455
Klement Sekera75e7d132017-09-20 08:26:30 +02001456/*
1457 * fd.io coding-style-patch-verification: ON
1458 *
1459 * Local Variables:
1460 * eval: (c-set-style "gnu")
1461 * End:
1462 */