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