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_edit.c: packet generator edits |
| 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 <vlib/vlib.h> |
| 41 | #include <vnet/pg/pg.h> |
| 42 | |
| 43 | static void |
| 44 | pg_edit_set_value_helper (pg_edit_t * e, u64 value, u8 * result) |
| 45 | { |
| 46 | int i, j, n_bits_left; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 47 | u8 *v, tmp[8]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
| 49 | v = tmp; |
| 50 | |
| 51 | n_bits_left = e->n_bits; |
| 52 | i = 0; |
| 53 | j = e->lsb_bit_offset % BITS (v[0]); |
| 54 | |
| 55 | if (n_bits_left > 0 && j != 0) |
| 56 | { |
| 57 | v[i] = (value & 0xff) << j; |
| 58 | value >>= BITS (v[0]) - j; |
| 59 | n_bits_left -= BITS (v[0]) - j; |
| 60 | i += 1; |
| 61 | } |
| 62 | |
| 63 | while (n_bits_left > 0) |
| 64 | { |
| 65 | v[i] = value & 0xff; |
| 66 | value >>= 8; |
| 67 | n_bits_left -= 8; |
| 68 | i += 1; |
| 69 | } |
| 70 | |
| 71 | /* Convert to network byte order. */ |
| 72 | for (j = 0; j < i; j++) |
| 73 | result[j] = v[i - 1 - j]; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | pg_edit_set_value (pg_edit_t * e, int hi_or_lo, u64 value) |
| 78 | { |
| 79 | pg_edit_alloc_value (e, hi_or_lo); |
| 80 | pg_edit_set_value_helper (e, value, e->values[hi_or_lo]); |
| 81 | } |
| 82 | |
| 83 | /* Parse an int either %d or 0x%x into network byte order. */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 84 | uword |
| 85 | unformat_pg_number (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 87 | u8 *result = va_arg (*args, u8 *); |
| 88 | pg_edit_t *e = va_arg (*args, pg_edit_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | u64 value; |
| 90 | |
| 91 | ASSERT (BITS (value) >= e->n_bits); |
| 92 | |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 93 | if (!unformat (input, "0x%X", sizeof (value), &value) |
| 94 | && !unformat (input, "%D", sizeof (value), &value)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | |
| 97 | /* Number given does not fit into bit field. */ |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 98 | if (e->n_bits < 64 && value >= (u64) 1 << (u64) e->n_bits) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | |
| 101 | pg_edit_set_value_helper (e, value, result); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | uword |
| 106 | unformat_pg_edit (unformat_input_t * input, va_list * args) |
| 107 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 108 | unformat_function_t *f = va_arg (*args, unformat_function_t *); |
| 109 | pg_edit_t *e = va_arg (*args, pg_edit_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
| 111 | pg_edit_alloc_value (e, PG_EDIT_LO); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 112 | if (!unformat_user (input, f, e->values[PG_EDIT_LO], e)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | |
| 115 | pg_edit_alloc_value (e, PG_EDIT_HI); |
| 116 | if (unformat (input, "-%U", f, e->values[PG_EDIT_HI], e)) |
| 117 | e->type = PG_EDIT_INCREMENT; |
| 118 | else if (unformat (input, "+%U", f, e->values[PG_EDIT_HI], e)) |
| 119 | e->type = PG_EDIT_RANDOM; |
| 120 | else |
| 121 | e->type = PG_EDIT_FIXED; |
| 122 | |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | uword |
| 127 | unformat_pg_payload (unformat_input_t * input, va_list * args) |
| 128 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 129 | pg_stream_t *s = va_arg (*args, pg_stream_t *); |
Damjan Marion | 6403436 | 2016-11-07 22:19:55 +0100 | [diff] [blame] | 130 | vlib_main_t *vm = vlib_get_main (); |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 131 | pg_edit_t *e; |
Kingwel Xie | 49cd3ba | 2018-10-26 23:55:47 -0400 | [diff] [blame] | 132 | u32 i, node_index, len, max_len, ilen; |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 133 | u8 *v; |
| 134 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | v = 0; |
| 136 | |
Kingwel Xie | 49cd3ba | 2018-10-26 23:55:47 -0400 | [diff] [blame] | 137 | if (unformat (input, "incrementing %d", &ilen)) |
| 138 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | else if (unformat (input, "hex 0x%U", unformat_hex_string, &v)) |
| 140 | ; |
| 141 | |
| 142 | else if (unformat (input, "%U", unformat_vlib_node, vm, &node_index)) |
| 143 | { |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 144 | pg_node_t *pn = pg_get_node (node_index); |
| 145 | if (!pn->unformat_edit) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | return 0; |
| 147 | return unformat (input, "%U", pn->unformat_edit, s); |
| 148 | } |
| 149 | |
| 150 | else |
| 151 | return 0; |
| 152 | |
| 153 | /* Length not including this payload. */ |
| 154 | max_len = pg_edit_group_n_bytes (s, 0); |
Kingwel Xie | 49cd3ba | 2018-10-26 23:55:47 -0400 | [diff] [blame] | 155 | if (max_len >= s->max_packet_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | { |
Kingwel Xie | 49cd3ba | 2018-10-26 23:55:47 -0400 | [diff] [blame] | 157 | /* no payload */ |
| 158 | len = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
Kingwel Xie | 49cd3ba | 2018-10-26 23:55:47 -0400 | [diff] [blame] | 160 | else |
| 161 | { |
| 162 | /* make a bigger v to hold the data */ |
| 163 | len = s->max_packet_bytes - max_len; |
| 164 | } |
| 165 | |
| 166 | vec_resize (v, len); |
| 167 | for (i = 0; i < len; i++) |
| 168 | v[i] = i % ilen; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
| 170 | e = pg_create_edit_group (s, sizeof (e[0]), vec_len (v), 0); |
| 171 | |
| 172 | e->type = PG_EDIT_FIXED; |
| 173 | e->n_bits = vec_len (v) * BITS (v[0]); |
| 174 | |
| 175 | /* Least significant bit is at end of bitstream, since everything is always bigendian. */ |
| 176 | e->lsb_bit_offset = e->n_bits - BITS (v[0]); |
| 177 | |
| 178 | e->values[PG_EDIT_LO] = v; |
| 179 | |
| 180 | return 1; |
| 181 | } |
Calvin | 71e97c6 | 2016-08-19 16:23:14 -0400 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * fd.io coding-style-patch-verification: ON |
| 185 | * |
| 186 | * Local Variables: |
| 187 | * eval: (c-set-style "gnu") |
| 188 | * End: |
| 189 | */ |