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
23 #include "stat_client_wrapper.h"
33 "git.fd.io/govpp.git/adapter"
37 ErrStatDirBusy = errors.New("stat dir busy")
38 ErrStatDumpBusy = errors.New("stat dump busy")
42 // DefaultStatSocket is the default path for the VPP stat socket file.
43 DefaultStatSocket = "/run/vpp/stats.sock"
46 // global VPP stats API client, library vppapiclient only supports
47 // single connection at a time
48 var globalStatClient *statClient
50 // stubStatClient is the default implementation of StatsAPI.
51 type statClient struct {
55 // NewStatClient returns new VPP stats API client.
56 func NewStatClient(socketName string) adapter.StatsAPI {
58 socketName: socketName,
62 func (c *statClient) Connect() error {
63 if globalStatClient != nil {
64 return fmt.Errorf("already connected to stats API, disconnect first")
68 if c.socketName == "" {
69 sockName = DefaultStatSocket
71 sockName = c.socketName
74 if _, err := os.Stat(sockName); err != nil {
75 if os.IsNotExist(err) {
76 return fmt.Errorf("stats socket file %q does not exists, ensure that VPP is running with `statseg { ... }` section in config", sockName)
78 return fmt.Errorf("stats socket file error: %v", err)
81 rc := C.govpp_stat_connect(C.CString(sockName))
83 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
90 func (c *statClient) Disconnect() error {
91 globalStatClient = nil
93 C.govpp_stat_disconnect()
97 func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
98 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
100 return nil, ErrStatDirBusy
102 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
104 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
105 for i := 0; i < int(l); i++ {
106 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
107 stats = append(stats, C.GoString(nameChar))
108 C.free(unsafe.Pointer(nameChar))
114 func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
115 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
117 return nil, ErrStatDirBusy
119 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
121 dump := C.govpp_stat_segment_dump(dir)
123 return nil, ErrStatDumpBusy
125 defer C.govpp_stat_segment_data_free(dump)
127 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
128 for i := 0; i < int(l); i++ {
129 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
131 name := C.GoString(nameChar)
132 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
134 stat := &adapter.StatEntry{
140 case adapter.ScalarIndex:
141 stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
143 case adapter.ErrorIndex:
144 stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
146 case adapter.SimpleCounterVector:
147 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
148 vector := make([][]adapter.Counter, length)
149 for k := 0; k < length; k++ {
150 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++ {
151 vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, C.int(k), C.int(j))))
154 stat.Data = adapter.SimpleCounterStat(vector)
156 case adapter.CombinedCounterVector:
157 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
158 vector := make([][]adapter.CombinedCounter, length)
159 for k := 0; k < length; k++ {
160 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++ {
161 vector[k] = append(vector[k], adapter.CombinedCounter{
162 Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, C.int(k), C.int(j))),
163 Bytes: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, C.int(k), C.int(j))),
167 stat.Data = adapter.CombinedCounterStat(vector)
169 case adapter.NameVector:
170 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_name_vector(&v))))
171 var vector []adapter.Name
172 for k := 0; k < length; k++ {
173 s := C.govpp_stat_segment_data_get_name_vector_index(&v, C.int(k))
174 var name adapter.Name
176 name = adapter.Name(C.GoString(s))
178 vector = append(vector, name)
180 stat.Data = adapter.NameStat(vector)
183 fmt.Fprintf(os.Stderr, "invalid stat type: %v (%v)\n", typ, name)
188 stats = append(stats, stat)
194 func convertStringSlice(strs []string) **C.uint8_t {
196 for _, str := range strs {
197 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))