blob: 0042a2be4e43a21029fb2018b483a436fa0e83fd [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 {
124 perror ("mmap");
125 return -1;
126 }
127 if ((memaddr =
128 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
129 {
130 perror ("mmap");
131 return -1;
132 }
133
134 sm->memory_size = st.st_size;
135 sm->shared_header = memaddr;
136 sm->directory_vector =
137 stat_segment_pointer (memaddr, sm->shared_header->directory_offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200138
139 return 0;
140}
141
142void
143stat_segment_disconnect (void)
144{
145 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200146 munmap (sm->shared_header, sm->memory_size);
147
Ole Troan2fee1672018-08-23 13:00:53 +0200148 return;
149}
150
Ole Troan58492a82018-09-04 13:19:12 +0200151double
Ole Troan2fee1672018-08-23 13:00:53 +0200152stat_segment_heartbeat (void)
153{
Ole Troan2fee1672018-08-23 13:00:53 +0200154 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200155 stat_segment_directory_entry_t *vec = get_stat_vector ();
156 double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset);
157 return *hb;
Ole Troan2fee1672018-08-23 13:00:53 +0200158}
159
160stat_segment_data_t
Ole Troan58492a82018-09-04 13:19:12 +0200161copy_data (stat_segment_directory_entry_t * ep)
Ole Troan2fee1672018-08-23 13:00:53 +0200162{
Ole Troan58492a82018-09-04 13:19:12 +0200163 stat_client_main_t *sm = &stat_client_main;
Ole Troan2fee1672018-08-23 13:00:53 +0200164 stat_segment_data_t result = { 0 };
Ole Troan2fee1672018-08-23 13:00:53 +0200165 int i;
166 vlib_counter_t **combined_c; /* Combined counter */
167 counter_t **simple_c; /* Simple counter */
Ole Troan58492a82018-09-04 13:19:12 +0200168 counter_t *error_base;
169 uint64_t *offset_vector;
170
171 assert (sm->shared_header);
172
Ole Troan2fee1672018-08-23 13:00:53 +0200173 result.type = ep->type;
Ole Troan58492a82018-09-04 13:19:12 +0200174 result.name = strdup (ep->name);
Ole Troan2fee1672018-08-23 13:00:53 +0200175 switch (ep->type)
176 {
Ole Troan58492a82018-09-04 13:19:12 +0200177 case STAT_DIR_TYPE_SCALAR_INDEX:
178 result.scalar_value = ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200179 break;
180
181 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
Ole Troanbf495902018-10-02 22:00:29 +0200182 if (ep->offset == 0)
183 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200184 simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200185 result.simple_counter_vec = vec_dup (simple_c);
Ole Troan58492a82018-09-04 13:19:12 +0200186 offset_vector =
187 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200188 for (i = 0; i < vec_len (simple_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200189 {
190 counter_t *cb =
191 stat_segment_pointer (sm->shared_header, offset_vector[i]);
192 result.simple_counter_vec[i] = vec_dup (cb);
193 }
Ole Troan2fee1672018-08-23 13:00:53 +0200194 break;
195
196 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
Ole Troanbf495902018-10-02 22:00:29 +0200197 if (ep->offset == 0)
198 return result;
Ole Troan58492a82018-09-04 13:19:12 +0200199 combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
Ole Troan2fee1672018-08-23 13:00:53 +0200200 result.combined_counter_vec = vec_dup (combined_c);
Ole Troan58492a82018-09-04 13:19:12 +0200201 offset_vector =
202 stat_segment_pointer (sm->shared_header, ep->offset_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200203 for (i = 0; i < vec_len (combined_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200204 {
205 vlib_counter_t *cb =
206 stat_segment_pointer (sm->shared_header, offset_vector[i]);
207 result.combined_counter_vec[i] = vec_dup (cb);
208 }
Ole Troan2fee1672018-08-23 13:00:53 +0200209 break;
210
211 case STAT_DIR_TYPE_ERROR_INDEX:
Ole Troan58492a82018-09-04 13:19:12 +0200212 error_base =
213 stat_segment_pointer (sm->shared_header,
214 sm->shared_header->error_offset);
215 result.error_value = error_base[ep->index];
Ole Troan2fee1672018-08-23 13:00:53 +0200216 break;
217
218 default:
219 fprintf (stderr, "Unknown type: %d", ep->type);
220 }
221 return result;
222}
223
Ole Troan2fee1672018-08-23 13:00:53 +0200224void
225stat_segment_data_free (stat_segment_data_t * res)
226{
227 int i, j;
228 for (i = 0; i < vec_len (res); i++)
229 {
230 switch (res[i].type)
231 {
232 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
233 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
234 vec_free (res[i].simple_counter_vec[j]);
235 vec_free (res[i].simple_counter_vec);
236 break;
237 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
238 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
239 vec_free (res[i].combined_counter_vec[j]);
240 vec_free (res[i].combined_counter_vec);
241 break;
242 default:
243 ;
244 }
Ole Troan58492a82018-09-04 13:19:12 +0200245 free (res[i].name);
Ole Troan2fee1672018-08-23 13:00:53 +0200246 }
247 vec_free (res);
248}
249
Ole Troan58492a82018-09-04 13:19:12 +0200250
251typedef struct
252{
253 uint64_t epoch;
254} stat_segment_access_t;
255
256static void
257stat_segment_access_start (stat_segment_access_t * sa)
Ole Troan2fee1672018-08-23 13:00:53 +0200258{
259 stat_client_main_t *sm = &stat_client_main;
Ole Troan58492a82018-09-04 13:19:12 +0200260 stat_segment_shared_header_t *shared_header = sm->shared_header;
261 sa->epoch = shared_header->epoch;
262 while (shared_header->in_progress != 0)
263 ;
264}
265
266static bool
267stat_segment_access_end (stat_segment_access_t * sa)
268{
269 stat_client_main_t *sm = &stat_client_main;
270 stat_segment_shared_header_t *shared_header = sm->shared_header;
271
272 if (shared_header->epoch != sa->epoch || shared_header->in_progress)
273 return false;
274 return true;
275}
276
277uint32_t *
278stat_segment_ls (uint8_t ** patterns)
279{
280 stat_client_main_t *sm = &stat_client_main;
281 stat_segment_access_t sa;
282
283 uint32_t *dir = 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200284 regex_t regex[vec_len (patterns)];
285
Ole Troan58492a82018-09-04 13:19:12 +0200286 int i, j;
Ole Troan2fee1672018-08-23 13:00:53 +0200287 for (i = 0; i < vec_len (patterns); i++)
288 {
289 int rv = regcomp (&regex[i], (char *) patterns[i], 0);
290 if (rv)
291 {
292 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
293 return dir;
294 }
295 }
296
Ole Troan58492a82018-09-04 13:19:12 +0200297 stat_segment_access_start (&sa);
Ole Troan2fee1672018-08-23 13:00:53 +0200298
Ole Troan58492a82018-09-04 13:19:12 +0200299 stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
300 for (j = 0; j < vec_len (counter_vec); j++)
301 {
302 for (i = 0; i < vec_len (patterns); i++)
303 {
304 int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
305 if (rv == 0)
306 {
307 vec_add1 (dir, j);
308 break;
309 }
310 }
311 if (vec_len (patterns) == 0)
312 vec_add1 (dir, j);
Ole Troan2fee1672018-08-23 13:00:53 +0200313 }
Ole Troan2fee1672018-08-23 13:00:53 +0200314
315 for (i = 0; i < vec_len (patterns); i++)
316 regfree (&regex[i]);
317
Ole Troan58492a82018-09-04 13:19:12 +0200318 if (!stat_segment_access_end (&sa))
319 {
320 /* Failed, clean up */
321 vec_free (dir);
322 return 0;
323
324 }
325
326 /* Update last version */
327 sm->current_epoch = sa.epoch;
Ole Troan2fee1672018-08-23 13:00:53 +0200328 return dir;
329}
330
331stat_segment_data_t *
Ole Troan58492a82018-09-04 13:19:12 +0200332stat_segment_dump (uint32_t * stats)
Ole Troan2fee1672018-08-23 13:00:53 +0200333{
334 int i;
Ole Troan2fee1672018-08-23 13:00:53 +0200335 stat_client_main_t *sm = &stat_client_main;
336 stat_segment_directory_entry_t *ep;
337 stat_segment_data_t *res = 0;
Ole Troan58492a82018-09-04 13:19:12 +0200338 stat_segment_access_t sa;
Ole Troan2fee1672018-08-23 13:00:53 +0200339
Ole Troan58492a82018-09-04 13:19:12 +0200340 /* Has directory been update? */
341 if (sm->shared_header->epoch != sm->current_epoch)
342 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200343
Ole Troan58492a82018-09-04 13:19:12 +0200344 stat_segment_access_start (&sa);
Ole Troan2fee1672018-08-23 13:00:53 +0200345 for (i = 0; i < vec_len (stats); i++)
346 {
Ole Troan2fee1672018-08-23 13:00:53 +0200347 /* Collect counter */
Ole Troan58492a82018-09-04 13:19:12 +0200348 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
349 vec_add1 (res, copy_data (ep));
Ole Troan2fee1672018-08-23 13:00:53 +0200350 }
Ole Troan2fee1672018-08-23 13:00:53 +0200351
Ole Troan58492a82018-09-04 13:19:12 +0200352 if (stat_segment_access_end (&sa))
353 return res;
354
355 fprintf (stderr, "Epoch changed while reading, invalid results\n");
356 // TODO increase counter
357 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200358}
359
Ole Troan73202102018-08-31 00:29:48 +0200360/* Wrapper for accessing vectors from other languages */
361int
362stat_segment_vec_len (void *vec)
363{
364 return vec_len (vec);
365}
366
Ole Troan82540182018-10-22 09:41:29 +0200367void
368stat_segment_vec_free (void *vec)
369{
370 vec_free (vec);
371}
372
Ole Troan73202102018-08-31 00:29:48 +0200373/* Create a vector from a string (or add to existing) */
374u8 **
375stat_segment_string_vector (u8 ** string_vector, char *string)
376{
377 u8 *name = 0;
378 name = vec_dup ((u8 *) string);
379 vec_add1 (string_vector, (u8 *) name);
380 return string_vector;
381}
382
Ole Troan58492a82018-09-04 13:19:12 +0200383stat_segment_data_t *
384stat_segment_dump_entry (uint32_t index)
385{
386 stat_client_main_t *sm = &stat_client_main;
387 stat_segment_directory_entry_t *ep;
388 stat_segment_data_t *res = 0;
389 stat_segment_access_t sa;
390
391 stat_segment_access_start (&sa);
392
393 /* Collect counter */
394 ep = vec_elt_at_index (sm->directory_vector, index);
395 vec_add1 (res, copy_data (ep));
396
397 if (stat_segment_access_end (&sa))
398 return res;
399 return 0;
400}
401
402char *
403stat_segment_index_to_name (uint32_t index)
404{
405 char *name;
406 stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
407 stat_segment_directory_entry_t *ep;
408 ep = vec_elt_at_index (counter_vec, index);
409 name = strdup (ep->name);
410 return name;
411}
412
Ole Troan2fee1672018-08-23 13:00:53 +0200413/*
414 * fd.io coding-style-patch-verification: ON
415 *
416 * Local Variables:
417 * eval: (c-set-style "gnu")
418 * End:
419 */