blob: 15ee3e3775d5d6d8e2ff605d6a8c6a5e224a8817 [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
Klement Sekera896c8962019-06-24 11:52:49 +000018 * @brief IPv6 Full Reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020019 *
Klement Sekera896c8962019-06-24 11:52:49 +000020 * This file contains the source code for IPv6 full reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020021 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
26#include <vppinfra/bihash_48_8.h>
Klement Sekera896c8962019-06-24 11:52:49 +000027#include <vnet/ip/reass/ip6_full_reass.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020028
29#define MSEC_PER_SEC 1000
Klement Sekera896c8962019-06-24 11:52:49 +000030#define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 100
31#define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
32#define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
33#define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
34#define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
Klement Sekera75e7d132017-09-20 08:26:30 +020035
Klement Sekerad0f70a32018-12-14 17:24:13 +010036typedef enum
37{
Klement Sekera896c8962019-06-24 11:52:49 +000038 IP6_FULL_REASS_RC_OK,
39 IP6_FULL_REASS_RC_INTERNAL_ERROR,
40 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
41 IP6_FULL_REASS_RC_NO_BUF,
42 IP6_FULL_REASS_RC_HANDOFF,
43} ip6_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020044
45typedef struct
46{
47 union
48 {
49 struct
50 {
51 ip6_address_t src;
52 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020053 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020054 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020055 u8 unused[7];
56 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020057 };
58 u64 as_u64[6];
59 };
Klement Sekera896c8962019-06-24 11:52:49 +000060} ip6_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020061
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080062typedef union
63{
64 struct
65 {
66 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000067 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080068 };
69 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000070} ip6_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080071
72typedef union
73{
74 struct
75 {
Klement Sekera896c8962019-06-24 11:52:49 +000076 ip6_full_reass_key_t k;
77 ip6_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080078 };
79 clib_bihash_kv_48_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +000080} ip6_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080081
82
Klement Sekera75e7d132017-09-20 08:26:30 +020083always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +000084ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020085{
86 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010087 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020088}
89
90always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +000091ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020092{
93 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010094 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +000095 (vnb->ip.reass.fragment_first +
96 ip6_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020097}
98
99typedef struct
100{
101 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000102 ip6_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200103 // time when last packet was received
104 f64 last_heard;
105 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100106 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200107 // buffer index of first buffer in this reassembly context
108 u32 first_bi;
109 // last octet of packet, ~0 until fragment without more_fragments arrives
110 u32 last_packet_octet;
111 // length of data collected so far
112 u32 data_len;
113 // trace operation counter
114 u32 trace_op_counter;
Klement Sekerae8498652019-06-17 12:23:15 +0000115 // next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200116 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000117 // error next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200118 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100119 // minimum fragment length for this reassembly - used to estimate MTU
120 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200121 // number of fragments for this reassembly
122 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000123 // thread owning memory for this context (whose pool contains this ctx)
124 u32 memory_owner_thread_index;
125 // thread which received fragment with offset 0 and which sends out the
126 // completed reassembly
127 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000128} ip6_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200129
130typedef struct
131{
Klement Sekera896c8962019-06-24 11:52:49 +0000132 ip6_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100133 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100134 u32 id_counter;
135 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000136} ip6_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100137
138typedef struct
139{
Klement Sekera75e7d132017-09-20 08:26:30 +0200140 // IPv6 config
141 u32 timeout_ms;
142 f64 timeout;
143 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200144 // maximum number of fragments in one reassembly
145 u32 max_reass_len;
146 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200147 u32 max_reass_n;
148
149 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200150 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100151
152 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000153 ip6_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200154
155 // convenience
156 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200157
158 // node index of ip6-drop node
159 u32 ip6_drop_idx;
160 u32 ip6_icmp_error_idx;
Klement Sekera896c8962019-06-24 11:52:49 +0000161 u32 ip6_full_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200162
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800163 /** Worker handoff */
164 u32 fq_index;
165 u32 fq_feature_index;
166
Klement Sekera7b2e9fb2019-10-01 13:00:22 +0000167 // reference count for enabling/disabling feature - per interface
168 u32 *feature_use_refcount_per_intf;
Klement Sekera896c8962019-06-24 11:52:49 +0000169} ip6_full_reass_main_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200170
Klement Sekera896c8962019-06-24 11:52:49 +0000171extern ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700172
173#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000174ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700175#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200176
177typedef enum
178{
Klement Sekera896c8962019-06-24 11:52:49 +0000179 IP6_FULL_REASSEMBLY_NEXT_INPUT,
180 IP6_FULL_REASSEMBLY_NEXT_DROP,
181 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
182 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
183 IP6_FULL_REASSEMBLY_N_NEXT,
184} ip6_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200185
186typedef enum
187{
188 RANGE_NEW,
189 RANGE_OVERLAP,
190 ICMP_ERROR_RT_EXCEEDED,
191 ICMP_ERROR_FL_TOO_BIG,
192 ICMP_ERROR_FL_NOT_MULT_8,
193 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000194 HANDOFF,
Klement Sekera896c8962019-06-24 11:52:49 +0000195} ip6_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200196
197typedef struct
198{
199 u16 range_first;
200 u16 range_last;
201 u32 range_bi;
202 i32 data_offset;
203 u32 data_len;
204 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000205} ip6_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200206
207typedef struct
208{
Klement Sekera896c8962019-06-24 11:52:49 +0000209 ip6_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200210 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000211 ip6_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200212 u32 op_id;
213 u32 fragment_first;
214 u32 fragment_last;
215 u32 total_data_len;
Klement Sekera630ab582019-07-19 09:14:19 +0000216 u32 thread_id;
217 u32 thread_id_to;
Klement Sekera8563cb32019-10-10 17:03:57 +0000218 bool is_after_handoff;
219 ip6_header_t ip6_header;
220 ip6_frag_hdr_t ip6_frag_header;
Klement Sekera896c8962019-06-24 11:52:49 +0000221} ip6_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200222
223static void
Klement Sekera896c8962019-06-24 11:52:49 +0000224ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
225 ip6_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200226{
227 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
228 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
229 trace->range_first = vnb->ip.reass.range_first;
230 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000231 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
232 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200233 trace->range_bi = bi;
234}
235
Klement Sekera4c533132018-02-22 11:41:12 +0100236static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000237format_ip6_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200238{
Klement Sekera896c8962019-06-24 11:52:49 +0000239 ip6_full_reass_range_trace_t *trace =
240 va_arg (*args, ip6_full_reass_range_trace_t *);
241 s =
242 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
243 trace->range_last, trace->data_offset, trace->data_len,
244 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200245 return s;
246}
247
Klement Sekera4c533132018-02-22 11:41:12 +0100248static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000249format_ip6_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200250{
251 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
252 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000253 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000254 u32 indent = 0;
255 if (~0 != t->reass_id)
256 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000257 if (t->is_after_handoff)
258 {
259 s =
260 format (s, "%U\n", format_ip6_header, &t->ip6_header,
261 sizeof (t->ip6_header));
262 s =
263 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
264 sizeof (t->ip6_frag_header));
265 indent = 2;
266 }
267 s =
268 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
269 t->reass_id, t->op_id);
Klement Sekera630ab582019-07-19 09:14:19 +0000270 indent = format_get_indent (s);
271 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
272 t->trace_range.first_bi, t->total_data_len,
273 t->fragment_first, t->fragment_last);
274 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200275 switch (t->action)
276 {
277 case RANGE_NEW:
278 s = format (s, "\n%Unew %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000279 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200280 break;
281 case RANGE_OVERLAP:
282 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000283 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200284 break;
285 case ICMP_ERROR_FL_TOO_BIG:
286 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000287 format_white_space, indent,
288 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200289 break;
290 case ICMP_ERROR_FL_NOT_MULT_8:
291 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000292 format_white_space, indent,
293 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200294 break;
295 case ICMP_ERROR_RT_EXCEEDED:
296 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
297 format_white_space, indent);
298 break;
299 case FINALIZE:
300 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
301 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000302 case HANDOFF:
303 s =
304 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
305 t->thread_id_to);
306 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200307 }
308 return s;
309}
310
311static void
Klement Sekera896c8962019-06-24 11:52:49 +0000312ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
313 ip6_full_reass_main_t * rm,
314 ip6_full_reass_t * reass, u32 bi,
Klement Sekera8563cb32019-10-10 17:03:57 +0000315 ip6_frag_hdr_t * ip6_frag_header,
Klement Sekera896c8962019-06-24 11:52:49 +0000316 ip6_full_reass_trace_operation_e action,
317 u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200318{
319 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
320 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera8563cb32019-10-10 17:03:57 +0000321 bool is_after_handoff = false;
322 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
323 {
324 is_after_handoff = true;
325 }
Klement Sekera896c8962019-06-24 11:52:49 +0000326 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000327 t->is_after_handoff = is_after_handoff;
328 if (t->is_after_handoff)
329 {
330 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
331 clib_min (sizeof (t->ip6_header), b->current_length));
332 if (ip6_frag_header)
333 {
334 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
335 sizeof (t->ip6_frag_header));
336 }
337 else
338 {
339 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
340 }
341 }
Klement Sekera896c8962019-06-24 11:52:49 +0000342 if (reass)
343 {
344 t->reass_id = reass->id;
345 t->op_id = reass->trace_op_counter;
346 t->trace_range.first_bi = reass->first_bi;
347 t->total_data_len = reass->data_len;
348 ++reass->trace_op_counter;
349 }
350 else
351 {
352 t->reass_id = ~0;
353 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200354 t->action = action;
Klement Sekera630ab582019-07-19 09:14:19 +0000355 t->thread_id = vm->thread_index;
356 t->thread_id_to = thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000357 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200358 t->fragment_first = vnb->ip.reass.fragment_first;
359 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200360#if 0
361 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000362 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200363 printf ("%.*s\n", vec_len (s), s);
364 fflush (stdout);
365 vec_reset_length (s);
366#endif
367}
368
Klement Sekera4c533132018-02-22 11:41:12 +0100369always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000370ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
371 ip6_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000372{
373 pool_put (rt->pool, reass);
374 --rt->reass_n;
375}
376
377always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000378ip6_full_reass_free (ip6_full_reass_main_t * rm,
379 ip6_full_reass_per_thread_t * rt,
380 ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200381{
382 clib_bihash_kv_48_8_t kv;
383 kv.key[0] = reass->key.as_u64[0];
384 kv.key[1] = reass->key.as_u64[1];
385 kv.key[2] = reass->key.as_u64[2];
386 kv.key[3] = reass->key.as_u64[3];
387 kv.key[4] = reass->key.as_u64[4];
388 kv.key[5] = reass->key.as_u64[5];
389 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000390 ip6_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200391}
392
Klement Sekera4c533132018-02-22 11:41:12 +0100393always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000394ip6_full_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
395 ip6_full_reass_main_t * rm, ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200396{
397 u32 range_bi = reass->first_bi;
398 vlib_buffer_t *range_b;
399 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100400 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200401 while (~0 != range_bi)
402 {
403 range_b = vlib_get_buffer (vm, range_bi);
404 range_vnb = vnet_buffer (range_b);
405 u32 bi = range_bi;
406 while (~0 != bi)
407 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100408 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200409 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
410 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
411 {
412 bi = b->next_buffer;
413 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
414 }
415 else
416 {
417 bi = ~0;
418 }
419 }
420 range_bi = range_vnb->ip.reass.next_range_bi;
421 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200422 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000423 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200424 {
425 u32 n_left_to_next, *to_next, next_index;
426
427 next_index = reass->error_next_index;
428 u32 bi = ~0;
429
430 while (vec_len (to_free) > 0)
431 {
432 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
433
434 while (vec_len (to_free) > 0 && n_left_to_next > 0)
435 {
436 bi = vec_pop (to_free);
437
438 if (~0 != bi)
439 {
440 to_next[0] = bi;
441 to_next += 1;
442 n_left_to_next -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200443 }
444 }
445 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
446 }
447 }
448 else
449 {
450 vlib_buffer_free (vm, to_free, vec_len (to_free));
451 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100452 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200453}
454
Klement Sekera4c533132018-02-22 11:41:12 +0100455always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000456ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
457 ip6_full_reass_main_t * rm,
458 ip6_full_reass_t * reass, u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200459{
460 if (~0 == reass->first_bi)
461 {
462 return;
463 }
Klement Sekerae8498652019-06-17 12:23:15 +0000464 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200465 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200466 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
467 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200468 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200469 *icmp_bi = reass->first_bi;
470 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
471 {
Klement Sekera896c8962019-06-24 11:52:49 +0000472 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
Klement Sekera8563cb32019-10-10 17:03:57 +0000473 NULL, ICMP_ERROR_RT_EXCEEDED, ~0);
Klement Sekera21aa8f12019-05-20 12:27:33 +0200474 }
475 // fragment with offset zero received - send icmp message back
476 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
477 {
478 // separate first buffer from chain and steer it towards icmp node
479 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
480 reass->first_bi = b->next_buffer;
481 }
482 else
483 {
484 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
485 }
486 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
487 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
488 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200489 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200490 }
Klement Sekera896c8962019-06-24 11:52:49 +0000491 ip6_full_reass_drop_all (vm, node, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200492}
493
Klement Sekera896c8962019-06-24 11:52:49 +0000494always_inline ip6_full_reass_t *
495ip6_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
496 ip6_full_reass_main_t * rm,
497 ip6_full_reass_per_thread_t * rt,
498 ip6_full_reass_kv_t * kv, u32 * icmp_bi,
499 u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200500{
Klement Sekera896c8962019-06-24 11:52:49 +0000501 ip6_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000502 f64 now;
503
504again:
505
506 reass = NULL;
507 now = vlib_time_now (vm);
Klement Sekera75e7d132017-09-20 08:26:30 +0200508
Klement Sekerac99c0252019-12-18 12:17:06 +0000509 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200510 {
Klement Sekera630ab582019-07-19 09:14:19 +0000511 reass =
512 pool_elt_at_index (rm->per_thread_data
513 [kv->v.memory_owner_thread_index].pool,
514 kv->v.reass_index);
515 if (vm->thread_index != kv->v.memory_owner_thread_index)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800516 {
517 *do_handoff = 1;
Klement Sekera630ab582019-07-19 09:14:19 +0000518 return reass;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800519 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800520
Klement Sekera75e7d132017-09-20 08:26:30 +0200521 if (now > reass->last_heard + rm->timeout)
522 {
Klement Sekera896c8962019-06-24 11:52:49 +0000523 ip6_full_reass_on_timeout (vm, node, rm, reass, icmp_bi);
524 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200525 reass = NULL;
526 }
527 }
528
529 if (reass)
530 {
531 reass->last_heard = now;
532 return reass;
533 }
534
Klement Sekera4c533132018-02-22 11:41:12 +0100535 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200536 {
537 reass = NULL;
538 return reass;
539 }
540 else
541 {
Klement Sekera4c533132018-02-22 11:41:12 +0100542 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400543 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800544 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100545 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200546 reass->first_bi = ~0;
547 reass->last_packet_octet = ~0;
548 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000549 reass->next_index = ~0;
550 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100551 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200552 }
553
Klement Sekerac99c0252019-12-18 12:17:06 +0000554 reass->key.as_u64[0] = kv->kv.key[0];
555 reass->key.as_u64[1] = kv->kv.key[1];
556 reass->key.as_u64[2] = kv->kv.key[2];
557 reass->key.as_u64[3] = kv->kv.key[3];
558 reass->key.as_u64[4] = kv->kv.key[4];
559 reass->key.as_u64[5] = kv->kv.key[5];
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800560 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000561 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200562 reass->last_heard = now;
563
Klement Sekerac99c0252019-12-18 12:17:06 +0000564 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
Klement Sekera630ab582019-07-19 09:14:19 +0000565 if (rv)
Klement Sekera75e7d132017-09-20 08:26:30 +0200566 {
Klement Sekera896c8962019-06-24 11:52:49 +0000567 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200568 reass = NULL;
Klement Sekera630ab582019-07-19 09:14:19 +0000569 // if other worker created a context already work with the other copy
570 if (-2 == rv)
571 goto again;
Klement Sekera75e7d132017-09-20 08:26:30 +0200572 }
573
574 return reass;
575}
576
Klement Sekera896c8962019-06-24 11:52:49 +0000577always_inline ip6_full_reass_rc_t
578ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
579 ip6_full_reass_main_t * rm,
580 ip6_full_reass_per_thread_t * rt,
581 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
582 u32 * error0, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200583{
Klement Sekera75e7d132017-09-20 08:26:30 +0200584 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200585 *error0 = IP6_ERROR_NONE;
586 ip6_frag_hdr_t *frag_hdr;
587 vlib_buffer_t *last_b = NULL;
588 u32 sub_chain_bi = reass->first_bi;
589 u32 total_length = 0;
590 u32 buf_cnt = 0;
591 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100592 u32 *vec_drop_compress = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000593 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200594 do
595 {
596 u32 tmp_bi = sub_chain_bi;
597 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100598 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
599 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
600 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
601 {
Klement Sekera896c8962019-06-24 11:52:49 +0000602 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100603 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100604 }
605
Klement Sekera896c8962019-06-24 11:52:49 +0000606 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200607 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
Klement Sekera896c8962019-06-24 11:52:49 +0000608 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200609 u32 trim_end =
610 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
611 if (tmp_bi == reass->first_bi)
612 {
613 /* first buffer - keep ip6 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000614 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100615 {
Klement Sekera896c8962019-06-24 11:52:49 +0000616 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100617 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100618 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200619 trim_front = 0;
620 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
621 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
622 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100623 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
624 {
Klement Sekera896c8962019-06-24 11:52:49 +0000625 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100626 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100627 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200628 }
629 u32 keep_data =
630 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
631 while (1)
632 {
633 ++buf_cnt;
634 if (trim_front)
635 {
636 if (trim_front > tmp->current_length)
637 {
638 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100639 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200640 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100641 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
642 {
Klement Sekera896c8962019-06-24 11:52:49 +0000643 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100644 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100645 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200646 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
647 tmp_bi = tmp->next_buffer;
648 tmp = vlib_get_buffer (vm, tmp_bi);
649 continue;
650 }
651 else
652 {
653 vlib_buffer_advance (tmp, trim_front);
654 trim_front = 0;
655 }
656 }
657 if (keep_data)
658 {
659 if (last_b)
660 {
661 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
662 last_b->next_buffer = tmp_bi;
663 }
664 last_b = tmp;
665 if (keep_data <= tmp->current_length)
666 {
667 tmp->current_length = keep_data;
668 keep_data = 0;
669 }
670 else
671 {
672 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100673 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
674 {
Klement Sekera896c8962019-06-24 11:52:49 +0000675 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100676 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100677 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200678 }
679 total_length += tmp->current_length;
680 }
681 else
682 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100683 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100684 if (reass->first_bi == tmp_bi)
685 {
Klement Sekera896c8962019-06-24 11:52:49 +0000686 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100687 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100688 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200689 ++dropped_cnt;
690 }
691 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
692 {
693 tmp_bi = tmp->next_buffer;
694 tmp = vlib_get_buffer (vm, tmp->next_buffer);
695 }
696 else
697 {
698 break;
699 }
700 }
701 sub_chain_bi =
702 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
703 reass.next_range_bi;
704 }
705 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700706
Klement Sekerad0f70a32018-12-14 17:24:13 +0100707 if (!last_b)
708 {
Klement Sekera896c8962019-06-24 11:52:49 +0000709 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100710 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100711 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200712 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
713 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100714 if (total_length < first_b->current_length)
715 {
Klement Sekera896c8962019-06-24 11:52:49 +0000716 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100717 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100718 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200719 total_length -= first_b->current_length;
720 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
721 first_b->total_length_not_including_first_buffer = total_length;
722 // drop fragment header
723 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
724 ip6_header_t *ip = vlib_buffer_get_current (first_b);
725 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
726 ip6_ext_header_t *prev_hdr;
Klement Sekera769145c2019-03-06 11:59:57 +0100727 frag_hdr =
728 ip6_ext_header_find (vm, first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
729 &prev_hdr);
Klement Sekera75e7d132017-09-20 08:26:30 +0200730 if (prev_hdr)
731 {
732 prev_hdr->next_hdr = frag_hdr->next_hdr;
733 }
734 else
735 {
736 ip->protocol = frag_hdr->next_hdr;
737 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100738 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
739 {
Klement Sekera896c8962019-06-24 11:52:49 +0000740 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100741 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100742 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200743 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
744 first_b->current_length - ip6_frag_hdr_offset -
745 sizeof (ip6_frag_hdr_t));
746 first_b->current_length -= sizeof (*frag_hdr);
747 ip->payload_length =
748 clib_host_to_net_u16 (total_length + first_b->current_length -
749 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100750 if (!vlib_buffer_chain_linearize (vm, first_b))
751 {
Klement Sekera896c8962019-06-24 11:52:49 +0000752 rv = IP6_FULL_REASS_RC_NO_BUF;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100753 goto free_buffers_and_return;
754 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700755 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200756 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
757 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000758 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi, NULL,
Klement Sekera896c8962019-06-24 11:52:49 +0000759 FINALIZE, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200760#if 0
761 // following code does a hexdump of packet fragments to stdout ...
762 do
763 {
764 u32 bi = reass->first_bi;
765 u8 *s = NULL;
766 while (~0 != bi)
767 {
768 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
769 s = format (s, "%u: %U\n", bi, format_hexdump,
770 vlib_buffer_get_current (b), b->current_length);
771 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
772 {
773 bi = b->next_buffer;
774 }
775 else
776 {
777 break;
778 }
779 }
780 printf ("%.*s\n", vec_len (s), s);
781 fflush (stdout);
782 vec_free (s);
783 }
784 while (0);
785#endif
786 }
Klement Sekerae8498652019-06-17 12:23:15 +0000787 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100788 {
Klement Sekera896c8962019-06-24 11:52:49 +0000789 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100790 }
791 else
792 {
793 *next0 = reass->next_index;
794 }
795 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera896c8962019-06-24 11:52:49 +0000796 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200797 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100798free_buffers_and_return:
799 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
800 vec_free (vec_drop_compress);
801 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200802}
803
Klement Sekera4c533132018-02-22 11:41:12 +0100804always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000805ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
806 ip6_full_reass_main_t * rm,
807 ip6_full_reass_per_thread_t * rt,
808 ip6_full_reass_t * reass,
809 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200810{
811
812 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
813 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
814 if (~0 != prev_range_bi)
815 {
816 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
817 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
818 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
819 prev_vnb->ip.reass.next_range_bi = new_next_bi;
820 }
821 else
822 {
823 if (~0 != reass->first_bi)
824 {
825 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
826 }
827 reass->first_bi = new_next_bi;
828 }
Klement Sekera896c8962019-06-24 11:52:49 +0000829 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200830}
831
Klement Sekera896c8962019-06-24 11:52:49 +0000832always_inline ip6_full_reass_rc_t
833ip6_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
834 ip6_full_reass_main_t * rm,
835 ip6_full_reass_per_thread_t * rt,
836 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
837 u32 * error0, ip6_frag_hdr_t * frag_hdr,
838 bool is_custom_app, u32 * handoff_thread_idx)
Klement Sekera75e7d132017-09-20 08:26:30 +0200839{
840 int consumed = 0;
841 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
842 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000843 if (is_custom_app)
844 {
845 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
846 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
847 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200848
Klement Sekera75e7d132017-09-20 08:26:30 +0200849 fvnb->ip.reass.ip6_frag_hdr_offset =
850 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
851 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100852 if (fb->current_length < sizeof (*fip) ||
853 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
854 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
855 {
Klement Sekera896c8962019-06-24 11:52:49 +0000856 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100857 }
858
Klement Sekera75e7d132017-09-20 08:26:30 +0200859 u32 fragment_first = fvnb->ip.reass.fragment_first =
860 ip6_frag_hdr_offset_bytes (frag_hdr);
861 u32 fragment_length =
862 vlib_buffer_length_in_chain (vm, fb) -
863 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
864 u32 fragment_last = fvnb->ip.reass.fragment_last =
865 fragment_first + fragment_length - 1;
866 int more_fragments = ip6_frag_hdr_more (frag_hdr);
867 u32 candidate_range_bi = reass->first_bi;
868 u32 prev_range_bi = ~0;
869 fvnb->ip.reass.range_first = fragment_first;
870 fvnb->ip.reass.range_last = fragment_last;
871 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200872 if (!more_fragments)
873 {
874 reass->last_packet_octet = fragment_last;
875 }
876 if (~0 == reass->first_bi)
877 {
878 // starting a new reassembly
Klement Sekera896c8962019-06-24 11:52:49 +0000879 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
880 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100881 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100882 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200883 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100884 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200885 }
Klement Sekera4c533132018-02-22 11:41:12 +0100886 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200887 clib_min (clib_net_to_host_u16 (fip->payload_length),
888 fvnb->ip.reass.estimated_mtu);
889 while (~0 != candidate_range_bi)
890 {
891 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
892 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
893 if (fragment_first > candidate_vnb->ip.reass.range_last)
894 {
895 // this fragments starts after candidate range
896 prev_range_bi = candidate_range_bi;
897 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
898 if (candidate_vnb->ip.reass.range_last < fragment_last &&
899 ~0 == candidate_range_bi)
900 {
901 // special case - this fragment falls beyond all known ranges
Klement Sekera896c8962019-06-24 11:52:49 +0000902 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
903 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200904 consumed = 1;
905 break;
906 }
907 continue;
908 }
909 if (fragment_last < candidate_vnb->ip.reass.range_first)
910 {
911 // this fragment ends before candidate range without any overlap
Klement Sekera896c8962019-06-24 11:52:49 +0000912 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
913 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200914 consumed = 1;
915 }
916 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
917 fragment_last == candidate_vnb->ip.reass.range_last)
918 {
919 // duplicate fragment - ignore
920 }
921 else
922 {
923 // overlapping fragment - not allowed by RFC 8200
Klement Sekera75e7d132017-09-20 08:26:30 +0200924 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
925 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000926 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, frag_hdr,
Klement Sekera896c8962019-06-24 11:52:49 +0000927 RANGE_OVERLAP, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200928 }
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200929 ip6_full_reass_drop_all (vm, node, rm, reass);
930 ip6_full_reass_free (rm, rt, reass);
Klement Sekera896c8962019-06-24 11:52:49 +0000931 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200932 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekera896c8962019-06-24 11:52:49 +0000933 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200934 }
935 break;
936 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200937 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100938check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200939 if (consumed)
940 {
941 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
942 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000943 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, frag_hdr,
944 RANGE_NEW, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200945 }
946 }
947 if (~0 != reass->last_packet_octet &&
948 reass->data_len == reass->last_packet_octet + 1)
949 {
Klement Sekera630ab582019-07-19 09:14:19 +0000950 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200951 int handoff =
952 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000953 ip6_full_reass_rc_t rc =
954 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
955 is_custom_app);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200956 if (IP6_FULL_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +0000957 {
Klement Sekera896c8962019-06-24 11:52:49 +0000958 return IP6_FULL_REASS_RC_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +0000959 }
960 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +0200961 }
962 else
963 {
964 if (consumed)
965 {
966 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200967 if (reass->fragments_n > rm->max_reass_len)
968 {
Klement Sekera896c8962019-06-24 11:52:49 +0000969 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
Klement Sekera3a343d42019-05-16 14:35:46 +0200970 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200971 }
972 else
973 {
Klement Sekera896c8962019-06-24 11:52:49 +0000974 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200975 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
976 }
977 }
Klement Sekera896c8962019-06-24 11:52:49 +0000978 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200979}
980
Klement Sekera4c533132018-02-22 11:41:12 +0100981always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +0000982ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
983 vlib_buffer_t * b,
984 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +0100985{
986 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
987 while (ip6_ext_hdr (tmp->next_hdr))
988 {
989 tmp = ip6_ext_next_header (tmp);
990 }
991 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
992 {
993 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
994 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
995 0);
996 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
997
998 return false;
999 }
1000 return true;
1001}
1002
1003always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001004ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1005 vlib_node_runtime_t * node,
1006 vlib_buffer_t * b,
1007 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001008{
1009 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1010 ip6_header_t *ip = vlib_buffer_get_current (b);
1011 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1012 u32 fragment_length =
1013 vlib_buffer_length_in_chain (vm, b) -
1014 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1015 if (more_fragments && 0 != fragment_length % 8)
1016 {
1017 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1018 ICMP6_parameter_problem_erroneous_header_field,
1019 (u8 *) & ip->payload_length - (u8 *) ip);
1020 return false;
1021 }
1022 return true;
1023}
1024
1025always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001026ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1027 vlib_node_runtime_t * node,
1028 vlib_buffer_t * b,
1029 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001030{
1031 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1032 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1033 u32 fragment_length =
1034 vlib_buffer_length_in_chain (vm, b) -
1035 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1036 if (fragment_first + fragment_length > 65535)
1037 {
1038 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1039 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1040 ICMP6_parameter_problem_erroneous_header_field,
1041 (u8 *) & frag_hdr->fragment_offset_and_more
1042 - (u8 *) ip0);
1043 return false;
1044 }
1045 return true;
1046}
1047
Klement Sekera75e7d132017-09-20 08:26:30 +02001048always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001049ip6_full_reassembly_inline (vlib_main_t * vm,
1050 vlib_node_runtime_t * node,
1051 vlib_frame_t * frame, bool is_feature,
1052 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +02001053{
1054 u32 *from = vlib_frame_vector_args (frame);
1055 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001056 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1057 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001058 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001059
1060 n_left_from = frame->n_vectors;
1061 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001062 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001063 {
1064 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1065
Klement Sekera75e7d132017-09-20 08:26:30 +02001066 while (n_left_from > 0 && n_left_to_next > 0)
1067 {
1068 u32 bi0;
1069 vlib_buffer_t *b0;
Klement Sekera896c8962019-06-24 11:52:49 +00001070 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001071 u32 error0 = IP6_ERROR_NONE;
1072 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001073
1074 bi0 = from[0];
1075 b0 = vlib_get_buffer (vm, bi0);
1076
1077 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001078 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001079 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +02001080 if (ip6_ext_hdr (ip0->protocol))
1081 {
Klement Sekera769145c2019-03-06 11:59:57 +01001082 frag_hdr =
1083 ip6_ext_header_find (vm, b0, ip0,
1084 IP_PROTOCOL_IPV6_FRAGMENTATION,
1085 &prev_hdr);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001086 }
Klement Sekera4c533132018-02-22 11:41:12 +01001087 if (!frag_hdr)
1088 {
1089 // this is a regular packet - no fragmentation
Klement Sekera896c8962019-06-24 11:52:49 +00001090 next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +01001091 goto skip_reass;
1092 }
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001093 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1094 (u8 *) frag_hdr - (u8 *) ip0;
1095
Klement Sekera75e7d132017-09-20 08:26:30 +02001096 if (0 == ip6_frag_hdr_offset (frag_hdr))
1097 {
1098 // first fragment - verify upper-layer is present
Klement Sekera896c8962019-06-24 11:52:49 +00001099 if (!ip6_full_reass_verify_upper_layer_present
1100 (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +02001101 {
Klement Sekera896c8962019-06-24 11:52:49 +00001102 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001103 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001104 }
Klement Sekera4c533132018-02-22 11:41:12 +01001105 }
Klement Sekera896c8962019-06-24 11:52:49 +00001106 if (!ip6_full_reass_verify_fragment_multiple_8
1107 (vm, node, b0, frag_hdr)
1108 || !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
1109 frag_hdr))
Klement Sekera4c533132018-02-22 11:41:12 +01001110 {
Klement Sekera896c8962019-06-24 11:52:49 +00001111 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001112 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001113 }
Klement Sekera896c8962019-06-24 11:52:49 +00001114 ip6_full_reass_kv_t kv;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001115 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001116
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001117 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1118 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1119 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1120 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1121 kv.k.as_u64[4] =
1122 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1123 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1124 (u64) frag_hdr->identification;
1125 kv.k.as_u64[5] = ip0->protocol;
1126
Klement Sekera896c8962019-06-24 11:52:49 +00001127 ip6_full_reass_t *reass =
1128 ip6_full_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
1129 &do_handoff);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001130
Klement Sekera630ab582019-07-19 09:14:19 +00001131 if (reass)
1132 {
1133 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1134 if (0 == fragment_first)
1135 {
1136 reass->sendout_thread_index = vm->thread_index;
1137 }
1138 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001139 if (PREDICT_FALSE (do_handoff))
1140 {
Klement Sekera896c8962019-06-24 11:52:49 +00001141 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001142 vnet_buffer (b0)->ip.reass.owner_thread_index =
1143 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001144 }
1145 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001146 {
Klement Sekera630ab582019-07-19 09:14:19 +00001147 u32 handoff_thread_idx;
Klement Sekera896c8962019-06-24 11:52:49 +00001148 switch (ip6_full_reass_update
1149 (vm, node, rm, rt, reass, &bi0, &next0, &error0,
1150 frag_hdr, is_custom_app, &handoff_thread_idx))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001151 {
Klement Sekera896c8962019-06-24 11:52:49 +00001152 case IP6_FULL_REASS_RC_OK:
Klement Sekerad0f70a32018-12-14 17:24:13 +01001153 /* nothing to do here */
1154 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001155 case IP6_FULL_REASS_RC_HANDOFF:
1156 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001157 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001158 vnet_buffer (b0)->ip.reass.owner_thread_index =
1159 handoff_thread_idx;
Klement Sekera630ab582019-07-19 09:14:19 +00001160 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001161 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekera3a343d42019-05-16 14:35:46 +02001162 vlib_node_increment_counter (vm, node->node_index,
1163 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1164 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001165 ip6_full_reass_drop_all (vm, node, rm, reass);
1166 ip6_full_reass_free (rm, rt, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001167 goto next_packet;
1168 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001169 case IP6_FULL_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001170 vlib_node_increment_counter (vm, node->node_index,
1171 IP6_ERROR_REASS_NO_BUF, 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001172 ip6_full_reass_drop_all (vm, node, rm, reass);
1173 ip6_full_reass_free (rm, rt, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001174 goto next_packet;
1175 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001176 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
Klement Sekera3a343d42019-05-16 14:35:46 +02001177 vlib_node_increment_counter (vm, node->node_index,
1178 IP6_ERROR_REASS_INTERNAL_ERROR,
1179 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001180 ip6_full_reass_drop_all (vm, node, rm, reass);
1181 ip6_full_reass_free (rm, rt, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001182 goto next_packet;
1183 break;
1184 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001185 }
1186 else
1187 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001188 if (is_feature)
1189 {
Klement Sekera896c8962019-06-24 11:52:49 +00001190 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera21aa8f12019-05-20 12:27:33 +02001191 }
1192 else
1193 {
1194 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1195 next0 = fvnb->ip.reass.error_next_index;
1196 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001197 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1198 }
1199
Klement Sekera75e7d132017-09-20 08:26:30 +02001200 if (~0 != bi0)
1201 {
Klement Sekera4c533132018-02-22 11:41:12 +01001202 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001203 to_next[0] = bi0;
1204 to_next += 1;
1205 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001206
1207 /* bi0 might have been updated by reass_finalize, reload */
1208 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001209 if (IP6_ERROR_NONE != error0)
1210 {
1211 b0->error = node->errors[error0];
1212 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001213
Klement Sekera896c8962019-06-24 11:52:49 +00001214 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001215 {
1216 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1217 {
Klement Sekerade34c352019-06-25 11:19:22 +00001218 ip6_full_reass_add_trace (vm, node, rm, NULL, bi0,
Klement Sekera8563cb32019-10-10 17:03:57 +00001219 frag_hdr, HANDOFF,
Klement Sekerade34c352019-06-25 11:19:22 +00001220 vnet_buffer (b0)->ip.
1221 reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001222 }
1223 }
1224 else if (is_feature && IP6_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001225 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001226 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001227 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1229 n_left_to_next, bi0, next0);
1230 }
1231
1232 if (~0 != icmp_bi)
1233 {
Klement Sekera896c8962019-06-24 11:52:49 +00001234 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera75e7d132017-09-20 08:26:30 +02001235 to_next[0] = icmp_bi;
1236 to_next += 1;
1237 n_left_to_next -= 1;
1238 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1239 n_left_to_next, icmp_bi,
1240 next0);
1241 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001242 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001243 from += 1;
1244 n_left_from -= 1;
1245 }
1246
1247 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1248 }
1249
Klement Sekera4c533132018-02-22 11:41:12 +01001250 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001251 return frame->n_vectors;
1252}
1253
Klement Sekera896c8962019-06-24 11:52:49 +00001254static char *ip6_full_reassembly_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001255#define _(sym, string) string,
1256 foreach_ip6_error
1257#undef _
1258};
1259
Klement Sekera896c8962019-06-24 11:52:49 +00001260VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1261 vlib_node_runtime_t * node,
1262 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001263{
Klement Sekera896c8962019-06-24 11:52:49 +00001264 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1265 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001266}
1267
Klement Sekera75e7d132017-09-20 08:26:30 +02001268/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001269VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1270 .name = "ip6-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001271 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001272 .format_trace = format_ip6_full_reass_trace,
1273 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1274 .error_strings = ip6_full_reassembly_error_strings,
1275 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001276 .next_nodes =
1277 {
Klement Sekera896c8962019-06-24 11:52:49 +00001278 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1279 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1280 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1281 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001282 },
1283};
1284/* *INDENT-ON* */
1285
Klement Sekera896c8962019-06-24 11:52:49 +00001286VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1287 vlib_node_runtime_t * node,
1288 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001289{
Klement Sekera896c8962019-06-24 11:52:49 +00001290 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1291 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001292}
1293
1294/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001295VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1296 .name = "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001297 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001298 .format_trace = format_ip6_full_reass_trace,
1299 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1300 .error_strings = ip6_full_reassembly_error_strings,
1301 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001302 .next_nodes =
1303 {
Klement Sekera896c8962019-06-24 11:52:49 +00001304 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1305 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1306 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1307 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001308 },
1309};
1310/* *INDENT-ON* */
1311
Klement Sekera4c533132018-02-22 11:41:12 +01001312/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001313VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001314 .arc_name = "ip6-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001315 .node_name = "ip6-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001316 .runs_before = VNET_FEATURES ("ip6-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001317 "ipsec6-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001318 .runs_after = 0,
1319};
1320/* *INDENT-ON* */
1321
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001322#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001323static u32
Klement Sekera896c8962019-06-24 11:52:49 +00001324ip6_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001325{
Klement Sekera896c8962019-06-24 11:52:49 +00001326 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001327 u32 nbuckets;
1328 u8 i;
1329
Klement Sekera896c8962019-06-24 11:52:49 +00001330 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001331
1332 for (i = 0; i < 31; i++)
1333 if ((1 << i) >= nbuckets)
1334 break;
1335 nbuckets = 1 << i;
1336
1337 return nbuckets;
1338}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001339#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001340
1341typedef enum
1342{
1343 IP6_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001344} ip6_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001345
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001346#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001347typedef struct
1348{
1349 int failure;
1350 clib_bihash_48_8_t *new_hash;
1351} ip6_rehash_cb_ctx;
1352
Neale Rannsf50bac12019-12-06 05:53:17 +00001353static int
Klement Sekera75e7d132017-09-20 08:26:30 +02001354ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1355{
1356 ip6_rehash_cb_ctx *ctx = _ctx;
1357 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1358 {
1359 ctx->failure = 1;
1360 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001361 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001362}
1363
Klement Sekera4c533132018-02-22 11:41:12 +01001364static void
Klement Sekera896c8962019-06-24 11:52:49 +00001365ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1366 u32 max_reassembly_length,
1367 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001368{
Klement Sekera896c8962019-06-24 11:52:49 +00001369 ip6_full_reass_main.timeout_ms = timeout_ms;
1370 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1371 ip6_full_reass_main.max_reass_n = max_reassemblies;
1372 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1373 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001374}
1375
Klement Sekera75e7d132017-09-20 08:26:30 +02001376vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001377ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1378 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001379{
Klement Sekera896c8962019-06-24 11:52:49 +00001380 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1381 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1382 max_reassembly_length, expire_walk_interval_ms);
1383 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1384 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001385 IP6_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001386 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1387 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001388 {
1389 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001390 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001391 ip6_rehash_cb_ctx ctx;
1392 ctx.failure = 0;
1393 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001394 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001395 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001396 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001397 ip6_rehash_cb, &ctx);
1398 if (ctx.failure)
1399 {
1400 clib_bihash_free_48_8 (&new_hash);
1401 return -1;
1402 }
1403 else
1404 {
Klement Sekera896c8962019-06-24 11:52:49 +00001405 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1406 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1407 sizeof (ip6_full_reass_main.hash));
1408 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001409 }
1410 }
1411 return 0;
1412}
1413
1414vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001415ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1416 u32 * max_reassembly_length,
1417 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001418{
Klement Sekera896c8962019-06-24 11:52:49 +00001419 *timeout_ms = ip6_full_reass_main.timeout_ms;
1420 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1421 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1422 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001423 return 0;
1424}
1425
Klement Sekera4c533132018-02-22 11:41:12 +01001426static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001427ip6_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001428{
Klement Sekera896c8962019-06-24 11:52:49 +00001429 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001430 clib_error_t *error = 0;
1431 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001432 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001433
1434 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001435
Juraj Slobodacd806922018-10-10 10:15:54 +02001436 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001437 ip6_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001438 vec_foreach (rt, rm->per_thread_data)
1439 {
1440 clib_spinlock_init (&rt->lock);
1441 pool_alloc (rt->pool, rm->max_reass_n);
1442 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001443
Klement Sekera896c8962019-06-24 11:52:49 +00001444 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001445 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001446 rm->ip6_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001447
Klement Sekera896c8962019-06-24 11:52:49 +00001448 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1449 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1450 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1451 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001452
Klement Sekera896c8962019-06-24 11:52:49 +00001453 nbuckets = ip6_full_reass_get_nbuckets ();
1454 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1455 nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001456
Dave Barach1403fcd2018-02-05 09:45:43 -05001457 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001458 ASSERT (node);
1459 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001460 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1461 ASSERT (node);
1462 rm->ip6_icmp_error_idx = node->index;
1463
1464 if ((error = vlib_call_init_function (vm, ip_main_init)))
1465 return error;
1466 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
Klement Sekera896c8962019-06-24 11:52:49 +00001467 ip6_full_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001468
Klement Sekera896c8962019-06-24 11:52:49 +00001469 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001470 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001471 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001472
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001473 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001474 return error;
1475}
1476
Klement Sekera896c8962019-06-24 11:52:49 +00001477VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001478#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001479
1480static uword
Klement Sekera896c8962019-06-24 11:52:49 +00001481ip6_full_reass_walk_expired (vlib_main_t * vm,
1482 vlib_node_runtime_t * node, vlib_frame_t * f)
Klement Sekera75e7d132017-09-20 08:26:30 +02001483{
Klement Sekera896c8962019-06-24 11:52:49 +00001484 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001485 uword event_type, *event_data = 0;
1486
1487 while (true)
1488 {
1489 vlib_process_wait_for_event_or_clock (vm,
1490 (f64) rm->expire_walk_interval_ms
1491 / (f64) MSEC_PER_SEC);
1492 event_type = vlib_process_get_events (vm, &event_data);
1493
1494 switch (event_type)
1495 {
1496 case ~0: /* no events => timeout */
1497 /* nothing to do here */
1498 break;
1499 case IP6_EVENT_CONFIG_CHANGED:
1500 break;
1501 default:
1502 clib_warning ("BUG: event type 0x%wx", event_type);
1503 break;
1504 }
1505 f64 now = vlib_time_now (vm);
1506
Klement Sekera896c8962019-06-24 11:52:49 +00001507 ip6_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001508 int *pool_indexes_to_free = NULL;
1509
Klement Sekera4c533132018-02-22 11:41:12 +01001510 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001511 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001512 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001513 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001514 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1515 {
Klement Sekera896c8962019-06-24 11:52:49 +00001516 ip6_full_reass_per_thread_t *rt =
1517 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001518 clib_spinlock_lock (&rt->lock);
1519
1520 vec_reset_length (pool_indexes_to_free);
1521 /* *INDENT-OFF* */
1522 pool_foreach_index (index, rt->pool, ({
1523 reass = pool_elt_at_index (rt->pool, index);
1524 if (now > reass->last_heard + rm->timeout)
1525 {
1526 vec_add1 (pool_indexes_to_free, index);
1527 }
1528 }));
1529 /* *INDENT-ON* */
1530 int *i;
1531 /* *INDENT-OFF* */
1532 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001533 {
Klement Sekera896c8962019-06-24 11:52:49 +00001534 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera4c533132018-02-22 11:41:12 +01001535 u32 icmp_bi = ~0;
Klement Sekera896c8962019-06-24 11:52:49 +00001536 ip6_full_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001537 if (~0 != icmp_bi)
Dave Baracha638c182019-06-21 18:24:07 -04001538 vec_add1 (vec_icmp_bi, icmp_bi);
1539
Klement Sekera896c8962019-06-24 11:52:49 +00001540 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001541 }
Klement Sekera4c533132018-02-22 11:41:12 +01001542 /* *INDENT-ON* */
1543
1544 clib_spinlock_unlock (&rt->lock);
1545 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001546
Klement Sekera75e7d132017-09-20 08:26:30 +02001547 while (vec_len (vec_icmp_bi) > 0)
1548 {
1549 vlib_frame_t *f =
1550 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1551 u32 *to_next = vlib_frame_vector_args (f);
1552 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001553 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001554 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1555 {
1556 u32 bi = vec_pop (vec_icmp_bi);
1557 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1558 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001559 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001560 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1561 to_next[0] = bi;
1562 ++f->n_vectors;
1563 to_next += 1;
1564 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001565 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001566 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001567 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1568 }
1569
1570 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001571 vec_free (vec_icmp_bi);
1572 if (event_data)
1573 {
1574 _vec_len (event_data) = 0;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
Klement Sekera75e7d132017-09-20 08:26:30 +02001581/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001582VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1583 .function = ip6_full_reass_walk_expired,
1584 .format_trace = format_ip6_full_reass_trace,
Klement Sekera75e7d132017-09-20 08:26:30 +02001585 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001586 .name = "ip6-full-reassembly-expire-walk",
Klement Sekera75e7d132017-09-20 08:26:30 +02001587
Klement Sekera896c8962019-06-24 11:52:49 +00001588 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1589 .error_strings = ip6_full_reassembly_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001590
1591};
1592/* *INDENT-ON* */
1593
1594static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001595format_ip6_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001596{
Klement Sekera896c8962019-06-24 11:52:49 +00001597 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001598 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1599 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1600 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1601 return s;
1602}
1603
1604static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001605format_ip6_full_reass (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001606{
1607 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001608 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001609
Klement Sekera4c533132018-02-22 11:41:12 +01001610 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001611 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001612 reass->id, format_ip6_full_reass_key, &reass->key,
1613 reass->first_bi, reass->data_len, reass->last_packet_octet,
Klement Sekera75e7d132017-09-20 08:26:30 +02001614 reass->trace_op_counter);
1615 u32 bi = reass->first_bi;
1616 u32 counter = 0;
1617 while (~0 != bi)
1618 {
1619 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1620 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1621 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1622 "fragment[%u, %u]\n",
1623 counter, vnb->ip.reass.range_first,
1624 vnb->ip.reass.range_last, bi,
Klement Sekera896c8962019-06-24 11:52:49 +00001625 ip6_full_reass_buffer_get_data_offset (b),
1626 ip6_full_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001627 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1628 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1629 {
1630 bi = b->next_buffer;
1631 }
1632 else
1633 {
1634 bi = ~0;
1635 }
1636 }
1637 return s;
1638}
1639
1640static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001641show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1642 CLIB_UNUSED (vlib_cli_command_t * lmd))
Klement Sekera75e7d132017-09-20 08:26:30 +02001643{
Klement Sekera896c8962019-06-24 11:52:49 +00001644 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001645
1646 vlib_cli_output (vm, "---------------------");
1647 vlib_cli_output (vm, "IP6 reassembly status");
1648 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001649 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001650 if (unformat (input, "details"))
1651 {
Klement Sekera4c533132018-02-22 11:41:12 +01001652 details = true;
1653 }
1654
1655 u32 sum_reass_n = 0;
1656 u64 sum_buffers_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001657 ip6_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001658 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001659 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001660 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1661 {
Klement Sekera896c8962019-06-24 11:52:49 +00001662 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001663 clib_spinlock_lock (&rt->lock);
1664 if (details)
1665 {
1666 /* *INDENT-OFF* */
1667 pool_foreach (reass, rt->pool, {
Klement Sekera896c8962019-06-24 11:52:49 +00001668 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +01001669 });
1670 /* *INDENT-ON* */
1671 }
1672 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001673 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001674 }
1675 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001676 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1677 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001678 vlib_cli_output (vm,
1679 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1680 (long unsigned) rm->max_reass_n);
1681 vlib_cli_output (vm,
1682 "Maximum configured full IP6 reassembly timeout: %lums\n",
1683 (long unsigned) rm->timeout_ms);
1684 vlib_cli_output (vm,
1685 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1686 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001687 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001688 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001689 return 0;
1690}
1691
1692/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001693VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1694 .path = "show ip6-full-reassembly",
1695 .short_help = "show ip6-full-reassembly [details]",
1696 .function = show_ip6_full_reass,
Klement Sekera75e7d132017-09-20 08:26:30 +02001697};
1698/* *INDENT-ON* */
1699
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001700#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001701vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001702ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001703{
Klement Sekera896c8962019-06-24 11:52:49 +00001704 return vnet_feature_enable_disable ("ip6-unicast",
1705 "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001706 sw_if_index, enable_disable, 0, 0);
1707}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001708#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001709
Klement Sekera896c8962019-06-24 11:52:49 +00001710#define foreach_ip6_full_reassembly_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001711_(CONGESTION_DROP, "congestion drop")
1712
1713
1714typedef enum
1715{
Klement Sekera896c8962019-06-24 11:52:49 +00001716#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1717 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001718#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001719 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1720} ip6_full_reassembly_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001721
Klement Sekera896c8962019-06-24 11:52:49 +00001722static char *ip6_full_reassembly_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001723#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001724 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001725#undef _
1726};
1727
1728typedef struct
1729{
1730 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001731} ip6_full_reassembly_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001732
1733static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001734format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001735{
1736 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1737 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001738 ip6_full_reassembly_handoff_trace_t *t =
1739 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001740
1741 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001742 format (s, "ip6-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001743 t->next_worker_index);
1744
1745 return s;
1746}
1747
1748always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001749ip6_full_reassembly_handoff_inline (vlib_main_t * vm,
1750 vlib_node_runtime_t * node,
1751 vlib_frame_t * frame, bool is_feature)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001752{
Klement Sekera896c8962019-06-24 11:52:49 +00001753 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001754
1755 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1756 u32 n_enq, n_left_from, *from;
1757 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1758 u32 fq_index;
1759
1760 from = vlib_frame_vector_args (frame);
1761 n_left_from = frame->n_vectors;
1762 vlib_get_buffers (vm, from, bufs, n_left_from);
1763
1764 b = bufs;
1765 ti = thread_indices;
1766
1767 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1768
1769 while (n_left_from > 0)
1770 {
Klement Sekerade34c352019-06-25 11:19:22 +00001771 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001772
1773 if (PREDICT_FALSE
1774 ((node->flags & VLIB_NODE_FLAG_TRACE)
1775 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1776 {
Klement Sekera896c8962019-06-24 11:52:49 +00001777 ip6_full_reassembly_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001778 vlib_add_trace (vm, node, b[0], sizeof (*t));
1779 t->next_worker_index = ti[0];
1780 }
1781
1782 n_left_from -= 1;
1783 ti += 1;
1784 b += 1;
1785 }
1786 n_enq =
1787 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1788 frame->n_vectors, 1);
1789
1790 if (n_enq < frame->n_vectors)
1791 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001792 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001793 frame->n_vectors - n_enq);
1794 return frame->n_vectors;
1795}
1796
Klement Sekera896c8962019-06-24 11:52:49 +00001797VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1798 vlib_node_runtime_t * node,
1799 vlib_frame_t * frame)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001800{
Klement Sekera896c8962019-06-24 11:52:49 +00001801 return ip6_full_reassembly_handoff_inline (vm, node, frame,
1802 false /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001803}
1804
1805/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001806VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1807 .name = "ip6-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001808 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001809 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1810 .error_strings = ip6_full_reassembly_handoff_error_strings,
1811 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001812
1813 .n_next_nodes = 1,
1814
1815 .next_nodes = {
1816 [0] = "error-drop",
1817 },
1818};
1819
1820
Klement Sekera896c8962019-06-24 11:52:49 +00001821VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001822 vlib_node_runtime_t * node, vlib_frame_t * frame)
1823{
Klement Sekera896c8962019-06-24 11:52:49 +00001824 return ip6_full_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001825}
1826
1827
1828/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001829VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1830 .name = "ip6-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001831 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001832 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1833 .error_strings = ip6_full_reassembly_handoff_error_strings,
1834 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001835
1836 .n_next_nodes = 1,
1837
1838 .next_nodes = {
1839 [0] = "error-drop",
1840 },
1841};
1842/* *INDENT-ON* */
1843
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001844#ifndef CLIB_MARCH_VARIANT
1845int
1846ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1847{
1848 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1849 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1850 if (is_enable)
1851 {
1852 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1853 {
1854 ++rm->feature_use_refcount_per_intf[sw_if_index];
1855 return vnet_feature_enable_disable ("ip6-unicast",
1856 "ip6-full-reassembly-feature",
1857 sw_if_index, 1, 0, 0);
1858 }
1859 ++rm->feature_use_refcount_per_intf[sw_if_index];
1860 }
1861 else
1862 {
1863 --rm->feature_use_refcount_per_intf[sw_if_index];
1864 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1865 return vnet_feature_enable_disable ("ip6-unicast",
1866 "ip6-full-reassembly-feature",
1867 sw_if_index, 0, 0, 0);
1868 }
1869 return -1;
1870}
1871#endif
1872
Klement Sekera75e7d132017-09-20 08:26:30 +02001873/*
1874 * fd.io coding-style-patch-verification: ON
1875 *
1876 * Local Variables:
1877 * eval: (c-set-style "gnu")
1878 * End:
1879 */