blob: 77d9b3778517aca4e9c15587a058a41955d45a9e [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
Klement Sekera896c8962019-06-24 11:52:49 +000031#define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 100
32#define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
33#define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
34#define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
35#define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
Klement Sekera75e7d132017-09-20 08:26:30 +020036
Klement Sekerad0f70a32018-12-14 17:24:13 +010037typedef enum
38{
Klement Sekera896c8962019-06-24 11:52:49 +000039 IP6_FULL_REASS_RC_OK,
40 IP6_FULL_REASS_RC_INTERNAL_ERROR,
41 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
42 IP6_FULL_REASS_RC_NO_BUF,
43 IP6_FULL_REASS_RC_HANDOFF,
Klement Sekera755042d2021-12-01 10:14:38 +000044 IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
Klement Sekerae63a2d42021-05-20 12:23:00 +020045 IP6_FULL_REASS_RC_OVERLAP,
Klement Sekera896c8962019-06-24 11:52:49 +000046} ip6_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020047
48typedef struct
49{
50 union
51 {
52 struct
53 {
54 ip6_address_t src;
55 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020056 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020057 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020058 u8 unused[7];
59 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020060 };
61 u64 as_u64[6];
62 };
Klement Sekera896c8962019-06-24 11:52:49 +000063} ip6_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020064
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080065typedef union
66{
67 struct
68 {
69 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000070 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080071 };
72 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000073} ip6_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080074
75typedef union
76{
77 struct
78 {
Klement Sekera896c8962019-06-24 11:52:49 +000079 ip6_full_reass_key_t k;
80 ip6_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080081 };
82 clib_bihash_kv_48_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +000083} ip6_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080084
85
Klement Sekera75e7d132017-09-20 08:26:30 +020086always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +000087ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020088{
89 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010090 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020091}
92
93always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +000094ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020095{
96 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010097 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +000098 (vnb->ip.reass.fragment_first +
99 ip6_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +0200100}
101
102typedef struct
103{
104 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000105 ip6_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200106 // time when last packet was received
107 f64 last_heard;
108 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100109 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200110 // buffer index of first buffer in this reassembly context
111 u32 first_bi;
112 // last octet of packet, ~0 until fragment without more_fragments arrives
113 u32 last_packet_octet;
114 // length of data collected so far
115 u32 data_len;
116 // trace operation counter
117 u32 trace_op_counter;
Klement Sekerae8498652019-06-17 12:23:15 +0000118 // next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200119 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000120 // error next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200121 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100122 // minimum fragment length for this reassembly - used to estimate MTU
123 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200124 // number of fragments for this reassembly
125 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000126 // thread owning memory for this context (whose pool contains this ctx)
127 u32 memory_owner_thread_index;
128 // thread which received fragment with offset 0 and which sends out the
129 // completed reassembly
130 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000131} ip6_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200132
133typedef struct
134{
Klement Sekera896c8962019-06-24 11:52:49 +0000135 ip6_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100136 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100137 u32 id_counter;
138 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000139} ip6_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100140
141typedef struct
142{
Klement Sekera75e7d132017-09-20 08:26:30 +0200143 // IPv6 config
144 u32 timeout_ms;
145 f64 timeout;
146 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200147 // maximum number of fragments in one reassembly
148 u32 max_reass_len;
149 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200150 u32 max_reass_n;
151
152 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200153 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100154
155 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000156 ip6_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200157
158 // convenience
159 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200160
Klement Sekera75e7d132017-09-20 08:26:30 +0200161 u32 ip6_icmp_error_idx;
Klement Sekera896c8962019-06-24 11:52:49 +0000162 u32 ip6_full_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200163
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800164 /** Worker handoff */
165 u32 fq_index;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000166 u32 fq_local_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800167 u32 fq_feature_index;
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +0000168 u32 fq_custom_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800169
Klement Sekera7b2e9fb2019-10-01 13:00:22 +0000170 // reference count for enabling/disabling feature - per interface
171 u32 *feature_use_refcount_per_intf;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000172
173 // whether local fragmented packets are reassembled or not
174 int is_local_reass_enabled;
Klement Sekera896c8962019-06-24 11:52:49 +0000175} ip6_full_reass_main_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200176
Klement Sekera896c8962019-06-24 11:52:49 +0000177extern ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700178
179#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000180ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700181#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200182
183typedef enum
184{
Klement Sekera896c8962019-06-24 11:52:49 +0000185 IP6_FULL_REASSEMBLY_NEXT_INPUT,
186 IP6_FULL_REASSEMBLY_NEXT_DROP,
187 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
188 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
189 IP6_FULL_REASSEMBLY_N_NEXT,
190} ip6_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200191
192typedef enum
193{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +0000194 NORMAL,
195 FEATURE,
196 CUSTOM
197} ip6_full_reass_node_type_t;
198
199typedef enum
200{
Klement Sekera75e7d132017-09-20 08:26:30 +0200201 RANGE_NEW,
202 RANGE_OVERLAP,
203 ICMP_ERROR_RT_EXCEEDED,
204 ICMP_ERROR_FL_TOO_BIG,
205 ICMP_ERROR_FL_NOT_MULT_8,
206 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000207 HANDOFF,
Klement Sekera01c1fa42021-12-14 18:25:11 +0000208 PASSTHROUGH,
Klement Sekera896c8962019-06-24 11:52:49 +0000209} ip6_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200210
211typedef struct
212{
213 u16 range_first;
214 u16 range_last;
215 u32 range_bi;
216 i32 data_offset;
217 u32 data_len;
218 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000219} ip6_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200220
221typedef struct
222{
Klement Sekera896c8962019-06-24 11:52:49 +0000223 ip6_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200224 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000225 ip6_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200226 u32 op_id;
227 u32 fragment_first;
228 u32 fragment_last;
229 u32 total_data_len;
Klement Sekera630ab582019-07-19 09:14:19 +0000230 u32 thread_id;
231 u32 thread_id_to;
Klement Sekera8563cb32019-10-10 17:03:57 +0000232 bool is_after_handoff;
233 ip6_header_t ip6_header;
234 ip6_frag_hdr_t ip6_frag_header;
Klement Sekera896c8962019-06-24 11:52:49 +0000235} ip6_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200236
237static void
Klement Sekera896c8962019-06-24 11:52:49 +0000238ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
239 ip6_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200240{
241 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
242 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
243 trace->range_first = vnb->ip.reass.range_first;
244 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000245 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
246 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200247 trace->range_bi = bi;
248}
249
Klement Sekera4c533132018-02-22 11:41:12 +0100250static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000251format_ip6_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200252{
Klement Sekera896c8962019-06-24 11:52:49 +0000253 ip6_full_reass_range_trace_t *trace =
254 va_arg (*args, ip6_full_reass_range_trace_t *);
255 s =
256 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
257 trace->range_last, trace->data_offset, trace->data_len,
258 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200259 return s;
260}
261
Klement Sekera4c533132018-02-22 11:41:12 +0100262static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000263format_ip6_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200264{
265 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
266 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000267 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000268 u32 indent = 0;
269 if (~0 != t->reass_id)
270 {
Klement Sekera8563cb32019-10-10 17:03:57 +0000271 if (t->is_after_handoff)
272 {
273 s =
274 format (s, "%U\n", format_ip6_header, &t->ip6_header,
275 sizeof (t->ip6_header));
276 s =
277 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
278 sizeof (t->ip6_frag_header));
279 indent = 2;
280 }
281 s =
282 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
283 t->reass_id, t->op_id);
Klement Sekera630ab582019-07-19 09:14:19 +0000284 indent = format_get_indent (s);
285 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
286 t->trace_range.first_bi, t->total_data_len,
287 t->fragment_first, t->fragment_last);
288 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200289 switch (t->action)
290 {
291 case RANGE_NEW:
292 s = format (s, "\n%Unew %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000293 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200294 break;
295 case RANGE_OVERLAP:
296 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000297 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200298 break;
299 case ICMP_ERROR_FL_TOO_BIG:
300 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000301 format_white_space, indent,
302 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200303 break;
304 case ICMP_ERROR_FL_NOT_MULT_8:
305 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000306 format_white_space, indent,
307 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200308 break;
309 case ICMP_ERROR_RT_EXCEEDED:
310 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
311 format_white_space, indent);
312 break;
313 case FINALIZE:
314 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
315 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000316 case HANDOFF:
317 s =
318 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
319 t->thread_id_to);
320 break;
Klement Sekera01c1fa42021-12-14 18:25:11 +0000321 case PASSTHROUGH:
322 s = format (s, "passthrough - not a fragment");
323 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200324 }
325 return s;
326}
327
328static void
Klement Sekera896c8962019-06-24 11:52:49 +0000329ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000330 ip6_full_reass_t * reass, u32 bi,
Klement Sekera8563cb32019-10-10 17:03:57 +0000331 ip6_frag_hdr_t * ip6_frag_header,
Klement Sekera896c8962019-06-24 11:52:49 +0000332 ip6_full_reass_trace_operation_e action,
333 u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200334{
335 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
336 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera8563cb32019-10-10 17:03:57 +0000337 bool is_after_handoff = false;
Klement Sekera53be16d2020-12-15 21:47:36 +0100338 if (pool_is_free_index
339 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
340 {
341 // this buffer's trace is gone
342 b->flags &= ~VLIB_BUFFER_IS_TRACED;
343 return;
344 }
Klement Sekera8563cb32019-10-10 17:03:57 +0000345 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
346 {
347 is_after_handoff = true;
348 }
Klement Sekera896c8962019-06-24 11:52:49 +0000349 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera8563cb32019-10-10 17:03:57 +0000350 t->is_after_handoff = is_after_handoff;
351 if (t->is_after_handoff)
352 {
353 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
354 clib_min (sizeof (t->ip6_header), b->current_length));
355 if (ip6_frag_header)
356 {
357 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
358 sizeof (t->ip6_frag_header));
359 }
360 else
361 {
362 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
363 }
364 }
Klement Sekera896c8962019-06-24 11:52:49 +0000365 if (reass)
366 {
367 t->reass_id = reass->id;
368 t->op_id = reass->trace_op_counter;
369 t->trace_range.first_bi = reass->first_bi;
370 t->total_data_len = reass->data_len;
371 ++reass->trace_op_counter;
372 }
373 else
374 {
375 t->reass_id = ~0;
376 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200377 t->action = action;
Klement Sekera630ab582019-07-19 09:14:19 +0000378 t->thread_id = vm->thread_index;
379 t->thread_id_to = thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000380 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200381 t->fragment_first = vnb->ip.reass.fragment_first;
382 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200383#if 0
384 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000385 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200386 printf ("%.*s\n", vec_len (s), s);
387 fflush (stdout);
388 vec_reset_length (s);
389#endif
390}
391
Klement Sekera4c533132018-02-22 11:41:12 +0100392always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000393ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
394 ip6_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000395{
396 pool_put (rt->pool, reass);
397 --rt->reass_n;
398}
399
400always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000401ip6_full_reass_free (ip6_full_reass_main_t * rm,
402 ip6_full_reass_per_thread_t * rt,
403 ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200404{
405 clib_bihash_kv_48_8_t kv;
406 kv.key[0] = reass->key.as_u64[0];
407 kv.key[1] = reass->key.as_u64[1];
408 kv.key[2] = reass->key.as_u64[2];
409 kv.key[3] = reass->key.as_u64[3];
410 kv.key[4] = reass->key.as_u64[4];
411 kv.key[5] = reass->key.as_u64[5];
412 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000413 ip6_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200414}
415
Klement Sekera4c533132018-02-22 11:41:12 +0100416always_inline void
Klement Sekera42cec0e2021-08-02 16:14:15 +0200417ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
Klement Sekerae63a2d42021-05-20 12:23:00 +0200418 ip6_full_reass_t *reass, u32 offending_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200419{
420 u32 range_bi = reass->first_bi;
421 vlib_buffer_t *range_b;
422 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100423 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200424 while (~0 != range_bi)
425 {
426 range_b = vlib_get_buffer (vm, range_bi);
427 range_vnb = vnet_buffer (range_b);
428 u32 bi = range_bi;
429 while (~0 != bi)
430 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100431 vec_add1 (to_free, bi);
Klement Sekerae63a2d42021-05-20 12:23:00 +0200432 if (bi == offending_bi)
433 {
434 offending_bi = ~0;
435 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200436 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
437 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
438 {
439 bi = b->next_buffer;
440 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
441 }
442 else
443 {
444 bi = ~0;
445 }
446 }
447 range_bi = range_vnb->ip.reass.next_range_bi;
448 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200449 if (~0 != offending_bi)
450 {
451 vec_add1 (to_free, offending_bi);
452 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200453 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000454 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200455 {
456 u32 n_left_to_next, *to_next, next_index;
457
458 next_index = reass->error_next_index;
459 u32 bi = ~0;
460
461 while (vec_len (to_free) > 0)
462 {
463 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
464
465 while (vec_len (to_free) > 0 && n_left_to_next > 0)
466 {
467 bi = vec_pop (to_free);
468
469 if (~0 != bi)
470 {
471 to_next[0] = bi;
472 to_next += 1;
473 n_left_to_next -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200474 }
475 }
476 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
477 }
478 }
479 else
480 {
481 vlib_buffer_free (vm, to_free, vec_len (to_free));
482 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100483 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200484}
485
Klement Sekera4c533132018-02-22 11:41:12 +0100486always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000487ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera896c8962019-06-24 11:52:49 +0000488 ip6_full_reass_t * reass, u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200489{
490 if (~0 == reass->first_bi)
491 {
492 return;
493 }
Klement Sekerae8498652019-06-17 12:23:15 +0000494 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200495 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200496 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
497 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200498 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200499 *icmp_bi = reass->first_bi;
500 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
501 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200502 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
503 ICMP_ERROR_RT_EXCEEDED, ~0);
Klement Sekera21aa8f12019-05-20 12:27:33 +0200504 }
505 // fragment with offset zero received - send icmp message back
506 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
507 {
508 // separate first buffer from chain and steer it towards icmp node
509 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
510 reass->first_bi = b->next_buffer;
511 }
512 else
513 {
514 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
515 }
516 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
517 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
518 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200519 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200520 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200521 ip6_full_reass_drop_all (vm, node, reass, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200522}
523
Klement Sekera896c8962019-06-24 11:52:49 +0000524always_inline ip6_full_reass_t *
Klement Sekera7c3275e2021-12-07 09:49:53 +0000525ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
526 ip6_full_reass_main_t *rm,
527 ip6_full_reass_per_thread_t *rt,
528 ip6_full_reass_kv_t *kv, u32 *icmp_bi,
529 u8 *do_handoff, int skip_bihash)
Klement Sekera75e7d132017-09-20 08:26:30 +0200530{
Klement Sekera896c8962019-06-24 11:52:49 +0000531 ip6_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000532 f64 now;
533
534again:
535
536 reass = NULL;
537 now = vlib_time_now (vm);
Klement Sekera75e7d132017-09-20 08:26:30 +0200538
Klement Sekera7c3275e2021-12-07 09:49:53 +0000539 if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200540 {
Gao Feng9165e032020-04-26 09:57:18 +0800541 if (vm->thread_index != kv->v.memory_owner_thread_index)
542 {
543 *do_handoff = 1;
544 return NULL;
545 }
546
Klement Sekera630ab582019-07-19 09:14:19 +0000547 reass =
548 pool_elt_at_index (rm->per_thread_data
549 [kv->v.memory_owner_thread_index].pool,
550 kv->v.reass_index);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800551
Klement Sekera75e7d132017-09-20 08:26:30 +0200552 if (now > reass->last_heard + rm->timeout)
553 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200554 ip6_full_reass_on_timeout (vm, node, reass, icmp_bi);
Klement Sekera896c8962019-06-24 11:52:49 +0000555 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200556 reass = NULL;
557 }
558 }
559
560 if (reass)
561 {
562 reass->last_heard = now;
563 return reass;
564 }
565
Klement Sekera4c533132018-02-22 11:41:12 +0100566 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200567 {
568 reass = NULL;
569 return reass;
570 }
571 else
572 {
Klement Sekera4c533132018-02-22 11:41:12 +0100573 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400574 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800575 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100576 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200577 reass->first_bi = ~0;
578 reass->last_packet_octet = ~0;
579 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000580 reass->next_index = ~0;
581 reass->error_next_index = ~0;
Klement Sekera8d4db8b2022-01-24 22:30:04 +0000582 reass->memory_owner_thread_index = vm->thread_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100583 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200584 }
585
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800586 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000587 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200588 reass->last_heard = now;
589
Klement Sekera7c3275e2021-12-07 09:49:53 +0000590 if (!skip_bihash)
Klement Sekera75e7d132017-09-20 08:26:30 +0200591 {
Klement Sekera7c3275e2021-12-07 09:49:53 +0000592 reass->key.as_u64[0] = kv->kv.key[0];
593 reass->key.as_u64[1] = kv->kv.key[1];
594 reass->key.as_u64[2] = kv->kv.key[2];
595 reass->key.as_u64[3] = kv->kv.key[3];
596 reass->key.as_u64[4] = kv->kv.key[4];
597 reass->key.as_u64[5] = kv->kv.key[5];
598
599 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
600 if (rv)
601 {
602 ip6_full_reass_free (rm, rt, reass);
603 reass = NULL;
604 // if other worker created a context already work with the other copy
605 if (-2 == rv)
606 goto again;
607 }
608 }
609 else
610 {
611 reass->key.as_u64[0] = ~0;
612 reass->key.as_u64[1] = ~0;
613 reass->key.as_u64[2] = ~0;
614 reass->key.as_u64[3] = ~0;
615 reass->key.as_u64[4] = ~0;
616 reass->key.as_u64[5] = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200617 }
618
619 return reass;
620}
621
Klement Sekera896c8962019-06-24 11:52:49 +0000622always_inline ip6_full_reass_rc_t
623ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
624 ip6_full_reass_main_t * rm,
625 ip6_full_reass_per_thread_t * rt,
626 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
627 u32 * error0, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200628{
Klement Sekera75e7d132017-09-20 08:26:30 +0200629 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200630 *error0 = IP6_ERROR_NONE;
631 ip6_frag_hdr_t *frag_hdr;
632 vlib_buffer_t *last_b = NULL;
633 u32 sub_chain_bi = reass->first_bi;
634 u32 total_length = 0;
635 u32 buf_cnt = 0;
636 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100637 u32 *vec_drop_compress = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000638 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200639 do
640 {
641 u32 tmp_bi = sub_chain_bi;
642 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100643 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
644 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
645 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
646 {
Klement Sekera896c8962019-06-24 11:52:49 +0000647 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100648 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100649 }
650
Klement Sekera896c8962019-06-24 11:52:49 +0000651 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200652 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
Klement Sekera896c8962019-06-24 11:52:49 +0000653 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200654 u32 trim_end =
655 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
656 if (tmp_bi == reass->first_bi)
657 {
658 /* first buffer - keep ip6 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000659 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100660 {
Klement Sekera896c8962019-06-24 11:52:49 +0000661 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100662 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100663 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200664 trim_front = 0;
665 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
666 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
667 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100668 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
669 {
Klement Sekera896c8962019-06-24 11:52:49 +0000670 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100671 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100672 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200673 }
674 u32 keep_data =
675 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
676 while (1)
677 {
678 ++buf_cnt;
679 if (trim_front)
680 {
681 if (trim_front > tmp->current_length)
682 {
683 /* drop whole buffer */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100684 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
685 {
Klement Sekera896c8962019-06-24 11:52:49 +0000686 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100687 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100688 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200689 trim_front -= tmp->current_length;
690 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200691 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
692 tmp_bi = tmp->next_buffer;
693 tmp = vlib_get_buffer (vm, tmp_bi);
694 continue;
695 }
696 else
697 {
698 vlib_buffer_advance (tmp, trim_front);
699 trim_front = 0;
700 }
701 }
702 if (keep_data)
703 {
704 if (last_b)
705 {
706 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
707 last_b->next_buffer = tmp_bi;
708 }
709 last_b = tmp;
710 if (keep_data <= tmp->current_length)
711 {
712 tmp->current_length = keep_data;
713 keep_data = 0;
714 }
715 else
716 {
717 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100718 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
719 {
Klement Sekera896c8962019-06-24 11:52:49 +0000720 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100721 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100722 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200723 }
724 total_length += tmp->current_length;
725 }
726 else
727 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100728 if (reass->first_bi == tmp_bi)
729 {
Klement Sekera896c8962019-06-24 11:52:49 +0000730 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100731 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100732 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200733 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200734 ++dropped_cnt;
735 }
736 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
737 {
738 tmp_bi = tmp->next_buffer;
739 tmp = vlib_get_buffer (vm, tmp->next_buffer);
740 }
741 else
742 {
743 break;
744 }
745 }
746 sub_chain_bi =
747 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
748 reass.next_range_bi;
749 }
750 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700751
Klement Sekerad0f70a32018-12-14 17:24:13 +0100752 if (!last_b)
753 {
Klement Sekera896c8962019-06-24 11:52:49 +0000754 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100755 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100756 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200757 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
758 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100759 if (total_length < first_b->current_length)
760 {
Klement Sekera896c8962019-06-24 11:52:49 +0000761 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100762 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100763 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200764 total_length -= first_b->current_length;
765 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
766 first_b->total_length_not_including_first_buffer = total_length;
767 // drop fragment header
768 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
769 ip6_header_t *ip = vlib_buffer_get_current (first_b);
770 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
Ole Troan03092c12021-11-23 15:55:39 +0100771 ip6_ext_hdr_chain_t hdr_chain;
772 ip6_ext_header_t *prev_hdr = 0;
773 int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
774 &hdr_chain);
775 if (res < 0 ||
776 (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
Klement Sekera75e7d132017-09-20 08:26:30 +0200777 {
Ole Troan03092c12021-11-23 15:55:39 +0100778 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
779 goto free_buffers_and_return;
780 }
781 frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
782 if (res > 0)
783 {
784 prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
Klement Sekera75e7d132017-09-20 08:26:30 +0200785 prev_hdr->next_hdr = frag_hdr->next_hdr;
786 }
787 else
788 {
789 ip->protocol = frag_hdr->next_hdr;
790 }
Ole Troan03092c12021-11-23 15:55:39 +0100791 if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
Klement Sekerad0f70a32018-12-14 17:24:13 +0100792 {
Klement Sekera896c8962019-06-24 11:52:49 +0000793 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100794 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100795 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200796 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
797 first_b->current_length - ip6_frag_hdr_offset -
798 sizeof (ip6_frag_hdr_t));
799 first_b->current_length -= sizeof (*frag_hdr);
800 ip->payload_length =
801 clib_host_to_net_u16 (total_length + first_b->current_length -
802 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100803 if (!vlib_buffer_chain_linearize (vm, first_b))
804 {
Klement Sekera896c8962019-06-24 11:52:49 +0000805 rv = IP6_FULL_REASS_RC_NO_BUF;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100806 goto free_buffers_and_return;
807 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700808 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200809 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
810 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200811 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
Klement Sekera896c8962019-06-24 11:52:49 +0000812 FINALIZE, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200813#if 0
814 // following code does a hexdump of packet fragments to stdout ...
815 do
816 {
817 u32 bi = reass->first_bi;
818 u8 *s = NULL;
819 while (~0 != bi)
820 {
821 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
822 s = format (s, "%u: %U\n", bi, format_hexdump,
823 vlib_buffer_get_current (b), b->current_length);
824 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
825 {
826 bi = b->next_buffer;
827 }
828 else
829 {
830 break;
831 }
832 }
833 printf ("%.*s\n", vec_len (s), s);
834 fflush (stdout);
835 vec_free (s);
836 }
837 while (0);
838#endif
839 }
Klement Sekerae8498652019-06-17 12:23:15 +0000840 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100841 {
Klement Sekera896c8962019-06-24 11:52:49 +0000842 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100843 }
844 else
845 {
846 *next0 = reass->next_index;
847 }
848 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera896c8962019-06-24 11:52:49 +0000849 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200850 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100851free_buffers_and_return:
852 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
853 vec_free (vec_drop_compress);
854 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200855}
856
Klement Sekera4c533132018-02-22 11:41:12 +0100857always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000858ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +0000859 ip6_full_reass_t * reass,
860 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200861{
862
863 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
864 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
865 if (~0 != prev_range_bi)
866 {
867 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
868 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
869 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
870 prev_vnb->ip.reass.next_range_bi = new_next_bi;
871 }
872 else
873 {
874 if (~0 != reass->first_bi)
875 {
876 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
877 }
878 reass->first_bi = new_next_bi;
879 }
Klement Sekera896c8962019-06-24 11:52:49 +0000880 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200881}
882
Klement Sekera896c8962019-06-24 11:52:49 +0000883always_inline ip6_full_reass_rc_t
Klement Sekera7c3275e2021-12-07 09:49:53 +0000884ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
885 ip6_full_reass_main_t *rm,
886 ip6_full_reass_per_thread_t *rt,
887 ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
888 u32 *error0, ip6_frag_hdr_t *frag_hdr,
889 bool is_custom_app, u32 *handoff_thread_idx,
890 int skip_bihash)
Klement Sekera75e7d132017-09-20 08:26:30 +0200891{
892 int consumed = 0;
893 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
894 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000895 if (is_custom_app)
896 {
897 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
898 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
899 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200900
Klement Sekera75e7d132017-09-20 08:26:30 +0200901 fvnb->ip.reass.ip6_frag_hdr_offset =
902 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
903 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100904 if (fb->current_length < sizeof (*fip) ||
905 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
906 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
907 {
Klement Sekera896c8962019-06-24 11:52:49 +0000908 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100909 }
910
Klement Sekera75e7d132017-09-20 08:26:30 +0200911 u32 fragment_first = fvnb->ip.reass.fragment_first =
912 ip6_frag_hdr_offset_bytes (frag_hdr);
913 u32 fragment_length =
914 vlib_buffer_length_in_chain (vm, fb) -
915 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
Klement Sekera755042d2021-12-01 10:14:38 +0000916 if (0 == fragment_length)
917 {
918 return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
919 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200920 u32 fragment_last = fvnb->ip.reass.fragment_last =
921 fragment_first + fragment_length - 1;
922 int more_fragments = ip6_frag_hdr_more (frag_hdr);
923 u32 candidate_range_bi = reass->first_bi;
924 u32 prev_range_bi = ~0;
925 fvnb->ip.reass.range_first = fragment_first;
926 fvnb->ip.reass.range_last = fragment_last;
927 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200928 if (!more_fragments)
929 {
930 reass->last_packet_octet = fragment_last;
931 }
932 if (~0 == reass->first_bi)
933 {
934 // starting a new reassembly
Klement Sekera42cec0e2021-08-02 16:14:15 +0200935 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100936 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100937 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200938 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100939 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200940 }
Klement Sekera4c533132018-02-22 11:41:12 +0100941 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200942 clib_min (clib_net_to_host_u16 (fip->payload_length),
943 fvnb->ip.reass.estimated_mtu);
944 while (~0 != candidate_range_bi)
945 {
946 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
947 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
948 if (fragment_first > candidate_vnb->ip.reass.range_last)
949 {
950 // this fragments starts after candidate range
951 prev_range_bi = candidate_range_bi;
952 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
953 if (candidate_vnb->ip.reass.range_last < fragment_last &&
954 ~0 == candidate_range_bi)
955 {
956 // special case - this fragment falls beyond all known ranges
Klement Sekera42cec0e2021-08-02 16:14:15 +0200957 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
958 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200959 consumed = 1;
960 break;
961 }
962 continue;
963 }
964 if (fragment_last < candidate_vnb->ip.reass.range_first)
965 {
966 // this fragment ends before candidate range without any overlap
Klement Sekera42cec0e2021-08-02 16:14:15 +0200967 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
968 *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200969 consumed = 1;
970 }
971 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
972 fragment_last == candidate_vnb->ip.reass.range_last)
973 {
974 // duplicate fragment - ignore
975 }
976 else
977 {
978 // overlapping fragment - not allowed by RFC 8200
Klement Sekera75e7d132017-09-20 08:26:30 +0200979 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
980 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200981 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
Klement Sekera896c8962019-06-24 11:52:49 +0000982 RANGE_OVERLAP, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200983 }
Klement Sekerae63a2d42021-05-20 12:23:00 +0200984 return IP6_FULL_REASS_RC_OVERLAP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200985 }
986 break;
987 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200988 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100989check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200990 if (consumed)
991 {
992 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
993 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200994 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
995 ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200996 }
997 }
Klement Sekera7c3275e2021-12-07 09:49:53 +0000998 else if (skip_bihash)
999 {
1000 // if this reassembly is not in bihash, then the packet must have been
1001 // consumed
1002 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1003 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001004 if (~0 != reass->last_packet_octet &&
1005 reass->data_len == reass->last_packet_octet + 1)
1006 {
Klement Sekera630ab582019-07-19 09:14:19 +00001007 *handoff_thread_idx = reass->sendout_thread_index;
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001008 int handoff =
1009 reass->memory_owner_thread_index != reass->sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001010 ip6_full_reass_rc_t rc =
1011 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1012 is_custom_app);
Benoît Ganne2d0ebd72019-07-19 13:42:12 +02001013 if (IP6_FULL_REASS_RC_OK == rc && handoff)
Klement Sekera630ab582019-07-19 09:14:19 +00001014 {
Klement Sekera896c8962019-06-24 11:52:49 +00001015 return IP6_FULL_REASS_RC_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001016 }
1017 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +02001018 }
1019 else
1020 {
Klement Sekera7c3275e2021-12-07 09:49:53 +00001021 if (skip_bihash)
1022 {
1023 // if this reassembly is not in bihash, it should've been an atomic
1024 // fragment and thus finalized
1025 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1026 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001027 if (consumed)
1028 {
1029 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +02001030 if (reass->fragments_n > rm->max_reass_len)
1031 {
Klement Sekera896c8962019-06-24 11:52:49 +00001032 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
Klement Sekera3a343d42019-05-16 14:35:46 +02001033 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001034 }
1035 else
1036 {
Klement Sekera896c8962019-06-24 11:52:49 +00001037 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +02001038 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1039 }
1040 }
Klement Sekera896c8962019-06-24 11:52:49 +00001041 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +02001042}
1043
Klement Sekera4c533132018-02-22 11:41:12 +01001044always_inline bool
Ole Troan03092c12021-11-23 15:55:39 +01001045ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1046 vlib_buffer_t *b,
1047 ip6_ext_hdr_chain_t *hc)
Klement Sekera4c533132018-02-22 11:41:12 +01001048{
Ole Troan03092c12021-11-23 15:55:39 +01001049 int nh = hc->eh[hc->length - 1].protocol;
1050 /* Checking to see if it's a terminating header */
1051 if (ip6_ext_hdr (nh))
Klement Sekera4c533132018-02-22 11:41:12 +01001052 {
Ole Troan03092c12021-11-23 15:55:39 +01001053 icmp6_error_set_vnet_buffer (
1054 b, ICMP6_parameter_problem,
1055 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
Klement Sekera4c533132018-02-22 11:41:12 +01001056 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
Klement Sekera4c533132018-02-22 11:41:12 +01001057 return false;
1058 }
1059 return true;
1060}
1061
1062always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001063ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +00001064 vlib_buffer_t * b,
1065 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001066{
1067 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1068 ip6_header_t *ip = vlib_buffer_get_current (b);
1069 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1070 u32 fragment_length =
1071 vlib_buffer_length_in_chain (vm, b) -
1072 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1073 if (more_fragments && 0 != fragment_length % 8)
1074 {
1075 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1076 ICMP6_parameter_problem_erroneous_header_field,
1077 (u8 *) & ip->payload_length - (u8 *) ip);
1078 return false;
1079 }
1080 return true;
1081}
1082
1083always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +00001084ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
Klement Sekera896c8962019-06-24 11:52:49 +00001085 vlib_buffer_t * b,
1086 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +01001087{
1088 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1089 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1090 u32 fragment_length =
1091 vlib_buffer_length_in_chain (vm, b) -
1092 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1093 if (fragment_first + fragment_length > 65535)
1094 {
1095 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1096 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1097 ICMP6_parameter_problem_erroneous_header_field,
1098 (u8 *) & frag_hdr->fragment_offset_and_more
1099 - (u8 *) ip0);
1100 return false;
1101 }
1102 return true;
1103}
1104
Klement Sekera75e7d132017-09-20 08:26:30 +02001105always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00001106ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1107 vlib_frame_t *frame, bool is_feature,
1108 bool is_custom_app, bool is_local)
Klement Sekera75e7d132017-09-20 08:26:30 +02001109{
1110 u32 *from = vlib_frame_vector_args (frame);
1111 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001112 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1113 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001114 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001115
1116 n_left_from = frame->n_vectors;
1117 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001118 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001119 {
1120 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1121
Klement Sekera75e7d132017-09-20 08:26:30 +02001122 while (n_left_from > 0 && n_left_to_next > 0)
1123 {
1124 u32 bi0;
1125 vlib_buffer_t *b0;
Klement Sekera896c8962019-06-24 11:52:49 +00001126 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001127 u32 error0 = IP6_ERROR_NONE;
1128 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001129
1130 bi0 = from[0];
1131 b0 = vlib_get_buffer (vm, bi0);
1132
1133 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Ole Troan03092c12021-11-23 15:55:39 +01001134 ip6_frag_hdr_t *frag_hdr;
1135 ip6_ext_hdr_chain_t hdr_chain;
1136 int res = ip6_ext_header_walk (
1137 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1138 if (res < 0 ||
1139 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
Klement Sekera3ecc2212018-03-27 10:34:43 +02001140 {
Ole Troan03092c12021-11-23 15:55:39 +01001141 // this is a mangled packet - no fragmentation
1142 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera01c1fa42021-12-14 18:25:11 +00001143 ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
1144 ~0);
1145 goto skip_reass;
1146 }
1147 if (is_local && !rm->is_local_reass_enabled)
1148 {
1149 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001150 goto skip_reass;
1151 }
Ole Troan03092c12021-11-23 15:55:39 +01001152 frag_hdr =
1153 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001154 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
Ole Troan03092c12021-11-23 15:55:39 +01001155 hdr_chain.eh[res].offset;
Klement Sekera38f7ccb2019-10-28 11:26:28 +00001156
Klement Sekera75e7d132017-09-20 08:26:30 +02001157 if (0 == ip6_frag_hdr_offset (frag_hdr))
1158 {
1159 // first fragment - verify upper-layer is present
Ole Troan03092c12021-11-23 15:55:39 +01001160 if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1161 &hdr_chain))
Klement Sekera75e7d132017-09-20 08:26:30 +02001162 {
Klement Sekera896c8962019-06-24 11:52:49 +00001163 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001164 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001165 }
Klement Sekera4c533132018-02-22 11:41:12 +01001166 }
Klement Sekera42cec0e2021-08-02 16:14:15 +02001167 if (!ip6_full_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
1168 !ip6_full_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
Klement Sekera4c533132018-02-22 11:41:12 +01001169 {
Klement Sekera896c8962019-06-24 11:52:49 +00001170 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001171 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001172 }
Klement Sekera7c3275e2021-12-07 09:49:53 +00001173
1174 int skip_bihash = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001175 ip6_full_reass_kv_t kv;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001176 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001177
Klement Sekera7c3275e2021-12-07 09:49:53 +00001178 if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1179 !ip6_frag_hdr_more (frag_hdr))
1180 {
1181 // this is atomic fragment and needs to be processed separately
1182 skip_bihash = 1;
1183 }
1184 else
1185 {
1186 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1187 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1188 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1189 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1190 kv.k.as_u64[4] =
1191 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1192 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1193 << 32 |
1194 (u64) frag_hdr->identification;
1195 kv.k.as_u64[5] = ip0->protocol;
1196 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001197
Klement Sekera7c3275e2021-12-07 09:49:53 +00001198 ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
1199 vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001200
Klement Sekera630ab582019-07-19 09:14:19 +00001201 if (reass)
1202 {
1203 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1204 if (0 == fragment_first)
1205 {
1206 reass->sendout_thread_index = vm->thread_index;
1207 }
1208 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001209 if (PREDICT_FALSE (do_handoff))
1210 {
Klement Sekera896c8962019-06-24 11:52:49 +00001211 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekerade34c352019-06-25 11:19:22 +00001212 vnet_buffer (b0)->ip.reass.owner_thread_index =
1213 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001214 }
1215 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001216 {
Klement Sekera630ab582019-07-19 09:14:19 +00001217 u32 handoff_thread_idx;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001218 u32 counter = ~0;
Klement Sekera7c3275e2021-12-07 09:49:53 +00001219 switch (ip6_full_reass_update (
1220 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1221 is_custom_app, &handoff_thread_idx, skip_bihash))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001222 {
Klement Sekera896c8962019-06-24 11:52:49 +00001223 case IP6_FULL_REASS_RC_OK:
Klement Sekerad0f70a32018-12-14 17:24:13 +01001224 /* nothing to do here */
1225 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001226 case IP6_FULL_REASS_RC_HANDOFF:
1227 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001228 b0 = vlib_get_buffer (vm, bi0);
Klement Sekerade34c352019-06-25 11:19:22 +00001229 vnet_buffer (b0)->ip.reass.owner_thread_index =
1230 handoff_thread_idx;
Klement Sekera630ab582019-07-19 09:14:19 +00001231 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001232 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001233 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
Klement Sekera3a343d42019-05-16 14:35:46 +02001234 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001235 case IP6_FULL_REASS_RC_NO_BUF:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001236 counter = IP6_ERROR_REASS_NO_BUF;
Klement Sekera3a343d42019-05-16 14:35:46 +02001237 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001238 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001239 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1240 break;
Klement Sekera755042d2021-12-01 10:14:38 +00001241 case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
1242 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
1243 break;
Klement Sekerae63a2d42021-05-20 12:23:00 +02001244 case IP6_FULL_REASS_RC_OVERLAP:
1245 counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
1246 break;
Klement Sekerad4ba0d12021-11-02 15:33:55 +01001247 }
1248 if (~0 != counter)
1249 {
1250 vlib_node_increment_counter (vm, node->node_index, counter,
Klement Sekera3a343d42019-05-16 14:35:46 +02001251 1);
Klement Sekerae63a2d42021-05-20 12:23:00 +02001252 ip6_full_reass_drop_all (vm, node, reass, bi0);
Klement Sekera896c8962019-06-24 11:52:49 +00001253 ip6_full_reass_free (rm, rt, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001254 goto next_packet;
Klement Sekerae63a2d42021-05-20 12:23:00 +02001255 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001256 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001257 }
1258 else
1259 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001260 if (is_feature)
1261 {
Klement Sekera896c8962019-06-24 11:52:49 +00001262 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera21aa8f12019-05-20 12:27:33 +02001263 }
1264 else
1265 {
1266 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1267 next0 = fvnb->ip.reass.error_next_index;
1268 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001269 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1270 }
1271
Klement Sekera75e7d132017-09-20 08:26:30 +02001272 if (~0 != bi0)
1273 {
Klement Sekera4c533132018-02-22 11:41:12 +01001274 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001275 to_next[0] = bi0;
1276 to_next += 1;
1277 n_left_to_next -= 1;
Benoît Gannecf7803d2019-10-23 13:53:49 +02001278
1279 /* bi0 might have been updated by reass_finalize, reload */
1280 b0 = vlib_get_buffer (vm, bi0);
Klement Sekera1766ddc2020-03-30 16:59:38 +02001281 if (IP6_ERROR_NONE != error0)
1282 {
1283 b0->error = node->errors[error0];
1284 }
Benoît Gannecf7803d2019-10-23 13:53:49 +02001285
Klement Sekera896c8962019-06-24 11:52:49 +00001286 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001287 {
1288 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1289 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001290 ip6_full_reass_add_trace (
1291 vm, node, NULL, bi0, frag_hdr, HANDOFF,
1292 vnet_buffer (b0)->ip.reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001293 }
1294 }
1295 else if (is_feature && IP6_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001296 {
Damjan Marion7d98a122018-07-19 20:42:08 +02001297 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001298 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001299 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1300 n_left_to_next, bi0, next0);
1301 }
1302
1303 if (~0 != icmp_bi)
1304 {
Klement Sekera896c8962019-06-24 11:52:49 +00001305 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera75e7d132017-09-20 08:26:30 +02001306 to_next[0] = icmp_bi;
1307 to_next += 1;
1308 n_left_to_next -= 1;
1309 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1310 n_left_to_next, icmp_bi,
1311 next0);
1312 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001313 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001314 from += 1;
1315 n_left_from -= 1;
1316 }
1317
1318 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1319 }
1320
Klement Sekera4c533132018-02-22 11:41:12 +01001321 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001322 return frame->n_vectors;
1323}
1324
Klement Sekera896c8962019-06-24 11:52:49 +00001325static char *ip6_full_reassembly_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001326#define _(sym, string) string,
1327 foreach_ip6_error
1328#undef _
1329};
1330
Klement Sekera896c8962019-06-24 11:52:49 +00001331VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1332 vlib_node_runtime_t * node,
1333 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001334{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001335 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1336 false /* is_custom_app */,
1337 false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001338}
1339
Klement Sekera896c8962019-06-24 11:52:49 +00001340VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1341 .name = "ip6-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001342 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001343 .format_trace = format_ip6_full_reass_trace,
1344 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1345 .error_strings = ip6_full_reassembly_error_strings,
1346 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001347 .next_nodes =
1348 {
Klement Sekera896c8962019-06-24 11:52:49 +00001349 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1350 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1351 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1352 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001353 },
1354};
Klement Sekera75e7d132017-09-20 08:26:30 +02001355
Klement Sekera01c1fa42021-12-14 18:25:11 +00001356VLIB_NODE_FN (ip6_local_full_reass_node)
1357(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1358{
1359 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1360 false /* is_custom_app */,
1361 true /* is_local */);
1362}
1363
1364VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
1365 .name = "ip6-local-full-reassembly",
1366 .vector_size = sizeof (u32),
1367 .format_trace = format_ip6_full_reass_trace,
1368 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1369 .error_strings = ip6_full_reassembly_error_strings,
1370 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1371 .next_nodes =
1372 {
1373 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1374 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1375 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1376 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
1377 },
1378};
1379
Klement Sekera896c8962019-06-24 11:52:49 +00001380VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1381 vlib_node_runtime_t * node,
1382 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001383{
Klement Sekera01c1fa42021-12-14 18:25:11 +00001384 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
1385 false /* is_custom_app */,
1386 false /* is_local */);
Klement Sekera4c533132018-02-22 11:41:12 +01001387}
1388
Klement Sekera896c8962019-06-24 11:52:49 +00001389VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1390 .name = "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001391 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001392 .format_trace = format_ip6_full_reass_trace,
1393 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1394 .error_strings = ip6_full_reassembly_error_strings,
1395 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001396 .next_nodes =
1397 {
Klement Sekera896c8962019-06-24 11:52:49 +00001398 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1399 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1400 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1401 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001402 },
1403};
Klement Sekera4c533132018-02-22 11:41:12 +01001404
Klement Sekera896c8962019-06-24 11:52:49 +00001405VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001406 .arc_name = "ip6-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001407 .node_name = "ip6-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001408 .runs_before = VNET_FEATURES ("ip6-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001409 "ipsec6-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001410 .runs_after = 0,
1411};
Klement Sekera4c533132018-02-22 11:41:12 +01001412
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001413VLIB_NODE_FN (ip6_full_reass_node_custom)
1414(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1415{
1416 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1417 true /* is_custom_app */,
1418 false /* is_local */);
1419}
1420
1421VLIB_REGISTER_NODE (ip6_full_reass_node_custom) = {
1422 .name = "ip6-full-reassembly-custom",
1423 .vector_size = sizeof (u32),
1424 .format_trace = format_ip6_full_reass_trace,
1425 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1426 .error_strings = ip6_full_reassembly_error_strings,
1427 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1428 .next_nodes =
1429 {
1430 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1431 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1432 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1433 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-custom-hoff",
1434 },
1435};
1436
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001437#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001438static u32
Klement Sekera896c8962019-06-24 11:52:49 +00001439ip6_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001440{
Klement Sekera896c8962019-06-24 11:52:49 +00001441 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001442 u32 nbuckets;
1443 u8 i;
1444
Vijayabhaskar Katamreddy3fda2002022-05-16 22:18:37 +00001445 /* need more mem with more workers */
1446 nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
1447 IP6_FULL_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001448
1449 for (i = 0; i < 31; i++)
1450 if ((1 << i) >= nbuckets)
1451 break;
1452 nbuckets = 1 << i;
1453
1454 return nbuckets;
1455}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001456#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001457
1458typedef enum
1459{
1460 IP6_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001461} ip6_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001462
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001463#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001464typedef struct
1465{
1466 int failure;
1467 clib_bihash_48_8_t *new_hash;
1468} ip6_rehash_cb_ctx;
1469
Neale Rannsf50bac12019-12-06 05:53:17 +00001470static int
Klement Sekera75e7d132017-09-20 08:26:30 +02001471ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1472{
1473 ip6_rehash_cb_ctx *ctx = _ctx;
1474 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1475 {
1476 ctx->failure = 1;
1477 }
Neale Rannsf50bac12019-12-06 05:53:17 +00001478 return (BIHASH_WALK_CONTINUE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001479}
1480
Klement Sekera4c533132018-02-22 11:41:12 +01001481static void
Klement Sekera896c8962019-06-24 11:52:49 +00001482ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1483 u32 max_reassembly_length,
1484 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001485{
Klement Sekera896c8962019-06-24 11:52:49 +00001486 ip6_full_reass_main.timeout_ms = timeout_ms;
1487 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1488 ip6_full_reass_main.max_reass_n = max_reassemblies;
1489 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1490 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001491}
1492
Klement Sekera75e7d132017-09-20 08:26:30 +02001493vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001494ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1495 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001496{
Klement Sekera896c8962019-06-24 11:52:49 +00001497 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1498 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1499 max_reassembly_length, expire_walk_interval_ms);
1500 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1501 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001502 IP6_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001503 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1504 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001505 {
1506 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001507 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001508 ip6_rehash_cb_ctx ctx;
1509 ctx.failure = 0;
1510 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001511 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001512 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001513 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001514 ip6_rehash_cb, &ctx);
1515 if (ctx.failure)
1516 {
1517 clib_bihash_free_48_8 (&new_hash);
1518 return -1;
1519 }
1520 else
1521 {
Klement Sekera896c8962019-06-24 11:52:49 +00001522 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1523 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1524 sizeof (ip6_full_reass_main.hash));
1525 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001526 }
1527 }
1528 return 0;
1529}
1530
1531vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001532ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1533 u32 * max_reassembly_length,
1534 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001535{
Klement Sekera896c8962019-06-24 11:52:49 +00001536 *timeout_ms = ip6_full_reass_main.timeout_ms;
1537 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1538 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1539 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001540 return 0;
1541}
1542
Klement Sekera4c533132018-02-22 11:41:12 +01001543static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001544ip6_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001545{
Klement Sekera896c8962019-06-24 11:52:49 +00001546 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001547 clib_error_t *error = 0;
1548 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001549 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001550
1551 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001552
Juraj Slobodacd806922018-10-10 10:15:54 +02001553 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001554 ip6_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001555 vec_foreach (rt, rm->per_thread_data)
1556 {
1557 clib_spinlock_init (&rt->lock);
1558 pool_alloc (rt->pool, rm->max_reass_n);
1559 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001560
Klement Sekera896c8962019-06-24 11:52:49 +00001561 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001562 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001563 rm->ip6_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001564
Klement Sekera896c8962019-06-24 11:52:49 +00001565 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1566 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1567 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1568 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001569
Klement Sekera896c8962019-06-24 11:52:49 +00001570 nbuckets = ip6_full_reass_get_nbuckets ();
1571 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1572 nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001573
Klement Sekera75e7d132017-09-20 08:26:30 +02001574 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1575 ASSERT (node);
1576 rm->ip6_icmp_error_idx = node->index;
1577
1578 if ((error = vlib_call_init_function (vm, ip_main_init)))
1579 return error;
1580 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
Klement Sekera01c1fa42021-12-14 18:25:11 +00001581 ip6_local_full_reass_node.index);
1582 rm->is_local_reass_enabled = 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001583
Klement Sekera896c8962019-06-24 11:52:49 +00001584 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
Klement Sekera01c1fa42021-12-14 18:25:11 +00001585 rm->fq_local_index =
1586 vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001587 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001588 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001589 rm->fq_custom_index =
1590 vlib_frame_queue_main_init (ip6_full_reass_node_custom.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001591
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00001592 rm->feature_use_refcount_per_intf = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001593 return error;
1594}
1595
Klement Sekera896c8962019-06-24 11:52:49 +00001596VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001597#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001598
1599static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001600ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1601 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekera75e7d132017-09-20 08:26:30 +02001602{
Klement Sekera896c8962019-06-24 11:52:49 +00001603 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001604 uword event_type, *event_data = 0;
1605
1606 while (true)
1607 {
1608 vlib_process_wait_for_event_or_clock (vm,
1609 (f64) rm->expire_walk_interval_ms
1610 / (f64) MSEC_PER_SEC);
1611 event_type = vlib_process_get_events (vm, &event_data);
1612
1613 switch (event_type)
1614 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001615 case ~0:
1616 /* no events => timeout */
1617 /* fallthrough */
Klement Sekera75e7d132017-09-20 08:26:30 +02001618 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001619 /* nothing to do here */
Klement Sekera75e7d132017-09-20 08:26:30 +02001620 break;
1621 default:
1622 clib_warning ("BUG: event type 0x%wx", event_type);
1623 break;
1624 }
1625 f64 now = vlib_time_now (vm);
1626
Klement Sekera896c8962019-06-24 11:52:49 +00001627 ip6_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001628 int *pool_indexes_to_free = NULL;
1629
Klement Sekera4c533132018-02-22 11:41:12 +01001630 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001631 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001632 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001633 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001634 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1635 {
Klement Sekera896c8962019-06-24 11:52:49 +00001636 ip6_full_reass_per_thread_t *rt =
1637 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001638 clib_spinlock_lock (&rt->lock);
1639
1640 vec_reset_length (pool_indexes_to_free);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001641 pool_foreach_index (index, rt->pool) {
Klement Sekera4c533132018-02-22 11:41:12 +01001642 reass = pool_elt_at_index (rt->pool, index);
1643 if (now > reass->last_heard + rm->timeout)
1644 {
1645 vec_add1 (pool_indexes_to_free, index);
1646 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001647 }
Klement Sekera4c533132018-02-22 11:41:12 +01001648 int *i;
Klement Sekera4c533132018-02-22 11:41:12 +01001649 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001650 {
Klement Sekera896c8962019-06-24 11:52:49 +00001651 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera4c533132018-02-22 11:41:12 +01001652 u32 icmp_bi = ~0;
Klement Sekera42cec0e2021-08-02 16:14:15 +02001653 ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi);
1654 if (~0 != icmp_bi)
1655 vec_add1 (vec_icmp_bi, icmp_bi);
Dave Baracha638c182019-06-21 18:24:07 -04001656
Klement Sekera42cec0e2021-08-02 16:14:15 +02001657 ip6_full_reass_free (rm, rt, reass);
1658 }
Klement Sekera4c533132018-02-22 11:41:12 +01001659
1660 clib_spinlock_unlock (&rt->lock);
1661 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001662
Klement Sekera75e7d132017-09-20 08:26:30 +02001663 while (vec_len (vec_icmp_bi) > 0)
1664 {
1665 vlib_frame_t *f =
1666 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1667 u32 *to_next = vlib_frame_vector_args (f);
1668 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001669 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001670 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1671 {
1672 u32 bi = vec_pop (vec_icmp_bi);
1673 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1674 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001675 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001676 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1677 to_next[0] = bi;
1678 ++f->n_vectors;
1679 to_next += 1;
1680 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001681 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001682 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001683 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1684 }
1685
1686 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001687 vec_free (vec_icmp_bi);
1688 if (event_data)
1689 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001690 vec_set_len (event_data, 0);
Klement Sekera75e7d132017-09-20 08:26:30 +02001691 }
1692 }
1693
1694 return 0;
1695}
1696
Klement Sekera896c8962019-06-24 11:52:49 +00001697VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1698 .function = ip6_full_reass_walk_expired,
1699 .format_trace = format_ip6_full_reass_trace,
Klement Sekera75e7d132017-09-20 08:26:30 +02001700 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001701 .name = "ip6-full-reassembly-expire-walk",
Klement Sekera75e7d132017-09-20 08:26:30 +02001702
Klement Sekera896c8962019-06-24 11:52:49 +00001703 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1704 .error_strings = ip6_full_reassembly_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001705
1706};
Klement Sekera75e7d132017-09-20 08:26:30 +02001707
1708static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001709format_ip6_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001710{
Klement Sekera896c8962019-06-24 11:52:49 +00001711 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001712 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1713 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1714 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1715 return s;
1716}
1717
1718static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001719format_ip6_full_reass (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001720{
1721 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001722 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001723
Klement Sekera4c533132018-02-22 11:41:12 +01001724 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001725 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001726 reass->id, format_ip6_full_reass_key, &reass->key,
1727 reass->first_bi, reass->data_len, reass->last_packet_octet,
Klement Sekera75e7d132017-09-20 08:26:30 +02001728 reass->trace_op_counter);
1729 u32 bi = reass->first_bi;
1730 u32 counter = 0;
1731 while (~0 != bi)
1732 {
1733 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1734 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1735 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1736 "fragment[%u, %u]\n",
1737 counter, vnb->ip.reass.range_first,
1738 vnb->ip.reass.range_last, bi,
Klement Sekera896c8962019-06-24 11:52:49 +00001739 ip6_full_reass_buffer_get_data_offset (b),
1740 ip6_full_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001741 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1742 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1743 {
1744 bi = b->next_buffer;
1745 }
1746 else
1747 {
1748 bi = ~0;
1749 }
1750 }
1751 return s;
1752}
1753
1754static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001755show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1756 CLIB_UNUSED (vlib_cli_command_t * lmd))
Klement Sekera75e7d132017-09-20 08:26:30 +02001757{
Klement Sekera896c8962019-06-24 11:52:49 +00001758 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001759
1760 vlib_cli_output (vm, "---------------------");
1761 vlib_cli_output (vm, "IP6 reassembly status");
1762 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001763 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001764 if (unformat (input, "details"))
1765 {
Klement Sekera4c533132018-02-22 11:41:12 +01001766 details = true;
1767 }
1768
1769 u32 sum_reass_n = 0;
1770 u64 sum_buffers_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001771 ip6_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001772 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001773 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001774 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1775 {
Klement Sekera896c8962019-06-24 11:52:49 +00001776 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001777 clib_spinlock_lock (&rt->lock);
1778 if (details)
1779 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001780 pool_foreach (reass, rt->pool) {
Klement Sekera896c8962019-06-24 11:52:49 +00001781 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001782 }
Klement Sekera4c533132018-02-22 11:41:12 +01001783 }
1784 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001785 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001786 }
1787 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001788 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1789 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001790 vlib_cli_output (vm,
1791 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1792 (long unsigned) rm->max_reass_n);
1793 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001794 "Maximum configured amount of fragments "
1795 "per full IP6 reassembly: %lu\n",
1796 (long unsigned) rm->max_reass_len);
1797 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001798 "Maximum configured full IP6 reassembly timeout: %lums\n",
1799 (long unsigned) rm->timeout_ms);
1800 vlib_cli_output (vm,
1801 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1802 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001803 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001804 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001805 return 0;
1806}
1807
Klement Sekera896c8962019-06-24 11:52:49 +00001808VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1809 .path = "show ip6-full-reassembly",
1810 .short_help = "show ip6-full-reassembly [details]",
1811 .function = show_ip6_full_reass,
Klement Sekera75e7d132017-09-20 08:26:30 +02001812};
Klement Sekera75e7d132017-09-20 08:26:30 +02001813
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001814#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001815vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001816ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001817{
Klement Sekera896c8962019-06-24 11:52:49 +00001818 return vnet_feature_enable_disable ("ip6-unicast",
1819 "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001820 sw_if_index, enable_disable, 0, 0);
1821}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001822#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001823
Klement Sekera896c8962019-06-24 11:52:49 +00001824#define foreach_ip6_full_reassembly_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001825_(CONGESTION_DROP, "congestion drop")
1826
1827
1828typedef enum
1829{
Klement Sekera896c8962019-06-24 11:52:49 +00001830#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1831 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001832#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001833 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1834} ip6_full_reassembly_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001835
Klement Sekera896c8962019-06-24 11:52:49 +00001836static char *ip6_full_reassembly_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001837#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001838 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001839#undef _
1840};
1841
1842typedef struct
1843{
1844 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001845} ip6_full_reassembly_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001846
1847static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001848format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001849{
1850 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1851 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001852 ip6_full_reassembly_handoff_trace_t *t =
1853 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001854
1855 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001856 format (s, "ip6-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001857 t->next_worker_index);
1858
1859 return s;
1860}
1861
1862always_inline uword
Klement Sekera01c1fa42021-12-14 18:25:11 +00001863ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001864 vlib_frame_t *frame,
1865 ip6_full_reass_node_type_t type,
Klement Sekera01c1fa42021-12-14 18:25:11 +00001866 bool is_local)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001867{
Klement Sekera896c8962019-06-24 11:52:49 +00001868 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001869
1870 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1871 u32 n_enq, n_left_from, *from;
1872 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1873 u32 fq_index;
1874
1875 from = vlib_frame_vector_args (frame);
1876 n_left_from = frame->n_vectors;
1877 vlib_get_buffers (vm, from, bufs, n_left_from);
1878
1879 b = bufs;
1880 ti = thread_indices;
1881
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001882 switch (type)
Klement Sekera01c1fa42021-12-14 18:25:11 +00001883 {
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001884 case NORMAL:
Klement Sekera01c1fa42021-12-14 18:25:11 +00001885 if (is_local)
1886 {
1887 fq_index = rm->fq_local_index;
1888 }
1889 else
1890 {
1891 fq_index = rm->fq_index;
1892 }
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001893 break;
1894 case FEATURE:
1895 fq_index = rm->fq_feature_index;
1896 break;
1897 case CUSTOM:
1898 fq_index = rm->fq_custom_index;
1899 break;
1900 default:
1901 clib_warning ("Unexpected `type' (%d)!", type);
1902 ASSERT (0);
Klement Sekera01c1fa42021-12-14 18:25:11 +00001903 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001904 while (n_left_from > 0)
1905 {
Klement Sekerade34c352019-06-25 11:19:22 +00001906 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001907
1908 if (PREDICT_FALSE
1909 ((node->flags & VLIB_NODE_FLAG_TRACE)
1910 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1911 {
Klement Sekera896c8962019-06-24 11:52:49 +00001912 ip6_full_reassembly_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001913 vlib_add_trace (vm, node, b[0], sizeof (*t));
1914 t->next_worker_index = ti[0];
1915 }
1916
1917 n_left_from -= 1;
1918 ti += 1;
1919 b += 1;
1920 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001921 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1922 thread_indices, frame->n_vectors, 1);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001923
1924 if (n_enq < frame->n_vectors)
1925 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001926 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001927 frame->n_vectors - n_enq);
1928 return frame->n_vectors;
1929}
1930
Klement Sekera896c8962019-06-24 11:52:49 +00001931VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1932 vlib_node_runtime_t * node,
1933 vlib_frame_t * frame)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001934{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001935 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
1936 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001937}
1938
Klement Sekera896c8962019-06-24 11:52:49 +00001939VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1940 .name = "ip6-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001941 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001942 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1943 .error_strings = ip6_full_reassembly_handoff_error_strings,
1944 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001945
1946 .n_next_nodes = 1,
1947
1948 .next_nodes = {
1949 [0] = "error-drop",
1950 },
1951};
1952
Klement Sekera01c1fa42021-12-14 18:25:11 +00001953VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
1954(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1955{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001956 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
1957 true /* is_feature */);
Klement Sekera01c1fa42021-12-14 18:25:11 +00001958}
1959
1960VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
1961 .name = "ip6-local-full-reassembly-handoff",
1962 .vector_size = sizeof (u32),
1963 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1964 .error_strings = ip6_full_reassembly_handoff_error_strings,
1965 .format_trace = format_ip6_full_reassembly_handoff_trace,
1966
1967 .n_next_nodes = 1,
1968
1969 .next_nodes = {
1970 [0] = "error-drop",
1971 },
1972};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001973
Klement Sekera896c8962019-06-24 11:52:49 +00001974VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001975 vlib_node_runtime_t * node, vlib_frame_t * frame)
1976{
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001977 return ip6_full_reassembly_handoff_inline (vm, node, frame, FEATURE,
1978 false /* is_local */);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001979}
1980
Klement Sekera896c8962019-06-24 11:52:49 +00001981VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1982 .name = "ip6-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001983 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001984 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1985 .error_strings = ip6_full_reassembly_handoff_error_strings,
1986 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001987
1988 .n_next_nodes = 1,
1989
1990 .next_nodes = {
1991 [0] = "error-drop",
1992 },
1993};
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001994
Vijayabhaskar Katamreddybc8c2102022-05-19 17:48:13 +00001995VLIB_NODE_FN (ip6_full_reassembly_custom_handoff_node)
1996(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1997{
1998 return ip6_full_reassembly_handoff_inline (vm, node, frame, CUSTOM,
1999 false /* is_local */);
2000}
2001
2002VLIB_REGISTER_NODE (ip6_full_reassembly_custom_handoff_node) = {
2003 .name = "ip6-full-reass-custom-hoff",
2004 .vector_size = sizeof (u32),
2005 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2006 .error_strings = ip6_full_reassembly_handoff_error_strings,
2007 .format_trace = format_ip6_full_reassembly_handoff_trace,
2008
2009 .n_next_nodes = 1,
2010
2011 .next_nodes = {
2012 [0] = "error-drop",
2013 },
2014};
2015
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002016#ifndef CLIB_MARCH_VARIANT
2017int
2018ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
2019{
2020 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
2021 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
2022 if (is_enable)
2023 {
2024 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2025 {
2026 ++rm->feature_use_refcount_per_intf[sw_if_index];
2027 return vnet_feature_enable_disable ("ip6-unicast",
2028 "ip6-full-reassembly-feature",
2029 sw_if_index, 1, 0, 0);
2030 }
2031 ++rm->feature_use_refcount_per_intf[sw_if_index];
2032 }
2033 else
2034 {
2035 --rm->feature_use_refcount_per_intf[sw_if_index];
2036 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2037 return vnet_feature_enable_disable ("ip6-unicast",
2038 "ip6-full-reassembly-feature",
2039 sw_if_index, 0, 0, 0);
2040 }
2041 return -1;
2042}
Klement Sekera01c1fa42021-12-14 18:25:11 +00002043
2044void
2045ip6_local_full_reass_enable_disable (int enable)
2046{
2047 if (enable)
2048 {
2049 if (!ip6_full_reass_main.is_local_reass_enabled)
2050 {
2051 ip6_full_reass_main.is_local_reass_enabled = 1;
2052 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
2053 ip6_local_full_reass_node.index);
2054 }
2055 }
2056 else
2057 {
2058 if (ip6_full_reass_main.is_local_reass_enabled)
2059 {
2060 ip6_full_reass_main.is_local_reass_enabled = 0;
2061 ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
2062 }
2063 }
2064}
2065
2066int
2067ip6_local_full_reass_enabled ()
2068{
2069 return ip6_full_reass_main.is_local_reass_enabled;
2070}
2071
Klement Sekera7b2e9fb2019-10-01 13:00:22 +00002072#endif
2073
Klement Sekera75e7d132017-09-20 08:26:30 +02002074/*
2075 * fd.io coding-style-patch-verification: ON
2076 *
2077 * Local Variables:
2078 * eval: (c-set-style "gnu")
2079 * End:
2080 */