/
datapath_trace.go
312 lines (276 loc) · 8.57 KB
/
datapath_trace.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
// Copyright 2016-2019 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 monitor
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net"
"os"
"unsafe"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/types"
)
const (
// traceNotifyCommonLen is the minimum length required to determine the version of the TN event.
traceNotifyCommonLen = 16
// traceNotifyV0Len is the amount of packet data provided in a trace notification v0.
traceNotifyV0Len = 32
// traceNotifyV1Len is the amount of packet data provided in a trace notification v1.
traceNotifyV1Len = 48
// TraceReasonEncryptMask is the bit used to indicate encryption or not
TraceReasonEncryptMask uint8 = 0x80
)
const (
// TraceNotifyFlagIsIPv6 is set in TraceNotify.Flags when the
// notification refers to an IPv6 flow
TraceNotifyFlagIsIPv6 uint8 = 1
)
const (
TraceNotifyVersion0 = iota
TraceNotifyVersion1
)
// TraceNotifyV0 is the common message format for versions 0 and 1.
type TraceNotifyV0 struct {
Type uint8
ObsPoint uint8
Source uint16
Hash uint32
OrigLen uint32
CapLen uint16
Version uint16
SrcLabel identity.NumericIdentity
DstLabel identity.NumericIdentity
DstID uint16
Reason uint8
Flags uint8
Ifindex uint32
// data
}
// TraceNotifyV1 is the version 1 message format.
type TraceNotifyV1 struct {
TraceNotifyV0
OrigIP types.IPv6
// data
}
// TraceNotify is the message format of a trace notification in the BPF ring buffer
type TraceNotify TraceNotifyV1
var (
traceNotifyLength = map[uint16]uint{
TraceNotifyVersion0: traceNotifyV0Len,
TraceNotifyVersion1: traceNotifyV1Len,
}
)
// Reasons for forwarding a packet.
const (
TraceReasonPolicy = iota
TraceReasonCtEstablished
TraceReasonCtReply
TraceReasonCtRelated
TraceReasonCtReopened
)
var traceReasons = map[uint8]string{
TraceReasonPolicy: "new",
TraceReasonCtEstablished: "established",
TraceReasonCtReply: "reply",
TraceReasonCtRelated: "related",
TraceReasonCtReopened: "reopened",
}
func connState(reason uint8) string {
r := reason & ^TraceReasonEncryptMask
if str, ok := traceReasons[r]; ok {
return str
}
return fmt.Sprintf("%d", reason)
}
func fetchVersion(data []byte, tn *TraceNotify) (version uint16, err error) {
offset := unsafe.Offsetof(tn.Version)
length := unsafe.Sizeof(tn.Version)
reader := bytes.NewReader(data[offset : offset+length])
err = binary.Read(reader, byteorder.Native, &version)
return version, err
}
// DecodeTraceNotify will decode 'data' into the provided TraceNotify structure
func DecodeTraceNotify(data []byte, tn *TraceNotify) error {
if len(data) < traceNotifyCommonLen {
return fmt.Errorf("Unknown trace event")
}
version, err := fetchVersion(data, tn)
if err != nil {
return err
}
switch version {
case TraceNotifyVersion0:
err = binary.Read(bytes.NewReader(data), byteorder.Native, &tn.TraceNotifyV0)
case TraceNotifyVersion1:
err = binary.Read(bytes.NewReader(data), byteorder.Native, tn)
default:
err = fmt.Errorf("Unrecognized trace event (version %d)", version)
}
return err
}
// dumpIdentity dumps the source and destination identities in numeric or
// human-readable format.
func (n *TraceNotify) dumpIdentity(buf *bufio.Writer, numeric DisplayFormat) {
if numeric {
fmt.Fprintf(buf, "identity %d->%d", n.SrcLabel, n.DstLabel)
} else {
fmt.Fprintf(buf, "identity %s->%s", n.SrcLabel, n.DstLabel)
}
}
func (n *TraceNotify) encryptReason() string {
if (n.Reason & TraceReasonEncryptMask) != 0 {
return "encrypted "
}
return ""
}
func (n *TraceNotify) traceReason() string {
return connState(n.Reason)
}
func (n *TraceNotify) traceSummary() string {
switch n.ObsPoint {
case api.TraceToLxc:
return fmt.Sprintf("-> endpoint %d", n.DstID)
case api.TraceToProxy:
return "-> proxy"
case api.TraceToHost:
return "-> host from"
case api.TraceToStack:
return "-> stack"
case api.TraceToOverlay:
return "-> overlay"
case api.TraceToNetwork:
return "-> network"
case api.TraceFromLxc:
return fmt.Sprintf("<- endpoint %d", n.Source)
case api.TraceFromProxy:
return "<- proxy"
case api.TraceFromHost:
return "<- host"
case api.TraceFromStack:
return "<- stack"
case api.TraceFromOverlay:
return "<- overlay"
case api.TraceFromNetwork:
return "<- network"
default:
return "unknown trace"
}
}
// OriginalIP returns the original source IP if reverse NAT was performed on
// the flow
func (n *TraceNotify) OriginalIP() net.IP {
if (n.Flags & TraceNotifyFlagIsIPv6) != 0 {
return n.OrigIP[:]
}
return n.OrigIP[:4]
}
// DataOffset returns the offset from the beginning of TraceNotify where the
// trace notify data begins.
//
// Returns zero for invalid or unknown TraceNotify messages.
func (n *TraceNotify) DataOffset() uint {
return traceNotifyLength[n.Version]
}
// DumpInfo prints a summary of the trace messages.
func (n *TraceNotify) DumpInfo(data []byte, numeric DisplayFormat) {
buf := bufio.NewWriter(os.Stdout)
hdrLen := n.DataOffset()
if n.encryptReason() != "" {
fmt.Fprintf(buf, "%s %s flow %#x ",
n.traceSummary(), n.encryptReason(), n.Hash)
} else {
fmt.Fprintf(buf, "%s flow %#x ", n.traceSummary(), n.Hash)
}
n.dumpIdentity(buf, numeric)
fmt.Fprintf(buf, " state %s ifindex %s orig-ip %s: %s\n", n.traceReason(),
ifname(int(n.Ifindex)), n.OriginalIP().String(), GetConnectionSummary(data[hdrLen:]))
buf.Flush()
}
// DumpVerbose prints the trace notification in human readable form
func (n *TraceNotify) DumpVerbose(dissect bool, data []byte, prefix string, numeric DisplayFormat) {
buf := bufio.NewWriter(os.Stdout)
fmt.Fprintf(buf, "%s MARK %#x FROM %d %s: %d bytes (%d captured), state %s",
prefix, n.Hash, n.Source, api.TraceObservationPoint(n.ObsPoint), n.OrigLen, n.CapLen, connState(n.Reason))
if n.Ifindex != 0 {
fmt.Fprintf(buf, ", interface %s", ifname(int(n.Ifindex)))
}
if n.SrcLabel != 0 || n.DstLabel != 0 {
n.dumpIdentity(buf, numeric)
}
fmt.Fprintf(buf, ", orig-ip %s", n.OriginalIP().String())
if n.DstID != 0 {
fmt.Fprintf(buf, ", to endpoint %d\n", n.DstID)
} else {
fmt.Fprintf(buf, "\n")
}
hdrLen := n.DataOffset()
if n.CapLen > 0 && len(data) > int(hdrLen) {
Dissect(dissect, data[hdrLen:])
}
buf.Flush()
}
func (n *TraceNotify) getJSON(data []byte, cpuPrefix string) (string, error) {
v := TraceNotifyToVerbose(n)
v.CPUPrefix = cpuPrefix
hdrLen := n.DataOffset()
if n.CapLen > 0 && len(data) > int(hdrLen) {
v.Summary = GetDissectSummary(data[hdrLen:])
}
ret, err := json.Marshal(v)
return string(ret), err
}
// DumpJSON prints notification in json format
func (n *TraceNotify) DumpJSON(data []byte, cpuPrefix string) {
resp, err := n.getJSON(data, cpuPrefix)
if err == nil {
fmt.Println(resp)
}
}
// TraceNotifyVerbose represents a json notification printed by monitor
type TraceNotifyVerbose struct {
CPUPrefix string `json:"cpu,omitempty"`
Type string `json:"type,omitempty"`
Mark string `json:"mark,omitempty"`
Ifindex string `json:"ifindex,omitempty"`
State string `json:"state,omitempty"`
ObservationPoint string `json:"observationPoint"`
TraceSummary string `json:"traceSummary"`
Source uint16 `json:"source"`
Bytes uint32 `json:"bytes"`
SrcLabel identity.NumericIdentity `json:"srcLabel"`
DstLabel identity.NumericIdentity `json:"dstLabel"`
DstID uint16 `json:"dstID"`
Summary *DissectSummary `json:"summary,omitempty"`
}
// TraceNotifyToVerbose creates verbose notification from base TraceNotify
func TraceNotifyToVerbose(n *TraceNotify) TraceNotifyVerbose {
return TraceNotifyVerbose{
Type: "trace",
Mark: fmt.Sprintf("%#x", n.Hash),
Ifindex: ifname(int(n.Ifindex)),
State: connState(n.Reason),
ObservationPoint: api.TraceObservationPoint(n.ObsPoint),
TraceSummary: n.traceSummary(),
Source: n.Source,
Bytes: n.OrigLen,
SrcLabel: n.SrcLabel,
DstLabel: n.DstLabel,
DstID: n.DstID,
}
}