blob: d415972de68290a3e172490b3e6853e2db0ea811 [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
34typedef struct
35{
Ole Troan58492a82018-09-04 13:19:12 +020036 uint64_t current_epoch;
37 stat_segment_shared_header_t *shared_header;
38 stat_segment_directory_entry_t *directory_vector;
39 ssize_t memory_size;
Ole Troan2fee1672018-08-23 13:00:53 +020040} stat_client_main_t;
41
42stat_client_main_t stat_client_main;
43
Ole Troan58492a82018-09-04 13:19:12 +020044static int
45recv_fd (int sock)
46{
47 struct msghdr msg = { 0 };
48 struct cmsghdr *cmsg;
49 int fd = -1;
50 char iobuf[1];
51 struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
52 union
53 {
54 char buf[CMSG_SPACE (sizeof (fd))];
55 struct cmsghdr align;
56 } u;
57 msg.msg_iov = &io;
58 msg.msg_iovlen = 1;
59 msg.msg_control = u.buf;
60 msg.msg_controllen = sizeof (u.buf);
61
62 ssize_t size;
63 if ((size = recvmsg (sock, &msg, 0)) < 0)
64 {
65 perror ("recvmsg failed");
66 return -1;
67 }
68 cmsg = CMSG_FIRSTHDR (&msg);
69 if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
70 {
71 memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
72 }
73 return fd;
74}
75
76static stat_segment_directory_entry_t *
77get_stat_vector (void)
78{
79 stat_client_main_t *sm = &stat_client_main;
80 ASSERT (sm->shared_header);
81 return stat_segment_pointer (sm->shared_header,
82 sm->shared_header->directory_offset);
83}
84
Ole Troan2fee1672018-08-23 13:00:53 +020085int
86stat_segment_connect (char *socket_name)
87{
88 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +020089 int mfd = -1;
90 int sock;
Ole Troan2fee1672018-08-23 13:00:53 +020091
92 memset (sm, 0, sizeof (*sm));
Ole Troan58492a82018-09-04 13:19:12 +020093 if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
Ole Troan2fee1672018-08-23 13:00:53 +020094 {
Ole Troan58492a82018-09-04 13:19:12 +020095 perror ("Couldn't open socket");
Ole Troan2fee1672018-08-23 13:00:53 +020096 return -1;
97 }
98
Ole Troan58492a82018-09-04 13:19:12 +020099 struct sockaddr_un un = { 0 };
100 un.sun_family = AF_UNIX;
101 strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
102 if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
103 0)
104 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400105 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200106 perror ("connect");
107 return -1;
108 }
Ole Troan2fee1672018-08-23 13:00:53 +0200109
Ole Troan58492a82018-09-04 13:19:12 +0200110 if ((mfd = recv_fd (sock)) < 0)
111 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400112 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200113 fprintf (stderr, "Receiving file descriptor failed\n");
114 return -1;
115 }
116 close (sock);
Ole Troan2fee1672018-08-23 13:00:53 +0200117
Ole Troan58492a82018-09-04 13:19:12 +0200118 /* mmap shared memory segment. */
119 void *memaddr;
120 struct stat st = { 0 };
121
122 if (fstat (mfd, &st) == -1)
123 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400124 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200125 perror ("mmap");
126 return -1;
127 }
128 if ((memaddr =
129 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
130 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400131 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200132 perror ("mmap");
133 return -1;
134 }
135
136 sm->memory_size = st.st_size;
137 sm->shared_header = memaddr;
138 sm->directory_vector =
139 stat_segment_pointer (memaddr, sm->shared_header->directory_offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200140
141 return 0;
142}
143
144void
145stat_segment_disconnect (void)
146{
147 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200148 munmap (sm->shared_header, sm->memory_size);
149
Ole Troan2fee1672018-08-23 13:00:53 +0200150 return;
151}
152
Ole Troan58492a82018-09-04 13:19:12 +0200153double
Ole Troan2fee1672018-08-23 13:00:53 +0200154stat_segment_heartbeat (void)
155{
Ole Troan2fee1672018-08-23 13:00:53 +0200156 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200157 stat_segment_directory_entry_t *vec = get_stat_vector ();
158 double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset);
159 return *hb;
Ole Troan2fee1672018-08-23 13:00:53 +0200160}
161
162stat_segment_data_t
Ole Troan58492a82018-09-04 13:19:12 +0200163copy_data (stat_segment_directory_entry_t * ep)
Ole Troan2fee1672018-08-23 13:00:53 +0200164{
Ole Troan58492a82018-09-04 13:19:12 +0200165 stat_client_main_t *sm = &stat_client_main;
Ole Troan2fee1672018-08-23 13:00:53 +0200166 stat_segment_data_t result = { 0 };
Ole Troan2fee1672018-08-23 13:00:53 +0200167 int i;
168 vlib_counter_t **combined_c; /* Combined counter */
169 counter_t **simple_c; /* Simple counter */
Ole Troan58492a82018-09-04 13:19:12 +0200170 counter_t *error_base;
171 uint64_t *offset_vector;
172
173 assert (sm->shared_header);
174
Ole Troan2fee1672018-08-23 13:00:53 +0200175 result.type = ep->type;
Ole Troan58492a82018-09-04 13:19:12 +0200176 result.name = strdup (ep->name);
Ole Troan2fee1672018-08-23 13:00:53 +0200177 switch (ep->type)
178 {
Ole Troan58492a82018-09-04 13:19:12 +0200179 case STAT_DIR_TYPE_SCALAR_INDEX:
180 result.scalar_value = ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200181 break;
182
183 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
Ole Troanbf495902018-10-02 22:00:29 +0200184 if (ep->offset == 0)
185 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200186 simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200187 result.simple_counter_vec = vec_dup (simple_c);
Ole Troan58492a82018-09-04 13:19:12 +0200188 offset_vector =
189 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200190 for (i = 0; i < vec_len (simple_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200191 {
192 counter_t *cb =
193 stat_segment_pointer (sm->shared_header, offset_vector[i]);
194 result.simple_counter_vec[i] = vec_dup (cb);
195 }
Ole Troan2fee1672018-08-23 13:00:53 +0200196 break;
197
198 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
Ole Troanbf495902018-10-02 22:00:29 +0200199 if (ep->offset == 0)
200 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200201 combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200202 result.combined_counter_vec = vec_dup (combined_c);
Ole Troan58492a82018-09-04 13:19:12 +0200203 offset_vector =
204 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200205 for (i = 0; i < vec_len (combined_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200206 {
207 vlib_counter_t *cb =
208 stat_segment_pointer (sm->shared_header, offset_vector[i]);
209 result.combined_counter_vec[i] = vec_dup (cb);
210 }
Ole Troan2fee1672018-08-23 13:00:53 +0200211 break;
212
213 case STAT_DIR_TYPE_ERROR_INDEX:
Ole Troan58492a82018-09-04 13:19:12 +0200214 error_base =
215 stat_segment_pointer (sm->shared_header,
216 sm->shared_header->error_offset);
217 result.error_value = error_base[ep->index];
Ole Troan2fee1672018-08-23 13:00:53 +0200218 break;
219
220 default:
221 fprintf (stderr, "Unknown type: %d", ep->type);
222 }
223 return result;
224}
225
Ole Troan2fee1672018-08-23 13:00:53 +0200226void
227stat_segment_data_free (stat_segment_data_t * res)
228{
229 int i, j;
230 for (i = 0; i < vec_len (res); i++)
231 {
232 switch (res[i].type)
233 {
234 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
235 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
236 vec_free (res[i].simple_counter_vec[j]);
237 vec_free (res[i].simple_counter_vec);
238 break;
239 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
240 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
241 vec_free (res[i].combined_counter_vec[j]);
242 vec_free (res[i].combined_counter_vec);
243 break;
244 default:
245 ;
246 }
Ole Troan58492a82018-09-04 13:19:12 +0200247 free (res[i].name);
Ole Troan2fee1672018-08-23 13:00:53 +0200248 }
249 vec_free (res);
250}
251
Ole Troan58492a82018-09-04 13:19:12 +0200252
253typedef struct
254{
255 uint64_t epoch;
256} stat_segment_access_t;
257
258static void
259stat_segment_access_start (stat_segment_access_t * sa)
Ole Troan2fee1672018-08-23 13:00:53 +0200260{
261 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200262 stat_segment_shared_header_t *shared_header = sm->shared_header;
263 sa->epoch = shared_header->epoch;
264 while (shared_header->in_progress != 0)
265 ;
266}
267
268static bool
269stat_segment_access_end (stat_segment_access_t * sa)
270{
271 stat_client_main_t *sm = &stat_client_main;
272 stat_segment_shared_header_t *shared_header = sm->shared_header;
273
274 if (shared_header->epoch != sa->epoch || shared_header->in_progress)
275 return false;
276 return true;
277}
278
279uint32_t *
280stat_segment_ls (uint8_t ** patterns)
281{
282 stat_client_main_t *sm = &stat_client_main;
283 stat_segment_access_t sa;
284
285 uint32_t *dir = 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200286 regex_t regex[vec_len (patterns)];
287
Ole Troan58492a82018-09-04 13:19:12 +0200288 int i, j;
Ole Troan2fee1672018-08-23 13:00:53 +0200289 for (i = 0; i < vec_len (patterns); i++)
290 {
291 int rv = regcomp (&regex[i], (char *) patterns[i], 0);
292 if (rv)
293 {
294 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
295 return dir;
296 }
297 }
298
Ole Troan58492a82018-09-04 13:19:12 +0200299 stat_segment_access_start (&sa);
Ole Troan2fee1672018-08-23 13:00:53 +0200300
Ole Troan58492a82018-09-04 13:19:12 +0200301 stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
302 for (j = 0; j < vec_len (counter_vec); j++)
303 {
304 for (i = 0; i < vec_len (patterns); i++)
305 {
306 int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
307 if (rv == 0)
308 {
309 vec_add1 (dir, j);
310 break;
311 }
312 }
313 if (vec_len (patterns) == 0)
314 vec_add1 (dir, j);
Ole Troan2fee1672018-08-23 13:00:53 +0200315 }
Ole Troan2fee1672018-08-23 13:00:53 +0200316
317 for (i = 0; i < vec_len (patterns); i++)
318 regfree (&regex[i]);
319
Ole Troan58492a82018-09-04 13:19:12 +0200320 if (!stat_segment_access_end (&sa))
321 {
322 /* Failed, clean up */
323 vec_free (dir);
324 return 0;
325
326 }
327
328 /* Update last version */
329 sm->current_epoch = sa.epoch;
Ole Troan2fee1672018-08-23 13:00:53 +0200330 return dir;
331}
332
333stat_segment_data_t *
Ole Troan58492a82018-09-04 13:19:12 +0200334stat_segment_dump (uint32_t * stats)
Ole Troan2fee1672018-08-23 13:00:53 +0200335{
336 int i;
Ole Troan2fee1672018-08-23 13:00:53 +0200337 stat_client_main_t *sm = &stat_client_main;
338 stat_segment_directory_entry_t *ep;
339 stat_segment_data_t *res = 0;
Ole Troan58492a82018-09-04 13:19:12 +0200340 stat_segment_access_t sa;
Ole Troan2fee1672018-08-23 13:00:53 +0200341
Ole Troan58492a82018-09-04 13:19:12 +0200342 /* Has directory been update? */
343 if (sm->shared_header->epoch != sm->current_epoch)
344 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200345
Ole Troan58492a82018-09-04 13:19:12 +0200346 stat_segment_access_start (&sa);
Ole Troan2fee1672018-08-23 13:00:53 +0200347 for (i = 0; i < vec_len (stats); i++)
348 {
Ole Troan2fee1672018-08-23 13:00:53 +0200349 /* Collect counter */
Ole Troan58492a82018-09-04 13:19:12 +0200350 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
351 vec_add1 (res, copy_data (ep));
Ole Troan2fee1672018-08-23 13:00:53 +0200352 }
Ole Troan2fee1672018-08-23 13:00:53 +0200353
Ole Troan58492a82018-09-04 13:19:12 +0200354 if (stat_segment_access_end (&sa))
355 return res;
356
357 fprintf (stderr, "Epoch changed while reading, invalid results\n");
358 // TODO increase counter
359 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200360}
361
Ole Troan73202102018-08-31 00:29:48 +0200362/* Wrapper for accessing vectors from other languages */
363int
364stat_segment_vec_len (void *vec)
365{
366 return vec_len (vec);
367}
368
369/* Create a vector from a string (or add to existing) */
370u8 **
371stat_segment_string_vector (u8 ** string_vector, char *string)
372{
373 u8 *name = 0;
374 name = vec_dup ((u8 *) string);
375 vec_add1 (string_vector, (u8 *) name);
376 return string_vector;
377}
378
Ole Troan58492a82018-09-04 13:19:12 +0200379stat_segment_data_t *
380stat_segment_dump_entry (uint32_t index)
381{
382 stat_client_main_t *sm = &stat_client_main;
383 stat_segment_directory_entry_t *ep;
384 stat_segment_data_t *res = 0;
385 stat_segment_access_t sa;
386
387 stat_segment_access_start (&sa);
388
389 /* Collect counter */
390 ep = vec_elt_at_index (sm->directory_vector, index);
391 vec_add1 (res, copy_data (ep));
392
393 if (stat_segment_access_end (&sa))
394 return res;
395 return 0;
396}
397
398char *
399stat_segment_index_to_name (uint32_t index)
400{
401 char *name;
402 stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
403 stat_segment_directory_entry_t *ep;
404 ep = vec_elt_at_index (counter_vec, index);
405 name = strdup (ep->name);
406 return name;
407}
408
Ole Troan2fee1672018-08-23 13:00:53 +0200409/*
410 * fd.io coding-style-patch-verification: ON
411 *
412 * Local Variables:
413 * eval: (c-set-style "gnu")
414 * End:
415 */