2 *------------------------------------------------------------------
3 * stat_client.c - Library for access to VPP statistics segment
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
22 #include <sys/types.h>
23 #include <sys/socket.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/lock.h>
31 #include <stdatomic.h>
32 #include <vlib/vlib.h>
33 #include <vlib/stats/stats.h>
34 #include <vpp-api/client/stat_client.h>
36 stat_client_main_t stat_client_main;
39 stat_client_get (void)
41 stat_client_main_t *sm;
42 sm = (stat_client_main_t *) malloc (sizeof (stat_client_main_t));
43 clib_memset (sm, 0, sizeof (stat_client_main_t));
48 stat_client_free (stat_client_main_t * sm)
56 struct msghdr msg = { 0 };
60 struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
63 char buf[CMSG_SPACE (sizeof (fd))];
68 msg.msg_control = u.buf;
69 msg.msg_controllen = sizeof (u.buf);
72 if ((size = recvmsg (sock, &msg, 0)) < 0)
74 perror ("recvmsg failed");
77 cmsg = CMSG_FIRSTHDR (&msg);
78 if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
80 memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
85 static vlib_stats_entry_t *
86 get_stat_vector_r (stat_client_main_t *sm)
88 ASSERT (sm->shared_header);
89 return stat_segment_adjust (sm,
90 (void *) sm->shared_header->directory_vector);
94 stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm)
99 clib_memset (sm, 0, sizeof (*sm));
100 if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
102 perror ("Stat client couldn't open socket");
106 struct sockaddr_un un = { 0 };
107 un.sun_family = AF_UNIX;
108 strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
109 if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
116 if ((mfd = recv_fd (sock)) < 0)
119 fprintf (stderr, "Receiving file descriptor failed\n");
124 /* mmap shared memory segment. */
126 struct stat st = { 0 };
128 if (fstat (mfd, &st) == -1)
131 perror ("mmap fstat failed");
135 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
138 perror ("mmap map failed");
143 sm->memory_size = st.st_size;
144 sm->shared_header = memaddr;
145 sm->directory_vector =
146 stat_segment_adjust (sm, (void *) sm->shared_header->directory_vector);
152 stat_segment_connect (const char *socket_name)
154 stat_client_main_t *sm = &stat_client_main;
155 return stat_segment_connect_r (socket_name, sm);
159 stat_segment_disconnect_r (stat_client_main_t * sm)
161 munmap (sm->shared_header, sm->memory_size);
166 stat_segment_disconnect (void)
168 stat_client_main_t *sm = &stat_client_main;
169 return stat_segment_disconnect_r (sm);
173 stat_segment_heartbeat_r (stat_client_main_t * sm)
175 stat_segment_access_t sa;
176 vlib_stats_entry_t *ep;
178 /* Has directory been updated? */
179 if (sm->shared_header->epoch != sm->current_epoch)
181 if (stat_segment_access_start (&sa, sm))
183 ep = vec_elt_at_index (sm->directory_vector, STAT_COUNTER_HEARTBEAT);
184 if (!stat_segment_access_end (&sa, sm))
190 stat_segment_heartbeat (void)
192 stat_client_main_t *sm = &stat_client_main;
193 return stat_segment_heartbeat_r (sm);
196 #define stat_vec_dup(S,V) \
198 __typeof__ ((V)[0]) * _v(v) = 0; \
199 if (V && ((void *)V > (void *)S->shared_header) && \
200 (((void*)V + vec_bytes(V)) < \
201 ((void *)S->shared_header + S->memory_size))) \
202 _v(v) = vec_dup(V); \
207 stat_vec_simple_init (counter_t c)
214 static vlib_counter_t *
215 stat_vec_combined_init (vlib_counter_t c)
217 vlib_counter_t *v = 0;
223 * If index2 is specified copy out the column (the indexed value across all
224 * threads), otherwise copy out all values.
226 static stat_segment_data_t
227 copy_data (vlib_stats_entry_t *ep, u32 index2, char *name,
228 stat_client_main_t *sm)
230 stat_segment_data_t result = { 0 };
232 vlib_counter_t **combined_c; /* Combined counter */
233 counter_t **simple_c; /* Simple counter */
234 uint64_t *error_vector;
236 assert (sm->shared_header);
238 result.type = ep->type;
239 result.name = strdup (name ? name : ep->name);
243 case STAT_DIR_TYPE_SCALAR_INDEX:
244 result.scalar_value = ep->value;
247 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
248 simple_c = stat_segment_adjust (sm, ep->data);
249 result.simple_counter_vec = stat_vec_dup (sm, simple_c);
250 for (i = 0; i < vec_len (simple_c); i++)
252 counter_t *cb = stat_segment_adjust (sm, simple_c[i]);
254 result.simple_counter_vec[i] = stat_vec_simple_init (cb[index2]);
256 result.simple_counter_vec[i] = stat_vec_dup (sm, cb);
260 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
261 combined_c = stat_segment_adjust (sm, ep->data);
262 result.combined_counter_vec = stat_vec_dup (sm, combined_c);
263 for (i = 0; i < vec_len (combined_c); i++)
265 vlib_counter_t *cb = stat_segment_adjust (sm, combined_c[i]);
267 result.combined_counter_vec[i] =
268 stat_vec_combined_init (cb[index2]);
270 result.combined_counter_vec[i] = stat_vec_dup (sm, cb);
274 case STAT_DIR_TYPE_ERROR_INDEX:
275 /* Gather errors from all threads into a vector */
277 stat_segment_adjust (sm, (void *) sm->shared_header->error_vector);
278 vec_validate (result.error_vector, vec_len (error_vector) - 1);
279 for (i = 0; i < vec_len (error_vector); i++)
281 counter_t *cb = stat_segment_adjust (sm, (void *) error_vector[i]);
282 result.error_vector[i] = cb[ep->index];
286 case STAT_DIR_TYPE_NAME_VECTOR:
288 uint8_t **name_vector = stat_segment_adjust (sm, ep->data);
289 result.name_vector = stat_vec_dup (sm, name_vector);
290 for (i = 0; i < vec_len (name_vector); i++)
292 u8 *name = stat_segment_adjust (sm, name_vector[i]);
293 result.name_vector[i] = stat_vec_dup (sm, name);
298 case STAT_DIR_TYPE_SYMLINK:
299 /* Gather info from all threads into a vector */
301 vlib_stats_entry_t *ep2;
302 ep2 = vec_elt_at_index (sm->directory_vector, ep->index1);
303 return copy_data (ep2, ep->index2, ep->name, sm);
306 case STAT_DIR_TYPE_EMPTY:
310 fprintf (stderr, "Unknown type: %d\n", ep->type);
316 stat_segment_data_free (stat_segment_data_t * res)
319 for (i = 0; i < vec_len (res); i++)
323 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
324 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
325 vec_free (res[i].simple_counter_vec[j]);
326 vec_free (res[i].simple_counter_vec);
328 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
329 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
330 vec_free (res[i].combined_counter_vec[j]);
331 vec_free (res[i].combined_counter_vec);
333 case STAT_DIR_TYPE_NAME_VECTOR:
334 for (j = 0; j < vec_len (res[i].name_vector); j++)
335 vec_free (res[i].name_vector[j]);
336 vec_free (res[i].name_vector);
338 case STAT_DIR_TYPE_ERROR_INDEX:
339 vec_free (res[i].error_vector);
341 case STAT_DIR_TYPE_SCALAR_INDEX:
342 case STAT_DIR_TYPE_EMPTY:
353 stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
355 stat_segment_access_t sa;
358 regex_t regex[vec_len (patterns)];
361 for (i = 0; i < vec_len (patterns); i++)
363 int rv = regcomp (®ex[i], (const char *) patterns[i], 0);
366 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
371 if (stat_segment_access_start (&sa, sm))
374 vlib_stats_entry_t *counter_vec = get_stat_vector_r (sm);
375 for (j = 0; j < vec_len (counter_vec); j++)
377 for (i = 0; i < vec_len (patterns); i++)
379 int rv = regexec (®ex[i], counter_vec[j].name, 0, NULL, 0);
386 if (vec_len (patterns) == 0)
390 for (i = 0; i < vec_len (patterns); i++)
393 if (!stat_segment_access_end (&sa, sm))
395 /* Failed, clean up */
401 /* Update last version */
402 sm->current_epoch = sa.epoch;
407 stat_segment_ls (uint8_t ** patterns)
409 stat_client_main_t *sm = &stat_client_main;
410 return stat_segment_ls_r ((uint8_t **) patterns, sm);
413 stat_segment_data_t *
414 stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
417 vlib_stats_entry_t *ep;
418 stat_segment_data_t *res = 0;
419 stat_segment_access_t sa;
421 /* Has directory been update? */
422 if (sm->shared_header->epoch != sm->current_epoch)
425 if (stat_segment_access_start (&sa, sm))
428 for (i = 0; i < vec_len (stats); i++)
430 /* Collect counter */
431 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
432 vec_add1 (res, copy_data (ep, ~0, 0, sm));
435 if (stat_segment_access_end (&sa, sm))
438 fprintf (stderr, "Epoch changed while reading, invalid results\n");
439 // TODO increase counter
443 stat_segment_data_t *
444 stat_segment_dump (uint32_t * stats)
446 stat_client_main_t *sm = &stat_client_main;
447 return stat_segment_dump_r (stats, sm);
450 /* Wrapper for accessing vectors from other languages */
452 stat_segment_vec_len (void *vec)
454 return vec_len (vec);
458 stat_segment_vec_free (void *vec)
463 /* Create a vector from a string (or add to existing) */
465 stat_segment_string_vector (uint8_t ** string_vector, const char *string)
468 size_t len = strlen (string);
470 vec_validate_init_c_string (name, string, len);
471 vec_add1 (string_vector, name);
472 return string_vector;
475 stat_segment_data_t *
476 stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm)
478 vlib_stats_entry_t *ep;
479 stat_segment_data_t *res = 0;
480 stat_segment_access_t sa;
482 /* Has directory been update? */
483 if (sm->shared_header->epoch != sm->current_epoch)
486 if (stat_segment_access_start (&sa, sm))
489 /* Collect counter */
490 ep = vec_elt_at_index (sm->directory_vector, index);
491 vec_add1 (res, copy_data (ep, ~0, 0, sm));
493 if (stat_segment_access_end (&sa, sm))
498 stat_segment_data_t *
499 stat_segment_dump_entry (uint32_t index)
501 stat_client_main_t *sm = &stat_client_main;
502 return stat_segment_dump_entry_r (index, sm);
506 stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm)
508 vlib_stats_entry_t *ep;
509 stat_segment_access_t sa;
510 vlib_stats_entry_t *vec;
512 /* Has directory been update? */
513 if (sm->shared_header->epoch != sm->current_epoch)
515 if (stat_segment_access_start (&sa, sm))
517 vec = get_stat_vector_r (sm);
518 ep = vec_elt_at_index (vec, index);
519 if (!stat_segment_access_end (&sa, sm))
521 return strdup (ep->name);
525 stat_segment_index_to_name (uint32_t index)
527 stat_client_main_t *sm = &stat_client_main;
528 return stat_segment_index_to_name_r (index, sm);
532 stat_segment_version_r (stat_client_main_t * sm)
534 ASSERT (sm->shared_header);
535 return sm->shared_header->version;
539 stat_segment_version (void)
541 stat_client_main_t *sm = &stat_client_main;
542 return stat_segment_version_r (sm);
546 * fd.io coding-style-patch-verification: ON
549 * eval: (c-set-style "gnu")