Skip to content

optimize(mtu): Set lowest MTU among bound interfaces to dae0/dae0peer #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions common/consts/net.go

This file was deleted.

3 changes: 1 addition & 2 deletions common/netutils/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sync"
"time"

"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/outbound/netproxy"
"github.com/daeuniverse/outbound/pkg/fastrand"
"github.com/daeuniverse/outbound/pool"
Expand Down Expand Up @@ -255,7 +254,7 @@ func resolve(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host st
}()
}
go func() {
buf := pool.GetFullCap(consts.EthernetMtu)
buf := pool.GetFullCap(GetEthernetMtu())
defer buf.Put()
if magicNetwork.Network == "tcp" {
// Read DNS response length
Expand Down
27 changes: 27 additions & 0 deletions common/netutils/mtu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2025, daeuniverse Organization <dae@v2raya.org>
*/

package netutils

import "sync"

var (
ethernetMtu = 9000
mtuMux sync.RWMutex
)

func UpdateEthernetMtu(mtu int) {
mtuMux.Lock()
defer mtuMux.Unlock()
if mtu < ethernetMtu {
ethernetMtu = mtu
}
}

func GetEthernetMtu() int {
mtuMux.RLock()
defer mtuMux.RUnlock()
return ethernetMtu
}
11 changes: 10 additions & 1 deletion control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
dnsmessage "github.com/miekg/dns"
"github.com/mohae/deepcopy"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -142,6 +143,14 @@ func NewControlPlane(
return nil, err
}

for _, ifname := range append(global.LanInterface, global.WanInterface...) {
link, err := netlink.LinkByName(ifname)
if err != nil {
return nil, err
}
netutils.UpdateEthernetMtu(link.Attrs().MTU)
}

if err = GetDaeNetns().Setup(); err != nil {
return nil, fmt.Errorf("failed to setup dae netns: %w", err)
}
Expand Down Expand Up @@ -789,7 +798,7 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}
}()
go func() {
buf := pool.GetFullCap(consts.EthernetMtu)
buf := pool.GetFullCap(netutils.GetEthernetMtu())
var oob [120]byte // Size for original dest
defer buf.Put()
for {
Expand Down
5 changes: 3 additions & 2 deletions control/dns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2025, daeuniverse Organization <dae@v2raya.org>
*/
*/

package control

Expand All @@ -19,6 +19,7 @@ import (

"github.com/daeuniverse/dae/common"
"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/dae/common/netutils"
"github.com/daeuniverse/dae/component/dns"
"github.com/daeuniverse/outbound/netproxy"
"github.com/daeuniverse/outbound/pool"
Expand Down Expand Up @@ -346,7 +347,7 @@ func (d *DoUDP) ForwardDNS(ctx context.Context, data []byte) (*dnsmessage.Msg, e
}()

// We can block here because we are in a coroutine.
respBuf := pool.GetFullCap(consts.EthernetMtu)
respBuf := pool.GetFullCap(netutils.GetEthernetMtu())
defer pool.Put(respBuf)
// Wait for response.
n, err := conn.Read(respBuf)
Expand Down
10 changes: 9 additions & 1 deletion control/netns_utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2025, daeuniverse Organization <dae@v2raya.org>
*/
*/

package control

Expand All @@ -15,6 +15,7 @@ import (
"sync/atomic"

"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/dae/common/netutils"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
Expand Down Expand Up @@ -242,6 +243,10 @@ func (ns *DaeNetns) setupVeth() (err error) {
if err = netlink.LinkSetUp(ns.dae0); err != nil {
return fmt.Errorf("failed to set link dae0 up: %v", err)
}

if err = netlink.LinkSetMTU(ns.dae0, netutils.GetEthernetMtu()); err != nil {
return fmt.Errorf("failed to set mtu %d for dae0: %v", netutils.GetEthernetMtu(), err)
}
return
}

Expand Down Expand Up @@ -269,6 +274,9 @@ func (ns *DaeNetns) setupNetns() (err error) {
if err = netlink.LinkSetUp(ns.dae0peer); err != nil {
return fmt.Errorf("failed to set link dae0peer up: %v", err)
}
if err = netlink.LinkSetMTU(ns.dae0peer, netutils.GetEthernetMtu()); err != nil {
return fmt.Errorf("failed to set mtu %d for dae0peer: %v", netutils.GetEthernetMtu(), err)
}
// re-fetch dae0peer to make sure we have the latest mac address
if ns.dae0peer, err = netlink.LinkByName(NsVethName); err != nil {
return fmt.Errorf("failed to get link dae0peer: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion control/udp_endpoint_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/dae/common/netutils"
"github.com/daeuniverse/dae/component/outbound"
"github.com/daeuniverse/dae/component/outbound/dialer"
"github.com/daeuniverse/outbound/netproxy"
Expand All @@ -38,7 +39,7 @@ type UdpEndpoint struct {
}

func (ue *UdpEndpoint) start() {
buf := pool.GetFullCap(consts.EthernetMtu)
buf := pool.GetFullCap(netutils.GetEthernetMtu())
defer pool.Put(buf)
for {
n, from, err := ue.conn.ReadFrom(buf[:])
Expand Down
Loading