@@ -10,13 +10,19 @@ import (
10
10
"net"
11
11
"strings"
12
12
"sync"
13
+
14
+ "github.com/celzero/firestack/intra/log"
13
15
)
14
16
15
17
type ConnTuple struct {
16
18
CID string // conn id
17
19
UID string // proc id
18
20
}
19
21
22
+ func (t ConnTuple ) String () string {
23
+ return t .CID + ":" + t .UID
24
+ }
25
+
20
26
type ConnMapper interface {
21
27
Clear () []string
22
28
Track (t ConnTuple , x ... net.Conn ) int
@@ -57,10 +63,11 @@ func (h *cm) Track(t ConnTuple, conns ...net.Conn) (n int) {
57
63
}
58
64
h .trackDstLocked (t , conns )
59
65
66
+ log .D ("connmap: track: %d conns for %s" , n , cid )
60
67
return
61
68
}
62
69
63
- func (h * cm ) trackDstLocked (t ConnTuple , conns []net.Conn ) {
70
+ func (h * cm ) trackDstLocked (t ConnTuple , conns []net.Conn ) ( n int ) {
64
71
for _ , c := range conns {
65
72
if c == nil {
66
73
continue
@@ -71,13 +78,18 @@ func (h *cm) trackDstLocked(t ConnTuple, conns []net.Conn) {
71
78
continue
72
79
}
73
80
dst := raddr .String ()
81
+ log .V ("connmap: track: %s -> %s" , t .CID , dst )
74
82
if tups , ok := h .dsttracker [dst ]; ok {
75
83
// TODO: do not add dup tuples (cid)
76
84
h .dsttracker [dst ] = append (tups , t )
85
+ n += len (tups ) + 1
77
86
} else {
78
87
h .dsttracker [dst ] = []ConnTuple {t }
88
+ n += 1
79
89
}
80
90
}
91
+ log .D ("connmap: track: %d dst for %s" , n , t .CID )
92
+ return
81
93
}
82
94
83
95
func (h * cm ) Untrack (cid string ) (n int ) {
@@ -86,34 +98,42 @@ func (h *cm) Untrack(cid string) (n int) {
86
98
87
99
for _ , c := range h .conntracker [cid ] {
88
100
if c != nil {
89
- n ++
90
101
h .untrackDstLocked (cid , c )
91
102
go c .Close ()
103
+ n += 1
92
104
}
93
105
}
94
106
delete (h .conntracker , cid )
107
+ log .D ("connmap: untrack: %d conns for %s" , n , cid )
95
108
return
96
109
}
97
110
98
- func (h * cm ) untrackDstLocked (cid string , c net.Conn ) {
111
+ func (h * cm ) untrackDstLocked (cid string , c net.Conn ) ( ok bool ) {
99
112
raddr := c .RemoteAddr ()
100
- if raddr == nil {
113
+ if raddr == nil { // should not happen?
114
+ log .W ("connmap: untrack: no remote addr for %s" , cid )
101
115
return
102
116
}
103
117
dst := raddr .String ()
104
118
if tups , ok := h .dsttracker [dst ]; ok {
105
119
for i , t := range tups {
106
120
if t .CID == cid {
121
+ log .D ("connmap: untrack: dst %s -> %s" , cid , dst )
107
122
// ids[i+1:] does not panic if i+1 is out of range
108
123
// go.dev/play/p/troeQ5djf9h
109
124
h .dsttracker [dst ] = append (tups [:i ], tups [i + 1 :]... )
125
+ ok = true
110
126
break
111
127
}
112
128
}
113
- if len (h . dsttracker [ dst ] ) == 0 {
129
+ if len (tups ) == 0 {
114
130
delete (h .dsttracker , dst )
115
131
}
132
+ log .D ("connmap: untrack: %d dst for %s; ok? %t" , len (tups ), cid , ok )
133
+ } else {
134
+ log .D ("connmap: untrack: no dst for %s" , cid )
116
135
}
136
+ return
117
137
}
118
138
119
139
func (h * cm ) UntrackBatch (cids []string ) (out []string ) {
@@ -131,6 +151,7 @@ func (h *cm) UntrackBatch(cids []string) (out []string) {
131
151
delete (h .conntracker , id )
132
152
out = append (out , id )
133
153
}
154
+ log .D ("connmap: untrack: batch %d conns" , len (out ))
134
155
return
135
156
}
136
157
@@ -146,23 +167,23 @@ func (h *cm) Get(cid string) (conns []net.Conn) {
146
167
147
168
func (h * cm ) Find (dst string ) (tups []ConnTuple ) {
148
169
if len (dst ) == 0 {
170
+ log .D ("connmap: find: empty dst" )
149
171
return
150
172
}
151
173
152
174
h .RLock ()
153
175
defer h .RUnlock ()
154
-
155
176
// TODO: handle unconnected udp sockets
156
- if tups , ok := h .dsttracker [dst ]; ok {
157
- return tups
158
- }
177
+ tups = h .dsttracker [dst ]
178
+ log .V ("connmap: find: %d tuples for %s" , len (tups ), dst )
159
179
return
160
180
}
161
181
162
182
func (h * cm ) FindAll (csvips , port string ) (out []ConnTuple ) {
163
183
out = make ([]ConnTuple , 0 )
164
184
165
185
if len (csvips ) == 0 {
186
+ log .D ("connmap: findAll: empty csvips" )
166
187
return
167
188
}
168
189
@@ -176,6 +197,7 @@ func (h *cm) FindAll(csvips, port string) (out []ConnTuple) {
176
197
out = append (out , tups ... )
177
198
}
178
199
}
200
+ log .V ("connmap: findAll: %d tuples for %s" , len (out ), csvips )
179
201
return
180
202
}
181
203
@@ -194,5 +216,6 @@ func (h *cm) Clear() (cids []string) {
194
216
}
195
217
clear (h .conntracker )
196
218
clear (h .dsttracker )
219
+ log .D ("connmap: clear: %d conns" , len (cids ))
197
220
return
198
221
}
0 commit comments