08ba3284a028bc4c3c8acef61e0270f64197c15f
[folly.git] / folly / io / async / AsyncTransport.h
1 /*
2  * Copyright 2016 Facebook, Inc.
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
17 #pragma once
18
19 #include <memory>
20 #include <sys/uio.h>
21
22 #include <folly/io/IOBuf.h>
23 #include <folly/io/async/DelayedDestruction.h>
24 #include <folly/io/async/EventBase.h>
25 #include <folly/io/async/AsyncSocketBase.h>
26 #include <folly/io/async/OpenSSLPtrTypes.h>
27
28 #include <openssl/ssl.h>
29
30 constexpr bool kOpenSslModeMoveBufferOwnership =
31 #ifdef SSL_MODE_MOVE_BUFFER_OWNERSHIP
32   true
33 #else
34   false
35 #endif
36 ;
37
38 namespace folly {
39
40 class AsyncSocketException;
41 class EventBase;
42 class SocketAddress;
43
44 /*
45  * flags given by the application for write* calls
46  */
47 enum class WriteFlags : uint32_t {
48   NONE = 0x00,
49   /*
50    * Whether to delay the output until a subsequent non-corked write.
51    * (Note: may not be supported in all subclasses or on all platforms.)
52    */
53   CORK = 0x01,
54   /*
55    * for a socket that has ACK latency enabled, it will cause the kernel
56    * to fire a TCP ESTATS event when the last byte of the given write call
57    * will be acknowledged.
58    */
59   EOR = 0x02,
60   /*
61    * this indicates that only the write side of socket should be shutdown
62    */
63   WRITE_SHUTDOWN = 0x04,
64 };
65
66 /*
67  * union operator
68  */
69 inline WriteFlags operator|(WriteFlags a, WriteFlags b) {
70   return static_cast<WriteFlags>(
71     static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
72 }
73
74 /*
75  * intersection operator
76  */
77 inline WriteFlags operator&(WriteFlags a, WriteFlags b) {
78   return static_cast<WriteFlags>(
79     static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
80 }
81
82 /*
83  * exclusion parameter
84  */
85 inline WriteFlags operator~(WriteFlags a) {
86   return static_cast<WriteFlags>(~static_cast<uint32_t>(a));
87 }
88
89 /*
90  * unset operator
91  */
92 inline WriteFlags unSet(WriteFlags a, WriteFlags b) {
93   return a & ~b;
94 }
95
96 /*
97  * inclusion operator
98  */
99 inline bool isSet(WriteFlags a, WriteFlags b) {
100   return (a & b) == b;
101 }
102
103
104 /**
105  * AsyncTransport defines an asynchronous API for streaming I/O.
106  *
107  * This class provides an API to for asynchronously waiting for data
108  * on a streaming transport, and for asynchronously sending data.
109  *
110  * The APIs for reading and writing are intentionally asymmetric.  Waiting for
111  * data to read is a persistent API: a callback is installed, and is notified
112  * whenever new data is available.  It continues to be notified of new events
113  * until it is uninstalled.
114  *
115  * AsyncTransport does not provide read timeout functionality, because it
116  * typically cannot determine when the timeout should be active.  Generally, a
117  * timeout should only be enabled when processing is blocked waiting on data
118  * from the remote endpoint.  For server-side applications, the timeout should
119  * not be active if the server is currently processing one or more outstanding
120  * requests on this transport.  For client-side applications, the timeout
121  * should not be active if there are no requests pending on the transport.
122  * Additionally, if a client has multiple pending requests, it will ususally
123  * want a separate timeout for each request, rather than a single read timeout.
124  *
125  * The write API is fairly intuitive: a user can request to send a block of
126  * data, and a callback will be informed once the entire block has been
127  * transferred to the kernel, or on error.  AsyncTransport does provide a send
128  * timeout, since most callers want to give up if the remote end stops
129  * responding and no further progress can be made sending the data.
130  */
131 class AsyncTransport : public DelayedDestruction, public AsyncSocketBase {
132  public:
133   typedef std::unique_ptr<AsyncTransport, Destructor> UniquePtr;
134
135   /**
136    * Close the transport.
137    *
138    * This gracefully closes the transport, waiting for all pending write
139    * requests to complete before actually closing the underlying transport.
140    *
141    * If a read callback is set, readEOF() will be called immediately.  If there
142    * are outstanding write requests, the close will be delayed until all
143    * remaining writes have completed.  No new writes may be started after
144    * close() has been called.
145    */
146   virtual void close() = 0;
147
148   /**
149    * Close the transport immediately.
150    *
151    * This closes the transport immediately, dropping any outstanding data
152    * waiting to be written.
153    *
154    * If a read callback is set, readEOF() will be called immediately.
155    * If there are outstanding write requests, these requests will be aborted
156    * and writeError() will be invoked immediately on all outstanding write
157    * callbacks.
158    */
159   virtual void closeNow() = 0;
160
161   /**
162    * Reset the transport immediately.
163    *
164    * This closes the transport immediately, sending a reset to the remote peer
165    * if possible to indicate abnormal shutdown.
166    *
167    * Note that not all subclasses implement this reset functionality: some
168    * subclasses may treat reset() the same as closeNow().  Subclasses that use
169    * TCP transports should terminate the connection with a TCP reset.
170    */
171   virtual void closeWithReset() {
172     closeNow();
173   }
174
175   /**
176    * Perform a half-shutdown of the write side of the transport.
177    *
178    * The caller should not make any more calls to write() or writev() after
179    * shutdownWrite() is called.  Any future write attempts will fail
180    * immediately.
181    *
182    * Not all transport types support half-shutdown.  If the underlying
183    * transport does not support half-shutdown, it will fully shutdown both the
184    * read and write sides of the transport.  (Fully shutting down the socket is
185    * better than doing nothing at all, since the caller may rely on the
186    * shutdownWrite() call to notify the other end of the connection that no
187    * more data can be read.)
188    *
189    * If there is pending data still waiting to be written on the transport,
190    * the actual shutdown will be delayed until the pending data has been
191    * written.
192    *
193    * Note: There is no corresponding shutdownRead() equivalent.  Simply
194    * uninstall the read callback if you wish to stop reading.  (On TCP sockets
195    * at least, shutting down the read side of the socket is a no-op anyway.)
196    */
197   virtual void shutdownWrite() = 0;
198
199   /**
200    * Perform a half-shutdown of the write side of the transport.
201    *
202    * shutdownWriteNow() is identical to shutdownWrite(), except that it
203    * immediately performs the shutdown, rather than waiting for pending writes
204    * to complete.  Any pending write requests will be immediately failed when
205    * shutdownWriteNow() is called.
206    */
207   virtual void shutdownWriteNow() = 0;
208
209   /**
210    * Determine if transport is open and ready to read or write.
211    *
212    * Note that this function returns false on EOF; you must also call error()
213    * to distinguish between an EOF and an error.
214    *
215    * @return  true iff the transport is open and ready, false otherwise.
216    */
217   virtual bool good() const = 0;
218
219   /**
220    * Determine if the transport is readable or not.
221    *
222    * @return  true iff the transport is readable, false otherwise.
223    */
224   virtual bool readable() const = 0;
225
226   /**
227    * Determine if the there is pending data on the transport.
228    *
229    * @return  true iff the if the there is pending data, false otherwise.
230    */
231   virtual bool isPending() const {
232     return readable();
233   }
234
235   /**
236    * Determine if transport is connected to the endpoint
237    *
238    * @return  false iff the transport is connected, otherwise true
239    */
240   virtual bool connecting() const = 0;
241
242   /**
243    * Determine if an error has occurred with this transport.
244    *
245    * @return  true iff an error has occurred (not EOF).
246    */
247   virtual bool error() const = 0;
248
249   /**
250    * Attach the transport to a EventBase.
251    *
252    * This may only be called if the transport is not currently attached to a
253    * EventBase (by an earlier call to detachEventBase()).
254    *
255    * This method must be invoked in the EventBase's thread.
256    */
257   virtual void attachEventBase(EventBase* eventBase) = 0;
258
259   /**
260    * Detach the transport from its EventBase.
261    *
262    * This may only be called when the transport is idle and has no reads or
263    * writes pending.  Once detached, the transport may not be used again until
264    * it is re-attached to a EventBase by calling attachEventBase().
265    *
266    * This method must be called from the current EventBase's thread.
267    */
268   virtual void detachEventBase() = 0;
269
270   /**
271    * Determine if the transport can be detached.
272    *
273    * This method must be called from the current EventBase's thread.
274    */
275   virtual bool isDetachable() const = 0;
276
277   /**
278    * Set the send timeout.
279    *
280    * If write requests do not make any progress for more than the specified
281    * number of milliseconds, fail all pending writes and close the transport.
282    *
283    * If write requests are currently pending when setSendTimeout() is called,
284    * the timeout interval is immediately restarted using the new value.
285    *
286    * @param milliseconds  The timeout duration, in milliseconds.  If 0, no
287    *                      timeout will be used.
288    */
289   virtual void setSendTimeout(uint32_t milliseconds) = 0;
290
291   /**
292    * Get the send timeout.
293    *
294    * @return Returns the current send timeout, in milliseconds.  A return value
295    *         of 0 indicates that no timeout is set.
296    */
297   virtual uint32_t getSendTimeout() const = 0;
298
299   /**
300    * Get the address of the local endpoint of this transport.
301    *
302    * This function may throw AsyncSocketException on error.
303    *
304    * @param address  The local address will be stored in the specified
305    *                 SocketAddress.
306    */
307   virtual void getLocalAddress(SocketAddress* address) const = 0;
308
309   virtual void getAddress(SocketAddress* address) const {
310     getLocalAddress(address);
311   }
312
313   /**
314    * Get the address of the remote endpoint to which this transport is
315    * connected.
316    *
317    * This function may throw AsyncSocketException on error.
318    *
319    * @param address  The remote endpoint's address will be stored in the
320    *                 specified SocketAddress.
321    */
322   virtual void getPeerAddress(SocketAddress* address) const = 0;
323
324   /**
325    * Get the certificate used to authenticate the peer.
326    */
327   virtual X509_UniquePtr getPeerCert() const { return nullptr; }
328
329   /**
330    * @return True iff end of record tracking is enabled
331    */
332   virtual bool isEorTrackingEnabled() const = 0;
333
334   virtual void setEorTracking(bool track) = 0;
335
336   virtual size_t getAppBytesWritten() const = 0;
337   virtual size_t getRawBytesWritten() const = 0;
338   virtual size_t getAppBytesReceived() const = 0;
339   virtual size_t getRawBytesReceived() const = 0;
340
341   class BufferCallback {
342    public:
343     virtual ~BufferCallback() {}
344     virtual void onEgressBuffered() = 0;
345     virtual void onEgressBufferCleared() = 0;
346   };
347
348   /**
349    * Callback class to signal when a transport that did not have replay
350    * protection gains replay protection. This is needed for 0-RTT security
351    * protocols.
352    */
353   class ReplaySafetyCallback {
354    public:
355     virtual ~ReplaySafetyCallback() = default;
356
357     /**
358      * Called when the transport becomes replay safe.
359      */
360     virtual void onReplaySafe() = 0;
361   };
362
363   /**
364    * False if the transport does not have replay protection, but will in the
365    * future.
366    */
367   virtual bool isReplaySafe() const { return true; }
368
369   /**
370    * Set the ReplaySafeCallback on this transport.
371    *
372    * This should only be called if isReplaySafe() returns false.
373    */
374   virtual void setReplaySafetyCallback(ReplaySafetyCallback* callback) {
375     if (callback) {
376       CHECK(false) << "setReplaySafetyCallback() not supported";
377     }
378   }
379
380  protected:
381   virtual ~AsyncTransport() = default;
382 };
383
384 class AsyncReader {
385  public:
386   class ReadCallback {
387    public:
388     virtual ~ReadCallback() = default;
389
390     /**
391      * When data becomes available, getReadBuffer() will be invoked to get the
392      * buffer into which data should be read.
393      *
394      * This method allows the ReadCallback to delay buffer allocation until
395      * data becomes available.  This allows applications to manage large
396      * numbers of idle connections, without having to maintain a separate read
397      * buffer for each idle connection.
398      *
399      * It is possible that in some cases, getReadBuffer() may be called
400      * multiple times before readDataAvailable() is invoked.  In this case, the
401      * data will be written to the buffer returned from the most recent call to
402      * readDataAvailable().  If the previous calls to readDataAvailable()
403      * returned different buffers, the ReadCallback is responsible for ensuring
404      * that they are not leaked.
405      *
406      * If getReadBuffer() throws an exception, returns a nullptr buffer, or
407      * returns a 0 length, the ReadCallback will be uninstalled and its
408      * readError() method will be invoked.
409      *
410      * getReadBuffer() is not allowed to change the transport state before it
411      * returns.  (For example, it should never uninstall the read callback, or
412      * set a different read callback.)
413      *
414      * @param bufReturn getReadBuffer() should update *bufReturn to contain the
415      *                  address of the read buffer.  This parameter will never
416      *                  be nullptr.
417      * @param lenReturn getReadBuffer() should update *lenReturn to contain the
418      *                  maximum number of bytes that may be written to the read
419      *                  buffer.  This parameter will never be nullptr.
420      */
421     virtual void getReadBuffer(void** bufReturn, size_t* lenReturn) = 0;
422
423     /**
424      * readDataAvailable() will be invoked when data has been successfully read
425      * into the buffer returned by the last call to getReadBuffer().
426      *
427      * The read callback remains installed after readDataAvailable() returns.
428      * It must be explicitly uninstalled to stop receiving read events.
429      * getReadBuffer() will be called at least once before each call to
430      * readDataAvailable().  getReadBuffer() will also be called before any
431      * call to readEOF().
432      *
433      * @param len       The number of bytes placed in the buffer.
434      */
435
436     virtual void readDataAvailable(size_t len) noexcept = 0;
437
438     /**
439      * When data becomes available, isBufferMovable() will be invoked to figure
440      * out which API will be used, readBufferAvailable() or
441      * readDataAvailable(). If isBufferMovable() returns true, that means
442      * ReadCallback supports the IOBuf ownership transfer and
443      * readBufferAvailable() will be used.  Otherwise, not.
444
445      * By default, isBufferMovable() always return false. If
446      * readBufferAvailable() is implemented and to be invoked, You should
447      * overwrite isBufferMovable() and return true in the inherited class.
448      *
449      * This method allows the AsyncSocket/AsyncSSLSocket do buffer allocation by
450      * itself until data becomes available.  Compared with the pre/post buffer
451      * allocation in getReadBuffer()/readDataAvailabe(), readBufferAvailable()
452      * has two advantages.  First, this can avoid memcpy. E.g., in
453      * AsyncSSLSocket, the decrypted data was copied from the openssl internal
454      * buffer to the readbuf buffer.  With the buffer ownership transfer, the
455      * internal buffer can be directly "moved" to ReadCallback. Second, the
456      * memory allocation can be more precise.  The reason is
457      * AsyncSocket/AsyncSSLSocket can allocate the memory of precise size
458      * because they have more context about the available data than
459      * ReadCallback.  Think about the getReadBuffer() pre-allocate 4072 bytes
460      * buffer, but the available data is always 16KB (max OpenSSL record size).
461      */
462
463     virtual bool isBufferMovable() noexcept {
464       return false;
465     }
466
467     /**
468      * readBufferAvailable() will be invoked when data has been successfully
469      * read.
470      *
471      * Note that only either readBufferAvailable() or readDataAvailable() will
472      * be invoked according to the return value of isBufferMovable(). The timing
473      * and aftereffect of readBufferAvailable() are the same as
474      * readDataAvailable()
475      *
476      * @param readBuf The unique pointer of read buffer.
477      */
478
479     virtual void readBufferAvailable(std::unique_ptr<IOBuf> /*readBuf*/)
480       noexcept {};
481
482     /**
483      * readEOF() will be invoked when the transport is closed.
484      *
485      * The read callback will be automatically uninstalled immediately before
486      * readEOF() is invoked.
487      */
488     virtual void readEOF() noexcept = 0;
489
490     /**
491      * readError() will be invoked if an error occurs reading from the
492      * transport.
493      *
494      * The read callback will be automatically uninstalled immediately before
495      * readError() is invoked.
496      *
497      * @param ex        An exception describing the error that occurred.
498      */
499     virtual void readErr(const AsyncSocketException& ex) noexcept = 0;
500   };
501
502   // Read methods that aren't part of AsyncTransport.
503   virtual void setReadCB(ReadCallback* callback) = 0;
504   virtual ReadCallback* getReadCallback() const = 0;
505
506  protected:
507   virtual ~AsyncReader() = default;
508 };
509
510 class AsyncWriter {
511  public:
512   class WriteCallback {
513    public:
514     virtual ~WriteCallback() = default;
515
516     /**
517      * writeSuccess() will be invoked when all of the data has been
518      * successfully written.
519      *
520      * Note that this mainly signals that the buffer containing the data to
521      * write is no longer needed and may be freed or re-used.  It does not
522      * guarantee that the data has been fully transmitted to the remote
523      * endpoint.  For example, on socket-based transports, writeSuccess() only
524      * indicates that the data has been given to the kernel for eventual
525      * transmission.
526      */
527     virtual void writeSuccess() noexcept = 0;
528
529     /**
530      * writeError() will be invoked if an error occurs writing the data.
531      *
532      * @param bytesWritten      The number of bytes that were successfull
533      * @param ex                An exception describing the error that occurred.
534      */
535     virtual void writeErr(size_t bytesWritten,
536                           const AsyncSocketException& ex) noexcept = 0;
537   };
538
539   // Write methods that aren't part of AsyncTransport
540   virtual void write(WriteCallback* callback, const void* buf, size_t bytes,
541                      WriteFlags flags = WriteFlags::NONE) = 0;
542   virtual void writev(WriteCallback* callback, const iovec* vec, size_t count,
543                       WriteFlags flags = WriteFlags::NONE) = 0;
544   virtual void writeChain(WriteCallback* callback,
545                           std::unique_ptr<IOBuf>&& buf,
546                           WriteFlags flags = WriteFlags::NONE) = 0;
547
548  protected:
549   virtual ~AsyncWriter() = default;
550 };
551
552 // Transitional intermediate interface. This is deprecated.
553 // Wrapper around folly::AsyncTransport, that includes read/write callbacks
554 class AsyncTransportWrapper : virtual public AsyncTransport,
555                               virtual public AsyncReader,
556                               virtual public AsyncWriter {
557  public:
558   using UniquePtr = std::unique_ptr<AsyncTransportWrapper, Destructor>;
559
560   // Alias for inherited members from AsyncReader and AsyncWriter
561   // to keep compatibility.
562   using ReadCallback    = AsyncReader::ReadCallback;
563   using WriteCallback   = AsyncWriter::WriteCallback;
564   virtual void setReadCB(ReadCallback* callback) override = 0;
565   virtual ReadCallback* getReadCallback() const override = 0;
566   virtual void write(WriteCallback* callback, const void* buf, size_t bytes,
567                      WriteFlags flags = WriteFlags::NONE) override = 0;
568   virtual void writev(WriteCallback* callback, const iovec* vec, size_t count,
569                       WriteFlags flags = WriteFlags::NONE) override = 0;
570   virtual void writeChain(WriteCallback* callback,
571                           std::unique_ptr<IOBuf>&& buf,
572                           WriteFlags flags = WriteFlags::NONE) override = 0;
573   /**
574    * The transport wrapper may wrap another transport. This returns the
575    * transport that is wrapped. It returns nullptr if there is no wrapped
576    * transport.
577    */
578   virtual const AsyncTransportWrapper* getWrappedTransport() const {
579     return nullptr;
580   }
581
582   /**
583    * In many cases when we need to set socket properties or otherwise access the
584    * underlying transport from a wrapped transport. This method allows access to
585    * the derived classes of the underlying transport.
586    */
587   template <class T>
588   const T* getUnderlyingTransport() const {
589     const AsyncTransportWrapper* current = this;
590     while (current) {
591       auto sock = dynamic_cast<const T*>(current);
592       if (sock) {
593         return sock;
594       }
595       current = current->getWrappedTransport();
596     }
597     return nullptr;
598   }
599
600   template <class T>
601   T* getUnderlyingTransport() {
602     return const_cast<T*>(static_cast<const AsyncTransportWrapper*>(this)
603         ->getUnderlyingTransport<T>());
604   }
605
606   /**
607    * Return the application protocol being used by the underlying transport
608    * protocol. This is useful for transports which are used to tunnel other
609    * protocols.
610    */
611   virtual std::string getApplicationProtocol() noexcept {
612     return "";
613   }
614
615   /**
616    * Returns the name of the security protocol being used.
617    */
618   virtual std::string getSecurityProtocol() const { return ""; }
619 };
620
621 } // folly