mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
[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.
This commit is contained in:
1 parent
ff97796848
commit
a1abc501be
3 files changed
+179
No files matched your search
@@ -154,6 +154,18 @@ internal enum IORequestCore {
|
||||
flags: CInt,
|
||||
context: UInt64 = 0
|
||||
)
|
||||
case sendMessage(
|
||||
socket: FileDescriptor,
|
||||
message: UnsafePointer<msghdr>,
|
||||
flags: CInt,
|
||||
context: UInt64 = 0
|
||||
)
|
||||
case receiveMessage(
|
||||
socket: FileDescriptor,
|
||||
message: UnsafeMutablePointer<msghdr>,
|
||||
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<msghdr>,
|
||||
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<msghdr>,
|
||||
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,
|
||||
|
||||
@@ -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<CInterop.MsgHdr>,
|
||||
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<CInterop.MsgHdr>,
|
||||
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
|
||||
|
||||
@@ -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<sockaddr_storage>.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<sockaddr_storage>.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 {
|
||||
|
||||
Reference in new issue
Block a user