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 IPv4 Reassembly. |
| 19 | * |
| 20 | * This file contains the source code for IPv4 reassembly. |
| 21 | */ |
| 22 | |
| 23 | #include <vppinfra/vec.h> |
| 24 | #include <vnet/vnet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vppinfra/bihash_24_8.h> |
| 27 | #include <vnet/ip/ip4_reassembly.h> |
| 28 | |
| 29 | #define MSEC_PER_SEC 1000 |
| 30 | #define IP4_REASS_TIMEOUT_DEFAULT_MS 100 |
| 31 | #define IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default |
| 32 | #define IP4_REASS_MAX_REASSEMBLIES_DEAFULT 1024 |
| 33 | #define IP4_REASS_HT_LOAD_FACTOR (0.75) |
| 34 | |
| 35 | #define IP4_REASS_DEBUG_BUFFERS 0 |
| 36 | #if IP4_REASS_DEBUG_BUFFERS |
| 37 | #define IP4_REASS_DEBUG_BUFFER(bi, what) \ |
| 38 | do \ |
| 39 | { \ |
| 40 | u32 _bi = bi; \ |
| 41 | printf (#what "buffer %u", _bi); \ |
| 42 | vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \ |
| 43 | while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \ |
| 44 | { \ |
| 45 | _bi = _b->next_buffer; \ |
| 46 | printf ("[%u]", _bi); \ |
| 47 | _b = vlib_get_buffer (vm, _bi); \ |
| 48 | } \ |
| 49 | printf ("\n"); \ |
| 50 | fflush (stdout); \ |
| 51 | } \ |
| 52 | while (0) |
| 53 | #else |
| 54 | #define IP4_REASS_DEBUG_BUFFER(...) |
| 55 | #endif |
| 56 | |
| 57 | static vlib_node_registration_t ip4_reass_node; |
| 58 | |
| 59 | typedef struct |
| 60 | { |
| 61 | union |
| 62 | { |
| 63 | struct |
| 64 | { |
| 65 | // align by making this 4 octets even though its a 2 octets field |
| 66 | u32 xx_id; |
| 67 | ip4_address_t src; |
| 68 | ip4_address_t dst; |
| 69 | // align by making this 4 octets even though its a 2 octets field |
| 70 | u32 frag_id; |
| 71 | // align by making this 4 octets even though its a 1 octet field |
| 72 | u32 proto; |
| 73 | u32 unused; |
| 74 | }; |
| 75 | u64 as_u64[3]; |
| 76 | }; |
| 77 | } ip4_reass_key_t; |
| 78 | |
| 79 | always_inline u32 |
| 80 | ip4_reass_buffer_get_data_offset_no_check (vlib_buffer_t * b) |
| 81 | { |
| 82 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 83 | return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first; |
| 84 | } |
| 85 | |
| 86 | always_inline u32 |
| 87 | ip4_reass_buffer_get_data_offset (vlib_buffer_t * b) |
| 88 | { |
| 89 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 90 | ASSERT (vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first); |
| 91 | return ip4_reass_buffer_get_data_offset_no_check (b); |
| 92 | } |
| 93 | |
| 94 | always_inline u16 |
| 95 | ip4_reass_buffer_get_data_len_no_check (vlib_buffer_t * b) |
| 96 | { |
| 97 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 98 | return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) - |
| 99 | (vnb->ip.reass.fragment_first + ip4_reass_buffer_get_data_offset (b)) + 1; |
| 100 | } |
| 101 | |
| 102 | always_inline u16 |
| 103 | ip4_reass_buffer_get_data_len (vlib_buffer_t * b) |
| 104 | { |
| 105 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 106 | ASSERT (vnb->ip.reass.range_last > vnb->ip.reass.fragment_first); |
| 107 | return ip4_reass_buffer_get_data_len_no_check (b); |
| 108 | } |
| 109 | |
| 110 | typedef struct |
| 111 | { |
| 112 | // hash table key |
| 113 | ip4_reass_key_t key; |
| 114 | f64 first_heard; |
| 115 | // time when last packet was received |
| 116 | f64 last_heard; |
| 117 | // internal id of this reassembly |
| 118 | u32 id; |
| 119 | // buffer index of first buffer in this reassembly context |
| 120 | u32 first_bi; |
| 121 | // last octet of packet, ~0 until fragment without more_fragments arrives |
| 122 | u32 last_packet_octet; |
| 123 | // length of data collected so far |
| 124 | u32 data_len; |
| 125 | // trace operation counter |
| 126 | u32 trace_op_counter; |
| 127 | } ip4_reass_t; |
| 128 | |
| 129 | typedef struct |
| 130 | { |
| 131 | // IPv4 config |
| 132 | u32 timeout_ms; |
| 133 | f64 timeout; |
| 134 | u32 expire_walk_interval_ms; |
| 135 | u32 max_reass_n; |
| 136 | |
| 137 | // IPv4 runtime |
| 138 | ip4_reass_t *pool; |
| 139 | clib_bihash_24_8_t hash; |
| 140 | u32 reass_n; |
| 141 | u32 id_counter; |
| 142 | u32 buffers_n; |
| 143 | |
| 144 | // convenience |
| 145 | vlib_main_t *vlib_main; |
| 146 | vnet_main_t *vnet_main; |
| 147 | |
| 148 | // node index of ip4-drop node |
| 149 | u32 ip4_drop_idx; |
| 150 | u32 ip4_reass_expire_node_idx; |
| 151 | |
| 152 | } ip4_reass_main_t; |
| 153 | |
| 154 | ip4_reass_main_t ip4_reass_main; |
| 155 | |
| 156 | typedef enum |
| 157 | { |
| 158 | IP4_REASSEMBLY_NEXT_INPUT, |
| 159 | IP4_REASSEMBLY_NEXT_DROP, |
| 160 | IP4_REASSEMBLY_N_NEXT, |
| 161 | } ip4_reass_next_t; |
| 162 | |
| 163 | typedef enum |
| 164 | { |
| 165 | RANGE_NEW, |
| 166 | RANGE_SHRINK, |
| 167 | RANGE_DISCARD, |
| 168 | RANGE_OVERLAP, |
| 169 | FINALIZE, |
| 170 | } ip4_reass_trace_operation_e; |
| 171 | |
| 172 | typedef struct |
| 173 | { |
| 174 | u16 range_first; |
| 175 | u16 range_last; |
| 176 | u32 range_bi; |
| 177 | i32 data_offset; |
| 178 | u32 data_len; |
| 179 | u32 first_bi; |
| 180 | } ip4_reass_range_trace_t; |
| 181 | |
| 182 | typedef struct |
| 183 | { |
| 184 | ip4_reass_trace_operation_e action; |
| 185 | u32 pool_index; |
| 186 | u32 reass_id; |
| 187 | ip4_reass_range_trace_t trace_range; |
| 188 | u32 size_diff; |
| 189 | u32 op_id; |
| 190 | u32 fragment_first; |
| 191 | u32 fragment_last; |
| 192 | u32 total_data_len; |
| 193 | } ip4_reass_trace_t; |
| 194 | |
| 195 | void |
| 196 | ip4_reass_trace_details (vlib_main_t * vm, u32 bi, |
| 197 | ip4_reass_range_trace_t * trace) |
| 198 | { |
| 199 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 200 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 201 | trace->range_first = vnb->ip.reass.range_first; |
| 202 | trace->range_last = vnb->ip.reass.range_last; |
| 203 | trace->data_offset = ip4_reass_buffer_get_data_offset_no_check (b); |
| 204 | trace->data_len = ip4_reass_buffer_get_data_len_no_check (b); |
| 205 | trace->range_bi = bi; |
| 206 | } |
| 207 | |
| 208 | u8 * |
| 209 | format_ip4_reass_range_trace (u8 * s, va_list * args) |
| 210 | { |
| 211 | ip4_reass_range_trace_t *trace = va_arg (*args, ip4_reass_range_trace_t *); |
| 212 | s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first, |
| 213 | trace->range_last, trace->data_offset, trace->data_len, |
| 214 | trace->range_bi); |
| 215 | return s; |
| 216 | } |
| 217 | |
| 218 | u8 * |
| 219 | format_ip4_reass_trace (u8 * s, va_list * args) |
| 220 | { |
| 221 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 222 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 223 | ip4_reass_trace_t *t = va_arg (*args, ip4_reass_trace_t *); |
| 224 | s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id); |
| 225 | u32 indent = format_get_indent (s); |
| 226 | s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]", |
| 227 | t->trace_range.first_bi, t->total_data_len, t->fragment_first, |
| 228 | t->fragment_last); |
| 229 | switch (t->action) |
| 230 | { |
| 231 | case RANGE_SHRINK: |
| 232 | s = format (s, "\n%Ushrink %U by %u", format_white_space, indent, |
| 233 | format_ip4_reass_range_trace, &t->trace_range, |
| 234 | t->size_diff); |
| 235 | break; |
| 236 | case RANGE_DISCARD: |
| 237 | s = format (s, "\n%Udiscard %U", format_white_space, indent, |
| 238 | format_ip4_reass_range_trace, &t->trace_range); |
| 239 | break; |
| 240 | case RANGE_NEW: |
| 241 | s = format (s, "\n%Unew %U", format_white_space, indent, |
| 242 | format_ip4_reass_range_trace, &t->trace_range); |
| 243 | break; |
| 244 | case RANGE_OVERLAP: |
| 245 | s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent, |
| 246 | format_ip4_reass_range_trace, &t->trace_range); |
| 247 | break; |
| 248 | case FINALIZE: |
| 249 | s = format (s, "\n%Ufinalize reassembly", format_white_space, indent); |
| 250 | break; |
| 251 | } |
| 252 | return s; |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | ip4_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 257 | ip4_reass_main_t * rm, ip4_reass_t * reass, u32 bi, |
| 258 | ip4_reass_trace_operation_e action, u32 size_diff) |
| 259 | { |
| 260 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 261 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 262 | ip4_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
| 263 | t->pool_index = reass - rm->pool; |
| 264 | t->reass_id = reass->id; |
| 265 | t->action = action; |
| 266 | ip4_reass_trace_details (vm, bi, &t->trace_range); |
| 267 | t->size_diff = size_diff; |
| 268 | t->op_id = reass->trace_op_counter; |
| 269 | ++reass->trace_op_counter; |
| 270 | t->fragment_first = vnb->ip.reass.fragment_first; |
| 271 | t->fragment_last = vnb->ip.reass.fragment_last; |
| 272 | t->trace_range.first_bi = reass->first_bi; |
| 273 | t->total_data_len = reass->data_len; |
| 274 | #if 0 |
| 275 | static u8 *s = NULL; |
| 276 | s = format (s, "%U", format_ip4_reass_trace, NULL, NULL, t); |
| 277 | printf ("%.*s\n", vec_len (s), s); |
| 278 | fflush (stdout); |
| 279 | vec_reset_length (s); |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | ip4_reass_free (ip4_reass_main_t * rm, ip4_reass_t * reass) |
| 285 | { |
| 286 | clib_bihash_kv_24_8_t kv; |
| 287 | kv.key[0] = reass->key.as_u64[0]; |
| 288 | kv.key[1] = reass->key.as_u64[1]; |
| 289 | kv.key[2] = reass->key.as_u64[2]; |
| 290 | clib_bihash_add_del_24_8 (&rm->hash, &kv, 0); |
| 291 | pool_put (rm->pool, reass); |
| 292 | --rm->reass_n; |
| 293 | } |
| 294 | |
| 295 | static void |
| 296 | ip4_reass_on_timeout (vlib_main_t * vm, ip4_reass_main_t * rm, |
| 297 | ip4_reass_t * reass, u32 ** vec_drop_timeout) |
| 298 | { |
| 299 | u32 range_bi = reass->first_bi; |
| 300 | vlib_buffer_t *range_b; |
| 301 | vnet_buffer_opaque_t *range_vnb; |
| 302 | while (~0 != range_bi) |
| 303 | { |
| 304 | range_b = vlib_get_buffer (vm, range_bi); |
| 305 | range_vnb = vnet_buffer (range_b); |
| 306 | u32 bi = range_bi; |
| 307 | while (~0 != bi) |
| 308 | { |
| 309 | vec_add1 (*vec_drop_timeout, bi); |
| 310 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 311 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 312 | { |
| 313 | bi = b->next_buffer; |
| 314 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | bi = ~0; |
| 319 | } |
| 320 | } |
| 321 | range_bi = range_vnb->ip.reass.next_range_bi; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | ip4_reass_t * |
| 326 | ip4_reass_find_or_create (vlib_main_t * vm, ip4_reass_main_t * rm, |
| 327 | ip4_reass_key_t * k, u32 ** vec_drop_timeout) |
| 328 | { |
| 329 | ip4_reass_t *reass = NULL; |
| 330 | f64 now = vlib_time_now (rm->vlib_main); |
| 331 | clib_bihash_kv_24_8_t kv, value; |
| 332 | kv.key[0] = k->as_u64[0]; |
| 333 | kv.key[1] = k->as_u64[1]; |
| 334 | kv.key[2] = k->as_u64[2]; |
| 335 | |
| 336 | if (!clib_bihash_search_24_8 (&rm->hash, &kv, &value)) |
| 337 | { |
| 338 | reass = pool_elt_at_index (rm->pool, value.value); |
| 339 | if (now > reass->last_heard + rm->timeout) |
| 340 | { |
| 341 | ip4_reass_on_timeout (vm, rm, reass, vec_drop_timeout); |
| 342 | ip4_reass_free (rm, reass); |
| 343 | reass = NULL; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (reass) |
| 348 | { |
| 349 | reass->last_heard = now; |
| 350 | return reass; |
| 351 | } |
| 352 | |
| 353 | if (rm->reass_n >= rm->max_reass_n) |
| 354 | { |
| 355 | reass = NULL; |
| 356 | return reass; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | pool_get (rm->pool, reass); |
| 361 | memset (reass, 0, sizeof (*reass)); |
| 362 | reass->id = rm->id_counter; |
| 363 | ++rm->id_counter; |
| 364 | reass->first_bi = ~0; |
| 365 | reass->last_packet_octet = ~0; |
| 366 | reass->data_len = 0; |
| 367 | ++rm->reass_n; |
| 368 | } |
| 369 | |
| 370 | reass->key.as_u64[0] = kv.key[0] = k->as_u64[0]; |
| 371 | reass->key.as_u64[1] = kv.key[1] = k->as_u64[1]; |
| 372 | reass->key.as_u64[2] = kv.key[2] = k->as_u64[2]; |
| 373 | kv.value = reass - rm->pool; |
| 374 | reass->last_heard = now; |
| 375 | |
| 376 | if (clib_bihash_add_del_24_8 (&rm->hash, &kv, 1)) |
| 377 | { |
| 378 | ip4_reass_free (rm, reass); |
| 379 | reass = NULL; |
| 380 | } |
| 381 | |
| 382 | return reass; |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | ip4_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 387 | ip4_reass_main_t * rm, ip4_reass_t * reass, u32 * bi0, |
| 388 | u32 * next0, vlib_error_t * error0, u32 next_input, |
| 389 | u32 ** vec_drop_compress, u32 ** vec_drop_overlap) |
| 390 | { |
| 391 | ASSERT (~0 != reass->first_bi); |
| 392 | vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi); |
| 393 | vlib_buffer_t *last_b = NULL; |
| 394 | u32 sub_chain_bi = reass->first_bi; |
| 395 | u32 total_length = 0; |
| 396 | u32 buf_cnt = 0; |
| 397 | u32 dropped_cnt = 0; |
| 398 | do |
| 399 | { |
| 400 | u32 tmp_bi = sub_chain_bi; |
| 401 | vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi); |
| 402 | ip4_header_t *ip = vlib_buffer_get_current (tmp); |
| 403 | u32 data_len = ip4_reass_buffer_get_data_len (tmp); |
| 404 | u32 trim_front = |
| 405 | ip4_header_bytes (ip) + ip4_reass_buffer_get_data_offset (tmp); |
| 406 | u32 trim_end = |
| 407 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len; |
| 408 | if (tmp_bi == reass->first_bi) |
| 409 | { |
| 410 | /* first buffer - keep ip4 header */ |
| 411 | ASSERT (0 == ip4_reass_buffer_get_data_offset (tmp)); |
| 412 | trim_front = 0; |
| 413 | trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len - |
| 414 | ip4_header_bytes (ip); |
Klement Sekera | 56e6f09 | 2018-02-02 16:17:55 +0100 | [diff] [blame] | 415 | ASSERT (vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 416 | } |
| 417 | u32 keep_data = |
| 418 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end; |
| 419 | while (1) |
| 420 | { |
| 421 | ++buf_cnt; |
| 422 | if (trim_front) |
| 423 | { |
| 424 | if (trim_front > tmp->current_length) |
| 425 | { |
| 426 | /* drop whole buffer */ |
| 427 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 428 | ++dropped_cnt; |
| 429 | trim_front -= tmp->current_length; |
| 430 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 431 | tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 432 | tmp_bi = tmp->next_buffer; |
| 433 | tmp = vlib_get_buffer (vm, tmp_bi); |
| 434 | continue; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | vlib_buffer_advance (tmp, trim_front); |
| 439 | trim_front = 0; |
| 440 | } |
| 441 | } |
| 442 | if (keep_data) |
| 443 | { |
| 444 | if (last_b) |
| 445 | { |
| 446 | last_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 447 | last_b->next_buffer = tmp_bi; |
| 448 | } |
| 449 | last_b = tmp; |
| 450 | if (keep_data <= tmp->current_length) |
| 451 | { |
| 452 | tmp->current_length = keep_data; |
| 453 | keep_data = 0; |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | keep_data -= tmp->current_length; |
| 458 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 459 | } |
| 460 | total_length += tmp->current_length; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | vec_add1 (*vec_drop_overlap, tmp_bi); |
| 465 | ASSERT (reass->first_bi != tmp_bi); |
| 466 | ++dropped_cnt; |
| 467 | } |
| 468 | if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 469 | { |
| 470 | tmp_bi = tmp->next_buffer; |
| 471 | tmp = vlib_get_buffer (vm, tmp->next_buffer); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | sub_chain_bi = |
| 479 | vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip. |
| 480 | reass.next_range_bi; |
| 481 | } |
| 482 | while (~0 != sub_chain_bi); |
| 483 | last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 484 | ASSERT (rm->buffers_n >= (buf_cnt - dropped_cnt)); |
| 485 | rm->buffers_n -= buf_cnt - dropped_cnt; |
| 486 | ASSERT (total_length >= first_b->current_length); |
| 487 | total_length -= first_b->current_length; |
| 488 | first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 489 | first_b->total_length_not_including_first_buffer = total_length; |
| 490 | ip4_header_t *ip = vlib_buffer_get_current (first_b); |
| 491 | ip->flags_and_fragment_offset = 0; |
| 492 | ip->length = clib_host_to_net_u16 (first_b->current_length + total_length); |
| 493 | ip->checksum = ip4_header_checksum (ip); |
| 494 | vlib_buffer_chain_compress (vm, first_b, vec_drop_compress); |
| 495 | if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 496 | { |
| 497 | ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0); |
| 498 | #if 0 |
| 499 | // following code does a hexdump of packet fragments to stdout ... |
| 500 | do |
| 501 | { |
| 502 | u32 bi = reass->first_bi; |
| 503 | u8 *s = NULL; |
| 504 | while (~0 != bi) |
| 505 | { |
| 506 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 507 | s = format (s, "%u: %U\n", bi, format_hexdump, |
| 508 | vlib_buffer_get_current (b), b->current_length); |
| 509 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 510 | { |
| 511 | bi = b->next_buffer; |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | printf ("%.*s\n", vec_len (s), s); |
| 519 | fflush (stdout); |
| 520 | vec_free (s); |
| 521 | } |
| 522 | while (0); |
| 523 | #endif |
| 524 | } |
| 525 | *bi0 = reass->first_bi; |
| 526 | *next0 = next_input; |
| 527 | *error0 = IP4_ERROR_NONE; |
| 528 | ip4_reass_free (rm, reass); |
| 529 | reass = NULL; |
| 530 | } |
| 531 | |
| 532 | static u32 |
| 533 | ip4_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b) |
| 534 | { |
| 535 | u32 len = 0; |
| 536 | while (b) |
| 537 | { |
| 538 | ++len; |
| 539 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 540 | { |
| 541 | b = vlib_get_buffer (vm, b->next_buffer); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | return len; |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | ip4_reass_insert_range_in_chain (vlib_main_t * vm, |
| 553 | ip4_reass_main_t * rm, |
| 554 | ip4_reass_t * reass, |
| 555 | u32 prev_range_bi, u32 new_next_bi) |
| 556 | { |
| 557 | |
| 558 | vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi); |
| 559 | vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b); |
| 560 | if (~0 != prev_range_bi) |
| 561 | { |
| 562 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 563 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 564 | new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi; |
| 565 | prev_vnb->ip.reass.next_range_bi = new_next_bi; |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | if (~0 != reass->first_bi) |
| 570 | { |
| 571 | new_next_vnb->ip.reass.next_range_bi = reass->first_bi; |
| 572 | } |
| 573 | reass->first_bi = new_next_bi; |
| 574 | } |
| 575 | reass->data_len += ip4_reass_buffer_get_data_len (new_next_b); |
| 576 | rm->buffers_n += ip4_reass_get_buffer_chain_length (vm, new_next_b); |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | ip4_reass_remove_range_from_chain (vlib_main_t * vm, |
| 581 | vlib_node_runtime_t * node, |
| 582 | ip4_reass_main_t * rm, |
| 583 | u32 ** vec_drop_overlap, |
| 584 | ip4_reass_t * reass, u32 prev_range_bi, |
| 585 | u32 discard_bi) |
| 586 | { |
| 587 | vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi); |
| 588 | vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b); |
| 589 | if (~0 != prev_range_bi) |
| 590 | { |
| 591 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 592 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 593 | ASSERT (prev_vnb->ip.reass.next_range_bi == discard_bi); |
| 594 | prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi; |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | reass->first_bi = discard_vnb->ip.reass.next_range_bi; |
| 599 | } |
| 600 | reass->data_len -= ip4_reass_buffer_get_data_len (discard_b); |
| 601 | while (1) |
| 602 | { |
| 603 | vec_add1 (*vec_drop_overlap, discard_bi); |
| 604 | if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 605 | { |
| 606 | ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD, |
| 607 | 0); |
| 608 | } |
| 609 | if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 610 | { |
| 611 | discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 612 | discard_bi = discard_b->next_buffer; |
| 613 | discard_b = vlib_get_buffer (vm, discard_bi); |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void |
| 623 | ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 624 | ip4_reass_main_t * rm, ip4_reass_t * reass, u32 * bi0, |
| 625 | u32 * next0, vlib_error_t * error0, |
| 626 | u32 ** vec_drop_overlap, u32 ** vec_drop_compress, |
| 627 | u32 next_input, u32 next_drop) |
| 628 | { |
| 629 | int consumed = 0; |
| 630 | vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0); |
| 631 | ip4_header_t *fip = vlib_buffer_get_current (fb); |
| 632 | ASSERT (fb->current_length >= sizeof (*fip)); |
| 633 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
| 634 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 635 | ip4_get_fragment_offset_bytes (fip); |
| 636 | u32 fragment_length = |
| 637 | clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip); |
| 638 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 639 | fragment_first + fragment_length - 1; |
| 640 | int more_fragments = ip4_get_fragment_more (fip); |
| 641 | u32 candidate_range_bi = reass->first_bi; |
| 642 | u32 prev_range_bi = ~0; |
| 643 | fvnb->ip.reass.range_first = fragment_first; |
| 644 | fvnb->ip.reass.range_last = fragment_last; |
| 645 | fvnb->ip.reass.next_range_bi = ~0; |
| 646 | if (!more_fragments) |
| 647 | { |
| 648 | reass->last_packet_octet = fragment_last; |
| 649 | } |
| 650 | if (~0 == reass->first_bi) |
| 651 | { |
| 652 | // starting a new reassembly |
| 653 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, *bi0); |
| 654 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 655 | { |
| 656 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 657 | } |
| 658 | *bi0 = ~0; |
| 659 | fvnb->ip.reass.estimated_mtu = clib_net_to_host_u16 (fip->length); |
| 660 | return; |
| 661 | } |
| 662 | fvnb->ip.reass.estimated_mtu = clib_min (clib_net_to_host_u16 (fip->length), |
| 663 | fvnb->ip.reass.estimated_mtu); |
| 664 | while (~0 != candidate_range_bi) |
| 665 | { |
| 666 | vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi); |
| 667 | vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b); |
| 668 | if (fragment_first > candidate_vnb->ip.reass.range_last) |
| 669 | { |
| 670 | // this fragments starts after candidate range |
| 671 | prev_range_bi = candidate_range_bi; |
| 672 | candidate_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 673 | if (candidate_vnb->ip.reass.range_last < fragment_last && |
| 674 | ~0 == candidate_range_bi) |
| 675 | { |
| 676 | // special case - this fragment falls beyond all known ranges |
| 677 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 678 | *bi0); |
| 679 | consumed = 1; |
| 680 | break; |
| 681 | } |
| 682 | continue; |
| 683 | } |
| 684 | if (fragment_last < candidate_vnb->ip.reass.range_first) |
| 685 | { |
| 686 | // this fragment ends before candidate range without any overlap |
| 687 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 688 | *bi0); |
| 689 | consumed = 1; |
| 690 | } |
| 691 | else |
| 692 | { |
| 693 | if (fragment_first >= candidate_vnb->ip.reass.range_first && |
| 694 | fragment_last <= candidate_vnb->ip.reass.range_last) |
| 695 | { |
| 696 | // this fragment is a (sub)part of existing range, ignore it |
| 697 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 698 | { |
| 699 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, |
| 700 | RANGE_OVERLAP, 0); |
| 701 | } |
| 702 | break; |
| 703 | } |
| 704 | int discard_candidate = 0; |
| 705 | if (fragment_first < candidate_vnb->ip.reass.range_first) |
| 706 | { |
| 707 | u32 overlap = |
| 708 | fragment_last - candidate_vnb->ip.reass.range_first + 1; |
| 709 | if (overlap < ip4_reass_buffer_get_data_len (candidate_b)) |
| 710 | { |
| 711 | candidate_vnb->ip.reass.range_first += overlap; |
| 712 | ASSERT (reass->data_len >= overlap); |
| 713 | reass->data_len -= overlap; |
| 714 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 715 | { |
| 716 | ip4_reass_add_trace (vm, node, rm, reass, |
| 717 | candidate_range_bi, RANGE_SHRINK, |
| 718 | overlap); |
| 719 | } |
| 720 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 721 | prev_range_bi, *bi0); |
| 722 | consumed = 1; |
| 723 | } |
| 724 | else |
| 725 | { |
| 726 | discard_candidate = 1; |
| 727 | } |
| 728 | } |
| 729 | else if (fragment_last > candidate_vnb->ip.reass.range_last) |
| 730 | { |
| 731 | u32 overlap = |
| 732 | candidate_vnb->ip.reass.range_last - fragment_first + 1; |
| 733 | if (overlap < ip4_reass_buffer_get_data_len (candidate_b)) |
| 734 | { |
| 735 | fvnb->ip.reass.range_first += overlap; |
| 736 | if (~0 != candidate_vnb->ip.reass.next_range_bi) |
| 737 | { |
| 738 | prev_range_bi = candidate_range_bi; |
| 739 | candidate_range_bi = |
| 740 | candidate_vnb->ip.reass.next_range_bi; |
| 741 | continue; |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | // special case - last range discarded |
| 746 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 747 | candidate_range_bi, |
| 748 | *bi0); |
| 749 | consumed = 1; |
| 750 | } |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | discard_candidate = 1; |
| 755 | } |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | discard_candidate = 1; |
| 760 | } |
| 761 | if (discard_candidate) |
| 762 | { |
| 763 | u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 764 | // discard candidate range, probe next range |
| 765 | ip4_reass_remove_range_from_chain (vm, node, rm, |
| 766 | vec_drop_overlap, reass, |
| 767 | prev_range_bi, |
| 768 | candidate_range_bi); |
| 769 | if (~0 != next_range_bi) |
| 770 | { |
| 771 | candidate_range_bi = next_range_bi; |
| 772 | continue; |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | // special case - last range discarded |
| 777 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 778 | prev_range_bi, *bi0); |
| 779 | consumed = 1; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | break; |
| 784 | } |
| 785 | if (consumed) |
| 786 | { |
| 787 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 788 | { |
| 789 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 790 | } |
| 791 | } |
| 792 | if (~0 != reass->last_packet_octet && |
| 793 | reass->data_len == reass->last_packet_octet + 1) |
| 794 | { |
| 795 | ip4_reass_finalize (vm, node, rm, reass, bi0, next0, error0, next_input, |
| 796 | vec_drop_compress, vec_drop_overlap); |
| 797 | } |
| 798 | else |
| 799 | { |
| 800 | if (consumed) |
| 801 | { |
| 802 | *bi0 = ~0; |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | *next0 = next_drop; |
| 807 | *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | always_inline uword |
| 813 | ip4_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 814 | vlib_frame_t * frame) |
| 815 | { |
| 816 | u32 *from = vlib_frame_vector_args (frame); |
| 817 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 818 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 819 | |
| 820 | n_left_from = frame->n_vectors; |
| 821 | next_index = node->cached_next_index; |
| 822 | static u32 *vec_drop_timeout = NULL; // indexes of buffers which timed out |
| 823 | static u32 *vec_drop_overlap = NULL; // indexes of buffers which were discarded due to overlap |
| 824 | static u32 *vec_drop_compress = NULL; // indexes of buffers dicarded due to buffer compression |
| 825 | while (n_left_from > 0 || vec_len (vec_drop_timeout) > 0 || |
| 826 | vec_len (vec_drop_overlap) > 0) |
| 827 | { |
| 828 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 829 | |
| 830 | while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0) |
| 831 | { |
| 832 | u32 bi = vec_pop (vec_drop_timeout); |
| 833 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 834 | b->error = node->errors[IP4_ERROR_REASS_TIMEOUT]; |
| 835 | to_next[0] = bi; |
| 836 | to_next += 1; |
| 837 | n_left_to_next -= 1; |
| 838 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 839 | n_left_to_next, bi, |
| 840 | IP4_REASSEMBLY_NEXT_DROP); |
| 841 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout); |
| 842 | ASSERT (rm->buffers_n > 0); |
| 843 | --rm->buffers_n; |
| 844 | } |
| 845 | |
| 846 | while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0) |
| 847 | { |
| 848 | u32 bi = vec_pop (vec_drop_overlap); |
| 849 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 850 | b->error = node->errors[IP4_ERROR_REASS_DUPLICATE_FRAGMENT]; |
| 851 | to_next[0] = bi; |
| 852 | to_next += 1; |
| 853 | n_left_to_next -= 1; |
| 854 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 855 | n_left_to_next, bi, |
| 856 | IP4_REASSEMBLY_NEXT_DROP); |
| 857 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_duplicate_fragment); |
| 858 | ASSERT (rm->buffers_n > 0); |
| 859 | --rm->buffers_n; |
| 860 | } |
| 861 | |
| 862 | while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0) |
| 863 | { |
| 864 | u32 bi = vec_pop (vec_drop_compress); |
| 865 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 866 | b->error = node->errors[IP4_ERROR_NONE]; |
| 867 | to_next[0] = bi; |
| 868 | to_next += 1; |
| 869 | n_left_to_next -= 1; |
| 870 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 871 | n_left_to_next, bi, |
| 872 | IP4_REASSEMBLY_NEXT_DROP); |
| 873 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_compress); |
| 874 | ASSERT (rm->buffers_n > 0); |
| 875 | --rm->buffers_n; |
| 876 | } |
| 877 | |
| 878 | while (n_left_from > 0 && n_left_to_next > 0) |
| 879 | { |
| 880 | u32 bi0; |
| 881 | vlib_buffer_t *b0; |
| 882 | u32 next0; //, error0; |
| 883 | |
| 884 | bi0 = from[0]; |
| 885 | b0 = vlib_get_buffer (vm, bi0); |
| 886 | |
| 887 | ip4_header_t *ip0 = vlib_buffer_get_current (b0); |
| 888 | ip4_reass_key_t k; |
| 889 | k.src.as_u32 = ip0->src_address.as_u32; |
| 890 | k.dst.as_u32 = ip0->dst_address.as_u32; |
| 891 | k.xx_id = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 892 | k.frag_id = ip0->fragment_id; |
| 893 | k.proto = ip0->protocol; |
| 894 | k.unused = 0; |
| 895 | ip4_reass_t *reass = |
| 896 | ip4_reass_find_or_create (vm, rm, &k, &vec_drop_timeout); |
| 897 | |
| 898 | u32 error0 = IP4_ERROR_NONE; |
| 899 | if (reass) |
| 900 | { |
| 901 | ip4_reass_update (vm, node, rm, reass, &bi0, &next0, &error0, |
| 902 | &vec_drop_overlap, &vec_drop_compress, |
| 903 | IP4_REASSEMBLY_NEXT_INPUT, |
| 904 | IP4_REASSEMBLY_NEXT_DROP); |
| 905 | } |
| 906 | else |
| 907 | { |
| 908 | next0 = IP4_REASSEMBLY_NEXT_DROP; |
| 909 | error0 = IP4_ERROR_REASS_LIMIT_REACHED; |
| 910 | } |
| 911 | |
| 912 | b0->error = node->errors[error0]; |
| 913 | |
| 914 | if (bi0 != ~0) |
| 915 | { |
| 916 | to_next[0] = bi0; |
| 917 | to_next += 1; |
| 918 | n_left_to_next -= 1; |
| 919 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 920 | n_left_to_next, bi0, next0); |
| 921 | IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next); |
| 922 | } |
| 923 | |
| 924 | from += 1; |
| 925 | n_left_from -= 1; |
| 926 | } |
| 927 | |
| 928 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 929 | } |
| 930 | |
| 931 | return frame->n_vectors; |
| 932 | } |
| 933 | |
| 934 | static char *ip4_reassembly_error_strings[] = { |
| 935 | #define _(sym, string) string, |
| 936 | foreach_ip4_error |
| 937 | #undef _ |
| 938 | }; |
| 939 | |
| 940 | /* *INDENT-OFF* */ |
| 941 | VLIB_REGISTER_NODE (ip4_reass_node, static) = { |
| 942 | .function = ip4_reassembly, |
| 943 | .name = "ip4-reassembly", |
| 944 | .vector_size = sizeof (u32), |
| 945 | .format_trace = format_ip4_reass_trace, |
| 946 | .n_errors = ARRAY_LEN (ip4_reassembly_error_strings), |
| 947 | .error_strings = ip4_reassembly_error_strings, |
| 948 | .n_next_nodes = IP4_REASSEMBLY_N_NEXT, |
| 949 | .next_nodes = |
| 950 | { |
| 951 | [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input", |
| 952 | [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop", |
| 953 | }, |
| 954 | }; |
| 955 | /* *INDENT-ON* */ |
| 956 | |
| 957 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node, ip4_reassembly) |
| 958 | static u32 ip4_reass_get_nbuckets () |
| 959 | { |
| 960 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 961 | u32 nbuckets; |
| 962 | u8 i; |
| 963 | |
| 964 | nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR); |
| 965 | |
| 966 | for (i = 0; i < 31; i++) |
| 967 | if ((1 << i) >= nbuckets) |
| 968 | break; |
| 969 | nbuckets = 1 << i; |
| 970 | |
| 971 | return nbuckets; |
| 972 | } |
| 973 | |
| 974 | typedef enum |
| 975 | { |
| 976 | IP4_EVENT_CONFIG_CHANGED = 1, |
| 977 | } ip4_reass_event_t; |
| 978 | |
| 979 | typedef struct |
| 980 | { |
| 981 | int failure; |
| 982 | clib_bihash_24_8_t *new_hash; |
| 983 | } ip4_rehash_cb_ctx; |
| 984 | |
| 985 | void |
| 986 | ip4_rehash_cb (clib_bihash_kv_24_8_t * kv, void *_ctx) |
| 987 | { |
| 988 | ip4_rehash_cb_ctx *ctx = _ctx; |
| 989 | if (clib_bihash_add_del_24_8 (ctx->new_hash, kv, 1)) |
| 990 | { |
| 991 | ctx->failure = 1; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | vnet_api_error_t |
| 996 | ip4_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 997 | u32 expire_walk_interval_ms) |
| 998 | { |
| 999 | u32 old_nbuckets = ip4_reass_get_nbuckets (); |
| 1000 | ip4_reass_main.timeout_ms = timeout_ms; |
| 1001 | ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 1002 | ip4_reass_main.max_reass_n = max_reassemblies; |
| 1003 | ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1004 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1005 | vlib_process_signal_event (ip4_reass_main.vlib_main, |
| 1006 | ip4_reass_main.ip4_reass_expire_node_idx, |
| 1007 | IP4_EVENT_CONFIG_CHANGED, 0); |
| 1008 | u32 new_nbuckets = ip4_reass_get_nbuckets (); |
| 1009 | if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > 1 && |
| 1010 | new_nbuckets != old_nbuckets) |
| 1011 | { |
| 1012 | clib_bihash_24_8_t new_hash; |
| 1013 | memset (&new_hash, 0, sizeof (new_hash)); |
| 1014 | ip4_rehash_cb_ctx ctx; |
| 1015 | ctx.failure = 0; |
| 1016 | ctx.new_hash = &new_hash; |
| 1017 | clib_bihash_init_24_8 (&new_hash, "ip4-reass", new_nbuckets, |
| 1018 | new_nbuckets * 1024); |
| 1019 | clib_bihash_foreach_key_value_pair_24_8 (&ip4_reass_main.hash, |
| 1020 | ip4_rehash_cb, &ctx); |
| 1021 | if (ctx.failure) |
| 1022 | { |
| 1023 | clib_bihash_free_24_8 (&new_hash); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | clib_bihash_free_24_8 (&ip4_reass_main.hash); |
| 1029 | clib_memcpy (&ip4_reass_main.hash, &new_hash, |
| 1030 | sizeof (ip4_reass_main.hash)); |
| 1031 | } |
| 1032 | } |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | vnet_api_error_t |
| 1037 | ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 1038 | u32 * expire_walk_interval_ms) |
| 1039 | { |
| 1040 | *timeout_ms = ip4_reass_main.timeout_ms; |
| 1041 | *max_reassemblies = ip4_reass_main.max_reass_n; |
| 1042 | *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms; |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | clib_error_t * |
| 1047 | ip4_reass_init_function (vlib_main_t * vm) |
| 1048 | { |
| 1049 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1050 | clib_error_t *error = 0; |
| 1051 | u32 nbuckets; |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1052 | vlib_node_t *node; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1053 | |
| 1054 | rm->vlib_main = vm; |
| 1055 | rm->vnet_main = vnet_get_main (); |
| 1056 | |
| 1057 | rm->reass_n = 0; |
| 1058 | pool_alloc (rm->pool, rm->max_reass_n); |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1059 | |
| 1060 | node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk"); |
| 1061 | ASSERT (node); |
| 1062 | rm->ip4_reass_expire_node_idx = node->index; |
| 1063 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1064 | ip4_reass_set (IP4_REASS_TIMEOUT_DEFAULT_MS, |
| 1065 | IP4_REASS_MAX_REASSEMBLIES_DEAFULT, |
| 1066 | IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1067 | |
| 1068 | nbuckets = ip4_reass_get_nbuckets (); |
| 1069 | clib_bihash_init_24_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024); |
| 1070 | |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1071 | node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop"); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1072 | ASSERT (node); |
| 1073 | rm->ip4_drop_idx = node->index; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1074 | return error; |
| 1075 | } |
| 1076 | |
| 1077 | VLIB_INIT_FUNCTION (ip4_reass_init_function); |
| 1078 | |
| 1079 | static uword |
| 1080 | ip4_reass_walk_expired (vlib_main_t * vm, |
| 1081 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 1082 | { |
| 1083 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1084 | uword event_type, *event_data = 0; |
| 1085 | |
| 1086 | while (true) |
| 1087 | { |
| 1088 | vlib_process_wait_for_event_or_clock (vm, |
| 1089 | (f64) rm->expire_walk_interval_ms |
| 1090 | / (f64) MSEC_PER_SEC); |
| 1091 | event_type = vlib_process_get_events (vm, &event_data); |
| 1092 | |
| 1093 | switch (event_type) |
| 1094 | { |
| 1095 | case ~0: /* no events => timeout */ |
| 1096 | /* nothing to do here */ |
| 1097 | break; |
| 1098 | case IP4_EVENT_CONFIG_CHANGED: |
| 1099 | break; |
| 1100 | default: |
| 1101 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1102 | break; |
| 1103 | } |
| 1104 | f64 now = vlib_time_now (vm); |
| 1105 | |
| 1106 | ip4_reass_t *reass; |
| 1107 | u32 *vec_drop_timeout = NULL; |
| 1108 | int *pool_indexes_to_free = NULL; |
| 1109 | |
| 1110 | int index; |
| 1111 | /* *INDENT-OFF* */ |
| 1112 | pool_foreach_index (index, rm->pool, ({ |
| 1113 | reass = pool_elt_at_index (rm->pool, index); |
| 1114 | if (now > reass->last_heard + rm->timeout) |
| 1115 | { |
| 1116 | vec_add1 (pool_indexes_to_free, index); |
| 1117 | } |
| 1118 | })); |
| 1119 | /* *INDENT-ON* */ |
| 1120 | int *i; |
| 1121 | /* *INDENT-OFF* */ |
| 1122 | vec_foreach (i, pool_indexes_to_free) |
| 1123 | { |
| 1124 | ip4_reass_t *reass = pool_elt_at_index (rm->pool, i[0]); |
| 1125 | ip4_reass_on_timeout (vm, rm, reass, &vec_drop_timeout); |
| 1126 | ip4_reass_free (rm, reass); |
| 1127 | } |
| 1128 | /* *INDENT-ON* */ |
| 1129 | |
| 1130 | while (vec_len (vec_drop_timeout) > 0) |
| 1131 | { |
| 1132 | vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip4_drop_idx); |
| 1133 | u32 *to_next = vlib_frame_vector_args (f); |
| 1134 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
| 1135 | u32 n_trace = 0; |
| 1136 | while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0) |
| 1137 | { |
| 1138 | u32 bi = vec_pop (vec_drop_timeout); |
| 1139 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1140 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1141 | { |
| 1142 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1143 | b->trace_index)) |
| 1144 | { |
| 1145 | /* the trace is gone, don't trace this buffer anymore */ |
| 1146 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1147 | } |
| 1148 | else |
| 1149 | { |
| 1150 | ++n_trace; |
| 1151 | } |
| 1152 | } |
| 1153 | b->error = node->errors[IP4_ERROR_REASS_TIMEOUT]; |
| 1154 | to_next[0] = bi; |
| 1155 | ++f->n_vectors; |
| 1156 | to_next += 1; |
| 1157 | n_left_to_next -= 1; |
| 1158 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout_walk); |
| 1159 | ASSERT (rm->buffers_n > 0); |
| 1160 | --rm->buffers_n; |
| 1161 | } |
| 1162 | if (PREDICT_FALSE (n_trace > 0)) |
| 1163 | { |
| 1164 | f->flags |= VLIB_FRAME_TRACE; |
| 1165 | } |
| 1166 | vlib_put_frame_to_node (vm, rm->ip4_drop_idx, f); |
| 1167 | } |
| 1168 | |
| 1169 | vec_free (pool_indexes_to_free); |
| 1170 | vec_free (vec_drop_timeout); |
| 1171 | if (event_data) |
| 1172 | { |
| 1173 | _vec_len (event_data) = 0; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | static vlib_node_registration_t ip4_reass_expire_node; |
| 1181 | |
| 1182 | /* *INDENT-OFF* */ |
| 1183 | VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = { |
| 1184 | .function = ip4_reass_walk_expired, |
| 1185 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1186 | .name = "ip4-reassembly-expire-walk", |
| 1187 | .format_trace = format_ip4_reass_trace, |
| 1188 | .n_errors = ARRAY_LEN (ip4_reassembly_error_strings), |
| 1189 | .error_strings = ip4_reassembly_error_strings, |
| 1190 | |
| 1191 | }; |
| 1192 | /* *INDENT-ON* */ |
| 1193 | |
| 1194 | static u8 * |
| 1195 | format_ip4_reass_key (u8 * s, va_list * args) |
| 1196 | { |
| 1197 | ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *); |
| 1198 | s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1199 | key->xx_id, format_ip4_address, &key->src, format_ip4_address, |
| 1200 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
| 1201 | return s; |
| 1202 | } |
| 1203 | |
| 1204 | static u8 * |
| 1205 | format_ip4_reass (u8 * s, va_list * args) |
| 1206 | { |
| 1207 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1208 | ip4_reass_t *reass = va_arg (*args, ip4_reass_t *); |
| 1209 | |
| 1210 | s = format (s, "ID: %u, key: %U\n first_bi: %u, data_len: %u, " |
| 1211 | "last_packet_octet: %u, trace_op_counter: %u\n", |
| 1212 | reass->id, format_ip4_reass_key, &reass->key, reass->first_bi, |
| 1213 | reass->data_len, reass->last_packet_octet, |
| 1214 | reass->trace_op_counter); |
| 1215 | u32 bi = reass->first_bi; |
| 1216 | u32 counter = 0; |
| 1217 | while (~0 != bi) |
| 1218 | { |
| 1219 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1220 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 1221 | s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, " |
| 1222 | "fragment[%u, %u]\n", |
| 1223 | counter, vnb->ip.reass.range_first, |
| 1224 | vnb->ip.reass.range_last, bi, |
| 1225 | ip4_reass_buffer_get_data_offset_no_check (b), |
| 1226 | ip4_reass_buffer_get_data_len_no_check (b), |
| 1227 | vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last); |
| 1228 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1229 | { |
| 1230 | bi = b->next_buffer; |
| 1231 | } |
| 1232 | else |
| 1233 | { |
| 1234 | bi = ~0; |
| 1235 | } |
| 1236 | } |
| 1237 | return s; |
| 1238 | } |
| 1239 | |
| 1240 | static clib_error_t * |
| 1241 | show_ip4_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1242 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1243 | { |
| 1244 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1245 | |
| 1246 | vlib_cli_output (vm, "---------------------"); |
| 1247 | vlib_cli_output (vm, "IP4 reassembly status"); |
| 1248 | vlib_cli_output (vm, "---------------------"); |
| 1249 | if (unformat (input, "details")) |
| 1250 | { |
| 1251 | ip4_reass_t *reass; |
| 1252 | /* *INDENT-OFF* */ |
| 1253 | pool_foreach (reass, rm->pool, { |
| 1254 | vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass); |
| 1255 | }); |
| 1256 | /* *INDENT-ON* */ |
| 1257 | } |
| 1258 | vlib_cli_output (vm, "---------------------"); |
| 1259 | vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n", rm->reass_n); |
| 1260 | vlib_cli_output (vm, |
| 1261 | "Maximum configured concurrent IP4 reassemblies: %lu\n", |
| 1262 | (long unsigned) rm->max_reass_n); |
| 1263 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
| 1264 | (long unsigned) rm->buffers_n); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | /* *INDENT-OFF* */ |
| 1269 | VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = { |
| 1270 | .path = "show ip4-reassembly", |
| 1271 | .short_help = "show ip4-reassembly [details]", |
| 1272 | .function = show_ip4_reass, |
| 1273 | }; |
| 1274 | /* *INDENT-ON* */ |
| 1275 | |
| 1276 | /* |
| 1277 | * fd.io coding-style-patch-verification: ON |
| 1278 | * |
| 1279 | * Local Variables: |
| 1280 | * eval: (c-set-style "gnu") |
| 1281 | * End: |
| 1282 | */ |