Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Reassembly. |
| 19 | * |
| 20 | * This file contains the source code for IPv6 reassembly. |
| 21 | */ |
| 22 | |
| 23 | #include <vppinfra/vec.h> |
| 24 | #include <vnet/vnet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vppinfra/bihash_48_8.h> |
| 27 | #include <vnet/ip/ip6_reassembly.h> |
| 28 | |
| 29 | #define MSEC_PER_SEC 1000 |
| 30 | #define IP6_REASS_TIMEOUT_DEFAULT_MS 100 |
| 31 | #define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 32 | #define IP6_REASS_MAX_REASSEMBLIES_DEFAULT 1024 |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 33 | #define IP6_REASS_HT_LOAD_FACTOR (0.75) |
| 34 | |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 35 | typedef enum |
| 36 | { |
| 37 | IP6_REASS_RC_OK, |
| 38 | IP6_REASS_RC_INTERNAL_ERROR, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 39 | IP6_REASS_RC_NO_BUF, |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 40 | } ip6_reass_rc_t; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 41 | |
| 42 | typedef struct |
| 43 | { |
| 44 | union |
| 45 | { |
| 46 | struct |
| 47 | { |
| 48 | ip6_address_t src; |
| 49 | ip6_address_t dst; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 50 | u32 xx_id; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 51 | u32 frag_id; |
Klement Sekera | 8dcfed5 | 2018-06-28 11:16:15 +0200 | [diff] [blame] | 52 | u8 unused[7]; |
| 53 | u8 proto; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 54 | }; |
| 55 | u64 as_u64[6]; |
| 56 | }; |
| 57 | } ip6_reass_key_t; |
| 58 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 59 | typedef union |
| 60 | { |
| 61 | struct |
| 62 | { |
| 63 | u32 reass_index; |
| 64 | u32 thread_index; |
| 65 | }; |
| 66 | u64 as_u64; |
| 67 | } ip6_reass_val_t; |
| 68 | |
| 69 | typedef union |
| 70 | { |
| 71 | struct |
| 72 | { |
| 73 | ip6_reass_key_t k; |
| 74 | ip6_reass_val_t v; |
| 75 | }; |
| 76 | clib_bihash_kv_48_8_t kv; |
| 77 | } ip6_reass_kv_t; |
| 78 | |
| 79 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 80 | always_inline u32 |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 81 | ip6_reass_buffer_get_data_offset (vlib_buffer_t * b) |
| 82 | { |
| 83 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 84 | return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | always_inline u16 |
| 88 | ip6_reass_buffer_get_data_len (vlib_buffer_t * b) |
| 89 | { |
| 90 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 91 | return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) - |
| 92 | (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | typedef struct |
| 96 | { |
| 97 | // hash table key |
| 98 | ip6_reass_key_t key; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 99 | // time when last packet was received |
| 100 | f64 last_heard; |
| 101 | // internal id of this reassembly |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 102 | u64 id; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 103 | // buffer index of first buffer in this reassembly context |
| 104 | u32 first_bi; |
| 105 | // last octet of packet, ~0 until fragment without more_fragments arrives |
| 106 | u32 last_packet_octet; |
| 107 | // length of data collected so far |
| 108 | u32 data_len; |
| 109 | // trace operation counter |
| 110 | u32 trace_op_counter; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 111 | // next index - used by non-feature node |
| 112 | u8 next_index; |
| 113 | // minimum fragment length for this reassembly - used to estimate MTU |
| 114 | u16 min_fragment_length; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 115 | } ip6_reass_t; |
| 116 | |
| 117 | typedef struct |
| 118 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 119 | ip6_reass_t *pool; |
| 120 | u32 reass_n; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 121 | u32 id_counter; |
| 122 | clib_spinlock_t lock; |
| 123 | } ip6_reass_per_thread_t; |
| 124 | |
| 125 | typedef struct |
| 126 | { |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 127 | // IPv6 config |
| 128 | u32 timeout_ms; |
| 129 | f64 timeout; |
| 130 | u32 expire_walk_interval_ms; |
| 131 | u32 max_reass_n; |
| 132 | |
| 133 | // IPv6 runtime |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 134 | clib_bihash_48_8_t hash; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 135 | |
| 136 | // per-thread data |
| 137 | ip6_reass_per_thread_t *per_thread_data; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 138 | |
| 139 | // convenience |
| 140 | vlib_main_t *vlib_main; |
| 141 | vnet_main_t *vnet_main; |
| 142 | |
| 143 | // node index of ip6-drop node |
| 144 | u32 ip6_drop_idx; |
| 145 | u32 ip6_icmp_error_idx; |
| 146 | u32 ip6_reass_expire_node_idx; |
| 147 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 148 | /** Worker handoff */ |
| 149 | u32 fq_index; |
| 150 | u32 fq_feature_index; |
| 151 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 152 | } ip6_reass_main_t; |
| 153 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 154 | extern ip6_reass_main_t ip6_reass_main; |
| 155 | |
| 156 | #ifndef CLIB_MARCH_VARIANT |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 157 | ip6_reass_main_t ip6_reass_main; |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 158 | #endif /* CLIB_MARCH_VARIANT */ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 159 | |
| 160 | typedef enum |
| 161 | { |
| 162 | IP6_REASSEMBLY_NEXT_INPUT, |
| 163 | IP6_REASSEMBLY_NEXT_DROP, |
| 164 | IP6_REASSEMBLY_NEXT_ICMP_ERROR, |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 165 | IP6_REASSEMBLY_NEXT_HANDOFF, |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 166 | IP6_REASSEMBLY_N_NEXT, |
| 167 | } ip6_reass_next_t; |
| 168 | |
| 169 | typedef enum |
| 170 | { |
| 171 | RANGE_NEW, |
| 172 | RANGE_OVERLAP, |
| 173 | ICMP_ERROR_RT_EXCEEDED, |
| 174 | ICMP_ERROR_FL_TOO_BIG, |
| 175 | ICMP_ERROR_FL_NOT_MULT_8, |
| 176 | FINALIZE, |
| 177 | } ip6_reass_trace_operation_e; |
| 178 | |
| 179 | typedef struct |
| 180 | { |
| 181 | u16 range_first; |
| 182 | u16 range_last; |
| 183 | u32 range_bi; |
| 184 | i32 data_offset; |
| 185 | u32 data_len; |
| 186 | u32 first_bi; |
| 187 | } ip6_reass_range_trace_t; |
| 188 | |
| 189 | typedef struct |
| 190 | { |
| 191 | ip6_reass_trace_operation_e action; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 192 | u32 reass_id; |
| 193 | ip6_reass_range_trace_t trace_range; |
| 194 | u32 size_diff; |
| 195 | u32 op_id; |
| 196 | u32 fragment_first; |
| 197 | u32 fragment_last; |
| 198 | u32 total_data_len; |
| 199 | } ip6_reass_trace_t; |
| 200 | |
| 201 | static void |
| 202 | ip6_reass_trace_details (vlib_main_t * vm, u32 bi, |
| 203 | ip6_reass_range_trace_t * trace) |
| 204 | { |
| 205 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 206 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 207 | trace->range_first = vnb->ip.reass.range_first; |
| 208 | trace->range_last = vnb->ip.reass.range_last; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 209 | trace->data_offset = ip6_reass_buffer_get_data_offset (b); |
| 210 | trace->data_len = ip6_reass_buffer_get_data_len (b); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 211 | trace->range_bi = bi; |
| 212 | } |
| 213 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 214 | static u8 * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 215 | format_ip6_reass_range_trace (u8 * s, va_list * args) |
| 216 | { |
| 217 | ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *); |
| 218 | s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first, |
| 219 | trace->range_last, trace->data_offset, trace->data_len, |
| 220 | trace->range_bi); |
| 221 | return s; |
| 222 | } |
| 223 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 224 | static u8 * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 225 | format_ip6_reass_trace (u8 * s, va_list * args) |
| 226 | { |
| 227 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 228 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 229 | ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *); |
| 230 | s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id); |
| 231 | u32 indent = format_get_indent (s); |
| 232 | s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]", |
| 233 | t->trace_range.first_bi, t->total_data_len, t->fragment_first, |
| 234 | t->fragment_last); |
| 235 | switch (t->action) |
| 236 | { |
| 237 | case RANGE_NEW: |
| 238 | s = format (s, "\n%Unew %U", format_white_space, indent, |
| 239 | format_ip6_reass_range_trace, &t->trace_range); |
| 240 | break; |
| 241 | case RANGE_OVERLAP: |
| 242 | s = format (s, "\n%Uoverlap %U", format_white_space, indent, |
| 243 | format_ip6_reass_range_trace, &t->trace_range); |
| 244 | break; |
| 245 | case ICMP_ERROR_FL_TOO_BIG: |
| 246 | s = format (s, "\n%Uicmp-error - frag_len > 65535 %U", |
| 247 | format_white_space, indent, format_ip6_reass_range_trace, |
| 248 | &t->trace_range); |
| 249 | break; |
| 250 | case ICMP_ERROR_FL_NOT_MULT_8: |
| 251 | s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U", |
| 252 | format_white_space, indent, format_ip6_reass_range_trace, |
| 253 | &t->trace_range); |
| 254 | break; |
| 255 | case ICMP_ERROR_RT_EXCEEDED: |
| 256 | s = format (s, "\n%Uicmp-error - reassembly time exceeded", |
| 257 | format_white_space, indent); |
| 258 | break; |
| 259 | case FINALIZE: |
| 260 | s = format (s, "\n%Ufinalize reassembly", format_white_space, indent); |
| 261 | break; |
| 262 | } |
| 263 | return s; |
| 264 | } |
| 265 | |
| 266 | static void |
| 267 | ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 268 | ip6_reass_main_t * rm, ip6_reass_t * reass, |
| 269 | u32 bi, ip6_reass_trace_operation_e action, |
| 270 | u32 size_diff) |
| 271 | { |
| 272 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 273 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 274 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index)) |
| 275 | { |
| 276 | // this buffer's trace is gone |
| 277 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 278 | return; |
| 279 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 280 | ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 281 | t->reass_id = reass->id; |
| 282 | t->action = action; |
| 283 | ip6_reass_trace_details (vm, bi, &t->trace_range); |
| 284 | t->size_diff = size_diff; |
| 285 | t->op_id = reass->trace_op_counter; |
| 286 | ++reass->trace_op_counter; |
| 287 | t->fragment_first = vnb->ip.reass.fragment_first; |
| 288 | t->fragment_last = vnb->ip.reass.fragment_last; |
| 289 | t->trace_range.first_bi = reass->first_bi; |
| 290 | t->total_data_len = reass->data_len; |
| 291 | #if 0 |
| 292 | static u8 *s = NULL; |
| 293 | s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t); |
| 294 | printf ("%.*s\n", vec_len (s), s); |
| 295 | fflush (stdout); |
| 296 | vec_reset_length (s); |
| 297 | #endif |
| 298 | } |
| 299 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 300 | always_inline void |
| 301 | ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
| 302 | ip6_reass_t * reass) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 303 | { |
| 304 | clib_bihash_kv_48_8_t kv; |
| 305 | kv.key[0] = reass->key.as_u64[0]; |
| 306 | kv.key[1] = reass->key.as_u64[1]; |
| 307 | kv.key[2] = reass->key.as_u64[2]; |
| 308 | kv.key[3] = reass->key.as_u64[3]; |
| 309 | kv.key[4] = reass->key.as_u64[4]; |
| 310 | kv.key[5] = reass->key.as_u64[5]; |
| 311 | clib_bihash_add_del_48_8 (&rm->hash, &kv, 0); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 312 | pool_put (rt->pool, reass); |
| 313 | --rt->reass_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 316 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 317 | ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 318 | ip6_reass_t * reass) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 319 | { |
| 320 | u32 range_bi = reass->first_bi; |
| 321 | vlib_buffer_t *range_b; |
| 322 | vnet_buffer_opaque_t *range_vnb; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 323 | u32 *to_free = NULL; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 324 | while (~0 != range_bi) |
| 325 | { |
| 326 | range_b = vlib_get_buffer (vm, range_bi); |
| 327 | range_vnb = vnet_buffer (range_b); |
| 328 | u32 bi = range_bi; |
| 329 | while (~0 != bi) |
| 330 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 331 | vec_add1 (to_free, bi); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 332 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 333 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 334 | { |
| 335 | bi = b->next_buffer; |
| 336 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | bi = ~0; |
| 341 | } |
| 342 | } |
| 343 | range_bi = range_vnb->ip.reass.next_range_bi; |
| 344 | } |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 345 | vlib_buffer_free (vm, to_free, vec_len (to_free)); |
| 346 | vec_free (to_free); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 349 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 350 | ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 351 | ip6_reass_main_t * rm, ip6_reass_t * reass, |
| 352 | u32 * icmp_bi) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 353 | { |
| 354 | if (~0 == reass->first_bi) |
| 355 | { |
| 356 | return; |
| 357 | } |
| 358 | vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi); |
| 359 | if (0 == vnet_buffer (b)->ip.reass.fragment_first) |
| 360 | { |
| 361 | *icmp_bi = reass->first_bi; |
| 362 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 363 | { |
| 364 | ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, |
| 365 | ICMP_ERROR_RT_EXCEEDED, 0); |
| 366 | } |
| 367 | // fragment with offset zero received - send icmp message back |
| 368 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 369 | { |
| 370 | // separate first buffer from chain and steer it towards icmp node |
| 371 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 372 | reass->first_bi = b->next_buffer; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi; |
| 377 | } |
| 378 | icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded, |
| 379 | ICMP6_time_exceeded_fragment_reassembly_time_exceeded, |
| 380 | 0); |
| 381 | } |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 382 | ip6_reass_drop_all (vm, rm, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 385 | always_inline ip6_reass_t * |
| 386 | ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 387 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 388 | ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 389 | { |
| 390 | ip6_reass_t *reass = NULL; |
| 391 | f64 now = vlib_time_now (rm->vlib_main); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 392 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 393 | if (!clib_bihash_search_48_8 |
| 394 | (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv)) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 395 | { |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 396 | if (vm->thread_index != kv->v.thread_index) |
| 397 | { |
| 398 | *do_handoff = 1; |
| 399 | return NULL; |
| 400 | } |
| 401 | reass = pool_elt_at_index (rt->pool, kv->v.reass_index); |
| 402 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 403 | if (now > reass->last_heard + rm->timeout) |
| 404 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 405 | ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 406 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 407 | reass = NULL; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (reass) |
| 412 | { |
| 413 | reass->last_heard = now; |
| 414 | return reass; |
| 415 | } |
| 416 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 417 | if (rt->reass_n >= rm->max_reass_n) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 418 | { |
| 419 | reass = NULL; |
| 420 | return reass; |
| 421 | } |
| 422 | else |
| 423 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 424 | pool_get (rt->pool, reass); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 425 | clib_memset (reass, 0, sizeof (*reass)); |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 426 | reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 427 | ++rt->id_counter; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 428 | reass->first_bi = ~0; |
| 429 | reass->last_packet_octet = ~0; |
| 430 | reass->data_len = 0; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 431 | ++rt->reass_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 434 | reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0]; |
| 435 | reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1]; |
| 436 | reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2]; |
| 437 | reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3]; |
| 438 | reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4]; |
| 439 | reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5]; |
| 440 | kv->v.reass_index = (reass - rt->pool); |
| 441 | kv->v.thread_index = vm->thread_index; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 442 | reass->last_heard = now; |
| 443 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 444 | if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1)) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 445 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 446 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 447 | reass = NULL; |
| 448 | } |
| 449 | |
| 450 | return reass; |
| 451 | } |
| 452 | |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 453 | always_inline ip6_reass_rc_t |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 454 | ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 455 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 456 | ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0, |
| 457 | bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 458 | { |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 459 | *bi0 = reass->first_bi; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 460 | *error0 = IP6_ERROR_NONE; |
| 461 | ip6_frag_hdr_t *frag_hdr; |
| 462 | vlib_buffer_t *last_b = NULL; |
| 463 | u32 sub_chain_bi = reass->first_bi; |
| 464 | u32 total_length = 0; |
| 465 | u32 buf_cnt = 0; |
| 466 | u32 dropped_cnt = 0; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 467 | u32 *vec_drop_compress = NULL; |
| 468 | ip6_reass_rc_t rv = IP6_REASS_RC_OK; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 469 | do |
| 470 | { |
| 471 | u32 tmp_bi = sub_chain_bi; |
| 472 | vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 473 | vnet_buffer_opaque_t *vnb = vnet_buffer (tmp); |
| 474 | if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) && |
| 475 | !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first)) |
| 476 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 477 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 478 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 481 | u32 data_len = ip6_reass_buffer_get_data_len (tmp); |
| 482 | u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset + |
| 483 | sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp); |
| 484 | u32 trim_end = |
| 485 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len; |
| 486 | if (tmp_bi == reass->first_bi) |
| 487 | { |
| 488 | /* first buffer - keep ip6 header */ |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 489 | if (0 != ip6_reass_buffer_get_data_offset (tmp)) |
| 490 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 491 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 492 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 493 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 494 | trim_front = 0; |
| 495 | trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len - |
| 496 | (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset + |
| 497 | sizeof (*frag_hdr)); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 498 | if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0)) |
| 499 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 500 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 501 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 502 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 503 | } |
| 504 | u32 keep_data = |
| 505 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end; |
| 506 | while (1) |
| 507 | { |
| 508 | ++buf_cnt; |
| 509 | if (trim_front) |
| 510 | { |
| 511 | if (trim_front > tmp->current_length) |
| 512 | { |
| 513 | /* drop whole buffer */ |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 514 | vec_add1 (vec_drop_compress, tmp_bi); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 515 | trim_front -= tmp->current_length; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 516 | if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 517 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 518 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 519 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 520 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 521 | tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 522 | tmp_bi = tmp->next_buffer; |
| 523 | tmp = vlib_get_buffer (vm, tmp_bi); |
| 524 | continue; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | vlib_buffer_advance (tmp, trim_front); |
| 529 | trim_front = 0; |
| 530 | } |
| 531 | } |
| 532 | if (keep_data) |
| 533 | { |
| 534 | if (last_b) |
| 535 | { |
| 536 | last_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 537 | last_b->next_buffer = tmp_bi; |
| 538 | } |
| 539 | last_b = tmp; |
| 540 | if (keep_data <= tmp->current_length) |
| 541 | { |
| 542 | tmp->current_length = keep_data; |
| 543 | keep_data = 0; |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | keep_data -= tmp->current_length; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 548 | if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 549 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 550 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 551 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 552 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 553 | } |
| 554 | total_length += tmp->current_length; |
| 555 | } |
| 556 | else |
| 557 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 558 | vec_add1 (vec_drop_compress, tmp_bi); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 559 | if (reass->first_bi == tmp_bi) |
| 560 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 561 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 562 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 563 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 564 | ++dropped_cnt; |
| 565 | } |
| 566 | if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 567 | { |
| 568 | tmp_bi = tmp->next_buffer; |
| 569 | tmp = vlib_get_buffer (vm, tmp->next_buffer); |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | sub_chain_bi = |
| 577 | vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip. |
| 578 | reass.next_range_bi; |
| 579 | } |
| 580 | while (~0 != sub_chain_bi); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 581 | |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 582 | if (!last_b) |
| 583 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 584 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 585 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 586 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 587 | last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 588 | vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 589 | if (total_length < first_b->current_length) |
| 590 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 591 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 592 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 593 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 594 | total_length -= first_b->current_length; |
| 595 | first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 596 | first_b->total_length_not_including_first_buffer = total_length; |
| 597 | // drop fragment header |
| 598 | vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b); |
| 599 | ip6_header_t *ip = vlib_buffer_get_current (first_b); |
| 600 | u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset; |
| 601 | ip6_ext_header_t *prev_hdr; |
| 602 | ip6_ext_header_find_t (ip, prev_hdr, frag_hdr, |
| 603 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 604 | if (prev_hdr) |
| 605 | { |
| 606 | prev_hdr->next_hdr = frag_hdr->next_hdr; |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | ip->protocol = frag_hdr->next_hdr; |
| 611 | } |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 612 | if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset)) |
| 613 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 614 | rv = IP6_REASS_RC_INTERNAL_ERROR; |
| 615 | goto free_buffers_and_return; |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 616 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 617 | memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr), |
| 618 | first_b->current_length - ip6_frag_hdr_offset - |
| 619 | sizeof (ip6_frag_hdr_t)); |
| 620 | first_b->current_length -= sizeof (*frag_hdr); |
| 621 | ip->payload_length = |
| 622 | clib_host_to_net_u16 (total_length + first_b->current_length - |
| 623 | sizeof (*ip)); |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 624 | if (!vlib_buffer_chain_linearize (vm, first_b)) |
| 625 | { |
| 626 | rv = IP6_REASS_RC_NO_BUF; |
| 627 | goto free_buffers_and_return; |
| 628 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 629 | if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 630 | { |
| 631 | ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0); |
| 632 | #if 0 |
| 633 | // following code does a hexdump of packet fragments to stdout ... |
| 634 | do |
| 635 | { |
| 636 | u32 bi = reass->first_bi; |
| 637 | u8 *s = NULL; |
| 638 | while (~0 != bi) |
| 639 | { |
| 640 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 641 | s = format (s, "%u: %U\n", bi, format_hexdump, |
| 642 | vlib_buffer_get_current (b), b->current_length); |
| 643 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 644 | { |
| 645 | bi = b->next_buffer; |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | printf ("%.*s\n", vec_len (s), s); |
| 653 | fflush (stdout); |
| 654 | vec_free (s); |
| 655 | } |
| 656 | while (0); |
| 657 | #endif |
| 658 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 659 | if (is_feature) |
| 660 | { |
| 661 | *next0 = IP6_REASSEMBLY_NEXT_INPUT; |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | *next0 = reass->next_index; |
| 666 | } |
| 667 | vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length; |
| 668 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 669 | reass = NULL; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 670 | free_buffers_and_return: |
| 671 | vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress)); |
| 672 | vec_free (vec_drop_compress); |
| 673 | return rv; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 676 | always_inline void |
| 677 | ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm, |
| 678 | ip6_reass_per_thread_t * rt, |
| 679 | ip6_reass_t * reass, u32 prev_range_bi, |
| 680 | u32 new_next_bi) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 681 | { |
| 682 | |
| 683 | vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi); |
| 684 | vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b); |
| 685 | if (~0 != prev_range_bi) |
| 686 | { |
| 687 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 688 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 689 | new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi; |
| 690 | prev_vnb->ip.reass.next_range_bi = new_next_bi; |
| 691 | } |
| 692 | else |
| 693 | { |
| 694 | if (~0 != reass->first_bi) |
| 695 | { |
| 696 | new_next_vnb->ip.reass.next_range_bi = reass->first_bi; |
| 697 | } |
| 698 | reass->first_bi = new_next_bi; |
| 699 | } |
| 700 | reass->data_len += ip6_reass_buffer_get_data_len (new_next_b); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 703 | always_inline ip6_reass_rc_t |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 704 | ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 705 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 706 | ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0, |
| 707 | ip6_frag_hdr_t * frag_hdr, bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 708 | { |
| 709 | int consumed = 0; |
| 710 | vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0); |
| 711 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
Vijayabhaskar Katamreddy | b361076 | 2018-06-29 05:03:40 -0700 | [diff] [blame] | 712 | reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 713 | fvnb->ip.reass.ip6_frag_hdr_offset = |
| 714 | (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb); |
| 715 | ip6_header_t *fip = vlib_buffer_get_current (fb); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 716 | if (fb->current_length < sizeof (*fip) || |
| 717 | fvnb->ip.reass.ip6_frag_hdr_offset == 0 || |
| 718 | fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length) |
| 719 | { |
| 720 | return IP6_REASS_RC_INTERNAL_ERROR; |
| 721 | } |
| 722 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 723 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 724 | ip6_frag_hdr_offset_bytes (frag_hdr); |
| 725 | u32 fragment_length = |
| 726 | vlib_buffer_length_in_chain (vm, fb) - |
| 727 | (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 728 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 729 | fragment_first + fragment_length - 1; |
| 730 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 731 | u32 candidate_range_bi = reass->first_bi; |
| 732 | u32 prev_range_bi = ~0; |
| 733 | fvnb->ip.reass.range_first = fragment_first; |
| 734 | fvnb->ip.reass.range_last = fragment_last; |
| 735 | fvnb->ip.reass.next_range_bi = ~0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 736 | if (!more_fragments) |
| 737 | { |
| 738 | reass->last_packet_octet = fragment_last; |
| 739 | } |
| 740 | if (~0 == reass->first_bi) |
| 741 | { |
| 742 | // starting a new reassembly |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 743 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi, |
| 744 | *bi0); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 745 | reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length); |
Klement Sekera | f1b4e52 | 2019-02-19 14:47:25 +0100 | [diff] [blame] | 746 | consumed = 1; |
| 747 | goto check_if_done_maybe; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 748 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 749 | reass->min_fragment_length = |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 750 | clib_min (clib_net_to_host_u16 (fip->payload_length), |
| 751 | fvnb->ip.reass.estimated_mtu); |
| 752 | while (~0 != candidate_range_bi) |
| 753 | { |
| 754 | vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi); |
| 755 | vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b); |
| 756 | if (fragment_first > candidate_vnb->ip.reass.range_last) |
| 757 | { |
| 758 | // this fragments starts after candidate range |
| 759 | prev_range_bi = candidate_range_bi; |
| 760 | candidate_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 761 | if (candidate_vnb->ip.reass.range_last < fragment_last && |
| 762 | ~0 == candidate_range_bi) |
| 763 | { |
| 764 | // special case - this fragment falls beyond all known ranges |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 765 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, |
| 766 | prev_range_bi, *bi0); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 767 | consumed = 1; |
| 768 | break; |
| 769 | } |
| 770 | continue; |
| 771 | } |
| 772 | if (fragment_last < candidate_vnb->ip.reass.range_first) |
| 773 | { |
| 774 | // this fragment ends before candidate range without any overlap |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 775 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi, |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 776 | *bi0); |
| 777 | consumed = 1; |
| 778 | } |
| 779 | else if (fragment_first == candidate_vnb->ip.reass.range_first && |
| 780 | fragment_last == candidate_vnb->ip.reass.range_last) |
| 781 | { |
| 782 | // duplicate fragment - ignore |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | // overlapping fragment - not allowed by RFC 8200 |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 787 | ip6_reass_drop_all (vm, rm, reass); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 788 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 789 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 790 | { |
| 791 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP, |
| 792 | 0); |
| 793 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 794 | *next0 = IP6_REASSEMBLY_NEXT_DROP; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 795 | *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT; |
Klement Sekera | be30fea | 2019-02-19 11:28:47 +0100 | [diff] [blame] | 796 | return IP6_REASS_RC_OK; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 797 | } |
| 798 | break; |
| 799 | } |
Klement Sekera | f1b4e52 | 2019-02-19 14:47:25 +0100 | [diff] [blame] | 800 | check_if_done_maybe: |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 801 | if (consumed) |
| 802 | { |
| 803 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 804 | { |
| 805 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 806 | } |
| 807 | } |
| 808 | if (~0 != reass->last_packet_octet && |
| 809 | reass->data_len == reass->last_packet_octet + 1) |
| 810 | { |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 811 | return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 812 | is_feature); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 813 | } |
| 814 | else |
| 815 | { |
| 816 | if (consumed) |
| 817 | { |
| 818 | *bi0 = ~0; |
| 819 | } |
| 820 | else |
| 821 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 822 | *next0 = IP6_REASSEMBLY_NEXT_DROP; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 823 | *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT; |
| 824 | } |
| 825 | } |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 826 | return IP6_REASS_RC_OK; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 829 | always_inline bool |
| 830 | ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node, |
| 831 | vlib_buffer_t * b, |
| 832 | ip6_frag_hdr_t * frag_hdr) |
| 833 | { |
| 834 | ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr; |
| 835 | while (ip6_ext_hdr (tmp->next_hdr)) |
| 836 | { |
| 837 | tmp = ip6_ext_next_header (tmp); |
| 838 | } |
| 839 | if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr) |
| 840 | { |
| 841 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 842 | ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, |
| 843 | 0); |
| 844 | b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER]; |
| 845 | |
| 846 | return false; |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | always_inline bool |
| 852 | ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm, |
| 853 | vlib_node_runtime_t * node, |
| 854 | vlib_buffer_t * b, |
| 855 | ip6_frag_hdr_t * frag_hdr) |
| 856 | { |
| 857 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 858 | ip6_header_t *ip = vlib_buffer_get_current (b); |
| 859 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 860 | u32 fragment_length = |
| 861 | vlib_buffer_length_in_chain (vm, b) - |
| 862 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 863 | if (more_fragments && 0 != fragment_length % 8) |
| 864 | { |
| 865 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 866 | ICMP6_parameter_problem_erroneous_header_field, |
| 867 | (u8 *) & ip->payload_length - (u8 *) ip); |
| 868 | return false; |
| 869 | } |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | always_inline bool |
| 874 | ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm, |
| 875 | vlib_node_runtime_t * node, |
| 876 | vlib_buffer_t * b, |
| 877 | ip6_frag_hdr_t * frag_hdr) |
| 878 | { |
| 879 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 880 | u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr); |
| 881 | u32 fragment_length = |
| 882 | vlib_buffer_length_in_chain (vm, b) - |
| 883 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 884 | if (fragment_first + fragment_length > 65535) |
| 885 | { |
| 886 | ip6_header_t *ip0 = vlib_buffer_get_current (b); |
| 887 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 888 | ICMP6_parameter_problem_erroneous_header_field, |
| 889 | (u8 *) & frag_hdr->fragment_offset_and_more |
| 890 | - (u8 *) ip0); |
| 891 | return false; |
| 892 | } |
| 893 | return true; |
| 894 | } |
| 895 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 896 | always_inline uword |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 897 | ip6_reassembly_inline (vlib_main_t * vm, |
| 898 | vlib_node_runtime_t * node, |
| 899 | vlib_frame_t * frame, bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 900 | { |
| 901 | u32 *from = vlib_frame_vector_args (frame); |
| 902 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 903 | ip6_reass_main_t *rm = &ip6_reass_main; |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 904 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index]; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 905 | clib_spinlock_lock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 906 | |
| 907 | n_left_from = frame->n_vectors; |
| 908 | next_index = node->cached_next_index; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 909 | while (n_left_from > 0) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 910 | { |
| 911 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 912 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 913 | while (n_left_from > 0 && n_left_to_next > 0) |
| 914 | { |
| 915 | u32 bi0; |
| 916 | vlib_buffer_t *b0; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 917 | u32 next0 = IP6_REASSEMBLY_NEXT_DROP; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 918 | u32 error0 = IP6_ERROR_NONE; |
| 919 | u32 icmp_bi = ~0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 920 | |
| 921 | bi0 = from[0]; |
| 922 | b0 = vlib_get_buffer (vm, bi0); |
| 923 | |
| 924 | ip6_header_t *ip0 = vlib_buffer_get_current (b0); |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 925 | ip6_frag_hdr_t *frag_hdr = NULL; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 926 | ip6_ext_header_t *prev_hdr; |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 927 | if (ip6_ext_hdr (ip0->protocol)) |
| 928 | { |
| 929 | ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr, |
| 930 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 931 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 932 | if (!frag_hdr) |
| 933 | { |
| 934 | // this is a regular packet - no fragmentation |
| 935 | next0 = IP6_REASSEMBLY_NEXT_INPUT; |
| 936 | goto skip_reass; |
| 937 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 938 | if (0 == ip6_frag_hdr_offset (frag_hdr)) |
| 939 | { |
| 940 | // first fragment - verify upper-layer is present |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 941 | if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr)) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 942 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 943 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 944 | goto skip_reass; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 945 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 946 | } |
| 947 | if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr) |
| 948 | || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0, |
| 949 | frag_hdr)) |
| 950 | { |
| 951 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 952 | goto skip_reass; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 953 | } |
| 954 | vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset = |
| 955 | (u8 *) frag_hdr - (u8 *) ip0; |
| 956 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 957 | ip6_reass_kv_t kv; |
| 958 | u8 do_handoff = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 959 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 960 | kv.k.as_u64[0] = ip0->src_address.as_u64[0]; |
| 961 | kv.k.as_u64[1] = ip0->src_address.as_u64[1]; |
| 962 | kv.k.as_u64[2] = ip0->dst_address.as_u64[0]; |
| 963 | kv.k.as_u64[3] = ip0->dst_address.as_u64[1]; |
| 964 | kv.k.as_u64[4] = |
| 965 | ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index, |
| 966 | vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 | |
| 967 | (u64) frag_hdr->identification; |
| 968 | kv.k.as_u64[5] = ip0->protocol; |
| 969 | |
| 970 | ip6_reass_t *reass = |
| 971 | ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi, |
| 972 | &do_handoff); |
| 973 | |
| 974 | if (PREDICT_FALSE (do_handoff)) |
| 975 | { |
| 976 | next0 = IP6_REASSEMBLY_NEXT_HANDOFF; |
| 977 | if (is_feature) |
| 978 | vnet_buffer (b0)->ip.reass.owner_feature_thread_index = |
| 979 | kv.v.thread_index; |
| 980 | else |
| 981 | vnet_buffer (b0)->ip.reass.owner_thread_index = |
| 982 | kv.v.thread_index; |
| 983 | } |
| 984 | else if (reass) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 985 | { |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 986 | switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0, |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 987 | &error0, frag_hdr, is_feature)) |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 988 | { |
| 989 | case IP6_REASS_RC_OK: |
| 990 | /* nothing to do here */ |
| 991 | break; |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 992 | case IP6_REASS_RC_NO_BUF: |
| 993 | /* fallthrough */ |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 994 | case IP6_REASS_RC_INTERNAL_ERROR: |
| 995 | /* drop everything and start with a clean slate */ |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 996 | ip6_reass_drop_all (vm, rm, reass); |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 997 | ip6_reass_free (rm, rt, reass); |
| 998 | goto next_packet; |
| 999 | break; |
| 1000 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1001 | } |
| 1002 | else |
| 1003 | { |
| 1004 | next0 = IP6_REASSEMBLY_NEXT_DROP; |
| 1005 | error0 = IP6_ERROR_REASS_LIMIT_REACHED; |
| 1006 | } |
| 1007 | |
| 1008 | b0->error = node->errors[error0]; |
| 1009 | |
| 1010 | if (~0 != bi0) |
| 1011 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1012 | skip_reass: |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1013 | to_next[0] = bi0; |
| 1014 | to_next += 1; |
| 1015 | n_left_to_next -= 1; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1016 | if (is_feature && IP6_ERROR_NONE == error0) |
| 1017 | { |
Kingwel Xie | a006065 | 2018-09-26 04:59:52 -0400 | [diff] [blame] | 1018 | b0 = vlib_get_buffer (vm, bi0); |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 1019 | vnet_feature_next (&next0, b0); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1020 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1021 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1022 | n_left_to_next, bi0, next0); |
| 1023 | } |
| 1024 | |
| 1025 | if (~0 != icmp_bi) |
| 1026 | { |
| 1027 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 1028 | to_next[0] = icmp_bi; |
| 1029 | to_next += 1; |
| 1030 | n_left_to_next -= 1; |
| 1031 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1032 | n_left_to_next, icmp_bi, |
| 1033 | next0); |
| 1034 | } |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 1035 | next_packet: |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1036 | from += 1; |
| 1037 | n_left_from -= 1; |
| 1038 | } |
| 1039 | |
| 1040 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1041 | } |
| 1042 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1043 | clib_spinlock_unlock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1044 | return frame->n_vectors; |
| 1045 | } |
| 1046 | |
| 1047 | static char *ip6_reassembly_error_strings[] = { |
| 1048 | #define _(sym, string) string, |
| 1049 | foreach_ip6_error |
| 1050 | #undef _ |
| 1051 | }; |
| 1052 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1053 | VLIB_NODE_FN (ip6_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1054 | vlib_frame_t * frame) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1055 | { |
| 1056 | return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ ); |
| 1057 | } |
| 1058 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1059 | /* *INDENT-OFF* */ |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1060 | VLIB_REGISTER_NODE (ip6_reass_node) = { |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1061 | .name = "ip6-reassembly", |
| 1062 | .vector_size = sizeof (u32), |
| 1063 | .format_trace = format_ip6_reass_trace, |
| 1064 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1065 | .error_strings = ip6_reassembly_error_strings, |
| 1066 | .n_next_nodes = IP6_REASSEMBLY_N_NEXT, |
| 1067 | .next_nodes = |
| 1068 | { |
| 1069 | [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 1070 | [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 1071 | [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 1072 | [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff", |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1073 | }, |
| 1074 | }; |
| 1075 | /* *INDENT-ON* */ |
| 1076 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1077 | VLIB_NODE_FN (ip6_reass_node_feature) (vlib_main_t * vm, |
| 1078 | vlib_node_runtime_t * node, |
| 1079 | vlib_frame_t * frame) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1080 | { |
| 1081 | return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ ); |
| 1082 | } |
| 1083 | |
| 1084 | /* *INDENT-OFF* */ |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1085 | VLIB_REGISTER_NODE (ip6_reass_node_feature) = { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1086 | .name = "ip6-reassembly-feature", |
| 1087 | .vector_size = sizeof (u32), |
| 1088 | .format_trace = format_ip6_reass_trace, |
| 1089 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1090 | .error_strings = ip6_reassembly_error_strings, |
| 1091 | .n_next_nodes = IP6_REASSEMBLY_N_NEXT, |
| 1092 | .next_nodes = |
| 1093 | { |
| 1094 | [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 1095 | [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 1096 | [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 1097 | [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1098 | }, |
| 1099 | }; |
| 1100 | /* *INDENT-ON* */ |
| 1101 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1102 | /* *INDENT-OFF* */ |
| 1103 | VNET_FEATURE_INIT (ip6_reassembly_feature, static) = { |
| 1104 | .arc_name = "ip6-unicast", |
| 1105 | .node_name = "ip6-reassembly-feature", |
| 1106 | .runs_before = VNET_FEATURES ("ip6-lookup"), |
| 1107 | .runs_after = 0, |
| 1108 | }; |
| 1109 | /* *INDENT-ON* */ |
| 1110 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1111 | #ifndef CLIB_MARCH_VARIANT |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1112 | static u32 |
| 1113 | ip6_reass_get_nbuckets () |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1114 | { |
| 1115 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1116 | u32 nbuckets; |
| 1117 | u8 i; |
| 1118 | |
| 1119 | nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR); |
| 1120 | |
| 1121 | for (i = 0; i < 31; i++) |
| 1122 | if ((1 << i) >= nbuckets) |
| 1123 | break; |
| 1124 | nbuckets = 1 << i; |
| 1125 | |
| 1126 | return nbuckets; |
| 1127 | } |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1128 | #endif /* CLIB_MARCH_VARIANT */ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1129 | |
| 1130 | typedef enum |
| 1131 | { |
| 1132 | IP6_EVENT_CONFIG_CHANGED = 1, |
| 1133 | } ip6_reass_event_t; |
| 1134 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1135 | #ifndef CLIB_MARCH_VARIANT |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1136 | typedef struct |
| 1137 | { |
| 1138 | int failure; |
| 1139 | clib_bihash_48_8_t *new_hash; |
| 1140 | } ip6_rehash_cb_ctx; |
| 1141 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1142 | static void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1143 | ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx) |
| 1144 | { |
| 1145 | ip6_rehash_cb_ctx *ctx = _ctx; |
| 1146 | if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1)) |
| 1147 | { |
| 1148 | ctx->failure = 1; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1152 | static void |
| 1153 | ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies, |
| 1154 | u32 expire_walk_interval_ms) |
| 1155 | { |
| 1156 | ip6_reass_main.timeout_ms = timeout_ms; |
| 1157 | ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 1158 | ip6_reass_main.max_reass_n = max_reassemblies; |
| 1159 | ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
| 1160 | } |
| 1161 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1162 | vnet_api_error_t |
| 1163 | ip6_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 1164 | u32 expire_walk_interval_ms) |
| 1165 | { |
| 1166 | u32 old_nbuckets = ip6_reass_get_nbuckets (); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1167 | ip6_reass_set_params (timeout_ms, max_reassemblies, |
| 1168 | expire_walk_interval_ms); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1169 | vlib_process_signal_event (ip6_reass_main.vlib_main, |
| 1170 | ip6_reass_main.ip6_reass_expire_node_idx, |
| 1171 | IP6_EVENT_CONFIG_CHANGED, 0); |
| 1172 | u32 new_nbuckets = ip6_reass_get_nbuckets (); |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 1173 | if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1174 | { |
| 1175 | clib_bihash_48_8_t new_hash; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1176 | clib_memset (&new_hash, 0, sizeof (new_hash)); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1177 | ip6_rehash_cb_ctx ctx; |
| 1178 | ctx.failure = 0; |
| 1179 | ctx.new_hash = &new_hash; |
| 1180 | clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets, |
| 1181 | new_nbuckets * 1024); |
| 1182 | clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash, |
| 1183 | ip6_rehash_cb, &ctx); |
| 1184 | if (ctx.failure) |
| 1185 | { |
| 1186 | clib_bihash_free_48_8 (&new_hash); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | clib_bihash_free_48_8 (&ip6_reass_main.hash); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1192 | clib_memcpy_fast (&ip6_reass_main.hash, &new_hash, |
| 1193 | sizeof (ip6_reass_main.hash)); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | vnet_api_error_t |
| 1200 | ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 1201 | u32 * expire_walk_interval_ms) |
| 1202 | { |
| 1203 | *timeout_ms = ip6_reass_main.timeout_ms; |
| 1204 | *max_reassemblies = ip6_reass_main.max_reass_n; |
| 1205 | *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms; |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1209 | static clib_error_t * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1210 | ip6_reass_init_function (vlib_main_t * vm) |
| 1211 | { |
| 1212 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1213 | clib_error_t *error = 0; |
| 1214 | u32 nbuckets; |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1215 | vlib_node_t *node; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1216 | |
| 1217 | rm->vlib_main = vm; |
| 1218 | rm->vnet_main = vnet_get_main (); |
| 1219 | |
Juraj Sloboda | cd80692 | 2018-10-10 10:15:54 +0200 | [diff] [blame] | 1220 | vec_validate (rm->per_thread_data, vlib_num_workers ()); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1221 | ip6_reass_per_thread_t *rt; |
| 1222 | vec_foreach (rt, rm->per_thread_data) |
| 1223 | { |
| 1224 | clib_spinlock_init (&rt->lock); |
| 1225 | pool_alloc (rt->pool, rm->max_reass_n); |
| 1226 | } |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1227 | |
| 1228 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk"); |
| 1229 | ASSERT (node); |
| 1230 | rm->ip6_reass_expire_node_idx = node->index; |
| 1231 | |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 1232 | ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS, |
| 1233 | IP6_REASS_MAX_REASSEMBLIES_DEFAULT, |
| 1234 | IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1235 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1236 | nbuckets = ip6_reass_get_nbuckets (); |
| 1237 | clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024); |
| 1238 | |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1239 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop"); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1240 | ASSERT (node); |
| 1241 | rm->ip6_drop_idx = node->index; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1242 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error"); |
| 1243 | ASSERT (node); |
| 1244 | rm->ip6_icmp_error_idx = node->index; |
| 1245 | |
| 1246 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 1247 | return error; |
| 1248 | ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 1249 | ip6_reass_node.index); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1250 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 1251 | rm->fq_index = vlib_frame_queue_main_init (ip6_reass_node.index, 0); |
| 1252 | rm->fq_feature_index = |
| 1253 | vlib_frame_queue_main_init (ip6_reass_node_feature.index, 0); |
| 1254 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1255 | return error; |
| 1256 | } |
| 1257 | |
| 1258 | VLIB_INIT_FUNCTION (ip6_reass_init_function); |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1259 | #endif /* CLIB_MARCH_VARIANT */ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1260 | |
| 1261 | static uword |
| 1262 | ip6_reass_walk_expired (vlib_main_t * vm, |
| 1263 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 1264 | { |
| 1265 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1266 | uword event_type, *event_data = 0; |
| 1267 | |
| 1268 | while (true) |
| 1269 | { |
| 1270 | vlib_process_wait_for_event_or_clock (vm, |
| 1271 | (f64) rm->expire_walk_interval_ms |
| 1272 | / (f64) MSEC_PER_SEC); |
| 1273 | event_type = vlib_process_get_events (vm, &event_data); |
| 1274 | |
| 1275 | switch (event_type) |
| 1276 | { |
| 1277 | case ~0: /* no events => timeout */ |
| 1278 | /* nothing to do here */ |
| 1279 | break; |
| 1280 | case IP6_EVENT_CONFIG_CHANGED: |
| 1281 | break; |
| 1282 | default: |
| 1283 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1284 | break; |
| 1285 | } |
| 1286 | f64 now = vlib_time_now (vm); |
| 1287 | |
| 1288 | ip6_reass_t *reass; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1289 | int *pool_indexes_to_free = NULL; |
| 1290 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1291 | uword thread_index = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1292 | int index; |
Juraj Sloboda | cd80692 | 2018-10-10 10:15:54 +0200 | [diff] [blame] | 1293 | const uword nthreads = vlib_num_workers () + 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1294 | u32 *vec_icmp_bi = NULL; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1295 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1296 | { |
| 1297 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1298 | clib_spinlock_lock (&rt->lock); |
| 1299 | |
| 1300 | vec_reset_length (pool_indexes_to_free); |
| 1301 | /* *INDENT-OFF* */ |
| 1302 | pool_foreach_index (index, rt->pool, ({ |
| 1303 | reass = pool_elt_at_index (rt->pool, index); |
| 1304 | if (now > reass->last_heard + rm->timeout) |
| 1305 | { |
| 1306 | vec_add1 (pool_indexes_to_free, index); |
| 1307 | } |
| 1308 | })); |
| 1309 | /* *INDENT-ON* */ |
| 1310 | int *i; |
| 1311 | /* *INDENT-OFF* */ |
| 1312 | vec_foreach (i, pool_indexes_to_free) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1313 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1314 | ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]); |
| 1315 | u32 icmp_bi = ~0; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1316 | vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi); |
| 1317 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1318 | { |
| 1319 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1320 | b->trace_index)) |
| 1321 | { |
| 1322 | /* the trace is gone, don't trace this buffer anymore */ |
| 1323 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1324 | } |
| 1325 | } |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1326 | ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1327 | if (~0 != icmp_bi) |
| 1328 | { |
| 1329 | vec_add1 (vec_icmp_bi, icmp_bi); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1330 | } |
| 1331 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1332 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1333 | /* *INDENT-ON* */ |
| 1334 | |
| 1335 | clib_spinlock_unlock (&rt->lock); |
| 1336 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1337 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1338 | while (vec_len (vec_icmp_bi) > 0) |
| 1339 | { |
| 1340 | vlib_frame_t *f = |
| 1341 | vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx); |
| 1342 | u32 *to_next = vlib_frame_vector_args (f); |
| 1343 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1344 | int trace_frame = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1345 | while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0) |
| 1346 | { |
| 1347 | u32 bi = vec_pop (vec_icmp_bi); |
| 1348 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1349 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1350 | { |
| 1351 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1352 | b->trace_index)) |
| 1353 | { |
| 1354 | /* the trace is gone, don't trace this buffer anymore */ |
| 1355 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1356 | } |
| 1357 | else |
| 1358 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1359 | trace_frame = 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 1363 | to_next[0] = bi; |
| 1364 | ++f->n_vectors; |
| 1365 | to_next += 1; |
| 1366 | n_left_to_next -= 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1367 | } |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1368 | f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1369 | vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f); |
| 1370 | } |
| 1371 | |
| 1372 | vec_free (pool_indexes_to_free); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1373 | vec_free (vec_icmp_bi); |
| 1374 | if (event_data) |
| 1375 | { |
| 1376 | _vec_len (event_data) = 0; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1383 | /* *INDENT-OFF* */ |
| 1384 | VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = { |
| 1385 | .function = ip6_reass_walk_expired, |
| 1386 | .format_trace = format_ip6_reass_trace, |
| 1387 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1388 | .name = "ip6-reassembly-expire-walk", |
| 1389 | |
| 1390 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1391 | .error_strings = ip6_reassembly_error_strings, |
| 1392 | |
| 1393 | }; |
| 1394 | /* *INDENT-ON* */ |
| 1395 | |
| 1396 | static u8 * |
| 1397 | format_ip6_reass_key (u8 * s, va_list * args) |
| 1398 | { |
| 1399 | ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *); |
| 1400 | s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1401 | key->xx_id, format_ip6_address, &key->src, format_ip6_address, |
| 1402 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
| 1403 | return s; |
| 1404 | } |
| 1405 | |
| 1406 | static u8 * |
| 1407 | format_ip6_reass (u8 * s, va_list * args) |
| 1408 | { |
| 1409 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1410 | ip6_reass_t *reass = va_arg (*args, ip6_reass_t *); |
| 1411 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1412 | s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, " |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1413 | "last_packet_octet: %u, trace_op_counter: %u\n", |
| 1414 | reass->id, format_ip6_reass_key, &reass->key, reass->first_bi, |
| 1415 | reass->data_len, reass->last_packet_octet, |
| 1416 | reass->trace_op_counter); |
| 1417 | u32 bi = reass->first_bi; |
| 1418 | u32 counter = 0; |
| 1419 | while (~0 != bi) |
| 1420 | { |
| 1421 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1422 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 1423 | s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, " |
| 1424 | "fragment[%u, %u]\n", |
| 1425 | counter, vnb->ip.reass.range_first, |
| 1426 | vnb->ip.reass.range_last, bi, |
Klement Sekera | d0f70a3 | 2018-12-14 17:24:13 +0100 | [diff] [blame] | 1427 | ip6_reass_buffer_get_data_offset (b), |
| 1428 | ip6_reass_buffer_get_data_len (b), |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1429 | vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last); |
| 1430 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1431 | { |
| 1432 | bi = b->next_buffer; |
| 1433 | } |
| 1434 | else |
| 1435 | { |
| 1436 | bi = ~0; |
| 1437 | } |
| 1438 | } |
| 1439 | return s; |
| 1440 | } |
| 1441 | |
| 1442 | static clib_error_t * |
| 1443 | show_ip6_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1444 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1445 | { |
| 1446 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1447 | |
| 1448 | vlib_cli_output (vm, "---------------------"); |
| 1449 | vlib_cli_output (vm, "IP6 reassembly status"); |
| 1450 | vlib_cli_output (vm, "---------------------"); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1451 | bool details = false; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1452 | if (unformat (input, "details")) |
| 1453 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1454 | details = true; |
| 1455 | } |
| 1456 | |
| 1457 | u32 sum_reass_n = 0; |
| 1458 | u64 sum_buffers_n = 0; |
| 1459 | ip6_reass_t *reass; |
| 1460 | uword thread_index; |
Juraj Sloboda | cd80692 | 2018-10-10 10:15:54 +0200 | [diff] [blame] | 1461 | const uword nthreads = vlib_num_workers () + 1; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1462 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1463 | { |
| 1464 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1465 | clib_spinlock_lock (&rt->lock); |
| 1466 | if (details) |
| 1467 | { |
| 1468 | /* *INDENT-OFF* */ |
| 1469 | pool_foreach (reass, rt->pool, { |
| 1470 | vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass); |
| 1471 | }); |
| 1472 | /* *INDENT-ON* */ |
| 1473 | } |
| 1474 | sum_reass_n += rt->reass_n; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1475 | clib_spinlock_unlock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1476 | } |
| 1477 | vlib_cli_output (vm, "---------------------"); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1478 | vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n", |
| 1479 | (long unsigned) sum_reass_n); |
| 1480 | vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per " |
| 1481 | "worker-thread: %lu\n", (long unsigned) rm->max_reass_n); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1482 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1483 | (long unsigned) sum_buffers_n); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /* *INDENT-OFF* */ |
| 1488 | VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = { |
| 1489 | .path = "show ip6-reassembly", |
| 1490 | .short_help = "show ip6-reassembly [details]", |
| 1491 | .function = show_ip6_reass, |
| 1492 | }; |
| 1493 | /* *INDENT-ON* */ |
| 1494 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1495 | #ifndef CLIB_MARCH_VARIANT |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1496 | vnet_api_error_t |
| 1497 | ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable) |
| 1498 | { |
| 1499 | return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature", |
| 1500 | sw_if_index, enable_disable, 0, 0); |
| 1501 | } |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 1502 | #endif /* CLIB_MARCH_VARIANT */ |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1503 | |
Vijayabhaskar Katamreddy | 470a370 | 2019-03-01 19:57:06 -0800 | [diff] [blame] | 1504 | #define foreach_ip6_reassembly_handoff_error \ |
| 1505 | _(CONGESTION_DROP, "congestion drop") |
| 1506 | |
| 1507 | |
| 1508 | typedef enum |
| 1509 | { |
| 1510 | #define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym, |
| 1511 | foreach_ip6_reassembly_handoff_error |
| 1512 | #undef _ |
| 1513 | IP6_REASSEMBLY_HANDOFF_N_ERROR, |
| 1514 | } ip6_reassembly_handoff_error_t; |
| 1515 | |
| 1516 | static char *ip6_reassembly_handoff_error_strings[] = { |
| 1517 | #define _(sym,string) string, |
| 1518 | foreach_ip6_reassembly_handoff_error |
| 1519 | #undef _ |
| 1520 | }; |
| 1521 | |
| 1522 | typedef struct |
| 1523 | { |
| 1524 | u32 next_worker_index; |
| 1525 | } ip6_reassembly_handoff_trace_t; |
| 1526 | |
| 1527 | static u8 * |
| 1528 | format_ip6_reassembly_handoff_trace (u8 * s, va_list * args) |
| 1529 | { |
| 1530 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 1531 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 1532 | ip6_reassembly_handoff_trace_t *t = |
| 1533 | va_arg (*args, ip6_reassembly_handoff_trace_t *); |
| 1534 | |
| 1535 | s = |
| 1536 | format (s, "ip6-reassembly-handoff: next-worker %d", |
| 1537 | t->next_worker_index); |
| 1538 | |
| 1539 | return s; |
| 1540 | } |
| 1541 | |
| 1542 | always_inline uword |
| 1543 | ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1544 | vlib_frame_t * frame, bool is_feature) |
| 1545 | { |
| 1546 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1547 | |
| 1548 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 1549 | u32 n_enq, n_left_from, *from; |
| 1550 | u16 thread_indices[VLIB_FRAME_SIZE], *ti; |
| 1551 | u32 fq_index; |
| 1552 | |
| 1553 | from = vlib_frame_vector_args (frame); |
| 1554 | n_left_from = frame->n_vectors; |
| 1555 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 1556 | |
| 1557 | b = bufs; |
| 1558 | ti = thread_indices; |
| 1559 | |
| 1560 | fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index; |
| 1561 | |
| 1562 | while (n_left_from > 0) |
| 1563 | { |
| 1564 | ti[0] = |
| 1565 | (is_feature) ? vnet_buffer (b[0])->ip. |
| 1566 | reass.owner_feature_thread_index : vnet_buffer (b[0])->ip. |
| 1567 | reass.owner_thread_index; |
| 1568 | |
| 1569 | if (PREDICT_FALSE |
| 1570 | ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 1571 | && (b[0]->flags & VLIB_BUFFER_IS_TRACED))) |
| 1572 | { |
| 1573 | ip6_reassembly_handoff_trace_t *t = |
| 1574 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
| 1575 | t->next_worker_index = ti[0]; |
| 1576 | } |
| 1577 | |
| 1578 | n_left_from -= 1; |
| 1579 | ti += 1; |
| 1580 | b += 1; |
| 1581 | } |
| 1582 | n_enq = |
| 1583 | vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices, |
| 1584 | frame->n_vectors, 1); |
| 1585 | |
| 1586 | if (n_enq < frame->n_vectors) |
| 1587 | vlib_node_increment_counter (vm, node->node_index, |
| 1588 | IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP, |
| 1589 | frame->n_vectors - n_enq); |
| 1590 | return frame->n_vectors; |
| 1591 | } |
| 1592 | |
| 1593 | VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm, |
| 1594 | vlib_node_runtime_t * node, |
| 1595 | vlib_frame_t * frame) |
| 1596 | { |
| 1597 | return ip6_reassembly_handoff_inline (vm, node, frame, |
| 1598 | false /* is_feature */ ); |
| 1599 | } |
| 1600 | |
| 1601 | /* *INDENT-OFF* */ |
| 1602 | VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = { |
| 1603 | .name = "ip6-reassembly-handoff", |
| 1604 | .vector_size = sizeof (u32), |
| 1605 | .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings), |
| 1606 | .error_strings = ip6_reassembly_handoff_error_strings, |
| 1607 | .format_trace = format_ip6_reassembly_handoff_trace, |
| 1608 | |
| 1609 | .n_next_nodes = 1, |
| 1610 | |
| 1611 | .next_nodes = { |
| 1612 | [0] = "error-drop", |
| 1613 | }, |
| 1614 | }; |
| 1615 | |
| 1616 | |
| 1617 | VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm, |
| 1618 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 1619 | { |
| 1620 | return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ ); |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | /* *INDENT-OFF* */ |
| 1625 | VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = { |
| 1626 | .name = "ip6-reass-feature-hoff", |
| 1627 | .vector_size = sizeof (u32), |
| 1628 | .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings), |
| 1629 | .error_strings = ip6_reassembly_handoff_error_strings, |
| 1630 | .format_trace = format_ip6_reassembly_handoff_trace, |
| 1631 | |
| 1632 | .n_next_nodes = 1, |
| 1633 | |
| 1634 | .next_nodes = { |
| 1635 | [0] = "error-drop", |
| 1636 | }, |
| 1637 | }; |
| 1638 | /* *INDENT-ON* */ |
| 1639 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1640 | /* |
| 1641 | * fd.io coding-style-patch-verification: ON |
| 1642 | * |
| 1643 | * Local Variables: |
| 1644 | * eval: (c-set-style "gnu") |
| 1645 | * End: |
| 1646 | */ |