Skip to content

Commit 2b14127

Browse files
authored
Combine connect and close connection collector in access log module (#162)
1 parent 3d92338 commit 2b14127

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed

pkg/accesslog/collector/close.go

-56
This file was deleted.

pkg/accesslog/collector/collector.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ func Collectors() []Collector {
3434
return []Collector{
3535
l24CollectorsInstance,
3636
transferCollectInstance,
37-
closeCollectorInstance,
38-
connectCollectInstance,
37+
connectionCollectInstance,
3938
tlsCollectInstance,
4039
processCollectInstance,
4140
zTunnelCollectInstance,

pkg/accesslog/collector/connect.go pkg/accesslog/collector/connection.go

+42-28
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ import (
4444
"golang.org/x/sys/unix"
4545
)
4646

47-
var connectLogger = logger.GetLogger("access_log", "collector", "connect")
47+
var connectionLogger = logger.GetLogger("access_log", "collector", "connection")
4848

49-
var connectCollectInstance = NewConnectCollector()
49+
var connectionCollectInstance = NewConnectionCollector()
5050

5151
type ConnectCollector struct {
5252
eventQueue *btf.EventQueue
5353
}
5454

55-
func NewConnectCollector() *ConnectCollector {
55+
func NewConnectionCollector() *ConnectCollector {
5656
return &ConnectCollector{}
5757
}
5858

@@ -72,7 +72,7 @@ func (c *ConnectCollector) Start(_ *module.Manager, ctx *common.AccessLogContext
7272
}
7373
track, err := ip.NewConnTrack()
7474
if err != nil {
75-
connectLogger.Warnf("cannot create the connection tracker, %v", err)
75+
connectionLogger.Warnf("cannot create the connection tracker, %v", err)
7676
}
7777
c.eventQueue = btf.NewEventQueue(ctx.Config.ConnectionAnalyze.Parallels, ctx.Config.ConnectionAnalyze.QueueSize, func(num int) btf.PartitionContext {
7878
return newConnectionPartitionContext(ctx, track)
@@ -82,6 +82,11 @@ func (c *ConnectCollector) Start(_ *module.Manager, ctx *common.AccessLogContext
8282
}, func(data interface{}) string {
8383
return fmt.Sprintf("%d", data.(*events.SocketConnectEvent).ConID)
8484
})
85+
c.eventQueue.RegisterReceiver(ctx.BPF.SocketCloseEventQueue, int(perCPUBufferSize), func() interface{} {
86+
return &events.SocketCloseEvent{}
87+
}, func(data interface{}) string {
88+
return fmt.Sprintf("%d", data.(*events.SocketCloseEvent).ConnectionID)
89+
})
8590
c.eventQueue.Start(ctx.RuntimeContext, ctx.BPF.Linker)
8691

8792
ctx.BPF.AddTracePoint("syscalls", "sys_enter_connect", ctx.BPF.TracepointEnterConnect)
@@ -90,6 +95,8 @@ func (c *ConnectCollector) Start(_ *module.Manager, ctx *common.AccessLogContext
9095
ctx.BPF.AddTracePoint("syscalls", "sys_exit_accept", ctx.BPF.TracepointExitAccept)
9196
ctx.BPF.AddTracePoint("syscalls", "sys_enter_accept4", ctx.BPF.TracepointEnterAccept)
9297
ctx.BPF.AddTracePoint("syscalls", "sys_exit_accept4", ctx.BPF.TracepointExitAccept)
98+
ctx.BPF.AddTracePoint("syscalls", "sys_enter_close", ctx.BPF.TracepointEnterClose)
99+
ctx.BPF.AddTracePoint("syscalls", "sys_exit_close", ctx.BPF.TracepointExitClose)
93100

94101
ctx.BPF.AddLink(link.Kprobe, map[string]*ebpf.Program{
95102
"tcp_connect": ctx.BPF.TcpConnect,
@@ -133,21 +140,28 @@ func (c *ConnectionPartitionContext) Start(ctx context.Context) {
133140
}
134141

135142
func (c *ConnectionPartitionContext) Consume(data interface{}) {
136-
event := data.(*events.SocketConnectEvent)
137-
connectLogger.Debugf("receive connect event, connection ID: %d, randomID: %d, "+
138-
"pid: %d, fd: %d, role: %s: func: %s, family: %d, success: %d, conntrack exist: %t",
139-
event.ConID, event.RandomID, event.PID, event.SocketFD, enums.ConnectionRole(event.Role), enums.SocketFunctionName(event.FuncName),
140-
event.SocketFamily, event.ConnectSuccess, event.ConnTrackUpstreamPort != 0)
141-
socketPair := c.buildSocketFromConnectEvent(event)
142-
if socketPair == nil {
143-
connectLogger.Debugf("cannot found the socket paire from connect event, connection ID: %d, randomID: %d",
144-
event.ConID, event.RandomID)
145-
return
143+
switch event := data.(type) {
144+
case *events.SocketConnectEvent:
145+
connectionLogger.Debugf("receive connect event, connection ID: %d, randomID: %d, "+
146+
"pid: %d, fd: %d, role: %s: func: %s, family: %d, success: %d, conntrack exist: %t",
147+
event.ConID, event.RandomID, event.PID, event.SocketFD, enums.ConnectionRole(event.Role), enums.SocketFunctionName(event.FuncName),
148+
event.SocketFamily, event.ConnectSuccess, event.ConnTrackUpstreamPort != 0)
149+
socketPair := c.buildSocketFromConnectEvent(event)
150+
if socketPair == nil {
151+
connectionLogger.Debugf("cannot found the socket paire from connect event, connection ID: %d, randomID: %d",
152+
event.ConID, event.RandomID)
153+
return
154+
}
155+
connectionLogger.Debugf("build socket pair success, connection ID: %d, randomID: %d, role: %s, local: %s:%d, remote: %s:%d",
156+
event.ConID, event.RandomID, socketPair.Role, socketPair.SrcIP, socketPair.SrcPort, socketPair.DestIP, socketPair.DestPort)
157+
c.context.ConnectionMgr.OnConnectEvent(event, socketPair)
158+
forwarder.SendConnectEvent(c.context, event, socketPair)
159+
case *events.SocketCloseEvent:
160+
connectionLogger.Debugf("receive close event, connection ID: %d, randomID: %d, pid: %d, fd: %d",
161+
event.ConnectionID, event.RandomID, event.PID, event.SocketFD)
162+
wapperedEvent := c.context.ConnectionMgr.OnConnectionClose(event)
163+
forwarder.SendCloseEvent(c.context, wapperedEvent)
146164
}
147-
connectLogger.Debugf("build socket pair success, connection ID: %d, randomID: %d, role: %s, local: %s:%d, remote: %s:%d",
148-
event.ConID, event.RandomID, socketPair.Role, socketPair.SrcIP, socketPair.SrcPort, socketPair.DestIP, socketPair.DestPort)
149-
c.context.ConnectionMgr.OnConnectEvent(event, socketPair)
150-
forwarder.SendConnectEvent(c.context, event, socketPair)
151165
}
152166

153167
func (c *ConnectionPartitionContext) fixSocketFamilyIfNeed(event *events.SocketConnectEvent, result *ip.SocketPair) {
@@ -163,7 +177,7 @@ func (c *ConnectionPartitionContext) fixSocketFamilyIfNeed(event *events.SocketC
163177
}
164178

165179
if result.Family != actual {
166-
connectLogger.Debugf("fix the socket family from %d to %d, connection ID: %d, randomID: %d",
180+
connectionLogger.Debugf("fix the socket family from %d to %d, connection ID: %d, randomID: %d",
167181
result.Family, actual, event.ConID, event.RandomID)
168182
result.Family = actual
169183
}
@@ -177,24 +191,24 @@ func (c *ConnectionPartitionContext) buildSocketFromConnectEvent(event *events.S
177191
}
178192
socketPair := c.buildSocketPair(event)
179193
if socketPair != nil && socketPair.IsValid() {
180-
connectLogger.Debugf("found the connection from the connect event is valid, connection ID: %d, randomID: %d",
194+
connectionLogger.Debugf("found the connection from the connect event is valid, connection ID: %d, randomID: %d",
181195
event.ConID, event.RandomID)
182196
return socketPair
183197
}
184198
// if only the local port not success, maybe the upstream port is not open, so it could be continued
185199
if c.isOnlyLocalPortEmpty(socketPair) {
186200
event.ConnectSuccess = 0
187-
connectLogger.Debugf("the connection from the connect event is only the local port is empty, connection ID: %d, randomID: %d",
201+
connectionLogger.Debugf("the connection from the connect event is only the local port is empty, connection ID: %d, randomID: %d",
188202
event.ConID, event.RandomID)
189203
return socketPair
190204
}
191205

192206
pair, err := ip.ParseSocket(event.PID, event.SocketFD)
193207
if err != nil {
194-
connectLogger.Debugf("cannot found the socket, pid: %d, socket FD: %d", event.PID, event.SocketFD)
208+
connectionLogger.Debugf("cannot found the socket, pid: %d, socket FD: %d", event.PID, event.SocketFD)
195209
return nil
196210
}
197-
connectLogger.Debugf("found the connection from the socket, connection ID: %d, randomID: %d",
211+
connectionLogger.Debugf("found the connection from the socket, connection ID: %d, randomID: %d",
198212
event.ConID, event.RandomID)
199213
pair.Role = enums.ConnectionRole(event.Role)
200214
c.fixSocketFamilyIfNeed(event, pair)
@@ -229,8 +243,8 @@ func (c *ConnectionPartitionContext) buildSocketPair(event *events.SocketConnect
229243
result.DestIP = ip.ParseIPV4(uint32(event.ConnTrackUpstreamIPl))
230244
result.DestPort = uint16(event.ConnTrackUpstreamPort)
231245

232-
if connectLogger.Enable(logrus.DebugLevel) {
233-
connectLogger.Debugf("found the connection from the conntrack, connection ID: %d, randomID: %d, original: %s:%d, conntrack: %s:%d",
246+
if connectionLogger.Enable(logrus.DebugLevel) {
247+
connectionLogger.Debugf("found the connection from the conntrack, connection ID: %d, randomID: %d, original: %s:%d, conntrack: %s:%d",
234248
event.ConID, event.RandomID, ip.ParseIPV4(event.RemoteAddrV4), uint16(event.RemoteAddrPort), result.DestIP, result.DestPort)
235249
}
236250
} else {
@@ -255,8 +269,8 @@ func (c *ConnectionPartitionContext) buildSocketPair(event *events.SocketConnect
255269
result.DestIP = ip.ParseIPV4(uint32(event.ConnTrackUpstreamIPl))
256270
}
257271
result.DestPort = uint16(event.ConnTrackUpstreamPort)
258-
if connectLogger.Enable(logrus.DebugLevel) {
259-
connectLogger.Debugf("found the connection from the conntrack, connection ID: %d, randomID: %d, original: %s:%d, conntrack: %s:%d",
272+
if connectionLogger.Enable(logrus.DebugLevel) {
273+
connectionLogger.Debugf("found the connection from the conntrack, connection ID: %d, randomID: %d, original: %s:%d, conntrack: %s:%d",
260274
event.ConID, event.RandomID, ip.ParseIPV6(event.RemoteAddrV6), uint16(event.RemoteAddrPort), result.DestIP, result.DestPort)
261275
}
262276
} else {
@@ -282,7 +296,7 @@ func (c *ConnectionPartitionContext) tryToUpdateSocketFromConntrack(event *event
282296
originalIP := socket.DestIP
283297
originalPort := socket.DestPort
284298
if c.connTracker.UpdateRealPeerAddress(socket) {
285-
connectLogger.Debugf("update the socket address from conntrack success, "+
299+
connectionLogger.Debugf("update the socket address from conntrack success, "+
286300
"connection ID: %d, randomID: %d, original remote: %s:%d, new remote: %s:%d",
287301
event.ConID, event.RandomID, originalIP, originalPort, socket.DestIP, socket.DestPort)
288302
}

0 commit comments

Comments
 (0)