2 * Copyright (c) 2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <builtinurl/builtinurl.h>
18 #include <http_static/http_static.h>
19 #include <vpp/app/version.h>
22 handle_get_version (u8 * request, http_session_t * hs)
26 /* Build some json bullshit */
27 s = format (s, "{\"vpp_details\": {");
28 s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER);
29 s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
33 hs->cache_pool_index = ~0;
39 trim_path_from_request (u8 * s, char *path)
42 int trim_length = strlen (path) + 1 /* remove '?' */ ;
44 /* Get rid of the path and question-mark */
45 vec_delete (s, trim_length, 0);
47 /* Tail trim irrelevant browser info */
49 while ((cp - s) < vec_len (s))
54 * Makes request a vector which happens to look
58 _vec_len (s) = cp - s;
66 handle_get_interface_stats (u8 * request, http_session_t * hs)
68 u8 *s = 0, *stats = 0;
71 vnet_sw_interface_t *si;
72 u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
73 vnet_sw_interface_t * si, int json);
75 vnet_main_t *vnm = vnet_get_main ();
77 trim_path_from_request (request, "interface_stats.json");
81 p = hash_get (vnm->interface_main.hw_interface_by_name, request);
84 s = format (s, "{\"interface_stats\": {");
85 s = format (s, " \"name\": \"%s\",", request);
86 s = format (s, " \"stats\": \"%s\"", "ERRORUnknownInterface");
87 s = format (s, "}}\r\n");
92 si = vnet_get_sw_interface (vnm, sw_if_index);
94 stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
97 /* No active counters */
100 s = format (s, "{\"interface_stats\": {");
101 s = format (s, " \"name\": \"%s\"", request);
102 s = format (s, "}}\r\n");
107 s = format (s, "{\"interface_stats\": {");
108 s = format (s, "\"name\": \"%s\",\n", request);
109 s = format (s, "%s", stats);
110 s = format (s, "}}\n");
116 hs->cache_pool_index = ~0;
122 handle_get_interface_list (u8 * request, http_session_t * hs)
126 vnet_main_t *vnm = vnet_get_main ();
127 vnet_interface_main_t *im = &vnm->interface_main;
128 vnet_hw_interface_t *hi;
129 u32 *hw_if_indices = 0;
132 /* Construct vector of active hw_if_indexes ... */
134 pool_foreach (hi, im->hw_interfaces,
136 /* No point in mentioning "local0"... */
137 if (hi - im->hw_interfaces)
138 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
143 s = format (s, "{\"interface_list\": [\n");
144 for (i = 0; i < vec_len (hw_if_indices); i++)
147 s = format (s, ",\n");
148 hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
149 s = format (s, "\"%v\"", hi->name);
152 s = format (s, "]}\n");
153 vec_free (hw_if_indices);
157 hs->cache_pool_index = ~0;
163 builtinurl_handler_init (builtinurl_main_t * bm)
166 bm->register_handler (handle_get_version, "version.json",
167 HTTP_BUILTIN_METHOD_GET);
168 bm->register_handler (handle_get_interface_list, "interface_list.json",
169 HTTP_BUILTIN_METHOD_GET);
170 bm->register_handler (handle_get_interface_stats,
171 "interface_stats.json", HTTP_BUILTIN_METHOD_POST);
175 * fd.io coding-style-patch-verification: ON
178 * eval: (c-set-style "gnu")