Skip to content

Commit

Permalink
🫛 conn: add UDP GRO option
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Sep 26, 2024
1 parent aaf9f82 commit 5dab961
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ type ListenerSocketOptions struct {
// Available on platforms supported by Go std's MPTCP implementation.
MultipathTCP bool

// UDPGenericReceiveOffload enables UDP Generic Receive Offload (GRO) on the listener.
//
// Available on Linux and Windows.
UDPGenericReceiveOffload bool

// ReceivePacketInfo enables the reception of packet information control messages on the listener.
//
// Available on Linux, macOS, and Windows.
Expand Down
5 changes: 5 additions & 0 deletions conn/conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func setTCPUserTimeout(fd, msecs int) error {
return nil
}

func setUDPGenericReceiveOffload(fd int) {
_ = unix.SetsockoptInt(fd, unix.IPPROTO_UDP, unix.UDP_GRO, 1)
}

func setTransparent(fd int, network string) error {
switch network {
case "tcp4", "udp4":
Expand Down Expand Up @@ -162,6 +166,7 @@ func (lso ListenerSocketOptions) buildSetFns() setFuncSlice {
appendSetReusePortFunc(lso.ReusePort).
appendSetTransparentFunc(lso.Transparent).
appendSetPMTUDFunc(lso.PathMTUDiscovery).
appendSetUDPGenericReceiveOffloadFunc(lso.UDPGenericReceiveOffload).
appendSetRecvPktinfoFunc(lso.ReceivePacketInfo).
appendSetRecvOrigDstAddrFunc(lso.ReceiveOriginalDestAddr)
}
Expand Down
13 changes: 13 additions & 0 deletions conn/conn_udpgro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build linux || windows

package conn

func (fns setFuncSlice) appendSetUDPGenericReceiveOffloadFunc(gro bool) setFuncSlice {
if gro {
return append(fns, func(fd int, _ string) error {
setUDPGenericReceiveOffload(fd)
return nil
})
}
return fns
}
6 changes: 6 additions & 0 deletions conn/conn_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func setPMTUD(fd int, network string) error {
return nil
}

func setUDPGenericReceiveOffload(fd int) {
// Both quinn and msquic set this to 65535.
_ = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_UDP, windows.UDP_RECV_MAX_COALESCED_SIZE, 65535)
}

func setRecvPktinfo(fd int, network string) error {
// Set IP_PKTINFO for both v4 and v6.
if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_PKTINFO, 1); err != nil {
Expand All @@ -63,6 +68,7 @@ func setRecvPktinfo(fd int, network string) error {
func (lso ListenerSocketOptions) buildSetFns() setFuncSlice {
return setFuncSlice{}.
appendSetPMTUDFunc(lso.PathMTUDiscovery).
appendSetUDPGenericReceiveOffloadFunc(lso.UDPGenericReceiveOffload).
appendSetRecvPktinfoFunc(lso.ReceivePacketInfo)
}

Expand Down

0 comments on commit 5dab961

Please sign in to comment.