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); |
| 415 | } |
| 416 | u32 keep_data = |
| 417 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end; |
| 418 | while (1) |
| 419 | { |
| 420 | ++buf_cnt; |
| 421 | if (trim_front) |
| 422 | { |
| 423 | if (trim_front > tmp->current_length) |
| 424 | { |
| 425 | /* drop whole buffer */ |
| 426 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 427 | ++dropped_cnt; |
| 428 | trim_front -= tmp->current_length; |
| 429 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 430 | tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 431 | tmp_bi = tmp->next_buffer; |
| 432 | tmp = vlib_get_buffer (vm, tmp_bi); |
| 433 | continue; |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | vlib_buffer_advance (tmp, trim_front); |
| 438 | trim_front = 0; |
| 439 | } |
| 440 | } |
| 441 | if (keep_data) |
| 442 | { |
| 443 | if (last_b) |
| 444 | { |
| 445 | last_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 446 | last_b->next_buffer = tmp_bi; |
| 447 | } |
| 448 | last_b = tmp; |
| 449 | if (keep_data <= tmp->current_length) |
| 450 | { |
| 451 | tmp->current_length = keep_data; |
| 452 | keep_data = 0; |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | keep_data -= tmp->current_length; |
| 457 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 458 | } |
| 459 | total_length += tmp->current_length; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | vec_add1 (*vec_drop_overlap, tmp_bi); |
| 464 | ASSERT (reass->first_bi != tmp_bi); |
| 465 | ++dropped_cnt; |
| 466 | } |
| 467 | if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 468 | { |
| 469 | tmp_bi = tmp->next_buffer; |
| 470 | tmp = vlib_get_buffer (vm, tmp->next_buffer); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | sub_chain_bi = |
| 478 | vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip. |
| 479 | reass.next_range_bi; |
| 480 | } |
| 481 | while (~0 != sub_chain_bi); |
| 482 | last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 483 | ASSERT (rm->buffers_n >= (buf_cnt - dropped_cnt)); |
| 484 | rm->buffers_n -= buf_cnt - dropped_cnt; |
| 485 | ASSERT (total_length >= first_b->current_length); |
| 486 | total_length -= first_b->current_length; |
| 487 | first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 488 | first_b->total_length_not_including_first_buffer = total_length; |
| 489 | ip4_header_t *ip = vlib_buffer_get_current (first_b); |
| 490 | ip->flags_and_fragment_offset = 0; |
| 491 | ip->length = clib_host_to_net_u16 (first_b->current_length + total_length); |
| 492 | ip->checksum = ip4_header_checksum (ip); |
| 493 | vlib_buffer_chain_compress (vm, first_b, vec_drop_compress); |
| 494 | if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 495 | { |
| 496 | ip4_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0); |
| 497 | #if 0 |
| 498 | // following code does a hexdump of packet fragments to stdout ... |
| 499 | do |
| 500 | { |
| 501 | u32 bi = reass->first_bi; |
| 502 | u8 *s = NULL; |
| 503 | while (~0 != bi) |
| 504 | { |
| 505 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 506 | s = format (s, "%u: %U\n", bi, format_hexdump, |
| 507 | vlib_buffer_get_current (b), b->current_length); |
| 508 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 509 | { |
| 510 | bi = b->next_buffer; |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | printf ("%.*s\n", vec_len (s), s); |
| 518 | fflush (stdout); |
| 519 | vec_free (s); |
| 520 | } |
| 521 | while (0); |
| 522 | #endif |
| 523 | } |
| 524 | *bi0 = reass->first_bi; |
| 525 | *next0 = next_input; |
| 526 | *error0 = IP4_ERROR_NONE; |
| 527 | ip4_reass_free (rm, reass); |
| 528 | reass = NULL; |
| 529 | } |
| 530 | |
| 531 | static u32 |
| 532 | ip4_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b) |
| 533 | { |
| 534 | u32 len = 0; |
| 535 | while (b) |
| 536 | { |
| 537 | ++len; |
| 538 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 539 | { |
| 540 | b = vlib_get_buffer (vm, b->next_buffer); |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | return len; |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | ip4_reass_insert_range_in_chain (vlib_main_t * vm, |
| 552 | ip4_reass_main_t * rm, |
| 553 | ip4_reass_t * reass, |
| 554 | u32 prev_range_bi, u32 new_next_bi) |
| 555 | { |
| 556 | |
| 557 | vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi); |
| 558 | vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b); |
| 559 | if (~0 != prev_range_bi) |
| 560 | { |
| 561 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 562 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 563 | new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi; |
| 564 | prev_vnb->ip.reass.next_range_bi = new_next_bi; |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | if (~0 != reass->first_bi) |
| 569 | { |
| 570 | new_next_vnb->ip.reass.next_range_bi = reass->first_bi; |
| 571 | } |
| 572 | reass->first_bi = new_next_bi; |
| 573 | } |
| 574 | reass->data_len += ip4_reass_buffer_get_data_len (new_next_b); |
| 575 | rm->buffers_n += ip4_reass_get_buffer_chain_length (vm, new_next_b); |
| 576 | } |
| 577 | |
| 578 | static void |
| 579 | ip4_reass_remove_range_from_chain (vlib_main_t * vm, |
| 580 | vlib_node_runtime_t * node, |
| 581 | ip4_reass_main_t * rm, |
| 582 | u32 ** vec_drop_overlap, |
| 583 | ip4_reass_t * reass, u32 prev_range_bi, |
| 584 | u32 discard_bi) |
| 585 | { |
| 586 | vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi); |
| 587 | vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b); |
| 588 | if (~0 != prev_range_bi) |
| 589 | { |
| 590 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 591 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 592 | ASSERT (prev_vnb->ip.reass.next_range_bi == discard_bi); |
| 593 | prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi; |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | reass->first_bi = discard_vnb->ip.reass.next_range_bi; |
| 598 | } |
| 599 | reass->data_len -= ip4_reass_buffer_get_data_len (discard_b); |
| 600 | while (1) |
| 601 | { |
| 602 | vec_add1 (*vec_drop_overlap, discard_bi); |
| 603 | if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 604 | { |
| 605 | ip4_reass_add_trace (vm, node, rm, reass, discard_bi, RANGE_DISCARD, |
| 606 | 0); |
| 607 | } |
| 608 | if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 609 | { |
| 610 | discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 611 | discard_bi = discard_b->next_buffer; |
| 612 | discard_b = vlib_get_buffer (vm, discard_bi); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | ip4_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 623 | ip4_reass_main_t * rm, ip4_reass_t * reass, u32 * bi0, |
| 624 | u32 * next0, vlib_error_t * error0, |
| 625 | u32 ** vec_drop_overlap, u32 ** vec_drop_compress, |
| 626 | u32 next_input, u32 next_drop) |
| 627 | { |
| 628 | int consumed = 0; |
| 629 | vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0); |
| 630 | ip4_header_t *fip = vlib_buffer_get_current (fb); |
| 631 | ASSERT (fb->current_length >= sizeof (*fip)); |
| 632 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
| 633 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 634 | ip4_get_fragment_offset_bytes (fip); |
| 635 | u32 fragment_length = |
| 636 | clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip); |
| 637 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 638 | fragment_first + fragment_length - 1; |
| 639 | int more_fragments = ip4_get_fragment_more (fip); |
| 640 | u32 candidate_range_bi = reass->first_bi; |
| 641 | u32 prev_range_bi = ~0; |
| 642 | fvnb->ip.reass.range_first = fragment_first; |
| 643 | fvnb->ip.reass.range_last = fragment_last; |
| 644 | fvnb->ip.reass.next_range_bi = ~0; |
| 645 | if (!more_fragments) |
| 646 | { |
| 647 | reass->last_packet_octet = fragment_last; |
| 648 | } |
| 649 | if (~0 == reass->first_bi) |
| 650 | { |
| 651 | // starting a new reassembly |
| 652 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, *bi0); |
| 653 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 654 | { |
| 655 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 656 | } |
| 657 | *bi0 = ~0; |
| 658 | fvnb->ip.reass.estimated_mtu = clib_net_to_host_u16 (fip->length); |
| 659 | return; |
| 660 | } |
| 661 | fvnb->ip.reass.estimated_mtu = clib_min (clib_net_to_host_u16 (fip->length), |
| 662 | fvnb->ip.reass.estimated_mtu); |
| 663 | while (~0 != candidate_range_bi) |
| 664 | { |
| 665 | vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi); |
| 666 | vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b); |
| 667 | if (fragment_first > candidate_vnb->ip.reass.range_last) |
| 668 | { |
| 669 | // this fragments starts after candidate range |
| 670 | prev_range_bi = candidate_range_bi; |
| 671 | candidate_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 672 | if (candidate_vnb->ip.reass.range_last < fragment_last && |
| 673 | ~0 == candidate_range_bi) |
| 674 | { |
| 675 | // special case - this fragment falls beyond all known ranges |
| 676 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 677 | *bi0); |
| 678 | consumed = 1; |
| 679 | break; |
| 680 | } |
| 681 | continue; |
| 682 | } |
| 683 | if (fragment_last < candidate_vnb->ip.reass.range_first) |
| 684 | { |
| 685 | // this fragment ends before candidate range without any overlap |
| 686 | ip4_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 687 | *bi0); |
| 688 | consumed = 1; |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | if (fragment_first >= candidate_vnb->ip.reass.range_first && |
| 693 | fragment_last <= candidate_vnb->ip.reass.range_last) |
| 694 | { |
| 695 | // this fragment is a (sub)part of existing range, ignore it |
| 696 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 697 | { |
| 698 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, |
| 699 | RANGE_OVERLAP, 0); |
| 700 | } |
| 701 | break; |
| 702 | } |
| 703 | int discard_candidate = 0; |
| 704 | if (fragment_first < candidate_vnb->ip.reass.range_first) |
| 705 | { |
| 706 | u32 overlap = |
| 707 | fragment_last - candidate_vnb->ip.reass.range_first + 1; |
| 708 | if (overlap < ip4_reass_buffer_get_data_len (candidate_b)) |
| 709 | { |
| 710 | candidate_vnb->ip.reass.range_first += overlap; |
| 711 | ASSERT (reass->data_len >= overlap); |
| 712 | reass->data_len -= overlap; |
| 713 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 714 | { |
| 715 | ip4_reass_add_trace (vm, node, rm, reass, |
| 716 | candidate_range_bi, RANGE_SHRINK, |
| 717 | overlap); |
| 718 | } |
| 719 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 720 | prev_range_bi, *bi0); |
| 721 | consumed = 1; |
| 722 | } |
| 723 | else |
| 724 | { |
| 725 | discard_candidate = 1; |
| 726 | } |
| 727 | } |
| 728 | else if (fragment_last > candidate_vnb->ip.reass.range_last) |
| 729 | { |
| 730 | u32 overlap = |
| 731 | candidate_vnb->ip.reass.range_last - fragment_first + 1; |
| 732 | if (overlap < ip4_reass_buffer_get_data_len (candidate_b)) |
| 733 | { |
| 734 | fvnb->ip.reass.range_first += overlap; |
| 735 | if (~0 != candidate_vnb->ip.reass.next_range_bi) |
| 736 | { |
| 737 | prev_range_bi = candidate_range_bi; |
| 738 | candidate_range_bi = |
| 739 | candidate_vnb->ip.reass.next_range_bi; |
| 740 | continue; |
| 741 | } |
| 742 | else |
| 743 | { |
| 744 | // special case - last range discarded |
| 745 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 746 | candidate_range_bi, |
| 747 | *bi0); |
| 748 | consumed = 1; |
| 749 | } |
| 750 | } |
| 751 | else |
| 752 | { |
| 753 | discard_candidate = 1; |
| 754 | } |
| 755 | } |
| 756 | else |
| 757 | { |
| 758 | discard_candidate = 1; |
| 759 | } |
| 760 | if (discard_candidate) |
| 761 | { |
| 762 | u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 763 | // discard candidate range, probe next range |
| 764 | ip4_reass_remove_range_from_chain (vm, node, rm, |
| 765 | vec_drop_overlap, reass, |
| 766 | prev_range_bi, |
| 767 | candidate_range_bi); |
| 768 | if (~0 != next_range_bi) |
| 769 | { |
| 770 | candidate_range_bi = next_range_bi; |
| 771 | continue; |
| 772 | } |
| 773 | else |
| 774 | { |
| 775 | // special case - last range discarded |
| 776 | ip4_reass_insert_range_in_chain (vm, rm, reass, |
| 777 | prev_range_bi, *bi0); |
| 778 | consumed = 1; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | break; |
| 783 | } |
| 784 | if (consumed) |
| 785 | { |
| 786 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 787 | { |
| 788 | ip4_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 789 | } |
| 790 | } |
| 791 | if (~0 != reass->last_packet_octet && |
| 792 | reass->data_len == reass->last_packet_octet + 1) |
| 793 | { |
| 794 | ip4_reass_finalize (vm, node, rm, reass, bi0, next0, error0, next_input, |
| 795 | vec_drop_compress, vec_drop_overlap); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | if (consumed) |
| 800 | { |
| 801 | *bi0 = ~0; |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | *next0 = next_drop; |
| 806 | *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | always_inline uword |
| 812 | ip4_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 813 | vlib_frame_t * frame) |
| 814 | { |
| 815 | u32 *from = vlib_frame_vector_args (frame); |
| 816 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 817 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 818 | |
| 819 | n_left_from = frame->n_vectors; |
| 820 | next_index = node->cached_next_index; |
| 821 | static u32 *vec_drop_timeout = NULL; // indexes of buffers which timed out |
| 822 | static u32 *vec_drop_overlap = NULL; // indexes of buffers which were discarded due to overlap |
| 823 | static u32 *vec_drop_compress = NULL; // indexes of buffers dicarded due to buffer compression |
| 824 | while (n_left_from > 0 || vec_len (vec_drop_timeout) > 0 || |
| 825 | vec_len (vec_drop_overlap) > 0) |
| 826 | { |
| 827 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 828 | |
| 829 | while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0) |
| 830 | { |
| 831 | u32 bi = vec_pop (vec_drop_timeout); |
| 832 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 833 | b->error = node->errors[IP4_ERROR_REASS_TIMEOUT]; |
| 834 | to_next[0] = bi; |
| 835 | to_next += 1; |
| 836 | n_left_to_next -= 1; |
| 837 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 838 | n_left_to_next, bi, |
| 839 | IP4_REASSEMBLY_NEXT_DROP); |
| 840 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout); |
| 841 | ASSERT (rm->buffers_n > 0); |
| 842 | --rm->buffers_n; |
| 843 | } |
| 844 | |
| 845 | while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0) |
| 846 | { |
| 847 | u32 bi = vec_pop (vec_drop_overlap); |
| 848 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 849 | b->error = node->errors[IP4_ERROR_REASS_DUPLICATE_FRAGMENT]; |
| 850 | to_next[0] = bi; |
| 851 | to_next += 1; |
| 852 | n_left_to_next -= 1; |
| 853 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 854 | n_left_to_next, bi, |
| 855 | IP4_REASSEMBLY_NEXT_DROP); |
| 856 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_duplicate_fragment); |
| 857 | ASSERT (rm->buffers_n > 0); |
| 858 | --rm->buffers_n; |
| 859 | } |
| 860 | |
| 861 | while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0) |
| 862 | { |
| 863 | u32 bi = vec_pop (vec_drop_compress); |
| 864 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 865 | b->error = node->errors[IP4_ERROR_NONE]; |
| 866 | to_next[0] = bi; |
| 867 | to_next += 1; |
| 868 | n_left_to_next -= 1; |
| 869 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 870 | n_left_to_next, bi, |
| 871 | IP4_REASSEMBLY_NEXT_DROP); |
| 872 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_compress); |
| 873 | ASSERT (rm->buffers_n > 0); |
| 874 | --rm->buffers_n; |
| 875 | } |
| 876 | |
| 877 | while (n_left_from > 0 && n_left_to_next > 0) |
| 878 | { |
| 879 | u32 bi0; |
| 880 | vlib_buffer_t *b0; |
| 881 | u32 next0; //, error0; |
| 882 | |
| 883 | bi0 = from[0]; |
| 884 | b0 = vlib_get_buffer (vm, bi0); |
| 885 | |
| 886 | ip4_header_t *ip0 = vlib_buffer_get_current (b0); |
| 887 | ip4_reass_key_t k; |
| 888 | k.src.as_u32 = ip0->src_address.as_u32; |
| 889 | k.dst.as_u32 = ip0->dst_address.as_u32; |
| 890 | k.xx_id = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 891 | k.frag_id = ip0->fragment_id; |
| 892 | k.proto = ip0->protocol; |
| 893 | k.unused = 0; |
| 894 | ip4_reass_t *reass = |
| 895 | ip4_reass_find_or_create (vm, rm, &k, &vec_drop_timeout); |
| 896 | |
| 897 | u32 error0 = IP4_ERROR_NONE; |
| 898 | if (reass) |
| 899 | { |
| 900 | ip4_reass_update (vm, node, rm, reass, &bi0, &next0, &error0, |
| 901 | &vec_drop_overlap, &vec_drop_compress, |
| 902 | IP4_REASSEMBLY_NEXT_INPUT, |
| 903 | IP4_REASSEMBLY_NEXT_DROP); |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | next0 = IP4_REASSEMBLY_NEXT_DROP; |
| 908 | error0 = IP4_ERROR_REASS_LIMIT_REACHED; |
| 909 | } |
| 910 | |
| 911 | b0->error = node->errors[error0]; |
| 912 | |
| 913 | if (bi0 != ~0) |
| 914 | { |
| 915 | to_next[0] = bi0; |
| 916 | to_next += 1; |
| 917 | n_left_to_next -= 1; |
| 918 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 919 | n_left_to_next, bi0, next0); |
| 920 | IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next); |
| 921 | } |
| 922 | |
| 923 | from += 1; |
| 924 | n_left_from -= 1; |
| 925 | } |
| 926 | |
| 927 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 928 | } |
| 929 | |
| 930 | return frame->n_vectors; |
| 931 | } |
| 932 | |
| 933 | static char *ip4_reassembly_error_strings[] = { |
| 934 | #define _(sym, string) string, |
| 935 | foreach_ip4_error |
| 936 | #undef _ |
| 937 | }; |
| 938 | |
| 939 | /* *INDENT-OFF* */ |
| 940 | VLIB_REGISTER_NODE (ip4_reass_node, static) = { |
| 941 | .function = ip4_reassembly, |
| 942 | .name = "ip4-reassembly", |
| 943 | .vector_size = sizeof (u32), |
| 944 | .format_trace = format_ip4_reass_trace, |
| 945 | .n_errors = ARRAY_LEN (ip4_reassembly_error_strings), |
| 946 | .error_strings = ip4_reassembly_error_strings, |
| 947 | .n_next_nodes = IP4_REASSEMBLY_N_NEXT, |
| 948 | .next_nodes = |
| 949 | { |
| 950 | [IP4_REASSEMBLY_NEXT_INPUT] = "ip4-input", |
| 951 | [IP4_REASSEMBLY_NEXT_DROP] = "ip4-drop", |
| 952 | }, |
| 953 | }; |
| 954 | /* *INDENT-ON* */ |
| 955 | |
| 956 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_reass_node, ip4_reassembly) |
| 957 | static u32 ip4_reass_get_nbuckets () |
| 958 | { |
| 959 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 960 | u32 nbuckets; |
| 961 | u8 i; |
| 962 | |
| 963 | nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR); |
| 964 | |
| 965 | for (i = 0; i < 31; i++) |
| 966 | if ((1 << i) >= nbuckets) |
| 967 | break; |
| 968 | nbuckets = 1 << i; |
| 969 | |
| 970 | return nbuckets; |
| 971 | } |
| 972 | |
| 973 | typedef enum |
| 974 | { |
| 975 | IP4_EVENT_CONFIG_CHANGED = 1, |
| 976 | } ip4_reass_event_t; |
| 977 | |
| 978 | typedef struct |
| 979 | { |
| 980 | int failure; |
| 981 | clib_bihash_24_8_t *new_hash; |
| 982 | } ip4_rehash_cb_ctx; |
| 983 | |
| 984 | void |
| 985 | ip4_rehash_cb (clib_bihash_kv_24_8_t * kv, void *_ctx) |
| 986 | { |
| 987 | ip4_rehash_cb_ctx *ctx = _ctx; |
| 988 | if (clib_bihash_add_del_24_8 (ctx->new_hash, kv, 1)) |
| 989 | { |
| 990 | ctx->failure = 1; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | vnet_api_error_t |
| 995 | ip4_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 996 | u32 expire_walk_interval_ms) |
| 997 | { |
| 998 | u32 old_nbuckets = ip4_reass_get_nbuckets (); |
| 999 | ip4_reass_main.timeout_ms = timeout_ms; |
| 1000 | ip4_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 1001 | ip4_reass_main.max_reass_n = max_reassemblies; |
| 1002 | ip4_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
| 1003 | vlib_process_signal_event (ip4_reass_main.vlib_main, |
| 1004 | ip4_reass_main.ip4_reass_expire_node_idx, |
| 1005 | IP4_EVENT_CONFIG_CHANGED, 0); |
| 1006 | u32 new_nbuckets = ip4_reass_get_nbuckets (); |
| 1007 | if (ip4_reass_main.max_reass_n > 0 && new_nbuckets > 1 && |
| 1008 | new_nbuckets != old_nbuckets) |
| 1009 | { |
| 1010 | clib_bihash_24_8_t new_hash; |
| 1011 | memset (&new_hash, 0, sizeof (new_hash)); |
| 1012 | ip4_rehash_cb_ctx ctx; |
| 1013 | ctx.failure = 0; |
| 1014 | ctx.new_hash = &new_hash; |
| 1015 | clib_bihash_init_24_8 (&new_hash, "ip4-reass", new_nbuckets, |
| 1016 | new_nbuckets * 1024); |
| 1017 | clib_bihash_foreach_key_value_pair_24_8 (&ip4_reass_main.hash, |
| 1018 | ip4_rehash_cb, &ctx); |
| 1019 | if (ctx.failure) |
| 1020 | { |
| 1021 | clib_bihash_free_24_8 (&new_hash); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | else |
| 1025 | { |
| 1026 | clib_bihash_free_24_8 (&ip4_reass_main.hash); |
| 1027 | clib_memcpy (&ip4_reass_main.hash, &new_hash, |
| 1028 | sizeof (ip4_reass_main.hash)); |
| 1029 | } |
| 1030 | } |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | vnet_api_error_t |
| 1035 | ip4_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 1036 | u32 * expire_walk_interval_ms) |
| 1037 | { |
| 1038 | *timeout_ms = ip4_reass_main.timeout_ms; |
| 1039 | *max_reassemblies = ip4_reass_main.max_reass_n; |
| 1040 | *expire_walk_interval_ms = ip4_reass_main.expire_walk_interval_ms; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | clib_error_t * |
| 1045 | ip4_reass_init_function (vlib_main_t * vm) |
| 1046 | { |
| 1047 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1048 | clib_error_t *error = 0; |
| 1049 | u32 nbuckets; |
| 1050 | |
| 1051 | rm->vlib_main = vm; |
| 1052 | rm->vnet_main = vnet_get_main (); |
| 1053 | |
| 1054 | rm->reass_n = 0; |
| 1055 | pool_alloc (rm->pool, rm->max_reass_n); |
| 1056 | ip4_reass_set (IP4_REASS_TIMEOUT_DEFAULT_MS, |
| 1057 | IP4_REASS_MAX_REASSEMBLIES_DEAFULT, |
| 1058 | IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1059 | |
| 1060 | nbuckets = ip4_reass_get_nbuckets (); |
| 1061 | clib_bihash_init_24_8 (&rm->hash, "ip4-reass", nbuckets, nbuckets * 1024); |
| 1062 | |
| 1063 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop"); |
| 1064 | ASSERT (node); |
| 1065 | rm->ip4_drop_idx = node->index; |
| 1066 | node = vlib_get_node_by_name (vm, (u8 *) "ip4-reassembly-expire-walk"); |
| 1067 | ASSERT (node); |
| 1068 | rm->ip4_reass_expire_node_idx = node->index; |
| 1069 | return error; |
| 1070 | } |
| 1071 | |
| 1072 | VLIB_INIT_FUNCTION (ip4_reass_init_function); |
| 1073 | |
| 1074 | static uword |
| 1075 | ip4_reass_walk_expired (vlib_main_t * vm, |
| 1076 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 1077 | { |
| 1078 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1079 | uword event_type, *event_data = 0; |
| 1080 | |
| 1081 | while (true) |
| 1082 | { |
| 1083 | vlib_process_wait_for_event_or_clock (vm, |
| 1084 | (f64) rm->expire_walk_interval_ms |
| 1085 | / (f64) MSEC_PER_SEC); |
| 1086 | event_type = vlib_process_get_events (vm, &event_data); |
| 1087 | |
| 1088 | switch (event_type) |
| 1089 | { |
| 1090 | case ~0: /* no events => timeout */ |
| 1091 | /* nothing to do here */ |
| 1092 | break; |
| 1093 | case IP4_EVENT_CONFIG_CHANGED: |
| 1094 | break; |
| 1095 | default: |
| 1096 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1097 | break; |
| 1098 | } |
| 1099 | f64 now = vlib_time_now (vm); |
| 1100 | |
| 1101 | ip4_reass_t *reass; |
| 1102 | u32 *vec_drop_timeout = NULL; |
| 1103 | int *pool_indexes_to_free = NULL; |
| 1104 | |
| 1105 | int index; |
| 1106 | /* *INDENT-OFF* */ |
| 1107 | pool_foreach_index (index, rm->pool, ({ |
| 1108 | reass = pool_elt_at_index (rm->pool, index); |
| 1109 | if (now > reass->last_heard + rm->timeout) |
| 1110 | { |
| 1111 | vec_add1 (pool_indexes_to_free, index); |
| 1112 | } |
| 1113 | })); |
| 1114 | /* *INDENT-ON* */ |
| 1115 | int *i; |
| 1116 | /* *INDENT-OFF* */ |
| 1117 | vec_foreach (i, pool_indexes_to_free) |
| 1118 | { |
| 1119 | ip4_reass_t *reass = pool_elt_at_index (rm->pool, i[0]); |
| 1120 | ip4_reass_on_timeout (vm, rm, reass, &vec_drop_timeout); |
| 1121 | ip4_reass_free (rm, reass); |
| 1122 | } |
| 1123 | /* *INDENT-ON* */ |
| 1124 | |
| 1125 | while (vec_len (vec_drop_timeout) > 0) |
| 1126 | { |
| 1127 | vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip4_drop_idx); |
| 1128 | u32 *to_next = vlib_frame_vector_args (f); |
| 1129 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
| 1130 | u32 n_trace = 0; |
| 1131 | while (vec_len (vec_drop_timeout) > 0 && n_left_to_next > 0) |
| 1132 | { |
| 1133 | u32 bi = vec_pop (vec_drop_timeout); |
| 1134 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1135 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1136 | { |
| 1137 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1138 | b->trace_index)) |
| 1139 | { |
| 1140 | /* the trace is gone, don't trace this buffer anymore */ |
| 1141 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | ++n_trace; |
| 1146 | } |
| 1147 | } |
| 1148 | b->error = node->errors[IP4_ERROR_REASS_TIMEOUT]; |
| 1149 | to_next[0] = bi; |
| 1150 | ++f->n_vectors; |
| 1151 | to_next += 1; |
| 1152 | n_left_to_next -= 1; |
| 1153 | IP4_REASS_DEBUG_BUFFER (bi, enqueue_drop_timeout_walk); |
| 1154 | ASSERT (rm->buffers_n > 0); |
| 1155 | --rm->buffers_n; |
| 1156 | } |
| 1157 | if (PREDICT_FALSE (n_trace > 0)) |
| 1158 | { |
| 1159 | f->flags |= VLIB_FRAME_TRACE; |
| 1160 | } |
| 1161 | vlib_put_frame_to_node (vm, rm->ip4_drop_idx, f); |
| 1162 | } |
| 1163 | |
| 1164 | vec_free (pool_indexes_to_free); |
| 1165 | vec_free (vec_drop_timeout); |
| 1166 | if (event_data) |
| 1167 | { |
| 1168 | _vec_len (event_data) = 0; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | static vlib_node_registration_t ip4_reass_expire_node; |
| 1176 | |
| 1177 | /* *INDENT-OFF* */ |
| 1178 | VLIB_REGISTER_NODE (ip4_reass_expire_node, static) = { |
| 1179 | .function = ip4_reass_walk_expired, |
| 1180 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1181 | .name = "ip4-reassembly-expire-walk", |
| 1182 | .format_trace = format_ip4_reass_trace, |
| 1183 | .n_errors = ARRAY_LEN (ip4_reassembly_error_strings), |
| 1184 | .error_strings = ip4_reassembly_error_strings, |
| 1185 | |
| 1186 | }; |
| 1187 | /* *INDENT-ON* */ |
| 1188 | |
| 1189 | static u8 * |
| 1190 | format_ip4_reass_key (u8 * s, va_list * args) |
| 1191 | { |
| 1192 | ip4_reass_key_t *key = va_arg (*args, ip4_reass_key_t *); |
| 1193 | s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1194 | key->xx_id, format_ip4_address, &key->src, format_ip4_address, |
| 1195 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
| 1196 | return s; |
| 1197 | } |
| 1198 | |
| 1199 | static u8 * |
| 1200 | format_ip4_reass (u8 * s, va_list * args) |
| 1201 | { |
| 1202 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1203 | ip4_reass_t *reass = va_arg (*args, ip4_reass_t *); |
| 1204 | |
| 1205 | s = format (s, "ID: %u, key: %U\n first_bi: %u, data_len: %u, " |
| 1206 | "last_packet_octet: %u, trace_op_counter: %u\n", |
| 1207 | reass->id, format_ip4_reass_key, &reass->key, reass->first_bi, |
| 1208 | reass->data_len, reass->last_packet_octet, |
| 1209 | reass->trace_op_counter); |
| 1210 | u32 bi = reass->first_bi; |
| 1211 | u32 counter = 0; |
| 1212 | while (~0 != bi) |
| 1213 | { |
| 1214 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1215 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 1216 | s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, " |
| 1217 | "fragment[%u, %u]\n", |
| 1218 | counter, vnb->ip.reass.range_first, |
| 1219 | vnb->ip.reass.range_last, bi, |
| 1220 | ip4_reass_buffer_get_data_offset_no_check (b), |
| 1221 | ip4_reass_buffer_get_data_len_no_check (b), |
| 1222 | vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last); |
| 1223 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1224 | { |
| 1225 | bi = b->next_buffer; |
| 1226 | } |
| 1227 | else |
| 1228 | { |
| 1229 | bi = ~0; |
| 1230 | } |
| 1231 | } |
| 1232 | return s; |
| 1233 | } |
| 1234 | |
| 1235 | static clib_error_t * |
| 1236 | show_ip4_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1237 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1238 | { |
| 1239 | ip4_reass_main_t *rm = &ip4_reass_main; |
| 1240 | |
| 1241 | vlib_cli_output (vm, "---------------------"); |
| 1242 | vlib_cli_output (vm, "IP4 reassembly status"); |
| 1243 | vlib_cli_output (vm, "---------------------"); |
| 1244 | if (unformat (input, "details")) |
| 1245 | { |
| 1246 | ip4_reass_t *reass; |
| 1247 | /* *INDENT-OFF* */ |
| 1248 | pool_foreach (reass, rm->pool, { |
| 1249 | vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass); |
| 1250 | }); |
| 1251 | /* *INDENT-ON* */ |
| 1252 | } |
| 1253 | vlib_cli_output (vm, "---------------------"); |
| 1254 | vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n", rm->reass_n); |
| 1255 | vlib_cli_output (vm, |
| 1256 | "Maximum configured concurrent IP4 reassemblies: %lu\n", |
| 1257 | (long unsigned) rm->max_reass_n); |
| 1258 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
| 1259 | (long unsigned) rm->buffers_n); |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | /* *INDENT-OFF* */ |
| 1264 | VLIB_CLI_COMMAND (show_ip4_reassembly_cmd, static) = { |
| 1265 | .path = "show ip4-reassembly", |
| 1266 | .short_help = "show ip4-reassembly [details]", |
| 1267 | .function = show_ip4_reass, |
| 1268 | }; |
| 1269 | /* *INDENT-ON* */ |
| 1270 | |
| 1271 | /* |
| 1272 | * fd.io coding-style-patch-verification: ON |
| 1273 | * |
| 1274 | * Local Variables: |
| 1275 | * eval: (c-set-style "gnu") |
| 1276 | * End: |
| 1277 | */ |