Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/transport/chttp2/internal.h"
35 :
36 : #include <grpc/support/log.h>
37 :
38 : #define TRANSPORT_FROM_GLOBAL(tg) \
39 : ((grpc_chttp2_transport *)((char *)(tg)-offsetof(grpc_chttp2_transport, \
40 : global)))
41 :
42 : #define STREAM_FROM_GLOBAL(sg) \
43 : ((grpc_chttp2_stream *)((char *)(sg)-offsetof(grpc_chttp2_stream, global)))
44 :
45 : #define TRANSPORT_FROM_WRITING(tw) \
46 : ((grpc_chttp2_transport *)((char *)(tw)-offsetof(grpc_chttp2_transport, \
47 : writing)))
48 :
49 : #define STREAM_FROM_WRITING(sw) \
50 : ((grpc_chttp2_stream *)((char *)(sw)-offsetof(grpc_chttp2_stream, writing)))
51 :
52 : #define TRANSPORT_FROM_PARSING(tp) \
53 : ((grpc_chttp2_transport *)((char *)(tp)-offsetof(grpc_chttp2_transport, \
54 : parsing)))
55 :
56 : #define STREAM_FROM_PARSING(sp) \
57 : ((grpc_chttp2_stream *)((char *)(sp)-offsetof(grpc_chttp2_stream, parsing)))
58 :
59 : /* core list management */
60 :
61 21275973 : static int stream_list_empty(grpc_chttp2_transport *t,
62 : grpc_chttp2_stream_list_id id) {
63 21275973 : return t->lists[id].head == NULL;
64 : }
65 :
66 107313380 : static int stream_list_pop(grpc_chttp2_transport *t,
67 : grpc_chttp2_stream **stream,
68 : grpc_chttp2_stream_list_id id) {
69 107313380 : grpc_chttp2_stream *s = t->lists[id].head;
70 107313380 : if (s) {
71 37460033 : grpc_chttp2_stream *new_head = s->links[id].next;
72 37460033 : GPR_ASSERT(s->included[id]);
73 37460033 : if (new_head) {
74 9411740 : t->lists[id].head = new_head;
75 9411740 : new_head->links[id].prev = NULL;
76 : } else {
77 28048293 : t->lists[id].head = NULL;
78 28048293 : t->lists[id].tail = NULL;
79 : }
80 37460033 : s->included[id] = 0;
81 : }
82 107313380 : *stream = s;
83 107313380 : return s != 0;
84 : }
85 :
86 4095705 : static void stream_list_remove(grpc_chttp2_transport *t, grpc_chttp2_stream *s,
87 : grpc_chttp2_stream_list_id id) {
88 4095705 : GPR_ASSERT(s->included[id]);
89 4095705 : s->included[id] = 0;
90 4095705 : if (s->links[id].prev) {
91 2166938 : s->links[id].prev->links[id].next = s->links[id].next;
92 : } else {
93 1928767 : GPR_ASSERT(t->lists[id].head == s);
94 1928767 : t->lists[id].head = s->links[id].next;
95 : }
96 4095705 : if (s->links[id].next) {
97 3382389 : s->links[id].next->links[id].prev = s->links[id].prev;
98 : } else {
99 713316 : t->lists[id].tail = s->links[id].prev;
100 : }
101 4095705 : }
102 :
103 13472042 : static void stream_list_maybe_remove(grpc_chttp2_transport *t,
104 : grpc_chttp2_stream *s,
105 : grpc_chttp2_stream_list_id id) {
106 13472042 : if (s->included[id]) {
107 4095703 : stream_list_remove(t, s, id);
108 : }
109 13470613 : }
110 :
111 16 : static void stream_list_add_head(grpc_chttp2_transport *t,
112 : grpc_chttp2_stream *s,
113 : grpc_chttp2_stream_list_id id) {
114 : grpc_chttp2_stream *old_head;
115 16 : GPR_ASSERT(!s->included[id]);
116 16 : old_head = t->lists[id].head;
117 16 : s->links[id].next = old_head;
118 16 : s->links[id].prev = NULL;
119 16 : if (old_head) {
120 0 : old_head->links[id].prev = s;
121 : } else {
122 16 : t->lists[id].tail = s;
123 : }
124 16 : t->lists[id].head = s;
125 16 : s->included[id] = 1;
126 16 : }
127 :
128 41730167 : static void stream_list_add_tail(grpc_chttp2_transport *t,
129 : grpc_chttp2_stream *s,
130 : grpc_chttp2_stream_list_id id) {
131 : grpc_chttp2_stream *old_tail;
132 41730167 : GPR_ASSERT(!s->included[id]);
133 41730167 : old_tail = t->lists[id].tail;
134 41730167 : s->links[id].next = NULL;
135 41730167 : s->links[id].prev = old_tail;
136 41730167 : if (old_tail) {
137 12932539 : old_tail->links[id].next = s;
138 : } else {
139 28797628 : t->lists[id].head = s;
140 : }
141 41730167 : t->lists[id].tail = s;
142 41730167 : s->included[id] = 1;
143 41730167 : }
144 :
145 88351140 : static void stream_list_add(grpc_chttp2_transport *t, grpc_chttp2_stream *s,
146 : grpc_chttp2_stream_list_id id) {
147 88351140 : if (s->included[id]) {
148 137812167 : return;
149 : }
150 38912135 : stream_list_add_tail(t, s, id);
151 : }
152 :
153 : /* wrappers for specializations */
154 :
155 11354066 : void grpc_chttp2_list_add_writable_stream(
156 : grpc_chttp2_transport_global *transport_global,
157 : grpc_chttp2_stream_global *stream_global) {
158 11354066 : GPR_ASSERT(stream_global->id != 0);
159 11354066 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
160 : STREAM_FROM_GLOBAL(stream_global), GRPC_CHTTP2_LIST_WRITABLE);
161 11351280 : }
162 :
163 16 : void grpc_chttp2_list_add_first_writable_stream(
164 : grpc_chttp2_transport_global *transport_global,
165 : grpc_chttp2_stream_global *stream_global) {
166 16 : GPR_ASSERT(stream_global->id != 0);
167 16 : stream_list_add_head(TRANSPORT_FROM_GLOBAL(transport_global),
168 : STREAM_FROM_GLOBAL(stream_global),
169 : GRPC_CHTTP2_LIST_WRITABLE);
170 16 : }
171 :
172 19860618 : int grpc_chttp2_list_pop_writable_stream(
173 : grpc_chttp2_transport_global *transport_global,
174 : grpc_chttp2_transport_writing *transport_writing,
175 : grpc_chttp2_stream_global **stream_global,
176 : grpc_chttp2_stream_writing **stream_writing) {
177 : grpc_chttp2_stream *stream;
178 19860618 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
179 : GRPC_CHTTP2_LIST_WRITABLE);
180 19879141 : if (r != 0) {
181 7343177 : *stream_global = &stream->global;
182 7343177 : *stream_writing = &stream->writing;
183 : }
184 19879141 : return r;
185 : }
186 :
187 5397438 : void grpc_chttp2_list_remove_writable_stream(
188 : grpc_chttp2_transport_global *transport_global,
189 : grpc_chttp2_stream_global *stream_global) {
190 5397438 : stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global),
191 : STREAM_FROM_GLOBAL(stream_global),
192 : GRPC_CHTTP2_LIST_WRITABLE);
193 5400287 : }
194 :
195 4309882 : void grpc_chttp2_list_add_writing_stream(
196 : grpc_chttp2_transport_writing *transport_writing,
197 : grpc_chttp2_stream_writing *stream_writing) {
198 4309882 : stream_list_add(TRANSPORT_FROM_WRITING(transport_writing),
199 : STREAM_FROM_WRITING(stream_writing),
200 : GRPC_CHTTP2_LIST_WRITING);
201 4309898 : }
202 :
203 15925883 : int grpc_chttp2_list_have_writing_streams(
204 : grpc_chttp2_transport_writing *transport_writing) {
205 15925883 : return !stream_list_empty(TRANSPORT_FROM_WRITING(transport_writing),
206 : GRPC_CHTTP2_LIST_WRITING);
207 : }
208 :
209 7733920 : int grpc_chttp2_list_pop_writing_stream(
210 : grpc_chttp2_transport_writing *transport_writing,
211 : grpc_chttp2_stream_writing **stream_writing) {
212 : grpc_chttp2_stream *stream;
213 7733920 : int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream,
214 : GRPC_CHTTP2_LIST_WRITING);
215 7740639 : if (r != 0) {
216 4308669 : *stream_writing = &stream->writing;
217 : }
218 7740639 : return r;
219 : }
220 :
221 4310900 : void grpc_chttp2_list_add_written_stream(
222 : grpc_chttp2_transport_writing *transport_writing,
223 : grpc_chttp2_stream_writing *stream_writing) {
224 4310900 : stream_list_add(TRANSPORT_FROM_WRITING(transport_writing),
225 : STREAM_FROM_WRITING(stream_writing),
226 : GRPC_CHTTP2_LIST_WRITTEN);
227 4310649 : }
228 :
229 7742438 : int grpc_chttp2_list_pop_written_stream(
230 : grpc_chttp2_transport_global *transport_global,
231 : grpc_chttp2_transport_writing *transport_writing,
232 : grpc_chttp2_stream_global **stream_global,
233 : grpc_chttp2_stream_writing **stream_writing) {
234 : grpc_chttp2_stream *stream;
235 7742438 : int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream,
236 : GRPC_CHTTP2_LIST_WRITTEN);
237 7741091 : if (r != 0) {
238 4312019 : *stream_global = &stream->global;
239 4312019 : *stream_writing = &stream->writing;
240 : }
241 7741091 : return r;
242 : }
243 :
244 47308642 : void grpc_chttp2_list_add_parsing_seen_stream(
245 : grpc_chttp2_transport_parsing *transport_parsing,
246 : grpc_chttp2_stream_parsing *stream_parsing) {
247 47308642 : stream_list_add(TRANSPORT_FROM_PARSING(transport_parsing),
248 : STREAM_FROM_PARSING(stream_parsing),
249 : GRPC_CHTTP2_LIST_PARSING_SEEN);
250 47310316 : }
251 :
252 6978191 : int grpc_chttp2_list_pop_parsing_seen_stream(
253 : grpc_chttp2_transport_global *transport_global,
254 : grpc_chttp2_transport_parsing *transport_parsing,
255 : grpc_chttp2_stream_global **stream_global,
256 : grpc_chttp2_stream_parsing **stream_parsing) {
257 : grpc_chttp2_stream *stream;
258 6978191 : int r = stream_list_pop(TRANSPORT_FROM_PARSING(transport_parsing), &stream,
259 : GRPC_CHTTP2_LIST_PARSING_SEEN);
260 6977880 : if (r != 0) {
261 4512318 : *stream_global = &stream->global;
262 4512318 : *stream_parsing = &stream->parsing;
263 : }
264 6977880 : return r;
265 : }
266 :
267 1402782 : void grpc_chttp2_list_add_waiting_for_concurrency(
268 : grpc_chttp2_transport_global *transport_global,
269 : grpc_chttp2_stream_global *stream_global) {
270 1402782 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
271 : STREAM_FROM_GLOBAL(stream_global),
272 : GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
273 1402897 : }
274 :
275 4206223 : int grpc_chttp2_list_pop_waiting_for_concurrency(
276 : grpc_chttp2_transport_global *transport_global,
277 : grpc_chttp2_stream_global **stream_global) {
278 : grpc_chttp2_stream *stream;
279 4206223 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
280 : GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
281 4206516 : if (r != 0) {
282 1402167 : *stream_global = &stream->global;
283 : }
284 4206516 : return r;
285 : }
286 :
287 64632 : void grpc_chttp2_list_add_closed_waiting_for_parsing(
288 : grpc_chttp2_transport_global *transport_global,
289 : grpc_chttp2_stream_global *stream_global) {
290 64632 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
291 : STREAM_FROM_GLOBAL(stream_global),
292 : GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
293 64632 : }
294 :
295 15415037 : int grpc_chttp2_list_pop_closed_waiting_for_parsing(
296 : grpc_chttp2_transport_global *transport_global,
297 : grpc_chttp2_stream_global **stream_global) {
298 : grpc_chttp2_stream *stream;
299 15415037 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
300 : GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
301 15434808 : if (r != 0) {
302 47035 : *stream_global = &stream->global;
303 : }
304 15434808 : return r;
305 : }
306 :
307 84 : void grpc_chttp2_list_add_cancelled_waiting_for_writing(
308 : grpc_chttp2_transport_global *transport_global,
309 : grpc_chttp2_stream_global *stream_global) {
310 84 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
311 : STREAM_FROM_GLOBAL(stream_global),
312 : GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
313 84 : }
314 :
315 12575254 : int grpc_chttp2_list_pop_cancelled_waiting_for_writing(
316 : grpc_chttp2_transport_global *transport_global,
317 : grpc_chttp2_stream_global **stream_global) {
318 : grpc_chttp2_stream *stream;
319 12575254 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
320 : GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
321 12572815 : if (r != 0) {
322 81 : *stream_global = &stream->global;
323 : }
324 12572815 : return r;
325 : }
326 :
327 3101249 : void grpc_chttp2_list_add_incoming_window_updated(
328 : grpc_chttp2_transport_global *transport_global,
329 : grpc_chttp2_stream_global *stream_global) {
330 3101249 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
331 : STREAM_FROM_GLOBAL(stream_global),
332 : GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
333 3101160 : }
334 :
335 4186363 : int grpc_chttp2_list_pop_incoming_window_updated(
336 : grpc_chttp2_transport_global *transport_global,
337 : grpc_chttp2_transport_parsing *transport_parsing,
338 : grpc_chttp2_stream_global **stream_global,
339 : grpc_chttp2_stream_parsing **stream_parsing) {
340 : grpc_chttp2_stream *stream;
341 4186363 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
342 : GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
343 4186175 : if (r != 0) {
344 1720626 : *stream_global = &stream->global;
345 1720626 : *stream_parsing = &stream->parsing;
346 : }
347 4186175 : return r;
348 : }
349 :
350 2704071 : void grpc_chttp2_list_remove_incoming_window_updated(
351 : grpc_chttp2_transport_global *transport_global,
352 : grpc_chttp2_stream_global *stream_global) {
353 2704071 : stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global),
354 : STREAM_FROM_GLOBAL(stream_global),
355 : GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
356 2704062 : }
357 :
358 16829632 : void grpc_chttp2_list_add_read_write_state_changed(
359 : grpc_chttp2_transport_global *transport_global,
360 : grpc_chttp2_stream_global *stream_global) {
361 16829632 : stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
362 : STREAM_FROM_GLOBAL(stream_global),
363 : GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
364 16830948 : }
365 :
366 30794118 : int grpc_chttp2_list_pop_read_write_state_changed(
367 : grpc_chttp2_transport_global *transport_global,
368 : grpc_chttp2_stream_global **stream_global) {
369 : grpc_chttp2_stream *stream;
370 30794118 : int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
371 : GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
372 30826777 : if (r != 0) {
373 14142694 : *stream_global = &stream->global;
374 : }
375 30826777 : return r;
376 : }
377 :
378 2704456 : void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
379 : grpc_chttp2_stream *s) {
380 2704456 : stream_list_add_tail(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
381 2704709 : }
382 :
383 5400655 : int grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
384 : grpc_chttp2_stream *s) {
385 5400655 : stream_list_maybe_remove(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
386 5400639 : return stream_list_empty(t, GRPC_CHTTP2_LIST_ALL_STREAMS);
387 : }
388 :
389 1772 : int grpc_chttp2_has_streams(grpc_chttp2_transport *t) {
390 1772 : return !stream_list_empty(t, GRPC_CHTTP2_LIST_ALL_STREAMS);
391 : }
392 :
393 8084 : void grpc_chttp2_for_all_streams(
394 : grpc_chttp2_transport_global *transport_global, void *user_data,
395 : void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
396 : grpc_chttp2_stream_global *stream_global)) {
397 : grpc_chttp2_stream *s;
398 8084 : grpc_chttp2_transport *t = TRANSPORT_FROM_GLOBAL(transport_global);
399 16228 : for (s = t->lists[GRPC_CHTTP2_LIST_ALL_STREAMS].head; s != NULL;
400 60 : s = s->links[GRPC_CHTTP2_LIST_ALL_STREAMS].next) {
401 60 : cb(transport_global, user_data, &s->global);
402 : }
403 8084 : }
|