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