-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun.go
379 lines (327 loc) · 11.4 KB
/
run.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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"reflect"
"runtime/debug"
"runtime/pprof"
"sort"
"strings"
"github.com/CN-TU/go-flows/flows"
"github.com/CN-TU/go-flows/packet"
"github.com/CN-TU/go-flows/util"
)
func tableUsage(cmd string, tableset *flag.FlagSet) {
main := "features [featureargs] spec.json [features ...] [export type [exportargs]] [filter type [filterargs]] [label type [filterargs]] input type [inputargs] [...]"
switch cmd {
case "callgraph":
cmdString(fmt.Sprintf("%s %s", cmd, main))
fmt.Fprint(os.Stderr, "\nWrites the resulting callgraph in dot representation to stdout.")
case "run":
cmdString(fmt.Sprintf("%s [args] %s input inputfile [...]", cmd, main))
fmt.Fprint(os.Stderr, `
Parse the packets from input source(s), apply filter(s) and/or label(s) and
export the specified feature set to the specified exporters.`)
}
fmt.Fprintf(os.Stderr, `
Featuresets (features), outputs (export), and, optionally, sources need to
be provided. It is possible to specify multiple feature statements and
multiple export statements. All the specified exporters always export the
features specified by the preceeding feature group.
If multiple sources are specified, processing starts with the first source.
Upon EOF from the source, the next source is used, until every source is
exhausted, after which go-flows exits.
If multiple filters are specified, those are tried in order. All filters
must accept the packet - otherwise it is ignored. If a filter rejects a
packet, the remaining filters are not considered.
If multiple labels are specified, they are used like with multiple sources.
At least one feature specification and one exporter is needed.
Identical exporters can be specified multiple times. Beware, that those will
share a common exporter instance, resulting in a field set specification
per specified featureset, and mixed field sets (depending on the feature
specification).
A list of supported exporters and features can be seen with the list
command. See also %s %s features -h.
Examples:
Export the feature set specified in example.json to example.csv
%s %s features example.json export csv example.csv source [sourcetype ...]
Export the feature sets a.json and b.json to a.csv and b.csv
%s %s features a.json export csv a.csv features b.json export b.csv source [sourcetype ...]
Export the feature sets a.json and b.json to a single common.csv (this
results in a csv with features from a in the odd lines, and features
from b in the even lines)
%s %s features a.json features b.json export common.csv source [sourcetype ...]
`, os.Args[0], cmd, os.Args[0], cmd, os.Args[0], cmd, os.Args[0], cmd)
flags()
fmt.Fprintln(os.Stderr, "\nArgs:")
tableset.PrintDefaults()
}
func init() {
addCommand("run", "Extract flows", parseArguments)
addCommand("callgraph", "Create a callgraph from a flowspecification", parseArguments)
}
func parseFeatures(cmd string, args []string) (arguments []string, features []interface{}, control, filter, key []string, bidirectional, allowZero bool, opt flows.FlowOptions) {
set := flag.NewFlagSet("features", flag.ExitOnError)
set.Usage = func() {
fmt.Fprint(os.Stderr, `
Usage:
features [args] spec.json
Reads feature list, as specified in spec.json.
Args:
`)
set.PrintDefaults()
}
selection := set.Uint("select", 0, "Use nth flow selection (key:nth flow in specification)")
v2 := set.Bool("v2", false, "Force v2 format")
simple := set.Bool("simple", false, "Treat file as if it only contains the flow specification")
set.Parse(args)
if *v2 && *simple {
log.Fatalf("Only one of -v2, or -simple can be chosen\n")
}
if set.NArg() == 0 {
log.Fatalln("features needs a json file as input.")
}
arguments = set.Args()[1:]
format := jsonAuto
switch {
case *v2:
format = jsonV2
case *simple:
format = jsonSimple
}
features, control, filter, key, bidirectional, allowZero, opt = decodeJSON(set.Arg(0), format, int(*selection))
if features == nil {
log.Fatalf("Couldn't parse %s (%d) - features missing\n", set.Arg(0), *selection)
}
if key == nil {
log.Fatalf("Couldn't parse %s (%d) - flow key missing\n", set.Arg(0), *selection)
}
return
}
type featureSpec struct {
features []interface{}
control []string
filter []string
key []string
bidirectional bool
allowZero bool
opt flows.FlowOptions
}
type exportedFeatures struct {
exporter []flows.Exporter
featureset []featureSpec
}
func parseCommandLine(cmd string, args []string) (result []exportedFeatures, exporters map[string]flows.Exporter, filters packet.Filters, sources packet.Sources, labels packet.Labels) {
var featureset []featureSpec
clear := false
var firstexporter []string
exporters = make(map[string]flows.Exporter)
var exportset []flows.Exporter
var err error
for len(args) >= 2 {
typ := args[0]
name := args[1]
switch typ {
case "features":
if clear {
if len(featureset) == 0 {
log.Fatalf("At least one feature is needed for '%s'\n", strings.Join(firstexporter, " "))
}
firstexporter = nil
clear = false
result = append(result, exportedFeatures{exportset, featureset})
featureset = nil
exportset = nil
}
var f featureSpec
args, f.features, f.control, f.filter, f.key, f.bidirectional, f.allowZero, f.opt = parseFeatures(cmd, args[1:])
featureset = append(featureset, f)
case "export":
if firstexporter == nil {
firstexporter = args
}
if len(args) < 1 {
log.Fatalln("Need an export type")
}
var e flows.Exporter
args, e, err = flows.MakeExporter(name, args[2:])
if err != nil {
log.Fatalf("Error creating exporter '%s': %s\n", name, err)
}
if existing, ok := exporters[e.ID()]; ok {
e = existing
} else {
exporters[e.ID()] = e
}
exportset = append(exportset, e)
clear = true
case "source":
if len(args) < 1 {
log.Fatalln("Need a source type")
}
var s packet.Source
args, s, err = packet.MakeSource(name, args[2:])
if err != nil {
log.Fatalf("Error creating source '%s': %s\n", name, err)
}
sources.Append(s)
case "filter":
if len(args) < 1 {
log.Fatalln("Need a filter type")
}
var s packet.Filter
args, s, err = packet.MakeFilter(name, args[2:])
if err != nil {
log.Fatalf("Error creating filter '%s': %s\n", name, err)
}
filters = append(filters, s)
case "label":
if len(args) < 1 {
log.Fatalln("Need a label type")
}
var s packet.Label
args, s, err = packet.MakeLabel(name, args[2:])
if err != nil {
log.Fatalf("Error creating label '%s': %s\n", name, err)
}
labels = append(labels, s)
default:
log.Fatalf("Command (features, export, source, label, filter) missing, instead found '%s'\n", strings.Join(args, " "))
}
}
if len(args) > 0 {
log.Fatalf("Argument at end of input to '%s' is missing!\n", args[0])
}
if clear {
if len(featureset) == 0 {
log.Fatalf("At least one feature is needed for '%s'\n", strings.Join(firstexporter, " "))
}
result = append(result, exportedFeatures{exportset, featureset})
}
return
}
func parseArguments(cmd string, args []string) {
set := flag.NewFlagSet("table", flag.ExitOnError)
set.Usage = func() { tableUsage(cmd, set) }
numProcessing := set.Uint("n", 4, "Number of parallel processing tables")
expireWindow := set.Bool("expireWindow", false, "Expire all flows after every window. Useful if flow key contains a window function")
flowExpire := set.Uint("expire", 100, "Check for expired timers with this period in seconds. expire↓ ⇒ memory↓, execution time↑")
maxPacket := set.Uint("size", 9000, "Maximum packet size handled internally. 0 = automatic")
printStats := set.Bool("stats", false, "Output statistics")
autoGC := set.Bool("scantFlows", false, "If you not have many flows setting this speeds up processing speed, but might cause a huge increase in memory usage.")
sortOrderStr := set.String("sort", "stop", `Sort output by "start" time, "stop" time, "expiry" time, or unsorted ("none")
Beware: Worst case performance of start/stop sorting is O(1), while expiry is O(flow log(flow)) (with average O(flow)).
Both need an additional O(flow) merge part if multiple tables are used.
Additionally, stop might lead to very high memory usage (and longer execution times) in case one long lasting flow keeps all other flows from expiring (active/idle timeout!).`)
verbose := set.Bool("verbose", false, "Verbose output")
set.Parse(args)
if set.NArg() == 0 {
set.Usage()
os.Exit(-1)
}
if *numProcessing == 0 {
log.Fatalln("Need at least one flow processing table!")
}
var result []exportedFeatures
var exporters map[string]flows.Exporter
var filters packet.Filters
var sources packet.Sources
var labels packet.Labels
result, exporters, filters, sources, labels = parseCommandLine(cmd, set.Args())
if len(result) == 0 {
log.Fatalf("At least one exporter is needed!\n")
}
var recordList flows.RecordListMaker
var key []string
var bidirectional, allowZero bool
var opts flows.FlowOptions
first := true
sortOrder, err := flows.AtoSort(*sortOrderStr)
if err != nil {
log.Fatalln(err)
}
for _, featureset := range result {
pipeline, err := flows.MakeExportPipeline(featureset.exporter, sortOrder, *numProcessing)
if err != nil {
log.Fatalln(err)
}
for _, feature := range featureset.featureset {
sort.Strings(feature.key)
if first {
first = false
key = feature.key
bidirectional = feature.bidirectional
allowZero = feature.allowZero
opts = feature.opt
} else {
if !reflect.DeepEqual(key, feature.key) {
log.Fatalln("key_features of every flowspec must match")
}
if bidirectional != feature.bidirectional {
log.Fatalln("bidirectional of every flow must match")
}
if allowZero != feature.allowZero {
log.Fatalln("allowZero of every flow must match")
}
if !reflect.DeepEqual(opts, feature.opt) {
log.Fatalln("timeouts and per packet of every flow must match")
}
}
if err := recordList.AppendRecord(feature.features, feature.control, feature.filter, pipeline, *verbose); err != nil {
log.Fatalf("Couldn't parse feature specification: %s\n", err)
}
}
}
keyselector := packet.MakeDynamicKeySelector(key, bidirectional, allowZero)
if cmd == "callgraph" {
recordList.CallGraph(os.Stdout)
return
}
for _, exporter := range exporters {
exporter.Init()
}
recordList.Init()
flows.CleanupFeatures()
util.CleanupModules()
recordList.Clean()
if !*autoGC {
debug.SetGCPercent(10000000) //We manually call gc after timing out flows; make that optional?
}
opts.WindowExpiry = *expireWindow
opts.SortOutput = sortOrder
flowtable := packet.NewFlowTable(int(*numProcessing), recordList, packet.NewFlow, opts,
flows.DateTimeNanoseconds(*flowExpire)*flows.SecondsInNanoseconds, keyselector, *autoGC)
engine := packet.NewEngine(int(*maxPacket), flowtable, filters, sources, labels)
cancel := make(chan os.Signal, 1)
signal.Notify(cancel, os.Interrupt)
go func() {
<-cancel
log.Println("Canceling...")
engine.Stop()
}()
stopped := engine.Run()
signal.Stop(cancel)
engine.Finish()
if *heapprofile != "" {
f, err := os.Create(*heapprofile)
if err != nil {
log.Fatalln("could not create memory profile: ", err)
}
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatalln("could not write memory profile: ", err)
}
f.Close()
}
flowtable.EOF(stopped)
recordList.Flush()
for _, exporter := range exporters {
exporter.Finish()
}
if *printStats {
engine.PrintStats(os.Stderr)
flowtable.PrintStats(os.Stderr)
}
}