blob: 359813f8d574b932d2717650cba7b08ba9d6c76c [file] [log] [blame]
Ole Troan2fee1672018-08-23 13:00:53 +02001/*
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
Ole Troan58492a82018-09-04 13:19:12 +020020#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>
Ole Troan2fee1672018-08-23 13:00:53 +020027#include <regex.h>
Ole Troan58492a82018-09-04 13:19:12 +020028#include <assert.h>
29#include <vppinfra/vec.h>
30#include <vppinfra/lock.h>
Ole Troan58492a82018-09-04 13:19:12 +020031#include <stdatomic.h>
Damjan Marion8973b072022-03-01 15:51:18 +010032#include <vlib/vlib.h>
33#include <vlib/stats/stats.h>
Dave Barach531969e2019-08-14 09:35:41 -040034#include <vpp-api/client/stat_client.h>
Ole Troan2fee1672018-08-23 13:00:53 +020035
36stat_client_main_t stat_client_main;
37
Paul Vinciguerra56421092018-11-21 16:34:09 -080038stat_client_main_t *
39stat_client_get (void)
40{
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));
44 return sm;
45}
46
47void
48stat_client_free (stat_client_main_t * sm)
49{
50 free (sm);
51}
52
Ole Troan58492a82018-09-04 13:19:12 +020053static int
54recv_fd (int sock)
55{
56 struct msghdr msg = { 0 };
57 struct cmsghdr *cmsg;
58 int fd = -1;
59 char iobuf[1];
60 struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
61 union
62 {
63 char buf[CMSG_SPACE (sizeof (fd))];
64 struct cmsghdr align;
65 } u;
66 msg.msg_iov = &io;
67 msg.msg_iovlen = 1;
68 msg.msg_control = u.buf;
69 msg.msg_controllen = sizeof (u.buf);
70
71 ssize_t size;
72 if ((size = recvmsg (sock, &msg, 0)) < 0)
73 {
74 perror ("recvmsg failed");
75 return -1;
76 }
77 cmsg = CMSG_FIRSTHDR (&msg);
78 if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
79 {
80 memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
81 }
82 return fd;
83}
84
Damjan Marion8973b072022-03-01 15:51:18 +010085static vlib_stats_entry_t *
86get_stat_vector_r (stat_client_main_t *sm)
Ole Troan58492a82018-09-04 13:19:12 +020087{
Ole Troan58492a82018-09-04 13:19:12 +020088 ASSERT (sm->shared_header);
Ole Troan7d29e322020-07-21 08:46:08 +020089 return stat_segment_adjust (sm,
90 (void *) sm->shared_header->directory_vector);
Ole Troan58492a82018-09-04 13:19:12 +020091}
92
Paul Vinciguerra56421092018-11-21 16:34:09 -080093int
Neale Ranns318d7942018-12-03 01:53:32 -080094stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm)
Paul Vinciguerra56421092018-11-21 16:34:09 -080095{
Ole Troan58492a82018-09-04 13:19:12 +020096 int mfd = -1;
97 int sock;
Ole Troan2fee1672018-08-23 13:00:53 +020098
Dave Barachb7b92992018-10-17 10:38:51 -040099 clib_memset (sm, 0, sizeof (*sm));
Ole Troan58492a82018-09-04 13:19:12 +0200100 if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
Ole Troan2fee1672018-08-23 13:00:53 +0200101 {
Paul Vinciguerra56421092018-11-21 16:34:09 -0800102 perror ("Stat client couldn't open socket");
Ole Troan2fee1672018-08-23 13:00:53 +0200103 return -1;
104 }
105
Ole Troan58492a82018-09-04 13:19:12 +0200106 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)) <
110 0)
111 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400112 close (sock);
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800113 return -2;
Ole Troan58492a82018-09-04 13:19:12 +0200114 }
Ole Troan2fee1672018-08-23 13:00:53 +0200115
Ole Troan58492a82018-09-04 13:19:12 +0200116 if ((mfd = recv_fd (sock)) < 0)
117 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400118 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200119 fprintf (stderr, "Receiving file descriptor failed\n");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800120 return -3;
Ole Troan58492a82018-09-04 13:19:12 +0200121 }
122 close (sock);
Ole Troan2fee1672018-08-23 13:00:53 +0200123
Ole Troan58492a82018-09-04 13:19:12 +0200124 /* mmap shared memory segment. */
125 void *memaddr;
126 struct stat st = { 0 };
127
128 if (fstat (mfd, &st) == -1)
129 {
Ole Troan8ae3a532019-03-10 21:26:14 +0100130 close (mfd);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800131 perror ("mmap fstat failed");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800132 return -4;
Ole Troan58492a82018-09-04 13:19:12 +0200133 }
134 if ((memaddr =
135 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
136 {
Ole Troan8ae3a532019-03-10 21:26:14 +0100137 close (mfd);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800138 perror ("mmap map failed");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800139 return -5;
Ole Troan58492a82018-09-04 13:19:12 +0200140 }
141
Ole Troan8ae3a532019-03-10 21:26:14 +0100142 close (mfd);
Ole Troan58492a82018-09-04 13:19:12 +0200143 sm->memory_size = st.st_size;
144 sm->shared_header = memaddr;
145 sm->directory_vector =
Ole Troan7d29e322020-07-21 08:46:08 +0200146 stat_segment_adjust (sm, (void *) sm->shared_header->directory_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200147
148 return 0;
149}
150
Paul Vinciguerra56421092018-11-21 16:34:09 -0800151int
Neale Ranns318d7942018-12-03 01:53:32 -0800152stat_segment_connect (const char *socket_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800153{
154 stat_client_main_t *sm = &stat_client_main;
155 return stat_segment_connect_r (socket_name, sm);
156}
157
158void
159stat_segment_disconnect_r (stat_client_main_t * sm)
160{
161 munmap (sm->shared_header, sm->memory_size);
162 return;
163}
164
Ole Troan2fee1672018-08-23 13:00:53 +0200165void
166stat_segment_disconnect (void)
167{
168 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800169 return stat_segment_disconnect_r (sm);
170}
Ole Troan58492a82018-09-04 13:19:12 +0200171
Paul Vinciguerra56421092018-11-21 16:34:09 -0800172double
173stat_segment_heartbeat_r (stat_client_main_t * sm)
174{
Ole Troancbb8f582019-04-15 08:53:46 +0200175 stat_segment_access_t sa;
Damjan Marion8973b072022-03-01 15:51:18 +0100176 vlib_stats_entry_t *ep;
Ole Troancbb8f582019-04-15 08:53:46 +0200177
Arthur de Kerhordb023802021-03-11 10:26:54 -0800178 /* Has directory been updated? */
Ole Troancbb8f582019-04-15 08:53:46 +0200179 if (sm->shared_header->epoch != sm->current_epoch)
180 return 0;
Ole Troanfdc67802020-08-10 11:59:20 +0200181 if (stat_segment_access_start (&sa, sm))
182 return 0;
Ole Troancbb8f582019-04-15 08:53:46 +0200183 ep = vec_elt_at_index (sm->directory_vector, STAT_COUNTER_HEARTBEAT);
184 if (!stat_segment_access_end (&sa, sm))
185 return 0.0;
186 return ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200187}
188
Ole Troan58492a82018-09-04 13:19:12 +0200189double
Ole Troan2fee1672018-08-23 13:00:53 +0200190stat_segment_heartbeat (void)
191{
Ole Troan2fee1672018-08-23 13:00:53 +0200192 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800193 return stat_segment_heartbeat_r (sm);
Ole Troan2fee1672018-08-23 13:00:53 +0200194}
195
Ole Troan65c56c82020-10-21 11:55:28 +0200196#define stat_vec_dup(S,V) \
197 ({ \
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); \
203 _v(v); \
204})
205
Arthur de Kerhordb023802021-03-11 10:26:54 -0800206static counter_t *
207stat_vec_simple_init (counter_t c)
208{
209 counter_t *v = 0;
210 vec_add1 (v, c);
211 return v;
212}
213
214static vlib_counter_t *
215stat_vec_combined_init (vlib_counter_t c)
216{
217 vlib_counter_t *v = 0;
218 vec_add1 (v, c);
219 return v;
220}
221
222/*
223 * If index2 is specified copy out the column (the indexed value across all
224 * threads), otherwise copy out all values.
225 */
Neale Ranns796c84b2019-03-28 07:56:23 -0700226static stat_segment_data_t
Damjan Marion8973b072022-03-01 15:51:18 +0100227copy_data (vlib_stats_entry_t *ep, u32 index2, char *name,
Ole Troan04853c62023-02-17 14:23:48 +0100228 stat_client_main_t *sm, bool via_symlink)
Ole Troan2fee1672018-08-23 13:00:53 +0200229{
230 stat_segment_data_t result = { 0 };
Ole Troan2fee1672018-08-23 13:00:53 +0200231 int i;
232 vlib_counter_t **combined_c; /* Combined counter */
233 counter_t **simple_c; /* Simple counter */
Ole Troan58492a82018-09-04 13:19:12 +0200234
235 assert (sm->shared_header);
236
Ole Troan2fee1672018-08-23 13:00:53 +0200237 result.type = ep->type;
Ole Troan04853c62023-02-17 14:23:48 +0100238 result.via_symlink = via_symlink;
Arthur de Kerhordb023802021-03-11 10:26:54 -0800239 result.name = strdup (name ? name : ep->name);
240
Ole Troan2fee1672018-08-23 13:00:53 +0200241 switch (ep->type)
242 {
Ole Troan58492a82018-09-04 13:19:12 +0200243 case STAT_DIR_TYPE_SCALAR_INDEX:
244 result.scalar_value = ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200245 break;
246
247 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
Ole Troan7d29e322020-07-21 08:46:08 +0200248 simple_c = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200249 result.simple_counter_vec = stat_vec_dup (sm, simple_c);
Ole Troan2fee1672018-08-23 13:00:53 +0200250 for (i = 0; i < vec_len (simple_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200251 {
Ole Troan7d29e322020-07-21 08:46:08 +0200252 counter_t *cb = stat_segment_adjust (sm, simple_c[i]);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800253 if (index2 != ~0)
254 result.simple_counter_vec[i] = stat_vec_simple_init (cb[index2]);
255 else
256 result.simple_counter_vec[i] = stat_vec_dup (sm, cb);
Ole Troan58492a82018-09-04 13:19:12 +0200257 }
Ole Troan2fee1672018-08-23 13:00:53 +0200258 break;
259
260 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
Ole Troan7d29e322020-07-21 08:46:08 +0200261 combined_c = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200262 result.combined_counter_vec = stat_vec_dup (sm, combined_c);
Ole Troan2fee1672018-08-23 13:00:53 +0200263 for (i = 0; i < vec_len (combined_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200264 {
Ole Troan7d29e322020-07-21 08:46:08 +0200265 vlib_counter_t *cb = stat_segment_adjust (sm, combined_c[i]);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800266 if (index2 != ~0)
267 result.combined_counter_vec[i] =
268 stat_vec_combined_init (cb[index2]);
269 else
270 result.combined_counter_vec[i] = stat_vec_dup (sm, cb);
Ole Troan58492a82018-09-04 13:19:12 +0200271 }
Ole Troan2fee1672018-08-23 13:00:53 +0200272 break;
273
Ole Troan703908a2019-02-26 16:37:03 +0100274 case STAT_DIR_TYPE_NAME_VECTOR:
Ole Troan7d29e322020-07-21 08:46:08 +0200275 {
276 uint8_t **name_vector = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200277 result.name_vector = stat_vec_dup (sm, name_vector);
Ole Troan7d29e322020-07-21 08:46:08 +0200278 for (i = 0; i < vec_len (name_vector); i++)
279 {
280 u8 *name = stat_segment_adjust (sm, name_vector[i]);
Ole Troan65c56c82020-10-21 11:55:28 +0200281 result.name_vector[i] = stat_vec_dup (sm, name);
Ole Troan7d29e322020-07-21 08:46:08 +0200282 }
283 }
Ole Troan703908a2019-02-26 16:37:03 +0100284 break;
285
Arthur de Kerhordb023802021-03-11 10:26:54 -0800286 case STAT_DIR_TYPE_SYMLINK:
287 /* Gather info from all threads into a vector */
288 {
Damjan Marion8973b072022-03-01 15:51:18 +0100289 vlib_stats_entry_t *ep2;
Arthur de Kerhordb023802021-03-11 10:26:54 -0800290 ep2 = vec_elt_at_index (sm->directory_vector, ep->index1);
Andrew Yourtchenko3f885f92022-11-28 17:56:16 +0000291 /* We do not intend to return the "result", avoid a leak */
292 free (result.name);
Ole Troan04853c62023-02-17 14:23:48 +0100293 return copy_data (ep2, ep->index2, ep->name, sm, true);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800294 }
295
Vladimir Isaev72e31bc2020-02-04 11:54:27 +0300296 case STAT_DIR_TYPE_EMPTY:
297 break;
298
Ole Troan2fee1672018-08-23 13:00:53 +0200299 default:
Ole Troan703908a2019-02-26 16:37:03 +0100300 fprintf (stderr, "Unknown type: %d\n", ep->type);
Ole Troan2fee1672018-08-23 13:00:53 +0200301 }
302 return result;
303}
304
Ole Troan2fee1672018-08-23 13:00:53 +0200305void
306stat_segment_data_free (stat_segment_data_t * res)
307{
308 int i, j;
309 for (i = 0; i < vec_len (res); i++)
310 {
311 switch (res[i].type)
312 {
313 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
314 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
315 vec_free (res[i].simple_counter_vec[j]);
316 vec_free (res[i].simple_counter_vec);
317 break;
318 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
319 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
320 vec_free (res[i].combined_counter_vec[j]);
321 vec_free (res[i].combined_counter_vec);
322 break;
Ole Troan3c70c052020-08-10 16:25:21 +0200323 case STAT_DIR_TYPE_NAME_VECTOR:
324 for (j = 0; j < vec_len (res[i].name_vector); j++)
325 vec_free (res[i].name_vector[j]);
326 vec_free (res[i].name_vector);
327 break;
Ole Troan65c56c82020-10-21 11:55:28 +0200328 case STAT_DIR_TYPE_SCALAR_INDEX:
Steven Luong0a214802022-01-28 14:12:53 -0800329 case STAT_DIR_TYPE_EMPTY:
Ole Troan65c56c82020-10-21 11:55:28 +0200330 break;
Ole Troan2fee1672018-08-23 13:00:53 +0200331 default:
Ole Troan3c70c052020-08-10 16:25:21 +0200332 assert (0);
Ole Troan2fee1672018-08-23 13:00:53 +0200333 }
Ole Troan58492a82018-09-04 13:19:12 +0200334 free (res[i].name);
Ole Troan2fee1672018-08-23 13:00:53 +0200335 }
336 vec_free (res);
337}
338
Ole Troan58492a82018-09-04 13:19:12 +0200339uint32_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800340stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200341{
Ole Troan58492a82018-09-04 13:19:12 +0200342 stat_segment_access_t sa;
343
344 uint32_t *dir = 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200345 regex_t regex[vec_len (patterns)];
346
Ole Troan58492a82018-09-04 13:19:12 +0200347 int i, j;
Ole Troan2fee1672018-08-23 13:00:53 +0200348 for (i = 0; i < vec_len (patterns); i++)
349 {
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000350 int rv = regcomp (&regex[i], (const char *) patterns[i], 0);
Ole Troan2fee1672018-08-23 13:00:53 +0200351 if (rv)
352 {
353 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
354 return dir;
355 }
356 }
357
Ole Troanfdc67802020-08-10 11:59:20 +0200358 if (stat_segment_access_start (&sa, sm))
359 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200360
Damjan Marion8973b072022-03-01 15:51:18 +0100361 vlib_stats_entry_t *counter_vec = get_stat_vector_r (sm);
Ole Troan58492a82018-09-04 13:19:12 +0200362 for (j = 0; j < vec_len (counter_vec); j++)
363 {
364 for (i = 0; i < vec_len (patterns); i++)
365 {
366 int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
367 if (rv == 0)
368 {
369 vec_add1 (dir, j);
370 break;
371 }
372 }
373 if (vec_len (patterns) == 0)
374 vec_add1 (dir, j);
Ole Troan2fee1672018-08-23 13:00:53 +0200375 }
Ole Troan2fee1672018-08-23 13:00:53 +0200376
377 for (i = 0; i < vec_len (patterns); i++)
378 regfree (&regex[i]);
379
Paul Vinciguerra56421092018-11-21 16:34:09 -0800380 if (!stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200381 {
382 /* Failed, clean up */
383 vec_free (dir);
384 return 0;
385
386 }
387
388 /* Update last version */
389 sm->current_epoch = sa.epoch;
Ole Troan2fee1672018-08-23 13:00:53 +0200390 return dir;
391}
392
Paul Vinciguerra56421092018-11-21 16:34:09 -0800393uint32_t *
394stat_segment_ls (uint8_t ** patterns)
395{
396 stat_client_main_t *sm = &stat_client_main;
397 return stat_segment_ls_r ((uint8_t **) patterns, sm);
398}
399
Ole Troan2fee1672018-08-23 13:00:53 +0200400stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800401stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200402{
403 int i;
Damjan Marion8973b072022-03-01 15:51:18 +0100404 vlib_stats_entry_t *ep;
Ole Troan2fee1672018-08-23 13:00:53 +0200405 stat_segment_data_t *res = 0;
Ole Troan58492a82018-09-04 13:19:12 +0200406 stat_segment_access_t sa;
Ole Troan2fee1672018-08-23 13:00:53 +0200407
Ole Troan58492a82018-09-04 13:19:12 +0200408 /* Has directory been update? */
409 if (sm->shared_header->epoch != sm->current_epoch)
410 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200411
Ole Troanfdc67802020-08-10 11:59:20 +0200412 if (stat_segment_access_start (&sa, sm))
413 return 0;
414
Andrew Yourtchenko9067f332022-12-02 21:22:37 +0000415 /* preallocate the elements.
416 * This takes care of a special case where
417 * the vec_len(stats) == 0,
418 * such that we return a vector of
419 * length 0, rather than a null pointer
420 * (since null pointer is an error)
421 */
422 vec_alloc (res, vec_len (stats));
423
Ole Troan2fee1672018-08-23 13:00:53 +0200424 for (i = 0; i < vec_len (stats); i++)
425 {
Ole Troan2fee1672018-08-23 13:00:53 +0200426 /* Collect counter */
Ole Troan58492a82018-09-04 13:19:12 +0200427 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
Ole Troan04853c62023-02-17 14:23:48 +0100428 vec_add1 (res, copy_data (ep, ~0, 0, sm, false));
Ole Troan2fee1672018-08-23 13:00:53 +0200429 }
Ole Troan2fee1672018-08-23 13:00:53 +0200430
Paul Vinciguerra56421092018-11-21 16:34:09 -0800431 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200432 return res;
433
434 fprintf (stderr, "Epoch changed while reading, invalid results\n");
435 // TODO increase counter
Duncan Eastoecf214cc2022-11-30 21:16:13 +0000436 if (res)
437 stat_segment_data_free (res);
Ole Troan58492a82018-09-04 13:19:12 +0200438 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200439}
440
Paul Vinciguerra56421092018-11-21 16:34:09 -0800441stat_segment_data_t *
442stat_segment_dump (uint32_t * stats)
443{
444 stat_client_main_t *sm = &stat_client_main;
445 return stat_segment_dump_r (stats, sm);
446}
447
Ole Troan73202102018-08-31 00:29:48 +0200448/* Wrapper for accessing vectors from other languages */
449int
450stat_segment_vec_len (void *vec)
451{
452 return vec_len (vec);
453}
454
Ole Troan82540182018-10-22 09:41:29 +0200455void
456stat_segment_vec_free (void *vec)
457{
458 vec_free (vec);
459}
460
Ole Troan73202102018-08-31 00:29:48 +0200461/* Create a vector from a string (or add to existing) */
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000462uint8_t **
463stat_segment_string_vector (uint8_t ** string_vector, const char *string)
Ole Troan73202102018-08-31 00:29:48 +0200464{
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000465 uint8_t *name = 0;
466 size_t len = strlen (string);
467
468 vec_validate_init_c_string (name, string, len);
469 vec_add1 (string_vector, name);
Ole Troan73202102018-08-31 00:29:48 +0200470 return string_vector;
471}
472
Ole Troan58492a82018-09-04 13:19:12 +0200473stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800474stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200475{
Damjan Marion8973b072022-03-01 15:51:18 +0100476 vlib_stats_entry_t *ep;
Ole Troan58492a82018-09-04 13:19:12 +0200477 stat_segment_data_t *res = 0;
478 stat_segment_access_t sa;
479
Arthur de Kerhordb023802021-03-11 10:26:54 -0800480 /* Has directory been update? */
481 if (sm->shared_header->epoch != sm->current_epoch)
482 return 0;
483
Ole Troanfdc67802020-08-10 11:59:20 +0200484 if (stat_segment_access_start (&sa, sm))
485 return 0;
Ole Troan58492a82018-09-04 13:19:12 +0200486
487 /* Collect counter */
488 ep = vec_elt_at_index (sm->directory_vector, index);
Ole Troan04853c62023-02-17 14:23:48 +0100489 vec_add1 (res, copy_data (ep, ~0, 0, sm, false));
Ole Troan58492a82018-09-04 13:19:12 +0200490
Paul Vinciguerra56421092018-11-21 16:34:09 -0800491 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200492 return res;
493 return 0;
494}
495
Paul Vinciguerra56421092018-11-21 16:34:09 -0800496stat_segment_data_t *
497stat_segment_dump_entry (uint32_t index)
498{
499 stat_client_main_t *sm = &stat_client_main;
500 return stat_segment_dump_entry_r (index, sm);
501}
502
Ole Troan58492a82018-09-04 13:19:12 +0200503char *
Ole Troancbb8f582019-04-15 08:53:46 +0200504stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm)
505{
Damjan Marion8973b072022-03-01 15:51:18 +0100506 vlib_stats_entry_t *ep;
Ole Troancbb8f582019-04-15 08:53:46 +0200507 stat_segment_access_t sa;
Damjan Marion8973b072022-03-01 15:51:18 +0100508 vlib_stats_entry_t *vec;
Ole Troancbb8f582019-04-15 08:53:46 +0200509
510 /* Has directory been update? */
511 if (sm->shared_header->epoch != sm->current_epoch)
512 return 0;
Ole Troanfdc67802020-08-10 11:59:20 +0200513 if (stat_segment_access_start (&sa, sm))
514 return 0;
Ole Troancbb8f582019-04-15 08:53:46 +0200515 vec = get_stat_vector_r (sm);
516 ep = vec_elt_at_index (vec, index);
Ole Troan22ca0d02023-03-23 22:09:51 +0100517 if (ep->type == STAT_DIR_TYPE_EMPTY)
518 {
519 stat_segment_access_end (&sa, sm);
520 return 0;
521 }
Ole Troancbb8f582019-04-15 08:53:46 +0200522 if (!stat_segment_access_end (&sa, sm))
523 return 0;
524 return strdup (ep->name);
525}
526
527char *
Ole Troan58492a82018-09-04 13:19:12 +0200528stat_segment_index_to_name (uint32_t index)
529{
Ole Troancbb8f582019-04-15 08:53:46 +0200530 stat_client_main_t *sm = &stat_client_main;
531 return stat_segment_index_to_name_r (index, sm);
Ole Troan58492a82018-09-04 13:19:12 +0200532}
533
Ole Troanb63dbc52019-06-14 10:26:14 +0200534uint64_t
535stat_segment_version_r (stat_client_main_t * sm)
536{
537 ASSERT (sm->shared_header);
538 return sm->shared_header->version;
539}
540
541uint64_t
542stat_segment_version (void)
543{
544 stat_client_main_t *sm = &stat_client_main;
545 return stat_segment_version_r (sm);
546}
547
Ole Troan2fee1672018-08-23 13:00:53 +0200548/*
549 * fd.io coding-style-patch-verification: ON
550 *
551 * Local Variables:
552 * eval: (c-set-style "gnu")
553 * End:
554 */