FD.io VPP  v21.06-3-gbb25fbf28
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 */
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* */
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  {
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,
123 
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);
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;
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* */
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  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
im
vnet_interface_main_t * im
Definition: interface_output.c:395
HTTP_BUILTIN_METHOD_GET
@ HTTP_BUILTIN_METHOD_GET
Definition: http_static.h:76
handle_get_version
int handle_get_version(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:22
vnet_sw_interface_t
Definition: interface.h:868
http_session_t::data_offset
u32 data_offset
Current data send offset.
Definition: http_static.h:104
http_builtin_method_type_t
http_builtin_method_type_t
Definition: http_static.h:74
HTTP_BUILTIN_METHOD_POST
@ HTTP_BUILTIN_METHOD_POST
Definition: http_static.h:77
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
path
vl_api_fib_path_t path
Definition: mfib_types.api:44
vnet_interface_main_t
Definition: interface.h:989
http_static.h
builtinurl_handler_init
void builtinurl_handler_init(builtinurl_main_t *bm)
Definition: builtins.c:188
vec_delete
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:875
vnet_get_sw_interface
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:58
stats
vl_api_ikev2_sa_stats_t stats
Definition: ikev2_types.api:162
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
vnet_interface_main_t::hw_interfaces
vnet_hw_interface_t * hw_interfaces
Definition: interface.h:992
handle_get_interface_list
int handle_get_interface_list(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:146
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
uword
u64 uword
Definition: types.h:112
hash_get
#define hash_get(h, key)
Definition: hash.h:249
http_session_t::free_data
int free_data
Need to free data in detach_cache_entry.
Definition: http_static.h:106
builtinurl_main_t::register_handler
void(* register_handler)(void *, char *, int)
Definition: builtinurl.h:34
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
http_session_t::data
u8 * data
File data, a vector.
Definition: http_static.h:102
http_session_t
Application session.
Definition: http_server.c:40
http_session_t::cache_pool_index
u32 cache_pool_index
File cache pool index.
Definition: http_static.h:109
trim_path_from_request
void trim_path_from_request(u8 *s, char *path)
Definition: builtins.c:40
builtinurl_main_t
Definition: builtinurl.h:28
request
vhost_user_req_t request
Definition: vhost_user.h:123
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
format_vnet_sw_interface_cntrs
u8 * format_vnet_sw_interface_cntrs(u8 *s, vnet_interface_main_t *im, vnet_sw_interface_t *si, int json)
Definition: interface_format.c:311
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
si
vnet_sw_interface_t * si
Definition: interface_output.c:398
vnet_interface_main_t::hw_interface_by_name
uword * hw_interface_by_name
Definition: interface.h:1003
u8
unsigned char u8
Definition: types.h:56
builtinurl.h
vnet.h
sw_if_indices
u32 sw_if_indices[VLIB_FRAME_SIZE]
Definition: interface_output.c:1075
handle_get_interface_stats
int handle_get_interface_stats(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:67
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81