Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 22 | typedef struct |
| 23 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | /* Word aligned value. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 25 | union |
| 26 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | u8 as_u8[32 - 1 * sizeof (u16)]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 28 | void *as_pointer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 36 | } __attribute ((packed)) |
| 37 | vlib_parse_value_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | |
| 39 | /* Instance of a type. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 40 | typedef struct |
| 41 | { |
| 42 | u32 |
| 43 | type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 45 | u32 |
| 46 | origin; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 48 | u32 |
| 49 | help_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 51 | union |
| 52 | { |
| 53 | void * |
| 54 | as_pointer; |
| 55 | uword |
| 56 | as_uword; |
| 57 | } value; |
| 58 | } vlib_parse_item_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 60 | typedef struct |
| 61 | { |
| 62 | /* Index of item for this node. */ |
| 63 | u32 |
| 64 | item; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 66 | /* Graph index of peer (sibling) node (linked list of peers). */ |
| 67 | u32 |
| 68 | peer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 70 | /* Graph index of deeper (child) node (linked list of children). */ |
| 71 | u32 |
| 72 | deeper; |
| 73 | } vlib_parse_graph_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | typedef enum |
| 86 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | #define _(a) VLIB_PARSE_##a, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 88 | foreach_parse_match_type |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | #undef _ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 90 | } vlib_parse_match_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | struct vlib_parse_type; |
| 93 | struct vlib_parse_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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 | *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | typedef struct vlib_parse_type |
| 103 | { |
| 104 | /* Type name. */ |
| 105 | char * |
| 106 | name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 108 | vlib_parse_match_function_t * |
| 109 | match_function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 111 | vlib_parse_value_cleanup_function_t * |
| 112 | value_cleanup_function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 114 | format_function_t * |
| 115 | format_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 117 | u32 |
| 118 | rule_index; |
| 119 | } vlib_parse_type_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 121 | typedef struct |
| 122 | { |
| 123 | char * |
| 124 | initializer; |
| 125 | void * |
| 126 | eof_match; |
| 127 | int |
| 128 | rule_length; |
| 129 | } parse_registration_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 139 | /* (item, peer, deeper) tuples */ |
| 140 | vlib_parse_graph_t * |
| 141 | parse_graph; |
| 142 | u32 |
| 143 | root_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | u8 * |
| 146 | register_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | /* parser types */ |
| 149 | vlib_parse_type_t * |
| 150 | parse_types; |
| 151 | uword * |
| 152 | parse_type_by_name_hash; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 154 | /* Vector of MATCH_VALUEs */ |
| 155 | vlib_parse_value_t * |
| 156 | parse_value; |
| 157 | u32 * |
| 158 | match_items; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | /* Parse registrations */ |
| 161 | parse_registration_t ** |
| 162 | parse_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | /* Token vector */ |
| 165 | vlib_lex_token_t * |
| 166 | tokens; |
| 167 | u32 |
| 168 | current_token_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 170 | vlib_lex_main_t * |
| 171 | lex_main; |
| 172 | vlib_main_t * |
| 173 | vlib_main; |
| 174 | } vlib_parse_main_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | vlib_parse_main_t |
| 177 | vlib_parse_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 183 | vlib_parse_match_t |
| 184 | vlib_parse_eval (u8 * input); |
| 185 | |
| 186 | format_function_t format_vlib_parse_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
| 188 | /* FIXME need these to be global? */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | vlib_parse_match_function_t rule_match, eof_match, word_match, |
| 190 | number_match; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * fd.io coding-style-patch-verification: ON |
| 217 | * |
| 218 | * Local Variables: |
| 219 | * eval: (c-set-style "gnu") |
| 220 | * End: |
| 221 | */ |