FD.io VPP  v21.06
Vector Packet Processing
format.c
Go to the documentation of this file.
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 
42 u8 *
43 format_vlib_rx_tx (u8 * s, va_list * args)
44 {
45  vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
46  char *t;
47 
48  switch (r)
49  {
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;
59  }
60 
61  vec_add (s, t, strlen (t));
62  return s;
63 }
64 
65 u8 *
66 format_vlib_read_write (u8 * s, va_list * args)
67 {
68  vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
69  char *t;
70 
71  switch (r)
72  {
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;
82  }
83 
84  vec_add (s, t, strlen (t));
85  return s;
86 }
87 
88 /* Formats buffer data as printable ascii or as hex. */
89 u8 *
90 format_vlib_buffer_data (u8 * s, va_list * args)
91 {
92  u8 *data = va_arg (*args, u8 *);
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  }
105 
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. */
115 uword
117 {
118  int *result = va_arg (*args, int *);
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. */
133 uword
134 unformat_vlib_rx_tx (unformat_input_t * input, va_list * args)
135 {
136  int *result = va_arg (*args, int *);
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. */
147 uword
148 unformat_vlib_number (unformat_input_t * input, va_list * args)
149 {
150  int *result = va_arg (*args, int *);
151 
152  return (unformat (input, "0x%x", result) || unformat (input, "%d", result));
153 }
154 
155 /* Parse a-zA-Z0-9_ token and hash to value. */
156 uword
158 {
159  uword *hash = va_arg (*args, uword *);
160  int *result = va_arg (*args, int *);
161  uword *p;
162  u8 *token;
163  int i;
164 
165  if (!unformat_user (input, unformat_token, "a-zA-Z0-9_", &token))
166  return 0;
167 
168  /* Null terminate. */
169  if (vec_len (token) > 0 && token[vec_len (token) - 1] != 0)
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 
183 done:
184  vec_free (token);
185  if (p)
186  *result = p[0];
187  return p != 0;
188 }
189 
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 
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 *
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 }
234 
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */
unformat_function_t unformat_token
Definition: format.h:278
vnet_hw_if_output_node_runtime_t * r
u32 thread_index
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:607
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
string name[64]
Definition: fib.api:25
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:92
unsigned int u32
Definition: types.h:88
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:148
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:689
vlib_rx_or_tx_t
Definition: defs.h:44
uword unformat_vlib_tmpfile(unformat_input_t *input, va_list *args)
Definition: format.c:192
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
u8 * format_vlib_read_write(u8 *s, va_list *args)
Definition: format.c:66
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
__clib_export u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:428
struct _unformat_input_t unformat_input_t
u8 * format_vlib_thread_name_and_index(u8 *s, va_list *args)
Definition: format.c:227
svmdb_client_t * c
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
u32 index
Definition: flow_types.api:221
uword unformat_vlib_rx_tx(unformat_input_t *input, va_list *args)
Definition: format.c:134
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
Definition: defs.h:64
Definition: defs.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uword unformat_vlib_enable_disable(unformat_input_t *input, va_list *args)
Definition: format.c:116
u64 uword
Definition: types.h:112
#define hash_get_mem(h, key)
Definition: hash.h:269
u8 * format_vlib_buffer_data(u8 *s, va_list *args)
Definition: format.c:90
u8 * format_vlib_rx_tx(u8 *s, va_list *args)
Definition: format.c:43
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
Definition: defs.h:46
u8 * format_vlib_thread_name(u8 *s, va_list *args)
Definition: format.c:214