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

     1  //go:build linux
     2  // +build linux
     3  
     4  package iouring_syscall
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"unsafe"
    10  )
    11  
    12  const (
    13  	SYS_IO_URING_SETUP    = 425
    14  	SYS_IO_URING_ENTER    = 426
    15  	SYS_IO_URING_REGISTER = 427
    16  )
    17  
    18  // IORING Offset
    19  const (
    20  	IORING_OFF_SQ_RING uint64 = 0
    21  	IORING_OFF_CQ_RING uint64 = 0x8000000
    22  	IORING_OFF_SQES    uint64 = 0x10000000
    23  )
    24  
    25  const (
    26  	IORING_OP_NOP uint8 = iota
    27  	IORING_OP_READV
    28  	IORING_OP_WRITEV
    29  	IORING_OP_FSYNC
    30  	IORING_OP_READ_FIXED
    31  	IORING_OP_WRITE_FIXED
    32  	IORING_OP_POLL_ADD
    33  	IORING_OP_POLL_REMOVE
    34  	IORING_OP_SYNC_FILE_RANGE
    35  	IORING_OP_SENDMSG
    36  	IORING_OP_RECVMSG
    37  	IORING_OP_TIMEOUT
    38  	IORING_OP_TIMEOUT_REMOVE
    39  	IORING_OP_ACCEPT
    40  	IORING_OP_ASYNC_CANCEL
    41  	IORING_OP_LINK_TIMEOUT
    42  	IORING_OP_CONNECT
    43  	IORING_OP_FALLOCATE
    44  	IORING_OP_OPENAT
    45  	IORING_OP_CLOSE
    46  	IORING_OP_FILES_UPDATE
    47  	IORING_OP_STATX
    48  	IORING_OP_READ
    49  	IORING_OP_WRITE
    50  	IORING_OP_FADVISE
    51  	IORING_OP_MADVISE
    52  	IORING_OP_SEND
    53  	IORING_OP_RECV
    54  	IORING_OP_OPENAT2
    55  	IORING_OP_EPOLL_CTL
    56  	IORING_OP_SPLICE
    57  	IORING_OP_PROVIDE_BUFFERS
    58  	IORING_OP_REMOVE_BUFFERS
    59  	IORING_OP_TEE
    60  	IORING_OP_SHUTDOWN
    61  	IORING_OP_RENAMEAT
    62  	IORING_OP_UNLINKAT
    63  	IORING_OP_MKDIRAT
    64  	IORING_OP_SYMLINKAT
    65  	IORING_OP_LINKAT
    66  	IORING_OP_MSG_RING
    67  	IORING_OP_FSETXATTR
    68  	IORING_OP_SETXATTR
    69  	IORING_OP_FGETXATTR
    70  	IORING_OP_GETXATTR
    71  	IORING_OP_SOCKET
    72  	IORING_OP_URING_CMD
    73  	IORING_OP_SEND_ZC
    74  	IORING_OP_SENDMSG_ZC
    75  
    76  	/* this goes last, obviously */
    77  	IORING_OP_LAST
    78  )
    79  
    80  const (
    81  	IORING_SQ_NEED_WAKEUP uint32 = 1 << iota
    82  	IORING_SQ_CQ_OVERFLOW
    83  )
    84  
    85  const (
    86  	IOSQE_FLAGS_FIXED_FILE uint8 = 1 << iota
    87  	IOSQE_FLAGS_IO_DRAIN
    88  	IOSQE_FLAGS_IO_LINK
    89  	IOSQE_FLAGS_IO_HARDLINK
    90  	IOSQE_FLAGS_ASYNC
    91  	IOSQE_FLAGS_BUFFER_SELECT
    92  )
    93  
    94  const IOSQE_SYNC_DATASYNC uint = 1
    95  const IOSQE_TIMEOUT_ABS uint = 1
    96  const IOSQE_SPLICE_F_FD_IN_FIXED = 1 << 31
    97  
    98  type SubmissionQueueEntry interface {
    99  	Opcode() uint8
   100  	Reset()
   101  	PrepOperation(op uint8, fd int32, addrOrSpliceOffIn uint64, len uint32, offsetOrCmdOp uint64)
   102  	Fd() int32
   103  	SetFdIndex(index int32)
   104  	SetOpFlags(opflags uint32)
   105  	SetUserData(userData uint64)
   106  	SetFlags(flag uint8)
   107  	CleanFlags(flags uint8)
   108  	SetIoprio(ioprio uint16)
   109  	SetBufIndex(bufIndex uint16)
   110  	SetBufGroup(bufGroup uint16)
   111  	SetPersonality(personality uint16)
   112  	SetSpliceFdIn(fdIn int32)
   113  
   114  	CMD(castType interface{}) interface{}
   115  }
   116  
   117  type sqeCore struct {
   118  	opcode   uint8
   119  	flags    uint8
   120  	ioprio   uint16
   121  	fd       int32
   122  	offset   uint64
   123  	addr     uint64
   124  	len      uint32
   125  	opFlags  uint32
   126  	userdata uint64
   127  
   128  	bufIndexOrGroup uint16
   129  	personality     uint16
   130  	spliceFdIn      int32
   131  }
   132  
   133  func (sqe *sqeCore) Opcode() uint8 {
   134  	return sqe.opcode
   135  }
   136  
   137  func (sqe *sqeCore) PrepOperation(op uint8, fd int32, addrOrSpliceOffIn uint64, len uint32, offsetOrCmdOp uint64) {
   138  	sqe.opcode = op
   139  	sqe.fd = fd
   140  	sqe.addr = addrOrSpliceOffIn
   141  	sqe.len = len
   142  	sqe.offset = offsetOrCmdOp
   143  }
   144  
   145  func (sqe *sqeCore) Fd() int32 {
   146  	return sqe.fd
   147  }
   148  
   149  func (sqe *sqeCore) SetFdIndex(index int32) {
   150  	sqe.fd = index
   151  	sqe.flags |= IOSQE_FLAGS_FIXED_FILE
   152  }
   153  
   154  func (sqe *sqeCore) SetOpFlags(opflags uint32) {
   155  	sqe.opFlags = opflags
   156  }
   157  
   158  func (sqe *sqeCore) SetUserData(userData uint64) {
   159  	sqe.userdata = userData
   160  }
   161  
   162  func (sqe *sqeCore) SetFlags(flags uint8) {
   163  	sqe.flags |= flags
   164  }
   165  
   166  func (sqe *sqeCore) CleanFlags(flags uint8) {
   167  	sqe.flags ^= flags
   168  }
   169  
   170  func (sqe *sqeCore) SetIoprio(ioprio uint16) {
   171  	sqe.ioprio = ioprio
   172  }
   173  
   174  func (sqe *sqeCore) SetBufIndex(bufIndex uint16) {
   175  	sqe.bufIndexOrGroup = bufIndex
   176  }
   177  
   178  func (sqe *sqeCore) SetBufGroup(bufGroup uint16) {
   179  	sqe.bufIndexOrGroup = bufGroup
   180  }
   181  
   182  func (sqe *sqeCore) SetPersonality(personality uint16) {
   183  	sqe.personality = personality
   184  }
   185  
   186  func (sqe *sqeCore) SetSpliceFdIn(fdIn int32) {
   187  	sqe.spliceFdIn = fdIn
   188  }
   189  
   190  type SubmissionQueueEntry64 struct {
   191  	sqeCore
   192  
   193  	extra [2]uint64
   194  }
   195  
   196  func (sqe *SubmissionQueueEntry64) Reset() {
   197  	*sqe = SubmissionQueueEntry64{}
   198  }
   199  
   200  func (sqe *SubmissionQueueEntry64) CMD(_ interface{}) interface{} {
   201  	panic(fmt.Errorf("unsupported interface for CMD command"))
   202  }
   203  
   204  type SubmissionQueueEntry128 struct {
   205  	sqeCore
   206  
   207  	cmd [80]uint8
   208  }
   209  
   210  func (sqe *SubmissionQueueEntry128) Reset() {
   211  	*sqe = SubmissionQueueEntry128{}
   212  }
   213  
   214  func (sqe *SubmissionQueueEntry128) CMD(castType interface{}) interface{} {
   215  	return reflect.NewAt(reflect.TypeOf(castType), unsafe.Pointer(&sqe.cmd[0])).Interface()
   216  }
   217  
   218  type CompletionQueueEvent interface {
   219  	UserData() uint64
   220  	Result() int32
   221  	Extra1() uint64
   222  	Extra2() uint64
   223  	Flags() uint32
   224  	Clone() CompletionQueueEvent
   225  }
   226  
   227  type cqeCore struct {
   228  	userData uint64
   229  	result   int32
   230  	flags    uint32
   231  }
   232  
   233  func (cqe *cqeCore) UserData() uint64 {
   234  	return cqe.userData
   235  }
   236  
   237  func (cqe *cqeCore) Result() int32 {
   238  	return cqe.result
   239  }
   240  
   241  func (cqe *cqeCore) Flags() uint32 {
   242  	return cqe.flags
   243  }
   244  
   245  type CompletionQueueEvent16 struct {
   246  	cqeCore
   247  }
   248  
   249  func (cqe *CompletionQueueEvent16) Extra1() uint64 {
   250  	return 0
   251  }
   252  
   253  func (cqe *CompletionQueueEvent16) Extra2() uint64 {
   254  	return 0
   255  }
   256  
   257  func (cqe *CompletionQueueEvent16) Clone() CompletionQueueEvent {
   258  	dest := &CompletionQueueEvent16{}
   259  	*dest = *cqe
   260  	return dest
   261  }
   262  
   263  type CompletionQueueEvent32 struct {
   264  	cqeCore
   265  
   266  	extra1 uint64
   267  	extra2 uint64
   268  }
   269  
   270  func (cqe *CompletionQueueEvent32) Extra1() uint64 {
   271  	return cqe.extra1
   272  }
   273  
   274  func (cqe *CompletionQueueEvent32) Extra2() uint64 {
   275  	return cqe.extra2
   276  }
   277  
   278  func (cqe *CompletionQueueEvent32) Clone() CompletionQueueEvent {
   279  	dest := &CompletionQueueEvent32{}
   280  	*dest = *cqe
   281  	return dest
   282  }
   283  
   284  const IORING_FSYNC_DATASYNC uint32 = 1
   285  const IORING_TIMEOUT_ABS uint32 = 1