vhost-user: fix crash in vhost_user_intfc_tx (VPP-519)
[vpp.git] / vnet / vnet / pg / pg.h
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.h: VLIB packet generator
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 #ifndef included_vlib_pg_h
41 #define included_vlib_pg_h
42
43 #include <vlib/vlib.h>          /* for VLIB_N_RX_TX */
44 #include <vnet/pg/edit.h>
45 #include <vppinfra/fifo.h>      /* for buffer_fifo */
46 #include <vnet/unix/pcap.h>
47 #include <vnet/interface.h>
48
49 extern vnet_device_class_t pg_dev_class;
50
51 struct pg_main_t;
52 struct pg_stream_t;
53
54 typedef struct pg_edit_group_t
55 {
56   /* Edits in this group. */
57   pg_edit_t *edits;
58
59   /* Vector of non-fixed edits for this group. */
60   pg_edit_t *non_fixed_edits;
61
62   /* Fixed edits for this group. */
63   u8 *fixed_packet_data;
64   u8 *fixed_packet_data_mask;
65
66   /* Byte offset where packet data begins. */
67   u32 start_byte_offset;
68
69   /* Number of packet bytes for this edit group. */
70   u32 n_packet_bytes;
71
72   /* Function to perform miscellaneous edits (e.g. set IP checksum, ...). */
73   void (*edit_function) (struct pg_main_t * pg,
74                          struct pg_stream_t * s,
75                          struct pg_edit_group_t * g,
76                          u32 * buffers, u32 n_buffers);
77
78   /* Opaque data for edit function's use. */
79   uword edit_function_opaque;
80 } pg_edit_group_t;
81
82 /* Packets are made of multiple buffers chained together.
83    This struct keeps track of data per-chain index. */
84 typedef struct
85 {
86   /* Vector of buffer edits for this stream and buffer index. */
87   pg_edit_t *edits;
88
89   /* Buffers pre-initialized with fixed buffer data for this stream. */
90   u32 *buffer_fifo;
91
92   /* Buffer free list for this buffer index in stream. */
93   u32 free_list_index;
94 } pg_buffer_index_t;
95
96 typedef struct pg_stream_t
97 {
98   /* Stream name. */
99   u8 *name;
100
101   u32 flags;
102
103   /* Stream is currently enabled. */
104 #define PG_STREAM_FLAGS_IS_ENABLED (1 << 0)
105 #define PG_STREAM_FLAGS_DISABLE_BUFFER_RECYCLE (1 << 1)
106
107   /* Edit groups are created by each protocol level (e.g. ethernet,
108      ip4, tcp, ...). */
109   pg_edit_group_t *edit_groups;
110
111   pg_edit_type_t packet_size_edit_type;
112
113   /* Min/max packet size. */
114   u32 min_packet_bytes, max_packet_bytes;
115
116   /* Vector of non-fixed edits for this stream.
117      All fixed edits are performed and placed into fixed_packet_data. */
118   pg_edit_t *non_fixed_edits;
119
120   /* Packet data with all fixed edits performed.
121      All packets in stream are initialized according with this data.
122      Mask specifies which bits of packet data are covered by fixed edits. */
123   u8 *fixed_packet_data, *fixed_packet_data_mask;
124
125   /* Size to use for buffers.  0 means use buffers big enough
126      for max_packet_bytes. */
127   u32 buffer_bytes;
128
129   /* Last packet length if packet size edit type is increment. */
130   u32 last_increment_packet_size;
131
132   /* Index into main interface pool for this stream. */
133   u32 pg_if_index;
134
135   /* Interface used to mark packets for this stream.  May be different
136      than hw/sw index from pg main interface pool.  They will be
137      different if this stream is being used generate buffers as if
138      they were received on a non-pg interface.  For example, suppose you
139      are trying to test vlan code and you want to generate buffers that
140      appear to come from an ethernet interface. */
141   u32 sw_if_index[VLIB_N_RX_TX];
142
143   /* Node where stream's buffers get put. */
144   u32 node_index;
145
146   /* Output next index to reach output node from stream input node. */
147   u32 next_index;
148
149   u32 if_id;
150
151   /* Number of packets currently generated. */
152   u64 n_packets_generated;
153
154   /* Stream is disabled when packet limit is reached.
155      Zero means no packet limit. */
156   u64 n_packets_limit;
157
158   /* Rate for this stream in packets/second.
159      Zero means unlimited rate. */
160   f64 rate_packets_per_second;
161
162   f64 time_last_generate;
163
164   f64 packet_accumulator;
165
166   pg_buffer_index_t *buffer_indices;
167
168   u8 **replay_packet_templates;
169   u32 current_replay_packet_index;
170 } pg_stream_t;
171
172 always_inline void
173 pg_buffer_index_free (pg_buffer_index_t * bi)
174 {
175   vec_free (bi->edits);
176   clib_fifo_free (bi->buffer_fifo);
177 }
178
179 always_inline void
180 pg_edit_group_free (pg_edit_group_t * g)
181 {
182   pg_edit_t *e;
183   vec_foreach (e, g->edits) pg_edit_free (e);
184   vec_free (g->edits);
185   vec_free (g->fixed_packet_data);
186   vec_free (g->fixed_packet_data_mask);
187 }
188
189 always_inline void
190 pg_stream_free (pg_stream_t * s)
191 {
192   pg_edit_group_t *g;
193   pg_edit_t *e;
194   vec_foreach (e, s->non_fixed_edits) pg_edit_free (e);
195   vec_free (s->non_fixed_edits);
196   vec_foreach (g, s->edit_groups) pg_edit_group_free (g);
197   vec_free (s->edit_groups);
198   vec_free (s->fixed_packet_data);
199   vec_free (s->fixed_packet_data_mask);
200   vec_free (s->name);
201
202   {
203     pg_buffer_index_t *bi;
204     vec_foreach (bi, s->buffer_indices) pg_buffer_index_free (bi);
205     vec_free (s->buffer_indices);
206   }
207 }
208
209 always_inline int
210 pg_stream_is_enabled (pg_stream_t * s)
211 {
212   return (s->flags & PG_STREAM_FLAGS_IS_ENABLED) != 0;
213 }
214
215 always_inline pg_edit_group_t *
216 pg_stream_get_group (pg_stream_t * s, u32 group_index)
217 {
218   return vec_elt_at_index (s->edit_groups, group_index);
219 }
220
221 always_inline void *
222 pg_create_edit_group (pg_stream_t * s,
223                       int n_edit_bytes, int n_packet_bytes, u32 * group_index)
224 {
225   pg_edit_group_t *g;
226   int n_edits;
227
228   vec_add2 (s->edit_groups, g, 1);
229   if (group_index)
230     *group_index = g - s->edit_groups;
231
232   ASSERT (n_edit_bytes % sizeof (pg_edit_t) == 0);
233   n_edits = n_edit_bytes / sizeof (pg_edit_t);
234   vec_resize (g->edits, n_edits);
235
236   g->n_packet_bytes = n_packet_bytes;
237
238   return g->edits;
239 }
240
241 always_inline void *
242 pg_add_edits (pg_stream_t * s, int n_edit_bytes, int n_packet_bytes,
243               u32 group_index)
244 {
245   pg_edit_group_t *g = pg_stream_get_group (s, group_index);
246   pg_edit_t *e;
247   int n_edits;
248   ASSERT (n_edit_bytes % sizeof (pg_edit_t) == 0);
249   n_edits = n_edit_bytes / sizeof (pg_edit_t);
250   vec_add2 (g->edits, e, n_edits);
251   g->n_packet_bytes += n_packet_bytes;
252   return e;
253 }
254
255 always_inline void *
256 pg_get_edit_group (pg_stream_t * s, u32 group_index)
257 {
258   pg_edit_group_t *g = pg_stream_get_group (s, group_index);
259   return g->edits;
260 }
261
262 /* Number of bytes for all groups >= given group. */
263 always_inline uword
264 pg_edit_group_n_bytes (pg_stream_t * s, u32 group_index)
265 {
266   pg_edit_group_t *g;
267   uword n_bytes = 0;
268
269   for (g = s->edit_groups + group_index; g < vec_end (s->edit_groups); g++)
270     n_bytes += g->n_packet_bytes;
271   return n_bytes;
272 }
273
274 always_inline void
275 pg_free_edit_group (pg_stream_t * s)
276 {
277   uword i = vec_len (s->edit_groups) - 1;
278   pg_edit_group_t *g = pg_stream_get_group (s, i);
279
280   pg_edit_group_free (g);
281   memset (g, 0, sizeof (g[0]));
282   _vec_len (s->edit_groups) = i;
283 }
284
285 typedef struct
286 {
287   /* VLIB interface indices. */
288   u32 hw_if_index, sw_if_index;
289
290   /* Identifies stream for this interface. */
291   u32 id;
292
293   pcap_main_t pcap_main;
294   u8 *pcap_file_name;
295 } pg_interface_t;
296
297 /* Per VLIB node data. */
298 typedef struct
299 {
300   /* Parser function indexed by node index. */
301   unformat_function_t *unformat_edit;
302 } pg_node_t;
303
304 typedef struct pg_main_t
305 {
306   /* Back pointer to main structure. */
307   vlib_main_t *vlib_main;
308
309   /* Pool of streams. */
310   pg_stream_t *streams;
311
312   /* Bitmap indicating which streams are currently enabled. */
313   uword *enabled_streams;
314
315   /* Hash mapping name -> stream index. */
316   uword *stream_index_by_name;
317
318   /* Pool of interfaces. */
319   pg_interface_t *interfaces;
320   uword *if_index_by_if_id;
321
322   /* Per VLIB node information. */
323   pg_node_t *nodes;
324 } pg_main_t;
325
326 /* Global main structure. */
327 extern pg_main_t pg_main;
328
329 /* Global node. */
330 extern vlib_node_registration_t pg_input_node;
331
332 /* Buffer generator input, output node functions. */
333 vlib_node_function_t pg_input, pg_output;
334
335 /* Stream add/delete. */
336 void pg_stream_del (pg_main_t * pg, uword index);
337 void pg_stream_add (pg_main_t * pg, pg_stream_t * s_init);
338
339 /* Enable/disable stream. */
340 void pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s,
341                                int is_enable);
342
343 /* Find/create free packet-generator interface index. */
344 u32 pg_interface_add_or_get (pg_main_t * pg, uword stream_index);
345
346 always_inline pg_node_t *
347 pg_get_node (uword node_index)
348 {
349   pg_main_t *pg = &pg_main;
350   vec_validate (pg->nodes, node_index);
351   return pg->nodes + node_index;
352 }
353
354 void pg_edit_group_get_fixed_packet_data (pg_stream_t * s,
355                                           u32 group_index,
356                                           void *fixed_packet_data,
357                                           void *fixed_packet_data_mask);
358
359 void pg_enable_disable (u32 stream_index, int is_enable);
360
361 typedef struct
362 {
363   u32 hw_if_index;
364   u32 dev_instance;
365   u8 is_enabled;
366   u8 *pcap_file_name;
367   u32 count;
368 } pg_capture_args_t;
369
370 clib_error_t *pg_capture (pg_capture_args_t * a);
371
372 #endif /* included_vlib_pg_h */
373
374 /*
375  * fd.io coding-style-patch-verification: ON
376  *
377  * Local Variables:
378  * eval: (c-set-style "gnu")
379  * End:
380  */