/src/trafficserver/include/proxy/http2/Http2CommonSession.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | Http2CommonSession.h |
4 | | |
5 | | @par License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <unordered_set> |
27 | | |
28 | | #include "proxy/http2/HTTP2.h" |
29 | | #include "proxy/ProxySession.h" |
30 | | #include "proxy/http2/Http2ConnectionState.h" |
31 | | #include "proxy/http2/Http2Frame.h" |
32 | | |
33 | | // Name Edata Description |
34 | | // HTTP2_SESSION_EVENT_INIT Http2CommonSession * HTTP/2 session is born |
35 | | // HTTP2_SESSION_EVENT_FINI Http2CommonSession * HTTP/2 session is ended |
36 | | // HTTP2_SESSION_EVENT_RECV Http2Frame * Received a frame |
37 | | // HTTP2_SESSION_EVENT_PRIO Http2Frame * Send this priority frame |
38 | | // HTTP2_SESSION_EVENT_DATA Http2Frame * Send the data frames in the stream |
39 | | // HTTP2_SESSION_EVENT_XMIT Http2CommonSession * Try retransmitting frames. |
40 | | |
41 | | #define HTTP2_SESSION_EVENT_INIT (HTTP2_SESSION_EVENTS_START + 1) |
42 | 0 | #define HTTP2_SESSION_EVENT_FINI (HTTP2_SESSION_EVENTS_START + 2) |
43 | | #define HTTP2_SESSION_EVENT_RECV (HTTP2_SESSION_EVENTS_START + 3) |
44 | 0 | #define HTTP2_SESSION_EVENT_PRIO (HTTP2_SESSION_EVENTS_START + 4) |
45 | 0 | #define HTTP2_SESSION_EVENT_DATA (HTTP2_SESSION_EVENTS_START + 5) |
46 | 0 | #define HTTP2_SESSION_EVENT_XMIT (HTTP2_SESSION_EVENTS_START + 6) |
47 | 0 | #define HTTP2_SESSION_EVENT_SHUTDOWN_INIT (HTTP2_SESSION_EVENTS_START + 7) |
48 | 0 | #define HTTP2_SESSION_EVENT_SHUTDOWN_CONT (HTTP2_SESSION_EVENTS_START + 8) |
49 | 0 | #define HTTP2_SESSION_EVENT_REENABLE (HTTP2_SESSION_EVENTS_START + 9) |
50 | 0 | #define HTTP2_SESSION_EVENT_ERROR (HTTP2_SESSION_EVENTS_START + 10) |
51 | | |
52 | | enum class Http2SessionCod : int { |
53 | | NOT_PROVIDED, |
54 | | HIGH_ERROR_RATE, |
55 | | }; |
56 | | |
57 | | enum class Http2SsnMilestone { |
58 | | OPEN = 0, |
59 | | CLOSE, |
60 | | LAST_ENTRY, |
61 | | }; |
62 | | |
63 | | #if TS_USE_MALLOC_ALLOCATOR |
64 | | size_t const HTTP2_HEADER_BUFFER_SIZE_INDEX = BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(4096); |
65 | | #else |
66 | | size_t const HTTP2_HEADER_BUFFER_SIZE_INDEX = BUFFER_SIZE_INDEX_4K; |
67 | | #endif |
68 | | |
69 | | /** |
70 | | @startuml |
71 | | title HTTP/2 Session Handler - state of reading HTTP/2 frame |
72 | | hide empty description |
73 | | |
74 | | [*] --> state_read_connection_preface : start() |
75 | | state_read_connection_preface --> state_start_frame_read : receive connection preface |
76 | | state_start_frame_read --> state_start_frame_read : do_complete_frame_read() |
77 | | state_start_frame_read --> state_complete_frame_read : reading HTTP/2 frame is halfway but no data in the buffer |
78 | | state_complete_frame_read --> state_start_frame_read : do_complete_frame_read() |
79 | | |
80 | | @enduml |
81 | | */ |
82 | | class Http2CommonSession |
83 | | { |
84 | | public: |
85 | | using SessionHandler = int (Http2CommonSession::*)(int, void *); |
86 | | |
87 | 0 | virtual ~Http2CommonSession() = default; |
88 | | |
89 | | ///////////////////// |
90 | | // Methods |
91 | | |
92 | | bool common_free(ProxySession *ssn); |
93 | | void write_reenable(); |
94 | | int64_t xmit(const Http2TxFrame &frame, bool flush = true); |
95 | | void flush(); |
96 | | |
97 | | int64_t get_connection_id(); |
98 | | Ptr<ProxyMutex> &get_mutex(); |
99 | | NetVConnection *get_netvc(); |
100 | | void do_clear_session_active(); |
101 | | |
102 | | //////////////////// |
103 | | // Accessors |
104 | | void set_dying_event(int event); |
105 | | int get_dying_event() const; |
106 | | bool ready_to_free() const; |
107 | | bool is_recursing() const; |
108 | | virtual void set_half_close_local_flag(bool flag); |
109 | | bool get_half_close_local_flag() const; |
110 | | bool is_url_pushed(const char *url, int url_len); |
111 | | void add_url_to_pushed_table(const char *url, int url_len); |
112 | | |
113 | | // Record history from Http2ConnectionState |
114 | | void remember(const SourceLocation &location, int event, int reentrant = NO_REENTRANT); |
115 | | |
116 | | int64_t write_avail(); |
117 | | bool is_write_high_water() const; |
118 | | |
119 | | virtual ProxySession *get_proxy_session() = 0; |
120 | | |
121 | | virtual void add_session(); |
122 | | virtual bool is_outbound() const; |
123 | | |
124 | | virtual void set_no_activity_timeout() = 0; |
125 | | |
126 | | void interrupt_reading_frames(); |
127 | | |
128 | | /////////////////// |
129 | | // Variables |
130 | | Http2ConnectionState connection_state; |
131 | | |
132 | | protected: |
133 | | // SessionHandler(s) - state of reading frame |
134 | | int state_read_connection_preface(int, void *); |
135 | | int state_start_frame_read(int, void *); |
136 | | int state_complete_frame_read(int, void *); |
137 | | |
138 | | // state_start_frame_read and state_complete_frame_read are set up as session event handler. |
139 | | // Both feed into do_process_frame_read which may iterate if there are multiple frames ready on the wire |
140 | | int do_process_frame_read(int event, VIO *vio, bool inside_frame); |
141 | | int do_start_frame_read(Http2ErrorCode &ret_error); |
142 | | int do_complete_frame_read(); |
143 | | |
144 | | /** |
145 | | * Count received frames |
146 | | */ |
147 | | void _count_received_frames(uint32_t type); |
148 | | |
149 | | bool _should_do_something_else(); |
150 | | |
151 | | //////// |
152 | | // Variables |
153 | | SessionHandler session_handler = nullptr; |
154 | | |
155 | | MIOBuffer *read_buffer = nullptr; |
156 | | IOBufferReader *_read_buffer_reader = nullptr; |
157 | | |
158 | | VIO *write_vio = nullptr; |
159 | | MIOBuffer *write_buffer = nullptr; |
160 | | IOBufferReader *_write_buffer_reader = nullptr; |
161 | | |
162 | | Http2FrameHeader current_hdr = {0, 0, 0, 0}; |
163 | | uint32_t _write_size_threshold = 0; |
164 | | uint32_t _write_time_threshold = 100; |
165 | | ink_hrtime _write_buffer_last_flush = 0; |
166 | | |
167 | | History<HISTORY_DEFAULT_SIZE> _history; |
168 | | Milestones<Http2SsnMilestone, static_cast<size_t>(Http2SsnMilestone::LAST_ENTRY)> _milestones; |
169 | | |
170 | | int dying_event = 0; |
171 | | bool kill_me = false; |
172 | | Http2SessionCod cause_of_death = Http2SessionCod::NOT_PROVIDED; |
173 | | bool half_close_local = false; |
174 | | int recursion = 0; |
175 | | |
176 | | std::unordered_set<std::string> *_h2_pushed_urls = nullptr; |
177 | | |
178 | | Event *_reenable_event = nullptr; |
179 | | int _n_frame_read = 0; |
180 | | |
181 | | uint32_t _pending_sending_data_size = 0; |
182 | | |
183 | | int64_t read_from_early_data = 0; |
184 | | bool cur_frame_from_early_data = false; |
185 | | |
186 | | // Counter for received frames |
187 | | std::atomic<uint64_t> _frame_counts_in[HTTP2_FRAME_TYPE_MAX + 1] = { |
188 | | 0, // DATA |
189 | | 0, // HEADERS |
190 | | 0, // PRIORITY |
191 | | 0, // RST_STREAM |
192 | | 0, // SETTINGS |
193 | | 0, // PUSH_PROMISE |
194 | | 0, // PING |
195 | | 0, // GOAWAY |
196 | | 0, // WINDOW_UPDATE |
197 | | 0, // CONTINUATION |
198 | | 0 // UNKNOWN |
199 | | }; |
200 | | |
201 | | private: |
202 | | bool _interrupt_reading_frames = false; |
203 | | }; |
204 | | |
205 | | /////////////////////////////////////////////// |
206 | | // INLINE |
207 | | |
208 | | inline bool |
209 | | Http2CommonSession::ready_to_free() const |
210 | 0 | { |
211 | 0 | return kill_me; |
212 | 0 | } |
213 | | |
214 | | inline void |
215 | | Http2CommonSession::set_dying_event(int event) |
216 | 0 | { |
217 | 0 | dying_event = event; |
218 | 0 | } |
219 | | |
220 | | inline int |
221 | | Http2CommonSession::get_dying_event() const |
222 | 0 | { |
223 | 0 | return dying_event; |
224 | 0 | } |
225 | | |
226 | | inline bool |
227 | | Http2CommonSession::is_recursing() const |
228 | 0 | { |
229 | 0 | return recursion > 0; |
230 | 0 | } |
231 | | |
232 | | inline bool |
233 | | Http2CommonSession::get_half_close_local_flag() const |
234 | 0 | { |
235 | 0 | return half_close_local; |
236 | 0 | } |
237 | | |
238 | | inline bool |
239 | | Http2CommonSession::is_url_pushed(const char *url, int url_len) |
240 | 0 | { |
241 | 0 | if (_h2_pushed_urls == nullptr) { |
242 | 0 | return false; |
243 | 0 | } |
244 | 0 |
|
245 | 0 | return _h2_pushed_urls->find(std::string{url, static_cast<size_t>(url_len)}) != _h2_pushed_urls->end(); |
246 | 0 | } |
247 | | |
248 | | inline int64_t |
249 | | Http2CommonSession::get_connection_id() |
250 | 0 | { |
251 | 0 | return get_proxy_session()->connection_id(); |
252 | 0 | } |
253 | | |
254 | | inline Ptr<ProxyMutex> & |
255 | | Http2CommonSession::get_mutex() |
256 | 0 | { |
257 | 0 | return get_proxy_session()->mutex; |
258 | 0 | } |
259 | | |
260 | | inline NetVConnection * |
261 | | Http2CommonSession::get_netvc() |
262 | 0 | { |
263 | 0 | return get_proxy_session()->get_netvc(); |
264 | 0 | } |
265 | | |
266 | | inline void |
267 | | Http2CommonSession::do_clear_session_active() |
268 | 0 | { |
269 | 0 | get_proxy_session()->clear_session_active(); |
270 | 0 | } |