github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/abi/linux/iouring.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  // Constants for io_uring_setup(2). See include/uapi/linux/io_uring.h.
    18  const (
    19  	IORING_SETUP_IOPOLL     = (1 << 0)
    20  	IORING_SETUP_SQPOLL     = (1 << 1)
    21  	IORING_SETUP_SQ_AFF     = (1 << 2)
    22  	IORING_SETUP_CQSIZE     = (1 << 3)
    23  	IORING_SETUP_CLAMP      = (1 << 4)
    24  	IORING_SETUP_ATTACH_WQ  = (1 << 5)
    25  	IORING_SETUP_R_DISABLED = (1 << 6)
    26  	IORING_SETUP_SUBMIT_ALL = (1 << 7)
    27  )
    28  
    29  // Constants for io_uring_enter(2). See include/uapi/linux/io_uring.h.
    30  const (
    31  	IORING_ENTER_GETEVENTS = (1 << 0)
    32  )
    33  
    34  // Constants for IoUringParams.Features. See include/uapi/linux/io_uring.h.
    35  const (
    36  	IORING_FEAT_SINGLE_MMAP = (1 << 0)
    37  )
    38  
    39  // Constants for IO_URING. See include/uapi/linux/io_uring.h.
    40  const (
    41  	IORING_SETUP_COOP_TASKRUN = (1 << 8)
    42  	IORING_SETUP_TASKRUN_FLAG = (1 << 9)
    43  	IORING_SETUP_SQE128       = (1 << 10)
    44  	IORING_SETUP_CQE32        = (1 << 11)
    45  )
    46  
    47  // Constants for IO_URING. See io_uring/io_uring.c.
    48  const (
    49  	IORING_MAX_ENTRIES    = (1 << 15) // 32768
    50  	IORING_MAX_CQ_ENTRIES = (2 * IORING_MAX_ENTRIES)
    51  )
    52  
    53  // Constants for the offsets for the application to mmap the data it needs.
    54  // See include/uapi/linux/io_uring.h.
    55  const (
    56  	IORING_OFF_SQ_RING = 0
    57  	IORING_OFF_CQ_RING = 0x8000000
    58  	IORING_OFF_SQES    = 0x10000000
    59  )
    60  
    61  // Constants for the IO_URING opcodes. See include/uapi/linux/io_uring.h.
    62  const (
    63  	IORING_OP_NOP   = 0
    64  	IORING_OP_READV = 1
    65  )
    66  
    67  // IORingIndex represents SQE array indexes.
    68  //
    69  // +marshal
    70  type IORingIndex uint32
    71  
    72  // IOSqRingOffsets implements io_sqring_offsets struct.
    73  // IOSqRingOffsets represents offsets into IORings.
    74  // See struct io_sqring_offsets in include/uapi/linux/io_uring.h.
    75  //
    76  // +marshal
    77  type IOSqRingOffsets struct {
    78  	Head        uint32 // Offset to io_rings.sq.head
    79  	Tail        uint32 // Offset to io_rings.sq.tail
    80  	RingMask    uint32 // Offset to io_rings.sq_ring_mask
    81  	RingEntries uint32 // Offset to io_rings.sq_ring_entries
    82  	Flags       uint32 // Offset to io_rings.sq_flags
    83  	Dropped     uint32 // Offset to io_rings.sq_dropped
    84  	Array       uint32 // Offset to an array of SQE indices
    85  	Resv1       uint32 // Currently reserved and expected to be zero
    86  	Resv2       uint64 // Currently reserved and expected to be zero
    87  }
    88  
    89  // IOCqRingOffsets implements io_cqring_offsets struct.
    90  // IOCqRingOffsets represents offsets into IORings.
    91  // See struct io_cqring_offsets in include/uapi/linux/io_uring.h.
    92  //
    93  // +marshal
    94  type IOCqRingOffsets struct {
    95  	Head        uint32 // Offset to io_rings.cq.head
    96  	Tail        uint32 // Offset to io_rings.cq.tail
    97  	RingMask    uint32 // Offset to io_rings.cq_ring_mask
    98  	RingEntries uint32 // Offset to io_rings.cq_ring_entries
    99  	Overflow    uint32 // Offset to io_rings.cq_overflow
   100  	Cqes        uint32 // Offset to io_rings.cqes
   101  	Flags       uint32 // Offset to io_rings.cq_flags
   102  	Resv1       uint32 // Currently reserved and expected to be zero
   103  	Resv2       uint64 // Currently reserved and expected to be zero
   104  }
   105  
   106  // IOUringParams implements io_uring_params struct.
   107  // See struct io_uring_params in include/uapi/linux/io_uring.h.
   108  //
   109  // +marshal
   110  type IOUringParams struct {
   111  	SqEntries    uint32
   112  	CqEntries    uint32
   113  	Flags        uint32
   114  	SqThreadCPU  uint32
   115  	SqThreadIdle uint32
   116  	Features     uint32
   117  	WqFd         uint32
   118  	Resv         [3]uint32
   119  	SqOff        IOSqRingOffsets
   120  	CqOff        IOCqRingOffsets
   121  }
   122  
   123  // IOUringCqe implements IO completion data structure (Completion Queue Entry)
   124  // io_uring_cqe struct. As we don't currently support IORING_SETUP_CQE32 flag
   125  // its size is 16 bytes.
   126  // See struct io_uring_cqe in include/uapi/linux/io_uring.h.
   127  //
   128  // +marshal
   129  // +stateify savable
   130  type IOUringCqe struct {
   131  	UserData uint64
   132  	Res      int32
   133  	Flags    uint32
   134  }
   135  
   136  // IOUring implements io_uring struct.
   137  // See struct io_uring in io_uring/io_uring.c.
   138  //
   139  // +marshal
   140  // +stateify savable
   141  type IOUring struct {
   142  	// Both head and tail should be cacheline aligned. And we assume that
   143  	// cacheline size is 64 bytes.
   144  	Head uint32
   145  	_    [60]byte
   146  	Tail uint32
   147  	_    [60]byte
   148  }
   149  
   150  // IORings implements io_rings struct.
   151  // This struct describes layout of the mapped region backed by the ringBuffersFile.
   152  // See struct io_rings in io_uring/io_uring.c.
   153  //
   154  // +marshal
   155  // +stateify savable
   156  type IORings struct {
   157  	Sq            IOUring
   158  	Cq            IOUring
   159  	SqRingMask    uint32
   160  	CqRingMask    uint32
   161  	SqRingEntries uint32
   162  	CqRingEntries uint32
   163  	sqDropped     uint32
   164  	sqFlags       int32
   165  	cqFlags       uint32
   166  	CqOverflow    uint32
   167  	_             [32]byte // Padding so cqes is cacheline aligned
   168  	// Linux has an additional field struct io_uring_cqe cqes[], which represents
   169  	// a dynamic array. We don't include it here in order to enable marshalling.
   170  }
   171  
   172  // IOUringSqe implements io_uring_sqe struct.
   173  // This struct represents IO submission data structure (Submission Queue Entry). As we don't yet
   174  // support IORING_SETUP_SQE128 flag, its size is 64 bytes with no extra padding at the end.
   175  // See include/uapi/linux/io_uring.h.
   176  //
   177  // +marshal
   178  // +stateify savable
   179  type IOUringSqe struct {
   180  	Opcode              uint8
   181  	Flags               uint8
   182  	IoPrio              uint16
   183  	Fd                  int32
   184  	OffOrAddrOrCmdOp    uint64
   185  	AddrOrSpliceOff     uint64
   186  	Len                 uint32
   187  	specialFlags        uint32
   188  	UserData            uint64
   189  	BufIndexOrGroup     uint16
   190  	personality         uint16
   191  	spliceFDOrFileIndex int32
   192  	addr3               uint64
   193  	_                   uint64
   194  }
   195  
   196  const (
   197  	_IOSqRingOffset        = 0   // +checkoffset . IORings.Sq
   198  	_IOSqRingOffsetHead    = 0   // +checkoffset . IOUring.Head
   199  	_IOSqRingOffsetTail    = 64  // +checkoffset . IOUring.Tail
   200  	_IOSqRingOffsetMask    = 256 // +checkoffset . IORings.SqRingMask
   201  	_IOSqRingOffsetEntries = 264 // +checkoffset . IORings.SqRingEntries
   202  	_IOSqRingOffsetFlags   = 276 // +checkoffset . IORings.sqFlags
   203  	_IOSqRingOffsetDropped = 272 // +checkoffset . IORings.sqDropped
   204  )
   205  
   206  // PreComputedIOSqRingOffsets returns precomputed values for IOSqRingOffsets.
   207  func PreComputedIOSqRingOffsets() IOSqRingOffsets {
   208  	return IOSqRingOffsets{
   209  		Head:        _IOSqRingOffset + _IOSqRingOffsetHead,
   210  		Tail:        _IOSqRingOffset + _IOSqRingOffsetTail,
   211  		RingMask:    _IOSqRingOffsetMask,
   212  		RingEntries: _IOSqRingOffsetEntries,
   213  		Flags:       _IOSqRingOffsetFlags,
   214  		Dropped:     _IOSqRingOffsetDropped,
   215  	}
   216  }
   217  
   218  const (
   219  	_IOCqRingOffset         = 128 // +checkoffset . IORings.Cq
   220  	_IOCqRingOffsetHead     = 0   // +checkoffset . IOUring.Head
   221  	_IOCqRingOffsetTail     = 64  // +checkoffset . IOUring.Tail
   222  	_IOCqRingOffsetMask     = 260 // +checkoffset . IORings.CqRingMask
   223  	_IOCqRingOffsetEntries  = 268 // +checkoffset . IORings.CqRingEntries
   224  	_IOCqRingOffsetFlags    = 280 // +checkoffset . IORings.cqFlags
   225  	_IOCqRingOffsetOverflow = 284 // +checkoffset . IORings.CqOverflow
   226  )
   227  
   228  // PreComputedIOCqRingOffsets returns precomputed values for IOCqRingOffsets.
   229  func PreComputedIOCqRingOffsets() IOCqRingOffsets {
   230  	return IOCqRingOffsets{
   231  		Head:        _IOCqRingOffset + _IOCqRingOffsetHead,
   232  		Tail:        _IOCqRingOffset + _IOCqRingOffsetTail,
   233  		RingMask:    _IOCqRingOffsetMask,
   234  		RingEntries: _IOCqRingOffsetEntries,
   235  		Overflow:    _IOCqRingOffsetOverflow,
   236  		Flags:       _IOCqRingOffsetFlags,
   237  	}
   238  }