blob: aa3409554d0e2488f2fcd4c4047f3df6dd7625f1 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Ole Troan73202102018-08-31 00:29:48 +02002
3from __future__ import print_function
4from cffi import FFI
Paul Vinciguerra56421092018-11-21 16:34:09 -08005import time
Ole Troan73202102018-08-31 00:29:48 +02006
7ffi = FFI()
8ffi.cdef("""
9typedef uint64_t counter_t;
10typedef struct {
11 counter_t packets;
12 counter_t bytes;
13} vlib_counter_t;
14
15typedef enum {
16 STAT_DIR_TYPE_ILLEGAL = 0,
Ole Troan58492a82018-09-04 13:19:12 +020017 STAT_DIR_TYPE_SCALAR_INDEX,
Ole Troan73202102018-08-31 00:29:48 +020018 STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
19 STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
20 STAT_DIR_TYPE_ERROR_INDEX,
Ole Troancbb8f582019-04-15 08:53:46 +020021 STAT_DIR_TYPE_NAME_VECTOR,
Ole Troan73202102018-08-31 00:29:48 +020022} stat_directory_type_t;
23
Ole Troan58492a82018-09-04 13:19:12 +020024typedef struct
25{
Ole Troan73202102018-08-31 00:29:48 +020026 stat_directory_type_t type;
27 union {
Ole Troan58492a82018-09-04 13:19:12 +020028 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
36typedef struct
37{
38 char *name;
39 stat_directory_type_t type;
40 union
41 {
Ole Troan73202102018-08-31 00:29:48 +020042 double scalar_value;
Ole Troan233e4682019-05-16 15:01:34 +020043 counter_t *error_vector;
Ole Troan73202102018-08-31 00:29:48 +020044 counter_t **simple_counter_vec;
45 vlib_counter_t **combined_counter_vec;
Ole Troancbb8f582019-04-15 08:53:46 +020046 uint8_t **name_vector;
Ole Troan73202102018-08-31 00:29:48 +020047 };
48} stat_segment_data_t;
49
Paul Vinciguerra56421092018-11-21 16:34:09 -080050typedef struct
51{
Ole Troanb63dbc52019-06-14 10:26:14 +020052 uint64_t version;
Paul Vinciguerra56421092018-11-21 16:34:09 -080053 uint64_t epoch;
54 uint64_t in_progress;
55 uint64_t directory_offset;
56 uint64_t error_offset;
57 uint64_t stats_offset;
58} stat_segment_shared_header_t;
59
60typedef struct
61{
62 uint64_t current_epoch;
63 stat_segment_shared_header_t *shared_header;
64 stat_segment_directory_entry_t *directory_vector;
65 ssize_t memory_size;
66} stat_client_main_t;
67
68stat_client_main_t * stat_client_get(void);
69void stat_client_free(stat_client_main_t * sm);
70int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020071int stat_segment_connect (char *socket_name);
Paul Vinciguerra56421092018-11-21 16:34:09 -080072void stat_segment_disconnect_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020073void stat_segment_disconnect (void);
74
Paul Vinciguerra56421092018-11-21 16:34:09 -080075uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020076uint32_t *stat_segment_ls (uint8_t ** pattern);
Ole Troan85465582019-04-30 10:04:36 +020077stat_segment_data_t *stat_segment_dump_r (uint32_t * stats,
78 stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020079stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
Ole Troan73202102018-08-31 00:29:48 +020080void stat_segment_data_free (stat_segment_data_t * res);
Paul Vinciguerra56421092018-11-21 16:34:09 -080081
82double stat_segment_heartbeat_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020083int stat_segment_vec_len(void *vec);
84uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string);
Ole Troancbb8f582019-04-15 08:53:46 +020085char *stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm);
Ole Troanb63dbc52019-06-14 10:26:14 +020086uint64_t stat_segment_version(void);
87uint64_t stat_segment_version_r(stat_client_main_t *sm);
Ole Troancbb8f582019-04-15 08:53:46 +020088void free(void *ptr);
Paul Vinciguerraae8819f2019-06-07 13:35:37 -040089""") # noqa: E501
Ole Troan73202102018-08-31 00:29:48 +020090
91
92# Utility functions
93def make_string_vector(api, strings):
94 vec = ffi.NULL
95 if type(strings) is not list:
96 strings = [strings]
97 for s in strings:
Ole Troan31555a32018-10-22 09:30:26 +020098 vec = api.stat_segment_string_vector(vec, ffi.new("char []",
Paul Vinciguerra3d04e332019-03-19 17:32:39 -070099 s.encode('utf-8')))
Ole Troan73202102018-08-31 00:29:48 +0200100 return vec
101
102
103def make_string_list(api, vec):
104 vec_len = api.stat_segment_vec_len(vec)
105 return [ffi.string(vec[i]) for i in range(vec_len)]
106
107
108# 2-dimensonal array of thread, index
109def simple_counter_vec_list(api, e):
110 vec = []
111 for thread in range(api.stat_segment_vec_len(e)):
112 len_interfaces = api.stat_segment_vec_len(e[thread])
113 if_per_thread = [e[thread][interfaces]
114 for interfaces in range(len_interfaces)]
115 vec.append(if_per_thread)
116 return vec
117
118
119def vlib_counter_dict(c):
120 return {'packets': c.packets,
121 'bytes': c.bytes}
122
123
124def combined_counter_vec_list(api, e):
125 vec = []
126 for thread in range(api.stat_segment_vec_len(e)):
127 len_interfaces = api.stat_segment_vec_len(e[thread])
128 if_per_thread = [vlib_counter_dict(e[thread][interfaces])
129 for interfaces in range(len_interfaces)]
130 vec.append(if_per_thread)
131 return vec
132
Paul Vinciguerraae8819f2019-06-07 13:35:37 -0400133
Ole Troan233e4682019-05-16 15:01:34 +0200134def error_vec_list(api, e):
135 vec = []
136 for thread in range(api.stat_segment_vec_len(e)):
137 vec.append(e[thread])
138 return vec
139
Paul Vinciguerraae8819f2019-06-07 13:35:37 -0400140
Ole Troancbb8f582019-04-15 08:53:46 +0200141def name_vec_list(api, e):
Ole Troan85465582019-04-30 10:04:36 +0200142 return [ffi.string(e[i]).decode('utf-8') for i in
143 range(api.stat_segment_vec_len(e)) if e[i] != ffi.NULL]
144
Ole Troan73202102018-08-31 00:29:48 +0200145
146def stat_entry_to_python(api, e):
Ole Troan58492a82018-09-04 13:19:12 +0200147 # Scalar index
148 if e.type == 1:
149 return e.scalar_value
Ole Troan58492a82018-09-04 13:19:12 +0200150 if e.type == 2:
151 return simple_counter_vec_list(api, e.simple_counter_vec)
Ole Troan73202102018-08-31 00:29:48 +0200152 if e.type == 3:
Ole Troan73202102018-08-31 00:29:48 +0200153 return combined_counter_vec_list(api, e.combined_counter_vec)
Ole Troan58492a82018-09-04 13:19:12 +0200154 if e.type == 4:
Ole Troan233e4682019-05-16 15:01:34 +0200155 return error_vec_list(api, e.error_vector)
Ole Troancbb8f582019-04-15 08:53:46 +0200156 if e.type == 5:
157 return name_vec_list(api, e.name_vector)
158 raise NotImplementedError()
Ole Troan73202102018-08-31 00:29:48 +0200159
160
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800161class VPPStatsIOError(IOError):
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800162 message = "Stat segment client connection returned: " \
163 "%(retval)s %(strerror)s."
164
165 strerror = {-1: "Stat client couldn't open socket",
166 -2: "Stat client socket open but couldn't connect",
167 -3: "Receiving file descriptor failed",
168 -4: "mmap fstat failed",
169 -5: "mmap map failed"
170 }
171
172 def __init__(self, message=None, **kwargs):
173 if 'retval' in kwargs:
174 self.retval = kwargs['retval']
175 kwargs['strerror'] = self.strerror[int(self.retval)]
176
177 if not message:
178 try:
179 message = self.message % kwargs
Paul Vinciguerraae8819f2019-06-07 13:35:37 -0400180 except Exception:
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800181 message = self.message
182 else:
183 message = message % kwargs
184
185 super(VPPStatsIOError, self).__init__(message)
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800186
187
188class VPPStatsClientLoadError(RuntimeError):
189 pass
190
191
Paul Vinciguerra7e713f12018-11-26 12:04:48 -0800192class VPPStats(object):
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800193 VPPStatsIOError = VPPStatsIOError
194
YohanPipereau71dd9d52019-06-06 16:34:14 +0200195 default_socketname = '/run/vpp/stats.sock'
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800196 sharedlib_name = 'libvppapiclient.so'
197
198 def __init__(self, socketname=default_socketname, timeout=10):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500199 self.socketname = socketname
200 self.timeout = timeout
201 self.connected = False
Paul Vinciguerra56421092018-11-21 16:34:09 -0800202 try:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800203 self.api = ffi.dlopen(VPPStats.sharedlib_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800204 except Exception:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800205 raise VPPStatsClientLoadError("Could not open: %s" %
206 VPPStats.sharedlib_name)
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500207
208 def connect(self):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800209 self.client = self.api.stat_client_get()
210
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500211 poll_end_time = time.time() + self.timeout
Paul Vinciguerra56421092018-11-21 16:34:09 -0800212 while time.time() < poll_end_time:
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500213 rv = self.api.stat_segment_connect_r(
214 self.socketname.encode('utf-8'), self.client)
Ole Troanc424de72019-06-04 12:33:32 +0200215 # Break out if success or any other error than "no such file"
216 # (indicating that VPP hasn't started yet)
217 if rv == 0 or ffi.errno != 2:
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500218 self.connected = True
Paul Vinciguerra56421092018-11-21 16:34:09 -0800219 break
220
Ole Troan73202102018-08-31 00:29:48 +0200221 if rv != 0:
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800222 raise VPPStatsIOError(retval=rv)
Ole Troan73202102018-08-31 00:29:48 +0200223
224 def heartbeat(self):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500225 if not self.connected:
226 self.connect()
Paul Vinciguerra56421092018-11-21 16:34:09 -0800227 return self.api.stat_segment_heartbeat_r(self.client)
Ole Troan73202102018-08-31 00:29:48 +0200228
229 def ls(self, patterns):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500230 if not self.connected:
231 self.connect()
Paul Vinciguerra56421092018-11-21 16:34:09 -0800232 return self.api.stat_segment_ls_r(make_string_vector(self.api,
233 patterns),
234 self.client)
Ole Troan73202102018-08-31 00:29:48 +0200235
Ole Troancbb8f582019-04-15 08:53:46 +0200236 def lsstr(self, patterns):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500237 if not self.connected:
238 self.connect()
Ole Troancbb8f582019-04-15 08:53:46 +0200239 rv = self.api.stat_segment_ls_r(make_string_vector(self.api,
240 patterns),
241 self.client)
242
243 if rv == ffi.NULL:
244 raise VPPStatsIOError()
Ole Troan85465582019-04-30 10:04:36 +0200245 return [ffi.string(self.api.stat_segment_index_to_name_r(
246 rv[i], self.client)).decode('utf-8')
Ole Troancbb8f582019-04-15 08:53:46 +0200247 for i in range(self.api.stat_segment_vec_len(rv))]
248
Ole Troan73202102018-08-31 00:29:48 +0200249 def dump(self, counters):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500250 if not self.connected:
251 self.connect()
Ole Troan73202102018-08-31 00:29:48 +0200252 stats = {}
Paul Vinciguerra56421092018-11-21 16:34:09 -0800253 rv = self.api.stat_segment_dump_r(counters, self.client)
Ole Troanb4603a72018-09-18 11:06:33 +0200254 # Raise exception and retry
255 if rv == ffi.NULL:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800256 raise VPPStatsIOError()
Ole Troan73202102018-08-31 00:29:48 +0200257 rv_len = self.api.stat_segment_vec_len(rv)
Ole Troancbb8f582019-04-15 08:53:46 +0200258
Ole Troan73202102018-08-31 00:29:48 +0200259 for i in range(rv_len):
Paul Vinciguerra3d04e332019-03-19 17:32:39 -0700260 n = ffi.string(rv[i].name).decode('utf-8')
Ole Troan73202102018-08-31 00:29:48 +0200261 e = stat_entry_to_python(self.api, rv[i])
Ole Troan31555a32018-10-22 09:30:26 +0200262 if e is not None:
Ole Troan282093f2018-09-19 12:38:51 +0200263 stats[n] = e
Ole Troan73202102018-08-31 00:29:48 +0200264 return stats
265
Ole Troan58492a82018-09-04 13:19:12 +0200266 def get_counter(self, name):
Ole Troanb4603a72018-09-18 11:06:33 +0200267 retries = 0
268 while True:
269 try:
Ole Troan7f991832018-12-06 17:35:12 +0100270 d = self.ls(name)
271 s = self.dump(d)
272 if len(s) > 1:
273 raise AttributeError('Matches multiple counters {}'
274 .format(name))
275 k, v = s.popitem()
276 return v
Paul Vinciguerraae8819f2019-06-07 13:35:37 -0400277 except VPPStatsIOError:
Ole Troanb4603a72018-09-18 11:06:33 +0200278 if retries > 10:
279 return None
280 retries += 1
Ole Troan73202102018-08-31 00:29:48 +0200281
Ole Troan233e4682019-05-16 15:01:34 +0200282 def get_err_counter(self, name):
283 """Get an error counter. The errors from each worker thread
284 are summed"""
285 return sum(self.get_counter(name))
286
Ole Troan73202102018-08-31 00:29:48 +0200287 def disconnect(self):
Paul Vinciguerrae090f4d2019-11-29 17:41:20 -0500288 try:
289 self.api.stat_segment_disconnect_r(self.client)
290 self.api.stat_client_free(self.client)
291 self.connected = False
292 del self.client
293 except AttributeError:
294 # no need to disconnect if we're not connected
295 pass
Ole Troan73202102018-08-31 00:29:48 +0200296
297 def set_errors(self):
298 '''Return all errors counters > 0'''
Ole Troanb4603a72018-09-18 11:06:33 +0200299 retries = 0
300 while True:
301 try:
302 error_names = self.ls(['/err/'])
303 error_counters = self.dump(error_names)
304 break
Paul Vinciguerraae8819f2019-06-07 13:35:37 -0400305 except VPPStatsIOError:
Ole Troanb4603a72018-09-18 11:06:33 +0200306 if retries > 10:
307 return None
308 retries += 1
Paul Vinciguerra56421092018-11-21 16:34:09 -0800309
Ole Troan233e4682019-05-16 15:01:34 +0200310 return {k: sum(error_counters[k])
311 for k in error_counters.keys() if sum(error_counters[k])}
Ole Troan73202102018-08-31 00:29:48 +0200312
313 def set_errors_str(self):
314 '''Return all errors counters > 0 pretty printed'''
Paul Vinciguerra5770c912019-06-29 08:33:05 -0400315 s = ['ERRORS:']
Ole Troan73202102018-08-31 00:29:48 +0200316 error_counters = self.set_errors()
317 for k in sorted(error_counters):
Paul Vinciguerra5770c912019-06-29 08:33:05 -0400318 s.append('{:<60}{:>10}'.format(k, error_counters[k]))
319 return '%s\n' % '\n'.join(s)