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 (http_builtin_method_type_t reqtype,
23 u8 * request, http_session_t * hs)
27 /* Build some json bullshit */
28 s = format (s, "{\"vpp_details\": {");
29 s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER);
30 s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
34 hs->cache_pool_index = ~0;
40 trim_path_from_request (u8 * s, char *path)
43 int trim_length = strlen (path) + 1 /* remove '?' */ ;
45 /* Get rid of the path and question-mark */
46 vec_delete (s, trim_length, 0);
48 /* Tail trim irrelevant browser info */
50 while ((cp - s) < vec_len (s))
55 * Makes request a vector which happens to look
59 _vec_len (s) = cp - s;
67 handle_get_interface_stats (http_builtin_method_type_t reqtype,
68 u8 * request, http_session_t * hs)
70 u8 *s = 0, *stats = 0;
72 u32 *sw_if_indices = 0;
73 vnet_hw_interface_t *hi;
74 vnet_sw_interface_t *si;
78 u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
79 vnet_sw_interface_t * si, int json);
80 vnet_main_t *vnm = vnet_get_main ();
81 vnet_interface_main_t *im = &vnm->interface_main;
83 /* Get stats for a single interface via http POST */
84 if (reqtype == HTTP_BUILTIN_METHOD_POST)
86 trim_path_from_request (request, "interface_stats.json");
88 /* Find the sw_if_index */
89 p = hash_get (im->hw_interface_by_name, request);
92 s = format (s, "{\"interface_stats\": {[\n");
93 s = format (s, " \"name\": \"%s\",", request);
94 s = format (s, " \"error\": \"%s\"", "UnknownInterface");
95 s = format (s, "]}\n");
99 vec_add1 (sw_if_indices, p[0]);
101 else /* default, HTTP_BUILTIN_METHOD_GET */
104 pool_foreach (hi, im->hw_interfaces,
106 vec_add1 (sw_if_indices, hi->sw_if_index);
111 s = format (s, "{%sinterface_stats%s: [\n", q, q);
113 for (i = 0; i < vec_len (sw_if_indices); i++)
115 si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
117 s = format (s, ",\n");
121 s = format (s, "{%sname%s: %s%U%s, ", q, q, q,
122 format_vnet_sw_if_index_name, vnm, sw_if_indices[i], q);
124 stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
127 s = format (s, "%v}", stats);
129 s = format (s, "%snone%s: %strue%s}", q, q, q, q);
130 vec_reset_length (stats);
133 s = format (s, "]}\n");
138 hs->cache_pool_index = ~0;
140 vec_free (sw_if_indices);
146 handle_get_interface_list (http_builtin_method_type_t reqtype,
147 u8 * request, http_session_t * hs)
151 vnet_main_t *vnm = vnet_get_main ();
152 vnet_interface_main_t *im = &vnm->interface_main;
153 vnet_hw_interface_t *hi;
154 u32 *hw_if_indices = 0;
157 /* Construct vector of active hw_if_indexes ... */
159 pool_foreach (hi, im->hw_interfaces,
161 /* No point in mentioning "local0"... */
162 if (hi - im->hw_interfaces)
163 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
168 s = format (s, "{\"interface_list\": [\n");
169 for (i = 0; i < vec_len (hw_if_indices); i++)
172 s = format (s, ",\n");
173 hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
174 s = format (s, "\"%v\"", hi->name);
177 s = format (s, "]}\n");
178 vec_free (hw_if_indices);
182 hs->cache_pool_index = ~0;
188 builtinurl_handler_init (builtinurl_main_t * bm)
191 bm->register_handler (handle_get_version, "version.json",
192 HTTP_BUILTIN_METHOD_GET);
193 bm->register_handler (handle_get_interface_list, "interface_list.json",
194 HTTP_BUILTIN_METHOD_GET);
195 bm->register_handler (handle_get_interface_stats,
196 "interface_stats.json", HTTP_BUILTIN_METHOD_GET);
197 bm->register_handler (handle_get_interface_stats,
198 "interface_stats.json", HTTP_BUILTIN_METHOD_POST);
202 * fd.io coding-style-patch-verification: ON
205 * eval: (c-set-style "gnu")