michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_SpdySession3_h michael@0: #define mozilla_net_SpdySession3_h michael@0: michael@0: // SPDY as defined by michael@0: // http://dev.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3 michael@0: michael@0: #include "ASpdySession.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsAHttpConnection.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsDataHashtable.h" michael@0: #include "nsDeque.h" michael@0: #include "nsHashKeys.h" michael@0: #include "zlib.h" michael@0: michael@0: class nsISocketTransport; michael@0: michael@0: namespace mozilla { namespace net { michael@0: michael@0: class SpdyPushedStream3; michael@0: class SpdyStream3; michael@0: michael@0: class SpdySession3 MOZ_FINAL : public ASpdySession michael@0: , public nsAHttpConnection michael@0: , public nsAHttpSegmentReader michael@0: , public nsAHttpSegmentWriter michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSAHTTPTRANSACTION michael@0: NS_DECL_NSAHTTPCONNECTION(mConnection) michael@0: NS_DECL_NSAHTTPSEGMENTREADER michael@0: NS_DECL_NSAHTTPSEGMENTWRITER michael@0: michael@0: SpdySession3(nsAHttpTransaction *, nsISocketTransport *, int32_t); michael@0: ~SpdySession3(); michael@0: michael@0: bool AddStream(nsAHttpTransaction *, int32_t); michael@0: bool CanReuse() { return !mShouldGoAway && !mClosed; } michael@0: bool RoomForMoreStreams(); michael@0: michael@0: // When the connection is active this is called up to once every 1 second michael@0: // return the interval (in seconds) that the connection next wants to michael@0: // have this invoked. It might happen sooner depending on the needs of michael@0: // other connections. michael@0: uint32_t ReadTimeoutTick(PRIntervalTime now); michael@0: michael@0: // Idle time represents time since "goodput".. e.g. a data or header frame michael@0: PRIntervalTime IdleTime(); michael@0: michael@0: // Registering with a newID of 0 means pick the next available odd ID michael@0: uint32_t RegisterStreamID(SpdyStream3 *, uint32_t aNewID = 0); michael@0: michael@0: const static uint8_t kVersion = 3; michael@0: michael@0: const static uint8_t kFlag_Control = 0x80; michael@0: michael@0: const static uint8_t kFlag_Data_FIN = 0x01; michael@0: const static uint8_t kFlag_Data_UNI = 0x02; michael@0: michael@0: enum michael@0: { michael@0: CONTROL_TYPE_FIRST = 0, michael@0: CONTROL_TYPE_SYN_STREAM = 1, michael@0: CONTROL_TYPE_SYN_REPLY = 2, michael@0: CONTROL_TYPE_RST_STREAM = 3, michael@0: CONTROL_TYPE_SETTINGS = 4, michael@0: CONTROL_TYPE_NOOP = 5, /* deprecated */ michael@0: CONTROL_TYPE_PING = 6, michael@0: CONTROL_TYPE_GOAWAY = 7, michael@0: CONTROL_TYPE_HEADERS = 8, michael@0: CONTROL_TYPE_WINDOW_UPDATE = 9, michael@0: CONTROL_TYPE_CREDENTIAL = 10, michael@0: CONTROL_TYPE_LAST = 11 michael@0: }; michael@0: michael@0: enum rstReason michael@0: { michael@0: RST_PROTOCOL_ERROR = 1, michael@0: RST_INVALID_STREAM = 2, michael@0: RST_REFUSED_STREAM = 3, michael@0: RST_UNSUPPORTED_VERSION = 4, michael@0: RST_CANCEL = 5, michael@0: RST_INTERNAL_ERROR = 6, michael@0: RST_FLOW_CONTROL_ERROR = 7, michael@0: RST_STREAM_IN_USE = 8, michael@0: RST_STREAM_ALREADY_CLOSED = 9, michael@0: RST_INVALID_CREDENTIALS = 10, michael@0: RST_FRAME_TOO_LARGE = 11 michael@0: }; michael@0: michael@0: enum goawayReason michael@0: { michael@0: OK = 0, michael@0: PROTOCOL_ERROR = 1, michael@0: INTERNAL_ERROR = 2, // sometimes misdocumented as 11 michael@0: NUM_STATUS_CODES = 3 // reserved by chromium but undocumented michael@0: }; michael@0: michael@0: enum settingsFlags michael@0: { michael@0: PERSIST_VALUE = 1, michael@0: PERSISTED_VALUE = 2 michael@0: }; michael@0: michael@0: enum michael@0: { michael@0: SETTINGS_TYPE_UPLOAD_BW = 1, // kb/s michael@0: SETTINGS_TYPE_DOWNLOAD_BW = 2, // kb/s michael@0: SETTINGS_TYPE_RTT = 3, // ms michael@0: SETTINGS_TYPE_MAX_CONCURRENT = 4, // streams michael@0: SETTINGS_TYPE_CWND = 5, // packets michael@0: SETTINGS_TYPE_DOWNLOAD_RETRANS_RATE = 6, // percentage michael@0: SETTINGS_TYPE_INITIAL_WINDOW = 7, // bytes for flow control michael@0: SETTINGS_CLIENT_CERTIFICATE_VECTOR_SIZE = 8 michael@0: }; michael@0: michael@0: // This should be big enough to hold all of your control packets, michael@0: // but if it needs to grow for huge headers it can do so dynamically. michael@0: // About 1% of responses from SPDY google services seem to be > 1000 michael@0: // with all less than 2000 when compression is enabled. michael@0: const static uint32_t kDefaultBufferSize = 2048; michael@0: michael@0: // kDefaultQueueSize must be >= other queue size constants michael@0: const static uint32_t kDefaultQueueSize = 32768; michael@0: const static uint32_t kQueueMinimumCleanup = 24576; michael@0: const static uint32_t kQueueTailRoom = 4096; michael@0: const static uint32_t kQueueReserved = 1024; michael@0: michael@0: const static uint32_t kDefaultMaxConcurrent = 100; michael@0: const static uint32_t kMaxStreamID = 0x7800000; michael@0: michael@0: // This is a sentinel for a deleted stream. It is not a valid michael@0: // 31 bit stream ID. michael@0: const static uint32_t kDeadStreamID = 0xffffdead; michael@0: michael@0: // below the emergency threshold of local window we ack every received michael@0: // byte. Above that we coalesce bytes into the MinimumToAck size. michael@0: const static int32_t kEmergencyWindowThreshold = 1024 * 1024; michael@0: const static uint32_t kMinimumToAck = 64 * 1024; michael@0: michael@0: // The default peer rwin is 64KB unless updated by a settings frame michael@0: const static uint32_t kDefaultServerRwin = 64 * 1024; michael@0: michael@0: static nsresult HandleSynStream(SpdySession3 *); michael@0: static nsresult HandleSynReply(SpdySession3 *); michael@0: static nsresult HandleRstStream(SpdySession3 *); michael@0: static nsresult HandleSettings(SpdySession3 *); michael@0: static nsresult HandleNoop(SpdySession3 *); michael@0: static nsresult HandlePing(SpdySession3 *); michael@0: static nsresult HandleGoAway(SpdySession3 *); michael@0: static nsresult HandleHeaders(SpdySession3 *); michael@0: static nsresult HandleWindowUpdate(SpdySession3 *); michael@0: static nsresult HandleCredential(SpdySession3 *); michael@0: michael@0: template michael@0: static void EnsureBuffer(nsAutoArrayPtr &, michael@0: uint32_t, uint32_t, uint32_t &); michael@0: michael@0: // For writing the SPDY data stream to LOG4 michael@0: static void LogIO(SpdySession3 *, SpdyStream3 *, const char *, michael@0: const char *, uint32_t); michael@0: michael@0: // an overload of nsAHttpConnection michael@0: void TransactionHasDataToWrite(nsAHttpTransaction *); michael@0: michael@0: // a similar version for SpdyStream3 michael@0: void TransactionHasDataToWrite(SpdyStream3 *); michael@0: michael@0: // an overload of nsAHttpSegementReader michael@0: virtual nsresult CommitToSegmentSize(uint32_t size, bool forceCommitment); michael@0: michael@0: uint32_t GetServerInitialWindow() { return mServerInitialWindow; } michael@0: michael@0: void ConnectPushedStream(SpdyStream3 *stream); michael@0: void DecrementConcurrent(SpdyStream3 *stream); michael@0: michael@0: uint64_t Serial() { return mSerial; } michael@0: michael@0: void PrintDiagnostics (nsCString &log); michael@0: michael@0: // Streams need access to these michael@0: uint32_t SendingChunkSize() { return mSendingChunkSize; } michael@0: uint32_t PushAllowance() { return mPushAllowance; } michael@0: z_stream *UpstreamZlib() { return &mUpstreamZlib; } michael@0: nsISocketTransport *SocketTransport() { return mSocketTransport; } michael@0: michael@0: private: michael@0: michael@0: enum stateType { michael@0: BUFFERING_FRAME_HEADER, michael@0: BUFFERING_CONTROL_FRAME, michael@0: PROCESSING_DATA_FRAME, michael@0: DISCARDING_DATA_FRAME, michael@0: PROCESSING_COMPLETE_HEADERS, michael@0: PROCESSING_CONTROL_RST_STREAM michael@0: }; michael@0: michael@0: nsresult ResponseHeadersComplete(); michael@0: uint32_t GetWriteQueueSize(); michael@0: void ChangeDownstreamState(enum stateType); michael@0: void ResetDownstreamState(); michael@0: nsresult UncompressAndDiscard(uint32_t, uint32_t); michael@0: void zlibInit(); michael@0: void GeneratePing(uint32_t); michael@0: void GenerateRstStream(uint32_t, uint32_t); michael@0: void GenerateGoAway(uint32_t); michael@0: void CleanupStream(SpdyStream3 *, nsresult, rstReason); michael@0: void CloseStream(SpdyStream3 *, nsresult); michael@0: void GenerateSettings(); michael@0: void RemoveStreamFromQueues(SpdyStream3 *); michael@0: michael@0: void SetWriteCallbacks(); michael@0: void FlushOutputQueue(); michael@0: void RealignOutputQueue(); michael@0: michael@0: bool RoomForMoreConcurrent(); michael@0: void ActivateStream(SpdyStream3 *); michael@0: void ProcessPending(); michael@0: nsresult SetInputFrameDataStream(uint32_t); michael@0: bool VerifyStream(SpdyStream3 *, uint32_t); michael@0: void SetNeedsCleanup(); michael@0: michael@0: void UpdateLocalRwin(SpdyStream3 *stream, uint32_t bytes); michael@0: michael@0: // a wrapper for all calls to the nshttpconnection level segment writer. Used michael@0: // to track network I/O for timeout purposes michael@0: nsresult NetworkRead(nsAHttpSegmentWriter *, char *, uint32_t, uint32_t *); michael@0: michael@0: static PLDHashOperator ShutdownEnumerator(nsAHttpTransaction *, michael@0: nsAutoPtr &, michael@0: void *); michael@0: michael@0: static PLDHashOperator GoAwayEnumerator(nsAHttpTransaction *, michael@0: nsAutoPtr &, michael@0: void *); michael@0: michael@0: static PLDHashOperator UpdateServerRwinEnumerator(nsAHttpTransaction *, michael@0: nsAutoPtr &, michael@0: void *); michael@0: michael@0: // This is intended to be nsHttpConnectionMgr:nsConnectionHandle taken michael@0: // from the first transaction on this session. That object contains the michael@0: // pointer to the real network-level nsHttpConnection object. michael@0: nsRefPtr mConnection; michael@0: michael@0: // The underlying socket transport object is needed to propogate some events michael@0: nsISocketTransport *mSocketTransport; michael@0: michael@0: // These are temporary state variables to hold the argument to michael@0: // Read/WriteSegments so it can be accessed by On(read/write)segment michael@0: // further up the stack. michael@0: nsAHttpSegmentReader *mSegmentReader; michael@0: nsAHttpSegmentWriter *mSegmentWriter; michael@0: michael@0: uint32_t mSendingChunkSize; /* the transmission chunk size */ michael@0: uint32_t mNextStreamID; /* 24 bits */ michael@0: uint32_t mConcurrentHighWater; /* max parallelism on session */ michael@0: uint32_t mPushAllowance; /* rwin for unmatched pushes */ michael@0: michael@0: stateType mDownstreamState; /* in frame, between frames, etc.. */ michael@0: michael@0: // Maintain 2 indexes - one by stream ID, one by transaction pointer. michael@0: // There are also several lists of streams: ready to write, queued due to michael@0: // max parallelism, streams that need to force a read for push, and the full michael@0: // set of pushed streams. michael@0: // The objects are not ref counted - they get destroyed michael@0: // by the nsClassHashtable implementation when they are removed from michael@0: // the transaction hash. michael@0: nsDataHashtable mStreamIDHash; michael@0: nsClassHashtable, michael@0: SpdyStream3> mStreamTransactionHash; michael@0: michael@0: nsDeque mReadyForWrite; michael@0: nsDeque mQueuedStreams; michael@0: nsDeque mReadyForRead; michael@0: nsTArray mPushedStreams; michael@0: michael@0: // Compression contexts for header transport using deflate. michael@0: // SPDY compresses only HTTP headers and does not reset zlib in between michael@0: // frames. Even data that is not associated with a stream (e.g invalid michael@0: // stream ID) is passed through these contexts to keep the compression michael@0: // context correct. michael@0: z_stream mDownstreamZlib; michael@0: z_stream mUpstreamZlib; michael@0: michael@0: // mInputFrameBuffer is used to store received control packets and the 8 bytes michael@0: // of header on data packets michael@0: uint32_t mInputFrameBufferSize; michael@0: uint32_t mInputFrameBufferUsed; michael@0: nsAutoArrayPtr mInputFrameBuffer; michael@0: michael@0: // mInputFrameDataSize/Read are used for tracking the amount of data consumed michael@0: // in a data frame. the data itself is not buffered in spdy michael@0: // The frame size is mInputFrameDataSize + the constant 8 byte header michael@0: uint32_t mInputFrameDataSize; michael@0: uint32_t mInputFrameDataRead; michael@0: bool mInputFrameDataLast; // This frame was marked FIN michael@0: michael@0: // When a frame has been received that is addressed to a particular stream michael@0: // (e.g. a data frame after the stream-id has been decoded), this points michael@0: // to the stream. michael@0: SpdyStream3 *mInputFrameDataStream; michael@0: michael@0: // mNeedsCleanup is a state variable to defer cleanup of a closed stream michael@0: // If needed, It is set in session::OnWriteSegments() and acted on and michael@0: // cleared when the stack returns to session::WriteSegments(). The stream michael@0: // cannot be destroyed directly out of OnWriteSegments because michael@0: // stream::writeSegments() is on the stack at that time. michael@0: SpdyStream3 *mNeedsCleanup; michael@0: michael@0: // The CONTROL_TYPE value for a control frame michael@0: uint32_t mFrameControlType; michael@0: michael@0: // This reason code in the last processed RESET frame michael@0: uint32_t mDownstreamRstReason; michael@0: michael@0: // for the conversion of downstream http headers into spdy formatted headers michael@0: // The data here does not persist between frames michael@0: nsCString mFlatHTTPResponseHeaders; michael@0: uint32_t mFlatHTTPResponseHeadersOut; michael@0: michael@0: // when set, the session will go away when it reaches 0 streams. This flag michael@0: // is set when: the stream IDs are running out (at either the client or the michael@0: // server), when DontReuse() is called, a RST that is not specific to a michael@0: // particular stream is received, a GOAWAY frame has been received from michael@0: // the server. michael@0: bool mShouldGoAway; michael@0: michael@0: // the session has received a nsAHttpTransaction::Close() call michael@0: bool mClosed; michael@0: michael@0: // the session received a GoAway frame with a valid GoAwayID michael@0: bool mCleanShutdown; michael@0: michael@0: // indicates PROCESSING_COMPLETE_HEADERS state was pushed onto the stack michael@0: // over an active PROCESSING_DATA_FRAME, which should be restored when michael@0: // the processed headers are written to the stream michael@0: bool mDataPending; michael@0: michael@0: // If a GoAway message was received this is the ID of the last valid michael@0: // stream. 0 otherwise. (0 is never a valid stream id.) michael@0: uint32_t mGoAwayID; michael@0: michael@0: // The limit on number of concurrent streams for this session. Normally it michael@0: // is basically unlimited, but the SETTINGS control message from the michael@0: // server might bring it down. michael@0: uint32_t mMaxConcurrent; michael@0: michael@0: // The actual number of concurrent streams at this moment. Generally below michael@0: // mMaxConcurrent, but the max can be lowered in real time to a value michael@0: // below the current value michael@0: uint32_t mConcurrent; michael@0: michael@0: // The number of server initiated SYN-STREAMS, tracked for telemetry michael@0: uint32_t mServerPushedResources; michael@0: michael@0: // The server rwin for new streams as determined from a SETTINGS frame michael@0: uint32_t mServerInitialWindow; michael@0: michael@0: // This is a output queue of bytes ready to be written to the SSL stream. michael@0: // When that streams returns WOULD_BLOCK on direct write the bytes get michael@0: // coalesced together here. This results in larger writes to the SSL layer. michael@0: // The buffer is not dynamically grown to accomodate stream writes, but michael@0: // does expand to accept infallible session wide frames like GoAway and RST. michael@0: uint32_t mOutputQueueSize; michael@0: uint32_t mOutputQueueUsed; michael@0: uint32_t mOutputQueueSent; michael@0: nsAutoArrayPtr mOutputQueueBuffer; michael@0: michael@0: PRIntervalTime mPingThreshold; michael@0: PRIntervalTime mLastReadEpoch; // used for ping timeouts michael@0: PRIntervalTime mLastDataReadEpoch; // used for IdleTime() michael@0: PRIntervalTime mPingSentEpoch; michael@0: uint32_t mNextPingID; michael@0: michael@0: // used as a temporary buffer while enumerating the stream hash during GoAway michael@0: nsDeque mGoAwayStreamsToRestart; michael@0: michael@0: // Each session gets a unique serial number because the push cache is correlated michael@0: // by the load group and the serial number can be used as part of the cache key michael@0: // to make sure streams aren't shared across sessions. michael@0: uint64_t mSerial; michael@0: }; michael@0: michael@0: }} // namespace mozilla::net michael@0: michael@0: #endif // mozilla_net_SpdySession3_h