blob: 58c7d8d8433e1967dc2a18727a9cc97295f58ae3 [file] [log] [blame]
Klement Sekerade34c352019-06-25 11:19:22 +00001/*
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
18 * @brief IPv6 Shallow Virtual Reassembly.
19 *
20 * This file contains the source code for IPv6 Shallow Virtual reassembly.
21 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
26#include <vnet/ip/ip6_to_ip4.h>
27#include <vppinfra/bihash_48_8.h>
28#include <vnet/ip/reass/ip6_sv_reass.h>
Ole Troan03092c12021-11-23 15:55:39 +010029#include <vnet/ip/ip6_inlines.h>
Klement Sekerade34c352019-06-25 11:19:22 +000030
31#define MSEC_PER_SEC 1000
32#define IP6_SV_REASS_TIMEOUT_DEFAULT_MS 100
33#define IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
34#define IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT 1024
35#define IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
36#define IP6_SV_REASS_HT_LOAD_FACTOR (0.75)
37
38typedef enum
39{
40 IP6_SV_REASS_RC_OK,
41 IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS,
42 IP6_SV_REASS_RC_INTERNAL_ERROR,
43 IP6_SV_REASS_RC_UNSUPP_IP_PROTO,
Klement Sekera755042d2021-12-01 10:14:38 +000044 IP6_SV_REASS_RC_INVALID_FRAG_LEN,
Klement Sekerade34c352019-06-25 11:19:22 +000045} ip6_sv_reass_rc_t;
46
47typedef struct
48{
49 union
50 {
51 struct
52 {
53 ip6_address_t src;
54 ip6_address_t dst;
Klement Sekera5a58e7c2021-11-03 17:57:28 +010055 u32 fib_index;
Klement Sekerade34c352019-06-25 11:19:22 +000056 u32 frag_id;
57 u8 unused[7];
58 u8 proto;
59 };
60 u64 as_u64[6];
61 };
62} ip6_sv_reass_key_t;
63
64typedef union
65{
66 struct
67 {
68 u32 reass_index;
69 u32 thread_index;
70 };
71 u64 as_u64;
72} ip6_sv_reass_val_t;
73
74typedef union
75{
76 struct
77 {
78 ip6_sv_reass_key_t k;
79 ip6_sv_reass_val_t v;
80 };
81 clib_bihash_kv_48_8_t kv;
82} ip6_sv_reass_kv_t;
83
84typedef struct
85{
86 // hash table key
87 ip6_sv_reass_key_t key;
88 // time when last packet was received
89 f64 last_heard;
90 // internal id of this reassembly
91 u64 id;
92 // trace operation counter
93 u32 trace_op_counter;
94 // buffer indexes of buffers in this reassembly in chronological order -
95 // including overlaps and duplicate fragments
96 u32 *cached_buffers;
97 // set to true when this reassembly is completed
98 bool is_complete;
99 // ip protocol
100 u8 ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000101 u8 icmp_type_or_tcp_flags;
102 u32 tcp_ack_number;
103 u32 tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000104 // l4 src port
105 u16 l4_src_port;
106 // l4 dst port
107 u16 l4_dst_port;
108 // lru indexes
109 u32 lru_prev;
110 u32 lru_next;
111} ip6_sv_reass_t;
112
113typedef struct
114{
115 ip6_sv_reass_t *pool;
116 u32 reass_n;
117 u32 id_counter;
118 clib_spinlock_t lock;
119 // lru indexes
120 u32 lru_first;
121 u32 lru_last;
122} ip6_sv_reass_per_thread_t;
123
124typedef struct
125{
126 // IPv6 config
127 u32 timeout_ms;
128 f64 timeout;
129 u32 expire_walk_interval_ms;
130 // maximum number of fragments in one reassembly
131 u32 max_reass_len;
132 // maximum number of reassemblies
133 u32 max_reass_n;
134
135 // IPv6 runtime
136 clib_bihash_48_8_t hash;
137
138 // per-thread data
139 ip6_sv_reass_per_thread_t *per_thread_data;
140
141 // convenience
142 vlib_main_t *vlib_main;
143 vnet_main_t *vnet_main;
144
145 // node index of ip6-drop node
146 u32 ip6_drop_idx;
147 u32 ip6_icmp_error_idx;
148 u32 ip6_sv_reass_expire_node_idx;
149
150 /** Worker handoff */
151 u32 fq_index;
152 u32 fq_feature_index;
153
Klement Sekera63c73532019-09-30 14:35:36 +0000154 // reference count for enabling/disabling feature - per interface
155 u32 *feature_use_refcount_per_intf;
Klement Sekerade34c352019-06-25 11:19:22 +0000156} ip6_sv_reass_main_t;
157
158extern ip6_sv_reass_main_t ip6_sv_reass_main;
159
160#ifndef CLIB_MARCH_VARIANT
161ip6_sv_reass_main_t ip6_sv_reass_main;
162#endif /* CLIB_MARCH_VARIANT */
163
164typedef enum
165{
166 IP6_SV_REASSEMBLY_NEXT_INPUT,
167 IP6_SV_REASSEMBLY_NEXT_DROP,
168 IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR,
169 IP6_SV_REASSEMBLY_NEXT_HANDOFF,
170 IP6_SV_REASSEMBLY_N_NEXT,
171} ip6_sv_reass_next_t;
172
173typedef enum
174{
Klement Sekerade34c352019-06-25 11:19:22 +0000175 REASS_FRAGMENT_CACHE,
176 REASS_FINISH,
177 REASS_FRAGMENT_FORWARD,
Klement Sekeraf126e742019-10-10 09:46:06 +0000178 REASS_PASSTHROUGH,
Klement Sekerade34c352019-06-25 11:19:22 +0000179} ip6_sv_reass_trace_operation_e;
180
181typedef struct
182{
183 ip6_sv_reass_trace_operation_e action;
184 u32 reass_id;
185 u32 op_id;
186 u8 ip_proto;
187 u16 l4_src_port;
188 u16 l4_dst_port;
189} ip6_sv_reass_trace_t;
190
191static u8 *
192format_ip6_sv_reass_trace (u8 * s, va_list * args)
193{
194 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
195 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
196 ip6_sv_reass_trace_t *t = va_arg (*args, ip6_sv_reass_trace_t *);
Klement Sekeraf126e742019-10-10 09:46:06 +0000197 if (REASS_PASSTHROUGH != t->action)
198 {
199 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
200 }
Klement Sekerade34c352019-06-25 11:19:22 +0000201 switch (t->action)
202 {
Klement Sekerade34c352019-06-25 11:19:22 +0000203 case REASS_FRAGMENT_CACHE:
204 s = format (s, "[cached]");
205 break;
206 case REASS_FINISH:
207 s =
208 format (s, "[finish, ip proto=%u, src_port=%u, dst_port=%u]",
209 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
210 clib_net_to_host_u16 (t->l4_dst_port));
211 break;
212 case REASS_FRAGMENT_FORWARD:
213 s =
214 format (s, "[forward, ip proto=%u, src_port=%u, dst_port=%u]",
215 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
216 clib_net_to_host_u16 (t->l4_dst_port));
217 break;
Klement Sekeraf126e742019-10-10 09:46:06 +0000218 case REASS_PASSTHROUGH:
Klement Sekera7c3275e2021-12-07 09:49:53 +0000219 s = format (s, "[not fragmented or atomic fragment]");
Klement Sekeraf126e742019-10-10 09:46:06 +0000220 break;
Klement Sekerade34c352019-06-25 11:19:22 +0000221 }
222 return s;
223}
224
225static void
226ip6_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerade34c352019-06-25 11:19:22 +0000227 ip6_sv_reass_t * reass, u32 bi,
228 ip6_sv_reass_trace_operation_e action,
229 u32 ip_proto, u16 l4_src_port, u16 l4_dst_port)
230{
231 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera53be16d2020-12-15 21:47:36 +0100232 if (pool_is_free_index
233 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
234 {
235 // this buffer's trace is gone
236 b->flags &= ~VLIB_BUFFER_IS_TRACED;
237 return;
238 }
Klement Sekerade34c352019-06-25 11:19:22 +0000239 ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekeraf126e742019-10-10 09:46:06 +0000240 if (reass)
241 {
242 t->reass_id = reass->id;
243 t->op_id = reass->trace_op_counter;
244 ++reass->trace_op_counter;
245 }
Klement Sekerade34c352019-06-25 11:19:22 +0000246 t->action = action;
Klement Sekerade34c352019-06-25 11:19:22 +0000247 t->ip_proto = ip_proto;
248 t->l4_src_port = l4_src_port;
249 t->l4_dst_port = l4_dst_port;
Klement Sekerade34c352019-06-25 11:19:22 +0000250#if 0
251 static u8 *s = NULL;
252 s = format (s, "%U", format_ip6_sv_reass_trace, NULL, NULL, t);
253 printf ("%.*s\n", vec_len (s), s);
254 fflush (stdout);
255 vec_reset_length (s);
256#endif
257}
258
259always_inline void
260ip6_sv_reass_free (vlib_main_t * vm, ip6_sv_reass_main_t * rm,
261 ip6_sv_reass_per_thread_t * rt, ip6_sv_reass_t * reass)
262{
263 clib_bihash_kv_48_8_t kv;
264 kv.key[0] = reass->key.as_u64[0];
265 kv.key[1] = reass->key.as_u64[1];
266 kv.key[2] = reass->key.as_u64[2];
267 kv.key[3] = reass->key.as_u64[3];
268 kv.key[4] = reass->key.as_u64[4];
269 kv.key[5] = reass->key.as_u64[5];
270 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
271 vlib_buffer_free (vm, reass->cached_buffers,
272 vec_len (reass->cached_buffers));
273 vec_free (reass->cached_buffers);
274 reass->cached_buffers = NULL;
275 if (~0 != reass->lru_prev)
276 {
277 ip6_sv_reass_t *lru_prev =
278 pool_elt_at_index (rt->pool, reass->lru_prev);
279 lru_prev->lru_next = reass->lru_next;
280 }
281 if (~0 != reass->lru_next)
282 {
283 ip6_sv_reass_t *lru_next =
284 pool_elt_at_index (rt->pool, reass->lru_next);
285 lru_next->lru_prev = reass->lru_prev;
286 }
287 if (rt->lru_first == reass - rt->pool)
288 {
289 rt->lru_first = reass->lru_next;
290 }
291 if (rt->lru_last == reass - rt->pool)
292 {
293 rt->lru_last = reass->lru_prev;
294 }
295 pool_put (rt->pool, reass);
296 --rt->reass_n;
297}
298
299always_inline void
300ip6_sv_reass_init (ip6_sv_reass_t * reass)
301{
302 reass->cached_buffers = NULL;
303 reass->is_complete = false;
304}
305
306always_inline ip6_sv_reass_t *
Klement Sekera42cec0e2021-08-02 16:14:15 +0200307ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm,
308 ip6_sv_reass_per_thread_t *rt,
309 ip6_sv_reass_kv_t *kv, u8 *do_handoff)
Klement Sekerade34c352019-06-25 11:19:22 +0000310{
311 ip6_sv_reass_t *reass = NULL;
Tom Seidenberg5a7f2f12020-04-28 17:58:12 -0400312 f64 now = vlib_time_now (vm);
Klement Sekerade34c352019-06-25 11:19:22 +0000313
Klement Sekerac99c0252019-12-18 12:17:06 +0000314 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekerade34c352019-06-25 11:19:22 +0000315 {
316 if (vm->thread_index != kv->v.thread_index)
317 {
318 *do_handoff = 1;
319 return NULL;
320 }
321 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
322
323 if (now > reass->last_heard + rm->timeout)
324 {
325 ip6_sv_reass_free (vm, rm, rt, reass);
326 reass = NULL;
327 }
328 }
329
330 if (reass)
331 {
332 reass->last_heard = now;
333 return reass;
334 }
335
336 if (rt->reass_n >= rm->max_reass_n)
337 {
zhengdelunce533632020-05-22 15:42:28 +0800338 reass = pool_elt_at_index (rt->pool, rt->lru_first);
Klement Sekerade34c352019-06-25 11:19:22 +0000339 ip6_sv_reass_free (vm, rm, rt, reass);
340 }
341
342 pool_get (rt->pool, reass);
343 clib_memset (reass, 0, sizeof (*reass));
344 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
345 ++rt->id_counter;
346 ip6_sv_reass_init (reass);
347 ++rt->reass_n;
348
349 reass->lru_prev = reass->lru_next = ~0;
350
351 if (~0 != rt->lru_last)
352 {
353 ip6_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last);
354 reass->lru_prev = rt->lru_last;
355 lru_last->lru_next = rt->lru_last = reass - rt->pool;
356 }
357
358 if (~0 == rt->lru_first)
359 {
360 rt->lru_first = rt->lru_last = reass - rt->pool;
361 }
362
Klement Sekerac99c0252019-12-18 12:17:06 +0000363 reass->key.as_u64[0] = kv->kv.key[0];
364 reass->key.as_u64[1] = kv->kv.key[1];
365 reass->key.as_u64[2] = kv->kv.key[2];
366 reass->key.as_u64[3] = kv->kv.key[3];
367 reass->key.as_u64[4] = kv->kv.key[4];
368 reass->key.as_u64[5] = kv->kv.key[5];
Klement Sekerade34c352019-06-25 11:19:22 +0000369 kv->v.reass_index = (reass - rt->pool);
370 kv->v.thread_index = vm->thread_index;
371 reass->last_heard = now;
372
Klement Sekerac99c0252019-12-18 12:17:06 +0000373 if (clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 1))
Klement Sekerade34c352019-06-25 11:19:22 +0000374 {
375 ip6_sv_reass_free (vm, rm, rt, reass);
376 reass = NULL;
377 }
378
379 return reass;
380}
381
382always_inline ip6_sv_reass_rc_t
Klement Sekera42cec0e2021-08-02 16:14:15 +0200383ip6_sv_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
384 ip6_sv_reass_main_t *rm, ip6_sv_reass_t *reass, u32 bi0,
385 ip6_frag_hdr_t *frag_hdr)
Klement Sekerade34c352019-06-25 11:19:22 +0000386{
387 vlib_buffer_t *fb = vlib_get_buffer (vm, bi0);
388 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
389 fvnb->ip.reass.ip6_frag_hdr_offset =
390 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
391 ip6_header_t *fip = vlib_buffer_get_current (fb);
392 if (fb->current_length < sizeof (*fip) ||
393 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
394 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
395 {
396 return IP6_SV_REASS_RC_INTERNAL_ERROR;
397 }
398
399 u32 fragment_first = fvnb->ip.reass.fragment_first =
400 ip6_frag_hdr_offset_bytes (frag_hdr);
401 u32 fragment_length =
402 vlib_buffer_length_in_chain (vm, fb) -
403 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
Klement Sekera755042d2021-12-01 10:14:38 +0000404 if (0 == fragment_length)
405 {
406 return IP6_SV_REASS_RC_INVALID_FRAG_LEN;
407 }
Klement Sekerade34c352019-06-25 11:19:22 +0000408 u32 fragment_last = fvnb->ip.reass.fragment_last =
409 fragment_first + fragment_length - 1;
410 fvnb->ip.reass.range_first = fragment_first;
411 fvnb->ip.reass.range_last = fragment_last;
412 fvnb->ip.reass.next_range_bi = ~0;
413 if (0 == fragment_first)
414 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000415 if (!ip6_get_port
416 (vm, fb, fip, fb->current_length, &reass->ip_proto,
417 &reass->l4_src_port, &reass->l4_dst_port,
418 &reass->icmp_type_or_tcp_flags, &reass->tcp_ack_number,
419 &reass->tcp_seq_number))
Klement Sekerade34c352019-06-25 11:19:22 +0000420 return IP6_SV_REASS_RC_UNSUPP_IP_PROTO;
Klement Sekeraf126e742019-10-10 09:46:06 +0000421
Klement Sekerade34c352019-06-25 11:19:22 +0000422 reass->is_complete = true;
423 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
424 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
425 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200426 ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FINISH,
Klement Sekerade34c352019-06-25 11:19:22 +0000427 reass->ip_proto, reass->l4_src_port,
428 reass->l4_dst_port);
429 }
430 }
431 vec_add1 (reass->cached_buffers, bi0);
432 if (!reass->is_complete)
433 {
434 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
435 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200436 ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FRAGMENT_CACHE,
437 reass->ip_proto, reass->l4_src_port,
438 reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000439 }
440 if (vec_len (reass->cached_buffers) > rm->max_reass_len)
441 {
442 return IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS;
443 }
444 }
445 return IP6_SV_REASS_RC_OK;
446}
447
448always_inline bool
Ole Troan03092c12021-11-23 15:55:39 +0100449ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
450 vlib_buffer_t *b,
451 ip6_ext_hdr_chain_t *hc)
Klement Sekerade34c352019-06-25 11:19:22 +0000452{
Ole Troan03092c12021-11-23 15:55:39 +0100453 int nh = hc->eh[hc->length - 1].protocol;
454 /* Checking to see if it's a terminating header */
455 if (ip6_ext_hdr (nh))
Klement Sekerade34c352019-06-25 11:19:22 +0000456 {
Ole Troan03092c12021-11-23 15:55:39 +0100457 icmp6_error_set_vnet_buffer (
458 b, ICMP6_parameter_problem,
459 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
Klement Sekerade34c352019-06-25 11:19:22 +0000460 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
Klement Sekerade34c352019-06-25 11:19:22 +0000461 return false;
462 }
463 return true;
464}
465
466always_inline bool
467ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000468 vlib_buffer_t * b,
469 ip6_frag_hdr_t * frag_hdr)
470{
471 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
472 ip6_header_t *ip = vlib_buffer_get_current (b);
473 int more_fragments = ip6_frag_hdr_more (frag_hdr);
474 u32 fragment_length =
475 vlib_buffer_length_in_chain (vm, b) -
476 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
477 if (more_fragments && 0 != fragment_length % 8)
478 {
479 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
480 ICMP6_parameter_problem_erroneous_header_field,
481 (u8 *) & ip->payload_length - (u8 *) ip);
482 return false;
483 }
484 return true;
485}
486
487always_inline bool
488ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000489 vlib_buffer_t * b,
490 ip6_frag_hdr_t * frag_hdr)
491{
492 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
493 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
494 u32 fragment_length =
495 vlib_buffer_length_in_chain (vm, b) -
496 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
497 if (fragment_first + fragment_length > 65535)
498 {
499 ip6_header_t *ip0 = vlib_buffer_get_current (b);
500 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
501 ICMP6_parameter_problem_erroneous_header_field,
502 (u8 *) & frag_hdr->fragment_offset_and_more
503 - (u8 *) ip0);
504 return false;
505 }
506 return true;
507}
508
509always_inline uword
510ip6_sv_reassembly_inline (vlib_main_t * vm,
511 vlib_node_runtime_t * node,
512 vlib_frame_t * frame, bool is_feature)
513{
514 u32 *from = vlib_frame_vector_args (frame);
515 u32 n_left_from, n_left_to_next, *to_next, next_index;
516 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
517 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
518 clib_spinlock_lock (&rt->lock);
519
520 n_left_from = frame->n_vectors;
521 next_index = node->cached_next_index;
522
523 while (n_left_from > 0)
524 {
525 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
526
527 while (n_left_from > 0 && n_left_to_next > 0)
528 {
529 u32 bi0;
530 vlib_buffer_t *b0;
531 u32 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
532 u32 error0 = IP6_ERROR_NONE;
Klement Sekerade34c352019-06-25 11:19:22 +0000533
534 bi0 = from[0];
535 b0 = vlib_get_buffer (vm, bi0);
536
537 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Ole Troan03092c12021-11-23 15:55:39 +0100538 ip6_frag_hdr_t *frag_hdr;
539 ip6_ext_hdr_chain_t hdr_chain;
Klement Sekera7c3275e2021-12-07 09:49:53 +0000540 bool is_atomic_fragment = false;
Ole Troan03092c12021-11-23 15:55:39 +0100541
542 int res = ip6_ext_header_walk (
543 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
Klement Sekera7c3275e2021-12-07 09:49:53 +0000544 if (res >= 0 &&
545 hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
Klement Sekerade34c352019-06-25 11:19:22 +0000546 {
Klement Sekera7c3275e2021-12-07 09:49:53 +0000547 frag_hdr =
548 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
549 is_atomic_fragment = (0 == ip6_frag_hdr_offset (frag_hdr) &&
550 !ip6_frag_hdr_more (frag_hdr));
551 }
552
553 if (res < 0 ||
554 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION ||
555 is_atomic_fragment)
556 {
557 // this is a regular unfragmented packet or an atomic fragment
Klement Sekeraf126e742019-10-10 09:46:06 +0000558 if (!ip6_get_port
559 (vm, b0, ip0, b0->current_length,
560 &(vnet_buffer (b0)->ip.reass.ip_proto),
561 &(vnet_buffer (b0)->ip.reass.l4_src_port),
562 &(vnet_buffer (b0)->ip.reass.l4_dst_port),
563 &(vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags),
564 &(vnet_buffer (b0)->ip.reass.tcp_ack_number),
565 &(vnet_buffer (b0)->ip.reass.tcp_seq_number)))
566 {
567 error0 = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200568 b0->error = node->errors[error0];
Klement Sekeraf126e742019-10-10 09:46:06 +0000569 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
570 goto packet_enqueue;
571 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000572 vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
Klement Sekerade34c352019-06-25 11:19:22 +0000573 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekeraf126e742019-10-10 09:46:06 +0000574 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
575 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200576 ip6_sv_reass_add_trace (
577 vm, node, NULL, bi0, REASS_PASSTHROUGH,
578 vnet_buffer (b0)->ip.reass.ip_proto,
579 vnet_buffer (b0)->ip.reass.l4_src_port,
580 vnet_buffer (b0)->ip.reass.l4_dst_port);
Klement Sekeraf126e742019-10-10 09:46:06 +0000581 }
Klement Sekerade34c352019-06-25 11:19:22 +0000582 goto packet_enqueue;
583 }
Klement Sekera7c3275e2021-12-07 09:49:53 +0000584
Klement Sekera38f7ccb2019-10-28 11:26:28 +0000585 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
Ole Troan03092c12021-11-23 15:55:39 +0100586 hdr_chain.eh[res].offset;
Klement Sekera7c3275e2021-12-07 09:49:53 +0000587
Klement Sekerade34c352019-06-25 11:19:22 +0000588 if (0 == ip6_frag_hdr_offset (frag_hdr))
589 {
590 // first fragment - verify upper-layer is present
Ole Troan03092c12021-11-23 15:55:39 +0100591 if (!ip6_sv_reass_verify_upper_layer_present (node, b0,
592 &hdr_chain))
Klement Sekerade34c352019-06-25 11:19:22 +0000593 {
594 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
595 goto packet_enqueue;
596 }
597 }
Klement Sekera42cec0e2021-08-02 16:14:15 +0200598 if (!ip6_sv_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
599 !ip6_sv_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
Klement Sekerade34c352019-06-25 11:19:22 +0000600 {
601 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
602 goto packet_enqueue;
603 }
Klement Sekerade34c352019-06-25 11:19:22 +0000604
605 ip6_sv_reass_kv_t kv;
606 u8 do_handoff = 0;
607
608 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
609 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
610 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
611 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
612 kv.k.as_u64[4] =
613 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
614 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
615 (u64) frag_hdr->identification;
616 kv.k.as_u64[5] = ip0->protocol;
617
618 ip6_sv_reass_t *reass =
Klement Sekera42cec0e2021-08-02 16:14:15 +0200619 ip6_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
Klement Sekerade34c352019-06-25 11:19:22 +0000620
621 if (PREDICT_FALSE (do_handoff))
622 {
623 next0 = IP6_SV_REASSEMBLY_NEXT_HANDOFF;
624 vnet_buffer (b0)->ip.reass.owner_thread_index =
625 kv.v.thread_index;
Klement Sekera364b20a2019-10-07 09:48:06 +0000626 goto packet_enqueue;
Klement Sekerade34c352019-06-25 11:19:22 +0000627 }
628
629 if (!reass)
630 {
631 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
632 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200633 b0->error = node->errors[error0];
Klement Sekerade34c352019-06-25 11:19:22 +0000634 goto packet_enqueue;
635 }
636
637 if (reass->is_complete)
638 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000639 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
640 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000641 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000642 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
643 reass->icmp_type_or_tcp_flags;
644 vnet_buffer (b0)->ip.reass.tcp_ack_number =
645 reass->tcp_ack_number;
646 vnet_buffer (b0)->ip.reass.tcp_seq_number =
647 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000648 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
649 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
650 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekerade34c352019-06-25 11:19:22 +0000651 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
652 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200653 ip6_sv_reass_add_trace (
654 vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
655 reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000656 }
657 goto packet_enqueue;
658 }
659
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100660 u32 counter = ~0;
Klement Sekera42cec0e2021-08-02 16:14:15 +0200661 switch (ip6_sv_reass_update (vm, node, rm, reass, bi0, frag_hdr))
Klement Sekerade34c352019-06-25 11:19:22 +0000662 {
663 case IP6_SV_REASS_RC_OK:
664 /* nothing to do here */
665 break;
666 case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100667 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
Klement Sekerade34c352019-06-25 11:19:22 +0000668 break;
669 case IP6_SV_REASS_RC_UNSUPP_IP_PROTO:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100670 counter = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
Klement Sekerade34c352019-06-25 11:19:22 +0000671 break;
672 case IP6_SV_REASS_RC_INTERNAL_ERROR:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100673 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
674 break;
Klement Sekera755042d2021-12-01 10:14:38 +0000675 case IP6_SV_REASS_RC_INVALID_FRAG_LEN:
676 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
677 break;
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100678 }
679 if (~0 != counter)
680 {
681 vlib_node_increment_counter (vm, node->node_index, counter, 1);
Klement Sekerade34c352019-06-25 11:19:22 +0000682 ip6_sv_reass_free (vm, rm, rt, reass);
683 goto next_packet;
Klement Sekerade34c352019-06-25 11:19:22 +0000684 }
685
Klement Sekerade34c352019-06-25 11:19:22 +0000686 if (reass->is_complete)
687 {
688 u32 idx;
689 vec_foreach_index (idx, reass->cached_buffers)
690 {
691 u32 bi0 = vec_elt (reass->cached_buffers, idx);
692 if (0 == n_left_to_next)
693 {
694 vlib_put_next_frame (vm, node, next_index,
695 n_left_to_next);
696 vlib_get_next_frame (vm, node, next_index, to_next,
697 n_left_to_next);
698 }
699 to_next[0] = bi0;
700 to_next += 1;
701 n_left_to_next -= 1;
702 b0 = vlib_get_buffer (vm, bi0);
703 if (is_feature)
704 {
705 vnet_feature_next (&next0, b0);
706 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000707 frag_hdr =
708 vlib_buffer_get_current (b0) +
709 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset;
Klement Sekeraf126e742019-10-10 09:46:06 +0000710 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
711 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000712 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000713 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
714 reass->icmp_type_or_tcp_flags;
715 vnet_buffer (b0)->ip.reass.tcp_ack_number =
716 reass->tcp_ack_number;
717 vnet_buffer (b0)->ip.reass.tcp_seq_number =
718 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000719 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
720 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
721 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
722 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200723 ip6_sv_reass_add_trace (
724 vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
725 reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000726 }
727 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
728 to_next, n_left_to_next, bi0,
729 next0);
730 }
731 _vec_len (reass->cached_buffers) = 0; // buffers are owned by frame now
732 }
733 goto next_packet;
734
735 packet_enqueue:
736 to_next[0] = bi0;
737 to_next += 1;
738 n_left_to_next -= 1;
739 if (is_feature && IP6_ERROR_NONE == error0)
740 {
741 b0 = vlib_get_buffer (vm, bi0);
742 vnet_feature_next (&next0, b0);
743 }
744 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
745 n_left_to_next, bi0, next0);
746
Klement Sekerade34c352019-06-25 11:19:22 +0000747 next_packet:
748 from += 1;
749 n_left_from -= 1;
750 }
751
752 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
753 }
754
755 clib_spinlock_unlock (&rt->lock);
756 return frame->n_vectors;
757}
758
759static char *ip6_sv_reassembly_error_strings[] = {
760#define _(sym, string) string,
761 foreach_ip6_error
762#undef _
763};
764
765VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm,
766 vlib_node_runtime_t * node,
767 vlib_frame_t * frame)
768{
769 return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */ );
770}
771
772/* *INDENT-OFF* */
773VLIB_REGISTER_NODE (ip6_sv_reass_node) = {
774 .name = "ip6-sv-reassembly",
775 .vector_size = sizeof (u32),
776 .format_trace = format_ip6_sv_reass_trace,
777 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
778 .error_strings = ip6_sv_reassembly_error_strings,
779 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
780 .next_nodes =
781 {
782 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
783 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
784 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
785 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff",
786 },
787};
788/* *INDENT-ON* */
789
790VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
791 vlib_node_runtime_t * node,
792 vlib_frame_t * frame)
793{
794 return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */ );
795}
796
797/* *INDENT-OFF* */
798VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = {
799 .name = "ip6-sv-reassembly-feature",
800 .vector_size = sizeof (u32),
801 .format_trace = format_ip6_sv_reass_trace,
802 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
803 .error_strings = ip6_sv_reassembly_error_strings,
804 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
805 .next_nodes =
806 {
807 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
808 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
809 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
810 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff",
811 },
812};
813/* *INDENT-ON* */
814
815/* *INDENT-OFF* */
816VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = {
817 .arc_name = "ip6-unicast",
818 .node_name = "ip6-sv-reassembly-feature",
819 .runs_before = VNET_FEATURES ("ip6-lookup"),
820 .runs_after = 0,
821};
822/* *INDENT-ON* */
823
824#ifndef CLIB_MARCH_VARIANT
825static u32
826ip6_sv_reass_get_nbuckets ()
827{
828 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
829 u32 nbuckets;
830 u8 i;
831
832 nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR);
833
834 for (i = 0; i < 31; i++)
835 if ((1 << i) >= nbuckets)
836 break;
837 nbuckets = 1 << i;
838
839 return nbuckets;
840}
841#endif /* CLIB_MARCH_VARIANT */
842
843typedef enum
844{
845 IP6_EVENT_CONFIG_CHANGED = 1,
846} ip6_sv_reass_event_t;
847
848#ifndef CLIB_MARCH_VARIANT
849typedef struct
850{
851 int failure;
852 clib_bihash_48_8_t *new_hash;
853} ip6_rehash_cb_ctx;
854
Neale Rannsf50bac12019-12-06 05:53:17 +0000855static int
Klement Sekerade34c352019-06-25 11:19:22 +0000856ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
857{
858 ip6_rehash_cb_ctx *ctx = _ctx;
859 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
860 {
861 ctx->failure = 1;
862 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000863 return (BIHASH_WALK_CONTINUE);
Klement Sekerade34c352019-06-25 11:19:22 +0000864}
865
866static void
867ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
868 u32 max_reassembly_length,
869 u32 expire_walk_interval_ms)
870{
871 ip6_sv_reass_main.timeout_ms = timeout_ms;
872 ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
873 ip6_sv_reass_main.max_reass_n = max_reassemblies;
874 ip6_sv_reass_main.max_reass_len = max_reassembly_length;
875 ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
876}
877
878vnet_api_error_t
879ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
880 u32 max_reassembly_length, u32 expire_walk_interval_ms)
881{
882 u32 old_nbuckets = ip6_sv_reass_get_nbuckets ();
883 ip6_sv_reass_set_params (timeout_ms, max_reassemblies,
884 max_reassembly_length, expire_walk_interval_ms);
885 vlib_process_signal_event (ip6_sv_reass_main.vlib_main,
886 ip6_sv_reass_main.ip6_sv_reass_expire_node_idx,
887 IP6_EVENT_CONFIG_CHANGED, 0);
888 u32 new_nbuckets = ip6_sv_reass_get_nbuckets ();
889 if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
890 {
891 clib_bihash_48_8_t new_hash;
892 clib_memset (&new_hash, 0, sizeof (new_hash));
893 ip6_rehash_cb_ctx ctx;
894 ctx.failure = 0;
895 ctx.new_hash = &new_hash;
896 clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets,
897 new_nbuckets * 1024);
898 clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash,
899 ip6_rehash_cb, &ctx);
900 if (ctx.failure)
901 {
902 clib_bihash_free_48_8 (&new_hash);
903 return -1;
904 }
905 else
906 {
907 clib_bihash_free_48_8 (&ip6_sv_reass_main.hash);
908 clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash,
909 sizeof (ip6_sv_reass_main.hash));
910 clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash);
911 }
912 }
913 return 0;
914}
915
916vnet_api_error_t
917ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
918 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
919{
920 *timeout_ms = ip6_sv_reass_main.timeout_ms;
921 *max_reassemblies = ip6_sv_reass_main.max_reass_n;
922 *max_reassembly_length = ip6_sv_reass_main.max_reass_len;
923 *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms;
924 return 0;
925}
926
927static clib_error_t *
928ip6_sv_reass_init_function (vlib_main_t * vm)
929{
930 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
931 clib_error_t *error = 0;
932 u32 nbuckets;
933 vlib_node_t *node;
934
935 rm->vlib_main = vm;
936 rm->vnet_main = vnet_get_main ();
937
938 vec_validate (rm->per_thread_data, vlib_num_workers ());
939 ip6_sv_reass_per_thread_t *rt;
940 vec_foreach (rt, rm->per_thread_data)
941 {
942 clib_spinlock_init (&rt->lock);
943 pool_alloc (rt->pool, rm->max_reass_n);
944 rt->lru_first = rt->lru_last = ~0;
945 }
946
947 node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk");
948 ASSERT (node);
949 rm->ip6_sv_reass_expire_node_idx = node->index;
950
951 ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS,
952 IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
953 IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
954 IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
955
956 nbuckets = ip6_sv_reass_get_nbuckets ();
957 clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets,
958 nbuckets * 1024);
959
960 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
961 ASSERT (node);
962 rm->ip6_drop_idx = node->index;
963 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
964 ASSERT (node);
965 rm->ip6_icmp_error_idx = node->index;
966
967 if ((error = vlib_call_init_function (vm, ip_main_init)))
968 return error;
Klement Sekerade34c352019-06-25 11:19:22 +0000969
970 rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0);
971 rm->fq_feature_index =
972 vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0);
973
Klement Sekera63c73532019-09-30 14:35:36 +0000974 rm->feature_use_refcount_per_intf = NULL;
975
Klement Sekerade34c352019-06-25 11:19:22 +0000976 return error;
977}
978
979VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
980#endif /* CLIB_MARCH_VARIANT */
981
982static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +0200983ip6_sv_reass_walk_expired (vlib_main_t *vm,
984 CLIB_UNUSED (vlib_node_runtime_t *node),
985 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekerade34c352019-06-25 11:19:22 +0000986{
987 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
988 uword event_type, *event_data = 0;
989
990 while (true)
991 {
992 vlib_process_wait_for_event_or_clock (vm,
993 (f64) rm->expire_walk_interval_ms
994 / (f64) MSEC_PER_SEC);
995 event_type = vlib_process_get_events (vm, &event_data);
996
997 switch (event_type)
998 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200999 case ~0:
1000 /* no events => timeout */
1001 /* fallthrough */
Klement Sekerade34c352019-06-25 11:19:22 +00001002 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001003 /* nothing to do here */
Klement Sekerade34c352019-06-25 11:19:22 +00001004 break;
1005 default:
1006 clib_warning ("BUG: event type 0x%wx", event_type);
1007 break;
1008 }
1009 f64 now = vlib_time_now (vm);
1010
1011 ip6_sv_reass_t *reass;
1012 int *pool_indexes_to_free = NULL;
1013
1014 uword thread_index = 0;
1015 int index;
1016 const uword nthreads = vlib_num_workers () + 1;
Klement Sekerade34c352019-06-25 11:19:22 +00001017 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1018 {
1019 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1020 clib_spinlock_lock (&rt->lock);
1021
1022 vec_reset_length (pool_indexes_to_free);
1023 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001024 pool_foreach_index (index, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001025 reass = pool_elt_at_index (rt->pool, index);
1026 if (now > reass->last_heard + rm->timeout)
1027 {
1028 vec_add1 (pool_indexes_to_free, index);
1029 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001030 }
Klement Sekerade34c352019-06-25 11:19:22 +00001031 /* *INDENT-ON* */
1032 int *i;
1033 /* *INDENT-OFF* */
1034 vec_foreach (i, pool_indexes_to_free)
1035 {
1036 ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1037 ip6_sv_reass_free (vm, rm, rt, reass);
1038 }
1039 /* *INDENT-ON* */
1040
1041 clib_spinlock_unlock (&rt->lock);
1042 }
1043
Klement Sekerade34c352019-06-25 11:19:22 +00001044 vec_free (pool_indexes_to_free);
Klement Sekerade34c352019-06-25 11:19:22 +00001045 if (event_data)
1046 {
1047 _vec_len (event_data) = 0;
1048 }
1049 }
1050
1051 return 0;
1052}
1053
1054/* *INDENT-OFF* */
1055VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
1056 .function = ip6_sv_reass_walk_expired,
1057 .format_trace = format_ip6_sv_reass_trace,
1058 .type = VLIB_NODE_TYPE_PROCESS,
1059 .name = "ip6-sv-reassembly-expire-walk",
1060
1061 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
1062 .error_strings = ip6_sv_reassembly_error_strings,
1063
1064};
1065/* *INDENT-ON* */
1066
1067static u8 *
1068format_ip6_sv_reass_key (u8 * s, va_list * args)
1069{
1070 ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
Klement Sekera5a58e7c2021-11-03 17:57:28 +01001071 s =
1072 format (s, "fib_index: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1073 key->fib_index, format_ip6_address, &key->src, format_ip6_address,
1074 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
Klement Sekerade34c352019-06-25 11:19:22 +00001075 return s;
1076}
1077
1078static u8 *
1079format_ip6_sv_reass (u8 * s, va_list * args)
1080{
1081 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1082 ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1083
1084 s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1085 reass->id, format_ip6_sv_reass_key, &reass->key,
1086 reass->trace_op_counter);
1087 vlib_buffer_t *b;
1088 u32 *bip;
1089 u32 counter = 0;
1090 vec_foreach (bip, reass->cached_buffers)
1091 {
1092 u32 bi = *bip;
1093 do
1094 {
1095 b = vlib_get_buffer (vm, bi);
1096 s = format (s, " #%03u: bi: %u\n", counter, bi);
1097 ++counter;
1098 bi = b->next_buffer;
1099 }
1100 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1101 }
1102 return s;
1103}
1104
1105static clib_error_t *
1106show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1107 CLIB_UNUSED (vlib_cli_command_t * lmd))
1108{
1109 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1110
1111 vlib_cli_output (vm, "---------------------");
1112 vlib_cli_output (vm, "IP6 reassembly status");
1113 vlib_cli_output (vm, "---------------------");
1114 bool details = false;
1115 if (unformat (input, "details"))
1116 {
1117 details = true;
1118 }
1119
1120 u32 sum_reass_n = 0;
1121 u64 sum_buffers_n = 0;
1122 ip6_sv_reass_t *reass;
1123 uword thread_index;
1124 const uword nthreads = vlib_num_workers () + 1;
1125 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1126 {
1127 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1128 clib_spinlock_lock (&rt->lock);
1129 if (details)
1130 {
1131 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001132 pool_foreach (reass, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001133 vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001134 }
Klement Sekerade34c352019-06-25 11:19:22 +00001135 /* *INDENT-ON* */
1136 }
1137 sum_reass_n += rt->reass_n;
1138 clib_spinlock_unlock (&rt->lock);
1139 }
1140 vlib_cli_output (vm, "---------------------");
1141 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1142 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001143 vlib_cli_output (vm,
1144 "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n",
1145 (long unsigned) rm->max_reass_n);
1146 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001147 "Maximum configured amount of fragments per shallow "
1148 "virtual IP6 reassembly: %lu\n",
1149 (long unsigned) rm->max_reass_len);
1150 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001151 "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n",
1152 (long unsigned) rm->timeout_ms);
1153 vlib_cli_output (vm,
1154 "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n",
1155 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekerade34c352019-06-25 11:19:22 +00001156 vlib_cli_output (vm, "Buffers in use: %lu\n",
1157 (long unsigned) sum_buffers_n);
1158 return 0;
1159}
1160
1161/* *INDENT-OFF* */
1162VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1163 .path = "show ip6-sv-reassembly",
1164 .short_help = "show ip6-sv-reassembly [details]",
1165 .function = show_ip6_sv_reass,
1166};
1167/* *INDENT-ON* */
1168
1169#ifndef CLIB_MARCH_VARIANT
1170vnet_api_error_t
1171ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1172{
Klement Sekeraf126e742019-10-10 09:46:06 +00001173 return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1174 enable_disable);
Klement Sekerade34c352019-06-25 11:19:22 +00001175}
1176#endif /* CLIB_MARCH_VARIANT */
1177
1178#define foreach_ip6_sv_reassembly_handoff_error \
1179_(CONGESTION_DROP, "congestion drop")
1180
1181
1182typedef enum
1183{
1184#define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1185 foreach_ip6_sv_reassembly_handoff_error
1186#undef _
1187 IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1188} ip6_sv_reassembly_handoff_error_t;
1189
1190static char *ip6_sv_reassembly_handoff_error_strings[] = {
1191#define _(sym,string) string,
1192 foreach_ip6_sv_reassembly_handoff_error
1193#undef _
1194};
1195
1196typedef struct
1197{
1198 u32 next_worker_index;
1199} ip6_sv_reassembly_handoff_trace_t;
1200
1201static u8 *
1202format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1203{
1204 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1205 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1206 ip6_sv_reassembly_handoff_trace_t *t =
1207 va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1208
1209 s =
1210 format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1211 t->next_worker_index);
1212
1213 return s;
1214}
1215
1216always_inline uword
1217ip6_sv_reassembly_handoff_inline (vlib_main_t * vm,
1218 vlib_node_runtime_t * node,
1219 vlib_frame_t * frame, bool is_feature)
1220{
1221 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1222
1223 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1224 u32 n_enq, n_left_from, *from;
1225 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1226 u32 fq_index;
1227
1228 from = vlib_frame_vector_args (frame);
1229 n_left_from = frame->n_vectors;
1230 vlib_get_buffers (vm, from, bufs, n_left_from);
1231
1232 b = bufs;
1233 ti = thread_indices;
1234
1235 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1236
1237 while (n_left_from > 0)
1238 {
1239 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1240
1241 if (PREDICT_FALSE
1242 ((node->flags & VLIB_NODE_FLAG_TRACE)
1243 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1244 {
1245 ip6_sv_reassembly_handoff_trace_t *t =
1246 vlib_add_trace (vm, node, b[0], sizeof (*t));
1247 t->next_worker_index = ti[0];
1248 }
1249
1250 n_left_from -= 1;
1251 ti += 1;
1252 b += 1;
1253 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001254 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1255 thread_indices, frame->n_vectors, 1);
Klement Sekerade34c352019-06-25 11:19:22 +00001256
1257 if (n_enq < frame->n_vectors)
1258 vlib_node_increment_counter (vm, node->node_index,
1259 IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1260 frame->n_vectors - n_enq);
1261 return frame->n_vectors;
1262}
1263
1264VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1265 vlib_node_runtime_t * node,
1266 vlib_frame_t * frame)
1267{
1268 return ip6_sv_reassembly_handoff_inline (vm, node, frame,
1269 false /* is_feature */ );
1270}
1271
1272/* *INDENT-OFF* */
1273VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1274 .name = "ip6-sv-reassembly-handoff",
1275 .vector_size = sizeof (u32),
1276 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1277 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1278 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1279
1280 .n_next_nodes = 1,
1281
1282 .next_nodes = {
1283 [0] = "error-drop",
1284 },
1285};
1286
1287
1288VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1289 vlib_node_runtime_t * node, vlib_frame_t * frame)
1290{
1291 return ip6_sv_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1292}
1293
1294
1295/* *INDENT-OFF* */
1296VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1297 .name = "ip6-sv-reass-feature-hoff",
1298 .vector_size = sizeof (u32),
1299 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1300 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1301 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1302
1303 .n_next_nodes = 1,
1304
1305 .next_nodes = {
1306 [0] = "error-drop",
1307 },
1308};
1309/* *INDENT-ON* */
1310
1311#ifndef CLIB_MARCH_VARIANT
1312int
1313ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1314{
1315 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
Klement Sekera63c73532019-09-30 14:35:36 +00001316 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001317 if (is_enable)
1318 {
Klement Sekera63c73532019-09-30 14:35:36 +00001319 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001320 {
Klement Sekera63c73532019-09-30 14:35:36 +00001321 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001322 return vnet_feature_enable_disable ("ip6-unicast",
1323 "ip6-sv-reassembly-feature",
1324 sw_if_index, 1, 0, 0);
1325 }
Klement Sekera63c73532019-09-30 14:35:36 +00001326 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001327 }
1328 else
1329 {
Klement Sekera63c73532019-09-30 14:35:36 +00001330 --rm->feature_use_refcount_per_intf[sw_if_index];
1331 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001332 return vnet_feature_enable_disable ("ip6-unicast",
1333 "ip6-sv-reassembly-feature",
1334 sw_if_index, 0, 0, 0);
1335 }
Klement Sekera407f5932019-12-11 13:06:27 +00001336 return 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001337}
1338#endif
1339
1340/*
1341 * fd.io coding-style-patch-verification: ON
1342 *
1343 * Local Variables:
1344 * eval: (c-set-style "gnu")
1345 * End:
1346 */