Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | from cffi import FFI |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 5 | import time |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 6 | |
| 7 | ffi = FFI() |
| 8 | ffi.cdef(""" |
| 9 | typedef uint64_t counter_t; |
| 10 | typedef struct { |
| 11 | counter_t packets; |
| 12 | counter_t bytes; |
| 13 | } vlib_counter_t; |
| 14 | |
| 15 | typedef enum { |
| 16 | STAT_DIR_TYPE_ILLEGAL = 0, |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 17 | STAT_DIR_TYPE_SCALAR_INDEX, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 18 | STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE, |
| 19 | STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED, |
| 20 | STAT_DIR_TYPE_ERROR_INDEX, |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 21 | STAT_DIR_TYPE_NAME_VECTOR, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 22 | } stat_directory_type_t; |
| 23 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 24 | typedef struct |
| 25 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 26 | stat_directory_type_t type; |
| 27 | union { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 28 | uint64_t offset; |
| 29 | uint64_t index; |
| 30 | uint64_t value; |
| 31 | }; |
| 32 | uint64_t offset_vector; |
| 33 | char name[128]; // TODO change this to pointer to "somewhere" |
| 34 | } stat_segment_directory_entry_t; |
| 35 | |
| 36 | typedef struct |
| 37 | { |
| 38 | char *name; |
| 39 | stat_directory_type_t type; |
| 40 | union |
| 41 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 42 | double scalar_value; |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 43 | counter_t *error_vector; |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 44 | counter_t **simple_counter_vec; |
| 45 | vlib_counter_t **combined_counter_vec; |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 46 | uint8_t **name_vector; |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 47 | }; |
| 48 | } stat_segment_data_t; |
| 49 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 50 | typedef struct |
| 51 | { |
| 52 | uint64_t epoch; |
| 53 | uint64_t in_progress; |
| 54 | uint64_t directory_offset; |
| 55 | uint64_t error_offset; |
| 56 | uint64_t stats_offset; |
| 57 | } stat_segment_shared_header_t; |
| 58 | |
| 59 | typedef struct |
| 60 | { |
| 61 | uint64_t current_epoch; |
| 62 | stat_segment_shared_header_t *shared_header; |
| 63 | stat_segment_directory_entry_t *directory_vector; |
| 64 | ssize_t memory_size; |
| 65 | } stat_client_main_t; |
| 66 | |
| 67 | stat_client_main_t * stat_client_get(void); |
| 68 | void stat_client_free(stat_client_main_t * sm); |
| 69 | int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 70 | int stat_segment_connect (char *socket_name); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 71 | void stat_segment_disconnect_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 72 | void stat_segment_disconnect (void); |
| 73 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 74 | uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 75 | uint32_t *stat_segment_ls (uint8_t ** pattern); |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 76 | stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, |
| 77 | stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 78 | stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 79 | void stat_segment_data_free (stat_segment_data_t * res); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 80 | |
| 81 | double stat_segment_heartbeat_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 82 | int stat_segment_vec_len(void *vec); |
| 83 | uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string); |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 84 | char *stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm); |
| 85 | void free(void *ptr); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 86 | """) |
| 87 | |
| 88 | |
| 89 | # Utility functions |
| 90 | def make_string_vector(api, strings): |
| 91 | vec = ffi.NULL |
| 92 | if type(strings) is not list: |
| 93 | strings = [strings] |
| 94 | for s in strings: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 95 | vec = api.stat_segment_string_vector(vec, ffi.new("char []", |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 96 | s.encode('utf-8'))) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 97 | return vec |
| 98 | |
| 99 | |
| 100 | def make_string_list(api, vec): |
| 101 | vec_len = api.stat_segment_vec_len(vec) |
| 102 | return [ffi.string(vec[i]) for i in range(vec_len)] |
| 103 | |
| 104 | |
| 105 | # 2-dimensonal array of thread, index |
| 106 | def simple_counter_vec_list(api, e): |
| 107 | vec = [] |
| 108 | for thread in range(api.stat_segment_vec_len(e)): |
| 109 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 110 | if_per_thread = [e[thread][interfaces] |
| 111 | for interfaces in range(len_interfaces)] |
| 112 | vec.append(if_per_thread) |
| 113 | return vec |
| 114 | |
| 115 | |
| 116 | def vlib_counter_dict(c): |
| 117 | return {'packets': c.packets, |
| 118 | 'bytes': c.bytes} |
| 119 | |
| 120 | |
| 121 | def combined_counter_vec_list(api, e): |
| 122 | vec = [] |
| 123 | for thread in range(api.stat_segment_vec_len(e)): |
| 124 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 125 | if_per_thread = [vlib_counter_dict(e[thread][interfaces]) |
| 126 | for interfaces in range(len_interfaces)] |
| 127 | vec.append(if_per_thread) |
| 128 | return vec |
| 129 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 130 | def error_vec_list(api, e): |
| 131 | vec = [] |
| 132 | for thread in range(api.stat_segment_vec_len(e)): |
| 133 | vec.append(e[thread]) |
| 134 | return vec |
| 135 | |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 136 | def name_vec_list(api, e): |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 137 | return [ffi.string(e[i]).decode('utf-8') for i in |
| 138 | range(api.stat_segment_vec_len(e)) if e[i] != ffi.NULL] |
| 139 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 140 | |
| 141 | def stat_entry_to_python(api, e): |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 142 | # Scalar index |
| 143 | if e.type == 1: |
| 144 | return e.scalar_value |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 145 | if e.type == 2: |
| 146 | return simple_counter_vec_list(api, e.simple_counter_vec) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 147 | if e.type == 3: |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 148 | return combined_counter_vec_list(api, e.combined_counter_vec) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 149 | if e.type == 4: |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 150 | return error_vec_list(api, e.error_vector) |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 151 | if e.type == 5: |
| 152 | return name_vec_list(api, e.name_vector) |
| 153 | raise NotImplementedError() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 154 | |
| 155 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 156 | class VPPStatsIOError(IOError): |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 157 | message = "Stat segment client connection returned: " \ |
| 158 | "%(retval)s %(strerror)s." |
| 159 | |
| 160 | strerror = {-1: "Stat client couldn't open socket", |
| 161 | -2: "Stat client socket open but couldn't connect", |
| 162 | -3: "Receiving file descriptor failed", |
| 163 | -4: "mmap fstat failed", |
| 164 | -5: "mmap map failed" |
| 165 | } |
| 166 | |
| 167 | def __init__(self, message=None, **kwargs): |
| 168 | if 'retval' in kwargs: |
| 169 | self.retval = kwargs['retval'] |
| 170 | kwargs['strerror'] = self.strerror[int(self.retval)] |
| 171 | |
| 172 | if not message: |
| 173 | try: |
| 174 | message = self.message % kwargs |
| 175 | except Exception as e: |
| 176 | message = self.message |
| 177 | else: |
| 178 | message = message % kwargs |
| 179 | |
| 180 | super(VPPStatsIOError, self).__init__(message) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 181 | |
| 182 | |
| 183 | class VPPStatsClientLoadError(RuntimeError): |
| 184 | pass |
| 185 | |
| 186 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 187 | class VPPStats(object): |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 188 | VPPStatsIOError = VPPStatsIOError |
| 189 | |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 190 | default_socketname = '/var/run/vpp/stats.sock' |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 191 | sharedlib_name = 'libvppapiclient.so' |
| 192 | |
| 193 | def __init__(self, socketname=default_socketname, timeout=10): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 194 | try: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 195 | self.api = ffi.dlopen(VPPStats.sharedlib_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 196 | except Exception: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 197 | raise VPPStatsClientLoadError("Could not open: %s" % |
| 198 | VPPStats.sharedlib_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 199 | self.client = self.api.stat_client_get() |
| 200 | |
| 201 | poll_end_time = time.time() + timeout |
| 202 | while time.time() < poll_end_time: |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 203 | rv = self.api.stat_segment_connect_r(socketname.encode('utf-8'), |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 204 | self.client) |
Ole Troan | c424de7 | 2019-06-04 12:33:32 +0200 | [diff] [blame] | 205 | # Break out if success or any other error than "no such file" |
| 206 | # (indicating that VPP hasn't started yet) |
| 207 | if rv == 0 or ffi.errno != 2: |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 208 | break |
| 209 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 210 | if rv != 0: |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 211 | raise VPPStatsIOError(retval=rv) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 212 | |
| 213 | def heartbeat(self): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 214 | return self.api.stat_segment_heartbeat_r(self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 215 | |
| 216 | def ls(self, patterns): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 217 | return self.api.stat_segment_ls_r(make_string_vector(self.api, |
| 218 | patterns), |
| 219 | self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 220 | |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 221 | def lsstr(self, patterns): |
| 222 | rv = self.api.stat_segment_ls_r(make_string_vector(self.api, |
| 223 | patterns), |
| 224 | self.client) |
| 225 | |
| 226 | if rv == ffi.NULL: |
| 227 | raise VPPStatsIOError() |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 228 | return [ffi.string(self.api.stat_segment_index_to_name_r( |
| 229 | rv[i], self.client)).decode('utf-8') |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 230 | for i in range(self.api.stat_segment_vec_len(rv))] |
| 231 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 232 | def dump(self, counters): |
| 233 | stats = {} |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 234 | rv = self.api.stat_segment_dump_r(counters, self.client) |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 235 | # Raise exception and retry |
| 236 | if rv == ffi.NULL: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 237 | raise VPPStatsIOError() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 238 | rv_len = self.api.stat_segment_vec_len(rv) |
Ole Troan | cbb8f58 | 2019-04-15 08:53:46 +0200 | [diff] [blame] | 239 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 240 | for i in range(rv_len): |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 241 | n = ffi.string(rv[i].name).decode('utf-8') |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 242 | e = stat_entry_to_python(self.api, rv[i]) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 243 | if e is not None: |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 244 | stats[n] = e |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 245 | return stats |
| 246 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 247 | def get_counter(self, name): |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 248 | retries = 0 |
| 249 | while True: |
| 250 | try: |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 251 | d = self.ls(name) |
| 252 | s = self.dump(d) |
| 253 | if len(s) > 1: |
| 254 | raise AttributeError('Matches multiple counters {}' |
| 255 | .format(name)) |
| 256 | k, v = s.popitem() |
| 257 | return v |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 258 | except VPPStatsIOError as e: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 259 | if retries > 10: |
| 260 | return None |
| 261 | retries += 1 |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 262 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 263 | def get_err_counter(self, name): |
| 264 | """Get an error counter. The errors from each worker thread |
| 265 | are summed""" |
| 266 | return sum(self.get_counter(name)) |
| 267 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 268 | def disconnect(self): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 269 | self.api.stat_segment_disconnect_r(self.client) |
| 270 | self.api.stat_client_free(self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 271 | |
| 272 | def set_errors(self): |
| 273 | '''Return all errors counters > 0''' |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 274 | retries = 0 |
| 275 | while True: |
| 276 | try: |
| 277 | error_names = self.ls(['/err/']) |
| 278 | error_counters = self.dump(error_names) |
| 279 | break |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 280 | except VPPStatsIOError as e: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 281 | if retries > 10: |
| 282 | return None |
| 283 | retries += 1 |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 284 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 285 | return {k: sum(error_counters[k]) |
| 286 | for k in error_counters.keys() if sum(error_counters[k])} |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 287 | |
| 288 | def set_errors_str(self): |
| 289 | '''Return all errors counters > 0 pretty printed''' |
| 290 | s = 'ERRORS:\n' |
| 291 | error_counters = self.set_errors() |
| 292 | for k in sorted(error_counters): |
| 293 | s += '{:<60}{:>10}\n'.format(k, error_counters[k]) |
| 294 | return s |