blob: 98010620a5dab81421de85b585beac6aed898cce [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barach9b8ffd92016-07-08 08:13:45 -040042u8 *
43format_vlib_rx_tx (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070044{
45 vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -040046 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
48 switch (r)
49 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040050 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 Warnickecb9cada2015-12-08 15:45:58 -070059 }
60
61 vec_add (s, t, strlen (t));
62 return s;
63}
64
Dave Barach9b8ffd92016-07-08 08:13:45 -040065u8 *
66format_vlib_read_write (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070067{
68 vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -040069 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71 switch (r)
72 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040073 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 Warnickecb9cada2015-12-08 15:45:58 -070082 }
83
84 vec_add (s, t, strlen (t));
85 return s;
86}
87
88/* Formats buffer data as printable ascii or as hex. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040089u8 *
90format_vlib_buffer_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Dave Barach9b8ffd92016-07-08 08:13:45 -040092 u8 *data = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 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 Barach9b8ffd92016-07-08 08:13:45 -0400105
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 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 Barach9b8ffd92016-07-08 08:13:45 -0400115uword
116unformat_vlib_enable_disable (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118 int *result = va_arg (*args, int *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 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 Barach9b8ffd92016-07-08 08:13:45 -0400133uword
134unformat_vlib_rx_tx (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136 int *result = va_arg (*args, int *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 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 Barach9b8ffd92016-07-08 08:13:45 -0400147uword
148unformat_vlib_number (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150 int *result = va_arg (*args, int *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 return (unformat (input, "0x%x", result) || unformat (input, "%d", result));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153}
154
155/* Parse a-zA-Z0-9_ token and hash to value. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156uword
157unformat_vlib_number_by_name (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400159 uword *hash = va_arg (*args, uword *);
160 int *result = va_arg (*args, int *);
161 uword *p;
162 u8 *token;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 int i;
164
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 if (!unformat_user (input, unformat_token, "a-zA-Z0-9_", &token))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 return 0;
167
168 /* Null terminate. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169 if (vec_len (token) > 0 && token[vec_len (token) - 1] != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 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 Barach9b8ffd92016-07-08 08:13:45 -0400183done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 vec_free (token);
185 if (p)
186 *result = p[0];
187 return p != 0;
188}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400189
Benoît Ganne362456a2019-01-21 17:41:25 +0100190/* Parse a filename to dump debug info */
191uword
192unformat_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 */
Matus Fabian7f163b62024-07-22 09:56:01 +0200201 if (strstr ((char *) filename, "..") || strchr ((char *) filename, '/'))
Benoît Ganne362456a2019-01-21 17:41:25 +0100202 {
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 Marion2cdb3012020-11-23 16:50:24 +0100213u8 *
214format_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
226u8 *
227format_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 Ganne362456a2019-01-21 17:41:25 +0100234
Dave Barach9b8ffd92016-07-08 08:13:45 -0400235/*
236 * fd.io coding-style-patch-verification: ON
237 *
238 * Local Variables:
239 * eval: (c-set-style "gnu")
240 * End:
241 */