blob: 98c275727f06b9ad8858b7f6363ced081c629949 [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 Troan2fee1672018-08-23 13:00:53 +020031#include "stat_client.h"
Ole Troan58492a82018-09-04 13:19:12 +020032#include <stdatomic.h>
Ole Troan2fee1672018-08-23 13:00:53 +020033
Ole Troan2fee1672018-08-23 13:00:53 +020034
35stat_client_main_t stat_client_main;
36
Paul Vinciguerra56421092018-11-21 16:34:09 -080037stat_client_main_t *
38stat_client_get (void)
39{
40 stat_client_main_t *sm;
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
46void
47stat_client_free (stat_client_main_t * sm)
48{
49 free (sm);
50}
51
Ole Troan58492a82018-09-04 13:19:12 +020052static int
53recv_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
84static stat_segment_directory_entry_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -080085get_stat_vector_r (stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +020086{
Ole Troan58492a82018-09-04 13:19:12 +020087 ASSERT (sm->shared_header);
88 return stat_segment_pointer (sm->shared_header,
89 sm->shared_header->directory_offset);
90}
91
Paul Vinciguerra56421092018-11-21 16:34:09 -080092static stat_segment_directory_entry_t *
93get_stat_vector (void)
Ole Troan2fee1672018-08-23 13:00:53 +020094{
95 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -080096 return get_stat_vector_r (sm);
97}
98
99int
100stat_segment_connect_r (char *socket_name, stat_client_main_t * sm)
101{
Ole Troan58492a82018-09-04 13:19:12 +0200102 int mfd = -1;
103 int sock;
Ole Troan2fee1672018-08-23 13:00:53 +0200104
Dave Barachb7b92992018-10-17 10:38:51 -0400105 clib_memset (sm, 0, sizeof (*sm));
Ole Troan58492a82018-09-04 13:19:12 +0200106 if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
Ole Troan2fee1672018-08-23 13:00:53 +0200107 {
Paul Vinciguerra56421092018-11-21 16:34:09 -0800108 perror ("Stat client couldn't open socket");
Ole Troan2fee1672018-08-23 13:00:53 +0200109 return -1;
110 }
111
Ole Troan58492a82018-09-04 13:19:12 +0200112 struct sockaddr_un un = { 0 };
113 un.sun_family = AF_UNIX;
114 strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
115 if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
116 0)
117 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400118 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200119 return -1;
120 }
Ole Troan2fee1672018-08-23 13:00:53 +0200121
Ole Troan58492a82018-09-04 13:19:12 +0200122 if ((mfd = recv_fd (sock)) < 0)
123 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400124 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200125 fprintf (stderr, "Receiving file descriptor failed\n");
126 return -1;
127 }
128 close (sock);
Ole Troan2fee1672018-08-23 13:00:53 +0200129
Ole Troan58492a82018-09-04 13:19:12 +0200130 /* mmap shared memory segment. */
131 void *memaddr;
132 struct stat st = { 0 };
133
134 if (fstat (mfd, &st) == -1)
135 {
Paul Vinciguerra56421092018-11-21 16:34:09 -0800136 perror ("mmap fstat failed");
Ole Troan58492a82018-09-04 13:19:12 +0200137 return -1;
138 }
139 if ((memaddr =
140 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
141 {
Paul Vinciguerra56421092018-11-21 16:34:09 -0800142 perror ("mmap map failed");
Ole Troan58492a82018-09-04 13:19:12 +0200143 return -1;
144 }
145
146 sm->memory_size = st.st_size;
147 sm->shared_header = memaddr;
148 sm->directory_vector =
149 stat_segment_pointer (memaddr, sm->shared_header->directory_offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200150
151 return 0;
152}
153
Paul Vinciguerra56421092018-11-21 16:34:09 -0800154int
155stat_segment_connect (char *socket_name)
156{
157 stat_client_main_t *sm = &stat_client_main;
158 return stat_segment_connect_r (socket_name, sm);
159}
160
161void
162stat_segment_disconnect_r (stat_client_main_t * sm)
163{
164 munmap (sm->shared_header, sm->memory_size);
165 return;
166}
167
Ole Troan2fee1672018-08-23 13:00:53 +0200168void
169stat_segment_disconnect (void)
170{
171 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800172 return stat_segment_disconnect_r (sm);
173}
Ole Troan58492a82018-09-04 13:19:12 +0200174
Paul Vinciguerra56421092018-11-21 16:34:09 -0800175double
176stat_segment_heartbeat_r (stat_client_main_t * sm)
177{
178 stat_segment_directory_entry_t *vec = get_stat_vector_r (sm);
179 double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset);
180 return *hb;
Ole Troan2fee1672018-08-23 13:00:53 +0200181}
182
Ole Troan58492a82018-09-04 13:19:12 +0200183double
Ole Troan2fee1672018-08-23 13:00:53 +0200184stat_segment_heartbeat (void)
185{
Ole Troan2fee1672018-08-23 13:00:53 +0200186 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800187 return stat_segment_heartbeat_r (sm);
Ole Troan2fee1672018-08-23 13:00:53 +0200188}
189
190stat_segment_data_t
Paul Vinciguerra56421092018-11-21 16:34:09 -0800191copy_data (stat_segment_directory_entry_t * ep, stat_client_main_t * sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200192{
193 stat_segment_data_t result = { 0 };
Ole Troan2fee1672018-08-23 13:00:53 +0200194 int i;
195 vlib_counter_t **combined_c; /* Combined counter */
196 counter_t **simple_c; /* Simple counter */
Ole Troan58492a82018-09-04 13:19:12 +0200197 counter_t *error_base;
198 uint64_t *offset_vector;
199
200 assert (sm->shared_header);
201
Ole Troan2fee1672018-08-23 13:00:53 +0200202 result.type = ep->type;
Ole Troan58492a82018-09-04 13:19:12 +0200203 result.name = strdup (ep->name);
Ole Troan2fee1672018-08-23 13:00:53 +0200204 switch (ep->type)
205 {
Ole Troan58492a82018-09-04 13:19:12 +0200206 case STAT_DIR_TYPE_SCALAR_INDEX:
207 result.scalar_value = ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200208 break;
209
210 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
Ole Troanbf495902018-10-02 22:00:29 +0200211 if (ep->offset == 0)
212 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200213 simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200214 result.simple_counter_vec = vec_dup (simple_c);
Ole Troan58492a82018-09-04 13:19:12 +0200215 offset_vector =
216 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200217 for (i = 0; i < vec_len (simple_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200218 {
219 counter_t *cb =
220 stat_segment_pointer (sm->shared_header, offset_vector[i]);
221 result.simple_counter_vec[i] = vec_dup (cb);
222 }
Ole Troan2fee1672018-08-23 13:00:53 +0200223 break;
224
225 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
Ole Troanbf495902018-10-02 22:00:29 +0200226 if (ep->offset == 0)
227 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200228 combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200229 result.combined_counter_vec = vec_dup (combined_c);
Ole Troan58492a82018-09-04 13:19:12 +0200230 offset_vector =
231 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200232 for (i = 0; i < vec_len (combined_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200233 {
234 vlib_counter_t *cb =
235 stat_segment_pointer (sm->shared_header, offset_vector[i]);
236 result.combined_counter_vec[i] = vec_dup (cb);
237 }
Ole Troan2fee1672018-08-23 13:00:53 +0200238 break;
239
240 case STAT_DIR_TYPE_ERROR_INDEX:
Ole Troan58492a82018-09-04 13:19:12 +0200241 error_base =
242 stat_segment_pointer (sm->shared_header,
243 sm->shared_header->error_offset);
244 result.error_value = error_base[ep->index];
Ole Troan2fee1672018-08-23 13:00:53 +0200245 break;
246
247 default:
248 fprintf (stderr, "Unknown type: %d", ep->type);
249 }
250 return result;
251}
252
Ole Troan2fee1672018-08-23 13:00:53 +0200253void
254stat_segment_data_free (stat_segment_data_t * res)
255{
256 int i, j;
257 for (i = 0; i < vec_len (res); i++)
258 {
259 switch (res[i].type)
260 {
261 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
262 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
263 vec_free (res[i].simple_counter_vec[j]);
264 vec_free (res[i].simple_counter_vec);
265 break;
266 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
267 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
268 vec_free (res[i].combined_counter_vec[j]);
269 vec_free (res[i].combined_counter_vec);
270 break;
271 default:
272 ;
273 }
Ole Troan58492a82018-09-04 13:19:12 +0200274 free (res[i].name);
Ole Troan2fee1672018-08-23 13:00:53 +0200275 }
276 vec_free (res);
277}
278
Ole Troan58492a82018-09-04 13:19:12 +0200279
280typedef struct
281{
282 uint64_t epoch;
283} stat_segment_access_t;
284
285static void
Paul Vinciguerra56421092018-11-21 16:34:09 -0800286stat_segment_access_start (stat_segment_access_t * sa,
287 stat_client_main_t * sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200288{
Ole Troan58492a82018-09-04 13:19:12 +0200289 stat_segment_shared_header_t *shared_header = sm->shared_header;
290 sa->epoch = shared_header->epoch;
291 while (shared_header->in_progress != 0)
292 ;
293}
294
295static bool
Paul Vinciguerra56421092018-11-21 16:34:09 -0800296stat_segment_access_end (stat_segment_access_t * sa, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200297{
Ole Troan58492a82018-09-04 13:19:12 +0200298 stat_segment_shared_header_t *shared_header = sm->shared_header;
299
300 if (shared_header->epoch != sa->epoch || shared_header->in_progress)
301 return false;
302 return true;
303}
304
305uint32_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800306stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200307{
Ole Troan58492a82018-09-04 13:19:12 +0200308 stat_segment_access_t sa;
309
310 uint32_t *dir = 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200311 regex_t regex[vec_len (patterns)];
312
Ole Troan58492a82018-09-04 13:19:12 +0200313 int i, j;
Ole Troan2fee1672018-08-23 13:00:53 +0200314 for (i = 0; i < vec_len (patterns); i++)
315 {
316 int rv = regcomp (&regex[i], (char *) patterns[i], 0);
317 if (rv)
318 {
319 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
320 return dir;
321 }
322 }
323
Paul Vinciguerra56421092018-11-21 16:34:09 -0800324 stat_segment_access_start (&sa, sm);
Ole Troan2fee1672018-08-23 13:00:53 +0200325
Paul Vinciguerra56421092018-11-21 16:34:09 -0800326 stat_segment_directory_entry_t *counter_vec = get_stat_vector_r (sm);
Ole Troan58492a82018-09-04 13:19:12 +0200327 for (j = 0; j < vec_len (counter_vec); j++)
328 {
329 for (i = 0; i < vec_len (patterns); i++)
330 {
331 int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
332 if (rv == 0)
333 {
334 vec_add1 (dir, j);
335 break;
336 }
337 }
338 if (vec_len (patterns) == 0)
339 vec_add1 (dir, j);
Ole Troan2fee1672018-08-23 13:00:53 +0200340 }
Ole Troan2fee1672018-08-23 13:00:53 +0200341
342 for (i = 0; i < vec_len (patterns); i++)
343 regfree (&regex[i]);
344
Paul Vinciguerra56421092018-11-21 16:34:09 -0800345 if (!stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200346 {
347 /* Failed, clean up */
348 vec_free (dir);
349 return 0;
350
351 }
352
353 /* Update last version */
354 sm->current_epoch = sa.epoch;
Ole Troan2fee1672018-08-23 13:00:53 +0200355 return dir;
356}
357
Paul Vinciguerra56421092018-11-21 16:34:09 -0800358uint32_t *
359stat_segment_ls (uint8_t ** patterns)
360{
361 stat_client_main_t *sm = &stat_client_main;
362 return stat_segment_ls_r ((uint8_t **) patterns, sm);
363}
364
Ole Troan2fee1672018-08-23 13:00:53 +0200365stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800366stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200367{
368 int i;
Ole Troan2fee1672018-08-23 13:00:53 +0200369 stat_segment_directory_entry_t *ep;
370 stat_segment_data_t *res = 0;
Ole Troan58492a82018-09-04 13:19:12 +0200371 stat_segment_access_t sa;
Ole Troan2fee1672018-08-23 13:00:53 +0200372
Ole Troan58492a82018-09-04 13:19:12 +0200373 /* Has directory been update? */
374 if (sm->shared_header->epoch != sm->current_epoch)
375 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200376
Paul Vinciguerra56421092018-11-21 16:34:09 -0800377 stat_segment_access_start (&sa, sm);
Ole Troan2fee1672018-08-23 13:00:53 +0200378 for (i = 0; i < vec_len (stats); i++)
379 {
Ole Troan2fee1672018-08-23 13:00:53 +0200380 /* Collect counter */
Ole Troan58492a82018-09-04 13:19:12 +0200381 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800382 vec_add1 (res, copy_data (ep, sm));
Ole Troan2fee1672018-08-23 13:00:53 +0200383 }
Ole Troan2fee1672018-08-23 13:00:53 +0200384
Paul Vinciguerra56421092018-11-21 16:34:09 -0800385 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200386 return res;
387
388 fprintf (stderr, "Epoch changed while reading, invalid results\n");
389 // TODO increase counter
390 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200391}
392
Paul Vinciguerra56421092018-11-21 16:34:09 -0800393stat_segment_data_t *
394stat_segment_dump (uint32_t * stats)
395{
396 stat_client_main_t *sm = &stat_client_main;
397 return stat_segment_dump_r (stats, sm);
398}
399
Ole Troan73202102018-08-31 00:29:48 +0200400/* Wrapper for accessing vectors from other languages */
401int
402stat_segment_vec_len (void *vec)
403{
404 return vec_len (vec);
405}
406
Ole Troan82540182018-10-22 09:41:29 +0200407void
408stat_segment_vec_free (void *vec)
409{
410 vec_free (vec);
411}
412
Ole Troan73202102018-08-31 00:29:48 +0200413/* Create a vector from a string (or add to existing) */
414u8 **
415stat_segment_string_vector (u8 ** string_vector, char *string)
416{
417 u8 *name = 0;
418 name = vec_dup ((u8 *) string);
419 vec_add1 (string_vector, (u8 *) name);
420 return string_vector;
421}
422
Ole Troan58492a82018-09-04 13:19:12 +0200423stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800424stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200425{
Ole Troan58492a82018-09-04 13:19:12 +0200426 stat_segment_directory_entry_t *ep;
427 stat_segment_data_t *res = 0;
428 stat_segment_access_t sa;
429
Paul Vinciguerra56421092018-11-21 16:34:09 -0800430 stat_segment_access_start (&sa, sm);
Ole Troan58492a82018-09-04 13:19:12 +0200431
432 /* Collect counter */
433 ep = vec_elt_at_index (sm->directory_vector, index);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800434 vec_add1 (res, copy_data (ep, sm));
Ole Troan58492a82018-09-04 13:19:12 +0200435
Paul Vinciguerra56421092018-11-21 16:34:09 -0800436 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200437 return res;
438 return 0;
439}
440
Paul Vinciguerra56421092018-11-21 16:34:09 -0800441stat_segment_data_t *
442stat_segment_dump_entry (uint32_t index)
443{
444 stat_client_main_t *sm = &stat_client_main;
445 return stat_segment_dump_entry_r (index, sm);
446}
447
Ole Troan58492a82018-09-04 13:19:12 +0200448char *
449stat_segment_index_to_name (uint32_t index)
450{
451 char *name;
452 stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
453 stat_segment_directory_entry_t *ep;
454 ep = vec_elt_at_index (counter_vec, index);
455 name = strdup (ep->name);
456 return name;
457}
458
Ole Troan2fee1672018-08-23 13:00:53 +0200459/*
460 * fd.io coding-style-patch-verification: ON
461 *
462 * Local Variables:
463 * eval: (c-set-style "gnu")
464 * End:
465 */