Support for error vectors
[govpp.git] / adapter / vppapiclient / stat_client.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 // +build !windows,!darwin
16
17 package vppapiclient
18
19 /*
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
22
23 #include "stat_client_wrapper.h"
24 */
25 import "C"
26
27 import (
28         "errors"
29         "fmt"
30         "os"
31         "unsafe"
32
33         "git.fd.io/govpp.git/adapter"
34 )
35
36 var (
37         ErrStatDirBusy  = errors.New("stat dir busy")
38         ErrStatDumpBusy = errors.New("stat dump busy")
39 )
40
41 var (
42         // DefaultStatSocket is the default path for the VPP stat socket file.
43         DefaultStatSocket = "/run/vpp/stats.sock"
44 )
45
46 // global VPP stats API client, library vppapiclient only supports
47 // single connection at a time
48 var globalStatClient *statClient
49
50 // stubStatClient is the default implementation of StatsAPI.
51 type statClient struct {
52         socketName string
53 }
54
55 // NewStatClient returns new VPP stats API client.
56 func NewStatClient(socketName string) adapter.StatsAPI {
57         return &statClient{
58                 socketName: socketName,
59         }
60 }
61
62 func (c *statClient) Connect() error {
63         if globalStatClient != nil {
64                 return fmt.Errorf("already connected to stats API, disconnect first")
65         }
66
67         var sockName string
68         if c.socketName == "" {
69                 sockName = DefaultStatSocket
70         } else {
71                 sockName = c.socketName
72         }
73
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)
77                 }
78                 return fmt.Errorf("stats socket file error: %v", err)
79         }
80
81         rc := C.govpp_stat_connect(C.CString(sockName))
82         if rc != 0 {
83                 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
84         }
85
86         globalStatClient = c
87         return nil
88 }
89
90 func (c *statClient) Disconnect() error {
91         globalStatClient = nil
92
93         C.govpp_stat_disconnect()
94         return nil
95 }
96
97 func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
98         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
99         if dir == nil {
100                 return nil, ErrStatDirBusy
101         }
102         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
103
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))
109         }
110
111         return stats, nil
112 }
113
114 func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
115         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
116         if dir == nil {
117                 return nil, ErrStatDirBusy
118         }
119         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
120
121         dump := C.govpp_stat_segment_dump(dir)
122         if dump == nil {
123                 return nil, ErrStatDumpBusy
124         }
125         defer C.govpp_stat_segment_data_free(dump)
126
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))
130                 nameChar := v.name
131                 name := C.GoString(nameChar)
132                 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
133
134                 stat := &adapter.StatEntry{
135                         Name: name,
136                         Type: typ,
137                 }
138
139                 switch typ {
140                 case adapter.ScalarIndex:
141                         stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
142
143                 case adapter.ErrorIndex:
144                         stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
145
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))))
152                                 }
153                         }
154                         stat.Data = adapter.SimpleCounterStat(vector)
155
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))),
164                                         })
165                                 }
166                         }
167                         stat.Data = adapter.CombinedCounterStat(vector)
168
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
175                                 if s != nil {
176                                         name = adapter.Name(C.GoString(s))
177                                 }
178                                 vector = append(vector, name)
179                         }
180                         stat.Data = adapter.NameStat(vector)
181
182                 default:
183                         fmt.Fprintf(os.Stderr, "invalid stat type: %v (%v)\n", typ, name)
184                         continue
185
186                 }
187
188                 stats = append(stats, stat)
189         }
190
191         return stats, nil
192 }
193
194 func convertStringSlice(strs []string) **C.uint8_t {
195         var arr **C.uint8_t
196         for _, str := range strs {
197                 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
198         }
199         return arr
200 }