blob: 582dbf606792f8c357247b194f9ed583dcbf66fb [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>
Ole Troan03092c12021-11-23 15:55:39 +010028#include <vnet/ip/ip6_inlines.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020029
30#define MSEC_PER_SEC 1000
Vijayabhaskar Katamreddy8b859292022-05-26 15:03:47 +000031#define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 200
32/* As there are only 1024 reass context per thread, either the DDOS attacks
33 * or fractions of real timeouts, would consume these contexts quickly and
34 * running out context space and unable to perform reassembly */
35#define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 50 // 50 ms default
Klement Sekera896c8962019-06-24 11:52:49 +000036#define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
37#define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
38#define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
Klement Sekera75e7d132017-09-20 08:26:30 +020039
Klement Sekerad0f70a32018-12-14 17:24:13 +010040typedef enum
41{
Klement Sekera896c8962019-06-24 11:52:49 +000042 IP6_FULL_REASS_RC_OK,
43 IP6_FULL_REASS_RC_INTERNAL_ERROR,
44 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
45 IP6_FULL_REASS_RC_NO_BUF,
46 IP6_FULL_REASS_RC_HANDOFF,
Klement Sekera755042d2021-12-01 10:14:38 +000047 IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
Klement Sekerae63a2d42021-05-20 12:23:00 +020048 IP6_FULL_REASS_RC_OVERLAP,
Klement Sekera896c8962019-06-24 11:52:49 +000049} ip6_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020050
51typedef struct
52{
53 union
54 {
55 struct
56 {
57 ip6_address_t src;
58 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020059 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020060 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020061 u8 unused[7];
62 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020063 };
64 u64 as_u64[6];
65 };
Klement Sekera896c8962019-06-24 11:52:49 +000066} ip6_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020067
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080068typedef union
69{
70 struct
71 {
72 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000073 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080074 };
75 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000076} ip6_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080077
78typedef union
79{
80 struct
81 {
Klement Sekera896c8962019-06-24 11:52:49 +000082 ip6_full_reass_key_t k;
83 ip6_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080084 };
85 clib_bihash_kv_48_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +000086} ip6_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080087
88
Klement Sekera75e7d132017-09-20 08:26:30 +020089always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +000090ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020091{
92 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010093 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020094}
95
96always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +000097ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020098{
99 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100100 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +0000101 (vnb->ip.reass.fragment_first +
102 ip6_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +0200103}
104
105typedef struct
106{
107 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000108 ip6_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200109 // time when last packet was received
110 f64 last_heard;
111 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100112 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200113 // buffer index of first buffer in this reassembly context
114 u32 first_bi;
115 // last octet of packet, ~0 until fragment without more_fragments arrives
116 u32 last_packet_octet;
117 // length of data collected so far
118 u32 data_len;
119 // trace operation counter
120 u32 trace_op_counter;
Klement Sekerae8498652019-06-17 12:23:15 +0000121 // next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200122 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000123 // error next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200124 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100125 // minimum fragment length for this reassembly - used to estimate MTU
126 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200127 // number of fragments for this reassembly
128 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000129 // thread owning memory for this context (whose pool contains this ctx)
130 u32 memory_owner_thread_index;
131 // thread which received fragment with offset 0 and which sends out the
132 // completed reassembly
133 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000134} ip6_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200135
136typedef struct
137{
Klement Sekera896c8962019-06-24 11:52:49 +0000138 ip6_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100139 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100140 u32 id_counter;
Vijayabhaskar Katamreddy8b859292022-05-26 15:03:47 +0000141 // for pacing the main thread timeouts
142 u32 last_id;
Klement Sekera4c533132018-02-22 11:41:12 +0100143 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000144} ip6_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100145
146typedef struct
147{
Klement Sekera75e7d132017-09-20 08:26:30 +0200148 // IPv6 config
149 u32 timeout_ms;
150 f64 timeout;
151 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200152 // maximum number of fragments in one reassembly
153 u32 max_reass_len;
154 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200155 u32 max_reass_n;
156
157 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200158 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100159
160 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000161 ip6_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200162
163 // convenience
164 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200165
Klement Sekera75e7d132017-09-20 08:26:30 +0200166 u32 ip6_icmp_error_idx;
Klement Sekera896c8962019-06-24 11:52:49 +0000167 u32 ip6_full_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200168
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800169 /** Worker handoff */
170 u32 fq_index;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000171 u32 fq_local_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800172 u32 fq_feature_index;
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +0000173 u32 fq_custom_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800174
Klement Sekera7b2e9fb2019-10-01 13:00:22 +0000175 // reference count for enabling/disabling feature - per interface
176 u32 *feature_use_refcount_per_intf;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000177
178 // whether local fragmented packets are reassembled or not
179 int is_local_reass_enabled;
Klement Sekera896c8962019-06-24 11:52:49 +0000180} ip6_full_reass_main_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200181
Klement Sekera896c8962019-06-24 11:52:49 +0000182extern ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700183
184#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000185ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700186#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200187
188typedef enum
189{
Klement Sekera896c8962019-06-24 11:52:49 +0000190 IP6_FULL_REASSEMBLY_NEXT_INPUT,
191 IP6_FULL_REASSEMBLY_NEXT_DROP,
192 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
193 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
194 IP6_FULL_REASSEMBLY_N_NEXT,
195} ip6_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200196
197typedef enum
198{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +0000199 NORMAL,
200 FEATURE,
201 CUSTOM
202} ip6_full_reass_node_type_t;
203
204typedef enum
205{
Klement Sekera75e7d132017-09-20 08:26:30 +0200206 RANGE_NEW,
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000207 RANGE_DISCARD,
Klement Sekera75e7d132017-09-20 08:26:30 +0200208 RANGE_OVERLAP,
209 ICMP_ERROR_RT_EXCEEDED,
210 ICMP_ERROR_FL_TOO_BIG,
211 ICMP_ERROR_FL_NOT_MULT_8,
212 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000213 HANDOFF,
Klement Sekera01c1fa42021-12-14 18:25:11 +0000214 PASSTHROUGH,
Klement Sekera896c8962019-06-24 11:52:49 +0000215} ip6_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200216
217typedef struct
218{
219 u16 range_first;
220 u16 range_last;
221 u32 range_bi;
222 i32 data_offset;
223 u32 data_len;
224 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000225} ip6_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200226
227typedef struct
228{
Klement Sekera896c8962019-06-24 11:52:49 +0000229 ip6_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200230 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000231 ip6_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200232 u32 op_id;
233 u32 fragment_first;
234 u32 fragment_last;
235 u32 total_data_len;
Klement Sekera630ab582019-07-19 09:14:19 +0000236 u32 thread_id;
237 u32 thread_id_to;
Klement Sekera8563cb32019-10-10 17:03:57 +0000238 bool is_after_handoff;
239 ip6_header_t ip6_header;
240 ip6_frag_hdr_t ip6_frag_header;
Klement Sekera896c8962019-06-24 11:52:49 +0000241} ip6_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200242
243static void
Klement Sekera896c8962019-06-24 11:52:49 +0000244ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
245 ip6_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200246{
247 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
248 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
249 trace->range_first = vnb->ip.reass.range_first;
250 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000251 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
252 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200253 trace->range_bi = bi;
254}
255
Klement Sekera4c533132018-02-22 11:41:12 +0100256static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000257format_ip6_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200258{
Klement Sekera896c8962019-06-24 11:52:49 +0000259 ip6_full_reass_range_trace_t *trace =
260 va_arg (*args, ip6_full_reass_range_trace_t *);
261 s =
262 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
263 trace->range_last, trace->data_offset, trace->data_len,
264 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200265 return s;
266}
267
Klement Sekera4c533132018-02-22 11:41:12 +0100268static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000269format_ip6_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200270{
271 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
272 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000273 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000274 u32 indent = 0;
275 if (~0 != t->reass_id)
276 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000277 if (t->is_after_handoff)
278 {
279 s =
280 format (s, "%U\n", format_ip6_header, &t->ip6_header,
281 sizeof (t->ip6_header));
282 s =
283 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
284 sizeof (t->ip6_frag_header));
285 indent = 2;
286 }
287 s =
288 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
289 t->reass_id, t->op_id);
Klement Sekera630ab582019-07-19 09:14:19 +0000290 indent = format_get_indent (s);
291 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
292 t->trace_range.first_bi, t->total_data_len,
293 t->fragment_first, t->fragment_last);
294 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200295 switch (t->action)
296 {
297 case RANGE_NEW:
298 s = format (s, "\n%Unew %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000299 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200300 break;
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000301 case RANGE_DISCARD:
302 s = format (s, "\n%Udiscard %U", format_white_space, indent,
303 format_ip6_full_reass_range_trace, &t->trace_range);
304 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200305 case RANGE_OVERLAP:
306 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000307 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200308 break;
309 case ICMP_ERROR_FL_TOO_BIG:
310 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000311 format_white_space, indent,
312 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200313 break;
314 case ICMP_ERROR_FL_NOT_MULT_8:
315 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000316 format_white_space, indent,
317 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200318 break;
319 case ICMP_ERROR_RT_EXCEEDED:
320 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
321 format_white_space, indent);
322 break;
323 case FINALIZE:
324 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
325 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000326 case HANDOFF:
327 s =
328 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
329 t->thread_id_to);
330 break;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000331 case PASSTHROUGH:
332 s = format (s, "passthrough - not a fragment");
333 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200334 }
335 return s;
336}
337
338static void
Klement Sekera896c8962019-06-24 11:52:49 +0000339ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000340 ip6_full_reass_t * reass, u32 bi,
Klement Sekera8563cb32019-10-10 17:03:57 +0000341 ip6_frag_hdr_t * ip6_frag_header,
Klement Sekera896c8962019-06-24 11:52:49 +0000342 ip6_full_reass_trace_operation_e action,
343 u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200344{
345 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
346 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera8563cb32019-10-10 17:03:57 +0000347 bool is_after_handoff = false;
Klement Sekera53be16d2020-12-15 21:47:36 +0100348 if (pool_is_free_index
349 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
350 {
351 // this buffer's trace is gone
352 b->flags &= ~VLIB_BUFFER_IS_TRACED;
353 return;
354 }
Klement Sekera8563cb32019-10-10 17:03:57 +0000355 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
356 {
357 is_after_handoff = true;
358 }
Klement Sekera896c8962019-06-24 11:52:49 +0000359 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000360 t->is_after_handoff = is_after_handoff;
361 if (t->is_after_handoff)
362 {
363 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
364 clib_min (sizeof (t->ip6_header), b->current_length));
365 if (ip6_frag_header)
366 {
367 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
368 sizeof (t->ip6_frag_header));
369 }
370 else
371 {
372 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
373 }
374 }
Klement Sekera896c8962019-06-24 11:52:49 +0000375 if (reass)
376 {
377 t->reass_id = reass->id;
378 t->op_id = reass->trace_op_counter;
379 t->trace_range.first_bi = reass->first_bi;
380 t->total_data_len = reass->data_len;
381 ++reass->trace_op_counter;
382 }
383 else
384 {
385 t->reass_id = ~0;
386 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200387 t->action = action;
Klement Sekera630ab582019-07-19 09:14:19 +0000388 t->thread_id = vm->thread_index;
389 t->thread_id_to = thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000390 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200391 t->fragment_first = vnb->ip.reass.fragment_first;
392 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200393#if 0
394 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000395 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200396 printf ("%.*s\n", vec_len (s), s);
397 fflush (stdout);
398 vec_reset_length (s);
399#endif
400}
401
Klement Sekera4c533132018-02-22 11:41:12 +0100402always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000403ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
404 ip6_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000405{
406 pool_put (rt->pool, reass);
407 --rt->reass_n;
408}
409
410always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000411ip6_full_reass_free (ip6_full_reass_main_t * rm,
412 ip6_full_reass_per_thread_t * rt,
413 ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200414{
415 clib_bihash_kv_48_8_t kv;
416 kv.key[0] = reass->key.as_u64[0];
417 kv.key[1] = reass->key.as_u64[1];
418 kv.key[2] = reass->key.as_u64[2];
419 kv.key[3] = reass->key.as_u64[3];
420 kv.key[4] = reass->key.as_u64[4];
421 kv.key[5] = reass->key.as_u64[5];
422 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000423 ip6_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200424}
425
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000426/* n_left_to_next, and to_next are taken as input params, as this function
427 * could be called from a graphnode, where its managing local copy of these
428 * variables, and ignoring those and still trying to enqueue the buffers
429 * with local variables would cause either buffer leak or corruption */
Klement Sekera4c533132018-02-22 11:41:12 +0100430always_inline void
Klement Sekera42cec0e2021-08-02 16:14:15 +0200431ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000432 ip6_full_reass_t *reass, u32 *n_left_to_next,
433 u32 **to_next)
Klement Sekera75e7d132017-09-20 08:26:30 +0200434{
435 u32 range_bi = reass->first_bi;
436 vlib_buffer_t *range_b;
437 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100438 u32 *to_free = NULL;
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000439
Klement Sekera75e7d132017-09-20 08:26:30 +0200440 while (~0 != range_bi)
441 {
442 range_b = vlib_get_buffer (vm, range_bi);
443 range_vnb = vnet_buffer (range_b);
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000444
445 if (~0 != range_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200446 {
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000447 vec_add1 (to_free, range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200448 }
449 range_bi = range_vnb->ip.reass.next_range_bi;
450 }
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000451
Klement Sekera21aa8f12019-05-20 12:27:33 +0200452 /* send to next_error_index */
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000453 if (~0 != reass->error_next_index &&
454 reass->error_next_index < node->n_next_nodes)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200455 {
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000456 u32 next_index;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200457
458 next_index = reass->error_next_index;
459 u32 bi = ~0;
460
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +0000461 /* record number of packets sent to custom app */
462 vlib_node_increment_counter (vm, node->node_index,
463 IP6_ERROR_REASS_TO_CUSTOM_APP,
464 vec_len (to_free));
465
Klement Sekera21aa8f12019-05-20 12:27:33 +0200466 while (vec_len (to_free) > 0)
467 {
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000468 vlib_get_next_frame (vm, node, next_index, *to_next,
469 (*n_left_to_next));
Klement Sekera21aa8f12019-05-20 12:27:33 +0200470
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000471 while (vec_len (to_free) > 0 && (*n_left_to_next) > 0)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200472 {
473 bi = vec_pop (to_free);
474
475 if (~0 != bi)
476 {
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000477 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
478 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
479 {
480 ip6_full_reass_add_trace (vm, node, reass, bi, NULL,
481 RANGE_DISCARD, ~0);
482 }
483 *to_next[0] = bi;
484 (*to_next) += 1;
485 (*n_left_to_next) -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200486 }
487 }
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000488 vlib_put_next_frame (vm, node, next_index, (*n_left_to_next));
Klement Sekera21aa8f12019-05-20 12:27:33 +0200489 }
490 }
491 else
492 {
493 vlib_buffer_free (vm, to_free, vec_len (to_free));
494 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100495 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200496}
497
Klement Sekera4c533132018-02-22 11:41:12 +0100498always_inline void
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000499sanitize_reass_buffers_add_missing (vlib_main_t *vm, ip6_full_reass_t *reass,
500 u32 *bi0)
501{
502 u32 range_bi = reass->first_bi;
503 vlib_buffer_t *range_b;
504 vnet_buffer_opaque_t *range_vnb;
505
506 while (~0 != range_bi)
507 {
508 range_b = vlib_get_buffer (vm, range_bi);
509 range_vnb = vnet_buffer (range_b);
510 u32 bi = range_bi;
511 if (~0 != bi)
512 {
513 if (bi == *bi0)
514 *bi0 = ~0;
515 if (range_b->flags & VLIB_BUFFER_NEXT_PRESENT)
516 {
517 u32 _bi = bi;
518 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi);
519 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)
520 {
521 if (_b->next_buffer != range_vnb->ip.reass.next_range_bi)
522 {
523 _bi = _b->next_buffer;
524 _b = vlib_get_buffer (vm, _bi);
525 }
526 else
527 {
528 _b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
529 break;
530 }
531 }
532 }
533 range_bi = range_vnb->ip.reass.next_range_bi;
534 }
535 }
536 if (*bi0 != ~0)
537 {
538 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
539 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
540 if (~0 != reass->first_bi)
541 {
542 fvnb->ip.reass.next_range_bi = reass->first_bi;
543 reass->first_bi = *bi0;
544 }
545 else
546 {
547 reass->first_bi = *bi0;
548 fvnb->ip.reass.next_range_bi = ~0;
549 }
550 *bi0 = ~0;
551 }
552}
553
554always_inline void
555ip6_full_reass_on_timeout (vlib_main_t *vm, vlib_node_runtime_t *node,
556 ip6_full_reass_t *reass, u32 *icmp_bi,
557 u32 *n_left_to_next, u32 **to_next)
Klement Sekera75e7d132017-09-20 08:26:30 +0200558{
559 if (~0 == reass->first_bi)
560 {
561 return;
562 }
Klement Sekerae8498652019-06-17 12:23:15 +0000563 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200564 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200565 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
566 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200567 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200568 *icmp_bi = reass->first_bi;
569 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
570 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200571 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
572 ICMP_ERROR_RT_EXCEEDED, ~0);
Klement Sekera21aa8f12019-05-20 12:27:33 +0200573 }
574 // fragment with offset zero received - send icmp message back
575 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
576 {
577 // separate first buffer from chain and steer it towards icmp node
578 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
579 reass->first_bi = b->next_buffer;
580 }
581 else
582 {
583 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
584 }
585 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
586 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
587 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200588 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200589 }
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000590 ip6_full_reass_drop_all (vm, node, reass, n_left_to_next, to_next);
Klement Sekera75e7d132017-09-20 08:26:30 +0200591}
592
Klement Sekera896c8962019-06-24 11:52:49 +0000593always_inline ip6_full_reass_t *
Klement Sekera7c3275e2021-12-07 09:49:53 +0000594ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
595 ip6_full_reass_main_t *rm,
596 ip6_full_reass_per_thread_t *rt,
597 ip6_full_reass_kv_t *kv, u32 *icmp_bi,
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000598 u8 *do_handoff, int skip_bihash,
599 u32 *n_left_to_next, u32 **to_next)
Klement Sekera75e7d132017-09-20 08:26:30 +0200600{
Klement Sekera896c8962019-06-24 11:52:49 +0000601 ip6_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000602 f64 now;
603
604again:
605
606 reass = NULL;
607 now = vlib_time_now (vm);
Klement Sekera75e7d132017-09-20 08:26:30 +0200608
Klement Sekera7c3275e2021-12-07 09:49:53 +0000609 if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200610 {
Gao Feng9165e032020-04-26 09:57:18 +0800611 if (vm->thread_index != kv->v.memory_owner_thread_index)
612 {
613 *do_handoff = 1;
614 return NULL;
615 }
616
Klement Sekera630ab582019-07-19 09:14:19 +0000617 reass =
618 pool_elt_at_index (rm->per_thread_data
619 [kv->v.memory_owner_thread_index].pool,
620 kv->v.reass_index);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800621
Klement Sekera75e7d132017-09-20 08:26:30 +0200622 if (now > reass->last_heard + rm->timeout)
623 {
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +0000624 vlib_node_increment_counter (vm, node->node_index,
625 IP6_ERROR_REASS_TIMEOUT, 1);
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +0000626 ip6_full_reass_on_timeout (vm, node, reass, icmp_bi, n_left_to_next,
627 to_next);
Klement Sekera896c8962019-06-24 11:52:49 +0000628 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200629 reass = NULL;
630 }
631 }
632
633 if (reass)
634 {
635 reass->last_heard = now;
636 return reass;
637 }
638
Klement Sekera4c533132018-02-22 11:41:12 +0100639 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200640 {
641 reass = NULL;
642 return reass;
643 }
644 else
645 {
Klement Sekera4c533132018-02-22 11:41:12 +0100646 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400647 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800648 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100649 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200650 reass->first_bi = ~0;
651 reass->last_packet_octet = ~0;
652 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000653 reass->next_index = ~0;
654 reass->error_next_index = ~0;
Klement Sekera8d4db8b2022-01-24 22:30:04 +0000655 reass->memory_owner_thread_index = vm->thread_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100656 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200657 }
658
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800659 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000660 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200661 reass->last_heard = now;
662
Klement Sekera7c3275e2021-12-07 09:49:53 +0000663 if (!skip_bihash)
Klement Sekera75e7d132017-09-20 08:26:30 +0200664 {
Klement Sekera7c3275e2021-12-07 09:49:53 +0000665 reass->key.as_u64[0] = kv->kv.key[0];
666 reass->key.as_u64[1] = kv->kv.key[1];
667 reass->key.as_u64[2] = kv->kv.key[2];
668 reass->key.as_u64[3] = kv->kv.key[3];
669 reass->key.as_u64[4] = kv->kv.key[4];
670 reass->key.as_u64[5] = kv->kv.key[5];
671
672 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
673 if (rv)
674 {
675 ip6_full_reass_free (rm, rt, reass);
676 reass = NULL;
677 // if other worker created a context already work with the other copy
678 if (-2 == rv)
679 goto again;
680 }
681 }
682 else
683 {
684 reass->key.as_u64[0] = ~0;
685 reass->key.as_u64[1] = ~0;
686 reass->key.as_u64[2] = ~0;
687 reass->key.as_u64[3] = ~0;
688 reass->key.as_u64[4] = ~0;
689 reass->key.as_u64[5] = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200690 }
691
692 return reass;
693}
694
Klement Sekera896c8962019-06-24 11:52:49 +0000695always_inline ip6_full_reass_rc_t
696ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
697 ip6_full_reass_main_t * rm,
698 ip6_full_reass_per_thread_t * rt,
699 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
700 u32 * error0, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200701{
Klement Sekera75e7d132017-09-20 08:26:30 +0200702 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200703 *error0 = IP6_ERROR_NONE;
704 ip6_frag_hdr_t *frag_hdr;
705 vlib_buffer_t *last_b = NULL;
706 u32 sub_chain_bi = reass->first_bi;
707 u32 total_length = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100708 u32 *vec_drop_compress = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000709 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200710 do
711 {
712 u32 tmp_bi = sub_chain_bi;
713 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100714 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
715 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
716 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
717 {
Klement Sekera896c8962019-06-24 11:52:49 +0000718 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100719 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100720 }
721
Klement Sekera896c8962019-06-24 11:52:49 +0000722 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200723 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
Klement Sekera896c8962019-06-24 11:52:49 +0000724 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200725 u32 trim_end =
726 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
727 if (tmp_bi == reass->first_bi)
728 {
729 /* first buffer - keep ip6 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000730 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100731 {
Klement Sekera896c8962019-06-24 11:52:49 +0000732 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100733 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100734 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200735 trim_front = 0;
736 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
737 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
738 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100739 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
740 {
Klement Sekera896c8962019-06-24 11:52:49 +0000741 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100742 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100743 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200744 }
745 u32 keep_data =
746 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
747 while (1)
748 {
Klement Sekera75e7d132017-09-20 08:26:30 +0200749 if (trim_front)
750 {
751 if (trim_front > tmp->current_length)
752 {
753 /* drop whole buffer */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100754 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
755 {
Klement Sekera896c8962019-06-24 11:52:49 +0000756 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100757 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100758 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200759 trim_front -= tmp->current_length;
760 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200761 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
762 tmp_bi = tmp->next_buffer;
763 tmp = vlib_get_buffer (vm, tmp_bi);
764 continue;
765 }
766 else
767 {
768 vlib_buffer_advance (tmp, trim_front);
769 trim_front = 0;
770 }
771 }
772 if (keep_data)
773 {
774 if (last_b)
775 {
776 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
777 last_b->next_buffer = tmp_bi;
778 }
779 last_b = tmp;
780 if (keep_data <= tmp->current_length)
781 {
782 tmp->current_length = keep_data;
783 keep_data = 0;
784 }
785 else
786 {
787 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100788 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
789 {
Klement Sekera896c8962019-06-24 11:52:49 +0000790 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100791 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100792 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200793 }
794 total_length += tmp->current_length;
795 }
796 else
797 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100798 if (reass->first_bi == tmp_bi)
799 {
Klement Sekera896c8962019-06-24 11:52:49 +0000800 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100801 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100802 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200803 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200804 }
805 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
806 {
807 tmp_bi = tmp->next_buffer;
808 tmp = vlib_get_buffer (vm, tmp->next_buffer);
809 }
810 else
811 {
812 break;
813 }
814 }
815 sub_chain_bi =
816 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
817 reass.next_range_bi;
818 }
819 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700820
Klement Sekerad0f70a32018-12-14 17:24:13 +0100821 if (!last_b)
822 {
Klement Sekera896c8962019-06-24 11:52:49 +0000823 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100824 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100825 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200826 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
827 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100828 if (total_length < first_b->current_length)
829 {
Klement Sekera896c8962019-06-24 11:52:49 +0000830 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100831 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100832 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200833 total_length -= first_b->current_length;
834 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
835 first_b->total_length_not_including_first_buffer = total_length;
836 // drop fragment header
837 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
838 ip6_header_t *ip = vlib_buffer_get_current (first_b);
839 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
Ole Troan03092c12021-11-23 15:55:39 +0100840 ip6_ext_hdr_chain_t hdr_chain;
841 ip6_ext_header_t *prev_hdr = 0;
842 int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
843 &hdr_chain);
844 if (res < 0 ||
845 (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
Klement Sekera75e7d132017-09-20 08:26:30 +0200846 {
Ole Troan03092c12021-11-23 15:55:39 +0100847 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
848 goto free_buffers_and_return;
849 }
850 frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
851 if (res > 0)
852 {
853 prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
Klement Sekera75e7d132017-09-20 08:26:30 +0200854 prev_hdr->next_hdr = frag_hdr->next_hdr;
855 }
856 else
857 {
858 ip->protocol = frag_hdr->next_hdr;
859 }
Ole Troan03092c12021-11-23 15:55:39 +0100860 if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
Klement Sekerad0f70a32018-12-14 17:24:13 +0100861 {
Klement Sekera896c8962019-06-24 11:52:49 +0000862 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100863 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100864 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200865 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
866 first_b->current_length - ip6_frag_hdr_offset -
867 sizeof (ip6_frag_hdr_t));
868 first_b->current_length -= sizeof (*frag_hdr);
869 ip->payload_length =
870 clib_host_to_net_u16 (total_length + first_b->current_length -
871 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100872 if (!vlib_buffer_chain_linearize (vm, first_b))
873 {
Klement Sekera896c8962019-06-24 11:52:49 +0000874 rv = IP6_FULL_REASS_RC_NO_BUF;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100875 goto free_buffers_and_return;
876 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700877 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200878 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
879 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200880 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
Klement Sekera896c8962019-06-24 11:52:49 +0000881 FINALIZE, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200882#if 0
883 // following code does a hexdump of packet fragments to stdout ...
884 do
885 {
886 u32 bi = reass->first_bi;
887 u8 *s = NULL;
888 while (~0 != bi)
889 {
890 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
891 s = format (s, "%u: %U\n", bi, format_hexdump,
892 vlib_buffer_get_current (b), b->current_length);
893 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
894 {
895 bi = b->next_buffer;
896 }
897 else
898 {
899 break;
900 }
901 }
902 printf ("%.*s\n", vec_len (s), s);
903 fflush (stdout);
904 vec_free (s);
905 }
906 while (0);
907#endif
908 }
Klement Sekerae8498652019-06-17 12:23:15 +0000909 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100910 {
Klement Sekera896c8962019-06-24 11:52:49 +0000911 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100912 }
913 else
914 {
915 *next0 = reass->next_index;
916 }
917 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +0000918 /* Keep track of number of successfully reassembled packets and number of
919 * fragments reassembled */
920 vlib_node_increment_counter (vm, node->node_index, IP6_ERROR_REASS_SUCCESS,
921 1);
922
923 vlib_node_increment_counter (vm, node->node_index,
924 IP6_ERROR_REASS_FRAGMENTS_REASSEMBLED,
925 reass->fragments_n);
926
Klement Sekera896c8962019-06-24 11:52:49 +0000927 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200928 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100929free_buffers_and_return:
930 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
931 vec_free (vec_drop_compress);
932 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200933}
934
Klement Sekera4c533132018-02-22 11:41:12 +0100935always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000936ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +0000937 ip6_full_reass_t * reass,
938 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200939{
940
941 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
942 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
943 if (~0 != prev_range_bi)
944 {
945 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
946 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
947 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
948 prev_vnb->ip.reass.next_range_bi = new_next_bi;
949 }
950 else
951 {
952 if (~0 != reass->first_bi)
953 {
954 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
955 }
956 reass->first_bi = new_next_bi;
957 }
Klement Sekera896c8962019-06-24 11:52:49 +0000958 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200959}
960
Klement Sekera896c8962019-06-24 11:52:49 +0000961always_inline ip6_full_reass_rc_t
Klement Sekera7c3275e2021-12-07 09:49:53 +0000962ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
963 ip6_full_reass_main_t *rm,
964 ip6_full_reass_per_thread_t *rt,
965 ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
966 u32 *error0, ip6_frag_hdr_t *frag_hdr,
967 bool is_custom_app, u32 *handoff_thread_idx,
968 int skip_bihash)
Klement Sekera75e7d132017-09-20 08:26:30 +0200969{
970 int consumed = 0;
971 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
972 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000973 if (is_custom_app)
974 {
975 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
976 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
977 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200978
Klement Sekera75e7d132017-09-20 08:26:30 +0200979 fvnb->ip.reass.ip6_frag_hdr_offset =
980 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
981 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100982 if (fb->current_length < sizeof (*fip) ||
983 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
984 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
985 {
Klement Sekera896c8962019-06-24 11:52:49 +0000986 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100987 }
988
Klement Sekera75e7d132017-09-20 08:26:30 +0200989 u32 fragment_first = fvnb->ip.reass.fragment_first =
990 ip6_frag_hdr_offset_bytes (frag_hdr);
991 u32 fragment_length =
992 vlib_buffer_length_in_chain (vm, fb) -
993 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
Klement Sekera755042d2021-12-01 10:14:38 +0000994 if (0 == fragment_length)
995 {
996 return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
997 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200998 u32 fragment_last = fvnb->ip.reass.fragment_last =
999 fragment_first + fragment_length - 1;
1000 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1001 u32 candidate_range_bi = reass->first_bi;
1002 u32 prev_range_bi = ~0;
1003 fvnb->ip.reass.range_first = fragment_first;
1004 fvnb->ip.reass.range_last = fragment_last;
1005 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001006 if (!more_fragments)
1007 {
1008 reass->last_packet_octet = fragment_last;
1009 }
1010 if (~0 == reass->first_bi)
1011 {
1012 // starting a new reassembly
Klement Sekera42cec0e2021-08-02 16:14:15 +02001013 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +01001014 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +01001015 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +02001016 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +01001017 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +02001018 }
Klement Sekera4c533132018-02-22 11:41:12 +01001019 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +02001020 clib_min (clib_net_to_host_u16 (fip->payload_length),
1021 fvnb->ip.reass.estimated_mtu);
1022 while (~0 != candidate_range_bi)
1023 {
1024 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
1025 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
1026 if (fragment_first > candidate_vnb->ip.reass.range_last)
1027 {
1028 // this fragments starts after candidate range
1029 prev_range_bi = candidate_range_bi;
1030 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
1031 if (candidate_vnb->ip.reass.range_last < fragment_last &&
1032 ~0 == candidate_range_bi)
1033 {
1034 // special case - this fragment falls beyond all known ranges
Klement Sekera42cec0e2021-08-02 16:14:15 +02001035 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1036 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001037 consumed = 1;
1038 break;
1039 }
1040 continue;
1041 }
1042 if (fragment_last < candidate_vnb->ip.reass.range_first)
1043 {
1044 // this fragment ends before candidate range without any overlap
Klement Sekera42cec0e2021-08-02 16:14:15 +02001045 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1046 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001047 consumed = 1;
1048 }
1049 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
1050 fragment_last == candidate_vnb->ip.reass.range_last)
1051 {
1052 // duplicate fragment - ignore
1053 }
1054 else
1055 {
1056 // overlapping fragment - not allowed by RFC 8200
Klement Sekera75e7d132017-09-20 08:26:30 +02001057 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1058 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001059 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
Klement Sekera896c8962019-06-24 11:52:49 +00001060 RANGE_OVERLAP, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001061 }
Klement Sekerae63a2d42021-05-20 12:23:00 +02001062 return IP6_FULL_REASS_RC_OVERLAP;
Klement Sekera75e7d132017-09-20 08:26:30 +02001063 }
1064 break;
1065 }
Klement Sekera3a343d42019-05-16 14:35:46 +02001066 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +01001067check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +02001068 if (consumed)
1069 {
1070 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1071 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001072 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
1073 ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001074 }
1075 }
Klement Sekera7c3275e2021-12-07 09:49:53 +00001076 else if (skip_bihash)
1077 {
1078 // if this reassembly is not in bihash, then the packet must have been
1079 // consumed
1080 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1081 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001082 if (~0 != reass->last_packet_octet &&
1083 reass->data_len == reass->last_packet_octet + 1)
1084 {
Klement Sekera630ab582019-07-19 09:14:19 +00001085 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001086 int handoff =
1087 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001088 ip6_full_reass_rc_t rc =
1089 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1090 is_custom_app);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001091 if (IP6_FULL_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +00001092 {
Klement Sekera896c8962019-06-24 11:52:49 +00001093 return IP6_FULL_REASS_RC_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001094 }
1095 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +02001096 }
1097 else
1098 {
Klement Sekera7c3275e2021-12-07 09:49:53 +00001099 if (skip_bihash)
1100 {
1101 // if this reassembly is not in bihash, it should've been an atomic
1102 // fragment and thus finalized
1103 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1104 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001105 if (consumed)
1106 {
1107 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +02001108 if (reass->fragments_n > rm->max_reass_len)
1109 {
Klement Sekera896c8962019-06-24 11:52:49 +00001110 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
Klement Sekera3a343d42019-05-16 14:35:46 +02001111 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001112 }
1113 else
1114 {
Klement Sekera896c8962019-06-24 11:52:49 +00001115 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +02001116 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1117 }
1118 }
Klement Sekera896c8962019-06-24 11:52:49 +00001119 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +02001120}
1121
Klement Sekera4c533132018-02-22 11:41:12 +01001122always_inline bool
Ole Troan03092c12021-11-23 15:55:39 +01001123ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1124 vlib_buffer_t *b,
1125 ip6_ext_hdr_chain_t *hc)
Klement Sekera4c533132018-02-22 11:41:12 +01001126{
Ole Troan03092c12021-11-23 15:55:39 +01001127 int nh = hc->eh[hc->length - 1].protocol;
1128 /* Checking to see if it's a terminating header */
1129 if (ip6_ext_hdr (nh))
Klement Sekera4c533132018-02-22 11:41:12 +01001130 {
Ole Troan03092c12021-11-23 15:55:39 +01001131 icmp6_error_set_vnet_buffer (
1132 b, ICMP6_parameter_problem,
1133 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001134 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
Klement Sekera4c533132018-02-22 11:41:12 +01001135 return false;
1136 }
1137 return true;
1138}
1139
1140always_inline bool
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001141ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t *vm,
1142 vlib_node_runtime_t *node,
1143 vlib_buffer_t *b,
1144 ip6_frag_hdr_t *frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001145{
1146 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1147 ip6_header_t *ip = vlib_buffer_get_current (b);
1148 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1149 u32 fragment_length =
1150 vlib_buffer_length_in_chain (vm, b) -
1151 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1152 if (more_fragments && 0 != fragment_length % 8)
1153 {
1154 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1155 ICMP6_parameter_problem_erroneous_header_field,
1156 (u8 *) & ip->payload_length - (u8 *) ip);
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001157 b->error = node->errors[IP6_ERROR_REASS_INVALID_FRAG_SIZE];
Klement Sekera4c533132018-02-22 11:41:12 +01001158 return false;
1159 }
1160 return true;
1161}
1162
1163always_inline bool
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001164ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t *vm,
1165 vlib_node_runtime_t *node,
1166 vlib_buffer_t *b,
1167 ip6_frag_hdr_t *frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001168{
1169 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1170 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1171 u32 fragment_length =
1172 vlib_buffer_length_in_chain (vm, b) -
1173 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1174 if (fragment_first + fragment_length > 65535)
1175 {
1176 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1177 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1178 ICMP6_parameter_problem_erroneous_header_field,
1179 (u8 *) & frag_hdr->fragment_offset_and_more
1180 - (u8 *) ip0);
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001181 b->error = node->errors[IP6_ERROR_REASS_INVALID_FRAG_SIZE];
Klement Sekera4c533132018-02-22 11:41:12 +01001182 return false;
1183 }
1184 return true;
1185}
1186
Klement Sekera75e7d132017-09-20 08:26:30 +02001187always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00001188ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1189 vlib_frame_t *frame, bool is_feature,
1190 bool is_custom_app, bool is_local)
Klement Sekera75e7d132017-09-20 08:26:30 +02001191{
1192 u32 *from = vlib_frame_vector_args (frame);
1193 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001194 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1195 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001196 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001197
1198 n_left_from = frame->n_vectors;
1199 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001200 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001201 {
1202 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1203
Klement Sekera75e7d132017-09-20 08:26:30 +02001204 while (n_left_from > 0 && n_left_to_next > 0)
1205 {
1206 u32 bi0;
1207 vlib_buffer_t *b0;
Klement Sekera896c8962019-06-24 11:52:49 +00001208 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001209 u32 error0 = IP6_ERROR_NONE;
1210 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001211
1212 bi0 = from[0];
1213 b0 = vlib_get_buffer (vm, bi0);
1214
1215 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001216 ip6_frag_hdr_t *frag_hdr = NULL;
Ole Troan03092c12021-11-23 15:55:39 +01001217 ip6_ext_hdr_chain_t hdr_chain;
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001218 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1219
Ole Troan03092c12021-11-23 15:55:39 +01001220 int res = ip6_ext_header_walk (
1221 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1222 if (res < 0 ||
1223 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
Klement Sekera3ecc2212018-03-27 10:34:43 +02001224 {
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001225 vlib_node_increment_counter (vm, node->node_index,
1226 IP6_ERROR_REASS_NO_FRAG_HDR, 1);
Ole Troan03092c12021-11-23 15:55:39 +01001227 // this is a mangled packet - no fragmentation
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001228 next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
1229 IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera01c1fa42021-12-14 18:25:11 +00001230 ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
1231 ~0);
1232 goto skip_reass;
1233 }
1234 if (is_local && !rm->is_local_reass_enabled)
1235 {
1236 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001237 goto skip_reass;
1238 }
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001239
1240 /* Keep track of received fragments */
1241 vlib_node_increment_counter (vm, node->node_index,
1242 IP6_ERROR_REASS_FRAGMENTS_RCVD, 1);
Ole Troan03092c12021-11-23 15:55:39 +01001243 frag_hdr =
1244 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001245 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
Ole Troan03092c12021-11-23 15:55:39 +01001246 hdr_chain.eh[res].offset;
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001247
Klement Sekera75e7d132017-09-20 08:26:30 +02001248 if (0 == ip6_frag_hdr_offset (frag_hdr))
1249 {
1250 // first fragment - verify upper-layer is present
Ole Troan03092c12021-11-23 15:55:39 +01001251 if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1252 &hdr_chain))
Klement Sekera75e7d132017-09-20 08:26:30 +02001253 {
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001254 next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
1255 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001256 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001257 }
Klement Sekera4c533132018-02-22 11:41:12 +01001258 }
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001259
1260 if (!ip6_full_reass_verify_fragment_multiple_8 (vm, node, b0,
1261 frag_hdr) ||
1262 !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
1263 frag_hdr))
Klement Sekera4c533132018-02-22 11:41:12 +01001264 {
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001265 next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
1266 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001267 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001268 }
Klement Sekera7c3275e2021-12-07 09:49:53 +00001269
1270 int skip_bihash = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001271 ip6_full_reass_kv_t kv;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001272 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001273
Klement Sekera7c3275e2021-12-07 09:49:53 +00001274 if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1275 !ip6_frag_hdr_more (frag_hdr))
1276 {
1277 // this is atomic fragment and needs to be processed separately
1278 skip_bihash = 1;
1279 }
1280 else
1281 {
1282 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1283 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1284 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1285 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1286 kv.k.as_u64[4] =
1287 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1288 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1289 << 32 |
1290 (u64) frag_hdr->identification;
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001291 /* RFC 8200: The Next Header values in the Fragment headers of
1292 * different fragments of the same original packet may differ.
1293 * Only the value from the Offset zero fragment packet is used
1294 * for reassembly.
1295 *
1296 * Also, IPv6 Header doesnt contain the protocol value unlike
1297 * IPv4.*/
1298 kv.k.as_u64[5] = 0;
Klement Sekera7c3275e2021-12-07 09:49:53 +00001299 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001300
Klement Sekera7c3275e2021-12-07 09:49:53 +00001301 ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +00001302 vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash,
1303 &n_left_to_next, &to_next);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001304
Klement Sekera630ab582019-07-19 09:14:19 +00001305 if (reass)
1306 {
1307 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1308 if (0 == fragment_first)
1309 {
1310 reass->sendout_thread_index = vm->thread_index;
1311 }
1312 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001313 if (PREDICT_FALSE (do_handoff))
1314 {
Klement Sekera896c8962019-06-24 11:52:49 +00001315 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001316 vnet_buffer (b0)->ip.reass.owner_thread_index =
1317 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001318 }
1319 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001320 {
Klement Sekera630ab582019-07-19 09:14:19 +00001321 u32 handoff_thread_idx;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001322 u32 counter = ~0;
Klement Sekera7c3275e2021-12-07 09:49:53 +00001323 switch (ip6_full_reass_update (
1324 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1325 is_custom_app, &handoff_thread_idx, skip_bihash))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001326 {
Klement Sekera896c8962019-06-24 11:52:49 +00001327 case IP6_FULL_REASS_RC_OK:
Klement Sekerad0f70a32018-12-14 17:24:13 +01001328 /* nothing to do here */
1329 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001330 case IP6_FULL_REASS_RC_HANDOFF:
1331 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001332 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001333 vnet_buffer (b0)->ip.reass.owner_thread_index =
1334 handoff_thread_idx;
Klement Sekera630ab582019-07-19 09:14:19 +00001335 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001336 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001337 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
Klement Sekera3a343d42019-05-16 14:35:46 +02001338 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001339 case IP6_FULL_REASS_RC_NO_BUF:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001340 counter = IP6_ERROR_REASS_NO_BUF;
Klement Sekera3a343d42019-05-16 14:35:46 +02001341 break;
Klement Sekera755042d2021-12-01 10:14:38 +00001342 case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
1343 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
1344 break;
Klement Sekerae63a2d42021-05-20 12:23:00 +02001345 case IP6_FULL_REASS_RC_OVERLAP:
1346 counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
1347 break;
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +00001348 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1349 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1350 /* Sanitization is needed in internal error cases only, as
1351 * the incoming packet is already dropped in other cases,
1352 * also adding bi0 back to the reassembly list, fixes the
1353 * leaking of buffers during internal errors.
1354 *
1355 * Also it doesnt make sense to send these buffers custom
1356 * app, these fragments are with internal errors */
1357 sanitize_reass_buffers_add_missing (vm, reass, &bi0);
1358 reass->error_next_index = ~0;
1359 break;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001360 }
1361 if (~0 != counter)
1362 {
1363 vlib_node_increment_counter (vm, node->node_index, counter,
Klement Sekera3a343d42019-05-16 14:35:46 +02001364 1);
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +00001365 ip6_full_reass_drop_all (vm, node, reass, &n_left_to_next,
1366 &to_next);
Klement Sekera896c8962019-06-24 11:52:49 +00001367 ip6_full_reass_free (rm, rt, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001368 goto next_packet;
Klement Sekerae63a2d42021-05-20 12:23:00 +02001369 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001370 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001371 }
1372 else
1373 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001374 if (is_feature)
1375 {
Klement Sekera896c8962019-06-24 11:52:49 +00001376 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera21aa8f12019-05-20 12:27:33 +02001377 }
1378 else
1379 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001380 next0 = fvnb->ip.reass.error_next_index;
1381 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001382 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1383 }
1384
Klement Sekera75e7d132017-09-20 08:26:30 +02001385 if (~0 != bi0)
1386 {
Klement Sekera4c533132018-02-22 11:41:12 +01001387 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001388 to_next[0] = bi0;
1389 to_next += 1;
1390 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001391
1392 /* bi0 might have been updated by reass_finalize, reload */
1393 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001394 if (IP6_ERROR_NONE != error0)
1395 {
1396 b0->error = node->errors[error0];
1397 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001398
Klement Sekera896c8962019-06-24 11:52:49 +00001399 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001400 {
1401 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1402 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001403 ip6_full_reass_add_trace (
1404 vm, node, NULL, bi0, frag_hdr, HANDOFF,
1405 vnet_buffer (b0)->ip.reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001406 }
1407 }
1408 else if (is_feature && IP6_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001409 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001410 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001411 }
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001412
1413 /* Increment the counter to-custom-app also as this fragment is
1414 * also going to application */
1415 if (is_custom_app)
1416 {
1417 vlib_node_increment_counter (
1418 vm, node->node_index, IP6_ERROR_REASS_TO_CUSTOM_APP, 1);
1419 }
1420
Klement Sekera75e7d132017-09-20 08:26:30 +02001421 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1422 n_left_to_next, bi0, next0);
1423 }
1424
1425 if (~0 != icmp_bi)
1426 {
Klement Sekera896c8962019-06-24 11:52:49 +00001427 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera75e7d132017-09-20 08:26:30 +02001428 to_next[0] = icmp_bi;
1429 to_next += 1;
1430 n_left_to_next -= 1;
1431 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1432 n_left_to_next, icmp_bi,
1433 next0);
1434 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001435 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001436 from += 1;
1437 n_left_from -= 1;
1438 }
1439
1440 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1441 }
1442
Klement Sekera4c533132018-02-22 11:41:12 +01001443 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001444 return frame->n_vectors;
1445}
1446
Klement Sekera896c8962019-06-24 11:52:49 +00001447VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1448 vlib_node_runtime_t * node,
1449 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001450{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001451 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1452 false /* is_custom_app */,
1453 false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001454}
1455
Klement Sekera896c8962019-06-24 11:52:49 +00001456VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1457 .name = "ip6-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001458 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001459 .format_trace = format_ip6_full_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00001460 .n_errors = IP6_N_ERROR,
1461 .error_counters = ip6_error_counters,
Klement Sekera896c8962019-06-24 11:52:49 +00001462 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001463 .next_nodes =
1464 {
Klement Sekera896c8962019-06-24 11:52:49 +00001465 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1466 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1467 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1468 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001469 },
1470};
Klement Sekera75e7d132017-09-20 08:26:30 +02001471
Klement Sekera01c1fa42021-12-14 18:25:11 +00001472VLIB_NODE_FN (ip6_local_full_reass_node)
1473(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1474{
1475 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1476 false /* is_custom_app */,
1477 true /* is_local */);
1478}
1479
1480VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
1481 .name = "ip6-local-full-reassembly",
1482 .vector_size = sizeof (u32),
1483 .format_trace = format_ip6_full_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00001484 .n_errors = IP6_N_ERROR,
1485 .error_counters = ip6_error_counters,
Klement Sekera01c1fa42021-12-14 18:25:11 +00001486 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1487 .next_nodes =
1488 {
1489 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1490 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1491 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1492 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
1493 },
1494};
1495
Klement Sekera896c8962019-06-24 11:52:49 +00001496VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1497 vlib_node_runtime_t * node,
1498 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001499{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001500 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
1501 false /* is_custom_app */,
1502 false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001503}
1504
Klement Sekera896c8962019-06-24 11:52:49 +00001505VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1506 .name = "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001507 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001508 .format_trace = format_ip6_full_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00001509 .n_errors = IP6_N_ERROR,
1510 .error_counters = ip6_error_counters,
Klement Sekera896c8962019-06-24 11:52:49 +00001511 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001512 .next_nodes =
1513 {
Klement Sekera896c8962019-06-24 11:52:49 +00001514 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1515 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1516 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1517 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001518 },
1519};
Klement Sekera4c533132018-02-22 11:41:12 +01001520
Klement Sekera896c8962019-06-24 11:52:49 +00001521VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001522 .arc_name = "ip6-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001523 .node_name = "ip6-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001524 .runs_before = VNET_FEATURES ("ip6-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001525 "ipsec6-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001526 .runs_after = 0,
1527};
Klement Sekera4c533132018-02-22 11:41:12 +01001528
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001529VLIB_NODE_FN (ip6_full_reass_node_custom)
1530(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1531{
1532 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1533 true /* is_custom_app */,
1534 false /* is_local */);
1535}
1536
1537VLIB_REGISTER_NODE (ip6_full_reass_node_custom) = {
1538 .name = "ip6-full-reassembly-custom",
1539 .vector_size = sizeof (u32),
1540 .format_trace = format_ip6_full_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00001541 .n_errors = IP6_N_ERROR,
1542 .error_counters = ip6_error_counters,
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001543 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1544 .next_nodes =
1545 {
1546 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1547 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1548 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1549 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-custom-hoff",
1550 },
1551};
1552
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001553#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001554static u32
Klement Sekera896c8962019-06-24 11:52:49 +00001555ip6_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001556{
Klement Sekera896c8962019-06-24 11:52:49 +00001557 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001558 u32 nbuckets;
1559 u8 i;
1560
Vijayabhaskar Katamreddy3fda2002022-05-16 22:18:37 +00001561 /* need more mem with more workers */
1562 nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
1563 IP6_FULL_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001564
1565 for (i = 0; i < 31; i++)
1566 if ((1 << i) >= nbuckets)
1567 break;
1568 nbuckets = 1 << i;
1569
1570 return nbuckets;
1571}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001572#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001573
1574typedef enum
1575{
1576 IP6_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001577} ip6_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001578
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001579#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001580typedef struct
1581{
1582 int failure;
1583 clib_bihash_48_8_t *new_hash;
1584} ip6_rehash_cb_ctx;
1585
Neale Rannsf50bac12019-12-06 05:53:17 +00001586static int
Klement Sekera75e7d132017-09-20 08:26:30 +02001587ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1588{
1589 ip6_rehash_cb_ctx *ctx = _ctx;
1590 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1591 {
1592 ctx->failure = 1;
1593 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001594 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001595}
1596
Klement Sekera4c533132018-02-22 11:41:12 +01001597static void
Klement Sekera896c8962019-06-24 11:52:49 +00001598ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1599 u32 max_reassembly_length,
1600 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001601{
Klement Sekera896c8962019-06-24 11:52:49 +00001602 ip6_full_reass_main.timeout_ms = timeout_ms;
1603 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1604 ip6_full_reass_main.max_reass_n = max_reassemblies;
1605 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1606 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001607}
1608
Klement Sekera75e7d132017-09-20 08:26:30 +02001609vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001610ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1611 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001612{
Klement Sekera896c8962019-06-24 11:52:49 +00001613 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1614 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1615 max_reassembly_length, expire_walk_interval_ms);
1616 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1617 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001618 IP6_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001619 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1620 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001621 {
1622 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001623 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001624 ip6_rehash_cb_ctx ctx;
1625 ctx.failure = 0;
1626 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001627 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001628 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001629 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001630 ip6_rehash_cb, &ctx);
1631 if (ctx.failure)
1632 {
1633 clib_bihash_free_48_8 (&new_hash);
1634 return -1;
1635 }
1636 else
1637 {
Klement Sekera896c8962019-06-24 11:52:49 +00001638 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1639 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1640 sizeof (ip6_full_reass_main.hash));
1641 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001642 }
1643 }
1644 return 0;
1645}
1646
1647vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001648ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1649 u32 * max_reassembly_length,
1650 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001651{
Klement Sekera896c8962019-06-24 11:52:49 +00001652 *timeout_ms = ip6_full_reass_main.timeout_ms;
1653 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1654 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1655 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001656 return 0;
1657}
1658
Klement Sekera4c533132018-02-22 11:41:12 +01001659static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001660ip6_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001661{
Klement Sekera896c8962019-06-24 11:52:49 +00001662 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001663 clib_error_t *error = 0;
1664 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001665 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001666
1667 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001668
Juraj Slobodacd806922018-10-10 10:15:54 +02001669 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001670 ip6_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001671 vec_foreach (rt, rm->per_thread_data)
1672 {
1673 clib_spinlock_init (&rt->lock);
1674 pool_alloc (rt->pool, rm->max_reass_n);
1675 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001676
Klement Sekera896c8962019-06-24 11:52:49 +00001677 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001678 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001679 rm->ip6_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001680
Klement Sekera896c8962019-06-24 11:52:49 +00001681 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1682 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1683 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1684 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001685
Klement Sekera896c8962019-06-24 11:52:49 +00001686 nbuckets = ip6_full_reass_get_nbuckets ();
1687 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1688 nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001689
Klement Sekera75e7d132017-09-20 08:26:30 +02001690 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1691 ASSERT (node);
1692 rm->ip6_icmp_error_idx = node->index;
1693
1694 if ((error = vlib_call_init_function (vm, ip_main_init)))
1695 return error;
1696 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
Klement Sekera01c1fa42021-12-14 18:25:11 +00001697 ip6_local_full_reass_node.index);
1698 rm->is_local_reass_enabled = 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001699
Klement Sekera896c8962019-06-24 11:52:49 +00001700 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
Klement Sekera01c1fa42021-12-14 18:25:11 +00001701 rm->fq_local_index =
1702 vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001703 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001704 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001705 rm->fq_custom_index =
1706 vlib_frame_queue_main_init (ip6_full_reass_node_custom.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001707
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001708 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001709 return error;
1710}
1711
Klement Sekera896c8962019-06-24 11:52:49 +00001712VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001713#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001714
1715static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001716ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1717 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekera75e7d132017-09-20 08:26:30 +02001718{
Klement Sekera896c8962019-06-24 11:52:49 +00001719 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001720 uword event_type, *event_data = 0;
1721
1722 while (true)
1723 {
1724 vlib_process_wait_for_event_or_clock (vm,
1725 (f64) rm->expire_walk_interval_ms
1726 / (f64) MSEC_PER_SEC);
1727 event_type = vlib_process_get_events (vm, &event_data);
1728
1729 switch (event_type)
1730 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001731 case ~0:
1732 /* no events => timeout */
1733 /* fallthrough */
Klement Sekera75e7d132017-09-20 08:26:30 +02001734 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001735 /* nothing to do here */
Klement Sekera75e7d132017-09-20 08:26:30 +02001736 break;
1737 default:
1738 clib_warning ("BUG: event type 0x%wx", event_type);
1739 break;
1740 }
1741 f64 now = vlib_time_now (vm);
1742
Klement Sekera896c8962019-06-24 11:52:49 +00001743 ip6_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001744 int *pool_indexes_to_free = NULL;
1745
Klement Sekera4c533132018-02-22 11:41:12 +01001746 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001747 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001748 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001749 u32 *vec_icmp_bi = NULL;
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +00001750 u32 n_left_to_next, *to_next;
1751
Klement Sekera4c533132018-02-22 11:41:12 +01001752 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1753 {
Klement Sekera896c8962019-06-24 11:52:49 +00001754 ip6_full_reass_per_thread_t *rt =
1755 &rm->per_thread_data[thread_index];
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001756 u32 reass_timeout_cnt = 0;
Klement Sekera4c533132018-02-22 11:41:12 +01001757 clib_spinlock_lock (&rt->lock);
1758
1759 vec_reset_length (pool_indexes_to_free);
Vijayabhaskar Katamreddy8b859292022-05-26 15:03:47 +00001760 /* Pace the number of timeouts handled per thread,to avoid barrier
1761 * sync issues in real world scenarios */
1762
1763 u32 beg = rt->last_id;
1764 /* to ensure we walk at least once per sec per context */
1765 u32 end = beg + (IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT *
1766 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS /
1767 MSEC_PER_SEC +
1768 1);
1769 if (end > vec_len (rt->pool))
1770 {
1771 end = vec_len (rt->pool);
1772 rt->last_id = 0;
1773 }
1774 else
1775 {
1776 rt->last_id = end;
1777 }
1778
1779 pool_foreach_stepping_index (index, beg, end, rt->pool)
1780 {
1781 reass = pool_elt_at_index (rt->pool, index);
1782 if (now > reass->last_heard + rm->timeout)
1783 {
1784 vec_add1 (pool_indexes_to_free, index);
1785 }
1786 }
1787
Klement Sekera4c533132018-02-22 11:41:12 +01001788 int *i;
Klement Sekera4c533132018-02-22 11:41:12 +01001789 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001790 {
Klement Sekera896c8962019-06-24 11:52:49 +00001791 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera4c533132018-02-22 11:41:12 +01001792 u32 icmp_bi = ~0;
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001793
1794 reass_timeout_cnt += reass->fragments_n;
Vijayabhaskar Katamreddy449efe92022-05-26 14:11:51 +00001795 ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi,
1796 &n_left_to_next, &to_next);
Klement Sekera42cec0e2021-08-02 16:14:15 +02001797 if (~0 != icmp_bi)
1798 vec_add1 (vec_icmp_bi, icmp_bi);
Dave Baracha638c182019-06-21 18:24:07 -04001799
Klement Sekera42cec0e2021-08-02 16:14:15 +02001800 ip6_full_reass_free (rm, rt, reass);
1801 }
Klement Sekera4c533132018-02-22 11:41:12 +01001802
1803 clib_spinlock_unlock (&rt->lock);
Vijayabhaskar Katamreddyada7e4e2022-05-31 15:58:10 +00001804 if (reass_timeout_cnt)
1805 vlib_node_increment_counter (vm, node->node_index,
1806 IP6_ERROR_REASS_TIMEOUT,
1807 reass_timeout_cnt);
Klement Sekera4c533132018-02-22 11:41:12 +01001808 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001809
Klement Sekera75e7d132017-09-20 08:26:30 +02001810 while (vec_len (vec_icmp_bi) > 0)
1811 {
1812 vlib_frame_t *f =
1813 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1814 u32 *to_next = vlib_frame_vector_args (f);
1815 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001816 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001817 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1818 {
1819 u32 bi = vec_pop (vec_icmp_bi);
1820 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1821 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001822 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001823 to_next[0] = bi;
1824 ++f->n_vectors;
1825 to_next += 1;
1826 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001827 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001828 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001829 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1830 }
1831
1832 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001833 vec_free (vec_icmp_bi);
1834 if (event_data)
1835 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001836 vec_set_len (event_data, 0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001837 }
1838 }
1839
1840 return 0;
1841}
1842
Klement Sekera896c8962019-06-24 11:52:49 +00001843VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
Neale Rannse22a7042022-08-09 03:03:29 +00001844 .function = ip6_full_reass_walk_expired,
1845 .format_trace = format_ip6_full_reass_trace,
1846 .type = VLIB_NODE_TYPE_PROCESS,
1847 .name = "ip6-full-reassembly-expire-walk",
Klement Sekera75e7d132017-09-20 08:26:30 +02001848
Neale Rannse22a7042022-08-09 03:03:29 +00001849 .n_errors = IP6_N_ERROR,
1850 .error_counters = ip6_error_counters,
Klement Sekera75e7d132017-09-20 08:26:30 +02001851};
Klement Sekera75e7d132017-09-20 08:26:30 +02001852
1853static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001854format_ip6_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001855{
Klement Sekera896c8962019-06-24 11:52:49 +00001856 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001857 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1858 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1859 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1860 return s;
1861}
1862
1863static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001864format_ip6_full_reass (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001865{
1866 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001867 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001868
Klement Sekera4c533132018-02-22 11:41:12 +01001869 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001870 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001871 reass->id, format_ip6_full_reass_key, &reass->key,
1872 reass->first_bi, reass->data_len, reass->last_packet_octet,
Klement Sekera75e7d132017-09-20 08:26:30 +02001873 reass->trace_op_counter);
1874 u32 bi = reass->first_bi;
1875 u32 counter = 0;
1876 while (~0 != bi)
1877 {
1878 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1879 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1880 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1881 "fragment[%u, %u]\n",
1882 counter, vnb->ip.reass.range_first,
1883 vnb->ip.reass.range_last, bi,
Klement Sekera896c8962019-06-24 11:52:49 +00001884 ip6_full_reass_buffer_get_data_offset (b),
1885 ip6_full_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001886 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1887 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1888 {
1889 bi = b->next_buffer;
1890 }
1891 else
1892 {
1893 bi = ~0;
1894 }
1895 }
1896 return s;
1897}
1898
1899static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001900show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1901 CLIB_UNUSED (vlib_cli_command_t * lmd))
Klement Sekera75e7d132017-09-20 08:26:30 +02001902{
Klement Sekera896c8962019-06-24 11:52:49 +00001903 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001904
1905 vlib_cli_output (vm, "---------------------");
1906 vlib_cli_output (vm, "IP6 reassembly status");
1907 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001908 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001909 if (unformat (input, "details"))
1910 {
Klement Sekera4c533132018-02-22 11:41:12 +01001911 details = true;
1912 }
1913
1914 u32 sum_reass_n = 0;
1915 u64 sum_buffers_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001916 ip6_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001917 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001918 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001919 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1920 {
Klement Sekera896c8962019-06-24 11:52:49 +00001921 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001922 clib_spinlock_lock (&rt->lock);
1923 if (details)
1924 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001925 pool_foreach (reass, rt->pool) {
Klement Sekera896c8962019-06-24 11:52:49 +00001926 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001927 }
Klement Sekera4c533132018-02-22 11:41:12 +01001928 }
1929 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001930 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001931 }
1932 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001933 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1934 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001935 vlib_cli_output (vm,
1936 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1937 (long unsigned) rm->max_reass_n);
1938 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001939 "Maximum configured amount of fragments "
1940 "per full IP6 reassembly: %lu\n",
1941 (long unsigned) rm->max_reass_len);
1942 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001943 "Maximum configured full IP6 reassembly timeout: %lums\n",
1944 (long unsigned) rm->timeout_ms);
1945 vlib_cli_output (vm,
1946 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1947 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001948 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001949 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001950 return 0;
1951}
1952
Klement Sekera896c8962019-06-24 11:52:49 +00001953VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1954 .path = "show ip6-full-reassembly",
1955 .short_help = "show ip6-full-reassembly [details]",
1956 .function = show_ip6_full_reass,
Klement Sekera75e7d132017-09-20 08:26:30 +02001957};
Klement Sekera75e7d132017-09-20 08:26:30 +02001958
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001959#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001960vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001961ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001962{
Klement Sekera896c8962019-06-24 11:52:49 +00001963 return vnet_feature_enable_disable ("ip6-unicast",
1964 "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001965 sw_if_index, enable_disable, 0, 0);
1966}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001967#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001968
Klement Sekera896c8962019-06-24 11:52:49 +00001969#define foreach_ip6_full_reassembly_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001970_(CONGESTION_DROP, "congestion drop")
1971
1972
1973typedef enum
1974{
Klement Sekera896c8962019-06-24 11:52:49 +00001975#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1976 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001977#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001978 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1979} ip6_full_reassembly_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001980
Klement Sekera896c8962019-06-24 11:52:49 +00001981static char *ip6_full_reassembly_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001982#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001983 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001984#undef _
1985};
1986
1987typedef struct
1988{
1989 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001990} ip6_full_reassembly_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001991
1992static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001993format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001994{
1995 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1996 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001997 ip6_full_reassembly_handoff_trace_t *t =
1998 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001999
2000 s =
Klement Sekera896c8962019-06-24 11:52:49 +00002001 format (s, "ip6-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002002 t->next_worker_index);
2003
2004 return s;
2005}
2006
2007always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00002008ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002009 vlib_frame_t *frame,
2010 ip6_full_reass_node_type_t type,
Klement Sekera01c1fa42021-12-14 18:25:11 +00002011 bool is_local)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002012{
Klement Sekera896c8962019-06-24 11:52:49 +00002013 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002014
2015 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
2016 u32 n_enq, n_left_from, *from;
2017 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
2018 u32 fq_index;
2019
2020 from = vlib_frame_vector_args (frame);
2021 n_left_from = frame->n_vectors;
2022 vlib_get_buffers (vm, from, bufs, n_left_from);
2023
2024 b = bufs;
2025 ti = thread_indices;
2026
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002027 switch (type)
Klement Sekera01c1fa42021-12-14 18:25:11 +00002028 {
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002029 case NORMAL:
Klement Sekera01c1fa42021-12-14 18:25:11 +00002030 if (is_local)
2031 {
2032 fq_index = rm->fq_local_index;
2033 }
2034 else
2035 {
2036 fq_index = rm->fq_index;
2037 }
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002038 break;
2039 case FEATURE:
2040 fq_index = rm->fq_feature_index;
2041 break;
2042 case CUSTOM:
2043 fq_index = rm->fq_custom_index;
2044 break;
2045 default:
2046 clib_warning ("Unexpected `type' (%d)!", type);
2047 ASSERT (0);
Klement Sekera01c1fa42021-12-14 18:25:11 +00002048 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002049 while (n_left_from > 0)
2050 {
Klement Sekerade34c352019-06-25 11:19:22 +00002051 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002052
2053 if (PREDICT_FALSE
2054 ((node->flags & VLIB_NODE_FLAG_TRACE)
2055 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
2056 {
Klement Sekera896c8962019-06-24 11:52:49 +00002057 ip6_full_reassembly_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002058 vlib_add_trace (vm, node, b[0], sizeof (*t));
2059 t->next_worker_index = ti[0];
2060 }
2061
2062 n_left_from -= 1;
2063 ti += 1;
2064 b += 1;
2065 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02002066 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
2067 thread_indices, frame->n_vectors, 1);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002068
2069 if (n_enq < frame->n_vectors)
2070 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00002071 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002072 frame->n_vectors - n_enq);
2073 return frame->n_vectors;
2074}
2075
Klement Sekera896c8962019-06-24 11:52:49 +00002076VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
2077 vlib_node_runtime_t * node,
2078 vlib_frame_t * frame)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002079{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002080 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2081 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002082}
2083
Klement Sekera896c8962019-06-24 11:52:49 +00002084VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
2085 .name = "ip6-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002086 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00002087 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2088 .error_strings = ip6_full_reassembly_handoff_error_strings,
2089 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002090
2091 .n_next_nodes = 1,
2092
2093 .next_nodes = {
2094 [0] = "error-drop",
2095 },
2096};
2097
Klement Sekera01c1fa42021-12-14 18:25:11 +00002098VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
2099(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2100{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002101 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2102 true /* is_feature */);
Klement Sekera01c1fa42021-12-14 18:25:11 +00002103}
2104
2105VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
2106 .name = "ip6-local-full-reassembly-handoff",
2107 .vector_size = sizeof (u32),
2108 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2109 .error_strings = ip6_full_reassembly_handoff_error_strings,
2110 .format_trace = format_ip6_full_reassembly_handoff_trace,
2111
2112 .n_next_nodes = 1,
2113
2114 .next_nodes = {
2115 [0] = "error-drop",
2116 },
2117};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002118
Klement Sekera896c8962019-06-24 11:52:49 +00002119VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002120 vlib_node_runtime_t * node, vlib_frame_t * frame)
2121{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002122 return ip6_full_reassembly_handoff_inline (vm, node, frame, FEATURE,
2123 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002124}
2125
Klement Sekera896c8962019-06-24 11:52:49 +00002126VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
2127 .name = "ip6-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002128 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00002129 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2130 .error_strings = ip6_full_reassembly_handoff_error_strings,
2131 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002132
2133 .n_next_nodes = 1,
2134
2135 .next_nodes = {
2136 [0] = "error-drop",
2137 },
2138};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08002139
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00002140VLIB_NODE_FN (ip6_full_reassembly_custom_handoff_node)
2141(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2142{
2143 return ip6_full_reassembly_handoff_inline (vm, node, frame, CUSTOM,
2144 false /* is_local */);
2145}
2146
2147VLIB_REGISTER_NODE (ip6_full_reassembly_custom_handoff_node) = {
2148 .name = "ip6-full-reass-custom-hoff",
2149 .vector_size = sizeof (u32),
2150 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2151 .error_strings = ip6_full_reassembly_handoff_error_strings,
2152 .format_trace = format_ip6_full_reassembly_handoff_trace,
2153
2154 .n_next_nodes = 1,
2155
2156 .next_nodes = {
2157 [0] = "error-drop",
2158 },
2159};
2160
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002161#ifndef CLIB_MARCH_VARIANT
2162int
2163ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
2164{
2165 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
2166 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
2167 if (is_enable)
2168 {
2169 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2170 {
2171 ++rm->feature_use_refcount_per_intf[sw_if_index];
2172 return vnet_feature_enable_disable ("ip6-unicast",
2173 "ip6-full-reassembly-feature",
2174 sw_if_index, 1, 0, 0);
2175 }
2176 ++rm->feature_use_refcount_per_intf[sw_if_index];
2177 }
2178 else
2179 {
2180 --rm->feature_use_refcount_per_intf[sw_if_index];
2181 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2182 return vnet_feature_enable_disable ("ip6-unicast",
2183 "ip6-full-reassembly-feature",
2184 sw_if_index, 0, 0, 0);
2185 }
2186 return -1;
2187}
Klement Sekera01c1fa42021-12-14 18:25:11 +00002188
2189void
2190ip6_local_full_reass_enable_disable (int enable)
2191{
2192 if (enable)
2193 {
2194 if (!ip6_full_reass_main.is_local_reass_enabled)
2195 {
2196 ip6_full_reass_main.is_local_reass_enabled = 1;
2197 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
2198 ip6_local_full_reass_node.index);
2199 }
2200 }
2201 else
2202 {
2203 if (ip6_full_reass_main.is_local_reass_enabled)
2204 {
2205 ip6_full_reass_main.is_local_reass_enabled = 0;
2206 ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
2207 }
2208 }
2209}
2210
2211int
2212ip6_local_full_reass_enabled ()
2213{
2214 return ip6_full_reass_main.is_local_reass_enabled;
2215}
2216
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002217#endif
2218
Klement Sekera75e7d132017-09-20 08:26:30 +02002219/*
2220 * fd.io coding-style-patch-verification: ON
2221 *
2222 * Local Variables:
2223 * eval: (c-set-style "gnu")
2224 * End:
2225 */