blob: 76ccf1042f6679dac4a2cad7d828d4f0aea58a7b [file] [log] [blame]
Ole Troan73202102018-08-31 00:29:48 +02001#!/usr/bin/env python
2
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{
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
59typedef 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
67stat_client_main_t * stat_client_get(void);
68void stat_client_free(stat_client_main_t * sm);
69int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020070int stat_segment_connect (char *socket_name);
Paul Vinciguerra56421092018-11-21 16:34:09 -080071void stat_segment_disconnect_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020072void stat_segment_disconnect (void);
73
Paul Vinciguerra56421092018-11-21 16:34:09 -080074uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020075uint32_t *stat_segment_ls (uint8_t ** pattern);
Paul Vinciguerra56421092018-11-21 16:34:09 -080076stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020077stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
Ole Troan73202102018-08-31 00:29:48 +020078void stat_segment_data_free (stat_segment_data_t * res);
Paul Vinciguerra56421092018-11-21 16:34:09 -080079
80double stat_segment_heartbeat_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020081int stat_segment_vec_len(void *vec);
82uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string);
Ole Troancbb8f582019-04-15 08:53:46 +020083char *stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm);
84void free(void *ptr);
Ole Troan73202102018-08-31 00:29:48 +020085""")
86
87
88# Utility functions
89def make_string_vector(api, strings):
90 vec = ffi.NULL
91 if type(strings) is not list:
92 strings = [strings]
93 for s in strings:
Ole Troan31555a32018-10-22 09:30:26 +020094 vec = api.stat_segment_string_vector(vec, ffi.new("char []",
Paul Vinciguerra3d04e332019-03-19 17:32:39 -070095 s.encode('utf-8')))
Ole Troan73202102018-08-31 00:29:48 +020096 return vec
97
98
99def make_string_list(api, vec):
100 vec_len = api.stat_segment_vec_len(vec)
101 return [ffi.string(vec[i]) for i in range(vec_len)]
102
103
104# 2-dimensonal array of thread, index
105def simple_counter_vec_list(api, e):
106 vec = []
107 for thread in range(api.stat_segment_vec_len(e)):
108 len_interfaces = api.stat_segment_vec_len(e[thread])
109 if_per_thread = [e[thread][interfaces]
110 for interfaces in range(len_interfaces)]
111 vec.append(if_per_thread)
112 return vec
113
114
115def vlib_counter_dict(c):
116 return {'packets': c.packets,
117 'bytes': c.bytes}
118
119
120def combined_counter_vec_list(api, e):
121 vec = []
122 for thread in range(api.stat_segment_vec_len(e)):
123 len_interfaces = api.stat_segment_vec_len(e[thread])
124 if_per_thread = [vlib_counter_dict(e[thread][interfaces])
125 for interfaces in range(len_interfaces)]
126 vec.append(if_per_thread)
127 return vec
128
Ole Troan233e4682019-05-16 15:01:34 +0200129def error_vec_list(api, e):
130 vec = []
131 for thread in range(api.stat_segment_vec_len(e)):
132 vec.append(e[thread])
133 return vec
134
Ole Troancbb8f582019-04-15 08:53:46 +0200135def name_vec_list(api, e):
136 return [ffi.string(e[i]).decode('utf-8') for i in range(api.stat_segment_vec_len(e)) if e[i] != ffi.NULL]
Ole Troan73202102018-08-31 00:29:48 +0200137
138def stat_entry_to_python(api, e):
Ole Troan58492a82018-09-04 13:19:12 +0200139 # Scalar index
140 if e.type == 1:
141 return e.scalar_value
Ole Troan58492a82018-09-04 13:19:12 +0200142 if e.type == 2:
143 return simple_counter_vec_list(api, e.simple_counter_vec)
Ole Troan73202102018-08-31 00:29:48 +0200144 if e.type == 3:
Ole Troan73202102018-08-31 00:29:48 +0200145 return combined_counter_vec_list(api, e.combined_counter_vec)
Ole Troan58492a82018-09-04 13:19:12 +0200146 if e.type == 4:
Ole Troan233e4682019-05-16 15:01:34 +0200147 return error_vec_list(api, e.error_vector)
Ole Troancbb8f582019-04-15 08:53:46 +0200148 if e.type == 5:
149 return name_vec_list(api, e.name_vector)
150 raise NotImplementedError()
Ole Troan73202102018-08-31 00:29:48 +0200151
152
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800153class VPPStatsIOError(IOError):
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800154 message = "Stat segment client connection returned: " \
155 "%(retval)s %(strerror)s."
156
157 strerror = {-1: "Stat client couldn't open socket",
158 -2: "Stat client socket open but couldn't connect",
159 -3: "Receiving file descriptor failed",
160 -4: "mmap fstat failed",
161 -5: "mmap map failed"
162 }
163
164 def __init__(self, message=None, **kwargs):
165 if 'retval' in kwargs:
166 self.retval = kwargs['retval']
167 kwargs['strerror'] = self.strerror[int(self.retval)]
168
169 if not message:
170 try:
171 message = self.message % kwargs
172 except Exception as e:
173 message = self.message
174 else:
175 message = message % kwargs
176
177 super(VPPStatsIOError, self).__init__(message)
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800178
179
180class VPPStatsClientLoadError(RuntimeError):
181 pass
182
183
Paul Vinciguerra7e713f12018-11-26 12:04:48 -0800184class VPPStats(object):
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800185 VPPStatsIOError = VPPStatsIOError
186
Ole Troancbb8f582019-04-15 08:53:46 +0200187 default_socketname = '/var/run/vpp/stats.sock'
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800188 sharedlib_name = 'libvppapiclient.so'
189
190 def __init__(self, socketname=default_socketname, timeout=10):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800191 try:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800192 self.api = ffi.dlopen(VPPStats.sharedlib_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800193 except Exception:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800194 raise VPPStatsClientLoadError("Could not open: %s" %
195 VPPStats.sharedlib_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800196 self.client = self.api.stat_client_get()
197
198 poll_end_time = time.time() + timeout
199 while time.time() < poll_end_time:
Paul Vinciguerra3d04e332019-03-19 17:32:39 -0700200 rv = self.api.stat_segment_connect_r(socketname.encode('utf-8'),
Paul Vinciguerra56421092018-11-21 16:34:09 -0800201 self.client)
Ole Troanc424de72019-06-04 12:33:32 +0200202 # Break out if success or any other error than "no such file"
203 # (indicating that VPP hasn't started yet)
204 if rv == 0 or ffi.errno != 2:
Paul Vinciguerra56421092018-11-21 16:34:09 -0800205 break
206
Ole Troan73202102018-08-31 00:29:48 +0200207 if rv != 0:
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800208 raise VPPStatsIOError(retval=rv)
Ole Troan73202102018-08-31 00:29:48 +0200209
210 def heartbeat(self):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800211 return self.api.stat_segment_heartbeat_r(self.client)
Ole Troan73202102018-08-31 00:29:48 +0200212
213 def ls(self, patterns):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800214 return self.api.stat_segment_ls_r(make_string_vector(self.api,
215 patterns),
216 self.client)
Ole Troan73202102018-08-31 00:29:48 +0200217
Ole Troancbb8f582019-04-15 08:53:46 +0200218 def lsstr(self, patterns):
219 rv = self.api.stat_segment_ls_r(make_string_vector(self.api,
220 patterns),
221 self.client)
222
223 if rv == ffi.NULL:
224 raise VPPStatsIOError()
225 return [ffi.string(self.api.stat_segment_index_to_name_r(rv[i], self.client)).decode('utf-8')
226 for i in range(self.api.stat_segment_vec_len(rv))]
227
Ole Troan73202102018-08-31 00:29:48 +0200228 def dump(self, counters):
229 stats = {}
Paul Vinciguerra56421092018-11-21 16:34:09 -0800230 rv = self.api.stat_segment_dump_r(counters, self.client)
Ole Troanb4603a72018-09-18 11:06:33 +0200231 # Raise exception and retry
232 if rv == ffi.NULL:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800233 raise VPPStatsIOError()
Ole Troan73202102018-08-31 00:29:48 +0200234 rv_len = self.api.stat_segment_vec_len(rv)
Ole Troancbb8f582019-04-15 08:53:46 +0200235
Ole Troan73202102018-08-31 00:29:48 +0200236 for i in range(rv_len):
Paul Vinciguerra3d04e332019-03-19 17:32:39 -0700237 n = ffi.string(rv[i].name).decode('utf-8')
Ole Troan73202102018-08-31 00:29:48 +0200238 e = stat_entry_to_python(self.api, rv[i])
Ole Troan31555a32018-10-22 09:30:26 +0200239 if e is not None:
Ole Troan282093f2018-09-19 12:38:51 +0200240 stats[n] = e
Ole Troan73202102018-08-31 00:29:48 +0200241 return stats
242
Ole Troan58492a82018-09-04 13:19:12 +0200243 def get_counter(self, name):
Ole Troanb4603a72018-09-18 11:06:33 +0200244 retries = 0
245 while True:
246 try:
Ole Troan7f991832018-12-06 17:35:12 +0100247 d = self.ls(name)
248 s = self.dump(d)
249 if len(s) > 1:
250 raise AttributeError('Matches multiple counters {}'
251 .format(name))
252 k, v = s.popitem()
253 return v
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800254 except VPPStatsIOError as e:
Ole Troanb4603a72018-09-18 11:06:33 +0200255 if retries > 10:
256 return None
257 retries += 1
Ole Troan73202102018-08-31 00:29:48 +0200258
Ole Troan233e4682019-05-16 15:01:34 +0200259 def get_err_counter(self, name):
260 """Get an error counter. The errors from each worker thread
261 are summed"""
262 return sum(self.get_counter(name))
263
Ole Troan73202102018-08-31 00:29:48 +0200264 def disconnect(self):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800265 self.api.stat_segment_disconnect_r(self.client)
266 self.api.stat_client_free(self.client)
Ole Troan73202102018-08-31 00:29:48 +0200267
268 def set_errors(self):
269 '''Return all errors counters > 0'''
Ole Troanb4603a72018-09-18 11:06:33 +0200270 retries = 0
271 while True:
272 try:
273 error_names = self.ls(['/err/'])
274 error_counters = self.dump(error_names)
275 break
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800276 except VPPStatsIOError as e:
Ole Troanb4603a72018-09-18 11:06:33 +0200277 if retries > 10:
278 return None
279 retries += 1
Paul Vinciguerra56421092018-11-21 16:34:09 -0800280
Ole Troan233e4682019-05-16 15:01:34 +0200281 return {k: sum(error_counters[k])
282 for k in error_counters.keys() if sum(error_counters[k])}
Ole Troan73202102018-08-31 00:29:48 +0200283
284 def set_errors_str(self):
285 '''Return all errors counters > 0 pretty printed'''
286 s = 'ERRORS:\n'
287 error_counters = self.set_errors()
288 for k in sorted(error_counters):
289 s += '{:<60}{:>10}\n'.format(k, error_counters[k])
290 return s