Skip to content

Commit

Permalink
intra/tunnel: seperate out tun fd updates from mtu
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Nov 13, 2024
1 parent 9d01b2c commit b5f4246
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
17 changes: 9 additions & 8 deletions intra/netstack/fdbased.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ var _ FdSwapper = (*linkFdSwap)(nil)
const invalidfd int = -1

type FdSwapper interface {
// Swap closes existing FDs; uses new fd and mtu.
Swap(fd, mtu int) error
// Swap closes existing FDs; uses new fd.
Swap(fd int) error
// Dispose closes all existing FDs.
Dispose() error
}
Expand Down Expand Up @@ -212,7 +212,9 @@ func NewFdbasedInjectableEndpoint(opts *Options) (SeamlessEndpoint, error) {
return nil, fmt.Errorf("len(opts.FDs) = %d, expected 1", len(opts.FDs))
}

if err := e.Swap(opts.FDs[0], int(opts.MTU)); err != nil {
e.SetMTU(opts.MTU)

if err := e.Swap(opts.FDs[0]); err != nil {
return nil, err
}

Expand Down Expand Up @@ -252,17 +254,15 @@ func (e *endpoint) Dispose() (err error) {
}

// Implements Swapper.
func (e *endpoint) Swap(fd, mtu int) (err error) {
e.SetMTU(uint32(mtu))

func (e *endpoint) Swap(fd int) (err error) {
if err = unix.SetNonblock(fd, true); err != nil {
clos(fd)
return fmt.Errorf("unix.SetNonblock(%v) failed: %v", fd, err)
}

prevfd := e.fds.Swap(fd) // commence WritePackets() on fd

log.D("ns: swapping tun... fd: %d => %d, mtu: %d", prevfd, fd, mtu)
log.D("ns: swapping tun... fd: %d => %d", prevfd, fd)

e.Lock()
defer e.Unlock()
Expand All @@ -277,7 +277,8 @@ func (e *endpoint) Swap(fd, mtu int) (err error) {
if err == nil && hasDispatcher { // attached?
go e.dispatchLoop(e.inboundDispatcher)
} else {
log.W("ns: tun(%d => %d): Swap: no dispatcher? %t for new fd; err %v", prevfd, fd, !hasDispatcher, err)
log.W("ns: tun(%d => %d): Swap: no dispatcher? %t for new fd; err %v",
prevfd, fd, !hasDispatcher, err)
}
return
}
Expand Down
3 changes: 2 additions & 1 deletion intra/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ func (t *rtunnel) SetLinkAndRoutes(fd, mtu, engine int) error {
}
})
}()
return t.Tunnel.SetLink(fd, mtu) // route is always dual-stack
t.Tunnel.SetMTU(int32(mtu))
return t.Tunnel.SetLink(fd) // route is always dual-stack
}

func (t *rtunnel) internalCtx() context.Context {
Expand Down
20 changes: 14 additions & 6 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ type Tunnel interface {
Write(data []byte) (int, error)
// Close connections
CloseConns(activecsv string) (closedcsv string)
// Creates a new link using fd (tun device) and mtu.
SetLink(fd, mtu int) error
// Creates a new link using fd (tun device).
SetLink(fd int) error
// Sets the MTU.
SetMTU(mtu int32)
// Unsets existing link and closes the fd (tun device).
Unlink() error
// Creates the link and updates the routes
Expand Down Expand Up @@ -244,7 +246,8 @@ func (t *gtunnel) SetPcap(fp string) error {
func (t *gtunnel) SetLinkAndRoutes(fd, mtu, engine int) (err error) {
// route is always dual-stack (settings.IP46); never changed
log.I("tun: requested route (%s); unchanged", settings.L3(engine))
return t.SetLink(fd, mtu)
t.SetMTU(int32(mtu))
return t.SetLink(fd)
}

func (t *gtunnel) Unlink() error {
Expand All @@ -253,7 +256,7 @@ func (t *gtunnel) Unlink() error {
return t.ep.Dispose()
}

func (t *gtunnel) SetLink(fd, mtu int) error {
func (t *gtunnel) SetLink(fd int) error {
defer core.Recover(core.Exit11, "g.SetLink")

dupfd, err := dup(fd) // tunnel will own dupfd
Expand All @@ -262,12 +265,17 @@ func (t *gtunnel) SetLink(fd, mtu int) error {
return err
}

err = t.ep.Swap(dupfd, mtu) // swap fd and mtu
err = t.ep.Swap(dupfd) // swap fd and mtu

log.I("tun: new link; fd(%d), mtu(%d); err? %v", dupfd, mtu, err)
log.I("tun: new link, fd(%d); err? %v", dupfd, err)
return err
}

func (t *gtunnel) SetMTU(mtu int32) {
t.ep.SetMTU(uint32(mtu))
log.I("tun: new mtu; %d", mtu)
}

func (t *gtunnel) SetRoute(engine int) error {
defer core.Recover(core.Exit11, "g.SetRoute")

Expand Down

0 comments on commit b5f4246

Please sign in to comment.