blob: 8c1aaf2b87a9466e3dcbf2b905bd3e321ca519aa [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
5
6ffi = FFI()
7ffi.cdef("""
8typedef uint64_t counter_t;
9typedef struct {
10 counter_t packets;
11 counter_t bytes;
12} vlib_counter_t;
13
14typedef enum {
15 STAT_DIR_TYPE_ILLEGAL = 0,
Ole Troan58492a82018-09-04 13:19:12 +020016 STAT_DIR_TYPE_SCALAR_INDEX,
Ole Troan73202102018-08-31 00:29:48 +020017 STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
18 STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
19 STAT_DIR_TYPE_ERROR_INDEX,
Ole Troan73202102018-08-31 00:29:48 +020020} stat_directory_type_t;
21
Ole Troan58492a82018-09-04 13:19:12 +020022typedef struct
23{
Ole Troan73202102018-08-31 00:29:48 +020024 stat_directory_type_t type;
25 union {
Ole Troan58492a82018-09-04 13:19:12 +020026 uint64_t offset;
27 uint64_t index;
28 uint64_t value;
29 };
30 uint64_t offset_vector;
31 char name[128]; // TODO change this to pointer to "somewhere"
32} stat_segment_directory_entry_t;
33
34typedef struct
35{
36 char *name;
37 stat_directory_type_t type;
38 union
39 {
Ole Troan73202102018-08-31 00:29:48 +020040 double scalar_value;
41 uint64_t error_value;
Ole Troan73202102018-08-31 00:29:48 +020042 counter_t **simple_counter_vec;
43 vlib_counter_t **combined_counter_vec;
44 };
45} stat_segment_data_t;
46
Ole Troan73202102018-08-31 00:29:48 +020047int stat_segment_connect (char *socket_name);
48void stat_segment_disconnect (void);
49
Ole Troan58492a82018-09-04 13:19:12 +020050uint32_t *stat_segment_ls (uint8_t ** pattern);
51stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
Ole Troan73202102018-08-31 00:29:48 +020052void stat_segment_data_free (stat_segment_data_t * res);
53double stat_segment_heartbeat (void);
54int stat_segment_vec_len(void *vec);
55uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string);
56""")
57
58
59# Utility functions
60def make_string_vector(api, strings):
61 vec = ffi.NULL
62 if type(strings) is not list:
63 strings = [strings]
64 for s in strings:
Ole Troan45d5c872018-09-18 10:23:01 +020065 vec = api.stat_segment_string_vector(vec, ffi.new("char []", s.encode()))
Ole Troan73202102018-08-31 00:29:48 +020066 return vec
67
68
69def make_string_list(api, vec):
70 vec_len = api.stat_segment_vec_len(vec)
71 return [ffi.string(vec[i]) for i in range(vec_len)]
72
73
74# 2-dimensonal array of thread, index
75def simple_counter_vec_list(api, e):
76 vec = []
77 for thread in range(api.stat_segment_vec_len(e)):
78 len_interfaces = api.stat_segment_vec_len(e[thread])
79 if_per_thread = [e[thread][interfaces]
80 for interfaces in range(len_interfaces)]
81 vec.append(if_per_thread)
82 return vec
83
84
85def vlib_counter_dict(c):
86 return {'packets': c.packets,
87 'bytes': c.bytes}
88
89
90def combined_counter_vec_list(api, e):
91 vec = []
92 for thread in range(api.stat_segment_vec_len(e)):
93 len_interfaces = api.stat_segment_vec_len(e[thread])
94 if_per_thread = [vlib_counter_dict(e[thread][interfaces])
95 for interfaces in range(len_interfaces)]
96 vec.append(if_per_thread)
97 return vec
98
99
100def stat_entry_to_python(api, e):
Ole Troan58492a82018-09-04 13:19:12 +0200101 # Scalar index
102 if e.type == 1:
103 return e.scalar_value
104 return None
105 if e.type == 2:
106 return simple_counter_vec_list(api, e.simple_counter_vec)
Ole Troan73202102018-08-31 00:29:48 +0200107 if e.type == 3:
Ole Troan73202102018-08-31 00:29:48 +0200108 return combined_counter_vec_list(api, e.combined_counter_vec)
Ole Troan58492a82018-09-04 13:19:12 +0200109 if e.type == 4:
Ole Troan73202102018-08-31 00:29:48 +0200110 return e.error_value
111 return None
112
113
114class VPPStats:
115 def __init__(self, socketname='/var/run/stats.sock'):
116 self.api = ffi.dlopen('libvppapiclient.so')
Ole Troan45d5c872018-09-18 10:23:01 +0200117 rv = self.api.stat_segment_connect(socketname.encode())
Ole Troan73202102018-08-31 00:29:48 +0200118 if rv != 0:
119 raise IOError()
120
121 def heartbeat(self):
122 return self.api.stat_segment_heartbeat()
123
124 def ls(self, patterns):
125 return self.api.stat_segment_ls(make_string_vector(self.api, patterns))
126
127 def dump(self, counters):
128 stats = {}
129 rv = self.api.stat_segment_dump(counters)
Ole Troanb4603a72018-09-18 11:06:33 +0200130 # Raise exception and retry
131 if rv == ffi.NULL:
132 raise IOError()
Ole Troan73202102018-08-31 00:29:48 +0200133 rv_len = self.api.stat_segment_vec_len(rv)
134 for i in range(rv_len):
Ole Troan45d5c872018-09-18 10:23:01 +0200135 n = ffi.string(rv[i].name).decode()
Ole Troan73202102018-08-31 00:29:48 +0200136 e = stat_entry_to_python(self.api, rv[i])
Ole Troan282093f2018-09-19 12:38:51 +0200137 if e != None:
138 stats[n] = e
Ole Troan73202102018-08-31 00:29:48 +0200139 return stats
140
Ole Troan58492a82018-09-04 13:19:12 +0200141 def get_counter(self, name):
Ole Troanb4603a72018-09-18 11:06:33 +0200142 retries = 0
143 while True:
144 try:
145 dir = self.ls(name)
146 return self.dump(dir).values()[0]
147 except:
148 if retries > 10:
149 return None
150 retries += 1
151 pass
Ole Troan73202102018-08-31 00:29:48 +0200152
153 def disconnect(self):
154 self.api.stat_segment_disconnect()
155
156 def set_errors(self):
157 '''Return all errors counters > 0'''
Ole Troanb4603a72018-09-18 11:06:33 +0200158 retries = 0
159 while True:
160 try:
161 error_names = self.ls(['/err/'])
162 error_counters = self.dump(error_names)
163 break
164 except:
165 if retries > 10:
166 return None
167 retries += 1
168 pass
Ole Troan73202102018-08-31 00:29:48 +0200169 return {k: error_counters[k]
170 for k in error_counters.keys() if error_counters[k]}
171
172 def set_errors_str(self):
173 '''Return all errors counters > 0 pretty printed'''
174 s = 'ERRORS:\n'
175 error_counters = self.set_errors()
176 for k in sorted(error_counters):
177 s += '{:<60}{:>10}\n'.format(k, error_counters[k])
178 return s