github.com/iceber/iouring-go@v0.0.0-20230403020409-002cfd2e2a90/syscall/io_uring_setup.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package iouring_syscall
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  const (
    13  	IORING_SETUP_IOPOLL uint32 = 1 << iota
    14  	IORING_SETUP_SQPOLL
    15  	IORING_SETUP_SQ_AFF
    16  	IORING_SETUP_CQSIZE
    17  	IORING_SETUP_CLAMP
    18  	IORING_SETUP_ATTACH_WQ
    19  	IORING_SETUP_R_DISABLED
    20  	IORING_SETUP_SUBMIT_ALL
    21  
    22  	IORING_SETUP_COOP_TASKRUN
    23  
    24  	IORING_SETUP_TASKRUN_FLAG
    25  	IORING_SETUP_SQE128
    26  	IORING_SETUP_CQE32
    27  )
    28  
    29  // io_uring features supported by current kernel version
    30  const (
    31  	IORING_FEAT_SINGLE_MMAP uint32 = 1 << iota
    32  	IORING_FEAT_NODROP
    33  	IORING_FEAT_SUBMIT_STABLE
    34  	IORING_FEAT_RW_CUR_POS
    35  	IORING_FEAT_CUR_PERSONALITY
    36  	IORING_FEAT_FAST_POLL
    37  	IORING_FEAT_POLL_32BITS
    38  	IORING_FEAT_SQPOLL_NONFIXED
    39  )
    40  
    41  // IOURingParams the flags, sq_thread_cpu, sq_thread_idle and WQFd fields are used to configure the io_uring instance
    42  type IOURingParams struct {
    43  	SQEntries    uint32 // specifies the number of submission queue entries allocated
    44  	CQEntries    uint32 // when IORING_SETUP_CQSIZE flag is specified
    45  	Flags        uint32 // a bit mast of 0 or more of the IORING_SETUP_*
    46  	SQThreadCPU  uint32 // when IORING_SETUP_SQPOLL and IORING_SETUP_SQ_AFF flags are specified
    47  	SQThreadIdle uint32 // when IORING_SETUP_SQPOLL flag is specified
    48  	Features     uint32
    49  	WQFd         uint32 // when IORING_SETUP_ATTACH_WQ flag is specified
    50  	Resv         [3]uint32
    51  
    52  	SQOffset SubmissionQueueRingOffset
    53  	CQOffset CompletionQueueRingOffset
    54  }
    55  
    56  // SubmissionQueueRingOffset describes the offsets of various ring buffer fields
    57  type SubmissionQueueRingOffset struct {
    58  	Head        uint32
    59  	Tail        uint32
    60  	RingMask    uint32
    61  	RingEntries uint32
    62  	Flags       uint32
    63  	Dropped     uint32
    64  	Array       uint32
    65  	Resv1       uint32
    66  	Resv2       uint64
    67  }
    68  
    69  // CompletionQueueRingOffset describes the offsets of various ring buffer fields
    70  type CompletionQueueRingOffset struct {
    71  	Head        uint32
    72  	Tail        uint32
    73  	RingMask    uint32
    74  	RingEntries uint32
    75  	Overflow    uint32
    76  	Cqes        uint32
    77  	Flags       uint32
    78  	Resv1       uint32
    79  	Resv2       uint64
    80  }
    81  
    82  func IOURingSetup(entries uint, params *IOURingParams) (int, error) {
    83  	res, _, errno := syscall.RawSyscall(
    84  		SYS_IO_URING_SETUP,
    85  		uintptr(entries),
    86  		uintptr(unsafe.Pointer(params)),
    87  		0,
    88  	)
    89  	if errno != 0 {
    90  		return int(res), os.NewSyscallError("iouring_setup", errno)
    91  	}
    92  
    93  	return int(res), nil
    94  }