FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
lex.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 #include <vlib/vlib.h>
16 #include <vlib/lex.h>
17 
19 
20 #define LEX_DEBUG 0
21 
22 u8 *
23 format_vlib_lex_token (u8 * s, va_list * args)
24 {
25  vlib_lex_main_t *lm = va_arg (*args, vlib_lex_main_t *);
26  vlib_lex_token_t *t = va_arg (*args, vlib_lex_token_t *);
27 
28  if (t->token == VLIB_LEX_word)
29  s = format (s, "%s", t->value.as_pointer);
30  else
31  s = format (s, "%s", lm->lex_token_names[t->token]);
32  return s;
33 }
34 
35 void
37 {
38  u8 c;
41  uword tv;
42 
43  if (PREDICT_FALSE (lm->pushback_sp >= 0))
44  {
45  rv[0] = lm->pushback_vector[lm->pushback_sp--];
46  return;
47  }
48 
49  rv->value.as_uword = ~0;
50 
51  while (1)
52  {
54  {
55  rv->token = VLIB_LEX_eof;
56  return;
57  }
58 
60  c = (lm->input_vector[lm->current_index++]) & 0x7f;
61  e = &t->entries[c];
63 
64  switch (e->action)
65  {
66  case VLIB_LEX_IGNORE:
67  continue;
68 
70  lm->current_token_value = 0;
71  /* fallthru */
72 
74  lm->current_number_base = e->token;
76  tv = c - '0';
77  if (tv >= lm->current_number_base)
78  {
79  tv = 10 + c - 'A';
80  if (tv >= lm->current_number_base)
81  tv = 10 + c - 'a';
82  }
83  lm->current_token_value += tv;
84  continue;
85 
87  vec_add1 (lm->token_buffer, c);
88  continue;
89 
91  {
92  uword *p;
93 
94  vec_add1 (lm->token_buffer, 0);
95 
96  /* It's either a keyword or just a word. */
97  p = hash_get_mem (lm->lex_keywords, lm->token_buffer);
98  if (p)
99  {
100  rv->token = p[0];
101  if (LEX_DEBUG > 0)
102  clib_warning ("keyword '%s' token %s",
103  lm->token_buffer,
104  lm->lex_token_names[rv->token]);
105  }
106  else
107  {
108  /* it's a WORD */
109  rv->token = VLIB_LEX_word;
110  rv->value.as_pointer = vec_dup (lm->token_buffer);
111  if (LEX_DEBUG > 0)
112  clib_warning ("%s, value '%s'",
113  lm->lex_token_names[VLIB_LEX_word],
114  rv->value.as_pointer);
115  }
116  _vec_len (lm->token_buffer) = 0;
117 
118  /* Rescan the character which terminated the keyword/word. */
119  lm->current_index--;
120  return;
121  }
122 
124  ASSERT (lm->current_index);
125  lm->current_index--;
126  /* note flow-through */
127 
128  case VLIB_LEX_RETURN:
129  rv->token = e->token;
130  rv->value.as_uword = lm->current_token_value;
131  lm->current_token_value = ~0;
132  if (LEX_DEBUG > 0)
133  {
135  ("table %s char '%c'(0x%02x) next table %s return %s",
136  t->name, c, c, lm->lex_tables[e->next_table_index].name,
137  lm->lex_token_names[e->token]);
138  if (rv->token == VLIB_LEX_number)
139  clib_warning (" numeric value 0x%x (%d)", rv->value,
140  rv->value);
141  }
142  return;
143  }
144  }
145 }
146 
147 u16
148 vlib_lex_add_token (vlib_lex_main_t * lm, char *token_name)
149 {
150  uword *p;
151  u16 rv;
152 
153  p = hash_get_mem (lm->lex_tokens_by_name, token_name);
154 
155  if (p)
156  return p[0];
157 
158  rv = vec_len (lm->lex_token_names);
159  hash_set_mem (lm->lex_tokens_by_name, token_name, rv);
160  vec_add1 (lm->lex_token_names, token_name);
161 
162  return rv;
163 }
164 
165 static u16
166 add_keyword (vlib_lex_main_t * lm, char *keyword, char *token_name)
167 {
168  uword *p;
169  u16 token;
170 
171  p = hash_get_mem (lm->lex_keywords, keyword);
172 
173  ASSERT (p == 0);
174 
175  token = vlib_lex_add_token (lm, token_name);
176 
177  hash_set_mem (lm->lex_keywords, keyword, token);
178  return token;
179 }
180 
181 u16
183  char *token_name)
184 {
185  uword *p = hash_get_mem (lm->lex_keywords, keyword);
186  return p ? p[0] : add_keyword (lm, keyword, token_name);
187 }
188 
189 void
191  u16 token, u32 next_table_index)
192 {
193  int i;
195  vlib_lex_table_t *t = pool_elt_at_index (lm->lex_tables, table_index);
196 
197  for (i = lo; i <= hi; i++)
198  {
199  ASSERT (i < ARRAY_LEN (t->entries));
200  t->entries[i].action = action;
201  t->entries[i].token = token;
202  t->entries[i].next_table_index = next_table_index;
203  }
204 }
205 
206 u16
208 {
210  vlib_lex_table_t *t;
211  uword *p;
212 
214 
215  ASSERT (p == 0);
216 
218 
219  t->name = name;
220 
222 
224  t - lm->lex_tables);
225 
227  VLIB_LEX_eof, t - lm->lex_tables);
228 
229  return t - lm->lex_tables;
230 }
231 
232 void
233 vlib_lex_reset (vlib_lex_main_t * lm, u8 * input_vector)
234 {
235  if (lm->pushback_vector)
236  _vec_len (lm->pushback_vector) = 0;
237  lm->pushback_sp = -1;
238 
239  lm->input_vector = input_vector;
240  lm->current_index = 0;
241 }
242 
243 static clib_error_t *
245 {
247 
248  lm->lex_tables_by_name = hash_create_string (0, sizeof (uword));
249  lm->lex_tokens_by_name = hash_create_string (0, sizeof (uword));
250  lm->lex_keywords = hash_create_string (0, sizeof (uword));
251  lm->pushback_sp = -1;
252 
253 #define _(f) { u16 tmp = vlib_lex_add_token (lm, #f); ASSERT (tmp == VLIB_LEX_##f); }
255 #undef _
256 
257  vec_validate (lm->token_buffer, 127);
258  _vec_len (lm->token_buffer) = 0;
259 
260  return 0;
261 }
262 
264 
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "gnu")
270  * End:
271  */
vlib.h
vlib_lex_main_t::input_vector
u8 * input_vector
Definition: lex.h:108
vlib_lex_table_entry_t::token
u16 token
Definition: lex.h:62
vlib_lex_table_entry_t
Definition: lex.h:58
vlib_lex_token_t::token
u32 token
Definition: lex.h:73
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
name
string name[64]
Definition: fib.api:25
VLIB_LEX_ADD_TO_TOKEN
@ VLIB_LEX_ADD_TO_TOKEN
Definition: lex.h:50
pool_get_aligned
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P with alignment A.
Definition: pool.h:249
vlib_lex_add_table
u16 vlib_lex_add_table(char *name)
Definition: lex.c:207
hash_create_string
#define hash_create_string(elts, value_bytes)
Definition: hash.h:689
vlib_lex_main_t::pushback_vector
vlib_lex_token_t * pushback_vector
Definition: lex.h:97
u16
unsigned short u16
Definition: types.h:57
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_lex_table_t
Definition: lex.h:65
VLIB_LEX_RETURN
@ VLIB_LEX_RETURN
Definition: lex.h:51
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
lex.h
vlib_lex_main_t::lex_keywords
uword * lex_keywords
Definition: lex.h:95
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_lex_main_t
Definition: lex.h:83
vlib_lex_main_t::current_number_base
uword current_number_base
Definition: lex.h:105
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vec_dup
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:444
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vlib_lex_main_t::lex_tables_by_name
uword * lex_tables_by_name
Definition: lex.h:86
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
vlib_lex_main_t::lex_tokens_by_name
uword * lex_tokens_by_name
Definition: lex.h:92
vlib_lex_main
vlib_lex_main_t vlib_lex_main
Definition: lex.c:18
uword
u64 uword
Definition: types.h:112
VLIB_LEX_START_NUMBER
@ VLIB_LEX_START_NUMBER
Definition: lex.h:54
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
vlib_lex_set_action_range
void vlib_lex_set_action_range(u32 table_index, u8 lo, u8 hi, u16 action, u16 token, u32 next_table_index)
Definition: lex.c:190
vlib_lex_reset
void vlib_lex_reset(vlib_lex_main_t *lm, u8 *input_vector)
Definition: lex.c:233
VLIB_LEX_KEYWORD_CHECK
@ VLIB_LEX_KEYWORD_CHECK
Definition: lex.h:53
vlib_lex_token_t::as_pointer
void * as_pointer
Definition: lex.h:78
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
vlib_lex_table_t::entries
vlib_lex_table_entry_t entries[128]
Definition: lex.h:68
foreach_vlib_lex_global_token
@ foreach_vlib_lex_global_token
Definition: lex.h:43
LEX_DEBUG
#define LEX_DEBUG
Definition: lex.c:20
vlib_lex_main_t::current_index
u32 current_index
Definition: lex.h:111
vlib_lex_table_entry_t::next_table_index
u16 next_table_index
Definition: lex.h:61
vlib_lex_table_entry_t::action
u16 action
Definition: lex.h:60
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
VLIB_LEX_RETURN_AND_RESCAN
@ VLIB_LEX_RETURN_AND_RESCAN
Definition: lex.h:52
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vlib_lex_main_t::pushback_sp
i32 pushback_sp
Definition: lex.h:99
vlib_lex_find_or_add_keyword
u16 vlib_lex_find_or_add_keyword(vlib_lex_main_t *lm, char *keyword, char *token_name)
Definition: lex.c:182
lex_onetime_init
static clib_error_t * lex_onetime_init(vlib_main_t *vm)
Definition: lex.c:244
vlib_lex_main_t::lex_token_names
char ** lex_token_names
Definition: lex.h:89
vlib_main_t
Definition: main.h:102
format_vlib_lex_token
u8 * format_vlib_lex_token(u8 *s, va_list *args)
Definition: lex.c:23
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_lex_main_t::lex_tables
vlib_lex_table_t * lex_tables
Definition: lex.h:85
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
VLIB_LEX_IGNORE
@ VLIB_LEX_IGNORE
Definition: lex.h:49
i
int i
Definition: flowhash_template.h:376
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
rv
int __clib_unused rv
Definition: application.c:491
vlib_lex_main_t::token_buffer
u8 * token_buffer
Definition: lex.h:114
vlib_lex_get_token
void vlib_lex_get_token(vlib_lex_main_t *lm, vlib_lex_token_t *rv)
Definition: lex.c:36
vlib_lex_main_t::current_table_index
u32 current_table_index
Definition: lex.h:101
vlib_lex_add_token
u16 vlib_lex_add_token(vlib_lex_main_t *lm, char *token_name)
Definition: lex.c:148
vlib_lex_main_t::current_token_value
uword current_token_value
Definition: lex.h:103
vlib_lex_token_t
Definition: lex.h:71
add_keyword
static u16 add_keyword(vlib_lex_main_t *lm, char *keyword, char *token_name)
Definition: lex.c:166
VLIB_LEX_ADD_TO_NUMBER
@ VLIB_LEX_ADD_TO_NUMBER
Definition: lex.h:55
action
vl_api_mac_event_action_t action
Definition: l2.api:211
lo
lo
Definition: vector_altivec.h:87
vlib_lex_table_t::name
char * name
Definition: lex.h:67
vlib_lex_token_t::value
union vlib_lex_token_t::@628 value