@@ -33,6 +33,7 @@ import (
3333 "fmt"
3434 "sync/atomic"
3535 "syscall"
36+ "time"
3637
3738 "github.com/celzero/firestack/intra/core"
3839 "github.com/celzero/firestack/intra/log"
@@ -195,7 +196,7 @@ func NewFdbasedInjectableEndpoint(opts *Options) (SeamlessEndpoint, error) {
195196
196197 e := & endpoint {
197198 mtu : atomic.Uint32 {},
198- fds : core.NewVolatile [ int ] (invalidfd ),
199+ fds : core .NewVolatile (invalidfd ),
199200 caps : caps ,
200201 addr : opts .Address ,
201202 hdrSize : hdrSize ,
@@ -263,12 +264,12 @@ func (e *endpoint) Dispose() (err error) {
263264func (e * endpoint ) Swap (fd int ) (err error ) {
264265 if err = unix .SetNonblock (fd , true ); err != nil {
265266 clos (fd )
266- return fmt .Errorf ("unix.SetNonblock(%v ) failed: %v" , fd , err )
267+ return fmt .Errorf ("ns: tun: set non blocking(%d ) failed: %v" , fd , err )
267268 }
268269
269270 prevfd := e .fds .Swap (fd ) // commence WritePackets() on fd
270271
271- log .D ("ns: swapping tun... fd: %d => %d" , prevfd , fd )
272+ log .D ("ns: swap: tun fd %d => %d" , prevfd , fd )
272273
273274 e .Lock ()
274275 defer e .Unlock ()
@@ -281,9 +282,11 @@ func (e *endpoint) Swap(fd int) (err error) {
281282
282283 hasDispatcher := e .dispatcher != nil
283284 if err == nil && hasDispatcher { // attached?
285+ log .I ("ns: tun(%d => %d): swap: restart looper %t for new fd" ,
286+ prevfd , fd , hasDispatcher )
284287 go e .dispatchLoop (e .inboundDispatcher )
285288 } else {
286- log .W ("ns: tun(%d => %d): Swap : no dispatcher? %t for new fd; err %v" ,
289+ log .W ("ns: tun(%d => %d): swap : no dispatcher? %t for new fd; err %v" ,
287290 prevfd , fd , ! hasDispatcher , err )
288291 }
289292 return
@@ -313,17 +316,17 @@ func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) {
313316 if dispatcher == nil && e .dispatcher != nil {
314317 log .I ("ns: tun(%d): attach: detach dispatcher (and inbound? %t)" , fd , pipe )
315318 if rx != nil {
316- go rx .stop () // avoid mutex
319+ go rx .stop () // avoid mutex; closes fd
317320 e .Wait () // on all inboundDispatcher w/ mutex locked?
318321 }
319322 e .dispatcher = nil
320- e .inboundDispatcher = nil
321- e .fds = nil
323+ e .inboundDispatcher = nil // rx
324+ e .fds . Store ( invalidfd )
322325 log .I ("ns: tun(%d): attach: done detaching dispatcher" , fd )
323326 return
324327 }
325328 if dispatcher != nil && e .dispatcher == nil {
326- log .I ("ns: tun(%d): attach: attach new dispatcher" , fd )
329+ log .I ("ns: tun(%d): attach: new dispatcher & looper " , fd )
327330 e .dispatcher = dispatcher
328331 go e .dispatchLoop (rx )
329332 return
@@ -484,13 +487,17 @@ func (e *endpoint) dispatchLoop(inbound linkDispatcher) tcpip.Error {
484487 return & tcpip.ErrUnknownDevice {}
485488 }
486489
490+ start := time .Now ()
487491 log .I ("ns: tun(%d): dispatchLoop: start" , fd )
488492 for {
489493 cont , err := inbound .dispatch ()
490- if err != nil || ! cont {
491- log .W ("ns: tun(%d): dispatchLoop: exit; err %v" , fd , err )
494+ if ! cont {
495+ elapsed := time .Since (start )
496+ log .W ("ns: tun(%d): dispatchLoop: exit; dur: %s; err? %v" , fd , elapsed , err )
492497 return err
493- }
498+ } else if err != nil {
499+ log .W ("ns: tun(%d): dispatchLoop: continue on err: %v" , fd , err )
500+ } // else: continue dispatching
494501 }
495502}
496503
0 commit comments