github.com/sagernet/wireguard-go@v0.0.0-20231215174105-89dec3b2f3e8/conn/features_linux.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package conn
     7  
     8  import (
     9  	"net"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func supportsUDPOffload(conn *net.UDPConn) (txOffload, rxOffload bool) {
    15  	rc, err := conn.SyscallConn()
    16  	if err != nil {
    17  		return
    18  	}
    19  	err = rc.Control(func(fd uintptr) {
    20  		_, errSyscall := unix.GetsockoptInt(int(fd), unix.IPPROTO_UDP, unix.UDP_SEGMENT)
    21  		txOffload = errSyscall == nil
    22  		opt, errSyscall := unix.GetsockoptInt(int(fd), unix.IPPROTO_UDP, unix.UDP_GRO)
    23  		rxOffload = errSyscall == nil && opt == 1
    24  	})
    25  	if err != nil {
    26  		return false, false
    27  	}
    28  	return txOffload, rxOffload
    29  }