Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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> |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 42 | #include <vnet/ethernet/ethernet.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 43 | #include <vnet/ip/ip.h> |
| 44 | #include <vnet/mpls/mpls.h> |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 45 | #include <vnet/devices/devices.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | /* Mark stream active or inactive. */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 48 | void |
| 49 | pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int want_enabled) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 51 | vlib_main_t *vm; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 52 | vnet_main_t *vnm = vnet_get_main (); |
| 53 | pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
| 55 | want_enabled = want_enabled != 0; |
| 56 | |
| 57 | if (pg_stream_is_enabled (s) == want_enabled) |
| 58 | /* No change necessary. */ |
| 59 | return; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 60 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | if (want_enabled) |
| 62 | s->n_packets_generated = 0; |
| 63 | |
| 64 | /* Toggle enabled flag. */ |
| 65 | s->flags ^= PG_STREAM_FLAGS_IS_ENABLED; |
| 66 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 67 | ASSERT (!pool_is_free (pg->streams, s)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
Damjan Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 69 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 74 | if (want_enabled) |
| 75 | { |
| 76 | vnet_hw_interface_set_flags (vnm, pi->hw_if_index, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 77 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 79 | vnet_sw_interface_set_flags (vnm, pi->sw_if_index, |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 80 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 81 | } |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 82 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 83 | 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 Marion | 3a4ed39 | 2016-11-08 13:20:42 +0100 | [diff] [blame] | 89 | (clib_bitmap_is_zero |
| 90 | (pg->enabled_streams[s->worker_index]) ? |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 91 | VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
| 93 | s->packet_accumulator = 0; |
| 94 | s->time_last_generate = 0; |
| 95 | } |
| 96 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 97 | static u8 * |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 98 | format_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 Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 103 | u32 indent = format_get_indent (s); |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 104 | |
| 105 | s = format (s, "%Ubuffer 0x%x: %U", |
| 106 | format_white_space, indent, |
Damjan Marion | bd846cd | 2017-11-21 13:12:41 +0100 | [diff] [blame] | 107 | t->buffer_index, format_vnet_buffer, &t->buffer); |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 108 | |
| 109 | s = format (s, "\n%U%U", format_white_space, indent, |
| 110 | format_ethernet_header_with_length, t->buffer.pre_data, |
| 111 | sizeof (t->buffer.pre_data)); |
| 112 | |
| 113 | return s; |
| 114 | } |
| 115 | |
| 116 | static u8 * |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 117 | format_pg_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 119 | pg_main_t *pg = &pg_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | u32 if_index = va_arg (*args, u32); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 121 | pg_interface_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 123 | pi = pool_elt_at_index (pg->interfaces, if_index); |
| 124 | s = format (s, "pg%d", pi->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
| 126 | return s; |
| 127 | } |
| 128 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 129 | static clib_error_t * |
| 130 | pg_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 131 | { |
| 132 | u32 hw_flags = 0; |
| 133 | |
| 134 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 135 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
| 136 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 137 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 142 | /* *INDENT-OFF* */ |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 143 | VNET_DEVICE_CLASS (pg_dev_class) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | .name = "pg", |
| 145 | .tx_function = pg_output, |
| 146 | .format_device_name = format_pg_interface_name, |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 147 | .format_tx_trace = format_pg_output_trace, |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 148 | .admin_up_down_function = pg_interface_admin_up_down, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | }; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 150 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 152 | static u8 * |
| 153 | pg_build_rewrite (vnet_main_t * vnm, |
| 154 | u32 sw_if_index, |
| 155 | vnet_link_t link_type, const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 157 | u8 *rewrite = NULL; |
| 158 | u16 *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 160 | vec_validate (rewrite, sizeof (*h) - 1); |
| 161 | h = (u16 *) rewrite; |
| 162 | h[0] = clib_host_to_net_u16 (vnet_link_to_l3_proto (link_type)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 164 | return (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 167 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = { |
| 169 | .name = "Packet generator", |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 170 | .build_rewrite = pg_build_rewrite, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | }; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 172 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 174 | static u32 |
| 175 | pg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) |
| 176 | { |
| 177 | /* nothing for now */ |
| 178 | return 0; |
| 179 | } |
| 180 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 181 | u32 |
| 182 | pg_interface_add_or_get (pg_main_t * pg, uword if_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 184 | vnet_main_t *vnm = vnet_get_main (); |
| 185 | vlib_main_t *vm = vlib_get_main (); |
| 186 | pg_interface_t *pi; |
| 187 | vnet_hw_interface_t *hi; |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 188 | uword *p; |
| 189 | u32 i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 191 | p = hash_get (pg->if_index_by_if_id, if_id); |
| 192 | |
| 193 | if (p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | { |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 195 | return p[0]; |
| 196 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | else |
| 198 | { |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 199 | u8 hw_addr[6]; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 200 | f64 now = vlib_time_now (vm); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 201 | u32 rnd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 203 | pool_get (pg->interfaces, pi); |
| 204 | i = pi - pg->interfaces; |
| 205 | |
| 206 | rnd = (u32) (now * 1e6); |
| 207 | rnd = random_u32 (&rnd); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 208 | clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd)); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 209 | hw_addr[0] = 2; |
| 210 | hw_addr[1] = 0xfe; |
| 211 | |
| 212 | pi->id = if_id; |
| 213 | ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr, |
| 214 | &pi->hw_if_index, pg_eth_flag_change); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | hi = vnet_get_hw_interface (vnm, pi->hw_if_index); |
| 216 | pi->sw_if_index = hi->sw_if_index; |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 217 | |
| 218 | hash_set (pg->if_index_by_if_id, if_id, i); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 219 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 220 | if (vlib_num_workers ()) |
| 221 | { |
| 222 | pi->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, |
| 223 | CLIB_CACHE_LINE_BYTES); |
| 224 | *pi->lockp = 0; |
| 225 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return i; |
| 229 | } |
| 230 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 231 | static void |
| 232 | do_edit (pg_stream_t * stream, |
| 233 | pg_edit_group_t * g, pg_edit_t * e, uword want_commit) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | { |
| 235 | u32 i, i0, i1, mask, n_bits_left; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 236 | u8 *v, *s, *m; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | |
| 238 | i0 = e->lsb_bit_offset / BITS (u8); |
| 239 | |
| 240 | /* Make space for edit in value and mask. */ |
| 241 | vec_validate (g->fixed_packet_data, i0); |
| 242 | vec_validate (g->fixed_packet_data_mask, i0); |
| 243 | |
| 244 | if (e->type != PG_EDIT_FIXED) |
| 245 | { |
| 246 | switch (e->type) |
| 247 | { |
| 248 | case PG_EDIT_RANDOM: |
| 249 | case PG_EDIT_INCREMENT: |
| 250 | e->last_increment_value = pg_edit_get_value (e, PG_EDIT_LO); |
| 251 | break; |
| 252 | |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 257 | if (want_commit) |
| 258 | { |
| 259 | ASSERT (e->type != PG_EDIT_INVALID_TYPE); |
| 260 | vec_add1 (g->non_fixed_edits, e[0]); |
| 261 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
| 265 | s = g->fixed_packet_data; |
| 266 | m = g->fixed_packet_data_mask; |
| 267 | |
| 268 | n_bits_left = e->n_bits; |
| 269 | i0 = e->lsb_bit_offset / BITS (u8); |
| 270 | i1 = e->lsb_bit_offset % BITS (u8); |
| 271 | |
| 272 | v = e->values[PG_EDIT_LO]; |
| 273 | i = pg_edit_n_alloc_bytes (e) - 1; |
| 274 | |
| 275 | /* Odd low order bits?. */ |
| 276 | if (i1 != 0 && n_bits_left > 0) |
| 277 | { |
| 278 | u32 n = clib_min (n_bits_left, BITS (u8) - i1); |
| 279 | |
| 280 | mask = pow2_mask (n) << i1; |
| 281 | |
| 282 | ASSERT (i0 < vec_len (s)); |
| 283 | ASSERT (i < vec_len (v)); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 284 | ASSERT ((v[i] & ~mask) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
| 286 | s[i0] |= v[i] & mask; |
| 287 | m[i0] |= mask; |
| 288 | |
| 289 | i0--; |
| 290 | i--; |
| 291 | n_bits_left -= n; |
| 292 | } |
| 293 | |
| 294 | /* Even bytes. */ |
| 295 | while (n_bits_left >= 8) |
| 296 | { |
| 297 | ASSERT (i0 < vec_len (s)); |
| 298 | ASSERT (i < vec_len (v)); |
| 299 | |
| 300 | s[i0] = v[i]; |
| 301 | m[i0] = ~0; |
| 302 | |
| 303 | i0--; |
| 304 | i--; |
| 305 | n_bits_left -= 8; |
| 306 | } |
| 307 | |
| 308 | /* Odd high order bits. */ |
| 309 | if (n_bits_left > 0) |
| 310 | { |
| 311 | mask = pow2_mask (n_bits_left); |
| 312 | |
| 313 | ASSERT (i0 < vec_len (s)); |
| 314 | ASSERT (i < vec_len (v)); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 315 | ASSERT ((v[i] & ~mask) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
| 317 | s[i0] |= v[i] & mask; |
| 318 | m[i0] |= mask; |
| 319 | } |
| 320 | |
| 321 | if (want_commit) |
| 322 | pg_edit_free (e); |
| 323 | } |
| 324 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 325 | void |
| 326 | pg_edit_group_get_fixed_packet_data (pg_stream_t * s, |
| 327 | u32 group_index, |
| 328 | void *packet_data, |
| 329 | void *packet_data_mask) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 331 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
| 332 | pg_edit_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 334 | vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 336 | clib_memcpy (packet_data, g->fixed_packet_data, |
| 337 | vec_len (g->fixed_packet_data)); |
| 338 | clib_memcpy (packet_data_mask, g->fixed_packet_data_mask, |
| 339 | vec_len (g->fixed_packet_data_mask)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 342 | static void |
| 343 | perform_fixed_edits (pg_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 345 | pg_edit_group_t *g; |
| 346 | pg_edit_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | word i; |
| 348 | |
| 349 | for (i = vec_len (s->edit_groups) - 1; i >= 0; i--) |
| 350 | { |
| 351 | g = vec_elt_at_index (s->edit_groups, i); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 352 | vec_foreach (e, g->edits) do_edit (s, g, e, /* want_commit */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | |
| 354 | /* All edits have either been performed or added to |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 355 | g->non_fixed_edits. So, we can delete the vector. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | vec_free (g->edits); |
| 357 | } |
| 358 | |
| 359 | vec_free (s->fixed_packet_data_mask); |
| 360 | vec_free (s->fixed_packet_data); |
| 361 | vec_foreach (g, s->edit_groups) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 362 | { |
| 363 | int i; |
| 364 | g->start_byte_offset = vec_len (s->fixed_packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 366 | /* Relocate and copy non-fixed edits from group to stream. */ |
| 367 | vec_foreach (e, g->non_fixed_edits) |
| 368 | e->lsb_bit_offset += g->start_byte_offset * BITS (u8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 370 | for (i = 0; i < vec_len (g->non_fixed_edits); i++) |
| 371 | ASSERT (g->non_fixed_edits[i].type != PG_EDIT_INVALID_TYPE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 373 | vec_add (s->non_fixed_edits, |
| 374 | g->non_fixed_edits, vec_len (g->non_fixed_edits)); |
| 375 | vec_free (g->non_fixed_edits); |
| 376 | |
| 377 | vec_add (s->fixed_packet_data, |
| 378 | g->fixed_packet_data, vec_len (g->fixed_packet_data)); |
| 379 | vec_add (s->fixed_packet_data_mask, |
| 380 | g->fixed_packet_data_mask, vec_len (g->fixed_packet_data_mask)); |
| 381 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 384 | void |
| 385 | pg_stream_add (pg_main_t * pg, pg_stream_t * s_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 387 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 388 | pg_stream_t *s; |
| 389 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 391 | if (!pg->stream_index_by_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | pg->stream_index_by_name |
| 393 | = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword)); |
| 394 | |
| 395 | /* Delete any old stream with the same name. */ |
| 396 | if (s_init->name |
| 397 | && (p = hash_get_mem (pg->stream_index_by_name, s_init->name))) |
| 398 | { |
| 399 | pg_stream_del (pg, p[0]); |
| 400 | } |
| 401 | |
| 402 | pool_get (pg->streams, s); |
| 403 | s[0] = s_init[0]; |
| 404 | |
| 405 | /* Give it a name. */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 406 | if (!s->name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | s->name = format (0, "stream%d", s - pg->streams); |
| 408 | else |
| 409 | s->name = vec_dup (s->name); |
| 410 | |
| 411 | hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams); |
| 412 | |
| 413 | /* Get fixed part of buffer data. */ |
Damjan Marion | ddbdd4f | 2016-07-08 22:08:16 +0200 | [diff] [blame] | 414 | if (s->edit_groups) |
| 415 | perform_fixed_edits (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | |
| 417 | /* Determine packet size. */ |
| 418 | switch (s->packet_size_edit_type) |
| 419 | { |
| 420 | case PG_EDIT_INCREMENT: |
| 421 | case PG_EDIT_RANDOM: |
| 422 | if (s->min_packet_bytes == s->max_packet_bytes) |
| 423 | s->packet_size_edit_type = PG_EDIT_FIXED; |
| 424 | break; |
| 425 | |
| 426 | default: |
| 427 | /* Get packet size from fixed edits. */ |
| 428 | s->packet_size_edit_type = PG_EDIT_FIXED; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 429 | if (!s->replay_packet_templates) |
| 430 | s->min_packet_bytes = s->max_packet_bytes = |
| 431 | vec_len (s->fixed_packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | |
| 435 | s->last_increment_packet_size = s->min_packet_bytes; |
| 436 | |
| 437 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 438 | pg_buffer_index_t *bi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | int n; |
| 440 | |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 441 | if (vm->buffer_main->callbacks_registered) |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 442 | s->buffer_bytes = VLIB_BUFFER_DATA_SIZE; |
Juraj Sloboda | e3371af | 2016-12-02 14:37:16 +0100 | [diff] [blame] | 443 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 444 | if (!s->buffer_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | s->buffer_bytes = s->max_packet_bytes; |
| 446 | |
| 447 | s->buffer_bytes = vlib_buffer_round_size (s->buffer_bytes); |
| 448 | |
| 449 | n = s->max_packet_bytes / s->buffer_bytes; |
| 450 | n += (s->max_packet_bytes % s->buffer_bytes) != 0; |
| 451 | |
| 452 | vec_resize (s->buffer_indices, n); |
| 453 | |
| 454 | vec_foreach (bi, s->buffer_indices) |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 455 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 456 | bi->free_list_index = |
Damjan Marion | 409ef61 | 2016-11-11 21:35:18 +0100 | [diff] [blame] | 457 | vlib_buffer_create_free_list (vm, s->buffer_bytes, |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 458 | "pg stream %d buffer #%d", |
| 459 | s - pg->streams, |
| 460 | 1 + (bi - s->buffer_indices)); |
| 461 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Find an interface to use. */ |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 465 | s->pg_if_index = pg_interface_add_or_get (pg, s->if_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | |
| 467 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 468 | pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | vlib_rx_or_tx_t rx_or_tx; |
| 470 | |
| 471 | vlib_foreach_rx_tx (rx_or_tx) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 472 | { |
| 473 | if (s->sw_if_index[rx_or_tx] == ~0) |
| 474 | s->sw_if_index[rx_or_tx] = pi->sw_if_index; |
| 475 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* Connect the graph. */ |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 479 | s->next_index = vlib_node_add_next (vm, device_input_node.index, |
| 480 | s->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 483 | void |
| 484 | pg_stream_del (pg_main_t * pg, uword index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 486 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 487 | pg_stream_t *s; |
| 488 | pg_buffer_index_t *bi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
| 490 | s = pool_elt_at_index (pg->streams, index); |
| 491 | |
| 492 | pg_stream_enable_disable (pg, s, /* want_enabled */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | hash_unset_mem (pg->stream_index_by_name, s->name); |
| 494 | |
| 495 | vec_foreach (bi, s->buffer_indices) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 496 | { |
Damjan Marion | 409ef61 | 2016-11-11 21:35:18 +0100 | [diff] [blame] | 497 | vlib_buffer_delete_free_list (vm, bi->free_list_index); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 498 | clib_fifo_free (bi->buffer_fifo); |
| 499 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | |
| 501 | pg_stream_free (s); |
| 502 | pool_put (pg->streams, s); |
| 503 | } |
| 504 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * fd.io coding-style-patch-verification: ON |
| 508 | * |
| 509 | * Local Variables: |
| 510 | * eval: (c-set-style "gnu") |
| 511 | * End: |
| 512 | */ |