FD.io VPP  v16.06
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 
43 /* Mark stream active or inactive. */
44 void pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int want_enabled)
45 {
46  vnet_main_t * vnm = vnet_get_main();
48 
49  want_enabled = want_enabled != 0;
50 
51  if (pg_stream_is_enabled (s) == want_enabled)
52  /* No change necessary. */
53  return;
54 
55  if (want_enabled)
56  s->n_packets_generated = 0;
57 
58  /* Toggle enabled flag. */
60 
61  ASSERT (! pool_is_free (pg->streams, s));
62 
63  pg->enabled_streams
64  = clib_bitmap_set (pg->enabled_streams, s - pg->streams, want_enabled);
65 
67  (want_enabled
69  : 0));
70 
72  (want_enabled
74  : 0));
75 
77  pg_input_node.index,
79  ? VLIB_NODE_STATE_DISABLED
80  : VLIB_NODE_STATE_POLLING));
81 
82  s->packet_accumulator = 0;
83  s->time_last_generate = 0;
84 }
85 
86 static u8 * format_pg_interface_name (u8 * s, va_list * args)
87 {
88  pg_main_t * pg = &pg_main;
89  u32 if_index = va_arg (*args, u32);
90  pg_interface_t * pi;
91 
92  pi = vec_elt_at_index (pg->interfaces, if_index);
93  s = format (s, "pg/stream-%d", pi->stream_index);
94 
95  return s;
96 }
97 
98 VNET_DEVICE_CLASS (pg_dev_class,static) = {
99  .name = "pg",
100  .tx_function = pg_output,
101  .format_device_name = format_pg_interface_name,
102 };
103 
105  u32 sw_if_index,
106  u32 l3_type,
107  void * dst_address,
108  void * rewrite,
109  uword max_rewrite_bytes)
110 {
111  u16 * h = rewrite;
112 
113  if (max_rewrite_bytes < sizeof (h[0]))
114  return 0;
115 
116  h[0] = clib_host_to_net_u16 (l3_type);
117  return sizeof (h[0]);
118 }
119 
120 VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = {
121  .name = "Packet generator",
122  .set_rewrite = pg_set_rewrite,
123 };
124 
126 {
127  vnet_main_t * vnm = vnet_get_main();
128  pg_interface_t * pi;
130  u32 i, l;
131 
132  if ((l = vec_len (pg->free_interfaces)) > 0)
133  {
134  i = pg->free_interfaces[l - 1];
135  _vec_len (pg->free_interfaces) = l - 1;
136  pi = vec_elt_at_index (pg->interfaces, i);
137  pi->stream_index = stream_index;
138  }
139  else
140  {
141  i = vec_len (pg->interfaces);
142  vec_add2 (pg->interfaces, pi, 1);
143 
144  pi->stream_index = stream_index;
146  pg_dev_class.index, i,
147  pg_interface_class.index, stream_index);
148  hi = vnet_get_hw_interface (vnm, pi->hw_if_index);
149  pi->sw_if_index = hi->sw_if_index;
150  }
151 
152  return i;
153 }
154 
155 static void do_edit (pg_stream_t * stream,
156  pg_edit_group_t * g,
157  pg_edit_t * e,
158  uword want_commit)
159 {
160  u32 i, i0, i1, mask, n_bits_left;
161  u8 * v, * s, * m;
162 
163  i0 = e->lsb_bit_offset / BITS (u8);
164 
165  /* Make space for edit in value and mask. */
168 
169  if (e->type != PG_EDIT_FIXED)
170  {
171  switch (e->type)
172  {
173  case PG_EDIT_RANDOM:
174  case PG_EDIT_INCREMENT:
176  break;
177 
178  default:
179  break;
180  }
181 
182  if (want_commit)
183  {
185  vec_add1 (g->non_fixed_edits, e[0]);
186  }
187  return;
188  }
189 
190  s = g->fixed_packet_data;
191  m = g->fixed_packet_data_mask;
192 
193  n_bits_left = e->n_bits;
194  i0 = e->lsb_bit_offset / BITS (u8);
195  i1 = e->lsb_bit_offset % BITS (u8);
196 
197  v = e->values[PG_EDIT_LO];
198  i = pg_edit_n_alloc_bytes (e) - 1;
199 
200  /* Odd low order bits?. */
201  if (i1 != 0 && n_bits_left > 0)
202  {
203  u32 n = clib_min (n_bits_left, BITS (u8) - i1);
204 
205  mask = pow2_mask (n) << i1;
206 
207  ASSERT (i0 < vec_len (s));
208  ASSERT (i < vec_len (v));
209  ASSERT ((v[i] &~ mask) == 0);
210 
211  s[i0] |= v[i] & mask;
212  m[i0] |= mask;
213 
214  i0--;
215  i--;
216  n_bits_left -= n;
217  }
218 
219  /* Even bytes. */
220  while (n_bits_left >= 8)
221  {
222  ASSERT (i0 < vec_len (s));
223  ASSERT (i < vec_len (v));
224 
225  s[i0] = v[i];
226  m[i0] = ~0;
227 
228  i0--;
229  i--;
230  n_bits_left -= 8;
231  }
232 
233  /* Odd high order bits. */
234  if (n_bits_left > 0)
235  {
236  mask = pow2_mask (n_bits_left);
237 
238  ASSERT (i0 < vec_len (s));
239  ASSERT (i < vec_len (v));
240  ASSERT ((v[i] &~ mask) == 0);
241 
242  s[i0] |= v[i] & mask;
243  m[i0] |= mask;
244  }
245 
246  if (want_commit)
247  pg_edit_free (e);
248 }
249 
251  u32 group_index,
252  void * packet_data,
253  void * packet_data_mask)
254 {
255  pg_edit_group_t * g = pg_stream_get_group (s, group_index);
256  pg_edit_t * e;
257 
258  vec_foreach (e, g->edits)
259  do_edit (s, g, e, /* want_commit */ 0);
260 
261  clib_memcpy (packet_data, g->fixed_packet_data, vec_len (g->fixed_packet_data));
263 }
264 
266 {
267  pg_edit_group_t * g;
268  pg_edit_t * e;
269  word i;
270 
271  for (i = vec_len (s->edit_groups) - 1; i >= 0; i--)
272  {
273  g = vec_elt_at_index (s->edit_groups, i);
274  vec_foreach (e, g->edits)
275  do_edit (s, g, e, /* want_commit */ 1);
276 
277  /* All edits have either been performed or added to
278  g->non_fixed_edits. So, we can delete the vector. */
279  vec_free (g->edits);
280  }
281 
284  vec_foreach (g, s->edit_groups)
285  {
286  int i;
288 
289  /* Relocate and copy non-fixed edits from group to stream. */
292 
293  for (i = 0; i < vec_len (g->non_fixed_edits); i++)
295 
297  g->non_fixed_edits,
298  vec_len (g->non_fixed_edits));
300 
303  vec_len (g->fixed_packet_data));
306  vec_len (g->fixed_packet_data_mask));
307  }
308 }
309 
310 void pg_stream_add (pg_main_t * pg, pg_stream_t * s_init)
311 {
312  vlib_main_t * vm = pg->vlib_main;
313  pg_stream_t * s;
314  uword * p;
315 
316  if (! pg->stream_index_by_name)
318  = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword));
319 
320  /* Delete any old stream with the same name. */
321  if (s_init->name
322  && (p = hash_get_mem (pg->stream_index_by_name, s_init->name)))
323  {
324  pg_stream_del (pg, p[0]);
325  }
326 
327  pool_get (pg->streams, s);
328  s[0] = s_init[0];
329 
330  /* Give it a name. */
331  if (! s->name)
332  s->name = format (0, "stream%d", s - pg->streams);
333  else
334  s->name = vec_dup (s->name);
335 
336  hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams);
337 
338  /* Get fixed part of buffer data. */
340 
341  /* Determine packet size. */
342  switch (s->packet_size_edit_type)
343  {
344  case PG_EDIT_INCREMENT:
345  case PG_EDIT_RANDOM:
346  if (s->min_packet_bytes == s->max_packet_bytes)
348  break;
349 
350  default:
351  /* Get packet size from fixed edits. */
353  if (! s->replay_packet_templates)
355  break;
356  }
357 
359 
360  {
361  pg_buffer_index_t * bi;
362  int n;
363 
364  if (! s->buffer_bytes)
366 
368 
369  n = s->max_packet_bytes / s->buffer_bytes;
370  n += (s->max_packet_bytes % s->buffer_bytes) != 0;
371 
372  vec_resize (s->buffer_indices, n);
373 
374  vec_foreach (bi, s->buffer_indices)
376  "pg stream %d buffer #%d",
377  s - pg->streams,
378  1 + (bi - s->buffer_indices));
379  }
380 
381  /* Find an interface to use. */
382  s->pg_if_index = pg_interface_find_free (pg, s - pg->streams);
383 
384  {
386  vlib_rx_or_tx_t rx_or_tx;
387 
388  vlib_foreach_rx_tx (rx_or_tx)
389  {
390  if (s->sw_if_index[rx_or_tx] == ~0)
391  s->sw_if_index[rx_or_tx] = pi->sw_if_index;
392  }
393  }
394 
395  /* Connect the graph. */
397 }
398 
399 void pg_stream_del (pg_main_t * pg, uword index)
400 {
401  vlib_main_t * vm = pg->vlib_main;
402  pg_stream_t * s;
403  pg_buffer_index_t * bi;
404 
405  s = pool_elt_at_index (pg->streams, index);
406 
407  pg_stream_enable_disable (pg, s, /* want_enabled */ 0);
410 
411  vec_foreach (bi, s->buffer_indices)
412  {
415  }
416 
417  pg_stream_free (s);
418  pool_put (pg->streams, s);
419 }
420 
u32 sw_if_index
Definition: pg.h:281
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
Definition: edit.h:63
u32 next_index
Definition: pg.h:141
vmrglw vmrglh hi
#define PG_EDIT_LO
Definition: edit.h:81
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define clib_min(x, y)
Definition: clib.h:295
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:454
u8 * fixed_packet_data
Definition: pg.h:117
Definition: pg.h:293
static uword pg_set_rewrite(vnet_main_t *vnm, u32 sw_if_index, u32 l3_type, void *dst_address, void *rewrite, uword max_rewrite_bytes)
Definition: stream.c:104
pg_edit_group_t * edit_groups
Definition: pg.h:103
u32 pg_interface_find_free(pg_main_t *pg, uword stream_index)
Definition: stream.c:125
uword * stream_index_by_name
Definition: pg.h:304
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
#define hash_set_mem(h, key, value)
Definition: hash.h:257
u64 last_increment_value
Definition: edit.h:85
#define pool_is_free(P, E)
Definition: pool.h:189
pg_buffer_index_t * buffer_indices
Definition: pg.h:158
u32 hw_if_index
Definition: pg.h:281
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:241
pg_edit_type_t packet_size_edit_type
Definition: pg.h:105
#define pool_get(P, E)
Definition: pool.h:186
VNET_HW_INTERFACE_CLASS(pg_interface_class, static)
u8 * fixed_packet_data_mask
Definition: pg.h:117
u32 start_byte_offset
Definition: pg.h:62
u32 stream_index
Definition: pg.h:284
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:557
vlib_rx_or_tx_t
Definition: defs.h:44
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
always_inline pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:210
vlib_node_registration_t pg_input_node
(constructor) VLIB_REGISTER_NODE (pg_input_node)
Definition: input.c:1736
static u8 * format_pg_interface_name(u8 *s, va_list *args)
Definition: stream.c:86
u8 * fixed_packet_data_mask
Definition: pg.h:59
pg_edit_type_t type
Definition: edit.h:64
u8 * fixed_packet_data
Definition: pg.h:58
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
always_inline void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Definition: node_funcs.h:100
i32 lsb_bit_offset
Definition: edit.h:71
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:199
void pg_stream_del(pg_main_t *pg, uword index)
Definition: stream.c:399
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:583
#define PG_STREAM_FLAGS_IS_ENABLED
Definition: pg.h:98
u32 buffer_bytes
Definition: pg.h:121
u8 * values[2]
Definition: edit.h:80
f64 packet_accumulator
Definition: pg.h:156
u32 last_increment_packet_size
Definition: pg.h:124
#define pool_elt_at_index(p, i)
Definition: pool.h:346
#define hash_unset_mem(h, key)
Definition: hash.h:263
u32 pg_if_index
Definition: pg.h:127
pg_edit_t * non_fixed_edits
Definition: pg.h:55
u8 * name
Definition: pg.h:93
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:169
#define pool_put(P, E)
Definition: pool.h:200
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:332
vlib_node_function_t pg_output
Definition: pg.h:322
always_inline uword * clib_bitmap_set(uword *ai, uword i, uword value)
Definition: bitmap.h:132
u32 * free_interfaces
Definition: pg.h:312
u32 vlib_buffer_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
Definition: buffer.c:495
u32 n_bits
Definition: edit.h:77
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u32 min_packet_bytes
Definition: pg.h:108
u32 max_packet_bytes
Definition: pg.h:108
#define clib_memcpy(a, b, c)
Definition: string.h:63
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:250
always_inline vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
pg_edit_t * non_fixed_edits
Definition: pg.h:112
u8 ** replay_packet_templates
Definition: pg.h:160
static void perform_fixed_edits(pg_stream_t *s)
Definition: stream.c:265
pg_stream_t * streams
Definition: pg.h:298
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:373
#define ASSERT(truth)
vlib_main_t * vlib_main
Definition: pg.h:295
unsigned int u32
Definition: types.h:88
always_inline void pg_stream_free(pg_stream_t *s)
Definition: pg.h:183
#define vlib_foreach_rx_tx(v)
Definition: defs.h:50
always_inline int pg_stream_is_enabled(pg_stream_t *s)
Definition: pg.h:206
u32 free_list_index
Definition: pg.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
uword * enabled_streams
Definition: pg.h:301
void pg_stream_enable_disable(pg_main_t *pg, pg_stream_t *s, int want_enabled)
Definition: stream.c:44
always_inline void pg_edit_free(pg_edit_t *e)
Definition: edit.h:89
void pg_stream_add(pg_main_t *pg, pg_stream_t *s_init)
Definition: stream.c:310
#define clib_fifo_free(f)
Definition: fifo.h:252
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
Definition: pg.h:91
pg_main_t pg_main
Definition: init.c:44
u64 uword
Definition: types.h:112
always_inline uword clib_bitmap_is_zero(uword *ai)
Definition: bitmap.h:50
static uword pg_edit_n_alloc_bytes(pg_edit_t *e)
Definition: edit.h:118
u32 node_index
Definition: pg.h:138
u32 sw_if_index[VLIB_N_RX_TX]
Definition: pg.h:135
unsigned short u16
Definition: types.h:57
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:601
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
VNET_DEVICE_CLASS(pg_dev_class, static)
pg_edit_t * edits
Definition: pg.h:52
#define hash_get_mem(h, key)
Definition: hash.h:251
static void do_edit(pg_stream_t *stream, pg_edit_group_t *g, pg_edit_t *e, uword want_commit)
Definition: stream.c:155
u64 n_packets_generated
Definition: pg.h:144
#define vec_foreach(var, vec)
Vector iterator.
always_inline u32 vlib_buffer_round_size(u32 size)
Definition: buffer_funcs.h:269
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:462
always_inline uword pow2_mask(uword x)
Definition: clib.h:242
#define BITS(x)
Definition: clib.h:58
f64 time_last_generate
Definition: pg.h:154
pg_interface_t * interfaces
Definition: pg.h:307
void vlib_buffer_delete_free_list(vlib_main_t *vm, u32 free_list_index)
Definition: buffer.c:548
u32 flags
Definition: pg.h:95
u32 * buffer_fifo
Definition: pg.h:85