-
Notifications
You must be signed in to change notification settings - Fork 0
/
probes.go
179 lines (155 loc) · 5.28 KB
/
probes.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
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
//go:generate go run ../../scripts/gendecoder.go -package github.com/skydive-project/skydive/flow/probes
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 ofthe 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 specificlanguage governing permissions and
* limitations under the License.
*
*/
package probes
import (
"encoding/json"
"fmt"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/config"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/logging"
"github.com/skydive-project/skydive/ondemand"
"github.com/skydive-project/skydive/probe"
)
// ErrProbeNotCompiled is thrown when a flow probe was not compiled within the binary
var ErrProbeNotCompiled = fmt.Errorf("probe is not compiled within skydive")
// Probe defines an active flow probe
type Probe = ondemand.Task
// FlowProbeHandler defines flow probe mechanism
type FlowProbeHandler interface {
probe.Handler // inheritance of the probe.Handler interface Start/Stop functions
RegisterProbe(n *graph.Node, capture *types.Capture, e ProbeEventHandler) (Probe, error)
UnregisterProbe(n *graph.Node, e ProbeEventHandler, p Probe) error
CaptureTypes() []string
Init(ctx Context, bundle *probe.Bundle) (FlowProbeHandler, error)
}
// Context defines a context to be used by constructor of probes
type Context struct {
Logger logging.Logger
Config config.Config
Graph *graph.Graph
FTA *flow.TableAllocator
TB *probe.Bundle
}
// ProbeEventHandler used by probes to notify state
type ProbeEventHandler interface {
OnStarted(*CaptureMetadata)
OnStopped()
OnError(err error)
}
// NewFlowProbeBundle returns a new bundle of flow probes
func NewFlowProbeBundle(tb *probe.Bundle, g *graph.Graph, fta *flow.TableAllocator) *probe.Bundle {
list := []string{"pcapsocket", "ovssflow", "sflow", "gopacket", "dpdk", "ebpf", "ovsmirror", "ovsnetflow"}
logging.GetLogger().Infof("Flow probes: %v", list)
var handler FlowProbeHandler
var err error
bundle := probe.NewBundle()
ctx := Context{
Logger: logging.GetLogger(),
Config: config.GetConfig(),
Graph: g,
FTA: fta,
TB: tb,
}
for _, t := range list {
if bundle.GetHandler(t) != nil {
continue
}
switch t {
case "pcapsocket":
handler, err = new(PcapSocketProbeHandler).Init(ctx, bundle)
case "ovssflow":
handler, err = new(OvsSFlowProbesHandler).Init(ctx, bundle)
case "ovsmirror":
handler, err = new(OvsMirrorProbesHandler).Init(ctx, bundle)
case "gopacket":
handler, err = new(GoPacketProbesHandler).Init(ctx, bundle)
case "sflow":
handler, err = new(SFlowProbesHandler).Init(ctx, bundle)
case "ovsnetflow":
handler, err = new(OvsNetFlowProbesHandler).Init(ctx, bundle)
case "dpdk":
handler, err = new(DPDKProbesHandler).Init(ctx, bundle)
case "ebpf":
handler, err = new(EBPFProbesHandler).Init(ctx, bundle)
default:
err = fmt.Errorf("unknown probe type %s", t)
}
if err != nil {
if err != ErrProbeNotCompiled {
logging.GetLogger().Errorf("Failed to create %s probe: %s", t, err)
} else {
logging.GetLogger().Infof("Not compiled with %s support, skipping it", t)
}
continue
}
for _, captureType := range handler.CaptureTypes() {
bundle.AddHandler(captureType, handler)
}
}
return bundle
}
func tableOptsFromCapture(capture *types.Capture) flow.TableOpts {
layerKeyMode, _ := flow.LayerKeyModeByName(capture.LayerKeyMode)
return flow.TableOpts{
RawPacketLimit: int64(capture.RawPacketLimit),
ExtraTCPMetric: capture.ExtraTCPMetric,
IPDefrag: capture.IPDefrag,
ReassembleTCP: capture.ReassembleTCP,
LayerKeyMode: layerKeyMode,
ExtraLayers: capture.ExtraLayers,
}
}
// Captures holds the captures metadata
// easyjson:json
// gendecoder
type Captures []*CaptureMetadata
// CaptureMetadata holds attributes and statistics about a capture
// easyjson:json
// gendecoder
type CaptureMetadata struct {
CaptureStats
ID string `json:",omitempty"`
State string `json:",omitempty"`
Name string `json:",omitempty"`
Description string `json:",omitempty"`
BPFFilter string `json:",omitempty"`
Type string `json:",omitempty"`
PCAPSocket string `json:",omitempty"`
MirrorOf string `json:",omitempty"`
SFlowSocket string `json:",omitempty"`
Error string `json:",omitempty"`
}
// CaptureStats describes the statistics of a running capture
// easyjson:json
// gendecoder
type CaptureStats struct {
PacketsReceived int64
PacketsDropped int64
PacketsIfDropped int64
}
// CapturesMetadataDecoder implements a json message raw decoder
func CapturesMetadataDecoder(raw json.RawMessage) (common.Getter, error) {
var captures Captures
if err := json.Unmarshal(raw, &captures); err != nil {
return nil, fmt.Errorf("unable to unmarshal captures metadata %s: %s", string(raw), err)
}
return &captures, nil
}