Fixup build with golang 1.12
[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 <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <arpa/inet.h>
27 #include <vpp-api/client/vppapiclient.h>
28 #include <vpp-api/client/stat_client.h>
29
30 static int
31 govpp_stat_connect(char *socket_name)
32 {
33         return stat_segment_connect(socket_name);
34 }
35
36 static void
37 govpp_stat_disconnect()
38 {
39     stat_segment_disconnect();
40 }
41
42 static uint32_t*
43 govpp_stat_segment_ls(uint8_t ** pattern)
44 {
45         return stat_segment_ls(pattern);
46 }
47
48 static int
49 govpp_stat_segment_vec_len(void *vec)
50 {
51         return stat_segment_vec_len(vec);
52 }
53
54 static void
55 govpp_stat_segment_vec_free(void *vec)
56 {
57         stat_segment_vec_free(vec);
58 }
59
60 static char*
61 govpp_stat_segment_dir_index_to_name(uint32_t *dir, uint32_t index)
62 {
63         return stat_segment_index_to_name(dir[index]);
64 }
65
66 static stat_segment_data_t*
67 govpp_stat_segment_dump(uint32_t *counter_vec)
68 {
69         return stat_segment_dump(counter_vec);
70 }
71
72 static stat_segment_data_t
73 govpp_stat_segment_dump_index(stat_segment_data_t *data, int index)
74 {
75         return data[index];
76 }
77
78 static int
79 govpp_stat_segment_data_type(stat_segment_data_t *data)
80 {
81         return data->type;
82 }
83
84 static double
85 govpp_stat_segment_data_get_scalar_value(stat_segment_data_t *data)
86 {
87         return data->scalar_value;
88 }
89
90 static double
91 govpp_stat_segment_data_get_error_value(stat_segment_data_t *data)
92 {
93         return data->error_value;
94 }
95
96 static uint64_t**
97 govpp_stat_segment_data_get_simple_counter(stat_segment_data_t *data)
98 {
99         return data->simple_counter_vec;
100 }
101
102 static uint64_t*
103 govpp_stat_segment_data_get_simple_counter_index(stat_segment_data_t *data, int index)
104 {
105         return data->simple_counter_vec[index];
106 }
107
108 static uint64_t
109 govpp_stat_segment_data_get_simple_counter_index_value(stat_segment_data_t *data, int index, int index2)
110 {
111         return data->simple_counter_vec[index][index2];
112 }
113
114 static vlib_counter_t**
115 govpp_stat_segment_data_get_combined_counter(stat_segment_data_t *data)
116 {
117         return data->combined_counter_vec;
118 }
119
120 static vlib_counter_t*
121 govpp_stat_segment_data_get_combined_counter_index(stat_segment_data_t *data, int index)
122 {
123         return data->combined_counter_vec[index];
124 }
125
126 static uint64_t
127 govpp_stat_segment_data_get_combined_counter_index_packets(stat_segment_data_t *data, int index, int index2)
128 {
129         return data->combined_counter_vec[index][index2].packets;
130 }
131
132 static uint64_t
133 govpp_stat_segment_data_get_combined_counter_index_bytes(stat_segment_data_t *data, int index, int index2)
134 {
135         return data->combined_counter_vec[index][index2].bytes;
136 }
137
138 static void
139 govpp_stat_segment_data_free(stat_segment_data_t *data)
140 {
141         stat_segment_data_free(data);
142 }
143
144 static uint8_t**
145 govpp_stat_segment_string_vector(uint8_t ** string_vector, char *string)
146 {
147         return stat_segment_string_vector(string_vector, string);
148 }
149 */
150 import "C"
151 import (
152         "fmt"
153         "os"
154         "unsafe"
155
156         "git.fd.io/govpp.git/adapter"
157 )
158
159 var (
160         // DefaultStatSocket is the default path for the VPP stat socket file.
161         DefaultStatSocket = "/run/vpp/stats.sock"
162 )
163
164 // global VPP stats API client, library vppapiclient only supports
165 // single connection at a time
166 var globalStatClient *statClient
167
168 // stubStatClient is the default implementation of StatsAPI.
169 type statClient struct {
170         socketName string
171 }
172
173 // NewStatClient returns new VPP stats API client.
174 func NewStatClient(socketName string) adapter.StatsAPI {
175         return &statClient{
176                 socketName: socketName,
177         }
178 }
179
180 func (c *statClient) Connect() error {
181         if globalStatClient != nil {
182                 return fmt.Errorf("already connected to stats API, disconnect first")
183         }
184
185         var sockName string
186         if c.socketName == "" {
187                 sockName = DefaultStatSocket
188         } else {
189                 sockName = c.socketName
190         }
191
192         rc := C.govpp_stat_connect(C.CString(sockName))
193         if rc != 0 {
194                 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
195         }
196
197         globalStatClient = c
198         return nil
199 }
200
201 func (c *statClient) Disconnect() error {
202         globalStatClient = nil
203
204         C.govpp_stat_disconnect()
205         return nil
206 }
207
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))
211
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))
217         }
218
219         return stats, nil
220 }
221
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))
225
226         dump := C.govpp_stat_segment_dump(dir)
227         defer C.govpp_stat_segment_data_free(dump)
228
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))
232                 nameChar := v.name
233                 name := C.GoString(nameChar)
234                 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
235
236                 stat := &adapter.StatEntry{
237                         Name: name,
238                         Type: typ,
239                 }
240
241                 switch typ {
242                 case adapter.ScalarIndex:
243                         stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
244
245                 case adapter.ErrorIndex:
246                         stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
247
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))))
254                                 }
255                         }
256                         stat.Data = adapter.SimpleCounterStat(vector)
257
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))),
266                                         })
267                                 }
268                         }
269                         stat.Data = adapter.CombinedCounterStat(vector)
270
271                 default:
272                         fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ)
273                         continue
274
275                 }
276
277                 stats = append(stats, stat)
278         }
279
280         return stats, nil
281 }
282
283 func convertStringSlice(strs []string) **C.uint8_t {
284         var arr **C.uint8_t
285         for _, str := range strs {
286                 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
287         }
288         return arr
289 }