Line data Source code
1 : /*
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:
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 :
16 : #include <vnet/vnet.h>
17 : #include <builtinurl/builtinurl.h>
18 : #include <http_static/http_static.h>
19 : #include <vpp/app/version.h>
20 :
21 : hss_url_handler_rc_t
22 0 : handle_get_version (hss_url_handler_args_t *args)
23 : {
24 0 : u8 *s = 0;
25 :
26 : /* Build some json bullshit */
27 0 : s = format (s, "{\"vpp_details\": {");
28 0 : s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER);
29 0 : s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
30 :
31 0 : args->data = s;
32 0 : args->data_len = vec_len (s);
33 0 : args->free_vec_data = 1;
34 0 : return HSS_URL_HANDLER_OK;
35 : }
36 :
37 : void
38 0 : trim_path_from_request (u8 * s, char *path)
39 : {
40 : u8 *cp;
41 0 : int trim_length = strlen (path) + 1 /* remove '?' */ ;
42 :
43 : /* Get rid of the path and question-mark */
44 0 : vec_delete (s, trim_length, 0);
45 :
46 : /* Tail trim irrelevant browser info */
47 0 : cp = s;
48 0 : while ((cp - s) < vec_len (s))
49 : {
50 0 : if (*cp == ' ')
51 : {
52 : /*
53 : * Makes request a vector which happens to look
54 : * like a c-string.
55 : */
56 0 : *cp = 0;
57 0 : vec_set_len (s, cp - s);
58 0 : break;
59 : }
60 0 : cp++;
61 : }
62 0 : }
63 :
64 : hss_url_handler_rc_t
65 0 : handle_get_interface_stats (hss_url_handler_args_t *args)
66 : {
67 0 : u8 *s = 0, *stats = 0;
68 : uword *p;
69 0 : u32 *sw_if_indices = 0;
70 : vnet_hw_interface_t *hi;
71 : vnet_sw_interface_t *si;
72 0 : char *q = "\"";
73 : int i;
74 0 : int need_comma = 0;
75 : u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
76 : vnet_sw_interface_t * si, int json);
77 0 : vnet_main_t *vnm = vnet_get_main ();
78 0 : vnet_interface_main_t *im = &vnm->interface_main;
79 :
80 : /* Get stats for a single interface via http POST */
81 0 : if (args->reqtype == HTTP_REQ_POST)
82 : {
83 0 : trim_path_from_request (args->request, "interface_stats.json");
84 :
85 : /* Find the sw_if_index */
86 0 : p = hash_get (im->hw_interface_by_name, args->request);
87 0 : if (!p)
88 : {
89 0 : s = format (s, "{\"interface_stats\": {[\n");
90 0 : s = format (s, " \"name\": \"%s\",", args->request);
91 0 : s = format (s, " \"error\": \"%s\"", "UnknownInterface");
92 0 : s = format (s, "]}\n");
93 0 : goto out;
94 : }
95 :
96 0 : vec_add1 (sw_if_indices, p[0]);
97 : }
98 : else /* default, HTTP_BUILTIN_METHOD_GET */
99 : {
100 : /* *INDENT-OFF* */
101 0 : pool_foreach (hi, im->hw_interfaces)
102 : {
103 0 : vec_add1 (sw_if_indices, hi->sw_if_index);
104 : }
105 : /* *INDENT-ON* */
106 : }
107 :
108 0 : s = format (s, "{%sinterface_stats%s: [\n", q, q);
109 :
110 0 : for (i = 0; i < vec_len (sw_if_indices); i++)
111 : {
112 0 : si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
113 0 : if (need_comma)
114 0 : s = format (s, ",\n");
115 :
116 0 : need_comma = 1;
117 :
118 0 : s = format (s, "{%sname%s: %s%U%s, ", q, q, q,
119 0 : format_vnet_sw_if_index_name, vnm, sw_if_indices[i], q);
120 :
121 0 : stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
122 : 1 /* want json */ );
123 0 : if (vec_len (stats))
124 0 : s = format (s, "%v}", stats);
125 : else
126 0 : s = format (s, "%snone%s: %strue%s}", q, q, q, q);
127 0 : vec_reset_length (stats);
128 : }
129 :
130 0 : s = format (s, "]}\n");
131 :
132 0 : out:
133 0 : args->data = s;
134 0 : args->data_len = vec_len (s);
135 0 : args->free_vec_data = 1;
136 0 : vec_free (sw_if_indices);
137 0 : vec_free (stats);
138 0 : return HSS_URL_HANDLER_OK;
139 : }
140 :
141 : hss_url_handler_rc_t
142 0 : handle_get_interface_list (hss_url_handler_args_t *args)
143 : {
144 0 : u8 *s = 0;
145 : int i;
146 0 : vnet_main_t *vnm = vnet_get_main ();
147 0 : vnet_interface_main_t *im = &vnm->interface_main;
148 : vnet_hw_interface_t *hi;
149 0 : u32 *hw_if_indices = 0;
150 0 : int need_comma = 0;
151 :
152 : /* Construct vector of active hw_if_indexes ... */
153 : /* *INDENT-OFF* */
154 0 : pool_foreach (hi, im->hw_interfaces)
155 : {
156 : /* No point in mentioning "local0"... */
157 0 : if (hi - im->hw_interfaces)
158 0 : vec_add1 (hw_if_indices, hi - im->hw_interfaces);
159 : }
160 : /* *INDENT-ON* */
161 :
162 : /* Build answer */
163 0 : s = format (s, "{\"interface_list\": [\n");
164 0 : for (i = 0; i < vec_len (hw_if_indices); i++)
165 : {
166 0 : if (need_comma)
167 0 : s = format (s, ",\n");
168 0 : hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
169 0 : s = format (s, "\"%v\"", hi->name);
170 0 : need_comma = 1;
171 : }
172 0 : s = format (s, "]}\n");
173 0 : vec_free (hw_if_indices);
174 :
175 0 : args->data = s;
176 0 : args->data_len = vec_len (s);
177 0 : args->free_vec_data = 1;
178 0 : return HSS_URL_HANDLER_OK;
179 : }
180 :
181 : void
182 0 : builtinurl_handler_init (builtinurl_main_t * bm)
183 : {
184 :
185 0 : bm->register_handler (handle_get_version, "version.json", HTTP_REQ_GET);
186 0 : bm->register_handler (handle_get_interface_list, "interface_list.json",
187 : HTTP_REQ_GET);
188 0 : bm->register_handler (handle_get_interface_stats, "interface_stats.json",
189 : HTTP_REQ_GET);
190 0 : bm->register_handler (handle_get_interface_stats, "interface_stats.json",
191 : HTTP_REQ_POST);
192 0 : }
193 :
194 : /*
195 : * fd.io coding-style-patch-verification: ON
196 : *
197 : * Local Variables:
198 : * eval: (c-set-style "gnu")
199 : * End:
200 : */
|