/
parserfactory.go
113 lines (105 loc) · 5.93 KB
/
parserfactory.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
// Copyright 2018 Authors of Cilium
//
// 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 proxylib
import (
log "github.com/sirupsen/logrus"
)
// A parser instance is used for each connection. OnData will be called from a single thread only.
type Parser interface {
// OnData() is called when input is available on the underlying connection. The Parser
// instance is only ever used for processing data of a single connection, which allows
// the parser instance to keep connection specific state. All OnData() calls for a
// single connection (both directions) are made from a single thread, so that
// no locking is needed for the parser instance if no other goroutines need to access
// the parser instance. (Note that any L7 policy protocol rule parsing happens in
// other goroutine so any such parsing should not access parser instances directly.)
//
// OnData() parameters are as follows:
// 'reply' is 'false' for original direction of the connection, 'true' otherwise.
// 'endStream' is true if there is no more data after 'data' in this direction.
// 'data' is the available data in the current direction. The datapath buffers
// partial frames as instructed by the operations returned by the parser
// so that the 'data' always starts on a frame boundary. That is, whenever
// the parser returns `MORE` indicating it needs more input, the bytes
// not 'PASS'ed or 'DROP'ped are retained in a datapath buffer and those
// same bytes are passed to the parser again when more input is available.
// 'data' may be an empty slice, but the slices contained are never empty.
//
// OnData() returns an operation and the number of bytes ('N') the operation applies.
// The possible values for 'op' are:
// 'MORE' - Data currently in 'data' is to be retained by the datapath and passed
// again to OnData() after 'N' bytes more data is available.
// 'PASS' - Allow 'N' bytes.
// 'DROP' - Drop 'N' bytes and call OnData() again for the remaining data.
// 'INJECT' - Insert 'N' bytes of data placed into the inject buffer in to the
// data stream in this direction.
// 'NOP' - Do nothing, to be used when it is known if no more input
// is to be expected.
// 'ERROR' - Protocol parsing failed and the connection should be closed.
//
// OnData() is called again after 'PASS', 'DROP', and 'INJECT' with the remaining
// data even if none remains.
OnData(reply, endStream bool, data [][]byte) (op OpType, N int)
}
// An alternate parser instance is used for each connection. OnData will be called from a single thread only.
type ReaderParser interface {
// OnData() is called when input is available on the underlying connection. The Parser
// instance is only ever used for processing data of a single connection, which allows
// the parser instance to keep connection specific state. All OnData() calls for a
// single connection (both directions) are made from a single thread, so that
// no locking is needed for the parser instance if no other goroutines need to access
// the parser instance. (Note that any L7 policy protocol rule parsing happens in
// other goroutine so any such parsing should not access parser instances directly.)
//
// OnData() parameters are as follows:
// 'reply' is 'false' for original direction of the connection, 'true' otherwise.
// 'endStream' is true if there is no more data after 'data' in this direction.
// 'data' is the available data in the current direction. The datapath buffers
// partial frames as instructed by the operations returned by the parser
// so that the 'data' always starts on a frame boundary. That is, whenever
// the parser returns `MORE` indicating it needs more input, the bytes
// not 'PASS'ed or 'DROP'ped are retained in a datapath buffer and those
// same bytes are passed to the parser again when more input is available.
// 'data' may be an empty slice, but the slices contained are never empty.
//
// OnData() returns an operation and the number of bytes ('N') the operation applies.
// The possible values for 'op' are:
// 'MORE' - Data currently in 'data' is to be retained by the datapath and passed
// again to OnData() after 'N' bytes more data is available.
// 'PASS' - Allow 'N' bytes.
// 'DROP' - Drop 'N' bytes and call OnData() again for the remaining data.
// 'INJECT' - Insert 'N' bytes of data placed into the inject buffer in to the
// data stream in this direction.
// 'NOP' - Do nothing, to be used when it is known if no more input
// is to be expected.
// 'ERROR' - Protocol parsing failed and the connection should be closed.
//
// OnData() is called again after 'PASS', 'DROP', and 'INJECT' with the remaining
// data even if none remains.
OnData(reply bool, reader *Reader) (op OpType, N int)
}
type ParserFactory interface {
Create(connection *Connection) interface{} // must be thread safe!
}
// const after initialization
var parserFactories map[string]ParserFactory = make(map[string]ParserFactory)
// RegisterParserFactory adds a protocol parser factory to the map of known parsers.
// This is called from parser init() functions while we are still single-threaded
func RegisterParserFactory(name string, parserFactory ParserFactory) {
log.Debugf("proxylib: Registering L7 parser: %v", name)
parserFactories[name] = parserFactory
}
func GetParserFactory(name string) ParserFactory {
return parserFactories[name]
}