Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / h2load_spdy_session.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "h2load_spdy_session.h"
26
27 #include <cassert>
28 #include <cerrno>
29
30 #include "h2load.h"
31 #include "util.h"
32
33 using namespace nghttp2;
34
35 namespace h2load {
36
37 SpdySession::SpdySession(Client *client, uint16_t spdy_version)
38     : client_(client), session_(nullptr), spdy_version_(spdy_version) {}
39
40 SpdySession::~SpdySession() { spdylay_session_del(session_); }
41
42 namespace {
43 void before_ctrl_send_callback(spdylay_session *session,
44                                spdylay_frame_type type, spdylay_frame *frame,
45                                void *user_data) {
46   auto client = static_cast<Client *>(user_data);
47   if (type != SPDYLAY_SYN_STREAM) {
48     return;
49   }
50   client->on_request(frame->syn_stream.stream_id);
51   auto req_stat =
52       static_cast<RequestStat *>(spdylay_session_get_stream_user_data(
53           session, frame->syn_stream.stream_id));
54   client->record_request_time(req_stat);
55 }
56 } // namespace
57
58 namespace {
59 void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
60                            spdylay_frame *frame, void *user_data) {
61   auto client = static_cast<Client *>(user_data);
62   if (type != SPDYLAY_SYN_REPLY) {
63     return;
64   }
65   for (auto p = frame->syn_reply.nv; *p; p += 2) {
66     auto name = *p;
67     auto value = *(p + 1);
68     client->on_header(frame->syn_reply.stream_id,
69                       reinterpret_cast<const uint8_t *>(name), strlen(name),
70                       reinterpret_cast<const uint8_t *>(value), strlen(value));
71   }
72   client->worker->stats.bytes_head += frame->syn_reply.hd.length;
73 }
74 } // namespace
75
76 namespace {
77 void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags,
78                                  int32_t stream_id, const uint8_t *data,
79                                  size_t len, void *user_data) {
80   auto client = static_cast<Client *>(user_data);
81   client->worker->stats.bytes_body += len;
82
83   auto spdy_session = static_cast<SpdySession *>(client->session.get());
84
85   spdy_session->handle_window_update(stream_id, len);
86 }
87 } // namespace
88
89 namespace {
90 void on_stream_close_callback(spdylay_session *session, int32_t stream_id,
91                               spdylay_status_code status_code,
92                               void *user_data) {
93   auto client = static_cast<Client *>(user_data);
94   auto req_stat = static_cast<RequestStat *>(
95       spdylay_session_get_stream_user_data(session, stream_id));
96   client->on_stream_close(stream_id, status_code == SPDYLAY_OK, req_stat);
97 }
98 } // namespace
99
100 namespace {
101 ssize_t send_callback(spdylay_session *session, const uint8_t *data,
102                       size_t length, int flags, void *user_data) {
103   auto client = static_cast<Client *>(user_data);
104   auto &wb = client->wb;
105
106   if (wb.wleft() == 0) {
107     return SPDYLAY_ERR_DEFERRED;
108   }
109
110   return wb.write(data, length);
111 }
112 } // namespace
113
114 namespace {
115 ssize_t file_read_callback(spdylay_session *session, int32_t stream_id,
116                            uint8_t *buf, size_t length, int *eof,
117                            spdylay_data_source *source, void *user_data) {
118   auto client = static_cast<Client *>(user_data);
119   auto config = client->worker->config;
120   auto req_stat = static_cast<RequestStat *>(
121       spdylay_session_get_stream_user_data(session, stream_id));
122
123   ssize_t nread;
124   while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
125              -1 &&
126          errno == EINTR)
127     ;
128
129   if (nread == -1) {
130     return SPDYLAY_ERR_TEMPORAL_CALLBACK_FAILURE;
131   }
132
133   req_stat->data_offset += nread;
134
135   if (nread == 0 || req_stat->data_offset == config->data_length) {
136     *eof = 1;
137   }
138
139   return nread;
140 }
141 } // namespace
142
143 void SpdySession::on_connect() {
144   spdylay_session_callbacks callbacks = {0};
145   callbacks.send_callback = send_callback;
146   callbacks.before_ctrl_send_callback = before_ctrl_send_callback;
147   callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
148   callbacks.on_stream_close_callback = on_stream_close_callback;
149   callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback;
150
151   spdylay_session_client_new(&session_, spdy_version_, &callbacks, client_);
152
153   int val = 1;
154   spdylay_session_set_option(session_, SPDYLAY_OPT_NO_AUTO_WINDOW_UPDATE, &val,
155                              sizeof(val));
156
157   spdylay_settings_entry iv;
158   iv.settings_id = SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE;
159   iv.flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
160   iv.value = (1 << client_->worker->config->window_bits);
161   spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, &iv, 1);
162
163   auto config = client_->worker->config;
164
165   if (spdy_version_ >= SPDYLAY_PROTO_SPDY3_1 &&
166       config->connection_window_bits > 16) {
167     auto delta =
168         (1 << config->connection_window_bits) - SPDYLAY_INITIAL_WINDOW_SIZE;
169     spdylay_submit_window_update(session_, 0, delta);
170   }
171
172   client_->signal_write();
173 }
174
175 void SpdySession::submit_request(RequestStat *req_stat) {
176   auto config = client_->worker->config;
177   auto &nv = config->nv[client_->reqidx++];
178
179   if (client_->reqidx == config->nv.size()) {
180     client_->reqidx = 0;
181   }
182
183   spdylay_data_provider prd{{0}, file_read_callback};
184
185   spdylay_submit_request(session_, 0, nv.data(),
186                          config->data_fd == -1 ? nullptr : &prd, req_stat);
187 }
188
189 int SpdySession::on_read(const uint8_t *data, size_t len) {
190   auto rv = spdylay_session_mem_recv(session_, data, len);
191   if (rv < 0) {
192     return -1;
193   }
194
195   assert(static_cast<size_t>(rv) == len);
196
197   if (spdylay_session_want_read(session_) == 0 &&
198       spdylay_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
199     return -1;
200   }
201
202   client_->signal_write();
203
204   return 0;
205 }
206
207 int SpdySession::on_write() {
208   auto rv = spdylay_session_send(session_);
209   if (rv != 0) {
210     return -1;
211   }
212
213   if (spdylay_session_want_read(session_) == 0 &&
214       spdylay_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
215     return -1;
216   }
217   return 0;
218 }
219
220 void SpdySession::terminate() {
221   spdylay_session_fail_session(session_, SPDYLAY_OK);
222 }
223
224 namespace {
225 int32_t determine_window_update_transmission(spdylay_session *session,
226                                              int32_t stream_id,
227                                              size_t window_bits) {
228   int32_t recv_length;
229
230   if (stream_id == 0) {
231     recv_length = spdylay_session_get_recv_data_length(session);
232   } else {
233     recv_length =
234         spdylay_session_get_stream_recv_data_length(session, stream_id);
235   }
236
237   auto window_size = 1 << window_bits;
238
239   if (recv_length != -1 && recv_length >= window_size / 2) {
240     return recv_length;
241   }
242
243   return -1;
244 }
245 } // namespace
246
247 void SpdySession::handle_window_update(int32_t stream_id, size_t recvlen) {
248   auto config = client_->worker->config;
249   size_t connection_window_bits;
250
251   if (config->connection_window_bits > 16) {
252     connection_window_bits = config->connection_window_bits;
253   } else {
254     connection_window_bits = 16;
255   }
256
257   auto delta =
258       determine_window_update_transmission(session_, 0, connection_window_bits);
259   if (delta > 0) {
260     spdylay_submit_window_update(session_, 0, delta);
261   }
262
263   delta = determine_window_update_transmission(session_, stream_id,
264                                                config->window_bits);
265   if (delta > 0) {
266     spdylay_submit_window_update(session_, stream_id, delta);
267   }
268 }
269
270 } // namespace h2load