+static u32
+ptls_tcp_to_app_write (picotls_ctx_t *ptls_ctx, svm_fifo_t *app_rx_fifo,
+ svm_fifo_t *tcp_rx_fifo)
+{
+ u32 ai = 0, thread_index, min_buf_len, to_copy, left, wrote = 0;
+ ptls_buffer_t *buf = &ptls_ctx->read_buffer;
+ int ret, i = 0, read = 0, tcp_len, n_fs_app;
+ const int n_segs = 4, max_len = 1 << 16;
+ svm_fifo_seg_t tcp_fs[n_segs], app_fs[n_segs];
+ picotls_main_t *pm = &picotls_main;
+ uword deq_now;
+ u8 is_nocopy;
+
+ thread_index = ptls_ctx->ctx.c_thread_index;
+
+ n_fs_app = svm_fifo_provision_chunks (app_rx_fifo, app_fs, n_segs, max_len);
+ if (n_fs_app <= 0)
+ return 0;
+
+ tcp_len = svm_fifo_segments (tcp_rx_fifo, 0, tcp_fs, n_segs, max_len);
+ if (tcp_len <= 0)
+ return 0;
+
+ if (ptls_ctx->read_buffer_offset)
+ {
+ to_copy = buf->off - ptls_ctx->read_buffer_offset;
+ left = ptls_copy_buf_to_fs (buf, to_copy, app_fs, &ai, n_fs_app);
+ wrote += to_copy - left;
+ if (left)
+ {
+ ptls_ctx->read_buffer_offset = buf->off - left;
+ goto do_checks;
+ }
+ ptls_ctx->read_buffer_offset = 0;
+ }
+
+ while (ai < n_fs_app && read < tcp_len)
+ {
+ deq_now = clib_min (tcp_fs[i].len, tcp_len - read);
+ min_buf_len = deq_now + (16 << 10);
+ is_nocopy = app_fs[ai].len < min_buf_len ? 0 : 1;
+ if (is_nocopy)
+ {
+ ptls_buffer_init (buf, app_fs[ai].data, app_fs[ai].len);
+ ret = ptls_receive (ptls_ctx->tls, buf, tcp_fs[i].data, &deq_now);
+ assert (ret == 0 || ret == PTLS_ERROR_IN_PROGRESS);
+
+ wrote += buf->off;
+ if (buf->off == app_fs[ai].len)
+ {
+ ai++;
+ }
+ else
+ {
+ app_fs[ai].len -= buf->off;
+ app_fs[ai].data += buf->off;
+ }
+ }
+ else
+ {
+ vec_validate (pm->rx_bufs[thread_index], min_buf_len);
+ ptls_buffer_init (buf, pm->rx_bufs[thread_index], min_buf_len);
+ ret = ptls_receive (ptls_ctx->tls, buf, tcp_fs[i].data, &deq_now);
+ assert (ret == 0 || ret == PTLS_ERROR_IN_PROGRESS);
+
+ left = ptls_copy_buf_to_fs (buf, buf->off, app_fs, &ai, n_fs_app);
+ if (!left)
+ {
+ ptls_ctx->read_buffer_offset = 0;
+ wrote += buf->off;
+ }
+ else
+ {
+ ptls_ctx->read_buffer_offset = buf->off - left;
+ wrote += ptls_ctx->read_buffer_offset;
+ }
+ }
+
+ assert (deq_now <= tcp_fs[i].len);
+ read += deq_now;
+ if (deq_now < tcp_fs[i].len)
+ {
+ tcp_fs[i].data += deq_now;
+ tcp_fs[i].len -= deq_now;
+ }
+ else
+ i++;
+ }
+
+do_checks:
+
+ if (read)
+ {
+ svm_fifo_dequeue_drop (tcp_rx_fifo, read);
+ if (svm_fifo_needs_deq_ntf (tcp_rx_fifo, read))
+ {
+ svm_fifo_clear_deq_ntf (tcp_rx_fifo);
+ session_send_io_evt_to_thread (tcp_rx_fifo, SESSION_IO_EVT_RX);
+ }
+ }
+
+ if (wrote)
+ svm_fifo_enqueue_nocopy (app_rx_fifo, wrote);
+
+ return wrote;
+}
+