forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
probes.go
148 lines (125 loc) · 3.9 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
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 probes
import (
"github.com/skydive-project/skydive/analyzer"
"github.com/skydive-project/skydive/api"
"github.com/skydive-project/skydive/config"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/flow/mappings"
"github.com/skydive-project/skydive/logging"
"github.com/skydive-project/skydive/probe"
"github.com/skydive-project/skydive/topology/graph"
"github.com/skydive-project/skydive/topology/probes"
)
type FlowProbeBundle struct {
probe.ProbeBundle
Graph *graph.Graph
FlowTableAllocator *flow.TableAllocator
}
type FlowProbeInterface interface {
probe.Probe
RegisterProbe(n *graph.Node, capture *api.Capture, ft *flow.Table) error
UnregisterProbe(n *graph.Node) error
}
type FlowProbe struct {
fpi FlowProbeInterface
pipeline *mappings.FlowMappingPipeline
client *analyzer.Client
}
func (fp FlowProbe) Start() {
fp.fpi.Start()
}
func (fp FlowProbe) Stop() {
fp.fpi.Stop()
}
func (fp *FlowProbe) RegisterProbe(n *graph.Node, capture *api.Capture, ft *flow.Table) error {
return fp.fpi.RegisterProbe(n, capture, ft)
}
func (fp *FlowProbe) UnregisterProbe(n *graph.Node) error {
return fp.fpi.UnregisterProbe(n)
}
func (fp *FlowProbe) AsyncFlowPipeline(flows []*flow.Flow) {
fp.pipeline.Enhance(flows)
if fp.client != nil {
fp.client.SendFlows(flows)
}
}
func (fpb *FlowProbeBundle) UnregisterAllProbes() {
fpb.Graph.Lock()
defer fpb.Graph.Unlock()
for _, n := range fpb.Graph.GetNodes() {
for _, p := range fpb.ProbeBundle.Probes {
fprobe := p.(FlowProbe)
fprobe.UnregisterProbe(n)
}
}
}
func NewFlowProbeBundleFromConfig(tb *probes.TopologyProbeBundle, g *graph.Graph, fta *flow.TableAllocator) *FlowProbeBundle {
list := config.GetConfig().GetStringSlice("agent.flow.probes")
logging.GetLogger().Infof("Flow probes: %v", list)
gfe := mappings.NewGraphFlowEnhancer(g)
var aclient *analyzer.Client
addr, port, err := config.GetAnalyzerClientAddr()
if err != nil {
logging.GetLogger().Errorf("Unable to parse analyzer client: %s", err.Error())
return nil
}
if addr != "" {
aclient, err = analyzer.NewClient(addr, port)
if err != nil {
logging.GetLogger().Errorf("Analyzer client error %s:%d : %s", addr, port, err.Error())
return nil
}
}
probes := make(map[string]probe.Probe)
for _, t := range list {
if _, ok := probes[t]; ok {
continue
}
switch t {
case "ovssflow":
o := NewOvsSFlowProbesHandler(tb, g)
if o != nil {
ofe := mappings.NewOvsFlowEnhancer(g)
pipeline := mappings.NewFlowMappingPipeline(gfe, ofe)
probes[t] = FlowProbe{fpi: o, pipeline: pipeline, client: aclient}
}
case "gopacket":
o := NewGoPacketProbesHandler(g)
if o != nil {
pipeline := mappings.NewFlowMappingPipeline(gfe)
gopacket := FlowProbe{fpi: o, pipeline: pipeline, client: aclient}
probes["afpacket"] = gopacket
probes["pcap"] = gopacket
}
default:
logging.GetLogger().Errorf("unknown probe type %s", t)
}
}
p := probe.NewProbeBundle(probes)
return &FlowProbeBundle{
ProbeBundle: *p,
Graph: g,
FlowTableAllocator: fta,
}
}