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 | * format.c: generic network formatting/unformating |
| 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 42 | u8 * |
| 43 | format_vlib_rx_tx (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | { |
| 45 | vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 46 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
| 48 | switch (r) |
| 49 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 50 | case VLIB_RX: |
| 51 | t = "rx"; |
| 52 | break; |
| 53 | case VLIB_TX: |
| 54 | t = "tx"; |
| 55 | break; |
| 56 | default: |
| 57 | t = "INVALID"; |
| 58 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | vec_add (s, t, strlen (t)); |
| 62 | return s; |
| 63 | } |
| 64 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | u8 * |
| 66 | format_vlib_read_write (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | { |
| 68 | vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 69 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | switch (r) |
| 72 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 73 | case VLIB_READ: |
| 74 | t = "read"; |
| 75 | break; |
| 76 | case VLIB_WRITE: |
| 77 | t = "write"; |
| 78 | break; |
| 79 | default: |
| 80 | t = "INVALID"; |
| 81 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | vec_add (s, t, strlen (t)); |
| 85 | return s; |
| 86 | } |
| 87 | |
| 88 | /* Formats buffer data as printable ascii or as hex. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | u8 * |
| 90 | format_vlib_buffer_data (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | u8 *data = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | u32 n_data_bytes = va_arg (*args, u32); |
| 94 | u32 i, is_printable; |
| 95 | |
| 96 | is_printable = 1; |
| 97 | for (i = 0; i < n_data_bytes && is_printable; i++) |
| 98 | { |
| 99 | u8 c = data[i]; |
| 100 | if (c < 0x20) |
| 101 | is_printable = 0; |
| 102 | else if (c >= 0x7f) |
| 103 | is_printable = 0; |
| 104 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 105 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | if (is_printable) |
| 107 | vec_add (s, data, n_data_bytes); |
| 108 | else |
| 109 | s = format (s, "%U", format_hex_bytes, data, n_data_bytes); |
| 110 | |
| 111 | return s; |
| 112 | } |
| 113 | |
| 114 | /* Enable/on => 1; disable/off => 0. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 115 | uword |
| 116 | unformat_vlib_enable_disable (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | int *result = va_arg (*args, int *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | int enable; |
| 120 | |
| 121 | if (unformat (input, "enable") || unformat (input, "on")) |
| 122 | enable = 1; |
| 123 | else if (unformat (input, "disable") || unformat (input, "off")) |
| 124 | enable = 0; |
| 125 | else |
| 126 | return 0; |
| 127 | |
| 128 | *result = enable; |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /* rx/tx => VLIB_RX/VLIB_TX. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 133 | uword |
| 134 | unformat_vlib_rx_tx (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 136 | int *result = va_arg (*args, int *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | if (unformat (input, "rx")) |
| 138 | *result = VLIB_RX; |
| 139 | else if (unformat (input, "tx")) |
| 140 | *result = VLIB_TX; |
| 141 | else |
| 142 | return 0; |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | /* Parse an int either %d or 0x%x. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 147 | uword |
| 148 | unformat_vlib_number (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 150 | int *result = va_arg (*args, int *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 152 | return (unformat (input, "0x%x", result) || unformat (input, "%d", result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* Parse a-zA-Z0-9_ token and hash to value. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 156 | uword |
| 157 | unformat_vlib_number_by_name (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 159 | uword *hash = va_arg (*args, uword *); |
| 160 | int *result = va_arg (*args, int *); |
| 161 | uword *p; |
| 162 | u8 *token; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | int i; |
| 164 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 165 | if (!unformat_user (input, unformat_token, "a-zA-Z0-9_", &token)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | |
| 168 | /* Null terminate. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 169 | if (vec_len (token) > 0 && token[vec_len (token) - 1] != 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | vec_add1 (token, 0); |
| 171 | |
| 172 | /* Check for exact match. */ |
| 173 | p = hash_get_mem (hash, token); |
| 174 | if (p) |
| 175 | goto done; |
| 176 | |
| 177 | /* Convert to upper case & try match. */ |
| 178 | for (i = 0; i < vec_len (token); i++) |
| 179 | if (token[i] >= 'a' && token[i] <= 'z') |
| 180 | token[i] = 'A' + token[i] - 'a'; |
| 181 | p = hash_get_mem (hash, token); |
| 182 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 183 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | vec_free (token); |
| 185 | if (p) |
| 186 | *result = p[0]; |
| 187 | return p != 0; |
| 188 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | |
Benoît Ganne | 362456a | 2019-01-21 17:41:25 +0100 | [diff] [blame] | 190 | /* Parse a filename to dump debug info */ |
| 191 | uword |
| 192 | unformat_vlib_tmpfile (unformat_input_t * input, va_list * args) |
| 193 | { |
| 194 | u8 **chroot_filename = va_arg (*args, u8 **); |
| 195 | u8 *filename; |
| 196 | |
| 197 | if (!unformat (input, "%s", &filename)) |
| 198 | return 0; |
| 199 | |
| 200 | /* Brain-police user path input */ |
| 201 | if (strstr ((char *) filename, "..") || index ((char *) filename, '/')) |
| 202 | { |
| 203 | vec_free (filename); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | *chroot_filename = format (0, "/tmp/%s%c", filename, 0); |
| 208 | vec_free (filename); |
| 209 | |
| 210 | return 1; |
| 211 | } |
| 212 | |
Damjan Marion | 2cdb301 | 2020-11-23 16:50:24 +0100 | [diff] [blame] | 213 | u8 * |
| 214 | format_vlib_thread_name (u8 * s, va_list * args) |
| 215 | { |
| 216 | u32 thread_index = va_arg (*args, u32); |
| 217 | |
| 218 | if (thread_index == 0) |
| 219 | return format (s, "main"); |
| 220 | |
| 221 | if (thread_index < vec_len (vlib_worker_threads)) |
| 222 | return format (s, "%s", vlib_worker_threads[thread_index].name); |
| 223 | return s; |
| 224 | } |
| 225 | |
| 226 | u8 * |
| 227 | format_vlib_thread_name_and_index (u8 * s, va_list * args) |
| 228 | { |
| 229 | u32 thread_index = va_arg (*args, u32); |
| 230 | |
| 231 | return format (s, "%U (%u)", format_vlib_thread_name, thread_index, |
| 232 | thread_index); |
| 233 | } |
Benoît Ganne | 362456a | 2019-01-21 17:41:25 +0100 | [diff] [blame] | 234 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 235 | /* |
| 236 | * fd.io coding-style-patch-verification: ON |
| 237 | * |
| 238 | * Local Variables: |
| 239 | * eval: (c-set-style "gnu") |
| 240 | * End: |
| 241 | */ |