blob: fe5c63768820240fcb37cbd0890225bd711e4009 [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;
Klement Sekera53be16d2020-12-15 21:47:36 +0100322 if (pool_is_free_index
323 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
324 {
325 // this buffer's trace is gone
326 b->flags &= ~VLIB_BUFFER_IS_TRACED;
327 return;
328 }
Klement Sekera8563cb32019-10-10 17:03:57 +0000329 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
330 {
331 is_after_handoff = true;
332 }
Klement Sekera896c8962019-06-24 11:52:49 +0000333 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000334 t->is_after_handoff = is_after_handoff;
335 if (t->is_after_handoff)
336 {
337 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
338 clib_min (sizeof (t->ip6_header), b->current_length));
339 if (ip6_frag_header)
340 {
341 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
342 sizeof (t->ip6_frag_header));
343 }
344 else
345 {
346 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
347 }
348 }
Klement Sekera896c8962019-06-24 11:52:49 +0000349 if (reass)
350 {
351 t->reass_id = reass->id;
352 t->op_id = reass->trace_op_counter;
353 t->trace_range.first_bi = reass->first_bi;
354 t->total_data_len = reass->data_len;
355 ++reass->trace_op_counter;
356 }
357 else
358 {
359 t->reass_id = ~0;
360 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200361 t->action = action;
Klement Sekera630ab582019-07-19 09:14:19 +0000362 t->thread_id = vm->thread_index;
363 t->thread_id_to = thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000364 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200365 t->fragment_first = vnb->ip.reass.fragment_first;
366 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200367#if 0
368 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000369 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200370 printf ("%.*s\n", vec_len (s), s);
371 fflush (stdout);
372 vec_reset_length (s);
373#endif
374}
375
Klement Sekera4c533132018-02-22 11:41:12 +0100376always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000377ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
378 ip6_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000379{
380 pool_put (rt->pool, reass);
381 --rt->reass_n;
382}
383
384always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000385ip6_full_reass_free (ip6_full_reass_main_t * rm,
386 ip6_full_reass_per_thread_t * rt,
387 ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200388{
389 clib_bihash_kv_48_8_t kv;
390 kv.key[0] = reass->key.as_u64[0];
391 kv.key[1] = reass->key.as_u64[1];
392 kv.key[2] = reass->key.as_u64[2];
393 kv.key[3] = reass->key.as_u64[3];
394 kv.key[4] = reass->key.as_u64[4];
395 kv.key[5] = reass->key.as_u64[5];
396 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000397 ip6_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200398}
399
Klement Sekera4c533132018-02-22 11:41:12 +0100400always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000401ip6_full_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
402 ip6_full_reass_main_t * rm, ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200403{
404 u32 range_bi = reass->first_bi;
405 vlib_buffer_t *range_b;
406 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100407 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200408 while (~0 != range_bi)
409 {
410 range_b = vlib_get_buffer (vm, range_bi);
411 range_vnb = vnet_buffer (range_b);
412 u32 bi = range_bi;
413 while (~0 != bi)
414 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100415 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200416 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
417 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
418 {
419 bi = b->next_buffer;
420 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
421 }
422 else
423 {
424 bi = ~0;
425 }
426 }
427 range_bi = range_vnb->ip.reass.next_range_bi;
428 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200429 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000430 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200431 {
432 u32 n_left_to_next, *to_next, next_index;
433
434 next_index = reass->error_next_index;
435 u32 bi = ~0;
436
437 while (vec_len (to_free) > 0)
438 {
439 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
440
441 while (vec_len (to_free) > 0 && n_left_to_next > 0)
442 {
443 bi = vec_pop (to_free);
444
445 if (~0 != bi)
446 {
447 to_next[0] = bi;
448 to_next += 1;
449 n_left_to_next -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200450 }
451 }
452 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
453 }
454 }
455 else
456 {
457 vlib_buffer_free (vm, to_free, vec_len (to_free));
458 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100459 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200460}
461
Klement Sekera4c533132018-02-22 11:41:12 +0100462always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000463ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
464 ip6_full_reass_main_t * rm,
465 ip6_full_reass_t * reass, u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200466{
467 if (~0 == reass->first_bi)
468 {
469 return;
470 }
Klement Sekerae8498652019-06-17 12:23:15 +0000471 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200472 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200473 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
474 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200475 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200476 *icmp_bi = reass->first_bi;
477 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
478 {
Klement Sekera896c8962019-06-24 11:52:49 +0000479 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
Klement Sekera8563cb32019-10-10 17:03:57 +0000480 NULL, ICMP_ERROR_RT_EXCEEDED, ~0);
Klement Sekera21aa8f12019-05-20 12:27:33 +0200481 }
482 // fragment with offset zero received - send icmp message back
483 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
484 {
485 // separate first buffer from chain and steer it towards icmp node
486 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
487 reass->first_bi = b->next_buffer;
488 }
489 else
490 {
491 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
492 }
493 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
494 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
495 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200496 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200497 }
Klement Sekera896c8962019-06-24 11:52:49 +0000498 ip6_full_reass_drop_all (vm, node, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200499}
500
Klement Sekera896c8962019-06-24 11:52:49 +0000501always_inline ip6_full_reass_t *
502ip6_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
503 ip6_full_reass_main_t * rm,
504 ip6_full_reass_per_thread_t * rt,
505 ip6_full_reass_kv_t * kv, u32 * icmp_bi,
506 u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200507{
Klement Sekera896c8962019-06-24 11:52:49 +0000508 ip6_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000509 f64 now;
510
511again:
512
513 reass = NULL;
514 now = vlib_time_now (vm);
Klement Sekera75e7d132017-09-20 08:26:30 +0200515
Klement Sekerac99c0252019-12-18 12:17:06 +0000516 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200517 {
Gao Feng9165e032020-04-26 09:57:18 +0800518 if (vm->thread_index != kv->v.memory_owner_thread_index)
519 {
520 *do_handoff = 1;
521 return NULL;
522 }
523
Klement Sekera630ab582019-07-19 09:14:19 +0000524 reass =
525 pool_elt_at_index (rm->per_thread_data
526 [kv->v.memory_owner_thread_index].pool,
527 kv->v.reass_index);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800528
Klement Sekera75e7d132017-09-20 08:26:30 +0200529 if (now > reass->last_heard + rm->timeout)
530 {
Klement Sekera896c8962019-06-24 11:52:49 +0000531 ip6_full_reass_on_timeout (vm, node, rm, reass, icmp_bi);
532 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200533 reass = NULL;
534 }
535 }
536
537 if (reass)
538 {
539 reass->last_heard = now;
540 return reass;
541 }
542
Klement Sekera4c533132018-02-22 11:41:12 +0100543 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200544 {
545 reass = NULL;
546 return reass;
547 }
548 else
549 {
Klement Sekera4c533132018-02-22 11:41:12 +0100550 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400551 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800552 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100553 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200554 reass->first_bi = ~0;
555 reass->last_packet_octet = ~0;
556 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000557 reass->next_index = ~0;
558 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100559 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200560 }
561
Klement Sekerac99c0252019-12-18 12:17:06 +0000562 reass->key.as_u64[0] = kv->kv.key[0];
563 reass->key.as_u64[1] = kv->kv.key[1];
564 reass->key.as_u64[2] = kv->kv.key[2];
565 reass->key.as_u64[3] = kv->kv.key[3];
566 reass->key.as_u64[4] = kv->kv.key[4];
567 reass->key.as_u64[5] = kv->kv.key[5];
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800568 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000569 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200570 reass->last_heard = now;
571
Klement Sekerac99c0252019-12-18 12:17:06 +0000572 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
Klement Sekera630ab582019-07-19 09:14:19 +0000573 if (rv)
Klement Sekera75e7d132017-09-20 08:26:30 +0200574 {
Klement Sekera896c8962019-06-24 11:52:49 +0000575 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200576 reass = NULL;
Klement Sekera630ab582019-07-19 09:14:19 +0000577 // if other worker created a context already work with the other copy
578 if (-2 == rv)
579 goto again;
Klement Sekera75e7d132017-09-20 08:26:30 +0200580 }
581
582 return reass;
583}
584
Klement Sekera896c8962019-06-24 11:52:49 +0000585always_inline ip6_full_reass_rc_t
586ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
587 ip6_full_reass_main_t * rm,
588 ip6_full_reass_per_thread_t * rt,
589 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
590 u32 * error0, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200591{
Klement Sekera75e7d132017-09-20 08:26:30 +0200592 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200593 *error0 = IP6_ERROR_NONE;
594 ip6_frag_hdr_t *frag_hdr;
595 vlib_buffer_t *last_b = NULL;
596 u32 sub_chain_bi = reass->first_bi;
597 u32 total_length = 0;
598 u32 buf_cnt = 0;
599 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100600 u32 *vec_drop_compress = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000601 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200602 do
603 {
604 u32 tmp_bi = sub_chain_bi;
605 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100606 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
607 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
608 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
609 {
Klement Sekera896c8962019-06-24 11:52:49 +0000610 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100611 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100612 }
613
Klement Sekera896c8962019-06-24 11:52:49 +0000614 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200615 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
Klement Sekera896c8962019-06-24 11:52:49 +0000616 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200617 u32 trim_end =
618 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
619 if (tmp_bi == reass->first_bi)
620 {
621 /* first buffer - keep ip6 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000622 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100623 {
Klement Sekera896c8962019-06-24 11:52:49 +0000624 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100625 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100626 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200627 trim_front = 0;
628 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
629 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
630 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100631 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
632 {
Klement Sekera896c8962019-06-24 11:52:49 +0000633 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100634 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100635 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200636 }
637 u32 keep_data =
638 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
639 while (1)
640 {
641 ++buf_cnt;
642 if (trim_front)
643 {
644 if (trim_front > tmp->current_length)
645 {
646 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100647 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200648 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100649 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
650 {
Klement Sekera896c8962019-06-24 11:52:49 +0000651 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100652 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100653 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200654 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
655 tmp_bi = tmp->next_buffer;
656 tmp = vlib_get_buffer (vm, tmp_bi);
657 continue;
658 }
659 else
660 {
661 vlib_buffer_advance (tmp, trim_front);
662 trim_front = 0;
663 }
664 }
665 if (keep_data)
666 {
667 if (last_b)
668 {
669 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
670 last_b->next_buffer = tmp_bi;
671 }
672 last_b = tmp;
673 if (keep_data <= tmp->current_length)
674 {
675 tmp->current_length = keep_data;
676 keep_data = 0;
677 }
678 else
679 {
680 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100681 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
682 {
Klement Sekera896c8962019-06-24 11:52:49 +0000683 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100684 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100685 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200686 }
687 total_length += tmp->current_length;
688 }
689 else
690 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100691 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100692 if (reass->first_bi == tmp_bi)
693 {
Klement Sekera896c8962019-06-24 11:52:49 +0000694 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100695 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100696 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200697 ++dropped_cnt;
698 }
699 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
700 {
701 tmp_bi = tmp->next_buffer;
702 tmp = vlib_get_buffer (vm, tmp->next_buffer);
703 }
704 else
705 {
706 break;
707 }
708 }
709 sub_chain_bi =
710 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
711 reass.next_range_bi;
712 }
713 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700714
Klement Sekerad0f70a32018-12-14 17:24:13 +0100715 if (!last_b)
716 {
Klement Sekera896c8962019-06-24 11:52:49 +0000717 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100718 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100719 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200720 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
721 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100722 if (total_length < first_b->current_length)
723 {
Klement Sekera896c8962019-06-24 11:52:49 +0000724 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100725 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100726 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200727 total_length -= first_b->current_length;
728 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
729 first_b->total_length_not_including_first_buffer = total_length;
730 // drop fragment header
731 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
732 ip6_header_t *ip = vlib_buffer_get_current (first_b);
733 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
734 ip6_ext_header_t *prev_hdr;
Klement Sekera769145c2019-03-06 11:59:57 +0100735 frag_hdr =
736 ip6_ext_header_find (vm, first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
737 &prev_hdr);
Klement Sekera75e7d132017-09-20 08:26:30 +0200738 if (prev_hdr)
739 {
740 prev_hdr->next_hdr = frag_hdr->next_hdr;
741 }
742 else
743 {
744 ip->protocol = frag_hdr->next_hdr;
745 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100746 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
747 {
Klement Sekera896c8962019-06-24 11:52:49 +0000748 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100749 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100750 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200751 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
752 first_b->current_length - ip6_frag_hdr_offset -
753 sizeof (ip6_frag_hdr_t));
754 first_b->current_length -= sizeof (*frag_hdr);
755 ip->payload_length =
756 clib_host_to_net_u16 (total_length + first_b->current_length -
757 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100758 if (!vlib_buffer_chain_linearize (vm, first_b))
759 {
Klement Sekera896c8962019-06-24 11:52:49 +0000760 rv = IP6_FULL_REASS_RC_NO_BUF;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100761 goto free_buffers_and_return;
762 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700763 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200764 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
765 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000766 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi, NULL,
Klement Sekera896c8962019-06-24 11:52:49 +0000767 FINALIZE, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200768#if 0
769 // following code does a hexdump of packet fragments to stdout ...
770 do
771 {
772 u32 bi = reass->first_bi;
773 u8 *s = NULL;
774 while (~0 != bi)
775 {
776 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
777 s = format (s, "%u: %U\n", bi, format_hexdump,
778 vlib_buffer_get_current (b), b->current_length);
779 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
780 {
781 bi = b->next_buffer;
782 }
783 else
784 {
785 break;
786 }
787 }
788 printf ("%.*s\n", vec_len (s), s);
789 fflush (stdout);
790 vec_free (s);
791 }
792 while (0);
793#endif
794 }
Klement Sekerae8498652019-06-17 12:23:15 +0000795 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100796 {
Klement Sekera896c8962019-06-24 11:52:49 +0000797 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100798 }
799 else
800 {
801 *next0 = reass->next_index;
802 }
803 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera896c8962019-06-24 11:52:49 +0000804 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200805 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100806free_buffers_and_return:
807 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
808 vec_free (vec_drop_compress);
809 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200810}
811
Klement Sekera4c533132018-02-22 11:41:12 +0100812always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000813ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
814 ip6_full_reass_main_t * rm,
815 ip6_full_reass_per_thread_t * rt,
816 ip6_full_reass_t * reass,
817 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200818{
819
820 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
821 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
822 if (~0 != prev_range_bi)
823 {
824 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
825 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
826 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
827 prev_vnb->ip.reass.next_range_bi = new_next_bi;
828 }
829 else
830 {
831 if (~0 != reass->first_bi)
832 {
833 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
834 }
835 reass->first_bi = new_next_bi;
836 }
Klement Sekera896c8962019-06-24 11:52:49 +0000837 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200838}
839
Klement Sekera896c8962019-06-24 11:52:49 +0000840always_inline ip6_full_reass_rc_t
841ip6_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
842 ip6_full_reass_main_t * rm,
843 ip6_full_reass_per_thread_t * rt,
844 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
845 u32 * error0, ip6_frag_hdr_t * frag_hdr,
846 bool is_custom_app, u32 * handoff_thread_idx)
Klement Sekera75e7d132017-09-20 08:26:30 +0200847{
848 int consumed = 0;
849 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
850 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000851 if (is_custom_app)
852 {
853 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
854 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
855 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200856
Klement Sekera75e7d132017-09-20 08:26:30 +0200857 fvnb->ip.reass.ip6_frag_hdr_offset =
858 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
859 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100860 if (fb->current_length < sizeof (*fip) ||
861 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
862 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
863 {
Klement Sekera896c8962019-06-24 11:52:49 +0000864 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100865 }
866
Klement Sekera75e7d132017-09-20 08:26:30 +0200867 u32 fragment_first = fvnb->ip.reass.fragment_first =
868 ip6_frag_hdr_offset_bytes (frag_hdr);
869 u32 fragment_length =
870 vlib_buffer_length_in_chain (vm, fb) -
871 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
872 u32 fragment_last = fvnb->ip.reass.fragment_last =
873 fragment_first + fragment_length - 1;
874 int more_fragments = ip6_frag_hdr_more (frag_hdr);
875 u32 candidate_range_bi = reass->first_bi;
876 u32 prev_range_bi = ~0;
877 fvnb->ip.reass.range_first = fragment_first;
878 fvnb->ip.reass.range_last = fragment_last;
879 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200880 if (!more_fragments)
881 {
882 reass->last_packet_octet = fragment_last;
883 }
884 if (~0 == reass->first_bi)
885 {
886 // starting a new reassembly
Klement Sekera896c8962019-06-24 11:52:49 +0000887 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
888 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100889 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100890 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200891 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100892 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200893 }
Klement Sekera4c533132018-02-22 11:41:12 +0100894 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200895 clib_min (clib_net_to_host_u16 (fip->payload_length),
896 fvnb->ip.reass.estimated_mtu);
897 while (~0 != candidate_range_bi)
898 {
899 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
900 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
901 if (fragment_first > candidate_vnb->ip.reass.range_last)
902 {
903 // this fragments starts after candidate range
904 prev_range_bi = candidate_range_bi;
905 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
906 if (candidate_vnb->ip.reass.range_last < fragment_last &&
907 ~0 == candidate_range_bi)
908 {
909 // special case - this fragment falls beyond all known ranges
Klement Sekera896c8962019-06-24 11:52:49 +0000910 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
911 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200912 consumed = 1;
913 break;
914 }
915 continue;
916 }
917 if (fragment_last < candidate_vnb->ip.reass.range_first)
918 {
919 // this fragment ends before candidate range without any overlap
Klement Sekera896c8962019-06-24 11:52:49 +0000920 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
921 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200922 consumed = 1;
923 }
924 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
925 fragment_last == candidate_vnb->ip.reass.range_last)
926 {
927 // duplicate fragment - ignore
928 }
929 else
930 {
931 // overlapping fragment - not allowed by RFC 8200
Klement Sekera75e7d132017-09-20 08:26:30 +0200932 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
933 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000934 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, frag_hdr,
Klement Sekera896c8962019-06-24 11:52:49 +0000935 RANGE_OVERLAP, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200936 }
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200937 ip6_full_reass_drop_all (vm, node, rm, reass);
938 ip6_full_reass_free (rm, rt, reass);
Klement Sekera896c8962019-06-24 11:52:49 +0000939 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200940 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekera896c8962019-06-24 11:52:49 +0000941 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200942 }
943 break;
944 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200945 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100946check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200947 if (consumed)
948 {
949 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
950 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000951 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, frag_hdr,
952 RANGE_NEW, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200953 }
954 }
955 if (~0 != reass->last_packet_octet &&
956 reass->data_len == reass->last_packet_octet + 1)
957 {
Klement Sekera630ab582019-07-19 09:14:19 +0000958 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200959 int handoff =
960 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000961 ip6_full_reass_rc_t rc =
962 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
963 is_custom_app);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +0200964 if (IP6_FULL_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +0000965 {
Klement Sekera896c8962019-06-24 11:52:49 +0000966 return IP6_FULL_REASS_RC_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +0000967 }
968 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +0200969 }
970 else
971 {
972 if (consumed)
973 {
974 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200975 if (reass->fragments_n > rm->max_reass_len)
976 {
Klement Sekera896c8962019-06-24 11:52:49 +0000977 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
Klement Sekera3a343d42019-05-16 14:35:46 +0200978 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200979 }
980 else
981 {
Klement Sekera896c8962019-06-24 11:52:49 +0000982 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200983 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
984 }
985 }
Klement Sekera896c8962019-06-24 11:52:49 +0000986 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200987}
988
Klement Sekera4c533132018-02-22 11:41:12 +0100989always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +0000990ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
991 vlib_buffer_t * b,
992 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +0100993{
994 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
995 while (ip6_ext_hdr (tmp->next_hdr))
996 {
997 tmp = ip6_ext_next_header (tmp);
998 }
999 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
1000 {
1001 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1002 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
1003 0);
1004 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
1005
1006 return false;
1007 }
1008 return true;
1009}
1010
1011always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001012ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1013 vlib_node_runtime_t * node,
1014 vlib_buffer_t * b,
1015 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001016{
1017 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1018 ip6_header_t *ip = vlib_buffer_get_current (b);
1019 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1020 u32 fragment_length =
1021 vlib_buffer_length_in_chain (vm, b) -
1022 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1023 if (more_fragments && 0 != fragment_length % 8)
1024 {
1025 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1026 ICMP6_parameter_problem_erroneous_header_field,
1027 (u8 *) & ip->payload_length - (u8 *) ip);
1028 return false;
1029 }
1030 return true;
1031}
1032
1033always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001034ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1035 vlib_node_runtime_t * node,
1036 vlib_buffer_t * b,
1037 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001038{
1039 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1040 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1041 u32 fragment_length =
1042 vlib_buffer_length_in_chain (vm, b) -
1043 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1044 if (fragment_first + fragment_length > 65535)
1045 {
1046 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1047 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1048 ICMP6_parameter_problem_erroneous_header_field,
1049 (u8 *) & frag_hdr->fragment_offset_and_more
1050 - (u8 *) ip0);
1051 return false;
1052 }
1053 return true;
1054}
1055
Klement Sekera75e7d132017-09-20 08:26:30 +02001056always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001057ip6_full_reassembly_inline (vlib_main_t * vm,
1058 vlib_node_runtime_t * node,
1059 vlib_frame_t * frame, bool is_feature,
1060 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +02001061{
1062 u32 *from = vlib_frame_vector_args (frame);
1063 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001064 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1065 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001066 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001067
1068 n_left_from = frame->n_vectors;
1069 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001070 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001071 {
1072 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1073
Klement Sekera75e7d132017-09-20 08:26:30 +02001074 while (n_left_from > 0 && n_left_to_next > 0)
1075 {
1076 u32 bi0;
1077 vlib_buffer_t *b0;
Klement Sekera896c8962019-06-24 11:52:49 +00001078 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001079 u32 error0 = IP6_ERROR_NONE;
1080 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001081
1082 bi0 = from[0];
1083 b0 = vlib_get_buffer (vm, bi0);
1084
1085 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001086 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001087 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +02001088 if (ip6_ext_hdr (ip0->protocol))
1089 {
Klement Sekera769145c2019-03-06 11:59:57 +01001090 frag_hdr =
1091 ip6_ext_header_find (vm, b0, ip0,
1092 IP_PROTOCOL_IPV6_FRAGMENTATION,
1093 &prev_hdr);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001094 }
Klement Sekera4c533132018-02-22 11:41:12 +01001095 if (!frag_hdr)
1096 {
1097 // this is a regular packet - no fragmentation
Klement Sekera896c8962019-06-24 11:52:49 +00001098 next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +01001099 goto skip_reass;
1100 }
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001101 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1102 (u8 *) frag_hdr - (u8 *) ip0;
1103
Klement Sekera75e7d132017-09-20 08:26:30 +02001104 if (0 == ip6_frag_hdr_offset (frag_hdr))
1105 {
1106 // first fragment - verify upper-layer is present
Klement Sekera896c8962019-06-24 11:52:49 +00001107 if (!ip6_full_reass_verify_upper_layer_present
1108 (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +02001109 {
Klement Sekera896c8962019-06-24 11:52:49 +00001110 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001111 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001112 }
Klement Sekera4c533132018-02-22 11:41:12 +01001113 }
Klement Sekera896c8962019-06-24 11:52:49 +00001114 if (!ip6_full_reass_verify_fragment_multiple_8
1115 (vm, node, b0, frag_hdr)
1116 || !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
1117 frag_hdr))
Klement Sekera4c533132018-02-22 11:41:12 +01001118 {
Klement Sekera896c8962019-06-24 11:52:49 +00001119 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001120 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001121 }
Klement Sekera896c8962019-06-24 11:52:49 +00001122 ip6_full_reass_kv_t kv;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001123 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001124
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001125 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1126 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1127 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1128 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1129 kv.k.as_u64[4] =
1130 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1131 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1132 (u64) frag_hdr->identification;
1133 kv.k.as_u64[5] = ip0->protocol;
1134
Klement Sekera896c8962019-06-24 11:52:49 +00001135 ip6_full_reass_t *reass =
1136 ip6_full_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
1137 &do_handoff);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001138
Klement Sekera630ab582019-07-19 09:14:19 +00001139 if (reass)
1140 {
1141 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1142 if (0 == fragment_first)
1143 {
1144 reass->sendout_thread_index = vm->thread_index;
1145 }
1146 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001147 if (PREDICT_FALSE (do_handoff))
1148 {
Klement Sekera896c8962019-06-24 11:52:49 +00001149 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001150 vnet_buffer (b0)->ip.reass.owner_thread_index =
1151 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001152 }
1153 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001154 {
Klement Sekera630ab582019-07-19 09:14:19 +00001155 u32 handoff_thread_idx;
Klement Sekera896c8962019-06-24 11:52:49 +00001156 switch (ip6_full_reass_update
1157 (vm, node, rm, rt, reass, &bi0, &next0, &error0,
1158 frag_hdr, is_custom_app, &handoff_thread_idx))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001159 {
Klement Sekera896c8962019-06-24 11:52:49 +00001160 case IP6_FULL_REASS_RC_OK:
Klement Sekerad0f70a32018-12-14 17:24:13 +01001161 /* nothing to do here */
1162 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001163 case IP6_FULL_REASS_RC_HANDOFF:
1164 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001165 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001166 vnet_buffer (b0)->ip.reass.owner_thread_index =
1167 handoff_thread_idx;
Klement Sekera630ab582019-07-19 09:14:19 +00001168 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001169 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekera3a343d42019-05-16 14:35:46 +02001170 vlib_node_increment_counter (vm, node->node_index,
1171 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1172 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001173 ip6_full_reass_drop_all (vm, node, rm, reass);
1174 ip6_full_reass_free (rm, rt, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001175 goto next_packet;
1176 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001177 case IP6_FULL_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001178 vlib_node_increment_counter (vm, node->node_index,
1179 IP6_ERROR_REASS_NO_BUF, 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 Sekera3a343d42019-05-16 14:35:46 +02001182 goto next_packet;
1183 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001184 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
Klement Sekera3a343d42019-05-16 14:35:46 +02001185 vlib_node_increment_counter (vm, node->node_index,
1186 IP6_ERROR_REASS_INTERNAL_ERROR,
1187 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001188 ip6_full_reass_drop_all (vm, node, rm, reass);
1189 ip6_full_reass_free (rm, rt, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001190 goto next_packet;
1191 break;
1192 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001193 }
1194 else
1195 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001196 if (is_feature)
1197 {
Klement Sekera896c8962019-06-24 11:52:49 +00001198 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera21aa8f12019-05-20 12:27:33 +02001199 }
1200 else
1201 {
1202 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1203 next0 = fvnb->ip.reass.error_next_index;
1204 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001205 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1206 }
1207
Klement Sekera75e7d132017-09-20 08:26:30 +02001208 if (~0 != bi0)
1209 {
Klement Sekera4c533132018-02-22 11:41:12 +01001210 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001211 to_next[0] = bi0;
1212 to_next += 1;
1213 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001214
1215 /* bi0 might have been updated by reass_finalize, reload */
1216 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001217 if (IP6_ERROR_NONE != error0)
1218 {
1219 b0->error = node->errors[error0];
1220 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001221
Klement Sekera896c8962019-06-24 11:52:49 +00001222 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001223 {
1224 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1225 {
Klement Sekerade34c352019-06-25 11:19:22 +00001226 ip6_full_reass_add_trace (vm, node, rm, NULL, bi0,
Klement Sekera8563cb32019-10-10 17:03:57 +00001227 frag_hdr, HANDOFF,
Klement Sekerade34c352019-06-25 11:19:22 +00001228 vnet_buffer (b0)->ip.
1229 reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001230 }
1231 }
1232 else if (is_feature && IP6_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001233 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001234 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001235 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001236 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1237 n_left_to_next, bi0, next0);
1238 }
1239
1240 if (~0 != icmp_bi)
1241 {
Klement Sekera896c8962019-06-24 11:52:49 +00001242 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera75e7d132017-09-20 08:26:30 +02001243 to_next[0] = icmp_bi;
1244 to_next += 1;
1245 n_left_to_next -= 1;
1246 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1247 n_left_to_next, icmp_bi,
1248 next0);
1249 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001250 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001251 from += 1;
1252 n_left_from -= 1;
1253 }
1254
1255 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1256 }
1257
Klement Sekera4c533132018-02-22 11:41:12 +01001258 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001259 return frame->n_vectors;
1260}
1261
Klement Sekera896c8962019-06-24 11:52:49 +00001262static char *ip6_full_reassembly_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001263#define _(sym, string) string,
1264 foreach_ip6_error
1265#undef _
1266};
1267
Klement Sekera896c8962019-06-24 11:52:49 +00001268VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1269 vlib_node_runtime_t * node,
1270 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001271{
Klement Sekera896c8962019-06-24 11:52:49 +00001272 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1273 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001274}
1275
Klement Sekera75e7d132017-09-20 08:26:30 +02001276/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001277VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1278 .name = "ip6-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001279 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001280 .format_trace = format_ip6_full_reass_trace,
1281 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1282 .error_strings = ip6_full_reassembly_error_strings,
1283 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001284 .next_nodes =
1285 {
Klement Sekera896c8962019-06-24 11:52:49 +00001286 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1287 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1288 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1289 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001290 },
1291};
1292/* *INDENT-ON* */
1293
Klement Sekera896c8962019-06-24 11:52:49 +00001294VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1295 vlib_node_runtime_t * node,
1296 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001297{
Klement Sekera896c8962019-06-24 11:52:49 +00001298 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1299 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001300}
1301
1302/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001303VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1304 .name = "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001305 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001306 .format_trace = format_ip6_full_reass_trace,
1307 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1308 .error_strings = ip6_full_reassembly_error_strings,
1309 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001310 .next_nodes =
1311 {
Klement Sekera896c8962019-06-24 11:52:49 +00001312 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1313 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1314 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1315 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001316 },
1317};
1318/* *INDENT-ON* */
1319
Klement Sekera4c533132018-02-22 11:41:12 +01001320/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001321VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001322 .arc_name = "ip6-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001323 .node_name = "ip6-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001324 .runs_before = VNET_FEATURES ("ip6-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001325 "ipsec6-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001326 .runs_after = 0,
1327};
1328/* *INDENT-ON* */
1329
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001330#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001331static u32
Klement Sekera896c8962019-06-24 11:52:49 +00001332ip6_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001333{
Klement Sekera896c8962019-06-24 11:52:49 +00001334 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001335 u32 nbuckets;
1336 u8 i;
1337
Klement Sekera896c8962019-06-24 11:52:49 +00001338 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001339
1340 for (i = 0; i < 31; i++)
1341 if ((1 << i) >= nbuckets)
1342 break;
1343 nbuckets = 1 << i;
1344
1345 return nbuckets;
1346}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001347#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001348
1349typedef enum
1350{
1351 IP6_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001352} ip6_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001353
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001354#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001355typedef struct
1356{
1357 int failure;
1358 clib_bihash_48_8_t *new_hash;
1359} ip6_rehash_cb_ctx;
1360
Neale Rannsf50bac12019-12-06 05:53:17 +00001361static int
Klement Sekera75e7d132017-09-20 08:26:30 +02001362ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1363{
1364 ip6_rehash_cb_ctx *ctx = _ctx;
1365 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1366 {
1367 ctx->failure = 1;
1368 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001369 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001370}
1371
Klement Sekera4c533132018-02-22 11:41:12 +01001372static void
Klement Sekera896c8962019-06-24 11:52:49 +00001373ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1374 u32 max_reassembly_length,
1375 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001376{
Klement Sekera896c8962019-06-24 11:52:49 +00001377 ip6_full_reass_main.timeout_ms = timeout_ms;
1378 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1379 ip6_full_reass_main.max_reass_n = max_reassemblies;
1380 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1381 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001382}
1383
Klement Sekera75e7d132017-09-20 08:26:30 +02001384vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001385ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1386 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001387{
Klement Sekera896c8962019-06-24 11:52:49 +00001388 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1389 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1390 max_reassembly_length, expire_walk_interval_ms);
1391 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1392 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001393 IP6_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001394 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1395 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001396 {
1397 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001398 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001399 ip6_rehash_cb_ctx ctx;
1400 ctx.failure = 0;
1401 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001402 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001403 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001404 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001405 ip6_rehash_cb, &ctx);
1406 if (ctx.failure)
1407 {
1408 clib_bihash_free_48_8 (&new_hash);
1409 return -1;
1410 }
1411 else
1412 {
Klement Sekera896c8962019-06-24 11:52:49 +00001413 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1414 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1415 sizeof (ip6_full_reass_main.hash));
1416 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001417 }
1418 }
1419 return 0;
1420}
1421
1422vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001423ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1424 u32 * max_reassembly_length,
1425 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001426{
Klement Sekera896c8962019-06-24 11:52:49 +00001427 *timeout_ms = ip6_full_reass_main.timeout_ms;
1428 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1429 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1430 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001431 return 0;
1432}
1433
Klement Sekera4c533132018-02-22 11:41:12 +01001434static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001435ip6_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001436{
Klement Sekera896c8962019-06-24 11:52:49 +00001437 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001438 clib_error_t *error = 0;
1439 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001440 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001441
1442 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001443
Juraj Slobodacd806922018-10-10 10:15:54 +02001444 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001445 ip6_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001446 vec_foreach (rt, rm->per_thread_data)
1447 {
1448 clib_spinlock_init (&rt->lock);
1449 pool_alloc (rt->pool, rm->max_reass_n);
1450 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001451
Klement Sekera896c8962019-06-24 11:52:49 +00001452 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001453 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001454 rm->ip6_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001455
Klement Sekera896c8962019-06-24 11:52:49 +00001456 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1457 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1458 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1459 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001460
Klement Sekera896c8962019-06-24 11:52:49 +00001461 nbuckets = ip6_full_reass_get_nbuckets ();
1462 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1463 nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001464
Dave Barach1403fcd2018-02-05 09:45:43 -05001465 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001466 ASSERT (node);
1467 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001468 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1469 ASSERT (node);
1470 rm->ip6_icmp_error_idx = node->index;
1471
1472 if ((error = vlib_call_init_function (vm, ip_main_init)))
1473 return error;
1474 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
Klement Sekera896c8962019-06-24 11:52:49 +00001475 ip6_full_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001476
Klement Sekera896c8962019-06-24 11:52:49 +00001477 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001478 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001479 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001480
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001481 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001482 return error;
1483}
1484
Klement Sekera896c8962019-06-24 11:52:49 +00001485VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001486#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001487
1488static uword
Klement Sekera896c8962019-06-24 11:52:49 +00001489ip6_full_reass_walk_expired (vlib_main_t * vm,
1490 vlib_node_runtime_t * node, vlib_frame_t * f)
Klement Sekera75e7d132017-09-20 08:26:30 +02001491{
Klement Sekera896c8962019-06-24 11:52:49 +00001492 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001493 uword event_type, *event_data = 0;
1494
1495 while (true)
1496 {
1497 vlib_process_wait_for_event_or_clock (vm,
1498 (f64) rm->expire_walk_interval_ms
1499 / (f64) MSEC_PER_SEC);
1500 event_type = vlib_process_get_events (vm, &event_data);
1501
1502 switch (event_type)
1503 {
1504 case ~0: /* no events => timeout */
1505 /* nothing to do here */
1506 break;
1507 case IP6_EVENT_CONFIG_CHANGED:
1508 break;
1509 default:
1510 clib_warning ("BUG: event type 0x%wx", event_type);
1511 break;
1512 }
1513 f64 now = vlib_time_now (vm);
1514
Klement Sekera896c8962019-06-24 11:52:49 +00001515 ip6_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001516 int *pool_indexes_to_free = NULL;
1517
Klement Sekera4c533132018-02-22 11:41:12 +01001518 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001519 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001520 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001521 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001522 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1523 {
Klement Sekera896c8962019-06-24 11:52:49 +00001524 ip6_full_reass_per_thread_t *rt =
1525 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001526 clib_spinlock_lock (&rt->lock);
1527
1528 vec_reset_length (pool_indexes_to_free);
1529 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001530 pool_foreach_index (index, rt->pool) {
Klement Sekera4c533132018-02-22 11:41:12 +01001531 reass = pool_elt_at_index (rt->pool, index);
1532 if (now > reass->last_heard + rm->timeout)
1533 {
1534 vec_add1 (pool_indexes_to_free, index);
1535 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001536 }
Klement Sekera4c533132018-02-22 11:41:12 +01001537 /* *INDENT-ON* */
1538 int *i;
1539 /* *INDENT-OFF* */
1540 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001541 {
Klement Sekera896c8962019-06-24 11:52:49 +00001542 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera4c533132018-02-22 11:41:12 +01001543 u32 icmp_bi = ~0;
Klement Sekera896c8962019-06-24 11:52:49 +00001544 ip6_full_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001545 if (~0 != icmp_bi)
Dave Baracha638c182019-06-21 18:24:07 -04001546 vec_add1 (vec_icmp_bi, icmp_bi);
1547
Klement Sekera896c8962019-06-24 11:52:49 +00001548 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001549 }
Klement Sekera4c533132018-02-22 11:41:12 +01001550 /* *INDENT-ON* */
1551
1552 clib_spinlock_unlock (&rt->lock);
1553 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001554
Klement Sekera75e7d132017-09-20 08:26:30 +02001555 while (vec_len (vec_icmp_bi) > 0)
1556 {
1557 vlib_frame_t *f =
1558 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1559 u32 *to_next = vlib_frame_vector_args (f);
1560 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001561 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001562 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1563 {
1564 u32 bi = vec_pop (vec_icmp_bi);
1565 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1566 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001567 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001568 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1569 to_next[0] = bi;
1570 ++f->n_vectors;
1571 to_next += 1;
1572 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001573 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001574 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001575 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1576 }
1577
1578 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001579 vec_free (vec_icmp_bi);
1580 if (event_data)
1581 {
1582 _vec_len (event_data) = 0;
1583 }
1584 }
1585
1586 return 0;
1587}
1588
Klement Sekera75e7d132017-09-20 08:26:30 +02001589/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001590VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1591 .function = ip6_full_reass_walk_expired,
1592 .format_trace = format_ip6_full_reass_trace,
Klement Sekera75e7d132017-09-20 08:26:30 +02001593 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001594 .name = "ip6-full-reassembly-expire-walk",
Klement Sekera75e7d132017-09-20 08:26:30 +02001595
Klement Sekera896c8962019-06-24 11:52:49 +00001596 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1597 .error_strings = ip6_full_reassembly_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001598
1599};
1600/* *INDENT-ON* */
1601
1602static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001603format_ip6_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001604{
Klement Sekera896c8962019-06-24 11:52:49 +00001605 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001606 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1607 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1608 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1609 return s;
1610}
1611
1612static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001613format_ip6_full_reass (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001614{
1615 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001616 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001617
Klement Sekera4c533132018-02-22 11:41:12 +01001618 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001619 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001620 reass->id, format_ip6_full_reass_key, &reass->key,
1621 reass->first_bi, reass->data_len, reass->last_packet_octet,
Klement Sekera75e7d132017-09-20 08:26:30 +02001622 reass->trace_op_counter);
1623 u32 bi = reass->first_bi;
1624 u32 counter = 0;
1625 while (~0 != bi)
1626 {
1627 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1628 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1629 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1630 "fragment[%u, %u]\n",
1631 counter, vnb->ip.reass.range_first,
1632 vnb->ip.reass.range_last, bi,
Klement Sekera896c8962019-06-24 11:52:49 +00001633 ip6_full_reass_buffer_get_data_offset (b),
1634 ip6_full_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001635 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1636 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1637 {
1638 bi = b->next_buffer;
1639 }
1640 else
1641 {
1642 bi = ~0;
1643 }
1644 }
1645 return s;
1646}
1647
1648static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001649show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1650 CLIB_UNUSED (vlib_cli_command_t * lmd))
Klement Sekera75e7d132017-09-20 08:26:30 +02001651{
Klement Sekera896c8962019-06-24 11:52:49 +00001652 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001653
1654 vlib_cli_output (vm, "---------------------");
1655 vlib_cli_output (vm, "IP6 reassembly status");
1656 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001657 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001658 if (unformat (input, "details"))
1659 {
Klement Sekera4c533132018-02-22 11:41:12 +01001660 details = true;
1661 }
1662
1663 u32 sum_reass_n = 0;
1664 u64 sum_buffers_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001665 ip6_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001666 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001667 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001668 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1669 {
Klement Sekera896c8962019-06-24 11:52:49 +00001670 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001671 clib_spinlock_lock (&rt->lock);
1672 if (details)
1673 {
1674 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001675 pool_foreach (reass, rt->pool) {
Klement Sekera896c8962019-06-24 11:52:49 +00001676 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001677 }
Klement Sekera4c533132018-02-22 11:41:12 +01001678 /* *INDENT-ON* */
1679 }
1680 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001681 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001682 }
1683 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001684 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1685 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001686 vlib_cli_output (vm,
1687 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1688 (long unsigned) rm->max_reass_n);
1689 vlib_cli_output (vm,
1690 "Maximum configured full IP6 reassembly timeout: %lums\n",
1691 (long unsigned) rm->timeout_ms);
1692 vlib_cli_output (vm,
1693 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1694 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001695 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001696 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001697 return 0;
1698}
1699
1700/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001701VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1702 .path = "show ip6-full-reassembly",
1703 .short_help = "show ip6-full-reassembly [details]",
1704 .function = show_ip6_full_reass,
Klement Sekera75e7d132017-09-20 08:26:30 +02001705};
1706/* *INDENT-ON* */
1707
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001708#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001709vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001710ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001711{
Klement Sekera896c8962019-06-24 11:52:49 +00001712 return vnet_feature_enable_disable ("ip6-unicast",
1713 "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001714 sw_if_index, enable_disable, 0, 0);
1715}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001716#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001717
Klement Sekera896c8962019-06-24 11:52:49 +00001718#define foreach_ip6_full_reassembly_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001719_(CONGESTION_DROP, "congestion drop")
1720
1721
1722typedef enum
1723{
Klement Sekera896c8962019-06-24 11:52:49 +00001724#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1725 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001726#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001727 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1728} ip6_full_reassembly_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001729
Klement Sekera896c8962019-06-24 11:52:49 +00001730static char *ip6_full_reassembly_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001731#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001732 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001733#undef _
1734};
1735
1736typedef struct
1737{
1738 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001739} ip6_full_reassembly_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001740
1741static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001742format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001743{
1744 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1745 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001746 ip6_full_reassembly_handoff_trace_t *t =
1747 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001748
1749 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001750 format (s, "ip6-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001751 t->next_worker_index);
1752
1753 return s;
1754}
1755
1756always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001757ip6_full_reassembly_handoff_inline (vlib_main_t * vm,
1758 vlib_node_runtime_t * node,
1759 vlib_frame_t * frame, bool is_feature)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001760{
Klement Sekera896c8962019-06-24 11:52:49 +00001761 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001762
1763 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1764 u32 n_enq, n_left_from, *from;
1765 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1766 u32 fq_index;
1767
1768 from = vlib_frame_vector_args (frame);
1769 n_left_from = frame->n_vectors;
1770 vlib_get_buffers (vm, from, bufs, n_left_from);
1771
1772 b = bufs;
1773 ti = thread_indices;
1774
1775 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1776
1777 while (n_left_from > 0)
1778 {
Klement Sekerade34c352019-06-25 11:19:22 +00001779 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001780
1781 if (PREDICT_FALSE
1782 ((node->flags & VLIB_NODE_FLAG_TRACE)
1783 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1784 {
Klement Sekera896c8962019-06-24 11:52:49 +00001785 ip6_full_reassembly_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001786 vlib_add_trace (vm, node, b[0], sizeof (*t));
1787 t->next_worker_index = ti[0];
1788 }
1789
1790 n_left_from -= 1;
1791 ti += 1;
1792 b += 1;
1793 }
1794 n_enq =
1795 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1796 frame->n_vectors, 1);
1797
1798 if (n_enq < frame->n_vectors)
1799 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001800 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001801 frame->n_vectors - n_enq);
1802 return frame->n_vectors;
1803}
1804
Klement Sekera896c8962019-06-24 11:52:49 +00001805VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1806 vlib_node_runtime_t * node,
1807 vlib_frame_t * frame)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001808{
Klement Sekera896c8962019-06-24 11:52:49 +00001809 return ip6_full_reassembly_handoff_inline (vm, node, frame,
1810 false /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001811}
1812
1813/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001814VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1815 .name = "ip6-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001816 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001817 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1818 .error_strings = ip6_full_reassembly_handoff_error_strings,
1819 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001820
1821 .n_next_nodes = 1,
1822
1823 .next_nodes = {
1824 [0] = "error-drop",
1825 },
1826};
1827
1828
Klement Sekera896c8962019-06-24 11:52:49 +00001829VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001830 vlib_node_runtime_t * node, vlib_frame_t * frame)
1831{
Klement Sekera896c8962019-06-24 11:52:49 +00001832 return ip6_full_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001833}
1834
1835
1836/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001837VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1838 .name = "ip6-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001839 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001840 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1841 .error_strings = ip6_full_reassembly_handoff_error_strings,
1842 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001843
1844 .n_next_nodes = 1,
1845
1846 .next_nodes = {
1847 [0] = "error-drop",
1848 },
1849};
1850/* *INDENT-ON* */
1851
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001852#ifndef CLIB_MARCH_VARIANT
1853int
1854ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1855{
1856 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1857 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1858 if (is_enable)
1859 {
1860 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1861 {
1862 ++rm->feature_use_refcount_per_intf[sw_if_index];
1863 return vnet_feature_enable_disable ("ip6-unicast",
1864 "ip6-full-reassembly-feature",
1865 sw_if_index, 1, 0, 0);
1866 }
1867 ++rm->feature_use_refcount_per_intf[sw_if_index];
1868 }
1869 else
1870 {
1871 --rm->feature_use_refcount_per_intf[sw_if_index];
1872 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1873 return vnet_feature_enable_disable ("ip6-unicast",
1874 "ip6-full-reassembly-feature",
1875 sw_if_index, 0, 0, 0);
1876 }
1877 return -1;
1878}
1879#endif
1880
Klement Sekera75e7d132017-09-20 08:26:30 +02001881/*
1882 * fd.io coding-style-patch-verification: ON
1883 *
1884 * Local Variables:
1885 * eval: (c-set-style "gnu")
1886 * End:
1887 */