From a1abc501bedd1feb80c8e26af0ba0b21b335173c Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Wed, 21 Oct 2026 12:01:53 +0200 Subject: [PATCH] [IO_URING] Add sendMessage and receiveMessage socket requests These submit IORING_OP_SENDMSG and IORING_OP_RECVMSG, which unconnected datagram sockets need to send to and receive from peer addresses. --- Sources/System/IORing/IORequest.swift | 44 ++++++++++++ .../Sockets/IORing+Sockets.swift | 68 +++++++++++++++++++ .../IORingSocketTests.swift | 67 ++++++++++++++++++ 3 files changed, 179 insertions(+) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index d9c333b..99e49e6 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -154,6 +154,18 @@ internal enum IORequestCore { flags: CInt, context: UInt64 = 0 ) + case sendMessage( + socket: FileDescriptor, + message: UnsafePointer, + flags: CInt, + context: UInt64 = 0 + ) + case receiveMessage( + socket: FileDescriptor, + message: UnsafeMutablePointer, + flags: CInt, + context: UInt64 = 0 + ) case shutdown( socket: FileDescriptor, how: CInt, @@ -723,6 +735,20 @@ extension IORing.Request { request.rawValue.len = UInt32(exactly: buffer.count)! request.rawValue.msg_flags = UInt32(bitPattern: flags) request.rawValue.user_data = context + case .sendMessage(let socket, let message, let flags, let context): + request.operation = .sendMessage + request.fileDescriptor = socket + request.rawValue.addr = UInt64(UInt(bitPattern: message)) + request.rawValue.len = 1 + request.rawValue.msg_flags = UInt32(bitPattern: flags) + request.rawValue.user_data = context + case .receiveMessage(let socket, let message, let flags, let context): + request.operation = .receiveMessage + request.fileDescriptor = socket + request.rawValue.addr = UInt64(UInt(bitPattern: message)) + request.rawValue.len = 1 + request.rawValue.msg_flags = UInt32(bitPattern: flags) + request.rawValue.user_data = context case .shutdown(let socket, let how, let context): request.operation = .shutdown request.fileDescriptor = socket @@ -818,6 +844,24 @@ extension IORing.Request { .init(core: .receive(socket: socket, buffer: buffer, flags: flags, context: context)) } + package static func _sendMessage( + _ message: UnsafePointer, + to socket: FileDescriptor, + flags: CInt, + context: UInt64 + ) -> IORing.Request { + .init(core: .sendMessage(socket: socket, message: message, flags: flags, context: context)) + } + + package static func _receiveMessage( + _ socket: FileDescriptor, + into message: UnsafeMutablePointer, + flags: CInt, + context: UInt64 + ) -> IORing.Request { + .init(core: .receiveMessage(socket: socket, message: message, flags: flags, context: context)) + } + package static func _shutdown( _ socket: FileDescriptor, how: CInt, diff --git a/Sources/SystemSockets/Sockets/IORing+Sockets.swift b/Sources/SystemSockets/Sockets/IORing+Sockets.swift index ddc5721..3a1dbc3 100644 --- a/Sources/SystemSockets/Sockets/IORing+Sockets.swift +++ b/Sources/SystemSockets/Sockets/IORing+Sockets.swift @@ -271,6 +271,74 @@ extension IORing.Request { ) } + /// Sends a message on a socket, optionally to a specific address. + /// + /// On success, the completion's ``IORing/Completion/result`` is the number + /// of bytes sent. This is how an unconnected datagram socket sends to a + /// peer, by setting the name of `message` to the peer's address. + /// + /// - Important: The kernel reads `message` and everything that it points to, + /// such as the address and the buffers, until the request completes, so + /// they have to stay valid until the completion has been consumed. + /// + /// The corresponding C function is `sendmsg`, and the operation is + /// `IORING_OP_SENDMSG`. + /// + /// - Parameters: + /// - message: The message to send. + /// - socket: The socket to send on. + /// - flags: Flags for sending. + /// - context: A value passed through to the completion. + /// - Returns: A request that sends the message. + public static func sendMessage( + _ message: UnsafePointer, + to socket: SocketDescriptor, + flags: SocketDescriptor.MessageFlags = .none, + context: UInt64 = 0 + ) -> IORing.Request { + ._sendMessage( + message, + to: FileDescriptor(rawValue: socket.rawValue), + flags: flags.rawValue, + context: context + ) + } + + /// Receives a message from a socket, including the address of its sender. + /// + /// On success, the completion's ``IORing/Completion/result`` is the number + /// of bytes received, and the kernel updates `message` with the length of + /// the sender's address and with the flags of the message, such as + /// truncation. This is how an unconnected datagram socket learns which peer + /// sent a datagram. + /// + /// - Important: The kernel writes into `message` and everything that it + /// points to until the request completes, so they have to stay valid until + /// the completion has been consumed. + /// + /// The corresponding C function is `recvmsg`, and the operation is + /// `IORING_OP_RECVMSG`. + /// + /// - Parameters: + /// - socket: The socket to receive from. + /// - message: The message to receive into. + /// - flags: Flags for receiving. + /// - context: A value passed through to the completion. + /// - Returns: A request that receives a message. + public static func receiveMessage( + _ socket: SocketDescriptor, + into message: UnsafeMutablePointer, + flags: SocketDescriptor.MessageFlags = .none, + context: UInt64 = 0 + ) -> IORing.Request { + ._receiveMessage( + FileDescriptor(rawValue: socket.rawValue), + into: message, + flags: flags.rawValue, + context: context + ) + } + /// Shuts down one or both directions of a socket. /// /// The corresponding C function is `shutdown`, and the operation is diff --git a/Tests/SystemSocketsTests/IORingSocketTests.swift b/Tests/SystemSocketsTests/IORingSocketTests.swift index 353b165..aa29417 100644 --- a/Tests/SystemSocketsTests/IORingSocketTests.swift +++ b/Tests/SystemSocketsTests/IORingSocketTests.swift @@ -62,6 +62,11 @@ private let socketSupported = kernelSupports( @available(System 199, *) private let shutdownSupported = kernelSupports(.shutdown(invalidSocket, .write)) +@available(System 199, *) +private let sendMessageSupported: Bool = { + var header = msghdr() + return kernelSupports(.sendMessage(&header, to: invalidSocket)) +}() @available(System 199, *) private let bindAndListenSupported = kernelSupports(.listen(invalidSocket, backlog: 1)) @@ -163,6 +168,68 @@ private struct IORingSocketTests { try client.connect(to: address) } + @available(System 199, *) + @Test(.enabled(if: sendMessageSupported)) + func sendAndReceiveMessageWithPeerAddress() throws { + let receiver = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp) + defer { try? receiver.close() } + try receiver.bind(to: SocketAddress(ipv4: .loopback(port: 0))) + let sender = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp) + defer { try? sender.close() } + try sender.bind(to: SocketAddress(ipv4: .loopback(port: 0))) + + var receiverAddress = sockaddr_storage() + var receiverAddressLength = socklen_t(MemoryLayout.size) + try withUnsafeMutablePointer(to: &receiverAddress) { + try $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { + guard getsockname(receiver.rawValue, $0, &receiverAddressLength) == 0 else { + throw Errno.current + } + } + } + + var ring = try IORing(queueDepth: 4) + let message: [UInt8] = Array("Hello, UDP!".utf8) + var received = [UInt8](repeating: 0, count: 64) + var peerAddress = sockaddr_storage() + + let results = try message.withUnsafeBytes { sendBytes in + try received.withUnsafeMutableBytes { receiveBytes in + try withUnsafeMutablePointer(to: &receiverAddress) { receiverAddressPointer in + try withUnsafeMutablePointer(to: &peerAddress) { peerAddressPointer in + var sendVector = iovec(iov_base: UnsafeMutableRawPointer(mutating: sendBytes.baseAddress), iov_len: sendBytes.count) + var receiveVector = iovec(iov_base: receiveBytes.baseAddress, iov_len: receiveBytes.count) + var sendHeader = msghdr() + sendHeader.msg_name = UnsafeMutableRawPointer(receiverAddressPointer) + sendHeader.msg_namelen = receiverAddressLength + var receiveHeader = msghdr() + receiveHeader.msg_name = UnsafeMutableRawPointer(peerAddressPointer) + receiveHeader.msg_namelen = socklen_t(MemoryLayout.size) + return try withUnsafeMutablePointer(to: &sendVector) { sendVectorPointer in + try withUnsafeMutablePointer(to: &receiveVector) { receiveVectorPointer in + sendHeader.msg_iov = sendVectorPointer + sendHeader.msg_iovlen = 1 + receiveHeader.msg_iov = receiveVectorPointer + receiveHeader.msg_iovlen = 1 + let receivePrepared = ring.prepare(request: .receiveMessage(receiver, into: &receiveHeader, context: 1)) + #expect(receivePrepared) + let sendPrepared = ring.prepare(request: .sendMessage(&sendHeader, to: sender, context: 2)) + #expect(sendPrepared) + try ring.submitPreparedRequests() + return try consumeResults(2, from: ring) + } + } + } + } + } + } + #expect(results[2] == Int32(message.count)) + #expect(results[1] == Int32(message.count)) + #expect(Array(received.prefix(message.count)) == message) + // The kernel reports who sent the datagram. + #expect(peerAddress.ss_family == sa_family_t(AF_INET)) + } + @available(System 199, *) @Test(.enabled(if: shutdownSupported)) func acceptConnectSendReceiveAndShutdown() throws {