blob: 036e744723bc7f2acab4731dd6a67f3fc7d4d7bf [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#ifndef included_vlib_parse_h
16#define included_vlib_parse_h
17
18#include <vlib/vlib.h>
19#include <vlib/lex.h>
20#include <vppinfra/mhash.h>
21
Dave Barach9b8ffd92016-07-08 08:13:45 -040022typedef struct
23{
Ed Warnickecb9cada2015-12-08 15:45:58 -070024 /* Word aligned value. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040025 union
26 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 u8 as_u8[32 - 1 * sizeof (u16)];
Dave Barach9b8ffd92016-07-08 08:13:45 -040028 void *as_pointer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 uword as_uword;
30 word as_word;
31 u64 as_u64;
32 } value;
33
34 /* 16 bit type at end so that 30 bytes of value are aligned. */
35 u16 type;
Dave Barach9b8ffd92016-07-08 08:13:45 -040036} __attribute ((packed))
37 vlib_parse_value_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070038
39/* Instance of a type. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040040 typedef struct
41 {
42 u32
43 type;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Dave Barach9b8ffd92016-07-08 08:13:45 -040045 u32
46 origin;
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Dave Barach9b8ffd92016-07-08 08:13:45 -040048 u32
49 help_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Dave Barach9b8ffd92016-07-08 08:13:45 -040051 union
52 {
53 void *
54 as_pointer;
55 uword
56 as_uword;
57 } value;
58 } vlib_parse_item_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
Dave Barach9b8ffd92016-07-08 08:13:45 -040060 typedef struct
61 {
62 /* Index of item for this node. */
63 u32
64 item;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Dave Barach9b8ffd92016-07-08 08:13:45 -040066 /* Graph index of peer (sibling) node (linked list of peers). */
67 u32
68 peer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
Dave Barach9b8ffd92016-07-08 08:13:45 -040070 /* Graph index of deeper (child) node (linked list of children). */
71 u32
72 deeper;
73 } vlib_parse_graph_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75#define foreach_parse_match_type \
76 _(MATCH_DONE) \
77 _(MATCH_RULE) \
78 _(MATCH_FAIL) \
79 _(MATCH_FULL) \
80 _(MATCH_VALUE) \
81 _(MATCH_PARTIAL) \
82 _(MATCH_AMBIGUOUS) \
83 _(MATCH_EVAL_FAIL)
84
Dave Barach9b8ffd92016-07-08 08:13:45 -040085 typedef enum
86 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070087#define _(a) VLIB_PARSE_##a,
Dave Barach9b8ffd92016-07-08 08:13:45 -040088 foreach_parse_match_type
Ed Warnickecb9cada2015-12-08 15:45:58 -070089#undef _
Dave Barach9b8ffd92016-07-08 08:13:45 -040090 } vlib_parse_match_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Dave Barach9b8ffd92016-07-08 08:13:45 -040092 struct vlib_parse_type;
93 struct vlib_parse_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Dave Barach9b8ffd92016-07-08 08:13:45 -040095 typedef
96 vlib_parse_match_t (vlib_parse_match_function_t)
97 (struct vlib_parse_main *,
98 struct vlib_parse_type *, vlib_lex_token_t *, vlib_parse_value_t *);
99 typedef void (vlib_parse_value_cleanup_function_t) (vlib_parse_value_t
100 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Dave Barach9b8ffd92016-07-08 08:13:45 -0400102 typedef struct vlib_parse_type
103 {
104 /* Type name. */
105 char *
106 name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108 vlib_parse_match_function_t *
109 match_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Dave Barach9b8ffd92016-07-08 08:13:45 -0400111 vlib_parse_value_cleanup_function_t *
112 value_cleanup_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 format_function_t *
115 format_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 u32
118 rule_index;
119 } vlib_parse_type_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121 typedef struct
122 {
123 char *
124 initializer;
125 void *
126 eof_match;
127 int
128 rule_length;
129 } parse_registration_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131 typedef struct vlib_parse_main
132 {
133 /* (type, origin, help, value) tuples */
134 vlib_parse_item_t *
135 parse_items;
136 mhash_t
137 parse_item_hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139 /* (item, peer, deeper) tuples */
140 vlib_parse_graph_t *
141 parse_graph;
142 u32
143 root_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 u8 *
146 register_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148 /* parser types */
149 vlib_parse_type_t *
150 parse_types;
151 uword *
152 parse_type_by_name_hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 /* Vector of MATCH_VALUEs */
155 vlib_parse_value_t *
156 parse_value;
157 u32 *
158 match_items;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 /* Parse registrations */
161 parse_registration_t **
162 parse_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 /* Token vector */
165 vlib_lex_token_t *
166 tokens;
167 u32
168 current_token_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
Dave Barach9b8ffd92016-07-08 08:13:45 -0400170 vlib_lex_main_t *
171 lex_main;
172 vlib_main_t *
173 vlib_main;
174 } vlib_parse_main_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vlib_parse_main_t
177 vlib_parse_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179 typedef
180 vlib_parse_match_t (vlib_parse_eval_function_t)
181 (vlib_parse_main_t *, vlib_parse_item_t *, vlib_parse_value_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183vlib_parse_match_t
184vlib_parse_eval (u8 * input);
185
186 format_function_t format_vlib_parse_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188/* FIXME need these to be global? */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400189 vlib_parse_match_function_t rule_match, eof_match, word_match,
190 number_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192#define _PARSE_REGISTRATION_DATA(x) \
193VLIB_ELF_SECTION_DATA(x##_registration,parse_registration_t,parse_registrations)
194
195#define PARSE_INIT(x, s, e) \
196static _PARSE_REGISTRATION_DATA(x) = { \
197 .initializer = s, \
198 .eof_match = e, \
199};
200
201#define _PARSE_TYPE_REGISTRATION_DATA(x) \
202VLIB_ELF_SECTION_DATA(x##_type_registration,vlib_parse_type_t, \
203parse_type_registrations)
204
205#define PARSE_TYPE_INIT(n, m, c, f) \
206static _PARSE_TYPE_REGISTRATION_DATA(n) = { \
207 .name = #n, \
208 .match_function = m, \
209 .value_cleanup_function = c, \
210 .format_value = f, \
211};
212
213#endif /* included_vlib_parse_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */