FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
stream.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * pg_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>
42 #include <vnet/ethernet/ethernet.h>
43 #include <vnet/ip/ip.h>
44 #include <vnet/mpls/mpls.h>
45 #include <vnet/devices/devices.h>
46 
47 /* Mark stream active or inactive. */
48 void
49 pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int want_enabled)
50 {
51  vlib_main_t *vm;
52  vnet_main_t *vnm = vnet_get_main ();
54 
55  want_enabled = want_enabled != 0;
56 
57  if (pg_stream_is_enabled (s) == want_enabled)
58  /* No change necessary. */
59  return;
60 
61  if (want_enabled)
62  s->n_packets_generated = 0;
63 
64  /* Toggle enabled flag. */
66 
67  ASSERT (!pool_is_free (pg->streams, s));
68 
72  want_enabled);
73 
74  if (want_enabled)
75  {
78 
81  }
82 
83  if (vlib_num_workers ())
85  else
86  vm = vlib_get_main ();
87 
90  (pg->enabled_streams[s->worker_index]) ?
91  VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING));
92 
93  s->packet_accumulator = 0;
94  s->time_last_generate = 0;
95 }
96 
97 static u8 *
98 format_pg_interface_name (u8 * s, va_list * args)
99 {
100  pg_main_t *pg = &pg_main;
101  u32 if_index = va_arg (*args, u32);
102  pg_interface_t *pi;
103 
104  pi = pool_elt_at_index (pg->interfaces, if_index);
105  s = format (s, "pg%d", pi->id);
106 
107  return s;
108 }
109 
110 static clib_error_t *
112 {
113  u32 hw_flags = 0;
114 
117 
118  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
119 
120  return 0;
121 }
122 
123 /* *INDENT-OFF* */
125  .name = "pg",
126  .tx_function = pg_output,
127  .format_device_name = format_pg_interface_name,
128  .admin_up_down_function = pg_interface_admin_up_down,
129 };
130 /* *INDENT-ON* */
131 
132 static u8 *
134  u32 sw_if_index,
135  vnet_link_t link_type, const void *dst_address)
136 {
137  u8 *rewrite = NULL;
138  u16 *h;
139 
140  vec_validate (rewrite, sizeof (*h) - 1);
141  h = (u16 *) rewrite;
142  h[0] = clib_host_to_net_u16 (vnet_link_to_l3_proto (link_type));
143 
144  return (rewrite);
145 }
146 
147 /* *INDENT-OFF* */
148 VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = {
149  .name = "Packet generator",
150  .build_rewrite = pg_build_rewrite,
151 };
152 /* *INDENT-ON* */
153 
154 static u32
156 {
157  /* nothing for now */
158  return 0;
159 }
160 
161 u32
163 {
164  vnet_main_t *vnm = vnet_get_main ();
165  vlib_main_t *vm = vlib_get_main ();
166  pg_interface_t *pi;
168  uword *p;
169  u32 i;
170 
171  p = hash_get (pg->if_index_by_if_id, if_id);
172 
173  if (p)
174  {
175  return p[0];
176  }
177  else
178  {
179  u8 hw_addr[6];
180  f64 now = vlib_time_now (vm);
181  u32 rnd;
182 
183  pool_get (pg->interfaces, pi);
184  i = pi - pg->interfaces;
185 
186  rnd = (u32) (now * 1e6);
187  rnd = random_u32 (&rnd);
188  clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
189  hw_addr[0] = 2;
190  hw_addr[1] = 0xfe;
191 
192  pi->id = if_id;
193  ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr,
195  hi = vnet_get_hw_interface (vnm, pi->hw_if_index);
196  pi->sw_if_index = hi->sw_if_index;
197 
198  hash_set (pg->if_index_by_if_id, if_id, i);
199 
200  if (vlib_num_workers ())
201  {
204  *pi->lockp = 0;
205  }
206 
210  }
211 
212  return i;
213 }
214 
215 static void
217  pg_edit_group_t * g, pg_edit_t * e, uword want_commit)
218 {
219  u32 i, i0, i1, mask, n_bits_left;
220  u8 *v, *s, *m;
221 
222  i0 = e->lsb_bit_offset / BITS (u8);
223 
224  /* Make space for edit in value and mask. */
227 
228  if (e->type != PG_EDIT_FIXED)
229  {
230  switch (e->type)
231  {
232  case PG_EDIT_RANDOM:
233  case PG_EDIT_INCREMENT:
235  break;
236 
237  default:
238  break;
239  }
240 
241  if (want_commit)
242  {
244  vec_add1 (g->non_fixed_edits, e[0]);
245  }
246  return;
247  }
248 
249  s = g->fixed_packet_data;
250  m = g->fixed_packet_data_mask;
251 
252  n_bits_left = e->n_bits;
253  i0 = e->lsb_bit_offset / BITS (u8);
254  i1 = e->lsb_bit_offset % BITS (u8);
255 
256  v = e->values[PG_EDIT_LO];
257  i = pg_edit_n_alloc_bytes (e) - 1;
258 
259  /* Odd low order bits?. */
260  if (i1 != 0 && n_bits_left > 0)
261  {
262  u32 n = clib_min (n_bits_left, BITS (u8) - i1);
263 
264  mask = pow2_mask (n) << i1;
265 
266  ASSERT (i0 < vec_len (s));
267  ASSERT (i < vec_len (v));
268  ASSERT ((v[i] & ~mask) == 0);
269 
270  s[i0] |= v[i] & mask;
271  m[i0] |= mask;
272 
273  i0--;
274  i--;
275  n_bits_left -= n;
276  }
277 
278  /* Even bytes. */
279  while (n_bits_left >= 8)
280  {
281  ASSERT (i0 < vec_len (s));
282  ASSERT (i < vec_len (v));
283 
284  s[i0] = v[i];
285  m[i0] = ~0;
286 
287  i0--;
288  i--;
289  n_bits_left -= 8;
290  }
291 
292  /* Odd high order bits. */
293  if (n_bits_left > 0)
294  {
295  mask = pow2_mask (n_bits_left);
296 
297  ASSERT (i0 < vec_len (s));
298  ASSERT (i < vec_len (v));
299  ASSERT ((v[i] & ~mask) == 0);
300 
301  s[i0] |= v[i] & mask;
302  m[i0] |= mask;
303  }
304 
305  if (want_commit)
306  pg_edit_free (e);
307 }
308 
309 void
311  u32 group_index,
312  void *packet_data,
313  void *packet_data_mask)
314 {
315  pg_edit_group_t *g = pg_stream_get_group (s, group_index);
316  pg_edit_t *e;
317 
318  vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 0);
319 
320  clib_memcpy (packet_data, g->fixed_packet_data,
322  clib_memcpy (packet_data_mask, g->fixed_packet_data_mask,
324 }
325 
326 static void
328 {
329  pg_edit_group_t *g;
330  pg_edit_t *e;
331  word i;
332 
333  for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
334  {
335  g = vec_elt_at_index (s->edit_groups, i);
336  vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 1);
337 
338  /* All edits have either been performed or added to
339  g->non_fixed_edits. So, we can delete the vector. */
340  vec_free (g->edits);
341  }
342 
345  vec_foreach (g, s->edit_groups)
346  {
347  int i;
349 
350  /* Relocate and copy non-fixed edits from group to stream. */
353 
354  for (i = 0; i < vec_len (g->non_fixed_edits); i++)
356 
358  g->non_fixed_edits, vec_len (g->non_fixed_edits));
360 
362  g->fixed_packet_data, vec_len (g->fixed_packet_data));
365  }
366 }
367 
368 void
370 {
371  vlib_main_t *vm = vlib_get_main ();
372  pg_stream_t *s;
373  uword *p;
374 
375  if (!pg->stream_index_by_name)
377  = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword));
378 
379  /* Delete any old stream with the same name. */
380  if (s_init->name
381  && (p = hash_get_mem (pg->stream_index_by_name, s_init->name)))
382  {
383  pg_stream_del (pg, p[0]);
384  }
385 
386  pool_get (pg->streams, s);
387  s[0] = s_init[0];
388 
389  /* Give it a name. */
390  if (!s->name)
391  s->name = format (0, "stream%d", s - pg->streams);
392  else
393  s->name = vec_dup (s->name);
394 
395  hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams);
396 
397  /* Get fixed part of buffer data. */
398  if (s->edit_groups)
400 
401  /* Determine packet size. */
402  switch (s->packet_size_edit_type)
403  {
404  case PG_EDIT_INCREMENT:
405  case PG_EDIT_RANDOM:
406  if (s->min_packet_bytes == s->max_packet_bytes)
408  break;
409 
410  default:
411  /* Get packet size from fixed edits. */
413  if (!s->replay_packet_templates)
416  break;
417  }
418 
420 
421  {
422  pg_buffer_index_t *bi;
423  int n;
424 
425 #if DPDK > 0
427 #endif
428 
429  if (!s->buffer_bytes)
431 
433 
434  n = s->max_packet_bytes / s->buffer_bytes;
435  n += (s->max_packet_bytes % s->buffer_bytes) != 0;
436 
437  vec_resize (s->buffer_indices, n);
438 
439  vec_foreach (bi, s->buffer_indices)
440  {
441  bi->free_list_index =
443  "pg stream %d buffer #%d",
444  s - pg->streams,
445  1 + (bi - s->buffer_indices));
446  }
447  }
448 
449  /* Find an interface to use. */
451 
452  {
454  vlib_rx_or_tx_t rx_or_tx;
455 
456  vlib_foreach_rx_tx (rx_or_tx)
457  {
458  if (s->sw_if_index[rx_or_tx] == ~0)
459  s->sw_if_index[rx_or_tx] = pi->sw_if_index;
460  }
461  }
462 
463  /* Connect the graph. */
465  s->node_index);
466 }
467 
468 void
470 {
471  vlib_main_t *vm = vlib_get_main ();
472  pg_stream_t *s;
473  pg_buffer_index_t *bi;
474 
475  s = pool_elt_at_index (pg->streams, index);
476 
477  pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
479 
480  vec_foreach (bi, s->buffer_indices)
481  {
484  }
485 
486  pg_stream_free (s);
487  pool_put (pg->streams, s);
488 }
489 
490 
491 /*
492  * fd.io coding-style-patch-verification: ON
493  *
494  * Local Variables:
495  * eval: (c-set-style "gnu")
496  * End:
497  */
u32 sw_if_index
Definition: pg.h:294
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
Definition: edit.h:64
u32 next_index
Definition: pg.h:150
static int pg_stream_is_enabled(pg_stream_t *s)
Definition: pg.h:213
vmrglw vmrglh hi
#define PG_EDIT_LO
Definition: edit.h:83
void mpls_sw_interface_enable_disable(mpls_main_t *mm, u32 sw_if_index, u8 is_enable)
Definition: interface.c:38
#define hash_set(h, key, value)
Definition: hash.h:254
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define clib_min(x, y)
Definition: clib.h:326
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:531
u8 * fixed_packet_data
Definition: pg.h:123
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
Definition: pg.h:310
pg_edit_group_t * edit_groups
Definition: pg.h:109
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
uword * stream_index_by_name
Definition: pg.h:319
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
VNET_DEVICE_CLASS(pg_dev_class)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
u32 worker_index
Definition: pg.h:147
static clib_error_t * pg_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: stream.c:111
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
#define hash_set_mem(h, key, value)
Definition: hash.h:274
u64 last_increment_value
Definition: edit.h:87
#define pool_is_free(P, E)
Use free bitmap to query whether given element is free.
Definition: pool.h:203
pg_buffer_index_t * buffer_indices
Definition: pg.h:169
u32 hw_if_index
Definition: pg.h:294
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:377
pg_edit_type_t packet_size_edit_type
Definition: pg.h:111
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1063
VNET_HW_INTERFACE_CLASS(pg_interface_class, static)
u8 * fixed_packet_data_mask
Definition: pg.h:123
u32 start_byte_offset
Definition: pg.h:67
static pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:219
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:559
vlib_rx_or_tx_t
Definition: defs.h:44
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vlib_node_registration_t pg_input_node
(constructor) VLIB_REGISTER_NODE (pg_input_node)
Definition: input.c:1648
static u8 * format_pg_interface_name(u8 *s, va_list *args)
Definition: stream.c:98
static uword pow2_mask(uword x)
Definition: clib.h:251
u8 * fixed_packet_data_mask
Definition: pg.h:64
static uword clib_bitmap_is_zero(uword *ai)
predicate function; is an entire bitmap empty?
Definition: bitmap.h:57
pg_edit_type_t type
Definition: edit.h:66
u8 * fixed_packet_data
Definition: pg.h:63
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
i32 lsb_bit_offset
Definition: edit.h:73
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:201
void pg_stream_del(pg_main_t *pg, uword index)
Definition: stream.c:469
#define PG_STREAM_FLAGS_IS_ENABLED
Definition: pg.h:104
uword * if_index_by_if_id
Definition: pg.h:323
u32 buffer_bytes
Definition: pg.h:127
static u32 pg_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: stream.c:155
volatile u32 * lockp
Definition: pg.h:291
u8 * values[2]
Definition: edit.h:82
f64 packet_accumulator
Definition: pg.h:167
#define hash_get(h, key)
Definition: hash.h:248
u32 last_increment_packet_size
Definition: pg.h:130
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
#define hash_unset_mem(h, key)
Definition: hash.h:280
u32 pg_if_index
Definition: pg.h:133
void ip4_sw_interface_enable_disable(u32 sw_if_index, u32 is_enable)
Definition: ip4_forward.c:807
pg_edit_t * non_fixed_edits
Definition: pg.h:60
#define v
Definition: acl.c:314
u8 * name
Definition: pg.h:99
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:173
static void pg_stream_free(pg_stream_t *s)
Definition: pg.h:193
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:334
vlib_node_function_t pg_output
Definition: pg.h:336
uword ** enabled_streams
Definition: pg.h:316
u32 n_bits
Definition: edit.h:79
static u8 * pg_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Definition: stream.c:133
mpls_main_t mpls_main
Definition: mpls.c:25
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
u32 min_packet_bytes
Definition: pg.h:114
u32 max_packet_bytes
Definition: pg.h:114
u32 pg_interface_add_or_get(pg_main_t *pg, uword if_id)
Definition: stream.c:162
#define clib_memcpy(a, b, c)
Definition: string.h:69
void pg_edit_group_get_fixed_packet_data(pg_stream_t *s, u32 group_index, void *packet_data, void *packet_data_mask)
Definition: stream.c:310
pg_edit_t * non_fixed_edits
Definition: pg.h:118
u8 ** replay_packet_templates
Definition: pg.h:171
static void perform_fixed_edits(pg_stream_t *s)
Definition: stream.c:327
pg_stream_t * streams
Definition: pg.h:313
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:528
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vlib_foreach_rx_tx(v)
Definition: defs.h:51
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
u32 free_list_index
Definition: pg.h:93
void pg_stream_enable_disable(pg_main_t *pg, pg_stream_t *s, int want_enabled)
Definition: stream.c:49
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:226
void pg_stream_add(pg_main_t *pg, pg_stream_t *s_init)
Definition: stream.c:369
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:146
#define clib_fifo_free(f)
Definition: fifo.h:257
Definition: pg.h:96
pg_main_t pg_main
Definition: init.c:44
void vlib_buffer_delete_free_list(vlib_main_t *vm, u32 free_list_index)
u64 uword
Definition: types.h:112
static uword pg_edit_n_alloc_bytes(pg_edit_t *e)
Definition: edit.h:120
u32 node_index
Definition: pg.h:144
u32 sw_if_index[VLIB_N_RX_TX]
Definition: pg.h:141
unsigned short u16
Definition: types.h:57
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:644
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
u32 id
Definition: pg.h:297
unsigned char u8
Definition: types.h:56
vnet_l3_packet_type_t vnet_link_to_l3_proto(vnet_link_t link)
Convert a link to to an Ethertype.
Definition: interface.c:1361
u32 if_id
Definition: pg.h:152
pg_edit_t * edits
Definition: pg.h:57
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:51
#define hash_get_mem(h, key)
Definition: hash.h:268
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:117
static void do_edit(pg_stream_t *stream, pg_edit_group_t *g, pg_edit_t *e, uword want_commit)
Definition: stream.c:216
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static u32 vlib_num_workers()
Definition: threads.h:340
u64 n_packets_generated
Definition: pg.h:155
#define vec_foreach(var, vec)
Vector iterator.
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:539
static void pg_edit_free(pg_edit_t *e)
Definition: edit.h:91
vnet_device_class_t pg_dev_class
u32 flags
Definition: vhost-user.h:75
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
#define BITS(x)
Definition: clib.h:58
f64 time_last_generate
Definition: pg.h:165
void ip6_sw_interface_enable_disable(u32 sw_if_index, u32 is_enable)
Definition: ip6_forward.c:391
pg_interface_t * interfaces
Definition: pg.h:322
u32 vlib_buffer_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
vlib_node_registration_t device_input_node
(constructor) VLIB_REGISTER_NODE (device_input_node)
Definition: devices.c:30
static vlib_main_t * vlib_get_worker_vlib_main(u32 worker_index)
Definition: threads.h:364
static u32 vlib_buffer_round_size(u32 size)
Definition: buffer_funcs.h:251
u32 flags
Definition: pg.h:101
u32 * buffer_fifo
Definition: pg.h:90