Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | * pg_input.c: buffer generator input |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vnet/pg/pg.h> |
| 42 | #include <vnet/vnet.h> |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 43 | #include <vnet/feature/feature.h> |
| 44 | #include <vnet/devices/devices.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | static int |
| 47 | validate_buffer_data2 (vlib_buffer_t * b, pg_stream_t * s, |
| 48 | u32 data_offset, u32 n_bytes) |
| 49 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 50 | u8 *bd, *pd, *pm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u32 i; |
| 52 | |
| 53 | bd = b->data; |
| 54 | pd = s->fixed_packet_data + data_offset; |
| 55 | pm = s->fixed_packet_data_mask + data_offset; |
| 56 | |
| 57 | if (pd + n_bytes >= vec_end (s->fixed_packet_data)) |
| 58 | n_bytes = (pd < vec_end (s->fixed_packet_data) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 59 | ? vec_end (s->fixed_packet_data) - pd : 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
| 61 | for (i = 0; i < n_bytes; i++) |
| 62 | if ((bd[i] & pm[i]) != pd[i]) |
| 63 | break; |
| 64 | |
| 65 | if (i >= n_bytes) |
| 66 | return 1; |
| 67 | |
| 68 | clib_warning ("buffer %U", format_vlib_buffer, b); |
| 69 | clib_warning ("differ at index %d", i); |
| 70 | clib_warning ("is %U", format_hex_bytes, bd, n_bytes); |
| 71 | clib_warning ("mask %U", format_hex_bytes, pm, n_bytes); |
| 72 | clib_warning ("expect %U", format_hex_bytes, pd, n_bytes); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | validate_buffer_data (vlib_buffer_t * b, pg_stream_t * s) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 78 | { |
| 79 | return validate_buffer_data2 (b, s, 0, s->buffer_bytes); |
| 80 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
| 82 | always_inline void |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 83 | set_1 (void *a0, |
| 84 | u64 v0, u64 v_min, u64 v_max, u32 n_bits, u32 is_net_byte_order) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
| 86 | ASSERT (v0 >= v_min && v0 <= v_max); |
| 87 | if (n_bits == BITS (u8)) |
| 88 | { |
| 89 | ((u8 *) a0)[0] = v0; |
| 90 | } |
| 91 | else if (n_bits == BITS (u16)) |
| 92 | { |
| 93 | if (is_net_byte_order) |
| 94 | v0 = clib_host_to_net_u16 (v0); |
| 95 | clib_mem_unaligned (a0, u16) = v0; |
| 96 | } |
| 97 | else if (n_bits == BITS (u32)) |
| 98 | { |
| 99 | if (is_net_byte_order) |
| 100 | v0 = clib_host_to_net_u32 (v0); |
| 101 | clib_mem_unaligned (a0, u32) = v0; |
| 102 | } |
| 103 | else if (n_bits == BITS (u64)) |
| 104 | { |
| 105 | if (is_net_byte_order) |
| 106 | v0 = clib_host_to_net_u64 (v0); |
| 107 | clib_mem_unaligned (a0, u64) = v0; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | always_inline void |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 112 | set_2 (void *a0, void *a1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | u64 v0, u64 v1, |
| 114 | u64 v_min, u64 v_max, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 115 | u32 n_bits, u32 is_net_byte_order, u32 is_increment) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | { |
| 117 | ASSERT (v0 >= v_min && v0 <= v_max); |
| 118 | ASSERT (v1 >= v_min && v1 <= (v_max + is_increment)); |
| 119 | if (n_bits == BITS (u8)) |
| 120 | { |
| 121 | ((u8 *) a0)[0] = v0; |
| 122 | ((u8 *) a1)[0] = v1; |
| 123 | } |
| 124 | else if (n_bits == BITS (u16)) |
| 125 | { |
| 126 | if (is_net_byte_order) |
| 127 | { |
| 128 | v0 = clib_host_to_net_u16 (v0); |
| 129 | v1 = clib_host_to_net_u16 (v1); |
| 130 | } |
| 131 | clib_mem_unaligned (a0, u16) = v0; |
| 132 | clib_mem_unaligned (a1, u16) = v1; |
| 133 | } |
| 134 | else if (n_bits == BITS (u32)) |
| 135 | { |
| 136 | if (is_net_byte_order) |
| 137 | { |
| 138 | v0 = clib_host_to_net_u32 (v0); |
| 139 | v1 = clib_host_to_net_u32 (v1); |
| 140 | } |
| 141 | clib_mem_unaligned (a0, u32) = v0; |
| 142 | clib_mem_unaligned (a1, u32) = v1; |
| 143 | } |
| 144 | else if (n_bits == BITS (u64)) |
| 145 | { |
| 146 | if (is_net_byte_order) |
| 147 | { |
| 148 | v0 = clib_host_to_net_u64 (v0); |
| 149 | v1 = clib_host_to_net_u64 (v1); |
| 150 | } |
| 151 | clib_mem_unaligned (a0, u64) = v0; |
| 152 | clib_mem_unaligned (a1, u64) = v1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static_always_inline void |
| 157 | do_set_fixed (pg_main_t * pg, |
| 158 | pg_stream_t * s, |
| 159 | u32 * buffers, |
| 160 | u32 n_buffers, |
| 161 | u32 n_bits, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 162 | u32 byte_offset, u32 is_net_byte_order, u64 v_min, u64 v_max) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 164 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
| 166 | while (n_buffers >= 4) |
| 167 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 168 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 169 | void *a0, *a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
| 171 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 172 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 173 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 174 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 175 | buffers += 2; |
| 176 | n_buffers -= 2; |
| 177 | |
| 178 | a0 = (void *) b0 + byte_offset; |
| 179 | a1 = (void *) b1 + byte_offset; |
| 180 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 181 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 182 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 183 | set_2 (a0, a1, v_min, v_min, v_min, v_max, n_bits, is_net_byte_order, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | /* is_increment */ 0); |
| 185 | |
| 186 | ASSERT (validate_buffer_data (b0, s)); |
| 187 | ASSERT (validate_buffer_data (b1, s)); |
| 188 | } |
| 189 | |
| 190 | while (n_buffers > 0) |
| 191 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 192 | vlib_buffer_t *b0; |
| 193 | void *a0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
| 195 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 196 | buffers += 1; |
| 197 | n_buffers -= 1; |
| 198 | |
| 199 | a0 = (void *) b0 + byte_offset; |
| 200 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 201 | set_1 (a0, v_min, v_min, v_max, n_bits, is_net_byte_order); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
| 203 | ASSERT (validate_buffer_data (b0, s)); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static_always_inline u64 |
| 208 | do_set_increment (pg_main_t * pg, |
| 209 | pg_stream_t * s, |
| 210 | u32 * buffers, |
| 211 | u32 n_buffers, |
| 212 | u32 n_bits, |
| 213 | u32 byte_offset, |
| 214 | u32 is_net_byte_order, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 215 | u32 want_sum, u64 * sum_result, u64 v_min, u64 v_max, u64 v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 217 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | u64 sum = 0; |
| 219 | |
| 220 | ASSERT (v >= v_min && v <= v_max); |
| 221 | |
| 222 | while (n_buffers >= 4) |
| 223 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 224 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 225 | void *a0, *a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | u64 v_old; |
| 227 | |
| 228 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 229 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 230 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 231 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 232 | buffers += 2; |
| 233 | n_buffers -= 2; |
| 234 | |
| 235 | a0 = (void *) b0 + byte_offset; |
| 236 | a1 = (void *) b1 + byte_offset; |
| 237 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 238 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 239 | |
| 240 | v_old = v; |
| 241 | v = v_old + 2; |
| 242 | v = v > v_max ? v_min : v; |
| 243 | set_2 (a0, a1, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 244 | v_old + 0, v_old + 1, v_min, v_max, n_bits, is_net_byte_order, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | /* is_increment */ 1); |
| 246 | |
| 247 | if (want_sum) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 248 | sum += 2 * v_old + 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
| 250 | if (PREDICT_FALSE (v_old + 1 > v_max)) |
| 251 | { |
| 252 | if (want_sum) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 253 | sum -= 2 * v_old + 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
| 255 | v = v_old; |
| 256 | set_1 (a0, v + 0, v_min, v_max, n_bits, is_net_byte_order); |
| 257 | if (want_sum) |
| 258 | sum += v; |
| 259 | v += 1; |
| 260 | |
| 261 | v = v > v_max ? v_min : v; |
| 262 | set_1 (a1, v + 0, v_min, v_max, n_bits, is_net_byte_order); |
| 263 | if (want_sum) |
| 264 | sum += v; |
| 265 | v += 1; |
| 266 | } |
| 267 | |
| 268 | ASSERT (validate_buffer_data (b0, s)); |
| 269 | ASSERT (validate_buffer_data (b1, s)); |
| 270 | } |
| 271 | |
| 272 | while (n_buffers > 0) |
| 273 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 274 | vlib_buffer_t *b0; |
| 275 | void *a0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | u64 v_old; |
| 277 | |
| 278 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 279 | buffers += 1; |
| 280 | n_buffers -= 1; |
| 281 | |
| 282 | a0 = (void *) b0 + byte_offset; |
| 283 | |
| 284 | v_old = v; |
| 285 | if (want_sum) |
| 286 | sum += v_old; |
| 287 | v += 1; |
| 288 | v = v > v_max ? v_min : v; |
| 289 | |
| 290 | ASSERT (v_old >= v_min && v_old <= v_max); |
| 291 | set_1 (a0, v_old, v_min, v_max, n_bits, is_net_byte_order); |
| 292 | |
| 293 | ASSERT (validate_buffer_data (b0, s)); |
| 294 | } |
| 295 | |
| 296 | if (want_sum) |
| 297 | *sum_result = sum; |
| 298 | |
| 299 | return v; |
| 300 | } |
| 301 | |
| 302 | static_always_inline void |
| 303 | do_set_random (pg_main_t * pg, |
| 304 | pg_stream_t * s, |
| 305 | u32 * buffers, |
| 306 | u32 n_buffers, |
| 307 | u32 n_bits, |
| 308 | u32 byte_offset, |
| 309 | u32 is_net_byte_order, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 310 | u32 want_sum, u64 * sum_result, u64 v_min, u64 v_max) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 312 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | u64 v_diff = v_max - v_min + 1; |
| 314 | u64 r_mask = max_pow2 (v_diff) - 1; |
| 315 | u64 v0, v1; |
| 316 | u64 sum = 0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 317 | void *random_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | |
| 319 | random_data = clib_random_buffer_get_data |
| 320 | (&vm->random_buffer, n_buffers * n_bits / BITS (u8)); |
| 321 | |
| 322 | v0 = v1 = v_min; |
| 323 | |
| 324 | while (n_buffers >= 4) |
| 325 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 326 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 327 | void *a0, *a1; |
| 328 | u64 r0 = 0, r1 = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | |
| 330 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 331 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 332 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 333 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 334 | buffers += 2; |
| 335 | n_buffers -= 2; |
| 336 | |
| 337 | a0 = (void *) b0 + byte_offset; |
| 338 | a1 = (void *) b1 + byte_offset; |
| 339 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 340 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 341 | |
| 342 | switch (n_bits) |
| 343 | { |
| 344 | #define _(n) \ |
| 345 | case BITS (u##n): \ |
| 346 | { \ |
| 347 | u##n * r = random_data; \ |
| 348 | r0 = r[0]; \ |
| 349 | r1 = r[1]; \ |
| 350 | random_data = r + 2; \ |
| 351 | } \ |
| 352 | break; |
| 353 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 354 | _(8); |
| 355 | _(16); |
| 356 | _(32); |
| 357 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
| 359 | #undef _ |
| 360 | } |
| 361 | |
| 362 | /* Add power of 2 sized random number which may be out of range. */ |
| 363 | v0 += r0 & r_mask; |
| 364 | v1 += r1 & r_mask; |
| 365 | |
| 366 | /* Twice should be enough to reduce to v_min .. v_max range. */ |
| 367 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 368 | v1 = v1 > v_max ? v1 - v_diff : v1; |
| 369 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 370 | v1 = v1 > v_max ? v1 - v_diff : v1; |
| 371 | |
| 372 | if (want_sum) |
| 373 | sum += v0 + v1; |
| 374 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 375 | set_2 (a0, a1, v0, v1, v_min, v_max, n_bits, is_net_byte_order, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | /* is_increment */ 0); |
| 377 | |
| 378 | ASSERT (validate_buffer_data (b0, s)); |
| 379 | ASSERT (validate_buffer_data (b1, s)); |
| 380 | } |
| 381 | |
| 382 | while (n_buffers > 0) |
| 383 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 384 | vlib_buffer_t *b0; |
| 385 | void *a0; |
| 386 | u64 r0 = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
| 388 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 389 | buffers += 1; |
| 390 | n_buffers -= 1; |
| 391 | |
| 392 | a0 = (void *) b0 + byte_offset; |
| 393 | |
| 394 | switch (n_bits) |
| 395 | { |
| 396 | #define _(n) \ |
| 397 | case BITS (u##n): \ |
| 398 | { \ |
| 399 | u##n * r = random_data; \ |
| 400 | r0 = r[0]; \ |
| 401 | random_data = r + 1; \ |
| 402 | } \ |
| 403 | break; |
| 404 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 405 | _(8); |
| 406 | _(16); |
| 407 | _(32); |
| 408 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | |
| 410 | #undef _ |
| 411 | } |
| 412 | |
| 413 | /* Add power of 2 sized random number which may be out of range. */ |
| 414 | v0 += r0 & r_mask; |
| 415 | |
| 416 | /* Twice should be enough to reduce to v_min .. v_max range. */ |
| 417 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 418 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 419 | |
| 420 | if (want_sum) |
| 421 | sum += v0; |
| 422 | |
| 423 | set_1 (a0, v0, v_min, v_max, n_bits, is_net_byte_order); |
| 424 | |
| 425 | ASSERT (validate_buffer_data (b0, s)); |
| 426 | } |
| 427 | |
| 428 | if (want_sum) |
| 429 | *sum_result = sum; |
| 430 | } |
| 431 | |
| 432 | #define _(i,t) \ |
| 433 | clib_mem_unaligned (a##i, t) = \ |
| 434 | clib_host_to_net_##t ((clib_net_to_host_mem_##t (a##i) &~ mask) \ |
| 435 | | (v##i << shift)) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 436 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | always_inline void |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 438 | setbits_1 (void *a0, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | u64 v0, |
| 440 | u64 v_min, u64 v_max, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 441 | u32 max_bits, u32 n_bits, u64 mask, u32 shift) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | { |
| 443 | ASSERT (v0 >= v_min && v0 <= v_max); |
| 444 | if (max_bits == BITS (u8)) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 445 | ((u8 *) a0)[0] = (((u8 *) a0)[0] & ~mask) | (v0 << shift); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | |
| 447 | else if (max_bits == BITS (u16)) |
| 448 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 449 | _(0, u16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | } |
| 451 | else if (max_bits == BITS (u32)) |
| 452 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 453 | _(0, u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | } |
| 455 | else if (max_bits == BITS (u64)) |
| 456 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 457 | _(0, u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | always_inline void |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 462 | setbits_2 (void *a0, void *a1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | u64 v0, u64 v1, |
| 464 | u64 v_min, u64 v_max, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 465 | u32 max_bits, u32 n_bits, u64 mask, u32 shift, u32 is_increment) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | { |
| 467 | ASSERT (v0 >= v_min && v0 <= v_max); |
| 468 | ASSERT (v1 >= v_min && v1 <= v_max + is_increment); |
| 469 | if (max_bits == BITS (u8)) |
| 470 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 471 | ((u8 *) a0)[0] = (((u8 *) a0)[0] & ~mask) | (v0 << shift); |
| 472 | ((u8 *) a1)[0] = (((u8 *) a1)[0] & ~mask) | (v1 << shift); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | else if (max_bits == BITS (u16)) |
| 476 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 477 | _(0, u16); |
| 478 | _(1, u16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | } |
| 480 | else if (max_bits == BITS (u32)) |
| 481 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 482 | _(0, u32); |
| 483 | _(1, u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | } |
| 485 | else if (max_bits == BITS (u64)) |
| 486 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 487 | _(0, u64); |
| 488 | _(1, u64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | #undef _ |
| 493 | |
| 494 | static_always_inline void |
| 495 | do_setbits_fixed (pg_main_t * pg, |
| 496 | pg_stream_t * s, |
| 497 | u32 * buffers, |
| 498 | u32 n_buffers, |
| 499 | u32 max_bits, |
| 500 | u32 n_bits, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 501 | u32 byte_offset, u64 v_min, u64 v_max, u64 mask, u32 shift) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 503 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | |
| 505 | while (n_buffers >= 4) |
| 506 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 507 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 508 | void *a0, *a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
| 510 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 511 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 512 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 513 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 514 | buffers += 2; |
| 515 | n_buffers -= 2; |
| 516 | |
| 517 | a0 = (void *) b0 + byte_offset; |
| 518 | a1 = (void *) b1 + byte_offset; |
| 519 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 520 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 521 | |
| 522 | setbits_2 (a0, a1, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 523 | v_min, v_min, v_min, v_max, max_bits, n_bits, mask, shift, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | /* is_increment */ 0); |
| 525 | |
| 526 | ASSERT (validate_buffer_data (b0, s)); |
| 527 | ASSERT (validate_buffer_data (b1, s)); |
| 528 | } |
| 529 | |
| 530 | while (n_buffers > 0) |
| 531 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 532 | vlib_buffer_t *b0; |
| 533 | void *a0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | |
| 535 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 536 | buffers += 1; |
| 537 | n_buffers -= 1; |
| 538 | |
| 539 | a0 = (void *) b0 + byte_offset; |
| 540 | |
| 541 | setbits_1 (a0, v_min, v_min, v_max, max_bits, n_bits, mask, shift); |
| 542 | ASSERT (validate_buffer_data (b0, s)); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | static_always_inline u64 |
| 547 | do_setbits_increment (pg_main_t * pg, |
| 548 | pg_stream_t * s, |
| 549 | u32 * buffers, |
| 550 | u32 n_buffers, |
| 551 | u32 max_bits, |
| 552 | u32 n_bits, |
| 553 | u32 byte_offset, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 554 | u64 v_min, u64 v_max, u64 v, u64 mask, u32 shift) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 556 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | |
| 558 | ASSERT (v >= v_min && v <= v_max); |
| 559 | |
| 560 | while (n_buffers >= 4) |
| 561 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 562 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 563 | void *a0, *a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | u64 v_old; |
| 565 | |
| 566 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 567 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 568 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 569 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 570 | buffers += 2; |
| 571 | n_buffers -= 2; |
| 572 | |
| 573 | a0 = (void *) b0 + byte_offset; |
| 574 | a1 = (void *) b1 + byte_offset; |
| 575 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 576 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 577 | |
| 578 | v_old = v; |
| 579 | v = v_old + 2; |
| 580 | v = v > v_max ? v_min : v; |
| 581 | setbits_2 (a0, a1, |
| 582 | v_old + 0, v_old + 1, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 583 | v_min, v_max, max_bits, n_bits, mask, shift, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | /* is_increment */ 1); |
| 585 | |
| 586 | if (PREDICT_FALSE (v_old + 1 > v_max)) |
| 587 | { |
| 588 | v = v_old; |
| 589 | setbits_1 (a0, v + 0, v_min, v_max, max_bits, n_bits, mask, shift); |
| 590 | v += 1; |
| 591 | |
| 592 | v = v > v_max ? v_min : v; |
| 593 | setbits_1 (a1, v + 0, v_min, v_max, max_bits, n_bits, mask, shift); |
| 594 | v += 1; |
| 595 | } |
| 596 | ASSERT (validate_buffer_data (b0, s)); |
| 597 | ASSERT (validate_buffer_data (b1, s)); |
| 598 | } |
| 599 | |
| 600 | while (n_buffers > 0) |
| 601 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 602 | vlib_buffer_t *b0; |
| 603 | void *a0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | u64 v_old; |
| 605 | |
| 606 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 607 | buffers += 1; |
| 608 | n_buffers -= 1; |
| 609 | |
| 610 | a0 = (void *) b0 + byte_offset; |
| 611 | |
| 612 | v_old = v; |
| 613 | v = v_old + 1; |
| 614 | v = v > v_max ? v_min : v; |
| 615 | |
| 616 | ASSERT (v_old >= v_min && v_old <= v_max); |
| 617 | setbits_1 (a0, v_old, v_min, v_max, max_bits, n_bits, mask, shift); |
| 618 | |
| 619 | ASSERT (validate_buffer_data (b0, s)); |
| 620 | } |
| 621 | |
| 622 | return v; |
| 623 | } |
| 624 | |
| 625 | static_always_inline void |
| 626 | do_setbits_random (pg_main_t * pg, |
| 627 | pg_stream_t * s, |
| 628 | u32 * buffers, |
| 629 | u32 n_buffers, |
| 630 | u32 max_bits, |
| 631 | u32 n_bits, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 632 | u32 byte_offset, u64 v_min, u64 v_max, u64 mask, u32 shift) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 634 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | u64 v_diff = v_max - v_min + 1; |
| 636 | u64 r_mask = max_pow2 (v_diff) - 1; |
| 637 | u64 v0, v1; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 638 | void *random_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 639 | |
| 640 | random_data = clib_random_buffer_get_data |
| 641 | (&vm->random_buffer, n_buffers * max_bits / BITS (u8)); |
| 642 | v0 = v1 = v_min; |
| 643 | |
| 644 | while (n_buffers >= 4) |
| 645 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 646 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 647 | void *a0, *a1; |
| 648 | u64 r0 = 0, r1 = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
| 650 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 651 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 652 | b2 = vlib_get_buffer (vm, buffers[2]); |
| 653 | b3 = vlib_get_buffer (vm, buffers[3]); |
| 654 | buffers += 2; |
| 655 | n_buffers -= 2; |
| 656 | |
| 657 | a0 = (void *) b0 + byte_offset; |
| 658 | a1 = (void *) b1 + byte_offset; |
| 659 | CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE); |
| 660 | CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE); |
| 661 | |
| 662 | switch (max_bits) |
| 663 | { |
| 664 | #define _(n) \ |
| 665 | case BITS (u##n): \ |
| 666 | { \ |
| 667 | u##n * r = random_data; \ |
| 668 | r0 = r[0]; \ |
| 669 | r1 = r[1]; \ |
| 670 | random_data = r + 2; \ |
| 671 | } \ |
| 672 | break; |
| 673 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 674 | _(8); |
| 675 | _(16); |
| 676 | _(32); |
| 677 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | |
| 679 | #undef _ |
| 680 | } |
| 681 | |
| 682 | /* Add power of 2 sized random number which may be out of range. */ |
| 683 | v0 += r0 & r_mask; |
| 684 | v1 += r1 & r_mask; |
| 685 | |
| 686 | /* Twice should be enough to reduce to v_min .. v_max range. */ |
| 687 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 688 | v1 = v1 > v_max ? v1 - v_diff : v1; |
| 689 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 690 | v1 = v1 > v_max ? v1 - v_diff : v1; |
| 691 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 692 | setbits_2 (a0, a1, v0, v1, v_min, v_max, max_bits, n_bits, mask, shift, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | /* is_increment */ 0); |
| 694 | |
| 695 | ASSERT (validate_buffer_data (b0, s)); |
| 696 | ASSERT (validate_buffer_data (b1, s)); |
| 697 | } |
| 698 | |
| 699 | while (n_buffers > 0) |
| 700 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 701 | vlib_buffer_t *b0; |
| 702 | void *a0; |
| 703 | u64 r0 = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | |
| 705 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 706 | buffers += 1; |
| 707 | n_buffers -= 1; |
| 708 | |
| 709 | a0 = (void *) b0 + byte_offset; |
| 710 | |
| 711 | switch (max_bits) |
| 712 | { |
| 713 | #define _(n) \ |
| 714 | case BITS (u##n): \ |
| 715 | { \ |
| 716 | u##n * r = random_data; \ |
| 717 | r0 = r[0]; \ |
| 718 | random_data = r + 1; \ |
| 719 | } \ |
| 720 | break; |
| 721 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 722 | _(8); |
| 723 | _(16); |
| 724 | _(32); |
| 725 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | |
| 727 | #undef _ |
| 728 | } |
| 729 | |
| 730 | /* Add power of 2 sized random number which may be out of range. */ |
| 731 | v0 += r0 & r_mask; |
| 732 | |
| 733 | /* Twice should be enough to reduce to v_min .. v_max range. */ |
| 734 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 735 | v0 = v0 > v_max ? v0 - v_diff : v0; |
| 736 | |
| 737 | setbits_1 (a0, v0, v_min, v_max, max_bits, n_bits, mask, shift); |
| 738 | |
| 739 | ASSERT (validate_buffer_data (b0, s)); |
| 740 | } |
| 741 | } |
| 742 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 743 | static u64 |
| 744 | do_it (pg_main_t * pg, |
| 745 | pg_stream_t * s, |
| 746 | u32 * buffers, |
| 747 | u32 n_buffers, |
| 748 | u32 lo_bit, u32 hi_bit, |
| 749 | u64 v_min, u64 v_max, u64 v, pg_edit_type_t edit_type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | { |
| 751 | u32 max_bits, l0, l1, h1, start_bit; |
| 752 | |
| 753 | if (v_min == v_max) |
| 754 | edit_type = PG_EDIT_FIXED; |
| 755 | |
| 756 | l0 = lo_bit / BITS (u8); |
| 757 | l1 = lo_bit % BITS (u8); |
| 758 | h1 = hi_bit % BITS (u8); |
| 759 | |
| 760 | start_bit = l0 * BITS (u8); |
| 761 | |
| 762 | max_bits = hi_bit - start_bit; |
| 763 | ASSERT (max_bits <= 64); |
| 764 | |
| 765 | #define _(n) \ |
| 766 | case (n): \ |
| 767 | if (edit_type == PG_EDIT_INCREMENT) \ |
| 768 | v = do_set_increment (pg, s, buffers, n_buffers, \ |
| 769 | BITS (u##n), \ |
| 770 | l0, \ |
| 771 | /* is_net_byte_order */ 1, \ |
| 772 | /* want sum */ 0, 0, \ |
| 773 | v_min, v_max, \ |
| 774 | v); \ |
| 775 | else if (edit_type == PG_EDIT_RANDOM) \ |
| 776 | do_set_random (pg, s, buffers, n_buffers, \ |
| 777 | BITS (u##n), \ |
| 778 | l0, \ |
| 779 | /* is_net_byte_order */ 1, \ |
| 780 | /* want sum */ 0, 0, \ |
| 781 | v_min, v_max); \ |
| 782 | else /* edit_type == PG_EDIT_FIXED */ \ |
| 783 | do_set_fixed (pg, s, buffers, n_buffers, \ |
| 784 | BITS (u##n), \ |
| 785 | l0, \ |
| 786 | /* is_net_byte_order */ 1, \ |
| 787 | v_min, v_max); \ |
| 788 | goto done; |
| 789 | |
| 790 | if (l1 == 0 && h1 == 0) |
| 791 | { |
| 792 | switch (max_bits) |
| 793 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 794 | _(8); |
| 795 | _(16); |
| 796 | _(32); |
| 797 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
| 801 | #undef _ |
| 802 | |
| 803 | { |
| 804 | u64 mask; |
| 805 | u32 shift = l1; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 806 | u32 n_bits = max_bits; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
| 808 | max_bits = clib_max (max_pow2 (n_bits), 8); |
| 809 | |
| 810 | mask = ((u64) 1 << (u64) n_bits) - 1; |
| 811 | mask &= ~(((u64) 1 << (u64) shift) - 1); |
| 812 | |
| 813 | mask <<= max_bits - n_bits; |
| 814 | shift += max_bits - n_bits; |
| 815 | |
| 816 | switch (max_bits) |
| 817 | { |
| 818 | #define _(n) \ |
| 819 | case (n): \ |
| 820 | if (edit_type == PG_EDIT_INCREMENT) \ |
| 821 | v = do_setbits_increment (pg, s, buffers, n_buffers, \ |
| 822 | BITS (u##n), n_bits, \ |
| 823 | l0, v_min, v_max, v, \ |
| 824 | mask, shift); \ |
| 825 | else if (edit_type == PG_EDIT_RANDOM) \ |
| 826 | do_setbits_random (pg, s, buffers, n_buffers, \ |
| 827 | BITS (u##n), n_bits, \ |
| 828 | l0, v_min, v_max, \ |
| 829 | mask, shift); \ |
| 830 | else /* edit_type == PG_EDIT_FIXED */ \ |
| 831 | do_setbits_fixed (pg, s, buffers, n_buffers, \ |
| 832 | BITS (u##n), n_bits, \ |
| 833 | l0, v_min, v_max, \ |
| 834 | mask, shift); \ |
| 835 | goto done; |
| 836 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 837 | _(8); |
| 838 | _(16); |
| 839 | _(32); |
| 840 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | |
| 842 | #undef _ |
| 843 | } |
| 844 | } |
| 845 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 846 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 847 | return v; |
| 848 | } |
| 849 | |
| 850 | static void |
| 851 | pg_generate_set_lengths (pg_main_t * pg, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 852 | pg_stream_t * s, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 853 | { |
| 854 | u64 v_min, v_max, length_sum; |
| 855 | pg_edit_type_t edit_type; |
| 856 | |
| 857 | v_min = s->min_packet_bytes; |
| 858 | v_max = s->max_packet_bytes; |
| 859 | edit_type = s->packet_size_edit_type; |
| 860 | |
| 861 | if (edit_type == PG_EDIT_INCREMENT) |
| 862 | s->last_increment_packet_size |
| 863 | = do_set_increment (pg, s, buffers, n_buffers, |
| 864 | 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length), |
| 865 | STRUCT_OFFSET_OF (vlib_buffer_t, current_length), |
| 866 | /* is_net_byte_order */ 0, |
| 867 | /* want sum */ 1, &length_sum, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 868 | v_min, v_max, s->last_increment_packet_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 869 | |
| 870 | else if (edit_type == PG_EDIT_RANDOM) |
| 871 | do_set_random (pg, s, buffers, n_buffers, |
| 872 | 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length), |
| 873 | STRUCT_OFFSET_OF (vlib_buffer_t, current_length), |
| 874 | /* is_net_byte_order */ 0, |
| 875 | /* want sum */ 1, &length_sum, |
| 876 | v_min, v_max); |
| 877 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 878 | else /* edit_type == PG_EDIT_FIXED */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 879 | { |
| 880 | do_set_fixed (pg, s, buffers, n_buffers, |
| 881 | 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length), |
| 882 | STRUCT_OFFSET_OF (vlib_buffer_t, current_length), |
| 883 | /* is_net_byte_order */ 0, |
| 884 | v_min, v_max); |
| 885 | length_sum = v_min * n_buffers; |
| 886 | } |
| 887 | |
| 888 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 889 | vnet_main_t *vnm = vnet_get_main (); |
| 890 | vnet_interface_main_t *im = &vnm->interface_main; |
| 891 | vnet_sw_interface_t *si = |
| 892 | vnet_get_sw_interface (vnm, s->sw_if_index[VLIB_RX]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 893 | |
| 894 | vlib_increment_combined_counter (im->combined_sw_if_counters |
| 895 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame^] | 896 | vlib_get_thread_index (), |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 897 | si->sw_if_index, n_buffers, length_sum); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static void |
| 903 | pg_generate_fix_multi_buffer_lengths (pg_main_t * pg, |
| 904 | pg_stream_t * s, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 905 | u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 906 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 907 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 908 | pg_buffer_index_t *pbi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 909 | uword n_bytes_left; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 910 | static u32 *unused_buffers = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 911 | |
| 912 | while (n_buffers > 0) |
| 913 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 914 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 915 | u32 bi; |
| 916 | |
| 917 | bi = buffers[0]; |
| 918 | b = vlib_get_buffer (vm, bi); |
| 919 | |
| 920 | /* Current length here is length of whole packet. */ |
| 921 | n_bytes_left = b->current_length; |
| 922 | |
| 923 | pbi = s->buffer_indices; |
| 924 | while (1) |
| 925 | { |
| 926 | uword n = clib_min (n_bytes_left, s->buffer_bytes); |
| 927 | |
| 928 | b->current_length = n; |
| 929 | n_bytes_left -= n; |
| 930 | if (n_bytes_left > 0) |
| 931 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 932 | else |
| 933 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 934 | |
| 935 | /* Return unused buffers to fifos. */ |
| 936 | if (n == 0) |
| 937 | vec_add1 (unused_buffers, bi); |
| 938 | |
| 939 | pbi++; |
| 940 | if (pbi >= vec_end (s->buffer_indices)) |
| 941 | break; |
| 942 | |
| 943 | bi = b->next_buffer; |
| 944 | b = vlib_get_buffer (vm, bi); |
| 945 | } |
| 946 | ASSERT (n_bytes_left == 0); |
| 947 | |
| 948 | buffers += 1; |
| 949 | n_buffers -= 1; |
| 950 | } |
| 951 | |
| 952 | if (vec_len (unused_buffers) > 0) |
| 953 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 954 | vlib_buffer_free_no_next (vm, unused_buffers, vec_len (unused_buffers)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 955 | _vec_len (unused_buffers) = 0; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | static void |
| 960 | pg_generate_edit (pg_main_t * pg, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 961 | pg_stream_t * s, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 963 | pg_edit_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 964 | |
| 965 | vec_foreach (e, s->non_fixed_edits) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 966 | { |
| 967 | switch (e->type) |
| 968 | { |
| 969 | case PG_EDIT_RANDOM: |
| 970 | case PG_EDIT_INCREMENT: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 972 | u32 lo_bit, hi_bit; |
| 973 | u64 v_min, v_max; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 975 | v_min = pg_edit_get_value (e, PG_EDIT_LO); |
| 976 | v_max = pg_edit_get_value (e, PG_EDIT_HI); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 977 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 978 | hi_bit = (BITS (u8) * STRUCT_OFFSET_OF (vlib_buffer_t, data) |
| 979 | + BITS (u8) + e->lsb_bit_offset); |
| 980 | lo_bit = hi_bit - e->n_bits; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 981 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 982 | e->last_increment_value |
| 983 | = do_it (pg, s, buffers, n_buffers, lo_bit, hi_bit, v_min, v_max, |
| 984 | e->last_increment_value, e->type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | } |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 986 | break; |
| 987 | |
| 988 | case PG_EDIT_UNSPECIFIED: |
| 989 | break; |
| 990 | |
| 991 | default: |
| 992 | /* Should not be any fixed edits left. */ |
| 993 | ASSERT (0); |
| 994 | break; |
| 995 | } |
| 996 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | |
| 998 | /* Call any edit functions to e.g. completely IP lengths, checksums, ... */ |
| 999 | { |
| 1000 | int i; |
| 1001 | for (i = vec_len (s->edit_groups) - 1; i >= 0; i--) |
| 1002 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1003 | pg_edit_group_t *g = s->edit_groups + i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1004 | if (g->edit_function) |
| 1005 | g->edit_function (pg, s, g, buffers, n_buffers); |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | static void |
| 1011 | pg_set_next_buffer_pointers (pg_main_t * pg, |
| 1012 | pg_stream_t * s, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1013 | u32 * buffers, u32 * next_buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1015 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1016 | |
| 1017 | while (n_buffers >= 4) |
| 1018 | { |
| 1019 | u32 ni0, ni1; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1020 | vlib_buffer_t *b0, *b1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1021 | |
| 1022 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 1023 | b1 = vlib_get_buffer (vm, buffers[1]); |
| 1024 | ni0 = next_buffers[0]; |
| 1025 | ni1 = next_buffers[1]; |
| 1026 | |
| 1027 | vlib_prefetch_buffer_with_index (vm, buffers[2], WRITE); |
| 1028 | vlib_prefetch_buffer_with_index (vm, buffers[3], WRITE); |
| 1029 | |
| 1030 | b0->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1031 | b1->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1032 | b0->next_buffer = ni0; |
| 1033 | b1->next_buffer = ni1; |
| 1034 | |
| 1035 | buffers += 2; |
| 1036 | next_buffers += 2; |
| 1037 | n_buffers -= 2; |
| 1038 | } |
| 1039 | |
| 1040 | while (n_buffers > 0) |
| 1041 | { |
| 1042 | u32 ni0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1043 | vlib_buffer_t *b0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1044 | |
| 1045 | b0 = vlib_get_buffer (vm, buffers[0]); |
| 1046 | ni0 = next_buffers[0]; |
| 1047 | buffers += 1; |
| 1048 | next_buffers += 1; |
| 1049 | n_buffers -= 1; |
| 1050 | |
| 1051 | b0->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1052 | b0->next_buffer = ni0; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | static_always_inline void |
| 1057 | init_replay_buffers_inline (vlib_main_t * vm, |
| 1058 | pg_stream_t * s, |
| 1059 | u32 * buffers, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1060 | u32 n_buffers, u32 data_offset, u32 n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1061 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1062 | u32 n_left, *b, i, l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1063 | |
| 1064 | n_left = n_buffers; |
| 1065 | b = buffers; |
| 1066 | i = s->current_replay_packet_index; |
| 1067 | l = vec_len (s->replay_packet_templates); |
| 1068 | |
| 1069 | while (n_left >= 1) |
| 1070 | { |
| 1071 | u32 bi0, n0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1072 | vlib_buffer_t *b0; |
| 1073 | u8 *d0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1074 | |
| 1075 | bi0 = b[0]; |
| 1076 | b += 1; |
| 1077 | n_left -= 1; |
| 1078 | |
| 1079 | b0 = vlib_get_buffer (vm, bi0); |
| 1080 | |
| 1081 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX]; |
| 1082 | /* was s->sw_if_index[VLIB_TX]; */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1083 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1084 | |
| 1085 | d0 = vec_elt (s->replay_packet_templates, i); |
| 1086 | |
| 1087 | n0 = n_data; |
| 1088 | if (data_offset + n_data >= vec_len (d0)) |
| 1089 | n0 = vec_len (d0) > data_offset ? vec_len (d0) - data_offset : 0; |
| 1090 | |
| 1091 | b0->current_length = n0; |
| 1092 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1093 | clib_memcpy (b0->data, d0 + data_offset, n0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | i = i + 1 == l ? 0 : i + 1; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | static_always_inline void |
| 1099 | init_buffers_inline (vlib_main_t * vm, |
| 1100 | pg_stream_t * s, |
| 1101 | u32 * buffers, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1102 | u32 n_buffers, u32 data_offset, u32 n_data, u32 set_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1103 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1104 | u32 n_left, *b; |
| 1105 | u8 *data, *mask; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | |
| 1107 | if (vec_len (s->replay_packet_templates) > 0) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1108 | return init_replay_buffers_inline (vm, s, buffers, n_buffers, data_offset, |
| 1109 | n_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1110 | |
| 1111 | data = s->fixed_packet_data + data_offset; |
| 1112 | mask = s->fixed_packet_data_mask + data_offset; |
| 1113 | if (data + n_data >= vec_end (s->fixed_packet_data)) |
| 1114 | n_data = (data < vec_end (s->fixed_packet_data) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1115 | ? vec_end (s->fixed_packet_data) - data : 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1116 | if (n_data > 0) |
| 1117 | { |
| 1118 | ASSERT (data + n_data <= vec_end (s->fixed_packet_data)); |
| 1119 | ASSERT (mask + n_data <= vec_end (s->fixed_packet_data_mask)); |
| 1120 | } |
| 1121 | |
| 1122 | n_left = n_buffers; |
| 1123 | b = buffers; |
| 1124 | |
| 1125 | while (n_left >= 4) |
| 1126 | { |
| 1127 | u32 bi0, bi1; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1128 | vlib_buffer_t *b0, *b1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1129 | |
| 1130 | /* Prefetch next iteration. */ |
| 1131 | vlib_prefetch_buffer_with_index (vm, b[2], STORE); |
| 1132 | vlib_prefetch_buffer_with_index (vm, b[3], STORE); |
| 1133 | |
| 1134 | bi0 = b[0]; |
| 1135 | bi1 = b[1]; |
| 1136 | b += 2; |
| 1137 | n_left -= 2; |
| 1138 | |
| 1139 | b0 = vlib_get_buffer (vm, bi0); |
| 1140 | b1 = vlib_get_buffer (vm, bi1); |
| 1141 | |
| 1142 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = |
| 1143 | vnet_buffer (b1)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX]; |
| 1144 | |
| 1145 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1146 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1147 | |
| 1148 | if (set_data) |
| 1149 | { |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1150 | clib_memcpy (b0->data, data, n_data); |
| 1151 | clib_memcpy (b1->data, data, n_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1152 | } |
| 1153 | else |
| 1154 | { |
| 1155 | ASSERT (validate_buffer_data2 (b0, s, data_offset, n_data)); |
| 1156 | ASSERT (validate_buffer_data2 (b1, s, data_offset, n_data)); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | while (n_left >= 1) |
| 1161 | { |
| 1162 | u32 bi0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1163 | vlib_buffer_t *b0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1164 | |
| 1165 | bi0 = b[0]; |
| 1166 | b += 1; |
| 1167 | n_left -= 1; |
| 1168 | |
| 1169 | b0 = vlib_get_buffer (vm, bi0); |
| 1170 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX]; |
| 1171 | /* s->sw_if_index[VLIB_TX]; */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1172 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1173 | |
| 1174 | if (set_data) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1175 | clib_memcpy (b0->data, data, n_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1176 | else |
| 1177 | ASSERT (validate_buffer_data2 (b0, s, data_offset, n_data)); |
| 1178 | } |
| 1179 | } |
| 1180 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1181 | static void |
| 1182 | pg_buffer_init (vlib_main_t * vm, |
| 1183 | vlib_buffer_free_list_t * fl, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1185 | pg_main_t *pg = &pg_main; |
| 1186 | pg_stream_t *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1187 | uword bi, si; |
| 1188 | |
| 1189 | si = fl->buffer_init_function_opaque & pow2_mask (24); |
| 1190 | bi = fl->buffer_init_function_opaque >> 24; |
| 1191 | |
| 1192 | s = pool_elt_at_index (pg->streams, si); |
| 1193 | |
| 1194 | init_buffers_inline (vm, s, buffers, n_buffers, |
| 1195 | /* data_offset */ bi * s->buffer_bytes, |
| 1196 | /* n_data */ s->buffer_bytes, |
| 1197 | /* set_data */ 1); |
| 1198 | } |
| 1199 | |
| 1200 | static u32 |
| 1201 | pg_stream_fill_helper (pg_main_t * pg, |
| 1202 | pg_stream_t * s, |
| 1203 | pg_buffer_index_t * bi, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1204 | u32 * buffers, u32 * next_buffers, u32 n_alloc) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1206 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1207 | vlib_buffer_free_list_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | uword is_start_of_packet = bi == s->buffer_indices; |
| 1209 | u32 n_allocated; |
| 1210 | |
| 1211 | f = vlib_buffer_get_free_list (vm, bi->free_list_index); |
| 1212 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1213 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1214 | * Historically, the pg maintained its own free lists and |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 1215 | * device drivers tx paths would return pkts. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1216 | */ |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 1217 | if (vm->buffer_main->extern_buffer_mgmt == 0 && |
| 1218 | !(s->flags & PG_STREAM_FLAGS_DISABLE_BUFFER_RECYCLE)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1219 | f->buffer_init_function = pg_buffer_init; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1220 | f->buffer_init_function_opaque = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1221 | (s - pg->streams) | ((bi - s->buffer_indices) << 24); |
| 1222 | |
| 1223 | if (is_start_of_packet) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1224 | vnet_buffer (&f->buffer_init_template)->sw_if_index[VLIB_RX] |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1225 | = vnet_main.local_interface_sw_if_index; |
| 1226 | |
| 1227 | n_allocated = vlib_buffer_alloc_from_free_list (vm, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1228 | buffers, |
| 1229 | n_alloc, |
| 1230 | bi->free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1231 | if (n_allocated == 0) |
| 1232 | return 0; |
| 1233 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1234 | /* |
| 1235 | * We can't assume we got all the buffers we asked for... |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1236 | * This never worked until recently. |
| 1237 | */ |
| 1238 | n_alloc = n_allocated; |
| 1239 | |
| 1240 | /* Reinitialize buffers */ |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 1241 | if (vm->buffer_main->extern_buffer_mgmt == 0 || CLIB_DEBUG > 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1242 | || (s->flags & PG_STREAM_FLAGS_DISABLE_BUFFER_RECYCLE)) |
| 1243 | init_buffers_inline |
| 1244 | (vm, s, |
| 1245 | buffers, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1246 | n_alloc, (bi - s->buffer_indices) * s->buffer_bytes /* data offset */ , |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1247 | s->buffer_bytes, |
| 1248 | /* set_data */ |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 1249 | vm->buffer_main->extern_buffer_mgmt != 0 |
| 1250 | || (s->flags & PG_STREAM_FLAGS_DISABLE_BUFFER_RECYCLE) != 0); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1251 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1252 | if (next_buffers) |
| 1253 | pg_set_next_buffer_pointers (pg, s, buffers, next_buffers, n_alloc); |
| 1254 | |
| 1255 | if (is_start_of_packet) |
| 1256 | { |
| 1257 | if (vec_len (s->replay_packet_templates) > 0) |
| 1258 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1259 | vnet_main_t *vnm = vnet_get_main (); |
| 1260 | vnet_interface_main_t *im = &vnm->interface_main; |
| 1261 | vnet_sw_interface_t *si = |
| 1262 | vnet_get_sw_interface (vnm, s->sw_if_index[VLIB_RX]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1263 | u32 l = 0; |
| 1264 | u32 i; |
| 1265 | for (i = 0; i < n_alloc; i++) |
| 1266 | l += vlib_buffer_index_length_in_chain (vm, buffers[i]); |
| 1267 | vlib_increment_combined_counter (im->combined_sw_if_counters |
| 1268 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame^] | 1269 | vlib_get_thread_index (), |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1270 | si->sw_if_index, n_alloc, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1271 | s->current_replay_packet_index += n_alloc; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1272 | s->current_replay_packet_index %= |
| 1273 | vec_len (s->replay_packet_templates); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | pg_generate_set_lengths (pg, s, buffers, n_alloc); |
| 1278 | if (vec_len (s->buffer_indices) > 1) |
| 1279 | pg_generate_fix_multi_buffer_lengths (pg, s, buffers, n_alloc); |
| 1280 | |
| 1281 | pg_generate_edit (pg, s, buffers, n_alloc); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | return n_alloc; |
| 1286 | } |
| 1287 | |
| 1288 | static u32 |
| 1289 | pg_stream_fill (pg_main_t * pg, pg_stream_t * s, u32 n_buffers) |
| 1290 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1291 | pg_buffer_index_t *bi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1292 | word i, n_in_fifo, n_alloc, n_free, n_added; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1293 | u32 *tail, *start, *end, *last_tail, *last_start; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1294 | |
| 1295 | bi = s->buffer_indices; |
| 1296 | |
| 1297 | n_in_fifo = clib_fifo_elts (bi->buffer_fifo); |
| 1298 | if (n_in_fifo >= n_buffers) |
| 1299 | return n_in_fifo; |
| 1300 | |
| 1301 | n_alloc = n_buffers - n_in_fifo; |
| 1302 | |
| 1303 | /* Round up, but never generate more than limit. */ |
| 1304 | n_alloc = clib_max (VLIB_FRAME_SIZE, n_alloc); |
| 1305 | |
| 1306 | if (s->n_packets_limit > 0 |
| 1307 | && s->n_packets_generated + n_in_fifo + n_alloc >= s->n_packets_limit) |
| 1308 | { |
| 1309 | n_alloc = s->n_packets_limit - s->n_packets_generated - n_in_fifo; |
| 1310 | if (n_alloc < 0) |
| 1311 | n_alloc = 0; |
| 1312 | } |
| 1313 | |
| 1314 | /* All buffer fifos should have the same size. */ |
| 1315 | if (CLIB_DEBUG > 0) |
| 1316 | { |
| 1317 | uword l = ~0, e; |
| 1318 | vec_foreach (bi, s->buffer_indices) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1319 | { |
| 1320 | e = clib_fifo_elts (bi->buffer_fifo); |
| 1321 | if (bi == s->buffer_indices) |
| 1322 | l = e; |
| 1323 | ASSERT (l == e); |
| 1324 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | last_tail = last_start = 0; |
| 1328 | n_added = n_alloc; |
| 1329 | |
| 1330 | for (i = vec_len (s->buffer_indices) - 1; i >= 0; i--) |
| 1331 | { |
| 1332 | bi = vec_elt_at_index (s->buffer_indices, i); |
| 1333 | |
| 1334 | n_free = clib_fifo_free_elts (bi->buffer_fifo); |
| 1335 | if (n_free < n_alloc) |
| 1336 | clib_fifo_resize (bi->buffer_fifo, n_alloc - n_free); |
| 1337 | |
| 1338 | tail = clib_fifo_advance_tail (bi->buffer_fifo, n_alloc); |
| 1339 | start = bi->buffer_fifo; |
| 1340 | end = clib_fifo_end (bi->buffer_fifo); |
| 1341 | |
| 1342 | if (tail + n_alloc <= end) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1343 | { |
| 1344 | n_added = |
| 1345 | pg_stream_fill_helper (pg, s, bi, tail, last_tail, n_alloc); |
| 1346 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1347 | else |
| 1348 | { |
| 1349 | u32 n = clib_min (end - tail, n_alloc); |
| 1350 | n_added = pg_stream_fill_helper (pg, s, bi, tail, last_tail, n); |
| 1351 | |
| 1352 | if (n_added == n && n_alloc > n_added) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1353 | { |
| 1354 | n_added += pg_stream_fill_helper |
| 1355 | (pg, s, bi, start, last_start, n_alloc - n_added); |
| 1356 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | if (PREDICT_FALSE (n_added < n_alloc)) |
| 1360 | tail = clib_fifo_advance_tail (bi->buffer_fifo, n_added - n_alloc); |
| 1361 | |
| 1362 | last_tail = tail; |
| 1363 | last_start = start; |
| 1364 | |
| 1365 | /* Verify that pkts in the fifo are properly allocated */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1366 | } |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1367 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1368 | return n_in_fifo + n_added; |
| 1369 | } |
| 1370 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1371 | typedef struct |
| 1372 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1373 | u32 stream_index; |
| 1374 | |
| 1375 | u32 packet_length; |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 1376 | u32 sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* Use pre data for packet data. */ |
| 1379 | vlib_buffer_t buffer; |
| 1380 | } pg_input_trace_t; |
| 1381 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1382 | static u8 * |
| 1383 | format_pg_input_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1384 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1385 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1386 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1387 | pg_input_trace_t *t = va_arg (*va, pg_input_trace_t *); |
| 1388 | pg_main_t *pg = &pg_main; |
| 1389 | pg_stream_t *stream; |
| 1390 | vlib_node_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1391 | uword indent = format_get_indent (s); |
| 1392 | |
| 1393 | stream = 0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1394 | if (!pool_is_free_index (pg->streams, t->stream_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1395 | stream = pool_elt_at_index (pg->streams, t->stream_index); |
| 1396 | |
| 1397 | if (stream) |
| 1398 | s = format (s, "stream %v", pg->streams[t->stream_index].name); |
| 1399 | else |
| 1400 | s = format (s, "stream %d", t->stream_index); |
| 1401 | |
| 1402 | s = format (s, ", %d bytes", t->packet_length); |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 1403 | s = format (s, ", %d sw_if_index", t->sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1404 | |
| 1405 | s = format (s, "\n%U%U", |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1406 | format_white_space, indent, format_vlib_buffer, &t->buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1407 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1408 | s = format (s, "\n%U", format_white_space, indent); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1409 | |
| 1410 | n = 0; |
| 1411 | if (stream) |
| 1412 | n = vlib_get_node (vm, stream->node_index); |
| 1413 | |
| 1414 | if (n && n->format_buffer) |
| 1415 | s = format (s, "%U", n->format_buffer, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1416 | t->buffer.pre_data, sizeof (t->buffer.pre_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1417 | else |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1418 | s = format (s, "%U", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1419 | format_hex_bytes, t->buffer.pre_data, |
| 1420 | ARRAY_LEN (t->buffer.pre_data)); |
| 1421 | return s; |
| 1422 | } |
| 1423 | |
| 1424 | static void |
| 1425 | pg_input_trace (pg_main_t * pg, |
| 1426 | vlib_node_runtime_t * node, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1427 | pg_stream_t * s, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1428 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1429 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1430 | u32 *b, n_left, stream_index, next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1431 | |
| 1432 | n_left = n_buffers; |
| 1433 | b = buffers; |
| 1434 | stream_index = s - pg->streams; |
| 1435 | next_index = s->next_index; |
| 1436 | |
| 1437 | while (n_left >= 2) |
| 1438 | { |
| 1439 | u32 bi0, bi1; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1440 | vlib_buffer_t *b0, *b1; |
| 1441 | pg_input_trace_t *t0, *t1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1442 | |
| 1443 | bi0 = b[0]; |
| 1444 | bi1 = b[1]; |
| 1445 | b += 2; |
| 1446 | n_left -= 2; |
| 1447 | |
| 1448 | b0 = vlib_get_buffer (vm, bi0); |
| 1449 | b1 = vlib_get_buffer (vm, bi1); |
| 1450 | |
| 1451 | vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 1); |
| 1452 | vlib_trace_buffer (vm, node, next_index, b1, /* follow_chain */ 1); |
| 1453 | |
| 1454 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 1455 | t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); |
| 1456 | |
| 1457 | t0->stream_index = stream_index; |
| 1458 | t1->stream_index = stream_index; |
| 1459 | |
| 1460 | t0->packet_length = vlib_buffer_length_in_chain (vm, b0); |
| 1461 | t1->packet_length = vlib_buffer_length_in_chain (vm, b1); |
| 1462 | |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 1463 | t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 1464 | t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 1465 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1466 | clib_memcpy (&t0->buffer, b0, sizeof (b0[0]) - sizeof (b0->pre_data)); |
| 1467 | clib_memcpy (&t1->buffer, b1, sizeof (b1[0]) - sizeof (b1->pre_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1468 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1469 | clib_memcpy (t0->buffer.pre_data, b0->data, |
| 1470 | sizeof (t0->buffer.pre_data)); |
| 1471 | clib_memcpy (t1->buffer.pre_data, b1->data, |
| 1472 | sizeof (t1->buffer.pre_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | while (n_left >= 1) |
| 1476 | { |
| 1477 | u32 bi0; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1478 | vlib_buffer_t *b0; |
| 1479 | pg_input_trace_t *t0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1480 | |
| 1481 | bi0 = b[0]; |
| 1482 | b += 1; |
| 1483 | n_left -= 1; |
| 1484 | |
| 1485 | b0 = vlib_get_buffer (vm, bi0); |
| 1486 | |
| 1487 | vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 1); |
| 1488 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 1489 | |
| 1490 | t0->stream_index = stream_index; |
| 1491 | t0->packet_length = vlib_buffer_length_in_chain (vm, b0); |
Neale Ranns | 3466c30 | 2017-02-16 07:45:03 -0800 | [diff] [blame] | 1492 | t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1493 | clib_memcpy (&t0->buffer, b0, sizeof (b0[0]) - sizeof (b0->pre_data)); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1494 | clib_memcpy (t0->buffer.pre_data, b0->data, |
| 1495 | sizeof (t0->buffer.pre_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | static uword |
| 1500 | pg_generate_packets (vlib_node_runtime_t * node, |
| 1501 | pg_main_t * pg, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1502 | pg_stream_t * s, uword n_packets_to_generate) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1503 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1504 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1505 | u32 *to_next, n_this_frame, n_left, n_trace, n_packets_in_fifo; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1506 | uword n_packets_generated; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1507 | pg_buffer_index_t *bi, *bi0; |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 1508 | u32 next_index = s->next_index; |
| 1509 | vnet_feature_main_t *fm = &feature_main; |
| 1510 | vnet_feature_config_main_t *cm; |
| 1511 | u8 feature_arc_index = fm->device_input_feature_arc_index; |
| 1512 | cm = &fm->feature_config_mains[feature_arc_index]; |
| 1513 | u32 current_config_index = ~(u32) 0; |
| 1514 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1515 | |
| 1516 | bi0 = s->buffer_indices; |
| 1517 | |
| 1518 | n_packets_in_fifo = pg_stream_fill (pg, s, n_packets_to_generate); |
| 1519 | n_packets_to_generate = clib_min (n_packets_in_fifo, n_packets_to_generate); |
| 1520 | n_packets_generated = 0; |
| 1521 | |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 1522 | if (PREDICT_FALSE |
| 1523 | (vnet_have_features (feature_arc_index, s->sw_if_index[VLIB_RX]))) |
| 1524 | { |
| 1525 | current_config_index = |
| 1526 | vec_elt (cm->config_index_by_sw_if_index, s->sw_if_index[VLIB_RX]); |
| 1527 | vnet_get_config_data (&cm->config_main, ¤t_config_index, |
| 1528 | &next_index, 0); |
| 1529 | } |
| 1530 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1531 | while (n_packets_to_generate > 0) |
| 1532 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1533 | u32 *head, *start, *end; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1534 | |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 1535 | vlib_get_next_frame (vm, node, next_index, to_next, n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1536 | |
| 1537 | n_this_frame = n_packets_to_generate; |
| 1538 | if (n_this_frame > n_left) |
| 1539 | n_this_frame = n_left; |
| 1540 | |
| 1541 | start = bi0->buffer_fifo; |
| 1542 | end = clib_fifo_end (bi0->buffer_fifo); |
| 1543 | head = clib_fifo_head (bi0->buffer_fifo); |
| 1544 | |
| 1545 | if (head + n_this_frame <= end) |
| 1546 | vlib_copy_buffers (to_next, head, n_this_frame); |
| 1547 | else |
| 1548 | { |
| 1549 | u32 n = end - head; |
| 1550 | vlib_copy_buffers (to_next + 0, head, n); |
| 1551 | vlib_copy_buffers (to_next + n, start, n_this_frame - n); |
| 1552 | } |
| 1553 | |
| 1554 | vec_foreach (bi, s->buffer_indices) |
| 1555 | clib_fifo_advance_head (bi->buffer_fifo, n_this_frame); |
| 1556 | |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 1557 | if (current_config_index != ~(u32) 0) |
| 1558 | for (i = 0; i < n_this_frame; i++) |
| 1559 | { |
| 1560 | vlib_buffer_t *b; |
| 1561 | b = vlib_get_buffer (vm, to_next[i]); |
| 1562 | vnet_buffer (b)->device_input_feat.saved_next_index = |
| 1563 | s->next_index; |
| 1564 | vnet_buffer (b)->device_input_feat.buffer_advance = 0; |
| 1565 | b->current_config_index = current_config_index; |
| 1566 | b->feature_arc_index = feature_arc_index; |
| 1567 | } |
| 1568 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1569 | n_trace = vlib_get_trace_count (vm, node); |
| 1570 | if (n_trace > 0) |
| 1571 | { |
| 1572 | u32 n = clib_min (n_trace, n_this_frame); |
| 1573 | pg_input_trace (pg, node, s, to_next, n); |
| 1574 | vlib_set_trace_count (vm, node, n_trace - n); |
| 1575 | } |
| 1576 | n_packets_to_generate -= n_this_frame; |
| 1577 | n_packets_generated += n_this_frame; |
| 1578 | n_left -= n_this_frame; |
Damjan Marion | d2017f6 | 2016-11-07 12:24:50 +0100 | [diff] [blame] | 1579 | vlib_put_next_frame (vm, node, next_index, n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | return n_packets_generated; |
| 1583 | } |
| 1584 | |
| 1585 | static uword |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1586 | pg_input_stream (vlib_node_runtime_t * node, pg_main_t * pg, pg_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1587 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1588 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1589 | uword n_packets; |
| 1590 | f64 time_now, dt; |
| 1591 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1592 | if (s->n_packets_limit > 0 && s->n_packets_generated >= s->n_packets_limit) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1593 | { |
| 1594 | pg_stream_enable_disable (pg, s, /* want_enabled */ 0); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | /* Apply rate limit. */ |
| 1599 | time_now = vlib_time_now (vm); |
| 1600 | if (s->time_last_generate == 0) |
| 1601 | s->time_last_generate = time_now; |
| 1602 | |
| 1603 | dt = time_now - s->time_last_generate; |
| 1604 | s->time_last_generate = time_now; |
| 1605 | |
| 1606 | n_packets = VLIB_FRAME_SIZE; |
| 1607 | if (s->rate_packets_per_second > 0) |
| 1608 | { |
| 1609 | s->packet_accumulator += dt * s->rate_packets_per_second; |
| 1610 | n_packets = s->packet_accumulator; |
| 1611 | |
| 1612 | /* Never allow accumulator to grow if we get behind. */ |
| 1613 | s->packet_accumulator -= n_packets; |
| 1614 | } |
| 1615 | |
| 1616 | /* Apply fixed limit. */ |
| 1617 | if (s->n_packets_limit > 0 |
| 1618 | && s->n_packets_generated + n_packets > s->n_packets_limit) |
| 1619 | n_packets = s->n_packets_limit - s->n_packets_generated; |
| 1620 | |
| 1621 | /* Generate up to one frame's worth of packets. */ |
| 1622 | if (n_packets > VLIB_FRAME_SIZE) |
| 1623 | n_packets = VLIB_FRAME_SIZE; |
| 1624 | |
| 1625 | if (n_packets > 0) |
| 1626 | n_packets = pg_generate_packets (node, pg, s, n_packets); |
| 1627 | |
| 1628 | s->n_packets_generated += n_packets; |
| 1629 | |
| 1630 | return n_packets; |
| 1631 | } |
| 1632 | |
| 1633 | uword |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1634 | pg_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1635 | { |
| 1636 | uword i; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1637 | pg_main_t *pg = &pg_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1638 | uword n_packets = 0; |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 1639 | u32 worker_index = 0; |
| 1640 | |
| 1641 | if (vlib_num_workers ()) |
| 1642 | worker_index = vlib_get_current_worker_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1643 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1644 | /* *INDENT-OFF* */ |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 1645 | clib_bitmap_foreach (i, pg->enabled_streams[worker_index], ({ |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 1646 | pg_stream_t *s = vec_elt_at_index (pg->streams, i); |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 1647 | n_packets += pg_input_stream (node, pg, s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1648 | })); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1649 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1650 | |
| 1651 | return n_packets; |
| 1652 | } |
| 1653 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1654 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1655 | VLIB_REGISTER_NODE (pg_input_node) = { |
| 1656 | .function = pg_input, |
| 1657 | .name = "pg-input", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 1658 | .sibling_of = "device-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1659 | .type = VLIB_NODE_TYPE_INPUT, |
| 1660 | |
| 1661 | .format_trace = format_pg_input_trace, |
| 1662 | |
| 1663 | /* Input node will be left disabled until a stream is active. */ |
| 1664 | .state = VLIB_NODE_STATE_DISABLED, |
| 1665 | }; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 1666 | /* *INDENT-ON* */ |
| 1667 | |
| 1668 | /* |
| 1669 | * fd.io coding-style-patch-verification: ON |
| 1670 | * |
| 1671 | * Local Variables: |
| 1672 | * eval: (c-set-style "gnu") |
| 1673 | * End: |
| 1674 | */ |