aboutsummaryrefslogtreecommitdiffstats
path: root/adapter/vppapiclient/stat_client.go
blob: 0ab088c8276fcd480295df0b283493935e5a0a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows,!darwin,!novpp,!beyond18.10

package vppapiclient

/*
#cgo CFLAGS: -DPNG_DEBUG=1
#cgo LDFLAGS: -lvppapiclient

#include "stat_client_wrapper.h"
*/
import "C"

import (
	"fmt"
	"os"
	"unsafe"

	"git.fd.io/govpp.git/adapter"
)

// global VPP stats API client, library vppapiclient only supports
// single connection at a time
var globalStatClient *statClient

// statClient is the default implementation of StatsAPI.
type statClient struct {
	socketName string
}

// NewStatClient returns new VPP stats API client.
func NewStatClient(socketName string) adapter.StatsAPI {
	return &statClient{
		socketName: socketName,
	}
}

func (c *statClient) Connect() error {
	if globalStatClient != nil {
		return fmt.Errorf("already connected to stats API, disconnect first")
	}

	var sockName string
	if c.socketName == "" {
		sockName = adapter.DefaultStatsSocket
	} else {
		sockName = c.socketName
	}

	if _, err := os.Stat(sockName); err != nil {
		if os.IsNotExist(err) {
			return fmt.Errorf("stats socket file %q does not exists, ensure that VPP is running with `statseg { ... }` section in config", sockName)
		}
		return fmt.Errorf("stats socket file error: %v", err)
	}

	rc := C.govpp_stat_connect(C.CString(sockName))
	if rc != 0 {
		return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
	}

	globalStatClient = c
	return nil
}

func (c *statClient) Disconnect() error {
	globalStatClient = nil

	C.govpp_stat_disconnect()
	return nil
}

func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
	dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
	if dir == nil {
		return nil, adapter.ErrStatDirBusy
	}
	defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))

	l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
	for i := 0; i < int(l); i++ {
		nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
		stats = append(stats, C.GoString(nameChar))
		C.free(unsafe.Pointer(nameChar))
	}

	return stats, nil
}

func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
	dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
	if dir == nil {
		return nil, adapter.ErrStatDirBusy
	}
	defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))

	dump := C.govpp_stat_segment_dump(dir)
	if dump == nil {
		return nil, adapter.ErrStatDumpBusy
	}
	defer C.govpp_stat_segment_data_free(dump)

	l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
	for i := 0; i < int(l); i++ {
		v := C.govpp_stat_segment_dump_index(dump, C.int(i))
		nameChar := v.name
		name := C.GoString(nameChar)
		typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))

		stat := &adapter.StatEntry{
			Name: name,
			Type: typ,
		}

		switch typ {
		case adapter.ScalarIndex:
			stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))

		case adapter.ErrorIndex:
			stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))

		case adapter.SimpleCounterVector:
			length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
			vector := make([][]adapter.Counter, length)
			for k := 0; k < length; k++ {
				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++ {
					vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, C.int(k), C.int(j))))
				}
			}
			stat.Data = adapter.SimpleCounterStat(vector)

		case adapter.CombinedCounterVector:
			length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
			vector := make([][]adapter.CombinedCounter, length)
			for k := 0; k < length; k++ {
				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++ {
					vector[k] = append(vector[k], adapter.CombinedCounter{
						Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, C.int(k), C.int(j))),
						Bytes:   adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, C.int(k), C.int(j))),
					})
				}
			}
			stat.Data = adapter.CombinedCounterStat(vector)

		case adapter.NameVector:
			length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_name_vector(&v))))
			var vector []adapter.Name
			for k := 0; k < length; k++ {
				s := C.govpp_stat_segment_data_get_name_vector_index(&v, C.int(k))
				var name adapter.Name
				if s != nil {
					name = adapter.Name(C.GoString(s))
				}
				vector = append(vector, name)
			}
			stat.Data = adapter.NameStat(vector)

		default:
			fmt.Fprintf(os.Stderr, "invalid stat type: %v (%v)\n", typ, name)
			continue

		}

		stats = append(stats, stat)
	}

	return stats, nil
}

func convertStringSlice(strs []string) **C.uint8_t {
	var arr **C.uint8_t
	for _, str := range strs {
		arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
	}
	return arr
}