dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / vlib / parse.h
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 #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
22 typedef struct
23 {
24   /* Word aligned value. */
25   union
26   {
27     u8 as_u8[32 - 1 * sizeof (u16)];
28     void *as_pointer;
29     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;
36 } __attribute ((packed))
37   vlib_parse_value_t;
38
39 /* Instance of a type. */
40      typedef struct
41      {
42        u32
43          type;
44
45        u32
46          origin;
47
48        u32
49          help_index;
50
51        union
52        {
53          void *
54            as_pointer;
55          uword
56            as_uword;
57        } value;
58      } vlib_parse_item_t;
59
60      typedef struct
61      {
62        /* Index of item for this node. */
63        u32
64          item;
65
66        /* Graph index of peer (sibling) node (linked list of peers). */
67        u32
68          peer;
69
70        /* Graph index of deeper (child) node (linked list of children). */
71        u32
72          deeper;
73      } vlib_parse_graph_t;
74
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
85      typedef enum
86      {
87 #define _(a) VLIB_PARSE_##a,
88        foreach_parse_match_type
89 #undef _
90      } vlib_parse_match_t;
91
92      struct vlib_parse_type;
93      struct vlib_parse_main;
94
95      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                                                          *);
101
102      typedef struct vlib_parse_type
103      {
104        /* Type name. */
105        char *
106          name;
107
108        vlib_parse_match_function_t *
109          match_function;
110
111        vlib_parse_value_cleanup_function_t *
112          value_cleanup_function;
113
114        format_function_t *
115          format_value;
116
117        u32
118          rule_index;
119      } vlib_parse_type_t;
120
121      typedef struct
122      {
123        char *
124          initializer;
125        void *
126          eof_match;
127        int
128          rule_length;
129      } parse_registration_t;
130
131      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;
138
139        /* (item, peer, deeper) tuples */
140        vlib_parse_graph_t *
141          parse_graph;
142        u32
143          root_index;
144
145        u8 *
146          register_input;
147
148        /* parser types */
149        vlib_parse_type_t *
150          parse_types;
151        uword *
152          parse_type_by_name_hash;
153
154        /* Vector of MATCH_VALUEs */
155        vlib_parse_value_t *
156          parse_value;
157        u32 *
158          match_items;
159
160        /* Parse registrations */
161        parse_registration_t **
162          parse_registrations;
163
164        /* Token vector */
165        vlib_lex_token_t *
166          tokens;
167        u32
168          current_token_index;
169
170        vlib_lex_main_t *
171          lex_main;
172        vlib_main_t *
173          vlib_main;
174      } vlib_parse_main_t;
175
176      vlib_parse_main_t
177        vlib_parse_main;
178
179      typedef
180      vlib_parse_match_t (vlib_parse_eval_function_t)
181   (vlib_parse_main_t *, vlib_parse_item_t *, vlib_parse_value_t *);
182
183 vlib_parse_match_t
184 vlib_parse_eval (u8 * input);
185
186      format_function_t format_vlib_parse_value;
187
188 /* FIXME need these to be global? */
189      vlib_parse_match_function_t rule_match, eof_match, word_match,
190        number_match;
191
192 #define _PARSE_REGISTRATION_DATA(x) \
193 VLIB_ELF_SECTION_DATA(x##_registration,parse_registration_t,parse_registrations)
194
195 #define PARSE_INIT(x, s, e)                     \
196 static _PARSE_REGISTRATION_DATA(x) = {          \
197     .initializer = s,                           \
198     .eof_match = e,                             \
199 };
200
201 #define _PARSE_TYPE_REGISTRATION_DATA(x) \
202 VLIB_ELF_SECTION_DATA(x##_type_registration,vlib_parse_type_t, \
203 parse_type_registrations)
204
205 #define PARSE_TYPE_INIT(n, m, c, f)             \
206 static _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 */
214
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "gnu")
220  * End:
221  */