blob: 9906250cb0f85ae205cae15f9675f8f96b8b293c [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
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700154extern ip6_reass_main_t ip6_reass_main;
155
156#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +0200157ip6_reass_main_t ip6_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700158#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200159
160typedef enum
161{
162 IP6_REASSEMBLY_NEXT_INPUT,
163 IP6_REASSEMBLY_NEXT_DROP,
164 IP6_REASSEMBLY_NEXT_ICMP_ERROR,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800165 IP6_REASSEMBLY_NEXT_HANDOFF,
Klement Sekera75e7d132017-09-20 08:26:30 +0200166 IP6_REASSEMBLY_N_NEXT,
167} ip6_reass_next_t;
168
169typedef enum
170{
171 RANGE_NEW,
172 RANGE_OVERLAP,
173 ICMP_ERROR_RT_EXCEEDED,
174 ICMP_ERROR_FL_TOO_BIG,
175 ICMP_ERROR_FL_NOT_MULT_8,
176 FINALIZE,
177} ip6_reass_trace_operation_e;
178
179typedef struct
180{
181 u16 range_first;
182 u16 range_last;
183 u32 range_bi;
184 i32 data_offset;
185 u32 data_len;
186 u32 first_bi;
187} ip6_reass_range_trace_t;
188
189typedef struct
190{
191 ip6_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200192 u32 reass_id;
193 ip6_reass_range_trace_t trace_range;
194 u32 size_diff;
195 u32 op_id;
196 u32 fragment_first;
197 u32 fragment_last;
198 u32 total_data_len;
199} ip6_reass_trace_t;
200
201static void
202ip6_reass_trace_details (vlib_main_t * vm, u32 bi,
203 ip6_reass_range_trace_t * trace)
204{
205 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
206 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
207 trace->range_first = vnb->ip.reass.range_first;
208 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100209 trace->data_offset = ip6_reass_buffer_get_data_offset (b);
210 trace->data_len = ip6_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200211 trace->range_bi = bi;
212}
213
Klement Sekera4c533132018-02-22 11:41:12 +0100214static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200215format_ip6_reass_range_trace (u8 * s, va_list * args)
216{
217 ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *);
218 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
219 trace->range_last, trace->data_offset, trace->data_len,
220 trace->range_bi);
221 return s;
222}
223
Klement Sekera4c533132018-02-22 11:41:12 +0100224static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200225format_ip6_reass_trace (u8 * s, va_list * args)
226{
227 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
228 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
229 ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
230 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
231 u32 indent = format_get_indent (s);
232 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
233 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
234 t->fragment_last);
235 switch (t->action)
236 {
237 case RANGE_NEW:
238 s = format (s, "\n%Unew %U", format_white_space, indent,
239 format_ip6_reass_range_trace, &t->trace_range);
240 break;
241 case RANGE_OVERLAP:
242 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
243 format_ip6_reass_range_trace, &t->trace_range);
244 break;
245 case ICMP_ERROR_FL_TOO_BIG:
246 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
247 format_white_space, indent, format_ip6_reass_range_trace,
248 &t->trace_range);
249 break;
250 case ICMP_ERROR_FL_NOT_MULT_8:
251 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
252 format_white_space, indent, format_ip6_reass_range_trace,
253 &t->trace_range);
254 break;
255 case ICMP_ERROR_RT_EXCEEDED:
256 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
257 format_white_space, indent);
258 break;
259 case FINALIZE:
260 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
261 break;
262 }
263 return s;
264}
265
266static void
267ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
268 ip6_reass_main_t * rm, ip6_reass_t * reass,
269 u32 bi, ip6_reass_trace_operation_e action,
270 u32 size_diff)
271{
272 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
273 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100274 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
275 {
276 // this buffer's trace is gone
277 b->flags &= ~VLIB_BUFFER_IS_TRACED;
278 return;
279 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200280 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200281 t->reass_id = reass->id;
282 t->action = action;
283 ip6_reass_trace_details (vm, bi, &t->trace_range);
284 t->size_diff = size_diff;
285 t->op_id = reass->trace_op_counter;
286 ++reass->trace_op_counter;
287 t->fragment_first = vnb->ip.reass.fragment_first;
288 t->fragment_last = vnb->ip.reass.fragment_last;
289 t->trace_range.first_bi = reass->first_bi;
290 t->total_data_len = reass->data_len;
291#if 0
292 static u8 *s = NULL;
293 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
294 printf ("%.*s\n", vec_len (s), s);
295 fflush (stdout);
296 vec_reset_length (s);
297#endif
298}
299
Klement Sekera4c533132018-02-22 11:41:12 +0100300always_inline void
301ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
302 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200303{
304 clib_bihash_kv_48_8_t kv;
305 kv.key[0] = reass->key.as_u64[0];
306 kv.key[1] = reass->key.as_u64[1];
307 kv.key[2] = reass->key.as_u64[2];
308 kv.key[3] = reass->key.as_u64[3];
309 kv.key[4] = reass->key.as_u64[4];
310 kv.key[5] = reass->key.as_u64[5];
311 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100312 pool_put (rt->pool, reass);
313 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200314}
315
Klement Sekera4c533132018-02-22 11:41:12 +0100316always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200317ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100318 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200319{
320 u32 range_bi = reass->first_bi;
321 vlib_buffer_t *range_b;
322 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100323 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200324 while (~0 != range_bi)
325 {
326 range_b = vlib_get_buffer (vm, range_bi);
327 range_vnb = vnet_buffer (range_b);
328 u32 bi = range_bi;
329 while (~0 != bi)
330 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100331 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200332 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
333 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
334 {
335 bi = b->next_buffer;
336 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
337 }
338 else
339 {
340 bi = ~0;
341 }
342 }
343 range_bi = range_vnb->ip.reass.next_range_bi;
344 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100345 vlib_buffer_free (vm, to_free, vec_len (to_free));
346 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200347}
348
Klement Sekera4c533132018-02-22 11:41:12 +0100349always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200350ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100351 ip6_reass_main_t * rm, ip6_reass_t * reass,
352 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200353{
354 if (~0 == reass->first_bi)
355 {
356 return;
357 }
358 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
359 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
360 {
361 *icmp_bi = reass->first_bi;
362 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
363 {
364 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
365 ICMP_ERROR_RT_EXCEEDED, 0);
366 }
367 // fragment with offset zero received - send icmp message back
368 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
369 {
370 // separate first buffer from chain and steer it towards icmp node
371 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
372 reass->first_bi = b->next_buffer;
373 }
374 else
375 {
376 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
377 }
378 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
379 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
380 0);
381 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100382 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200383}
384
Klement Sekera4c533132018-02-22 11:41:12 +0100385always_inline ip6_reass_t *
386ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
387 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800388 ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200389{
390 ip6_reass_t *reass = NULL;
391 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200392
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800393 if (!clib_bihash_search_48_8
394 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200395 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800396 if (vm->thread_index != kv->v.thread_index)
397 {
398 *do_handoff = 1;
399 return NULL;
400 }
401 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
402
Klement Sekera75e7d132017-09-20 08:26:30 +0200403 if (now > reass->last_heard + rm->timeout)
404 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100405 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100406 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200407 reass = NULL;
408 }
409 }
410
411 if (reass)
412 {
413 reass->last_heard = now;
414 return reass;
415 }
416
Klement Sekera4c533132018-02-22 11:41:12 +0100417 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200418 {
419 reass = NULL;
420 return reass;
421 }
422 else
423 {
Klement Sekera4c533132018-02-22 11:41:12 +0100424 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400425 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800426 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100427 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200428 reass->first_bi = ~0;
429 reass->last_packet_octet = ~0;
430 reass->data_len = 0;
Klement Sekera4c533132018-02-22 11:41:12 +0100431 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200432 }
433
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800434 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
435 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
436 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
437 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
438 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
439 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
440 kv->v.reass_index = (reass - rt->pool);
441 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200442 reass->last_heard = now;
443
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800444 if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200445 {
Klement Sekera4c533132018-02-22 11:41:12 +0100446 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200447 reass = NULL;
448 }
449
450 return reass;
451}
452
Klement Sekerad0f70a32018-12-14 17:24:13 +0100453always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200454ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100455 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100456 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
457 bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200458{
Klement Sekera75e7d132017-09-20 08:26:30 +0200459 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200460 *error0 = IP6_ERROR_NONE;
461 ip6_frag_hdr_t *frag_hdr;
462 vlib_buffer_t *last_b = NULL;
463 u32 sub_chain_bi = reass->first_bi;
464 u32 total_length = 0;
465 u32 buf_cnt = 0;
466 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100467 u32 *vec_drop_compress = NULL;
468 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200469 do
470 {
471 u32 tmp_bi = sub_chain_bi;
472 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100473 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
474 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
475 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
476 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100477 rv = IP6_REASS_RC_INTERNAL_ERROR;
478 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100479 }
480
Klement Sekera75e7d132017-09-20 08:26:30 +0200481 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
482 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
483 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
484 u32 trim_end =
485 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
486 if (tmp_bi == reass->first_bi)
487 {
488 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100489 if (0 != ip6_reass_buffer_get_data_offset (tmp))
490 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100491 rv = IP6_REASS_RC_INTERNAL_ERROR;
492 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100493 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200494 trim_front = 0;
495 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
496 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
497 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100498 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
499 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100500 rv = IP6_REASS_RC_INTERNAL_ERROR;
501 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100502 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200503 }
504 u32 keep_data =
505 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
506 while (1)
507 {
508 ++buf_cnt;
509 if (trim_front)
510 {
511 if (trim_front > tmp->current_length)
512 {
513 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100514 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200515 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100516 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
517 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100518 rv = IP6_REASS_RC_INTERNAL_ERROR;
519 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100520 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200521 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
522 tmp_bi = tmp->next_buffer;
523 tmp = vlib_get_buffer (vm, tmp_bi);
524 continue;
525 }
526 else
527 {
528 vlib_buffer_advance (tmp, trim_front);
529 trim_front = 0;
530 }
531 }
532 if (keep_data)
533 {
534 if (last_b)
535 {
536 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
537 last_b->next_buffer = tmp_bi;
538 }
539 last_b = tmp;
540 if (keep_data <= tmp->current_length)
541 {
542 tmp->current_length = keep_data;
543 keep_data = 0;
544 }
545 else
546 {
547 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100548 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
549 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100550 rv = IP6_REASS_RC_INTERNAL_ERROR;
551 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100552 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200553 }
554 total_length += tmp->current_length;
555 }
556 else
557 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100558 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100559 if (reass->first_bi == tmp_bi)
560 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100561 rv = IP6_REASS_RC_INTERNAL_ERROR;
562 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100563 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200564 ++dropped_cnt;
565 }
566 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
567 {
568 tmp_bi = tmp->next_buffer;
569 tmp = vlib_get_buffer (vm, tmp->next_buffer);
570 }
571 else
572 {
573 break;
574 }
575 }
576 sub_chain_bi =
577 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
578 reass.next_range_bi;
579 }
580 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700581
Klement Sekerad0f70a32018-12-14 17:24:13 +0100582 if (!last_b)
583 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100584 rv = IP6_REASS_RC_INTERNAL_ERROR;
585 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100586 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200587 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
588 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100589 if (total_length < first_b->current_length)
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 total_length -= first_b->current_length;
595 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
596 first_b->total_length_not_including_first_buffer = total_length;
597 // drop fragment header
598 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
599 ip6_header_t *ip = vlib_buffer_get_current (first_b);
600 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
601 ip6_ext_header_t *prev_hdr;
602 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
603 IP_PROTOCOL_IPV6_FRAGMENTATION);
604 if (prev_hdr)
605 {
606 prev_hdr->next_hdr = frag_hdr->next_hdr;
607 }
608 else
609 {
610 ip->protocol = frag_hdr->next_hdr;
611 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100612 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
613 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100614 rv = IP6_REASS_RC_INTERNAL_ERROR;
615 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100616 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200617 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
618 first_b->current_length - ip6_frag_hdr_offset -
619 sizeof (ip6_frag_hdr_t));
620 first_b->current_length -= sizeof (*frag_hdr);
621 ip->payload_length =
622 clib_host_to_net_u16 (total_length + first_b->current_length -
623 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100624 if (!vlib_buffer_chain_linearize (vm, first_b))
625 {
626 rv = IP6_REASS_RC_NO_BUF;
627 goto free_buffers_and_return;
628 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200629 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
630 {
631 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
632#if 0
633 // following code does a hexdump of packet fragments to stdout ...
634 do
635 {
636 u32 bi = reass->first_bi;
637 u8 *s = NULL;
638 while (~0 != bi)
639 {
640 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
641 s = format (s, "%u: %U\n", bi, format_hexdump,
642 vlib_buffer_get_current (b), b->current_length);
643 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
644 {
645 bi = b->next_buffer;
646 }
647 else
648 {
649 break;
650 }
651 }
652 printf ("%.*s\n", vec_len (s), s);
653 fflush (stdout);
654 vec_free (s);
655 }
656 while (0);
657#endif
658 }
Klement Sekera4c533132018-02-22 11:41:12 +0100659 if (is_feature)
660 {
661 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
662 }
663 else
664 {
665 *next0 = reass->next_index;
666 }
667 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
668 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200669 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100670free_buffers_and_return:
671 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
672 vec_free (vec_drop_compress);
673 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200674}
675
Klement Sekera4c533132018-02-22 11:41:12 +0100676always_inline void
677ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
678 ip6_reass_per_thread_t * rt,
679 ip6_reass_t * reass, u32 prev_range_bi,
680 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200681{
682
683 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
684 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
685 if (~0 != prev_range_bi)
686 {
687 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
688 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
689 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
690 prev_vnb->ip.reass.next_range_bi = new_next_bi;
691 }
692 else
693 {
694 if (~0 != reass->first_bi)
695 {
696 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
697 }
698 reass->first_bi = new_next_bi;
699 }
700 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200701}
702
Klement Sekerad0f70a32018-12-14 17:24:13 +0100703always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200704ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100705 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100706 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
707 ip6_frag_hdr_t * frag_hdr, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200708{
709 int consumed = 0;
710 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
711 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Vijayabhaskar Katamreddyb3610762018-06-29 05:03:40 -0700712 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
Klement Sekera75e7d132017-09-20 08:26:30 +0200713 fvnb->ip.reass.ip6_frag_hdr_offset =
714 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
715 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100716 if (fb->current_length < sizeof (*fip) ||
717 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
718 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
719 {
720 return IP6_REASS_RC_INTERNAL_ERROR;
721 }
722
Klement Sekera75e7d132017-09-20 08:26:30 +0200723 u32 fragment_first = fvnb->ip.reass.fragment_first =
724 ip6_frag_hdr_offset_bytes (frag_hdr);
725 u32 fragment_length =
726 vlib_buffer_length_in_chain (vm, fb) -
727 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
728 u32 fragment_last = fvnb->ip.reass.fragment_last =
729 fragment_first + fragment_length - 1;
730 int more_fragments = ip6_frag_hdr_more (frag_hdr);
731 u32 candidate_range_bi = reass->first_bi;
732 u32 prev_range_bi = ~0;
733 fvnb->ip.reass.range_first = fragment_first;
734 fvnb->ip.reass.range_last = fragment_last;
735 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200736 if (!more_fragments)
737 {
738 reass->last_packet_octet = fragment_last;
739 }
740 if (~0 == reass->first_bi)
741 {
742 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100743 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
744 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100745 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100746 consumed = 1;
747 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200748 }
Klement Sekera4c533132018-02-22 11:41:12 +0100749 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200750 clib_min (clib_net_to_host_u16 (fip->payload_length),
751 fvnb->ip.reass.estimated_mtu);
752 while (~0 != candidate_range_bi)
753 {
754 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
755 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
756 if (fragment_first > candidate_vnb->ip.reass.range_last)
757 {
758 // this fragments starts after candidate range
759 prev_range_bi = candidate_range_bi;
760 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
761 if (candidate_vnb->ip.reass.range_last < fragment_last &&
762 ~0 == candidate_range_bi)
763 {
764 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100765 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
766 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200767 consumed = 1;
768 break;
769 }
770 continue;
771 }
772 if (fragment_last < candidate_vnb->ip.reass.range_first)
773 {
774 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100775 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200776 *bi0);
777 consumed = 1;
778 }
779 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
780 fragment_last == candidate_vnb->ip.reass.range_last)
781 {
782 // duplicate fragment - ignore
783 }
784 else
785 {
786 // overlapping fragment - not allowed by RFC 8200
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100787 ip6_reass_drop_all (vm, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100788 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200789 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
790 {
791 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
792 0);
793 }
Klement Sekera4c533132018-02-22 11:41:12 +0100794 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200795 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100796 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200797 }
798 break;
799 }
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100800check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200801 if (consumed)
802 {
803 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
804 {
805 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
806 }
807 }
808 if (~0 != reass->last_packet_octet &&
809 reass->data_len == reass->last_packet_octet + 1)
810 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100811 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100812 is_feature);
Klement Sekera75e7d132017-09-20 08:26:30 +0200813 }
814 else
815 {
816 if (consumed)
817 {
818 *bi0 = ~0;
819 }
820 else
821 {
Klement Sekera4c533132018-02-22 11:41:12 +0100822 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200823 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
824 }
825 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100826 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200827}
828
Klement Sekera4c533132018-02-22 11:41:12 +0100829always_inline bool
830ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
831 vlib_buffer_t * b,
832 ip6_frag_hdr_t * frag_hdr)
833{
834 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
835 while (ip6_ext_hdr (tmp->next_hdr))
836 {
837 tmp = ip6_ext_next_header (tmp);
838 }
839 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
840 {
841 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
842 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
843 0);
844 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
845
846 return false;
847 }
848 return true;
849}
850
851always_inline bool
852ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
853 vlib_node_runtime_t * node,
854 vlib_buffer_t * b,
855 ip6_frag_hdr_t * frag_hdr)
856{
857 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
858 ip6_header_t *ip = vlib_buffer_get_current (b);
859 int more_fragments = ip6_frag_hdr_more (frag_hdr);
860 u32 fragment_length =
861 vlib_buffer_length_in_chain (vm, b) -
862 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
863 if (more_fragments && 0 != fragment_length % 8)
864 {
865 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
866 ICMP6_parameter_problem_erroneous_header_field,
867 (u8 *) & ip->payload_length - (u8 *) ip);
868 return false;
869 }
870 return true;
871}
872
873always_inline bool
874ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
875 vlib_node_runtime_t * node,
876 vlib_buffer_t * b,
877 ip6_frag_hdr_t * frag_hdr)
878{
879 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
880 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
881 u32 fragment_length =
882 vlib_buffer_length_in_chain (vm, b) -
883 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
884 if (fragment_first + fragment_length > 65535)
885 {
886 ip6_header_t *ip0 = vlib_buffer_get_current (b);
887 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
888 ICMP6_parameter_problem_erroneous_header_field,
889 (u8 *) & frag_hdr->fragment_offset_and_more
890 - (u8 *) ip0);
891 return false;
892 }
893 return true;
894}
895
Klement Sekera75e7d132017-09-20 08:26:30 +0200896always_inline uword
Klement Sekera4c533132018-02-22 11:41:12 +0100897ip6_reassembly_inline (vlib_main_t * vm,
898 vlib_node_runtime_t * node,
899 vlib_frame_t * frame, bool is_feature)
Klement Sekera75e7d132017-09-20 08:26:30 +0200900{
901 u32 *from = vlib_frame_vector_args (frame);
902 u32 n_left_from, n_left_to_next, *to_next, next_index;
903 ip6_reass_main_t *rm = &ip6_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800904 ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +0100905 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200906
907 n_left_from = frame->n_vectors;
908 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100909 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200910 {
911 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
912
Klement Sekera75e7d132017-09-20 08:26:30 +0200913 while (n_left_from > 0 && n_left_to_next > 0)
914 {
915 u32 bi0;
916 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100917 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100918 u32 error0 = IP6_ERROR_NONE;
919 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200920
921 bi0 = from[0];
922 b0 = vlib_get_buffer (vm, bi0);
923
924 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200925 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200926 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200927 if (ip6_ext_hdr (ip0->protocol))
928 {
929 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
930 IP_PROTOCOL_IPV6_FRAGMENTATION);
931 }
Klement Sekera4c533132018-02-22 11:41:12 +0100932 if (!frag_hdr)
933 {
934 // this is a regular packet - no fragmentation
935 next0 = IP6_REASSEMBLY_NEXT_INPUT;
936 goto skip_reass;
937 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200938 if (0 == ip6_frag_hdr_offset (frag_hdr))
939 {
940 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100941 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +0200942 {
Klement Sekera4c533132018-02-22 11:41:12 +0100943 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
944 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200945 }
Klement Sekera4c533132018-02-22 11:41:12 +0100946 }
947 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
948 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
949 frag_hdr))
950 {
951 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
952 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200953 }
954 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
955 (u8 *) frag_hdr - (u8 *) ip0;
956
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800957 ip6_reass_kv_t kv;
958 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200959
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800960 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
961 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
962 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
963 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
964 kv.k.as_u64[4] =
965 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
966 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
967 (u64) frag_hdr->identification;
968 kv.k.as_u64[5] = ip0->protocol;
969
970 ip6_reass_t *reass =
971 ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
972 &do_handoff);
973
974 if (PREDICT_FALSE (do_handoff))
975 {
976 next0 = IP6_REASSEMBLY_NEXT_HANDOFF;
977 if (is_feature)
978 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
979 kv.v.thread_index;
980 else
981 vnet_buffer (b0)->ip.reass.owner_thread_index =
982 kv.v.thread_index;
983 }
984 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200985 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100986 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100987 &error0, frag_hdr, is_feature))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100988 {
989 case IP6_REASS_RC_OK:
990 /* nothing to do here */
991 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100992 case IP6_REASS_RC_NO_BUF:
993 /* fallthrough */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100994 case IP6_REASS_RC_INTERNAL_ERROR:
995 /* drop everything and start with a clean slate */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100996 ip6_reass_drop_all (vm, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100997 ip6_reass_free (rm, rt, reass);
998 goto next_packet;
999 break;
1000 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001001 }
1002 else
1003 {
1004 next0 = IP6_REASSEMBLY_NEXT_DROP;
1005 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1006 }
1007
1008 b0->error = node->errors[error0];
1009
1010 if (~0 != bi0)
1011 {
Klement Sekera4c533132018-02-22 11:41:12 +01001012 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001013 to_next[0] = bi0;
1014 to_next += 1;
1015 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001016 if (is_feature && IP6_ERROR_NONE == error0)
1017 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001018 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001019 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001020 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001021 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1022 n_left_to_next, bi0, next0);
1023 }
1024
1025 if (~0 != icmp_bi)
1026 {
1027 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1028 to_next[0] = icmp_bi;
1029 to_next += 1;
1030 n_left_to_next -= 1;
1031 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1032 n_left_to_next, icmp_bi,
1033 next0);
1034 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001035 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001036 from += 1;
1037 n_left_from -= 1;
1038 }
1039
1040 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1041 }
1042
Klement Sekera4c533132018-02-22 11:41:12 +01001043 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001044 return frame->n_vectors;
1045}
1046
1047static char *ip6_reassembly_error_strings[] = {
1048#define _(sym, string) string,
1049 foreach_ip6_error
1050#undef _
1051};
1052
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001053VLIB_NODE_FN (ip6_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1054 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001055{
1056 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1057}
1058
Klement Sekera75e7d132017-09-20 08:26:30 +02001059/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001060VLIB_REGISTER_NODE (ip6_reass_node) = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001061 .name = "ip6-reassembly",
1062 .vector_size = sizeof (u32),
1063 .format_trace = format_ip6_reass_trace,
1064 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1065 .error_strings = ip6_reassembly_error_strings,
1066 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1067 .next_nodes =
1068 {
1069 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1070 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1071 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001072 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001073 },
1074};
1075/* *INDENT-ON* */
1076
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001077VLIB_NODE_FN (ip6_reass_node_feature) (vlib_main_t * vm,
1078 vlib_node_runtime_t * node,
1079 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001080{
1081 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1082}
1083
1084/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001085VLIB_REGISTER_NODE (ip6_reass_node_feature) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001086 .name = "ip6-reassembly-feature",
1087 .vector_size = sizeof (u32),
1088 .format_trace = format_ip6_reass_trace,
1089 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1090 .error_strings = ip6_reassembly_error_strings,
1091 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1092 .next_nodes =
1093 {
1094 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1095 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1096 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001097 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001098 },
1099};
1100/* *INDENT-ON* */
1101
Klement Sekera4c533132018-02-22 11:41:12 +01001102/* *INDENT-OFF* */
1103VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1104 .arc_name = "ip6-unicast",
1105 .node_name = "ip6-reassembly-feature",
1106 .runs_before = VNET_FEATURES ("ip6-lookup"),
1107 .runs_after = 0,
1108};
1109/* *INDENT-ON* */
1110
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001111#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001112static u32
1113ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001114{
1115 ip6_reass_main_t *rm = &ip6_reass_main;
1116 u32 nbuckets;
1117 u8 i;
1118
1119 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1120
1121 for (i = 0; i < 31; i++)
1122 if ((1 << i) >= nbuckets)
1123 break;
1124 nbuckets = 1 << i;
1125
1126 return nbuckets;
1127}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001128#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001129
1130typedef enum
1131{
1132 IP6_EVENT_CONFIG_CHANGED = 1,
1133} ip6_reass_event_t;
1134
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001135#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001136typedef 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);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001259#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001260
1261static uword
1262ip6_reass_walk_expired (vlib_main_t * vm,
1263 vlib_node_runtime_t * node, vlib_frame_t * f)
1264{
1265 ip6_reass_main_t *rm = &ip6_reass_main;
1266 uword event_type, *event_data = 0;
1267
1268 while (true)
1269 {
1270 vlib_process_wait_for_event_or_clock (vm,
1271 (f64) rm->expire_walk_interval_ms
1272 / (f64) MSEC_PER_SEC);
1273 event_type = vlib_process_get_events (vm, &event_data);
1274
1275 switch (event_type)
1276 {
1277 case ~0: /* no events => timeout */
1278 /* nothing to do here */
1279 break;
1280 case IP6_EVENT_CONFIG_CHANGED:
1281 break;
1282 default:
1283 clib_warning ("BUG: event type 0x%wx", event_type);
1284 break;
1285 }
1286 f64 now = vlib_time_now (vm);
1287
1288 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001289 int *pool_indexes_to_free = NULL;
1290
Klement Sekera4c533132018-02-22 11:41:12 +01001291 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001292 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001293 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001294 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001295 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1296 {
1297 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1298 clib_spinlock_lock (&rt->lock);
1299
1300 vec_reset_length (pool_indexes_to_free);
1301 /* *INDENT-OFF* */
1302 pool_foreach_index (index, rt->pool, ({
1303 reass = pool_elt_at_index (rt->pool, index);
1304 if (now > reass->last_heard + rm->timeout)
1305 {
1306 vec_add1 (pool_indexes_to_free, index);
1307 }
1308 }));
1309 /* *INDENT-ON* */
1310 int *i;
1311 /* *INDENT-OFF* */
1312 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001313 {
Klement Sekera4c533132018-02-22 11:41:12 +01001314 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1315 u32 icmp_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +01001316 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1317 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1318 {
1319 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1320 b->trace_index))
1321 {
1322 /* the trace is gone, don't trace this buffer anymore */
1323 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1324 }
1325 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001326 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001327 if (~0 != icmp_bi)
1328 {
1329 vec_add1 (vec_icmp_bi, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001330 }
1331 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001332 }
Klement Sekera4c533132018-02-22 11:41:12 +01001333 /* *INDENT-ON* */
1334
1335 clib_spinlock_unlock (&rt->lock);
1336 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001337
Klement Sekera75e7d132017-09-20 08:26:30 +02001338 while (vec_len (vec_icmp_bi) > 0)
1339 {
1340 vlib_frame_t *f =
1341 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1342 u32 *to_next = vlib_frame_vector_args (f);
1343 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001344 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001345 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1346 {
1347 u32 bi = vec_pop (vec_icmp_bi);
1348 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1349 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1350 {
1351 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1352 b->trace_index))
1353 {
1354 /* the trace is gone, don't trace this buffer anymore */
1355 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1356 }
1357 else
1358 {
Klement Sekera4c533132018-02-22 11:41:12 +01001359 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001360 }
1361 }
1362 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1363 to_next[0] = bi;
1364 ++f->n_vectors;
1365 to_next += 1;
1366 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001367 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001368 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001369 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1370 }
1371
1372 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001373 vec_free (vec_icmp_bi);
1374 if (event_data)
1375 {
1376 _vec_len (event_data) = 0;
1377 }
1378 }
1379
1380 return 0;
1381}
1382
Klement Sekera75e7d132017-09-20 08:26:30 +02001383/* *INDENT-OFF* */
1384VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1385 .function = ip6_reass_walk_expired,
1386 .format_trace = format_ip6_reass_trace,
1387 .type = VLIB_NODE_TYPE_PROCESS,
1388 .name = "ip6-reassembly-expire-walk",
1389
1390 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1391 .error_strings = ip6_reassembly_error_strings,
1392
1393};
1394/* *INDENT-ON* */
1395
1396static u8 *
1397format_ip6_reass_key (u8 * s, va_list * args)
1398{
1399 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1400 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1401 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1402 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1403 return s;
1404}
1405
1406static u8 *
1407format_ip6_reass (u8 * s, va_list * args)
1408{
1409 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1410 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1411
Klement Sekera4c533132018-02-22 11:41:12 +01001412 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001413 "last_packet_octet: %u, trace_op_counter: %u\n",
1414 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1415 reass->data_len, reass->last_packet_octet,
1416 reass->trace_op_counter);
1417 u32 bi = reass->first_bi;
1418 u32 counter = 0;
1419 while (~0 != bi)
1420 {
1421 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1422 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1423 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1424 "fragment[%u, %u]\n",
1425 counter, vnb->ip.reass.range_first,
1426 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001427 ip6_reass_buffer_get_data_offset (b),
1428 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001429 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1430 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1431 {
1432 bi = b->next_buffer;
1433 }
1434 else
1435 {
1436 bi = ~0;
1437 }
1438 }
1439 return s;
1440}
1441
1442static clib_error_t *
1443show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1444 CLIB_UNUSED (vlib_cli_command_t * lmd))
1445{
1446 ip6_reass_main_t *rm = &ip6_reass_main;
1447
1448 vlib_cli_output (vm, "---------------------");
1449 vlib_cli_output (vm, "IP6 reassembly status");
1450 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001451 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001452 if (unformat (input, "details"))
1453 {
Klement Sekera4c533132018-02-22 11:41:12 +01001454 details = true;
1455 }
1456
1457 u32 sum_reass_n = 0;
1458 u64 sum_buffers_n = 0;
1459 ip6_reass_t *reass;
1460 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001461 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001462 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1463 {
1464 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1465 clib_spinlock_lock (&rt->lock);
1466 if (details)
1467 {
1468 /* *INDENT-OFF* */
1469 pool_foreach (reass, rt->pool, {
1470 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1471 });
1472 /* *INDENT-ON* */
1473 }
1474 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001475 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001476 }
1477 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001478 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1479 (long unsigned) sum_reass_n);
1480 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1481 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001482 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001483 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001484 return 0;
1485}
1486
1487/* *INDENT-OFF* */
1488VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1489 .path = "show ip6-reassembly",
1490 .short_help = "show ip6-reassembly [details]",
1491 .function = show_ip6_reass,
1492};
1493/* *INDENT-ON* */
1494
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001495#ifndef CLIB_MARCH_VARIANT
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}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001502#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001503
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001504#define foreach_ip6_reassembly_handoff_error \
1505_(CONGESTION_DROP, "congestion drop")
1506
1507
1508typedef enum
1509{
1510#define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1511 foreach_ip6_reassembly_handoff_error
1512#undef _
1513 IP6_REASSEMBLY_HANDOFF_N_ERROR,
1514} ip6_reassembly_handoff_error_t;
1515
1516static char *ip6_reassembly_handoff_error_strings[] = {
1517#define _(sym,string) string,
1518 foreach_ip6_reassembly_handoff_error
1519#undef _
1520};
1521
1522typedef struct
1523{
1524 u32 next_worker_index;
1525} ip6_reassembly_handoff_trace_t;
1526
1527static u8 *
1528format_ip6_reassembly_handoff_trace (u8 * s, va_list * args)
1529{
1530 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1531 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1532 ip6_reassembly_handoff_trace_t *t =
1533 va_arg (*args, ip6_reassembly_handoff_trace_t *);
1534
1535 s =
1536 format (s, "ip6-reassembly-handoff: next-worker %d",
1537 t->next_worker_index);
1538
1539 return s;
1540}
1541
1542always_inline uword
1543ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1544 vlib_frame_t * frame, bool is_feature)
1545{
1546 ip6_reass_main_t *rm = &ip6_reass_main;
1547
1548 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1549 u32 n_enq, n_left_from, *from;
1550 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1551 u32 fq_index;
1552
1553 from = vlib_frame_vector_args (frame);
1554 n_left_from = frame->n_vectors;
1555 vlib_get_buffers (vm, from, bufs, n_left_from);
1556
1557 b = bufs;
1558 ti = thread_indices;
1559
1560 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1561
1562 while (n_left_from > 0)
1563 {
1564 ti[0] =
1565 (is_feature) ? vnet_buffer (b[0])->ip.
1566 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1567 reass.owner_thread_index;
1568
1569 if (PREDICT_FALSE
1570 ((node->flags & VLIB_NODE_FLAG_TRACE)
1571 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1572 {
1573 ip6_reassembly_handoff_trace_t *t =
1574 vlib_add_trace (vm, node, b[0], sizeof (*t));
1575 t->next_worker_index = ti[0];
1576 }
1577
1578 n_left_from -= 1;
1579 ti += 1;
1580 b += 1;
1581 }
1582 n_enq =
1583 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1584 frame->n_vectors, 1);
1585
1586 if (n_enq < frame->n_vectors)
1587 vlib_node_increment_counter (vm, node->node_index,
1588 IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1589 frame->n_vectors - n_enq);
1590 return frame->n_vectors;
1591}
1592
1593VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm,
1594 vlib_node_runtime_t * node,
1595 vlib_frame_t * frame)
1596{
1597 return ip6_reassembly_handoff_inline (vm, node, frame,
1598 false /* is_feature */ );
1599}
1600
1601/* *INDENT-OFF* */
1602VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = {
1603 .name = "ip6-reassembly-handoff",
1604 .vector_size = sizeof (u32),
1605 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1606 .error_strings = ip6_reassembly_handoff_error_strings,
1607 .format_trace = format_ip6_reassembly_handoff_trace,
1608
1609 .n_next_nodes = 1,
1610
1611 .next_nodes = {
1612 [0] = "error-drop",
1613 },
1614};
1615
1616
1617VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm,
1618 vlib_node_runtime_t * node, vlib_frame_t * frame)
1619{
1620 return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1621}
1622
1623
1624/* *INDENT-OFF* */
1625VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = {
1626 .name = "ip6-reass-feature-hoff",
1627 .vector_size = sizeof (u32),
1628 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1629 .error_strings = ip6_reassembly_handoff_error_strings,
1630 .format_trace = format_ip6_reassembly_handoff_trace,
1631
1632 .n_next_nodes = 1,
1633
1634 .next_nodes = {
1635 [0] = "error-drop",
1636 },
1637};
1638/* *INDENT-ON* */
1639
Klement Sekera75e7d132017-09-20 08:26:30 +02001640/*
1641 * fd.io coding-style-patch-verification: ON
1642 *
1643 * Local Variables:
1644 * eval: (c-set-style "gnu")
1645 * End:
1646 */