blob: 605567c1296848a1ffc25431ac61cd71e4bf85e6 [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
BenoƮt Ganne03f2a012021-07-20 16:49:13 +0200105 s = format (s, "%Ubuffer 0x%x: %U", format_white_space, indent,
106 t->buffer_index, format_vnet_buffer_no_chain, &t->buffer);
Damjan Marionbfe4dfa2017-02-03 21:16:16 +0100107
108 s = format (s, "\n%U%U", format_white_space, indent,
109 format_ethernet_header_with_length, t->buffer.pre_data,
110 sizeof (t->buffer.pre_data));
111
112 return s;
113}
114
115static u8 *
Calvin71e97c62016-08-19 16:23:14 -0400116format_pg_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
Calvin71e97c62016-08-19 16:23:14 -0400118 pg_main_t *pg = &pg_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 u32 if_index = va_arg (*args, u32);
Calvin71e97c62016-08-19 16:23:14 -0400120 pg_interface_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200122 pi = pool_elt_at_index (pg->interfaces, if_index);
123 s = format (s, "pg%d", pi->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125 return s;
126}
127
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200128static clib_error_t *
129pg_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
130{
131 u32 hw_flags = 0;
132
133 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
134 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
135
Calvin71e97c62016-08-19 16:23:14 -0400136 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200137
138 return 0;
139}
140
Neale Ranns21fb4f72020-10-05 12:26:47 +0000141static int
142pg_mac_address_cmp (const mac_address_t * m1, const mac_address_t * m2)
143{
144 return (!mac_address_cmp (m1, m2));
145}
146
147static clib_error_t *
148pg_add_del_mac_address (vnet_hw_interface_t * hi,
149 const u8 * address, u8 is_add)
150{
151 pg_main_t *pg = &pg_main;
152
153 if (ethernet_address_cast (address))
154 {
155 mac_address_t mac;
156 pg_interface_t *pi;
157
158 pi = pool_elt_at_index (pg->interfaces, hi->dev_instance);
159
160 mac_address_from_bytes (&mac, address);
161 if (is_add)
162 vec_add1 (pi->allowed_mcast_macs, mac);
163 else
164 {
165 u32 pos = vec_search_with_function (pi->allowed_mcast_macs, &mac,
166 pg_mac_address_cmp);
167 if (~0 != pos)
168 vec_del1 (pi->allowed_mcast_macs, pos);
169 }
170 }
171 return (NULL);
172}
173
Calvin71e97c62016-08-19 16:23:14 -0400174/* *INDENT-OFF* */
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200175VNET_DEVICE_CLASS (pg_dev_class) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 .name = "pg",
177 .tx_function = pg_output,
178 .format_device_name = format_pg_interface_name,
Damjan Marionbfe4dfa2017-02-03 21:16:16 +0100179 .format_tx_trace = format_pg_output_trace,
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200180 .admin_up_down_function = pg_interface_admin_up_down,
Neale Ranns21fb4f72020-10-05 12:26:47 +0000181 .mac_addr_add_del_function = pg_add_del_mac_address,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182};
Calvin71e97c62016-08-19 16:23:14 -0400183/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Neale Rannsb80c5362016-10-08 13:03:40 +0100185static u8 *
186pg_build_rewrite (vnet_main_t * vnm,
187 u32 sw_if_index,
188 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189{
Neale Rannsb80c5362016-10-08 13:03:40 +0100190 u8 *rewrite = NULL;
191 u16 *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Neale Rannsb80c5362016-10-08 13:03:40 +0100193 vec_validate (rewrite, sizeof (*h) - 1);
194 h = (u16 *) rewrite;
195 h[0] = clib_host_to_net_u16 (vnet_link_to_l3_proto (link_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Neale Rannsb80c5362016-10-08 13:03:40 +0100197 return (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198}
199
Calvin71e97c62016-08-19 16:23:14 -0400200/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = {
202 .name = "Packet generator",
Neale Rannsb80c5362016-10-08 13:03:40 +0100203 .build_rewrite = pg_build_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204};
Calvin71e97c62016-08-19 16:23:14 -0400205/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200207static u32
208pg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
209{
210 /* nothing for now */
211 return 0;
212}
213
Mohsin Kazmif382b062020-08-11 15:00:44 +0200214void
215pg_interface_enable_disable_coalesce (pg_interface_t * pi, u8 enable,
216 u32 tx_node_index)
217{
218 if (enable)
219 {
220 gro_flow_table_init (&pi->flow_table, 1 /* is_l2 */ ,
221 tx_node_index);
222 pi->coalesce_enabled = 1;
223 }
224 else
225 {
226 pi->coalesce_enabled = 0;
227 gro_flow_table_free (pi->flow_table);
228 }
229}
230
Neale Ranns6197cb72021-06-03 14:43:21 +0000231u8 *
232format_pg_tun_tx_trace (u8 *s, va_list *args)
233{
234 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
235 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
236
237 s = format (s, "PG: tunnel (no-encap)");
238 return s;
239}
240
241VNET_HW_INTERFACE_CLASS (pg_tun_hw_interface_class) = {
242 .name = "PG-tun",
243 //.format_header = format_gre_header_with_length,
244 //.unformat_header = unformat_gre_header,
245 .build_rewrite = NULL,
246 //.update_adjacency = gre_update_adj,
247 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
248};
249
Calvin71e97c62016-08-19 16:23:14 -0400250u32
Neale Ranns6197cb72021-06-03 14:43:21 +0000251pg_interface_add_or_get (pg_main_t *pg, uword if_id, u8 gso_enabled,
252 u32 gso_size, u8 coalesce_enabled,
253 pg_interface_mode_t mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
Calvin71e97c62016-08-19 16:23:14 -0400255 vnet_main_t *vnm = vnet_get_main ();
256 vlib_main_t *vm = vlib_get_main ();
257 pg_interface_t *pi;
258 vnet_hw_interface_t *hi;
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200259 uword *p;
260 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200262 p = hash_get (pg->if_index_by_if_id, if_id);
263
264 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 {
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200266 return p[0];
267 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 else
269 {
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200270 u8 hw_addr[6];
Calvin71e97c62016-08-19 16:23:14 -0400271 f64 now = vlib_time_now (vm);
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200272 u32 rnd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200274 pool_get (pg->interfaces, pi);
275 i = pi - pg->interfaces;
276
277 rnd = (u32) (now * 1e6);
278 rnd = random_u32 (&rnd);
Dave Barach178cf492018-11-13 16:34:13 -0500279 clib_memcpy_fast (hw_addr + 2, &rnd, sizeof (rnd));
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200280 hw_addr[0] = 2;
281 hw_addr[1] = 0xfe;
282
283 pi->id = if_id;
Neale Ranns6197cb72021-06-03 14:43:21 +0000284 pi->mode = mode;
285
286 switch (pi->mode)
287 {
288 case PG_MODE_ETHERNET:
289 ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr,
290 &pi->hw_if_index, pg_eth_flag_change);
291 break;
292 case PG_MODE_IP4:
293 case PG_MODE_IP6:
294 pi->hw_if_index = vnet_register_interface (
295 vnm, pg_dev_class.index, i, pg_tun_hw_interface_class.index, i);
296 break;
297 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 hi = vnet_get_hw_interface (vnm, pi->hw_if_index);
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200299 if (gso_enabled)
300 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100301 hi->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200302 pi->gso_enabled = 1;
303 pi->gso_size = gso_size;
Mohsin Kazmif382b062020-08-11 15:00:44 +0200304 if (coalesce_enabled)
305 {
306 pg_interface_enable_disable_coalesce (pi, 1, hi->tx_node_index);
307 }
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200308 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 pi->sw_if_index = hi->sw_if_index;
Damjan Marion3d9c86e2016-07-04 21:04:40 +0200310
311 hash_set (pg->if_index_by_if_id, if_id, i);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100312
Neale Ranns21fb4f72020-10-05 12:26:47 +0000313 vec_validate (pg->if_id_by_sw_if_index, hi->sw_if_index);
314 pg->if_id_by_sw_if_index[hi->sw_if_index] = i;
315
Damjan Marion64034362016-11-07 22:19:55 +0100316 if (vlib_num_workers ())
317 {
318 pi->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
319 CLIB_CACHE_LINE_BYTES);
320 *pi->lockp = 0;
321 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 }
323
324 return i;
325}
326
Calvin71e97c62016-08-19 16:23:14 -0400327static void
328do_edit (pg_stream_t * stream,
329 pg_edit_group_t * g, pg_edit_t * e, uword want_commit)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330{
331 u32 i, i0, i1, mask, n_bits_left;
Calvin71e97c62016-08-19 16:23:14 -0400332 u8 *v, *s, *m;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
334 i0 = e->lsb_bit_offset / BITS (u8);
335
336 /* Make space for edit in value and mask. */
337 vec_validate (g->fixed_packet_data, i0);
338 vec_validate (g->fixed_packet_data_mask, i0);
339
340 if (e->type != PG_EDIT_FIXED)
341 {
342 switch (e->type)
343 {
344 case PG_EDIT_RANDOM:
345 case PG_EDIT_INCREMENT:
346 e->last_increment_value = pg_edit_get_value (e, PG_EDIT_LO);
347 break;
348
349 default:
350 break;
351 }
352
Calvin71e97c62016-08-19 16:23:14 -0400353 if (want_commit)
354 {
355 ASSERT (e->type != PG_EDIT_INVALID_TYPE);
356 vec_add1 (g->non_fixed_edits, e[0]);
357 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 return;
359 }
360
361 s = g->fixed_packet_data;
362 m = g->fixed_packet_data_mask;
363
364 n_bits_left = e->n_bits;
365 i0 = e->lsb_bit_offset / BITS (u8);
366 i1 = e->lsb_bit_offset % BITS (u8);
367
368 v = e->values[PG_EDIT_LO];
369 i = pg_edit_n_alloc_bytes (e) - 1;
370
371 /* Odd low order bits?. */
372 if (i1 != 0 && n_bits_left > 0)
373 {
374 u32 n = clib_min (n_bits_left, BITS (u8) - i1);
375
376 mask = pow2_mask (n) << i1;
377
378 ASSERT (i0 < vec_len (s));
379 ASSERT (i < vec_len (v));
Calvin71e97c62016-08-19 16:23:14 -0400380 ASSERT ((v[i] & ~mask) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
382 s[i0] |= v[i] & mask;
383 m[i0] |= mask;
384
385 i0--;
386 i--;
387 n_bits_left -= n;
388 }
389
390 /* Even bytes. */
391 while (n_bits_left >= 8)
392 {
393 ASSERT (i0 < vec_len (s));
394 ASSERT (i < vec_len (v));
395
396 s[i0] = v[i];
397 m[i0] = ~0;
398
399 i0--;
400 i--;
401 n_bits_left -= 8;
402 }
403
404 /* Odd high order bits. */
405 if (n_bits_left > 0)
406 {
407 mask = pow2_mask (n_bits_left);
408
409 ASSERT (i0 < vec_len (s));
410 ASSERT (i < vec_len (v));
Calvin71e97c62016-08-19 16:23:14 -0400411 ASSERT ((v[i] & ~mask) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
413 s[i0] |= v[i] & mask;
414 m[i0] |= mask;
415 }
416
417 if (want_commit)
418 pg_edit_free (e);
419}
420
Calvin71e97c62016-08-19 16:23:14 -0400421void
422pg_edit_group_get_fixed_packet_data (pg_stream_t * s,
423 u32 group_index,
424 void *packet_data,
425 void *packet_data_mask)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426{
Calvin71e97c62016-08-19 16:23:14 -0400427 pg_edit_group_t *g = pg_stream_get_group (s, group_index);
428 pg_edit_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Calvin71e97c62016-08-19 16:23:14 -0400430 vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Dave Barach178cf492018-11-13 16:34:13 -0500432 clib_memcpy_fast (packet_data, g->fixed_packet_data,
433 vec_len (g->fixed_packet_data));
434 clib_memcpy_fast (packet_data_mask, g->fixed_packet_data_mask,
435 vec_len (g->fixed_packet_data_mask));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436}
437
Calvin71e97c62016-08-19 16:23:14 -0400438static void
439perform_fixed_edits (pg_stream_t * s)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440{
Calvin71e97c62016-08-19 16:23:14 -0400441 pg_edit_group_t *g;
442 pg_edit_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 word i;
444
445 for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
446 {
447 g = vec_elt_at_index (s->edit_groups, i);
Calvin71e97c62016-08-19 16:23:14 -0400448 vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
450 /* All edits have either been performed or added to
Calvin71e97c62016-08-19 16:23:14 -0400451 g->non_fixed_edits. So, we can delete the vector. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 vec_free (g->edits);
453 }
454
455 vec_free (s->fixed_packet_data_mask);
456 vec_free (s->fixed_packet_data);
457 vec_foreach (g, s->edit_groups)
Calvin71e97c62016-08-19 16:23:14 -0400458 {
459 int i;
460 g->start_byte_offset = vec_len (s->fixed_packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
Calvin71e97c62016-08-19 16:23:14 -0400462 /* Relocate and copy non-fixed edits from group to stream. */
463 vec_foreach (e, g->non_fixed_edits)
464 e->lsb_bit_offset += g->start_byte_offset * BITS (u8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Calvin71e97c62016-08-19 16:23:14 -0400466 for (i = 0; i < vec_len (g->non_fixed_edits); i++)
467 ASSERT (g->non_fixed_edits[i].type != PG_EDIT_INVALID_TYPE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Calvin71e97c62016-08-19 16:23:14 -0400469 vec_add (s->non_fixed_edits,
470 g->non_fixed_edits, vec_len (g->non_fixed_edits));
471 vec_free (g->non_fixed_edits);
472
473 vec_add (s->fixed_packet_data,
474 g->fixed_packet_data, vec_len (g->fixed_packet_data));
475 vec_add (s->fixed_packet_data_mask,
476 g->fixed_packet_data_mask, vec_len (g->fixed_packet_data_mask));
477 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478}
479
Calvin71e97c62016-08-19 16:23:14 -0400480void
481pg_stream_add (pg_main_t * pg, pg_stream_t * s_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482{
Damjan Marion64034362016-11-07 22:19:55 +0100483 vlib_main_t *vm = vlib_get_main ();
Calvin71e97c62016-08-19 16:23:14 -0400484 pg_stream_t *s;
485 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
Calvin71e97c62016-08-19 16:23:14 -0400487 if (!pg->stream_index_by_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 pg->stream_index_by_name
489 = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword));
490
491 /* Delete any old stream with the same name. */
492 if (s_init->name
493 && (p = hash_get_mem (pg->stream_index_by_name, s_init->name)))
494 {
495 pg_stream_del (pg, p[0]);
496 }
497
498 pool_get (pg->streams, s);
499 s[0] = s_init[0];
500
501 /* Give it a name. */
Calvin71e97c62016-08-19 16:23:14 -0400502 if (!s->name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 s->name = format (0, "stream%d", s - pg->streams);
504 else
505 s->name = vec_dup (s->name);
506
507 hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams);
508
509 /* Get fixed part of buffer data. */
Damjan Marionddbdd4f2016-07-08 22:08:16 +0200510 if (s->edit_groups)
511 perform_fixed_edits (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
513 /* Determine packet size. */
514 switch (s->packet_size_edit_type)
515 {
516 case PG_EDIT_INCREMENT:
517 case PG_EDIT_RANDOM:
518 if (s->min_packet_bytes == s->max_packet_bytes)
519 s->packet_size_edit_type = PG_EDIT_FIXED;
520 break;
521
522 default:
523 /* Get packet size from fixed edits. */
524 s->packet_size_edit_type = PG_EDIT_FIXED;
Calvin71e97c62016-08-19 16:23:14 -0400525 if (!s->replay_packet_templates)
526 s->min_packet_bytes = s->max_packet_bytes =
527 vec_len (s->fixed_packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 break;
529 }
530
531 s->last_increment_packet_size = s->min_packet_bytes;
532
533 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 int n;
535
Damjan Marion8934a042019-02-09 23:29:26 +0100536 s->buffer_bytes = vlib_buffer_get_default_data_size (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 n = s->max_packet_bytes / s->buffer_bytes;
538 n += (s->max_packet_bytes % s->buffer_bytes) != 0;
539
540 vec_resize (s->buffer_indices, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 }
542
543 /* Find an interface to use. */
Neale Ranns6197cb72021-06-03 14:43:21 +0000544 s->pg_if_index = pg_interface_add_or_get (
545 pg, s->if_id, 0 /* gso_enabled */, 0 /* gso_size */,
546 0 /* coalesce_enabled */, PG_MODE_ETHERNET);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Dave Barach7d31ab22019-05-08 19:18:18 -0400548 if (s->sw_if_index[VLIB_RX] == ~0)
Calvin71e97c62016-08-19 16:23:14 -0400549 {
Dave Barach7d31ab22019-05-08 19:18:18 -0400550 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
551 /*
552 * Default the RX interface if unset. It's a bad mistake to
553 * set [VLIB_TX] prior to ip lookup, since the ip lookup code
554 * interprets [VLIB_TX] as a fib index...
555 */
556 s->sw_if_index[VLIB_RX] = pi->sw_if_index;
Calvin71e97c62016-08-19 16:23:14 -0400557 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558
559 /* Connect the graph. */
Damjan Marion51327ac2016-11-09 11:59:42 +0100560 s->next_index = vlib_node_add_next (vm, device_input_node.index,
561 s->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562}
563
Calvin71e97c62016-08-19 16:23:14 -0400564void
565pg_stream_del (pg_main_t * pg, uword index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566{
Calvin71e97c62016-08-19 16:23:14 -0400567 pg_stream_t *s;
568 pg_buffer_index_t *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
570 s = pool_elt_at_index (pg->streams, index);
571
572 pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 hash_unset_mem (pg->stream_index_by_name, s->name);
574
575 vec_foreach (bi, s->buffer_indices)
Calvin71e97c62016-08-19 16:23:14 -0400576 {
Calvin71e97c62016-08-19 16:23:14 -0400577 clib_fifo_free (bi->buffer_fifo);
578 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
580 pg_stream_free (s);
581 pool_put (pg->streams, s);
582}
583
Kingwel Xie42223472019-01-19 22:49:26 -0500584void
585pg_stream_change (pg_main_t * pg, pg_stream_t * s)
586{
587 /* Determine packet size. */
588 switch (s->packet_size_edit_type)
589 {
590 case PG_EDIT_INCREMENT:
591 case PG_EDIT_RANDOM:
592 if (s->min_packet_bytes == s->max_packet_bytes)
593 s->packet_size_edit_type = PG_EDIT_FIXED;
594 case PG_EDIT_FIXED:
595 break;
596
597 default:
598 /* Get packet size from fixed edits. */
599 s->packet_size_edit_type = PG_EDIT_FIXED;
600 if (!s->replay_packet_templates)
601 s->min_packet_bytes = s->max_packet_bytes =
602 vec_len (s->fixed_packet_data);
603 break;
604 }
605
606 s->last_increment_packet_size = s->min_packet_bytes;
607}
608
Calvin71e97c62016-08-19 16:23:14 -0400609
610/*
611 * fd.io coding-style-patch-verification: ON
612 *
613 * Local Variables:
614 * eval: (c-set-style "gnu")
615 * End:
616 */