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 // global VPP stats API client, library vppapiclient only supports
165 // single connection at a time
166 var globalStatClient *statClient
168 // stubStatClient is the default implementation of StatsAPI.
169 type statClient struct {
173 // NewStatClient returns new VPP stats API client.
174 func NewStatClient(socketName string) adapter.StatsAPI {
176 socketName: socketName,
180 func (c *statClient) Connect() error {
181 if globalStatClient != nil {
182 return fmt.Errorf("already connected to stats API, disconnect first")
186 if c.socketName == "" {
187 sockName = DefaultStatSocket
189 sockName = c.socketName
192 rc := C.govpp_stat_connect(C.CString(sockName))
194 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
201 func (c *statClient) Disconnect() error {
202 globalStatClient = nil
204 C.govpp_stat_disconnect()
208 func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
209 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
210 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
212 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
213 for i := 0; i < int(l); i++ {
214 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
215 stats = append(stats, C.GoString(nameChar))
216 C.free(unsafe.Pointer(nameChar))
222 func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
223 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
224 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
226 dump := C.govpp_stat_segment_dump(dir)
227 defer C.govpp_stat_segment_data_free(dump)
229 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
230 for i := 0; i < int(l); i++ {
231 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
233 name := C.GoString(nameChar)
234 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
236 stat := &adapter.StatEntry{
242 case adapter.ScalarIndex:
243 stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
245 case adapter.ErrorIndex:
246 stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
248 case adapter.SimpleCounterVector:
249 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
250 vector := make([][]adapter.Counter, 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_simple_counter_index(&v, C.int(k))))); j++ {
253 vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, C.int(k), C.int(j))))
256 stat.Data = adapter.SimpleCounterStat(vector)
258 case adapter.CombinedCounterVector:
259 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
260 vector := make([][]adapter.CombinedCounter, length)
261 for k := 0; k < length; k++ {
262 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter_index(&v, C.int(k))))); j++ {
263 vector[k] = append(vector[k], adapter.CombinedCounter{
264 Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, C.int(k), C.int(j))),
265 Bytes: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, C.int(k), C.int(j))),
269 stat.Data = adapter.CombinedCounterStat(vector)
272 fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ)
277 stats = append(stats, stat)
283 func convertStringSlice(strs []string) **C.uint8_t {
285 for _, str := range strs {
286 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))