source: mainline/uspace/srv/net/tcp/rqueue.c@ 4802dd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4802dd7 was 88a6819, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Use fibrils instead of threads for background work in TCP.

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[c5808b41]1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup tcp
30 * @{
31 */
32
33/**
[032bbe7]34 * @file Global segment receive queue
[c5808b41]35 */
36
37#include <adt/prodcons.h>
38#include <errno.h>
39#include <io/log.h>
40#include <stdlib.h>
[88a6819]41#include <fibril.h>
[032bbe7]42#include "conn.h"
[762b48a]43#include "pdu.h"
[c5808b41]44#include "rqueue.h"
[eea65f4]45#include "segment.h"
[c5808b41]46#include "tcp_type.h"
[762b48a]47#include "ucall.h"
[c5808b41]48
[eea65f4]49/** Transcode bounced segments.
50 *
51 * If defined, segments bounced via the internal debugging loopback will
52 * be encoded to a PDU and the decoded. Otherwise they will be bounced back
53 * directly without passing the encoder-decoder.
54 */
55#define BOUNCE_TRANSCODE
56
[c5808b41]57static prodcons_t rqueue;
58
[032bbe7]59/** Initialize segment receive queue. */
[c5808b41]60void tcp_rqueue_init(void)
61{
62 prodcons_initialize(&rqueue);
63}
64
65/** Bounce segment directy into receive queue without constructing the PDU.
66 *
67 * This is for testing purposes only.
[032bbe7]68 *
69 * @param sp Socket pair, oriented for transmission
70 * @param seg Segment
[c5808b41]71 */
72void tcp_rqueue_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
73{
74 tcp_sockpair_t rident;
75
76 log_msg(LVL_DEBUG, "tcp_rqueue_bounce_seg()");
77
[eea65f4]78#ifdef BOUNCE_TRANSCODE
79 tcp_pdu_t *pdu;
80 tcp_segment_t *dseg;
81
82 if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
83 log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
84 return;
85 }
86
87 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
88 log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
89 return;
90 }
91
92 tcp_pdu_delete(pdu);
93
94 /** Insert decoded segment into rqueue */
95 tcp_rqueue_insert_seg(&rident, dseg);
96 tcp_segment_delete(seg);
97#else
[c5808b41]98 /* Reverse the identification */
[032bbe7]99 tcp_sockpair_flipped(sp, &rident);
[c5808b41]100
[eea65f4]101 /* Insert segment back into rqueue */
[c5808b41]102 tcp_rqueue_insert_seg(&rident, seg);
[eea65f4]103#endif
[c5808b41]104}
105
[032bbe7]106/** Insert segment into receive queue.
107 *
108 * @param sp Socket pair, oriented for reception
109 * @param seg Segment
110 */
[c5808b41]111void tcp_rqueue_insert_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
112{
113 tcp_rqueue_entry_t *rqe;
114 log_msg(LVL_DEBUG, "tcp_rqueue_insert_seg()");
115
[6896409c]116 tcp_segment_dump(seg);
117
[c5808b41]118 rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
119 if (rqe == NULL) {
120 log_msg(LVL_ERROR, "Failed allocating RQE.");
121 return;
122 }
123
124 rqe->sp = *sp;
125 rqe->seg = seg;
126
127 prodcons_produce(&rqueue, &rqe->link);
128}
129
[88a6819]130/** Receive queue handler fibril. */
131static int tcp_rqueue_fibril(void *arg)
[c5808b41]132{
133 link_t *link;
134 tcp_rqueue_entry_t *rqe;
135
[88a6819]136 log_msg(LVL_DEBUG, "tcp_rqueue_fibril()");
[c5808b41]137
138 while (true) {
139 link = prodcons_consume(&rqueue);
140 rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
141
142 tcp_as_segment_arrived(&rqe->sp, rqe->seg);
143 }
[88a6819]144
145 /* Not reached */
146 return 0;
[c5808b41]147}
148
[88a6819]149/** Start receive queue handler fibril. */
150void tcp_rqueue_fibril_start(void)
[c5808b41]151{
[88a6819]152 fid_t fid;
[c5808b41]153
[88a6819]154 log_msg(LVL_DEBUG, "tcp_rqueue_fibril_start()");
[c5808b41]155
[88a6819]156 fid = fibril_create(tcp_rqueue_fibril, NULL);
157 if (fid == 0) {
158 log_msg(LVL_ERROR, "Failed creating rqueue fibril.");
[c5808b41]159 return;
160 }
[88a6819]161
162 fibril_add_ready(fid);
[c5808b41]163}
164
165/**
166 * @}
167 */
Note: See TracBrowser for help on using the repository browser.