FD.io VPP  v19.08.3-2-gbabecb413
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);
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  stat_segment_access_start (&sa, sm);
182  if (!stat_segment_access_end (&sa, sm))
183  return 0.0;
184  return ep->value;
185 }
186 
187 double
189 {
191  return stat_segment_heartbeat_r (sm);
192 }
193 
194 static stat_segment_data_t
196 {
197  stat_segment_data_t result = { 0 };
198  int i;
199  vlib_counter_t **combined_c; /* Combined counter */
200  counter_t **simple_c; /* Simple counter */
201  uint64_t *offset_vector;
202 
203  assert (sm->shared_header);
204 
205  result.type = ep->type;
206  result.name = strdup (ep->name);
207  switch (ep->type)
208  {
210  result.scalar_value = ep->value;
211  break;
212 
214  if (ep->offset == 0)
215  return result;
216  simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
217  result.simple_counter_vec = vec_dup (simple_c);
218  offset_vector =
220  for (i = 0; i < vec_len (simple_c); i++)
221  {
222  counter_t *cb =
223  stat_segment_pointer (sm->shared_header, offset_vector[i]);
224  result.simple_counter_vec[i] = vec_dup (cb);
225  }
226  break;
227 
229  if (ep->offset == 0)
230  return result;
231  combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
232  result.combined_counter_vec = vec_dup (combined_c);
233  offset_vector =
235  for (i = 0; i < vec_len (combined_c); i++)
236  {
237  vlib_counter_t *cb =
238  stat_segment_pointer (sm->shared_header, offset_vector[i]);
239  result.combined_counter_vec[i] = vec_dup (cb);
240  }
241  break;
242 
244  /* Gather errors from all threads into a vector */
245  offset_vector = stat_segment_pointer (sm->shared_header,
247  vec_validate (result.error_vector, vec_len (offset_vector) - 1);
248  for (i = 0; i < vec_len (offset_vector); i++)
249  {
250  counter_t *cb =
251  stat_segment_pointer (sm->shared_header, offset_vector[i]);
252  result.error_vector[i] = cb[ep->index];
253  }
254  break;
255 
257  if (ep->offset == 0)
258  return result;
259  uint8_t **name_vector =
261  result.name_vector = vec_dup (name_vector);
262  offset_vector =
264  for (i = 0; i < vec_len (name_vector); i++)
265  {
266  if (offset_vector[i])
267  {
268  u8 *name =
269  stat_segment_pointer (sm->shared_header, offset_vector[i]);
270  result.name_vector[i] = vec_dup (name);
271  }
272  else
273  result.name_vector[i] = 0;
274  }
275  break;
276 
277  case STAT_DIR_TYPE_EMPTY:
278  break;
279 
280  default:
281  fprintf (stderr, "Unknown type: %d\n", ep->type);
282  }
283  return result;
284 }
285 
286 void
288 {
289  int i, j;
290  for (i = 0; i < vec_len (res); i++)
291  {
292  switch (res[i].type)
293  {
295  for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
296  vec_free (res[i].simple_counter_vec[j]);
297  vec_free (res[i].simple_counter_vec);
298  break;
300  for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
301  vec_free (res[i].combined_counter_vec[j]);
302  vec_free (res[i].combined_counter_vec);
303  break;
304  default:
305  ;
306  }
307  free (res[i].name);
308  }
309  vec_free (res);
310 }
311 
312 uint32_t *
313 stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
314 {
316 
317  uint32_t *dir = 0;
318  regex_t regex[vec_len (patterns)];
319 
320  int i, j;
321  for (i = 0; i < vec_len (patterns); i++)
322  {
323  int rv = regcomp (&regex[i], (const char *) patterns[i], 0);
324  if (rv)
325  {
326  fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
327  return dir;
328  }
329  }
330 
331  stat_segment_access_start (&sa, sm);
332 
334  for (j = 0; j < vec_len (counter_vec); j++)
335  {
336  for (i = 0; i < vec_len (patterns); i++)
337  {
338  int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
339  if (rv == 0)
340  {
341  vec_add1 (dir, j);
342  break;
343  }
344  }
345  if (vec_len (patterns) == 0)
346  vec_add1 (dir, j);
347  }
348 
349  for (i = 0; i < vec_len (patterns); i++)
350  regfree (&regex[i]);
351 
352  if (!stat_segment_access_end (&sa, sm))
353  {
354  /* Failed, clean up */
355  vec_free (dir);
356  return 0;
357 
358  }
359 
360  /* Update last version */
361  sm->current_epoch = sa.epoch;
362  return dir;
363 }
364 
365 uint32_t *
366 stat_segment_ls (uint8_t ** patterns)
367 {
369  return stat_segment_ls_r ((uint8_t **) patterns, sm);
370 }
371 
373 stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
374 {
375  int i;
377  stat_segment_data_t *res = 0;
379 
380  /* Has directory been update? */
381  if (sm->shared_header->epoch != sm->current_epoch)
382  return 0;
383 
384  stat_segment_access_start (&sa, sm);
385  for (i = 0; i < vec_len (stats); i++)
386  {
387  /* Collect counter */
388  ep = vec_elt_at_index (sm->directory_vector, stats[i]);
389  vec_add1 (res, copy_data (ep, sm));
390  }
391 
392  if (stat_segment_access_end (&sa, sm))
393  return res;
394 
395  fprintf (stderr, "Epoch changed while reading, invalid results\n");
396  // TODO increase counter
397  return 0;
398 }
399 
401 stat_segment_dump (uint32_t * stats)
402 {
404  return stat_segment_dump_r (stats, sm);
405 }
406 
407 /* Wrapper for accessing vectors from other languages */
408 int
410 {
411  return vec_len (vec);
412 }
413 
414 void
416 {
417  vec_free (vec);
418 }
419 
420 /* Create a vector from a string (or add to existing) */
421 uint8_t **
422 stat_segment_string_vector (uint8_t ** string_vector, const char *string)
423 {
424  uint8_t *name = 0;
425  size_t len = strlen (string);
426 
427  vec_validate_init_c_string (name, string, len);
428  vec_add1 (string_vector, name);
429  return string_vector;
430 }
431 
434 {
436  stat_segment_data_t *res = 0;
438 
439  stat_segment_access_start (&sa, sm);
440 
441  /* Collect counter */
442  ep = vec_elt_at_index (sm->directory_vector, index);
443  vec_add1 (res, copy_data (ep, sm));
444 
445  if (stat_segment_access_end (&sa, sm))
446  return res;
447  return 0;
448 }
449 
451 stat_segment_dump_entry (uint32_t index)
452 {
454  return stat_segment_dump_entry_r (index, sm);
455 }
456 
457 char *
459 {
463 
464  /* Has directory been update? */
465  if (sm->shared_header->epoch != sm->current_epoch)
466  return 0;
467  stat_segment_access_start (&sa, sm);
468  vec = get_stat_vector_r (sm);
469  ep = vec_elt_at_index (vec, index);
470  if (!stat_segment_access_end (&sa, sm))
471  return 0;
472  return strdup (ep->name);
473 }
474 
475 char *
477 {
479  return stat_segment_index_to_name_r (index, sm);
480 }
481 
482 uint64_t
484 {
485  ASSERT (sm->shared_header);
486  return sm->shared_header->version;
487 }
488 
489 uint64_t
491 {
493  return stat_segment_version_r (sm);
494 }
495 
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
static void stat_segment_access_start(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.h:90
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:451
uint64_t index
stat_segment_directory_entry_t * directory_vector
Definition: stat_client.h:50
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:458
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:287
counter_t ** simple_counter_vec
Definition: stat_client.h:40
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:522
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
int i
unsigned char u8
Definition: types.h:56
#define assert(x)
Definition: dlmalloc.c:30
stat_segment_shared_header_t * shared_header
Definition: stat_client.h:49
uint32_t * stat_segment_ls_r(uint8_t **patterns, stat_client_main_t *sm)
Definition: stat_client.c:313
uint64_t value
uint64_t current_epoch
Definition: stat_client.h:48
static void * stat_segment_pointer(void *start, uint64_t offset)
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:42
stat_segment_data_t * stat_segment_dump_entry_r(uint32_t index, stat_client_main_t *sm)
Definition: stat_client.c:433
double stat_segment_heartbeat_r(stat_client_main_t *sm)
Definition: stat_client.c:172
vl_api_fib_path_type_t type
Definition: fib_types.api:123
uword size
uint64_t offset
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
stat_directory_type_t type
Definition: stat_client.h:35
stat_segment_data_t * stat_segment_dump_r(uint32_t *stats, stat_client_main_t *sm)
Definition: stat_client.c:373
u8 name[64]
Definition: memclnt.api:152
void stat_segment_disconnect(void)
Definition: stat_client.c:165
u8 len
Definition: ip_types.api:90
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:422
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
char * stat_segment_index_to_name(uint32_t index)
Definition: stat_client.c:476
uint64_t offset_vector
#define vec_validate_init_c_string(V, S, L)
Make a vector containing a NULL terminated c-string.
Definition: vec.h:995
char name[128]
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:366
#define ASSERT(truth)
static stat_segment_directory_entry_t * get_stat_vector_r(stat_client_main_t *sm)
Definition: stat_client.c:85
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:401
static int recv_fd(int sock)
Definition: stat_client.c:53
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:41
static bool stat_segment_access_end(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.h:103
uint64_t stat_segment_version(void)
Definition: stat_client.c:490
stat_client_main_t * stat_client_get(void)
Definition: stat_client.c:38
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:415
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:409
static stat_segment_data_t copy_data(stat_segment_directory_entry_t *ep, stat_client_main_t *sm)
Definition: stat_client.c:195
uint64_t stat_segment_version_r(stat_client_main_t *sm)
Definition: stat_client.c:483
counter_t * error_vector
Definition: stat_client.h:39
double stat_segment_heartbeat(void)
Definition: stat_client.c:188
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