PlatformSupport.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Logging
  17. import NIOCore
  18. import NIOPosix
  19. import NIOTransportServices
  20. /// How a network implementation should be chosen.
  21. public struct NetworkPreference: Hashable {
  22. private enum Wrapped: Hashable {
  23. case best
  24. case userDefined(NetworkImplementation)
  25. }
  26. private var wrapped: Wrapped
  27. private init(_ wrapped: Wrapped) {
  28. self.wrapped = wrapped
  29. }
  30. /// Use the best available, that is, Network.framework (and NIOTransportServices) when it is
  31. /// available on Darwin platforms (macOS 10.14+, iOS 12.0+, tvOS 12.0+, watchOS 6.0+), and
  32. /// falling back to the POSIX network model otherwise.
  33. public static let best = NetworkPreference(.best)
  34. /// Use the given implementation. Doing so may require additional availability checks depending
  35. /// on the implementation.
  36. public static func userDefined(_ implementation: NetworkImplementation) -> NetworkPreference {
  37. return NetworkPreference(.userDefined(implementation))
  38. }
  39. }
  40. /// The network implementation to use: POSIX sockets or Network.framework. This also determines
  41. /// which variant of NIO to use; NIO or NIOTransportServices, respectively.
  42. public struct NetworkImplementation: Hashable {
  43. fileprivate enum Wrapped: Hashable {
  44. case networkFramework
  45. case posix
  46. }
  47. fileprivate var wrapped: Wrapped
  48. private init(_ wrapped: Wrapped) {
  49. self.wrapped = wrapped
  50. }
  51. #if canImport(Network)
  52. /// Network.framework (NIOTransportServices).
  53. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  54. public static let networkFramework = NetworkImplementation(.networkFramework)
  55. #endif
  56. /// POSIX (NIO).
  57. public static let posix = NetworkImplementation(.posix)
  58. internal static func matchingEventLoopGroup(_ group: EventLoopGroup) -> NetworkImplementation {
  59. #if canImport(Network)
  60. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  61. if PlatformSupport.isTransportServicesEventLoopGroup(group) {
  62. return .networkFramework
  63. }
  64. }
  65. #endif
  66. return .posix
  67. }
  68. }
  69. extension NetworkPreference {
  70. /// The network implementation, and by extension the NIO variant which will be used.
  71. ///
  72. /// Network.framework is available on macOS 10.14+, iOS 12.0+, tvOS 12.0+ and watchOS 6.0+.
  73. ///
  74. /// This isn't directly useful when implementing code which branches on the network preference
  75. /// since that code will still need the appropriate availability check:
  76. ///
  77. /// - `@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)`, or
  78. /// - `#available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)`.
  79. public var implementation: NetworkImplementation {
  80. switch self.wrapped {
  81. case .best:
  82. #if canImport(Network)
  83. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  84. return .networkFramework
  85. } else {
  86. // Older platforms must use the POSIX loop.
  87. return .posix
  88. }
  89. #else
  90. return .posix
  91. #endif
  92. case let .userDefined(implementation):
  93. return implementation
  94. }
  95. }
  96. }
  97. // MARK: - Generic Bootstraps
  98. // TODO: Revisit the handling of NIO/NIOTS once https://github.com/apple/swift-nio/issues/796
  99. // is addressed.
  100. /// This protocol is intended as a layer of abstraction over `ClientBootstrap` and
  101. /// `NIOTSConnectionBootstrap`.
  102. public protocol ClientBootstrapProtocol {
  103. func connect(to: SocketAddress) -> EventLoopFuture<Channel>
  104. func connect(host: String, port: Int) -> EventLoopFuture<Channel>
  105. func connect(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
  106. func withConnectedSocket(_ socket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel>
  107. func connect(to vsockAddress: VsockAddress) -> EventLoopFuture<Channel>
  108. func connectTimeout(_ timeout: TimeAmount) -> Self
  109. func channelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  110. #if swift(>=5.7)
  111. @preconcurrency
  112. func channelInitializer(_ handler: @escaping @Sendable (Channel) -> EventLoopFuture<Void>) -> Self
  113. #else
  114. func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  115. #endif
  116. }
  117. extension ClientBootstrapProtocol {
  118. public func withConnectedSocket(_ socket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel> {
  119. preconditionFailure("withConnectedSocket(_:) is not implemented")
  120. }
  121. }
  122. extension ClientBootstrap: ClientBootstrapProtocol {}
  123. #if canImport(Network)
  124. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  125. extension NIOTSConnectionBootstrap: ClientBootstrapProtocol {
  126. public func withConnectedSocket(_ socket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel> {
  127. preconditionFailure("NIOTSConnectionBootstrap does not support withConnectedSocket(_:)")
  128. }
  129. public func connect(to vsockAddress: VsockAddress) -> EventLoopFuture<Channel> {
  130. preconditionFailure("NIOTSConnectionBootstrap does not support connect(to vsockAddress:)")
  131. }
  132. }
  133. #endif
  134. /// This protocol is intended as a layer of abstraction over `ServerBootstrap` and
  135. /// `NIOTSListenerBootstrap`.
  136. public protocol ServerBootstrapProtocol {
  137. func bind(to: SocketAddress) -> EventLoopFuture<Channel>
  138. func bind(host: String, port: Int) -> EventLoopFuture<Channel>
  139. func bind(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
  140. func withBoundSocket(_ connectedSocket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel>
  141. func bind(to vsockAddress: VsockAddress) -> EventLoopFuture<Channel>
  142. #if swift(>=5.7)
  143. @preconcurrency
  144. func serverChannelInitializer(
  145. _ handler: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  146. ) -> Self
  147. #else
  148. func serverChannelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  149. #endif
  150. func serverChannelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  151. #if swift(>=5.7)
  152. @preconcurrency
  153. func childChannelInitializer(_ handler: @escaping @Sendable (Channel) -> EventLoopFuture<Void>)
  154. -> Self
  155. #else
  156. func childChannelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  157. #endif
  158. func childChannelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  159. }
  160. extension ServerBootstrapProtocol {
  161. public func withBoundSocket(_ connectedSocket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel> {
  162. preconditionFailure("withBoundSocket(_:) is not implemented")
  163. }
  164. }
  165. extension ServerBootstrap: ServerBootstrapProtocol {}
  166. #if canImport(Network)
  167. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  168. extension NIOTSListenerBootstrap: ServerBootstrapProtocol {
  169. public func withBoundSocket(_ connectedSocket: NIOBSDSocket.Handle) -> EventLoopFuture<Channel> {
  170. preconditionFailure("NIOTSListenerBootstrap does not support withBoundSocket(_:)")
  171. }
  172. public func bind(to vsockAddress: VsockAddress) -> EventLoopFuture<Channel> {
  173. preconditionFailure("NIOTSListenerBootstrap does not support bind(to vsockAddress:)")
  174. }
  175. }
  176. #endif
  177. // MARK: - Bootstrap / EventLoopGroup helpers
  178. public enum PlatformSupport {
  179. /// Makes a new event loop group based on the network preference.
  180. ///
  181. /// If `.best` is chosen and `Network.framework` is available then `NIOTSEventLoopGroup` will
  182. /// be returned. A `MultiThreadedEventLoopGroup` will be returned otherwise.
  183. ///
  184. /// - Parameter loopCount: The number of event loops to create in the event loop group.
  185. /// - Parameter networkPreference: Network preference; defaulting to `.best`.
  186. public static func makeEventLoopGroup(
  187. loopCount: Int,
  188. networkPreference: NetworkPreference = .best,
  189. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  190. ) -> EventLoopGroup {
  191. logger.debug("making EventLoopGroup for \(networkPreference) network preference")
  192. switch networkPreference.implementation.wrapped {
  193. case .networkFramework:
  194. #if canImport(Network)
  195. guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else {
  196. logger.critical("Network.framework can be imported but is not supported on this platform")
  197. // This is gated by the availability of `.networkFramework` so should never happen.
  198. fatalError(".networkFramework is being used on an unsupported platform")
  199. }
  200. logger.debug("created NIOTSEventLoopGroup for \(networkPreference) preference")
  201. return NIOTSEventLoopGroup(loopCount: loopCount)
  202. #else
  203. fatalError(".networkFramework is being used on an unsupported platform")
  204. #endif
  205. case .posix:
  206. logger.debug("created MultiThreadedEventLoopGroup for \(networkPreference) preference")
  207. return MultiThreadedEventLoopGroup(numberOfThreads: loopCount)
  208. }
  209. }
  210. /// Makes a new client bootstrap using the given `EventLoopGroup`.
  211. ///
  212. /// If the `EventLoopGroup` is a `NIOTSEventLoopGroup` then the returned bootstrap will be a
  213. /// `NIOTSConnectionBootstrap`, otherwise it will be a `ClientBootstrap`.
  214. ///
  215. /// - Parameter group: The `EventLoopGroup` to use.
  216. public static func makeClientBootstrap(
  217. group: EventLoopGroup,
  218. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  219. ) -> ClientBootstrapProtocol {
  220. logger.debug("making client bootstrap with event loop group of type \(type(of: group))")
  221. #if canImport(Network)
  222. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  223. if isTransportServicesEventLoopGroup(group) {
  224. logger.debug(
  225. "Network.framework is available and the EventLoopGroup is compatible with NIOTS, creating a NIOTSConnectionBootstrap"
  226. )
  227. return NIOTSConnectionBootstrap(group: group)
  228. } else {
  229. logger.debug(
  230. "Network.framework is available but the EventLoopGroup is not compatible with NIOTS, falling back to ClientBootstrap"
  231. )
  232. }
  233. }
  234. #endif
  235. logger.debug("creating a ClientBootstrap")
  236. return ClientBootstrap(group: group)
  237. }
  238. internal static func isTransportServicesEventLoopGroup(_ group: EventLoopGroup) -> Bool {
  239. #if canImport(Network)
  240. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  241. return group is NIOTSEventLoopGroup || group is QoSEventLoop
  242. }
  243. #endif
  244. return false
  245. }
  246. internal static func makeClientBootstrap(
  247. group: EventLoopGroup,
  248. tlsConfiguration: GRPCTLSConfiguration?,
  249. logger: Logger
  250. ) -> ClientBootstrapProtocol {
  251. let bootstrap = self.makeClientBootstrap(group: group, logger: logger)
  252. guard let tlsConfigruation = tlsConfiguration else {
  253. return bootstrap
  254. }
  255. #if canImport(Network)
  256. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *),
  257. let transportServicesBootstrap = bootstrap as? NIOTSConnectionBootstrap {
  258. return transportServicesBootstrap.tlsOptions(from: tlsConfigruation)
  259. }
  260. #endif
  261. return bootstrap
  262. }
  263. /// Makes a new server bootstrap using the given `EventLoopGroup`.
  264. ///
  265. /// If the `EventLoopGroup` is a `NIOTSEventLoopGroup` then the returned bootstrap will be a
  266. /// `NIOTSListenerBootstrap`, otherwise it will be a `ServerBootstrap`.
  267. ///
  268. /// - Parameter group: The `EventLoopGroup` to use.
  269. public static func makeServerBootstrap(
  270. group: EventLoopGroup,
  271. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  272. ) -> ServerBootstrapProtocol {
  273. logger.debug("making server bootstrap with event loop group of type \(type(of: group))")
  274. #if canImport(Network)
  275. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  276. if let tsGroup = group as? NIOTSEventLoopGroup {
  277. logger
  278. .debug(
  279. "Network.framework is available and the group is correctly typed, creating a NIOTSListenerBootstrap"
  280. )
  281. return NIOTSListenerBootstrap(group: tsGroup)
  282. } else if let qosEventLoop = group as? QoSEventLoop {
  283. logger
  284. .debug(
  285. "Network.framework is available and the group is correctly typed, creating a NIOTSListenerBootstrap"
  286. )
  287. return NIOTSListenerBootstrap(group: qosEventLoop)
  288. }
  289. logger
  290. .debug(
  291. "Network.framework is available but the group is not typed for NIOTS, falling back to ServerBootstrap"
  292. )
  293. }
  294. #endif
  295. logger.debug("creating a ServerBootstrap")
  296. return ServerBootstrap(group: group)
  297. }
  298. /// Determines whether we may need to work around an issue in Network.framework with zero-length writes.
  299. ///
  300. /// See https://github.com/apple/swift-nio-transport-services/pull/72 for more.
  301. static func requiresZeroLengthWriteWorkaround(group: EventLoopGroup, hasTLS: Bool) -> Bool {
  302. #if canImport(Network)
  303. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  304. if group is NIOTSEventLoopGroup || group is QoSEventLoop {
  305. // We need the zero-length write workaround on NIOTS when not using TLS.
  306. return !hasTLS
  307. } else {
  308. return false
  309. }
  310. } else {
  311. return false
  312. }
  313. #else
  314. return false
  315. #endif
  316. }
  317. }
  318. extension PlatformSupport {
  319. /// Make an `EventLoopGroup` which is compatible with the given TLS configuration/
  320. ///
  321. /// - Parameters:
  322. /// - configuration: The configuration to make a compatible `EventLoopGroup` for.
  323. /// - loopCount: The number of loops the `EventLoopGroup` should have.
  324. /// - Returns: An `EventLoopGroup` compatible with the given `configuration`.
  325. public static func makeEventLoopGroup(
  326. compatibleWith configuration: GRPCTLSConfiguration,
  327. loopCount: Int
  328. ) -> EventLoopGroup {
  329. #if canImport(Network)
  330. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  331. if configuration.isNetworkFrameworkTLSBackend {
  332. return NIOTSEventLoopGroup(loopCount: loopCount)
  333. }
  334. }
  335. #endif
  336. return MultiThreadedEventLoopGroup(numberOfThreads: loopCount)
  337. }
  338. }
  339. extension GRPCTLSConfiguration {
  340. /// Provides a `GRPCTLSConfiguration` suitable for the given `EventLoopGroup`.
  341. public static func makeClientDefault(
  342. compatibleWith eventLoopGroup: EventLoopGroup
  343. ) -> GRPCTLSConfiguration {
  344. let networkImplementation: NetworkImplementation = .matchingEventLoopGroup(eventLoopGroup)
  345. return GRPCTLSConfiguration.makeClientDefault(for: .userDefined(networkImplementation))
  346. }
  347. /// Provides a `GRPCTLSConfiguration` suitable for the given network preference.
  348. public static func makeClientDefault(
  349. for networkPreference: NetworkPreference
  350. ) -> GRPCTLSConfiguration {
  351. switch networkPreference.implementation.wrapped {
  352. case .networkFramework:
  353. #if canImport(Network)
  354. guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else {
  355. // This is gated by the availability of `.networkFramework` so should never happen.
  356. fatalError(".networkFramework is being used on an unsupported platform")
  357. }
  358. return .makeClientConfigurationBackedByNetworkFramework()
  359. #else
  360. fatalError(".networkFramework is being used on an unsupported platform")
  361. #endif
  362. case .posix:
  363. #if canImport(NIOSSL)
  364. return .makeClientConfigurationBackedByNIOSSL()
  365. #else
  366. fatalError("Default client TLS configuration for '.posix' requires NIOSSL")
  367. #endif
  368. }
  369. }
  370. }
  371. extension EventLoopGroup {
  372. internal func isCompatible(with tlsConfiguration: GRPCTLSConfiguration) -> Bool {
  373. let isTransportServicesGroup = PlatformSupport.isTransportServicesEventLoopGroup(self)
  374. let isNetworkFrameworkTLSBackend = tlsConfiguration.isNetworkFrameworkTLSBackend
  375. // If the group is from NIOTransportServices then we can use either the NIOSSL or the
  376. // Network.framework TLS backend.
  377. //
  378. // If it isn't then we must not use the Network.Framework TLS backend.
  379. return isTransportServicesGroup || !isNetworkFrameworkTLSBackend
  380. }
  381. internal func preconditionCompatible(
  382. with tlsConfiguration: GRPCTLSConfiguration,
  383. file: StaticString = #fileID,
  384. line: UInt = #line
  385. ) {
  386. precondition(
  387. self.isCompatible(with: tlsConfiguration),
  388. "Unsupported 'EventLoopGroup' and 'GRPCLSConfiguration' pairing (Network.framework backed TLS configurations MUST use an EventLoopGroup from NIOTransportServices)",
  389. file: file,
  390. line: line
  391. )
  392. }
  393. }