Skip to content

Commit c7aa11b

Browse files
committed
netstack,tunnel: endpoint stats as struct
1 parent 29f324a commit c7aa11b

4 files changed

Lines changed: 53 additions & 23 deletions

File tree

intra/backend/netstat.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ type NICStat struct {
3131
type TUNStat struct {
3232
Open bool
3333
Up bool
34-
Fd int
35-
Sid int
3634
Mtu int32
35+
Sid int
3736
EpStats string
3837
PcapMode string
3938
}

intra/netstack/fdbased.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,43 @@ const errorOnInvalidFD = false
6060
const waitttl = wrapttl
6161

6262
type FdSwapper interface {
63-
// Cur returns the current FD.
64-
Cur() int
6563
// Swap closes existing FDs; uses new fd.
6664
Swap(fd int) error
6765
// Dispose closes all existing FDs.
6866
Dispose() error
69-
// Stat returns csv (fd, age, read, written, lastRead, lastWrite) stats.
70-
Stat() string
67+
// Stat returns EpStat (fd, age, read, written, lastRead, lastWrite).
68+
Stat() EpStat
69+
}
70+
71+
type EpStat struct {
72+
// Fd is the file descriptor of the endpoint.
73+
Fd int
74+
// Alive indicates whether the endpoint is alive.
75+
Alive bool
76+
// Age is the age of the endpoint.
77+
Age string
78+
// Read is the number of bytes read from the endpoint.
79+
Read string
80+
// Written is the number of bytes written to the endpoint.
81+
Written string
82+
// LastRead is the last time the endpoint was read from.
83+
LastRead string
84+
// LastWrite is the last time the endpoint was written to.
85+
LastWrite string
86+
}
87+
88+
func (s EpStat) String() string {
89+
if s.Fd == 0 {
90+
return "<nil>"
91+
}
92+
return fmt.Sprintf("Fd: %d,Alive: %t,Age: %s,R: %s,W: %s,LastRead: %s,LastWrite: %s",
93+
s.Fd,
94+
s.Alive,
95+
s.Age,
96+
s.Read,
97+
s.Written,
98+
s.LastRead,
99+
s.LastWrite)
71100
}
72101

73102
type SeamlessEndpoint interface {
@@ -243,14 +272,10 @@ func createInboundDispatcher(e *endpoint, f *fds) (linkDispatcher, error) {
243272
return d, nil
244273
}
245274

246-
func (e *endpoint) Cur() int {
247-
return e.fd()
248-
}
249-
250-
func (e *endpoint) Stat() string {
275+
func (e *endpoint) Stat() (zz EpStat) {
251276
fds := e.fds.Load()
252277
if fds == nil {
253-
return "<nil>"
278+
return
254279
}
255280

256281
t := time.Now()
@@ -260,13 +285,15 @@ func (e *endpoint) Stat() string {
260285

261286
age := t.Sub(time.UnixMilli(fds.since.Load()))
262287

263-
return fmt.Sprintf("Fd: %d, Age: %s, R: %s, W: %s, LastRead: %s, LastWrite%s",
264-
fds.tunFd, // f.tun() returns invalidfd if f.tunFd is closed
265-
core.FmtPeriod(age),
266-
core.FmtBytes(uint64(fds.read.Load())),
267-
core.FmtBytes(uint64(fds.written.Load())),
268-
core.FmtUnixMillisAsPeriod(fds.lastRead.Load()),
269-
core.FmtUnixMillisAsPeriod(fds.lastWrite.Load()))
288+
return EpStat{
289+
Fd: fds.tunFd, // f.tun() returns invalidfd if f.tunFd is closed
290+
Alive: !fds.closed.Load(),
291+
Age: core.FmtPeriod(age),
292+
Read: core.FmtBytes(uint64(fds.read.Load())),
293+
Written: core.FmtBytes(uint64(fds.written.Load())),
294+
LastRead: core.FmtUnixMillisAsPeriod(fds.lastRead.Load()),
295+
LastWrite: core.FmtUnixMillisAsPeriod(fds.lastWrite.Load()),
296+
}
270297
}
271298

272299
func (e *endpoint) Dispose() (err error) {

intra/tunnel.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func NewTunnel(fd, mtu int, fakedns string, dtr DefaultDNS, bdg Bridge) (t Tunne
144144

145145
const dualstack = settings.IP46
146146

147-
log.SetConsole(&clogAdapter{bdg})
147+
log.SetConsole(ctx, &clogAdapter{bdg})
148148
natpt := x64.NewNatPt()
149149
proxies := ipn.NewProxifier(ctx, dualstack, mtu, bdg, bdg)
150150
services := rnet.NewServices(ctx, proxies, bdg, bdg)
@@ -293,6 +293,11 @@ func (t *rtunnel) GetServices() (x.Services, error) {
293293
}
294294

295295
func (t *rtunnel) Stat() (*x.NetStat, error) {
296+
if settings.Debug {
297+
// if debugging, bypass the barrier
298+
return t.stat()
299+
}
300+
296301
v, err := bar.DoIt("stat", func() (*x.NetStat, error) {
297302
return t.stat()
298303
})

tunnel/tunnel.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func NewGTunnel(pctx context.Context, fd, mtu int, l3 string, hdl netstack.GConn
192192
}
193193
netstack.Route(stack, l3)
194194

195-
who := strconv.Itoa(ep.Cur())
195+
who := strconv.Itoa(ep.Stat().Fd)
196196

197197
var nic tcpip.NICID
198198
// Enabled() may temporarily return false when Up() is in progress.
@@ -302,11 +302,10 @@ func (t *gtunnel) Stat() (*x.NetStat, error) {
302302
if err == nil && st != nil {
303303
st.TUNSt.Open = !t.closed.Load()
304304
st.TUNSt.Up = t.ep.IsAttached()
305-
st.TUNSt.Fd = t.ep.Cur()
306305
st.TUNSt.Sid = t.sid.Load() // session id (tunnel fd)
307306
st.TUNSt.Mtu = int32(t.ep.MTU())
308307
st.TUNSt.PcapMode = t.pcapio.mode()
309-
st.TUNSt.EpStats = t.ep.Stat()
308+
st.TUNSt.EpStats = t.ep.Stat().String()
310309

311310
if t := t.hdl.TCP(); t != nil {
312311
st.RDNSIn.OpenConnsTCP = t.OpenConns()

0 commit comments

Comments
 (0)