blob: 88c89371c6cdba4aca700b3fcca42b03e4812de9 [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_stream.c: packet generator streams
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 <vnet/vnet.h>
41#include <vnet/pg/pg.h>
Damjan Marion3d9c86e2016-07-04 21:04:40 +020042#include <vnet/ethernet/ethernet.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043#include <vnet/ip/ip.h>
44#include <vnet/mpls/mpls.h>
Damjan Marion51327ac2016-11-09 11:59:42 +010045#include <vnet/devices/devices.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47/* Mark stream active or inactive. */
Calvin71e97c62016-08-19 16:23:14 -040048void
49pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int want_enabled)
Ed Warnickecb9cada2015-12-08 15:45:58 -070050{
Damjan Marion64034362016-11-07 22:19:55 +010051 vlib_main_t *vm;
Calvin71e97c62016-08-19 16:23:14 -040052 vnet_main_t *vnm = vnet_get_main ();
53 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 want_enabled = want_enabled != 0;
56
57 if (pg_stream_is_enabled (s) == want_enabled)
58 /* No change necessary. */
59 return;
Calvin71e97c62016-08-19 16:23:14 -040060
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 if (want_enabled)
62 s->n_packets_generated = 0;
63
64 /* Toggle enabled flag. */
65 s->flags ^= PG_STREAM_FLAGS_IS_ENABLED;
66
Calvin71e97c62016-08-19 16:23:14 -040067 ASSERT (!pool_is_free (pg->streams, s));
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Damjan Marion3a4ed392016-11-08 13:20:42 +010069 vec_validate (pg->enabled_streams, s->worker_index);
70 pg->enabled_streams[s->worker_index] =
71 clib_bitmap_set (pg->enabled_streams[s->worker_index], s - pg->streams,
72 want_enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Damjan Marion3d9c86e2016-07-04 21:04:40 +020074 if (want_enabled)
75 {
76 vnet_hw_interface_set_flags (vnm, pi->hw_if_index,
Calvin71e97c62016-08-19 16:23:14 -040077 VNET_HW_INTERFACE_FLAG_LINK_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Damjan Marion3d9c86e2016-07-04 21:04:40 +020079 vnet_sw_interface_set_flags (vnm, pi->sw_if_index,
Calvin71e97c62016-08-19 16:23:14 -040080 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Damjan Marion3d9c86e2016-07-04 21:04:40 +020081 }
Calvin71e97c62016-08-19 16:23:14 -040082
Damjan Marion64034362016-11-07 22:19:55 +010083 if (vlib_num_workers ())
84 vm = vlib_get_worker_vlib_main (s->worker_index);
85 else
86 vm = vlib_get_main ();
87
88 vlib_node_set_state (vm, pg_input_node.index,
Damjan Marion3a4ed392016-11-08 13:20:42 +010089 (clib_bitmap_is_zero
90 (pg->enabled_streams[s->worker_index]) ?
Damjan Marion64034362016-11-07 22:19:55 +010091 VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING));
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
93 s->packet_accumulator = 0;
94 s->time_last_generate = 0;
95}
96
Calvin71e97c62016-08-19 16:23:14 -040097static u8 *
Damjan Marionbfe4dfa2017-02-03 21:16:16 +010098format_pg_output_trace (u8 * s, va_list * va)
99{
100 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
101 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
102 pg_output_trace_t *t = va_arg (*va, pg_output_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200103 u32 indent = format_get_indent (s);
Damjan Marionbfe4dfa2017-02-03 21:16:16 +0100104
105 s = format (s, "%Ubuffer 0x%x: %U",
106 format_white_space, indent,
Damjan Marionbd846cd2017-11-21 13:12:41 +0100107 t->buffer_index, format_vnet_buffer, &t->buffer);
Damjan Marionbfe4dfa2017-02-03 21:16:16 +0100108
109 s = format (s, "\n%U%U", format_white_space, indent,
110 format_ethernet_header_with_length, t->buffer.pre_data,
111 sizeof (t->buffer.pre_data));
112
113 return s;
114}
115
116static u8 *
Calvin71e97c62016-08-19 16:23:14 -0400117format_pg_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118{
Calvin71e97c62016-08-19 16:23:14 -0400119 pg_main_t *pg = &pg_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 u32 if_index = va_arg (*args, u32);
Calvin71e97c62016-08-19 16:23:14 -0400121 pg_interface_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200123 pi = pool_elt_at_index (pg->interfaces, if_index);
124 s = format (s, "pg%d", pi->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126 return s;
127}
128
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200129static clib_error_t *
130pg_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
131{
132 u32 hw_flags = 0;
133
134 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
135 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
136
Calvin71e97c62016-08-19 16:23:14 -0400137 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200138
139 return 0;
140}
141
Calvin71e97c62016-08-19 16:23:14 -0400142/* *INDENT-OFF* */
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200143VNET_DEVICE_CLASS (pg_dev_class) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 .name = "pg",
145 .tx_function = pg_output,
146 .format_device_name = format_pg_interface_name,
Damjan Marionbfe4dfa2017-02-03 21:16:16 +0100147 .format_tx_trace = format_pg_output_trace,
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200148 .admin_up_down_function = pg_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149};
Calvin71e97c62016-08-19 16:23:14 -0400150/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Neale Rannsb80c5362016-10-08 13:03:40 +0100152static u8 *
153pg_build_rewrite (vnet_main_t * vnm,
154 u32 sw_if_index,
155 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
Neale Rannsb80c5362016-10-08 13:03:40 +0100157 u8 *rewrite = NULL;
158 u16 *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Neale Rannsb80c5362016-10-08 13:03:40 +0100160 vec_validate (rewrite, sizeof (*h) - 1);
161 h = (u16 *) rewrite;
162 h[0] = clib_host_to_net_u16 (vnet_link_to_l3_proto (link_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Neale Rannsb80c5362016-10-08 13:03:40 +0100164 return (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165}
166
Calvin71e97c62016-08-19 16:23:14 -0400167/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = {
169 .name = "Packet generator",
Neale Rannsb80c5362016-10-08 13:03:40 +0100170 .build_rewrite = pg_build_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171};
Calvin71e97c62016-08-19 16:23:14 -0400172/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200174static u32
175pg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
176{
177 /* nothing for now */
178 return 0;
179}
180
Mohsin Kazmif382b062020-08-11 15:00:44 +0200181void
182pg_interface_enable_disable_coalesce (pg_interface_t * pi, u8 enable,
183 u32 tx_node_index)
184{
185 if (enable)
186 {
187 gro_flow_table_init (&pi->flow_table, 1 /* is_l2 */ ,
188 tx_node_index);
189 pi->coalesce_enabled = 1;
190 }
191 else
192 {
193 pi->coalesce_enabled = 0;
194 gro_flow_table_free (pi->flow_table);
195 }
196}
197
Calvin71e97c62016-08-19 16:23:14 -0400198u32
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200199pg_interface_add_or_get (pg_main_t * pg, uword if_id, u8 gso_enabled,
Mohsin Kazmif382b062020-08-11 15:00:44 +0200200 u32 gso_size, u8 coalesce_enabled)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201{
Calvin71e97c62016-08-19 16:23:14 -0400202 vnet_main_t *vnm = vnet_get_main ();
203 vlib_main_t *vm = vlib_get_main ();
204 pg_interface_t *pi;
205 vnet_hw_interface_t *hi;
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200206 uword *p;
207 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200209 p = hash_get (pg->if_index_by_if_id, if_id);
210
211 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 {
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200213 return p[0];
214 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 else
216 {
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200217 u8 hw_addr[6];
Calvin71e97c62016-08-19 16:23:14 -0400218 f64 now = vlib_time_now (vm);
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200219 u32 rnd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200221 pool_get (pg->interfaces, pi);
222 i = pi - pg->interfaces;
223
224 rnd = (u32) (now * 1e6);
225 rnd = random_u32 (&rnd);
Dave Barach178cf492018-11-13 16:34:13 -0500226 clib_memcpy_fast (hw_addr + 2, &rnd, sizeof (rnd));
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200227 hw_addr[0] = 2;
228 hw_addr[1] = 0xfe;
229
230 pi->id = if_id;
231 ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr,
232 &pi->hw_if_index, pg_eth_flag_change);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 hi = vnet_get_hw_interface (vnm, pi->hw_if_index);
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200234 if (gso_enabled)
235 {
236 hi->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200237 pi->gso_enabled = 1;
238 pi->gso_size = gso_size;
Mohsin Kazmif382b062020-08-11 15:00:44 +0200239 if (coalesce_enabled)
240 {
241 pg_interface_enable_disable_coalesce (pi, 1, hi->tx_node_index);
242 }
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200243 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 pi->sw_if_index = hi->sw_if_index;
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200245
246 hash_set (pg->if_index_by_if_id, if_id, i);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247
Damjan Marion64034362016-11-07 22:19:55 +0100248 if (vlib_num_workers ())
249 {
250 pi->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
251 CLIB_CACHE_LINE_BYTES);
252 *pi->lockp = 0;
253 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 }
255
256 return i;
257}
258
Calvin71e97c62016-08-19 16:23:14 -0400259static void
260do_edit (pg_stream_t * stream,
261 pg_edit_group_t * g, pg_edit_t * e, uword want_commit)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262{
263 u32 i, i0, i1, mask, n_bits_left;
Calvin71e97c62016-08-19 16:23:14 -0400264 u8 *v, *s, *m;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266 i0 = e->lsb_bit_offset / BITS (u8);
267
268 /* Make space for edit in value and mask. */
269 vec_validate (g->fixed_packet_data, i0);
270 vec_validate (g->fixed_packet_data_mask, i0);
271
272 if (e->type != PG_EDIT_FIXED)
273 {
274 switch (e->type)
275 {
276 case PG_EDIT_RANDOM:
277 case PG_EDIT_INCREMENT:
278 e->last_increment_value = pg_edit_get_value (e, PG_EDIT_LO);
279 break;
280
281 default:
282 break;
283 }
284
Calvin71e97c62016-08-19 16:23:14 -0400285 if (want_commit)
286 {
287 ASSERT (e->type != PG_EDIT_INVALID_TYPE);
288 vec_add1 (g->non_fixed_edits, e[0]);
289 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 return;
291 }
292
293 s = g->fixed_packet_data;
294 m = g->fixed_packet_data_mask;
295
296 n_bits_left = e->n_bits;
297 i0 = e->lsb_bit_offset / BITS (u8);
298 i1 = e->lsb_bit_offset % BITS (u8);
299
300 v = e->values[PG_EDIT_LO];
301 i = pg_edit_n_alloc_bytes (e) - 1;
302
303 /* Odd low order bits?. */
304 if (i1 != 0 && n_bits_left > 0)
305 {
306 u32 n = clib_min (n_bits_left, BITS (u8) - i1);
307
308 mask = pow2_mask (n) << i1;
309
310 ASSERT (i0 < vec_len (s));
311 ASSERT (i < vec_len (v));
Calvin71e97c62016-08-19 16:23:14 -0400312 ASSERT ((v[i] & ~mask) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
314 s[i0] |= v[i] & mask;
315 m[i0] |= mask;
316
317 i0--;
318 i--;
319 n_bits_left -= n;
320 }
321
322 /* Even bytes. */
323 while (n_bits_left >= 8)
324 {
325 ASSERT (i0 < vec_len (s));
326 ASSERT (i < vec_len (v));
327
328 s[i0] = v[i];
329 m[i0] = ~0;
330
331 i0--;
332 i--;
333 n_bits_left -= 8;
334 }
335
336 /* Odd high order bits. */
337 if (n_bits_left > 0)
338 {
339 mask = pow2_mask (n_bits_left);
340
341 ASSERT (i0 < vec_len (s));
342 ASSERT (i < vec_len (v));
Calvin71e97c62016-08-19 16:23:14 -0400343 ASSERT ((v[i] & ~mask) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
345 s[i0] |= v[i] & mask;
346 m[i0] |= mask;
347 }
348
349 if (want_commit)
350 pg_edit_free (e);
351}
352
Calvin71e97c62016-08-19 16:23:14 -0400353void
354pg_edit_group_get_fixed_packet_data (pg_stream_t * s,
355 u32 group_index,
356 void *packet_data,
357 void *packet_data_mask)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
Calvin71e97c62016-08-19 16:23:14 -0400359 pg_edit_group_t *g = pg_stream_get_group (s, group_index);
360 pg_edit_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Calvin71e97c62016-08-19 16:23:14 -0400362 vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
Dave Barach178cf492018-11-13 16:34:13 -0500364 clib_memcpy_fast (packet_data, g->fixed_packet_data,
365 vec_len (g->fixed_packet_data));
366 clib_memcpy_fast (packet_data_mask, g->fixed_packet_data_mask,
367 vec_len (g->fixed_packet_data_mask));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368}
369
Calvin71e97c62016-08-19 16:23:14 -0400370static void
371perform_fixed_edits (pg_stream_t * s)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372{
Calvin71e97c62016-08-19 16:23:14 -0400373 pg_edit_group_t *g;
374 pg_edit_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 word i;
376
377 for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
378 {
379 g = vec_elt_at_index (s->edit_groups, i);
Calvin71e97c62016-08-19 16:23:14 -0400380 vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
382 /* All edits have either been performed or added to
Calvin71e97c62016-08-19 16:23:14 -0400383 g->non_fixed_edits. So, we can delete the vector. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 vec_free (g->edits);
385 }
386
387 vec_free (s->fixed_packet_data_mask);
388 vec_free (s->fixed_packet_data);
389 vec_foreach (g, s->edit_groups)
Calvin71e97c62016-08-19 16:23:14 -0400390 {
391 int i;
392 g->start_byte_offset = vec_len (s->fixed_packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Calvin71e97c62016-08-19 16:23:14 -0400394 /* Relocate and copy non-fixed edits from group to stream. */
395 vec_foreach (e, g->non_fixed_edits)
396 e->lsb_bit_offset += g->start_byte_offset * BITS (u8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
Calvin71e97c62016-08-19 16:23:14 -0400398 for (i = 0; i < vec_len (g->non_fixed_edits); i++)
399 ASSERT (g->non_fixed_edits[i].type != PG_EDIT_INVALID_TYPE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400
Calvin71e97c62016-08-19 16:23:14 -0400401 vec_add (s->non_fixed_edits,
402 g->non_fixed_edits, vec_len (g->non_fixed_edits));
403 vec_free (g->non_fixed_edits);
404
405 vec_add (s->fixed_packet_data,
406 g->fixed_packet_data, vec_len (g->fixed_packet_data));
407 vec_add (s->fixed_packet_data_mask,
408 g->fixed_packet_data_mask, vec_len (g->fixed_packet_data_mask));
409 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410}
411
Calvin71e97c62016-08-19 16:23:14 -0400412void
413pg_stream_add (pg_main_t * pg, pg_stream_t * s_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414{
Damjan Marion64034362016-11-07 22:19:55 +0100415 vlib_main_t *vm = vlib_get_main ();
Calvin71e97c62016-08-19 16:23:14 -0400416 pg_stream_t *s;
417 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Calvin71e97c62016-08-19 16:23:14 -0400419 if (!pg->stream_index_by_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 pg->stream_index_by_name
421 = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword));
422
423 /* Delete any old stream with the same name. */
424 if (s_init->name
425 && (p = hash_get_mem (pg->stream_index_by_name, s_init->name)))
426 {
427 pg_stream_del (pg, p[0]);
428 }
429
430 pool_get (pg->streams, s);
431 s[0] = s_init[0];
432
433 /* Give it a name. */
Calvin71e97c62016-08-19 16:23:14 -0400434 if (!s->name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 s->name = format (0, "stream%d", s - pg->streams);
436 else
437 s->name = vec_dup (s->name);
438
439 hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams);
440
441 /* Get fixed part of buffer data. */
Damjan Marionddbdd4f2016-07-08 22:08:16 +0200442 if (s->edit_groups)
443 perform_fixed_edits (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
445 /* Determine packet size. */
446 switch (s->packet_size_edit_type)
447 {
448 case PG_EDIT_INCREMENT:
449 case PG_EDIT_RANDOM:
450 if (s->min_packet_bytes == s->max_packet_bytes)
451 s->packet_size_edit_type = PG_EDIT_FIXED;
452 break;
453
454 default:
455 /* Get packet size from fixed edits. */
456 s->packet_size_edit_type = PG_EDIT_FIXED;
Calvin71e97c62016-08-19 16:23:14 -0400457 if (!s->replay_packet_templates)
458 s->min_packet_bytes = s->max_packet_bytes =
459 vec_len (s->fixed_packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 break;
461 }
462
463 s->last_increment_packet_size = s->min_packet_bytes;
464
465 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 int n;
467
Damjan Marion8934a042019-02-09 23:29:26 +0100468 s->buffer_bytes = vlib_buffer_get_default_data_size (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469 n = s->max_packet_bytes / s->buffer_bytes;
470 n += (s->max_packet_bytes % s->buffer_bytes) != 0;
471
472 vec_resize (s->buffer_indices, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 }
474
475 /* Find an interface to use. */
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200476 s->pg_if_index =
477 pg_interface_add_or_get (pg, s->if_id, 0 /* gso_enabled */ ,
Mohsin Kazmif382b062020-08-11 15:00:44 +0200478 0 /* gso_size */ , 0 /* coalesce_enabled */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
Dave Barach7d31ab22019-05-08 19:18:18 -0400480 if (s->sw_if_index[VLIB_RX] == ~0)
Calvin71e97c62016-08-19 16:23:14 -0400481 {
Dave Barach7d31ab22019-05-08 19:18:18 -0400482 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
483 /*
484 * Default the RX interface if unset. It's a bad mistake to
485 * set [VLIB_TX] prior to ip lookup, since the ip lookup code
486 * interprets [VLIB_TX] as a fib index...
487 */
488 s->sw_if_index[VLIB_RX] = pi->sw_if_index;
Calvin71e97c62016-08-19 16:23:14 -0400489 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
491 /* Connect the graph. */
Damjan Marion51327ac2016-11-09 11:59:42 +0100492 s->next_index = vlib_node_add_next (vm, device_input_node.index,
493 s->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494}
495
Calvin71e97c62016-08-19 16:23:14 -0400496void
497pg_stream_del (pg_main_t * pg, uword index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498{
Calvin71e97c62016-08-19 16:23:14 -0400499 pg_stream_t *s;
500 pg_buffer_index_t *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
502 s = pool_elt_at_index (pg->streams, index);
503
504 pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 hash_unset_mem (pg->stream_index_by_name, s->name);
506
507 vec_foreach (bi, s->buffer_indices)
Calvin71e97c62016-08-19 16:23:14 -0400508 {
Calvin71e97c62016-08-19 16:23:14 -0400509 clib_fifo_free (bi->buffer_fifo);
510 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
512 pg_stream_free (s);
513 pool_put (pg->streams, s);
514}
515
Kingwel Xie42223472019-01-19 22:49:26 -0500516void
517pg_stream_change (pg_main_t * pg, pg_stream_t * s)
518{
519 /* Determine packet size. */
520 switch (s->packet_size_edit_type)
521 {
522 case PG_EDIT_INCREMENT:
523 case PG_EDIT_RANDOM:
524 if (s->min_packet_bytes == s->max_packet_bytes)
525 s->packet_size_edit_type = PG_EDIT_FIXED;
526 case PG_EDIT_FIXED:
527 break;
528
529 default:
530 /* Get packet size from fixed edits. */
531 s->packet_size_edit_type = PG_EDIT_FIXED;
532 if (!s->replay_packet_templates)
533 s->min_packet_bytes = s->max_packet_bytes =
534 vec_len (s->fixed_packet_data);
535 break;
536 }
537
538 s->last_increment_packet_size = s->min_packet_bytes;
539}
540
Calvin71e97c62016-08-19 16:23:14 -0400541
542/*
543 * fd.io coding-style-patch-verification: ON
544 *
545 * Local Variables:
546 * eval: (c-set-style "gnu")
547 * End:
548 */