1 | /*
|
---|
2 | * Copyright (c) 2015 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 | /**
|
---|
34 | * @file Global segment receive queue
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/prodcons.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <fibril.h>
|
---|
43 | #include <fibril_synch.h>
|
---|
44 | #include "conn.h"
|
---|
45 | #include "pdu.h"
|
---|
46 | #include "rqueue.h"
|
---|
47 | #include "segment.h"
|
---|
48 | #include "tcp_type.h"
|
---|
49 | #include "ucall.h"
|
---|
50 |
|
---|
51 | /** Transcode bounced segments.
|
---|
52 | *
|
---|
53 | * If defined, segments bounced via the internal debugging loopback will
|
---|
54 | * be encoded to a PDU and the decoded. Otherwise they will be bounced back
|
---|
55 | * directly without passing the encoder-decoder.
|
---|
56 | */
|
---|
57 | #define BOUNCE_TRANSCODE
|
---|
58 |
|
---|
59 | static prodcons_t rqueue;
|
---|
60 | static bool fibril_active;
|
---|
61 | static fibril_mutex_t lock;
|
---|
62 | static fibril_condvar_t cv;
|
---|
63 | static tcp_rqueue_cb_t *rqueue_cb;
|
---|
64 |
|
---|
65 | /** Initialize segment receive queue. */
|
---|
66 | void tcp_rqueue_init(tcp_rqueue_cb_t *rcb)
|
---|
67 | {
|
---|
68 | prodcons_initialize(&rqueue);
|
---|
69 | fibril_mutex_initialize(&lock);
|
---|
70 | fibril_condvar_initialize(&cv);
|
---|
71 | fibril_active = false;
|
---|
72 | rqueue_cb = rcb;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Finalize segment receive queue. */
|
---|
76 | void tcp_rqueue_fini(void)
|
---|
77 | {
|
---|
78 | inet_ep2_t epp;
|
---|
79 |
|
---|
80 | inet_ep2_init(&epp);
|
---|
81 | tcp_rqueue_insert_seg(&epp, NULL);
|
---|
82 |
|
---|
83 | fibril_mutex_lock(&lock);
|
---|
84 | while (fibril_active)
|
---|
85 | fibril_condvar_wait(&cv, &lock);
|
---|
86 | fibril_mutex_unlock(&lock);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Bounce segment directy into receive queue without constructing the PDU.
|
---|
90 | *
|
---|
91 | * This is for testing purposes only.
|
---|
92 | *
|
---|
93 | * @param sp Endpoint pair, oriented for transmission
|
---|
94 | * @param seg Segment
|
---|
95 | */
|
---|
96 | void tcp_rqueue_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
97 | {
|
---|
98 | inet_ep2_t rident;
|
---|
99 |
|
---|
100 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_bounce_seg()");
|
---|
101 |
|
---|
102 | #ifdef BOUNCE_TRANSCODE
|
---|
103 | tcp_pdu_t *pdu;
|
---|
104 | tcp_segment_t *dseg;
|
---|
105 |
|
---|
106 | if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
|
---|
107 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
---|
112 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | tcp_pdu_delete(pdu);
|
---|
117 |
|
---|
118 | /** Insert decoded segment into rqueue */
|
---|
119 | tcp_rqueue_insert_seg(&rident, dseg);
|
---|
120 | tcp_segment_delete(seg);
|
---|
121 | #else
|
---|
122 | /* Reverse the identification */
|
---|
123 | tcp_ep2_flipped(epp, &rident);
|
---|
124 |
|
---|
125 | /* Insert segment back into rqueue */
|
---|
126 | tcp_rqueue_insert_seg(&rident, seg);
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Insert segment into receive queue.
|
---|
131 | *
|
---|
132 | * @param epp Endpoint pair, oriented for reception
|
---|
133 | * @param seg Segment
|
---|
134 | */
|
---|
135 | void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
136 | {
|
---|
137 | tcp_rqueue_entry_t *rqe;
|
---|
138 |
|
---|
139 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_insert_seg()");
|
---|
140 |
|
---|
141 | if (seg != NULL)
|
---|
142 | tcp_segment_dump(seg);
|
---|
143 |
|
---|
144 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
---|
145 | if (rqe == NULL) {
|
---|
146 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating RQE.");
|
---|
147 | return;
|
---|
148 | }
|
---|
149 |
|
---|
150 | rqe->epp = *epp;
|
---|
151 | rqe->seg = seg;
|
---|
152 |
|
---|
153 | prodcons_produce(&rqueue, &rqe->link);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** Receive queue handler fibril. */
|
---|
157 | static int tcp_rqueue_fibril(void *arg)
|
---|
158 | {
|
---|
159 | link_t *link;
|
---|
160 | tcp_rqueue_entry_t *rqe;
|
---|
161 |
|
---|
162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril()");
|
---|
163 |
|
---|
164 | while (true) {
|
---|
165 | link = prodcons_consume(&rqueue);
|
---|
166 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
---|
167 |
|
---|
168 | if (rqe->seg == NULL) {
|
---|
169 | free(rqe);
|
---|
170 | break;
|
---|
171 | }
|
---|
172 |
|
---|
173 | rqueue_cb->seg_received(&rqe->epp, rqe->seg);
|
---|
174 | free(rqe);
|
---|
175 | }
|
---|
176 |
|
---|
177 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_fibril() exiting");
|
---|
178 |
|
---|
179 | /* Finished */
|
---|
180 | fibril_mutex_lock(&lock);
|
---|
181 | fibril_active = false;
|
---|
182 | fibril_mutex_unlock(&lock);
|
---|
183 | fibril_condvar_broadcast(&cv);
|
---|
184 |
|
---|
185 | return 0;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Start receive queue handler fibril. */
|
---|
189 | void tcp_rqueue_fibril_start(void)
|
---|
190 | {
|
---|
191 | fid_t fid;
|
---|
192 |
|
---|
193 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril_start()");
|
---|
194 |
|
---|
195 | fid = fibril_create(tcp_rqueue_fibril, NULL);
|
---|
196 | if (fid == 0) {
|
---|
197 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating rqueue fibril.");
|
---|
198 | return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | fibril_add_ready(fid);
|
---|
202 | fibril_active = true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * @}
|
---|
207 | */
|
---|