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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
// Copyright (c) 2019 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.
package statsclient
import (
"bytes"
"fmt"
"net"
"os"
"regexp"
"sync/atomic"
"syscall"
"time"
"unsafe"
"github.com/ftrvxmtrx/fd"
logger "github.com/sirupsen/logrus"
"git.fd.io/govpp.git/adapter"
)
var (
// Debug is global variable that determines debug mode
Debug = os.Getenv("DEBUG_GOVPP_STATS") != ""
// Log is global logger
Log = logger.New()
)
// init initializes global logger, which logs debug level messages to stdout.
func init() {
Log.Out = os.Stdout
if Debug {
Log.Level = logger.DebugLevel
Log.Debug("enabled debug mode")
}
}
// StatsClient is the pure Go implementation for VPP stats API.
type StatsClient struct {
sockAddr string
currentEpoch int64
sharedHeader []byte
directoryVector uintptr
memorySize int
}
// NewStatsClient returns new VPP stats API client.
func NewStatsClient(socketName string) *StatsClient {
return &StatsClient{
sockAddr: socketName,
}
}
func (c *StatsClient) Connect() error {
var sockName string
if c.sockAddr == "" {
sockName = adapter.DefaultStatsSocket
} else {
sockName = c.sockAddr
}
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)
}
if err := c.statSegmentConnect(sockName); err != nil {
return err
}
return nil
}
const statshmFilename = "statshm"
func (c *StatsClient) statSegmentConnect(sockName string) error {
addr := &net.UnixAddr{
Net: "unixpacket",
Name: sockName,
}
Log.Debugf("connecting to: %v", addr)
conn, err := net.DialUnix(addr.Net, nil, addr)
if err != nil {
Log.Warnf("connecting to socket %s failed: %s", addr, err)
return err
}
defer func() {
if err := conn.Close(); err != nil {
Log.Warnf("closing socket failed: %v", err)
}
}()
Log.Debugf("connected to socket: %v", addr)
files, err := fd.Get(conn, 1, []string{statshmFilename})
if err != nil {
return fmt.Errorf("getting file descriptor over socket failed: %v", err)
} else if len(files) == 0 {
return fmt.Errorf("no files received over socket")
}
defer func() {
for _, f := range files {
if err := f.Close(); err != nil {
Log.Warnf("closing file %s failed: %v", f.Name(), err)
}
}
}()
Log.Debugf("received %d files over socket", len(files))
f := files[0]
info, err := f.Stat()
if err != nil {
return err
}
size := int(info.Size())
Log.Debugf("fd: name=%v size=%v", info.Name(), size)
data, err := syscall.Mmap(int(f.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
Log.Warnf("mapping shared memory failed: %v", err)
return fmt.Errorf("mapping shared memory failed: %v", err)
}
Log.Debugf("successfuly mapped shared memory")
c.sharedHeader = data
c.memorySize = size
return nil
}
func (c *StatsClient) Disconnect() error {
err := syscall.Munmap(c.sharedHeader)
if err != nil {
Log.Warnf("unmapping shared memory failed: %v", err)
return fmt.Errorf("unmapping shared memory failed: %v", err)
}
Log.Debugf("successfuly unmapped shared memory")
return nil
}
func nameMatches(name string, patterns []string) bool {
if len(patterns) == 0 {
return true
}
for _, pattern := range patterns {
matched, err := regexp.MatchString(pattern, name)
if err == nil && matched {
return true
}
}
return false
}
func (c *StatsClient) ListStats(patterns ...string) (statNames []string, err error) {
sa := c.accessStart()
if sa == nil {
return nil, fmt.Errorf("access failed")
}
dirOffset, _, _ := c.readOffsets()
Log.Debugf("dirOffset: %v", dirOffset)
vecLen := vectorLen(unsafe.Pointer(&c.sharedHeader[dirOffset]))
Log.Debugf("vecLen: %v", vecLen)
Log.Debugf("unsafe.Sizeof(statSegDirectoryEntry{}): %v", unsafe.Sizeof(statSegDirectoryEntry{}))
for i := uint64(0); i < vecLen; i++ {
offset := uintptr(i) * unsafe.Sizeof(statSegDirectoryEntry{})
dirEntry := (*statSegDirectoryEntry)(add(unsafe.Pointer(&c.sharedHeader[dirOffset]), offset))
nul := bytes.IndexByte(dirEntry.name[:], '\x00')
if nul < 0 {
Log.Warnf("no zero byte found for: %q", dirEntry.name[:])
continue
}
name := string(dirEntry.name[:nul])
Log.Debugf(" %80q (type: %v, data: %d, offset: %d) ", name, dirEntry.directoryType, dirEntry.unionData, dirEntry.offsetVector)
if nameMatches(name, patterns) {
statNames = append(statNames, name)
}
// TODO: copy the listed entries elsewhere
}
if !c.accessEnd(sa) {
return nil, adapter.ErrStatDirBusy
}
c.currentEpoch = sa.epoch
return statNames, nil
}
func (c *StatsClient) DumpStats(patterns ...string) (entries []*adapter.StatEntry, err error) {
epoch, _ := c.readEpoch()
if c.currentEpoch > 0 && c.currentEpoch != epoch { // TODO: do list stats before dump
return nil, fmt.Errorf("old data")
}
sa := c.accessStart()
if sa == nil {
return nil, fmt.Errorf("access failed")
}
dirOffset, _, _ := c.readOffsets()
vecLen := vectorLen(unsafe.Pointer(&c.sharedHeader[dirOffset]))
for i := uint64(0); i < vecLen; i++ {
offset := uintptr(i) * unsafe.Sizeof(statSegDirectoryEntry{})
dirEntry := (*statSegDirectoryEntry)(add(unsafe.Pointer(&c.sharedHeader[dirOffset]), offset))
entry := c.copyData(dirEntry)
if nameMatches(entry.Name, patterns) {
entries = append(entries, &entry)
}
}
if !c.accessEnd(sa) {
return nil, adapter.ErrStatDumpBusy
}
return entries, nil
}
func (c *StatsClient) copyData(dirEntry *statSegDirectoryEntry) (statEntry adapter.StatEntry) {
name := dirEntry.name[:]
if nul := bytes.IndexByte(name, '\x00'); nul < 0 {
Log.Warnf("no zero byte found for: %q", dirEntry.name[:])
} else {
name = dirEntry.name[:nul]
}
statEntry.Name = string(name)
statEntry.Type = adapter.StatType(dirEntry.directoryType)
Log.Debugf(" - %s (type: %v, data: %v, offset: %v) ", statEntry.Name, statEntry.Type, dirEntry.unionData, dirEntry.offsetVector)
switch statEntry.Type {
case adapter.ScalarIndex:
statEntry.Data = adapter.ScalarStat(dirEntry.unionData)
case adapter.ErrorIndex:
_, errOffset, _ := c.readOffsets()
offsetVector := unsafe.Pointer(&c.sharedHeader[errOffset])
vecLen := vectorLen(offsetVector)
var errData adapter.Counter
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
offset := uintptr(cb) + uintptr(dirEntry.unionData)*unsafe.Sizeof(adapter.Counter(0))
val := *(*adapter.Counter)(add(unsafe.Pointer(&c.sharedHeader[0]), offset))
errData += val
}
statEntry.Data = adapter.ErrorStat(errData)
case adapter.SimpleCounterVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
simpleCounter := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(simpleCounter)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([][]adapter.Counter, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
vecLen2 := vectorLen(counterVec)
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
val := *(*adapter.Counter)(add(counterVec, offset))
data[i] = append(data[i], val)
}
}
statEntry.Data = adapter.SimpleCounterStat(data)
case adapter.CombinedCounterVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
combinedCounter := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(combinedCounter)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([][]adapter.CombinedCounter, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
vecLen2 := vectorLen(counterVec)
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
val := *(*adapter.CombinedCounter)(add(counterVec, offset))
data[i] = append(data[i], val)
}
}
statEntry.Data = adapter.CombinedCounterStat(data)
case adapter.NameVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
nameVector := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(nameVector)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([]adapter.Name, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
nameVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
vecLen2 := vectorLen(nameVec)
var nameStr []byte
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(byte(0))
val := *(*byte)(add(nameVec, offset))
if val > 0 {
nameStr = append(nameStr, val)
}
}
data[i] = adapter.Name(nameStr)
}
statEntry.Data = adapter.NameStat(data)
default:
Log.Warnf("Unknown type %d for stat entry: %s", statEntry.Type, statEntry.Name)
}
Log.Debugf("\tentry data: %#v", statEntry.Data)
return statEntry
}
type statDirectoryType int32
func (t statDirectoryType) String() string {
return adapter.StatType(t).String()
}
type statSegDirectoryEntry struct {
directoryType statDirectoryType
// unionData can represent: offset, index or value
unionData uint64
offsetVector uint64
name [128]byte
}
type statSegSharedHeader struct {
version uint64
epoch int64
inProgress int64
directoryOffset int64
errorOffset int64
statsOffset int64
}
func (c *StatsClient) readVersion() uint64 {
header := *(*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
version := atomic.LoadUint64(&header.version)
return version
}
func (c *StatsClient) readEpoch() (int64, bool) {
header := *(*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
epoch := atomic.LoadInt64(&header.epoch)
inprog := atomic.LoadInt64(&header.inProgress)
return epoch, inprog != 0
}
func (c *StatsClient) readOffsets() (dir, err, stat int64) {
header := *(*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
dirOffset := atomic.LoadInt64(&header.directoryOffset)
errOffset := atomic.LoadInt64(&header.errorOffset)
statOffset := atomic.LoadInt64(&header.statsOffset)
return dirOffset, errOffset, statOffset
}
type statSegAccess struct {
epoch int64
}
var maxWaitInProgress = 1 * time.Second
func (c *StatsClient) accessStart() *statSegAccess {
epoch, inprog := c.readEpoch()
t := time.Now()
for inprog {
if time.Since(t) > maxWaitInProgress {
return nil
}
epoch, inprog = c.readEpoch()
}
return &statSegAccess{
epoch: epoch,
}
}
func (c *StatsClient) accessEnd(acc *statSegAccess) bool {
epoch, inprog := c.readEpoch()
if acc.epoch != epoch || inprog {
return false
}
return true
}
type vecHeader struct {
length uint64
vectorData [0]uint8
}
func vectorLen(v unsafe.Pointer) uint64 {
vec := *(*vecHeader)(unsafe.Pointer(uintptr(v) - unsafe.Sizeof(uintptr(0))))
return vec.length
}
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
|