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