Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [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_vat_h__ |
| 16 | #define __included_vat_h__ |
| 17 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <setjmp.h> |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 24 | #include <vppinfra/clib.h> |
| 25 | #include <vppinfra/format.h> |
| 26 | #include <vppinfra/error.h> |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 27 | #include <vppinfra/elog.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 28 | #include <vppinfra/time.h> |
| 29 | #include <vppinfra/macros.h> |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 30 | #include <vppinfra/socket.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 31 | #include <vnet/vnet.h> |
| 32 | #include <vlib/vlib.h> |
| 33 | #include <vlib/unix/unix.h> |
| 34 | #include <vlibapi/api.h> |
| 35 | #include <vlibmemory/api.h> |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 36 | #include <vlibmemory/memclnt.api_enum.h> |
| 37 | #include <vlibmemory/memclnt.api_types.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 38 | |
| 39 | #include "vat/json_format.h" |
| 40 | |
| 41 | #include <vlib/vlib.h> |
| 42 | |
| 43 | typedef struct |
| 44 | { |
| 45 | u8 *interface_name; |
| 46 | u32 sw_if_index; |
| 47 | /* |
| 48 | * Subinterface ID. A number 0-N to uniquely identify this |
| 49 | * subinterface under the super interface |
| 50 | */ |
| 51 | u32 sub_id; |
| 52 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 53 | /* Number of tags 0-2 */ |
| 54 | u8 sub_number_of_tags; |
| 55 | u16 sub_outer_vlan_id; |
| 56 | u16 sub_inner_vlan_id; |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 57 | |
| 58 | union |
| 59 | { |
| 60 | u16 raw_flags; |
| 61 | struct |
| 62 | { |
| 63 | u16 no_tags:1; |
| 64 | u16 one_tag:1; |
| 65 | u16 two_tags:1; |
| 66 | /* 0 = dot1q, 1=dot1ad */ |
| 67 | u16 sub_dot1ad:1; |
| 68 | u16 sub_exact_match:1; |
| 69 | u16 sub_default:1; |
| 70 | u16 sub_outer_vlan_id_any:1; |
| 71 | u16 sub_inner_vlan_id_any:1; |
| 72 | }; |
| 73 | }; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 74 | |
| 75 | /* vlan tag rewrite */ |
| 76 | u32 vtr_op; |
| 77 | u32 vtr_push_dot1q; |
| 78 | u32 vtr_tag1; |
| 79 | u32 vtr_tag2; |
| 80 | } sw_interface_subif_t; |
| 81 | |
| 82 | typedef struct |
| 83 | { |
| 84 | u8 ip[16]; |
| 85 | u8 prefix_length; |
| 86 | } ip_address_details_t; |
| 87 | |
| 88 | typedef struct |
| 89 | { |
| 90 | u8 present; |
| 91 | ip_address_details_t *addr; |
| 92 | } ip_details_t; |
| 93 | |
| 94 | typedef struct |
| 95 | { |
| 96 | u64 packets; |
| 97 | u64 bytes; |
| 98 | } interface_counter_t; |
| 99 | |
| 100 | typedef struct |
| 101 | { |
| 102 | struct in_addr address; |
| 103 | u8 address_length; |
| 104 | u64 packets; |
| 105 | u64 bytes; |
| 106 | } ip4_fib_counter_t; |
| 107 | |
| 108 | typedef struct |
| 109 | { |
| 110 | struct in6_addr address; |
| 111 | u8 address_length; |
| 112 | u64 packets; |
| 113 | u64 bytes; |
| 114 | } ip6_fib_counter_t; |
| 115 | |
| 116 | typedef struct |
| 117 | { |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 118 | struct in_addr address; |
| 119 | vnet_link_t linkt; |
| 120 | u64 packets; |
| 121 | u64 bytes; |
| 122 | } ip4_nbr_counter_t; |
| 123 | |
| 124 | typedef struct |
| 125 | { |
| 126 | struct in6_addr address; |
| 127 | vnet_link_t linkt; |
| 128 | u64 packets; |
| 129 | u64 bytes; |
| 130 | } ip6_nbr_counter_t; |
| 131 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 132 | struct vat_registered_features_t; |
| 133 | |
Damjan Marion | dd298e8 | 2022-10-12 16:02:18 +0200 | [diff] [blame] | 134 | typedef struct vat_main_ |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 135 | { |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 136 | /* vpe input queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 137 | svm_queue_t *vl_input_queue; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 138 | |
| 139 | /* interface name table */ |
| 140 | uword *sw_if_index_by_interface_name; |
| 141 | |
| 142 | /* subinterface table */ |
| 143 | sw_interface_subif_t *sw_if_subif_table; |
| 144 | |
| 145 | /* Graph node table */ |
| 146 | uword *graph_node_index_by_name; |
Dave Barach | 1ddbc01 | 2018-06-13 09:26:05 -0400 | [diff] [blame] | 147 | vlib_node_t ***graph_nodes; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 148 | |
| 149 | /* ip tables */ |
| 150 | ip_details_t *ip_details_by_sw_if_index[2]; |
| 151 | |
| 152 | /* sw_if_index of currently processed interface */ |
| 153 | u32 current_sw_if_index; |
| 154 | |
| 155 | /* remember that we are dumping ipv6 */ |
| 156 | u8 is_ipv6; |
| 157 | |
| 158 | /* function table */ |
| 159 | uword *function_by_name; |
| 160 | |
| 161 | /* help strings */ |
| 162 | uword *help_by_name; |
| 163 | |
| 164 | /* macro table */ |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 165 | clib_macro_main_t macro_main; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 166 | |
| 167 | /* Errors by number */ |
| 168 | uword *error_string_by_error_number; |
| 169 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 170 | /* Main thread can spin (w/ timeout) here if needed */ |
| 171 | u32 async_mode; |
| 172 | u32 async_errors; |
| 173 | volatile u32 result_ready; |
| 174 | volatile i32 retval; |
| 175 | volatile u32 sw_if_index; |
| 176 | volatile u8 *shmem_result; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 177 | u8 *cmd_reply; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 178 | |
| 179 | /* our client index */ |
| 180 | u32 my_client_index; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 181 | int client_index_invalid; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 182 | |
| 183 | /* Time is of the essence... */ |
| 184 | clib_time_t clib_time; |
| 185 | |
| 186 | /* Unwind (so we can quit) */ |
| 187 | jmp_buf jump_buf; |
| 188 | int jump_buf_set; |
| 189 | volatile int do_exit; |
| 190 | |
| 191 | /* temporary parse buffer */ |
| 192 | unformat_input_t *input; |
| 193 | |
| 194 | /* input buffer */ |
| 195 | u8 *inbuf; |
| 196 | |
| 197 | /* stdio input / output FILEs */ |
| 198 | FILE *ifp, *ofp; |
| 199 | u8 *current_file; |
| 200 | u32 input_line_number; |
| 201 | |
| 202 | /* exec mode toggle */ |
| 203 | int exec_mode; |
| 204 | |
| 205 | /* Regenerate the interface table */ |
| 206 | volatile int regenerate_interface_table; |
| 207 | |
| 208 | /* flag for JSON output format */ |
| 209 | u8 json_output; |
| 210 | |
| 211 | /* flag for interface event display */ |
| 212 | u8 interface_event_display; |
| 213 | |
| 214 | /* JSON tree used in composing dump api call results */ |
| 215 | vat_json_node_t json_tree; |
| 216 | |
| 217 | /* counters */ |
| 218 | u64 **simple_interface_counters; |
| 219 | interface_counter_t **combined_interface_counters; |
| 220 | ip4_fib_counter_t **ip4_fib_counters; |
| 221 | u32 *ip4_fib_counters_vrf_id_by_index; |
| 222 | ip6_fib_counter_t **ip6_fib_counters; |
| 223 | u32 *ip6_fib_counters_vrf_id_by_index; |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 224 | ip4_nbr_counter_t **ip4_nbr_counters; |
| 225 | ip6_nbr_counter_t **ip6_nbr_counters; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 226 | |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 227 | ssvm_private_t stat_segment; |
| 228 | clib_spinlock_t *stat_segment_lockp; |
| 229 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 230 | socket_client_main_t *socket_client_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 231 | u8 *socket_name; |
| 232 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 233 | elog_main_t elog_main; |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 234 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 235 | struct vat_registered_features_t *feature_function_registrations; |
| 236 | |
Damjan Marion | dd298e8 | 2022-10-12 16:02:18 +0200 | [diff] [blame] | 237 | int (*api_sw_interface_dump) (struct vat_main_ *); |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 238 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 239 | /* Convenience */ |
| 240 | vlib_main_t *vlib_main; |
| 241 | } vat_main_t; |
| 242 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 243 | extern vat_main_t vat_main; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 244 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 245 | void vat_suspend (vlib_main_t * vm, f64 interval); |
| 246 | f64 vat_time_now (vat_main_t * vam); |
| 247 | void errmsg (char *fmt, ...); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 248 | void vat_api_hookup (vat_main_t * vam); |
| 249 | int api_sw_interface_dump (vat_main_t * vam); |
| 250 | void do_one_file (vat_main_t * vam); |
| 251 | int exec (vat_main_t * vam); |
| 252 | |
| 253 | /* Plugin API library functions */ |
BenoƮt Ganne | 49ee684 | 2019-04-30 11:50:46 +0200 | [diff] [blame] | 254 | extern char *vat_plugin_path; |
| 255 | extern char *vat_plugin_name_filter; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 256 | void vat_plugin_api_reference (void); |
| 257 | uword unformat_sw_if_index (unformat_input_t * input, va_list * args); |
| 258 | uword unformat_ip4_address (unformat_input_t * input, va_list * args); |
| 259 | uword unformat_ethernet_address (unformat_input_t * input, va_list * args); |
| 260 | uword unformat_ethernet_type_host_byte_order (unformat_input_t * input, |
| 261 | va_list * args); |
| 262 | uword unformat_ip6_address (unformat_input_t * input, va_list * args); |
| 263 | u8 *format_ip4_address (u8 * s, va_list * args); |
Jon Loeliger | 229a6b7 | 2017-05-08 16:53:44 -0500 | [diff] [blame] | 264 | u8 *format_ip6_address (u8 * s, va_list * args); |
| 265 | u8 *format_ip46_address (u8 * s, va_list * args); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 266 | u8 *format_ethernet_address (u8 * s, va_list * args); |
| 267 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 268 | int vat_socket_connect (vat_main_t * vam); |
| 269 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 270 | #if VPP_API_TEST_BUILTIN |
| 271 | #define print api_cli_output |
| 272 | void api_cli_output (void *, const char *fmt, ...); |
| 273 | #else |
| 274 | #define print fformat_append_cr |
| 275 | void fformat_append_cr (FILE *, const char *fmt, ...); |
| 276 | #endif |
| 277 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 278 | |
| 279 | typedef clib_error_t *(vat_feature_function_t) (vat_main_t * vam); |
| 280 | typedef struct vat_registered_features_t |
| 281 | { |
| 282 | vat_feature_function_t *function; |
| 283 | struct vat_registered_features_t *next; |
| 284 | } vat_registered_features_t; |
| 285 | |
| 286 | |
| 287 | #define VAT_REGISTER_FEATURE_FUNCTION(x) \ |
| 288 | vat_registered_features_t _vat_feature_function_##x; \ |
| 289 | static void __vlib_add_config_function_##x (void) \ |
| 290 | __attribute__((__constructor__)) ; \ |
| 291 | static void __vlib_add_config_function_##x (void) \ |
| 292 | { \ |
| 293 | vat_main_t * vam = &vat_main; \ |
| 294 | _vat_feature_function_##x.next = vam->feature_function_registrations; \ |
| 295 | vam->feature_function_registrations = &_vat_feature_function_##x; \ |
| 296 | } \ |
| 297 | vat_registered_features_t _vat_feature_function_##x = \ |
| 298 | { \ |
| 299 | .function = x, \ |
| 300 | } |
| 301 | |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 302 | #if VPP_API_TEST_BUILTIN == 0 |
| 303 | static_always_inline uword |
| 304 | api_unformat_sw_if_index (unformat_input_t *input, va_list *args) |
| 305 | { |
| 306 | vat_main_t *vam = va_arg (*args, vat_main_t *); |
| 307 | u32 *result = va_arg (*args, u32 *); |
| 308 | u8 *if_name; |
| 309 | uword *p; |
| 310 | |
| 311 | if (!unformat (input, "%s", &if_name)) |
| 312 | return 0; |
| 313 | |
| 314 | p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name); |
| 315 | if (p == 0) |
| 316 | return 0; |
| 317 | *result = p[0]; |
| 318 | return 1; |
| 319 | } |
| 320 | #endif /* VPP_API_TEST_BUILTIN */ |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 321 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 322 | #endif /* __included_vat_h__ */ |
| 323 | |
| 324 | /* |
| 325 | * fd.io coding-style-patch-verification: ON |
| 326 | * |
| 327 | * Local Variables: |
| 328 | * eval: (c-set-style "gnu") |
| 329 | * End: |
| 330 | */ |