1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // +build !windows,!darwin
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
26 #include <arpa/inet.h>
27 #include <vpp-api/client/vppapiclient.h>
28 #include <vpp-api/client/stat_client.h>
31 govpp_stat_connect(char *socket_name)
33 return stat_segment_connect(socket_name);
37 govpp_stat_disconnect()
39 stat_segment_disconnect();
43 govpp_stat_segment_ls(uint8_t ** pattern)
45 return stat_segment_ls(pattern);
49 govpp_stat_segment_vec_len(void *vec)
51 return stat_segment_vec_len(vec);
55 govpp_stat_segment_vec_free(void *vec)
57 stat_segment_vec_free(vec);
61 govpp_stat_segment_dir_index_to_name(uint32_t *dir, uint32_t index)
63 return stat_segment_index_to_name(dir[index]);
66 static stat_segment_data_t*
67 govpp_stat_segment_dump(uint32_t *counter_vec)
69 return stat_segment_dump(counter_vec);
72 static stat_segment_data_t
73 govpp_stat_segment_dump_index(stat_segment_data_t *data, int index)
79 govpp_stat_segment_data_type(stat_segment_data_t *data)
85 govpp_stat_segment_data_get_scalar_value(stat_segment_data_t *data)
87 return data->scalar_value;
91 govpp_stat_segment_data_get_error_value(stat_segment_data_t *data)
93 return data->error_value;
97 govpp_stat_segment_data_get_simple_counter(stat_segment_data_t *data)
99 return data->simple_counter_vec;
103 govpp_stat_segment_data_get_simple_counter_index(stat_segment_data_t *data, int index)
105 return data->simple_counter_vec[index];
109 govpp_stat_segment_data_get_simple_counter_index_value(stat_segment_data_t *data, int index, int index2)
111 return data->simple_counter_vec[index][index2];
114 static vlib_counter_t**
115 govpp_stat_segment_data_get_combined_counter(stat_segment_data_t *data)
117 return data->combined_counter_vec;
120 static vlib_counter_t*
121 govpp_stat_segment_data_get_combined_counter_index(stat_segment_data_t *data, int index)
123 return data->combined_counter_vec[index];
127 govpp_stat_segment_data_get_combined_counter_index_packets(stat_segment_data_t *data, int index, int index2)
129 return data->combined_counter_vec[index][index2].packets;
133 govpp_stat_segment_data_get_combined_counter_index_bytes(stat_segment_data_t *data, int index, int index2)
135 return data->combined_counter_vec[index][index2].bytes;
139 govpp_stat_segment_data_free(stat_segment_data_t *data)
141 stat_segment_data_free(data);
145 govpp_stat_segment_string_vector(uint8_t ** string_vector, char *string)
147 return stat_segment_string_vector(string_vector, string);
156 "git.fd.io/govpp.git/adapter"
160 // DefaultStatSocket is the default path for the VPP stat socket file.
161 DefaultStatSocket = "/run/vpp/stats.sock"
164 // StatClient is the default implementation of StatsAPI.
165 type StatClient struct {
169 // NewStatClient returns new VPP stats API client.
170 func NewStatClient(socketName string) *StatClient {
172 socketName: socketName,
176 func (c *StatClient) Connect() error {
179 if c.socketName == "" {
180 sockName = DefaultStatSocket
182 sockName = c.socketName
185 rc := C.govpp_stat_connect(C.CString(sockName))
187 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
193 func (c *StatClient) Disconnect() error {
194 C.govpp_stat_disconnect()
198 func (c *StatClient) ListStats(patterns ...string) (stats []string, err error) {
199 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
200 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
202 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
203 for i := 0; i < int(l); i++ {
204 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
205 stats = append(stats, C.GoString(nameChar))
206 C.free(unsafe.Pointer(nameChar))
212 func (c *StatClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
213 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
214 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
216 dump := C.govpp_stat_segment_dump(dir)
217 defer C.govpp_stat_segment_data_free(dump)
219 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
220 for i := 0; i < int(l); i++ {
221 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
223 name := C.GoString(nameChar)
224 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
226 stat := &adapter.StatEntry{
232 case adapter.ScalarIndex:
233 stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
235 case adapter.ErrorIndex:
236 stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
238 case adapter.SimpleCounterVector:
239 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
240 vector := make([][]adapter.Counter, length)
241 for k := 0; k < length; k++ {
242 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter_index(&v, _Ctype_int(k))))); j++ {
243 vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, _Ctype_int(k), _Ctype_int(j))))
246 stat.Data = adapter.SimpleCounterStat(vector)
248 case adapter.CombinedCounterVector:
249 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
250 vector := make([][]adapter.CombinedCounter, length)
251 for k := 0; k < length; k++ {
252 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter_index(&v, _Ctype_int(k))))); j++ {
253 vector[k] = append(vector[k], adapter.CombinedCounter{
254 Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, _Ctype_int(k), _Ctype_int(j))),
255 Bytes: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, _Ctype_int(k), _Ctype_int(j))),
259 stat.Data = adapter.CombinedCounterStat(vector)
262 fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ)
267 stats = append(stats, stat)
273 func convertStringSlice(strs []string) **C.uint8_t {
275 for _, str := range strs {
276 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))