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 (hss_url_handler_args_t *args)
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);
32 args->data_len = vec_len (s);
33 args->free_vec_data = 1;
34 return HSS_URL_HANDLER_OK;
38 trim_path_from_request (u8 * s, char *path)
41 int trim_length = strlen (path) + 1 /* remove '?' */ ;
43 /* Get rid of the path and question-mark */
44 vec_delete (s, trim_length, 0);
46 /* Tail trim irrelevant browser info */
48 while ((cp - s) < vec_len (s))
53 * Makes request a vector which happens to look
57 _vec_len (s) = cp - s;
65 handle_get_interface_stats (hss_url_handler_args_t *args)
67 u8 *s = 0, *stats = 0;
69 u32 *sw_if_indices = 0;
70 vnet_hw_interface_t *hi;
71 vnet_sw_interface_t *si;
75 u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
76 vnet_sw_interface_t * si, int json);
77 vnet_main_t *vnm = vnet_get_main ();
78 vnet_interface_main_t *im = &vnm->interface_main;
80 /* Get stats for a single interface via http POST */
81 if (args->reqtype == HTTP_REQ_POST)
83 trim_path_from_request (args->request, "interface_stats.json");
85 /* Find the sw_if_index */
86 p = hash_get (im->hw_interface_by_name, args->request);
89 s = format (s, "{\"interface_stats\": {[\n");
90 s = format (s, " \"name\": \"%s\",", args->request);
91 s = format (s, " \"error\": \"%s\"", "UnknownInterface");
92 s = format (s, "]}\n");
96 vec_add1 (sw_if_indices, p[0]);
98 else /* default, HTTP_BUILTIN_METHOD_GET */
101 pool_foreach (hi, im->hw_interfaces)
103 vec_add1 (sw_if_indices, hi->sw_if_index);
108 s = format (s, "{%sinterface_stats%s: [\n", q, q);
110 for (i = 0; i < vec_len (sw_if_indices); i++)
112 si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
114 s = format (s, ",\n");
118 s = format (s, "{%sname%s: %s%U%s, ", q, q, q,
119 format_vnet_sw_if_index_name, vnm, sw_if_indices[i], q);
121 stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
124 s = format (s, "%v}", stats);
126 s = format (s, "%snone%s: %strue%s}", q, q, q, q);
127 vec_reset_length (stats);
130 s = format (s, "]}\n");
134 args->data_len = vec_len (s);
135 args->free_vec_data = 1;
136 vec_free (sw_if_indices);
138 return HSS_URL_HANDLER_OK;
142 handle_get_interface_list (hss_url_handler_args_t *args)
146 vnet_main_t *vnm = vnet_get_main ();
147 vnet_interface_main_t *im = &vnm->interface_main;
148 vnet_hw_interface_t *hi;
149 u32 *hw_if_indices = 0;
152 /* Construct vector of active hw_if_indexes ... */
154 pool_foreach (hi, im->hw_interfaces)
156 /* No point in mentioning "local0"... */
157 if (hi - im->hw_interfaces)
158 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
163 s = format (s, "{\"interface_list\": [\n");
164 for (i = 0; i < vec_len (hw_if_indices); i++)
167 s = format (s, ",\n");
168 hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
169 s = format (s, "\"%v\"", hi->name);
172 s = format (s, "]}\n");
173 vec_free (hw_if_indices);
176 args->data_len = vec_len (s);
177 args->free_vec_data = 1;
178 return HSS_URL_HANDLER_OK;
182 builtinurl_handler_init (builtinurl_main_t * bm)
185 bm->register_handler (handle_get_version, "version.json", HTTP_REQ_GET);
186 bm->register_handler (handle_get_interface_list, "interface_list.json",
188 bm->register_handler (handle_get_interface_stats, "interface_stats.json",
190 bm->register_handler (handle_get_interface_stats, "interface_stats.json",
195 * fd.io coding-style-patch-verification: ON
198 * eval: (c-set-style "gnu")