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