FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 "stat_client.h"
32 #include <stdatomic.h>
33 #include <vpp/stats/stat_segment.h>
34 
36 {
37  uint64_t current_epoch;
40  ssize_t memory_size;
41 };
42 
44 
47 {
49  sm = (stat_client_main_t *) malloc (sizeof (stat_client_main_t));
50  clib_memset (sm, 0, sizeof (stat_client_main_t));
51  return sm;
52 }
53 
54 void
56 {
57  free (sm);
58 }
59 
60 static int
61 recv_fd (int sock)
62 {
63  struct msghdr msg = { 0 };
64  struct cmsghdr *cmsg;
65  int fd = -1;
66  char iobuf[1];
67  struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
68  union
69  {
70  char buf[CMSG_SPACE (sizeof (fd))];
71  struct cmsghdr align;
72  } u;
73  msg.msg_iov = &io;
74  msg.msg_iovlen = 1;
75  msg.msg_control = u.buf;
76  msg.msg_controllen = sizeof (u.buf);
77 
78  ssize_t size;
79  if ((size = recvmsg (sock, &msg, 0)) < 0)
80  {
81  perror ("recvmsg failed");
82  return -1;
83  }
84  cmsg = CMSG_FIRSTHDR (&msg);
85  if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
86  {
87  memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
88  }
89  return fd;
90 }
91 
94 {
95  ASSERT (sm->shared_header);
98 }
99 
102 {
104  return get_stat_vector_r (sm);
105 }
106 
107 int
108 stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm)
109 {
110  int mfd = -1;
111  int sock;
112 
113  clib_memset (sm, 0, sizeof (*sm));
114  if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
115  {
116  perror ("Stat client couldn't open socket");
117  return -1;
118  }
119 
120  struct sockaddr_un un = { 0 };
121  un.sun_family = AF_UNIX;
122  strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
123  if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
124  0)
125  {
126  close (sock);
127  return -2;
128  }
129 
130  if ((mfd = recv_fd (sock)) < 0)
131  {
132  close (sock);
133  fprintf (stderr, "Receiving file descriptor failed\n");
134  return -3;
135  }
136  close (sock);
137 
138  /* mmap shared memory segment. */
139  void *memaddr;
140  struct stat st = { 0 };
141 
142  if (fstat (mfd, &st) == -1)
143  {
144  close (mfd);
145  perror ("mmap fstat failed");
146  return -4;
147  }
148  if ((memaddr =
149  mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
150  {
151  close (mfd);
152  perror ("mmap map failed");
153  return -5;
154  }
155 
156  close (mfd);
157  sm->memory_size = st.st_size;
158  sm->shared_header = memaddr;
159  sm->directory_vector =
161 
162  return 0;
163 }
164 
165 int
166 stat_segment_connect (const char *socket_name)
167 {
169  return stat_segment_connect_r (socket_name, sm);
170 }
171 
172 void
174 {
175  munmap (sm->shared_header, sm->memory_size);
176  return;
177 }
178 
179 void
181 {
183  return stat_segment_disconnect_r (sm);
184 }
185 
186 double
188 {
190  double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset);
191  return *hb;
192 }
193 
194 double
196 {
198  return stat_segment_heartbeat_r (sm);
199 }
200 
201 static stat_segment_data_t
203 {
204  stat_segment_data_t result = { 0 };
205  int i;
206  vlib_counter_t **combined_c; /* Combined counter */
207  counter_t **simple_c; /* Simple counter */
208  counter_t *error_base;
209  uint64_t *offset_vector;
210 
211  assert (sm->shared_header);
212 
213  result.type = ep->type;
214  result.name = strdup (ep->name);
215  switch (ep->type)
216  {
218  result.scalar_value = ep->value;
219  break;
220 
222  if (ep->offset == 0)
223  return result;
224  simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
225  result.simple_counter_vec = vec_dup (simple_c);
226  offset_vector =
228  for (i = 0; i < vec_len (simple_c); i++)
229  {
230  counter_t *cb =
231  stat_segment_pointer (sm->shared_header, offset_vector[i]);
232  result.simple_counter_vec[i] = vec_dup (cb);
233  }
234  break;
235 
237  if (ep->offset == 0)
238  return result;
239  combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
240  result.combined_counter_vec = vec_dup (combined_c);
241  offset_vector =
243  for (i = 0; i < vec_len (combined_c); i++)
244  {
245  vlib_counter_t *cb =
246  stat_segment_pointer (sm->shared_header, offset_vector[i]);
247  result.combined_counter_vec[i] = vec_dup (cb);
248  }
249  break;
250 
252  error_base =
255  result.error_value = error_base[ep->index];
256  break;
257 
259  if (ep->offset == 0)
260  return result;
261  uint8_t **name_vector =
263  result.name_vector = vec_dup (name_vector);
264  offset_vector =
266  for (i = 0; i < vec_len (name_vector); i++)
267  {
268  if (offset_vector[i])
269  {
270  u8 *name =
271  stat_segment_pointer (sm->shared_header, offset_vector[i]);
272  result.name_vector[i] = vec_dup (name);
273  }
274  else
275  result.name_vector[i] = 0;
276  }
277  break;
278 
279  default:
280  fprintf (stderr, "Unknown type: %d\n", ep->type);
281  }
282  return result;
283 }
284 
285 void
287 {
288  int i, j;
289  for (i = 0; i < vec_len (res); i++)
290  {
291  switch (res[i].type)
292  {
294  for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
295  vec_free (res[i].simple_counter_vec[j]);
296  vec_free (res[i].simple_counter_vec);
297  break;
299  for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
300  vec_free (res[i].combined_counter_vec[j]);
301  vec_free (res[i].combined_counter_vec);
302  break;
303  default:
304  ;
305  }
306  free (res[i].name);
307  }
308  vec_free (res);
309 }
310 
311 
312 typedef struct
313 {
314  uint64_t epoch;
316 
317 static void
319  stat_client_main_t * sm)
320 {
322  sa->epoch = shared_header->epoch;
323  while (shared_header->in_progress != 0)
324  ;
327 }
328 
329 static bool
331 {
333 
334  if (shared_header->epoch != sa->epoch || shared_header->in_progress)
335  return false;
336  return true;
337 }
338 
339 uint32_t *
340 stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
341 {
343 
344  uint32_t *dir = 0;
345  regex_t regex[vec_len (patterns)];
346 
347  int i, j;
348  for (i = 0; i < vec_len (patterns); i++)
349  {
350  int rv = regcomp (&regex[i], (const char *) patterns[i], 0);
351  if (rv)
352  {
353  fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
354  return dir;
355  }
356  }
357 
358  stat_segment_access_start (&sa, sm);
359 
361  for (j = 0; j < vec_len (counter_vec); j++)
362  {
363  for (i = 0; i < vec_len (patterns); i++)
364  {
365  int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
366  if (rv == 0)
367  {
368  vec_add1 (dir, j);
369  break;
370  }
371  }
372  if (vec_len (patterns) == 0)
373  vec_add1 (dir, j);
374  }
375 
376  for (i = 0; i < vec_len (patterns); i++)
377  regfree (&regex[i]);
378 
379  if (!stat_segment_access_end (&sa, sm))
380  {
381  /* Failed, clean up */
382  vec_free (dir);
383  return 0;
384 
385  }
386 
387  /* Update last version */
388  sm->current_epoch = sa.epoch;
389  return dir;
390 }
391 
392 uint32_t *
393 stat_segment_ls (uint8_t ** patterns)
394 {
396  return stat_segment_ls_r ((uint8_t **) patterns, sm);
397 }
398 
400 stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
401 {
402  int i;
404  stat_segment_data_t *res = 0;
406 
407  /* Has directory been update? */
408  if (sm->shared_header->epoch != sm->current_epoch)
409  return 0;
410 
411  stat_segment_access_start (&sa, sm);
412  for (i = 0; i < vec_len (stats); i++)
413  {
414  /* Collect counter */
415  ep = vec_elt_at_index (sm->directory_vector, stats[i]);
416  vec_add1 (res, copy_data (ep, sm));
417  }
418 
419  if (stat_segment_access_end (&sa, sm))
420  return res;
421 
422  fprintf (stderr, "Epoch changed while reading, invalid results\n");
423  // TODO increase counter
424  return 0;
425 }
426 
428 stat_segment_dump (uint32_t * stats)
429 {
431  return stat_segment_dump_r (stats, sm);
432 }
433 
434 /* Wrapper for accessing vectors from other languages */
435 int
437 {
438  return vec_len (vec);
439 }
440 
441 void
443 {
444  vec_free (vec);
445 }
446 
447 /* Create a vector from a string (or add to existing) */
448 uint8_t **
449 stat_segment_string_vector (uint8_t ** string_vector, const char *string)
450 {
451  uint8_t *name = 0;
452  size_t len = strlen (string);
453 
454  vec_validate_init_c_string (name, string, len);
455  vec_add1 (string_vector, name);
456  return string_vector;
457 }
458 
461 {
463  stat_segment_data_t *res = 0;
465 
466  stat_segment_access_start (&sa, sm);
467 
468  /* Collect counter */
469  ep = vec_elt_at_index (sm->directory_vector, index);
470  vec_add1 (res, copy_data (ep, sm));
471 
472  if (stat_segment_access_end (&sa, sm))
473  return res;
474  return 0;
475 }
476 
478 stat_segment_dump_entry (uint32_t index)
479 {
481  return stat_segment_dump_entry_r (index, sm);
482 }
483 
484 char *
486 {
487  char *name;
490  ep = vec_elt_at_index (counter_vec, index);
491  name = strdup (ep->name);
492  return name;
493 }
494 
495 /*
496  * fd.io coding-style-patch-verification: ON
497  *
498  * Local Variables:
499  * eval: (c-set-style "gnu")
500  * End:
501  */
static stat_segment_directory_entry_t * get_stat_vector(void)
Definition: stat_client.c:101
int stat_segment_connect_r(const char *socket_name, stat_client_main_t *sm)
Definition: stat_client.c:108
Definition: stat_segment.h:58
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:166
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:478
uint64_t index
Definition: stat_segment.h:63
stat_segment_directory_entry_t * directory_vector
Definition: stat_client.c:39
void stat_client_free(stat_client_main_t *sm)
Definition: stat_client.c:55
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:286
counter_t ** simple_counter_vec
Definition: stat_client.h:50
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
stat_client_main_t stat_client_main
Definition: stat_client.c:43
#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
atomic_int_fast64_t in_progress
Definition: stat_segment.h:79
#define assert(x)
Definition: dlmalloc.c:30
stat_segment_shared_header_t * shared_header
Definition: stat_client.c:38
uint32_t * stat_segment_ls_r(uint8_t **patterns, stat_client_main_t *sm)
Definition: stat_client.c:340
uint64_t value
Definition: stat_segment.h:64
uint64_t current_epoch
Definition: stat_client.c:37
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:52
stat_segment_data_t * stat_segment_dump_entry_r(uint32_t index, stat_client_main_t *sm)
Definition: stat_client.c:460
double stat_segment_heartbeat_r(stat_client_main_t *sm)
Definition: stat_client.c:187
uword size
static void * stat_segment_pointer(void *start, uint64_t offset)
Definition: stat_segment.h:92
uint64_t offset
Definition: stat_segment.h:62
#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:45
stat_segment_data_t * stat_segment_dump_r(uint32_t *stats, stat_client_main_t *sm)
Definition: stat_client.c:400
u8 name[64]
Definition: memclnt.api:152
void stat_segment_disconnect(void)
Definition: stat_client.c:180
u8 len
Definition: ip_types.api:49
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:449
#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:485
uint64_t offset_vector
Definition: stat_segment.h:66
#define vec_validate_init_c_string(V, S, L)
Make a vector containing a NULL terminated c-string.
Definition: vec.h:991
char name[128]
Definition: stat_segment.h:67
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:393
#define ASSERT(truth)
static stat_segment_directory_entry_t * get_stat_vector_r(stat_client_main_t *sm)
Definition: stat_client.c:93
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:428
static bool stat_segment_access_end(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.c:330
atomic_int_fast64_t epoch
Definition: stat_segment.h:78
static int recv_fd(int sock)
Definition: stat_client.c:61
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:51
stat_client_main_t * stat_client_get(void)
Definition: stat_client.c:46
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:442
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uint64_t error_value
Definition: stat_client.h:49
static void stat_segment_access_start(stat_segment_access_t *sa, stat_client_main_t *sm)
Definition: stat_client.c:318
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:436
static stat_segment_data_t copy_data(stat_segment_directory_entry_t *ep, stat_client_main_t *sm)
Definition: stat_client.c:202
atomic_int_fast64_t error_offset
Definition: stat_segment.h:81
double stat_segment_heartbeat(void)
Definition: stat_client.c:195
void stat_segment_disconnect_r(stat_client_main_t *sm)
Definition: stat_client.c:173
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
atomic_int_fast64_t directory_offset
Definition: stat_segment.h:80
stat_directory_type_t type
Definition: stat_segment.h:60