blob: 4db4b45edcb26644d83e0342cc207d89d4bd0651 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Marion650223c2018-11-14 16:55:53 +010043#include <vnet/ethernet/ethernet.h>
Damjan Mariond2017f62016-11-07 12:24:50 +010044#include <vnet/feature/feature.h>
45#include <vnet/devices/devices.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
Ed Warnickecb9cada2015-12-08 15:45:58 -070047static int
48validate_buffer_data2 (vlib_buffer_t * b, pg_stream_t * s,
49 u32 data_offset, u32 n_bytes)
50{
Calvin71e97c62016-08-19 16:23:14 -040051 u8 *bd, *pd, *pm;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 u32 i;
53
54 bd = b->data;
55 pd = s->fixed_packet_data + data_offset;
56 pm = s->fixed_packet_data_mask + data_offset;
57
58 if (pd + n_bytes >= vec_end (s->fixed_packet_data))
59 n_bytes = (pd < vec_end (s->fixed_packet_data)
Calvin71e97c62016-08-19 16:23:14 -040060 ? vec_end (s->fixed_packet_data) - pd : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62 for (i = 0; i < n_bytes; i++)
63 if ((bd[i] & pm[i]) != pd[i])
64 break;
65
66 if (i >= n_bytes)
67 return 1;
68
Damjan Marionbd846cd2017-11-21 13:12:41 +010069 clib_warning ("buffer %U", format_vnet_buffer, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 clib_warning ("differ at index %d", i);
71 clib_warning ("is %U", format_hex_bytes, bd, n_bytes);
72 clib_warning ("mask %U", format_hex_bytes, pm, n_bytes);
73 clib_warning ("expect %U", format_hex_bytes, pd, n_bytes);
74 return 0;
75}
76
77static int
78validate_buffer_data (vlib_buffer_t * b, pg_stream_t * s)
Calvin71e97c62016-08-19 16:23:14 -040079{
80 return validate_buffer_data2 (b, s, 0, s->buffer_bytes);
81}
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83always_inline void
Calvin71e97c62016-08-19 16:23:14 -040084set_1 (void *a0,
85 u64 v0, u64 v_min, u64 v_max, u32 n_bits, u32 is_net_byte_order)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
87 ASSERT (v0 >= v_min && v0 <= v_max);
88 if (n_bits == BITS (u8))
89 {
90 ((u8 *) a0)[0] = v0;
91 }
92 else if (n_bits == BITS (u16))
93 {
94 if (is_net_byte_order)
95 v0 = clib_host_to_net_u16 (v0);
96 clib_mem_unaligned (a0, u16) = v0;
97 }
98 else if (n_bits == BITS (u32))
99 {
100 if (is_net_byte_order)
101 v0 = clib_host_to_net_u32 (v0);
102 clib_mem_unaligned (a0, u32) = v0;
103 }
104 else if (n_bits == BITS (u64))
105 {
106 if (is_net_byte_order)
107 v0 = clib_host_to_net_u64 (v0);
108 clib_mem_unaligned (a0, u64) = v0;
109 }
110}
111
112always_inline void
Calvin71e97c62016-08-19 16:23:14 -0400113set_2 (void *a0, void *a1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 u64 v0, u64 v1,
115 u64 v_min, u64 v_max,
Calvin71e97c62016-08-19 16:23:14 -0400116 u32 n_bits, u32 is_net_byte_order, u32 is_increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
118 ASSERT (v0 >= v_min && v0 <= v_max);
119 ASSERT (v1 >= v_min && v1 <= (v_max + is_increment));
120 if (n_bits == BITS (u8))
121 {
122 ((u8 *) a0)[0] = v0;
123 ((u8 *) a1)[0] = v1;
124 }
125 else if (n_bits == BITS (u16))
126 {
127 if (is_net_byte_order)
128 {
129 v0 = clib_host_to_net_u16 (v0);
130 v1 = clib_host_to_net_u16 (v1);
131 }
132 clib_mem_unaligned (a0, u16) = v0;
133 clib_mem_unaligned (a1, u16) = v1;
134 }
135 else if (n_bits == BITS (u32))
136 {
137 if (is_net_byte_order)
138 {
139 v0 = clib_host_to_net_u32 (v0);
140 v1 = clib_host_to_net_u32 (v1);
141 }
142 clib_mem_unaligned (a0, u32) = v0;
143 clib_mem_unaligned (a1, u32) = v1;
144 }
145 else if (n_bits == BITS (u64))
146 {
147 if (is_net_byte_order)
148 {
149 v0 = clib_host_to_net_u64 (v0);
150 v1 = clib_host_to_net_u64 (v1);
151 }
152 clib_mem_unaligned (a0, u64) = v0;
153 clib_mem_unaligned (a1, u64) = v1;
154 }
155}
156
157static_always_inline void
158do_set_fixed (pg_main_t * pg,
159 pg_stream_t * s,
160 u32 * buffers,
161 u32 n_buffers,
162 u32 n_bits,
Calvin71e97c62016-08-19 16:23:14 -0400163 u32 byte_offset, u32 is_net_byte_order, u64 v_min, u64 v_max)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164{
Damjan Marion64034362016-11-07 22:19:55 +0100165 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
167 while (n_buffers >= 4)
168 {
Calvin71e97c62016-08-19 16:23:14 -0400169 vlib_buffer_t *b0, *b1, *b2, *b3;
170 void *a0, *a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 b0 = vlib_get_buffer (vm, buffers[0]);
173 b1 = vlib_get_buffer (vm, buffers[1]);
174 b2 = vlib_get_buffer (vm, buffers[2]);
175 b3 = vlib_get_buffer (vm, buffers[3]);
176 buffers += 2;
177 n_buffers -= 2;
178
179 a0 = (void *) b0 + byte_offset;
180 a1 = (void *) b1 + byte_offset;
181 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
182 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
183
Calvin71e97c62016-08-19 16:23:14 -0400184 set_2 (a0, a1, v_min, v_min, v_min, v_max, n_bits, is_net_byte_order,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 /* is_increment */ 0);
186
187 ASSERT (validate_buffer_data (b0, s));
188 ASSERT (validate_buffer_data (b1, s));
189 }
190
191 while (n_buffers > 0)
192 {
Calvin71e97c62016-08-19 16:23:14 -0400193 vlib_buffer_t *b0;
194 void *a0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
196 b0 = vlib_get_buffer (vm, buffers[0]);
197 buffers += 1;
198 n_buffers -= 1;
199
200 a0 = (void *) b0 + byte_offset;
201
Calvin71e97c62016-08-19 16:23:14 -0400202 set_1 (a0, v_min, v_min, v_max, n_bits, is_net_byte_order);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 ASSERT (validate_buffer_data (b0, s));
205 }
206}
207
208static_always_inline u64
209do_set_increment (pg_main_t * pg,
210 pg_stream_t * s,
211 u32 * buffers,
212 u32 n_buffers,
213 u32 n_bits,
214 u32 byte_offset,
215 u32 is_net_byte_order,
Calvin71e97c62016-08-19 16:23:14 -0400216 u32 want_sum, u64 * sum_result, u64 v_min, u64 v_max, u64 v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217{
Damjan Marion64034362016-11-07 22:19:55 +0100218 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 u64 sum = 0;
220
221 ASSERT (v >= v_min && v <= v_max);
222
223 while (n_buffers >= 4)
224 {
Calvin71e97c62016-08-19 16:23:14 -0400225 vlib_buffer_t *b0, *b1, *b2, *b3;
226 void *a0, *a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 u64 v_old;
228
229 b0 = vlib_get_buffer (vm, buffers[0]);
230 b1 = vlib_get_buffer (vm, buffers[1]);
231 b2 = vlib_get_buffer (vm, buffers[2]);
232 b3 = vlib_get_buffer (vm, buffers[3]);
233 buffers += 2;
234 n_buffers -= 2;
235
236 a0 = (void *) b0 + byte_offset;
237 a1 = (void *) b1 + byte_offset;
238 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
239 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
240
241 v_old = v;
242 v = v_old + 2;
243 v = v > v_max ? v_min : v;
244 set_2 (a0, a1,
Calvin71e97c62016-08-19 16:23:14 -0400245 v_old + 0, v_old + 1, v_min, v_max, n_bits, is_net_byte_order,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 /* is_increment */ 1);
247
248 if (want_sum)
Calvin71e97c62016-08-19 16:23:14 -0400249 sum += 2 * v_old + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
251 if (PREDICT_FALSE (v_old + 1 > v_max))
252 {
253 if (want_sum)
Calvin71e97c62016-08-19 16:23:14 -0400254 sum -= 2 * v_old + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256 v = v_old;
257 set_1 (a0, v + 0, v_min, v_max, n_bits, is_net_byte_order);
258 if (want_sum)
259 sum += v;
260 v += 1;
261
262 v = v > v_max ? v_min : v;
263 set_1 (a1, v + 0, v_min, v_max, n_bits, is_net_byte_order);
264 if (want_sum)
265 sum += v;
266 v += 1;
267 }
268
269 ASSERT (validate_buffer_data (b0, s));
270 ASSERT (validate_buffer_data (b1, s));
271 }
272
273 while (n_buffers > 0)
274 {
Calvin71e97c62016-08-19 16:23:14 -0400275 vlib_buffer_t *b0;
276 void *a0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 u64 v_old;
278
279 b0 = vlib_get_buffer (vm, buffers[0]);
280 buffers += 1;
281 n_buffers -= 1;
282
283 a0 = (void *) b0 + byte_offset;
284
285 v_old = v;
286 if (want_sum)
287 sum += v_old;
288 v += 1;
289 v = v > v_max ? v_min : v;
290
291 ASSERT (v_old >= v_min && v_old <= v_max);
292 set_1 (a0, v_old, v_min, v_max, n_bits, is_net_byte_order);
293
294 ASSERT (validate_buffer_data (b0, s));
295 }
296
297 if (want_sum)
298 *sum_result = sum;
299
300 return v;
301}
302
303static_always_inline void
304do_set_random (pg_main_t * pg,
305 pg_stream_t * s,
306 u32 * buffers,
307 u32 n_buffers,
308 u32 n_bits,
309 u32 byte_offset,
310 u32 is_net_byte_order,
Calvin71e97c62016-08-19 16:23:14 -0400311 u32 want_sum, u64 * sum_result, u64 v_min, u64 v_max)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312{
Damjan Marion64034362016-11-07 22:19:55 +0100313 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 u64 v_diff = v_max - v_min + 1;
315 u64 r_mask = max_pow2 (v_diff) - 1;
316 u64 v0, v1;
317 u64 sum = 0;
Calvin71e97c62016-08-19 16:23:14 -0400318 void *random_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
320 random_data = clib_random_buffer_get_data
321 (&vm->random_buffer, n_buffers * n_bits / BITS (u8));
322
323 v0 = v1 = v_min;
324
325 while (n_buffers >= 4)
326 {
Calvin71e97c62016-08-19 16:23:14 -0400327 vlib_buffer_t *b0, *b1, *b2, *b3;
328 void *a0, *a1;
329 u64 r0 = 0, r1 = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
331 b0 = vlib_get_buffer (vm, buffers[0]);
332 b1 = vlib_get_buffer (vm, buffers[1]);
333 b2 = vlib_get_buffer (vm, buffers[2]);
334 b3 = vlib_get_buffer (vm, buffers[3]);
335 buffers += 2;
336 n_buffers -= 2;
337
338 a0 = (void *) b0 + byte_offset;
339 a1 = (void *) b1 + byte_offset;
340 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
341 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
342
343 switch (n_bits)
344 {
345#define _(n) \
346 case BITS (u##n): \
347 { \
348 u##n * r = random_data; \
349 r0 = r[0]; \
350 r1 = r[1]; \
351 random_data = r + 2; \
352 } \
353 break;
354
Calvin71e97c62016-08-19 16:23:14 -0400355 _(8);
356 _(16);
357 _(32);
358 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
360#undef _
361 }
362
363 /* Add power of 2 sized random number which may be out of range. */
364 v0 += r0 & r_mask;
365 v1 += r1 & r_mask;
366
367 /* Twice should be enough to reduce to v_min .. v_max range. */
368 v0 = v0 > v_max ? v0 - v_diff : v0;
369 v1 = v1 > v_max ? v1 - v_diff : v1;
370 v0 = v0 > v_max ? v0 - v_diff : v0;
371 v1 = v1 > v_max ? v1 - v_diff : v1;
372
373 if (want_sum)
374 sum += v0 + v1;
375
Calvin71e97c62016-08-19 16:23:14 -0400376 set_2 (a0, a1, v0, v1, v_min, v_max, n_bits, is_net_byte_order,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 /* is_increment */ 0);
378
379 ASSERT (validate_buffer_data (b0, s));
380 ASSERT (validate_buffer_data (b1, s));
381 }
382
383 while (n_buffers > 0)
384 {
Calvin71e97c62016-08-19 16:23:14 -0400385 vlib_buffer_t *b0;
386 void *a0;
387 u64 r0 = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
389 b0 = vlib_get_buffer (vm, buffers[0]);
390 buffers += 1;
391 n_buffers -= 1;
392
393 a0 = (void *) b0 + byte_offset;
394
395 switch (n_bits)
396 {
397#define _(n) \
398 case BITS (u##n): \
399 { \
400 u##n * r = random_data; \
401 r0 = r[0]; \
402 random_data = r + 1; \
403 } \
404 break;
405
Calvin71e97c62016-08-19 16:23:14 -0400406 _(8);
407 _(16);
408 _(32);
409 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
411#undef _
412 }
413
414 /* Add power of 2 sized random number which may be out of range. */
415 v0 += r0 & r_mask;
416
417 /* Twice should be enough to reduce to v_min .. v_max range. */
418 v0 = v0 > v_max ? v0 - v_diff : v0;
419 v0 = v0 > v_max ? v0 - v_diff : v0;
420
421 if (want_sum)
422 sum += v0;
423
424 set_1 (a0, v0, v_min, v_max, n_bits, is_net_byte_order);
425
426 ASSERT (validate_buffer_data (b0, s));
427 }
428
429 if (want_sum)
430 *sum_result = sum;
431}
432
433#define _(i,t) \
434 clib_mem_unaligned (a##i, t) = \
435 clib_host_to_net_##t ((clib_net_to_host_mem_##t (a##i) &~ mask) \
436 | (v##i << shift))
Calvin71e97c62016-08-19 16:23:14 -0400437
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438always_inline void
Calvin71e97c62016-08-19 16:23:14 -0400439setbits_1 (void *a0,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 u64 v0,
441 u64 v_min, u64 v_max,
Calvin71e97c62016-08-19 16:23:14 -0400442 u32 max_bits, u32 n_bits, u64 mask, u32 shift)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443{
444 ASSERT (v0 >= v_min && v0 <= v_max);
445 if (max_bits == BITS (u8))
Calvin71e97c62016-08-19 16:23:14 -0400446 ((u8 *) a0)[0] = (((u8 *) a0)[0] & ~mask) | (v0 << shift);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
448 else if (max_bits == BITS (u16))
449 {
Calvin71e97c62016-08-19 16:23:14 -0400450 _(0, u16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451 }
452 else if (max_bits == BITS (u32))
453 {
Calvin71e97c62016-08-19 16:23:14 -0400454 _(0, u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 }
456 else if (max_bits == BITS (u64))
457 {
Calvin71e97c62016-08-19 16:23:14 -0400458 _(0, u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
460}
461
462always_inline void
Calvin71e97c62016-08-19 16:23:14 -0400463setbits_2 (void *a0, void *a1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 u64 v0, u64 v1,
465 u64 v_min, u64 v_max,
Calvin71e97c62016-08-19 16:23:14 -0400466 u32 max_bits, u32 n_bits, u64 mask, u32 shift, u32 is_increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467{
468 ASSERT (v0 >= v_min && v0 <= v_max);
469 ASSERT (v1 >= v_min && v1 <= v_max + is_increment);
470 if (max_bits == BITS (u8))
471 {
Calvin71e97c62016-08-19 16:23:14 -0400472 ((u8 *) a0)[0] = (((u8 *) a0)[0] & ~mask) | (v0 << shift);
473 ((u8 *) a1)[0] = (((u8 *) a1)[0] & ~mask) | (v1 << shift);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 }
475
476 else if (max_bits == BITS (u16))
477 {
Calvin71e97c62016-08-19 16:23:14 -0400478 _(0, u16);
479 _(1, u16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 }
481 else if (max_bits == BITS (u32))
482 {
Calvin71e97c62016-08-19 16:23:14 -0400483 _(0, u32);
484 _(1, u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 }
486 else if (max_bits == BITS (u64))
487 {
Calvin71e97c62016-08-19 16:23:14 -0400488 _(0, u64);
489 _(1, u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 }
491}
492
493#undef _
494
495static_always_inline void
496do_setbits_fixed (pg_main_t * pg,
497 pg_stream_t * s,
498 u32 * buffers,
499 u32 n_buffers,
500 u32 max_bits,
501 u32 n_bits,
Calvin71e97c62016-08-19 16:23:14 -0400502 u32 byte_offset, u64 v_min, u64 v_max, u64 mask, u32 shift)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503{
Damjan Marion64034362016-11-07 22:19:55 +0100504 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
506 while (n_buffers >= 4)
507 {
Calvin71e97c62016-08-19 16:23:14 -0400508 vlib_buffer_t *b0, *b1, *b2, *b3;
509 void *a0, *a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
511 b0 = vlib_get_buffer (vm, buffers[0]);
512 b1 = vlib_get_buffer (vm, buffers[1]);
513 b2 = vlib_get_buffer (vm, buffers[2]);
514 b3 = vlib_get_buffer (vm, buffers[3]);
515 buffers += 2;
516 n_buffers -= 2;
517
518 a0 = (void *) b0 + byte_offset;
519 a1 = (void *) b1 + byte_offset;
520 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
521 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
522
523 setbits_2 (a0, a1,
Calvin71e97c62016-08-19 16:23:14 -0400524 v_min, v_min, v_min, v_max, max_bits, n_bits, mask, shift,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 /* is_increment */ 0);
526
527 ASSERT (validate_buffer_data (b0, s));
528 ASSERT (validate_buffer_data (b1, s));
529 }
530
531 while (n_buffers > 0)
532 {
Calvin71e97c62016-08-19 16:23:14 -0400533 vlib_buffer_t *b0;
534 void *a0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
536 b0 = vlib_get_buffer (vm, buffers[0]);
537 buffers += 1;
538 n_buffers -= 1;
539
540 a0 = (void *) b0 + byte_offset;
541
542 setbits_1 (a0, v_min, v_min, v_max, max_bits, n_bits, mask, shift);
543 ASSERT (validate_buffer_data (b0, s));
544 }
545}
546
547static_always_inline u64
548do_setbits_increment (pg_main_t * pg,
549 pg_stream_t * s,
550 u32 * buffers,
551 u32 n_buffers,
552 u32 max_bits,
553 u32 n_bits,
554 u32 byte_offset,
Calvin71e97c62016-08-19 16:23:14 -0400555 u64 v_min, u64 v_max, u64 v, u64 mask, u32 shift)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556{
Damjan Marion64034362016-11-07 22:19:55 +0100557 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558
559 ASSERT (v >= v_min && v <= v_max);
560
561 while (n_buffers >= 4)
562 {
Calvin71e97c62016-08-19 16:23:14 -0400563 vlib_buffer_t *b0, *b1, *b2, *b3;
564 void *a0, *a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 u64 v_old;
566
567 b0 = vlib_get_buffer (vm, buffers[0]);
568 b1 = vlib_get_buffer (vm, buffers[1]);
569 b2 = vlib_get_buffer (vm, buffers[2]);
570 b3 = vlib_get_buffer (vm, buffers[3]);
571 buffers += 2;
572 n_buffers -= 2;
573
574 a0 = (void *) b0 + byte_offset;
575 a1 = (void *) b1 + byte_offset;
576 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
577 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
578
579 v_old = v;
580 v = v_old + 2;
581 v = v > v_max ? v_min : v;
582 setbits_2 (a0, a1,
583 v_old + 0, v_old + 1,
Calvin71e97c62016-08-19 16:23:14 -0400584 v_min, v_max, max_bits, n_bits, mask, shift,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585 /* is_increment */ 1);
586
587 if (PREDICT_FALSE (v_old + 1 > v_max))
588 {
589 v = v_old;
590 setbits_1 (a0, v + 0, v_min, v_max, max_bits, n_bits, mask, shift);
591 v += 1;
592
593 v = v > v_max ? v_min : v;
594 setbits_1 (a1, v + 0, v_min, v_max, max_bits, n_bits, mask, shift);
595 v += 1;
596 }
597 ASSERT (validate_buffer_data (b0, s));
598 ASSERT (validate_buffer_data (b1, s));
599 }
600
601 while (n_buffers > 0)
602 {
Calvin71e97c62016-08-19 16:23:14 -0400603 vlib_buffer_t *b0;
604 void *a0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 u64 v_old;
606
607 b0 = vlib_get_buffer (vm, buffers[0]);
608 buffers += 1;
609 n_buffers -= 1;
610
611 a0 = (void *) b0 + byte_offset;
612
613 v_old = v;
614 v = v_old + 1;
615 v = v > v_max ? v_min : v;
616
617 ASSERT (v_old >= v_min && v_old <= v_max);
618 setbits_1 (a0, v_old, v_min, v_max, max_bits, n_bits, mask, shift);
619
620 ASSERT (validate_buffer_data (b0, s));
621 }
622
623 return v;
624}
625
626static_always_inline void
627do_setbits_random (pg_main_t * pg,
628 pg_stream_t * s,
629 u32 * buffers,
630 u32 n_buffers,
631 u32 max_bits,
632 u32 n_bits,
Calvin71e97c62016-08-19 16:23:14 -0400633 u32 byte_offset, u64 v_min, u64 v_max, u64 mask, u32 shift)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634{
Damjan Marion64034362016-11-07 22:19:55 +0100635 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 u64 v_diff = v_max - v_min + 1;
637 u64 r_mask = max_pow2 (v_diff) - 1;
638 u64 v0, v1;
Calvin71e97c62016-08-19 16:23:14 -0400639 void *random_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
641 random_data = clib_random_buffer_get_data
642 (&vm->random_buffer, n_buffers * max_bits / BITS (u8));
643 v0 = v1 = v_min;
644
645 while (n_buffers >= 4)
646 {
Calvin71e97c62016-08-19 16:23:14 -0400647 vlib_buffer_t *b0, *b1, *b2, *b3;
648 void *a0, *a1;
649 u64 r0 = 0, r1 = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
651 b0 = vlib_get_buffer (vm, buffers[0]);
652 b1 = vlib_get_buffer (vm, buffers[1]);
653 b2 = vlib_get_buffer (vm, buffers[2]);
654 b3 = vlib_get_buffer (vm, buffers[3]);
655 buffers += 2;
656 n_buffers -= 2;
657
658 a0 = (void *) b0 + byte_offset;
659 a1 = (void *) b1 + byte_offset;
660 CLIB_PREFETCH ((void *) b2 + byte_offset, sizeof (v_min), WRITE);
661 CLIB_PREFETCH ((void *) b3 + byte_offset, sizeof (v_min), WRITE);
662
663 switch (max_bits)
664 {
665#define _(n) \
666 case BITS (u##n): \
667 { \
668 u##n * r = random_data; \
669 r0 = r[0]; \
670 r1 = r[1]; \
671 random_data = r + 2; \
672 } \
673 break;
674
Calvin71e97c62016-08-19 16:23:14 -0400675 _(8);
676 _(16);
677 _(32);
678 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
680#undef _
681 }
682
683 /* Add power of 2 sized random number which may be out of range. */
684 v0 += r0 & r_mask;
685 v1 += r1 & r_mask;
686
687 /* Twice should be enough to reduce to v_min .. v_max range. */
688 v0 = v0 > v_max ? v0 - v_diff : v0;
689 v1 = v1 > v_max ? v1 - v_diff : v1;
690 v0 = v0 > v_max ? v0 - v_diff : v0;
691 v1 = v1 > v_max ? v1 - v_diff : v1;
692
Calvin71e97c62016-08-19 16:23:14 -0400693 setbits_2 (a0, a1, v0, v1, v_min, v_max, max_bits, n_bits, mask, shift,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 /* is_increment */ 0);
695
696 ASSERT (validate_buffer_data (b0, s));
697 ASSERT (validate_buffer_data (b1, s));
698 }
699
700 while (n_buffers > 0)
701 {
Calvin71e97c62016-08-19 16:23:14 -0400702 vlib_buffer_t *b0;
703 void *a0;
704 u64 r0 = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
706 b0 = vlib_get_buffer (vm, buffers[0]);
707 buffers += 1;
708 n_buffers -= 1;
709
710 a0 = (void *) b0 + byte_offset;
711
712 switch (max_bits)
713 {
714#define _(n) \
715 case BITS (u##n): \
716 { \
717 u##n * r = random_data; \
718 r0 = r[0]; \
719 random_data = r + 1; \
720 } \
721 break;
722
Calvin71e97c62016-08-19 16:23:14 -0400723 _(8);
724 _(16);
725 _(32);
726 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
728#undef _
729 }
730
731 /* Add power of 2 sized random number which may be out of range. */
732 v0 += r0 & r_mask;
733
734 /* Twice should be enough to reduce to v_min .. v_max range. */
735 v0 = v0 > v_max ? v0 - v_diff : v0;
736 v0 = v0 > v_max ? v0 - v_diff : v0;
737
738 setbits_1 (a0, v0, v_min, v_max, max_bits, n_bits, mask, shift);
739
740 ASSERT (validate_buffer_data (b0, s));
741 }
742}
743
Calvin71e97c62016-08-19 16:23:14 -0400744static u64
745do_it (pg_main_t * pg,
746 pg_stream_t * s,
747 u32 * buffers,
748 u32 n_buffers,
749 u32 lo_bit, u32 hi_bit,
750 u64 v_min, u64 v_max, u64 v, pg_edit_type_t edit_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751{
752 u32 max_bits, l0, l1, h1, start_bit;
753
754 if (v_min == v_max)
755 edit_type = PG_EDIT_FIXED;
756
757 l0 = lo_bit / BITS (u8);
758 l1 = lo_bit % BITS (u8);
759 h1 = hi_bit % BITS (u8);
760
761 start_bit = l0 * BITS (u8);
762
763 max_bits = hi_bit - start_bit;
764 ASSERT (max_bits <= 64);
765
766#define _(n) \
767 case (n): \
768 if (edit_type == PG_EDIT_INCREMENT) \
769 v = do_set_increment (pg, s, buffers, n_buffers, \
770 BITS (u##n), \
771 l0, \
772 /* is_net_byte_order */ 1, \
773 /* want sum */ 0, 0, \
774 v_min, v_max, \
775 v); \
776 else if (edit_type == PG_EDIT_RANDOM) \
777 do_set_random (pg, s, buffers, n_buffers, \
778 BITS (u##n), \
779 l0, \
780 /* is_net_byte_order */ 1, \
781 /* want sum */ 0, 0, \
782 v_min, v_max); \
783 else /* edit_type == PG_EDIT_FIXED */ \
784 do_set_fixed (pg, s, buffers, n_buffers, \
785 BITS (u##n), \
786 l0, \
787 /* is_net_byte_order */ 1, \
788 v_min, v_max); \
789 goto done;
790
791 if (l1 == 0 && h1 == 0)
792 {
793 switch (max_bits)
794 {
Calvin71e97c62016-08-19 16:23:14 -0400795 _(8);
796 _(16);
797 _(32);
798 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 }
800 }
801
802#undef _
803
804 {
805 u64 mask;
806 u32 shift = l1;
Calvin71e97c62016-08-19 16:23:14 -0400807 u32 n_bits = max_bits;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808
809 max_bits = clib_max (max_pow2 (n_bits), 8);
810
811 mask = ((u64) 1 << (u64) n_bits) - 1;
812 mask &= ~(((u64) 1 << (u64) shift) - 1);
813
814 mask <<= max_bits - n_bits;
815 shift += max_bits - n_bits;
816
817 switch (max_bits)
818 {
819#define _(n) \
820 case (n): \
821 if (edit_type == PG_EDIT_INCREMENT) \
822 v = do_setbits_increment (pg, s, buffers, n_buffers, \
823 BITS (u##n), n_bits, \
824 l0, v_min, v_max, v, \
825 mask, shift); \
826 else if (edit_type == PG_EDIT_RANDOM) \
827 do_setbits_random (pg, s, buffers, n_buffers, \
828 BITS (u##n), n_bits, \
829 l0, v_min, v_max, \
830 mask, shift); \
831 else /* edit_type == PG_EDIT_FIXED */ \
832 do_setbits_fixed (pg, s, buffers, n_buffers, \
833 BITS (u##n), n_bits, \
834 l0, v_min, v_max, \
835 mask, shift); \
836 goto done;
837
Calvin71e97c62016-08-19 16:23:14 -0400838 _(8);
839 _(16);
840 _(32);
841 _(64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
843#undef _
844 }
845 }
846
Calvin71e97c62016-08-19 16:23:14 -0400847done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 return v;
849}
850
851static void
852pg_generate_set_lengths (pg_main_t * pg,
Calvin71e97c62016-08-19 16:23:14 -0400853 pg_stream_t * s, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854{
855 u64 v_min, v_max, length_sum;
856 pg_edit_type_t edit_type;
857
858 v_min = s->min_packet_bytes;
859 v_max = s->max_packet_bytes;
860 edit_type = s->packet_size_edit_type;
861
862 if (edit_type == PG_EDIT_INCREMENT)
863 s->last_increment_packet_size
864 = do_set_increment (pg, s, buffers, n_buffers,
865 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length),
866 STRUCT_OFFSET_OF (vlib_buffer_t, current_length),
867 /* is_net_byte_order */ 0,
868 /* want sum */ 1, &length_sum,
Calvin71e97c62016-08-19 16:23:14 -0400869 v_min, v_max, s->last_increment_packet_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
871 else if (edit_type == PG_EDIT_RANDOM)
872 do_set_random (pg, s, buffers, n_buffers,
873 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length),
874 STRUCT_OFFSET_OF (vlib_buffer_t, current_length),
875 /* is_net_byte_order */ 0,
876 /* want sum */ 1, &length_sum,
877 v_min, v_max);
878
Calvin71e97c62016-08-19 16:23:14 -0400879 else /* edit_type == PG_EDIT_FIXED */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 {
881 do_set_fixed (pg, s, buffers, n_buffers,
882 8 * STRUCT_SIZE_OF (vlib_buffer_t, current_length),
883 STRUCT_OFFSET_OF (vlib_buffer_t, current_length),
884 /* is_net_byte_order */ 0,
885 v_min, v_max);
886 length_sum = v_min * n_buffers;
887 }
888
889 {
Calvin71e97c62016-08-19 16:23:14 -0400890 vnet_main_t *vnm = vnet_get_main ();
891 vnet_interface_main_t *im = &vnm->interface_main;
892 vnet_sw_interface_t *si =
893 vnet_get_sw_interface (vnm, s->sw_if_index[VLIB_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894
895 vlib_increment_combined_counter (im->combined_sw_if_counters
896 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200897 vlib_get_thread_index (),
Calvin71e97c62016-08-19 16:23:14 -0400898 si->sw_if_index, n_buffers, length_sum);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 }
900
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901}
902
903static void
904pg_generate_fix_multi_buffer_lengths (pg_main_t * pg,
905 pg_stream_t * s,
Calvin71e97c62016-08-19 16:23:14 -0400906 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907{
Damjan Marion64034362016-11-07 22:19:55 +0100908 vlib_main_t *vm = vlib_get_main ();
Calvin71e97c62016-08-19 16:23:14 -0400909 pg_buffer_index_t *pbi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 uword n_bytes_left;
Calvin71e97c62016-08-19 16:23:14 -0400911 static u32 *unused_buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
913 while (n_buffers > 0)
914 {
Calvin71e97c62016-08-19 16:23:14 -0400915 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 u32 bi;
917
918 bi = buffers[0];
919 b = vlib_get_buffer (vm, bi);
920
921 /* Current length here is length of whole packet. */
922 n_bytes_left = b->current_length;
923
924 pbi = s->buffer_indices;
925 while (1)
926 {
927 uword n = clib_min (n_bytes_left, s->buffer_bytes);
928
929 b->current_length = n;
930 n_bytes_left -= n;
931 if (n_bytes_left > 0)
932 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
933 else
934 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
935
936 /* Return unused buffers to fifos. */
937 if (n == 0)
938 vec_add1 (unused_buffers, bi);
939
940 pbi++;
941 if (pbi >= vec_end (s->buffer_indices))
942 break;
943
944 bi = b->next_buffer;
945 b = vlib_get_buffer (vm, bi);
946 }
947 ASSERT (n_bytes_left == 0);
948
949 buffers += 1;
950 n_buffers -= 1;
951 }
952
953 if (vec_len (unused_buffers) > 0)
954 {
Calvin71e97c62016-08-19 16:23:14 -0400955 vlib_buffer_free_no_next (vm, unused_buffers, vec_len (unused_buffers));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956 _vec_len (unused_buffers) = 0;
957 }
958}
959
960static void
961pg_generate_edit (pg_main_t * pg,
Calvin71e97c62016-08-19 16:23:14 -0400962 pg_stream_t * s, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963{
Calvin71e97c62016-08-19 16:23:14 -0400964 pg_edit_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
966 vec_foreach (e, s->non_fixed_edits)
Calvin71e97c62016-08-19 16:23:14 -0400967 {
968 switch (e->type)
969 {
970 case PG_EDIT_RANDOM:
971 case PG_EDIT_INCREMENT:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 {
Calvin71e97c62016-08-19 16:23:14 -0400973 u32 lo_bit, hi_bit;
974 u64 v_min, v_max;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Calvin71e97c62016-08-19 16:23:14 -0400976 v_min = pg_edit_get_value (e, PG_EDIT_LO);
977 v_max = pg_edit_get_value (e, PG_EDIT_HI);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978
Calvin71e97c62016-08-19 16:23:14 -0400979 hi_bit = (BITS (u8) * STRUCT_OFFSET_OF (vlib_buffer_t, data)
980 + BITS (u8) + e->lsb_bit_offset);
981 lo_bit = hi_bit - e->n_bits;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Calvin71e97c62016-08-19 16:23:14 -0400983 e->last_increment_value
984 = do_it (pg, s, buffers, n_buffers, lo_bit, hi_bit, v_min, v_max,
985 e->last_increment_value, e->type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986 }
Calvin71e97c62016-08-19 16:23:14 -0400987 break;
988
989 case PG_EDIT_UNSPECIFIED:
990 break;
991
992 default:
993 /* Should not be any fixed edits left. */
994 ASSERT (0);
995 break;
996 }
997 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998
999 /* Call any edit functions to e.g. completely IP lengths, checksums, ... */
1000 {
1001 int i;
1002 for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
1003 {
Calvin71e97c62016-08-19 16:23:14 -04001004 pg_edit_group_t *g = s->edit_groups + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 if (g->edit_function)
1006 g->edit_function (pg, s, g, buffers, n_buffers);
1007 }
1008 }
1009}
1010
1011static void
1012pg_set_next_buffer_pointers (pg_main_t * pg,
1013 pg_stream_t * s,
Calvin71e97c62016-08-19 16:23:14 -04001014 u32 * buffers, u32 * next_buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015{
Damjan Marion64034362016-11-07 22:19:55 +01001016 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017
1018 while (n_buffers >= 4)
1019 {
1020 u32 ni0, ni1;
Calvin71e97c62016-08-19 16:23:14 -04001021 vlib_buffer_t *b0, *b1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022
1023 b0 = vlib_get_buffer (vm, buffers[0]);
1024 b1 = vlib_get_buffer (vm, buffers[1]);
1025 ni0 = next_buffers[0];
1026 ni1 = next_buffers[1];
1027
1028 vlib_prefetch_buffer_with_index (vm, buffers[2], WRITE);
1029 vlib_prefetch_buffer_with_index (vm, buffers[3], WRITE);
1030
1031 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
1032 b1->flags |= VLIB_BUFFER_NEXT_PRESENT;
1033 b0->next_buffer = ni0;
1034 b1->next_buffer = ni1;
1035
1036 buffers += 2;
1037 next_buffers += 2;
1038 n_buffers -= 2;
1039 }
1040
1041 while (n_buffers > 0)
1042 {
1043 u32 ni0;
Calvin71e97c62016-08-19 16:23:14 -04001044 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045
1046 b0 = vlib_get_buffer (vm, buffers[0]);
1047 ni0 = next_buffers[0];
1048 buffers += 1;
1049 next_buffers += 1;
1050 n_buffers -= 1;
1051
1052 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
1053 b0->next_buffer = ni0;
1054 }
1055}
1056
1057static_always_inline void
1058init_replay_buffers_inline (vlib_main_t * vm,
1059 pg_stream_t * s,
1060 u32 * buffers,
Calvin71e97c62016-08-19 16:23:14 -04001061 u32 n_buffers, u32 data_offset, u32 n_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062{
Calvin71e97c62016-08-19 16:23:14 -04001063 u32 n_left, *b, i, l;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
1065 n_left = n_buffers;
1066 b = buffers;
1067 i = s->current_replay_packet_index;
1068 l = vec_len (s->replay_packet_templates);
1069
1070 while (n_left >= 1)
1071 {
1072 u32 bi0, n0;
Calvin71e97c62016-08-19 16:23:14 -04001073 vlib_buffer_t *b0;
1074 u8 *d0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075
1076 bi0 = b[0];
1077 b += 1;
1078 n_left -= 1;
1079
1080 b0 = vlib_get_buffer (vm, bi0);
1081
1082 vnet_buffer (b0)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX];
1083 /* was s->sw_if_index[VLIB_TX]; */
Calvin71e97c62016-08-19 16:23:14 -04001084 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085
1086 d0 = vec_elt (s->replay_packet_templates, i);
Dave Barach78c56892018-05-16 11:34:35 -04001087 vnet_buffer2 (b0)->pg_replay_timestamp = s->replay_packet_timestamps[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
1089 n0 = n_data;
1090 if (data_offset + n_data >= vec_len (d0))
1091 n0 = vec_len (d0) > data_offset ? vec_len (d0) - data_offset : 0;
1092
1093 b0->current_length = n0;
1094
Dave Barach178cf492018-11-13 16:34:13 -05001095 clib_memcpy_fast (b0->data, d0 + data_offset, n0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096 i = i + 1 == l ? 0 : i + 1;
1097 }
1098}
1099
1100static_always_inline void
1101init_buffers_inline (vlib_main_t * vm,
1102 pg_stream_t * s,
1103 u32 * buffers,
Calvin71e97c62016-08-19 16:23:14 -04001104 u32 n_buffers, u32 data_offset, u32 n_data, u32 set_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105{
Calvin71e97c62016-08-19 16:23:14 -04001106 u32 n_left, *b;
1107 u8 *data, *mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108
1109 if (vec_len (s->replay_packet_templates) > 0)
Calvin71e97c62016-08-19 16:23:14 -04001110 return init_replay_buffers_inline (vm, s, buffers, n_buffers, data_offset,
1111 n_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112
1113 data = s->fixed_packet_data + data_offset;
1114 mask = s->fixed_packet_data_mask + data_offset;
1115 if (data + n_data >= vec_end (s->fixed_packet_data))
1116 n_data = (data < vec_end (s->fixed_packet_data)
Calvin71e97c62016-08-19 16:23:14 -04001117 ? vec_end (s->fixed_packet_data) - data : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 if (n_data > 0)
1119 {
1120 ASSERT (data + n_data <= vec_end (s->fixed_packet_data));
1121 ASSERT (mask + n_data <= vec_end (s->fixed_packet_data_mask));
1122 }
1123
1124 n_left = n_buffers;
1125 b = buffers;
1126
1127 while (n_left >= 4)
1128 {
1129 u32 bi0, bi1;
Calvin71e97c62016-08-19 16:23:14 -04001130 vlib_buffer_t *b0, *b1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131
1132 /* Prefetch next iteration. */
1133 vlib_prefetch_buffer_with_index (vm, b[2], STORE);
1134 vlib_prefetch_buffer_with_index (vm, b[3], STORE);
1135
1136 bi0 = b[0];
1137 bi1 = b[1];
1138 b += 2;
1139 n_left -= 2;
1140
1141 b0 = vlib_get_buffer (vm, bi0);
1142 b1 = vlib_get_buffer (vm, bi1);
1143
1144 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1145 vnet_buffer (b1)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX];
1146
1147 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
Calvin71e97c62016-08-19 16:23:14 -04001148 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
1150 if (set_data)
1151 {
Dave Barach178cf492018-11-13 16:34:13 -05001152 clib_memcpy_fast (b0->data, data, n_data);
1153 clib_memcpy_fast (b1->data, data, n_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154 }
1155 else
1156 {
1157 ASSERT (validate_buffer_data2 (b0, s, data_offset, n_data));
1158 ASSERT (validate_buffer_data2 (b1, s, data_offset, n_data));
1159 }
1160 }
1161
1162 while (n_left >= 1)
1163 {
1164 u32 bi0;
Calvin71e97c62016-08-19 16:23:14 -04001165 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166
1167 bi0 = b[0];
1168 b += 1;
1169 n_left -= 1;
1170
1171 b0 = vlib_get_buffer (vm, bi0);
1172 vnet_buffer (b0)->sw_if_index[VLIB_RX] = s->sw_if_index[VLIB_RX];
1173 /* s->sw_if_index[VLIB_TX]; */
Calvin71e97c62016-08-19 16:23:14 -04001174 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175
1176 if (set_data)
Dave Barach178cf492018-11-13 16:34:13 -05001177 clib_memcpy_fast (b0->data, data, n_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 else
1179 ASSERT (validate_buffer_data2 (b0, s, data_offset, n_data));
1180 }
1181}
1182
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183static u32
1184pg_stream_fill_helper (pg_main_t * pg,
1185 pg_stream_t * s,
1186 pg_buffer_index_t * bi,
Calvin71e97c62016-08-19 16:23:14 -04001187 u32 * buffers, u32 * next_buffers, u32 n_alloc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
Damjan Marion64034362016-11-07 22:19:55 +01001189 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001190 uword is_start_of_packet = bi == s->buffer_indices;
1191 u32 n_allocated;
1192
Damjan Marion671e60e2018-12-30 18:09:59 +01001193 n_allocated = vlib_buffer_alloc (vm, buffers, n_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194 if (n_allocated == 0)
1195 return 0;
1196
Calvin71e97c62016-08-19 16:23:14 -04001197 /*
1198 * We can't assume we got all the buffers we asked for...
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199 * This never worked until recently.
1200 */
1201 n_alloc = n_allocated;
1202
1203 /* Reinitialize buffers */
Damjan Marionef2e5842018-03-07 13:21:04 +01001204 init_buffers_inline
1205 (vm, s,
1206 buffers,
1207 n_alloc, (bi - s->buffer_indices) * s->buffer_bytes /* data offset */ ,
1208 s->buffer_bytes,
1209 /* set_data */ 1);
Calvin71e97c62016-08-19 16:23:14 -04001210
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 if (next_buffers)
1212 pg_set_next_buffer_pointers (pg, s, buffers, next_buffers, n_alloc);
1213
1214 if (is_start_of_packet)
1215 {
1216 if (vec_len (s->replay_packet_templates) > 0)
1217 {
Calvin71e97c62016-08-19 16:23:14 -04001218 vnet_main_t *vnm = vnet_get_main ();
1219 vnet_interface_main_t *im = &vnm->interface_main;
1220 vnet_sw_interface_t *si =
1221 vnet_get_sw_interface (vnm, s->sw_if_index[VLIB_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222 u32 l = 0;
1223 u32 i;
1224 for (i = 0; i < n_alloc; i++)
1225 l += vlib_buffer_index_length_in_chain (vm, buffers[i]);
1226 vlib_increment_combined_counter (im->combined_sw_if_counters
1227 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +02001228 vlib_get_thread_index (),
Calvin71e97c62016-08-19 16:23:14 -04001229 si->sw_if_index, n_alloc, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230 s->current_replay_packet_index += n_alloc;
Calvin71e97c62016-08-19 16:23:14 -04001231 s->current_replay_packet_index %=
1232 vec_len (s->replay_packet_templates);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233 }
1234 else
1235 {
1236 pg_generate_set_lengths (pg, s, buffers, n_alloc);
1237 if (vec_len (s->buffer_indices) > 1)
1238 pg_generate_fix_multi_buffer_lengths (pg, s, buffers, n_alloc);
1239
1240 pg_generate_edit (pg, s, buffers, n_alloc);
1241 }
1242 }
1243
1244 return n_alloc;
1245}
1246
1247static u32
1248pg_stream_fill (pg_main_t * pg, pg_stream_t * s, u32 n_buffers)
1249{
Calvin71e97c62016-08-19 16:23:14 -04001250 pg_buffer_index_t *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 word i, n_in_fifo, n_alloc, n_free, n_added;
Calvin71e97c62016-08-19 16:23:14 -04001252 u32 *tail, *start, *end, *last_tail, *last_start;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253
1254 bi = s->buffer_indices;
1255
1256 n_in_fifo = clib_fifo_elts (bi->buffer_fifo);
1257 if (n_in_fifo >= n_buffers)
1258 return n_in_fifo;
1259
1260 n_alloc = n_buffers - n_in_fifo;
1261
1262 /* Round up, but never generate more than limit. */
1263 n_alloc = clib_max (VLIB_FRAME_SIZE, n_alloc);
1264
1265 if (s->n_packets_limit > 0
1266 && s->n_packets_generated + n_in_fifo + n_alloc >= s->n_packets_limit)
1267 {
1268 n_alloc = s->n_packets_limit - s->n_packets_generated - n_in_fifo;
1269 if (n_alloc < 0)
1270 n_alloc = 0;
1271 }
1272
1273 /* All buffer fifos should have the same size. */
1274 if (CLIB_DEBUG > 0)
1275 {
1276 uword l = ~0, e;
1277 vec_foreach (bi, s->buffer_indices)
Calvin71e97c62016-08-19 16:23:14 -04001278 {
1279 e = clib_fifo_elts (bi->buffer_fifo);
1280 if (bi == s->buffer_indices)
1281 l = e;
1282 ASSERT (l == e);
1283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001284 }
1285
1286 last_tail = last_start = 0;
1287 n_added = n_alloc;
1288
1289 for (i = vec_len (s->buffer_indices) - 1; i >= 0; i--)
1290 {
1291 bi = vec_elt_at_index (s->buffer_indices, i);
1292
1293 n_free = clib_fifo_free_elts (bi->buffer_fifo);
1294 if (n_free < n_alloc)
1295 clib_fifo_resize (bi->buffer_fifo, n_alloc - n_free);
1296
1297 tail = clib_fifo_advance_tail (bi->buffer_fifo, n_alloc);
1298 start = bi->buffer_fifo;
1299 end = clib_fifo_end (bi->buffer_fifo);
1300
1301 if (tail + n_alloc <= end)
Calvin71e97c62016-08-19 16:23:14 -04001302 {
1303 n_added =
1304 pg_stream_fill_helper (pg, s, bi, tail, last_tail, n_alloc);
1305 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001306 else
1307 {
1308 u32 n = clib_min (end - tail, n_alloc);
1309 n_added = pg_stream_fill_helper (pg, s, bi, tail, last_tail, n);
1310
1311 if (n_added == n && n_alloc > n_added)
Calvin71e97c62016-08-19 16:23:14 -04001312 {
1313 n_added += pg_stream_fill_helper
1314 (pg, s, bi, start, last_start, n_alloc - n_added);
1315 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 }
1317
1318 if (PREDICT_FALSE (n_added < n_alloc))
1319 tail = clib_fifo_advance_tail (bi->buffer_fifo, n_added - n_alloc);
1320
1321 last_tail = tail;
1322 last_start = start;
1323
1324 /* Verify that pkts in the fifo are properly allocated */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325 }
Calvin71e97c62016-08-19 16:23:14 -04001326
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327 return n_in_fifo + n_added;
1328}
1329
Calvin71e97c62016-08-19 16:23:14 -04001330typedef struct
1331{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332 u32 stream_index;
1333
1334 u32 packet_length;
Neale Ranns3466c302017-02-16 07:45:03 -08001335 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336
1337 /* Use pre data for packet data. */
1338 vlib_buffer_t buffer;
1339} pg_input_trace_t;
1340
Calvin71e97c62016-08-19 16:23:14 -04001341static u8 *
1342format_pg_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343{
Calvin71e97c62016-08-19 16:23:14 -04001344 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Calvin71e97c62016-08-19 16:23:14 -04001346 pg_input_trace_t *t = va_arg (*va, pg_input_trace_t *);
1347 pg_main_t *pg = &pg_main;
1348 pg_stream_t *stream;
1349 vlib_node_t *n;
Christophe Fontained3c008d2017-10-02 18:10:54 +02001350 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351
1352 stream = 0;
Calvin71e97c62016-08-19 16:23:14 -04001353 if (!pool_is_free_index (pg->streams, t->stream_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354 stream = pool_elt_at_index (pg->streams, t->stream_index);
1355
1356 if (stream)
1357 s = format (s, "stream %v", pg->streams[t->stream_index].name);
1358 else
1359 s = format (s, "stream %d", t->stream_index);
1360
1361 s = format (s, ", %d bytes", t->packet_length);
Neale Ranns3466c302017-02-16 07:45:03 -08001362 s = format (s, ", %d sw_if_index", t->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001363
1364 s = format (s, "\n%U%U",
Damjan Marionbd846cd2017-11-21 13:12:41 +01001365 format_white_space, indent, format_vnet_buffer, &t->buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366
Calvin71e97c62016-08-19 16:23:14 -04001367 s = format (s, "\n%U", format_white_space, indent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368
1369 n = 0;
1370 if (stream)
1371 n = vlib_get_node (vm, stream->node_index);
1372
1373 if (n && n->format_buffer)
1374 s = format (s, "%U", n->format_buffer,
Calvin71e97c62016-08-19 16:23:14 -04001375 t->buffer.pre_data, sizeof (t->buffer.pre_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376 else
Calvin71e97c62016-08-19 16:23:14 -04001377 s = format (s, "%U",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001378 format_hex_bytes, t->buffer.pre_data,
1379 ARRAY_LEN (t->buffer.pre_data));
1380 return s;
1381}
1382
1383static void
1384pg_input_trace (pg_main_t * pg,
1385 vlib_node_runtime_t * node,
Calvin71e97c62016-08-19 16:23:14 -04001386 pg_stream_t * s, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001387{
Damjan Marion64034362016-11-07 22:19:55 +01001388 vlib_main_t *vm = vlib_get_main ();
Calvin71e97c62016-08-19 16:23:14 -04001389 u32 *b, n_left, stream_index, next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
1391 n_left = n_buffers;
1392 b = buffers;
1393 stream_index = s - pg->streams;
1394 next_index = s->next_index;
1395
1396 while (n_left >= 2)
1397 {
1398 u32 bi0, bi1;
Calvin71e97c62016-08-19 16:23:14 -04001399 vlib_buffer_t *b0, *b1;
1400 pg_input_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
1402 bi0 = b[0];
1403 bi1 = b[1];
1404 b += 2;
1405 n_left -= 2;
1406
1407 b0 = vlib_get_buffer (vm, bi0);
1408 b1 = vlib_get_buffer (vm, bi1);
1409
1410 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 1);
1411 vlib_trace_buffer (vm, node, next_index, b1, /* follow_chain */ 1);
1412
1413 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1414 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1415
1416 t0->stream_index = stream_index;
1417 t1->stream_index = stream_index;
1418
1419 t0->packet_length = vlib_buffer_length_in_chain (vm, b0);
1420 t1->packet_length = vlib_buffer_length_in_chain (vm, b1);
1421
Neale Ranns3466c302017-02-16 07:45:03 -08001422 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1423 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1424
Dave Barach178cf492018-11-13 16:34:13 -05001425 clib_memcpy_fast (&t0->buffer, b0,
1426 sizeof (b0[0]) - sizeof (b0->pre_data));
1427 clib_memcpy_fast (&t1->buffer, b1,
1428 sizeof (b1[0]) - sizeof (b1->pre_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001429
Dave Barach178cf492018-11-13 16:34:13 -05001430 clib_memcpy_fast (t0->buffer.pre_data, b0->data,
1431 sizeof (t0->buffer.pre_data));
1432 clib_memcpy_fast (t1->buffer.pre_data, b1->data,
1433 sizeof (t1->buffer.pre_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434 }
1435
1436 while (n_left >= 1)
1437 {
1438 u32 bi0;
Calvin71e97c62016-08-19 16:23:14 -04001439 vlib_buffer_t *b0;
1440 pg_input_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001441
1442 bi0 = b[0];
1443 b += 1;
1444 n_left -= 1;
1445
1446 b0 = vlib_get_buffer (vm, bi0);
1447
1448 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 1);
1449 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1450
1451 t0->stream_index = stream_index;
1452 t0->packet_length = vlib_buffer_length_in_chain (vm, b0);
Neale Ranns3466c302017-02-16 07:45:03 -08001453 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach178cf492018-11-13 16:34:13 -05001454 clib_memcpy_fast (&t0->buffer, b0,
1455 sizeof (b0[0]) - sizeof (b0->pre_data));
1456 clib_memcpy_fast (t0->buffer.pre_data, b0->data,
1457 sizeof (t0->buffer.pre_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 }
1459}
1460
1461static uword
1462pg_generate_packets (vlib_node_runtime_t * node,
1463 pg_main_t * pg,
Calvin71e97c62016-08-19 16:23:14 -04001464 pg_stream_t * s, uword n_packets_to_generate)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465{
Damjan Marion64034362016-11-07 22:19:55 +01001466 vlib_main_t *vm = vlib_get_main ();
Calvin71e97c62016-08-19 16:23:14 -04001467 u32 *to_next, n_this_frame, n_left, n_trace, n_packets_in_fifo;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 uword n_packets_generated;
Calvin71e97c62016-08-19 16:23:14 -04001469 pg_buffer_index_t *bi, *bi0;
Damjan Mariond2017f62016-11-07 12:24:50 +01001470 u32 next_index = s->next_index;
1471 vnet_feature_main_t *fm = &feature_main;
1472 vnet_feature_config_main_t *cm;
1473 u8 feature_arc_index = fm->device_input_feature_arc_index;
1474 cm = &fm->feature_config_mains[feature_arc_index];
1475 u32 current_config_index = ~(u32) 0;
1476 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001477
1478 bi0 = s->buffer_indices;
1479
1480 n_packets_in_fifo = pg_stream_fill (pg, s, n_packets_to_generate);
1481 n_packets_to_generate = clib_min (n_packets_in_fifo, n_packets_to_generate);
1482 n_packets_generated = 0;
1483
Damjan Mariond2017f62016-11-07 12:24:50 +01001484 if (PREDICT_FALSE
1485 (vnet_have_features (feature_arc_index, s->sw_if_index[VLIB_RX])))
1486 {
1487 current_config_index =
1488 vec_elt (cm->config_index_by_sw_if_index, s->sw_if_index[VLIB_RX]);
1489 vnet_get_config_data (&cm->config_main, &current_config_index,
1490 &next_index, 0);
1491 }
1492
Ed Warnickecb9cada2015-12-08 15:45:58 -07001493 while (n_packets_to_generate > 0)
1494 {
Calvin71e97c62016-08-19 16:23:14 -04001495 u32 *head, *start, *end;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496
Damjan Marion650223c2018-11-14 16:55:53 +01001497 if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
1498 {
1499 vlib_next_frame_t *nf;
1500 vlib_frame_t *f;
1501 ethernet_input_frame_t *ef;
1502 pg_interface_t *pi;
1503 vlib_get_new_next_frame (vm, node, next_index, to_next, n_left);
1504 nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
1505 f = vlib_get_frame (vm, nf->frame_index);
1506 f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
1507
1508 ef = vlib_frame_scalar_args (f);
1509 pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
1510 ef->sw_if_index = pi->sw_if_index;
1511 ef->hw_if_index = pi->hw_if_index;
1512 }
1513 else
1514 vlib_get_next_frame (vm, node, next_index, to_next, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515
1516 n_this_frame = n_packets_to_generate;
1517 if (n_this_frame > n_left)
1518 n_this_frame = n_left;
1519
1520 start = bi0->buffer_fifo;
1521 end = clib_fifo_end (bi0->buffer_fifo);
1522 head = clib_fifo_head (bi0->buffer_fifo);
1523
1524 if (head + n_this_frame <= end)
Damjan Marion671e60e2018-12-30 18:09:59 +01001525 clib_memcpy_fast (to_next, head, n_this_frame * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526 else
1527 {
1528 u32 n = end - head;
Damjan Marion671e60e2018-12-30 18:09:59 +01001529 clib_memcpy_fast (to_next + 0, head, n * sizeof (u32));
1530 clib_memcpy_fast (to_next + n, start,
1531 (n_this_frame - n) * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 }
1533
1534 vec_foreach (bi, s->buffer_indices)
1535 clib_fifo_advance_head (bi->buffer_fifo, n_this_frame);
1536
Damjan Mariond2017f62016-11-07 12:24:50 +01001537 if (current_config_index != ~(u32) 0)
1538 for (i = 0; i < n_this_frame; i++)
1539 {
1540 vlib_buffer_t *b;
1541 b = vlib_get_buffer (vm, to_next[i]);
Damjan Mariond2017f62016-11-07 12:24:50 +01001542 b->current_config_index = current_config_index;
Damjan Marionaa682a32018-04-26 22:45:40 +02001543 vnet_buffer (b)->feature_arc_index = feature_arc_index;
Damjan Mariond2017f62016-11-07 12:24:50 +01001544 }
1545
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 n_trace = vlib_get_trace_count (vm, node);
1547 if (n_trace > 0)
1548 {
1549 u32 n = clib_min (n_trace, n_this_frame);
1550 pg_input_trace (pg, node, s, to_next, n);
1551 vlib_set_trace_count (vm, node, n_trace - n);
1552 }
1553 n_packets_to_generate -= n_this_frame;
1554 n_packets_generated += n_this_frame;
1555 n_left -= n_this_frame;
Damjan Mariond2017f62016-11-07 12:24:50 +01001556 vlib_put_next_frame (vm, node, next_index, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001557 }
1558
1559 return n_packets_generated;
1560}
1561
1562static uword
Calvin71e97c62016-08-19 16:23:14 -04001563pg_input_stream (vlib_node_runtime_t * node, pg_main_t * pg, pg_stream_t * s)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564{
Damjan Marion64034362016-11-07 22:19:55 +01001565 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566 uword n_packets;
1567 f64 time_now, dt;
1568
Calvin71e97c62016-08-19 16:23:14 -04001569 if (s->n_packets_limit > 0 && s->n_packets_generated >= s->n_packets_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001570 {
1571 pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
1572 return 0;
1573 }
1574
1575 /* Apply rate limit. */
1576 time_now = vlib_time_now (vm);
1577 if (s->time_last_generate == 0)
1578 s->time_last_generate = time_now;
1579
1580 dt = time_now - s->time_last_generate;
1581 s->time_last_generate = time_now;
1582
1583 n_packets = VLIB_FRAME_SIZE;
1584 if (s->rate_packets_per_second > 0)
1585 {
1586 s->packet_accumulator += dt * s->rate_packets_per_second;
1587 n_packets = s->packet_accumulator;
1588
1589 /* Never allow accumulator to grow if we get behind. */
1590 s->packet_accumulator -= n_packets;
1591 }
1592
1593 /* Apply fixed limit. */
1594 if (s->n_packets_limit > 0
1595 && s->n_packets_generated + n_packets > s->n_packets_limit)
1596 n_packets = s->n_packets_limit - s->n_packets_generated;
1597
1598 /* Generate up to one frame's worth of packets. */
1599 if (n_packets > VLIB_FRAME_SIZE)
1600 n_packets = VLIB_FRAME_SIZE;
1601
1602 if (n_packets > 0)
1603 n_packets = pg_generate_packets (node, pg, s, n_packets);
1604
1605 s->n_packets_generated += n_packets;
1606
1607 return n_packets;
1608}
1609
1610uword
Calvin71e97c62016-08-19 16:23:14 -04001611pg_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001612{
1613 uword i;
Calvin71e97c62016-08-19 16:23:14 -04001614 pg_main_t *pg = &pg_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615 uword n_packets = 0;
Damjan Marion3a4ed392016-11-08 13:20:42 +01001616 u32 worker_index = 0;
1617
1618 if (vlib_num_workers ())
1619 worker_index = vlib_get_current_worker_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001620
Calvin71e97c62016-08-19 16:23:14 -04001621 /* *INDENT-OFF* */
Damjan Marion3a4ed392016-11-08 13:20:42 +01001622 clib_bitmap_foreach (i, pg->enabled_streams[worker_index], ({
Damjan Marion64034362016-11-07 22:19:55 +01001623 pg_stream_t *s = vec_elt_at_index (pg->streams, i);
Damjan Marion3a4ed392016-11-08 13:20:42 +01001624 n_packets += pg_input_stream (node, pg, s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001625 }));
Calvin71e97c62016-08-19 16:23:14 -04001626 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
1628 return n_packets;
1629}
1630
Calvin71e97c62016-08-19 16:23:14 -04001631/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001632VLIB_REGISTER_NODE (pg_input_node) = {
1633 .function = pg_input,
1634 .name = "pg-input",
Damjan Marion51327ac2016-11-09 11:59:42 +01001635 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001636 .type = VLIB_NODE_TYPE_INPUT,
1637
1638 .format_trace = format_pg_input_trace,
1639
1640 /* Input node will be left disabled until a stream is active. */
1641 .state = VLIB_NODE_STATE_DISABLED,
1642};
Calvin71e97c62016-08-19 16:23:14 -04001643/* *INDENT-ON* */
1644
1645/*
1646 * fd.io coding-style-patch-verification: ON
1647 *
1648 * Local Variables:
1649 * eval: (c-set-style "gnu")
1650 * End:
1651 */