/
parser.go
88 lines (74 loc) · 2.16 KB
/
parser.go
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of Hubble
package debug
import (
"bytes"
"encoding/binary"
"fmt"
flowpb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/hubble/parser/errors"
"github.com/cilium/cilium/pkg/hubble/parser/getters"
"github.com/cilium/cilium/pkg/monitor"
"github.com/cilium/cilium/pkg/monitor/api"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// Parser is a parser for debug payloads
type Parser struct {
log logrus.FieldLogger
endpointGetter getters.EndpointGetter
}
// New creates a new parser
func New(log logrus.FieldLogger, endpointGetter getters.EndpointGetter) (*Parser, error) {
return &Parser{
log: log,
endpointGetter: endpointGetter,
}, nil
}
// Decode takes the a debug event payload obtained from the perf event ring
// buffer and decodes it
func (p *Parser) Decode(data []byte, cpu int) (*flowpb.DebugEvent, error) {
if len(data) == 0 {
return nil, errors.ErrEmptyData
}
eventType := data[0]
if eventType != api.MessageTypeDebug {
return nil, errors.NewErrInvalidType(eventType)
}
dbg := &monitor.DebugMsg{}
if err := binary.Read(bytes.NewReader(data), byteorder.Native, dbg); err != nil {
return nil, fmt.Errorf("failed to parse debug event: %w", err)
}
decoded := &flowpb.DebugEvent{
Type: flowpb.DebugEventType(dbg.SubType),
Source: p.decodeEndpoint(dbg.Source),
Hash: wrapperspb.UInt32(dbg.Hash),
Arg1: wrapperspb.UInt32(dbg.Arg1),
Arg2: wrapperspb.UInt32(dbg.Arg2),
Arg3: wrapperspb.UInt32(dbg.Arg3),
Cpu: wrapperspb.Int32(int32(cpu)),
Message: dbg.Message(),
}
return decoded, nil
}
func (p *Parser) decodeEndpoint(id uint16) *flowpb.Endpoint {
if id == 0 {
return nil
}
epId := uint32(id)
if p.endpointGetter != nil {
if ep, ok := p.endpointGetter.GetEndpointInfoByID(id); ok {
return &flowpb.Endpoint{
ID: epId,
Identity: uint32(ep.GetIdentity()),
Namespace: ep.GetK8sNamespace(),
Labels: ep.GetLabels(),
PodName: ep.GetK8sPodName(),
}
}
}
return &flowpb.Endpoint{
ID: epId,
}
}