|
1 | 1 | #include <unistd.h>
|
2 | 2 | #include <sys/syscall.h>
|
3 | 3 | #include <sys/uio.h>
|
4 |
| - |
5 | 4 | #include <signal.h>
|
| 5 | + |
| 6 | +#define __SWIFT_IORING_SQE_FALLBACK_STRUCT { \ |
| 7 | + __u8 opcode; \ |
| 8 | + __u8 flags; \ |
| 9 | + __u16 ioprio; \ |
| 10 | + __s32 fd; \ |
| 11 | + union { \ |
| 12 | + __u64 off; \ |
| 13 | + __u64 addr2; \ |
| 14 | + struct { \ |
| 15 | + __u32 cmd_op; \ |
| 16 | + __u32 __pad1; \ |
| 17 | + }; \ |
| 18 | + }; \ |
| 19 | + union { \ |
| 20 | + __u64 addr; \ |
| 21 | + __u64 splice_off_in; \ |
| 22 | + struct { \ |
| 23 | + __u32 level; \ |
| 24 | + __u32 optname; \ |
| 25 | + }; \ |
| 26 | + }; \ |
| 27 | + __u32 len; \ |
| 28 | + union { \ |
| 29 | + __kernel_rwf_t rw_flags; \ |
| 30 | + __u32 fsync_flags; \ |
| 31 | + __u16 poll_events; \ |
| 32 | + __u32 poll32_events; \ |
| 33 | + __u32 sync_range_flags; \ |
| 34 | + __u32 msg_flags; \ |
| 35 | + __u32 timeout_flags; \ |
| 36 | + __u32 accept_flags; \ |
| 37 | + __u32 cancel_flags; \ |
| 38 | + __u32 open_flags; \ |
| 39 | + __u32 statx_flags; \ |
| 40 | + __u32 fadvise_advice; \ |
| 41 | + __u32 splice_flags; \ |
| 42 | + __u32 rename_flags; \ |
| 43 | + __u32 unlink_flags; \ |
| 44 | + __u32 hardlink_flags; \ |
| 45 | + __u32 xattr_flags; \ |
| 46 | + __u32 msg_ring_flags; \ |
| 47 | + __u32 uring_cmd_flags; \ |
| 48 | + __u32 waitid_flags; \ |
| 49 | + __u32 futex_flags; \ |
| 50 | + __u32 install_fd_flags; \ |
| 51 | + __u32 nop_flags; \ |
| 52 | + }; \ |
| 53 | + __u64 user_data; \ |
| 54 | + union { \ |
| 55 | + __u16 buf_index; \ |
| 56 | + __u16 buf_group; \ |
| 57 | + } __attribute__((packed)); \ |
| 58 | + __u16 personality; \ |
| 59 | + union { \ |
| 60 | + __s32 splice_fd_in; \ |
| 61 | + __u32 file_index; \ |
| 62 | + __u32 optlen; \ |
| 63 | + struct { \ |
| 64 | + __u16 addr_len; \ |
| 65 | + __u16 __pad3[1]; \ |
| 66 | + }; \ |
| 67 | + }; \ |
| 68 | + union { \ |
| 69 | + struct { \ |
| 70 | + __u64 addr3; \ |
| 71 | + __u64 __pad2[1]; \ |
| 72 | + }; \ |
| 73 | + __u64 optval; \ |
| 74 | + __u8 cmd[0]; \ |
| 75 | + }; \ |
| 76 | +} |
| 77 | + |
| 78 | +#if __has_include(<linux/io_uring.h>) |
6 | 79 | #include <linux/io_uring.h>
|
7 | 80 |
|
| 81 | +#ifdef IORING_TIMEOUT_BOOTTIME |
| 82 | +// Kernel version >= 5.15, io_uring_sqe has file_index |
| 83 | +// and all current Swift operations are supported. |
| 84 | +#define __SWIFT_IORING_SUPPORTED true |
| 85 | +typedef struct io_uring_sqe swift_io_uring_sqe; |
| 86 | +#else |
| 87 | +// io_uring_sqe is missing properties that IORequest expects. |
| 88 | +// This configuration is not supported for now. |
| 89 | +// |
| 90 | +// Define a fallback struct to avoid build errors, but IORing |
| 91 | +// will throw ENOTSUP on initialization. |
| 92 | +#define __SWIFT_IORING_SUPPORTED false |
| 93 | +typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe; |
| 94 | +#endif |
| 95 | + |
| 96 | +// We can define more specific availability later |
| 97 | + |
| 98 | +#ifdef IORING_FEAT_RW_CUR_POS |
| 99 | +// Kernel version >= 5.6, io_uring_sqe has open_flags |
| 100 | +#endif |
| 101 | + |
| 102 | +#ifdef IORING_FEAT_NODROP |
| 103 | +// Kernel version >= 5.5, io_uring_sqe has cancel_flags |
| 104 | +#endif |
| 105 | + |
| 106 | +#else |
| 107 | +// Minimal fallback definitions when linux/io_uring.h is not available (e.g. static SDK) |
| 108 | +#include <stdint.h> |
| 109 | + |
| 110 | +#define __SWIFT_IORING_SUPPORTED false |
| 111 | + |
| 112 | +#define IORING_OFF_SQ_RING 0ULL |
| 113 | +#define IORING_OFF_CQ_RING 0x8000000ULL |
| 114 | +#define IORING_OFF_SQES 0x10000000ULL |
| 115 | + |
| 116 | +#define IORING_ENTER_GETEVENTS (1U << 0) |
| 117 | + |
| 118 | +#define IORING_FEAT_SINGLE_MMAP (1U << 0) |
| 119 | +#define IORING_FEAT_NODROP (1U << 1) |
| 120 | +#define IORING_FEAT_SUBMIT_STABLE (1U << 2) |
| 121 | +#define IORING_FEAT_RW_CUR_POS (1U << 3) |
| 122 | +#define IORING_FEAT_CUR_PERSONALITY (1U << 4) |
| 123 | +#define IORING_FEAT_FAST_POLL (1U << 5) |
| 124 | +#define IORING_FEAT_POLL_32BITS (1U << 6) |
| 125 | +#define IORING_FEAT_SQPOLL_NONFIXED (1U << 7) |
| 126 | +#define IORING_FEAT_EXT_ARG (1U << 8) |
| 127 | +#define IORING_FEAT_NATIVE_WORKERS (1U << 9) |
| 128 | +#define IORING_FEAT_RSRC_TAGS (1U << 10) |
| 129 | +#define IORING_FEAT_CQE_SKIP (1U << 11) |
| 130 | +#define IORING_FEAT_LINKED_FILE (1U << 12) |
| 131 | +#define IORING_FEAT_REG_REG_RING (1U << 13) |
| 132 | +#define IORING_FEAT_RECVSEND_BUNDLE (1U << 14) |
| 133 | +#define IORING_FEAT_MIN_TIMEOUT (1U << 15) |
| 134 | +#define IORING_FEAT_RW_ATTR (1U << 16) |
| 135 | +#define IORING_FEAT_NO_IOWAIT (1U << 17) |
| 136 | + |
| 137 | +typedef uint8_t __u8; |
| 138 | +typedef uint16_t __u16; |
| 139 | +typedef uint32_t __u32; |
| 140 | +typedef uint64_t __u64; |
| 141 | +typedef int32_t __s32; |
| 142 | + |
| 143 | +#ifndef __kernel_rwf_t |
| 144 | +typedef int __kernel_rwf_t; |
| 145 | +#endif |
| 146 | + |
| 147 | +typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe; |
| 148 | + |
| 149 | +struct io_uring_cqe { |
| 150 | + __u64 user_data; |
| 151 | + __s32 res; |
| 152 | + __u32 flags; |
| 153 | +}; |
| 154 | + |
| 155 | +struct io_sqring_offsets { |
| 156 | + __u32 head; |
| 157 | + __u32 tail; |
| 158 | + __u32 ring_mask; |
| 159 | + __u32 ring_entries; |
| 160 | + __u32 flags; |
| 161 | + __u32 dropped; |
| 162 | + __u32 array; |
| 163 | + __u32 resv1; |
| 164 | + __u64 user_addr; |
| 165 | +}; |
| 166 | + |
| 167 | +struct io_cqring_offsets { |
| 168 | + __u32 head; |
| 169 | + __u32 tail; |
| 170 | + __u32 ring_mask; |
| 171 | + __u32 ring_entries; |
| 172 | + __u32 overflow; |
| 173 | + __u32 cqes; |
| 174 | + __u32 flags; |
| 175 | + __u32 resv1; |
| 176 | + __u64 user_addr; |
| 177 | +}; |
| 178 | + |
| 179 | +struct io_uring_params { |
| 180 | + __u32 sq_entries; |
| 181 | + __u32 cq_entries; |
| 182 | + __u32 flags; |
| 183 | + __u32 sq_thread_cpu; |
| 184 | + __u32 sq_thread_idle; |
| 185 | + __u32 features; |
| 186 | + __u32 wq_fd; |
| 187 | + __u32 resv[3]; |
| 188 | + struct io_sqring_offsets sq_off; |
| 189 | + struct io_cqring_offsets cq_off; |
| 190 | +}; |
| 191 | +#endif // __has_include(<linux/io_uring.h>) |
| 192 | + |
8 | 193 | #ifndef SWIFT_IORING_C_WRAPPER
|
9 | 194 | #define SWIFT_IORING_C_WRAPPER
|
10 | 195 |
|
|
0 commit comments