blob: cb1cd9afeb4f1225bc3009201eba5f827e2fea95 [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 Sekera3a343d42019-05-16 14:35:46 +020033#define IP6_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
Klement Sekera75e7d132017-09-20 08:26:30 +020034#define IP6_REASS_HT_LOAD_FACTOR (0.75)
35
Klement Sekerad0f70a32018-12-14 17:24:13 +010036typedef enum
37{
38 IP6_REASS_RC_OK,
39 IP6_REASS_RC_INTERNAL_ERROR,
Klement Sekera3a343d42019-05-16 14:35:46 +020040 IP6_REASS_RC_TOO_MANY_FRAGMENTS,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010041 IP6_REASS_RC_NO_BUF,
Klement Sekerad0f70a32018-12-14 17:24:13 +010042} ip6_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020043
44typedef struct
45{
46 union
47 {
48 struct
49 {
50 ip6_address_t src;
51 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020052 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020053 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020054 u8 unused[7];
55 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020056 };
57 u64 as_u64[6];
58 };
59} ip6_reass_key_t;
60
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080061typedef union
62{
63 struct
64 {
65 u32 reass_index;
66 u32 thread_index;
67 };
68 u64 as_u64;
69} ip6_reass_val_t;
70
71typedef union
72{
73 struct
74 {
75 ip6_reass_key_t k;
76 ip6_reass_val_t v;
77 };
78 clib_bihash_kv_48_8_t kv;
79} ip6_reass_kv_t;
80
81
Klement Sekera75e7d132017-09-20 08:26:30 +020082always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +020083ip6_reass_buffer_get_data_offset (vlib_buffer_t * b)
84{
85 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010086 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020087}
88
89always_inline u16
90ip6_reass_buffer_get_data_len (vlib_buffer_t * b)
91{
92 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010093 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
94 (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020095}
96
97typedef struct
98{
99 // hash table key
100 ip6_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200101 // time when last packet was received
102 f64 last_heard;
103 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100104 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200105 // buffer index of first buffer in this reassembly context
106 u32 first_bi;
107 // last octet of packet, ~0 until fragment without more_fragments arrives
108 u32 last_packet_octet;
109 // length of data collected so far
110 u32 data_len;
111 // trace operation counter
112 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100113 // next index - used by non-feature node
114 u8 next_index;
115 // minimum fragment length for this reassembly - used to estimate MTU
116 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200117 // number of fragments for this reassembly
118 u32 fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200119} ip6_reass_t;
120
121typedef struct
122{
Klement Sekera4c533132018-02-22 11:41:12 +0100123 ip6_reass_t *pool;
124 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100125 u32 id_counter;
126 clib_spinlock_t lock;
127} ip6_reass_per_thread_t;
128
129typedef struct
130{
Klement Sekera75e7d132017-09-20 08:26:30 +0200131 // IPv6 config
132 u32 timeout_ms;
133 f64 timeout;
134 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200135 // maximum number of fragments in one reassembly
136 u32 max_reass_len;
137 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200138 u32 max_reass_n;
139
140 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200141 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100142
143 // per-thread data
144 ip6_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200145
146 // convenience
147 vlib_main_t *vlib_main;
148 vnet_main_t *vnet_main;
149
150 // node index of ip6-drop node
151 u32 ip6_drop_idx;
152 u32 ip6_icmp_error_idx;
153 u32 ip6_reass_expire_node_idx;
154
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800155 /** Worker handoff */
156 u32 fq_index;
157 u32 fq_feature_index;
158
Klement Sekera75e7d132017-09-20 08:26:30 +0200159} ip6_reass_main_t;
160
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700161extern ip6_reass_main_t ip6_reass_main;
162
163#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +0200164ip6_reass_main_t ip6_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700165#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200166
167typedef enum
168{
169 IP6_REASSEMBLY_NEXT_INPUT,
170 IP6_REASSEMBLY_NEXT_DROP,
171 IP6_REASSEMBLY_NEXT_ICMP_ERROR,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800172 IP6_REASSEMBLY_NEXT_HANDOFF,
Klement Sekera75e7d132017-09-20 08:26:30 +0200173 IP6_REASSEMBLY_N_NEXT,
174} ip6_reass_next_t;
175
176typedef enum
177{
178 RANGE_NEW,
179 RANGE_OVERLAP,
180 ICMP_ERROR_RT_EXCEEDED,
181 ICMP_ERROR_FL_TOO_BIG,
182 ICMP_ERROR_FL_NOT_MULT_8,
183 FINALIZE,
184} ip6_reass_trace_operation_e;
185
186typedef struct
187{
188 u16 range_first;
189 u16 range_last;
190 u32 range_bi;
191 i32 data_offset;
192 u32 data_len;
193 u32 first_bi;
194} ip6_reass_range_trace_t;
195
196typedef struct
197{
198 ip6_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200199 u32 reass_id;
200 ip6_reass_range_trace_t trace_range;
201 u32 size_diff;
202 u32 op_id;
203 u32 fragment_first;
204 u32 fragment_last;
205 u32 total_data_len;
206} ip6_reass_trace_t;
207
208static void
209ip6_reass_trace_details (vlib_main_t * vm, u32 bi,
210 ip6_reass_range_trace_t * trace)
211{
212 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
213 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
214 trace->range_first = vnb->ip.reass.range_first;
215 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100216 trace->data_offset = ip6_reass_buffer_get_data_offset (b);
217 trace->data_len = ip6_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200218 trace->range_bi = bi;
219}
220
Klement Sekera4c533132018-02-22 11:41:12 +0100221static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200222format_ip6_reass_range_trace (u8 * s, va_list * args)
223{
224 ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *);
225 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
226 trace->range_last, trace->data_offset, trace->data_len,
227 trace->range_bi);
228 return s;
229}
230
Klement Sekera4c533132018-02-22 11:41:12 +0100231static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200232format_ip6_reass_trace (u8 * s, va_list * args)
233{
234 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
235 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
236 ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
237 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
238 u32 indent = format_get_indent (s);
239 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
240 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
241 t->fragment_last);
242 switch (t->action)
243 {
244 case RANGE_NEW:
245 s = format (s, "\n%Unew %U", format_white_space, indent,
246 format_ip6_reass_range_trace, &t->trace_range);
247 break;
248 case RANGE_OVERLAP:
249 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
250 format_ip6_reass_range_trace, &t->trace_range);
251 break;
252 case ICMP_ERROR_FL_TOO_BIG:
253 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
254 format_white_space, indent, format_ip6_reass_range_trace,
255 &t->trace_range);
256 break;
257 case ICMP_ERROR_FL_NOT_MULT_8:
258 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
259 format_white_space, indent, format_ip6_reass_range_trace,
260 &t->trace_range);
261 break;
262 case ICMP_ERROR_RT_EXCEEDED:
263 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
264 format_white_space, indent);
265 break;
266 case FINALIZE:
267 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
268 break;
269 }
270 return s;
271}
272
273static void
274ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
275 ip6_reass_main_t * rm, ip6_reass_t * reass,
276 u32 bi, ip6_reass_trace_operation_e action,
277 u32 size_diff)
278{
279 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
280 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100281 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
282 {
283 // this buffer's trace is gone
284 b->flags &= ~VLIB_BUFFER_IS_TRACED;
285 return;
286 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200287 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200288 t->reass_id = reass->id;
289 t->action = action;
290 ip6_reass_trace_details (vm, bi, &t->trace_range);
291 t->size_diff = size_diff;
292 t->op_id = reass->trace_op_counter;
293 ++reass->trace_op_counter;
294 t->fragment_first = vnb->ip.reass.fragment_first;
295 t->fragment_last = vnb->ip.reass.fragment_last;
296 t->trace_range.first_bi = reass->first_bi;
297 t->total_data_len = reass->data_len;
298#if 0
299 static u8 *s = NULL;
300 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
301 printf ("%.*s\n", vec_len (s), s);
302 fflush (stdout);
303 vec_reset_length (s);
304#endif
305}
306
Klement Sekera4c533132018-02-22 11:41:12 +0100307always_inline void
308ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
309 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200310{
311 clib_bihash_kv_48_8_t kv;
312 kv.key[0] = reass->key.as_u64[0];
313 kv.key[1] = reass->key.as_u64[1];
314 kv.key[2] = reass->key.as_u64[2];
315 kv.key[3] = reass->key.as_u64[3];
316 kv.key[4] = reass->key.as_u64[4];
317 kv.key[5] = reass->key.as_u64[5];
318 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100319 pool_put (rt->pool, reass);
320 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200321}
322
Klement Sekera4c533132018-02-22 11:41:12 +0100323always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200324ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100325 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200326{
327 u32 range_bi = reass->first_bi;
328 vlib_buffer_t *range_b;
329 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100330 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200331 while (~0 != range_bi)
332 {
333 range_b = vlib_get_buffer (vm, range_bi);
334 range_vnb = vnet_buffer (range_b);
335 u32 bi = range_bi;
336 while (~0 != bi)
337 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100338 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200339 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
340 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
341 {
342 bi = b->next_buffer;
343 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
344 }
345 else
346 {
347 bi = ~0;
348 }
349 }
350 range_bi = range_vnb->ip.reass.next_range_bi;
351 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100352 vlib_buffer_free (vm, to_free, vec_len (to_free));
353 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200354}
355
Klement Sekera4c533132018-02-22 11:41:12 +0100356always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200357ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100358 ip6_reass_main_t * rm, ip6_reass_t * reass,
359 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200360{
361 if (~0 == reass->first_bi)
362 {
363 return;
364 }
365 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
366 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
367 {
368 *icmp_bi = reass->first_bi;
369 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
370 {
371 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
372 ICMP_ERROR_RT_EXCEEDED, 0);
373 }
374 // fragment with offset zero received - send icmp message back
375 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
376 {
377 // separate first buffer from chain and steer it towards icmp node
378 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
379 reass->first_bi = b->next_buffer;
380 }
381 else
382 {
383 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
384 }
385 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
386 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
387 0);
388 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100389 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200390}
391
Klement Sekera4c533132018-02-22 11:41:12 +0100392always_inline ip6_reass_t *
393ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
394 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800395 ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200396{
397 ip6_reass_t *reass = NULL;
398 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200399
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800400 if (!clib_bihash_search_48_8
401 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200402 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800403 if (vm->thread_index != kv->v.thread_index)
404 {
405 *do_handoff = 1;
406 return NULL;
407 }
408 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
409
Klement Sekera75e7d132017-09-20 08:26:30 +0200410 if (now > reass->last_heard + rm->timeout)
411 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100412 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100413 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200414 reass = NULL;
415 }
416 }
417
418 if (reass)
419 {
420 reass->last_heard = now;
421 return reass;
422 }
423
Klement Sekera4c533132018-02-22 11:41:12 +0100424 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200425 {
426 reass = NULL;
427 return reass;
428 }
429 else
430 {
Klement Sekera4c533132018-02-22 11:41:12 +0100431 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400432 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800433 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100434 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200435 reass->first_bi = ~0;
436 reass->last_packet_octet = ~0;
437 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100438 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200439 }
440
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800441 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
442 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
443 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
444 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
445 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
446 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
447 kv->v.reass_index = (reass - rt->pool);
448 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200449 reass->last_heard = now;
450
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800451 if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200452 {
Klement Sekera4c533132018-02-22 11:41:12 +0100453 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200454 reass = NULL;
455 }
456
457 return reass;
458}
459
Klement Sekerad0f70a32018-12-14 17:24:13 +0100460always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200461ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100462 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100463 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
464 bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200465{
Klement Sekera75e7d132017-09-20 08:26:30 +0200466 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200467 *error0 = IP6_ERROR_NONE;
468 ip6_frag_hdr_t *frag_hdr;
469 vlib_buffer_t *last_b = NULL;
470 u32 sub_chain_bi = reass->first_bi;
471 u32 total_length = 0;
472 u32 buf_cnt = 0;
473 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100474 u32 *vec_drop_compress = NULL;
475 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200476 do
477 {
478 u32 tmp_bi = sub_chain_bi;
479 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100480 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
481 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
482 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
483 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100484 rv = IP6_REASS_RC_INTERNAL_ERROR;
485 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100486 }
487
Klement Sekera75e7d132017-09-20 08:26:30 +0200488 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
489 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
490 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
491 u32 trim_end =
492 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
493 if (tmp_bi == reass->first_bi)
494 {
495 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100496 if (0 != ip6_reass_buffer_get_data_offset (tmp))
497 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100498 rv = IP6_REASS_RC_INTERNAL_ERROR;
499 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100500 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200501 trim_front = 0;
502 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
503 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
504 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100505 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
506 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100507 rv = IP6_REASS_RC_INTERNAL_ERROR;
508 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100509 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200510 }
511 u32 keep_data =
512 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
513 while (1)
514 {
515 ++buf_cnt;
516 if (trim_front)
517 {
518 if (trim_front > tmp->current_length)
519 {
520 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100521 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200522 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100523 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
524 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100525 rv = IP6_REASS_RC_INTERNAL_ERROR;
526 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100527 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200528 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
529 tmp_bi = tmp->next_buffer;
530 tmp = vlib_get_buffer (vm, tmp_bi);
531 continue;
532 }
533 else
534 {
535 vlib_buffer_advance (tmp, trim_front);
536 trim_front = 0;
537 }
538 }
539 if (keep_data)
540 {
541 if (last_b)
542 {
543 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
544 last_b->next_buffer = tmp_bi;
545 }
546 last_b = tmp;
547 if (keep_data <= tmp->current_length)
548 {
549 tmp->current_length = keep_data;
550 keep_data = 0;
551 }
552 else
553 {
554 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100555 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
556 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100557 rv = IP6_REASS_RC_INTERNAL_ERROR;
558 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100559 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200560 }
561 total_length += tmp->current_length;
562 }
563 else
564 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100565 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100566 if (reass->first_bi == tmp_bi)
567 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100568 rv = IP6_REASS_RC_INTERNAL_ERROR;
569 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100570 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200571 ++dropped_cnt;
572 }
573 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
574 {
575 tmp_bi = tmp->next_buffer;
576 tmp = vlib_get_buffer (vm, tmp->next_buffer);
577 }
578 else
579 {
580 break;
581 }
582 }
583 sub_chain_bi =
584 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
585 reass.next_range_bi;
586 }
587 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700588
Klement Sekerad0f70a32018-12-14 17:24:13 +0100589 if (!last_b)
590 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100591 rv = IP6_REASS_RC_INTERNAL_ERROR;
592 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100593 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200594 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
595 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100596 if (total_length < first_b->current_length)
597 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100598 rv = IP6_REASS_RC_INTERNAL_ERROR;
599 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100600 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200601 total_length -= first_b->current_length;
602 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
603 first_b->total_length_not_including_first_buffer = total_length;
604 // drop fragment header
605 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
606 ip6_header_t *ip = vlib_buffer_get_current (first_b);
607 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
608 ip6_ext_header_t *prev_hdr;
609 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
610 IP_PROTOCOL_IPV6_FRAGMENTATION);
611 if (prev_hdr)
612 {
613 prev_hdr->next_hdr = frag_hdr->next_hdr;
614 }
615 else
616 {
617 ip->protocol = frag_hdr->next_hdr;
618 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100619 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
620 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100621 rv = IP6_REASS_RC_INTERNAL_ERROR;
622 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100623 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200624 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
625 first_b->current_length - ip6_frag_hdr_offset -
626 sizeof (ip6_frag_hdr_t));
627 first_b->current_length -= sizeof (*frag_hdr);
628 ip->payload_length =
629 clib_host_to_net_u16 (total_length + first_b->current_length -
630 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100631 if (!vlib_buffer_chain_linearize (vm, first_b))
632 {
633 rv = IP6_REASS_RC_NO_BUF;
634 goto free_buffers_and_return;
635 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700636 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200637 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
638 {
639 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
640#if 0
641 // following code does a hexdump of packet fragments to stdout ...
642 do
643 {
644 u32 bi = reass->first_bi;
645 u8 *s = NULL;
646 while (~0 != bi)
647 {
648 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
649 s = format (s, "%u: %U\n", bi, format_hexdump,
650 vlib_buffer_get_current (b), b->current_length);
651 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
652 {
653 bi = b->next_buffer;
654 }
655 else
656 {
657 break;
658 }
659 }
660 printf ("%.*s\n", vec_len (s), s);
661 fflush (stdout);
662 vec_free (s);
663 }
664 while (0);
665#endif
666 }
Klement Sekera4c533132018-02-22 11:41:12 +0100667 if (is_feature)
668 {
669 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
670 }
671 else
672 {
673 *next0 = reass->next_index;
674 }
675 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
676 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200677 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100678free_buffers_and_return:
679 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
680 vec_free (vec_drop_compress);
681 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200682}
683
Klement Sekera4c533132018-02-22 11:41:12 +0100684always_inline void
685ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
686 ip6_reass_per_thread_t * rt,
687 ip6_reass_t * reass, u32 prev_range_bi,
688 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200689{
690
691 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
692 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
693 if (~0 != prev_range_bi)
694 {
695 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
696 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
697 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
698 prev_vnb->ip.reass.next_range_bi = new_next_bi;
699 }
700 else
701 {
702 if (~0 != reass->first_bi)
703 {
704 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
705 }
706 reass->first_bi = new_next_bi;
707 }
708 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200709}
710
Klement Sekerad0f70a32018-12-14 17:24:13 +0100711always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200712ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100713 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100714 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
715 ip6_frag_hdr_t * frag_hdr, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200716{
717 int consumed = 0;
718 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
719 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700720 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200721 fvnb->ip.reass.ip6_frag_hdr_offset =
722 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
723 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100724 if (fb->current_length < sizeof (*fip) ||
725 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
726 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
727 {
728 return IP6_REASS_RC_INTERNAL_ERROR;
729 }
730
Klement Sekera75e7d132017-09-20 08:26:30 +0200731 u32 fragment_first = fvnb->ip.reass.fragment_first =
732 ip6_frag_hdr_offset_bytes (frag_hdr);
733 u32 fragment_length =
734 vlib_buffer_length_in_chain (vm, fb) -
735 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
736 u32 fragment_last = fvnb->ip.reass.fragment_last =
737 fragment_first + fragment_length - 1;
738 int more_fragments = ip6_frag_hdr_more (frag_hdr);
739 u32 candidate_range_bi = reass->first_bi;
740 u32 prev_range_bi = ~0;
741 fvnb->ip.reass.range_first = fragment_first;
742 fvnb->ip.reass.range_last = fragment_last;
743 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200744 if (!more_fragments)
745 {
746 reass->last_packet_octet = fragment_last;
747 }
748 if (~0 == reass->first_bi)
749 {
750 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100751 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
752 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100753 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100754 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200755 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100756 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200757 }
Klement Sekera4c533132018-02-22 11:41:12 +0100758 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200759 clib_min (clib_net_to_host_u16 (fip->payload_length),
760 fvnb->ip.reass.estimated_mtu);
761 while (~0 != candidate_range_bi)
762 {
763 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
764 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
765 if (fragment_first > candidate_vnb->ip.reass.range_last)
766 {
767 // this fragments starts after candidate range
768 prev_range_bi = candidate_range_bi;
769 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
770 if (candidate_vnb->ip.reass.range_last < fragment_last &&
771 ~0 == candidate_range_bi)
772 {
773 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100774 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
775 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200776 consumed = 1;
777 break;
778 }
779 continue;
780 }
781 if (fragment_last < candidate_vnb->ip.reass.range_first)
782 {
783 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100784 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200785 *bi0);
786 consumed = 1;
787 }
788 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
789 fragment_last == candidate_vnb->ip.reass.range_last)
790 {
791 // duplicate fragment - ignore
792 }
793 else
794 {
795 // overlapping fragment - not allowed by RFC 8200
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100796 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100797 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200798 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
799 {
800 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
801 0);
802 }
Klement Sekera4c533132018-02-22 11:41:12 +0100803 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200804 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100805 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200806 }
807 break;
808 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200809 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100810check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200811 if (consumed)
812 {
813 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
814 {
815 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
816 }
817 }
818 if (~0 != reass->last_packet_octet &&
819 reass->data_len == reass->last_packet_octet + 1)
820 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100821 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100822 is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200823 }
824 else
825 {
826 if (consumed)
827 {
828 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200829 if (reass->fragments_n > rm->max_reass_len)
830 {
831 return IP6_REASS_RC_TOO_MANY_FRAGMENTS;
832 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200833 }
834 else
835 {
Klement Sekera4c533132018-02-22 11:41:12 +0100836 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200837 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
838 }
839 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100840 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200841}
842
Klement Sekera4c533132018-02-22 11:41:12 +0100843always_inline bool
844ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
845 vlib_buffer_t * b,
846 ip6_frag_hdr_t * frag_hdr)
847{
848 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
849 while (ip6_ext_hdr (tmp->next_hdr))
850 {
851 tmp = ip6_ext_next_header (tmp);
852 }
853 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
854 {
855 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
856 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
857 0);
858 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
859
860 return false;
861 }
862 return true;
863}
864
865always_inline bool
866ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
867 vlib_node_runtime_t * node,
868 vlib_buffer_t * b,
869 ip6_frag_hdr_t * frag_hdr)
870{
871 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
872 ip6_header_t *ip = vlib_buffer_get_current (b);
873 int more_fragments = ip6_frag_hdr_more (frag_hdr);
874 u32 fragment_length =
875 vlib_buffer_length_in_chain (vm, b) -
876 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
877 if (more_fragments && 0 != fragment_length % 8)
878 {
879 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
880 ICMP6_parameter_problem_erroneous_header_field,
881 (u8 *) & ip->payload_length - (u8 *) ip);
882 return false;
883 }
884 return true;
885}
886
887always_inline bool
888ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
889 vlib_node_runtime_t * node,
890 vlib_buffer_t * b,
891 ip6_frag_hdr_t * frag_hdr)
892{
893 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
894 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
895 u32 fragment_length =
896 vlib_buffer_length_in_chain (vm, b) -
897 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
898 if (fragment_first + fragment_length > 65535)
899 {
900 ip6_header_t *ip0 = vlib_buffer_get_current (b);
901 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
902 ICMP6_parameter_problem_erroneous_header_field,
903 (u8 *) & frag_hdr->fragment_offset_and_more
904 - (u8 *) ip0);
905 return false;
906 }
907 return true;
908}
909
Klement Sekera75e7d132017-09-20 08:26:30 +0200910always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100911ip6_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 ip6_reass_main_t *rm = &ip6_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800918 ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +0100919 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;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100923 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200924 {
925 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
926
Klement Sekera75e7d132017-09-20 08:26:30 +0200927 while (n_left_from > 0 && n_left_to_next > 0)
928 {
929 u32 bi0;
930 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100931 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100932 u32 error0 = IP6_ERROR_NONE;
933 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200934
935 bi0 = from[0];
936 b0 = vlib_get_buffer (vm, bi0);
937
938 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200939 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200940 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200941 if (ip6_ext_hdr (ip0->protocol))
942 {
943 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
944 IP_PROTOCOL_IPV6_FRAGMENTATION);
945 }
Klement Sekera4c533132018-02-22 11:41:12 +0100946 if (!frag_hdr)
947 {
948 // this is a regular packet - no fragmentation
949 next0 = IP6_REASSEMBLY_NEXT_INPUT;
950 goto skip_reass;
951 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200952 if (0 == ip6_frag_hdr_offset (frag_hdr))
953 {
954 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100955 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +0200956 {
Klement Sekera4c533132018-02-22 11:41:12 +0100957 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
958 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200959 }
Klement Sekera4c533132018-02-22 11:41:12 +0100960 }
961 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
962 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
963 frag_hdr))
964 {
965 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
966 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200967 }
968 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
969 (u8 *) frag_hdr - (u8 *) ip0;
970
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800971 ip6_reass_kv_t kv;
972 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200973
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800974 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
975 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
976 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
977 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
978 kv.k.as_u64[4] =
979 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
980 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
981 (u64) frag_hdr->identification;
982 kv.k.as_u64[5] = ip0->protocol;
983
984 ip6_reass_t *reass =
985 ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
986 &do_handoff);
987
988 if (PREDICT_FALSE (do_handoff))
989 {
990 next0 = IP6_REASSEMBLY_NEXT_HANDOFF;
991 if (is_feature)
992 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
993 kv.v.thread_index;
994 else
995 vnet_buffer (b0)->ip.reass.owner_thread_index =
996 kv.v.thread_index;
997 }
998 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200999 {
Klement Sekerad0f70a32018-12-14 17:24:13 +01001000 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001001 &error0, frag_hdr, is_feature))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001002 {
1003 case IP6_REASS_RC_OK:
1004 /* nothing to do here */
1005 break;
Klement Sekera3a343d42019-05-16 14:35:46 +02001006 case IP6_REASS_RC_TOO_MANY_FRAGMENTS:
1007 vlib_node_increment_counter (vm, node->node_index,
1008 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1009 1);
1010 ip6_reass_drop_all (vm, rm, reass);
1011 ip6_reass_free (rm, rt, reass);
1012 goto next_packet;
1013 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001014 case IP6_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001015 vlib_node_increment_counter (vm, node->node_index,
1016 IP6_ERROR_REASS_NO_BUF, 1);
1017 ip6_reass_drop_all (vm, rm, reass);
1018 ip6_reass_free (rm, rt, reass);
1019 goto next_packet;
1020 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001021 case IP6_REASS_RC_INTERNAL_ERROR:
Klement Sekera3a343d42019-05-16 14:35:46 +02001022 vlib_node_increment_counter (vm, node->node_index,
1023 IP6_ERROR_REASS_INTERNAL_ERROR,
1024 1);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001025 ip6_reass_drop_all (vm, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001026 ip6_reass_free (rm, rt, reass);
1027 goto next_packet;
1028 break;
1029 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001030 }
1031 else
1032 {
1033 next0 = IP6_REASSEMBLY_NEXT_DROP;
1034 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1035 }
1036
1037 b0->error = node->errors[error0];
1038
1039 if (~0 != bi0)
1040 {
Klement Sekera4c533132018-02-22 11:41:12 +01001041 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001042 to_next[0] = bi0;
1043 to_next += 1;
1044 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001045 if (is_feature && IP6_ERROR_NONE == error0)
1046 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001047 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001048 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001049 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001050 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1051 n_left_to_next, bi0, next0);
1052 }
1053
1054 if (~0 != icmp_bi)
1055 {
1056 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1057 to_next[0] = icmp_bi;
1058 to_next += 1;
1059 n_left_to_next -= 1;
1060 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1061 n_left_to_next, icmp_bi,
1062 next0);
1063 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001064 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001065 from += 1;
1066 n_left_from -= 1;
1067 }
1068
1069 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1070 }
1071
Klement Sekera4c533132018-02-22 11:41:12 +01001072 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001073 return frame->n_vectors;
1074}
1075
1076static char *ip6_reassembly_error_strings[] = {
1077#define _(sym, string) string,
1078 foreach_ip6_error
1079#undef _
1080};
1081
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001082VLIB_NODE_FN (ip6_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1083 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001084{
1085 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1086}
1087
Klement Sekera75e7d132017-09-20 08:26:30 +02001088/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001089VLIB_REGISTER_NODE (ip6_reass_node) = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001090 .name = "ip6-reassembly",
1091 .vector_size = sizeof (u32),
1092 .format_trace = format_ip6_reass_trace,
1093 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1094 .error_strings = ip6_reassembly_error_strings,
1095 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1096 .next_nodes =
1097 {
1098 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1099 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1100 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001101 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001102 },
1103};
1104/* *INDENT-ON* */
1105
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001106VLIB_NODE_FN (ip6_reass_node_feature) (vlib_main_t * vm,
1107 vlib_node_runtime_t * node,
1108 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001109{
1110 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1111}
1112
1113/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001114VLIB_REGISTER_NODE (ip6_reass_node_feature) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001115 .name = "ip6-reassembly-feature",
1116 .vector_size = sizeof (u32),
1117 .format_trace = format_ip6_reass_trace,
1118 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1119 .error_strings = ip6_reassembly_error_strings,
1120 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1121 .next_nodes =
1122 {
1123 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1124 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1125 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001126 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001127 },
1128};
1129/* *INDENT-ON* */
1130
Klement Sekera4c533132018-02-22 11:41:12 +01001131/* *INDENT-OFF* */
1132VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1133 .arc_name = "ip6-unicast",
1134 .node_name = "ip6-reassembly-feature",
1135 .runs_before = VNET_FEATURES ("ip6-lookup"),
1136 .runs_after = 0,
1137};
1138/* *INDENT-ON* */
1139
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001140#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001141static u32
1142ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001143{
1144 ip6_reass_main_t *rm = &ip6_reass_main;
1145 u32 nbuckets;
1146 u8 i;
1147
1148 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1149
1150 for (i = 0; i < 31; i++)
1151 if ((1 << i) >= nbuckets)
1152 break;
1153 nbuckets = 1 << i;
1154
1155 return nbuckets;
1156}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001157#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001158
1159typedef enum
1160{
1161 IP6_EVENT_CONFIG_CHANGED = 1,
1162} ip6_reass_event_t;
1163
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001164#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001165typedef struct
1166{
1167 int failure;
1168 clib_bihash_48_8_t *new_hash;
1169} ip6_rehash_cb_ctx;
1170
Klement Sekera4c533132018-02-22 11:41:12 +01001171static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001172ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1173{
1174 ip6_rehash_cb_ctx *ctx = _ctx;
1175 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1176 {
1177 ctx->failure = 1;
1178 }
1179}
1180
Klement Sekera4c533132018-02-22 11:41:12 +01001181static void
1182ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001183 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001184{
1185 ip6_reass_main.timeout_ms = timeout_ms;
1186 ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1187 ip6_reass_main.max_reass_n = max_reassemblies;
Klement Sekera3a343d42019-05-16 14:35:46 +02001188 ip6_reass_main.max_reass_len = max_reassembly_length;
Klement Sekera4c533132018-02-22 11:41:12 +01001189 ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1190}
1191
Klement Sekera75e7d132017-09-20 08:26:30 +02001192vnet_api_error_t
1193ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001194 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001195{
1196 u32 old_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3a343d42019-05-16 14:35:46 +02001197 ip6_reass_set_params (timeout_ms, max_reassemblies, max_reassembly_length,
Klement Sekera4c533132018-02-22 11:41:12 +01001198 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001199 vlib_process_signal_event (ip6_reass_main.vlib_main,
1200 ip6_reass_main.ip6_reass_expire_node_idx,
1201 IP6_EVENT_CONFIG_CHANGED, 0);
1202 u32 new_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001203 if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001204 {
1205 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001206 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001207 ip6_rehash_cb_ctx ctx;
1208 ctx.failure = 0;
1209 ctx.new_hash = &new_hash;
1210 clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1211 new_nbuckets * 1024);
1212 clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1213 ip6_rehash_cb, &ctx);
1214 if (ctx.failure)
1215 {
1216 clib_bihash_free_48_8 (&new_hash);
1217 return -1;
1218 }
1219 else
1220 {
1221 clib_bihash_free_48_8 (&ip6_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001222 clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1223 sizeof (ip6_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001224 }
1225 }
1226 return 0;
1227}
1228
1229vnet_api_error_t
1230ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1231 u32 * expire_walk_interval_ms)
1232{
1233 *timeout_ms = ip6_reass_main.timeout_ms;
1234 *max_reassemblies = ip6_reass_main.max_reass_n;
1235 *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1236 return 0;
1237}
1238
Klement Sekera4c533132018-02-22 11:41:12 +01001239static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001240ip6_reass_init_function (vlib_main_t * vm)
1241{
1242 ip6_reass_main_t *rm = &ip6_reass_main;
1243 clib_error_t *error = 0;
1244 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001245 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001246
1247 rm->vlib_main = vm;
1248 rm->vnet_main = vnet_get_main ();
1249
Juraj Slobodacd806922018-10-10 10:15:54 +02001250 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001251 ip6_reass_per_thread_t *rt;
1252 vec_foreach (rt, rm->per_thread_data)
1253 {
1254 clib_spinlock_init (&rt->lock);
1255 pool_alloc (rt->pool, rm->max_reass_n);
1256 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001257
1258 node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1259 ASSERT (node);
1260 rm->ip6_reass_expire_node_idx = node->index;
1261
Klement Sekera3ecc2212018-03-27 10:34:43 +02001262 ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS,
1263 IP6_REASS_MAX_REASSEMBLIES_DEFAULT,
Klement Sekera3a343d42019-05-16 14:35:46 +02001264 IP6_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
Klement Sekera3ecc2212018-03-27 10:34:43 +02001265 IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1266
Klement Sekera75e7d132017-09-20 08:26:30 +02001267 nbuckets = ip6_reass_get_nbuckets ();
1268 clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1269
Dave Barach1403fcd2018-02-05 09:45:43 -05001270 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001271 ASSERT (node);
1272 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001273 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1274 ASSERT (node);
1275 rm->ip6_icmp_error_idx = node->index;
1276
1277 if ((error = vlib_call_init_function (vm, ip_main_init)))
1278 return error;
1279 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1280 ip6_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001281
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001282 rm->fq_index = vlib_frame_queue_main_init (ip6_reass_node.index, 0);
1283 rm->fq_feature_index =
1284 vlib_frame_queue_main_init (ip6_reass_node_feature.index, 0);
1285
Klement Sekera75e7d132017-09-20 08:26:30 +02001286 return error;
1287}
1288
1289VLIB_INIT_FUNCTION (ip6_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001290#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001291
1292static uword
1293ip6_reass_walk_expired (vlib_main_t * vm,
1294 vlib_node_runtime_t * node, vlib_frame_t * f)
1295{
1296 ip6_reass_main_t *rm = &ip6_reass_main;
1297 uword event_type, *event_data = 0;
1298
1299 while (true)
1300 {
1301 vlib_process_wait_for_event_or_clock (vm,
1302 (f64) rm->expire_walk_interval_ms
1303 / (f64) MSEC_PER_SEC);
1304 event_type = vlib_process_get_events (vm, &event_data);
1305
1306 switch (event_type)
1307 {
1308 case ~0: /* no events => timeout */
1309 /* nothing to do here */
1310 break;
1311 case IP6_EVENT_CONFIG_CHANGED:
1312 break;
1313 default:
1314 clib_warning ("BUG: event type 0x%wx", event_type);
1315 break;
1316 }
1317 f64 now = vlib_time_now (vm);
1318
1319 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001320 int *pool_indexes_to_free = NULL;
1321
Klement Sekera4c533132018-02-22 11:41:12 +01001322 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001323 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001324 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001325 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001326 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1327 {
1328 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1329 clib_spinlock_lock (&rt->lock);
1330
1331 vec_reset_length (pool_indexes_to_free);
1332 /* *INDENT-OFF* */
1333 pool_foreach_index (index, rt->pool, ({
1334 reass = pool_elt_at_index (rt->pool, index);
1335 if (now > reass->last_heard + rm->timeout)
1336 {
1337 vec_add1 (pool_indexes_to_free, index);
1338 }
1339 }));
1340 /* *INDENT-ON* */
1341 int *i;
1342 /* *INDENT-OFF* */
1343 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001344 {
Klement Sekera4c533132018-02-22 11:41:12 +01001345 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1346 u32 icmp_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +01001347 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1348 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1349 {
1350 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1351 b->trace_index))
1352 {
1353 /* the trace is gone, don't trace this buffer anymore */
1354 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1355 }
1356 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001357 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001358 if (~0 != icmp_bi)
1359 {
1360 vec_add1 (vec_icmp_bi, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001361 }
1362 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001363 }
Klement Sekera4c533132018-02-22 11:41:12 +01001364 /* *INDENT-ON* */
1365
1366 clib_spinlock_unlock (&rt->lock);
1367 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001368
Klement Sekera75e7d132017-09-20 08:26:30 +02001369 while (vec_len (vec_icmp_bi) > 0)
1370 {
1371 vlib_frame_t *f =
1372 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1373 u32 *to_next = vlib_frame_vector_args (f);
1374 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001375 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001376 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1377 {
1378 u32 bi = vec_pop (vec_icmp_bi);
1379 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1380 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1381 {
1382 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1383 b->trace_index))
1384 {
1385 /* the trace is gone, don't trace this buffer anymore */
1386 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1387 }
1388 else
1389 {
Klement Sekera4c533132018-02-22 11:41:12 +01001390 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001391 }
1392 }
1393 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1394 to_next[0] = bi;
1395 ++f->n_vectors;
1396 to_next += 1;
1397 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001398 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001399 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001400 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1401 }
1402
1403 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001404 vec_free (vec_icmp_bi);
1405 if (event_data)
1406 {
1407 _vec_len (event_data) = 0;
1408 }
1409 }
1410
1411 return 0;
1412}
1413
Klement Sekera75e7d132017-09-20 08:26:30 +02001414/* *INDENT-OFF* */
1415VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1416 .function = ip6_reass_walk_expired,
1417 .format_trace = format_ip6_reass_trace,
1418 .type = VLIB_NODE_TYPE_PROCESS,
1419 .name = "ip6-reassembly-expire-walk",
1420
1421 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1422 .error_strings = ip6_reassembly_error_strings,
1423
1424};
1425/* *INDENT-ON* */
1426
1427static u8 *
1428format_ip6_reass_key (u8 * s, va_list * args)
1429{
1430 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1431 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1432 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1433 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1434 return s;
1435}
1436
1437static u8 *
1438format_ip6_reass (u8 * s, va_list * args)
1439{
1440 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1441 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1442
Klement Sekera4c533132018-02-22 11:41:12 +01001443 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001444 "last_packet_octet: %u, trace_op_counter: %u\n",
1445 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1446 reass->data_len, reass->last_packet_octet,
1447 reass->trace_op_counter);
1448 u32 bi = reass->first_bi;
1449 u32 counter = 0;
1450 while (~0 != bi)
1451 {
1452 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1453 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1454 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1455 "fragment[%u, %u]\n",
1456 counter, vnb->ip.reass.range_first,
1457 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001458 ip6_reass_buffer_get_data_offset (b),
1459 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001460 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1461 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1462 {
1463 bi = b->next_buffer;
1464 }
1465 else
1466 {
1467 bi = ~0;
1468 }
1469 }
1470 return s;
1471}
1472
1473static clib_error_t *
1474show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1475 CLIB_UNUSED (vlib_cli_command_t * lmd))
1476{
1477 ip6_reass_main_t *rm = &ip6_reass_main;
1478
1479 vlib_cli_output (vm, "---------------------");
1480 vlib_cli_output (vm, "IP6 reassembly status");
1481 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001482 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001483 if (unformat (input, "details"))
1484 {
Klement Sekera4c533132018-02-22 11:41:12 +01001485 details = true;
1486 }
1487
1488 u32 sum_reass_n = 0;
1489 u64 sum_buffers_n = 0;
1490 ip6_reass_t *reass;
1491 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001492 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001493 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1494 {
1495 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1496 clib_spinlock_lock (&rt->lock);
1497 if (details)
1498 {
1499 /* *INDENT-OFF* */
1500 pool_foreach (reass, rt->pool, {
1501 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1502 });
1503 /* *INDENT-ON* */
1504 }
1505 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001506 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001507 }
1508 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001509 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1510 (long unsigned) sum_reass_n);
1511 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1512 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001513 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001514 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001515 return 0;
1516}
1517
1518/* *INDENT-OFF* */
1519VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1520 .path = "show ip6-reassembly",
1521 .short_help = "show ip6-reassembly [details]",
1522 .function = show_ip6_reass,
1523};
1524/* *INDENT-ON* */
1525
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001526#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001527vnet_api_error_t
1528ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1529{
1530 return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1531 sw_if_index, enable_disable, 0, 0);
1532}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001533#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001534
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001535#define foreach_ip6_reassembly_handoff_error \
1536_(CONGESTION_DROP, "congestion drop")
1537
1538
1539typedef enum
1540{
1541#define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1542 foreach_ip6_reassembly_handoff_error
1543#undef _
1544 IP6_REASSEMBLY_HANDOFF_N_ERROR,
1545} ip6_reassembly_handoff_error_t;
1546
1547static char *ip6_reassembly_handoff_error_strings[] = {
1548#define _(sym,string) string,
1549 foreach_ip6_reassembly_handoff_error
1550#undef _
1551};
1552
1553typedef struct
1554{
1555 u32 next_worker_index;
1556} ip6_reassembly_handoff_trace_t;
1557
1558static u8 *
1559format_ip6_reassembly_handoff_trace (u8 * s, va_list * args)
1560{
1561 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1562 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1563 ip6_reassembly_handoff_trace_t *t =
1564 va_arg (*args, ip6_reassembly_handoff_trace_t *);
1565
1566 s =
1567 format (s, "ip6-reassembly-handoff: next-worker %d",
1568 t->next_worker_index);
1569
1570 return s;
1571}
1572
1573always_inline uword
1574ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1575 vlib_frame_t * frame, bool is_feature)
1576{
1577 ip6_reass_main_t *rm = &ip6_reass_main;
1578
1579 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1580 u32 n_enq, n_left_from, *from;
1581 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1582 u32 fq_index;
1583
1584 from = vlib_frame_vector_args (frame);
1585 n_left_from = frame->n_vectors;
1586 vlib_get_buffers (vm, from, bufs, n_left_from);
1587
1588 b = bufs;
1589 ti = thread_indices;
1590
1591 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1592
1593 while (n_left_from > 0)
1594 {
1595 ti[0] =
1596 (is_feature) ? vnet_buffer (b[0])->ip.
1597 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1598 reass.owner_thread_index;
1599
1600 if (PREDICT_FALSE
1601 ((node->flags & VLIB_NODE_FLAG_TRACE)
1602 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1603 {
1604 ip6_reassembly_handoff_trace_t *t =
1605 vlib_add_trace (vm, node, b[0], sizeof (*t));
1606 t->next_worker_index = ti[0];
1607 }
1608
1609 n_left_from -= 1;
1610 ti += 1;
1611 b += 1;
1612 }
1613 n_enq =
1614 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1615 frame->n_vectors, 1);
1616
1617 if (n_enq < frame->n_vectors)
1618 vlib_node_increment_counter (vm, node->node_index,
1619 IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1620 frame->n_vectors - n_enq);
1621 return frame->n_vectors;
1622}
1623
1624VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm,
1625 vlib_node_runtime_t * node,
1626 vlib_frame_t * frame)
1627{
1628 return ip6_reassembly_handoff_inline (vm, node, frame,
1629 false /* is_feature */ );
1630}
1631
1632/* *INDENT-OFF* */
1633VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = {
1634 .name = "ip6-reassembly-handoff",
1635 .vector_size = sizeof (u32),
1636 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1637 .error_strings = ip6_reassembly_handoff_error_strings,
1638 .format_trace = format_ip6_reassembly_handoff_trace,
1639
1640 .n_next_nodes = 1,
1641
1642 .next_nodes = {
1643 [0] = "error-drop",
1644 },
1645};
1646
1647
1648VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm,
1649 vlib_node_runtime_t * node, vlib_frame_t * frame)
1650{
1651 return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1652}
1653
1654
1655/* *INDENT-OFF* */
1656VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = {
1657 .name = "ip6-reass-feature-hoff",
1658 .vector_size = sizeof (u32),
1659 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1660 .error_strings = ip6_reassembly_handoff_error_strings,
1661 .format_trace = format_ip6_reassembly_handoff_trace,
1662
1663 .n_next_nodes = 1,
1664
1665 .next_nodes = {
1666 [0] = "error-drop",
1667 },
1668};
1669/* *INDENT-ON* */
1670
Klement Sekera75e7d132017-09-20 08:26:30 +02001671/*
1672 * fd.io coding-style-patch-verification: ON
1673 *
1674 * Local Variables:
1675 * eval: (c-set-style "gnu")
1676 * End:
1677 */