1 /* vim:set ts=4 sw=2 sts=2 et cin: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef ConnectionEntry_h__
7 #define ConnectionEntry_h__
8 
9 #include "PendingTransactionInfo.h"
10 #include "PendingTransactionQueue.h"
11 #include "DnsAndConnectSocket.h"
12 #include "DashboardTypes.h"
13 
14 namespace mozilla {
15 namespace net {
16 
17 // ConnectionEntry
18 //
19 // nsHttpConnectionMgr::mCT maps connection info hash key to ConnectionEntry
20 // object, which contains list of active and idle connections as well as the
21 // list of pending transactions.
22 class ConnectionEntry {
23  public:
24   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ConnectionEntry)
25   explicit ConnectionEntry(nsHttpConnectionInfo* ci);
26 
27   void ReschedTransaction(nsHttpTransaction* aTrans);
28 
29   nsTArray<RefPtr<PendingTransactionInfo>>* GetTransactionPendingQHelper(
30       nsAHttpTransaction* trans);
31 
32   void InsertTransactionSorted(
33       nsTArray<RefPtr<PendingTransactionInfo>>& pendingQ,
34       PendingTransactionInfo* pendingTransInfo,
35       bool aInsertAsFirstForTheSamePriority = false);
36 
37   void InsertTransaction(PendingTransactionInfo* aPendingTransInfo,
38                          bool aInsertAsFirstForTheSamePriority = false);
39 
40   size_t UrgentStartQueueLength();
41 
42   void PrintPendingQ();
43 
44   void Compact();
45 
46   void CancelAllTransactions(nsresult reason);
47 
48   nsresult CloseIdleConnection(nsHttpConnection* conn);
49   void CloseIdleConnections();
50   void CloseIdleConnections(uint32_t maxToClose);
51   nsresult RemoveIdleConnection(nsHttpConnection* conn);
52   bool IsInIdleConnections(HttpConnectionBase* conn);
IdleConnectionsLength()53   size_t IdleConnectionsLength() const { return mIdleConns.Length(); }
54   void InsertIntoIdleConnections(nsHttpConnection* conn);
55   already_AddRefed<nsHttpConnection> GetIdleConnection(bool respectUrgency,
56                                                        bool urgentTrans,
57                                                        bool* onlyUrgent);
58 
ActiveConnsLength()59   size_t ActiveConnsLength() const { return mActiveConns.Length(); }
60   void InsertIntoActiveConns(HttpConnectionBase* conn);
61   bool IsInActiveConns(HttpConnectionBase* conn);
62   nsresult RemoveActiveConnection(HttpConnectionBase* conn);
63   void MakeAllDontReuseExcept(HttpConnectionBase* conn);
64   bool FindConnToClaim(PendingTransactionInfo* pendingTransInfo);
65   void CloseActiveConnections();
66   void CloseAllActiveConnsWithNullTransactcion(nsresult aCloseCode);
67 
68   HttpConnectionBase* GetH2orH3ActiveConn();
69   // Make an active spdy connection DontReuse.
70   // TODO: this is a helper function and should nbe improved.
71   bool MakeFirstActiveSpdyConnDontReuse();
72 
73   void ClosePersistentConnections();
74 
75   uint32_t PruneDeadConnections();
76   void VerifyTraffic();
77   void PruneNoTraffic();
78   uint32_t TimeoutTick();
79 
80   void MoveConnection(HttpConnectionBase* proxyConn, ConnectionEntry* otherEnt);
81 
DnsAndConnectSocketsLength()82   size_t DnsAndConnectSocketsLength() const {
83     return mDnsAndConnectSockets.Length();
84   }
85 
86   void InsertIntoDnsAndConnectSockets(DnsAndConnectSocket* sock);
87   void RemoveDnsAndConnectSocket(DnsAndConnectSocket* dnsAndSock, bool abandon);
88   void CloseAllDnsAndConnectSockets();
89 
90   HttpRetParams GetConnectionData();
91   void LogConnections();
92 
93   RefPtr<nsHttpConnectionInfo> mConnInfo;
94 
95   bool AvailableForDispatchNow();
96 
97   // calculate the number of half open sockets that have not had at least 1
98   // connection complete
99   uint32_t UnconnectedDnsAndConnectSockets() const;
100 
101   // Remove a particular DnsAndConnectSocket from the mDnsAndConnectSocket array
102   bool RemoveDnsAndConnectSocket(DnsAndConnectSocket*);
103 
104   bool MaybeProcessCoalescingKeys(nsIDNSAddrRecord* dnsRecord,
105                                   bool aIsHttp3 = false);
106 
107   nsresult CreateDnsAndConnectSocket(nsAHttpTransaction* trans, uint32_t caps,
108                                      bool speculative, bool isFromPredictor,
109                                      bool urgentStart, bool allow1918,
110                                      PendingTransactionInfo* pendingTransInfo);
111 
112   // Spdy sometimes resolves the address in the socket manager in order
113   // to re-coalesce sharded HTTP hosts. The dotted decimal address is
114   // combined with the Anonymous flag and OA from the connection information
115   // to build the hash key for hosts in the same ip pool.
116   //
117   nsTArray<nsCString> mCoalescingKeys;
118 
119   // To have the UsingSpdy flag means some host with the same connection
120   // entry has done NPN=spdy/* at some point. It does not mean every
121   // connection is currently using spdy.
122   bool mUsingSpdy : 1;
123 
124   // Determines whether or not we can continue to use spdy-enabled
125   // connections in the future. This is generally set to false either when
126   // some connection here encounters connection-based auth (such as NTLM)
127   // or when some connection here encounters a server that is totally
128   // busted (such as it fails to properly perform the h2 handshake).
129   bool mCanUseSpdy : 1;
130 
131   // Flags to remember our happy-eyeballs decision.
132   // Reset only by Ctrl-F5 reload.
133   // True when we've first connected an IPv4 server for this host,
134   // initially false.
135   bool mPreferIPv4 : 1;
136   // True when we've first connected an IPv6 server for this host,
137   // initially false.
138   bool mPreferIPv6 : 1;
139 
140   // True if this connection entry has initiated a socket
141   bool mUsedForConnection : 1;
142 
143   bool mDoNotDestroy : 1;
144 
IsHttp3()145   bool IsHttp3() const { return mConnInfo->IsHttp3(); }
AllowHttp2()146   bool AllowHttp2() const { return mCanUseSpdy; }
147   void DisallowHttp2();
148   void DontReuseHttp3Conn();
149 
150   // Set the IP family preference flags according the connected family
151   void RecordIPFamilyPreference(uint16_t family);
152   // Resets all flags to their default values
153   void ResetIPFamilyPreference();
154   // True iff there is currently an established IP family preference
155   bool PreferenceKnown() const;
156 
157   // Return the count of pending transactions for all window ids.
158   size_t PendingQueueLength() const;
159   size_t PendingQueueLengthForWindow(uint64_t windowId) const;
160 
161   void AppendPendingUrgentStartQ(
162       nsTArray<RefPtr<PendingTransactionInfo>>& result);
163 
164   // Append transactions to the |result| whose window id
165   // is equal to |windowId|.
166   // NOTE: maxCount == 0 will get all transactions in the queue.
167   void AppendPendingQForFocusedWindow(
168       uint64_t windowId, nsTArray<RefPtr<PendingTransactionInfo>>& result,
169       uint32_t maxCount = 0);
170 
171   // Append transactions whose window id isn't equal to |windowId|.
172   // NOTE: windowId == 0 will get all transactions for both
173   // focused and non-focused windows.
174   void AppendPendingQForNonFocusedWindows(
175       uint64_t windowId, nsTArray<RefPtr<PendingTransactionInfo>>& result,
176       uint32_t maxCount = 0);
177 
178   // Remove the empty pendingQ in |mPendingTransactionTable|.
179   void RemoveEmptyPendingQ();
180 
181   void PrintDiagnostics(nsCString& log, uint32_t aMaxPersistConns);
182 
183   bool RestrictConnections();
184 
185   // Return total active connection count, which is the sum of
186   // active connections and unconnected half open connections.
187   uint32_t TotalActiveConnections() const;
188 
189   bool RemoveTransFromPendingQ(nsHttpTransaction* aTrans);
190 
191   void MaybeUpdateEchConfig(nsHttpConnectionInfo* aConnInfo);
192 
193  private:
194   void InsertIntoIdleConnections_internal(nsHttpConnection* conn);
195   void RemoveFromIdleConnectionsIndex(size_t inx);
196   bool RemoveFromIdleConnections(nsHttpConnection* conn);
197 
198   nsTArray<RefPtr<nsHttpConnection>> mIdleConns;  // idle persistent connections
199   nsTArray<RefPtr<HttpConnectionBase>> mActiveConns;  // active connections
200 
201   nsTArray<RefPtr<DnsAndConnectSocket>>
202       mDnsAndConnectSockets;  // dns resolution and half open connections
203 
204   PendingTransactionQueue mPendingQ;
205   ~ConnectionEntry();
206 };
207 
208 }  // namespace net
209 }  // namespace mozilla
210 
211 #endif  // !ConnectionEntry_h__
212