FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
builtins.c
Go to the documentation of this file.
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>
19 #include <vpp/app/version.h>
20 
21 int
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
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
68  u8 * request, http_session_t * hs)
69 {
70  u8 *s = 0, *stats = 0;
71  uword *p;
72  u32 *sw_if_indices = 0;
75  char *q = "\"";
76  int i;
77  int need_comma = 0;
79  vnet_sw_interface_t * si, int json);
80  vnet_main_t *vnm = vnet_get_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
147  u8 * request, http_session_t * hs)
148 {
149  u8 *s = 0;
150  int i;
151  vnet_main_t *vnm = vnet_get_main ();
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
189 {
190 
191  bm->register_handler (handle_get_version, "version.json",
193  bm->register_handler (handle_get_interface_list, "interface_list.json",
196  "interface_stats.json", HTTP_BUILTIN_METHOD_GET);
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  */
Static http server definitions.
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
u8 * format_vnet_sw_interface_cntrs(u8 *s, vnet_interface_main_t *im, vnet_sw_interface_t *si, int json)
u32 data_offset
Current data send offset.
Definition: http_static.h:104
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:590
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_fib_path_t path
Definition: mfib_types.api:34
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
int handle_get_version(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:22
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
unsigned int u32
Definition: types.h:88
int free_data
Need to free data in detach_cache_entry.
Definition: http_static.h:106
uword * hw_interface_by_name
Definition: interface.h:847
vnet_hw_interface_t * hw_interfaces
Definition: interface.h:844
#define hash_get(h, key)
Definition: hash.h:249
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
u8 * data
File data, a vector.
Definition: http_static.h:102
int handle_get_interface_stats(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:67
Application session.
Definition: http_server.c:40
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
void builtinurl_handler_init(builtinurl_main_t *bm)
Definition: builtins.c:188
u32 cache_pool_index
File cache pool index.
Definition: http_static.h:109
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:852
void(* register_handler)(void *, char *, int)
Definition: builtinurl.h:34
void trim_path_from_request(u8 *s, char *path)
Definition: builtins.c:40
http_builtin_method_type_t
Definition: http_static.h:74
vl_api_ip4_address_t hi
Definition: arp.api:37
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
int handle_get_interface_list(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:146
u64 uword
Definition: types.h:112
vhost_user_req_t request
Definition: vhost_user.h:247
u8 si
Definition: lisp_types.api:47