nghttp2_stream.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 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 "nghttp2_stream.h"
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include "nghttp2_session.h"
  29. #include "nghttp2_helper.h"
  30. #include "nghttp2_debug.h"
  31. #include "nghttp2_frame.h"
  32. /* Maximum distance between any two stream's cycle in the same
  33. priority queue. Imagine stream A's cycle is A, and stream B's
  34. cycle is B, and A < B. The cycle is unsigned 32 bit integer, it
  35. may get overflow. Because of how we calculate the next cycle
  36. value, if B - A is less than or equals to
  37. NGHTTP2_MAX_CYCLE_DISTANCE, A and B are in the same scale, in other
  38. words, B is really greater than or equal to A. Otherwise, A is a
  39. result of overflow, and it is actually A > B if we consider that
  40. fact. */
  41. #define NGHTTP2_MAX_CYCLE_DISTANCE \
  42. ((uint64_t)NGHTTP2_MAX_FRAME_SIZE_MAX * 256 + 255)
  43. static int stream_less(const void *lhsx, const void *rhsx) {
  44. const nghttp2_stream *lhs, *rhs;
  45. lhs = nghttp2_struct_of(lhsx, nghttp2_stream, pq_entry);
  46. rhs = nghttp2_struct_of(rhsx, nghttp2_stream, pq_entry);
  47. if (lhs->cycle == rhs->cycle) {
  48. return lhs->seq < rhs->seq;
  49. }
  50. return rhs->cycle - lhs->cycle <= NGHTTP2_MAX_CYCLE_DISTANCE;
  51. }
  52. void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
  53. uint8_t flags, nghttp2_stream_state initial_state,
  54. int32_t weight, int32_t remote_initial_window_size,
  55. int32_t local_initial_window_size,
  56. void *stream_user_data, nghttp2_mem *mem) {
  57. nghttp2_pq_init(&stream->obq, stream_less, mem);
  58. stream->stream_id = stream_id;
  59. stream->flags = flags;
  60. stream->state = initial_state;
  61. stream->shut_flags = NGHTTP2_SHUT_NONE;
  62. stream->stream_user_data = stream_user_data;
  63. stream->item = NULL;
  64. stream->remote_window_size = remote_initial_window_size;
  65. stream->local_window_size = local_initial_window_size;
  66. stream->recv_window_size = 0;
  67. stream->consumed_size = 0;
  68. stream->recv_reduction = 0;
  69. stream->window_update_queued = 0;
  70. stream->dep_prev = NULL;
  71. stream->dep_next = NULL;
  72. stream->sib_prev = NULL;
  73. stream->sib_next = NULL;
  74. stream->closed_prev = NULL;
  75. stream->closed_next = NULL;
  76. stream->weight = weight;
  77. stream->sum_dep_weight = 0;
  78. stream->http_flags = NGHTTP2_HTTP_FLAG_NONE;
  79. stream->content_length = -1;
  80. stream->recv_content_length = 0;
  81. stream->status_code = -1;
  82. stream->queued = 0;
  83. stream->descendant_last_cycle = 0;
  84. stream->cycle = 0;
  85. stream->pending_penalty = 0;
  86. stream->descendant_next_seq = 0;
  87. stream->seq = 0;
  88. stream->last_writelen = 0;
  89. stream->extpri = stream->http_extpri = NGHTTP2_EXTPRI_DEFAULT_URGENCY;
  90. }
  91. void nghttp2_stream_free(nghttp2_stream *stream) {
  92. nghttp2_pq_free(&stream->obq);
  93. /* We don't free stream->item. If it is assigned to aob, then
  94. active_outbound_item_reset() will delete it. Otherwise,
  95. nghttp2_stream_close() or session_del() will delete it. */
  96. }
  97. void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag) {
  98. stream->shut_flags = (uint8_t)(stream->shut_flags | flag);
  99. }
  100. /*
  101. * Returns nonzero if |stream| is active. This function does not take
  102. * into account its descendants.
  103. */
  104. static int stream_active(nghttp2_stream *stream) {
  105. return stream->item &&
  106. (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) == 0;
  107. }
  108. /*
  109. * Returns nonzero if |stream| or one of its descendants is active
  110. */
  111. static int stream_subtree_active(nghttp2_stream *stream) {
  112. return stream_active(stream) || !nghttp2_pq_empty(&stream->obq);
  113. }
  114. /*
  115. * Returns next cycle for |stream|.
  116. */
  117. static void stream_next_cycle(nghttp2_stream *stream, uint64_t last_cycle) {
  118. uint64_t penalty;
  119. penalty = (uint64_t)stream->last_writelen * NGHTTP2_MAX_WEIGHT +
  120. stream->pending_penalty;
  121. stream->cycle = last_cycle + penalty / (uint32_t)stream->weight;
  122. stream->pending_penalty = (uint32_t)(penalty % (uint32_t)stream->weight);
  123. }
  124. static int stream_obq_push(nghttp2_stream *dep_stream, nghttp2_stream *stream) {
  125. int rv;
  126. for (; dep_stream && !stream->queued;
  127. stream = dep_stream, dep_stream = dep_stream->dep_prev) {
  128. stream_next_cycle(stream, dep_stream->descendant_last_cycle);
  129. stream->seq = dep_stream->descendant_next_seq++;
  130. DEBUGF("stream: stream=%d obq push cycle=%lu\n", stream->stream_id,
  131. stream->cycle);
  132. DEBUGF("stream: push stream %d to stream %d\n", stream->stream_id,
  133. dep_stream->stream_id);
  134. rv = nghttp2_pq_push(&dep_stream->obq, &stream->pq_entry);
  135. if (rv != 0) {
  136. return rv;
  137. }
  138. stream->queued = 1;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Removes |stream| from parent's obq. If removal of |stream| makes
  144. * parent's obq empty, and parent is not active, then parent is also
  145. * removed. This process is repeated recursively.
  146. */
  147. static void stream_obq_remove(nghttp2_stream *stream) {
  148. nghttp2_stream *dep_stream;
  149. dep_stream = stream->dep_prev;
  150. if (!stream->queued) {
  151. return;
  152. }
  153. for (; dep_stream; stream = dep_stream, dep_stream = dep_stream->dep_prev) {
  154. DEBUGF("stream: remove stream %d from stream %d\n", stream->stream_id,
  155. dep_stream->stream_id);
  156. nghttp2_pq_remove(&dep_stream->obq, &stream->pq_entry);
  157. assert(stream->queued);
  158. stream->queued = 0;
  159. stream->cycle = 0;
  160. stream->pending_penalty = 0;
  161. stream->descendant_last_cycle = 0;
  162. stream->last_writelen = 0;
  163. if (stream_subtree_active(dep_stream)) {
  164. return;
  165. }
  166. }
  167. }
  168. /*
  169. * Moves |stream| from |src|'s obq to |dest|'s obq. Removal from
  170. * |src|'s obq is just done calling nghttp2_pq_remove(), so it does
  171. * not recursively remove |src| and ancestors, like
  172. * stream_obq_remove().
  173. */
  174. static int stream_obq_move(nghttp2_stream *dest, nghttp2_stream *src,
  175. nghttp2_stream *stream) {
  176. if (!stream->queued) {
  177. return 0;
  178. }
  179. DEBUGF("stream: remove stream %d from stream %d (move)\n", stream->stream_id,
  180. src->stream_id);
  181. nghttp2_pq_remove(&src->obq, &stream->pq_entry);
  182. stream->queued = 0;
  183. return stream_obq_push(dest, stream);
  184. }
  185. void nghttp2_stream_reschedule(nghttp2_stream *stream) {
  186. nghttp2_stream *dep_stream;
  187. assert(stream->queued);
  188. dep_stream = stream->dep_prev;
  189. for (; dep_stream; stream = dep_stream, dep_stream = dep_stream->dep_prev) {
  190. nghttp2_pq_remove(&dep_stream->obq, &stream->pq_entry);
  191. stream_next_cycle(stream, dep_stream->descendant_last_cycle);
  192. stream->seq = dep_stream->descendant_next_seq++;
  193. nghttp2_pq_push(&dep_stream->obq, &stream->pq_entry);
  194. DEBUGF("stream: stream=%d obq resched cycle=%lu\n", stream->stream_id,
  195. stream->cycle);
  196. dep_stream->last_writelen = stream->last_writelen;
  197. }
  198. }
  199. void nghttp2_stream_change_weight(nghttp2_stream *stream, int32_t weight) {
  200. nghttp2_stream *dep_stream;
  201. uint64_t last_cycle;
  202. int32_t old_weight;
  203. uint64_t wlen_penalty;
  204. if (stream->weight == weight) {
  205. return;
  206. }
  207. old_weight = stream->weight;
  208. stream->weight = weight;
  209. dep_stream = stream->dep_prev;
  210. if (!dep_stream) {
  211. return;
  212. }
  213. dep_stream->sum_dep_weight += weight - old_weight;
  214. if (!stream->queued) {
  215. return;
  216. }
  217. nghttp2_pq_remove(&dep_stream->obq, &stream->pq_entry);
  218. wlen_penalty = (uint64_t)stream->last_writelen * NGHTTP2_MAX_WEIGHT;
  219. /* Compute old stream->pending_penalty we used to calculate
  220. stream->cycle */
  221. stream->pending_penalty =
  222. (uint32_t)((stream->pending_penalty + (uint32_t)old_weight -
  223. (wlen_penalty % (uint32_t)old_weight)) %
  224. (uint32_t)old_weight);
  225. last_cycle = stream->cycle -
  226. (wlen_penalty + stream->pending_penalty) / (uint32_t)old_weight;
  227. /* Now we have old stream->pending_penalty and new stream->weight in
  228. place */
  229. stream_next_cycle(stream, last_cycle);
  230. if (dep_stream->descendant_last_cycle - stream->cycle <=
  231. NGHTTP2_MAX_CYCLE_DISTANCE) {
  232. stream->cycle = dep_stream->descendant_last_cycle;
  233. }
  234. /* Continue to use same stream->seq */
  235. nghttp2_pq_push(&dep_stream->obq, &stream->pq_entry);
  236. DEBUGF("stream: stream=%d obq resched cycle=%lu\n", stream->stream_id,
  237. stream->cycle);
  238. }
  239. static nghttp2_stream *stream_last_sib(nghttp2_stream *stream) {
  240. for (; stream->sib_next; stream = stream->sib_next)
  241. ;
  242. return stream;
  243. }
  244. int32_t nghttp2_stream_dep_distributed_weight(nghttp2_stream *stream,
  245. int32_t weight) {
  246. weight = stream->weight * weight / stream->sum_dep_weight;
  247. return nghttp2_max_int32(1, weight);
  248. }
  249. #ifdef STREAM_DEP_DEBUG
  250. static void ensure_inactive(nghttp2_stream *stream) {
  251. nghttp2_stream *si;
  252. if (stream->queued) {
  253. fprintf(stderr, "stream(%p)=%d, stream->queued = 1; want 0\n", stream,
  254. stream->stream_id);
  255. assert(0);
  256. }
  257. if (stream_active(stream)) {
  258. fprintf(stderr, "stream(%p)=%d, stream_active(stream) = 1; want 0\n",
  259. stream, stream->stream_id);
  260. assert(0);
  261. }
  262. if (!nghttp2_pq_empty(&stream->obq)) {
  263. fprintf(stderr, "stream(%p)=%d, nghttp2_pq_size() = %zu; want 0\n", stream,
  264. stream->stream_id, nghttp2_pq_size(&stream->obq));
  265. assert(0);
  266. }
  267. for (si = stream->dep_next; si; si = si->sib_next) {
  268. ensure_inactive(si);
  269. }
  270. }
  271. static void check_queued(nghttp2_stream *stream) {
  272. nghttp2_stream *si;
  273. int queued;
  274. if (stream->queued) {
  275. if (!stream_subtree_active(stream)) {
  276. fprintf(stderr,
  277. "stream(%p)=%d, stream->queued == 1, but "
  278. "stream_active() == %d and nghttp2_pq_size(&stream->obq) = %zu\n",
  279. stream, stream->stream_id, stream_active(stream),
  280. nghttp2_pq_size(&stream->obq));
  281. assert(0);
  282. }
  283. if (!stream_active(stream)) {
  284. queued = 0;
  285. for (si = stream->dep_next; si; si = si->sib_next) {
  286. if (si->queued) {
  287. ++queued;
  288. }
  289. }
  290. if (queued == 0) {
  291. fprintf(stderr,
  292. "stream(%p)=%d, stream->queued == 1, and "
  293. "!stream_active(), but no descendants is queued\n",
  294. stream, stream->stream_id);
  295. assert(0);
  296. }
  297. }
  298. for (si = stream->dep_next; si; si = si->sib_next) {
  299. check_queued(si);
  300. }
  301. } else {
  302. if (stream_active(stream) || !nghttp2_pq_empty(&stream->obq)) {
  303. fprintf(stderr,
  304. "stream(%p) = %d, stream->queued == 0, but "
  305. "stream_active(stream) == %d and "
  306. "nghttp2_pq_size(&stream->obq) = %zu\n",
  307. stream, stream->stream_id, stream_active(stream),
  308. nghttp2_pq_size(&stream->obq));
  309. assert(0);
  310. }
  311. for (si = stream->dep_next; si; si = si->sib_next) {
  312. ensure_inactive(si);
  313. }
  314. }
  315. }
  316. static void check_sum_dep(nghttp2_stream *stream) {
  317. nghttp2_stream *si;
  318. int32_t n = 0;
  319. for (si = stream->dep_next; si; si = si->sib_next) {
  320. n += si->weight;
  321. }
  322. if (n != stream->sum_dep_weight) {
  323. fprintf(stderr, "stream(%p)=%d, sum_dep_weight = %d; want %d\n", stream,
  324. stream->stream_id, n, stream->sum_dep_weight);
  325. assert(0);
  326. }
  327. for (si = stream->dep_next; si; si = si->sib_next) {
  328. check_sum_dep(si);
  329. }
  330. }
  331. static void check_dep_prev(nghttp2_stream *stream) {
  332. nghttp2_stream *si;
  333. for (si = stream->dep_next; si; si = si->sib_next) {
  334. if (si->dep_prev != stream) {
  335. fprintf(stderr, "si->dep_prev = %p; want %p\n", si->dep_prev, stream);
  336. assert(0);
  337. }
  338. check_dep_prev(si);
  339. }
  340. }
  341. #endif /* STREAM_DEP_DEBUG */
  342. #ifdef STREAM_DEP_DEBUG
  343. static void validate_tree(nghttp2_stream *stream) {
  344. nghttp2_stream *si;
  345. if (!stream) {
  346. return;
  347. }
  348. for (; stream->dep_prev; stream = stream->dep_prev)
  349. ;
  350. assert(stream->stream_id == 0);
  351. assert(!stream->queued);
  352. fprintf(stderr, "checking...\n");
  353. if (nghttp2_pq_empty(&stream->obq)) {
  354. fprintf(stderr, "root obq empty\n");
  355. for (si = stream->dep_next; si; si = si->sib_next) {
  356. ensure_inactive(si);
  357. }
  358. } else {
  359. for (si = stream->dep_next; si; si = si->sib_next) {
  360. check_queued(si);
  361. }
  362. }
  363. check_sum_dep(stream);
  364. check_dep_prev(stream);
  365. }
  366. #else /* !STREAM_DEP_DEBUG */
  367. static void validate_tree(nghttp2_stream *stream) { (void)stream; }
  368. #endif /* !STREAM_DEP_DEBUG*/
  369. static int stream_update_dep_on_attach_item(nghttp2_stream *stream) {
  370. int rv;
  371. rv = stream_obq_push(stream->dep_prev, stream);
  372. if (rv != 0) {
  373. return rv;
  374. }
  375. validate_tree(stream);
  376. return 0;
  377. }
  378. static void stream_update_dep_on_detach_item(nghttp2_stream *stream) {
  379. if (nghttp2_pq_empty(&stream->obq)) {
  380. stream_obq_remove(stream);
  381. }
  382. validate_tree(stream);
  383. }
  384. int nghttp2_stream_attach_item(nghttp2_stream *stream,
  385. nghttp2_outbound_item *item) {
  386. int rv;
  387. assert((stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) == 0);
  388. assert(stream->item == NULL);
  389. DEBUGF("stream: stream=%d attach item=%p\n", stream->stream_id, item);
  390. stream->item = item;
  391. if (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) {
  392. return 0;
  393. }
  394. rv = stream_update_dep_on_attach_item(stream);
  395. if (rv != 0) {
  396. /* This may relave stream->queued == 1, but stream->item == NULL.
  397. But only consequence of this error is fatal one, and session
  398. destruction. In that execution path, these inconsistency does
  399. not matter. */
  400. stream->item = NULL;
  401. return rv;
  402. }
  403. return 0;
  404. }
  405. void nghttp2_stream_detach_item(nghttp2_stream *stream) {
  406. DEBUGF("stream: stream=%d detach item=%p\n", stream->stream_id, stream->item);
  407. stream->item = NULL;
  408. stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_DEFERRED_ALL);
  409. if (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) {
  410. return;
  411. }
  412. stream_update_dep_on_detach_item(stream);
  413. }
  414. void nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags) {
  415. assert(stream->item);
  416. DEBUGF("stream: stream=%d defer item=%p cause=%02x\n", stream->stream_id,
  417. stream->item, flags);
  418. stream->flags |= flags;
  419. if (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) {
  420. return;
  421. }
  422. stream_update_dep_on_detach_item(stream);
  423. }
  424. int nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, uint8_t flags) {
  425. assert(stream->item);
  426. DEBUGF("stream: stream=%d resume item=%p flags=%02x\n", stream->stream_id,
  427. stream->item, flags);
  428. stream->flags = (uint8_t)(stream->flags & ~flags);
  429. if (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) {
  430. return 0;
  431. }
  432. if (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) {
  433. return 0;
  434. }
  435. return stream_update_dep_on_attach_item(stream);
  436. }
  437. int nghttp2_stream_check_deferred_item(nghttp2_stream *stream) {
  438. return stream->item && (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL);
  439. }
  440. int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream) {
  441. return stream->item &&
  442. (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
  443. }
  444. static int update_initial_window_size(int32_t *window_size_ptr,
  445. int32_t new_initial_window_size,
  446. int32_t old_initial_window_size) {
  447. int64_t new_window_size = (int64_t)(*window_size_ptr) +
  448. new_initial_window_size - old_initial_window_size;
  449. if (INT32_MIN > new_window_size ||
  450. new_window_size > NGHTTP2_MAX_WINDOW_SIZE) {
  451. return -1;
  452. }
  453. *window_size_ptr = (int32_t)new_window_size;
  454. return 0;
  455. }
  456. int nghttp2_stream_update_remote_initial_window_size(
  457. nghttp2_stream *stream, int32_t new_initial_window_size,
  458. int32_t old_initial_window_size) {
  459. return update_initial_window_size(&stream->remote_window_size,
  460. new_initial_window_size,
  461. old_initial_window_size);
  462. }
  463. int nghttp2_stream_update_local_initial_window_size(
  464. nghttp2_stream *stream, int32_t new_initial_window_size,
  465. int32_t old_initial_window_size) {
  466. return update_initial_window_size(&stream->local_window_size,
  467. new_initial_window_size,
  468. old_initial_window_size);
  469. }
  470. void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream) {
  471. stream->state = NGHTTP2_STREAM_OPENED;
  472. stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH);
  473. }
  474. int nghttp2_stream_dep_find_ancestor(nghttp2_stream *stream,
  475. nghttp2_stream *target) {
  476. for (; stream; stream = stream->dep_prev) {
  477. if (stream == target) {
  478. return 1;
  479. }
  480. }
  481. return 0;
  482. }
  483. int nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
  484. nghttp2_stream *stream) {
  485. nghttp2_stream *si;
  486. int rv;
  487. DEBUGF("stream: dep_insert dep_stream(%p)=%d, stream(%p)=%d\n", dep_stream,
  488. dep_stream->stream_id, stream, stream->stream_id);
  489. stream->sum_dep_weight = dep_stream->sum_dep_weight;
  490. dep_stream->sum_dep_weight = stream->weight;
  491. if (dep_stream->dep_next) {
  492. for (si = dep_stream->dep_next; si; si = si->sib_next) {
  493. si->dep_prev = stream;
  494. if (si->queued) {
  495. rv = stream_obq_move(stream, dep_stream, si);
  496. if (rv != 0) {
  497. return rv;
  498. }
  499. }
  500. }
  501. if (stream_subtree_active(stream)) {
  502. rv = stream_obq_push(dep_stream, stream);
  503. if (rv != 0) {
  504. return rv;
  505. }
  506. }
  507. stream->dep_next = dep_stream->dep_next;
  508. }
  509. dep_stream->dep_next = stream;
  510. stream->dep_prev = dep_stream;
  511. validate_tree(stream);
  512. return 0;
  513. }
  514. static void set_dep_prev(nghttp2_stream *stream, nghttp2_stream *dep) {
  515. for (; stream; stream = stream->sib_next) {
  516. stream->dep_prev = dep;
  517. }
  518. }
  519. static void link_dep(nghttp2_stream *dep_stream, nghttp2_stream *stream) {
  520. dep_stream->dep_next = stream;
  521. if (stream) {
  522. stream->dep_prev = dep_stream;
  523. }
  524. }
  525. static void link_sib(nghttp2_stream *a, nghttp2_stream *b) {
  526. a->sib_next = b;
  527. if (b) {
  528. b->sib_prev = a;
  529. }
  530. }
  531. static void insert_link_dep(nghttp2_stream *dep_stream,
  532. nghttp2_stream *stream) {
  533. nghttp2_stream *sib_next;
  534. assert(stream->sib_prev == NULL);
  535. sib_next = dep_stream->dep_next;
  536. link_sib(stream, sib_next);
  537. link_dep(dep_stream, stream);
  538. }
  539. static void unlink_sib(nghttp2_stream *stream) {
  540. nghttp2_stream *prev, *next, *dep_next;
  541. prev = stream->sib_prev;
  542. dep_next = stream->dep_next;
  543. assert(prev);
  544. if (dep_next) {
  545. /*
  546. * prev--stream(--sib_next--...)
  547. * |
  548. * dep_next
  549. */
  550. link_sib(prev, dep_next);
  551. set_dep_prev(dep_next, stream->dep_prev);
  552. if (stream->sib_next) {
  553. link_sib(stream_last_sib(dep_next), stream->sib_next);
  554. }
  555. } else {
  556. /*
  557. * prev--stream(--sib_next--...)
  558. */
  559. next = stream->sib_next;
  560. prev->sib_next = next;
  561. if (next) {
  562. next->sib_prev = prev;
  563. }
  564. }
  565. }
  566. static void unlink_dep(nghttp2_stream *stream) {
  567. nghttp2_stream *prev, *next, *dep_next;
  568. prev = stream->dep_prev;
  569. dep_next = stream->dep_next;
  570. assert(prev);
  571. if (dep_next) {
  572. /*
  573. * prev
  574. * |
  575. * stream(--sib_next--...)
  576. * |
  577. * dep_next
  578. */
  579. link_dep(prev, dep_next);
  580. set_dep_prev(dep_next, stream->dep_prev);
  581. if (stream->sib_next) {
  582. link_sib(stream_last_sib(dep_next), stream->sib_next);
  583. }
  584. } else if (stream->sib_next) {
  585. /*
  586. * prev
  587. * |
  588. * stream--sib_next
  589. */
  590. next = stream->sib_next;
  591. next->sib_prev = NULL;
  592. link_dep(prev, next);
  593. } else {
  594. prev->dep_next = NULL;
  595. }
  596. }
  597. void nghttp2_stream_dep_add(nghttp2_stream *dep_stream,
  598. nghttp2_stream *stream) {
  599. DEBUGF("stream: dep_add dep_stream(%p)=%d, stream(%p)=%d\n", dep_stream,
  600. dep_stream->stream_id, stream, stream->stream_id);
  601. dep_stream->sum_dep_weight += stream->weight;
  602. if (dep_stream->dep_next == NULL) {
  603. link_dep(dep_stream, stream);
  604. } else {
  605. insert_link_dep(dep_stream, stream);
  606. }
  607. validate_tree(stream);
  608. }
  609. int nghttp2_stream_dep_remove(nghttp2_stream *stream) {
  610. nghttp2_stream *dep_prev, *si;
  611. int32_t sum_dep_weight_delta;
  612. int rv;
  613. DEBUGF("stream: dep_remove stream(%p)=%d\n", stream, stream->stream_id);
  614. /* Distribute weight of |stream| to direct descendants */
  615. sum_dep_weight_delta = -stream->weight;
  616. for (si = stream->dep_next; si; si = si->sib_next) {
  617. si->weight = nghttp2_stream_dep_distributed_weight(stream, si->weight);
  618. sum_dep_weight_delta += si->weight;
  619. if (si->queued) {
  620. rv = stream_obq_move(stream->dep_prev, stream, si);
  621. if (rv != 0) {
  622. return rv;
  623. }
  624. }
  625. }
  626. assert(stream->dep_prev);
  627. dep_prev = stream->dep_prev;
  628. dep_prev->sum_dep_weight += sum_dep_weight_delta;
  629. if (stream->queued) {
  630. stream_obq_remove(stream);
  631. }
  632. if (stream->sib_prev) {
  633. unlink_sib(stream);
  634. } else {
  635. unlink_dep(stream);
  636. }
  637. stream->sum_dep_weight = 0;
  638. stream->dep_prev = NULL;
  639. stream->dep_next = NULL;
  640. stream->sib_prev = NULL;
  641. stream->sib_next = NULL;
  642. validate_tree(dep_prev);
  643. return 0;
  644. }
  645. int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
  646. nghttp2_stream *stream) {
  647. nghttp2_stream *last_sib;
  648. nghttp2_stream *dep_next;
  649. nghttp2_stream *si;
  650. int rv;
  651. DEBUGF("stream: dep_insert_subtree dep_stream(%p)=%d stream(%p)=%d\n",
  652. dep_stream, dep_stream->stream_id, stream, stream->stream_id);
  653. stream->sum_dep_weight += dep_stream->sum_dep_weight;
  654. dep_stream->sum_dep_weight = stream->weight;
  655. if (dep_stream->dep_next) {
  656. dep_next = dep_stream->dep_next;
  657. link_dep(dep_stream, stream);
  658. if (stream->dep_next) {
  659. last_sib = stream_last_sib(stream->dep_next);
  660. link_sib(last_sib, dep_next);
  661. } else {
  662. link_dep(stream, dep_next);
  663. }
  664. for (si = dep_next; si; si = si->sib_next) {
  665. si->dep_prev = stream;
  666. if (si->queued) {
  667. rv = stream_obq_move(stream, dep_stream, si);
  668. if (rv != 0) {
  669. return rv;
  670. }
  671. }
  672. }
  673. } else {
  674. link_dep(dep_stream, stream);
  675. }
  676. if (stream_subtree_active(stream)) {
  677. rv = stream_obq_push(dep_stream, stream);
  678. if (rv != 0) {
  679. return rv;
  680. }
  681. }
  682. validate_tree(dep_stream);
  683. return 0;
  684. }
  685. int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
  686. nghttp2_stream *stream) {
  687. int rv;
  688. DEBUGF("stream: dep_add_subtree dep_stream(%p)=%d stream(%p)=%d\n",
  689. dep_stream, dep_stream->stream_id, stream, stream->stream_id);
  690. dep_stream->sum_dep_weight += stream->weight;
  691. if (dep_stream->dep_next) {
  692. insert_link_dep(dep_stream, stream);
  693. } else {
  694. link_dep(dep_stream, stream);
  695. }
  696. if (stream_subtree_active(stream)) {
  697. rv = stream_obq_push(dep_stream, stream);
  698. if (rv != 0) {
  699. return rv;
  700. }
  701. }
  702. validate_tree(dep_stream);
  703. return 0;
  704. }
  705. void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream) {
  706. nghttp2_stream *next, *dep_prev;
  707. DEBUGF("stream: dep_remove_subtree stream(%p)=%d\n", stream,
  708. stream->stream_id);
  709. assert(stream->dep_prev);
  710. dep_prev = stream->dep_prev;
  711. if (stream->sib_prev) {
  712. link_sib(stream->sib_prev, stream->sib_next);
  713. } else {
  714. next = stream->sib_next;
  715. link_dep(dep_prev, next);
  716. if (next) {
  717. next->sib_prev = NULL;
  718. }
  719. }
  720. dep_prev->sum_dep_weight -= stream->weight;
  721. if (stream->queued) {
  722. stream_obq_remove(stream);
  723. }
  724. validate_tree(dep_prev);
  725. stream->sib_prev = NULL;
  726. stream->sib_next = NULL;
  727. stream->dep_prev = NULL;
  728. }
  729. int nghttp2_stream_in_dep_tree(nghttp2_stream *stream) {
  730. return stream->dep_prev || stream->dep_next || stream->sib_prev ||
  731. stream->sib_next;
  732. }
  733. nghttp2_outbound_item *
  734. nghttp2_stream_next_outbound_item(nghttp2_stream *stream) {
  735. nghttp2_pq_entry *ent;
  736. nghttp2_stream *si;
  737. for (;;) {
  738. if (stream_active(stream)) {
  739. /* Update ascendant's descendant_last_cycle here, so that we can
  740. assure that new stream is scheduled based on it. */
  741. for (si = stream; si->dep_prev; si = si->dep_prev) {
  742. si->dep_prev->descendant_last_cycle = si->cycle;
  743. }
  744. return stream->item;
  745. }
  746. ent = nghttp2_pq_top(&stream->obq);
  747. if (!ent) {
  748. return NULL;
  749. }
  750. stream = nghttp2_struct_of(ent, nghttp2_stream, pq_entry);
  751. }
  752. }
  753. nghttp2_stream_proto_state nghttp2_stream_get_state(nghttp2_stream *stream) {
  754. if (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) {
  755. return NGHTTP2_STREAM_STATE_CLOSED;
  756. }
  757. if (stream->flags & NGHTTP2_STREAM_FLAG_PUSH) {
  758. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  759. return NGHTTP2_STREAM_STATE_RESERVED_LOCAL;
  760. }
  761. if (stream->shut_flags & NGHTTP2_SHUT_WR) {
  762. return NGHTTP2_STREAM_STATE_RESERVED_REMOTE;
  763. }
  764. }
  765. if (stream->shut_flags & NGHTTP2_SHUT_RD) {
  766. return NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE;
  767. }
  768. if (stream->shut_flags & NGHTTP2_SHUT_WR) {
  769. return NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL;
  770. }
  771. if (stream->state == NGHTTP2_STREAM_IDLE) {
  772. return NGHTTP2_STREAM_STATE_IDLE;
  773. }
  774. return NGHTTP2_STREAM_STATE_OPEN;
  775. }
  776. nghttp2_stream *nghttp2_stream_get_parent(nghttp2_stream *stream) {
  777. return stream->dep_prev;
  778. }
  779. nghttp2_stream *nghttp2_stream_get_next_sibling(nghttp2_stream *stream) {
  780. return stream->sib_next;
  781. }
  782. nghttp2_stream *nghttp2_stream_get_previous_sibling(nghttp2_stream *stream) {
  783. return stream->sib_prev;
  784. }
  785. nghttp2_stream *nghttp2_stream_get_first_child(nghttp2_stream *stream) {
  786. return stream->dep_next;
  787. }
  788. int32_t nghttp2_stream_get_weight(nghttp2_stream *stream) {
  789. return stream->weight;
  790. }
  791. int32_t nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream) {
  792. return stream->sum_dep_weight;
  793. }
  794. int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream) {
  795. return stream->stream_id;
  796. }