misc: move to new pool_foreach macros
[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 int
22 handle_get_version (http_builtin_method_type_t reqtype,
23                     u8 * request, http_session_t * hs)
24 {
25   u8 *s = 0;
26
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);
31
32   hs->data = s;
33   hs->data_offset = 0;
34   hs->cache_pool_index = ~0;
35   hs->free_data = 1;
36   return 0;
37 }
38
39 void
40 trim_path_from_request (u8 * s, char *path)
41 {
42   u8 *cp;
43   int trim_length = strlen (path) + 1 /* remove '?' */ ;
44
45   /* Get rid of the path and question-mark */
46   vec_delete (s, trim_length, 0);
47
48   /* Tail trim irrelevant browser info */
49   cp = s;
50   while ((cp - s) < vec_len (s))
51     {
52       if (*cp == ' ')
53         {
54           /*
55            * Makes request a vector which happens to look
56            * like a c-string.
57            */
58           *cp = 0;
59           _vec_len (s) = cp - s;
60           break;
61         }
62       cp++;
63     }
64 }
65
66 int
67 handle_get_interface_stats (http_builtin_method_type_t reqtype,
68                             u8 * request, http_session_t * hs)
69 {
70   u8 *s = 0, *stats = 0;
71   uword *p;
72   u32 *sw_if_indices = 0;
73   vnet_hw_interface_t *hi;
74   vnet_sw_interface_t *si;
75   char *q = "\"";
76   int i;
77   int need_comma = 0;
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;
82
83   /* Get stats for a single interface via http POST */
84   if (reqtype == HTTP_BUILTIN_METHOD_POST)
85     {
86       trim_path_from_request (request, "interface_stats.json");
87
88       /* Find the sw_if_index */
89       p = hash_get (im->hw_interface_by_name, request);
90       if (!p)
91         {
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");
96           goto out;
97         }
98
99       vec_add1 (sw_if_indices, p[0]);
100     }
101   else                          /* default, HTTP_BUILTIN_METHOD_GET */
102     {
103       /* *INDENT-OFF* */
104       pool_foreach (hi, im->hw_interfaces)
105        {
106         vec_add1 (sw_if_indices, hi->sw_if_index);
107       }
108       /* *INDENT-ON* */
109     }
110
111   s = format (s, "{%sinterface_stats%s: [\n", q, q);
112
113   for (i = 0; i < vec_len (sw_if_indices); i++)
114     {
115       si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
116       if (need_comma)
117         s = format (s, ",\n");
118
119       need_comma = 1;
120
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);
123
124       stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
125                                               1 /* want json */ );
126       if (vec_len (stats))
127         s = format (s, "%v}", stats);
128       else
129         s = format (s, "%snone%s: %strue%s}", q, q, q, q);
130       vec_reset_length (stats);
131     }
132
133   s = format (s, "]}\n");
134
135 out:
136   hs->data = s;
137   hs->data_offset = 0;
138   hs->cache_pool_index = ~0;
139   hs->free_data = 1;
140   vec_free (sw_if_indices);
141   vec_free (stats);
142   return 0;
143 }
144
145 int
146 handle_get_interface_list (http_builtin_method_type_t reqtype,
147                            u8 * request, http_session_t * hs)
148 {
149   u8 *s = 0;
150   int i;
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;
155   int need_comma = 0;
156
157   /* Construct vector of active hw_if_indexes ... */
158   /* *INDENT-OFF* */
159   pool_foreach (hi, im->hw_interfaces)
160    {
161     /* No point in mentioning "local0"... */
162     if (hi - im->hw_interfaces)
163       vec_add1 (hw_if_indices, hi - im->hw_interfaces);
164   }
165   /* *INDENT-ON* */
166
167   /* Build answer */
168   s = format (s, "{\"interface_list\": [\n");
169   for (i = 0; i < vec_len (hw_if_indices); i++)
170     {
171       if (need_comma)
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);
175       need_comma = 1;
176     }
177   s = format (s, "]}\n");
178   vec_free (hw_if_indices);
179
180   hs->data = s;
181   hs->data_offset = 0;
182   hs->cache_pool_index = ~0;
183   hs->free_data = 1;
184   return 0;
185 }
186
187 void
188 builtinurl_handler_init (builtinurl_main_t * bm)
189 {
190
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);
199 }
200
201 /*
202  * fd.io coding-style-patch-verification: ON
203  *
204  * Local Variables:
205  * eval: (c-set-style "gnu")
206  * End:
207  */