1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows,!darwin
package vppapiclient
/*
#cgo CFLAGS: -DPNG_DEBUG=1
#cgo LDFLAGS: -lvppapiclient
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <vpp-api/client/vppapiclient.h>
#include <vpp-api/client/stat_client.h>
static int
govpp_stat_connect(char *socket_name)
{
return stat_segment_connect(socket_name);
}
static void
govpp_stat_disconnect()
{
stat_segment_disconnect();
}
static uint32_t*
govpp_stat_segment_ls(uint8_t ** pattern)
{
return stat_segment_ls(pattern);
}
static int
govpp_stat_segment_vec_len(void *vec)
{
return stat_segment_vec_len(vec);
}
static void
govpp_stat_segment_vec_free(void *vec)
{
stat_segment_vec_free(vec);
}
static char*
govpp_stat_segment_dir_index_to_name(uint32_t *dir, uint32_t index)
{
return stat_segment_index_to_name(dir[index]);
}
static stat_segment_data_t*
govpp_stat_segment_dump(uint32_t *counter_vec)
{
return stat_segment_dump(counter_vec);
}
static stat_segment_data_t
govpp_stat_segment_dump_index(stat_segment_data_t *data, int index)
{
return data[index];
}
static int
govpp_stat_segment_data_type(stat_segment_data_t *data)
{
return data->type;
}
static double
govpp_stat_segment_data_get_scalar_value(stat_segment_data_t *data)
{
return data->scalar_value;
}
static double
govpp_stat_segment_data_get_error_value(stat_segment_data_t *data)
{
return data->error_value;
}
static uint64_t**
govpp_stat_segment_data_get_simple_counter(stat_segment_data_t *data)
{
return data->simple_counter_vec;
}
static uint64_t*
govpp_stat_segment_data_get_simple_counter_index(stat_segment_data_t *data, int index)
{
return data->simple_counter_vec[index];
}
static uint64_t
govpp_stat_segment_data_get_simple_counter_index_value(stat_segment_data_t *data, int index, int index2)
{
return data->simple_counter_vec[index][index2];
}
static vlib_counter_t**
govpp_stat_segment_data_get_combined_counter(stat_segment_data_t *data)
{
return data->combined_counter_vec;
}
static vlib_counter_t*
govpp_stat_segment_data_get_combined_counter_index(stat_segment_data_t *data, int index)
{
return data->combined_counter_vec[index];
}
static uint64_t
govpp_stat_segment_data_get_combined_counter_index_packets(stat_segment_data_t *data, int index, int index2)
{
return data->combined_counter_vec[index][index2].packets;
}
static uint64_t
govpp_stat_segment_data_get_combined_counter_index_bytes(stat_segment_data_t *data, int index, int index2)
{
return data->combined_counter_vec[index][index2].bytes;
}
static void
govpp_stat_segment_data_free(stat_segment_data_t *data)
{
stat_segment_data_free(data);
}
static uint8_t**
govpp_stat_segment_string_vector(uint8_t ** string_vector, char *string)
{
return stat_segment_string_vector(string_vector, string);
}
*/
import "C"
import (
"fmt"
"os"
"unsafe"
"git.fd.io/govpp.git/adapter"
)
var (
// DefaultStatSocket is the default path for the VPP stat socket file.
DefaultStatSocket = "/run/vpp/stats.sock"
)
// global VPP stats API client, library vppapiclient only supports
// single connection at a time
var globalStatClient *statClient
// stubStatClient is the default implementation of StatsAPI.
type statClient struct {
socketName string
}
// NewStatClient returns new VPP stats API client.
func NewStatClient(socketName string) adapter.StatsAPI {
return &statClient{
socketName: socketName,
}
}
func (c *statClient) Connect() error {
if globalStatClient != nil {
return fmt.Errorf("already connected to stats API, disconnect first")
}
var sockName string
if c.socketName == "" {
sockName = DefaultStatSocket
} else {
sockName = c.socketName
}
rc := C.govpp_stat_connect(C.CString(sockName))
if rc != 0 {
return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
}
globalStatClient = c
return nil
}
func (c *statClient) Disconnect() error {
globalStatClient = nil
C.govpp_stat_disconnect()
return nil
}
func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
for i := 0; i < int(l); i++ {
nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
stats = append(stats, C.GoString(nameChar))
C.free(unsafe.Pointer(nameChar))
}
return stats, nil
}
func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
dump := C.govpp_stat_segment_dump(dir)
defer C.govpp_stat_segment_data_free(dump)
l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
for i := 0; i < int(l); i++ {
v := C.govpp_stat_segment_dump_index(dump, C.int(i))
nameChar := v.name
name := C.GoString(nameChar)
typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
stat := &adapter.StatEntry{
Name: name,
Type: typ,
}
switch typ {
case adapter.ScalarIndex:
stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
case adapter.ErrorIndex:
stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
case adapter.SimpleCounterVector:
length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
vector := make([][]adapter.Counter, length)
for k := 0; k < length; k++ {
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++ {
vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, _Ctype_int(k), _Ctype_int(j))))
}
}
stat.Data = adapter.SimpleCounterStat(vector)
case adapter.CombinedCounterVector:
length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
vector := make([][]adapter.CombinedCounter, length)
for k := 0; k < length; k++ {
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++ {
vector[k] = append(vector[k], adapter.CombinedCounter{
Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, _Ctype_int(k), _Ctype_int(j))),
Bytes: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, _Ctype_int(k), _Ctype_int(j))),
})
}
}
stat.Data = adapter.CombinedCounterStat(vector)
default:
fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ)
continue
}
stats = append(stats, stat)
}
return stats, nil
}
func convertStringSlice(strs []string) **C.uint8_t {
var arr **C.uint8_t
for _, str := range strs {
arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
}
return arr
}
|