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 | |
BenoƮt Ganne | 03f2a01 | 2021-07-20 16:49:13 +0200 | [diff] [blame] | 105 | s = format (s, "%Ubuffer 0x%x: %U", format_white_space, indent, |
| 106 | t->buffer_index, format_vnet_buffer_no_chain, &t->buffer); |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 107 | |
| 108 | s = format (s, "\n%U%U", format_white_space, indent, |
| 109 | format_ethernet_header_with_length, t->buffer.pre_data, |
| 110 | sizeof (t->buffer.pre_data)); |
| 111 | |
| 112 | return s; |
| 113 | } |
| 114 | |
| 115 | static u8 * |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 116 | format_pg_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 118 | pg_main_t *pg = &pg_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | u32 if_index = va_arg (*args, u32); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 120 | pg_interface_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 122 | pi = pool_elt_at_index (pg->interfaces, if_index); |
| 123 | s = format (s, "pg%d", pi->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
| 125 | return s; |
| 126 | } |
| 127 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 128 | static clib_error_t * |
| 129 | pg_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 130 | { |
| 131 | u32 hw_flags = 0; |
| 132 | |
| 133 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 134 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
| 135 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 136 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Neale Ranns | 21fb4f7 | 2020-10-05 12:26:47 +0000 | [diff] [blame] | 141 | static int |
| 142 | pg_mac_address_cmp (const mac_address_t * m1, const mac_address_t * m2) |
| 143 | { |
| 144 | return (!mac_address_cmp (m1, m2)); |
| 145 | } |
| 146 | |
| 147 | static clib_error_t * |
| 148 | pg_add_del_mac_address (vnet_hw_interface_t * hi, |
| 149 | const u8 * address, u8 is_add) |
| 150 | { |
| 151 | pg_main_t *pg = &pg_main; |
| 152 | |
| 153 | if (ethernet_address_cast (address)) |
| 154 | { |
| 155 | mac_address_t mac; |
| 156 | pg_interface_t *pi; |
| 157 | |
| 158 | pi = pool_elt_at_index (pg->interfaces, hi->dev_instance); |
| 159 | |
| 160 | mac_address_from_bytes (&mac, address); |
| 161 | if (is_add) |
| 162 | vec_add1 (pi->allowed_mcast_macs, mac); |
| 163 | else |
| 164 | { |
| 165 | u32 pos = vec_search_with_function (pi->allowed_mcast_macs, &mac, |
| 166 | pg_mac_address_cmp); |
| 167 | if (~0 != pos) |
| 168 | vec_del1 (pi->allowed_mcast_macs, pos); |
| 169 | } |
| 170 | } |
| 171 | return (NULL); |
| 172 | } |
| 173 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 174 | VNET_DEVICE_CLASS (pg_dev_class) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | .name = "pg", |
| 176 | .tx_function = pg_output, |
| 177 | .format_device_name = format_pg_interface_name, |
Damjan Marion | bfe4dfa | 2017-02-03 21:16:16 +0100 | [diff] [blame] | 178 | .format_tx_trace = format_pg_output_trace, |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 179 | .admin_up_down_function = pg_interface_admin_up_down, |
Neale Ranns | 21fb4f7 | 2020-10-05 12:26:47 +0000 | [diff] [blame] | 180 | .mac_addr_add_del_function = pg_add_del_mac_address, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 183 | static u8 * |
| 184 | pg_build_rewrite (vnet_main_t * vnm, |
| 185 | u32 sw_if_index, |
| 186 | vnet_link_t link_type, const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 188 | u8 *rewrite = NULL; |
| 189 | u16 *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | vec_validate (rewrite, sizeof (*h) - 1); |
| 192 | h = (u16 *) rewrite; |
| 193 | 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] | 194 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 195 | return (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | VNET_HW_INTERFACE_CLASS (pg_interface_class,static) = { |
| 199 | .name = "Packet generator", |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 200 | .build_rewrite = pg_build_rewrite, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 203 | static u32 |
| 204 | pg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) |
| 205 | { |
| 206 | /* nothing for now */ |
| 207 | return 0; |
| 208 | } |
| 209 | |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 210 | void |
| 211 | pg_interface_enable_disable_coalesce (pg_interface_t * pi, u8 enable, |
| 212 | u32 tx_node_index) |
| 213 | { |
| 214 | if (enable) |
| 215 | { |
| 216 | gro_flow_table_init (&pi->flow_table, 1 /* is_l2 */ , |
| 217 | tx_node_index); |
| 218 | pi->coalesce_enabled = 1; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | pi->coalesce_enabled = 0; |
| 223 | gro_flow_table_free (pi->flow_table); |
| 224 | } |
| 225 | } |
| 226 | |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 227 | u8 * |
| 228 | format_pg_tun_tx_trace (u8 *s, va_list *args) |
| 229 | { |
| 230 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 231 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 232 | |
| 233 | s = format (s, "PG: tunnel (no-encap)"); |
| 234 | return s; |
| 235 | } |
| 236 | |
| 237 | VNET_HW_INTERFACE_CLASS (pg_tun_hw_interface_class) = { |
| 238 | .name = "PG-tun", |
| 239 | //.format_header = format_gre_header_with_length, |
| 240 | //.unformat_header = unformat_gre_header, |
| 241 | .build_rewrite = NULL, |
| 242 | //.update_adjacency = gre_update_adj, |
| 243 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Mohsin Kazmi | 0d05c0d | 2021-11-09 17:44:10 +0000 | [diff] [blame] | 244 | .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP, |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 247 | u32 |
Maxime Peim | 7217dcd | 2024-02-19 11:26:23 +0100 | [diff] [blame] | 248 | pg_interface_add_or_get (pg_main_t *pg, u32 if_id, u8 gso_enabled, |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 249 | u32 gso_size, u8 coalesce_enabled, |
| 250 | pg_interface_mode_t mode) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 252 | vnet_main_t *vnm = vnet_get_main (); |
| 253 | vlib_main_t *vm = vlib_get_main (); |
| 254 | pg_interface_t *pi; |
| 255 | vnet_hw_interface_t *hi; |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 256 | uword *p; |
| 257 | u32 i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 259 | p = hash_get (pg->if_index_by_if_id, if_id); |
| 260 | |
| 261 | if (p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | { |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 263 | return p[0]; |
| 264 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | else |
| 266 | { |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 267 | vnet_eth_interface_registration_t eir = {}; |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 268 | u8 hw_addr[6]; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 269 | f64 now = vlib_time_now (vm); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 270 | u32 rnd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 272 | pool_get (pg->interfaces, pi); |
| 273 | i = pi - pg->interfaces; |
| 274 | |
| 275 | rnd = (u32) (now * 1e6); |
| 276 | rnd = random_u32 (&rnd); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 277 | clib_memcpy_fast (hw_addr + 2, &rnd, sizeof (rnd)); |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 278 | hw_addr[0] = 2; |
| 279 | hw_addr[1] = 0xfe; |
| 280 | |
| 281 | pi->id = if_id; |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 282 | pi->mode = mode; |
| 283 | |
| 284 | switch (pi->mode) |
| 285 | { |
| 286 | case PG_MODE_ETHERNET: |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 287 | eir.dev_class_index = pg_dev_class.index; |
| 288 | eir.dev_instance = i; |
| 289 | eir.address = hw_addr; |
| 290 | eir.cb.flag_change = pg_eth_flag_change; |
| 291 | pi->hw_if_index = vnet_eth_register_interface (vnm, &eir); |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 292 | break; |
| 293 | case PG_MODE_IP4: |
| 294 | case PG_MODE_IP6: |
| 295 | pi->hw_if_index = vnet_register_interface ( |
| 296 | vnm, pg_dev_class.index, i, pg_tun_hw_interface_class.index, i); |
| 297 | break; |
| 298 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | hi = vnet_get_hw_interface (vnm, pi->hw_if_index); |
Mohsin Kazmi | 22e9cfd | 2019-07-23 11:54:48 +0200 | [diff] [blame] | 300 | if (gso_enabled) |
| 301 | { |
Damjan Marion | d4f88cc | 2022-01-05 01:52:38 +0100 | [diff] [blame] | 302 | vnet_hw_if_set_caps (vnm, pi->hw_if_index, VNET_HW_IF_CAP_TCP_GSO); |
Mohsin Kazmi | 22e9cfd | 2019-07-23 11:54:48 +0200 | [diff] [blame] | 303 | pi->gso_enabled = 1; |
| 304 | pi->gso_size = gso_size; |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 305 | if (coalesce_enabled) |
| 306 | { |
| 307 | pg_interface_enable_disable_coalesce (pi, 1, hi->tx_node_index); |
| 308 | } |
Mohsin Kazmi | 22e9cfd | 2019-07-23 11:54:48 +0200 | [diff] [blame] | 309 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | pi->sw_if_index = hi->sw_if_index; |
Damjan Marion | 3d9c86e | 2016-07-04 21:04:40 +0200 | [diff] [blame] | 311 | |
| 312 | hash_set (pg->if_index_by_if_id, if_id, i); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 313 | |
Maxime Peim | 7217dcd | 2024-02-19 11:26:23 +0100 | [diff] [blame] | 314 | vec_validate (pg->if_index_by_sw_if_index, hi->sw_if_index); |
| 315 | pg->if_index_by_sw_if_index[hi->sw_if_index] = i; |
Neale Ranns | 21fb4f7 | 2020-10-05 12:26:47 +0000 | [diff] [blame] | 316 | |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 317 | if (vlib_num_workers ()) |
| 318 | { |
| 319 | pi->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, |
| 320 | CLIB_CACHE_LINE_BYTES); |
| 321 | *pi->lockp = 0; |
| 322 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return i; |
| 326 | } |
| 327 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 328 | static void |
| 329 | do_edit (pg_stream_t * stream, |
| 330 | pg_edit_group_t * g, pg_edit_t * e, uword want_commit) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | { |
| 332 | u32 i, i0, i1, mask, n_bits_left; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 333 | u8 *v, *s, *m; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | |
| 335 | i0 = e->lsb_bit_offset / BITS (u8); |
| 336 | |
| 337 | /* Make space for edit in value and mask. */ |
| 338 | vec_validate (g->fixed_packet_data, i0); |
| 339 | vec_validate (g->fixed_packet_data_mask, i0); |
| 340 | |
| 341 | if (e->type != PG_EDIT_FIXED) |
| 342 | { |
| 343 | switch (e->type) |
| 344 | { |
| 345 | case PG_EDIT_RANDOM: |
| 346 | case PG_EDIT_INCREMENT: |
| 347 | e->last_increment_value = pg_edit_get_value (e, PG_EDIT_LO); |
| 348 | break; |
| 349 | |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 354 | if (want_commit) |
| 355 | { |
| 356 | ASSERT (e->type != PG_EDIT_INVALID_TYPE); |
| 357 | vec_add1 (g->non_fixed_edits, e[0]); |
| 358 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | |
| 362 | s = g->fixed_packet_data; |
| 363 | m = g->fixed_packet_data_mask; |
| 364 | |
| 365 | n_bits_left = e->n_bits; |
| 366 | i0 = e->lsb_bit_offset / BITS (u8); |
| 367 | i1 = e->lsb_bit_offset % BITS (u8); |
| 368 | |
| 369 | v = e->values[PG_EDIT_LO]; |
| 370 | i = pg_edit_n_alloc_bytes (e) - 1; |
| 371 | |
| 372 | /* Odd low order bits?. */ |
| 373 | if (i1 != 0 && n_bits_left > 0) |
| 374 | { |
| 375 | u32 n = clib_min (n_bits_left, BITS (u8) - i1); |
| 376 | |
| 377 | mask = pow2_mask (n) << i1; |
| 378 | |
| 379 | ASSERT (i0 < vec_len (s)); |
| 380 | ASSERT (i < vec_len (v)); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 381 | ASSERT ((v[i] & ~mask) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | |
| 383 | s[i0] |= v[i] & mask; |
| 384 | m[i0] |= mask; |
| 385 | |
| 386 | i0--; |
| 387 | i--; |
| 388 | n_bits_left -= n; |
| 389 | } |
| 390 | |
| 391 | /* Even bytes. */ |
| 392 | while (n_bits_left >= 8) |
| 393 | { |
| 394 | ASSERT (i0 < vec_len (s)); |
| 395 | ASSERT (i < vec_len (v)); |
| 396 | |
| 397 | s[i0] = v[i]; |
| 398 | m[i0] = ~0; |
| 399 | |
| 400 | i0--; |
| 401 | i--; |
| 402 | n_bits_left -= 8; |
| 403 | } |
| 404 | |
| 405 | /* Odd high order bits. */ |
| 406 | if (n_bits_left > 0) |
| 407 | { |
| 408 | mask = pow2_mask (n_bits_left); |
| 409 | |
| 410 | ASSERT (i0 < vec_len (s)); |
| 411 | ASSERT (i < vec_len (v)); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 412 | ASSERT ((v[i] & ~mask) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | |
| 414 | s[i0] |= v[i] & mask; |
| 415 | m[i0] |= mask; |
| 416 | } |
| 417 | |
| 418 | if (want_commit) |
| 419 | pg_edit_free (e); |
| 420 | } |
| 421 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 422 | void |
| 423 | pg_edit_group_get_fixed_packet_data (pg_stream_t * s, |
| 424 | u32 group_index, |
| 425 | void *packet_data, |
| 426 | void *packet_data_mask) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 428 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
| 429 | pg_edit_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 431 | 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] | 432 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 433 | clib_memcpy_fast (packet_data, g->fixed_packet_data, |
| 434 | vec_len (g->fixed_packet_data)); |
| 435 | clib_memcpy_fast (packet_data_mask, g->fixed_packet_data_mask, |
| 436 | vec_len (g->fixed_packet_data_mask)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 439 | static void |
| 440 | perform_fixed_edits (pg_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 442 | pg_edit_group_t *g; |
| 443 | pg_edit_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | word i; |
| 445 | |
| 446 | for (i = vec_len (s->edit_groups) - 1; i >= 0; i--) |
| 447 | { |
| 448 | g = vec_elt_at_index (s->edit_groups, i); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 449 | 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] | 450 | |
| 451 | /* All edits have either been performed or added to |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 452 | g->non_fixed_edits. So, we can delete the vector. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | vec_free (g->edits); |
| 454 | } |
| 455 | |
| 456 | vec_free (s->fixed_packet_data_mask); |
| 457 | vec_free (s->fixed_packet_data); |
| 458 | vec_foreach (g, s->edit_groups) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 459 | { |
| 460 | int i; |
| 461 | g->start_byte_offset = vec_len (s->fixed_packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 463 | /* Relocate and copy non-fixed edits from group to stream. */ |
| 464 | vec_foreach (e, g->non_fixed_edits) |
| 465 | e->lsb_bit_offset += g->start_byte_offset * BITS (u8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 467 | for (i = 0; i < vec_len (g->non_fixed_edits); i++) |
| 468 | ASSERT (g->non_fixed_edits[i].type != PG_EDIT_INVALID_TYPE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 470 | vec_add (s->non_fixed_edits, |
| 471 | g->non_fixed_edits, vec_len (g->non_fixed_edits)); |
| 472 | vec_free (g->non_fixed_edits); |
| 473 | |
| 474 | vec_add (s->fixed_packet_data, |
| 475 | g->fixed_packet_data, vec_len (g->fixed_packet_data)); |
| 476 | vec_add (s->fixed_packet_data_mask, |
| 477 | g->fixed_packet_data_mask, vec_len (g->fixed_packet_data_mask)); |
| 478 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 481 | void |
| 482 | pg_stream_add (pg_main_t * pg, pg_stream_t * s_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | { |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 484 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 485 | pg_stream_t *s; |
| 486 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 488 | if (!pg->stream_index_by_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | pg->stream_index_by_name |
| 490 | = hash_create_vec (0, sizeof (s->name[0]), sizeof (uword)); |
| 491 | |
| 492 | /* Delete any old stream with the same name. */ |
| 493 | if (s_init->name |
| 494 | && (p = hash_get_mem (pg->stream_index_by_name, s_init->name))) |
| 495 | { |
| 496 | pg_stream_del (pg, p[0]); |
| 497 | } |
| 498 | |
| 499 | pool_get (pg->streams, s); |
| 500 | s[0] = s_init[0]; |
| 501 | |
| 502 | /* Give it a name. */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 503 | if (!s->name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | s->name = format (0, "stream%d", s - pg->streams); |
| 505 | else |
| 506 | s->name = vec_dup (s->name); |
| 507 | |
| 508 | hash_set_mem (pg->stream_index_by_name, s->name, s - pg->streams); |
| 509 | |
| 510 | /* Get fixed part of buffer data. */ |
Damjan Marion | ddbdd4f | 2016-07-08 22:08:16 +0200 | [diff] [blame] | 511 | if (s->edit_groups) |
| 512 | perform_fixed_edits (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | |
| 514 | /* Determine packet size. */ |
| 515 | switch (s->packet_size_edit_type) |
| 516 | { |
| 517 | case PG_EDIT_INCREMENT: |
| 518 | case PG_EDIT_RANDOM: |
| 519 | if (s->min_packet_bytes == s->max_packet_bytes) |
| 520 | s->packet_size_edit_type = PG_EDIT_FIXED; |
| 521 | break; |
| 522 | |
| 523 | default: |
| 524 | /* Get packet size from fixed edits. */ |
| 525 | s->packet_size_edit_type = PG_EDIT_FIXED; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 526 | if (!s->replay_packet_templates) |
| 527 | s->min_packet_bytes = s->max_packet_bytes = |
| 528 | vec_len (s->fixed_packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | |
| 532 | s->last_increment_packet_size = s->min_packet_bytes; |
| 533 | |
| 534 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | int n; |
| 536 | |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 537 | s->buffer_bytes = vlib_buffer_get_default_data_size (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | n = s->max_packet_bytes / s->buffer_bytes; |
| 539 | n += (s->max_packet_bytes % s->buffer_bytes) != 0; |
| 540 | |
| 541 | vec_resize (s->buffer_indices, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Find an interface to use. */ |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 545 | s->pg_if_index = pg_interface_add_or_get ( |
| 546 | pg, s->if_id, 0 /* gso_enabled */, 0 /* gso_size */, |
| 547 | 0 /* coalesce_enabled */, PG_MODE_ETHERNET); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Dave Barach | 7d31ab2 | 2019-05-08 19:18:18 -0400 | [diff] [blame] | 549 | if (s->sw_if_index[VLIB_RX] == ~0) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 550 | { |
Dave Barach | 7d31ab2 | 2019-05-08 19:18:18 -0400 | [diff] [blame] | 551 | pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index); |
| 552 | /* |
| 553 | * Default the RX interface if unset. It's a bad mistake to |
| 554 | * set [VLIB_TX] prior to ip lookup, since the ip lookup code |
| 555 | * interprets [VLIB_TX] as a fib index... |
| 556 | */ |
| 557 | s->sw_if_index[VLIB_RX] = pi->sw_if_index; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 558 | } |
Maxime Peim | 7217dcd | 2024-02-19 11:26:23 +0100 | [diff] [blame] | 559 | else if (vec_len (pg->if_index_by_sw_if_index) <= s->sw_if_index[VLIB_RX]) |
| 560 | { |
| 561 | vec_validate (pg->if_index_by_sw_if_index, s->sw_if_index[VLIB_RX]); |
| 562 | pg->if_index_by_sw_if_index[s->sw_if_index[VLIB_RX]] = s->pg_if_index; |
| 563 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | |
| 565 | /* Connect the graph. */ |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 566 | s->next_index = vlib_node_add_next (vm, device_input_node.index, |
| 567 | s->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 570 | void |
| 571 | pg_stream_del (pg_main_t * pg, uword index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 573 | pg_stream_t *s; |
| 574 | pg_buffer_index_t *bi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | |
| 576 | s = pool_elt_at_index (pg->streams, index); |
| 577 | |
| 578 | pg_stream_enable_disable (pg, s, /* want_enabled */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | hash_unset_mem (pg->stream_index_by_name, s->name); |
| 580 | |
| 581 | vec_foreach (bi, s->buffer_indices) |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 582 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 583 | clib_fifo_free (bi->buffer_fifo); |
| 584 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 585 | |
| 586 | pg_stream_free (s); |
| 587 | pool_put (pg->streams, s); |
| 588 | } |
| 589 | |
Kingwel Xie | 4222347 | 2019-01-19 22:49:26 -0500 | [diff] [blame] | 590 | void |
| 591 | pg_stream_change (pg_main_t * pg, pg_stream_t * s) |
| 592 | { |
| 593 | /* Determine packet size. */ |
| 594 | switch (s->packet_size_edit_type) |
| 595 | { |
| 596 | case PG_EDIT_INCREMENT: |
| 597 | case PG_EDIT_RANDOM: |
| 598 | if (s->min_packet_bytes == s->max_packet_bytes) |
| 599 | s->packet_size_edit_type = PG_EDIT_FIXED; |
| 600 | case PG_EDIT_FIXED: |
| 601 | break; |
| 602 | |
| 603 | default: |
| 604 | /* Get packet size from fixed edits. */ |
| 605 | s->packet_size_edit_type = PG_EDIT_FIXED; |
| 606 | if (!s->replay_packet_templates) |
| 607 | s->min_packet_bytes = s->max_packet_bytes = |
| 608 | vec_len (s->fixed_packet_data); |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | s->last_increment_packet_size = s->min_packet_bytes; |
| 613 | } |
| 614 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * fd.io coding-style-patch-verification: ON |
| 618 | * |
| 619 | * Local Variables: |
| 620 | * eval: (c-set-style "gnu") |
| 621 | * End: |
| 622 | */ |