-
Notifications
You must be signed in to change notification settings - Fork 269
/
protocol_logs.go
477 lines (424 loc) · 12 KB
/
protocol_logs.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (c) 2024 Yunshan Networks
*
* 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 datatype
import (
"fmt"
"net"
"time"
"github.com/deepflowio/deepflow/server/libs/codec"
"github.com/deepflowio/deepflow/server/libs/datatype/pb"
"github.com/deepflowio/deepflow/server/libs/pool"
"github.com/deepflowio/deepflow/server/libs/utils"
)
type LogMessageType uint8
const (
MSG_T_REQUEST LogMessageType = iota
MSG_T_RESPONSE
MSG_T_SESSION
MSG_T_OTHER
MSG_T_MAX
)
func (t *LogMessageType) String() string {
formatted := ""
switch *t {
case MSG_T_SESSION:
formatted = "SESSION"
case MSG_T_REQUEST:
formatted = "REQUEST"
case MSG_T_RESPONSE:
formatted = "RESPONSE"
case MSG_T_OTHER:
formatted = "OTHER"
default:
formatted = "UNKNOWN"
}
return formatted
}
type LogMessageStatus uint8
const (
STATUS_OK LogMessageStatus = iota
STATUS_ERROR
STATUS_NOT_EXIST
STATUS_SERVER_ERROR
STATUS_CLIENT_ERROR
)
func (t LogMessageStatus) String() string {
switch t {
case STATUS_OK:
return "Success"
case STATUS_ERROR:
return "Error"
case STATUS_SERVER_ERROR:
return "Server Error"
case STATUS_CLIENT_ERROR:
return "Client Error"
default:
return "Unknown"
}
}
type AppProtoHead struct {
Proto L7Protocol
MsgType LogMessageType // HTTP,DNS: request/response
Status uint8 // 状态描述:0:正常,1:已废弃使用(先前用于表示异常),2:不存在,3:服务端异常,4:客户端异常
Code uint16 // HTTP状态码: 1xx-5xx, DNS状态码: 0-7
RRT time.Duration // HTTP,DNS时延: response-request
}
func (h *AppProtoHead) WriteToPB(p *pb.AppProtoHead) {
p.Proto = uint32(h.Proto)
p.MsgType = uint32(h.MsgType)
p.Rrt = uint64(h.RRT)
}
type AppProtoLogsBaseInfo struct {
StartTime time.Duration // 开始时间, packet的时间戳
EndTime time.Duration // 结束时间, 初始化时等于开始时间
FlowId uint64 // 对应flow的ID
TapPort TapPort
VtapId uint16
TapType uint16
IsIPv6 bool
TapSide uint8
AppProtoHead
/* L2 */
MacSrc uint64
MacDst uint64
/* L3 */
IPSrc IPv4Int
IPDst IPv4Int
/* L3 IPv6 */
IP6Src [net.IPv6len]byte
IP6Dst [net.IPv6len]byte
/* L3EpcID */
L3EpcIDSrc int32
L3EpcIDDst int32
/* L4 */
PortSrc uint16
PortDst uint16
/* First L7 TCP Seq */
ReqTcpSeq uint32
RespTcpSeq uint32
Protocol uint8
IsVIPInterfaceSrc bool
IsVIPInterfaceDst bool
}
func (i *AppProtoLogsBaseInfo) String() string {
formatted := ""
formatted += fmt.Sprintf("StartTime: %v ", i.StartTime)
formatted += fmt.Sprintf("EndTime: %v ", i.EndTime)
formatted += fmt.Sprintf("FlowId: %v ", i.FlowId)
formatted += fmt.Sprintf("VtapId: %v ", i.VtapId)
formatted += fmt.Sprintf("TapType: %v ", i.TapType)
formatted += fmt.Sprintf("TapPort: %s ", i.TapPort)
formatted += fmt.Sprintf("Proto: %s ", i.Proto.String(false))
formatted += fmt.Sprintf("MsgType: %s ", i.MsgType.String())
formatted += fmt.Sprintf("Code: %v ", i.Code)
formatted += fmt.Sprintf("Status: %v ", i.Status)
formatted += fmt.Sprintf("RRT: %v ", i.RRT)
formatted += fmt.Sprintf("TapSide: %d ", i.TapSide)
formatted += fmt.Sprintf("IsVIPInterfaceSrc: %v ", i.IsVIPInterfaceSrc)
formatted += fmt.Sprintf("IsVIPInterfaceDst: %v ", i.IsVIPInterfaceDst)
if i.MacSrc > 0 || i.MacDst > 0 {
formatted += fmt.Sprintf("MacSrc: %s ", utils.Uint64ToMac(i.MacSrc))
formatted += fmt.Sprintf("MacDst: %s ", utils.Uint64ToMac(i.MacDst))
}
if i.IsIPv6 {
formatted += fmt.Sprintf("IP6Src: %s ", net.IP(i.IP6Src[:]))
formatted += fmt.Sprintf("IP6Dst: %s ", net.IP(i.IP6Dst[:]))
} else {
formatted += fmt.Sprintf("IPSrc: %s ", utils.IpFromUint32(i.IPSrc))
formatted += fmt.Sprintf("IPDst: %s ", utils.IpFromUint32(i.IPDst))
}
formatted += fmt.Sprintf("Protocol: %v ", i.Protocol)
formatted += fmt.Sprintf("PortSrc: %v ", i.PortSrc)
formatted += fmt.Sprintf("PortDst: %v ", i.PortDst)
formatted += fmt.Sprintf("L3EpcIDSrc: %v ", i.L3EpcIDSrc)
formatted += fmt.Sprintf("L3EpcIDDst: %v ", i.L3EpcIDDst)
formatted += fmt.Sprintf("ReqTcpSeq: %v ", i.ReqTcpSeq)
formatted += fmt.Sprintf("RespTcpSeq: %v", i.RespTcpSeq)
return formatted
}
type AppProtoLogsData struct {
AppProtoLogsBaseInfo
Detail ProtoSpecialInfo
pool.ReferenceCount
}
var httpInfoPool = pool.NewLockFreePool(func() interface{} {
return new(HTTPInfo)
})
func AcquireHTTPInfo() *HTTPInfo {
return httpInfoPool.Get().(*HTTPInfo)
}
func ReleaseHTTPInfo(h *HTTPInfo) {
*h = HTTPInfo{}
httpInfoPool.Put(h)
}
var dnsInfoPool = pool.NewLockFreePool(func() interface{} {
return new(DNSInfo)
})
func AcquireDNSInfo() *DNSInfo {
return dnsInfoPool.Get().(*DNSInfo)
}
func ReleaseDNSInfo(d *DNSInfo) {
*d = DNSInfo{}
dnsInfoPool.Put(d)
}
var appProtoLogsDataPool = pool.NewLockFreePool(func() interface{} {
return new(AppProtoLogsData)
})
var zeroAppProtoLogsData = AppProtoLogsData{}
func AcquireAppProtoLogsData() *AppProtoLogsData {
d := appProtoLogsDataPool.Get().(*AppProtoLogsData)
d.Reset()
return d
}
func ReleaseAppProtoLogsData(d *AppProtoLogsData) {
if d.SubReferenceCount() {
return
}
switch d.Proto {
case L7_PROTOCOL_HTTP_2:
fallthrough
case L7_PROTOCOL_HTTP_1:
ReleaseHTTPInfo(d.Detail.(*HTTPInfo))
case L7_PROTOCOL_DNS:
ReleaseDNSInfo(d.Detail.(*DNSInfo))
case L7_PROTOCOL_MYSQL:
ReleaseMYSQLInfo(d.Detail.(*MysqlInfo))
case L7_PROTOCOL_REDIS:
ReleaseREDISInfo(d.Detail.(*RedisInfo))
case L7_PROTOCOL_DUBBO:
ReleaseDubboInfo(d.Detail.(*DubboInfo))
case L7_PROTOCOL_KAFKA:
ReleaseKafkaInfo(d.Detail.(*KafkaInfo))
case L7_PROTOCOL_MQTT:
ReleaseMqttInfo(d.Detail.(*MqttInfo))
}
*d = zeroAppProtoLogsData
appProtoLogsDataPool.Put(d)
}
func CloneAppProtoLogsData(d *AppProtoLogsData) *AppProtoLogsData {
newAppProtoLogsData := AcquireAppProtoLogsData()
*newAppProtoLogsData = *d
newAppProtoLogsData.Reset()
return newAppProtoLogsData
}
func (l *AppProtoLogsData) String() string {
return fmt.Sprintf("base info: %s, Detail info: %s",
l.AppProtoLogsBaseInfo.String(), l.Detail.String())
}
func (l *AppProtoLogsData) Release() {
ReleaseAppProtoLogsData(l)
}
func (l *AppProtoLogsBaseInfo) WriteToPB(p *pb.AppProtoLogsBaseInfo) {
p.StartTime = uint64(l.StartTime)
p.EndTime = uint64(l.EndTime)
p.FlowId = l.FlowId
p.TapPort = uint64(l.TapPort)
p.VtapId = uint32(l.VtapId)
p.TapType = uint32(l.TapType)
p.IsIpv6 = utils.Bool2UInt32(l.IsIPv6)
p.TapSide = uint32(l.TapSide)
if p.Head == nil {
p.Head = &pb.AppProtoHead{}
}
l.AppProtoHead.WriteToPB(p.Head)
p.MacSrc = l.MacSrc
p.MacDst = l.MacDst
p.IpSrc = l.IPSrc
p.IpDst = l.IPDst
p.Ip6Src = l.IP6Src[:]
p.Ip6Dst = l.IP6Dst[:]
p.L3EpcIdSrc = l.L3EpcIDSrc
p.L3EpcIdDst = l.L3EpcIDDst
p.PortSrc = uint32(l.PortSrc)
p.PortDst = uint32(l.PortDst)
p.Protocol = uint32(l.Protocol)
p.IsVipInterfaceSrc = utils.Bool2UInt32(l.IsVIPInterfaceSrc)
p.IsVipInterfaceDst = utils.Bool2UInt32(l.IsVIPInterfaceDst)
p.ReqTcpSeq = l.ReqTcpSeq
p.RespTcpSeq = l.RespTcpSeq
}
func (l *AppProtoLogsData) EncodePB(encoder *codec.SimpleEncoder, i interface{}) error {
p, ok := i.(*pb.AppProtoLogsData)
if !ok {
return fmt.Errorf("invalid interface type, should be *pb.AppProtoLogsData")
}
l.WriteToPB(p)
encoder.WritePB(p)
return nil
}
func (l *AppProtoLogsData) WriteToPB(p *pb.AppProtoLogsData) {
if p.Base == nil {
p.Base = &pb.AppProtoLogsBaseInfo{}
}
l.AppProtoLogsBaseInfo.WriteToPB(p.Base)
if l.AppProtoLogsBaseInfo.MsgType == MSG_T_RESPONSE ||
l.AppProtoLogsBaseInfo.MsgType == MSG_T_SESSION {
p.Resp = &pb.L7Response{
Status: uint32(l.AppProtoLogsBaseInfo.AppProtoHead.Status),
Code: int32(l.AppProtoLogsBaseInfo.AppProtoHead.Code),
}
}
switch l.Proto {
case L7_PROTOCOL_HTTP_1:
fallthrough
case L7_PROTOCOL_HTTP_2:
if http, ok := l.Detail.(*HTTPInfo); ok {
http.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_DNS:
if dns, ok := l.Detail.(*DNSInfo); ok {
dns.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_MYSQL:
if mysql, ok := l.Detail.(*MysqlInfo); ok {
mysql.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_REDIS:
if redis, ok := l.Detail.(*RedisInfo); ok {
redis.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_DUBBO:
if dubbo, ok := l.Detail.(*DubboInfo); ok {
dubbo.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_KAFKA:
if kafka, ok := l.Detail.(*KafkaInfo); ok {
kafka.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
case L7_PROTOCOL_MQTT:
if mqtt, ok := l.Detail.(*MqttInfo); ok {
mqtt.WriteToPB(p, l.AppProtoLogsBaseInfo.MsgType)
}
}
}
type ProtoSpecialInfo interface {
String() string
Merge(interface{})
}
// HTTPv2根据需要添加
type HTTPInfo struct {
StreamID uint32 // HTTPv2
Version string
TraceID string
SpanID string
Method string
Path string
Host string
ClientIP string
XRequestId string
ReqContentLength int64
RespContentLength int64
}
func (h *HTTPInfo) WriteToPB(p *pb.AppProtoLogsData, msgType LogMessageType) {
p.Version = h.Version
p.TraceInfo = &pb.TraceInfo{
TraceId: h.TraceID,
SpanId: h.SpanID,
}
p.ReqLen, p.RespLen = -1, -1
if msgType == MSG_T_REQUEST || msgType == MSG_T_SESSION {
p.Req = &pb.L7Request{
ReqType: h.Method,
Domain: h.Host,
Resource: h.Path,
}
p.ExtInfo = &pb.ExtendedInfo{
ClientIp: h.ClientIP,
RequestId: h.StreamID,
// FIXME trident not support XRequestId1, always fill value to XRequestId_0
XRequestId_0: h.XRequestId,
}
p.ReqLen = int32(h.ReqContentLength)
}
if msgType == MSG_T_RESPONSE || msgType == MSG_T_SESSION {
if h.XRequestId != "" {
if p.ExtInfo == nil {
p.ExtInfo = &pb.ExtendedInfo{}
}
// FIXME trident not support XRequestId1, always fill value to XRequestId_0
p.ExtInfo.XRequestId_0 = h.XRequestId
}
p.RespLen = int32(h.RespContentLength)
}
}
func (h *HTTPInfo) String() string {
return fmt.Sprintf("%#v", h)
}
func (h *HTTPInfo) Merge(r interface{}) {
if http, ok := r.(*HTTPInfo); ok {
h.RespContentLength = http.RespContentLength
if h.XRequestId == "" {
h.XRequestId = http.XRequestId
}
if h.TraceID == "" {
h.TraceID = http.TraceID
}
if h.SpanID == "" {
h.SpanID = http.SpanID
}
}
}
// | type | 查询类型 | 说明|
// | ---- | -------- | --- |
// | 1 | A |由域名获得IPv4地址|
// | 2 | NS |查询域名服务器|
// | 5 | CNAME |查询规范名称|
// | 6 | SOA |开始授权|
// | 11 | WKS |熟知服务|
// | 12 | PTR |把IP地址转换成域名|
// | 13 | HINFO |主机信息|
// | 15 | MX |邮件交换|
// | 28 | AAAA |由域名获得IPv6地址|
// | 252 | AXFR |传送整个区的请求|
// | 255 | ANY |对所有记录的请求|
type DNSInfo struct {
TransID uint16
QueryType uint16
QueryName string
// 根据查询类型的不同而不同,如:
// A: ipv4/ipv6地址
// NS: name server
// SOA: primary name server
Answers string
}
func (h *DNSInfo) WriteToPB(p *pb.AppProtoLogsData, msgType LogMessageType) {
if h.TransID != 0 {
p.ExtInfo = &pb.ExtendedInfo{
RequestId: uint32(h.TransID),
}
}
p.ReqLen, p.RespLen = -1, -1
if msgType == MSG_T_REQUEST || msgType == MSG_T_SESSION {
p.Req = &pb.L7Request{
ReqType: GetDNSQueryType(uint8(h.QueryType)),
Resource: h.QueryName,
}
}
if msgType == MSG_T_RESPONSE || msgType == MSG_T_SESSION {
p.Resp.Result = h.Answers
if p.Resp.Code == 0 {
p.Resp.Code = L7PROTOCOL_LOG_RESP_CODE_NONE
}
}
}
func (d *DNSInfo) String() string {
return fmt.Sprintf("%#v", d)
}
func (d *DNSInfo) Merge(r interface{}) {
if response, ok := r.(*DNSInfo); ok {
d.Answers = response.Answers
}
}