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 <http_static/http_static.h>
17 #include <vpp/app/version.h>
20 handle_get_version (hss_url_handler_args_t *args)
24 s = format (s, "{\"vpp_details\": {");
25 s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER);
26 s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
29 args->data_len = vec_len (s);
30 args->free_vec_data = 1;
31 return HSS_URL_HANDLER_OK;
35 trim_path_from_request (u8 *s, char *path)
38 int trim_length = strlen (path) + 1 /* remove '?' */;
40 /* Get rid of the path and question-mark */
41 vec_delete (s, trim_length, 0);
43 /* Tail trim irrelevant browser info */
45 while ((cp - s) < vec_len (s))
50 * Makes request a vector which happens to look
54 vec_set_len (s, cp - s);
62 handle_get_interface_stats (hss_url_handler_args_t *args)
64 u8 *s = 0, *stats = 0;
66 u32 *sw_if_indices = 0;
67 vnet_hw_interface_t *hi;
68 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);
74 vnet_main_t *vnm = vnet_get_main ();
75 vnet_interface_main_t *im = &vnm->interface_main;
77 /* Get stats for a single interface via http POST */
78 if (args->reqtype == HTTP_REQ_POST)
80 trim_path_from_request (args->request, "interface_stats.json");
82 /* Find the sw_if_index */
83 p = hash_get (im->hw_interface_by_name, args->request);
86 s = format (s, "{\"interface_stats\": {[\n");
87 s = format (s, " \"name\": \"%s\",", args->request);
88 s = format (s, " \"error\": \"%s\"", "UnknownInterface");
89 s = format (s, "]}\n");
93 vec_add1 (sw_if_indices, p[0]);
95 else /* default, HTTP_BUILTIN_METHOD_GET */
97 pool_foreach (hi, im->hw_interfaces)
99 vec_add1 (sw_if_indices, hi->sw_if_index);
103 s = format (s, "{%sinterface_stats%s: [\n", q, q);
105 for (i = 0; i < vec_len (sw_if_indices); i++)
107 si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
109 s = format (s, ",\n");
113 s = format (s, "{%sname%s: %s%U%s, ", q, q, q,
114 format_vnet_sw_if_index_name, vnm, sw_if_indices[i], q);
116 stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
119 s = format (s, "%v}", stats);
121 s = format (s, "%snone%s: %strue%s}", q, q, q, q);
122 vec_reset_length (stats);
125 s = format (s, "]}\n");
129 args->data_len = vec_len (s);
130 args->free_vec_data = 1;
131 vec_free (sw_if_indices);
133 return HSS_URL_HANDLER_OK;
137 handle_get_interface_list (hss_url_handler_args_t *args)
141 vnet_main_t *vnm = vnet_get_main ();
142 vnet_interface_main_t *im = &vnm->interface_main;
143 vnet_hw_interface_t *hi;
144 u32 *hw_if_indices = 0;
147 /* Construct vector of active hw_if_indexes ... */
148 pool_foreach (hi, im->hw_interfaces)
150 /* No point in mentioning "local0"... */
151 if (hi - im->hw_interfaces)
152 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
156 s = format (s, "{\"interface_list\": [\n");
157 for (i = 0; i < vec_len (hw_if_indices); i++)
160 s = format (s, ",\n");
161 hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
162 s = format (s, "\"%v\"", hi->name);
165 s = format (s, "]}\n");
166 vec_free (hw_if_indices);
169 args->data_len = vec_len (s);
170 args->free_vec_data = 1;
171 return HSS_URL_HANDLER_OK;
175 hss_builtinurl_json_handlers_init (void)
177 hss_register_url_handler (handle_get_version, "version.json", HTTP_REQ_GET);
178 hss_register_url_handler (handle_get_interface_list, "interface_list.json",
180 hss_register_url_handler (handle_get_interface_stats, "interface_stats.json",
182 hss_register_url_handler (handle_get_interface_stats, "interface_stats.json",
187 * fd.io coding-style-patch-verification: ON
190 * eval: (c-set-style "gnu")