blob: 06daaf86f508143f69834dbf5790aa29c7d2042f [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 Troan73202102018-08-31 00:29:48 +020021} stat_directory_type_t;
22
Ole Troan58492a82018-09-04 13:19:12 +020023typedef struct
24{
Ole Troan73202102018-08-31 00:29:48 +020025 stat_directory_type_t type;
26 union {
Ole Troan58492a82018-09-04 13:19:12 +020027 uint64_t offset;
28 uint64_t index;
29 uint64_t value;
30 };
31 uint64_t offset_vector;
32 char name[128]; // TODO change this to pointer to "somewhere"
33} stat_segment_directory_entry_t;
34
35typedef struct
36{
37 char *name;
38 stat_directory_type_t type;
39 union
40 {
Ole Troan73202102018-08-31 00:29:48 +020041 double scalar_value;
42 uint64_t error_value;
Ole Troan73202102018-08-31 00:29:48 +020043 counter_t **simple_counter_vec;
44 vlib_counter_t **combined_counter_vec;
45 };
46} stat_segment_data_t;
47
Paul Vinciguerra56421092018-11-21 16:34:09 -080048typedef struct
49{
50 uint64_t epoch;
51 uint64_t in_progress;
52 uint64_t directory_offset;
53 uint64_t error_offset;
54 uint64_t stats_offset;
55} stat_segment_shared_header_t;
56
57typedef struct
58{
59 uint64_t current_epoch;
60 stat_segment_shared_header_t *shared_header;
61 stat_segment_directory_entry_t *directory_vector;
62 ssize_t memory_size;
63} stat_client_main_t;
64
65stat_client_main_t * stat_client_get(void);
66void stat_client_free(stat_client_main_t * sm);
67int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020068int stat_segment_connect (char *socket_name);
Paul Vinciguerra56421092018-11-21 16:34:09 -080069void stat_segment_disconnect_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020070void stat_segment_disconnect (void);
71
Paul Vinciguerra56421092018-11-21 16:34:09 -080072uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020073uint32_t *stat_segment_ls (uint8_t ** pattern);
Paul Vinciguerra56421092018-11-21 16:34:09 -080074stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm);
Ole Troan58492a82018-09-04 13:19:12 +020075stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
Ole Troan73202102018-08-31 00:29:48 +020076void stat_segment_data_free (stat_segment_data_t * res);
Paul Vinciguerra56421092018-11-21 16:34:09 -080077
78double stat_segment_heartbeat_r (stat_client_main_t * sm);
Ole Troan73202102018-08-31 00:29:48 +020079double stat_segment_heartbeat (void);
80int stat_segment_vec_len(void *vec);
81uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string);
82""")
83
84
85# Utility functions
86def make_string_vector(api, strings):
87 vec = ffi.NULL
88 if type(strings) is not list:
89 strings = [strings]
90 for s in strings:
Ole Troan31555a32018-10-22 09:30:26 +020091 vec = api.stat_segment_string_vector(vec, ffi.new("char []",
92 s.encode()))
Ole Troan73202102018-08-31 00:29:48 +020093 return vec
94
95
96def make_string_list(api, vec):
97 vec_len = api.stat_segment_vec_len(vec)
98 return [ffi.string(vec[i]) for i in range(vec_len)]
99
100
101# 2-dimensonal array of thread, index
102def simple_counter_vec_list(api, e):
103 vec = []
104 for thread in range(api.stat_segment_vec_len(e)):
105 len_interfaces = api.stat_segment_vec_len(e[thread])
106 if_per_thread = [e[thread][interfaces]
107 for interfaces in range(len_interfaces)]
108 vec.append(if_per_thread)
109 return vec
110
111
112def vlib_counter_dict(c):
113 return {'packets': c.packets,
114 'bytes': c.bytes}
115
116
117def combined_counter_vec_list(api, e):
118 vec = []
119 for thread in range(api.stat_segment_vec_len(e)):
120 len_interfaces = api.stat_segment_vec_len(e[thread])
121 if_per_thread = [vlib_counter_dict(e[thread][interfaces])
122 for interfaces in range(len_interfaces)]
123 vec.append(if_per_thread)
124 return vec
125
126
127def stat_entry_to_python(api, e):
Ole Troan58492a82018-09-04 13:19:12 +0200128 # Scalar index
129 if e.type == 1:
130 return e.scalar_value
131 return None
132 if e.type == 2:
133 return simple_counter_vec_list(api, e.simple_counter_vec)
Ole Troan73202102018-08-31 00:29:48 +0200134 if e.type == 3:
Ole Troan73202102018-08-31 00:29:48 +0200135 return combined_counter_vec_list(api, e.combined_counter_vec)
Ole Troan58492a82018-09-04 13:19:12 +0200136 if e.type == 4:
Ole Troan73202102018-08-31 00:29:48 +0200137 return e.error_value
138 return None
139
140
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800141class VPPStatsIOError(IOError):
142 pass
143
144
145class VPPStatsClientLoadError(RuntimeError):
146 pass
147
148
Paul Vinciguerra7e713f12018-11-26 12:04:48 -0800149class VPPStats(object):
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800150 VPPStatsIOError = VPPStatsIOError
151
152 default_socketname = '/var/run/stats.sock'
153 sharedlib_name = 'libvppapiclient.so'
154
155 def __init__(self, socketname=default_socketname, timeout=10):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800156 try:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800157 self.api = ffi.dlopen(VPPStats.sharedlib_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800158 except Exception:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800159 raise VPPStatsClientLoadError("Could not open: %s" %
160 VPPStats.sharedlib_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800161 self.client = self.api.stat_client_get()
162
163 poll_end_time = time.time() + timeout
164 while time.time() < poll_end_time:
165 rv = self.api.stat_segment_connect_r(socketname.encode(),
166 self.client)
167 if rv == 0:
168 break
169
Ole Troan73202102018-08-31 00:29:48 +0200170 if rv != 0:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800171 raise VPPStatsIOError()
Ole Troan73202102018-08-31 00:29:48 +0200172
173 def heartbeat(self):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800174 return self.api.stat_segment_heartbeat_r(self.client)
Ole Troan73202102018-08-31 00:29:48 +0200175
176 def ls(self, patterns):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800177 return self.api.stat_segment_ls_r(make_string_vector(self.api,
178 patterns),
179 self.client)
Ole Troan73202102018-08-31 00:29:48 +0200180
181 def dump(self, counters):
182 stats = {}
Paul Vinciguerra56421092018-11-21 16:34:09 -0800183 rv = self.api.stat_segment_dump_r(counters, self.client)
Ole Troanb4603a72018-09-18 11:06:33 +0200184 # Raise exception and retry
185 if rv == ffi.NULL:
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800186 raise VPPStatsIOError()
Ole Troan73202102018-08-31 00:29:48 +0200187 rv_len = self.api.stat_segment_vec_len(rv)
188 for i in range(rv_len):
Ole Troan45d5c872018-09-18 10:23:01 +0200189 n = ffi.string(rv[i].name).decode()
Ole Troan73202102018-08-31 00:29:48 +0200190 e = stat_entry_to_python(self.api, rv[i])
Ole Troan31555a32018-10-22 09:30:26 +0200191 if e is not None:
Ole Troan282093f2018-09-19 12:38:51 +0200192 stats[n] = e
Ole Troan73202102018-08-31 00:29:48 +0200193 return stats
194
Ole Troan58492a82018-09-04 13:19:12 +0200195 def get_counter(self, name):
Ole Troanb4603a72018-09-18 11:06:33 +0200196 retries = 0
197 while True:
198 try:
Ole Troan7f991832018-12-06 17:35:12 +0100199 d = self.ls(name)
200 s = self.dump(d)
201 if len(s) > 1:
202 raise AttributeError('Matches multiple counters {}'
203 .format(name))
204 k, v = s.popitem()
205 return v
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800206 except VPPStatsIOError as e:
Ole Troanb4603a72018-09-18 11:06:33 +0200207 if retries > 10:
208 return None
209 retries += 1
Ole Troan73202102018-08-31 00:29:48 +0200210
211 def disconnect(self):
Paul Vinciguerra56421092018-11-21 16:34:09 -0800212 self.api.stat_segment_disconnect_r(self.client)
213 self.api.stat_client_free(self.client)
Ole Troan73202102018-08-31 00:29:48 +0200214
215 def set_errors(self):
216 '''Return all errors counters > 0'''
Ole Troanb4603a72018-09-18 11:06:33 +0200217 retries = 0
218 while True:
219 try:
220 error_names = self.ls(['/err/'])
221 error_counters = self.dump(error_names)
222 break
Paul Vinciguerra6ccc6e92018-11-27 08:15:22 -0800223 except VPPStatsIOError as e:
Ole Troanb4603a72018-09-18 11:06:33 +0200224 if retries > 10:
225 return None
226 retries += 1
Paul Vinciguerra56421092018-11-21 16:34:09 -0800227
Ole Troan73202102018-08-31 00:29:48 +0200228 return {k: error_counters[k]
229 for k in error_counters.keys() if error_counters[k]}
230
231 def set_errors_str(self):
232 '''Return all errors counters > 0 pretty printed'''
233 s = 'ERRORS:\n'
234 error_counters = self.set_errors()
235 for k in sorted(error_counters):
236 s += '{:<60}{:>10}\n'.format(k, error_counters[k])
237 return s