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 18f0e1b commit d37e6c8
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 @@ -57,6 +57,11 @@ type ListenerSocketOptions struct {
// Available on Linux, macOS, FreeBSD, and Windows.
PathMTUDiscovery 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 setPMTUD(fd int, network string) error {
return nil
}

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

func setRecvPktinfo(fd int, network string) error {
switch network {
case "udp4":
Expand All @@ -72,5 +76,6 @@ func (lso ListenerSocketOptions) buildSetFns() setFuncSlice {
appendSetFwmarkFunc(lso.Fwmark).
appendSetTrafficClassFunc(lso.TrafficClass).
appendSetPMTUDFunc(lso.PathMTUDiscovery).
appendSetUDPGenericReceiveOffloadFunc(lso.UDPGenericReceiveOffload).
appendSetRecvPktinfoFunc(lso.ReceivePacketInfo)
}
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 d37e6c8

Please sign in to comment.