FD.io VPP  v21.01.1
Vector Packet Processing
stat_client.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * stat_client.c - Library for access to VPP statistics segment
4  *
5  * Copyright (c) 2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <stdio.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <stdbool.h>
26 #include <sys/stat.h>
27 #include <regex.h>
28 #include <assert.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/lock.h>
31 #include <stdatomic.h>
32 #include <vpp/stats/stat_segment.h>
34 
36 
39 {
41  sm = (stat_client_main_t *) malloc (sizeof (stat_client_main_t));
42  clib_memset (sm, 0, sizeof (stat_client_main_t));
43  return sm;
44 }
45 
46 void
48 {
49  free (sm);
50 }
51 
52 static int
53 recv_fd (int sock)
54 {
55  struct msghdr msg = { 0 };
56  struct cmsghdr *cmsg;
57  int fd = -1;
58  char iobuf[1];
59  struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
60  union
61  {
62  char buf[CMSG_SPACE (sizeof (fd))];
63  struct cmsghdr align;
64  } u;
65  msg.msg_iov = &io;
66  msg.msg_iovlen = 1;
67  msg.msg_control = u.buf;
68  msg.msg_controllen = sizeof (u.buf);
69 
70  ssize_t size;
71  if ((size = recvmsg (sock, &msg, 0)) < 0)
72  {
73  perror ("recvmsg failed");
74  return -1;
75  }
76  cmsg = CMSG_FIRSTHDR (&msg);
77  if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
78  {
79  memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
80  }
81  return fd;
82 }
83 
86 {
87  ASSERT (sm->shared_header);
88  return stat_segment_adjust (sm,
89  (void *) sm->shared_header->directory_vector);
90 }
91 
92 int
93 stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm)
94 {
95  int mfd = -1;
96  int sock;
97 
98  clib_memset (sm, 0, sizeof (*sm));
99  if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
100  {
101  perror ("Stat client couldn't open socket");
102  return -1;
103  }
104 
105  struct sockaddr_un un = { 0 };
106  un.sun_family = AF_UNIX;
107  strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
108  if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
109  0)
110  {
111  close (sock);
112  return -2;
113  }
114 
115  if ((mfd = recv_fd (sock)) < 0)
116  {
117  close (sock);
118  fprintf (stderr, "Receiving file descriptor failed\n");
119  return -3;
120  }
121  close (sock);
122 
123  /* mmap shared memory segment. */
124  void *memaddr;
125  struct stat st = { 0 };
126 
127  if (fstat (mfd, &st) == -1)
128  {
129  close (mfd);
130  perror ("mmap fstat failed");
131  return -4;
132  }
133  if ((memaddr =
134  mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
135  {
136  close (mfd);
137  perror ("mmap map failed");
138  return -5;
139  }
140 
141  close (mfd);
142  sm->memory_size = st.st_size;
143  sm->shared_header = memaddr;
144  sm->directory_vector =
146 
147  return 0;
148 }
149 
150 int
151 stat_segment_connect (const char *socket_name)
152 {
154  return stat_segment_connect_r (socket_name, sm);
155 }
156 
157 void
159 {
160  munmap (sm->shared_header, sm->memory_size);
161  return;
162 }
163 
164 void
166 {
168  return stat_segment_disconnect_r (sm);
169 }
170 
171 double
173 {
176 
177  /* Has directory been update? */
178  if (sm->shared_header->epoch != sm->current_epoch)
179  return 0;
180  if (stat_segment_access_start (&sa, sm))
181  return 0;
183  if (!stat_segment_access_end (&sa, sm))
184  return 0.0;
185  return ep->value;
186 }
187 
188 double
190 {
192  return stat_segment_heartbeat_r (sm);
193 }
194 
195 #define stat_vec_dup(S,V) \
196  ({ \
197  __typeof__ ((V)[0]) * _v(v) = 0; \
198  if (V && ((void *)V > (void *)S->shared_header) && \
199  (((void*)V + vec_bytes(V)) < \
200  ((void *)S->shared_header + S->memory_size))) \
201  _v(v) = vec_dup(V); \
202  _v(v); \
203 })
204 
205 static stat_segment_data_t
207 {
208  stat_segment_data_t result = { 0 };
209  int i;
210  vlib_counter_t **combined_c; /* Combined counter */
211  counter_t **simple_c; /* Simple counter */
212  uint64_t *error_vector;
213 
214  assert (sm->shared_header);
215 
216  result.type = ep->type;
217  result.name = strdup (ep->name);
218  switch (ep->type)
219  {
221  result.scalar_value = ep->value;
222  break;
223 
225  simple_c = stat_segment_adjust (sm, ep->data);
226  result.simple_counter_vec = stat_vec_dup (sm, simple_c);
227  for (i = 0; i < vec_len (simple_c); i++)
228  {
229  counter_t *cb = stat_segment_adjust (sm, simple_c[i]);
230  result.simple_counter_vec[i] = stat_vec_dup (sm, cb);
231  }
232  break;
233 
235  combined_c = stat_segment_adjust (sm, ep->data);
236  result.combined_counter_vec = stat_vec_dup (sm, combined_c);
237  for (i = 0; i < vec_len (combined_c); i++)
238  {
239  vlib_counter_t *cb = stat_segment_adjust (sm, combined_c[i]);
240  result.combined_counter_vec[i] = stat_vec_dup (sm, cb);
241  }
242  break;
243 
245  /* Gather errors from all threads into a vector */
246  error_vector =
247  stat_segment_adjust (sm, (void *) sm->shared_header->error_vector);
248  vec_validate (result.error_vector, vec_len (error_vector) - 1);
249  for (i = 0; i < vec_len (error_vector); i++)
250  {
251  counter_t *cb = stat_segment_adjust (sm, (void *) error_vector[i]);
252  result.error_vector[i] = cb[ep->index];
253  }
254  break;
255 
257  {
258  uint8_t **name_vector = stat_segment_adjust (sm, ep->data);
259  result.name_vector = stat_vec_dup (sm, name_vector);
260  for (i = 0; i < vec_len (name_vector); i++)
261  {
262  u8 *name = stat_segment_adjust (sm, name_vector[i]);
263  result.name_vector[i] = stat_vec_dup (sm, name);
264  }
265  }
266  break;
267 
268  case STAT_DIR_TYPE_EMPTY:
269  break;
270 
271  default:
272  fprintf (stderr, "Unknown type: %d\n", ep->type);
273  }
274  return result;
275 }
276 
277 void
279 {
280  int i, j;
281  for (i = 0; i < vec_len (res); i++)
282  {
283  switch (res[i].type)
284  {
286  for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
287  vec_free (res[i].simple_counter_vec[j]);
288  vec_free (res[i].simple_counter_vec);
289  break;
291  for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
292  vec_free (res[i].combined_counter_vec[j]);
293  vec_free (res[i].combined_counter_vec);
294  break;
296  for (j = 0; j < vec_len (res[i].name_vector); j++)
297  vec_free (res[i].name_vector[j]);
298  vec_free (res[i].name_vector);
299  break;
301  vec_free (res[i].error_vector);
302  break;
304  break;
305  default:
306  assert (0);
307  }
308  free (res[i].name);
309  }
310  vec_free (res);
311 }
312 
313 uint32_t *
314 stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
315 {
317 
318  uint32_t *dir = 0;
319  regex_t regex[vec_len (patterns)];
320 
321  int i, j;
322  for (i = 0; i < vec_len (patterns); i++)
323  {
324  int rv = regcomp (&regex[i], (const char *) patterns[i], 0);
325  if (rv)
326  {
327  fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
328  return dir;
329  }
330  }
331 
332  if (stat_segment_access_start (&sa, sm))
333  return 0;
334 
336  for (j = 0; j < vec_len (counter_vec); j++)
337  {
338  for (i = 0; i < vec_len (patterns); i++)
339  {
340  int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
341  if (rv == 0)
342  {
343  vec_add1 (dir, j);
344  break;
345  }
346  }
347  if (vec_len (patterns) == 0)
348  vec_add1 (dir, j);
349  }
350 
351  for (i = 0; i < vec_len (patterns); i++)
352  regfree (&regex[i]);
353 
354  if (!stat_segment_access_end (&sa, sm))
355  {
356  /* Failed, clean up */
357  vec_free (dir);
358  return 0;
359 
360  }
361 
362  /* Update last version */
363  sm->current_epoch = sa.epoch;
364  return dir;
365 }
366 
367 uint32_t *
368 stat_segment_ls (uint8_t ** patterns)
369 {
371  return stat_segment_ls_r ((uint8_t **) patterns, sm);
372 }
373 
375 stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
376 {
377  int i;
379  stat_segment_data_t *res = 0;
381 
382  /* Has directory been update? */
383  if (sm->shared_header->epoch != sm->current_epoch)
384  return 0;
385 
386  if (stat_segment_access_start (&sa, sm))
387  return 0;
388 
389  for (i = 0; i < vec_len (stats); i++)
390  {
391  /* Collect counter */
392  ep = vec_elt_at_index (sm->directory_vector, stats[i]);
393  vec_add1 (res, copy_data (ep, sm));
394  }
395 
396  if (stat_segment_access_end (&sa, sm))
397  return res;
398 
399  fprintf (stderr, "Epoch changed while reading, invalid results\n");
400  // TODO increase counter
401  return 0;
402 }
403 
405 stat_segment_dump (uint32_t * stats)
406 {
408  return stat_segment_dump_r (stats, sm);
409 }
410 
411 /* Wrapper for accessing vectors from other languages */
412 int
414 {
415  return vec_len (vec);
416 }
417 
418 void
420 {
421  vec_free (vec);
422 }
423 
424 /* Create a vector from a string (or add to existing) */
425 uint8_t **
426 stat_segment_string_vector (uint8_t ** string_vector, const char *string)
427 {
428  uint8_t *name = 0;
429  size_t len = strlen (string);
430 
431  vec_validate_init_c_string (name, string, len);
432  vec_add1 (string_vector, name);
433  return string_vector;
434 }
435 
438 {
440  stat_segment_data_t *res = 0;
442 
443  if (stat_segment_access_start (&sa, sm))
444  return 0;
445 
446  /* Collect counter */
447  ep = vec_elt_at_index (sm->directory_vector, index);
448  vec_add1 (res, copy_data (ep, sm));
449 
450  if (stat_segment_access_end (&sa, sm))
451  return res;
452  return 0;
453 }
454 
457 {
459  return stat_segment_dump_entry_r (index, sm);
460 }
461 
462 char *
464 {
468 
469  /* Has directory been update? */
470  if (sm->shared_header->epoch != sm->current_epoch)
471  return 0;
472  if (stat_segment_access_start (&sa, sm))
473  return 0;
474  vec = get_stat_vector_r (sm);
475  ep = vec_elt_at_index (vec, index);
476  if (!stat_segment_access_end (&sa, sm))
477  return 0;
478  return strdup (ep->name);
479 }
480 
481 char *
483 {
485  return stat_segment_index_to_name_r (index, sm);
486 }
487 
488 uint64_t
490 {
491  ASSERT (sm->shared_header);
492  return sm->shared_header->version;
493 }
494 
495 uint64_t
497 {
499  return stat_segment_version_r (sm);
500 }
501 
502 /*
503  * fd.io coding-style-patch-verification: ON
504  *
505  * Local Variables:
506  * eval: (c-set-style "gnu")
507  * End:
508  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
int stat_segment_connect_r(const char *socket_name, stat_client_main_t *sm)
Definition: stat_client.c:93
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:151
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:456
#define stat_vec_dup(S, V)
Definition: stat_client.c:195
uint64_t index
stat_segment_directory_entry_t * directory_vector
Definition: stat_client.h:53
void stat_client_free(stat_client_main_t *sm)
Definition: stat_client.c:47
char * stat_segment_index_to_name_r(uint32_t index, stat_client_main_t *sm)
Definition: stat_client.c:463
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:278
counter_t ** simple_counter_vec
Definition: stat_client.h:43
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
stat_client_main_t stat_client_main
Definition: stat_client.c:35
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
void * data
unsigned char u8
Definition: types.h:56
#define assert(x)
Definition: dlmalloc.c:31
stat_segment_shared_header_t * shared_header
Definition: stat_client.h:52
uint32_t * stat_segment_ls_r(uint8_t **patterns, stat_client_main_t *sm)
Definition: stat_client.c:314
uint64_t value
uint64_t current_epoch
Definition: stat_client.h:51
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
uint8_t ** name_vector
Definition: stat_client.h:45
stat_segment_data_t * stat_segment_dump_entry_r(uint32_t index, stat_client_main_t *sm)
Definition: stat_client.c:437
double stat_segment_heartbeat_r(stat_client_main_t *sm)
Definition: stat_client.c:172
static void * stat_segment_adjust(stat_client_main_t *sm, void *data)
Definition: stat_client.h:102
vl_api_fib_path_type_t type
Definition: fib_types.api:123
u32 size
Definition: vhost_user.h:106
stat_directory_type_t type
Definition: stat_client.h:38
stat_segment_data_t * stat_segment_dump_r(uint32_t *stats, stat_client_main_t *sm)
Definition: stat_client.c:375
void * malloc(size_t size)
Definition: mem.c:33
void stat_segment_disconnect(void)
Definition: stat_client.c:165
u8 len
Definition: ip_types.api:103
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:426
volatile uint64_t ** error_vector
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
char * stat_segment_index_to_name(uint32_t index)
Definition: stat_client.c:482
string name[64]
Definition: ip.api:44
#define vec_validate_init_c_string(V, S, L)
Make a vector containing a NULL terminated c-string.
Definition: vec.h:1067
char name[128]
void free(void *p)
Definition: mem.c:42
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:368
#define ASSERT(truth)
static stat_segment_directory_entry_t * get_stat_vector_r(stat_client_main_t *sm)
Definition: stat_client.c:85
volatile stat_segment_directory_entry_t * directory_vector
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:405
static int recv_fd(int sock)
Definition: stat_client.c:53
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:44
static bool stat_segment_access_end(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.h:165
uint64_t stat_segment_version(void)
Definition: stat_client.c:496
stat_client_main_t * stat_client_get(void)
Definition: stat_client.c:38
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:419
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 index
Definition: flow_types.api:221
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:413
static stat_segment_data_t copy_data(stat_segment_directory_entry_t *ep, stat_client_main_t *sm)
Definition: stat_client.c:206
uint64_t stat_segment_version_r(stat_client_main_t *sm)
Definition: stat_client.c:489
static int stat_segment_access_start(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.h:115
counter_t * error_vector
Definition: stat_client.h:42
double stat_segment_heartbeat(void)
Definition: stat_client.c:189
void stat_segment_disconnect_r(stat_client_main_t *sm)
Definition: stat_client.c:158
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
stat_directory_type_t type