github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/util_linux.go (about)

     1  // +build linux
     2  
     3  package fastdns
     4  
     5  import (
     6  	"context"
     7  	"net"
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  func listen(network, address string) (*net.UDPConn, error) {
    13  	lc := &net.ListenConfig{
    14  		Control: func(network, address string, conn syscall.RawConn) error {
    15  			return conn.Control(func(fd uintptr) {
    16  				const SO_REUSEPORT = 15
    17  				_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, SO_REUSEPORT, 1)
    18  			})
    19  		},
    20  	}
    21  
    22  	conn, err := lc.ListenPacket(context.Background(), network, address)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return conn.(*net.UDPConn), nil
    28  }
    29  
    30  func taskset(cpu int) error {
    31  	const SYS_SCHED_SETAFFINITY = 203
    32  
    33  	mask := make([]byte, 128)
    34  	mask[cpu/8] = 1 << (cpu % 8)
    35  
    36  	_, _, e := syscall.RawSyscall(SYS_SCHED_SETAFFINITY, uintptr(0), uintptr(len(mask)), uintptr(unsafe.Pointer(&mask[0])))
    37  	if e == 0 {
    38  		return nil
    39  	}
    40  
    41  	return e
    42  }