1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
/// Represents the `kqueue` filters we might use:
///
/// - `except` corresponds to `EVFILT_EXCEPT`
/// - `read` corresponds to `EVFILT_READ`
/// - `write` corresponds to `EVFILT_WRITE`
private struct KQueueEventFilterSet: OptionSet, Equatable {
typealias RawValue = UInt8
let rawValue: RawValue
static let _none = KQueueEventFilterSet([])
// skipping `1 << 0` because kqueue doesn't have a direct match for `.reset` (`EPOLLHUP` for epoll)
static let except = KQueueEventFilterSet(rawValue: 1 << 1)
static let read = KQueueEventFilterSet(rawValue: 1 << 2)
static let write = KQueueEventFilterSet(rawValue: 1 << 3)
init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
extension KQueueEventFilterSet {
/// Convert NIO's `SelectorEventSet` set to a `KQueueEventFilterSet`
init(selectorEventSet: SelectorEventSet) {
var kqueueFilterSet: KQueueEventFilterSet = .init(rawValue: 0)
if selectorEventSet.contains(.read) {
kqueueFilterSet.formUnion(.read)
}
if selectorEventSet.contains(.write) {
kqueueFilterSet.formUnion(.write)
}
if isEarlyEOFDeliveryWorkingOnThisOS && selectorEventSet.contains(.readEOF) {
kqueueFilterSet.formUnion(.except)
}
self = kqueueFilterSet
}
/// Calculate the kqueue filter changes that are necessary to transition from `previousKQueueFilterSet` to `self`.
/// The `body` closure is then called with the changes necessary expressed as a number of `kevent`.
///
/// - parameters:
/// - previousKQueueFilterSet: The previous filter set that is currently registered with kqueue.
/// - fileDescriptor: The file descriptor the `kevent`s should be generated to.
/// - body: The closure that will then apply the change set.
func calculateKQueueFilterSetChanges(previousKQueueFilterSet: KQueueEventFilterSet,
fileDescriptor: CInt,
registrationID: SelectorRegistrationID,
_ body: (UnsafeMutableBufferPointer<kevent>) throws -> Void) rethrows {
// we only use three filters (EVFILT_READ, EVFILT_WRITE and EVFILT_EXCEPT) so the number of changes would be 3.
var keventsHopefullyOnStack = (kevent(), kevent(), kevent())
try withUnsafeMutableBytes(of: &keventsHopefullyOnStack) { rawPtr in
assert(MemoryLayout<kevent>.size * 3 == rawPtr.count)
let keventBuffer = rawPtr.baseAddress!.bindMemory(to: kevent.self, capacity: 3)
let differences = previousKQueueFilterSet.symmetricDifference(self) // contains all the events that need a change (either need to be added or removed)
func calculateKQueueChange(event: KQueueEventFilterSet) -> UInt16? {
guard differences.contains(event) else {
return nil
}
return UInt16(self.contains(event) ? EV_ADD : EV_DELETE)
}
var index: Int = 0
for (event, filter) in [(KQueueEventFilterSet.read, EVFILT_READ), (.write, EVFILT_WRITE), (.except, EVFILT_EXCEPT)] {
if let flags = calculateKQueueChange(event: event) {
keventBuffer[index].setEvent(fileDescriptor: fileDescriptor, filter: filter, flags: flags, registrationID: registrationID)
index += 1
}
}
try body(UnsafeMutableBufferPointer(start: keventBuffer, count: index))
}
}
}
extension SelectorRegistrationID {
init(kqueueUData: UnsafeMutableRawPointer?) {
self = .init(rawValue: UInt32(truncatingIfNeeded: UInt(bitPattern: kqueueUData)))
}
}
/* this is deliberately not thread-safe, only the wakeup() function may be called unprotectedly */
extension Selector: _SelectorBackendProtocol {
private static func toKQueueTimeSpec(strategy: SelectorStrategy) -> timespec? {
switch strategy {
case .block:
return nil
case .now:
return timespec(tv_sec: 0, tv_nsec: 0)
case .blockUntilTimeout(let nanoseconds):
// A scheduledTask() specified with a zero or negative timeAmount, will be scheduled immediately
// and therefore SHOULD NOT result in a kevent being created with a negative or zero timespec.
precondition(nanoseconds.nanoseconds > 0, "\(nanoseconds) is invalid (0 < nanoseconds)")
var ts = timespec(timeAmount: nanoseconds)
// Check that the timespec tv_nsec field conforms to the definition in the C11 standard (ISO/IEC 9899:2011).
assert((0..<1_000_000_000).contains(ts.tv_nsec), "\(ts) is invalid (0 <= tv_nsec < 1_000_000_000)")
// The maximum value in seconds supported by the Darwin-kernel interval timer (kern_time.c:itimerfix())
// (note that - whilst unlikely - this value *could* change).
let sysIntervalTimerMaxSec = 100_000_000
// Clamp the timespec tv_sec value to the maximum supported by the Darwin-kernel.
// Whilst this schedules the event far into the future (several years) it will still be triggered provided
// the system stays up.
ts.tv_sec = min(ts.tv_sec, sysIntervalTimerMaxSec)
return ts
}
}
/// Apply a kqueue changeset by calling the `kevent` function with the `kevent`s supplied in `keventBuffer`.
private func kqueueApplyEventChangeSet(keventBuffer: UnsafeMutableBufferPointer<kevent>) throws {
// WARNING: This is called on `self.myThread` OR with `self.externalSelectorFDLock` held.
// So it MUST NOT touch anything on `self.` apart from constants and `self.selectorFD`.
guard keventBuffer.count > 0 else {
// nothing to do
return
}
do {
try KQueue.kevent(kq: self.selectorFD,
changelist: keventBuffer.baseAddress!,
nchanges: CInt(keventBuffer.count),
eventlist: nil,
nevents: 0,
timeout: nil)
} catch let err as IOError {
if err.errnoCode == EINTR {
// See https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
// When kevent() call fails with EINTR error, all changes in the changelist have been applied.
return
}
throw err
}
}
private func kqueueUpdateEventNotifications<S: Selectable>(selectable: S, interested: SelectorEventSet, oldInterested: SelectorEventSet?, registrationID: SelectorRegistrationID) throws {
assert(self.myThread == NIOThread.current)
let oldKQueueFilters = KQueueEventFilterSet(selectorEventSet: oldInterested ?? ._none)
let newKQueueFilters = KQueueEventFilterSet(selectorEventSet: interested)
assert(interested.contains(.reset))
assert(oldInterested?.contains(.reset) ?? true)
try selectable.withUnsafeHandle {
try newKQueueFilters.calculateKQueueFilterSetChanges(previousKQueueFilterSet: oldKQueueFilters,
fileDescriptor: $0,
registrationID: registrationID,
kqueueApplyEventChangeSet)
}
}
func initialiseState0() throws {
self.selectorFD = try KQueue.kqueue()
self.lifecycleState = .open
var event = kevent()
event.ident = 0
event.filter = Int16(EVFILT_USER)
event.fflags = UInt32(NOTE_FFNOP)
event.data = 0
event.udata = nil
event.flags = UInt16(EV_ADD | EV_ENABLE | EV_CLEAR)
try withUnsafeMutablePointer(to: &event) { ptr in
try kqueueApplyEventChangeSet(keventBuffer: UnsafeMutableBufferPointer(start: ptr, count: 1))
}
}
func deinitAssertions0() {
}
func register0<S: Selectable>(selectable: S, fileDescriptor: CInt, interested: SelectorEventSet, registrationID: SelectorRegistrationID) throws {
try kqueueUpdateEventNotifications(selectable: selectable, interested: interested, oldInterested: nil, registrationID: registrationID)
}
func reregister0<S: Selectable>(selectable: S, fileDescriptor: CInt, oldInterested: SelectorEventSet, newInterested: SelectorEventSet, registrationID: SelectorRegistrationID) throws {
try kqueueUpdateEventNotifications(selectable: selectable, interested: newInterested, oldInterested: oldInterested, registrationID: registrationID)
}
func deregister0<S: Selectable>(selectable: S, fileDescriptor: CInt, oldInterested: SelectorEventSet, registrationID: SelectorRegistrationID) throws {
try kqueueUpdateEventNotifications(selectable: selectable, interested: .reset, oldInterested: oldInterested, registrationID: registrationID)
}
/// Apply the given `SelectorStrategy` and execute `body` once it's complete (which may produce `SelectorEvent`s to handle).
///
/// - parameters:
/// - strategy: The `SelectorStrategy` to apply
/// - body: The function to execute for each `SelectorEvent` that was produced.
func whenReady0(strategy: SelectorStrategy, onLoopBegin loopStart: () -> Void, _ body: (SelectorEvent<R>) throws -> Void) throws -> Void {
assert(self.myThread == NIOThread.current)
guard self.lifecycleState == .open else {
throw IOError(errnoCode: EBADF, reason: "can't call whenReady for selector as it's \(self.lifecycleState).")
}
let timespec = Selector.toKQueueTimeSpec(strategy: strategy)
let ready = try timespec.withUnsafeOptionalPointer { ts in
Int(try KQueue.kevent(kq: self.selectorFD, changelist: nil, nchanges: 0, eventlist: events, nevents: Int32(eventsCapacity), timeout: ts))
}
loopStart()
for i in 0..<ready {
let ev = events[i]
let filter = Int32(ev.filter)
let eventRegistrationID = SelectorRegistrationID(kqueueUData: ev.udata)
guard Int32(ev.flags) & EV_ERROR == 0 else {
throw IOError(errnoCode: Int32(ev.data), reason: "kevent returned with EV_ERROR set: \(String(describing: ev))")
}
guard filter != EVFILT_USER, let registration = registrations[Int(ev.ident)] else {
continue
}
guard eventRegistrationID == registration.registrationID else {
continue
}
var selectorEvent: SelectorEventSet = ._none
switch filter {
case EVFILT_READ:
selectorEvent.formUnion(.read)
fallthrough // falling through here as `EVFILT_READ` also delivers `EV_EOF` (meaning `.readEOF`)
case EVFILT_EXCEPT:
if Int32(ev.flags) & EV_EOF != 0 && registration.interested.contains(.readEOF) {
// we only add `.readEOF` if it happened and the user asked for it
selectorEvent.formUnion(.readEOF)
}
case EVFILT_WRITE:
selectorEvent.formUnion(.write)
default:
// We only use EVFILT_USER, EVFILT_READ, EVFILT_EXCEPT and EVFILT_WRITE.
fatalError("unexpected filter \(ev.filter)")
}
if ev.fflags != 0 {
selectorEvent.formUnion(.reset)
}
// we can only verify the events for i == 0 as for i > 0 the user might have changed the registrations since then.
assert(i != 0 || selectorEvent.isSubset(of: registration.interested), "selectorEvent: \(selectorEvent), registration: \(registration)")
// in any case we only want what the user is currently registered for & what we got
selectorEvent = selectorEvent.intersection(registration.interested)
guard selectorEvent != ._none else {
continue
}
try body((SelectorEvent(io: selectorEvent, registration: registration)))
}
growEventArrayIfNeeded(ready: ready)
}
/// Close the `Selector`.
///
/// After closing the `Selector` it's no longer possible to use it.
func close0() throws {
self.externalSelectorFDLock.withLock {
// We try! all of the closes because close can only fail in the following ways:
// - EINTR, which we eat in Posix.close
// - EIO, which can only happen for on-disk files
// - EBADF, which can't happen here because we would crash as EBADF is marked unacceptable
// Therefore, we assert here that close will always succeed and if not, that's a NIO bug we need to know
// about.
// We limit close to only be for positive FD:s though, as subclasses (e.g. uring)
// may already have closed some of these FD:s in their close function.
try! Posix.close(descriptor: self.selectorFD)
self.selectorFD = -1
}
}
/* attention, this may (will!) be called from outside the event loop, ie. can't access mutable shared state (such as `self.open`) */
func wakeup0() throws {
assert(NIOThread.current != self.myThread)
try self.externalSelectorFDLock.withLock {
guard self.selectorFD >= 0 else {
throw EventLoopError.shutdown
}
var event = kevent()
event.ident = 0
event.filter = Int16(EVFILT_USER)
event.fflags = UInt32(NOTE_TRIGGER | NOTE_FFNOP)
event.data = 0
event.udata = nil
event.flags = 0
try withUnsafeMutablePointer(to: &event) { ptr in
try self.kqueueApplyEventChangeSet(keventBuffer: UnsafeMutableBufferPointer(start: ptr, count: 1))
}
}
}
}
extension kevent {
/// Update a kevent for a given filter, file descriptor, and set of flags.
mutating func setEvent(fileDescriptor fd: CInt, filter: CInt, flags: UInt16, registrationID: SelectorRegistrationID) {
self.ident = UInt(fd)
self.filter = Int16(filter)
self.flags = flags
self.udata = UnsafeMutableRawPointer(bitPattern: UInt(registrationID.rawValue))
// On macOS, EVFILT_EXCEPT will fire whenever there is unread data in the socket receive
// buffer. This is not a behaviour we want from EVFILT_EXCEPT: we only want it to tell us
// about actually exceptional conditions. For this reason, when we set EVFILT_EXCEPT
// we do it with NOTE_LOWAT set to Int.max, which will ensure that there is never enough data
// in the send buffer to trigger EVFILT_EXCEPT. Thanks to the sensible design of kqueue,
// this only affects our EXCEPT filter: EVFILT_READ behaves separately.
if filter == EVFILT_EXCEPT {
self.fflags = CUnsignedInt(NOTE_LOWAT)
self.data = Int.max
} else {
self.fflags = 0
self.data = 0
}
}
}
#endif
|