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,!novpp,!beyond18.10
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
23 #include "stat_client_wrapper.h"
32 "git.fd.io/govpp.git/adapter"
35 // global VPP stats API client, library vppapiclient only supports
36 // single connection at a time
37 var globalStatClient *statClient
39 // statClient is the default implementation of StatsAPI.
40 type statClient struct {
44 // NewStatClient returns new VPP stats API client.
45 func NewStatClient(socketName string) adapter.StatsAPI {
47 socketName: socketName,
51 func (c *statClient) Connect() error {
52 if globalStatClient != nil {
53 return fmt.Errorf("already connected to stats API, disconnect first")
57 if c.socketName == "" {
58 sockName = adapter.DefaultStatsSocket
60 sockName = c.socketName
63 if _, err := os.Stat(sockName); err != nil {
64 if os.IsNotExist(err) {
65 return fmt.Errorf("stats socket file %q does not exists, ensure that VPP is running with `statseg { ... }` section in config", sockName)
67 return fmt.Errorf("stats socket file error: %v", err)
70 rc := C.govpp_stat_connect(C.CString(sockName))
72 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
79 func (c *statClient) Disconnect() error {
80 globalStatClient = nil
82 C.govpp_stat_disconnect()
86 func (c *statClient) ListStats(patterns ...string) (indexes []adapter.StatIdentifier, err error) {
87 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
89 return nil, adapter.ErrStatsDataBusy
91 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
93 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
94 for i := 0; i < int(l); i++ {
95 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
96 indexes = append(indexes, adapter.StatIdentifier{
97 Name: []byte(C.GoString(nameChar)),
100 C.free(unsafe.Pointer(nameChar))
106 func (c *statClient) DumpStats(patterns ...string) (stats []adapter.StatEntry, err error) {
107 dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
109 return nil, adapter.ErrStatsDataBusy
111 defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
113 dump := C.govpp_stat_segment_dump(dir)
115 return nil, adapter.ErrStatsDataBusy
117 defer C.govpp_stat_segment_data_free(dump)
119 l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
120 for i := 0; i < int(l); i++ {
121 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
123 name := C.GoString(nameChar)
124 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
126 stat := adapter.StatEntry{
127 StatIdentifier: adapter.StatIdentifier{
135 case adapter.ScalarIndex:
136 stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
138 case adapter.ErrorIndex:
139 stat.Data = adapter.ErrorStat([]adapter.Counter{adapter.Counter(C.govpp_stat_segment_data_get_error_value(&v))})
141 case adapter.SimpleCounterVector:
142 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
143 vector := make([][]adapter.Counter, length)
144 for k := 0; k < length; k++ {
145 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++ {
146 vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, C.int(k), C.int(j))))
149 stat.Data = adapter.SimpleCounterStat(vector)
151 case adapter.CombinedCounterVector:
152 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
153 vector := make([][]adapter.CombinedCounter, length)
154 for k := 0; k < length; k++ {
155 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++ {
156 vector[k] = append(vector[k], [2]uint64{
157 uint64(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, C.int(k), C.int(j))),
158 uint64(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, C.int(k), C.int(j))),
162 stat.Data = adapter.CombinedCounterStat(vector)
164 case adapter.NameVector:
165 length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_name_vector(&v))))
166 var vector []adapter.Name
167 for k := 0; k < length; k++ {
168 s := C.govpp_stat_segment_data_get_name_vector_index(&v, C.int(k))
169 var name adapter.Name
171 name = adapter.Name(C.GoString(s))
173 vector = append(vector, name)
175 stat.Data = adapter.NameStat(vector)
178 fmt.Fprintf(os.Stderr, "invalid stat type: %v (%v)\n", typ, name)
183 stats = append(stats, stat)
189 func (c *statClient) PrepareDir(_ ...string) (*adapter.StatDir, error) {
190 return nil, adapter.ErrNotImplemented
193 func (c *statClient) PrepareDirOnIndex(_ ...uint32) (*adapter.StatDir, error) {
194 return nil, adapter.ErrNotImplemented
197 func (c *statClient) UpdateDir(_ *adapter.StatDir) error {
198 return adapter.ErrNotImplemented
201 func convertStringSlice(strs []string) **C.uint8_t {
203 for _, str := range strs {
204 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))