Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 20 | #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 Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 27 | #include <regex.h> |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | #include <vppinfra/vec.h> |
| 30 | #include <vppinfra/lock.h> |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 31 | #include <stdatomic.h> |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 32 | #include <vlib/vlib.h> |
| 33 | #include <vlib/stats/stats.h> |
Dave Barach | 531969e | 2019-08-14 09:35:41 -0400 | [diff] [blame] | 34 | #include <vpp-api/client/stat_client.h> |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 35 | |
| 36 | stat_client_main_t stat_client_main; |
| 37 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 38 | stat_client_main_t * |
| 39 | stat_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 | |
| 47 | void |
| 48 | stat_client_free (stat_client_main_t * sm) |
| 49 | { |
| 50 | free (sm); |
| 51 | } |
| 52 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 53 | static int |
| 54 | recv_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 Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 85 | static vlib_stats_entry_t * |
| 86 | get_stat_vector_r (stat_client_main_t *sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 87 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 88 | ASSERT (sm->shared_header); |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 89 | return stat_segment_adjust (sm, |
| 90 | (void *) sm->shared_header->directory_vector); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 93 | int |
Neale Ranns | 318d794 | 2018-12-03 01:53:32 -0800 | [diff] [blame] | 94 | stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 95 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 96 | int mfd = -1; |
| 97 | int sock; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 98 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 99 | clib_memset (sm, 0, sizeof (*sm)); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 100 | if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 101 | { |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 102 | perror ("Stat client couldn't open socket"); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 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)) < |
| 110 | 0) |
| 111 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame] | 112 | close (sock); |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 113 | return -2; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 114 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 115 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 116 | if ((mfd = recv_fd (sock)) < 0) |
| 117 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame] | 118 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 119 | fprintf (stderr, "Receiving file descriptor failed\n"); |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 120 | return -3; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 121 | } |
| 122 | close (sock); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 123 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 124 | /* mmap shared memory segment. */ |
| 125 | void *memaddr; |
| 126 | struct stat st = { 0 }; |
| 127 | |
| 128 | if (fstat (mfd, &st) == -1) |
| 129 | { |
Ole Troan | 8ae3a53 | 2019-03-10 21:26:14 +0100 | [diff] [blame] | 130 | close (mfd); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 131 | perror ("mmap fstat failed"); |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 132 | return -4; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 133 | } |
| 134 | if ((memaddr = |
| 135 | mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED) |
| 136 | { |
Ole Troan | 8ae3a53 | 2019-03-10 21:26:14 +0100 | [diff] [blame] | 137 | close (mfd); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 138 | perror ("mmap map failed"); |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 139 | return -5; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Ole Troan | 8ae3a53 | 2019-03-10 21:26:14 +0100 | [diff] [blame] | 142 | close (mfd); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 143 | sm->memory_size = st.st_size; |
| 144 | sm->shared_header = memaddr; |
| 145 | sm->directory_vector = |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 146 | stat_segment_adjust (sm, (void *) sm->shared_header->directory_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 151 | int |
Neale Ranns | 318d794 | 2018-12-03 01:53:32 -0800 | [diff] [blame] | 152 | stat_segment_connect (const char *socket_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 153 | { |
| 154 | stat_client_main_t *sm = &stat_client_main; |
| 155 | return stat_segment_connect_r (socket_name, sm); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | stat_segment_disconnect_r (stat_client_main_t * sm) |
| 160 | { |
| 161 | munmap (sm->shared_header, sm->memory_size); |
| 162 | return; |
| 163 | } |
| 164 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 165 | void |
| 166 | stat_segment_disconnect (void) |
| 167 | { |
| 168 | stat_client_main_t *sm = &stat_client_main; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 169 | return stat_segment_disconnect_r (sm); |
| 170 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 171 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 172 | double |
| 173 | stat_segment_heartbeat_r (stat_client_main_t * sm) |
| 174 | { |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 175 | stat_segment_access_t sa; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 176 | vlib_stats_entry_t *ep; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 177 | |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 178 | /* Has directory been updated? */ |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 179 | if (sm->shared_header->epoch != sm->current_epoch) |
| 180 | return 0; |
Ole Troan | fdc6780 | 2020-08-10 11:59:20 +0200 | [diff] [blame] | 181 | if (stat_segment_access_start (&sa, sm)) |
| 182 | return 0; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 183 | 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 Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 189 | double |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 190 | stat_segment_heartbeat (void) |
| 191 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 192 | stat_client_main_t *sm = &stat_client_main; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 193 | return stat_segment_heartbeat_r (sm); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 196 | #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 Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 206 | static counter_t * |
| 207 | stat_vec_simple_init (counter_t c) |
| 208 | { |
| 209 | counter_t *v = 0; |
| 210 | vec_add1 (v, c); |
| 211 | return v; |
| 212 | } |
| 213 | |
| 214 | static vlib_counter_t * |
| 215 | stat_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 Ranns | 796c84b | 2019-03-28 07:56:23 -0700 | [diff] [blame] | 226 | static stat_segment_data_t |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 227 | copy_data (vlib_stats_entry_t *ep, u32 index2, char *name, |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 228 | stat_client_main_t *sm) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 229 | { |
| 230 | stat_segment_data_t result = { 0 }; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 231 | int i; |
| 232 | vlib_counter_t **combined_c; /* Combined counter */ |
| 233 | counter_t **simple_c; /* Simple counter */ |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 234 | uint64_t *error_vector; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 235 | |
| 236 | assert (sm->shared_header); |
| 237 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 238 | result.type = ep->type; |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 239 | result.name = strdup (name ? name : ep->name); |
| 240 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 241 | switch (ep->type) |
| 242 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 243 | case STAT_DIR_TYPE_SCALAR_INDEX: |
| 244 | result.scalar_value = ep->value; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 248 | simple_c = stat_segment_adjust (sm, ep->data); |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 249 | result.simple_counter_vec = stat_vec_dup (sm, simple_c); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 250 | for (i = 0; i < vec_len (simple_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 251 | { |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 252 | counter_t *cb = stat_segment_adjust (sm, simple_c[i]); |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 253 | 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 Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 257 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 261 | combined_c = stat_segment_adjust (sm, ep->data); |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 262 | result.combined_counter_vec = stat_vec_dup (sm, combined_c); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 263 | for (i = 0; i < vec_len (combined_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 264 | { |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 265 | vlib_counter_t *cb = stat_segment_adjust (sm, combined_c[i]); |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 266 | 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 Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 271 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 272 | break; |
| 273 | |
| 274 | case STAT_DIR_TYPE_ERROR_INDEX: |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 275 | /* Gather errors from all threads into a vector */ |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 276 | error_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++) |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 280 | { |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 281 | counter_t *cb = stat_segment_adjust (sm, (void *) error_vector[i]); |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 282 | result.error_vector[i] = cb[ep->index]; |
| 283 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 284 | break; |
| 285 | |
Ole Troan | 703908a | 2019-02-26 16:37:03 +0100 | [diff] [blame] | 286 | case STAT_DIR_TYPE_NAME_VECTOR: |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 287 | { |
| 288 | uint8_t **name_vector = stat_segment_adjust (sm, ep->data); |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 289 | result.name_vector = stat_vec_dup (sm, name_vector); |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 290 | for (i = 0; i < vec_len (name_vector); i++) |
| 291 | { |
| 292 | u8 *name = stat_segment_adjust (sm, name_vector[i]); |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 293 | result.name_vector[i] = stat_vec_dup (sm, name); |
Ole Troan | 7d29e32 | 2020-07-21 08:46:08 +0200 | [diff] [blame] | 294 | } |
| 295 | } |
Ole Troan | 703908a | 2019-02-26 16:37:03 +0100 | [diff] [blame] | 296 | break; |
| 297 | |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 298 | case STAT_DIR_TYPE_SYMLINK: |
| 299 | /* Gather info from all threads into a vector */ |
| 300 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 301 | vlib_stats_entry_t *ep2; |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 302 | ep2 = vec_elt_at_index (sm->directory_vector, ep->index1); |
| 303 | return copy_data (ep2, ep->index2, ep->name, sm); |
| 304 | } |
| 305 | |
Vladimir Isaev | 72e31bc | 2020-02-04 11:54:27 +0300 | [diff] [blame] | 306 | case STAT_DIR_TYPE_EMPTY: |
| 307 | break; |
| 308 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 309 | default: |
Ole Troan | 703908a | 2019-02-26 16:37:03 +0100 | [diff] [blame] | 310 | fprintf (stderr, "Unknown type: %d\n", ep->type); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 311 | } |
| 312 | return result; |
| 313 | } |
| 314 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 315 | void |
| 316 | stat_segment_data_free (stat_segment_data_t * res) |
| 317 | { |
| 318 | int i, j; |
| 319 | for (i = 0; i < vec_len (res); i++) |
| 320 | { |
| 321 | switch (res[i].type) |
| 322 | { |
| 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); |
| 327 | break; |
| 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); |
| 332 | break; |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 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); |
| 337 | break; |
| 338 | case STAT_DIR_TYPE_ERROR_INDEX: |
| 339 | vec_free (res[i].error_vector); |
| 340 | break; |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 341 | case STAT_DIR_TYPE_SCALAR_INDEX: |
Steven Luong | 0a21480 | 2022-01-28 14:12:53 -0800 | [diff] [blame] | 342 | case STAT_DIR_TYPE_EMPTY: |
Ole Troan | 65c56c8 | 2020-10-21 11:55:28 +0200 | [diff] [blame] | 343 | break; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 344 | default: |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 345 | assert (0); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 346 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 347 | free (res[i].name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 348 | } |
| 349 | vec_free (res); |
| 350 | } |
| 351 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 352 | uint32_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 353 | stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 354 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 355 | stat_segment_access_t sa; |
| 356 | |
| 357 | uint32_t *dir = 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 358 | regex_t regex[vec_len (patterns)]; |
| 359 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 360 | int i, j; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 361 | for (i = 0; i < vec_len (patterns); i++) |
| 362 | { |
Mohsin Kazmi | 90a9fdf | 2018-12-04 14:31:15 +0000 | [diff] [blame] | 363 | int rv = regcomp (®ex[i], (const char *) patterns[i], 0); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 364 | if (rv) |
| 365 | { |
| 366 | fprintf (stderr, "Could not compile regex %s\n", patterns[i]); |
| 367 | return dir; |
| 368 | } |
| 369 | } |
| 370 | |
Ole Troan | fdc6780 | 2020-08-10 11:59:20 +0200 | [diff] [blame] | 371 | if (stat_segment_access_start (&sa, sm)) |
| 372 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 373 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 374 | vlib_stats_entry_t *counter_vec = get_stat_vector_r (sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 375 | for (j = 0; j < vec_len (counter_vec); j++) |
| 376 | { |
| 377 | for (i = 0; i < vec_len (patterns); i++) |
| 378 | { |
| 379 | int rv = regexec (®ex[i], counter_vec[j].name, 0, NULL, 0); |
| 380 | if (rv == 0) |
| 381 | { |
| 382 | vec_add1 (dir, j); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | if (vec_len (patterns) == 0) |
| 387 | vec_add1 (dir, j); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 388 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 389 | |
| 390 | for (i = 0; i < vec_len (patterns); i++) |
| 391 | regfree (®ex[i]); |
| 392 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 393 | if (!stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 394 | { |
| 395 | /* Failed, clean up */ |
| 396 | vec_free (dir); |
| 397 | return 0; |
| 398 | |
| 399 | } |
| 400 | |
| 401 | /* Update last version */ |
| 402 | sm->current_epoch = sa.epoch; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 403 | return dir; |
| 404 | } |
| 405 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 406 | uint32_t * |
| 407 | stat_segment_ls (uint8_t ** patterns) |
| 408 | { |
| 409 | stat_client_main_t *sm = &stat_client_main; |
| 410 | return stat_segment_ls_r ((uint8_t **) patterns, sm); |
| 411 | } |
| 412 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 413 | stat_segment_data_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 414 | stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 415 | { |
| 416 | int i; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 417 | vlib_stats_entry_t *ep; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 418 | stat_segment_data_t *res = 0; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 419 | stat_segment_access_t sa; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 420 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 421 | /* Has directory been update? */ |
| 422 | if (sm->shared_header->epoch != sm->current_epoch) |
| 423 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 424 | |
Ole Troan | fdc6780 | 2020-08-10 11:59:20 +0200 | [diff] [blame] | 425 | if (stat_segment_access_start (&sa, sm)) |
| 426 | return 0; |
| 427 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 428 | for (i = 0; i < vec_len (stats); i++) |
| 429 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 430 | /* Collect counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 431 | ep = vec_elt_at_index (sm->directory_vector, stats[i]); |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 432 | vec_add1 (res, copy_data (ep, ~0, 0, sm)); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 433 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 434 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 435 | if (stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 436 | return res; |
| 437 | |
| 438 | fprintf (stderr, "Epoch changed while reading, invalid results\n"); |
| 439 | // TODO increase counter |
| 440 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 443 | stat_segment_data_t * |
| 444 | stat_segment_dump (uint32_t * stats) |
| 445 | { |
| 446 | stat_client_main_t *sm = &stat_client_main; |
| 447 | return stat_segment_dump_r (stats, sm); |
| 448 | } |
| 449 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 450 | /* Wrapper for accessing vectors from other languages */ |
| 451 | int |
| 452 | stat_segment_vec_len (void *vec) |
| 453 | { |
| 454 | return vec_len (vec); |
| 455 | } |
| 456 | |
Ole Troan | 8254018 | 2018-10-22 09:41:29 +0200 | [diff] [blame] | 457 | void |
| 458 | stat_segment_vec_free (void *vec) |
| 459 | { |
| 460 | vec_free (vec); |
| 461 | } |
| 462 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 463 | /* Create a vector from a string (or add to existing) */ |
Mohsin Kazmi | 90a9fdf | 2018-12-04 14:31:15 +0000 | [diff] [blame] | 464 | uint8_t ** |
| 465 | stat_segment_string_vector (uint8_t ** string_vector, const char *string) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 466 | { |
Mohsin Kazmi | 90a9fdf | 2018-12-04 14:31:15 +0000 | [diff] [blame] | 467 | uint8_t *name = 0; |
| 468 | size_t len = strlen (string); |
| 469 | |
| 470 | vec_validate_init_c_string (name, string, len); |
| 471 | vec_add1 (string_vector, name); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 472 | return string_vector; |
| 473 | } |
| 474 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 475 | stat_segment_data_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 476 | stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 477 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 478 | vlib_stats_entry_t *ep; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 479 | stat_segment_data_t *res = 0; |
| 480 | stat_segment_access_t sa; |
| 481 | |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 482 | /* Has directory been update? */ |
| 483 | if (sm->shared_header->epoch != sm->current_epoch) |
| 484 | return 0; |
| 485 | |
Ole Troan | fdc6780 | 2020-08-10 11:59:20 +0200 | [diff] [blame] | 486 | if (stat_segment_access_start (&sa, sm)) |
| 487 | return 0; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 488 | |
| 489 | /* Collect counter */ |
| 490 | ep = vec_elt_at_index (sm->directory_vector, index); |
Arthur de Kerhor | db02380 | 2021-03-11 10:26:54 -0800 | [diff] [blame] | 491 | vec_add1 (res, copy_data (ep, ~0, 0, sm)); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 492 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 493 | if (stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 494 | return res; |
| 495 | return 0; |
| 496 | } |
| 497 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 498 | stat_segment_data_t * |
| 499 | stat_segment_dump_entry (uint32_t index) |
| 500 | { |
| 501 | stat_client_main_t *sm = &stat_client_main; |
| 502 | return stat_segment_dump_entry_r (index, sm); |
| 503 | } |
| 504 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 505 | char * |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 506 | stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm) |
| 507 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 508 | vlib_stats_entry_t *ep; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 509 | stat_segment_access_t sa; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 510 | vlib_stats_entry_t *vec; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 511 | |
| 512 | /* Has directory been update? */ |
| 513 | if (sm->shared_header->epoch != sm->current_epoch) |
| 514 | return 0; |
Ole Troan | fdc6780 | 2020-08-10 11:59:20 +0200 | [diff] [blame] | 515 | if (stat_segment_access_start (&sa, sm)) |
| 516 | return 0; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 517 | vec = get_stat_vector_r (sm); |
| 518 | ep = vec_elt_at_index (vec, index); |
| 519 | if (!stat_segment_access_end (&sa, sm)) |
| 520 | return 0; |
| 521 | return strdup (ep->name); |
| 522 | } |
| 523 | |
| 524 | char * |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 525 | stat_segment_index_to_name (uint32_t index) |
| 526 | { |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 527 | stat_client_main_t *sm = &stat_client_main; |
| 528 | return stat_segment_index_to_name_r (index, sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Ole Troan | b63dbc5 | 2019-06-14 10:26:14 +0200 | [diff] [blame] | 531 | uint64_t |
| 532 | stat_segment_version_r (stat_client_main_t * sm) |
| 533 | { |
| 534 | ASSERT (sm->shared_header); |
| 535 | return sm->shared_header->version; |
| 536 | } |
| 537 | |
| 538 | uint64_t |
| 539 | stat_segment_version (void) |
| 540 | { |
| 541 | stat_client_main_t *sm = &stat_client_main; |
| 542 | return stat_segment_version_r (sm); |
| 543 | } |
| 544 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 545 | /* |
| 546 | * fd.io coding-style-patch-verification: ON |
| 547 | * |
| 548 | * Local Variables: |
| 549 | * eval: (c-set-style "gnu") |
| 550 | * End: |
| 551 | */ |