-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
datapath_trace.go
380 lines (335 loc) · 10.5 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
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
380
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package monitor
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"unsafe"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/hubble/parser/getters"
"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.
// This struct needs to be kept in sync with the decodeTraceNotifyVersion0
// func.
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
}
// decodeTraceNotifyVersion0 decodes the trace notify message in 'data' into
// the struct. This function needs to be kept in sync with the TraceNotifyV0
// struct.
func (tn *TraceNotifyV0) decodeTraceNotifyVersion0(data []byte) error {
// This eliminates the bounds check in the accesses to `data` below.
if l := len(data); l < traceNotifyV0Len {
return fmt.Errorf("unexpected TraceNotifyV0 data length, expected %d but got %d", traceNotifyV0Len, l)
}
tn.Type = data[0]
tn.ObsPoint = data[1]
tn.Source = byteorder.Native.Uint16(data[2:4])
tn.Hash = byteorder.Native.Uint32(data[4:8])
tn.OrigLen = byteorder.Native.Uint32(data[8:12])
tn.CapLen = byteorder.Native.Uint16(data[12:14])
tn.Version = byteorder.Native.Uint16(data[14:16])
tn.SrcLabel = identity.NumericIdentity(byteorder.Native.Uint32(data[16:20]))
tn.DstLabel = identity.NumericIdentity(byteorder.Native.Uint32(data[20:24]))
tn.DstID = byteorder.Native.Uint16(data[24:26])
tn.Reason = data[26]
tn.Flags = data[27]
tn.Ifindex = byteorder.Native.Uint32(data[28:32])
return nil
}
// TraceNotifyV1 is the version 1 message format. This struct needs to be kept
// in sync with the decodeTraceNotifyVersion1 func.
type TraceNotifyV1 struct {
TraceNotifyV0
OrigIP types.IPv6
// data
}
// decodeTraceNotifyVersion1 decodes the trace notify message in 'data' into
// the struct. This function needs to be kept in sync with the TraceNotifyV1
// struct.
func (tn *TraceNotifyV1) decodeTraceNotifyVersion1(data []byte) error {
if l := len(data); l < traceNotifyV1Len {
return fmt.Errorf("unexpected TraceNotifyV1 data length, expected %d but got %d", traceNotifyV1Len, l)
}
if err := tn.decodeTraceNotifyVersion0(data); err != nil {
return err
}
copy(tn.OrigIP[:], data[32:48])
return nil
}
// 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
TraceReasonUnknown
TraceReasonSRv6Encap
TraceReasonSRv6Decap
)
var traceReasons = map[uint8]string{
TraceReasonPolicy: "new",
TraceReasonCtEstablished: "established",
TraceReasonCtReply: "reply",
TraceReasonCtRelated: "related",
TraceReasonCtReopened: "reopened",
TraceReasonUnknown: "unknown",
TraceReasonSRv6Encap: "srv6-encap",
TraceReasonSRv6Decap: "srv6-decap",
}
func connState(reason uint8) string {
r := reason & ^TraceReasonEncryptMask
if str, ok := traceReasons[r]; ok {
return str
}
return fmt.Sprintf("%d", reason)
}
func TraceReasonIsKnown(reason uint8) bool {
switch reason {
case TraceReasonUnknown:
return false
default:
return true
}
}
func TraceReasonIsEncap(reason uint8) bool {
switch reason {
case TraceReasonSRv6Encap:
return true
default:
return false
}
}
func TraceReasonIsDecap(reason uint8) bool {
switch reason {
case TraceReasonSRv6Decap:
return true
default:
return false
}
}
// 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")
}
offset := unsafe.Offsetof(tn.Version)
length := unsafe.Sizeof(tn.Version)
version := byteorder.Native.Uint16(data[offset : offset+length])
switch version {
case TraceNotifyVersion0:
return tn.decodeTraceNotifyVersion0(data)
case TraceNotifyVersion1:
return ((*TraceNotifyV1)(tn)).decodeTraceNotifyVersion1(data)
}
return fmt.Errorf("Unrecognized trace event (version %d)", version)
}
// 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:
pp := ""
if n.DstID != 0 {
pp = fmt.Sprintf(" port %d", n.DstID)
}
return "-> proxy" + pp
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, linkMonitor getters.LinkGetter) {
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)
ifname := linkMonitor.Name(n.Ifindex)
fmt.Fprintf(buf, " state %s ifindex %s orig-ip %s: %s\n", n.traceReason(),
ifname, 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, linkMonitor getters.LinkGetter) {
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 {
ifname := linkMonitor.Name(n.Ifindex)
fmt.Fprintf(buf, ", interface %s", ifname)
}
if n.SrcLabel != 0 || n.DstLabel != 0 {
fmt.Fprintf(buf, ", ")
n.dumpIdentity(buf, numeric)
}
fmt.Fprintf(buf, ", orig-ip %s", n.OriginalIP().String())
if n.DstID != 0 {
dst := "endpoint"
if n.ObsPoint == api.TraceToProxy {
dst = "proxy-port"
}
fmt.Fprintf(buf, ", to %s %d\n", dst, 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, linkMonitor getters.LinkGetter) (string, error) {
v := TraceNotifyToVerbose(n, linkMonitor)
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, linkMonitor getters.LinkGetter) {
resp, err := n.getJSON(data, cpuPrefix, linkMonitor)
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, linkMonitor getters.LinkGetter) TraceNotifyVerbose {
ifname := linkMonitor.Name(n.Ifindex)
return TraceNotifyVerbose{
Type: "trace",
Mark: fmt.Sprintf("%#x", n.Hash),
Ifindex: ifname,
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,
}
}