blob: 0c86145fe402ed555c398a1eab1d4c6206199b91 [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
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080059typedef union
60{
61 struct
62 {
63 u32 reass_index;
64 u32 thread_index;
65 };
66 u64 as_u64;
67} ip6_reass_val_t;
68
69typedef union
70{
71 struct
72 {
73 ip6_reass_key_t k;
74 ip6_reass_val_t v;
75 };
76 clib_bihash_kv_48_8_t kv;
77} ip6_reass_kv_t;
78
79
Klement Sekera75e7d132017-09-20 08:26:30 +020080always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +020081ip6_reass_buffer_get_data_offset (vlib_buffer_t * b)
82{
83 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010084 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020085}
86
87always_inline u16
88ip6_reass_buffer_get_data_len (vlib_buffer_t * b)
89{
90 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010091 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
92 (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020093}
94
95typedef struct
96{
97 // hash table key
98 ip6_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +020099 // time when last packet was received
100 f64 last_heard;
101 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100102 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200103 // buffer index of first buffer in this reassembly context
104 u32 first_bi;
105 // last octet of packet, ~0 until fragment without more_fragments arrives
106 u32 last_packet_octet;
107 // length of data collected so far
108 u32 data_len;
109 // trace operation counter
110 u32 trace_op_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100111 // next index - used by non-feature node
112 u8 next_index;
113 // minimum fragment length for this reassembly - used to estimate MTU
114 u16 min_fragment_length;
Klement Sekera75e7d132017-09-20 08:26:30 +0200115} ip6_reass_t;
116
117typedef struct
118{
Klement Sekera4c533132018-02-22 11:41:12 +0100119 ip6_reass_t *pool;
120 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100121 u32 id_counter;
122 clib_spinlock_t lock;
123} ip6_reass_per_thread_t;
124
125typedef struct
126{
Klement Sekera75e7d132017-09-20 08:26:30 +0200127 // IPv6 config
128 u32 timeout_ms;
129 f64 timeout;
130 u32 expire_walk_interval_ms;
131 u32 max_reass_n;
132
133 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200134 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100135
136 // per-thread data
137 ip6_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200138
139 // convenience
140 vlib_main_t *vlib_main;
141 vnet_main_t *vnet_main;
142
143 // node index of ip6-drop node
144 u32 ip6_drop_idx;
145 u32 ip6_icmp_error_idx;
146 u32 ip6_reass_expire_node_idx;
147
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800148 /** Worker handoff */
149 u32 fq_index;
150 u32 fq_feature_index;
151
Klement Sekera75e7d132017-09-20 08:26:30 +0200152} ip6_reass_main_t;
153
154ip6_reass_main_t ip6_reass_main;
155
156typedef enum
157{
158 IP6_REASSEMBLY_NEXT_INPUT,
159 IP6_REASSEMBLY_NEXT_DROP,
160 IP6_REASSEMBLY_NEXT_ICMP_ERROR,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800161 IP6_REASSEMBLY_NEXT_HANDOFF,
Klement Sekera75e7d132017-09-20 08:26:30 +0200162 IP6_REASSEMBLY_N_NEXT,
163} ip6_reass_next_t;
164
165typedef enum
166{
167 RANGE_NEW,
168 RANGE_OVERLAP,
169 ICMP_ERROR_RT_EXCEEDED,
170 ICMP_ERROR_FL_TOO_BIG,
171 ICMP_ERROR_FL_NOT_MULT_8,
172 FINALIZE,
173} ip6_reass_trace_operation_e;
174
175typedef struct
176{
177 u16 range_first;
178 u16 range_last;
179 u32 range_bi;
180 i32 data_offset;
181 u32 data_len;
182 u32 first_bi;
183} ip6_reass_range_trace_t;
184
185typedef struct
186{
187 ip6_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200188 u32 reass_id;
189 ip6_reass_range_trace_t trace_range;
190 u32 size_diff;
191 u32 op_id;
192 u32 fragment_first;
193 u32 fragment_last;
194 u32 total_data_len;
195} ip6_reass_trace_t;
196
197static void
198ip6_reass_trace_details (vlib_main_t * vm, u32 bi,
199 ip6_reass_range_trace_t * trace)
200{
201 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
202 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
203 trace->range_first = vnb->ip.reass.range_first;
204 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100205 trace->data_offset = ip6_reass_buffer_get_data_offset (b);
206 trace->data_len = ip6_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200207 trace->range_bi = bi;
208}
209
Klement Sekera4c533132018-02-22 11:41:12 +0100210static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200211format_ip6_reass_range_trace (u8 * s, va_list * args)
212{
213 ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *);
214 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
215 trace->range_last, trace->data_offset, trace->data_len,
216 trace->range_bi);
217 return s;
218}
219
Klement Sekera4c533132018-02-22 11:41:12 +0100220static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200221format_ip6_reass_trace (u8 * s, va_list * args)
222{
223 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
224 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
225 ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
226 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
227 u32 indent = format_get_indent (s);
228 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
229 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
230 t->fragment_last);
231 switch (t->action)
232 {
233 case RANGE_NEW:
234 s = format (s, "\n%Unew %U", format_white_space, indent,
235 format_ip6_reass_range_trace, &t->trace_range);
236 break;
237 case RANGE_OVERLAP:
238 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
239 format_ip6_reass_range_trace, &t->trace_range);
240 break;
241 case ICMP_ERROR_FL_TOO_BIG:
242 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
243 format_white_space, indent, format_ip6_reass_range_trace,
244 &t->trace_range);
245 break;
246 case ICMP_ERROR_FL_NOT_MULT_8:
247 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
248 format_white_space, indent, format_ip6_reass_range_trace,
249 &t->trace_range);
250 break;
251 case ICMP_ERROR_RT_EXCEEDED:
252 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
253 format_white_space, indent);
254 break;
255 case FINALIZE:
256 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
257 break;
258 }
259 return s;
260}
261
262static void
263ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
264 ip6_reass_main_t * rm, ip6_reass_t * reass,
265 u32 bi, ip6_reass_trace_operation_e action,
266 u32 size_diff)
267{
268 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
269 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100270 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
271 {
272 // this buffer's trace is gone
273 b->flags &= ~VLIB_BUFFER_IS_TRACED;
274 return;
275 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200276 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200277 t->reass_id = reass->id;
278 t->action = action;
279 ip6_reass_trace_details (vm, bi, &t->trace_range);
280 t->size_diff = size_diff;
281 t->op_id = reass->trace_op_counter;
282 ++reass->trace_op_counter;
283 t->fragment_first = vnb->ip.reass.fragment_first;
284 t->fragment_last = vnb->ip.reass.fragment_last;
285 t->trace_range.first_bi = reass->first_bi;
286 t->total_data_len = reass->data_len;
287#if 0
288 static u8 *s = NULL;
289 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
290 printf ("%.*s\n", vec_len (s), s);
291 fflush (stdout);
292 vec_reset_length (s);
293#endif
294}
295
Klement Sekera4c533132018-02-22 11:41:12 +0100296always_inline void
297ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
298 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200299{
300 clib_bihash_kv_48_8_t kv;
301 kv.key[0] = reass->key.as_u64[0];
302 kv.key[1] = reass->key.as_u64[1];
303 kv.key[2] = reass->key.as_u64[2];
304 kv.key[3] = reass->key.as_u64[3];
305 kv.key[4] = reass->key.as_u64[4];
306 kv.key[5] = reass->key.as_u64[5];
307 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100308 pool_put (rt->pool, reass);
309 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200310}
311
Klement Sekera4c533132018-02-22 11:41:12 +0100312always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200313ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100314 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200315{
316 u32 range_bi = reass->first_bi;
317 vlib_buffer_t *range_b;
318 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100319 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200320 while (~0 != range_bi)
321 {
322 range_b = vlib_get_buffer (vm, range_bi);
323 range_vnb = vnet_buffer (range_b);
324 u32 bi = range_bi;
325 while (~0 != bi)
326 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100327 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200328 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
329 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
330 {
331 bi = b->next_buffer;
332 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
333 }
334 else
335 {
336 bi = ~0;
337 }
338 }
339 range_bi = range_vnb->ip.reass.next_range_bi;
340 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100341 vlib_buffer_free (vm, to_free, vec_len (to_free));
342 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200343}
344
Klement Sekera4c533132018-02-22 11:41:12 +0100345always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200346ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100347 ip6_reass_main_t * rm, ip6_reass_t * reass,
348 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200349{
350 if (~0 == reass->first_bi)
351 {
352 return;
353 }
354 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
355 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
356 {
357 *icmp_bi = reass->first_bi;
358 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
359 {
360 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
361 ICMP_ERROR_RT_EXCEEDED, 0);
362 }
363 // fragment with offset zero received - send icmp message back
364 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
365 {
366 // separate first buffer from chain and steer it towards icmp node
367 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
368 reass->first_bi = b->next_buffer;
369 }
370 else
371 {
372 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
373 }
374 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
375 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
376 0);
377 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100378 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200379}
380
Klement Sekera4c533132018-02-22 11:41:12 +0100381always_inline ip6_reass_t *
382ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
383 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800384 ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200385{
386 ip6_reass_t *reass = NULL;
387 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200388
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800389 if (!clib_bihash_search_48_8
390 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200391 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800392 if (vm->thread_index != kv->v.thread_index)
393 {
394 *do_handoff = 1;
395 return NULL;
396 }
397 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
398
Klement Sekera75e7d132017-09-20 08:26:30 +0200399 if (now > reass->last_heard + rm->timeout)
400 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100401 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100402 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200403 reass = NULL;
404 }
405 }
406
407 if (reass)
408 {
409 reass->last_heard = now;
410 return reass;
411 }
412
Klement Sekera4c533132018-02-22 11:41:12 +0100413 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200414 {
415 reass = NULL;
416 return reass;
417 }
418 else
419 {
Klement Sekera4c533132018-02-22 11:41:12 +0100420 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400421 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800422 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100423 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200424 reass->first_bi = ~0;
425 reass->last_packet_octet = ~0;
426 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100427 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200428 }
429
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800430 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
431 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
432 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
433 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
434 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
435 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
436 kv->v.reass_index = (reass - rt->pool);
437 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200438 reass->last_heard = now;
439
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800440 if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200441 {
Klement Sekera4c533132018-02-22 11:41:12 +0100442 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200443 reass = NULL;
444 }
445
446 return reass;
447}
448
Klement Sekerad0f70a32018-12-14 17:24:13 +0100449always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200450ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100451 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100452 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
453 bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200454{
Klement Sekera75e7d132017-09-20 08:26:30 +0200455 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200456 *error0 = IP6_ERROR_NONE;
457 ip6_frag_hdr_t *frag_hdr;
458 vlib_buffer_t *last_b = NULL;
459 u32 sub_chain_bi = reass->first_bi;
460 u32 total_length = 0;
461 u32 buf_cnt = 0;
462 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100463 u32 *vec_drop_compress = NULL;
464 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200465 do
466 {
467 u32 tmp_bi = sub_chain_bi;
468 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100469 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
470 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
471 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
472 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100473 rv = IP6_REASS_RC_INTERNAL_ERROR;
474 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100475 }
476
Klement Sekera75e7d132017-09-20 08:26:30 +0200477 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
478 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
479 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
480 u32 trim_end =
481 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
482 if (tmp_bi == reass->first_bi)
483 {
484 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100485 if (0 != ip6_reass_buffer_get_data_offset (tmp))
486 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100487 rv = IP6_REASS_RC_INTERNAL_ERROR;
488 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100489 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200490 trim_front = 0;
491 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
492 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
493 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100494 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
495 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100496 rv = IP6_REASS_RC_INTERNAL_ERROR;
497 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100498 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200499 }
500 u32 keep_data =
501 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
502 while (1)
503 {
504 ++buf_cnt;
505 if (trim_front)
506 {
507 if (trim_front > tmp->current_length)
508 {
509 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100510 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200511 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100512 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
513 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100514 rv = IP6_REASS_RC_INTERNAL_ERROR;
515 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100516 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200517 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
518 tmp_bi = tmp->next_buffer;
519 tmp = vlib_get_buffer (vm, tmp_bi);
520 continue;
521 }
522 else
523 {
524 vlib_buffer_advance (tmp, trim_front);
525 trim_front = 0;
526 }
527 }
528 if (keep_data)
529 {
530 if (last_b)
531 {
532 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
533 last_b->next_buffer = tmp_bi;
534 }
535 last_b = tmp;
536 if (keep_data <= tmp->current_length)
537 {
538 tmp->current_length = keep_data;
539 keep_data = 0;
540 }
541 else
542 {
543 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100544 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
545 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100546 rv = IP6_REASS_RC_INTERNAL_ERROR;
547 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100548 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200549 }
550 total_length += tmp->current_length;
551 }
552 else
553 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100554 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100555 if (reass->first_bi == tmp_bi)
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 ++dropped_cnt;
561 }
562 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
563 {
564 tmp_bi = tmp->next_buffer;
565 tmp = vlib_get_buffer (vm, tmp->next_buffer);
566 }
567 else
568 {
569 break;
570 }
571 }
572 sub_chain_bi =
573 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
574 reass.next_range_bi;
575 }
576 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700577
Klement Sekerad0f70a32018-12-14 17:24:13 +0100578 if (!last_b)
579 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100580 rv = IP6_REASS_RC_INTERNAL_ERROR;
581 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100582 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200583 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
584 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100585 if (total_length < first_b->current_length)
586 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100587 rv = IP6_REASS_RC_INTERNAL_ERROR;
588 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100589 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200590 total_length -= first_b->current_length;
591 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
592 first_b->total_length_not_including_first_buffer = total_length;
593 // drop fragment header
594 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
595 ip6_header_t *ip = vlib_buffer_get_current (first_b);
596 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
597 ip6_ext_header_t *prev_hdr;
598 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
599 IP_PROTOCOL_IPV6_FRAGMENTATION);
600 if (prev_hdr)
601 {
602 prev_hdr->next_hdr = frag_hdr->next_hdr;
603 }
604 else
605 {
606 ip->protocol = frag_hdr->next_hdr;
607 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100608 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
609 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100610 rv = IP6_REASS_RC_INTERNAL_ERROR;
611 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100612 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200613 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
614 first_b->current_length - ip6_frag_hdr_offset -
615 sizeof (ip6_frag_hdr_t));
616 first_b->current_length -= sizeof (*frag_hdr);
617 ip->payload_length =
618 clib_host_to_net_u16 (total_length + first_b->current_length -
619 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100620 if (!vlib_buffer_chain_linearize (vm, first_b))
621 {
622 rv = IP6_REASS_RC_NO_BUF;
623 goto free_buffers_and_return;
624 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200625 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
626 {
627 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
628#if 0
629 // following code does a hexdump of packet fragments to stdout ...
630 do
631 {
632 u32 bi = reass->first_bi;
633 u8 *s = NULL;
634 while (~0 != bi)
635 {
636 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
637 s = format (s, "%u: %U\n", bi, format_hexdump,
638 vlib_buffer_get_current (b), b->current_length);
639 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
640 {
641 bi = b->next_buffer;
642 }
643 else
644 {
645 break;
646 }
647 }
648 printf ("%.*s\n", vec_len (s), s);
649 fflush (stdout);
650 vec_free (s);
651 }
652 while (0);
653#endif
654 }
Klement Sekera4c533132018-02-22 11:41:12 +0100655 if (is_feature)
656 {
657 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
658 }
659 else
660 {
661 *next0 = reass->next_index;
662 }
663 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
664 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200665 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100666free_buffers_and_return:
667 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
668 vec_free (vec_drop_compress);
669 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200670}
671
Klement Sekera4c533132018-02-22 11:41:12 +0100672always_inline void
673ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
674 ip6_reass_per_thread_t * rt,
675 ip6_reass_t * reass, u32 prev_range_bi,
676 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200677{
678
679 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
680 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
681 if (~0 != prev_range_bi)
682 {
683 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
684 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
685 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
686 prev_vnb->ip.reass.next_range_bi = new_next_bi;
687 }
688 else
689 {
690 if (~0 != reass->first_bi)
691 {
692 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
693 }
694 reass->first_bi = new_next_bi;
695 }
696 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200697}
698
Klement Sekerad0f70a32018-12-14 17:24:13 +0100699always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200700ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100701 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100702 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
703 ip6_frag_hdr_t * frag_hdr, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200704{
705 int consumed = 0;
706 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
707 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700708 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200709 fvnb->ip.reass.ip6_frag_hdr_offset =
710 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
711 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100712 if (fb->current_length < sizeof (*fip) ||
713 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
714 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
715 {
716 return IP6_REASS_RC_INTERNAL_ERROR;
717 }
718
Klement Sekera75e7d132017-09-20 08:26:30 +0200719 u32 fragment_first = fvnb->ip.reass.fragment_first =
720 ip6_frag_hdr_offset_bytes (frag_hdr);
721 u32 fragment_length =
722 vlib_buffer_length_in_chain (vm, fb) -
723 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
724 u32 fragment_last = fvnb->ip.reass.fragment_last =
725 fragment_first + fragment_length - 1;
726 int more_fragments = ip6_frag_hdr_more (frag_hdr);
727 u32 candidate_range_bi = reass->first_bi;
728 u32 prev_range_bi = ~0;
729 fvnb->ip.reass.range_first = fragment_first;
730 fvnb->ip.reass.range_last = fragment_last;
731 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200732 if (!more_fragments)
733 {
734 reass->last_packet_octet = fragment_last;
735 }
736 if (~0 == reass->first_bi)
737 {
738 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100739 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
740 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100741 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100742 consumed = 1;
743 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200744 }
Klement Sekera4c533132018-02-22 11:41:12 +0100745 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200746 clib_min (clib_net_to_host_u16 (fip->payload_length),
747 fvnb->ip.reass.estimated_mtu);
748 while (~0 != candidate_range_bi)
749 {
750 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
751 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
752 if (fragment_first > candidate_vnb->ip.reass.range_last)
753 {
754 // this fragments starts after candidate range
755 prev_range_bi = candidate_range_bi;
756 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
757 if (candidate_vnb->ip.reass.range_last < fragment_last &&
758 ~0 == candidate_range_bi)
759 {
760 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100761 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
762 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200763 consumed = 1;
764 break;
765 }
766 continue;
767 }
768 if (fragment_last < candidate_vnb->ip.reass.range_first)
769 {
770 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100771 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200772 *bi0);
773 consumed = 1;
774 }
775 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
776 fragment_last == candidate_vnb->ip.reass.range_last)
777 {
778 // duplicate fragment - ignore
779 }
780 else
781 {
782 // overlapping fragment - not allowed by RFC 8200
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100783 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100784 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200785 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
786 {
787 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
788 0);
789 }
Klement Sekera4c533132018-02-22 11:41:12 +0100790 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200791 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100792 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200793 }
794 break;
795 }
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100796check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200797 if (consumed)
798 {
799 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
800 {
801 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
802 }
803 }
804 if (~0 != reass->last_packet_octet &&
805 reass->data_len == reass->last_packet_octet + 1)
806 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100807 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100808 is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200809 }
810 else
811 {
812 if (consumed)
813 {
814 *bi0 = ~0;
815 }
816 else
817 {
Klement Sekera4c533132018-02-22 11:41:12 +0100818 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200819 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
820 }
821 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100822 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200823}
824
Klement Sekera4c533132018-02-22 11:41:12 +0100825always_inline bool
826ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
827 vlib_buffer_t * b,
828 ip6_frag_hdr_t * frag_hdr)
829{
830 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
831 while (ip6_ext_hdr (tmp->next_hdr))
832 {
833 tmp = ip6_ext_next_header (tmp);
834 }
835 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
836 {
837 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
838 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
839 0);
840 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
841
842 return false;
843 }
844 return true;
845}
846
847always_inline bool
848ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
849 vlib_node_runtime_t * node,
850 vlib_buffer_t * b,
851 ip6_frag_hdr_t * frag_hdr)
852{
853 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
854 ip6_header_t *ip = vlib_buffer_get_current (b);
855 int more_fragments = ip6_frag_hdr_more (frag_hdr);
856 u32 fragment_length =
857 vlib_buffer_length_in_chain (vm, b) -
858 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
859 if (more_fragments && 0 != fragment_length % 8)
860 {
861 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
862 ICMP6_parameter_problem_erroneous_header_field,
863 (u8 *) & ip->payload_length - (u8 *) ip);
864 return false;
865 }
866 return true;
867}
868
869always_inline bool
870ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
871 vlib_node_runtime_t * node,
872 vlib_buffer_t * b,
873 ip6_frag_hdr_t * frag_hdr)
874{
875 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
876 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
877 u32 fragment_length =
878 vlib_buffer_length_in_chain (vm, b) -
879 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
880 if (fragment_first + fragment_length > 65535)
881 {
882 ip6_header_t *ip0 = vlib_buffer_get_current (b);
883 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
884 ICMP6_parameter_problem_erroneous_header_field,
885 (u8 *) & frag_hdr->fragment_offset_and_more
886 - (u8 *) ip0);
887 return false;
888 }
889 return true;
890}
891
Klement Sekera75e7d132017-09-20 08:26:30 +0200892always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100893ip6_reassembly_inline (vlib_main_t * vm,
894 vlib_node_runtime_t * node,
895 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200896{
897 u32 *from = vlib_frame_vector_args (frame);
898 u32 n_left_from, n_left_to_next, *to_next, next_index;
899 ip6_reass_main_t *rm = &ip6_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800900 ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +0100901 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200902
903 n_left_from = frame->n_vectors;
904 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100905 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200906 {
907 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
908
Klement Sekera75e7d132017-09-20 08:26:30 +0200909 while (n_left_from > 0 && n_left_to_next > 0)
910 {
911 u32 bi0;
912 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100913 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100914 u32 error0 = IP6_ERROR_NONE;
915 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200916
917 bi0 = from[0];
918 b0 = vlib_get_buffer (vm, bi0);
919
920 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200921 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200922 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200923 if (ip6_ext_hdr (ip0->protocol))
924 {
925 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
926 IP_PROTOCOL_IPV6_FRAGMENTATION);
927 }
Klement Sekera4c533132018-02-22 11:41:12 +0100928 if (!frag_hdr)
929 {
930 // this is a regular packet - no fragmentation
931 next0 = IP6_REASSEMBLY_NEXT_INPUT;
932 goto skip_reass;
933 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200934 if (0 == ip6_frag_hdr_offset (frag_hdr))
935 {
936 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100937 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +0200938 {
Klement Sekera4c533132018-02-22 11:41:12 +0100939 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
940 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200941 }
Klement Sekera4c533132018-02-22 11:41:12 +0100942 }
943 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
944 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
945 frag_hdr))
946 {
947 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
948 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200949 }
950 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
951 (u8 *) frag_hdr - (u8 *) ip0;
952
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800953 ip6_reass_kv_t kv;
954 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200955
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800956 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
957 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
958 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
959 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
960 kv.k.as_u64[4] =
961 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
962 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
963 (u64) frag_hdr->identification;
964 kv.k.as_u64[5] = ip0->protocol;
965
966 ip6_reass_t *reass =
967 ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
968 &do_handoff);
969
970 if (PREDICT_FALSE (do_handoff))
971 {
972 next0 = IP6_REASSEMBLY_NEXT_HANDOFF;
973 if (is_feature)
974 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
975 kv.v.thread_index;
976 else
977 vnet_buffer (b0)->ip.reass.owner_thread_index =
978 kv.v.thread_index;
979 }
980 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200981 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100982 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100983 &error0, frag_hdr, is_feature))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100984 {
985 case IP6_REASS_RC_OK:
986 /* nothing to do here */
987 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100988 case IP6_REASS_RC_NO_BUF:
989 /* fallthrough */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100990 case IP6_REASS_RC_INTERNAL_ERROR:
991 /* drop everything and start with a clean slate */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100992 ip6_reass_drop_all (vm, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100993 ip6_reass_free (rm, rt, reass);
994 goto next_packet;
995 break;
996 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200997 }
998 else
999 {
1000 next0 = IP6_REASSEMBLY_NEXT_DROP;
1001 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1002 }
1003
1004 b0->error = node->errors[error0];
1005
1006 if (~0 != bi0)
1007 {
Klement Sekera4c533132018-02-22 11:41:12 +01001008 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001009 to_next[0] = bi0;
1010 to_next += 1;
1011 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001012 if (is_feature && IP6_ERROR_NONE == error0)
1013 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001014 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001015 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001016 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001017 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1018 n_left_to_next, bi0, next0);
1019 }
1020
1021 if (~0 != icmp_bi)
1022 {
1023 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1024 to_next[0] = icmp_bi;
1025 to_next += 1;
1026 n_left_to_next -= 1;
1027 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1028 n_left_to_next, icmp_bi,
1029 next0);
1030 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001031 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001032 from += 1;
1033 n_left_from -= 1;
1034 }
1035
1036 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1037 }
1038
Klement Sekera4c533132018-02-22 11:41:12 +01001039 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001040 return frame->n_vectors;
1041}
1042
1043static char *ip6_reassembly_error_strings[] = {
1044#define _(sym, string) string,
1045 foreach_ip6_error
1046#undef _
1047};
1048
Damjan Marion2574d9f2018-06-26 14:45:49 +02001049static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001050ip6_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node,
1051 vlib_frame_t * frame)
1052{
1053 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1054}
1055
Klement Sekera75e7d132017-09-20 08:26:30 +02001056/* *INDENT-OFF* */
1057VLIB_REGISTER_NODE (ip6_reass_node, static) = {
1058 .function = ip6_reassembly,
1059 .name = "ip6-reassembly",
1060 .vector_size = sizeof (u32),
1061 .format_trace = format_ip6_reass_trace,
1062 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1063 .error_strings = ip6_reassembly_error_strings,
1064 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1065 .next_nodes =
1066 {
1067 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1068 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1069 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001070 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001071 },
1072};
1073/* *INDENT-ON* */
1074
Klement Sekera4c533132018-02-22 11:41:12 +01001075VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node, ip6_reassembly);
1076
Damjan Marion2574d9f2018-06-26 14:45:49 +02001077static uword
Klement Sekera4c533132018-02-22 11:41:12 +01001078ip6_reassembly_feature (vlib_main_t * vm,
1079 vlib_node_runtime_t * node, vlib_frame_t * frame)
1080{
1081 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1082}
1083
1084/* *INDENT-OFF* */
1085VLIB_REGISTER_NODE (ip6_reass_node_feature, static) = {
1086 .function = ip6_reassembly_feature,
1087 .name = "ip6-reassembly-feature",
1088 .vector_size = sizeof (u32),
1089 .format_trace = format_ip6_reass_trace,
1090 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1091 .error_strings = ip6_reassembly_error_strings,
1092 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1093 .next_nodes =
1094 {
1095 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1096 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1097 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001098 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001099 },
1100};
1101/* *INDENT-ON* */
1102
1103VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node_feature, ip6_reassembly_feature);
1104
1105/* *INDENT-OFF* */
1106VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1107 .arc_name = "ip6-unicast",
1108 .node_name = "ip6-reassembly-feature",
1109 .runs_before = VNET_FEATURES ("ip6-lookup"),
1110 .runs_after = 0,
1111};
1112/* *INDENT-ON* */
1113
1114static u32
1115ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001116{
1117 ip6_reass_main_t *rm = &ip6_reass_main;
1118 u32 nbuckets;
1119 u8 i;
1120
1121 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1122
1123 for (i = 0; i < 31; i++)
1124 if ((1 << i) >= nbuckets)
1125 break;
1126 nbuckets = 1 << i;
1127
1128 return nbuckets;
1129}
1130
1131typedef enum
1132{
1133 IP6_EVENT_CONFIG_CHANGED = 1,
1134} ip6_reass_event_t;
1135
1136typedef struct
1137{
1138 int failure;
1139 clib_bihash_48_8_t *new_hash;
1140} ip6_rehash_cb_ctx;
1141
Klement Sekera4c533132018-02-22 11:41:12 +01001142static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001143ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1144{
1145 ip6_rehash_cb_ctx *ctx = _ctx;
1146 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1147 {
1148 ctx->failure = 1;
1149 }
1150}
1151
Klement Sekera4c533132018-02-22 11:41:12 +01001152static void
1153ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1154 u32 expire_walk_interval_ms)
1155{
1156 ip6_reass_main.timeout_ms = timeout_ms;
1157 ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1158 ip6_reass_main.max_reass_n = max_reassemblies;
1159 ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1160}
1161
Klement Sekera75e7d132017-09-20 08:26:30 +02001162vnet_api_error_t
1163ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
1164 u32 expire_walk_interval_ms)
1165{
1166 u32 old_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera4c533132018-02-22 11:41:12 +01001167 ip6_reass_set_params (timeout_ms, max_reassemblies,
1168 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001169 vlib_process_signal_event (ip6_reass_main.vlib_main,
1170 ip6_reass_main.ip6_reass_expire_node_idx,
1171 IP6_EVENT_CONFIG_CHANGED, 0);
1172 u32 new_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001173 if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001174 {
1175 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001176 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001177 ip6_rehash_cb_ctx ctx;
1178 ctx.failure = 0;
1179 ctx.new_hash = &new_hash;
1180 clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1181 new_nbuckets * 1024);
1182 clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1183 ip6_rehash_cb, &ctx);
1184 if (ctx.failure)
1185 {
1186 clib_bihash_free_48_8 (&new_hash);
1187 return -1;
1188 }
1189 else
1190 {
1191 clib_bihash_free_48_8 (&ip6_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001192 clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1193 sizeof (ip6_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001194 }
1195 }
1196 return 0;
1197}
1198
1199vnet_api_error_t
1200ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1201 u32 * expire_walk_interval_ms)
1202{
1203 *timeout_ms = ip6_reass_main.timeout_ms;
1204 *max_reassemblies = ip6_reass_main.max_reass_n;
1205 *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1206 return 0;
1207}
1208
Klement Sekera4c533132018-02-22 11:41:12 +01001209static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001210ip6_reass_init_function (vlib_main_t * vm)
1211{
1212 ip6_reass_main_t *rm = &ip6_reass_main;
1213 clib_error_t *error = 0;
1214 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001215 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001216
1217 rm->vlib_main = vm;
1218 rm->vnet_main = vnet_get_main ();
1219
Juraj Slobodacd806922018-10-10 10:15:54 +02001220 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001221 ip6_reass_per_thread_t *rt;
1222 vec_foreach (rt, rm->per_thread_data)
1223 {
1224 clib_spinlock_init (&rt->lock);
1225 pool_alloc (rt->pool, rm->max_reass_n);
1226 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001227
1228 node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1229 ASSERT (node);
1230 rm->ip6_reass_expire_node_idx = node->index;
1231
Klement Sekera3ecc2212018-03-27 10:34:43 +02001232 ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS,
1233 IP6_REASS_MAX_REASSEMBLIES_DEFAULT,
1234 IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1235
Klement Sekera75e7d132017-09-20 08:26:30 +02001236 nbuckets = ip6_reass_get_nbuckets ();
1237 clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1238
Dave Barach1403fcd2018-02-05 09:45:43 -05001239 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001240 ASSERT (node);
1241 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001242 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1243 ASSERT (node);
1244 rm->ip6_icmp_error_idx = node->index;
1245
1246 if ((error = vlib_call_init_function (vm, ip_main_init)))
1247 return error;
1248 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1249 ip6_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001250
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001251 rm->fq_index = vlib_frame_queue_main_init (ip6_reass_node.index, 0);
1252 rm->fq_feature_index =
1253 vlib_frame_queue_main_init (ip6_reass_node_feature.index, 0);
1254
Klement Sekera75e7d132017-09-20 08:26:30 +02001255 return error;
1256}
1257
1258VLIB_INIT_FUNCTION (ip6_reass_init_function);
1259
1260static uword
1261ip6_reass_walk_expired (vlib_main_t * vm,
1262 vlib_node_runtime_t * node, vlib_frame_t * f)
1263{
1264 ip6_reass_main_t *rm = &ip6_reass_main;
1265 uword event_type, *event_data = 0;
1266
1267 while (true)
1268 {
1269 vlib_process_wait_for_event_or_clock (vm,
1270 (f64) rm->expire_walk_interval_ms
1271 / (f64) MSEC_PER_SEC);
1272 event_type = vlib_process_get_events (vm, &event_data);
1273
1274 switch (event_type)
1275 {
1276 case ~0: /* no events => timeout */
1277 /* nothing to do here */
1278 break;
1279 case IP6_EVENT_CONFIG_CHANGED:
1280 break;
1281 default:
1282 clib_warning ("BUG: event type 0x%wx", event_type);
1283 break;
1284 }
1285 f64 now = vlib_time_now (vm);
1286
1287 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001288 int *pool_indexes_to_free = NULL;
1289
Klement Sekera4c533132018-02-22 11:41:12 +01001290 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001291 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001292 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001293 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001294 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1295 {
1296 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1297 clib_spinlock_lock (&rt->lock);
1298
1299 vec_reset_length (pool_indexes_to_free);
1300 /* *INDENT-OFF* */
1301 pool_foreach_index (index, rt->pool, ({
1302 reass = pool_elt_at_index (rt->pool, index);
1303 if (now > reass->last_heard + rm->timeout)
1304 {
1305 vec_add1 (pool_indexes_to_free, index);
1306 }
1307 }));
1308 /* *INDENT-ON* */
1309 int *i;
1310 /* *INDENT-OFF* */
1311 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001312 {
Klement Sekera4c533132018-02-22 11:41:12 +01001313 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1314 u32 icmp_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +01001315 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1316 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1317 {
1318 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1319 b->trace_index))
1320 {
1321 /* the trace is gone, don't trace this buffer anymore */
1322 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1323 }
1324 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001325 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001326 if (~0 != icmp_bi)
1327 {
1328 vec_add1 (vec_icmp_bi, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001329 }
1330 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001331 }
Klement Sekera4c533132018-02-22 11:41:12 +01001332 /* *INDENT-ON* */
1333
1334 clib_spinlock_unlock (&rt->lock);
1335 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001336
Klement Sekera75e7d132017-09-20 08:26:30 +02001337 while (vec_len (vec_icmp_bi) > 0)
1338 {
1339 vlib_frame_t *f =
1340 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1341 u32 *to_next = vlib_frame_vector_args (f);
1342 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001343 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001344 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1345 {
1346 u32 bi = vec_pop (vec_icmp_bi);
1347 vlib_buffer_t *b = vlib_get_buffer (vm, 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 else
1357 {
Klement Sekera4c533132018-02-22 11:41:12 +01001358 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001359 }
1360 }
1361 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1362 to_next[0] = bi;
1363 ++f->n_vectors;
1364 to_next += 1;
1365 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001366 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001367 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001368 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1369 }
1370
1371 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001372 vec_free (vec_icmp_bi);
1373 if (event_data)
1374 {
1375 _vec_len (event_data) = 0;
1376 }
1377 }
1378
1379 return 0;
1380}
1381
1382static vlib_node_registration_t ip6_reass_expire_node;
1383
1384/* *INDENT-OFF* */
1385VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1386 .function = ip6_reass_walk_expired,
1387 .format_trace = format_ip6_reass_trace,
1388 .type = VLIB_NODE_TYPE_PROCESS,
1389 .name = "ip6-reassembly-expire-walk",
1390
1391 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1392 .error_strings = ip6_reassembly_error_strings,
1393
1394};
1395/* *INDENT-ON* */
1396
1397static u8 *
1398format_ip6_reass_key (u8 * s, va_list * args)
1399{
1400 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1401 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1402 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1403 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1404 return s;
1405}
1406
1407static u8 *
1408format_ip6_reass (u8 * s, va_list * args)
1409{
1410 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1411 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1412
Klement Sekera4c533132018-02-22 11:41:12 +01001413 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001414 "last_packet_octet: %u, trace_op_counter: %u\n",
1415 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1416 reass->data_len, reass->last_packet_octet,
1417 reass->trace_op_counter);
1418 u32 bi = reass->first_bi;
1419 u32 counter = 0;
1420 while (~0 != bi)
1421 {
1422 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1423 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1424 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1425 "fragment[%u, %u]\n",
1426 counter, vnb->ip.reass.range_first,
1427 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001428 ip6_reass_buffer_get_data_offset (b),
1429 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001430 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1431 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1432 {
1433 bi = b->next_buffer;
1434 }
1435 else
1436 {
1437 bi = ~0;
1438 }
1439 }
1440 return s;
1441}
1442
1443static clib_error_t *
1444show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1445 CLIB_UNUSED (vlib_cli_command_t * lmd))
1446{
1447 ip6_reass_main_t *rm = &ip6_reass_main;
1448
1449 vlib_cli_output (vm, "---------------------");
1450 vlib_cli_output (vm, "IP6 reassembly status");
1451 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001452 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001453 if (unformat (input, "details"))
1454 {
Klement Sekera4c533132018-02-22 11:41:12 +01001455 details = true;
1456 }
1457
1458 u32 sum_reass_n = 0;
1459 u64 sum_buffers_n = 0;
1460 ip6_reass_t *reass;
1461 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001462 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001463 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1464 {
1465 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1466 clib_spinlock_lock (&rt->lock);
1467 if (details)
1468 {
1469 /* *INDENT-OFF* */
1470 pool_foreach (reass, rt->pool, {
1471 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1472 });
1473 /* *INDENT-ON* */
1474 }
1475 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001476 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001477 }
1478 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001479 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1480 (long unsigned) sum_reass_n);
1481 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1482 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001483 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001484 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001485 return 0;
1486}
1487
1488/* *INDENT-OFF* */
1489VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1490 .path = "show ip6-reassembly",
1491 .short_help = "show ip6-reassembly [details]",
1492 .function = show_ip6_reass,
1493};
1494/* *INDENT-ON* */
1495
Klement Sekera4c533132018-02-22 11:41:12 +01001496vnet_api_error_t
1497ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1498{
1499 return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1500 sw_if_index, enable_disable, 0, 0);
1501}
1502
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001503#define foreach_ip6_reassembly_handoff_error \
1504_(CONGESTION_DROP, "congestion drop")
1505
1506
1507typedef enum
1508{
1509#define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1510 foreach_ip6_reassembly_handoff_error
1511#undef _
1512 IP6_REASSEMBLY_HANDOFF_N_ERROR,
1513} ip6_reassembly_handoff_error_t;
1514
1515static char *ip6_reassembly_handoff_error_strings[] = {
1516#define _(sym,string) string,
1517 foreach_ip6_reassembly_handoff_error
1518#undef _
1519};
1520
1521typedef struct
1522{
1523 u32 next_worker_index;
1524} ip6_reassembly_handoff_trace_t;
1525
1526static u8 *
1527format_ip6_reassembly_handoff_trace (u8 * s, va_list * args)
1528{
1529 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1530 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1531 ip6_reassembly_handoff_trace_t *t =
1532 va_arg (*args, ip6_reassembly_handoff_trace_t *);
1533
1534 s =
1535 format (s, "ip6-reassembly-handoff: next-worker %d",
1536 t->next_worker_index);
1537
1538 return s;
1539}
1540
1541always_inline uword
1542ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1543 vlib_frame_t * frame, bool is_feature)
1544{
1545 ip6_reass_main_t *rm = &ip6_reass_main;
1546
1547 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1548 u32 n_enq, n_left_from, *from;
1549 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1550 u32 fq_index;
1551
1552 from = vlib_frame_vector_args (frame);
1553 n_left_from = frame->n_vectors;
1554 vlib_get_buffers (vm, from, bufs, n_left_from);
1555
1556 b = bufs;
1557 ti = thread_indices;
1558
1559 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1560
1561 while (n_left_from > 0)
1562 {
1563 ti[0] =
1564 (is_feature) ? vnet_buffer (b[0])->ip.
1565 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1566 reass.owner_thread_index;
1567
1568 if (PREDICT_FALSE
1569 ((node->flags & VLIB_NODE_FLAG_TRACE)
1570 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1571 {
1572 ip6_reassembly_handoff_trace_t *t =
1573 vlib_add_trace (vm, node, b[0], sizeof (*t));
1574 t->next_worker_index = ti[0];
1575 }
1576
1577 n_left_from -= 1;
1578 ti += 1;
1579 b += 1;
1580 }
1581 n_enq =
1582 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1583 frame->n_vectors, 1);
1584
1585 if (n_enq < frame->n_vectors)
1586 vlib_node_increment_counter (vm, node->node_index,
1587 IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1588 frame->n_vectors - n_enq);
1589 return frame->n_vectors;
1590}
1591
1592VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm,
1593 vlib_node_runtime_t * node,
1594 vlib_frame_t * frame)
1595{
1596 return ip6_reassembly_handoff_inline (vm, node, frame,
1597 false /* is_feature */ );
1598}
1599
1600/* *INDENT-OFF* */
1601VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = {
1602 .name = "ip6-reassembly-handoff",
1603 .vector_size = sizeof (u32),
1604 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1605 .error_strings = ip6_reassembly_handoff_error_strings,
1606 .format_trace = format_ip6_reassembly_handoff_trace,
1607
1608 .n_next_nodes = 1,
1609
1610 .next_nodes = {
1611 [0] = "error-drop",
1612 },
1613};
1614
1615
1616VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm,
1617 vlib_node_runtime_t * node, vlib_frame_t * frame)
1618{
1619 return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1620}
1621
1622
1623/* *INDENT-OFF* */
1624VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = {
1625 .name = "ip6-reass-feature-hoff",
1626 .vector_size = sizeof (u32),
1627 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1628 .error_strings = ip6_reassembly_handoff_error_strings,
1629 .format_trace = format_ip6_reassembly_handoff_trace,
1630
1631 .n_next_nodes = 1,
1632
1633 .next_nodes = {
1634 [0] = "error-drop",
1635 },
1636};
1637/* *INDENT-ON* */
1638
Klement Sekera75e7d132017-09-20 08:26:30 +02001639/*
1640 * fd.io coding-style-patch-verification: ON
1641 *
1642 * Local Variables:
1643 * eval: (c-set-style "gnu")
1644 * End:
1645 */