blob: 7b11e78b40f827dcd0d334e5f9e28b8ac5546dc8 [file] [log] [blame]
Klement Sekera75e7d132017-09-20 08:26:30 +02001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file
Klement Sekera896c8962019-06-24 11:52:49 +000018 * @brief IPv6 Full Reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020019 *
Klement Sekera896c8962019-06-24 11:52:49 +000020 * This file contains the source code for IPv6 full reassembly.
Klement Sekera75e7d132017-09-20 08:26:30 +020021 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
26#include <vppinfra/bihash_48_8.h>
Klement Sekera896c8962019-06-24 11:52:49 +000027#include <vnet/ip/reass/ip6_full_reass.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020028
29#define MSEC_PER_SEC 1000
Klement Sekera896c8962019-06-24 11:52:49 +000030#define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 100
31#define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
32#define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
33#define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
34#define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
Klement Sekera75e7d132017-09-20 08:26:30 +020035
Klement Sekerad0f70a32018-12-14 17:24:13 +010036typedef enum
37{
Klement Sekera896c8962019-06-24 11:52:49 +000038 IP6_FULL_REASS_RC_OK,
39 IP6_FULL_REASS_RC_INTERNAL_ERROR,
40 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
41 IP6_FULL_REASS_RC_NO_BUF,
42 IP6_FULL_REASS_RC_HANDOFF,
43} ip6_full_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020044
45typedef struct
46{
47 union
48 {
49 struct
50 {
51 ip6_address_t src;
52 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020053 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020054 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020055 u8 unused[7];
56 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020057 };
58 u64 as_u64[6];
59 };
Klement Sekera896c8962019-06-24 11:52:49 +000060} ip6_full_reass_key_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020061
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080062typedef union
63{
64 struct
65 {
66 u32 reass_index;
Klement Sekera630ab582019-07-19 09:14:19 +000067 u32 memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080068 };
69 u64 as_u64;
Klement Sekera896c8962019-06-24 11:52:49 +000070} ip6_full_reass_val_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080071
72typedef union
73{
74 struct
75 {
Klement Sekera896c8962019-06-24 11:52:49 +000076 ip6_full_reass_key_t k;
77 ip6_full_reass_val_t v;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080078 };
79 clib_bihash_kv_48_8_t kv;
Klement Sekera896c8962019-06-24 11:52:49 +000080} ip6_full_reass_kv_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080081
82
Klement Sekera75e7d132017-09-20 08:26:30 +020083always_inline u32
Klement Sekera896c8962019-06-24 11:52:49 +000084ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020085{
86 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010087 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020088}
89
90always_inline u16
Klement Sekera896c8962019-06-24 11:52:49 +000091ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +020092{
93 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010094 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
Klement Sekera896c8962019-06-24 11:52:49 +000095 (vnb->ip.reass.fragment_first +
96 ip6_full_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020097}
98
99typedef struct
100{
101 // hash table key
Klement Sekera896c8962019-06-24 11:52:49 +0000102 ip6_full_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200103 // time when last packet was received
104 f64 last_heard;
105 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100106 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200107 // buffer index of first buffer in this reassembly context
108 u32 first_bi;
109 // last octet of packet, ~0 until fragment without more_fragments arrives
110 u32 last_packet_octet;
111 // length of data collected so far
112 u32 data_len;
113 // trace operation counter
114 u32 trace_op_counter;
Klement Sekerae8498652019-06-17 12:23:15 +0000115 // next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200116 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000117 // error next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200118 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100119 // minimum fragment length for this reassembly - used to estimate MTU
120 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200121 // number of fragments for this reassembly
122 u32 fragments_n;
Klement Sekera630ab582019-07-19 09:14:19 +0000123 // thread owning memory for this context (whose pool contains this ctx)
124 u32 memory_owner_thread_index;
125 // thread which received fragment with offset 0 and which sends out the
126 // completed reassembly
127 u32 sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000128} ip6_full_reass_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200129
130typedef struct
131{
Klement Sekera896c8962019-06-24 11:52:49 +0000132 ip6_full_reass_t *pool;
Klement Sekera4c533132018-02-22 11:41:12 +0100133 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100134 u32 id_counter;
135 clib_spinlock_t lock;
Klement Sekera896c8962019-06-24 11:52:49 +0000136} ip6_full_reass_per_thread_t;
Klement Sekera4c533132018-02-22 11:41:12 +0100137
138typedef struct
139{
Klement Sekera75e7d132017-09-20 08:26:30 +0200140 // IPv6 config
141 u32 timeout_ms;
142 f64 timeout;
143 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200144 // maximum number of fragments in one reassembly
145 u32 max_reass_len;
146 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200147 u32 max_reass_n;
148
149 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200150 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100151
152 // per-thread data
Klement Sekera896c8962019-06-24 11:52:49 +0000153 ip6_full_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200154
155 // convenience
156 vlib_main_t *vlib_main;
Klement Sekera75e7d132017-09-20 08:26:30 +0200157
158 // node index of ip6-drop node
159 u32 ip6_drop_idx;
160 u32 ip6_icmp_error_idx;
Klement Sekera896c8962019-06-24 11:52:49 +0000161 u32 ip6_full_reass_expire_node_idx;
Klement Sekera75e7d132017-09-20 08:26:30 +0200162
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800163 /** Worker handoff */
164 u32 fq_index;
165 u32 fq_feature_index;
166
Klement Sekera896c8962019-06-24 11:52:49 +0000167} ip6_full_reass_main_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200168
Klement Sekera896c8962019-06-24 11:52:49 +0000169extern ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700170
171#ifndef CLIB_MARCH_VARIANT
Klement Sekera896c8962019-06-24 11:52:49 +0000172ip6_full_reass_main_t ip6_full_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700173#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200174
175typedef enum
176{
Klement Sekera896c8962019-06-24 11:52:49 +0000177 IP6_FULL_REASSEMBLY_NEXT_INPUT,
178 IP6_FULL_REASSEMBLY_NEXT_DROP,
179 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
180 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
181 IP6_FULL_REASSEMBLY_N_NEXT,
182} ip6_full_reass_next_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200183
184typedef enum
185{
186 RANGE_NEW,
187 RANGE_OVERLAP,
188 ICMP_ERROR_RT_EXCEEDED,
189 ICMP_ERROR_FL_TOO_BIG,
190 ICMP_ERROR_FL_NOT_MULT_8,
191 FINALIZE,
Klement Sekera630ab582019-07-19 09:14:19 +0000192 HANDOFF,
Klement Sekera896c8962019-06-24 11:52:49 +0000193} ip6_full_reass_trace_operation_e;
Klement Sekera75e7d132017-09-20 08:26:30 +0200194
195typedef struct
196{
197 u16 range_first;
198 u16 range_last;
199 u32 range_bi;
200 i32 data_offset;
201 u32 data_len;
202 u32 first_bi;
Klement Sekera896c8962019-06-24 11:52:49 +0000203} ip6_full_reass_range_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200204
205typedef struct
206{
Klement Sekera896c8962019-06-24 11:52:49 +0000207 ip6_full_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200208 u32 reass_id;
Klement Sekera896c8962019-06-24 11:52:49 +0000209 ip6_full_reass_range_trace_t trace_range;
Klement Sekera75e7d132017-09-20 08:26:30 +0200210 u32 op_id;
211 u32 fragment_first;
212 u32 fragment_last;
213 u32 total_data_len;
Klement Sekera630ab582019-07-19 09:14:19 +0000214 u32 thread_id;
215 u32 thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000216} ip6_full_reass_trace_t;
Klement Sekera75e7d132017-09-20 08:26:30 +0200217
218static void
Klement Sekera896c8962019-06-24 11:52:49 +0000219ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
220 ip6_full_reass_range_trace_t * trace)
Klement Sekera75e7d132017-09-20 08:26:30 +0200221{
222 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
223 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
224 trace->range_first = vnb->ip.reass.range_first;
225 trace->range_last = vnb->ip.reass.range_last;
Klement Sekera896c8962019-06-24 11:52:49 +0000226 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
227 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200228 trace->range_bi = bi;
229}
230
Klement Sekera4c533132018-02-22 11:41:12 +0100231static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000232format_ip6_full_reass_range_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200233{
Klement Sekera896c8962019-06-24 11:52:49 +0000234 ip6_full_reass_range_trace_t *trace =
235 va_arg (*args, ip6_full_reass_range_trace_t *);
236 s =
237 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
238 trace->range_last, trace->data_offset, trace->data_len,
239 trace->range_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200240 return s;
241}
242
Klement Sekera4c533132018-02-22 11:41:12 +0100243static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +0000244format_ip6_full_reass_trace (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +0200245{
246 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
247 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +0000248 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
Klement Sekera630ab582019-07-19 09:14:19 +0000249 u32 indent = 0;
250 if (~0 != t->reass_id)
251 {
252 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
253 indent = format_get_indent (s);
254 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
255 t->trace_range.first_bi, t->total_data_len,
256 t->fragment_first, t->fragment_last);
257 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200258 switch (t->action)
259 {
260 case RANGE_NEW:
261 s = format (s, "\n%Unew %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000262 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200263 break;
264 case RANGE_OVERLAP:
265 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
Klement Sekera896c8962019-06-24 11:52:49 +0000266 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200267 break;
268 case ICMP_ERROR_FL_TOO_BIG:
269 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000270 format_white_space, indent,
271 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200272 break;
273 case ICMP_ERROR_FL_NOT_MULT_8:
274 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
Klement Sekera896c8962019-06-24 11:52:49 +0000275 format_white_space, indent,
276 format_ip6_full_reass_range_trace, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200277 break;
278 case ICMP_ERROR_RT_EXCEEDED:
279 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
280 format_white_space, indent);
281 break;
282 case FINALIZE:
283 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
284 break;
Klement Sekera630ab582019-07-19 09:14:19 +0000285 case HANDOFF:
286 s =
287 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
288 t->thread_id_to);
289 break;
Klement Sekera75e7d132017-09-20 08:26:30 +0200290 }
291 return s;
292}
293
294static void
Klement Sekera896c8962019-06-24 11:52:49 +0000295ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
296 ip6_full_reass_main_t * rm,
297 ip6_full_reass_t * reass, u32 bi,
298 ip6_full_reass_trace_operation_e action,
299 u32 thread_id_to)
Klement Sekera75e7d132017-09-20 08:26:30 +0200300{
301 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
302 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera896c8962019-06-24 11:52:49 +0000303 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
304 if (reass)
305 {
306 t->reass_id = reass->id;
307 t->op_id = reass->trace_op_counter;
308 t->trace_range.first_bi = reass->first_bi;
309 t->total_data_len = reass->data_len;
310 ++reass->trace_op_counter;
311 }
312 else
313 {
314 t->reass_id = ~0;
315 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200316 t->action = action;
Klement Sekera630ab582019-07-19 09:14:19 +0000317 t->thread_id = vm->thread_index;
318 t->thread_id_to = thread_id_to;
Klement Sekera896c8962019-06-24 11:52:49 +0000319 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
Klement Sekera75e7d132017-09-20 08:26:30 +0200320 t->fragment_first = vnb->ip.reass.fragment_first;
321 t->fragment_last = vnb->ip.reass.fragment_last;
Klement Sekera75e7d132017-09-20 08:26:30 +0200322#if 0
323 static u8 *s = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000324 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
Klement Sekera75e7d132017-09-20 08:26:30 +0200325 printf ("%.*s\n", vec_len (s), s);
326 fflush (stdout);
327 vec_reset_length (s);
328#endif
329}
330
Klement Sekera4c533132018-02-22 11:41:12 +0100331always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000332ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
333 ip6_full_reass_t * reass)
Klement Sekera630ab582019-07-19 09:14:19 +0000334{
335 pool_put (rt->pool, reass);
336 --rt->reass_n;
337}
338
339always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000340ip6_full_reass_free (ip6_full_reass_main_t * rm,
341 ip6_full_reass_per_thread_t * rt,
342 ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200343{
344 clib_bihash_kv_48_8_t kv;
345 kv.key[0] = reass->key.as_u64[0];
346 kv.key[1] = reass->key.as_u64[1];
347 kv.key[2] = reass->key.as_u64[2];
348 kv.key[3] = reass->key.as_u64[3];
349 kv.key[4] = reass->key.as_u64[4];
350 kv.key[5] = reass->key.as_u64[5];
351 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera896c8962019-06-24 11:52:49 +0000352 ip6_full_reass_free_ctx (rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200353}
354
Klement Sekera4c533132018-02-22 11:41:12 +0100355always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000356ip6_full_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
357 ip6_full_reass_main_t * rm, ip6_full_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200358{
359 u32 range_bi = reass->first_bi;
360 vlib_buffer_t *range_b;
361 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100362 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200363 while (~0 != range_bi)
364 {
365 range_b = vlib_get_buffer (vm, range_bi);
366 range_vnb = vnet_buffer (range_b);
367 u32 bi = range_bi;
368 while (~0 != bi)
369 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100370 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200371 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
372 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
373 {
374 bi = b->next_buffer;
375 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
376 }
377 else
378 {
379 bi = ~0;
380 }
381 }
382 range_bi = range_vnb->ip.reass.next_range_bi;
383 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200384 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000385 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200386 {
387 u32 n_left_to_next, *to_next, next_index;
388
389 next_index = reass->error_next_index;
390 u32 bi = ~0;
391
392 while (vec_len (to_free) > 0)
393 {
394 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
395
396 while (vec_len (to_free) > 0 && n_left_to_next > 0)
397 {
398 bi = vec_pop (to_free);
399
400 if (~0 != bi)
401 {
402 to_next[0] = bi;
403 to_next += 1;
404 n_left_to_next -= 1;
Klement Sekera21aa8f12019-05-20 12:27:33 +0200405 }
406 }
407 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
408 }
409 }
410 else
411 {
412 vlib_buffer_free (vm, to_free, vec_len (to_free));
413 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100414 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200415}
416
Klement Sekera4c533132018-02-22 11:41:12 +0100417always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000418ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
419 ip6_full_reass_main_t * rm,
420 ip6_full_reass_t * reass, u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200421{
422 if (~0 == reass->first_bi)
423 {
424 return;
425 }
Klement Sekerae8498652019-06-17 12:23:15 +0000426 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200427 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200428 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
429 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200430 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200431 *icmp_bi = reass->first_bi;
432 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
433 {
Klement Sekera896c8962019-06-24 11:52:49 +0000434 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
435 ICMP_ERROR_RT_EXCEEDED, ~0);
Klement Sekera21aa8f12019-05-20 12:27:33 +0200436 }
437 // fragment with offset zero received - send icmp message back
438 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
439 {
440 // separate first buffer from chain and steer it towards icmp node
441 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
442 reass->first_bi = b->next_buffer;
443 }
444 else
445 {
446 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
447 }
448 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
449 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
450 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200451 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200452 }
Klement Sekera896c8962019-06-24 11:52:49 +0000453 ip6_full_reass_drop_all (vm, node, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200454}
455
Klement Sekera896c8962019-06-24 11:52:49 +0000456always_inline ip6_full_reass_t *
457ip6_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
458 ip6_full_reass_main_t * rm,
459 ip6_full_reass_per_thread_t * rt,
460 ip6_full_reass_kv_t * kv, u32 * icmp_bi,
461 u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200462{
Klement Sekera896c8962019-06-24 11:52:49 +0000463 ip6_full_reass_t *reass;
Klement Sekera630ab582019-07-19 09:14:19 +0000464 f64 now;
465
466again:
467
468 reass = NULL;
469 now = vlib_time_now (vm);
Klement Sekera75e7d132017-09-20 08:26:30 +0200470
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800471 if (!clib_bihash_search_48_8
472 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200473 {
Klement Sekera630ab582019-07-19 09:14:19 +0000474 reass =
475 pool_elt_at_index (rm->per_thread_data
476 [kv->v.memory_owner_thread_index].pool,
477 kv->v.reass_index);
478 if (vm->thread_index != kv->v.memory_owner_thread_index)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800479 {
480 *do_handoff = 1;
Klement Sekera630ab582019-07-19 09:14:19 +0000481 return reass;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800482 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800483
Klement Sekera75e7d132017-09-20 08:26:30 +0200484 if (now > reass->last_heard + rm->timeout)
485 {
Klement Sekera896c8962019-06-24 11:52:49 +0000486 ip6_full_reass_on_timeout (vm, node, rm, reass, icmp_bi);
487 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200488 reass = NULL;
489 }
490 }
491
492 if (reass)
493 {
494 reass->last_heard = now;
495 return reass;
496 }
497
Klement Sekera4c533132018-02-22 11:41:12 +0100498 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200499 {
500 reass = NULL;
501 return reass;
502 }
503 else
504 {
Klement Sekera4c533132018-02-22 11:41:12 +0100505 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400506 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800507 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100508 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200509 reass->first_bi = ~0;
510 reass->last_packet_octet = ~0;
511 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000512 reass->next_index = ~0;
513 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100514 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200515 }
516
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800517 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
518 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
519 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
520 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
521 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
522 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
523 kv->v.reass_index = (reass - rt->pool);
Klement Sekera630ab582019-07-19 09:14:19 +0000524 kv->v.memory_owner_thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200525 reass->last_heard = now;
526
Klement Sekera630ab582019-07-19 09:14:19 +0000527 int rv =
528 clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 2);
529 if (rv)
Klement Sekera75e7d132017-09-20 08:26:30 +0200530 {
Klement Sekera896c8962019-06-24 11:52:49 +0000531 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200532 reass = NULL;
Klement Sekera630ab582019-07-19 09:14:19 +0000533 // if other worker created a context already work with the other copy
534 if (-2 == rv)
535 goto again;
Klement Sekera75e7d132017-09-20 08:26:30 +0200536 }
537
538 return reass;
539}
540
Klement Sekera896c8962019-06-24 11:52:49 +0000541always_inline ip6_full_reass_rc_t
542ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
543 ip6_full_reass_main_t * rm,
544 ip6_full_reass_per_thread_t * rt,
545 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
546 u32 * error0, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200547{
Klement Sekera75e7d132017-09-20 08:26:30 +0200548 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200549 *error0 = IP6_ERROR_NONE;
550 ip6_frag_hdr_t *frag_hdr;
551 vlib_buffer_t *last_b = NULL;
552 u32 sub_chain_bi = reass->first_bi;
553 u32 total_length = 0;
554 u32 buf_cnt = 0;
555 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100556 u32 *vec_drop_compress = NULL;
Klement Sekera896c8962019-06-24 11:52:49 +0000557 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200558 do
559 {
560 u32 tmp_bi = sub_chain_bi;
561 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100562 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
563 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
564 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
565 {
Klement Sekera896c8962019-06-24 11:52:49 +0000566 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100567 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100568 }
569
Klement Sekera896c8962019-06-24 11:52:49 +0000570 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200571 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
Klement Sekera896c8962019-06-24 11:52:49 +0000572 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
Klement Sekera75e7d132017-09-20 08:26:30 +0200573 u32 trim_end =
574 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
575 if (tmp_bi == reass->first_bi)
576 {
577 /* first buffer - keep ip6 header */
Klement Sekera896c8962019-06-24 11:52:49 +0000578 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
Klement Sekerad0f70a32018-12-14 17:24:13 +0100579 {
Klement Sekera896c8962019-06-24 11:52:49 +0000580 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100581 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100582 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200583 trim_front = 0;
584 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
585 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
586 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100587 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
588 {
Klement Sekera896c8962019-06-24 11:52:49 +0000589 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100590 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100591 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200592 }
593 u32 keep_data =
594 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
595 while (1)
596 {
597 ++buf_cnt;
598 if (trim_front)
599 {
600 if (trim_front > tmp->current_length)
601 {
602 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100603 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200604 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100605 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
606 {
Klement Sekera896c8962019-06-24 11:52:49 +0000607 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100608 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100609 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200610 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
611 tmp_bi = tmp->next_buffer;
612 tmp = vlib_get_buffer (vm, tmp_bi);
613 continue;
614 }
615 else
616 {
617 vlib_buffer_advance (tmp, trim_front);
618 trim_front = 0;
619 }
620 }
621 if (keep_data)
622 {
623 if (last_b)
624 {
625 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
626 last_b->next_buffer = tmp_bi;
627 }
628 last_b = tmp;
629 if (keep_data <= tmp->current_length)
630 {
631 tmp->current_length = keep_data;
632 keep_data = 0;
633 }
634 else
635 {
636 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100637 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
638 {
Klement Sekera896c8962019-06-24 11:52:49 +0000639 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100640 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100641 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200642 }
643 total_length += tmp->current_length;
644 }
645 else
646 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100647 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100648 if (reass->first_bi == tmp_bi)
649 {
Klement Sekera896c8962019-06-24 11:52:49 +0000650 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100651 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100652 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200653 ++dropped_cnt;
654 }
655 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
656 {
657 tmp_bi = tmp->next_buffer;
658 tmp = vlib_get_buffer (vm, tmp->next_buffer);
659 }
660 else
661 {
662 break;
663 }
664 }
665 sub_chain_bi =
666 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
667 reass.next_range_bi;
668 }
669 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700670
Klement Sekerad0f70a32018-12-14 17:24:13 +0100671 if (!last_b)
672 {
Klement Sekera896c8962019-06-24 11:52:49 +0000673 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100674 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100675 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200676 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
677 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100678 if (total_length < first_b->current_length)
679 {
Klement Sekera896c8962019-06-24 11:52:49 +0000680 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100681 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100682 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200683 total_length -= first_b->current_length;
684 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
685 first_b->total_length_not_including_first_buffer = total_length;
686 // drop fragment header
687 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
688 ip6_header_t *ip = vlib_buffer_get_current (first_b);
689 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
690 ip6_ext_header_t *prev_hdr;
691 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
692 IP_PROTOCOL_IPV6_FRAGMENTATION);
693 if (prev_hdr)
694 {
695 prev_hdr->next_hdr = frag_hdr->next_hdr;
696 }
697 else
698 {
699 ip->protocol = frag_hdr->next_hdr;
700 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100701 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
702 {
Klement Sekera896c8962019-06-24 11:52:49 +0000703 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100704 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100705 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200706 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
707 first_b->current_length - ip6_frag_hdr_offset -
708 sizeof (ip6_frag_hdr_t));
709 first_b->current_length -= sizeof (*frag_hdr);
710 ip->payload_length =
711 clib_host_to_net_u16 (total_length + first_b->current_length -
712 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100713 if (!vlib_buffer_chain_linearize (vm, first_b))
714 {
Klement Sekera896c8962019-06-24 11:52:49 +0000715 rv = IP6_FULL_REASS_RC_NO_BUF;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100716 goto free_buffers_and_return;
717 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700718 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200719 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
720 {
Klement Sekera896c8962019-06-24 11:52:49 +0000721 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
722 FINALIZE, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200723#if 0
724 // following code does a hexdump of packet fragments to stdout ...
725 do
726 {
727 u32 bi = reass->first_bi;
728 u8 *s = NULL;
729 while (~0 != bi)
730 {
731 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
732 s = format (s, "%u: %U\n", bi, format_hexdump,
733 vlib_buffer_get_current (b), b->current_length);
734 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
735 {
736 bi = b->next_buffer;
737 }
738 else
739 {
740 break;
741 }
742 }
743 printf ("%.*s\n", vec_len (s), s);
744 fflush (stdout);
745 vec_free (s);
746 }
747 while (0);
748#endif
749 }
Klement Sekerae8498652019-06-17 12:23:15 +0000750 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100751 {
Klement Sekera896c8962019-06-24 11:52:49 +0000752 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +0100753 }
754 else
755 {
756 *next0 = reass->next_index;
757 }
758 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
Klement Sekera896c8962019-06-24 11:52:49 +0000759 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200760 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100761free_buffers_and_return:
762 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
763 vec_free (vec_drop_compress);
764 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200765}
766
Klement Sekera4c533132018-02-22 11:41:12 +0100767always_inline void
Klement Sekera896c8962019-06-24 11:52:49 +0000768ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
769 ip6_full_reass_main_t * rm,
770 ip6_full_reass_per_thread_t * rt,
771 ip6_full_reass_t * reass,
772 u32 prev_range_bi, u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200773{
774
775 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
776 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
777 if (~0 != prev_range_bi)
778 {
779 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
780 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
781 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
782 prev_vnb->ip.reass.next_range_bi = new_next_bi;
783 }
784 else
785 {
786 if (~0 != reass->first_bi)
787 {
788 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
789 }
790 reass->first_bi = new_next_bi;
791 }
Klement Sekera896c8962019-06-24 11:52:49 +0000792 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200793}
794
Klement Sekera896c8962019-06-24 11:52:49 +0000795always_inline ip6_full_reass_rc_t
796ip6_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
797 ip6_full_reass_main_t * rm,
798 ip6_full_reass_per_thread_t * rt,
799 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
800 u32 * error0, ip6_frag_hdr_t * frag_hdr,
801 bool is_custom_app, u32 * handoff_thread_idx)
Klement Sekera75e7d132017-09-20 08:26:30 +0200802{
803 int consumed = 0;
804 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
805 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000806 if (is_custom_app)
807 {
808 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
809 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
810 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200811
Klement Sekera75e7d132017-09-20 08:26:30 +0200812 fvnb->ip.reass.ip6_frag_hdr_offset =
813 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
814 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100815 if (fb->current_length < sizeof (*fip) ||
816 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
817 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
818 {
Klement Sekera896c8962019-06-24 11:52:49 +0000819 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100820 }
821
Klement Sekera75e7d132017-09-20 08:26:30 +0200822 u32 fragment_first = fvnb->ip.reass.fragment_first =
823 ip6_frag_hdr_offset_bytes (frag_hdr);
824 u32 fragment_length =
825 vlib_buffer_length_in_chain (vm, fb) -
826 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
827 u32 fragment_last = fvnb->ip.reass.fragment_last =
828 fragment_first + fragment_length - 1;
829 int more_fragments = ip6_frag_hdr_more (frag_hdr);
830 u32 candidate_range_bi = reass->first_bi;
831 u32 prev_range_bi = ~0;
832 fvnb->ip.reass.range_first = fragment_first;
833 fvnb->ip.reass.range_last = fragment_last;
834 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200835 if (!more_fragments)
836 {
837 reass->last_packet_octet = fragment_last;
838 }
839 if (~0 == reass->first_bi)
840 {
841 // starting a new reassembly
Klement Sekera896c8962019-06-24 11:52:49 +0000842 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
843 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100844 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100845 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200846 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100847 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200848 }
Klement Sekera4c533132018-02-22 11:41:12 +0100849 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200850 clib_min (clib_net_to_host_u16 (fip->payload_length),
851 fvnb->ip.reass.estimated_mtu);
852 while (~0 != candidate_range_bi)
853 {
854 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
855 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
856 if (fragment_first > candidate_vnb->ip.reass.range_last)
857 {
858 // this fragments starts after candidate range
859 prev_range_bi = candidate_range_bi;
860 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
861 if (candidate_vnb->ip.reass.range_last < fragment_last &&
862 ~0 == candidate_range_bi)
863 {
864 // special case - this fragment falls beyond all known ranges
Klement Sekera896c8962019-06-24 11:52:49 +0000865 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
866 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200867 consumed = 1;
868 break;
869 }
870 continue;
871 }
872 if (fragment_last < candidate_vnb->ip.reass.range_first)
873 {
874 // this fragment ends before candidate range without any overlap
Klement Sekera896c8962019-06-24 11:52:49 +0000875 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
876 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200877 consumed = 1;
878 }
879 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
880 fragment_last == candidate_vnb->ip.reass.range_last)
881 {
882 // duplicate fragment - ignore
883 }
884 else
885 {
886 // overlapping fragment - not allowed by RFC 8200
Klement Sekera896c8962019-06-24 11:52:49 +0000887 ip6_full_reass_drop_all (vm, node, rm, reass);
888 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200889 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
890 {
Klement Sekera896c8962019-06-24 11:52:49 +0000891 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0,
892 RANGE_OVERLAP, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200893 }
Klement Sekera896c8962019-06-24 11:52:49 +0000894 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200895 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekera896c8962019-06-24 11:52:49 +0000896 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200897 }
898 break;
899 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200900 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100901check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200902 if (consumed)
903 {
904 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
905 {
Klement Sekera896c8962019-06-24 11:52:49 +0000906 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, ~0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200907 }
908 }
909 if (~0 != reass->last_packet_octet &&
910 reass->data_len == reass->last_packet_octet + 1)
911 {
Klement Sekera630ab582019-07-19 09:14:19 +0000912 *handoff_thread_idx = reass->sendout_thread_index;
Klement Sekera896c8962019-06-24 11:52:49 +0000913 ip6_full_reass_rc_t rc =
914 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
915 is_custom_app);
916 if (IP6_FULL_REASS_RC_OK == rc
Klement Sekera630ab582019-07-19 09:14:19 +0000917 && reass->memory_owner_thread_index != reass->sendout_thread_index)
918 {
Klement Sekera896c8962019-06-24 11:52:49 +0000919 return IP6_FULL_REASS_RC_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +0000920 }
921 return rc;
Klement Sekera75e7d132017-09-20 08:26:30 +0200922 }
923 else
924 {
925 if (consumed)
926 {
927 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200928 if (reass->fragments_n > rm->max_reass_len)
929 {
Klement Sekera896c8962019-06-24 11:52:49 +0000930 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
Klement Sekera3a343d42019-05-16 14:35:46 +0200931 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200932 }
933 else
934 {
Klement Sekera896c8962019-06-24 11:52:49 +0000935 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200936 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
937 }
938 }
Klement Sekera896c8962019-06-24 11:52:49 +0000939 return IP6_FULL_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200940}
941
Klement Sekera4c533132018-02-22 11:41:12 +0100942always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +0000943ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
944 vlib_buffer_t * b,
945 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +0100946{
947 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
948 while (ip6_ext_hdr (tmp->next_hdr))
949 {
950 tmp = ip6_ext_next_header (tmp);
951 }
952 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
953 {
954 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
955 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
956 0);
957 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
958
959 return false;
960 }
961 return true;
962}
963
964always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +0000965ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
966 vlib_node_runtime_t * node,
967 vlib_buffer_t * b,
968 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +0100969{
970 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
971 ip6_header_t *ip = vlib_buffer_get_current (b);
972 int more_fragments = ip6_frag_hdr_more (frag_hdr);
973 u32 fragment_length =
974 vlib_buffer_length_in_chain (vm, b) -
975 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
976 if (more_fragments && 0 != fragment_length % 8)
977 {
978 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
979 ICMP6_parameter_problem_erroneous_header_field,
980 (u8 *) & ip->payload_length - (u8 *) ip);
981 return false;
982 }
983 return true;
984}
985
986always_inline bool
Klement Sekera896c8962019-06-24 11:52:49 +0000987ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
988 vlib_node_runtime_t * node,
989 vlib_buffer_t * b,
990 ip6_frag_hdr_t * frag_hdr)
Klement Sekera4c533132018-02-22 11:41:12 +0100991{
992 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
993 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
994 u32 fragment_length =
995 vlib_buffer_length_in_chain (vm, b) -
996 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
997 if (fragment_first + fragment_length > 65535)
998 {
999 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1000 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1001 ICMP6_parameter_problem_erroneous_header_field,
1002 (u8 *) & frag_hdr->fragment_offset_and_more
1003 - (u8 *) ip0);
1004 return false;
1005 }
1006 return true;
1007}
1008
Klement Sekera75e7d132017-09-20 08:26:30 +02001009always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001010ip6_full_reassembly_inline (vlib_main_t * vm,
1011 vlib_node_runtime_t * node,
1012 vlib_frame_t * frame, bool is_feature,
1013 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +02001014{
1015 u32 *from = vlib_frame_vector_args (frame);
1016 u32 n_left_from, n_left_to_next, *to_next, next_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001017 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1018 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001019 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001020
1021 n_left_from = frame->n_vectors;
1022 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001023 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001024 {
1025 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1026
Klement Sekera75e7d132017-09-20 08:26:30 +02001027 while (n_left_from > 0 && n_left_to_next > 0)
1028 {
1029 u32 bi0;
1030 vlib_buffer_t *b0;
Klement Sekera896c8962019-06-24 11:52:49 +00001031 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +01001032 u32 error0 = IP6_ERROR_NONE;
1033 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001034
1035 bi0 = from[0];
1036 b0 = vlib_get_buffer (vm, bi0);
1037
1038 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001039 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +02001040 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +02001041 if (ip6_ext_hdr (ip0->protocol))
1042 {
1043 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
1044 IP_PROTOCOL_IPV6_FRAGMENTATION);
1045 }
Klement Sekera4c533132018-02-22 11:41:12 +01001046 if (!frag_hdr)
1047 {
1048 // this is a regular packet - no fragmentation
Klement Sekera896c8962019-06-24 11:52:49 +00001049 next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
Klement Sekera4c533132018-02-22 11:41:12 +01001050 goto skip_reass;
1051 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001052 if (0 == ip6_frag_hdr_offset (frag_hdr))
1053 {
1054 // first fragment - verify upper-layer is present
Klement Sekera896c8962019-06-24 11:52:49 +00001055 if (!ip6_full_reass_verify_upper_layer_present
1056 (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +02001057 {
Klement Sekera896c8962019-06-24 11:52:49 +00001058 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001059 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001060 }
Klement Sekera4c533132018-02-22 11:41:12 +01001061 }
Klement Sekera896c8962019-06-24 11:52:49 +00001062 if (!ip6_full_reass_verify_fragment_multiple_8
1063 (vm, node, b0, frag_hdr)
1064 || !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
1065 frag_hdr))
Klement Sekera4c533132018-02-22 11:41:12 +01001066 {
Klement Sekera896c8962019-06-24 11:52:49 +00001067 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera4c533132018-02-22 11:41:12 +01001068 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001069 }
1070 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1071 (u8 *) frag_hdr - (u8 *) ip0;
1072
Klement Sekera896c8962019-06-24 11:52:49 +00001073 ip6_full_reass_kv_t kv;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001074 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001075
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001076 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1077 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1078 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1079 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1080 kv.k.as_u64[4] =
1081 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1082 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1083 (u64) frag_hdr->identification;
1084 kv.k.as_u64[5] = ip0->protocol;
1085
Klement Sekera896c8962019-06-24 11:52:49 +00001086 ip6_full_reass_t *reass =
1087 ip6_full_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
1088 &do_handoff);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001089
Klement Sekera630ab582019-07-19 09:14:19 +00001090 if (reass)
1091 {
1092 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1093 if (0 == fragment_first)
1094 {
1095 reass->sendout_thread_index = vm->thread_index;
1096 }
1097 }
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001098 if (PREDICT_FALSE (do_handoff))
1099 {
Klement Sekera896c8962019-06-24 11:52:49 +00001100 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001101 if (is_feature)
1102 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
Klement Sekera630ab582019-07-19 09:14:19 +00001103 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001104 else
1105 vnet_buffer (b0)->ip.reass.owner_thread_index =
Klement Sekera630ab582019-07-19 09:14:19 +00001106 kv.v.memory_owner_thread_index;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001107 }
1108 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001109 {
Klement Sekera630ab582019-07-19 09:14:19 +00001110 u32 handoff_thread_idx;
Klement Sekera896c8962019-06-24 11:52:49 +00001111 switch (ip6_full_reass_update
1112 (vm, node, rm, rt, reass, &bi0, &next0, &error0,
1113 frag_hdr, is_custom_app, &handoff_thread_idx))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001114 {
Klement Sekera896c8962019-06-24 11:52:49 +00001115 case IP6_FULL_REASS_RC_OK:
Klement Sekerad0f70a32018-12-14 17:24:13 +01001116 /* nothing to do here */
1117 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001118 case IP6_FULL_REASS_RC_HANDOFF:
1119 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
Klement Sekera630ab582019-07-19 09:14:19 +00001120 b0 = vlib_get_buffer (vm, bi0);
1121 if (is_feature)
1122 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
1123 handoff_thread_idx;
1124 else
1125 vnet_buffer (b0)->ip.reass.owner_thread_index =
1126 handoff_thread_idx;
1127 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001128 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekera3a343d42019-05-16 14:35:46 +02001129 vlib_node_increment_counter (vm, node->node_index,
1130 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1131 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001132 ip6_full_reass_drop_all (vm, node, rm, reass);
1133 ip6_full_reass_free (rm, rt, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001134 goto next_packet;
1135 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001136 case IP6_FULL_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001137 vlib_node_increment_counter (vm, node->node_index,
1138 IP6_ERROR_REASS_NO_BUF, 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001139 ip6_full_reass_drop_all (vm, node, rm, reass);
1140 ip6_full_reass_free (rm, rt, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001141 goto next_packet;
1142 break;
Klement Sekera896c8962019-06-24 11:52:49 +00001143 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
Klement Sekera3a343d42019-05-16 14:35:46 +02001144 vlib_node_increment_counter (vm, node->node_index,
1145 IP6_ERROR_REASS_INTERNAL_ERROR,
1146 1);
Klement Sekera896c8962019-06-24 11:52:49 +00001147 ip6_full_reass_drop_all (vm, node, rm, reass);
1148 ip6_full_reass_free (rm, rt, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001149 goto next_packet;
1150 break;
1151 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001152 }
1153 else
1154 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001155 if (is_feature)
1156 {
Klement Sekera896c8962019-06-24 11:52:49 +00001157 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
Klement Sekera21aa8f12019-05-20 12:27:33 +02001158 }
1159 else
1160 {
1161 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1162 next0 = fvnb->ip.reass.error_next_index;
1163 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001164 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1165 }
1166
1167 b0->error = node->errors[error0];
1168
1169 if (~0 != bi0)
1170 {
Klement Sekera4c533132018-02-22 11:41:12 +01001171 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001172 to_next[0] = bi0;
1173 to_next += 1;
1174 n_left_to_next -= 1;
Klement Sekera896c8962019-06-24 11:52:49 +00001175 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
Klement Sekera630ab582019-07-19 09:14:19 +00001176 {
1177 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1178 {
1179 if (is_feature)
Klement Sekera896c8962019-06-24 11:52:49 +00001180 ip6_full_reass_add_trace (vm, node, rm, NULL, bi0,
1181 HANDOFF,
1182 vnet_buffer (b0)->ip.
1183 reass.owner_feature_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001184 else
Klement Sekera896c8962019-06-24 11:52:49 +00001185 ip6_full_reass_add_trace (vm, node, rm, NULL, bi0,
1186 HANDOFF,
1187 vnet_buffer (b0)->ip.
1188 reass.owner_thread_index);
Klement Sekera630ab582019-07-19 09:14:19 +00001189 }
1190 }
1191 else if (is_feature && IP6_ERROR_NONE == error0)
Klement Sekera4c533132018-02-22 11:41:12 +01001192 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001193 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001194 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001195 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001196 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1197 n_left_to_next, bi0, next0);
1198 }
1199
1200 if (~0 != icmp_bi)
1201 {
Klement Sekera896c8962019-06-24 11:52:49 +00001202 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
Klement Sekera75e7d132017-09-20 08:26:30 +02001203 to_next[0] = icmp_bi;
1204 to_next += 1;
1205 n_left_to_next -= 1;
1206 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1207 n_left_to_next, icmp_bi,
1208 next0);
1209 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001210 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001211 from += 1;
1212 n_left_from -= 1;
1213 }
1214
1215 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1216 }
1217
Klement Sekera4c533132018-02-22 11:41:12 +01001218 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001219 return frame->n_vectors;
1220}
1221
Klement Sekera896c8962019-06-24 11:52:49 +00001222static char *ip6_full_reassembly_error_strings[] = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001223#define _(sym, string) string,
1224 foreach_ip6_error
1225#undef _
1226};
1227
Klement Sekera896c8962019-06-24 11:52:49 +00001228VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1229 vlib_node_runtime_t * node,
1230 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001231{
Klement Sekera896c8962019-06-24 11:52:49 +00001232 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1233 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001234}
1235
Klement Sekera75e7d132017-09-20 08:26:30 +02001236/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001237VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1238 .name = "ip6-full-reassembly",
Klement Sekera75e7d132017-09-20 08:26:30 +02001239 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001240 .format_trace = format_ip6_full_reass_trace,
1241 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1242 .error_strings = ip6_full_reassembly_error_strings,
1243 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera75e7d132017-09-20 08:26:30 +02001244 .next_nodes =
1245 {
Klement Sekera896c8962019-06-24 11:52:49 +00001246 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1247 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1248 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1249 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001250 },
1251};
1252/* *INDENT-ON* */
1253
Klement Sekera896c8962019-06-24 11:52:49 +00001254VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1255 vlib_node_runtime_t * node,
1256 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001257{
Klement Sekera896c8962019-06-24 11:52:49 +00001258 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1259 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001260}
1261
1262/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001263VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1264 .name = "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001265 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001266 .format_trace = format_ip6_full_reass_trace,
1267 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1268 .error_strings = ip6_full_reassembly_error_strings,
1269 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
Klement Sekera4c533132018-02-22 11:41:12 +01001270 .next_nodes =
1271 {
Klement Sekera896c8962019-06-24 11:52:49 +00001272 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1273 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1274 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1275 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001276 },
1277};
1278/* *INDENT-ON* */
1279
Klement Sekera4c533132018-02-22 11:41:12 +01001280/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001281VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001282 .arc_name = "ip6-unicast",
Klement Sekera896c8962019-06-24 11:52:49 +00001283 .node_name = "ip6-full-reassembly-feature",
Neale Ranns14046982019-07-29 14:49:52 +00001284 .runs_before = VNET_FEATURES ("ip6-lookup",
Neale Ranns2be3eb62019-08-02 01:17:13 -07001285 "ipsec6-input-feature"),
Klement Sekera4c533132018-02-22 11:41:12 +01001286 .runs_after = 0,
1287};
1288/* *INDENT-ON* */
1289
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001290#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001291static u32
Klement Sekera896c8962019-06-24 11:52:49 +00001292ip6_full_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001293{
Klement Sekera896c8962019-06-24 11:52:49 +00001294 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001295 u32 nbuckets;
1296 u8 i;
1297
Klement Sekera896c8962019-06-24 11:52:49 +00001298 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
Klement Sekera75e7d132017-09-20 08:26:30 +02001299
1300 for (i = 0; i < 31; i++)
1301 if ((1 << i) >= nbuckets)
1302 break;
1303 nbuckets = 1 << i;
1304
1305 return nbuckets;
1306}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001307#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001308
1309typedef enum
1310{
1311 IP6_EVENT_CONFIG_CHANGED = 1,
Klement Sekera896c8962019-06-24 11:52:49 +00001312} ip6_full_reass_event_t;
Klement Sekera75e7d132017-09-20 08:26:30 +02001313
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001314#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001315typedef struct
1316{
1317 int failure;
1318 clib_bihash_48_8_t *new_hash;
1319} ip6_rehash_cb_ctx;
1320
Klement Sekera4c533132018-02-22 11:41:12 +01001321static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001322ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1323{
1324 ip6_rehash_cb_ctx *ctx = _ctx;
1325 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1326 {
1327 ctx->failure = 1;
1328 }
1329}
1330
Klement Sekera4c533132018-02-22 11:41:12 +01001331static void
Klement Sekera896c8962019-06-24 11:52:49 +00001332ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1333 u32 max_reassembly_length,
1334 u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001335{
Klement Sekera896c8962019-06-24 11:52:49 +00001336 ip6_full_reass_main.timeout_ms = timeout_ms;
1337 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1338 ip6_full_reass_main.max_reass_n = max_reassemblies;
1339 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1340 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
Klement Sekera4c533132018-02-22 11:41:12 +01001341}
1342
Klement Sekera75e7d132017-09-20 08:26:30 +02001343vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001344ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1345 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001346{
Klement Sekera896c8962019-06-24 11:52:49 +00001347 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1348 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1349 max_reassembly_length, expire_walk_interval_ms);
1350 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1351 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
Klement Sekera75e7d132017-09-20 08:26:30 +02001352 IP6_EVENT_CONFIG_CHANGED, 0);
Klement Sekera896c8962019-06-24 11:52:49 +00001353 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1354 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001355 {
1356 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001357 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001358 ip6_rehash_cb_ctx ctx;
1359 ctx.failure = 0;
1360 ctx.new_hash = &new_hash;
Klement Sekera896c8962019-06-24 11:52:49 +00001361 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
Klement Sekera75e7d132017-09-20 08:26:30 +02001362 new_nbuckets * 1024);
Klement Sekera896c8962019-06-24 11:52:49 +00001363 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
Klement Sekera75e7d132017-09-20 08:26:30 +02001364 ip6_rehash_cb, &ctx);
1365 if (ctx.failure)
1366 {
1367 clib_bihash_free_48_8 (&new_hash);
1368 return -1;
1369 }
1370 else
1371 {
Klement Sekera896c8962019-06-24 11:52:49 +00001372 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1373 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1374 sizeof (ip6_full_reass_main.hash));
1375 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001376 }
1377 }
1378 return 0;
1379}
1380
1381vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001382ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1383 u32 * max_reassembly_length,
1384 u32 * expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001385{
Klement Sekera896c8962019-06-24 11:52:49 +00001386 *timeout_ms = ip6_full_reass_main.timeout_ms;
1387 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1388 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1389 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
Klement Sekera75e7d132017-09-20 08:26:30 +02001390 return 0;
1391}
1392
Klement Sekera4c533132018-02-22 11:41:12 +01001393static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001394ip6_full_reass_init_function (vlib_main_t * vm)
Klement Sekera75e7d132017-09-20 08:26:30 +02001395{
Klement Sekera896c8962019-06-24 11:52:49 +00001396 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001397 clib_error_t *error = 0;
1398 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001399 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001400
1401 rm->vlib_main = vm;
Klement Sekera75e7d132017-09-20 08:26:30 +02001402
Juraj Slobodacd806922018-10-10 10:15:54 +02001403 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera896c8962019-06-24 11:52:49 +00001404 ip6_full_reass_per_thread_t *rt;
Klement Sekera4c533132018-02-22 11:41:12 +01001405 vec_foreach (rt, rm->per_thread_data)
1406 {
1407 clib_spinlock_init (&rt->lock);
1408 pool_alloc (rt->pool, rm->max_reass_n);
1409 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001410
Klement Sekera896c8962019-06-24 11:52:49 +00001411 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
Dave Barach1403fcd2018-02-05 09:45:43 -05001412 ASSERT (node);
Klement Sekera896c8962019-06-24 11:52:49 +00001413 rm->ip6_full_reass_expire_node_idx = node->index;
Dave Barach1403fcd2018-02-05 09:45:43 -05001414
Klement Sekera896c8962019-06-24 11:52:49 +00001415 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1416 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1417 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1418 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
Klement Sekera3ecc2212018-03-27 10:34:43 +02001419
Klement Sekera896c8962019-06-24 11:52:49 +00001420 nbuckets = ip6_full_reass_get_nbuckets ();
1421 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1422 nbuckets * 1024);
Klement Sekera75e7d132017-09-20 08:26:30 +02001423
Dave Barach1403fcd2018-02-05 09:45:43 -05001424 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001425 ASSERT (node);
1426 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001427 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1428 ASSERT (node);
1429 rm->ip6_icmp_error_idx = node->index;
1430
1431 if ((error = vlib_call_init_function (vm, ip_main_init)))
1432 return error;
1433 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
Klement Sekera896c8962019-06-24 11:52:49 +00001434 ip6_full_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001435
Klement Sekera896c8962019-06-24 11:52:49 +00001436 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001437 rm->fq_feature_index =
Klement Sekera896c8962019-06-24 11:52:49 +00001438 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001439
Klement Sekera75e7d132017-09-20 08:26:30 +02001440 return error;
1441}
1442
Klement Sekera896c8962019-06-24 11:52:49 +00001443VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001444#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001445
1446static uword
Klement Sekera896c8962019-06-24 11:52:49 +00001447ip6_full_reass_walk_expired (vlib_main_t * vm,
1448 vlib_node_runtime_t * node, vlib_frame_t * f)
Klement Sekera75e7d132017-09-20 08:26:30 +02001449{
Klement Sekera896c8962019-06-24 11:52:49 +00001450 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001451 uword event_type, *event_data = 0;
1452
1453 while (true)
1454 {
1455 vlib_process_wait_for_event_or_clock (vm,
1456 (f64) rm->expire_walk_interval_ms
1457 / (f64) MSEC_PER_SEC);
1458 event_type = vlib_process_get_events (vm, &event_data);
1459
1460 switch (event_type)
1461 {
1462 case ~0: /* no events => timeout */
1463 /* nothing to do here */
1464 break;
1465 case IP6_EVENT_CONFIG_CHANGED:
1466 break;
1467 default:
1468 clib_warning ("BUG: event type 0x%wx", event_type);
1469 break;
1470 }
1471 f64 now = vlib_time_now (vm);
1472
Klement Sekera896c8962019-06-24 11:52:49 +00001473 ip6_full_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001474 int *pool_indexes_to_free = NULL;
1475
Klement Sekera4c533132018-02-22 11:41:12 +01001476 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001477 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001478 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001479 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001480 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1481 {
Klement Sekera896c8962019-06-24 11:52:49 +00001482 ip6_full_reass_per_thread_t *rt =
1483 &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001484 clib_spinlock_lock (&rt->lock);
1485
1486 vec_reset_length (pool_indexes_to_free);
1487 /* *INDENT-OFF* */
1488 pool_foreach_index (index, rt->pool, ({
1489 reass = pool_elt_at_index (rt->pool, index);
1490 if (now > reass->last_heard + rm->timeout)
1491 {
1492 vec_add1 (pool_indexes_to_free, index);
1493 }
1494 }));
1495 /* *INDENT-ON* */
1496 int *i;
1497 /* *INDENT-OFF* */
1498 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 {
Klement Sekera896c8962019-06-24 11:52:49 +00001500 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
Klement Sekera4c533132018-02-22 11:41:12 +01001501 u32 icmp_bi = ~0;
Klement Sekera896c8962019-06-24 11:52:49 +00001502 ip6_full_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001503 if (~0 != icmp_bi)
Dave Baracha638c182019-06-21 18:24:07 -04001504 vec_add1 (vec_icmp_bi, icmp_bi);
1505
Klement Sekera896c8962019-06-24 11:52:49 +00001506 ip6_full_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001507 }
Klement Sekera4c533132018-02-22 11:41:12 +01001508 /* *INDENT-ON* */
1509
1510 clib_spinlock_unlock (&rt->lock);
1511 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001512
Klement Sekera75e7d132017-09-20 08:26:30 +02001513 while (vec_len (vec_icmp_bi) > 0)
1514 {
1515 vlib_frame_t *f =
1516 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1517 u32 *to_next = vlib_frame_vector_args (f);
1518 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001519 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001520 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1521 {
1522 u32 bi = vec_pop (vec_icmp_bi);
1523 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1524 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001525 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001526 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1527 to_next[0] = bi;
1528 ++f->n_vectors;
1529 to_next += 1;
1530 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001531 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001532 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001533 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1534 }
1535
1536 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001537 vec_free (vec_icmp_bi);
1538 if (event_data)
1539 {
1540 _vec_len (event_data) = 0;
1541 }
1542 }
1543
1544 return 0;
1545}
1546
Klement Sekera75e7d132017-09-20 08:26:30 +02001547/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001548VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1549 .function = ip6_full_reass_walk_expired,
1550 .format_trace = format_ip6_full_reass_trace,
Klement Sekera75e7d132017-09-20 08:26:30 +02001551 .type = VLIB_NODE_TYPE_PROCESS,
Klement Sekera896c8962019-06-24 11:52:49 +00001552 .name = "ip6-full-reassembly-expire-walk",
Klement Sekera75e7d132017-09-20 08:26:30 +02001553
Klement Sekera896c8962019-06-24 11:52:49 +00001554 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1555 .error_strings = ip6_full_reassembly_error_strings,
Klement Sekera75e7d132017-09-20 08:26:30 +02001556
1557};
1558/* *INDENT-ON* */
1559
1560static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001561format_ip6_full_reass_key (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001562{
Klement Sekera896c8962019-06-24 11:52:49 +00001563 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001564 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1565 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1566 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1567 return s;
1568}
1569
1570static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001571format_ip6_full_reass (u8 * s, va_list * args)
Klement Sekera75e7d132017-09-20 08:26:30 +02001572{
1573 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001574 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
Klement Sekera75e7d132017-09-20 08:26:30 +02001575
Klement Sekera4c533132018-02-22 11:41:12 +01001576 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001577 "last_packet_octet: %u, trace_op_counter: %u\n",
Klement Sekera896c8962019-06-24 11:52:49 +00001578 reass->id, format_ip6_full_reass_key, &reass->key,
1579 reass->first_bi, reass->data_len, reass->last_packet_octet,
Klement Sekera75e7d132017-09-20 08:26:30 +02001580 reass->trace_op_counter);
1581 u32 bi = reass->first_bi;
1582 u32 counter = 0;
1583 while (~0 != bi)
1584 {
1585 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1586 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1587 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1588 "fragment[%u, %u]\n",
1589 counter, vnb->ip.reass.range_first,
1590 vnb->ip.reass.range_last, bi,
Klement Sekera896c8962019-06-24 11:52:49 +00001591 ip6_full_reass_buffer_get_data_offset (b),
1592 ip6_full_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001593 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1594 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1595 {
1596 bi = b->next_buffer;
1597 }
1598 else
1599 {
1600 bi = ~0;
1601 }
1602 }
1603 return s;
1604}
1605
1606static clib_error_t *
Klement Sekera896c8962019-06-24 11:52:49 +00001607show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1608 CLIB_UNUSED (vlib_cli_command_t * lmd))
Klement Sekera75e7d132017-09-20 08:26:30 +02001609{
Klement Sekera896c8962019-06-24 11:52:49 +00001610 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Klement Sekera75e7d132017-09-20 08:26:30 +02001611
1612 vlib_cli_output (vm, "---------------------");
1613 vlib_cli_output (vm, "IP6 reassembly status");
1614 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001615 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001616 if (unformat (input, "details"))
1617 {
Klement Sekera4c533132018-02-22 11:41:12 +01001618 details = true;
1619 }
1620
1621 u32 sum_reass_n = 0;
1622 u64 sum_buffers_n = 0;
Klement Sekera896c8962019-06-24 11:52:49 +00001623 ip6_full_reass_t *reass;
Klement Sekera4c533132018-02-22 11:41:12 +01001624 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001625 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001626 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1627 {
Klement Sekera896c8962019-06-24 11:52:49 +00001628 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +01001629 clib_spinlock_lock (&rt->lock);
1630 if (details)
1631 {
1632 /* *INDENT-OFF* */
1633 pool_foreach (reass, rt->pool, {
Klement Sekera896c8962019-06-24 11:52:49 +00001634 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +01001635 });
1636 /* *INDENT-ON* */
1637 }
1638 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001639 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001640 }
1641 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001642 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1643 (long unsigned) sum_reass_n);
1644 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1645 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001646 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001647 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001648 return 0;
1649}
1650
1651/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001652VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1653 .path = "show ip6-full-reassembly",
1654 .short_help = "show ip6-full-reassembly [details]",
1655 .function = show_ip6_full_reass,
Klement Sekera75e7d132017-09-20 08:26:30 +02001656};
1657/* *INDENT-ON* */
1658
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001659#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001660vnet_api_error_t
Klement Sekera896c8962019-06-24 11:52:49 +00001661ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
Klement Sekera4c533132018-02-22 11:41:12 +01001662{
Klement Sekera896c8962019-06-24 11:52:49 +00001663 return vnet_feature_enable_disable ("ip6-unicast",
1664 "ip6-full-reassembly-feature",
Klement Sekera4c533132018-02-22 11:41:12 +01001665 sw_if_index, enable_disable, 0, 0);
1666}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001667#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001668
Klement Sekera896c8962019-06-24 11:52:49 +00001669#define foreach_ip6_full_reassembly_handoff_error \
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001670_(CONGESTION_DROP, "congestion drop")
1671
1672
1673typedef enum
1674{
Klement Sekera896c8962019-06-24 11:52:49 +00001675#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1676 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001677#undef _
Klement Sekera896c8962019-06-24 11:52:49 +00001678 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1679} ip6_full_reassembly_handoff_error_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001680
Klement Sekera896c8962019-06-24 11:52:49 +00001681static char *ip6_full_reassembly_handoff_error_strings[] = {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001682#define _(sym,string) string,
Klement Sekera896c8962019-06-24 11:52:49 +00001683 foreach_ip6_full_reassembly_handoff_error
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001684#undef _
1685};
1686
1687typedef struct
1688{
1689 u32 next_worker_index;
Klement Sekera896c8962019-06-24 11:52:49 +00001690} ip6_full_reassembly_handoff_trace_t;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001691
1692static u8 *
Klement Sekera896c8962019-06-24 11:52:49 +00001693format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001694{
1695 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1696 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Klement Sekera896c8962019-06-24 11:52:49 +00001697 ip6_full_reassembly_handoff_trace_t *t =
1698 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001699
1700 s =
Klement Sekera896c8962019-06-24 11:52:49 +00001701 format (s, "ip6-full-reassembly-handoff: next-worker %d",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001702 t->next_worker_index);
1703
1704 return s;
1705}
1706
1707always_inline uword
Klement Sekera896c8962019-06-24 11:52:49 +00001708ip6_full_reassembly_handoff_inline (vlib_main_t * vm,
1709 vlib_node_runtime_t * node,
1710 vlib_frame_t * frame, bool is_feature)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001711{
Klement Sekera896c8962019-06-24 11:52:49 +00001712 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001713
1714 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1715 u32 n_enq, n_left_from, *from;
1716 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1717 u32 fq_index;
1718
1719 from = vlib_frame_vector_args (frame);
1720 n_left_from = frame->n_vectors;
1721 vlib_get_buffers (vm, from, bufs, n_left_from);
1722
1723 b = bufs;
1724 ti = thread_indices;
1725
1726 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1727
1728 while (n_left_from > 0)
1729 {
1730 ti[0] =
1731 (is_feature) ? vnet_buffer (b[0])->ip.
1732 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1733 reass.owner_thread_index;
1734
1735 if (PREDICT_FALSE
1736 ((node->flags & VLIB_NODE_FLAG_TRACE)
1737 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1738 {
Klement Sekera896c8962019-06-24 11:52:49 +00001739 ip6_full_reassembly_handoff_trace_t *t =
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001740 vlib_add_trace (vm, node, b[0], sizeof (*t));
1741 t->next_worker_index = ti[0];
1742 }
1743
1744 n_left_from -= 1;
1745 ti += 1;
1746 b += 1;
1747 }
1748 n_enq =
1749 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1750 frame->n_vectors, 1);
1751
1752 if (n_enq < frame->n_vectors)
1753 vlib_node_increment_counter (vm, node->node_index,
Klement Sekera896c8962019-06-24 11:52:49 +00001754 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001755 frame->n_vectors - n_enq);
1756 return frame->n_vectors;
1757}
1758
Klement Sekera896c8962019-06-24 11:52:49 +00001759VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1760 vlib_node_runtime_t * node,
1761 vlib_frame_t * frame)
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001762{
Klement Sekera896c8962019-06-24 11:52:49 +00001763 return ip6_full_reassembly_handoff_inline (vm, node, frame,
1764 false /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001765}
1766
1767/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001768VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1769 .name = "ip6-full-reassembly-handoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001770 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001771 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1772 .error_strings = ip6_full_reassembly_handoff_error_strings,
1773 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001774
1775 .n_next_nodes = 1,
1776
1777 .next_nodes = {
1778 [0] = "error-drop",
1779 },
1780};
1781
1782
Klement Sekera896c8962019-06-24 11:52:49 +00001783VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001784 vlib_node_runtime_t * node, vlib_frame_t * frame)
1785{
Klement Sekera896c8962019-06-24 11:52:49 +00001786 return ip6_full_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001787}
1788
1789
1790/* *INDENT-OFF* */
Klement Sekera896c8962019-06-24 11:52:49 +00001791VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1792 .name = "ip6-full-reass-feature-hoff",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001793 .vector_size = sizeof (u32),
Klement Sekera896c8962019-06-24 11:52:49 +00001794 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1795 .error_strings = ip6_full_reassembly_handoff_error_strings,
1796 .format_trace = format_ip6_full_reassembly_handoff_trace,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001797
1798 .n_next_nodes = 1,
1799
1800 .next_nodes = {
1801 [0] = "error-drop",
1802 },
1803};
1804/* *INDENT-ON* */
1805
Klement Sekera75e7d132017-09-20 08:26:30 +02001806/*
1807 * fd.io coding-style-patch-verification: ON
1808 *
1809 * Local Variables:
1810 * eval: (c-set-style "gnu")
1811 * End:
1812 */