1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke * Copyright (c) 2015 Cisco and/or its affiliates.
3cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
4cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
5cb9cadadSEd Warnicke * You may obtain a copy of the License at:
6cb9cadadSEd Warnicke *
7cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
8cb9cadadSEd Warnicke *
9cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
10cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
11cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
13cb9cadadSEd Warnicke * limitations under the License.
14cb9cadadSEd Warnicke */
15cb9cadadSEd Warnicke/*
16cb9cadadSEd Warnicke * pg_stream.c: packet generator streams
17cb9cadadSEd Warnicke *
18cb9cadadSEd Warnicke * Copyright (c) 2008 Eliot Dresselhaus
19cb9cadadSEd Warnicke *
20cb9cadadSEd Warnicke * Permission is hereby granted, free of charge, to any person obtaining
21cb9cadadSEd Warnicke * a copy of this software and associated documentation files (the
22cb9cadadSEd Warnicke * "Software"), to deal in the Software without restriction, including
23cb9cadadSEd Warnicke * without limitation the rights to use, copy, modify, merge, publish,
24cb9cadadSEd Warnicke * distribute, sublicense, and/or sell copies of the Software, and to
25cb9cadadSEd Warnicke * permit persons to whom the Software is furnished to do so, subject to
26cb9cadadSEd Warnicke * the following conditions:
27cb9cadadSEd Warnicke *
28cb9cadadSEd Warnicke * The above copyright notice and this permission notice shall be
29cb9cadadSEd Warnicke * included in all copies or substantial portions of the Software.
30cb9cadadSEd Warnicke *
31cb9cadadSEd Warnicke *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32cb9cadadSEd Warnicke *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33cb9cadadSEd Warnicke *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34cb9cadadSEd Warnicke *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35cb9cadadSEd Warnicke *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36cb9cadadSEd Warnicke *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37cb9cadadSEd Warnicke *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38cb9cadadSEd Warnicke */
39cb9cadadSEd Warnicke
40cb9cadadSEd Warnicke#include <vnet/vnet.h>
41cb9cadadSEd Warnicke#include <vnet/pg/pg.h>
423d9c86e9SDamjan Marion#include <vnet/ethernet/ethernet.h>
430bfe5d8cSNeale Ranns#include <vnet/ip/ip.h>
440bfe5d8cSNeale Ranns#include <vnet/mpls/mpls.h>
4551327ac5SDamjan Marion#include <vnet/devices/devices.h>
46cb9cadadSEd Warnicke
47cb9cadadSEd Warnicke/* Mark stream active or inactive. */
4871e97c63SCalvinvoid
4971e97c63SCalvinpg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int want_enabled)
50cb9cadadSEd Warnicke{
5164034367SDamjan Marion  vlib_main_t *vm;
5271e97c63SCalvin  vnet_main_t *vnm = vnet_get_main ();
5371e97c63SCalvin  pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
54cb9cadadSEd Warnicke
55cb9cadadSEd Warnicke  want_enabled = want_enabled != 0;
56cb9cadadSEd Warnicke
57cb9cadadSEd Warnicke  if (pg_stream_is_enabled (s) == want_enabled)
58cb9cadadSEd Warnicke    /* No change necessary. */
59cb9cadadSEd Warnicke    return;
6071e97c63SCalvin
61cb9cadadSEd Warnicke  if (want_enabled)
62cb9cadadSEd Warnicke    s->n_packets_generated = 0;
63cb9cadadSEd Warnicke
64cb9cadadSEd Warnicke  /* Toggle enabled flag. */
65cb9cadadSEd Warnicke  s->flags ^= PG_STREAM_FLAGS_IS_ENABLED;
66cb9cadadSEd Warnicke
6771e97c63SCalvin  ASSERT (!pool_is_free (pg->streams, s));
68cb9cadadSEd Warnicke
693a4ed393SDamjan Marion  vec_validate (pg->enabled_streams, s->worker_index);
703a4ed393SDamjan Marion  pg->enabled_streams[s->worker_index] =
713a4ed393SDamjan Marion    clib_bitmap_set (pg->enabled_streams[s->worker_index], s - pg->streams,
723a4ed393SDamjan Marion		     want_enabled);
73cb9cadadSEd Warnicke
743d9c86e9SDamjan Marion  if (want_enabled)
753d9c86e9SDamjan Marion    {
763d9c86e9SDamjan Marion      vnet_hw_interface_set_flags (vnm, pi->hw_if_index,
7771e97c63SCalvin				   VNET_HW_INTERFACE_FLAG_LINK_UP);
78cb9cadadSEd Warnicke
793d9c86e9SDamjan Marion      vnet_sw_interface_set_flags (vnm, pi->sw_if_index,
8071e97c63SCalvin				   VNET_SW_INTERFACE_FLAG_ADMIN_UP);
813d9c86e9SDamjan Marion    }
8271e97c63SCalvin
8364034367SDamjan Marion  if (vlib_num_workers ())
8464034367SDamjan Marion    vm = vlib_get_worker_vlib_main (s->worker_index);
8564034367SDamjan Marion  else
8664034367SDamjan Marion    vm = vlib_get_main ();
8764034367SDamjan Marion
8864034367SDamjan Marion  vlib_node_set_state (vm, pg_input_node.index,
893a4ed393SDamjan Marion		       (clib_bitmap_is_zero
903a4ed393SDamjan Marion			(pg->enabled_streams[s->worker_index]) ?
9164034367SDamjan Marion			VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING));
92cb9cadadSEd Warnicke
93cb9cadadSEd Warnicke  s->packet_accumulator = 0;
94cb9cadadSEd Warnicke  s->time_last_generate = 0;
95cb9cadadSEd Warnicke}
96cb9cadadSEd Warnicke
97bfe4dfa7SDamjan Marionstatic u8 *
98bfe4dfa7SDamjan Marionformat_pg_output_trace (u8 * s, va_list * va)
99bfe4dfa7SDamjan Marion{
100bfe4dfa7SDamjan Marion  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
101bfe4dfa7SDamjan Marion  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
102bfe4dfa7SDamjan Marion  pg_output_trace_t *t = va_arg (*va, pg_output_trace_t *);
103d3c008d1SChristophe Fontaine  u32 indent = format_get_indent (s);
104bfe4dfa7SDamjan Marion
105bfe4dfa7SDamjan Marion  s = format (s, "%Ubuffer 0x%x: %U",
106bfe4dfa7SDamjan Marion	      format_white_space, indent,
107bd846cdcSDamjan Marion	      t->buffer_index, format_vnet_buffer, &t->buffer);
108bfe4dfa7SDamjan Marion
109bfe4dfa7SDamjan Marion  s = format (s, "\n%U%U", format_white_space, indent,
110bfe4dfa7SDamjan Marion	      format_ethernet_header_with_length, t->buffer.pre_data,
111bfe4dfa7SDamjan Marion	      sizeof (t->buffer.pre_data));
112bfe4dfa7SDamjan Marion
113bfe4dfa7SDamjan Marion  return s;
114bfe4dfa7SDamjan Marion}
115bfe4dfa7SDamjan Marion
11671e97c63SCalvinstatic u8 *
11771e97c63SCalvinformat_pg_interface_name (u8 * s, va_list * args)
118cb9cadadSEd Warnicke{
11971e97c63SCalvin  pg_main_t *pg = &pg_main;
120cb9cadadSEd Warnicke  u32 if_index = va_arg (*args, u32);
12171e97c63SCalvin  pg_interface_t *pi;
122cb9cadadSEd Warnicke
1233d9c86e9SDamjan Marion  pi = pool_elt_at_index (pg->interfaces, if_index);
1243d9c86e9SDamjan Marion  s = format (s, "pg%d", pi->id);
125cb9cadadSEd Warnicke
126cb9cadadSEd Warnicke  return s;
127cb9cadadSEd Warnicke}
128cb9cadadSEd Warnicke
1293d9c86e9SDamjan Marionstatic clib_error_t *
1303d9c86e9SDamjan Marionpg_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1313d9c86e9SDamjan Marion{
1323d9c86e9SDamjan Marion  u32 hw_flags = 0;
1333d9c86e9SDamjan Marion
1343d9c86e9SDamjan Marion  if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1353d9c86e9SDamjan Marion    hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
1363d9c86e9SDamjan Marion
13771e97c63SCalvin  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
1383d9c86e9SDamjan Marion
1393d9c86e9SDamjan Marion  return 0;
1403d9c86e9SDamjan Marion}
1413d9c86e9SDamjan Marion
14271e97c63SCalvin/* *INDENT-OFF* */
1433d9c86e9SDamjan MarionVNET_DEVICE_CLASS (pg_dev_class) = {
144cb9cadadSEd Warnicke  .name = "pg",
145cb9cadadSEd Warnicke  .tx_function = pg_output,
146cb9cadadSEd Warnicke  .format_device_name = format_pg_interface_name,
147bfe4dfa7SDamjan Marion  .format_tx_trace = format_pg_output_trace,
1483d9c86e9SDamjan Marion  .admin_up_down_function = pg_interface_admin_up_down,
149cb9cadadSEd Warnicke};
15071e97c63SCalvin/* *INDENT-ON* */
151cb9cadadSEd Warnicke
152b80c536eSNeale Rannsstatic u8 *
153b80c536eSNeale Rannspg_build_rewrite (vnet_main_t * vnm,
154b80c536eSNeale Ranns		  u32 sw_if_index,
155b80c536eSNeale Ranns		  vnet_link_t link_type, const void *dst_address)
156cb9cadadSEd Warnicke{
157b80c536eSNeale Ranns  u8 *rewrite = NULL;
158b80c536eSNeale Ranns  u16 *h;
159cb9cadadSEd Warnicke
160b80c536eSNeale Ranns  vec_validate (rewrite, sizeof (*h) - 1);
161b80c536eSNeale Ranns  h = (u16 *) rewrite;
162b80c536eSNeale Ranns  h[0] = clib_host_to_net_u16 (vnet_link_to_l3_proto (link_type));
163cb9cadadSEd Warnicke
164b80c536eSNeale Ranns  return (rewrite);
165cb9cadadSEd Warnicke}
166cb9cadadSEd Warnicke
16771e97c63SCalvin/* *INDENT-OFF* */
168cb9cadadSEd WarnickeVNET_HW_INTERFACE_CLASS (pg_interface_class,static) = {
169cb9cadadSEd Warnicke  .name = "Packet generator",
170b80c536eSNeale Ranns  .build_rewrite = pg_build_rewrite,
171cb9cadadSEd Warnicke};
17271e97c63SCalvin/* *INDENT-ON* */
173cb9cadadSEd Warnicke
1743d9c86e9SDamjan Marionstatic u32
1753d9c86e9SDamjan Marionpg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
1763d9c86e9SDamjan Marion{
1773d9c86e9SDamjan Marion  /* nothing for now */
1783d9c86e9SDamjan Marion  return 0;
1793d9c86e9SDamjan Marion}
1803d9c86e9SDamjan Marion
18171e97c63SCalvinu32
18222e9cfd7SMohsin Kazmipg_interface_add_or_get (pg_main_t * pg, uword if_id, u8 gso_enabled,
18322e9cfd7SMohsin Kazmi			 u32 gso_size)
184cb9cadadSEd Warnicke{
18571e97c63SCalvin  vnet_main_t *vnm = vnet_get_main ();
18671e97c63SCalvin  vlib_main_t *vm = vlib_get_main ();
18771e97c63SCalvin  pg_interface_t *pi;
18871e97c63SCalvin  vnet_hw_interface_t *hi;
1893d9c86e9SDamjan Marion  uword *p;
1903d9c86e9SDamjan Marion  u32 i;
191cb9cadadSEd Warnicke
1923d9c86e9SDamjan Marion  p = hash_get (pg->if_index_by_if_id, if_id);
1933d9c86e9SDamjan Marion
1943d9c86e9SDamjan Marion  if (p)
195cb9cadadSEd Warnicke    {
1963d9c86e9SDamjan Marion      return p[0];
1973d9c86e9SDamjan Marion    }
198cb9cadadSEd Warnicke  else
199cb9cadadSEd Warnicke    {
2003d9c86e9SDamjan Marion      u8 hw_addr[6];
20171e97c63SCalvin      f64 now = vlib_time_now (vm);
2023d9c86e9SDamjan Marion      u32 rnd;
2033d9c86e9SDamjan Marion
2043d9c86e9SDamjan Marion      pool_get (pg->interfaces, pi);
2053d9c86e9SDamjan Marion      i = pi - pg->interfaces;
2063d9c86e9SDamjan Marion
2073d9c86e9SDamjan Marion      rnd = (u32) (now * 1e6);
2083d9c86e9SDamjan Marion      rnd = random_u32 (&rnd);
209178cf493SDave Barach      clib_memcpy_fast (hw_addr + 2, &rnd, sizeof (rnd));
2103d9c86e9SDamjan Marion      hw_addr[0] = 2;
2113d9c86e9SDamjan Marion      hw_addr[1] = 0xfe;
2123d9c86e9SDamjan Marion
2133d9c86e9SDamjan Marion      pi->id = if_id;
2143d9c86e9SDamjan Marion      ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr,
2153d9c86e9SDamjan Marion				   &pi->hw_if_index, pg_eth_flag_change);
216cb9cadadSEd Warnicke      hi = vnet_get_hw_interface (vnm, pi->hw_if_index);
21722e9cfd7SMohsin Kazmi      if (gso_enabled)
21822e9cfd7SMohsin Kazmi	{
21922e9cfd7SMohsin Kazmi	  hi->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
22022e9cfd7SMohsin Kazmi	  pi->gso_enabled = 1;
22122e9cfd7SMohsin Kazmi	  pi->gso_size = gso_size;
22222e9cfd7SMohsin Kazmi	}
223cb9cadadSEd Warnicke      pi->sw_if_index = hi->sw_if_index;
2243d9c86e9SDamjan Marion
2253d9c86e9SDamjan Marion      hash_set (pg->if_index_by_if_id, if_id, i);
2260bfe5d8cSNeale Ranns
22764034367SDamjan Marion      if (vlib_num_workers ())
22864034367SDamjan Marion	{
22964034367SDamjan Marion	  pi->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
23064034367SDamjan Marion					      CLIB_CACHE_LINE_BYTES);
23164034367SDamjan Marion	  *pi->lockp = 0;
23264034367SDamjan Marion	}
233cb9cadadSEd Warnicke    }
234cb9cadadSEd Warnicke
235cb9cadadSEd Warnicke  return i;
236cb9cadadSEd Warnicke}
237cb9cadadSEd Warnicke
23871e97c63SCalvinstatic void
23971e97c63SCalvindo_edit (pg_stream_t * stream,
24071e97c63SCalvin	 pg_edit_group_t * g, pg_edit_t * e, uword want_commit)
241cb9cadadSEd Warnicke{
242cb9cadadSEd Warnicke  u32 i, i0, i1, mask, n_bits_left;
24371e97c63SCalvin  u8 *v, *s, *m;
244cb9cadadSEd Warnicke
245cb9cadadSEd Warnicke  i0 = e->lsb_bit_offset / BITS (u8);
246cb9cadadSEd Warnicke
247cb9cadadSEd Warnicke  /* Make space for edit in value and mask. */
248cb9cadadSEd Warnicke  vec_validate (g->fixed_packet_data, i0);
249cb9cadadSEd Warnicke  vec_validate (g->fixed_packet_data_mask, i0);
250cb9cadadSEd Warnicke
251cb9cadadSEd Warnicke  if (e->type != PG_EDIT_FIXED)
252cb9cadadSEd Warnicke    {
253cb9cadadSEd Warnicke      switch (e->type)
254cb9cadadSEd Warnicke	{
255cb9cadadSEd Warnicke	case PG_EDIT_RANDOM:
256cb9cadadSEd Warnicke	case PG_EDIT_INCREMENT:
257cb9cadadSEd Warnicke	  e->last_increment_value = pg_edit_get_value (e, PG_EDIT_LO);
258cb9cadadSEd Warnicke	  break;
259cb9cadadSEd Warnicke
260cb9cadadSEd Warnicke	default:
261cb9cadadSEd Warnicke	  break;
262cb9cadadSEd Warnicke	}
263cb9cadadSEd Warnicke
26471e97c63SCalvin      if (want_commit)
26571e97c63SCalvin	{
26671e97c63SCalvin	  ASSERT (e->type != PG_EDIT_INVALID_TYPE);
26771e97c63SCalvin	  vec_add1 (g->non_fixed_edits, e[0]);
26871e97c63SCalvin	}
269cb9cadadSEd Warnicke      return;
270cb9cadadSEd Warnicke    }
271cb9cadadSEd Warnicke
272cb9cadadSEd Warnicke  s = g->fixed_packet_data;
273cb9cadadSEd Warnicke  m = g->fixed_packet_data_mask;
274cb9cadadSEd Warnicke
275cb9cadadSEd Warnicke  n_bits_left = e->n_bits;
276cb9cadadSEd Warnicke  i0 = e->lsb_bit_offset / BITS (u8);
277cb9cadadSEd Warnicke  i1 = e->lsb_bit_offset % BITS (u8);
278cb9cadadSEd Warnicke
279cb9cadadSEd Warnicke  v = e->values[PG_EDIT_LO];
280cb9cadadSEd Warnicke  i = pg_edit_n_alloc_bytes (e) - 1;
281cb9cadadSEd Warnicke
282cb9cadadSEd Warnicke  /* Odd low order bits?. */
283cb9cadadSEd Warnicke  if (i1 != 0 && n_bits_left > 0)
284cb9cadadSEd Warnicke    {
285cb9cadadSEd Warnicke      u32 n = clib_min (n_bits_left, BITS (u8) - i1);
286cb9cadadSEd Warnicke
287cb9cadadSEd Warnicke      mask = pow2_mask (n) << i1;
288cb9cadadSEd Warnicke
289cb9cadadSEd Warnicke      ASSERT (i0 < vec_len (s));
290cb9cadadSEd Warnicke      ASSERT (i < vec_len (v));
29171e97c63SCalvin      ASSERT ((v[i] & ~mask) == 0);
292cb9cadadSEd Warnicke
293cb9cadadSEd Warnicke      s[i0] |= v[i] & mask;
294cb9cadadSEd Warnicke      m[i0] |= mask;
295cb9cadadSEd Warnicke
296cb9cadadSEd Warnicke      i0--;
297cb9cadadSEd Warnicke      i--;
298cb9cadadSEd Warnicke      n_bits_left -= n;
299cb9cadadSEd Warnicke    }
300cb9cadadSEd Warnicke
301cb9cadadSEd Warnicke  /* Even bytes. */
302cb9cadadSEd Warnicke  while (n_bits_left >= 8)
303cb9cadadSEd Warnicke    {
304cb9cadadSEd Warnicke      ASSERT (i0 < vec_len (s));
305cb9cadadSEd Warnicke      ASSERT (i < vec_len (v));
306cb9cadadSEd Warnicke
307cb9cadadSEd Warnicke      s[i0] = v[i];
308cb9cadadSEd Warnicke      m[i0] = ~0;
309cb9cadadSEd Warnicke
310cb9cadadSEd Warnicke      i0--;
311cb9cadadSEd Warnicke      i--;
312cb9cadadSEd Warnicke      n_bits_left -= 8;
313cb9cadadSEd Warnicke    }
314cb9cadadSEd Warnicke
315cb9cadadSEd Warnicke  /* Odd high order bits. */
316cb9cadadSEd Warnicke  if (n_bits_left > 0)
317cb9cadadSEd Warnicke    {
318cb9cadadSEd Warnicke      mask = pow2_mask (n_bits_left);
319cb9cadadSEd Warnicke
320cb9cadadSEd Warnicke      ASSERT (i0 < vec_len (s));
321cb9cadadSEd Warnicke      ASSERT (i < vec_len (v));
32271e97c63SCalvin      ASSERT ((v[i] & ~mask) == 0);
323cb9cadadSEd Warnicke
324cb9cadadSEd Warnicke      s[i0] |= v[i] & mask;
325cb9cadadSEd Warnicke      m[i0] |= mask;
326cb9cadadSEd Warnicke    }
327cb9cadadSEd Warnicke
328cb9cadadSEd Warnicke  if (want_commit)
329cb9cadadSEd Warnicke    pg_edit_free (e);
330cb9cadadSEd Warnicke}
331cb9cadadSEd Warnicke
33271e97c63SCalvinvoid
33371e97c63SCalvinpg_edit_group_get_fixed_packet_data (pg_stream_t * s,
33471e97c63SCalvin				     u32 group_index,
33571e97c63SCalvin				     void *packet_data,
33671e97c63SCalvin				     void *packet_data_mask)
337cb9cadadSEd Warnicke{
33871e97c63SCalvin  pg_edit_group_t *g = pg_stream_get_group (s, group_index);
33971e97c63SCalvin  pg_edit_t *e;
340cb9cadadSEd Warnicke
34171e97c63SCalvin  vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 0);
342cb9cadadSEd Warnicke
343178cf493SDave Barach  clib_memcpy_fast (packet_data, g->fixed_packet_data,
344178cf493SDave Barach		    vec_len (g->fixed_packet_data));
345178cf493SDave Barach  clib_memcpy_fast (packet_data_mask, g->fixed_packet_data_mask,
346178cf493SDave Barach		    vec_len (g->fixed_packet_data_mask));
347cb9cadadSEd Warnicke}
348cb9cadadSEd Warnicke
34971e97c63SCalvinstatic void
35071e97c63SCalvinperform_fixed_edits (pg_stream_t * s)
351cb9cadadSEd Warnicke{
35271e97c63SCalvin  pg_edit_group_t *g;
35371e97c63SCalvin  pg_edit_t *e;
354cb9cadadSEd Warnicke  word i;
355cb9cadadSEd Warnicke
356cb9cadadSEd Warnicke  for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
357cb9cadadSEd Warnicke    {
358cb9cadadSEd Warnicke      g = vec_elt_at_index (s->edit_groups, i);
35971e97c63SCalvin      vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 1);
360cb9cadadSEd Warnicke
361cb9cadadSEd Warnicke      /* All edits have either been performed or added to
36271e97c63SCalvin         g->non_fixed_edits.  So, we can delete the vector. */
363cb9cadadSEd Warnicke      vec_free (g->edits);
364cb9cadadSEd Warnicke    }
365cb9cadadSEd Warnicke
366cb9cadadSEd Warnicke  vec_free (s->fixed_packet_data_mask);
367cb9cadadSEd Warnicke  vec_free (s->fixed_packet_data);
368cb9cadadSEd Warnicke  vec_foreach (g, s->edit_groups)
36971e97c63SCalvin  {
37071e97c63SCalvin    int i;
37171e97c63SCalvin    g->start_byte_offset = vec_len (s->fixed_packet_data);
37271e97c63SCalvin
37371e97c63SCalvin    /* Relocate and copy non-fixed edits from group to stream. */
37471e97c63SCalvin    vec_foreach (e, g->non_fixed_edits)
37571e97c63SCalvin      e->lsb_bit_offset += g->start_byte_offset * BITS (u8);
37671e97c63SCalvin
37771e97c63SCalvin    for (i = 0; i < vec_len (g->non_fixed_edits); i++)
37871e97c63SCalvin      ASSERT (g->non_fixed_edits[i].type != PG_EDIT_INVALID_TYPE);
37971e97c63SCalvin
38071e97c63SCalvin    vec_add (s->non_fixed_edits,
38171e97c63SCalvin	     g->non_fixed_edits, vec_len (g->non_fixed_edits));
38271e97c63SCalvin    vec_free (g->non_fixed_edits);
38371e97c63SCalvin
38471e97c63SCalvin    vec_add (s->fixed_packet_data,
38571e97c63SCalvin	     g->fixed_packet_data, vec_len (g->fixed_packet_data));
38671e97c63SCalvin    vec_add (s->fixed_packet_data_mask,
38771e97c63SCalvin	     g->fixed_packet_data_mask, vec_len (g->fixed_packet_data_mask));
38871e97c63SCalvin  }
389cb9cadadSEd Warnicke}
390cb9cadadSEd Warnicke
39171e97c63SCalvinvoid
39271e97c63SCalvinpg_stream_add (pg_main_t * pg, pg_stream_t * s_init)
393cb9cadadSEd Warnicke{
39464034367SDamjan Marion  vlib_main_t *vm = vlib_get_main ();
39571e97c63SCalvin  pg_stream_t *s;
39671e97c63SCalvin  uword *p;
397cb9cadadSEd Warnicke
39871e97c63SCalvin  if (!pg->stream_index_by_name)
399cb9cadadSEd Warnicke    pg->stream_index_by_name
400cb9cadadSEd Warnicke      = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword));
401cb9cadadSEd Warnicke
402cb9cadadSEd Warnicke  /* Delete any old stream with the same name. */
403cb9cadadSEd Warnicke  if (s_init->name
404cb9cadadSEd Warnicke      && (p = hash_get_mem (pg->stream_index_by_name, s_init->name)))
405cb9cadadSEd Warnicke    {
406cb9cadadSEd Warnicke      pg_stream_del (pg, p[0]);
407cb9cadadSEd Warnicke    }
408cb9cadadSEd Warnicke
409cb9cadadSEd Warnicke  pool_get (pg->streams, s);
410cb9cadadSEd Warnicke  s[0] = s_init[0];
411cb9cadadSEd Warnicke
412cb9cadadSEd Warnicke  /* Give it a name. */
41371e97c63SCalvin  if (!s->name)
414cb9cadadSEd Warnicke    s->name = format (0, "stream%d", s - pg->streams);
415cb9cadadSEd Warnicke  else
416cb9cadadSEd Warnicke    s->name = vec_dup (s->name);
417cb9cadadSEd Warnicke
418cb9cadadSEd Warnicke  hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams);
419cb9cadadSEd Warnicke
420cb9cadadSEd Warnicke  /* Get fixed part of buffer data. */
421ddbdd4f9SDamjan Marion  if (s->edit_groups)
422ddbdd4f9SDamjan Marion    perform_fixed_edits (s);
423cb9cadadSEd Warnicke
424cb9cadadSEd Warnicke  /* Determine packet size. */
425cb9cadadSEd Warnicke  switch (s->packet_size_edit_type)
426cb9cadadSEd Warnicke    {
427cb9cadadSEd Warnicke    case PG_EDIT_INCREMENT:
428cb9cadadSEd Warnicke    case PG_EDIT_RANDOM:
429cb9cadadSEd Warnicke      if (s->min_packet_bytes == s->max_packet_bytes)
430cb9cadadSEd Warnicke	s->packet_size_edit_type = PG_EDIT_FIXED;
431cb9cadadSEd Warnicke      break;
432cb9cadadSEd Warnicke
433cb9cadadSEd Warnicke    default:
434cb9cadadSEd Warnicke      /* Get packet size from fixed edits. */
435cb9cadadSEd Warnicke      s->packet_size_edit_type = PG_EDIT_FIXED;
43671e97c63SCalvin      if (!s->replay_packet_templates)
43771e97c63SCalvin	s->min_packet_bytes = s->max_packet_bytes =
43871e97c63SCalvin	  vec_len (s->fixed_packet_data);
439cb9cadadSEd Warnicke      break;
440cb9cadadSEd Warnicke    }
441cb9cadadSEd Warnicke
442cb9cadadSEd Warnicke  s->last_increment_packet_size = s->min_packet_bytes;
443cb9cadadSEd Warnicke
444cb9cadadSEd Warnicke  {
445cb9cadadSEd Warnicke    int n;
446cb9cadadSEd Warnicke
4478934a045SDamjan Marion    s->buffer_bytes = vlib_buffer_get_default_data_size (vm);
448cb9cadadSEd Warnicke    n = s->max_packet_bytes / s->buffer_bytes;
449cb9cadadSEd Warnicke    n += (s->max_packet_bytes % s->buffer_bytes) != 0;
450cb9cadadSEd Warnicke
451cb9cadadSEd Warnicke    vec_resize (s->buffer_indices, n);
452cb9cadadSEd Warnicke  }
453cb9cadadSEd Warnicke
454cb9cadadSEd Warnicke  /* Find an interface to use. */
45522e9cfd7SMohsin Kazmi  s->pg_if_index =
45622e9cfd7SMohsin Kazmi    pg_interface_add_or_get (pg, s->if_id, 0 /* gso_enabled */ ,
45722e9cfd7SMohsin Kazmi			     0 /* gso_size */ );
458cb9cadadSEd Warnicke
4597d31ab2aSDave Barach  if (s->sw_if_index[VLIB_RX] == ~0)
46071e97c63SCalvin    {
4617d31ab2aSDave Barach      pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
4627d31ab2aSDave Barach      /*
4637d31ab2aSDave Barach       * Default the RX interface if unset. It's a bad mistake to
4647d31ab2aSDave Barach       * set [VLIB_TX] prior to ip lookup, since the ip lookup code
4657d31ab2aSDave Barach       * interprets [VLIB_TX] as a fib index...
4667d31ab2aSDave Barach       */
4677d31ab2aSDave Barach      s->sw_if_index[VLIB_RX] = pi->sw_if_index;
46871e97c63SCalvin    }
469cb9cadadSEd Warnicke
470cb9cadadSEd Warnicke  /* Connect the graph. */
47151327ac5SDamjan Marion  s->next_index = vlib_node_add_next (vm, device_input_node.index,
47251327ac5SDamjan Marion				      s->node_index);
473cb9cadadSEd Warnicke}
474cb9cadadSEd Warnicke
47571e97c63SCalvinvoid
47671e97c63SCalvinpg_stream_del (pg_main_t * pg, uword index)
477cb9cadadSEd Warnicke{
47871e97c63SCalvin  pg_stream_t *s;
47971e97c63SCalvin  pg_buffer_index_t *bi;
480cb9cadadSEd Warnicke
481cb9cadadSEd Warnicke  s = pool_elt_at_index (pg->streams, index);
482cb9cadadSEd Warnicke
483cb9cadadSEd Warnicke  pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
484cb9cadadSEd Warnicke  hash_unset_mem (pg->stream_index_by_name, s->name);
485cb9cadadSEd Warnicke
486cb9cadadSEd Warnicke  vec_foreach (bi, s->buffer_indices)
48771e97c63SCalvin  {
48871e97c63SCalvin    clib_fifo_free (bi->buffer_fifo);
48971e97c63SCalvin  }
490cb9cadadSEd Warnicke
491cb9cadadSEd Warnicke  pg_stream_free (s);
492cb9cadadSEd Warnicke  pool_put (pg->streams, s);
493cb9cadadSEd Warnicke}
494cb9cadadSEd Warnicke
4954222347aSKingwel Xievoid
4964222347aSKingwel Xiepg_stream_change (pg_main_t * pg, pg_stream_t * s)
4974222347aSKingwel Xie{
4984222347aSKingwel Xie  /* Determine packet size. */
4994222347aSKingwel Xie  switch (s->packet_size_edit_type)
5004222347aSKingwel Xie    {
5014222347aSKingwel Xie    case PG_EDIT_INCREMENT:
5024222347aSKingwel Xie    case PG_EDIT_RANDOM:
5034222347aSKingwel Xie      if (s->min_packet_bytes == s->max_packet_bytes)
5044222347aSKingwel Xie	s->packet_size_edit_type = PG_EDIT_FIXED;
5054222347aSKingwel Xie    case PG_EDIT_FIXED:
5064222347aSKingwel Xie      break;
5074222347aSKingwel Xie
5084222347aSKingwel Xie    default:
5094222347aSKingwel Xie      /* Get packet size from fixed edits. */
5104222347aSKingwel Xie      s->packet_size_edit_type = PG_EDIT_FIXED;
5114222347aSKingwel Xie      if (!s->replay_packet_templates)
5124222347aSKingwel Xie	s->min_packet_bytes = s->max_packet_bytes =
5134222347aSKingwel Xie	  vec_len (s->fixed_packet_data);
5144222347aSKingwel Xie      break;
5154222347aSKingwel Xie    }
5164222347aSKingwel Xie
5174222347aSKingwel Xie  s->last_increment_packet_size = s->min_packet_bytes;
5184222347aSKingwel Xie}
5194222347aSKingwel Xie
52071e97c63SCalvin
52171e97c63SCalvin/*
52271e97c63SCalvin * fd.io coding-style-patch-verification: ON
52371e97c63SCalvin *
52471e97c63SCalvin * Local Variables:
52571e97c63SCalvin * eval: (c-set-style "gnu")
52671e97c63SCalvin * End:
52771e97c63SCalvin */
528